blob: 47df4b5eae2fc1c72381f0ed916d03729d13dacb [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 {
32 /*
33 * This array stores the ID bytes.
34 * The first three bytes are the JEDIC ID.
35 * JEDEC ID zero means "no ID" (mostly older chips).
36 */
37 u8 id[SPI_NOR_MAX_ID_LEN];
38 u8 id_len;
39
40 /* The size listed here is what works with SPINOR_OP_SE, which isn't
41 * necessarily called a "sector" by the vendor.
42 */
43 unsigned sector_size;
44 u16 n_sectors;
45
46 u16 page_size;
47 u16 addr_width;
48
49 u16 flags;
50#define SECT_4K 0x01 /* SPINOR_OP_BE_4K works uniformly */
51#define SPI_NOR_NO_ERASE 0x02 /* No erase command needed */
52#define SST_WRITE 0x04 /* use SST byte programming */
53#define SPI_NOR_NO_FR 0x08 /* Can't do fastread */
54#define SECT_4K_PMC 0x10 /* SPINOR_OP_BE_4K_PMC works uniformly */
55#define SPI_NOR_DUAL_READ 0x20 /* Flash supports Dual Read */
56#define SPI_NOR_QUAD_READ 0x40 /* Flash supports Quad Read */
57#define USE_FSR 0x80 /* use flag status register */
58};
59
60#define JEDEC_MFR(info) ((info)->id[0])
Huang Shijieb1994892014-02-24 18:37:37 +080061
Ben Hutchings70f3ce02014-09-29 11:47:54 +020062static const struct spi_device_id *spi_nor_match_id(const char *name);
63
Huang Shijieb1994892014-02-24 18:37:37 +080064/*
65 * Read the status register, returning its value in the location
66 * Return the status register value.
67 * Returns negative if error occurred.
68 */
69static int read_sr(struct spi_nor *nor)
70{
71 int ret;
72 u8 val;
73
Brian Norrisb02e7f32014-04-08 18:15:31 -070074 ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
Huang Shijieb1994892014-02-24 18:37:37 +080075 if (ret < 0) {
76 pr_err("error %d reading SR\n", (int) ret);
77 return ret;
78 }
79
80 return val;
81}
82
83/*
grmoore@altera.comc14dedd2014-04-29 10:29:51 -050084 * Read the flag status register, returning its value in the location
85 * Return the status register value.
86 * Returns negative if error occurred.
87 */
88static int read_fsr(struct spi_nor *nor)
89{
90 int ret;
91 u8 val;
92
93 ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
94 if (ret < 0) {
95 pr_err("error %d reading FSR\n", ret);
96 return ret;
97 }
98
99 return val;
100}
101
102/*
Huang Shijieb1994892014-02-24 18:37:37 +0800103 * Read configuration register, returning its value in the
104 * location. Return the configuration register value.
105 * Returns negative if error occured.
106 */
107static int read_cr(struct spi_nor *nor)
108{
109 int ret;
110 u8 val;
111
Brian Norrisb02e7f32014-04-08 18:15:31 -0700112 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
Huang Shijieb1994892014-02-24 18:37:37 +0800113 if (ret < 0) {
114 dev_err(nor->dev, "error %d reading CR\n", ret);
115 return ret;
116 }
117
118 return val;
119}
120
121/*
122 * Dummy Cycle calculation for different type of read.
123 * It can be used to support more commands with
124 * different dummy cycle requirements.
125 */
126static inline int spi_nor_read_dummy_cycles(struct spi_nor *nor)
127{
128 switch (nor->flash_read) {
129 case SPI_NOR_FAST:
130 case SPI_NOR_DUAL:
131 case SPI_NOR_QUAD:
Huang Shijie0b78a2c2014-04-28 11:53:38 +0800132 return 8;
Huang Shijieb1994892014-02-24 18:37:37 +0800133 case SPI_NOR_NORMAL:
134 return 0;
135 }
136 return 0;
137}
138
139/*
140 * Write status register 1 byte
141 * Returns negative if error occurred.
142 */
143static inline int write_sr(struct spi_nor *nor, u8 val)
144{
145 nor->cmd_buf[0] = val;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700146 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800147}
148
149/*
150 * Set write enable latch with Write Enable command.
151 * Returns negative if error occurred.
152 */
153static inline int write_enable(struct spi_nor *nor)
154{
Brian Norrisb02e7f32014-04-08 18:15:31 -0700155 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800156}
157
158/*
159 * Send write disble instruction to the chip.
160 */
161static inline int write_disable(struct spi_nor *nor)
162{
Brian Norrisb02e7f32014-04-08 18:15:31 -0700163 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800164}
165
166static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
167{
168 return mtd->priv;
169}
170
171/* Enable/disable 4-byte addressing mode. */
Huang Shijied928a252014-11-06 11:24:33 +0800172static inline int set_4byte(struct spi_nor *nor, struct flash_info *info,
173 int enable)
Huang Shijieb1994892014-02-24 18:37:37 +0800174{
175 int status;
176 bool need_wren = false;
177 u8 cmd;
178
Huang Shijied928a252014-11-06 11:24:33 +0800179 switch (JEDEC_MFR(info)) {
Huang Shijieb1994892014-02-24 18:37:37 +0800180 case CFI_MFR_ST: /* Micron, actually */
181 /* Some Micron need WREN command; all will accept it */
182 need_wren = true;
183 case CFI_MFR_MACRONIX:
184 case 0xEF /* winbond */:
185 if (need_wren)
186 write_enable(nor);
187
Brian Norrisb02e7f32014-04-08 18:15:31 -0700188 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
Huang Shijieb1994892014-02-24 18:37:37 +0800189 status = nor->write_reg(nor, cmd, NULL, 0, 0);
190 if (need_wren)
191 write_disable(nor);
192
193 return status;
194 default:
195 /* Spansion style */
196 nor->cmd_buf[0] = enable << 7;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700197 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800198 }
199}
Brian Norris51983b72014-09-10 00:26:16 -0700200static inline int spi_nor_sr_ready(struct spi_nor *nor)
201{
202 int sr = read_sr(nor);
203 if (sr < 0)
204 return sr;
205 else
206 return !(sr & SR_WIP);
207}
208
209static inline int spi_nor_fsr_ready(struct spi_nor *nor)
210{
211 int fsr = read_fsr(nor);
212 if (fsr < 0)
213 return fsr;
214 else
215 return fsr & FSR_READY;
216}
217
218static int spi_nor_ready(struct spi_nor *nor)
219{
220 int sr, fsr;
221 sr = spi_nor_sr_ready(nor);
222 if (sr < 0)
223 return sr;
224 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
225 if (fsr < 0)
226 return fsr;
227 return sr && fsr;
228}
Huang Shijieb1994892014-02-24 18:37:37 +0800229
Brian Norrisb94ed082014-08-06 18:17:00 -0700230/*
231 * Service routine to read status register until ready, or timeout occurs.
232 * Returns non-zero if error.
233 */
Huang Shijieb1994892014-02-24 18:37:37 +0800234static int spi_nor_wait_till_ready(struct spi_nor *nor)
235{
236 unsigned long deadline;
Brian Norrisa95ce922014-11-05 02:32:03 -0800237 int timeout = 0, ret;
Huang Shijieb1994892014-02-24 18:37:37 +0800238
239 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
240
Brian Norrisa95ce922014-11-05 02:32:03 -0800241 while (!timeout) {
242 if (time_after_eq(jiffies, deadline))
243 timeout = 1;
Huang Shijieb1994892014-02-24 18:37:37 +0800244
Brian Norris51983b72014-09-10 00:26:16 -0700245 ret = spi_nor_ready(nor);
246 if (ret < 0)
247 return ret;
248 if (ret)
Huang Shijieb1994892014-02-24 18:37:37 +0800249 return 0;
Brian Norrisa95ce922014-11-05 02:32:03 -0800250
251 cond_resched();
252 }
253
254 dev_err(nor->dev, "flash operation timed out\n");
Huang Shijieb1994892014-02-24 18:37:37 +0800255
256 return -ETIMEDOUT;
257}
258
259/*
Huang Shijieb1994892014-02-24 18:37:37 +0800260 * Erase the whole flash memory
261 *
262 * Returns 0 if successful, non-zero otherwise.
263 */
264static int erase_chip(struct spi_nor *nor)
265{
Huang Shijieb1994892014-02-24 18:37:37 +0800266 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd->size >> 10));
267
Brian Norrisb02e7f32014-04-08 18:15:31 -0700268 return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800269}
270
271static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
272{
273 int ret = 0;
274
275 mutex_lock(&nor->lock);
276
277 if (nor->prepare) {
278 ret = nor->prepare(nor, ops);
279 if (ret) {
280 dev_err(nor->dev, "failed in the preparation.\n");
281 mutex_unlock(&nor->lock);
282 return ret;
283 }
284 }
285 return ret;
286}
287
288static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
289{
290 if (nor->unprepare)
291 nor->unprepare(nor, ops);
292 mutex_unlock(&nor->lock);
293}
294
295/*
296 * Erase an address range on the nor chip. The address range may extend
297 * one or more erase sectors. Return an error is there is a problem erasing.
298 */
299static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
300{
301 struct spi_nor *nor = mtd_to_spi_nor(mtd);
302 u32 addr, len;
303 uint32_t rem;
304 int ret;
305
306 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
307 (long long)instr->len);
308
309 div_u64_rem(instr->len, mtd->erasesize, &rem);
310 if (rem)
311 return -EINVAL;
312
313 addr = instr->addr;
314 len = instr->len;
315
316 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
317 if (ret)
318 return ret;
319
320 /* whole-chip erase? */
321 if (len == mtd->size) {
Brian Norris05241ae2014-11-05 02:29:03 -0800322 write_enable(nor);
323
Huang Shijieb1994892014-02-24 18:37:37 +0800324 if (erase_chip(nor)) {
325 ret = -EIO;
326 goto erase_err;
327 }
328
Brian Norrisdfa9c0c2014-08-06 18:16:57 -0700329 ret = spi_nor_wait_till_ready(nor);
330 if (ret)
331 goto erase_err;
332
Huang Shijieb1994892014-02-24 18:37:37 +0800333 /* REVISIT in some cases we could speed up erasing large regions
Brian Norrisb02e7f32014-04-08 18:15:31 -0700334 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
Huang Shijieb1994892014-02-24 18:37:37 +0800335 * to use "small sector erase", but that's not always optimal.
336 */
337
338 /* "sector"-at-a-time erase */
339 } else {
340 while (len) {
Brian Norris05241ae2014-11-05 02:29:03 -0800341 write_enable(nor);
342
Huang Shijieb1994892014-02-24 18:37:37 +0800343 if (nor->erase(nor, addr)) {
344 ret = -EIO;
345 goto erase_err;
346 }
347
348 addr += mtd->erasesize;
349 len -= mtd->erasesize;
Brian Norrisdfa9c0c2014-08-06 18:16:57 -0700350
351 ret = spi_nor_wait_till_ready(nor);
352 if (ret)
353 goto erase_err;
Huang Shijieb1994892014-02-24 18:37:37 +0800354 }
355 }
356
Brian Norris05241ae2014-11-05 02:29:03 -0800357 write_disable(nor);
358
Huang Shijieb1994892014-02-24 18:37:37 +0800359 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
360
361 instr->state = MTD_ERASE_DONE;
362 mtd_erase_callback(instr);
363
364 return ret;
365
366erase_err:
367 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
368 instr->state = MTD_ERASE_FAILED;
369 return ret;
370}
371
Brian Norris8cc7f332015-03-13 00:38:39 -0700372static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
Huang Shijieb1994892014-02-24 18:37:37 +0800373{
Brian Norris8cc7f332015-03-13 00:38:39 -0700374 struct mtd_info *mtd = nor->mtd;
Huang Shijieb1994892014-02-24 18:37:37 +0800375 uint32_t offset = ofs;
376 uint8_t status_old, status_new;
377 int ret = 0;
378
Huang Shijieb1994892014-02-24 18:37:37 +0800379 status_old = read_sr(nor);
380
381 if (offset < mtd->size - (mtd->size / 2))
382 status_new = status_old | SR_BP2 | SR_BP1 | SR_BP0;
383 else if (offset < mtd->size - (mtd->size / 4))
384 status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
385 else if (offset < mtd->size - (mtd->size / 8))
386 status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
387 else if (offset < mtd->size - (mtd->size / 16))
388 status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
389 else if (offset < mtd->size - (mtd->size / 32))
390 status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
391 else if (offset < mtd->size - (mtd->size / 64))
392 status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
393 else
394 status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
395
396 /* Only modify protection if it will not unlock other areas */
397 if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) >
398 (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
399 write_enable(nor);
400 ret = write_sr(nor, status_new);
Huang Shijieb1994892014-02-24 18:37:37 +0800401 }
402
Huang Shijieb1994892014-02-24 18:37:37 +0800403 return ret;
404}
405
Brian Norris8cc7f332015-03-13 00:38:39 -0700406static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
Huang Shijieb1994892014-02-24 18:37:37 +0800407{
Brian Norris8cc7f332015-03-13 00:38:39 -0700408 struct mtd_info *mtd = nor->mtd;
Huang Shijieb1994892014-02-24 18:37:37 +0800409 uint32_t offset = ofs;
410 uint8_t status_old, status_new;
411 int ret = 0;
412
Huang Shijieb1994892014-02-24 18:37:37 +0800413 status_old = read_sr(nor);
414
415 if (offset+len > mtd->size - (mtd->size / 64))
416 status_new = status_old & ~(SR_BP2 | SR_BP1 | SR_BP0);
417 else if (offset+len > mtd->size - (mtd->size / 32))
418 status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
419 else if (offset+len > mtd->size - (mtd->size / 16))
420 status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
421 else if (offset+len > mtd->size - (mtd->size / 8))
422 status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
423 else if (offset+len > mtd->size - (mtd->size / 4))
424 status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
425 else if (offset+len > mtd->size - (mtd->size / 2))
426 status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
427 else
428 status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
429
430 /* Only modify protection if it will not lock other areas */
431 if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) <
432 (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
433 write_enable(nor);
434 ret = write_sr(nor, status_new);
Huang Shijieb1994892014-02-24 18:37:37 +0800435 }
436
Brian Norris8cc7f332015-03-13 00:38:39 -0700437 return ret;
438}
439
440static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
441{
442 struct spi_nor *nor = mtd_to_spi_nor(mtd);
443 int ret;
444
445 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
446 if (ret)
447 return ret;
448
449 ret = nor->flash_lock(nor, ofs, len);
450
Huang Shijieb1994892014-02-24 18:37:37 +0800451 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
452 return ret;
453}
454
Brian Norris8cc7f332015-03-13 00:38:39 -0700455static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
456{
457 struct spi_nor *nor = mtd_to_spi_nor(mtd);
458 int ret;
459
460 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
461 if (ret)
462 return ret;
463
464 ret = nor->flash_unlock(nor, ofs, len);
465
466 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
467 return ret;
468}
469
Huang Shijie09ffafb2014-11-06 07:34:01 +0100470/* Used when the "_ext_id" is two bytes at most */
Huang Shijieb1994892014-02-24 18:37:37 +0800471#define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
472 ((kernel_ulong_t)&(struct flash_info) { \
Huang Shijie09ffafb2014-11-06 07:34:01 +0100473 .id = { \
474 ((_jedec_id) >> 16) & 0xff, \
475 ((_jedec_id) >> 8) & 0xff, \
476 (_jedec_id) & 0xff, \
477 ((_ext_id) >> 8) & 0xff, \
478 (_ext_id) & 0xff, \
479 }, \
480 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
Huang Shijieb1994892014-02-24 18:37:37 +0800481 .sector_size = (_sector_size), \
482 .n_sectors = (_n_sectors), \
483 .page_size = 256, \
484 .flags = (_flags), \
485 })
486
Huang Shijie6d7604e2014-08-12 08:54:56 +0800487#define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
488 ((kernel_ulong_t)&(struct flash_info) { \
489 .id = { \
490 ((_jedec_id) >> 16) & 0xff, \
491 ((_jedec_id) >> 8) & 0xff, \
492 (_jedec_id) & 0xff, \
493 ((_ext_id) >> 16) & 0xff, \
494 ((_ext_id) >> 8) & 0xff, \
495 (_ext_id) & 0xff, \
496 }, \
497 .id_len = 6, \
498 .sector_size = (_sector_size), \
499 .n_sectors = (_n_sectors), \
500 .page_size = 256, \
501 .flags = (_flags), \
502 })
503
Huang Shijieb1994892014-02-24 18:37:37 +0800504#define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
505 ((kernel_ulong_t)&(struct flash_info) { \
506 .sector_size = (_sector_size), \
507 .n_sectors = (_n_sectors), \
508 .page_size = (_page_size), \
509 .addr_width = (_addr_width), \
510 .flags = (_flags), \
511 })
512
513/* NOTE: double check command sets and memory organization when you add
514 * more nor chips. This current list focusses on newer chips, which
515 * have been converging on command sets which including JEDEC ID.
Rafał Miłeckic19900e2015-04-25 12:41:30 +0200516 *
517 * All newly added entries should describe *hardware* and should use SECT_4K
518 * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
519 * scenarios excluding small sectors there is config option that can be
520 * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
521 * For historical (and compatibility) reasons (before we got above config) some
522 * old entries may be missing 4K flag.
Huang Shijieb1994892014-02-24 18:37:37 +0800523 */
Ben Hutchingsa5b76162014-09-30 03:14:55 +0100524static const struct spi_device_id spi_nor_ids[] = {
Huang Shijieb1994892014-02-24 18:37:37 +0800525 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
526 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
527 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
528
529 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
530 { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
531 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
532
533 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
534 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
535 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
536 { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
537
538 { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
539
540 /* EON -- en25xxx */
541 { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
542 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
543 { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
544 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
545 { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
Sergey Ryazanova41595b2014-06-12 18:16:46 +0400546 { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
Huang Shijieb1994892014-02-24 18:37:37 +0800547 { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
Rafał Miłeckic19900e2015-04-25 12:41:30 +0200548 { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800549
550 /* ESMT */
551 { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K) },
552
553 /* Everspin */
554 { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
555 { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
556
Rostislav Lisovyce56ce72014-10-29 10:10:47 +0100557 /* Fujitsu */
558 { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
559
Huang Shijieb1994892014-02-24 18:37:37 +0800560 /* GigaDevice */
561 { "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64, SECT_4K) },
562 { "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128, SECT_4K) },
Rafał Miłeckifcc87a92014-12-16 22:46:56 +0100563 { "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800564
565 /* Intel/Numonyx -- xxxs33b */
566 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
567 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
568 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
569
Gabor Juhosb79c3322015-04-07 19:35:02 +0200570 /* ISSI */
571 { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
572
Huang Shijieb1994892014-02-24 18:37:37 +0800573 /* Macronix */
Gabor Juhos660b5b02015-04-07 19:35:01 +0200574 { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800575 { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
576 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
577 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
578 { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
579 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
580 { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
581 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
Mika Westerberg81a12092015-02-05 18:39:03 +0200582 { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800583 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
584 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
585 { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
586 { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
587 { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_QUAD_READ) },
588 { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
589
590 /* Micron */
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000591 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
Alexey Firago0db7fae2015-06-30 12:53:46 +0300592 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000593 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
594 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
595 { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
596 { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
597 { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
598 { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
Huang Shijieb1994892014-02-24 18:37:37 +0800599
600 /* PMC */
601 { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
602 { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
603 { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
604
605 /* Spansion -- single (large) sector size only, at least
606 * for the chips listed here (without boot sectors).
607 */
Geert Uytterhoeven9ab86992014-04-22 14:45:32 +0200608 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
Huang Shijieb1994892014-02-24 18:37:37 +0800609 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, 0) },
610 { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
611 { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
612 { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
613 { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
614 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
615 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
Rafał Miłeckic19900e2015-04-25 12:41:30 +0200616 { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
Huang Shijieb1994892014-02-24 18:37:37 +0800617 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
618 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
619 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
620 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
621 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
622 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
623 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
624 { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
625 { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K) },
626 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
Rafał Miłeckic19900e2015-04-25 12:41:30 +0200627 { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
Rafał Miłecki413780d2015-04-25 12:01:35 +0200628 { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
Antony Pavlovb4d97f02015-06-16 11:37:04 +0300629 { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800630
631 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
632 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
633 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
634 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
635 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
636 { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
637 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
638 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
639 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
640 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
Harini Katakamf02985b2014-10-21 13:37:59 +0200641 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
Huang Shijieb1994892014-02-24 18:37:37 +0800642
643 /* ST Microelectronics -- newer production may have feature updates */
644 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
645 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
646 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
647 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
648 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
649 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
650 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
651 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
652 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
Huang Shijieb1994892014-02-24 18:37:37 +0800653
654 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
655 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
656 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
657 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
658 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
659 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
660 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
661 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
662 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
663
664 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
665 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
666 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
667
668 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
669 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
670 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
671
672 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
673 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
674 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
675 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
676 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
Thomas Petazzonif2fabe12014-07-27 23:56:08 +0200677 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
Huang Shijieb1994892014-02-24 18:37:37 +0800678
679 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
Gabor Juhos40d19ab2015-03-26 23:58:02 +0100680 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800681 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
682 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
683 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
684 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
685 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
686 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
687 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
688 { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K) },
689 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
690 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
Mika Westerberge88e5672015-02-05 18:39:02 +0200691 { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K) },
Huang Shijieb1994892014-02-24 18:37:37 +0800692 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
693 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
694 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
695 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) },
696
697 /* Catalyst / On Semiconductor -- non-JEDEC */
698 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
699 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
700 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
701 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
702 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
703 { },
704};
705
706static const struct spi_device_id *spi_nor_read_id(struct spi_nor *nor)
707{
708 int tmp;
Huang Shijie09ffafb2014-11-06 07:34:01 +0100709 u8 id[SPI_NOR_MAX_ID_LEN];
Huang Shijieb1994892014-02-24 18:37:37 +0800710 struct flash_info *info;
711
Huang Shijie09ffafb2014-11-06 07:34:01 +0100712 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
Huang Shijieb1994892014-02-24 18:37:37 +0800713 if (tmp < 0) {
714 dev_dbg(nor->dev, " error %d reading JEDEC ID\n", tmp);
715 return ERR_PTR(tmp);
716 }
Huang Shijieb1994892014-02-24 18:37:37 +0800717
718 for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
719 info = (void *)spi_nor_ids[tmp].driver_data;
Huang Shijie09ffafb2014-11-06 07:34:01 +0100720 if (info->id_len) {
721 if (!memcmp(info->id, id, info->id_len))
Huang Shijieb1994892014-02-24 18:37:37 +0800722 return &spi_nor_ids[tmp];
723 }
724 }
Huang Shijie09ffafb2014-11-06 07:34:01 +0100725 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %2x, %2x\n",
726 id[0], id[1], id[2]);
Huang Shijieb1994892014-02-24 18:37:37 +0800727 return ERR_PTR(-ENODEV);
728}
729
Huang Shijieb1994892014-02-24 18:37:37 +0800730static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
731 size_t *retlen, u_char *buf)
732{
733 struct spi_nor *nor = mtd_to_spi_nor(mtd);
734 int ret;
735
736 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
737
738 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
739 if (ret)
740 return ret;
741
742 ret = nor->read(nor, from, len, retlen, buf);
743
744 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
745 return ret;
746}
747
748static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
749 size_t *retlen, const u_char *buf)
750{
751 struct spi_nor *nor = mtd_to_spi_nor(mtd);
752 size_t actual;
753 int ret;
754
755 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
756
757 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
758 if (ret)
759 return ret;
760
Huang Shijieb1994892014-02-24 18:37:37 +0800761 write_enable(nor);
762
763 nor->sst_write_second = false;
764
765 actual = to % 2;
766 /* Start write from odd address. */
767 if (actual) {
Brian Norrisb02e7f32014-04-08 18:15:31 -0700768 nor->program_opcode = SPINOR_OP_BP;
Huang Shijieb1994892014-02-24 18:37:37 +0800769
770 /* write one byte. */
771 nor->write(nor, to, 1, retlen, buf);
Brian Norrisb94ed082014-08-06 18:17:00 -0700772 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800773 if (ret)
774 goto time_out;
775 }
776 to += actual;
777
778 /* Write out most of the data here. */
779 for (; actual < len - 1; actual += 2) {
Brian Norrisb02e7f32014-04-08 18:15:31 -0700780 nor->program_opcode = SPINOR_OP_AAI_WP;
Huang Shijieb1994892014-02-24 18:37:37 +0800781
782 /* write two bytes. */
783 nor->write(nor, to, 2, retlen, buf + actual);
Brian Norrisb94ed082014-08-06 18:17:00 -0700784 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800785 if (ret)
786 goto time_out;
787 to += 2;
788 nor->sst_write_second = true;
789 }
790 nor->sst_write_second = false;
791
792 write_disable(nor);
Brian Norrisb94ed082014-08-06 18:17:00 -0700793 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800794 if (ret)
795 goto time_out;
796
797 /* Write out trailing byte if it exists. */
798 if (actual != len) {
799 write_enable(nor);
800
Brian Norrisb02e7f32014-04-08 18:15:31 -0700801 nor->program_opcode = SPINOR_OP_BP;
Huang Shijieb1994892014-02-24 18:37:37 +0800802 nor->write(nor, to, 1, retlen, buf + actual);
803
Brian Norrisb94ed082014-08-06 18:17:00 -0700804 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800805 if (ret)
806 goto time_out;
807 write_disable(nor);
808 }
809time_out:
810 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
811 return ret;
812}
813
814/*
815 * Write an address range to the nor chip. Data must be written in
816 * FLASH_PAGESIZE chunks. The address range may be any size provided
817 * it is within the physical boundaries.
818 */
819static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
820 size_t *retlen, const u_char *buf)
821{
822 struct spi_nor *nor = mtd_to_spi_nor(mtd);
823 u32 page_offset, page_size, i;
824 int ret;
825
826 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
827
828 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
829 if (ret)
830 return ret;
831
Huang Shijieb1994892014-02-24 18:37:37 +0800832 write_enable(nor);
833
834 page_offset = to & (nor->page_size - 1);
835
836 /* do all the bytes fit onto one page? */
837 if (page_offset + len <= nor->page_size) {
838 nor->write(nor, to, len, retlen, buf);
839 } else {
840 /* the size of data remaining on the first page */
841 page_size = nor->page_size - page_offset;
842 nor->write(nor, to, page_size, retlen, buf);
843
844 /* write everything in nor->page_size chunks */
845 for (i = page_size; i < len; i += page_size) {
846 page_size = len - i;
847 if (page_size > nor->page_size)
848 page_size = nor->page_size;
849
Brian Norrisb94ed082014-08-06 18:17:00 -0700850 ret = spi_nor_wait_till_ready(nor);
Brian Norris1d61dcb2014-08-06 18:16:56 -0700851 if (ret)
852 goto write_err;
853
Huang Shijieb1994892014-02-24 18:37:37 +0800854 write_enable(nor);
855
856 nor->write(nor, to + i, page_size, retlen, buf + i);
857 }
858 }
859
Brian Norrisdfa9c0c2014-08-06 18:16:57 -0700860 ret = spi_nor_wait_till_ready(nor);
Huang Shijieb1994892014-02-24 18:37:37 +0800861write_err:
862 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
Brian Norris1d61dcb2014-08-06 18:16:56 -0700863 return ret;
Huang Shijieb1994892014-02-24 18:37:37 +0800864}
865
866static int macronix_quad_enable(struct spi_nor *nor)
867{
868 int ret, val;
869
870 val = read_sr(nor);
871 write_enable(nor);
872
873 nor->cmd_buf[0] = val | SR_QUAD_EN_MX;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700874 nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800875
Brian Norrisb94ed082014-08-06 18:17:00 -0700876 if (spi_nor_wait_till_ready(nor))
Huang Shijieb1994892014-02-24 18:37:37 +0800877 return 1;
878
879 ret = read_sr(nor);
880 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
881 dev_err(nor->dev, "Macronix Quad bit not set\n");
882 return -EINVAL;
883 }
884
885 return 0;
886}
887
888/*
889 * Write status Register and configuration register with 2 bytes
890 * The first byte will be written to the status register, while the
891 * second byte will be written to the configuration register.
892 * Return negative if error occured.
893 */
894static int write_sr_cr(struct spi_nor *nor, u16 val)
895{
896 nor->cmd_buf[0] = val & 0xff;
897 nor->cmd_buf[1] = (val >> 8);
898
Brian Norrisb02e7f32014-04-08 18:15:31 -0700899 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2, 0);
Huang Shijieb1994892014-02-24 18:37:37 +0800900}
901
902static int spansion_quad_enable(struct spi_nor *nor)
903{
904 int ret;
905 int quad_en = CR_QUAD_EN_SPAN << 8;
906
907 write_enable(nor);
908
909 ret = write_sr_cr(nor, quad_en);
910 if (ret < 0) {
911 dev_err(nor->dev,
912 "error while writing configuration register\n");
913 return -EINVAL;
914 }
915
916 /* read back and check it */
917 ret = read_cr(nor);
918 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
919 dev_err(nor->dev, "Spansion Quad bit not set\n");
920 return -EINVAL;
921 }
922
923 return 0;
924}
925
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000926static int micron_quad_enable(struct spi_nor *nor)
927{
928 int ret;
929 u8 val;
930
931 ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
932 if (ret < 0) {
933 dev_err(nor->dev, "error %d reading EVCR\n", ret);
934 return ret;
935 }
936
937 write_enable(nor);
938
939 /* set EVCR, enable quad I/O */
940 nor->cmd_buf[0] = val & ~EVCR_QUAD_EN_MICRON;
941 ret = nor->write_reg(nor, SPINOR_OP_WD_EVCR, nor->cmd_buf, 1, 0);
942 if (ret < 0) {
943 dev_err(nor->dev, "error while writing EVCR register\n");
944 return ret;
945 }
946
947 ret = spi_nor_wait_till_ready(nor);
948 if (ret)
949 return ret;
950
951 /* read EVCR and check it */
952 ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
953 if (ret < 0) {
954 dev_err(nor->dev, "error %d reading EVCR\n", ret);
955 return ret;
956 }
957 if (val & EVCR_QUAD_EN_MICRON) {
958 dev_err(nor->dev, "Micron EVCR Quad bit not clear\n");
959 return -EINVAL;
960 }
961
962 return 0;
963}
964
Huang Shijied928a252014-11-06 11:24:33 +0800965static int set_quad_mode(struct spi_nor *nor, struct flash_info *info)
Huang Shijieb1994892014-02-24 18:37:37 +0800966{
967 int status;
968
Huang Shijied928a252014-11-06 11:24:33 +0800969 switch (JEDEC_MFR(info)) {
Huang Shijieb1994892014-02-24 18:37:37 +0800970 case CFI_MFR_MACRONIX:
971 status = macronix_quad_enable(nor);
972 if (status) {
973 dev_err(nor->dev, "Macronix quad-read not enabled\n");
974 return -EINVAL;
975 }
976 return status;
Bean Huo 霍斌斌 (beanhuo)548cd3a2014-12-17 07:35:45 +0000977 case CFI_MFR_ST:
978 status = micron_quad_enable(nor);
979 if (status) {
980 dev_err(nor->dev, "Micron quad-read not enabled\n");
981 return -EINVAL;
982 }
983 return status;
Huang Shijieb1994892014-02-24 18:37:37 +0800984 default:
985 status = spansion_quad_enable(nor);
986 if (status) {
987 dev_err(nor->dev, "Spansion quad-read not enabled\n");
988 return -EINVAL;
989 }
990 return status;
991 }
992}
993
994static int spi_nor_check(struct spi_nor *nor)
995{
996 if (!nor->dev || !nor->read || !nor->write ||
997 !nor->read_reg || !nor->write_reg || !nor->erase) {
998 pr_err("spi-nor: please fill all the necessary fields!\n");
999 return -EINVAL;
1000 }
1001
Huang Shijieb1994892014-02-24 18:37:37 +08001002 return 0;
1003}
1004
Ben Hutchings70f3ce02014-09-29 11:47:54 +02001005int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
Huang Shijieb1994892014-02-24 18:37:37 +08001006{
Ben Hutchings70f3ce02014-09-29 11:47:54 +02001007 const struct spi_device_id *id = NULL;
Huang Shijieb1994892014-02-24 18:37:37 +08001008 struct flash_info *info;
Huang Shijieb1994892014-02-24 18:37:37 +08001009 struct device *dev = nor->dev;
1010 struct mtd_info *mtd = nor->mtd;
1011 struct device_node *np = dev->of_node;
1012 int ret;
1013 int i;
1014
1015 ret = spi_nor_check(nor);
1016 if (ret)
1017 return ret;
1018
Brian Norris43163022015-05-19 14:38:22 -07001019 if (name)
Rafał Miłecki58c81952014-12-01 09:42:16 +01001020 id = spi_nor_match_id(name);
Brian Norris43163022015-05-19 14:38:22 -07001021 /* Try to auto-detect if chip name wasn't specified or not found */
1022 if (!id)
1023 id = spi_nor_read_id(nor);
Rafał Miłecki58c81952014-12-01 09:42:16 +01001024 if (IS_ERR_OR_NULL(id))
Ben Hutchings70f3ce02014-09-29 11:47:54 +02001025 return -ENOENT;
1026
Huang Shijieb1994892014-02-24 18:37:37 +08001027 info = (void *)id->driver_data;
1028
Rafał Miłecki58c81952014-12-01 09:42:16 +01001029 /*
1030 * If caller has specified name of flash model that can normally be
1031 * detected using JEDEC, let's verify it.
1032 */
1033 if (name && info->id_len) {
Huang Shijieb1994892014-02-24 18:37:37 +08001034 const struct spi_device_id *jid;
1035
Ben Hutchingse66fcf72014-09-30 03:15:04 +01001036 jid = spi_nor_read_id(nor);
Huang Shijieb1994892014-02-24 18:37:37 +08001037 if (IS_ERR(jid)) {
1038 return PTR_ERR(jid);
1039 } else if (jid != id) {
1040 /*
1041 * JEDEC knows better, so overwrite platform ID. We
1042 * can't trust partitions any longer, but we'll let
1043 * mtd apply them anyway, since some partitions may be
1044 * marked read-only, and we don't want to lose that
1045 * information, even if it's not 100% accurate.
1046 */
1047 dev_warn(dev, "found %s, expected %s\n",
1048 jid->name, id->name);
1049 id = jid;
1050 info = (void *)jid->driver_data;
1051 }
1052 }
1053
1054 mutex_init(&nor->lock);
1055
1056 /*
1057 * Atmel, SST and Intel/Numonyx serial nor tend to power
1058 * up with the software protection bits set
1059 */
1060
Huang Shijied928a252014-11-06 11:24:33 +08001061 if (JEDEC_MFR(info) == CFI_MFR_ATMEL ||
1062 JEDEC_MFR(info) == CFI_MFR_INTEL ||
1063 JEDEC_MFR(info) == CFI_MFR_SST) {
Huang Shijieb1994892014-02-24 18:37:37 +08001064 write_enable(nor);
1065 write_sr(nor, 0);
1066 }
1067
Rafał Miłecki32f1b7c2014-09-28 22:36:54 +02001068 if (!mtd->name)
Huang Shijieb1994892014-02-24 18:37:37 +08001069 mtd->name = dev_name(dev);
Huang Shijieb1994892014-02-24 18:37:37 +08001070 mtd->type = MTD_NORFLASH;
1071 mtd->writesize = 1;
1072 mtd->flags = MTD_CAP_NORFLASH;
1073 mtd->size = info->sector_size * info->n_sectors;
1074 mtd->_erase = spi_nor_erase;
1075 mtd->_read = spi_nor_read;
1076
1077 /* nor protection support for STmicro chips */
Huang Shijied928a252014-11-06 11:24:33 +08001078 if (JEDEC_MFR(info) == CFI_MFR_ST) {
Brian Norris8cc7f332015-03-13 00:38:39 -07001079 nor->flash_lock = stm_lock;
1080 nor->flash_unlock = stm_unlock;
1081 }
1082
1083 if (nor->flash_lock && nor->flash_unlock) {
Huang Shijieb1994892014-02-24 18:37:37 +08001084 mtd->_lock = spi_nor_lock;
1085 mtd->_unlock = spi_nor_unlock;
1086 }
1087
1088 /* sst nor chips use AAI word program */
1089 if (info->flags & SST_WRITE)
1090 mtd->_write = sst_write;
1091 else
1092 mtd->_write = spi_nor_write;
1093
Brian Norris51983b72014-09-10 00:26:16 -07001094 if (info->flags & USE_FSR)
1095 nor->flags |= SNOR_F_USE_FSR;
grmoore@altera.comc14dedd2014-04-29 10:29:51 -05001096
Rafał Miłecki57cf26c2014-08-17 11:27:26 +02001097#ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
Huang Shijieb1994892014-02-24 18:37:37 +08001098 /* prefer "small sector" erase if possible */
1099 if (info->flags & SECT_4K) {
Brian Norrisb02e7f32014-04-08 18:15:31 -07001100 nor->erase_opcode = SPINOR_OP_BE_4K;
Huang Shijieb1994892014-02-24 18:37:37 +08001101 mtd->erasesize = 4096;
1102 } else if (info->flags & SECT_4K_PMC) {
Brian Norrisb02e7f32014-04-08 18:15:31 -07001103 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
Huang Shijieb1994892014-02-24 18:37:37 +08001104 mtd->erasesize = 4096;
Rafał Miłecki57cf26c2014-08-17 11:27:26 +02001105 } else
1106#endif
1107 {
Brian Norrisb02e7f32014-04-08 18:15:31 -07001108 nor->erase_opcode = SPINOR_OP_SE;
Huang Shijieb1994892014-02-24 18:37:37 +08001109 mtd->erasesize = info->sector_size;
1110 }
1111
1112 if (info->flags & SPI_NOR_NO_ERASE)
1113 mtd->flags |= MTD_NO_ERASE;
1114
1115 mtd->dev.parent = dev;
1116 nor->page_size = info->page_size;
1117 mtd->writebufsize = nor->page_size;
1118
1119 if (np) {
1120 /* If we were instantiated by DT, use it */
1121 if (of_property_read_bool(np, "m25p,fast-read"))
1122 nor->flash_read = SPI_NOR_FAST;
1123 else
1124 nor->flash_read = SPI_NOR_NORMAL;
1125 } else {
1126 /* If we weren't instantiated by DT, default to fast-read */
1127 nor->flash_read = SPI_NOR_FAST;
1128 }
1129
1130 /* Some devices cannot do fast-read, no matter what DT tells us */
1131 if (info->flags & SPI_NOR_NO_FR)
1132 nor->flash_read = SPI_NOR_NORMAL;
1133
1134 /* Quad/Dual-read mode takes precedence over fast/normal */
1135 if (mode == SPI_NOR_QUAD && info->flags & SPI_NOR_QUAD_READ) {
Huang Shijied928a252014-11-06 11:24:33 +08001136 ret = set_quad_mode(nor, info);
Huang Shijieb1994892014-02-24 18:37:37 +08001137 if (ret) {
1138 dev_err(dev, "quad mode not supported\n");
1139 return ret;
1140 }
1141 nor->flash_read = SPI_NOR_QUAD;
1142 } else if (mode == SPI_NOR_DUAL && info->flags & SPI_NOR_DUAL_READ) {
1143 nor->flash_read = SPI_NOR_DUAL;
1144 }
1145
1146 /* Default commands */
1147 switch (nor->flash_read) {
1148 case SPI_NOR_QUAD:
Brian Norris58b89a12014-04-08 19:16:49 -07001149 nor->read_opcode = SPINOR_OP_READ_1_1_4;
Huang Shijieb1994892014-02-24 18:37:37 +08001150 break;
1151 case SPI_NOR_DUAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001152 nor->read_opcode = SPINOR_OP_READ_1_1_2;
Huang Shijieb1994892014-02-24 18:37:37 +08001153 break;
1154 case SPI_NOR_FAST:
Brian Norris58b89a12014-04-08 19:16:49 -07001155 nor->read_opcode = SPINOR_OP_READ_FAST;
Huang Shijieb1994892014-02-24 18:37:37 +08001156 break;
1157 case SPI_NOR_NORMAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001158 nor->read_opcode = SPINOR_OP_READ;
Huang Shijieb1994892014-02-24 18:37:37 +08001159 break;
1160 default:
1161 dev_err(dev, "No Read opcode defined\n");
1162 return -EINVAL;
1163 }
1164
Brian Norrisb02e7f32014-04-08 18:15:31 -07001165 nor->program_opcode = SPINOR_OP_PP;
Huang Shijieb1994892014-02-24 18:37:37 +08001166
1167 if (info->addr_width)
1168 nor->addr_width = info->addr_width;
1169 else if (mtd->size > 0x1000000) {
1170 /* enable 4-byte addressing if the device exceeds 16MiB */
1171 nor->addr_width = 4;
Huang Shijied928a252014-11-06 11:24:33 +08001172 if (JEDEC_MFR(info) == CFI_MFR_AMD) {
Huang Shijieb1994892014-02-24 18:37:37 +08001173 /* Dedicated 4-byte command set */
1174 switch (nor->flash_read) {
1175 case SPI_NOR_QUAD:
Brian Norris58b89a12014-04-08 19:16:49 -07001176 nor->read_opcode = SPINOR_OP_READ4_1_1_4;
Huang Shijieb1994892014-02-24 18:37:37 +08001177 break;
1178 case SPI_NOR_DUAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001179 nor->read_opcode = SPINOR_OP_READ4_1_1_2;
Huang Shijieb1994892014-02-24 18:37:37 +08001180 break;
1181 case SPI_NOR_FAST:
Brian Norris58b89a12014-04-08 19:16:49 -07001182 nor->read_opcode = SPINOR_OP_READ4_FAST;
Huang Shijieb1994892014-02-24 18:37:37 +08001183 break;
1184 case SPI_NOR_NORMAL:
Brian Norris58b89a12014-04-08 19:16:49 -07001185 nor->read_opcode = SPINOR_OP_READ4;
Huang Shijieb1994892014-02-24 18:37:37 +08001186 break;
1187 }
Brian Norrisb02e7f32014-04-08 18:15:31 -07001188 nor->program_opcode = SPINOR_OP_PP_4B;
Huang Shijieb1994892014-02-24 18:37:37 +08001189 /* No small sector erase for 4-byte command set */
Brian Norrisb02e7f32014-04-08 18:15:31 -07001190 nor->erase_opcode = SPINOR_OP_SE_4B;
Huang Shijieb1994892014-02-24 18:37:37 +08001191 mtd->erasesize = info->sector_size;
1192 } else
Huang Shijied928a252014-11-06 11:24:33 +08001193 set_4byte(nor, info, 1);
Huang Shijieb1994892014-02-24 18:37:37 +08001194 } else {
1195 nor->addr_width = 3;
1196 }
1197
1198 nor->read_dummy = spi_nor_read_dummy_cycles(nor);
1199
1200 dev_info(dev, "%s (%lld Kbytes)\n", id->name,
1201 (long long)mtd->size >> 10);
1202
1203 dev_dbg(dev,
1204 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
1205 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
1206 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
1207 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
1208
1209 if (mtd->numeraseregions)
1210 for (i = 0; i < mtd->numeraseregions; i++)
1211 dev_dbg(dev,
1212 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
1213 ".erasesize = 0x%.8x (%uKiB), "
1214 ".numblocks = %d }\n",
1215 i, (long long)mtd->eraseregions[i].offset,
1216 mtd->eraseregions[i].erasesize,
1217 mtd->eraseregions[i].erasesize / 1024,
1218 mtd->eraseregions[i].numblocks);
1219 return 0;
1220}
Brian Norrisb61834b2014-04-08 18:22:57 -07001221EXPORT_SYMBOL_GPL(spi_nor_scan);
Huang Shijieb1994892014-02-24 18:37:37 +08001222
Ben Hutchings70f3ce02014-09-29 11:47:54 +02001223static const struct spi_device_id *spi_nor_match_id(const char *name)
Huang Shijie0d8c11c2014-02-24 18:37:40 +08001224{
1225 const struct spi_device_id *id = spi_nor_ids;
1226
1227 while (id->name[0]) {
1228 if (!strcmp(name, id->name))
1229 return id;
1230 id++;
1231 }
1232 return NULL;
1233}
1234
Huang Shijieb1994892014-02-24 18:37:37 +08001235MODULE_LICENSE("GPL");
1236MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
1237MODULE_AUTHOR("Mike Lavender");
1238MODULE_DESCRIPTION("framework for SPI NOR");