1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Common SPI flash Interface
4  *
5  * Copyright (C) 2008 Atmel Corporation
6  * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
7  */
8 
9 #ifndef _SPI_FLASH_H_
10 #define _SPI_FLASH_H_
11 
12 #include <linux/types.h>
13 #include <linux/mtd/spi-nor.h>
14 
15 struct udevice;
16 
17 struct spi_slave;
18 
19 struct dm_spi_flash_ops {
20 	int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
21 	int (*write)(struct udevice *dev, u32 offset, size_t len,
22 		     const void *buf);
23 	int (*erase)(struct udevice *dev, u32 offset, size_t len);
24 	/**
25 	 * get_sw_write_prot() - Check state of software write-protect feature
26 	 *
27 	 * SPI flash chips can lock a region of the flash defined by a
28 	 * 'protected area'. This function checks if this protected area is
29 	 * defined.
30 	 *
31 	 * @dev:	SPI flash device
32 	 * @return 0 if no region is write-protected, 1 if a region is
33 	 *	write-protected, -ENOSYS if the driver does not implement this,
34 	 *	other -ve value on error
35 	 */
36 	int (*get_sw_write_prot)(struct udevice *dev);
37 };
38 
39 /* Access the serial operations for a device */
40 #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
41 
42 #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
43 /**
44  * spi_flash_read_dm() - Read data from SPI flash
45  *
46  * @dev:	SPI flash device
47  * @offset:	Offset into device in bytes to read from
48  * @len:	Number of bytes to read
49  * @buf:	Buffer to put the data that is read
50  * Return: 0 if OK, -ve on error
51  */
52 int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
53 
54 /**
55  * spi_flash_write_dm() - Write data to SPI flash
56  *
57  * @dev:	SPI flash device
58  * @offset:	Offset into device in bytes to write to
59  * @len:	Number of bytes to write
60  * @buf:	Buffer containing bytes to write
61  * Return: 0 if OK, -ve on error
62  */
63 int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
64 		       const void *buf);
65 
66 /**
67  * spi_flash_erase_dm() - Erase blocks of the SPI flash
68  *
69  * Note that @len must be a muiltiple of the flash sector size.
70  *
71  * @dev:	SPI flash device
72  * @offset:	Offset into device in bytes to start erasing
73  * @len:	Number of bytes to erase
74  * Return: 0 if OK, -ve on error
75  */
76 int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
77 
78 /**
79  * spl_flash_get_sw_write_prot() - Check state of software write-protect feature
80  *
81  * SPI flash chips can lock a region of the flash defined by a
82  * 'protected area'. This function checks if this protected area is
83  * defined.
84  *
85  * @dev:	SPI flash device
86  * Return: 0 if no region is write-protected, 1 if a region is
87  *	write-protected, -ENOSYS if the driver does not implement this,
88  *	other -ve value on error
89  */
90 int spl_flash_get_sw_write_prot(struct udevice *dev);
91 
92 /**
93  * spi_flash_std_probe() - Probe a SPI flash device
94  *
95  * This is the standard internal method for probing a SPI flash device to
96  * determine its type. It can be used in chip-specific drivers which need to
97  * do this, typically with of-platdata
98  *
99  * @dev: SPI-flash device to probe
100  * Return: 0 if OK, -ve on error
101  */
102 int spi_flash_std_probe(struct udevice *dev);
103 
104 int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
105 			   struct udevice **devp);
106 
107 /* Compatibility function - this is the old U-Boot API */
108 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
109 				  unsigned int max_hz, unsigned int spi_mode);
110 
111 /* Compatibility function - this is the old U-Boot API */
spi_flash_free(struct spi_flash * flash)112 static inline void spi_flash_free(struct spi_flash *flash)
113 {
114 }
115 
spi_flash_read(struct spi_flash * flash,u32 offset,size_t len,void * buf)116 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
117 				 size_t len, void *buf)
118 {
119 	return spi_flash_read_dm(flash->dev, offset, len, buf);
120 }
121 
spi_flash_write(struct spi_flash * flash,u32 offset,size_t len,const void * buf)122 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
123 				  size_t len, const void *buf)
124 {
125 	return spi_flash_write_dm(flash->dev, offset, len, buf);
126 }
127 
spi_flash_erase(struct spi_flash * flash,u32 offset,size_t len)128 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
129 				  size_t len)
130 {
131 	return spi_flash_erase_dm(flash->dev, offset, len);
132 }
133 
134 struct sandbox_state;
135 
136 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
137 			 struct udevice *bus, ofnode node, const char *spec);
138 
139 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
140 
141 #else
142 /* Compatibility functions for when DM_SPI_FLASH is disabled */
spi_flash_probe_bus_cs(unsigned int busnum,unsigned int cs,struct udevice ** devp)143 static inline int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
144 					 struct udevice **devp)
145 {
146 	return -ENODEV;
147 }
148 
spi_flash_read_dm(struct udevice * dev,u32 offset,size_t len,void * buf)149 static inline int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len,
150 				    void *buf)
151 {
152 	return -ENODEV;
153 }
154 
spi_flash_write_dm(struct udevice * dev,u32 offset,size_t len,const void * buf)155 static inline int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
156 				     const void *buf)
157 {
158 	return -ENODEV;
159 }
160 
spi_flash_erase_dm(struct udevice * dev,u32 offset,size_t len)161 static inline int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len)
162 {
163 	return -ENODEV;
164 }
165 
spl_flash_get_sw_write_prot(struct udevice * dev)166 static inline int spl_flash_get_sw_write_prot(struct udevice *dev)
167 {
168 	return -ENODEV;
169 }
170 
spi_flash_std_probe(struct udevice * dev)171 static inline int spi_flash_std_probe(struct udevice *dev)
172 {
173 	return -ENODEV;
174 }
175 
176 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
177 		unsigned int max_hz, unsigned int spi_mode);
178 
179 void spi_flash_free(struct spi_flash *flash);
180 
spi_flash_read(struct spi_flash * flash,u32 offset,size_t len,void * buf)181 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
182 		size_t len, void *buf)
183 {
184 	struct mtd_info *mtd = &flash->mtd;
185 	size_t retlen;
186 
187 	if (!len)
188 		return 0;
189 
190 	return mtd->_read(mtd, offset, len, &retlen, buf);
191 }
192 
spi_flash_write(struct spi_flash * flash,u32 offset,size_t len,const void * buf)193 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
194 		size_t len, const void *buf)
195 {
196 	struct mtd_info *mtd = &flash->mtd;
197 	size_t retlen;
198 
199 	if (!len)
200 		return 0;
201 
202 	return mtd->_write(mtd, offset, len, &retlen, buf);
203 }
204 
spi_flash_erase(struct spi_flash * flash,u32 offset,size_t len)205 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
206 		size_t len)
207 {
208 	struct mtd_info *mtd = &flash->mtd;
209 	struct erase_info instr;
210 
211 	if (offset % mtd->erasesize || len % mtd->erasesize) {
212 		printf("SF: Erase offset/length not multiple of erase size\n");
213 		return -EINVAL;
214 	}
215 
216 	if (!len)
217 		return 0;
218 
219 	memset(&instr, 0, sizeof(instr));
220 	instr.addr = offset;
221 	instr.len = len;
222 
223 	return mtd->_erase(mtd, &instr);
224 }
225 #endif
226 
spi_flash_protect(struct spi_flash * flash,u32 ofs,u32 len,bool prot)227 static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
228 					bool prot)
229 {
230 	if (!flash->flash_lock || !flash->flash_unlock)
231 		return -EOPNOTSUPP;
232 
233 	if (prot)
234 		return flash->flash_lock(flash, ofs, len);
235 	else
236 		return flash->flash_unlock(flash, ofs, len);
237 }
238 
239 #endif /* _SPI_FLASH_H_ */
240