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