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