xref: /rk3399_rockchip-uboot/include/spi_flash.h (revision 90108c6c51a1d7dcea7ee7753f7af05735b371b4)
1 /*
2  * Common SPI flash Interface
3  *
4  * Copyright (C) 2008 Atmel Corporation
5  * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
6  *
7  * SPDX-License-Identifier:	GPL-2.0
8  */
9 
10 #ifndef _SPI_FLASH_H_
11 #define _SPI_FLASH_H_
12 
13 #include <dm.h>	/* Because we dereference struct udevice here */
14 #include <linux/types.h>
15 
16 #ifndef CONFIG_SF_DEFAULT_SPEED
17 # define CONFIG_SF_DEFAULT_SPEED	1000000
18 #endif
19 #ifndef CONFIG_SF_DEFAULT_MODE
20 # define CONFIG_SF_DEFAULT_MODE		SPI_MODE_3
21 #endif
22 #ifndef CONFIG_SF_DEFAULT_CS
23 # define CONFIG_SF_DEFAULT_CS		0
24 #endif
25 #ifndef CONFIG_SF_DEFAULT_BUS
26 # define CONFIG_SF_DEFAULT_BUS		0
27 #endif
28 
29 struct spi_slave;
30 
31 /**
32  * struct spi_flash - SPI flash structure
33  *
34  * @spi:		SPI slave
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
62  */
63 struct spi_flash {
64 	struct spi_slave *spi;
65 #ifdef CONFIG_DM_SPI_FLASH
66 	struct udevice *dev;
67 #endif
68 	const char *name;
69 	u8 dual_flash;
70 	u8 shift;
71 	u16 flags;
72 
73 	u32 size;
74 	u32 page_size;
75 	u32 sector_size;
76 	u32 erase_size;
77 #ifdef CONFIG_SPI_FLASH_BAR
78 	u8 bank_read_cmd;
79 	u8 bank_write_cmd;
80 	u8 bank_curr;
81 #endif
82 	u8 erase_cmd;
83 	u8 read_cmd;
84 	u8 write_cmd;
85 	u8 dummy_byte;
86 
87 	void *memory_map;
88 
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
93 	/*
94 	 * These are not strictly needed for driver model, but keep them here
95 	 * while the transition is in progress.
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
101 	 * if required, perhaps with a way of scanning through the list to
102 	 * find the driver that matches the device.
103 	 */
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);
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);
116 	/**
117 	 * get_sw_write_prot() - Check state of software write-protect feature
118 	 *
119 	 * SPI flash chips can lock a region of the flash defined by a
120 	 * 'protected area'. This function checks if this protected area is
121 	 * defined.
122 	 *
123 	 * @dev:	SPI flash device
124 	 * @return 0 if no region is write-protected, 1 if a region is
125 	 *	write-protected, -ENOSYS if the driver does not implement this,
126 	 *	other -ve value on error
127 	 */
128 	int (*get_sw_write_prot)(struct udevice *dev);
129 };
130 
131 /* Access the serial operations for a device */
132 #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
133 
134 #ifdef CONFIG_DM_SPI_FLASH
135 /**
136  * spi_flash_read_dm() - Read data from SPI flash
137  *
138  * @dev:	SPI flash device
139  * @offset:	Offset into device in bytes to read from
140  * @len:	Number of bytes to read
141  * @buf:	Buffer to put the data that is read
142  * @return 0 if OK, -ve on error
143  */
144 int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
145 
146 /**
147  * spi_flash_write_dm() - Write data to SPI flash
148  *
149  * @dev:	SPI flash device
150  * @offset:	Offset into device in bytes to write to
151  * @len:	Number of bytes to write
152  * @buf:	Buffer containing bytes to write
153  * @return 0 if OK, -ve on error
154  */
155 int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
156 		       const void *buf);
157 
158 /**
159  * spi_flash_erase_dm() - Erase blocks of the SPI flash
160  *
161  * Note that @len must be a muiltiple of the flash sector size.
162  *
163  * @dev:	SPI flash device
164  * @offset:	Offset into device in bytes to start erasing
165  * @len:	Number of bytes to erase
166  * @return 0 if OK, -ve on error
167  */
168 int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
169 
170 /**
171  * spl_flash_get_sw_write_prot() - Check state of software write-protect feature
172  *
173  * SPI flash chips can lock a region of the flash defined by a
174  * 'protected area'. This function checks if this protected area is
175  * defined.
176  *
177  * @dev:	SPI flash device
178  * @return 0 if no region is write-protected, 1 if a region is
179  *	write-protected, -ENOSYS if the driver does not implement this,
180  *	other -ve value on error
181  */
182 int spl_flash_get_sw_write_prot(struct udevice *dev);
183 
184 int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
185 			   unsigned int max_hz, unsigned int spi_mode,
186 			   struct udevice **devp);
187 
188 /* Compatibility function - this is the old U-Boot API */
189 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
190 				  unsigned int max_hz, unsigned int spi_mode);
191 
192 /* Compatibility function - this is the old U-Boot API */
193 void spi_flash_free(struct spi_flash *flash);
194 
195 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
196 				 size_t len, void *buf)
197 {
198 	return spi_flash_read_dm(flash->dev, offset, len, buf);
199 }
200 
201 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
202 				  size_t len, const void *buf)
203 {
204 	return spi_flash_write_dm(flash->dev, offset, len, buf);
205 }
206 
207 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
208 				  size_t len)
209 {
210 	return spi_flash_erase_dm(flash->dev, offset, len);
211 }
212 
213 struct sandbox_state;
214 
215 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
216 			 struct udevice *bus, ofnode node, const char *spec);
217 
218 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
219 
220 #else
221 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
222 		unsigned int max_hz, unsigned int spi_mode);
223 
224 void spi_flash_free(struct spi_flash *flash);
225 
226 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
227 		size_t len, void *buf)
228 {
229 	return flash->read(flash, offset, len, buf);
230 }
231 
232 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
233 		size_t len, const void *buf)
234 {
235 	return flash->write(flash, offset, len, buf);
236 }
237 
238 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
239 		size_t len)
240 {
241 	return flash->erase(flash, offset, len);
242 }
243 #endif
244 
245 static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
246 					bool prot)
247 {
248 	if (!flash->flash_lock || !flash->flash_unlock)
249 		return -EOPNOTSUPP;
250 
251 	if (prot)
252 		return flash->flash_lock(flash, ofs, len);
253 	else
254 		return flash->flash_unlock(flash, ofs, len);
255 }
256 
257 #endif /* _SPI_FLASH_H_ */
258