Commit | Line | Data |
---|---|---|
d25ce7d2 | 1 | /* |
a5e8199a | 2 | * Common SPI flash Interface |
d25ce7d2 HS |
3 | * |
4 | * Copyright (C) 2008 Atmel Corporation | |
a5e8199a | 5 | * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc. |
d25ce7d2 HS |
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 | |
3765b3e7 | 12 | * version 2 as published by the Free Software Foundation. |
d25ce7d2 | 13 | */ |
a5e8199a | 14 | |
d25ce7d2 HS |
15 | #ifndef _SPI_FLASH_H_ |
16 | #define _SPI_FLASH_H_ | |
17 | ||
4c2dbefd | 18 | #include <dm.h> /* Because we dereference struct udevice here */ |
e06ab654 | 19 | #include <linux/types.h> |
d25ce7d2 | 20 | |
88e34e5f NK |
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 | ||
ff0960f9 | 34 | struct spi_slave; |
33adfb5f | 35 | |
7ab35d92 JT |
36 | /** |
37 | * struct spi_flash - SPI flash structure | |
38 | * | |
39 | * @spi: SPI slave | |
5d69df35 JT |
40 | * @dev: SPI flash device |
41 | * @flags: Indication of spi flash flags | |
7ab35d92 | 42 | * @name: Name of SPI flash |
5d69df35 | 43 | * @dual_flash: Indicates dual flash memories - dual stacked, parallel |
056fbc73 | 44 | * @shift: Flash shift useful in dual parallel |
7ab35d92 JT |
45 | * @size: Total flash size |
46 | * @page_size: Write (page) size | |
47 | * @sector_size: Sector size | |
5d69df35 | 48 | * @erase_size: Erase size |
7ab35d92 JT |
49 | * @bank_read_cmd: Bank read cmd |
50 | * @bank_write_cmd: Bank write cmd | |
51 | * @bank_curr: Current flash bank | |
52 | * @poll_cmd: Poll cmd - for flash erase/program | |
53 | * @erase_cmd: Erase cmd 4K, 32K, 64K | |
3163aaa6 JT |
54 | * @read_cmd: Read cmd - Array Fast, Extn read and quad read. |
55 | * @write_cmd: Write cmd - page and quad program. | |
5d69df35 JT |
56 | * @dummy_byte: Dummy cycles for read operation. |
57 | * @memory_map: Address of read-only SPI flash access | |
7ab35d92 JT |
58 | * @read: Flash read ops: Read len bytes at offset into buf |
59 | * Supported cmds: Fast Array Read | |
801cec59 | 60 | * @write: Flash write ops: Write len bytes from buf into offset |
7ab35d92 JT |
61 | * Supported cmds: Page Program |
62 | * @erase: Flash erase ops: Erase len bytes from offset | |
63 | * Supported cmds: Sector erase 4K, 32K, 64K | |
801cec59 | 64 | * return 0 - Success, 1 - Failure |
7ab35d92 | 65 | */ |
d25ce7d2 HS |
66 | struct spi_flash { |
67 | struct spi_slave *spi; | |
d15e74f1 | 68 | #ifdef CONFIG_DM_SPI_FLASH |
4c2dbefd | 69 | struct udevice *dev; |
be7be78e | 70 | u16 flags; |
4c2dbefd | 71 | #endif |
7ab35d92 | 72 | const char *name; |
f77f4691 | 73 | u8 dual_flash; |
056fbc73 | 74 | u8 shift; |
d25ce7d2 | 75 | |
7ab35d92 JT |
76 | u32 size; |
77 | u32 page_size; | |
78 | u32 sector_size; | |
79 | u32 erase_size; | |
1dcd6d03 | 80 | #ifdef CONFIG_SPI_FLASH_BAR |
7ab35d92 JT |
81 | u8 bank_read_cmd; |
82 | u8 bank_write_cmd; | |
83 | u8 bank_curr; | |
1dcd6d03 | 84 | #endif |
7ab35d92 JT |
85 | u8 poll_cmd; |
86 | u8 erase_cmd; | |
4e09cc1e | 87 | u8 read_cmd; |
3163aaa6 | 88 | u8 write_cmd; |
ff063ed4 | 89 | u8 dummy_byte; |
615a1561 | 90 | |
7ab35d92 | 91 | void *memory_map; |
4c2dbefd SG |
92 | #ifndef CONFIG_DM_SPI_FLASH |
93 | /* | |
94 | * These are not strictly needed for driver model, but keep them here | |
d15e74f1 | 95 | * while the transition is in progress. |
4c2dbefd SG |
96 | * |
97 | * Normally each driver would provide its own operations, but for | |
98 | * SPI flash most chips use the same algorithms. One approach is | |
99 | * to create a 'common' SPI flash device which knows how to talk | |
100 | * to most devices, and then allow other drivers to be used instead | |
d15e74f1 | 101 | * if required, perhaps with a way of scanning through the list to |
4c2dbefd SG |
102 | * find the driver that matches the device. |
103 | */ | |
7ab35d92 JT |
104 | int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf); |
105 | int (*write)(struct spi_flash *flash, u32 offset, size_t len, | |
106 | const void *buf); | |
107 | int (*erase)(struct spi_flash *flash, u32 offset, size_t len); | |
4c2dbefd SG |
108 | #endif |
109 | }; | |
110 | ||
111 | struct dm_spi_flash_ops { | |
112 | int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf); | |
113 | int (*write)(struct udevice *dev, u32 offset, size_t len, | |
114 | const void *buf); | |
115 | int (*erase)(struct udevice *dev, u32 offset, size_t len); | |
d25ce7d2 HS |
116 | }; |
117 | ||
4c2dbefd SG |
118 | /* Access the serial operations for a device */ |
119 | #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops) | |
120 | ||
121 | #ifdef CONFIG_DM_SPI_FLASH | |
8d987abc SG |
122 | /** |
123 | * spi_flash_read_dm() - Read data from SPI flash | |
124 | * | |
125 | * @dev: SPI flash device | |
126 | * @offset: Offset into device in bytes to read from | |
127 | * @len: Number of bytes to read | |
128 | * @buf: Buffer to put the data that is read | |
129 | * @return 0 if OK, -ve on error | |
130 | */ | |
131 | int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf); | |
132 | ||
133 | /** | |
134 | * spi_flash_write_dm() - Write data to SPI flash | |
135 | * | |
136 | * @dev: SPI flash device | |
137 | * @offset: Offset into device in bytes to write to | |
138 | * @len: Number of bytes to write | |
139 | * @buf: Buffer containing bytes to write | |
140 | * @return 0 if OK, -ve on error | |
141 | */ | |
142 | int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len, | |
143 | const void *buf); | |
144 | ||
145 | /** | |
146 | * spi_flash_erase_dm() - Erase blocks of the SPI flash | |
147 | * | |
148 | * Note that @len must be a muiltiple of the flash sector size. | |
149 | * | |
150 | * @dev: SPI flash device | |
151 | * @offset: Offset into device in bytes to start erasing | |
152 | * @len: Number of bytes to erase | |
153 | * @return 0 if OK, -ve on error | |
154 | */ | |
155 | int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len); | |
156 | ||
4c2dbefd SG |
157 | int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs, |
158 | unsigned int max_hz, unsigned int spi_mode, | |
159 | struct udevice **devp); | |
160 | ||
161 | /* Compatibility function - this is the old U-Boot API */ | |
162 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, | |
163 | unsigned int max_hz, unsigned int spi_mode); | |
164 | ||
165 | /* Compatibility function - this is the old U-Boot API */ | |
166 | void spi_flash_free(struct spi_flash *flash); | |
167 | ||
168 | int spi_flash_remove(struct udevice *flash); | |
169 | ||
170 | static inline int spi_flash_read(struct spi_flash *flash, u32 offset, | |
8d987abc | 171 | size_t len, void *buf) |
4c2dbefd | 172 | { |
8d987abc | 173 | return spi_flash_read_dm(flash->dev, offset, len, buf); |
4c2dbefd SG |
174 | } |
175 | ||
176 | static inline int spi_flash_write(struct spi_flash *flash, u32 offset, | |
8d987abc | 177 | size_t len, const void *buf) |
4c2dbefd | 178 | { |
8d987abc | 179 | return spi_flash_write_dm(flash->dev, offset, len, buf); |
4c2dbefd SG |
180 | } |
181 | ||
182 | static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, | |
8d987abc | 183 | size_t len) |
4c2dbefd | 184 | { |
8d987abc | 185 | return spi_flash_erase_dm(flash->dev, offset, len); |
4c2dbefd SG |
186 | } |
187 | ||
188 | struct sandbox_state; | |
189 | ||
190 | int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, | |
191 | struct udevice *bus, int of_offset, const char *spec); | |
192 | ||
193 | void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs); | |
194 | ||
195 | #else | |
d25ce7d2 HS |
196 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
197 | unsigned int max_hz, unsigned int spi_mode); | |
0efc0249 SG |
198 | |
199 | /** | |
200 | * Set up a new SPI flash from an fdt node | |
201 | * | |
202 | * @param blob Device tree blob | |
203 | * @param slave_node Pointer to this SPI slave node in the device tree | |
204 | * @param spi_node Cached pointer to the SPI interface this node belongs | |
205 | * to | |
206 | * @return 0 if ok, -1 on error | |
207 | */ | |
208 | struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node, | |
209 | int spi_node); | |
210 | ||
d25ce7d2 HS |
211 | void spi_flash_free(struct spi_flash *flash); |
212 | ||
213 | static inline int spi_flash_read(struct spi_flash *flash, u32 offset, | |
214 | size_t len, void *buf) | |
215 | { | |
216 | return flash->read(flash, offset, len, buf); | |
217 | } | |
218 | ||
219 | static inline int spi_flash_write(struct spi_flash *flash, u32 offset, | |
220 | size_t len, const void *buf) | |
221 | { | |
222 | return flash->write(flash, offset, len, buf); | |
223 | } | |
224 | ||
225 | static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, | |
226 | size_t len) | |
227 | { | |
228 | return flash->erase(flash, offset, len); | |
229 | } | |
4c2dbefd | 230 | #endif |
d25ce7d2 | 231 | |
32b11273 | 232 | void spi_boot(void) __noreturn; |
1eaa742d | 233 | void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst); |
32b11273 | 234 | |
d25ce7d2 | 235 | #endif /* _SPI_FLASH_H_ */ |