blob: 250b6f5c47c273e04700c60edb1246f01969add3 [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 *
Peter Pan7529df42018-06-22 14:28:23 +02006 * Author:
7 * Peter Pan <peterpandong@micron.com>
8 * Boris Brezillon <boris.brezillon@bootlin.com>
Boris Brezillonc36ff262018-04-26 18:18:14 +02009 */
10
11#ifndef __LINUX_SPI_MEM_H
12#define __LINUX_SPI_MEM_H
13
14#include <linux/spi/spi.h>
15
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
Boris Brezillon0ebb2612018-11-06 17:05:31 +010060 * @SPI_MEM_NO_DATA: no data transferred
Boris Brezillonc36ff262018-04-26 18:18:14 +020061 * @SPI_MEM_DATA_IN: data coming from the SPI memory
Boris Brezillon6afe76a2018-11-06 17:05:30 +010062 * @SPI_MEM_DATA_OUT: data sent to the SPI memory
Boris Brezillonc36ff262018-04-26 18:18:14 +020063 */
64enum spi_mem_data_dir {
Boris Brezillon0ebb2612018-11-06 17:05:31 +010065 SPI_MEM_NO_DATA,
Boris Brezillonc36ff262018-04-26 18:18:14 +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
74 * @addr.nbytes: number of address bytes to send. Can be zero if the operation
75 * does not need to send an address
76 * @addr.buswidth: number of IO lines used to transmit the address cycles
77 * @addr.val: address value. This value is always sent MSB first on the bus.
78 * Note that only @addr.nbytes are taken into account in this
79 * address value, so users should make sure the value fits in the
80 * assigned number of bytes.
81 * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can
82 * be zero if the operation does not require dummy bytes
83 * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes
84 * @data.buswidth: number of IO lanes used to send/receive the data
85 * @data.dir: direction of the transfer
Boris Brezillon60489f02018-09-20 09:31:10 +020086 * @data.nbytes: number of data bytes to send/receive. Can be zero if the
87 * operation does not involve transferring data
Boris Brezillonc949a8e2018-09-20 09:31:11 +020088 * @data.buf.in: input buffer (must be DMA-able)
89 * @data.buf.out: output buffer (must be DMA-able)
Boris Brezillonc36ff262018-04-26 18:18:14 +020090 */
91struct spi_mem_op {
92 struct {
93 u8 buswidth;
94 u8 opcode;
95 } cmd;
96
97 struct {
98 u8 nbytes;
99 u8 buswidth;
100 u64 val;
101 } addr;
102
103 struct {
104 u8 nbytes;
105 u8 buswidth;
106 } dummy;
107
108 struct {
109 u8 buswidth;
110 enum spi_mem_data_dir dir;
111 unsigned int nbytes;
Boris Brezillonc36ff262018-04-26 18:18:14 +0200112 union {
113 void *in;
114 const void *out;
115 } buf;
116 } data;
117};
118
119#define SPI_MEM_OP(__cmd, __addr, __dummy, __data) \
120 { \
121 .cmd = __cmd, \
122 .addr = __addr, \
123 .dummy = __dummy, \
124 .data = __data, \
125 }
126
127/**
128 * struct spi_mem - describes a SPI memory device
129 * @spi: the underlying SPI device
Frieder Schrempf06bcb512018-08-02 14:53:52 +0200130 * @drvpriv: spi_mem_driver private data
Frieder Schrempf5d27a9c2018-08-02 14:53:53 +0200131 * @name: name of the SPI memory device
Boris Brezillonc36ff262018-04-26 18:18:14 +0200132 *
133 * Extra information that describe the SPI memory device and may be needed by
134 * the controller to properly handle this device should be placed here.
135 *
136 * One example would be the device size since some controller expose their SPI
137 * mem devices through a io-mapped region.
138 */
139struct spi_mem {
140 struct spi_device *spi;
141 void *drvpriv;
Boris Brezillon401c0d72018-08-03 11:50:39 +0200142 const char *name;
Boris Brezillonc36ff262018-04-26 18:18:14 +0200143};
144
145/**
146 * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
147 * device
148 * @mem: memory device
149 * @data: data to attach to the memory device
150 */
151static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
152{
153 mem->drvpriv = data;
154}
155
156/**
157 * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
158 * device
159 * @mem: memory device
160 *
161 * Return: the data attached to the mem device.
162 */
163static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
164{
165 return mem->drvpriv;
166}
167
168/**
169 * struct spi_controller_mem_ops - SPI memory operations
170 * @adjust_op_size: shrink the data xfer of an operation to match controller's
171 * limitations (can be alignment of max RX/TX size
172 * limitations)
173 * @supports_op: check if an operation is supported by the controller
174 * @exec_op: execute a SPI memory operation
Frieder Schrempf5d27a9c2018-08-02 14:53:53 +0200175 * @get_name: get a custom name for the SPI mem device from the controller.
176 * This might be needed if the controller driver has been ported
177 * to use the SPI mem layer and a custom name is used to keep
178 * mtdparts compatible.
179 * Note that if the implementation of this function allocates memory
180 * dynamically, then it should do so with devm_xxx(), as we don't
181 * have a ->free_name() function.
Boris Brezillonc36ff262018-04-26 18:18:14 +0200182 *
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_mem *mem, struct spi_mem_op *op);
189 bool (*supports_op)(struct spi_mem *mem,
190 const struct spi_mem_op *op);
191 int (*exec_op)(struct spi_mem *mem,
192 const struct spi_mem_op *op);
Frieder Schrempf5d27a9c2018-08-02 14:53:53 +0200193 const char *(*get_name)(struct spi_mem *mem);
Boris Brezillonc36ff262018-04-26 18:18:14 +0200194};
195
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{
233 return -ENOTSUPP;
234}
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
244int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op);
245
246bool spi_mem_supports_op(struct spi_mem *mem,
247 const struct spi_mem_op *op);
248
249int spi_mem_exec_op(struct spi_mem *mem,
250 const struct spi_mem_op *op);
251
Frieder Schrempf5d27a9c2018-08-02 14:53:53 +0200252const char *spi_mem_get_name(struct spi_mem *mem);
253
Boris Brezillonc36ff262018-04-26 18:18:14 +0200254int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
255 struct module *owner);
256
257void spi_mem_driver_unregister(struct spi_mem_driver *drv);
258
259#define spi_mem_driver_register(__drv) \
260 spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
261
262#define module_spi_mem_driver(__drv) \
263 module_driver(__drv, spi_mem_driver_register, \
264 spi_mem_driver_unregister)
265
266#endif /* __LINUX_SPI_MEM_H */