You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

770 lines
16 KiB

  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. #include "embeddedice.h"
  23. #include "minidriver.h"
  24. #include "interface.h"
  25. #include <cyg/hal/hal_io.h> // low level i/o
  26. #include <cyg/hal/hal_diag.h>
  27. #define ZYLIN_VERSION "1.52"
  28. #define ZYLIN_DATE __DATE__
  29. #define ZYLIN_TIME __TIME__
  30. #define ZYLIN_OPENOCD "$Revision$"
  31. #define ZYLIN_OPENOCD_VERSION "Zylin JTAG ZY1000 " ZYLIN_VERSION " " ZYLIN_DATE " " ZYLIN_TIME
  32. /* low level command set
  33. */
  34. void zy1000_reset(int trst, int srst);
  35. int zy1000_speed(int speed);
  36. int zy1000_register_commands(struct command_context_s *cmd_ctx);
  37. int zy1000_init(void);
  38. int zy1000_quit(void);
  39. /* interface commands */
  40. int zy1000_handle_zy1000_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  41. static int zy1000_khz(int khz, int *jtag_speed)
  42. {
  43. if (khz==0)
  44. {
  45. *jtag_speed=0;
  46. }
  47. else
  48. {
  49. *jtag_speed=64000/khz;
  50. }
  51. return ERROR_OK;
  52. }
  53. static int zy1000_speed_div(int speed, int *khz)
  54. {
  55. if (speed==0)
  56. {
  57. *khz = 0;
  58. }
  59. else
  60. {
  61. *khz=64000/speed;
  62. }
  63. return ERROR_OK;
  64. }
  65. static bool readPowerDropout(void)
  66. {
  67. cyg_uint32 state;
  68. // sample and clear power dropout
  69. HAL_WRITE_UINT32(ZY1000_JTAG_BASE+0x10, 0x80);
  70. HAL_READ_UINT32(ZY1000_JTAG_BASE+0x10, state);
  71. bool powerDropout;
  72. powerDropout = (state & 0x80) != 0;
  73. return powerDropout;
  74. }
  75. static bool readSRST(void)
  76. {
  77. cyg_uint32 state;
  78. // sample and clear SRST sensing
  79. HAL_WRITE_UINT32(ZY1000_JTAG_BASE+0x10, 0x00000040);
  80. HAL_READ_UINT32(ZY1000_JTAG_BASE+0x10, state);
  81. bool srstAsserted;
  82. srstAsserted = (state & 0x40) != 0;
  83. return srstAsserted;
  84. }
  85. static int zy1000_srst_asserted(int *srst_asserted)
  86. {
  87. *srst_asserted=readSRST();
  88. return ERROR_OK;
  89. }
  90. static int zy1000_power_dropout(int *dropout)
  91. {
  92. *dropout=readPowerDropout();
  93. return ERROR_OK;
  94. }
  95. jtag_interface_t zy1000_interface =
  96. {
  97. .name = "ZY1000",
  98. .execute_queue = NULL,
  99. .speed = zy1000_speed,
  100. .register_commands = zy1000_register_commands,
  101. .init = zy1000_init,
  102. .quit = zy1000_quit,
  103. .khz = zy1000_khz,
  104. .speed_div = zy1000_speed_div,
  105. .power_dropout = zy1000_power_dropout,
  106. .srst_asserted = zy1000_srst_asserted,
  107. };
  108. void zy1000_reset(int trst, int srst)
  109. {
  110. LOG_DEBUG("zy1000 trst=%d, srst=%d", trst, srst);
  111. if (!srst)
  112. {
  113. ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x00000001);
  114. }
  115. else
  116. {
  117. /* Danger!!! if clk!=0 when in
  118. * idle in TAP_IDLE, reset halt on str912 will fail.
  119. */
  120. ZY1000_POKE(ZY1000_JTAG_BASE+0x10, 0x00000001);
  121. }
  122. if (!trst)
  123. {
  124. ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x00000002);
  125. }
  126. else
  127. {
  128. /* assert reset */
  129. ZY1000_POKE(ZY1000_JTAG_BASE+0x10, 0x00000002);
  130. }
  131. if (trst||(srst&&(jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
  132. {
  133. waitIdle();
  134. /* we're now in the RESET state until trst is deasserted */
  135. ZY1000_POKE(ZY1000_JTAG_BASE+0x20, TAP_RESET);
  136. } else
  137. {
  138. /* We'll get RCLK failure when we assert TRST, so clear any false positives here */
  139. ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x400);
  140. }
  141. /* wait for srst to float back up */
  142. if (!srst)
  143. {
  144. int i;
  145. for (i=0; i<1000; i++)
  146. {
  147. // We don't want to sense our own reset, so we clear here.
  148. // There is of course a timing hole where we could loose
  149. // a "real" reset.
  150. if (!readSRST())
  151. break;
  152. /* wait 1ms */
  153. alive_sleep(1);
  154. }
  155. if (i==1000)
  156. {
  157. LOG_USER("SRST didn't deassert after %dms", i);
  158. } else if (i>1)
  159. {
  160. LOG_USER("SRST took %dms to deassert", i);
  161. }
  162. }
  163. }
  164. int zy1000_speed(int speed)
  165. {
  166. if (speed == 0)
  167. {
  168. /*0 means RCLK*/
  169. speed = 0;
  170. ZY1000_POKE(ZY1000_JTAG_BASE+0x10, 0x100);
  171. LOG_DEBUG("jtag_speed using RCLK");
  172. }
  173. else
  174. {
  175. if (speed > 8190 || speed < 2)
  176. {
  177. LOG_USER("valid ZY1000 jtag_speed=[8190,2]. Divisor is 64MHz / even values between 8190-2, i.e. min 7814Hz, max 32MHz");
  178. return ERROR_INVALID_ARGUMENTS;
  179. }
  180. LOG_USER("jtag_speed %d => JTAG clk=%f", speed, 64.0/(float)speed);
  181. ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x100);
  182. ZY1000_POKE(ZY1000_JTAG_BASE+0x1c, speed&~1);
  183. }
  184. return ERROR_OK;
  185. }
  186. static bool savePower;
  187. static void setPower(bool power)
  188. {
  189. savePower = power;
  190. if (power)
  191. {
  192. HAL_WRITE_UINT32(ZY1000_JTAG_BASE+0x14, 0x8);
  193. } else
  194. {
  195. HAL_WRITE_UINT32(ZY1000_JTAG_BASE+0x10, 0x8);
  196. }
  197. }
  198. int handle_power_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  199. {
  200. if (argc > 1)
  201. {
  202. return ERROR_INVALID_ARGUMENTS;
  203. }
  204. if (argc == 1)
  205. {
  206. if (strcmp(args[0], "on") == 0)
  207. {
  208. setPower(1);
  209. }
  210. else if (strcmp(args[0], "off") == 0)
  211. {
  212. setPower(0);
  213. } else
  214. {
  215. command_print(cmd_ctx, "arg is \"on\" or \"off\"");
  216. return ERROR_INVALID_ARGUMENTS;
  217. }
  218. }
  219. command_print(cmd_ctx, "Target power %s", savePower ? "on" : "off");
  220. return ERROR_OK;
  221. }
  222. /* Give TELNET a way to find out what version this is */
  223. static int jim_zy1000_version(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  224. {
  225. if ((argc < 1) || (argc > 2))
  226. return JIM_ERR;
  227. char buff[128];
  228. const char *version_str=NULL;
  229. if (argc == 1)
  230. {
  231. version_str=ZYLIN_OPENOCD_VERSION;
  232. } else
  233. {
  234. const char *str = Jim_GetString(argv[1], NULL);
  235. if (strcmp("openocd", str) == 0)
  236. {
  237. int revision;
  238. revision = atol(ZYLIN_OPENOCD+strlen("XRevision: "));
  239. sprintf(buff, "%d", revision);
  240. version_str=buff;
  241. }
  242. else if (strcmp("zy1000", str) == 0)
  243. {
  244. version_str=ZYLIN_VERSION;
  245. }
  246. else if (strcmp("date", str) == 0)
  247. {
  248. version_str=ZYLIN_DATE;
  249. }
  250. else
  251. {
  252. return JIM_ERR;
  253. }
  254. }
  255. Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));
  256. return JIM_OK;
  257. }
  258. static int
  259. zylinjtag_Jim_Command_powerstatus(Jim_Interp *interp,
  260. int argc,
  261. Jim_Obj * const *argv)
  262. {
  263. if (argc != 1)
  264. {
  265. Jim_WrongNumArgs(interp, 1, argv, "powerstatus");
  266. return JIM_ERR;
  267. }
  268. cyg_uint32 status;
  269. ZY1000_PEEK(ZY1000_JTAG_BASE+0x10, status);
  270. Jim_SetResult(interp, Jim_NewIntObj(interp, (status&0x80)!=0));
  271. return JIM_OK;
  272. }
  273. int zy1000_register_commands(struct command_context_s *cmd_ctx)
  274. {
  275. register_command(cmd_ctx, NULL, "power", handle_power_command, COMMAND_ANY,
  276. "power <on/off> - turn power switch to target on/off. No arguments - print status.");
  277. Jim_CreateCommand(interp, "zy1000_version", jim_zy1000_version, NULL, NULL);
  278. Jim_CreateCommand(interp, "powerstatus", zylinjtag_Jim_Command_powerstatus, NULL, NULL);
  279. return ERROR_OK;
  280. }
  281. int zy1000_init(void)
  282. {
  283. LOG_USER("%s", ZYLIN_OPENOCD_VERSION);
  284. ZY1000_POKE(ZY1000_JTAG_BASE+0x10, 0x30); // Turn on LED1 & LED2
  285. setPower(true); // on by default
  286. /* deassert resets. Important to avoid infinite loop waiting for SRST to deassert */
  287. zy1000_reset(0, 0);
  288. zy1000_speed(jtag_get_speed());
  289. return ERROR_OK;
  290. }
  291. int zy1000_quit(void)
  292. {
  293. return ERROR_OK;
  294. }
  295. int interface_jtag_execute_queue(void)
  296. {
  297. cyg_uint32 empty;
  298. waitIdle();
  299. ZY1000_PEEK(ZY1000_JTAG_BASE+0x10, empty);
  300. /* clear JTAG error register */
  301. ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x400);
  302. if ((empty&0x400)!=0)
  303. {
  304. LOG_WARNING("RCLK timeout");
  305. /* the error is informative only as we don't want to break the firmware if there
  306. * is a false positive.
  307. */
  308. // return ERROR_FAIL;
  309. }
  310. return ERROR_OK;
  311. }
  312. static cyg_uint32 getShiftValue(void)
  313. {
  314. cyg_uint32 value;
  315. waitIdle();
  316. ZY1000_PEEK(ZY1000_JTAG_BASE+0xc, value);
  317. VERBOSE(LOG_INFO("getShiftValue %08x", value));
  318. return value;
  319. }
  320. #if 0
  321. static cyg_uint32 getShiftValueFlip(void)
  322. {
  323. cyg_uint32 value;
  324. waitIdle();
  325. ZY1000_PEEK(ZY1000_JTAG_BASE+0x18, value);
  326. VERBOSE(LOG_INFO("getShiftValue %08x (flipped)", value));
  327. return value;
  328. }
  329. #endif
  330. #if 0
  331. static void shiftValueInnerFlip(const tap_state_t state, const tap_state_t endState, int repeat, cyg_uint32 value)
  332. {
  333. VERBOSE(LOG_INFO("shiftValueInner %s %s %d %08x (flipped)", tap_state_name(state), tap_state_name(endState), repeat, value));
  334. cyg_uint32 a,b;
  335. a=state;
  336. b=endState;
  337. ZY1000_POKE(ZY1000_JTAG_BASE+0xc, value);
  338. ZY1000_POKE(ZY1000_JTAG_BASE+0x8, (1<<15)|(repeat<<8)|(a<<4)|b);
  339. VERBOSE(getShiftValueFlip());
  340. }
  341. #endif
  342. extern int jtag_check_value(uint8_t *captured, void *priv);
  343. static __inline void scanFields(int num_fields, const scan_field_t *fields, tap_state_t shiftState, tap_state_t end_state)
  344. {
  345. int i;
  346. int j;
  347. int k;
  348. for (i = 0; i < num_fields; i++)
  349. {
  350. cyg_uint32 value;
  351. uint8_t *inBuffer=NULL;
  352. // figure out where to store the input data
  353. int num_bits=fields[i].num_bits;
  354. if (fields[i].in_value!=NULL)
  355. {
  356. inBuffer=fields[i].in_value;
  357. }
  358. // here we shuffle N bits out/in
  359. j=0;
  360. while (j<num_bits)
  361. {
  362. tap_state_t pause_state;
  363. int l;
  364. k=num_bits-j;
  365. pause_state=(shiftState==TAP_DRSHIFT)?TAP_DRSHIFT:TAP_IRSHIFT;
  366. if (k>32)
  367. {
  368. k=32;
  369. /* we have more to shift out */
  370. } else if (i == num_fields-1)
  371. {
  372. /* this was the last to shift out this time */
  373. pause_state=end_state;
  374. }
  375. // we have (num_bits+7)/8 bytes of bits to toggle out.
  376. // bits are pushed out LSB to MSB
  377. value=0;
  378. if (fields[i].out_value!=NULL)
  379. {
  380. for (l=0; l<k; l+=8)
  381. {
  382. value|=fields[i].out_value[(j+l)/8]<<l;
  383. }
  384. }
  385. /* mask away unused bits for easier debugging */
  386. value&=~(((uint32_t)0xffffffff)<<k);
  387. shiftValueInner(shiftState, pause_state, k, value);
  388. if (inBuffer!=NULL)
  389. {
  390. // data in, LSB to MSB
  391. value=getShiftValue();
  392. // we're shifting in data to MSB, shift data to be aligned for returning the value
  393. value >>= 32-k;
  394. for (l=0; l<k; l+=8)
  395. {
  396. inBuffer[(j+l)/8]=(value>>l)&0xff;
  397. }
  398. }
  399. j+=k;
  400. }
  401. }
  402. }
  403. int interface_jtag_set_end_state(tap_state_t state)
  404. {
  405. return ERROR_OK;
  406. }
  407. int interface_jtag_add_ir_scan(int num_fields, const scan_field_t *fields, tap_state_t state)
  408. {
  409. int j;
  410. int scan_size = 0;
  411. jtag_tap_t *tap, *nextTap;
  412. for (tap = jtag_tap_next_enabled(NULL); tap!= NULL; tap=nextTap)
  413. {
  414. nextTap=jtag_tap_next_enabled(tap);
  415. tap_state_t end_state;
  416. if (nextTap==NULL)
  417. {
  418. end_state = state;
  419. } else
  420. {
  421. end_state = TAP_IRSHIFT;
  422. }
  423. int found = 0;
  424. scan_size = tap->ir_length;
  425. /* search the list */
  426. for (j=0; j < num_fields; j++)
  427. {
  428. if (tap == fields[j].tap)
  429. {
  430. found = 1;
  431. scanFields(1, fields+j, TAP_IRSHIFT, end_state);
  432. /* update device information */
  433. buf_cpy(fields[j].out_value, tap->cur_instr, scan_size);
  434. tap->bypass = 0;
  435. break;
  436. }
  437. }
  438. if (!found)
  439. {
  440. /* if a device isn't listed, set it to BYPASS */
  441. uint8_t ones[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
  442. scan_field_t tmp;
  443. memset(&tmp, 0, sizeof(tmp));
  444. tmp.out_value = ones;
  445. tmp.num_bits = scan_size;
  446. scanFields(1, &tmp, TAP_IRSHIFT, end_state);
  447. /* update device information */
  448. buf_cpy(tmp.out_value, tap->cur_instr, scan_size);
  449. tap->bypass = 1;
  450. }
  451. }
  452. return ERROR_OK;
  453. }
  454. int interface_jtag_add_plain_ir_scan(int num_fields, const scan_field_t *fields, tap_state_t state)
  455. {
  456. scanFields(num_fields, fields, TAP_IRSHIFT, state);
  457. return ERROR_OK;
  458. }
  459. /*extern jtag_command_t **jtag_get_last_command_p(void);*/
  460. int interface_jtag_add_dr_scan(int num_fields, const scan_field_t *fields, tap_state_t state)
  461. {
  462. int j;
  463. jtag_tap_t *tap, *nextTap;
  464. for (tap = jtag_tap_next_enabled(NULL); tap!= NULL; tap=nextTap)
  465. {
  466. nextTap=jtag_tap_next_enabled(tap);
  467. int found=0;
  468. tap_state_t end_state;
  469. if (nextTap==NULL)
  470. {
  471. end_state = state;
  472. } else
  473. {
  474. end_state = TAP_DRSHIFT;
  475. }
  476. for (j=0; j < num_fields; j++)
  477. {
  478. if (tap == fields[j].tap)
  479. {
  480. found = 1;
  481. scanFields(1, fields+j, TAP_DRSHIFT, end_state);
  482. }
  483. }
  484. if (!found)
  485. {
  486. scan_field_t tmp;
  487. /* program the scan field to 1 bit length, and ignore it's value */
  488. tmp.num_bits = 1;
  489. tmp.out_value = NULL;
  490. tmp.in_value = NULL;
  491. scanFields(1, &tmp, TAP_DRSHIFT, end_state);
  492. }
  493. else
  494. {
  495. }
  496. }
  497. return ERROR_OK;
  498. }
  499. int interface_jtag_add_plain_dr_scan(int num_fields, const scan_field_t *fields, tap_state_t state)
  500. {
  501. scanFields(num_fields, fields, TAP_DRSHIFT, state);
  502. return ERROR_OK;
  503. }
  504. int interface_jtag_add_tlr()
  505. {
  506. setCurrentState(TAP_RESET);
  507. return ERROR_OK;
  508. }
  509. extern int jtag_nsrst_delay;
  510. extern int jtag_ntrst_delay;
  511. int interface_jtag_add_reset(int req_trst, int req_srst)
  512. {
  513. zy1000_reset(req_trst, req_srst);
  514. return ERROR_OK;
  515. }
  516. static int zy1000_jtag_add_clocks(int num_cycles, tap_state_t state, tap_state_t clockstate)
  517. {
  518. /* num_cycles can be 0 */
  519. setCurrentState(clockstate);
  520. /* execute num_cycles, 32 at the time. */
  521. int i;
  522. for (i=0; i<num_cycles; i+=32)
  523. {
  524. int num;
  525. num=32;
  526. if (num_cycles-i<num)
  527. {
  528. num=num_cycles-i;
  529. }
  530. shiftValueInner(clockstate, clockstate, num, 0);
  531. }
  532. #if !TEST_MANUAL()
  533. /* finish in end_state */
  534. setCurrentState(state);
  535. #else
  536. tap_state_t t=TAP_IDLE;
  537. /* test manual drive code on any target */
  538. int tms;
  539. uint8_t tms_scan = tap_get_tms_path(t, state);
  540. int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  541. for (i = 0; i < tms_count; i++)
  542. {
  543. tms = (tms_scan >> i) & 1;
  544. waitIdle();
  545. ZY1000_POKE(ZY1000_JTAG_BASE+0x28, tms);
  546. }
  547. waitIdle();
  548. ZY1000_POKE(ZY1000_JTAG_BASE+0x20, state);
  549. #endif
  550. return ERROR_OK;
  551. }
  552. int interface_jtag_add_runtest(int num_cycles, tap_state_t state)
  553. {
  554. return zy1000_jtag_add_clocks(num_cycles, state, TAP_IDLE);
  555. }
  556. int interface_jtag_add_clocks(int num_cycles)
  557. {
  558. return zy1000_jtag_add_clocks(num_cycles, cmd_queue_cur_state, cmd_queue_cur_state);
  559. }
  560. int interface_jtag_add_sleep(uint32_t us)
  561. {
  562. jtag_sleep(us);
  563. return ERROR_OK;
  564. }
  565. int interface_jtag_add_pathmove(int num_states, const tap_state_t *path)
  566. {
  567. int state_count;
  568. int tms = 0;
  569. /*wait for the fifo to be empty*/
  570. waitIdle();
  571. state_count = 0;
  572. tap_state_t cur_state=cmd_queue_cur_state;
  573. while (num_states)
  574. {
  575. if (tap_state_transition(cur_state, false) == path[state_count])
  576. {
  577. tms = 0;
  578. }
  579. else if (tap_state_transition(cur_state, true) == path[state_count])
  580. {
  581. tms = 1;
  582. }
  583. else
  584. {
  585. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(cur_state), tap_state_name(path[state_count]));
  586. exit(-1);
  587. }
  588. waitIdle();
  589. ZY1000_POKE(ZY1000_JTAG_BASE+0x28, tms);
  590. cur_state = path[state_count];
  591. state_count++;
  592. num_states--;
  593. }
  594. waitIdle();
  595. ZY1000_POKE(ZY1000_JTAG_BASE+0x20, cur_state);
  596. return ERROR_OK;
  597. }
  598. void embeddedice_write_dcc(jtag_tap_t *tap, int reg_addr, uint8_t *buffer, int little, int count)
  599. {
  600. // static int const reg_addr=0x5;
  601. tap_state_t end_state=jtag_get_end_state();
  602. if (jtag_tap_next_enabled(jtag_tap_next_enabled(NULL))==NULL)
  603. {
  604. /* better performance via code duplication */
  605. if (little)
  606. {
  607. int i;
  608. for (i = 0; i < count; i++)
  609. {
  610. shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 1));
  611. shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr|(1<<5));
  612. buffer+=4;
  613. }
  614. } else
  615. {
  616. int i;
  617. for (i = 0; i < count; i++)
  618. {
  619. shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 0));
  620. shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr|(1<<5));
  621. buffer+=4;
  622. }
  623. }
  624. }
  625. else
  626. {
  627. int i;
  628. for (i = 0; i < count; i++)
  629. {
  630. embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, little));
  631. buffer += 4;
  632. }
  633. }
  634. }