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