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