Huang Shijie | f39d2fa | 2014-02-24 18:37:35 +0800 | [diff] [blame] | 1 | #ifndef __LINUX_MTD_SPI_NOR_H |
| 2 | #define __LINUX_MTD_SPI_NOR_H |
| 3 | |
| 4 | /* Flash opcodes. */ |
| 5 | #define OPCODE_WREN 0x06 /* Write enable */ |
| 6 | #define OPCODE_RDSR 0x05 /* Read status register */ |
| 7 | #define OPCODE_WRSR 0x01 /* Write status register 1 byte */ |
| 8 | #define OPCODE_NORM_READ 0x03 /* Read data bytes (low frequency) */ |
| 9 | #define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */ |
| 10 | #define OPCODE_DUAL_READ 0x3b /* Read data bytes (Dual SPI) */ |
| 11 | #define OPCODE_QUAD_READ 0x6b /* Read data bytes (Quad SPI) */ |
| 12 | #define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */ |
| 13 | #define OPCODE_BE_4K 0x20 /* Erase 4KiB block */ |
| 14 | #define OPCODE_BE_4K_PMC 0xd7 /* Erase 4KiB block on PMC chips */ |
| 15 | #define OPCODE_BE_32K 0x52 /* Erase 32KiB block */ |
| 16 | #define OPCODE_CHIP_ERASE 0xc7 /* Erase whole flash chip */ |
| 17 | #define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */ |
| 18 | #define OPCODE_RDID 0x9f /* Read JEDEC ID */ |
| 19 | #define OPCODE_RDCR 0x35 /* Read configuration register */ |
| 20 | |
| 21 | /* 4-byte address opcodes - used on Spansion and some Macronix flashes. */ |
| 22 | #define OPCODE_NORM_READ_4B 0x13 /* Read data bytes (low frequency) */ |
| 23 | #define OPCODE_FAST_READ_4B 0x0c /* Read data bytes (high frequency) */ |
| 24 | #define OPCODE_DUAL_READ_4B 0x3c /* Read data bytes (Dual SPI) */ |
| 25 | #define OPCODE_QUAD_READ_4B 0x6c /* Read data bytes (Quad SPI) */ |
| 26 | #define OPCODE_PP_4B 0x12 /* Page program (up to 256 bytes) */ |
| 27 | #define OPCODE_SE_4B 0xdc /* Sector erase (usually 64KiB) */ |
| 28 | |
| 29 | /* Used for SST flashes only. */ |
| 30 | #define OPCODE_BP 0x02 /* Byte program */ |
| 31 | #define OPCODE_WRDI 0x04 /* Write disable */ |
| 32 | #define OPCODE_AAI_WP 0xad /* Auto address increment word program */ |
| 33 | |
| 34 | /* Used for Macronix and Winbond flashes. */ |
| 35 | #define OPCODE_EN4B 0xb7 /* Enter 4-byte mode */ |
| 36 | #define OPCODE_EX4B 0xe9 /* Exit 4-byte mode */ |
| 37 | |
| 38 | /* Used for Spansion flashes only. */ |
| 39 | #define OPCODE_BRWR 0x17 /* Bank register write */ |
| 40 | |
| 41 | /* Status Register bits. */ |
| 42 | #define SR_WIP 1 /* Write in progress */ |
| 43 | #define SR_WEL 2 /* Write enable latch */ |
| 44 | /* meaning of other SR_* bits may differ between vendors */ |
| 45 | #define SR_BP0 4 /* Block protect 0 */ |
| 46 | #define SR_BP1 8 /* Block protect 1 */ |
| 47 | #define SR_BP2 0x10 /* Block protect 2 */ |
| 48 | #define SR_SRWD 0x80 /* SR write protect */ |
| 49 | |
| 50 | #define SR_QUAD_EN_MX 0x40 /* Macronix Quad I/O */ |
| 51 | |
| 52 | /* Configuration Register bits. */ |
| 53 | #define CR_QUAD_EN_SPAN 0x2 /* Spansion Quad I/O */ |
| 54 | |
Huang Shijie | 6e602ef | 2014-02-24 18:37:36 +0800 | [diff] [blame] | 55 | enum read_mode { |
| 56 | SPI_NOR_NORMAL = 0, |
| 57 | SPI_NOR_FAST, |
| 58 | SPI_NOR_DUAL, |
| 59 | SPI_NOR_QUAD, |
| 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * struct spi_nor_xfer_cfg - Structure for defining a Serial Flash transfer |
| 64 | * @wren: command for "Write Enable", or 0x00 for not required |
| 65 | * @cmd: command for operation |
| 66 | * @cmd_pins: number of pins to send @cmd (1, 2, 4) |
| 67 | * @addr: address for operation |
| 68 | * @addr_pins: number of pins to send @addr (1, 2, 4) |
| 69 | * @addr_width: number of address bytes |
| 70 | * (3,4, or 0 for address not required) |
| 71 | * @mode: mode data |
| 72 | * @mode_pins: number of pins to send @mode (1, 2, 4) |
| 73 | * @mode_cycles: number of mode cycles (0 for mode not required) |
| 74 | * @dummy_cycles: number of dummy cycles (0 for dummy not required) |
| 75 | */ |
| 76 | struct spi_nor_xfer_cfg { |
| 77 | u8 wren; |
| 78 | u8 cmd; |
| 79 | u8 cmd_pins; |
| 80 | u32 addr; |
| 81 | u8 addr_pins; |
| 82 | u8 addr_width; |
| 83 | u8 mode; |
| 84 | u8 mode_pins; |
| 85 | u8 mode_cycles; |
| 86 | u8 dummy_cycles; |
| 87 | }; |
| 88 | |
| 89 | #define SPI_NOR_MAX_CMD_SIZE 8 |
| 90 | enum spi_nor_ops { |
| 91 | SPI_NOR_OPS_READ = 0, |
| 92 | SPI_NOR_OPS_WRITE, |
| 93 | SPI_NOR_OPS_ERASE, |
| 94 | SPI_NOR_OPS_LOCK, |
| 95 | SPI_NOR_OPS_UNLOCK, |
| 96 | }; |
| 97 | |
| 98 | /** |
| 99 | * struct spi_nor - Structure for defining a the SPI NOR layer |
| 100 | * @mtd: point to a mtd_info structure |
| 101 | * @lock: the lock for the read/write/erase/lock/unlock operations |
| 102 | * @dev: point to a spi device, or a spi nor controller device. |
| 103 | * @page_size: the page size of the SPI NOR |
| 104 | * @addr_width: number of address bytes |
| 105 | * @erase_opcode: the opcode for erasing a sector |
| 106 | * @read_opcode: the read opcode |
| 107 | * @read_dummy: the dummy needed by the read operation |
| 108 | * @program_opcode: the program opcode |
| 109 | * @flash_read: the mode of the read |
| 110 | * @sst_write_second: used by the SST write operation |
| 111 | * @cfg: used by the read_xfer/write_xfer |
| 112 | * @cmd_buf: used by the write_reg |
| 113 | * @prepare: [OPTIONAL] do some preparations for the |
| 114 | * read/write/erase/lock/unlock operations |
| 115 | * @unprepare: [OPTIONAL] do some post work after the |
| 116 | * read/write/erase/lock/unlock operations |
| 117 | * @read_xfer: [OPTIONAL] the read fundamental primitive |
| 118 | * @write_xfer: [OPTIONAL] the writefundamental primitive |
| 119 | * @read_reg: [DRIVER-SPECIFIC] read out the register |
| 120 | * @write_reg: [DRIVER-SPECIFIC] write data to the register |
| 121 | * @read_id: [REPLACEABLE] read out the ID data, and find |
| 122 | * the proper spi_device_id |
| 123 | * @wait_till_ready: [REPLACEABLE] wait till the NOR becomes ready |
| 124 | * @read: [DRIVER-SPECIFIC] read data from the SPI NOR |
| 125 | * @write: [DRIVER-SPECIFIC] write data to the SPI NOR |
| 126 | * @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR |
| 127 | * at the offset @offs |
| 128 | * @priv: the private data |
| 129 | */ |
| 130 | struct spi_nor { |
| 131 | struct mtd_info *mtd; |
| 132 | struct mutex lock; |
| 133 | struct device *dev; |
| 134 | u32 page_size; |
| 135 | u8 addr_width; |
| 136 | u8 erase_opcode; |
| 137 | u8 read_opcode; |
| 138 | u8 read_dummy; |
| 139 | u8 program_opcode; |
| 140 | enum read_mode flash_read; |
| 141 | bool sst_write_second; |
| 142 | struct spi_nor_xfer_cfg cfg; |
| 143 | u8 cmd_buf[SPI_NOR_MAX_CMD_SIZE]; |
| 144 | |
| 145 | int (*prepare)(struct spi_nor *nor, enum spi_nor_ops ops); |
| 146 | void (*unprepare)(struct spi_nor *nor, enum spi_nor_ops ops); |
| 147 | int (*read_xfer)(struct spi_nor *nor, struct spi_nor_xfer_cfg *cfg, |
| 148 | u8 *buf, size_t len); |
| 149 | int (*write_xfer)(struct spi_nor *nor, struct spi_nor_xfer_cfg *cfg, |
| 150 | u8 *buf, size_t len); |
| 151 | int (*read_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len); |
| 152 | int (*write_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len, |
| 153 | int write_enable); |
| 154 | const struct spi_device_id *(*read_id)(struct spi_nor *nor); |
| 155 | int (*wait_till_ready)(struct spi_nor *nor); |
| 156 | |
| 157 | int (*read)(struct spi_nor *nor, loff_t from, |
| 158 | size_t len, size_t *retlen, u_char *read_buf); |
| 159 | void (*write)(struct spi_nor *nor, loff_t to, |
| 160 | size_t len, size_t *retlen, const u_char *write_buf); |
| 161 | int (*erase)(struct spi_nor *nor, loff_t offs); |
| 162 | |
| 163 | void *priv; |
| 164 | }; |
Huang Shijie | b199489 | 2014-02-24 18:37:37 +0800 | [diff] [blame^] | 165 | |
| 166 | /** |
| 167 | * spi_nor_scan() - scan the SPI NOR |
| 168 | * @nor: the spi_nor structure |
| 169 | * @id: the spi_device_id provided by the driver |
| 170 | * @mode: the read mode supported by the driver |
| 171 | * |
| 172 | * The drivers can use this fuction to scan the SPI NOR. |
| 173 | * In the scanning, it will try to get all the necessary information to |
| 174 | * fill the mtd_info{} and the spi_nor{}. |
| 175 | * |
| 176 | * The board may assigns a spi_device_id with @id which be used to compared with |
| 177 | * the spi_device_id detected by the scanning. |
| 178 | * |
| 179 | * Return: 0 for success, others for failure. |
| 180 | */ |
| 181 | int spi_nor_scan(struct spi_nor *nor, const struct spi_device_id *id, |
| 182 | enum read_mode mode); |
| 183 | extern const struct spi_device_id spi_nor_ids[]; |
| 184 | |
Huang Shijie | f39d2fa | 2014-02-24 18:37:35 +0800 | [diff] [blame] | 185 | #endif |