xref: /openbmc/u-boot/drivers/mtd/spi/spi-nor-core.c (revision 591e1cf0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with
4  * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
5  *
6  * Copyright (C) 2005, Intec Automation Inc.
7  * Copyright (C) 2014, Freescale Semiconductor, Inc.
8  *
9  * Synced from Linux v4.19
10  */
11 
12 #include <common.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/log2.h>
16 #include <linux/math64.h>
17 #include <linux/sizes.h>
18 
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/spi-nor.h>
21 #include <spi-mem.h>
22 #include <spi.h>
23 
24 #include "sf_internal.h"
25 
26 /* Define max times to check status register before we give up. */
27 
28 /*
29  * For everything but full-chip erase; probably could be much smaller, but kept
30  * around for safety for now
31  */
32 
33 #define HZ					CONFIG_SYS_HZ
34 
35 #define DEFAULT_READY_WAIT_JIFFIES		(40UL * HZ)
36 
37 /**
38  * spi_nor_setup_op() - Set up common properties of a spi-mem op.
39  * @nor:		pointer to a 'struct spi_nor'
40  * @op:			pointer to the 'struct spi_mem_op' whose properties
41  *			need to be initialized.
42  * @proto:		the protocol from which the properties need to be set.
43  */
spi_nor_setup_op(const struct spi_nor * nor,struct spi_mem_op * op,const enum spi_nor_protocol proto)44 void spi_nor_setup_op(const struct spi_nor *nor,
45 		      struct spi_mem_op *op,
46 		      const enum spi_nor_protocol proto)
47 {
48 	op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(proto);
49 
50 	if (op->addr.nbytes)
51 		op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto);
52 
53 	if (op->dummy.nbytes)
54 		op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto);
55 
56 	if (op->data.nbytes)
57 		op->data.buswidth = spi_nor_get_protocol_data_nbits(proto);
58 }
59 
spi_nor_read_write_reg(struct spi_nor * nor,struct spi_mem_op * op,void * buf)60 static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op
61 		*op, void *buf)
62 {
63 	if (op->data.dir == SPI_MEM_DATA_IN)
64 		op->data.buf.in = buf;
65 	else
66 		op->data.buf.out = buf;
67 	return spi_mem_exec_op(nor->spi, op);
68 }
69 
spi_nor_read_reg(struct spi_nor * nor,u8 code,u8 * val,int len)70 static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
71 {
72 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1),
73 					  SPI_MEM_OP_NO_ADDR,
74 					  SPI_MEM_OP_NO_DUMMY,
75 					  SPI_MEM_OP_DATA_IN(len, NULL, 1));
76 	int ret;
77 
78 	ret = spi_nor_read_write_reg(nor, &op, val);
79 	if (ret < 0)
80 		dev_dbg(&flash->spimem->spi->dev, "error %d reading %x\n", ret,
81 			code);
82 
83 	return ret;
84 }
85 
spi_nor_write_reg(struct spi_nor * nor,u8 opcode,u8 * buf,int len)86 static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
87 {
88 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1),
89 					  SPI_MEM_OP_NO_ADDR,
90 					  SPI_MEM_OP_NO_DUMMY,
91 					  SPI_MEM_OP_DATA_OUT(len, NULL, 1));
92 
93 	return spi_nor_read_write_reg(nor, &op, buf);
94 }
95 
96 #ifdef CONFIG_SPI_FLASH_SPANSION
spansion_read_any_reg(struct spi_nor * nor,u32 addr,u8 dummy,u8 * val)97 static int spansion_read_any_reg(struct spi_nor *nor, u32 addr, u8 dummy,
98 				 u8 *val)
99 {
100 	int ret;
101 	u8 dummy_ori;
102 	struct spi_mem_op op =
103 			SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDAR, 1),
104 				   SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
105 				   SPI_MEM_OP_DUMMY(dummy / 8, 1),
106 				   SPI_MEM_OP_DATA_IN(1, NULL, 1));
107 
108 	dummy_ori = nor->read_dummy;
109 	nor->read_dummy = dummy;
110 
111 	ret = spi_nor_read_write_reg(nor, &op, val);
112 
113 	nor->read_dummy = dummy_ori;
114 
115 	return ret;
116 }
117 
spansion_write_any_reg(struct spi_nor * nor,u32 addr,u8 val)118 static int spansion_write_any_reg(struct spi_nor *nor, u32 addr, u8 val)
119 {
120 	struct spi_mem_op op =
121 			SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRAR, 1),
122 				   SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
123 				   SPI_MEM_OP_NO_DUMMY,
124 				   SPI_MEM_OP_DATA_OUT(1, NULL, 1));
125 
126 	return spi_nor_read_write_reg(nor, &op, &val);
127 }
128 #endif
129 
spi_nor_read_data(struct spi_nor * nor,loff_t from,size_t len,u_char * buf)130 static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
131 				 u_char *buf)
132 {
133 	struct spi_mem_op op =
134 			SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
135 				   SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
136 				   SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
137 				   SPI_MEM_OP_DATA_IN(len, buf, 1));
138 	size_t remaining = len;
139 	int ret;
140 
141 	/* get transfer protocols. */
142 	op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
143 	op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
144 	op.dummy.buswidth = op.addr.buswidth;
145 	op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
146 
147 	/* convert the dummy cycles to the number of bytes */
148 	op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
149 
150 	while (remaining) {
151 		op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
152 		ret = spi_mem_adjust_op_size(nor->spi, &op);
153 		if (ret)
154 			return ret;
155 
156 		ret = spi_mem_exec_op(nor->spi, &op);
157 		if (ret)
158 			return ret;
159 
160 		op.addr.val += op.data.nbytes;
161 		remaining -= op.data.nbytes;
162 		op.data.buf.in += op.data.nbytes;
163 	}
164 
165 	return len;
166 }
167 
spi_nor_write_data(struct spi_nor * nor,loff_t to,size_t len,const u_char * buf)168 static ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
169 				  const u_char *buf)
170 {
171 	struct spi_mem_op op =
172 			SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1),
173 				   SPI_MEM_OP_ADDR(nor->addr_width, to, 1),
174 				   SPI_MEM_OP_NO_DUMMY,
175 				   SPI_MEM_OP_DATA_OUT(len, buf, 1));
176 	size_t remaining = len;
177 	int ret;
178 
179 	/* get transfer protocols. */
180 	op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto);
181 	op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto);
182 	op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
183 
184 	if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
185 		op.addr.nbytes = 0;
186 
187 	while (remaining) {
188 		op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
189 		ret = spi_mem_adjust_op_size(nor->spi, &op);
190 		if (ret)
191 			return ret;
192 
193 		ret = spi_mem_exec_op(nor->spi, &op);
194 		if (ret)
195 			return ret;
196 
197 		op.addr.val += op.data.nbytes;
198 		remaining -= op.data.nbytes;
199 		op.data.buf.out += op.data.nbytes;
200 	}
201 
202 	return len;
203 }
204 
205 /*
206  * Read the status register, returning its value in the location
207  * Return the status register value.
208  * Returns negative if error occurred.
209  */
read_sr(struct spi_nor * nor)210 static int read_sr(struct spi_nor *nor)
211 {
212 	int ret;
213 	u8 val;
214 
215 	ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
216 	if (ret < 0) {
217 		pr_debug("error %d reading SR\n", (int)ret);
218 		return ret;
219 	}
220 
221 	return val;
222 }
223 
read_winbond_sr2(struct spi_nor * nor)224 static int read_winbond_sr2(struct spi_nor *nor)
225 {
226 	int ret;
227 	u8 val;
228 
229 	ret = nor->read_reg(nor, SPINOR_OP_WINBOND_RDSR2, &val, 1);
230 	if (ret < 0) {
231 		pr_debug("error %d reading SR2\n", (int)ret);
232 		return ret;
233 	}
234 
235 	return val;
236 }
237 
238 /*
239  * Read the flag status register, returning its value in the location
240  * Return the status register value.
241  * Returns negative if error occurred.
242  */
read_fsr(struct spi_nor * nor)243 static int read_fsr(struct spi_nor *nor)
244 {
245 	int ret;
246 	u8 val;
247 
248 	ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
249 	if (ret < 0) {
250 		pr_debug("error %d reading FSR\n", ret);
251 		return ret;
252 	}
253 
254 	return val;
255 }
256 
257 /*
258  * Read configuration register, returning its value in the
259  * location. Return the configuration register value.
260  * Returns negative if error occurred.
261  */
262 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
read_cr(struct spi_nor * nor)263 static int read_cr(struct spi_nor *nor)
264 {
265 	int ret;
266 	u8 val;
267 
268 	ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
269 	if (ret < 0) {
270 		dev_dbg(nor->dev, "error %d reading CR\n", ret);
271 		return ret;
272 	}
273 
274 	return val;
275 }
276 #endif
277 
278 /*
279  * Write status register 1 byte
280  * Returns negative if error occurred.
281  */
write_sr(struct spi_nor * nor,u8 val)282 static int write_sr(struct spi_nor *nor, u8 val)
283 {
284 	nor->cmd_buf[0] = val;
285 	return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
286 }
287 
write_winbond_sr2(struct spi_nor * nor,u8 val)288 static int write_winbond_sr2(struct spi_nor *nor, u8 val)
289 {
290 	nor->cmd_buf[0] = val;
291 	return nor->write_reg(nor, SPINOR_OP_WINBOND_WRSR2, nor->cmd_buf, 1);
292 }
293 
294 /*
295  * Set write enable latch with Write Enable command.
296  * Returns negative if error occurred.
297  */
write_enable(struct spi_nor * nor)298 static int write_enable(struct spi_nor *nor)
299 {
300 	return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
301 }
302 
303 /*
304  * Send write disable instruction to the chip.
305  */
write_disable(struct spi_nor * nor)306 static int write_disable(struct spi_nor *nor)
307 {
308 	return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
309 }
310 
mtd_to_spi_nor(struct mtd_info * mtd)311 static struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
312 {
313 	return mtd->priv;
314 }
315 
316 #ifndef CONFIG_SPI_FLASH_BAR
spi_nor_convert_opcode(u8 opcode,const u8 table[][2],size_t size)317 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
318 {
319 	size_t i;
320 
321 	for (i = 0; i < size; i++)
322 		if (table[i][0] == opcode)
323 			return table[i][1];
324 
325 	/* No conversion found, keep input op code. */
326 	return opcode;
327 }
328 
spi_nor_convert_3to4_read(u8 opcode)329 static u8 spi_nor_convert_3to4_read(u8 opcode)
330 {
331 	static const u8 spi_nor_3to4_read[][2] = {
332 		{ SPINOR_OP_READ,	SPINOR_OP_READ_4B },
333 		{ SPINOR_OP_READ_FAST,	SPINOR_OP_READ_FAST_4B },
334 		{ SPINOR_OP_READ_1_1_2,	SPINOR_OP_READ_1_1_2_4B },
335 		{ SPINOR_OP_READ_1_2_2,	SPINOR_OP_READ_1_2_2_4B },
336 		{ SPINOR_OP_READ_1_1_4,	SPINOR_OP_READ_1_1_4_4B },
337 		{ SPINOR_OP_READ_1_4_4,	SPINOR_OP_READ_1_4_4_4B },
338 
339 		{ SPINOR_OP_READ_1_1_1_DTR,	SPINOR_OP_READ_1_1_1_DTR_4B },
340 		{ SPINOR_OP_READ_1_2_2_DTR,	SPINOR_OP_READ_1_2_2_DTR_4B },
341 		{ SPINOR_OP_READ_1_4_4_DTR,	SPINOR_OP_READ_1_4_4_DTR_4B },
342 	};
343 
344 	return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
345 				      ARRAY_SIZE(spi_nor_3to4_read));
346 }
347 
spi_nor_convert_3to4_program(u8 opcode)348 static u8 spi_nor_convert_3to4_program(u8 opcode)
349 {
350 	static const u8 spi_nor_3to4_program[][2] = {
351 		{ SPINOR_OP_PP,		SPINOR_OP_PP_4B },
352 		{ SPINOR_OP_PP_1_1_4,	SPINOR_OP_PP_1_1_4_4B },
353 		{ SPINOR_OP_PP_1_4_4,	SPINOR_OP_PP_1_4_4_4B },
354 	};
355 
356 	return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
357 				      ARRAY_SIZE(spi_nor_3to4_program));
358 }
359 
spi_nor_convert_3to4_erase(u8 opcode)360 static u8 spi_nor_convert_3to4_erase(u8 opcode)
361 {
362 	static const u8 spi_nor_3to4_erase[][2] = {
363 		{ SPINOR_OP_BE_4K,	SPINOR_OP_BE_4K_4B },
364 		{ SPINOR_OP_BE_32K,	SPINOR_OP_BE_32K_4B },
365 		{ SPINOR_OP_SE,		SPINOR_OP_SE_4B },
366 	};
367 
368 	return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
369 				      ARRAY_SIZE(spi_nor_3to4_erase));
370 }
371 
spi_nor_set_4byte_opcodes(struct spi_nor * nor,const struct flash_info * info)372 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
373 				      const struct flash_info *info)
374 {
375 	/* Do some manufacturer fixups first */
376 	switch (JEDEC_MFR(info)) {
377 	case SNOR_MFR_SPANSION:
378 	case SNOR_MFR_CYPRESS:
379 		/* No small sector erase for 4-byte command set */
380 		nor->erase_opcode = SPINOR_OP_SE;
381 		nor->mtd.erasesize = info->sector_size;
382 		break;
383 
384 	default:
385 		break;
386 	}
387 
388 	nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
389 	nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
390 	nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
391 }
392 #endif /* !CONFIG_SPI_FLASH_BAR */
393 
394 /* Enable/disable 4-byte addressing mode. */
set_4byte(struct spi_nor * nor,const struct flash_info * info,int enable)395 static int set_4byte(struct spi_nor *nor, const struct flash_info *info,
396 		     int enable)
397 {
398 	int status;
399 	bool need_wren = false;
400 	u8 cmd;
401 
402 	switch (JEDEC_MFR(info)) {
403 	case SNOR_MFR_ST:
404 	case SNOR_MFR_MICRON:
405 		/* Some Micron need WREN command; all will accept it */
406 		need_wren = true;
407 	case SNOR_MFR_MACRONIX:
408 	case SNOR_MFR_WINBOND:
409 	case SNOR_MFR_GIGADEVICE:
410 	case SNOR_MFR_ISSI:
411 	case SNOR_MFR_CYPRESS:
412 		if (need_wren)
413 			write_enable(nor);
414 
415 		cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
416 		status = nor->write_reg(nor, cmd, NULL, 0);
417 		if (need_wren)
418 			write_disable(nor);
419 
420 		if (!status && !enable &&
421 		    JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
422 			/*
423 			 * On Winbond W25Q256FV, leaving 4byte mode causes
424 			 * the Extended Address Register to be set to 1, so all
425 			 * 3-byte-address reads come from the second 16M.
426 			 * We must clear the register to enable normal behavior.
427 			 */
428 			write_enable(nor);
429 			nor->cmd_buf[0] = 0;
430 			nor->write_reg(nor, SPINOR_OP_WREAR, nor->cmd_buf, 1);
431 			write_disable(nor);
432 		}
433 
434 		return status;
435 	default:
436 		/* Spansion style */
437 		nor->cmd_buf[0] = enable << 7;
438 		return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
439 	}
440 }
441 
spi_nor_sr_ready(struct spi_nor * nor)442 static int spi_nor_sr_ready(struct spi_nor *nor)
443 {
444 	int sr = read_sr(nor);
445 
446 	if (sr < 0)
447 		return sr;
448 
449 	if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) {
450 		if (sr & SR_E_ERR)
451 			dev_dbg(nor->dev, "Erase Error occurred\n");
452 		else
453 			dev_dbg(nor->dev, "Programming Error occurred\n");
454 
455 		nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
456 		return -EIO;
457 	}
458 
459 	return !(sr & SR_WIP);
460 }
461 
spi_nor_fsr_ready(struct spi_nor * nor)462 static int spi_nor_fsr_ready(struct spi_nor *nor)
463 {
464 	int fsr = read_fsr(nor);
465 
466 	if (fsr < 0)
467 		return fsr;
468 
469 	if (fsr & (FSR_E_ERR | FSR_P_ERR)) {
470 		if (fsr & FSR_E_ERR)
471 			dev_dbg(nor->dev, "Erase operation failed.\n");
472 		else
473 			dev_dbg(nor->dev, "Program operation failed.\n");
474 
475 		if (fsr & FSR_PT_ERR)
476 			dev_dbg(nor->dev,
477 				"Attempted to modify a protected sector.\n");
478 
479 		nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0);
480 		return -EIO;
481 	}
482 
483 	return fsr & FSR_READY;
484 }
485 
spi_nor_ready(struct spi_nor * nor)486 static int spi_nor_ready(struct spi_nor *nor)
487 {
488 	int sr, fsr;
489 
490 	sr = spi_nor_sr_ready(nor);
491 	if (sr < 0)
492 		return sr;
493 	fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
494 	if (fsr < 0)
495 		return fsr;
496 	return sr && fsr;
497 }
498 
499 /*
500  * Service routine to read status register until ready, or timeout occurs.
501  * Returns non-zero if error.
502  */
spi_nor_wait_till_ready_with_timeout(struct spi_nor * nor,unsigned long timeout)503 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
504 						unsigned long timeout)
505 {
506 	unsigned long timebase;
507 	int ret;
508 
509 	timebase = get_timer(0);
510 
511 	while (get_timer(timebase) < timeout) {
512 		ret = spi_nor_ready(nor);
513 		if (ret < 0)
514 			return ret;
515 		if (ret)
516 			return 0;
517 	}
518 
519 	dev_err(nor->dev, "flash operation timed out\n");
520 
521 	return -ETIMEDOUT;
522 }
523 
spi_nor_wait_till_ready(struct spi_nor * nor)524 static int spi_nor_wait_till_ready(struct spi_nor *nor)
525 {
526 	return spi_nor_wait_till_ready_with_timeout(nor,
527 						    DEFAULT_READY_WAIT_JIFFIES);
528 }
529 
530 #ifdef CONFIG_SPI_FLASH_BAR
531 /*
532  * This "clean_bar" is necessary in a situation when one was accessing
533  * spi flash memory > 16 MiB by using Bank Address Register's BA24 bit.
534  *
535  * After it the BA24 bit shall be cleared to allow access to correct
536  * memory region after SW reset (by calling "reset" command).
537  *
538  * Otherwise, the BA24 bit may be left set and then after reset, the
539  * ROM would read/write/erase SPL from 16 MiB * bank_sel address.
540  */
clean_bar(struct spi_nor * nor)541 static int clean_bar(struct spi_nor *nor)
542 {
543 	u8 cmd, bank_sel = 0;
544 
545 	if (nor->bank_curr == 0)
546 		return 0;
547 	cmd = nor->bank_write_cmd;
548 	nor->bank_curr = 0;
549 	write_enable(nor);
550 
551 	return nor->write_reg(nor, cmd, &bank_sel, 1);
552 }
553 
write_bar(struct spi_nor * nor,u32 offset)554 static int write_bar(struct spi_nor *nor, u32 offset)
555 {
556 	u8 cmd, bank_sel;
557 	int ret;
558 
559 	bank_sel = offset / SZ_16M;
560 	if (bank_sel == nor->bank_curr)
561 		goto bar_end;
562 
563 	cmd = nor->bank_write_cmd;
564 	write_enable(nor);
565 	ret = nor->write_reg(nor, cmd, &bank_sel, 1);
566 	if (ret < 0) {
567 		debug("SF: fail to write bank register\n");
568 		return ret;
569 	}
570 
571 bar_end:
572 	nor->bank_curr = bank_sel;
573 	return nor->bank_curr;
574 }
575 
read_bar(struct spi_nor * nor,const struct flash_info * info)576 static int read_bar(struct spi_nor *nor, const struct flash_info *info)
577 {
578 	u8 curr_bank = 0;
579 	int ret;
580 
581 	switch (JEDEC_MFR(info)) {
582 	case SNOR_MFR_SPANSION:
583 		nor->bank_read_cmd = SPINOR_OP_BRRD;
584 		nor->bank_write_cmd = SPINOR_OP_BRWR;
585 		break;
586 	default:
587 		nor->bank_read_cmd = SPINOR_OP_RDEAR;
588 		nor->bank_write_cmd = SPINOR_OP_WREAR;
589 	}
590 
591 	ret = nor->read_reg(nor, nor->bank_read_cmd,
592 				    &curr_bank, 1);
593 	if (ret) {
594 		debug("SF: fail to read bank addr register\n");
595 		return ret;
596 	}
597 	nor->bank_curr = curr_bank;
598 
599 	return 0;
600 }
601 #endif
602 
603 /*
604  * Initiate the erasure of a single sector
605  */
spi_nor_erase_sector(struct spi_nor * nor,u32 addr)606 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
607 {
608 	u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
609 	int i;
610 	int ret = 0;
611 
612 	if (nor->erase)
613 		return nor->erase(nor, addr);
614 
615 	/*
616 	 * Default implementation, if driver doesn't have a specialized HW
617 	 * control
618 	 */
619 	for (i = nor->addr_width - 1; i >= 0; i--) {
620 		buf[i] = addr & 0xff;
621 		addr >>= 8;
622 	}
623 
624 	ret = nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
625 	if (ret)
626 		return ret;
627 
628 	return nor->mtd.erasesize;
629 }
630 
631 /*
632  * Erase an address range on the nor chip.  The address range may extend
633  * one or more erase sectors.  Return an error is there is a problem erasing.
634  */
spi_nor_erase(struct mtd_info * mtd,struct erase_info * instr)635 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
636 {
637 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
638 	u32 addr, len, rem;
639 	int ret;
640 
641 	dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
642 		(long long)instr->len);
643 
644 	div_u64_rem(instr->len, mtd->erasesize, &rem);
645 	if (rem)
646 		return -EINVAL;
647 
648 	addr = instr->addr;
649 	len = instr->len;
650 
651 	while (len) {
652 #ifdef CONFIG_SPI_FLASH_BAR
653 		ret = write_bar(nor, addr);
654 		if (ret < 0)
655 			return ret;
656 #endif
657 		write_enable(nor);
658 
659 		ret = spi_nor_erase_sector(nor, addr);
660 		if (ret < 0)
661 			goto erase_err;
662 
663 		addr += ret;
664 		len -= ret;
665 
666 		ret = spi_nor_wait_till_ready(nor);
667 		if (ret)
668 			goto erase_err;
669 	}
670 
671 erase_err:
672 #ifdef CONFIG_SPI_FLASH_BAR
673 	ret = clean_bar(nor);
674 #endif
675 	write_disable(nor);
676 
677 	return ret;
678 }
679 
680 #ifdef CONFIG_SPI_FLASH_SPANSION
681 /**
682  * spansion_erase_non_uniform() - erase non-uniform sectors for Spansion/Cypress
683  *                                chips
684  * @nor:	pointer to a 'struct spi_nor'
685  * @addr:	address of the sector to erase
686  * @opcode_4k:	opcode for 4K sector erase
687  * @ovlsz_top:	size of overlaid portion at the top address
688  * @ovlsz_btm:	size of overlaid portion at the bottom address
689  *
690  * Erase an address range on the nor chip that can contain 4KB sectors overlaid
691  * on top and/or bottom. The appropriate erase opcode and size are chosen by
692  * address to erase and size of overlaid portion.
693  *
694  * Return: number of bytes erased on success, -errno otherwise.
695  */
spansion_erase_non_uniform(struct spi_nor * nor,u32 addr,u8 opcode_4k,u32 ovlsz_top,u32 ovlsz_btm)696 static int spansion_erase_non_uniform(struct spi_nor *nor, u32 addr,
697 				      u8 opcode_4k, u32 ovlsz_top,
698 				      u32 ovlsz_btm)
699 {
700 	struct spi_mem_op op =
701 		SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 0),
702 			   SPI_MEM_OP_ADDR(nor->addr_width, addr, 0),
703 			   SPI_MEM_OP_NO_DUMMY,
704 			   SPI_MEM_OP_NO_DATA);
705 	struct mtd_info *mtd = &nor->mtd;
706 	u32 erasesize;
707 	int ret;
708 
709 	/* 4KB sectors */
710 	if (op.addr.val < ovlsz_btm ||
711 	    op.addr.val >= mtd->size - ovlsz_top) {
712 		op.cmd.opcode = opcode_4k;
713 		erasesize = SZ_4K;
714 
715 	/* Non-overlaid portion in the normal sector at the bottom */
716 	} else if (op.addr.val == ovlsz_btm) {
717 		op.cmd.opcode = nor->erase_opcode;
718 		erasesize = mtd->erasesize - ovlsz_btm;
719 
720 	/* Non-overlaid portion in the normal sector at the top */
721 	} else if (op.addr.val == mtd->size - mtd->erasesize) {
722 		op.cmd.opcode = nor->erase_opcode;
723 		erasesize = mtd->erasesize - ovlsz_top;
724 
725 	/* Normal sectors */
726 	} else {
727 		op.cmd.opcode = nor->erase_opcode;
728 		erasesize = mtd->erasesize;
729 	}
730 
731 	spi_nor_setup_op(nor, &op, nor->write_proto);
732 
733 	ret = spi_mem_exec_op(nor->spi, &op);
734 	if (ret)
735 		return ret;
736 
737 	return erasesize;
738 }
739 
s25hx_t_erase_non_uniform(struct spi_nor * nor,loff_t addr)740 static int s25hx_t_erase_non_uniform(struct spi_nor *nor, loff_t addr)
741 {
742 	/* Support 32 x 4KB sectors at bottom */
743 	return spansion_erase_non_uniform(nor, addr, SPINOR_OP_BE_4K_4B, 0,
744 					  SZ_128K);
745 }
746 
s25hx_t_setup(struct spi_nor * nor,const struct flash_info * info)747 static int s25hx_t_setup(struct spi_nor *nor, const struct flash_info *info)
748 {
749 	int ret;
750 	u8 cfr3v;
751 
752 #ifdef CONFIG_SPI_FLASH_BAR
753 	return -ENOTSUPP; /* Bank Address Register is not supported */
754 #endif
755 	/*
756 	 * Read CFR3V to check if uniform sector is selected. If not, assign an
757 	 * erase hook that supports non-uniform erase.
758 	 */
759 	ret = spansion_read_any_reg(nor, SPINOR_REG_ADDR_CFR3V, 0, &cfr3v);
760 	if (ret)
761 		return ret;
762 
763 	if (!(cfr3v & CFR3V_UNHYSA))
764 		nor->erase = s25hx_t_erase_non_uniform;
765 
766 	return 0;
767 }
768 #endif
769 
micron_read_nvcr(struct spi_nor * nor)770 static int micron_read_nvcr(struct spi_nor *nor)
771 {
772 	int ret;
773 	int val;
774 
775 	ret = nor->read_reg(nor, SPINOR_OP_MICRON_RDNVCR, (u8 *)&val, 2);
776 	if (ret < 0) {
777 		dev_err(nor->dev, "[Micron] error %d reading NVCR\n", ret);
778 		return ret;
779 	}
780 
781 	return val;
782 }
783 
micron_write_nvcr(struct spi_nor * nor,int val)784 static int micron_write_nvcr(struct spi_nor *nor, int val)
785 {
786 	int ret;
787 
788 	write_enable(nor);
789 
790 	nor->cmd_buf[0] = val & 0xff;
791 	nor->cmd_buf[1] = (val >> 8) & 0xff;
792 
793 	ret = nor->write_reg(nor, SPINOR_OP_MICRON_WRNVCR, nor->cmd_buf, 2);
794 	if (ret < 0) {
795 		dev_err(nor->dev,
796 			"[Micron] error while writing configuration register\n");
797 		return -EINVAL;
798 	}
799 
800 	ret = spi_nor_wait_till_ready(nor);
801 	if (ret) {
802 		dev_err(nor->dev,
803 			"[Micron] timeout while writing configuration register\n");
804 		return ret;
805 	}
806 
807 	return 0;
808 }
809 
micron_read_cr_quad_enable(struct spi_nor * nor)810 static int micron_read_cr_quad_enable(struct spi_nor *nor)
811 {
812 	int ret;
813 
814 	/* Check current Quad Enable bit value. */
815 	ret = micron_read_nvcr(nor);
816 	if (ret < 0) {
817 		dev_err(dev, "[Micron] error while reading nonvolatile configuration register\n");
818 		return -EINVAL;
819 	}
820 
821 	if ((ret & MICRON_RST_HOLD_CTRL) == 0)
822 		return 0;
823 
824 	ret &= ~MICRON_RST_HOLD_CTRL;
825 
826 	/* Keep the current value of the Status Register. */
827 	ret = micron_write_nvcr(nor, ret);
828 	if (ret < 0) {
829 		dev_err(dev, "[Micron] error while writing nonvolatile configuration register\n");
830 		return -EINVAL;
831 	}
832 
833 	ret = micron_read_nvcr(nor);
834 	if (ret > 0 && (ret & MICRON_RST_HOLD_CTRL)) {
835 		dev_err(nor->dev, "[Micron] Quad bit not set\n");
836 		return -EINVAL;
837 	}
838 
839 	return 0;
840 }
841 
cypress_s25hx_t(const struct flash_info * info)842 static bool cypress_s25hx_t(const struct flash_info *info)
843 {
844 	if (JEDEC_MFR(info) == SNOR_MFR_CYPRESS) {
845 		switch (info->id[1]) {
846 		case 0x2a: /* S25HL (QSPI, 3.3V) */
847 		case 0x2b: /* S25HS (QSPI, 1.8V) */
848 			return true;
849 			break;
850 
851 		default:
852 			break;
853 		}
854 	}
855 
856 	return false;
857 }
858 
859 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
860 /* Write status register and ensure bits in mask match written values */
write_sr_and_check(struct spi_nor * nor,u8 status_new,u8 mask)861 static int write_sr_and_check(struct spi_nor *nor, u8 status_new, u8 mask)
862 {
863 	int ret;
864 
865 	write_enable(nor);
866 	ret = write_sr(nor, status_new);
867 	if (ret)
868 		return ret;
869 
870 	ret = spi_nor_wait_till_ready(nor);
871 	if (ret)
872 		return ret;
873 
874 	ret = read_sr(nor);
875 	if (ret < 0)
876 		return ret;
877 
878 	return ((ret & mask) != (status_new & mask)) ? -EIO : 0;
879 }
880 
stm_get_locked_range(struct spi_nor * nor,u8 sr,loff_t * ofs,uint64_t * len)881 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
882 				 uint64_t *len)
883 {
884 	struct mtd_info *mtd = &nor->mtd;
885 	u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
886 	int shift = ffs(mask) - 1;
887 	int pow;
888 
889 	if (!(sr & mask)) {
890 		/* No protection */
891 		*ofs = 0;
892 		*len = 0;
893 	} else {
894 		pow = ((sr & mask) ^ mask) >> shift;
895 		*len = mtd->size >> pow;
896 		if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
897 			*ofs = 0;
898 		else
899 			*ofs = mtd->size - *len;
900 	}
901 }
902 
903 /*
904  * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
905  * @locked is false); 0 otherwise
906  */
stm_check_lock_status_sr(struct spi_nor * nor,loff_t ofs,u64 len,u8 sr,bool locked)907 static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, u64 len,
908 				    u8 sr, bool locked)
909 {
910 	loff_t lock_offs;
911 	uint64_t lock_len;
912 
913 	if (!len)
914 		return 1;
915 
916 	stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
917 
918 	if (locked)
919 		/* Requested range is a sub-range of locked range */
920 		return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
921 	else
922 		/* Requested range does not overlap with locked range */
923 		return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
924 }
925 
stm_is_locked_sr(struct spi_nor * nor,loff_t ofs,uint64_t len,u8 sr)926 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
927 			    u8 sr)
928 {
929 	return stm_check_lock_status_sr(nor, ofs, len, sr, true);
930 }
931 
stm_is_unlocked_sr(struct spi_nor * nor,loff_t ofs,uint64_t len,u8 sr)932 static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
933 			      u8 sr)
934 {
935 	return stm_check_lock_status_sr(nor, ofs, len, sr, false);
936 }
937 
938 /*
939  * Lock a region of the flash. Compatible with ST Micro and similar flash.
940  * Supports the block protection bits BP{0,1,2} in the status register
941  * (SR). Does not support these features found in newer SR bitfields:
942  *   - SEC: sector/block protect - only handle SEC=0 (block protect)
943  *   - CMP: complement protect - only support CMP=0 (range is not complemented)
944  *
945  * Support for the following is provided conditionally for some flash:
946  *   - TB: top/bottom protect
947  *
948  * Sample table portion for 8MB flash (Winbond w25q64fw):
949  *
950  *   SEC  |  TB   |  BP2  |  BP1  |  BP0  |  Prot Length  | Protected Portion
951  *  --------------------------------------------------------------------------
952  *    X   |   X   |   0   |   0   |   0   |  NONE         | NONE
953  *    0   |   0   |   0   |   0   |   1   |  128 KB       | Upper 1/64
954  *    0   |   0   |   0   |   1   |   0   |  256 KB       | Upper 1/32
955  *    0   |   0   |   0   |   1   |   1   |  512 KB       | Upper 1/16
956  *    0   |   0   |   1   |   0   |   0   |  1 MB         | Upper 1/8
957  *    0   |   0   |   1   |   0   |   1   |  2 MB         | Upper 1/4
958  *    0   |   0   |   1   |   1   |   0   |  4 MB         | Upper 1/2
959  *    X   |   X   |   1   |   1   |   1   |  8 MB         | ALL
960  *  ------|-------|-------|-------|-------|---------------|-------------------
961  *    0   |   1   |   0   |   0   |   1   |  128 KB       | Lower 1/64
962  *    0   |   1   |   0   |   1   |   0   |  256 KB       | Lower 1/32
963  *    0   |   1   |   0   |   1   |   1   |  512 KB       | Lower 1/16
964  *    0   |   1   |   1   |   0   |   0   |  1 MB         | Lower 1/8
965  *    0   |   1   |   1   |   0   |   1   |  2 MB         | Lower 1/4
966  *    0   |   1   |   1   |   1   |   0   |  4 MB         | Lower 1/2
967  *
968  * Returns negative on errors, 0 on success.
969  */
stm_lock(struct spi_nor * nor,loff_t ofs,uint64_t len)970 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
971 {
972 	struct mtd_info *mtd = &nor->mtd;
973 	int status_old, status_new;
974 	u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
975 	u8 shift = ffs(mask) - 1, pow, val;
976 	loff_t lock_len;
977 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
978 	bool use_top;
979 
980 	status_old = read_sr(nor);
981 	if (status_old < 0)
982 		return status_old;
983 
984 	/* If nothing in our range is unlocked, we don't need to do anything */
985 	if (stm_is_locked_sr(nor, ofs, len, status_old))
986 		return 0;
987 
988 	/* If anything below us is unlocked, we can't use 'bottom' protection */
989 	if (!stm_is_locked_sr(nor, 0, ofs, status_old))
990 		can_be_bottom = false;
991 
992 	/* If anything above us is unlocked, we can't use 'top' protection */
993 	if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
994 			      status_old))
995 		can_be_top = false;
996 
997 	if (!can_be_bottom && !can_be_top)
998 		return -EINVAL;
999 
1000 	/* Prefer top, if both are valid */
1001 	use_top = can_be_top;
1002 
1003 	/* lock_len: length of region that should end up locked */
1004 	if (use_top)
1005 		lock_len = mtd->size - ofs;
1006 	else
1007 		lock_len = ofs + len;
1008 
1009 	/*
1010 	 * Need smallest pow such that:
1011 	 *
1012 	 *   1 / (2^pow) <= (len / size)
1013 	 *
1014 	 * so (assuming power-of-2 size) we do:
1015 	 *
1016 	 *   pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
1017 	 */
1018 	pow = ilog2(mtd->size) - ilog2(lock_len);
1019 	val = mask - (pow << shift);
1020 	if (val & ~mask)
1021 		return -EINVAL;
1022 	/* Don't "lock" with no region! */
1023 	if (!(val & mask))
1024 		return -EINVAL;
1025 
1026 	status_new = (status_old & ~mask & ~SR_TB) | val;
1027 
1028 	/* Disallow further writes if WP pin is asserted */
1029 	status_new |= SR_SRWD;
1030 
1031 	if (!use_top)
1032 		status_new |= SR_TB;
1033 
1034 	/* Don't bother if they're the same */
1035 	if (status_new == status_old)
1036 		return 0;
1037 
1038 	/* Only modify protection if it will not unlock other areas */
1039 	if ((status_new & mask) < (status_old & mask))
1040 		return -EINVAL;
1041 
1042 	return write_sr_and_check(nor, status_new, mask);
1043 }
1044 
1045 /*
1046  * Unlock a region of the flash. See stm_lock() for more info
1047  *
1048  * Returns negative on errors, 0 on success.
1049  */
stm_unlock(struct spi_nor * nor,loff_t ofs,uint64_t len)1050 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1051 {
1052 	struct mtd_info *mtd = &nor->mtd;
1053 	int status_old, status_new;
1054 	u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1055 	u8 shift = ffs(mask) - 1, pow, val;
1056 	loff_t lock_len;
1057 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1058 	bool use_top;
1059 
1060 	status_old = read_sr(nor);
1061 	if (status_old < 0)
1062 		return status_old;
1063 
1064 	/* If nothing in our range is locked, we don't need to do anything */
1065 	if (stm_is_unlocked_sr(nor, ofs, len, status_old))
1066 		return 0;
1067 
1068 	/* If anything below us is locked, we can't use 'top' protection */
1069 	if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
1070 		can_be_top = false;
1071 
1072 	/* If anything above us is locked, we can't use 'bottom' protection */
1073 	if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
1074 				status_old))
1075 		can_be_bottom = false;
1076 
1077 	if (!can_be_bottom && !can_be_top)
1078 		return -EINVAL;
1079 
1080 	/* Prefer top, if both are valid */
1081 	use_top = can_be_top;
1082 
1083 	/* lock_len: length of region that should remain locked */
1084 	if (use_top)
1085 		lock_len = mtd->size - (ofs + len);
1086 	else
1087 		lock_len = ofs;
1088 
1089 	/*
1090 	 * Need largest pow such that:
1091 	 *
1092 	 *   1 / (2^pow) >= (len / size)
1093 	 *
1094 	 * so (assuming power-of-2 size) we do:
1095 	 *
1096 	 *   pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
1097 	 */
1098 	pow = ilog2(mtd->size) - order_base_2(lock_len);
1099 	if (lock_len == 0) {
1100 		val = 0; /* fully unlocked */
1101 	} else {
1102 		val = mask - (pow << shift);
1103 		/* Some power-of-two sizes are not supported */
1104 		if (val & ~mask)
1105 			return -EINVAL;
1106 	}
1107 
1108 	status_new = (status_old & ~mask & ~SR_TB) | val;
1109 
1110 	/* Don't protect status register if we're fully unlocked */
1111 	if (lock_len == 0)
1112 		status_new &= ~SR_SRWD;
1113 
1114 	if (!use_top)
1115 		status_new |= SR_TB;
1116 
1117 	/* Don't bother if they're the same */
1118 	if (status_new == status_old)
1119 		return 0;
1120 
1121 	/* Only modify protection if it will not lock other areas */
1122 	if ((status_new & mask) > (status_old & mask))
1123 		return -EINVAL;
1124 
1125 	return write_sr_and_check(nor, status_new, mask);
1126 }
1127 
1128 /*
1129  * Check if a region of the flash is (completely) locked. See stm_lock() for
1130  * more info.
1131  *
1132  * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
1133  * negative on errors.
1134  */
stm_is_locked(struct spi_nor * nor,loff_t ofs,uint64_t len)1135 static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
1136 {
1137 	int status;
1138 
1139 	status = read_sr(nor);
1140 	if (status < 0)
1141 		return status;
1142 
1143 	return stm_is_locked_sr(nor, ofs, len, status);
1144 }
1145 #endif /* CONFIG_SPI_FLASH_STMICRO */
1146 
spi_nor_wlock_by_host_ctrl(struct spi_nor * nor,u32 offset,size_t len)1147 static int spi_nor_wlock_by_host_ctrl(struct spi_nor *nor,
1148 				      u32 offset, size_t len)
1149 {
1150 	struct spi_slave *spi = nor->spi;
1151 	int ret;
1152 
1153 	ret = spi_claim_bus(spi);
1154 	if (ret < 0)
1155 		return ret;
1156 
1157 	ret = spi_nor_ctrl_wlock(spi, offset, len);
1158 
1159 	spi_release_bus(spi);
1160 
1161 	return ret;
1162 }
1163 
spi_nor_wunlock_by_host_ctrl(struct spi_nor * nor,u32 offset,size_t len)1164 static int spi_nor_wunlock_by_host_ctrl(struct spi_nor *nor,
1165 					u32 offset, size_t len)
1166 {
1167 	struct spi_slave *spi = nor->spi;
1168 	int ret;
1169 
1170 	ret = spi_claim_bus(spi);
1171 	if (ret < 0)
1172 		return ret;
1173 
1174 	ret = spi_nor_ctrl_wunlock(spi, offset, len);
1175 
1176 	spi_release_bus(spi);
1177 
1178 	return ret;
1179 }
1180 
spi_nor_read_id(struct spi_nor * nor)1181 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
1182 {
1183 	int			tmp;
1184 	u8			id[SPI_NOR_MAX_ID_LEN];
1185 	const struct flash_info	*info;
1186 
1187 	tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
1188 	if (tmp < 0) {
1189 		dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
1190 		return ERR_PTR(tmp);
1191 	}
1192 
1193 	info = spi_nor_ids;
1194 	for (; info->name; info++) {
1195 		if (info->id_len) {
1196 			if (!memcmp(info->id, id, info->id_len))
1197 				return info;
1198 		}
1199 	}
1200 
1201 	dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
1202 		id[0], id[1], id[2]);
1203 	return ERR_PTR(-ENODEV);
1204 }
1205 
spi_nor_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)1206 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
1207 			size_t *retlen, u_char *buf)
1208 {
1209 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
1210 	int ret;
1211 
1212 	dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
1213 
1214 	while (len) {
1215 		loff_t addr = from;
1216 		size_t read_len = len;
1217 
1218 #ifdef CONFIG_SPI_FLASH_BAR
1219 		u32 remain_len;
1220 
1221 		ret = write_bar(nor, addr);
1222 		if (ret < 0)
1223 			return log_ret(ret);
1224 		remain_len = (SZ_16M * (nor->bank_curr + 1)) - addr;
1225 
1226 		if (len < remain_len)
1227 			read_len = len;
1228 		else
1229 			read_len = remain_len;
1230 #endif
1231 
1232 		ret = nor->read(nor, addr, read_len, buf);
1233 		if (ret == 0) {
1234 			/* We shouldn't see 0-length reads */
1235 			ret = -EIO;
1236 			goto read_err;
1237 		}
1238 		if (ret < 0)
1239 			goto read_err;
1240 
1241 		*retlen += ret;
1242 		buf += ret;
1243 		from += ret;
1244 		len -= ret;
1245 	}
1246 	ret = 0;
1247 
1248 read_err:
1249 #ifdef CONFIG_SPI_FLASH_BAR
1250 	ret = clean_bar(nor);
1251 #endif
1252 	return ret;
1253 }
1254 
1255 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
sst_read_nvcr(struct spi_nor * nor)1256 static int sst_read_nvcr(struct spi_nor *nor)
1257 {
1258 	int ret;
1259 	int val;
1260 
1261 	ret = nor->read_reg(nor, SPINOR_OP_SST_RDNVCR, (u8 *)&val, 2);
1262 	if (ret < 0) {
1263 		dev_err(nor->dev, "SST error %d while reading CR\n", ret);
1264 		return ret;
1265 	}
1266 
1267 	return val;
1268 }
1269 
sst_write_nvcr(struct spi_nor * nor,int val)1270 static int sst_write_nvcr(struct spi_nor *nor, int val)
1271 {
1272 	int ret;
1273 
1274 	write_enable(nor);
1275 
1276 	nor->cmd_buf[0] = val & 0xff;
1277 	nor->cmd_buf[1] = (val >> 8) & 0xff;
1278 
1279 	ret = nor->write_reg(nor, SPINOR_OP_SST_WRNVCR, nor->cmd_buf, 2);
1280 	if (ret < 0) {
1281 		dev_err(nor->dev,
1282 			"SST error while writing configuration register\n");
1283 		return -EINVAL;
1284 	}
1285 
1286 	ret = spi_nor_wait_till_ready(nor);
1287 	if (ret) {
1288 		dev_err(nor->dev,
1289 			"SST timeout while writing configuration register\n");
1290 		return ret;
1291 	}
1292 
1293 	return 0;
1294 }
1295 
sst_cr_quad_enable(struct spi_nor * nor)1296 static int sst_cr_quad_enable(struct spi_nor *nor)
1297 {
1298 	int ret;
1299 
1300 	/* Check current Quad Enable bit value. */
1301 	ret = sst_read_nvcr(nor);
1302 	if (ret < 0) {
1303 		dev_dbg(nor->dev, "SST error while reading nonvolatile configuration register\n");
1304 		return -EINVAL;
1305 	}
1306 
1307 	if ((ret & SPINOR_SST_RST_HOLD_CTRL) == 0)
1308 		return 0;
1309 
1310 	/* Nonvolatile Configuration Register bit 4 */
1311 	ret &= ~SPINOR_SST_RST_HOLD_CTRL;
1312 
1313 	/* Keep the current value of the Status Register. */
1314 	ret = sst_write_nvcr(nor, ret);
1315 	if (ret < 0) {
1316 		dev_err(nor->dev, "SST error while writing nonvolatile configuration register\n");
1317 		return -EINVAL;
1318 	}
1319 
1320 	ret = sst_read_nvcr(nor);
1321 	if (ret > 0 && (ret & SPINOR_SST_RST_HOLD_CTRL)) {
1322 		dev_err(nor->dev, "SST Quad bit not set\n");
1323 		return -EINVAL;
1324 	}
1325 
1326 	return 0;
1327 }
1328 #endif
1329 
1330 #ifdef CONFIG_SPI_FLASH_SST
sst_write_byteprogram(struct spi_nor * nor,loff_t to,size_t len,size_t * retlen,const u_char * buf)1331 static int sst_write_byteprogram(struct spi_nor *nor, loff_t to, size_t len,
1332 				 size_t *retlen, const u_char *buf)
1333 {
1334 	size_t actual;
1335 	int ret = 0;
1336 
1337 	for (actual = 0; actual < len; actual++) {
1338 		nor->program_opcode = SPINOR_OP_BP;
1339 
1340 		write_enable(nor);
1341 		/* write one byte. */
1342 		ret = nor->write(nor, to, 1, buf + actual);
1343 		if (ret < 0)
1344 			goto sst_write_err;
1345 		ret = spi_nor_wait_till_ready(nor);
1346 		if (ret)
1347 			goto sst_write_err;
1348 		to++;
1349 	}
1350 
1351 sst_write_err:
1352 	write_disable(nor);
1353 	return ret;
1354 }
1355 
sst_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)1356 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
1357 		     size_t *retlen, const u_char *buf)
1358 {
1359 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
1360 	struct spi_slave *spi = nor->spi;
1361 	size_t actual;
1362 	int ret;
1363 
1364 	dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1365 	if (spi->mode & SPI_TX_BYTE)
1366 		return sst_write_byteprogram(nor, to, len, retlen, buf);
1367 
1368 	write_enable(nor);
1369 
1370 	nor->sst_write_second = false;
1371 
1372 	actual = to % 2;
1373 	/* Start write from odd address. */
1374 	if (actual) {
1375 		nor->program_opcode = SPINOR_OP_BP;
1376 
1377 		/* write one byte. */
1378 		ret = nor->write(nor, to, 1, buf);
1379 		if (ret < 0)
1380 			goto sst_write_err;
1381 		ret = spi_nor_wait_till_ready(nor);
1382 		if (ret)
1383 			goto sst_write_err;
1384 	}
1385 	to += actual;
1386 
1387 	/* Write out most of the data here. */
1388 	for (; actual < len - 1; actual += 2) {
1389 		nor->program_opcode = SPINOR_OP_AAI_WP;
1390 
1391 		/* write two bytes. */
1392 		ret = nor->write(nor, to, 2, buf + actual);
1393 		if (ret < 0)
1394 			goto sst_write_err;
1395 		ret = spi_nor_wait_till_ready(nor);
1396 		if (ret)
1397 			goto sst_write_err;
1398 		to += 2;
1399 		nor->sst_write_second = true;
1400 	}
1401 	nor->sst_write_second = false;
1402 
1403 	write_disable(nor);
1404 	ret = spi_nor_wait_till_ready(nor);
1405 	if (ret)
1406 		goto sst_write_err;
1407 
1408 	/* Write out trailing byte if it exists. */
1409 	if (actual != len) {
1410 		write_enable(nor);
1411 
1412 		nor->program_opcode = SPINOR_OP_BP;
1413 		ret = nor->write(nor, to, 1, buf + actual);
1414 		if (ret < 0)
1415 			goto sst_write_err;
1416 		ret = spi_nor_wait_till_ready(nor);
1417 		if (ret)
1418 			goto sst_write_err;
1419 		write_disable(nor);
1420 		actual += 1;
1421 	}
1422 sst_write_err:
1423 	*retlen += actual;
1424 	return ret;
1425 }
1426 #endif
1427 /*
1428  * Write an address range to the nor chip.  Data must be written in
1429  * FLASH_PAGESIZE chunks.  The address range may be any size provided
1430  * it is within the physical boundaries.
1431  */
spi_nor_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)1432 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1433 	size_t *retlen, const u_char *buf)
1434 {
1435 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
1436 	size_t page_offset, page_remain, i;
1437 	ssize_t ret;
1438 
1439 	dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1440 
1441 	for (i = 0; i < len; ) {
1442 		ssize_t written;
1443 		loff_t addr = to + i;
1444 
1445 		/*
1446 		 * If page_size is a power of two, the offset can be quickly
1447 		 * calculated with an AND operation. On the other cases we
1448 		 * need to do a modulus operation (more expensive).
1449 		 * Power of two numbers have only one bit set and we can use
1450 		 * the instruction hweight32 to detect if we need to do a
1451 		 * modulus (do_div()) or not.
1452 		 */
1453 		if (hweight32(nor->page_size) == 1) {
1454 			page_offset = addr & (nor->page_size - 1);
1455 		} else {
1456 			u64 aux = addr;
1457 
1458 			page_offset = do_div(aux, nor->page_size);
1459 		}
1460 		/* the size of data remaining on the first page */
1461 		page_remain = min_t(size_t,
1462 				    nor->page_size - page_offset, len - i);
1463 
1464 #ifdef CONFIG_SPI_FLASH_BAR
1465 		ret = write_bar(nor, addr);
1466 		if (ret < 0)
1467 			return ret;
1468 #endif
1469 		write_enable(nor);
1470 		ret = nor->write(nor, addr, page_remain, buf + i);
1471 		if (ret < 0)
1472 			goto write_err;
1473 		written = ret;
1474 
1475 		ret = spi_nor_wait_till_ready(nor);
1476 		if (ret)
1477 			goto write_err;
1478 		*retlen += written;
1479 		i += written;
1480 		if (written != page_remain) {
1481 			ret = -EIO;
1482 			goto write_err;
1483 		}
1484 	}
1485 
1486 write_err:
1487 #ifdef CONFIG_SPI_FLASH_BAR
1488 	ret = clean_bar(nor);
1489 #endif
1490 	return ret;
1491 }
1492 
1493 #ifdef CONFIG_SPI_FLASH_MACRONIX
1494 /**
1495  * macronix_quad_enable() - set QE bit in Status Register.
1496  * @nor:	pointer to a 'struct spi_nor'
1497  *
1498  * Set the Quad Enable (QE) bit in the Status Register.
1499  *
1500  * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
1501  *
1502  * Return: 0 on success, -errno otherwise.
1503  */
macronix_quad_enable(struct spi_nor * nor)1504 static int macronix_quad_enable(struct spi_nor *nor)
1505 {
1506 	int ret, val;
1507 
1508 	val = read_sr(nor);
1509 	if (val < 0)
1510 		return val;
1511 	if (val & SR_QUAD_EN_MX)
1512 		return 0;
1513 
1514 	write_enable(nor);
1515 
1516 	write_sr(nor, val | SR_QUAD_EN_MX);
1517 
1518 	ret = spi_nor_wait_till_ready(nor);
1519 	if (ret)
1520 		return ret;
1521 
1522 	ret = read_sr(nor);
1523 	if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
1524 		dev_err(nor->dev, "Macronix Quad bit not set\n");
1525 		return -EINVAL;
1526 	}
1527 
1528 	return 0;
1529 }
1530 #endif
1531 
1532 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
1533 /*
1534  * Write status Register and configuration register with 2 bytes
1535  * The first byte will be written to the status register, while the
1536  * second byte will be written to the configuration register.
1537  * Return negative if error occurred.
1538  */
write_sr_cr(struct spi_nor * nor,u8 * sr_cr)1539 static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
1540 {
1541 	int ret;
1542 
1543 	write_enable(nor);
1544 
1545 	ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
1546 	if (ret < 0) {
1547 		dev_dbg(nor->dev,
1548 			"error while writing configuration register\n");
1549 		return -EINVAL;
1550 	}
1551 
1552 	ret = spi_nor_wait_till_ready(nor);
1553 	if (ret) {
1554 		dev_dbg(nor->dev,
1555 			"timeout while writing configuration register\n");
1556 		return ret;
1557 	}
1558 
1559 	return 0;
1560 }
1561 
1562 /**
1563  * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
1564  * @nor:	pointer to a 'struct spi_nor'
1565  *
1566  * Set the Quad Enable (QE) bit in the Configuration Register.
1567  * This function should be used with QSPI memories supporting the Read
1568  * Configuration Register (35h) instruction.
1569  *
1570  * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1571  * memories.
1572  *
1573  * Return: 0 on success, -errno otherwise.
1574  */
spansion_read_cr_quad_enable(struct spi_nor * nor)1575 static int spansion_read_cr_quad_enable(struct spi_nor *nor)
1576 {
1577 	u8 sr_cr[2];
1578 	int ret;
1579 
1580 	/* Check current Quad Enable bit value. */
1581 	ret = read_cr(nor);
1582 	if (ret < 0) {
1583 		dev_dbg(nor->dev, "error while reading configuration register\n");
1584 		return -EINVAL;
1585 	}
1586 
1587 	if (ret & CR_QUAD_EN_SPAN)
1588 		return 0;
1589 
1590 	sr_cr[1] = ret | CR_QUAD_EN_SPAN;
1591 
1592 	/* Keep the current value of the Status Register. */
1593 	ret = read_sr(nor);
1594 	if (ret < 0) {
1595 		dev_dbg(nor->dev, "error while reading status register\n");
1596 		return -EINVAL;
1597 	}
1598 	sr_cr[0] = ret;
1599 
1600 	ret = write_sr_cr(nor, sr_cr);
1601 	if (ret)
1602 		return ret;
1603 
1604 	/* Read back and check it. */
1605 	ret = read_cr(nor);
1606 	if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1607 		dev_dbg(nor->dev, "Spansion Quad bit not set\n");
1608 		return -EINVAL;
1609 	}
1610 
1611 	return 0;
1612 }
1613 
1614 /**
1615  * sr2_bit1_quad_enable() - set QE bit in Status Register 2.
1616  * @nor:	pointer to a 'struct spi_nor'
1617  *
1618  * Set the Quad Enable (QE) bit in the Status Register 2.
1619  *
1620  * Return: 0 on success, -errno otherwise.
1621  */
winbond_sr2_bit1_quad_enable(struct spi_nor * nor)1622 static int winbond_sr2_bit1_quad_enable(struct spi_nor *nor)
1623 {
1624 	u8 sr2 = 0;
1625 	int ret;
1626 
1627 	/* Check current Quad Enable bit value. */
1628 	ret = read_winbond_sr2(nor);
1629 	if (ret < 0) {
1630 		dev_err(nor->dev, "error while reading status register 2\n");
1631 		return -EINVAL;
1632 	}
1633 
1634 	if (ret & SR2_QUAD_EN_BIT1)
1635 		return 0;
1636 
1637 	/* Update the Quad Enable bit. */
1638 	sr2 = (u8)(ret | SR2_QUAD_EN_BIT1);
1639 
1640 	write_enable(nor);
1641 
1642 	ret = write_winbond_sr2(nor, sr2);
1643 	if (ret < 0) {
1644 		dev_err(nor->dev, "error while writing status register 2\n");
1645 		return -EINVAL;
1646 	}
1647 
1648 	ret = spi_nor_wait_till_ready(nor);
1649 	if (ret < 0) {
1650 		dev_err(nor->dev, "timeout while writing status register 2\n");
1651 		return ret;
1652 	}
1653 
1654 	/* Read back and check it. */
1655 	ret = read_winbond_sr2(nor);
1656 	if (ret < 0 || !(ret & SR2_QUAD_EN_BIT1)) {
1657 		dev_err(nor->dev, "SR2 Quad bit not set\n");
1658 		return -EINVAL;
1659 	}
1660 
1661 	return 0;
1662 }
1663 
1664 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
1665 /**
1666  * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
1667  * @nor:	pointer to a 'struct spi_nor'
1668  *
1669  * Set the Quad Enable (QE) bit in the Configuration Register.
1670  * This function should be used with QSPI memories not supporting the Read
1671  * Configuration Register (35h) instruction.
1672  *
1673  * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1674  * memories.
1675  *
1676  * Return: 0 on success, -errno otherwise.
1677  */
spansion_no_read_cr_quad_enable(struct spi_nor * nor)1678 static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
1679 {
1680 	u8 sr_cr[2];
1681 	int ret;
1682 
1683 	/* Keep the current value of the Status Register. */
1684 	ret = read_sr(nor);
1685 	if (ret < 0) {
1686 		dev_dbg(nor->dev, "error while reading status register\n");
1687 		return -EINVAL;
1688 	}
1689 	sr_cr[0] = ret;
1690 	sr_cr[1] = CR_QUAD_EN_SPAN;
1691 
1692 	return write_sr_cr(nor, sr_cr);
1693 }
1694 
1695 #endif /* CONFIG_SPI_FLASH_SFDP_SUPPORT */
1696 #endif /* CONFIG_SPI_FLASH_SPANSION */
1697 
1698 struct spi_nor_read_command {
1699 	u8			num_mode_clocks;
1700 	u8			num_wait_states;
1701 	u8			opcode;
1702 	enum spi_nor_protocol	proto;
1703 };
1704 
1705 struct spi_nor_pp_command {
1706 	u8			opcode;
1707 	enum spi_nor_protocol	proto;
1708 };
1709 
1710 enum spi_nor_read_command_index {
1711 	SNOR_CMD_READ,
1712 	SNOR_CMD_READ_FAST,
1713 	SNOR_CMD_READ_1_1_1_DTR,
1714 
1715 	/* Dual SPI */
1716 	SNOR_CMD_READ_1_1_2,
1717 	SNOR_CMD_READ_1_2_2,
1718 	SNOR_CMD_READ_2_2_2,
1719 	SNOR_CMD_READ_1_2_2_DTR,
1720 
1721 	/* Quad SPI */
1722 	SNOR_CMD_READ_1_1_4,
1723 	SNOR_CMD_READ_1_4_4,
1724 	SNOR_CMD_READ_4_4_4,
1725 	SNOR_CMD_READ_1_4_4_DTR,
1726 
1727 	/* Octo SPI */
1728 	SNOR_CMD_READ_1_1_8,
1729 	SNOR_CMD_READ_1_8_8,
1730 	SNOR_CMD_READ_8_8_8,
1731 	SNOR_CMD_READ_1_8_8_DTR,
1732 
1733 	SNOR_CMD_READ_MAX
1734 };
1735 
1736 enum spi_nor_pp_command_index {
1737 	SNOR_CMD_PP,
1738 
1739 	/* Quad SPI */
1740 	SNOR_CMD_PP_1_1_4,
1741 	SNOR_CMD_PP_1_4_4,
1742 	SNOR_CMD_PP_4_4_4,
1743 
1744 	/* Octo SPI */
1745 	SNOR_CMD_PP_1_1_8,
1746 	SNOR_CMD_PP_1_8_8,
1747 	SNOR_CMD_PP_8_8_8,
1748 
1749 	SNOR_CMD_PP_MAX
1750 };
1751 
1752 struct spi_nor_flash_parameter {
1753 	u64				size;
1754 	u32				page_size;
1755 
1756 	struct spi_nor_hwcaps		hwcaps;
1757 	struct spi_nor_read_command	reads[SNOR_CMD_READ_MAX];
1758 	struct spi_nor_pp_command	page_programs[SNOR_CMD_PP_MAX];
1759 
1760 	int (*quad_enable)(struct spi_nor *nor);
1761 };
1762 
1763 #ifdef CONFIG_SPI_FLASH_SPANSION
1764 /**
1765  * spansion_quad_enable_volatile() - enable Quad I/O mode in volatile register.
1766  * @nor:	pointer to a 'struct spi_nor'
1767  *
1768  * It is recommended to update volatile registers in the field application due
1769  * to a risk of the non-volatile registers corruption by power interrupt. This
1770  * function sets Quad Enable bit in CFR1 volatile.
1771  *
1772  * Return: 0 on success, -errno otherwise.
1773  */
spansion_quad_enable_volatile(struct spi_nor * nor)1774 static int spansion_quad_enable_volatile(struct spi_nor *nor)
1775 {
1776 	u32 addr = SPINOR_REG_ADDR_CFR1V;
1777 
1778 	u8 cr;
1779 	int ret;
1780 
1781 	/* Check current Quad Enable bit value. */
1782 	ret = spansion_read_any_reg(nor, addr, 0, &cr);
1783 	if (ret < 0) {
1784 		dev_dbg(nor->dev,
1785 			"error while reading configuration register\n");
1786 		return -EINVAL;
1787 	}
1788 
1789 	if (cr & CR_QUAD_EN_SPAN)
1790 		return 0;
1791 
1792 	cr |= CR_QUAD_EN_SPAN;
1793 
1794 	write_enable(nor);
1795 
1796 	ret = spansion_write_any_reg(nor, addr, cr);
1797 
1798 	if (ret < 0) {
1799 		dev_dbg(nor->dev,
1800 			"error while writing configuration register\n");
1801 		return -EINVAL;
1802 	}
1803 
1804 	/* Read back and check it. */
1805 	ret = spansion_read_any_reg(nor, addr, 0, &cr);
1806 	if (ret || !(cr & CR_QUAD_EN_SPAN)) {
1807 		dev_dbg(nor->dev, "Spansion Quad bit not set\n");
1808 		return -EINVAL;
1809 	}
1810 
1811 	return 0;
1812 }
1813 #endif
1814 
1815 static void
spi_nor_set_read_settings(struct spi_nor_read_command * read,u8 num_mode_clocks,u8 num_wait_states,u8 opcode,enum spi_nor_protocol proto)1816 spi_nor_set_read_settings(struct spi_nor_read_command *read,
1817 			  u8 num_mode_clocks,
1818 			  u8 num_wait_states,
1819 			  u8 opcode,
1820 			  enum spi_nor_protocol proto)
1821 {
1822 	read->num_mode_clocks = num_mode_clocks;
1823 	read->num_wait_states = num_wait_states;
1824 	read->opcode = opcode;
1825 	read->proto = proto;
1826 }
1827 
1828 static void
spi_nor_set_pp_settings(struct spi_nor_pp_command * pp,u8 opcode,enum spi_nor_protocol proto)1829 spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
1830 			u8 opcode,
1831 			enum spi_nor_protocol proto)
1832 {
1833 	pp->opcode = opcode;
1834 	pp->proto = proto;
1835 }
1836 
1837 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
1838 /*
1839  * Serial Flash Discoverable Parameters (SFDP) parsing.
1840  */
1841 
1842 /**
1843  * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
1844  * @nor:	pointer to a 'struct spi_nor'
1845  * @addr:	offset in the SFDP area to start reading data from
1846  * @len:	number of bytes to read
1847  * @buf:	buffer where the SFDP data are copied into (dma-safe memory)
1848  *
1849  * Whatever the actual numbers of bytes for address and dummy cycles are
1850  * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
1851  * followed by a 3-byte address and 8 dummy clock cycles.
1852  *
1853  * Return: 0 on success, -errno otherwise.
1854  */
spi_nor_read_sfdp(struct spi_nor * nor,u32 addr,size_t len,void * buf)1855 static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
1856 			     size_t len, void *buf)
1857 {
1858 	u8 addr_width, read_opcode, read_dummy;
1859 	enum spi_nor_protocol	read_proto;
1860 	int ret;
1861 
1862 	read_opcode = nor->read_opcode;
1863 	addr_width = nor->addr_width;
1864 	read_dummy = nor->read_dummy;
1865 	read_proto = nor->read_proto;
1866 
1867 	nor->read_opcode = SPINOR_OP_RDSFDP;
1868 	nor->read_proto = SNOR_PROTO_1_1_1;
1869 	nor->addr_width = 3;
1870 	nor->read_dummy = 8;
1871 
1872 	while (len) {
1873 		ret = nor->read(nor, addr, len, (u8 *)buf);
1874 		if (!ret || ret > len) {
1875 			ret = -EIO;
1876 			goto read_err;
1877 		}
1878 		if (ret < 0)
1879 			goto read_err;
1880 
1881 		buf += ret;
1882 		addr += ret;
1883 		len -= ret;
1884 	}
1885 	ret = 0;
1886 
1887 read_err:
1888 	nor->read_opcode = read_opcode;
1889 	nor->addr_width = addr_width;
1890 	nor->read_dummy = read_dummy;
1891 	nor->read_proto = read_proto;
1892 
1893 	return ret;
1894 }
1895 
1896 struct sfdp_parameter_header {
1897 	u8		id_lsb;
1898 	u8		minor;
1899 	u8		major;
1900 	u8		length; /* in double words */
1901 	u8		parameter_table_pointer[3]; /* byte address */
1902 	u8		id_msb;
1903 };
1904 
1905 #define SFDP_PARAM_HEADER_ID(p)	(((p)->id_msb << 8) | (p)->id_lsb)
1906 #define SFDP_PARAM_HEADER_PTP(p) \
1907 	(((p)->parameter_table_pointer[2] << 16) | \
1908 	 ((p)->parameter_table_pointer[1] <<  8) | \
1909 	 ((p)->parameter_table_pointer[0] <<  0))
1910 
1911 #define SFDP_BFPT_ID		0xff00	/* Basic Flash Parameter Table */
1912 #define SFDP_SECTOR_MAP_ID	0xff81	/* Sector Map Table */
1913 
1914 #define SFDP_SIGNATURE		0x50444653U
1915 #define SFDP_JESD216_MAJOR	1
1916 #define SFDP_JESD216_MINOR	0
1917 #define SFDP_JESD216A_MINOR	5
1918 #define SFDP_JESD216B_MINOR	6
1919 
1920 struct sfdp_header {
1921 	u32		signature; /* Ox50444653U <=> "SFDP" */
1922 	u8		minor;
1923 	u8		major;
1924 	u8		nph; /* 0-base number of parameter headers */
1925 	u8		unused;
1926 
1927 	/* Basic Flash Parameter Table. */
1928 	struct sfdp_parameter_header	bfpt_header;
1929 };
1930 
1931 /* Basic Flash Parameter Table */
1932 
1933 /*
1934  * JESD216 rev B defines a Basic Flash Parameter Table of 16 DWORDs.
1935  * They are indexed from 1 but C arrays are indexed from 0.
1936  */
1937 #define BFPT_DWORD(i)		((i) - 1)
1938 #define BFPT_DWORD_MAX		16
1939 
1940 /* The first version of JESB216 defined only 9 DWORDs. */
1941 #define BFPT_DWORD_MAX_JESD216			9
1942 
1943 /* 1st DWORD. */
1944 #define BFPT_DWORD1_FAST_READ_1_1_2		BIT(16)
1945 #define BFPT_DWORD1_ADDRESS_BYTES_MASK		GENMASK(18, 17)
1946 #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY	(0x0UL << 17)
1947 #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4	(0x1UL << 17)
1948 #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY	(0x2UL << 17)
1949 #define BFPT_DWORD1_DTR				BIT(19)
1950 #define BFPT_DWORD1_FAST_READ_1_2_2		BIT(20)
1951 #define BFPT_DWORD1_FAST_READ_1_4_4		BIT(21)
1952 #define BFPT_DWORD1_FAST_READ_1_1_4		BIT(22)
1953 
1954 /* 5th DWORD. */
1955 #define BFPT_DWORD5_FAST_READ_2_2_2		BIT(0)
1956 #define BFPT_DWORD5_FAST_READ_4_4_4		BIT(4)
1957 
1958 /* 11th DWORD. */
1959 #define BFPT_DWORD11_PAGE_SIZE_SHIFT		4
1960 #define BFPT_DWORD11_PAGE_SIZE_MASK		GENMASK(7, 4)
1961 
1962 /* 15th DWORD. */
1963 
1964 /*
1965  * (from JESD216 rev B)
1966  * Quad Enable Requirements (QER):
1967  * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
1968  *         reads based on instruction. DQ3/HOLD# functions are hold during
1969  *         instruction phase.
1970  * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
1971  *         two data bytes where bit 1 of the second byte is one.
1972  *         [...]
1973  *         Writing only one byte to the status register has the side-effect of
1974  *         clearing status register 2, including the QE bit. The 100b code is
1975  *         used if writing one byte to the status register does not modify
1976  *         status register 2.
1977  * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
1978  *         one data byte where bit 6 is one.
1979  *         [...]
1980  * - 011b: QE is bit 7 of status register 2. It is set via Write status
1981  *         register 2 instruction 3Eh with one data byte where bit 7 is one.
1982  *         [...]
1983  *         The status register 2 is read using instruction 3Fh.
1984  * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
1985  *         two data bytes where bit 1 of the second byte is one.
1986  *         [...]
1987  *         In contrast to the 001b code, writing one byte to the status
1988  *         register does not modify status register 2.
1989  * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
1990  *         Read Status instruction 05h. Status register2 is read using
1991  *         instruction 35h. QE is set via Writ Status instruction 01h with
1992  *         two data bytes where bit 1 of the second byte is one.
1993  *         [...]
1994  */
1995 #define BFPT_DWORD15_QER_MASK			GENMASK(22, 20)
1996 #define BFPT_DWORD15_QER_NONE			(0x0UL << 20) /* Micron */
1997 #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY		(0x1UL << 20)
1998 #define BFPT_DWORD15_QER_SR1_BIT6		(0x2UL << 20) /* Macronix */
1999 #define BFPT_DWORD15_QER_SR2_BIT7		(0x3UL << 20)
2000 #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD		(0x4UL << 20)
2001 #define BFPT_DWORD15_QER_SR2_BIT1		(0x5UL << 20) /* Spansion */
2002 #define BFPT_DWORD15_QER_NONE_111		(0x7UL << 20) /* Gigadevice */
2003 
2004 struct sfdp_bfpt {
2005 	u32	dwords[BFPT_DWORD_MAX];
2006 };
2007 
2008 /* Fast Read settings. */
2009 
2010 static void
spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command * read,u16 half,enum spi_nor_protocol proto)2011 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
2012 				    u16 half,
2013 				    enum spi_nor_protocol proto)
2014 {
2015 	read->num_mode_clocks = (half >> 5) & 0x07;
2016 	read->num_wait_states = (half >> 0) & 0x1f;
2017 	read->opcode = (half >> 8) & 0xff;
2018 	read->proto = proto;
2019 }
2020 
2021 struct sfdp_bfpt_read {
2022 	/* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
2023 	u32			hwcaps;
2024 
2025 	/*
2026 	 * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
2027 	 * whether the Fast Read x-y-z command is supported.
2028 	 */
2029 	u32			supported_dword;
2030 	u32			supported_bit;
2031 
2032 	/*
2033 	 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
2034 	 * encodes the op code, the number of mode clocks and the number of wait
2035 	 * states to be used by Fast Read x-y-z command.
2036 	 */
2037 	u32			settings_dword;
2038 	u32			settings_shift;
2039 
2040 	/* The SPI protocol for this Fast Read x-y-z command. */
2041 	enum spi_nor_protocol	proto;
2042 };
2043 
2044 static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
2045 	/* Fast Read 1-1-2 */
2046 	{
2047 		SNOR_HWCAPS_READ_1_1_2,
2048 		BFPT_DWORD(1), BIT(16),	/* Supported bit */
2049 		BFPT_DWORD(4), 0,	/* Settings */
2050 		SNOR_PROTO_1_1_2,
2051 	},
2052 
2053 	/* Fast Read 1-2-2 */
2054 	{
2055 		SNOR_HWCAPS_READ_1_2_2,
2056 		BFPT_DWORD(1), BIT(20),	/* Supported bit */
2057 		BFPT_DWORD(4), 16,	/* Settings */
2058 		SNOR_PROTO_1_2_2,
2059 	},
2060 
2061 	/* Fast Read 2-2-2 */
2062 	{
2063 		SNOR_HWCAPS_READ_2_2_2,
2064 		BFPT_DWORD(5),  BIT(0),	/* Supported bit */
2065 		BFPT_DWORD(6), 16,	/* Settings */
2066 		SNOR_PROTO_2_2_2,
2067 	},
2068 
2069 	/* Fast Read 1-1-4 */
2070 	{
2071 		SNOR_HWCAPS_READ_1_1_4,
2072 		BFPT_DWORD(1), BIT(22),	/* Supported bit */
2073 		BFPT_DWORD(3), 16,	/* Settings */
2074 		SNOR_PROTO_1_1_4,
2075 	},
2076 
2077 	/* Fast Read 1-4-4 */
2078 	{
2079 		SNOR_HWCAPS_READ_1_4_4,
2080 		BFPT_DWORD(1), BIT(21),	/* Supported bit */
2081 		BFPT_DWORD(3), 0,	/* Settings */
2082 		SNOR_PROTO_1_4_4,
2083 	},
2084 
2085 	/* Fast Read 4-4-4 */
2086 	{
2087 		SNOR_HWCAPS_READ_4_4_4,
2088 		BFPT_DWORD(5), BIT(4),	/* Supported bit */
2089 		BFPT_DWORD(7), 16,	/* Settings */
2090 		SNOR_PROTO_4_4_4,
2091 	},
2092 };
2093 
2094 struct sfdp_bfpt_erase {
2095 	/*
2096 	 * The half-word at offset <shift> in DWORD <dwoard> encodes the
2097 	 * op code and erase sector size to be used by Sector Erase commands.
2098 	 */
2099 	u32			dword;
2100 	u32			shift;
2101 };
2102 
2103 static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
2104 	/* Erase Type 1 in DWORD8 bits[15:0] */
2105 	{BFPT_DWORD(8), 0},
2106 
2107 	/* Erase Type 2 in DWORD8 bits[31:16] */
2108 	{BFPT_DWORD(8), 16},
2109 
2110 	/* Erase Type 3 in DWORD9 bits[15:0] */
2111 	{BFPT_DWORD(9), 0},
2112 
2113 	/* Erase Type 4 in DWORD9 bits[31:16] */
2114 	{BFPT_DWORD(9), 16},
2115 };
2116 
2117 static int spi_nor_hwcaps_read2cmd(u32 hwcaps);
2118 
2119 /**
2120  * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
2121  * @nor:		pointer to a 'struct spi_nor'
2122  * @bfpt_header:	pointer to the 'struct sfdp_parameter_header' describing
2123  *			the Basic Flash Parameter Table length and version
2124  * @params:		pointer to the 'struct spi_nor_flash_parameter' to be
2125  *			filled
2126  *
2127  * The Basic Flash Parameter Table is the main and only mandatory table as
2128  * defined by the SFDP (JESD216) specification.
2129  * It provides us with the total size (memory density) of the data array and
2130  * the number of address bytes for Fast Read, Page Program and Sector Erase
2131  * commands.
2132  * For Fast READ commands, it also gives the number of mode clock cycles and
2133  * wait states (regrouped in the number of dummy clock cycles) for each
2134  * supported instruction op code.
2135  * For Page Program, the page size is now available since JESD216 rev A, however
2136  * the supported instruction op codes are still not provided.
2137  * For Sector Erase commands, this table stores the supported instruction op
2138  * codes and the associated sector sizes.
2139  * Finally, the Quad Enable Requirements (QER) are also available since JESD216
2140  * rev A. The QER bits encode the manufacturer dependent procedure to be
2141  * executed to set the Quad Enable (QE) bit in some internal register of the
2142  * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
2143  * sending any Quad SPI command to the memory. Actually, setting the QE bit
2144  * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
2145  * and IO3 hence enabling 4 (Quad) I/O lines.
2146  *
2147  * Return: 0 on success, -errno otherwise.
2148  */
spi_nor_parse_bfpt(struct spi_nor * nor,const struct sfdp_parameter_header * bfpt_header,struct spi_nor_flash_parameter * params)2149 static int spi_nor_parse_bfpt(struct spi_nor *nor,
2150 			      const struct sfdp_parameter_header *bfpt_header,
2151 			      struct spi_nor_flash_parameter *params)
2152 {
2153 	struct mtd_info *mtd = &nor->mtd;
2154 	struct sfdp_bfpt bfpt;
2155 	size_t len;
2156 	int i, cmd, err;
2157 	u32 addr;
2158 	u16 half;
2159 
2160 	/* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
2161 	if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
2162 		return -EINVAL;
2163 
2164 	/* Read the Basic Flash Parameter Table. */
2165 	len = min_t(size_t, sizeof(bfpt),
2166 		    bfpt_header->length * sizeof(u32));
2167 	addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
2168 	memset(&bfpt, 0, sizeof(bfpt));
2169 	err = spi_nor_read_sfdp(nor,  addr, len, &bfpt);
2170 	if (err < 0)
2171 		return err;
2172 
2173 	/* Fix endianness of the BFPT DWORDs. */
2174 	for (i = 0; i < BFPT_DWORD_MAX; i++)
2175 		bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]);
2176 
2177 	/* Number of address bytes. */
2178 	switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
2179 	case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
2180 		nor->addr_width = 3;
2181 		break;
2182 
2183 	case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
2184 		nor->addr_width = 4;
2185 		break;
2186 
2187 	default:
2188 		break;
2189 	}
2190 
2191 	/* Flash Memory Density (in bits). */
2192 	params->size = bfpt.dwords[BFPT_DWORD(2)];
2193 	if (params->size & BIT(31)) {
2194 		params->size &= ~BIT(31);
2195 
2196 		/*
2197 		 * Prevent overflows on params->size. Anyway, a NOR of 2^64
2198 		 * bits is unlikely to exist so this error probably means
2199 		 * the BFPT we are reading is corrupted/wrong.
2200 		 */
2201 		if (params->size > 63)
2202 			return -EINVAL;
2203 
2204 		params->size = 1ULL << params->size;
2205 	} else {
2206 		params->size++;
2207 	}
2208 	params->size >>= 3; /* Convert to bytes. */
2209 
2210 	/* Fast Read settings. */
2211 	for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
2212 		const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
2213 		struct spi_nor_read_command *read;
2214 
2215 		if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
2216 			params->hwcaps.mask &= ~rd->hwcaps;
2217 			continue;
2218 		}
2219 
2220 		params->hwcaps.mask |= rd->hwcaps;
2221 		cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
2222 		read = &params->reads[cmd];
2223 		half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
2224 		spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
2225 	}
2226 
2227 	/* Sector Erase settings. */
2228 	for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
2229 		const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
2230 		u32 erasesize;
2231 		u8 opcode;
2232 
2233 		half = bfpt.dwords[er->dword] >> er->shift;
2234 		erasesize = half & 0xff;
2235 
2236 		/* erasesize == 0 means this Erase Type is not supported. */
2237 		if (!erasesize)
2238 			continue;
2239 
2240 		erasesize = 1U << erasesize;
2241 		opcode = (half >> 8) & 0xff;
2242 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
2243 		if (erasesize == SZ_4K) {
2244 			nor->erase_opcode = opcode;
2245 			mtd->erasesize = erasesize;
2246 			break;
2247 		}
2248 #endif
2249 		if (!mtd->erasesize || mtd->erasesize < erasesize) {
2250 			nor->erase_opcode = opcode;
2251 			mtd->erasesize = erasesize;
2252 		}
2253 	}
2254 
2255 	/* Stop here if not JESD216 rev A or later. */
2256 	if (bfpt_header->length < BFPT_DWORD_MAX)
2257 		return 0;
2258 
2259 	/* Page size: this field specifies 'N' so the page size = 2^N bytes. */
2260 	params->page_size = bfpt.dwords[BFPT_DWORD(11)];
2261 	params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK;
2262 	params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
2263 	params->page_size = 1U << params->page_size;
2264 
2265 	/* Quad Enable Requirements. */
2266 	switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
2267 	case BFPT_DWORD15_QER_NONE:
2268 		params->quad_enable = sst_cr_quad_enable;
2269 		break;
2270 	case BFPT_DWORD15_QER_NONE_111:
2271 		params->quad_enable = NULL;
2272 		break;
2273 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2274 	case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
2275 	case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
2276 		params->quad_enable = spansion_no_read_cr_quad_enable;
2277 		break;
2278 #endif
2279 #ifdef CONFIG_SPI_FLASH_MACRONIX
2280 	case BFPT_DWORD15_QER_SR1_BIT6:
2281 		params->quad_enable = macronix_quad_enable;
2282 		break;
2283 #endif
2284 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2285 	case BFPT_DWORD15_QER_SR2_BIT1:
2286 		params->quad_enable = spansion_read_cr_quad_enable;
2287 		break;
2288 #endif
2289 	default:
2290 		return -EINVAL;
2291 	}
2292 
2293 	return 0;
2294 }
2295 
2296 /**
2297  * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
2298  * @nor:		pointer to a 'struct spi_nor'
2299  * @params:		pointer to the 'struct spi_nor_flash_parameter' to be
2300  *			filled
2301  *
2302  * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
2303  * specification. This is a standard which tends to supported by almost all
2304  * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
2305  * runtime the main parameters needed to perform basic SPI flash operations such
2306  * as Fast Read, Page Program or Sector Erase commands.
2307  *
2308  * Return: 0 on success, -errno otherwise.
2309  */
spi_nor_parse_sfdp(struct spi_nor * nor,struct spi_nor_flash_parameter * params)2310 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2311 			      struct spi_nor_flash_parameter *params)
2312 {
2313 	const struct sfdp_parameter_header *param_header, *bfpt_header;
2314 	struct sfdp_parameter_header *param_headers = NULL;
2315 	struct sfdp_header header;
2316 	size_t psize;
2317 	int i, err;
2318 
2319 	/* Get the SFDP header. */
2320 	err = spi_nor_read_sfdp(nor, 0, sizeof(header), &header);
2321 	if (err < 0)
2322 		return err;
2323 
2324 	/* Check the SFDP header version. */
2325 	if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
2326 	    header.major != SFDP_JESD216_MAJOR)
2327 		return -EINVAL;
2328 
2329 	/*
2330 	 * Verify that the first and only mandatory parameter header is a
2331 	 * Basic Flash Parameter Table header as specified in JESD216.
2332 	 */
2333 	bfpt_header = &header.bfpt_header;
2334 	if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
2335 	    bfpt_header->major != SFDP_JESD216_MAJOR)
2336 		return -EINVAL;
2337 
2338 	/*
2339 	 * Allocate memory then read all parameter headers with a single
2340 	 * Read SFDP command. These parameter headers will actually be parsed
2341 	 * twice: a first time to get the latest revision of the basic flash
2342 	 * parameter table, then a second time to handle the supported optional
2343 	 * tables.
2344 	 * Hence we read the parameter headers once for all to reduce the
2345 	 * processing time. Also we use kmalloc() instead of devm_kmalloc()
2346 	 * because we don't need to keep these parameter headers: the allocated
2347 	 * memory is always released with kfree() before exiting this function.
2348 	 */
2349 	if (header.nph) {
2350 		psize = header.nph * sizeof(*param_headers);
2351 
2352 		param_headers = kmalloc(psize, GFP_KERNEL);
2353 		if (!param_headers)
2354 			return -ENOMEM;
2355 
2356 		err = spi_nor_read_sfdp(nor, sizeof(header),
2357 					psize, param_headers);
2358 		if (err < 0) {
2359 			dev_err(dev, "failed to read SFDP parameter headers\n");
2360 			goto exit;
2361 		}
2362 	}
2363 
2364 	/*
2365 	 * Check other parameter headers to get the latest revision of
2366 	 * the basic flash parameter table.
2367 	 */
2368 	for (i = 0; i < header.nph; i++) {
2369 		param_header = &param_headers[i];
2370 
2371 		if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
2372 		    param_header->major == SFDP_JESD216_MAJOR &&
2373 		    (param_header->minor > bfpt_header->minor ||
2374 		     (param_header->minor == bfpt_header->minor &&
2375 		      param_header->length > bfpt_header->length)))
2376 			bfpt_header = param_header;
2377 	}
2378 
2379 	err = spi_nor_parse_bfpt(nor, bfpt_header, params);
2380 	if (err)
2381 		goto exit;
2382 
2383 	/* Parse other parameter headers. */
2384 	for (i = 0; i < header.nph; i++) {
2385 		param_header = &param_headers[i];
2386 
2387 		switch (SFDP_PARAM_HEADER_ID(param_header)) {
2388 		case SFDP_SECTOR_MAP_ID:
2389 			dev_info(dev, "non-uniform erase sector maps are not supported yet.\n");
2390 			break;
2391 
2392 		default:
2393 			break;
2394 		}
2395 
2396 		if (err)
2397 			goto exit;
2398 	}
2399 
2400 exit:
2401 	kfree(param_headers);
2402 	return err;
2403 }
2404 #else
spi_nor_parse_sfdp(struct spi_nor * nor,struct spi_nor_flash_parameter * params)2405 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2406 			      struct spi_nor_flash_parameter *params)
2407 {
2408 	return -EINVAL;
2409 }
2410 #endif /* SPI_FLASH_SFDP_SUPPORT */
2411 
spi_nor_init_params(struct spi_nor * nor,const struct flash_info * info,struct spi_nor_flash_parameter * params)2412 static int spi_nor_init_params(struct spi_nor *nor,
2413 			       const struct flash_info *info,
2414 			       struct spi_nor_flash_parameter *params)
2415 {
2416 	int ret;
2417 	/* Set legacy flash parameters as default. */
2418 	memset(params, 0, sizeof(*params));
2419 
2420 	/* Set SPI NOR sizes. */
2421 	params->size = info->sector_size * info->n_sectors;
2422 	params->page_size = info->page_size;
2423 
2424 	/* (Fast) Read settings. */
2425 	params->hwcaps.mask |= SNOR_HWCAPS_READ;
2426 	spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
2427 				  0, 0, SPINOR_OP_READ,
2428 				  SNOR_PROTO_1_1_1);
2429 
2430 	if (!(info->flags & SPI_NOR_NO_FR)) {
2431 		params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2432 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
2433 					  0, 8, SPINOR_OP_READ_FAST,
2434 					  SNOR_PROTO_1_1_1);
2435 #ifdef CONFIG_SPI_FLASH_SPANSION
2436 		if (cypress_s25hx_t(info))
2437 			params->reads[SNOR_CMD_READ_FAST].num_mode_clocks = 8;
2438 #endif
2439 	}
2440 
2441 	if (info->flags & SPI_NOR_DUAL_READ) {
2442 		params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2443 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_2],
2444 					  0, 8, SPINOR_OP_READ_1_1_2,
2445 					  SNOR_PROTO_1_1_2);
2446 	}
2447 
2448 	if (info->flags & SPI_NOR_QUAD_READ) {
2449 		params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2450 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
2451 					  0, 8, SPINOR_OP_READ_1_1_4,
2452 					  SNOR_PROTO_1_1_4);
2453 	}
2454 
2455 	/* Page Program settings. */
2456 	params->hwcaps.mask |= SNOR_HWCAPS_PP;
2457 	spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP],
2458 				SPINOR_OP_PP, SNOR_PROTO_1_1_1);
2459 
2460 	if (info->flags & SPI_NOR_QUAD_READ) {
2461 		params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
2462 		spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_1_1_4],
2463 					SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4);
2464 	}
2465 
2466 	/* Select the procedure to set the Quad Enable bit. */
2467 	if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
2468 				   SNOR_HWCAPS_PP_QUAD)) {
2469 		switch (JEDEC_MFR(info)) {
2470 #ifdef CONFIG_SPI_FLASH_MACRONIX
2471 		case SNOR_MFR_MACRONIX:
2472 			params->quad_enable = macronix_quad_enable;
2473 			break;
2474 #endif
2475 		case SNOR_MFR_ST:
2476 		case SNOR_MFR_MICRON:
2477 		case SNOR_MFR_ISSI:
2478 			break;
2479 #ifdef CONFIG_SPI_FLASH_SPANSION
2480 		case SNOR_MFR_CYPRESS:
2481 			if (info->id[1] == 0x2a || info->id[1] == 0x2b) {
2482 				params->quad_enable = spansion_quad_enable_volatile;
2483 			}
2484 			break;
2485 #endif
2486 
2487 		default:
2488 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2489 			/* Kept only for backward compatibility purpose. */
2490 			params->quad_enable = spansion_read_cr_quad_enable;
2491 #endif
2492 			break;
2493 		}
2494 	}
2495 
2496 	/* Override the parameters with data read from SFDP tables. */
2497 	nor->addr_width = 0;
2498 	nor->mtd.erasesize = 0;
2499 	if ((info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) &&
2500 	    !(info->flags & SPI_NOR_SKIP_SFDP)) {
2501 		struct spi_nor_flash_parameter sfdp_params;
2502 
2503 		memcpy(&sfdp_params, params, sizeof(sfdp_params));
2504 		if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
2505 			nor->addr_width = 0;
2506 			nor->mtd.erasesize = 0;
2507 		} else {
2508 			memcpy(params, &sfdp_params, sizeof(*params));
2509 #ifdef CONFIG_SPI_FLASH_SPANSION
2510 			if (cypress_s25hx_t(info)) {
2511 				/* BFPT fixup */
2512 				nor->erase_opcode = SPINOR_OP_SE_4B;
2513 				nor->mtd.erasesize = info->sector_size;
2514 				ret = set_4byte(nor, info, 1);
2515 				if (ret)
2516 					return ret;
2517 
2518 				nor->addr_width = 4;
2519 
2520 				/* SFDP fixup */
2521 				/* Default page size is 256-byte, but BFPT reports 512-byte */
2522 				params->page_size = 256;
2523 				/* READ_FAST_4B (0Ch) requires mode cycles*/
2524 				params->reads[SNOR_CMD_READ_FAST].opcode = SPINOR_OP_READ_FAST_4B;
2525 				params->reads[SNOR_CMD_READ_FAST].num_mode_clocks = 8;
2526 				/* PP_1_1_4 is not supported */
2527 				params->hwcaps.mask &= ~SNOR_HWCAPS_PP_1_1_4;
2528 				/* Use volatile register to enable quad */
2529 				params->quad_enable = spansion_quad_enable_volatile;
2530 			}
2531 #endif
2532 		}
2533 
2534 		/* need to disable hold/reset pin feature */
2535 		if (JEDEC_MFR(info) == SNOR_MFR_ST)
2536 			params->quad_enable = micron_read_cr_quad_enable;
2537 
2538 		if (JEDEC_MFR(info) == SNOR_MFR_GIGADEVICE)
2539 			params->quad_enable = winbond_sr2_bit1_quad_enable;
2540 	}
2541 
2542 	return 0;
2543 }
2544 
spi_nor_hwcaps2cmd(u32 hwcaps,const int table[][2],size_t size)2545 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
2546 {
2547 	size_t i;
2548 
2549 	for (i = 0; i < size; i++)
2550 		if (table[i][0] == (int)hwcaps)
2551 			return table[i][1];
2552 
2553 	return -EINVAL;
2554 }
2555 
spi_nor_hwcaps_read2cmd(u32 hwcaps)2556 static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
2557 {
2558 	static const int hwcaps_read2cmd[][2] = {
2559 		{ SNOR_HWCAPS_READ,		SNOR_CMD_READ },
2560 		{ SNOR_HWCAPS_READ_FAST,	SNOR_CMD_READ_FAST },
2561 		{ SNOR_HWCAPS_READ_1_1_1_DTR,	SNOR_CMD_READ_1_1_1_DTR },
2562 		{ SNOR_HWCAPS_READ_1_1_2,	SNOR_CMD_READ_1_1_2 },
2563 		{ SNOR_HWCAPS_READ_1_2_2,	SNOR_CMD_READ_1_2_2 },
2564 		{ SNOR_HWCAPS_READ_2_2_2,	SNOR_CMD_READ_2_2_2 },
2565 		{ SNOR_HWCAPS_READ_1_2_2_DTR,	SNOR_CMD_READ_1_2_2_DTR },
2566 		{ SNOR_HWCAPS_READ_1_1_4,	SNOR_CMD_READ_1_1_4 },
2567 		{ SNOR_HWCAPS_READ_1_4_4,	SNOR_CMD_READ_1_4_4 },
2568 		{ SNOR_HWCAPS_READ_4_4_4,	SNOR_CMD_READ_4_4_4 },
2569 		{ SNOR_HWCAPS_READ_1_4_4_DTR,	SNOR_CMD_READ_1_4_4_DTR },
2570 		{ SNOR_HWCAPS_READ_1_1_8,	SNOR_CMD_READ_1_1_8 },
2571 		{ SNOR_HWCAPS_READ_1_8_8,	SNOR_CMD_READ_1_8_8 },
2572 		{ SNOR_HWCAPS_READ_8_8_8,	SNOR_CMD_READ_8_8_8 },
2573 		{ SNOR_HWCAPS_READ_1_8_8_DTR,	SNOR_CMD_READ_1_8_8_DTR },
2574 	};
2575 
2576 	return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
2577 				  ARRAY_SIZE(hwcaps_read2cmd));
2578 }
2579 
spi_nor_hwcaps_pp2cmd(u32 hwcaps)2580 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
2581 {
2582 	static const int hwcaps_pp2cmd[][2] = {
2583 		{ SNOR_HWCAPS_PP,		SNOR_CMD_PP },
2584 		{ SNOR_HWCAPS_PP_1_1_4,		SNOR_CMD_PP_1_1_4 },
2585 		{ SNOR_HWCAPS_PP_1_4_4,		SNOR_CMD_PP_1_4_4 },
2586 		{ SNOR_HWCAPS_PP_4_4_4,		SNOR_CMD_PP_4_4_4 },
2587 		{ SNOR_HWCAPS_PP_1_1_8,		SNOR_CMD_PP_1_1_8 },
2588 		{ SNOR_HWCAPS_PP_1_8_8,		SNOR_CMD_PP_1_8_8 },
2589 		{ SNOR_HWCAPS_PP_8_8_8,		SNOR_CMD_PP_8_8_8 },
2590 	};
2591 
2592 	return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
2593 				  ARRAY_SIZE(hwcaps_pp2cmd));
2594 }
2595 
spi_nor_select_read(struct spi_nor * nor,const struct spi_nor_flash_parameter * params,u32 shared_hwcaps)2596 static int spi_nor_select_read(struct spi_nor *nor,
2597 			       const struct spi_nor_flash_parameter *params,
2598 			       u32 shared_hwcaps)
2599 {
2600 	int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
2601 	const struct spi_nor_read_command *read;
2602 
2603 	if (best_match < 0)
2604 		return -EINVAL;
2605 
2606 	cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
2607 	if (cmd < 0)
2608 		return -EINVAL;
2609 
2610 	read = &params->reads[cmd];
2611 	nor->read_opcode = read->opcode;
2612 	nor->read_proto = read->proto;
2613 
2614 	/*
2615 	 * In the spi-nor framework, we don't need to make the difference
2616 	 * between mode clock cycles and wait state clock cycles.
2617 	 * Indeed, the value of the mode clock cycles is used by a QSPI
2618 	 * flash memory to know whether it should enter or leave its 0-4-4
2619 	 * (Continuous Read / XIP) mode.
2620 	 * eXecution In Place is out of the scope of the mtd sub-system.
2621 	 * Hence we choose to merge both mode and wait state clock cycles
2622 	 * into the so called dummy clock cycles.
2623 	 */
2624 	nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
2625 	return 0;
2626 }
2627 
spi_nor_select_pp(struct spi_nor * nor,const struct spi_nor_flash_parameter * params,u32 shared_hwcaps)2628 static int spi_nor_select_pp(struct spi_nor *nor,
2629 			     const struct spi_nor_flash_parameter *params,
2630 			     u32 shared_hwcaps)
2631 {
2632 	int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
2633 	const struct spi_nor_pp_command *pp;
2634 
2635 	if (best_match < 0)
2636 		return -EINVAL;
2637 
2638 	cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
2639 	if (cmd < 0)
2640 		return -EINVAL;
2641 
2642 	pp = &params->page_programs[cmd];
2643 	nor->program_opcode = pp->opcode;
2644 	nor->write_proto = pp->proto;
2645 	return 0;
2646 }
2647 
spi_nor_select_erase(struct spi_nor * nor,const struct flash_info * info)2648 static int spi_nor_select_erase(struct spi_nor *nor,
2649 				const struct flash_info *info)
2650 {
2651 	struct mtd_info *mtd = &nor->mtd;
2652 
2653 	/* Do nothing if already configured from SFDP. */
2654 	if (mtd->erasesize)
2655 		return 0;
2656 
2657 #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
2658 	/* prefer "small sector" erase if possible */
2659 	if (info->flags & SECT_4K) {
2660 		nor->erase_opcode = SPINOR_OP_BE_4K;
2661 		mtd->erasesize = 4096;
2662 	} else if (info->flags & SECT_4K_PMC) {
2663 		nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
2664 		mtd->erasesize = 4096;
2665 	} else
2666 #endif
2667 	{
2668 		nor->erase_opcode = SPINOR_OP_SE;
2669 		mtd->erasesize = info->sector_size;
2670 	}
2671 	return 0;
2672 }
2673 
spi_nor_setup(struct spi_nor * nor,const struct flash_info * info,const struct spi_nor_flash_parameter * params,const struct spi_nor_hwcaps * hwcaps)2674 static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
2675 			 const struct spi_nor_flash_parameter *params,
2676 			 const struct spi_nor_hwcaps *hwcaps)
2677 {
2678 	u32 ignored_mask, shared_mask;
2679 	bool enable_quad_io;
2680 	int err;
2681 
2682 	/*
2683 	 * Keep only the hardware capabilities supported by both the SPI
2684 	 * controller and the SPI flash memory.
2685 	 */
2686 	shared_mask = hwcaps->mask & params->hwcaps.mask;
2687 
2688 	/* SPI n-n-n protocols are not supported yet. */
2689 	ignored_mask = (SNOR_HWCAPS_READ_1_1_1_DTR |
2690 			SNOR_HWCAPS_READ_1_2_2 |
2691 			SNOR_HWCAPS_READ_1_2_2_DTR |
2692 			SNOR_HWCAPS_READ_2_2_2 |
2693 			SNOR_HWCAPS_READ_1_4_4 |
2694 			SNOR_HWCAPS_READ_1_4_4_DTR |
2695 			SNOR_HWCAPS_READ_4_4_4 |
2696 			SNOR_HWCAPS_READ_8_8_8 |
2697 			SNOR_HWCAPS_PP_1_4_4 |
2698 			SNOR_HWCAPS_PP_4_4_4 |
2699 			SNOR_HWCAPS_PP_8_8_8);
2700 	if (shared_mask & ignored_mask) {
2701 		dev_dbg(nor->dev,
2702 			"SPI n-n-n protocols are not supported yet.\n");
2703 		shared_mask &= ~ignored_mask;
2704 	}
2705 
2706 	/* Select the (Fast) Read command. */
2707 	err = spi_nor_select_read(nor, params, shared_mask);
2708 	if (err) {
2709 		dev_dbg(nor->dev,
2710 			"can't select read settings supported by both the SPI controller and memory.\n");
2711 		return err;
2712 	}
2713 
2714 	/* Select the Page Program command. */
2715 	err = spi_nor_select_pp(nor, params, shared_mask);
2716 	if (err) {
2717 		dev_dbg(nor->dev,
2718 			"can't select write settings supported by both the SPI controller and memory.\n");
2719 		return err;
2720 	}
2721 
2722 	/* Select the Sector Erase command. */
2723 	err = spi_nor_select_erase(nor, info);
2724 	if (err) {
2725 		dev_dbg(nor->dev,
2726 			"can't select erase settings supported by both the SPI controller and memory.\n");
2727 		return err;
2728 	}
2729 
2730 	/* Enable Quad I/O if needed. */
2731 	enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
2732 			  spi_nor_get_protocol_width(nor->write_proto) == 4);
2733 	if (enable_quad_io && params->quad_enable)
2734 		nor->quad_enable = params->quad_enable;
2735 	else
2736 		nor->quad_enable = NULL;
2737 
2738 	return 0;
2739 }
2740 
spi_nor_init(struct spi_nor * nor)2741 static int spi_nor_init(struct spi_nor *nor)
2742 {
2743 	int err;
2744 
2745 	/*
2746 	 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
2747 	 * with the software protection bits set
2748 	 */
2749 	if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
2750 	    JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
2751 	    JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
2752 	    nor->info->flags & SPI_NOR_HAS_LOCK) {
2753 		write_enable(nor);
2754 		write_sr(nor, 0);
2755 		spi_nor_wait_till_ready(nor);
2756 	}
2757 
2758 	if (nor->quad_enable) {
2759 		err = nor->quad_enable(nor);
2760 		if (err) {
2761 			dev_dbg(nor->dev, "quad mode not supported\n");
2762 			return err;
2763 		}
2764 	}
2765 
2766 	if (nor->addr_width == 4 &&
2767 	    (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION)) {
2768 
2769 		/*
2770 		 * If the RESET# pin isn't hooked up properly, or the system
2771 		 * otherwise doesn't perform a reset command in the boot
2772 		 * sequence, it's impossible to 100% protect against unexpected
2773 		 * reboots (e.g., crashes). Warn the user (or hopefully, system
2774 		 * designer) that this is bad.
2775 		 */
2776 		if (nor->flags & SNOR_F_BROKEN_RESET)
2777 			printf("enabling reset hack; may not recover from unexpected reboots\n");
2778 		set_4byte(nor, nor->info, 1);
2779 	}
2780 
2781 	return 0;
2782 }
2783 
spi_nor_scan(struct spi_nor * nor)2784 int spi_nor_scan(struct spi_nor *nor)
2785 {
2786 	struct spi_nor_flash_parameter params;
2787 	const struct flash_info *info = NULL;
2788 	struct mtd_info *mtd = &nor->mtd;
2789 	struct spi_nor_hwcaps hwcaps = {
2790 		.mask = SNOR_HWCAPS_READ |
2791 			SNOR_HWCAPS_READ_FAST |
2792 			SNOR_HWCAPS_PP,
2793 	};
2794 	struct spi_slave *spi = nor->spi;
2795 	int ret;
2796 
2797 	/* Reset SPI protocol for all commands. */
2798 	nor->reg_proto = SNOR_PROTO_1_1_1;
2799 	nor->read_proto = SNOR_PROTO_1_1_1;
2800 	nor->write_proto = SNOR_PROTO_1_1_1;
2801 	nor->read = spi_nor_read_data;
2802 	nor->write = spi_nor_write_data;
2803 	nor->read_reg = spi_nor_read_reg;
2804 	nor->write_reg = spi_nor_write_reg;
2805 	nor->flash_lock_by_host_ctrl = spi_nor_wlock_by_host_ctrl;
2806 	nor->flash_unlock_by_host_ctrl = spi_nor_wunlock_by_host_ctrl;
2807 
2808 	if (spi->mode & SPI_RX_QUAD) {
2809 		hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2810 
2811 		if (spi->mode & SPI_TX_QUAD)
2812 			hwcaps.mask |= (SNOR_HWCAPS_READ_1_4_4 |
2813 					SNOR_HWCAPS_PP_1_1_4 |
2814 					SNOR_HWCAPS_PP_1_4_4);
2815 	} else if (spi->mode & SPI_RX_DUAL) {
2816 		hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2817 
2818 		if (spi->mode & SPI_TX_DUAL)
2819 			hwcaps.mask |= SNOR_HWCAPS_READ_1_2_2;
2820 	}
2821 
2822 	info = spi_nor_read_id(nor);
2823 	if (IS_ERR_OR_NULL(info))
2824 		return -ENOENT;
2825 	/* Parse the Serial Flash Discoverable Parameters table. */
2826 	ret = spi_nor_init_params(nor, info, &params);
2827 	if (ret)
2828 		return ret;
2829 
2830 	if (!mtd->name)
2831 		mtd->name = info->name;
2832 	mtd->priv = nor;
2833 	mtd->type = MTD_NORFLASH;
2834 	mtd->writesize = 1;
2835 	mtd->flags = MTD_CAP_NORFLASH;
2836 	mtd->size = params.size;
2837 	mtd->_erase = spi_nor_erase;
2838 	mtd->_read = spi_nor_read;
2839 
2840 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
2841 	/* NOR protection support for STmicro/Micron chips and similar */
2842 	if (JEDEC_MFR(info) == SNOR_MFR_ST ||
2843 	    JEDEC_MFR(info) == SNOR_MFR_MICRON ||
2844 	    JEDEC_MFR(info) == SNOR_MFR_SST ||
2845 			info->flags & SPI_NOR_HAS_LOCK) {
2846 		nor->flash_lock = stm_lock;
2847 		nor->flash_unlock = stm_unlock;
2848 		nor->flash_is_locked = stm_is_locked;
2849 	}
2850 #endif
2851 
2852 #ifdef CONFIG_SPI_FLASH_SST
2853 	/* sst nor chips use AAI word program */
2854 	if (info->flags & SST_WRITE)
2855 		mtd->_write = sst_write;
2856 	else
2857 #endif
2858 		mtd->_write = spi_nor_write;
2859 
2860 	if (info->flags & USE_FSR)
2861 		nor->flags |= SNOR_F_USE_FSR;
2862 	if (info->flags & SPI_NOR_HAS_TB)
2863 		nor->flags |= SNOR_F_HAS_SR_TB;
2864 	if (info->flags & NO_CHIP_ERASE)
2865 		nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
2866 	if (info->flags & USE_CLSR)
2867 		nor->flags |= SNOR_F_USE_CLSR;
2868 
2869 	if (info->flags & SPI_NOR_NO_ERASE)
2870 		mtd->flags |= MTD_NO_ERASE;
2871 
2872 	nor->page_size = params.page_size;
2873 	mtd->writebufsize = nor->page_size;
2874 
2875 #ifdef CONFIG_SPI_FLASH_SPANSION
2876 	if (cypress_s25hx_t(info)) {
2877 		/*
2878 		 * The Cypress Semper family has transparent ECC. To preserve
2879 		 * ECC enabled, multi-pass programming within the same 16-byte
2880 		 * ECC data unit needs to be avoided. Set writesize to the page
2881 		 * size and remove the MTD_BIT_WRITEABLE flag in mtd_info to
2882 		 * prevent multi-pass programming.
2883 		 */
2884 		nor->mtd.writesize = params.page_size;
2885 		nor->mtd.flags &= ~MTD_BIT_WRITEABLE;
2886 		ret = s25hx_t_setup(nor, info);
2887 		if (ret) {
2888 			dev_err(nor->dev, "fail to setup s25hx_t flash\n");
2889 			return ret;
2890 		}
2891 	}
2892 #endif
2893 
2894 	/* Some devices cannot do fast-read, no matter what DT tells us */
2895 	if ((info->flags & SPI_NOR_NO_FR) || (spi->mode & SPI_RX_SLOW))
2896 		params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
2897 
2898 	/*
2899 	 * Configure the SPI memory:
2900 	 * - select op codes for (Fast) Read, Page Program and Sector Erase.
2901 	 * - set the number of dummy cycles (mode cycles + wait states).
2902 	 * - set the SPI protocols for register and memory accesses.
2903 	 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
2904 	 */
2905 	ret = spi_nor_setup(nor, info, &params, &hwcaps);
2906 	if (ret)
2907 		return ret;
2908 
2909 	if (nor->addr_width) {
2910 		/* already configured from SFDP */
2911 	} else if (info->addr_width) {
2912 		nor->addr_width = info->addr_width;
2913 	} else if (mtd->size > SZ_16M) {
2914 #ifndef CONFIG_SPI_FLASH_BAR
2915 		/* enable 4-byte addressing if the device exceeds 16MiB */
2916 		nor->addr_width = 4;
2917 		if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
2918 		    info->flags & SPI_NOR_4B_OPCODES)
2919 			spi_nor_set_4byte_opcodes(nor, info);
2920 #else
2921 		/* Configure the BAR - discover bank cmds and read current bank */
2922 		nor->addr_width = 3;
2923 		ret = read_bar(nor, info);
2924 		if (ret < 0)
2925 			return ret;
2926 #endif
2927 	} else {
2928 		nor->addr_width = 3;
2929 	}
2930 
2931 	if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
2932 		dev_dbg(dev, "address width is too large: %u\n",
2933 			nor->addr_width);
2934 		return -EINVAL;
2935 	}
2936 
2937 	/* Send all the required SPI flash commands to initialize device */
2938 	nor->info = info;
2939 	ret = spi_nor_init(nor);
2940 	if (ret)
2941 		return ret;
2942 
2943 	nor->name = mtd->name;
2944 	nor->size = mtd->size;
2945 	nor->erase_size = mtd->erasesize;
2946 	nor->sector_size = mtd->erasesize;
2947 
2948 #ifndef CONFIG_SPL_BUILD
2949 	printf("SF: Detected %s with page size ", nor->name);
2950 	print_size(nor->page_size, ", erase size ");
2951 	print_size(nor->erase_size, ", total ");
2952 	print_size(nor->size, "");
2953 	puts("\n");
2954 #endif
2955 
2956 	return 0;
2957 }
2958 
2959 /* U-Boot specific functions, need to extend MTD to support these */
spi_flash_cmd_get_sw_write_prot(struct spi_nor * nor)2960 int spi_flash_cmd_get_sw_write_prot(struct spi_nor *nor)
2961 {
2962 	int sr = read_sr(nor);
2963 
2964 	if (sr < 0)
2965 		return sr;
2966 
2967 	return (sr >> 2) & 7;
2968 }
2969 
spi_flash_wlock_by_host_ctrl(struct spi_nor * nor,u32 offset,size_t len)2970 int spi_flash_wlock_by_host_ctrl(struct spi_nor *nor, u32 offset, size_t len)
2971 {
2972 	nor->flash_lock_by_host_ctrl(nor, offset, len);
2973 	return 0;
2974 }
2975 
spi_flash_wunlock_by_host_ctrl(struct spi_nor * nor,u32 offset,size_t len)2976 int spi_flash_wunlock_by_host_ctrl(struct spi_nor *nor, u32 offset, size_t len)
2977 {
2978 	nor->flash_unlock_by_host_ctrl(nor, offset, len);
2979 	return 0;
2980 }
2981 
2982