1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Common SPI flash Interface
5 * Copyright (C) 2008 Atmel Corporation
6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
12 #include <dm.h> /* Because we dereference struct udevice here */
13 #include <linux/types.h>
15 #ifndef CONFIG_SF_DEFAULT_SPEED
16 # define CONFIG_SF_DEFAULT_SPEED 1000000
18 #ifndef CONFIG_SF_DEFAULT_MODE
19 # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
21 #ifndef CONFIG_SF_DEFAULT_CS
22 # define CONFIG_SF_DEFAULT_CS 0
24 #ifndef CONFIG_SF_DEFAULT_BUS
25 # define CONFIG_SF_DEFAULT_BUS 0
31 * struct spi_flash - SPI flash structure
34 * @dev: SPI flash device
35 * @name: Name of SPI flash
36 * @dual_flash: Indicates dual flash memories - dual stacked, parallel
37 * @shift: Flash shift useful in dual parallel
38 * @flags: Indication of spi flash flags
39 * @size: Total flash size
40 * @page_size: Write (page) size
41 * @sector_size: Sector size
42 * @erase_size: Erase size
43 * @bank_read_cmd: Bank read cmd
44 * @bank_write_cmd: Bank write cmd
45 * @bank_curr: Current flash bank
46 * @erase_cmd: Erase cmd 4K, 32K, 64K
47 * @read_cmd: Read cmd - Array Fast, Extn read and quad read.
48 * @write_cmd: Write cmd - page and quad program.
49 * @dummy_byte: Dummy cycles for read operation.
50 * @memory_map: Address of read-only SPI flash access
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
54 * @read: Flash read ops: Read len bytes at offset into buf
55 * Supported cmds: Fast Array Read
56 * @write: Flash write ops: Write len bytes from buf into offset
57 * Supported cmds: Page Program
58 * @erase: Flash erase ops: Erase len bytes from offset
59 * Supported cmds: Sector erase 4K, 32K, 64K
60 * return 0 - Success, 1 - Failure
63 struct spi_slave *spi;
64 #ifdef CONFIG_DM_SPI_FLASH
76 #ifdef CONFIG_SPI_FLASH_BAR
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);
91 #ifndef CONFIG_DM_SPI_FLASH
93 * These are not strictly needed for driver model, but keep them here
94 * while the transition is in progress.
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 required, perhaps with a way of scanning through the list to
101 * find the driver that matches the device.
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,
106 int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
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,
114 int (*erase)(struct udevice *dev, u32 offset, size_t len);
117 /* Access the serial operations for a device */
118 #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
120 #ifdef CONFIG_DM_SPI_FLASH
122 * spi_flash_read_dm() - Read data from SPI flash
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
130 int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
133 * spi_flash_write_dm() - Write data to SPI flash
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
141 int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
145 * spi_flash_erase_dm() - Erase blocks of the SPI flash
147 * Note that @len must be a muiltiple of the flash sector size.
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
154 int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
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);
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);
164 /* Compatibility function - this is the old U-Boot API */
165 void spi_flash_free(struct spi_flash *flash);
167 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
168 size_t len, void *buf)
170 return spi_flash_read_dm(flash->dev, offset, len, buf);
173 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
174 size_t len, const void *buf)
176 return spi_flash_write_dm(flash->dev, offset, len, buf);
179 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
182 return spi_flash_erase_dm(flash->dev, offset, len);
185 struct sandbox_state;
187 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
188 struct udevice *bus, int of_offset, const char *spec);
190 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
193 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
194 unsigned int max_hz, unsigned int spi_mode);
196 void spi_flash_free(struct spi_flash *flash);
198 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
199 size_t len, void *buf)
201 return flash->read(flash, offset, len, buf);
204 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
205 size_t len, const void *buf)
207 return flash->write(flash, offset, len, buf);
210 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
213 return flash->erase(flash, offset, len);
217 static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
220 if (!flash->flash_lock || !flash->flash_unlock)
224 return flash->flash_lock(flash, ofs, len);
226 return flash->flash_unlock(flash, ofs, len);
229 #endif /* _SPI_FLASH_H_ */