blob: d356a1f98c42eedddb3662be90de3efa1f8c9697 [file] [log] [blame]
Huang Shijieb1994892014-02-24 18:37:37 +08001/*
Huang Shijie8eabdd12014-04-10 16:27:28 +08002 * 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 Shijieb1994892014-02-24 18:37:37 +08007 *
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 Shaikh09b6a372015-09-18 14:59:17 -070019#include <linux/sizes.h>
Huang Shijieb1994892014-02-24 18:37:37 +080020
Huang Shijieb1994892014-02-24 18:37:37 +080021#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 Shaikh09b6a372015-09-18 14:59:17 -070027
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 Shijieb1994892014-02-24 18:37:37 +080039
Huang Shijied928a252014-11-06 11:24:33 +080040#define SPI_NOR_MAX_ID_LEN 6
41
42struct flash_info {
Rafał Miłecki06bb6f52015-08-10 21:39:03 +020043 char *name;
44
Huang Shijied928a252014-11-06 11:24:33 +080045 /*
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 Shijieb1994892014-02-24 18:37:37 +080074
Rafał Miłecki06bb6f52015-08-10 21:39:03 +020075static const struct flash_info *spi_nor_match_id(const char *name);
Ben Hutchings70f3ce02014-09-29 11:47:54 +020076
Huang Shijieb1994892014-02-24 18:37:37 +080077/*
78 * Read the status register, returning its value in the location
79 * Return the status register value.
80 * Returns negative if error occurred.
81 */
82static int read_sr(struct spi_nor *nor)
83{
84 int ret;
85 u8 val;
86
Brian Norrisb02e7f32014-04-08 18:15:31 -070087 ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
Huang Shijieb1994892014-02-24 18:37:37 +080088 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.comc14dedd2014-04-29 10:29:51 -050097 * Read the flag status register, returning its value in the location
98 * Return the status register value.
99 * Returns negative if error occurred.
100 */
101static 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 Shijieb1994892014-02-24 18:37:37 +0800116 * Read configuration register, returning its value in the
117 * location. Return the configuration register value.
118 * Returns negative if error occured.
119 */
120static int read_cr(struct spi_nor *nor)
121{
122 int ret;
123 u8 val;
124
Brian Norrisb02e7f32014-04-08 18:15:31 -0700125 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
Huang Shijieb1994892014-02-24 18:37:37 +0800126 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 */
139static 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 Shijie0b78a2c2014-04-28 11:53:38 +0800145 return 8;
Huang Shijieb1994892014-02-24 18:37:37 +0800146 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 */
156static inline int write_sr(struct spi_nor *nor, u8 val)
157{
158 nor->cmd_buf[0] = val;
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530159 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
Huang Shijieb1994892014-02-24 18:37:37 +0800160}
161
162/*
163 * Set write enable latch with Write Enable command.
164 * Returns negative if error occurred.
165 */
166static inline int write_enable(struct spi_nor *nor)
167{
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530168 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800169}
170
171/*
172 * Send write disble instruction to the chip.
173 */
174static inline int write_disable(struct spi_nor *nor)
175{
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530176 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800177}
178
179static 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łecki06bb6f52015-08-10 21:39:03 +0200185static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
Huang Shijied928a252014-11-06 11:24:33 +0800186 int enable)
Huang Shijieb1994892014-02-24 18:37:37 +0800187{
188 int status;
189 bool need_wren = false;
190 u8 cmd;
191
Huang Shijied928a252014-11-06 11:24:33 +0800192 switch (JEDEC_MFR(info)) {
Brian Norrisf0d24482015-09-01 12:57:09 -0700193 case SNOR_MFR_MICRON:
Huang Shijieb1994892014-02-24 18:37:37 +0800194 /* Some Micron need WREN command; all will accept it */
195 need_wren = true;
Brian Norrisf0d24482015-09-01 12:57:09 -0700196 case SNOR_MFR_MACRONIX:
197 case SNOR_MFR_WINBOND:
Huang Shijieb1994892014-02-24 18:37:37 +0800198 if (need_wren)
199 write_enable(nor);
200
Brian Norrisb02e7f32014-04-08 18:15:31 -0700201 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530202 status = nor->write_reg(nor, cmd, NULL, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800203 if (need_wren)
204 write_disable(nor);
205
206 return status;
207 default:
208 /* Spansion style */
209 nor->cmd_buf[0] = enable << 7;
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530210 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
Huang Shijieb1994892014-02-24 18:37:37 +0800211 }
212}
Brian Norris51983b72014-09-10 00:26:16 -0700213static 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
222static 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
231static 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 Shijieb1994892014-02-24 18:37:37 +0800242
Brian Norrisb94ed082014-08-06 18:17:00 -0700243/*
244 * Service routine to read status register until ready, or timeout occurs.
245 * Returns non-zero if error.
246 */
Furquan Shaikh09b6a372015-09-18 14:59:17 -0700247static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
248 unsigned long timeout_jiffies)
Huang Shijieb1994892014-02-24 18:37:37 +0800249{
250 unsigned long deadline;
Brian Norrisa95ce922014-11-05 02:32:03 -0800251 int timeout = 0, ret;
Huang Shijieb1994892014-02-24 18:37:37 +0800252
Furquan Shaikh09b6a372015-09-18 14:59:17 -0700253 deadline = jiffies + timeout_jiffies;
Huang Shijieb1994892014-02-24 18:37:37 +0800254
Brian Norrisa95ce922014-11-05 02:32:03 -0800255 while (!timeout) {
256 if (time_after_eq(jiffies, deadline))
257 timeout = 1;
Huang Shijieb1994892014-02-24 18:37:37 +0800258
Brian Norris51983b72014-09-10 00:26:16 -0700259 ret = spi_nor_ready(nor);
260 if (ret < 0)
261 return ret;
262 if (ret)
Huang Shijieb1994892014-02-24 18:37:37 +0800263 return 0;
Brian Norrisa95ce922014-11-05 02:32:03 -0800264
265 cond_resched();
266 }
267
268 dev_err(nor->dev, "flash operation timed out\n");
Huang Shijieb1994892014-02-24 18:37:37 +0800269
270 return -ETIMEDOUT;
271}
272
Furquan Shaikh09b6a372015-09-18 14:59:17 -0700273static 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 Shijieb1994892014-02-24 18:37:37 +0800279/*
Huang Shijieb1994892014-02-24 18:37:37 +0800280 * Erase the whole flash memory
281 *
282 * Returns 0 if successful, non-zero otherwise.
283 */
284static int erase_chip(struct spi_nor *nor)
285{
Brian Norris19763672015-08-13 15:46:05 -0700286 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
Huang Shijieb1994892014-02-24 18:37:37 +0800287
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530288 return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800289}
290
291static 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
308static 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 */
319static 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 Shaikh09b6a372015-09-18 14:59:17 -0700342 unsigned long timeout;
343
Brian Norris05241ae2014-11-05 02:29:03 -0800344 write_enable(nor);
345
Huang Shijieb1994892014-02-24 18:37:37 +0800346 if (erase_chip(nor)) {
347 ret = -EIO;
348 goto erase_err;
349 }
350
Furquan Shaikh09b6a372015-09-18 14:59:17 -0700351 /*
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 Norrisdfa9c0c2014-08-06 18:16:57 -0700361 if (ret)
362 goto erase_err;
363
Huang Shijieb1994892014-02-24 18:37:37 +0800364 /* REVISIT in some cases we could speed up erasing large regions
Brian Norrisb02e7f32014-04-08 18:15:31 -0700365 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
Huang Shijieb1994892014-02-24 18:37:37 +0800366 * 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 Norris05241ae2014-11-05 02:29:03 -0800372 write_enable(nor);
373
Huang Shijieb1994892014-02-24 18:37:37 +0800374 if (nor->erase(nor, addr)) {
375 ret = -EIO;
376 goto erase_err;
377 }
378
379 addr += mtd->erasesize;
380 len -= mtd->erasesize;
Brian Norrisdfa9c0c2014-08-06 18:16:57 -0700381
382 ret = spi_nor_wait_till_ready(nor);
383 if (ret)
384 goto erase_err;
Huang Shijieb1994892014-02-24 18:37:37 +0800385 }
386 }
387
Brian Norris05241ae2014-11-05 02:29:03 -0800388 write_disable(nor);
389
Huang Shijieb1994892014-02-24 18:37:37 +0800390 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
397erase_err:
398 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
399 instr->state = MTD_ERASE_FAILED;
400 return ret;
401}
402
Brian Norris8cc7f332015-03-13 00:38:39 -0700403static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
Huang Shijieb1994892014-02-24 18:37:37 +0800404{
Brian Norris19763672015-08-13 15:46:05 -0700405 struct mtd_info *mtd = &nor->mtd;
Huang Shijieb1994892014-02-24 18:37:37 +0800406 uint32_t offset = ofs;
407 uint8_t status_old, status_new;
408 int ret = 0;
409
Huang Shijieb1994892014-02-24 18:37:37 +0800410 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 Shijieb1994892014-02-24 18:37:37 +0800432 }
433
Huang Shijieb1994892014-02-24 18:37:37 +0800434 return ret;
435}
436
Brian Norris8cc7f332015-03-13 00:38:39 -0700437static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
Huang Shijieb1994892014-02-24 18:37:37 +0800438{
Brian Norris19763672015-08-13 15:46:05 -0700439 struct mtd_info *mtd = &nor->mtd;
Huang Shijieb1994892014-02-24 18:37:37 +0800440 uint32_t offset = ofs;
441 uint8_t status_old, status_new;
442 int ret = 0;
443
Huang Shijieb1994892014-02-24 18:37:37 +0800444 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 Shijieb1994892014-02-24 18:37:37 +0800466 }
467
Brian Norris8cc7f332015-03-13 00:38:39 -0700468 return ret;
469}
470
471static 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 Shijieb1994892014-02-24 18:37:37 +0800482 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
483 return ret;
484}
485
Brian Norris8cc7f332015-03-13 00:38:39 -0700486static 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 Shijie09ffafb2014-11-06 07:34:01 +0100501/* Used when the "_ext_id" is two bytes at most */
Huang Shijieb1994892014-02-24 18:37:37 +0800502#define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
Huang Shijie09ffafb2014-11-06 07:34:01 +0100503 .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 Shijieb1994892014-02-24 18:37:37 +0800511 .sector_size = (_sector_size), \
512 .n_sectors = (_n_sectors), \
513 .page_size = 256, \
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200514 .flags = (_flags),
Huang Shijieb1994892014-02-24 18:37:37 +0800515
Huang Shijie6d7604e2014-08-12 08:54:56 +0800516#define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
Huang Shijie6d7604e2014-08-12 08:54:56 +0800517 .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łecki06bb6f52015-08-10 21:39:03 +0200529 .flags = (_flags),
Huang Shijie6d7604e2014-08-12 08:54:56 +0800530
Huang Shijieb1994892014-02-24 18:37:37 +0800531#define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
Huang Shijieb1994892014-02-24 18:37:37 +0800532 .sector_size = (_sector_size), \
533 .n_sectors = (_n_sectors), \
534 .page_size = (_page_size), \
535 .addr_width = (_addr_width), \
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200536 .flags = (_flags),
Huang Shijieb1994892014-02-24 18:37:37 +0800537
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łeckic19900e2015-04-25 12:41:30 +0200541 *
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 Shijieb1994892014-02-24 18:37:37 +0800548 */
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200549static const struct flash_info spi_nor_ids[] = {
Huang Shijieb1994892014-02-24 18:37:37 +0800550 /* 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 Ryazanova41595b2014-06-12 18:16:46 +0400571 { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
Huang Shijieb1994892014-02-24 18:37:37 +0800572 { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
Rafał Miłeckic19900e2015-04-25 12:41:30 +0200573 { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800574
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 Lisovyce56ce72014-10-29 10:10:47 +0100582 /* Fujitsu */
583 { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
584
Huang Shijieb1994892014-02-24 18:37:37 +0800585 /* GigaDevice */
586 { "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64, SECT_4K) },
587 { "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128, SECT_4K) },
Rafał Miłeckifcc87a92014-12-16 22:46:56 +0100588 { "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800589
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 Juhosb79c3322015-04-07 19:35:02 +0200595 /* ISSI */
596 { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
597
Huang Shijieb1994892014-02-24 18:37:37 +0800598 /* Macronix */
Gabor Juhos660b5b02015-04-07 19:35:01 +0200599 { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800600 { "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 Westerberg81a12092015-02-05 18:39:03 +0200607 { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800608 { "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)548cd3a2014-12-17 07:35:45 +0000616 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
Aurelien Chanotf9bcb6d2015-10-07 12:10:08 -0700617 { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
Alexey Firago0db7fae2015-06-30 12:53:46 +0300618 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
Mika Westerberg2a06c7b2015-08-27 12:52:19 +0300619 { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000620 { "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 Shijieb1994892014-02-24 18:37:37 +0800626
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 Uytterhoeven9ab86992014-04-22 14:45:32 +0200635 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
Joachim Eastwood0f12a272015-08-14 18:42:32 +0200636 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
Huang Shijieb1994892014-02-24 18:37:37 +0800637 { "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łeckic19900e2015-04-25 12:41:30 +0200643 { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
Jonas Gorskic1752082015-08-26 14:56:53 +0200644 { "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 Shijieb1994892014-02-24 18:37:37 +0800646 { "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 Nyekjaer7c748f52015-10-13 08:50:30 +0200651 { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
Joachim Eastwoodadf508c2015-07-09 22:30:57 +0200652 { "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 Shijieb1994892014-02-24 18:37:37 +0800654 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
Rafał Miłeckic19900e2015-04-25 12:41:30 +0200655 { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
Rafał Miłecki413780d2015-04-25 12:01:35 +0200656 { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
Sean Nyekjaeraada20c2015-10-13 08:51:14 +0200657 { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
Huang Shijieb1994892014-02-24 18:37:37 +0800658
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 Balliera1d97ef2015-08-14 19:35:39 +0200668 { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
Yao Yuanc887be72015-09-16 17:59:45 +0800669 { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800670 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
Harini Katakamf02985b2014-10-21 13:37:59 +0200671 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
Huang Shijieb1994892014-02-24 18:37:37 +0800672
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 Shijieb1994892014-02-24 18:37:37 +0800683
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 Petazzonif2fabe12014-07-27 23:56:08 +0200707 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
Huang Shijieb1994892014-02-24 18:37:37 +0800708
709 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
Gabor Juhos40d19ab2015-03-26 23:58:02 +0100710 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800711 { "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 Westerberge88e5672015-02-05 18:39:02 +0200721 { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K) },
Brian Norris4404bd72015-09-18 15:08:14 -0700722 { "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
Huang Shijieb1994892014-02-24 18:37:37 +0800723 { "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łecki06bb6f52015-08-10 21:39:03 +0200737static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
Huang Shijieb1994892014-02-24 18:37:37 +0800738{
739 int tmp;
Huang Shijie09ffafb2014-11-06 07:34:01 +0100740 u8 id[SPI_NOR_MAX_ID_LEN];
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200741 const struct flash_info *info;
Huang Shijieb1994892014-02-24 18:37:37 +0800742
Huang Shijie09ffafb2014-11-06 07:34:01 +0100743 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
Huang Shijieb1994892014-02-24 18:37:37 +0800744 if (tmp < 0) {
745 dev_dbg(nor->dev, " error %d reading JEDEC ID\n", tmp);
746 return ERR_PTR(tmp);
747 }
Huang Shijieb1994892014-02-24 18:37:37 +0800748
749 for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200750 info = &spi_nor_ids[tmp];
Huang Shijie09ffafb2014-11-06 07:34:01 +0100751 if (info->id_len) {
752 if (!memcmp(info->id, id, info->id_len))
Huang Shijieb1994892014-02-24 18:37:37 +0800753 return &spi_nor_ids[tmp];
754 }
755 }
Huang Shijie09ffafb2014-11-06 07:34:01 +0100756 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %2x, %2x\n",
757 id[0], id[1], id[2]);
Huang Shijieb1994892014-02-24 18:37:37 +0800758 return ERR_PTR(-ENODEV);
759}
760
Huang Shijieb1994892014-02-24 18:37:37 +0800761static 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
779static 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 Shijieb1994892014-02-24 18:37:37 +0800792 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 Norrisb02e7f32014-04-08 18:15:31 -0700799 nor->program_opcode = SPINOR_OP_BP;
Huang Shijieb1994892014-02-24 18:37:37 +0800800
801 /* write one byte. */
802 nor->write(nor, to, 1, retlen, buf);
Brian Norrisb94ed082014-08-06 18:17:00 -0700803 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800804 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 Norrisb02e7f32014-04-08 18:15:31 -0700811 nor->program_opcode = SPINOR_OP_AAI_WP;
Huang Shijieb1994892014-02-24 18:37:37 +0800812
813 /* write two bytes. */
814 nor->write(nor, to, 2, retlen, buf + actual);
Brian Norrisb94ed082014-08-06 18:17:00 -0700815 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800816 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 Norrisb94ed082014-08-06 18:17:00 -0700824 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800825 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 Norrisb02e7f32014-04-08 18:15:31 -0700832 nor->program_opcode = SPINOR_OP_BP;
Huang Shijieb1994892014-02-24 18:37:37 +0800833 nor->write(nor, to, 1, retlen, buf + actual);
834
Brian Norrisb94ed082014-08-06 18:17:00 -0700835 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800836 if (ret)
837 goto time_out;
838 write_disable(nor);
839 }
840time_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 */
850static 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 Shijieb1994892014-02-24 18:37:37 +0800863 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 Norrisb94ed082014-08-06 18:17:00 -0700881 ret = spi_nor_wait_till_ready(nor);
Brian Norris1d61dcb2014-08-06 18:16:56 -0700882 if (ret)
883 goto write_err;
884
Huang Shijieb1994892014-02-24 18:37:37 +0800885 write_enable(nor);
886
887 nor->write(nor, to + i, page_size, retlen, buf + i);
888 }
889 }
890
Brian Norrisdfa9c0c2014-08-06 18:16:57 -0700891 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800892write_err:
893 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
Brian Norris1d61dcb2014-08-06 18:16:56 -0700894 return ret;
Huang Shijieb1994892014-02-24 18:37:37 +0800895}
896
897static 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 Tekifd725232015-08-19 15:26:43 +0530904 write_sr(nor, val | SR_QUAD_EN_MX);
Huang Shijieb1994892014-02-24 18:37:37 +0800905
Brian Norrisb94ed082014-08-06 18:17:00 -0700906 if (spi_nor_wait_till_ready(nor))
Huang Shijieb1994892014-02-24 18:37:37 +0800907 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 */
924static 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 Tekif9f3ce82015-08-19 15:26:44 +0530929 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2);
Huang Shijieb1994892014-02-24 18:37:37 +0800930}
931
932static 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)548cd3a2014-12-17 07:35:45 +0000956static 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 Tekif9f3ce82015-08-19 15:26:44 +0530971 ret = nor->write_reg(nor, SPINOR_OP_WD_EVCR, nor->cmd_buf, 1);
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000972 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łecki06bb6f52015-08-10 21:39:03 +0200995static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info)
Huang Shijieb1994892014-02-24 18:37:37 +0800996{
997 int status;
998
Huang Shijied928a252014-11-06 11:24:33 +0800999 switch (JEDEC_MFR(info)) {
Brian Norrisf0d24482015-09-01 12:57:09 -07001000 case SNOR_MFR_MACRONIX:
Huang Shijieb1994892014-02-24 18:37:37 +08001001 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 Norrisf0d24482015-09-01 12:57:09 -07001007 case SNOR_MFR_MICRON:
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +00001008 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 Shijieb1994892014-02-24 18:37:37 +08001014 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
1024static 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 Shijieb1994892014-02-24 18:37:37 +08001032 return 0;
1033}
1034
Ben Hutchings70f3ce02014-09-29 11:47:54 +02001035int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
Huang Shijieb1994892014-02-24 18:37:37 +08001036{
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001037 const struct flash_info *info = NULL;
Huang Shijieb1994892014-02-24 18:37:37 +08001038 struct device *dev = nor->dev;
Brian Norris19763672015-08-13 15:46:05 -07001039 struct mtd_info *mtd = &nor->mtd;
Marek Vasut11bff0b2015-09-03 18:35:36 +02001040 struct device_node *np = nor->flash_node;
Huang Shijieb1994892014-02-24 18:37:37 +08001041 int ret;
1042 int i;
1043
1044 ret = spi_nor_check(nor);
1045 if (ret)
1046 return ret;
1047
Brian Norris43163022015-05-19 14:38:22 -07001048 if (name)
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001049 info = spi_nor_match_id(name);
Brian Norris43163022015-05-19 14:38:22 -07001050 /* Try to auto-detect if chip name wasn't specified or not found */
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001051 if (!info)
1052 info = spi_nor_read_id(nor);
1053 if (IS_ERR_OR_NULL(info))
Ben Hutchings70f3ce02014-09-29 11:47:54 +02001054 return -ENOENT;
1055
Rafał Miłecki58c81952014-12-01 09:42:16 +01001056 /*
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łecki06bb6f52015-08-10 21:39:03 +02001061 const struct flash_info *jinfo;
Huang Shijieb1994892014-02-24 18:37:37 +08001062
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001063 jinfo = spi_nor_read_id(nor);
1064 if (IS_ERR(jinfo)) {
1065 return PTR_ERR(jinfo);
1066 } else if (jinfo != info) {
Huang Shijieb1994892014-02-24 18:37:37 +08001067 /*
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łecki06bb6f52015-08-10 21:39:03 +02001075 jinfo->name, info->name);
1076 info = jinfo;
Huang Shijieb1994892014-02-24 18:37:37 +08001077 }
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 Norrisf0d24482015-09-01 12:57:09 -07001087 if (JEDEC_MFR(info) == SNOR_MFR_ATMEL ||
1088 JEDEC_MFR(info) == SNOR_MFR_INTEL ||
1089 JEDEC_MFR(info) == SNOR_MFR_SST) {
Huang Shijieb1994892014-02-24 18:37:37 +08001090 write_enable(nor);
1091 write_sr(nor, 0);
1092 }
1093
Rafał Miłecki32f1b7c2014-09-28 22:36:54 +02001094 if (!mtd->name)
Huang Shijieb1994892014-02-24 18:37:37 +08001095 mtd->name = dev_name(dev);
Brian Norrisc9ec3902015-08-13 15:46:03 -07001096 mtd->priv = nor;
Huang Shijieb1994892014-02-24 18:37:37 +08001097 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 Norrisf0d24482015-09-01 12:57:09 -07001104 /* NOR protection support for STmicro/Micron chips */
1105 if (JEDEC_MFR(info) == SNOR_MFR_MICRON) {
Brian Norris8cc7f332015-03-13 00:38:39 -07001106 nor->flash_lock = stm_lock;
1107 nor->flash_unlock = stm_unlock;
1108 }
1109
1110 if (nor->flash_lock && nor->flash_unlock) {
Huang Shijieb1994892014-02-24 18:37:37 +08001111 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 Norris51983b72014-09-10 00:26:16 -07001121 if (info->flags & USE_FSR)
1122 nor->flags |= SNOR_F_USE_FSR;
grmoore@altera.comc14dedd2014-04-29 10:29:51 -05001123
Rafał Miłecki57cf26c2014-08-17 11:27:26 +02001124#ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
Huang Shijieb1994892014-02-24 18:37:37 +08001125 /* prefer "small sector" erase if possible */
1126 if (info->flags & SECT_4K) {
Brian Norrisb02e7f32014-04-08 18:15:31 -07001127 nor->erase_opcode = SPINOR_OP_BE_4K;
Huang Shijieb1994892014-02-24 18:37:37 +08001128 mtd->erasesize = 4096;
1129 } else if (info->flags & SECT_4K_PMC) {
Brian Norrisb02e7f32014-04-08 18:15:31 -07001130 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
Huang Shijieb1994892014-02-24 18:37:37 +08001131 mtd->erasesize = 4096;
Rafał Miłecki57cf26c2014-08-17 11:27:26 +02001132 } else
1133#endif
1134 {
Brian Norrisb02e7f32014-04-08 18:15:31 -07001135 nor->erase_opcode = SPINOR_OP_SE;
Huang Shijieb1994892014-02-24 18:37:37 +08001136 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 Shijied928a252014-11-06 11:24:33 +08001163 ret = set_quad_mode(nor, info);
Huang Shijieb1994892014-02-24 18:37:37 +08001164 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 Norris58b89a12014-04-08 19:16:49 -07001176 nor->read_opcode = SPINOR_OP_READ_1_1_4;
Huang Shijieb1994892014-02-24 18:37:37 +08001177 break;
1178 case SPI_NOR_DUAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001179 nor->read_opcode = SPINOR_OP_READ_1_1_2;
Huang Shijieb1994892014-02-24 18:37:37 +08001180 break;
1181 case SPI_NOR_FAST:
Brian Norris58b89a12014-04-08 19:16:49 -07001182 nor->read_opcode = SPINOR_OP_READ_FAST;
Huang Shijieb1994892014-02-24 18:37:37 +08001183 break;
1184 case SPI_NOR_NORMAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001185 nor->read_opcode = SPINOR_OP_READ;
Huang Shijieb1994892014-02-24 18:37:37 +08001186 break;
1187 default:
1188 dev_err(dev, "No Read opcode defined\n");
1189 return -EINVAL;
1190 }
1191
Brian Norrisb02e7f32014-04-08 18:15:31 -07001192 nor->program_opcode = SPINOR_OP_PP;
Huang Shijieb1994892014-02-24 18:37:37 +08001193
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 Norrisf0d24482015-09-01 12:57:09 -07001199 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION) {
Huang Shijieb1994892014-02-24 18:37:37 +08001200 /* Dedicated 4-byte command set */
1201 switch (nor->flash_read) {
1202 case SPI_NOR_QUAD:
Brian Norris58b89a12014-04-08 19:16:49 -07001203 nor->read_opcode = SPINOR_OP_READ4_1_1_4;
Huang Shijieb1994892014-02-24 18:37:37 +08001204 break;
1205 case SPI_NOR_DUAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001206 nor->read_opcode = SPINOR_OP_READ4_1_1_2;
Huang Shijieb1994892014-02-24 18:37:37 +08001207 break;
1208 case SPI_NOR_FAST:
Brian Norris58b89a12014-04-08 19:16:49 -07001209 nor->read_opcode = SPINOR_OP_READ4_FAST;
Huang Shijieb1994892014-02-24 18:37:37 +08001210 break;
1211 case SPI_NOR_NORMAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001212 nor->read_opcode = SPINOR_OP_READ4;
Huang Shijieb1994892014-02-24 18:37:37 +08001213 break;
1214 }
Brian Norrisb02e7f32014-04-08 18:15:31 -07001215 nor->program_opcode = SPINOR_OP_PP_4B;
Huang Shijieb1994892014-02-24 18:37:37 +08001216 /* No small sector erase for 4-byte command set */
Brian Norrisb02e7f32014-04-08 18:15:31 -07001217 nor->erase_opcode = SPINOR_OP_SE_4B;
Huang Shijieb1994892014-02-24 18:37:37 +08001218 mtd->erasesize = info->sector_size;
1219 } else
Huang Shijied928a252014-11-06 11:24:33 +08001220 set_4byte(nor, info, 1);
Huang Shijieb1994892014-02-24 18:37:37 +08001221 } else {
1222 nor->addr_width = 3;
1223 }
1224
1225 nor->read_dummy = spi_nor_read_dummy_cycles(nor);
1226
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001227 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
Huang Shijieb1994892014-02-24 18:37:37 +08001228 (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 Norrisb61834b2014-04-08 18:22:57 -07001248EXPORT_SYMBOL_GPL(spi_nor_scan);
Huang Shijieb1994892014-02-24 18:37:37 +08001249
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001250static const struct flash_info *spi_nor_match_id(const char *name)
Huang Shijie0d8c11c2014-02-24 18:37:40 +08001251{
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001252 const struct flash_info *id = spi_nor_ids;
Huang Shijie0d8c11c2014-02-24 18:37:40 +08001253
Brian Norris2ff46e62015-09-02 16:34:35 -07001254 while (id->name) {
Huang Shijie0d8c11c2014-02-24 18:37:40 +08001255 if (!strcmp(name, id->name))
1256 return id;
1257 id++;
1258 }
1259 return NULL;
1260}
1261
Huang Shijieb1994892014-02-24 18:37:37 +08001262MODULE_LICENSE("GPL");
1263MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
1264MODULE_AUTHOR("Mike Lavender");
1265MODULE_DESCRIPTION("framework for SPI NOR");