altera-jtag.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * altera-jtag.c
  3. *
  4. * altera FPGA driver
  5. *
  6. * Copyright (C) Altera Corporation 1998-2001
  7. * Copyright (C) 2010 NetUP Inc.
  8. * Copyright (C) 2010 Igor M. Liplianin <liplianin@netup.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. *
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/delay.h>
  26. #include <linux/firmware.h>
  27. #include <linux/slab.h>
  28. #include <misc/altera.h>
  29. #include "altera-exprt.h"
  30. #include "altera-jtag.h"
  31. #define alt_jtag_io(a, b, c)\
  32. astate->config->jtag_io(astate->config->dev, a, b, c);
  33. #define alt_malloc(a) kzalloc(a, GFP_KERNEL);
  34. /*
  35. * This structure shows, for each JTAG state, which state is reached after
  36. * a single TCK clock cycle with TMS high or TMS low, respectively. This
  37. * describes all possible state transitions in the JTAG state machine.
  38. */
  39. struct altera_jtag_machine {
  40. enum altera_jtag_state tms_high;
  41. enum altera_jtag_state tms_low;
  42. };
  43. static const struct altera_jtag_machine altera_transitions[] = {
  44. /* RESET */ { RESET, IDLE },
  45. /* IDLE */ { DRSELECT, IDLE },
  46. /* DRSELECT */ { IRSELECT, DRCAPTURE },
  47. /* DRCAPTURE */ { DREXIT1, DRSHIFT },
  48. /* DRSHIFT */ { DREXIT1, DRSHIFT },
  49. /* DREXIT1 */ { DRUPDATE, DRPAUSE },
  50. /* DRPAUSE */ { DREXIT2, DRPAUSE },
  51. /* DREXIT2 */ { DRUPDATE, DRSHIFT },
  52. /* DRUPDATE */ { DRSELECT, IDLE },
  53. /* IRSELECT */ { RESET, IRCAPTURE },
  54. /* IRCAPTURE */ { IREXIT1, IRSHIFT },
  55. /* IRSHIFT */ { IREXIT1, IRSHIFT },
  56. /* IREXIT1 */ { IRUPDATE, IRPAUSE },
  57. /* IRPAUSE */ { IREXIT2, IRPAUSE },
  58. /* IREXIT2 */ { IRUPDATE, IRSHIFT },
  59. /* IRUPDATE */ { DRSELECT, IDLE }
  60. };
  61. /*
  62. * This table contains the TMS value to be used to take the NEXT STEP on
  63. * the path to the desired state. The array index is the current state,
  64. * and the bit position is the desired endstate. To find out which state
  65. * is used as the intermediate state, look up the TMS value in the
  66. * altera_transitions[] table.
  67. */
  68. static const u16 altera_jtag_path_map[16] = {
  69. /* RST RTI SDRS CDR SDR E1DR PDR E2DR */
  70. 0x0001, 0xFFFD, 0xFE01, 0xFFE7, 0xFFEF, 0xFF0F, 0xFFBF, 0xFFFF,
  71. /* UDR SIRS CIR SIR E1IR PIR E2IR UIR */
  72. 0xFEFD, 0x0001, 0xF3FF, 0xF7FF, 0x87FF, 0xDFFF, 0xFFFF, 0x7FFD
  73. };
  74. /* Flag bits for alt_jtag_io() function */
  75. #define TMS_HIGH 1
  76. #define TMS_LOW 0
  77. #define TDI_HIGH 1
  78. #define TDI_LOW 0
  79. #define READ_TDO 1
  80. #define IGNORE_TDO 0
  81. int altera_jinit(struct altera_state *astate)
  82. {
  83. struct altera_jtag *js = &astate->js;
  84. /* initial JTAG state is unknown */
  85. js->jtag_state = ILLEGAL_JTAG_STATE;
  86. /* initialize to default state */
  87. js->drstop_state = IDLE;
  88. js->irstop_state = IDLE;
  89. js->dr_pre = 0;
  90. js->dr_post = 0;
  91. js->ir_pre = 0;
  92. js->ir_post = 0;
  93. js->dr_length = 0;
  94. js->ir_length = 0;
  95. js->dr_pre_data = NULL;
  96. js->dr_post_data = NULL;
  97. js->ir_pre_data = NULL;
  98. js->ir_post_data = NULL;
  99. js->dr_buffer = NULL;
  100. js->ir_buffer = NULL;
  101. return 0;
  102. }
  103. int altera_set_drstop(struct altera_jtag *js, enum altera_jtag_state state)
  104. {
  105. js->drstop_state = state;
  106. return 0;
  107. }
  108. int altera_set_irstop(struct altera_jtag *js, enum altera_jtag_state state)
  109. {
  110. js->irstop_state = state;
  111. return 0;
  112. }
  113. int altera_set_dr_pre(struct altera_jtag *js,
  114. u32 count, u32 start_index,
  115. u8 *preamble_data)
  116. {
  117. int status = 0;
  118. u32 i;
  119. u32 j;
  120. if (count > js->dr_pre) {
  121. kfree(js->dr_pre_data);
  122. js->dr_pre_data = (u8 *)alt_malloc((count + 7) >> 3);
  123. if (js->dr_pre_data == NULL)
  124. status = -ENOMEM;
  125. else
  126. js->dr_pre = count;
  127. } else
  128. js->dr_pre = count;
  129. if (status == 0) {
  130. for (i = 0; i < count; ++i) {
  131. j = i + start_index;
  132. if (preamble_data == NULL)
  133. js->dr_pre_data[i >> 3] |= (1 << (i & 7));
  134. else {
  135. if (preamble_data[j >> 3] & (1 << (j & 7)))
  136. js->dr_pre_data[i >> 3] |=
  137. (1 << (i & 7));
  138. else
  139. js->dr_pre_data[i >> 3] &=
  140. ~(u32)(1 << (i & 7));
  141. }
  142. }
  143. }
  144. return status;
  145. }
  146. int altera_set_ir_pre(struct altera_jtag *js, u32 count, u32 start_index,
  147. u8 *preamble_data)
  148. {
  149. int status = 0;
  150. u32 i;
  151. u32 j;
  152. if (count > js->ir_pre) {
  153. kfree(js->ir_pre_data);
  154. js->ir_pre_data = (u8 *)alt_malloc((count + 7) >> 3);
  155. if (js->ir_pre_data == NULL)
  156. status = -ENOMEM;
  157. else
  158. js->ir_pre = count;
  159. } else
  160. js->ir_pre = count;
  161. if (status == 0) {
  162. for (i = 0; i < count; ++i) {
  163. j = i + start_index;
  164. if (preamble_data == NULL)
  165. js->ir_pre_data[i >> 3] |= (1 << (i & 7));
  166. else {
  167. if (preamble_data[j >> 3] & (1 << (j & 7)))
  168. js->ir_pre_data[i >> 3] |=
  169. (1 << (i & 7));
  170. else
  171. js->ir_pre_data[i >> 3] &=
  172. ~(u32)(1 << (i & 7));
  173. }
  174. }
  175. }
  176. return status;
  177. }
  178. int altera_set_dr_post(struct altera_jtag *js, u32 count, u32 start_index,
  179. u8 *postamble_data)
  180. {
  181. int status = 0;
  182. u32 i;
  183. u32 j;
  184. if (count > js->dr_post) {
  185. kfree(js->dr_post_data);
  186. js->dr_post_data = (u8 *)alt_malloc((count + 7) >> 3);
  187. if (js->dr_post_data == NULL)
  188. status = -ENOMEM;
  189. else
  190. js->dr_post = count;
  191. } else
  192. js->dr_post = count;
  193. if (status == 0) {
  194. for (i = 0; i < count; ++i) {
  195. j = i + start_index;
  196. if (postamble_data == NULL)
  197. js->dr_post_data[i >> 3] |= (1 << (i & 7));
  198. else {
  199. if (postamble_data[j >> 3] & (1 << (j & 7)))
  200. js->dr_post_data[i >> 3] |=
  201. (1 << (i & 7));
  202. else
  203. js->dr_post_data[i >> 3] &=
  204. ~(u32)(1 << (i & 7));
  205. }
  206. }
  207. }
  208. return status;
  209. }
  210. int altera_set_ir_post(struct altera_jtag *js, u32 count, u32 start_index,
  211. u8 *postamble_data)
  212. {
  213. int status = 0;
  214. u32 i;
  215. u32 j;
  216. if (count > js->ir_post) {
  217. kfree(js->ir_post_data);
  218. js->ir_post_data = (u8 *)alt_malloc((count + 7) >> 3);
  219. if (js->ir_post_data == NULL)
  220. status = -ENOMEM;
  221. else
  222. js->ir_post = count;
  223. } else
  224. js->ir_post = count;
  225. if (status != 0)
  226. return status;
  227. for (i = 0; i < count; ++i) {
  228. j = i + start_index;
  229. if (postamble_data == NULL)
  230. js->ir_post_data[i >> 3] |= (1 << (i & 7));
  231. else {
  232. if (postamble_data[j >> 3] & (1 << (j & 7)))
  233. js->ir_post_data[i >> 3] |= (1 << (i & 7));
  234. else
  235. js->ir_post_data[i >> 3] &=
  236. ~(u32)(1 << (i & 7));
  237. }
  238. }
  239. return status;
  240. }
  241. static void altera_jreset_idle(struct altera_state *astate)
  242. {
  243. struct altera_jtag *js = &astate->js;
  244. int i;
  245. /* Go to Test Logic Reset (no matter what the starting state may be) */
  246. for (i = 0; i < 5; ++i)
  247. alt_jtag_io(TMS_HIGH, TDI_LOW, IGNORE_TDO);
  248. /* Now step to Run Test / Idle */
  249. alt_jtag_io(TMS_LOW, TDI_LOW, IGNORE_TDO);
  250. js->jtag_state = IDLE;
  251. }
  252. int altera_goto_jstate(struct altera_state *astate,
  253. enum altera_jtag_state state)
  254. {
  255. struct altera_jtag *js = &astate->js;
  256. int tms;
  257. int count = 0;
  258. int status = 0;
  259. if (js->jtag_state == ILLEGAL_JTAG_STATE)
  260. /* initialize JTAG chain to known state */
  261. altera_jreset_idle(astate);
  262. if (js->jtag_state == state) {
  263. /*
  264. * We are already in the desired state.
  265. * If it is a stable state, loop here.
  266. * Otherwise do nothing (no clock cycles).
  267. */
  268. if ((state == IDLE) || (state == DRSHIFT) ||
  269. (state == DRPAUSE) || (state == IRSHIFT) ||
  270. (state == IRPAUSE)) {
  271. alt_jtag_io(TMS_LOW, TDI_LOW, IGNORE_TDO);
  272. } else if (state == RESET)
  273. alt_jtag_io(TMS_HIGH, TDI_LOW, IGNORE_TDO);
  274. } else {
  275. while ((js->jtag_state != state) && (count < 9)) {
  276. /* Get TMS value to take a step toward desired state */
  277. tms = (altera_jtag_path_map[js->jtag_state] &
  278. (1 << state))
  279. ? TMS_HIGH : TMS_LOW;
  280. /* Take a step */
  281. alt_jtag_io(tms, TDI_LOW, IGNORE_TDO);
  282. if (tms)
  283. js->jtag_state =
  284. altera_transitions[js->jtag_state].tms_high;
  285. else
  286. js->jtag_state =
  287. altera_transitions[js->jtag_state].tms_low;
  288. ++count;
  289. }
  290. }
  291. if (js->jtag_state != state)
  292. status = -EREMOTEIO;
  293. return status;
  294. }
  295. int altera_wait_cycles(struct altera_state *astate,
  296. s32 cycles,
  297. enum altera_jtag_state wait_state)
  298. {
  299. struct altera_jtag *js = &astate->js;
  300. int tms;
  301. s32 count;
  302. int status = 0;
  303. if (js->jtag_state != wait_state)
  304. status = altera_goto_jstate(astate, wait_state);
  305. if (status == 0) {
  306. /*
  307. * Set TMS high to loop in RESET state
  308. * Set TMS low to loop in any other stable state
  309. */
  310. tms = (wait_state == RESET) ? TMS_HIGH : TMS_LOW;
  311. for (count = 0L; count < cycles; count++)
  312. alt_jtag_io(tms, TDI_LOW, IGNORE_TDO);
  313. }
  314. return status;
  315. }
  316. int altera_wait_msecs(struct altera_state *astate,
  317. s32 microseconds, enum altera_jtag_state wait_state)
  318. /*
  319. * Causes JTAG hardware to sit in the specified stable
  320. * state for the specified duration of real time. If
  321. * no JTAG operations have been performed yet, then only
  322. * a delay is performed. This permits the WAIT USECS
  323. * statement to be used in VECTOR programs without causing
  324. * any JTAG operations.
  325. * Returns 0 for success, else appropriate error code.
  326. */
  327. {
  328. struct altera_jtag *js = &astate->js;
  329. int status = 0;
  330. if ((js->jtag_state != ILLEGAL_JTAG_STATE) &&
  331. (js->jtag_state != wait_state))
  332. status = altera_goto_jstate(astate, wait_state);
  333. if (status == 0)
  334. /* Wait for specified time interval */
  335. udelay(microseconds);
  336. return status;
  337. }
  338. static void altera_concatenate_data(u8 *buffer,
  339. u8 *preamble_data,
  340. u32 preamble_count,
  341. u8 *target_data,
  342. u32 start_index,
  343. u32 target_count,
  344. u8 *postamble_data,
  345. u32 postamble_count)
  346. /*
  347. * Copies preamble data, target data, and postamble data
  348. * into one buffer for IR or DR scans.
  349. */
  350. {
  351. u32 i, j, k;
  352. for (i = 0L; i < preamble_count; ++i) {
  353. if (preamble_data[i >> 3L] & (1L << (i & 7L)))
  354. buffer[i >> 3L] |= (1L << (i & 7L));
  355. else
  356. buffer[i >> 3L] &= ~(u32)(1L << (i & 7L));
  357. }
  358. j = start_index;
  359. k = preamble_count + target_count;
  360. for (; i < k; ++i, ++j) {
  361. if (target_data[j >> 3L] & (1L << (j & 7L)))
  362. buffer[i >> 3L] |= (1L << (i & 7L));
  363. else
  364. buffer[i >> 3L] &= ~(u32)(1L << (i & 7L));
  365. }
  366. j = 0L;
  367. k = preamble_count + target_count + postamble_count;
  368. for (; i < k; ++i, ++j) {
  369. if (postamble_data[j >> 3L] & (1L << (j & 7L)))
  370. buffer[i >> 3L] |= (1L << (i & 7L));
  371. else
  372. buffer[i >> 3L] &= ~(u32)(1L << (i & 7L));
  373. }
  374. }
  375. static int alt_jtag_drscan(struct altera_state *astate,
  376. int start_state,
  377. int count,
  378. u8 *tdi,
  379. u8 *tdo)
  380. {
  381. int i = 0;
  382. int tdo_bit = 0;
  383. int status = 1;
  384. /* First go to DRSHIFT state */
  385. switch (start_state) {
  386. case 0: /* IDLE */
  387. alt_jtag_io(1, 0, 0); /* DRSELECT */
  388. alt_jtag_io(0, 0, 0); /* DRCAPTURE */
  389. alt_jtag_io(0, 0, 0); /* DRSHIFT */
  390. break;
  391. case 1: /* DRPAUSE */
  392. alt_jtag_io(1, 0, 0); /* DREXIT2 */
  393. alt_jtag_io(1, 0, 0); /* DRUPDATE */
  394. alt_jtag_io(1, 0, 0); /* DRSELECT */
  395. alt_jtag_io(0, 0, 0); /* DRCAPTURE */
  396. alt_jtag_io(0, 0, 0); /* DRSHIFT */
  397. break;
  398. case 2: /* IRPAUSE */
  399. alt_jtag_io(1, 0, 0); /* IREXIT2 */
  400. alt_jtag_io(1, 0, 0); /* IRUPDATE */
  401. alt_jtag_io(1, 0, 0); /* DRSELECT */
  402. alt_jtag_io(0, 0, 0); /* DRCAPTURE */
  403. alt_jtag_io(0, 0, 0); /* DRSHIFT */
  404. break;
  405. default:
  406. status = 0;
  407. }
  408. if (status) {
  409. /* loop in the SHIFT-DR state */
  410. for (i = 0; i < count; i++) {
  411. tdo_bit = alt_jtag_io(
  412. (i == count - 1),
  413. tdi[i >> 3] & (1 << (i & 7)),
  414. (tdo != NULL));
  415. if (tdo != NULL) {
  416. if (tdo_bit)
  417. tdo[i >> 3] |= (1 << (i & 7));
  418. else
  419. tdo[i >> 3] &= ~(u32)(1 << (i & 7));
  420. }
  421. }
  422. alt_jtag_io(0, 0, 0); /* DRPAUSE */
  423. }
  424. return status;
  425. }
  426. static int alt_jtag_irscan(struct altera_state *astate,
  427. int start_state,
  428. int count,
  429. u8 *tdi,
  430. u8 *tdo)
  431. {
  432. int i = 0;
  433. int tdo_bit = 0;
  434. int status = 1;
  435. /* First go to IRSHIFT state */
  436. switch (start_state) {
  437. case 0: /* IDLE */
  438. alt_jtag_io(1, 0, 0); /* DRSELECT */
  439. alt_jtag_io(1, 0, 0); /* IRSELECT */
  440. alt_jtag_io(0, 0, 0); /* IRCAPTURE */
  441. alt_jtag_io(0, 0, 0); /* IRSHIFT */
  442. break;
  443. case 1: /* DRPAUSE */
  444. alt_jtag_io(1, 0, 0); /* DREXIT2 */
  445. alt_jtag_io(1, 0, 0); /* DRUPDATE */
  446. alt_jtag_io(1, 0, 0); /* DRSELECT */
  447. alt_jtag_io(1, 0, 0); /* IRSELECT */
  448. alt_jtag_io(0, 0, 0); /* IRCAPTURE */
  449. alt_jtag_io(0, 0, 0); /* IRSHIFT */
  450. break;
  451. case 2: /* IRPAUSE */
  452. alt_jtag_io(1, 0, 0); /* IREXIT2 */
  453. alt_jtag_io(1, 0, 0); /* IRUPDATE */
  454. alt_jtag_io(1, 0, 0); /* DRSELECT */
  455. alt_jtag_io(1, 0, 0); /* IRSELECT */
  456. alt_jtag_io(0, 0, 0); /* IRCAPTURE */
  457. alt_jtag_io(0, 0, 0); /* IRSHIFT */
  458. break;
  459. default:
  460. status = 0;
  461. }
  462. if (status) {
  463. /* loop in the SHIFT-IR state */
  464. for (i = 0; i < count; i++) {
  465. tdo_bit = alt_jtag_io(
  466. (i == count - 1),
  467. tdi[i >> 3] & (1 << (i & 7)),
  468. (tdo != NULL));
  469. if (tdo != NULL) {
  470. if (tdo_bit)
  471. tdo[i >> 3] |= (1 << (i & 7));
  472. else
  473. tdo[i >> 3] &= ~(u32)(1 << (i & 7));
  474. }
  475. }
  476. alt_jtag_io(0, 0, 0); /* IRPAUSE */
  477. }
  478. return status;
  479. }
  480. static void altera_extract_target_data(u8 *buffer,
  481. u8 *target_data,
  482. u32 start_index,
  483. u32 preamble_count,
  484. u32 target_count)
  485. /*
  486. * Copies target data from scan buffer, filtering out
  487. * preamble and postamble data.
  488. */
  489. {
  490. u32 i;
  491. u32 j;
  492. u32 k;
  493. j = preamble_count;
  494. k = start_index + target_count;
  495. for (i = start_index; i < k; ++i, ++j) {
  496. if (buffer[j >> 3] & (1 << (j & 7)))
  497. target_data[i >> 3] |= (1 << (i & 7));
  498. else
  499. target_data[i >> 3] &= ~(u32)(1 << (i & 7));
  500. }
  501. }
  502. int altera_irscan(struct altera_state *astate,
  503. u32 count,
  504. u8 *tdi_data,
  505. u32 start_index)
  506. /* Shifts data into instruction register */
  507. {
  508. struct altera_jtag *js = &astate->js;
  509. int start_code = 0;
  510. u32 alloc_chars = 0;
  511. u32 shift_count = js->ir_pre + count + js->ir_post;
  512. int status = 0;
  513. enum altera_jtag_state start_state = ILLEGAL_JTAG_STATE;
  514. switch (js->jtag_state) {
  515. case ILLEGAL_JTAG_STATE:
  516. case RESET:
  517. case IDLE:
  518. start_code = 0;
  519. start_state = IDLE;
  520. break;
  521. case DRSELECT:
  522. case DRCAPTURE:
  523. case DRSHIFT:
  524. case DREXIT1:
  525. case DRPAUSE:
  526. case DREXIT2:
  527. case DRUPDATE:
  528. start_code = 1;
  529. start_state = DRPAUSE;
  530. break;
  531. case IRSELECT:
  532. case IRCAPTURE:
  533. case IRSHIFT:
  534. case IREXIT1:
  535. case IRPAUSE:
  536. case IREXIT2:
  537. case IRUPDATE:
  538. start_code = 2;
  539. start_state = IRPAUSE;
  540. break;
  541. default:
  542. status = -EREMOTEIO;
  543. break;
  544. }
  545. if (status == 0)
  546. if (js->jtag_state != start_state)
  547. status = altera_goto_jstate(astate, start_state);
  548. if (status == 0) {
  549. if (shift_count > js->ir_length) {
  550. alloc_chars = (shift_count + 7) >> 3;
  551. kfree(js->ir_buffer);
  552. js->ir_buffer = (u8 *)alt_malloc(alloc_chars);
  553. if (js->ir_buffer == NULL)
  554. status = -ENOMEM;
  555. else
  556. js->ir_length = alloc_chars * 8;
  557. }
  558. }
  559. if (status == 0) {
  560. /*
  561. * Copy preamble data, IR data,
  562. * and postamble data into a buffer
  563. */
  564. altera_concatenate_data(js->ir_buffer,
  565. js->ir_pre_data,
  566. js->ir_pre,
  567. tdi_data,
  568. start_index,
  569. count,
  570. js->ir_post_data,
  571. js->ir_post);
  572. /* Do the IRSCAN */
  573. alt_jtag_irscan(astate,
  574. start_code,
  575. shift_count,
  576. js->ir_buffer,
  577. NULL);
  578. /* alt_jtag_irscan() always ends in IRPAUSE state */
  579. js->jtag_state = IRPAUSE;
  580. }
  581. if (status == 0)
  582. if (js->irstop_state != IRPAUSE)
  583. status = altera_goto_jstate(astate, js->irstop_state);
  584. return status;
  585. }
  586. int altera_swap_ir(struct altera_state *astate,
  587. u32 count,
  588. u8 *in_data,
  589. u32 in_index,
  590. u8 *out_data,
  591. u32 out_index)
  592. /* Shifts data into instruction register, capturing output data */
  593. {
  594. struct altera_jtag *js = &astate->js;
  595. int start_code = 0;
  596. u32 alloc_chars = 0;
  597. u32 shift_count = js->ir_pre + count + js->ir_post;
  598. int status = 0;
  599. enum altera_jtag_state start_state = ILLEGAL_JTAG_STATE;
  600. switch (js->jtag_state) {
  601. case ILLEGAL_JTAG_STATE:
  602. case RESET:
  603. case IDLE:
  604. start_code = 0;
  605. start_state = IDLE;
  606. break;
  607. case DRSELECT:
  608. case DRCAPTURE:
  609. case DRSHIFT:
  610. case DREXIT1:
  611. case DRPAUSE:
  612. case DREXIT2:
  613. case DRUPDATE:
  614. start_code = 1;
  615. start_state = DRPAUSE;
  616. break;
  617. case IRSELECT:
  618. case IRCAPTURE:
  619. case IRSHIFT:
  620. case IREXIT1:
  621. case IRPAUSE:
  622. case IREXIT2:
  623. case IRUPDATE:
  624. start_code = 2;
  625. start_state = IRPAUSE;
  626. break;
  627. default:
  628. status = -EREMOTEIO;
  629. break;
  630. }
  631. if (status == 0)
  632. if (js->jtag_state != start_state)
  633. status = altera_goto_jstate(astate, start_state);
  634. if (status == 0) {
  635. if (shift_count > js->ir_length) {
  636. alloc_chars = (shift_count + 7) >> 3;
  637. kfree(js->ir_buffer);
  638. js->ir_buffer = (u8 *)alt_malloc(alloc_chars);
  639. if (js->ir_buffer == NULL)
  640. status = -ENOMEM;
  641. else
  642. js->ir_length = alloc_chars * 8;
  643. }
  644. }
  645. if (status == 0) {
  646. /*
  647. * Copy preamble data, IR data,
  648. * and postamble data into a buffer
  649. */
  650. altera_concatenate_data(js->ir_buffer,
  651. js->ir_pre_data,
  652. js->ir_pre,
  653. in_data,
  654. in_index,
  655. count,
  656. js->ir_post_data,
  657. js->ir_post);
  658. /* Do the IRSCAN */
  659. alt_jtag_irscan(astate,
  660. start_code,
  661. shift_count,
  662. js->ir_buffer,
  663. js->ir_buffer);
  664. /* alt_jtag_irscan() always ends in IRPAUSE state */
  665. js->jtag_state = IRPAUSE;
  666. }
  667. if (status == 0)
  668. if (js->irstop_state != IRPAUSE)
  669. status = altera_goto_jstate(astate, js->irstop_state);
  670. if (status == 0)
  671. /* Now extract the returned data from the buffer */
  672. altera_extract_target_data(js->ir_buffer,
  673. out_data, out_index,
  674. js->ir_pre, count);
  675. return status;
  676. }
  677. int altera_drscan(struct altera_state *astate,
  678. u32 count,
  679. u8 *tdi_data,
  680. u32 start_index)
  681. /* Shifts data into data register (ignoring output data) */
  682. {
  683. struct altera_jtag *js = &astate->js;
  684. int start_code = 0;
  685. u32 alloc_chars = 0;
  686. u32 shift_count = js->dr_pre + count + js->dr_post;
  687. int status = 0;
  688. enum altera_jtag_state start_state = ILLEGAL_JTAG_STATE;
  689. switch (js->jtag_state) {
  690. case ILLEGAL_JTAG_STATE:
  691. case RESET:
  692. case IDLE:
  693. start_code = 0;
  694. start_state = IDLE;
  695. break;
  696. case DRSELECT:
  697. case DRCAPTURE:
  698. case DRSHIFT:
  699. case DREXIT1:
  700. case DRPAUSE:
  701. case DREXIT2:
  702. case DRUPDATE:
  703. start_code = 1;
  704. start_state = DRPAUSE;
  705. break;
  706. case IRSELECT:
  707. case IRCAPTURE:
  708. case IRSHIFT:
  709. case IREXIT1:
  710. case IRPAUSE:
  711. case IREXIT2:
  712. case IRUPDATE:
  713. start_code = 2;
  714. start_state = IRPAUSE;
  715. break;
  716. default:
  717. status = -EREMOTEIO;
  718. break;
  719. }
  720. if (status == 0)
  721. if (js->jtag_state != start_state)
  722. status = altera_goto_jstate(astate, start_state);
  723. if (status == 0) {
  724. if (shift_count > js->dr_length) {
  725. alloc_chars = (shift_count + 7) >> 3;
  726. kfree(js->dr_buffer);
  727. js->dr_buffer = (u8 *)alt_malloc(alloc_chars);
  728. if (js->dr_buffer == NULL)
  729. status = -ENOMEM;
  730. else
  731. js->dr_length = alloc_chars * 8;
  732. }
  733. }
  734. if (status == 0) {
  735. /*
  736. * Copy preamble data, DR data,
  737. * and postamble data into a buffer
  738. */
  739. altera_concatenate_data(js->dr_buffer,
  740. js->dr_pre_data,
  741. js->dr_pre,
  742. tdi_data,
  743. start_index,
  744. count,
  745. js->dr_post_data,
  746. js->dr_post);
  747. /* Do the DRSCAN */
  748. alt_jtag_drscan(astate, start_code, shift_count,
  749. js->dr_buffer, NULL);
  750. /* alt_jtag_drscan() always ends in DRPAUSE state */
  751. js->jtag_state = DRPAUSE;
  752. }
  753. if (status == 0)
  754. if (js->drstop_state != DRPAUSE)
  755. status = altera_goto_jstate(astate, js->drstop_state);
  756. return status;
  757. }
  758. int altera_swap_dr(struct altera_state *astate, u32 count,
  759. u8 *in_data, u32 in_index,
  760. u8 *out_data, u32 out_index)
  761. /* Shifts data into data register, capturing output data */
  762. {
  763. struct altera_jtag *js = &astate->js;
  764. int start_code = 0;
  765. u32 alloc_chars = 0;
  766. u32 shift_count = js->dr_pre + count + js->dr_post;
  767. int status = 0;
  768. enum altera_jtag_state start_state = ILLEGAL_JTAG_STATE;
  769. switch (js->jtag_state) {
  770. case ILLEGAL_JTAG_STATE:
  771. case RESET:
  772. case IDLE:
  773. start_code = 0;
  774. start_state = IDLE;
  775. break;
  776. case DRSELECT:
  777. case DRCAPTURE:
  778. case DRSHIFT:
  779. case DREXIT1:
  780. case DRPAUSE:
  781. case DREXIT2:
  782. case DRUPDATE:
  783. start_code = 1;
  784. start_state = DRPAUSE;
  785. break;
  786. case IRSELECT:
  787. case IRCAPTURE:
  788. case IRSHIFT:
  789. case IREXIT1:
  790. case IRPAUSE:
  791. case IREXIT2:
  792. case IRUPDATE:
  793. start_code = 2;
  794. start_state = IRPAUSE;
  795. break;
  796. default:
  797. status = -EREMOTEIO;
  798. break;
  799. }
  800. if (status == 0)
  801. if (js->jtag_state != start_state)
  802. status = altera_goto_jstate(astate, start_state);
  803. if (status == 0) {
  804. if (shift_count > js->dr_length) {
  805. alloc_chars = (shift_count + 7) >> 3;
  806. kfree(js->dr_buffer);
  807. js->dr_buffer = (u8 *)alt_malloc(alloc_chars);
  808. if (js->dr_buffer == NULL)
  809. status = -ENOMEM;
  810. else
  811. js->dr_length = alloc_chars * 8;
  812. }
  813. }
  814. if (status == 0) {
  815. /*
  816. * Copy preamble data, DR data,
  817. * and postamble data into a buffer
  818. */
  819. altera_concatenate_data(js->dr_buffer,
  820. js->dr_pre_data,
  821. js->dr_pre,
  822. in_data,
  823. in_index,
  824. count,
  825. js->dr_post_data,
  826. js->dr_post);
  827. /* Do the DRSCAN */
  828. alt_jtag_drscan(astate,
  829. start_code,
  830. shift_count,
  831. js->dr_buffer,
  832. js->dr_buffer);
  833. /* alt_jtag_drscan() always ends in DRPAUSE state */
  834. js->jtag_state = DRPAUSE;
  835. }
  836. if (status == 0)
  837. if (js->drstop_state != DRPAUSE)
  838. status = altera_goto_jstate(astate, js->drstop_state);
  839. if (status == 0)
  840. /* Now extract the returned data from the buffer */
  841. altera_extract_target_data(js->dr_buffer,
  842. out_data,
  843. out_index,
  844. js->dr_pre,
  845. count);
  846. return status;
  847. }
  848. void altera_free_buffers(struct altera_state *astate)
  849. {
  850. struct altera_jtag *js = &astate->js;
  851. /* If the JTAG interface was used, reset it to TLR */
  852. if (js->jtag_state != ILLEGAL_JTAG_STATE)
  853. altera_jreset_idle(astate);
  854. kfree(js->dr_pre_data);
  855. js->dr_pre_data = NULL;
  856. kfree(js->dr_post_data);
  857. js->dr_post_data = NULL;
  858. kfree(js->dr_buffer);
  859. js->dr_buffer = NULL;
  860. kfree(js->ir_pre_data);
  861. js->ir_pre_data = NULL;
  862. kfree(js->ir_post_data);
  863. js->ir_post_data = NULL;
  864. kfree(js->ir_buffer);
  865. js->ir_buffer = NULL;
  866. }