linux/drivers/mtd/devices/sst25l.c
<<
>>
Prefs
   1/*
   2 * sst25l.c
   3 *
   4 * Driver for SST25L SPI Flash chips
   5 *
   6 * Copyright © 2009 Bluewater Systems Ltd
   7 * Author: Andre Renaud <andre@bluewatersys.com>
   8 * Author: Ryan Mallon <ryan@bluewatersys.com>
   9 *
  10 * Based on m25p80.c
  11 *
  12 * This code is free software; you can redistribute it and/or modify
  13 * it under the terms of the GNU General Public License version 2 as
  14 * published by the Free Software Foundation.
  15 *
  16 */
  17
  18#include <linux/init.h>
  19#include <linux/module.h>
  20#include <linux/device.h>
  21#include <linux/mutex.h>
  22#include <linux/interrupt.h>
  23#include <linux/sched.h>
  24
  25#include <linux/mtd/mtd.h>
  26#include <linux/mtd/partitions.h>
  27
  28#include <linux/spi/spi.h>
  29#include <linux/spi/flash.h>
  30
  31/* Erases can take up to 3 seconds! */
  32#define MAX_READY_WAIT_JIFFIES  msecs_to_jiffies(3000)
  33
  34#define SST25L_CMD_WRSR         0x01    /* Write status register */
  35#define SST25L_CMD_WRDI         0x04    /* Write disable */
  36#define SST25L_CMD_RDSR         0x05    /* Read status register */
  37#define SST25L_CMD_WREN         0x06    /* Write enable */
  38#define SST25L_CMD_READ         0x03    /* High speed read */
  39
  40#define SST25L_CMD_EWSR         0x50    /* Enable write status register */
  41#define SST25L_CMD_SECTOR_ERASE 0x20    /* Erase sector */
  42#define SST25L_CMD_READ_ID      0x90    /* Read device ID */
  43#define SST25L_CMD_AAI_PROGRAM  0xaf    /* Auto address increment */
  44
  45#define SST25L_STATUS_BUSY      (1 << 0)        /* Chip is busy */
  46#define SST25L_STATUS_WREN      (1 << 1)        /* Write enabled */
  47#define SST25L_STATUS_BP0       (1 << 2)        /* Block protection 0 */
  48#define SST25L_STATUS_BP1       (1 << 3)        /* Block protection 1 */
  49
  50struct sst25l_flash {
  51        struct spi_device       *spi;
  52        struct mutex            lock;
  53        struct mtd_info         mtd;
  54
  55        int                     partitioned;
  56};
  57
  58struct flash_info {
  59        const char              *name;
  60        uint16_t                device_id;
  61        unsigned                page_size;
  62        unsigned                nr_pages;
  63        unsigned                erase_size;
  64};
  65
  66#define to_sst25l_flash(x) container_of(x, struct sst25l_flash, mtd)
  67
  68static struct flash_info __initdata sst25l_flash_info[] = {
  69        {"sst25lf020a", 0xbf43, 256, 1024, 4096},
  70        {"sst25lf040a", 0xbf44, 256, 2048, 4096},
  71};
  72
  73static int sst25l_status(struct sst25l_flash *flash, int *status)
  74{
  75        unsigned char command, response;
  76        int err;
  77
  78        command = SST25L_CMD_RDSR;
  79        err = spi_write_then_read(flash->spi, &command, 1, &response, 1);
  80        if (err < 0)
  81                return err;
  82
  83        *status = response;
  84        return 0;
  85}
  86
  87static int sst25l_write_enable(struct sst25l_flash *flash, int enable)
  88{
  89        unsigned char command[2];
  90        int status, err;
  91
  92        command[0] = enable ? SST25L_CMD_WREN : SST25L_CMD_WRDI;
  93        err = spi_write(flash->spi, command, 1);
  94        if (err)
  95                return err;
  96
  97        command[0] = SST25L_CMD_EWSR;
  98        err = spi_write(flash->spi, command, 1);
  99        if (err)
 100                return err;
 101
 102        command[0] = SST25L_CMD_WRSR;
 103        command[1] = enable ? 0 : SST25L_STATUS_BP0 | SST25L_STATUS_BP1;
 104        err = spi_write(flash->spi, command, 2);
 105        if (err)
 106                return err;
 107
 108        if (enable) {
 109                err = sst25l_status(flash, &status);
 110                if (err)
 111                        return err;
 112                if (!(status & SST25L_STATUS_WREN))
 113                        return -EROFS;
 114        }
 115
 116        return 0;
 117}
 118
 119static int sst25l_wait_till_ready(struct sst25l_flash *flash)
 120{
 121        unsigned long deadline;
 122        int status, err;
 123
 124        deadline = jiffies + MAX_READY_WAIT_JIFFIES;
 125        do {
 126                err = sst25l_status(flash, &status);
 127                if (err)
 128                        return err;
 129                if (!(status & SST25L_STATUS_BUSY))
 130                        return 0;
 131
 132                cond_resched();
 133        } while (!time_after_eq(jiffies, deadline));
 134
 135        return -ETIMEDOUT;
 136}
 137
 138static int sst25l_erase_sector(struct sst25l_flash *flash, uint32_t offset)
 139{
 140        unsigned char command[4];
 141        int err;
 142
 143        err = sst25l_write_enable(flash, 1);
 144        if (err)
 145                return err;
 146
 147        command[0] = SST25L_CMD_SECTOR_ERASE;
 148        command[1] = offset >> 16;
 149        command[2] = offset >> 8;
 150        command[3] = offset;
 151        err = spi_write(flash->spi, command, 4);
 152        if (err)
 153                return err;
 154
 155        err = sst25l_wait_till_ready(flash);
 156        if (err)
 157                return err;
 158
 159        return sst25l_write_enable(flash, 0);
 160}
 161
 162static int sst25l_erase(struct mtd_info *mtd, struct erase_info *instr)
 163{
 164        struct sst25l_flash *flash = to_sst25l_flash(mtd);
 165        uint32_t addr, end;
 166        int err;
 167
 168        /* Sanity checks */
 169        if (instr->addr + instr->len > flash->mtd.size)
 170                return -EINVAL;
 171
 172        if ((uint32_t)instr->len % mtd->erasesize)
 173                return -EINVAL;
 174
 175        if ((uint32_t)instr->addr % mtd->erasesize)
 176                return -EINVAL;
 177
 178        addr = instr->addr;
 179        end = addr + instr->len;
 180
 181        mutex_lock(&flash->lock);
 182
 183        err = sst25l_wait_till_ready(flash);
 184        if (err) {
 185                mutex_unlock(&flash->lock);
 186                return err;
 187        }
 188
 189        while (addr < end) {
 190                err = sst25l_erase_sector(flash, addr);
 191                if (err) {
 192                        mutex_unlock(&flash->lock);
 193                        instr->state = MTD_ERASE_FAILED;
 194                        dev_err(&flash->spi->dev, "Erase failed\n");
 195                        return err;
 196                }
 197
 198                addr += mtd->erasesize;
 199        }
 200
 201        mutex_unlock(&flash->lock);
 202
 203        instr->state = MTD_ERASE_DONE;
 204        mtd_erase_callback(instr);
 205        return 0;
 206}
 207
 208static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len,
 209                       size_t *retlen, unsigned char *buf)
 210{
 211        struct sst25l_flash *flash = to_sst25l_flash(mtd);
 212        struct spi_transfer transfer[2];
 213        struct spi_message message;
 214        unsigned char command[4];
 215        int ret;
 216
 217        /* Sanity checking */
 218        if (len == 0)
 219                return 0;
 220
 221        if (from + len > flash->mtd.size)
 222                return -EINVAL;
 223
 224        if (retlen)
 225                *retlen = 0;
 226
 227        spi_message_init(&message);
 228        memset(&transfer, 0, sizeof(transfer));
 229
 230        command[0] = SST25L_CMD_READ;
 231        command[1] = from >> 16;
 232        command[2] = from >> 8;
 233        command[3] = from;
 234
 235        transfer[0].tx_buf = command;
 236        transfer[0].len = sizeof(command);
 237        spi_message_add_tail(&transfer[0], &message);
 238
 239        transfer[1].rx_buf = buf;
 240        transfer[1].len = len;
 241        spi_message_add_tail(&transfer[1], &message);
 242
 243        mutex_lock(&flash->lock);
 244
 245        /* Wait for previous write/erase to complete */
 246        ret = sst25l_wait_till_ready(flash);
 247        if (ret) {
 248                mutex_unlock(&flash->lock);
 249                return ret;
 250        }
 251
 252        spi_sync(flash->spi, &message);
 253
 254        if (retlen && message.actual_length > sizeof(command))
 255                *retlen += message.actual_length - sizeof(command);
 256
 257        mutex_unlock(&flash->lock);
 258        return 0;
 259}
 260
 261static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len,
 262                        size_t *retlen, const unsigned char *buf)
 263{
 264        struct sst25l_flash *flash = to_sst25l_flash(mtd);
 265        int i, j, ret, bytes, copied = 0;
 266        unsigned char command[5];
 267
 268        /* Sanity checks */
 269        if (!len)
 270                return 0;
 271
 272        if (to + len > flash->mtd.size)
 273                return -EINVAL;
 274
 275        if ((uint32_t)to % mtd->writesize)
 276                return -EINVAL;
 277
 278        mutex_lock(&flash->lock);
 279
 280        ret = sst25l_write_enable(flash, 1);
 281        if (ret)
 282                goto out;
 283
 284        for (i = 0; i < len; i += mtd->writesize) {
 285                ret = sst25l_wait_till_ready(flash);
 286                if (ret)
 287                        goto out;
 288
 289                /* Write the first byte of the page */
 290                command[0] = SST25L_CMD_AAI_PROGRAM;
 291                command[1] = (to + i) >> 16;
 292                command[2] = (to + i) >> 8;
 293                command[3] = (to + i);
 294                command[4] = buf[i];
 295                ret = spi_write(flash->spi, command, 5);
 296                if (ret < 0)
 297                        goto out;
 298                copied++;
 299
 300                /*
 301                 * Write the remaining bytes using auto address
 302                 * increment mode
 303                 */
 304                bytes = min_t(uint32_t, mtd->writesize, len - i);
 305                for (j = 1; j < bytes; j++, copied++) {
 306                        ret = sst25l_wait_till_ready(flash);
 307                        if (ret)
 308                                goto out;
 309
 310                        command[1] = buf[i + j];
 311                        ret = spi_write(flash->spi, command, 2);
 312                        if (ret)
 313                                goto out;
 314                }
 315        }
 316
 317out:
 318        ret = sst25l_write_enable(flash, 0);
 319
 320        if (retlen)
 321                *retlen = copied;
 322
 323        mutex_unlock(&flash->lock);
 324        return ret;
 325}
 326
 327static struct flash_info *__init sst25l_match_device(struct spi_device *spi)
 328{
 329        struct flash_info *flash_info = NULL;
 330        unsigned char command[4], response;
 331        int i, err;
 332        uint16_t id;
 333
 334        command[0] = SST25L_CMD_READ_ID;
 335        command[1] = 0;
 336        command[2] = 0;
 337        command[3] = 0;
 338        err = spi_write_then_read(spi, command, sizeof(command), &response, 1);
 339        if (err < 0) {
 340                dev_err(&spi->dev, "error reading device id msb\n");
 341                return NULL;
 342        }
 343
 344        id = response << 8;
 345
 346        command[0] = SST25L_CMD_READ_ID;
 347        command[1] = 0;
 348        command[2] = 0;
 349        command[3] = 1;
 350        err = spi_write_then_read(spi, command, sizeof(command), &response, 1);
 351        if (err < 0) {
 352                dev_err(&spi->dev, "error reading device id lsb\n");
 353                return NULL;
 354        }
 355
 356        id |= response;
 357
 358        for (i = 0; i < ARRAY_SIZE(sst25l_flash_info); i++)
 359                if (sst25l_flash_info[i].device_id == id)
 360                        flash_info = &sst25l_flash_info[i];
 361
 362        if (!flash_info)
 363                dev_err(&spi->dev, "unknown id %.4x\n", id);
 364
 365        return flash_info;
 366}
 367
 368static int __init sst25l_probe(struct spi_device *spi)
 369{
 370        struct flash_info *flash_info;
 371        struct sst25l_flash *flash;
 372        struct flash_platform_data *data;
 373        int ret, i;
 374
 375        flash_info = sst25l_match_device(spi);
 376        if (!flash_info)
 377                return -ENODEV;
 378
 379        flash = kzalloc(sizeof(struct sst25l_flash), GFP_KERNEL);
 380        if (!flash)
 381                return -ENOMEM;
 382
 383        flash->spi = spi;
 384        mutex_init(&flash->lock);
 385        dev_set_drvdata(&spi->dev, flash);
 386
 387        data = spi->dev.platform_data;
 388        if (data && data->name)
 389                flash->mtd.name = data->name;
 390        else
 391                flash->mtd.name = dev_name(&spi->dev);
 392
 393        flash->mtd.type         = MTD_NORFLASH;
 394        flash->mtd.flags        = MTD_CAP_NORFLASH;
 395        flash->mtd.erasesize    = flash_info->erase_size;
 396        flash->mtd.writesize    = flash_info->page_size;
 397        flash->mtd.size         = flash_info->page_size * flash_info->nr_pages;
 398        flash->mtd.erase        = sst25l_erase;
 399        flash->mtd.read         = sst25l_read;
 400        flash->mtd.write        = sst25l_write;
 401
 402        dev_info(&spi->dev, "%s (%lld KiB)\n", flash_info->name,
 403                 (long long)flash->mtd.size >> 10);
 404
 405        DEBUG(MTD_DEBUG_LEVEL2,
 406              "mtd .name = %s, .size = 0x%llx (%lldMiB) "
 407              ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
 408              flash->mtd.name,
 409              (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
 410              flash->mtd.erasesize, flash->mtd.erasesize / 1024,
 411              flash->mtd.numeraseregions);
 412
 413        if (flash->mtd.numeraseregions)
 414                for (i = 0; i < flash->mtd.numeraseregions; i++)
 415                        DEBUG(MTD_DEBUG_LEVEL2,
 416                              "mtd.eraseregions[%d] = { .offset = 0x%llx, "
 417                              ".erasesize = 0x%.8x (%uKiB), "
 418                              ".numblocks = %d }\n",
 419                              i, (long long)flash->mtd.eraseregions[i].offset,
 420                              flash->mtd.eraseregions[i].erasesize,
 421                              flash->mtd.eraseregions[i].erasesize / 1024,
 422                              flash->mtd.eraseregions[i].numblocks);
 423
 424        if (mtd_has_partitions()) {
 425                struct mtd_partition *parts = NULL;
 426                int nr_parts = 0;
 427
 428                if (mtd_has_cmdlinepart()) {
 429                        static const char *part_probes[] =
 430                                {"cmdlinepart", NULL};
 431
 432                        nr_parts = parse_mtd_partitions(&flash->mtd,
 433                                                        part_probes,
 434                                                        &parts, 0);
 435                }
 436
 437                if (nr_parts <= 0 && data && data->parts) {
 438                        parts = data->parts;
 439                        nr_parts = data->nr_parts;
 440                }
 441
 442                if (nr_parts > 0) {
 443                        for (i = 0; i < nr_parts; i++) {
 444                                DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
 445                                      "{.name = %s, .offset = 0x%llx, "
 446                                      ".size = 0x%llx (%lldKiB) }\n",
 447                                      i, parts[i].name,
 448                                      (long long)parts[i].offset,
 449                                      (long long)parts[i].size,
 450                                      (long long)(parts[i].size >> 10));
 451                        }
 452
 453                        flash->partitioned = 1;
 454                        return add_mtd_partitions(&flash->mtd,
 455                                                  parts, nr_parts);
 456                }
 457
 458        } else if (data->nr_parts) {
 459                dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
 460                         data->nr_parts, data->name);
 461        }
 462
 463        ret = add_mtd_device(&flash->mtd);
 464        if (ret == 1) {
 465                kfree(flash);
 466                dev_set_drvdata(&spi->dev, NULL);
 467                return -ENODEV;
 468        }
 469
 470        return 0;
 471}
 472
 473static int __exit sst25l_remove(struct spi_device *spi)
 474{
 475        struct sst25l_flash *flash = dev_get_drvdata(&spi->dev);
 476        int ret;
 477
 478        if (mtd_has_partitions() && flash->partitioned)
 479                ret = del_mtd_partitions(&flash->mtd);
 480        else
 481                ret = del_mtd_device(&flash->mtd);
 482        if (ret == 0)
 483                kfree(flash);
 484        return ret;
 485}
 486
 487static struct spi_driver sst25l_driver = {
 488        .driver = {
 489                .name   = "sst25l",
 490                .bus    = &spi_bus_type,
 491                .owner  = THIS_MODULE,
 492        },
 493        .probe          = sst25l_probe,
 494        .remove         = __exit_p(sst25l_remove),
 495};
 496
 497static int __init sst25l_init(void)
 498{
 499        return spi_register_driver(&sst25l_driver);
 500}
 501
 502static void __exit sst25l_exit(void)
 503{
 504        spi_unregister_driver(&sst25l_driver);
 505}
 506
 507module_init(sst25l_init);
 508module_exit(sst25l_exit);
 509
 510MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips");
 511MODULE_AUTHOR("Andre Renaud <andre@bluewatersys.com>, "
 512              "Ryan Mallon <ryan@bluewatersys.com>");
 513MODULE_LICENSE("GPL");
 514