1 /***************************************************************************
2 * Copyright (C) 2007-2008 by Øyvind Harboe *
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. *
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. *
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 ***************************************************************************/
27 #include "../target/embeddedice.h"
30 #include <cyg/hal/hal_io.h> // low level i/o
31 #include <cyg/hal/hal_diag.h>
36 extern int jtag_error;
38 /* low level command set
40 int zy1000_read(void);
41 static void zy1000_write(int tck, int tms, int tdi);
42 void zy1000_reset(int trst, int srst);
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);
50 /* interface commands */
51 int zy1000_handle_zy1000_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 static int zy1000_khz(int khz, int *jtag_speed)
61 *jtag_speed=64000/khz;
66 static int zy1000_speed_div(int speed, int *khz)
80 static bool readPowerDropout()
83 // sample and clear power dropout
84 HAL_WRITE_UINT32(ZY1000_JTAG_BASE+0x10, 0x80);
85 HAL_READ_UINT32(ZY1000_JTAG_BASE+0x10, state);
87 powerDropout = (state & 0x80) != 0;
92 static bool readSRST()
95 // sample and clear SRST sensing
96 HAL_WRITE_UINT32(ZY1000_JTAG_BASE+0x10, 0x00000040);
97 HAL_READ_UINT32(ZY1000_JTAG_BASE+0x10, state);
99 srstAsserted = (state & 0x40) != 0;
103 static int zy1000_srst_asserted(int *srst_asserted)
105 *srst_asserted=readSRST();
109 static int zy1000_power_dropout(int *dropout)
111 *dropout=readPowerDropout();
116 jtag_interface_t zy1000_interface =
119 .execute_queue = bitbang_execute_queue,
120 .speed = zy1000_speed,
121 .register_commands = zy1000_register_commands,
125 .speed_div = zy1000_speed_div,
126 .power_dropout = zy1000_power_dropout,
127 .srst_asserted = zy1000_srst_asserted,
130 bitbang_interface_t zy1000_bitbang =
133 .write = zy1000_write,
134 .reset = zy1000_reset
139 static void zy1000_write(int tck, int tms, int tdi)
144 int zy1000_read(void)
149 extern bool readSRST();
151 void zy1000_reset(int trst, int srst)
153 LOG_DEBUG("zy1000 trst=%d, srst=%d", trst, srst);
156 ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x00000001);
160 /* Danger!!! if clk!=0 when in
161 * idle in TAP_IDLE, reset halt on str912 will fail.
163 ZY1000_POKE(ZY1000_JTAG_BASE+0x10, 0x00000001);
168 ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x00000002);
173 ZY1000_POKE(ZY1000_JTAG_BASE+0x10, 0x00000002);
176 if (trst||(srst&&(jtag_reset_config & RESET_SRST_PULLS_TRST)))
179 /* we're now in the TLR state until trst is deasserted */
180 ZY1000_POKE(ZY1000_JTAG_BASE+0x20, TAP_RESET);
183 /* We'll get RCLK failure when we assert TRST, so clear any false positives here */
184 ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x400);
187 /* wait for srst to float back up */
191 for (i=0; i<1000; i++)
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
205 LOG_USER("SRST didn't deassert after %dms", i);
208 LOG_USER("SRST took %dms to deassert", i);
213 int zy1000_speed(int speed)
219 ZY1000_POKE(ZY1000_JTAG_BASE+0x10, 0x100);
220 LOG_DEBUG("jtag_speed using RCLK");
224 if(speed > 8190 || speed < 2)
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;
230 LOG_USER("jtag_speed %d => JTAG clk=%f", speed, 64.0/(float)speed);
231 ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x100);
232 ZY1000_POKE(ZY1000_JTAG_BASE+0x1c, speed&~1);
237 int zy1000_register_commands(struct command_context_s *cmd_ctx)
243 int zy1000_init(void)
245 ZY1000_POKE(ZY1000_JTAG_BASE+0x10, 0x30); // Turn on LED1 & LED2
247 /* deassert resets. Important to avoid infinite loop waiting for SRST to deassert */
249 zy1000_speed(jtag_speed);
251 bitbang_interface = &zy1000_bitbang;
256 int zy1000_quit(void)
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)
269 pFile = fopen (fileName,"rb");
272 LOG_ERROR("Can't open %s\n", fileName);
273 return ERROR_JTAG_DEVICE_ERROR;
275 if (fseek (pFile, 0, SEEK_END)!=0)
277 LOG_ERROR("Can't open %s\n", fileName);
279 return ERROR_JTAG_DEVICE_ERROR;
284 LOG_ERROR("Can't open %s\n", fileName);
286 return ERROR_JTAG_DEVICE_ERROR;
289 if (fseek (pFile, 0, SEEK_SET)!=0)
291 LOG_ERROR("Can't open %s\n", fileName);
293 return ERROR_JTAG_DEVICE_ERROR;
295 *data=malloc(*len+1);
298 LOG_ERROR("Can't open %s\n", fileName);
300 return ERROR_JTAG_DEVICE_ERROR;
303 if (fread(*data, 1, *len, pFile)!=*len)
307 LOG_ERROR("Can't open %s\n", fileName);
308 return ERROR_JTAG_DEVICE_ERROR;
311 *(((char *)(*data))+*len)=0; /* sentinel */
322 int interface_jtag_execute_queue(void)
327 ZY1000_PEEK(ZY1000_JTAG_BASE+0x10, empty);
328 /* clear JTAG error register */
329 ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x400);
331 if ((empty&0x400)!=0)
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.
337 // return ERROR_FAIL;
346 static cyg_uint32 getShiftValue()
350 ZY1000_PEEK(ZY1000_JTAG_BASE+0xc, value);
351 VERBOSE(LOG_INFO("getShiftValue %08x", value));
355 static cyg_uint32 getShiftValueFlip()
359 ZY1000_PEEK(ZY1000_JTAG_BASE+0x18, value);
360 VERBOSE(LOG_INFO("getShiftValue %08x (flipped)", value));
366 static void shiftValueInnerFlip(const enum tap_state state, const enum tap_state endState, int repeat, cyg_uint32 value)
368 VERBOSE(LOG_INFO("shiftValueInner %s %s %d %08x (flipped)", tap_state_strings[state], tap_state_strings[endState], repeat, value));
372 ZY1000_POKE(ZY1000_JTAG_BASE+0xc, value);
373 ZY1000_POKE(ZY1000_JTAG_BASE+0x8, (1<<15)|(repeat<<8)|(a<<4)|b);
374 VERBOSE(getShiftValueFlip());
378 extern int jtag_check_value(u8 *captured, void *priv);
380 static void gotoEndState()
382 setCurrentState(cmd_queue_end_state);
385 static __inline void scanFields(int num_fields, scan_field_t *fields, enum tap_state shiftState, int pause)
391 for (i = 0; i < num_fields; i++)
395 static u8 *in_buff=NULL; /* pointer to buffer for scanned data */
396 static int in_buff_size=0;
400 // figure out where to store the input data
401 int num_bits=fields[i].num_bits;
402 if (fields[i].in_value!=NULL)
404 inBuffer=fields[i].in_value;
405 } else if (fields[i].in_handler!=NULL)
407 if (in_buff_size*8<num_bits)
409 // we need more space
413 in_buff_size=(num_bits+7)/8;
414 in_buff=malloc(in_buff_size);
417 LOG_ERROR("Out of memory");
418 jtag_error=ERROR_JTAG_QUEUE_FAILED;
425 // here we shuffle N bits out/in
429 enum tap_state pause_state;
432 pause_state=(shiftState==TAP_DRSHIFT)?TAP_DRSHIFT:TAP_IRSHIFT;
436 /* we have more to shift out */
437 } else if (pause&&(i == num_fields-1))
439 /* this was the last to shift out this time */
440 pause_state=(shiftState==TAP_DRSHIFT)?TAP_DRPAUSE:TAP_IRPAUSE;
443 // we have (num_bits+7)/8 bytes of bits to toggle out.
444 // bits are pushed out LSB to MSB
446 if (fields[i].out_value!=NULL)
450 value|=fields[i].out_value[(j+l)/8]<<l;
453 /* mask away unused bits for easier debugging */
454 value&=~(((u32)0xffffffff)<<k);
456 shiftValueInner(shiftState, pause_state, k, value);
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
467 inBuffer[(j+l)/8]=(value>>l)&0xff;
473 if (fields[i].in_handler!=NULL)
476 int r=fields[i].in_handler(inBuffer, fields[i].in_handler_priv, fields+i);
479 /* this will cause jtag_execute_queue() to return an error */
486 int interface_jtag_add_end_state(enum tap_state state)
492 int interface_jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
497 jtag_tap_t *tap, *nextTap;
498 for(tap = jtag_NextEnabledTap(NULL); tap!= NULL; tap=nextTap)
500 nextTap=jtag_NextEnabledTap(tap);
501 int pause=(nextTap==NULL);
505 scan_size = tap->ir_length;
507 /* search the list */
508 for (j=0; j < num_fields; j++)
510 if (tap == fields[j].tap)
514 if ((jtag_verify_capture_ir)&&(fields[j].in_handler==NULL))
516 jtag_set_check_value(fields+j, tap->expected, tap->expected_mask, NULL);
517 } else if (jtag_verify_capture_ir)
519 fields[j].in_check_value = tap->expected;
520 fields[j].in_check_mask = tap->expected_mask;
523 scanFields(1, fields+j, TAP_IRSHIFT, pause);
524 /* update device information */
525 buf_cpy(fields[j].out_value, tap->cur_instr, scan_size);
534 /* if a device isn't listed, set it to BYPASS */
535 u8 ones[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
538 memset(&tmp, 0, sizeof(tmp));
539 tmp.out_value = ones;
540 tmp.num_bits = scan_size;
541 scanFields(1, &tmp, TAP_IRSHIFT, pause);
542 /* update device information */
543 buf_cpy(tmp.out_value, tap->cur_instr, scan_size);
556 int interface_jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
558 scanFields(num_fields, fields, TAP_IRSHIFT, 1);
564 /*extern jtag_command_t **jtag_get_last_command_p(void);*/
566 int interface_jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
570 jtag_tap_t *tap, *nextTap;
571 for(tap = jtag_NextEnabledTap(NULL); tap!= NULL; tap=nextTap)
573 nextTap=jtag_NextEnabledTap(tap);
575 int pause=(nextTap==NULL);
577 for (j=0; j < num_fields; j++)
579 if (tap == fields[j].tap)
583 scanFields(1, fields+j, TAP_DRSHIFT, pause);
589 /* program the scan field to 1 bit length, and ignore it's value */
591 tmp.out_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;
599 scanFields(1, &tmp, TAP_DRSHIFT, pause);
609 int interface_jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
611 scanFields(num_fields, fields, TAP_DRSHIFT, 1);
617 int interface_jtag_add_tlr()
619 setCurrentState(TAP_RESET);
626 extern int jtag_nsrst_delay;
627 extern int jtag_ntrst_delay;
629 int interface_jtag_add_reset(int req_trst, int req_srst)
631 zy1000_reset(req_trst, req_srst);
635 int interface_jtag_add_runtest(int num_cycles, enum tap_state state)
637 /* num_cycles can be 0 */
638 setCurrentState(TAP_IDLE);
640 /* execute num_cycles, 32 at the time. */
642 for (i=0; i<num_cycles; i+=32)
646 if (num_cycles-i<num)
650 shiftValueInner(TAP_IDLE, TAP_IDLE, num, 0);
654 /* finish in end_state */
655 setCurrentState(state);
657 enum tap_state t=TAP_IDLE;
658 /* test manual drive code on any target */
660 u8 tms_scan = TAP_MOVE(t, state);
662 for (i = 0; i < 7; i++)
664 tms = (tms_scan >> i) & 1;
666 ZY1000_POKE(ZY1000_JTAG_BASE+0x28, tms);
669 ZY1000_POKE(ZY1000_JTAG_BASE+0x20, state);
676 int interface_jtag_add_sleep(u32 us)
682 int interface_jtag_add_pathmove(int num_states, enum tap_state *path)
687 /*wait for the fifo to be empty*/
692 enum tap_state cur_state=cmd_queue_cur_state;
696 if (tap_transitions[cur_state].low == path[state_count])
700 else if (tap_transitions[cur_state].high == path[state_count])
706 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[state_count]]);
711 ZY1000_POKE(ZY1000_JTAG_BASE+0x28, tms);
713 cur_state = path[state_count];
719 ZY1000_POKE(ZY1000_JTAG_BASE+0x20, cur_state);
725 void embeddedice_write_dcc(jtag_tap_t *tap, int reg_addr, u8 *buffer, int little, int count)
727 // static int const reg_addr=0x5;
728 enum tap_state end_state=cmd_queue_end_state;
729 if (jtag_NextEnabledTap(jtag_NextEnabledTap(NULL))==NULL)
731 /* better performance via code duplication */
735 for (i = 0; i < count; i++)
737 shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 1));
738 shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr|(1<<5));
744 for (i = 0; i < count; i++)
746 shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 0));
747 shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr|(1<<5));
755 for (i = 0; i < count; i++)
757 embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, little));