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

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)