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