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/var_io.h> // common registers
32 #include <cyg/hal/plf_io.h> // platform registers
33 #include <cyg/hal/hal_diag.h>
38 extern int jtag_error;
40 /* low level command set
42 int eCosBoard_read(void);
43 static void eCosBoard_write(int tck, int tms, int tdi);
44 void eCosBoard_reset(int trst, int srst);
47 int eCosBoard_speed(int speed);
48 int eCosBoard_register_commands(struct command_context_s *cmd_ctx);
49 int eCosBoard_init(void);
50 int eCosBoard_quit(void);
52 /* interface commands */
53 int eCosBoard_handle_eCosBoard_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
55 static int eCosBoard_khz(int khz, int *jtag_speed)
63 *jtag_speed=64000/khz;
68 static int eCosBoard_speed_div(int speed, int *khz)
83 jtag_interface_t eCosBoard_interface =
86 .execute_queue = bitbang_execute_queue,
87 .speed = eCosBoard_speed,
88 .register_commands = eCosBoard_register_commands,
89 .init = eCosBoard_init,
90 .quit = eCosBoard_quit,
92 .speed_div = eCosBoard_speed_div,
95 bitbang_interface_t eCosBoard_bitbang =
97 .read = eCosBoard_read,
98 .write = eCosBoard_write,
99 .reset = eCosBoard_reset
104 static void eCosBoard_write(int tck, int tms, int tdi)
109 int eCosBoard_read(void)
114 extern bool readSRST();
116 void eCosBoard_reset(int trst, int srst)
118 LOG_DEBUG("zy1000 trst=%d, srst=%d", trst, srst);
121 ZY1000_POKE(0x08000014, 0x00000001);
125 /* Danger!!! if clk!=0 when in
126 * idle in TAP_RTI, reset halt on str912 will fail.
128 ZY1000_POKE(0x08000010, 0x00000001);
133 ZY1000_POKE(0x08000014, 0x00000002);
138 ZY1000_POKE(0x08000010, 0x00000002);
141 if (trst||(srst&&(jtag_reset_config & RESET_SRST_PULLS_TRST)))
144 /* we're now in the TLR state until trst is deasserted */
145 ZY1000_POKE(0x08000020, TAP_TLR);
148 /* We'll get RCLK failure when we assert TRST, so clear any false positives here */
149 ZY1000_POKE(0x08000014, 0x400);
152 /* wait for srst to float back up */
156 for (i=0; i<1000; i++)
158 // We don't want to sense our own reset, so we clear here.
159 // There is of course a timing hole where we could loose
170 LOG_USER("SRST didn't deassert after %dms", i);
173 LOG_USER("SRST took %dms to deassert", i);
178 int eCosBoard_speed(int speed)
184 ZY1000_POKE(0x08000010, 0x100);
185 LOG_DEBUG("jtag_speed using RCLK");
189 if(speed > 8190 || speed < 2)
191 LOG_ERROR("valid ZY1000 jtag_speed=[8190,2]. Divisor is 64MHz / even values between 8190-2, i.e. min 7814Hz, max 32MHz");
192 return ERROR_INVALID_ARGUMENTS;
195 LOG_USER("jtag_speed %d => JTAG clk=%f", speed, 64.0/(float)speed);
196 ZY1000_POKE(0x08000014, 0x100);
197 ZY1000_POKE(0x0800001c, speed&~1);
202 int eCosBoard_register_commands(struct command_context_s *cmd_ctx)
208 int eCosBoard_init(void)
210 ZY1000_POKE(0x08000010, 0x30); // Turn on LED1 & LED2
212 /* deassert resets. Important to avoid infinite loop waiting for SRST to deassert */
213 eCosBoard_reset(0, 0);
214 eCosBoard_speed(jtag_speed);
216 bitbang_interface = &eCosBoard_bitbang;
221 int eCosBoard_quit(void)
229 /* loads a file and returns a pointer to it in memory. The file contains
230 * a 0 byte(sentinel) after len bytes - the length of the file. */
231 int loadFile(const char *fileName, void **data, int *len)
234 pFile = fopen (fileName,"rb");
237 LOG_ERROR("Can't open %s\n", fileName);
238 return ERROR_JTAG_DEVICE_ERROR;
240 if (fseek (pFile, 0, SEEK_END)!=0)
242 LOG_ERROR("Can't open %s\n", fileName);
244 return ERROR_JTAG_DEVICE_ERROR;
249 LOG_ERROR("Can't open %s\n", fileName);
251 return ERROR_JTAG_DEVICE_ERROR;
254 if (fseek (pFile, 0, SEEK_SET)!=0)
256 LOG_ERROR("Can't open %s\n", fileName);
258 return ERROR_JTAG_DEVICE_ERROR;
260 *data=malloc(*len+1);
263 LOG_ERROR("Can't open %s\n", fileName);
265 return ERROR_JTAG_DEVICE_ERROR;
268 if (fread(*data, 1, *len, pFile)!=*len)
272 LOG_ERROR("Can't open %s\n", fileName);
273 return ERROR_JTAG_DEVICE_ERROR;
276 *(((char *)(*data))+*len)=0; /* sentinel */
287 int interface_jtag_execute_queue(void)
292 ZY1000_PEEK(0x08000010, empty);
293 /* clear JTAG error register */
294 ZY1000_POKE(0x08000014, 0x400);
296 if ((empty&0x400)!=0)
298 LOG_WARNING("RCLK timeout");
299 /* the error is informative only as we don't want to break the firmware if there
300 * is a false positive.
302 // return ERROR_FAIL;
311 static cyg_uint32 getShiftValue()
315 ZY1000_PEEK(0x0800000c, value);
316 VERBOSE(LOG_INFO("getShiftValue %08x", value));
320 static cyg_uint32 getShiftValueFlip()
324 ZY1000_PEEK(0x08000018, value);
325 VERBOSE(LOG_INFO("getShiftValue %08x (flipped)", value));
331 static void shiftValueInnerFlip(const enum tap_state state, const enum tap_state endState, int repeat, cyg_uint32 value)
333 VERBOSE(LOG_INFO("shiftValueInner %s %s %d %08x (flipped)", tap_state_strings[state], tap_state_strings[endState], repeat, value));
337 ZY1000_POKE(0x0800000c, value);
338 ZY1000_POKE(0x08000008, (1<<15)|(repeat<<8)|(a<<4)|b);
339 VERBOSE(getShiftValueFlip());
343 extern int jtag_check_value(u8 *captured, void *priv);
345 static void gotoEndState()
347 setCurrentState(cmd_queue_end_state);
350 static __inline void scanFields(int num_fields, scan_field_t *fields, enum tap_state shiftState, int pause)
356 for (i = 0; i < num_fields; i++)
360 static u8 *in_buff=NULL; /* pointer to buffer for scanned data */
361 static int in_buff_size=0;
365 // figure out where to store the input data
366 int num_bits=fields[i].num_bits;
367 if (fields[i].in_value!=NULL)
369 inBuffer=fields[i].in_value;
370 } else if (fields[i].in_handler!=NULL)
372 if (in_buff_size*8<num_bits)
374 // we need more space
378 in_buff_size=(num_bits+7)/8;
379 in_buff=malloc(in_buff_size);
382 LOG_ERROR("Out of memory");
383 jtag_error=ERROR_JTAG_QUEUE_FAILED;
390 // here we shuffle N bits out/in
394 enum tap_state pause_state;
397 pause_state=(shiftState==TAP_SD)?TAP_SD:TAP_SI;
401 /* we have more to shift out */
402 } else if (pause&&(i == num_fields-1))
404 /* this was the last to shift out this time */
405 pause_state=(shiftState==TAP_SD)?TAP_PD:TAP_PI;
408 // we have (num_bits+7)/8 bytes of bits to toggle out.
409 // bits are pushed out LSB to MSB
411 if (fields[i].out_value!=NULL)
415 value|=fields[i].out_value[(j+l)/8]<<l;
418 /* mask away unused bits for easier debugging */
419 value&=~(((u32)0xffffffff)<<k);
421 shiftValueInner(shiftState, pause_state, k, value);
425 // data in, LSB to MSB
426 value=getShiftValue();
427 // we're shifting in data to MSB, shift data to be aligned for returning the value
432 inBuffer[(j+l)/8]=(value>>l)&0xff;
438 if (fields[i].in_handler!=NULL)
441 int r=fields[i].in_handler(inBuffer, fields[i].in_handler_priv, fields+i);
444 /* this will cause jtag_execute_queue() to return an error */
451 int interface_jtag_add_end_state(enum tap_state state)
457 int interface_jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
462 jtag_device_t *device;
464 for (i=0; i < jtag_num_devices; i++)
466 int pause=i==(jtag_num_devices-1);
468 device = jtag_get_device(i);
469 scan_size = device->ir_length;
471 /* search the list */
472 for (j=0; j < num_fields; j++)
474 if (i == fields[j].device)
478 if ((jtag_verify_capture_ir)&&(fields[j].in_handler==NULL))
480 jtag_set_check_value(fields+j, device->expected, device->expected_mask, NULL);
481 } else if (jtag_verify_capture_ir)
483 fields[j].in_check_value = device->expected;
484 fields[j].in_check_mask = device->expected_mask;
487 scanFields(1, fields+j, TAP_SI, pause);
488 /* update device information */
489 buf_cpy(fields[j].out_value, jtag_get_device(i)->cur_instr, scan_size);
498 /* if a device isn't listed, set it to BYPASS */
499 u8 ones[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
502 memset(&tmp, 0, sizeof(tmp));
503 tmp.out_value = ones;
504 tmp.num_bits = scan_size;
505 scanFields(1, &tmp, TAP_SI, pause);
506 /* update device information */
507 buf_cpy(tmp.out_value, jtag_get_device(i)->cur_instr, scan_size);
520 int interface_jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
522 scanFields(num_fields, fields, TAP_SI, 1);
528 /*extern jtag_command_t **jtag_get_last_command_p(void);*/
530 int interface_jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
533 for (i=0; i < jtag_num_devices; i++)
536 int pause = (i==(jtag_num_devices-1));
538 for (j=0; j < num_fields; j++)
540 if (i == fields[j].device)
544 scanFields(1, fields+j, TAP_SD, pause);
549 #ifdef _DEBUG_JTAG_IO_
550 /* if a device isn't listed, the BYPASS register should be selected */
551 if (!jtag_get_device(i)->bypass)
553 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
559 /* program the scan field to 1 bit length, and ignore it's value */
561 tmp.out_value = NULL;
564 tmp.in_check_value = NULL;
565 tmp.in_check_mask = NULL;
566 tmp.in_handler = NULL;
567 tmp.in_handler_priv = NULL;
569 scanFields(1, &tmp, TAP_SD, pause);
573 #ifdef _DEBUG_JTAG_IO_
574 /* if a device is listed, the BYPASS register must not be selected */
575 if (jtag_get_device(i)->bypass)
577 LOG_WARNING("scan data for a device in BYPASS");
586 int interface_jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
588 scanFields(num_fields, fields, TAP_SD, 1);
594 int interface_jtag_add_tlr()
596 setCurrentState(TAP_TLR);
603 extern int jtag_nsrst_delay;
604 extern int jtag_ntrst_delay;
606 int interface_jtag_add_reset(int req_trst, int req_srst)
608 eCosBoard_reset(req_trst, req_srst);
612 int interface_jtag_add_runtest(int num_cycles, enum tap_state state)
614 /* num_cycles can be 0 */
615 setCurrentState(TAP_RTI);
617 /* execute num_cycles, 32 at the time. */
619 for (i=0; i<num_cycles; i+=32)
623 if (num_cycles-i<num)
627 shiftValueInner(TAP_RTI, TAP_RTI, num, 0);
631 /* finish in end_state */
632 setCurrentState(state);
634 enum tap_state t=TAP_RTI;
635 /* test manual drive code on any target */
637 u8 tms_scan = TAP_MOVE(t, state);
639 for (i = 0; i < 7; i++)
641 tms = (tms_scan >> i) & 1;
643 ZY1000_POKE(0x08000028, tms);
646 ZY1000_POKE(0x08000020, state);
653 int interface_jtag_add_sleep(u32 us)
659 int interface_jtag_add_pathmove(int num_states, enum tap_state *path)
664 /*wait for the fifo to be empty*/
669 enum tap_state cur_state=cmd_queue_cur_state;
673 if (tap_transitions[cur_state].low == path[state_count])
677 else if (tap_transitions[cur_state].high == path[state_count])
683 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[state_count]]);
688 ZY1000_POKE(0x08000028, tms);
690 cur_state = path[state_count];
696 ZY1000_POKE(0x08000020, cur_state);
702 void embeddedice_write_dcc(int chain_pos, int reg_addr, u8 *buffer, int little, int count)
704 // static int const reg_addr=0x5;
705 enum tap_state end_state=cmd_queue_end_state;
706 if (jtag_num_devices==1)
708 /* better performance via code duplication */
712 for (i = 0; i < count; i++)
714 shiftValueInner(TAP_SD, TAP_SD, 32, fast_target_buffer_get_u32(buffer, 1));
715 shiftValueInner(TAP_SD, end_state, 6, reg_addr|(1<<5));
721 for (i = 0; i < count; i++)
723 shiftValueInner(TAP_SD, TAP_SD, 32, fast_target_buffer_get_u32(buffer, 0));
724 shiftValueInner(TAP_SD, end_state, 6, reg_addr|(1<<5));
732 for (i = 0; i < count; i++)
734 embeddedice_write_reg_inner(chain_pos, reg_addr, fast_target_buffer_get_u32(buffer, little));