xref: /linux/drivers/mtd/spi-nor/core.h (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2005, Intec Automation Inc.
4  * Copyright (C) 2014, Freescale Semiconductor, Inc.
5  */
6 
7 #ifndef __LINUX_MTD_SPI_NOR_INTERNAL_H
8 #define __LINUX_MTD_SPI_NOR_INTERNAL_H
9 
10 #include "sfdp.h"
11 
12 #define SPI_NOR_MAX_ID_LEN	6
13 /*
14  * 256 bytes is a sane default for most older flashes. Newer flashes will
15  * have the page size defined within their SFDP tables.
16  */
17 #define SPI_NOR_DEFAULT_PAGE_SIZE 256
18 #define SPI_NOR_DEFAULT_N_BANKS 1
19 #define SPI_NOR_DEFAULT_SECTOR_SIZE SZ_64K
20 
21 /* Standard SPI NOR flash operations. */
22 #define SPI_NOR_READID_OP(naddr, ndummy, buf, len)			\
23 	SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDID, 0),			\
24 		   SPI_MEM_OP_ADDR(naddr, 0, 0),			\
25 		   SPI_MEM_OP_DUMMY(ndummy, 0),				\
26 		   SPI_MEM_OP_DATA_IN(len, buf, 0))
27 
28 #define SPI_NOR_WREN_OP							\
29 	SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WREN, 0),			\
30 		   SPI_MEM_OP_NO_ADDR,					\
31 		   SPI_MEM_OP_NO_DUMMY,					\
32 		   SPI_MEM_OP_NO_DATA)
33 
34 #define SPI_NOR_WRDI_OP							\
35 	SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRDI, 0),			\
36 		   SPI_MEM_OP_NO_ADDR,					\
37 		   SPI_MEM_OP_NO_DUMMY,					\
38 		   SPI_MEM_OP_NO_DATA)
39 
40 #define SPI_NOR_RDSR_OP(buf)						\
41 	SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR, 0),			\
42 		   SPI_MEM_OP_NO_ADDR,					\
43 		   SPI_MEM_OP_NO_DUMMY,					\
44 		   SPI_MEM_OP_DATA_IN(1, buf, 0))
45 
46 #define SPI_NOR_WRSR_OP(buf, len)					\
47 	SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR, 0),			\
48 		   SPI_MEM_OP_NO_ADDR,					\
49 		   SPI_MEM_OP_NO_DUMMY,					\
50 		   SPI_MEM_OP_DATA_OUT(len, buf, 0))
51 
52 #define SPI_NOR_RDSR2_OP(buf)						\
53 	SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR2, 0),			\
54 		   SPI_MEM_OP_NO_ADDR,					\
55 		   SPI_MEM_OP_NO_DUMMY,					\
56 		   SPI_MEM_OP_DATA_OUT(1, buf, 0))
57 
58 #define SPI_NOR_WRSR2_OP(buf)						\
59 	SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR2, 0),			\
60 		   SPI_MEM_OP_NO_ADDR,					\
61 		   SPI_MEM_OP_NO_DUMMY,					\
62 		   SPI_MEM_OP_DATA_OUT(1, buf, 0))
63 
64 #define SPI_NOR_RDCR_OP(buf)						\
65 	SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDCR, 0),			\
66 		   SPI_MEM_OP_NO_ADDR,					\
67 		   SPI_MEM_OP_NO_DUMMY,					\
68 		   SPI_MEM_OP_DATA_IN(1, buf, 0))
69 
70 #define SPI_NOR_EN4B_EX4B_OP(enable)					\
71 	SPI_MEM_OP(SPI_MEM_OP_CMD(enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B, 0),	\
72 		   SPI_MEM_OP_NO_ADDR,					\
73 		   SPI_MEM_OP_NO_DUMMY,					\
74 		   SPI_MEM_OP_NO_DATA)
75 
76 #define SPI_NOR_BRWR_OP(buf)						\
77 	SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_BRWR, 0),			\
78 		   SPI_MEM_OP_NO_ADDR,					\
79 		   SPI_MEM_OP_NO_DUMMY,					\
80 		   SPI_MEM_OP_DATA_OUT(1, buf, 0))
81 
82 #define SPI_NOR_GBULK_OP						\
83 	SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_GBULK, 0),			\
84 		   SPI_MEM_OP_NO_ADDR,					\
85 		   SPI_MEM_OP_NO_DUMMY,					\
86 		   SPI_MEM_OP_NO_DATA)
87 
88 #define SPI_NOR_DIE_ERASE_OP(opcode, addr_nbytes, addr, dice)		\
89 	SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 0),				\
90 		   SPI_MEM_OP_ADDR(dice ? addr_nbytes : 0, addr, 0),	\
91 		   SPI_MEM_OP_NO_DUMMY,					\
92 		   SPI_MEM_OP_NO_DATA)
93 
94 #define SPI_NOR_SECTOR_ERASE_OP(opcode, addr_nbytes, addr)		\
95 	SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 0),				\
96 		   SPI_MEM_OP_ADDR(addr_nbytes, addr, 0),		\
97 		   SPI_MEM_OP_NO_DUMMY,					\
98 		   SPI_MEM_OP_NO_DATA)
99 
100 #define SPI_NOR_READ_OP(opcode)						\
101 	SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 0),				\
102 		   SPI_MEM_OP_ADDR(3, 0, 0),				\
103 		   SPI_MEM_OP_DUMMY(1, 0),				\
104 		   SPI_MEM_OP_DATA_IN(2, NULL, 0))
105 
106 #define SPI_NOR_PP_OP(opcode)						\
107 	SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 0),				\
108 		   SPI_MEM_OP_ADDR(3, 0, 0),				\
109 		   SPI_MEM_OP_NO_DUMMY,					\
110 		   SPI_MEM_OP_DATA_OUT(2, NULL, 0))
111 
112 #define SPINOR_SRSTEN_OP						\
113 	SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_SRSTEN, 0),			\
114 		   SPI_MEM_OP_NO_DUMMY,					\
115 		   SPI_MEM_OP_NO_ADDR,					\
116 		   SPI_MEM_OP_NO_DATA)
117 
118 #define SPINOR_SRST_OP							\
119 	SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_SRST, 0),			\
120 		   SPI_MEM_OP_NO_DUMMY,					\
121 		   SPI_MEM_OP_NO_ADDR,					\
122 		   SPI_MEM_OP_NO_DATA)
123 
124 /* Keep these in sync with the list in debugfs.c */
125 enum spi_nor_option_flags {
126 	SNOR_F_HAS_SR_TB	= BIT(0),
127 	SNOR_F_NO_OP_CHIP_ERASE	= BIT(1),
128 	SNOR_F_BROKEN_RESET	= BIT(2),
129 	SNOR_F_4B_OPCODES	= BIT(3),
130 	SNOR_F_HAS_4BAIT	= BIT(4),
131 	SNOR_F_HAS_LOCK		= BIT(5),
132 	SNOR_F_HAS_16BIT_SR	= BIT(6),
133 	SNOR_F_NO_READ_CR	= BIT(7),
134 	SNOR_F_HAS_SR_TB_BIT6	= BIT(8),
135 	SNOR_F_HAS_4BIT_BP      = BIT(9),
136 	SNOR_F_HAS_SR_BP3_BIT6  = BIT(10),
137 	SNOR_F_IO_MODE_EN_VOLATILE = BIT(11),
138 	SNOR_F_SOFT_RESET	= BIT(12),
139 	SNOR_F_SWP_IS_VOLATILE	= BIT(13),
140 	SNOR_F_RWW		= BIT(14),
141 	SNOR_F_ECC		= BIT(15),
142 	SNOR_F_NO_WP		= BIT(16),
143 	SNOR_F_SWAP16		= BIT(17),
144 	SNOR_F_HAS_SR2_CMP_BIT6	= BIT(18),
145 };
146 
147 struct spi_nor_read_command {
148 	u8			num_mode_clocks;
149 	u8			num_wait_states;
150 	u8			opcode;
151 	enum spi_nor_protocol	proto;
152 };
153 
154 struct spi_nor_pp_command {
155 	u8			opcode;
156 	enum spi_nor_protocol	proto;
157 };
158 
159 enum spi_nor_read_command_index {
160 	SNOR_CMD_READ,
161 	SNOR_CMD_READ_FAST,
162 	SNOR_CMD_READ_1_1_1_DTR,
163 
164 	/* Dual SPI */
165 	SNOR_CMD_READ_1_1_2,
166 	SNOR_CMD_READ_1_2_2,
167 	SNOR_CMD_READ_2_2_2,
168 	SNOR_CMD_READ_1_2_2_DTR,
169 
170 	/* Quad SPI */
171 	SNOR_CMD_READ_1_1_4,
172 	SNOR_CMD_READ_1_4_4,
173 	SNOR_CMD_READ_4_4_4,
174 	SNOR_CMD_READ_1_4_4_DTR,
175 
176 	/* Octal SPI */
177 	SNOR_CMD_READ_1_1_8,
178 	SNOR_CMD_READ_1_8_8,
179 	SNOR_CMD_READ_8_8_8,
180 	SNOR_CMD_READ_1_8_8_DTR,
181 	SNOR_CMD_READ_8_8_8_DTR,
182 
183 	SNOR_CMD_READ_MAX
184 };
185 
186 enum spi_nor_pp_command_index {
187 	SNOR_CMD_PP,
188 
189 	/* Quad SPI */
190 	SNOR_CMD_PP_1_1_4,
191 	SNOR_CMD_PP_1_4_4,
192 	SNOR_CMD_PP_4_4_4,
193 
194 	/* Octal SPI */
195 	SNOR_CMD_PP_1_1_8,
196 	SNOR_CMD_PP_1_8_8,
197 	SNOR_CMD_PP_8_8_8,
198 	SNOR_CMD_PP_8_8_8_DTR,
199 
200 	SNOR_CMD_PP_MAX
201 };
202 
203 /**
204  * struct spi_nor_erase_type - Structure to describe a SPI NOR erase type
205  * @size:		the size of the sector/block erased by the erase type.
206  *			JEDEC JESD216B imposes erase sizes to be a power of 2.
207  * @size_shift:		@size is a power of 2, the shift is stored in
208  *			@size_shift.
209  * @size_mask:		the size mask based on @size_shift.
210  * @opcode:		the SPI command op code to erase the sector/block.
211  * @idx:		Erase Type index as sorted in the Basic Flash Parameter
212  *			Table. It will be used to synchronize the supported
213  *			Erase Types with the ones identified in the SFDP
214  *			optional tables.
215  */
216 struct spi_nor_erase_type {
217 	u32	size;
218 	u32	size_shift;
219 	u32	size_mask;
220 	u8	opcode;
221 	u8	idx;
222 };
223 
224 /**
225  * struct spi_nor_erase_command - Used for non-uniform erases
226  * The structure is used to describe a list of erase commands to be executed
227  * once we validate that the erase can be performed. The elements in the list
228  * are run-length encoded.
229  * @list:		for inclusion into the list of erase commands.
230  * @count:		how many times the same erase command should be
231  *			consecutively used.
232  * @size:		the size of the sector/block erased by the command.
233  * @opcode:		the SPI command op code to erase the sector/block.
234  */
235 struct spi_nor_erase_command {
236 	struct list_head	list;
237 	u32			count;
238 	u32			size;
239 	u8			opcode;
240 };
241 
242 /**
243  * struct spi_nor_erase_region - Structure to describe a SPI NOR erase region
244  * @offset:		the offset in the data array of erase region start.
245  * @size:		the size of the region in bytes.
246  * @erase_mask:		bitmask to indicate all the supported erase commands
247  *			inside this region. The erase types are sorted in
248  *			ascending order with the smallest Erase Type size being
249  *			at BIT(0).
250  * @overlaid:		determine if this region is overlaid.
251  */
252 struct spi_nor_erase_region {
253 	u64		offset;
254 	u64		size;
255 	u8		erase_mask;
256 	bool		overlaid;
257 };
258 
259 #define SNOR_ERASE_TYPE_MAX	4
260 
261 /**
262  * struct spi_nor_erase_map - Structure to describe the SPI NOR erase map
263  * @regions:		array of erase regions. The regions are consecutive in
264  *			address space. Walking through the regions is done
265  *			incrementally.
266  * @uniform_region:	a pre-allocated erase region for SPI NOR with a uniform
267  *			sector size (legacy implementation).
268  * @erase_type:		an array of erase types shared by all the regions.
269  *			The erase types are sorted in ascending order, with the
270  *			smallest Erase Type size being the first member in the
271  *			erase_type array.
272  * @n_regions:		number of erase regions.
273  */
274 struct spi_nor_erase_map {
275 	struct spi_nor_erase_region	*regions;
276 	struct spi_nor_erase_region	uniform_region;
277 	struct spi_nor_erase_type	erase_type[SNOR_ERASE_TYPE_MAX];
278 	unsigned int			n_regions;
279 };
280 
281 /**
282  * struct spi_nor_locking_ops - SPI NOR locking methods
283  * @lock:	lock a region of the SPI NOR, never locks more than what is
284  *		requested, ie. may lock less.
285  * @unlock:	unlock a region of the SPI NOR, may unlock more than what is
286  *		requested.
287  * @is_locked:	check if a region of the SPI NOR is completely locked, returns
288  *		false otherwise. This feedback may be misleading because users
289  *		may get an "unlocked" status even though a subpart of the region
290  *		is effectively locked.
291  *
292  * If in doubt during development, check-out the debugfs output which tries to
293  * be more user friendly.
294  */
295 struct spi_nor_locking_ops {
296 	int (*lock)(struct spi_nor *nor, loff_t ofs, u64 len);
297 	int (*unlock)(struct spi_nor *nor, loff_t ofs, u64 len);
298 	int (*is_locked)(struct spi_nor *nor, loff_t ofs, u64 len);
299 };
300 
301 /**
302  * struct spi_nor_otp_organization - Structure to describe the SPI NOR OTP regions
303  * @len:	size of one OTP region in bytes.
304  * @base:	start address of the OTP area.
305  * @offset:	offset between consecutive OTP regions if there are more
306  *              than one.
307  * @n_regions:	number of individual OTP regions.
308  */
309 struct spi_nor_otp_organization {
310 	size_t len;
311 	loff_t base;
312 	loff_t offset;
313 	unsigned int n_regions;
314 };
315 
316 /**
317  * struct spi_nor_otp_ops - SPI NOR OTP methods
318  * @read:	read from the SPI NOR OTP area.
319  * @write:	write to the SPI NOR OTP area.
320  * @lock:	lock an OTP region.
321  * @erase:	erase an OTP region.
322  * @is_locked:	check if an OTP region of the SPI NOR is locked.
323  */
324 struct spi_nor_otp_ops {
325 	int (*read)(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf);
326 	int (*write)(struct spi_nor *nor, loff_t addr, size_t len,
327 		     const u8 *buf);
328 	int (*lock)(struct spi_nor *nor, unsigned int region);
329 	int (*erase)(struct spi_nor *nor, loff_t addr);
330 	int (*is_locked)(struct spi_nor *nor, unsigned int region);
331 };
332 
333 /**
334  * struct spi_nor_otp - SPI NOR OTP grouping structure
335  * @org:	OTP region organization
336  * @ops:	OTP access ops
337  */
338 struct spi_nor_otp {
339 	const struct spi_nor_otp_organization *org;
340 	const struct spi_nor_otp_ops *ops;
341 };
342 
343 /**
344  * struct spi_nor_flash_parameter - SPI NOR flash parameters and settings.
345  * Includes legacy flash parameters and settings that can be overwritten
346  * by the spi_nor_fixups hooks, or dynamically when parsing the JESD216
347  * Serial Flash Discoverable Parameters (SFDP) tables.
348  *
349  * @bank_size:		the flash memory bank density in bytes.
350  * @size:		the total flash memory density in bytes.
351  * @writesize		Minimal writable flash unit size. Defaults to 1. Set to
352  *			ECC unit size for ECC-ed flashes.
353  * @page_size:		the page size of the SPI NOR flash memory.
354  * @addr_nbytes:	number of address bytes to send.
355  * @addr_mode_nbytes:	number of address bytes of current address mode. Useful
356  *			when the flash operates with 4B opcodes but needs the
357  *			internal address mode for opcodes that don't have a 4B
358  *			opcode correspondent.
359  * @rdsr_dummy:		dummy cycles needed for Read Status Register command
360  *			in octal DTR mode.
361  * @rdsr_addr_nbytes:	dummy address bytes needed for Read Status Register
362  *			command in octal DTR mode.
363  * @n_banks:		number of banks.
364  * @n_dice:		number of dice in the flash memory.
365  * @die_erase_opcode:	die erase opcode. Defaults to SPINOR_OP_CHIP_ERASE.
366  * @vreg_offset:	volatile register offset for each die.
367  * @hwcaps:		describes the read and page program hardware
368  *			capabilities.
369  * @reads:		read capabilities ordered by priority: the higher index
370  *                      in the array, the higher priority.
371  * @page_programs:	page program capabilities ordered by priority: the
372  *                      higher index in the array, the higher priority.
373  * @erase_map:		the erase map parsed from the SFDP Sector Map Parameter
374  *                      Table.
375  * @otp:		SPI NOR OTP info.
376  * @set_octal_dtr:	enables or disables SPI NOR octal DTR mode.
377  * @quad_enable:	enables SPI NOR quad mode.
378  * @set_4byte_addr_mode: puts the SPI NOR in 4 byte addressing mode.
379  * @ready:		(optional) flashes might use a different mechanism
380  *			than reading the status register to indicate they
381  *			are ready for a new command
382  * @locking_ops:	SPI NOR locking methods.
383  * @priv:		flash's private data.
384  */
385 struct spi_nor_flash_parameter {
386 	u64				bank_size;
387 	u64				size;
388 	u32				writesize;
389 	u32				page_size;
390 	u8				addr_nbytes;
391 	u8				addr_mode_nbytes;
392 	u8				rdsr_dummy;
393 	u8				rdsr_addr_nbytes;
394 	u8				n_banks;
395 	u8				n_dice;
396 	u8				die_erase_opcode;
397 	u32				*vreg_offset;
398 
399 	struct spi_nor_hwcaps		hwcaps;
400 	struct spi_nor_read_command	reads[SNOR_CMD_READ_MAX];
401 	struct spi_nor_pp_command	page_programs[SNOR_CMD_PP_MAX];
402 
403 	struct spi_nor_erase_map        erase_map;
404 	struct spi_nor_otp		otp;
405 
406 	int (*set_octal_dtr)(struct spi_nor *nor, bool enable);
407 	int (*quad_enable)(struct spi_nor *nor);
408 	int (*set_4byte_addr_mode)(struct spi_nor *nor, bool enable);
409 	int (*ready)(struct spi_nor *nor);
410 
411 	const struct spi_nor_locking_ops *locking_ops;
412 	void *priv;
413 };
414 
415 /**
416  * struct spi_nor_fixups - SPI NOR fixup hooks
417  * @default_init: called after default flash parameters init. Used to tweak
418  *                flash parameters when information provided by the flash_info
419  *                table is incomplete or wrong.
420  * @post_bfpt: called after the BFPT table has been parsed
421  * @smpt_read_dummy: called during SMPT table is being parsed. Used to fix the
422  *                   number of dummy cycles in read register ops.
423  * @smpt_map_id: called after map ID in SMPT table has been determined for the
424  *               case the map ID is wrong and needs to be fixed.
425  * @post_sfdp: called after SFDP has been parsed (is not called for SPI NORs
426  *             that do not support RDSFDP). Typically used to tweak various
427  *             parameters that could not be extracted by other means (i.e.
428  *             when information provided by the SFDP/flash_info tables are
429  *             incomplete or wrong).
430  * @late_init: used to initialize flash parameters that are not declared in the
431  *             JESD216 SFDP standard, or where SFDP tables not defined at all.
432  *             Will replace the default_init() hook.
433  *
434  * Those hooks can be used to tweak the SPI NOR configuration when the SFDP
435  * table is broken or not available.
436  */
437 struct spi_nor_fixups {
438 	void (*default_init)(struct spi_nor *nor);
439 	int (*post_bfpt)(struct spi_nor *nor,
440 			 const struct sfdp_parameter_header *bfpt_header,
441 			 const struct sfdp_bfpt *bfpt);
442 	void (*smpt_read_dummy)(const struct spi_nor *nor, u8 *read_dummy);
443 	void (*smpt_map_id)(const struct spi_nor *nor, u8 *map_id);
444 	int (*post_sfdp)(struct spi_nor *nor);
445 	int (*late_init)(struct spi_nor *nor);
446 };
447 
448 /**
449  * struct spi_nor_id - SPI NOR flash ID.
450  *
451  * @bytes: the bytes returned by the flash when issuing command 9F. Typically,
452  *         the first byte is the manufacturer ID code (see JEP106) and the next
453  *         two bytes are a flash part specific ID.
454  * @len:   the number of bytes of ID.
455  */
456 struct spi_nor_id {
457 	const u8 *bytes;
458 	u8 len;
459 };
460 
461 /**
462  * struct flash_info - SPI NOR flash_info entry.
463  * @id:   pointer to struct spi_nor_id or NULL, which means "no ID" (mostly
464  *        older chips).
465  * @name: (obsolete) the name of the flash. Do not set it for new additions.
466  * @size:           the size of the flash in bytes. The flash size is one
467  *                  property parsed by the SFDP. We use it as an indicator
468  *                  whether we need SFDP parsing for a particular flash.
469  *                  I.e. non-legacy flash entries in flash_info will have
470  *                  a size of zero iff SFDP should be used.
471  * @sector_size:    (optional) the size listed here is what works with
472  *                  SPINOR_OP_SE, which isn't necessarily called a "sector" by
473  *                  the vendor. Defaults to 64k.
474  * @n_banks:        (optional) the number of banks. Defaults to 1.
475  * @page_size:      (optional) the flash's page size. Defaults to 256.
476  * @addr_nbytes:    number of address bytes to send.
477  *
478  * @flags:          flags that indicate support that is not defined by the
479  *                  JESD216 standard in its SFDP tables. Flag meanings:
480  *   SPI_NOR_HAS_LOCK:        flash supports lock/unlock via SR
481  *   SPI_NOR_HAS_TB:          flash SR has Top/Bottom (TB) protect bit. Must be
482  *                            used with SPI_NOR_HAS_LOCK.
483  *   SPI_NOR_TB_SR_BIT6:      Top/Bottom (TB) is bit 6 of status register.
484  *                            Must be used with SPI_NOR_HAS_TB.
485  *   SPI_NOR_4BIT_BP:         flash SR has 4 bit fields (BP0-3) for block
486  *                            protection.
487  *   SPI_NOR_BP3_SR_BIT6:     BP3 is bit 6 of status register. Must be used with
488  *                            SPI_NOR_4BIT_BP.
489  *   SPI_NOR_SWP_IS_VOLATILE: flash has volatile software write protection bits.
490  *                            Usually these will power-up in a write-protected
491  *                            state.
492  *   SPI_NOR_NO_ERASE:        no erase command needed.
493  *   SPI_NOR_QUAD_PP:         flash supports Quad Input Page Program.
494  *   SPI_NOR_RWW:             flash supports reads while write.
495  *   SPI_NOR_HAS_CMP:         flash SR2 has complement (CMP) protect bit. Must
496  *                            be used with SPI_NOR_HAS_LOCK.
497  *
498  * @no_sfdp_flags:  flags that indicate support that can be discovered via SFDP.
499  *                  Used when SFDP tables are not defined in the flash. These
500  *                  flags are used together with the SPI_NOR_SKIP_SFDP flag.
501  *   SPI_NOR_SKIP_SFDP:       skip parsing of SFDP tables.
502  *   SECT_4K:                 SPINOR_OP_BE_4K works uniformly.
503  *   SPI_NOR_DUAL_READ:       flash supports Dual Read.
504  *   SPI_NOR_QUAD_READ:       flash supports Quad Read.
505  *   SPI_NOR_OCTAL_READ:      flash supports Octal Read.
506  *   SPI_NOR_OCTAL_DTR_READ:  flash supports octal DTR Read.
507  *   SPI_NOR_OCTAL_DTR_PP:    flash supports Octal DTR Page Program.
508  *
509  * @fixup_flags:    flags that indicate support that can be discovered via SFDP
510  *                  ideally, but can not be discovered for this particular flash
511  *                  because the SFDP table that indicates this support is not
512  *                  defined by the flash. In case the table for this support is
513  *                  defined but has wrong values, one should instead use a
514  *                  post_sfdp() hook to set the SNOR_F equivalent flag.
515  *
516  *   SPI_NOR_4B_OPCODES:      use dedicated 4byte address op codes to support
517  *                            memory size above 128Mib.
518  *   SPI_NOR_IO_MODE_EN_VOLATILE: flash enables the best available I/O mode
519  *                            via a volatile bit.
520  * @mfr_flags:      manufacturer private flags. Used in the manufacturer fixup
521  *                  hooks to differentiate support between flashes of the same
522  *                  manufacturer.
523  * @otp_org:        flash's OTP organization.
524  * @fixups:         part specific fixup hooks.
525  */
526 struct flash_info {
527 	char *name;
528 	const struct spi_nor_id *id;
529 	size_t size;
530 	unsigned sector_size;
531 	u16 page_size;
532 	u8 n_banks;
533 	u8 addr_nbytes;
534 
535 	u16 flags;
536 #define SPI_NOR_HAS_LOCK		BIT(0)
537 #define SPI_NOR_HAS_TB			BIT(1)
538 #define SPI_NOR_TB_SR_BIT6		BIT(2)
539 #define SPI_NOR_4BIT_BP			BIT(3)
540 #define SPI_NOR_BP3_SR_BIT6		BIT(4)
541 #define SPI_NOR_SWP_IS_VOLATILE		BIT(5)
542 #define SPI_NOR_NO_ERASE		BIT(6)
543 #define SPI_NOR_QUAD_PP			BIT(8)
544 #define SPI_NOR_RWW			BIT(9)
545 #define SPI_NOR_HAS_CMP			BIT(10)
546 
547 	u8 no_sfdp_flags;
548 #define SPI_NOR_SKIP_SFDP		BIT(0)
549 #define SECT_4K				BIT(1)
550 #define SPI_NOR_DUAL_READ		BIT(3)
551 #define SPI_NOR_QUAD_READ		BIT(4)
552 #define SPI_NOR_OCTAL_READ		BIT(5)
553 #define SPI_NOR_OCTAL_DTR_READ		BIT(6)
554 #define SPI_NOR_OCTAL_DTR_PP		BIT(7)
555 
556 	u8 fixup_flags;
557 #define SPI_NOR_4B_OPCODES		BIT(0)
558 #define SPI_NOR_IO_MODE_EN_VOLATILE	BIT(1)
559 
560 	u8 mfr_flags;
561 
562 	const struct spi_nor_otp_organization *otp;
563 	const struct spi_nor_fixups *fixups;
564 };
565 
566 #define SNOR_ID(...)							\
567 	(&(const struct spi_nor_id){					\
568 		.bytes = (const u8[]){ __VA_ARGS__ },			\
569 		.len = sizeof((u8[]){ __VA_ARGS__ }),			\
570 	})
571 
572 #define SNOR_OTP(_len, _n_regions, _base, _offset)			\
573 	(&(const struct spi_nor_otp_organization){			\
574 		.len = (_len),						\
575 		.base = (_base),					\
576 		.offset = (_offset),					\
577 		.n_regions = (_n_regions),				\
578 	})
579 
580 /**
581  * struct spi_nor_manufacturer - SPI NOR manufacturer object
582  * @name: manufacturer name
583  * @parts: array of parts supported by this manufacturer
584  * @nparts: number of entries in the parts array
585  * @fixups: hooks called at various points in time during spi_nor_scan()
586  */
587 struct spi_nor_manufacturer {
588 	const char *name;
589 	const struct flash_info *parts;
590 	unsigned int nparts;
591 	const struct spi_nor_fixups *fixups;
592 };
593 
594 /**
595  * struct sfdp - SFDP data
596  * @num_dwords: number of entries in the dwords array
597  * @dwords: array of double words of the SFDP data
598  */
599 struct sfdp {
600 	size_t	num_dwords;
601 	u32	*dwords;
602 };
603 
604 /* Manufacturer drivers. */
605 extern const struct spi_nor_manufacturer spi_nor_atmel;
606 extern const struct spi_nor_manufacturer spi_nor_eon;
607 extern const struct spi_nor_manufacturer spi_nor_esmt;
608 extern const struct spi_nor_manufacturer spi_nor_everspin;
609 extern const struct spi_nor_manufacturer spi_nor_gigadevice;
610 extern const struct spi_nor_manufacturer spi_nor_intel;
611 extern const struct spi_nor_manufacturer spi_nor_issi;
612 extern const struct spi_nor_manufacturer spi_nor_macronix;
613 extern const struct spi_nor_manufacturer spi_nor_micron;
614 extern const struct spi_nor_manufacturer spi_nor_st;
615 extern const struct spi_nor_manufacturer spi_nor_spansion;
616 extern const struct spi_nor_manufacturer spi_nor_sst;
617 extern const struct spi_nor_manufacturer spi_nor_winbond;
618 extern const struct spi_nor_manufacturer spi_nor_xmc;
619 
620 extern const struct attribute_group *spi_nor_sysfs_groups[];
621 
622 void spi_nor_spimem_setup_op(const struct spi_nor *nor,
623 			     struct spi_mem_op *op,
624 			     const enum spi_nor_protocol proto);
625 int spi_nor_write_enable(struct spi_nor *nor);
626 int spi_nor_write_disable(struct spi_nor *nor);
627 int spi_nor_set_4byte_addr_mode_en4b_ex4b(struct spi_nor *nor, bool enable);
628 int spi_nor_set_4byte_addr_mode_wren_en4b_ex4b(struct spi_nor *nor,
629 					       bool enable);
630 int spi_nor_set_4byte_addr_mode_brwr(struct spi_nor *nor, bool enable);
631 int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable);
632 int spi_nor_wait_till_ready(struct spi_nor *nor);
633 int spi_nor_global_block_unlock(struct spi_nor *nor);
634 int spi_nor_prep_and_lock(struct spi_nor *nor);
635 void spi_nor_unlock_and_unprep(struct spi_nor *nor);
636 int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor);
637 int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor);
638 int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor);
639 int spi_nor_read_id(struct spi_nor *nor, u8 naddr, u8 ndummy, u8 *id,
640 		    enum spi_nor_protocol reg_proto);
641 int spi_nor_read_sr(struct spi_nor *nor, u8 *sr);
642 int spi_nor_sr_ready(struct spi_nor *nor);
643 int spi_nor_read_cr(struct spi_nor *nor, u8 *cr);
644 int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len);
645 int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1);
646 int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr);
647 int spi_nor_write_sr_cr_and_check(struct spi_nor *nor, const u8 *regs);
648 
649 ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
650 			  u8 *buf);
651 ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
652 			   const u8 *buf);
653 int spi_nor_read_any_reg(struct spi_nor *nor, struct spi_mem_op *op,
654 			 enum spi_nor_protocol proto);
655 int spi_nor_write_any_volatile_reg(struct spi_nor *nor, struct spi_mem_op *op,
656 				   enum spi_nor_protocol proto);
657 int spi_nor_erase_sector(struct spi_nor *nor, u32 addr);
658 
659 int spi_nor_otp_read_secr(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf);
660 int spi_nor_otp_write_secr(struct spi_nor *nor, loff_t addr, size_t len,
661 			   const u8 *buf);
662 int spi_nor_otp_erase_secr(struct spi_nor *nor, loff_t addr);
663 int spi_nor_otp_lock_sr2(struct spi_nor *nor, unsigned int region);
664 int spi_nor_otp_is_locked_sr2(struct spi_nor *nor, unsigned int region);
665 
666 int spi_nor_hwcaps_read2cmd(u32 hwcaps);
667 int spi_nor_hwcaps_pp2cmd(u32 hwcaps);
668 u8 spi_nor_convert_3to4_read(u8 opcode);
669 void spi_nor_set_read_settings(struct spi_nor_read_command *read,
670 			       u8 num_mode_clocks,
671 			       u8 num_wait_states,
672 			       u8 opcode,
673 			       enum spi_nor_protocol proto);
674 void spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, u8 opcode,
675 			     enum spi_nor_protocol proto);
676 
677 void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size,
678 			    u8 opcode);
679 void spi_nor_mask_erase_type(struct spi_nor_erase_type *erase);
680 void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
681 				    u8 erase_mask, u64 flash_size);
682 
683 int spi_nor_post_bfpt_fixups(struct spi_nor *nor,
684 			     const struct sfdp_parameter_header *bfpt_header,
685 			     const struct sfdp_bfpt *bfpt);
686 
687 void spi_nor_init_default_locking_ops(struct spi_nor *nor);
688 bool spi_nor_has_default_locking_ops(struct spi_nor *nor);
689 void spi_nor_try_unlock_all(struct spi_nor *nor);
690 void spi_nor_cache_sr_lock_bits(struct spi_nor *nor, u8 *sr);
691 void spi_nor_set_mtd_locking_ops(struct spi_nor *nor);
692 void spi_nor_set_mtd_otp_ops(struct spi_nor *nor);
693 
694 int spi_nor_controller_ops_read_reg(struct spi_nor *nor, u8 opcode,
695 				    u8 *buf, size_t len);
696 int spi_nor_controller_ops_write_reg(struct spi_nor *nor, u8 opcode,
697 				     const u8 *buf, size_t len);
698 
699 int spi_nor_check_sfdp_signature(struct spi_nor *nor);
700 int spi_nor_parse_sfdp(struct spi_nor *nor);
701 
702 static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
703 {
704 	return container_of(mtd, struct spi_nor, mtd);
705 }
706 
707 /**
708  * spi_nor_needs_sfdp() - returns true if SFDP parsing is used for this flash.
709  *
710  * Return: true if SFDP parsing is needed
711  */
712 static inline bool spi_nor_needs_sfdp(const struct spi_nor *nor)
713 {
714 	/*
715 	 * The flash size is one property parsed by the SFDP. We use it as an
716 	 * indicator whether we need SFDP parsing for a particular flash. I.e.
717 	 * non-legacy flash entries in flash_info will have a size of zero iff
718 	 * SFDP should be used.
719 	 */
720 	return !nor->info->size;
721 }
722 
723 u64 spi_nor_get_min_prot_length_sr(struct spi_nor *nor);
724 void spi_nor_get_locked_range_sr(struct spi_nor *nor, const u8 *sr, loff_t *ofs, u64 *len);
725 bool spi_nor_is_locked_sr(struct spi_nor *nor, loff_t ofs, u64 len, const u8 *sr);
726 
727 #ifdef CONFIG_DEBUG_FS
728 void spi_nor_debugfs_register(struct spi_nor *nor);
729 void spi_nor_debugfs_shutdown(void);
730 #else
731 static inline void spi_nor_debugfs_register(struct spi_nor *nor) {}
732 static inline void spi_nor_debugfs_shutdown(void) {}
733 #endif
734 
735 #endif /* __LINUX_MTD_SPI_NOR_INTERNAL_H */
736