zy1000: fix breakage in command parsing code for power command
[openocd.git] / src / jtag / zy1000 / zy1000.c
blob8b5b753a5c0f8417d62826fe05fbf234bbd16e87
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
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>
31 #include <time.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)
51 if (khz == 0)
53 *jtag_speed = 0;
55 else
57 *jtag_speed = 64000/khz;
59 return ERROR_OK;
62 static int zy1000_speed_div(int speed, int *khz)
64 if (speed == 0)
66 *khz = 0;
68 else
70 *khz = 64000/speed;
73 return ERROR_OK;
76 static bool readPowerDropout(void)
78 cyg_uint32 state;
79 // sample and clear power dropout
80 HAL_WRITE_UINT32(ZY1000_JTAG_BASE + 0x10, 0x80);
81 HAL_READ_UINT32(ZY1000_JTAG_BASE + 0x10, state);
82 bool powerDropout;
83 powerDropout = (state & 0x80) != 0;
84 return powerDropout;
88 static bool readSRST(void)
90 cyg_uint32 state;
91 // sample and clear SRST sensing
92 HAL_WRITE_UINT32(ZY1000_JTAG_BASE + 0x10, 0x00000040);
93 HAL_READ_UINT32(ZY1000_JTAG_BASE + 0x10, state);
94 bool srstAsserted;
95 srstAsserted = (state & 0x40) != 0;
96 return srstAsserted;
99 static int zy1000_srst_asserted(int *srst_asserted)
101 *srst_asserted = readSRST();
102 return ERROR_OK;
105 static int zy1000_power_dropout(int *dropout)
107 *dropout = readPowerDropout();
108 return ERROR_OK;
112 struct jtag_interface zy1000_interface =
114 .name = "ZY1000",
115 .execute_queue = NULL,
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 .srst_asserted = zy1000_srst_asserted,
126 void zy1000_reset(int trst, int srst)
128 LOG_DEBUG("zy1000 trst=%d, srst=%d", trst, srst);
129 if (!srst)
131 ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x00000001);
133 else
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);
141 if (!trst)
143 ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x00000002);
145 else
147 /* assert reset */
148 ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x00000002);
151 if (trst||(srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
153 waitIdle();
154 /* we're now in the RESET state until trst is deasserted */
155 ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, TAP_RESET);
156 } else
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 */
163 if (!srst)
165 int i;
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
170 // a "real" reset.
171 if (!readSRST())
172 break;
174 /* wait 1ms */
175 alive_sleep(1);
178 if (i == 1000)
180 LOG_USER("SRST didn't deassert after %dms", i);
181 } else if (i > 1)
183 LOG_USER("SRST took %dms to deassert", i);
188 int zy1000_speed(int speed)
190 if (speed == 0)
192 /*0 means RCLK*/
193 speed = 0;
194 ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x100);
195 LOG_DEBUG("jtag_speed using RCLK");
197 else
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);
209 return ERROR_OK;
212 static bool savePower;
215 static void setPower(bool power)
217 savePower = power;
218 if (power)
220 HAL_WRITE_UINT32(ZY1000_JTAG_BASE + 0x14, 0x8);
221 } else
223 HAL_WRITE_UINT32(ZY1000_JTAG_BASE + 0x10, 0x8);
227 COMMAND_HANDLER(handle_power_command)
229 switch (CMD_ARGC)
231 case 1: {
232 bool enable;
233 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
234 setPower(enable);
235 // fall through
237 case 0:
238 LOG_INFO("Target power %s", savePower ? "on" : "off");
239 break;
240 default:
241 return ERROR_INVALID_ARGUMENTS;
244 return ERROR_OK;
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))
252 return JIM_ERR;
253 const char *version_str = NULL;
255 if (argc == 1)
257 version_str = ZYLIN_OPENOCD_VERSION;
258 } else
260 const char *str = Jim_GetString(argv[1], NULL);
261 const char *str2 = NULL;
262 if (argc > 2)
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
283 version_str="c";
284 #else
285 version_str="b";
286 #endif
288 #ifdef CYGPKG_HAL_NIOS2
289 else if (strcmp("fpga", str) == 0)
292 /* return a list of 32 bit integers to describe the expected
293 * and actual FPGA
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) ;
305 t[strlen(t)-1] = 0;
306 version_str = t;
309 #endif
311 else
313 return JIM_ERR;
317 Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));
319 return JIM_OK;
323 #ifdef CYGPKG_HAL_NIOS2
324 static int jim_zy1000_writefirmware(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
326 if (argc != 2)
327 return JIM_ERR;
329 int length;
330 int stat;
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)
338 return JIM_ERR;
340 void *err_addr;
342 if ((stat = flash_erase((void *)firmware_address, firmware_length, (void **)&err_addr)) != 0)
344 return JIM_ERR;
347 if ((stat = flash_program(firmware_address, str, length, (void **)&err_addr)) != 0)
348 return JIM_ERR;
350 return JIM_OK;
352 #endif
354 static int
355 zylinjtag_Jim_Command_powerstatus(Jim_Interp *interp,
356 int argc,
357 Jim_Obj * const *argv)
359 if (argc != 1)
361 Jim_WrongNumArgs(interp, 1, argv, "powerstatus");
362 return JIM_ERR;
365 cyg_uint32 status;
366 ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, status);
368 Jim_SetResult(interp, Jim_NewIntObj(interp, (status&0x80) != 0));
370 return JIM_OK;
373 int zy1000_register_commands(struct command_context *cmd_ctx)
375 register_command(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);
385 #endif
388 return ERROR_OK;
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 */
404 zy1000_reset(0, 0);
405 zy1000_speed(jtag_get_speed());
407 return ERROR_OK;
410 int zy1000_quit(void)
413 return ERROR_OK;
418 int interface_jtag_execute_queue(void)
420 cyg_uint32 empty;
422 waitIdle();
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;
435 return ERROR_OK;
442 static cyg_uint32 getShiftValue(void)
444 cyg_uint32 value;
445 waitIdle();
446 ZY1000_PEEK(ZY1000_JTAG_BASE + 0xc, value);
447 VERBOSE(LOG_INFO("getShiftValue %08x", value));
448 return value;
450 #if 0
451 static cyg_uint32 getShiftValueFlip(void)
453 cyg_uint32 value;
454 waitIdle();
455 ZY1000_PEEK(ZY1000_JTAG_BASE + 0x18, value);
456 VERBOSE(LOG_INFO("getShiftValue %08x (flipped)", value));
457 return value;
459 #endif
461 #if 0
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));
465 cyg_uint32 a,b;
466 a = state;
467 b = endState;
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());
472 #endif
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)
481 int i;
482 int j;
483 int k;
485 for (i = 0; i < num_fields; i++)
487 cyg_uint32 value;
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
500 j = 0;
501 while (j < num_bits)
503 tap_state_t pause_state;
504 int l;
505 k = num_bits-j;
506 pause_state = (shiftState == TAP_DRSHIFT)?TAP_DRSHIFT:TAP_IRSHIFT;
507 if (k > 32)
509 k = 32;
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
519 value = 0;
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 */
528 if (k < 32)
530 value&=~(((uint32_t)0xffffffff) << k);
531 } else
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
544 value >>= 32-k;
546 for (l = 0; l < k; l += 8)
548 inBuffer[(j + l)/8]=(value >> l)&0xff;
551 j += k;
556 int interface_jtag_add_ir_scan(int num_fields, const struct scan_field *fields, tap_state_t state)
559 int j;
560 int scan_size = 0;
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);
567 int found = 0;
569 scan_size = tap->ir_length;
571 /* search the list */
572 for (j = 0; j < num_fields; j++)
574 if (tap == fields[j].tap)
576 found = 1;
578 scanFields(1, fields + j, TAP_IRSHIFT, pause);
579 /* update device information */
580 buf_cpy(fields[j].out_value, tap->cur_instr, scan_size);
582 tap->bypass = 0;
583 break;
587 if (!found)
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);
599 tap->bypass = 1;
602 gotoEndState(state);
604 return ERROR_OK;
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);
614 gotoEndState(state);
616 return ERROR_OK;
619 int interface_jtag_add_dr_scan(int num_fields, const struct scan_field *fields, tap_state_t state)
622 int j;
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);
627 int found = 0;
628 int pause = (nextTap==NULL);
630 for (j = 0; j < num_fields; j++)
632 if (tap == fields[j].tap)
634 found = 1;
636 scanFields(1, fields+j, TAP_DRSHIFT, pause);
639 if (!found)
641 struct scan_field tmp;
642 /* program the scan field to 1 bit length, and ignore it's value */
643 tmp.num_bits = 1;
644 tmp.out_value = NULL;
645 tmp.in_value = NULL;
647 scanFields(1, &tmp, TAP_DRSHIFT, pause);
649 else
653 gotoEndState(state);
654 return ERROR_OK;
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);
660 gotoEndState(state);
661 return ERROR_OK;
665 int interface_jtag_add_tlr()
667 setCurrentState(TAP_RESET);
668 return ERROR_OK;
674 int interface_jtag_add_reset(int req_trst, int req_srst)
676 zy1000_reset(req_trst, req_srst);
677 return ERROR_OK;
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. */
686 int i;
687 for (i = 0; i < num_cycles; i += 32)
689 int num;
690 num = 32;
691 if (num_cycles-i < num)
693 num = num_cycles-i;
695 shiftValueInner(clockstate, clockstate, num, 0);
698 #if !TEST_MANUAL()
699 /* finish in end_state */
700 setCurrentState(state);
701 #else
702 tap_state_t t = TAP_IDLE;
703 /* test manual drive code on any target */
704 int tms;
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;
711 waitIdle();
712 ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, tms);
714 waitIdle();
715 ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, state);
716 #endif
719 return ERROR_OK;
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)
734 jtag_sleep(us);
735 return ERROR_OK;
738 int interface_jtag_add_pathmove(int num_states, const tap_state_t *path)
740 int state_count;
741 int tms = 0;
743 /*wait for the fifo to be empty*/
744 waitIdle();
746 state_count = 0;
748 tap_state_t cur_state = cmd_queue_cur_state;
750 while (num_states)
752 if (tap_state_transition(cur_state, false) == path[state_count])
754 tms = 0;
756 else if (tap_state_transition(cur_state, true) == path[state_count])
758 tms = 1;
760 else
762 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(cur_state), tap_state_name(path[state_count]));
763 exit(-1);
766 waitIdle();
767 ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, tms);
769 cur_state = path[state_count];
770 state_count++;
771 num_states--;
774 waitIdle();
775 ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, cur_state);
776 return ERROR_OK;
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 */
788 if (little)
790 int i;
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));
795 buffer += 4;
797 } else
799 int i;
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));
804 buffer += 4;
808 else
810 int i;
811 for (i = 0; i < count; i++)
813 embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, little));
814 buffer += 4;