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