a50ed529c8b6990c328c2cd623631f4e36222001
[openocd.git] / src / jtag / zy1000.c
1 /***************************************************************************
2 * Copyright (C) 2007-2008 by Øyvind Harboe *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23
24 #include "log.h"
25 #include "jtag.h"
26 #include "bitbang.h"
27 #include "../target/embeddedice.h"
28
29
30 #include <cyg/hal/hal_io.h> // low level i/o
31 #include <cyg/hal/hal_diag.h>
32
33 #include <stdlib.h>
34
35
36 extern int jtag_error;
37
38 /* low level command set
39 */
40 int zy1000_read(void);
41 static void zy1000_write(int tck, int tms, int tdi);
42 void zy1000_reset(int trst, int srst);
43
44
45 int zy1000_speed(int speed);
46 int zy1000_register_commands(struct command_context_s *cmd_ctx);
47 int zy1000_init(void);
48 int zy1000_quit(void);
49
50 /* interface commands */
51 int zy1000_handle_zy1000_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52
53 static int zy1000_khz(int khz, int *jtag_speed)
54 {
55 if (khz==0)
56 {
57 *jtag_speed=0;
58 }
59 else
60 {
61 *jtag_speed=64000/khz;
62 }
63 return ERROR_OK;
64 }
65
66 static int zy1000_speed_div(int speed, int *khz)
67 {
68 if (speed==0)
69 {
70 *khz = 0;
71 }
72 else
73 {
74 *khz=64000/speed;
75 }
76
77 return ERROR_OK;
78 }
79
80 static bool readPowerDropout()
81 {
82 cyg_uint32 state;
83 // sample and clear power dropout
84 HAL_WRITE_UINT32(0x08000010, 0x80);
85 HAL_READ_UINT32(0x08000010, state);
86 bool powerDropout;
87 powerDropout = (state & 0x80) != 0;
88 return powerDropout;
89 }
90
91
92 static bool readSRST()
93 {
94 cyg_uint32 state;
95 // sample and clear SRST sensing
96 HAL_WRITE_UINT32(0x08000010, 0x00000040);
97 HAL_READ_UINT32(0x08000010, state);
98 bool srstAsserted;
99 srstAsserted = (state & 0x40) != 0;
100 return srstAsserted;
101 }
102
103 static int zy1000_srst_asserted(int *srst_asserted)
104 {
105 *srst_asserted=readSRST();
106 return ERROR_OK;
107 }
108
109 static int zy1000_power_dropout(int *dropout)
110 {
111 *dropout=readPowerDropout();
112 return ERROR_OK;
113 }
114
115
116 jtag_interface_t zy1000_interface =
117 {
118 .name = "ZY1000",
119 .execute_queue = bitbang_execute_queue,
120 .speed = zy1000_speed,
121 .register_commands = zy1000_register_commands,
122 .init = zy1000_init,
123 .quit = zy1000_quit,
124 .khz = zy1000_khz,
125 .speed_div = zy1000_speed_div,
126 .power_dropout = zy1000_power_dropout,
127 .srst_asserted = zy1000_srst_asserted,
128 };
129
130 bitbang_interface_t zy1000_bitbang =
131 {
132 .read = zy1000_read,
133 .write = zy1000_write,
134 .reset = zy1000_reset
135 };
136
137
138
139 static void zy1000_write(int tck, int tms, int tdi)
140 {
141
142 }
143
144 int zy1000_read(void)
145 {
146 return -1;
147 }
148
149 extern bool readSRST();
150
151 void zy1000_reset(int trst, int srst)
152 {
153 LOG_DEBUG("zy1000 trst=%d, srst=%d", trst, srst);
154 if(!srst)
155 {
156 ZY1000_POKE(0x08000014, 0x00000001);
157 }
158 else
159 {
160 /* Danger!!! if clk!=0 when in
161 * idle in TAP_RTI, reset halt on str912 will fail.
162 */
163 ZY1000_POKE(0x08000010, 0x00000001);
164 }
165
166 if(!trst)
167 {
168 ZY1000_POKE(0x08000014, 0x00000002);
169 }
170 else
171 {
172 /* assert reset */
173 ZY1000_POKE(0x08000010, 0x00000002);
174 }
175
176 if (trst||(srst&&(jtag_reset_config & RESET_SRST_PULLS_TRST)))
177 {
178 waitIdle();
179 /* we're now in the TLR state until trst is deasserted */
180 ZY1000_POKE(0x08000020, TAP_TLR);
181 } else
182 {
183 /* We'll get RCLK failure when we assert TRST, so clear any false positives here */
184 ZY1000_POKE(0x08000014, 0x400);
185 }
186
187 /* wait for srst to float back up */
188 if (!srst)
189 {
190 int i;
191 for (i=0; i<1000; i++)
192 {
193 // We don't want to sense our own reset, so we clear here.
194 // There is of course a timing hole where we could loose
195 // a "real" reset.
196 if (!readSRST())
197 break;
198
199 /* wait 1ms */
200 alive_sleep(1);
201 }
202
203 if (i==1000)
204 {
205 LOG_USER("SRST didn't deassert after %dms", i);
206 } else if (i>1)
207 {
208 LOG_USER("SRST took %dms to deassert", i);
209 }
210 }
211 }
212
213 int zy1000_speed(int speed)
214 {
215 if(speed == 0)
216 {
217 /*0 means RCLK*/
218 speed = 0;
219 ZY1000_POKE(0x08000010, 0x100);
220 LOG_DEBUG("jtag_speed using RCLK");
221 }
222 else
223 {
224 if(speed > 8190 || speed < 2)
225 {
226 LOG_ERROR("valid ZY1000 jtag_speed=[8190,2]. Divisor is 64MHz / even values between 8190-2, i.e. min 7814Hz, max 32MHz");
227 return ERROR_INVALID_ARGUMENTS;
228 }
229
230 LOG_USER("jtag_speed %d => JTAG clk=%f", speed, 64.0/(float)speed);
231 ZY1000_POKE(0x08000014, 0x100);
232 ZY1000_POKE(0x0800001c, speed&~1);
233 }
234 return ERROR_OK;
235 }
236
237 int zy1000_register_commands(struct command_context_s *cmd_ctx)
238 {
239 return ERROR_OK;
240 }
241
242
243 int zy1000_init(void)
244 {
245 ZY1000_POKE(0x08000010, 0x30); // Turn on LED1 & LED2
246
247 /* deassert resets. Important to avoid infinite loop waiting for SRST to deassert */
248 zy1000_reset(0, 0);
249 zy1000_speed(jtag_speed);
250
251 bitbang_interface = &zy1000_bitbang;
252
253 return ERROR_OK;
254 }
255
256 int zy1000_quit(void)
257 {
258
259 return ERROR_OK;
260 }
261
262
263
264 /* loads a file and returns a pointer to it in memory. The file contains
265 * a 0 byte(sentinel) after len bytes - the length of the file. */
266 int loadFile(const char *fileName, void **data, int *len)
267 {
268 FILE * pFile;
269 pFile = fopen (fileName,"rb");
270 if (pFile==NULL)
271 {
272 LOG_ERROR("Can't open %s\n", fileName);
273 return ERROR_JTAG_DEVICE_ERROR;
274 }
275 if (fseek (pFile, 0, SEEK_END)!=0)
276 {
277 LOG_ERROR("Can't open %s\n", fileName);
278 fclose(pFile);
279 return ERROR_JTAG_DEVICE_ERROR;
280 }
281 *len=ftell (pFile);
282 if (*len==-1)
283 {
284 LOG_ERROR("Can't open %s\n", fileName);
285 fclose(pFile);
286 return ERROR_JTAG_DEVICE_ERROR;
287 }
288
289 if (fseek (pFile, 0, SEEK_SET)!=0)
290 {
291 LOG_ERROR("Can't open %s\n", fileName);
292 fclose(pFile);
293 return ERROR_JTAG_DEVICE_ERROR;
294 }
295 *data=malloc(*len+1);
296 if (*data==NULL)
297 {
298 LOG_ERROR("Can't open %s\n", fileName);
299 fclose(pFile);
300 return ERROR_JTAG_DEVICE_ERROR;
301 }
302
303 if (fread(*data, 1, *len, pFile)!=*len)
304 {
305 fclose(pFile);
306 free(*data);
307 LOG_ERROR("Can't open %s\n", fileName);
308 return ERROR_JTAG_DEVICE_ERROR;
309 }
310 fclose (pFile);
311 *(((char *)(*data))+*len)=0; /* sentinel */
312
313 return ERROR_OK;
314
315
316
317 }
318
319
320
321
322 int interface_jtag_execute_queue(void)
323 {
324 cyg_uint32 empty;
325
326 waitIdle();
327 ZY1000_PEEK(0x08000010, empty);
328 /* clear JTAG error register */
329 ZY1000_POKE(0x08000014, 0x400);
330
331 if ((empty&0x400)!=0)
332 {
333 LOG_WARNING("RCLK timeout");
334 /* the error is informative only as we don't want to break the firmware if there
335 * is a false positive.
336 */
337 // return ERROR_FAIL;
338 }
339 return ERROR_OK;
340 }
341
342
343
344
345
346 static cyg_uint32 getShiftValue()
347 {
348 cyg_uint32 value;
349 waitIdle();
350 ZY1000_PEEK(0x0800000c, value);
351 VERBOSE(LOG_INFO("getShiftValue %08x", value));
352 return value;
353 }
354 #if 0
355 static cyg_uint32 getShiftValueFlip()
356 {
357 cyg_uint32 value;
358 waitIdle();
359 ZY1000_PEEK(0x08000018, value);
360 VERBOSE(LOG_INFO("getShiftValue %08x (flipped)", value));
361 return value;
362 }
363 #endif
364
365 #if 0
366 static void shiftValueInnerFlip(const enum tap_state state, const enum tap_state endState, int repeat, cyg_uint32 value)
367 {
368 VERBOSE(LOG_INFO("shiftValueInner %s %s %d %08x (flipped)", tap_state_strings[state], tap_state_strings[endState], repeat, value));
369 cyg_uint32 a,b;
370 a=state;
371 b=endState;
372 ZY1000_POKE(0x0800000c, value);
373 ZY1000_POKE(0x08000008, (1<<15)|(repeat<<8)|(a<<4)|b);
374 VERBOSE(getShiftValueFlip());
375 }
376 #endif
377
378 extern int jtag_check_value(u8 *captured, void *priv);
379
380 static void gotoEndState()
381 {
382 setCurrentState(cmd_queue_end_state);
383 }
384
385 static __inline void scanFields(int num_fields, scan_field_t *fields, enum tap_state shiftState, int pause)
386 {
387 int i;
388 int j;
389 int k;
390
391 for (i = 0; i < num_fields; i++)
392 {
393 cyg_uint32 value;
394
395 static u8 *in_buff=NULL; /* pointer to buffer for scanned data */
396 static int in_buff_size=0;
397 u8 *inBuffer=NULL;
398
399
400 // figure out where to store the input data
401 int num_bits=fields[i].num_bits;
402 if (fields[i].in_value!=NULL)
403 {
404 inBuffer=fields[i].in_value;
405 } else if (fields[i].in_handler!=NULL)
406 {
407 if (in_buff_size*8<num_bits)
408 {
409 // we need more space
410 if (in_buff!=NULL)
411 free(in_buff);
412 in_buff=NULL;
413 in_buff_size=(num_bits+7)/8;
414 in_buff=malloc(in_buff_size);
415 if (in_buff==NULL)
416 {
417 LOG_ERROR("Out of memory");
418 jtag_error=ERROR_JTAG_QUEUE_FAILED;
419 return;
420 }
421 }
422 inBuffer=in_buff;
423 }
424
425 // here we shuffle N bits out/in
426 j=0;
427 while (j<num_bits)
428 {
429 enum tap_state pause_state;
430 int l;
431 k=num_bits-j;
432 pause_state=(shiftState==TAP_SD)?TAP_SD:TAP_SI;
433 if (k>32)
434 {
435 k=32;
436 /* we have more to shift out */
437 } else if (pause&&(i == num_fields-1))
438 {
439 /* this was the last to shift out this time */
440 pause_state=(shiftState==TAP_SD)?TAP_PD:TAP_PI;
441 }
442
443 // we have (num_bits+7)/8 bytes of bits to toggle out.
444 // bits are pushed out LSB to MSB
445 value=0;
446 if (fields[i].out_value!=NULL)
447 {
448 for (l=0; l<k; l+=8)
449 {
450 value|=fields[i].out_value[(j+l)/8]<<l;
451 }
452 }
453 /* mask away unused bits for easier debugging */
454 value&=~(((u32)0xffffffff)<<k);
455
456 shiftValueInner(shiftState, pause_state, k, value);
457
458 if (inBuffer!=NULL)
459 {
460 // data in, LSB to MSB
461 value=getShiftValue();
462 // we're shifting in data to MSB, shift data to be aligned for returning the value
463 value >>= 32-k;
464
465 for (l=0; l<k; l+=8)
466 {
467 inBuffer[(j+l)/8]=(value>>l)&0xff;
468 }
469 }
470 j+=k;
471 }
472
473 if (fields[i].in_handler!=NULL)
474 {
475 // invoke callback
476 int r=fields[i].in_handler(inBuffer, fields[i].in_handler_priv, fields+i);
477 if (r!=ERROR_OK)
478 {
479 /* this will cause jtag_execute_queue() to return an error */
480 jtag_error=r;
481 }
482 }
483 }
484 }
485
486 int interface_jtag_add_end_state(enum tap_state state)
487 {
488 return ERROR_OK;
489 }
490
491
492 int interface_jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
493 {
494
495 int i, j;
496 int scan_size = 0;
497 jtag_device_t *device;
498
499 for (i=0; i < jtag_num_devices; i++)
500 {
501 int pause=i==(jtag_num_devices-1);
502 int found = 0;
503 device = jtag_get_device(i);
504 scan_size = device->ir_length;
505
506 /* search the list */
507 for (j=0; j < num_fields; j++)
508 {
509 if (i == fields[j].device)
510 {
511 found = 1;
512
513 if ((jtag_verify_capture_ir)&&(fields[j].in_handler==NULL))
514 {
515 jtag_set_check_value(fields+j, device->expected, device->expected_mask, NULL);
516 } else if (jtag_verify_capture_ir)
517 {
518 fields[j].in_check_value = device->expected;
519 fields[j].in_check_mask = device->expected_mask;
520 }
521
522 scanFields(1, fields+j, TAP_SI, pause);
523 /* update device information */
524 buf_cpy(fields[j].out_value, jtag_get_device(i)->cur_instr, scan_size);
525
526 device->bypass = 0;
527 break;
528 }
529 }
530
531 if (!found)
532 {
533 /* if a device isn't listed, set it to BYPASS */
534 u8 ones[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
535
536 scan_field_t tmp;
537 memset(&tmp, 0, sizeof(tmp));
538 tmp.out_value = ones;
539 tmp.num_bits = scan_size;
540 scanFields(1, &tmp, TAP_SI, pause);
541 /* update device information */
542 buf_cpy(tmp.out_value, jtag_get_device(i)->cur_instr, scan_size);
543 device->bypass = 1;
544 }
545 }
546 gotoEndState();
547
548 return ERROR_OK;
549 }
550
551
552
553
554
555 int interface_jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
556 {
557 scanFields(num_fields, fields, TAP_SI, 1);
558 gotoEndState();
559
560 return ERROR_OK;
561 }
562
563 /*extern jtag_command_t **jtag_get_last_command_p(void);*/
564
565 int interface_jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
566 {
567 int i, j;
568 for (i=0; i < jtag_num_devices; i++)
569 {
570 int found = 0;
571 int pause = (i==(jtag_num_devices-1));
572
573 for (j=0; j < num_fields; j++)
574 {
575 if (i == fields[j].device)
576 {
577 found = 1;
578
579 scanFields(1, fields+j, TAP_SD, pause);
580 }
581 }
582 if (!found)
583 {
584 #ifdef _DEBUG_JTAG_IO_
585 /* if a device isn't listed, the BYPASS register should be selected */
586 if (!jtag_get_device(i)->bypass)
587 {
588 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
589 exit(-1);
590 }
591 #endif
592
593 scan_field_t tmp;
594 /* program the scan field to 1 bit length, and ignore it's value */
595 tmp.num_bits = 1;
596 tmp.out_value = NULL;
597 tmp.out_mask = NULL;
598 tmp.in_value = NULL;
599 tmp.in_check_value = NULL;
600 tmp.in_check_mask = NULL;
601 tmp.in_handler = NULL;
602 tmp.in_handler_priv = NULL;
603
604 scanFields(1, &tmp, TAP_SD, pause);
605 }
606 else
607 {
608 #ifdef _DEBUG_JTAG_IO_
609 /* if a device is listed, the BYPASS register must not be selected */
610 if (jtag_get_device(i)->bypass)
611 {
612 LOG_WARNING("scan data for a device in BYPASS");
613 }
614 #endif
615 }
616 }
617 gotoEndState();
618 return ERROR_OK;
619 }
620
621 int interface_jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
622 {
623 scanFields(num_fields, fields, TAP_SD, 1);
624 gotoEndState();
625 return ERROR_OK;
626 }
627
628
629 int interface_jtag_add_tlr()
630 {
631 setCurrentState(TAP_TLR);
632 return ERROR_OK;
633 }
634
635
636
637
638 extern int jtag_nsrst_delay;
639 extern int jtag_ntrst_delay;
640
641 int interface_jtag_add_reset(int req_trst, int req_srst)
642 {
643 zy1000_reset(req_trst, req_srst);
644 return ERROR_OK;
645 }
646
647 int interface_jtag_add_runtest(int num_cycles, enum tap_state state)
648 {
649 /* num_cycles can be 0 */
650 setCurrentState(TAP_RTI);
651
652 /* execute num_cycles, 32 at the time. */
653 int i;
654 for (i=0; i<num_cycles; i+=32)
655 {
656 int num;
657 num=32;
658 if (num_cycles-i<num)
659 {
660 num=num_cycles-i;
661 }
662 shiftValueInner(TAP_RTI, TAP_RTI, num, 0);
663 }
664
665 #if !TEST_MANUAL()
666 /* finish in end_state */
667 setCurrentState(state);
668 #else
669 enum tap_state t=TAP_RTI;
670 /* test manual drive code on any target */
671 int tms;
672 u8 tms_scan = TAP_MOVE(t, state);
673
674 for (i = 0; i < 7; i++)
675 {
676 tms = (tms_scan >> i) & 1;
677 waitIdle();
678 ZY1000_POKE(0x08000028, tms);
679 }
680 waitIdle();
681 ZY1000_POKE(0x08000020, state);
682 #endif
683
684
685 return ERROR_OK;
686 }
687
688 int interface_jtag_add_sleep(u32 us)
689 {
690 jtag_sleep(us);
691 return ERROR_OK;
692 }
693
694 int interface_jtag_add_pathmove(int num_states, enum tap_state *path)
695 {
696 int state_count;
697 int tms = 0;
698
699 /*wait for the fifo to be empty*/
700 waitIdle();
701
702 state_count = 0;
703
704 enum tap_state cur_state=cmd_queue_cur_state;
705
706 while (num_states)
707 {
708 if (tap_transitions[cur_state].low == path[state_count])
709 {
710 tms = 0;
711 }
712 else if (tap_transitions[cur_state].high == path[state_count])
713 {
714 tms = 1;
715 }
716 else
717 {
718 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[state_count]]);
719 exit(-1);
720 }
721
722 waitIdle();
723 ZY1000_POKE(0x08000028, tms);
724
725 cur_state = path[state_count];
726 state_count++;
727 num_states--;
728 }
729
730 waitIdle();
731 ZY1000_POKE(0x08000020, cur_state);
732 return ERROR_OK;
733 }
734
735
736
737 void embeddedice_write_dcc(int chain_pos, int reg_addr, u8 *buffer, int little, int count)
738 {
739 // static int const reg_addr=0x5;
740 enum tap_state end_state=cmd_queue_end_state;
741 if (jtag_num_devices==1)
742 {
743 /* better performance via code duplication */
744 if (little)
745 {
746 int i;
747 for (i = 0; i < count; i++)
748 {
749 shiftValueInner(TAP_SD, TAP_SD, 32, fast_target_buffer_get_u32(buffer, 1));
750 shiftValueInner(TAP_SD, end_state, 6, reg_addr|(1<<5));
751 buffer+=4;
752 }
753 } else
754 {
755 int i;
756 for (i = 0; i < count; i++)
757 {
758 shiftValueInner(TAP_SD, TAP_SD, 32, fast_target_buffer_get_u32(buffer, 0));
759 shiftValueInner(TAP_SD, end_state, 6, reg_addr|(1<<5));
760 buffer+=4;
761 }
762 }
763 }
764 else
765 {
766 int i;
767 for (i = 0; i < count; i++)
768 {
769 embeddedice_write_reg_inner(chain_pos, reg_addr, fast_target_buffer_get_u32(buffer, little));
770 buffer += 4;
771 }
772 }
773 }
774

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)