xref: /openbmc/u-boot/include/linux/mtd/spi-nor.h (revision 591e1cf026383b6774449ad6edc355211dc85708)
17aeedac0SVignesh R // SPDX-License-Identifier: GPL-2.0
27aeedac0SVignesh R /*
37aeedac0SVignesh R  * Copyright (C) 2014 Freescale Semiconductor, Inc.
47aeedac0SVignesh R  * Synced from Linux v4.19
57aeedac0SVignesh R  */
67aeedac0SVignesh R 
77aeedac0SVignesh R #ifndef __LINUX_MTD_SPI_NOR_H
87aeedac0SVignesh R #define __LINUX_MTD_SPI_NOR_H
97aeedac0SVignesh R 
107aeedac0SVignesh R #include <linux/bitops.h>
117aeedac0SVignesh R #include <linux/mtd/cfi.h>
127aeedac0SVignesh R #include <linux/mtd/mtd.h>
137aeedac0SVignesh R 
147aeedac0SVignesh R /*
157aeedac0SVignesh R  * Manufacturer IDs
167aeedac0SVignesh R  *
177aeedac0SVignesh R  * The first byte returned from the flash after sending opcode SPINOR_OP_RDID.
187aeedac0SVignesh R  * Sometimes these are the same as CFI IDs, but sometimes they aren't.
197aeedac0SVignesh R  */
207aeedac0SVignesh R #define SNOR_MFR_ATMEL		CFI_MFR_ATMEL
217aeedac0SVignesh R #define SNOR_MFR_GIGADEVICE	0xc8
227aeedac0SVignesh R #define SNOR_MFR_INTEL		CFI_MFR_INTEL
237aeedac0SVignesh R #define SNOR_MFR_ST		CFI_MFR_ST /* ST Micro <--> Micron */
247aeedac0SVignesh R #define SNOR_MFR_MICRON		CFI_MFR_MICRON /* ST Micro <--> Micron */
257aeedac0SVignesh R #define SNOR_MFR_MACRONIX	CFI_MFR_MACRONIX
267aeedac0SVignesh R #define SNOR_MFR_SPANSION	CFI_MFR_AMD
277aeedac0SVignesh R #define SNOR_MFR_SST		CFI_MFR_SST
287aeedac0SVignesh R #define SNOR_MFR_WINBOND	0xef /* Also used by some Spansion */
2900554b9bSChin-Ting Kuo #define SNOR_MFR_ISSI		0x9d
30cd800046SChin-Ting Kuo #define SNOR_MFR_CYPRESS	0x34
317aeedac0SVignesh R 
327aeedac0SVignesh R /*
337aeedac0SVignesh R  * Note on opcode nomenclature: some opcodes have a format like
347aeedac0SVignesh R  * SPINOR_OP_FUNCTION{4,}_x_y_z. The numbers x, y, and z stand for the number
357aeedac0SVignesh R  * of I/O lines used for the opcode, address, and data (respectively). The
367aeedac0SVignesh R  * FUNCTION has an optional suffix of '4', to represent an opcode which
377aeedac0SVignesh R  * requires a 4-byte (32-bit) address.
387aeedac0SVignesh R  */
397aeedac0SVignesh R 
407aeedac0SVignesh R /* Flash opcodes. */
417aeedac0SVignesh R #define SPINOR_OP_WREN		0x06	/* Write enable */
427aeedac0SVignesh R #define SPINOR_OP_RDSR		0x05	/* Read status register */
437aeedac0SVignesh R #define SPINOR_OP_WRSR		0x01	/* Write status register 1 byte */
447aeedac0SVignesh R #define SPINOR_OP_RDSR2		0x3f	/* Read status register 2 */
457aeedac0SVignesh R #define SPINOR_OP_WRSR2		0x3e	/* Write status register 2 */
467aeedac0SVignesh R #define SPINOR_OP_READ		0x03	/* Read data bytes (low frequency) */
477aeedac0SVignesh R #define SPINOR_OP_READ_FAST	0x0b	/* Read data bytes (high frequency) */
487aeedac0SVignesh R #define SPINOR_OP_READ_1_1_2	0x3b	/* Read data bytes (Dual Output SPI) */
497aeedac0SVignesh R #define SPINOR_OP_READ_1_2_2	0xbb	/* Read data bytes (Dual I/O SPI) */
507aeedac0SVignesh R #define SPINOR_OP_READ_1_1_4	0x6b	/* Read data bytes (Quad Output SPI) */
517aeedac0SVignesh R #define SPINOR_OP_READ_1_4_4	0xeb	/* Read data bytes (Quad I/O SPI) */
527aeedac0SVignesh R #define SPINOR_OP_PP		0x02	/* Page program (up to 256 bytes) */
537aeedac0SVignesh R #define SPINOR_OP_PP_1_1_4	0x32	/* Quad page program */
547aeedac0SVignesh R #define SPINOR_OP_PP_1_4_4	0x38	/* Quad page program */
557aeedac0SVignesh R #define SPINOR_OP_BE_4K		0x20	/* Erase 4KiB block */
567aeedac0SVignesh R #define SPINOR_OP_BE_4K_PMC	0xd7	/* Erase 4KiB block on PMC chips */
577aeedac0SVignesh R #define SPINOR_OP_BE_32K	0x52	/* Erase 32KiB block */
587aeedac0SVignesh R #define SPINOR_OP_CHIP_ERASE	0xc7	/* Erase whole flash chip */
597aeedac0SVignesh R #define SPINOR_OP_SE		0xd8	/* Sector erase (usually 64KiB) */
607aeedac0SVignesh R #define SPINOR_OP_RDID		0x9f	/* Read JEDEC ID */
617aeedac0SVignesh R #define SPINOR_OP_RDSFDP	0x5a	/* Read SFDP */
627aeedac0SVignesh R #define SPINOR_OP_RDCR		0x35	/* Read configuration register */
637aeedac0SVignesh R #define SPINOR_OP_RDFSR		0x70	/* Read flag status register */
647aeedac0SVignesh R #define SPINOR_OP_CLFSR		0x50	/* Clear flag status register */
657aeedac0SVignesh R #define SPINOR_OP_RDEAR		0xc8	/* Read Extended Address Register */
667aeedac0SVignesh R #define SPINOR_OP_WREAR		0xc5	/* Write Extended Address Register */
677aeedac0SVignesh R 
687aeedac0SVignesh R /* 4-byte address opcodes - used on Spansion and some Macronix flashes. */
697aeedac0SVignesh R #define SPINOR_OP_READ_4B	0x13	/* Read data bytes (low frequency) */
707aeedac0SVignesh R #define SPINOR_OP_READ_FAST_4B	0x0c	/* Read data bytes (high frequency) */
717aeedac0SVignesh R #define SPINOR_OP_READ_1_1_2_4B	0x3c	/* Read data bytes (Dual Output SPI) */
727aeedac0SVignesh R #define SPINOR_OP_READ_1_2_2_4B	0xbc	/* Read data bytes (Dual I/O SPI) */
737aeedac0SVignesh R #define SPINOR_OP_READ_1_1_4_4B	0x6c	/* Read data bytes (Quad Output SPI) */
747aeedac0SVignesh R #define SPINOR_OP_READ_1_4_4_4B	0xec	/* Read data bytes (Quad I/O SPI) */
757aeedac0SVignesh R #define SPINOR_OP_PP_4B		0x12	/* Page program (up to 256 bytes) */
767aeedac0SVignesh R #define SPINOR_OP_PP_1_1_4_4B	0x34	/* Quad page program */
777aeedac0SVignesh R #define SPINOR_OP_PP_1_4_4_4B	0x3e	/* Quad page program */
787aeedac0SVignesh R #define SPINOR_OP_BE_4K_4B	0x21	/* Erase 4KiB block */
797aeedac0SVignesh R #define SPINOR_OP_BE_32K_4B	0x5c	/* Erase 32KiB block */
807aeedac0SVignesh R #define SPINOR_OP_SE_4B		0xdc	/* Sector erase (usually 64KiB) */
817aeedac0SVignesh R 
827aeedac0SVignesh R /* Double Transfer Rate opcodes - defined in JEDEC JESD216B. */
837aeedac0SVignesh R #define SPINOR_OP_READ_1_1_1_DTR	0x0d
847aeedac0SVignesh R #define SPINOR_OP_READ_1_2_2_DTR	0xbd
857aeedac0SVignesh R #define SPINOR_OP_READ_1_4_4_DTR	0xed
867aeedac0SVignesh R 
877aeedac0SVignesh R #define SPINOR_OP_READ_1_1_1_DTR_4B	0x0e
887aeedac0SVignesh R #define SPINOR_OP_READ_1_2_2_DTR_4B	0xbe
897aeedac0SVignesh R #define SPINOR_OP_READ_1_4_4_DTR_4B	0xee
907aeedac0SVignesh R 
917aeedac0SVignesh R /* Used for SST flashes only. */
927aeedac0SVignesh R #define SPINOR_OP_BP		0x02	/* Byte program */
937aeedac0SVignesh R #define SPINOR_OP_WRDI		0x04	/* Write disable */
947aeedac0SVignesh R #define SPINOR_OP_AAI_WP	0xad	/* Auto address increment word program */
95838fb1e6SChin-Ting Kuo #define SPINOR_OP_SST_RDNVCR	0xB5	/* Read nonvolatile configuration register */
96838fb1e6SChin-Ting Kuo #define SPINOR_OP_SST_WRNVCR	0xB1	/* Write nonvolatile configuration register */
97838fb1e6SChin-Ting Kuo #define SPINOR_SST_RST_HOLD_CTRL	BIT(4)
987aeedac0SVignesh R 
997aeedac0SVignesh R /* Used for S3AN flashes only */
1007aeedac0SVignesh R #define SPINOR_OP_XSE		0x50	/* Sector erase */
1017aeedac0SVignesh R #define SPINOR_OP_XPP		0x82	/* Page program */
1027aeedac0SVignesh R #define SPINOR_OP_XRDSR		0xd7	/* Read status register */
1037aeedac0SVignesh R 
1047aeedac0SVignesh R #define XSR_PAGESIZE		BIT(0)	/* Page size in Po2 or Linear */
1057aeedac0SVignesh R #define XSR_RDY			BIT(7)	/* Ready */
1067aeedac0SVignesh R 
1077aeedac0SVignesh R /* Used for Macronix and Winbond flashes. */
1087aeedac0SVignesh R #define SPINOR_OP_EN4B		0xb7	/* Enter 4-byte mode */
1097aeedac0SVignesh R #define SPINOR_OP_EX4B		0xe9	/* Exit 4-byte mode */
1107aeedac0SVignesh R 
111878b2ba4SChin-Ting Kuo #define SPINOR_OP_WINBOND_RDSR2		0x35
112878b2ba4SChin-Ting Kuo #define SPINOR_OP_WINBOND_WRSR2		0x31
113878b2ba4SChin-Ting Kuo 
1147aeedac0SVignesh R /* Used for Spansion flashes only. */
1157aeedac0SVignesh R #define SPINOR_OP_BRWR		0x17	/* Bank register write */
1168c927809SVignesh R #define SPINOR_OP_BRRD		0x16	/* Bank register read */
1177aeedac0SVignesh R #define SPINOR_OP_CLSR		0x30	/* Clear status register 1 */
118eaad4c09SChin-Ting Kuo #define SPINOR_OP_EX4B_CYPRESS	0xB8	/* Exit 4-byte mode */
119eaad4c09SChin-Ting Kuo #define SPINOR_OP_RDAR		0x65	/* Read any register */
120cd800046SChin-Ting Kuo #define SPINOR_OP_WRAR		0x71	/* Write any register */
121eaad4c09SChin-Ting Kuo #define SPINOR_REG_ADDR_STR1V	0x00800000
122cd800046SChin-Ting Kuo #define SPINOR_REG_ADDR_CFR1V	0x00800002
123eaad4c09SChin-Ting Kuo #define SPINOR_REG_ADDR_CFR3V	0x00800004
124eaad4c09SChin-Ting Kuo #define CFR3V_UNHYSA		BIT(3)	/* Uniform sectors or not */
125eaad4c09SChin-Ting Kuo #define CFR3V_PGMBUF		BIT(4)	/* Program buffer size */
1267aeedac0SVignesh R 
1277aeedac0SVignesh R /* Used for Micron flashes only. */
1287aeedac0SVignesh R #define SPINOR_OP_RD_EVCR      0x65    /* Read EVCR register */
1297aeedac0SVignesh R #define SPINOR_OP_WD_EVCR      0x61    /* Write EVCR register */
130a25e89c3SChin-Ting Kuo #define SPINOR_OP_MICRON_RDNVCR 0xB5   /* Read nonvolatile configuration register */
131a25e89c3SChin-Ting Kuo #define SPINOR_OP_MICRON_WRNVCR 0xB1   /* Write nonvolatile configuration register */
1327aeedac0SVignesh R 
1337aeedac0SVignesh R /* Status Register bits. */
1347aeedac0SVignesh R #define SR_WIP			BIT(0)	/* Write in progress */
1357aeedac0SVignesh R #define SR_WEL			BIT(1)	/* Write enable latch */
1367aeedac0SVignesh R /* meaning of other SR_* bits may differ between vendors */
1377aeedac0SVignesh R #define SR_BP0			BIT(2)	/* Block protect 0 */
1387aeedac0SVignesh R #define SR_BP1			BIT(3)	/* Block protect 1 */
1397aeedac0SVignesh R #define SR_BP2			BIT(4)	/* Block protect 2 */
1407aeedac0SVignesh R #define SR_TB			BIT(5)	/* Top/Bottom protect */
1417aeedac0SVignesh R #define SR_SRWD			BIT(7)	/* SR write protect */
1427aeedac0SVignesh R /* Spansion/Cypress specific status bits */
1437aeedac0SVignesh R #define SR_E_ERR		BIT(5)
1447aeedac0SVignesh R #define SR_P_ERR		BIT(6)
1457aeedac0SVignesh R 
1467aeedac0SVignesh R #define SR_QUAD_EN_MX		BIT(6)	/* Macronix Quad I/O */
1477aeedac0SVignesh R 
1487aeedac0SVignesh R /* Enhanced Volatile Configuration Register bits */
1497aeedac0SVignesh R #define EVCR_QUAD_EN_MICRON	BIT(7)	/* Micron Quad I/O */
150a25e89c3SChin-Ting Kuo #define MICRON_RST_HOLD_CTRL BIT(4)
1517aeedac0SVignesh R 
1527aeedac0SVignesh R /* Flag Status Register bits */
1537aeedac0SVignesh R #define FSR_READY		BIT(7)	/* Device status, 0 = Busy, 1 = Ready */
1547aeedac0SVignesh R #define FSR_E_ERR		BIT(5)	/* Erase operation status */
1557aeedac0SVignesh R #define FSR_P_ERR		BIT(4)	/* Program operation status */
1567aeedac0SVignesh R #define FSR_PT_ERR		BIT(1)	/* Protection error bit */
1577aeedac0SVignesh R 
1587aeedac0SVignesh R /* Configuration Register bits. */
1597aeedac0SVignesh R #define CR_QUAD_EN_SPAN		BIT(1)	/* Spansion Quad I/O */
1607aeedac0SVignesh R 
1617aeedac0SVignesh R /* Status Register 2 bits. */
1627aeedac0SVignesh R #define SR2_QUAD_EN_BIT7	BIT(7)
163878b2ba4SChin-Ting Kuo #define SR2_QUAD_EN_BIT1	BIT(1)
1647aeedac0SVignesh R 
1657aeedac0SVignesh R /* Supported SPI protocols */
1667aeedac0SVignesh R #define SNOR_PROTO_INST_MASK	GENMASK(23, 16)
1677aeedac0SVignesh R #define SNOR_PROTO_INST_SHIFT	16
1687aeedac0SVignesh R #define SNOR_PROTO_INST(_nbits)	\
1697aeedac0SVignesh R 	((((unsigned long)(_nbits)) << SNOR_PROTO_INST_SHIFT) & \
1707aeedac0SVignesh R 	 SNOR_PROTO_INST_MASK)
1717aeedac0SVignesh R 
1727aeedac0SVignesh R #define SNOR_PROTO_ADDR_MASK	GENMASK(15, 8)
1737aeedac0SVignesh R #define SNOR_PROTO_ADDR_SHIFT	8
1747aeedac0SVignesh R #define SNOR_PROTO_ADDR(_nbits)	\
1757aeedac0SVignesh R 	((((unsigned long)(_nbits)) << SNOR_PROTO_ADDR_SHIFT) & \
1767aeedac0SVignesh R 	 SNOR_PROTO_ADDR_MASK)
1777aeedac0SVignesh R 
1787aeedac0SVignesh R #define SNOR_PROTO_DATA_MASK	GENMASK(7, 0)
1797aeedac0SVignesh R #define SNOR_PROTO_DATA_SHIFT	0
1807aeedac0SVignesh R #define SNOR_PROTO_DATA(_nbits)	\
1817aeedac0SVignesh R 	((((unsigned long)(_nbits)) << SNOR_PROTO_DATA_SHIFT) & \
1827aeedac0SVignesh R 	 SNOR_PROTO_DATA_MASK)
1837aeedac0SVignesh R 
1847aeedac0SVignesh R #define SNOR_PROTO_IS_DTR	BIT(24)	/* Double Transfer Rate */
1857aeedac0SVignesh R 
1867aeedac0SVignesh R #define SNOR_PROTO_STR(_inst_nbits, _addr_nbits, _data_nbits)	\
1877aeedac0SVignesh R 	(SNOR_PROTO_INST(_inst_nbits) |				\
1887aeedac0SVignesh R 	 SNOR_PROTO_ADDR(_addr_nbits) |				\
1897aeedac0SVignesh R 	 SNOR_PROTO_DATA(_data_nbits))
1907aeedac0SVignesh R #define SNOR_PROTO_DTR(_inst_nbits, _addr_nbits, _data_nbits)	\
1917aeedac0SVignesh R 	(SNOR_PROTO_IS_DTR |					\
1927aeedac0SVignesh R 	 SNOR_PROTO_STR(_inst_nbits, _addr_nbits, _data_nbits))
1937aeedac0SVignesh R 
1947aeedac0SVignesh R enum spi_nor_protocol {
1957aeedac0SVignesh R 	SNOR_PROTO_1_1_1 = SNOR_PROTO_STR(1, 1, 1),
1967aeedac0SVignesh R 	SNOR_PROTO_1_1_2 = SNOR_PROTO_STR(1, 1, 2),
1977aeedac0SVignesh R 	SNOR_PROTO_1_1_4 = SNOR_PROTO_STR(1, 1, 4),
1987aeedac0SVignesh R 	SNOR_PROTO_1_1_8 = SNOR_PROTO_STR(1, 1, 8),
1997aeedac0SVignesh R 	SNOR_PROTO_1_2_2 = SNOR_PROTO_STR(1, 2, 2),
2007aeedac0SVignesh R 	SNOR_PROTO_1_4_4 = SNOR_PROTO_STR(1, 4, 4),
2017aeedac0SVignesh R 	SNOR_PROTO_1_8_8 = SNOR_PROTO_STR(1, 8, 8),
2027aeedac0SVignesh R 	SNOR_PROTO_2_2_2 = SNOR_PROTO_STR(2, 2, 2),
2037aeedac0SVignesh R 	SNOR_PROTO_4_4_4 = SNOR_PROTO_STR(4, 4, 4),
2047aeedac0SVignesh R 	SNOR_PROTO_8_8_8 = SNOR_PROTO_STR(8, 8, 8),
2057aeedac0SVignesh R 
2067aeedac0SVignesh R 	SNOR_PROTO_1_1_1_DTR = SNOR_PROTO_DTR(1, 1, 1),
2077aeedac0SVignesh R 	SNOR_PROTO_1_2_2_DTR = SNOR_PROTO_DTR(1, 2, 2),
2087aeedac0SVignesh R 	SNOR_PROTO_1_4_4_DTR = SNOR_PROTO_DTR(1, 4, 4),
2097aeedac0SVignesh R 	SNOR_PROTO_1_8_8_DTR = SNOR_PROTO_DTR(1, 8, 8),
2107aeedac0SVignesh R };
2117aeedac0SVignesh R 
spi_nor_protocol_is_dtr(enum spi_nor_protocol proto)2127aeedac0SVignesh R static inline bool spi_nor_protocol_is_dtr(enum spi_nor_protocol proto)
2137aeedac0SVignesh R {
2147aeedac0SVignesh R 	return !!(proto & SNOR_PROTO_IS_DTR);
2157aeedac0SVignesh R }
2167aeedac0SVignesh R 
spi_nor_get_protocol_inst_nbits(enum spi_nor_protocol proto)2177aeedac0SVignesh R static inline u8 spi_nor_get_protocol_inst_nbits(enum spi_nor_protocol proto)
2187aeedac0SVignesh R {
2197aeedac0SVignesh R 	return ((unsigned long)(proto & SNOR_PROTO_INST_MASK)) >>
2207aeedac0SVignesh R 		SNOR_PROTO_INST_SHIFT;
2217aeedac0SVignesh R }
2227aeedac0SVignesh R 
spi_nor_get_protocol_addr_nbits(enum spi_nor_protocol proto)2237aeedac0SVignesh R static inline u8 spi_nor_get_protocol_addr_nbits(enum spi_nor_protocol proto)
2247aeedac0SVignesh R {
2257aeedac0SVignesh R 	return ((unsigned long)(proto & SNOR_PROTO_ADDR_MASK)) >>
2267aeedac0SVignesh R 		SNOR_PROTO_ADDR_SHIFT;
2277aeedac0SVignesh R }
2287aeedac0SVignesh R 
spi_nor_get_protocol_data_nbits(enum spi_nor_protocol proto)2297aeedac0SVignesh R static inline u8 spi_nor_get_protocol_data_nbits(enum spi_nor_protocol proto)
2307aeedac0SVignesh R {
2317aeedac0SVignesh R 	return ((unsigned long)(proto & SNOR_PROTO_DATA_MASK)) >>
2327aeedac0SVignesh R 		SNOR_PROTO_DATA_SHIFT;
2337aeedac0SVignesh R }
2347aeedac0SVignesh R 
spi_nor_get_protocol_width(enum spi_nor_protocol proto)2357aeedac0SVignesh R static inline u8 spi_nor_get_protocol_width(enum spi_nor_protocol proto)
2367aeedac0SVignesh R {
2377aeedac0SVignesh R 	return spi_nor_get_protocol_data_nbits(proto);
2387aeedac0SVignesh R }
2397aeedac0SVignesh R 
2407aeedac0SVignesh R #define SPI_NOR_MAX_CMD_SIZE	8
2417aeedac0SVignesh R enum spi_nor_ops {
2427aeedac0SVignesh R 	SPI_NOR_OPS_READ = 0,
2437aeedac0SVignesh R 	SPI_NOR_OPS_WRITE,
2447aeedac0SVignesh R 	SPI_NOR_OPS_ERASE,
2457aeedac0SVignesh R 	SPI_NOR_OPS_LOCK,
2467aeedac0SVignesh R 	SPI_NOR_OPS_UNLOCK,
2477aeedac0SVignesh R };
2487aeedac0SVignesh R 
2497aeedac0SVignesh R enum spi_nor_option_flags {
2507aeedac0SVignesh R 	SNOR_F_USE_FSR		= BIT(0),
2517aeedac0SVignesh R 	SNOR_F_HAS_SR_TB	= BIT(1),
2527aeedac0SVignesh R 	SNOR_F_NO_OP_CHIP_ERASE	= BIT(2),
2537aeedac0SVignesh R 	SNOR_F_S3AN_ADDR_DEFAULT = BIT(3),
2547aeedac0SVignesh R 	SNOR_F_READY_XSR_RDY	= BIT(4),
2557aeedac0SVignesh R 	SNOR_F_USE_CLSR		= BIT(5),
2567aeedac0SVignesh R 	SNOR_F_BROKEN_RESET	= BIT(6),
2577aeedac0SVignesh R };
2587aeedac0SVignesh R 
2597aeedac0SVignesh R /**
2607aeedac0SVignesh R  * struct flash_info - Forward declaration of a structure used internally by
2617aeedac0SVignesh R  *		       spi_nor_scan()
2627aeedac0SVignesh R  */
2637aeedac0SVignesh R struct flash_info;
2647aeedac0SVignesh R 
2657aeedac0SVignesh R /* TODO: Remove, once all users of spi_flash interface are moved to MTD */
2667aeedac0SVignesh R #define spi_flash spi_nor
2677aeedac0SVignesh R 
2687aeedac0SVignesh R /**
2697aeedac0SVignesh R  * struct spi_nor - Structure for defining a the SPI NOR layer
2707aeedac0SVignesh R  * @mtd:		point to a mtd_info structure
2717aeedac0SVignesh R  * @lock:		the lock for the read/write/erase/lock/unlock operations
2727aeedac0SVignesh R  * @dev:		point to a spi device, or a spi nor controller device.
2737aeedac0SVignesh R  * @info:		spi-nor part JDEC MFR id and other info
2747aeedac0SVignesh R  * @page_size:		the page size of the SPI NOR
2757aeedac0SVignesh R  * @addr_width:		number of address bytes
2767aeedac0SVignesh R  * @erase_opcode:	the opcode for erasing a sector
2777aeedac0SVignesh R  * @read_opcode:	the read opcode
2787aeedac0SVignesh R  * @read_dummy:		the dummy needed by the read operation
2797aeedac0SVignesh R  * @program_opcode:	the program opcode
2808c927809SVignesh R  * @bank_read_cmd:	Bank read cmd
2818c927809SVignesh R  * @bank_write_cmd:	Bank write cmd
2828c927809SVignesh R  * @bank_curr:		Current flash bank
2837aeedac0SVignesh R  * @sst_write_second:	used by the SST write operation
2847aeedac0SVignesh R  * @flags:		flag options for the current SPI-NOR (SNOR_F_*)
2857aeedac0SVignesh R  * @read_proto:		the SPI protocol for read operations
2867aeedac0SVignesh R  * @write_proto:	the SPI protocol for write operations
2877aeedac0SVignesh R  * @reg_proto		the SPI protocol for read_reg/write_reg/erase operations
2887aeedac0SVignesh R  * @cmd_buf:		used by the write_reg
2897aeedac0SVignesh R  * @prepare:		[OPTIONAL] do some preparations for the
2907aeedac0SVignesh R  *			read/write/erase/lock/unlock operations
2917aeedac0SVignesh R  * @unprepare:		[OPTIONAL] do some post work after the
2927aeedac0SVignesh R  *			read/write/erase/lock/unlock operations
2937aeedac0SVignesh R  * @read_reg:		[DRIVER-SPECIFIC] read out the register
2947aeedac0SVignesh R  * @write_reg:		[DRIVER-SPECIFIC] write data to the register
2957aeedac0SVignesh R  * @read:		[DRIVER-SPECIFIC] read data from the SPI NOR
2967aeedac0SVignesh R  * @write:		[DRIVER-SPECIFIC] write data to the SPI NOR
2977aeedac0SVignesh R  * @erase:		[DRIVER-SPECIFIC] erase a sector of the SPI NOR
2987aeedac0SVignesh R  *			at the offset @offs; if not provided by the driver,
2997aeedac0SVignesh R  *			spi-nor will send the erase opcode via write_reg()
3007aeedac0SVignesh R  * @flash_lock:		[FLASH-SPECIFIC] lock a region of the SPI NOR
3017aeedac0SVignesh R  * @flash_unlock:	[FLASH-SPECIFIC] unlock a region of the SPI NOR
3027aeedac0SVignesh R  * @flash_is_locked:	[FLASH-SPECIFIC] check if a region of the SPI NOR is
3037aeedac0SVignesh R  * @quad_enable:	[FLASH-SPECIFIC] enables SPI NOR quad mode
3047aeedac0SVignesh R  *			completely locked
3057aeedac0SVignesh R  * @priv:		the private data
3067aeedac0SVignesh R  */
3077aeedac0SVignesh R struct spi_nor {
3087aeedac0SVignesh R 	struct mtd_info		mtd;
3097aeedac0SVignesh R 	struct udevice		*dev;
3107aeedac0SVignesh R 	struct spi_slave	*spi;
3117aeedac0SVignesh R 	const struct flash_info	*info;
3127aeedac0SVignesh R 	u32			page_size;
3137aeedac0SVignesh R 	u8			addr_width;
3147aeedac0SVignesh R 	u8			erase_opcode;
3157aeedac0SVignesh R 	u8			read_opcode;
3167aeedac0SVignesh R 	u8			read_dummy;
3177aeedac0SVignesh R 	u8			program_opcode;
3188c927809SVignesh R #ifdef CONFIG_SPI_FLASH_BAR
3198c927809SVignesh R 	u8			bank_read_cmd;
3208c927809SVignesh R 	u8			bank_write_cmd;
3218c927809SVignesh R 	u8			bank_curr;
3228c927809SVignesh R #endif
3237aeedac0SVignesh R 	enum spi_nor_protocol	read_proto;
3247aeedac0SVignesh R 	enum spi_nor_protocol	write_proto;
3257aeedac0SVignesh R 	enum spi_nor_protocol	reg_proto;
3267aeedac0SVignesh R 	bool			sst_write_second;
3277aeedac0SVignesh R 	u32			flags;
3287aeedac0SVignesh R 	u8			cmd_buf[SPI_NOR_MAX_CMD_SIZE];
3297aeedac0SVignesh R 
3307aeedac0SVignesh R 	int (*prepare)(struct spi_nor *nor, enum spi_nor_ops ops);
3317aeedac0SVignesh R 	void (*unprepare)(struct spi_nor *nor, enum spi_nor_ops ops);
3327aeedac0SVignesh R 	int (*read_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len);
3337aeedac0SVignesh R 	int (*write_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len);
3347aeedac0SVignesh R 
3357aeedac0SVignesh R 	ssize_t (*read)(struct spi_nor *nor, loff_t from,
3367aeedac0SVignesh R 			size_t len, u_char *read_buf);
3377aeedac0SVignesh R 	ssize_t (*write)(struct spi_nor *nor, loff_t to,
3387aeedac0SVignesh R 			 size_t len, const u_char *write_buf);
3397aeedac0SVignesh R 	int (*erase)(struct spi_nor *nor, loff_t offs);
3407aeedac0SVignesh R 
3417aeedac0SVignesh R 	int (*flash_lock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
3427aeedac0SVignesh R 	int (*flash_unlock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
3437aeedac0SVignesh R 	int (*flash_is_locked)(struct spi_nor *nor, loff_t ofs, uint64_t len);
3447aeedac0SVignesh R 	int (*quad_enable)(struct spi_nor *nor);
345*591e1cf0SChin-Ting Kuo 	int (*flash_lock_by_host_ctrl)(struct spi_nor *nor, u32 ofs, size_t len);
346*591e1cf0SChin-Ting Kuo 	int (*flash_unlock_by_host_ctrl)(struct spi_nor *nor, u32 ofs, size_t len);
3477aeedac0SVignesh R 
3487aeedac0SVignesh R 	void *priv;
3497aeedac0SVignesh R /* Compatibility for spi_flash, remove once sf layer is merged with mtd */
3507aeedac0SVignesh R 	const char *name;
3517aeedac0SVignesh R 	u32 size;
3527aeedac0SVignesh R 	u32 sector_size;
3537aeedac0SVignesh R 	u32 erase_size;
3547aeedac0SVignesh R };
3557aeedac0SVignesh R 
spi_nor_set_flash_node(struct spi_nor * nor,const struct device_node * np)3567aeedac0SVignesh R static inline void spi_nor_set_flash_node(struct spi_nor *nor,
3577aeedac0SVignesh R 					  const struct device_node *np)
3587aeedac0SVignesh R {
3597aeedac0SVignesh R 	mtd_set_of_node(&nor->mtd, np);
3607aeedac0SVignesh R }
3617aeedac0SVignesh R 
3627aeedac0SVignesh R static inline const struct
spi_nor_get_flash_node(struct spi_nor * nor)3637aeedac0SVignesh R device_node *spi_nor_get_flash_node(struct spi_nor *nor)
3647aeedac0SVignesh R {
3657aeedac0SVignesh R 	return mtd_get_of_node(&nor->mtd);
3667aeedac0SVignesh R }
3677aeedac0SVignesh R 
3687aeedac0SVignesh R /**
3697aeedac0SVignesh R  * struct spi_nor_hwcaps - Structure for describing the hardware capabilies
3707aeedac0SVignesh R  * supported by the SPI controller (bus master).
3717aeedac0SVignesh R  * @mask:		the bitmask listing all the supported hw capabilies
3727aeedac0SVignesh R  */
3737aeedac0SVignesh R struct spi_nor_hwcaps {
3747aeedac0SVignesh R 	u32	mask;
3757aeedac0SVignesh R };
3767aeedac0SVignesh R 
3777aeedac0SVignesh R /*
3787aeedac0SVignesh R  *(Fast) Read capabilities.
3797aeedac0SVignesh R  * MUST be ordered by priority: the higher bit position, the higher priority.
3807aeedac0SVignesh R  * As a matter of performances, it is relevant to use Octo SPI protocols first,
3817aeedac0SVignesh R  * then Quad SPI protocols before Dual SPI protocols, Fast Read and lastly
3827aeedac0SVignesh R  * (Slow) Read.
3837aeedac0SVignesh R  */
3847aeedac0SVignesh R #define SNOR_HWCAPS_READ_MASK		GENMASK(14, 0)
3857aeedac0SVignesh R #define SNOR_HWCAPS_READ		BIT(0)
3867aeedac0SVignesh R #define SNOR_HWCAPS_READ_FAST		BIT(1)
3877aeedac0SVignesh R #define SNOR_HWCAPS_READ_1_1_1_DTR	BIT(2)
3887aeedac0SVignesh R 
3897aeedac0SVignesh R #define SNOR_HWCAPS_READ_DUAL		GENMASK(6, 3)
3907aeedac0SVignesh R #define SNOR_HWCAPS_READ_1_1_2		BIT(3)
3917aeedac0SVignesh R #define SNOR_HWCAPS_READ_1_2_2		BIT(4)
3927aeedac0SVignesh R #define SNOR_HWCAPS_READ_2_2_2		BIT(5)
3937aeedac0SVignesh R #define SNOR_HWCAPS_READ_1_2_2_DTR	BIT(6)
3947aeedac0SVignesh R 
3957aeedac0SVignesh R #define SNOR_HWCAPS_READ_QUAD		GENMASK(10, 7)
3967aeedac0SVignesh R #define SNOR_HWCAPS_READ_1_1_4		BIT(7)
3977aeedac0SVignesh R #define SNOR_HWCAPS_READ_1_4_4		BIT(8)
3987aeedac0SVignesh R #define SNOR_HWCAPS_READ_4_4_4		BIT(9)
3997aeedac0SVignesh R #define SNOR_HWCAPS_READ_1_4_4_DTR	BIT(10)
4007aeedac0SVignesh R 
4017aeedac0SVignesh R #define SNOR_HWCPAS_READ_OCTO		GENMASK(14, 11)
4027aeedac0SVignesh R #define SNOR_HWCAPS_READ_1_1_8		BIT(11)
4037aeedac0SVignesh R #define SNOR_HWCAPS_READ_1_8_8		BIT(12)
4047aeedac0SVignesh R #define SNOR_HWCAPS_READ_8_8_8		BIT(13)
4057aeedac0SVignesh R #define SNOR_HWCAPS_READ_1_8_8_DTR	BIT(14)
4067aeedac0SVignesh R 
4077aeedac0SVignesh R /*
4087aeedac0SVignesh R  * Page Program capabilities.
4097aeedac0SVignesh R  * MUST be ordered by priority: the higher bit position, the higher priority.
4107aeedac0SVignesh R  * Like (Fast) Read capabilities, Octo/Quad SPI protocols are preferred to the
4117aeedac0SVignesh R  * legacy SPI 1-1-1 protocol.
4127aeedac0SVignesh R  * Note that Dual Page Programs are not supported because there is no existing
4137aeedac0SVignesh R  * JEDEC/SFDP standard to define them. Also at this moment no SPI flash memory
4147aeedac0SVignesh R  * implements such commands.
4157aeedac0SVignesh R  */
4167aeedac0SVignesh R #define SNOR_HWCAPS_PP_MASK	GENMASK(22, 16)
4177aeedac0SVignesh R #define SNOR_HWCAPS_PP		BIT(16)
4187aeedac0SVignesh R 
4197aeedac0SVignesh R #define SNOR_HWCAPS_PP_QUAD	GENMASK(19, 17)
4207aeedac0SVignesh R #define SNOR_HWCAPS_PP_1_1_4	BIT(17)
4217aeedac0SVignesh R #define SNOR_HWCAPS_PP_1_4_4	BIT(18)
4227aeedac0SVignesh R #define SNOR_HWCAPS_PP_4_4_4	BIT(19)
4237aeedac0SVignesh R 
4247aeedac0SVignesh R #define SNOR_HWCAPS_PP_OCTO	GENMASK(22, 20)
4257aeedac0SVignesh R #define SNOR_HWCAPS_PP_1_1_8	BIT(20)
4267aeedac0SVignesh R #define SNOR_HWCAPS_PP_1_8_8	BIT(21)
4277aeedac0SVignesh R #define SNOR_HWCAPS_PP_8_8_8	BIT(22)
4287aeedac0SVignesh R 
4297aeedac0SVignesh R /**
4307aeedac0SVignesh R  * spi_nor_scan() - scan the SPI NOR
4317aeedac0SVignesh R  * @nor:	the spi_nor structure
4327aeedac0SVignesh R  *
4337aeedac0SVignesh R  * The drivers can use this function to scan the SPI NOR.
4347aeedac0SVignesh R  * In the scanning, it will try to get all the necessary information to
4357aeedac0SVignesh R  * fill the mtd_info{} and the spi_nor{}.
4367aeedac0SVignesh R  *
4377aeedac0SVignesh R  * Return: 0 for success, others for failure.
4387aeedac0SVignesh R  */
4397aeedac0SVignesh R int spi_nor_scan(struct spi_nor *nor);
4407aeedac0SVignesh R 
4417aeedac0SVignesh R #endif
442