xref: /openbmc/linux/include/linux/spi/spi-mem.h (revision cbabf03c)
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 		.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 to 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.dtr: whether the address should be sent in DTR mode or not
82  * @addr.val: address value. This value is always sent MSB first on the bus.
83  *	      Note that only @addr.nbytes are taken into account in this
84  *	      address value, so users should make sure the value fits in the
85  *	      assigned number of bytes.
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.ecc: whether error correction is required or not
93  * @data.dir: direction of the transfer
94  * @data.nbytes: number of data bytes to send/receive. Can be zero if the
95  *		 operation does not involve transferring data
96  * @data.buf.in: input buffer (must be DMA-able)
97  * @data.buf.out: output buffer (must be DMA-able)
98  */
99 struct spi_mem_op {
100 	struct {
101 		u8 nbytes;
102 		u8 buswidth;
103 		u8 dtr : 1;
104 		u16 opcode;
105 	} cmd;
106 
107 	struct {
108 		u8 nbytes;
109 		u8 buswidth;
110 		u8 dtr : 1;
111 		u64 val;
112 	} addr;
113 
114 	struct {
115 		u8 nbytes;
116 		u8 buswidth;
117 		u8 dtr : 1;
118 	} dummy;
119 
120 	struct {
121 		u8 buswidth;
122 		u8 dtr : 1;
123 		u8 ecc : 1;
124 		enum spi_mem_data_dir dir;
125 		unsigned int nbytes;
126 		union {
127 			void *in;
128 			const void *out;
129 		} buf;
130 	} data;
131 };
132 
133 #define SPI_MEM_OP(__cmd, __addr, __dummy, __data)		\
134 	{							\
135 		.cmd = __cmd,					\
136 		.addr = __addr,					\
137 		.dummy = __dummy,				\
138 		.data = __data,					\
139 	}
140 
141 /**
142  * struct spi_mem_dirmap_info - Direct mapping information
143  * @op_tmpl: operation template that should be used by the direct mapping when
144  *	     the memory device is accessed
145  * @offset: absolute offset this direct mapping is pointing to
146  * @length: length in byte of this direct mapping
147  *
148  * These information are used by the controller specific implementation to know
149  * the portion of memory that is directly mapped and the spi_mem_op that should
150  * be used to access the device.
151  * A direct mapping is only valid for one direction (read or write) and this
152  * direction is directly encoded in the ->op_tmpl.data.dir field.
153  */
154 struct spi_mem_dirmap_info {
155 	struct spi_mem_op op_tmpl;
156 	u64 offset;
157 	u64 length;
158 };
159 
160 /**
161  * struct spi_mem_dirmap_desc - Direct mapping descriptor
162  * @mem: the SPI memory device this direct mapping is attached to
163  * @info: information passed at direct mapping creation time
164  * @nodirmap: set to 1 if the SPI controller does not implement
165  *	      ->mem_ops->dirmap_create() or when this function returned an
166  *	      error. If @nodirmap is true, all spi_mem_dirmap_{read,write}()
167  *	      calls will use spi_mem_exec_op() to access the memory. This is a
168  *	      degraded mode that allows spi_mem drivers to use the same code
169  *	      no matter whether the controller supports direct mapping or not
170  * @priv: field pointing to controller specific data
171  *
172  * Common part of a direct mapping descriptor. This object is created by
173  * spi_mem_dirmap_create() and controller implementation of ->create_dirmap()
174  * can create/attach direct mapping resources to the descriptor in the ->priv
175  * field.
176  */
177 struct spi_mem_dirmap_desc {
178 	struct spi_mem *mem;
179 	struct spi_mem_dirmap_info info;
180 	unsigned int nodirmap;
181 	void *priv;
182 };
183 
184 /**
185  * struct spi_mem - describes a SPI memory device
186  * @spi: the underlying SPI device
187  * @drvpriv: spi_mem_driver private data
188  * @name: name of the SPI memory device
189  *
190  * Extra information that describe the SPI memory device and may be needed by
191  * the controller to properly handle this device should be placed here.
192  *
193  * One example would be the device size since some controller expose their SPI
194  * mem devices through a io-mapped region.
195  */
196 struct spi_mem {
197 	struct spi_device *spi;
198 	void *drvpriv;
199 	const char *name;
200 };
201 
202 /**
203  * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
204  *				  device
205  * @mem: memory device
206  * @data: data to attach to the memory device
207  */
208 static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
209 {
210 	mem->drvpriv = data;
211 }
212 
213 /**
214  * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
215  *				  device
216  * @mem: memory device
217  *
218  * Return: the data attached to the mem device.
219  */
220 static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
221 {
222 	return mem->drvpriv;
223 }
224 
225 /**
226  * struct spi_controller_mem_ops - SPI memory operations
227  * @adjust_op_size: shrink the data xfer of an operation to match controller's
228  *		    limitations (can be alignment of max RX/TX size
229  *		    limitations)
230  * @supports_op: check if an operation is supported by the controller
231  * @exec_op: execute a SPI memory operation
232  * @get_name: get a custom name for the SPI mem device from the controller.
233  *	      This might be needed if the controller driver has been ported
234  *	      to use the SPI mem layer and a custom name is used to keep
235  *	      mtdparts compatible.
236  *	      Note that if the implementation of this function allocates memory
237  *	      dynamically, then it should do so with devm_xxx(), as we don't
238  *	      have a ->free_name() function.
239  * @dirmap_create: create a direct mapping descriptor that can later be used to
240  *		   access the memory device. This method is optional
241  * @dirmap_destroy: destroy a memory descriptor previous created by
242  *		    ->dirmap_create()
243  * @dirmap_read: read data from the memory device using the direct mapping
244  *		 created by ->dirmap_create(). The function can return less
245  *		 data than requested (for example when the request is crossing
246  *		 the currently mapped area), and the caller of
247  *		 spi_mem_dirmap_read() is responsible for calling it again in
248  *		 this case.
249  * @dirmap_write: write data to the memory device using the direct mapping
250  *		  created by ->dirmap_create(). The function can return less
251  *		  data than requested (for example when the request is crossing
252  *		  the currently mapped area), and the caller of
253  *		  spi_mem_dirmap_write() is responsible for calling it again in
254  *		  this case.
255  * @poll_status: poll memory device status until (status & mask) == match or
256  *               when the timeout has expired. It fills the data buffer with
257  *               the last status value.
258  *
259  * This interface should be implemented by SPI controllers providing an
260  * high-level interface to execute SPI memory operation, which is usually the
261  * case for QSPI controllers.
262  *
263  * Note on ->dirmap_{read,write}(): drivers should avoid accessing the direct
264  * mapping from the CPU because doing that can stall the CPU waiting for the
265  * SPI mem transaction to finish, and this will make real-time maintainers
266  * unhappy and might make your system less reactive. Instead, drivers should
267  * use DMA to access this direct mapping.
268  */
269 struct spi_controller_mem_ops {
270 	int (*adjust_op_size)(struct spi_mem *mem, struct spi_mem_op *op);
271 	bool (*supports_op)(struct spi_mem *mem,
272 			    const struct spi_mem_op *op);
273 	int (*exec_op)(struct spi_mem *mem,
274 		       const struct spi_mem_op *op);
275 	const char *(*get_name)(struct spi_mem *mem);
276 	int (*dirmap_create)(struct spi_mem_dirmap_desc *desc);
277 	void (*dirmap_destroy)(struct spi_mem_dirmap_desc *desc);
278 	ssize_t (*dirmap_read)(struct spi_mem_dirmap_desc *desc,
279 			       u64 offs, size_t len, void *buf);
280 	ssize_t (*dirmap_write)(struct spi_mem_dirmap_desc *desc,
281 				u64 offs, size_t len, const void *buf);
282 	int (*poll_status)(struct spi_mem *mem,
283 			   const struct spi_mem_op *op,
284 			   u16 mask, u16 match,
285 			   unsigned long initial_delay_us,
286 			   unsigned long polling_rate_us,
287 			   unsigned long timeout_ms);
288 };
289 
290 /**
291  * struct spi_controller_mem_caps - SPI memory controller capabilities
292  * @dtr: Supports DTR operations
293  * @ecc: Supports operations with error correction
294  */
295 struct spi_controller_mem_caps {
296 	bool dtr;
297 	bool ecc;
298 };
299 
300 #define spi_mem_controller_is_capable(ctlr, cap)	\
301 	((ctlr)->mem_caps && (ctlr)->mem_caps->cap)
302 
303 /**
304  * struct spi_mem_driver - SPI memory driver
305  * @spidrv: inherit from a SPI driver
306  * @probe: probe a SPI memory. Usually where detection/initialization takes
307  *	   place
308  * @remove: remove a SPI memory
309  * @shutdown: take appropriate action when the system is shutdown
310  *
311  * This is just a thin wrapper around a spi_driver. The core takes care of
312  * allocating the spi_mem object and forwarding the probe/remove/shutdown
313  * request to the spi_mem_driver. The reason we use this wrapper is because
314  * we might have to stuff more information into the spi_mem struct to let
315  * SPI controllers know more about the SPI memory they interact with, and
316  * having this intermediate layer allows us to do that without adding more
317  * useless fields to the spi_device object.
318  */
319 struct spi_mem_driver {
320 	struct spi_driver spidrv;
321 	int (*probe)(struct spi_mem *mem);
322 	int (*remove)(struct spi_mem *mem);
323 	void (*shutdown)(struct spi_mem *mem);
324 };
325 
326 #if IS_ENABLED(CONFIG_SPI_MEM)
327 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
328 				       const struct spi_mem_op *op,
329 				       struct sg_table *sg);
330 
331 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
332 					  const struct spi_mem_op *op,
333 					  struct sg_table *sg);
334 
335 bool spi_mem_default_supports_op(struct spi_mem *mem,
336 				 const struct spi_mem_op *op);
337 #else
338 static inline int
339 spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
340 				   const struct spi_mem_op *op,
341 				   struct sg_table *sg)
342 {
343 	return -ENOTSUPP;
344 }
345 
346 static inline void
347 spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
348 				     const struct spi_mem_op *op,
349 				     struct sg_table *sg)
350 {
351 }
352 
353 static inline
354 bool spi_mem_default_supports_op(struct spi_mem *mem,
355 				 const struct spi_mem_op *op)
356 {
357 	return false;
358 }
359 #endif /* CONFIG_SPI_MEM */
360 
361 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op);
362 
363 bool spi_mem_supports_op(struct spi_mem *mem,
364 			 const struct spi_mem_op *op);
365 
366 int spi_mem_exec_op(struct spi_mem *mem,
367 		    const struct spi_mem_op *op);
368 
369 const char *spi_mem_get_name(struct spi_mem *mem);
370 
371 struct spi_mem_dirmap_desc *
372 spi_mem_dirmap_create(struct spi_mem *mem,
373 		      const struct spi_mem_dirmap_info *info);
374 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc);
375 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
376 			    u64 offs, size_t len, void *buf);
377 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
378 			     u64 offs, size_t len, const void *buf);
379 struct spi_mem_dirmap_desc *
380 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem,
381 			   const struct spi_mem_dirmap_info *info);
382 void devm_spi_mem_dirmap_destroy(struct device *dev,
383 				 struct spi_mem_dirmap_desc *desc);
384 
385 int spi_mem_poll_status(struct spi_mem *mem,
386 			const struct spi_mem_op *op,
387 			u16 mask, u16 match,
388 			unsigned long initial_delay_us,
389 			unsigned long polling_delay_us,
390 			u16 timeout_ms);
391 
392 int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
393 				       struct module *owner);
394 
395 void spi_mem_driver_unregister(struct spi_mem_driver *drv);
396 
397 #define spi_mem_driver_register(__drv)                                  \
398 	spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
399 
400 #define module_spi_mem_driver(__drv)                                    \
401 	module_driver(__drv, spi_mem_driver_register,                   \
402 		      spi_mem_driver_unregister)
403 
404 #endif /* __LINUX_SPI_MEM_H */
405