1
2
3
4
5
6
7
8
9
10
11#ifndef __UBOOT_SPI_MEM_H
12#define __UBOOT_SPI_MEM_H
13
14#include <common.h>
15#include <dm.h>
16#include <errno.h>
17#include <spi.h>
18
19#define SPI_MEM_OP_CMD(__opcode, __buswidth) \
20 { \
21 .buswidth = __buswidth, \
22 .opcode = __opcode, \
23 }
24
25#define SPI_MEM_OP_ADDR(__nbytes, __val, __buswidth) \
26 { \
27 .nbytes = __nbytes, \
28 .val = __val, \
29 .buswidth = __buswidth, \
30 }
31
32#define SPI_MEM_OP_NO_ADDR { }
33
34#define SPI_MEM_OP_DUMMY(__nbytes, __buswidth) \
35 { \
36 .nbytes = __nbytes, \
37 .buswidth = __buswidth, \
38 }
39
40#define SPI_MEM_OP_NO_DUMMY { }
41
42#define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth) \
43 { \
44 .dir = SPI_MEM_DATA_IN, \
45 .nbytes = __nbytes, \
46 .buf.in = __buf, \
47 .buswidth = __buswidth, \
48 }
49
50#define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, __buswidth) \
51 { \
52 .dir = SPI_MEM_DATA_OUT, \
53 .nbytes = __nbytes, \
54 .buf.out = __buf, \
55 .buswidth = __buswidth, \
56 }
57
58#define SPI_MEM_OP_NO_DATA { }
59
60
61
62
63
64
65
66enum spi_mem_data_dir {
67 SPI_MEM_DATA_IN,
68 SPI_MEM_DATA_OUT,
69};
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90struct spi_mem_op {
91 struct {
92 u8 buswidth;
93 u8 opcode;
94 } cmd;
95
96 struct {
97 u8 nbytes;
98 u8 buswidth;
99 u64 val;
100 } addr;
101
102 struct {
103 u8 nbytes;
104 u8 buswidth;
105 } dummy;
106
107 struct {
108 u8 buswidth;
109 enum spi_mem_data_dir dir;
110 unsigned int nbytes;
111
112 union {
113 void *in;
114 const void *out;
115 } buf;
116 } data;
117};
118
119#define SPI_MEM_OP(__cmd, __addr, __dummy, __data) \
120 { \
121 .cmd = __cmd, \
122 .addr = __addr, \
123 .dummy = __dummy, \
124 .data = __data, \
125 }
126
127#ifndef __UBOOT__
128
129
130
131
132
133
134
135
136
137
138
139struct spi_mem {
140 struct udevice *dev;
141 void *drvpriv;
142};
143
144
145
146
147
148
149
150static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
151{
152 mem->drvpriv = data;
153}
154
155
156
157
158
159
160
161
162static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
163{
164 return mem->drvpriv;
165}
166#endif
167
168
169
170
171
172
173
174
175
176
177
178
179
180struct spi_controller_mem_ops {
181 int (*adjust_op_size)(struct spi_slave *slave, struct spi_mem_op *op);
182 bool (*supports_op)(struct spi_slave *slave,
183 const struct spi_mem_op *op);
184 int (*exec_op)(struct spi_slave *slave,
185 const struct spi_mem_op *op);
186};
187
188#ifndef __UBOOT__
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205struct spi_mem_driver {
206 struct spi_driver spidrv;
207 int (*probe)(struct spi_mem *mem);
208 int (*remove)(struct spi_mem *mem);
209 void (*shutdown)(struct spi_mem *mem);
210};
211
212#if IS_ENABLED(CONFIG_SPI_MEM)
213int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
214 const struct spi_mem_op *op,
215 struct sg_table *sg);
216
217void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
218 const struct spi_mem_op *op,
219 struct sg_table *sg);
220#else
221static inline int
222spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
223 const struct spi_mem_op *op,
224 struct sg_table *sg)
225{
226 return -ENOTSUPP;
227}
228
229static inline void
230spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
231 const struct spi_mem_op *op,
232 struct sg_table *sg)
233{
234}
235#endif
236#endif
237
238int spi_mem_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op);
239
240bool spi_mem_supports_op(struct spi_slave *slave, const struct spi_mem_op *op);
241
242int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op);
243
244#ifndef __UBOOT__
245int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
246 struct module *owner);
247
248void spi_mem_driver_unregister(struct spi_mem_driver *drv);
249
250#define spi_mem_driver_register(__drv) \
251 spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
252
253#define module_spi_mem_driver(__drv) \
254 module_driver(__drv, spi_mem_driver_register, \
255 spi_mem_driver_unregister)
256#endif
257
258#endif
259