Merge git://git.infradead.org/mtd-2.6
[pandora-kernel.git] / drivers / mtd / chips / cfi_cmdset_0002.c
index c93e47d..d81079e 100644 (file)
@@ -32,6 +32,7 @@
 #include <linux/slab.h>
 #include <linux/delay.h>
 #include <linux/interrupt.h>
+#include <linux/reboot.h>
 #include <linux/mtd/compatmac.h>
 #include <linux/mtd/map.h>
 #include <linux/mtd/mtd.h>
@@ -56,6 +57,7 @@ static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
 static void cfi_amdstd_sync (struct mtd_info *);
 static int cfi_amdstd_suspend (struct mtd_info *);
 static void cfi_amdstd_resume (struct mtd_info *);
+static int cfi_amdstd_reboot(struct notifier_block *, unsigned long, void *);
 static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
 
 static void cfi_amdstd_destroy(struct mtd_info *);
@@ -256,6 +258,42 @@ static void fixup_use_atmel_lock(struct mtd_info *mtd, void *param)
        mtd->flags |= MTD_POWERUP_LOCK;
 }
 
+static void fixup_old_sst_eraseregion(struct mtd_info *mtd)
+{
+       struct map_info *map = mtd->priv;
+       struct cfi_private *cfi = map->fldrv_priv;
+
+       /*
+        * These flashes report two seperate eraseblock regions based on the
+        * sector_erase-size and block_erase-size, although they both operate on the
+        * same memory. This is not allowed according to CFI, so we just pick the
+        * sector_erase-size.
+        */
+       cfi->cfiq->NumEraseRegions = 1;
+}
+
+static void fixup_sst39vf(struct mtd_info *mtd, void *param)
+{
+       struct map_info *map = mtd->priv;
+       struct cfi_private *cfi = map->fldrv_priv;
+
+       fixup_old_sst_eraseregion(mtd);
+
+       cfi->addr_unlock1 = 0x5555;
+       cfi->addr_unlock2 = 0x2AAA;
+}
+
+static void fixup_sst39vf_rev_b(struct mtd_info *mtd, void *param)
+{
+       struct map_info *map = mtd->priv;
+       struct cfi_private *cfi = map->fldrv_priv;
+
+       fixup_old_sst_eraseregion(mtd);
+
+       cfi->addr_unlock1 = 0x555;
+       cfi->addr_unlock2 = 0x2AA;
+}
+
 static void fixup_s29gl064n_sectors(struct mtd_info *mtd, void *param)
 {
        struct map_info *map = mtd->priv;
@@ -278,6 +316,19 @@ static void fixup_s29gl032n_sectors(struct mtd_info *mtd, void *param)
        }
 }
 
+/* Used to fix CFI-Tables of chips without Extended Query Tables */
+static struct cfi_fixup cfi_nopri_fixup_table[] = {
+       { CFI_MFR_SST, 0x234A, fixup_sst39vf, NULL, }, // SST39VF1602
+       { CFI_MFR_SST, 0x234B, fixup_sst39vf, NULL, }, // SST39VF1601
+       { CFI_MFR_SST, 0x235A, fixup_sst39vf, NULL, }, // SST39VF3202
+       { CFI_MFR_SST, 0x235B, fixup_sst39vf, NULL, }, // SST39VF3201
+       { CFI_MFR_SST, 0x235C, fixup_sst39vf_rev_b, NULL, }, // SST39VF3202B
+       { CFI_MFR_SST, 0x235D, fixup_sst39vf_rev_b, NULL, }, // SST39VF3201B
+       { CFI_MFR_SST, 0x236C, fixup_sst39vf_rev_b, NULL, }, // SST39VF6402B
+       { CFI_MFR_SST, 0x236D, fixup_sst39vf_rev_b, NULL, }, // SST39VF6401B
+       { 0, 0, NULL, NULL }
+};
+
 static struct cfi_fixup cfi_fixup_table[] = {
        { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
 #ifdef AMD_BOOTLOC_BUG
@@ -351,67 +402,72 @@ struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
        mtd->name    = map->name;
        mtd->writesize = 1;
 
+       mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
+
        if (cfi->cfi_mode==CFI_MODE_CFI){
                unsigned char bootloc;
-               /*
-                * It's a real CFI chip, not one for which the probe
-                * routine faked a CFI structure. So we read the feature
-                * table from it.
-                */
                __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
                struct cfi_pri_amdstd *extp;
 
                extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
-               if (!extp) {
-                       kfree(mtd);
-                       return NULL;
-               }
-
-               cfi_fixup_major_minor(cfi, extp);
-
-               if (extp->MajorVersion != '1' ||
-                   (extp->MinorVersion < '0' || extp->MinorVersion > '4')) {
-                       printk(KERN_ERR "  Unknown Amd/Fujitsu Extended Query "
-                              "version %c.%c.\n",  extp->MajorVersion,
-                              extp->MinorVersion);
-                       kfree(extp);
-                       kfree(mtd);
-                       return NULL;
-               }
+               if (extp) {
+                       /*
+                        * It's a real CFI chip, not one for which the probe
+                        * routine faked a CFI structure.
+                        */
+                       cfi_fixup_major_minor(cfi, extp);
+
+                       if (extp->MajorVersion != '1' ||
+                           (extp->MinorVersion < '0' || extp->MinorVersion > '4')) {
+                               printk(KERN_ERR "  Unknown Amd/Fujitsu Extended Query "
+                                      "version %c.%c.\n",  extp->MajorVersion,
+                                      extp->MinorVersion);
+                               kfree(extp);
+                               kfree(mtd);
+                               return NULL;
+                       }
 
-               /* Install our own private info structure */
-               cfi->cmdset_priv = extp;
+                       /* Install our own private info structure */
+                       cfi->cmdset_priv = extp;
 
-               /* Apply cfi device specific fixups */
-               cfi_fixup(mtd, cfi_fixup_table);
+                       /* Apply cfi device specific fixups */
+                       cfi_fixup(mtd, cfi_fixup_table);
 
 #ifdef DEBUG_CFI_FEATURES
-               /* Tell the user about it in lots of lovely detail */
-               cfi_tell_features(extp);
+                       /* Tell the user about it in lots of lovely detail */
+                       cfi_tell_features(extp);
 #endif
 
-               bootloc = extp->TopBottom;
-               if ((bootloc != 2) && (bootloc != 3)) {
-                       printk(KERN_WARNING "%s: CFI does not contain boot "
-                              "bank location. Assuming top.\n", map->name);
-                       bootloc = 2;
-               }
+                       bootloc = extp->TopBottom;
+                       if ((bootloc < 2) || (bootloc > 5)) {
+                               printk(KERN_WARNING "%s: CFI contains unrecognised boot "
+                                      "bank location (%d). Assuming bottom.\n",
+                                      map->name, bootloc);
+                               bootloc = 2;
+                       }
 
-               if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
-                       printk(KERN_WARNING "%s: Swapping erase regions for broken CFI table.\n", map->name);
+                       if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
+                               printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
 
-                       for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
-                               int j = (cfi->cfiq->NumEraseRegions-1)-i;
-                               __u32 swap;
+                               for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
+                                       int j = (cfi->cfiq->NumEraseRegions-1)-i;
+                                       __u32 swap;
 
-                               swap = cfi->cfiq->EraseRegionInfo[i];
-                               cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
-                               cfi->cfiq->EraseRegionInfo[j] = swap;
+                                       swap = cfi->cfiq->EraseRegionInfo[i];
+                                       cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
+                                       cfi->cfiq->EraseRegionInfo[j] = swap;
+                               }
                        }
+                       /* Set the default CFI lock/unlock addresses */
+                       cfi->addr_unlock1 = 0x555;
+                       cfi->addr_unlock2 = 0x2aa;
+               }
+               cfi_fixup(mtd, cfi_nopri_fixup_table);
+
+               if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
+                       kfree(mtd);
+                       return NULL;
                }
-               /* Set the default CFI lock/unlock addresses */
-               cfi->addr_unlock1 = 0x555;
-               cfi->addr_unlock2 = 0x2aa;
 
        } /* CFI mode */
        else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
@@ -433,7 +489,11 @@ struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
 
        return cfi_amdstd_setup(mtd);
 }
+struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
+struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
 EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
+EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
+EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
 
 static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
 {
@@ -487,6 +547,7 @@ static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
 #endif
 
        __module_get(THIS_MODULE);
+       register_reboot_notifier(&mtd->reboot_notifier);
        return mtd;
 
  setup_err:
@@ -628,6 +689,10 @@ static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr
                chip->state = FL_READY;
                return 0;
 
+       case FL_SHUTDOWN:
+               /* The machine is rebooting */
+               return -EIO;
+
        case FL_POINT:
                /* Only if there's no operation suspended... */
                if (mode == FL_READY && chip->oldstate == FL_READY)
@@ -1918,11 +1983,58 @@ static void cfi_amdstd_resume(struct mtd_info *mtd)
        }
 }
 
+
+/*
+ * Ensure that the flash device is put back into read array mode before
+ * unloading the driver or rebooting.  On some systems, rebooting while
+ * the flash is in query/program/erase mode will prevent the CPU from
+ * fetching the bootloader code, requiring a hard reset or power cycle.
+ */
+static int cfi_amdstd_reset(struct mtd_info *mtd)
+{
+       struct map_info *map = mtd->priv;
+       struct cfi_private *cfi = map->fldrv_priv;
+       int i, ret;
+       struct flchip *chip;
+
+       for (i = 0; i < cfi->numchips; i++) {
+
+               chip = &cfi->chips[i];
+
+               mutex_lock(&chip->mutex);
+
+               ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
+               if (!ret) {
+                       map_write(map, CMD(0xF0), chip->start);
+                       chip->state = FL_SHUTDOWN;
+                       put_chip(map, chip, chip->start);
+               }
+
+               mutex_unlock(&chip->mutex);
+       }
+
+       return 0;
+}
+
+
+static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
+                              void *v)
+{
+       struct mtd_info *mtd;
+
+       mtd = container_of(nb, struct mtd_info, reboot_notifier);
+       cfi_amdstd_reset(mtd);
+       return NOTIFY_DONE;
+}
+
+
 static void cfi_amdstd_destroy(struct mtd_info *mtd)
 {
        struct map_info *map = mtd->priv;
        struct cfi_private *cfi = map->fldrv_priv;
 
+       cfi_amdstd_reset(mtd);
+       unregister_reboot_notifier(&mtd->reboot_notifier);
        kfree(cfi->cmdset_priv);
        kfree(cfi->cfiq);
        kfree(cfi);
@@ -1932,3 +2044,5 @@ static void cfi_amdstd_destroy(struct mtd_info *mtd)
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
 MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
+MODULE_ALIAS("cfi_cmdset_0006");
+MODULE_ALIAS("cfi_cmdset_0701");