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