blob: 8bd44596747924c2c2433971774f1f95216b2478 [file] [log] [blame]
Boris Brezillond13f5b22018-08-16 17:30:11 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright (C) 2018 Exceet Electronics GmbH
4 * Copyright (C) 2018 Bootlin
5 *
6 * Author:
7 * Peter Pan <peterpandong@micron.com>
8 * Boris Brezillon <boris.brezillon@bootlin.com>
9 */
10
11#ifndef __UBOOT_SPI_MEM_H
12#define __UBOOT_SPI_MEM_H
13
Simon Glass340fd102020-07-19 10:15:34 -060014struct udevice;
Boris Brezillond13f5b22018-08-16 17:30:11 +020015
16#define SPI_MEM_OP_CMD(__opcode, __buswidth) \
17 { \
18 .buswidth = __buswidth, \
19 .opcode = __opcode, \
20 }
21
22#define SPI_MEM_OP_ADDR(__nbytes, __val, __buswidth) \
23 { \
24 .nbytes = __nbytes, \
25 .val = __val, \
26 .buswidth = __buswidth, \
27 }
28
29#define SPI_MEM_OP_NO_ADDR { }
30
31#define SPI_MEM_OP_DUMMY(__nbytes, __buswidth) \
32 { \
33 .nbytes = __nbytes, \
34 .buswidth = __buswidth, \
35 }
36
37#define SPI_MEM_OP_NO_DUMMY { }
38
39#define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth) \
40 { \
41 .dir = SPI_MEM_DATA_IN, \
42 .nbytes = __nbytes, \
43 .buf.in = __buf, \
44 .buswidth = __buswidth, \
45 }
46
47#define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, __buswidth) \
48 { \
49 .dir = SPI_MEM_DATA_OUT, \
50 .nbytes = __nbytes, \
51 .buf.out = __buf, \
52 .buswidth = __buswidth, \
53 }
54
55#define SPI_MEM_OP_NO_DATA { }
56
57/**
58 * enum spi_mem_data_dir - describes the direction of a SPI memory data
59 * transfer from the controller perspective
Tudor Ambarus790c1692020-03-20 09:35:31 +000060 * @SPI_MEM_NO_DATA: no data transferred
Boris Brezillond13f5b22018-08-16 17:30:11 +020061 * @SPI_MEM_DATA_IN: data coming from the SPI memory
62 * @SPI_MEM_DATA_OUT: data sent the SPI memory
63 */
64enum spi_mem_data_dir {
Tudor Ambarus790c1692020-03-20 09:35:31 +000065 SPI_MEM_NO_DATA,
Boris Brezillond13f5b22018-08-16 17:30:11 +020066 SPI_MEM_DATA_IN,
67 SPI_MEM_DATA_OUT,
68};
69
70/**
71 * struct spi_mem_op - describes a SPI memory operation
72 * @cmd.buswidth: number of IO lines used to transmit the command
73 * @cmd.opcode: operation opcode
Pratyush Yadava1eb40b2021-06-26 00:47:03 +053074 * @cmd.dtr: whether the command opcode should be sent in DTR mode or not
Boris Brezillond13f5b22018-08-16 17:30:11 +020075 * @addr.nbytes: number of address bytes to send. Can be zero if the operation
76 * does not need to send an address
77 * @addr.buswidth: number of IO lines used to transmit the address cycles
78 * @addr.val: address value. This value is always sent MSB first on the bus.
79 * Note that only @addr.nbytes are taken into account in this
80 * address value, so users should make sure the value fits in the
81 * assigned number of bytes.
Pratyush Yadava1eb40b2021-06-26 00:47:03 +053082 * @addr.dtr: whether the address should be sent in DTR mode or not
Boris Brezillond13f5b22018-08-16 17:30:11 +020083 * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can
84 * be zero if the operation does not require dummy bytes
85 * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes
Pratyush Yadava1eb40b2021-06-26 00:47:03 +053086 * @dummy.dtr: whether the dummy bytes should be sent in DTR mode or not
Boris Brezillond13f5b22018-08-16 17:30:11 +020087 * @data.buswidth: number of IO lanes used to send/receive the data
Pratyush Yadava1eb40b2021-06-26 00:47:03 +053088 * @data.dtr: whether the data should be sent in DTR mode or not
Boris Brezillond13f5b22018-08-16 17:30:11 +020089 * @data.dir: direction of the transfer
90 * @data.buf.in: input buffer
91 * @data.buf.out: output buffer
92 */
93struct spi_mem_op {
94 struct {
95 u8 buswidth;
96 u8 opcode;
Pratyush Yadava1eb40b2021-06-26 00:47:03 +053097 u8 dtr : 1;
Boris Brezillond13f5b22018-08-16 17:30:11 +020098 } cmd;
99
100 struct {
101 u8 nbytes;
102 u8 buswidth;
Pratyush Yadava1eb40b2021-06-26 00:47:03 +0530103 u8 dtr : 1;
Boris Brezillond13f5b22018-08-16 17:30:11 +0200104 u64 val;
105 } addr;
106
107 struct {
108 u8 nbytes;
109 u8 buswidth;
Pratyush Yadava1eb40b2021-06-26 00:47:03 +0530110 u8 dtr : 1;
Boris Brezillond13f5b22018-08-16 17:30:11 +0200111 } dummy;
112
113 struct {
114 u8 buswidth;
Pratyush Yadava1eb40b2021-06-26 00:47:03 +0530115 u8 dtr : 1;
Boris Brezillond13f5b22018-08-16 17:30:11 +0200116 enum spi_mem_data_dir dir;
117 unsigned int nbytes;
118 /* buf.{in,out} must be DMA-able. */
119 union {
120 void *in;
121 const void *out;
122 } buf;
123 } data;
124};
125
126#define SPI_MEM_OP(__cmd, __addr, __dummy, __data) \
127 { \
128 .cmd = __cmd, \
129 .addr = __addr, \
130 .dummy = __dummy, \
131 .data = __data, \
132 }
133
134#ifndef __UBOOT__
135/**
136 * struct spi_mem - describes a SPI memory device
137 * @spi: the underlying SPI device
138 * @drvpriv: spi_mem_driver private data
139 *
140 * Extra information that describe the SPI memory device and may be needed by
141 * the controller to properly handle this device should be placed here.
142 *
143 * One example would be the device size since some controller expose their SPI
144 * mem devices through a io-mapped region.
145 */
146struct spi_mem {
147 struct udevice *dev;
148 void *drvpriv;
149};
150
151/**
152 * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
153 * device
154 * @mem: memory device
155 * @data: data to attach to the memory device
156 */
157static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
158{
159 mem->drvpriv = data;
160}
161
162/**
163 * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
164 * device
165 * @mem: memory device
166 *
167 * Return: the data attached to the mem device.
168 */
169static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
170{
171 return mem->drvpriv;
172}
173#endif /* __UBOOT__ */
174
175/**
176 * struct spi_controller_mem_ops - SPI memory operations
177 * @adjust_op_size: shrink the data xfer of an operation to match controller's
178 * limitations (can be alignment of max RX/TX size
179 * limitations)
180 * @supports_op: check if an operation is supported by the controller
181 * @exec_op: execute a SPI memory operation
182 *
183 * This interface should be implemented by SPI controllers providing an
184 * high-level interface to execute SPI memory operation, which is usually the
185 * case for QSPI controllers.
186 */
187struct spi_controller_mem_ops {
188 int (*adjust_op_size)(struct spi_slave *slave, struct spi_mem_op *op);
189 bool (*supports_op)(struct spi_slave *slave,
190 const struct spi_mem_op *op);
191 int (*exec_op)(struct spi_slave *slave,
192 const struct spi_mem_op *op);
193};
194
195#ifndef __UBOOT__
196/**
197 * struct spi_mem_driver - SPI memory driver
198 * @spidrv: inherit from a SPI driver
199 * @probe: probe a SPI memory. Usually where detection/initialization takes
200 * place
201 * @remove: remove a SPI memory
202 * @shutdown: take appropriate action when the system is shutdown
203 *
204 * This is just a thin wrapper around a spi_driver. The core takes care of
205 * allocating the spi_mem object and forwarding the probe/remove/shutdown
206 * request to the spi_mem_driver. The reason we use this wrapper is because
207 * we might have to stuff more information into the spi_mem struct to let
208 * SPI controllers know more about the SPI memory they interact with, and
209 * having this intermediate layer allows us to do that without adding more
210 * useless fields to the spi_device object.
211 */
212struct spi_mem_driver {
213 struct spi_driver spidrv;
214 int (*probe)(struct spi_mem *mem);
215 int (*remove)(struct spi_mem *mem);
216 void (*shutdown)(struct spi_mem *mem);
217};
218
219#if IS_ENABLED(CONFIG_SPI_MEM)
220int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
221 const struct spi_mem_op *op,
222 struct sg_table *sg);
223
224void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
225 const struct spi_mem_op *op,
226 struct sg_table *sg);
227#else
228static inline int
229spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
230 const struct spi_mem_op *op,
231 struct sg_table *sg)
232{
Simon Glass24e3d5d2021-03-25 10:26:06 +1300233 return -ENOSYS;
Boris Brezillond13f5b22018-08-16 17:30:11 +0200234}
235
236static inline void
237spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
238 const struct spi_mem_op *op,
239 struct sg_table *sg)
240{
241}
242#endif /* CONFIG_SPI_MEM */
243#endif /* __UBOOT__ */
244
245int spi_mem_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op);
246
247bool spi_mem_supports_op(struct spi_slave *slave, const struct spi_mem_op *op);
248
249int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op);
250
Mathew McBrideaf6266c2021-01-25 03:55:20 +0000251bool spi_mem_default_supports_op(struct spi_slave *mem,
252 const struct spi_mem_op *op);
253
Boris Brezillond13f5b22018-08-16 17:30:11 +0200254#ifndef __UBOOT__
255int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
256 struct module *owner);
257
258void spi_mem_driver_unregister(struct spi_mem_driver *drv);
259
260#define spi_mem_driver_register(__drv) \
261 spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
262
263#define module_spi_mem_driver(__drv) \
264 module_driver(__drv, spi_mem_driver_register, \
265 spi_mem_driver_unregister)
266#endif
267
268#endif /* __LINUX_SPI_MEM_H */