2 * Common SPI flash Interface
4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
7 * SPDX-License-Identifier: GPL-2.0
13 #include <dm.h> /* Because we dereference struct udevice here */
14 #include <linux/types.h>
16 #ifndef CONFIG_SF_DEFAULT_SPEED
17 # define CONFIG_SF_DEFAULT_SPEED 1000000
19 #ifndef CONFIG_SF_DEFAULT_MODE
20 # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
22 #ifndef CONFIG_SF_DEFAULT_CS
23 # define CONFIG_SF_DEFAULT_CS 0
25 #ifndef CONFIG_SF_DEFAULT_BUS
26 # define CONFIG_SF_DEFAULT_BUS 0
32 * struct spi_flash - SPI flash structure
35 * @dev: SPI flash device
36 * @name: Name of SPI flash
37 * @dual_flash: Indicates dual flash memories - dual stacked, parallel
38 * @shift: Flash shift useful in dual parallel
39 * @flags: Indication of spi flash flags
40 * @size: Total flash size
41 * @page_size: Write (page) size
42 * @sector_size: Sector size
43 * @erase_size: Erase size
44 * @bank_read_cmd: Bank read cmd
45 * @bank_write_cmd: Bank write cmd
46 * @bank_curr: Current flash bank
47 * @erase_cmd: Erase cmd 4K, 32K, 64K
48 * @read_cmd: Read cmd - Array Fast, Extn read and quad read.
49 * @write_cmd: Write cmd - page and quad program.
50 * @dummy_byte: Dummy cycles for read operation.
51 * @memory_map: Address of read-only SPI flash access
52 * @flash_lock: lock a region of the SPI Flash
53 * @flash_unlock: unlock a region of the SPI Flash
54 * @flash_is_locked: check if a region of the SPI Flash is completely locked
55 * @read: Flash read ops: Read len bytes at offset into buf
56 * Supported cmds: Fast Array Read
57 * @write: Flash write ops: Write len bytes from buf into offset
58 * Supported cmds: Page Program
59 * @erase: Flash erase ops: Erase len bytes from offset
60 * Supported cmds: Sector erase 4K, 32K, 64K
61 * return 0 - Success, 1 - Failure
64 struct spi_slave *spi;
65 #ifdef CONFIG_DM_SPI_FLASH
77 #ifdef CONFIG_SPI_FLASH_BAR
89 int (*flash_lock)(struct spi_flash *flash, u32 ofs, size_t len);
90 int (*flash_unlock)(struct spi_flash *flash, u32 ofs, size_t len);
91 int (*flash_is_locked)(struct spi_flash *flash, u32 ofs, size_t len);
92 #ifndef CONFIG_DM_SPI_FLASH
94 * These are not strictly needed for driver model, but keep them here
95 * while the transition is in progress.
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
101 * if required, perhaps with a way of scanning through the list to
102 * find the driver that matches the device.
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,
107 int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
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,
115 int (*erase)(struct udevice *dev, u32 offset, size_t len);
118 /* Access the serial operations for a device */
119 #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
121 #ifdef CONFIG_DM_SPI_FLASH
123 * spi_flash_read_dm() - Read data from SPI flash
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
131 int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
134 * spi_flash_write_dm() - Write data to SPI flash
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
142 int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
146 * spi_flash_erase_dm() - Erase blocks of the SPI flash
148 * Note that @len must be a muiltiple of the flash sector size.
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
155 int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
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);
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);
165 /* Compatibility function - this is the old U-Boot API */
166 void spi_flash_free(struct spi_flash *flash);
168 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
169 size_t len, void *buf)
171 return spi_flash_read_dm(flash->dev, offset, len, buf);
174 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
175 size_t len, const void *buf)
177 return spi_flash_write_dm(flash->dev, offset, len, buf);
180 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
183 return spi_flash_erase_dm(flash->dev, offset, len);
186 struct sandbox_state;
188 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
189 struct udevice *bus, int of_offset, const char *spec);
191 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
194 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
195 unsigned int max_hz, unsigned int spi_mode);
198 * Set up a new SPI flash from an fdt node
200 * @param blob Device tree blob
201 * @param slave_node Pointer to this SPI slave node in the device tree
202 * @param spi_node Cached pointer to the SPI interface this node belongs
204 * @return 0 if ok, -1 on error
206 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
209 void spi_flash_free(struct spi_flash *flash);
211 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
212 size_t len, void *buf)
214 return flash->read(flash, offset, len, buf);
217 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
218 size_t len, const void *buf)
220 return flash->write(flash, offset, len, buf);
223 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
226 return flash->erase(flash, offset, len);
230 static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
233 if (!flash->flash_lock || !flash->flash_unlock)
237 return flash->flash_lock(flash, ofs, len);
239 return flash->flash_unlock(flash, ofs, len);
242 #endif /* _SPI_FLASH_H_ */