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 (1024 * 1024)
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 AT25F512_SECTOR 2
54 #define AT25F2048_SECTOR 4
55 #define AMD25FL_SECTOR 4
57 /* use 60 instead 63 for aligned on word (4 bytes) boundaries */
62 /* Standard Boolean declarations */
66 #define SPI_STATUS_OK 0
67 #define SPI_STATUS_INVALID_LEN -1
69 /* Command codes for the flash_command routine */
70 #define FLASH_READ 0x03 /* read data from memory array */
71 #define FLASH_PROG 0x02 /* program data into memory array */
72 #define FLASH_WREN 0x06 /* set write enable latch */
73 #define FLASH_WRDI 0x04 /* reset write enable latch */
74 #define FLASH_RDSR 0x05 /* read status register */
75 #define FLASH_WRST 0x01 /* write status register */
76 #define FLASH_EWSR 0x50 /* enable write status */
77 #define FLASH_WORD_AAI 0xAD /* auto address increment word program */
78 #define FLASH_AAI 0xAF /* auto address increment program */
80 #define SST_FLASH_CERASE 0x60 /* erase all sectors in memory array */
81 #define SST_FLASH_SERASE 0x20 /* erase one sector in memroy array */
82 #define SST_FLASH_RDID 0x90 /* read manufacturer and product id */
84 #define ATMEL_FLASH_CERASE 0x62 /* erase all sectors in memory array */
85 #define ATMEL_FLASH_SERASE 0x52 /* erase one sector in memroy array */
86 #define ATMEL_FLASH_RDID 0x15 /* read manufacturer and product id */
88 #define AMD_FLASH_CERASE 0xC7 /* erase all sectors in memory array */
89 #define AMD_FLASH_SERASE 0xD8 /* erase one sector in memroy array */
90 #define AMD_FLASH_RDID 0xAB /* read manufacturer and product id */
92 /* RDSR return status bit definition */
100 /* Return codes from flash_status */
101 #define STATUS_READY 0 /* ready for action */
102 #define STATUS_BUSY 1 /* operation in progress */
103 #define STATUS_TIMEOUT 2 /* operation timed out */
104 #define STATUS_ERROR 3 /* unclassified but unhappy status */
106 /* Used to mask of bytes from word data */
107 #define HIGH_BYTE(a) (a >> 8)
108 #define LOW_BYTE(a) (a & 0xFF)
110 /* Define different type of flash */
111 #define FLASH_UNDEFINED 0
113 #define FLASH_ATMEL 2
116 /* ATMEL's manufacturer ID */
117 #define ATMELPART 0x1F
119 /* A list of ATMEL device ID's - add others as needed */
120 #define ID_AT25F512 0x60
121 #define ID_AT25F2048 0x63
123 /* AMD's device ID */
124 #define AMD_S25FL002D 0x11
126 /* SST's manufacturer ID */
129 /* A list of SST device ID's - add others as needed */
130 #define ID_SST25VF016B 0x41
131 #define ID_SST25VF020 0x43
132 #define ID_SST25VF040 0x44
133 #define ID_SST25VF080 0x80
135 #define SPI_MAKE_ID(A,B) \
136 (((unsigned short) (A) << 8) | ((unsigned short) B & 0xff))
138 #define SPI_FLASH_DEVICES \
139 {{SPI_MAKE_ID(ATMELPART, ID_AT25F512), "AT25F512"}, \
140 {SPI_MAKE_ID(ATMELPART, ID_AT25F2048), "AT25F2048"}, \
141 {SPI_MAKE_ID(AMD_S25FL002D, 0), "AMD_S25FL002D"}, \
142 {SPI_MAKE_ID(SSTPART, ID_SST25VF016B), "SST25VF016B"}, \
143 {SPI_MAKE_ID(SSTPART, ID_SST25VF020), "SST25VF020"}, \
144 {SPI_MAKE_ID(SSTPART, ID_SST25VF040), "SST25VF040"}, \
145 {SPI_MAKE_ID(SSTPART, ID_SST25VF080), "SST25VF080"}, \
151 /* A structure for identifying a flash part. There is one for each
152 * of the flash part definitions. We need to keep track of the
153 * sector organization, the address register used, and the size
157 char *name; /* "AT25F512", etc. */
158 unsigned long addr; /* physical address, once translated */
159 int nsect; /* # of sectors */
161 long size; /* # of bytes in this sector */
162 long base; /* offset from beginning of device */
163 } sec[MAXSECTORS]; /* per-sector info */
166 struct flash_name_from_id {
167 unsigned short fnfi_id;
173 int spi_flash_init(flash_device_info_t **flash_info);
174 static int spi_read( unsigned char *msg_buf, int prependcnt, int nbytes );
175 static int spi_write( unsigned char *msg_buf, int nbytes );
176 static int spi_flash_sector_erase_int(unsigned short sector);
177 static int spi_flash_reset(void);
178 static int spi_flash_read_buf(unsigned short sector, int offset,
179 unsigned char *buffer, int numbytes);
180 static int spi_flash_ub(unsigned short sector);
181 static int spi_flash_write_status(unsigned char status);
182 static int spi_flash_write_buf(unsigned short sector, int offset,
183 unsigned char *buffer, int numbytes);
184 static int spi_flash_reset_ub(void);
185 static int spi_flash_get_numsectors(void);
186 static int spi_flash_get_sector_size(unsigned short sector);
187 static unsigned char *spi_get_flash_memptr(unsigned short sector);
188 static unsigned char *spi_flash_get_memptr(unsigned short sector);
189 static int spi_flash_write(unsigned short sector, int offset, unsigned char *buf,
191 static int spi_flash_status(void);
192 static unsigned short spi_flash_get_device_id(unsigned short sector);
193 static int spi_flash_get_blk(int addr);
194 static int spi_flash_get_total_size(void);
195 static int spi_flash_get_total_memory_mapped_size(void);
199 static flash_device_info_t flash_spi_dev =
203 spi_flash_sector_erase_int,
206 spi_flash_get_numsectors,
207 spi_flash_get_sector_size,
208 spi_flash_get_memptr,
210 spi_flash_get_total_size,
211 spi_flash_get_total_memory_mapped_size
214 /*********************************************************************/
215 /* 'meminfo' should be a pointer, but most C compilers will not */
216 /* allocate static storage for a pointer without calling */
217 /* non-portable functions such as 'new'. We also want to avoid */
218 /* the overhead of passing this pointer for every driver call. */
219 /* Systems with limited heap space will need to do this. */
220 /*********************************************************************/
221 static struct flashinfo meminfo; /* Flash information structure */
222 static int totalSize = 0;
223 static int flashFamily = FLASH_UNDEFINED;
224 static int sstAaiWordProgram = FALSE;
226 static int spi_read( unsigned char *msg_buf, int prependcnt, int nbytes )
229 SPI->spiMsgCtl = (nbytes << SPI_BYTE_CNT_SHIFT |
230 HALF_DUPLEX_R << SPI_MSG_TYPE_SHIFT);
232 for (i = 0; i < prependcnt; i++)
233 SPI->spiMsgData[i] = msg_buf[i];
235 SPI->spiIntStatus = SPI_INTR_CLEAR_ALL;
237 SPI->spiCmd = (SPI_CMD_START_IMMEDIATE << SPI_CMD_COMMAND_SHIFT |
238 0 << SPI_CMD_DEVICE_ID_SHIFT |
239 prependcnt << SPI_CMD_PREPEND_BYTE_CNT_SHIFT );
242 while (!(SPI->spiIntStatus & SPI_INTR_CMD_DONE));
244 SPI->spiIntStatus = SPI_INTR_CLEAR_ALL;
246 for(i = 0; i < nbytes; i++) {
247 msg_buf[i] = SPI->spiRxDataFifo[i];
249 return SPI_STATUS_OK;
252 static int spi_write( unsigned char *msg_buf, int nbytes )
256 SPI->spiMsgCtl = (nbytes << SPI_BYTE_CNT_SHIFT |
257 HALF_DUPLEX_W << SPI_MSG_TYPE_SHIFT);
259 for (i = 0; i < nbytes; i++)
260 SPI->spiMsgData[i] = msg_buf[i];
262 SPI->spiCmd = (SPI_CMD_START_IMMEDIATE << SPI_CMD_COMMAND_SHIFT |
263 0 << SPI_CMD_DEVICE_ID_SHIFT |
264 0 << SPI_CMD_PREPEND_BYTE_CNT_SHIFT );
266 while (!(SPI->spiIntStatus & SPI_INTR_CMD_DONE));
268 SPI->spiIntStatus = SPI_INTR_CLEAR_ALL;
270 return SPI_STATUS_OK;
273 /*********************************************************************/
274 /* Init_flash is used to build a sector table. This information is */
275 /* translated from erase_block information to base:offset information*/
276 /* for each individual sector. This information is then stored */
277 /* in the meminfo structure, and used throughout the driver to access*/
278 /* sector information. */
280 /* This is more efficient than deriving the sector base:offset */
281 /* information every time the memory map switches (since on the */
282 /* development platform can only map 64k at a time). If the entire */
283 /* flash memory array can be mapped in, then the addition static */
284 /* allocation for the meminfo structure can be eliminated, but the */
285 /* drivers will have to be re-written. */
287 /* The meminfo struct occupies 44 bytes of heap space, depending */
288 /* on the value of the define MAXSECTORS. Adjust to suit */
290 /*********************************************************************/
292 int spi_flash_init(flash_device_info_t **flash_info)
294 struct flash_name_from_id fnfi[] = SPI_FLASH_DEVICES;
295 struct flash_name_from_id *fnfi_ptr;
298 unsigned short device_id;
299 unsigned short blkEnables;
303 *flash_info = &flash_spi_dev;
307 * in case of flash corrupt, the following steps can erase the flash
308 * 1. jumper USE_SPI_SLAVE to make SPI in slave mode
309 * 2. start up JTAG debuger and remove the USE_SPI_SLAVE jumper
310 * 3. run the following code to erase the flash
312 flash_sector_erase_int(0);
313 flash_sector_erase_int(1);
314 printk("flash_init: erase all sectors\n");
317 blkEnables = PERF->blkEnables;
318 if ((blkEnables & SPI_CLK_EN) == 0) {
319 blkEnables |= SPI_CLK_EN;
320 PERF->blkEnables = blkEnables;
323 flash_spi_dev.flash_device_id = device_id = spi_flash_get_device_id(0);
325 if ((((char)(device_id >> 8)) == ATMELPART)) {
326 flashFamily = FLASH_ATMEL;
327 switch ((char)(device_id & 0x00ff)) {
329 numsector = AT25F512_SECTOR;
330 sectorsize = SECTOR_SIZE_32K;
333 numsector = AT25F2048_SECTOR;
334 sectorsize = SECTOR_SIZE_64K;
340 else if (((char)(device_id >> 8)) == (char)SSTPART) {
341 flashFamily = FLASH_SST;
342 sectorsize = SECTOR_SIZE_4K;
343 switch ((unsigned char)(device_id & 0x00ff)) {
345 numsector = SST25VF016B_SECTOR;
346 sstAaiWordProgram = TRUE;
349 numsector = SST25VF080_SECTOR;
352 numsector = SST25VF040_SECTOR;
356 numsector = SST25VF020_SECTOR;
360 else if (((char)(device_id >> 8)) == (char)AMD_S25FL002D) {
361 flashFamily = FLASH_AMD;
362 numsector = AMD25FL_SECTOR;
363 sectorsize = SECTOR_SIZE_64K;
369 meminfo.sec[0].size = SECTOR_SIZE_4K;
370 meminfo.sec[0].base = 0x00000;
371 return FLASH_API_ERROR;
375 meminfo.nsect = numsector;
376 for (i = 0; i < numsector; i++) {
377 meminfo.sec[i].size = sectorsize;
378 meminfo.sec[i].base = basecount;
379 basecount += meminfo.sec[i].size;
382 totalSize = meminfo.sec[count-1].base + meminfo.sec[count-1].size;
384 for( fnfi_ptr = fnfi; fnfi_ptr->fnfi_id != 0; fnfi_ptr++ ) {
385 if( fnfi_ptr->fnfi_id == device_id ) {
386 strcpy( flash_spi_dev.flash_device_name, fnfi_ptr->fnfi_name );
391 return (FLASH_API_OK);
394 /*********************************************************************/
395 /* Flash_sector_erase_int() wait until the erase is completed before */
396 /* returning control to the calling function. This can be used in */
397 /* cases which require the program to hold until a sector is erased, */
398 /* without adding the wait check external to this function. */
399 /*********************************************************************/
401 static int spi_flash_sector_erase_int(unsigned short sector)
403 unsigned char buf[4];
407 if (flashFamily == FLASH_UNDEFINED)
408 return FLASH_API_ERROR;
410 /* set device to write enabled */
412 spi_flash_ub(sector);
414 switch (flashFamily) {
416 buf[0] = SST_FLASH_SERASE;
419 buf[0] = ATMEL_FLASH_SERASE;
422 buf[0] = AMD_FLASH_SERASE;
427 /* erase the sector */
428 addr = (unsigned int) spi_get_flash_memptr(sector);
429 buf[1] = (unsigned char)((addr & 0x00ff0000) >> 16);
430 buf[2] = (unsigned char)((addr & 0x0000ff00) >> 8);
431 buf[3] = (unsigned char)(addr & 0x000000ff);
432 rc = spi_write(buf, sizeof(buf));
434 /* check device is ready */
435 if (rc == SPI_STATUS_OK) {
436 while (spi_flash_status() != STATUS_READY) {}
439 return(FLASH_API_OK);
442 /*********************************************************************/
443 /* flash_chip_erase_int() wait until the erase is completed before */
444 /* returning control to the calling function. This can be used in */
445 /* cases which require the program to hold until a sector is erased, */
446 /* without adding the wait check external to this function. */
447 /*********************************************************************/
450 unsigned char spi_flash_chip_erase_int(void)
452 unsigned char buf[4];
455 if (flashFamily == FLASH_UNDEFINED)
456 return FLASH_API_ERROR;
458 /* set device to write enabled */
460 rc = spi_write(buf, 1);
462 /* check device is ready */
463 if (rc == SPI_STATUS_OK) {
466 rc = spi_read(buf, 1, 1);
467 if (rc == SPI_STATUS_OK) {
468 if (buf[0] & SR_WEN) {
477 switch (flashFamily) {
479 buf[0] = SST_FLASH_CERASE;
482 buf[0] = ATMEL_FLASH_CERASE;
485 buf[0] = AMD_FLASH_CERASE;
488 /* erase the sector */
489 rc = spi_write(buf, 1);
491 /* check device is ready */
492 if (rc == SPI_STATUS_OK) {
493 while (spi_flash_status() != STATUS_READY) {}
496 return(FLASH_API_OK);
500 /*********************************************************************/
501 /* flash_reset() will reset the flash device to reading array data. */
502 /* It is good practice to call this function after autoselect */
503 /* sequences had been performed. */
504 /*********************************************************************/
506 static int spi_flash_reset(void)
508 if (flashFamily == FLASH_UNDEFINED)
509 return FLASH_API_ERROR;
510 spi_flash_reset_ub();
511 while (spi_flash_status() != STATUS_READY) { }
512 return(FLASH_API_OK);
515 /*********************************************************************/
516 /* flash_read_buf() reads buffer of data from the specified */
517 /* offset from the sector parameter. */
518 /*********************************************************************/
520 static int spi_flash_read_buf(unsigned short sector, int offset,
521 unsigned char *buffer, int numbytes)
523 unsigned char buf[MAX_READ];
528 if (flashFamily == FLASH_UNDEFINED)
529 return FLASH_API_ERROR;
533 addr = (unsigned int) spi_get_flash_memptr(sector);
537 maxread = (numbytes < sizeof(buf)) ? numbytes : sizeof(buf);
539 buf[1] = (unsigned char)((addr & 0x00ff0000) >> 16);
540 buf[2] = (unsigned char)((addr & 0x0000ff00) >> 8);
541 buf[3] = (unsigned char)(addr & 0x000000ff);
542 spi_read(buf, 4, maxread);
543 while (spi_flash_status() != STATUS_READY) {}
544 memcpy(buffer+idx, buf, maxread);
550 return (FLASH_API_OK);
553 /*********************************************************************/
554 /* flash_ub() places the flash into unlock bypass mode. This */
555 /* is REQUIRED to be called before any of the other unlock bypass */
556 /* commands will become valid (most will be ignored without first */
557 /* calling this function. */
558 /*********************************************************************/
560 static int spi_flash_ub(unsigned short sector)
562 unsigned char buf[4];
567 rc = spi_read(buf, 1, 1);
569 if (rc == SPI_STATUS_OK) {
570 while (spi_flash_status() != STATUS_READY) {}
571 if (buf[0] & (SR_BP2|SR_BP1|SR_BP0)) {
572 spi_flash_write_status((unsigned char)~(SR_WPEN|SR_BP2|SR_BP1|SR_BP0));
581 /* set device to write enabled */
583 rc = spi_write(buf, 1);
585 /* check device is ready */
586 if (rc == SPI_STATUS_OK) {
587 while (spi_flash_status() != STATUS_READY) {}
590 rc = spi_read(buf, 1, 1);
591 if (rc == SPI_STATUS_OK) {
592 while (spi_flash_status() != STATUS_READY) {}
593 if (buf[0] & SR_WEN) {
602 return(FLASH_API_OK);
605 static int spi_flash_write_status(unsigned char status)
607 unsigned char buf[4];
608 int rc = SPI_STATUS_OK;
610 if (flashFamily == FLASH_UNDEFINED)
611 return FLASH_API_ERROR;
613 switch (flashFamily) {
616 rc = spi_write(buf, 1);
621 if (rc == SPI_STATUS_OK) {
623 buf[1] = (status & (SR_WPEN|SR_BP2|SR_BP1|SR_BP0));
624 rc = spi_write(buf, 2);
625 if (rc == SPI_STATUS_OK)
626 while (spi_flash_status() != STATUS_READY) {}
632 /*********************************************************************/
633 /* flash_write_buf() utilizes */
634 /* the unlock bypass mode of the flash device. This can remove */
635 /* significant overhead from the bulk programming operation, and */
636 /* when programming bulk data a sizeable performance increase can be */
638 /*********************************************************************/
640 static int spi_flash_write_buf(unsigned short sector, int offset,
641 unsigned char *buffer, int numbytes)
643 int ret = FLASH_API_ERROR;
645 if (flashFamily == FLASH_UNDEFINED)
646 return FLASH_API_ERROR;
648 ret = spi_flash_write(sector, offset, buffer, numbytes, TRUE);
651 printk( "Flash write error. Verify failed\n" );
656 /*********************************************************************/
657 /* flash_reset_ub() is required to remove the flash from unlock */
658 /* bypass mode. This is important, as other flash commands will be */
659 /* ignored while the flash is in unlock bypass mode. */
660 /*********************************************************************/
662 static int spi_flash_reset_ub(void)
664 unsigned char buf[4];
666 if (flashFamily == FLASH_UNDEFINED)
667 return FLASH_API_ERROR;
668 /* set device to write disabled */
671 while (spi_flash_status() != STATUS_READY) {}
673 return(FLASH_API_OK);
676 /*********************************************************************/
677 /* Usefull funtion to return the number of sectors in the device. */
678 /* Can be used for functions which need to loop among all the */
679 /* sectors, or wish to know the number of the last sector. */
680 /*********************************************************************/
682 static int spi_flash_get_numsectors(void)
684 return meminfo.nsect;
687 /*********************************************************************/
688 /* flash_get_sector_size() is provided for cases in which the size */
689 /* of a sector is required by a host application. The sector size */
690 /* (in bytes) is returned in the data location pointed to by the */
691 /* 'size' parameter. */
692 /*********************************************************************/
694 static int spi_flash_get_sector_size(unsigned short sector)
696 return meminfo.sec[sector].size;
699 /*********************************************************************/
700 /* The purpose of get_flash_memptr() is to return a memory pointer */
701 /* which points to the beginning of memory space allocated for the */
702 /* flash. All function pointers are then referenced from this */
705 /* Different systems will implement this in different ways: */
706 /* possibilities include: */
707 /* - A direct memory pointer */
708 /* - A pointer to a memory map */
709 /* - A pointer to a hardware port from which the linear */
710 /* address is translated */
711 /* - Output of an MMU function / service */
713 /* Also note that this function expects the pointer to a specific */
714 /* sector of the device. This can be provided by dereferencing */
715 /* the pointer from a translated offset of the sector from a */
716 /* global base pointer (e.g. flashptr = base_pointer + sector_offset)*/
718 /* Important: Many AMD flash devices need both bank and or sector */
719 /* address bits to be correctly set (bank address bits are A18-A16, */
720 /* and sector address bits are A18-A12, or A12-A15). Flash parts */
721 /* which do not need these bits will ignore them, so it is safe to */
722 /* assume that every part will require these bits to be set. */
723 /*********************************************************************/
725 static unsigned char *spi_get_flash_memptr(unsigned short sector)
727 unsigned char *memptr = (unsigned char*)
728 (FLASH_BASE + meminfo.sec[sector].base);
733 static unsigned char *spi_flash_get_memptr(unsigned short sector)
735 return( spi_get_flash_memptr(sector) );
738 /*********************************************************************/
739 /* Flash_write extends the functionality of flash_program() by */
740 /* providing an faster way to program multiple data words, without */
741 /* needing the function overhead of looping algorithms which */
742 /* program word by word. This function utilizes fast pointers */
743 /* to quickly loop through bulk data. */
744 /*********************************************************************/
745 static int spi_flash_write(unsigned short sector, int offset, unsigned char *buf,
748 unsigned char wbuf[64];
756 addr = (unsigned int) spi_get_flash_memptr(sector);
760 switch (flashFamily) {
762 if( sstAaiWordProgram == FALSE )
764 /* Auto Address Increment one byte at a time. */
765 spi_flash_ub(sector); /* enable write */
772 wbuf[1] = (unsigned char)((dst & 0x00ff0000) >> 16);
773 wbuf[2] = (unsigned char)((dst & 0x0000ff00) >> 8);
774 wbuf[3] = (unsigned char)(dst & 0x000000ff);
778 spi_write(wbuf, 1+cmdlen);
779 while (spi_flash_status() != STATUS_READY) {}
780 pbuf++; /* update address and count by one byte */
783 spi_flash_reset_ub();
784 while (spi_flash_status() != STATUS_READY) {}
788 /* Auto Address Increment one word (2 bytes) at a time. */
789 spi_flash_ub(sector); /* enable write */
790 wbuf[0] = FLASH_WORD_AAI;
794 wbuf[2] = *(pbuf + 1);
797 wbuf[1] = (unsigned char)((dst & 0x00ff0000) >> 16);
798 wbuf[2] = (unsigned char)((dst & 0x0000ff00) >> 8);
799 wbuf[3] = (unsigned char)(dst & 0x000000ff);
801 wbuf[5] = *(pbuf + 1);
804 spi_write(wbuf, cmdlen);
805 while (spi_flash_status() != STATUS_READY) {}
806 pbuf += 2; /* update address and count by two bytes */
809 spi_flash_reset_ub();
810 while (spi_flash_status() != STATUS_READY) {}
816 spi_flash_ub(sector); /* enable write */
817 maxwrite = (nbytes < (sizeof(SPI->spiMsgData)-CMD_LEN_4))
818 ? nbytes : (sizeof(SPI->spiMsgData)-CMD_LEN_4);
819 /* maxwrite is limit to page boundary */
820 pagelimit = FLASH_PAGE_SIZE - (dst & 0x000000ff);
821 maxwrite = (maxwrite < pagelimit) ? maxwrite : pagelimit;
823 wbuf[0] = FLASH_PROG;
824 wbuf[1] = (unsigned char)((dst & 0x00ff0000) >> 16);
825 wbuf[2] = (unsigned char)((dst & 0x0000ff00) >> 8);
826 wbuf[3] = (unsigned char)(dst & 0x000000ff);
827 memcpy(&wbuf[4], pbuf, maxwrite);
828 spi_write(wbuf, maxwrite+CMD_LEN_4);
829 while (spi_flash_status() != STATUS_READY) {}
838 spi_flash_ub(sector); /* enable write */
839 maxwrite = (nbytes < (sizeof(SPI->spiMsgData)-CMD_LEN_4))
840 ? nbytes : (sizeof(SPI->spiMsgData)-CMD_LEN_4);
841 /* maxwrite is limit to page boundary */
842 pagelimit = FLASH_PAGE_SIZE - (dst & 0x000000ff);
843 maxwrite = (maxwrite < pagelimit) ? maxwrite : pagelimit;
845 wbuf[0] = FLASH_PROG;
846 wbuf[1] = (unsigned char)((dst & 0x00ff0000) >> 16);
847 wbuf[2] = (unsigned char)((dst & 0x0000ff00) >> 8);
848 wbuf[3] = (unsigned char)(dst & 0x000000ff);
849 memcpy(&wbuf[4], pbuf, maxwrite);
850 spi_write(wbuf, maxwrite+CMD_LEN_4);
851 while (spi_flash_status() != STATUS_READY) {}
865 /*********************************************************************/
866 /* Flash_status return an appropriate status code */
867 /*********************************************************************/
869 static int spi_flash_status(void)
871 unsigned char buf[4];
875 /* check device is ready */
878 rc = spi_read(buf, 1, 1);
879 if (rc == SPI_STATUS_OK) {
880 if (!(buf[0] & SR_RDY)) {
889 return STATUS_TIMEOUT;
892 /*********************************************************************/
893 /* flash_get_device_id() return the device id of the component. */
894 /*********************************************************************/
896 static unsigned short spi_flash_get_device_id(unsigned short sector)
898 unsigned char buf[4];
902 for (i = 0; i < MAX_RETRY; i++) {
903 /* read product id command */
904 buf[0] = SST_FLASH_RDID;
909 spi_read(buf, prependcnt, sizeof(unsigned short));
910 while (spi_flash_status() != STATUS_READY) {}
912 if (buf[0] == SSTPART) {
913 return( *(unsigned short *)&buf[0] );
917 for (i = 0; i < MAX_RETRY; i++) {
918 buf[0] = ATMEL_FLASH_RDID;
920 spi_read(buf, prependcnt, sizeof(unsigned short));
921 while (spi_flash_status() != STATUS_READY) {}
923 if (buf[0] == ATMELPART) {
924 return( *(unsigned short *)&buf[0] );
928 for (i = 0; i < MAX_RETRY; i++) {
929 buf[0] = AMD_FLASH_RDID;
934 spi_read(buf, prependcnt, sizeof(unsigned short));
935 while (spi_flash_status() != STATUS_READY) {}
937 if (buf[0] == AMD_S25FL002D) {
938 return( *(unsigned short *)&buf[0] );
942 /* return manufacturer code and device code */
943 return( *(unsigned short *)&buf[0] );
946 /*********************************************************************/
947 /* The purpose of flash_get_blk() is to return a the block number */
948 /* for a given memory address. */
949 /*********************************************************************/
951 static int spi_flash_get_blk(int addr)
954 int last_blk = spi_flash_get_numsectors();
955 int relative_addr = addr - (int) FLASH_BASE;
957 for(blk_start=0, i=0; i < relative_addr && blk_start < last_blk; blk_start++)
958 i += spi_flash_get_sector_size(blk_start);
960 if( (unsigned int)i > (unsigned int)relative_addr ) {
961 blk_start--; // last blk, dec by 1
963 if( blk_start == last_blk )
965 printk("Address is too big.\n");
973 /************************************************************************/
974 /* The purpose of flash_get_total_size() is to return the total size of */
976 /************************************************************************/
977 static int spi_flash_get_total_size(void)
982 static int spi_flash_get_total_memory_mapped_size(void)
984 return((totalSize < MAX_MEMORY_MAPPED_SIZE)
985 ? totalSize : MAX_MEMORY_MAPPED_SIZE);