www.usr.com/support/gpl/USR9113_release1.0.tar.gz
[bcm963xx.git] / shared / opensource / flash / spiflash.c
1 /************************************************************************/
2 /*                                                                      */
3 /*  SPI Flash Memory Drivers                                            */
4 /*  File name: spiflash.c                                               */
5 /*  Revision:  1.0  1/27/2004                                           */
6 /*                                                                      */
7 /************************************************************************/                        
8
9 /** Includes. **/
10 #ifdef _CFE_                                                
11 #include "lib_types.h"
12 #include "lib_printf.h"
13 #include "lib_string.h"
14 #include "bcm_map.h"  
15 #define printk  printf
16 #else       // linux
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> 
22 #endif
23
24 #include "bcmtypes.h"
25 #include "board.h"
26 #include "flash_api.h"
27
28
29 /** Defines. **/
30 #define OSL_DELAY(X)                        \
31     do { { int i; for( i = 0; i < (X) * 500; i++ ) ; } } while(0)
32
33 #define MAX_RETRY           3
34
35 #define FAR
36
37 #ifndef NULL
38 #define NULL 0
39 #endif
40
41 #define MAX_MEMORY_MAPPED_SIZE  0
42
43 #define MAXSECTORS          1024    /* maximum number of sectors supported */
44
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
60
61 /* use 60 instead 63 for aligned on word (4 bytes) boundaries */
62 #define MAX_READ            60
63 #define CMD_LEN_1           1
64 #define CMD_LEN_4           4
65
66 /* Standard Boolean declarations */
67 #define TRUE                1
68 #define FALSE               0
69
70 #define SPI_STATUS_OK               0
71 #define SPI_STATUS_INVALID_LEN      -1
72
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 */
83
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 */
87
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 */
91
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 */
95
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 */
99
100 #define ST_FLASH_RDID       0x9F   /* read manufacturer and product id */
101
102 /* RDSR return status bit definition */
103 #define SR_WPEN             0x80
104 #define SR_BP2              0x10
105 #define SR_BP1              0x08
106 #define SR_BP0              0x04
107 #define SR_WEN              0x02
108 #define SR_RDY              0x01
109
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 */
115
116 /* Used to mask of bytes from word data */
117 #define HIGH_BYTE(a)        (a >> 8)
118 #define LOW_BYTE(a)         (a & 0xFF)
119
120 /* Define different type of flash */
121 #define FLASH_UNDEFINED     0
122 #define FLASH_SST           1
123 #define FLASH_ATMEL         2
124 #define FLASH_AMD           3
125 #define FLASH_SPAN          4
126
127 /* ATMEL's manufacturer ID */
128 #define ATMELPART           0x1F
129
130 /* A list of ATMEL device ID's - add others as needed */
131 #define ID_AT25F512         0x60
132 #define ID_AT25F2048        0x63
133
134 /* AMD's device ID */
135 #define AMD_S25FL002D       0x11
136
137 /* SST's manufacturer ID */
138 #define SSTPART             0xBF
139
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
145
146 /* NexFlash's manufacturer ID */
147 #define NXPART              0xEF
148
149 /* A list of NexFlash device ID's - add others as needed */
150 #define ID_NX25P20          0x11
151 #define ID_NX25P40          0x12
152
153
154 /* StFlash's manufacturer ID */
155 #define STPART              0x12 
156
157 /* A list of StFlash device ID's - add others as needed */
158 #define ID_M25P40           0x12
159
160 /* SPANSION manufacturer ID */
161 #define SPANPART            0x01
162 #define SPANPART2           0x02
163
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
169
170
171 #define SPI_MAKE_ID(A,B)    \
172     (((unsigned short) (A) << 8) | ((unsigned short) B & 0xff))
173
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"}, \
189      {0,""}                                                 \
190     }
191
192
193 /** Structs. **/
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
197  * of the sectors.
198  */
199 struct flashinfo {
200     char *name;         /* "AT25F512", etc. */
201     unsigned long addr; /* physical address, once translated */
202     int nsect;          /* # of sectors */
203     struct {
204         long size;      /* # of bytes in this sector */
205         long base;      /* offset from beginning of device */
206     } sec[MAXSECTORS];  /* per-sector info */
207 };
208
209 struct flash_name_from_id {
210     unsigned short fnfi_id;
211     char fnfi_name[30];
212 };
213
214
215 /** Prototypes. **/
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,
233     int nbytes,int ub);
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);
239
240
241 /** Variables. **/
242 static flash_device_info_t flash_spi_dev =
243     {
244         0xffff,
245         "",
246         spi_flash_sector_erase_int,
247         spi_flash_read_buf,
248         spi_flash_write_buf,
249         spi_flash_get_numsectors,
250         spi_flash_get_sector_size,
251         spi_flash_get_memptr,
252         spi_flash_get_blk,
253         spi_flash_get_total_size,
254         spi_flash_get_total_memory_mapped_size
255     };
256
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;
268
269 static int spi_read( unsigned char *msg_buf, int prependcnt, int nbytes )
270 {
271     int i;
272     SPI->spiMsgCtl = (nbytes << SPI_BYTE_CNT_SHIFT |
273                             HALF_DUPLEX_R << SPI_MSG_TYPE_SHIFT);
274     
275     for (i = 0; i < prependcnt; i++)
276         SPI->spiMsgData[i] = msg_buf[i];
277
278     SPI->spiIntStatus = SPI_INTR_CLEAR_ALL;
279
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 );
283
284
285     while (!(SPI->spiIntStatus & SPI_INTR_CMD_DONE));
286
287     SPI->spiIntStatus = SPI_INTR_CLEAR_ALL;
288
289     for(i = 0; i < nbytes; i++) {
290         msg_buf[i] = SPI->spiRxDataFifo[i];
291     }
292     return SPI_STATUS_OK;
293 }
294
295 static int spi_write( unsigned char *msg_buf, int nbytes )
296 {
297     int i;
298
299     SPI->spiMsgCtl = (nbytes << SPI_BYTE_CNT_SHIFT |
300                     HALF_DUPLEX_W << SPI_MSG_TYPE_SHIFT);
301
302     for (i = 0; i < nbytes; i++)
303         SPI->spiMsgData[i] = msg_buf[i];
304
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 );
308
309     while (!(SPI->spiIntStatus & SPI_INTR_CMD_DONE));
310
311     SPI->spiIntStatus = SPI_INTR_CLEAR_ALL;
312
313     return SPI_STATUS_OK;
314 }
315
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.                                               */
322 /*                                                                   */
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.                               */
329 /*                                                                   */
330 /* The meminfo struct occupies 44 bytes of heap space, depending     */
331 /* on the value of the define MAXSECTORS.  Adjust to suit            */
332 /* application                                                       */ 
333 /*********************************************************************/
334
335 int spi_flash_init(flash_device_info_t **flash_info)
336 {
337     struct flash_name_from_id fnfi[] = SPI_FLASH_DEVICES;
338     struct flash_name_from_id *fnfi_ptr;
339     int i=0, count=0;
340     int basecount=0L;
341     unsigned short device_id;
342     unsigned short blkEnables;
343     int sectorsize = 0;
344     int numsector = 0;
345
346     *flash_info = &flash_spi_dev;
347
348 #if 0
349     /* 
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
354      */
355     flash_sector_erase_int(0);
356     flash_sector_erase_int(1);
357     printk("flash_init: erase all sectors\n");
358     return FLASH_API_OK;
359 #endif
360     blkEnables = PERF->blkEnables;
361     if ((blkEnables & SPI_CLK_EN) == 0) {
362         blkEnables |= SPI_CLK_EN;
363         PERF->blkEnables = blkEnables;
364     }
365
366     flash_spi_dev.flash_device_id = device_id = spi_flash_get_device_id(0);
367
368     switch( device_id >> 8 ) {
369     case ATMELPART:
370         flashFamily = FLASH_ATMEL;
371         switch ((char)(device_id & 0x00ff)) {
372             case ID_AT25F512:
373                 numsector = AT25F512_SECTOR;
374                 sectorsize = SECTOR_SIZE_32K;
375                 break;
376             case ID_AT25F2048:
377                 numsector = AT25F2048_SECTOR;
378                 sectorsize = SECTOR_SIZE_64K;
379                 break;
380             default:
381                 break;
382         }
383         break;
384
385     case SSTPART:
386         flashFamily = FLASH_SST;
387         sectorsize = SECTOR_SIZE_4K;
388         switch ((unsigned char)(device_id & 0x00ff)) {
389             case ID_SST25VF016B:
390                 numsector = SST25VF016B_SECTOR;
391                 sstAaiWordProgram = TRUE;
392                 break;
393             case ID_SST25VF080:
394                 numsector = SST25VF080_SECTOR;
395                 break;
396             case ID_SST25VF040:
397                 numsector = SST25VF040_SECTOR;
398                 break;
399             case ID_SST25VF020:
400             default:
401                 numsector = SST25VF020_SECTOR;
402                 break;
403         }
404         break;
405
406     case AMD_S25FL002D:
407         flashFamily = FLASH_AMD;
408         numsector = AMD25FL_SECTOR;
409         sectorsize = SECTOR_SIZE_64K;
410         device_id &= 0xff00;
411         break;
412
413     case NXPART:
414         /* NexFlash parts are AMD compatible. */
415         flashFamily = FLASH_AMD;
416         sectorsize = SECTOR_SIZE_64K;
417         switch ((unsigned char)(device_id & 0x00ff)) {
418             case ID_NX25P20:
419                 numsector = 4; /* 4 * 64KB == 256KB flash size */
420                 break;
421             case ID_NX25P40:
422                 numsector = 8; /* 8 * 64KB == 512KB flash size */
423                 break;
424         }
425         break;
426         
427     case STPART:
428         /* StFlash parts are AMD compatible. */
429         flashFamily = FLASH_AMD;
430         sectorsize = SECTOR_SIZE_64K;
431         
432         numsector = 8; /* 8 * 64KB == 512KB flash size */
433         break;
434
435     case SPANPART:
436         flashFamily = FLASH_SPAN;
437         sectorsize = SECTOR_SIZE_64K;
438         switch ((unsigned short)(device_id & 0x00ff)) {
439             case ID_SPAN25FL032A:
440                 numsector = SPAN25FL032A_SECTOR;
441                 break;
442             case ID_SPAN25FL016A:
443                 numsector = SPAN25FL016A_SECTOR;
444                 break;
445             case ID_SPAN25FL008A:
446                 numsector = SPAN25FL008A_SECTOR;
447                 break;
448             case ID_SPAN25FL004A:
449             default:
450                 numsector = SPAN25FL004A_SECTOR;
451                 break;
452         }
453     break;
454
455     default:
456         meminfo.addr = 0L;
457         meminfo.nsect = 1;
458         meminfo.sec[0].size = SECTOR_SIZE_4K;
459         meminfo.sec[0].base = 0x00000;
460         return FLASH_API_ERROR;
461     }
462
463     meminfo.addr = 0L;
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;
469         count++;
470     }
471     totalSize = meminfo.sec[count-1].base + meminfo.sec[count-1].size;
472
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 ); 
476             break;
477         }
478     }
479     return (FLASH_API_OK);
480 }
481
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 /*********************************************************************/
488
489 static int spi_flash_sector_erase_int(unsigned short sector)
490 {
491     unsigned char buf[4];
492     unsigned int addr;
493     int rc;
494
495     if (flashFamily == FLASH_UNDEFINED)
496         return FLASH_API_ERROR;
497
498     /* set device to write enabled */
499     spi_flash_reset();
500     spi_flash_ub(sector);
501
502     switch (flashFamily) {
503         case FLASH_SST:
504             buf[0] = SST_FLASH_SERASE;
505             break;
506         case FLASH_ATMEL:
507             buf[0] = ATMEL_FLASH_SERASE;
508             break;
509         case FLASH_AMD:
510             buf[0] = AMD_FLASH_SERASE;
511             break;
512         case FLASH_SPAN:
513             buf[0] = SPAN_FLASH_SERASE;
514             break;
515     };
516
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));
523
524     /* check device is ready */
525     if (rc == SPI_STATUS_OK) {
526         while (spi_flash_status() != STATUS_READY) {}
527     }
528     return(FLASH_API_OK);
529 }
530
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 /*********************************************************************/
537
538 #if 0 /* not used */
539 unsigned char spi_flash_chip_erase_int(void)
540 {
541     unsigned char buf[4];
542     int rc;
543
544     if (flashFamily == FLASH_UNDEFINED)
545         return FLASH_API_ERROR;
546
547     /* set device to write enabled */
548     buf[0] = FLASH_WREN;
549     rc = spi_write(buf, 1);
550
551     /* check device is ready */
552     if (rc == SPI_STATUS_OK) {
553         do {
554             buf[0] = FLASH_RDSR;
555             rc = spi_read(buf, 1, 1);
556             if (rc == SPI_STATUS_OK) {
557                 if (buf[0] & SR_WEN) {
558                     break;
559                 }
560             } else {
561                 break;
562             }
563         } while (1);
564     }
565     
566     switch (flashFamily) {
567         case FLASH_SST:
568             buf[0] = SST_FLASH_CERASE;
569             break;
570         case FLASH_ATMEL:
571             buf[0] = ATMEL_FLASH_CERASE;
572             break;
573         case FLASH_AMD:
574             buf[0] = AMD_FLASH_CERASE;
575             break;
576     };
577     /* erase the sector  */
578     rc = spi_write(buf, 1);
579
580     /* check device is ready */
581     if (rc == SPI_STATUS_OK) {
582         while (spi_flash_status() != STATUS_READY) {}
583     }
584
585     return(FLASH_API_OK);
586 }
587 #endif
588
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 /*********************************************************************/
594
595 static int spi_flash_reset(void)
596 {
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);
602 }
603
604 /*********************************************************************/
605 /* flash_read_buf() reads buffer of data from the specified          */
606 /* offset from the sector parameter.                                 */
607 /*********************************************************************/
608
609 static int spi_flash_read_buf(unsigned short sector, int offset,
610     unsigned char *buffer, int numbytes)
611 {
612     unsigned char buf[MAX_READ];
613     unsigned int addr;
614     int maxread;
615     int idx;
616
617     if (flashFamily == FLASH_UNDEFINED)
618         return FLASH_API_ERROR;
619
620     spi_flash_reset();
621
622     addr = (unsigned int) spi_get_flash_memptr(sector);
623     addr += offset;
624     idx = 0;
625     while (numbytes > 0) {
626         maxread = (numbytes < sizeof(buf)) ? numbytes : sizeof(buf);
627         buf[0] = FLASH_READ;
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);
634         idx += maxread;
635         numbytes -= maxread;
636         addr += maxread;
637     }
638
639     return (FLASH_API_OK);
640 }
641
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 /*********************************************************************/
648
649 static int spi_flash_ub(unsigned short sector)
650 {
651     unsigned char buf[4];
652     int rc;
653
654     do {
655         buf[0] = FLASH_RDSR;
656         rc = spi_read(buf, 1, 1);
657
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));
662             } else {
663                 break;
664             }
665         } else {
666             break;
667         }
668     } while (1);
669
670     /* set device to write enabled */
671     buf[0] = FLASH_WREN;
672     rc = spi_write(buf, 1);
673
674     /* check device is ready */
675     if (rc == SPI_STATUS_OK) {
676         while (spi_flash_status() != STATUS_READY) {}
677         do {
678             buf[0] = FLASH_RDSR;
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) {
683                     break;
684                 }
685             } else {
686                 break;
687             }
688         } while (1);
689     }
690
691     return(FLASH_API_OK);
692 }
693
694 static int spi_flash_write_status(unsigned char status)
695 {
696     unsigned char buf[4];
697     int rc = SPI_STATUS_OK;
698
699     if (flashFamily == FLASH_UNDEFINED)
700         return FLASH_API_ERROR;
701
702     switch (flashFamily) {
703         case FLASH_SST:
704             buf[0] = FLASH_EWSR;
705             rc = spi_write(buf, 1);
706             break;
707         case FLASH_SPAN:
708             buf[0] = FLASH_WREN;
709             rc = spi_write(buf, 1);
710             break;
711         default:
712             break;
713     }
714     if (rc == SPI_STATUS_OK) {
715         buf[0] = FLASH_WRST;
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) {}
720     }
721
722     return FLASH_API_OK;
723 }
724
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 */
730 /* observed.                                                         */
731 /*********************************************************************/
732
733 static int spi_flash_write_buf(unsigned short sector, int offset,
734     unsigned char *buffer, int numbytes)
735 {
736     int ret = FLASH_API_ERROR;
737
738     if (flashFamily == FLASH_UNDEFINED)
739         return FLASH_API_ERROR;
740
741     ret = spi_flash_write(sector, offset, buffer, numbytes, TRUE);
742
743     if( ret == -1 )
744         printk( "Flash write error.  Verify failed\n" );
745
746     return( ret );
747 }
748
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 /*********************************************************************/
754
755 static int spi_flash_reset_ub(void)
756 {
757     unsigned char buf[4];
758
759     if (flashFamily == FLASH_UNDEFINED)
760         return FLASH_API_ERROR;
761     /* set device to write disabled */
762     buf[0] = FLASH_WRDI;
763     spi_write(buf, 1);
764     while (spi_flash_status() != STATUS_READY) {}
765
766     return(FLASH_API_OK);
767 }
768
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 /*********************************************************************/
774
775 static int spi_flash_get_numsectors(void)
776 {
777     return meminfo.nsect;
778 }
779
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 /*********************************************************************/
786
787 static int spi_flash_get_sector_size(unsigned short sector)
788 {
789     return meminfo.sec[sector].size;
790 }
791
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       */
796 /* pointer.                                  */
797 /*                                                                   */
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                            */
805 /*                                                                   */
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)*/
810 /*                                                                   */
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 /*********************************************************************/
817
818 static unsigned char *spi_get_flash_memptr(unsigned short sector)
819 {
820     unsigned char *memptr = (unsigned char*)
821         (FLASH_BASE + meminfo.sec[sector].base);
822
823     return (memptr);
824 }
825
826 static unsigned char *spi_flash_get_memptr(unsigned short sector)
827 {
828     return( spi_get_flash_memptr(sector) );
829 }
830
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,
839     int nbytes, int ub)
840 {
841     unsigned char wbuf[FLASH_PAGE_SIZE+32];
842     unsigned int addr;
843     unsigned int dst;
844     unsigned char *pbuf;
845     int cmdlen;
846     int maxwrite;
847     int pagelimit;
848
849     addr = (unsigned int) spi_get_flash_memptr(sector);
850     dst = addr + offset;
851
852     pbuf = buf;
853     switch (flashFamily) {
854         case FLASH_SST:
855             if( sstAaiWordProgram == FALSE )
856             {
857                 /* Auto Address Increment one byte at a time. */
858                 spi_flash_ub(sector); /* enable write */
859                 wbuf[0] = FLASH_AAI;
860                 while (nbytes) {
861                     if (pbuf != buf) {
862                         wbuf[1] = *pbuf; 
863                         cmdlen = CMD_LEN_1;
864                     } else {
865                         wbuf[1] = (unsigned char)((dst & 0x00ff0000) >> 16);
866                         wbuf[2] = (unsigned char)((dst & 0x0000ff00) >> 8);
867                         wbuf[3] = (unsigned char)(dst & 0x000000ff);
868                         wbuf[4] = *pbuf;
869                         cmdlen = CMD_LEN_4;
870                     }
871                     spi_write(wbuf, 1+cmdlen);
872                     while (spi_flash_status() != STATUS_READY) {}
873                     pbuf++; /* update address and count by one byte */
874                     nbytes--;
875                 }
876                 spi_flash_reset_ub();
877                 while (spi_flash_status() != STATUS_READY) {}
878             }
879             else
880             {
881                 /* Auto Address Increment one word (2 bytes) at a time. */
882                 spi_flash_ub(sector); /* enable write */
883                 wbuf[0] = FLASH_WORD_AAI;
884                 while (nbytes) {
885                     if (pbuf != buf) {
886                         wbuf[1] = *pbuf; 
887                         wbuf[2] = *(pbuf + 1); 
888                         cmdlen = 3;
889                     } else {
890                         wbuf[1] = (unsigned char)((dst & 0x00ff0000) >> 16);
891                         wbuf[2] = (unsigned char)((dst & 0x0000ff00) >> 8);
892                         wbuf[3] = (unsigned char)(dst & 0x000000ff);
893                         wbuf[4] = *pbuf;
894                         wbuf[5] = *(pbuf + 1); 
895                         cmdlen = 6;
896                     }
897                     spi_write(wbuf, cmdlen);
898                     while (spi_flash_status() != STATUS_READY) {}
899                     pbuf += 2; /* update address and count by two bytes */
900                     nbytes -= 2;
901                 }
902                 spi_flash_reset_ub();
903                 while (spi_flash_status() != STATUS_READY) {}
904             }
905             break;
906
907         case FLASH_ATMEL:
908         case FLASH_AMD:
909         case FLASH_SPAN:
910             while (nbytes > 0) {
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;
917
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) {}
925                 pbuf += maxwrite;
926                 nbytes -= maxwrite;
927                 dst += maxwrite;
928             }
929             break;
930
931         default:
932             return 0;
933     }
934     return (pbuf-buf);
935 }
936
937 /*********************************************************************/
938 /* Flash_status return an appropriate status code                    */
939 /*********************************************************************/
940
941 static int spi_flash_status(void)
942 {
943     unsigned char buf[4];
944     int rc;
945     int retry = 10;
946
947     /* check device is ready */
948     do {
949         buf[0] = FLASH_RDSR;
950         rc = spi_read(buf, 1, 1);
951         if (rc == SPI_STATUS_OK) {
952             if (!(buf[0] & SR_RDY)) {
953                 return STATUS_READY;
954             }
955         } else {
956             return STATUS_ERROR;
957         }
958         OSL_DELAY(10);
959     } while (retry--);
960
961     return STATUS_TIMEOUT;
962 }
963
964 /*********************************************************************/
965 /* flash_get_device_id() return the device id of the component.      */
966 /*********************************************************************/
967
968 static unsigned short spi_flash_get_device_id(unsigned short sector)
969 {
970     unsigned char buf[4];
971     int prependcnt;
972     int i, j;
973
974     for (i = 0; i < MAX_RETRY; i++) {
975         /* read product id command */
976         buf[0] = SST_FLASH_RDID;
977         buf[1] = 0;
978         buf[2] = 0;
979         buf[3] = 0;
980         prependcnt = 4;
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] );
986             }
987         }
988     }
989     spi_flash_reset();
990
991     for (i = 0; i < MAX_RETRY; i++) {
992         buf[0] = ATMEL_FLASH_RDID;
993         prependcnt = 1;
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] );
999             }
1000         }
1001     }
1002     spi_flash_reset();
1003
1004     for (i = 0; i < MAX_RETRY; i++) {
1005         buf[0] = SPAN_FLASH_RDID;
1006         prependcnt = 1;
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)) {
1011                     buf[1] = buf[2];
1012                     return( *(unsigned short *)&buf[0] );
1013                 }
1014             }
1015         }
1016     }
1017     spi_flash_reset();
1018
1019     // AMD_FLASH_RDID is the same as RES command for SPAN,
1020     // so it has to be the last one.
1021
1022     for (i = 0; i < MAX_RETRY; i++) {
1023         buf[0] = AMD_FLASH_RDID;
1024         buf[1] = 0;
1025         buf[2] = 0;
1026         buf[3] = 0;
1027         prependcnt = 4;
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] );
1033             }
1034         }
1035      }
1036     /* return manufacturer code and device code */
1037     return( *(unsigned short *)&buf[0] );
1038 }
1039
1040 /*********************************************************************/
1041 /* The purpose of flash_get_blk() is to return a the block number */
1042 /* for a given memory address.                                       */
1043 /*********************************************************************/
1044
1045 static int spi_flash_get_blk(int addr)
1046 {
1047     int blk_start, i;
1048     int last_blk = spi_flash_get_numsectors();
1049     int relative_addr = addr - (int) FLASH_BASE;
1050
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);
1053
1054     if( (unsigned int)i > (unsigned int)relative_addr ) {
1055         blk_start--;        // last blk, dec by 1
1056     } else {
1057         if( blk_start == last_blk )
1058         {
1059             printk("Address is too big.\n");
1060             blk_start = -1;
1061         }
1062     }
1063
1064     return( blk_start );
1065 }
1066
1067 /************************************************************************/
1068 /* The purpose of flash_get_total_size() is to return the total size of */
1069 /* the flash                                                            */
1070 /************************************************************************/
1071 static int spi_flash_get_total_size(void)
1072 {
1073     return totalSize;
1074 }
1075
1076 static int spi_flash_get_total_memory_mapped_size(void)
1077 {
1078     return((totalSize < MAX_MEMORY_MAPPED_SIZE)
1079         ? totalSize : MAX_MEMORY_MAPPED_SIZE);
1080 }
1081