spi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /****************************************************************************
  2. * SPI flash driver for M25P64
  3. ****************************************************************************/
  4. #include <common.h>
  5. #include <linux/ctype.h>
  6. #if defined(CONFIG_SPI)
  7. /*Application definitions */
  8. #define NUM_SECTORS 128 /* number of sectors */
  9. #define SECTOR_SIZE 0x10000
  10. #define NOP_NUM 1000
  11. #define COMMON_SPI_SETTINGS (SPE|MSTR|CPHA|CPOL) /*Settings to the SPI_CTL */
  12. #define TIMOD01 (0x01) /*stes the SPI to work with core instructions */
  13. /*Flash commands */
  14. #define SPI_WREN (0x06) /*Set Write Enable Latch */
  15. #define SPI_WRDI (0x04) /*Reset Write Enable Latch */
  16. #define SPI_RDSR (0x05) /*Read Status Register */
  17. #define SPI_WRSR (0x01) /*Write Status Register */
  18. #define SPI_READ (0x03) /*Read data from memory */
  19. #define SPI_PP (0x02) /*Program Data into memory */
  20. #define SPI_SE (0xD8) /*Erase one sector in memory */
  21. #define SPI_BE (0xC7) /*Erase all memory */
  22. #define WIP (0x1) /*Check the write in progress bit of the SPI status register */
  23. #define WEL (0x2) /*Check the write enable bit of the SPI status register */
  24. #define TIMEOUT 350000000
  25. typedef enum {
  26. NO_ERR,
  27. POLL_TIMEOUT,
  28. INVALID_SECTOR,
  29. INVALID_BLOCK,
  30. } ERROR_CODE;
  31. void spi_init_f(void);
  32. void spi_init_r(void);
  33. ssize_t spi_read(uchar *, int, uchar *, int);
  34. ssize_t spi_write(uchar *, int, uchar *, int);
  35. char ReadStatusRegister(void);
  36. void Wait_For_SPIF(void);
  37. void SetupSPI(const int spi_setting);
  38. void SPI_OFF(void);
  39. void SendSingleCommand(const int iCommand);
  40. ERROR_CODE GetSectorNumber(unsigned long ulOffset, int *pnSector);
  41. ERROR_CODE EraseBlock(int nBlock);
  42. ERROR_CODE ReadData(unsigned long ulStart, long lCount, int *pnData);
  43. ERROR_CODE WriteData(unsigned long ulStart, long lCount, int *pnData);
  44. ERROR_CODE Wait_For_Status(char Statusbit);
  45. ERROR_CODE Wait_For_WEL(void);
  46. /* -------------------
  47. * Variables
  48. * ------------------- */
  49. /* **************************************************************************
  50. *
  51. * Function: spi_init_f
  52. *
  53. * Description: Init SPI-Controller (ROM part)
  54. *
  55. * return: ---
  56. *
  57. * *********************************************************************** */
  58. void spi_init_f(void)
  59. {
  60. }
  61. /* **************************************************************************
  62. *
  63. * Function: spi_init_r
  64. *
  65. * Description: Init SPI-Controller (RAM part) -
  66. * The malloc engine is ready and we can move our buffers to
  67. * normal RAM
  68. *
  69. * return: ---
  70. *
  71. * *********************************************************************** */
  72. void spi_init_r(void)
  73. {
  74. return;
  75. }
  76. /****************************************************************************
  77. * Function: spi_write
  78. **************************************************************************** */
  79. ssize_t spi_write(uchar * addr, int alen, uchar * buffer, int len)
  80. {
  81. unsigned long offset;
  82. int start_block, end_block;
  83. int start_byte, end_byte;
  84. ERROR_CODE result = NO_ERR;
  85. uchar temp[SECTOR_SIZE];
  86. int i, num;
  87. offset = addr[0] << 16 | addr[1] << 8 | addr[2];
  88. /* Get the start block number */
  89. result = GetSectorNumber(offset, &start_block);
  90. if (result == INVALID_SECTOR) {
  91. printf("Invalid sector! ");
  92. return 0;
  93. }
  94. /* Get the end block number */
  95. result = GetSectorNumber(offset + len - 1, &end_block);
  96. if (result == INVALID_SECTOR) {
  97. printf("Invalid sector! ");
  98. return 0;
  99. }
  100. for (num = start_block; num <= end_block; num++) {
  101. ReadData(num * SECTOR_SIZE, SECTOR_SIZE, (int *)temp);
  102. start_byte = num * SECTOR_SIZE;
  103. end_byte = (num + 1) * SECTOR_SIZE - 1;
  104. if (start_byte < offset)
  105. start_byte = offset;
  106. if (end_byte > (offset + len))
  107. end_byte = (offset + len - 1);
  108. for (i = start_byte; i <= end_byte; i++)
  109. temp[i - num * SECTOR_SIZE] = buffer[i - offset];
  110. EraseBlock(num);
  111. result = WriteData(num * SECTOR_SIZE, SECTOR_SIZE, (int *)temp);
  112. if (result != NO_ERR)
  113. return 0;
  114. printf(".");
  115. }
  116. return len;
  117. }
  118. /****************************************************************************
  119. * Function: spi_read
  120. **************************************************************************** */
  121. ssize_t spi_read(uchar * addr, int alen, uchar * buffer, int len)
  122. {
  123. unsigned long offset;
  124. offset = addr[0] << 16 | addr[1] << 8 | addr[2];
  125. ReadData(offset, len, (int *)buffer);
  126. return len;
  127. }
  128. void SendSingleCommand(const int iCommand)
  129. {
  130. unsigned short dummy;
  131. /*turns on the SPI in single write mode */
  132. SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
  133. /*sends the actual command to the SPI TX register */
  134. *pSPI_TDBR = iCommand;
  135. __builtin_bfin_ssync();
  136. /*The SPI status register will be polled to check the SPIF bit */
  137. Wait_For_SPIF();
  138. dummy = *pSPI_RDBR;
  139. /*The SPI will be turned off */
  140. SPI_OFF();
  141. }
  142. void SetupSPI(const int spi_setting)
  143. {
  144. if (icache_status() || dcache_status())
  145. udelay(CONFIG_CCLK_HZ / 50000000);
  146. /*sets up the PF2 to be the slave select of the SPI */
  147. *pSPI_FLG = 0xFB04;
  148. *pSPI_BAUD = CONFIG_SPI_BAUD;
  149. *pSPI_CTL = spi_setting;
  150. __builtin_bfin_ssync();
  151. }
  152. void SPI_OFF(void)
  153. {
  154. *pSPI_CTL = 0x0400; /* disable SPI */
  155. *pSPI_FLG = 0;
  156. *pSPI_BAUD = 0;
  157. __builtin_bfin_ssync();
  158. udelay(CONFIG_CCLK_HZ / 50000000);
  159. }
  160. void Wait_For_SPIF(void)
  161. {
  162. unsigned short dummyread;
  163. while ((*pSPI_STAT & TXS)) ;
  164. while (!(*pSPI_STAT & SPIF)) ;
  165. while (!(*pSPI_STAT & RXS)) ;
  166. dummyread = *pSPI_RDBR; /* Read dummy to empty the receive register */
  167. }
  168. ERROR_CODE Wait_For_WEL(void)
  169. {
  170. int i;
  171. char status_register = 0;
  172. ERROR_CODE ErrorCode = NO_ERR; /* tells us if there was an error erasing flash */
  173. for (i = 0; i < TIMEOUT; i++) {
  174. status_register = ReadStatusRegister();
  175. if ((status_register & WEL)) {
  176. ErrorCode = NO_ERR; /* tells us if there was an error erasing flash */
  177. break;
  178. }
  179. ErrorCode = POLL_TIMEOUT; /* Time out error */
  180. };
  181. return ErrorCode;
  182. }
  183. ERROR_CODE Wait_For_Status(char Statusbit)
  184. {
  185. int i;
  186. char status_register = 0xFF;
  187. ERROR_CODE ErrorCode = NO_ERR; /* tells us if there was an error erasing flash */
  188. for (i = 0; i < TIMEOUT; i++) {
  189. status_register = ReadStatusRegister();
  190. if (!(status_register & Statusbit)) {
  191. ErrorCode = NO_ERR; /* tells us if there was an error erasing flash */
  192. break;
  193. }
  194. ErrorCode = POLL_TIMEOUT; /* Time out error */
  195. };
  196. return ErrorCode;
  197. }
  198. char ReadStatusRegister(void)
  199. {
  200. char status_register = 0;
  201. SetupSPI((COMMON_SPI_SETTINGS | TIMOD01)); /* Turn on the SPI */
  202. *pSPI_TDBR = SPI_RDSR; /* send instruction to read status register */
  203. __builtin_bfin_ssync();
  204. Wait_For_SPIF(); /*wait until the instruction has been sent */
  205. *pSPI_TDBR = 0; /*send dummy to receive the status register */
  206. __builtin_bfin_ssync();
  207. Wait_For_SPIF(); /*wait until the data has been sent */
  208. status_register = *pSPI_RDBR; /*read the status register */
  209. SPI_OFF(); /* Turn off the SPI */
  210. return status_register;
  211. }
  212. ERROR_CODE GetSectorNumber(unsigned long ulOffset, int *pnSector)
  213. {
  214. int nSector = 0;
  215. ERROR_CODE ErrorCode = NO_ERR;
  216. if (ulOffset > (NUM_SECTORS * 0x10000 - 1)) {
  217. ErrorCode = INVALID_SECTOR;
  218. return ErrorCode;
  219. }
  220. nSector = (int)ulOffset / 0x10000;
  221. *pnSector = nSector;
  222. /* ok */
  223. return ErrorCode;
  224. }
  225. ERROR_CODE EraseBlock(int nBlock)
  226. {
  227. unsigned long ulSectorOff = 0x0, ShiftValue;
  228. ERROR_CODE ErrorCode = NO_ERR;
  229. /* if the block is invalid just return */
  230. if ((nBlock < 0) || (nBlock > NUM_SECTORS)) {
  231. ErrorCode = INVALID_BLOCK; /* tells us if there was an error erasing flash */
  232. return ErrorCode;
  233. }
  234. /* figure out the offset of the block in flash */
  235. if ((nBlock >= 0) && (nBlock < NUM_SECTORS)) {
  236. ulSectorOff = (nBlock * SECTOR_SIZE);
  237. } else {
  238. ErrorCode = INVALID_BLOCK; /* tells us if there was an error erasing flash */
  239. return ErrorCode;
  240. }
  241. /* A write enable instruction must previously have been executed */
  242. SendSingleCommand(SPI_WREN);
  243. /*The status register will be polled to check the write enable latch "WREN" */
  244. ErrorCode = Wait_For_WEL();
  245. if (POLL_TIMEOUT == ErrorCode) {
  246. printf("SPI Erase block error\n");
  247. return ErrorCode;
  248. } else
  249. /*Turn on the SPI to send single commands */
  250. SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
  251. /* Send the erase block command to the flash followed by the 24 address */
  252. /* to point to the start of a sector. */
  253. *pSPI_TDBR = SPI_SE;
  254. __builtin_bfin_ssync();
  255. Wait_For_SPIF();
  256. ShiftValue = (ulSectorOff >> 16); /* Send the highest byte of the 24 bit address at first */
  257. *pSPI_TDBR = ShiftValue;
  258. __builtin_bfin_ssync();
  259. Wait_For_SPIF(); /* Wait until the instruction has been sent */
  260. ShiftValue = (ulSectorOff >> 8); /* Send the middle byte of the 24 bit address at second */
  261. *pSPI_TDBR = ShiftValue;
  262. __builtin_bfin_ssync();
  263. Wait_For_SPIF(); /* Wait until the instruction has been sent */
  264. *pSPI_TDBR = ulSectorOff; /* Send the lowest byte of the 24 bit address finally */
  265. __builtin_bfin_ssync();
  266. Wait_For_SPIF(); /* Wait until the instruction has been sent */
  267. /*Turns off the SPI */
  268. SPI_OFF();
  269. /* Poll the status register to check the Write in Progress bit */
  270. /* Sector erase takes time */
  271. ErrorCode = Wait_For_Status(WIP);
  272. /* block erase should be complete */
  273. return ErrorCode;
  274. }
  275. /*****************************************************************************
  276. * ERROR_CODE ReadData()
  277. *
  278. * Read a value from flash for verify purpose
  279. *
  280. * Inputs: unsigned long ulStart - holds the SPI start address
  281. * int pnData - pointer to store value read from flash
  282. * long lCount - number of elements to read
  283. ***************************************************************************** */
  284. ERROR_CODE ReadData(unsigned long ulStart, long lCount, int *pnData)
  285. {
  286. unsigned long ShiftValue;
  287. char *cnData;
  288. int i;
  289. cnData = (char *)pnData; /* Pointer cast to be able to increment byte wise */
  290. /* Start SPI interface */
  291. SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
  292. *pSPI_TDBR = SPI_READ; /* Send the read command to SPI device */
  293. __builtin_bfin_ssync();
  294. Wait_For_SPIF(); /* Wait until the instruction has been sent */
  295. ShiftValue = (ulStart >> 16); /* Send the highest byte of the 24 bit address at first */
  296. *pSPI_TDBR = ShiftValue; /* Send the byte to the SPI device */
  297. __builtin_bfin_ssync();
  298. Wait_For_SPIF(); /* Wait until the instruction has been sent */
  299. ShiftValue = (ulStart >> 8); /* Send the middle byte of the 24 bit address at second */
  300. *pSPI_TDBR = ShiftValue; /* Send the byte to the SPI device */
  301. __builtin_bfin_ssync();
  302. Wait_For_SPIF(); /* Wait until the instruction has been sent */
  303. *pSPI_TDBR = ulStart; /* Send the lowest byte of the 24 bit address finally */
  304. __builtin_bfin_ssync();
  305. Wait_For_SPIF(); /* Wait until the instruction has been sent */
  306. /* After the SPI device address has been placed on the MOSI pin the data can be */
  307. /* received on the MISO pin. */
  308. for (i = 0; i < lCount; i++) {
  309. *pSPI_TDBR = 0; /*send dummy */
  310. __builtin_bfin_ssync();
  311. while (!(*pSPI_STAT & RXS)) ;
  312. *cnData++ = *pSPI_RDBR; /*read */
  313. if ((i >= SECTOR_SIZE) && (i % SECTOR_SIZE == 0))
  314. printf(".");
  315. }
  316. SPI_OFF(); /* Turn off the SPI */
  317. return NO_ERR;
  318. }
  319. ERROR_CODE WriteFlash(unsigned long ulStartAddr, long lTransferCount,
  320. int *iDataSource, long *lWriteCount)
  321. {
  322. unsigned long ulWAddr;
  323. long lWTransferCount = 0;
  324. int i;
  325. char iData;
  326. char *temp = (char *)iDataSource;
  327. ERROR_CODE ErrorCode = NO_ERR; /* tells us if there was an error erasing flash */
  328. /* First, a Write Enable Command must be sent to the SPI. */
  329. SendSingleCommand(SPI_WREN);
  330. /* Second, the SPI Status Register will be tested whether the */
  331. /* Write Enable Bit has been set. */
  332. ErrorCode = Wait_For_WEL();
  333. if (POLL_TIMEOUT == ErrorCode) {
  334. printf("SPI Write Time Out\n");
  335. return ErrorCode;
  336. } else
  337. /* Third, the 24 bit address will be shifted out the SPI MOSI bytewise. */
  338. SetupSPI((COMMON_SPI_SETTINGS | TIMOD01)); /* Turns the SPI on */
  339. *pSPI_TDBR = SPI_PP;
  340. __builtin_bfin_ssync();
  341. Wait_For_SPIF(); /*wait until the instruction has been sent */
  342. ulWAddr = (ulStartAddr >> 16);
  343. *pSPI_TDBR = ulWAddr;
  344. __builtin_bfin_ssync();
  345. Wait_For_SPIF(); /*wait until the instruction has been sent */
  346. ulWAddr = (ulStartAddr >> 8);
  347. *pSPI_TDBR = ulWAddr;
  348. __builtin_bfin_ssync();
  349. Wait_For_SPIF(); /*wait until the instruction has been sent */
  350. ulWAddr = ulStartAddr;
  351. *pSPI_TDBR = ulWAddr;
  352. __builtin_bfin_ssync();
  353. Wait_For_SPIF(); /*wait until the instruction has been sent */
  354. /* Fourth, maximum number of 256 bytes will be taken from the Buffer */
  355. /* and sent to the SPI device. */
  356. for (i = 0; (i < lTransferCount) && (i < 256); i++, lWTransferCount++) {
  357. iData = *temp;
  358. *pSPI_TDBR = iData;
  359. __builtin_bfin_ssync();
  360. Wait_For_SPIF(); /*wait until the instruction has been sent */
  361. temp++;
  362. }
  363. SPI_OFF(); /* Turns the SPI off */
  364. /* Sixth, the SPI Write in Progress Bit must be toggled to ensure the */
  365. /* programming is done before start of next transfer. */
  366. ErrorCode = Wait_For_Status(WIP);
  367. if (POLL_TIMEOUT == ErrorCode) {
  368. printf("SPI Program Time out!\n");
  369. return ErrorCode;
  370. } else
  371. *lWriteCount = lWTransferCount;
  372. return ErrorCode;
  373. }
  374. ERROR_CODE WriteData(unsigned long ulStart, long lCount, int *pnData)
  375. {
  376. unsigned long ulWStart = ulStart;
  377. long lWCount = lCount, lWriteCount;
  378. long *pnWriteCount = &lWriteCount;
  379. ERROR_CODE ErrorCode = NO_ERR;
  380. while (lWCount != 0) {
  381. ErrorCode = WriteFlash(ulWStart, lWCount, pnData, pnWriteCount);
  382. /* After each function call of WriteFlash the counter must be adjusted */
  383. lWCount -= *pnWriteCount;
  384. /* Also, both address pointers must be recalculated. */
  385. ulWStart += *pnWriteCount;
  386. pnData += *pnWriteCount / 4;
  387. }
  388. /* return the appropriate error code */
  389. return ErrorCode;
  390. }
  391. #endif /* CONFIG_SPI */