issp_routines.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /* filename: ISSP_Routines.c
  2. */
  3. #include "issp_revision.h"
  4. #ifdef PROJECT_REV_304
  5. /*
  6. * Copyright 2006-2007, Cypress Semiconductor Corporation.
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. * MA 02110-1301, USA.
  21. */
  22. /*
  23. #include <m8c.h>
  24. #include "PSoCAPI.h"
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/fs.h>
  29. #include <linux/i2c.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/irq.h>
  32. #include <linux/sched.h>
  33. #include <linux/pm.h>
  34. #include <linux/sysctl.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/delay.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/input.h>
  39. /*#include <mach/regs-gpio.h>
  40. #include <plat/gpio-cfg.h>*/
  41. #include <linux/gpio.h>
  42. #include <linux/miscdevice.h>
  43. #include <linux/uaccess.h>
  44. #include <linux/io.h>
  45. #include <linux/hrtimer.h>
  46. #include "issp_defs.h"
  47. #include "issp_vectors.h"
  48. #include "issp_extern.h"
  49. #include "issp_errors.h"
  50. #include "issp_directives.h"
  51. #include "issp_delays.h"
  52. unsigned char bTargetDataIN;
  53. unsigned char abTargetDataOUT[MAX_TARGET_DATABUFF_LEN];
  54. unsigned char bTargetAddress;
  55. unsigned char bTargetDataPtr;
  56. unsigned char bTargetID[10];
  57. unsigned char bTargetStatus[10];
  58. /*PTJ: created to support READ-STATUS in fReadStatus()*/
  59. unsigned char fIsError;
  60. /* ((((((((((((((((((((( LOW-LEVEL ISSP SUBROUTINE SECTION ))))))))))))))))))))
  61. (( The subroutines in this section use functions from the C file ))
  62. (( ISSP_Drive_Routines.c. The functions in that file interface to the ))
  63. (( processor specific hardware. So, these functions should work as is, if ))
  64. (( the routines in ISSP_Drive_Routines.c are correctly converted. ))
  65. (((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))
  66. */
  67. /*============================================================================
  68. RunClock()
  69. Description:
  70. Run Clock without sending/receiving bits. Use this when transitioning from
  71. write to read and read to write "num_cycles" is number of SCLK cycles, not
  72. number of counter cycles.
  73. SCLK cannot run faster than the specified maximum frequency of 8MHz. Some
  74. processors may need to have delays added after setting SCLK low and setting
  75. SCLK high in order to not exceed this specification. The maximum frequency
  76. of SCLK should be measured as part of validation of the final program
  77. ============================================================================
  78. */
  79. void RunClock(unsigned int iNumCycles)
  80. {
  81. int i;
  82. for (i = 0; i < iNumCycles; i++) {
  83. SCLKLow();
  84. SCLKHigh();
  85. }
  86. }
  87. /*============================================================================
  88. bReceiveBit()
  89. Clocks the SCLK pin (high-low-high) and reads the status of the SDATA pin
  90. after the rising edge.
  91. SCLK cannot run faster than the specified maximum frequency of 8MHz. Some
  92. processors may need to have delays added after setting SCLK low and setting
  93. SCLK high in order to not exceed this specification. The maximum frequency
  94. of SCLK should be measured as part of validation of the final program
  95. Returns:
  96. 0 if SDATA was low
  97. 1 if SDATA was high
  98. ============================================================================
  99. */
  100. unsigned char bReceiveBit(void)
  101. {
  102. SCLKLow();
  103. SCLKHigh();
  104. if (fSDATACheck())
  105. return 1;
  106. else
  107. return 0;
  108. }
  109. /*============================================================================
  110. bReceiveByte()
  111. Calls ReceiveBit 8 times to receive one byte.
  112. Returns:
  113. The 8-bit values recieved.
  114. ============================================================================
  115. */
  116. unsigned char bReceiveByte(void)
  117. {
  118. unsigned char b;
  119. unsigned char bCurrByte = 0x00;
  120. for (b = 0; b < 8; b++)
  121. bCurrByte = (bCurrByte<<1) + bReceiveBit();
  122. return bCurrByte;
  123. }
  124. /*
  125. ============================================================================
  126. SendByte()
  127. This routine sends up to one byte of a vector, one bit at a time.
  128. bCurrByte the byte that contains the bits to be sent.
  129. bSize the number of bits to be sent. Valid values are 1 to 8.
  130. SCLK cannot run faster than the specified maximum frequency of 8MHz. Some
  131. processors may need to have delays added after setting SCLK low and setting
  132. SCLK high in order to not exceed this specification. The maximum frequency
  133. of SCLK should be measured as part of validation of the final program
  134. There is no returned value.
  135. ============================================================================
  136. */
  137. void SendByte(unsigned char bCurrByte, unsigned char bSize)
  138. {
  139. unsigned char b = 0;
  140. for (b = 0; b < bSize; b++) {
  141. if (bCurrByte & 0x80) {
  142. SetSDATAHigh();
  143. SCLKHigh();
  144. SCLKLow();
  145. } else {
  146. SetSDATALow();
  147. SCLKHigh();
  148. SCLKLow();
  149. }
  150. bCurrByte = bCurrByte << 1;
  151. }
  152. }
  153. /*============================================================================
  154. SendVector()
  155. This routine sends the vector specifed. All vectors constant strings found
  156. in ISSP_Vectors.h. The data line is returned to HiZ after the vector is
  157. sent.
  158. bVect a pointer to the vector to be sent.
  159. nNumBits the number of bits to be sent.
  160. bCurrByte scratch var to keep the byte to be sent.
  161. There is no returned value.
  162. ============================================================================
  163. */
  164. void SendVector(const unsigned char *bVect, unsigned int iNumBits)
  165. {
  166. SetSDATAStrong();
  167. while (iNumBits > 0) {
  168. if (iNumBits >= 8) {
  169. SendByte(*(bVect), 8);
  170. iNumBits -= 8;
  171. bVect++;
  172. } else {
  173. SendByte(*(bVect), iNumBits);
  174. iNumBits = 0;
  175. }
  176. }
  177. SetSDATAHiZ();
  178. }
  179. /*
  180. ============================================================================
  181. fDetectHiLoTransition()
  182. Waits for transition from SDATA = 1 to SDATA = 0. Has a 100 msec timeout.
  183. TRANSITION_TIMEOUT is a loop counter for a 100msec timeout when waiting for
  184. a high-to-low transition. This is used in the polling loop of
  185. fDetectHiLoTransition(). The timing of the while(1) loops can be calculated
  186. and the number of loops is counted, using iTimer, to determine when 100
  187. msec has passed.
  188. SCLK cannot run faster than the specified maximum frequency of 8MHz. Some
  189. processors may need to have delays added after setting SCLK low and setting
  190. SCLK high in order to not exceed this specification. The maximum frequency
  191. of SCLK should be measured as part of validation of the final program
  192. Returns:
  193. 0 if successful
  194. -1 if timed out.
  195. ============================================================================
  196. */
  197. signed char fDetectHiLoTransition(void)
  198. {
  199. /* nTimer breaks out of the while loops if the wait in the two loops totals
  200. more than 100 msec. Making this static makes the loop run a faster.
  201. This is really a processor/compiler dependency and it not needed.
  202. */
  203. unsigned long int iTimer = 0;
  204. /* NOTE:
  205. These loops look unconventional, but it is necessary to check SDATA_PIN
  206. as shown because the transition can be missed otherwise, due to the
  207. length of the SDATA Low-High-Low after certain commands.
  208. Generate clocks for the target to pull SDATA High
  209. */
  210. iTimer = TRANSITION_TIMEOUT;
  211. SetSCLKStrong();
  212. while (1) {
  213. SCLKLow();
  214. /*Delay(DELAY100us);
  215. Delay(CLK_DELAY); */
  216. if (fSDATACheck()) /* exit once SDATA goes HI */
  217. break;
  218. SCLKHigh();
  219. /*Delay(DELAY100us);
  220. Delay(CLK_DELAY);
  221. If the wait is too long then timeout*/
  222. if (iTimer-- == 0)
  223. return ERROR;
  224. }
  225. /*printk(KERN_ERR"[TKEY] *****1\n");*/
  226. /*
  227. Generate Clocks and wait for Target to pull SDATA Low again
  228. */
  229. iTimer = TRANSITION_TIMEOUT;
  230. while (1) {
  231. SCLKLow();
  232. /*udelay(1);*/
  233. if (!fSDATACheck()) /* exit once SDATA returns LOW */
  234. break;
  235. /*SCLKHigh();
  236. udelay(300);
  237. If the wait is too long then timeout */
  238. if (iTimer-- == 0)
  239. return ERROR;
  240. }
  241. /*printk(KERN_ERR"[TKEY] *****2\n");*/
  242. return PASS;
  243. }
  244. /* ((((((((((((((((((((( HIGH-LEVEL ISSP ROUTINE SECTION ))))))))))))))))))))))
  245. (( These functions are mostly made of calls to the low level routines ))
  246. (( above. This should isolate the processor-specific changes so that ))
  247. (( these routines do not need to be modified. ))
  248. (((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))
  249. */
  250. #ifdef RESET_MODE
  251. /*============================================================================
  252. fXRESInitializeTargetForISSP()
  253. Implements the intialization vectors for the device.
  254. Returns:
  255. 0 if successful
  256. INIT_ERROR if timed out on handshake to the device.
  257. ============================================================================
  258. */
  259. signed char fXRESInitializeTargetForISSP(void)
  260. {
  261. /*Configure the pins for initialization*/
  262. SetSDATAHiZ();
  263. SetSCLKStrong();
  264. SCLKLow();
  265. SetXRESStrong();
  266. /* Cycle reset and put the device
  267. in programming mode when it exits reset */
  268. AssertXRES();
  269. Delay(XRES_CLK_DELAY);
  270. DeassertXRES();
  271. /*
  272. !!! NOTE:
  273. The timing spec that requires that the first Init-Vector happen within
  274. 1 msec after the reset/power up. For this reason, it is not advisable
  275. to separate the above RESET_MODE or POWER_CYCLE_MODE code from the
  276. Init-Vector instructions below. Doing so could introduce excess delay
  277. and cause the target device to exit ISSP Mode.
  278. PTJ: Send id_setup_1 instead of init1_v
  279. PTJ: both send CA Test Key and do a Calibrate1 SROM function
  280. */
  281. SendVector(id_setup_1, num_bits_id_setup_1);
  282. fIsError = fDetectHiLoTransition();
  283. if (fIsError != 0)
  284. return INIT_ERROR;
  285. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  286. /*
  287. NOTE: DO NOT not wait for HiLo on SDATA after vector Init-3
  288. it does not occur (per spec).
  289. */
  290. return PASS;
  291. }
  292. #else /* else = the part is power cycle programmed */
  293. /*
  294. ============================================================================
  295. fPowerCycleInitializeTargetForISSP()
  296. Implements the intialization vectors for the device.
  297. The first time fDetectHiLoTransition is called the Clk pin is highZ because
  298. the clock is not needed during acquire.
  299. Returns:
  300. 0 if successful
  301. INIT_ERROR if timed out on handshake to the device.
  302. ============================================================================
  303. */
  304. signed char fPowerCycleInitializeTargetForISSP(struct cypress_touchkey_info *info)
  305. {
  306. unsigned char n;
  307. /*
  308. Set all pins to highZ to avoid back powering the PSoC through the GPIO
  309. protection diodes.
  310. */
  311. SetSCLKHiZ();
  312. SetSDATAHiZ();
  313. /* Turn on power to the target device before other signals */
  314. SetTargetVDDStrong();
  315. for (n = 0; n < 200; n++)
  316. Delay(50000);
  317. ApplyTargetVDD(info);
  318. /* wait 1msec for the power to stabilize */
  319. for (n = 0; n < 10; n++)
  320. Delay(DELAY100us);
  321. /*printk(KERN_ERR"[TKEY] ___1111\n");*/
  322. /*
  323. Set SCLK to high Z so there is no clock and wait for a high to low
  324. transition on SDAT. SCLK is not needed this time.
  325. */
  326. SetSCLKHiZ();
  327. fIsError = fDetectHiLoTransition();
  328. /*printk(KERN_ERR"[TKEY] ___2222 %d\n",fIsError);*/
  329. if (fIsError != 0)
  330. return INIT_ERROR;
  331. /*Configure the pins for initialization */
  332. SetSDATAHiZ();
  333. SetSCLKStrong();
  334. SCLKLow();
  335. /*PTJ: DO NOT SET A BREAKPOINT HERE AND EXPECT SILICON ID TO PASS!
  336. !!! NOTE:
  337. The timing spec that requires that the first Init-Vector happen within
  338. 1 msec after the reset/power up. For this reason, it is not advisable
  339. to separate the above RESET_MODE or POWER_CYCLE_MODE code from the
  340. Init-Vector instructions below. Doing so could introduce excess delay
  341. and cause the target device to exit ISSP Mode.
  342. */
  343. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  344. /*20100114 KJHW(Jason) : 0114 by KJHW */
  345. SendVector(id_setup_1, num_bits_id_setup_1);
  346. fIsError = fDetectHiLoTransition();
  347. if (fIsError != 0)
  348. return INIT_ERROR;
  349. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  350. /*
  351. NOTE: DO NOT not wait for HiLo on SDATA after vector Init-3
  352. it does not occur (per spec).
  353. */
  354. return PASS;
  355. }
  356. #endif
  357. /* ============================================================================
  358. // fVerifySiliconID()
  359. // Returns:
  360. // 0 if successful
  361. // Si_ID_ERROR if timed out on handshake to the device.
  362. // ==========================================================================*/
  363. signed char fVerifySiliconID(void)
  364. {
  365. SendVector(id_setup_2, num_bits_id_setup_2);
  366. fIsError = fDetectHiLoTransition();
  367. if (fIsError) {
  368. /*printk("fVerifySiliconID(): fDetectHiLoTransition Error\n");*/
  369. return SiID_ERROR;
  370. }
  371. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  372. SendVector(tsync_enable, num_bits_tsync_enable);
  373. /*Send Read ID vector and get Target ID*/
  374. SendVector(read_id_v, 11);/* Read-MSB Vector is the first 11-Bits*/
  375. RunClock(2);/* Two SCLK cycles between write & read*/
  376. bTargetID[0] = bReceiveByte();
  377. RunClock(1);
  378. SendVector(read_id_v+2, 12);/* 1+11 bits starting from the 3rd byte*/
  379. RunClock(2);/* Read-LSB Command*/
  380. bTargetID[1] = bReceiveByte();
  381. RunClock(1);
  382. SendVector(read_id_v+4, 1);/* 1 bit starting from the 5th byte*/
  383. /*read Revision ID from Accumulator A and Accumulator X*/
  384. SendVector(read_id_v+5, 11);/*11 bits starting from the 6th byte*/
  385. RunClock(2);
  386. bTargetID[2] = bReceiveByte();/*Read from Acc.X*/
  387. RunClock(1);
  388. SendVector(read_id_v+7, 12);/*1+11 bits starting from the 8th byte*/
  389. RunClock(2);
  390. bTargetID[3] = bReceiveByte();/*Read from Acc.A*/
  391. RunClock(1);
  392. SendVector(read_id_v+4, 1);/*1bit starting from the 5th byte,*/
  393. SendVector(tsync_disable, num_bits_tsync_disable);
  394. return PASS;
  395. }
  396. /* PTJ: =======================================================================
  397. // fReadStatus()
  398. // Returns:
  399. // 0 if successful
  400. // _____ if timed out on handshake to the device.
  401. // =========================================================================*/
  402. signed char fReadStatus(void)
  403. {
  404. SendVector(tsync_enable, num_bits_tsync_enable);
  405. /*Send Read ID vector and get Target ID*/
  406. SendVector(read_id_v, 11);/* Read-MSB Vector is the first 11-Bits*/
  407. RunClock(2); /* Two SCLK cycles between write & read*/
  408. bTargetStatus[0] = bReceiveByte();
  409. RunClock(1);
  410. /*SendVector(read_id_v+2, 12);// 12 bits starting from the 3rd character
  411. //RunClock(2); // Read-LSB Command
  412. //bTargetStatus[1] = bReceiveByte();
  413. //RunClock(1);*/
  414. SendVector(read_id_v+4, 1);/* 1 bit starting from the 5th character*/
  415. SendVector(tsync_disable, num_bits_tsync_disable);
  416. if (bTargetStatus[0] == target_status00_v)
  417. return PASS;
  418. /*PTJ: Status = 00 means Success, the SROM function did
  419. what it was supposed to*/
  420. if (bTargetStatus[0] == target_status01_v)
  421. return STATUS_ERROR;
  422. /*PTJ: Status = 01 means that function is not allowed because of block
  423. level protection, for test with verify_setup (VERIFY-SETUP)*/
  424. if (bTargetStatus[0] == target_status03_v)
  425. return STATUS_ERROR;
  426. /*PTJ: Status = 03 is fatal error, SROM halted*/
  427. if (bTargetStatus[0] == target_status04_v)
  428. return STATUS_ERROR;
  429. /*PTJ: Status = 04 means there was a checksum faliure with either
  430. the smart write code checksum, or the smart write paramters
  431. checksum, for test with PROGRAM-AND-VERIFY*/
  432. if (bTargetStatus[0] == target_status06_v)
  433. return STATUS_ERROR;
  434. /*PTJ: Status = 06 means that Calibrate1 failed, for test with
  435. id_setup_1 (ID-SETUP-1)*/
  436. else
  437. return STATUS_ERROR;
  438. }
  439. /* PTJ: =======================================================================
  440. // fReadCalRegisters()
  441. // PTJ: use this to read some cal registers that should be loaded by
  442. Calibrate1 in id_setup_1
  443. // Returns:
  444. // 0 if successful
  445. // _____ if timed out on handshake to the device.
  446. // ==========================================================================*/
  447. signed char fReadCalRegisters(void)
  448. {
  449. SendVector(tsync_enable, num_bits_tsync_enable);
  450. SendVector(Switch_Bank1, 22);
  451. SendVector(read_IMOtrim, 11);/* Read-MSB Vector is the first 11-Bits*/
  452. RunClock(2);/* Two SCLK cycles between write & read*/
  453. bTargetStatus[0] = bReceiveByte();
  454. RunClock(1);
  455. /*Set SDATA to Strong Drive here because SendByte() does not*/
  456. SetSDATAStrong();
  457. SendByte(read_reg_end, 1);
  458. SendVector(read_SPCtrim, 11);/* Read-MSB Vector is the first 11-Bits*/
  459. RunClock(2);/* Two SCLK cycles between write & read*/
  460. bTargetStatus[1] = bReceiveByte();
  461. RunClock(1);
  462. /* Set SDATA to Strong Drive here because SendByte() does not*/
  463. SetSDATAStrong();
  464. SendByte(read_reg_end, 1);
  465. SendVector(read_VBGfinetrim, 11);/* Read-MSB Vector is the first
  466. 11-Bits*/
  467. RunClock(2);/* Two SCLK cycles between write & read*/
  468. bTargetStatus[2] = bReceiveByte();
  469. RunClock(1);
  470. /*Set SDATA to Strong Drive here because SendByte() does not*/
  471. SetSDATAStrong();
  472. SendByte(read_reg_end, 1);
  473. SendVector(Switch_Bank0, 22);
  474. SendVector(tsync_disable, num_bits_tsync_disable);
  475. if (bTargetStatus[0] == target_status00_v) {
  476. return PASS;
  477. /*PTJ: Status = 00 means Success, the SROM function
  478. did what it was supposed to*/
  479. }
  480. return PASS;
  481. }
  482. /* PTJ: =======================================================================
  483. // fReadWriteSetup()
  484. // PTJ: The READ-WRITE-SETUP vector will enable TSYNC and switches the device
  485. // to SRAM bank1 for PROGRAM-AND-VERIFY, SECURE and VERIFY-SETUP.
  486. // Returns:
  487. // 0 if successful
  488. // _____ if timed out on handshake to the device.
  489. // ==========================================================================*/
  490. signed char fReadWriteSetup(void)
  491. {
  492. SendVector(read_write_setup, num_bits_read_write_setup);
  493. return PASS;
  494. /*PTJ: is there anything else that should be done?*/
  495. }
  496. /* PTJ: =======================================================================
  497. // fSyncEnable()
  498. // PTJ: The SYNC-ENABLE vector will enable TSYNC
  499. //
  500. // Returns:
  501. // 0 if successful
  502. // _____ if timed out on handshake to the device.
  503. // ==========================================================================*/
  504. signed char fSyncEnable(void)
  505. {
  506. SendVector(tsync_enable, num_bits_tsync_enable);
  507. /*PTJ: 307 for tsync enable testing*/
  508. return PASS;
  509. /*PTJ: is there anything else that should be done?*/
  510. }
  511. /* PTJ: =======================================================================
  512. // fSyncDisable()
  513. // PTJ: The SYNC-ENABLE vector will enable TSYNC
  514. //
  515. // Returns:
  516. // 0 if successful
  517. // _____ if timed out on handshake to the device.
  518. // =========================================================================*/
  519. signed char fSyncDisable(void)
  520. {
  521. SendVector(tsync_disable, num_bits_tsync_disable);
  522. /*PTJ: 307 for tsync enable testing*/
  523. return PASS;
  524. }
  525. /* ============================================================================
  526. fEraseTarget()
  527. Perform a bulk erase of the target device.
  528. Returns:
  529. 0 if successful
  530. ERASE_ERROR if timed out on handshake to the device.
  531. ============================================================================
  532. */
  533. signed char fEraseTarget(void)
  534. {
  535. SendVector(erase, num_bits_erase);
  536. fIsError = fDetectHiLoTransition();
  537. if (fIsError != 0)
  538. return ERASE_ERROR;
  539. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  540. return PASS;
  541. }
  542. /*============================================================================
  543. LoadTarget()
  544. Transfers data from array in Host to RAM buffer in the target.
  545. Returns the checksum of the data.
  546. ============================================================================*/
  547. unsigned int iLoadTarget(void)
  548. {
  549. unsigned char bTemp;
  550. unsigned int iChecksumData = 0;
  551. /*Set SDATA to Strong Drive here because SendByte() does not */
  552. SetSDATAStrong();
  553. bTargetAddress = 0x00;
  554. bTargetDataPtr = 0x00;
  555. while (bTargetDataPtr < TargetDatabufLen) {
  556. bTemp = abTargetDataOUT[bTargetDataPtr];
  557. iChecksumData += bTemp;
  558. SendByte(write_byte_start, 4);
  559. /*PTJ: we need to be able to write 128 bytes from address 0x80 to 0xFF*/
  560. SendByte(bTargetAddress, 7);
  561. /*PTJ: we need to be able to write 128 bytes from address 0x80 to 0xFF*/
  562. SendByte(bTemp, 8);
  563. SendByte(write_byte_end, 3);
  564. bTargetAddress += 2;
  565. /*PTJ: inc by 2 in order to support a 128 byte address
  566. space, MSB~1 for address*/
  567. bTargetDataPtr++;
  568. }
  569. return iChecksumData;
  570. }
  571. #ifdef MULTI_BANK
  572. /* ============================================================================
  573. // SetBankNumber()
  574. // Set the bank number in the target device.
  575. // Returns:
  576. // none
  577. // ==========================================================================*/
  578. void SetBankNumber(unsigned char bBankNumber)
  579. {
  580. /*Send the bank-select vector.*/
  581. SendVector(set_bank_number, 33);
  582. /* Set the drive here because SendByte() does not.*/
  583. SetSDATAStrong();
  584. SendByte(bBankNumber, 8);
  585. SendVector(set_bank_number_end, 25);
  586. }
  587. #endif
  588. /*============================================================================
  589. fProgramTargetBlock()
  590. Program one block with data that has been loaded into a RAM buffer in the
  591. target device.
  592. Returns:
  593. 0 if successful
  594. BLOCK_ERROR if timed out on handshake to the device.
  595. ============================================================================
  596. */
  597. signed char fProgramTargetBlock(unsigned char bBankNumber,
  598. unsigned char bBlockNumber)
  599. {
  600. SendVector(tsync_enable, num_bits_tsync_enable);
  601. SendVector(set_block_num, num_bits_set_block_num);
  602. /* Set the drive here because SendByte() does not.*/
  603. SetSDATAStrong();
  604. SendByte(bBlockNumber, 8);
  605. SendByte(set_block_num_end, 3);
  606. SendVector(tsync_disable, num_bits_tsync_disable);
  607. /*Send the program-block vector.*/
  608. SendVector(program_and_verify, num_bits_program_and_verify);
  609. /*PTJ: PROGRAM-AND-VERIFY
  610. wait for acknowledge from target.*/
  611. fIsError = fDetectHiLoTransition();
  612. if (fIsError)
  613. return BLOCK_ERROR;
  614. /* Send the Wait-For-Poll-End vector*/
  615. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  616. return PASS;
  617. /*PTJ: Don't do READ-STATUS here because that will
  618. //PTJ: require that we return multiple error values, if error occurs*/
  619. }
  620. /*============================================================================
  621. fAddTargetBankChecksum()
  622. Reads and adds the target bank checksum to the referenced accumulator.
  623. Returns:
  624. 0 if successful
  625. VERIFY_ERROR if timed out on handshake to the device.
  626. ============================================================================
  627. */
  628. signed char fAccTargetBankChecksum(unsigned int *pAcc)
  629. {
  630. unsigned int wCheckSumData = 0;
  631. SendVector(checksum_v, num_bits_checksum);
  632. fIsError = fDetectHiLoTransition();
  633. if (fIsError != 0)
  634. return CHECKSUM_ERROR;
  635. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  636. SendVector(tsync_enable, num_bits_tsync_enable);
  637. SendVector(read_checksum_v, 11); /*first 11-bits is ReadCKSum-MSB*/
  638. RunClock(2); /*Two SCLKs between write & read*/
  639. bTargetDataIN = bReceiveByte();
  640. wCheckSumData = bTargetDataIN<<8;
  641. RunClock(1);
  642. /*12 bits starting from 3rd character*/
  643. SendVector(read_checksum_v + 2, 12);
  644. RunClock(2); /* Read-LSB Command*/
  645. bTargetDataIN = bReceiveByte();
  646. wCheckSumData |= (bTargetDataIN & 0xFF);
  647. RunClock(1);
  648. /*Send the final bit of the command */
  649. SendVector(read_checksum_v + 3, 1);
  650. /* Send the final bit of the command
  651. PTJ: read_checksum_v may have to change if TSYNC needs to be enabled*/
  652. SendVector(tsync_disable, num_bits_tsync_disable);
  653. *pAcc = wCheckSumData;
  654. return PASS;
  655. }
  656. /*
  657. ============================================================================
  658. ReStartTarget()
  659. After programming, the target PSoC must be reset to take it out of
  660. programming mode. This routine performs a reset.
  661. ============================================================================
  662. */
  663. void ReStartTarget(struct cypress_touchkey_info *info)
  664. {
  665. int i;
  666. #ifdef RESET_MODE
  667. /* Assert XRES, then release, then disable XRES-Enable */
  668. AssertXRES();
  669. Delay(XRES_CLK_DELAY);
  670. DeassertXRES();
  671. #else
  672. /* Set all pins to highZ to avoid back powering
  673. the PSoC through the GPIO protection diodes.
  674. */
  675. SetSCLKHiZ();
  676. SetSDATAHiZ();
  677. /* Cycle power on the target to cause a reset */
  678. RemoveTargetVDD(info);
  679. for (i = 0; i < 100; i++)
  680. Delay(50000);
  681. ApplyTargetVDD(info);
  682. #endif
  683. }
  684. /*============================================================================
  685. fVerifySetup()
  686. Verify the block just written to. This can be done byte-by-byte before the
  687. protection bits are set.
  688. Returns:
  689. 0 if successful
  690. BLOCK_ERROR if timed out on handshake to the device.
  691. ============================================================================
  692. */
  693. signed char fVerifySetup(unsigned char bBankNumber, unsigned char bBlockNumber)
  694. {
  695. SendVector(tsync_enable, num_bits_tsync_enable);
  696. SendVector(set_block_num, num_bits_set_block_num);
  697. /* Set the drive here because SendByte() does not */
  698. SetSDATAStrong();
  699. SendByte(bBlockNumber, 8);
  700. SendByte(set_block_num_end, 3);
  701. SendVector(tsync_disable, num_bits_tsync_disable);
  702. SendVector(verify_setup, num_bits_my_verify_setup);
  703. fIsError = fDetectHiLoTransition();
  704. if (fIsError != 0)
  705. return VERIFY_ERROR;
  706. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  707. return PASS;
  708. }
  709. /*============================================================================
  710. fReadByteLoop()
  711. Reads the data back from Target SRAM and compares it to expected data in
  712. Host SRAM
  713. Returns:
  714. 0 if successful
  715. BLOCK_ERROR if timed out on handshake to the device.
  716. ============================================================================
  717. */
  718. signed char fReadByteLoop(void)
  719. {
  720. bTargetAddress = 0;
  721. bTargetDataPtr = 0;
  722. while (bTargetDataPtr < TargetDatabufLen) {
  723. /* Send Read Byte vector and then get a byte from Target */
  724. SendVector(read_byte_v, 4);
  725. /* Set the drive here because SendByte() does not */
  726. SetSDATAStrong();
  727. SendByte(bTargetAddress, 7);
  728. /* Run two SCLK cycles between writing and reading */
  729. RunClock(2);
  730. /* Set to HiZ so Target can drive SDATA */
  731. SetSDATAHiZ();
  732. bTargetDataIN = bReceiveByte();
  733. RunClock(1);
  734. /* Send the ReadByte Vector End */
  735. SendVector(read_byte_v + 1, 1);
  736. if (bTargetDataIN != abTargetDataOUT[bTargetDataPtr]) {
  737. #ifdef TX_ON
  738. TX8SW_PutCRLF();
  739. TX8SW_CPutString("bTargetDataIN : ");
  740. TX8SW_PutSHexByte(bTargetDataIN);
  741. TX8SW_CPutString(" abTargetDataOUT : ");
  742. TX8SW_PutSHexByte(abTargetDataOUT[bTargetDataPtr]);
  743. #endif
  744. return BLOCK_ERROR;
  745. }
  746. bTargetDataPtr++;
  747. bTargetAddress += 2;
  748. }
  749. return PASS;
  750. }
  751. /* ============================================================================
  752. // fVerifyTargetBlock()
  753. // Verify the block just written to. This can be done byte-by-byte before the
  754. // protection bits are set.
  755. // Returns:
  756. // 0 if successful
  757. // BLOCK_ERROR if timed out on handshake to the device.
  758. // ==========================================================================*/
  759. signed char fVerifyTargetBlock(unsigned char bBankNumber,
  760. unsigned char bBlockNumber)
  761. {
  762. SendVector(set_block_number, 11);
  763. /*Set the drive here because SendByte() does not*/
  764. SetSDATAStrong();
  765. SendByte(bBlockNumber, 8);
  766. SendByte(set_block_number_end, 3);
  767. SendVector(verify_setup_v, num_bits_verify_setup);
  768. fIsError = fDetectHiLoTransition();
  769. if (fIsError)
  770. return BLOCK_ERROR;
  771. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  772. bTargetAddress = 0;
  773. bTargetDataPtr = 0;
  774. while (bTargetDataPtr < TARGET_DATABUFF_LEN) {
  775. /*Send Read Byte vector and then get a byte from Target*/
  776. SendVector(read_byte_v, 4);
  777. /*PTJ 308: this was changed from sending the first 5 bits to sending
  778. the first 4 Set the drive here because SendByte() does not*/
  779. SetSDATAStrong();
  780. SendByte(bTargetAddress, 6);
  781. RunClock(2);/* Run two SCLK cycles between writing and reading*/
  782. SetSDATAHiZ();/* Set to HiZ so Target can drive SDATA*/
  783. bTargetDataIN = bReceiveByte();
  784. RunClock(1);
  785. SendVector(read_byte_v + 1, 1);/* Send the ReadByte Vector End*/
  786. /* Test the Byte that was read from the Target against the original
  787. // value (already in the 128-Byte array "abTargetDataOUT[]"). If it
  788. // matches, then bump the address & pointer,loop-back and continue.
  789. // If it does NOT match abort the loop and return an error.*/
  790. if (bTargetDataIN != abTargetDataOUT[bTargetDataPtr])
  791. return BLOCK_ERROR;
  792. bTargetDataPtr++;
  793. /* Increment the address by four to accomodate 6-Bit addressing
  794. (puts the 6-bit address into MSBit locations for "SendByte()").*/
  795. bTargetAddress += 4;
  796. }
  797. return PASS;
  798. }
  799. /*
  800. fSecureTargetFlash()
  801. Before calling, load the array, abTargetDataOUT, with the desired security
  802. settings using LoadArrayWithSecurityData(StartAddress,Length,SecurityType).
  803. The can be called multiple times with different SecurityTypes as needed for
  804. particular Flash Blocks. Or set them all the same using the call below:
  805. LoadArrayWithSecurityData(0,SECURITY_BYTES_PER_BANK, 0);
  806. Returns:
  807. 0 if successful
  808. SECURITY_ERROR if timed out on handshake to the device.
  809. ============================================================================
  810. */
  811. signed char fSecureTargetFlash(void)
  812. {
  813. unsigned char bTemp;
  814. bTargetAddress = 0x00;
  815. bTargetDataPtr = 0x00;
  816. SetSDATAStrong();
  817. while (bTargetDataPtr < SecurityBytesPerBank) {
  818. bTemp = abTargetDataOUT[bTargetDataPtr];
  819. SendByte(write_byte_start, 4);
  820. SendByte(bTargetAddress, 7);
  821. SendByte(bTemp, 8);
  822. SendByte(write_byte_end, 3);
  823. bTargetAddress += 2;
  824. /*PTJ: inc by 2 in order to support a 128 byte address space*/
  825. bTargetDataPtr++;
  826. }
  827. SendVector(secure, num_bits_secure);
  828. fIsError = fDetectHiLoTransition();
  829. if (fIsError != 0)
  830. return SECURITY_ERROR;
  831. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  832. return PASS;
  833. }
  834. /* ============================================================================
  835. // PTJ: fReadSecurity()
  836. // This reads from SM0 with Read Supervisory SPC command.
  837. // Need to have SPC Test Mode enabled before using these commands?
  838. // Returns:
  839. // 0 if successful
  840. // __________ if timed out on handshake to the device.
  841. // =========================================================================*/
  842. signed char fReadSecurity(void)
  843. {
  844. SendVector(ReadSecuritySetup, num_bits_ReadSecuritySetup);
  845. /*SendVector(SPCTestMode_enable, num_bits_SPCTestMode_enable);*/
  846. bTargetAddress = 0x00;
  847. while (bTargetAddress < (SECURITY_BYTES_PER_BANK * 2)) {
  848. /*PTJ: we do SECURITY_BYTES_PER_BANK * 2 because we
  849. bTargetAddress += 2 PTJ: TSYNC Enable*/
  850. SendVector(tsync_enable, num_bits_tsync_enable);
  851. SendVector(read_security_pt1, num_bits_read_security_pt1);
  852. /* Set the drive here because SendByte() does not.*/
  853. SetSDATAStrong();
  854. SendByte(bTargetAddress, 7);
  855. /*PTJ: hardcode MSb of address as 0 in bit stream*/
  856. SendVector(read_security_pt1_end, num_bits_read_security_pt1_end);
  857. /*PTJ: TSYNC Disable*/
  858. SendVector(tsync_disable, num_bits_tsync_disable);
  859. SendVector(read_security_pt2, num_bits_read_security_pt2);
  860. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  861. SendVector(read_security_pt3, num_bits_read_security_pt3);
  862. SetSDATAStrong();
  863. SendByte(bTargetAddress, 7);
  864. SendVector(read_security_pt3_end, num_bits_read_security_pt3_end);
  865. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  866. bTargetAddress += 2;
  867. }
  868. bTargetAddress = 0x00;
  869. bTargetDataPtr = 0x00;
  870. SendVector(tsync_enable, num_bits_tsync_enable);
  871. /*PTJ: 307, added for tsync testing*/
  872. while (bTargetAddress < (SECURITY_BYTES_PER_BANK * 2)) {
  873. /*PTJ: we do SECURITY_BYTES_PER_BANK * 2 because we
  874. bTargetAddress += 2*/
  875. /*Send Read Byte vector and then get a byte from Target*/
  876. SendVector(read_byte_v, 4);
  877. /* Set the drive here because SendByte() does not*/
  878. SetSDATAStrong();
  879. SendByte(bTargetAddress, 7);
  880. RunClock(2); /* Run two SCLK cycles between writing and reading*/
  881. SetSDATAHiZ(); /* Set to HiZ so Target can drive SDATA*/
  882. bTargetDataIN = bReceiveByte();
  883. RunClock(1);
  884. SendVector(read_byte_v + 1, 1); /* Send the ReadByte Vector End*/
  885. /*Test the Byte that was read from the Target against the original
  886. value (already in the 128-Byte array "abTargetDataOUT[]"). If it
  887. matches, then bump the address & pointer,loop-back and continue.
  888. If it does NOT match abort the loop and return and error.*/
  889. if (bTargetDataIN != abTargetDataOUT[bTargetDataPtr])
  890. return BLOCK_ERROR;
  891. /* Increment the address by two to accomodate 7-Bit addressing*/
  892. /* (puts the 7-bit address into MSBit locations for "SendByte()").*/
  893. bTargetDataPtr++;
  894. bTargetAddress += 2;
  895. }
  896. SendVector(tsync_disable, num_bits_tsync_disable);
  897. /*PTJ: 307, added for tsync testing*/
  898. return PASS;
  899. }
  900. #endif /*(PROJECT_REV_) */
  901. /* end of file ISSP_Routines.c */