]> Pileus Git - ~andy/linux/blobdiff - drivers/mtd/chips/cfi_cmdset_0001.c
[MTD] [NOR] Support for auto locking flash on power up
[~andy/linux] / drivers / mtd / chips / cfi_cmdset_0001.c
index 39edb8250fbc13f85f23347f6aff7b9a0c20d6a4..2f19fa78d24a67236d03fb4fce8651dd1ef7f05b 100644 (file)
@@ -15,6 +15,8 @@
  *     - optimized write buffer method
  * 02/05/2002  Christopher Hoover <ch@hpl.hp.com>/<ch@murgatroid.com>
  *     - reworked lock/unlock/erase support for var size flash
+ * 21/03/2007   Rodolfo Giometti <giometti@linux.it>
+ *     - auto unlock sectors on resume for auto locking flash on power up
  */
 
 #include <linux/module.h>
@@ -30,6 +32,7 @@
 #include <linux/delay.h>
 #include <linux/interrupt.h>
 #include <linux/reboot.h>
+#include <linux/bitmap.h>
 #include <linux/mtd/xip.h>
 #include <linux/mtd/map.h>
 #include <linux/mtd/mtd.h>
@@ -220,6 +223,15 @@ static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
        }
 }
 
+/*
+ * Some chips power-up with all sectors locked by default.
+ */
+static void fixup_use_powerup_lock(struct mtd_info *mtd, void *param)
+{
+       printk(KERN_INFO "Using auto-unlock on power-up/resume\n" );
+       mtd->flags |= MTD_STUPID_LOCK;
+}
+
 static struct cfi_fixup cfi_fixup_table[] = {
 #ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
        { CFI_MFR_ANY, CFI_ID_ANY, fixup_intel_strataflash, NULL },
@@ -232,6 +244,7 @@ static struct cfi_fixup cfi_fixup_table[] = {
 #endif
        { CFI_MFR_ST, 0x00ba, /* M28W320CT */ fixup_st_m28w320ct, NULL },
        { CFI_MFR_ST, 0x00bb, /* M28W320CB */ fixup_st_m28w320cb, NULL },
+       { MANUFACTURER_INTEL, 0x891c,         fixup_use_powerup_lock, NULL, },
        { 0, 0, NULL, NULL }
 };
 
@@ -337,12 +350,11 @@ struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary)
        struct mtd_info *mtd;
        int i;
 
-       mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
+       mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
        if (!mtd) {
                printk(KERN_ERR "Failed to allocate memory for MTD device\n");
                return NULL;
        }
-       memset(mtd, 0, sizeof(*mtd));
        mtd->priv = map;
        mtd->type = MTD_NORFLASH;
 
@@ -398,9 +410,23 @@ struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary)
        cfi_fixup(mtd, fixup_table);
 
        for (i=0; i< cfi->numchips; i++) {
-               cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
-               cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
-               cfi->chips[i].erase_time = 1000<<cfi->cfiq->BlockEraseTimeoutTyp;
+               if (cfi->cfiq->WordWriteTimeoutTyp)
+                       cfi->chips[i].word_write_time =
+                               1<<cfi->cfiq->WordWriteTimeoutTyp;
+               else
+                       cfi->chips[i].word_write_time = 50000;
+
+               if (cfi->cfiq->BufWriteTimeoutTyp)
+                       cfi->chips[i].buffer_write_time =
+                               1<<cfi->cfiq->BufWriteTimeoutTyp;
+               /* No default; if it isn't specified, we won't use it */
+
+               if (cfi->cfiq->BlockEraseTimeoutTyp)
+                       cfi->chips[i].erase_time =
+                               1000<<cfi->cfiq->BlockEraseTimeoutTyp;
+               else
+                       cfi->chips[i].erase_time = 2000000;
+
                cfi->chips[i].ref_point_counter = 0;
                init_waitqueue_head(&(cfi->chips[i].wq));
        }
@@ -447,6 +473,7 @@ static struct mtd_info *cfi_intelext_setup(struct mtd_info *mtd)
                        mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
                        mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
                        mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
+                       mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap = kmalloc(ernum / 8 + 1, GFP_KERNEL);
                }
                offset += (ersize * ernum);
        }
@@ -547,13 +574,11 @@ static int cfi_intelext_partition_fixup(struct mtd_info *mtd,
                        struct cfi_intelext_programming_regioninfo *prinfo;
                        prinfo = (struct cfi_intelext_programming_regioninfo *)&extp->extra[offs];
                        mtd->writesize = cfi->interleave << prinfo->ProgRegShift;
-                       MTD_PROGREGION_CTRLMODE_VALID(mtd) = cfi->interleave * prinfo->ControlValid;
-                       MTD_PROGREGION_CTRLMODE_INVALID(mtd) = cfi->interleave * prinfo->ControlInvalid;
                        mtd->flags &= ~MTD_BIT_WRITEABLE;
                        printk(KERN_DEBUG "%s: program region size/ctrl_valid/ctrl_inval = %d/%d/%d\n",
                               map->name, mtd->writesize,
-                              MTD_PROGREGION_CTRLMODE_VALID(mtd),
-                              MTD_PROGREGION_CTRLMODE_INVALID(mtd));
+                              cfi->interleave * prinfo->ControlValid,
+                              cfi->interleave * prinfo->ControlInvalid);
                }
 
                /*
@@ -908,7 +933,7 @@ static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
 
 static int __xipram xip_wait_for_operation(
                struct map_info *map, struct flchip *chip,
-               unsigned long adr, int *chip_op_time )
+               unsigned long adr, unsigned int chip_op_time )
 {
        struct cfi_private *cfi = map->fldrv_priv;
        struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
@@ -917,7 +942,7 @@ static int __xipram xip_wait_for_operation(
        flstate_t oldstate, newstate;
 
                start = xip_currtime();
-       usec = *chip_op_time * 8;
+       usec = chip_op_time * 8;
        if (usec == 0)
                usec = 500000;
        done = 0;
@@ -1027,8 +1052,8 @@ static int __xipram xip_wait_for_operation(
 #define XIP_INVAL_CACHED_RANGE(map, from, size)  \
        INVALIDATE_CACHED_RANGE(map, from, size)
 
-#define INVAL_CACHE_AND_WAIT(map, chip, cmd_adr, inval_adr, inval_len, p_usec) \
-       xip_wait_for_operation(map, chip, cmd_adr, p_usec)
+#define INVAL_CACHE_AND_WAIT(map, chip, cmd_adr, inval_adr, inval_len, usec) \
+       xip_wait_for_operation(map, chip, cmd_adr, usec)
 
 #else
 
@@ -1040,64 +1065,64 @@ static int __xipram xip_wait_for_operation(
 static int inval_cache_and_wait_for_operation(
                struct map_info *map, struct flchip *chip,
                unsigned long cmd_adr, unsigned long inval_adr, int inval_len,
-               int *chip_op_time )
+               unsigned int chip_op_time)
 {
        struct cfi_private *cfi = map->fldrv_priv;
        map_word status, status_OK = CMD(0x80);
-       int z, chip_state = chip->state;
-       unsigned long timeo;
+       int chip_state = chip->state;
+       unsigned int timeo, sleep_time;
 
        spin_unlock(chip->mutex);
        if (inval_len)
                INVALIDATE_CACHED_RANGE(map, inval_adr, inval_len);
-       if (*chip_op_time)
-               cfi_udelay(*chip_op_time);
        spin_lock(chip->mutex);
 
-       timeo = *chip_op_time * 8 * HZ / 1000000;
-       if (timeo < HZ/2)
-               timeo = HZ/2;
-       timeo += jiffies;
+       /* set our timeout to 8 times the expected delay */
+       timeo = chip_op_time * 8;
+       if (!timeo)
+               timeo = 500000;
+       sleep_time = chip_op_time / 2;
 
-       z = 0;
        for (;;) {
-               if (chip->state != chip_state) {
-                       /* Someone's suspended the operation: sleep */
-                       DECLARE_WAITQUEUE(wait, current);
-
-                       set_current_state(TASK_UNINTERRUPTIBLE);
-                       add_wait_queue(&chip->wq, &wait);
-                       spin_unlock(chip->mutex);
-                       schedule();
-                       remove_wait_queue(&chip->wq, &wait);
-                       timeo = jiffies + (HZ / 2); /* FIXME */
-                       spin_lock(chip->mutex);
-                       continue;
-               }
-
                status = map_read(map, cmd_adr);
                if (map_word_andequal(map, status, status_OK, status_OK))
                        break;
 
-               /* OK Still waiting */
-               if (time_after(jiffies, timeo)) {
+               if (!timeo) {
                        map_write(map, CMD(0x70), cmd_adr);
                        chip->state = FL_STATUS;
                        return -ETIME;
                }
 
-               /* Latency issues. Drop the lock, wait a while and retry */
-               z++;
+               /* OK Still waiting. Drop the lock, wait a while and retry. */
                spin_unlock(chip->mutex);
-               cfi_udelay(1);
+               if (sleep_time >= 1000000/HZ) {
+                       /*
+                        * Half of the normal delay still remaining
+                        * can be performed with a sleeping delay instead
+                        * of busy waiting.
+                        */
+                       msleep(sleep_time/1000);
+                       timeo -= sleep_time;
+                       sleep_time = 1000000/HZ;
+               } else {
+                       udelay(1);
+                       cond_resched();
+                       timeo--;
+               }
                spin_lock(chip->mutex);
-       }
 
-       if (!z) {
-               if (!--(*chip_op_time))
-                       *chip_op_time = 1;
-       } else if (z > 1)
-               ++(*chip_op_time);
+               while (chip->state != chip_state) {
+                       /* Someone's suspended the operation: sleep */
+                       DECLARE_WAITQUEUE(wait, current);
+                       set_current_state(TASK_UNINTERRUPTIBLE);
+                       add_wait_queue(&chip->wq, &wait);
+                       spin_unlock(chip->mutex);
+                       schedule();
+                       remove_wait_queue(&chip->wq, &wait);
+                       spin_lock(chip->mutex);
+               }
+       }
 
        /* Done and happy. */
        chip->state = FL_STATUS;
@@ -1107,8 +1132,7 @@ static int inval_cache_and_wait_for_operation(
 #endif
 
 #define WAIT_TIMEOUT(map, chip, adr, udelay) \
-       ({ int __udelay = (udelay); \
-          INVAL_CACHE_AND_WAIT(map, chip, adr, 0, 0, &__udelay); })
+       INVAL_CACHE_AND_WAIT(map, chip, adr, 0, 0, udelay);
 
 
 static int do_point_onechip (struct map_info *map, struct flchip *chip, loff_t adr, size_t len)
@@ -1332,7 +1356,7 @@ static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
 
        ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
                                   adr, map_bankwidth(map),
-                                  &chip->word_write_time);
+                                  chip->word_write_time);
        if (ret) {
                xip_enable(map, chip, adr);
                printk(KERN_ERR "%s: word write error (status timeout)\n", map->name);
@@ -1569,7 +1593,7 @@ static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
 
        ret = INVAL_CACHE_AND_WAIT(map, chip, cmd_adr,
                                   adr, len,
-                                  &chip->buffer_write_time);
+                                  chip->buffer_write_time);
        if (ret) {
                map_write(map, CMD(0x70), cmd_adr);
                chip->state = FL_STATUS;
@@ -1704,7 +1728,7 @@ static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip,
 
        ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
                                   adr, len,
-                                  &chip->erase_time);
+                                  chip->erase_time);
        if (ret) {
                map_write(map, CMD(0x70), adr);
                chip->state = FL_STATUS;
@@ -1815,8 +1839,7 @@ static void cfi_intelext_sync (struct mtd_info *mtd)
        }
 }
 
-#ifdef DEBUG_LOCK_BITS
-static int __xipram do_printlockstatus_oneblock(struct map_info *map,
+static int __xipram do_getlockstatus_oneblock(struct map_info *map,
                                                struct flchip *chip,
                                                unsigned long adr,
                                                int len, void *thunk)
@@ -1830,8 +1853,17 @@ static int __xipram do_printlockstatus_oneblock(struct map_info *map,
        chip->state = FL_JEDEC_QUERY;
        status = cfi_read_query(map, adr+(2*ofs_factor));
        xip_enable(map, chip, 0);
+       return status;
+}
+
+#ifdef DEBUG_LOCK_BITS
+static int __xipram do_printlockstatus_oneblock(struct map_info *map,
+                                               struct flchip *chip,
+                                               unsigned long adr,
+                                               int len, void *thunk)
+{
        printk(KERN_DEBUG "block status register for 0x%08lx is %x\n",
-              adr, status);
+              adr, do_getlockstatus_oneblock(map, chip, adr, len, thunk));
        return 0;
 }
 #endif
@@ -2206,14 +2238,45 @@ static int cfi_intelext_get_user_prot_info(struct mtd_info *mtd,
 
 #endif
 
+static void cfi_intelext_save_locks(struct mtd_info *mtd)
+{
+       struct mtd_erase_region_info *region;
+       int block, status, i;
+       unsigned long adr;
+       size_t len;
+
+       for (i = 0; i < mtd->numeraseregions; i++) {
+               region = &mtd->eraseregions[i];
+               if (!region->lockmap)
+                       continue;
+
+               for (block = 0; block < region->numblocks; block++){
+                       len = region->erasesize;
+                       adr = region->offset + block * len;
+
+                       status = cfi_varsize_frob(mtd,
+                                       do_getlockstatus_oneblock, adr, len, 0);
+                       if (status)
+                               set_bit(block, region->lockmap);
+                       else
+                               clear_bit(block, region->lockmap);
+               }
+       }
+}
+
 static int cfi_intelext_suspend(struct mtd_info *mtd)
 {
        struct map_info *map = mtd->priv;
        struct cfi_private *cfi = map->fldrv_priv;
+       struct cfi_pri_intelext *extp = cfi->cmdset_priv;
        int i;
        struct flchip *chip;
        int ret = 0;
 
+       if ((mtd->flags & MTD_STUPID_LOCK)
+           && extp && (extp->FeatureSupport & (1 << 5)))
+               cfi_intelext_save_locks(mtd);
+
        for (i=0; !ret && i<cfi->numchips; i++) {
                chip = &cfi->chips[i];
 
@@ -2225,6 +2288,8 @@ static int cfi_intelext_suspend(struct mtd_info *mtd)
                case FL_CFI_QUERY:
                case FL_JEDEC_QUERY:
                        if (chip->oldstate == FL_READY) {
+                               /* place the chip in a known state before suspend */
+                               map_write(map, CMD(0xFF), cfi->chips[i].start);
                                chip->oldstate = chip->state;
                                chip->state = FL_PM_SUSPENDED;
                                /* No need to wake_up() on this state change -
@@ -2273,10 +2338,33 @@ static int cfi_intelext_suspend(struct mtd_info *mtd)
        return ret;
 }
 
+static void cfi_intelext_restore_locks(struct mtd_info *mtd)
+{
+       struct mtd_erase_region_info *region;
+       int block, i;
+       unsigned long adr;
+       size_t len;
+
+       for (i = 0; i < mtd->numeraseregions; i++) {
+               region = &mtd->eraseregions[i];
+               if (!region->lockmap)
+                       continue;
+
+               for (block = 0; block < region->numblocks; block++) {
+                       len = region->erasesize;
+                       adr = region->offset + block * len;
+
+                       if (!test_bit(block, region->lockmap))
+                               cfi_intelext_unlock(mtd, adr, len);
+               }
+       }
+}
+
 static void cfi_intelext_resume(struct mtd_info *mtd)
 {
        struct map_info *map = mtd->priv;
        struct cfi_private *cfi = map->fldrv_priv;
+       struct cfi_pri_intelext *extp = cfi->cmdset_priv;
        int i;
        struct flchip *chip;
 
@@ -2295,6 +2383,10 @@ static void cfi_intelext_resume(struct mtd_info *mtd)
 
                spin_unlock(chip->mutex);
        }
+
+       if ((mtd->flags & MTD_STUPID_LOCK)
+           && extp && (extp->FeatureSupport & (1 << 5)))
+               cfi_intelext_restore_locks(mtd);
 }
 
 static int cfi_intelext_reset(struct mtd_info *mtd)
@@ -2335,12 +2427,19 @@ static void cfi_intelext_destroy(struct mtd_info *mtd)
 {
        struct map_info *map = mtd->priv;
        struct cfi_private *cfi = map->fldrv_priv;
+       struct mtd_erase_region_info *region;
+       int i;
        cfi_intelext_reset(mtd);
        unregister_reboot_notifier(&mtd->reboot_notifier);
        kfree(cfi->cmdset_priv);
        kfree(cfi->cfiq);
        kfree(cfi->chips[0].priv);
        kfree(cfi);
+       for (i = 0; i < mtd->numeraseregions; i++) {
+               region = &mtd->eraseregions[i];
+               if (region->lockmap)
+                       kfree(region->lockmap);
+       }
        kfree(mtd->eraseregions);
 }