Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 1 | /* |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 2 | * MTD SPI driver for ST M25Pxx (and similar) serial flash chips |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 3 | * |
| 4 | * Author: Mike Lavender, mike@steroidmicros.com |
| 5 | * |
| 6 | * Copyright (c) 2005, Intec Automation Inc. |
| 7 | * |
| 8 | * Some parts are based on lart.c by Abraham Van Der Merwe |
| 9 | * |
| 10 | * Cleaned up and generalized based on mtd_dataflash.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/interrupt.h> |
David Brownell | 7d5230e | 2007-06-24 15:09:13 -0700 | [diff] [blame] | 22 | #include <linux/mutex.h> |
Artem Bityutskiy | d85316a | 2008-12-18 14:10:05 +0200 | [diff] [blame] | 23 | #include <linux/math64.h> |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 24 | #include <linux/mod_devicetable.h> |
David Brownell | 7d5230e | 2007-06-24 15:09:13 -0700 | [diff] [blame] | 25 | |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 26 | #include <linux/mtd/mtd.h> |
| 27 | #include <linux/mtd/partitions.h> |
David Brownell | 7d5230e | 2007-06-24 15:09:13 -0700 | [diff] [blame] | 28 | |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 29 | #include <linux/spi/spi.h> |
| 30 | #include <linux/spi/flash.h> |
| 31 | |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 32 | |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 33 | #define FLASH_PAGESIZE 256 |
| 34 | |
| 35 | /* Flash opcodes. */ |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 36 | #define OPCODE_WREN 0x06 /* Write enable */ |
| 37 | #define OPCODE_RDSR 0x05 /* Read status register */ |
Michael Hennerich | 7228982 | 2008-07-03 23:54:42 -0700 | [diff] [blame] | 38 | #define OPCODE_WRSR 0x01 /* Write status register 1 byte */ |
Bryan Wu | 2230b76 | 2008-04-25 12:07:32 +0800 | [diff] [blame] | 39 | #define OPCODE_NORM_READ 0x03 /* Read data bytes (low frequency) */ |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 40 | #define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */ |
| 41 | #define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */ |
Chen Gong | 7854643 | 2008-11-26 10:23:57 +0000 | [diff] [blame] | 42 | #define OPCODE_BE_4K 0x20 /* Erase 4KiB block */ |
David Woodhouse | 02d087d | 2007-06-28 22:38:38 +0100 | [diff] [blame] | 43 | #define OPCODE_BE_32K 0x52 /* Erase 32KiB block */ |
Chen Gong | 7854643 | 2008-11-26 10:23:57 +0000 | [diff] [blame] | 44 | #define OPCODE_CHIP_ERASE 0xc7 /* Erase whole flash chip */ |
David Woodhouse | 02d087d | 2007-06-28 22:38:38 +0100 | [diff] [blame] | 45 | #define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */ |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 46 | #define OPCODE_RDID 0x9f /* Read JEDEC ID */ |
| 47 | |
Graf Yang | 49aac4a | 2009-06-15 08:23:41 +0000 | [diff] [blame] | 48 | /* Used for SST flashes only. */ |
| 49 | #define OPCODE_BP 0x02 /* Byte program */ |
| 50 | #define OPCODE_WRDI 0x04 /* Write disable */ |
| 51 | #define OPCODE_AAI_WP 0xad /* Auto address increment word program */ |
| 52 | |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 53 | /* Status Register bits. */ |
| 54 | #define SR_WIP 1 /* Write in progress */ |
| 55 | #define SR_WEL 2 /* Write enable latch */ |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 56 | /* meaning of other SR_* bits may differ between vendors */ |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 57 | #define SR_BP0 4 /* Block protect 0 */ |
| 58 | #define SR_BP1 8 /* Block protect 1 */ |
| 59 | #define SR_BP2 0x10 /* Block protect 2 */ |
| 60 | #define SR_SRWD 0x80 /* SR write protect */ |
| 61 | |
| 62 | /* Define max times to check status register before we give up. */ |
Steven A. Falco | 89bb871 | 2009-06-26 12:42:47 -0400 | [diff] [blame] | 63 | #define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */ |
Bryan Wu | 2230b76 | 2008-04-25 12:07:32 +0800 | [diff] [blame] | 64 | #define CMD_SIZE 4 |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 65 | |
Bryan Wu | 2230b76 | 2008-04-25 12:07:32 +0800 | [diff] [blame] | 66 | #ifdef CONFIG_M25PXX_USE_FAST_READ |
| 67 | #define OPCODE_READ OPCODE_FAST_READ |
| 68 | #define FAST_READ_DUMMY_BYTE 1 |
| 69 | #else |
| 70 | #define OPCODE_READ OPCODE_NORM_READ |
| 71 | #define FAST_READ_DUMMY_BYTE 0 |
| 72 | #endif |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 73 | |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 74 | /****************************************************************************/ |
| 75 | |
| 76 | struct m25p { |
| 77 | struct spi_device *spi; |
David Brownell | 7d5230e | 2007-06-24 15:09:13 -0700 | [diff] [blame] | 78 | struct mutex lock; |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 79 | struct mtd_info mtd; |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 80 | unsigned partitioned:1; |
| 81 | u8 erase_opcode; |
Johannes Stezenbach | 61c3506 | 2009-10-28 14:21:37 +0100 | [diff] [blame] | 82 | u8 *command; |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd) |
| 86 | { |
| 87 | return container_of(mtd, struct m25p, mtd); |
| 88 | } |
| 89 | |
| 90 | /****************************************************************************/ |
| 91 | |
| 92 | /* |
| 93 | * Internal helper functions |
| 94 | */ |
| 95 | |
| 96 | /* |
| 97 | * Read the status register, returning its value in the location |
| 98 | * Return the status register value. |
| 99 | * Returns negative if error occurred. |
| 100 | */ |
| 101 | static int read_sr(struct m25p *flash) |
| 102 | { |
| 103 | ssize_t retval; |
| 104 | u8 code = OPCODE_RDSR; |
| 105 | u8 val; |
| 106 | |
| 107 | retval = spi_write_then_read(flash->spi, &code, 1, &val, 1); |
| 108 | |
| 109 | if (retval < 0) { |
| 110 | dev_err(&flash->spi->dev, "error %d reading SR\n", |
| 111 | (int) retval); |
| 112 | return retval; |
| 113 | } |
| 114 | |
| 115 | return val; |
| 116 | } |
| 117 | |
Michael Hennerich | 7228982 | 2008-07-03 23:54:42 -0700 | [diff] [blame] | 118 | /* |
| 119 | * Write status register 1 byte |
| 120 | * Returns negative if error occurred. |
| 121 | */ |
| 122 | static int write_sr(struct m25p *flash, u8 val) |
| 123 | { |
| 124 | flash->command[0] = OPCODE_WRSR; |
| 125 | flash->command[1] = val; |
| 126 | |
| 127 | return spi_write(flash->spi, flash->command, 2); |
| 128 | } |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * Set write enable latch with Write Enable command. |
| 132 | * Returns negative if error occurred. |
| 133 | */ |
| 134 | static inline int write_enable(struct m25p *flash) |
| 135 | { |
| 136 | u8 code = OPCODE_WREN; |
| 137 | |
David Woodhouse | 8a1a627 | 2008-10-20 09:26:16 +0100 | [diff] [blame] | 138 | return spi_write_then_read(flash->spi, &code, 1, NULL, 0); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Graf Yang | 49aac4a | 2009-06-15 08:23:41 +0000 | [diff] [blame] | 141 | /* |
| 142 | * Send write disble instruction to the chip. |
| 143 | */ |
| 144 | static inline int write_disable(struct m25p *flash) |
| 145 | { |
| 146 | u8 code = OPCODE_WRDI; |
| 147 | |
| 148 | return spi_write_then_read(flash->spi, &code, 1, NULL, 0); |
| 149 | } |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * Service routine to read status register until ready, or timeout occurs. |
| 153 | * Returns non-zero if error. |
| 154 | */ |
| 155 | static int wait_till_ready(struct m25p *flash) |
| 156 | { |
Peter Horton | cd1a6de | 2009-05-08 13:51:53 +0100 | [diff] [blame] | 157 | unsigned long deadline; |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 158 | int sr; |
| 159 | |
Peter Horton | cd1a6de | 2009-05-08 13:51:53 +0100 | [diff] [blame] | 160 | deadline = jiffies + MAX_READY_WAIT_JIFFIES; |
| 161 | |
| 162 | do { |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 163 | if ((sr = read_sr(flash)) < 0) |
| 164 | break; |
| 165 | else if (!(sr & SR_WIP)) |
| 166 | return 0; |
| 167 | |
Peter Horton | cd1a6de | 2009-05-08 13:51:53 +0100 | [diff] [blame] | 168 | cond_resched(); |
| 169 | |
| 170 | } while (!time_after_eq(jiffies, deadline)); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 171 | |
| 172 | return 1; |
| 173 | } |
| 174 | |
Chen Gong | faff375 | 2008-08-11 16:59:13 +0800 | [diff] [blame] | 175 | /* |
| 176 | * Erase the whole flash memory |
| 177 | * |
| 178 | * Returns 0 if successful, non-zero otherwise. |
| 179 | */ |
Chen Gong | 7854643 | 2008-11-26 10:23:57 +0000 | [diff] [blame] | 180 | static int erase_chip(struct m25p *flash) |
Chen Gong | faff375 | 2008-08-11 16:59:13 +0800 | [diff] [blame] | 181 | { |
Artem Bityutskiy | d85316a | 2008-12-18 14:10:05 +0200 | [diff] [blame] | 182 | DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %lldKiB\n", |
Kay Sievers | 160bbab | 2008-12-23 10:00:14 +0000 | [diff] [blame] | 183 | dev_name(&flash->spi->dev), __func__, |
| 184 | (long long)(flash->mtd.size >> 10)); |
Chen Gong | faff375 | 2008-08-11 16:59:13 +0800 | [diff] [blame] | 185 | |
| 186 | /* Wait until finished previous write command. */ |
| 187 | if (wait_till_ready(flash)) |
| 188 | return 1; |
| 189 | |
| 190 | /* Send write enable, then erase commands. */ |
| 191 | write_enable(flash); |
| 192 | |
| 193 | /* Set up command buffer. */ |
Chen Gong | 7854643 | 2008-11-26 10:23:57 +0000 | [diff] [blame] | 194 | flash->command[0] = OPCODE_CHIP_ERASE; |
Chen Gong | faff375 | 2008-08-11 16:59:13 +0800 | [diff] [blame] | 195 | |
| 196 | spi_write(flash->spi, flash->command, 1); |
| 197 | |
| 198 | return 0; |
| 199 | } |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 200 | |
| 201 | /* |
| 202 | * Erase one sector of flash memory at offset ``offset'' which is any |
| 203 | * address within the sector which should be erased. |
| 204 | * |
| 205 | * Returns 0 if successful, non-zero otherwise. |
| 206 | */ |
| 207 | static int erase_sector(struct m25p *flash, u32 offset) |
| 208 | { |
David Woodhouse | 02d087d | 2007-06-28 22:38:38 +0100 | [diff] [blame] | 209 | DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n", |
Kay Sievers | 160bbab | 2008-12-23 10:00:14 +0000 | [diff] [blame] | 210 | dev_name(&flash->spi->dev), __func__, |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 211 | flash->mtd.erasesize / 1024, offset); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 212 | |
| 213 | /* Wait until finished previous write command. */ |
| 214 | if (wait_till_ready(flash)) |
| 215 | return 1; |
| 216 | |
| 217 | /* Send write enable, then erase commands. */ |
| 218 | write_enable(flash); |
| 219 | |
| 220 | /* Set up command buffer. */ |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 221 | flash->command[0] = flash->erase_opcode; |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 222 | flash->command[1] = offset >> 16; |
| 223 | flash->command[2] = offset >> 8; |
| 224 | flash->command[3] = offset; |
| 225 | |
Bryan Wu | 2230b76 | 2008-04-25 12:07:32 +0800 | [diff] [blame] | 226 | spi_write(flash->spi, flash->command, CMD_SIZE); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /****************************************************************************/ |
| 232 | |
| 233 | /* |
| 234 | * MTD implementation |
| 235 | */ |
| 236 | |
| 237 | /* |
| 238 | * Erase an address range on the flash chip. The address range may extend |
| 239 | * one or more erase sectors. Return an error is there is a problem erasing. |
| 240 | */ |
| 241 | static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 242 | { |
| 243 | struct m25p *flash = mtd_to_m25p(mtd); |
| 244 | u32 addr,len; |
Artem Bityutskiy | d85316a | 2008-12-18 14:10:05 +0200 | [diff] [blame] | 245 | uint32_t rem; |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 246 | |
Artem Bityutskiy | d85316a | 2008-12-18 14:10:05 +0200 | [diff] [blame] | 247 | DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%llx, len %lld\n", |
Kay Sievers | 160bbab | 2008-12-23 10:00:14 +0000 | [diff] [blame] | 248 | dev_name(&flash->spi->dev), __func__, "at", |
| 249 | (long long)instr->addr, (long long)instr->len); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 250 | |
| 251 | /* sanity checks */ |
| 252 | if (instr->addr + instr->len > flash->mtd.size) |
| 253 | return -EINVAL; |
Artem Bityutskiy | d85316a | 2008-12-18 14:10:05 +0200 | [diff] [blame] | 254 | div_u64_rem(instr->len, mtd->erasesize, &rem); |
| 255 | if (rem) |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 256 | return -EINVAL; |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 257 | |
| 258 | addr = instr->addr; |
| 259 | len = instr->len; |
| 260 | |
David Brownell | 7d5230e | 2007-06-24 15:09:13 -0700 | [diff] [blame] | 261 | mutex_lock(&flash->lock); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 262 | |
Chen Gong | 7854643 | 2008-11-26 10:23:57 +0000 | [diff] [blame] | 263 | /* whole-chip erase? */ |
Steven A. Falco | 3f33b0a | 2009-04-27 17:10:10 -0400 | [diff] [blame] | 264 | if (len == flash->mtd.size) { |
| 265 | if (erase_chip(flash)) { |
| 266 | instr->state = MTD_ERASE_FAILED; |
| 267 | mutex_unlock(&flash->lock); |
| 268 | return -EIO; |
| 269 | } |
Chen Gong | 7854643 | 2008-11-26 10:23:57 +0000 | [diff] [blame] | 270 | |
| 271 | /* REVISIT in some cases we could speed up erasing large regions |
| 272 | * by using OPCODE_SE instead of OPCODE_BE_4K. We may have set up |
| 273 | * to use "small sector erase", but that's not always optimal. |
| 274 | */ |
| 275 | |
| 276 | /* "sector"-at-a-time erase */ |
Chen Gong | faff375 | 2008-08-11 16:59:13 +0800 | [diff] [blame] | 277 | } else { |
| 278 | while (len) { |
| 279 | if (erase_sector(flash, addr)) { |
| 280 | instr->state = MTD_ERASE_FAILED; |
| 281 | mutex_unlock(&flash->lock); |
| 282 | return -EIO; |
| 283 | } |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 284 | |
Chen Gong | faff375 | 2008-08-11 16:59:13 +0800 | [diff] [blame] | 285 | addr += mtd->erasesize; |
| 286 | len -= mtd->erasesize; |
| 287 | } |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 288 | } |
| 289 | |
David Brownell | 7d5230e | 2007-06-24 15:09:13 -0700 | [diff] [blame] | 290 | mutex_unlock(&flash->lock); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 291 | |
| 292 | instr->state = MTD_ERASE_DONE; |
| 293 | mtd_erase_callback(instr); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Read an address range from the flash chip. The address range |
| 300 | * may be any size provided it is within the physical boundaries. |
| 301 | */ |
| 302 | static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 303 | size_t *retlen, u_char *buf) |
| 304 | { |
| 305 | struct m25p *flash = mtd_to_m25p(mtd); |
| 306 | struct spi_transfer t[2]; |
| 307 | struct spi_message m; |
| 308 | |
| 309 | DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n", |
Kay Sievers | 160bbab | 2008-12-23 10:00:14 +0000 | [diff] [blame] | 310 | dev_name(&flash->spi->dev), __func__, "from", |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 311 | (u32)from, len); |
| 312 | |
| 313 | /* sanity checks */ |
| 314 | if (!len) |
| 315 | return 0; |
| 316 | |
| 317 | if (from + len > flash->mtd.size) |
| 318 | return -EINVAL; |
| 319 | |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 320 | spi_message_init(&m); |
| 321 | memset(t, 0, (sizeof t)); |
| 322 | |
Bryan Wu | 2230b76 | 2008-04-25 12:07:32 +0800 | [diff] [blame] | 323 | /* NOTE: |
| 324 | * OPCODE_FAST_READ (if available) is faster. |
| 325 | * Should add 1 byte DUMMY_BYTE. |
| 326 | */ |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 327 | t[0].tx_buf = flash->command; |
Bryan Wu | 2230b76 | 2008-04-25 12:07:32 +0800 | [diff] [blame] | 328 | t[0].len = CMD_SIZE + FAST_READ_DUMMY_BYTE; |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 329 | spi_message_add_tail(&t[0], &m); |
| 330 | |
| 331 | t[1].rx_buf = buf; |
| 332 | t[1].len = len; |
| 333 | spi_message_add_tail(&t[1], &m); |
| 334 | |
| 335 | /* Byte count starts at zero. */ |
| 336 | if (retlen) |
| 337 | *retlen = 0; |
| 338 | |
David Brownell | 7d5230e | 2007-06-24 15:09:13 -0700 | [diff] [blame] | 339 | mutex_lock(&flash->lock); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 340 | |
| 341 | /* Wait till previous write/erase is done. */ |
| 342 | if (wait_till_ready(flash)) { |
| 343 | /* REVISIT status return?? */ |
David Brownell | 7d5230e | 2007-06-24 15:09:13 -0700 | [diff] [blame] | 344 | mutex_unlock(&flash->lock); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 345 | return 1; |
| 346 | } |
| 347 | |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 348 | /* FIXME switch to OPCODE_FAST_READ. It's required for higher |
| 349 | * clocks; and at this writing, every chip this driver handles |
| 350 | * supports that opcode. |
| 351 | */ |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 352 | |
| 353 | /* Set up the write data buffer. */ |
| 354 | flash->command[0] = OPCODE_READ; |
| 355 | flash->command[1] = from >> 16; |
| 356 | flash->command[2] = from >> 8; |
| 357 | flash->command[3] = from; |
| 358 | |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 359 | spi_sync(flash->spi, &m); |
| 360 | |
Bryan Wu | 2230b76 | 2008-04-25 12:07:32 +0800 | [diff] [blame] | 361 | *retlen = m.actual_length - CMD_SIZE - FAST_READ_DUMMY_BYTE; |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 362 | |
David Brownell | 7d5230e | 2007-06-24 15:09:13 -0700 | [diff] [blame] | 363 | mutex_unlock(&flash->lock); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Write an address range to the flash chip. Data must be written in |
| 370 | * FLASH_PAGESIZE chunks. The address range may be any size provided |
| 371 | * it is within the physical boundaries. |
| 372 | */ |
| 373 | static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 374 | size_t *retlen, const u_char *buf) |
| 375 | { |
| 376 | struct m25p *flash = mtd_to_m25p(mtd); |
| 377 | u32 page_offset, page_size; |
| 378 | struct spi_transfer t[2]; |
| 379 | struct spi_message m; |
| 380 | |
| 381 | DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n", |
Kay Sievers | 160bbab | 2008-12-23 10:00:14 +0000 | [diff] [blame] | 382 | dev_name(&flash->spi->dev), __func__, "to", |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 383 | (u32)to, len); |
| 384 | |
| 385 | if (retlen) |
| 386 | *retlen = 0; |
| 387 | |
| 388 | /* sanity checks */ |
| 389 | if (!len) |
| 390 | return(0); |
| 391 | |
| 392 | if (to + len > flash->mtd.size) |
| 393 | return -EINVAL; |
| 394 | |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 395 | spi_message_init(&m); |
| 396 | memset(t, 0, (sizeof t)); |
| 397 | |
| 398 | t[0].tx_buf = flash->command; |
Bryan Wu | 2230b76 | 2008-04-25 12:07:32 +0800 | [diff] [blame] | 399 | t[0].len = CMD_SIZE; |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 400 | spi_message_add_tail(&t[0], &m); |
| 401 | |
| 402 | t[1].tx_buf = buf; |
| 403 | spi_message_add_tail(&t[1], &m); |
| 404 | |
David Brownell | 7d5230e | 2007-06-24 15:09:13 -0700 | [diff] [blame] | 405 | mutex_lock(&flash->lock); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 406 | |
| 407 | /* Wait until finished previous write command. */ |
Chen Gong | bc01886 | 2008-06-05 21:50:04 +0800 | [diff] [blame] | 408 | if (wait_till_ready(flash)) { |
| 409 | mutex_unlock(&flash->lock); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 410 | return 1; |
Chen Gong | bc01886 | 2008-06-05 21:50:04 +0800 | [diff] [blame] | 411 | } |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 412 | |
| 413 | write_enable(flash); |
| 414 | |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 415 | /* Set up the opcode in the write buffer. */ |
| 416 | flash->command[0] = OPCODE_PP; |
| 417 | flash->command[1] = to >> 16; |
| 418 | flash->command[2] = to >> 8; |
| 419 | flash->command[3] = to; |
| 420 | |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 421 | /* what page do we start with? */ |
| 422 | page_offset = to % FLASH_PAGESIZE; |
| 423 | |
| 424 | /* do all the bytes fit onto one page? */ |
| 425 | if (page_offset + len <= FLASH_PAGESIZE) { |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 426 | t[1].len = len; |
| 427 | |
| 428 | spi_sync(flash->spi, &m); |
| 429 | |
Bryan Wu | 2230b76 | 2008-04-25 12:07:32 +0800 | [diff] [blame] | 430 | *retlen = m.actual_length - CMD_SIZE; |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 431 | } else { |
| 432 | u32 i; |
| 433 | |
| 434 | /* the size of data remaining on the first page */ |
| 435 | page_size = FLASH_PAGESIZE - page_offset; |
| 436 | |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 437 | t[1].len = page_size; |
| 438 | spi_sync(flash->spi, &m); |
| 439 | |
Bryan Wu | 2230b76 | 2008-04-25 12:07:32 +0800 | [diff] [blame] | 440 | *retlen = m.actual_length - CMD_SIZE; |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 441 | |
| 442 | /* write everything in PAGESIZE chunks */ |
| 443 | for (i = page_size; i < len; i += page_size) { |
| 444 | page_size = len - i; |
| 445 | if (page_size > FLASH_PAGESIZE) |
| 446 | page_size = FLASH_PAGESIZE; |
| 447 | |
| 448 | /* write the next page to flash */ |
| 449 | flash->command[1] = (to + i) >> 16; |
| 450 | flash->command[2] = (to + i) >> 8; |
| 451 | flash->command[3] = (to + i); |
| 452 | |
| 453 | t[1].tx_buf = buf + i; |
| 454 | t[1].len = page_size; |
| 455 | |
| 456 | wait_till_ready(flash); |
| 457 | |
| 458 | write_enable(flash); |
| 459 | |
| 460 | spi_sync(flash->spi, &m); |
| 461 | |
David Brownell | 7111763d | 2006-01-08 13:34:29 -0800 | [diff] [blame] | 462 | if (retlen) |
Bryan Wu | 2230b76 | 2008-04-25 12:07:32 +0800 | [diff] [blame] | 463 | *retlen += m.actual_length - CMD_SIZE; |
David Brownell | 7d5230e | 2007-06-24 15:09:13 -0700 | [diff] [blame] | 464 | } |
| 465 | } |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 466 | |
David Brownell | 7d5230e | 2007-06-24 15:09:13 -0700 | [diff] [blame] | 467 | mutex_unlock(&flash->lock); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
Graf Yang | 49aac4a | 2009-06-15 08:23:41 +0000 | [diff] [blame] | 472 | static int sst_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 473 | size_t *retlen, const u_char *buf) |
| 474 | { |
| 475 | struct m25p *flash = mtd_to_m25p(mtd); |
| 476 | struct spi_transfer t[2]; |
| 477 | struct spi_message m; |
| 478 | size_t actual; |
| 479 | int cmd_sz, ret; |
| 480 | |
| 481 | if (retlen) |
| 482 | *retlen = 0; |
| 483 | |
| 484 | /* sanity checks */ |
| 485 | if (!len) |
| 486 | return 0; |
| 487 | |
| 488 | if (to + len > flash->mtd.size) |
| 489 | return -EINVAL; |
| 490 | |
| 491 | spi_message_init(&m); |
| 492 | memset(t, 0, (sizeof t)); |
| 493 | |
| 494 | t[0].tx_buf = flash->command; |
| 495 | t[0].len = CMD_SIZE; |
| 496 | spi_message_add_tail(&t[0], &m); |
| 497 | |
| 498 | t[1].tx_buf = buf; |
| 499 | spi_message_add_tail(&t[1], &m); |
| 500 | |
| 501 | mutex_lock(&flash->lock); |
| 502 | |
| 503 | /* Wait until finished previous write command. */ |
| 504 | ret = wait_till_ready(flash); |
| 505 | if (ret) |
| 506 | goto time_out; |
| 507 | |
| 508 | write_enable(flash); |
| 509 | |
| 510 | actual = to % 2; |
| 511 | /* Start write from odd address. */ |
| 512 | if (actual) { |
| 513 | flash->command[0] = OPCODE_BP; |
| 514 | flash->command[1] = to >> 16; |
| 515 | flash->command[2] = to >> 8; |
| 516 | flash->command[3] = to; |
| 517 | |
| 518 | /* write one byte. */ |
| 519 | t[1].len = 1; |
| 520 | spi_sync(flash->spi, &m); |
| 521 | ret = wait_till_ready(flash); |
| 522 | if (ret) |
| 523 | goto time_out; |
| 524 | *retlen += m.actual_length - CMD_SIZE; |
| 525 | } |
| 526 | to += actual; |
| 527 | |
| 528 | flash->command[0] = OPCODE_AAI_WP; |
| 529 | flash->command[1] = to >> 16; |
| 530 | flash->command[2] = to >> 8; |
| 531 | flash->command[3] = to; |
| 532 | |
| 533 | /* Write out most of the data here. */ |
| 534 | cmd_sz = CMD_SIZE; |
| 535 | for (; actual < len - 1; actual += 2) { |
| 536 | t[0].len = cmd_sz; |
| 537 | /* write two bytes. */ |
| 538 | t[1].len = 2; |
| 539 | t[1].tx_buf = buf + actual; |
| 540 | |
| 541 | spi_sync(flash->spi, &m); |
| 542 | ret = wait_till_ready(flash); |
| 543 | if (ret) |
| 544 | goto time_out; |
| 545 | *retlen += m.actual_length - cmd_sz; |
| 546 | cmd_sz = 1; |
| 547 | to += 2; |
| 548 | } |
| 549 | write_disable(flash); |
| 550 | ret = wait_till_ready(flash); |
| 551 | if (ret) |
| 552 | goto time_out; |
| 553 | |
| 554 | /* Write out trailing byte if it exists. */ |
| 555 | if (actual != len) { |
| 556 | write_enable(flash); |
| 557 | flash->command[0] = OPCODE_BP; |
| 558 | flash->command[1] = to >> 16; |
| 559 | flash->command[2] = to >> 8; |
| 560 | flash->command[3] = to; |
| 561 | t[0].len = CMD_SIZE; |
| 562 | t[1].len = 1; |
| 563 | t[1].tx_buf = buf + actual; |
| 564 | |
| 565 | spi_sync(flash->spi, &m); |
| 566 | ret = wait_till_ready(flash); |
| 567 | if (ret) |
| 568 | goto time_out; |
| 569 | *retlen += m.actual_length - CMD_SIZE; |
| 570 | write_disable(flash); |
| 571 | } |
| 572 | |
| 573 | time_out: |
| 574 | mutex_unlock(&flash->lock); |
| 575 | return ret; |
| 576 | } |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 577 | |
| 578 | /****************************************************************************/ |
| 579 | |
| 580 | /* |
| 581 | * SPI device driver setup and teardown |
| 582 | */ |
| 583 | |
| 584 | struct flash_info { |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 585 | /* JEDEC id zero means "no ID" (most older chips); otherwise it has |
| 586 | * a high byte of zero plus three data bytes: the manufacturer id, |
| 587 | * then a two byte device id. |
| 588 | */ |
| 589 | u32 jedec_id; |
Chen Gong | d0e8c47 | 2008-08-11 16:59:15 +0800 | [diff] [blame] | 590 | u16 ext_id; |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 591 | |
| 592 | /* The size listed here is what works with OPCODE_SE, which isn't |
| 593 | * necessarily called a "sector" by the vendor. |
| 594 | */ |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 595 | unsigned sector_size; |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 596 | u16 n_sectors; |
| 597 | |
| 598 | u16 flags; |
| 599 | #define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */ |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 600 | }; |
| 601 | |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 602 | #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \ |
| 603 | ((kernel_ulong_t)&(struct flash_info) { \ |
| 604 | .jedec_id = (_jedec_id), \ |
| 605 | .ext_id = (_ext_id), \ |
| 606 | .sector_size = (_sector_size), \ |
| 607 | .n_sectors = (_n_sectors), \ |
| 608 | .flags = (_flags), \ |
| 609 | }) |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 610 | |
| 611 | /* NOTE: double check command sets and memory organization when you add |
| 612 | * more flash chips. This current list focusses on newer chips, which |
| 613 | * have been converging on command sets which including JEDEC ID. |
| 614 | */ |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 615 | static const struct spi_device_id m25p_ids[] = { |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 616 | /* Atmel -- some are (confusingly) marketed as "DataFlash" */ |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 617 | { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) }, |
| 618 | { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) }, |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 619 | |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 620 | { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) }, |
| 621 | { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) }, |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 622 | |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 623 | { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) }, |
| 624 | { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) }, |
| 625 | { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) }, |
| 626 | { "at26df321", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) }, |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 627 | |
Lennert Buytenhek | ab1ff21 | 2009-05-20 13:07:11 +0200 | [diff] [blame] | 628 | /* Macronix */ |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 629 | { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) }, |
| 630 | { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) }, |
| 631 | { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) }, |
| 632 | { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) }, |
Lennert Buytenhek | ab1ff21 | 2009-05-20 13:07:11 +0200 | [diff] [blame] | 633 | |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 634 | /* Spansion -- single (large) sector size only, at least |
| 635 | * for the chips listed here (without boot sectors). |
| 636 | */ |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 637 | { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) }, |
| 638 | { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) }, |
| 639 | { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) }, |
| 640 | { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) }, |
| 641 | { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) }, |
| 642 | { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) }, |
| 643 | { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) }, |
| 644 | { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) }, |
| 645 | { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) }, |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 646 | |
| 647 | /* SST -- large erase sizes are "overlays", "sectors" are 4K */ |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 648 | { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K) }, |
| 649 | { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K) }, |
| 650 | { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K) }, |
| 651 | { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K) }, |
| 652 | { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K) }, |
| 653 | { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K) }, |
| 654 | { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K) }, |
| 655 | { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K) }, |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 656 | |
| 657 | /* ST Microelectronics -- newer production may have feature updates */ |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 658 | { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) }, |
| 659 | { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) }, |
| 660 | { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) }, |
| 661 | { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) }, |
| 662 | { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) }, |
| 663 | { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) }, |
| 664 | { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) }, |
| 665 | { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) }, |
| 666 | { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) }, |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 667 | |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 668 | { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) }, |
| 669 | { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) }, |
| 670 | { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) }, |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 671 | |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 672 | { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) }, |
| 673 | { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) }, |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 674 | |
David Woodhouse | 02d087d | 2007-06-28 22:38:38 +0100 | [diff] [blame] | 675 | /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */ |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 676 | { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) }, |
| 677 | { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) }, |
| 678 | { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) }, |
| 679 | { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) }, |
| 680 | { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) }, |
| 681 | { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) }, |
| 682 | { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) }, |
| 683 | { }, |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 684 | }; |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 685 | MODULE_DEVICE_TABLE(spi, m25p_ids); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 686 | |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 687 | static const struct spi_device_id *__devinit jedec_probe(struct spi_device *spi) |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 688 | { |
| 689 | int tmp; |
| 690 | u8 code = OPCODE_RDID; |
Chen Gong | daa8473 | 2008-09-16 14:14:12 +0800 | [diff] [blame] | 691 | u8 id[5]; |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 692 | u32 jedec; |
Chen Gong | d0e8c47 | 2008-08-11 16:59:15 +0800 | [diff] [blame] | 693 | u16 ext_jedec; |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 694 | struct flash_info *info; |
| 695 | |
| 696 | /* JEDEC also defines an optional "extended device information" |
| 697 | * string for after vendor-specific data, after the three bytes |
| 698 | * we use here. Supporting some chips might require using it. |
| 699 | */ |
Chen Gong | daa8473 | 2008-09-16 14:14:12 +0800 | [diff] [blame] | 700 | tmp = spi_write_then_read(spi, &code, 1, id, 5); |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 701 | if (tmp < 0) { |
| 702 | DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n", |
Kay Sievers | 160bbab | 2008-12-23 10:00:14 +0000 | [diff] [blame] | 703 | dev_name(&spi->dev), tmp); |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 704 | return NULL; |
| 705 | } |
| 706 | jedec = id[0]; |
| 707 | jedec = jedec << 8; |
| 708 | jedec |= id[1]; |
| 709 | jedec = jedec << 8; |
| 710 | jedec |= id[2]; |
| 711 | |
Anton Vorontsov | 18c6182 | 2009-10-12 20:24:38 +0400 | [diff] [blame^] | 712 | /* |
| 713 | * Some chips (like Numonyx M25P80) have JEDEC and non-JEDEC variants, |
| 714 | * which depend on technology process. Officially RDID command doesn't |
| 715 | * exist for non-JEDEC chips, but for compatibility they return ID 0. |
| 716 | */ |
| 717 | if (jedec == 0) |
| 718 | return NULL; |
| 719 | |
Chen Gong | d0e8c47 | 2008-08-11 16:59:15 +0800 | [diff] [blame] | 720 | ext_jedec = id[3] << 8 | id[4]; |
| 721 | |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 722 | for (tmp = 0; tmp < ARRAY_SIZE(m25p_ids) - 1; tmp++) { |
| 723 | info = (void *)m25p_ids[tmp].driver_data; |
Mike Frysinger | a3d3f73 | 2008-11-26 10:23:25 +0000 | [diff] [blame] | 724 | if (info->jedec_id == jedec) { |
Mike Frysinger | 9168ab8 | 2008-11-26 10:23:35 +0000 | [diff] [blame] | 725 | if (info->ext_id != 0 && info->ext_id != ext_jedec) |
Chen Gong | d0e8c47 | 2008-08-11 16:59:15 +0800 | [diff] [blame] | 726 | continue; |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 727 | return &m25p_ids[tmp]; |
Mike Frysinger | a3d3f73 | 2008-11-26 10:23:25 +0000 | [diff] [blame] | 728 | } |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 729 | } |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 734 | /* |
| 735 | * board specific setup should have ensured the SPI clock used here |
| 736 | * matches what the READ command supports, at least until this driver |
| 737 | * understands FAST_READ (for clocks over 25 MHz). |
| 738 | */ |
| 739 | static int __devinit m25p_probe(struct spi_device *spi) |
| 740 | { |
Anton Vorontsov | 18c6182 | 2009-10-12 20:24:38 +0400 | [diff] [blame^] | 741 | const struct spi_device_id *id = spi_get_device_id(spi); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 742 | struct flash_platform_data *data; |
| 743 | struct m25p *flash; |
| 744 | struct flash_info *info; |
| 745 | unsigned i; |
| 746 | |
| 747 | /* Platform data helps sort out which chip type we have, as |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 748 | * well as how this board partitions it. If we don't have |
| 749 | * a chip ID, try the JEDEC id commands; they'll work for most |
| 750 | * newer chips, even if we don't recognize the particular chip. |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 751 | */ |
| 752 | data = spi->dev.platform_data; |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 753 | if (data && data->type) { |
Anton Vorontsov | 18c6182 | 2009-10-12 20:24:38 +0400 | [diff] [blame^] | 754 | const struct spi_device_id *plat_id; |
| 755 | |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 756 | for (i = 0; i < ARRAY_SIZE(m25p_ids) - 1; i++) { |
Anton Vorontsov | 18c6182 | 2009-10-12 20:24:38 +0400 | [diff] [blame^] | 757 | plat_id = &m25p_ids[i]; |
| 758 | if (strcmp(data->type, plat_id->name)) |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 759 | continue; |
| 760 | break; |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 761 | } |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 762 | |
Anton Vorontsov | 18c6182 | 2009-10-12 20:24:38 +0400 | [diff] [blame^] | 763 | if (plat_id) |
| 764 | id = plat_id; |
| 765 | else |
| 766 | dev_warn(&spi->dev, "unrecognized id %s\n", data->type); |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 767 | } |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 768 | |
Anton Vorontsov | 18c6182 | 2009-10-12 20:24:38 +0400 | [diff] [blame^] | 769 | info = (void *)id->driver_data; |
| 770 | |
| 771 | if (info->jedec_id) { |
| 772 | const struct spi_device_id *jid; |
| 773 | |
| 774 | jid = jedec_probe(spi); |
| 775 | if (!jid) { |
| 776 | dev_info(&spi->dev, "non-JEDEC variant of %s\n", |
| 777 | id->name); |
| 778 | } else if (jid != id) { |
| 779 | /* |
| 780 | * JEDEC knows better, so overwrite platform ID. We |
| 781 | * can't trust partitions any longer, but we'll let |
| 782 | * mtd apply them anyway, since some partitions may be |
| 783 | * marked read-only, and we don't want to lose that |
| 784 | * information, even if it's not 100% accurate. |
| 785 | */ |
| 786 | dev_warn(&spi->dev, "found %s, expected %s\n", |
| 787 | jid->name, id->name); |
| 788 | id = jid; |
| 789 | info = (void *)jid->driver_data; |
| 790 | } |
| 791 | } |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 792 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 793 | flash = kzalloc(sizeof *flash, GFP_KERNEL); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 794 | if (!flash) |
| 795 | return -ENOMEM; |
Johannes Stezenbach | 61c3506 | 2009-10-28 14:21:37 +0100 | [diff] [blame] | 796 | flash->command = kmalloc(CMD_SIZE + FAST_READ_DUMMY_BYTE, GFP_KERNEL); |
| 797 | if (!flash->command) { |
| 798 | kfree(flash); |
| 799 | return -ENOMEM; |
| 800 | } |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 801 | |
| 802 | flash->spi = spi; |
David Brownell | 7d5230e | 2007-06-24 15:09:13 -0700 | [diff] [blame] | 803 | mutex_init(&flash->lock); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 804 | dev_set_drvdata(&spi->dev, flash); |
| 805 | |
Michael Hennerich | 7228982 | 2008-07-03 23:54:42 -0700 | [diff] [blame] | 806 | /* |
Graf Yang | ea60658a | 2009-09-24 15:46:22 -0400 | [diff] [blame] | 807 | * Atmel and SST serial flash tend to power |
| 808 | * up with the software protection bits set |
Michael Hennerich | 7228982 | 2008-07-03 23:54:42 -0700 | [diff] [blame] | 809 | */ |
| 810 | |
Graf Yang | ea60658a | 2009-09-24 15:46:22 -0400 | [diff] [blame] | 811 | if (info->jedec_id >> 16 == 0x1f || |
| 812 | info->jedec_id >> 16 == 0xbf) { |
Michael Hennerich | 7228982 | 2008-07-03 23:54:42 -0700 | [diff] [blame] | 813 | write_enable(flash); |
| 814 | write_sr(flash, 0); |
| 815 | } |
| 816 | |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 817 | if (data && data->name) |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 818 | flash->mtd.name = data->name; |
| 819 | else |
Kay Sievers | 160bbab | 2008-12-23 10:00:14 +0000 | [diff] [blame] | 820 | flash->mtd.name = dev_name(&spi->dev); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 821 | |
| 822 | flash->mtd.type = MTD_NORFLASH; |
Artem B. Bityutskiy | 783ed81 | 2006-06-14 19:53:44 +0400 | [diff] [blame] | 823 | flash->mtd.writesize = 1; |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 824 | flash->mtd.flags = MTD_CAP_NORFLASH; |
| 825 | flash->mtd.size = info->sector_size * info->n_sectors; |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 826 | flash->mtd.erase = m25p80_erase; |
| 827 | flash->mtd.read = m25p80_read; |
Graf Yang | 49aac4a | 2009-06-15 08:23:41 +0000 | [diff] [blame] | 828 | |
| 829 | /* sst flash chips use AAI word program */ |
| 830 | if (info->jedec_id >> 16 == 0xbf) |
| 831 | flash->mtd.write = sst_write; |
| 832 | else |
| 833 | flash->mtd.write = m25p80_write; |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 834 | |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 835 | /* prefer "small sector" erase if possible */ |
| 836 | if (info->flags & SECT_4K) { |
| 837 | flash->erase_opcode = OPCODE_BE_4K; |
| 838 | flash->mtd.erasesize = 4096; |
| 839 | } else { |
| 840 | flash->erase_opcode = OPCODE_SE; |
| 841 | flash->mtd.erasesize = info->sector_size; |
| 842 | } |
| 843 | |
David Brownell | 87f39f0 | 2009-03-26 00:42:50 -0700 | [diff] [blame] | 844 | flash->mtd.dev.parent = &spi->dev; |
| 845 | |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 846 | dev_info(&spi->dev, "%s (%lld Kbytes)\n", id->name, |
Artem Bityutskiy | d85316a | 2008-12-18 14:10:05 +0200 | [diff] [blame] | 847 | (long long)flash->mtd.size >> 10); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 848 | |
| 849 | DEBUG(MTD_DEBUG_LEVEL2, |
Artem Bityutskiy | d85316a | 2008-12-18 14:10:05 +0200 | [diff] [blame] | 850 | "mtd .name = %s, .size = 0x%llx (%lldMiB) " |
David Woodhouse | 02d087d | 2007-06-28 22:38:38 +0100 | [diff] [blame] | 851 | ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n", |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 852 | flash->mtd.name, |
Artem Bityutskiy | d85316a | 2008-12-18 14:10:05 +0200 | [diff] [blame] | 853 | (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20), |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 854 | flash->mtd.erasesize, flash->mtd.erasesize / 1024, |
| 855 | flash->mtd.numeraseregions); |
| 856 | |
| 857 | if (flash->mtd.numeraseregions) |
| 858 | for (i = 0; i < flash->mtd.numeraseregions; i++) |
| 859 | DEBUG(MTD_DEBUG_LEVEL2, |
Artem Bityutskiy | d85316a | 2008-12-18 14:10:05 +0200 | [diff] [blame] | 860 | "mtd.eraseregions[%d] = { .offset = 0x%llx, " |
David Woodhouse | 02d087d | 2007-06-28 22:38:38 +0100 | [diff] [blame] | 861 | ".erasesize = 0x%.8x (%uKiB), " |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 862 | ".numblocks = %d }\n", |
Artem Bityutskiy | d85316a | 2008-12-18 14:10:05 +0200 | [diff] [blame] | 863 | i, (long long)flash->mtd.eraseregions[i].offset, |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 864 | flash->mtd.eraseregions[i].erasesize, |
| 865 | flash->mtd.eraseregions[i].erasesize / 1024, |
| 866 | flash->mtd.eraseregions[i].numblocks); |
| 867 | |
| 868 | |
| 869 | /* partitions should match sector boundaries; and it may be good to |
| 870 | * use readonly partitions for writeprotected sectors (BP2..BP0). |
| 871 | */ |
| 872 | if (mtd_has_partitions()) { |
| 873 | struct mtd_partition *parts = NULL; |
| 874 | int nr_parts = 0; |
| 875 | |
David Brownell | a4b6d51 | 2009-03-04 12:01:41 -0800 | [diff] [blame] | 876 | if (mtd_has_cmdlinepart()) { |
| 877 | static const char *part_probes[] |
| 878 | = { "cmdlinepart", NULL, }; |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 879 | |
David Brownell | a4b6d51 | 2009-03-04 12:01:41 -0800 | [diff] [blame] | 880 | nr_parts = parse_mtd_partitions(&flash->mtd, |
| 881 | part_probes, &parts, 0); |
| 882 | } |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 883 | |
| 884 | if (nr_parts <= 0 && data && data->parts) { |
| 885 | parts = data->parts; |
| 886 | nr_parts = data->nr_parts; |
| 887 | } |
| 888 | |
| 889 | if (nr_parts > 0) { |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 890 | for (i = 0; i < nr_parts; i++) { |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 891 | DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = " |
Artem Bityutskiy | d85316a | 2008-12-18 14:10:05 +0200 | [diff] [blame] | 892 | "{.name = %s, .offset = 0x%llx, " |
| 893 | ".size = 0x%llx (%lldKiB) }\n", |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 894 | i, parts[i].name, |
Artem Bityutskiy | d85316a | 2008-12-18 14:10:05 +0200 | [diff] [blame] | 895 | (long long)parts[i].offset, |
| 896 | (long long)parts[i].size, |
| 897 | (long long)(parts[i].size >> 10)); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 898 | } |
| 899 | flash->partitioned = 1; |
| 900 | return add_mtd_partitions(&flash->mtd, parts, nr_parts); |
| 901 | } |
Anton Vorontsov | edcb3b1 | 2009-08-06 15:18:37 -0700 | [diff] [blame] | 902 | } else if (data && data->nr_parts) |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 903 | dev_warn(&spi->dev, "ignoring %d default partitions on %s\n", |
| 904 | data->nr_parts, data->name); |
| 905 | |
| 906 | return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0; |
| 907 | } |
| 908 | |
| 909 | |
| 910 | static int __devexit m25p_remove(struct spi_device *spi) |
| 911 | { |
| 912 | struct m25p *flash = dev_get_drvdata(&spi->dev); |
| 913 | int status; |
| 914 | |
| 915 | /* Clean up MTD stuff. */ |
| 916 | if (mtd_has_partitions() && flash->partitioned) |
| 917 | status = del_mtd_partitions(&flash->mtd); |
| 918 | else |
| 919 | status = del_mtd_device(&flash->mtd); |
Johannes Stezenbach | 61c3506 | 2009-10-28 14:21:37 +0100 | [diff] [blame] | 920 | if (status == 0) { |
| 921 | kfree(flash->command); |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 922 | kfree(flash); |
Johannes Stezenbach | 61c3506 | 2009-10-28 14:21:37 +0100 | [diff] [blame] | 923 | } |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | |
| 928 | static struct spi_driver m25p80_driver = { |
| 929 | .driver = { |
| 930 | .name = "m25p80", |
| 931 | .bus = &spi_bus_type, |
| 932 | .owner = THIS_MODULE, |
| 933 | }, |
Anton Vorontsov | b34bc03 | 2009-10-12 20:24:35 +0400 | [diff] [blame] | 934 | .id_table = m25p_ids, |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 935 | .probe = m25p_probe, |
| 936 | .remove = __devexit_p(m25p_remove), |
David Brownell | fa0a8c7 | 2007-06-24 15:12:35 -0700 | [diff] [blame] | 937 | |
| 938 | /* REVISIT: many of these chips have deep power-down modes, which |
| 939 | * should clearly be entered on suspend() to minimize power use. |
| 940 | * And also when they're otherwise idle... |
| 941 | */ |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 942 | }; |
| 943 | |
| 944 | |
Peter Huewe | 627df23 | 2009-06-11 02:23:33 +0200 | [diff] [blame] | 945 | static int __init m25p80_init(void) |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 946 | { |
| 947 | return spi_register_driver(&m25p80_driver); |
| 948 | } |
| 949 | |
| 950 | |
Peter Huewe | 627df23 | 2009-06-11 02:23:33 +0200 | [diff] [blame] | 951 | static void __exit m25p80_exit(void) |
Mike Lavender | 2f9f762 | 2006-01-08 13:34:27 -0800 | [diff] [blame] | 952 | { |
| 953 | spi_unregister_driver(&m25p80_driver); |
| 954 | } |
| 955 | |
| 956 | |
| 957 | module_init(m25p80_init); |
| 958 | module_exit(m25p80_exit); |
| 959 | |
| 960 | MODULE_LICENSE("GPL"); |
| 961 | MODULE_AUTHOR("Mike Lavender"); |
| 962 | MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips"); |