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 <linux/types.h>
13 #include <linux/mtd/spi-nor.h>
17 /* by default ENV use the same parameters than SF command */
18 #ifndef CONFIG_ENV_SPI_BUS
19 # define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS
21 #ifndef CONFIG_ENV_SPI_CS
22 # define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS
24 #ifndef CONFIG_ENV_SPI_MAX_HZ
25 # define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED
27 #ifndef CONFIG_ENV_SPI_MODE
28 # define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE
33 struct dm_spi_flash_ops {
34 int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
35 int (*write)(struct udevice *dev, u32 offset, size_t len,
37 int (*erase)(struct udevice *dev, u32 offset, size_t len);
39 * get_sw_write_prot() - Check state of software write-protect feature
41 * SPI flash chips can lock a region of the flash defined by a
42 * 'protected area'. This function checks if this protected area is
45 * @dev: SPI flash device
46 * @return 0 if no region is write-protected, 1 if a region is
47 * write-protected, -ENOSYS if the driver does not implement this,
48 * other -ve value on error
50 int (*get_sw_write_prot)(struct udevice *dev);
53 /* Access the serial operations for a device */
54 #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
56 #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
58 * spi_flash_read_dm() - Read data from SPI flash
60 * @dev: SPI flash device
61 * @offset: Offset into device in bytes to read from
62 * @len: Number of bytes to read
63 * @buf: Buffer to put the data that is read
64 * @return 0 if OK, -ve on error
66 int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
69 * spi_flash_write_dm() - Write data to SPI flash
71 * @dev: SPI flash device
72 * @offset: Offset into device in bytes to write to
73 * @len: Number of bytes to write
74 * @buf: Buffer containing bytes to write
75 * @return 0 if OK, -ve on error
77 int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
81 * spi_flash_erase_dm() - Erase blocks of the SPI flash
83 * Note that @len must be a muiltiple of the flash sector size.
85 * @dev: SPI flash device
86 * @offset: Offset into device in bytes to start erasing
87 * @len: Number of bytes to erase
88 * @return 0 if OK, -ve on error
90 int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
93 * spl_flash_get_sw_write_prot() - Check state of software write-protect feature
95 * SPI flash chips can lock a region of the flash defined by a
96 * 'protected area'. This function checks if this protected area is
99 * @dev: SPI flash device
100 * @return 0 if no region is write-protected, 1 if a region is
101 * write-protected, -ENOSYS if the driver does not implement this,
102 * other -ve value on error
104 int spl_flash_get_sw_write_prot(struct udevice *dev);
107 * spi_flash_std_probe() - Probe a SPI flash device
109 * This is the standard internal method for probing a SPI flash device to
110 * determine its type. It can be used in chip-specific drivers which need to
111 * do this, typically with of-platdata
113 * @dev: SPI-flash device to probe
114 * @return 0 if OK, -ve on error
116 int spi_flash_std_probe(struct udevice *dev);
118 int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
119 unsigned int max_hz, unsigned int spi_mode,
120 struct udevice **devp);
122 /* Compatibility function - this is the old U-Boot API */
123 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
124 unsigned int max_hz, unsigned int spi_mode);
126 /* Compatibility function - this is the old U-Boot API */
127 static inline void spi_flash_free(struct spi_flash *flash)
131 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
132 size_t len, void *buf)
134 return spi_flash_read_dm(flash->dev, offset, len, buf);
137 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
138 size_t len, const void *buf)
140 return spi_flash_write_dm(flash->dev, offset, len, buf);
143 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
146 return spi_flash_erase_dm(flash->dev, offset, len);
149 struct sandbox_state;
151 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
152 struct udevice *bus, ofnode node, const char *spec);
154 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
157 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
158 unsigned int max_hz, unsigned int spi_mode);
160 void spi_flash_free(struct spi_flash *flash);
162 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
163 size_t len, void *buf)
165 struct mtd_info *mtd = &flash->mtd;
168 return mtd->_read(mtd, offset, len, &retlen, buf);
171 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
172 size_t len, const void *buf)
174 struct mtd_info *mtd = &flash->mtd;
177 return mtd->_write(mtd, offset, len, &retlen, buf);
180 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
183 struct mtd_info *mtd = &flash->mtd;
184 struct erase_info instr;
186 if (offset % mtd->erasesize || len % mtd->erasesize) {
187 printf("SF: Erase offset/length not multiple of erase size\n");
191 memset(&instr, 0, sizeof(instr));
195 return mtd->_erase(mtd, &instr);
199 static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
202 if (!flash->flash_lock || !flash->flash_unlock)
206 return flash->flash_lock(flash, ofs, len);
208 return flash->flash_unlock(flash, ofs, len);
211 #endif /* _SPI_FLASH_H_ */