Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 2 | /* |
Jagannadha Sutradharudu Teki | a5e8199 | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 3 | * Common SPI flash Interface |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2008 Atmel Corporation |
Jagannadha Sutradharudu Teki | a5e8199 | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 6 | * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc. |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 7 | */ |
Jagannadha Sutradharudu Teki | a5e8199 | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 8 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 9 | #ifndef _SPI_FLASH_H_ |
| 10 | #define _SPI_FLASH_H_ |
| 11 | |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 12 | #include <dm.h> /* Because we dereference struct udevice here */ |
Mike Frysinger | e06ab65 | 2009-11-03 11:36:39 -0500 | [diff] [blame] | 13 | #include <linux/types.h> |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 14 | |
Nikita Kiryanov | 88e34e5 | 2014-08-20 15:08:48 +0300 | [diff] [blame] | 15 | #ifndef CONFIG_SF_DEFAULT_SPEED |
| 16 | # define CONFIG_SF_DEFAULT_SPEED 1000000 |
| 17 | #endif |
| 18 | #ifndef CONFIG_SF_DEFAULT_MODE |
| 19 | # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3 |
| 20 | #endif |
| 21 | #ifndef CONFIG_SF_DEFAULT_CS |
| 22 | # define CONFIG_SF_DEFAULT_CS 0 |
| 23 | #endif |
| 24 | #ifndef CONFIG_SF_DEFAULT_BUS |
| 25 | # define CONFIG_SF_DEFAULT_BUS 0 |
| 26 | #endif |
| 27 | |
Simon Glass | ff0960f | 2014-10-13 23:42:04 -0600 | [diff] [blame] | 28 | struct spi_slave; |
Jagannadha Sutradharudu Teki | 33adfb5 | 2013-12-23 23:34:42 +0530 | [diff] [blame] | 29 | |
| 30 | /** |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 31 | * struct spi_flash - SPI flash structure |
| 32 | * |
| 33 | * @spi: SPI slave |
Jagan Teki | 5d69df3 | 2015-06-27 00:51:30 +0530 | [diff] [blame] | 34 | * @dev: SPI flash device |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 35 | * @name: Name of SPI flash |
Jagan Teki | 5d69df3 | 2015-06-27 00:51:30 +0530 | [diff] [blame] | 36 | * @dual_flash: Indicates dual flash memories - dual stacked, parallel |
Jagannadha Sutradharudu Teki | 056fbc7 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 37 | * @shift: Flash shift useful in dual parallel |
Jagan Teki | 1fabefd | 2015-09-29 11:17:02 +0530 | [diff] [blame] | 38 | * @flags: Indication of spi flash flags |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 39 | * @size: Total flash size |
| 40 | * @page_size: Write (page) size |
| 41 | * @sector_size: Sector size |
Jagan Teki | 5d69df3 | 2015-06-27 00:51:30 +0530 | [diff] [blame] | 42 | * @erase_size: Erase size |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 43 | * @bank_read_cmd: Bank read cmd |
| 44 | * @bank_write_cmd: Bank write cmd |
| 45 | * @bank_curr: Current flash bank |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 46 | * @erase_cmd: Erase cmd 4K, 32K, 64K |
Jagannadha Sutradharudu Teki | 3163aaa | 2014-01-11 15:13:11 +0530 | [diff] [blame] | 47 | * @read_cmd: Read cmd - Array Fast, Extn read and quad read. |
| 48 | * @write_cmd: Write cmd - page and quad program. |
Jagan Teki | 5d69df3 | 2015-06-27 00:51:30 +0530 | [diff] [blame] | 49 | * @dummy_byte: Dummy cycles for read operation. |
| 50 | * @memory_map: Address of read-only SPI flash access |
Fabio Estevam | c3c016c | 2015-11-05 12:43:42 -0200 | [diff] [blame] | 51 | * @flash_lock: lock a region of the SPI Flash |
| 52 | * @flash_unlock: unlock a region of the SPI Flash |
| 53 | * @flash_is_locked: check if a region of the SPI Flash is completely locked |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 54 | * @read: Flash read ops: Read len bytes at offset into buf |
| 55 | * Supported cmds: Fast Array Read |
Vasili Galka | 801cec5 | 2014-03-04 17:24:09 +0200 | [diff] [blame] | 56 | * @write: Flash write ops: Write len bytes from buf into offset |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 57 | * Supported cmds: Page Program |
| 58 | * @erase: Flash erase ops: Erase len bytes from offset |
| 59 | * Supported cmds: Sector erase 4K, 32K, 64K |
Vasili Galka | 801cec5 | 2014-03-04 17:24:09 +0200 | [diff] [blame] | 60 | * return 0 - Success, 1 - Failure |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 61 | */ |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 62 | struct spi_flash { |
Pavel Machek | d15e74f | 2015-04-21 10:37:45 +0200 | [diff] [blame] | 63 | struct spi_slave *spi; |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 64 | #ifdef CONFIG_DM_SPI_FLASH |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 65 | struct udevice *dev; |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 66 | #endif |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 67 | const char *name; |
Jagannadha Sutradharudu Teki | f77f469 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 68 | u8 dual_flash; |
Jagannadha Sutradharudu Teki | 056fbc7 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 69 | u8 shift; |
Jagan Teki | 1fabefd | 2015-09-29 11:17:02 +0530 | [diff] [blame] | 70 | u16 flags; |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 71 | |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 72 | u32 size; |
| 73 | u32 page_size; |
| 74 | u32 sector_size; |
| 75 | u32 erase_size; |
Jagannadha Sutradharudu Teki | 1dcd6d0 | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 76 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 77 | u8 bank_read_cmd; |
| 78 | u8 bank_write_cmd; |
| 79 | u8 bank_curr; |
Jagannadha Sutradharudu Teki | 1dcd6d0 | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 80 | #endif |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 81 | u8 erase_cmd; |
Jagannadha Sutradharudu Teki | 4e09cc1 | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 82 | u8 read_cmd; |
Jagannadha Sutradharudu Teki | 3163aaa | 2014-01-11 15:13:11 +0530 | [diff] [blame] | 83 | u8 write_cmd; |
Jagannadha Sutradharudu Teki | ff063ed | 2014-01-11 16:50:45 +0530 | [diff] [blame] | 84 | u8 dummy_byte; |
Jagannadha Sutradharudu Teki | 615a156 | 2013-06-21 15:56:30 +0530 | [diff] [blame] | 85 | |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 86 | void *memory_map; |
Fabio Estevam | c3c016c | 2015-11-05 12:43:42 -0200 | [diff] [blame] | 87 | |
| 88 | int (*flash_lock)(struct spi_flash *flash, u32 ofs, size_t len); |
| 89 | int (*flash_unlock)(struct spi_flash *flash, u32 ofs, size_t len); |
| 90 | int (*flash_is_locked)(struct spi_flash *flash, u32 ofs, size_t len); |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 91 | #ifndef CONFIG_DM_SPI_FLASH |
| 92 | /* |
| 93 | * These are not strictly needed for driver model, but keep them here |
Pavel Machek | d15e74f | 2015-04-21 10:37:45 +0200 | [diff] [blame] | 94 | * while the transition is in progress. |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 95 | * |
| 96 | * Normally each driver would provide its own operations, but for |
| 97 | * SPI flash most chips use the same algorithms. One approach is |
| 98 | * to create a 'common' SPI flash device which knows how to talk |
| 99 | * to most devices, and then allow other drivers to be used instead |
Pavel Machek | d15e74f | 2015-04-21 10:37:45 +0200 | [diff] [blame] | 100 | * if required, perhaps with a way of scanning through the list to |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 101 | * find the driver that matches the device. |
| 102 | */ |
Jagannadha Sutradharudu Teki | 7ab35d9 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 103 | int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf); |
| 104 | int (*write)(struct spi_flash *flash, u32 offset, size_t len, |
| 105 | const void *buf); |
| 106 | int (*erase)(struct spi_flash *flash, u32 offset, size_t len); |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 107 | #endif |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 108 | }; |
| 109 | |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 110 | struct dm_spi_flash_ops { |
| 111 | int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf); |
| 112 | int (*write)(struct udevice *dev, u32 offset, size_t len, |
| 113 | const void *buf); |
| 114 | int (*erase)(struct udevice *dev, u32 offset, size_t len); |
Simon Glass | a58986c | 2018-11-06 15:21:41 -0700 | [diff] [blame] | 115 | /** |
| 116 | * get_sw_write_prot() - Check state of software write-protect feature |
| 117 | * |
| 118 | * SPI flash chips can lock a region of the flash defined by a |
| 119 | * 'protected area'. This function checks if this protected area is |
| 120 | * defined. |
| 121 | * |
| 122 | * @dev: SPI flash device |
| 123 | * @return 0 if no region is write-protected, 1 if a region is |
| 124 | * write-protected, -ENOSYS if the driver does not implement this, |
| 125 | * other -ve value on error |
| 126 | */ |
| 127 | int (*get_sw_write_prot)(struct udevice *dev); |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | /* Access the serial operations for a device */ |
| 131 | #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops) |
| 132 | |
| 133 | #ifdef CONFIG_DM_SPI_FLASH |
Simon Glass | 8d987ab | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 134 | /** |
| 135 | * spi_flash_read_dm() - Read data from SPI flash |
| 136 | * |
| 137 | * @dev: SPI flash device |
| 138 | * @offset: Offset into device in bytes to read from |
| 139 | * @len: Number of bytes to read |
| 140 | * @buf: Buffer to put the data that is read |
| 141 | * @return 0 if OK, -ve on error |
| 142 | */ |
| 143 | int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf); |
| 144 | |
| 145 | /** |
| 146 | * spi_flash_write_dm() - Write data to SPI flash |
| 147 | * |
| 148 | * @dev: SPI flash device |
| 149 | * @offset: Offset into device in bytes to write to |
| 150 | * @len: Number of bytes to write |
| 151 | * @buf: Buffer containing bytes to write |
| 152 | * @return 0 if OK, -ve on error |
| 153 | */ |
| 154 | int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len, |
| 155 | const void *buf); |
| 156 | |
| 157 | /** |
| 158 | * spi_flash_erase_dm() - Erase blocks of the SPI flash |
| 159 | * |
| 160 | * Note that @len must be a muiltiple of the flash sector size. |
| 161 | * |
| 162 | * @dev: SPI flash device |
| 163 | * @offset: Offset into device in bytes to start erasing |
| 164 | * @len: Number of bytes to erase |
| 165 | * @return 0 if OK, -ve on error |
| 166 | */ |
| 167 | int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len); |
| 168 | |
Simon Glass | a58986c | 2018-11-06 15:21:41 -0700 | [diff] [blame] | 169 | /** |
| 170 | * spl_flash_get_sw_write_prot() - Check state of software write-protect feature |
| 171 | * |
| 172 | * SPI flash chips can lock a region of the flash defined by a |
| 173 | * 'protected area'. This function checks if this protected area is |
| 174 | * defined. |
| 175 | * |
| 176 | * @dev: SPI flash device |
| 177 | * @return 0 if no region is write-protected, 1 if a region is |
| 178 | * write-protected, -ENOSYS if the driver does not implement this, |
| 179 | * other -ve value on error |
| 180 | */ |
| 181 | int spl_flash_get_sw_write_prot(struct udevice *dev); |
| 182 | |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 183 | int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs, |
| 184 | unsigned int max_hz, unsigned int spi_mode, |
| 185 | struct udevice **devp); |
| 186 | |
| 187 | /* Compatibility function - this is the old U-Boot API */ |
| 188 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 189 | unsigned int max_hz, unsigned int spi_mode); |
| 190 | |
| 191 | /* Compatibility function - this is the old U-Boot API */ |
| 192 | void spi_flash_free(struct spi_flash *flash); |
| 193 | |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 194 | static inline int spi_flash_read(struct spi_flash *flash, u32 offset, |
Simon Glass | 8d987ab | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 195 | size_t len, void *buf) |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 196 | { |
Simon Glass | 8d987ab | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 197 | return spi_flash_read_dm(flash->dev, offset, len, buf); |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static inline int spi_flash_write(struct spi_flash *flash, u32 offset, |
Simon Glass | 8d987ab | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 201 | size_t len, const void *buf) |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 202 | { |
Simon Glass | 8d987ab | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 203 | return spi_flash_write_dm(flash->dev, offset, len, buf); |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, |
Simon Glass | 8d987ab | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 207 | size_t len) |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 208 | { |
Simon Glass | 8d987ab | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 209 | return spi_flash_erase_dm(flash->dev, offset, len); |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | struct sandbox_state; |
| 213 | |
| 214 | int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, |
Simon Glass | 008dcdd | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 215 | struct udevice *bus, ofnode node, const char *spec); |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 216 | |
| 217 | void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs); |
| 218 | |
| 219 | #else |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 220 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 221 | unsigned int max_hz, unsigned int spi_mode); |
Simon Glass | 0efc024 | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 222 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 223 | void spi_flash_free(struct spi_flash *flash); |
| 224 | |
| 225 | static inline int spi_flash_read(struct spi_flash *flash, u32 offset, |
| 226 | size_t len, void *buf) |
| 227 | { |
| 228 | return flash->read(flash, offset, len, buf); |
| 229 | } |
| 230 | |
| 231 | static inline int spi_flash_write(struct spi_flash *flash, u32 offset, |
| 232 | size_t len, const void *buf) |
| 233 | { |
| 234 | return flash->write(flash, offset, len, buf); |
| 235 | } |
| 236 | |
| 237 | static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, |
| 238 | size_t len) |
| 239 | { |
| 240 | return flash->erase(flash, offset, len); |
| 241 | } |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 242 | #endif |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 243 | |
Fabio Estevam | c3c016c | 2015-11-05 12:43:42 -0200 | [diff] [blame] | 244 | static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len, |
| 245 | bool prot) |
| 246 | { |
Bin Meng | 439fcb9 | 2015-11-13 02:46:26 -0800 | [diff] [blame] | 247 | if (!flash->flash_lock || !flash->flash_unlock) |
Fabio Estevam | c3c016c | 2015-11-05 12:43:42 -0200 | [diff] [blame] | 248 | return -EOPNOTSUPP; |
| 249 | |
| 250 | if (prot) |
| 251 | return flash->flash_lock(flash, ofs, len); |
| 252 | else |
| 253 | return flash->flash_unlock(flash, ofs, len); |
| 254 | } |
| 255 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 256 | #endif /* _SPI_FLASH_H_ */ |