📄 mapped.c
字号:
for (I = 0; map->chips[I].jedec != 0; I++) { unsigned long off; struct flash_chip *chip = map->chips + I; unsigned long base; unsigned long flags; if (chip->length == 0) continue; if (page_jump(map,chip->base + chip->start,0x555,&base,0) != 0) return -EIO; // Send the erase setup code writeb(0xF0,base + 0x555); writeb(0xAA,base + 0x555); writeb(0x55,base + 0x2AA); writeb(0x80,base + 0x555); writeb(0xAA,base + 0x555); writeb(0x55,base + 0x2AA); // Use chip erase if possible if (chip->start == 0 && chip->length == chip->size) { writeb(0x10,base+0x555); continue; } /* Once we start selecting the erase sectors the delay between each command must not exceed 50us or it will immediately start erasing and ignore the other sectors */ save_flags(flags); cli(); for (off = 0; off < chip->length; off += chip->sectorsize) { if (page_jump(map,chip->base + chip->start + off,1,&base,0) != 0) return -EIO; // Check to make sure we didn't timeout writeb(0x30,base); if ((readb(base) & (1 << 3)) != 0) { printk("mtd: Ack! We timed out the erase timer!\n"); return -EIO; } } restore_flags(flags); } /* We could split this into a timer routine and return early, performing background erasure.. Maybe later if the need warrents */ /* Poll the flash for erasure completion, specs say this can take as long as 480 seconds to do all the sectors (for a 2 meg flash). Erasure time is dependant on chip age, temp and wear.. */ Time = 0; NoTime = 0; for (I = 0; map->chips[I].jedec != 0; I++) { struct flash_chip *chip = map->chips + I; unsigned long base; unsigned long off = 0; if (chip->length == 0) continue; if (page_jump(map,chip->base + chip->start,1,&base,0) != 0) return -EIO; while (1) { unsigned char Last[4]; unsigned long Count = 0; /* During erase bit 7 is held low and bit 6 toggles, we watch this, should it stop toggling or go high then the erase is completed, or this is not really flash ;> */ Last[0] = readb(base); Last[1] = readb(base); Last[2] = readb(base); for (Count = 3; (Last[(Count - 1) % 4] & (1 << 7)) == 0 && Last[(Count - 1) % 4] != Last[(Count - 2) % 4]; Count++) { if (NoTime == 0) Time += HZ/10 - schedule_timeout(HZ/10); NoTime = 0; Last[Count % 4] = readb(base); // Count time, max of 15s per sector (according to AMD) if (Time > 15*len/mtd->erasesize*HZ) { printk("mtd: Flash Erase Timed out\n"); return -EIO; } } if (Last[(Count - 1) % 4] == Last[(Count - 2) % 4]) { flash_failed(Last[(Count - 3) % 4]); return -EIO; } // Skip to the next chip if we used chip erase if (chip->length == chip->size) off = chip->size; else off += chip->sectorsize; if (off >= chip->length) break; if (page_jump(map,chip->base + chip->start + off,1,&base,0) != 0) return -EIO; NoTime = 1; } } // Paranoid verify of erasure { unsigned long base; unsigned long buflen; while (len > 0) { unsigned long step; if (page_jump(map,start,len,&base,&buflen) != 0) return -EIO; start += buflen; len -= buflen; step = buflen/128; for (;buflen != 0; buflen -= step) { if (readb(base+buflen-1) != 0xFF) { printk("mtd: Flash Erase didn't take %lu %lu %lu\n",buflen,len,start); return -EIO; } } } } return 0;}#if 1 /*}}}*/// flash_write - Generic writing function /*{{{*/// ---------------------------------------------------------------------/* This could do parallel writes on multiple chips but doesnt, memory constraints make that infeasable. This should work with any sort of linear flash that is not interleved */extern int flash_write(struct mtd_info *mtd, loff_t start, size_t len, size_t *retlen, const u_char *buf){ struct mapped_mtd_info *map = (struct mapped_mtd_info *)mtd; unsigned long base; unsigned long off; DEBUG(1,"\n"); if (start + len > mtd->size) return -EIO; while (len != 0) { // Compute the page offset and reposition base = map->page(map,(u_long)start/map->pagesize); off = (u_long)start % map->pagesize; // Loop over this page for (; off != map->pagesize && len != 0; start++, len--, off++,buf++) { unsigned char oldbyte = readb(base+off); unsigned char Last[4]; unsigned long Count = 0; if (oldbyte == *buf) continue; if (((~oldbyte) & *buf) != 0) printk("mtd: warn: Trying to set a 0 to a 1\n"); // Write writeb(0xAA,base + 0x555); writeb(0x55,base + 0x2AA); writeb(0xA0,base + 0x555); writeb(*buf,base + off); Last[0] = readb(base + off); Last[1] = readb(base + off); Last[2] = readb(base + off); /* Wait for the flash to finish the operation. We store the last 4 status bytes that have been retrieved so we can determine why it failed. The toggle bits keep toggling when there is a failure */ for (Count = 3; Last[(Count - 1) % 4] != Last[(Count - 2) % 4] && Count < 10000; Count++) Last[Count % 4] = readb(base + off); if (Last[(Count - 1) % 4] != *buf) { flash_failed(Last[(Count - 3) % 4]); return -EIO; } } } *retlen = len; return 0;}#endif// ram_write - Generic writing function for ram /*{{{*/// ---------------------------------------------------------------------/* */extern int ram_write(struct mtd_info *mtd, loff_t start, size_t len, size_t *retlen, const u_char *buf){ struct mapped_mtd_info *map = (struct mapped_mtd_info *)mtd; unsigned long base; size_t origlen = len; unsigned long buflen; DEBUG(1,"\n"); if (start + len > mtd->size) return -EIO; while (len != 0) { // Reposition.. if (page_jump(map,start,len,&base,&buflen) != 0) return -EIO; // Copy memcpy_toio(base,buf,buflen); len -= buflen; start += buflen; } *retlen = origlen; return 0;}// rom_read - Read handler for any sort of device /*{{{*/// ---------------------------------------------------------------------/* This is a generic read function that should work with any device in the mapped region. */extern int rom_read(struct mtd_info *mtd, loff_t start, size_t len, size_t *retlen, u_char *buf){ struct mapped_mtd_info *map = (struct mapped_mtd_info *)mtd; size_t origlen = len; unsigned long base; unsigned long buflen; printk("Rom_Read\n"); if (start + len > mtd->size) return -EIO; while (len != 0) { // Reposition.. if (page_jump(map,start,len,&base,&buflen) != 0) return -EIO; // Copy memcpy_fromio(buf,base,buflen); len -= buflen; start += buflen; } *retlen = origlen; return 0;}// page_jump - Move the window and return the buffer /*{{{*/// ---------------------------------------------------------------------/* Unlike the page function this returns a buffer and length adjusted for the page dimensions and the reading offset into the page, simplifies many of the other routines */int page_jump(struct mapped_mtd_info *map,unsigned long start, unsigned long len,unsigned long *base, unsigned long *retlen){ DEBUG(1,"Page Jump\n"); if (start > map->mtd.size || start + len > map->mtd.size) return -EINVAL; *base = map->page(map,start/map->pagesize); if (*base == 0) return -EIO; *base += start % map->pagesize; // If retlen is 0 that mean the caller requires len bytes, no quibbling. if (retlen == 0) { if (len > map->pagesize - (start % map->pagesize)) return -EIO; return 0; } // Compute the buffer paramaters and return if (len > map->pagesize - (start % map->pagesize)) *retlen = map->pagesize - (start % map->pagesize); else *retlen = len; return 0;} /*}}}*/// flash_chip_scan - Intersect a region with the flash chip structure /*{{{*/// ---------------------------------------------------------------------/* This is used to enhance the speed of the erase routine, when things are being done to multiple chips it is possible to parallize the operations, particularly full memory erases of multi chip memories benifit */void flash_chip_scan(struct mapped_mtd_info *map,unsigned long start, unsigned long len){ unsigned int I = 0; DEBUG(1,"\n"); // Zero the records for (I = 0; map->chips[I].jedec != 0; I++) map->chips[I].start = map->chips[I].length = 0; // Intesect our region with the chip structures for (I = 0; map->chips[I].jedec != 0 && len != 0; I++) { // Havent found the start yet if (start >= map->chips[I].base + map->chips[I].size) continue; // Store the portion of this chip that is being effected map->chips[I].start = start - map->chips[I].base; if (len <= map->chips[I].size - map->chips[I].start) map->chips[I].length = len; else map->chips[I].length = map->chips[I].size - map->chips[I].start; len -= map->chips[I].length; start = map->chips[I].base + map->chips[I].size; }} /*}}}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -