1 /****************************************************************************
2 * SPI flash driver for M25P64
3 ****************************************************************************/
5 #include <linux/ctype.h>
8 #if defined(CONFIG_SPI)
10 /* Application definitions */
12 #define NUM_SECTORS 128 /* number of sectors */
13 #define SECTOR_SIZE 0x10000
16 #define COMMON_SPI_SETTINGS (SPE|MSTR|CPHA|CPOL) /* Settings to the SPI_CTL */
17 #define TIMOD01 (0x01) /* stes the SPI to work with core instructions */
20 #define SPI_WREN (0x06) /*Set Write Enable Latch */
21 #define SPI_WRDI (0x04) /*Reset Write Enable Latch */
22 #define SPI_RDSR (0x05) /*Read Status Register */
23 #define SPI_WRSR (0x01) /*Write Status Register */
24 #define SPI_READ (0x03) /*Read data from memory */
25 #define SPI_FAST_READ (0x0B) /*Read data from memory */
26 #define SPI_PP (0x02) /*Program Data into memory */
27 #define SPI_SE (0xD8) /*Erase one sector in memory */
28 #define SPI_BE (0xC7) /*Erase all memory */
29 #define WIP (0x1) /*Check the write in progress bit of the SPI status register */
30 #define WEL (0x2) /*Check the write enable bit of the SPI status register */
32 #define TIMEOUT 350000000
41 void spi_init_f(void);
42 void spi_init_r(void);
43 ssize_t spi_read(uchar *, int, uchar *, int);
44 ssize_t spi_write(uchar *, int, uchar *, int);
46 char ReadStatusRegister(void);
47 void Wait_For_SPIF(void);
48 void SetupSPI(const int spi_setting);
50 void SendSingleCommand(const int iCommand);
52 ERROR_CODE GetSectorNumber(unsigned long ulOffset, int *pnSector);
53 ERROR_CODE EraseBlock(int nBlock);
54 ERROR_CODE ReadData(unsigned long ulStart, long lCount, int *pnData);
55 ERROR_CODE WriteData(unsigned long ulStart, long lCount, int *pnData);
56 ERROR_CODE Wait_For_Status(char Statusbit);
57 ERROR_CODE Wait_For_WEL(void);
60 * Function: spi_init_f
61 * Description: Init SPI-Controller (ROM part)
69 * Function: spi_init_r
70 * Description: Init SPI-Controller (RAM part) -
71 * The malloc engine is ready and we can move our buffers to
83 ssize_t spi_write(uchar * addr, int alen, uchar * buffer, int len)
86 int start_block, end_block;
87 int start_byte, end_byte;
88 ERROR_CODE result = NO_ERR;
89 uchar temp[SECTOR_SIZE];
92 offset = addr[0] << 16 | addr[1] << 8 | addr[2];
93 /* Get the start block number */
94 result = GetSectorNumber(offset, &start_block);
95 if (result == INVALID_SECTOR) {
96 printf("Invalid sector! ");
99 /* Get the end block number */
100 result = GetSectorNumber(offset + len - 1, &end_block);
101 if (result == INVALID_SECTOR) {
102 printf("Invalid sector! ");
106 for (num = start_block; num <= end_block; num++) {
107 ReadData(num * SECTOR_SIZE, SECTOR_SIZE, (int *)temp);
108 start_byte = num * SECTOR_SIZE;
109 end_byte = (num + 1) * SECTOR_SIZE - 1;
110 if (start_byte < offset)
112 if (end_byte > (offset + len))
113 end_byte = (offset + len - 1);
114 for (i = start_byte; i <= end_byte; i++)
115 temp[i - num * SECTOR_SIZE] = buffer[i - offset];
117 result = WriteData(num * SECTOR_SIZE, SECTOR_SIZE, (int *)temp);
118 if (result != NO_ERR)
128 ssize_t spi_read(uchar * addr, int alen, uchar * buffer, int len)
130 unsigned long offset;
131 offset = addr[0] << 16 | addr[1] << 8 | addr[2];
132 ReadData(offset, len, (int *)buffer);
136 void SendSingleCommand(const int iCommand)
138 unsigned short dummy;
140 /* turns on the SPI in single write mode */
141 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
143 /* sends the actual command to the SPI TX register */
144 *pSPI_TDBR = iCommand;
147 /* The SPI status register will be polled to check the SPIF bit */
152 /* The SPI will be turned off */
157 void SetupSPI(const int spi_setting)
160 if (icache_status() || dcache_status())
161 udelay(CONFIG_CCLK_HZ / 50000000);
162 /*sets up the PF10 to be the slave select of the SPI */
163 *pPORTF_FER |= (PF10 | PF11 | PF12 | PF13);
165 *pSPI_BAUD = CONFIG_SPI_BAUD;
166 *pSPI_CTL = spi_setting;
176 *pSPI_CTL = 0x0400; /* disable SPI */
180 udelay(CONFIG_CCLK_HZ / 50000000);
184 void Wait_For_SPIF(void)
186 unsigned short dummyread;
187 while ((*pSPI_STAT & TXS)) ;
188 while (!(*pSPI_STAT & SPIF)) ;
189 while (!(*pSPI_STAT & RXS)) ;
190 /* Read dummy to empty the receive register */
191 dummyread = *pSPI_RDBR;
194 ERROR_CODE Wait_For_WEL(void)
197 char status_register = 0;
198 ERROR_CODE ErrorCode = NO_ERR;
200 for (i = 0; i < TIMEOUT; i++) {
201 status_register = ReadStatusRegister();
202 if ((status_register & WEL)) {
206 ErrorCode = POLL_TIMEOUT; /* Time out error */
212 ERROR_CODE Wait_For_Status(char Statusbit)
215 char status_register = 0xFF;
216 ERROR_CODE ErrorCode = NO_ERR;
218 for (i = 0; i < TIMEOUT; i++) {
219 status_register = ReadStatusRegister();
220 if (!(status_register & Statusbit)) {
224 ErrorCode = POLL_TIMEOUT; /* Time out error */
230 char ReadStatusRegister(void)
232 char status_register = 0;
234 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01)); /* Turn on the SPI */
236 *pSPI_TDBR = SPI_RDSR; /* send instruction to read status register */
238 Wait_For_SPIF(); /*wait until the instruction has been sent */
239 *pSPI_TDBR = 0; /*send dummy to receive the status register */
241 Wait_For_SPIF(); /*wait until the data has been sent */
242 status_register = *pSPI_RDBR; /*read the status register */
244 SPI_OFF(); /* Turn off the SPI */
246 return status_register;
249 ERROR_CODE GetSectorNumber(unsigned long ulOffset, int *pnSector)
252 ERROR_CODE ErrorCode = NO_ERR;
254 if (ulOffset > (NUM_SECTORS * 0x10000 - 1)) {
255 ErrorCode = INVALID_SECTOR;
259 nSector = (int)ulOffset / 0x10000;
265 ERROR_CODE EraseBlock(int nBlock)
267 unsigned long ulSectorOff = 0x0, ShiftValue;
268 ERROR_CODE ErrorCode = NO_ERR;
270 /* if the block is invalid just return */
271 if ((nBlock < 0) || (nBlock > NUM_SECTORS)) {
272 ErrorCode = INVALID_BLOCK;
275 /* figure out the offset of the block in flash */
276 if ((nBlock >= 0) && (nBlock < NUM_SECTORS)) {
277 ulSectorOff = (nBlock * SECTOR_SIZE);
280 ErrorCode = INVALID_BLOCK;
284 /* A write enable instruction must previously have been executed */
285 SendSingleCommand(SPI_WREN);
287 /* The status register will be polled to check the write enable latch "WREN" */
288 ErrorCode = Wait_For_WEL();
290 if (POLL_TIMEOUT == ErrorCode) {
291 printf("SPI Erase block error\n");
295 /* Turn on the SPI to send single commands */
296 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
299 * Send the erase block command to the flash followed by the 24 address
300 * to point to the start of a sector
305 /* Send the highest byte of the 24 bit address at first */
306 ShiftValue = (ulSectorOff >> 16);
307 *pSPI_TDBR = ShiftValue;
309 /* Wait until the instruction has been sent */
311 /* Send the middle byte of the 24 bit address at second */
312 ShiftValue = (ulSectorOff >> 8);
313 *pSPI_TDBR = ShiftValue;
315 /* Wait until the instruction has been sent */
317 /* Send the lowest byte of the 24 bit address finally */
318 *pSPI_TDBR = ulSectorOff;
320 /* Wait until the instruction has been sent */
323 /* Turns off the SPI */
326 /* Poll the status register to check the Write in Progress bit */
327 /* Sector erase takes time */
328 ErrorCode = Wait_For_Status(WIP);
330 /* block erase should be complete */
335 * ERROR_CODE ReadData()
336 * Read a value from flash for verify purpose
337 * Inputs: unsigned long ulStart - holds the SPI start address
338 * int pnData - pointer to store value read from flash
339 * long lCount - number of elements to read
341 ERROR_CODE ReadData(unsigned long ulStart, long lCount, int *pnData)
343 unsigned long ShiftValue;
347 /* Pointer cast to be able to increment byte wise */
349 cnData = (char *)pnData;
350 /* Start SPI interface */
351 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
353 #ifdef CONFIG_SPI_FLASH_FAST_READ
354 /* Send the read command to SPI device */
355 *pSPI_TDBR = SPI_FAST_READ;
357 /* Send the read command to SPI device */
358 *pSPI_TDBR = SPI_READ;
361 /* Wait until the instruction has been sent */
363 /* Send the highest byte of the 24 bit address at first */
364 ShiftValue = (ulStart >> 16);
365 /* Send the byte to the SPI device */
366 *pSPI_TDBR = ShiftValue;
368 /* Wait until the instruction has been sent */
370 /* Send the middle byte of the 24 bit address at second */
371 ShiftValue = (ulStart >> 8);
372 /* Send the byte to the SPI device */
373 *pSPI_TDBR = ShiftValue;
375 /* Wait until the instruction has been sent */
377 /* Send the lowest byte of the 24 bit address finally */
378 *pSPI_TDBR = ulStart;
380 /* Wait until the instruction has been sent */
383 #ifdef CONFIG_SPI_FLASH_FAST_READ
384 /* Send dummy for FAST_READ */
387 /* Wait until the instruction has been sent */
391 /* After the SPI device address has been placed on the MOSI pin the data can be */
392 /* received on the MISO pin. */
393 for (i = 0; i < lCount; i++) {
396 while (!(*pSPI_STAT & RXS)) ;
397 *cnData++ = *pSPI_RDBR;
399 if ((i >= SECTOR_SIZE) && (i % SECTOR_SIZE == 0))
403 /* Turn off the SPI */
409 ERROR_CODE WriteFlash(unsigned long ulStartAddr, long lTransferCount,
410 int *iDataSource, long *lWriteCount)
413 unsigned long ulWAddr;
414 long lWTransferCount = 0;
417 char *temp = (char *)iDataSource;
418 ERROR_CODE ErrorCode = NO_ERR;
420 /* First, a Write Enable Command must be sent to the SPI. */
421 SendSingleCommand(SPI_WREN);
424 * Second, the SPI Status Register will be tested whether the
425 * Write Enable Bit has been set
427 ErrorCode = Wait_For_WEL();
428 if (POLL_TIMEOUT == ErrorCode) {
429 printf("SPI Write Time Out\n");
432 /* Third, the 24 bit address will be shifted out
433 * the SPI MOSI bytewise.
436 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
439 /*wait until the instruction has been sent */
441 ulWAddr = (ulStartAddr >> 16);
442 *pSPI_TDBR = ulWAddr;
444 /*wait until the instruction has been sent */
446 ulWAddr = (ulStartAddr >> 8);
447 *pSPI_TDBR = ulWAddr;
449 /*wait until the instruction has been sent */
451 ulWAddr = ulStartAddr;
452 *pSPI_TDBR = ulWAddr;
454 /*wait until the instruction has been sent */
457 * Fourth, maximum number of 256 bytes will be taken from the Buffer
458 * and sent to the SPI device.
460 for (i = 0; (i < lTransferCount) && (i < 256); i++, lWTransferCount++) {
464 /*wait until the instruction has been sent */
469 /* Turns the SPI off */
473 * Sixth, the SPI Write in Progress Bit must be toggled to ensure the
474 * programming is done before start of next transfer
476 ErrorCode = Wait_For_Status(WIP);
478 if (POLL_TIMEOUT == ErrorCode) {
479 printf("SPI Program Time out!\n");
483 *lWriteCount = lWTransferCount;
488 ERROR_CODE WriteData(unsigned long ulStart, long lCount, int *pnData)
491 unsigned long ulWStart = ulStart;
492 long lWCount = lCount, lWriteCount;
493 long *pnWriteCount = &lWriteCount;
495 ERROR_CODE ErrorCode = NO_ERR;
497 while (lWCount != 0) {
498 ErrorCode = WriteFlash(ulWStart, lWCount, pnData, pnWriteCount);
501 * After each function call of WriteFlash the counter
504 lWCount -= *pnWriteCount;
506 /* Also, both address pointers must be recalculated. */
507 ulWStart += *pnWriteCount;
508 pnData += *pnWriteCount / 4;
511 /* return the appropriate error code */
515 #endif /* CONFIG_SPI */