xref: /rk3399_rockchip-uboot/include/spi_flash.h (revision d15e74f16cc521da0e08060cda2c87307bd4363f)
1 /*
2  * Common SPI flash Interface
3  *
4  * Copyright (C) 2008 Atmel Corporation
5  * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * version 2 as published by the Free Software Foundation.
13  */
14 
15 #ifndef _SPI_FLASH_H_
16 #define _SPI_FLASH_H_
17 
18 #include <dm.h>	/* Because we dereference struct udevice here */
19 #include <linux/types.h>
20 
21 #ifndef CONFIG_SF_DEFAULT_SPEED
22 # define CONFIG_SF_DEFAULT_SPEED	1000000
23 #endif
24 #ifndef CONFIG_SF_DEFAULT_MODE
25 # define CONFIG_SF_DEFAULT_MODE		SPI_MODE_3
26 #endif
27 #ifndef CONFIG_SF_DEFAULT_CS
28 # define CONFIG_SF_DEFAULT_CS		0
29 #endif
30 #ifndef CONFIG_SF_DEFAULT_BUS
31 # define CONFIG_SF_DEFAULT_BUS		0
32 #endif
33 
34 struct spi_slave;
35 
36 /**
37  * struct spi_flash - SPI flash structure
38  *
39  * @spi:		SPI slave
40  * @name:		Name of SPI flash
41  * @dual_flash:	Indicates dual flash memories - dual stacked, parallel
42  * @shift:		Flash shift useful in dual parallel
43  * @size:		Total flash size
44  * @page_size:		Write (page) size
45  * @sector_size:	Sector size
46  * @erase_size:	Erase size
47  * @bank_read_cmd:	Bank read cmd
48  * @bank_write_cmd:	Bank write cmd
49  * @bank_curr:		Current flash bank
50  * @poll_cmd:		Poll cmd - for flash erase/program
51  * @erase_cmd:		Erase cmd 4K, 32K, 64K
52  * @read_cmd:		Read cmd - Array Fast, Extn read and quad read.
53  * @write_cmd:		Write cmd - page and quad program.
54  * @dummy_byte:	Dummy cycles for read operation.
55  * @memory_map:	Address of read-only SPI flash access
56  * @read:		Flash read ops: Read len bytes at offset into buf
57  *			Supported cmds: Fast Array Read
58  * @write:		Flash write ops: Write len bytes from buf into offset
59  *			Supported cmds: Page Program
60  * @erase:		Flash erase ops: Erase len bytes from offset
61  *			Supported cmds: Sector erase 4K, 32K, 64K
62  * return 0 - Success, 1 - Failure
63  */
64 struct spi_flash {
65 	struct spi_slave *spi;
66 #ifdef CONFIG_DM_SPI_FLASH
67 	struct udevice *dev;
68 #endif
69 	const char *name;
70 	u8 dual_flash;
71 	u8 shift;
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 poll_cmd;
83 	u8 erase_cmd;
84 	u8 read_cmd;
85 	u8 write_cmd;
86 	u8 dummy_byte;
87 
88 	void *memory_map;
89 #ifndef CONFIG_DM_SPI_FLASH
90 	/*
91 	 * These are not strictly needed for driver model, but keep them here
92 	 * while the transition is in progress.
93 	 *
94 	 * Normally each driver would provide its own operations, but for
95 	 * SPI flash most chips use the same algorithms. One approach is
96 	 * to create a 'common' SPI flash device which knows how to talk
97 	 * to most devices, and then allow other drivers to be used instead
98 	 * if required, perhaps with a way of scanning through the list to
99 	 * find the driver that matches the device.
100 	 */
101 	int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);
102 	int (*write)(struct spi_flash *flash, u32 offset, size_t len,
103 			const void *buf);
104 	int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
105 #endif
106 };
107 
108 struct dm_spi_flash_ops {
109 	int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
110 	int (*write)(struct udevice *dev, u32 offset, size_t len,
111 		     const void *buf);
112 	int (*erase)(struct udevice *dev, u32 offset, size_t len);
113 };
114 
115 /* Access the serial operations for a device */
116 #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
117 
118 #ifdef CONFIG_DM_SPI_FLASH
119 /**
120  * spi_flash_read_dm() - Read data from SPI flash
121  *
122  * @dev:	SPI flash device
123  * @offset:	Offset into device in bytes to read from
124  * @len:	Number of bytes to read
125  * @buf:	Buffer to put the data that is read
126  * @return 0 if OK, -ve on error
127  */
128 int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
129 
130 /**
131  * spi_flash_write_dm() - Write data to SPI flash
132  *
133  * @dev:	SPI flash device
134  * @offset:	Offset into device in bytes to write to
135  * @len:	Number of bytes to write
136  * @buf:	Buffer containing bytes to write
137  * @return 0 if OK, -ve on error
138  */
139 int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
140 		       const void *buf);
141 
142 /**
143  * spi_flash_erase_dm() - Erase blocks of the SPI flash
144  *
145  * Note that @len must be a muiltiple of the flash sector size.
146  *
147  * @dev:	SPI flash device
148  * @offset:	Offset into device in bytes to start erasing
149  * @len:	Number of bytes to erase
150  * @return 0 if OK, -ve on error
151  */
152 int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
153 
154 int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
155 			   unsigned int max_hz, unsigned int spi_mode,
156 			   struct udevice **devp);
157 
158 /* Compatibility function - this is the old U-Boot API */
159 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
160 				  unsigned int max_hz, unsigned int spi_mode);
161 
162 /* Compatibility function - this is the old U-Boot API */
163 void spi_flash_free(struct spi_flash *flash);
164 
165 int spi_flash_remove(struct udevice *flash);
166 
167 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
168 				 size_t len, void *buf)
169 {
170 	return spi_flash_read_dm(flash->dev, offset, len, buf);
171 }
172 
173 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
174 				  size_t len, const void *buf)
175 {
176 	return spi_flash_write_dm(flash->dev, offset, len, buf);
177 }
178 
179 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
180 				  size_t len)
181 {
182 	return spi_flash_erase_dm(flash->dev, offset, len);
183 }
184 
185 struct sandbox_state;
186 
187 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
188 			 struct udevice *bus, int of_offset, const char *spec);
189 
190 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
191 
192 #else
193 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
194 		unsigned int max_hz, unsigned int spi_mode);
195 
196 /**
197  * Set up a new SPI flash from an fdt node
198  *
199  * @param blob		Device tree blob
200  * @param slave_node	Pointer to this SPI slave node in the device tree
201  * @param spi_node	Cached pointer to the SPI interface this node belongs
202  *			to
203  * @return 0 if ok, -1 on error
204  */
205 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
206 				      int spi_node);
207 
208 void spi_flash_free(struct spi_flash *flash);
209 
210 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
211 		size_t len, void *buf)
212 {
213 	return flash->read(flash, offset, len, buf);
214 }
215 
216 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
217 		size_t len, const void *buf)
218 {
219 	return flash->write(flash, offset, len, buf);
220 }
221 
222 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
223 		size_t len)
224 {
225 	return flash->erase(flash, offset, len);
226 }
227 #endif
228 
229 void spi_boot(void) __noreturn;
230 void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst);
231 
232 #endif /* _SPI_FLASH_H_ */
233