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 ***************************************************************************/
23 #include "embeddedice.h"
24 #include "minidriver.h"
25 #include "interface.h"
26 #include "zy1000_version.h"
28 #include <cyg/hal/hal_io.h> // low level i/o
29 #include <cyg/hal/hal_diag.h>
33 #define ZYLIN_VERSION GIT_ZY1000_VERSION
34 #define ZYLIN_DATE __DATE__
35 #define ZYLIN_TIME __TIME__
36 #define ZYLIN_OPENOCD GIT_OPENOCD_VERSION
37 #define ZYLIN_OPENOCD_VERSION "ZY1000 " ZYLIN_VERSION " " ZYLIN_DATE
39 /* low level command set
41 void zy1000_reset(int trst, int srst);
44 int zy1000_speed(int speed);
45 int zy1000_register_commands(struct command_context *cmd_ctx);
46 int zy1000_init(void);
47 int zy1000_quit(void);
49 static int zy1000_khz(int khz, int *jtag_speed)
57 *jtag_speed = 64000/khz;
62 static int zy1000_speed_div(int speed, int *khz)
76 static bool readPowerDropout(void)
79 // sample and clear power dropout
80 HAL_WRITE_UINT32(ZY1000_JTAG_BASE + 0x10, 0x80);
81 HAL_READ_UINT32(ZY1000_JTAG_BASE + 0x10, state);
83 powerDropout = (state & 0x80) != 0;
88 static bool readSRST(void)
91 // sample and clear SRST sensing
92 HAL_WRITE_UINT32(ZY1000_JTAG_BASE + 0x10, 0x00000040);
93 HAL_READ_UINT32(ZY1000_JTAG_BASE + 0x10, state);
95 srstAsserted = (state & 0x40) != 0;
99 static int zy1000_srst_asserted(int *srst_asserted)
101 *srst_asserted = readSRST();
105 static int zy1000_power_dropout(int *dropout)
107 *dropout = readPowerDropout();
112 struct jtag_interface zy1000_interface =
115 .execute_queue = NULL,
116 .speed = zy1000_speed,
117 .register_commands = zy1000_register_commands,
121 .speed_div = zy1000_speed_div,
122 .power_dropout = zy1000_power_dropout,
123 .srst_asserted = zy1000_srst_asserted,
126 void zy1000_reset(int trst, int srst)
128 LOG_DEBUG("zy1000 trst=%d, srst=%d", trst, srst);
131 ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x00000001);
135 /* Danger!!! if clk != 0 when in
136 * idle in TAP_IDLE, reset halt on str912 will fail.
138 ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x00000001);
143 ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x00000002);
148 ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x00000002);
151 if (trst||(srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
154 /* we're now in the RESET state until trst is deasserted */
155 ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, TAP_RESET);
158 /* We'll get RCLK failure when we assert TRST, so clear any false positives here */
159 ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x400);
162 /* wait for srst to float back up */
166 for (i = 0; i < 1000; i++)
168 // We don't want to sense our own reset, so we clear here.
169 // There is of course a timing hole where we could loose
180 LOG_USER("SRST didn't deassert after %dms", i);
183 LOG_USER("SRST took %dms to deassert", i);
188 int zy1000_speed(int speed)
194 ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x100);
195 LOG_DEBUG("jtag_speed using RCLK");
199 if (speed > 8190 || speed < 2)
201 LOG_USER("valid ZY1000 jtag_speed=[8190,2]. Divisor is 64MHz / even values between 8190-2, i.e. min 7814Hz, max 32MHz");
202 return ERROR_INVALID_ARGUMENTS;
205 LOG_USER("jtag_speed %d => JTAG clk=%f", speed, 64.0/(float)speed);
206 ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x100);
207 ZY1000_POKE(ZY1000_JTAG_BASE + 0x1c, speed&~1);
212 static bool savePower;
215 static void setPower(bool power)
220 HAL_WRITE_UINT32(ZY1000_JTAG_BASE + 0x14, 0x8);
223 HAL_WRITE_UINT32(ZY1000_JTAG_BASE + 0x10, 0x8);
227 COMMAND_HANDLER(handle_power_command)
233 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
238 LOG_INFO("Target power %s", savePower ? "on" : "off");
241 return ERROR_INVALID_ARGUMENTS;
248 /* Give TELNET a way to find out what version this is */
249 static int jim_zy1000_version(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
251 if ((argc < 1) || (argc > 3))
253 const char *version_str = NULL;
257 version_str = ZYLIN_OPENOCD_VERSION;
260 const char *str = Jim_GetString(argv[1], NULL);
261 const char *str2 = NULL;
263 str2 = Jim_GetString(argv[2], NULL);
264 if (strcmp("openocd", str) == 0)
266 version_str = ZYLIN_OPENOCD;
268 else if (strcmp("zy1000", str) == 0)
270 version_str = ZYLIN_VERSION;
272 else if (strcmp("date", str) == 0)
274 version_str = ZYLIN_DATE;
276 else if (strcmp("time", str) == 0)
278 version_str = ZYLIN_TIME;
280 else if (strcmp("pcb", str) == 0)
282 #ifdef CYGPKG_HAL_NIOS2
288 #ifdef CYGPKG_HAL_NIOS2
289 else if (strcmp("fpga", str) == 0)
292 /* return a list of 32 bit integers to describe the expected
295 static char *fpga_id = "0x12345678 0x12345678 0x12345678 0x12345678";
296 cyg_uint32 id, timestamp;
297 HAL_READ_UINT32(SYSID_BASE, id);
298 HAL_READ_UINT32(SYSID_BASE+4, timestamp);
299 sprintf(fpga_id, "0x%08x 0x%08x 0x%08x 0x%08x", id, timestamp, SYSID_ID, SYSID_TIMESTAMP);
300 version_str = fpga_id;
301 if ((argc>2) && (strcmp("time", str2) == 0))
303 time_t last_mod = timestamp;
304 char * t = ctime (&last_mod) ;
317 Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));
323 #ifdef CYGPKG_HAL_NIOS2
324 static int jim_zy1000_writefirmware(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
331 const char *str = Jim_GetString(argv[1], &length);
333 /* BUG!!!! skip header! */
334 void *firmware_address=0x4000000;
335 int firmware_length=0x100000;
337 if (length>firmware_length)
342 if ((stat = flash_erase((void *)firmware_address, firmware_length, (void **)&err_addr)) != 0)
347 if ((stat = flash_program(firmware_address, str, length, (void **)&err_addr)) != 0)
355 zylinjtag_Jim_Command_powerstatus(Jim_Interp *interp,
357 Jim_Obj * const *argv)
361 Jim_WrongNumArgs(interp, 1, argv, "powerstatus");
366 ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, status);
368 Jim_SetResult(interp, Jim_NewIntObj(interp, (status&0x80) != 0));
373 int zy1000_register_commands(struct command_context *cmd_ctx)
375 COMMAND_REGISTER(cmd_ctx, NULL, "power", handle_power_command, COMMAND_ANY,
376 "power <on/off> - turn power switch to target on/off. No arguments - print status.");
378 Jim_CreateCommand(interp, "zy1000_version", jim_zy1000_version, NULL, NULL);
381 Jim_CreateCommand(interp, "powerstatus", zylinjtag_Jim_Command_powerstatus, NULL, NULL);
383 #ifdef CYGPKG_HAL_NIOS2
384 Jim_CreateCommand(interp, "updatezy1000firmware", jim_zy1000_writefirmware, NULL, NULL);
394 int zy1000_init(void)
396 LOG_USER("%s", ZYLIN_OPENOCD_VERSION);
398 ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x30); // Turn on LED1 & LED2
400 setPower(true); // on by default
403 /* deassert resets. Important to avoid infinite loop waiting for SRST to deassert */
405 zy1000_speed(jtag_get_speed());
410 int zy1000_quit(void)
418 int interface_jtag_execute_queue(void)
423 ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, empty);
424 /* clear JTAG error register */
425 ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x400);
427 if ((empty&0x400) != 0)
429 LOG_WARNING("RCLK timeout");
430 /* the error is informative only as we don't want to break the firmware if there
431 * is a false positive.
433 // return ERROR_FAIL;
442 static cyg_uint32 getShiftValue(void)
446 ZY1000_PEEK(ZY1000_JTAG_BASE + 0xc, value);
447 VERBOSE(LOG_INFO("getShiftValue %08x", value));
451 static cyg_uint32 getShiftValueFlip(void)
455 ZY1000_PEEK(ZY1000_JTAG_BASE + 0x18, value);
456 VERBOSE(LOG_INFO("getShiftValue %08x (flipped)", value));
462 static void shiftValueInnerFlip(const tap_state_t state, const tap_state_t endState, int repeat, cyg_uint32 value)
464 VERBOSE(LOG_INFO("shiftValueInner %s %s %d %08x (flipped)", tap_state_name(state), tap_state_name(endState), repeat, value));
468 ZY1000_POKE(ZY1000_JTAG_BASE + 0xc, value);
469 ZY1000_POKE(ZY1000_JTAG_BASE + 0x8, (1 << 15) | (repeat << 8) | (a << 4) | b);
470 VERBOSE(getShiftValueFlip());
474 static void gotoEndState(tap_state_t end_state)
476 setCurrentState(end_state);
479 static __inline void scanFields(int num_fields, const struct scan_field *fields, tap_state_t shiftState, int pause)
485 for (i = 0; i < num_fields; i++)
489 uint8_t *inBuffer = NULL;
492 // figure out where to store the input data
493 int num_bits = fields[i].num_bits;
494 if (fields[i].in_value != NULL)
496 inBuffer = fields[i].in_value;
499 // here we shuffle N bits out/in
503 tap_state_t pause_state;
506 pause_state = (shiftState == TAP_DRSHIFT)?TAP_DRSHIFT:TAP_IRSHIFT;
510 /* we have more to shift out */
511 } else if (pause&&(i == num_fields-1))
513 /* this was the last to shift out this time */
514 pause_state = (shiftState==TAP_DRSHIFT)?TAP_DRPAUSE:TAP_IRPAUSE;
517 // we have (num_bits + 7)/8 bytes of bits to toggle out.
518 // bits are pushed out LSB to MSB
520 if (fields[i].out_value != NULL)
522 for (l = 0; l < k; l += 8)
524 value|=fields[i].out_value[(j + l)/8]<<l;
527 /* mask away unused bits for easier debugging */
530 value&=~(((uint32_t)0xffffffff) << k);
533 /* Shifting by >= 32 is not defined by the C standard
534 * and will in fact shift by &0x1f bits on nios */
537 shiftValueInner(shiftState, pause_state, k, value);
539 if (inBuffer != NULL)
541 // data in, LSB to MSB
542 value = getShiftValue();
543 // we're shifting in data to MSB, shift data to be aligned for returning the value
546 for (l = 0; l < k; l += 8)
548 inBuffer[(j + l)/8]=(value >> l)&0xff;
556 int interface_jtag_add_ir_scan(int num_fields, const struct scan_field *fields, tap_state_t state)
561 struct jtag_tap *tap, *nextTap;
562 for (tap = jtag_tap_next_enabled(NULL); tap!= NULL; tap = nextTap)
564 nextTap = jtag_tap_next_enabled(tap);
565 int pause = (nextTap==NULL);
569 scan_size = tap->ir_length;
571 /* search the list */
572 for (j = 0; j < num_fields; j++)
574 if (tap == fields[j].tap)
578 scanFields(1, fields + j, TAP_IRSHIFT, pause);
579 /* update device information */
580 buf_cpy(fields[j].out_value, tap->cur_instr, scan_size);
589 /* if a device isn't listed, set it to BYPASS */
590 uint8_t ones[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
592 struct scan_field tmp;
593 memset(&tmp, 0, sizeof(tmp));
594 tmp.out_value = ones;
595 tmp.num_bits = scan_size;
596 scanFields(1, &tmp, TAP_IRSHIFT, pause);
597 /* update device information */
598 buf_cpy(tmp.out_value, tap->cur_instr, scan_size);
611 int interface_jtag_add_plain_ir_scan(int num_fields, const struct scan_field *fields, tap_state_t state)
613 scanFields(num_fields, fields, TAP_IRSHIFT, 1);
619 int interface_jtag_add_dr_scan(int num_fields, const struct scan_field *fields, tap_state_t state)
623 struct jtag_tap *tap, *nextTap;
624 for (tap = jtag_tap_next_enabled(NULL); tap!= NULL; tap = nextTap)
626 nextTap = jtag_tap_next_enabled(tap);
628 int pause = (nextTap==NULL);
630 for (j = 0; j < num_fields; j++)
632 if (tap == fields[j].tap)
636 scanFields(1, fields+j, TAP_DRSHIFT, pause);
641 struct scan_field tmp;
642 /* program the scan field to 1 bit length, and ignore it's value */
644 tmp.out_value = NULL;
647 scanFields(1, &tmp, TAP_DRSHIFT, pause);
657 int interface_jtag_add_plain_dr_scan(int num_fields, const struct scan_field *fields, tap_state_t state)
659 scanFields(num_fields, fields, TAP_DRSHIFT, 1);
665 int interface_jtag_add_tlr()
667 setCurrentState(TAP_RESET);
674 int interface_jtag_add_reset(int req_trst, int req_srst)
676 zy1000_reset(req_trst, req_srst);
680 static int zy1000_jtag_add_clocks(int num_cycles, tap_state_t state, tap_state_t clockstate)
682 /* num_cycles can be 0 */
683 setCurrentState(clockstate);
685 /* execute num_cycles, 32 at the time. */
687 for (i = 0; i < num_cycles; i += 32)
691 if (num_cycles-i < num)
695 shiftValueInner(clockstate, clockstate, num, 0);
699 /* finish in end_state */
700 setCurrentState(state);
702 tap_state_t t = TAP_IDLE;
703 /* test manual drive code on any target */
705 uint8_t tms_scan = tap_get_tms_path(t, state);
706 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
708 for (i = 0; i < tms_count; i++)
710 tms = (tms_scan >> i) & 1;
712 ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, tms);
715 ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, state);
722 int interface_jtag_add_runtest(int num_cycles, tap_state_t state)
724 return zy1000_jtag_add_clocks(num_cycles, state, TAP_IDLE);
727 int interface_jtag_add_clocks(int num_cycles)
729 return zy1000_jtag_add_clocks(num_cycles, cmd_queue_cur_state, cmd_queue_cur_state);
732 int interface_jtag_add_sleep(uint32_t us)
738 int interface_jtag_add_pathmove(int num_states, const tap_state_t *path)
743 /*wait for the fifo to be empty*/
748 tap_state_t cur_state = cmd_queue_cur_state;
752 if (tap_state_transition(cur_state, false) == path[state_count])
756 else if (tap_state_transition(cur_state, true) == path[state_count])
762 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(cur_state), tap_state_name(path[state_count]));
767 ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, tms);
769 cur_state = path[state_count];
775 ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, cur_state);
781 void embeddedice_write_dcc(struct jtag_tap *tap, int reg_addr, uint8_t *buffer, int little, int count)
783 // static int const reg_addr = 0x5;
784 tap_state_t end_state = jtag_get_end_state();
785 if (jtag_tap_next_enabled(jtag_tap_next_enabled(NULL)) == NULL)
787 /* better performance via code duplication */
791 for (i = 0; i < count; i++)
793 shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 1));
794 shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr | (1 << 5));
800 for (i = 0; i < count; i++)
802 shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 0));
803 shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr | (1 << 5));
811 for (i = 0; i < count; i++)
813 embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, little));