2  * Atmel DataFlash driver for Atmel AT91RM9200 (Thunder)
 
   3  * This is a largely modified version of at91_dataflash.c that
 
   4  * supports AT26xxx dataflash chips. The original driver supports
 
   7  * Note: This driver was only tested with an AT26F004. It should be
 
   8  * easy to make it work with other AT26xxx dataflash devices, though.
 
  10  * Copyright (C) 2007 Hans J. Koch <hjk@linutronix.de>
 
  11  * original Copyright (C) SAN People (Pty) Ltd
 
  13  * This program is free software; you can redistribute it and/or
 
  14  * modify it under the terms of the GNU General Public License
 
  15  * version 2 as published by the Free Software Foundation.
 
  18 #include <linux/config.h>
 
  19 #include <linux/module.h>
 
  20 #include <linux/init.h>
 
  21 #include <linux/mtd/mtd.h>
 
  23 #include <asm/arch/at91_spi.h>
 
  25 #define DATAFLASH_MAX_DEVICES   4       /* max number of dataflash devices */
 
  27 #define MANUFACTURER_ID_ATMEL           0x1F
 
  31 #define AT26_OP_READ_STATUS             0x05
 
  32 #define AT26_OP_READ_DEV_ID             0x9F
 
  33 #define AT26_OP_ERASE_PAGE_4K           0x20
 
  34 #define AT26_OP_READ_ARRAY_FAST         0x0B
 
  35 #define AT26_OP_SEQUENTIAL_WRITE        0xAF
 
  36 #define AT26_OP_WRITE_ENABLE            0x06
 
  37 #define AT26_OP_WRITE_DISABLE           0x04
 
  38 #define AT26_OP_SECTOR_PROTECT          0x36
 
  39 #define AT26_OP_SECTOR_UNPROTECT        0x39
 
  41 /* status register bits */
 
  43 #define AT26_STATUS_BUSY                0x01
 
  44 #define AT26_STATUS_WRITE_ENABLE        0x02
 
  46 struct dataflash_local
 
  48         int spi;                        /* SPI chip-select number */
 
  49         unsigned int page_size;         /* number of bytes per page */
 
  53 /* Detected DataFlash devices */
 
  54 static struct mtd_info* mtd_devices[DATAFLASH_MAX_DEVICES];
 
  55 static int nr_devices = 0;
 
  57 /* Allocate a single SPI transfer descriptor.  We're assuming that if multiple
 
  58    SPI transfers occur at the same time, spi_access_bus() will serialize them.
 
  59    If this is not valid, then either (i) each dataflash 'priv' structure
 
  60    needs it's own transfer descriptor, (ii) we lock this one, or (iii) use
 
  62 static struct spi_transfer_list* spi_transfer_desc;
 
  65  * Perform a SPI transfer to access the DataFlash device.
 
  67 static int do_spi_transfer(int nr, char* tx, int tx_len, char* rx, int rx_len,
 
  68                 char* txnext, int txnext_len, char* rxnext, int rxnext_len)
 
  70         struct spi_transfer_list* list = spi_transfer_desc;
 
  72         list->tx[0] = tx;       list->txlen[0] = tx_len;
 
  73         list->rx[0] = rx;       list->rxlen[0] = rx_len;
 
  75         list->tx[1] = txnext;   list->txlen[1] = txnext_len;
 
  76         list->rx[1] = rxnext;   list->rxlen[1] = rxnext_len;
 
  78         list->nr_transfers = nr;
 
  79         /* Note: spi_transfer() always returns 0, there are no error checks */
 
  80         return spi_transfer(list);
 
  84  * Return the status of the DataFlash device.
 
  86 static unsigned char at91_dataflash26_status(void)
 
  88         unsigned char command[2];
 
  90         command[0] = AT26_OP_READ_STATUS;
 
  93         do_spi_transfer(1, command, 2, command, 2, NULL, 0, NULL, 0);
 
  99  * Poll the DataFlash device until it is READY.
 
 101 static unsigned char at91_dataflash26_waitready(void)
 
 103         unsigned char status;
 
 106                 status = at91_dataflash26_status();
 
 107                 if (!(status & AT26_STATUS_BUSY))
 
 113  * Enable/disable write access
 
 115  static void at91_dataflash26_write_enable(int enable)
 
 117         unsigned char cmd[2];
 
 119         DEBUG(MTD_DEBUG_LEVEL3, "write_enable: enable=%i\n", enable);
 
 122                 cmd[0] = AT26_OP_WRITE_ENABLE;
 
 124                 cmd[0] = AT26_OP_WRITE_DISABLE;
 
 127         do_spi_transfer(1, cmd, 2, cmd, 2, NULL, 0, NULL, 0);
 
 131  * Protect/unprotect sector
 
 133  static void at91_dataflash26_sector_protect(loff_t addr, int protect)
 
 135         unsigned char cmd[4];
 
 137         DEBUG(MTD_DEBUG_LEVEL3, "sector_protect: addr=0x%06x prot=%d\n",
 
 141                 cmd[0] = AT26_OP_SECTOR_PROTECT;
 
 143                 cmd[0] = AT26_OP_SECTOR_UNPROTECT;
 
 144         cmd[1] = (addr & 0x00FF0000) >> 16;
 
 145         cmd[2] = (addr & 0x0000FF00) >> 8;
 
 146         cmd[3] = (addr & 0x000000FF);
 
 148         do_spi_transfer(1, cmd, 4, cmd, 4, NULL, 0, NULL, 0);
 
 152  * Erase blocks of flash.
 
 154 static int at91_dataflash26_erase(struct mtd_info *mtd,
 
 155                                   struct erase_info *instr)
 
 157         struct dataflash_local *priv = (struct dataflash_local *) mtd->priv;
 
 158         unsigned char cmd[4];
 
 160         DEBUG(MTD_DEBUG_LEVEL1, "dataflash_erase: addr=0x%06x len=%i\n",
 
 161                instr->addr, instr->len);
 
 164         if (priv->page_size != 4096)
 
 165                 return -EINVAL; /* Can't handle other sizes at the moment */
 
 167         if (   ((instr->len % mtd->erasesize) != 0)
 
 168             || ((instr->len % priv->page_size) != 0)
 
 169             || ((instr->addr % priv->page_size) != 0)
 
 170             || ((instr->addr + instr->len) > mtd->size))
 
 173         spi_access_bus(priv->spi);
 
 175         while (instr->len > 0) {
 
 176                 at91_dataflash26_write_enable(1);
 
 177                 at91_dataflash26_sector_protect(instr->addr, 0);
 
 178                 at91_dataflash26_write_enable(1);
 
 179                 cmd[0] = AT26_OP_ERASE_PAGE_4K;
 
 180                 cmd[1] = (instr->addr & 0x00FF0000) >> 16;
 
 181                 cmd[2] = (instr->addr & 0x0000FF00) >> 8;
 
 182                 cmd[3] = (instr->addr & 0x000000FF);
 
 184                 DEBUG(MTD_DEBUG_LEVEL3, "ERASE: (0x%02x) 0x%02x 0x%02x"
 
 186                         cmd[0], cmd[1], cmd[2], cmd[3]);
 
 188                 do_spi_transfer(1, cmd, 4, cmd, 4, NULL, 0, NULL, 0);
 
 189                 at91_dataflash26_waitready();
 
 191                 instr->addr += priv->page_size;  /* next page */
 
 192                 instr->len -= priv->page_size;
 
 195         at91_dataflash26_write_enable(0);
 
 196         spi_release_bus(priv->spi);
 
 198         /* Inform MTD subsystem that erase is complete */
 
 199         instr->state = MTD_ERASE_DONE;
 
 201                 instr->callback(instr);
 
 207  * Read from the DataFlash device.
 
 208  *   from   : Start offset in flash device
 
 209  *   len    : Number of bytes to read
 
 210  *   retlen : Number of bytes actually read
 
 211  *   buf    : Buffer that will receive data
 
 213 static int at91_dataflash26_read(struct mtd_info *mtd, loff_t from, size_t len,
 
 214                                  size_t *retlen, u_char *buf)
 
 216         struct dataflash_local *priv = (struct dataflash_local *) mtd->priv;
 
 217         unsigned char cmd[5];
 
 219         DEBUG(MTD_DEBUG_LEVEL1, "dataflash_read: %lli .. %lli\n",
 
 227         if (from + len > mtd->size)
 
 230         cmd[0] = AT26_OP_READ_ARRAY_FAST;
 
 231         cmd[1] = (from & 0x00FF0000) >> 16;
 
 232         cmd[2] = (from & 0x0000FF00) >> 8;
 
 233         cmd[3] = (from & 0x000000FF);
 
 234         /* cmd[4] is a "Don't care" byte  */
 
 236         DEBUG(MTD_DEBUG_LEVEL3, "READ: (0x%02x) 0x%02x 0x%02x 0x%02x\n",
 
 237                cmd[0], cmd[1], cmd[2], cmd[3]);
 
 239         spi_access_bus(priv->spi);
 
 240         do_spi_transfer(2, cmd, 5, cmd, 5, buf, len, buf, len);
 
 241         spi_release_bus(priv->spi);
 
 248  * Write to the DataFlash device.
 
 249  *   to     : Start offset in flash device
 
 250  *   len    : Number of bytes to write
 
 251  *   retlen : Number of bytes actually written
 
 252  *   buf    : Buffer containing the data
 
 254 static int at91_dataflash26_write(struct mtd_info *mtd, loff_t to, size_t len,
 
 255                                   size_t *retlen, const u_char *buf)
 
 257         struct dataflash_local *priv = (struct dataflash_local *) mtd->priv;
 
 258         unsigned int addr, buf_index = 0;
 
 259         int ret = -EIO, sector, last_sector;
 
 260         unsigned char status, cmd[5];
 
 262         DEBUG(MTD_DEBUG_LEVEL1, "dataflash_write: %lli .. %lli\n", to, to+len);
 
 269         if (to + len > mtd->size)
 
 272         spi_access_bus(priv->spi);
 
 277         while (buf_index < len) {
 
 278                 sector = addr / priv->page_size;
 
 279                 /* Write first byte if a new sector begins */
 
 280                 if (sector != last_sector) {
 
 281                         at91_dataflash26_write_enable(1);
 
 282                         at91_dataflash26_sector_protect(addr, 0);
 
 283                         at91_dataflash26_write_enable(1);
 
 285                         /* Program first byte of a new sector */
 
 286                         cmd[0] = AT26_OP_SEQUENTIAL_WRITE;
 
 287                         cmd[1] = (addr & 0x00FF0000) >> 16;
 
 288                         cmd[2] = (addr & 0x0000FF00) >> 8;
 
 289                         cmd[3] = (addr & 0x000000FF);
 
 290                         cmd[4] = buf[buf_index++];
 
 291                         do_spi_transfer(1, cmd, 5, cmd, 5, NULL, 0, NULL, 0);
 
 292                         status = at91_dataflash26_waitready();
 
 294                         /* On write errors, the chip resets the write enable
 
 295                            flag. This also happens after the last byte of a
 
 296                            sector is successfully programmed. */
 
 297                         if (   ( !(status & AT26_STATUS_WRITE_ENABLE))
 
 298                             && ((addr % priv->page_size) != 0) ) {
 
 299                                 DEBUG(MTD_DEBUG_LEVEL1,
 
 300                                         "write error1: addr=0x%06x, "
 
 301                                         "status=0x%02x\n", addr, status);
 
 305                         last_sector = sector;
 
 308                 /* Write subsequent bytes in the same sector */
 
 309                 cmd[0] = AT26_OP_SEQUENTIAL_WRITE;
 
 310                 cmd[1] = buf[buf_index++];
 
 311                 do_spi_transfer(1, cmd, 2, cmd, 2, NULL, 0, NULL, 0);
 
 312                 status = at91_dataflash26_waitready();
 
 315                 if (   ( !(status & AT26_STATUS_WRITE_ENABLE))
 
 316                     && ((addr % priv->page_size) != 0) ) {
 
 317                         DEBUG(MTD_DEBUG_LEVEL1, "write error2: addr=0x%06x, "
 
 318                                 "status=0x%02x\n", addr, status);
 
 326         at91_dataflash26_write_enable(0);
 
 328         spi_release_bus(priv->spi);
 
 333  * Initialize and register DataFlash device with MTD subsystem.
 
 335 static int __init add_dataflash(int channel, char *name, int nr_pages,
 
 338         struct mtd_info *device;
 
 339         struct dataflash_local *priv;
 
 341         if (nr_devices >= DATAFLASH_MAX_DEVICES) {
 
 342                 printk(KERN_ERR "at91_dataflash26: Too many devices "
 
 347         device = kzalloc(sizeof(struct mtd_info) + strlen(name) + 8,
 
 352         device->name = (char *)&device[1];
 
 353         sprintf(device->name, "%s.spi%d", name, channel);
 
 354         device->size = nr_pages * pagesize;
 
 355         device->erasesize = pagesize;
 
 356         device->owner = THIS_MODULE;
 
 357         device->type = MTD_DATAFLASH;
 
 358         device->flags = MTD_CAP_NORFLASH;
 
 359         device->erase = at91_dataflash26_erase;
 
 360         device->read = at91_dataflash26_read;
 
 361         device->write = at91_dataflash26_write;
 
 363         priv = (struct dataflash_local *)kzalloc(sizeof(struct dataflash_local),
 
 371         priv->page_size = pagesize;
 
 374         mtd_devices[nr_devices] = device;
 
 376         printk(KERN_INFO "at91_dataflash26: %s detected [spi%i] (%i bytes)\n",
 
 377                name, channel, device->size);
 
 379         return add_mtd_device(device);
 
 383  * Detect and initialize DataFlash device connected to specified SPI channel.
 
 387 struct dataflash26_types {
 
 395 struct dataflash26_types df26_types[] = {
 
 406                 .name = "AT26DF081A", /* Not tested ! */
 
 412 static int __init at91_dataflash26_detect(int channel)
 
 414         unsigned char status, cmd[5];
 
 417         spi_access_bus(channel);
 
 418         status = at91_dataflash26_status();
 
 420         if (status == 0 || status == 0xff) {
 
 421                 printk(KERN_ERR "at91_dataflash26_detect: status error %d\n",
 
 423                 spi_release_bus(channel);
 
 427         cmd[0] = AT26_OP_READ_DEV_ID;
 
 428         do_spi_transfer(1, cmd, 5, cmd, 5, NULL, 0, NULL, 0);
 
 429         spi_release_bus(channel);
 
 431         if (cmd[1] != MANUFACTURER_ID_ATMEL)
 
 434         for (i = 0; i < ARRAY_SIZE(df26_types); i++) {
 
 435                 if (   cmd[2] == df26_types[i].id0
 
 436                     && cmd[3] == df26_types[i].id1)
 
 437                         return add_dataflash(channel,
 
 439                                                 df26_types[i].nr_pages,
 
 440                                                 df26_types[i].pagesize);
 
 443         printk(KERN_ERR "at91_dataflash26_detect: Unsupported device "
 
 444                         "(0x%02x/0x%02x)\n", cmd[2], cmd[3]);
 
 448 static int __init at91_dataflash26_init(void)
 
 450         spi_transfer_desc = kmalloc(sizeof(struct spi_transfer_list),
 
 452         if (!spi_transfer_desc)
 
 455         /* DataFlash (SPI chip select 0) */
 
 456         at91_dataflash26_detect(0);
 
 458 #ifdef CONFIG_MTD_AT91_DATAFLASH_CARD
 
 459         /* DataFlash card (SPI chip select 3) */
 
 460         at91_dataflash26_detect(3);
 
 465 static void __exit at91_dataflash26_exit(void)
 
 469         for (i = 0; i < DATAFLASH_MAX_DEVICES; i++) {
 
 470                 if (mtd_devices[i]) {
 
 471                         del_mtd_device(mtd_devices[i]);
 
 472                         kfree(mtd_devices[i]->priv);
 
 473                         kfree(mtd_devices[i]);
 
 477         kfree(spi_transfer_desc);
 
 480 module_init(at91_dataflash26_init);
 
 481 module_exit(at91_dataflash26_exit);
 
 483 MODULE_LICENSE("GPL");
 
 484 MODULE_AUTHOR("Hans J. Koch");
 
 485 MODULE_DESCRIPTION("DataFlash AT26xxx driver for Atmel AT91RM9200");