blob: c616ff5701a1ecfc37c0645d0f1b0754524c56ae [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) },
Yao Yuanc887be72015-09-16 17:59:45 +0800638 { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800639 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
Harini Katakamf02985b2014-10-21 13:37:59 +0200640 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
Huang Shijieb1994892014-02-24 18:37:37 +0800641
642 /* ST Microelectronics -- newer production may have feature updates */
643 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
644 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
645 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
646 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
647 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
648 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
649 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
650 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
651 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
Huang Shijieb1994892014-02-24 18:37:37 +0800652
653 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
654 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
655 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
656 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
657 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
658 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
659 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
660 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
661 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
662
663 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
664 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
665 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
666
667 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
668 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
669 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
670
671 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
672 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
673 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
674 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
675 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
Thomas Petazzonif2fabe12014-07-27 23:56:08 +0200676 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
Huang Shijieb1994892014-02-24 18:37:37 +0800677
678 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
Gabor Juhos40d19ab2015-03-26 23:58:02 +0100679 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800680 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
681 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
682 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
683 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
684 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
685 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
686 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
687 { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K) },
688 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
689 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
Mika Westerberge88e5672015-02-05 18:39:02 +0200690 { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800691 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
692 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
693 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
694 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) },
695
696 /* Catalyst / On Semiconductor -- non-JEDEC */
697 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
698 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
699 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
700 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
701 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
702 { },
703};
704
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200705static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
Huang Shijieb1994892014-02-24 18:37:37 +0800706{
707 int tmp;
Huang Shijie09ffafb2014-11-06 07:34:01 +0100708 u8 id[SPI_NOR_MAX_ID_LEN];
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200709 const struct flash_info *info;
Huang Shijieb1994892014-02-24 18:37:37 +0800710
Huang Shijie09ffafb2014-11-06 07:34:01 +0100711 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
Huang Shijieb1994892014-02-24 18:37:37 +0800712 if (tmp < 0) {
713 dev_dbg(nor->dev, " error %d reading JEDEC ID\n", tmp);
714 return ERR_PTR(tmp);
715 }
Huang Shijieb1994892014-02-24 18:37:37 +0800716
717 for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200718 info = &spi_nor_ids[tmp];
Huang Shijie09ffafb2014-11-06 07:34:01 +0100719 if (info->id_len) {
720 if (!memcmp(info->id, id, info->id_len))
Huang Shijieb1994892014-02-24 18:37:37 +0800721 return &spi_nor_ids[tmp];
722 }
723 }
Huang Shijie09ffafb2014-11-06 07:34:01 +0100724 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %2x, %2x\n",
725 id[0], id[1], id[2]);
Huang Shijieb1994892014-02-24 18:37:37 +0800726 return ERR_PTR(-ENODEV);
727}
728
Huang Shijieb1994892014-02-24 18:37:37 +0800729static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
730 size_t *retlen, u_char *buf)
731{
732 struct spi_nor *nor = mtd_to_spi_nor(mtd);
733 int ret;
734
735 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
736
737 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
738 if (ret)
739 return ret;
740
741 ret = nor->read(nor, from, len, retlen, buf);
742
743 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
744 return ret;
745}
746
747static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
748 size_t *retlen, const u_char *buf)
749{
750 struct spi_nor *nor = mtd_to_spi_nor(mtd);
751 size_t actual;
752 int ret;
753
754 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
755
756 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
757 if (ret)
758 return ret;
759
Huang Shijieb1994892014-02-24 18:37:37 +0800760 write_enable(nor);
761
762 nor->sst_write_second = false;
763
764 actual = to % 2;
765 /* Start write from odd address. */
766 if (actual) {
Brian Norrisb02e7f32014-04-08 18:15:31 -0700767 nor->program_opcode = SPINOR_OP_BP;
Huang Shijieb1994892014-02-24 18:37:37 +0800768
769 /* write one byte. */
770 nor->write(nor, to, 1, retlen, buf);
Brian Norrisb94ed082014-08-06 18:17:00 -0700771 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800772 if (ret)
773 goto time_out;
774 }
775 to += actual;
776
777 /* Write out most of the data here. */
778 for (; actual < len - 1; actual += 2) {
Brian Norrisb02e7f32014-04-08 18:15:31 -0700779 nor->program_opcode = SPINOR_OP_AAI_WP;
Huang Shijieb1994892014-02-24 18:37:37 +0800780
781 /* write two bytes. */
782 nor->write(nor, to, 2, retlen, buf + actual);
Brian Norrisb94ed082014-08-06 18:17:00 -0700783 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800784 if (ret)
785 goto time_out;
786 to += 2;
787 nor->sst_write_second = true;
788 }
789 nor->sst_write_second = false;
790
791 write_disable(nor);
Brian Norrisb94ed082014-08-06 18:17:00 -0700792 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800793 if (ret)
794 goto time_out;
795
796 /* Write out trailing byte if it exists. */
797 if (actual != len) {
798 write_enable(nor);
799
Brian Norrisb02e7f32014-04-08 18:15:31 -0700800 nor->program_opcode = SPINOR_OP_BP;
Huang Shijieb1994892014-02-24 18:37:37 +0800801 nor->write(nor, to, 1, retlen, buf + actual);
802
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 write_disable(nor);
807 }
808time_out:
809 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
810 return ret;
811}
812
813/*
814 * Write an address range to the nor chip. Data must be written in
815 * FLASH_PAGESIZE chunks. The address range may be any size provided
816 * it is within the physical boundaries.
817 */
818static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
819 size_t *retlen, const u_char *buf)
820{
821 struct spi_nor *nor = mtd_to_spi_nor(mtd);
822 u32 page_offset, page_size, i;
823 int ret;
824
825 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
826
827 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
828 if (ret)
829 return ret;
830
Huang Shijieb1994892014-02-24 18:37:37 +0800831 write_enable(nor);
832
833 page_offset = to & (nor->page_size - 1);
834
835 /* do all the bytes fit onto one page? */
836 if (page_offset + len <= nor->page_size) {
837 nor->write(nor, to, len, retlen, buf);
838 } else {
839 /* the size of data remaining on the first page */
840 page_size = nor->page_size - page_offset;
841 nor->write(nor, to, page_size, retlen, buf);
842
843 /* write everything in nor->page_size chunks */
844 for (i = page_size; i < len; i += page_size) {
845 page_size = len - i;
846 if (page_size > nor->page_size)
847 page_size = nor->page_size;
848
Brian Norrisb94ed082014-08-06 18:17:00 -0700849 ret = spi_nor_wait_till_ready(nor);
Brian Norris1d61dcb2014-08-06 18:16:56 -0700850 if (ret)
851 goto write_err;
852
Huang Shijieb1994892014-02-24 18:37:37 +0800853 write_enable(nor);
854
855 nor->write(nor, to + i, page_size, retlen, buf + i);
856 }
857 }
858
Brian Norrisdfa9c0c2014-08-06 18:16:57 -0700859 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800860write_err:
861 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
Brian Norris1d61dcb2014-08-06 18:16:56 -0700862 return ret;
Huang Shijieb1994892014-02-24 18:37:37 +0800863}
864
865static int macronix_quad_enable(struct spi_nor *nor)
866{
867 int ret, val;
868
869 val = read_sr(nor);
870 write_enable(nor);
871
Jagan Tekifd725232015-08-19 15:26:43 +0530872 write_sr(nor, val | SR_QUAD_EN_MX);
Huang Shijieb1994892014-02-24 18:37:37 +0800873
Brian Norrisb94ed082014-08-06 18:17:00 -0700874 if (spi_nor_wait_till_ready(nor))
Huang Shijieb1994892014-02-24 18:37:37 +0800875 return 1;
876
877 ret = read_sr(nor);
878 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
879 dev_err(nor->dev, "Macronix Quad bit not set\n");
880 return -EINVAL;
881 }
882
883 return 0;
884}
885
886/*
887 * Write status Register and configuration register with 2 bytes
888 * The first byte will be written to the status register, while the
889 * second byte will be written to the configuration register.
890 * Return negative if error occured.
891 */
892static int write_sr_cr(struct spi_nor *nor, u16 val)
893{
894 nor->cmd_buf[0] = val & 0xff;
895 nor->cmd_buf[1] = (val >> 8);
896
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530897 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2);
Huang Shijieb1994892014-02-24 18:37:37 +0800898}
899
900static int spansion_quad_enable(struct spi_nor *nor)
901{
902 int ret;
903 int quad_en = CR_QUAD_EN_SPAN << 8;
904
905 write_enable(nor);
906
907 ret = write_sr_cr(nor, quad_en);
908 if (ret < 0) {
909 dev_err(nor->dev,
910 "error while writing configuration register\n");
911 return -EINVAL;
912 }
913
914 /* read back and check it */
915 ret = read_cr(nor);
916 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
917 dev_err(nor->dev, "Spansion Quad bit not set\n");
918 return -EINVAL;
919 }
920
921 return 0;
922}
923
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000924static int micron_quad_enable(struct spi_nor *nor)
925{
926 int ret;
927 u8 val;
928
929 ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
930 if (ret < 0) {
931 dev_err(nor->dev, "error %d reading EVCR\n", ret);
932 return ret;
933 }
934
935 write_enable(nor);
936
937 /* set EVCR, enable quad I/O */
938 nor->cmd_buf[0] = val & ~EVCR_QUAD_EN_MICRON;
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530939 ret = nor->write_reg(nor, SPINOR_OP_WD_EVCR, nor->cmd_buf, 1);
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000940 if (ret < 0) {
941 dev_err(nor->dev, "error while writing EVCR register\n");
942 return ret;
943 }
944
945 ret = spi_nor_wait_till_ready(nor);
946 if (ret)
947 return ret;
948
949 /* read EVCR and check it */
950 ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
951 if (ret < 0) {
952 dev_err(nor->dev, "error %d reading EVCR\n", ret);
953 return ret;
954 }
955 if (val & EVCR_QUAD_EN_MICRON) {
956 dev_err(nor->dev, "Micron EVCR Quad bit not clear\n");
957 return -EINVAL;
958 }
959
960 return 0;
961}
962
Rafał Miłecki06bb6f52015-08-10 21:39:03 +0200963static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info)
Huang Shijieb1994892014-02-24 18:37:37 +0800964{
965 int status;
966
Huang Shijied928a252014-11-06 11:24:33 +0800967 switch (JEDEC_MFR(info)) {
Huang Shijieb1994892014-02-24 18:37:37 +0800968 case CFI_MFR_MACRONIX:
969 status = macronix_quad_enable(nor);
970 if (status) {
971 dev_err(nor->dev, "Macronix quad-read not enabled\n");
972 return -EINVAL;
973 }
974 return status;
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000975 case CFI_MFR_ST:
976 status = micron_quad_enable(nor);
977 if (status) {
978 dev_err(nor->dev, "Micron quad-read not enabled\n");
979 return -EINVAL;
980 }
981 return status;
Huang Shijieb1994892014-02-24 18:37:37 +0800982 default:
983 status = spansion_quad_enable(nor);
984 if (status) {
985 dev_err(nor->dev, "Spansion quad-read not enabled\n");
986 return -EINVAL;
987 }
988 return status;
989 }
990}
991
992static int spi_nor_check(struct spi_nor *nor)
993{
994 if (!nor->dev || !nor->read || !nor->write ||
995 !nor->read_reg || !nor->write_reg || !nor->erase) {
996 pr_err("spi-nor: please fill all the necessary fields!\n");
997 return -EINVAL;
998 }
999
Huang Shijieb1994892014-02-24 18:37:37 +08001000 return 0;
1001}
1002
Ben Hutchings70f3ce02014-09-29 11:47:54 +02001003int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
Huang Shijieb1994892014-02-24 18:37:37 +08001004{
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001005 const struct flash_info *info = NULL;
Huang Shijieb1994892014-02-24 18:37:37 +08001006 struct device *dev = nor->dev;
Brian Norris19763672015-08-13 15:46:05 -07001007 struct mtd_info *mtd = &nor->mtd;
Marek Vasut11bff0b2015-09-03 18:35:36 +02001008 struct device_node *np = nor->flash_node;
Huang Shijieb1994892014-02-24 18:37:37 +08001009 int ret;
1010 int i;
1011
1012 ret = spi_nor_check(nor);
1013 if (ret)
1014 return ret;
1015
Brian Norris43163022015-05-19 14:38:22 -07001016 if (name)
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001017 info = spi_nor_match_id(name);
Brian Norris43163022015-05-19 14:38:22 -07001018 /* Try to auto-detect if chip name wasn't specified or not found */
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001019 if (!info)
1020 info = spi_nor_read_id(nor);
1021 if (IS_ERR_OR_NULL(info))
Ben Hutchings70f3ce02014-09-29 11:47:54 +02001022 return -ENOENT;
1023
Rafał Miłecki58c81952014-12-01 09:42:16 +01001024 /*
1025 * If caller has specified name of flash model that can normally be
1026 * detected using JEDEC, let's verify it.
1027 */
1028 if (name && info->id_len) {
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001029 const struct flash_info *jinfo;
Huang Shijieb1994892014-02-24 18:37:37 +08001030
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001031 jinfo = spi_nor_read_id(nor);
1032 if (IS_ERR(jinfo)) {
1033 return PTR_ERR(jinfo);
1034 } else if (jinfo != info) {
Huang Shijieb1994892014-02-24 18:37:37 +08001035 /*
1036 * JEDEC knows better, so overwrite platform ID. We
1037 * can't trust partitions any longer, but we'll let
1038 * mtd apply them anyway, since some partitions may be
1039 * marked read-only, and we don't want to lose that
1040 * information, even if it's not 100% accurate.
1041 */
1042 dev_warn(dev, "found %s, expected %s\n",
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001043 jinfo->name, info->name);
1044 info = jinfo;
Huang Shijieb1994892014-02-24 18:37:37 +08001045 }
1046 }
1047
1048 mutex_init(&nor->lock);
1049
1050 /*
1051 * Atmel, SST and Intel/Numonyx serial nor tend to power
1052 * up with the software protection bits set
1053 */
1054
Huang Shijied928a252014-11-06 11:24:33 +08001055 if (JEDEC_MFR(info) == CFI_MFR_ATMEL ||
1056 JEDEC_MFR(info) == CFI_MFR_INTEL ||
1057 JEDEC_MFR(info) == CFI_MFR_SST) {
Huang Shijieb1994892014-02-24 18:37:37 +08001058 write_enable(nor);
1059 write_sr(nor, 0);
1060 }
1061
Rafał Miłecki32f1b7c2014-09-28 22:36:54 +02001062 if (!mtd->name)
Huang Shijieb1994892014-02-24 18:37:37 +08001063 mtd->name = dev_name(dev);
Brian Norrisc9ec3902015-08-13 15:46:03 -07001064 mtd->priv = nor;
Huang Shijieb1994892014-02-24 18:37:37 +08001065 mtd->type = MTD_NORFLASH;
1066 mtd->writesize = 1;
1067 mtd->flags = MTD_CAP_NORFLASH;
1068 mtd->size = info->sector_size * info->n_sectors;
1069 mtd->_erase = spi_nor_erase;
1070 mtd->_read = spi_nor_read;
1071
1072 /* nor protection support for STmicro chips */
Huang Shijied928a252014-11-06 11:24:33 +08001073 if (JEDEC_MFR(info) == CFI_MFR_ST) {
Brian Norris8cc7f332015-03-13 00:38:39 -07001074 nor->flash_lock = stm_lock;
1075 nor->flash_unlock = stm_unlock;
1076 }
1077
1078 if (nor->flash_lock && nor->flash_unlock) {
Huang Shijieb1994892014-02-24 18:37:37 +08001079 mtd->_lock = spi_nor_lock;
1080 mtd->_unlock = spi_nor_unlock;
1081 }
1082
1083 /* sst nor chips use AAI word program */
1084 if (info->flags & SST_WRITE)
1085 mtd->_write = sst_write;
1086 else
1087 mtd->_write = spi_nor_write;
1088
Brian Norris51983b72014-09-10 00:26:16 -07001089 if (info->flags & USE_FSR)
1090 nor->flags |= SNOR_F_USE_FSR;
grmoore@altera.comc14dedd2014-04-29 10:29:51 -05001091
Rafał Miłecki57cf26c2014-08-17 11:27:26 +02001092#ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
Huang Shijieb1994892014-02-24 18:37:37 +08001093 /* prefer "small sector" erase if possible */
1094 if (info->flags & SECT_4K) {
Brian Norrisb02e7f32014-04-08 18:15:31 -07001095 nor->erase_opcode = SPINOR_OP_BE_4K;
Huang Shijieb1994892014-02-24 18:37:37 +08001096 mtd->erasesize = 4096;
1097 } else if (info->flags & SECT_4K_PMC) {
Brian Norrisb02e7f32014-04-08 18:15:31 -07001098 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
Huang Shijieb1994892014-02-24 18:37:37 +08001099 mtd->erasesize = 4096;
Rafał Miłecki57cf26c2014-08-17 11:27:26 +02001100 } else
1101#endif
1102 {
Brian Norrisb02e7f32014-04-08 18:15:31 -07001103 nor->erase_opcode = SPINOR_OP_SE;
Huang Shijieb1994892014-02-24 18:37:37 +08001104 mtd->erasesize = info->sector_size;
1105 }
1106
1107 if (info->flags & SPI_NOR_NO_ERASE)
1108 mtd->flags |= MTD_NO_ERASE;
1109
1110 mtd->dev.parent = dev;
1111 nor->page_size = info->page_size;
1112 mtd->writebufsize = nor->page_size;
1113
1114 if (np) {
1115 /* If we were instantiated by DT, use it */
1116 if (of_property_read_bool(np, "m25p,fast-read"))
1117 nor->flash_read = SPI_NOR_FAST;
1118 else
1119 nor->flash_read = SPI_NOR_NORMAL;
1120 } else {
1121 /* If we weren't instantiated by DT, default to fast-read */
1122 nor->flash_read = SPI_NOR_FAST;
1123 }
1124
1125 /* Some devices cannot do fast-read, no matter what DT tells us */
1126 if (info->flags & SPI_NOR_NO_FR)
1127 nor->flash_read = SPI_NOR_NORMAL;
1128
1129 /* Quad/Dual-read mode takes precedence over fast/normal */
1130 if (mode == SPI_NOR_QUAD && info->flags & SPI_NOR_QUAD_READ) {
Huang Shijied928a252014-11-06 11:24:33 +08001131 ret = set_quad_mode(nor, info);
Huang Shijieb1994892014-02-24 18:37:37 +08001132 if (ret) {
1133 dev_err(dev, "quad mode not supported\n");
1134 return ret;
1135 }
1136 nor->flash_read = SPI_NOR_QUAD;
1137 } else if (mode == SPI_NOR_DUAL && info->flags & SPI_NOR_DUAL_READ) {
1138 nor->flash_read = SPI_NOR_DUAL;
1139 }
1140
1141 /* Default commands */
1142 switch (nor->flash_read) {
1143 case SPI_NOR_QUAD:
Brian Norris58b89a12014-04-08 19:16:49 -07001144 nor->read_opcode = SPINOR_OP_READ_1_1_4;
Huang Shijieb1994892014-02-24 18:37:37 +08001145 break;
1146 case SPI_NOR_DUAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001147 nor->read_opcode = SPINOR_OP_READ_1_1_2;
Huang Shijieb1994892014-02-24 18:37:37 +08001148 break;
1149 case SPI_NOR_FAST:
Brian Norris58b89a12014-04-08 19:16:49 -07001150 nor->read_opcode = SPINOR_OP_READ_FAST;
Huang Shijieb1994892014-02-24 18:37:37 +08001151 break;
1152 case SPI_NOR_NORMAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001153 nor->read_opcode = SPINOR_OP_READ;
Huang Shijieb1994892014-02-24 18:37:37 +08001154 break;
1155 default:
1156 dev_err(dev, "No Read opcode defined\n");
1157 return -EINVAL;
1158 }
1159
Brian Norrisb02e7f32014-04-08 18:15:31 -07001160 nor->program_opcode = SPINOR_OP_PP;
Huang Shijieb1994892014-02-24 18:37:37 +08001161
1162 if (info->addr_width)
1163 nor->addr_width = info->addr_width;
1164 else if (mtd->size > 0x1000000) {
1165 /* enable 4-byte addressing if the device exceeds 16MiB */
1166 nor->addr_width = 4;
Huang Shijied928a252014-11-06 11:24:33 +08001167 if (JEDEC_MFR(info) == CFI_MFR_AMD) {
Huang Shijieb1994892014-02-24 18:37:37 +08001168 /* Dedicated 4-byte command set */
1169 switch (nor->flash_read) {
1170 case SPI_NOR_QUAD:
Brian Norris58b89a12014-04-08 19:16:49 -07001171 nor->read_opcode = SPINOR_OP_READ4_1_1_4;
Huang Shijieb1994892014-02-24 18:37:37 +08001172 break;
1173 case SPI_NOR_DUAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001174 nor->read_opcode = SPINOR_OP_READ4_1_1_2;
Huang Shijieb1994892014-02-24 18:37:37 +08001175 break;
1176 case SPI_NOR_FAST:
Brian Norris58b89a12014-04-08 19:16:49 -07001177 nor->read_opcode = SPINOR_OP_READ4_FAST;
Huang Shijieb1994892014-02-24 18:37:37 +08001178 break;
1179 case SPI_NOR_NORMAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001180 nor->read_opcode = SPINOR_OP_READ4;
Huang Shijieb1994892014-02-24 18:37:37 +08001181 break;
1182 }
Brian Norrisb02e7f32014-04-08 18:15:31 -07001183 nor->program_opcode = SPINOR_OP_PP_4B;
Huang Shijieb1994892014-02-24 18:37:37 +08001184 /* No small sector erase for 4-byte command set */
Brian Norrisb02e7f32014-04-08 18:15:31 -07001185 nor->erase_opcode = SPINOR_OP_SE_4B;
Huang Shijieb1994892014-02-24 18:37:37 +08001186 mtd->erasesize = info->sector_size;
1187 } else
Huang Shijied928a252014-11-06 11:24:33 +08001188 set_4byte(nor, info, 1);
Huang Shijieb1994892014-02-24 18:37:37 +08001189 } else {
1190 nor->addr_width = 3;
1191 }
1192
1193 nor->read_dummy = spi_nor_read_dummy_cycles(nor);
1194
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001195 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
Huang Shijieb1994892014-02-24 18:37:37 +08001196 (long long)mtd->size >> 10);
1197
1198 dev_dbg(dev,
1199 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
1200 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
1201 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
1202 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
1203
1204 if (mtd->numeraseregions)
1205 for (i = 0; i < mtd->numeraseregions; i++)
1206 dev_dbg(dev,
1207 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
1208 ".erasesize = 0x%.8x (%uKiB), "
1209 ".numblocks = %d }\n",
1210 i, (long long)mtd->eraseregions[i].offset,
1211 mtd->eraseregions[i].erasesize,
1212 mtd->eraseregions[i].erasesize / 1024,
1213 mtd->eraseregions[i].numblocks);
1214 return 0;
1215}
Brian Norrisb61834b2014-04-08 18:22:57 -07001216EXPORT_SYMBOL_GPL(spi_nor_scan);
Huang Shijieb1994892014-02-24 18:37:37 +08001217
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001218static const struct flash_info *spi_nor_match_id(const char *name)
Huang Shijie0d8c11c2014-02-24 18:37:40 +08001219{
Rafał Miłecki06bb6f52015-08-10 21:39:03 +02001220 const struct flash_info *id = spi_nor_ids;
Huang Shijie0d8c11c2014-02-24 18:37:40 +08001221
Brian Norris2ff46e62015-09-02 16:34:35 -07001222 while (id->name) {
Huang Shijie0d8c11c2014-02-24 18:37:40 +08001223 if (!strcmp(name, id->name))
1224 return id;
1225 id++;
1226 }
1227 return NULL;
1228}
1229
Huang Shijieb1994892014-02-24 18:37:37 +08001230MODULE_LICENSE("GPL");
1231MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
1232MODULE_AUTHOR("Mike Lavender");
1233MODULE_DESCRIPTION("framework for SPI NOR");