1 --- a/drivers/mtd/spi-nor/fsl-quadspi.c
2 +++ b/drivers/mtd/spi-nor/fsl-quadspi.c
3 @@ -719,16 +719,10 @@ static int fsl_qspi_read(struct spi_nor
5 struct fsl_qspi *q = nor->priv;
6 u8 cmd = nor->read_opcode;
9 dev_dbg(q->dev, "cmd [%x],read from (0x%p, 0x%.8x, 0x%.8x),len:%d\n",
10 cmd, q->ahb_base, q->chip_base_addr, (unsigned int)from, len);
12 - /* Wait until the previous command is finished. */
13 - ret = nor->wait_till_ready(nor);
17 /* Read out the data directly from the AHB buffer.*/
18 memcpy(buf, q->ahb_base + q->chip_base_addr + from, len);
20 @@ -744,16 +738,6 @@ static int fsl_qspi_erase(struct spi_nor
21 dev_dbg(nor->dev, "%dKiB at 0x%08x:0x%08x\n",
22 nor->mtd->erasesize / 1024, q->chip_base_addr, (u32)offs);
24 - /* Wait until finished previous write command. */
25 - ret = nor->wait_till_ready(nor);
29 - /* Send write enable, then erase commands. */
30 - ret = nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0, 0);
34 ret = fsl_qspi_runcmd(q, nor->erase_opcode, offs, 0);
37 --- a/drivers/mtd/spi-nor/spi-nor.c
38 +++ b/drivers/mtd/spi-nor/spi-nor.c
39 @@ -163,81 +163,69 @@ static inline int set_4byte(struct spi_n
40 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1, 0);
44 -static int spi_nor_wait_till_ready(struct spi_nor *nor)
45 +static inline int spi_nor_sr_ready(struct spi_nor *nor)
47 - unsigned long deadline;
50 - deadline = jiffies + MAX_READY_WAIT_JIFFIES;
54 + int sr = read_sr(nor);
58 + return !(sr & SR_WIP);
64 - else if (!(sr & SR_WIP))
66 - } while (!time_after_eq(jiffies, deadline));
67 +static inline int spi_nor_fsr_ready(struct spi_nor *nor)
69 + int fsr = read_fsr(nor);
73 + return fsr & FSR_READY;
77 +static int spi_nor_ready(struct spi_nor *nor)
80 + sr = spi_nor_sr_ready(nor);
83 + fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
89 -static int spi_nor_wait_till_fsr_ready(struct spi_nor *nor)
91 + * Service routine to read status register until ready, or timeout occurs.
92 + * Returns non-zero if error.
94 +static int spi_nor_wait_till_ready(struct spi_nor *nor)
96 unsigned long deadline;
101 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
109 - } else if (!(sr & SR_WIP)) {
110 - fsr = read_fsr(nor);
113 - if (fsr & FSR_READY)
116 + ret = spi_nor_ready(nor);
121 } while (!time_after_eq(jiffies, deadline));
127 - * Service routine to read status register until ready, or timeout occurs.
128 - * Returns non-zero if error.
130 -static int wait_till_ready(struct spi_nor *nor)
132 - return nor->wait_till_ready(nor);
136 * Erase the whole flash memory
138 * Returns 0 if successful, non-zero otherwise.
140 static int erase_chip(struct spi_nor *nor)
144 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd->size >> 10));
146 - /* Wait until finished previous write command. */
147 - ret = wait_till_ready(nor);
151 - /* Send write enable, then erase commands. */
154 return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0, 0);
157 @@ -290,6 +278,8 @@ static int spi_nor_erase(struct mtd_info
163 /* whole-chip erase? */
164 if (len == mtd->size) {
165 if (erase_chip(nor)) {
166 @@ -297,6 +287,10 @@ static int spi_nor_erase(struct mtd_info
170 + ret = spi_nor_wait_till_ready(nor);
174 /* REVISIT in some cases we could speed up erasing large regions
175 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
176 * to use "small sector erase", but that's not always optimal.
177 @@ -312,9 +306,15 @@ static int spi_nor_erase(struct mtd_info
179 addr += mtd->erasesize;
180 len -= mtd->erasesize;
182 + ret = spi_nor_wait_till_ready(nor);
188 + write_disable(nor);
190 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
192 instr->state = MTD_ERASE_DONE;
193 @@ -339,11 +339,6 @@ static int spi_nor_lock(struct mtd_info
197 - /* Wait until finished previous command */
198 - ret = wait_till_ready(nor);
202 status_old = read_sr(nor);
204 if (offset < mtd->size - (mtd->size / 2))
205 @@ -386,11 +381,6 @@ static int spi_nor_unlock(struct mtd_inf
209 - /* Wait until finished previous command */
210 - ret = wait_till_ready(nor);
214 status_old = read_sr(nor);
216 if (offset+len > mtd->size - (mtd->size / 64))
217 @@ -702,11 +692,6 @@ static int sst_write(struct mtd_info *mt
221 - /* Wait until finished previous write command. */
222 - ret = wait_till_ready(nor);
228 nor->sst_write_second = false;
229 @@ -718,7 +703,7 @@ static int sst_write(struct mtd_info *mt
231 /* write one byte. */
232 nor->write(nor, to, 1, retlen, buf);
233 - ret = wait_till_ready(nor);
234 + ret = spi_nor_wait_till_ready(nor);
238 @@ -730,7 +715,7 @@ static int sst_write(struct mtd_info *mt
240 /* write two bytes. */
241 nor->write(nor, to, 2, retlen, buf + actual);
242 - ret = wait_till_ready(nor);
243 + ret = spi_nor_wait_till_ready(nor);
247 @@ -739,7 +724,7 @@ static int sst_write(struct mtd_info *mt
248 nor->sst_write_second = false;
251 - ret = wait_till_ready(nor);
252 + ret = spi_nor_wait_till_ready(nor);
256 @@ -750,7 +735,7 @@ static int sst_write(struct mtd_info *mt
257 nor->program_opcode = SPINOR_OP_BP;
258 nor->write(nor, to, 1, retlen, buf + actual);
260 - ret = wait_till_ready(nor);
261 + ret = spi_nor_wait_till_ready(nor);
265 @@ -778,11 +763,6 @@ static int spi_nor_write(struct mtd_info
269 - /* Wait until finished previous write command. */
270 - ret = wait_till_ready(nor);
276 page_offset = to & (nor->page_size - 1);
277 @@ -801,16 +781,20 @@ static int spi_nor_write(struct mtd_info
278 if (page_size > nor->page_size)
279 page_size = nor->page_size;
281 - wait_till_ready(nor);
282 + ret = spi_nor_wait_till_ready(nor);
288 nor->write(nor, to + i, page_size, retlen, buf + i);
292 + ret = spi_nor_wait_till_ready(nor);
294 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
299 static int macronix_quad_enable(struct spi_nor *nor)
300 @@ -823,7 +807,7 @@ static int macronix_quad_enable(struct s
301 nor->cmd_buf[0] = val | SR_QUAD_EN_MX;
302 nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1, 0);
304 - if (wait_till_ready(nor))
305 + if (spi_nor_wait_till_ready(nor))
309 @@ -905,8 +889,6 @@ static int spi_nor_check(struct spi_nor
312 nor->read_id = spi_nor_read_id;
313 - if (!nor->wait_till_ready)
314 - nor->wait_till_ready = spi_nor_wait_till_ready;
318 @@ -977,9 +959,8 @@ int spi_nor_scan(struct spi_nor *nor, co
320 mtd->_write = spi_nor_write;
322 - if ((info->flags & USE_FSR) &&
323 - nor->wait_till_ready == spi_nor_wait_till_ready)
324 - nor->wait_till_ready = spi_nor_wait_till_fsr_ready;
325 + if (info->flags & USE_FSR)
326 + nor->flags |= SNOR_F_USE_FSR;
328 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
329 /* prefer "small sector" erase if possible */
330 --- a/include/linux/mtd/spi-nor.h
331 +++ b/include/linux/mtd/spi-nor.h
332 @@ -116,6 +116,10 @@ enum spi_nor_ops {
336 +enum spi_nor_option_flags {
337 + SNOR_F_USE_FSR = BIT(0),
341 * struct spi_nor - Structure for defining a the SPI NOR layer
342 * @mtd: point to a mtd_info structure
343 @@ -129,6 +133,7 @@ enum spi_nor_ops {
344 * @program_opcode: the program opcode
345 * @flash_read: the mode of the read
346 * @sst_write_second: used by the SST write operation
347 + * @flags: flag options for the current SPI-NOR (SNOR_F_*)
348 * @cfg: used by the read_xfer/write_xfer
349 * @cmd_buf: used by the write_reg
350 * @prepare: [OPTIONAL] do some preparations for the
351 @@ -141,7 +146,6 @@ enum spi_nor_ops {
352 * @write_reg: [DRIVER-SPECIFIC] write data to the register
353 * @read_id: [REPLACEABLE] read out the ID data, and find
354 * the proper spi_device_id
355 - * @wait_till_ready: [REPLACEABLE] wait till the NOR becomes ready
356 * @read: [DRIVER-SPECIFIC] read data from the SPI NOR
357 * @write: [DRIVER-SPECIFIC] write data to the SPI NOR
358 * @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR
359 @@ -160,6 +164,7 @@ struct spi_nor {
361 enum read_mode flash_read;
362 bool sst_write_second;
364 struct spi_nor_xfer_cfg cfg;
365 u8 cmd_buf[SPI_NOR_MAX_CMD_SIZE];
367 @@ -173,7 +178,6 @@ struct spi_nor {
368 int (*write_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len,
370 const struct spi_device_id *(*read_id)(struct spi_nor *nor);
371 - int (*wait_till_ready)(struct spi_nor *nor);
373 int (*read)(struct spi_nor *nor, loff_t from,
374 size_t len, size_t *retlen, u_char *read_buf);