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.ecc: whether error correction is required or not
93 * @data.swap16: whether the byte order of 16-bit words is swapped when read
94 * or written in Octal DTR mode compared to STR mode.
95 * @data.dir: direction of the transfer
96 * @data.nbytes: number of data bytes to send/receive. Can be zero if the
97 * operation does not involve transferring data
98 * @data.buf.in: input buffer (must be DMA-able)
99 * @data.buf.out: output buffer (must be DMA-able)
131 enum spi_mem_data_dir dir;
140 #define SPI_MEM_OP(__cmd, __addr, __dummy, __data) \
149 * struct spi_mem_dirmap_info - Direct mapping information
150 * @op_tmpl: operation template that should be used by the direct mapping when
151 * the memory device is accessed
152 * @offset: absolute offset this direct mapping is pointing to
153 * @length: length in byte of this direct mapping
155 * These information are used by the controller specific implementation to know
156 * the portion of memory that is directly mapped and the spi_mem_op that should
157 * be used to access the device.
158 * A direct mapping is only valid for one direction (read or write) and this
159 * direction is directly encoded in the ->op_tmpl.data.dir field.
161 struct spi_mem_dirmap_info {
162 struct spi_mem_op op_tmpl;
168 * struct spi_mem_dirmap_desc - Direct mapping descriptor
169 * @mem: the SPI memory device this direct mapping is attached to
170 * @info: information passed at direct mapping creation time
171 * @nodirmap: set to 1 if the SPI controller does not implement
172 * ->mem_ops->dirmap_create() or when this function returned an
173 * error. If @nodirmap is true, all spi_mem_dirmap_{read,write}()
174 * calls will use spi_mem_exec_op() to access the memory. This is a
175 * degraded mode that allows spi_mem drivers to use the same code
176 * no matter whether the controller supports direct mapping or not
177 * @priv: field pointing to controller specific data
179 * Common part of a direct mapping descriptor. This object is created by
180 * spi_mem_dirmap_create() and controller implementation of ->create_dirmap()
181 * can create/attach direct mapping resources to the descriptor in the ->priv
184 struct spi_mem_dirmap_desc {
186 struct spi_mem_dirmap_info info;
187 unsigned int nodirmap;
192 * struct spi_mem - describes a SPI memory device
193 * @spi: the underlying SPI device
194 * @drvpriv: spi_mem_driver private data
195 * @name: name of the SPI memory device
197 * Extra information that describe the SPI memory device and may be needed by
198 * the controller to properly handle this device should be placed here.
200 * One example would be the device size since some controller expose their SPI
201 * mem devices through a io-mapped region.
204 struct spi_device *spi;
210 * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
212 * @mem: memory device
213 * @data: data to attach to the memory device
215 static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
221 * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
223 * @mem: memory device
225 * Return: the data attached to the mem device.
227 static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
233 * struct spi_controller_mem_ops - SPI memory operations
234 * @adjust_op_size: shrink the data xfer of an operation to match controller's
235 * limitations (can be alignment or max RX/TX size
237 * @supports_op: check if an operation is supported by the controller
238 * @exec_op: execute a SPI memory operation
239 * not all driver provides supports_op(), so it can return -EOPNOTSUPP
240 * if the op is not supported by the driver/controller
241 * @get_name: get a custom name for the SPI mem device from the controller.
242 * This might be needed if the controller driver has been ported
243 * to use the SPI mem layer and a custom name is used to keep
244 * mtdparts compatible.
245 * Note that if the implementation of this function allocates memory
246 * dynamically, then it should do so with devm_xxx(), as we don't
247 * have a ->free_name() function.
248 * @dirmap_create: create a direct mapping descriptor that can later be used to
249 * access the memory device. This method is optional
250 * @dirmap_destroy: destroy a memory descriptor previous created by
252 * @dirmap_read: read data from the memory device using the direct mapping
253 * created by ->dirmap_create(). The function can return less
254 * data than requested (for example when the request is crossing
255 * the currently mapped area), and the caller of
256 * spi_mem_dirmap_read() is responsible for calling it again in
258 * @dirmap_write: write data to the memory device using the direct mapping
259 * created by ->dirmap_create(). The function can return less
260 * data than requested (for example when the request is crossing
261 * the currently mapped area), and the caller of
262 * spi_mem_dirmap_write() is responsible for calling it again in
264 * @poll_status: poll memory device status until (status & mask) == match or
265 * when the timeout has expired. It fills the data buffer with
266 * the last status value.
268 * This interface should be implemented by SPI controllers providing an
269 * high-level interface to execute SPI memory operation, which is usually the
270 * case for QSPI controllers.
272 * Note on ->dirmap_{read,write}(): drivers should avoid accessing the direct
273 * mapping from the CPU because doing that can stall the CPU waiting for the
274 * SPI mem transaction to finish, and this will make real-time maintainers
275 * unhappy and might make your system less reactive. Instead, drivers should
276 * use DMA to access this direct mapping.
278 struct spi_controller_mem_ops {
279 int (*adjust_op_size)(struct spi_mem *mem, struct spi_mem_op *op);
280 bool (*supports_op)(struct spi_mem *mem,
281 const struct spi_mem_op *op);
282 int (*exec_op)(struct spi_mem *mem,
283 const struct spi_mem_op *op);
284 const char *(*get_name)(struct spi_mem *mem);
285 int (*dirmap_create)(struct spi_mem_dirmap_desc *desc);
286 void (*dirmap_destroy)(struct spi_mem_dirmap_desc *desc);
287 ssize_t (*dirmap_read)(struct spi_mem_dirmap_desc *desc,
288 u64 offs, size_t len, void *buf);
289 ssize_t (*dirmap_write)(struct spi_mem_dirmap_desc *desc,
290 u64 offs, size_t len, const void *buf);
291 int (*poll_status)(struct spi_mem *mem,
292 const struct spi_mem_op *op,
294 unsigned long initial_delay_us,
295 unsigned long polling_rate_us,
296 unsigned long timeout_ms);
300 * struct spi_controller_mem_caps - SPI memory controller capabilities
301 * @dtr: Supports DTR operations
302 * @ecc: Supports operations with error correction
303 * @swap16: Supports swapping bytes on a 16 bit boundary when configured in
306 struct spi_controller_mem_caps {
312 #define spi_mem_controller_is_capable(ctlr, cap) \
313 ((ctlr)->mem_caps && (ctlr)->mem_caps->cap)
316 * struct spi_mem_driver - SPI memory driver
317 * @spidrv: inherit from a SPI driver
318 * @probe: probe a SPI memory. Usually where detection/initialization takes
320 * @remove: remove a SPI memory
321 * @shutdown: take appropriate action when the system is shutdown
323 * This is just a thin wrapper around a spi_driver. The core takes care of
324 * allocating the spi_mem object and forwarding the probe/remove/shutdown
325 * request to the spi_mem_driver. The reason we use this wrapper is because
326 * we might have to stuff more information into the spi_mem struct to let
327 * SPI controllers know more about the SPI memory they interact with, and
328 * having this intermediate layer allows us to do that without adding more
329 * useless fields to the spi_device object.
331 struct spi_mem_driver {
332 struct spi_driver spidrv;
333 int (*probe)(struct spi_mem *mem);
334 int (*remove)(struct spi_mem *mem);
335 void (*shutdown)(struct spi_mem *mem);
338 #if IS_ENABLED(CONFIG_SPI_MEM)
339 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
340 const struct spi_mem_op *op,
341 struct sg_table *sg);
343 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
344 const struct spi_mem_op *op,
345 struct sg_table *sg);
347 bool spi_mem_default_supports_op(struct spi_mem *mem,
348 const struct spi_mem_op *op);
351 spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
352 const struct spi_mem_op *op,
359 spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
360 const struct spi_mem_op *op,
366 bool spi_mem_default_supports_op(struct spi_mem *mem,
367 const struct spi_mem_op *op)
371 #endif /* CONFIG_SPI_MEM */
373 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op);
375 bool spi_mem_supports_op(struct spi_mem *mem,
376 const struct spi_mem_op *op);
378 int spi_mem_exec_op(struct spi_mem *mem,
379 const struct spi_mem_op *op);
381 const char *spi_mem_get_name(struct spi_mem *mem);
383 struct spi_mem_dirmap_desc *
384 spi_mem_dirmap_create(struct spi_mem *mem,
385 const struct spi_mem_dirmap_info *info);
386 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc);
387 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
388 u64 offs, size_t len, void *buf);
389 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
390 u64 offs, size_t len, const void *buf);
391 struct spi_mem_dirmap_desc *
392 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem,
393 const struct spi_mem_dirmap_info *info);
394 void devm_spi_mem_dirmap_destroy(struct device *dev,
395 struct spi_mem_dirmap_desc *desc);
397 int spi_mem_poll_status(struct spi_mem *mem,
398 const struct spi_mem_op *op,
400 unsigned long initial_delay_us,
401 unsigned long polling_delay_us,
404 int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
405 struct module *owner);
407 void spi_mem_driver_unregister(struct spi_mem_driver *drv);
409 #define spi_mem_driver_register(__drv) \
410 spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
412 #define module_spi_mem_driver(__drv) \
413 module_driver(__drv, spi_mem_driver_register, \
414 spi_mem_driver_unregister)
416 #endif /* __LINUX_SPI_MEM_H */