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.
 
 
 
 
 
 

1265 lines
30 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007-2010 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. /* This file supports the zy1000 debugger: http://www.zylin.com/zy1000.html
  20. *
  21. * The zy1000 is a standalone debugger that has a web interface and
  22. * requires no drivers on the developer host as all communication
  23. * is via TCP/IP. The zy1000 gets it performance(~400-700kBytes/s
  24. * DCC downloads @ 16MHz target) as it has an FPGA to hardware
  25. * accelerate the JTAG commands, while offering *very* low latency
  26. * between OpenOCD and the FPGA registers.
  27. *
  28. * The disadvantage of the zy1000 is that it has a feeble CPU compared to
  29. * a PC(ca. 50-500 DMIPS depending on how one counts it), whereas a PC
  30. * is on the order of 10000 DMIPS(i.e. at a factor of 20-200).
  31. *
  32. * The zy1000 revc hardware is using an Altera Nios CPU, whereas the
  33. * revb is using ARM7 + Xilinx.
  34. *
  35. * See Zylin web pages or contact Zylin for more information.
  36. *
  37. * The reason this code is in OpenOCD rather than OpenOCD linked with the
  38. * ZY1000 code is that OpenOCD is the long road towards getting
  39. * libopenocd into place. libopenocd will support both low performance,
  40. * low latency systems(embedded) and high performance high latency
  41. * systems(PCs).
  42. */
  43. #ifdef HAVE_CONFIG_H
  44. #include "config.h"
  45. #endif
  46. #include <pthread.h>
  47. #include <target/embeddedice.h>
  48. #include <jtag/minidriver.h>
  49. #include <jtag/interface.h>
  50. #include <time.h>
  51. #include <helper/time_support.h>
  52. #include <netinet/tcp.h>
  53. /* Assume we're connecting to a revc w/60MHz clock. */
  54. #define ZYLIN_KHZ 60000
  55. /* The software needs to check if it's in RCLK mode or not */
  56. static bool zy1000_rclk;
  57. static int zy1000_khz(int khz, int *jtag_speed)
  58. {
  59. if (khz == 0)
  60. *jtag_speed = 0;
  61. else {
  62. int speed;
  63. /* Round speed up to nearest divisor.
  64. *
  65. * E.g. 16000kHz
  66. * (64000 + 15999) / 16000 = 4
  67. * (4 + 1) / 2 = 2
  68. * 2 * 2 = 4
  69. *
  70. * 64000 / 4 = 16000
  71. *
  72. * E.g. 15999
  73. * (64000 + 15998) / 15999 = 5
  74. * (5 + 1) / 2 = 3
  75. * 3 * 2 = 6
  76. *
  77. * 64000 / 6 = 10666
  78. *
  79. */
  80. speed = (ZYLIN_KHZ + (khz - 1)) / khz;
  81. speed = (speed + 1) / 2;
  82. speed *= 2;
  83. if (speed > 8190) {
  84. /* maximum dividend */
  85. speed = 8190;
  86. }
  87. *jtag_speed = speed;
  88. }
  89. return ERROR_OK;
  90. }
  91. static int zy1000_speed_div(int speed, int *khz)
  92. {
  93. if (speed == 0)
  94. *khz = 0;
  95. else
  96. *khz = ZYLIN_KHZ / speed;
  97. return ERROR_OK;
  98. }
  99. static bool readPowerDropout(void)
  100. {
  101. uint32_t state;
  102. /* sample and clear power dropout */
  103. ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x80);
  104. ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, state);
  105. bool powerDropout;
  106. powerDropout = (state & 0x80) != 0;
  107. return powerDropout;
  108. }
  109. static bool readSRST(void)
  110. {
  111. uint32_t state;
  112. /* sample and clear SRST sensing */
  113. ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x00000040);
  114. ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, state);
  115. bool srstAsserted;
  116. srstAsserted = (state & 0x40) != 0;
  117. return srstAsserted;
  118. }
  119. static int zy1000_srst_asserted(int *srst_asserted)
  120. {
  121. *srst_asserted = readSRST();
  122. return ERROR_OK;
  123. }
  124. static int zy1000_power_dropout(int *dropout)
  125. {
  126. *dropout = readPowerDropout();
  127. return ERROR_OK;
  128. }
  129. /* Wait for SRST to assert or deassert */
  130. static void waitSRST(bool asserted)
  131. {
  132. bool first = true;
  133. long long start = 0;
  134. long total = 0;
  135. const char *mode = asserted ? "assert" : "deassert";
  136. for (;; ) {
  137. bool srstAsserted = readSRST();
  138. if ((asserted && srstAsserted) || (!asserted && !srstAsserted)) {
  139. if (total > 1)
  140. LOG_USER("SRST took %dms to %s", (int)total, mode);
  141. break;
  142. }
  143. if (first) {
  144. first = false;
  145. start = timeval_ms();
  146. }
  147. total = timeval_ms() - start;
  148. keep_alive();
  149. if (total > 5000) {
  150. LOG_ERROR("SRST took too long to %s: %dms", mode, (int)total);
  151. break;
  152. }
  153. }
  154. }
  155. void zy1000_reset(int trst, int srst)
  156. {
  157. LOG_DEBUG("zy1000 trst=%d, srst=%d", trst, srst);
  158. /* flush the JTAG FIFO. Not flushing the queue before messing with
  159. * reset has such interesting bugs as causing hard to reproduce
  160. * RCLK bugs as RCLK will stop responding when TRST is asserted
  161. */
  162. waitIdle();
  163. if (!srst)
  164. ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x00000001);
  165. else {
  166. /* Danger!!! if clk != 0 when in
  167. * idle in TAP_IDLE, reset halt on str912 will fail.
  168. */
  169. ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x00000001);
  170. waitSRST(true);
  171. }
  172. if (!trst)
  173. ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x00000002);
  174. else {
  175. /* assert reset */
  176. ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x00000002);
  177. }
  178. if (trst || (srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST))) {
  179. /* we're now in the RESET state until trst is deasserted */
  180. ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, TAP_RESET);
  181. } else {
  182. /* We'll get RCLK failure when we assert TRST, so clear any false positives here */
  183. ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x400);
  184. }
  185. /* wait for srst to float back up */
  186. if ((!srst && ((jtag_get_reset_config() & RESET_TRST_PULLS_SRST) == 0)) ||
  187. (!srst && !trst && (jtag_get_reset_config() & RESET_TRST_PULLS_SRST)))
  188. waitSRST(false);
  189. }
  190. int zy1000_speed(int speed)
  191. {
  192. /* flush JTAG master FIFO before setting speed */
  193. waitIdle();
  194. zy1000_rclk = false;
  195. if (speed == 0) {
  196. /*0 means RCLK*/
  197. ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x100);
  198. zy1000_rclk = true;
  199. LOG_DEBUG("jtag_speed using RCLK");
  200. } else {
  201. if (speed > 8190 || speed < 2) {
  202. LOG_USER(
  203. "valid ZY1000 jtag_speed=[8190,2]. With divisor is %dkHz / even values between 8190-2, i.e. min %dHz, max %dMHz",
  204. ZYLIN_KHZ,
  205. (ZYLIN_KHZ * 1000) / 8190,
  206. ZYLIN_KHZ / (2 * 1000));
  207. return ERROR_COMMAND_SYNTAX_ERROR;
  208. }
  209. int khz;
  210. speed &= ~1;
  211. zy1000_speed_div(speed, &khz);
  212. LOG_USER("jtag_speed %d => JTAG clk=%d kHz", speed, khz);
  213. ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x100);
  214. ZY1000_POKE(ZY1000_JTAG_BASE + 0x1c, speed);
  215. }
  216. return ERROR_OK;
  217. }
  218. static bool savePower;
  219. static void setPower(bool power)
  220. {
  221. savePower = power;
  222. if (power)
  223. ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x8);
  224. else
  225. ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x8);
  226. }
  227. COMMAND_HANDLER(handle_power_command)
  228. {
  229. switch (CMD_ARGC) {
  230. case 1: {
  231. bool enable;
  232. COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
  233. setPower(enable);
  234. /* fall through */
  235. }
  236. case 0:
  237. LOG_INFO("Target power %s", savePower ? "on" : "off");
  238. break;
  239. default:
  240. return ERROR_COMMAND_SYNTAX_ERROR;
  241. }
  242. return ERROR_OK;
  243. }
  244. #if !BUILD_ZY1000_MASTER
  245. static char *tcp_server = "notspecified";
  246. static int jim_zy1000_server(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
  247. {
  248. if (argc != 2)
  249. return JIM_ERR;
  250. tcp_server = strdup(Jim_GetString(argv[1], NULL));
  251. return JIM_OK;
  252. }
  253. #endif
  254. static int zylinjtag_Jim_Command_powerstatus(Jim_Interp *interp,
  255. int argc,
  256. Jim_Obj * const *argv)
  257. {
  258. if (argc != 1) {
  259. Jim_WrongNumArgs(interp, 1, argv, "powerstatus");
  260. return JIM_ERR;
  261. }
  262. bool dropout = readPowerDropout();
  263. Jim_SetResult(interp, Jim_NewIntObj(interp, dropout));
  264. return JIM_OK;
  265. }
  266. int zy1000_quit(void)
  267. {
  268. return ERROR_OK;
  269. }
  270. int interface_jtag_execute_queue(void)
  271. {
  272. uint32_t empty;
  273. waitIdle();
  274. /* We must make sure to write data read back to memory location before we return
  275. * from this fn
  276. */
  277. zy1000_flush_readqueue();
  278. /* and handle any callbacks... */
  279. zy1000_flush_callbackqueue();
  280. if (zy1000_rclk) {
  281. /* Only check for errors when using RCLK to speed up
  282. * jtag over TCP/IP
  283. */
  284. ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, empty);
  285. /* clear JTAG error register */
  286. ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x400);
  287. if ((empty&0x400) != 0) {
  288. LOG_WARNING("RCLK timeout");
  289. /* the error is informative only as we don't want to break the firmware if there
  290. * is a false positive.
  291. */
  292. /* return ERROR_FAIL; */
  293. }
  294. }
  295. return ERROR_OK;
  296. }
  297. static void writeShiftValue(uint8_t *data, int bits);
  298. /* here we shuffle N bits out/in */
  299. static inline void scanBits(const uint8_t *out_value,
  300. uint8_t *in_value,
  301. int num_bits,
  302. bool pause_now,
  303. tap_state_t shiftState,
  304. tap_state_t end_state)
  305. {
  306. tap_state_t pause_state = shiftState;
  307. for (int j = 0; j < num_bits; j += 32) {
  308. int k = num_bits - j;
  309. if (k > 32) {
  310. k = 32;
  311. /* we have more to shift out */
  312. } else if (pause_now) {
  313. /* this was the last to shift out this time */
  314. pause_state = end_state;
  315. }
  316. /* we have (num_bits + 7)/8 bytes of bits to toggle out. */
  317. /* bits are pushed out LSB to MSB */
  318. uint32_t value;
  319. value = 0;
  320. if (out_value != NULL) {
  321. for (int l = 0; l < k; l += 8)
  322. value |= out_value[(j + l)/8]<<l;
  323. }
  324. /* mask away unused bits for easier debugging */
  325. if (k < 32)
  326. value &= ~(((uint32_t)0xffffffff) << k);
  327. else {
  328. /* Shifting by >= 32 is not defined by the C standard
  329. * and will in fact shift by &0x1f bits on nios */
  330. }
  331. shiftValueInner(shiftState, pause_state, k, value);
  332. if (in_value != NULL)
  333. writeShiftValue(in_value + (j/8), k);
  334. }
  335. }
  336. static inline void scanFields(int num_fields,
  337. const struct scan_field *fields,
  338. tap_state_t shiftState,
  339. tap_state_t end_state)
  340. {
  341. for (int i = 0; i < num_fields; i++) {
  342. scanBits(fields[i].out_value,
  343. fields[i].in_value,
  344. fields[i].num_bits,
  345. (i == num_fields-1),
  346. shiftState,
  347. end_state);
  348. }
  349. }
  350. int interface_jtag_add_ir_scan(struct jtag_tap *active,
  351. const struct scan_field *fields,
  352. tap_state_t state)
  353. {
  354. int scan_size = 0;
  355. struct jtag_tap *tap, *nextTap;
  356. tap_state_t pause_state = TAP_IRSHIFT;
  357. for (tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = nextTap) {
  358. nextTap = jtag_tap_next_enabled(tap);
  359. if (nextTap == NULL)
  360. pause_state = state;
  361. scan_size = tap->ir_length;
  362. /* search the list */
  363. if (tap == active) {
  364. scanFields(1, fields, TAP_IRSHIFT, pause_state);
  365. /* update device information */
  366. buf_cpy(fields[0].out_value, tap->cur_instr, scan_size);
  367. tap->bypass = 0;
  368. } else {
  369. /* if a device isn't listed, set it to BYPASS */
  370. assert(scan_size <= 32);
  371. shiftValueInner(TAP_IRSHIFT, pause_state, scan_size, 0xffffffff);
  372. /* Optimization code will check what the cur_instr is set to, so
  373. * we must set it to bypass value.
  374. */
  375. buf_set_ones(tap->cur_instr, tap->ir_length);
  376. tap->bypass = 1;
  377. }
  378. }
  379. return ERROR_OK;
  380. }
  381. int interface_jtag_add_plain_ir_scan(int num_bits,
  382. const uint8_t *out_bits,
  383. uint8_t *in_bits,
  384. tap_state_t state)
  385. {
  386. scanBits(out_bits, in_bits, num_bits, true, TAP_IRSHIFT, state);
  387. return ERROR_OK;
  388. }
  389. int interface_jtag_add_dr_scan(struct jtag_tap *active,
  390. int num_fields,
  391. const struct scan_field *fields,
  392. tap_state_t state)
  393. {
  394. struct jtag_tap *tap, *nextTap;
  395. tap_state_t pause_state = TAP_DRSHIFT;
  396. for (tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = nextTap) {
  397. nextTap = jtag_tap_next_enabled(tap);
  398. if (nextTap == NULL)
  399. pause_state = state;
  400. /* Find a range of fields to write to this tap */
  401. if (tap == active) {
  402. assert(!tap->bypass);
  403. scanFields(num_fields, fields, TAP_DRSHIFT, pause_state);
  404. } else {
  405. /* Shift out a 0 for disabled tap's */
  406. assert(tap->bypass);
  407. shiftValueInner(TAP_DRSHIFT, pause_state, 1, 0);
  408. }
  409. }
  410. return ERROR_OK;
  411. }
  412. int interface_jtag_add_plain_dr_scan(int num_bits,
  413. const uint8_t *out_bits,
  414. uint8_t *in_bits,
  415. tap_state_t state)
  416. {
  417. scanBits(out_bits, in_bits, num_bits, true, TAP_DRSHIFT, state);
  418. return ERROR_OK;
  419. }
  420. int interface_jtag_add_tlr()
  421. {
  422. setCurrentState(TAP_RESET);
  423. return ERROR_OK;
  424. }
  425. int interface_jtag_add_reset(int req_trst, int req_srst)
  426. {
  427. zy1000_reset(req_trst, req_srst);
  428. return ERROR_OK;
  429. }
  430. static int zy1000_jtag_add_clocks(int num_cycles, tap_state_t state, tap_state_t clockstate)
  431. {
  432. /* num_cycles can be 0 */
  433. setCurrentState(clockstate);
  434. /* execute num_cycles, 32 at the time. */
  435. int i;
  436. for (i = 0; i < num_cycles; i += 32) {
  437. int num;
  438. num = 32;
  439. if (num_cycles-i < num)
  440. num = num_cycles-i;
  441. shiftValueInner(clockstate, clockstate, num, 0);
  442. }
  443. #if !TEST_MANUAL()
  444. /* finish in end_state */
  445. setCurrentState(state);
  446. #else
  447. tap_state_t t = TAP_IDLE;
  448. /* test manual drive code on any target */
  449. int tms;
  450. uint8_t tms_scan = tap_get_tms_path(t, state);
  451. int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  452. for (i = 0; i < tms_count; i++) {
  453. tms = (tms_scan >> i) & 1;
  454. waitIdle();
  455. ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, tms);
  456. }
  457. waitIdle();
  458. ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, state);
  459. #endif
  460. return ERROR_OK;
  461. }
  462. int interface_jtag_add_runtest(int num_cycles, tap_state_t state)
  463. {
  464. return zy1000_jtag_add_clocks(num_cycles, state, TAP_IDLE);
  465. }
  466. int interface_jtag_add_clocks(int num_cycles)
  467. {
  468. return zy1000_jtag_add_clocks(num_cycles, cmd_queue_cur_state, cmd_queue_cur_state);
  469. }
  470. int interface_add_tms_seq(unsigned num_bits, const uint8_t *seq, enum tap_state state)
  471. {
  472. /*wait for the fifo to be empty*/
  473. waitIdle();
  474. for (unsigned i = 0; i < num_bits; i++) {
  475. int tms;
  476. if (((seq[i/8] >> (i % 8)) & 1) == 0)
  477. tms = 0;
  478. else
  479. tms = 1;
  480. waitIdle();
  481. ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, tms);
  482. }
  483. waitIdle();
  484. if (state != TAP_INVALID)
  485. ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, state);
  486. else {
  487. /* this would be normal if
  488. * we are switching to SWD mode */
  489. }
  490. return ERROR_OK;
  491. }
  492. int interface_jtag_add_pathmove(int num_states, const tap_state_t *path)
  493. {
  494. int state_count;
  495. int tms = 0;
  496. state_count = 0;
  497. tap_state_t cur_state = cmd_queue_cur_state;
  498. uint8_t seq[16];
  499. memset(seq, 0, sizeof(seq));
  500. assert(num_states < (int)((sizeof(seq) * 8)));
  501. while (num_states) {
  502. if (tap_state_transition(cur_state, false) == path[state_count])
  503. tms = 0;
  504. else if (tap_state_transition(cur_state, true) == path[state_count])
  505. tms = 1;
  506. else {
  507. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
  508. tap_state_name(cur_state), tap_state_name(path[state_count]));
  509. exit(-1);
  510. }
  511. seq[state_count/8] = seq[state_count/8] | (tms << (state_count % 8));
  512. cur_state = path[state_count];
  513. state_count++;
  514. num_states--;
  515. }
  516. return interface_add_tms_seq(state_count, seq, cur_state);
  517. }
  518. static void jtag_pre_post_bits(struct jtag_tap *tap, int *pre, int *post)
  519. {
  520. /* bypass bits before and after */
  521. int pre_bits = 0;
  522. int post_bits = 0;
  523. bool found = false;
  524. struct jtag_tap *cur_tap, *nextTap;
  525. for (cur_tap = jtag_tap_next_enabled(NULL); cur_tap != NULL; cur_tap = nextTap) {
  526. nextTap = jtag_tap_next_enabled(cur_tap);
  527. if (cur_tap == tap)
  528. found = true;
  529. else {
  530. if (found)
  531. post_bits++;
  532. else
  533. pre_bits++;
  534. }
  535. }
  536. *pre = pre_bits;
  537. *post = post_bits;
  538. }
  539. #if 0
  540. static const int embeddedice_num_bits[] = {32, 6};
  541. uint32_t values[2];
  542. values[0] = value;
  543. values[1] = (1 << 5) | reg_addr;
  544. jtag_add_dr_out(tap, 2, embeddedice_num_bits, values, TAP_IDLE);
  545. #endif
  546. void embeddedice_write_dcc(struct jtag_tap *tap,
  547. int reg_addr,
  548. const uint8_t *buffer,
  549. int little,
  550. int count)
  551. {
  552. #if 0
  553. int i;
  554. for (i = 0; i < count; i++) {
  555. embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer,
  556. little));
  557. buffer += 4;
  558. }
  559. #else
  560. int pre_bits;
  561. int post_bits;
  562. jtag_pre_post_bits(tap, &pre_bits, &post_bits);
  563. if ((pre_bits > 32) || (post_bits + 6 > 32)) {
  564. int i;
  565. for (i = 0; i < count; i++) {
  566. embeddedice_write_reg_inner(tap, reg_addr,
  567. fast_target_buffer_get_u32(buffer, little));
  568. buffer += 4;
  569. }
  570. } else {
  571. int i;
  572. for (i = 0; i < count; i++) {
  573. /* Fewer pokes means we get to use the FIFO more efficiently */
  574. shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, pre_bits, 0);
  575. shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32,
  576. fast_target_buffer_get_u32(buffer, little));
  577. /* Danger! here we need to exit into the TAP_IDLE state to make
  578. * DCC pick up this value.
  579. */
  580. shiftValueInner(TAP_DRSHIFT, TAP_IDLE, 6 + post_bits,
  581. (reg_addr | (1 << 5)));
  582. buffer += 4;
  583. }
  584. }
  585. #endif
  586. }
  587. int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap *tap,
  588. uint32_t opcode,
  589. const uint32_t *data,
  590. size_t count)
  591. {
  592. /* bypass bits before and after */
  593. int pre_bits;
  594. int post_bits;
  595. jtag_pre_post_bits(tap, &pre_bits, &post_bits);
  596. post_bits += 2;
  597. if ((pre_bits > 32) || (post_bits > 32)) {
  598. int arm11_run_instr_data_to_core_noack_inner_default(struct jtag_tap *,
  599. uint32_t, const uint32_t *, size_t);
  600. return arm11_run_instr_data_to_core_noack_inner_default(tap, opcode, data, count);
  601. } else {
  602. static const int bits[] = {32, 2};
  603. uint32_t values[] = {0, 0};
  604. /* FIX!!!!!! the target_write_memory() API started this nasty problem
  605. * with unaligned uint32_t * pointers... */
  606. const uint8_t *t = (const uint8_t *)data;
  607. while (--count > 0) {
  608. #if 1
  609. /* Danger! This code doesn't update cmd_queue_cur_state, so
  610. * invoking jtag_add_pathmove() before jtag_add_dr_out() after
  611. * this loop would fail!
  612. */
  613. shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, pre_bits, 0);
  614. uint32_t value;
  615. value = *t++;
  616. value |= (*t++<<8);
  617. value |= (*t++<<16);
  618. value |= (*t++<<24);
  619. shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, value);
  620. /* minimum 2 bits */
  621. shiftValueInner(TAP_DRSHIFT, TAP_DRPAUSE, post_bits, 0);
  622. /* copy & paste from arm11_dbgtap.c */
  623. /* TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_DRSELECT,
  624. * TAP_DRCAPTURE, TAP_DRSHIFT */
  625. /* KLUDGE! we have to flush the fifo or the Nios CPU locks up.
  626. * This is probably a bug in the Avalon bus(cross clocking bridge?)
  627. * or in the jtag registers module.
  628. */
  629. waitIdle();
  630. ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 1);
  631. ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 1);
  632. ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 0);
  633. ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 0);
  634. ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 0);
  635. ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 1);
  636. ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 0);
  637. ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 0);
  638. /* we don't have to wait for the queue to empty here */
  639. ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, TAP_DRSHIFT);
  640. waitIdle();
  641. #else
  642. static const tap_state_t arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay[] = {
  643. TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE, TAP_IDLE, TAP_IDLE,
  644. TAP_DRSELECT, TAP_DRCAPTURE, TAP_DRSHIFT
  645. };
  646. values[0] = *t++;
  647. values[0] |= (*t++<<8);
  648. values[0] |= (*t++<<16);
  649. values[0] |= (*t++<<24);
  650. jtag_add_dr_out(tap,
  651. 2,
  652. bits,
  653. values,
  654. TAP_IDLE);
  655. jtag_add_pathmove(ARRAY_SIZE(arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay),
  656. arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay);
  657. #endif
  658. }
  659. values[0] = *t++;
  660. values[0] |= (*t++<<8);
  661. values[0] |= (*t++<<16);
  662. values[0] |= (*t++<<24);
  663. /* This will happen on the last iteration updating cmd_queue_cur_state
  664. * so we don't have to track it during the common code path
  665. */
  666. jtag_add_dr_out(tap,
  667. 2,
  668. bits,
  669. values,
  670. TAP_IDLE);
  671. return jtag_execute_queue();
  672. }
  673. }
  674. static const struct command_registration zy1000_commands[] = {
  675. {
  676. .name = "power",
  677. .handler = handle_power_command,
  678. .mode = COMMAND_ANY,
  679. .help = "Turn power switch to target on/off. "
  680. "With no arguments, prints status.",
  681. .usage = "('on'|'off)",
  682. },
  683. #if !BUILD_ZY1000_MASTER
  684. {
  685. .name = "zy1000_server",
  686. .mode = COMMAND_ANY,
  687. .jim_handler = jim_zy1000_server,
  688. .help = "Tcpip address for ZY1000 server.",
  689. .usage = "address",
  690. },
  691. #endif
  692. {
  693. .name = "powerstatus",
  694. .mode = COMMAND_ANY,
  695. .jim_handler = zylinjtag_Jim_Command_powerstatus,
  696. .help = "Returns power status of target",
  697. },
  698. COMMAND_REGISTRATION_DONE
  699. };
  700. #if !BUILD_ZY1000_MASTER
  701. static int tcp_ip = -1;
  702. /* Write large packets if we can */
  703. static size_t out_pos;
  704. static uint8_t out_buffer[16384];
  705. static size_t in_pos;
  706. static size_t in_write;
  707. static uint8_t in_buffer[16384];
  708. static bool flush_writes(void)
  709. {
  710. bool ok = (write(tcp_ip, out_buffer, out_pos) == (int)out_pos);
  711. out_pos = 0;
  712. return ok;
  713. }
  714. static bool writeLong(uint32_t l)
  715. {
  716. int i;
  717. for (i = 0; i < 4; i++) {
  718. uint8_t c = (l >> (i*8))&0xff;
  719. out_buffer[out_pos++] = c;
  720. if (out_pos >= sizeof(out_buffer)) {
  721. if (!flush_writes())
  722. return false;
  723. }
  724. }
  725. return true;
  726. }
  727. static bool readLong(uint32_t *out_data)
  728. {
  729. uint32_t data = 0;
  730. int i;
  731. for (i = 0; i < 4; i++) {
  732. uint8_t c;
  733. if (in_pos == in_write) {
  734. /* If we have some data that we can send, send them before
  735. * we wait for more data
  736. */
  737. if (out_pos > 0) {
  738. if (!flush_writes())
  739. return false;
  740. }
  741. /* read more */
  742. int t;
  743. t = read(tcp_ip, in_buffer, sizeof(in_buffer));
  744. if (t < 1)
  745. return false;
  746. in_write = (size_t) t;
  747. in_pos = 0;
  748. }
  749. c = in_buffer[in_pos++];
  750. data |= (c << (i*8));
  751. }
  752. *out_data = data;
  753. return true;
  754. }
  755. enum ZY1000_CMD {
  756. ZY1000_CMD_POKE = 0x0,
  757. ZY1000_CMD_PEEK = 0x8,
  758. ZY1000_CMD_SLEEP = 0x1,
  759. ZY1000_CMD_WAITIDLE = 2
  760. };
  761. #include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
  762. #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
  763. /* We initialize this late since we need to know the server address
  764. * first.
  765. */
  766. static void tcpip_open(void)
  767. {
  768. if (tcp_ip >= 0)
  769. return;
  770. struct sockaddr_in echoServAddr;/* Echo server address */
  771. /* Create a reliable, stream socket using TCP */
  772. tcp_ip = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  773. if (tcp_ip < 0) {
  774. fprintf(stderr, "Failed to connect to zy1000 server\n");
  775. exit(-1);
  776. }
  777. /* Construct the server address structure */
  778. memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
  779. echoServAddr.sin_family = AF_INET; /* Internet address family */
  780. echoServAddr.sin_addr.s_addr = inet_addr(tcp_server); /* Server IP address */
  781. echoServAddr.sin_port = htons(7777); /* Server port */
  782. /* Establish the connection to the echo server */
  783. if (connect(tcp_ip, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) {
  784. fprintf(stderr, "Failed to connect to zy1000 server\n");
  785. exit(-1);
  786. }
  787. int flag = 1;
  788. setsockopt(tcp_ip, /* socket affected */
  789. IPPROTO_TCP, /* set option at TCP level */
  790. TCP_NODELAY, /* name of option */
  791. (char *)&flag, /* the cast is historical cruft */
  792. sizeof(int)); /* length of option value */
  793. }
  794. /* send a poke */
  795. void zy1000_tcpout(uint32_t address, uint32_t data)
  796. {
  797. tcpip_open();
  798. if (!writeLong((ZY1000_CMD_POKE << 24) | address) || !writeLong(data)) {
  799. fprintf(stderr, "Could not write to zy1000 server\n");
  800. exit(-1);
  801. }
  802. }
  803. /* By sending the wait to the server, we avoid a readback
  804. * of status. Radically improves performance for this operation
  805. * with long ping times.
  806. */
  807. void waitIdle(void)
  808. {
  809. tcpip_open();
  810. if (!writeLong((ZY1000_CMD_WAITIDLE << 24))) {
  811. fprintf(stderr, "Could not write to zy1000 server\n");
  812. exit(-1);
  813. }
  814. }
  815. uint32_t zy1000_tcpin(uint32_t address)
  816. {
  817. tcpip_open();
  818. zy1000_flush_readqueue();
  819. uint32_t data;
  820. if (!writeLong((ZY1000_CMD_PEEK << 24) | address) || !readLong(&data)) {
  821. fprintf(stderr, "Could not read from zy1000 server\n");
  822. exit(-1);
  823. }
  824. return data;
  825. }
  826. int interface_jtag_add_sleep(uint32_t us)
  827. {
  828. tcpip_open();
  829. if (!writeLong((ZY1000_CMD_SLEEP << 24)) || !writeLong(us)) {
  830. fprintf(stderr, "Could not read from zy1000 server\n");
  831. exit(-1);
  832. }
  833. return ERROR_OK;
  834. }
  835. /* queue a readback */
  836. #define readqueue_size 16384
  837. static struct {
  838. uint8_t *dest;
  839. int bits;
  840. } readqueue[readqueue_size];
  841. static int readqueue_pos;
  842. /* flush the readqueue, this means reading any data that
  843. * we're expecting and store them into the final position
  844. */
  845. void zy1000_flush_readqueue(void)
  846. {
  847. if (readqueue_pos == 0) {
  848. /* simply debugging by allowing easy breakpoints when there
  849. * is something to do. */
  850. return;
  851. }
  852. int i;
  853. tcpip_open();
  854. for (i = 0; i < readqueue_pos; i++) {
  855. uint32_t value;
  856. if (!readLong(&value)) {
  857. fprintf(stderr, "Could not read from zy1000 server\n");
  858. exit(-1);
  859. }
  860. uint8_t *in_value = readqueue[i].dest;
  861. int k = readqueue[i].bits;
  862. /* we're shifting in data to MSB, shift data to be aligned for returning the value */
  863. value >>= 32-k;
  864. for (int l = 0; l < k; l += 8)
  865. in_value[l/8] = (value >> l)&0xff;
  866. }
  867. readqueue_pos = 0;
  868. }
  869. /* By queuing the callback's we avoid flushing the
  870. * read queue until jtag_execute_queue(). This can
  871. * reduce latency dramatically for cases where
  872. * callbacks are used extensively.
  873. */
  874. #define callbackqueue_size 128
  875. static struct callbackentry {
  876. jtag_callback_t callback;
  877. jtag_callback_data_t data0;
  878. jtag_callback_data_t data1;
  879. jtag_callback_data_t data2;
  880. jtag_callback_data_t data3;
  881. } callbackqueue[callbackqueue_size];
  882. static int callbackqueue_pos;
  883. void zy1000_jtag_add_callback4(jtag_callback_t callback,
  884. jtag_callback_data_t data0,
  885. jtag_callback_data_t data1,
  886. jtag_callback_data_t data2,
  887. jtag_callback_data_t data3)
  888. {
  889. if (callbackqueue_pos >= callbackqueue_size)
  890. zy1000_flush_callbackqueue();
  891. callbackqueue[callbackqueue_pos].callback = callback;
  892. callbackqueue[callbackqueue_pos].data0 = data0;
  893. callbackqueue[callbackqueue_pos].data1 = data1;
  894. callbackqueue[callbackqueue_pos].data2 = data2;
  895. callbackqueue[callbackqueue_pos].data3 = data3;
  896. callbackqueue_pos++;
  897. /* KLUDGE!
  898. * make callbacks synchronous for now as minidriver requires callback
  899. * to be synchronous.
  900. *
  901. * We can get away with making read and writes asynchronous so we
  902. * don't completely kill performance.
  903. */
  904. zy1000_flush_callbackqueue();
  905. }
  906. static int zy1000_jtag_convert_to_callback4(jtag_callback_data_t data0,
  907. jtag_callback_data_t data1,
  908. jtag_callback_data_t data2,
  909. jtag_callback_data_t data3)
  910. {
  911. ((jtag_callback1_t)data1)(data0);
  912. return ERROR_OK;
  913. }
  914. void zy1000_jtag_add_callback(jtag_callback1_t callback, jtag_callback_data_t data0)
  915. {
  916. zy1000_jtag_add_callback4(zy1000_jtag_convert_to_callback4,
  917. data0,
  918. (jtag_callback_data_t)callback,
  919. 0,
  920. 0);
  921. }
  922. void zy1000_flush_callbackqueue(void)
  923. {
  924. /* we have to flush the read queue so we have access to
  925. the data the callbacks will use
  926. */
  927. zy1000_flush_readqueue();
  928. int i;
  929. for (i = 0; i < callbackqueue_pos; i++) {
  930. struct callbackentry *entry = &callbackqueue[i];
  931. jtag_set_error(entry->callback(entry->data0, entry->data1, entry->data2,
  932. entry->data3));
  933. }
  934. callbackqueue_pos = 0;
  935. }
  936. static void writeShiftValue(uint8_t *data, int bits)
  937. {
  938. waitIdle();
  939. if (!writeLong((ZY1000_CMD_PEEK << 24) | (ZY1000_JTAG_BASE + 0xc))) {
  940. fprintf(stderr, "Could not read from zy1000 server\n");
  941. exit(-1);
  942. }
  943. if (readqueue_pos >= readqueue_size)
  944. zy1000_flush_readqueue();
  945. readqueue[readqueue_pos].dest = data;
  946. readqueue[readqueue_pos].bits = bits;
  947. readqueue_pos++;
  948. /* KLUDGE!!! minidriver requires readqueue to be synchronous */
  949. zy1000_flush_readqueue();
  950. }
  951. #else
  952. static void writeShiftValue(uint8_t *data, int bits)
  953. {
  954. uint32_t value;
  955. waitIdle();
  956. ZY1000_PEEK(ZY1000_JTAG_BASE + 0xc, value);
  957. VERBOSE(LOG_INFO("getShiftValue %08x", value));
  958. /* data in, LSB to MSB */
  959. /* we're shifting in data to MSB, shift data to be aligned for returning the value */
  960. value >>= 32 - bits;
  961. for (int l = 0; l < bits; l += 8)
  962. data[l/8] = (value >> l)&0xff;
  963. }
  964. #endif
  965. #if BUILD_ZY1000_MASTER
  966. #ifdef WATCHDOG_BASE
  967. /* If we connect to port 8888 we must send a char every 10s or the board resets itself */
  968. static void watchdog_server(cyg_addrword_t data)
  969. {
  970. int so_reuseaddr_option = 1;
  971. int fd = socket(AF_INET, SOCK_STREAM, 0);
  972. if (fd == -1) {
  973. LOG_ERROR("error creating socket: %s", strerror(errno));
  974. exit(-1);
  975. }
  976. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &so_reuseaddr_option,
  977. sizeof(int));
  978. struct sockaddr_in sin;
  979. unsigned int address_size;
  980. address_size = sizeof(sin);
  981. memset(&sin, 0, sizeof(sin));
  982. sin.sin_family = AF_INET;
  983. sin.sin_addr.s_addr = INADDR_ANY;
  984. sin.sin_port = htons(8888);
  985. if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
  986. LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
  987. exit(-1);
  988. }
  989. if (listen(fd, 1) == -1) {
  990. LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
  991. exit(-1);
  992. }
  993. for (;; ) {
  994. int watchdog_ip = accept(fd, (struct sockaddr *) &sin, &address_size);
  995. /* Start watchdog, must be reset every 10 seconds. */
  996. HAL_WRITE_UINT32(WATCHDOG_BASE + 4, 4);
  997. if (watchdog_ip < 0) {
  998. LOG_ERROR("couldn't open watchdog socket: %s", strerror(errno));
  999. exit(-1);
  1000. }
  1001. int flag = 1;
  1002. setsockopt(watchdog_ip, /* socket affected */
  1003. IPPROTO_TCP, /* set option at TCP level */
  1004. TCP_NODELAY, /* name of option */
  1005. (char *)&flag, /* the cast is historical cruft */
  1006. sizeof(int)); /* length of option value */
  1007. char buf;
  1008. for (;; ) {
  1009. if (read(watchdog_ip, &buf, 1) == 1) {
  1010. /* Reset timer */
  1011. HAL_WRITE_UINT32(WATCHDOG_BASE + 8, 0x1234);
  1012. /* Echo so we can telnet in and see that resetting works */
  1013. write(watchdog_ip, &buf, 1);
  1014. } else {
  1015. /* Stop tickling the watchdog, the CPU will reset in < 10 seconds
  1016. * now.
  1017. */
  1018. return;
  1019. }
  1020. }
  1021. /* Never reached */
  1022. }
  1023. }
  1024. #endif
  1025. #endif
  1026. #if BUILD_ZY1000_MASTER
  1027. int interface_jtag_add_sleep(uint32_t us)
  1028. {
  1029. jtag_sleep(us);
  1030. return ERROR_OK;
  1031. }
  1032. #endif
  1033. #if BUILD_ZY1000_MASTER
  1034. volatile void *zy1000_jtag_master;
  1035. #include <sys/mman.h>
  1036. #endif
  1037. int zy1000_init(void)
  1038. {
  1039. #if BUILD_ZY1000_MASTER
  1040. int fd = open("/dev/mem", O_RDWR | O_SYNC);
  1041. if (fd == -1) {
  1042. LOG_ERROR("No access to /dev/mem");
  1043. return ERROR_FAIL;
  1044. }
  1045. #ifndef REGISTERS_BASE
  1046. #define REGISTERS_BASE 0x9002000
  1047. #define REGISTERS_SPAN 128
  1048. #endif
  1049. zy1000_jtag_master = mmap(0,
  1050. REGISTERS_SPAN,
  1051. PROT_READ | PROT_WRITE,
  1052. MAP_SHARED,
  1053. fd,
  1054. REGISTERS_BASE);
  1055. if (zy1000_jtag_master == (void *) -1) {
  1056. close(fd);
  1057. LOG_ERROR("No access to /dev/mem");
  1058. return ERROR_FAIL;
  1059. }
  1060. #endif
  1061. ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x30); /* Turn on LED1 & LED2 */
  1062. setPower(true); /* on by default */
  1063. /* deassert resets. Important to avoid infinite loop waiting for SRST to deassert */
  1064. zy1000_reset(0, 0);
  1065. return ERROR_OK;
  1066. }
  1067. struct jtag_interface zy1000_interface = {
  1068. .name = "ZY1000",
  1069. .supported = DEBUG_CAP_TMS_SEQ,
  1070. .execute_queue = NULL,
  1071. .speed = zy1000_speed,
  1072. .commands = zy1000_commands,
  1073. .init = zy1000_init,
  1074. .quit = zy1000_quit,
  1075. .khz = zy1000_khz,
  1076. .speed_div = zy1000_speed_div,
  1077. .power_dropout = zy1000_power_dropout,
  1078. .srst_asserted = zy1000_srst_asserted,
  1079. };