drivers/usb/net/kaweth.c: add device "Allied Telesyn AT-USB10 USB Ethernet Adapter"
[safe/jmp/linux-2.6] / drivers / mtd / devices / mtd_dataflash.c
index 04271d0..1981740 100644 (file)
@@ -9,12 +9,15 @@
  * as published by the Free Software Foundation; either version
  * 2 of the License, or (at your option) any later version.
 */
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/slab.h>
 #include <linux/delay.h>
 #include <linux/device.h>
+#include <linux/mutex.h>
+#include <linux/err.h>
+#include <linux/math64.h>
+
 #include <linux/spi/spi.h>
 #include <linux/spi/flash.h>
 
  * doesn't (yet) use these for any kind of i/o overlap or prefetching.
  *
  * Sometimes DataFlash is packaged in MMC-format cards, although the
- * MMC stack can't use SPI (yet), or distinguish between MMC and DataFlash
+ * MMC stack can't (yet?) distinguish between MMC and DataFlash
  * protocols during enumeration.
  */
 
-#define CONFIG_DATAFLASH_WRITE_VERIFY
-
 /* reads can bypass the buffers */
 #define OP_READ_CONTINUOUS     0xE8
 #define OP_READ_PAGE           0xD2
  */
 #define OP_READ_ID             0x9F
 #define OP_READ_SECURITY       0x77
-#define OP_WRITE_SECURITY      0x9A    /* OTP bits */
+#define OP_WRITE_SECURITY_REVC 0x9A
+#define OP_WRITE_SECURITY      0x9B    /* revision D */
 
 
 struct dataflash {
-       u8                      command[4];
+       uint8_t                 command[4];
        char                    name[24];
 
        unsigned                partitioned:1;
@@ -90,18 +92,12 @@ struct dataflash {
        unsigned short          page_offset;    /* offset in flash address */
        unsigned int            page_size;      /* of bytes per page */
 
-       struct semaphore        lock;
+       struct mutex            lock;
        struct spi_device       *spi;
 
        struct mtd_info         mtd;
 };
 
-#ifdef CONFIG_MTD_PARTITIONS
-#define        mtd_has_partitions()    (1)
-#else
-#define        mtd_has_partitions()    (0)
-#endif
-
 /* ......................................................................... */
 
 /*
@@ -127,7 +123,7 @@ static int dataflash_waitready(struct spi_device *spi)
                status = dataflash_status(spi);
                if (status < 0) {
                        DEBUG(MTD_DEBUG_LEVEL1, "%s: status %d?\n",
-                                       spi->dev.bus_id, status);
+                                       dev_name(&spi->dev), status);
                        status = 0;
                }
 
@@ -150,16 +146,21 @@ static int dataflash_erase(struct mtd_info *mtd, struct erase_info *instr)
        struct spi_transfer     x = { .tx_dma = 0, };
        struct spi_message      msg;
        unsigned                blocksize = priv->page_size << 3;
-       u8                      *command;
+       uint8_t                 *command;
+       uint32_t                rem;
 
-       DEBUG(MTD_DEBUG_LEVEL2, "%s: erase addr=0x%x len 0x%x\n",
-                       spi->dev.bus_id,
-                       instr->addr, instr->len);
+       DEBUG(MTD_DEBUG_LEVEL2, "%s: erase addr=0x%llx len 0x%llx\n",
+             dev_name(&spi->dev), (long long)instr->addr,
+             (long long)instr->len);
 
        /* Sanity checks */
-       if ((instr->addr + instr->len) > mtd->size
-                       || (instr->len % priv->page_size) != 0
-                       || (instr->addr % priv->page_size) != 0)
+       if (instr->addr + instr->len > mtd->size)
+               return -EINVAL;
+       div_u64_rem(instr->len, priv->page_size, &rem);
+       if (rem)
+               return -EINVAL;
+       div_u64_rem(instr->addr, priv->page_size, &rem);
+       if (rem)
                return -EINVAL;
 
        spi_message_init(&msg);
@@ -168,7 +169,7 @@ static int dataflash_erase(struct mtd_info *mtd, struct erase_info *instr)
        x.len = 4;
        spi_message_add_tail(&x, &msg);
 
-       down(&priv->lock);
+       mutex_lock(&priv->lock);
        while (instr->len > 0) {
                unsigned int    pageaddr;
                int             status;
@@ -177,13 +178,13 @@ static int dataflash_erase(struct mtd_info *mtd, struct erase_info *instr)
                /* Calculate flash page address; use block erase (for speed) if
                 * we're at a block boundary and need to erase the whole block.
                 */
-               pageaddr = instr->addr / priv->page_size;
+               pageaddr = div_u64(instr->addr, priv->page_size);
                do_block = (pageaddr & 0x7) == 0 && instr->len >= blocksize;
                pageaddr = pageaddr << priv->page_offset;
 
                command[0] = do_block ? OP_ERASE_BLOCK : OP_ERASE_PAGE;
-               command[1] = (u8)(pageaddr >> 16);
-               command[2] = (u8)(pageaddr >> 8);
+               command[1] = (uint8_t)(pageaddr >> 16);
+               command[2] = (uint8_t)(pageaddr >> 8);
                command[3] = 0;
 
                DEBUG(MTD_DEBUG_LEVEL3, "ERASE %s: (%x) %x %x %x [%i]\n",
@@ -196,7 +197,7 @@ static int dataflash_erase(struct mtd_info *mtd, struct erase_info *instr)
 
                if (status < 0) {
                        printk(KERN_ERR "%s: erase %x, err %d\n",
-                               spi->dev.bus_id, pageaddr, status);
+                               dev_name(&spi->dev), pageaddr, status);
                        /* REVISIT:  can retry instr->retries times; or
                         * giveup and instr->fail_addr = instr->addr;
                         */
@@ -211,7 +212,7 @@ static int dataflash_erase(struct mtd_info *mtd, struct erase_info *instr)
                        instr->len -= priv->page_size;
                }
        }
-       up(&priv->lock);
+       mutex_unlock(&priv->lock);
 
        /* Inform MTD subsystem that erase is complete */
        instr->state = MTD_ERASE_DONE;
@@ -234,11 +235,11 @@ static int dataflash_read(struct mtd_info *mtd, loff_t from, size_t len,
        struct spi_transfer     x[2] = { { .tx_dma = 0, }, };
        struct spi_message      msg;
        unsigned int            addr;
-       u8                      *command;
+       uint8_t                 *command;
        int                     status;
 
        DEBUG(MTD_DEBUG_LEVEL2, "%s: read 0x%x..0x%x\n",
-               priv->spi->dev.bus_id, (unsigned)from, (unsigned)(from + len));
+               dev_name(&priv->spi->dev), (unsigned)from, (unsigned)(from + len));
 
        *retlen = 0;
 
@@ -267,27 +268,27 @@ static int dataflash_read(struct mtd_info *mtd, loff_t from, size_t len,
        x[1].len = len;
        spi_message_add_tail(&x[1], &msg);
 
-       down(&priv->lock);
+       mutex_lock(&priv->lock);
 
        /* Continuous read, max clock = f(car) which may be less than
         * the peak rate available.  Some chips support commands with
         * fewer "don't care" bytes.  Both buffers stay unchanged.
         */
        command[0] = OP_READ_CONTINUOUS;
-       command[1] = (u8)(addr >> 16);
-       command[2] = (u8)(addr >> 8);
-       command[3] = (u8)(addr >> 0);
+       command[1] = (uint8_t)(addr >> 16);
+       command[2] = (uint8_t)(addr >> 8);
+       command[3] = (uint8_t)(addr >> 0);
        /* plus 4 "don't care" bytes */
 
        status = spi_sync(priv->spi, &msg);
-       up(&priv->lock);
+       mutex_unlock(&priv->lock);
 
        if (status >= 0) {
                *retlen = msg.actual_length - 8;
                status = 0;
        } else
                DEBUG(MTD_DEBUG_LEVEL1, "%s: read %x..%x --> %d\n",
-                       priv->spi->dev.bus_id,
+                       dev_name(&priv->spi->dev),
                        (unsigned)from, (unsigned)(from + len),
                        status);
        return status;
@@ -311,10 +312,10 @@ static int dataflash_write(struct mtd_info *mtd, loff_t to, size_t len,
        size_t                  remaining = len;
        u_char                  *writebuf = (u_char *) buf;
        int                     status = -EINVAL;
-       u8                      *command;
+       uint8_t                 *command;
 
        DEBUG(MTD_DEBUG_LEVEL2, "%s: write 0x%x..0x%x\n",
-               spi->dev.bus_id, (unsigned)to, (unsigned)(to + len));
+               dev_name(&spi->dev), (unsigned)to, (unsigned)(to + len));
 
        *retlen = 0;
 
@@ -337,7 +338,7 @@ static int dataflash_write(struct mtd_info *mtd, loff_t to, size_t len,
        else
                writelen = len;
 
-       down(&priv->lock);
+       mutex_lock(&priv->lock);
        while (remaining > 0) {
                DEBUG(MTD_DEBUG_LEVEL3, "write @ %i:%i len=%i\n",
                        pageaddr, offset, writelen);
@@ -373,7 +374,7 @@ static int dataflash_write(struct mtd_info *mtd, loff_t to, size_t len,
                        status = spi_sync(spi, &msg);
                        if (status < 0)
                                DEBUG(MTD_DEBUG_LEVEL1, "%s: xfer %u -> %d \n",
-                                       spi->dev.bus_id, addr, status);
+                                       dev_name(&spi->dev), addr, status);
 
                        (void) dataflash_waitready(priv->spi);
                }
@@ -395,12 +396,12 @@ static int dataflash_write(struct mtd_info *mtd, loff_t to, size_t len,
                spi_transfer_del(x + 1);
                if (status < 0)
                        DEBUG(MTD_DEBUG_LEVEL1, "%s: pgm %u/%u -> %d \n",
-                               spi->dev.bus_id, addr, writelen, status);
+                               dev_name(&spi->dev), addr, writelen, status);
 
                (void) dataflash_waitready(priv->spi);
 
 
-#ifdef CONFIG_DATAFLASH_WRITE_VERIFY
+#ifdef CONFIG_MTD_DATAFLASH_WRITE_VERIFY
 
                /* (3) Compare to Buffer1 */
                addr = pageaddr << priv->page_offset;
@@ -415,21 +416,21 @@ static int dataflash_write(struct mtd_info *mtd, loff_t to, size_t len,
                status = spi_sync(spi, &msg);
                if (status < 0)
                        DEBUG(MTD_DEBUG_LEVEL1, "%s: compare %u -> %d \n",
-                               spi->dev.bus_id, addr, status);
+                               dev_name(&spi->dev), addr, status);
 
                status = dataflash_waitready(priv->spi);
 
                /* Check result of the compare operation */
-               if ((status & (1 << 6)) == 1) {
+               if (status & (1 << 6)) {
                        printk(KERN_ERR "%s: compare page %u, err %d\n",
-                               spi->dev.bus_id, pageaddr, status);
+                               dev_name(&spi->dev), pageaddr, status);
                        remaining = 0;
                        status = -EIO;
                        break;
                } else
                        status = 0;
 
-#endif /* CONFIG_DATAFLASH_WRITE_VERIFY */
+#endif /* CONFIG_MTD_DATAFLASH_WRITE_VERIFY */
 
                remaining = remaining - writelen;
                pageaddr++;
@@ -442,29 +443,206 @@ static int dataflash_write(struct mtd_info *mtd, loff_t to, size_t len,
                else
                        writelen = remaining;
        }
-       up(&priv->lock);
+       mutex_unlock(&priv->lock);
 
        return status;
 }
 
 /* ......................................................................... */
 
+#ifdef CONFIG_MTD_DATAFLASH_OTP
+
+static int dataflash_get_otp_info(struct mtd_info *mtd,
+               struct otp_info *info, size_t len)
+{
+       /* Report both blocks as identical:  bytes 0..64, locked.
+        * Unless the user block changed from all-ones, we can't
+        * tell whether it's still writable; so we assume it isn't.
+        */
+       info->start = 0;
+       info->length = 64;
+       info->locked = 1;
+       return sizeof(*info);
+}
+
+static ssize_t otp_read(struct spi_device *spi, unsigned base,
+               uint8_t *buf, loff_t off, size_t len)
+{
+       struct spi_message      m;
+       size_t                  l;
+       uint8_t                 *scratch;
+       struct spi_transfer     t;
+       int                     status;
+
+       if (off > 64)
+               return -EINVAL;
+
+       if ((off + len) > 64)
+               len = 64 - off;
+       if (len == 0)
+               return len;
+
+       spi_message_init(&m);
+
+       l = 4 + base + off + len;
+       scratch = kzalloc(l, GFP_KERNEL);
+       if (!scratch)
+               return -ENOMEM;
+
+       /* OUT: OP_READ_SECURITY, 3 don't-care bytes, zeroes
+        * IN:  ignore 4 bytes, data bytes 0..N (max 127)
+        */
+       scratch[0] = OP_READ_SECURITY;
+
+       memset(&t, 0, sizeof t);
+       t.tx_buf = scratch;
+       t.rx_buf = scratch;
+       t.len = l;
+       spi_message_add_tail(&t, &m);
+
+       dataflash_waitready(spi);
+
+       status = spi_sync(spi, &m);
+       if (status >= 0) {
+               memcpy(buf, scratch + 4 + base + off, len);
+               status = len;
+       }
+
+       kfree(scratch);
+       return status;
+}
+
+static int dataflash_read_fact_otp(struct mtd_info *mtd,
+               loff_t from, size_t len, size_t *retlen, u_char *buf)
+{
+       struct dataflash        *priv = (struct dataflash *)mtd->priv;
+       int                     status;
+
+       /* 64 bytes, from 0..63 ... start at 64 on-chip */
+       mutex_lock(&priv->lock);
+       status = otp_read(priv->spi, 64, buf, from, len);
+       mutex_unlock(&priv->lock);
+
+       if (status < 0)
+               return status;
+       *retlen = status;
+       return 0;
+}
+
+static int dataflash_read_user_otp(struct mtd_info *mtd,
+               loff_t from, size_t len, size_t *retlen, u_char *buf)
+{
+       struct dataflash        *priv = (struct dataflash *)mtd->priv;
+       int                     status;
+
+       /* 64 bytes, from 0..63 ... start at 0 on-chip */
+       mutex_lock(&priv->lock);
+       status = otp_read(priv->spi, 0, buf, from, len);
+       mutex_unlock(&priv->lock);
+
+       if (status < 0)
+               return status;
+       *retlen = status;
+       return 0;
+}
+
+static int dataflash_write_user_otp(struct mtd_info *mtd,
+               loff_t from, size_t len, size_t *retlen, u_char *buf)
+{
+       struct spi_message      m;
+       const size_t            l = 4 + 64;
+       uint8_t                 *scratch;
+       struct spi_transfer     t;
+       struct dataflash        *priv = (struct dataflash *)mtd->priv;
+       int                     status;
+
+       if (len > 64)
+               return -EINVAL;
+
+       /* Strictly speaking, we *could* truncate the write ... but
+        * let's not do that for the only write that's ever possible.
+        */
+       if ((from + len) > 64)
+               return -EINVAL;
+
+       /* OUT: OP_WRITE_SECURITY, 3 zeroes, 64 data-or-zero bytes
+        * IN:  ignore all
+        */
+       scratch = kzalloc(l, GFP_KERNEL);
+       if (!scratch)
+               return -ENOMEM;
+       scratch[0] = OP_WRITE_SECURITY;
+       memcpy(scratch + 4 + from, buf, len);
+
+       spi_message_init(&m);
+
+       memset(&t, 0, sizeof t);
+       t.tx_buf = scratch;
+       t.len = l;
+       spi_message_add_tail(&t, &m);
+
+       /* Write the OTP bits, if they've not yet been written.
+        * This modifies SRAM buffer1.
+        */
+       mutex_lock(&priv->lock);
+       dataflash_waitready(priv->spi);
+       status = spi_sync(priv->spi, &m);
+       mutex_unlock(&priv->lock);
+
+       kfree(scratch);
+
+       if (status >= 0) {
+               status = 0;
+               *retlen = len;
+       }
+       return status;
+}
+
+static char *otp_setup(struct mtd_info *device, char revision)
+{
+       device->get_fact_prot_info = dataflash_get_otp_info;
+       device->read_fact_prot_reg = dataflash_read_fact_otp;
+       device->get_user_prot_info = dataflash_get_otp_info;
+       device->read_user_prot_reg = dataflash_read_user_otp;
+
+       /* rev c parts (at45db321c and at45db1281 only!) use a
+        * different write procedure; not (yet?) implemented.
+        */
+       if (revision > 'c')
+               device->write_user_prot_reg = dataflash_write_user_otp;
+
+       return ", OTP";
+}
+
+#else
+
+static char *otp_setup(struct mtd_info *device, char revision)
+{
+       return " (OTP)";
+}
+
+#endif
+
+/* ......................................................................... */
+
 /*
  * Register DataFlash device with MTD subsystem.
  */
 static int __devinit
-add_dataflash(struct spi_device *spi, char *name,
-               int nr_pages, int pagesize, int pageoffset)
+add_dataflash_otp(struct spi_device *spi, char *name,
+               int nr_pages, int pagesize, int pageoffset, char revision)
 {
        struct dataflash                *priv;
        struct mtd_info                 *device;
        struct flash_platform_data      *pdata = spi->dev.platform_data;
+       char                            *otp_tag = "";
+       int                             err = 0;
 
-       priv = (struct dataflash *) kzalloc(sizeof *priv, GFP_KERNEL);
+       priv = kzalloc(sizeof *priv, GFP_KERNEL);
        if (!priv)
                return -ENOMEM;
 
-       init_MUTEX(&priv->lock);
+       mutex_init(&priv->lock);
        priv->spi = spi;
        priv->page_size = pagesize;
        priv->page_offset = pageoffset;
@@ -481,24 +659,33 @@ add_dataflash(struct spi_device *spi, char *name,
        device->writesize = pagesize;
        device->owner = THIS_MODULE;
        device->type = MTD_DATAFLASH;
-       device->flags = MTD_CAP_NORFLASH;
+       device->flags = MTD_WRITEABLE;
        device->erase = dataflash_erase;
        device->read = dataflash_read;
        device->write = dataflash_write;
        device->priv = priv;
 
-       dev_info(&spi->dev, "%s (%d KBytes)\n", name, device->size/1024);
+       device->dev.parent = &spi->dev;
+
+       if (revision >= 'c')
+               otp_tag = otp_setup(device, revision);
+
+       dev_info(&spi->dev, "%s (%lld KBytes) pagesize %d bytes%s\n",
+                       name, (long long)((device->size + 1023) >> 10),
+                       pagesize, otp_tag);
        dev_set_drvdata(&spi->dev, priv);
 
        if (mtd_has_partitions()) {
                struct mtd_partition    *parts;
                int                     nr_parts = 0;
 
-#ifdef CONFIG_MTD_CMDLINE_PARTS
-               static const char *part_probes[] = { "cmdlinepart", NULL, };
+               if (mtd_has_cmdlinepart()) {
+                       static const char *part_probes[]
+                                       = { "cmdlinepart", NULL, };
 
-               nr_parts = parse_mtd_partitions(device, part_probes, &parts, 0);
-#endif
+                       nr_parts = parse_mtd_partitions(device,
+                                       part_probes, &parts, 0);
+               }
 
                if (nr_parts <= 0 && pdata && pdata->parts) {
                        parts = pdata->parts;
@@ -507,21 +694,163 @@ add_dataflash(struct spi_device *spi, char *name,
 
                if (nr_parts > 0) {
                        priv->partitioned = 1;
-                       return add_mtd_partitions(device, parts, nr_parts);
+                       err = add_mtd_partitions(device, parts, nr_parts);
+                       goto out;
                }
        } else if (pdata && pdata->nr_parts)
                dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
                                pdata->nr_parts, device->name);
 
-       return add_mtd_device(device) == 1 ? -ENODEV : 0;
+       if (add_mtd_device(device) == 1)
+               err = -ENODEV;
+
+out:
+       if (!err)
+               return 0;
+
+       dev_set_drvdata(&spi->dev, NULL);
+       kfree(priv);
+       return err;
+}
+
+static inline int __devinit
+add_dataflash(struct spi_device *spi, char *name,
+               int nr_pages, int pagesize, int pageoffset)
+{
+       return add_dataflash_otp(spi, name, nr_pages, pagesize,
+                       pageoffset, 0);
+}
+
+struct flash_info {
+       char            *name;
+
+       /* JEDEC id has a high byte of zero plus three data bytes:
+        * the manufacturer id, then a two byte device id.
+        */
+       uint32_t        jedec_id;
+
+       /* The size listed here is what works with OP_ERASE_PAGE. */
+       unsigned        nr_pages;
+       uint16_t        pagesize;
+       uint16_t        pageoffset;
+
+       uint16_t        flags;
+#define SUP_POW2PS     0x0002          /* supports 2^N byte pages */
+#define IS_POW2PS      0x0001          /* uses 2^N byte pages */
+};
+
+static struct flash_info __devinitdata dataflash_data [] = {
+
+       /*
+        * NOTE:  chips with SUP_POW2PS (rev D and up) need two entries,
+        * one with IS_POW2PS and the other without.  The entry with the
+        * non-2^N byte page size can't name exact chip revisions without
+        * losing backwards compatibility for cmdlinepart.
+        *
+        * These newer chips also support 128-byte security registers (with
+        * 64 bytes one-time-programmable) and software write-protection.
+        */
+       { "AT45DB011B",  0x1f2200, 512, 264, 9, SUP_POW2PS},
+       { "at45db011d",  0x1f2200, 512, 256, 8, SUP_POW2PS | IS_POW2PS},
+
+       { "AT45DB021B",  0x1f2300, 1024, 264, 9, SUP_POW2PS},
+       { "at45db021d",  0x1f2300, 1024, 256, 8, SUP_POW2PS | IS_POW2PS},
+
+       { "AT45DB041x",  0x1f2400, 2048, 264, 9, SUP_POW2PS},
+       { "at45db041d",  0x1f2400, 2048, 256, 8, SUP_POW2PS | IS_POW2PS},
+
+       { "AT45DB081B",  0x1f2500, 4096, 264, 9, SUP_POW2PS},
+       { "at45db081d",  0x1f2500, 4096, 256, 8, SUP_POW2PS | IS_POW2PS},
+
+       { "AT45DB161x",  0x1f2600, 4096, 528, 10, SUP_POW2PS},
+       { "at45db161d",  0x1f2600, 4096, 512, 9, SUP_POW2PS | IS_POW2PS},
+
+       { "AT45DB321x",  0x1f2700, 8192, 528, 10, 0},           /* rev C */
+
+       { "AT45DB321x",  0x1f2701, 8192, 528, 10, SUP_POW2PS},
+       { "at45db321d",  0x1f2701, 8192, 512, 9, SUP_POW2PS | IS_POW2PS},
+
+       { "AT45DB642x",  0x1f2800, 8192, 1056, 11, SUP_POW2PS},
+       { "at45db642d",  0x1f2800, 8192, 1024, 10, SUP_POW2PS | IS_POW2PS},
+};
+
+static struct flash_info *__devinit jedec_probe(struct spi_device *spi)
+{
+       int                     tmp;
+       uint8_t                 code = OP_READ_ID;
+       uint8_t                 id[3];
+       uint32_t                jedec;
+       struct flash_info       *info;
+       int status;
+
+       /* JEDEC also defines an optional "extended device information"
+        * string for after vendor-specific data, after the three bytes
+        * we use here.  Supporting some chips might require using it.
+        *
+        * If the vendor ID isn't Atmel's (0x1f), assume this call failed.
+        * That's not an error; only rev C and newer chips handle it, and
+        * only Atmel sells these chips.
+        */
+       tmp = spi_write_then_read(spi, &code, 1, id, 3);
+       if (tmp < 0) {
+               DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
+                       dev_name(&spi->dev), tmp);
+               return ERR_PTR(tmp);
+       }
+       if (id[0] != 0x1f)
+               return NULL;
+
+       jedec = id[0];
+       jedec = jedec << 8;
+       jedec |= id[1];
+       jedec = jedec << 8;
+       jedec |= id[2];
+
+       for (tmp = 0, info = dataflash_data;
+                       tmp < ARRAY_SIZE(dataflash_data);
+                       tmp++, info++) {
+               if (info->jedec_id == jedec) {
+                       DEBUG(MTD_DEBUG_LEVEL1, "%s: OTP, sector protect%s\n",
+                               dev_name(&spi->dev),
+                               (info->flags & SUP_POW2PS)
+                                       ? ", binary pagesize" : ""
+                               );
+                       if (info->flags & SUP_POW2PS) {
+                               status = dataflash_status(spi);
+                               if (status < 0) {
+                                       DEBUG(MTD_DEBUG_LEVEL1,
+                                               "%s: status error %d\n",
+                                               dev_name(&spi->dev), status);
+                                       return ERR_PTR(status);
+                               }
+                               if (status & 0x1) {
+                                       if (info->flags & IS_POW2PS)
+                                               return info;
+                               } else {
+                                       if (!(info->flags & IS_POW2PS))
+                                               return info;
+                               }
+                       } else
+                               return info;
+               }
+       }
+
+       /*
+        * Treat other chips as errors ... we won't know the right page
+        * size (it might be binary) even when we can tell which density
+        * class is involved (legacy chip id scheme).
+        */
+       dev_warn(&spi->dev, "JEDEC id %06x not handled\n", jedec);
+       return ERR_PTR(-ENODEV);
 }
 
 /*
- * Detect and initialize DataFlash device:
+ * Detect and initialize DataFlash device, using JEDEC IDs on newer chips
+ * or else the ID code embedded in the status bits:
  *
  *   Device      Density         ID code          #Pages PageSize  Offset
  *   AT45DB011B  1Mbit   (128K)  xx0011xx (0x0c)    512    264      9
- *   AT45DB021B  2Mbit   (256K)  xx0101xx (0x14)   1025    264      9
+ *   AT45DB021B  2Mbit   (256K)  xx0101xx (0x14)   1024    264      9
  *   AT45DB041B  4Mbit   (512K)  xx0111xx (0x1c)   2048    264      9
  *   AT45DB081B  8Mbit   (1M)    xx1001xx (0x24)   4096    264      9
  *   AT45DB0161B 16Mbit  (2M)    xx1011xx (0x2c)   4096    528     10
@@ -532,12 +861,32 @@ add_dataflash(struct spi_device *spi, char *name,
 static int __devinit dataflash_probe(struct spi_device *spi)
 {
        int status;
-
+       struct flash_info       *info;
+
+       /*
+        * Try to detect dataflash by JEDEC ID.
+        * If it succeeds we know we have either a C or D part.
+        * D will support power of 2 pagesize option.
+        * Both support the security register, though with different
+        * write procedures.
+        */
+       info = jedec_probe(spi);
+       if (IS_ERR(info))
+               return PTR_ERR(info);
+       if (info != NULL)
+               return add_dataflash_otp(spi, info->name, info->nr_pages,
+                               info->pagesize, info->pageoffset,
+                               (info->flags & SUP_POW2PS) ? 'd' : 'c');
+
+       /*
+        * Older chips support only legacy commands, identifing
+        * capacity using bits in the status byte.
+        */
        status = dataflash_status(spi);
        if (status <= 0 || status == 0xff) {
                DEBUG(MTD_DEBUG_LEVEL1, "%s: status error %d\n",
-                               spi->dev.bus_id, status);
-               if (status == 0xff)
+                               dev_name(&spi->dev), status);
+               if (status == 0 || status == 0xff)
                        status = -ENODEV;
                return status;
        }
@@ -551,7 +900,7 @@ static int __devinit dataflash_probe(struct spi_device *spi)
                status = add_dataflash(spi, "AT45DB011B", 512, 264, 9);
                break;
        case 0x14:      /* 0 1 0 1 x x */
-               status = add_dataflash(spi, "AT45DB021B", 1025, 264, 9);
+               status = add_dataflash(spi, "AT45DB021B", 1024, 264, 9);
                break;
        case 0x1c:      /* 0 1 1 1 x x */
                status = add_dataflash(spi, "AT45DB041x", 2048, 264, 9);
@@ -572,13 +921,13 @@ static int __devinit dataflash_probe(struct spi_device *spi)
        /* obsolete AT45DB1282 not (yet?) supported */
        default:
                DEBUG(MTD_DEBUG_LEVEL1, "%s: unsupported device (%x)\n",
-                               spi->dev.bus_id, status & 0x3c);
+                               dev_name(&spi->dev), status & 0x3c);
                status = -ENODEV;
        }
 
        if (status < 0)
                DEBUG(MTD_DEBUG_LEVEL1, "%s: add_dataflash --> %d\n",
-                               spi->dev.bus_id, status);
+                               dev_name(&spi->dev), status);
 
        return status;
 }
@@ -588,14 +937,16 @@ static int __devexit dataflash_remove(struct spi_device *spi)
        struct dataflash        *flash = dev_get_drvdata(&spi->dev);
        int                     status;
 
-       DEBUG(MTD_DEBUG_LEVEL1, "%s: remove\n", spi->dev.bus_id);
+       DEBUG(MTD_DEBUG_LEVEL1, "%s: remove\n", dev_name(&spi->dev));
 
        if (mtd_has_partitions() && flash->partitioned)
                status = del_mtd_partitions(&flash->mtd);
        else
                status = del_mtd_device(&flash->mtd);
-       if (status == 0)
+       if (status == 0) {
+               dev_set_drvdata(&spi->dev, NULL);
                kfree(flash);
+       }
        return status;
 }
 
@@ -628,3 +979,4 @@ module_exit(dataflash_exit);
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Andrew Victor, David Brownell");
 MODULE_DESCRIPTION("MTD DataFlash driver");
+MODULE_ALIAS("spi:mtd_dataflash");