Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 2 | /* |
Jagannadha Sutradharudu Teki | 25dc86a | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 3 | * Common SPI flash Interface |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2008 Atmel Corporation |
Jagannadha Sutradharudu Teki | 25dc86a | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 6 | * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc. |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 7 | */ |
Jagannadha Sutradharudu Teki | 25dc86a | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 8 | |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 9 | #ifndef _SPI_FLASH_H_ |
| 10 | #define _SPI_FLASH_H_ |
| 11 | |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 12 | #include <dm.h> /* Because we dereference struct udevice here */ |
Mike Frysinger | e4c698b | 2009-11-03 11:36:39 -0500 | [diff] [blame] | 13 | #include <linux/types.h> |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 14 | |
Nikita Kiryanov | 6c6ccdf | 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 | d34b456 | 2014-10-13 23:42:04 -0600 | [diff] [blame] | 28 | struct spi_slave; |
Jagannadha Sutradharudu Teki | 20e45bc | 2013-12-23 23:34:42 +0530 | [diff] [blame] | 29 | |
| 30 | /** |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 31 | * struct spi_flash - SPI flash structure |
| 32 | * |
| 33 | * @spi: SPI slave |
Jagan Teki | 7943612 | 2015-06-27 00:51:30 +0530 | [diff] [blame] | 34 | * @dev: SPI flash device |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 35 | * @name: Name of SPI flash |
Jagan Teki | 7943612 | 2015-06-27 00:51:30 +0530 | [diff] [blame] | 36 | * @dual_flash: Indicates dual flash memories - dual stacked, parallel |
Jagannadha Sutradharudu Teki | b47b741 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 37 | * @shift: Flash shift useful in dual parallel |
Jagan Teki | 4537cec | 2015-09-29 11:17:02 +0530 | [diff] [blame] | 38 | * @flags: Indication of spi flash flags |
Jagannadha Sutradharudu Teki | f000ad0 | 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 | 7943612 | 2015-06-27 00:51:30 +0530 | [diff] [blame] | 42 | * @erase_size: Erase size |
Jagannadha Sutradharudu Teki | f000ad0 | 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 | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 46 | * @erase_cmd: Erase cmd 4K, 32K, 64K |
Jagannadha Sutradharudu Teki | e0ebabc | 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 | 7943612 | 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 | 1cd8761 | 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 | f000ad0 | 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 | c0a19fd | 2014-03-04 17:24:09 +0200 | [diff] [blame] | 56 | * @write: Flash write ops: Write len bytes from buf into offset |
Jagannadha Sutradharudu Teki | f000ad0 | 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 | c0a19fd | 2014-03-04 17:24:09 +0200 | [diff] [blame] | 60 | * return 0 - Success, 1 - Failure |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 61 | */ |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 62 | struct spi_flash { |
| 63 | struct spi_slave *spi; |
Pavel Machek | 026f1fd | 2015-04-21 10:37:45 +0200 | [diff] [blame] | 64 | #ifdef CONFIG_DM_SPI_FLASH |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 65 | struct udevice *dev; |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 66 | #endif |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 67 | const char *name; |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 68 | u8 dual_flash; |
Jagannadha Sutradharudu Teki | b47b741 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 69 | u8 shift; |
Jagan Teki | 4537cec | 2015-09-29 11:17:02 +0530 | [diff] [blame] | 70 | u16 flags; |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 71 | |
Jagannadha Sutradharudu Teki | f000ad0 | 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 | c6d173d | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 76 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagannadha Sutradharudu Teki | f000ad0 | 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 | c6d173d | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 80 | #endif |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 81 | u8 erase_cmd; |
Jagannadha Sutradharudu Teki | 02eee9a | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 82 | u8 read_cmd; |
Jagannadha Sutradharudu Teki | e0ebabc | 2014-01-11 15:13:11 +0530 | [diff] [blame] | 83 | u8 write_cmd; |
Jagannadha Sutradharudu Teki | ca79986 | 2014-01-11 16:50:45 +0530 | [diff] [blame] | 84 | u8 dummy_byte; |
Jagannadha Sutradharudu Teki | 750f3ac | 2013-06-21 15:56:30 +0530 | [diff] [blame] | 85 | |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 86 | void *memory_map; |
Fabio Estevam | 1cd8761 | 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 | d6eedf7 | 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 | 026f1fd | 2015-04-21 10:37:45 +0200 | [diff] [blame] | 94 | * while the transition is in progress. |
Simon Glass | d6eedf7 | 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 | 026f1fd | 2015-04-21 10:37:45 +0200 | [diff] [blame] | 100 | * if required, perhaps with a way of scanning through the list to |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 101 | * find the driver that matches the device. |
| 102 | */ |
Jagannadha Sutradharudu Teki | f000ad0 | 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 | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 107 | #endif |
| 108 | }; |
| 109 | |
| 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); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 117 | /* Access the serial operations for a device */ |
| 118 | #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops) |
| 119 | |
| 120 | #ifdef CONFIG_DM_SPI_FLASH |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 121 | /** |
| 122 | * spi_flash_read_dm() - Read data from SPI flash |
| 123 | * |
| 124 | * @dev: SPI flash device |
| 125 | * @offset: Offset into device in bytes to read from |
| 126 | * @len: Number of bytes to read |
| 127 | * @buf: Buffer to put the data that is read |
| 128 | * @return 0 if OK, -ve on error |
| 129 | */ |
| 130 | int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf); |
| 131 | |
| 132 | /** |
| 133 | * spi_flash_write_dm() - Write data to SPI flash |
| 134 | * |
| 135 | * @dev: SPI flash device |
| 136 | * @offset: Offset into device in bytes to write to |
| 137 | * @len: Number of bytes to write |
| 138 | * @buf: Buffer containing bytes to write |
| 139 | * @return 0 if OK, -ve on error |
| 140 | */ |
| 141 | int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len, |
| 142 | const void *buf); |
| 143 | |
| 144 | /** |
| 145 | * spi_flash_erase_dm() - Erase blocks of the SPI flash |
| 146 | * |
| 147 | * Note that @len must be a muiltiple of the flash sector size. |
| 148 | * |
| 149 | * @dev: SPI flash device |
| 150 | * @offset: Offset into device in bytes to start erasing |
| 151 | * @len: Number of bytes to erase |
| 152 | * @return 0 if OK, -ve on error |
| 153 | */ |
| 154 | int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len); |
| 155 | |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 156 | int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs, |
| 157 | unsigned int max_hz, unsigned int spi_mode, |
| 158 | struct udevice **devp); |
| 159 | |
| 160 | /* Compatibility function - this is the old U-Boot API */ |
| 161 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 162 | unsigned int max_hz, unsigned int spi_mode); |
| 163 | |
| 164 | /* Compatibility function - this is the old U-Boot API */ |
| 165 | void spi_flash_free(struct spi_flash *flash); |
| 166 | |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 167 | static inline int spi_flash_read(struct spi_flash *flash, u32 offset, |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 168 | size_t len, void *buf) |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 169 | { |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 170 | return spi_flash_read_dm(flash->dev, offset, len, buf); |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static inline int spi_flash_write(struct spi_flash *flash, u32 offset, |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 174 | size_t len, const void *buf) |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 175 | { |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 176 | return spi_flash_write_dm(flash->dev, offset, len, buf); |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 180 | size_t len) |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 181 | { |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 182 | return spi_flash_erase_dm(flash->dev, offset, len); |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | struct sandbox_state; |
| 186 | |
| 187 | int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, |
| 188 | struct udevice *bus, int of_offset, const char *spec); |
| 189 | |
| 190 | void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs); |
| 191 | |
| 192 | #else |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 193 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 194 | unsigned int max_hz, unsigned int spi_mode); |
Simon Glass | 49e9d2c | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 195 | |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 196 | void spi_flash_free(struct spi_flash *flash); |
| 197 | |
| 198 | static inline int spi_flash_read(struct spi_flash *flash, u32 offset, |
| 199 | size_t len, void *buf) |
| 200 | { |
| 201 | return flash->read(flash, offset, len, buf); |
| 202 | } |
| 203 | |
| 204 | static inline int spi_flash_write(struct spi_flash *flash, u32 offset, |
| 205 | size_t len, const void *buf) |
| 206 | { |
| 207 | return flash->write(flash, offset, len, buf); |
| 208 | } |
| 209 | |
| 210 | static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, |
| 211 | size_t len) |
| 212 | { |
| 213 | return flash->erase(flash, offset, len); |
| 214 | } |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 215 | #endif |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 216 | |
Fabio Estevam | 1cd8761 | 2015-11-05 12:43:42 -0200 | [diff] [blame] | 217 | static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len, |
| 218 | bool prot) |
| 219 | { |
Bin Meng | 66528f6 | 2015-11-13 02:46:26 -0800 | [diff] [blame] | 220 | if (!flash->flash_lock || !flash->flash_unlock) |
Fabio Estevam | 1cd8761 | 2015-11-05 12:43:42 -0200 | [diff] [blame] | 221 | return -EOPNOTSUPP; |
| 222 | |
| 223 | if (prot) |
| 224 | return flash->flash_lock(flash, ofs, len); |
| 225 | else |
| 226 | return flash->flash_unlock(flash, ofs, len); |
| 227 | } |
| 228 | |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 229 | #endif /* _SPI_FLASH_H_ */ |