1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2018 Exceet Electronics GmbH
4 * Copyright (C) 2018 Bootlin
11 #ifndef __LINUX_SPI_MEM_H
12 #define __LINUX_SPI_MEM_H
14 #include <linux/spi/spi.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 to 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.dtr: whether the address should be sent in DTR mode or not
82 * @addr.val: address value. This value is always sent MSB first on the bus.
83 * Note that only @addr.nbytes are taken into account in this
84 * address value, so users should make sure the value fits in the
85 * assigned number of bytes.
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.nbytes: number of data bytes to send/receive. Can be zero if the
94 * operation does not involve transferring data
95 * @data.buf.in: input buffer (must be DMA-able)
96 * @data.buf.out: output buffer (must be DMA-able)
122 enum spi_mem_data_dir dir;
131 #define SPI_MEM_OP(__cmd, __addr, __dummy, __data) \
140 * struct spi_mem_dirmap_info - Direct mapping information
141 * @op_tmpl: operation template that should be used by the direct mapping when
142 * the memory device is accessed
143 * @offset: absolute offset this direct mapping is pointing to
144 * @length: length in byte of this direct mapping
146 * These information are used by the controller specific implementation to know
147 * the portion of memory that is directly mapped and the spi_mem_op that should
148 * be used to access the device.
149 * A direct mapping is only valid for one direction (read or write) and this
150 * direction is directly encoded in the ->op_tmpl.data.dir field.
152 struct spi_mem_dirmap_info {
153 struct spi_mem_op op_tmpl;
159 * struct spi_mem_dirmap_desc - Direct mapping descriptor
160 * @mem: the SPI memory device this direct mapping is attached to
161 * @info: information passed at direct mapping creation time
162 * @nodirmap: set to 1 if the SPI controller does not implement
163 * ->mem_ops->dirmap_create() or when this function returned an
164 * error. If @nodirmap is true, all spi_mem_dirmap_{read,write}()
165 * calls will use spi_mem_exec_op() to access the memory. This is a
166 * degraded mode that allows spi_mem drivers to use the same code
167 * no matter whether the controller supports direct mapping or not
168 * @priv: field pointing to controller specific data
170 * Common part of a direct mapping descriptor. This object is created by
171 * spi_mem_dirmap_create() and controller implementation of ->create_dirmap()
172 * can create/attach direct mapping resources to the descriptor in the ->priv
175 struct spi_mem_dirmap_desc {
177 struct spi_mem_dirmap_info info;
178 unsigned int nodirmap;
183 * struct spi_mem - describes a SPI memory device
184 * @spi: the underlying SPI device
185 * @drvpriv: spi_mem_driver private data
186 * @name: name of the SPI memory device
188 * Extra information that describe the SPI memory device and may be needed by
189 * the controller to properly handle this device should be placed here.
191 * One example would be the device size since some controller expose their SPI
192 * mem devices through a io-mapped region.
195 struct spi_device *spi;
201 * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
203 * @mem: memory device
204 * @data: data to attach to the memory device
206 static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
212 * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
214 * @mem: memory device
216 * Return: the data attached to the mem device.
218 static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
224 * struct spi_controller_mem_ops - SPI memory operations
225 * @adjust_op_size: shrink the data xfer of an operation to match controller's
226 * limitations (can be alignment of max RX/TX size
228 * @supports_op: check if an operation is supported by the controller
229 * @exec_op: execute a SPI memory operation
230 * @get_name: get a custom name for the SPI mem device from the controller.
231 * This might be needed if the controller driver has been ported
232 * to use the SPI mem layer and a custom name is used to keep
233 * mtdparts compatible.
234 * Note that if the implementation of this function allocates memory
235 * dynamically, then it should do so with devm_xxx(), as we don't
236 * have a ->free_name() function.
237 * @dirmap_create: create a direct mapping descriptor that can later be used to
238 * access the memory device. This method is optional
239 * @dirmap_destroy: destroy a memory descriptor previous created by
241 * @dirmap_read: read data from the memory device using the direct mapping
242 * created by ->dirmap_create(). The function can return less
243 * data than requested (for example when the request is crossing
244 * the currently mapped area), and the caller of
245 * spi_mem_dirmap_read() is responsible for calling it again in
247 * @dirmap_write: write data to the memory device using the direct mapping
248 * created by ->dirmap_create(). The function can return less
249 * data than requested (for example when the request is crossing
250 * the currently mapped area), and the caller of
251 * spi_mem_dirmap_write() is responsible for calling it again in
254 * This interface should be implemented by SPI controllers providing an
255 * high-level interface to execute SPI memory operation, which is usually the
256 * case for QSPI controllers.
258 * Note on ->dirmap_{read,write}(): drivers should avoid accessing the direct
259 * mapping from the CPU because doing that can stall the CPU waiting for the
260 * SPI mem transaction to finish, and this will make real-time maintainers
261 * unhappy and might make your system less reactive. Instead, drivers should
262 * use DMA to access this direct mapping.
264 struct spi_controller_mem_ops {
265 int (*adjust_op_size)(struct spi_mem *mem, struct spi_mem_op *op);
266 bool (*supports_op)(struct spi_mem *mem,
267 const struct spi_mem_op *op);
268 int (*exec_op)(struct spi_mem *mem,
269 const struct spi_mem_op *op);
270 const char *(*get_name)(struct spi_mem *mem);
271 int (*dirmap_create)(struct spi_mem_dirmap_desc *desc);
272 void (*dirmap_destroy)(struct spi_mem_dirmap_desc *desc);
273 ssize_t (*dirmap_read)(struct spi_mem_dirmap_desc *desc,
274 u64 offs, size_t len, void *buf);
275 ssize_t (*dirmap_write)(struct spi_mem_dirmap_desc *desc,
276 u64 offs, size_t len, const void *buf);
280 * struct spi_mem_driver - SPI memory driver
281 * @spidrv: inherit from a SPI driver
282 * @probe: probe a SPI memory. Usually where detection/initialization takes
284 * @remove: remove a SPI memory
285 * @shutdown: take appropriate action when the system is shutdown
287 * This is just a thin wrapper around a spi_driver. The core takes care of
288 * allocating the spi_mem object and forwarding the probe/remove/shutdown
289 * request to the spi_mem_driver. The reason we use this wrapper is because
290 * we might have to stuff more information into the spi_mem struct to let
291 * SPI controllers know more about the SPI memory they interact with, and
292 * having this intermediate layer allows us to do that without adding more
293 * useless fields to the spi_device object.
295 struct spi_mem_driver {
296 struct spi_driver spidrv;
297 int (*probe)(struct spi_mem *mem);
298 int (*remove)(struct spi_mem *mem);
299 void (*shutdown)(struct spi_mem *mem);
302 #if IS_ENABLED(CONFIG_SPI_MEM)
303 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
304 const struct spi_mem_op *op,
305 struct sg_table *sg);
307 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
308 const struct spi_mem_op *op,
309 struct sg_table *sg);
311 bool spi_mem_default_supports_op(struct spi_mem *mem,
312 const struct spi_mem_op *op);
316 spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
317 const struct spi_mem_op *op,
324 spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
325 const struct spi_mem_op *op,
331 bool spi_mem_default_supports_op(struct spi_mem *mem,
332 const struct spi_mem_op *op)
337 #endif /* CONFIG_SPI_MEM */
339 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op);
341 bool spi_mem_supports_op(struct spi_mem *mem,
342 const struct spi_mem_op *op);
344 int spi_mem_exec_op(struct spi_mem *mem,
345 const struct spi_mem_op *op);
347 const char *spi_mem_get_name(struct spi_mem *mem);
349 struct spi_mem_dirmap_desc *
350 spi_mem_dirmap_create(struct spi_mem *mem,
351 const struct spi_mem_dirmap_info *info);
352 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc);
353 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
354 u64 offs, size_t len, void *buf);
355 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
356 u64 offs, size_t len, const void *buf);
357 struct spi_mem_dirmap_desc *
358 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem,
359 const struct spi_mem_dirmap_info *info);
360 void devm_spi_mem_dirmap_destroy(struct device *dev,
361 struct spi_mem_dirmap_desc *desc);
363 int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
364 struct module *owner);
366 void spi_mem_driver_unregister(struct spi_mem_driver *drv);
368 #define spi_mem_driver_register(__drv) \
369 spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
371 #define module_spi_mem_driver(__drv) \
372 module_driver(__drv, spi_mem_driver_register, \
373 spi_mem_driver_unregister)
375 #endif /* __LINUX_SPI_MEM_H */