📄 mapped.c
字号:
// -*- mode: cpp; mode: fold -*-// Description /*{{{*/// $Id: mapped.c,v 1.8 2000/03/31 14:40:42 dwmw2 Exp $/* ###################################################################### Flash MTD Routines These routine support IDing and manipulating flash. Currently the older JEDEC ID mechanism and a table is used for determining the flash characterisitics, but it is trivial to add support for the CFI specification: http://www.pentium.com/design/flash/ in the technote section. ##################################################################### */ /*}}}*/#include <linux/mtd/mapped.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/errno.h>#include <linux/delay.h>#include <linux/sched.h>#include <asm/io.h>struct JEDECTable mtd_JEDEC_table[] = {{0x01AD,"AMD Am29F016",2*1024*1024,64*1024,MTD_CAP_NORFLASH}, {0x01D5,"AMD Am29F080",1*1024*1024,64*1024,MTD_CAP_NORFLASH}, {}};// flash_setup - Setup the mapped_mtd_info structure for normal flash /*{{{*/// ---------------------------------------------------------------------/* There is a set of commands that flash manufactures follow for getting the JEDEC id, erasing and writing. So long as your flash device supports getting the JEDEC ID in this (standard?) way it will be supported as flash, otherwise it is converted to ROM. Upon completion the structure is registered with the MTD layer */int mtd_mapped_setup(struct mapped_mtd_info *map){ DEBUG(1, "\n"); // Must define a page function to use the defaults! if (map->page == 0) return -1; if (map->jedec_sense == 0) map->jedec_sense = flash_jedec; if (map->jedec_sense(map) != 0) return -1; if (map->mtd.erase == 0 && map->mtd.type == MTD_NORFLASH) map->mtd.erase = flash_erase; if (map->mtd.write == 0) { if (map->mtd.type == MTD_NORFLASH) map->mtd.write = flash_write; if (map->mtd.type == MTD_RAM) map->mtd.write = ram_write; } if (map->mtd.read == 0) map->mtd.read = rom_read; return add_mtd_device(&map->mtd);} /*}}}*/// flash_remove - Remove the flash device from the MTD layer /*{{{*/// ---------------------------------------------------------------------/* Free any memory allocated for the device here */int mtd_mapped_remove(struct mapped_mtd_info *map){ return del_mtd_device(&map->mtd);} /*}}}*/// checkparity - Checks a number for odd parity /*{{{*/// ---------------------------------------------------------------------/* Helper for the JEDEC function, JEDEC numbers all have odd parity */static int checkparity(u_char C){ u_char parity = 0; while (C != 0) { parity ^= C & 1; C >>= 1; } return parity == 1;} /*}}}*/// SetJedec - Set the jedec information for a chip /*{{{*/// ---------------------------------------------------------------------/* We track the configuration of each chip separately in the chip list, each chip can have a different type and configuration to allow for maximum flexability. */void set_jedec(struct mapped_mtd_info *map,unsigned chip,unsigned char mfr, unsigned char id){ unsigned long longID = (mfr << 8) + id; unsigned int I; map->mtd.type = MTD_NORFLASH; map->mfr = mfr; map->id = id; // Locate the chip in the jedec table for (I = 0; mtd_JEDEC_table[I].jedec != 0; I++) { if (mtd_JEDEC_table[I].jedec == longID) break; } if (mtd_JEDEC_table[I].jedec != longID || longID == 0) { printk("Unknown JEDEC number %x-%x, treating as ROM\n",map->mfr, map->id); map->mtd.type = MTD_ROM; return; } // Setup the MTD from the JEDEC information// map->mtd.size = mtd_JEDEC_table[I].size;// map->mtd.erasesize = mtd_JEDEC_table[I].sectorsize;// map->mtd.capabilities = mtd_JEDEC_table[I].capabilities;// strncpy(map->mtd.part,mtd_JEDEC_table[I].name,sizeof(map->mtd.part)-1); map->chips[chip].jedec = longID; map->chips[chip].size = mtd_JEDEC_table[I].size; map->chips[chip].sectorsize = mtd_JEDEC_table[I].sectorsize; map->chips[chip].capabilities = mtd_JEDEC_table[I].capabilities; map->chips[chip].base = 0; } /*}}}*/// isjedec - Check if reading from the memory location gives jedec #s /*{{{*/// ---------------------------------------------------------------------/* This is ment to be called on the flash window once it is in jedec mode */int isjedec(unsigned long base){ // Test #1, JEDEC numbers are readable from 0x??00/0x??01 if (readb(base + 0) != readb(base + 0x100) || readb(base + 1) != readb(base + 0x101)) return 0; // Test #2 JEDEC numbers exhibit odd parity if (checkparity(readb(base + 0)) == 0 || checkparity(readb(base + 1)) == 0) return 0; return 1;} /*}}}*/// flash_jedec - JEDEC ID sensor /*{{{*/// ---------------------------------------------------------------------/* The mysterious jedec flash probe sequence writes a specific pattern of bytes to the flash. This should be general enough to work with any MTD structure that may contain a flash chip, but note that it will corrupt address 0x5555 on SRAM cards if the machine dies between the two critical operations. */int flash_jedec(struct mapped_mtd_info *map){ unsigned I; u_char OldVal; unsigned long base; unsigned long baseaddr = 0; unsigned chip = 0; unsigned count; // Who has a page size this small? :> if (map->pagesize < 0x555) return 1; base = map->page(map,0); // Wait for any write/erase operation to settle OldVal = readb(base); for (I = 0; OldVal != readb(base) && I < 10000; I++) OldVal = readb(base); /* Check for sram by writing to it, the write also happens to be part of the flash reset sequence.. */ OldVal = readb(base + 0x555); writeb(OldVal,base + 0x555); writeb(0xF0,base + 0x555); if (OldVal != readb(base + 0x555)) { udelay(100); // Set it back and make sure.. writeb(OldVal,base + 0x555); if (OldVal == readb(base + 0x555)) { map->mtd.type = MTD_RAM; return 0; } writeb(0xF0,base + 0x555); } // Probe for chips while (chip < sizeof(map->chips)/sizeof(map->chips[0])) { // Already in jedec mode, we might be doing some address wrap around if (chip != 0 && isjedec(base) != 0) { /* Try to reset this page and check if that resets the first page to confirm */ writeb(0xF0,base + 0x555); if (isjedec(base) != 0) break; base = map->page(map,0); if (isjedec(base) == 0) break; base = map->page(map,baseaddr/map->pagesize); } // Send the sequence writeb(0xAA,base + 0x555); writeb(0x55,base + 0x2AA); writeb(0x90,base + 0x555); // Check the jedec number if (isjedec(base) == 0) { /* If this is the first chip it must be rom, otherwise it is the end of the flash region */ if (chip == 0) { map->mtd.type = MTD_ROM; return 0; } break; } // Store the jdec info set_jedec(map,chip,readb(base + 0),readb(base + 1)); map->chips[chip].base = baseaddr; // Jump to the next chip baseaddr += map->chips[chip].size; if (baseaddr/map->pagesize > map->maxsize) break; base = map->page(map,baseaddr/map->pagesize); if (base == 0) return -EIO; chip++; } // Reset all of the chips map->mtd.size = 0; baseaddr = 0; map->mtd.flags = 0xFFFF; for (I = 0; map->chips[I].jedec != 0; I++) { // Fill in the various MTD structures map->mtd.size += map->chips[I].size; if (map->mtd.erasesize < map->chips[I].sectorsize) map->mtd.erasesize = map->chips[I].sectorsize; map->mtd.flags &= map->chips[I].capabilities; base = map->page(map,baseaddr/map->pagesize); baseaddr += map->chips[chip].size; writeb(0xF0,base + 0); // Reset } /* Generate a part name that includes the number of different chips and other configuration information */ count = 1; map->part[0] = 0; for (I = 0; map->chips[I].jedec != 0; I++) { unsigned J; if (map->chips[I+1].jedec == map->chips[I].jedec) { count++; continue; } // Locate the chip in the jedec table for (J = 0; mtd_JEDEC_table[J].jedec != 0; J++) { if (mtd_JEDEC_table[J].jedec == map->chips[I].jedec) break; } if (map->part[0] != 0) strcat(map->part,","); if (count != 1) sprintf(map->part+strlen(map->part),"%u*[%s]",count, mtd_JEDEC_table[J].name); else sprintf(map->part+strlen(map->part),"%s", mtd_JEDEC_table[J].name); count = 1; } return 0;} /*}}}*/// flash_failed - Print a console message about why the failure /*{{{*/// ---------------------------------------------------------------------/* Pass the flags value that the flash return before it re-entered read mode. */static void flash_failed(unsigned char code){ /* Bit 5 being high indicates that there was an internal device failure, erasure time limits exceeded or something */ if ((code & (1 << 5)) != 0) { printk("mtd: Internal Flash failure\n"); return; } printk("mtd: Programming didn't take\n");} /*}}}*/// flash_erase - Generic erase function /*{{{*/// ---------------------------------------------------------------------/* This uses the erasure function described in the AMD Flash Handbook, it will work for flashes with a fixed sector size only. Flashes with a selection of sector sizes (ie the AMD Am29F800B) will need a different routine. This routine tries to parallize erasing multiple chips/sectors where possible */int flash_erase(struct mtd_info *mtd, struct erase_info *instr){ unsigned long Time = 0; unsigned long NoTime = 0; unsigned long start = instr->addr, len = instr->len; unsigned int I; struct mapped_mtd_info *map = (struct mapped_mtd_info *)mtd; // Verify the arguments.. if (start + len > map->mtd.size || (start % map->mtd.erasesize) != 0 || (len % map->mtd.erasesize) != 0 || (len/map->mtd.erasesize) == 0) return -EINVAL; flash_chip_scan(map,start,len); // Start the erase sequence on each chip
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -