xref: /rk3399_rockchip-uboot/include/linux/mtd/spi-nor.h (revision 305d7e6eb355be2c9fdd766a19e8873f59ce6378)
1a2b7f194SVignesh R // SPDX-License-Identifier: GPL-2.0
2a2b7f194SVignesh R /*
3a2b7f194SVignesh R  * Copyright (C) 2014 Freescale Semiconductor, Inc.
4a2b7f194SVignesh R  * Synced from Linux v4.19
5a2b7f194SVignesh R  */
6a2b7f194SVignesh R 
7a2b7f194SVignesh R #ifndef __LINUX_MTD_SPI_NOR_H
8a2b7f194SVignesh R #define __LINUX_MTD_SPI_NOR_H
9a2b7f194SVignesh R 
10a2b7f194SVignesh R #include <linux/bitops.h>
11a2b7f194SVignesh R #include <linux/mtd/cfi.h>
12a2b7f194SVignesh R #include <linux/mtd/mtd.h>
13a2b7f194SVignesh R 
14a2b7f194SVignesh R /*
15a2b7f194SVignesh R  * Manufacturer IDs
16a2b7f194SVignesh R  *
17a2b7f194SVignesh R  * The first byte returned from the flash after sending opcode SPINOR_OP_RDID.
18a2b7f194SVignesh R  * Sometimes these are the same as CFI IDs, but sometimes they aren't.
19a2b7f194SVignesh R  */
20a2b7f194SVignesh R #define SNOR_MFR_ATMEL		CFI_MFR_ATMEL
21a2b7f194SVignesh R #define SNOR_MFR_GIGADEVICE	0xc8
22a2b7f194SVignesh R #define SNOR_MFR_INTEL		CFI_MFR_INTEL
23a2b7f194SVignesh R #define SNOR_MFR_ST		CFI_MFR_ST /* ST Micro <--> Micron */
24a2b7f194SVignesh R #define SNOR_MFR_MICRON		CFI_MFR_MICRON /* ST Micro <--> Micron */
25a2b7f194SVignesh R #define SNOR_MFR_MACRONIX	CFI_MFR_MACRONIX
26a2b7f194SVignesh R #define SNOR_MFR_SPANSION	CFI_MFR_AMD
27a2b7f194SVignesh R #define SNOR_MFR_SST		CFI_MFR_SST
28a2b7f194SVignesh R #define SNOR_MFR_WINBOND	0xef /* Also used by some Spansion */
29a2b7f194SVignesh R 
30a2b7f194SVignesh R /*
31a2b7f194SVignesh R  * Note on opcode nomenclature: some opcodes have a format like
32a2b7f194SVignesh R  * SPINOR_OP_FUNCTION{4,}_x_y_z. The numbers x, y, and z stand for the number
33a2b7f194SVignesh R  * of I/O lines used for the opcode, address, and data (respectively). The
34a2b7f194SVignesh R  * FUNCTION has an optional suffix of '4', to represent an opcode which
35a2b7f194SVignesh R  * requires a 4-byte (32-bit) address.
36a2b7f194SVignesh R  */
37a2b7f194SVignesh R 
38a2b7f194SVignesh R /* Flash opcodes. */
39a2b7f194SVignesh R #define SPINOR_OP_WREN		0x06	/* Write enable */
40a2b7f194SVignesh R #define SPINOR_OP_RDSR		0x05	/* Read status register */
41a2b7f194SVignesh R #define SPINOR_OP_WRSR		0x01	/* Write status register 1 byte */
42a2b7f194SVignesh R #define SPINOR_OP_RDSR2		0x3f	/* Read status register 2 */
43a2b7f194SVignesh R #define SPINOR_OP_WRSR2		0x3e	/* Write status register 2 */
44a2b7f194SVignesh R #define SPINOR_OP_READ		0x03	/* Read data bytes (low frequency) */
45a2b7f194SVignesh R #define SPINOR_OP_READ_FAST	0x0b	/* Read data bytes (high frequency) */
46a2b7f194SVignesh R #define SPINOR_OP_READ_1_1_2	0x3b	/* Read data bytes (Dual Output SPI) */
47a2b7f194SVignesh R #define SPINOR_OP_READ_1_2_2	0xbb	/* Read data bytes (Dual I/O SPI) */
48a2b7f194SVignesh R #define SPINOR_OP_READ_1_1_4	0x6b	/* Read data bytes (Quad Output SPI) */
49a2b7f194SVignesh R #define SPINOR_OP_READ_1_4_4	0xeb	/* Read data bytes (Quad I/O SPI) */
50*305d7e6eSVignesh Raghavendra #define SPINOR_OP_READ_1_1_8	0x8b    /* Read data bytes (Octal Output SPI) */
51*305d7e6eSVignesh Raghavendra #define SPINOR_OP_READ_1_8_8	0xcb    /* Read data bytes (Octal I/O SPI) */
52a2b7f194SVignesh R #define SPINOR_OP_PP		0x02	/* Page program (up to 256 bytes) */
53a2b7f194SVignesh R #define SPINOR_OP_PP_1_1_4	0x32	/* Quad page program */
54a2b7f194SVignesh R #define SPINOR_OP_PP_1_4_4	0x38	/* Quad page program */
55*305d7e6eSVignesh Raghavendra #define SPINOR_OP_PP_1_1_8	0x82    /* Octal page program */
56*305d7e6eSVignesh Raghavendra #define SPINOR_OP_PP_1_8_8	0xc2    /* Octal page program */
57a2b7f194SVignesh R #define SPINOR_OP_BE_4K		0x20	/* Erase 4KiB block */
58a2b7f194SVignesh R #define SPINOR_OP_BE_4K_PMC	0xd7	/* Erase 4KiB block on PMC chips */
59a2b7f194SVignesh R #define SPINOR_OP_BE_32K	0x52	/* Erase 32KiB block */
60a2b7f194SVignesh R #define SPINOR_OP_CHIP_ERASE	0xc7	/* Erase whole flash chip */
61a2b7f194SVignesh R #define SPINOR_OP_SE		0xd8	/* Sector erase (usually 64KiB) */
62a2b7f194SVignesh R #define SPINOR_OP_RDID		0x9f	/* Read JEDEC ID */
63a2b7f194SVignesh R #define SPINOR_OP_RDSFDP	0x5a	/* Read SFDP */
64a2b7f194SVignesh R #define SPINOR_OP_RDCR		0x35	/* Read configuration register */
65a2b7f194SVignesh R #define SPINOR_OP_RDFSR		0x70	/* Read flag status register */
66a2b7f194SVignesh R #define SPINOR_OP_CLFSR		0x50	/* Clear flag status register */
67a2b7f194SVignesh R #define SPINOR_OP_RDEAR		0xc8	/* Read Extended Address Register */
68a2b7f194SVignesh R #define SPINOR_OP_WREAR		0xc5	/* Write Extended Address Register */
69a2b7f194SVignesh R 
70a2b7f194SVignesh R /* 4-byte address opcodes - used on Spansion and some Macronix flashes. */
71a2b7f194SVignesh R #define SPINOR_OP_READ_4B	0x13	/* Read data bytes (low frequency) */
72a2b7f194SVignesh R #define SPINOR_OP_READ_FAST_4B	0x0c	/* Read data bytes (high frequency) */
73a2b7f194SVignesh R #define SPINOR_OP_READ_1_1_2_4B	0x3c	/* Read data bytes (Dual Output SPI) */
74a2b7f194SVignesh R #define SPINOR_OP_READ_1_2_2_4B	0xbc	/* Read data bytes (Dual I/O SPI) */
75a2b7f194SVignesh R #define SPINOR_OP_READ_1_1_4_4B	0x6c	/* Read data bytes (Quad Output SPI) */
76a2b7f194SVignesh R #define SPINOR_OP_READ_1_4_4_4B	0xec	/* Read data bytes (Quad I/O SPI) */
77*305d7e6eSVignesh Raghavendra #define SPINOR_OP_READ_1_1_8_4B	0x7c    /* Read data bytes (Octal Output SPI) */
78*305d7e6eSVignesh Raghavendra #define SPINOR_OP_READ_1_8_8_4B	0xcc    /* Read data bytes (Octal I/O SPI) */
79a2b7f194SVignesh R #define SPINOR_OP_PP_4B		0x12	/* Page program (up to 256 bytes) */
80a2b7f194SVignesh R #define SPINOR_OP_PP_1_1_4_4B	0x34	/* Quad page program */
81a2b7f194SVignesh R #define SPINOR_OP_PP_1_4_4_4B	0x3e	/* Quad page program */
82*305d7e6eSVignesh Raghavendra #define SPINOR_OP_PP_1_1_8_4B	0x84    /* Octal page program */
83*305d7e6eSVignesh Raghavendra #define SPINOR_OP_PP_1_8_8_4B	0x8e    /* Octal page program */
84a2b7f194SVignesh R #define SPINOR_OP_BE_4K_4B	0x21	/* Erase 4KiB block */
85a2b7f194SVignesh R #define SPINOR_OP_BE_32K_4B	0x5c	/* Erase 32KiB block */
86a2b7f194SVignesh R #define SPINOR_OP_SE_4B		0xdc	/* Sector erase (usually 64KiB) */
87a2b7f194SVignesh R 
88a2b7f194SVignesh R /* Double Transfer Rate opcodes - defined in JEDEC JESD216B. */
89a2b7f194SVignesh R #define SPINOR_OP_READ_1_1_1_DTR	0x0d
90a2b7f194SVignesh R #define SPINOR_OP_READ_1_2_2_DTR	0xbd
91a2b7f194SVignesh R #define SPINOR_OP_READ_1_4_4_DTR	0xed
92a2b7f194SVignesh R 
93a2b7f194SVignesh R #define SPINOR_OP_READ_1_1_1_DTR_4B	0x0e
94a2b7f194SVignesh R #define SPINOR_OP_READ_1_2_2_DTR_4B	0xbe
95a2b7f194SVignesh R #define SPINOR_OP_READ_1_4_4_DTR_4B	0xee
96a2b7f194SVignesh R 
97a2b7f194SVignesh R /* Used for SST flashes only. */
98a2b7f194SVignesh R #define SPINOR_OP_BP		0x02	/* Byte program */
99a2b7f194SVignesh R #define SPINOR_OP_WRDI		0x04	/* Write disable */
100a2b7f194SVignesh R #define SPINOR_OP_AAI_WP	0xad	/* Auto address increment word program */
101a2b7f194SVignesh R 
1024b522e90SEugeniy Paltsev /* Used for SST26* flashes only. */
1034b522e90SEugeniy Paltsev #define SPINOR_OP_READ_BPR	0x72	/* Read block protection register */
1044b522e90SEugeniy Paltsev #define SPINOR_OP_WRITE_BPR	0x42	/* Write block protection register */
1054b522e90SEugeniy Paltsev 
106a2b7f194SVignesh R /* Used for S3AN flashes only */
107a2b7f194SVignesh R #define SPINOR_OP_XSE		0x50	/* Sector erase */
108a2b7f194SVignesh R #define SPINOR_OP_XPP		0x82	/* Page program */
109a2b7f194SVignesh R #define SPINOR_OP_XRDSR		0xd7	/* Read status register */
110a2b7f194SVignesh R 
111a2b7f194SVignesh R #define XSR_PAGESIZE		BIT(0)	/* Page size in Po2 or Linear */
112a2b7f194SVignesh R #define XSR_RDY			BIT(7)	/* Ready */
113a2b7f194SVignesh R 
114a2b7f194SVignesh R /* Used for Macronix and Winbond flashes. */
115a2b7f194SVignesh R #define SPINOR_OP_EN4B		0xb7	/* Enter 4-byte mode */
116a2b7f194SVignesh R #define SPINOR_OP_EX4B		0xe9	/* Exit 4-byte mode */
117a2b7f194SVignesh R 
118a2b7f194SVignesh R /* Used for Spansion flashes only. */
119a2b7f194SVignesh R #define SPINOR_OP_BRWR		0x17	/* Bank register write */
120dc6fa43fSVignesh R #define SPINOR_OP_BRRD		0x16	/* Bank register read */
121a2b7f194SVignesh R #define SPINOR_OP_CLSR		0x30	/* Clear status register 1 */
122a2b7f194SVignesh R 
123a2b7f194SVignesh R /* Used for Micron flashes only. */
124a2b7f194SVignesh R #define SPINOR_OP_RD_EVCR      0x65    /* Read EVCR register */
125a2b7f194SVignesh R #define SPINOR_OP_WD_EVCR      0x61    /* Write EVCR register */
126a2b7f194SVignesh R 
127a2b7f194SVignesh R /* Status Register bits. */
128a2b7f194SVignesh R #define SR_WIP			BIT(0)	/* Write in progress */
129a2b7f194SVignesh R #define SR_WEL			BIT(1)	/* Write enable latch */
130a2b7f194SVignesh R /* meaning of other SR_* bits may differ between vendors */
131a2b7f194SVignesh R #define SR_BP0			BIT(2)	/* Block protect 0 */
132a2b7f194SVignesh R #define SR_BP1			BIT(3)	/* Block protect 1 */
133a2b7f194SVignesh R #define SR_BP2			BIT(4)	/* Block protect 2 */
134a2b7f194SVignesh R #define SR_TB			BIT(5)	/* Top/Bottom protect */
135a2b7f194SVignesh R #define SR_SRWD			BIT(7)	/* SR write protect */
136a2b7f194SVignesh R /* Spansion/Cypress specific status bits */
137a2b7f194SVignesh R #define SR_E_ERR		BIT(5)
138a2b7f194SVignesh R #define SR_P_ERR		BIT(6)
139a2b7f194SVignesh R 
140a2b7f194SVignesh R #define SR_QUAD_EN_MX		BIT(6)	/* Macronix Quad I/O */
141a2b7f194SVignesh R 
142a2b7f194SVignesh R /* Enhanced Volatile Configuration Register bits */
143a2b7f194SVignesh R #define EVCR_QUAD_EN_MICRON	BIT(7)	/* Micron Quad I/O */
144a2b7f194SVignesh R 
145a2b7f194SVignesh R /* Flag Status Register bits */
146a2b7f194SVignesh R #define FSR_READY		BIT(7)	/* Device status, 0 = Busy, 1 = Ready */
147a2b7f194SVignesh R #define FSR_E_ERR		BIT(5)	/* Erase operation status */
148a2b7f194SVignesh R #define FSR_P_ERR		BIT(4)	/* Program operation status */
149a2b7f194SVignesh R #define FSR_PT_ERR		BIT(1)	/* Protection error bit */
150a2b7f194SVignesh R 
151a2b7f194SVignesh R /* Configuration Register bits. */
152a2b7f194SVignesh R #define CR_QUAD_EN_SPAN		BIT(1)	/* Spansion Quad I/O */
153a2b7f194SVignesh R 
154a2b7f194SVignesh R /* Status Register 2 bits. */
155a2b7f194SVignesh R #define SR2_QUAD_EN_BIT7	BIT(7)
156a2b7f194SVignesh R 
157a2b7f194SVignesh R /* Supported SPI protocols */
158a2b7f194SVignesh R #define SNOR_PROTO_INST_MASK	GENMASK(23, 16)
159a2b7f194SVignesh R #define SNOR_PROTO_INST_SHIFT	16
160a2b7f194SVignesh R #define SNOR_PROTO_INST(_nbits)	\
161a2b7f194SVignesh R 	((((unsigned long)(_nbits)) << SNOR_PROTO_INST_SHIFT) & \
162a2b7f194SVignesh R 	 SNOR_PROTO_INST_MASK)
163a2b7f194SVignesh R 
164a2b7f194SVignesh R #define SNOR_PROTO_ADDR_MASK	GENMASK(15, 8)
165a2b7f194SVignesh R #define SNOR_PROTO_ADDR_SHIFT	8
166a2b7f194SVignesh R #define SNOR_PROTO_ADDR(_nbits)	\
167a2b7f194SVignesh R 	((((unsigned long)(_nbits)) << SNOR_PROTO_ADDR_SHIFT) & \
168a2b7f194SVignesh R 	 SNOR_PROTO_ADDR_MASK)
169a2b7f194SVignesh R 
170a2b7f194SVignesh R #define SNOR_PROTO_DATA_MASK	GENMASK(7, 0)
171a2b7f194SVignesh R #define SNOR_PROTO_DATA_SHIFT	0
172a2b7f194SVignesh R #define SNOR_PROTO_DATA(_nbits)	\
173a2b7f194SVignesh R 	((((unsigned long)(_nbits)) << SNOR_PROTO_DATA_SHIFT) & \
174a2b7f194SVignesh R 	 SNOR_PROTO_DATA_MASK)
175a2b7f194SVignesh R 
176a2b7f194SVignesh R #define SNOR_PROTO_IS_DTR	BIT(24)	/* Double Transfer Rate */
177a2b7f194SVignesh R 
178a2b7f194SVignesh R #define SNOR_PROTO_STR(_inst_nbits, _addr_nbits, _data_nbits)	\
179a2b7f194SVignesh R 	(SNOR_PROTO_INST(_inst_nbits) |				\
180a2b7f194SVignesh R 	 SNOR_PROTO_ADDR(_addr_nbits) |				\
181a2b7f194SVignesh R 	 SNOR_PROTO_DATA(_data_nbits))
182a2b7f194SVignesh R #define SNOR_PROTO_DTR(_inst_nbits, _addr_nbits, _data_nbits)	\
183a2b7f194SVignesh R 	(SNOR_PROTO_IS_DTR |					\
184a2b7f194SVignesh R 	 SNOR_PROTO_STR(_inst_nbits, _addr_nbits, _data_nbits))
185a2b7f194SVignesh R 
186a2b7f194SVignesh R enum spi_nor_protocol {
187a2b7f194SVignesh R 	SNOR_PROTO_1_1_1 = SNOR_PROTO_STR(1, 1, 1),
188a2b7f194SVignesh R 	SNOR_PROTO_1_1_2 = SNOR_PROTO_STR(1, 1, 2),
189a2b7f194SVignesh R 	SNOR_PROTO_1_1_4 = SNOR_PROTO_STR(1, 1, 4),
190a2b7f194SVignesh R 	SNOR_PROTO_1_1_8 = SNOR_PROTO_STR(1, 1, 8),
191a2b7f194SVignesh R 	SNOR_PROTO_1_2_2 = SNOR_PROTO_STR(1, 2, 2),
192a2b7f194SVignesh R 	SNOR_PROTO_1_4_4 = SNOR_PROTO_STR(1, 4, 4),
193a2b7f194SVignesh R 	SNOR_PROTO_1_8_8 = SNOR_PROTO_STR(1, 8, 8),
194a2b7f194SVignesh R 	SNOR_PROTO_2_2_2 = SNOR_PROTO_STR(2, 2, 2),
195a2b7f194SVignesh R 	SNOR_PROTO_4_4_4 = SNOR_PROTO_STR(4, 4, 4),
196a2b7f194SVignesh R 	SNOR_PROTO_8_8_8 = SNOR_PROTO_STR(8, 8, 8),
197a2b7f194SVignesh R 
198a2b7f194SVignesh R 	SNOR_PROTO_1_1_1_DTR = SNOR_PROTO_DTR(1, 1, 1),
199a2b7f194SVignesh R 	SNOR_PROTO_1_2_2_DTR = SNOR_PROTO_DTR(1, 2, 2),
200a2b7f194SVignesh R 	SNOR_PROTO_1_4_4_DTR = SNOR_PROTO_DTR(1, 4, 4),
201a2b7f194SVignesh R 	SNOR_PROTO_1_8_8_DTR = SNOR_PROTO_DTR(1, 8, 8),
202a2b7f194SVignesh R };
203a2b7f194SVignesh R 
204a2b7f194SVignesh R static inline bool spi_nor_protocol_is_dtr(enum spi_nor_protocol proto)
205a2b7f194SVignesh R {
206a2b7f194SVignesh R 	return !!(proto & SNOR_PROTO_IS_DTR);
207a2b7f194SVignesh R }
208a2b7f194SVignesh R 
209a2b7f194SVignesh R static inline u8 spi_nor_get_protocol_inst_nbits(enum spi_nor_protocol proto)
210a2b7f194SVignesh R {
211a2b7f194SVignesh R 	return ((unsigned long)(proto & SNOR_PROTO_INST_MASK)) >>
212a2b7f194SVignesh R 		SNOR_PROTO_INST_SHIFT;
213a2b7f194SVignesh R }
214a2b7f194SVignesh R 
215a2b7f194SVignesh R static inline u8 spi_nor_get_protocol_addr_nbits(enum spi_nor_protocol proto)
216a2b7f194SVignesh R {
217a2b7f194SVignesh R 	return ((unsigned long)(proto & SNOR_PROTO_ADDR_MASK)) >>
218a2b7f194SVignesh R 		SNOR_PROTO_ADDR_SHIFT;
219a2b7f194SVignesh R }
220a2b7f194SVignesh R 
221a2b7f194SVignesh R static inline u8 spi_nor_get_protocol_data_nbits(enum spi_nor_protocol proto)
222a2b7f194SVignesh R {
223a2b7f194SVignesh R 	return ((unsigned long)(proto & SNOR_PROTO_DATA_MASK)) >>
224a2b7f194SVignesh R 		SNOR_PROTO_DATA_SHIFT;
225a2b7f194SVignesh R }
226a2b7f194SVignesh R 
227a2b7f194SVignesh R static inline u8 spi_nor_get_protocol_width(enum spi_nor_protocol proto)
228a2b7f194SVignesh R {
229a2b7f194SVignesh R 	return spi_nor_get_protocol_data_nbits(proto);
230a2b7f194SVignesh R }
231a2b7f194SVignesh R 
232a2b7f194SVignesh R #define SPI_NOR_MAX_CMD_SIZE	8
233a2b7f194SVignesh R enum spi_nor_ops {
234a2b7f194SVignesh R 	SPI_NOR_OPS_READ = 0,
235a2b7f194SVignesh R 	SPI_NOR_OPS_WRITE,
236a2b7f194SVignesh R 	SPI_NOR_OPS_ERASE,
237a2b7f194SVignesh R 	SPI_NOR_OPS_LOCK,
238a2b7f194SVignesh R 	SPI_NOR_OPS_UNLOCK,
239a2b7f194SVignesh R };
240a2b7f194SVignesh R 
241a2b7f194SVignesh R enum spi_nor_option_flags {
242a2b7f194SVignesh R 	SNOR_F_USE_FSR		= BIT(0),
243a2b7f194SVignesh R 	SNOR_F_HAS_SR_TB	= BIT(1),
244a2b7f194SVignesh R 	SNOR_F_NO_OP_CHIP_ERASE	= BIT(2),
245a2b7f194SVignesh R 	SNOR_F_S3AN_ADDR_DEFAULT = BIT(3),
246a2b7f194SVignesh R 	SNOR_F_READY_XSR_RDY	= BIT(4),
247a2b7f194SVignesh R 	SNOR_F_USE_CLSR		= BIT(5),
248a2b7f194SVignesh R 	SNOR_F_BROKEN_RESET	= BIT(6),
249a2b7f194SVignesh R };
250a2b7f194SVignesh R 
251a2b7f194SVignesh R /**
252a2b7f194SVignesh R  * struct flash_info - Forward declaration of a structure used internally by
253a2b7f194SVignesh R  *		       spi_nor_scan()
254a2b7f194SVignesh R  */
255a2b7f194SVignesh R struct flash_info;
256a2b7f194SVignesh R 
257a2b7f194SVignesh R /* TODO: Remove, once all users of spi_flash interface are moved to MTD */
258a2b7f194SVignesh R #define spi_flash spi_nor
259a2b7f194SVignesh R 
260a2b7f194SVignesh R /**
261a2b7f194SVignesh R  * struct spi_nor - Structure for defining a the SPI NOR layer
262a2b7f194SVignesh R  * @mtd:		point to a mtd_info structure
263a2b7f194SVignesh R  * @lock:		the lock for the read/write/erase/lock/unlock operations
264a2b7f194SVignesh R  * @dev:		point to a spi device, or a spi nor controller device.
265a2b7f194SVignesh R  * @info:		spi-nor part JDEC MFR id and other info
266a2b7f194SVignesh R  * @page_size:		the page size of the SPI NOR
267a2b7f194SVignesh R  * @addr_width:		number of address bytes
268a2b7f194SVignesh R  * @erase_opcode:	the opcode for erasing a sector
269a2b7f194SVignesh R  * @read_opcode:	the read opcode
270a2b7f194SVignesh R  * @read_dummy:		the dummy needed by the read operation
271a2b7f194SVignesh R  * @program_opcode:	the program opcode
272dc6fa43fSVignesh R  * @bank_read_cmd:	Bank read cmd
273dc6fa43fSVignesh R  * @bank_write_cmd:	Bank write cmd
274dc6fa43fSVignesh R  * @bank_curr:		Current flash bank
275a2b7f194SVignesh R  * @sst_write_second:	used by the SST write operation
276a2b7f194SVignesh R  * @flags:		flag options for the current SPI-NOR (SNOR_F_*)
277a2b7f194SVignesh R  * @read_proto:		the SPI protocol for read operations
278a2b7f194SVignesh R  * @write_proto:	the SPI protocol for write operations
279a2b7f194SVignesh R  * @reg_proto		the SPI protocol for read_reg/write_reg/erase operations
280a2b7f194SVignesh R  * @cmd_buf:		used by the write_reg
281a2b7f194SVignesh R  * @prepare:		[OPTIONAL] do some preparations for the
282a2b7f194SVignesh R  *			read/write/erase/lock/unlock operations
283a2b7f194SVignesh R  * @unprepare:		[OPTIONAL] do some post work after the
284a2b7f194SVignesh R  *			read/write/erase/lock/unlock operations
285a2b7f194SVignesh R  * @read_reg:		[DRIVER-SPECIFIC] read out the register
286a2b7f194SVignesh R  * @write_reg:		[DRIVER-SPECIFIC] write data to the register
287a2b7f194SVignesh R  * @read:		[DRIVER-SPECIFIC] read data from the SPI NOR
288a2b7f194SVignesh R  * @write:		[DRIVER-SPECIFIC] write data to the SPI NOR
289a2b7f194SVignesh R  * @erase:		[DRIVER-SPECIFIC] erase a sector of the SPI NOR
290a2b7f194SVignesh R  *			at the offset @offs; if not provided by the driver,
291a2b7f194SVignesh R  *			spi-nor will send the erase opcode via write_reg()
292a2b7f194SVignesh R  * @flash_lock:		[FLASH-SPECIFIC] lock a region of the SPI NOR
293a2b7f194SVignesh R  * @flash_unlock:	[FLASH-SPECIFIC] unlock a region of the SPI NOR
294a2b7f194SVignesh R  * @flash_is_locked:	[FLASH-SPECIFIC] check if a region of the SPI NOR is
295a2b7f194SVignesh R  * @quad_enable:	[FLASH-SPECIFIC] enables SPI NOR quad mode
296a2b7f194SVignesh R  *			completely locked
297a2b7f194SVignesh R  * @priv:		the private data
298a2b7f194SVignesh R  */
299a2b7f194SVignesh R struct spi_nor {
300a2b7f194SVignesh R 	struct mtd_info		mtd;
301a2b7f194SVignesh R 	struct udevice		*dev;
302a2b7f194SVignesh R 	struct spi_slave	*spi;
303a2b7f194SVignesh R 	const struct flash_info	*info;
304a2b7f194SVignesh R 	u32			page_size;
305a2b7f194SVignesh R 	u8			addr_width;
306a2b7f194SVignesh R 	u8			erase_opcode;
307a2b7f194SVignesh R 	u8			read_opcode;
308a2b7f194SVignesh R 	u8			read_dummy;
309a2b7f194SVignesh R 	u8			program_opcode;
310dc6fa43fSVignesh R #ifdef CONFIG_SPI_FLASH_BAR
311dc6fa43fSVignesh R 	u8			bank_read_cmd;
312dc6fa43fSVignesh R 	u8			bank_write_cmd;
313dc6fa43fSVignesh R 	u8			bank_curr;
314dc6fa43fSVignesh R #endif
315a2b7f194SVignesh R 	enum spi_nor_protocol	read_proto;
316a2b7f194SVignesh R 	enum spi_nor_protocol	write_proto;
317a2b7f194SVignesh R 	enum spi_nor_protocol	reg_proto;
318a2b7f194SVignesh R 	bool			sst_write_second;
319a2b7f194SVignesh R 	u32			flags;
320a2b7f194SVignesh R 	u8			cmd_buf[SPI_NOR_MAX_CMD_SIZE];
321a2b7f194SVignesh R 
322a2b7f194SVignesh R 	int (*prepare)(struct spi_nor *nor, enum spi_nor_ops ops);
323a2b7f194SVignesh R 	void (*unprepare)(struct spi_nor *nor, enum spi_nor_ops ops);
324a2b7f194SVignesh R 	int (*read_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len);
325a2b7f194SVignesh R 	int (*write_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len);
326a2b7f194SVignesh R 
327a2b7f194SVignesh R 	ssize_t (*read)(struct spi_nor *nor, loff_t from,
328a2b7f194SVignesh R 			size_t len, u_char *read_buf);
329a2b7f194SVignesh R 	ssize_t (*write)(struct spi_nor *nor, loff_t to,
330a2b7f194SVignesh R 			 size_t len, const u_char *write_buf);
331a2b7f194SVignesh R 	int (*erase)(struct spi_nor *nor, loff_t offs);
332a2b7f194SVignesh R 
333a2b7f194SVignesh R 	int (*flash_lock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
334a2b7f194SVignesh R 	int (*flash_unlock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
335a2b7f194SVignesh R 	int (*flash_is_locked)(struct spi_nor *nor, loff_t ofs, uint64_t len);
336a2b7f194SVignesh R 	int (*quad_enable)(struct spi_nor *nor);
337a2b7f194SVignesh R 
338a2b7f194SVignesh R 	void *priv;
339a2b7f194SVignesh R /* Compatibility for spi_flash, remove once sf layer is merged with mtd */
340a2b7f194SVignesh R 	const char *name;
341a2b7f194SVignesh R 	u32 size;
342a2b7f194SVignesh R 	u32 sector_size;
343a2b7f194SVignesh R 	u32 erase_size;
344a2b7f194SVignesh R };
345a2b7f194SVignesh R 
346a2b7f194SVignesh R static inline void spi_nor_set_flash_node(struct spi_nor *nor,
347a2b7f194SVignesh R 					  const struct device_node *np)
348a2b7f194SVignesh R {
349a2b7f194SVignesh R 	mtd_set_of_node(&nor->mtd, np);
350a2b7f194SVignesh R }
351a2b7f194SVignesh R 
352a2b7f194SVignesh R static inline const struct
353a2b7f194SVignesh R device_node *spi_nor_get_flash_node(struct spi_nor *nor)
354a2b7f194SVignesh R {
355a2b7f194SVignesh R 	return mtd_get_of_node(&nor->mtd);
356a2b7f194SVignesh R }
357a2b7f194SVignesh R 
358a2b7f194SVignesh R /**
359a2b7f194SVignesh R  * struct spi_nor_hwcaps - Structure for describing the hardware capabilies
360a2b7f194SVignesh R  * supported by the SPI controller (bus master).
361a2b7f194SVignesh R  * @mask:		the bitmask listing all the supported hw capabilies
362a2b7f194SVignesh R  */
363a2b7f194SVignesh R struct spi_nor_hwcaps {
364a2b7f194SVignesh R 	u32	mask;
365a2b7f194SVignesh R };
366a2b7f194SVignesh R 
367a2b7f194SVignesh R /*
368a2b7f194SVignesh R  *(Fast) Read capabilities.
369a2b7f194SVignesh R  * MUST be ordered by priority: the higher bit position, the higher priority.
370a2b7f194SVignesh R  * As a matter of performances, it is relevant to use Octo SPI protocols first,
371a2b7f194SVignesh R  * then Quad SPI protocols before Dual SPI protocols, Fast Read and lastly
372a2b7f194SVignesh R  * (Slow) Read.
373a2b7f194SVignesh R  */
374a2b7f194SVignesh R #define SNOR_HWCAPS_READ_MASK		GENMASK(14, 0)
375a2b7f194SVignesh R #define SNOR_HWCAPS_READ		BIT(0)
376a2b7f194SVignesh R #define SNOR_HWCAPS_READ_FAST		BIT(1)
377a2b7f194SVignesh R #define SNOR_HWCAPS_READ_1_1_1_DTR	BIT(2)
378a2b7f194SVignesh R 
379a2b7f194SVignesh R #define SNOR_HWCAPS_READ_DUAL		GENMASK(6, 3)
380a2b7f194SVignesh R #define SNOR_HWCAPS_READ_1_1_2		BIT(3)
381a2b7f194SVignesh R #define SNOR_HWCAPS_READ_1_2_2		BIT(4)
382a2b7f194SVignesh R #define SNOR_HWCAPS_READ_2_2_2		BIT(5)
383a2b7f194SVignesh R #define SNOR_HWCAPS_READ_1_2_2_DTR	BIT(6)
384a2b7f194SVignesh R 
385a2b7f194SVignesh R #define SNOR_HWCAPS_READ_QUAD		GENMASK(10, 7)
386a2b7f194SVignesh R #define SNOR_HWCAPS_READ_1_1_4		BIT(7)
387a2b7f194SVignesh R #define SNOR_HWCAPS_READ_1_4_4		BIT(8)
388a2b7f194SVignesh R #define SNOR_HWCAPS_READ_4_4_4		BIT(9)
389a2b7f194SVignesh R #define SNOR_HWCAPS_READ_1_4_4_DTR	BIT(10)
390a2b7f194SVignesh R 
391a2b7f194SVignesh R #define SNOR_HWCPAS_READ_OCTO		GENMASK(14, 11)
392a2b7f194SVignesh R #define SNOR_HWCAPS_READ_1_1_8		BIT(11)
393a2b7f194SVignesh R #define SNOR_HWCAPS_READ_1_8_8		BIT(12)
394a2b7f194SVignesh R #define SNOR_HWCAPS_READ_8_8_8		BIT(13)
395a2b7f194SVignesh R #define SNOR_HWCAPS_READ_1_8_8_DTR	BIT(14)
396a2b7f194SVignesh R 
397a2b7f194SVignesh R /*
398a2b7f194SVignesh R  * Page Program capabilities.
399a2b7f194SVignesh R  * MUST be ordered by priority: the higher bit position, the higher priority.
400a2b7f194SVignesh R  * Like (Fast) Read capabilities, Octo/Quad SPI protocols are preferred to the
401a2b7f194SVignesh R  * legacy SPI 1-1-1 protocol.
402a2b7f194SVignesh R  * Note that Dual Page Programs are not supported because there is no existing
403a2b7f194SVignesh R  * JEDEC/SFDP standard to define them. Also at this moment no SPI flash memory
404a2b7f194SVignesh R  * implements such commands.
405a2b7f194SVignesh R  */
406a2b7f194SVignesh R #define SNOR_HWCAPS_PP_MASK	GENMASK(22, 16)
407a2b7f194SVignesh R #define SNOR_HWCAPS_PP		BIT(16)
408a2b7f194SVignesh R 
409a2b7f194SVignesh R #define SNOR_HWCAPS_PP_QUAD	GENMASK(19, 17)
410a2b7f194SVignesh R #define SNOR_HWCAPS_PP_1_1_4	BIT(17)
411a2b7f194SVignesh R #define SNOR_HWCAPS_PP_1_4_4	BIT(18)
412a2b7f194SVignesh R #define SNOR_HWCAPS_PP_4_4_4	BIT(19)
413a2b7f194SVignesh R 
414a2b7f194SVignesh R #define SNOR_HWCAPS_PP_OCTO	GENMASK(22, 20)
415a2b7f194SVignesh R #define SNOR_HWCAPS_PP_1_1_8	BIT(20)
416a2b7f194SVignesh R #define SNOR_HWCAPS_PP_1_8_8	BIT(21)
417a2b7f194SVignesh R #define SNOR_HWCAPS_PP_8_8_8	BIT(22)
418a2b7f194SVignesh R 
419a2b7f194SVignesh R /**
420a2b7f194SVignesh R  * spi_nor_scan() - scan the SPI NOR
421a2b7f194SVignesh R  * @nor:	the spi_nor structure
422a2b7f194SVignesh R  *
423a2b7f194SVignesh R  * The drivers can use this function to scan the SPI NOR.
424a2b7f194SVignesh R  * In the scanning, it will try to get all the necessary information to
425a2b7f194SVignesh R  * fill the mtd_info{} and the spi_nor{}.
426a2b7f194SVignesh R  *
427a2b7f194SVignesh R  * Return: 0 for success, others for failure.
428a2b7f194SVignesh R  */
429a2b7f194SVignesh R int spi_nor_scan(struct spi_nor *nor);
430a2b7f194SVignesh R 
431a2b7f194SVignesh R #endif
432