1 /* 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 struct udevice;
15 
16 #define SPI_MEM_OP_CMD(__opcode, __buswidth)			\
17 	{							\
18 		.buswidth = __buswidth,				\
19 		.opcode = __opcode,				\
20 		.nbytes = 1,					\
21 	}
22 
23 #define SPI_MEM_OP_ADDR(__nbytes, __val, __buswidth)		\
24 	{							\
25 		.nbytes = __nbytes,				\
26 		.val = __val,					\
27 		.buswidth = __buswidth,				\
28 	}
29 
30 #define SPI_MEM_OP_NO_ADDR	{ }
31 
32 #define SPI_MEM_OP_DUMMY(__nbytes, __buswidth)			\
33 	{							\
34 		.nbytes = __nbytes,				\
35 		.buswidth = __buswidth,				\
36 	}
37 
38 #define SPI_MEM_OP_NO_DUMMY	{ }
39 
40 #define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth)		\
41 	{							\
42 		.dir = SPI_MEM_DATA_IN,				\
43 		.nbytes = __nbytes,				\
44 		.buf.in = __buf,				\
45 		.buswidth = __buswidth,				\
46 	}
47 
48 #define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, __buswidth)	\
49 	{							\
50 		.dir = SPI_MEM_DATA_OUT,			\
51 		.nbytes = __nbytes,				\
52 		.buf.out = __buf,				\
53 		.buswidth = __buswidth,				\
54 	}
55 
56 #define SPI_MEM_OP_NO_DATA	{ }
57 
58 /**
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 the SPI memory
64  */
65 enum spi_mem_data_dir {
66 	SPI_MEM_NO_DATA,
67 	SPI_MEM_DATA_IN,
68 	SPI_MEM_DATA_OUT,
69 };
70 
71 /**
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
74  *		sent MSB-first.
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.val: address value. This value is always sent MSB first on the bus.
82  *	      Note that only @addr.nbytes are taken into account in this
83  *	      address value, so users should make sure the value fits in the
84  *	      assigned number of bytes.
85  * @addr.dtr: whether the address should be sent in DTR mode or not
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.buf.in: input buffer
94  * @data.buf.out: output buffer
95  */
96 struct spi_mem_op {
97 	struct {
98 		u8 nbytes;
99 		u8 buswidth;
100 		u8 dtr : 1;
101 		u16 opcode;
102 	} cmd;
103 
104 	struct {
105 		u8 nbytes;
106 		u8 buswidth;
107 		u8 dtr : 1;
108 		u64 val;
109 	} addr;
110 
111 	struct {
112 		u8 nbytes;
113 		u8 buswidth;
114 		u8 dtr : 1;
115 	} dummy;
116 
117 	struct {
118 		u8 buswidth;
119 		u8 dtr : 1;
120 		enum spi_mem_data_dir dir;
121 		unsigned int nbytes;
122 		/* buf.{in,out} must be DMA-able. */
123 		union {
124 			void *in;
125 			const void *out;
126 		} buf;
127 	} data;
128 };
129 
130 #define SPI_MEM_OP(__cmd, __addr, __dummy, __data)		\
131 	{							\
132 		.cmd = __cmd,					\
133 		.addr = __addr,					\
134 		.dummy = __dummy,				\
135 		.data = __data,					\
136 	}
137 /**
138  * struct spi_mem_dirmap_info - Direct mapping information
139  * @op_tmpl: operation template that should be used by the direct mapping when
140  *	     the memory device is accessed
141  * @offset: absolute offset this direct mapping is pointing to
142  * @length: length in byte of this direct mapping
143  *
144  * This information is used by the controller specific implementation to know
145  * the portion of memory that is directly mapped and the spi_mem_op that should
146  * be used to access the device.
147  * A direct mapping is only valid for one direction (read or write) and this
148  * direction is directly encoded in the ->op_tmpl.data.dir field.
149  */
150 struct spi_mem_dirmap_info {
151 	struct spi_mem_op op_tmpl;
152 	u64 offset;
153 	u64 length;
154 };
155 
156 /**
157  * struct spi_mem_dirmap_desc - Direct mapping descriptor
158  * @mem: the SPI memory device this direct mapping is attached to
159  * @info: information passed at direct mapping creation time
160  * @nodirmap: set to 1 if the SPI controller does not implement
161  *            ->mem_ops->dirmap_create() or when this function returned an
162  *            error. If @nodirmap is true, all spi_mem_dirmap_{read,write}()
163  *            calls will use spi_mem_exec_op() to access the memory. This is a
164  *            degraded mode that allows spi_mem drivers to use the same code
165  *            no matter whether the controller supports direct mapping or not
166  * @priv: field pointing to controller specific data
167  *
168  * Common part of a direct mapping descriptor. This object is created by
169  * spi_mem_dirmap_create() and controller implementation of ->create_dirmap()
170  * can create/attach direct mapping resources to the descriptor in the ->priv
171  * field.
172  */
173 struct spi_mem_dirmap_desc {
174 	struct spi_slave *slave;
175 	struct spi_mem_dirmap_info info;
176 	unsigned int nodirmap;
177 	void *priv;
178 };
179 
180 #ifndef __UBOOT__
181 /**
182  * struct spi_mem - describes a SPI memory device
183  * @spi: the underlying SPI device
184  * @drvpriv: spi_mem_driver private data
185  *
186  * Extra information that describe the SPI memory device and may be needed by
187  * the controller to properly handle this device should be placed here.
188  *
189  * One example would be the device size since some controller expose their SPI
190  * mem devices through a io-mapped region.
191  */
192 struct spi_mem {
193 	struct udevice *dev;
194 	void *drvpriv;
195 };
196 
197 /**
198  * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
199  *				  device
200  * @mem: memory device
201  * @data: data to attach to the memory device
202  */
spi_mem_set_drvdata(struct spi_mem * mem,void * data)203 static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
204 {
205 	mem->drvpriv = data;
206 }
207 
208 /**
209  * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
210  *				  device
211  * @mem: memory device
212  *
213  * Return: the data attached to the mem device.
214  */
spi_mem_get_drvdata(struct spi_mem * mem)215 static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
216 {
217 	return mem->drvpriv;
218 }
219 #endif /* __UBOOT__ */
220 
221 /**
222  * struct spi_controller_mem_ops - SPI memory operations
223  * @adjust_op_size: shrink the data xfer of an operation to match controller's
224  *		    limitations (can be alignment of max RX/TX size
225  *		    limitations)
226  * @supports_op: check if an operation is supported by the controller
227  * @exec_op: execute a SPI memory operation
228  * @dirmap_create: create a direct mapping descriptor that can later be used to
229  *		   access the memory device. This method is optional
230  * @dirmap_destroy: destroy a memory descriptor previous created by
231  *		    ->dirmap_create()
232  * @dirmap_read: read data from the memory device using the direct mapping
233  *		 created by ->dirmap_create(). The function can return less
234  *		 data than requested (for example when the request is crossing
235  *		 the currently mapped area), and the caller of
236  *		 spi_mem_dirmap_read() is responsible for calling it again in
237  *		 this case.
238  * @dirmap_write: write data to the memory device using the direct mapping
239  *		  created by ->dirmap_create(). The function can return less
240  *		  data than requested (for example when the request is crossing
241  *		  the currently mapped area), and the caller of
242  *		  spi_mem_dirmap_write() is responsible for calling it again in
243  *		  this case.
244  *
245  * This interface should be implemented by SPI controllers providing an
246  * high-level interface to execute SPI memory operation, which is usually the
247  * case for QSPI controllers.
248  *
249  * Note on ->dirmap_{read,write}(): drivers should avoid accessing the direct
250  * mapping from the CPU because doing that can stall the CPU waiting for the
251  * SPI mem transaction to finish, and this will make real-time maintainers
252  * unhappy and might make your system less reactive. Instead, drivers should
253  * use DMA to access this direct mapping.
254  */
255 struct spi_controller_mem_ops {
256 	int (*adjust_op_size)(struct spi_slave *slave, struct spi_mem_op *op);
257 	bool (*supports_op)(struct spi_slave *slave,
258 			    const struct spi_mem_op *op);
259 	int (*exec_op)(struct spi_slave *slave,
260 		       const struct spi_mem_op *op);
261 	int (*dirmap_create)(struct spi_mem_dirmap_desc *desc);
262 	void (*dirmap_destroy)(struct spi_mem_dirmap_desc *desc);
263 	ssize_t (*dirmap_read)(struct spi_mem_dirmap_desc *desc,
264 			       u64 offs, size_t len, void *buf);
265 	ssize_t (*dirmap_write)(struct spi_mem_dirmap_desc *desc,
266 				u64 offs, size_t len, const void *buf);
267 };
268 
269 #ifndef __UBOOT__
270 /**
271  * struct spi_mem_driver - SPI memory driver
272  * @spidrv: inherit from a SPI driver
273  * @probe: probe a SPI memory. Usually where detection/initialization takes
274  *	   place
275  * @remove: remove a SPI memory
276  * @shutdown: take appropriate action when the system is shutdown
277  *
278  * This is just a thin wrapper around a spi_driver. The core takes care of
279  * allocating the spi_mem object and forwarding the probe/remove/shutdown
280  * request to the spi_mem_driver. The reason we use this wrapper is because
281  * we might have to stuff more information into the spi_mem struct to let
282  * SPI controllers know more about the SPI memory they interact with, and
283  * having this intermediate layer allows us to do that without adding more
284  * useless fields to the spi_device object.
285  */
286 struct spi_mem_driver {
287 	struct spi_driver spidrv;
288 	int (*probe)(struct spi_mem *mem);
289 	int (*remove)(struct spi_mem *mem);
290 	void (*shutdown)(struct spi_mem *mem);
291 };
292 
293 #if IS_ENABLED(CONFIG_SPI_MEM)
294 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
295 				       const struct spi_mem_op *op,
296 				       struct sg_table *sg);
297 
298 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
299 					  const struct spi_mem_op *op,
300 					  struct sg_table *sg);
301 #else
302 static inline int
spi_controller_dma_map_mem_op_data(struct spi_controller * ctlr,const struct spi_mem_op * op,struct sg_table * sg)303 spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
304 				   const struct spi_mem_op *op,
305 				   struct sg_table *sg)
306 {
307 	return -ENOSYS;
308 }
309 
310 static inline void
spi_controller_dma_unmap_mem_op_data(struct spi_controller * ctlr,const struct spi_mem_op * op,struct sg_table * sg)311 spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
312 				     const struct spi_mem_op *op,
313 				     struct sg_table *sg)
314 {
315 }
316 #endif /* CONFIG_SPI_MEM */
317 #endif /* __UBOOT__ */
318 
319 int spi_mem_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op);
320 
321 bool spi_mem_supports_op(struct spi_slave *slave, const struct spi_mem_op *op);
322 bool spi_mem_dtr_supports_op(struct spi_slave *slave,
323 			     const struct spi_mem_op *op);
324 
325 bool spi_mem_default_supports_op(struct spi_slave *slave,
326 				 const struct spi_mem_op *op);
327 
328 int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op);
329 
330 bool spi_mem_default_supports_op(struct spi_slave *mem,
331 				 const struct spi_mem_op *op);
332 
333 struct spi_mem_dirmap_desc *
334 spi_mem_dirmap_create(struct spi_slave *mem,
335 		      const struct spi_mem_dirmap_info *info);
336 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc);
337 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
338 			    u64 offs, size_t len, void *buf);
339 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
340 			     u64 offs, size_t len, const void *buf);
341 
342 #ifndef __UBOOT__
343 int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
344 				       struct module *owner);
345 
346 void spi_mem_driver_unregister(struct spi_mem_driver *drv);
347 
348 #define spi_mem_driver_register(__drv)                                  \
349 	spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
350 
351 #define module_spi_mem_driver(__drv)                                    \
352 	module_driver(__drv, spi_mem_driver_register,                   \
353 		      spi_mem_driver_unregister)
354 #endif
355 
356 #endif /* __LINUX_SPI_MEM_H */
357