added files
[bcm963xx.git] / shared / src / 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      (1024 * 1024)
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 AT25F512_SECTOR     2
54 #define AT25F2048_SECTOR    4
55 #define AMD25FL_SECTOR      4
56
57 /* use 60 instead 63 for aligned on word (4 bytes) boundaries */
58 #define MAX_READ            60
59 #define CMD_LEN_1           1
60 #define CMD_LEN_4           4
61
62 /* Standard Boolean declarations */
63 #define TRUE                1
64 #define FALSE               0
65
66 #define SPI_STATUS_OK               0
67 #define SPI_STATUS_INVALID_LEN      -1
68
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 */
79
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 */
83
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 */
87
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 */
91
92 /* RDSR return status bit definition */
93 #define SR_WPEN             0x80
94 #define SR_BP2              0x10
95 #define SR_BP1              0x08
96 #define SR_BP0              0x04
97 #define SR_WEN              0x02
98 #define SR_RDY              0x01
99
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 */
105
106 /* Used to mask of bytes from word data */
107 #define HIGH_BYTE(a)        (a >> 8)
108 #define LOW_BYTE(a)         (a & 0xFF)
109
110 /* Define different type of flash */
111 #define FLASH_UNDEFINED     0
112 #define FLASH_SST           1
113 #define FLASH_ATMEL         2
114 #define FLASH_AMD           3
115
116 /* ATMEL's manufacturer ID */
117 #define ATMELPART           0x1F
118
119 /* A list of ATMEL device ID's - add others as needed */
120 #define ID_AT25F512         0x60
121 #define ID_AT25F2048        0x63
122
123 /* AMD's device ID */
124 #define AMD_S25FL002D       0x11
125
126 /* SST's manufacturer ID */
127 #define SSTPART             0xBF
128
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
134
135 #define SPI_MAKE_ID(A,B)    \
136     (((unsigned short) (A) << 8) | ((unsigned short) B & 0xff))
137
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"},   \
146      {0,""}                                                 \
147     }
148
149
150 /** Structs. **/
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
154  * of the sectors.
155  */
156 struct flashinfo {
157     char *name;         /* "AT25F512", etc. */
158     unsigned long addr; /* physical address, once translated */
159     int nsect;          /* # of sectors */
160     struct {
161         long size;      /* # of bytes in this sector */
162         long base;      /* offset from beginning of device */
163     } sec[MAXSECTORS];  /* per-sector info */
164 };
165
166 struct flash_name_from_id {
167     unsigned short fnfi_id;
168     char fnfi_name[30];
169 };
170
171
172 /** Prototypes. **/
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,
190     int nbytes,int ub);
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);
196
197
198 /** Variables. **/
199 static flash_device_info_t flash_spi_dev =
200     {
201         0xffff,
202         "",
203         spi_flash_sector_erase_int,
204         spi_flash_read_buf,
205         spi_flash_write_buf,
206         spi_flash_get_numsectors,
207         spi_flash_get_sector_size,
208         spi_flash_get_memptr,
209         spi_flash_get_blk,
210         spi_flash_get_total_size,
211         spi_flash_get_total_memory_mapped_size
212     };
213
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;
225
226 static int spi_read( unsigned char *msg_buf, int prependcnt, int nbytes )
227 {
228     int i;
229     SPI->spiMsgCtl = (nbytes << SPI_BYTE_CNT_SHIFT |
230                             HALF_DUPLEX_R << SPI_MSG_TYPE_SHIFT);
231     
232     for (i = 0; i < prependcnt; i++)
233         SPI->spiMsgData[i] = msg_buf[i];
234
235     SPI->spiIntStatus = SPI_INTR_CLEAR_ALL;
236
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 );
240
241
242     while (!(SPI->spiIntStatus & SPI_INTR_CMD_DONE));
243
244     SPI->spiIntStatus = SPI_INTR_CLEAR_ALL;
245
246     for(i = 0; i < nbytes; i++) {
247         msg_buf[i] = SPI->spiRxDataFifo[i];
248     }
249     return SPI_STATUS_OK;
250 }
251
252 static int spi_write( unsigned char *msg_buf, int nbytes )
253 {
254     int i;
255
256     SPI->spiMsgCtl = (nbytes << SPI_BYTE_CNT_SHIFT |
257                     HALF_DUPLEX_W << SPI_MSG_TYPE_SHIFT);
258
259     for (i = 0; i < nbytes; i++)
260         SPI->spiMsgData[i] = msg_buf[i];
261
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 );
265
266     while (!(SPI->spiIntStatus & SPI_INTR_CMD_DONE));
267
268     SPI->spiIntStatus = SPI_INTR_CLEAR_ALL;
269
270     return SPI_STATUS_OK;
271 }
272
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.                                               */
279 /*                                                                   */
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.                               */
286 /*                                                                   */
287 /* The meminfo struct occupies 44 bytes of heap space, depending     */
288 /* on the value of the define MAXSECTORS.  Adjust to suit            */
289 /* application                                                       */ 
290 /*********************************************************************/
291
292 int spi_flash_init(flash_device_info_t **flash_info)
293 {
294     struct flash_name_from_id fnfi[] = SPI_FLASH_DEVICES;
295     struct flash_name_from_id *fnfi_ptr;
296     int i=0, count=0;
297     int basecount=0L;
298     unsigned short device_id;
299     unsigned short blkEnables;
300     int sectorsize = 0;
301     int numsector = 0;
302
303     *flash_info = &flash_spi_dev;
304
305 #if 0
306     /* 
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
311      */
312     flash_sector_erase_int(0);
313     flash_sector_erase_int(1);
314     printk("flash_init: erase all sectors\n");
315     return FLASH_API_OK;
316 #endif
317     blkEnables = PERF->blkEnables;
318     if ((blkEnables & SPI_CLK_EN) == 0) {
319         blkEnables |= SPI_CLK_EN;
320         PERF->blkEnables = blkEnables;
321     }
322
323     flash_spi_dev.flash_device_id = device_id = spi_flash_get_device_id(0);
324
325     if ((((char)(device_id >> 8)) == ATMELPART)) {
326         flashFamily = FLASH_ATMEL;
327         switch ((char)(device_id & 0x00ff)) {
328             case ID_AT25F512:
329                 numsector = AT25F512_SECTOR;
330                 sectorsize = SECTOR_SIZE_32K;
331                 break;
332             case ID_AT25F2048:
333                 numsector = AT25F2048_SECTOR;
334                 sectorsize = SECTOR_SIZE_64K;
335                 break;
336             default:
337                 break;
338         }
339     }
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)) {
344             case ID_SST25VF016B:
345                 numsector = SST25VF016B_SECTOR;
346                 sstAaiWordProgram = TRUE;
347                 break;
348             case ID_SST25VF080:
349                 numsector = SST25VF080_SECTOR;
350                 break;
351             case ID_SST25VF040:
352                 numsector = SST25VF040_SECTOR;
353                 break;
354             case ID_SST25VF020:
355             default:
356                 numsector = SST25VF020_SECTOR;
357                 break;
358         }
359     }
360     else if (((char)(device_id >> 8)) == (char)AMD_S25FL002D) {
361             flashFamily = FLASH_AMD;
362             numsector = AMD25FL_SECTOR;
363             sectorsize = SECTOR_SIZE_64K;
364             device_id &= 0xff00;
365     } 
366     else {
367             meminfo.addr = 0L;
368             meminfo.nsect = 1;
369             meminfo.sec[0].size = SECTOR_SIZE_4K;
370             meminfo.sec[0].base = 0x00000;
371             return FLASH_API_ERROR;
372     }
373
374     meminfo.addr = 0L;
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;
380         count++;
381     }
382     totalSize = meminfo.sec[count-1].base + meminfo.sec[count-1].size;
383
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 ); 
387             break;
388         }
389     }
390
391     return (FLASH_API_OK);
392 }
393
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 /*********************************************************************/
400
401 static int spi_flash_sector_erase_int(unsigned short sector)
402 {
403     unsigned char buf[4];
404     unsigned int addr;
405     int rc;
406
407     if (flashFamily == FLASH_UNDEFINED)
408         return FLASH_API_ERROR;
409
410     /* set device to write enabled */
411     spi_flash_reset();
412     spi_flash_ub(sector);
413
414     switch (flashFamily) {
415         case FLASH_SST:
416             buf[0] = SST_FLASH_SERASE;
417             break;
418         case FLASH_ATMEL:
419             buf[0] = ATMEL_FLASH_SERASE;
420             break;
421         case FLASH_AMD:
422             buf[0] = AMD_FLASH_SERASE;
423             break;
424     };
425
426
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));
433
434     /* check device is ready */
435     if (rc == SPI_STATUS_OK) {
436         while (spi_flash_status() != STATUS_READY) {}
437     }
438
439     return(FLASH_API_OK);
440 }
441
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 /*********************************************************************/
448
449 #if 0 /* not used */
450 unsigned char spi_flash_chip_erase_int(void)
451 {
452     unsigned char buf[4];
453     int rc;
454
455     if (flashFamily == FLASH_UNDEFINED)
456         return FLASH_API_ERROR;
457
458     /* set device to write enabled */
459     buf[0] = FLASH_WREN;
460     rc = spi_write(buf, 1);
461
462     /* check device is ready */
463     if (rc == SPI_STATUS_OK) {
464         do {
465             buf[0] = FLASH_RDSR;
466             rc = spi_read(buf, 1, 1);
467             if (rc == SPI_STATUS_OK) {
468                 if (buf[0] & SR_WEN) {
469                     break;
470                 }
471             } else {
472                 break;
473             }
474         } while (1);
475     }
476     
477     switch (flashFamily) {
478         case FLASH_SST:
479             buf[0] = SST_FLASH_CERASE;
480             break;
481         case FLASH_ATMEL:
482             buf[0] = ATMEL_FLASH_CERASE;
483             break;
484         case FLASH_AMD:
485             buf[0] = AMD_FLASH_CERASE;
486             break;
487     };
488     /* erase the sector  */
489     rc = spi_write(buf, 1);
490
491     /* check device is ready */
492     if (rc == SPI_STATUS_OK) {
493         while (spi_flash_status() != STATUS_READY) {}
494     }
495
496     return(FLASH_API_OK);
497 }
498 #endif
499
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 /*********************************************************************/
505
506 static int spi_flash_reset(void)
507 {
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);
513 }
514
515 /*********************************************************************/
516 /* flash_read_buf() reads buffer of data from the specified          */
517 /* offset from the sector parameter.                                 */
518 /*********************************************************************/
519
520 static int spi_flash_read_buf(unsigned short sector, int offset,
521     unsigned char *buffer, int numbytes)
522 {
523     unsigned char buf[MAX_READ];
524     unsigned int addr;
525     int maxread;
526     int idx;
527
528     if (flashFamily == FLASH_UNDEFINED)
529         return FLASH_API_ERROR;
530
531     spi_flash_reset();
532
533     addr = (unsigned int) spi_get_flash_memptr(sector);
534     addr += offset;
535     idx = 0;
536     while (numbytes) {
537         maxread = (numbytes < sizeof(buf)) ? numbytes : sizeof(buf);
538         buf[0] = FLASH_READ;
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);
545         idx += maxread;
546         numbytes -= maxread;
547         addr += maxread;
548     }
549
550     return (FLASH_API_OK);
551 }
552
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 /*********************************************************************/
559
560 static int spi_flash_ub(unsigned short sector)
561 {
562     unsigned char buf[4];
563     int rc;
564
565     do {
566         buf[0] = FLASH_RDSR;
567         rc = spi_read(buf, 1, 1);
568
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));
573             } else {
574                 break;
575             }
576         } else {
577             break;
578         }
579     } while (1);
580
581     /* set device to write enabled */
582     buf[0] = FLASH_WREN;
583     rc = spi_write(buf, 1);
584
585     /* check device is ready */
586     if (rc == SPI_STATUS_OK) {
587         while (spi_flash_status() != STATUS_READY) {}
588         do {
589             buf[0] = FLASH_RDSR;
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) {
594                     break;
595                 }
596             } else {
597                 break;
598             }
599         } while (1);
600     }
601
602     return(FLASH_API_OK);
603 }
604
605 static int spi_flash_write_status(unsigned char status)
606 {
607     unsigned char buf[4];
608     int rc = SPI_STATUS_OK;
609
610     if (flashFamily == FLASH_UNDEFINED)
611         return FLASH_API_ERROR;
612
613     switch (flashFamily) {
614         case FLASH_SST:
615             buf[0] = FLASH_EWSR;
616             rc = spi_write(buf, 1);
617             break;
618         default:
619             break;
620     }
621     if (rc == SPI_STATUS_OK) {
622         buf[0] = FLASH_WRST;
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) {}
627     }
628
629     return FLASH_API_OK;
630 }
631
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 */
637 /* observed.                                                         */
638 /*********************************************************************/
639
640 static int spi_flash_write_buf(unsigned short sector, int offset,
641     unsigned char *buffer, int numbytes)
642 {
643     int ret = FLASH_API_ERROR;
644
645     if (flashFamily == FLASH_UNDEFINED)
646         return FLASH_API_ERROR;
647
648     ret = spi_flash_write(sector, offset, buffer, numbytes, TRUE);
649
650     if( ret == -1 )
651         printk( "Flash write error.  Verify failed\n" );
652
653     return( ret );
654 }
655
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 /*********************************************************************/
661
662 static int spi_flash_reset_ub(void)
663 {
664     unsigned char buf[4];
665
666     if (flashFamily == FLASH_UNDEFINED)
667         return FLASH_API_ERROR;
668     /* set device to write disabled */
669     buf[0] = FLASH_WRDI;
670     spi_write(buf, 1);
671     while (spi_flash_status() != STATUS_READY) {}
672
673     return(FLASH_API_OK);
674 }
675
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 /*********************************************************************/
681
682 static int spi_flash_get_numsectors(void)
683 {
684     return meminfo.nsect;
685 }
686
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 /*********************************************************************/
693
694 static int spi_flash_get_sector_size(unsigned short sector)
695 {
696     return meminfo.sec[sector].size;
697 }
698
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       */
703 /* pointer.                                  */
704 /*                                                                   */
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                            */
712 /*                                                                   */
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)*/
717 /*                                                                   */
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 /*********************************************************************/
724
725 static unsigned char *spi_get_flash_memptr(unsigned short sector)
726 {
727     unsigned char *memptr = (unsigned char*)
728         (FLASH_BASE + meminfo.sec[sector].base);
729
730     return (memptr);
731 }
732
733 static unsigned char *spi_flash_get_memptr(unsigned short sector)
734 {
735     return( spi_get_flash_memptr(sector) );
736 }
737
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,
746     int nbytes, int ub)
747 {
748     unsigned char wbuf[64];
749     unsigned int addr;
750     unsigned int dst;
751     unsigned char *pbuf;
752     int cmdlen;
753     int maxwrite;
754     int pagelimit;
755
756     addr = (unsigned int) spi_get_flash_memptr(sector);
757     dst = addr + offset;
758
759     pbuf = buf;
760     switch (flashFamily) {
761         case FLASH_SST:
762             if( sstAaiWordProgram == FALSE )
763             {
764                 /* Auto Address Increment one byte at a time. */
765                 spi_flash_ub(sector); /* enable write */
766                 wbuf[0] = FLASH_AAI;
767                 while (nbytes) {
768                     if (pbuf != buf) {
769                         wbuf[1] = *pbuf; 
770                         cmdlen = CMD_LEN_1;
771                     } else {
772                         wbuf[1] = (unsigned char)((dst & 0x00ff0000) >> 16);
773                         wbuf[2] = (unsigned char)((dst & 0x0000ff00) >> 8);
774                         wbuf[3] = (unsigned char)(dst & 0x000000ff);
775                         wbuf[4] = *pbuf;
776                         cmdlen = CMD_LEN_4;
777                     }
778                     spi_write(wbuf, 1+cmdlen);
779                     while (spi_flash_status() != STATUS_READY) {}
780                     pbuf++; /* update address and count by one byte */
781                     nbytes--;
782                 }
783                 spi_flash_reset_ub();
784                 while (spi_flash_status() != STATUS_READY) {}
785             }
786             else
787             {
788                 /* Auto Address Increment one word (2 bytes) at a time. */
789                 spi_flash_ub(sector); /* enable write */
790                 wbuf[0] = FLASH_WORD_AAI;
791                 while (nbytes) {
792                     if (pbuf != buf) {
793                         wbuf[1] = *pbuf; 
794                         wbuf[2] = *(pbuf + 1); 
795                         cmdlen = 3;
796                     } else {
797                         wbuf[1] = (unsigned char)((dst & 0x00ff0000) >> 16);
798                         wbuf[2] = (unsigned char)((dst & 0x0000ff00) >> 8);
799                         wbuf[3] = (unsigned char)(dst & 0x000000ff);
800                         wbuf[4] = *pbuf;
801                         wbuf[5] = *(pbuf + 1); 
802                         cmdlen = 6;
803                     }
804                     spi_write(wbuf, cmdlen);
805                     while (spi_flash_status() != STATUS_READY) {}
806                     pbuf += 2; /* update address and count by two bytes */
807                     nbytes -= 2;
808                 }
809                 spi_flash_reset_ub();
810                 while (spi_flash_status() != STATUS_READY) {}
811             }
812             break;
813
814         case FLASH_ATMEL:
815             while (nbytes) {
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;
822
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) {}
830                 pbuf += maxwrite;
831                 nbytes -= maxwrite;
832                 dst += maxwrite;
833             }
834             break;
835
836         case FLASH_AMD:
837             while (nbytes) {
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;
844
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) {}
852                 pbuf += maxwrite;
853                 nbytes -= maxwrite;
854                 dst += maxwrite;
855             }
856             break;
857
858         default:
859             return 0;
860     }
861
862     return (pbuf-buf);
863 }
864
865 /*********************************************************************/
866 /* Flash_status return an appropriate status code                    */
867 /*********************************************************************/
868
869 static int spi_flash_status(void)
870 {
871     unsigned char buf[4];
872     int rc;
873     int retry = 10;
874
875     /* check device is ready */
876     do {
877         buf[0] = FLASH_RDSR;
878         rc = spi_read(buf, 1, 1);
879         if (rc == SPI_STATUS_OK) {
880             if (!(buf[0] & SR_RDY)) {
881                 return STATUS_READY;
882             }
883         } else {
884             return STATUS_ERROR;
885         }
886         OSL_DELAY(10);
887     } while (retry--);
888
889     return STATUS_TIMEOUT;
890 }
891
892 /*********************************************************************/
893 /* flash_get_device_id() return the device id of the component.      */
894 /*********************************************************************/
895
896 static unsigned short spi_flash_get_device_id(unsigned short sector)
897 {
898     unsigned char buf[4];
899     int prependcnt;
900     int i;
901
902     for (i = 0; i < MAX_RETRY; i++) {
903         /* read product id command */
904         buf[0] = SST_FLASH_RDID;
905         buf[1] = 0;
906         buf[2] = 0;
907         buf[3] = 0;
908         prependcnt = 4;
909         spi_read(buf, prependcnt, sizeof(unsigned short));
910         while (spi_flash_status() != STATUS_READY) {}
911
912         if (buf[0] == SSTPART) {
913             return( *(unsigned short *)&buf[0] );
914         }
915     }
916
917     for (i = 0; i < MAX_RETRY; i++) {
918         buf[0] = ATMEL_FLASH_RDID;
919         prependcnt = 1;
920         spi_read(buf, prependcnt, sizeof(unsigned short));
921         while (spi_flash_status() != STATUS_READY) {}
922
923         if (buf[0] == ATMELPART) {
924             return( *(unsigned short *)&buf[0] );
925         }
926     }
927
928     for (i = 0; i < MAX_RETRY; i++) {
929         buf[0] = AMD_FLASH_RDID;
930         buf[1] = 0;
931         buf[2] = 0;
932         buf[3] = 0;
933         prependcnt = 4;
934         spi_read(buf, prependcnt, sizeof(unsigned short));
935         while (spi_flash_status() != STATUS_READY) {}
936
937         if (buf[0] == AMD_S25FL002D) {
938             return( *(unsigned short *)&buf[0] );
939         }
940     }
941
942     /* return manufacturer code and device code */
943     return( *(unsigned short *)&buf[0] );
944 }
945
946 /*********************************************************************/
947 /* The purpose of flash_get_blk() is to return a the block number */
948 /* for a given memory address.                                       */
949 /*********************************************************************/
950
951 static int spi_flash_get_blk(int addr)
952 {
953     int blk_start, i;
954     int last_blk = spi_flash_get_numsectors();
955     int relative_addr = addr - (int) FLASH_BASE;
956
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);
959
960     if( (unsigned int)i > (unsigned int)relative_addr ) {
961         blk_start--;        // last blk, dec by 1
962     } else {
963         if( blk_start == last_blk )
964         {
965             printk("Address is too big.\n");
966             blk_start = -1;
967         }
968     }
969
970     return( blk_start );
971 }
972
973 /************************************************************************/
974 /* The purpose of flash_get_total_size() is to return the total size of */
975 /* the flash                                                            */
976 /************************************************************************/
977 static int spi_flash_get_total_size(void)
978 {
979     return totalSize;
980 }
981
982 static int spi_flash_get_total_memory_mapped_size(void)
983 {
984     return((totalSize < MAX_MEMORY_MAPPED_SIZE)
985         ? totalSize : MAX_MEMORY_MAPPED_SIZE);
986 }
987