Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1 | /* |
Huang Shijie | 8eabdd1 | 2014-04-10 16:27:28 +0800 | [diff] [blame] | 2 | * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with |
| 3 | * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c |
| 4 | * |
| 5 | * Copyright (C) 2005, Intec Automation Inc. |
| 6 | * Copyright (C) 2014, Freescale Semiconductor, Inc. |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 7 | * |
| 8 | * This code is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/math64.h> |
Furquan Shaikh | 09b6a37 | 2015-09-18 14:59:17 -0700 | [diff] [blame] | 19 | #include <linux/sizes.h> |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 20 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 21 | #include <linux/mtd/mtd.h> |
| 22 | #include <linux/of_platform.h> |
| 23 | #include <linux/spi/flash.h> |
| 24 | #include <linux/mtd/spi-nor.h> |
| 25 | |
| 26 | /* Define max times to check status register before we give up. */ |
Furquan Shaikh | 09b6a37 | 2015-09-18 14:59:17 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * For everything but full-chip erase; probably could be much smaller, but kept |
| 30 | * around for safety for now |
| 31 | */ |
| 32 | #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ) |
| 33 | |
| 34 | /* |
| 35 | * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up |
| 36 | * for larger flash |
| 37 | */ |
| 38 | #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ) |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 39 | |
Huang Shijie | d928a25 | 2014-11-06 11:24:33 +0800 | [diff] [blame] | 40 | #define SPI_NOR_MAX_ID_LEN 6 |
| 41 | |
| 42 | struct flash_info { |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 43 | char *name; |
| 44 | |
Huang Shijie | d928a25 | 2014-11-06 11:24:33 +0800 | [diff] [blame] | 45 | /* |
| 46 | * This array stores the ID bytes. |
| 47 | * The first three bytes are the JEDIC ID. |
| 48 | * JEDEC ID zero means "no ID" (mostly older chips). |
| 49 | */ |
| 50 | u8 id[SPI_NOR_MAX_ID_LEN]; |
| 51 | u8 id_len; |
| 52 | |
| 53 | /* The size listed here is what works with SPINOR_OP_SE, which isn't |
| 54 | * necessarily called a "sector" by the vendor. |
| 55 | */ |
| 56 | unsigned sector_size; |
| 57 | u16 n_sectors; |
| 58 | |
| 59 | u16 page_size; |
| 60 | u16 addr_width; |
| 61 | |
| 62 | u16 flags; |
| 63 | #define SECT_4K 0x01 /* SPINOR_OP_BE_4K works uniformly */ |
| 64 | #define SPI_NOR_NO_ERASE 0x02 /* No erase command needed */ |
| 65 | #define SST_WRITE 0x04 /* use SST byte programming */ |
| 66 | #define SPI_NOR_NO_FR 0x08 /* Can't do fastread */ |
| 67 | #define SECT_4K_PMC 0x10 /* SPINOR_OP_BE_4K_PMC works uniformly */ |
| 68 | #define SPI_NOR_DUAL_READ 0x20 /* Flash supports Dual Read */ |
| 69 | #define SPI_NOR_QUAD_READ 0x40 /* Flash supports Quad Read */ |
| 70 | #define USE_FSR 0x80 /* use flag status register */ |
| 71 | }; |
| 72 | |
| 73 | #define JEDEC_MFR(info) ((info)->id[0]) |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 74 | |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 75 | static const struct flash_info *spi_nor_match_id(const char *name); |
Ben Hutchings | 70f3ce0 | 2014-09-29 11:47:54 +0200 | [diff] [blame] | 76 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 77 | /* |
| 78 | * Read the status register, returning its value in the location |
| 79 | * Return the status register value. |
| 80 | * Returns negative if error occurred. |
| 81 | */ |
| 82 | static int read_sr(struct spi_nor *nor) |
| 83 | { |
| 84 | int ret; |
| 85 | u8 val; |
| 86 | |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 87 | ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 88 | if (ret < 0) { |
| 89 | pr_err("error %d reading SR\n", (int) ret); |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | return val; |
| 94 | } |
| 95 | |
| 96 | /* |
grmoore@altera.com | c14dedd | 2014-04-29 10:29:51 -0500 | [diff] [blame] | 97 | * Read the flag status register, returning its value in the location |
| 98 | * Return the status register value. |
| 99 | * Returns negative if error occurred. |
| 100 | */ |
| 101 | static int read_fsr(struct spi_nor *nor) |
| 102 | { |
| 103 | int ret; |
| 104 | u8 val; |
| 105 | |
| 106 | ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1); |
| 107 | if (ret < 0) { |
| 108 | pr_err("error %d reading FSR\n", ret); |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | return val; |
| 113 | } |
| 114 | |
| 115 | /* |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 116 | * Read configuration register, returning its value in the |
| 117 | * location. Return the configuration register value. |
| 118 | * Returns negative if error occured. |
| 119 | */ |
| 120 | static int read_cr(struct spi_nor *nor) |
| 121 | { |
| 122 | int ret; |
| 123 | u8 val; |
| 124 | |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 125 | ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 126 | if (ret < 0) { |
| 127 | dev_err(nor->dev, "error %d reading CR\n", ret); |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | return val; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Dummy Cycle calculation for different type of read. |
| 136 | * It can be used to support more commands with |
| 137 | * different dummy cycle requirements. |
| 138 | */ |
| 139 | static inline int spi_nor_read_dummy_cycles(struct spi_nor *nor) |
| 140 | { |
| 141 | switch (nor->flash_read) { |
| 142 | case SPI_NOR_FAST: |
| 143 | case SPI_NOR_DUAL: |
| 144 | case SPI_NOR_QUAD: |
Huang Shijie | 0b78a2c | 2014-04-28 11:53:38 +0800 | [diff] [blame] | 145 | return 8; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 146 | case SPI_NOR_NORMAL: |
| 147 | return 0; |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Write status register 1 byte |
| 154 | * Returns negative if error occurred. |
| 155 | */ |
| 156 | static inline int write_sr(struct spi_nor *nor, u8 val) |
| 157 | { |
| 158 | nor->cmd_buf[0] = val; |
Jagan Teki | f9f3ce8 | 2015-08-19 15:26:44 +0530 | [diff] [blame] | 159 | return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Set write enable latch with Write Enable command. |
| 164 | * Returns negative if error occurred. |
| 165 | */ |
| 166 | static inline int write_enable(struct spi_nor *nor) |
| 167 | { |
Jagan Teki | f9f3ce8 | 2015-08-19 15:26:44 +0530 | [diff] [blame] | 168 | return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Send write disble instruction to the chip. |
| 173 | */ |
| 174 | static inline int write_disable(struct spi_nor *nor) |
| 175 | { |
Jagan Teki | f9f3ce8 | 2015-08-19 15:26:44 +0530 | [diff] [blame] | 176 | return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd) |
| 180 | { |
| 181 | return mtd->priv; |
| 182 | } |
| 183 | |
| 184 | /* Enable/disable 4-byte addressing mode. */ |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 185 | static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info, |
Huang Shijie | d928a25 | 2014-11-06 11:24:33 +0800 | [diff] [blame] | 186 | int enable) |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 187 | { |
| 188 | int status; |
| 189 | bool need_wren = false; |
| 190 | u8 cmd; |
| 191 | |
Huang Shijie | d928a25 | 2014-11-06 11:24:33 +0800 | [diff] [blame] | 192 | switch (JEDEC_MFR(info)) { |
Brian Norris | f0d2448 | 2015-09-01 12:57:09 -0700 | [diff] [blame^] | 193 | case SNOR_MFR_MICRON: |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 194 | /* Some Micron need WREN command; all will accept it */ |
| 195 | need_wren = true; |
Brian Norris | f0d2448 | 2015-09-01 12:57:09 -0700 | [diff] [blame^] | 196 | case SNOR_MFR_MACRONIX: |
| 197 | case SNOR_MFR_WINBOND: |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 198 | if (need_wren) |
| 199 | write_enable(nor); |
| 200 | |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 201 | cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B; |
Jagan Teki | f9f3ce8 | 2015-08-19 15:26:44 +0530 | [diff] [blame] | 202 | status = nor->write_reg(nor, cmd, NULL, 0); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 203 | if (need_wren) |
| 204 | write_disable(nor); |
| 205 | |
| 206 | return status; |
| 207 | default: |
| 208 | /* Spansion style */ |
| 209 | nor->cmd_buf[0] = enable << 7; |
Jagan Teki | f9f3ce8 | 2015-08-19 15:26:44 +0530 | [diff] [blame] | 210 | return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 211 | } |
| 212 | } |
Brian Norris | 51983b7 | 2014-09-10 00:26:16 -0700 | [diff] [blame] | 213 | static inline int spi_nor_sr_ready(struct spi_nor *nor) |
| 214 | { |
| 215 | int sr = read_sr(nor); |
| 216 | if (sr < 0) |
| 217 | return sr; |
| 218 | else |
| 219 | return !(sr & SR_WIP); |
| 220 | } |
| 221 | |
| 222 | static inline int spi_nor_fsr_ready(struct spi_nor *nor) |
| 223 | { |
| 224 | int fsr = read_fsr(nor); |
| 225 | if (fsr < 0) |
| 226 | return fsr; |
| 227 | else |
| 228 | return fsr & FSR_READY; |
| 229 | } |
| 230 | |
| 231 | static int spi_nor_ready(struct spi_nor *nor) |
| 232 | { |
| 233 | int sr, fsr; |
| 234 | sr = spi_nor_sr_ready(nor); |
| 235 | if (sr < 0) |
| 236 | return sr; |
| 237 | fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1; |
| 238 | if (fsr < 0) |
| 239 | return fsr; |
| 240 | return sr && fsr; |
| 241 | } |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 242 | |
Brian Norris | b94ed08 | 2014-08-06 18:17:00 -0700 | [diff] [blame] | 243 | /* |
| 244 | * Service routine to read status register until ready, or timeout occurs. |
| 245 | * Returns non-zero if error. |
| 246 | */ |
Furquan Shaikh | 09b6a37 | 2015-09-18 14:59:17 -0700 | [diff] [blame] | 247 | static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor, |
| 248 | unsigned long timeout_jiffies) |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 249 | { |
| 250 | unsigned long deadline; |
Brian Norris | a95ce92 | 2014-11-05 02:32:03 -0800 | [diff] [blame] | 251 | int timeout = 0, ret; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 252 | |
Furquan Shaikh | 09b6a37 | 2015-09-18 14:59:17 -0700 | [diff] [blame] | 253 | deadline = jiffies + timeout_jiffies; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 254 | |
Brian Norris | a95ce92 | 2014-11-05 02:32:03 -0800 | [diff] [blame] | 255 | while (!timeout) { |
| 256 | if (time_after_eq(jiffies, deadline)) |
| 257 | timeout = 1; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 258 | |
Brian Norris | 51983b7 | 2014-09-10 00:26:16 -0700 | [diff] [blame] | 259 | ret = spi_nor_ready(nor); |
| 260 | if (ret < 0) |
| 261 | return ret; |
| 262 | if (ret) |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 263 | return 0; |
Brian Norris | a95ce92 | 2014-11-05 02:32:03 -0800 | [diff] [blame] | 264 | |
| 265 | cond_resched(); |
| 266 | } |
| 267 | |
| 268 | dev_err(nor->dev, "flash operation timed out\n"); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 269 | |
| 270 | return -ETIMEDOUT; |
| 271 | } |
| 272 | |
Furquan Shaikh | 09b6a37 | 2015-09-18 14:59:17 -0700 | [diff] [blame] | 273 | static int spi_nor_wait_till_ready(struct spi_nor *nor) |
| 274 | { |
| 275 | return spi_nor_wait_till_ready_with_timeout(nor, |
| 276 | DEFAULT_READY_WAIT_JIFFIES); |
| 277 | } |
| 278 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 279 | /* |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 280 | * Erase the whole flash memory |
| 281 | * |
| 282 | * Returns 0 if successful, non-zero otherwise. |
| 283 | */ |
| 284 | static int erase_chip(struct spi_nor *nor) |
| 285 | { |
Brian Norris | 1976367 | 2015-08-13 15:46:05 -0700 | [diff] [blame] | 286 | dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10)); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 287 | |
Jagan Teki | f9f3ce8 | 2015-08-19 15:26:44 +0530 | [diff] [blame] | 288 | return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops) |
| 292 | { |
| 293 | int ret = 0; |
| 294 | |
| 295 | mutex_lock(&nor->lock); |
| 296 | |
| 297 | if (nor->prepare) { |
| 298 | ret = nor->prepare(nor, ops); |
| 299 | if (ret) { |
| 300 | dev_err(nor->dev, "failed in the preparation.\n"); |
| 301 | mutex_unlock(&nor->lock); |
| 302 | return ret; |
| 303 | } |
| 304 | } |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops) |
| 309 | { |
| 310 | if (nor->unprepare) |
| 311 | nor->unprepare(nor, ops); |
| 312 | mutex_unlock(&nor->lock); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Erase an address range on the nor chip. The address range may extend |
| 317 | * one or more erase sectors. Return an error is there is a problem erasing. |
| 318 | */ |
| 319 | static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 320 | { |
| 321 | struct spi_nor *nor = mtd_to_spi_nor(mtd); |
| 322 | u32 addr, len; |
| 323 | uint32_t rem; |
| 324 | int ret; |
| 325 | |
| 326 | dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr, |
| 327 | (long long)instr->len); |
| 328 | |
| 329 | div_u64_rem(instr->len, mtd->erasesize, &rem); |
| 330 | if (rem) |
| 331 | return -EINVAL; |
| 332 | |
| 333 | addr = instr->addr; |
| 334 | len = instr->len; |
| 335 | |
| 336 | ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE); |
| 337 | if (ret) |
| 338 | return ret; |
| 339 | |
| 340 | /* whole-chip erase? */ |
| 341 | if (len == mtd->size) { |
Furquan Shaikh | 09b6a37 | 2015-09-18 14:59:17 -0700 | [diff] [blame] | 342 | unsigned long timeout; |
| 343 | |
Brian Norris | 05241ae | 2014-11-05 02:29:03 -0800 | [diff] [blame] | 344 | write_enable(nor); |
| 345 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 346 | if (erase_chip(nor)) { |
| 347 | ret = -EIO; |
| 348 | goto erase_err; |
| 349 | } |
| 350 | |
Furquan Shaikh | 09b6a37 | 2015-09-18 14:59:17 -0700 | [diff] [blame] | 351 | /* |
| 352 | * Scale the timeout linearly with the size of the flash, with |
| 353 | * a minimum calibrated to an old 2MB flash. We could try to |
| 354 | * pull these from CFI/SFDP, but these values should be good |
| 355 | * enough for now. |
| 356 | */ |
| 357 | timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES, |
| 358 | CHIP_ERASE_2MB_READY_WAIT_JIFFIES * |
| 359 | (unsigned long)(mtd->size / SZ_2M)); |
| 360 | ret = spi_nor_wait_till_ready_with_timeout(nor, timeout); |
Brian Norris | dfa9c0c | 2014-08-06 18:16:57 -0700 | [diff] [blame] | 361 | if (ret) |
| 362 | goto erase_err; |
| 363 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 364 | /* REVISIT in some cases we could speed up erasing large regions |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 365 | * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 366 | * to use "small sector erase", but that's not always optimal. |
| 367 | */ |
| 368 | |
| 369 | /* "sector"-at-a-time erase */ |
| 370 | } else { |
| 371 | while (len) { |
Brian Norris | 05241ae | 2014-11-05 02:29:03 -0800 | [diff] [blame] | 372 | write_enable(nor); |
| 373 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 374 | if (nor->erase(nor, addr)) { |
| 375 | ret = -EIO; |
| 376 | goto erase_err; |
| 377 | } |
| 378 | |
| 379 | addr += mtd->erasesize; |
| 380 | len -= mtd->erasesize; |
Brian Norris | dfa9c0c | 2014-08-06 18:16:57 -0700 | [diff] [blame] | 381 | |
| 382 | ret = spi_nor_wait_till_ready(nor); |
| 383 | if (ret) |
| 384 | goto erase_err; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Brian Norris | 05241ae | 2014-11-05 02:29:03 -0800 | [diff] [blame] | 388 | write_disable(nor); |
| 389 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 390 | spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE); |
| 391 | |
| 392 | instr->state = MTD_ERASE_DONE; |
| 393 | mtd_erase_callback(instr); |
| 394 | |
| 395 | return ret; |
| 396 | |
| 397 | erase_err: |
| 398 | spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE); |
| 399 | instr->state = MTD_ERASE_FAILED; |
| 400 | return ret; |
| 401 | } |
| 402 | |
Brian Norris | 8cc7f33 | 2015-03-13 00:38:39 -0700 | [diff] [blame] | 403 | static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len) |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 404 | { |
Brian Norris | 1976367 | 2015-08-13 15:46:05 -0700 | [diff] [blame] | 405 | struct mtd_info *mtd = &nor->mtd; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 406 | uint32_t offset = ofs; |
| 407 | uint8_t status_old, status_new; |
| 408 | int ret = 0; |
| 409 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 410 | status_old = read_sr(nor); |
| 411 | |
| 412 | if (offset < mtd->size - (mtd->size / 2)) |
| 413 | status_new = status_old | SR_BP2 | SR_BP1 | SR_BP0; |
| 414 | else if (offset < mtd->size - (mtd->size / 4)) |
| 415 | status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1; |
| 416 | else if (offset < mtd->size - (mtd->size / 8)) |
| 417 | status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0; |
| 418 | else if (offset < mtd->size - (mtd->size / 16)) |
| 419 | status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2; |
| 420 | else if (offset < mtd->size - (mtd->size / 32)) |
| 421 | status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0; |
| 422 | else if (offset < mtd->size - (mtd->size / 64)) |
| 423 | status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1; |
| 424 | else |
| 425 | status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0; |
| 426 | |
| 427 | /* Only modify protection if it will not unlock other areas */ |
| 428 | if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) > |
| 429 | (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) { |
| 430 | write_enable(nor); |
| 431 | ret = write_sr(nor, status_new); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 432 | } |
| 433 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 434 | return ret; |
| 435 | } |
| 436 | |
Brian Norris | 8cc7f33 | 2015-03-13 00:38:39 -0700 | [diff] [blame] | 437 | static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len) |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 438 | { |
Brian Norris | 1976367 | 2015-08-13 15:46:05 -0700 | [diff] [blame] | 439 | struct mtd_info *mtd = &nor->mtd; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 440 | uint32_t offset = ofs; |
| 441 | uint8_t status_old, status_new; |
| 442 | int ret = 0; |
| 443 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 444 | status_old = read_sr(nor); |
| 445 | |
| 446 | if (offset+len > mtd->size - (mtd->size / 64)) |
| 447 | status_new = status_old & ~(SR_BP2 | SR_BP1 | SR_BP0); |
| 448 | else if (offset+len > mtd->size - (mtd->size / 32)) |
| 449 | status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0; |
| 450 | else if (offset+len > mtd->size - (mtd->size / 16)) |
| 451 | status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1; |
| 452 | else if (offset+len > mtd->size - (mtd->size / 8)) |
| 453 | status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0; |
| 454 | else if (offset+len > mtd->size - (mtd->size / 4)) |
| 455 | status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2; |
| 456 | else if (offset+len > mtd->size - (mtd->size / 2)) |
| 457 | status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0; |
| 458 | else |
| 459 | status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1; |
| 460 | |
| 461 | /* Only modify protection if it will not lock other areas */ |
| 462 | if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) < |
| 463 | (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) { |
| 464 | write_enable(nor); |
| 465 | ret = write_sr(nor, status_new); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 466 | } |
| 467 | |
Brian Norris | 8cc7f33 | 2015-03-13 00:38:39 -0700 | [diff] [blame] | 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 472 | { |
| 473 | struct spi_nor *nor = mtd_to_spi_nor(mtd); |
| 474 | int ret; |
| 475 | |
| 476 | ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK); |
| 477 | if (ret) |
| 478 | return ret; |
| 479 | |
| 480 | ret = nor->flash_lock(nor, ofs, len); |
| 481 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 482 | spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK); |
| 483 | return ret; |
| 484 | } |
| 485 | |
Brian Norris | 8cc7f33 | 2015-03-13 00:38:39 -0700 | [diff] [blame] | 486 | static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 487 | { |
| 488 | struct spi_nor *nor = mtd_to_spi_nor(mtd); |
| 489 | int ret; |
| 490 | |
| 491 | ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK); |
| 492 | if (ret) |
| 493 | return ret; |
| 494 | |
| 495 | ret = nor->flash_unlock(nor, ofs, len); |
| 496 | |
| 497 | spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK); |
| 498 | return ret; |
| 499 | } |
| 500 | |
Huang Shijie | 09ffafb | 2014-11-06 07:34:01 +0100 | [diff] [blame] | 501 | /* Used when the "_ext_id" is two bytes at most */ |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 502 | #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \ |
Huang Shijie | 09ffafb | 2014-11-06 07:34:01 +0100 | [diff] [blame] | 503 | .id = { \ |
| 504 | ((_jedec_id) >> 16) & 0xff, \ |
| 505 | ((_jedec_id) >> 8) & 0xff, \ |
| 506 | (_jedec_id) & 0xff, \ |
| 507 | ((_ext_id) >> 8) & 0xff, \ |
| 508 | (_ext_id) & 0xff, \ |
| 509 | }, \ |
| 510 | .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \ |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 511 | .sector_size = (_sector_size), \ |
| 512 | .n_sectors = (_n_sectors), \ |
| 513 | .page_size = 256, \ |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 514 | .flags = (_flags), |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 515 | |
Huang Shijie | 6d7604e | 2014-08-12 08:54:56 +0800 | [diff] [blame] | 516 | #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \ |
Huang Shijie | 6d7604e | 2014-08-12 08:54:56 +0800 | [diff] [blame] | 517 | .id = { \ |
| 518 | ((_jedec_id) >> 16) & 0xff, \ |
| 519 | ((_jedec_id) >> 8) & 0xff, \ |
| 520 | (_jedec_id) & 0xff, \ |
| 521 | ((_ext_id) >> 16) & 0xff, \ |
| 522 | ((_ext_id) >> 8) & 0xff, \ |
| 523 | (_ext_id) & 0xff, \ |
| 524 | }, \ |
| 525 | .id_len = 6, \ |
| 526 | .sector_size = (_sector_size), \ |
| 527 | .n_sectors = (_n_sectors), \ |
| 528 | .page_size = 256, \ |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 529 | .flags = (_flags), |
Huang Shijie | 6d7604e | 2014-08-12 08:54:56 +0800 | [diff] [blame] | 530 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 531 | #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \ |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 532 | .sector_size = (_sector_size), \ |
| 533 | .n_sectors = (_n_sectors), \ |
| 534 | .page_size = (_page_size), \ |
| 535 | .addr_width = (_addr_width), \ |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 536 | .flags = (_flags), |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 537 | |
| 538 | /* NOTE: double check command sets and memory organization when you add |
| 539 | * more nor chips. This current list focusses on newer chips, which |
| 540 | * have been converging on command sets which including JEDEC ID. |
Rafał Miłecki | c19900e | 2015-04-25 12:41:30 +0200 | [diff] [blame] | 541 | * |
| 542 | * All newly added entries should describe *hardware* and should use SECT_4K |
| 543 | * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage |
| 544 | * scenarios excluding small sectors there is config option that can be |
| 545 | * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS. |
| 546 | * For historical (and compatibility) reasons (before we got above config) some |
| 547 | * old entries may be missing 4K flag. |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 548 | */ |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 549 | static const struct flash_info spi_nor_ids[] = { |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 550 | /* Atmel -- some are (confusingly) marketed as "DataFlash" */ |
| 551 | { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) }, |
| 552 | { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) }, |
| 553 | |
| 554 | { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) }, |
| 555 | { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) }, |
| 556 | { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) }, |
| 557 | |
| 558 | { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) }, |
| 559 | { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) }, |
| 560 | { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) }, |
| 561 | { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) }, |
| 562 | |
| 563 | { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) }, |
| 564 | |
| 565 | /* EON -- en25xxx */ |
| 566 | { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) }, |
| 567 | { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) }, |
| 568 | { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) }, |
| 569 | { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) }, |
| 570 | { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) }, |
Sergey Ryazanov | a41595b | 2014-06-12 18:16:46 +0400 | [diff] [blame] | 571 | { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 572 | { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) }, |
Rafał Miłecki | c19900e | 2015-04-25 12:41:30 +0200 | [diff] [blame] | 573 | { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 574 | |
| 575 | /* ESMT */ |
| 576 | { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K) }, |
| 577 | |
| 578 | /* Everspin */ |
| 579 | { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) }, |
| 580 | { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) }, |
| 581 | |
Rostislav Lisovy | ce56ce7 | 2014-10-29 10:10:47 +0100 | [diff] [blame] | 582 | /* Fujitsu */ |
| 583 | { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) }, |
| 584 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 585 | /* GigaDevice */ |
| 586 | { "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64, SECT_4K) }, |
| 587 | { "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128, SECT_4K) }, |
Rafał Miłecki | fcc87a9 | 2014-12-16 22:46:56 +0100 | [diff] [blame] | 588 | { "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256, SECT_4K) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 589 | |
| 590 | /* Intel/Numonyx -- xxxs33b */ |
| 591 | { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) }, |
| 592 | { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) }, |
| 593 | { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) }, |
| 594 | |
Gabor Juhos | b79c332 | 2015-04-07 19:35:02 +0200 | [diff] [blame] | 595 | /* ISSI */ |
| 596 | { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) }, |
| 597 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 598 | /* Macronix */ |
Gabor Juhos | 660b5b0 | 2015-04-07 19:35:01 +0200 | [diff] [blame] | 599 | { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 600 | { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) }, |
| 601 | { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) }, |
| 602 | { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) }, |
| 603 | { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) }, |
| 604 | { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) }, |
| 605 | { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) }, |
| 606 | { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) }, |
Mika Westerberg | 81a1209 | 2015-02-05 18:39:03 +0200 | [diff] [blame] | 607 | { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 608 | { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) }, |
| 609 | { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) }, |
| 610 | { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) }, |
| 611 | { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) }, |
| 612 | { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_QUAD_READ) }, |
| 613 | { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) }, |
| 614 | |
| 615 | /* Micron */ |
Bean Huo 霍斌斌 (beanhuo) | 548cd3a | 2014-12-17 07:35:45 +0000 | [diff] [blame] | 616 | { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) }, |
Aurelien Chanot | f9bcb6d | 2015-10-07 12:10:08 -0700 | [diff] [blame] | 617 | { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) }, |
Alexey Firago | 0db7fae | 2015-06-30 12:53:46 +0300 | [diff] [blame] | 618 | { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) }, |
Mika Westerberg | 2a06c7b | 2015-08-27 12:52:19 +0300 | [diff] [blame] | 619 | { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) }, |
Bean Huo 霍斌斌 (beanhuo) | 548cd3a | 2014-12-17 07:35:45 +0000 | [diff] [blame] | 620 | { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) }, |
| 621 | { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) }, |
| 622 | { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) }, |
| 623 | { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) }, |
| 624 | { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) }, |
| 625 | { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 626 | |
| 627 | /* PMC */ |
| 628 | { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) }, |
| 629 | { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) }, |
| 630 | { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) }, |
| 631 | |
| 632 | /* Spansion -- single (large) sector size only, at least |
| 633 | * for the chips listed here (without boot sectors). |
| 634 | */ |
Geert Uytterhoeven | 9ab8699 | 2014-04-22 14:45:32 +0200 | [diff] [blame] | 635 | { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) }, |
Joachim Eastwood | 0f12a27 | 2015-08-14 18:42:32 +0200 | [diff] [blame] | 636 | { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 637 | { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) }, |
| 638 | { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) }, |
| 639 | { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) }, |
| 640 | { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) }, |
| 641 | { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) }, |
| 642 | { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) }, |
Rafał Miłecki | c19900e | 2015-04-25 12:41:30 +0200 | [diff] [blame] | 643 | { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) }, |
Jonas Gorski | c175208 | 2015-08-26 14:56:53 +0200 | [diff] [blame] | 644 | { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) }, |
| 645 | { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 646 | { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) }, |
| 647 | { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) }, |
| 648 | { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) }, |
| 649 | { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) }, |
| 650 | { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) }, |
Sean Nyekjaer | 7c748f5 | 2015-10-13 08:50:30 +0200 | [diff] [blame] | 651 | { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) }, |
Joachim Eastwood | adf508c | 2015-07-09 22:30:57 +0200 | [diff] [blame] | 652 | { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) }, |
| 653 | { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 654 | { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) }, |
Rafał Miłecki | c19900e | 2015-04-25 12:41:30 +0200 | [diff] [blame] | 655 | { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) }, |
Rafał Miłecki | 413780d | 2015-04-25 12:01:35 +0200 | [diff] [blame] | 656 | { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) }, |
Sean Nyekjaer | aada20c | 2015-10-13 08:51:14 +0200 | [diff] [blame] | 657 | { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 658 | |
| 659 | /* SST -- large erase sizes are "overlays", "sectors" are 4K */ |
| 660 | { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) }, |
| 661 | { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) }, |
| 662 | { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) }, |
| 663 | { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) }, |
| 664 | { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) }, |
| 665 | { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) }, |
| 666 | { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) }, |
| 667 | { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) }, |
Alexis Ballier | a1d97ef | 2015-08-14 19:35:39 +0200 | [diff] [blame] | 668 | { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) }, |
Yao Yuan | c887be7 | 2015-09-16 17:59:45 +0800 | [diff] [blame] | 669 | { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 670 | { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) }, |
Harini Katakam | f02985b | 2014-10-21 13:37:59 +0200 | [diff] [blame] | 671 | { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 672 | |
| 673 | /* ST Microelectronics -- newer production may have feature updates */ |
| 674 | { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) }, |
| 675 | { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) }, |
| 676 | { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) }, |
| 677 | { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) }, |
| 678 | { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) }, |
| 679 | { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) }, |
| 680 | { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) }, |
| 681 | { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) }, |
| 682 | { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 683 | |
| 684 | { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) }, |
| 685 | { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) }, |
| 686 | { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) }, |
| 687 | { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) }, |
| 688 | { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) }, |
| 689 | { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) }, |
| 690 | { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) }, |
| 691 | { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) }, |
| 692 | { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) }, |
| 693 | |
| 694 | { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) }, |
| 695 | { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) }, |
| 696 | { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) }, |
| 697 | |
| 698 | { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) }, |
| 699 | { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) }, |
| 700 | { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) }, |
| 701 | |
| 702 | { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) }, |
| 703 | { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) }, |
| 704 | { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) }, |
| 705 | { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) }, |
| 706 | { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) }, |
Thomas Petazzoni | f2fabe1 | 2014-07-27 23:56:08 +0200 | [diff] [blame] | 707 | { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 708 | |
| 709 | /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */ |
Gabor Juhos | 40d19ab | 2015-03-26 23:58:02 +0100 | [diff] [blame] | 710 | { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 711 | { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) }, |
| 712 | { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) }, |
| 713 | { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) }, |
| 714 | { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) }, |
| 715 | { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) }, |
| 716 | { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) }, |
| 717 | { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) }, |
| 718 | { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K) }, |
| 719 | { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) }, |
| 720 | { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) }, |
Mika Westerberg | e88e567 | 2015-02-05 18:39:02 +0200 | [diff] [blame] | 721 | { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K) }, |
Brian Norris | 4404bd7 | 2015-09-18 15:08:14 -0700 | [diff] [blame] | 722 | { "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) }, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 723 | { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) }, |
| 724 | { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) }, |
| 725 | { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) }, |
| 726 | { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) }, |
| 727 | |
| 728 | /* Catalyst / On Semiconductor -- non-JEDEC */ |
| 729 | { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) }, |
| 730 | { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) }, |
| 731 | { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) }, |
| 732 | { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) }, |
| 733 | { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) }, |
| 734 | { }, |
| 735 | }; |
| 736 | |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 737 | static const struct flash_info *spi_nor_read_id(struct spi_nor *nor) |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 738 | { |
| 739 | int tmp; |
Huang Shijie | 09ffafb | 2014-11-06 07:34:01 +0100 | [diff] [blame] | 740 | u8 id[SPI_NOR_MAX_ID_LEN]; |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 741 | const struct flash_info *info; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 742 | |
Huang Shijie | 09ffafb | 2014-11-06 07:34:01 +0100 | [diff] [blame] | 743 | tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 744 | if (tmp < 0) { |
| 745 | dev_dbg(nor->dev, " error %d reading JEDEC ID\n", tmp); |
| 746 | return ERR_PTR(tmp); |
| 747 | } |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 748 | |
| 749 | for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) { |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 750 | info = &spi_nor_ids[tmp]; |
Huang Shijie | 09ffafb | 2014-11-06 07:34:01 +0100 | [diff] [blame] | 751 | if (info->id_len) { |
| 752 | if (!memcmp(info->id, id, info->id_len)) |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 753 | return &spi_nor_ids[tmp]; |
| 754 | } |
| 755 | } |
Huang Shijie | 09ffafb | 2014-11-06 07:34:01 +0100 | [diff] [blame] | 756 | dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %2x, %2x\n", |
| 757 | id[0], id[1], id[2]); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 758 | return ERR_PTR(-ENODEV); |
| 759 | } |
| 760 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 761 | static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 762 | size_t *retlen, u_char *buf) |
| 763 | { |
| 764 | struct spi_nor *nor = mtd_to_spi_nor(mtd); |
| 765 | int ret; |
| 766 | |
| 767 | dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len); |
| 768 | |
| 769 | ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ); |
| 770 | if (ret) |
| 771 | return ret; |
| 772 | |
| 773 | ret = nor->read(nor, from, len, retlen, buf); |
| 774 | |
| 775 | spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ); |
| 776 | return ret; |
| 777 | } |
| 778 | |
| 779 | static int sst_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 780 | size_t *retlen, const u_char *buf) |
| 781 | { |
| 782 | struct spi_nor *nor = mtd_to_spi_nor(mtd); |
| 783 | size_t actual; |
| 784 | int ret; |
| 785 | |
| 786 | dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len); |
| 787 | |
| 788 | ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE); |
| 789 | if (ret) |
| 790 | return ret; |
| 791 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 792 | write_enable(nor); |
| 793 | |
| 794 | nor->sst_write_second = false; |
| 795 | |
| 796 | actual = to % 2; |
| 797 | /* Start write from odd address. */ |
| 798 | if (actual) { |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 799 | nor->program_opcode = SPINOR_OP_BP; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 800 | |
| 801 | /* write one byte. */ |
| 802 | nor->write(nor, to, 1, retlen, buf); |
Brian Norris | b94ed08 | 2014-08-06 18:17:00 -0700 | [diff] [blame] | 803 | ret = spi_nor_wait_till_ready(nor); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 804 | if (ret) |
| 805 | goto time_out; |
| 806 | } |
| 807 | to += actual; |
| 808 | |
| 809 | /* Write out most of the data here. */ |
| 810 | for (; actual < len - 1; actual += 2) { |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 811 | nor->program_opcode = SPINOR_OP_AAI_WP; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 812 | |
| 813 | /* write two bytes. */ |
| 814 | nor->write(nor, to, 2, retlen, buf + actual); |
Brian Norris | b94ed08 | 2014-08-06 18:17:00 -0700 | [diff] [blame] | 815 | ret = spi_nor_wait_till_ready(nor); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 816 | if (ret) |
| 817 | goto time_out; |
| 818 | to += 2; |
| 819 | nor->sst_write_second = true; |
| 820 | } |
| 821 | nor->sst_write_second = false; |
| 822 | |
| 823 | write_disable(nor); |
Brian Norris | b94ed08 | 2014-08-06 18:17:00 -0700 | [diff] [blame] | 824 | ret = spi_nor_wait_till_ready(nor); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 825 | if (ret) |
| 826 | goto time_out; |
| 827 | |
| 828 | /* Write out trailing byte if it exists. */ |
| 829 | if (actual != len) { |
| 830 | write_enable(nor); |
| 831 | |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 832 | nor->program_opcode = SPINOR_OP_BP; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 833 | nor->write(nor, to, 1, retlen, buf + actual); |
| 834 | |
Brian Norris | b94ed08 | 2014-08-06 18:17:00 -0700 | [diff] [blame] | 835 | ret = spi_nor_wait_till_ready(nor); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 836 | if (ret) |
| 837 | goto time_out; |
| 838 | write_disable(nor); |
| 839 | } |
| 840 | time_out: |
| 841 | spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE); |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * Write an address range to the nor chip. Data must be written in |
| 847 | * FLASH_PAGESIZE chunks. The address range may be any size provided |
| 848 | * it is within the physical boundaries. |
| 849 | */ |
| 850 | static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 851 | size_t *retlen, const u_char *buf) |
| 852 | { |
| 853 | struct spi_nor *nor = mtd_to_spi_nor(mtd); |
| 854 | u32 page_offset, page_size, i; |
| 855 | int ret; |
| 856 | |
| 857 | dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len); |
| 858 | |
| 859 | ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE); |
| 860 | if (ret) |
| 861 | return ret; |
| 862 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 863 | write_enable(nor); |
| 864 | |
| 865 | page_offset = to & (nor->page_size - 1); |
| 866 | |
| 867 | /* do all the bytes fit onto one page? */ |
| 868 | if (page_offset + len <= nor->page_size) { |
| 869 | nor->write(nor, to, len, retlen, buf); |
| 870 | } else { |
| 871 | /* the size of data remaining on the first page */ |
| 872 | page_size = nor->page_size - page_offset; |
| 873 | nor->write(nor, to, page_size, retlen, buf); |
| 874 | |
| 875 | /* write everything in nor->page_size chunks */ |
| 876 | for (i = page_size; i < len; i += page_size) { |
| 877 | page_size = len - i; |
| 878 | if (page_size > nor->page_size) |
| 879 | page_size = nor->page_size; |
| 880 | |
Brian Norris | b94ed08 | 2014-08-06 18:17:00 -0700 | [diff] [blame] | 881 | ret = spi_nor_wait_till_ready(nor); |
Brian Norris | 1d61dcb | 2014-08-06 18:16:56 -0700 | [diff] [blame] | 882 | if (ret) |
| 883 | goto write_err; |
| 884 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 885 | write_enable(nor); |
| 886 | |
| 887 | nor->write(nor, to + i, page_size, retlen, buf + i); |
| 888 | } |
| 889 | } |
| 890 | |
Brian Norris | dfa9c0c | 2014-08-06 18:16:57 -0700 | [diff] [blame] | 891 | ret = spi_nor_wait_till_ready(nor); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 892 | write_err: |
| 893 | spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE); |
Brian Norris | 1d61dcb | 2014-08-06 18:16:56 -0700 | [diff] [blame] | 894 | return ret; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | static int macronix_quad_enable(struct spi_nor *nor) |
| 898 | { |
| 899 | int ret, val; |
| 900 | |
| 901 | val = read_sr(nor); |
| 902 | write_enable(nor); |
| 903 | |
Jagan Teki | fd72523 | 2015-08-19 15:26:43 +0530 | [diff] [blame] | 904 | write_sr(nor, val | SR_QUAD_EN_MX); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 905 | |
Brian Norris | b94ed08 | 2014-08-06 18:17:00 -0700 | [diff] [blame] | 906 | if (spi_nor_wait_till_ready(nor)) |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 907 | return 1; |
| 908 | |
| 909 | ret = read_sr(nor); |
| 910 | if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) { |
| 911 | dev_err(nor->dev, "Macronix Quad bit not set\n"); |
| 912 | return -EINVAL; |
| 913 | } |
| 914 | |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | /* |
| 919 | * Write status Register and configuration register with 2 bytes |
| 920 | * The first byte will be written to the status register, while the |
| 921 | * second byte will be written to the configuration register. |
| 922 | * Return negative if error occured. |
| 923 | */ |
| 924 | static int write_sr_cr(struct spi_nor *nor, u16 val) |
| 925 | { |
| 926 | nor->cmd_buf[0] = val & 0xff; |
| 927 | nor->cmd_buf[1] = (val >> 8); |
| 928 | |
Jagan Teki | f9f3ce8 | 2015-08-19 15:26:44 +0530 | [diff] [blame] | 929 | return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | static int spansion_quad_enable(struct spi_nor *nor) |
| 933 | { |
| 934 | int ret; |
| 935 | int quad_en = CR_QUAD_EN_SPAN << 8; |
| 936 | |
| 937 | write_enable(nor); |
| 938 | |
| 939 | ret = write_sr_cr(nor, quad_en); |
| 940 | if (ret < 0) { |
| 941 | dev_err(nor->dev, |
| 942 | "error while writing configuration register\n"); |
| 943 | return -EINVAL; |
| 944 | } |
| 945 | |
| 946 | /* read back and check it */ |
| 947 | ret = read_cr(nor); |
| 948 | if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) { |
| 949 | dev_err(nor->dev, "Spansion Quad bit not set\n"); |
| 950 | return -EINVAL; |
| 951 | } |
| 952 | |
| 953 | return 0; |
| 954 | } |
| 955 | |
Bean Huo 霍斌斌 (beanhuo) | 548cd3a | 2014-12-17 07:35:45 +0000 | [diff] [blame] | 956 | static int micron_quad_enable(struct spi_nor *nor) |
| 957 | { |
| 958 | int ret; |
| 959 | u8 val; |
| 960 | |
| 961 | ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1); |
| 962 | if (ret < 0) { |
| 963 | dev_err(nor->dev, "error %d reading EVCR\n", ret); |
| 964 | return ret; |
| 965 | } |
| 966 | |
| 967 | write_enable(nor); |
| 968 | |
| 969 | /* set EVCR, enable quad I/O */ |
| 970 | nor->cmd_buf[0] = val & ~EVCR_QUAD_EN_MICRON; |
Jagan Teki | f9f3ce8 | 2015-08-19 15:26:44 +0530 | [diff] [blame] | 971 | ret = nor->write_reg(nor, SPINOR_OP_WD_EVCR, nor->cmd_buf, 1); |
Bean Huo 霍斌斌 (beanhuo) | 548cd3a | 2014-12-17 07:35:45 +0000 | [diff] [blame] | 972 | if (ret < 0) { |
| 973 | dev_err(nor->dev, "error while writing EVCR register\n"); |
| 974 | return ret; |
| 975 | } |
| 976 | |
| 977 | ret = spi_nor_wait_till_ready(nor); |
| 978 | if (ret) |
| 979 | return ret; |
| 980 | |
| 981 | /* read EVCR and check it */ |
| 982 | ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1); |
| 983 | if (ret < 0) { |
| 984 | dev_err(nor->dev, "error %d reading EVCR\n", ret); |
| 985 | return ret; |
| 986 | } |
| 987 | if (val & EVCR_QUAD_EN_MICRON) { |
| 988 | dev_err(nor->dev, "Micron EVCR Quad bit not clear\n"); |
| 989 | return -EINVAL; |
| 990 | } |
| 991 | |
| 992 | return 0; |
| 993 | } |
| 994 | |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 995 | static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info) |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 996 | { |
| 997 | int status; |
| 998 | |
Huang Shijie | d928a25 | 2014-11-06 11:24:33 +0800 | [diff] [blame] | 999 | switch (JEDEC_MFR(info)) { |
Brian Norris | f0d2448 | 2015-09-01 12:57:09 -0700 | [diff] [blame^] | 1000 | case SNOR_MFR_MACRONIX: |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1001 | status = macronix_quad_enable(nor); |
| 1002 | if (status) { |
| 1003 | dev_err(nor->dev, "Macronix quad-read not enabled\n"); |
| 1004 | return -EINVAL; |
| 1005 | } |
| 1006 | return status; |
Brian Norris | f0d2448 | 2015-09-01 12:57:09 -0700 | [diff] [blame^] | 1007 | case SNOR_MFR_MICRON: |
Bean Huo 霍斌斌 (beanhuo) | 548cd3a | 2014-12-17 07:35:45 +0000 | [diff] [blame] | 1008 | status = micron_quad_enable(nor); |
| 1009 | if (status) { |
| 1010 | dev_err(nor->dev, "Micron quad-read not enabled\n"); |
| 1011 | return -EINVAL; |
| 1012 | } |
| 1013 | return status; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1014 | default: |
| 1015 | status = spansion_quad_enable(nor); |
| 1016 | if (status) { |
| 1017 | dev_err(nor->dev, "Spansion quad-read not enabled\n"); |
| 1018 | return -EINVAL; |
| 1019 | } |
| 1020 | return status; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | static int spi_nor_check(struct spi_nor *nor) |
| 1025 | { |
| 1026 | if (!nor->dev || !nor->read || !nor->write || |
| 1027 | !nor->read_reg || !nor->write_reg || !nor->erase) { |
| 1028 | pr_err("spi-nor: please fill all the necessary fields!\n"); |
| 1029 | return -EINVAL; |
| 1030 | } |
| 1031 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1032 | return 0; |
| 1033 | } |
| 1034 | |
Ben Hutchings | 70f3ce0 | 2014-09-29 11:47:54 +0200 | [diff] [blame] | 1035 | int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode) |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1036 | { |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 1037 | const struct flash_info *info = NULL; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1038 | struct device *dev = nor->dev; |
Brian Norris | 1976367 | 2015-08-13 15:46:05 -0700 | [diff] [blame] | 1039 | struct mtd_info *mtd = &nor->mtd; |
Marek Vasut | 11bff0b | 2015-09-03 18:35:36 +0200 | [diff] [blame] | 1040 | struct device_node *np = nor->flash_node; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1041 | int ret; |
| 1042 | int i; |
| 1043 | |
| 1044 | ret = spi_nor_check(nor); |
| 1045 | if (ret) |
| 1046 | return ret; |
| 1047 | |
Brian Norris | 4316302 | 2015-05-19 14:38:22 -0700 | [diff] [blame] | 1048 | if (name) |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 1049 | info = spi_nor_match_id(name); |
Brian Norris | 4316302 | 2015-05-19 14:38:22 -0700 | [diff] [blame] | 1050 | /* Try to auto-detect if chip name wasn't specified or not found */ |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 1051 | if (!info) |
| 1052 | info = spi_nor_read_id(nor); |
| 1053 | if (IS_ERR_OR_NULL(info)) |
Ben Hutchings | 70f3ce0 | 2014-09-29 11:47:54 +0200 | [diff] [blame] | 1054 | return -ENOENT; |
| 1055 | |
Rafał Miłecki | 58c8195 | 2014-12-01 09:42:16 +0100 | [diff] [blame] | 1056 | /* |
| 1057 | * If caller has specified name of flash model that can normally be |
| 1058 | * detected using JEDEC, let's verify it. |
| 1059 | */ |
| 1060 | if (name && info->id_len) { |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 1061 | const struct flash_info *jinfo; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1062 | |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 1063 | jinfo = spi_nor_read_id(nor); |
| 1064 | if (IS_ERR(jinfo)) { |
| 1065 | return PTR_ERR(jinfo); |
| 1066 | } else if (jinfo != info) { |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1067 | /* |
| 1068 | * JEDEC knows better, so overwrite platform ID. We |
| 1069 | * can't trust partitions any longer, but we'll let |
| 1070 | * mtd apply them anyway, since some partitions may be |
| 1071 | * marked read-only, and we don't want to lose that |
| 1072 | * information, even if it's not 100% accurate. |
| 1073 | */ |
| 1074 | dev_warn(dev, "found %s, expected %s\n", |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 1075 | jinfo->name, info->name); |
| 1076 | info = jinfo; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | mutex_init(&nor->lock); |
| 1081 | |
| 1082 | /* |
| 1083 | * Atmel, SST and Intel/Numonyx serial nor tend to power |
| 1084 | * up with the software protection bits set |
| 1085 | */ |
| 1086 | |
Brian Norris | f0d2448 | 2015-09-01 12:57:09 -0700 | [diff] [blame^] | 1087 | if (JEDEC_MFR(info) == SNOR_MFR_ATMEL || |
| 1088 | JEDEC_MFR(info) == SNOR_MFR_INTEL || |
| 1089 | JEDEC_MFR(info) == SNOR_MFR_SST) { |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1090 | write_enable(nor); |
| 1091 | write_sr(nor, 0); |
| 1092 | } |
| 1093 | |
Rafał Miłecki | 32f1b7c | 2014-09-28 22:36:54 +0200 | [diff] [blame] | 1094 | if (!mtd->name) |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1095 | mtd->name = dev_name(dev); |
Brian Norris | c9ec390 | 2015-08-13 15:46:03 -0700 | [diff] [blame] | 1096 | mtd->priv = nor; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1097 | mtd->type = MTD_NORFLASH; |
| 1098 | mtd->writesize = 1; |
| 1099 | mtd->flags = MTD_CAP_NORFLASH; |
| 1100 | mtd->size = info->sector_size * info->n_sectors; |
| 1101 | mtd->_erase = spi_nor_erase; |
| 1102 | mtd->_read = spi_nor_read; |
| 1103 | |
Brian Norris | f0d2448 | 2015-09-01 12:57:09 -0700 | [diff] [blame^] | 1104 | /* NOR protection support for STmicro/Micron chips */ |
| 1105 | if (JEDEC_MFR(info) == SNOR_MFR_MICRON) { |
Brian Norris | 8cc7f33 | 2015-03-13 00:38:39 -0700 | [diff] [blame] | 1106 | nor->flash_lock = stm_lock; |
| 1107 | nor->flash_unlock = stm_unlock; |
| 1108 | } |
| 1109 | |
| 1110 | if (nor->flash_lock && nor->flash_unlock) { |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1111 | mtd->_lock = spi_nor_lock; |
| 1112 | mtd->_unlock = spi_nor_unlock; |
| 1113 | } |
| 1114 | |
| 1115 | /* sst nor chips use AAI word program */ |
| 1116 | if (info->flags & SST_WRITE) |
| 1117 | mtd->_write = sst_write; |
| 1118 | else |
| 1119 | mtd->_write = spi_nor_write; |
| 1120 | |
Brian Norris | 51983b7 | 2014-09-10 00:26:16 -0700 | [diff] [blame] | 1121 | if (info->flags & USE_FSR) |
| 1122 | nor->flags |= SNOR_F_USE_FSR; |
grmoore@altera.com | c14dedd | 2014-04-29 10:29:51 -0500 | [diff] [blame] | 1123 | |
Rafał Miłecki | 57cf26c | 2014-08-17 11:27:26 +0200 | [diff] [blame] | 1124 | #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1125 | /* prefer "small sector" erase if possible */ |
| 1126 | if (info->flags & SECT_4K) { |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 1127 | nor->erase_opcode = SPINOR_OP_BE_4K; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1128 | mtd->erasesize = 4096; |
| 1129 | } else if (info->flags & SECT_4K_PMC) { |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 1130 | nor->erase_opcode = SPINOR_OP_BE_4K_PMC; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1131 | mtd->erasesize = 4096; |
Rafał Miłecki | 57cf26c | 2014-08-17 11:27:26 +0200 | [diff] [blame] | 1132 | } else |
| 1133 | #endif |
| 1134 | { |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 1135 | nor->erase_opcode = SPINOR_OP_SE; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1136 | mtd->erasesize = info->sector_size; |
| 1137 | } |
| 1138 | |
| 1139 | if (info->flags & SPI_NOR_NO_ERASE) |
| 1140 | mtd->flags |= MTD_NO_ERASE; |
| 1141 | |
| 1142 | mtd->dev.parent = dev; |
| 1143 | nor->page_size = info->page_size; |
| 1144 | mtd->writebufsize = nor->page_size; |
| 1145 | |
| 1146 | if (np) { |
| 1147 | /* If we were instantiated by DT, use it */ |
| 1148 | if (of_property_read_bool(np, "m25p,fast-read")) |
| 1149 | nor->flash_read = SPI_NOR_FAST; |
| 1150 | else |
| 1151 | nor->flash_read = SPI_NOR_NORMAL; |
| 1152 | } else { |
| 1153 | /* If we weren't instantiated by DT, default to fast-read */ |
| 1154 | nor->flash_read = SPI_NOR_FAST; |
| 1155 | } |
| 1156 | |
| 1157 | /* Some devices cannot do fast-read, no matter what DT tells us */ |
| 1158 | if (info->flags & SPI_NOR_NO_FR) |
| 1159 | nor->flash_read = SPI_NOR_NORMAL; |
| 1160 | |
| 1161 | /* Quad/Dual-read mode takes precedence over fast/normal */ |
| 1162 | if (mode == SPI_NOR_QUAD && info->flags & SPI_NOR_QUAD_READ) { |
Huang Shijie | d928a25 | 2014-11-06 11:24:33 +0800 | [diff] [blame] | 1163 | ret = set_quad_mode(nor, info); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1164 | if (ret) { |
| 1165 | dev_err(dev, "quad mode not supported\n"); |
| 1166 | return ret; |
| 1167 | } |
| 1168 | nor->flash_read = SPI_NOR_QUAD; |
| 1169 | } else if (mode == SPI_NOR_DUAL && info->flags & SPI_NOR_DUAL_READ) { |
| 1170 | nor->flash_read = SPI_NOR_DUAL; |
| 1171 | } |
| 1172 | |
| 1173 | /* Default commands */ |
| 1174 | switch (nor->flash_read) { |
| 1175 | case SPI_NOR_QUAD: |
Brian Norris | 58b89a1 | 2014-04-08 19:16:49 -0700 | [diff] [blame] | 1176 | nor->read_opcode = SPINOR_OP_READ_1_1_4; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1177 | break; |
| 1178 | case SPI_NOR_DUAL: |
Brian Norris | 58b89a1 | 2014-04-08 19:16:49 -0700 | [diff] [blame] | 1179 | nor->read_opcode = SPINOR_OP_READ_1_1_2; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1180 | break; |
| 1181 | case SPI_NOR_FAST: |
Brian Norris | 58b89a1 | 2014-04-08 19:16:49 -0700 | [diff] [blame] | 1182 | nor->read_opcode = SPINOR_OP_READ_FAST; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1183 | break; |
| 1184 | case SPI_NOR_NORMAL: |
Brian Norris | 58b89a1 | 2014-04-08 19:16:49 -0700 | [diff] [blame] | 1185 | nor->read_opcode = SPINOR_OP_READ; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1186 | break; |
| 1187 | default: |
| 1188 | dev_err(dev, "No Read opcode defined\n"); |
| 1189 | return -EINVAL; |
| 1190 | } |
| 1191 | |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 1192 | nor->program_opcode = SPINOR_OP_PP; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1193 | |
| 1194 | if (info->addr_width) |
| 1195 | nor->addr_width = info->addr_width; |
| 1196 | else if (mtd->size > 0x1000000) { |
| 1197 | /* enable 4-byte addressing if the device exceeds 16MiB */ |
| 1198 | nor->addr_width = 4; |
Brian Norris | f0d2448 | 2015-09-01 12:57:09 -0700 | [diff] [blame^] | 1199 | if (JEDEC_MFR(info) == SNOR_MFR_SPANSION) { |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1200 | /* Dedicated 4-byte command set */ |
| 1201 | switch (nor->flash_read) { |
| 1202 | case SPI_NOR_QUAD: |
Brian Norris | 58b89a1 | 2014-04-08 19:16:49 -0700 | [diff] [blame] | 1203 | nor->read_opcode = SPINOR_OP_READ4_1_1_4; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1204 | break; |
| 1205 | case SPI_NOR_DUAL: |
Brian Norris | 58b89a1 | 2014-04-08 19:16:49 -0700 | [diff] [blame] | 1206 | nor->read_opcode = SPINOR_OP_READ4_1_1_2; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1207 | break; |
| 1208 | case SPI_NOR_FAST: |
Brian Norris | 58b89a1 | 2014-04-08 19:16:49 -0700 | [diff] [blame] | 1209 | nor->read_opcode = SPINOR_OP_READ4_FAST; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1210 | break; |
| 1211 | case SPI_NOR_NORMAL: |
Brian Norris | 58b89a1 | 2014-04-08 19:16:49 -0700 | [diff] [blame] | 1212 | nor->read_opcode = SPINOR_OP_READ4; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1213 | break; |
| 1214 | } |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 1215 | nor->program_opcode = SPINOR_OP_PP_4B; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1216 | /* No small sector erase for 4-byte command set */ |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 1217 | nor->erase_opcode = SPINOR_OP_SE_4B; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1218 | mtd->erasesize = info->sector_size; |
| 1219 | } else |
Huang Shijie | d928a25 | 2014-11-06 11:24:33 +0800 | [diff] [blame] | 1220 | set_4byte(nor, info, 1); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1221 | } else { |
| 1222 | nor->addr_width = 3; |
| 1223 | } |
| 1224 | |
| 1225 | nor->read_dummy = spi_nor_read_dummy_cycles(nor); |
| 1226 | |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 1227 | dev_info(dev, "%s (%lld Kbytes)\n", info->name, |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1228 | (long long)mtd->size >> 10); |
| 1229 | |
| 1230 | dev_dbg(dev, |
| 1231 | "mtd .name = %s, .size = 0x%llx (%lldMiB), " |
| 1232 | ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n", |
| 1233 | mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20), |
| 1234 | mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions); |
| 1235 | |
| 1236 | if (mtd->numeraseregions) |
| 1237 | for (i = 0; i < mtd->numeraseregions; i++) |
| 1238 | dev_dbg(dev, |
| 1239 | "mtd.eraseregions[%d] = { .offset = 0x%llx, " |
| 1240 | ".erasesize = 0x%.8x (%uKiB), " |
| 1241 | ".numblocks = %d }\n", |
| 1242 | i, (long long)mtd->eraseregions[i].offset, |
| 1243 | mtd->eraseregions[i].erasesize, |
| 1244 | mtd->eraseregions[i].erasesize / 1024, |
| 1245 | mtd->eraseregions[i].numblocks); |
| 1246 | return 0; |
| 1247 | } |
Brian Norris | b61834b | 2014-04-08 18:22:57 -0700 | [diff] [blame] | 1248 | EXPORT_SYMBOL_GPL(spi_nor_scan); |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1249 | |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 1250 | static const struct flash_info *spi_nor_match_id(const char *name) |
Huang Shijie | 0d8c11c | 2014-02-24 18:37:40 +0800 | [diff] [blame] | 1251 | { |
Rafał Miłecki | 06bb6f5 | 2015-08-10 21:39:03 +0200 | [diff] [blame] | 1252 | const struct flash_info *id = spi_nor_ids; |
Huang Shijie | 0d8c11c | 2014-02-24 18:37:40 +0800 | [diff] [blame] | 1253 | |
Brian Norris | 2ff46e6 | 2015-09-02 16:34:35 -0700 | [diff] [blame] | 1254 | while (id->name) { |
Huang Shijie | 0d8c11c | 2014-02-24 18:37:40 +0800 | [diff] [blame] | 1255 | if (!strcmp(name, id->name)) |
| 1256 | return id; |
| 1257 | id++; |
| 1258 | } |
| 1259 | return NULL; |
| 1260 | } |
| 1261 | |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame] | 1262 | MODULE_LICENSE("GPL"); |
| 1263 | MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>"); |
| 1264 | MODULE_AUTHOR("Mike Lavender"); |
| 1265 | MODULE_DESCRIPTION("framework for SPI NOR"); |