1
2
3
4
5
6
7
8
9
10#ifndef _SPI_FLASH_H_
11#define _SPI_FLASH_H_
12
13#include <dm.h>
14#include <linux/types.h>
15
16#define SF_DUALIO_FLASH 1
17
18#ifndef CONFIG_SF_DEFAULT_SPEED
19# define CONFIG_SF_DEFAULT_SPEED 1000000
20#endif
21#ifndef CONFIG_SF_DEFAULT_MODE
22# define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
23#endif
24#ifndef CONFIG_SF_DEFAULT_CS
25# define CONFIG_SF_DEFAULT_CS 0
26#endif
27#ifndef CONFIG_SF_DEFAULT_BUS
28# define CONFIG_SF_DEFAULT_BUS 0
29#endif
30
31struct spi_slave;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65struct spi_flash {
66 struct spi_slave *spi;
67#ifdef CONFIG_DM_SPI_FLASH
68 struct udevice *dev;
69#endif
70 const char *name;
71 u8 dual_flash;
72 u8 shift;
73 u16 flags;
74
75 u32 size;
76 u32 page_size;
77 u32 sector_size;
78 u32 erase_size;
79#ifdef CONFIG_SPI_FLASH_BAR
80 u8 bank_read_cmd;
81 u8 bank_write_cmd;
82 u8 bank_curr;
83 u8 upage_prev;
84#endif
85 u8 erase_cmd;
86 u8 read_cmd;
87 u8 write_cmd;
88 u8 dummy_byte;
89
90 void *memory_map;
91
92 int (*flash_lock)(struct spi_flash *flash, u32 ofs, size_t len);
93 int (*flash_unlock)(struct spi_flash *flash, u32 ofs, size_t len);
94 int (*flash_is_locked)(struct spi_flash *flash, u32 ofs, size_t len);
95#ifndef CONFIG_DM_SPI_FLASH
96
97
98
99
100
101
102
103
104
105
106
107 int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);
108 int (*write)(struct spi_flash *flash, u32 offset, size_t len,
109 const void *buf);
110 int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
111#endif
112};
113
114struct dm_spi_flash_ops {
115 int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
116 int (*write)(struct udevice *dev, u32 offset, size_t len,
117 const void *buf);
118 int (*erase)(struct udevice *dev, u32 offset, size_t len);
119};
120
121
122#define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
123
124#ifdef CONFIG_DM_SPI_FLASH
125
126
127
128
129
130
131
132
133
134int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
135
136
137
138
139
140
141
142
143
144
145int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
146 const void *buf);
147
148
149
150
151
152
153
154
155
156
157
158int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
159
160int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
161 unsigned int max_hz, unsigned int spi_mode,
162 struct udevice **devp);
163
164
165struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
166 unsigned int max_hz, unsigned int spi_mode);
167
168
169void spi_flash_free(struct spi_flash *flash);
170
171static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
172 size_t len, void *buf)
173{
174 return spi_flash_read_dm(flash->dev, offset, len, buf);
175}
176
177static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
178 size_t len, const void *buf)
179{
180 return spi_flash_write_dm(flash->dev, offset, len, buf);
181}
182
183static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
184 size_t len)
185{
186 return spi_flash_erase_dm(flash->dev, offset, len);
187}
188
189struct sandbox_state;
190
191int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
192 struct udevice *bus, int of_offset, const char *spec);
193
194void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
195
196#else
197struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
198 unsigned int max_hz, unsigned int spi_mode);
199
200
201
202
203
204
205
206
207
208
209struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
210 int spi_node);
211
212void spi_flash_free(struct spi_flash *flash);
213
214static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
215 size_t len, void *buf)
216{
217 return flash->read(flash, offset, len, buf);
218}
219
220static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
221 size_t len, const void *buf)
222{
223 return flash->write(flash, offset, len, buf);
224}
225
226static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
227 size_t len)
228{
229 return flash->erase(flash, offset, len);
230}
231#endif
232
233static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
234 bool prot)
235{
236 if (!flash->flash_lock || !flash->flash_unlock)
237 return -EOPNOTSUPP;
238
239 if (prot)
240 return flash->flash_lock(flash, ofs, len);
241 else
242 return flash->flash_unlock(flash, ofs, len);
243}
244
245#endif
246