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_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);
59 /* -------------------
61 * ------------------- */
63 /* **************************************************************************
65 * Function: spi_init_f
67 * Description: Init SPI-Controller (ROM part)
71 * *********************************************************************** */
76 /* **************************************************************************
78 * Function: spi_init_r
80 * Description: Init SPI-Controller (RAM part) -
81 * The malloc engine is ready and we can move our buffers to
86 * *********************************************************************** */
92 /****************************************************************************
94 **************************************************************************** */
95 ssize_t spi_write(uchar * addr, int alen, uchar * buffer, int len)
98 int start_block, end_block;
99 int start_byte, end_byte;
100 ERROR_CODE result = NO_ERR;
101 uchar temp[SECTOR_SIZE];
104 offset = addr[0] << 16 | addr[1] << 8 | addr[2];
105 /* Get the start block number */
106 result = GetSectorNumber(offset, &start_block);
107 if (result == INVALID_SECTOR) {
108 printf("Invalid sector! ");
111 /* Get the end block number */
112 result = GetSectorNumber(offset + len - 1, &end_block);
113 if (result == INVALID_SECTOR) {
114 printf("Invalid sector! ");
118 for (num = start_block; num <= end_block; num++) {
119 ReadData(num * SECTOR_SIZE, SECTOR_SIZE, (int *)temp);
120 start_byte = num * SECTOR_SIZE;
121 end_byte = (num + 1) * SECTOR_SIZE - 1;
122 if (start_byte < offset)
124 if (end_byte > (offset + len))
125 end_byte = (offset + len - 1);
126 for (i = start_byte; i <= end_byte; i++)
127 temp[i - num * SECTOR_SIZE] = buffer[i - offset];
129 result = WriteData(num * SECTOR_SIZE, SECTOR_SIZE, (int *)temp);
130 if (result != NO_ERR)
137 /****************************************************************************
139 **************************************************************************** */
140 ssize_t spi_read(uchar * addr, int alen, uchar * buffer, int len)
142 unsigned long offset;
143 offset = addr[0] << 16 | addr[1] << 8 | addr[2];
144 ReadData(offset, len, (int *)buffer);
148 void SendSingleCommand(const int iCommand)
150 unsigned short dummy;
152 /*turns on the SPI in single write mode */
153 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
155 /*sends the actual command to the SPI TX register */
156 *pSPI_TDBR = iCommand;
159 /*The SPI status register will be polled to check the SPIF bit */
164 /*The SPI will be turned off */
169 void SetupSPI(const int spi_setting)
172 if (icache_status() || dcache_status())
173 udelay(CONFIG_CCLK_HZ / 50000000);
174 /*sets up the PF2 to be the slave select of the SPI */
176 *pSPI_BAUD = CONFIG_SPI_BAUD;
177 *pSPI_CTL = spi_setting;
184 *pSPI_CTL = 0x0400; /* disable SPI */
188 udelay(CONFIG_CCLK_HZ / 50000000);
192 void Wait_For_SPIF(void)
194 unsigned short dummyread;
195 while ((*pSPI_STAT & TXS)) ;
196 while (!(*pSPI_STAT & SPIF)) ;
197 while (!(*pSPI_STAT & RXS)) ;
198 dummyread = *pSPI_RDBR; /* Read dummy to empty the receive register */
202 ERROR_CODE Wait_For_WEL(void)
205 char status_register = 0;
206 ERROR_CODE ErrorCode = NO_ERR; /* tells us if there was an error erasing flash */
208 for (i = 0; i < TIMEOUT; i++) {
209 status_register = ReadStatusRegister();
210 if ((status_register & WEL)) {
211 ErrorCode = NO_ERR; /* tells us if there was an error erasing flash */
214 ErrorCode = POLL_TIMEOUT; /* Time out error */
220 ERROR_CODE Wait_For_Status(char Statusbit)
223 char status_register = 0xFF;
224 ERROR_CODE ErrorCode = NO_ERR; /* tells us if there was an error erasing flash */
226 for (i = 0; i < TIMEOUT; i++) {
227 status_register = ReadStatusRegister();
228 if (!(status_register & Statusbit)) {
229 ErrorCode = NO_ERR; /* tells us if there was an error erasing flash */
232 ErrorCode = POLL_TIMEOUT; /* Time out error */
238 char ReadStatusRegister(void)
240 char status_register = 0;
242 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01)); /* Turn on the SPI */
244 *pSPI_TDBR = SPI_RDSR; /* send instruction to read status register */
246 Wait_For_SPIF(); /*wait until the instruction has been sent */
247 *pSPI_TDBR = 0; /*send dummy to receive the status register */
249 Wait_For_SPIF(); /*wait until the data has been sent */
250 status_register = *pSPI_RDBR; /*read the status register */
252 SPI_OFF(); /* Turn off the SPI */
254 return status_register;
257 ERROR_CODE GetSectorNumber(unsigned long ulOffset, int *pnSector)
260 ERROR_CODE ErrorCode = NO_ERR;
262 if (ulOffset > (NUM_SECTORS * 0x10000 - 1)) {
263 ErrorCode = INVALID_SECTOR;
267 nSector = (int)ulOffset / 0x10000;
274 ERROR_CODE EraseBlock(int nBlock)
276 unsigned long ulSectorOff = 0x0, ShiftValue;
277 ERROR_CODE ErrorCode = NO_ERR;
279 /* if the block is invalid just return */
280 if ((nBlock < 0) || (nBlock > NUM_SECTORS)) {
281 ErrorCode = INVALID_BLOCK; /* tells us if there was an error erasing flash */
284 /* figure out the offset of the block in flash */
285 if ((nBlock >= 0) && (nBlock < NUM_SECTORS)) {
286 ulSectorOff = (nBlock * SECTOR_SIZE);
289 ErrorCode = INVALID_BLOCK; /* tells us if there was an error erasing flash */
293 /* A write enable instruction must previously have been executed */
294 SendSingleCommand(SPI_WREN);
296 /*The status register will be polled to check the write enable latch "WREN" */
297 ErrorCode = Wait_For_WEL();
299 if (POLL_TIMEOUT == ErrorCode) {
300 printf("SPI Erase block error\n");
303 /*Turn on the SPI to send single commands */
304 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
306 /* Send the erase block command to the flash followed by the 24 address */
307 /* to point to the start of a sector. */
311 ShiftValue = (ulSectorOff >> 16); /* Send the highest byte of the 24 bit address at first */
312 *pSPI_TDBR = ShiftValue;
314 Wait_For_SPIF(); /* Wait until the instruction has been sent */
315 ShiftValue = (ulSectorOff >> 8); /* Send the middle byte of the 24 bit address at second */
316 *pSPI_TDBR = ShiftValue;
318 Wait_For_SPIF(); /* Wait until the instruction has been sent */
319 *pSPI_TDBR = ulSectorOff; /* Send the lowest byte of the 24 bit address finally */
321 Wait_For_SPIF(); /* 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 */
334 /*****************************************************************************
335 * ERROR_CODE ReadData()
337 * Read a value from flash for verify purpose
339 * Inputs: unsigned long ulStart - holds the SPI start address
340 * int pnData - pointer to store value read from flash
341 * long lCount - number of elements to read
342 ***************************************************************************** */
343 ERROR_CODE ReadData(unsigned long ulStart, long lCount, int *pnData)
345 unsigned long ShiftValue;
349 cnData = (char *)pnData; /* Pointer cast to be able to increment byte wise */
351 /* Start SPI interface */
352 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));
354 *pSPI_TDBR = SPI_READ; /* Send the read command to SPI device */
356 Wait_For_SPIF(); /* Wait until the instruction has been sent */
357 ShiftValue = (ulStart >> 16); /* Send the highest byte of the 24 bit address at first */
358 *pSPI_TDBR = ShiftValue; /* Send the byte to the SPI device */
360 Wait_For_SPIF(); /* Wait until the instruction has been sent */
361 ShiftValue = (ulStart >> 8); /* Send the middle byte of the 24 bit address at second */
362 *pSPI_TDBR = ShiftValue; /* Send the byte to the SPI device */
364 Wait_For_SPIF(); /* Wait until the instruction has been sent */
365 *pSPI_TDBR = ulStart; /* Send the lowest byte of the 24 bit address finally */
367 Wait_For_SPIF(); /* Wait until the instruction has been sent */
369 /* After the SPI device address has been placed on the MOSI pin the data can be */
370 /* received on the MISO pin. */
371 for (i = 0; i < lCount; i++) {
372 *pSPI_TDBR = 0; /*send dummy */
374 while (!(*pSPI_STAT & RXS)) ;
375 *cnData++ = *pSPI_RDBR; /*read */
377 if ((i >= SECTOR_SIZE) && (i % SECTOR_SIZE == 0))
381 SPI_OFF(); /* Turn off the SPI */
386 ERROR_CODE WriteFlash(unsigned long ulStartAddr, long lTransferCount,
387 int *iDataSource, long *lWriteCount)
390 unsigned long ulWAddr;
391 long lWTransferCount = 0;
394 char *temp = (char *)iDataSource;
395 ERROR_CODE ErrorCode = NO_ERR; /* tells us if there was an error erasing flash */
397 /* First, a Write Enable Command must be sent to the SPI. */
398 SendSingleCommand(SPI_WREN);
400 /* Second, the SPI Status Register will be tested whether the */
401 /* Write Enable Bit has been set. */
402 ErrorCode = Wait_For_WEL();
403 if (POLL_TIMEOUT == ErrorCode) {
404 printf("SPI Write Time Out\n");
407 /* Third, the 24 bit address will be shifted out the SPI MOSI bytewise. */
408 SetupSPI((COMMON_SPI_SETTINGS | TIMOD01)); /* Turns the SPI on */
411 Wait_For_SPIF(); /*wait until the instruction has been sent */
412 ulWAddr = (ulStartAddr >> 16);
413 *pSPI_TDBR = ulWAddr;
415 Wait_For_SPIF(); /*wait until the instruction has been sent */
416 ulWAddr = (ulStartAddr >> 8);
417 *pSPI_TDBR = ulWAddr;
419 Wait_For_SPIF(); /*wait until the instruction has been sent */
420 ulWAddr = ulStartAddr;
421 *pSPI_TDBR = ulWAddr;
423 Wait_For_SPIF(); /*wait until the instruction has been sent */
424 /* Fourth, maximum number of 256 bytes will be taken from the Buffer */
425 /* and sent to the SPI device. */
426 for (i = 0; (i < lTransferCount) && (i < 256); i++, lWTransferCount++) {
430 Wait_For_SPIF(); /*wait until the instruction has been sent */
434 SPI_OFF(); /* Turns the SPI off */
436 /* Sixth, the SPI Write in Progress Bit must be toggled to ensure the */
437 /* programming is done before start of next transfer. */
438 ErrorCode = Wait_For_Status(WIP);
440 if (POLL_TIMEOUT == ErrorCode) {
441 printf("SPI Program Time out!\n");
445 *lWriteCount = lWTransferCount;
450 ERROR_CODE WriteData(unsigned long ulStart, long lCount, int *pnData)
453 unsigned long ulWStart = ulStart;
454 long lWCount = lCount, lWriteCount;
455 long *pnWriteCount = &lWriteCount;
457 ERROR_CODE ErrorCode = NO_ERR;
459 while (lWCount != 0) {
460 ErrorCode = WriteFlash(ulWStart, lWCount, pnData, pnWriteCount);
462 /* After each function call of WriteFlash the counter must be adjusted */
463 lWCount -= *pnWriteCount;
465 /* Also, both address pointers must be recalculated. */
466 ulWStart += *pnWriteCount;
467 pnData += *pnWriteCount / 4;
470 /* return the appropriate error code */
474 #endif /* CONFIG_SPI */