1 /************************************************************************/
3 /* SPI Flash Memory Drivers */
4 /* File name: spiflash.c */
5 /* Revision: 1.0 1/27/2004 */
7 /************************************************************************/
11 #include "lib_types.h"
12 #include "lib_printf.h"
13 #include "lib_string.h"
17 #include <linux/param.h>
18 #include <linux/sched.h>
19 #include <linux/timer.h>
20 #include <linux/kernel.h>
21 #include <bcm_map_part.h>
26 #include "flash_api.h"
30 #define OSL_DELAY(X) \
31 do { { int i; for( i = 0; i < (X) * 500; i++ ) ; } } while(0)
41 #define MAX_MEMORY_MAPPED_SIZE 0
43 #define MAXSECTORS 1024 /* maximum number of sectors supported */
45 #define FLASH_PAGE_SIZE 256
46 #define SECTOR_SIZE_4K (4 * 1024)
47 #define SECTOR_SIZE_32K (32 * 1024)
48 #define SECTOR_SIZE_64K (64 * 1024)
49 #define SST25VF020_SECTOR 64 /* 2 Mbit */
50 #define SST25VF040_SECTOR 128 /* 4 Mbit */
51 #define SST25VF080_SECTOR 256 /* 8 Mbit */
52 #define SST25VF016B_SECTOR 512 /* 16 Mbit */
53 #define SPAN25FL004A_SECTOR 8
54 #define SPAN25FL008A_SECTOR 16
55 #define SPAN25FL016A_SECTOR 32
56 #define SPAN25FL032A_SECTOR 64
57 #define AT25F512_SECTOR 2
58 #define AT25F2048_SECTOR 4
59 #define AMD25FL_SECTOR 4
61 /* use 60 instead 63 for aligned on word (4 bytes) boundaries */
66 /* Standard Boolean declarations */
70 #define SPI_STATUS_OK 0
71 #define SPI_STATUS_INVALID_LEN -1
73 /* Command codes for the flash_command routine */
74 #define FLASH_READ 0x03 /* read data from memory array */
75 #define FLASH_PROG 0x02 /* program data into memory array */
76 #define FLASH_WREN 0x06 /* set write enable latch */
77 #define FLASH_WRDI 0x04 /* reset write enable latch */
78 #define FLASH_RDSR 0x05 /* read status register */
79 #define FLASH_WRST 0x01 /* write status register */
80 #define FLASH_EWSR 0x50 /* enable write status */
81 #define FLASH_WORD_AAI 0xAD /* auto address increment word program */
82 #define FLASH_AAI 0xAF /* auto address increment program */
84 #define SST_FLASH_CERASE 0x60 /* erase all sectors in memory array */
85 #define SST_FLASH_SERASE 0x20 /* erase one sector in memroy array */
86 #define SST_FLASH_RDID 0x90 /* read manufacturer and product id */
88 #define ATMEL_FLASH_CERASE 0x62 /* erase all sectors in memory array */
89 #define ATMEL_FLASH_SERASE 0x52 /* erase one sector in memroy array */
90 #define ATMEL_FLASH_RDID 0x15 /* read manufacturer and product id */
92 #define AMD_FLASH_CERASE 0xC7 /* erase all sectors in memory array */
93 #define AMD_FLASH_SERASE 0xD8 /* erase one sector in memroy array */
94 #define AMD_FLASH_RDID 0xAB /* read manufacturer and product id */
96 #define SPAN_FLASH_CERASE 0xC7 /* erase all sectors in memory array */
97 #define SPAN_FLASH_SERASE 0xD8 /* erase one sector in memory array */
98 #define SPAN_FLASH_RDID 0x9F /* read manufacturer and product id */
100 #define ST_FLASH_RDID 0x9F /* read manufacturer and product id */
102 /* RDSR return status bit definition */
110 /* Return codes from flash_status */
111 #define STATUS_READY 0 /* ready for action */
112 #define STATUS_BUSY 1 /* operation in progress */
113 #define STATUS_TIMEOUT 2 /* operation timed out */
114 #define STATUS_ERROR 3 /* unclassified but unhappy status */
116 /* Used to mask of bytes from word data */
117 #define HIGH_BYTE(a) (a >> 8)
118 #define LOW_BYTE(a) (a & 0xFF)
120 /* Define different type of flash */
121 #define FLASH_UNDEFINED 0
123 #define FLASH_ATMEL 2
127 /* ATMEL's manufacturer ID */
128 #define ATMELPART 0x1F
130 /* A list of ATMEL device ID's - add others as needed */
131 #define ID_AT25F512 0x60
132 #define ID_AT25F2048 0x63
134 /* AMD's device ID */
135 #define AMD_S25FL002D 0x11
137 /* SST's manufacturer ID */
140 /* A list of SST device ID's - add others as needed */
141 #define ID_SST25VF016B 0x41
142 #define ID_SST25VF020 0x43
143 #define ID_SST25VF040 0x44
144 #define ID_SST25VF080 0x80
146 /* NexFlash's manufacturer ID */
149 /* A list of NexFlash device ID's - add others as needed */
150 #define ID_NX25P20 0x11
151 #define ID_NX25P40 0x12
154 /* StFlash's manufacturer ID */
157 /* A list of StFlash device ID's - add others as needed */
158 #define ID_M25P40 0x12
160 /* SPANSION manufacturer ID */
161 #define SPANPART 0x01
162 #define SPANPART2 0x02
164 /* SPANSION device ID's */
165 #define ID_SPAN25FL004A 0x12
166 #define ID_SPAN25FL008A 0x13
167 #define ID_SPAN25FL016A 0x14
168 #define ID_SPAN25FL032A 0x15
171 #define SPI_MAKE_ID(A,B) \
172 (((unsigned short) (A) << 8) | ((unsigned short) B & 0xff))
174 #define SPI_FLASH_DEVICES \
175 {{SPI_MAKE_ID(ATMELPART, ID_AT25F512), "AT25F512"}, \
176 {SPI_MAKE_ID(ATMELPART, ID_AT25F2048), "AT25F2048"}, \
177 {SPI_MAKE_ID(AMD_S25FL002D, 0), "AMD_S25FL002D"}, \
178 {SPI_MAKE_ID(SSTPART, ID_SST25VF016B), "SST25VF016B"}, \
179 {SPI_MAKE_ID(SSTPART, ID_SST25VF020), "SST25VF020"}, \
180 {SPI_MAKE_ID(SSTPART, ID_SST25VF040), "SST25VF040"}, \
181 {SPI_MAKE_ID(SSTPART, ID_SST25VF080), "SST25VF080"}, \
182 {SPI_MAKE_ID(NXPART, ID_NX25P20), "NX25P20"}, \
183 {SPI_MAKE_ID(NXPART, ID_NX25P40), "NX25P40"}, \
184 {SPI_MAKE_ID(STPART, ID_M25P40), "M25P40"}, \
185 {SPI_MAKE_ID(SPANPART, ID_SPAN25FL004A), "S25FL004A"}, \
186 {SPI_MAKE_ID(SPANPART, ID_SPAN25FL008A), "S25FL008A"}, \
187 {SPI_MAKE_ID(SPANPART, ID_SPAN25FL016A), "S25FL016A"}, \
188 {SPI_MAKE_ID(SPANPART, ID_SPAN25FL032A), "S25FL032A"}, \
194 /* A structure for identifying a flash part. There is one for each
195 * of the flash part definitions. We need to keep track of the
196 * sector organization, the address register used, and the size
200 char *name; /* "AT25F512", etc. */
201 unsigned long addr; /* physical address, once translated */
202 int nsect; /* # of sectors */
204 long size; /* # of bytes in this sector */
205 long base; /* offset from beginning of device */
206 } sec[MAXSECTORS]; /* per-sector info */
209 struct flash_name_from_id {
210 unsigned short fnfi_id;
216 int spi_flash_init(flash_device_info_t **flash_info);
217 static int spi_read( unsigned char *msg_buf, int prependcnt, int nbytes );
218 static int spi_write( unsigned char *msg_buf, int nbytes );
219 static int spi_flash_sector_erase_int(unsigned short sector);
220 static int spi_flash_reset(void);
221 static int spi_flash_read_buf(unsigned short sector, int offset,
222 unsigned char *buffer, int numbytes);
223 static int spi_flash_ub(unsigned short sector);
224 static int spi_flash_write_status(unsigned char status);
225 static int spi_flash_write_buf(unsigned short sector, int offset,
226 unsigned char *buffer, int numbytes);
227 static int spi_flash_reset_ub(void);
228 static int spi_flash_get_numsectors(void);
229 static int spi_flash_get_sector_size(unsigned short sector);
230 static unsigned char *spi_get_flash_memptr(unsigned short sector);
231 static unsigned char *spi_flash_get_memptr(unsigned short sector);
232 static int spi_flash_write(unsigned short sector, int offset, unsigned char *buf,
234 static int spi_flash_status(void);
235 static unsigned short spi_flash_get_device_id(unsigned short sector);
236 static int spi_flash_get_blk(int addr);
237 static int spi_flash_get_total_size(void);
238 static int spi_flash_get_total_memory_mapped_size(void);
242 static flash_device_info_t flash_spi_dev =
246 spi_flash_sector_erase_int,
249 spi_flash_get_numsectors,
250 spi_flash_get_sector_size,
251 spi_flash_get_memptr,
253 spi_flash_get_total_size,
254 spi_flash_get_total_memory_mapped_size
257 /*********************************************************************/
258 /* 'meminfo' should be a pointer, but most C compilers will not */
259 /* allocate static storage for a pointer without calling */
260 /* non-portable functions such as 'new'. We also want to avoid */
261 /* the overhead of passing this pointer for every driver call. */
262 /* Systems with limited heap space will need to do this. */
263 /*********************************************************************/
264 static struct flashinfo meminfo; /* Flash information structure */
265 static int totalSize = 0;
266 static int flashFamily = FLASH_UNDEFINED;
267 static int sstAaiWordProgram = FALSE;
269 static int spi_read( unsigned char *msg_buf, int prependcnt, int nbytes )
272 SPI->spiMsgCtl = (nbytes << SPI_BYTE_CNT_SHIFT |
273 HALF_DUPLEX_R << SPI_MSG_TYPE_SHIFT);
275 for (i = 0; i < prependcnt; i++)
276 SPI->spiMsgData[i] = msg_buf[i];
278 SPI->spiIntStatus = SPI_INTR_CLEAR_ALL;
280 SPI->spiCmd = (SPI_CMD_START_IMMEDIATE << SPI_CMD_COMMAND_SHIFT |
281 0 << SPI_CMD_DEVICE_ID_SHIFT |
282 prependcnt << SPI_CMD_PREPEND_BYTE_CNT_SHIFT );
285 while (!(SPI->spiIntStatus & SPI_INTR_CMD_DONE));
287 SPI->spiIntStatus = SPI_INTR_CLEAR_ALL;
289 for(i = 0; i < nbytes; i++) {
290 msg_buf[i] = SPI->spiRxDataFifo[i];
292 return SPI_STATUS_OK;
295 static int spi_write( unsigned char *msg_buf, int nbytes )
299 SPI->spiMsgCtl = (nbytes << SPI_BYTE_CNT_SHIFT |
300 HALF_DUPLEX_W << SPI_MSG_TYPE_SHIFT);
302 for (i = 0; i < nbytes; i++)
303 SPI->spiMsgData[i] = msg_buf[i];
305 SPI->spiCmd = (SPI_CMD_START_IMMEDIATE << SPI_CMD_COMMAND_SHIFT |
306 0 << SPI_CMD_DEVICE_ID_SHIFT |
307 0 << SPI_CMD_PREPEND_BYTE_CNT_SHIFT );
309 while (!(SPI->spiIntStatus & SPI_INTR_CMD_DONE));
311 SPI->spiIntStatus = SPI_INTR_CLEAR_ALL;
313 return SPI_STATUS_OK;
316 /*********************************************************************/
317 /* Init_flash is used to build a sector table. This information is */
318 /* translated from erase_block information to base:offset information*/
319 /* for each individual sector. This information is then stored */
320 /* in the meminfo structure, and used throughout the driver to access*/
321 /* sector information. */
323 /* This is more efficient than deriving the sector base:offset */
324 /* information every time the memory map switches (since on the */
325 /* development platform can only map 64k at a time). If the entire */
326 /* flash memory array can be mapped in, then the addition static */
327 /* allocation for the meminfo structure can be eliminated, but the */
328 /* drivers will have to be re-written. */
330 /* The meminfo struct occupies 44 bytes of heap space, depending */
331 /* on the value of the define MAXSECTORS. Adjust to suit */
333 /*********************************************************************/
335 int spi_flash_init(flash_device_info_t **flash_info)
337 struct flash_name_from_id fnfi[] = SPI_FLASH_DEVICES;
338 struct flash_name_from_id *fnfi_ptr;
341 unsigned short device_id;
342 unsigned short blkEnables;
346 *flash_info = &flash_spi_dev;
350 * in case of flash corrupt, the following steps can erase the flash
351 * 1. jumper USE_SPI_SLAVE to make SPI in slave mode
352 * 2. start up JTAG debuger and remove the USE_SPI_SLAVE jumper
353 * 3. run the following code to erase the flash
355 flash_sector_erase_int(0);
356 flash_sector_erase_int(1);
357 printk("flash_init: erase all sectors\n");
360 blkEnables = PERF->blkEnables;
361 if ((blkEnables & SPI_CLK_EN) == 0) {
362 blkEnables |= SPI_CLK_EN;
363 PERF->blkEnables = blkEnables;
366 flash_spi_dev.flash_device_id = device_id = spi_flash_get_device_id(0);
368 switch( device_id >> 8 ) {
370 flashFamily = FLASH_ATMEL;
371 switch ((char)(device_id & 0x00ff)) {
373 numsector = AT25F512_SECTOR;
374 sectorsize = SECTOR_SIZE_32K;
377 numsector = AT25F2048_SECTOR;
378 sectorsize = SECTOR_SIZE_64K;
386 flashFamily = FLASH_SST;
387 sectorsize = SECTOR_SIZE_4K;
388 switch ((unsigned char)(device_id & 0x00ff)) {
390 numsector = SST25VF016B_SECTOR;
391 sstAaiWordProgram = TRUE;
394 numsector = SST25VF080_SECTOR;
397 numsector = SST25VF040_SECTOR;
401 numsector = SST25VF020_SECTOR;
407 flashFamily = FLASH_AMD;
408 numsector = AMD25FL_SECTOR;
409 sectorsize = SECTOR_SIZE_64K;
414 /* NexFlash parts are AMD compatible. */
415 flashFamily = FLASH_AMD;
416 sectorsize = SECTOR_SIZE_64K;
417 switch ((unsigned char)(device_id & 0x00ff)) {
419 numsector = 4; /* 4 * 64KB == 256KB flash size */
422 numsector = 8; /* 8 * 64KB == 512KB flash size */
428 /* StFlash parts are AMD compatible. */
429 flashFamily = FLASH_AMD;
430 sectorsize = SECTOR_SIZE_64K;
432 numsector = 8; /* 8 * 64KB == 512KB flash size */
436 flashFamily = FLASH_SPAN;
437 sectorsize = SECTOR_SIZE_64K;
438 switch ((unsigned short)(device_id & 0x00ff)) {
439 case ID_SPAN25FL032A:
440 numsector = SPAN25FL032A_SECTOR;
442 case ID_SPAN25FL016A:
443 numsector = SPAN25FL016A_SECTOR;
445 case ID_SPAN25FL008A:
446 numsector = SPAN25FL008A_SECTOR;
448 case ID_SPAN25FL004A:
450 numsector = SPAN25FL004A_SECTOR;
458 meminfo.sec[0].size = SECTOR_SIZE_4K;
459 meminfo.sec[0].base = 0x00000;
460 return FLASH_API_ERROR;
464 meminfo.nsect = numsector;
465 for (i = 0; i < numsector; i++) {
466 meminfo.sec[i].size = sectorsize;
467 meminfo.sec[i].base = basecount;
468 basecount += meminfo.sec[i].size;
471 totalSize = meminfo.sec[count-1].base + meminfo.sec[count-1].size;
473 for( fnfi_ptr = fnfi; fnfi_ptr->fnfi_id != 0; fnfi_ptr++ ) {
474 if( fnfi_ptr->fnfi_id == device_id ) {
475 strcpy( flash_spi_dev.flash_device_name, fnfi_ptr->fnfi_name );
479 return (FLASH_API_OK);
482 /*********************************************************************/
483 /* Flash_sector_erase_int() wait until the erase is completed before */
484 /* returning control to the calling function. This can be used in */
485 /* cases which require the program to hold until a sector is erased, */
486 /* without adding the wait check external to this function. */
487 /*********************************************************************/
489 static int spi_flash_sector_erase_int(unsigned short sector)
491 unsigned char buf[4];
495 if (flashFamily == FLASH_UNDEFINED)
496 return FLASH_API_ERROR;
498 /* set device to write enabled */
500 spi_flash_ub(sector);
502 switch (flashFamily) {
504 buf[0] = SST_FLASH_SERASE;
507 buf[0] = ATMEL_FLASH_SERASE;
510 buf[0] = AMD_FLASH_SERASE;
513 buf[0] = SPAN_FLASH_SERASE;
517 /* erase the sector */
518 addr = (unsigned int) spi_get_flash_memptr(sector);
519 buf[1] = (unsigned char)((addr & 0x00ff0000) >> 16);
520 buf[2] = (unsigned char)((addr & 0x0000ff00) >> 8);
521 buf[3] = (unsigned char)(addr & 0x000000ff);
522 rc = spi_write(buf, sizeof(buf));
524 /* check device is ready */
525 if (rc == SPI_STATUS_OK) {
526 while (spi_flash_status() != STATUS_READY) {}
528 return(FLASH_API_OK);
531 /*********************************************************************/
532 /* flash_chip_erase_int() wait until the erase is completed before */
533 /* returning control to the calling function. This can be used in */
534 /* cases which require the program to hold until a sector is erased, */
535 /* without adding the wait check external to this function. */
536 /*********************************************************************/
539 unsigned char spi_flash_chip_erase_int(void)
541 unsigned char buf[4];
544 if (flashFamily == FLASH_UNDEFINED)
545 return FLASH_API_ERROR;
547 /* set device to write enabled */
549 rc = spi_write(buf, 1);
551 /* check device is ready */
552 if (rc == SPI_STATUS_OK) {
555 rc = spi_read(buf, 1, 1);
556 if (rc == SPI_STATUS_OK) {
557 if (buf[0] & SR_WEN) {
566 switch (flashFamily) {
568 buf[0] = SST_FLASH_CERASE;
571 buf[0] = ATMEL_FLASH_CERASE;
574 buf[0] = AMD_FLASH_CERASE;
577 /* erase the sector */
578 rc = spi_write(buf, 1);
580 /* check device is ready */
581 if (rc == SPI_STATUS_OK) {
582 while (spi_flash_status() != STATUS_READY) {}
585 return(FLASH_API_OK);
589 /*********************************************************************/
590 /* flash_reset() will reset the flash device to reading array data. */
591 /* It is good practice to call this function after autoselect */
592 /* sequences had been performed. */
593 /*********************************************************************/
595 static int spi_flash_reset(void)
597 if (flashFamily == FLASH_UNDEFINED)
598 return FLASH_API_ERROR;
599 spi_flash_reset_ub();
600 while (spi_flash_status() != STATUS_READY) { }
601 return(FLASH_API_OK);
604 /*********************************************************************/
605 /* flash_read_buf() reads buffer of data from the specified */
606 /* offset from the sector parameter. */
607 /*********************************************************************/
609 static int spi_flash_read_buf(unsigned short sector, int offset,
610 unsigned char *buffer, int numbytes)
612 unsigned char buf[MAX_READ];
617 if (flashFamily == FLASH_UNDEFINED)
618 return FLASH_API_ERROR;
622 addr = (unsigned int) spi_get_flash_memptr(sector);
625 while (numbytes > 0) {
626 maxread = (numbytes < sizeof(buf)) ? numbytes : sizeof(buf);
628 buf[1] = (unsigned char)((addr & 0x00ff0000) >> 16);
629 buf[2] = (unsigned char)((addr & 0x0000ff00) >> 8);
630 buf[3] = (unsigned char)(addr & 0x000000ff);
631 spi_read(buf, 4, maxread);
632 while (spi_flash_status() != STATUS_READY) {}
633 memcpy(buffer+idx, buf, maxread);
639 return (FLASH_API_OK);
642 /*********************************************************************/
643 /* flash_ub() places the flash into unlock bypass mode. This */
644 /* is REQUIRED to be called before any of the other unlock bypass */
645 /* commands will become valid (most will be ignored without first */
646 /* calling this function. */
647 /*********************************************************************/
649 static int spi_flash_ub(unsigned short sector)
651 unsigned char buf[4];
656 rc = spi_read(buf, 1, 1);
658 if (rc == SPI_STATUS_OK) {
659 while (spi_flash_status() != STATUS_READY) {}
660 if (buf[0] & (SR_BP2|SR_BP1|SR_BP0)) {
661 spi_flash_write_status((unsigned char)~(SR_WPEN|SR_BP2|SR_BP1|SR_BP0));
670 /* set device to write enabled */
672 rc = spi_write(buf, 1);
674 /* check device is ready */
675 if (rc == SPI_STATUS_OK) {
676 while (spi_flash_status() != STATUS_READY) {}
679 rc = spi_read(buf, 1, 1);
680 if (rc == SPI_STATUS_OK) {
681 while (spi_flash_status() != STATUS_READY) {}
682 if (buf[0] & SR_WEN) {
691 return(FLASH_API_OK);
694 static int spi_flash_write_status(unsigned char status)
696 unsigned char buf[4];
697 int rc = SPI_STATUS_OK;
699 if (flashFamily == FLASH_UNDEFINED)
700 return FLASH_API_ERROR;
702 switch (flashFamily) {
705 rc = spi_write(buf, 1);
709 rc = spi_write(buf, 1);
714 if (rc == SPI_STATUS_OK) {
716 buf[1] = (status & (SR_WPEN|SR_BP2|SR_BP1|SR_BP0));
717 rc = spi_write(buf, 2);
718 if (rc == SPI_STATUS_OK)
719 while (spi_flash_status() != STATUS_READY) {}
725 /*********************************************************************/
726 /* flash_write_buf() utilizes */
727 /* the unlock bypass mode of the flash device. This can remove */
728 /* significant overhead from the bulk programming operation, and */
729 /* when programming bulk data a sizeable performance increase can be */
731 /*********************************************************************/
733 static int spi_flash_write_buf(unsigned short sector, int offset,
734 unsigned char *buffer, int numbytes)
736 int ret = FLASH_API_ERROR;
738 if (flashFamily == FLASH_UNDEFINED)
739 return FLASH_API_ERROR;
741 ret = spi_flash_write(sector, offset, buffer, numbytes, TRUE);
744 printk( "Flash write error. Verify failed\n" );
749 /*********************************************************************/
750 /* flash_reset_ub() is required to remove the flash from unlock */
751 /* bypass mode. This is important, as other flash commands will be */
752 /* ignored while the flash is in unlock bypass mode. */
753 /*********************************************************************/
755 static int spi_flash_reset_ub(void)
757 unsigned char buf[4];
759 if (flashFamily == FLASH_UNDEFINED)
760 return FLASH_API_ERROR;
761 /* set device to write disabled */
764 while (spi_flash_status() != STATUS_READY) {}
766 return(FLASH_API_OK);
769 /*********************************************************************/
770 /* Usefull funtion to return the number of sectors in the device. */
771 /* Can be used for functions which need to loop among all the */
772 /* sectors, or wish to know the number of the last sector. */
773 /*********************************************************************/
775 static int spi_flash_get_numsectors(void)
777 return meminfo.nsect;
780 /*********************************************************************/
781 /* flash_get_sector_size() is provided for cases in which the size */
782 /* of a sector is required by a host application. The sector size */
783 /* (in bytes) is returned in the data location pointed to by the */
784 /* 'size' parameter. */
785 /*********************************************************************/
787 static int spi_flash_get_sector_size(unsigned short sector)
789 return meminfo.sec[sector].size;
792 /*********************************************************************/
793 /* The purpose of get_flash_memptr() is to return a memory pointer */
794 /* which points to the beginning of memory space allocated for the */
795 /* flash. All function pointers are then referenced from this */
798 /* Different systems will implement this in different ways: */
799 /* possibilities include: */
800 /* - A direct memory pointer */
801 /* - A pointer to a memory map */
802 /* - A pointer to a hardware port from which the linear */
803 /* address is translated */
804 /* - Output of an MMU function / service */
806 /* Also note that this function expects the pointer to a specific */
807 /* sector of the device. This can be provided by dereferencing */
808 /* the pointer from a translated offset of the sector from a */
809 /* global base pointer (e.g. flashptr = base_pointer + sector_offset)*/
811 /* Important: Many AMD flash devices need both bank and or sector */
812 /* address bits to be correctly set (bank address bits are A18-A16, */
813 /* and sector address bits are A18-A12, or A12-A15). Flash parts */
814 /* which do not need these bits will ignore them, so it is safe to */
815 /* assume that every part will require these bits to be set. */
816 /*********************************************************************/
818 static unsigned char *spi_get_flash_memptr(unsigned short sector)
820 unsigned char *memptr = (unsigned char*)
821 (FLASH_BASE + meminfo.sec[sector].base);
826 static unsigned char *spi_flash_get_memptr(unsigned short sector)
828 return( spi_get_flash_memptr(sector) );
831 /*********************************************************************/
832 /* Flash_write extends the functionality of flash_program() by */
833 /* providing an faster way to program multiple data words, without */
834 /* needing the function overhead of looping algorithms which */
835 /* program word by word. This function utilizes fast pointers */
836 /* to quickly loop through bulk data. */
837 /*********************************************************************/
838 static int spi_flash_write(unsigned short sector, int offset, unsigned char *buf,
841 unsigned char wbuf[FLASH_PAGE_SIZE+32];
849 addr = (unsigned int) spi_get_flash_memptr(sector);
853 switch (flashFamily) {
855 if( sstAaiWordProgram == FALSE )
857 /* Auto Address Increment one byte at a time. */
858 spi_flash_ub(sector); /* enable write */
865 wbuf[1] = (unsigned char)((dst & 0x00ff0000) >> 16);
866 wbuf[2] = (unsigned char)((dst & 0x0000ff00) >> 8);
867 wbuf[3] = (unsigned char)(dst & 0x000000ff);
871 spi_write(wbuf, 1+cmdlen);
872 while (spi_flash_status() != STATUS_READY) {}
873 pbuf++; /* update address and count by one byte */
876 spi_flash_reset_ub();
877 while (spi_flash_status() != STATUS_READY) {}
881 /* Auto Address Increment one word (2 bytes) at a time. */
882 spi_flash_ub(sector); /* enable write */
883 wbuf[0] = FLASH_WORD_AAI;
887 wbuf[2] = *(pbuf + 1);
890 wbuf[1] = (unsigned char)((dst & 0x00ff0000) >> 16);
891 wbuf[2] = (unsigned char)((dst & 0x0000ff00) >> 8);
892 wbuf[3] = (unsigned char)(dst & 0x000000ff);
894 wbuf[5] = *(pbuf + 1);
897 spi_write(wbuf, cmdlen);
898 while (spi_flash_status() != STATUS_READY) {}
899 pbuf += 2; /* update address and count by two bytes */
902 spi_flash_reset_ub();
903 while (spi_flash_status() != STATUS_READY) {}
911 spi_flash_ub(sector); /* enable write */
912 maxwrite = (nbytes < (sizeof(SPI->spiMsgData)-CMD_LEN_4))
913 ? nbytes : (sizeof(SPI->spiMsgData)-CMD_LEN_4);
914 /* maxwrite is limit to page boundary */
915 pagelimit = FLASH_PAGE_SIZE - (dst & 0x000000ff);
916 maxwrite = (maxwrite < pagelimit) ? maxwrite : pagelimit;
918 wbuf[0] = FLASH_PROG;
919 wbuf[1] = (unsigned char)((dst & 0x00ff0000) >> 16);
920 wbuf[2] = (unsigned char)((dst & 0x0000ff00) >> 8);
921 wbuf[3] = (unsigned char)(dst & 0x000000ff);
922 memcpy(&wbuf[4], pbuf, maxwrite);
923 spi_write(wbuf, maxwrite+CMD_LEN_4);
924 while (spi_flash_status() != STATUS_READY) {}
937 /*********************************************************************/
938 /* Flash_status return an appropriate status code */
939 /*********************************************************************/
941 static int spi_flash_status(void)
943 unsigned char buf[4];
947 /* check device is ready */
950 rc = spi_read(buf, 1, 1);
951 if (rc == SPI_STATUS_OK) {
952 if (!(buf[0] & SR_RDY)) {
961 return STATUS_TIMEOUT;
964 /*********************************************************************/
965 /* flash_get_device_id() return the device id of the component. */
966 /*********************************************************************/
968 static unsigned short spi_flash_get_device_id(unsigned short sector)
970 unsigned char buf[4];
974 for (i = 0; i < MAX_RETRY; i++) {
975 /* read product id command */
976 buf[0] = SST_FLASH_RDID;
981 spi_read(buf, prependcnt, sizeof(unsigned short));
982 for (j = 0; j < MAX_RETRY * 10; j++) {
983 if (spi_flash_status() == STATUS_READY) {
984 if (buf[0] == SSTPART || buf[0] == NXPART)
985 return( *(unsigned short *)&buf[0] );
991 for (i = 0; i < MAX_RETRY; i++) {
992 buf[0] = ATMEL_FLASH_RDID;
994 spi_read(buf, prependcnt, sizeof(unsigned short));
995 for (j = 0; j < MAX_RETRY * 10; j++) {
996 if (spi_flash_status() == STATUS_READY) {
997 if (buf[0] == ATMELPART)
998 return( *(unsigned short *)&buf[0] );
1004 for (i = 0; i < MAX_RETRY; i++) {
1005 buf[0] = SPAN_FLASH_RDID;
1007 spi_read(buf, prependcnt, 3);
1008 for (j = 0; j < MAX_RETRY * 10; j++) {
1009 if (spi_flash_status() == STATUS_READY) {
1010 if ((buf[0] == SPANPART) && (buf[1] == SPANPART2)) {
1012 return( *(unsigned short *)&buf[0] );
1019 // AMD_FLASH_RDID is the same as RES command for SPAN,
1020 // so it has to be the last one.
1022 for (i = 0; i < MAX_RETRY; i++) {
1023 buf[0] = AMD_FLASH_RDID;
1028 spi_read(buf, prependcnt, sizeof(unsigned short));
1029 for (j = 0; j < MAX_RETRY * 10; j++) {
1030 if (spi_flash_status() == STATUS_READY) {
1031 if (buf[0] == AMD_S25FL002D || buf[0] == STPART)
1032 return( *(unsigned short *)&buf[0] );
1036 /* return manufacturer code and device code */
1037 return( *(unsigned short *)&buf[0] );
1040 /*********************************************************************/
1041 /* The purpose of flash_get_blk() is to return a the block number */
1042 /* for a given memory address. */
1043 /*********************************************************************/
1045 static int spi_flash_get_blk(int addr)
1048 int last_blk = spi_flash_get_numsectors();
1049 int relative_addr = addr - (int) FLASH_BASE;
1051 for(blk_start=0, i=0; i < relative_addr && blk_start < last_blk; blk_start++)
1052 i += spi_flash_get_sector_size(blk_start);
1054 if( (unsigned int)i > (unsigned int)relative_addr ) {
1055 blk_start--; // last blk, dec by 1
1057 if( blk_start == last_blk )
1059 printk("Address is too big.\n");
1064 return( blk_start );
1067 /************************************************************************/
1068 /* The purpose of flash_get_total_size() is to return the total size of */
1070 /************************************************************************/
1071 static int spi_flash_get_total_size(void)
1076 static int spi_flash_get_total_memory_mapped_size(void)
1078 return((totalSize < MAX_MEMORY_MAPPED_SIZE)
1079 ? totalSize : MAX_MEMORY_MAPPED_SIZE);