stm_m25p64.c 12 KB

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