📄 jedec.c
字号:
/* JEDEC Flash Interface. * This is an older type of interface for self programming flash. It is * commonly use in older AMD chips and is obsolete compared with CFI. * It is called JEDEC because the JEDEC association distributes the ID codes * for the chips. * * See the AMD flash databook for information on how to operate the interface. * * This code does not support anything wider than 8 bit flash chips, I am * not going to guess how to send commands to them, plus I expect they will * all speak CFI.. * * $Id: jedec.c,v 1.1 2000/07/04 07:21:57 jgg Exp $ */#include <linux/mtd/jedec.h>struct mtd_info *jedec_probe(struct map_info *);int jedec_probe8(struct map_info *map,unsigned long base, struct jedec_private *priv);int jedec_probe16(struct map_info *map,unsigned long base, struct jedec_private *priv);int jedec_probe32(struct map_info *map,unsigned long base, struct jedec_private *priv);static void jedec_flash_chip_scan(struct jedec_private *priv,unsigned long start, unsigned long len);static int flash_erase(struct mtd_info *mtd, struct erase_info *instr);static int flash_write(struct mtd_info *mtd, loff_t start, size_t len, size_t *retlen, const u_char *buf); EXPORT_SYMBOL(jedec_probe);/* Listing of parts and sizes. We need this table to learn the sector size of the chip and the total length */static const struct JEDECTable JEDEC_table[] = {{0x01AD,"AMD Am29F016",2*1024*1024,64*1024,MTD_CAP_NORFLASH}, {0x01D5,"AMD Am29F080",1*1024*1024,64*1024,MTD_CAP_NORFLASH}, {0x01A4,"AMD Am29F040",512*1024,64*1024,MTD_CAP_NORFLASH}, {}};static void jedec_sync(struct mtd_info *mtd) {};static int jedec_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);static int jedec_read_banked(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);/* Probe entry point */ struct jedec_private priv; struct mtd_info __MTD;struct mtd_info *jedec_probe(struct map_info *map){ struct mtd_info *MTD = &__MTD; unsigned long Base; unsigned long SectorSize; unsigned count; unsigned I,Uniq; char Part[200]; memset(&priv,0,sizeof(priv)); if (map->bank_size == 0) map->bank_size = map->size; if (map->size/map->bank_size > MAX_JEDEC_CHIPS) { printk("mtd: Increase MAX_JEDEC_CHIPS, too many banks.\n"); return 0; } for (Base = 0; Base < map->size; Base += map->bank_size) { // Perhaps zero could designate all tests? if (map->bus_width == 0) map->bus_width = 8; if (map->bus_width == 8) jedec_probe8(map,Base,&priv); if (map->bus_width == 16) jedec_probe16(map,Base,&priv); if (map->bus_width == 32) jedec_probe32(map,Base,&priv); } // Get the biggest sector size SectorSize = 0; for (I = 0; priv.chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++) { if (priv.chips[I].sectorsize > SectorSize) SectorSize = priv.chips[I].sectorsize; } // Quickly ensure that the other sector sizes are factors of the largest for (I = 0; priv.chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++) { if ((SectorSize/priv.chips[I].sectorsize)*priv.chips[I].sectorsize != SectorSize) { printk("mtd: Failed. Device has incompatible mixed sector sizes\n"); return 0; } } /* Generate a part name that includes the number of different chips and other configuration information */ count = 1; strncpy(Part,map->name,sizeof(Part)-10); Part[sizeof(Part)-11] = 0; strcat(Part," "); Uniq = 0; for (I = 0; priv.chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++) { const struct JEDECTable *JEDEC; if (priv.chips[I+1].jedec == priv.chips[I].jedec) { count++; continue; } // Locate the chip in the jedec table JEDEC = jedec_idtoinf(priv.chips[I].jedec >> 8,priv.chips[I].jedec); if (JEDEC == 0) { printk("mtd: Internal Error, JEDEC not set\n"); return 0; } if (Uniq != 0) strcat(Part,","); Uniq++; if (count != 1) sprintf(Part+strlen(Part),"%x*[%s]",count,JEDEC->name); else sprintf(Part+strlen(Part),"%s",JEDEC->name); if (strlen(Part) > sizeof(Part)*2/3) break; count = 1; } /* Determine if the chips are organized in a linear fashion, or if there are empty banks. Note, the last bank does not count here, only the first banks are important. Holes on non-bank boundaries can not exist due to the way the detection algorithm works. */ if (priv.size < map->bank_size) map->bank_size = priv.size; priv.is_banked = 0; for (I = 0; I != priv.size/map->bank_size - 1; I++) { if (priv.bank_fill[I] != map->bank_size) priv.is_banked = 1; /* This even could be eliminated, but new de-optimized read/write functions have to be written */ if (priv.bank_fill[I] != priv.bank_fill[0]) { printk("mtd: Failed. Cannot handle unsymetric banking\n"); return 0; } } if (priv.is_banked == 1) strcat(Part,", banked"); xprintf("Part: '%s'\n",Part); memset(MTD,0,sizeof(*MTD)); strncpy(MTD->name,Part,sizeof(MTD->name)); MTD->name[sizeof(MTD->name)-1] = 0; MTD->type = MTD_NORFLASH; MTD->flags = MTD_CAP_NORFLASH; MTD->erasesize = SectorSize*(map->bus_width/8); MTD->size = priv.size; //MTD->module = THIS_MODULE; // ? Maybe this should be the low level module? MTD->erase = flash_erase; if (priv.is_banked == 1) MTD->read = jedec_read_banked; else MTD->read = jedec_read; MTD->write = flash_write; MTD->sync = jedec_sync; MTD->priv = map; map->fldrv_priv = &priv; return MTD;}/* 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;}/* Take an array of JEDEC numbers that represent interleved flash chips and process them. Check to make sure they are good JEDEC numbers, look them up and then add them to the chip list */ int handle_jedecs(struct map_info *map,__u8 *Mfg,__u8 *Id,unsigned Count, unsigned long base,struct jedec_private *priv){ unsigned I,J; unsigned long Size; unsigned long SectorSize; const struct JEDECTable *JEDEC; // Test #2 JEDEC numbers exhibit odd parity for (I = 0; I != Count; I++) { if (checkparity(Mfg[I]) == 0 || checkparity(Id[I]) == 0) return 0; } // Finally, just make sure all the chip sizes are the same JEDEC = jedec_idtoinf(Mfg[0],Id[0]); if (JEDEC == 0) { printk("mtd: Found JEDEC flash chip, but do not have a table entry for %x:%x\n",Mfg[0],Mfg[1]); return 0; } Size = JEDEC->size; SectorSize = JEDEC->sectorsize; for (I = 0; I != Count; I++) { JEDEC = jedec_idtoinf(Mfg[0],Id[0]); if (JEDEC == 0) { printk("mtd: Found JEDEC flash chip, but do not have a table entry for %x:%x\n",Mfg[0],Mfg[1]); return 0; } if (Size != JEDEC->size || SectorSize != JEDEC->sectorsize) { printk("mtd: Failed. Interleved flash does not have matching characteristics\n"); return 0; } } // Load the Chips for (I = 0; I != MAX_JEDEC_CHIPS; I++) { if (priv->chips[I].jedec == 0) break; } if (I + Count > MAX_JEDEC_CHIPS) { printk("mtd: Device has too many chips. Increase MAX_JEDEC_CHIPS\n"); return 0; } // Add them to the table for (J = 0; J != Count; J++) { unsigned long Bank; JEDEC = jedec_idtoinf(Mfg[J],Id[J]); priv->chips[I].jedec = (Mfg[J] << 8) | Id[J]; priv->chips[I].size = JEDEC->size; priv->chips[I].sectorsize = JEDEC->sectorsize; priv->chips[I].base = base + J; priv->chips[I].datashift = J*8; priv->chips[I].capabilities = JEDEC->capabilities; priv->chips[I].offset = priv->size + J; // log2 n :| priv->chips[I].addrshift = 0; for (Bank = Count; Bank != 1; Bank >>= 1, priv->chips[I].addrshift++); // Determine how filled this bank is. Bank = base & (~(map->bank_size-1)); if (priv->bank_fill[Bank/map->bank_size] < base + (JEDEC->size << priv->chips[I].addrshift) - Bank) priv->bank_fill[Bank/map->bank_size] = base + (JEDEC->size << priv->chips[I].addrshift) - Bank; I++; } priv->size += priv->chips[I-1].size*Count; return priv->chips[I-1].size;}/* Lookup the chip information from the JEDEC ID table. */const struct JEDECTable *jedec_idtoinf(__u8 mfr,__u8 id){ __u16 Id = (mfr << 8) | id; unsigned long I = 0; for (I = 0; JEDEC_table[I].jedec != 0; I++) if (JEDEC_table[I].jedec == Id) return JEDEC_table + I; return 0;}// Look for flash using an 8 bit bus interfaceint jedec_probe8(struct map_info *map,unsigned long base, struct jedec_private *priv){ return 0;}// Look for flash using a 16 bit bus interface (ie 2 8-bit chips)int jedec_probe16(struct map_info *map,unsigned long base, struct jedec_private *priv){ return 0;}// Look for flash using a 32 bit bus interface (ie 4 8-bit chips)int jedec_probe32(struct map_info *map,unsigned long base, struct jedec_private *priv){ #define flread(x) map->read32(map,base+((x)<<2)) #define flwrite(v,x) map->write32(map,v,base+((x)<<2)) const unsigned long AutoSel1 = 0xAAAAAAAA; const unsigned long AutoSel2 = 0x55555555; const unsigned long AutoSel3 = 0x90909090; const unsigned long Reset = 0x90909090; __u32 OldVal; __u8 Mfg[4]; __u8 Id[4]; unsigned I; unsigned long Size; // Wait for any write/erase operation to settle OldVal = flread(base); for (I = 0; OldVal != flread(base) && I < 10000; I++) OldVal = flread(base); // Reset the chip flwrite(Reset,0x555); // Send the sequence flwrite(AutoSel1,0x555); flwrite(AutoSel2,0x2AA); flwrite(AutoSel3,0x555); // Test #1, JEDEC numbers are readable from 0x??00/0x??01 if (flread(0) != flread(0x100) || flread(1) != flread(0x101)) { flwrite(Reset,0x555); return 0; } // Split up the JEDEC numbers OldVal = flread(0); for (I = 0; I != 4; I++) Mfg[I] = (OldVal >> (I*8)); OldVal = flread(1); for (I = 0; I != 4; I++) Id[I] = (OldVal >> (I*8)); Size = handle_jedecs(map,Mfg,Id,4,base,priv); if (Size == 0) { flwrite(Reset,0x555); return 0; } /* Check if there is address wrap around within a single bank, if this returns JEDEC numbers then we assume that it is wrap around. Notice we call this routine with the JEDEC return still enabled, if two or more flashes have a truncated address space the probe test will still work */ if (base + Size+0x555 < map->size && base + Size+0x555 < (base & (~(map->bank_size-1))) + map->bank_size) { if (flread(base+Size) != flread(base+Size + 0x100) || flread(base+Size + 1) != flread(base+Size + 0x101)) { jedec_probe32(map,base+Size,priv); } } // Reset. flwrite(0xF0F0F0F0,0x555); return 1; #undef flread
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -