1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2018 Exceet Electronics GmbH
4 * Copyright (C) 2018 Bootlin
7 * Peter Pan <peterpandong@micron.com>
8 * Boris Brezillon <boris.brezillon@bootlin.com>
11 #ifndef __UBOOT_SPI_MEM_H
12 #define __UBOOT_SPI_MEM_H
16 #define SPI_MEM_OP_CMD(__opcode, __buswidth) \
18 .buswidth = __buswidth, \
23 #define SPI_MEM_OP_ADDR(__nbytes, __val, __buswidth) \
27 .buswidth = __buswidth, \
30 #define SPI_MEM_OP_NO_ADDR { }
32 #define SPI_MEM_OP_DUMMY(__nbytes, __buswidth) \
35 .buswidth = __buswidth, \
38 #define SPI_MEM_OP_NO_DUMMY { }
40 #define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth) \
42 .dir = SPI_MEM_DATA_IN, \
45 .buswidth = __buswidth, \
48 #define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, __buswidth) \
50 .dir = SPI_MEM_DATA_OUT, \
53 .buswidth = __buswidth, \
56 #define SPI_MEM_OP_NO_DATA { }
59 * enum spi_mem_data_dir - describes the direction of a SPI memory data
60 * transfer from the controller perspective
61 * @SPI_MEM_NO_DATA: no data transferred
62 * @SPI_MEM_DATA_IN: data coming from the SPI memory
63 * @SPI_MEM_DATA_OUT: data sent the SPI memory
65 enum spi_mem_data_dir {
72 * struct spi_mem_op - describes a SPI memory operation
73 * @cmd.nbytes: number of opcode bytes (only 1 or 2 are valid). The opcode is
75 * @cmd.buswidth: number of IO lines used to transmit the command
76 * @cmd.opcode: operation opcode
77 * @cmd.dtr: whether the command opcode should be sent in DTR mode or not
78 * @addr.nbytes: number of address bytes to send. Can be zero if the operation
79 * does not need to send an address
80 * @addr.buswidth: number of IO lines used to transmit the address cycles
81 * @addr.val: address value. This value is always sent MSB first on the bus.
82 * Note that only @addr.nbytes are taken into account in this
83 * address value, so users should make sure the value fits in the
84 * assigned number of bytes.
85 * @addr.dtr: whether the address should be sent in DTR mode or not
86 * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can
87 * be zero if the operation does not require dummy bytes
88 * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes
89 * @dummy.dtr: whether the dummy bytes should be sent in DTR mode or not
90 * @data.buswidth: number of IO lanes used to send/receive the data
91 * @data.dtr: whether the data should be sent in DTR mode or not
92 * @data.dir: direction of the transfer
93 * @data.buf.in: input buffer
94 * @data.buf.out: output buffer
120 enum spi_mem_data_dir dir;
122 /* buf.{in,out} must be DMA-able. */
130 #define SPI_MEM_OP(__cmd, __addr, __dummy, __data) \
140 * struct spi_mem - describes a SPI memory device
141 * @spi: the underlying SPI device
142 * @drvpriv: spi_mem_driver private data
144 * Extra information that describe the SPI memory device and may be needed by
145 * the controller to properly handle this device should be placed here.
147 * One example would be the device size since some controller expose their SPI
148 * mem devices through a io-mapped region.
156 * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
158 * @mem: memory device
159 * @data: data to attach to the memory device
161 static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
167 * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
169 * @mem: memory device
171 * Return: the data attached to the mem device.
173 static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
177 #endif /* __UBOOT__ */
180 * struct spi_controller_mem_ops - SPI memory operations
181 * @adjust_op_size: shrink the data xfer of an operation to match controller's
182 * limitations (can be alignment of max RX/TX size
184 * @supports_op: check if an operation is supported by the controller
185 * @exec_op: execute a SPI memory operation
187 * This interface should be implemented by SPI controllers providing an
188 * high-level interface to execute SPI memory operation, which is usually the
189 * case for QSPI controllers.
191 struct spi_controller_mem_ops {
192 int (*adjust_op_size)(struct spi_slave *slave, struct spi_mem_op *op);
193 bool (*supports_op)(struct spi_slave *slave,
194 const struct spi_mem_op *op);
195 int (*exec_op)(struct spi_slave *slave,
196 const struct spi_mem_op *op);
201 * struct spi_mem_driver - SPI memory driver
202 * @spidrv: inherit from a SPI driver
203 * @probe: probe a SPI memory. Usually where detection/initialization takes
205 * @remove: remove a SPI memory
206 * @shutdown: take appropriate action when the system is shutdown
208 * This is just a thin wrapper around a spi_driver. The core takes care of
209 * allocating the spi_mem object and forwarding the probe/remove/shutdown
210 * request to the spi_mem_driver. The reason we use this wrapper is because
211 * we might have to stuff more information into the spi_mem struct to let
212 * SPI controllers know more about the SPI memory they interact with, and
213 * having this intermediate layer allows us to do that without adding more
214 * useless fields to the spi_device object.
216 struct spi_mem_driver {
217 struct spi_driver spidrv;
218 int (*probe)(struct spi_mem *mem);
219 int (*remove)(struct spi_mem *mem);
220 void (*shutdown)(struct spi_mem *mem);
223 #if IS_ENABLED(CONFIG_SPI_MEM)
224 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
225 const struct spi_mem_op *op,
226 struct sg_table *sg);
228 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
229 const struct spi_mem_op *op,
230 struct sg_table *sg);
233 spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
234 const struct spi_mem_op *op,
241 spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
242 const struct spi_mem_op *op,
246 #endif /* CONFIG_SPI_MEM */
247 #endif /* __UBOOT__ */
249 int spi_mem_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op);
251 bool spi_mem_supports_op(struct spi_slave *slave, const struct spi_mem_op *op);
252 bool spi_mem_dtr_supports_op(struct spi_slave *slave,
253 const struct spi_mem_op *op);
255 bool spi_mem_default_supports_op(struct spi_slave *slave,
256 const struct spi_mem_op *op);
258 int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op);
260 bool spi_mem_default_supports_op(struct spi_slave *mem,
261 const struct spi_mem_op *op);
264 int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
265 struct module *owner);
267 void spi_mem_driver_unregister(struct spi_mem_driver *drv);
269 #define spi_mem_driver_register(__drv) \
270 spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
272 #define module_spi_mem_driver(__drv) \
273 module_driver(__drv, spi_mem_driver_register, \
274 spi_mem_driver_unregister)
277 #endif /* __LINUX_SPI_MEM_H */