1 // -*- mode: cpp; mode: fold -*-
3 // $Id: mapped.c,v 1.8 2000/03/31 14:40:42 dwmw2 Exp $
4 /* ######################################################################
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
12 http://www.pentium.com/design/flash/ in the technote section.
14 ##################################################################### */
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>
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
},
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
)
41 // Must define a page function to use the defaults!
45 if (map
->jedec_sense
== 0)
46 map
->jedec_sense
= flash_jedec
;
48 if (map
->jedec_sense(map
) != 0)
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
);
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
);
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
)
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
,
98 unsigned long longID
= (mfr
<< 8) + id
;
101 map
->mtd
.type
= MTD_NORFLASH
;
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
)
112 if (mtd_JEDEC_table
[I
].jedec
!= longID
|| longID
== 0)
114 printk("Unknown JEDEC number %x-%x, treating as ROM\n",map
->mfr
,
116 map
->mtd
.type
= MTD_ROM
;
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;
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))
143 // Test #2 JEDEC numbers exhibit odd parity
144 if (checkparity(readb(base
+ 0)) == 0 || checkparity(readb(base
+ 1)) == 0)
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
)
161 unsigned long baseaddr
= 0;
165 // Who has a page size this small? :>
166 if (map
->pagesize
< 0x555)
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))
185 // Set it back and make sure..
186 writeb(OldVal
,base
+ 0x555);
187 if (OldVal
== readb(base
+ 0x555))
189 map
->mtd
.type
= MTD_RAM
;
193 writeb(0xF0,base
+ 0x555);
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
204 writeb(0xF0,base
+ 0x555);
205 if (isjedec(base
) != 0)
207 base
= map
->page(map
,0);
208 if (isjedec(base
) == 0)
210 base
= map
->page(map
,baseaddr
/map
->pagesize
);
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 */
225 map
->mtd
.type
= MTD_ROM
;
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
)
239 base
= map
->page(map
,baseaddr
/map
->pagesize
);
246 // Reset all of the chips
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 */
267 for (I
= 0; map
->chips
[I
].jedec
!= 0; I
++)
270 if (map
->chips
[I
+1].jedec
== map
->chips
[I
].jedec
)
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
)
283 if (map
->part
[0] != 0)
284 strcat(map
->part
,",");
287 sprintf(map
->part
+strlen(map
->part
),"%u*[%s]",count
,
288 mtd_JEDEC_table
[J
].name
);
290 sprintf(map
->part
+strlen(map
->part
),"%s",
291 mtd_JEDEC_table
[J
].name
);
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
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");
311 printk("mtd: Programming didn't take\n");
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
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
;
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)
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
++)
342 struct flash_chip
*chip
= map
->chips
+ I
;
346 if (chip
->length
== 0)
349 if (page_jump(map
,chip
->base
+ chip
->start
,0x555,&base
,0) != 0)
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);
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 */
372 for (off
= 0; off
< chip
->length
; off
+= chip
->sectorsize
)
374 if (page_jump(map
,chip
->base
+ chip
->start
+ off
,1,&base
,0) != 0)
377 // Check to make sure we didn't timeout
379 if ((readb(base
) & (1 << 3)) != 0)
381 printk("mtd: Ack! We timed out the erase timer!\n");
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.. */
396 for (I
= 0; map
->chips
[I
].jedec
!= 0; I
++)
398 struct flash_chip
*chip
= map
->chips
+ I
;
400 unsigned long off
= 0;
401 if (chip
->length
== 0)
404 if (page_jump(map
,chip
->base
+ chip
->start
,1,&base
,0) != 0)
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
++)
422 Time
+= HZ
/10 - schedule_timeout(HZ
/10);
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");
435 if (Last
[(Count
- 1) % 4] == Last
[(Count
- 2) % 4])
437 flash_failed(Last
[(Count
- 3) % 4]);
441 // Skip to the next chip if we used chip erase
442 if (chip
->length
== chip
->size
)
445 off
+= chip
->sectorsize
;
447 if (off
>= chip
->length
)
449 if (page_jump(map
,chip
->base
+ chip
->start
+ off
,1,&base
,0) != 0)
455 // Paranoid verify of erasure
458 unsigned long buflen
;
463 if (page_jump(map
,start
,len
,&base
,&buflen
) != 0)
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
);
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
;
495 if (start
+ len
> mtd
->size
)
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;
513 if (((~oldbyte
) & *buf
) != 0)
514 printk("mtd: warn: Trying to set a 0 to a 1\n");
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
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]);
544 // ram_write - Generic writing function for ram /*{{{*/
545 // ---------------------------------------------------------------------
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
;
552 size_t origlen
= len
;
553 unsigned long buflen
;
555 if (start
+ len
> mtd
->size
)
561 if (page_jump(map
,start
,len
,&base
,&buflen
) != 0)
565 memcpy_toio(base
,buf
,buflen
);
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
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
;
583 unsigned long buflen
;
585 printk("Rom_Read\n");
586 if (start
+ len
> mtd
->size
)
592 if (page_jump(map
,start
,len
,&base
,&buflen
) != 0)
596 memcpy_fromio(buf
,base
,buflen
);
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
)
617 *base
= map
->page(map
,start
/map
->pagesize
);
621 *base
+= start
% map
->pagesize
;
623 // If retlen is 0 that mean the caller requires len bytes, no quibbling.
626 if (len
> map
->pagesize
- (start
% map
->pagesize
))
631 // Compute the buffer paramaters and return
632 if (len
> map
->pagesize
- (start
% map
->pagesize
))
633 *retlen
= map
->pagesize
- (start
% map
->pagesize
);
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
,
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
)
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
;
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
;