Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 1 | /* |
Jagannadha Sutradharudu Teki | 25dc86a | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 2 | * Common SPI flash Interface |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2008 Atmel Corporation |
Jagannadha Sutradharudu Teki | 25dc86a | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 5 | * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc. |
Haavard Skinnemoen | 2f5bfb7 | 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 | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 12 | * version 2 as published by the Free Software Foundation. |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 13 | */ |
Jagannadha Sutradharudu Teki | 25dc86a | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 14 | |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 15 | #ifndef _SPI_FLASH_H_ |
| 16 | #define _SPI_FLASH_H_ |
| 17 | |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 18 | #include <dm.h> /* Because we dereference struct udevice here */ |
Mike Frysinger | e4c698b | 2009-11-03 11:36:39 -0500 | [diff] [blame] | 19 | #include <linux/types.h> |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 20 | |
Nikita Kiryanov | 6c6ccdf | 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 | d34b456 | 2014-10-13 23:42:04 -0600 | [diff] [blame] | 34 | struct spi_slave; |
Jagannadha Sutradharudu Teki | 20e45bc | 2013-12-23 23:34:42 +0530 | [diff] [blame] | 35 | |
| 36 | /** |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 37 | * struct spi_flash - SPI flash structure |
| 38 | * |
| 39 | * @spi: SPI slave |
| 40 | * @name: Name of SPI flash |
Simon Glass | d34b456 | 2014-10-13 23:42:04 -0600 | [diff] [blame] | 41 | * @dual_flash: Indicates dual flash memories - dual stacked, parallel |
Jagannadha Sutradharudu Teki | b47b741 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 42 | * @shift: Flash shift useful in dual parallel |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 43 | * @size: Total flash size |
| 44 | * @page_size: Write (page) size |
| 45 | * @sector_size: Sector size |
Simon Glass | d34b456 | 2014-10-13 23:42:04 -0600 | [diff] [blame] | 46 | * @erase_size: Erase size |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 47 | * @bank_read_cmd: Bank read cmd |
| 48 | * @bank_write_cmd: Bank write cmd |
| 49 | * @bank_curr: Current flash bank |
| 50 | * @poll_cmd: Poll cmd - for flash erase/program |
| 51 | * @erase_cmd: Erase cmd 4K, 32K, 64K |
Jagannadha Sutradharudu Teki | e0ebabc | 2014-01-11 15:13:11 +0530 | [diff] [blame] | 52 | * @read_cmd: Read cmd - Array Fast, Extn read and quad read. |
| 53 | * @write_cmd: Write cmd - page and quad program. |
Simon Glass | d34b456 | 2014-10-13 23:42:04 -0600 | [diff] [blame] | 54 | * @dummy_byte: Dummy cycles for read operation. |
| 55 | * @memory_map: Address of read-only SPI flash access |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 56 | * @read: Flash read ops: Read len bytes at offset into buf |
| 57 | * Supported cmds: Fast Array Read |
Vasili Galka | c0a19fd | 2014-03-04 17:24:09 +0200 | [diff] [blame] | 58 | * @write: Flash write ops: Write len bytes from buf into offset |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 59 | * Supported cmds: Page Program |
| 60 | * @erase: Flash erase ops: Erase len bytes from offset |
| 61 | * Supported cmds: Sector erase 4K, 32K, 64K |
Vasili Galka | c0a19fd | 2014-03-04 17:24:09 +0200 | [diff] [blame] | 62 | * return 0 - Success, 1 - Failure |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 63 | */ |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 64 | struct spi_flash { |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 65 | #ifdef CONFIG_DM_SPI_FLASH |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 66 | struct spi_slave *spi; |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 67 | struct udevice *dev; |
| 68 | #else |
| 69 | struct spi_slave *spi; |
| 70 | #endif |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 71 | const char *name; |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 72 | u8 dual_flash; |
Jagannadha Sutradharudu Teki | b47b741 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 73 | u8 shift; |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 74 | |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 75 | u32 size; |
| 76 | u32 page_size; |
| 77 | u32 sector_size; |
| 78 | u32 erase_size; |
Jagannadha Sutradharudu Teki | c6d173d | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 79 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 80 | u8 bank_read_cmd; |
| 81 | u8 bank_write_cmd; |
| 82 | u8 bank_curr; |
Jagannadha Sutradharudu Teki | c6d173d | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 83 | #endif |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 84 | u8 poll_cmd; |
| 85 | u8 erase_cmd; |
Jagannadha Sutradharudu Teki | 02eee9a | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 86 | u8 read_cmd; |
Jagannadha Sutradharudu Teki | e0ebabc | 2014-01-11 15:13:11 +0530 | [diff] [blame] | 87 | u8 write_cmd; |
Jagannadha Sutradharudu Teki | ca79986 | 2014-01-11 16:50:45 +0530 | [diff] [blame] | 88 | u8 dummy_byte; |
Jagannadha Sutradharudu Teki | 750f3ac | 2013-06-21 15:56:30 +0530 | [diff] [blame] | 89 | |
Jagannadha Sutradharudu Teki | f000ad0 | 2013-09-28 18:10:43 +0530 | [diff] [blame] | 90 | void *memory_map; |
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 |
| 94 | * whilt the transition is in progress. |
| 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 |
| 100 | * if requird, perhaps with a way of scanning through the list to |
| 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 | |
| 167 | int spi_flash_remove(struct udevice *flash); |
| 168 | |
| 169 | static inline int spi_flash_read(struct spi_flash *flash, u32 offset, |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 170 | size_t len, void *buf) |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 171 | { |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 172 | return spi_flash_read_dm(flash->dev, offset, len, buf); |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static inline int spi_flash_write(struct spi_flash *flash, u32 offset, |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 176 | size_t len, const void *buf) |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 177 | { |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 178 | return spi_flash_write_dm(flash->dev, offset, len, buf); |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 182 | size_t len) |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 183 | { |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 184 | return spi_flash_erase_dm(flash->dev, offset, len); |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | struct sandbox_state; |
| 188 | |
| 189 | int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, |
| 190 | struct udevice *bus, int of_offset, const char *spec); |
| 191 | |
| 192 | void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs); |
| 193 | |
| 194 | #else |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 195 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 196 | unsigned int max_hz, unsigned int spi_mode); |
Simon Glass | 49e9d2c | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * Set up a new SPI flash from an fdt node |
| 200 | * |
| 201 | * @param blob Device tree blob |
| 202 | * @param slave_node Pointer to this SPI slave node in the device tree |
| 203 | * @param spi_node Cached pointer to the SPI interface this node belongs |
| 204 | * to |
| 205 | * @return 0 if ok, -1 on error |
| 206 | */ |
| 207 | struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node, |
| 208 | int spi_node); |
| 209 | |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 210 | void spi_flash_free(struct spi_flash *flash); |
| 211 | |
| 212 | static inline int spi_flash_read(struct spi_flash *flash, u32 offset, |
| 213 | size_t len, void *buf) |
| 214 | { |
| 215 | return flash->read(flash, offset, len, buf); |
| 216 | } |
| 217 | |
| 218 | static inline int spi_flash_write(struct spi_flash *flash, u32 offset, |
| 219 | size_t len, const void *buf) |
| 220 | { |
| 221 | return flash->write(flash, offset, len, buf); |
| 222 | } |
| 223 | |
| 224 | static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, |
| 225 | size_t len) |
| 226 | { |
| 227 | return flash->erase(flash, offset, len); |
| 228 | } |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 229 | #endif |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 230 | |
Christian Riesch | 23f16a8 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 231 | void spi_boot(void) __noreturn; |
Prabhakar Kushwaha | 9ea255a | 2014-04-08 19:13:22 +0530 | [diff] [blame] | 232 | void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst); |
Christian Riesch | 23f16a8 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 233 | |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 234 | #endif /* _SPI_FLASH_H_ */ |