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