xref: /openbmc/linux/drivers/spi/spi-mem.c (revision 93f5715e)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018 Exceet Electronics GmbH
4  * Copyright (C) 2018 Bootlin
5  *
6  * Author: Boris Brezillon <boris.brezillon@bootlin.com>
7  */
8 #include <linux/dmaengine.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/spi/spi.h>
11 #include <linux/spi/spi-mem.h>
12 
13 #include "internals.h"
14 
15 /**
16  * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a
17  *					  memory operation
18  * @ctlr: the SPI controller requesting this dma_map()
19  * @op: the memory operation containing the buffer to map
20  * @sgt: a pointer to a non-initialized sg_table that will be filled by this
21  *	 function
22  *
23  * Some controllers might want to do DMA on the data buffer embedded in @op.
24  * This helper prepares everything for you and provides a ready-to-use
25  * sg_table. This function is not intended to be called from spi drivers.
26  * Only SPI controller drivers should use it.
27  * Note that the caller must ensure the memory region pointed by
28  * op->data.buf.{in,out} is DMA-able before calling this function.
29  *
30  * Return: 0 in case of success, a negative error code otherwise.
31  */
32 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
33 				       const struct spi_mem_op *op,
34 				       struct sg_table *sgt)
35 {
36 	struct device *dmadev;
37 
38 	if (!op->data.nbytes)
39 		return -EINVAL;
40 
41 	if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
42 		dmadev = ctlr->dma_tx->device->dev;
43 	else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
44 		dmadev = ctlr->dma_rx->device->dev;
45 	else
46 		dmadev = ctlr->dev.parent;
47 
48 	if (!dmadev)
49 		return -EINVAL;
50 
51 	return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes,
52 			   op->data.dir == SPI_MEM_DATA_IN ?
53 			   DMA_FROM_DEVICE : DMA_TO_DEVICE);
54 }
55 EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data);
56 
57 /**
58  * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a
59  *					    memory operation
60  * @ctlr: the SPI controller requesting this dma_unmap()
61  * @op: the memory operation containing the buffer to unmap
62  * @sgt: a pointer to an sg_table previously initialized by
63  *	 spi_controller_dma_map_mem_op_data()
64  *
65  * Some controllers might want to do DMA on the data buffer embedded in @op.
66  * This helper prepares things so that the CPU can access the
67  * op->data.buf.{in,out} buffer again.
68  *
69  * This function is not intended to be called from SPI drivers. Only SPI
70  * controller drivers should use it.
71  *
72  * This function should be called after the DMA operation has finished and is
73  * only valid if the previous spi_controller_dma_map_mem_op_data() call
74  * returned 0.
75  *
76  * Return: 0 in case of success, a negative error code otherwise.
77  */
78 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
79 					  const struct spi_mem_op *op,
80 					  struct sg_table *sgt)
81 {
82 	struct device *dmadev;
83 
84 	if (!op->data.nbytes)
85 		return;
86 
87 	if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
88 		dmadev = ctlr->dma_tx->device->dev;
89 	else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
90 		dmadev = ctlr->dma_rx->device->dev;
91 	else
92 		dmadev = ctlr->dev.parent;
93 
94 	spi_unmap_buf(ctlr, dmadev, sgt,
95 		      op->data.dir == SPI_MEM_DATA_IN ?
96 		      DMA_FROM_DEVICE : DMA_TO_DEVICE);
97 }
98 EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data);
99 
100 static int spi_check_buswidth_req(struct spi_mem *mem, u8 buswidth, bool tx)
101 {
102 	u32 mode = mem->spi->mode;
103 
104 	switch (buswidth) {
105 	case 1:
106 		return 0;
107 
108 	case 2:
109 		if ((tx && (mode & (SPI_TX_DUAL | SPI_TX_QUAD))) ||
110 		    (!tx && (mode & (SPI_RX_DUAL | SPI_RX_QUAD))))
111 			return 0;
112 
113 		break;
114 
115 	case 4:
116 		if ((tx && (mode & SPI_TX_QUAD)) ||
117 		    (!tx && (mode & SPI_RX_QUAD)))
118 			return 0;
119 
120 		break;
121 
122 	default:
123 		break;
124 	}
125 
126 	return -ENOTSUPP;
127 }
128 
129 static bool spi_mem_default_supports_op(struct spi_mem *mem,
130 					const struct spi_mem_op *op)
131 {
132 	if (spi_check_buswidth_req(mem, op->cmd.buswidth, true))
133 		return false;
134 
135 	if (op->addr.nbytes &&
136 	    spi_check_buswidth_req(mem, op->addr.buswidth, true))
137 		return false;
138 
139 	if (op->dummy.nbytes &&
140 	    spi_check_buswidth_req(mem, op->dummy.buswidth, true))
141 		return false;
142 
143 	if (op->data.nbytes &&
144 	    spi_check_buswidth_req(mem, op->data.buswidth,
145 				   op->data.dir == SPI_MEM_DATA_OUT))
146 		return false;
147 
148 	return true;
149 }
150 EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
151 
152 /**
153  * spi_mem_supports_op() - Check if a memory device and the controller it is
154  *			   connected to support a specific memory operation
155  * @mem: the SPI memory
156  * @op: the memory operation to check
157  *
158  * Some controllers are only supporting Single or Dual IOs, others might only
159  * support specific opcodes, or it can even be that the controller and device
160  * both support Quad IOs but the hardware prevents you from using it because
161  * only 2 IO lines are connected.
162  *
163  * This function checks whether a specific operation is supported.
164  *
165  * Return: true if @op is supported, false otherwise.
166  */
167 bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
168 {
169 	struct spi_controller *ctlr = mem->spi->controller;
170 
171 	if (ctlr->mem_ops && ctlr->mem_ops->supports_op)
172 		return ctlr->mem_ops->supports_op(mem, op);
173 
174 	return spi_mem_default_supports_op(mem, op);
175 }
176 EXPORT_SYMBOL_GPL(spi_mem_supports_op);
177 
178 /**
179  * spi_mem_exec_op() - Execute a memory operation
180  * @mem: the SPI memory
181  * @op: the memory operation to execute
182  *
183  * Executes a memory operation.
184  *
185  * This function first checks that @op is supported and then tries to execute
186  * it.
187  *
188  * Return: 0 in case of success, a negative error code otherwise.
189  */
190 int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
191 {
192 	unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0;
193 	struct spi_controller *ctlr = mem->spi->controller;
194 	struct spi_transfer xfers[4] = { };
195 	struct spi_message msg;
196 	u8 *tmpbuf;
197 	int ret;
198 
199 	if (!spi_mem_supports_op(mem, op))
200 		return -ENOTSUPP;
201 
202 	if (ctlr->mem_ops) {
203 		/*
204 		 * Flush the message queue before executing our SPI memory
205 		 * operation to prevent preemption of regular SPI transfers.
206 		 */
207 		spi_flush_queue(ctlr);
208 
209 		if (ctlr->auto_runtime_pm) {
210 			ret = pm_runtime_get_sync(ctlr->dev.parent);
211 			if (ret < 0) {
212 				dev_err(&ctlr->dev,
213 					"Failed to power device: %d\n",
214 					ret);
215 				return ret;
216 			}
217 		}
218 
219 		mutex_lock(&ctlr->bus_lock_mutex);
220 		mutex_lock(&ctlr->io_mutex);
221 		ret = ctlr->mem_ops->exec_op(mem, op);
222 		mutex_unlock(&ctlr->io_mutex);
223 		mutex_unlock(&ctlr->bus_lock_mutex);
224 
225 		if (ctlr->auto_runtime_pm)
226 			pm_runtime_put(ctlr->dev.parent);
227 
228 		/*
229 		 * Some controllers only optimize specific paths (typically the
230 		 * read path) and expect the core to use the regular SPI
231 		 * interface in other cases.
232 		 */
233 		if (!ret || ret != -ENOTSUPP)
234 			return ret;
235 	}
236 
237 	tmpbufsize = sizeof(op->cmd.opcode) + op->addr.nbytes +
238 		     op->dummy.nbytes;
239 
240 	/*
241 	 * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so
242 	 * we're guaranteed that this buffer is DMA-able, as required by the
243 	 * SPI layer.
244 	 */
245 	tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
246 	if (!tmpbuf)
247 		return -ENOMEM;
248 
249 	spi_message_init(&msg);
250 
251 	tmpbuf[0] = op->cmd.opcode;
252 	xfers[xferpos].tx_buf = tmpbuf;
253 	xfers[xferpos].len = sizeof(op->cmd.opcode);
254 	xfers[xferpos].tx_nbits = op->cmd.buswidth;
255 	spi_message_add_tail(&xfers[xferpos], &msg);
256 	xferpos++;
257 	totalxferlen++;
258 
259 	if (op->addr.nbytes) {
260 		int i;
261 
262 		for (i = 0; i < op->addr.nbytes; i++)
263 			tmpbuf[i + 1] = op->addr.val >>
264 					(8 * (op->addr.nbytes - i - 1));
265 
266 		xfers[xferpos].tx_buf = tmpbuf + 1;
267 		xfers[xferpos].len = op->addr.nbytes;
268 		xfers[xferpos].tx_nbits = op->addr.buswidth;
269 		spi_message_add_tail(&xfers[xferpos], &msg);
270 		xferpos++;
271 		totalxferlen += op->addr.nbytes;
272 	}
273 
274 	if (op->dummy.nbytes) {
275 		memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
276 		xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
277 		xfers[xferpos].len = op->dummy.nbytes;
278 		xfers[xferpos].tx_nbits = op->dummy.buswidth;
279 		spi_message_add_tail(&xfers[xferpos], &msg);
280 		xferpos++;
281 		totalxferlen += op->dummy.nbytes;
282 	}
283 
284 	if (op->data.nbytes) {
285 		if (op->data.dir == SPI_MEM_DATA_IN) {
286 			xfers[xferpos].rx_buf = op->data.buf.in;
287 			xfers[xferpos].rx_nbits = op->data.buswidth;
288 		} else {
289 			xfers[xferpos].tx_buf = op->data.buf.out;
290 			xfers[xferpos].tx_nbits = op->data.buswidth;
291 		}
292 
293 		xfers[xferpos].len = op->data.nbytes;
294 		spi_message_add_tail(&xfers[xferpos], &msg);
295 		xferpos++;
296 		totalxferlen += op->data.nbytes;
297 	}
298 
299 	ret = spi_sync(mem->spi, &msg);
300 
301 	kfree(tmpbuf);
302 
303 	if (ret)
304 		return ret;
305 
306 	if (msg.actual_length != totalxferlen)
307 		return -EIO;
308 
309 	return 0;
310 }
311 EXPORT_SYMBOL_GPL(spi_mem_exec_op);
312 
313 /**
314  * spi_mem_get_name() - Return the SPI mem device name to be used by the
315  *			upper layer if necessary
316  * @mem: the SPI memory
317  *
318  * This function allows SPI mem users to retrieve the SPI mem device name.
319  * It is useful if the upper layer needs to expose a custom name for
320  * compatibility reasons.
321  *
322  * Return: a string containing the name of the memory device to be used
323  *	   by the SPI mem user
324  */
325 const char *spi_mem_get_name(struct spi_mem *mem)
326 {
327 	return mem->name;
328 }
329 EXPORT_SYMBOL_GPL(spi_mem_get_name);
330 
331 /**
332  * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
333  *			      match controller limitations
334  * @mem: the SPI memory
335  * @op: the operation to adjust
336  *
337  * Some controllers have FIFO limitations and must split a data transfer
338  * operation into multiple ones, others require a specific alignment for
339  * optimized accesses. This function allows SPI mem drivers to split a single
340  * operation into multiple sub-operations when required.
341  *
342  * Return: a negative error code if the controller can't properly adjust @op,
343  *	   0 otherwise. Note that @op->data.nbytes will be updated if @op
344  *	   can't be handled in a single step.
345  */
346 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
347 {
348 	struct spi_controller *ctlr = mem->spi->controller;
349 
350 	if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
351 		return ctlr->mem_ops->adjust_op_size(mem, op);
352 
353 	return 0;
354 }
355 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
356 
357 static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
358 {
359 	return container_of(drv, struct spi_mem_driver, spidrv.driver);
360 }
361 
362 static int spi_mem_probe(struct spi_device *spi)
363 {
364 	struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
365 	struct spi_controller *ctlr = spi->controller;
366 	struct spi_mem *mem;
367 
368 	mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
369 	if (!mem)
370 		return -ENOMEM;
371 
372 	mem->spi = spi;
373 
374 	if (ctlr->mem_ops && ctlr->mem_ops->get_name)
375 		mem->name = ctlr->mem_ops->get_name(mem);
376 	else
377 		mem->name = dev_name(&spi->dev);
378 
379 	if (IS_ERR_OR_NULL(mem->name))
380 		return PTR_ERR(mem->name);
381 
382 	spi_set_drvdata(spi, mem);
383 
384 	return memdrv->probe(mem);
385 }
386 
387 static int spi_mem_remove(struct spi_device *spi)
388 {
389 	struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
390 	struct spi_mem *mem = spi_get_drvdata(spi);
391 
392 	if (memdrv->remove)
393 		return memdrv->remove(mem);
394 
395 	return 0;
396 }
397 
398 static void spi_mem_shutdown(struct spi_device *spi)
399 {
400 	struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
401 	struct spi_mem *mem = spi_get_drvdata(spi);
402 
403 	if (memdrv->shutdown)
404 		memdrv->shutdown(mem);
405 }
406 
407 /**
408  * spi_mem_driver_register_with_owner() - Register a SPI memory driver
409  * @memdrv: the SPI memory driver to register
410  * @owner: the owner of this driver
411  *
412  * Registers a SPI memory driver.
413  *
414  * Return: 0 in case of success, a negative error core otherwise.
415  */
416 
417 int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
418 				       struct module *owner)
419 {
420 	memdrv->spidrv.probe = spi_mem_probe;
421 	memdrv->spidrv.remove = spi_mem_remove;
422 	memdrv->spidrv.shutdown = spi_mem_shutdown;
423 
424 	return __spi_register_driver(owner, &memdrv->spidrv);
425 }
426 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
427 
428 /**
429  * spi_mem_driver_unregister_with_owner() - Unregister a SPI memory driver
430  * @memdrv: the SPI memory driver to unregister
431  *
432  * Unregisters a SPI memory driver.
433  */
434 void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
435 {
436 	spi_unregister_driver(&memdrv->spidrv);
437 }
438 EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);
439