xref: /openbmc/u-boot/drivers/mtd/spi/spi-nor-tiny.c (revision 50e24381)
1*778572d7SVignesh R // SPDX-License-Identifier: GPL-2.0
2*778572d7SVignesh R /*
3*778572d7SVignesh R  * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with
4*778572d7SVignesh R  * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
5*778572d7SVignesh R  *
6*778572d7SVignesh R  * Copyright (C) 2005, Intec Automation Inc.
7*778572d7SVignesh R  * Copyright (C) 2014, Freescale Semiconductor, Inc.
8*778572d7SVignesh R  *
9*778572d7SVignesh R  * Synced from Linux v4.19
10*778572d7SVignesh R  */
11*778572d7SVignesh R 
12*778572d7SVignesh R #include <common.h>
13*778572d7SVignesh R #include <linux/err.h>
14*778572d7SVignesh R #include <linux/errno.h>
15*778572d7SVignesh R #include <linux/log2.h>
16*778572d7SVignesh R #include <linux/math64.h>
17*778572d7SVignesh R #include <linux/sizes.h>
18*778572d7SVignesh R 
19*778572d7SVignesh R #include <linux/mtd/mtd.h>
20*778572d7SVignesh R #include <linux/mtd/spi-nor.h>
21*778572d7SVignesh R #include <spi-mem.h>
22*778572d7SVignesh R #include <spi.h>
23*778572d7SVignesh R 
24*778572d7SVignesh R #include "sf_internal.h"
25*778572d7SVignesh R 
26*778572d7SVignesh R /* Define max times to check status register before we give up. */
27*778572d7SVignesh R 
28*778572d7SVignesh R /*
29*778572d7SVignesh R  * For everything but full-chip erase; probably could be much smaller, but kept
30*778572d7SVignesh R  * around for safety for now
31*778572d7SVignesh R  */
32*778572d7SVignesh R 
33*778572d7SVignesh R #define HZ					CONFIG_SYS_HZ
34*778572d7SVignesh R 
35*778572d7SVignesh R #define DEFAULT_READY_WAIT_JIFFIES		(40UL * HZ)
36*778572d7SVignesh R 
spi_nor_read_write_reg(struct spi_nor * nor,struct spi_mem_op * op,void * buf)37*778572d7SVignesh R static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op
38*778572d7SVignesh R 		*op, void *buf)
39*778572d7SVignesh R {
40*778572d7SVignesh R 	if (op->data.dir == SPI_MEM_DATA_IN)
41*778572d7SVignesh R 		op->data.buf.in = buf;
42*778572d7SVignesh R 	else
43*778572d7SVignesh R 		op->data.buf.out = buf;
44*778572d7SVignesh R 	return spi_mem_exec_op(nor->spi, op);
45*778572d7SVignesh R }
46*778572d7SVignesh R 
spi_nor_read_reg(struct spi_nor * nor,u8 code,u8 * val,int len)47*778572d7SVignesh R static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
48*778572d7SVignesh R {
49*778572d7SVignesh R 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1),
50*778572d7SVignesh R 					  SPI_MEM_OP_NO_ADDR,
51*778572d7SVignesh R 					  SPI_MEM_OP_NO_DUMMY,
52*778572d7SVignesh R 					  SPI_MEM_OP_DATA_IN(len, NULL, 1));
53*778572d7SVignesh R 	int ret;
54*778572d7SVignesh R 
55*778572d7SVignesh R 	ret = spi_nor_read_write_reg(nor, &op, val);
56*778572d7SVignesh R 	if (ret < 0)
57*778572d7SVignesh R 		dev_dbg(&flash->spimem->spi->dev, "error %d reading %x\n", ret,
58*778572d7SVignesh R 			code);
59*778572d7SVignesh R 
60*778572d7SVignesh R 	return ret;
61*778572d7SVignesh R }
62*778572d7SVignesh R 
spi_nor_write_reg(struct spi_nor * nor,u8 opcode,u8 * buf,int len)63*778572d7SVignesh R static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
64*778572d7SVignesh R {
65*778572d7SVignesh R 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1),
66*778572d7SVignesh R 					  SPI_MEM_OP_NO_ADDR,
67*778572d7SVignesh R 					  SPI_MEM_OP_NO_DUMMY,
68*778572d7SVignesh R 					  SPI_MEM_OP_DATA_OUT(len, NULL, 1));
69*778572d7SVignesh R 
70*778572d7SVignesh R 	return spi_nor_read_write_reg(nor, &op, buf);
71*778572d7SVignesh R }
72*778572d7SVignesh R 
spi_nor_read_data(struct spi_nor * nor,loff_t from,size_t len,u_char * buf)73*778572d7SVignesh R static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
74*778572d7SVignesh R 				 u_char *buf)
75*778572d7SVignesh R {
76*778572d7SVignesh R 	struct spi_mem_op op =
77*778572d7SVignesh R 			SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
78*778572d7SVignesh R 				   SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
79*778572d7SVignesh R 				   SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
80*778572d7SVignesh R 				   SPI_MEM_OP_DATA_IN(len, buf, 1));
81*778572d7SVignesh R 	size_t remaining = len;
82*778572d7SVignesh R 	int ret;
83*778572d7SVignesh R 
84*778572d7SVignesh R 	/* get transfer protocols. */
85*778572d7SVignesh R 	op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
86*778572d7SVignesh R 	op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
87*778572d7SVignesh R 	op.dummy.buswidth = op.addr.buswidth;
88*778572d7SVignesh R 	op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
89*778572d7SVignesh R 
90*778572d7SVignesh R 	/* convert the dummy cycles to the number of bytes */
91*778572d7SVignesh R 	op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
92*778572d7SVignesh R 
93*778572d7SVignesh R 	while (remaining) {
94*778572d7SVignesh R 		op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
95*778572d7SVignesh R 		ret = spi_mem_adjust_op_size(nor->spi, &op);
96*778572d7SVignesh R 		if (ret)
97*778572d7SVignesh R 			return ret;
98*778572d7SVignesh R 
99*778572d7SVignesh R 		ret = spi_mem_exec_op(nor->spi, &op);
100*778572d7SVignesh R 		if (ret)
101*778572d7SVignesh R 			return ret;
102*778572d7SVignesh R 
103*778572d7SVignesh R 		op.addr.val += op.data.nbytes;
104*778572d7SVignesh R 		remaining -= op.data.nbytes;
105*778572d7SVignesh R 		op.data.buf.in += op.data.nbytes;
106*778572d7SVignesh R 	}
107*778572d7SVignesh R 
108*778572d7SVignesh R 	return len;
109*778572d7SVignesh R }
110*778572d7SVignesh R 
111*778572d7SVignesh R #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
112*778572d7SVignesh R /*
113*778572d7SVignesh R  * Read configuration register, returning its value in the
114*778572d7SVignesh R  * location. Return the configuration register value.
115*778572d7SVignesh R  * Returns negative if error occurred.
116*778572d7SVignesh R  */
read_cr(struct spi_nor * nor)117*778572d7SVignesh R static int read_cr(struct spi_nor *nor)
118*778572d7SVignesh R {
119*778572d7SVignesh R 	int ret;
120*778572d7SVignesh R 	u8 val;
121*778572d7SVignesh R 
122*778572d7SVignesh R 	ret = spi_nor_read_reg(nor, SPINOR_OP_RDCR, &val, 1);
123*778572d7SVignesh R 	if (ret < 0) {
124*778572d7SVignesh R 		dev_dbg(nor->dev, "error %d reading CR\n", ret);
125*778572d7SVignesh R 		return ret;
126*778572d7SVignesh R 	}
127*778572d7SVignesh R 
128*778572d7SVignesh R 	return val;
129*778572d7SVignesh R }
130*778572d7SVignesh R #endif
131*778572d7SVignesh R 
132*778572d7SVignesh R /*
133*778572d7SVignesh R  * Write status register 1 byte
134*778572d7SVignesh R  * Returns negative if error occurred.
135*778572d7SVignesh R  */
write_sr(struct spi_nor * nor,u8 val)136*778572d7SVignesh R static inline int write_sr(struct spi_nor *nor, u8 val)
137*778572d7SVignesh R {
138*778572d7SVignesh R 	nor->cmd_buf[0] = val;
139*778572d7SVignesh R 	return spi_nor_write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
140*778572d7SVignesh R }
141*778572d7SVignesh R 
142*778572d7SVignesh R /*
143*778572d7SVignesh R  * Set write enable latch with Write Enable command.
144*778572d7SVignesh R  * Returns negative if error occurred.
145*778572d7SVignesh R  */
write_enable(struct spi_nor * nor)146*778572d7SVignesh R static inline int write_enable(struct spi_nor *nor)
147*778572d7SVignesh R {
148*778572d7SVignesh R 	return spi_nor_write_reg(nor, SPINOR_OP_WREN, NULL, 0);
149*778572d7SVignesh R }
150*778572d7SVignesh R 
151*778572d7SVignesh R /*
152*778572d7SVignesh R  * Send write disable instruction to the chip.
153*778572d7SVignesh R  */
write_disable(struct spi_nor * nor)154*778572d7SVignesh R static inline int write_disable(struct spi_nor *nor)
155*778572d7SVignesh R {
156*778572d7SVignesh R 	return spi_nor_write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
157*778572d7SVignesh R }
158*778572d7SVignesh R 
mtd_to_spi_nor(struct mtd_info * mtd)159*778572d7SVignesh R static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
160*778572d7SVignesh R {
161*778572d7SVignesh R 	return mtd->priv;
162*778572d7SVignesh R }
163*778572d7SVignesh R 
spi_nor_convert_opcode(u8 opcode,const u8 table[][2],size_t size)164*778572d7SVignesh R static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
165*778572d7SVignesh R {
166*778572d7SVignesh R 	size_t i;
167*778572d7SVignesh R 
168*778572d7SVignesh R 	for (i = 0; i < size; i++)
169*778572d7SVignesh R 		if (table[i][0] == opcode)
170*778572d7SVignesh R 			return table[i][1];
171*778572d7SVignesh R 
172*778572d7SVignesh R 	/* No conversion found, keep input op code. */
173*778572d7SVignesh R 	return opcode;
174*778572d7SVignesh R }
175*778572d7SVignesh R 
spi_nor_convert_3to4_read(u8 opcode)176*778572d7SVignesh R static inline u8 spi_nor_convert_3to4_read(u8 opcode)
177*778572d7SVignesh R {
178*778572d7SVignesh R 	static const u8 spi_nor_3to4_read[][2] = {
179*778572d7SVignesh R 		{ SPINOR_OP_READ,	SPINOR_OP_READ_4B },
180*778572d7SVignesh R 		{ SPINOR_OP_READ_FAST,	SPINOR_OP_READ_FAST_4B },
181*778572d7SVignesh R 		{ SPINOR_OP_READ_1_1_2,	SPINOR_OP_READ_1_1_2_4B },
182*778572d7SVignesh R 		{ SPINOR_OP_READ_1_2_2,	SPINOR_OP_READ_1_2_2_4B },
183*778572d7SVignesh R 		{ SPINOR_OP_READ_1_1_4,	SPINOR_OP_READ_1_1_4_4B },
184*778572d7SVignesh R 		{ SPINOR_OP_READ_1_4_4,	SPINOR_OP_READ_1_4_4_4B },
185*778572d7SVignesh R 	};
186*778572d7SVignesh R 
187*778572d7SVignesh R 	return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
188*778572d7SVignesh R 				      ARRAY_SIZE(spi_nor_3to4_read));
189*778572d7SVignesh R }
190*778572d7SVignesh R 
spi_nor_set_4byte_opcodes(struct spi_nor * nor,const struct flash_info * info)191*778572d7SVignesh R static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
192*778572d7SVignesh R 				      const struct flash_info *info)
193*778572d7SVignesh R {
194*778572d7SVignesh R 	nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
195*778572d7SVignesh R }
196*778572d7SVignesh R 
197*778572d7SVignesh R /* Enable/disable 4-byte addressing mode. */
set_4byte(struct spi_nor * nor,const struct flash_info * info,int enable)198*778572d7SVignesh R static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
199*778572d7SVignesh R 			    int enable)
200*778572d7SVignesh R {
201*778572d7SVignesh R 	int status;
202*778572d7SVignesh R 	bool need_wren = false;
203*778572d7SVignesh R 	u8 cmd;
204*778572d7SVignesh R 
205*778572d7SVignesh R 	switch (JEDEC_MFR(info)) {
206*778572d7SVignesh R 	case SNOR_MFR_ST:
207*778572d7SVignesh R 	case SNOR_MFR_MICRON:
208*778572d7SVignesh R 		/* Some Micron need WREN command; all will accept it */
209*778572d7SVignesh R 		need_wren = true;
210*778572d7SVignesh R 	case SNOR_MFR_MACRONIX:
211*778572d7SVignesh R 	case SNOR_MFR_WINBOND:
212*778572d7SVignesh R 		if (need_wren)
213*778572d7SVignesh R 			write_enable(nor);
214*778572d7SVignesh R 
215*778572d7SVignesh R 		cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
216*778572d7SVignesh R 		status = spi_nor_write_reg(nor, cmd, NULL, 0);
217*778572d7SVignesh R 		if (need_wren)
218*778572d7SVignesh R 			write_disable(nor);
219*778572d7SVignesh R 
220*778572d7SVignesh R 		if (!status && !enable &&
221*778572d7SVignesh R 		    JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
222*778572d7SVignesh R 			/*
223*778572d7SVignesh R 			 * On Winbond W25Q256FV, leaving 4byte mode causes
224*778572d7SVignesh R 			 * the Extended Address Register to be set to 1, so all
225*778572d7SVignesh R 			 * 3-byte-address reads come from the second 16M.
226*778572d7SVignesh R 			 * We must clear the register to enable normal behavior.
227*778572d7SVignesh R 			 */
228*778572d7SVignesh R 			write_enable(nor);
229*778572d7SVignesh R 			nor->cmd_buf[0] = 0;
230*778572d7SVignesh R 			spi_nor_write_reg(nor, SPINOR_OP_WREAR,
231*778572d7SVignesh R 					  nor->cmd_buf, 1);
232*778572d7SVignesh R 			write_disable(nor);
233*778572d7SVignesh R 		}
234*778572d7SVignesh R 
235*778572d7SVignesh R 		return status;
236*778572d7SVignesh R 	default:
237*778572d7SVignesh R 		/* Spansion style */
238*778572d7SVignesh R 		nor->cmd_buf[0] = enable << 7;
239*778572d7SVignesh R 		return spi_nor_write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
240*778572d7SVignesh R 	}
241*778572d7SVignesh R }
242*778572d7SVignesh R 
243*778572d7SVignesh R #if defined(CONFIG_SPI_FLASH_SPANSION) ||	\
244*778572d7SVignesh R 	defined(CONFIG_SPI_FLASH_WINBOND) ||	\
245*778572d7SVignesh R 	defined(CONFIG_SPI_FLASH_MACRONIX)
246*778572d7SVignesh R /*
247*778572d7SVignesh R  * Read the status register, returning its value in the location
248*778572d7SVignesh R  * Return the status register value.
249*778572d7SVignesh R  * Returns negative if error occurred.
250*778572d7SVignesh R  */
read_sr(struct spi_nor * nor)251*778572d7SVignesh R static int read_sr(struct spi_nor *nor)
252*778572d7SVignesh R {
253*778572d7SVignesh R 	int ret;
254*778572d7SVignesh R 	u8 val;
255*778572d7SVignesh R 
256*778572d7SVignesh R 	ret = spi_nor_read_reg(nor, SPINOR_OP_RDSR, &val, 1);
257*778572d7SVignesh R 	if (ret < 0) {
258*778572d7SVignesh R 		pr_debug("error %d reading SR\n", (int)ret);
259*778572d7SVignesh R 		return ret;
260*778572d7SVignesh R 	}
261*778572d7SVignesh R 
262*778572d7SVignesh R 	return val;
263*778572d7SVignesh R }
264*778572d7SVignesh R 
265*778572d7SVignesh R /*
266*778572d7SVignesh R  * Read the flag status register, returning its value in the location
267*778572d7SVignesh R  * Return the status register value.
268*778572d7SVignesh R  * Returns negative if error occurred.
269*778572d7SVignesh R  */
read_fsr(struct spi_nor * nor)270*778572d7SVignesh R static int read_fsr(struct spi_nor *nor)
271*778572d7SVignesh R {
272*778572d7SVignesh R 	int ret;
273*778572d7SVignesh R 	u8 val;
274*778572d7SVignesh R 
275*778572d7SVignesh R 	ret = spi_nor_read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
276*778572d7SVignesh R 	if (ret < 0) {
277*778572d7SVignesh R 		pr_debug("error %d reading FSR\n", ret);
278*778572d7SVignesh R 		return ret;
279*778572d7SVignesh R 	}
280*778572d7SVignesh R 
281*778572d7SVignesh R 	return val;
282*778572d7SVignesh R }
283*778572d7SVignesh R 
spi_nor_sr_ready(struct spi_nor * nor)284*778572d7SVignesh R static int spi_nor_sr_ready(struct spi_nor *nor)
285*778572d7SVignesh R {
286*778572d7SVignesh R 	int sr = read_sr(nor);
287*778572d7SVignesh R 
288*778572d7SVignesh R 	if (sr < 0)
289*778572d7SVignesh R 		return sr;
290*778572d7SVignesh R 
291*778572d7SVignesh R 	return !(sr & SR_WIP);
292*778572d7SVignesh R }
293*778572d7SVignesh R 
spi_nor_fsr_ready(struct spi_nor * nor)294*778572d7SVignesh R static int spi_nor_fsr_ready(struct spi_nor *nor)
295*778572d7SVignesh R {
296*778572d7SVignesh R 	int fsr = read_fsr(nor);
297*778572d7SVignesh R 
298*778572d7SVignesh R 	if (fsr < 0)
299*778572d7SVignesh R 		return fsr;
300*778572d7SVignesh R 	return fsr & FSR_READY;
301*778572d7SVignesh R }
302*778572d7SVignesh R 
spi_nor_ready(struct spi_nor * nor)303*778572d7SVignesh R static int spi_nor_ready(struct spi_nor *nor)
304*778572d7SVignesh R {
305*778572d7SVignesh R 	int sr, fsr;
306*778572d7SVignesh R 
307*778572d7SVignesh R 	sr = spi_nor_sr_ready(nor);
308*778572d7SVignesh R 	if (sr < 0)
309*778572d7SVignesh R 		return sr;
310*778572d7SVignesh R 	fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
311*778572d7SVignesh R 	if (fsr < 0)
312*778572d7SVignesh R 		return fsr;
313*778572d7SVignesh R 	return sr && fsr;
314*778572d7SVignesh R }
315*778572d7SVignesh R 
316*778572d7SVignesh R /*
317*778572d7SVignesh R  * Service routine to read status register until ready, or timeout occurs.
318*778572d7SVignesh R  * Returns non-zero if error.
319*778572d7SVignesh R  */
spi_nor_wait_till_ready_with_timeout(struct spi_nor * nor,unsigned long timeout)320*778572d7SVignesh R static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
321*778572d7SVignesh R 						unsigned long timeout)
322*778572d7SVignesh R {
323*778572d7SVignesh R 	unsigned long timebase;
324*778572d7SVignesh R 	int ret;
325*778572d7SVignesh R 
326*778572d7SVignesh R 	timebase = get_timer(0);
327*778572d7SVignesh R 
328*778572d7SVignesh R 	while (get_timer(timebase) < timeout) {
329*778572d7SVignesh R 		ret = spi_nor_ready(nor);
330*778572d7SVignesh R 		if (ret < 0)
331*778572d7SVignesh R 			return ret;
332*778572d7SVignesh R 		if (ret)
333*778572d7SVignesh R 			return 0;
334*778572d7SVignesh R 	}
335*778572d7SVignesh R 
336*778572d7SVignesh R 	dev_err(nor->dev, "flash operation timed out\n");
337*778572d7SVignesh R 
338*778572d7SVignesh R 	return -ETIMEDOUT;
339*778572d7SVignesh R }
340*778572d7SVignesh R 
spi_nor_wait_till_ready(struct spi_nor * nor)341*778572d7SVignesh R static int spi_nor_wait_till_ready(struct spi_nor *nor)
342*778572d7SVignesh R {
343*778572d7SVignesh R 	return spi_nor_wait_till_ready_with_timeout(nor,
344*778572d7SVignesh R 						    DEFAULT_READY_WAIT_JIFFIES);
345*778572d7SVignesh R }
346*778572d7SVignesh R #endif /* CONFIG_SPI_FLASH_SPANSION */
347*778572d7SVignesh R 
348*778572d7SVignesh R /*
349*778572d7SVignesh R  * Erase an address range on the nor chip.  The address range may extend
350*778572d7SVignesh R  * one or more erase sectors.  Return an error is there is a problem erasing.
351*778572d7SVignesh R  */
spi_nor_erase(struct mtd_info * mtd,struct erase_info * instr)352*778572d7SVignesh R static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
353*778572d7SVignesh R {
354*778572d7SVignesh R 	return -ENOTSUPP;
355*778572d7SVignesh R }
356*778572d7SVignesh R 
spi_nor_read_id(struct spi_nor * nor)357*778572d7SVignesh R static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
358*778572d7SVignesh R {
359*778572d7SVignesh R 	int			tmp;
360*778572d7SVignesh R 	u8			id[SPI_NOR_MAX_ID_LEN];
361*778572d7SVignesh R 	const struct flash_info	*info;
362*778572d7SVignesh R 
363*778572d7SVignesh R 	tmp = spi_nor_read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
364*778572d7SVignesh R 	if (tmp < 0) {
365*778572d7SVignesh R 		dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
366*778572d7SVignesh R 		return ERR_PTR(tmp);
367*778572d7SVignesh R 	}
368*778572d7SVignesh R 
369*778572d7SVignesh R 	info = spi_nor_ids;
370*778572d7SVignesh R 	for (; info->sector_size != 0; info++) {
371*778572d7SVignesh R 		if (info->id_len) {
372*778572d7SVignesh R 			if (!memcmp(info->id, id, info->id_len))
373*778572d7SVignesh R 				return info;
374*778572d7SVignesh R 		}
375*778572d7SVignesh R 	}
376*778572d7SVignesh R 	dev_dbg(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
377*778572d7SVignesh R 		id[0], id[1], id[2]);
378*778572d7SVignesh R 	return ERR_PTR(-ENODEV);
379*778572d7SVignesh R }
380*778572d7SVignesh R 
spi_nor_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)381*778572d7SVignesh R static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
382*778572d7SVignesh R 			size_t *retlen, u_char *buf)
383*778572d7SVignesh R {
384*778572d7SVignesh R 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
385*778572d7SVignesh R 	int ret;
386*778572d7SVignesh R 
387*778572d7SVignesh R 	dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
388*778572d7SVignesh R 
389*778572d7SVignesh R 	while (len) {
390*778572d7SVignesh R 		loff_t addr = from;
391*778572d7SVignesh R 
392*778572d7SVignesh R 		ret = spi_nor_read_data(nor, addr, len, buf);
393*778572d7SVignesh R 		if (ret == 0) {
394*778572d7SVignesh R 			/* We shouldn't see 0-length reads */
395*778572d7SVignesh R 			ret = -EIO;
396*778572d7SVignesh R 			goto read_err;
397*778572d7SVignesh R 		}
398*778572d7SVignesh R 		if (ret < 0)
399*778572d7SVignesh R 			goto read_err;
400*778572d7SVignesh R 
401*778572d7SVignesh R 		*retlen += ret;
402*778572d7SVignesh R 		buf += ret;
403*778572d7SVignesh R 		from += ret;
404*778572d7SVignesh R 		len -= ret;
405*778572d7SVignesh R 	}
406*778572d7SVignesh R 	ret = 0;
407*778572d7SVignesh R 
408*778572d7SVignesh R read_err:
409*778572d7SVignesh R 	return ret;
410*778572d7SVignesh R }
411*778572d7SVignesh R 
412*778572d7SVignesh R /*
413*778572d7SVignesh R  * Write an address range to the nor chip.  Data must be written in
414*778572d7SVignesh R  * FLASH_PAGESIZE chunks.  The address range may be any size provided
415*778572d7SVignesh R  * it is within the physical boundaries.
416*778572d7SVignesh R  */
spi_nor_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)417*778572d7SVignesh R static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
418*778572d7SVignesh R 			 size_t *retlen, const u_char *buf)
419*778572d7SVignesh R {
420*778572d7SVignesh R 	return -ENOTSUPP;
421*778572d7SVignesh R }
422*778572d7SVignesh R 
423*778572d7SVignesh R #ifdef CONFIG_SPI_FLASH_MACRONIX
424*778572d7SVignesh R /**
425*778572d7SVignesh R  * macronix_quad_enable() - set QE bit in Status Register.
426*778572d7SVignesh R  * @nor:	pointer to a 'struct spi_nor'
427*778572d7SVignesh R  *
428*778572d7SVignesh R  * Set the Quad Enable (QE) bit in the Status Register.
429*778572d7SVignesh R  *
430*778572d7SVignesh R  * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
431*778572d7SVignesh R  *
432*778572d7SVignesh R  * Return: 0 on success, -errno otherwise.
433*778572d7SVignesh R  */
macronix_quad_enable(struct spi_nor * nor)434*778572d7SVignesh R static int macronix_quad_enable(struct spi_nor *nor)
435*778572d7SVignesh R {
436*778572d7SVignesh R 	int ret, val;
437*778572d7SVignesh R 
438*778572d7SVignesh R 	val = read_sr(nor);
439*778572d7SVignesh R 	if (val < 0)
440*778572d7SVignesh R 		return val;
441*778572d7SVignesh R 	if (val & SR_QUAD_EN_MX)
442*778572d7SVignesh R 		return 0;
443*778572d7SVignesh R 
444*778572d7SVignesh R 	write_enable(nor);
445*778572d7SVignesh R 
446*778572d7SVignesh R 	write_sr(nor, val | SR_QUAD_EN_MX);
447*778572d7SVignesh R 
448*778572d7SVignesh R 	ret = spi_nor_wait_till_ready(nor);
449*778572d7SVignesh R 	if (ret)
450*778572d7SVignesh R 		return ret;
451*778572d7SVignesh R 
452*778572d7SVignesh R 	ret = read_sr(nor);
453*778572d7SVignesh R 	if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
454*778572d7SVignesh R 		dev_err(nor->dev, "Macronix Quad bit not set\n");
455*778572d7SVignesh R 		return -EINVAL;
456*778572d7SVignesh R 	}
457*778572d7SVignesh R 
458*778572d7SVignesh R 	return 0;
459*778572d7SVignesh R }
460*778572d7SVignesh R #endif
461*778572d7SVignesh R 
462*778572d7SVignesh R #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
463*778572d7SVignesh R /*
464*778572d7SVignesh R  * Write status Register and configuration register with 2 bytes
465*778572d7SVignesh R  * The first byte will be written to the status register, while the
466*778572d7SVignesh R  * second byte will be written to the configuration register.
467*778572d7SVignesh R  * Return negative if error occurred.
468*778572d7SVignesh R  */
write_sr_cr(struct spi_nor * nor,u8 * sr_cr)469*778572d7SVignesh R static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
470*778572d7SVignesh R {
471*778572d7SVignesh R 	int ret;
472*778572d7SVignesh R 
473*778572d7SVignesh R 	write_enable(nor);
474*778572d7SVignesh R 
475*778572d7SVignesh R 	ret = spi_nor_write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
476*778572d7SVignesh R 	if (ret < 0) {
477*778572d7SVignesh R 		dev_dbg(nor->dev,
478*778572d7SVignesh R 			"error while writing configuration register\n");
479*778572d7SVignesh R 		return -EINVAL;
480*778572d7SVignesh R 	}
481*778572d7SVignesh R 
482*778572d7SVignesh R 	ret = spi_nor_wait_till_ready(nor);
483*778572d7SVignesh R 	if (ret) {
484*778572d7SVignesh R 		dev_dbg(nor->dev,
485*778572d7SVignesh R 			"timeout while writing configuration register\n");
486*778572d7SVignesh R 		return ret;
487*778572d7SVignesh R 	}
488*778572d7SVignesh R 
489*778572d7SVignesh R 	return 0;
490*778572d7SVignesh R }
491*778572d7SVignesh R 
492*778572d7SVignesh R /**
493*778572d7SVignesh R  * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
494*778572d7SVignesh R  * @nor:	pointer to a 'struct spi_nor'
495*778572d7SVignesh R  *
496*778572d7SVignesh R  * Set the Quad Enable (QE) bit in the Configuration Register.
497*778572d7SVignesh R  * This function should be used with QSPI memories supporting the Read
498*778572d7SVignesh R  * Configuration Register (35h) instruction.
499*778572d7SVignesh R  *
500*778572d7SVignesh R  * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
501*778572d7SVignesh R  * memories.
502*778572d7SVignesh R  *
503*778572d7SVignesh R  * Return: 0 on success, -errno otherwise.
504*778572d7SVignesh R  */
spansion_read_cr_quad_enable(struct spi_nor * nor)505*778572d7SVignesh R static int spansion_read_cr_quad_enable(struct spi_nor *nor)
506*778572d7SVignesh R {
507*778572d7SVignesh R 	u8 sr_cr[2];
508*778572d7SVignesh R 	int ret;
509*778572d7SVignesh R 
510*778572d7SVignesh R 	/* Check current Quad Enable bit value. */
511*778572d7SVignesh R 	ret = read_cr(nor);
512*778572d7SVignesh R 	if (ret < 0) {
513*778572d7SVignesh R 		dev_dbg(dev, "error while reading configuration register\n");
514*778572d7SVignesh R 		return -EINVAL;
515*778572d7SVignesh R 	}
516*778572d7SVignesh R 
517*778572d7SVignesh R 	if (ret & CR_QUAD_EN_SPAN)
518*778572d7SVignesh R 		return 0;
519*778572d7SVignesh R 
520*778572d7SVignesh R 	sr_cr[1] = ret | CR_QUAD_EN_SPAN;
521*778572d7SVignesh R 
522*778572d7SVignesh R 	/* Keep the current value of the Status Register. */
523*778572d7SVignesh R 	ret = read_sr(nor);
524*778572d7SVignesh R 	if (ret < 0) {
525*778572d7SVignesh R 		dev_dbg(dev, "error while reading status register\n");
526*778572d7SVignesh R 		return -EINVAL;
527*778572d7SVignesh R 	}
528*778572d7SVignesh R 	sr_cr[0] = ret;
529*778572d7SVignesh R 
530*778572d7SVignesh R 	ret = write_sr_cr(nor, sr_cr);
531*778572d7SVignesh R 	if (ret)
532*778572d7SVignesh R 		return ret;
533*778572d7SVignesh R 
534*778572d7SVignesh R 	/* Read back and check it. */
535*778572d7SVignesh R 	ret = read_cr(nor);
536*778572d7SVignesh R 	if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
537*778572d7SVignesh R 		dev_dbg(nor->dev, "Spansion Quad bit not set\n");
538*778572d7SVignesh R 		return -EINVAL;
539*778572d7SVignesh R 	}
540*778572d7SVignesh R 
541*778572d7SVignesh R 	return 0;
542*778572d7SVignesh R }
543*778572d7SVignesh R #endif /* CONFIG_SPI_FLASH_SPANSION */
544*778572d7SVignesh R 
545*778572d7SVignesh R struct spi_nor_read_command {
546*778572d7SVignesh R 	u8			num_mode_clocks;
547*778572d7SVignesh R 	u8			num_wait_states;
548*778572d7SVignesh R 	u8			opcode;
549*778572d7SVignesh R 	enum spi_nor_protocol	proto;
550*778572d7SVignesh R };
551*778572d7SVignesh R 
552*778572d7SVignesh R enum spi_nor_read_command_index {
553*778572d7SVignesh R 	SNOR_CMD_READ,
554*778572d7SVignesh R 	SNOR_CMD_READ_FAST,
555*778572d7SVignesh R 
556*778572d7SVignesh R 	/* Quad SPI */
557*778572d7SVignesh R 	SNOR_CMD_READ_1_1_4,
558*778572d7SVignesh R 
559*778572d7SVignesh R 	SNOR_CMD_READ_MAX
560*778572d7SVignesh R };
561*778572d7SVignesh R 
562*778572d7SVignesh R struct spi_nor_flash_parameter {
563*778572d7SVignesh R 	struct spi_nor_hwcaps		hwcaps;
564*778572d7SVignesh R 	struct spi_nor_read_command	reads[SNOR_CMD_READ_MAX];
565*778572d7SVignesh R };
566*778572d7SVignesh R 
567*778572d7SVignesh R 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)568*778572d7SVignesh R spi_nor_set_read_settings(struct spi_nor_read_command *read,
569*778572d7SVignesh R 			  u8 num_mode_clocks,
570*778572d7SVignesh R 			  u8 num_wait_states,
571*778572d7SVignesh R 			  u8 opcode,
572*778572d7SVignesh R 			  enum spi_nor_protocol proto)
573*778572d7SVignesh R {
574*778572d7SVignesh R 	read->num_mode_clocks = num_mode_clocks;
575*778572d7SVignesh R 	read->num_wait_states = num_wait_states;
576*778572d7SVignesh R 	read->opcode = opcode;
577*778572d7SVignesh R 	read->proto = proto;
578*778572d7SVignesh R }
579*778572d7SVignesh R 
spi_nor_init_params(struct spi_nor * nor,const struct flash_info * info,struct spi_nor_flash_parameter * params)580*778572d7SVignesh R static int spi_nor_init_params(struct spi_nor *nor,
581*778572d7SVignesh R 			       const struct flash_info *info,
582*778572d7SVignesh R 			       struct spi_nor_flash_parameter *params)
583*778572d7SVignesh R {
584*778572d7SVignesh R 	/* (Fast) Read settings. */
585*778572d7SVignesh R 	params->hwcaps.mask = SNOR_HWCAPS_READ;
586*778572d7SVignesh R 	spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
587*778572d7SVignesh R 				  0, 0, SPINOR_OP_READ,
588*778572d7SVignesh R 				  SNOR_PROTO_1_1_1);
589*778572d7SVignesh R 
590*778572d7SVignesh R 	if (!(info->flags & SPI_NOR_NO_FR)) {
591*778572d7SVignesh R 		params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
592*778572d7SVignesh R 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
593*778572d7SVignesh R 					  0, 8, SPINOR_OP_READ_FAST,
594*778572d7SVignesh R 					  SNOR_PROTO_1_1_1);
595*778572d7SVignesh R 	}
596*778572d7SVignesh R 
597*778572d7SVignesh R 	if (info->flags & SPI_NOR_QUAD_READ) {
598*778572d7SVignesh R 		params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
599*778572d7SVignesh R 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
600*778572d7SVignesh R 					  0, 8, SPINOR_OP_READ_1_1_4,
601*778572d7SVignesh R 					  SNOR_PROTO_1_1_4);
602*778572d7SVignesh R 	}
603*778572d7SVignesh R 
604*778572d7SVignesh R 	return 0;
605*778572d7SVignesh R }
606*778572d7SVignesh R 
spi_nor_select_read(struct spi_nor * nor,const struct spi_nor_flash_parameter * params,u32 shared_hwcaps)607*778572d7SVignesh R static int spi_nor_select_read(struct spi_nor *nor,
608*778572d7SVignesh R 			       const struct spi_nor_flash_parameter *params,
609*778572d7SVignesh R 			       u32 shared_hwcaps)
610*778572d7SVignesh R {
611*778572d7SVignesh R 	int best_match = shared_hwcaps & SNOR_HWCAPS_READ_MASK;
612*778572d7SVignesh R 	int cmd;
613*778572d7SVignesh R 	const struct spi_nor_read_command *read;
614*778572d7SVignesh R 
615*778572d7SVignesh R 	if (best_match < 0)
616*778572d7SVignesh R 		return -EINVAL;
617*778572d7SVignesh R 
618*778572d7SVignesh R 	if (best_match & SNOR_HWCAPS_READ_1_1_4)
619*778572d7SVignesh R 		cmd = SNOR_CMD_READ_1_1_4;
620*778572d7SVignesh R 	else if (best_match & SNOR_HWCAPS_READ_FAST)
621*778572d7SVignesh R 		cmd = SNOR_CMD_READ_FAST;
622*778572d7SVignesh R 	else
623*778572d7SVignesh R 		cmd = SNOR_CMD_READ;
624*778572d7SVignesh R 
625*778572d7SVignesh R 	read = &params->reads[cmd];
626*778572d7SVignesh R 	nor->read_opcode = read->opcode;
627*778572d7SVignesh R 	nor->read_proto = read->proto;
628*778572d7SVignesh R 
629*778572d7SVignesh R 	/*
630*778572d7SVignesh R 	 * In the spi-nor framework, we don't need to make the difference
631*778572d7SVignesh R 	 * between mode clock cycles and wait state clock cycles.
632*778572d7SVignesh R 	 * Indeed, the value of the mode clock cycles is used by a QSPI
633*778572d7SVignesh R 	 * flash memory to know whether it should enter or leave its 0-4-4
634*778572d7SVignesh R 	 * (Continuous Read / XIP) mode.
635*778572d7SVignesh R 	 * eXecution In Place is out of the scope of the mtd sub-system.
636*778572d7SVignesh R 	 * Hence we choose to merge both mode and wait state clock cycles
637*778572d7SVignesh R 	 * into the so called dummy clock cycles.
638*778572d7SVignesh R 	 */
639*778572d7SVignesh R 	nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
640*778572d7SVignesh R 	return 0;
641*778572d7SVignesh R }
642*778572d7SVignesh R 
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)643*778572d7SVignesh R static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
644*778572d7SVignesh R 			 const struct spi_nor_flash_parameter *params,
645*778572d7SVignesh R 			 const struct spi_nor_hwcaps *hwcaps)
646*778572d7SVignesh R {
647*778572d7SVignesh R 	u32 shared_mask;
648*778572d7SVignesh R 	int err;
649*778572d7SVignesh R 
650*778572d7SVignesh R 	/*
651*778572d7SVignesh R 	 * Keep only the hardware capabilities supported by both the SPI
652*778572d7SVignesh R 	 * controller and the SPI flash memory.
653*778572d7SVignesh R 	 */
654*778572d7SVignesh R 	shared_mask = hwcaps->mask & params->hwcaps.mask;
655*778572d7SVignesh R 
656*778572d7SVignesh R 	/* Select the (Fast) Read command. */
657*778572d7SVignesh R 	err = spi_nor_select_read(nor, params, shared_mask);
658*778572d7SVignesh R 	if (err) {
659*778572d7SVignesh R 		dev_dbg(nor->dev,
660*778572d7SVignesh R 			"can't select read settings supported by both the SPI controller and memory.\n");
661*778572d7SVignesh R 		return err;
662*778572d7SVignesh R 	}
663*778572d7SVignesh R 
664*778572d7SVignesh R 	/* Enable Quad I/O if needed. */
665*778572d7SVignesh R 	if (spi_nor_get_protocol_width(nor->read_proto) == 4) {
666*778572d7SVignesh R 		switch (JEDEC_MFR(info)) {
667*778572d7SVignesh R #ifdef CONFIG_SPI_FLASH_MACRONIX
668*778572d7SVignesh R 		case SNOR_MFR_MACRONIX:
669*778572d7SVignesh R 			err = macronix_quad_enable(nor);
670*778572d7SVignesh R 			break;
671*778572d7SVignesh R #endif
672*778572d7SVignesh R 		case SNOR_MFR_ST:
673*778572d7SVignesh R 		case SNOR_MFR_MICRON:
674*778572d7SVignesh R 			break;
675*778572d7SVignesh R 
676*778572d7SVignesh R 		default:
677*778572d7SVignesh R #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
678*778572d7SVignesh R 			/* Kept only for backward compatibility purpose. */
679*778572d7SVignesh R 			err = spansion_read_cr_quad_enable(nor);
680*778572d7SVignesh R #endif
681*778572d7SVignesh R 			break;
682*778572d7SVignesh R 		}
683*778572d7SVignesh R 	}
684*778572d7SVignesh R 	if (err) {
685*778572d7SVignesh R 		dev_dbg(nor->dev, "quad mode not supported\n");
686*778572d7SVignesh R 		return err;
687*778572d7SVignesh R 	}
688*778572d7SVignesh R 
689*778572d7SVignesh R 	return 0;
690*778572d7SVignesh R }
691*778572d7SVignesh R 
spi_nor_init(struct spi_nor * nor)692*778572d7SVignesh R static int spi_nor_init(struct spi_nor *nor)
693*778572d7SVignesh R {
694*778572d7SVignesh R 	if (nor->addr_width == 4 &&
695*778572d7SVignesh R 	    (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
696*778572d7SVignesh R 	    !(nor->info->flags & SPI_NOR_4B_OPCODES)) {
697*778572d7SVignesh R 		/*
698*778572d7SVignesh R 		 * If the RESET# pin isn't hooked up properly, or the system
699*778572d7SVignesh R 		 * otherwise doesn't perform a reset command in the boot
700*778572d7SVignesh R 		 * sequence, it's impossible to 100% protect against unexpected
701*778572d7SVignesh R 		 * reboots (e.g., crashes). Warn the user (or hopefully, system
702*778572d7SVignesh R 		 * designer) that this is bad.
703*778572d7SVignesh R 		 */
704*778572d7SVignesh R 		if (nor->flags & SNOR_F_BROKEN_RESET)
705*778572d7SVignesh R 			printf("enabling reset hack; may not recover from unexpected reboots\n");
706*778572d7SVignesh R 		set_4byte(nor, nor->info, 1);
707*778572d7SVignesh R 	}
708*778572d7SVignesh R 
709*778572d7SVignesh R 	return 0;
710*778572d7SVignesh R }
711*778572d7SVignesh R 
spi_nor_scan(struct spi_nor * nor)712*778572d7SVignesh R int spi_nor_scan(struct spi_nor *nor)
713*778572d7SVignesh R {
714*778572d7SVignesh R 	struct spi_nor_flash_parameter params;
715*778572d7SVignesh R 	const struct flash_info *info = NULL;
716*778572d7SVignesh R 	struct mtd_info *mtd = &nor->mtd;
717*778572d7SVignesh R 	struct spi_nor_hwcaps hwcaps = {
718*778572d7SVignesh R 		.mask = SNOR_HWCAPS_READ |
719*778572d7SVignesh R 			SNOR_HWCAPS_READ_FAST
720*778572d7SVignesh R 	};
721*778572d7SVignesh R 	struct spi_slave *spi = nor->spi;
722*778572d7SVignesh R 	int ret;
723*778572d7SVignesh R 
724*778572d7SVignesh R 	/* Reset SPI protocol for all commands. */
725*778572d7SVignesh R 	nor->reg_proto = SNOR_PROTO_1_1_1;
726*778572d7SVignesh R 	nor->read_proto = SNOR_PROTO_1_1_1;
727*778572d7SVignesh R 	nor->write_proto = SNOR_PROTO_1_1_1;
728*778572d7SVignesh R 
729*778572d7SVignesh R 	if (spi->mode & SPI_RX_QUAD)
730*778572d7SVignesh R 		hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
731*778572d7SVignesh R 
732*778572d7SVignesh R 	info = spi_nor_read_id(nor);
733*778572d7SVignesh R 	if (IS_ERR_OR_NULL(info))
734*778572d7SVignesh R 		return -ENOENT;
735*778572d7SVignesh R 	/* Parse the Serial Flash Discoverable Parameters table. */
736*778572d7SVignesh R 	ret = spi_nor_init_params(nor, info, &params);
737*778572d7SVignesh R 	if (ret)
738*778572d7SVignesh R 		return ret;
739*778572d7SVignesh R 
740*778572d7SVignesh R 	mtd->name = "spi-flash";
741*778572d7SVignesh R 	mtd->priv = nor;
742*778572d7SVignesh R 	mtd->type = MTD_NORFLASH;
743*778572d7SVignesh R 	mtd->writesize = 1;
744*778572d7SVignesh R 	mtd->flags = MTD_CAP_NORFLASH;
745*778572d7SVignesh R 	mtd->size = info->sector_size * info->n_sectors;
746*778572d7SVignesh R 	mtd->_erase = spi_nor_erase;
747*778572d7SVignesh R 	mtd->_read = spi_nor_read;
748*778572d7SVignesh R 	mtd->_write = spi_nor_write;
749*778572d7SVignesh R 
750*778572d7SVignesh R 	nor->size = mtd->size;
751*778572d7SVignesh R 
752*778572d7SVignesh R 	if (info->flags & USE_FSR)
753*778572d7SVignesh R 		nor->flags |= SNOR_F_USE_FSR;
754*778572d7SVignesh R 	if (info->flags & USE_CLSR)
755*778572d7SVignesh R 		nor->flags |= SNOR_F_USE_CLSR;
756*778572d7SVignesh R 
757*778572d7SVignesh R 	if (info->flags & SPI_NOR_NO_FR)
758*778572d7SVignesh R 		params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
759*778572d7SVignesh R 
760*778572d7SVignesh R 	/*
761*778572d7SVignesh R 	 * Configure the SPI memory:
762*778572d7SVignesh R 	 * - select op codes for (Fast) Read, Page Program and Sector Erase.
763*778572d7SVignesh R 	 * - set the number of dummy cycles (mode cycles + wait states).
764*778572d7SVignesh R 	 * - set the SPI protocols for register and memory accesses.
765*778572d7SVignesh R 	 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
766*778572d7SVignesh R 	 */
767*778572d7SVignesh R 	ret = spi_nor_setup(nor, info, &params, &hwcaps);
768*778572d7SVignesh R 	if (ret)
769*778572d7SVignesh R 		return ret;
770*778572d7SVignesh R 
771*778572d7SVignesh R 	if (nor->addr_width) {
772*778572d7SVignesh R 		/* already configured from SFDP */
773*778572d7SVignesh R 	} else if (info->addr_width) {
774*778572d7SVignesh R 		nor->addr_width = info->addr_width;
775*778572d7SVignesh R 	} else if (mtd->size > 0x1000000) {
776*778572d7SVignesh R 		/* enable 4-byte addressing if the device exceeds 16MiB */
777*778572d7SVignesh R 		nor->addr_width = 4;
778*778572d7SVignesh R 		if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
779*778572d7SVignesh R 		    info->flags & SPI_NOR_4B_OPCODES)
780*778572d7SVignesh R 			spi_nor_set_4byte_opcodes(nor, info);
781*778572d7SVignesh R 	} else {
782*778572d7SVignesh R 		nor->addr_width = 3;
783*778572d7SVignesh R 	}
784*778572d7SVignesh R 
785*778572d7SVignesh R 	if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
786*778572d7SVignesh R 		dev_dbg(dev, "address width is too large: %u\n",
787*778572d7SVignesh R 			nor->addr_width);
788*778572d7SVignesh R 		return -EINVAL;
789*778572d7SVignesh R 	}
790*778572d7SVignesh R 
791*778572d7SVignesh R 	/* Send all the required SPI flash commands to initialize device */
792*778572d7SVignesh R 	nor->info = info;
793*778572d7SVignesh R 	ret = spi_nor_init(nor);
794*778572d7SVignesh R 	if (ret)
795*778572d7SVignesh R 		return ret;
796*778572d7SVignesh R 
797*778572d7SVignesh R 	return 0;
798*778572d7SVignesh R }
799*778572d7SVignesh R 
800*778572d7SVignesh R /* U-Boot specific functions, need to extend MTD to support these */
spi_flash_cmd_get_sw_write_prot(struct spi_nor * nor)801*778572d7SVignesh R int spi_flash_cmd_get_sw_write_prot(struct spi_nor *nor)
802*778572d7SVignesh R {
803*778572d7SVignesh R 	return -ENOTSUPP;
804*778572d7SVignesh R }
805