blob: 7195fbc234aa2781ccb9406075ddaca957b20390 [file] [log] [blame]
Boris Brezillonc36ff262018-04-26 18:18:14 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright (C) 2018 Exceet Electronics GmbH
4 * Copyright (C) 2018 Bootlin
5 *
6 * Author: Boris Brezillon <boris.brezillon@bootlin.com>
7 */
8
9#ifndef __LINUX_SPI_MEM_H
10#define __LINUX_SPI_MEM_H
11
12#include <linux/spi/spi.h>
13
14#define SPI_MEM_OP_CMD(__opcode, __buswidth) \
15 { \
16 .buswidth = __buswidth, \
17 .opcode = __opcode, \
18 }
19
20#define SPI_MEM_OP_ADDR(__nbytes, __val, __buswidth) \
21 { \
22 .nbytes = __nbytes, \
23 .val = __val, \
24 .buswidth = __buswidth, \
25 }
26
27#define SPI_MEM_OP_NO_ADDR { }
28
29#define SPI_MEM_OP_DUMMY(__nbytes, __buswidth) \
30 { \
31 .nbytes = __nbytes, \
32 .buswidth = __buswidth, \
33 }
34
35#define SPI_MEM_OP_NO_DUMMY { }
36
37#define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth) \
38 { \
39 .dir = SPI_MEM_DATA_IN, \
40 .nbytes = __nbytes, \
41 .buf.in = __buf, \
42 .buswidth = __buswidth, \
43 }
44
45#define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, __buswidth) \
46 { \
47 .dir = SPI_MEM_DATA_OUT, \
48 .nbytes = __nbytes, \
49 .buf.out = __buf, \
50 .buswidth = __buswidth, \
51 }
52
53#define SPI_MEM_OP_NO_DATA { }
54
55/**
56 * enum spi_mem_data_dir - describes the direction of a SPI memory data
57 * transfer from the controller perspective
58 * @SPI_MEM_DATA_IN: data coming from the SPI memory
59 * @SPI_MEM_DATA_OUT: data sent the SPI memory
60 */
61enum spi_mem_data_dir {
62 SPI_MEM_DATA_IN,
63 SPI_MEM_DATA_OUT,
64};
65
66/**
67 * struct spi_mem_op - describes a SPI memory operation
68 * @cmd.buswidth: number of IO lines used to transmit the command
69 * @cmd.opcode: operation opcode
70 * @addr.nbytes: number of address bytes to send. Can be zero if the operation
71 * does not need to send an address
72 * @addr.buswidth: number of IO lines used to transmit the address cycles
73 * @addr.val: address value. This value is always sent MSB first on the bus.
74 * Note that only @addr.nbytes are taken into account in this
75 * address value, so users should make sure the value fits in the
76 * assigned number of bytes.
77 * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can
78 * be zero if the operation does not require dummy bytes
79 * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes
80 * @data.buswidth: number of IO lanes used to send/receive the data
81 * @data.dir: direction of the transfer
Boris Brezillon60489f02018-09-20 09:31:10 +020082 * @data.nbytes: number of data bytes to send/receive. Can be zero if the
83 * operation does not involve transferring data
Boris Brezillonc949a8e2018-09-20 09:31:11 +020084 * @data.buf.in: input buffer (must be DMA-able)
85 * @data.buf.out: output buffer (must be DMA-able)
Boris Brezillonc36ff262018-04-26 18:18:14 +020086 */
87struct spi_mem_op {
88 struct {
89 u8 buswidth;
90 u8 opcode;
91 } cmd;
92
93 struct {
94 u8 nbytes;
95 u8 buswidth;
96 u64 val;
97 } addr;
98
99 struct {
100 u8 nbytes;
101 u8 buswidth;
102 } dummy;
103
104 struct {
105 u8 buswidth;
106 enum spi_mem_data_dir dir;
107 unsigned int nbytes;
Boris Brezillonc36ff262018-04-26 18:18:14 +0200108 union {
109 void *in;
110 const void *out;
111 } buf;
112 } data;
113};
114
115#define SPI_MEM_OP(__cmd, __addr, __dummy, __data) \
116 { \
117 .cmd = __cmd, \
118 .addr = __addr, \
119 .dummy = __dummy, \
120 .data = __data, \
121 }
122
123/**
124 * struct spi_mem - describes a SPI memory device
125 * @spi: the underlying SPI device
Frieder Schrempf06bcb512018-08-02 14:53:52 +0200126 * @drvpriv: spi_mem_driver private data
Frieder Schrempf5d27a9c2018-08-02 14:53:53 +0200127 * @name: name of the SPI memory device
Boris Brezillonc36ff262018-04-26 18:18:14 +0200128 *
129 * Extra information that describe the SPI memory device and may be needed by
130 * the controller to properly handle this device should be placed here.
131 *
132 * One example would be the device size since some controller expose their SPI
133 * mem devices through a io-mapped region.
134 */
135struct spi_mem {
136 struct spi_device *spi;
137 void *drvpriv;
Boris Brezillon401c0d72018-08-03 11:50:39 +0200138 const char *name;
Boris Brezillonc36ff262018-04-26 18:18:14 +0200139};
140
141/**
142 * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
143 * device
144 * @mem: memory device
145 * @data: data to attach to the memory device
146 */
147static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
148{
149 mem->drvpriv = data;
150}
151
152/**
153 * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
154 * device
155 * @mem: memory device
156 *
157 * Return: the data attached to the mem device.
158 */
159static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
160{
161 return mem->drvpriv;
162}
163
164/**
165 * struct spi_controller_mem_ops - SPI memory operations
166 * @adjust_op_size: shrink the data xfer of an operation to match controller's
167 * limitations (can be alignment of max RX/TX size
168 * limitations)
169 * @supports_op: check if an operation is supported by the controller
170 * @exec_op: execute a SPI memory operation
Frieder Schrempf5d27a9c2018-08-02 14:53:53 +0200171 * @get_name: get a custom name for the SPI mem device from the controller.
172 * This might be needed if the controller driver has been ported
173 * to use the SPI mem layer and a custom name is used to keep
174 * mtdparts compatible.
175 * Note that if the implementation of this function allocates memory
176 * dynamically, then it should do so with devm_xxx(), as we don't
177 * have a ->free_name() function.
Boris Brezillonc36ff262018-04-26 18:18:14 +0200178 *
179 * This interface should be implemented by SPI controllers providing an
180 * high-level interface to execute SPI memory operation, which is usually the
181 * case for QSPI controllers.
182 */
183struct spi_controller_mem_ops {
184 int (*adjust_op_size)(struct spi_mem *mem, struct spi_mem_op *op);
185 bool (*supports_op)(struct spi_mem *mem,
186 const struct spi_mem_op *op);
187 int (*exec_op)(struct spi_mem *mem,
188 const struct spi_mem_op *op);
Frieder Schrempf5d27a9c2018-08-02 14:53:53 +0200189 const char *(*get_name)(struct spi_mem *mem);
Boris Brezillonc36ff262018-04-26 18:18:14 +0200190};
191
192/**
193 * struct spi_mem_driver - SPI memory driver
194 * @spidrv: inherit from a SPI driver
195 * @probe: probe a SPI memory. Usually where detection/initialization takes
196 * place
197 * @remove: remove a SPI memory
198 * @shutdown: take appropriate action when the system is shutdown
199 *
200 * This is just a thin wrapper around a spi_driver. The core takes care of
201 * allocating the spi_mem object and forwarding the probe/remove/shutdown
202 * request to the spi_mem_driver. The reason we use this wrapper is because
203 * we might have to stuff more information into the spi_mem struct to let
204 * SPI controllers know more about the SPI memory they interact with, and
205 * having this intermediate layer allows us to do that without adding more
206 * useless fields to the spi_device object.
207 */
208struct spi_mem_driver {
209 struct spi_driver spidrv;
210 int (*probe)(struct spi_mem *mem);
211 int (*remove)(struct spi_mem *mem);
212 void (*shutdown)(struct spi_mem *mem);
213};
214
215#if IS_ENABLED(CONFIG_SPI_MEM)
216int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
217 const struct spi_mem_op *op,
218 struct sg_table *sg);
219
220void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
221 const struct spi_mem_op *op,
222 struct sg_table *sg);
223#else
224static inline int
225spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
226 const struct spi_mem_op *op,
227 struct sg_table *sg)
228{
229 return -ENOTSUPP;
230}
231
232static inline void
233spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
234 const struct spi_mem_op *op,
235 struct sg_table *sg)
236{
237}
238#endif /* CONFIG_SPI_MEM */
239
240int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op);
241
242bool spi_mem_supports_op(struct spi_mem *mem,
243 const struct spi_mem_op *op);
244
245int spi_mem_exec_op(struct spi_mem *mem,
246 const struct spi_mem_op *op);
247
Frieder Schrempf5d27a9c2018-08-02 14:53:53 +0200248const char *spi_mem_get_name(struct spi_mem *mem);
249
Boris Brezillonc36ff262018-04-26 18:18:14 +0200250int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
251 struct module *owner);
252
253void spi_mem_driver_unregister(struct spi_mem_driver *drv);
254
255#define spi_mem_driver_register(__drv) \
256 spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
257
258#define module_spi_mem_driver(__drv) \
259 module_driver(__drv, spi_mem_driver_register, \
260 spi_mem_driver_unregister)
261
262#endif /* __LINUX_SPI_MEM_H */