xref: /openbmc/u-boot/drivers/mtd/spi/spi-nor-core.c (revision 591e1cf0)
17aeedac0SVignesh R // SPDX-License-Identifier: GPL-2.0
27aeedac0SVignesh R /*
37aeedac0SVignesh R  * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with
47aeedac0SVignesh R  * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
57aeedac0SVignesh R  *
67aeedac0SVignesh R  * Copyright (C) 2005, Intec Automation Inc.
77aeedac0SVignesh R  * Copyright (C) 2014, Freescale Semiconductor, Inc.
87aeedac0SVignesh R  *
97aeedac0SVignesh R  * Synced from Linux v4.19
107aeedac0SVignesh R  */
117aeedac0SVignesh R 
127aeedac0SVignesh R #include <common.h>
137aeedac0SVignesh R #include <linux/err.h>
147aeedac0SVignesh R #include <linux/errno.h>
157aeedac0SVignesh R #include <linux/log2.h>
167aeedac0SVignesh R #include <linux/math64.h>
177aeedac0SVignesh R #include <linux/sizes.h>
187aeedac0SVignesh R 
197aeedac0SVignesh R #include <linux/mtd/mtd.h>
207aeedac0SVignesh R #include <linux/mtd/spi-nor.h>
217aeedac0SVignesh R #include <spi-mem.h>
227aeedac0SVignesh R #include <spi.h>
237aeedac0SVignesh R 
24c4e88623SVignesh R #include "sf_internal.h"
25c4e88623SVignesh R 
267aeedac0SVignesh R /* Define max times to check status register before we give up. */
277aeedac0SVignesh R 
287aeedac0SVignesh R /*
297aeedac0SVignesh R  * For everything but full-chip erase; probably could be much smaller, but kept
307aeedac0SVignesh R  * around for safety for now
317aeedac0SVignesh R  */
327aeedac0SVignesh R 
337aeedac0SVignesh R #define HZ					CONFIG_SYS_HZ
347aeedac0SVignesh R 
357aeedac0SVignesh R #define DEFAULT_READY_WAIT_JIFFIES		(40UL * HZ)
367aeedac0SVignesh R 
37eaad4c09SChin-Ting Kuo /**
38eaad4c09SChin-Ting Kuo  * spi_nor_setup_op() - Set up common properties of a spi-mem op.
39eaad4c09SChin-Ting Kuo  * @nor:		pointer to a 'struct spi_nor'
40eaad4c09SChin-Ting Kuo  * @op:			pointer to the 'struct spi_mem_op' whose properties
41eaad4c09SChin-Ting Kuo  *			need to be initialized.
42eaad4c09SChin-Ting Kuo  * @proto:		the protocol from which the properties need to be set.
43eaad4c09SChin-Ting Kuo  */
spi_nor_setup_op(const struct spi_nor * nor,struct spi_mem_op * op,const enum spi_nor_protocol proto)44eaad4c09SChin-Ting Kuo void spi_nor_setup_op(const struct spi_nor *nor,
45eaad4c09SChin-Ting Kuo 		      struct spi_mem_op *op,
46eaad4c09SChin-Ting Kuo 		      const enum spi_nor_protocol proto)
47eaad4c09SChin-Ting Kuo {
48eaad4c09SChin-Ting Kuo 	op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(proto);
49eaad4c09SChin-Ting Kuo 
50eaad4c09SChin-Ting Kuo 	if (op->addr.nbytes)
51eaad4c09SChin-Ting Kuo 		op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto);
52eaad4c09SChin-Ting Kuo 
53eaad4c09SChin-Ting Kuo 	if (op->dummy.nbytes)
54eaad4c09SChin-Ting Kuo 		op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto);
55eaad4c09SChin-Ting Kuo 
56eaad4c09SChin-Ting Kuo 	if (op->data.nbytes)
57eaad4c09SChin-Ting Kuo 		op->data.buswidth = spi_nor_get_protocol_data_nbits(proto);
58eaad4c09SChin-Ting Kuo }
59eaad4c09SChin-Ting Kuo 
spi_nor_read_write_reg(struct spi_nor * nor,struct spi_mem_op * op,void * buf)60492e65b2SVignesh R static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op
61492e65b2SVignesh R 		*op, void *buf)
62492e65b2SVignesh R {
63492e65b2SVignesh R 	if (op->data.dir == SPI_MEM_DATA_IN)
64492e65b2SVignesh R 		op->data.buf.in = buf;
65492e65b2SVignesh R 	else
66492e65b2SVignesh R 		op->data.buf.out = buf;
67492e65b2SVignesh R 	return spi_mem_exec_op(nor->spi, op);
68492e65b2SVignesh R }
69492e65b2SVignesh R 
spi_nor_read_reg(struct spi_nor * nor,u8 code,u8 * val,int len)707aeedac0SVignesh R static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
717aeedac0SVignesh R {
72492e65b2SVignesh R 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1),
73492e65b2SVignesh R 					  SPI_MEM_OP_NO_ADDR,
74492e65b2SVignesh R 					  SPI_MEM_OP_NO_DUMMY,
75492e65b2SVignesh R 					  SPI_MEM_OP_DATA_IN(len, NULL, 1));
76492e65b2SVignesh R 	int ret;
77492e65b2SVignesh R 
78492e65b2SVignesh R 	ret = spi_nor_read_write_reg(nor, &op, val);
79492e65b2SVignesh R 	if (ret < 0)
80492e65b2SVignesh R 		dev_dbg(&flash->spimem->spi->dev, "error %d reading %x\n", ret,
81492e65b2SVignesh R 			code);
82492e65b2SVignesh R 
83492e65b2SVignesh R 	return ret;
847aeedac0SVignesh R }
857aeedac0SVignesh R 
spi_nor_write_reg(struct spi_nor * nor,u8 opcode,u8 * buf,int len)867aeedac0SVignesh R static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
877aeedac0SVignesh R {
88492e65b2SVignesh R 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1),
89492e65b2SVignesh R 					  SPI_MEM_OP_NO_ADDR,
90492e65b2SVignesh R 					  SPI_MEM_OP_NO_DUMMY,
91492e65b2SVignesh R 					  SPI_MEM_OP_DATA_OUT(len, NULL, 1));
92492e65b2SVignesh R 
93492e65b2SVignesh R 	return spi_nor_read_write_reg(nor, &op, buf);
947aeedac0SVignesh R }
957aeedac0SVignesh R 
96eaad4c09SChin-Ting Kuo #ifdef CONFIG_SPI_FLASH_SPANSION
spansion_read_any_reg(struct spi_nor * nor,u32 addr,u8 dummy,u8 * val)97eaad4c09SChin-Ting Kuo static int spansion_read_any_reg(struct spi_nor *nor, u32 addr, u8 dummy,
98eaad4c09SChin-Ting Kuo 				 u8 *val)
99eaad4c09SChin-Ting Kuo {
100eaad4c09SChin-Ting Kuo 	int ret;
101eaad4c09SChin-Ting Kuo 	u8 dummy_ori;
102eaad4c09SChin-Ting Kuo 	struct spi_mem_op op =
103eaad4c09SChin-Ting Kuo 			SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDAR, 1),
104eaad4c09SChin-Ting Kuo 				   SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
105eaad4c09SChin-Ting Kuo 				   SPI_MEM_OP_DUMMY(dummy / 8, 1),
106eaad4c09SChin-Ting Kuo 				   SPI_MEM_OP_DATA_IN(1, NULL, 1));
107eaad4c09SChin-Ting Kuo 
108eaad4c09SChin-Ting Kuo 	dummy_ori = nor->read_dummy;
109eaad4c09SChin-Ting Kuo 	nor->read_dummy = dummy;
110eaad4c09SChin-Ting Kuo 
111eaad4c09SChin-Ting Kuo 	ret = spi_nor_read_write_reg(nor, &op, val);
112eaad4c09SChin-Ting Kuo 
113eaad4c09SChin-Ting Kuo 	nor->read_dummy = dummy_ori;
114eaad4c09SChin-Ting Kuo 
115eaad4c09SChin-Ting Kuo 	return ret;
116eaad4c09SChin-Ting Kuo }
117eaad4c09SChin-Ting Kuo 
spansion_write_any_reg(struct spi_nor * nor,u32 addr,u8 val)118eaad4c09SChin-Ting Kuo static int spansion_write_any_reg(struct spi_nor *nor, u32 addr, u8 val)
119eaad4c09SChin-Ting Kuo {
120eaad4c09SChin-Ting Kuo 	struct spi_mem_op op =
121eaad4c09SChin-Ting Kuo 			SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRAR, 1),
122eaad4c09SChin-Ting Kuo 				   SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
123eaad4c09SChin-Ting Kuo 				   SPI_MEM_OP_NO_DUMMY,
124eaad4c09SChin-Ting Kuo 				   SPI_MEM_OP_DATA_OUT(1, NULL, 1));
125eaad4c09SChin-Ting Kuo 
126eaad4c09SChin-Ting Kuo 	return spi_nor_read_write_reg(nor, &op, &val);
127eaad4c09SChin-Ting Kuo }
128eaad4c09SChin-Ting Kuo #endif
129eaad4c09SChin-Ting Kuo 
spi_nor_read_data(struct spi_nor * nor,loff_t from,size_t len,u_char * buf)1307aeedac0SVignesh R static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
1317aeedac0SVignesh R 				 u_char *buf)
1327aeedac0SVignesh R {
133492e65b2SVignesh R 	struct spi_mem_op op =
134492e65b2SVignesh R 			SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
135492e65b2SVignesh R 				   SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
136492e65b2SVignesh R 				   SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
137492e65b2SVignesh R 				   SPI_MEM_OP_DATA_IN(len, buf, 1));
138492e65b2SVignesh R 	size_t remaining = len;
139492e65b2SVignesh R 	int ret;
140492e65b2SVignesh R 
141492e65b2SVignesh R 	/* get transfer protocols. */
142492e65b2SVignesh R 	op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
143492e65b2SVignesh R 	op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
144492e65b2SVignesh R 	op.dummy.buswidth = op.addr.buswidth;
145492e65b2SVignesh R 	op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
146492e65b2SVignesh R 
147492e65b2SVignesh R 	/* convert the dummy cycles to the number of bytes */
148492e65b2SVignesh R 	op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
149492e65b2SVignesh R 
150492e65b2SVignesh R 	while (remaining) {
151492e65b2SVignesh R 		op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
152492e65b2SVignesh R 		ret = spi_mem_adjust_op_size(nor->spi, &op);
153492e65b2SVignesh R 		if (ret)
154492e65b2SVignesh R 			return ret;
155492e65b2SVignesh R 
156492e65b2SVignesh R 		ret = spi_mem_exec_op(nor->spi, &op);
157492e65b2SVignesh R 		if (ret)
158492e65b2SVignesh R 			return ret;
159492e65b2SVignesh R 
160492e65b2SVignesh R 		op.addr.val += op.data.nbytes;
161492e65b2SVignesh R 		remaining -= op.data.nbytes;
162492e65b2SVignesh R 		op.data.buf.in += op.data.nbytes;
163492e65b2SVignesh R 	}
164492e65b2SVignesh R 
165492e65b2SVignesh R 	return len;
1667aeedac0SVignesh R }
1677aeedac0SVignesh R 
spi_nor_write_data(struct spi_nor * nor,loff_t to,size_t len,const u_char * buf)1687aeedac0SVignesh R static ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
1697aeedac0SVignesh R 				  const u_char *buf)
1707aeedac0SVignesh R {
171492e65b2SVignesh R 	struct spi_mem_op op =
172492e65b2SVignesh R 			SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1),
173492e65b2SVignesh R 				   SPI_MEM_OP_ADDR(nor->addr_width, to, 1),
174492e65b2SVignesh R 				   SPI_MEM_OP_NO_DUMMY,
175492e65b2SVignesh R 				   SPI_MEM_OP_DATA_OUT(len, buf, 1));
176492e65b2SVignesh R 	size_t remaining = len;
177492e65b2SVignesh R 	int ret;
178492e65b2SVignesh R 
179492e65b2SVignesh R 	/* get transfer protocols. */
180492e65b2SVignesh R 	op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto);
181492e65b2SVignesh R 	op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto);
182492e65b2SVignesh R 	op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
183492e65b2SVignesh R 
184492e65b2SVignesh R 	if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
185492e65b2SVignesh R 		op.addr.nbytes = 0;
186492e65b2SVignesh R 
187492e65b2SVignesh R 	while (remaining) {
188492e65b2SVignesh R 		op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
189492e65b2SVignesh R 		ret = spi_mem_adjust_op_size(nor->spi, &op);
190492e65b2SVignesh R 		if (ret)
191492e65b2SVignesh R 			return ret;
192492e65b2SVignesh R 
193492e65b2SVignesh R 		ret = spi_mem_exec_op(nor->spi, &op);
194492e65b2SVignesh R 		if (ret)
195492e65b2SVignesh R 			return ret;
196492e65b2SVignesh R 
197492e65b2SVignesh R 		op.addr.val += op.data.nbytes;
198492e65b2SVignesh R 		remaining -= op.data.nbytes;
199492e65b2SVignesh R 		op.data.buf.out += op.data.nbytes;
200492e65b2SVignesh R 	}
201492e65b2SVignesh R 
202492e65b2SVignesh R 	return len;
2037aeedac0SVignesh R }
2047aeedac0SVignesh R 
2057aeedac0SVignesh R /*
2067aeedac0SVignesh R  * Read the status register, returning its value in the location
2077aeedac0SVignesh R  * Return the status register value.
2087aeedac0SVignesh R  * Returns negative if error occurred.
2097aeedac0SVignesh R  */
read_sr(struct spi_nor * nor)2107aeedac0SVignesh R static int read_sr(struct spi_nor *nor)
2117aeedac0SVignesh R {
2127aeedac0SVignesh R 	int ret;
2137aeedac0SVignesh R 	u8 val;
2147aeedac0SVignesh R 
2157aeedac0SVignesh R 	ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
2167aeedac0SVignesh R 	if (ret < 0) {
2177aeedac0SVignesh R 		pr_debug("error %d reading SR\n", (int)ret);
2187aeedac0SVignesh R 		return ret;
2197aeedac0SVignesh R 	}
2207aeedac0SVignesh R 
2217aeedac0SVignesh R 	return val;
2227aeedac0SVignesh R }
2237aeedac0SVignesh R 
read_winbond_sr2(struct spi_nor * nor)224878b2ba4SChin-Ting Kuo static int read_winbond_sr2(struct spi_nor *nor)
225878b2ba4SChin-Ting Kuo {
226878b2ba4SChin-Ting Kuo 	int ret;
227878b2ba4SChin-Ting Kuo 	u8 val;
228878b2ba4SChin-Ting Kuo 
229878b2ba4SChin-Ting Kuo 	ret = nor->read_reg(nor, SPINOR_OP_WINBOND_RDSR2, &val, 1);
230878b2ba4SChin-Ting Kuo 	if (ret < 0) {
231878b2ba4SChin-Ting Kuo 		pr_debug("error %d reading SR2\n", (int)ret);
232878b2ba4SChin-Ting Kuo 		return ret;
233878b2ba4SChin-Ting Kuo 	}
234878b2ba4SChin-Ting Kuo 
235878b2ba4SChin-Ting Kuo 	return val;
236878b2ba4SChin-Ting Kuo }
237878b2ba4SChin-Ting Kuo 
2387aeedac0SVignesh R /*
2397aeedac0SVignesh R  * Read the flag status register, returning its value in the location
2407aeedac0SVignesh R  * Return the status register value.
2417aeedac0SVignesh R  * Returns negative if error occurred.
2427aeedac0SVignesh R  */
read_fsr(struct spi_nor * nor)2437aeedac0SVignesh R static int read_fsr(struct spi_nor *nor)
2447aeedac0SVignesh R {
2457aeedac0SVignesh R 	int ret;
2467aeedac0SVignesh R 	u8 val;
2477aeedac0SVignesh R 
2487aeedac0SVignesh R 	ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
2497aeedac0SVignesh R 	if (ret < 0) {
2507aeedac0SVignesh R 		pr_debug("error %d reading FSR\n", ret);
2517aeedac0SVignesh R 		return ret;
2527aeedac0SVignesh R 	}
2537aeedac0SVignesh R 
2547aeedac0SVignesh R 	return val;
2557aeedac0SVignesh R }
2567aeedac0SVignesh R 
2577aeedac0SVignesh R /*
2587aeedac0SVignesh R  * Read configuration register, returning its value in the
2597aeedac0SVignesh R  * location. Return the configuration register value.
2607aeedac0SVignesh R  * Returns negative if error occurred.
2617aeedac0SVignesh R  */
2627aeedac0SVignesh R #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
read_cr(struct spi_nor * nor)2637aeedac0SVignesh R static int read_cr(struct spi_nor *nor)
2647aeedac0SVignesh R {
2657aeedac0SVignesh R 	int ret;
2667aeedac0SVignesh R 	u8 val;
2677aeedac0SVignesh R 
2687aeedac0SVignesh R 	ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
2697aeedac0SVignesh R 	if (ret < 0) {
2707aeedac0SVignesh R 		dev_dbg(nor->dev, "error %d reading CR\n", ret);
2717aeedac0SVignesh R 		return ret;
2727aeedac0SVignesh R 	}
2737aeedac0SVignesh R 
2747aeedac0SVignesh R 	return val;
2757aeedac0SVignesh R }
2767aeedac0SVignesh R #endif
2777aeedac0SVignesh R 
2787aeedac0SVignesh R /*
2797aeedac0SVignesh R  * Write status register 1 byte
2807aeedac0SVignesh R  * Returns negative if error occurred.
2817aeedac0SVignesh R  */
write_sr(struct spi_nor * nor,u8 val)2827aeedac0SVignesh R static int write_sr(struct spi_nor *nor, u8 val)
2837aeedac0SVignesh R {
2847aeedac0SVignesh R 	nor->cmd_buf[0] = val;
2857aeedac0SVignesh R 	return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
2867aeedac0SVignesh R }
2877aeedac0SVignesh R 
write_winbond_sr2(struct spi_nor * nor,u8 val)288878b2ba4SChin-Ting Kuo static int write_winbond_sr2(struct spi_nor *nor, u8 val)
289878b2ba4SChin-Ting Kuo {
290878b2ba4SChin-Ting Kuo 	nor->cmd_buf[0] = val;
291878b2ba4SChin-Ting Kuo 	return nor->write_reg(nor, SPINOR_OP_WINBOND_WRSR2, nor->cmd_buf, 1);
292878b2ba4SChin-Ting Kuo }
293878b2ba4SChin-Ting Kuo 
2947aeedac0SVignesh R /*
2957aeedac0SVignesh R  * Set write enable latch with Write Enable command.
2967aeedac0SVignesh R  * Returns negative if error occurred.
2977aeedac0SVignesh R  */
write_enable(struct spi_nor * nor)2987aeedac0SVignesh R static int write_enable(struct spi_nor *nor)
2997aeedac0SVignesh R {
3007aeedac0SVignesh R 	return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
3017aeedac0SVignesh R }
3027aeedac0SVignesh R 
3037aeedac0SVignesh R /*
3047aeedac0SVignesh R  * Send write disable instruction to the chip.
3057aeedac0SVignesh R  */
write_disable(struct spi_nor * nor)3067aeedac0SVignesh R static int write_disable(struct spi_nor *nor)
3077aeedac0SVignesh R {
3087aeedac0SVignesh R 	return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
3097aeedac0SVignesh R }
3107aeedac0SVignesh R 
mtd_to_spi_nor(struct mtd_info * mtd)3117aeedac0SVignesh R static struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
3127aeedac0SVignesh R {
3137aeedac0SVignesh R 	return mtd->priv;
3147aeedac0SVignesh R }
3157aeedac0SVignesh R 
3168c927809SVignesh R #ifndef CONFIG_SPI_FLASH_BAR
spi_nor_convert_opcode(u8 opcode,const u8 table[][2],size_t size)31761059bc5SVignesh R static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
31861059bc5SVignesh R {
31961059bc5SVignesh R 	size_t i;
32061059bc5SVignesh R 
32161059bc5SVignesh R 	for (i = 0; i < size; i++)
32261059bc5SVignesh R 		if (table[i][0] == opcode)
32361059bc5SVignesh R 			return table[i][1];
32461059bc5SVignesh R 
32561059bc5SVignesh R 	/* No conversion found, keep input op code. */
32661059bc5SVignesh R 	return opcode;
32761059bc5SVignesh R }
32861059bc5SVignesh R 
spi_nor_convert_3to4_read(u8 opcode)32961059bc5SVignesh R static u8 spi_nor_convert_3to4_read(u8 opcode)
33061059bc5SVignesh R {
33161059bc5SVignesh R 	static const u8 spi_nor_3to4_read[][2] = {
33261059bc5SVignesh R 		{ SPINOR_OP_READ,	SPINOR_OP_READ_4B },
33361059bc5SVignesh R 		{ SPINOR_OP_READ_FAST,	SPINOR_OP_READ_FAST_4B },
33461059bc5SVignesh R 		{ SPINOR_OP_READ_1_1_2,	SPINOR_OP_READ_1_1_2_4B },
33561059bc5SVignesh R 		{ SPINOR_OP_READ_1_2_2,	SPINOR_OP_READ_1_2_2_4B },
33661059bc5SVignesh R 		{ SPINOR_OP_READ_1_1_4,	SPINOR_OP_READ_1_1_4_4B },
33761059bc5SVignesh R 		{ SPINOR_OP_READ_1_4_4,	SPINOR_OP_READ_1_4_4_4B },
33861059bc5SVignesh R 
33961059bc5SVignesh R 		{ SPINOR_OP_READ_1_1_1_DTR,	SPINOR_OP_READ_1_1_1_DTR_4B },
34061059bc5SVignesh R 		{ SPINOR_OP_READ_1_2_2_DTR,	SPINOR_OP_READ_1_2_2_DTR_4B },
34161059bc5SVignesh R 		{ SPINOR_OP_READ_1_4_4_DTR,	SPINOR_OP_READ_1_4_4_DTR_4B },
34261059bc5SVignesh R 	};
34361059bc5SVignesh R 
34461059bc5SVignesh R 	return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
34561059bc5SVignesh R 				      ARRAY_SIZE(spi_nor_3to4_read));
34661059bc5SVignesh R }
34761059bc5SVignesh R 
spi_nor_convert_3to4_program(u8 opcode)34861059bc5SVignesh R static u8 spi_nor_convert_3to4_program(u8 opcode)
34961059bc5SVignesh R {
35061059bc5SVignesh R 	static const u8 spi_nor_3to4_program[][2] = {
35161059bc5SVignesh R 		{ SPINOR_OP_PP,		SPINOR_OP_PP_4B },
35261059bc5SVignesh R 		{ SPINOR_OP_PP_1_1_4,	SPINOR_OP_PP_1_1_4_4B },
35361059bc5SVignesh R 		{ SPINOR_OP_PP_1_4_4,	SPINOR_OP_PP_1_4_4_4B },
35461059bc5SVignesh R 	};
35561059bc5SVignesh R 
35661059bc5SVignesh R 	return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
35761059bc5SVignesh R 				      ARRAY_SIZE(spi_nor_3to4_program));
35861059bc5SVignesh R }
35961059bc5SVignesh R 
spi_nor_convert_3to4_erase(u8 opcode)36061059bc5SVignesh R static u8 spi_nor_convert_3to4_erase(u8 opcode)
36161059bc5SVignesh R {
36261059bc5SVignesh R 	static const u8 spi_nor_3to4_erase[][2] = {
36361059bc5SVignesh R 		{ SPINOR_OP_BE_4K,	SPINOR_OP_BE_4K_4B },
36461059bc5SVignesh R 		{ SPINOR_OP_BE_32K,	SPINOR_OP_BE_32K_4B },
36561059bc5SVignesh R 		{ SPINOR_OP_SE,		SPINOR_OP_SE_4B },
36661059bc5SVignesh R 	};
36761059bc5SVignesh R 
36861059bc5SVignesh R 	return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
36961059bc5SVignesh R 				      ARRAY_SIZE(spi_nor_3to4_erase));
37061059bc5SVignesh R }
37161059bc5SVignesh R 
spi_nor_set_4byte_opcodes(struct spi_nor * nor,const struct flash_info * info)37261059bc5SVignesh R static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
37361059bc5SVignesh R 				      const struct flash_info *info)
37461059bc5SVignesh R {
37561059bc5SVignesh R 	/* Do some manufacturer fixups first */
37661059bc5SVignesh R 	switch (JEDEC_MFR(info)) {
37761059bc5SVignesh R 	case SNOR_MFR_SPANSION:
378cd800046SChin-Ting Kuo 	case SNOR_MFR_CYPRESS:
37961059bc5SVignesh R 		/* No small sector erase for 4-byte command set */
38061059bc5SVignesh R 		nor->erase_opcode = SPINOR_OP_SE;
38161059bc5SVignesh R 		nor->mtd.erasesize = info->sector_size;
38261059bc5SVignesh R 		break;
38361059bc5SVignesh R 
38461059bc5SVignesh R 	default:
38561059bc5SVignesh R 		break;
38661059bc5SVignesh R 	}
38761059bc5SVignesh R 
38861059bc5SVignesh R 	nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
38961059bc5SVignesh R 	nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
39061059bc5SVignesh R 	nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
39161059bc5SVignesh R }
3928c927809SVignesh R #endif /* !CONFIG_SPI_FLASH_BAR */
39361059bc5SVignesh R 
39461059bc5SVignesh R /* Enable/disable 4-byte addressing mode. */
set_4byte(struct spi_nor * nor,const struct flash_info * info,int enable)39561059bc5SVignesh R static int set_4byte(struct spi_nor *nor, const struct flash_info *info,
39661059bc5SVignesh R 		     int enable)
39761059bc5SVignesh R {
39861059bc5SVignesh R 	int status;
39961059bc5SVignesh R 	bool need_wren = false;
40061059bc5SVignesh R 	u8 cmd;
40161059bc5SVignesh R 
40261059bc5SVignesh R 	switch (JEDEC_MFR(info)) {
40361059bc5SVignesh R 	case SNOR_MFR_ST:
40461059bc5SVignesh R 	case SNOR_MFR_MICRON:
40561059bc5SVignesh R 		/* Some Micron need WREN command; all will accept it */
40661059bc5SVignesh R 		need_wren = true;
40761059bc5SVignesh R 	case SNOR_MFR_MACRONIX:
40861059bc5SVignesh R 	case SNOR_MFR_WINBOND:
40997989e54SChin-Ting Kuo 	case SNOR_MFR_GIGADEVICE:
41000554b9bSChin-Ting Kuo 	case SNOR_MFR_ISSI:
411cd800046SChin-Ting Kuo 	case SNOR_MFR_CYPRESS:
41261059bc5SVignesh R 		if (need_wren)
41361059bc5SVignesh R 			write_enable(nor);
41461059bc5SVignesh R 
41561059bc5SVignesh R 		cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
41661059bc5SVignesh R 		status = nor->write_reg(nor, cmd, NULL, 0);
41761059bc5SVignesh R 		if (need_wren)
41861059bc5SVignesh R 			write_disable(nor);
41961059bc5SVignesh R 
42061059bc5SVignesh R 		if (!status && !enable &&
42161059bc5SVignesh R 		    JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
42261059bc5SVignesh R 			/*
42361059bc5SVignesh R 			 * On Winbond W25Q256FV, leaving 4byte mode causes
42461059bc5SVignesh R 			 * the Extended Address Register to be set to 1, so all
42561059bc5SVignesh R 			 * 3-byte-address reads come from the second 16M.
42661059bc5SVignesh R 			 * We must clear the register to enable normal behavior.
42761059bc5SVignesh R 			 */
42861059bc5SVignesh R 			write_enable(nor);
42961059bc5SVignesh R 			nor->cmd_buf[0] = 0;
43061059bc5SVignesh R 			nor->write_reg(nor, SPINOR_OP_WREAR, nor->cmd_buf, 1);
43161059bc5SVignesh R 			write_disable(nor);
43261059bc5SVignesh R 		}
43361059bc5SVignesh R 
43461059bc5SVignesh R 		return status;
43561059bc5SVignesh R 	default:
43661059bc5SVignesh R 		/* Spansion style */
43761059bc5SVignesh R 		nor->cmd_buf[0] = enable << 7;
43861059bc5SVignesh R 		return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
43961059bc5SVignesh R 	}
44061059bc5SVignesh R }
44161059bc5SVignesh R 
spi_nor_sr_ready(struct spi_nor * nor)4427aeedac0SVignesh R static int spi_nor_sr_ready(struct spi_nor *nor)
4437aeedac0SVignesh R {
4447aeedac0SVignesh R 	int sr = read_sr(nor);
4457aeedac0SVignesh R 
4467aeedac0SVignesh R 	if (sr < 0)
4477aeedac0SVignesh R 		return sr;
4487aeedac0SVignesh R 
4497aeedac0SVignesh R 	if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) {
4507aeedac0SVignesh R 		if (sr & SR_E_ERR)
4517aeedac0SVignesh R 			dev_dbg(nor->dev, "Erase Error occurred\n");
4527aeedac0SVignesh R 		else
4537aeedac0SVignesh R 			dev_dbg(nor->dev, "Programming Error occurred\n");
4547aeedac0SVignesh R 
4557aeedac0SVignesh R 		nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
4567aeedac0SVignesh R 		return -EIO;
4577aeedac0SVignesh R 	}
4587aeedac0SVignesh R 
4597aeedac0SVignesh R 	return !(sr & SR_WIP);
4607aeedac0SVignesh R }
4617aeedac0SVignesh R 
spi_nor_fsr_ready(struct spi_nor * nor)4627aeedac0SVignesh R static int spi_nor_fsr_ready(struct spi_nor *nor)
4637aeedac0SVignesh R {
4647aeedac0SVignesh R 	int fsr = read_fsr(nor);
4657aeedac0SVignesh R 
4667aeedac0SVignesh R 	if (fsr < 0)
4677aeedac0SVignesh R 		return fsr;
4687aeedac0SVignesh R 
4697aeedac0SVignesh R 	if (fsr & (FSR_E_ERR | FSR_P_ERR)) {
4707aeedac0SVignesh R 		if (fsr & FSR_E_ERR)
4717aeedac0SVignesh R 			dev_dbg(nor->dev, "Erase operation failed.\n");
4727aeedac0SVignesh R 		else
4737aeedac0SVignesh R 			dev_dbg(nor->dev, "Program operation failed.\n");
4747aeedac0SVignesh R 
4757aeedac0SVignesh R 		if (fsr & FSR_PT_ERR)
4767aeedac0SVignesh R 			dev_dbg(nor->dev,
4777aeedac0SVignesh R 				"Attempted to modify a protected sector.\n");
4787aeedac0SVignesh R 
4797aeedac0SVignesh R 		nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0);
4807aeedac0SVignesh R 		return -EIO;
4817aeedac0SVignesh R 	}
4827aeedac0SVignesh R 
4837aeedac0SVignesh R 	return fsr & FSR_READY;
4847aeedac0SVignesh R }
4857aeedac0SVignesh R 
spi_nor_ready(struct spi_nor * nor)4867aeedac0SVignesh R static int spi_nor_ready(struct spi_nor *nor)
4877aeedac0SVignesh R {
4887aeedac0SVignesh R 	int sr, fsr;
4897aeedac0SVignesh R 
4907aeedac0SVignesh R 	sr = spi_nor_sr_ready(nor);
4917aeedac0SVignesh R 	if (sr < 0)
4927aeedac0SVignesh R 		return sr;
4937aeedac0SVignesh R 	fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
4947aeedac0SVignesh R 	if (fsr < 0)
4957aeedac0SVignesh R 		return fsr;
4967aeedac0SVignesh R 	return sr && fsr;
4977aeedac0SVignesh R }
4987aeedac0SVignesh R 
4997aeedac0SVignesh R /*
5007aeedac0SVignesh R  * Service routine to read status register until ready, or timeout occurs.
5017aeedac0SVignesh R  * Returns non-zero if error.
5027aeedac0SVignesh R  */
spi_nor_wait_till_ready_with_timeout(struct spi_nor * nor,unsigned long timeout)5037aeedac0SVignesh R static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
5047aeedac0SVignesh R 						unsigned long timeout)
5057aeedac0SVignesh R {
5067aeedac0SVignesh R 	unsigned long timebase;
5077aeedac0SVignesh R 	int ret;
5087aeedac0SVignesh R 
5097aeedac0SVignesh R 	timebase = get_timer(0);
5107aeedac0SVignesh R 
5117aeedac0SVignesh R 	while (get_timer(timebase) < timeout) {
5127aeedac0SVignesh R 		ret = spi_nor_ready(nor);
5137aeedac0SVignesh R 		if (ret < 0)
5147aeedac0SVignesh R 			return ret;
5157aeedac0SVignesh R 		if (ret)
5167aeedac0SVignesh R 			return 0;
5177aeedac0SVignesh R 	}
5187aeedac0SVignesh R 
5197aeedac0SVignesh R 	dev_err(nor->dev, "flash operation timed out\n");
5207aeedac0SVignesh R 
5217aeedac0SVignesh R 	return -ETIMEDOUT;
5227aeedac0SVignesh R }
5237aeedac0SVignesh R 
spi_nor_wait_till_ready(struct spi_nor * nor)5247aeedac0SVignesh R static int spi_nor_wait_till_ready(struct spi_nor *nor)
5257aeedac0SVignesh R {
5267aeedac0SVignesh R 	return spi_nor_wait_till_ready_with_timeout(nor,
5277aeedac0SVignesh R 						    DEFAULT_READY_WAIT_JIFFIES);
5287aeedac0SVignesh R }
5297aeedac0SVignesh R 
5308c927809SVignesh R #ifdef CONFIG_SPI_FLASH_BAR
5318c927809SVignesh R /*
5328c927809SVignesh R  * This "clean_bar" is necessary in a situation when one was accessing
5338c927809SVignesh R  * spi flash memory > 16 MiB by using Bank Address Register's BA24 bit.
5348c927809SVignesh R  *
5358c927809SVignesh R  * After it the BA24 bit shall be cleared to allow access to correct
5368c927809SVignesh R  * memory region after SW reset (by calling "reset" command).
5378c927809SVignesh R  *
5388c927809SVignesh R  * Otherwise, the BA24 bit may be left set and then after reset, the
5398c927809SVignesh R  * ROM would read/write/erase SPL from 16 MiB * bank_sel address.
5408c927809SVignesh R  */
clean_bar(struct spi_nor * nor)5418c927809SVignesh R static int clean_bar(struct spi_nor *nor)
5428c927809SVignesh R {
5438c927809SVignesh R 	u8 cmd, bank_sel = 0;
5448c927809SVignesh R 
5458c927809SVignesh R 	if (nor->bank_curr == 0)
5468c927809SVignesh R 		return 0;
5478c927809SVignesh R 	cmd = nor->bank_write_cmd;
5488c927809SVignesh R 	nor->bank_curr = 0;
5498c927809SVignesh R 	write_enable(nor);
5508c927809SVignesh R 
5518c927809SVignesh R 	return nor->write_reg(nor, cmd, &bank_sel, 1);
5528c927809SVignesh R }
5538c927809SVignesh R 
write_bar(struct spi_nor * nor,u32 offset)5548c927809SVignesh R static int write_bar(struct spi_nor *nor, u32 offset)
5558c927809SVignesh R {
5568c927809SVignesh R 	u8 cmd, bank_sel;
5578c927809SVignesh R 	int ret;
5588c927809SVignesh R 
5598c927809SVignesh R 	bank_sel = offset / SZ_16M;
5608c927809SVignesh R 	if (bank_sel == nor->bank_curr)
5618c927809SVignesh R 		goto bar_end;
5628c927809SVignesh R 
5638c927809SVignesh R 	cmd = nor->bank_write_cmd;
5648c927809SVignesh R 	write_enable(nor);
5658c927809SVignesh R 	ret = nor->write_reg(nor, cmd, &bank_sel, 1);
5668c927809SVignesh R 	if (ret < 0) {
5678c927809SVignesh R 		debug("SF: fail to write bank register\n");
5688c927809SVignesh R 		return ret;
5698c927809SVignesh R 	}
5708c927809SVignesh R 
5718c927809SVignesh R bar_end:
5728c927809SVignesh R 	nor->bank_curr = bank_sel;
5738c927809SVignesh R 	return nor->bank_curr;
5748c927809SVignesh R }
5758c927809SVignesh R 
read_bar(struct spi_nor * nor,const struct flash_info * info)5768c927809SVignesh R static int read_bar(struct spi_nor *nor, const struct flash_info *info)
5778c927809SVignesh R {
5788c927809SVignesh R 	u8 curr_bank = 0;
5798c927809SVignesh R 	int ret;
5808c927809SVignesh R 
5818c927809SVignesh R 	switch (JEDEC_MFR(info)) {
5828c927809SVignesh R 	case SNOR_MFR_SPANSION:
5838c927809SVignesh R 		nor->bank_read_cmd = SPINOR_OP_BRRD;
5848c927809SVignesh R 		nor->bank_write_cmd = SPINOR_OP_BRWR;
5858c927809SVignesh R 		break;
5868c927809SVignesh R 	default:
5878c927809SVignesh R 		nor->bank_read_cmd = SPINOR_OP_RDEAR;
5888c927809SVignesh R 		nor->bank_write_cmd = SPINOR_OP_WREAR;
5898c927809SVignesh R 	}
5908c927809SVignesh R 
5918c927809SVignesh R 	ret = nor->read_reg(nor, nor->bank_read_cmd,
5928c927809SVignesh R 				    &curr_bank, 1);
5938c927809SVignesh R 	if (ret) {
5948c927809SVignesh R 		debug("SF: fail to read bank addr register\n");
5958c927809SVignesh R 		return ret;
5968c927809SVignesh R 	}
5978c927809SVignesh R 	nor->bank_curr = curr_bank;
5988c927809SVignesh R 
5998c927809SVignesh R 	return 0;
6008c927809SVignesh R }
6018c927809SVignesh R #endif
6028c927809SVignesh R 
6037aeedac0SVignesh R /*
6047aeedac0SVignesh R  * Initiate the erasure of a single sector
6057aeedac0SVignesh R  */
spi_nor_erase_sector(struct spi_nor * nor,u32 addr)6067aeedac0SVignesh R static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
6077aeedac0SVignesh R {
6087aeedac0SVignesh R 	u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
6097aeedac0SVignesh R 	int i;
610eaad4c09SChin-Ting Kuo 	int ret = 0;
6117aeedac0SVignesh R 
6127aeedac0SVignesh R 	if (nor->erase)
6137aeedac0SVignesh R 		return nor->erase(nor, addr);
6147aeedac0SVignesh R 
6157aeedac0SVignesh R 	/*
6167aeedac0SVignesh R 	 * Default implementation, if driver doesn't have a specialized HW
6177aeedac0SVignesh R 	 * control
6187aeedac0SVignesh R 	 */
6197aeedac0SVignesh R 	for (i = nor->addr_width - 1; i >= 0; i--) {
6207aeedac0SVignesh R 		buf[i] = addr & 0xff;
6217aeedac0SVignesh R 		addr >>= 8;
6227aeedac0SVignesh R 	}
6237aeedac0SVignesh R 
624eaad4c09SChin-Ting Kuo 	ret = nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
625eaad4c09SChin-Ting Kuo 	if (ret)
626eaad4c09SChin-Ting Kuo 		return ret;
627eaad4c09SChin-Ting Kuo 
628eaad4c09SChin-Ting Kuo 	return nor->mtd.erasesize;
6297aeedac0SVignesh R }
6307aeedac0SVignesh R 
6317aeedac0SVignesh R /*
6327aeedac0SVignesh R  * Erase an address range on the nor chip.  The address range may extend
6337aeedac0SVignesh R  * one or more erase sectors.  Return an error is there is a problem erasing.
6347aeedac0SVignesh R  */
spi_nor_erase(struct mtd_info * mtd,struct erase_info * instr)6357aeedac0SVignesh R static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
6367aeedac0SVignesh R {
6377aeedac0SVignesh R 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
6387aeedac0SVignesh R 	u32 addr, len, rem;
6397aeedac0SVignesh R 	int ret;
6407aeedac0SVignesh R 
6417aeedac0SVignesh R 	dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
6427aeedac0SVignesh R 		(long long)instr->len);
6437aeedac0SVignesh R 
6447aeedac0SVignesh R 	div_u64_rem(instr->len, mtd->erasesize, &rem);
6457aeedac0SVignesh R 	if (rem)
6467aeedac0SVignesh R 		return -EINVAL;
6477aeedac0SVignesh R 
6487aeedac0SVignesh R 	addr = instr->addr;
6497aeedac0SVignesh R 	len = instr->len;
6507aeedac0SVignesh R 
6517aeedac0SVignesh R 	while (len) {
6528c927809SVignesh R #ifdef CONFIG_SPI_FLASH_BAR
6538c927809SVignesh R 		ret = write_bar(nor, addr);
6548c927809SVignesh R 		if (ret < 0)
6558c927809SVignesh R 			return ret;
6568c927809SVignesh R #endif
6577aeedac0SVignesh R 		write_enable(nor);
6587aeedac0SVignesh R 
6597aeedac0SVignesh R 		ret = spi_nor_erase_sector(nor, addr);
660eaad4c09SChin-Ting Kuo 		if (ret < 0)
6617aeedac0SVignesh R 			goto erase_err;
6627aeedac0SVignesh R 
663eaad4c09SChin-Ting Kuo 		addr += ret;
664eaad4c09SChin-Ting Kuo 		len -= ret;
6657aeedac0SVignesh R 
6667aeedac0SVignesh R 		ret = spi_nor_wait_till_ready(nor);
6677aeedac0SVignesh R 		if (ret)
6687aeedac0SVignesh R 			goto erase_err;
6697aeedac0SVignesh R 	}
6707aeedac0SVignesh R 
6718c927809SVignesh R erase_err:
6728c927809SVignesh R #ifdef CONFIG_SPI_FLASH_BAR
6738c927809SVignesh R 	ret = clean_bar(nor);
6748c927809SVignesh R #endif
6757aeedac0SVignesh R 	write_disable(nor);
6767aeedac0SVignesh R 
6777aeedac0SVignesh R 	return ret;
6787aeedac0SVignesh R }
6797aeedac0SVignesh R 
680eaad4c09SChin-Ting Kuo #ifdef CONFIG_SPI_FLASH_SPANSION
681eaad4c09SChin-Ting Kuo /**
682eaad4c09SChin-Ting Kuo  * spansion_erase_non_uniform() - erase non-uniform sectors for Spansion/Cypress
683eaad4c09SChin-Ting Kuo  *                                chips
684eaad4c09SChin-Ting Kuo  * @nor:	pointer to a 'struct spi_nor'
685eaad4c09SChin-Ting Kuo  * @addr:	address of the sector to erase
686eaad4c09SChin-Ting Kuo  * @opcode_4k:	opcode for 4K sector erase
687eaad4c09SChin-Ting Kuo  * @ovlsz_top:	size of overlaid portion at the top address
688eaad4c09SChin-Ting Kuo  * @ovlsz_btm:	size of overlaid portion at the bottom address
689eaad4c09SChin-Ting Kuo  *
690eaad4c09SChin-Ting Kuo  * Erase an address range on the nor chip that can contain 4KB sectors overlaid
691eaad4c09SChin-Ting Kuo  * on top and/or bottom. The appropriate erase opcode and size are chosen by
692eaad4c09SChin-Ting Kuo  * address to erase and size of overlaid portion.
693eaad4c09SChin-Ting Kuo  *
694eaad4c09SChin-Ting Kuo  * Return: number of bytes erased on success, -errno otherwise.
695eaad4c09SChin-Ting Kuo  */
spansion_erase_non_uniform(struct spi_nor * nor,u32 addr,u8 opcode_4k,u32 ovlsz_top,u32 ovlsz_btm)696eaad4c09SChin-Ting Kuo static int spansion_erase_non_uniform(struct spi_nor *nor, u32 addr,
697eaad4c09SChin-Ting Kuo 				      u8 opcode_4k, u32 ovlsz_top,
698eaad4c09SChin-Ting Kuo 				      u32 ovlsz_btm)
699eaad4c09SChin-Ting Kuo {
700eaad4c09SChin-Ting Kuo 	struct spi_mem_op op =
701eaad4c09SChin-Ting Kuo 		SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 0),
702eaad4c09SChin-Ting Kuo 			   SPI_MEM_OP_ADDR(nor->addr_width, addr, 0),
703eaad4c09SChin-Ting Kuo 			   SPI_MEM_OP_NO_DUMMY,
704eaad4c09SChin-Ting Kuo 			   SPI_MEM_OP_NO_DATA);
705eaad4c09SChin-Ting Kuo 	struct mtd_info *mtd = &nor->mtd;
706eaad4c09SChin-Ting Kuo 	u32 erasesize;
707eaad4c09SChin-Ting Kuo 	int ret;
708eaad4c09SChin-Ting Kuo 
709eaad4c09SChin-Ting Kuo 	/* 4KB sectors */
710eaad4c09SChin-Ting Kuo 	if (op.addr.val < ovlsz_btm ||
711eaad4c09SChin-Ting Kuo 	    op.addr.val >= mtd->size - ovlsz_top) {
712eaad4c09SChin-Ting Kuo 		op.cmd.opcode = opcode_4k;
713eaad4c09SChin-Ting Kuo 		erasesize = SZ_4K;
714eaad4c09SChin-Ting Kuo 
715eaad4c09SChin-Ting Kuo 	/* Non-overlaid portion in the normal sector at the bottom */
716eaad4c09SChin-Ting Kuo 	} else if (op.addr.val == ovlsz_btm) {
717eaad4c09SChin-Ting Kuo 		op.cmd.opcode = nor->erase_opcode;
718eaad4c09SChin-Ting Kuo 		erasesize = mtd->erasesize - ovlsz_btm;
719eaad4c09SChin-Ting Kuo 
720eaad4c09SChin-Ting Kuo 	/* Non-overlaid portion in the normal sector at the top */
721eaad4c09SChin-Ting Kuo 	} else if (op.addr.val == mtd->size - mtd->erasesize) {
722eaad4c09SChin-Ting Kuo 		op.cmd.opcode = nor->erase_opcode;
723eaad4c09SChin-Ting Kuo 		erasesize = mtd->erasesize - ovlsz_top;
724eaad4c09SChin-Ting Kuo 
725eaad4c09SChin-Ting Kuo 	/* Normal sectors */
726eaad4c09SChin-Ting Kuo 	} else {
727eaad4c09SChin-Ting Kuo 		op.cmd.opcode = nor->erase_opcode;
728eaad4c09SChin-Ting Kuo 		erasesize = mtd->erasesize;
729eaad4c09SChin-Ting Kuo 	}
730eaad4c09SChin-Ting Kuo 
731eaad4c09SChin-Ting Kuo 	spi_nor_setup_op(nor, &op, nor->write_proto);
732eaad4c09SChin-Ting Kuo 
733eaad4c09SChin-Ting Kuo 	ret = spi_mem_exec_op(nor->spi, &op);
734eaad4c09SChin-Ting Kuo 	if (ret)
735eaad4c09SChin-Ting Kuo 		return ret;
736eaad4c09SChin-Ting Kuo 
737eaad4c09SChin-Ting Kuo 	return erasesize;
738eaad4c09SChin-Ting Kuo }
739eaad4c09SChin-Ting Kuo 
s25hx_t_erase_non_uniform(struct spi_nor * nor,loff_t addr)740eaad4c09SChin-Ting Kuo static int s25hx_t_erase_non_uniform(struct spi_nor *nor, loff_t addr)
741eaad4c09SChin-Ting Kuo {
742eaad4c09SChin-Ting Kuo 	/* Support 32 x 4KB sectors at bottom */
743eaad4c09SChin-Ting Kuo 	return spansion_erase_non_uniform(nor, addr, SPINOR_OP_BE_4K_4B, 0,
744eaad4c09SChin-Ting Kuo 					  SZ_128K);
745eaad4c09SChin-Ting Kuo }
746eaad4c09SChin-Ting Kuo 
s25hx_t_setup(struct spi_nor * nor,const struct flash_info * info)747eaad4c09SChin-Ting Kuo static int s25hx_t_setup(struct spi_nor *nor, const struct flash_info *info)
748eaad4c09SChin-Ting Kuo {
749eaad4c09SChin-Ting Kuo 	int ret;
750eaad4c09SChin-Ting Kuo 	u8 cfr3v;
751eaad4c09SChin-Ting Kuo 
752eaad4c09SChin-Ting Kuo #ifdef CONFIG_SPI_FLASH_BAR
753eaad4c09SChin-Ting Kuo 	return -ENOTSUPP; /* Bank Address Register is not supported */
754eaad4c09SChin-Ting Kuo #endif
755eaad4c09SChin-Ting Kuo 	/*
756eaad4c09SChin-Ting Kuo 	 * Read CFR3V to check if uniform sector is selected. If not, assign an
757eaad4c09SChin-Ting Kuo 	 * erase hook that supports non-uniform erase.
758eaad4c09SChin-Ting Kuo 	 */
759eaad4c09SChin-Ting Kuo 	ret = spansion_read_any_reg(nor, SPINOR_REG_ADDR_CFR3V, 0, &cfr3v);
760eaad4c09SChin-Ting Kuo 	if (ret)
761eaad4c09SChin-Ting Kuo 		return ret;
762eaad4c09SChin-Ting Kuo 
763eaad4c09SChin-Ting Kuo 	if (!(cfr3v & CFR3V_UNHYSA))
764eaad4c09SChin-Ting Kuo 		nor->erase = s25hx_t_erase_non_uniform;
765eaad4c09SChin-Ting Kuo 
766eaad4c09SChin-Ting Kuo 	return 0;
767eaad4c09SChin-Ting Kuo }
768eaad4c09SChin-Ting Kuo #endif
769eaad4c09SChin-Ting Kuo 
micron_read_nvcr(struct spi_nor * nor)770a25e89c3SChin-Ting Kuo static int micron_read_nvcr(struct spi_nor *nor)
771a25e89c3SChin-Ting Kuo {
772a25e89c3SChin-Ting Kuo 	int ret;
773a25e89c3SChin-Ting Kuo 	int val;
774a25e89c3SChin-Ting Kuo 
775a25e89c3SChin-Ting Kuo 	ret = nor->read_reg(nor, SPINOR_OP_MICRON_RDNVCR, (u8 *)&val, 2);
776a25e89c3SChin-Ting Kuo 	if (ret < 0) {
777a25e89c3SChin-Ting Kuo 		dev_err(nor->dev, "[Micron] error %d reading NVCR\n", ret);
778a25e89c3SChin-Ting Kuo 		return ret;
779a25e89c3SChin-Ting Kuo 	}
780a25e89c3SChin-Ting Kuo 
781a25e89c3SChin-Ting Kuo 	return val;
782a25e89c3SChin-Ting Kuo }
783a25e89c3SChin-Ting Kuo 
micron_write_nvcr(struct spi_nor * nor,int val)784a25e89c3SChin-Ting Kuo static int micron_write_nvcr(struct spi_nor *nor, int val)
785a25e89c3SChin-Ting Kuo {
786a25e89c3SChin-Ting Kuo 	int ret;
787a25e89c3SChin-Ting Kuo 
788a25e89c3SChin-Ting Kuo 	write_enable(nor);
789a25e89c3SChin-Ting Kuo 
790a25e89c3SChin-Ting Kuo 	nor->cmd_buf[0] = val & 0xff;
791a25e89c3SChin-Ting Kuo 	nor->cmd_buf[1] = (val >> 8) & 0xff;
792a25e89c3SChin-Ting Kuo 
793a25e89c3SChin-Ting Kuo 	ret = nor->write_reg(nor, SPINOR_OP_MICRON_WRNVCR, nor->cmd_buf, 2);
794a25e89c3SChin-Ting Kuo 	if (ret < 0) {
795a25e89c3SChin-Ting Kuo 		dev_err(nor->dev,
796a25e89c3SChin-Ting Kuo 			"[Micron] error while writing configuration register\n");
797a25e89c3SChin-Ting Kuo 		return -EINVAL;
798a25e89c3SChin-Ting Kuo 	}
799a25e89c3SChin-Ting Kuo 
800a25e89c3SChin-Ting Kuo 	ret = spi_nor_wait_till_ready(nor);
801a25e89c3SChin-Ting Kuo 	if (ret) {
802a25e89c3SChin-Ting Kuo 		dev_err(nor->dev,
803a25e89c3SChin-Ting Kuo 			"[Micron] timeout while writing configuration register\n");
804a25e89c3SChin-Ting Kuo 		return ret;
805a25e89c3SChin-Ting Kuo 	}
806a25e89c3SChin-Ting Kuo 
807a25e89c3SChin-Ting Kuo 	return 0;
808a25e89c3SChin-Ting Kuo }
809a25e89c3SChin-Ting Kuo 
micron_read_cr_quad_enable(struct spi_nor * nor)810a25e89c3SChin-Ting Kuo static int micron_read_cr_quad_enable(struct spi_nor *nor)
811a25e89c3SChin-Ting Kuo {
812a25e89c3SChin-Ting Kuo 	int ret;
813a25e89c3SChin-Ting Kuo 
814a25e89c3SChin-Ting Kuo 	/* Check current Quad Enable bit value. */
815a25e89c3SChin-Ting Kuo 	ret = micron_read_nvcr(nor);
816a25e89c3SChin-Ting Kuo 	if (ret < 0) {
817a25e89c3SChin-Ting Kuo 		dev_err(dev, "[Micron] error while reading nonvolatile configuration register\n");
818a25e89c3SChin-Ting Kuo 		return -EINVAL;
819a25e89c3SChin-Ting Kuo 	}
820a25e89c3SChin-Ting Kuo 
821a25e89c3SChin-Ting Kuo 	if ((ret & MICRON_RST_HOLD_CTRL) == 0)
822a25e89c3SChin-Ting Kuo 		return 0;
823a25e89c3SChin-Ting Kuo 
824a25e89c3SChin-Ting Kuo 	ret &= ~MICRON_RST_HOLD_CTRL;
825a25e89c3SChin-Ting Kuo 
826a25e89c3SChin-Ting Kuo 	/* Keep the current value of the Status Register. */
827a25e89c3SChin-Ting Kuo 	ret = micron_write_nvcr(nor, ret);
828a25e89c3SChin-Ting Kuo 	if (ret < 0) {
829a25e89c3SChin-Ting Kuo 		dev_err(dev, "[Micron] error while writing nonvolatile configuration register\n");
830a25e89c3SChin-Ting Kuo 		return -EINVAL;
831a25e89c3SChin-Ting Kuo 	}
832a25e89c3SChin-Ting Kuo 
833a25e89c3SChin-Ting Kuo 	ret = micron_read_nvcr(nor);
834a25e89c3SChin-Ting Kuo 	if (ret > 0 && (ret & MICRON_RST_HOLD_CTRL)) {
835a25e89c3SChin-Ting Kuo 		dev_err(nor->dev, "[Micron] Quad bit not set\n");
836a25e89c3SChin-Ting Kuo 		return -EINVAL;
837a25e89c3SChin-Ting Kuo 	}
838a25e89c3SChin-Ting Kuo 
839a25e89c3SChin-Ting Kuo 	return 0;
840a25e89c3SChin-Ting Kuo }
841a25e89c3SChin-Ting Kuo 
cypress_s25hx_t(const struct flash_info * info)842cd800046SChin-Ting Kuo static bool cypress_s25hx_t(const struct flash_info *info)
843cd800046SChin-Ting Kuo {
844cd800046SChin-Ting Kuo 	if (JEDEC_MFR(info) == SNOR_MFR_CYPRESS) {
845cd800046SChin-Ting Kuo 		switch (info->id[1]) {
846cd800046SChin-Ting Kuo 		case 0x2a: /* S25HL (QSPI, 3.3V) */
847cd800046SChin-Ting Kuo 		case 0x2b: /* S25HS (QSPI, 1.8V) */
848cd800046SChin-Ting Kuo 			return true;
849cd800046SChin-Ting Kuo 			break;
850cd800046SChin-Ting Kuo 
851cd800046SChin-Ting Kuo 		default:
852cd800046SChin-Ting Kuo 			break;
853cd800046SChin-Ting Kuo 		}
854cd800046SChin-Ting Kuo 	}
855cd800046SChin-Ting Kuo 
856cd800046SChin-Ting Kuo 	return false;
857cd800046SChin-Ting Kuo }
858cd800046SChin-Ting Kuo 
8597aeedac0SVignesh R #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
8607aeedac0SVignesh R /* Write status register and ensure bits in mask match written values */
write_sr_and_check(struct spi_nor * nor,u8 status_new,u8 mask)8617aeedac0SVignesh R static int write_sr_and_check(struct spi_nor *nor, u8 status_new, u8 mask)
8627aeedac0SVignesh R {
8637aeedac0SVignesh R 	int ret;
8647aeedac0SVignesh R 
8657aeedac0SVignesh R 	write_enable(nor);
8667aeedac0SVignesh R 	ret = write_sr(nor, status_new);
8677aeedac0SVignesh R 	if (ret)
8687aeedac0SVignesh R 		return ret;
8697aeedac0SVignesh R 
8707aeedac0SVignesh R 	ret = spi_nor_wait_till_ready(nor);
8717aeedac0SVignesh R 	if (ret)
8727aeedac0SVignesh R 		return ret;
8737aeedac0SVignesh R 
8747aeedac0SVignesh R 	ret = read_sr(nor);
8757aeedac0SVignesh R 	if (ret < 0)
8767aeedac0SVignesh R 		return ret;
8777aeedac0SVignesh R 
8787aeedac0SVignesh R 	return ((ret & mask) != (status_new & mask)) ? -EIO : 0;
8797aeedac0SVignesh R }
8807aeedac0SVignesh R 
stm_get_locked_range(struct spi_nor * nor,u8 sr,loff_t * ofs,uint64_t * len)8817aeedac0SVignesh R static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
8827aeedac0SVignesh R 				 uint64_t *len)
8837aeedac0SVignesh R {
8847aeedac0SVignesh R 	struct mtd_info *mtd = &nor->mtd;
8857aeedac0SVignesh R 	u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
8867aeedac0SVignesh R 	int shift = ffs(mask) - 1;
8877aeedac0SVignesh R 	int pow;
8887aeedac0SVignesh R 
8897aeedac0SVignesh R 	if (!(sr & mask)) {
8907aeedac0SVignesh R 		/* No protection */
8917aeedac0SVignesh R 		*ofs = 0;
8927aeedac0SVignesh R 		*len = 0;
8937aeedac0SVignesh R 	} else {
8947aeedac0SVignesh R 		pow = ((sr & mask) ^ mask) >> shift;
8957aeedac0SVignesh R 		*len = mtd->size >> pow;
8967aeedac0SVignesh R 		if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
8977aeedac0SVignesh R 			*ofs = 0;
8987aeedac0SVignesh R 		else
8997aeedac0SVignesh R 			*ofs = mtd->size - *len;
9007aeedac0SVignesh R 	}
9017aeedac0SVignesh R }
9027aeedac0SVignesh R 
9037aeedac0SVignesh R /*
9047aeedac0SVignesh R  * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
9057aeedac0SVignesh R  * @locked is false); 0 otherwise
9067aeedac0SVignesh R  */
stm_check_lock_status_sr(struct spi_nor * nor,loff_t ofs,u64 len,u8 sr,bool locked)9077aeedac0SVignesh R static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, u64 len,
9087aeedac0SVignesh R 				    u8 sr, bool locked)
9097aeedac0SVignesh R {
9107aeedac0SVignesh R 	loff_t lock_offs;
9117aeedac0SVignesh R 	uint64_t lock_len;
9127aeedac0SVignesh R 
9137aeedac0SVignesh R 	if (!len)
9147aeedac0SVignesh R 		return 1;
9157aeedac0SVignesh R 
9167aeedac0SVignesh R 	stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
9177aeedac0SVignesh R 
9187aeedac0SVignesh R 	if (locked)
9197aeedac0SVignesh R 		/* Requested range is a sub-range of locked range */
9207aeedac0SVignesh R 		return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
9217aeedac0SVignesh R 	else
9227aeedac0SVignesh R 		/* Requested range does not overlap with locked range */
9237aeedac0SVignesh R 		return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
9247aeedac0SVignesh R }
9257aeedac0SVignesh R 
stm_is_locked_sr(struct spi_nor * nor,loff_t ofs,uint64_t len,u8 sr)9267aeedac0SVignesh R static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
9277aeedac0SVignesh R 			    u8 sr)
9287aeedac0SVignesh R {
9297aeedac0SVignesh R 	return stm_check_lock_status_sr(nor, ofs, len, sr, true);
9307aeedac0SVignesh R }
9317aeedac0SVignesh R 
stm_is_unlocked_sr(struct spi_nor * nor,loff_t ofs,uint64_t len,u8 sr)9327aeedac0SVignesh R static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
9337aeedac0SVignesh R 			      u8 sr)
9347aeedac0SVignesh R {
9357aeedac0SVignesh R 	return stm_check_lock_status_sr(nor, ofs, len, sr, false);
9367aeedac0SVignesh R }
9377aeedac0SVignesh R 
9387aeedac0SVignesh R /*
9397aeedac0SVignesh R  * Lock a region of the flash. Compatible with ST Micro and similar flash.
9407aeedac0SVignesh R  * Supports the block protection bits BP{0,1,2} in the status register
9417aeedac0SVignesh R  * (SR). Does not support these features found in newer SR bitfields:
9427aeedac0SVignesh R  *   - SEC: sector/block protect - only handle SEC=0 (block protect)
9437aeedac0SVignesh R  *   - CMP: complement protect - only support CMP=0 (range is not complemented)
9447aeedac0SVignesh R  *
9457aeedac0SVignesh R  * Support for the following is provided conditionally for some flash:
9467aeedac0SVignesh R  *   - TB: top/bottom protect
9477aeedac0SVignesh R  *
9487aeedac0SVignesh R  * Sample table portion for 8MB flash (Winbond w25q64fw):
9497aeedac0SVignesh R  *
9507aeedac0SVignesh R  *   SEC  |  TB   |  BP2  |  BP1  |  BP0  |  Prot Length  | Protected Portion
9517aeedac0SVignesh R  *  --------------------------------------------------------------------------
9527aeedac0SVignesh R  *    X   |   X   |   0   |   0   |   0   |  NONE         | NONE
9537aeedac0SVignesh R  *    0   |   0   |   0   |   0   |   1   |  128 KB       | Upper 1/64
9547aeedac0SVignesh R  *    0   |   0   |   0   |   1   |   0   |  256 KB       | Upper 1/32
9557aeedac0SVignesh R  *    0   |   0   |   0   |   1   |   1   |  512 KB       | Upper 1/16
9567aeedac0SVignesh R  *    0   |   0   |   1   |   0   |   0   |  1 MB         | Upper 1/8
9577aeedac0SVignesh R  *    0   |   0   |   1   |   0   |   1   |  2 MB         | Upper 1/4
9587aeedac0SVignesh R  *    0   |   0   |   1   |   1   |   0   |  4 MB         | Upper 1/2
9597aeedac0SVignesh R  *    X   |   X   |   1   |   1   |   1   |  8 MB         | ALL
9607aeedac0SVignesh R  *  ------|-------|-------|-------|-------|---------------|-------------------
9617aeedac0SVignesh R  *    0   |   1   |   0   |   0   |   1   |  128 KB       | Lower 1/64
9627aeedac0SVignesh R  *    0   |   1   |   0   |   1   |   0   |  256 KB       | Lower 1/32
9637aeedac0SVignesh R  *    0   |   1   |   0   |   1   |   1   |  512 KB       | Lower 1/16
9647aeedac0SVignesh R  *    0   |   1   |   1   |   0   |   0   |  1 MB         | Lower 1/8
9657aeedac0SVignesh R  *    0   |   1   |   1   |   0   |   1   |  2 MB         | Lower 1/4
9667aeedac0SVignesh R  *    0   |   1   |   1   |   1   |   0   |  4 MB         | Lower 1/2
9677aeedac0SVignesh R  *
9687aeedac0SVignesh R  * Returns negative on errors, 0 on success.
9697aeedac0SVignesh R  */
stm_lock(struct spi_nor * nor,loff_t ofs,uint64_t len)9707aeedac0SVignesh R static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
9717aeedac0SVignesh R {
9727aeedac0SVignesh R 	struct mtd_info *mtd = &nor->mtd;
9737aeedac0SVignesh R 	int status_old, status_new;
9747aeedac0SVignesh R 	u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
9757aeedac0SVignesh R 	u8 shift = ffs(mask) - 1, pow, val;
9767aeedac0SVignesh R 	loff_t lock_len;
9777aeedac0SVignesh R 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
9787aeedac0SVignesh R 	bool use_top;
9797aeedac0SVignesh R 
9807aeedac0SVignesh R 	status_old = read_sr(nor);
9817aeedac0SVignesh R 	if (status_old < 0)
9827aeedac0SVignesh R 		return status_old;
9837aeedac0SVignesh R 
9847aeedac0SVignesh R 	/* If nothing in our range is unlocked, we don't need to do anything */
9857aeedac0SVignesh R 	if (stm_is_locked_sr(nor, ofs, len, status_old))
9867aeedac0SVignesh R 		return 0;
9877aeedac0SVignesh R 
9887aeedac0SVignesh R 	/* If anything below us is unlocked, we can't use 'bottom' protection */
9897aeedac0SVignesh R 	if (!stm_is_locked_sr(nor, 0, ofs, status_old))
9907aeedac0SVignesh R 		can_be_bottom = false;
9917aeedac0SVignesh R 
9927aeedac0SVignesh R 	/* If anything above us is unlocked, we can't use 'top' protection */
9937aeedac0SVignesh R 	if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
9947aeedac0SVignesh R 			      status_old))
9957aeedac0SVignesh R 		can_be_top = false;
9967aeedac0SVignesh R 
9977aeedac0SVignesh R 	if (!can_be_bottom && !can_be_top)
9987aeedac0SVignesh R 		return -EINVAL;
9997aeedac0SVignesh R 
10007aeedac0SVignesh R 	/* Prefer top, if both are valid */
10017aeedac0SVignesh R 	use_top = can_be_top;
10027aeedac0SVignesh R 
10037aeedac0SVignesh R 	/* lock_len: length of region that should end up locked */
10047aeedac0SVignesh R 	if (use_top)
10057aeedac0SVignesh R 		lock_len = mtd->size - ofs;
10067aeedac0SVignesh R 	else
10077aeedac0SVignesh R 		lock_len = ofs + len;
10087aeedac0SVignesh R 
10097aeedac0SVignesh R 	/*
10107aeedac0SVignesh R 	 * Need smallest pow such that:
10117aeedac0SVignesh R 	 *
10127aeedac0SVignesh R 	 *   1 / (2^pow) <= (len / size)
10137aeedac0SVignesh R 	 *
10147aeedac0SVignesh R 	 * so (assuming power-of-2 size) we do:
10157aeedac0SVignesh R 	 *
10167aeedac0SVignesh R 	 *   pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
10177aeedac0SVignesh R 	 */
10187aeedac0SVignesh R 	pow = ilog2(mtd->size) - ilog2(lock_len);
10197aeedac0SVignesh R 	val = mask - (pow << shift);
10207aeedac0SVignesh R 	if (val & ~mask)
10217aeedac0SVignesh R 		return -EINVAL;
10227aeedac0SVignesh R 	/* Don't "lock" with no region! */
10237aeedac0SVignesh R 	if (!(val & mask))
10247aeedac0SVignesh R 		return -EINVAL;
10257aeedac0SVignesh R 
10267aeedac0SVignesh R 	status_new = (status_old & ~mask & ~SR_TB) | val;
10277aeedac0SVignesh R 
10287aeedac0SVignesh R 	/* Disallow further writes if WP pin is asserted */
10297aeedac0SVignesh R 	status_new |= SR_SRWD;
10307aeedac0SVignesh R 
10317aeedac0SVignesh R 	if (!use_top)
10327aeedac0SVignesh R 		status_new |= SR_TB;
10337aeedac0SVignesh R 
10347aeedac0SVignesh R 	/* Don't bother if they're the same */
10357aeedac0SVignesh R 	if (status_new == status_old)
10367aeedac0SVignesh R 		return 0;
10377aeedac0SVignesh R 
10387aeedac0SVignesh R 	/* Only modify protection if it will not unlock other areas */
10397aeedac0SVignesh R 	if ((status_new & mask) < (status_old & mask))
10407aeedac0SVignesh R 		return -EINVAL;
10417aeedac0SVignesh R 
10427aeedac0SVignesh R 	return write_sr_and_check(nor, status_new, mask);
10437aeedac0SVignesh R }
10447aeedac0SVignesh R 
10457aeedac0SVignesh R /*
10467aeedac0SVignesh R  * Unlock a region of the flash. See stm_lock() for more info
10477aeedac0SVignesh R  *
10487aeedac0SVignesh R  * Returns negative on errors, 0 on success.
10497aeedac0SVignesh R  */
stm_unlock(struct spi_nor * nor,loff_t ofs,uint64_t len)10507aeedac0SVignesh R static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
10517aeedac0SVignesh R {
10527aeedac0SVignesh R 	struct mtd_info *mtd = &nor->mtd;
10537aeedac0SVignesh R 	int status_old, status_new;
10547aeedac0SVignesh R 	u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
10557aeedac0SVignesh R 	u8 shift = ffs(mask) - 1, pow, val;
10567aeedac0SVignesh R 	loff_t lock_len;
10577aeedac0SVignesh R 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
10587aeedac0SVignesh R 	bool use_top;
10597aeedac0SVignesh R 
10607aeedac0SVignesh R 	status_old = read_sr(nor);
10617aeedac0SVignesh R 	if (status_old < 0)
10627aeedac0SVignesh R 		return status_old;
10637aeedac0SVignesh R 
10647aeedac0SVignesh R 	/* If nothing in our range is locked, we don't need to do anything */
10657aeedac0SVignesh R 	if (stm_is_unlocked_sr(nor, ofs, len, status_old))
10667aeedac0SVignesh R 		return 0;
10677aeedac0SVignesh R 
10687aeedac0SVignesh R 	/* If anything below us is locked, we can't use 'top' protection */
10697aeedac0SVignesh R 	if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
10707aeedac0SVignesh R 		can_be_top = false;
10717aeedac0SVignesh R 
10727aeedac0SVignesh R 	/* If anything above us is locked, we can't use 'bottom' protection */
10737aeedac0SVignesh R 	if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
10747aeedac0SVignesh R 				status_old))
10757aeedac0SVignesh R 		can_be_bottom = false;
10767aeedac0SVignesh R 
10777aeedac0SVignesh R 	if (!can_be_bottom && !can_be_top)
10787aeedac0SVignesh R 		return -EINVAL;
10797aeedac0SVignesh R 
10807aeedac0SVignesh R 	/* Prefer top, if both are valid */
10817aeedac0SVignesh R 	use_top = can_be_top;
10827aeedac0SVignesh R 
10837aeedac0SVignesh R 	/* lock_len: length of region that should remain locked */
10847aeedac0SVignesh R 	if (use_top)
10857aeedac0SVignesh R 		lock_len = mtd->size - (ofs + len);
10867aeedac0SVignesh R 	else
10877aeedac0SVignesh R 		lock_len = ofs;
10887aeedac0SVignesh R 
10897aeedac0SVignesh R 	/*
10907aeedac0SVignesh R 	 * Need largest pow such that:
10917aeedac0SVignesh R 	 *
10927aeedac0SVignesh R 	 *   1 / (2^pow) >= (len / size)
10937aeedac0SVignesh R 	 *
10947aeedac0SVignesh R 	 * so (assuming power-of-2 size) we do:
10957aeedac0SVignesh R 	 *
10967aeedac0SVignesh R 	 *   pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
10977aeedac0SVignesh R 	 */
10987aeedac0SVignesh R 	pow = ilog2(mtd->size) - order_base_2(lock_len);
10997aeedac0SVignesh R 	if (lock_len == 0) {
11007aeedac0SVignesh R 		val = 0; /* fully unlocked */
11017aeedac0SVignesh R 	} else {
11027aeedac0SVignesh R 		val = mask - (pow << shift);
11037aeedac0SVignesh R 		/* Some power-of-two sizes are not supported */
11047aeedac0SVignesh R 		if (val & ~mask)
11057aeedac0SVignesh R 			return -EINVAL;
11067aeedac0SVignesh R 	}
11077aeedac0SVignesh R 
11087aeedac0SVignesh R 	status_new = (status_old & ~mask & ~SR_TB) | val;
11097aeedac0SVignesh R 
11107aeedac0SVignesh R 	/* Don't protect status register if we're fully unlocked */
11117aeedac0SVignesh R 	if (lock_len == 0)
11127aeedac0SVignesh R 		status_new &= ~SR_SRWD;
11137aeedac0SVignesh R 
11147aeedac0SVignesh R 	if (!use_top)
11157aeedac0SVignesh R 		status_new |= SR_TB;
11167aeedac0SVignesh R 
11177aeedac0SVignesh R 	/* Don't bother if they're the same */
11187aeedac0SVignesh R 	if (status_new == status_old)
11197aeedac0SVignesh R 		return 0;
11207aeedac0SVignesh R 
11217aeedac0SVignesh R 	/* Only modify protection if it will not lock other areas */
11227aeedac0SVignesh R 	if ((status_new & mask) > (status_old & mask))
11237aeedac0SVignesh R 		return -EINVAL;
11247aeedac0SVignesh R 
11257aeedac0SVignesh R 	return write_sr_and_check(nor, status_new, mask);
11267aeedac0SVignesh R }
11277aeedac0SVignesh R 
11287aeedac0SVignesh R /*
11297aeedac0SVignesh R  * Check if a region of the flash is (completely) locked. See stm_lock() for
11307aeedac0SVignesh R  * more info.
11317aeedac0SVignesh R  *
11327aeedac0SVignesh R  * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
11337aeedac0SVignesh R  * negative on errors.
11347aeedac0SVignesh R  */
stm_is_locked(struct spi_nor * nor,loff_t ofs,uint64_t len)11357aeedac0SVignesh R static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
11367aeedac0SVignesh R {
11377aeedac0SVignesh R 	int status;
11387aeedac0SVignesh R 
11397aeedac0SVignesh R 	status = read_sr(nor);
11407aeedac0SVignesh R 	if (status < 0)
11417aeedac0SVignesh R 		return status;
11427aeedac0SVignesh R 
11437aeedac0SVignesh R 	return stm_is_locked_sr(nor, ofs, len, status);
11447aeedac0SVignesh R }
11457aeedac0SVignesh R #endif /* CONFIG_SPI_FLASH_STMICRO */
11467aeedac0SVignesh R 
spi_nor_wlock_by_host_ctrl(struct spi_nor * nor,u32 offset,size_t len)1147*591e1cf0SChin-Ting Kuo static int spi_nor_wlock_by_host_ctrl(struct spi_nor *nor,
1148*591e1cf0SChin-Ting Kuo 				      u32 offset, size_t len)
1149*591e1cf0SChin-Ting Kuo {
1150*591e1cf0SChin-Ting Kuo 	struct spi_slave *spi = nor->spi;
1151*591e1cf0SChin-Ting Kuo 	int ret;
1152*591e1cf0SChin-Ting Kuo 
1153*591e1cf0SChin-Ting Kuo 	ret = spi_claim_bus(spi);
1154*591e1cf0SChin-Ting Kuo 	if (ret < 0)
1155*591e1cf0SChin-Ting Kuo 		return ret;
1156*591e1cf0SChin-Ting Kuo 
1157*591e1cf0SChin-Ting Kuo 	ret = spi_nor_ctrl_wlock(spi, offset, len);
1158*591e1cf0SChin-Ting Kuo 
1159*591e1cf0SChin-Ting Kuo 	spi_release_bus(spi);
1160*591e1cf0SChin-Ting Kuo 
1161*591e1cf0SChin-Ting Kuo 	return ret;
1162*591e1cf0SChin-Ting Kuo }
1163*591e1cf0SChin-Ting Kuo 
spi_nor_wunlock_by_host_ctrl(struct spi_nor * nor,u32 offset,size_t len)1164*591e1cf0SChin-Ting Kuo static int spi_nor_wunlock_by_host_ctrl(struct spi_nor *nor,
1165*591e1cf0SChin-Ting Kuo 					u32 offset, size_t len)
1166*591e1cf0SChin-Ting Kuo {
1167*591e1cf0SChin-Ting Kuo 	struct spi_slave *spi = nor->spi;
1168*591e1cf0SChin-Ting Kuo 	int ret;
1169*591e1cf0SChin-Ting Kuo 
1170*591e1cf0SChin-Ting Kuo 	ret = spi_claim_bus(spi);
1171*591e1cf0SChin-Ting Kuo 	if (ret < 0)
1172*591e1cf0SChin-Ting Kuo 		return ret;
1173*591e1cf0SChin-Ting Kuo 
1174*591e1cf0SChin-Ting Kuo 	ret = spi_nor_ctrl_wunlock(spi, offset, len);
1175*591e1cf0SChin-Ting Kuo 
1176*591e1cf0SChin-Ting Kuo 	spi_release_bus(spi);
1177*591e1cf0SChin-Ting Kuo 
1178*591e1cf0SChin-Ting Kuo 	return ret;
1179*591e1cf0SChin-Ting Kuo }
1180*591e1cf0SChin-Ting Kuo 
spi_nor_read_id(struct spi_nor * nor)11817aeedac0SVignesh R static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
11827aeedac0SVignesh R {
11837aeedac0SVignesh R 	int			tmp;
11847aeedac0SVignesh R 	u8			id[SPI_NOR_MAX_ID_LEN];
11857aeedac0SVignesh R 	const struct flash_info	*info;
11867aeedac0SVignesh R 
11877aeedac0SVignesh R 	tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
11887aeedac0SVignesh R 	if (tmp < 0) {
11897aeedac0SVignesh R 		dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
11907aeedac0SVignesh R 		return ERR_PTR(tmp);
11917aeedac0SVignesh R 	}
11927aeedac0SVignesh R 
1193778572d7SVignesh R 	info = spi_nor_ids;
1194778572d7SVignesh R 	for (; info->name; info++) {
11957aeedac0SVignesh R 		if (info->id_len) {
11967aeedac0SVignesh R 			if (!memcmp(info->id, id, info->id_len))
1197778572d7SVignesh R 				return info;
11987aeedac0SVignesh R 		}
11997aeedac0SVignesh R 	}
1200778572d7SVignesh R 
12017aeedac0SVignesh R 	dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
12027aeedac0SVignesh R 		id[0], id[1], id[2]);
12037aeedac0SVignesh R 	return ERR_PTR(-ENODEV);
12047aeedac0SVignesh R }
12057aeedac0SVignesh R 
spi_nor_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)12067aeedac0SVignesh R static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
12077aeedac0SVignesh R 			size_t *retlen, u_char *buf)
12087aeedac0SVignesh R {
12097aeedac0SVignesh R 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
12107aeedac0SVignesh R 	int ret;
12117aeedac0SVignesh R 
12127aeedac0SVignesh R 	dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
12137aeedac0SVignesh R 
12147aeedac0SVignesh R 	while (len) {
12157aeedac0SVignesh R 		loff_t addr = from;
12168c927809SVignesh R 		size_t read_len = len;
12177aeedac0SVignesh R 
12188c927809SVignesh R #ifdef CONFIG_SPI_FLASH_BAR
12198c927809SVignesh R 		u32 remain_len;
12208c927809SVignesh R 
12218c927809SVignesh R 		ret = write_bar(nor, addr);
12228c927809SVignesh R 		if (ret < 0)
12238c927809SVignesh R 			return log_ret(ret);
12248c927809SVignesh R 		remain_len = (SZ_16M * (nor->bank_curr + 1)) - addr;
12258c927809SVignesh R 
12268c927809SVignesh R 		if (len < remain_len)
12278c927809SVignesh R 			read_len = len;
12288c927809SVignesh R 		else
12298c927809SVignesh R 			read_len = remain_len;
12308c927809SVignesh R #endif
12318c927809SVignesh R 
12328c927809SVignesh R 		ret = nor->read(nor, addr, read_len, buf);
12337aeedac0SVignesh R 		if (ret == 0) {
12347aeedac0SVignesh R 			/* We shouldn't see 0-length reads */
12357aeedac0SVignesh R 			ret = -EIO;
12367aeedac0SVignesh R 			goto read_err;
12377aeedac0SVignesh R 		}
12387aeedac0SVignesh R 		if (ret < 0)
12397aeedac0SVignesh R 			goto read_err;
12407aeedac0SVignesh R 
12417aeedac0SVignesh R 		*retlen += ret;
12427aeedac0SVignesh R 		buf += ret;
12437aeedac0SVignesh R 		from += ret;
12447aeedac0SVignesh R 		len -= ret;
12457aeedac0SVignesh R 	}
12467aeedac0SVignesh R 	ret = 0;
12477aeedac0SVignesh R 
12487aeedac0SVignesh R read_err:
12498c927809SVignesh R #ifdef CONFIG_SPI_FLASH_BAR
12508c927809SVignesh R 	ret = clean_bar(nor);
12518c927809SVignesh R #endif
12527aeedac0SVignesh R 	return ret;
12537aeedac0SVignesh R }
12547aeedac0SVignesh R 
1255838fb1e6SChin-Ting Kuo #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
sst_read_nvcr(struct spi_nor * nor)1256838fb1e6SChin-Ting Kuo static int sst_read_nvcr(struct spi_nor *nor)
1257838fb1e6SChin-Ting Kuo {
1258838fb1e6SChin-Ting Kuo 	int ret;
1259838fb1e6SChin-Ting Kuo 	int val;
1260838fb1e6SChin-Ting Kuo 
1261838fb1e6SChin-Ting Kuo 	ret = nor->read_reg(nor, SPINOR_OP_SST_RDNVCR, (u8 *)&val, 2);
1262838fb1e6SChin-Ting Kuo 	if (ret < 0) {
1263838fb1e6SChin-Ting Kuo 		dev_err(nor->dev, "SST error %d while reading CR\n", ret);
1264838fb1e6SChin-Ting Kuo 		return ret;
1265838fb1e6SChin-Ting Kuo 	}
1266838fb1e6SChin-Ting Kuo 
1267838fb1e6SChin-Ting Kuo 	return val;
1268838fb1e6SChin-Ting Kuo }
1269838fb1e6SChin-Ting Kuo 
sst_write_nvcr(struct spi_nor * nor,int val)1270838fb1e6SChin-Ting Kuo static int sst_write_nvcr(struct spi_nor *nor, int val)
1271838fb1e6SChin-Ting Kuo {
1272838fb1e6SChin-Ting Kuo 	int ret;
1273838fb1e6SChin-Ting Kuo 
1274838fb1e6SChin-Ting Kuo 	write_enable(nor);
1275838fb1e6SChin-Ting Kuo 
1276838fb1e6SChin-Ting Kuo 	nor->cmd_buf[0] = val & 0xff;
1277838fb1e6SChin-Ting Kuo 	nor->cmd_buf[1] = (val >> 8) & 0xff;
1278838fb1e6SChin-Ting Kuo 
1279838fb1e6SChin-Ting Kuo 	ret = nor->write_reg(nor, SPINOR_OP_SST_WRNVCR, nor->cmd_buf, 2);
1280838fb1e6SChin-Ting Kuo 	if (ret < 0) {
1281838fb1e6SChin-Ting Kuo 		dev_err(nor->dev,
1282838fb1e6SChin-Ting Kuo 			"SST error while writing configuration register\n");
1283838fb1e6SChin-Ting Kuo 		return -EINVAL;
1284838fb1e6SChin-Ting Kuo 	}
1285838fb1e6SChin-Ting Kuo 
1286838fb1e6SChin-Ting Kuo 	ret = spi_nor_wait_till_ready(nor);
1287838fb1e6SChin-Ting Kuo 	if (ret) {
1288838fb1e6SChin-Ting Kuo 		dev_err(nor->dev,
1289838fb1e6SChin-Ting Kuo 			"SST timeout while writing configuration register\n");
1290838fb1e6SChin-Ting Kuo 		return ret;
1291838fb1e6SChin-Ting Kuo 	}
1292838fb1e6SChin-Ting Kuo 
1293838fb1e6SChin-Ting Kuo 	return 0;
1294838fb1e6SChin-Ting Kuo }
1295838fb1e6SChin-Ting Kuo 
sst_cr_quad_enable(struct spi_nor * nor)1296838fb1e6SChin-Ting Kuo static int sst_cr_quad_enable(struct spi_nor *nor)
1297838fb1e6SChin-Ting Kuo {
1298838fb1e6SChin-Ting Kuo 	int ret;
1299838fb1e6SChin-Ting Kuo 
1300838fb1e6SChin-Ting Kuo 	/* Check current Quad Enable bit value. */
1301838fb1e6SChin-Ting Kuo 	ret = sst_read_nvcr(nor);
1302838fb1e6SChin-Ting Kuo 	if (ret < 0) {
1303838fb1e6SChin-Ting Kuo 		dev_dbg(nor->dev, "SST error while reading nonvolatile configuration register\n");
1304838fb1e6SChin-Ting Kuo 		return -EINVAL;
1305838fb1e6SChin-Ting Kuo 	}
1306838fb1e6SChin-Ting Kuo 
1307838fb1e6SChin-Ting Kuo 	if ((ret & SPINOR_SST_RST_HOLD_CTRL) == 0)
1308838fb1e6SChin-Ting Kuo 		return 0;
1309838fb1e6SChin-Ting Kuo 
1310838fb1e6SChin-Ting Kuo 	/* Nonvolatile Configuration Register bit 4 */
1311838fb1e6SChin-Ting Kuo 	ret &= ~SPINOR_SST_RST_HOLD_CTRL;
1312838fb1e6SChin-Ting Kuo 
1313838fb1e6SChin-Ting Kuo 	/* Keep the current value of the Status Register. */
1314838fb1e6SChin-Ting Kuo 	ret = sst_write_nvcr(nor, ret);
1315838fb1e6SChin-Ting Kuo 	if (ret < 0) {
1316838fb1e6SChin-Ting Kuo 		dev_err(nor->dev, "SST error while writing nonvolatile configuration register\n");
1317838fb1e6SChin-Ting Kuo 		return -EINVAL;
1318838fb1e6SChin-Ting Kuo 	}
1319838fb1e6SChin-Ting Kuo 
1320838fb1e6SChin-Ting Kuo 	ret = sst_read_nvcr(nor);
1321838fb1e6SChin-Ting Kuo 	if (ret > 0 && (ret & SPINOR_SST_RST_HOLD_CTRL)) {
1322838fb1e6SChin-Ting Kuo 		dev_err(nor->dev, "SST Quad bit not set\n");
1323838fb1e6SChin-Ting Kuo 		return -EINVAL;
1324838fb1e6SChin-Ting Kuo 	}
1325838fb1e6SChin-Ting Kuo 
1326838fb1e6SChin-Ting Kuo 	return 0;
1327838fb1e6SChin-Ting Kuo }
1328838fb1e6SChin-Ting Kuo #endif
1329838fb1e6SChin-Ting Kuo 
13307aeedac0SVignesh R #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)13318c927809SVignesh R static int sst_write_byteprogram(struct spi_nor *nor, loff_t to, size_t len,
13328c927809SVignesh R 				 size_t *retlen, const u_char *buf)
13338c927809SVignesh R {
13348c927809SVignesh R 	size_t actual;
13358c927809SVignesh R 	int ret = 0;
13368c927809SVignesh R 
13378c927809SVignesh R 	for (actual = 0; actual < len; actual++) {
13388c927809SVignesh R 		nor->program_opcode = SPINOR_OP_BP;
13398c927809SVignesh R 
13408c927809SVignesh R 		write_enable(nor);
13418c927809SVignesh R 		/* write one byte. */
13428c927809SVignesh R 		ret = nor->write(nor, to, 1, buf + actual);
13438c927809SVignesh R 		if (ret < 0)
13448c927809SVignesh R 			goto sst_write_err;
13458c927809SVignesh R 		ret = spi_nor_wait_till_ready(nor);
13468c927809SVignesh R 		if (ret)
13478c927809SVignesh R 			goto sst_write_err;
13488c927809SVignesh R 		to++;
13498c927809SVignesh R 	}
13508c927809SVignesh R 
13518c927809SVignesh R sst_write_err:
13528c927809SVignesh R 	write_disable(nor);
13538c927809SVignesh R 	return ret;
13548c927809SVignesh R }
13558c927809SVignesh R 
sst_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)13567aeedac0SVignesh R static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
13577aeedac0SVignesh R 		     size_t *retlen, const u_char *buf)
13587aeedac0SVignesh R {
13597aeedac0SVignesh R 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
13608c927809SVignesh R 	struct spi_slave *spi = nor->spi;
13617aeedac0SVignesh R 	size_t actual;
13627aeedac0SVignesh R 	int ret;
13637aeedac0SVignesh R 
13647aeedac0SVignesh R 	dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
13658c927809SVignesh R 	if (spi->mode & SPI_TX_BYTE)
13668c927809SVignesh R 		return sst_write_byteprogram(nor, to, len, retlen, buf);
13677aeedac0SVignesh R 
13687aeedac0SVignesh R 	write_enable(nor);
13697aeedac0SVignesh R 
13707aeedac0SVignesh R 	nor->sst_write_second = false;
13717aeedac0SVignesh R 
13727aeedac0SVignesh R 	actual = to % 2;
13737aeedac0SVignesh R 	/* Start write from odd address. */
13747aeedac0SVignesh R 	if (actual) {
13757aeedac0SVignesh R 		nor->program_opcode = SPINOR_OP_BP;
13767aeedac0SVignesh R 
13777aeedac0SVignesh R 		/* write one byte. */
13787aeedac0SVignesh R 		ret = nor->write(nor, to, 1, buf);
13797aeedac0SVignesh R 		if (ret < 0)
13807aeedac0SVignesh R 			goto sst_write_err;
13817aeedac0SVignesh R 		ret = spi_nor_wait_till_ready(nor);
13827aeedac0SVignesh R 		if (ret)
13837aeedac0SVignesh R 			goto sst_write_err;
13847aeedac0SVignesh R 	}
13857aeedac0SVignesh R 	to += actual;
13867aeedac0SVignesh R 
13877aeedac0SVignesh R 	/* Write out most of the data here. */
13887aeedac0SVignesh R 	for (; actual < len - 1; actual += 2) {
13897aeedac0SVignesh R 		nor->program_opcode = SPINOR_OP_AAI_WP;
13907aeedac0SVignesh R 
13917aeedac0SVignesh R 		/* write two bytes. */
13927aeedac0SVignesh R 		ret = nor->write(nor, to, 2, buf + actual);
13937aeedac0SVignesh R 		if (ret < 0)
13947aeedac0SVignesh R 			goto sst_write_err;
13957aeedac0SVignesh R 		ret = spi_nor_wait_till_ready(nor);
13967aeedac0SVignesh R 		if (ret)
13977aeedac0SVignesh R 			goto sst_write_err;
13987aeedac0SVignesh R 		to += 2;
13997aeedac0SVignesh R 		nor->sst_write_second = true;
14007aeedac0SVignesh R 	}
14017aeedac0SVignesh R 	nor->sst_write_second = false;
14027aeedac0SVignesh R 
14037aeedac0SVignesh R 	write_disable(nor);
14047aeedac0SVignesh R 	ret = spi_nor_wait_till_ready(nor);
14057aeedac0SVignesh R 	if (ret)
14067aeedac0SVignesh R 		goto sst_write_err;
14077aeedac0SVignesh R 
14087aeedac0SVignesh R 	/* Write out trailing byte if it exists. */
14097aeedac0SVignesh R 	if (actual != len) {
14107aeedac0SVignesh R 		write_enable(nor);
14117aeedac0SVignesh R 
14127aeedac0SVignesh R 		nor->program_opcode = SPINOR_OP_BP;
14137aeedac0SVignesh R 		ret = nor->write(nor, to, 1, buf + actual);
14147aeedac0SVignesh R 		if (ret < 0)
14157aeedac0SVignesh R 			goto sst_write_err;
14167aeedac0SVignesh R 		ret = spi_nor_wait_till_ready(nor);
14177aeedac0SVignesh R 		if (ret)
14187aeedac0SVignesh R 			goto sst_write_err;
14197aeedac0SVignesh R 		write_disable(nor);
14207aeedac0SVignesh R 		actual += 1;
14217aeedac0SVignesh R 	}
14227aeedac0SVignesh R sst_write_err:
14237aeedac0SVignesh R 	*retlen += actual;
14247aeedac0SVignesh R 	return ret;
14257aeedac0SVignesh R }
14267aeedac0SVignesh R #endif
14277aeedac0SVignesh R /*
14287aeedac0SVignesh R  * Write an address range to the nor chip.  Data must be written in
14297aeedac0SVignesh R  * FLASH_PAGESIZE chunks.  The address range may be any size provided
14307aeedac0SVignesh R  * it is within the physical boundaries.
14317aeedac0SVignesh R  */
spi_nor_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)14327aeedac0SVignesh R static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
14337aeedac0SVignesh R 	size_t *retlen, const u_char *buf)
14347aeedac0SVignesh R {
14357aeedac0SVignesh R 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
14367aeedac0SVignesh R 	size_t page_offset, page_remain, i;
14377aeedac0SVignesh R 	ssize_t ret;
14387aeedac0SVignesh R 
14397aeedac0SVignesh R 	dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
14407aeedac0SVignesh R 
14417aeedac0SVignesh R 	for (i = 0; i < len; ) {
14427aeedac0SVignesh R 		ssize_t written;
14437aeedac0SVignesh R 		loff_t addr = to + i;
14447aeedac0SVignesh R 
14457aeedac0SVignesh R 		/*
14467aeedac0SVignesh R 		 * If page_size is a power of two, the offset can be quickly
14477aeedac0SVignesh R 		 * calculated with an AND operation. On the other cases we
14487aeedac0SVignesh R 		 * need to do a modulus operation (more expensive).
14497aeedac0SVignesh R 		 * Power of two numbers have only one bit set and we can use
14507aeedac0SVignesh R 		 * the instruction hweight32 to detect if we need to do a
14517aeedac0SVignesh R 		 * modulus (do_div()) or not.
14527aeedac0SVignesh R 		 */
14537aeedac0SVignesh R 		if (hweight32(nor->page_size) == 1) {
14547aeedac0SVignesh R 			page_offset = addr & (nor->page_size - 1);
14557aeedac0SVignesh R 		} else {
14567aeedac0SVignesh R 			u64 aux = addr;
14577aeedac0SVignesh R 
14587aeedac0SVignesh R 			page_offset = do_div(aux, nor->page_size);
14597aeedac0SVignesh R 		}
14607aeedac0SVignesh R 		/* the size of data remaining on the first page */
14617aeedac0SVignesh R 		page_remain = min_t(size_t,
14627aeedac0SVignesh R 				    nor->page_size - page_offset, len - i);
14637aeedac0SVignesh R 
14648c927809SVignesh R #ifdef CONFIG_SPI_FLASH_BAR
14658c927809SVignesh R 		ret = write_bar(nor, addr);
14668c927809SVignesh R 		if (ret < 0)
14678c927809SVignesh R 			return ret;
14688c927809SVignesh R #endif
14697aeedac0SVignesh R 		write_enable(nor);
14707aeedac0SVignesh R 		ret = nor->write(nor, addr, page_remain, buf + i);
14717aeedac0SVignesh R 		if (ret < 0)
14727aeedac0SVignesh R 			goto write_err;
14737aeedac0SVignesh R 		written = ret;
14747aeedac0SVignesh R 
14757aeedac0SVignesh R 		ret = spi_nor_wait_till_ready(nor);
14767aeedac0SVignesh R 		if (ret)
14777aeedac0SVignesh R 			goto write_err;
14787aeedac0SVignesh R 		*retlen += written;
14797aeedac0SVignesh R 		i += written;
14807aeedac0SVignesh R 		if (written != page_remain) {
14817aeedac0SVignesh R 			ret = -EIO;
14827aeedac0SVignesh R 			goto write_err;
14837aeedac0SVignesh R 		}
14847aeedac0SVignesh R 	}
14857aeedac0SVignesh R 
14867aeedac0SVignesh R write_err:
14878c927809SVignesh R #ifdef CONFIG_SPI_FLASH_BAR
14888c927809SVignesh R 	ret = clean_bar(nor);
14898c927809SVignesh R #endif
14907aeedac0SVignesh R 	return ret;
14917aeedac0SVignesh R }
14927aeedac0SVignesh R 
14937aeedac0SVignesh R #ifdef CONFIG_SPI_FLASH_MACRONIX
14947aeedac0SVignesh R /**
14957aeedac0SVignesh R  * macronix_quad_enable() - set QE bit in Status Register.
14967aeedac0SVignesh R  * @nor:	pointer to a 'struct spi_nor'
14977aeedac0SVignesh R  *
14987aeedac0SVignesh R  * Set the Quad Enable (QE) bit in the Status Register.
14997aeedac0SVignesh R  *
15007aeedac0SVignesh R  * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
15017aeedac0SVignesh R  *
15027aeedac0SVignesh R  * Return: 0 on success, -errno otherwise.
15037aeedac0SVignesh R  */
macronix_quad_enable(struct spi_nor * nor)15047aeedac0SVignesh R static int macronix_quad_enable(struct spi_nor *nor)
15057aeedac0SVignesh R {
15067aeedac0SVignesh R 	int ret, val;
15077aeedac0SVignesh R 
15087aeedac0SVignesh R 	val = read_sr(nor);
15097aeedac0SVignesh R 	if (val < 0)
15107aeedac0SVignesh R 		return val;
15117aeedac0SVignesh R 	if (val & SR_QUAD_EN_MX)
15127aeedac0SVignesh R 		return 0;
15137aeedac0SVignesh R 
15147aeedac0SVignesh R 	write_enable(nor);
15157aeedac0SVignesh R 
15167aeedac0SVignesh R 	write_sr(nor, val | SR_QUAD_EN_MX);
15177aeedac0SVignesh R 
15187aeedac0SVignesh R 	ret = spi_nor_wait_till_ready(nor);
15197aeedac0SVignesh R 	if (ret)
15207aeedac0SVignesh R 		return ret;
15217aeedac0SVignesh R 
15227aeedac0SVignesh R 	ret = read_sr(nor);
15237aeedac0SVignesh R 	if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
15247aeedac0SVignesh R 		dev_err(nor->dev, "Macronix Quad bit not set\n");
15257aeedac0SVignesh R 		return -EINVAL;
15267aeedac0SVignesh R 	}
15277aeedac0SVignesh R 
15287aeedac0SVignesh R 	return 0;
15297aeedac0SVignesh R }
15307aeedac0SVignesh R #endif
15317aeedac0SVignesh R 
15327aeedac0SVignesh R #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
15337aeedac0SVignesh R /*
15347aeedac0SVignesh R  * Write status Register and configuration register with 2 bytes
15357aeedac0SVignesh R  * The first byte will be written to the status register, while the
15367aeedac0SVignesh R  * second byte will be written to the configuration register.
15377aeedac0SVignesh R  * Return negative if error occurred.
15387aeedac0SVignesh R  */
write_sr_cr(struct spi_nor * nor,u8 * sr_cr)15397aeedac0SVignesh R static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
15407aeedac0SVignesh R {
15417aeedac0SVignesh R 	int ret;
15427aeedac0SVignesh R 
15437aeedac0SVignesh R 	write_enable(nor);
15447aeedac0SVignesh R 
15457aeedac0SVignesh R 	ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
15467aeedac0SVignesh R 	if (ret < 0) {
15477aeedac0SVignesh R 		dev_dbg(nor->dev,
15487aeedac0SVignesh R 			"error while writing configuration register\n");
15497aeedac0SVignesh R 		return -EINVAL;
15507aeedac0SVignesh R 	}
15517aeedac0SVignesh R 
15527aeedac0SVignesh R 	ret = spi_nor_wait_till_ready(nor);
15537aeedac0SVignesh R 	if (ret) {
15547aeedac0SVignesh R 		dev_dbg(nor->dev,
15557aeedac0SVignesh R 			"timeout while writing configuration register\n");
15567aeedac0SVignesh R 		return ret;
15577aeedac0SVignesh R 	}
15587aeedac0SVignesh R 
15597aeedac0SVignesh R 	return 0;
15607aeedac0SVignesh R }
15617aeedac0SVignesh R 
15627aeedac0SVignesh R /**
15637aeedac0SVignesh R  * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
15647aeedac0SVignesh R  * @nor:	pointer to a 'struct spi_nor'
15657aeedac0SVignesh R  *
15667aeedac0SVignesh R  * Set the Quad Enable (QE) bit in the Configuration Register.
15677aeedac0SVignesh R  * This function should be used with QSPI memories supporting the Read
15687aeedac0SVignesh R  * Configuration Register (35h) instruction.
15697aeedac0SVignesh R  *
15707aeedac0SVignesh R  * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
15717aeedac0SVignesh R  * memories.
15727aeedac0SVignesh R  *
15737aeedac0SVignesh R  * Return: 0 on success, -errno otherwise.
15747aeedac0SVignesh R  */
spansion_read_cr_quad_enable(struct spi_nor * nor)15757aeedac0SVignesh R static int spansion_read_cr_quad_enable(struct spi_nor *nor)
15767aeedac0SVignesh R {
15777aeedac0SVignesh R 	u8 sr_cr[2];
15787aeedac0SVignesh R 	int ret;
15797aeedac0SVignesh R 
15807aeedac0SVignesh R 	/* Check current Quad Enable bit value. */
15817aeedac0SVignesh R 	ret = read_cr(nor);
15827aeedac0SVignesh R 	if (ret < 0) {
1583878b2ba4SChin-Ting Kuo 		dev_dbg(nor->dev, "error while reading configuration register\n");
15847aeedac0SVignesh R 		return -EINVAL;
15857aeedac0SVignesh R 	}
15867aeedac0SVignesh R 
15877aeedac0SVignesh R 	if (ret & CR_QUAD_EN_SPAN)
15887aeedac0SVignesh R 		return 0;
15897aeedac0SVignesh R 
15907aeedac0SVignesh R 	sr_cr[1] = ret | CR_QUAD_EN_SPAN;
15917aeedac0SVignesh R 
15927aeedac0SVignesh R 	/* Keep the current value of the Status Register. */
15937aeedac0SVignesh R 	ret = read_sr(nor);
15947aeedac0SVignesh R 	if (ret < 0) {
1595878b2ba4SChin-Ting Kuo 		dev_dbg(nor->dev, "error while reading status register\n");
15967aeedac0SVignesh R 		return -EINVAL;
15977aeedac0SVignesh R 	}
15987aeedac0SVignesh R 	sr_cr[0] = ret;
15997aeedac0SVignesh R 
16007aeedac0SVignesh R 	ret = write_sr_cr(nor, sr_cr);
16017aeedac0SVignesh R 	if (ret)
16027aeedac0SVignesh R 		return ret;
16037aeedac0SVignesh R 
16047aeedac0SVignesh R 	/* Read back and check it. */
16057aeedac0SVignesh R 	ret = read_cr(nor);
16067aeedac0SVignesh R 	if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
16077aeedac0SVignesh R 		dev_dbg(nor->dev, "Spansion Quad bit not set\n");
16087aeedac0SVignesh R 		return -EINVAL;
16097aeedac0SVignesh R 	}
16107aeedac0SVignesh R 
16117aeedac0SVignesh R 	return 0;
16127aeedac0SVignesh R }
16130c6f187cSVignesh R 
1614878b2ba4SChin-Ting Kuo /**
1615878b2ba4SChin-Ting Kuo  * sr2_bit1_quad_enable() - set QE bit in Status Register 2.
1616878b2ba4SChin-Ting Kuo  * @nor:	pointer to a 'struct spi_nor'
1617878b2ba4SChin-Ting Kuo  *
1618878b2ba4SChin-Ting Kuo  * Set the Quad Enable (QE) bit in the Status Register 2.
1619878b2ba4SChin-Ting Kuo  *
1620878b2ba4SChin-Ting Kuo  * Return: 0 on success, -errno otherwise.
1621878b2ba4SChin-Ting Kuo  */
winbond_sr2_bit1_quad_enable(struct spi_nor * nor)1622878b2ba4SChin-Ting Kuo static int winbond_sr2_bit1_quad_enable(struct spi_nor *nor)
1623878b2ba4SChin-Ting Kuo {
1624878b2ba4SChin-Ting Kuo 	u8 sr2 = 0;
1625878b2ba4SChin-Ting Kuo 	int ret;
1626878b2ba4SChin-Ting Kuo 
1627878b2ba4SChin-Ting Kuo 	/* Check current Quad Enable bit value. */
1628878b2ba4SChin-Ting Kuo 	ret = read_winbond_sr2(nor);
1629878b2ba4SChin-Ting Kuo 	if (ret < 0) {
1630878b2ba4SChin-Ting Kuo 		dev_err(nor->dev, "error while reading status register 2\n");
1631878b2ba4SChin-Ting Kuo 		return -EINVAL;
1632878b2ba4SChin-Ting Kuo 	}
1633878b2ba4SChin-Ting Kuo 
1634878b2ba4SChin-Ting Kuo 	if (ret & SR2_QUAD_EN_BIT1)
1635878b2ba4SChin-Ting Kuo 		return 0;
1636878b2ba4SChin-Ting Kuo 
1637878b2ba4SChin-Ting Kuo 	/* Update the Quad Enable bit. */
1638878b2ba4SChin-Ting Kuo 	sr2 = (u8)(ret | SR2_QUAD_EN_BIT1);
1639878b2ba4SChin-Ting Kuo 
1640878b2ba4SChin-Ting Kuo 	write_enable(nor);
1641878b2ba4SChin-Ting Kuo 
1642878b2ba4SChin-Ting Kuo 	ret = write_winbond_sr2(nor, sr2);
1643878b2ba4SChin-Ting Kuo 	if (ret < 0) {
1644878b2ba4SChin-Ting Kuo 		dev_err(nor->dev, "error while writing status register 2\n");
1645878b2ba4SChin-Ting Kuo 		return -EINVAL;
1646878b2ba4SChin-Ting Kuo 	}
1647878b2ba4SChin-Ting Kuo 
1648878b2ba4SChin-Ting Kuo 	ret = spi_nor_wait_till_ready(nor);
1649878b2ba4SChin-Ting Kuo 	if (ret < 0) {
1650878b2ba4SChin-Ting Kuo 		dev_err(nor->dev, "timeout while writing status register 2\n");
1651878b2ba4SChin-Ting Kuo 		return ret;
1652878b2ba4SChin-Ting Kuo 	}
1653878b2ba4SChin-Ting Kuo 
1654878b2ba4SChin-Ting Kuo 	/* Read back and check it. */
1655878b2ba4SChin-Ting Kuo 	ret = read_winbond_sr2(nor);
1656878b2ba4SChin-Ting Kuo 	if (ret < 0 || !(ret & SR2_QUAD_EN_BIT1)) {
1657878b2ba4SChin-Ting Kuo 		dev_err(nor->dev, "SR2 Quad bit not set\n");
1658878b2ba4SChin-Ting Kuo 		return -EINVAL;
1659878b2ba4SChin-Ting Kuo 	}
1660878b2ba4SChin-Ting Kuo 
1661878b2ba4SChin-Ting Kuo 	return 0;
1662878b2ba4SChin-Ting Kuo }
1663878b2ba4SChin-Ting Kuo 
16640c6f187cSVignesh R #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
16650c6f187cSVignesh R /**
16660c6f187cSVignesh R  * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
16670c6f187cSVignesh R  * @nor:	pointer to a 'struct spi_nor'
16680c6f187cSVignesh R  *
16690c6f187cSVignesh R  * Set the Quad Enable (QE) bit in the Configuration Register.
16700c6f187cSVignesh R  * This function should be used with QSPI memories not supporting the Read
16710c6f187cSVignesh R  * Configuration Register (35h) instruction.
16720c6f187cSVignesh R  *
16730c6f187cSVignesh R  * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
16740c6f187cSVignesh R  * memories.
16750c6f187cSVignesh R  *
16760c6f187cSVignesh R  * Return: 0 on success, -errno otherwise.
16770c6f187cSVignesh R  */
spansion_no_read_cr_quad_enable(struct spi_nor * nor)16780c6f187cSVignesh R static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
16790c6f187cSVignesh R {
16800c6f187cSVignesh R 	u8 sr_cr[2];
16810c6f187cSVignesh R 	int ret;
16820c6f187cSVignesh R 
16830c6f187cSVignesh R 	/* Keep the current value of the Status Register. */
16840c6f187cSVignesh R 	ret = read_sr(nor);
16850c6f187cSVignesh R 	if (ret < 0) {
16860c6f187cSVignesh R 		dev_dbg(nor->dev, "error while reading status register\n");
16870c6f187cSVignesh R 		return -EINVAL;
16880c6f187cSVignesh R 	}
16890c6f187cSVignesh R 	sr_cr[0] = ret;
16900c6f187cSVignesh R 	sr_cr[1] = CR_QUAD_EN_SPAN;
16910c6f187cSVignesh R 
16920c6f187cSVignesh R 	return write_sr_cr(nor, sr_cr);
16930c6f187cSVignesh R }
16940c6f187cSVignesh R 
16950c6f187cSVignesh R #endif /* CONFIG_SPI_FLASH_SFDP_SUPPORT */
16967aeedac0SVignesh R #endif /* CONFIG_SPI_FLASH_SPANSION */
16977aeedac0SVignesh R 
16987aeedac0SVignesh R struct spi_nor_read_command {
16997aeedac0SVignesh R 	u8			num_mode_clocks;
17007aeedac0SVignesh R 	u8			num_wait_states;
17017aeedac0SVignesh R 	u8			opcode;
17027aeedac0SVignesh R 	enum spi_nor_protocol	proto;
17037aeedac0SVignesh R };
17047aeedac0SVignesh R 
17057aeedac0SVignesh R struct spi_nor_pp_command {
17067aeedac0SVignesh R 	u8			opcode;
17077aeedac0SVignesh R 	enum spi_nor_protocol	proto;
17087aeedac0SVignesh R };
17097aeedac0SVignesh R 
17107aeedac0SVignesh R enum spi_nor_read_command_index {
17117aeedac0SVignesh R 	SNOR_CMD_READ,
17127aeedac0SVignesh R 	SNOR_CMD_READ_FAST,
17137aeedac0SVignesh R 	SNOR_CMD_READ_1_1_1_DTR,
17147aeedac0SVignesh R 
17157aeedac0SVignesh R 	/* Dual SPI */
17167aeedac0SVignesh R 	SNOR_CMD_READ_1_1_2,
17177aeedac0SVignesh R 	SNOR_CMD_READ_1_2_2,
17187aeedac0SVignesh R 	SNOR_CMD_READ_2_2_2,
17197aeedac0SVignesh R 	SNOR_CMD_READ_1_2_2_DTR,
17207aeedac0SVignesh R 
17217aeedac0SVignesh R 	/* Quad SPI */
17227aeedac0SVignesh R 	SNOR_CMD_READ_1_1_4,
17237aeedac0SVignesh R 	SNOR_CMD_READ_1_4_4,
17247aeedac0SVignesh R 	SNOR_CMD_READ_4_4_4,
17257aeedac0SVignesh R 	SNOR_CMD_READ_1_4_4_DTR,
17267aeedac0SVignesh R 
17277aeedac0SVignesh R 	/* Octo SPI */
17287aeedac0SVignesh R 	SNOR_CMD_READ_1_1_8,
17297aeedac0SVignesh R 	SNOR_CMD_READ_1_8_8,
17307aeedac0SVignesh R 	SNOR_CMD_READ_8_8_8,
17317aeedac0SVignesh R 	SNOR_CMD_READ_1_8_8_DTR,
17327aeedac0SVignesh R 
17337aeedac0SVignesh R 	SNOR_CMD_READ_MAX
17347aeedac0SVignesh R };
17357aeedac0SVignesh R 
17367aeedac0SVignesh R enum spi_nor_pp_command_index {
17377aeedac0SVignesh R 	SNOR_CMD_PP,
17387aeedac0SVignesh R 
17397aeedac0SVignesh R 	/* Quad SPI */
17407aeedac0SVignesh R 	SNOR_CMD_PP_1_1_4,
17417aeedac0SVignesh R 	SNOR_CMD_PP_1_4_4,
17427aeedac0SVignesh R 	SNOR_CMD_PP_4_4_4,
17437aeedac0SVignesh R 
17447aeedac0SVignesh R 	/* Octo SPI */
17457aeedac0SVignesh R 	SNOR_CMD_PP_1_1_8,
17467aeedac0SVignesh R 	SNOR_CMD_PP_1_8_8,
17477aeedac0SVignesh R 	SNOR_CMD_PP_8_8_8,
17487aeedac0SVignesh R 
17497aeedac0SVignesh R 	SNOR_CMD_PP_MAX
17507aeedac0SVignesh R };
17517aeedac0SVignesh R 
17527aeedac0SVignesh R struct spi_nor_flash_parameter {
17537aeedac0SVignesh R 	u64				size;
17547aeedac0SVignesh R 	u32				page_size;
17557aeedac0SVignesh R 
17567aeedac0SVignesh R 	struct spi_nor_hwcaps		hwcaps;
17577aeedac0SVignesh R 	struct spi_nor_read_command	reads[SNOR_CMD_READ_MAX];
17587aeedac0SVignesh R 	struct spi_nor_pp_command	page_programs[SNOR_CMD_PP_MAX];
17597aeedac0SVignesh R 
17607aeedac0SVignesh R 	int (*quad_enable)(struct spi_nor *nor);
17617aeedac0SVignesh R };
17627aeedac0SVignesh R 
1763cd800046SChin-Ting Kuo #ifdef CONFIG_SPI_FLASH_SPANSION
1764cd800046SChin-Ting Kuo /**
1765cd800046SChin-Ting Kuo  * spansion_quad_enable_volatile() - enable Quad I/O mode in volatile register.
1766cd800046SChin-Ting Kuo  * @nor:	pointer to a 'struct spi_nor'
1767cd800046SChin-Ting Kuo  *
1768cd800046SChin-Ting Kuo  * It is recommended to update volatile registers in the field application due
1769cd800046SChin-Ting Kuo  * to a risk of the non-volatile registers corruption by power interrupt. This
1770cd800046SChin-Ting Kuo  * function sets Quad Enable bit in CFR1 volatile.
1771cd800046SChin-Ting Kuo  *
1772cd800046SChin-Ting Kuo  * Return: 0 on success, -errno otherwise.
1773cd800046SChin-Ting Kuo  */
spansion_quad_enable_volatile(struct spi_nor * nor)1774cd800046SChin-Ting Kuo static int spansion_quad_enable_volatile(struct spi_nor *nor)
1775cd800046SChin-Ting Kuo {
1776eaad4c09SChin-Ting Kuo 	u32 addr = SPINOR_REG_ADDR_CFR1V;
1777eaad4c09SChin-Ting Kuo 
1778cd800046SChin-Ting Kuo 	u8 cr;
1779cd800046SChin-Ting Kuo 	int ret;
1780cd800046SChin-Ting Kuo 
1781cd800046SChin-Ting Kuo 	/* Check current Quad Enable bit value. */
1782eaad4c09SChin-Ting Kuo 	ret = spansion_read_any_reg(nor, addr, 0, &cr);
1783cd800046SChin-Ting Kuo 	if (ret < 0) {
1784cd800046SChin-Ting Kuo 		dev_dbg(nor->dev,
1785cd800046SChin-Ting Kuo 			"error while reading configuration register\n");
1786cd800046SChin-Ting Kuo 		return -EINVAL;
1787cd800046SChin-Ting Kuo 	}
1788cd800046SChin-Ting Kuo 
1789eaad4c09SChin-Ting Kuo 	if (cr & CR_QUAD_EN_SPAN)
1790cd800046SChin-Ting Kuo 		return 0;
1791cd800046SChin-Ting Kuo 
1792eaad4c09SChin-Ting Kuo 	cr |= CR_QUAD_EN_SPAN;
1793cd800046SChin-Ting Kuo 
1794cd800046SChin-Ting Kuo 	write_enable(nor);
1795cd800046SChin-Ting Kuo 
1796eaad4c09SChin-Ting Kuo 	ret = spansion_write_any_reg(nor, addr, cr);
1797cd800046SChin-Ting Kuo 
1798cd800046SChin-Ting Kuo 	if (ret < 0) {
1799cd800046SChin-Ting Kuo 		dev_dbg(nor->dev,
1800cd800046SChin-Ting Kuo 			"error while writing configuration register\n");
1801cd800046SChin-Ting Kuo 		return -EINVAL;
1802cd800046SChin-Ting Kuo 	}
1803cd800046SChin-Ting Kuo 
1804cd800046SChin-Ting Kuo 	/* Read back and check it. */
1805eaad4c09SChin-Ting Kuo 	ret = spansion_read_any_reg(nor, addr, 0, &cr);
1806eaad4c09SChin-Ting Kuo 	if (ret || !(cr & CR_QUAD_EN_SPAN)) {
1807cd800046SChin-Ting Kuo 		dev_dbg(nor->dev, "Spansion Quad bit not set\n");
1808cd800046SChin-Ting Kuo 		return -EINVAL;
1809cd800046SChin-Ting Kuo 	}
1810cd800046SChin-Ting Kuo 
1811cd800046SChin-Ting Kuo 	return 0;
1812cd800046SChin-Ting Kuo }
1813cd800046SChin-Ting Kuo #endif
1814cd800046SChin-Ting Kuo 
18157aeedac0SVignesh 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)18167aeedac0SVignesh R spi_nor_set_read_settings(struct spi_nor_read_command *read,
18177aeedac0SVignesh R 			  u8 num_mode_clocks,
18187aeedac0SVignesh R 			  u8 num_wait_states,
18197aeedac0SVignesh R 			  u8 opcode,
18207aeedac0SVignesh R 			  enum spi_nor_protocol proto)
18217aeedac0SVignesh R {
18227aeedac0SVignesh R 	read->num_mode_clocks = num_mode_clocks;
18237aeedac0SVignesh R 	read->num_wait_states = num_wait_states;
18247aeedac0SVignesh R 	read->opcode = opcode;
18257aeedac0SVignesh R 	read->proto = proto;
18267aeedac0SVignesh R }
18277aeedac0SVignesh R 
18287aeedac0SVignesh R static void
spi_nor_set_pp_settings(struct spi_nor_pp_command * pp,u8 opcode,enum spi_nor_protocol proto)18297aeedac0SVignesh R spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
18307aeedac0SVignesh R 			u8 opcode,
18317aeedac0SVignesh R 			enum spi_nor_protocol proto)
18327aeedac0SVignesh R {
18337aeedac0SVignesh R 	pp->opcode = opcode;
18347aeedac0SVignesh R 	pp->proto = proto;
18357aeedac0SVignesh R }
18367aeedac0SVignesh R 
18370c6f187cSVignesh R #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
18380c6f187cSVignesh R /*
18390c6f187cSVignesh R  * Serial Flash Discoverable Parameters (SFDP) parsing.
18400c6f187cSVignesh R  */
18410c6f187cSVignesh R 
18420c6f187cSVignesh R /**
18430c6f187cSVignesh R  * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
18440c6f187cSVignesh R  * @nor:	pointer to a 'struct spi_nor'
18450c6f187cSVignesh R  * @addr:	offset in the SFDP area to start reading data from
18460c6f187cSVignesh R  * @len:	number of bytes to read
18470c6f187cSVignesh R  * @buf:	buffer where the SFDP data are copied into (dma-safe memory)
18480c6f187cSVignesh R  *
18490c6f187cSVignesh R  * Whatever the actual numbers of bytes for address and dummy cycles are
18500c6f187cSVignesh R  * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
18510c6f187cSVignesh R  * followed by a 3-byte address and 8 dummy clock cycles.
18520c6f187cSVignesh R  *
18530c6f187cSVignesh R  * Return: 0 on success, -errno otherwise.
18540c6f187cSVignesh R  */
spi_nor_read_sfdp(struct spi_nor * nor,u32 addr,size_t len,void * buf)18550c6f187cSVignesh R static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
18560c6f187cSVignesh R 			     size_t len, void *buf)
18570c6f187cSVignesh R {
18580c6f187cSVignesh R 	u8 addr_width, read_opcode, read_dummy;
1859734f8860SChin-Ting Kuo 	enum spi_nor_protocol	read_proto;
18600c6f187cSVignesh R 	int ret;
18610c6f187cSVignesh R 
18620c6f187cSVignesh R 	read_opcode = nor->read_opcode;
18630c6f187cSVignesh R 	addr_width = nor->addr_width;
18640c6f187cSVignesh R 	read_dummy = nor->read_dummy;
1865734f8860SChin-Ting Kuo 	read_proto = nor->read_proto;
18660c6f187cSVignesh R 
18670c6f187cSVignesh R 	nor->read_opcode = SPINOR_OP_RDSFDP;
1868734f8860SChin-Ting Kuo 	nor->read_proto = SNOR_PROTO_1_1_1;
18690c6f187cSVignesh R 	nor->addr_width = 3;
18700c6f187cSVignesh R 	nor->read_dummy = 8;
18710c6f187cSVignesh R 
18720c6f187cSVignesh R 	while (len) {
18730c6f187cSVignesh R 		ret = nor->read(nor, addr, len, (u8 *)buf);
18740c6f187cSVignesh R 		if (!ret || ret > len) {
18750c6f187cSVignesh R 			ret = -EIO;
18760c6f187cSVignesh R 			goto read_err;
18770c6f187cSVignesh R 		}
18780c6f187cSVignesh R 		if (ret < 0)
18790c6f187cSVignesh R 			goto read_err;
18800c6f187cSVignesh R 
18810c6f187cSVignesh R 		buf += ret;
18820c6f187cSVignesh R 		addr += ret;
18830c6f187cSVignesh R 		len -= ret;
18840c6f187cSVignesh R 	}
18850c6f187cSVignesh R 	ret = 0;
18860c6f187cSVignesh R 
18870c6f187cSVignesh R read_err:
18880c6f187cSVignesh R 	nor->read_opcode = read_opcode;
18890c6f187cSVignesh R 	nor->addr_width = addr_width;
18900c6f187cSVignesh R 	nor->read_dummy = read_dummy;
1891734f8860SChin-Ting Kuo 	nor->read_proto = read_proto;
18920c6f187cSVignesh R 
18930c6f187cSVignesh R 	return ret;
18940c6f187cSVignesh R }
18950c6f187cSVignesh R 
18960c6f187cSVignesh R struct sfdp_parameter_header {
18970c6f187cSVignesh R 	u8		id_lsb;
18980c6f187cSVignesh R 	u8		minor;
18990c6f187cSVignesh R 	u8		major;
19000c6f187cSVignesh R 	u8		length; /* in double words */
19010c6f187cSVignesh R 	u8		parameter_table_pointer[3]; /* byte address */
19020c6f187cSVignesh R 	u8		id_msb;
19030c6f187cSVignesh R };
19040c6f187cSVignesh R 
19050c6f187cSVignesh R #define SFDP_PARAM_HEADER_ID(p)	(((p)->id_msb << 8) | (p)->id_lsb)
19060c6f187cSVignesh R #define SFDP_PARAM_HEADER_PTP(p) \
19070c6f187cSVignesh R 	(((p)->parameter_table_pointer[2] << 16) | \
19080c6f187cSVignesh R 	 ((p)->parameter_table_pointer[1] <<  8) | \
19090c6f187cSVignesh R 	 ((p)->parameter_table_pointer[0] <<  0))
19100c6f187cSVignesh R 
19110c6f187cSVignesh R #define SFDP_BFPT_ID		0xff00	/* Basic Flash Parameter Table */
19120c6f187cSVignesh R #define SFDP_SECTOR_MAP_ID	0xff81	/* Sector Map Table */
19130c6f187cSVignesh R 
19140c6f187cSVignesh R #define SFDP_SIGNATURE		0x50444653U
19150c6f187cSVignesh R #define SFDP_JESD216_MAJOR	1
19160c6f187cSVignesh R #define SFDP_JESD216_MINOR	0
19170c6f187cSVignesh R #define SFDP_JESD216A_MINOR	5
19180c6f187cSVignesh R #define SFDP_JESD216B_MINOR	6
19190c6f187cSVignesh R 
19200c6f187cSVignesh R struct sfdp_header {
19210c6f187cSVignesh R 	u32		signature; /* Ox50444653U <=> "SFDP" */
19220c6f187cSVignesh R 	u8		minor;
19230c6f187cSVignesh R 	u8		major;
19240c6f187cSVignesh R 	u8		nph; /* 0-base number of parameter headers */
19250c6f187cSVignesh R 	u8		unused;
19260c6f187cSVignesh R 
19270c6f187cSVignesh R 	/* Basic Flash Parameter Table. */
19280c6f187cSVignesh R 	struct sfdp_parameter_header	bfpt_header;
19290c6f187cSVignesh R };
19300c6f187cSVignesh R 
19310c6f187cSVignesh R /* Basic Flash Parameter Table */
19320c6f187cSVignesh R 
19330c6f187cSVignesh R /*
19340c6f187cSVignesh R  * JESD216 rev B defines a Basic Flash Parameter Table of 16 DWORDs.
19350c6f187cSVignesh R  * They are indexed from 1 but C arrays are indexed from 0.
19360c6f187cSVignesh R  */
19370c6f187cSVignesh R #define BFPT_DWORD(i)		((i) - 1)
19380c6f187cSVignesh R #define BFPT_DWORD_MAX		16
19390c6f187cSVignesh R 
19400c6f187cSVignesh R /* The first version of JESB216 defined only 9 DWORDs. */
19410c6f187cSVignesh R #define BFPT_DWORD_MAX_JESD216			9
19420c6f187cSVignesh R 
19430c6f187cSVignesh R /* 1st DWORD. */
19440c6f187cSVignesh R #define BFPT_DWORD1_FAST_READ_1_1_2		BIT(16)
19450c6f187cSVignesh R #define BFPT_DWORD1_ADDRESS_BYTES_MASK		GENMASK(18, 17)
19460c6f187cSVignesh R #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY	(0x0UL << 17)
19470c6f187cSVignesh R #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4	(0x1UL << 17)
19480c6f187cSVignesh R #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY	(0x2UL << 17)
19490c6f187cSVignesh R #define BFPT_DWORD1_DTR				BIT(19)
19500c6f187cSVignesh R #define BFPT_DWORD1_FAST_READ_1_2_2		BIT(20)
19510c6f187cSVignesh R #define BFPT_DWORD1_FAST_READ_1_4_4		BIT(21)
19520c6f187cSVignesh R #define BFPT_DWORD1_FAST_READ_1_1_4		BIT(22)
19530c6f187cSVignesh R 
19540c6f187cSVignesh R /* 5th DWORD. */
19550c6f187cSVignesh R #define BFPT_DWORD5_FAST_READ_2_2_2		BIT(0)
19560c6f187cSVignesh R #define BFPT_DWORD5_FAST_READ_4_4_4		BIT(4)
19570c6f187cSVignesh R 
19580c6f187cSVignesh R /* 11th DWORD. */
19590c6f187cSVignesh R #define BFPT_DWORD11_PAGE_SIZE_SHIFT		4
19600c6f187cSVignesh R #define BFPT_DWORD11_PAGE_SIZE_MASK		GENMASK(7, 4)
19610c6f187cSVignesh R 
19620c6f187cSVignesh R /* 15th DWORD. */
19630c6f187cSVignesh R 
19640c6f187cSVignesh R /*
19650c6f187cSVignesh R  * (from JESD216 rev B)
19660c6f187cSVignesh R  * Quad Enable Requirements (QER):
19670c6f187cSVignesh R  * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
19680c6f187cSVignesh R  *         reads based on instruction. DQ3/HOLD# functions are hold during
19690c6f187cSVignesh R  *         instruction phase.
19700c6f187cSVignesh R  * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
19710c6f187cSVignesh R  *         two data bytes where bit 1 of the second byte is one.
19720c6f187cSVignesh R  *         [...]
19730c6f187cSVignesh R  *         Writing only one byte to the status register has the side-effect of
19740c6f187cSVignesh R  *         clearing status register 2, including the QE bit. The 100b code is
19750c6f187cSVignesh R  *         used if writing one byte to the status register does not modify
19760c6f187cSVignesh R  *         status register 2.
19770c6f187cSVignesh R  * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
19780c6f187cSVignesh R  *         one data byte where bit 6 is one.
19790c6f187cSVignesh R  *         [...]
19800c6f187cSVignesh R  * - 011b: QE is bit 7 of status register 2. It is set via Write status
19810c6f187cSVignesh R  *         register 2 instruction 3Eh with one data byte where bit 7 is one.
19820c6f187cSVignesh R  *         [...]
19830c6f187cSVignesh R  *         The status register 2 is read using instruction 3Fh.
19840c6f187cSVignesh R  * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
19850c6f187cSVignesh R  *         two data bytes where bit 1 of the second byte is one.
19860c6f187cSVignesh R  *         [...]
19870c6f187cSVignesh R  *         In contrast to the 001b code, writing one byte to the status
19880c6f187cSVignesh R  *         register does not modify status register 2.
19890c6f187cSVignesh R  * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
19900c6f187cSVignesh R  *         Read Status instruction 05h. Status register2 is read using
19910c6f187cSVignesh R  *         instruction 35h. QE is set via Writ Status instruction 01h with
19920c6f187cSVignesh R  *         two data bytes where bit 1 of the second byte is one.
19930c6f187cSVignesh R  *         [...]
19940c6f187cSVignesh R  */
19950c6f187cSVignesh R #define BFPT_DWORD15_QER_MASK			GENMASK(22, 20)
19960c6f187cSVignesh R #define BFPT_DWORD15_QER_NONE			(0x0UL << 20) /* Micron */
19970c6f187cSVignesh R #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY		(0x1UL << 20)
19980c6f187cSVignesh R #define BFPT_DWORD15_QER_SR1_BIT6		(0x2UL << 20) /* Macronix */
19990c6f187cSVignesh R #define BFPT_DWORD15_QER_SR2_BIT7		(0x3UL << 20)
20000c6f187cSVignesh R #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD		(0x4UL << 20)
20010c6f187cSVignesh R #define BFPT_DWORD15_QER_SR2_BIT1		(0x5UL << 20) /* Spansion */
200299b119ffSChin-Ting Kuo #define BFPT_DWORD15_QER_NONE_111		(0x7UL << 20) /* Gigadevice */
20030c6f187cSVignesh R 
20040c6f187cSVignesh R struct sfdp_bfpt {
20050c6f187cSVignesh R 	u32	dwords[BFPT_DWORD_MAX];
20060c6f187cSVignesh R };
20070c6f187cSVignesh R 
20080c6f187cSVignesh R /* Fast Read settings. */
20090c6f187cSVignesh R 
20100c6f187cSVignesh R static void
spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command * read,u16 half,enum spi_nor_protocol proto)20110c6f187cSVignesh R spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
20120c6f187cSVignesh R 				    u16 half,
20130c6f187cSVignesh R 				    enum spi_nor_protocol proto)
20140c6f187cSVignesh R {
20150c6f187cSVignesh R 	read->num_mode_clocks = (half >> 5) & 0x07;
20160c6f187cSVignesh R 	read->num_wait_states = (half >> 0) & 0x1f;
20170c6f187cSVignesh R 	read->opcode = (half >> 8) & 0xff;
20180c6f187cSVignesh R 	read->proto = proto;
20190c6f187cSVignesh R }
20200c6f187cSVignesh R 
20210c6f187cSVignesh R struct sfdp_bfpt_read {
20220c6f187cSVignesh R 	/* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
20230c6f187cSVignesh R 	u32			hwcaps;
20240c6f187cSVignesh R 
20250c6f187cSVignesh R 	/*
20260c6f187cSVignesh R 	 * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
20270c6f187cSVignesh R 	 * whether the Fast Read x-y-z command is supported.
20280c6f187cSVignesh R 	 */
20290c6f187cSVignesh R 	u32			supported_dword;
20300c6f187cSVignesh R 	u32			supported_bit;
20310c6f187cSVignesh R 
20320c6f187cSVignesh R 	/*
20330c6f187cSVignesh R 	 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
20340c6f187cSVignesh R 	 * encodes the op code, the number of mode clocks and the number of wait
20350c6f187cSVignesh R 	 * states to be used by Fast Read x-y-z command.
20360c6f187cSVignesh R 	 */
20370c6f187cSVignesh R 	u32			settings_dword;
20380c6f187cSVignesh R 	u32			settings_shift;
20390c6f187cSVignesh R 
20400c6f187cSVignesh R 	/* The SPI protocol for this Fast Read x-y-z command. */
20410c6f187cSVignesh R 	enum spi_nor_protocol	proto;
20420c6f187cSVignesh R };
20430c6f187cSVignesh R 
20440c6f187cSVignesh R static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
20450c6f187cSVignesh R 	/* Fast Read 1-1-2 */
20460c6f187cSVignesh R 	{
20470c6f187cSVignesh R 		SNOR_HWCAPS_READ_1_1_2,
20480c6f187cSVignesh R 		BFPT_DWORD(1), BIT(16),	/* Supported bit */
20490c6f187cSVignesh R 		BFPT_DWORD(4), 0,	/* Settings */
20500c6f187cSVignesh R 		SNOR_PROTO_1_1_2,
20510c6f187cSVignesh R 	},
20520c6f187cSVignesh R 
20530c6f187cSVignesh R 	/* Fast Read 1-2-2 */
20540c6f187cSVignesh R 	{
20550c6f187cSVignesh R 		SNOR_HWCAPS_READ_1_2_2,
20560c6f187cSVignesh R 		BFPT_DWORD(1), BIT(20),	/* Supported bit */
20570c6f187cSVignesh R 		BFPT_DWORD(4), 16,	/* Settings */
20580c6f187cSVignesh R 		SNOR_PROTO_1_2_2,
20590c6f187cSVignesh R 	},
20600c6f187cSVignesh R 
20610c6f187cSVignesh R 	/* Fast Read 2-2-2 */
20620c6f187cSVignesh R 	{
20630c6f187cSVignesh R 		SNOR_HWCAPS_READ_2_2_2,
20640c6f187cSVignesh R 		BFPT_DWORD(5),  BIT(0),	/* Supported bit */
20650c6f187cSVignesh R 		BFPT_DWORD(6), 16,	/* Settings */
20660c6f187cSVignesh R 		SNOR_PROTO_2_2_2,
20670c6f187cSVignesh R 	},
20680c6f187cSVignesh R 
20690c6f187cSVignesh R 	/* Fast Read 1-1-4 */
20700c6f187cSVignesh R 	{
20710c6f187cSVignesh R 		SNOR_HWCAPS_READ_1_1_4,
20720c6f187cSVignesh R 		BFPT_DWORD(1), BIT(22),	/* Supported bit */
20730c6f187cSVignesh R 		BFPT_DWORD(3), 16,	/* Settings */
20740c6f187cSVignesh R 		SNOR_PROTO_1_1_4,
20750c6f187cSVignesh R 	},
20760c6f187cSVignesh R 
20770c6f187cSVignesh R 	/* Fast Read 1-4-4 */
20780c6f187cSVignesh R 	{
20790c6f187cSVignesh R 		SNOR_HWCAPS_READ_1_4_4,
20800c6f187cSVignesh R 		BFPT_DWORD(1), BIT(21),	/* Supported bit */
20810c6f187cSVignesh R 		BFPT_DWORD(3), 0,	/* Settings */
20820c6f187cSVignesh R 		SNOR_PROTO_1_4_4,
20830c6f187cSVignesh R 	},
20840c6f187cSVignesh R 
20850c6f187cSVignesh R 	/* Fast Read 4-4-4 */
20860c6f187cSVignesh R 	{
20870c6f187cSVignesh R 		SNOR_HWCAPS_READ_4_4_4,
20880c6f187cSVignesh R 		BFPT_DWORD(5), BIT(4),	/* Supported bit */
20890c6f187cSVignesh R 		BFPT_DWORD(7), 16,	/* Settings */
20900c6f187cSVignesh R 		SNOR_PROTO_4_4_4,
20910c6f187cSVignesh R 	},
20920c6f187cSVignesh R };
20930c6f187cSVignesh R 
20940c6f187cSVignesh R struct sfdp_bfpt_erase {
20950c6f187cSVignesh R 	/*
20960c6f187cSVignesh R 	 * The half-word at offset <shift> in DWORD <dwoard> encodes the
20970c6f187cSVignesh R 	 * op code and erase sector size to be used by Sector Erase commands.
20980c6f187cSVignesh R 	 */
20990c6f187cSVignesh R 	u32			dword;
21000c6f187cSVignesh R 	u32			shift;
21010c6f187cSVignesh R };
21020c6f187cSVignesh R 
21030c6f187cSVignesh R static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
21040c6f187cSVignesh R 	/* Erase Type 1 in DWORD8 bits[15:0] */
21050c6f187cSVignesh R 	{BFPT_DWORD(8), 0},
21060c6f187cSVignesh R 
21070c6f187cSVignesh R 	/* Erase Type 2 in DWORD8 bits[31:16] */
21080c6f187cSVignesh R 	{BFPT_DWORD(8), 16},
21090c6f187cSVignesh R 
21100c6f187cSVignesh R 	/* Erase Type 3 in DWORD9 bits[15:0] */
21110c6f187cSVignesh R 	{BFPT_DWORD(9), 0},
21120c6f187cSVignesh R 
21130c6f187cSVignesh R 	/* Erase Type 4 in DWORD9 bits[31:16] */
21140c6f187cSVignesh R 	{BFPT_DWORD(9), 16},
21150c6f187cSVignesh R };
21160c6f187cSVignesh R 
21170c6f187cSVignesh R static int spi_nor_hwcaps_read2cmd(u32 hwcaps);
21180c6f187cSVignesh R 
21190c6f187cSVignesh R /**
21200c6f187cSVignesh R  * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
21210c6f187cSVignesh R  * @nor:		pointer to a 'struct spi_nor'
21220c6f187cSVignesh R  * @bfpt_header:	pointer to the 'struct sfdp_parameter_header' describing
21230c6f187cSVignesh R  *			the Basic Flash Parameter Table length and version
21240c6f187cSVignesh R  * @params:		pointer to the 'struct spi_nor_flash_parameter' to be
21250c6f187cSVignesh R  *			filled
21260c6f187cSVignesh R  *
21270c6f187cSVignesh R  * The Basic Flash Parameter Table is the main and only mandatory table as
21280c6f187cSVignesh R  * defined by the SFDP (JESD216) specification.
21290c6f187cSVignesh R  * It provides us with the total size (memory density) of the data array and
21300c6f187cSVignesh R  * the number of address bytes for Fast Read, Page Program and Sector Erase
21310c6f187cSVignesh R  * commands.
21320c6f187cSVignesh R  * For Fast READ commands, it also gives the number of mode clock cycles and
21330c6f187cSVignesh R  * wait states (regrouped in the number of dummy clock cycles) for each
21340c6f187cSVignesh R  * supported instruction op code.
21350c6f187cSVignesh R  * For Page Program, the page size is now available since JESD216 rev A, however
21360c6f187cSVignesh R  * the supported instruction op codes are still not provided.
21370c6f187cSVignesh R  * For Sector Erase commands, this table stores the supported instruction op
21380c6f187cSVignesh R  * codes and the associated sector sizes.
21390c6f187cSVignesh R  * Finally, the Quad Enable Requirements (QER) are also available since JESD216
21400c6f187cSVignesh R  * rev A. The QER bits encode the manufacturer dependent procedure to be
21410c6f187cSVignesh R  * executed to set the Quad Enable (QE) bit in some internal register of the
21420c6f187cSVignesh R  * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
21430c6f187cSVignesh R  * sending any Quad SPI command to the memory. Actually, setting the QE bit
21440c6f187cSVignesh R  * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
21450c6f187cSVignesh R  * and IO3 hence enabling 4 (Quad) I/O lines.
21460c6f187cSVignesh R  *
21470c6f187cSVignesh R  * Return: 0 on success, -errno otherwise.
21480c6f187cSVignesh R  */
spi_nor_parse_bfpt(struct spi_nor * nor,const struct sfdp_parameter_header * bfpt_header,struct spi_nor_flash_parameter * params)21490c6f187cSVignesh R static int spi_nor_parse_bfpt(struct spi_nor *nor,
21500c6f187cSVignesh R 			      const struct sfdp_parameter_header *bfpt_header,
21510c6f187cSVignesh R 			      struct spi_nor_flash_parameter *params)
21520c6f187cSVignesh R {
21530c6f187cSVignesh R 	struct mtd_info *mtd = &nor->mtd;
21540c6f187cSVignesh R 	struct sfdp_bfpt bfpt;
21550c6f187cSVignesh R 	size_t len;
21560c6f187cSVignesh R 	int i, cmd, err;
21570c6f187cSVignesh R 	u32 addr;
21580c6f187cSVignesh R 	u16 half;
21590c6f187cSVignesh R 
21600c6f187cSVignesh R 	/* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
21610c6f187cSVignesh R 	if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
21620c6f187cSVignesh R 		return -EINVAL;
21630c6f187cSVignesh R 
21640c6f187cSVignesh R 	/* Read the Basic Flash Parameter Table. */
21650c6f187cSVignesh R 	len = min_t(size_t, sizeof(bfpt),
21660c6f187cSVignesh R 		    bfpt_header->length * sizeof(u32));
21670c6f187cSVignesh R 	addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
21680c6f187cSVignesh R 	memset(&bfpt, 0, sizeof(bfpt));
21690c6f187cSVignesh R 	err = spi_nor_read_sfdp(nor,  addr, len, &bfpt);
21700c6f187cSVignesh R 	if (err < 0)
21710c6f187cSVignesh R 		return err;
21720c6f187cSVignesh R 
21730c6f187cSVignesh R 	/* Fix endianness of the BFPT DWORDs. */
21740c6f187cSVignesh R 	for (i = 0; i < BFPT_DWORD_MAX; i++)
21750c6f187cSVignesh R 		bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]);
21760c6f187cSVignesh R 
21770c6f187cSVignesh R 	/* Number of address bytes. */
21780c6f187cSVignesh R 	switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
21790c6f187cSVignesh R 	case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
21800c6f187cSVignesh R 		nor->addr_width = 3;
21810c6f187cSVignesh R 		break;
21820c6f187cSVignesh R 
21830c6f187cSVignesh R 	case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
21840c6f187cSVignesh R 		nor->addr_width = 4;
21850c6f187cSVignesh R 		break;
21860c6f187cSVignesh R 
21870c6f187cSVignesh R 	default:
21880c6f187cSVignesh R 		break;
21890c6f187cSVignesh R 	}
21900c6f187cSVignesh R 
21910c6f187cSVignesh R 	/* Flash Memory Density (in bits). */
21920c6f187cSVignesh R 	params->size = bfpt.dwords[BFPT_DWORD(2)];
21930c6f187cSVignesh R 	if (params->size & BIT(31)) {
21940c6f187cSVignesh R 		params->size &= ~BIT(31);
21950c6f187cSVignesh R 
21960c6f187cSVignesh R 		/*
21970c6f187cSVignesh R 		 * Prevent overflows on params->size. Anyway, a NOR of 2^64
21980c6f187cSVignesh R 		 * bits is unlikely to exist so this error probably means
21990c6f187cSVignesh R 		 * the BFPT we are reading is corrupted/wrong.
22000c6f187cSVignesh R 		 */
22010c6f187cSVignesh R 		if (params->size > 63)
22020c6f187cSVignesh R 			return -EINVAL;
22030c6f187cSVignesh R 
22040c6f187cSVignesh R 		params->size = 1ULL << params->size;
22050c6f187cSVignesh R 	} else {
22060c6f187cSVignesh R 		params->size++;
22070c6f187cSVignesh R 	}
22080c6f187cSVignesh R 	params->size >>= 3; /* Convert to bytes. */
22090c6f187cSVignesh R 
22100c6f187cSVignesh R 	/* Fast Read settings. */
22110c6f187cSVignesh R 	for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
22120c6f187cSVignesh R 		const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
22130c6f187cSVignesh R 		struct spi_nor_read_command *read;
22140c6f187cSVignesh R 
22150c6f187cSVignesh R 		if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
22160c6f187cSVignesh R 			params->hwcaps.mask &= ~rd->hwcaps;
22170c6f187cSVignesh R 			continue;
22180c6f187cSVignesh R 		}
22190c6f187cSVignesh R 
22200c6f187cSVignesh R 		params->hwcaps.mask |= rd->hwcaps;
22210c6f187cSVignesh R 		cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
22220c6f187cSVignesh R 		read = &params->reads[cmd];
22230c6f187cSVignesh R 		half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
22240c6f187cSVignesh R 		spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
22250c6f187cSVignesh R 	}
22260c6f187cSVignesh R 
22270c6f187cSVignesh R 	/* Sector Erase settings. */
22280c6f187cSVignesh R 	for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
22290c6f187cSVignesh R 		const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
22300c6f187cSVignesh R 		u32 erasesize;
22310c6f187cSVignesh R 		u8 opcode;
22320c6f187cSVignesh R 
22330c6f187cSVignesh R 		half = bfpt.dwords[er->dword] >> er->shift;
22340c6f187cSVignesh R 		erasesize = half & 0xff;
22350c6f187cSVignesh R 
22360c6f187cSVignesh R 		/* erasesize == 0 means this Erase Type is not supported. */
22370c6f187cSVignesh R 		if (!erasesize)
22380c6f187cSVignesh R 			continue;
22390c6f187cSVignesh R 
22400c6f187cSVignesh R 		erasesize = 1U << erasesize;
22410c6f187cSVignesh R 		opcode = (half >> 8) & 0xff;
22420c6f187cSVignesh R #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
22430c6f187cSVignesh R 		if (erasesize == SZ_4K) {
22440c6f187cSVignesh R 			nor->erase_opcode = opcode;
22450c6f187cSVignesh R 			mtd->erasesize = erasesize;
22460c6f187cSVignesh R 			break;
22470c6f187cSVignesh R 		}
22480c6f187cSVignesh R #endif
22490c6f187cSVignesh R 		if (!mtd->erasesize || mtd->erasesize < erasesize) {
22500c6f187cSVignesh R 			nor->erase_opcode = opcode;
22510c6f187cSVignesh R 			mtd->erasesize = erasesize;
22520c6f187cSVignesh R 		}
22530c6f187cSVignesh R 	}
22540c6f187cSVignesh R 
22550c6f187cSVignesh R 	/* Stop here if not JESD216 rev A or later. */
22560c6f187cSVignesh R 	if (bfpt_header->length < BFPT_DWORD_MAX)
22570c6f187cSVignesh R 		return 0;
22580c6f187cSVignesh R 
22590c6f187cSVignesh R 	/* Page size: this field specifies 'N' so the page size = 2^N bytes. */
22600c6f187cSVignesh R 	params->page_size = bfpt.dwords[BFPT_DWORD(11)];
22610c6f187cSVignesh R 	params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK;
22620c6f187cSVignesh R 	params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
22630c6f187cSVignesh R 	params->page_size = 1U << params->page_size;
22640c6f187cSVignesh R 
22650c6f187cSVignesh R 	/* Quad Enable Requirements. */
22660c6f187cSVignesh R 	switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
22670c6f187cSVignesh R 	case BFPT_DWORD15_QER_NONE:
2268838fb1e6SChin-Ting Kuo 		params->quad_enable = sst_cr_quad_enable;
2269838fb1e6SChin-Ting Kuo 		break;
227099b119ffSChin-Ting Kuo 	case BFPT_DWORD15_QER_NONE_111:
22710c6f187cSVignesh R 		params->quad_enable = NULL;
22720c6f187cSVignesh R 		break;
22730c6f187cSVignesh R #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
22740c6f187cSVignesh R 	case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
22750c6f187cSVignesh R 	case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
22760c6f187cSVignesh R 		params->quad_enable = spansion_no_read_cr_quad_enable;
22770c6f187cSVignesh R 		break;
22780c6f187cSVignesh R #endif
22790c6f187cSVignesh R #ifdef CONFIG_SPI_FLASH_MACRONIX
22800c6f187cSVignesh R 	case BFPT_DWORD15_QER_SR1_BIT6:
22810c6f187cSVignesh R 		params->quad_enable = macronix_quad_enable;
22820c6f187cSVignesh R 		break;
22830c6f187cSVignesh R #endif
22840c6f187cSVignesh R #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
22850c6f187cSVignesh R 	case BFPT_DWORD15_QER_SR2_BIT1:
22860c6f187cSVignesh R 		params->quad_enable = spansion_read_cr_quad_enable;
22870c6f187cSVignesh R 		break;
22880c6f187cSVignesh R #endif
22890c6f187cSVignesh R 	default:
22900c6f187cSVignesh R 		return -EINVAL;
22910c6f187cSVignesh R 	}
22920c6f187cSVignesh R 
22930c6f187cSVignesh R 	return 0;
22940c6f187cSVignesh R }
22950c6f187cSVignesh R 
22960c6f187cSVignesh R /**
22970c6f187cSVignesh R  * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
22980c6f187cSVignesh R  * @nor:		pointer to a 'struct spi_nor'
22990c6f187cSVignesh R  * @params:		pointer to the 'struct spi_nor_flash_parameter' to be
23000c6f187cSVignesh R  *			filled
23010c6f187cSVignesh R  *
23020c6f187cSVignesh R  * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
23030c6f187cSVignesh R  * specification. This is a standard which tends to supported by almost all
23040c6f187cSVignesh R  * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
23050c6f187cSVignesh R  * runtime the main parameters needed to perform basic SPI flash operations such
23060c6f187cSVignesh R  * as Fast Read, Page Program or Sector Erase commands.
23070c6f187cSVignesh R  *
23080c6f187cSVignesh R  * Return: 0 on success, -errno otherwise.
23090c6f187cSVignesh R  */
spi_nor_parse_sfdp(struct spi_nor * nor,struct spi_nor_flash_parameter * params)23100c6f187cSVignesh R static int spi_nor_parse_sfdp(struct spi_nor *nor,
23110c6f187cSVignesh R 			      struct spi_nor_flash_parameter *params)
23120c6f187cSVignesh R {
23130c6f187cSVignesh R 	const struct sfdp_parameter_header *param_header, *bfpt_header;
23140c6f187cSVignesh R 	struct sfdp_parameter_header *param_headers = NULL;
23150c6f187cSVignesh R 	struct sfdp_header header;
23160c6f187cSVignesh R 	size_t psize;
23170c6f187cSVignesh R 	int i, err;
23180c6f187cSVignesh R 
23190c6f187cSVignesh R 	/* Get the SFDP header. */
23200c6f187cSVignesh R 	err = spi_nor_read_sfdp(nor, 0, sizeof(header), &header);
23210c6f187cSVignesh R 	if (err < 0)
23220c6f187cSVignesh R 		return err;
23230c6f187cSVignesh R 
23240c6f187cSVignesh R 	/* Check the SFDP header version. */
23250c6f187cSVignesh R 	if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
23260c6f187cSVignesh R 	    header.major != SFDP_JESD216_MAJOR)
23270c6f187cSVignesh R 		return -EINVAL;
23280c6f187cSVignesh R 
23290c6f187cSVignesh R 	/*
23300c6f187cSVignesh R 	 * Verify that the first and only mandatory parameter header is a
23310c6f187cSVignesh R 	 * Basic Flash Parameter Table header as specified in JESD216.
23320c6f187cSVignesh R 	 */
23330c6f187cSVignesh R 	bfpt_header = &header.bfpt_header;
23340c6f187cSVignesh R 	if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
23350c6f187cSVignesh R 	    bfpt_header->major != SFDP_JESD216_MAJOR)
23360c6f187cSVignesh R 		return -EINVAL;
23370c6f187cSVignesh R 
23380c6f187cSVignesh R 	/*
23390c6f187cSVignesh R 	 * Allocate memory then read all parameter headers with a single
23400c6f187cSVignesh R 	 * Read SFDP command. These parameter headers will actually be parsed
23410c6f187cSVignesh R 	 * twice: a first time to get the latest revision of the basic flash
23420c6f187cSVignesh R 	 * parameter table, then a second time to handle the supported optional
23430c6f187cSVignesh R 	 * tables.
23440c6f187cSVignesh R 	 * Hence we read the parameter headers once for all to reduce the
23450c6f187cSVignesh R 	 * processing time. Also we use kmalloc() instead of devm_kmalloc()
23460c6f187cSVignesh R 	 * because we don't need to keep these parameter headers: the allocated
23470c6f187cSVignesh R 	 * memory is always released with kfree() before exiting this function.
23480c6f187cSVignesh R 	 */
23490c6f187cSVignesh R 	if (header.nph) {
23500c6f187cSVignesh R 		psize = header.nph * sizeof(*param_headers);
23510c6f187cSVignesh R 
23520c6f187cSVignesh R 		param_headers = kmalloc(psize, GFP_KERNEL);
23530c6f187cSVignesh R 		if (!param_headers)
23540c6f187cSVignesh R 			return -ENOMEM;
23550c6f187cSVignesh R 
23560c6f187cSVignesh R 		err = spi_nor_read_sfdp(nor, sizeof(header),
23570c6f187cSVignesh R 					psize, param_headers);
23580c6f187cSVignesh R 		if (err < 0) {
23590c6f187cSVignesh R 			dev_err(dev, "failed to read SFDP parameter headers\n");
23600c6f187cSVignesh R 			goto exit;
23610c6f187cSVignesh R 		}
23620c6f187cSVignesh R 	}
23630c6f187cSVignesh R 
23640c6f187cSVignesh R 	/*
23650c6f187cSVignesh R 	 * Check other parameter headers to get the latest revision of
23660c6f187cSVignesh R 	 * the basic flash parameter table.
23670c6f187cSVignesh R 	 */
23680c6f187cSVignesh R 	for (i = 0; i < header.nph; i++) {
23690c6f187cSVignesh R 		param_header = &param_headers[i];
23700c6f187cSVignesh R 
23710c6f187cSVignesh R 		if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
23720c6f187cSVignesh R 		    param_header->major == SFDP_JESD216_MAJOR &&
23730c6f187cSVignesh R 		    (param_header->minor > bfpt_header->minor ||
23740c6f187cSVignesh R 		     (param_header->minor == bfpt_header->minor &&
23750c6f187cSVignesh R 		      param_header->length > bfpt_header->length)))
23760c6f187cSVignesh R 			bfpt_header = param_header;
23770c6f187cSVignesh R 	}
23780c6f187cSVignesh R 
23790c6f187cSVignesh R 	err = spi_nor_parse_bfpt(nor, bfpt_header, params);
23800c6f187cSVignesh R 	if (err)
23810c6f187cSVignesh R 		goto exit;
23820c6f187cSVignesh R 
23830c6f187cSVignesh R 	/* Parse other parameter headers. */
23840c6f187cSVignesh R 	for (i = 0; i < header.nph; i++) {
23850c6f187cSVignesh R 		param_header = &param_headers[i];
23860c6f187cSVignesh R 
23870c6f187cSVignesh R 		switch (SFDP_PARAM_HEADER_ID(param_header)) {
23880c6f187cSVignesh R 		case SFDP_SECTOR_MAP_ID:
23890c6f187cSVignesh R 			dev_info(dev, "non-uniform erase sector maps are not supported yet.\n");
23900c6f187cSVignesh R 			break;
23910c6f187cSVignesh R 
23920c6f187cSVignesh R 		default:
23930c6f187cSVignesh R 			break;
23940c6f187cSVignesh R 		}
23950c6f187cSVignesh R 
23960c6f187cSVignesh R 		if (err)
23970c6f187cSVignesh R 			goto exit;
23980c6f187cSVignesh R 	}
23990c6f187cSVignesh R 
24000c6f187cSVignesh R exit:
24010c6f187cSVignesh R 	kfree(param_headers);
24020c6f187cSVignesh R 	return err;
24030c6f187cSVignesh R }
24040c6f187cSVignesh R #else
spi_nor_parse_sfdp(struct spi_nor * nor,struct spi_nor_flash_parameter * params)24050c6f187cSVignesh R static int spi_nor_parse_sfdp(struct spi_nor *nor,
24060c6f187cSVignesh R 			      struct spi_nor_flash_parameter *params)
24070c6f187cSVignesh R {
24080c6f187cSVignesh R 	return -EINVAL;
24090c6f187cSVignesh R }
24100c6f187cSVignesh R #endif /* SPI_FLASH_SFDP_SUPPORT */
24110c6f187cSVignesh R 
spi_nor_init_params(struct spi_nor * nor,const struct flash_info * info,struct spi_nor_flash_parameter * params)24127aeedac0SVignesh R static int spi_nor_init_params(struct spi_nor *nor,
24137aeedac0SVignesh R 			       const struct flash_info *info,
24147aeedac0SVignesh R 			       struct spi_nor_flash_parameter *params)
24157aeedac0SVignesh R {
2416eaad4c09SChin-Ting Kuo 	int ret;
24177aeedac0SVignesh R 	/* Set legacy flash parameters as default. */
24187aeedac0SVignesh R 	memset(params, 0, sizeof(*params));
24197aeedac0SVignesh R 
24207aeedac0SVignesh R 	/* Set SPI NOR sizes. */
24217aeedac0SVignesh R 	params->size = info->sector_size * info->n_sectors;
24227aeedac0SVignesh R 	params->page_size = info->page_size;
24237aeedac0SVignesh R 
24247aeedac0SVignesh R 	/* (Fast) Read settings. */
24257aeedac0SVignesh R 	params->hwcaps.mask |= SNOR_HWCAPS_READ;
24267aeedac0SVignesh R 	spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
24277aeedac0SVignesh R 				  0, 0, SPINOR_OP_READ,
24287aeedac0SVignesh R 				  SNOR_PROTO_1_1_1);
24297aeedac0SVignesh R 
24307aeedac0SVignesh R 	if (!(info->flags & SPI_NOR_NO_FR)) {
24317aeedac0SVignesh R 		params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
24327aeedac0SVignesh R 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
24337aeedac0SVignesh R 					  0, 8, SPINOR_OP_READ_FAST,
24347aeedac0SVignesh R 					  SNOR_PROTO_1_1_1);
2435e6fafa05SChin-Ting Kuo #ifdef CONFIG_SPI_FLASH_SPANSION
2436e6fafa05SChin-Ting Kuo 		if (cypress_s25hx_t(info))
2437e6fafa05SChin-Ting Kuo 			params->reads[SNOR_CMD_READ_FAST].num_mode_clocks = 8;
2438e6fafa05SChin-Ting Kuo #endif
24397aeedac0SVignesh R 	}
24407aeedac0SVignesh R 
24417aeedac0SVignesh R 	if (info->flags & SPI_NOR_DUAL_READ) {
24427aeedac0SVignesh R 		params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
24437aeedac0SVignesh R 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_2],
24447aeedac0SVignesh R 					  0, 8, SPINOR_OP_READ_1_1_2,
24457aeedac0SVignesh R 					  SNOR_PROTO_1_1_2);
24467aeedac0SVignesh R 	}
24477aeedac0SVignesh R 
24487aeedac0SVignesh R 	if (info->flags & SPI_NOR_QUAD_READ) {
24497aeedac0SVignesh R 		params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
24507aeedac0SVignesh R 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
24517aeedac0SVignesh R 					  0, 8, SPINOR_OP_READ_1_1_4,
24527aeedac0SVignesh R 					  SNOR_PROTO_1_1_4);
24537aeedac0SVignesh R 	}
24547aeedac0SVignesh R 
24557aeedac0SVignesh R 	/* Page Program settings. */
24567aeedac0SVignesh R 	params->hwcaps.mask |= SNOR_HWCAPS_PP;
24577aeedac0SVignesh R 	spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP],
24587aeedac0SVignesh R 				SPINOR_OP_PP, SNOR_PROTO_1_1_1);
24597aeedac0SVignesh R 
24607aeedac0SVignesh R 	if (info->flags & SPI_NOR_QUAD_READ) {
24617aeedac0SVignesh R 		params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
24627aeedac0SVignesh R 		spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_1_1_4],
24637aeedac0SVignesh R 					SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4);
24647aeedac0SVignesh R 	}
24657aeedac0SVignesh R 
24667aeedac0SVignesh R 	/* Select the procedure to set the Quad Enable bit. */
24677aeedac0SVignesh R 	if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
24687aeedac0SVignesh R 				   SNOR_HWCAPS_PP_QUAD)) {
24697aeedac0SVignesh R 		switch (JEDEC_MFR(info)) {
24707aeedac0SVignesh R #ifdef CONFIG_SPI_FLASH_MACRONIX
24717aeedac0SVignesh R 		case SNOR_MFR_MACRONIX:
24727aeedac0SVignesh R 			params->quad_enable = macronix_quad_enable;
24737aeedac0SVignesh R 			break;
24747aeedac0SVignesh R #endif
24757aeedac0SVignesh R 		case SNOR_MFR_ST:
24767aeedac0SVignesh R 		case SNOR_MFR_MICRON:
247700554b9bSChin-Ting Kuo 		case SNOR_MFR_ISSI:
24787aeedac0SVignesh R 			break;
2479cd800046SChin-Ting Kuo #ifdef CONFIG_SPI_FLASH_SPANSION
2480cd800046SChin-Ting Kuo 		case SNOR_MFR_CYPRESS:
2481cd800046SChin-Ting Kuo 			if (info->id[1] == 0x2a || info->id[1] == 0x2b) {
2482cd800046SChin-Ting Kuo 				params->quad_enable = spansion_quad_enable_volatile;
2483cd800046SChin-Ting Kuo 			}
2484cd800046SChin-Ting Kuo 			break;
2485cd800046SChin-Ting Kuo #endif
24867aeedac0SVignesh R 
24877aeedac0SVignesh R 		default:
24887aeedac0SVignesh R #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
24897aeedac0SVignesh R 			/* Kept only for backward compatibility purpose. */
24907aeedac0SVignesh R 			params->quad_enable = spansion_read_cr_quad_enable;
24917aeedac0SVignesh R #endif
24927aeedac0SVignesh R 			break;
24937aeedac0SVignesh R 		}
24947aeedac0SVignesh R 	}
24950c6f187cSVignesh R 
24960c6f187cSVignesh R 	/* Override the parameters with data read from SFDP tables. */
24970c6f187cSVignesh R 	nor->addr_width = 0;
24980c6f187cSVignesh R 	nor->mtd.erasesize = 0;
24990c6f187cSVignesh R 	if ((info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) &&
25000c6f187cSVignesh R 	    !(info->flags & SPI_NOR_SKIP_SFDP)) {
25010c6f187cSVignesh R 		struct spi_nor_flash_parameter sfdp_params;
25020c6f187cSVignesh R 
25030c6f187cSVignesh R 		memcpy(&sfdp_params, params, sizeof(sfdp_params));
25040c6f187cSVignesh R 		if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
25050c6f187cSVignesh R 			nor->addr_width = 0;
25060c6f187cSVignesh R 			nor->mtd.erasesize = 0;
25070c6f187cSVignesh R 		} else {
25080c6f187cSVignesh R 			memcpy(params, &sfdp_params, sizeof(*params));
2509cd800046SChin-Ting Kuo #ifdef CONFIG_SPI_FLASH_SPANSION
2510cd800046SChin-Ting Kuo 			if (cypress_s25hx_t(info)) {
2511eaad4c09SChin-Ting Kuo 				/* BFPT fixup */
2512eaad4c09SChin-Ting Kuo 				nor->erase_opcode = SPINOR_OP_SE_4B;
2513eaad4c09SChin-Ting Kuo 				nor->mtd.erasesize = info->sector_size;
2514eaad4c09SChin-Ting Kuo 				ret = set_4byte(nor, info, 1);
2515eaad4c09SChin-Ting Kuo 				if (ret)
2516eaad4c09SChin-Ting Kuo 					return ret;
2517eaad4c09SChin-Ting Kuo 
2518eaad4c09SChin-Ting Kuo 				nor->addr_width = 4;
2519eaad4c09SChin-Ting Kuo 
2520eaad4c09SChin-Ting Kuo 				/* SFDP fixup */
2521cd800046SChin-Ting Kuo 				/* Default page size is 256-byte, but BFPT reports 512-byte */
2522cd800046SChin-Ting Kuo 				params->page_size = 256;
2523cd800046SChin-Ting Kuo 				/* READ_FAST_4B (0Ch) requires mode cycles*/
2524543e3ecbSChin-Ting Kuo 				params->reads[SNOR_CMD_READ_FAST].opcode = SPINOR_OP_READ_FAST_4B;
2525e6fafa05SChin-Ting Kuo 				params->reads[SNOR_CMD_READ_FAST].num_mode_clocks = 8;
2526cd800046SChin-Ting Kuo 				/* PP_1_1_4 is not supported */
2527cd800046SChin-Ting Kuo 				params->hwcaps.mask &= ~SNOR_HWCAPS_PP_1_1_4;
2528cd800046SChin-Ting Kuo 				/* Use volatile register to enable quad */
2529cd800046SChin-Ting Kuo 				params->quad_enable = spansion_quad_enable_volatile;
2530cd800046SChin-Ting Kuo 			}
2531cd800046SChin-Ting Kuo #endif
25320c6f187cSVignesh R 		}
2533a25e89c3SChin-Ting Kuo 
2534a25e89c3SChin-Ting Kuo 		/* need to disable hold/reset pin feature */
2535a25e89c3SChin-Ting Kuo 		if (JEDEC_MFR(info) == SNOR_MFR_ST)
2536a25e89c3SChin-Ting Kuo 			params->quad_enable = micron_read_cr_quad_enable;
2537878b2ba4SChin-Ting Kuo 
2538878b2ba4SChin-Ting Kuo 		if (JEDEC_MFR(info) == SNOR_MFR_GIGADEVICE)
2539878b2ba4SChin-Ting Kuo 			params->quad_enable = winbond_sr2_bit1_quad_enable;
25400c6f187cSVignesh R 	}
25410c6f187cSVignesh R 
25420c6f187cSVignesh R 	return 0;
25437aeedac0SVignesh R }
25447aeedac0SVignesh R 
spi_nor_hwcaps2cmd(u32 hwcaps,const int table[][2],size_t size)25457aeedac0SVignesh R static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
25467aeedac0SVignesh R {
25477aeedac0SVignesh R 	size_t i;
25487aeedac0SVignesh R 
25497aeedac0SVignesh R 	for (i = 0; i < size; i++)
25507aeedac0SVignesh R 		if (table[i][0] == (int)hwcaps)
25517aeedac0SVignesh R 			return table[i][1];
25527aeedac0SVignesh R 
25537aeedac0SVignesh R 	return -EINVAL;
25547aeedac0SVignesh R }
25557aeedac0SVignesh R 
spi_nor_hwcaps_read2cmd(u32 hwcaps)25567aeedac0SVignesh R static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
25577aeedac0SVignesh R {
25587aeedac0SVignesh R 	static const int hwcaps_read2cmd[][2] = {
25597aeedac0SVignesh R 		{ SNOR_HWCAPS_READ,		SNOR_CMD_READ },
25607aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_FAST,	SNOR_CMD_READ_FAST },
25617aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_1_1_1_DTR,	SNOR_CMD_READ_1_1_1_DTR },
25627aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_1_1_2,	SNOR_CMD_READ_1_1_2 },
25637aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_1_2_2,	SNOR_CMD_READ_1_2_2 },
25647aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_2_2_2,	SNOR_CMD_READ_2_2_2 },
25657aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_1_2_2_DTR,	SNOR_CMD_READ_1_2_2_DTR },
25667aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_1_1_4,	SNOR_CMD_READ_1_1_4 },
25677aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_1_4_4,	SNOR_CMD_READ_1_4_4 },
25687aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_4_4_4,	SNOR_CMD_READ_4_4_4 },
25697aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_1_4_4_DTR,	SNOR_CMD_READ_1_4_4_DTR },
25707aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_1_1_8,	SNOR_CMD_READ_1_1_8 },
25717aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_1_8_8,	SNOR_CMD_READ_1_8_8 },
25727aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_8_8_8,	SNOR_CMD_READ_8_8_8 },
25737aeedac0SVignesh R 		{ SNOR_HWCAPS_READ_1_8_8_DTR,	SNOR_CMD_READ_1_8_8_DTR },
25747aeedac0SVignesh R 	};
25757aeedac0SVignesh R 
25767aeedac0SVignesh R 	return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
25777aeedac0SVignesh R 				  ARRAY_SIZE(hwcaps_read2cmd));
25787aeedac0SVignesh R }
25797aeedac0SVignesh R 
spi_nor_hwcaps_pp2cmd(u32 hwcaps)25807aeedac0SVignesh R static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
25817aeedac0SVignesh R {
25827aeedac0SVignesh R 	static const int hwcaps_pp2cmd[][2] = {
25837aeedac0SVignesh R 		{ SNOR_HWCAPS_PP,		SNOR_CMD_PP },
25847aeedac0SVignesh R 		{ SNOR_HWCAPS_PP_1_1_4,		SNOR_CMD_PP_1_1_4 },
25857aeedac0SVignesh R 		{ SNOR_HWCAPS_PP_1_4_4,		SNOR_CMD_PP_1_4_4 },
25867aeedac0SVignesh R 		{ SNOR_HWCAPS_PP_4_4_4,		SNOR_CMD_PP_4_4_4 },
25877aeedac0SVignesh R 		{ SNOR_HWCAPS_PP_1_1_8,		SNOR_CMD_PP_1_1_8 },
25887aeedac0SVignesh R 		{ SNOR_HWCAPS_PP_1_8_8,		SNOR_CMD_PP_1_8_8 },
25897aeedac0SVignesh R 		{ SNOR_HWCAPS_PP_8_8_8,		SNOR_CMD_PP_8_8_8 },
25907aeedac0SVignesh R 	};
25917aeedac0SVignesh R 
25927aeedac0SVignesh R 	return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
25937aeedac0SVignesh R 				  ARRAY_SIZE(hwcaps_pp2cmd));
25947aeedac0SVignesh R }
25957aeedac0SVignesh R 
spi_nor_select_read(struct spi_nor * nor,const struct spi_nor_flash_parameter * params,u32 shared_hwcaps)25967aeedac0SVignesh R static int spi_nor_select_read(struct spi_nor *nor,
25977aeedac0SVignesh R 			       const struct spi_nor_flash_parameter *params,
25987aeedac0SVignesh R 			       u32 shared_hwcaps)
25997aeedac0SVignesh R {
26007aeedac0SVignesh R 	int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
26017aeedac0SVignesh R 	const struct spi_nor_read_command *read;
26027aeedac0SVignesh R 
26037aeedac0SVignesh R 	if (best_match < 0)
26047aeedac0SVignesh R 		return -EINVAL;
26057aeedac0SVignesh R 
26067aeedac0SVignesh R 	cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
26077aeedac0SVignesh R 	if (cmd < 0)
26087aeedac0SVignesh R 		return -EINVAL;
26097aeedac0SVignesh R 
26107aeedac0SVignesh R 	read = &params->reads[cmd];
26117aeedac0SVignesh R 	nor->read_opcode = read->opcode;
26127aeedac0SVignesh R 	nor->read_proto = read->proto;
26137aeedac0SVignesh R 
26147aeedac0SVignesh R 	/*
26157aeedac0SVignesh R 	 * In the spi-nor framework, we don't need to make the difference
26167aeedac0SVignesh R 	 * between mode clock cycles and wait state clock cycles.
26177aeedac0SVignesh R 	 * Indeed, the value of the mode clock cycles is used by a QSPI
26187aeedac0SVignesh R 	 * flash memory to know whether it should enter or leave its 0-4-4
26197aeedac0SVignesh R 	 * (Continuous Read / XIP) mode.
26207aeedac0SVignesh R 	 * eXecution In Place is out of the scope of the mtd sub-system.
26217aeedac0SVignesh R 	 * Hence we choose to merge both mode and wait state clock cycles
26227aeedac0SVignesh R 	 * into the so called dummy clock cycles.
26237aeedac0SVignesh R 	 */
26247aeedac0SVignesh R 	nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
26257aeedac0SVignesh R 	return 0;
26267aeedac0SVignesh R }
26277aeedac0SVignesh R 
spi_nor_select_pp(struct spi_nor * nor,const struct spi_nor_flash_parameter * params,u32 shared_hwcaps)26287aeedac0SVignesh R static int spi_nor_select_pp(struct spi_nor *nor,
26297aeedac0SVignesh R 			     const struct spi_nor_flash_parameter *params,
26307aeedac0SVignesh R 			     u32 shared_hwcaps)
26317aeedac0SVignesh R {
26327aeedac0SVignesh R 	int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
26337aeedac0SVignesh R 	const struct spi_nor_pp_command *pp;
26347aeedac0SVignesh R 
26357aeedac0SVignesh R 	if (best_match < 0)
26367aeedac0SVignesh R 		return -EINVAL;
26377aeedac0SVignesh R 
26387aeedac0SVignesh R 	cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
26397aeedac0SVignesh R 	if (cmd < 0)
26407aeedac0SVignesh R 		return -EINVAL;
26417aeedac0SVignesh R 
26427aeedac0SVignesh R 	pp = &params->page_programs[cmd];
26437aeedac0SVignesh R 	nor->program_opcode = pp->opcode;
26447aeedac0SVignesh R 	nor->write_proto = pp->proto;
26457aeedac0SVignesh R 	return 0;
26467aeedac0SVignesh R }
26477aeedac0SVignesh R 
spi_nor_select_erase(struct spi_nor * nor,const struct flash_info * info)26487aeedac0SVignesh R static int spi_nor_select_erase(struct spi_nor *nor,
26497aeedac0SVignesh R 				const struct flash_info *info)
26507aeedac0SVignesh R {
26517aeedac0SVignesh R 	struct mtd_info *mtd = &nor->mtd;
26527aeedac0SVignesh R 
26530c6f187cSVignesh R 	/* Do nothing if already configured from SFDP. */
26540c6f187cSVignesh R 	if (mtd->erasesize)
26550c6f187cSVignesh R 		return 0;
26560c6f187cSVignesh R 
26577aeedac0SVignesh R #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
26587aeedac0SVignesh R 	/* prefer "small sector" erase if possible */
26597aeedac0SVignesh R 	if (info->flags & SECT_4K) {
26607aeedac0SVignesh R 		nor->erase_opcode = SPINOR_OP_BE_4K;
26617aeedac0SVignesh R 		mtd->erasesize = 4096;
26627aeedac0SVignesh R 	} else if (info->flags & SECT_4K_PMC) {
26637aeedac0SVignesh R 		nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
26647aeedac0SVignesh R 		mtd->erasesize = 4096;
26657aeedac0SVignesh R 	} else
26667aeedac0SVignesh R #endif
26677aeedac0SVignesh R 	{
26687aeedac0SVignesh R 		nor->erase_opcode = SPINOR_OP_SE;
26697aeedac0SVignesh R 		mtd->erasesize = info->sector_size;
26707aeedac0SVignesh R 	}
26717aeedac0SVignesh R 	return 0;
26727aeedac0SVignesh R }
26737aeedac0SVignesh 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)26747aeedac0SVignesh R static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
26757aeedac0SVignesh R 			 const struct spi_nor_flash_parameter *params,
26767aeedac0SVignesh R 			 const struct spi_nor_hwcaps *hwcaps)
26777aeedac0SVignesh R {
26787aeedac0SVignesh R 	u32 ignored_mask, shared_mask;
26797aeedac0SVignesh R 	bool enable_quad_io;
26807aeedac0SVignesh R 	int err;
26817aeedac0SVignesh R 
26827aeedac0SVignesh R 	/*
26837aeedac0SVignesh R 	 * Keep only the hardware capabilities supported by both the SPI
26847aeedac0SVignesh R 	 * controller and the SPI flash memory.
26857aeedac0SVignesh R 	 */
26867aeedac0SVignesh R 	shared_mask = hwcaps->mask & params->hwcaps.mask;
26877aeedac0SVignesh R 
26887aeedac0SVignesh R 	/* SPI n-n-n protocols are not supported yet. */
2689734f8860SChin-Ting Kuo 	ignored_mask = (SNOR_HWCAPS_READ_1_1_1_DTR |
2690734f8860SChin-Ting Kuo 			SNOR_HWCAPS_READ_1_2_2 |
2691734f8860SChin-Ting Kuo 			SNOR_HWCAPS_READ_1_2_2_DTR |
2692734f8860SChin-Ting Kuo 			SNOR_HWCAPS_READ_2_2_2 |
2693734f8860SChin-Ting Kuo 			SNOR_HWCAPS_READ_1_4_4 |
2694734f8860SChin-Ting Kuo 			SNOR_HWCAPS_READ_1_4_4_DTR |
26957aeedac0SVignesh R 			SNOR_HWCAPS_READ_4_4_4 |
26967aeedac0SVignesh R 			SNOR_HWCAPS_READ_8_8_8 |
2697734f8860SChin-Ting Kuo 			SNOR_HWCAPS_PP_1_4_4 |
26987aeedac0SVignesh R 			SNOR_HWCAPS_PP_4_4_4 |
26997aeedac0SVignesh R 			SNOR_HWCAPS_PP_8_8_8);
27007aeedac0SVignesh R 	if (shared_mask & ignored_mask) {
27017aeedac0SVignesh R 		dev_dbg(nor->dev,
27027aeedac0SVignesh R 			"SPI n-n-n protocols are not supported yet.\n");
27037aeedac0SVignesh R 		shared_mask &= ~ignored_mask;
27047aeedac0SVignesh R 	}
27057aeedac0SVignesh R 
27067aeedac0SVignesh R 	/* Select the (Fast) Read command. */
27077aeedac0SVignesh R 	err = spi_nor_select_read(nor, params, shared_mask);
27087aeedac0SVignesh R 	if (err) {
27097aeedac0SVignesh R 		dev_dbg(nor->dev,
27107aeedac0SVignesh R 			"can't select read settings supported by both the SPI controller and memory.\n");
27117aeedac0SVignesh R 		return err;
27127aeedac0SVignesh R 	}
27137aeedac0SVignesh R 
27147aeedac0SVignesh R 	/* Select the Page Program command. */
27157aeedac0SVignesh R 	err = spi_nor_select_pp(nor, params, shared_mask);
27167aeedac0SVignesh R 	if (err) {
27177aeedac0SVignesh R 		dev_dbg(nor->dev,
27187aeedac0SVignesh R 			"can't select write settings supported by both the SPI controller and memory.\n");
27197aeedac0SVignesh R 		return err;
27207aeedac0SVignesh R 	}
27217aeedac0SVignesh R 
27227aeedac0SVignesh R 	/* Select the Sector Erase command. */
27237aeedac0SVignesh R 	err = spi_nor_select_erase(nor, info);
27247aeedac0SVignesh R 	if (err) {
27257aeedac0SVignesh R 		dev_dbg(nor->dev,
27267aeedac0SVignesh R 			"can't select erase settings supported by both the SPI controller and memory.\n");
27277aeedac0SVignesh R 		return err;
27287aeedac0SVignesh R 	}
27297aeedac0SVignesh R 
27307aeedac0SVignesh R 	/* Enable Quad I/O if needed. */
27317aeedac0SVignesh R 	enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
27327aeedac0SVignesh R 			  spi_nor_get_protocol_width(nor->write_proto) == 4);
27337aeedac0SVignesh R 	if (enable_quad_io && params->quad_enable)
27347aeedac0SVignesh R 		nor->quad_enable = params->quad_enable;
27357aeedac0SVignesh R 	else
27367aeedac0SVignesh R 		nor->quad_enable = NULL;
27377aeedac0SVignesh R 
27387aeedac0SVignesh R 	return 0;
27397aeedac0SVignesh R }
27407aeedac0SVignesh R 
spi_nor_init(struct spi_nor * nor)27417aeedac0SVignesh R static int spi_nor_init(struct spi_nor *nor)
27427aeedac0SVignesh R {
27437aeedac0SVignesh R 	int err;
27447aeedac0SVignesh R 
27457aeedac0SVignesh R 	/*
27467aeedac0SVignesh R 	 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
27477aeedac0SVignesh R 	 * with the software protection bits set
27487aeedac0SVignesh R 	 */
27497aeedac0SVignesh R 	if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
27507aeedac0SVignesh R 	    JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
27517aeedac0SVignesh R 	    JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
27527aeedac0SVignesh R 	    nor->info->flags & SPI_NOR_HAS_LOCK) {
27537aeedac0SVignesh R 		write_enable(nor);
27547aeedac0SVignesh R 		write_sr(nor, 0);
27557aeedac0SVignesh R 		spi_nor_wait_till_ready(nor);
27567aeedac0SVignesh R 	}
27577aeedac0SVignesh R 
27587aeedac0SVignesh R 	if (nor->quad_enable) {
27597aeedac0SVignesh R 		err = nor->quad_enable(nor);
27607aeedac0SVignesh R 		if (err) {
27617aeedac0SVignesh R 			dev_dbg(nor->dev, "quad mode not supported\n");
27627aeedac0SVignesh R 			return err;
27637aeedac0SVignesh R 		}
27647aeedac0SVignesh R 	}
27657aeedac0SVignesh R 
276661059bc5SVignesh R 	if (nor->addr_width == 4 &&
2767d32338fdSryan_chen 	    (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION)) {
2768cd800046SChin-Ting Kuo 
276961059bc5SVignesh R 		/*
277061059bc5SVignesh R 		 * If the RESET# pin isn't hooked up properly, or the system
277161059bc5SVignesh R 		 * otherwise doesn't perform a reset command in the boot
277261059bc5SVignesh R 		 * sequence, it's impossible to 100% protect against unexpected
277361059bc5SVignesh R 		 * reboots (e.g., crashes). Warn the user (or hopefully, system
277461059bc5SVignesh R 		 * designer) that this is bad.
277561059bc5SVignesh R 		 */
277661059bc5SVignesh R 		if (nor->flags & SNOR_F_BROKEN_RESET)
277761059bc5SVignesh R 			printf("enabling reset hack; may not recover from unexpected reboots\n");
277861059bc5SVignesh R 		set_4byte(nor, nor->info, 1);
277961059bc5SVignesh R 	}
278061059bc5SVignesh R 
27817aeedac0SVignesh R 	return 0;
27827aeedac0SVignesh R }
27837aeedac0SVignesh R 
spi_nor_scan(struct spi_nor * nor)27847aeedac0SVignesh R int spi_nor_scan(struct spi_nor *nor)
27857aeedac0SVignesh R {
27867aeedac0SVignesh R 	struct spi_nor_flash_parameter params;
27877aeedac0SVignesh R 	const struct flash_info *info = NULL;
27887aeedac0SVignesh R 	struct mtd_info *mtd = &nor->mtd;
27897aeedac0SVignesh R 	struct spi_nor_hwcaps hwcaps = {
27907aeedac0SVignesh R 		.mask = SNOR_HWCAPS_READ |
27917aeedac0SVignesh R 			SNOR_HWCAPS_READ_FAST |
27927aeedac0SVignesh R 			SNOR_HWCAPS_PP,
27937aeedac0SVignesh R 	};
27947aeedac0SVignesh R 	struct spi_slave *spi = nor->spi;
27957aeedac0SVignesh R 	int ret;
27967aeedac0SVignesh R 
27977aeedac0SVignesh R 	/* Reset SPI protocol for all commands. */
27987aeedac0SVignesh R 	nor->reg_proto = SNOR_PROTO_1_1_1;
27997aeedac0SVignesh R 	nor->read_proto = SNOR_PROTO_1_1_1;
28007aeedac0SVignesh R 	nor->write_proto = SNOR_PROTO_1_1_1;
28017aeedac0SVignesh R 	nor->read = spi_nor_read_data;
28027aeedac0SVignesh R 	nor->write = spi_nor_write_data;
28037aeedac0SVignesh R 	nor->read_reg = spi_nor_read_reg;
28047aeedac0SVignesh R 	nor->write_reg = spi_nor_write_reg;
2805*591e1cf0SChin-Ting Kuo 	nor->flash_lock_by_host_ctrl = spi_nor_wlock_by_host_ctrl;
2806*591e1cf0SChin-Ting Kuo 	nor->flash_unlock_by_host_ctrl = spi_nor_wunlock_by_host_ctrl;
28077aeedac0SVignesh R 
28087aeedac0SVignesh R 	if (spi->mode & SPI_RX_QUAD) {
28097aeedac0SVignesh R 		hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
28107aeedac0SVignesh R 
28117aeedac0SVignesh R 		if (spi->mode & SPI_TX_QUAD)
28127aeedac0SVignesh R 			hwcaps.mask |= (SNOR_HWCAPS_READ_1_4_4 |
28137aeedac0SVignesh R 					SNOR_HWCAPS_PP_1_1_4 |
28147aeedac0SVignesh R 					SNOR_HWCAPS_PP_1_4_4);
28157aeedac0SVignesh R 	} else if (spi->mode & SPI_RX_DUAL) {
28167aeedac0SVignesh R 		hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
28177aeedac0SVignesh R 
28187aeedac0SVignesh R 		if (spi->mode & SPI_TX_DUAL)
28197aeedac0SVignesh R 			hwcaps.mask |= SNOR_HWCAPS_READ_1_2_2;
28207aeedac0SVignesh R 	}
28217aeedac0SVignesh R 
28227aeedac0SVignesh R 	info = spi_nor_read_id(nor);
28237aeedac0SVignesh R 	if (IS_ERR_OR_NULL(info))
28247aeedac0SVignesh R 		return -ENOENT;
28250c6f187cSVignesh R 	/* Parse the Serial Flash Discoverable Parameters table. */
28267aeedac0SVignesh R 	ret = spi_nor_init_params(nor, info, &params);
28277aeedac0SVignesh R 	if (ret)
28287aeedac0SVignesh R 		return ret;
28297aeedac0SVignesh R 
28307aeedac0SVignesh R 	if (!mtd->name)
28317aeedac0SVignesh R 		mtd->name = info->name;
28327aeedac0SVignesh R 	mtd->priv = nor;
28337aeedac0SVignesh R 	mtd->type = MTD_NORFLASH;
28347aeedac0SVignesh R 	mtd->writesize = 1;
28357aeedac0SVignesh R 	mtd->flags = MTD_CAP_NORFLASH;
28367aeedac0SVignesh R 	mtd->size = params.size;
28377aeedac0SVignesh R 	mtd->_erase = spi_nor_erase;
28387aeedac0SVignesh R 	mtd->_read = spi_nor_read;
28397aeedac0SVignesh R 
28407aeedac0SVignesh R #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
28417aeedac0SVignesh R 	/* NOR protection support for STmicro/Micron chips and similar */
28427aeedac0SVignesh R 	if (JEDEC_MFR(info) == SNOR_MFR_ST ||
28437aeedac0SVignesh R 	    JEDEC_MFR(info) == SNOR_MFR_MICRON ||
28447aeedac0SVignesh R 	    JEDEC_MFR(info) == SNOR_MFR_SST ||
28457aeedac0SVignesh R 			info->flags & SPI_NOR_HAS_LOCK) {
28467aeedac0SVignesh R 		nor->flash_lock = stm_lock;
28477aeedac0SVignesh R 		nor->flash_unlock = stm_unlock;
28487aeedac0SVignesh R 		nor->flash_is_locked = stm_is_locked;
28497aeedac0SVignesh R 	}
28507aeedac0SVignesh R #endif
28517aeedac0SVignesh R 
28527aeedac0SVignesh R #ifdef CONFIG_SPI_FLASH_SST
28537aeedac0SVignesh R 	/* sst nor chips use AAI word program */
28547aeedac0SVignesh R 	if (info->flags & SST_WRITE)
28557aeedac0SVignesh R 		mtd->_write = sst_write;
28567aeedac0SVignesh R 	else
28577aeedac0SVignesh R #endif
28587aeedac0SVignesh R 		mtd->_write = spi_nor_write;
28597aeedac0SVignesh R 
28607aeedac0SVignesh R 	if (info->flags & USE_FSR)
28617aeedac0SVignesh R 		nor->flags |= SNOR_F_USE_FSR;
28627aeedac0SVignesh R 	if (info->flags & SPI_NOR_HAS_TB)
28637aeedac0SVignesh R 		nor->flags |= SNOR_F_HAS_SR_TB;
28647aeedac0SVignesh R 	if (info->flags & NO_CHIP_ERASE)
28657aeedac0SVignesh R 		nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
28667aeedac0SVignesh R 	if (info->flags & USE_CLSR)
28677aeedac0SVignesh R 		nor->flags |= SNOR_F_USE_CLSR;
28687aeedac0SVignesh R 
28697aeedac0SVignesh R 	if (info->flags & SPI_NOR_NO_ERASE)
28707aeedac0SVignesh R 		mtd->flags |= MTD_NO_ERASE;
28717aeedac0SVignesh R 
28727aeedac0SVignesh R 	nor->page_size = params.page_size;
28737aeedac0SVignesh R 	mtd->writebufsize = nor->page_size;
28747aeedac0SVignesh R 
2875cd800046SChin-Ting Kuo #ifdef CONFIG_SPI_FLASH_SPANSION
2876cd800046SChin-Ting Kuo 	if (cypress_s25hx_t(info)) {
2877cd800046SChin-Ting Kuo 		/*
2878cd800046SChin-Ting Kuo 		 * The Cypress Semper family has transparent ECC. To preserve
2879cd800046SChin-Ting Kuo 		 * ECC enabled, multi-pass programming within the same 16-byte
2880cd800046SChin-Ting Kuo 		 * ECC data unit needs to be avoided. Set writesize to the page
2881cd800046SChin-Ting Kuo 		 * size and remove the MTD_BIT_WRITEABLE flag in mtd_info to
2882cd800046SChin-Ting Kuo 		 * prevent multi-pass programming.
2883cd800046SChin-Ting Kuo 		 */
2884cd800046SChin-Ting Kuo 		nor->mtd.writesize = params.page_size;
2885cd800046SChin-Ting Kuo 		nor->mtd.flags &= ~MTD_BIT_WRITEABLE;
2886eaad4c09SChin-Ting Kuo 		ret = s25hx_t_setup(nor, info);
2887eaad4c09SChin-Ting Kuo 		if (ret) {
2888eaad4c09SChin-Ting Kuo 			dev_err(nor->dev, "fail to setup s25hx_t flash\n");
2889eaad4c09SChin-Ting Kuo 			return ret;
2890eaad4c09SChin-Ting Kuo 		}
2891cd800046SChin-Ting Kuo 	}
2892cd800046SChin-Ting Kuo #endif
2893cd800046SChin-Ting Kuo 
28947aeedac0SVignesh R 	/* Some devices cannot do fast-read, no matter what DT tells us */
28957aeedac0SVignesh R 	if ((info->flags & SPI_NOR_NO_FR) || (spi->mode & SPI_RX_SLOW))
28967aeedac0SVignesh R 		params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
28977aeedac0SVignesh R 
28987aeedac0SVignesh R 	/*
28997aeedac0SVignesh R 	 * Configure the SPI memory:
29007aeedac0SVignesh R 	 * - select op codes for (Fast) Read, Page Program and Sector Erase.
29017aeedac0SVignesh R 	 * - set the number of dummy cycles (mode cycles + wait states).
29027aeedac0SVignesh R 	 * - set the SPI protocols for register and memory accesses.
29037aeedac0SVignesh R 	 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
29047aeedac0SVignesh R 	 */
29057aeedac0SVignesh R 	ret = spi_nor_setup(nor, info, &params, &hwcaps);
29067aeedac0SVignesh R 	if (ret)
29077aeedac0SVignesh R 		return ret;
29087aeedac0SVignesh R 
29090c6f187cSVignesh R 	if (nor->addr_width) {
29100c6f187cSVignesh R 		/* already configured from SFDP */
29110c6f187cSVignesh R 	} else if (info->addr_width) {
29127aeedac0SVignesh R 		nor->addr_width = info->addr_width;
29138c927809SVignesh R 	} else if (mtd->size > SZ_16M) {
29148c927809SVignesh R #ifndef CONFIG_SPI_FLASH_BAR
291561059bc5SVignesh R 		/* enable 4-byte addressing if the device exceeds 16MiB */
291661059bc5SVignesh R 		nor->addr_width = 4;
2917cd800046SChin-Ting Kuo 		if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
2918cd800046SChin-Ting Kuo 		    info->flags & SPI_NOR_4B_OPCODES)
291961059bc5SVignesh R 			spi_nor_set_4byte_opcodes(nor, info);
29208c927809SVignesh R #else
29218c927809SVignesh R 		/* Configure the BAR - discover bank cmds and read current bank */
29228c927809SVignesh R 		nor->addr_width = 3;
29238c927809SVignesh R 		ret = read_bar(nor, info);
29248c927809SVignesh R 		if (ret < 0)
29258c927809SVignesh R 			return ret;
29268c927809SVignesh R #endif
29277aeedac0SVignesh R 	} else {
29287aeedac0SVignesh R 		nor->addr_width = 3;
29297aeedac0SVignesh R 	}
29307aeedac0SVignesh R 
29317aeedac0SVignesh R 	if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
29327aeedac0SVignesh R 		dev_dbg(dev, "address width is too large: %u\n",
29337aeedac0SVignesh R 			nor->addr_width);
29347aeedac0SVignesh R 		return -EINVAL;
29357aeedac0SVignesh R 	}
29367aeedac0SVignesh R 
29377aeedac0SVignesh R 	/* Send all the required SPI flash commands to initialize device */
29387aeedac0SVignesh R 	nor->info = info;
29397aeedac0SVignesh R 	ret = spi_nor_init(nor);
29407aeedac0SVignesh R 	if (ret)
29417aeedac0SVignesh R 		return ret;
29427aeedac0SVignesh R 
29437aeedac0SVignesh R 	nor->name = mtd->name;
29447aeedac0SVignesh R 	nor->size = mtd->size;
29457aeedac0SVignesh R 	nor->erase_size = mtd->erasesize;
29467aeedac0SVignesh R 	nor->sector_size = mtd->erasesize;
29477aeedac0SVignesh R 
29487aeedac0SVignesh R #ifndef CONFIG_SPL_BUILD
29497aeedac0SVignesh R 	printf("SF: Detected %s with page size ", nor->name);
29507aeedac0SVignesh R 	print_size(nor->page_size, ", erase size ");
29517aeedac0SVignesh R 	print_size(nor->erase_size, ", total ");
29527aeedac0SVignesh R 	print_size(nor->size, "");
29537aeedac0SVignesh R 	puts("\n");
29547aeedac0SVignesh R #endif
29557aeedac0SVignesh R 
29567aeedac0SVignesh R 	return 0;
29577aeedac0SVignesh R }
29588c927809SVignesh R 
29598c927809SVignesh R /* U-Boot specific functions, need to extend MTD to support these */
spi_flash_cmd_get_sw_write_prot(struct spi_nor * nor)29608c927809SVignesh R int spi_flash_cmd_get_sw_write_prot(struct spi_nor *nor)
29618c927809SVignesh R {
29628c927809SVignesh R 	int sr = read_sr(nor);
29638c927809SVignesh R 
29648c927809SVignesh R 	if (sr < 0)
29658c927809SVignesh R 		return sr;
29668c927809SVignesh R 
29678c927809SVignesh R 	return (sr >> 2) & 7;
29688c927809SVignesh R }
2969*591e1cf0SChin-Ting Kuo 
spi_flash_wlock_by_host_ctrl(struct spi_nor * nor,u32 offset,size_t len)2970*591e1cf0SChin-Ting Kuo int spi_flash_wlock_by_host_ctrl(struct spi_nor *nor, u32 offset, size_t len)
2971*591e1cf0SChin-Ting Kuo {
2972*591e1cf0SChin-Ting Kuo 	nor->flash_lock_by_host_ctrl(nor, offset, len);
2973*591e1cf0SChin-Ting Kuo 	return 0;
2974*591e1cf0SChin-Ting Kuo }
2975*591e1cf0SChin-Ting Kuo 
spi_flash_wunlock_by_host_ctrl(struct spi_nor * nor,u32 offset,size_t len)2976*591e1cf0SChin-Ting Kuo int spi_flash_wunlock_by_host_ctrl(struct spi_nor *nor, u32 offset, size_t len)
2977*591e1cf0SChin-Ting Kuo {
2978*591e1cf0SChin-Ting Kuo 	nor->flash_unlock_by_host_ctrl(nor, offset, len);
2979*591e1cf0SChin-Ting Kuo 	return 0;
2980*591e1cf0SChin-Ting Kuo }
2981*591e1cf0SChin-Ting Kuo 
2982