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