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#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
29struct spi_slave;
30
31
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
63struct 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
95
96
97
98
99
100
101
102
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
111struct 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
118
119#define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
120
121#ifdef CONFIG_DM_SPI_FLASH
122
123
124
125
126
127
128
129
130
131int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
132
133
134
135
136
137
138
139
140
141
142int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
143 const void *buf);
144
145
146
147
148
149
150
151
152
153
154
155int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
156
157int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
158 unsigned int max_hz, unsigned int spi_mode,
159 struct udevice **devp);
160
161
162struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
163 unsigned int max_hz, unsigned int spi_mode);
164
165
166void spi_flash_free(struct spi_flash *flash);
167
168static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
169 size_t len, void *buf)
170{
171 return spi_flash_read_dm(flash->dev, offset, len, buf);
172}
173
174static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
175 size_t len, const void *buf)
176{
177 return spi_flash_write_dm(flash->dev, offset, len, buf);
178}
179
180static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
181 size_t len)
182{
183 return spi_flash_erase_dm(flash->dev, offset, len);
184}
185
186struct sandbox_state;
187
188int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
189 struct udevice *bus, int of_offset, const char *spec);
190
191void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
192
193#else
194struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
195 unsigned int max_hz, unsigned int spi_mode);
196
197
198
199
200
201
202
203
204
205
206struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
207 int spi_node);
208
209void spi_flash_free(struct spi_flash *flash);
210
211static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
212 size_t len, void *buf)
213{
214 return flash->read(flash, offset, len, buf);
215}
216
217static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
218 size_t len, const void *buf)
219{
220 return flash->write(flash, offset, len, buf);
221}
222
223static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
224 size_t len)
225{
226 return flash->erase(flash, offset, len);
227}
228#endif
229
230static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
231 bool prot)
232{
233 if (!flash->flash_lock || !flash->flash_unlock)
234 return -EOPNOTSUPP;
235
236 if (prot)
237 return flash->flash_lock(flash, ofs, len);
238 else
239 return flash->flash_unlock(flash, ofs, len);
240}
241
242#endif
243