import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / mtd / chips / jedec.c
1
2 /* JEDEC Flash Interface.
3  * This is an older type of interface for self programming flash. It is 
4  * commonly use in older AMD chips and is obsolete compared with CFI.
5  * It is called JEDEC because the JEDEC association distributes the ID codes
6  * for the chips.
7  *
8  * See the AMD flash databook for information on how to operate the interface.
9  *
10  * This code does not support anything wider than 8 bit flash chips, I am
11  * not going to guess how to send commands to them, plus I expect they will
12  * all speak CFI..
13  *
14  * $Id: jedec.c,v 1.1.1.1 2005/04/11 02:50:25 jack Exp $
15  */
16
17 #include <linux/mtd/jedec.h>
18
19 static struct mtd_info *jedec_probe(struct map_info *);
20 static int jedec_probe8(struct map_info *map,unsigned long base,
21                   struct jedec_private *priv);
22 static int jedec_probe16(struct map_info *map,unsigned long base,
23                   struct jedec_private *priv);
24 static int jedec_probe32(struct map_info *map,unsigned long base,
25                   struct jedec_private *priv);
26 static void jedec_flash_chip_scan(struct jedec_private *priv,unsigned long start,
27                             unsigned long len);
28 static int flash_erase(struct mtd_info *mtd, struct erase_info *instr);
29 static int flash_write(struct mtd_info *mtd, loff_t start, size_t len,
30                        size_t *retlen, const u_char *buf);
31
32 static unsigned long my_bank_size;
33
34 /* Listing of parts and sizes. We need this table to learn the sector
35    size of the chip and the total length */
36 static const struct JEDECTable JEDEC_table[] = 
37   {{0x013D,"AMD Am29F017D",2*1024*1024,64*1024,MTD_CAP_NORFLASH},
38    {0x01AD,"AMD Am29F016",2*1024*1024,64*1024,MTD_CAP_NORFLASH},
39    {0x01D5,"AMD Am29F080",1*1024*1024,64*1024,MTD_CAP_NORFLASH},
40    {0x01A4,"AMD Am29F040",512*1024,64*1024,MTD_CAP_NORFLASH},
41    {0x014F,"AMD Am29LV040B",512*1024,64*1024,MTD_CAP_NORFLASH},
42    {0x20E3,"AMD Am29W040B",512*1024,64*1024,MTD_CAP_NORFLASH},
43    {0xC2AD,"Macronix MX29F016",2*1024*1024,64*1024,MTD_CAP_NORFLASH},
44    {}};
45
46 static const struct JEDECTable *jedec_idtoinf(__u8 mfr,__u8 id);
47 static void jedec_sync(struct mtd_info *mtd) {};
48 static int jedec_read(struct mtd_info *mtd, loff_t from, size_t len, 
49                       size_t *retlen, u_char *buf);
50 static int jedec_read_banked(struct mtd_info *mtd, loff_t from, size_t len, 
51                              size_t *retlen, u_char *buf);
52
53 static struct mtd_info *jedec_probe(struct map_info *map);
54
55
56
57 static struct mtd_chip_driver jedec_chipdrv = {
58         probe: jedec_probe,
59         name: "jedec",
60         module: THIS_MODULE
61 };
62
63 /* Probe entry point */
64
65 static struct mtd_info *jedec_probe(struct map_info *map)
66 {
67    struct mtd_info *MTD;
68    struct jedec_private *priv;
69    unsigned long Base;
70    unsigned long SectorSize;
71    unsigned count;
72    unsigned I,Uniq;
73    char Part[200];
74    memset(&priv,0,sizeof(priv));
75
76    MTD = kmalloc(sizeof(struct mtd_info) + sizeof(struct jedec_private), GFP_KERNEL);
77    if (!MTD)
78            return NULL;
79
80    memset(MTD, 0, sizeof(struct mtd_info) + sizeof(struct jedec_private));
81    priv = (struct jedec_private *)&MTD[1];
82    
83    my_bank_size = map->size;
84
85    if (map->size/my_bank_size > MAX_JEDEC_CHIPS)
86    {
87       printk("mtd: Increase MAX_JEDEC_CHIPS, too many banks.\n");
88       kfree(MTD);
89       return 0;
90    }
91    
92    for (Base = 0; Base < map->size; Base += my_bank_size)
93    {
94       // Perhaps zero could designate all tests?
95       if (map->buswidth == 0)
96          map->buswidth = 1;
97       
98       if (map->buswidth == 1){
99          if (jedec_probe8(map,Base,priv) == 0) {
100                  printk("did recognize jedec chip\n");
101                  kfree(MTD);
102                  return 0;
103          }
104       }
105       if (map->buswidth == 2)
106          jedec_probe16(map,Base,priv);
107       if (map->buswidth == 4)
108          jedec_probe32(map,Base,priv);
109    }
110    
111    // Get the biggest sector size
112    SectorSize = 0;
113    for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
114    {
115            //      printk("priv->chips[%d].jedec is %x\n",I,priv->chips[I].jedec);
116            //      printk("priv->chips[%d].sectorsize is %lx\n",I,priv->chips[I].sectorsize);
117       if (priv->chips[I].sectorsize > SectorSize)
118          SectorSize = priv->chips[I].sectorsize;
119    }
120    
121    // Quickly ensure that the other sector sizes are factors of the largest
122    for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
123    {
124       if ((SectorSize/priv->chips[I].sectorsize)*priv->chips[I].sectorsize != SectorSize)
125       {
126          printk("mtd: Failed. Device has incompatible mixed sector sizes\n");
127          kfree(MTD);
128          return 0;
129       }      
130    }
131    
132    /* Generate a part name that includes the number of different chips and
133       other configuration information */
134    count = 1;
135    strncpy(Part,map->name,sizeof(Part)-10);
136    Part[sizeof(Part)-11] = 0;
137    strcat(Part," ");
138    Uniq = 0;
139    for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
140    {
141       const struct JEDECTable *JEDEC;
142       
143       if (priv->chips[I+1].jedec == priv->chips[I].jedec)
144       {
145          count++;
146          continue;
147       }
148       
149       // Locate the chip in the jedec table
150       JEDEC = jedec_idtoinf(priv->chips[I].jedec >> 8,priv->chips[I].jedec);
151       if (JEDEC == 0)
152       {
153          printk("mtd: Internal Error, JEDEC not set\n");
154          kfree(MTD);
155          return 0;
156       }
157       
158       if (Uniq != 0)
159          strcat(Part,",");
160       Uniq++;
161       
162       if (count != 1)
163          sprintf(Part+strlen(Part),"%x*[%s]",count,JEDEC->name);
164       else
165          sprintf(Part+strlen(Part),"%s",JEDEC->name);
166       if (strlen(Part) > sizeof(Part)*2/3)
167          break;
168       count = 1;
169    }   
170
171    /* Determine if the chips are organized in a linear fashion, or if there
172       are empty banks. Note, the last bank does not count here, only the
173       first banks are important. Holes on non-bank boundaries can not exist
174       due to the way the detection algorithm works. */
175    if (priv->size < my_bank_size)
176       my_bank_size = priv->size;
177    priv->is_banked = 0;
178    //printk("priv->size is %x, my_bank_size is %x\n",priv->size,my_bank_size);
179    //printk("priv->bank_fill[0] is %x\n",priv->bank_fill[0]);
180    if (!priv->size) {
181            printk("priv->size is zero\n");
182            kfree(MTD);
183            return 0;
184    }
185    if (priv->size/my_bank_size) {
186            if (priv->size/my_bank_size == 1) {
187                    priv->size = my_bank_size;
188            }
189            else {
190                    for (I = 0; I != priv->size/my_bank_size - 1; I++)
191                    {
192                       if (priv->bank_fill[I] != my_bank_size)
193                          priv->is_banked = 1;
194                       
195                       /* This even could be eliminated, but new de-optimized read/write
196                          functions have to be written */
197                       printk("priv->bank_fill[%d] is %lx, priv->bank_fill[0] is %lx\n",I,priv->bank_fill[I],priv->bank_fill[0]);
198                       if (priv->bank_fill[I] != priv->bank_fill[0])
199                       {
200                          printk("mtd: Failed. Cannot handle unsymmetric banking\n");
201                          kfree(MTD);
202                          return 0;
203                       }      
204                    }
205            }
206    }
207    if (priv->is_banked == 1)
208       strcat(Part,", banked");
209
210    //   printk("Part: '%s'\n",Part);
211    
212    memset(MTD,0,sizeof(*MTD));
213   // strncpy(MTD->name,Part,sizeof(MTD->name));
214   // MTD->name[sizeof(MTD->name)-1] = 0;
215    MTD->name = map->name;
216    MTD->type = MTD_NORFLASH;
217    MTD->flags = MTD_CAP_NORFLASH;
218    MTD->erasesize = SectorSize*(map->buswidth);
219    //   printk("MTD->erasesize is %x\n",(unsigned int)MTD->erasesize);
220    MTD->size = priv->size;
221    //   printk("MTD->size is %x\n",(unsigned int)MTD->size);
222    //MTD->module = THIS_MODULE; // ? Maybe this should be the low level module?
223    MTD->erase = flash_erase;
224    if (priv->is_banked == 1)
225       MTD->read = jedec_read_banked;
226    else
227       MTD->read = jedec_read;
228    MTD->write = flash_write;
229    MTD->sync = jedec_sync;
230    MTD->priv = map;
231    map->fldrv_priv = priv;
232    map->fldrv = &jedec_chipdrv;
233    MOD_INC_USE_COUNT;
234    return MTD;
235 }
236
237 /* Helper for the JEDEC function, JEDEC numbers all have odd parity */
238 static int checkparity(u_char C)
239 {
240    u_char parity = 0;
241    while (C != 0)
242    {
243       parity ^= C & 1;
244       C >>= 1;
245    }
246
247    return parity == 1;
248 }
249
250
251 /* Take an array of JEDEC numbers that represent interleved flash chips
252    and process them. Check to make sure they are good JEDEC numbers, look
253    them up and then add them to the chip list */   
254 static int handle_jedecs(struct map_info *map,__u8 *Mfg,__u8 *Id,unsigned Count,
255                   unsigned long base,struct jedec_private *priv)
256 {
257    unsigned I,J;
258    unsigned long Size;
259    unsigned long SectorSize;
260    const struct JEDECTable *JEDEC;
261
262    // Test #2 JEDEC numbers exhibit odd parity
263    for (I = 0; I != Count; I++)
264    {
265       if (checkparity(Mfg[I]) == 0 || checkparity(Id[I]) == 0)
266          return 0;
267    }
268    
269    // Finally, just make sure all the chip sizes are the same
270    JEDEC = jedec_idtoinf(Mfg[0],Id[0]);
271    
272    if (JEDEC == 0)
273    {
274       printk("mtd: Found JEDEC flash chip, but do not have a table entry for %x:%x\n",Mfg[0],Mfg[1]);
275       return 0;
276    }
277    
278    Size = JEDEC->size;
279    SectorSize = JEDEC->sectorsize;
280    for (I = 0; I != Count; I++)
281    {
282       JEDEC = jedec_idtoinf(Mfg[0],Id[0]);
283       if (JEDEC == 0)
284       {
285          printk("mtd: Found JEDEC flash chip, but do not have a table entry for %x:%x\n",Mfg[0],Mfg[1]);
286          return 0;
287       }
288
289       if (Size != JEDEC->size || SectorSize != JEDEC->sectorsize)
290       {
291          printk("mtd: Failed. Interleved flash does not have matching characteristics\n");
292          return 0;
293       }      
294    }
295
296    // Load the Chips
297    for (I = 0; I != MAX_JEDEC_CHIPS; I++)
298    {
299       if (priv->chips[I].jedec == 0)
300          break;
301    }
302
303    if (I + Count > MAX_JEDEC_CHIPS)
304    {
305       printk("mtd: Device has too many chips. Increase MAX_JEDEC_CHIPS\n");
306       return 0;
307    }      
308    
309    // Add them to the table
310    for (J = 0; J != Count; J++)
311    {
312       unsigned long Bank;
313          
314       JEDEC = jedec_idtoinf(Mfg[J],Id[J]);
315       priv->chips[I].jedec = (Mfg[J] << 8) | Id[J];
316       priv->chips[I].size = JEDEC->size;
317       priv->chips[I].sectorsize = JEDEC->sectorsize;
318       priv->chips[I].base = base + J;
319       priv->chips[I].datashift = J*8;
320       priv->chips[I].capabilities = JEDEC->capabilities;
321       priv->chips[I].offset = priv->size + J;
322
323       // log2 n :|
324       priv->chips[I].addrshift = 0;
325       for (Bank = Count; Bank != 1; Bank >>= 1, priv->chips[I].addrshift++);
326       
327       // Determine how filled this bank is.
328       Bank = base & (~(my_bank_size-1));
329       if (priv->bank_fill[Bank/my_bank_size] < base + 
330           (JEDEC->size << priv->chips[I].addrshift) - Bank)
331          priv->bank_fill[Bank/my_bank_size] =  base + (JEDEC->size << priv->chips[I].addrshift) - Bank;
332       I++;
333    }
334
335    priv->size += priv->chips[I-1].size*Count;
336          
337    return priv->chips[I-1].size;
338 }
339
340 /* Lookup the chip information from the JEDEC ID table. */
341 static const struct JEDECTable *jedec_idtoinf(__u8 mfr,__u8 id)
342 {
343    __u16 Id = (mfr << 8) | id;
344    unsigned long I = 0;
345    for (I = 0; JEDEC_table[I].jedec != 0; I++)
346       if (JEDEC_table[I].jedec == Id)
347          return JEDEC_table + I;
348    return 0;
349 }
350
351 // Look for flash using an 8 bit bus interface
352 static int jedec_probe8(struct map_info *map,unsigned long base,
353                   struct jedec_private *priv)
354
355    #define flread(x) map->read8(map,base+x)
356    #define flwrite(v,x) map->write8(map,v,base+x)
357
358    const unsigned long AutoSel1 = 0xAA;
359    const unsigned long AutoSel2 = 0x55;
360    const unsigned long AutoSel3 = 0x90;
361    const unsigned long Reset = 0xF0;
362    __u32 OldVal;
363    __u8 Mfg[1];
364    __u8 Id[1];
365    unsigned I;
366    unsigned long Size;
367
368    // Wait for any write/erase operation to settle
369    OldVal = flread(base);
370    for (I = 0; OldVal != flread(base) && I < 10000; I++)
371       OldVal = flread(base);
372    
373    // Reset the chip
374    flwrite(Reset,0x555); 
375    
376    // Send the sequence
377    flwrite(AutoSel1,0x555);
378    flwrite(AutoSel2,0x2AA);
379    flwrite(AutoSel3,0x555);
380    
381    //  Get the JEDEC numbers
382    Mfg[0] = flread(0);
383    Id[0] = flread(1);
384    //   printk("Mfg is %x, Id is %x\n",Mfg[0],Id[0]);
385       
386    Size = handle_jedecs(map,Mfg,Id,1,base,priv);
387    //   printk("handle_jedecs Size is %x\n",(unsigned int)Size);
388    if (Size == 0)
389    {
390       flwrite(Reset,0x555);
391       return 0;
392    }
393    
394
395    // Reset.
396    flwrite(Reset,0x555);
397    
398    return 1;
399    
400    #undef flread
401    #undef flwrite
402 }
403
404 // Look for flash using a 16 bit bus interface (ie 2 8-bit chips)
405 static int jedec_probe16(struct map_info *map,unsigned long base,
406                   struct jedec_private *priv)
407 {
408    return 0;
409 }
410
411 // Look for flash using a 32 bit bus interface (ie 4 8-bit chips)
412 static int jedec_probe32(struct map_info *map,unsigned long base,
413                   struct jedec_private *priv)
414 {
415    #define flread(x) map->read32(map,base+((x)<<2))
416    #define flwrite(v,x) map->write32(map,v,base+((x)<<2))
417
418    const unsigned long AutoSel1 = 0xAAAAAAAA;
419    const unsigned long AutoSel2 = 0x55555555;
420    const unsigned long AutoSel3 = 0x90909090;
421    const unsigned long Reset = 0xF0F0F0F0;
422    __u32 OldVal;
423    __u8 Mfg[4];
424    __u8 Id[4];
425    unsigned I;
426    unsigned long Size;
427
428    // Wait for any write/erase operation to settle
429    OldVal = flread(base);
430    for (I = 0; OldVal != flread(base) && I < 10000; I++)
431       OldVal = flread(base);
432    
433    // Reset the chip
434    flwrite(Reset,0x555); 
435    
436    // Send the sequence
437    flwrite(AutoSel1,0x555);
438    flwrite(AutoSel2,0x2AA);
439    flwrite(AutoSel3,0x555);
440    
441    // Test #1, JEDEC numbers are readable from 0x??00/0x??01
442    if (flread(0) != flread(0x100) || 
443        flread(1) != flread(0x101))
444    {
445       flwrite(Reset,0x555);
446       return 0;
447    }
448
449    // Split up the JEDEC numbers
450    OldVal = flread(0);
451    for (I = 0; I != 4; I++)
452       Mfg[I] = (OldVal >> (I*8));
453    OldVal = flread(1);
454    for (I = 0; I != 4; I++)
455       Id[I] = (OldVal >> (I*8));
456       
457    Size = handle_jedecs(map,Mfg,Id,4,base,priv);
458    if (Size == 0)
459    {
460       flwrite(Reset,0x555);
461       return 0;
462    }
463    
464    /* Check if there is address wrap around within a single bank, if this
465       returns JEDEC numbers then we assume that it is wrap around. Notice
466       we call this routine with the JEDEC return still enabled, if two or
467       more flashes have a truncated address space the probe test will still
468       work */
469    if (base + (Size<<2)+0x555 < map->size &&
470        base + (Size<<2)+0x555 < (base & (~(my_bank_size-1))) + my_bank_size)
471    {
472       if (flread(base+Size) != flread(base+Size + 0x100) ||
473           flread(base+Size + 1) != flread(base+Size + 0x101))
474       {
475          jedec_probe32(map,base+Size,priv);
476       }
477    }
478
479    // Reset.
480    flwrite(0xF0F0F0F0,0x555);
481    
482    return 1;
483    
484    #undef flread
485    #undef flwrite
486 }
487
488 /* Linear read. */
489 static int jedec_read(struct mtd_info *mtd, loff_t from, size_t len, 
490                       size_t *retlen, u_char *buf)
491 {
492    struct map_info *map = (struct map_info *)mtd->priv;
493    
494    map->copy_from(map, buf, from, len);
495    *retlen = len;
496    return 0;   
497 }
498
499 /* Banked read. Take special care to jump past the holes in the bank
500    mapping. This version assumes symetry in the holes.. */
501 static int jedec_read_banked(struct mtd_info *mtd, loff_t from, size_t len, 
502                              size_t *retlen, u_char *buf)
503 {
504    struct map_info *map = (struct map_info *)mtd->priv;
505    struct jedec_private *priv = (struct jedec_private *)map->fldrv_priv;
506
507    *retlen = 0;
508    while (len > 0)
509    {
510       // Determine what bank and offset into that bank the first byte is
511       unsigned long bank = from & (~(priv->bank_fill[0]-1));
512       unsigned long offset = from & (priv->bank_fill[0]-1);
513       unsigned long get = len;
514       if (priv->bank_fill[0] - offset < len)
515          get = priv->bank_fill[0] - offset;
516
517       bank /= priv->bank_fill[0];      
518       map->copy_from(map,buf + *retlen,bank*my_bank_size + offset,get);
519       
520       len -= get;
521       *retlen += get;
522       from += get;
523    }   
524    return 0;   
525 }
526
527 /* Pass the flags value that the flash return before it re-entered read 
528    mode. */
529 static void jedec_flash_failed(unsigned char code)
530 {
531    /* Bit 5 being high indicates that there was an internal device
532       failure, erasure time limits exceeded or something */
533    if ((code & (1 << 5)) != 0)
534    {
535       printk("mtd: Internal Flash failure\n");
536       return;
537    }
538    printk("mtd: Programming didn't take\n");
539 }
540
541 /* This uses the erasure function described in the AMD Flash Handbook, 
542    it will work for flashes with a fixed sector size only. Flashes with
543    a selection of sector sizes (ie the AMD Am29F800B) will need a different
544    routine. This routine tries to parallize erasing multiple chips/sectors 
545    where possible */
546 static int flash_erase(struct mtd_info *mtd, struct erase_info *instr)
547 {
548    // Does IO to the currently selected chip
549    #define flread(x) map->read8(map,chip->base+((x)<<chip->addrshift))
550    #define flwrite(v,x) map->write8(map,v,chip->base+((x)<<chip->addrshift))
551    
552    unsigned long Time = 0;
553    unsigned long NoTime = 0;
554    unsigned long start = instr->addr, len = instr->len;
555    unsigned int I;
556    struct map_info *map = (struct map_info *)mtd->priv;
557    struct jedec_private *priv = (struct jedec_private *)map->fldrv_priv;
558
559    // Verify the arguments..
560    if (start + len > mtd->size ||
561        (start % mtd->erasesize) != 0 ||
562        (len % mtd->erasesize) != 0 ||
563        (len/mtd->erasesize) == 0)
564       return -EINVAL;
565    
566    jedec_flash_chip_scan(priv,start,len);
567
568    // Start the erase sequence on each chip
569    for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
570    {
571       unsigned long off;
572       struct jedec_flash_chip *chip = priv->chips + I;
573       
574       if (chip->length == 0)
575          continue;
576       
577       if (chip->start + chip->length > chip->size)
578       {
579          printk("DIE\n");
580          return -EIO;
581       }     
582       
583       flwrite(0xF0,chip->start + 0x555);
584       flwrite(0xAA,chip->start + 0x555);
585       flwrite(0x55,chip->start + 0x2AA);
586       flwrite(0x80,chip->start + 0x555);
587       flwrite(0xAA,chip->start + 0x555);
588       flwrite(0x55,chip->start + 0x2AA);
589
590       /* Once we start selecting the erase sectors the delay between each 
591          command must not exceed 50us or it will immediately start erasing 
592          and ignore the other sectors */
593       for (off = 0; off < len; off += chip->sectorsize)
594       {
595          // Check to make sure we didn't timeout
596          flwrite(0x30,chip->start + off);
597          if (off == 0)
598             continue;
599          if ((flread(chip->start + off) & (1 << 3)) != 0)
600          {
601             printk("mtd: Ack! We timed out the erase timer!\n");
602             return -EIO;
603          }               
604       }
605    }   
606
607    /* We could split this into a timer routine and return early, performing
608       background erasure.. Maybe later if the need warrents */
609
610    /* Poll the flash for erasure completion, specs say this can take as long
611       as 480 seconds to do all the sectors (for a 2 meg flash). 
612       Erasure time is dependant on chip age, temp and wear.. */
613    
614    /* This being a generic routine assumes a 32 bit bus. It does read32s
615       and bundles interleved chips into the same grouping. This will work 
616       for all bus widths */
617    Time = 0;
618    NoTime = 0;
619    for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
620    {
621       struct jedec_flash_chip *chip = priv->chips + I;
622       unsigned long off = 0;
623       unsigned todo[4] = {0,0,0,0};
624       unsigned todo_left = 0;
625       unsigned J;
626       
627       if (chip->length == 0)
628          continue;
629
630       /* Find all chips in this data line, realistically this is all 
631          or nothing up to the interleve count */
632       for (J = 0; priv->chips[J].jedec != 0 && J < MAX_JEDEC_CHIPS; J++)
633       {
634          if ((priv->chips[J].base & (~((1<<chip->addrshift)-1))) == 
635              (chip->base & (~((1<<chip->addrshift)-1))))
636          {
637             todo_left++;
638             todo[priv->chips[J].base & ((1<<chip->addrshift)-1)] = 1;
639          }       
640       }
641
642       /*      printk("todo: %x %x %x %x\n",(short)todo[0],(short)todo[1],
643               (short)todo[2],(short)todo[3]);
644       */
645       while (1)
646       {
647          __u32 Last[4];
648          unsigned long Count = 0;
649          
650          /* During erase bit 7 is held low and bit 6 toggles, we watch this,
651             should it stop toggling or go high then the erase is completed,
652             or this is not really flash ;> */
653          switch (map->buswidth) {
654          case 1:
655             Last[0] = map->read8(map,(chip->base >> chip->addrshift) + chip->start + off);
656             Last[1] = map->read8(map,(chip->base >> chip->addrshift) + chip->start + off);
657             Last[2] = map->read8(map,(chip->base >> chip->addrshift) + chip->start + off);
658             break;
659          case 2:
660             Last[0] = map->read16(map,(chip->base >> chip->addrshift) + chip->start + off);
661             Last[1] = map->read16(map,(chip->base >> chip->addrshift) + chip->start + off);
662             Last[2] = map->read16(map,(chip->base >> chip->addrshift) + chip->start + off);
663             break;
664          case 3:
665             Last[0] = map->read32(map,(chip->base >> chip->addrshift) + chip->start + off);
666             Last[1] = map->read32(map,(chip->base >> chip->addrshift) + chip->start + off);
667             Last[2] = map->read32(map,(chip->base >> chip->addrshift) + chip->start + off);
668             break;
669          }
670          Count = 3;
671          while (todo_left != 0)
672          {
673             for (J = 0; J != 4; J++)
674             {
675                __u8 Byte1 = (Last[(Count-1)%4] >> (J*8)) & 0xFF;
676                __u8 Byte2 = (Last[(Count-2)%4] >> (J*8)) & 0xFF;
677                __u8 Byte3 = (Last[(Count-3)%4] >> (J*8)) & 0xFF;
678                if (todo[J] == 0)
679                   continue;
680                
681                if ((Byte1 & (1 << 7)) == 0 && Byte1 != Byte2)
682                {
683 //                printk("Check %x %x %x\n",(short)J,(short)Byte1,(short)Byte2);
684                   continue;
685                }
686                
687                if (Byte1 == Byte2)
688                {
689                   jedec_flash_failed(Byte3);
690                   return -EIO;
691                }
692                
693                todo[J] = 0;
694                todo_left--;
695             }
696             
697 /*          if (NoTime == 0)
698                Time += HZ/10 - schedule_timeout(HZ/10);*/
699             NoTime = 0;
700
701             switch (map->buswidth) {
702             case 1:
703                Last[Count % 4] = map->read8(map,(chip->base >> chip->addrshift) + chip->start + off);
704               break;
705             case 2:
706                Last[Count % 4] = map->read16(map,(chip->base >> chip->addrshift) + chip->start + off);
707               break;
708             case 4:
709                Last[Count % 4] = map->read32(map,(chip->base >> chip->addrshift) + chip->start + off);
710               break;
711             }
712             Count++;
713             
714 /*          // Count time, max of 15s per sector (according to AMD)
715             if (Time > 15*len/mtd->erasesize*HZ)
716             {
717                printk("mtd: Flash Erase Timed out\n");
718                return -EIO;
719             }       */
720          }
721                  
722          // Skip to the next chip if we used chip erase
723          if (chip->length == chip->size)
724             off = chip->size;
725          else
726             off += chip->sectorsize;
727          
728          if (off >= chip->length)
729             break;
730          NoTime = 1;
731       }
732       
733       for (J = 0; priv->chips[J].jedec != 0 && J < MAX_JEDEC_CHIPS; J++)
734       {
735          if ((priv->chips[J].base & (~((1<<chip->addrshift)-1))) ==
736              (chip->base & (~((1<<chip->addrshift)-1))))
737             priv->chips[J].length = 0;
738       }      
739    }
740             
741    //printk("done\n");
742    instr->state = MTD_ERASE_DONE;
743    if (instr->callback)
744         instr->callback(instr);
745    return 0;
746    
747    #undef flread
748    #undef flwrite
749 }
750
751 /* This is the simple flash writing function. It writes to every byte, in
752    sequence. It takes care of how to properly address the flash if
753    the flash is interleved. It can only be used if all the chips in the 
754    array are identical!*/
755 static int flash_write(struct mtd_info *mtd, loff_t start, size_t len,
756                        size_t *retlen, const u_char *buf)
757 {
758    /* Does IO to the currently selected chip. It takes the bank addressing
759       base (which is divisable by the chip size) adds the necesary lower bits
760       of addrshift (interleve index) and then adds the control register index. */
761    #define flread(x) map->read8(map,base+(off&((1<<chip->addrshift)-1))+((x)<<chip->addrshift))
762    #define flwrite(v,x) map->write8(map,v,base+(off&((1<<chip->addrshift)-1))+((x)<<chip->addrshift))
763    
764    struct map_info *map = (struct map_info *)mtd->priv;
765    struct jedec_private *priv = (struct jedec_private *)map->fldrv_priv;
766    unsigned long base;
767    unsigned long off;
768    size_t save_len = len;
769    
770    if (start + len > mtd->size)
771       return -EIO;
772    
773    //printk("Here");
774    
775    //printk("flash_write: start is %x, len is %x\n",start,(unsigned long)len);
776    while (len != 0)
777    {
778       struct jedec_flash_chip *chip = priv->chips;
779       unsigned long bank;
780       unsigned long boffset;
781          
782       // Compute the base of the flash.
783       off = ((unsigned long)start) % (chip->size << chip->addrshift);
784       base = start - off;
785
786       // Perform banked addressing translation.
787       bank = base & (~(priv->bank_fill[0]-1));
788       boffset = base & (priv->bank_fill[0]-1);
789       bank = (bank/priv->bank_fill[0])*my_bank_size;
790       base = bank + boffset;
791       
792     //  printk("Flasing %X %X %X\n",base,chip->size,len);
793      // printk("off is %x, compare with %x\n",off,chip->size << chip->addrshift);
794       
795       // Loop over this page
796       for (; off != (chip->size << chip->addrshift) && len != 0; start++, len--, off++,buf++)
797       {
798          unsigned char oldbyte = map->read8(map,base+off);
799          unsigned char Last[4];
800          unsigned long Count = 0;
801
802          if (oldbyte == *buf) {
803         //       printk("oldbyte and *buf is %x,len is %x\n",oldbyte,len);
804             continue;
805          }
806          if (((~oldbyte) & *buf) != 0)
807             printk("mtd: warn: Trying to set a 0 to a 1\n");
808              
809          // Write
810          flwrite(0xAA,0x555);
811          flwrite(0x55,0x2AA);
812          flwrite(0xA0,0x555);
813          map->write8(map,*buf,base + off);
814          Last[0] = map->read8(map,base + off);
815          Last[1] = map->read8(map,base + off);
816          Last[2] = map->read8(map,base + off);
817          
818          /* Wait for the flash to finish the operation. We store the last 4
819             status bytes that have been retrieved so we can determine why
820             it failed. The toggle bits keep toggling when there is a 
821             failure */
822          for (Count = 3; Last[(Count - 1) % 4] != Last[(Count - 2) % 4] &&
823               Count < 10000; Count++)
824             Last[Count % 4] = map->read8(map,base + off);
825          if (Last[(Count - 1) % 4] != *buf)
826          {
827             jedec_flash_failed(Last[(Count - 3) % 4]);
828             return -EIO;
829          }       
830       }
831    }
832    *retlen = save_len;
833    return 0;
834 }
835
836 /* This is used to enhance the speed of the erase routine,
837    when things are being done to multiple chips it is possible to
838    parallize the operations, particularly full memory erases of multi
839    chip memories benifit */
840 static void jedec_flash_chip_scan(struct jedec_private *priv,unsigned long start,
841                      unsigned long len)
842 {
843    unsigned int I;
844
845    // Zero the records
846    for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
847       priv->chips[I].start = priv->chips[I].length = 0;
848    
849    // Intersect the region with each chip
850    for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
851    {
852       struct jedec_flash_chip *chip = priv->chips + I;
853       unsigned long ByteStart;
854       unsigned long ChipEndByte = chip->offset + (chip->size << chip->addrshift);
855       
856       // End is before this chip or the start is after it
857       if (start+len < chip->offset ||
858           ChipEndByte - (1 << chip->addrshift) < start)
859          continue;
860       
861       if (start < chip->offset)
862       {
863          ByteStart = chip->offset;
864          chip->start = 0;
865       }      
866       else
867       {
868          chip->start = (start - chip->offset + (1 << chip->addrshift)-1) >> chip->addrshift;
869          ByteStart = start;
870       }
871
872       if (start + len >= ChipEndByte)
873          chip->length = (ChipEndByte - ByteStart) >> chip->addrshift;
874       else
875          chip->length = (start + len - ByteStart + (1 << chip->addrshift)-1) >> chip->addrshift;
876    }
877 }
878
879 int __init jedec_init(void)
880 {
881         register_mtd_chip_driver(&jedec_chipdrv);
882         return 0;
883 }
884
885 static void __exit jedec_exit(void)
886 {
887         unregister_mtd_chip_driver(&jedec_chipdrv);
888 }
889
890 module_init(jedec_init);
891 module_exit(jedec_exit);
892
893 MODULE_LICENSE("GPL");
894 MODULE_AUTHOR("Jason Gunthorpe <jgg@deltatee.com> et al.");
895 MODULE_DESCRIPTION("Old MTD chip driver for JEDEC-compliant flash chips");