1 /****************************************************************************
2 * SPI flash driver for M25P64
3 ****************************************************************************/
5 #include <linux/ctype.h>
7 #include <asm/mach-common/bits/spi.h>
9 #if defined(CONFIG_SPI)
11 /* Application definitions */
13 #define NUM_SECTORS 128 /* number of sectors */
14 #define SECTOR_SIZE 0x10000
17 #define COMMON_SPI_SETTINGS (SPE|MSTR|CPHA|CPOL) /* Settings to the SPI_CTL */
18 #define TIMOD01 (0x01) /* stes the SPI to work with core instructions */
21 #define SPI_WREN (0x06) /*Set Write Enable Latch */
22 #define SPI_WRDI (0x04) /*Reset Write Enable Latch */
23 #define SPI_RDSR (0x05) /*Read Status Register */
24 #define SPI_WRSR (0x01) /*Write Status Register */
25 #define SPI_READ (0x03) /*Read data from memory */
26 #define SPI_FAST_READ (0x0B) /*Read data from memory */
27 #define SPI_PP (0x02) /*Program Data into memory */
28 #define SPI_SE (0xD8) /*Erase one sector in memory */
29 #define SPI_BE (0xC7) /*Erase all memory */
30 #define WIP (0x1) /*Check the write in progress bit of the SPI status register */
31 #define WEL (0x2) /*Check the write enable bit of the SPI status register */
33 #define TIMEOUT 350000000
42 void spi_init_f(void);
43 void spi_init_r(void);
44 ssize_t spi_read(uchar *, int, uchar *, int);
45 ssize_t spi_write(uchar *, int, uchar *, int);
47 char ReadStatusRegister(void);
48 void Wait_For_SPIF(void);
49 void SetupSPI(const int spi_setting);
51 void SendSingleCommand(const int iCommand);
53 ERROR_CODE GetSectorNumber(unsigned long ulOffset, int *pnSector);
54 ERROR_CODE EraseBlock(int nBlock);
55 ERROR_CODE ReadData(unsigned long ulStart, long lCount, int *pnData);
56 ERROR_CODE WriteData(unsigned long ulStart, long lCount, int *pnData);
57 ERROR_CODE Wait_For_Status(char Statusbit);
58 ERROR_CODE Wait_For_WEL(void);
61 * Function: spi_init_f
62 * Description: Init SPI-Controller (ROM part)
70 * Function: spi_init_r
71 * Description: Init SPI-Controller (RAM part) -
72 * The malloc engine is ready and we can move our buffers to
84 ssize_t spi_write(uchar * addr, int alen, uchar * buffer, int len)
87 int start_block, end_block;
88 int start_byte, end_byte;
89 ERROR_CODE result = NO_ERR;
90 uchar temp[SECTOR_SIZE];
93 offset = addr[0] << 16 | addr[1] << 8 | addr[2];
94 /* Get the start block number */
95 result = GetSectorNumber(offset, &start_block);
96 if (result == INVALID_SECTOR) {
97 printf("Invalid sector! ");
100 /* Get the end block number */
101 result = GetSectorNumber(offset + len - 1, &end_block);
102 if (result == INVALID_SECTOR) {
103 printf("Invalid sector! ");
107 for (num = start_block; num <= end_block; num++) {
108 ReadData(num * SECTOR_SIZE, SECTOR_SIZE, (int *)temp);
109 start_byte = num * SECTOR_SIZE;
110 end_byte = (num + 1) * SECTOR_SIZE - 1;
111 if (start_byte < offset)
113 if (end_byte > (offset + len))
114 end_byte = (offset + len - 1);
115 for (i = start_byte; i <= end_byte; i++)
116 temp[i - num * SECTOR_SIZE] = buffer[i - offset];
118 result = WriteData(num * SECTOR_SIZE, SECTOR_SIZE, (int *)temp);
119 if (result != NO_ERR)
129 ssize_t spi_read(uchar * addr, int alen, uchar * buffer, int len)
131 unsigned long offset;
132 offset = addr[0] << 16 | addr[1] << 8 | addr[2];
133 ReadData(offset, len, (int *)buffer);
137 void SendSingleCommand(const int iCommand)
139 unsigned short dummy;
141 /* turns on the SPI in single write mode */
142 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
144 /* sends the actual command to the SPI TX register */
145 *pSPI_TDBR = iCommand;
148 /* The SPI status register will be polled to check the SPIF bit */
153 /* The SPI will be turned off */
158 void SetupSPI(const int spi_setting)
161 if (icache_status() || dcache_status())
162 udelay(CONFIG_CCLK_HZ / 50000000);
163 /*sets up the PF10 to be the slave select of the SPI */
164 *pPORTF_FER |= (PF10 | PF11 | PF12 | PF13);
166 *pSPI_BAUD = CONFIG_SPI_BAUD;
167 *pSPI_CTL = spi_setting;
177 *pSPI_CTL = 0x0400; /* disable SPI */
181 udelay(CONFIG_CCLK_HZ / 50000000);
185 void Wait_For_SPIF(void)
187 unsigned short dummyread;
188 while ((*pSPI_STAT & TXS)) ;
189 while (!(*pSPI_STAT & SPIF)) ;
190 while (!(*pSPI_STAT & RXS)) ;
191 /* Read dummy to empty the receive register */
192 dummyread = *pSPI_RDBR;
195 ERROR_CODE Wait_For_WEL(void)
198 char status_register = 0;
199 ERROR_CODE ErrorCode = NO_ERR;
201 for (i = 0; i < TIMEOUT; i++) {
202 status_register = ReadStatusRegister();
203 if ((status_register & WEL)) {
207 ErrorCode = POLL_TIMEOUT; /* Time out error */
213 ERROR_CODE Wait_For_Status(char Statusbit)
216 char status_register = 0xFF;
217 ERROR_CODE ErrorCode = NO_ERR;
219 for (i = 0; i < TIMEOUT; i++) {
220 status_register = ReadStatusRegister();
221 if (!(status_register & Statusbit)) {
225 ErrorCode = POLL_TIMEOUT; /* Time out error */
231 char ReadStatusRegister(void)
233 char status_register = 0;
235 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01)); /* Turn on the SPI */
237 *pSPI_TDBR = SPI_RDSR; /* send instruction to read status register */
239 Wait_For_SPIF(); /*wait until the instruction has been sent */
240 *pSPI_TDBR = 0; /*send dummy to receive the status register */
242 Wait_For_SPIF(); /*wait until the data has been sent */
243 status_register = *pSPI_RDBR; /*read the status register */
245 SPI_OFF(); /* Turn off the SPI */
247 return status_register;
250 ERROR_CODE GetSectorNumber(unsigned long ulOffset, int *pnSector)
253 ERROR_CODE ErrorCode = NO_ERR;
255 if (ulOffset > (NUM_SECTORS * 0x10000 - 1)) {
256 ErrorCode = INVALID_SECTOR;
260 nSector = (int)ulOffset / 0x10000;
266 ERROR_CODE EraseBlock(int nBlock)
268 unsigned long ulSectorOff = 0x0, ShiftValue;
269 ERROR_CODE ErrorCode = NO_ERR;
271 /* if the block is invalid just return */
272 if ((nBlock < 0) || (nBlock > NUM_SECTORS)) {
273 ErrorCode = INVALID_BLOCK;
276 /* figure out the offset of the block in flash */
277 if ((nBlock >= 0) && (nBlock < NUM_SECTORS)) {
278 ulSectorOff = (nBlock * SECTOR_SIZE);
281 ErrorCode = INVALID_BLOCK;
285 /* A write enable instruction must previously have been executed */
286 SendSingleCommand(SPI_WREN);
288 /* The status register will be polled to check the write enable latch "WREN" */
289 ErrorCode = Wait_For_WEL();
291 if (POLL_TIMEOUT == ErrorCode) {
292 printf("SPI Erase block error\n");
296 /* Turn on the SPI to send single commands */
297 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
300 * Send the erase block command to the flash followed by the 24 address
301 * to point to the start of a sector
306 /* Send the highest byte of the 24 bit address at first */
307 ShiftValue = (ulSectorOff >> 16);
308 *pSPI_TDBR = ShiftValue;
310 /* Wait until the instruction has been sent */
312 /* Send the middle byte of the 24 bit address at second */
313 ShiftValue = (ulSectorOff >> 8);
314 *pSPI_TDBR = ShiftValue;
316 /* Wait until the instruction has been sent */
318 /* Send the lowest byte of the 24 bit address finally */
319 *pSPI_TDBR = ulSectorOff;
321 /* Wait until the instruction has been sent */
324 /* Turns off the SPI */
327 /* Poll the status register to check the Write in Progress bit */
328 /* Sector erase takes time */
329 ErrorCode = Wait_For_Status(WIP);
331 /* block erase should be complete */
336 * ERROR_CODE ReadData()
337 * Read a value from flash for verify purpose
338 * Inputs: unsigned long ulStart - holds the SPI start address
339 * int pnData - pointer to store value read from flash
340 * long lCount - number of elements to read
342 ERROR_CODE ReadData(unsigned long ulStart, long lCount, int *pnData)
344 unsigned long ShiftValue;
348 /* Pointer cast to be able to increment byte wise */
350 cnData = (char *)pnData;
351 /* Start SPI interface */
352 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
354 #ifdef CONFIG_SPI_FLASH_FAST_READ
355 /* Send the read command to SPI device */
356 *pSPI_TDBR = SPI_FAST_READ;
358 /* Send the read command to SPI device */
359 *pSPI_TDBR = SPI_READ;
362 /* Wait until the instruction has been sent */
364 /* Send the highest byte of the 24 bit address at first */
365 ShiftValue = (ulStart >> 16);
366 /* Send the byte to the SPI device */
367 *pSPI_TDBR = ShiftValue;
369 /* Wait until the instruction has been sent */
371 /* Send the middle byte of the 24 bit address at second */
372 ShiftValue = (ulStart >> 8);
373 /* Send the byte to the SPI device */
374 *pSPI_TDBR = ShiftValue;
376 /* Wait until the instruction has been sent */
378 /* Send the lowest byte of the 24 bit address finally */
379 *pSPI_TDBR = ulStart;
381 /* Wait until the instruction has been sent */
384 #ifdef CONFIG_SPI_FLASH_FAST_READ
385 /* Send dummy for FAST_READ */
388 /* Wait until the instruction has been sent */
392 /* After the SPI device address has been placed on the MOSI pin the data can be */
393 /* received on the MISO pin. */
394 for (i = 0; i < lCount; i++) {
397 while (!(*pSPI_STAT & RXS)) ;
398 *cnData++ = *pSPI_RDBR;
400 if ((i >= SECTOR_SIZE) && (i % SECTOR_SIZE == 0))
404 /* Turn off the SPI */
410 ERROR_CODE WriteFlash(unsigned long ulStartAddr, long lTransferCount,
411 int *iDataSource, long *lWriteCount)
414 unsigned long ulWAddr;
415 long lWTransferCount = 0;
418 char *temp = (char *)iDataSource;
419 ERROR_CODE ErrorCode = NO_ERR;
421 /* First, a Write Enable Command must be sent to the SPI. */
422 SendSingleCommand(SPI_WREN);
425 * Second, the SPI Status Register will be tested whether the
426 * Write Enable Bit has been set
428 ErrorCode = Wait_For_WEL();
429 if (POLL_TIMEOUT == ErrorCode) {
430 printf("SPI Write Time Out\n");
433 /* Third, the 24 bit address will be shifted out
434 * the SPI MOSI bytewise.
437 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
440 /*wait until the instruction has been sent */
442 ulWAddr = (ulStartAddr >> 16);
443 *pSPI_TDBR = ulWAddr;
445 /*wait until the instruction has been sent */
447 ulWAddr = (ulStartAddr >> 8);
448 *pSPI_TDBR = ulWAddr;
450 /*wait until the instruction has been sent */
452 ulWAddr = ulStartAddr;
453 *pSPI_TDBR = ulWAddr;
455 /*wait until the instruction has been sent */
458 * Fourth, maximum number of 256 bytes will be taken from the Buffer
459 * and sent to the SPI device.
461 for (i = 0; (i < lTransferCount) && (i < 256); i++, lWTransferCount++) {
465 /*wait until the instruction has been sent */
470 /* Turns the SPI off */
474 * Sixth, the SPI Write in Progress Bit must be toggled to ensure the
475 * programming is done before start of next transfer
477 ErrorCode = Wait_For_Status(WIP);
479 if (POLL_TIMEOUT == ErrorCode) {
480 printf("SPI Program Time out!\n");
484 *lWriteCount = lWTransferCount;
489 ERROR_CODE WriteData(unsigned long ulStart, long lCount, int *pnData)
492 unsigned long ulWStart = ulStart;
493 long lWCount = lCount, lWriteCount;
494 long *pnWriteCount = &lWriteCount;
496 ERROR_CODE ErrorCode = NO_ERR;
498 while (lWCount != 0) {
499 ErrorCode = WriteFlash(ulWStart, lWCount, pnData, pnWriteCount);
502 * After each function call of WriteFlash the counter
505 lWCount -= *pnWriteCount;
507 /* Also, both address pointers must be recalculated. */
508 ulWStart += *pnWriteCount;
509 pnData += *pnWriteCount / 4;
512 /* return the appropriate error code */
516 #endif /* CONFIG_SPI */