📄 cfi_cmdset_0002.c
字号:
int ta = 0; __u32 ones = 0; retry: cfi_spin_lock(chip->mutex); if (chip->state != FL_READY){ set_current_state(TASK_UNINTERRUPTIBLE); add_wait_queue(&chip->wq, &wait); cfi_spin_unlock(chip->mutex); schedule(); remove_wait_queue(&chip->wq, &wait);#if 0 if(signal_pending(current)) return -EINTR;#endif timeo = jiffies + HZ; goto retry; } chip->state = FL_ERASING; adr += chip->start; DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n", __func__, adr ); ENABLE_VPP(map); cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL); cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL); cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL); cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL); cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL); cfi_write(map, CMD(0x30), adr); timeo = jiffies + (HZ*20); /* Wait for the end of programing/erasure by using the toggle method. * As long as there is a programming procedure going on, bit 6 * is toggling it's state with each consecutive read. * The toggling stops as soon as the procedure is completed. * * If the process has gone on for too long on the chip bit 5 gets. * After bit5 is set you can kill the operation by sending a reset * command to the chip. */ /* see comments in do_write_oneword */ dq6 = CMD(1<<6); oldstatus = cfi_read(map, adr); status = cfi_read(map, adr); DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): Check 0x%.8x 0x%.8x\n", __func__, oldstatus, status ); while( ( ( status ^ oldstatus ) & dq6 ) && ! ( ta = time_after(jiffies, timeo) ) ) { int wait_reps; /* an initial short sleep */ cfi_spin_unlock(chip->mutex); schedule_timeout(HZ/100); cfi_spin_lock(chip->mutex); if (chip->state != FL_ERASING) { /* Someone's suspended the erase. Sleep */ set_current_state(TASK_UNINTERRUPTIBLE); add_wait_queue(&chip->wq, &wait); cfi_spin_unlock(chip->mutex); printk(KERN_DEBUG "erase suspended. Sleeping\n"); schedule(); remove_wait_queue(&chip->wq, &wait);#if 0 if (signal_pending(current)) return -EINTR;#endif timeo = jiffies + (HZ*2); /* FIXME */ cfi_spin_lock(chip->mutex); continue; } /* Busy wait for 1/10 of a milisecond */ for(wait_reps = 0; (wait_reps < 100) && ( ( status ^ oldstatus ) & dq6 ); wait_reps++) { /* Latency issues. Drop the lock, wait a while and retry */ cfi_spin_unlock(chip->mutex); cfi_udelay(1); cfi_spin_lock(chip->mutex); oldstatus = cfi_read(map, adr); status = cfi_read(map, adr); DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): Check 0x%.8x 0x%.8x\n", __func__, oldstatus, status ); } oldstatus = cfi_read(map, adr); status = cfi_read(map, adr); DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): Check 0x%.8x 0x%.8x\n", __func__, oldstatus, status ); } prev_oldstatus = oldstatus; prev_status = status; oldstatus = cfi_read(map, adr); status = cfi_read(map, adr); DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): Check 0x%.8x 0x%.8x\n", __func__, oldstatus, status ); ones = CMD( (__u8)~0 ); if ( oldstatus == ones && status == ones ) { /* success - do nothing */ goto erase_done; } if ( ta ) { int dq5mask = ( ( status ^ oldstatus ) & dq6 ) >> 1; if ( status & dq5mask ) { /* dq5 asserted - decode interleave chips */ printk( KERN_WARNING "MTD %s(): FLASH internal timeout: 0x%.8x\n", __func__, status & dq5mask ); } else { printk( KERN_WARNING "MTD %s(): Software timed out during write.\n", __func__ ); } goto erase_failed; } printk(KERN_WARNING "MTD %s(): Wacky! Unable to decode failure status\n", __func__ ); printk(KERN_WARNING "MTD %s(): 0x%.8lx(0x%.8x): 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n", __func__, adr, ones, prev_oldstatus, prev_status, oldstatus, status); erase_failed: ret = -EIO; /* reset on all failures. */ cfi_write( map, CMD(0xF0), chip->start ); erase_done: DISABLE_VPP(map); chip->state = FL_READY; wake_up(&chip->wq); cfi_spin_unlock(chip->mutex); return ret;}static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr){ struct map_info *map = mtd->priv; struct cfi_private *cfi = map->fldrv_priv; unsigned long adr, len; int chipnum, ret = 0; int i, first; struct mtd_erase_region_info *regions = mtd->eraseregions; if (instr->addr > mtd->size) return -EINVAL; if ((instr->len + instr->addr) > mtd->size) return -EINVAL; /* Check that both start and end of the requested erase are * aligned with the erasesize at the appropriate addresses. */ i = 0; /* Skip all erase regions which are ended before the start of the requested erase. Actually, to save on the calculations, we skip to the first erase region which starts after the start of the requested erase, and then go back one. */ while (i < mtd->numeraseregions && instr->addr >= regions[i].offset) i++; i--; /* OK, now i is pointing at the erase region in which this erase request starts. Check the start of the requested erase range is aligned with the erase size which is in effect here. */ if (instr->addr & (regions[i].erasesize-1)) return -EINVAL; /* Remember the erase region we start on */ first = i; /* Next, check that the end of the requested erase is aligned * with the erase region at that address. */ while (i<mtd->numeraseregions && (instr->addr + instr->len) >= regions[i].offset) i++; /* As before, drop back one to point at the region in which the address actually falls */ i--; if ((instr->addr + instr->len) & (regions[i].erasesize-1)) return -EINVAL; chipnum = instr->addr >> cfi->chipshift; adr = instr->addr - (chipnum << cfi->chipshift); len = instr->len; i=first; while(len) { ret = do_erase_oneblock(map, &cfi->chips[chipnum], adr); if (ret) return ret; adr += regions[i].erasesize; len -= regions[i].erasesize; if (adr % (1<< cfi->chipshift) == ((regions[i].offset + (regions[i].erasesize * regions[i].numblocks)) %( 1<< cfi->chipshift))) i++; if (adr >> cfi->chipshift) { adr = 0; chipnum++; if (chipnum >= cfi->numchips) break; } } instr->state = MTD_ERASE_DONE; if (instr->callback) instr->callback(instr); return 0;}static int cfi_amdstd_erase_onesize(struct mtd_info *mtd, struct erase_info *instr){ struct map_info *map = mtd->priv; struct cfi_private *cfi = map->fldrv_priv; unsigned long adr, len; int chipnum, ret = 0; if (instr->addr & (mtd->erasesize - 1)) return -EINVAL; if (instr->len & (mtd->erasesize -1)) return -EINVAL; if ((instr->len + instr->addr) > mtd->size) return -EINVAL; chipnum = instr->addr >> cfi->chipshift; adr = instr->addr - (chipnum << cfi->chipshift); len = instr->len; while(len) { ret = do_erase_oneblock(map, &cfi->chips[chipnum], adr); if (ret) return ret; adr += mtd->erasesize; len -= mtd->erasesize; if (adr >> cfi->chipshift) { adr = 0; chipnum++; if (chipnum >= cfi->numchips) break; } } instr->state = MTD_ERASE_DONE; if (instr->callback) instr->callback(instr); return 0;}static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr){ struct map_info *map = mtd->priv; struct cfi_private *cfi = map->fldrv_priv; int ret = 0; if (instr->addr != 0) return -EINVAL; if (instr->len != mtd->size) return -EINVAL; ret = do_erase_chip(map, &cfi->chips[0]); if (ret) return ret; instr->state = MTD_ERASE_DONE; if (instr->callback) instr->callback(instr); return 0;}static void cfi_amdstd_sync (struct mtd_info *mtd){ struct map_info *map = mtd->priv; struct cfi_private *cfi = map->fldrv_priv; int i; struct flchip *chip; int ret = 0; DECLARE_WAITQUEUE(wait, current); for (i=0; !ret && i<cfi->numchips; i++) { chip = &cfi->chips[i]; retry: cfi_spin_lock(chip->mutex); switch(chip->state) { case FL_READY: case FL_STATUS: case FL_CFI_QUERY: case FL_JEDEC_QUERY: chip->oldstate = chip->state; chip->state = FL_SYNCING; /* No need to wake_up() on this state change - * as the whole point is that nobody can do anything * with the chip now anyway. */ case FL_SYNCING: cfi_spin_unlock(chip->mutex); break; default: /* Not an idle state */ add_wait_queue(&chip->wq, &wait); cfi_spin_unlock(chip->mutex); schedule(); remove_wait_queue(&chip->wq, &wait); goto retry; } } /* Unlock the chips again */ for (i--; i >=0; i--) { chip = &cfi->chips[i]; cfi_spin_lock(chip->mutex); if (chip->state == FL_SYNCING) { chip->state = chip->oldstate; wake_up(&chip->wq); } cfi_spin_unlock(chip->mutex); }}static int cfi_amdstd_suspend(struct mtd_info *mtd){ struct map_info *map = mtd->priv; struct cfi_private *cfi = map->fldrv_priv; int i; struct flchip *chip; int ret = 0; for (i=0; !ret && i<cfi->numchips; i++) { chip = &cfi->chips[i]; cfi_spin_lock(chip->mutex); switch(chip->state) { case FL_READY: case FL_STATUS: case FL_CFI_QUERY: case FL_JEDEC_QUERY: chip->oldstate = chip->state; chip->state = FL_PM_SUSPENDED; /* No need to wake_up() on this state change - * as the whole point is that nobody can do anything * with the chip now anyway. */ case FL_PM_SUSPENDED: break; default: ret = -EAGAIN; break; } cfi_spin_unlock(chip->mutex); } /* Unlock the chips again */ if (ret) { for (i--; i >=0; i--) { chip = &cfi->chips[i]; cfi_spin_lock(chip->mutex); if (chip->state == FL_PM_SUSPENDED) { chip->state = chip->oldstate; wake_up(&chip->wq); } cfi_spin_unlock(chip->mutex); } } return ret;}static void cfi_amdstd_resume(struct mtd_info *mtd){ struct map_info *map = mtd->priv; struct cfi_private *cfi = map->fldrv_priv; int i; struct flchip *chip; for (i=0; i<cfi->numchips; i++) { chip = &cfi->chips[i]; cfi_spin_lock(chip->mutex); if (chip->state == FL_PM_SUSPENDED) { chip->state = FL_READY; cfi_write(map, CMD(0xF0), chip->start); wake_up(&chip->wq); } else printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n"); cfi_spin_unlock(chip->mutex); }}static void cfi_amdstd_destroy(struct mtd_info *mtd){ struct map_info *map = mtd->priv; struct cfi_private *cfi = map->fldrv_priv; kfree(cfi->cmdset_priv); kfree(cfi->cfiq); kfree(cfi); kfree(mtd->eraseregions);}static char im_name[]="cfi_cmdset_0002";int __init cfi_amdstd_init(void){ inter_module_register(im_name, THIS_MODULE, &cfi_cmdset_0002); return 0;}static void __exit cfi_amdstd_exit(void){ inter_module_unregister(im_name);}module_init(cfi_amdstd_init);module_exit(cfi_amdstd_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -