blob: 11c882a5ab219f47d3a562334e97023ecd45ac59 [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>
19
20#include <linux/mtd/cfi.h>
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. */
27#define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
28
Huang Shijied928a252014-11-06 11:24:33 +080029#define SPI_NOR_MAX_ID_LEN 6
30
31struct flash_info {
Rafał Miłecki06bb6f52015-08-10 21:39:03 +020032 char *name;
33
Huang Shijied928a252014-11-06 11:24:33 +080034 /*
35 * This array stores the ID bytes.
36 * The first three bytes are the JEDIC ID.
37 * JEDEC ID zero means "no ID" (mostly older chips).
38 */
39 u8 id[SPI_NOR_MAX_ID_LEN];
40 u8 id_len;
41
42 /* The size listed here is what works with SPINOR_OP_SE, which isn't
43 * necessarily called a "sector" by the vendor.
44 */
45 unsigned sector_size;
46 u16 n_sectors;
47
48 u16 page_size;
49 u16 addr_width;
50
51 u16 flags;
52#define SECT_4K 0x01 /* SPINOR_OP_BE_4K works uniformly */
53#define SPI_NOR_NO_ERASE 0x02 /* No erase command needed */
54#define SST_WRITE 0x04 /* use SST byte programming */
55#define SPI_NOR_NO_FR 0x08 /* Can't do fastread */
56#define SECT_4K_PMC 0x10 /* SPINOR_OP_BE_4K_PMC works uniformly */
57#define SPI_NOR_DUAL_READ 0x20 /* Flash supports Dual Read */
58#define SPI_NOR_QUAD_READ 0x40 /* Flash supports Quad Read */
59#define USE_FSR 0x80 /* use flag status register */
60};
61
62#define JEDEC_MFR(info) ((info)->id[0])
Huang Shijieb1994892014-02-24 18:37:37 +080063
Rafał Miłecki06bb6f52015-08-10 21:39:03 +020064static const struct flash_info *spi_nor_match_id(const char *name);
Ben Hutchings70f3ce02014-09-29 11:47:54 +020065
Huang Shijieb1994892014-02-24 18:37:37 +080066/*
67 * Read the status register, returning its value in the location
68 * Return the status register value.
69 * Returns negative if error occurred.
70 */
71static int read_sr(struct spi_nor *nor)
72{
73 int ret;
74 u8 val;
75
Brian Norrisb02e7f32014-04-08 18:15:31 -070076 ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
Huang Shijieb1994892014-02-24 18:37:37 +080077 if (ret < 0) {
78 pr_err("error %d reading SR\n", (int) ret);
79 return ret;
80 }
81
82 return val;
83}
84
85/*
grmoore@altera.comc14dedd2014-04-29 10:29:51 -050086 * Read the flag status register, returning its value in the location
87 * Return the status register value.
88 * Returns negative if error occurred.
89 */
90static int read_fsr(struct spi_nor *nor)
91{
92 int ret;
93 u8 val;
94
95 ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
96 if (ret < 0) {
97 pr_err("error %d reading FSR\n", ret);
98 return ret;
99 }
100
101 return val;
102}
103
104/*
Huang Shijieb1994892014-02-24 18:37:37 +0800105 * Read configuration register, returning its value in the
106 * location. Return the configuration register value.
107 * Returns negative if error occured.
108 */
109static int read_cr(struct spi_nor *nor)
110{
111 int ret;
112 u8 val;
113
Brian Norrisb02e7f32014-04-08 18:15:31 -0700114 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
Huang Shijieb1994892014-02-24 18:37:37 +0800115 if (ret < 0) {
116 dev_err(nor->dev, "error %d reading CR\n", ret);
117 return ret;
118 }
119
120 return val;
121}
122
123/*
124 * Dummy Cycle calculation for different type of read.
125 * It can be used to support more commands with
126 * different dummy cycle requirements.
127 */
128static inline int spi_nor_read_dummy_cycles(struct spi_nor *nor)
129{
130 switch (nor->flash_read) {
131 case SPI_NOR_FAST:
132 case SPI_NOR_DUAL:
133 case SPI_NOR_QUAD:
Huang Shijie0b78a2c2014-04-28 11:53:38 +0800134 return 8;
Huang Shijieb1994892014-02-24 18:37:37 +0800135 case SPI_NOR_NORMAL:
136 return 0;
137 }
138 return 0;
139}
140
141/*
142 * Write status register 1 byte
143 * Returns negative if error occurred.
144 */
145static inline int write_sr(struct spi_nor *nor, u8 val)
146{
147 nor->cmd_buf[0] = val;
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530148 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
Huang Shijieb1994892014-02-24 18:37:37 +0800149}
150
151/*
152 * Set write enable latch with Write Enable command.
153 * Returns negative if error occurred.
154 */
155static inline int write_enable(struct spi_nor *nor)
156{
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530157 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800158}
159
160/*
161 * Send write disble instruction to the chip.
162 */
163static inline int write_disable(struct spi_nor *nor)
164{
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530165 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800166}
167
168static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
169{
170 return mtd->priv;
171}
172
173/* Enable/disable 4-byte addressing mode. */
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200174static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
Huang Shijied928a252014-11-06 11:24:33 +0800175 int enable)
Huang Shijieb1994892014-02-24 18:37:37 +0800176{
177 int status;
178 bool need_wren = false;
179 u8 cmd;
180
Huang Shijied928a252014-11-06 11:24:33 +0800181 switch (JEDEC_MFR(info)) {
Huang Shijieb1994892014-02-24 18:37:37 +0800182 case CFI_MFR_ST: /* Micron, actually */
183 /* Some Micron need WREN command; all will accept it */
184 need_wren = true;
185 case CFI_MFR_MACRONIX:
186 case 0xEF /* winbond */:
187 if (need_wren)
188 write_enable(nor);
189
Brian Norrisb02e7f32014-04-08 18:15:31 -0700190 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530191 status = nor->write_reg(nor, cmd, NULL, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800192 if (need_wren)
193 write_disable(nor);
194
195 return status;
196 default:
197 /* Spansion style */
198 nor->cmd_buf[0] = enable << 7;
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530199 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
Huang Shijieb1994892014-02-24 18:37:37 +0800200 }
201}
Brian Norris51983b72014-09-10 00:26:16 -0700202static inline int spi_nor_sr_ready(struct spi_nor *nor)
203{
204 int sr = read_sr(nor);
205 if (sr < 0)
206 return sr;
207 else
208 return !(sr & SR_WIP);
209}
210
211static inline int spi_nor_fsr_ready(struct spi_nor *nor)
212{
213 int fsr = read_fsr(nor);
214 if (fsr < 0)
215 return fsr;
216 else
217 return fsr & FSR_READY;
218}
219
220static int spi_nor_ready(struct spi_nor *nor)
221{
222 int sr, fsr;
223 sr = spi_nor_sr_ready(nor);
224 if (sr < 0)
225 return sr;
226 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
227 if (fsr < 0)
228 return fsr;
229 return sr && fsr;
230}
Huang Shijieb1994892014-02-24 18:37:37 +0800231
Brian Norrisb94ed082014-08-06 18:17:00 -0700232/*
233 * Service routine to read status register until ready, or timeout occurs.
234 * Returns non-zero if error.
235 */
Huang Shijieb1994892014-02-24 18:37:37 +0800236static int spi_nor_wait_till_ready(struct spi_nor *nor)
237{
238 unsigned long deadline;
Brian Norrisa95ce922014-11-05 02:32:03 -0800239 int timeout = 0, ret;
Huang Shijieb1994892014-02-24 18:37:37 +0800240
241 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
242
Brian Norrisa95ce922014-11-05 02:32:03 -0800243 while (!timeout) {
244 if (time_after_eq(jiffies, deadline))
245 timeout = 1;
Huang Shijieb1994892014-02-24 18:37:37 +0800246
Brian Norris51983b72014-09-10 00:26:16 -0700247 ret = spi_nor_ready(nor);
248 if (ret < 0)
249 return ret;
250 if (ret)
Huang Shijieb1994892014-02-24 18:37:37 +0800251 return 0;
Brian Norrisa95ce922014-11-05 02:32:03 -0800252
253 cond_resched();
254 }
255
256 dev_err(nor->dev, "flash operation timed out\n");
Huang Shijieb1994892014-02-24 18:37:37 +0800257
258 return -ETIMEDOUT;
259}
260
261/*
Huang Shijieb1994892014-02-24 18:37:37 +0800262 * Erase the whole flash memory
263 *
264 * Returns 0 if successful, non-zero otherwise.
265 */
266static int erase_chip(struct spi_nor *nor)
267{
Brian Norris19763672015-08-13 15:46:05 -0700268 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
Huang Shijieb1994892014-02-24 18:37:37 +0800269
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530270 return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800271}
272
273static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
274{
275 int ret = 0;
276
277 mutex_lock(&nor->lock);
278
279 if (nor->prepare) {
280 ret = nor->prepare(nor, ops);
281 if (ret) {
282 dev_err(nor->dev, "failed in the preparation.\n");
283 mutex_unlock(&nor->lock);
284 return ret;
285 }
286 }
287 return ret;
288}
289
290static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
291{
292 if (nor->unprepare)
293 nor->unprepare(nor, ops);
294 mutex_unlock(&nor->lock);
295}
296
297/*
298 * Erase an address range on the nor chip. The address range may extend
299 * one or more erase sectors. Return an error is there is a problem erasing.
300 */
301static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
302{
303 struct spi_nor *nor = mtd_to_spi_nor(mtd);
304 u32 addr, len;
305 uint32_t rem;
306 int ret;
307
308 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
309 (long long)instr->len);
310
311 div_u64_rem(instr->len, mtd->erasesize, &rem);
312 if (rem)
313 return -EINVAL;
314
315 addr = instr->addr;
316 len = instr->len;
317
318 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
319 if (ret)
320 return ret;
321
322 /* whole-chip erase? */
323 if (len == mtd->size) {
Brian Norris05241ae2014-11-05 02:29:03 -0800324 write_enable(nor);
325
Huang Shijieb1994892014-02-24 18:37:37 +0800326 if (erase_chip(nor)) {
327 ret = -EIO;
328 goto erase_err;
329 }
330
Brian Norrisdfa9c0c2014-08-06 18:16:57 -0700331 ret = spi_nor_wait_till_ready(nor);
332 if (ret)
333 goto erase_err;
334
Huang Shijieb1994892014-02-24 18:37:37 +0800335 /* REVISIT in some cases we could speed up erasing large regions
Brian Norrisb02e7f32014-04-08 18:15:31 -0700336 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
Huang Shijieb1994892014-02-24 18:37:37 +0800337 * to use "small sector erase", but that's not always optimal.
338 */
339
340 /* "sector"-at-a-time erase */
341 } else {
342 while (len) {
Brian Norris05241ae2014-11-05 02:29:03 -0800343 write_enable(nor);
344
Huang Shijieb1994892014-02-24 18:37:37 +0800345 if (nor->erase(nor, addr)) {
346 ret = -EIO;
347 goto erase_err;
348 }
349
350 addr += mtd->erasesize;
351 len -= mtd->erasesize;
Brian Norrisdfa9c0c2014-08-06 18:16:57 -0700352
353 ret = spi_nor_wait_till_ready(nor);
354 if (ret)
355 goto erase_err;
Huang Shijieb1994892014-02-24 18:37:37 +0800356 }
357 }
358
Brian Norris05241ae2014-11-05 02:29:03 -0800359 write_disable(nor);
360
Huang Shijieb1994892014-02-24 18:37:37 +0800361 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
362
363 instr->state = MTD_ERASE_DONE;
364 mtd_erase_callback(instr);
365
366 return ret;
367
368erase_err:
369 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
370 instr->state = MTD_ERASE_FAILED;
371 return ret;
372}
373
Brian Norris8cc7f332015-03-13 00:38:39 -0700374static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
Huang Shijieb1994892014-02-24 18:37:37 +0800375{
Brian Norris19763672015-08-13 15:46:05 -0700376 struct mtd_info *mtd = &nor->mtd;
Huang Shijieb1994892014-02-24 18:37:37 +0800377 uint32_t offset = ofs;
378 uint8_t status_old, status_new;
379 int ret = 0;
380
Huang Shijieb1994892014-02-24 18:37:37 +0800381 status_old = read_sr(nor);
382
383 if (offset < mtd->size - (mtd->size / 2))
384 status_new = status_old | SR_BP2 | SR_BP1 | SR_BP0;
385 else if (offset < mtd->size - (mtd->size / 4))
386 status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
387 else if (offset < mtd->size - (mtd->size / 8))
388 status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
389 else if (offset < mtd->size - (mtd->size / 16))
390 status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
391 else if (offset < mtd->size - (mtd->size / 32))
392 status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
393 else if (offset < mtd->size - (mtd->size / 64))
394 status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
395 else
396 status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
397
398 /* Only modify protection if it will not unlock other areas */
399 if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) >
400 (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
401 write_enable(nor);
402 ret = write_sr(nor, status_new);
Huang Shijieb1994892014-02-24 18:37:37 +0800403 }
404
Huang Shijieb1994892014-02-24 18:37:37 +0800405 return ret;
406}
407
Brian Norris8cc7f332015-03-13 00:38:39 -0700408static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
Huang Shijieb1994892014-02-24 18:37:37 +0800409{
Brian Norris19763672015-08-13 15:46:05 -0700410 struct mtd_info *mtd = &nor->mtd;
Huang Shijieb1994892014-02-24 18:37:37 +0800411 uint32_t offset = ofs;
412 uint8_t status_old, status_new;
413 int ret = 0;
414
Huang Shijieb1994892014-02-24 18:37:37 +0800415 status_old = read_sr(nor);
416
417 if (offset+len > mtd->size - (mtd->size / 64))
418 status_new = status_old & ~(SR_BP2 | SR_BP1 | SR_BP0);
419 else if (offset+len > mtd->size - (mtd->size / 32))
420 status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
421 else if (offset+len > mtd->size - (mtd->size / 16))
422 status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
423 else if (offset+len > mtd->size - (mtd->size / 8))
424 status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
425 else if (offset+len > mtd->size - (mtd->size / 4))
426 status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
427 else if (offset+len > mtd->size - (mtd->size / 2))
428 status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
429 else
430 status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
431
432 /* Only modify protection if it will not lock other areas */
433 if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) <
434 (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
435 write_enable(nor);
436 ret = write_sr(nor, status_new);
Huang Shijieb1994892014-02-24 18:37:37 +0800437 }
438
Brian Norris8cc7f332015-03-13 00:38:39 -0700439 return ret;
440}
441
442static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
443{
444 struct spi_nor *nor = mtd_to_spi_nor(mtd);
445 int ret;
446
447 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
448 if (ret)
449 return ret;
450
451 ret = nor->flash_lock(nor, ofs, len);
452
Huang Shijieb1994892014-02-24 18:37:37 +0800453 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
454 return ret;
455}
456
Brian Norris8cc7f332015-03-13 00:38:39 -0700457static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
458{
459 struct spi_nor *nor = mtd_to_spi_nor(mtd);
460 int ret;
461
462 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
463 if (ret)
464 return ret;
465
466 ret = nor->flash_unlock(nor, ofs, len);
467
468 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
469 return ret;
470}
471
Huang Shijie09ffafb2014-11-06 07:34:01 +0100472/* Used when the "_ext_id" is two bytes at most */
Huang Shijieb1994892014-02-24 18:37:37 +0800473#define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
Huang Shijie09ffafb2014-11-06 07:34:01 +0100474 .id = { \
475 ((_jedec_id) >> 16) & 0xff, \
476 ((_jedec_id) >> 8) & 0xff, \
477 (_jedec_id) & 0xff, \
478 ((_ext_id) >> 8) & 0xff, \
479 (_ext_id) & 0xff, \
480 }, \
481 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
Huang Shijieb1994892014-02-24 18:37:37 +0800482 .sector_size = (_sector_size), \
483 .n_sectors = (_n_sectors), \
484 .page_size = 256, \
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200485 .flags = (_flags),
Huang Shijieb1994892014-02-24 18:37:37 +0800486
Huang Shijie6d7604e2014-08-12 08:54:56 +0800487#define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
Huang Shijie6d7604e2014-08-12 08:54:56 +0800488 .id = { \
489 ((_jedec_id) >> 16) & 0xff, \
490 ((_jedec_id) >> 8) & 0xff, \
491 (_jedec_id) & 0xff, \
492 ((_ext_id) >> 16) & 0xff, \
493 ((_ext_id) >> 8) & 0xff, \
494 (_ext_id) & 0xff, \
495 }, \
496 .id_len = 6, \
497 .sector_size = (_sector_size), \
498 .n_sectors = (_n_sectors), \
499 .page_size = 256, \
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200500 .flags = (_flags),
Huang Shijie6d7604e2014-08-12 08:54:56 +0800501
Huang Shijieb1994892014-02-24 18:37:37 +0800502#define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
Huang Shijieb1994892014-02-24 18:37:37 +0800503 .sector_size = (_sector_size), \
504 .n_sectors = (_n_sectors), \
505 .page_size = (_page_size), \
506 .addr_width = (_addr_width), \
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200507 .flags = (_flags),
Huang Shijieb1994892014-02-24 18:37:37 +0800508
509/* NOTE: double check command sets and memory organization when you add
510 * more nor chips. This current list focusses on newer chips, which
511 * have been converging on command sets which including JEDEC ID.
Rafał Miłeckic19900e2015-04-25 12:41:30 +0200512 *
513 * All newly added entries should describe *hardware* and should use SECT_4K
514 * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
515 * scenarios excluding small sectors there is config option that can be
516 * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
517 * For historical (and compatibility) reasons (before we got above config) some
518 * old entries may be missing 4K flag.
Huang Shijieb1994892014-02-24 18:37:37 +0800519 */
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200520static const struct flash_info spi_nor_ids[] = {
Huang Shijieb1994892014-02-24 18:37:37 +0800521 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
522 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
523 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
524
525 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
526 { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
527 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
528
529 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
530 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
531 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
532 { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
533
534 { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
535
536 /* EON -- en25xxx */
537 { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
538 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
539 { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
540 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
541 { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
Sergey Ryazanova41595b2014-06-12 18:16:46 +0400542 { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
Huang Shijieb1994892014-02-24 18:37:37 +0800543 { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
Rafał Miłeckic19900e2015-04-25 12:41:30 +0200544 { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800545
546 /* ESMT */
547 { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K) },
548
549 /* Everspin */
550 { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
551 { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
552
Rostislav Lisovyce56ce72014-10-29 10:10:47 +0100553 /* Fujitsu */
554 { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
555
Huang Shijieb1994892014-02-24 18:37:37 +0800556 /* GigaDevice */
557 { "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64, SECT_4K) },
558 { "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128, SECT_4K) },
Rafał Miłeckifcc87a92014-12-16 22:46:56 +0100559 { "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800560
561 /* Intel/Numonyx -- xxxs33b */
562 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
563 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
564 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
565
Gabor Juhosb79c3322015-04-07 19:35:02 +0200566 /* ISSI */
567 { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
568
Huang Shijieb1994892014-02-24 18:37:37 +0800569 /* Macronix */
Gabor Juhos660b5b02015-04-07 19:35:01 +0200570 { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800571 { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
572 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
573 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
574 { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
575 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
576 { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
577 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
Mika Westerberg81a12092015-02-05 18:39:03 +0200578 { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800579 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
580 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
581 { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
582 { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
583 { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_QUAD_READ) },
584 { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
585
586 /* Micron */
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000587 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
Alexey Firago0db7fae2015-06-30 12:53:46 +0300588 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
Mika Westerberg2a06c7b2015-08-27 12:52:19 +0300589 { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000590 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
591 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
592 { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
593 { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
594 { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
595 { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
Huang Shijieb1994892014-02-24 18:37:37 +0800596
597 /* PMC */
598 { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
599 { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
600 { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
601
602 /* Spansion -- single (large) sector size only, at least
603 * for the chips listed here (without boot sectors).
604 */
Geert Uytterhoeven9ab86992014-04-22 14:45:32 +0200605 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
Joachim Eastwood0f12a272015-08-14 18:42:32 +0200606 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
Huang Shijieb1994892014-02-24 18:37:37 +0800607 { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
608 { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
609 { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
610 { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
611 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
612 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
Rafał Miłeckic19900e2015-04-25 12:41:30 +0200613 { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
Jonas Gorskic1752082015-08-26 14:56:53 +0200614 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
615 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
Huang Shijieb1994892014-02-24 18:37:37 +0800616 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
617 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
618 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
619 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
620 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
Joachim Eastwoodadf508c2015-07-09 22:30:57 +0200621 { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
622 { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
Huang Shijieb1994892014-02-24 18:37:37 +0800623 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
Rafał Miłeckic19900e2015-04-25 12:41:30 +0200624 { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
Rafał Miłecki413780d2015-04-25 12:01:35 +0200625 { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
Antony Pavlovb4d97f02015-06-16 11:37:04 +0300626 { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800627
628 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
629 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
630 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
631 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
632 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
633 { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
634 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
635 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
636 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
Alexis Balliera1d97ef2015-08-14 19:35:39 +0200637 { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800638 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
Harini Katakamf02985b2014-10-21 13:37:59 +0200639 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
Huang Shijieb1994892014-02-24 18:37:37 +0800640
641 /* ST Microelectronics -- newer production may have feature updates */
642 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
643 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
644 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
645 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
646 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
647 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
648 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
649 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
650 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
Huang Shijieb1994892014-02-24 18:37:37 +0800651
652 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
653 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
654 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
655 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
656 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
657 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
658 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
659 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
660 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
661
662 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
663 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
664 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
665
666 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
667 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
668 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
669
670 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
671 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
672 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
673 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
674 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
Thomas Petazzonif2fabe12014-07-27 23:56:08 +0200675 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
Huang Shijieb1994892014-02-24 18:37:37 +0800676
677 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
Gabor Juhos40d19ab2015-03-26 23:58:02 +0100678 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800679 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
680 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
681 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
682 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
683 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
684 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
685 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
686 { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K) },
687 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
688 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
Mika Westerberge88e5672015-02-05 18:39:02 +0200689 { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800690 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
691 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
692 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
693 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) },
694
695 /* Catalyst / On Semiconductor -- non-JEDEC */
696 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
697 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
698 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
699 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
700 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
701 { },
702};
703
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200704static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
Huang Shijieb1994892014-02-24 18:37:37 +0800705{
706 int tmp;
Huang Shijie09ffafb2014-11-06 07:34:01 +0100707 u8 id[SPI_NOR_MAX_ID_LEN];
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200708 const struct flash_info *info;
Huang Shijieb1994892014-02-24 18:37:37 +0800709
Huang Shijie09ffafb2014-11-06 07:34:01 +0100710 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
Huang Shijieb1994892014-02-24 18:37:37 +0800711 if (tmp < 0) {
712 dev_dbg(nor->dev, " error %d reading JEDEC ID\n", tmp);
713 return ERR_PTR(tmp);
714 }
Huang Shijieb1994892014-02-24 18:37:37 +0800715
716 for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200717 info = &spi_nor_ids[tmp];
Huang Shijie09ffafb2014-11-06 07:34:01 +0100718 if (info->id_len) {
719 if (!memcmp(info->id, id, info->id_len))
Huang Shijieb1994892014-02-24 18:37:37 +0800720 return &spi_nor_ids[tmp];
721 }
722 }
Huang Shijie09ffafb2014-11-06 07:34:01 +0100723 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %2x, %2x\n",
724 id[0], id[1], id[2]);
Huang Shijieb1994892014-02-24 18:37:37 +0800725 return ERR_PTR(-ENODEV);
726}
727
Huang Shijieb1994892014-02-24 18:37:37 +0800728static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
729 size_t *retlen, u_char *buf)
730{
731 struct spi_nor *nor = mtd_to_spi_nor(mtd);
732 int ret;
733
734 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
735
736 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
737 if (ret)
738 return ret;
739
740 ret = nor->read(nor, from, len, retlen, buf);
741
742 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
743 return ret;
744}
745
746static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
747 size_t *retlen, const u_char *buf)
748{
749 struct spi_nor *nor = mtd_to_spi_nor(mtd);
750 size_t actual;
751 int ret;
752
753 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
754
755 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
756 if (ret)
757 return ret;
758
Huang Shijieb1994892014-02-24 18:37:37 +0800759 write_enable(nor);
760
761 nor->sst_write_second = false;
762
763 actual = to % 2;
764 /* Start write from odd address. */
765 if (actual) {
Brian Norrisb02e7f32014-04-08 18:15:31 -0700766 nor->program_opcode = SPINOR_OP_BP;
Huang Shijieb1994892014-02-24 18:37:37 +0800767
768 /* write one byte. */
769 nor->write(nor, to, 1, retlen, buf);
Brian Norrisb94ed082014-08-06 18:17:00 -0700770 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800771 if (ret)
772 goto time_out;
773 }
774 to += actual;
775
776 /* Write out most of the data here. */
777 for (; actual < len - 1; actual += 2) {
Brian Norrisb02e7f32014-04-08 18:15:31 -0700778 nor->program_opcode = SPINOR_OP_AAI_WP;
Huang Shijieb1994892014-02-24 18:37:37 +0800779
780 /* write two bytes. */
781 nor->write(nor, to, 2, retlen, buf + actual);
Brian Norrisb94ed082014-08-06 18:17:00 -0700782 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800783 if (ret)
784 goto time_out;
785 to += 2;
786 nor->sst_write_second = true;
787 }
788 nor->sst_write_second = false;
789
790 write_disable(nor);
Brian Norrisb94ed082014-08-06 18:17:00 -0700791 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800792 if (ret)
793 goto time_out;
794
795 /* Write out trailing byte if it exists. */
796 if (actual != len) {
797 write_enable(nor);
798
Brian Norrisb02e7f32014-04-08 18:15:31 -0700799 nor->program_opcode = SPINOR_OP_BP;
Huang Shijieb1994892014-02-24 18:37:37 +0800800 nor->write(nor, to, 1, retlen, buf + actual);
801
Brian Norrisb94ed082014-08-06 18:17:00 -0700802 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800803 if (ret)
804 goto time_out;
805 write_disable(nor);
806 }
807time_out:
808 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
809 return ret;
810}
811
812/*
813 * Write an address range to the nor chip. Data must be written in
814 * FLASH_PAGESIZE chunks. The address range may be any size provided
815 * it is within the physical boundaries.
816 */
817static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
818 size_t *retlen, const u_char *buf)
819{
820 struct spi_nor *nor = mtd_to_spi_nor(mtd);
821 u32 page_offset, page_size, i;
822 int ret;
823
824 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
825
826 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
827 if (ret)
828 return ret;
829
Huang Shijieb1994892014-02-24 18:37:37 +0800830 write_enable(nor);
831
832 page_offset = to & (nor->page_size - 1);
833
834 /* do all the bytes fit onto one page? */
835 if (page_offset + len <= nor->page_size) {
836 nor->write(nor, to, len, retlen, buf);
837 } else {
838 /* the size of data remaining on the first page */
839 page_size = nor->page_size - page_offset;
840 nor->write(nor, to, page_size, retlen, buf);
841
842 /* write everything in nor->page_size chunks */
843 for (i = page_size; i < len; i += page_size) {
844 page_size = len - i;
845 if (page_size > nor->page_size)
846 page_size = nor->page_size;
847
Brian Norrisb94ed082014-08-06 18:17:00 -0700848 ret = spi_nor_wait_till_ready(nor);
Brian Norris1d61dcb2014-08-06 18:16:56 -0700849 if (ret)
850 goto write_err;
851
Huang Shijieb1994892014-02-24 18:37:37 +0800852 write_enable(nor);
853
854 nor->write(nor, to + i, page_size, retlen, buf + i);
855 }
856 }
857
Brian Norrisdfa9c0c2014-08-06 18:16:57 -0700858 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800859write_err:
860 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
Brian Norris1d61dcb2014-08-06 18:16:56 -0700861 return ret;
Huang Shijieb1994892014-02-24 18:37:37 +0800862}
863
864static int macronix_quad_enable(struct spi_nor *nor)
865{
866 int ret, val;
867
868 val = read_sr(nor);
869 write_enable(nor);
870
Jagan Tekifd725232015-08-19 15:26:43 +0530871 write_sr(nor, val | SR_QUAD_EN_MX);
Huang Shijieb1994892014-02-24 18:37:37 +0800872
Brian Norrisb94ed082014-08-06 18:17:00 -0700873 if (spi_nor_wait_till_ready(nor))
Huang Shijieb1994892014-02-24 18:37:37 +0800874 return 1;
875
876 ret = read_sr(nor);
877 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
878 dev_err(nor->dev, "Macronix Quad bit not set\n");
879 return -EINVAL;
880 }
881
882 return 0;
883}
884
885/*
886 * Write status Register and configuration register with 2 bytes
887 * The first byte will be written to the status register, while the
888 * second byte will be written to the configuration register.
889 * Return negative if error occured.
890 */
891static int write_sr_cr(struct spi_nor *nor, u16 val)
892{
893 nor->cmd_buf[0] = val & 0xff;
894 nor->cmd_buf[1] = (val >> 8);
895
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530896 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2);
Huang Shijieb1994892014-02-24 18:37:37 +0800897}
898
899static int spansion_quad_enable(struct spi_nor *nor)
900{
901 int ret;
902 int quad_en = CR_QUAD_EN_SPAN << 8;
903
904 write_enable(nor);
905
906 ret = write_sr_cr(nor, quad_en);
907 if (ret < 0) {
908 dev_err(nor->dev,
909 "error while writing configuration register\n");
910 return -EINVAL;
911 }
912
913 /* read back and check it */
914 ret = read_cr(nor);
915 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
916 dev_err(nor->dev, "Spansion Quad bit not set\n");
917 return -EINVAL;
918 }
919
920 return 0;
921}
922
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000923static int micron_quad_enable(struct spi_nor *nor)
924{
925 int ret;
926 u8 val;
927
928 ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
929 if (ret < 0) {
930 dev_err(nor->dev, "error %d reading EVCR\n", ret);
931 return ret;
932 }
933
934 write_enable(nor);
935
936 /* set EVCR, enable quad I/O */
937 nor->cmd_buf[0] = val & ~EVCR_QUAD_EN_MICRON;
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530938 ret = nor->write_reg(nor, SPINOR_OP_WD_EVCR, nor->cmd_buf, 1);
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000939 if (ret < 0) {
940 dev_err(nor->dev, "error while writing EVCR register\n");
941 return ret;
942 }
943
944 ret = spi_nor_wait_till_ready(nor);
945 if (ret)
946 return ret;
947
948 /* read EVCR and check it */
949 ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
950 if (ret < 0) {
951 dev_err(nor->dev, "error %d reading EVCR\n", ret);
952 return ret;
953 }
954 if (val & EVCR_QUAD_EN_MICRON) {
955 dev_err(nor->dev, "Micron EVCR Quad bit not clear\n");
956 return -EINVAL;
957 }
958
959 return 0;
960}
961
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200962static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info)
Huang Shijieb1994892014-02-24 18:37:37 +0800963{
964 int status;
965
Huang Shijied928a252014-11-06 11:24:33 +0800966 switch (JEDEC_MFR(info)) {
Huang Shijieb1994892014-02-24 18:37:37 +0800967 case CFI_MFR_MACRONIX:
968 status = macronix_quad_enable(nor);
969 if (status) {
970 dev_err(nor->dev, "Macronix quad-read not enabled\n");
971 return -EINVAL;
972 }
973 return status;
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000974 case CFI_MFR_ST:
975 status = micron_quad_enable(nor);
976 if (status) {
977 dev_err(nor->dev, "Micron quad-read not enabled\n");
978 return -EINVAL;
979 }
980 return status;
Huang Shijieb1994892014-02-24 18:37:37 +0800981 default:
982 status = spansion_quad_enable(nor);
983 if (status) {
984 dev_err(nor->dev, "Spansion quad-read not enabled\n");
985 return -EINVAL;
986 }
987 return status;
988 }
989}
990
991static int spi_nor_check(struct spi_nor *nor)
992{
993 if (!nor->dev || !nor->read || !nor->write ||
994 !nor->read_reg || !nor->write_reg || !nor->erase) {
995 pr_err("spi-nor: please fill all the necessary fields!\n");
996 return -EINVAL;
997 }
998
Huang Shijieb1994892014-02-24 18:37:37 +0800999 return 0;
1000}
1001
Ben Hutchings70f3ce02014-09-29 11:47:54 +02001002int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
Huang Shijieb1994892014-02-24 18:37:37 +08001003{
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001004 const struct flash_info *info = NULL;
Huang Shijieb1994892014-02-24 18:37:37 +08001005 struct device *dev = nor->dev;
Brian Norris19763672015-08-13 15:46:05 -07001006 struct mtd_info *mtd = &nor->mtd;
Marek Vasut11bff0b2015-09-03 18:35:36 +02001007 struct device_node *np = nor->flash_node;
Huang Shijieb1994892014-02-24 18:37:37 +08001008 int ret;
1009 int i;
1010
1011 ret = spi_nor_check(nor);
1012 if (ret)
1013 return ret;
1014
Brian Norris43163022015-05-19 14:38:22 -07001015 if (name)
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001016 info = spi_nor_match_id(name);
Brian Norris43163022015-05-19 14:38:22 -07001017 /* Try to auto-detect if chip name wasn't specified or not found */
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001018 if (!info)
1019 info = spi_nor_read_id(nor);
1020 if (IS_ERR_OR_NULL(info))
Ben Hutchings70f3ce02014-09-29 11:47:54 +02001021 return -ENOENT;
1022
Rafał Miłecki58c81952014-12-01 09:42:16 +01001023 /*
1024 * If caller has specified name of flash model that can normally be
1025 * detected using JEDEC, let's verify it.
1026 */
1027 if (name && info->id_len) {
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001028 const struct flash_info *jinfo;
Huang Shijieb1994892014-02-24 18:37:37 +08001029
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001030 jinfo = spi_nor_read_id(nor);
1031 if (IS_ERR(jinfo)) {
1032 return PTR_ERR(jinfo);
1033 } else if (jinfo != info) {
Huang Shijieb1994892014-02-24 18:37:37 +08001034 /*
1035 * JEDEC knows better, so overwrite platform ID. We
1036 * can't trust partitions any longer, but we'll let
1037 * mtd apply them anyway, since some partitions may be
1038 * marked read-only, and we don't want to lose that
1039 * information, even if it's not 100% accurate.
1040 */
1041 dev_warn(dev, "found %s, expected %s\n",
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001042 jinfo->name, info->name);
1043 info = jinfo;
Huang Shijieb1994892014-02-24 18:37:37 +08001044 }
1045 }
1046
1047 mutex_init(&nor->lock);
1048
1049 /*
1050 * Atmel, SST and Intel/Numonyx serial nor tend to power
1051 * up with the software protection bits set
1052 */
1053
Huang Shijied928a252014-11-06 11:24:33 +08001054 if (JEDEC_MFR(info) == CFI_MFR_ATMEL ||
1055 JEDEC_MFR(info) == CFI_MFR_INTEL ||
1056 JEDEC_MFR(info) == CFI_MFR_SST) {
Huang Shijieb1994892014-02-24 18:37:37 +08001057 write_enable(nor);
1058 write_sr(nor, 0);
1059 }
1060
Rafał Miłecki32f1b7c2014-09-28 22:36:54 +02001061 if (!mtd->name)
Huang Shijieb1994892014-02-24 18:37:37 +08001062 mtd->name = dev_name(dev);
Brian Norrisc9ec3902015-08-13 15:46:03 -07001063 mtd->priv = nor;
Huang Shijieb1994892014-02-24 18:37:37 +08001064 mtd->type = MTD_NORFLASH;
1065 mtd->writesize = 1;
1066 mtd->flags = MTD_CAP_NORFLASH;
1067 mtd->size = info->sector_size * info->n_sectors;
1068 mtd->_erase = spi_nor_erase;
1069 mtd->_read = spi_nor_read;
1070
1071 /* nor protection support for STmicro chips */
Huang Shijied928a252014-11-06 11:24:33 +08001072 if (JEDEC_MFR(info) == CFI_MFR_ST) {
Brian Norris8cc7f332015-03-13 00:38:39 -07001073 nor->flash_lock = stm_lock;
1074 nor->flash_unlock = stm_unlock;
1075 }
1076
1077 if (nor->flash_lock && nor->flash_unlock) {
Huang Shijieb1994892014-02-24 18:37:37 +08001078 mtd->_lock = spi_nor_lock;
1079 mtd->_unlock = spi_nor_unlock;
1080 }
1081
1082 /* sst nor chips use AAI word program */
1083 if (info->flags & SST_WRITE)
1084 mtd->_write = sst_write;
1085 else
1086 mtd->_write = spi_nor_write;
1087
Brian Norris51983b72014-09-10 00:26:16 -07001088 if (info->flags & USE_FSR)
1089 nor->flags |= SNOR_F_USE_FSR;
grmoore@altera.comc14dedd2014-04-29 10:29:51 -05001090
Rafał Miłecki57cf26c2014-08-17 11:27:26 +02001091#ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
Huang Shijieb1994892014-02-24 18:37:37 +08001092 /* prefer "small sector" erase if possible */
1093 if (info->flags & SECT_4K) {
Brian Norrisb02e7f32014-04-08 18:15:31 -07001094 nor->erase_opcode = SPINOR_OP_BE_4K;
Huang Shijieb1994892014-02-24 18:37:37 +08001095 mtd->erasesize = 4096;
1096 } else if (info->flags & SECT_4K_PMC) {
Brian Norrisb02e7f32014-04-08 18:15:31 -07001097 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
Huang Shijieb1994892014-02-24 18:37:37 +08001098 mtd->erasesize = 4096;
Rafał Miłecki57cf26c2014-08-17 11:27:26 +02001099 } else
1100#endif
1101 {
Brian Norrisb02e7f32014-04-08 18:15:31 -07001102 nor->erase_opcode = SPINOR_OP_SE;
Huang Shijieb1994892014-02-24 18:37:37 +08001103 mtd->erasesize = info->sector_size;
1104 }
1105
1106 if (info->flags & SPI_NOR_NO_ERASE)
1107 mtd->flags |= MTD_NO_ERASE;
1108
1109 mtd->dev.parent = dev;
1110 nor->page_size = info->page_size;
1111 mtd->writebufsize = nor->page_size;
1112
1113 if (np) {
1114 /* If we were instantiated by DT, use it */
1115 if (of_property_read_bool(np, "m25p,fast-read"))
1116 nor->flash_read = SPI_NOR_FAST;
1117 else
1118 nor->flash_read = SPI_NOR_NORMAL;
1119 } else {
1120 /* If we weren't instantiated by DT, default to fast-read */
1121 nor->flash_read = SPI_NOR_FAST;
1122 }
1123
1124 /* Some devices cannot do fast-read, no matter what DT tells us */
1125 if (info->flags & SPI_NOR_NO_FR)
1126 nor->flash_read = SPI_NOR_NORMAL;
1127
1128 /* Quad/Dual-read mode takes precedence over fast/normal */
1129 if (mode == SPI_NOR_QUAD && info->flags & SPI_NOR_QUAD_READ) {
Huang Shijied928a252014-11-06 11:24:33 +08001130 ret = set_quad_mode(nor, info);
Huang Shijieb1994892014-02-24 18:37:37 +08001131 if (ret) {
1132 dev_err(dev, "quad mode not supported\n");
1133 return ret;
1134 }
1135 nor->flash_read = SPI_NOR_QUAD;
1136 } else if (mode == SPI_NOR_DUAL && info->flags & SPI_NOR_DUAL_READ) {
1137 nor->flash_read = SPI_NOR_DUAL;
1138 }
1139
1140 /* Default commands */
1141 switch (nor->flash_read) {
1142 case SPI_NOR_QUAD:
Brian Norris58b89a12014-04-08 19:16:49 -07001143 nor->read_opcode = SPINOR_OP_READ_1_1_4;
Huang Shijieb1994892014-02-24 18:37:37 +08001144 break;
1145 case SPI_NOR_DUAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001146 nor->read_opcode = SPINOR_OP_READ_1_1_2;
Huang Shijieb1994892014-02-24 18:37:37 +08001147 break;
1148 case SPI_NOR_FAST:
Brian Norris58b89a12014-04-08 19:16:49 -07001149 nor->read_opcode = SPINOR_OP_READ_FAST;
Huang Shijieb1994892014-02-24 18:37:37 +08001150 break;
1151 case SPI_NOR_NORMAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001152 nor->read_opcode = SPINOR_OP_READ;
Huang Shijieb1994892014-02-24 18:37:37 +08001153 break;
1154 default:
1155 dev_err(dev, "No Read opcode defined\n");
1156 return -EINVAL;
1157 }
1158
Brian Norrisb02e7f32014-04-08 18:15:31 -07001159 nor->program_opcode = SPINOR_OP_PP;
Huang Shijieb1994892014-02-24 18:37:37 +08001160
1161 if (info->addr_width)
1162 nor->addr_width = info->addr_width;
1163 else if (mtd->size > 0x1000000) {
1164 /* enable 4-byte addressing if the device exceeds 16MiB */
1165 nor->addr_width = 4;
Huang Shijied928a252014-11-06 11:24:33 +08001166 if (JEDEC_MFR(info) == CFI_MFR_AMD) {
Huang Shijieb1994892014-02-24 18:37:37 +08001167 /* Dedicated 4-byte command set */
1168 switch (nor->flash_read) {
1169 case SPI_NOR_QUAD:
Brian Norris58b89a12014-04-08 19:16:49 -07001170 nor->read_opcode = SPINOR_OP_READ4_1_1_4;
Huang Shijieb1994892014-02-24 18:37:37 +08001171 break;
1172 case SPI_NOR_DUAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001173 nor->read_opcode = SPINOR_OP_READ4_1_1_2;
Huang Shijieb1994892014-02-24 18:37:37 +08001174 break;
1175 case SPI_NOR_FAST:
Brian Norris58b89a12014-04-08 19:16:49 -07001176 nor->read_opcode = SPINOR_OP_READ4_FAST;
Huang Shijieb1994892014-02-24 18:37:37 +08001177 break;
1178 case SPI_NOR_NORMAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001179 nor->read_opcode = SPINOR_OP_READ4;
Huang Shijieb1994892014-02-24 18:37:37 +08001180 break;
1181 }
Brian Norrisb02e7f32014-04-08 18:15:31 -07001182 nor->program_opcode = SPINOR_OP_PP_4B;
Huang Shijieb1994892014-02-24 18:37:37 +08001183 /* No small sector erase for 4-byte command set */
Brian Norrisb02e7f32014-04-08 18:15:31 -07001184 nor->erase_opcode = SPINOR_OP_SE_4B;
Huang Shijieb1994892014-02-24 18:37:37 +08001185 mtd->erasesize = info->sector_size;
1186 } else
Huang Shijied928a252014-11-06 11:24:33 +08001187 set_4byte(nor, info, 1);
Huang Shijieb1994892014-02-24 18:37:37 +08001188 } else {
1189 nor->addr_width = 3;
1190 }
1191
1192 nor->read_dummy = spi_nor_read_dummy_cycles(nor);
1193
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001194 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
Huang Shijieb1994892014-02-24 18:37:37 +08001195 (long long)mtd->size >> 10);
1196
1197 dev_dbg(dev,
1198 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
1199 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
1200 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
1201 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
1202
1203 if (mtd->numeraseregions)
1204 for (i = 0; i < mtd->numeraseregions; i++)
1205 dev_dbg(dev,
1206 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
1207 ".erasesize = 0x%.8x (%uKiB), "
1208 ".numblocks = %d }\n",
1209 i, (long long)mtd->eraseregions[i].offset,
1210 mtd->eraseregions[i].erasesize,
1211 mtd->eraseregions[i].erasesize / 1024,
1212 mtd->eraseregions[i].numblocks);
1213 return 0;
1214}
Brian Norrisb61834b2014-04-08 18:22:57 -07001215EXPORT_SYMBOL_GPL(spi_nor_scan);
Huang Shijieb1994892014-02-24 18:37:37 +08001216
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001217static const struct flash_info *spi_nor_match_id(const char *name)
Huang Shijie0d8c11c2014-02-24 18:37:40 +08001218{
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001219 const struct flash_info *id = spi_nor_ids;
Huang Shijie0d8c11c2014-02-24 18:37:40 +08001220
Brian Norris2ff46e62015-09-02 16:34:35 -07001221 while (id->name) {
Huang Shijie0d8c11c2014-02-24 18:37:40 +08001222 if (!strcmp(name, id->name))
1223 return id;
1224 id++;
1225 }
1226 return NULL;
1227}
1228
Huang Shijieb1994892014-02-24 18:37:37 +08001229MODULE_LICENSE("GPL");
1230MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
1231MODULE_AUTHOR("Mike Lavender");
1232MODULE_DESCRIPTION("framework for SPI NOR");