- pre2
[davej-history.git] / drivers / mtd / mapped.c
blob84a74036e8945e14f0cb9bae78e1f3235463545a
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: mapped.c,v 1.8 2000/03/31 14:40:42 dwmw2 Exp $
4 /* ######################################################################
6 Flash MTD Routines
8 These routine support IDing and manipulating flash. Currently the
9 older JEDEC ID mechanism and a table is used for determining the
10 flash characterisitics, but it is trivial to add support for the
11 CFI specification:
12 http://www.pentium.com/design/flash/ in the technote section.
14 ##################################################################### */
15 /*}}}*/
16 #include <linux/mtd/mapped.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
21 #include <linux/delay.h>
22 #include <linux/sched.h>
23 #include <asm/io.h>
24 #include <asm/delay.h>
26 struct JEDECTable mtd_JEDEC_table[] =
27 {{0x01AD,"AMD Am29F016",2*1024*1024,64*1024,MTD_CAP_NORFLASH},
28 {0x01D5,"AMD Am29F080",1*1024*1024,64*1024,MTD_CAP_NORFLASH},
29 {}};
31 // flash_setup - Setup the mapped_mtd_info structure for normal flash /*{{{*/
32 // ---------------------------------------------------------------------
33 /* There is a set of commands that flash manufactures follow for getting the
34 JEDEC id, erasing and writing. So long as your flash device supports
35 getting the JEDEC ID in this (standard?) way it will be supported as flash,
36 otherwise it is converted to ROM. Upon completion the structure is
37 registered with the MTD layer */
38 int mtd_mapped_setup(struct mapped_mtd_info *map)
40 DEBUG(1, "\n");
41 // Must define a page function to use the defaults!
42 if (map->page == 0)
43 return -1;
45 if (map->jedec_sense == 0)
46 map->jedec_sense = flash_jedec;
48 if (map->jedec_sense(map) != 0)
49 return -1;
51 if (map->mtd.erase == 0 && map->mtd.type == MTD_NORFLASH)
52 map->mtd.erase = flash_erase;
53 if (map->mtd.write == 0)
55 if (map->mtd.type == MTD_NORFLASH)
56 map->mtd.write = flash_write;
57 if (map->mtd.type == MTD_RAM)
58 map->mtd.write = ram_write;
60 if (map->mtd.read == 0)
61 map->mtd.read = rom_read;
63 return add_mtd_device(&map->mtd);
65 /*}}}*/
66 // flash_remove - Remove the flash device from the MTD layer /*{{{*/
67 // ---------------------------------------------------------------------
68 /* Free any memory allocated for the device here */
69 int mtd_mapped_remove(struct mapped_mtd_info *map)
71 return del_mtd_device(&map->mtd);
73 /*}}}*/
75 // checkparity - Checks a number for odd parity /*{{{*/
76 // ---------------------------------------------------------------------
77 /* Helper for the JEDEC function, JEDEC numbers all have odd parity */
78 static int checkparity(u_char C)
80 u_char parity = 0;
81 while (C != 0)
83 parity ^= C & 1;
84 C >>= 1;
87 return parity == 1;
89 /*}}}*/
90 // SetJedec - Set the jedec information for a chip /*{{{*/
91 // ---------------------------------------------------------------------
92 /* We track the configuration of each chip separately in the chip list,
93 each chip can have a different type and configuration to allow for
94 maximum flexability. */
95 void set_jedec(struct mapped_mtd_info *map,unsigned chip,unsigned char mfr,
96 unsigned char id)
98 unsigned long longID = (mfr << 8) + id;
99 unsigned int I;
101 map->mtd.type = MTD_NORFLASH;
102 map->mfr = mfr;
103 map->id = id;
105 // Locate the chip in the jedec table
106 for (I = 0; mtd_JEDEC_table[I].jedec != 0; I++)
108 if (mtd_JEDEC_table[I].jedec == longID)
109 break;
112 if (mtd_JEDEC_table[I].jedec != longID || longID == 0)
114 printk("Unknown JEDEC number %x-%x, treating as ROM\n",map->mfr,
115 map->id);
116 map->mtd.type = MTD_ROM;
117 return;
120 // Setup the MTD from the JEDEC information
121 // map->mtd.size = mtd_JEDEC_table[I].size;
122 // map->mtd.erasesize = mtd_JEDEC_table[I].sectorsize;
123 // map->mtd.capabilities = mtd_JEDEC_table[I].capabilities;
124 // strncpy(map->mtd.part,mtd_JEDEC_table[I].name,sizeof(map->mtd.part)-1);
126 map->chips[chip].jedec = longID;
127 map->chips[chip].size = mtd_JEDEC_table[I].size;
128 map->chips[chip].sectorsize = mtd_JEDEC_table[I].sectorsize;
129 map->chips[chip].capabilities = mtd_JEDEC_table[I].capabilities;
130 map->chips[chip].base = 0;
132 /*}}}*/
133 // isjedec - Check if reading from the memory location gives jedec #s /*{{{*/
134 // ---------------------------------------------------------------------
135 /* This is ment to be called on the flash window once it is in jedec mode */
136 int isjedec(unsigned long base)
138 // Test #1, JEDEC numbers are readable from 0x??00/0x??01
139 if (readb(base + 0) != readb(base + 0x100) ||
140 readb(base + 1) != readb(base + 0x101))
141 return 0;
143 // Test #2 JEDEC numbers exhibit odd parity
144 if (checkparity(readb(base + 0)) == 0 || checkparity(readb(base + 1)) == 0)
145 return 0;
146 return 1;
148 /*}}}*/
149 // flash_jedec - JEDEC ID sensor /*{{{*/
150 // ---------------------------------------------------------------------
151 /* The mysterious jedec flash probe sequence writes a specific pattern of
152 bytes to the flash. This should be general enough to work with any MTD
153 structure that may contain a flash chip, but note that it will corrupt
154 address 0x5555 on SRAM cards if the machine dies between the two
155 critical operations. */
156 int flash_jedec(struct mapped_mtd_info *map)
158 unsigned I;
159 u_char OldVal;
160 unsigned long base;
161 unsigned long baseaddr = 0;
162 unsigned chip = 0;
163 unsigned count;
165 // Who has a page size this small? :>
166 if (map->pagesize < 0x555)
167 return 1;
169 base = map->page(map,0);
171 // Wait for any write/erase operation to settle
172 OldVal = readb(base);
173 for (I = 0; OldVal != readb(base) && I < 10000; I++)
174 OldVal = readb(base);
176 /* Check for sram by writing to it, the write also happens to be part
177 of the flash reset sequence.. */
178 OldVal = readb(base + 0x555);
179 writeb(OldVal,base + 0x555);
180 writeb(0xF0,base + 0x555);
181 if (OldVal != readb(base + 0x555))
183 udelay(100);
185 // Set it back and make sure..
186 writeb(OldVal,base + 0x555);
187 if (OldVal == readb(base + 0x555))
189 map->mtd.type = MTD_RAM;
190 return 0;
193 writeb(0xF0,base + 0x555);
196 // Probe for chips
197 while (chip < sizeof(map->chips)/sizeof(map->chips[0]))
199 // Already in jedec mode, we might be doing some address wrap around
200 if (chip != 0 && isjedec(base) != 0)
202 /* Try to reset this page and check if that resets the first page
203 to confirm */
204 writeb(0xF0,base + 0x555);
205 if (isjedec(base) != 0)
206 break;
207 base = map->page(map,0);
208 if (isjedec(base) == 0)
209 break;
210 base = map->page(map,baseaddr/map->pagesize);
213 // Send the sequence
214 writeb(0xAA,base + 0x555);
215 writeb(0x55,base + 0x2AA);
216 writeb(0x90,base + 0x555);
218 // Check the jedec number
219 if (isjedec(base) == 0)
221 /* If this is the first chip it must be rom, otherwise it is the
222 end of the flash region */
223 if (chip == 0)
225 map->mtd.type = MTD_ROM;
226 return 0;
228 break;
231 // Store the jdec info
232 set_jedec(map,chip,readb(base + 0),readb(base + 1));
233 map->chips[chip].base = baseaddr;
235 // Jump to the next chip
236 baseaddr += map->chips[chip].size;
237 if (baseaddr/map->pagesize > map->maxsize)
238 break;
239 base = map->page(map,baseaddr/map->pagesize);
240 if (base == 0)
241 return -EIO;
243 chip++;
246 // Reset all of the chips
247 map->mtd.size = 0;
248 baseaddr = 0;
249 map->mtd.flags = 0xFFFF;
250 for (I = 0; map->chips[I].jedec != 0; I++)
252 // Fill in the various MTD structures
253 map->mtd.size += map->chips[I].size;
254 if (map->mtd.erasesize < map->chips[I].sectorsize)
255 map->mtd.erasesize = map->chips[I].sectorsize;
256 map->mtd.flags &= map->chips[I].capabilities;
258 base = map->page(map,baseaddr/map->pagesize);
259 baseaddr += map->chips[chip].size;
260 writeb(0xF0,base + 0); // Reset
263 /* Generate a part name that includes the number of different chips and
264 other configuration information */
265 count = 1;
266 map->part[0] = 0;
267 for (I = 0; map->chips[I].jedec != 0; I++)
269 unsigned J;
270 if (map->chips[I+1].jedec == map->chips[I].jedec)
272 count++;
273 continue;
276 // Locate the chip in the jedec table
277 for (J = 0; mtd_JEDEC_table[J].jedec != 0; J++)
279 if (mtd_JEDEC_table[J].jedec == map->chips[I].jedec)
280 break;
283 if (map->part[0] != 0)
284 strcat(map->part,",");
286 if (count != 1)
287 sprintf(map->part+strlen(map->part),"%u*[%s]",count,
288 mtd_JEDEC_table[J].name);
289 else
290 sprintf(map->part+strlen(map->part),"%s",
291 mtd_JEDEC_table[J].name);
292 count = 1;
294 return 0;
296 /*}}}*/
298 // flash_failed - Print a console message about why the failure /*{{{*/
299 // ---------------------------------------------------------------------
300 /* Pass the flags value that the flash return before it re-entered read
301 mode. */
302 static void flash_failed(unsigned char code)
304 /* Bit 5 being high indicates that there was an internal device
305 failure, erasure time limits exceeded or something */
306 if ((code & (1 << 5)) != 0)
308 printk("mtd: Internal Flash failure\n");
309 return;
311 printk("mtd: Programming didn't take\n");
313 /*}}}*/
314 // flash_erase - Generic erase function /*{{{*/
315 // ---------------------------------------------------------------------
316 /* This uses the erasure function described in the AMD Flash Handbook,
317 it will work for flashes with a fixed sector size only. Flashes with
318 a selection of sector sizes (ie the AMD Am29F800B) will need a different
319 routine. This routine tries to parallize erasing multiple chips/sectors
320 where possible */
321 int flash_erase(struct mtd_info *mtd, struct erase_info *instr)
323 unsigned long Time = 0;
324 unsigned long NoTime = 0;
325 unsigned long start = instr->addr, len = instr->len;
326 unsigned int I;
327 struct mapped_mtd_info *map = (struct mapped_mtd_info *)mtd;
329 // Verify the arguments..
330 if (start + len > map->mtd.size ||
331 (start % map->mtd.erasesize) != 0 ||
332 (len % map->mtd.erasesize) != 0 ||
333 (len/map->mtd.erasesize) == 0)
334 return -EINVAL;
336 flash_chip_scan(map,start,len);
338 // Start the erase sequence on each chip
339 for (I = 0; map->chips[I].jedec != 0; I++)
341 unsigned long off;
342 struct flash_chip *chip = map->chips + I;
343 unsigned long base;
344 unsigned long flags;
346 if (chip->length == 0)
347 continue;
349 if (page_jump(map,chip->base + chip->start,0x555,&base,0) != 0)
350 return -EIO;
352 // Send the erase setup code
353 writeb(0xF0,base + 0x555);
354 writeb(0xAA,base + 0x555);
355 writeb(0x55,base + 0x2AA);
356 writeb(0x80,base + 0x555);
357 writeb(0xAA,base + 0x555);
358 writeb(0x55,base + 0x2AA);
360 // Use chip erase if possible
361 if (chip->start == 0 && chip->length == chip->size)
363 writeb(0x10,base+0x555);
364 continue;
367 /* Once we start selecting the erase sectors the delay between each
368 command must not exceed 50us or it will immediately start erasing
369 and ignore the other sectors */
370 save_flags(flags);
371 cli();
372 for (off = 0; off < chip->length; off += chip->sectorsize)
374 if (page_jump(map,chip->base + chip->start + off,1,&base,0) != 0)
375 return -EIO;
377 // Check to make sure we didn't timeout
378 writeb(0x30,base);
379 if ((readb(base) & (1 << 3)) != 0)
381 printk("mtd: Ack! We timed out the erase timer!\n");
382 return -EIO;
385 restore_flags(flags);
388 /* We could split this into a timer routine and return early, performing
389 background erasure.. Maybe later if the need warrents */
391 /* Poll the flash for erasure completion, specs say this can take as long
392 as 480 seconds to do all the sectors (for a 2 meg flash).
393 Erasure time is dependant on chip age, temp and wear.. */
394 Time = 0;
395 NoTime = 0;
396 for (I = 0; map->chips[I].jedec != 0; I++)
398 struct flash_chip *chip = map->chips + I;
399 unsigned long base;
400 unsigned long off = 0;
401 if (chip->length == 0)
402 continue;
404 if (page_jump(map,chip->base + chip->start,1,&base,0) != 0)
405 return -EIO;
407 while (1)
409 unsigned char Last[4];
410 unsigned long Count = 0;
412 /* During erase bit 7 is held low and bit 6 toggles, we watch this,
413 should it stop toggling or go high then the erase is completed,
414 or this is not really flash ;> */
415 Last[0] = readb(base);
416 Last[1] = readb(base);
417 Last[2] = readb(base);
418 for (Count = 3; (Last[(Count - 1) % 4] & (1 << 7)) == 0 &&
419 Last[(Count - 1) % 4] != Last[(Count - 2) % 4]; Count++)
421 if (NoTime == 0)
422 Time += HZ/10 - schedule_timeout(HZ/10);
423 NoTime = 0;
425 Last[Count % 4] = readb(base);
427 // Count time, max of 15s per sector (according to AMD)
428 if (Time > 15*len/mtd->erasesize*HZ)
430 printk("mtd: Flash Erase Timed out\n");
431 return -EIO;
435 if (Last[(Count - 1) % 4] == Last[(Count - 2) % 4])
437 flash_failed(Last[(Count - 3) % 4]);
438 return -EIO;
441 // Skip to the next chip if we used chip erase
442 if (chip->length == chip->size)
443 off = chip->size;
444 else
445 off += chip->sectorsize;
447 if (off >= chip->length)
448 break;
449 if (page_jump(map,chip->base + chip->start + off,1,&base,0) != 0)
450 return -EIO;
451 NoTime = 1;
455 // Paranoid verify of erasure
457 unsigned long base;
458 unsigned long buflen;
459 while (len > 0)
461 unsigned long step;
463 if (page_jump(map,start,len,&base,&buflen) != 0)
464 return -EIO;
465 start += buflen;
466 len -= buflen;
467 step = buflen/128;
468 for (;buflen != 0; buflen -= step)
470 if (readb(base+buflen-1) != 0xFF)
472 printk("mtd: Flash Erase didn't take %lu %lu %lu\n",buflen,len,start);
473 return -EIO;
479 return 0;
481 #if 1
482 /*}}}*/
483 // flash_write - Generic writing function /*{{{*/
484 // ---------------------------------------------------------------------
485 /* This could do parallel writes on multiple chips but doesnt, memory
486 constraints make that infeasable. This should work with any sort of
487 linear flash that is not interleved */
488 extern int flash_write(struct mtd_info *mtd, loff_t start, size_t len,
489 size_t *retlen, const u_char *buf)
491 struct mapped_mtd_info *map = (struct mapped_mtd_info *)mtd;
492 unsigned long base;
493 unsigned long off;
494 DEBUG(1,"\n");
495 if (start + len > mtd->size)
496 return -EIO;
498 while (len != 0)
500 // Compute the page offset and reposition
501 base = map->page(map,(u_long)start/map->pagesize);
502 off = (u_long)start % map->pagesize;
504 // Loop over this page
505 for (; off != map->pagesize && len != 0; start++, len--, off++,buf++)
507 unsigned char oldbyte = readb(base+off);
508 unsigned char Last[4];
509 unsigned long Count = 0;
511 if (oldbyte == *buf)
512 continue;
513 if (((~oldbyte) & *buf) != 0)
514 printk("mtd: warn: Trying to set a 0 to a 1\n");
516 // Write
517 writeb(0xAA,base + 0x555);
518 writeb(0x55,base + 0x2AA);
519 writeb(0xA0,base + 0x555);
520 writeb(*buf,base + off);
521 Last[0] = readb(base + off);
522 Last[1] = readb(base + off);
523 Last[2] = readb(base + off);
525 /* Wait for the flash to finish the operation. We store the last 4
526 status bytes that have been retrieved so we can determine why
527 it failed. The toggle bits keep toggling when there is a
528 failure */
529 for (Count = 3; Last[(Count - 1) % 4] != Last[(Count - 2) % 4] &&
530 Count < 10000; Count++)
531 Last[Count % 4] = readb(base + off);
532 if (Last[(Count - 1) % 4] != *buf)
534 flash_failed(Last[(Count - 3) % 4]);
535 return -EIO;
539 *retlen = len;
540 return 0;
542 #endif
544 // ram_write - Generic writing function for ram /*{{{*/
545 // ---------------------------------------------------------------------
546 /* */
547 extern int ram_write(struct mtd_info *mtd, loff_t start, size_t len,
548 size_t *retlen, const u_char *buf)
550 struct mapped_mtd_info *map = (struct mapped_mtd_info *)mtd;
551 unsigned long base;
552 size_t origlen = len;
553 unsigned long buflen;
554 DEBUG(1,"\n");
555 if (start + len > mtd->size)
556 return -EIO;
558 while (len != 0)
560 // Reposition..
561 if (page_jump(map,start,len,&base,&buflen) != 0)
562 return -EIO;
564 // Copy
565 memcpy_toio(base,buf,buflen);
566 len -= buflen;
567 start += buflen;
569 *retlen = origlen;
570 return 0;
573 // rom_read - Read handler for any sort of device /*{{{*/
574 // ---------------------------------------------------------------------
575 /* This is a generic read function that should work with any device in the
576 mapped region. */
577 extern int rom_read(struct mtd_info *mtd, loff_t start, size_t len,
578 size_t *retlen, u_char *buf)
580 struct mapped_mtd_info *map = (struct mapped_mtd_info *)mtd;
581 size_t origlen = len;
582 unsigned long base;
583 unsigned long buflen;
585 printk("Rom_Read\n");
586 if (start + len > mtd->size)
587 return -EIO;
589 while (len != 0)
591 // Reposition..
592 if (page_jump(map,start,len,&base,&buflen) != 0)
593 return -EIO;
595 // Copy
596 memcpy_fromio(buf,base,buflen);
597 len -= buflen;
598 start += buflen;
600 *retlen = origlen;
601 return 0;
604 // page_jump - Move the window and return the buffer /*{{{*/
605 // ---------------------------------------------------------------------
606 /* Unlike the page function this returns a buffer and length adjusted for
607 the page dimensions and the reading offset into the page, simplifies
608 many of the other routines */
609 int page_jump(struct mapped_mtd_info *map,unsigned long start,
610 unsigned long len,unsigned long *base,
611 unsigned long *retlen)
613 DEBUG(1,"Page Jump\n");
614 if (start > map->mtd.size || start + len > map->mtd.size)
615 return -EINVAL;
617 *base = map->page(map,start/map->pagesize);
618 if (*base == 0)
619 return -EIO;
621 *base += start % map->pagesize;
623 // If retlen is 0 that mean the caller requires len bytes, no quibbling.
624 if (retlen == 0)
626 if (len > map->pagesize - (start % map->pagesize))
627 return -EIO;
628 return 0;
631 // Compute the buffer paramaters and return
632 if (len > map->pagesize - (start % map->pagesize))
633 *retlen = map->pagesize - (start % map->pagesize);
634 else
635 *retlen = len;
636 return 0;
638 /*}}}*/
639 // flash_chip_scan - Intersect a region with the flash chip structure /*{{{*/
640 // ---------------------------------------------------------------------
641 /* This is used to enhance the speed of the erase routine,
642 when things are being done to multiple chips it is possible to
643 parallize the operations, particularly full memory erases of multi
644 chip memories benifit */
646 void flash_chip_scan(struct mapped_mtd_info *map,unsigned long start,
647 unsigned long len)
649 unsigned int I = 0;
651 DEBUG(1,"\n");
652 // Zero the records
653 for (I = 0; map->chips[I].jedec != 0; I++)
654 map->chips[I].start = map->chips[I].length = 0;
656 // Intesect our region with the chip structures
657 for (I = 0; map->chips[I].jedec != 0 && len != 0; I++)
659 // Havent found the start yet
660 if (start >= map->chips[I].base + map->chips[I].size)
661 continue;
663 // Store the portion of this chip that is being effected
664 map->chips[I].start = start - map->chips[I].base;
665 if (len <= map->chips[I].size - map->chips[I].start)
666 map->chips[I].length = len;
667 else
668 map->chips[I].length = map->chips[I].size - map->chips[I].start;
669 len -= map->chips[I].length;
670 start = map->chips[I].base + map->chips[I].size;
673 /*}}}*/