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.
 
 
 
 
 
 

763 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 "log.h"
  23. #include "jtag.h"
  24. #include "bitbang.h"
  25. #include "../target/embeddedice.h"
  26. #include <cyg/hal/hal_io.h> // low level i/o
  27. #include <cyg/hal/hal_diag.h>
  28. #include <stdlib.h>
  29. extern int jtag_error;
  30. /* low level command set
  31. */
  32. int zy1000_read(void);
  33. static void zy1000_write(int tck, int tms, int tdi);
  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()
  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()
  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 = bitbang_execute_queue,
  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. bitbang_interface_t zy1000_bitbang =
  109. {
  110. .read = zy1000_read,
  111. .write = zy1000_write,
  112. .reset = zy1000_reset
  113. };
  114. static void zy1000_write(int tck, int tms, int tdi)
  115. {
  116. }
  117. int zy1000_read(void)
  118. {
  119. return -1;
  120. }
  121. extern bool readSRST();
  122. void zy1000_reset(int trst, int srst)
  123. {
  124. LOG_DEBUG("zy1000 trst=%d, srst=%d", trst, srst);
  125. if(!srst)
  126. {
  127. ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x00000001);
  128. }
  129. else
  130. {
  131. /* Danger!!! if clk!=0 when in
  132. * idle in TAP_IDLE, reset halt on str912 will fail.
  133. */
  134. ZY1000_POKE(ZY1000_JTAG_BASE+0x10, 0x00000001);
  135. }
  136. if(!trst)
  137. {
  138. ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x00000002);
  139. }
  140. else
  141. {
  142. /* assert reset */
  143. ZY1000_POKE(ZY1000_JTAG_BASE+0x10, 0x00000002);
  144. }
  145. if (trst||(srst&&(jtag_reset_config & RESET_SRST_PULLS_TRST)))
  146. {
  147. waitIdle();
  148. /* we're now in the RESET state until trst is deasserted */
  149. ZY1000_POKE(ZY1000_JTAG_BASE+0x20, TAP_RESET);
  150. } else
  151. {
  152. /* We'll get RCLK failure when we assert TRST, so clear any false positives here */
  153. ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x400);
  154. }
  155. /* wait for srst to float back up */
  156. if (!srst)
  157. {
  158. int i;
  159. for (i=0; i<1000; i++)
  160. {
  161. // We don't want to sense our own reset, so we clear here.
  162. // There is of course a timing hole where we could loose
  163. // a "real" reset.
  164. if (!readSRST())
  165. break;
  166. /* wait 1ms */
  167. alive_sleep(1);
  168. }
  169. if (i==1000)
  170. {
  171. LOG_USER("SRST didn't deassert after %dms", i);
  172. } else if (i>1)
  173. {
  174. LOG_USER("SRST took %dms to deassert", i);
  175. }
  176. }
  177. }
  178. int zy1000_speed(int speed)
  179. {
  180. if(speed == 0)
  181. {
  182. /*0 means RCLK*/
  183. speed = 0;
  184. ZY1000_POKE(ZY1000_JTAG_BASE+0x10, 0x100);
  185. LOG_DEBUG("jtag_speed using RCLK");
  186. }
  187. else
  188. {
  189. if(speed > 8190 || speed < 2)
  190. {
  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;
  193. }
  194. LOG_USER("jtag_speed %d => JTAG clk=%f", speed, 64.0/(float)speed);
  195. ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x100);
  196. ZY1000_POKE(ZY1000_JTAG_BASE+0x1c, speed&~1);
  197. }
  198. return ERROR_OK;
  199. }
  200. int zy1000_register_commands(struct command_context_s *cmd_ctx)
  201. {
  202. return ERROR_OK;
  203. }
  204. int zy1000_init(void)
  205. {
  206. ZY1000_POKE(ZY1000_JTAG_BASE+0x10, 0x30); // Turn on LED1 & LED2
  207. /* deassert resets. Important to avoid infinite loop waiting for SRST to deassert */
  208. zy1000_reset(0, 0);
  209. zy1000_speed(jtag_speed);
  210. bitbang_interface = &zy1000_bitbang;
  211. return ERROR_OK;
  212. }
  213. int zy1000_quit(void)
  214. {
  215. return ERROR_OK;
  216. }
  217. /* loads a file and returns a pointer to it in memory. The file contains
  218. * a 0 byte(sentinel) after len bytes - the length of the file. */
  219. int loadFile(const char *fileName, void **data, int *len)
  220. {
  221. FILE * pFile;
  222. pFile = fopen (fileName,"rb");
  223. if (pFile==NULL)
  224. {
  225. LOG_ERROR("Can't open %s\n", fileName);
  226. return ERROR_JTAG_DEVICE_ERROR;
  227. }
  228. if (fseek (pFile, 0, SEEK_END)!=0)
  229. {
  230. LOG_ERROR("Can't open %s\n", fileName);
  231. fclose(pFile);
  232. return ERROR_JTAG_DEVICE_ERROR;
  233. }
  234. *len=ftell (pFile);
  235. if (*len==-1)
  236. {
  237. LOG_ERROR("Can't open %s\n", fileName);
  238. fclose(pFile);
  239. return ERROR_JTAG_DEVICE_ERROR;
  240. }
  241. if (fseek (pFile, 0, SEEK_SET)!=0)
  242. {
  243. LOG_ERROR("Can't open %s\n", fileName);
  244. fclose(pFile);
  245. return ERROR_JTAG_DEVICE_ERROR;
  246. }
  247. *data=malloc(*len+1);
  248. if (*data==NULL)
  249. {
  250. LOG_ERROR("Can't open %s\n", fileName);
  251. fclose(pFile);
  252. return ERROR_JTAG_DEVICE_ERROR;
  253. }
  254. if (fread(*data, 1, *len, pFile)!=*len)
  255. {
  256. fclose(pFile);
  257. free(*data);
  258. LOG_ERROR("Can't open %s\n", fileName);
  259. return ERROR_JTAG_DEVICE_ERROR;
  260. }
  261. fclose (pFile);
  262. *(((char *)(*data))+*len)=0; /* sentinel */
  263. return ERROR_OK;
  264. }
  265. int interface_jtag_execute_queue(void)
  266. {
  267. cyg_uint32 empty;
  268. waitIdle();
  269. ZY1000_PEEK(ZY1000_JTAG_BASE+0x10, empty);
  270. /* clear JTAG error register */
  271. ZY1000_POKE(ZY1000_JTAG_BASE+0x14, 0x400);
  272. if ((empty&0x400)!=0)
  273. {
  274. LOG_WARNING("RCLK timeout");
  275. /* the error is informative only as we don't want to break the firmware if there
  276. * is a false positive.
  277. */
  278. // return ERROR_FAIL;
  279. }
  280. return ERROR_OK;
  281. }
  282. static cyg_uint32 getShiftValue()
  283. {
  284. cyg_uint32 value;
  285. waitIdle();
  286. ZY1000_PEEK(ZY1000_JTAG_BASE+0xc, value);
  287. VERBOSE(LOG_INFO("getShiftValue %08x", value));
  288. return value;
  289. }
  290. #if 0
  291. static cyg_uint32 getShiftValueFlip()
  292. {
  293. cyg_uint32 value;
  294. waitIdle();
  295. ZY1000_PEEK(ZY1000_JTAG_BASE+0x18, value);
  296. VERBOSE(LOG_INFO("getShiftValue %08x (flipped)", value));
  297. return value;
  298. }
  299. #endif
  300. #if 0
  301. static void shiftValueInnerFlip(const enum tap_state state, const enum tap_state endState, int repeat, cyg_uint32 value)
  302. {
  303. VERBOSE(LOG_INFO("shiftValueInner %s %s %d %08x (flipped)", tap_state_strings[state], tap_state_strings[endState], repeat, value));
  304. cyg_uint32 a,b;
  305. a=state;
  306. b=endState;
  307. ZY1000_POKE(ZY1000_JTAG_BASE+0xc, value);
  308. ZY1000_POKE(ZY1000_JTAG_BASE+0x8, (1<<15)|(repeat<<8)|(a<<4)|b);
  309. VERBOSE(getShiftValueFlip());
  310. }
  311. #endif
  312. extern int jtag_check_value(u8 *captured, void *priv);
  313. static void gotoEndState()
  314. {
  315. setCurrentState(cmd_queue_end_state);
  316. }
  317. static __inline void scanFields(int num_fields, scan_field_t *fields, enum tap_state shiftState, int pause)
  318. {
  319. int i;
  320. int j;
  321. int k;
  322. for (i = 0; i < num_fields; i++)
  323. {
  324. cyg_uint32 value;
  325. static u8 *in_buff=NULL; /* pointer to buffer for scanned data */
  326. static int in_buff_size=0;
  327. u8 *inBuffer=NULL;
  328. // figure out where to store the input data
  329. int num_bits=fields[i].num_bits;
  330. if (fields[i].in_value!=NULL)
  331. {
  332. inBuffer=fields[i].in_value;
  333. } else if (fields[i].in_handler!=NULL)
  334. {
  335. if (in_buff_size*8<num_bits)
  336. {
  337. // we need more space
  338. if (in_buff!=NULL)
  339. free(in_buff);
  340. in_buff=NULL;
  341. in_buff_size=(num_bits+7)/8;
  342. in_buff=malloc(in_buff_size);
  343. if (in_buff==NULL)
  344. {
  345. LOG_ERROR("Out of memory");
  346. jtag_error=ERROR_JTAG_QUEUE_FAILED;
  347. return;
  348. }
  349. }
  350. inBuffer=in_buff;
  351. }
  352. // here we shuffle N bits out/in
  353. j=0;
  354. while (j<num_bits)
  355. {
  356. enum tap_state pause_state;
  357. int l;
  358. k=num_bits-j;
  359. pause_state=(shiftState==TAP_DRSHIFT)?TAP_DRSHIFT:TAP_IRSHIFT;
  360. if (k>32)
  361. {
  362. k=32;
  363. /* we have more to shift out */
  364. } else if (pause&&(i == num_fields-1))
  365. {
  366. /* this was the last to shift out this time */
  367. pause_state=(shiftState==TAP_DRSHIFT)?TAP_DRPAUSE:TAP_IRPAUSE;
  368. }
  369. // we have (num_bits+7)/8 bytes of bits to toggle out.
  370. // bits are pushed out LSB to MSB
  371. value=0;
  372. if (fields[i].out_value!=NULL)
  373. {
  374. for (l=0; l<k; l+=8)
  375. {
  376. value|=fields[i].out_value[(j+l)/8]<<l;
  377. }
  378. }
  379. /* mask away unused bits for easier debugging */
  380. value&=~(((u32)0xffffffff)<<k);
  381. shiftValueInner(shiftState, pause_state, k, value);
  382. if (inBuffer!=NULL)
  383. {
  384. // data in, LSB to MSB
  385. value=getShiftValue();
  386. // we're shifting in data to MSB, shift data to be aligned for returning the value
  387. value >>= 32-k;
  388. for (l=0; l<k; l+=8)
  389. {
  390. inBuffer[(j+l)/8]=(value>>l)&0xff;
  391. }
  392. }
  393. j+=k;
  394. }
  395. if (fields[i].in_handler!=NULL)
  396. {
  397. // invoke callback
  398. int r=fields[i].in_handler(inBuffer, fields[i].in_handler_priv, fields+i);
  399. if (r!=ERROR_OK)
  400. {
  401. /* this will cause jtag_execute_queue() to return an error */
  402. jtag_error=r;
  403. }
  404. }
  405. }
  406. }
  407. int interface_jtag_add_end_state(enum tap_state state)
  408. {
  409. return ERROR_OK;
  410. }
  411. int interface_jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
  412. {
  413. int j;
  414. int scan_size = 0;
  415. jtag_tap_t *tap, *nextTap;
  416. for(tap = jtag_NextEnabledTap(NULL); tap!= NULL; tap=nextTap)
  417. {
  418. nextTap=jtag_NextEnabledTap(tap);
  419. int pause=(nextTap==NULL);
  420. int found = 0;
  421. scan_size = tap->ir_length;
  422. /* search the list */
  423. for (j=0; j < num_fields; j++)
  424. {
  425. if (tap == fields[j].tap)
  426. {
  427. found = 1;
  428. if ((jtag_verify_capture_ir)&&(fields[j].in_handler==NULL))
  429. {
  430. jtag_set_check_value(fields+j, tap->expected, tap->expected_mask, NULL);
  431. } else if (jtag_verify_capture_ir)
  432. {
  433. fields[j].in_check_value = tap->expected;
  434. fields[j].in_check_mask = tap->expected_mask;
  435. }
  436. scanFields(1, fields+j, TAP_IRSHIFT, pause);
  437. /* update device information */
  438. buf_cpy(fields[j].out_value, tap->cur_instr, scan_size);
  439. tap->bypass = 0;
  440. break;
  441. }
  442. }
  443. if (!found)
  444. {
  445. /* if a device isn't listed, set it to BYPASS */
  446. u8 ones[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
  447. scan_field_t tmp;
  448. memset(&tmp, 0, sizeof(tmp));
  449. tmp.out_value = ones;
  450. tmp.num_bits = scan_size;
  451. scanFields(1, &tmp, TAP_IRSHIFT, pause);
  452. /* update device information */
  453. buf_cpy(tmp.out_value, tap->cur_instr, scan_size);
  454. tap->bypass = 1;
  455. }
  456. }
  457. gotoEndState();
  458. return ERROR_OK;
  459. }
  460. int interface_jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
  461. {
  462. scanFields(num_fields, fields, TAP_IRSHIFT, 1);
  463. gotoEndState();
  464. return ERROR_OK;
  465. }
  466. /*extern jtag_command_t **jtag_get_last_command_p(void);*/
  467. int interface_jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
  468. {
  469. int j;
  470. jtag_tap_t *tap, *nextTap;
  471. for(tap = jtag_NextEnabledTap(NULL); tap!= NULL; tap=nextTap)
  472. {
  473. nextTap=jtag_NextEnabledTap(tap);
  474. int found=0;
  475. int pause=(nextTap==NULL);
  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, pause);
  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.out_mask = NULL;
  491. tmp.in_value = NULL;
  492. tmp.in_check_value = NULL;
  493. tmp.in_check_mask = NULL;
  494. tmp.in_handler = NULL;
  495. tmp.in_handler_priv = NULL;
  496. scanFields(1, &tmp, TAP_DRSHIFT, pause);
  497. }
  498. else
  499. {
  500. }
  501. }
  502. gotoEndState();
  503. return ERROR_OK;
  504. }
  505. int interface_jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
  506. {
  507. scanFields(num_fields, fields, TAP_DRSHIFT, 1);
  508. gotoEndState();
  509. return ERROR_OK;
  510. }
  511. int interface_jtag_add_tlr()
  512. {
  513. setCurrentState(TAP_RESET);
  514. return ERROR_OK;
  515. }
  516. extern int jtag_nsrst_delay;
  517. extern int jtag_ntrst_delay;
  518. int interface_jtag_add_reset(int req_trst, int req_srst)
  519. {
  520. zy1000_reset(req_trst, req_srst);
  521. return ERROR_OK;
  522. }
  523. int interface_jtag_add_runtest(int num_cycles, enum tap_state state)
  524. {
  525. /* num_cycles can be 0 */
  526. setCurrentState(TAP_IDLE);
  527. /* execute num_cycles, 32 at the time. */
  528. int i;
  529. for (i=0; i<num_cycles; i+=32)
  530. {
  531. int num;
  532. num=32;
  533. if (num_cycles-i<num)
  534. {
  535. num=num_cycles-i;
  536. }
  537. shiftValueInner(TAP_IDLE, TAP_IDLE, num, 0);
  538. }
  539. #if !TEST_MANUAL()
  540. /* finish in end_state */
  541. setCurrentState(state);
  542. #else
  543. enum tap_state t=TAP_IDLE;
  544. /* test manual drive code on any target */
  545. int tms;
  546. u8 tms_scan = TAP_MOVE(t, state);
  547. for (i = 0; i < 7; i++)
  548. {
  549. tms = (tms_scan >> i) & 1;
  550. waitIdle();
  551. ZY1000_POKE(ZY1000_JTAG_BASE+0x28, tms);
  552. }
  553. waitIdle();
  554. ZY1000_POKE(ZY1000_JTAG_BASE+0x20, state);
  555. #endif
  556. return ERROR_OK;
  557. }
  558. int interface_jtag_add_sleep(u32 us)
  559. {
  560. jtag_sleep(us);
  561. return ERROR_OK;
  562. }
  563. int interface_jtag_add_pathmove(int num_states, enum tap_state *path)
  564. {
  565. int state_count;
  566. int tms = 0;
  567. /*wait for the fifo to be empty*/
  568. waitIdle();
  569. state_count = 0;
  570. enum tap_state cur_state=cmd_queue_cur_state;
  571. while (num_states)
  572. {
  573. if (tap_transitions[cur_state].low == path[state_count])
  574. {
  575. tms = 0;
  576. }
  577. else if (tap_transitions[cur_state].high == path[state_count])
  578. {
  579. tms = 1;
  580. }
  581. else
  582. {
  583. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[state_count]]);
  584. exit(-1);
  585. }
  586. waitIdle();
  587. ZY1000_POKE(ZY1000_JTAG_BASE+0x28, tms);
  588. cur_state = path[state_count];
  589. state_count++;
  590. num_states--;
  591. }
  592. waitIdle();
  593. ZY1000_POKE(ZY1000_JTAG_BASE+0x20, cur_state);
  594. return ERROR_OK;
  595. }
  596. void embeddedice_write_dcc(jtag_tap_t *tap, int reg_addr, u8 *buffer, int little, int count)
  597. {
  598. // static int const reg_addr=0x5;
  599. enum tap_state end_state=cmd_queue_end_state;
  600. if (jtag_NextEnabledTap(jtag_NextEnabledTap(NULL))==NULL)
  601. {
  602. /* better performance via code duplication */
  603. if (little)
  604. {
  605. int i;
  606. for (i = 0; i < count; i++)
  607. {
  608. shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 1));
  609. shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr|(1<<5));
  610. buffer+=4;
  611. }
  612. } else
  613. {
  614. int i;
  615. for (i = 0; i < count; i++)
  616. {
  617. shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 0));
  618. shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr|(1<<5));
  619. buffer+=4;
  620. }
  621. }
  622. }
  623. else
  624. {
  625. int i;
  626. for (i = 0; i < count; i++)
  627. {
  628. embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, little));
  629. buffer += 4;
  630. }
  631. }
  632. }