xref: /linux/include/linux/spi/spi-mem.h (revision 1b49e363252632d0493546511a41a65ed1a6fbbb)
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 __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 		.nbytes = 1,					\
19 		.buswidth = __buswidth,				\
20 		.opcode = __opcode,				\
21 	}
22 
23 #define SPI_MEM_DTR_OP_RPT_CMD(__opcode, __buswidth)		\
24 	{							\
25 		.nbytes = 2,					\
26 		.opcode = __opcode | __opcode << 8,		\
27 		.buswidth = __buswidth,				\
28 		.dtr = true,					\
29 	}
30 
31 #define SPI_MEM_OP_ADDR(__nbytes, __val, __buswidth)		\
32 	{							\
33 		.nbytes = __nbytes,				\
34 		.buswidth = __buswidth,				\
35 		.val = __val,					\
36 	}
37 
38 #define SPI_MEM_DTR_OP_ADDR(__nbytes, __val, __buswidth)	\
39 	{							\
40 		.nbytes = __nbytes,				\
41 		.val = __val,					\
42 		.buswidth = __buswidth,				\
43 		.dtr = true,					\
44 	}
45 
46 #define SPI_MEM_DTR_OP_RPT_ADDR(__val, __buswidth)		\
47 	{							\
48 		.nbytes = 2,					\
49 		.val = __val | __val << 8,			\
50 		.buswidth = __buswidth,				\
51 		.dtr = true,					\
52 	}
53 
54 #define SPI_MEM_DTR_OP_RPT_ADDR(__val, __buswidth)		\
55 	{							\
56 		.nbytes = 2,					\
57 		.val = __val | __val << 8,			\
58 		.buswidth = __buswidth,				\
59 		.dtr = true,					\
60 	}
61 
62 #define SPI_MEM_OP_NO_ADDR	{ }
63 
64 #define SPI_MEM_OP_DUMMY(__nbytes, __buswidth)			\
65 	{							\
66 		.nbytes = __nbytes,				\
67 		.buswidth = __buswidth,				\
68 	}
69 
70 #define SPI_MEM_DTR_OP_DUMMY(__nbytes, __buswidth)		\
71 	{							\
72 		.nbytes = __nbytes,				\
73 		.buswidth = __buswidth,				\
74 		.dtr = true,					\
75 	}
76 
77 #define SPI_MEM_OP_NO_DUMMY	{ }
78 
79 #define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth)		\
80 	{							\
81 		.buswidth = __buswidth,				\
82 		.dir = SPI_MEM_DATA_IN,				\
83 		.nbytes = __nbytes,				\
84 		.buf.in = __buf,				\
85 	}
86 
87 #define SPI_MEM_DTR_OP_DATA_IN(__nbytes, __buf, __buswidth)	\
88 	{							\
89 		.dir = SPI_MEM_DATA_IN,				\
90 		.nbytes = __nbytes,				\
91 		.buf.in = __buf,				\
92 		.buswidth = __buswidth,				\
93 		.dtr = true,					\
94 	}
95 
96 #define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, __buswidth)	\
97 	{							\
98 		.buswidth = __buswidth,				\
99 		.dir = SPI_MEM_DATA_OUT,			\
100 		.nbytes = __nbytes,				\
101 		.buf.out = __buf,				\
102 	}
103 
104 #define SPI_MEM_DTR_OP_DATA_OUT(__nbytes, __buf, __buswidth)	\
105 	{							\
106 		.dir = SPI_MEM_DATA_OUT,			\
107 		.nbytes = __nbytes,				\
108 		.buf.out = __buf,				\
109 		.buswidth = __buswidth,				\
110 		.dtr = true,					\
111 	}
112 
113 #define SPI_MEM_OP_NO_DATA	{ }
114 
115 /**
116  * enum spi_mem_data_dir - describes the direction of a SPI memory data
117  *			   transfer from the controller perspective
118  * @SPI_MEM_NO_DATA: no data transferred
119  * @SPI_MEM_DATA_IN: data coming from the SPI memory
120  * @SPI_MEM_DATA_OUT: data sent to the SPI memory
121  */
122 enum spi_mem_data_dir {
123 	SPI_MEM_NO_DATA,
124 	SPI_MEM_DATA_IN,
125 	SPI_MEM_DATA_OUT,
126 };
127 
128 #define SPI_MEM_OP_MAX_FREQ(__freq)				\
129 	.max_freq = __freq
130 
131 /**
132  * struct spi_mem_op - describes a SPI memory operation
133  * @cmd.nbytes: number of opcode bytes (only 1 or 2 are valid). The opcode is
134  *		sent MSB-first.
135  * @cmd.buswidth: number of IO lines used to transmit the command
136  * @cmd.opcode: operation opcode
137  * @cmd.dtr: whether the command opcode should be sent in DTR mode or not
138  * @addr.nbytes: number of address bytes to send. Can be zero if the operation
139  *		 does not need to send an address
140  * @addr.buswidth: number of IO lines used to transmit the address cycles
141  * @addr.dtr: whether the address should be sent in DTR mode or not
142  * @addr.val: address value. This value is always sent MSB first on the bus.
143  *	      Note that only @addr.nbytes are taken into account in this
144  *	      address value, so users should make sure the value fits in the
145  *	      assigned number of bytes.
146  * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can
147  *		  be zero if the operation does not require dummy bytes
148  * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes
149  * @dummy.dtr: whether the dummy bytes should be sent in DTR mode or not
150  * @data.buswidth: number of IO lanes used to send/receive the data
151  * @data.dtr: whether the data should be sent in DTR mode or not
152  * @data.ecc: whether error correction is required or not
153  * @data.swap16: whether the byte order of 16-bit words is swapped when read
154  *		 or written in Octal DTR mode compared to STR mode.
155  * @data.dir: direction of the transfer
156  * @data.nbytes: number of data bytes to send/receive. Can be zero if the
157  *		 operation does not involve transferring data
158  * @data.buf.in: input buffer (must be DMA-able)
159  * @data.buf.out: output buffer (must be DMA-able)
160  * @max_freq: frequency limitation wrt this operation. 0 means there is no
161  *	      specific constraint and the highest achievable frequency can be
162  *	      attempted.
163  */
164 struct spi_mem_op {
165 	struct {
166 		u8 nbytes;
167 		u8 buswidth;
168 		u8 dtr : 1;
169 		u8 __pad : 7;
170 		u16 opcode;
171 	} cmd;
172 
173 	struct {
174 		u8 nbytes;
175 		u8 buswidth;
176 		u8 dtr : 1;
177 		u8 __pad : 7;
178 		u64 val;
179 	} addr;
180 
181 	struct {
182 		u8 nbytes;
183 		u8 buswidth;
184 		u8 dtr : 1;
185 		u8 __pad : 7;
186 	} dummy;
187 
188 	struct {
189 		u8 buswidth;
190 		u8 dtr : 1;
191 		u8 ecc : 1;
192 		u8 swap16 : 1;
193 		u8 __pad : 5;
194 		enum spi_mem_data_dir dir;
195 		unsigned int nbytes;
196 		union {
197 			void *in;
198 			const void *out;
199 		} buf;
200 	} data;
201 
202 	unsigned int max_freq;
203 };
204 
205 #define SPI_MEM_OP(__cmd, __addr, __dummy, __data, ...)		\
206 	{							\
207 		.cmd = __cmd,					\
208 		.addr = __addr,					\
209 		.dummy = __dummy,				\
210 		.data = __data,					\
211 		__VA_ARGS__					\
212 	}
213 
214 /**
215  * struct spi_mem_dirmap_info - Direct mapping information
216  * @op_tmpl: operation template that should be used by the direct mapping when
217  *	     the memory device is accessed
218  * @offset: absolute offset this direct mapping is pointing to
219  * @length: length in byte of this direct mapping
220  *
221  * These information are used by the controller specific implementation to know
222  * the portion of memory that is directly mapped and the spi_mem_op that should
223  * be used to access the device.
224  * A direct mapping is only valid for one direction (read or write) and this
225  * direction is directly encoded in the ->op_tmpl.data.dir field.
226  */
227 struct spi_mem_dirmap_info {
228 	struct spi_mem_op op_tmpl;
229 	u64 offset;
230 	u64 length;
231 };
232 
233 /**
234  * struct spi_mem_dirmap_desc - Direct mapping descriptor
235  * @mem: the SPI memory device this direct mapping is attached to
236  * @info: information passed at direct mapping creation time
237  * @nodirmap: set to 1 if the SPI controller does not implement
238  *	      ->mem_ops->dirmap_create() or when this function returned an
239  *	      error. If @nodirmap is true, all spi_mem_dirmap_{read,write}()
240  *	      calls will use spi_mem_exec_op() to access the memory. This is a
241  *	      degraded mode that allows spi_mem drivers to use the same code
242  *	      no matter whether the controller supports direct mapping or not
243  * @priv: field pointing to controller specific data
244  *
245  * Common part of a direct mapping descriptor. This object is created by
246  * spi_mem_dirmap_create() and controller implementation of ->create_dirmap()
247  * can create/attach direct mapping resources to the descriptor in the ->priv
248  * field.
249  */
250 struct spi_mem_dirmap_desc {
251 	struct spi_mem *mem;
252 	struct spi_mem_dirmap_info info;
253 	unsigned int nodirmap;
254 	void *priv;
255 };
256 
257 /**
258  * struct spi_mem - describes a SPI memory device
259  * @spi: the underlying SPI device
260  * @drvpriv: spi_mem_driver private data
261  * @name: name of the SPI memory device
262  *
263  * Extra information that describe the SPI memory device and may be needed by
264  * the controller to properly handle this device should be placed here.
265  *
266  * One example would be the device size since some controller expose their SPI
267  * mem devices through a io-mapped region.
268  */
269 struct spi_mem {
270 	struct spi_device *spi;
271 	void *drvpriv;
272 	const char *name;
273 };
274 
275 /**
276  * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
277  *				  device
278  * @mem: memory device
279  * @data: data to attach to the memory device
280  */
spi_mem_set_drvdata(struct spi_mem * mem,void * data)281 static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
282 {
283 	mem->drvpriv = data;
284 }
285 
286 /**
287  * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
288  *				  device
289  * @mem: memory device
290  *
291  * Return: the data attached to the mem device.
292  */
spi_mem_get_drvdata(struct spi_mem * mem)293 static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
294 {
295 	return mem->drvpriv;
296 }
297 
298 /**
299  * struct spi_controller_mem_ops - SPI memory operations
300  * @adjust_op_size: shrink the data xfer of an operation to match controller's
301  *		    limitations (can be alignment or max RX/TX size
302  *		    limitations)
303  * @supports_op: check if an operation is supported by the controller
304  * @exec_op: execute a SPI memory operation
305  *           not all driver provides supports_op(), so it can return -EOPNOTSUPP
306  *           if the op is not supported by the driver/controller
307  * @get_name: get a custom name for the SPI mem device from the controller.
308  *	      This might be needed if the controller driver has been ported
309  *	      to use the SPI mem layer and a custom name is used to keep
310  *	      mtdparts compatible.
311  *	      Note that if the implementation of this function allocates memory
312  *	      dynamically, then it should do so with devm_xxx(), as we don't
313  *	      have a ->free_name() function.
314  * @dirmap_create: create a direct mapping descriptor that can later be used to
315  *		   access the memory device. This method is optional
316  * @dirmap_destroy: destroy a memory descriptor previous created by
317  *		    ->dirmap_create()
318  * @dirmap_read: read data from the memory device using the direct mapping
319  *		 created by ->dirmap_create(). The function can return less
320  *		 data than requested (for example when the request is crossing
321  *		 the currently mapped area), and the caller of
322  *		 spi_mem_dirmap_read() is responsible for calling it again in
323  *		 this case.
324  * @dirmap_write: write data to the memory device using the direct mapping
325  *		  created by ->dirmap_create(). The function can return less
326  *		  data than requested (for example when the request is crossing
327  *		  the currently mapped area), and the caller of
328  *		  spi_mem_dirmap_write() is responsible for calling it again in
329  *		  this case.
330  * @poll_status: poll memory device status until (status & mask) == match or
331  *               when the timeout has expired. It fills the data buffer with
332  *               the last status value.
333  *
334  * This interface should be implemented by SPI controllers providing an
335  * high-level interface to execute SPI memory operation, which is usually the
336  * case for QSPI controllers.
337  *
338  * Note on ->dirmap_{read,write}(): drivers should avoid accessing the direct
339  * mapping from the CPU because doing that can stall the CPU waiting for the
340  * SPI mem transaction to finish, and this will make real-time maintainers
341  * unhappy and might make your system less reactive. Instead, drivers should
342  * use DMA to access this direct mapping.
343  */
344 struct spi_controller_mem_ops {
345 	int (*adjust_op_size)(struct spi_mem *mem, struct spi_mem_op *op);
346 	bool (*supports_op)(struct spi_mem *mem,
347 			    const struct spi_mem_op *op);
348 	int (*exec_op)(struct spi_mem *mem,
349 		       const struct spi_mem_op *op);
350 	const char *(*get_name)(struct spi_mem *mem);
351 	int (*dirmap_create)(struct spi_mem_dirmap_desc *desc);
352 	void (*dirmap_destroy)(struct spi_mem_dirmap_desc *desc);
353 	ssize_t (*dirmap_read)(struct spi_mem_dirmap_desc *desc,
354 			       u64 offs, size_t len, void *buf);
355 	ssize_t (*dirmap_write)(struct spi_mem_dirmap_desc *desc,
356 				u64 offs, size_t len, const void *buf);
357 	int (*poll_status)(struct spi_mem *mem,
358 			   const struct spi_mem_op *op,
359 			   u16 mask, u16 match,
360 			   unsigned long initial_delay_us,
361 			   unsigned long polling_rate_us,
362 			   unsigned long timeout_ms);
363 };
364 
365 /**
366  * struct spi_controller_mem_caps - SPI memory controller capabilities
367  * @dtr: Supports DTR operations
368  * @ecc: Supports operations with error correction
369  * @swap16: Supports swapping bytes on a 16 bit boundary when configured in
370  *	    Octal DTR
371  * @per_op_freq: Supports per operation frequency switching
372  */
373 struct spi_controller_mem_caps {
374 	bool dtr;
375 	bool ecc;
376 	bool swap16;
377 	bool per_op_freq;
378 };
379 
380 #define spi_mem_controller_is_capable(ctlr, cap)	\
381 	((ctlr)->mem_caps && (ctlr)->mem_caps->cap)
382 
383 /**
384  * struct spi_mem_driver - SPI memory driver
385  * @spidrv: inherit from a SPI driver
386  * @probe: probe a SPI memory. Usually where detection/initialization takes
387  *	   place
388  * @remove: remove a SPI memory
389  * @shutdown: take appropriate action when the system is shutdown
390  *
391  * This is just a thin wrapper around a spi_driver. The core takes care of
392  * allocating the spi_mem object and forwarding the probe/remove/shutdown
393  * request to the spi_mem_driver. The reason we use this wrapper is because
394  * we might have to stuff more information into the spi_mem struct to let
395  * SPI controllers know more about the SPI memory they interact with, and
396  * having this intermediate layer allows us to do that without adding more
397  * useless fields to the spi_device object.
398  */
399 struct spi_mem_driver {
400 	struct spi_driver spidrv;
401 	int (*probe)(struct spi_mem *mem);
402 	int (*remove)(struct spi_mem *mem);
403 	void (*shutdown)(struct spi_mem *mem);
404 };
405 
406 #if IS_ENABLED(CONFIG_SPI_MEM)
407 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
408 				       const struct spi_mem_op *op,
409 				       struct sg_table *sg);
410 
411 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
412 					  const struct spi_mem_op *op,
413 					  struct sg_table *sg);
414 
415 bool spi_mem_default_supports_op(struct spi_mem *mem,
416 				 const struct spi_mem_op *op);
417 #else
418 static inline int
spi_controller_dma_map_mem_op_data(struct spi_controller * ctlr,const struct spi_mem_op * op,struct sg_table * sg)419 spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
420 				   const struct spi_mem_op *op,
421 				   struct sg_table *sg)
422 {
423 	return -ENOTSUPP;
424 }
425 
426 static inline void
spi_controller_dma_unmap_mem_op_data(struct spi_controller * ctlr,const struct spi_mem_op * op,struct sg_table * sg)427 spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
428 				     const struct spi_mem_op *op,
429 				     struct sg_table *sg)
430 {
431 }
432 
433 static inline
spi_mem_default_supports_op(struct spi_mem * mem,const struct spi_mem_op * op)434 bool spi_mem_default_supports_op(struct spi_mem *mem,
435 				 const struct spi_mem_op *op)
436 {
437 	return false;
438 }
439 #endif /* CONFIG_SPI_MEM */
440 
441 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op);
442 void spi_mem_adjust_op_freq(struct spi_mem *mem, struct spi_mem_op *op);
443 u64 spi_mem_calc_op_duration(struct spi_mem *mem, struct spi_mem_op *op);
444 
445 bool spi_mem_supports_op(struct spi_mem *mem,
446 			 const struct spi_mem_op *op);
447 
448 int spi_mem_exec_op(struct spi_mem *mem,
449 		    const struct spi_mem_op *op);
450 
451 const char *spi_mem_get_name(struct spi_mem *mem);
452 
453 struct spi_mem_dirmap_desc *
454 spi_mem_dirmap_create(struct spi_mem *mem,
455 		      const struct spi_mem_dirmap_info *info);
456 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc);
457 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
458 			    u64 offs, size_t len, void *buf);
459 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
460 			     u64 offs, size_t len, const void *buf);
461 struct spi_mem_dirmap_desc *
462 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem,
463 			   const struct spi_mem_dirmap_info *info);
464 void devm_spi_mem_dirmap_destroy(struct device *dev,
465 				 struct spi_mem_dirmap_desc *desc);
466 
467 int spi_mem_poll_status(struct spi_mem *mem,
468 			const struct spi_mem_op *op,
469 			u16 mask, u16 match,
470 			unsigned long initial_delay_us,
471 			unsigned long polling_delay_us,
472 			u16 timeout_ms);
473 
474 int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
475 				       struct module *owner);
476 
477 void spi_mem_driver_unregister(struct spi_mem_driver *drv);
478 
479 #define spi_mem_driver_register(__drv)                                  \
480 	spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
481 
482 #define module_spi_mem_driver(__drv)                                    \
483 	module_driver(__drv, spi_mem_driver_register,                   \
484 		      spi_mem_driver_unregister)
485 
486 #endif /* __LINUX_SPI_MEM_H */
487