blob: b8cf8dd76b66cabcc830a542dd79e606b3589d99 [file] [log] [blame]
Boris Brezillon32473fe2018-08-16 17:30:11 +02001// 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
9#ifndef __UBOOT__
Simon Glassd66c5f72020-02-03 07:36:15 -070010#include <dm/devres.h>
Boris Brezillon32473fe2018-08-16 17:30:11 +020011#include <linux/dmaengine.h>
12#include <linux/pm_runtime.h>
13#include "internals.h"
14#else
15#include <spi.h>
16#include <spi-mem.h>
17#endif
18
19#ifndef __UBOOT__
20/**
21 * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a
22 * memory operation
23 * @ctlr: the SPI controller requesting this dma_map()
24 * @op: the memory operation containing the buffer to map
25 * @sgt: a pointer to a non-initialized sg_table that will be filled by this
26 * function
27 *
28 * Some controllers might want to do DMA on the data buffer embedded in @op.
29 * This helper prepares everything for you and provides a ready-to-use
30 * sg_table. This function is not intended to be called from spi drivers.
31 * Only SPI controller drivers should use it.
32 * Note that the caller must ensure the memory region pointed by
33 * op->data.buf.{in,out} is DMA-able before calling this function.
34 *
35 * Return: 0 in case of success, a negative error code otherwise.
36 */
37int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
38 const struct spi_mem_op *op,
39 struct sg_table *sgt)
40{
41 struct device *dmadev;
42
43 if (!op->data.nbytes)
44 return -EINVAL;
45
46 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
47 dmadev = ctlr->dma_tx->device->dev;
48 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
49 dmadev = ctlr->dma_rx->device->dev;
50 else
51 dmadev = ctlr->dev.parent;
52
53 if (!dmadev)
54 return -EINVAL;
55
56 return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes,
57 op->data.dir == SPI_MEM_DATA_IN ?
58 DMA_FROM_DEVICE : DMA_TO_DEVICE);
59}
60EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data);
61
62/**
63 * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a
64 * memory operation
65 * @ctlr: the SPI controller requesting this dma_unmap()
66 * @op: the memory operation containing the buffer to unmap
67 * @sgt: a pointer to an sg_table previously initialized by
68 * spi_controller_dma_map_mem_op_data()
69 *
70 * Some controllers might want to do DMA on the data buffer embedded in @op.
71 * This helper prepares things so that the CPU can access the
72 * op->data.buf.{in,out} buffer again.
73 *
74 * This function is not intended to be called from SPI drivers. Only SPI
75 * controller drivers should use it.
76 *
77 * This function should be called after the DMA operation has finished and is
78 * only valid if the previous spi_controller_dma_map_mem_op_data() call
79 * returned 0.
80 *
81 * Return: 0 in case of success, a negative error code otherwise.
82 */
83void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
84 const struct spi_mem_op *op,
85 struct sg_table *sgt)
86{
87 struct device *dmadev;
88
89 if (!op->data.nbytes)
90 return;
91
92 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
93 dmadev = ctlr->dma_tx->device->dev;
94 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
95 dmadev = ctlr->dma_rx->device->dev;
96 else
97 dmadev = ctlr->dev.parent;
98
99 spi_unmap_buf(ctlr, dmadev, sgt,
100 op->data.dir == SPI_MEM_DATA_IN ?
101 DMA_FROM_DEVICE : DMA_TO_DEVICE);
102}
103EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data);
104#endif /* __UBOOT__ */
105
106static int spi_check_buswidth_req(struct spi_slave *slave, u8 buswidth, bool tx)
107{
108 u32 mode = slave->mode;
109
110 switch (buswidth) {
111 case 1:
112 return 0;
113
114 case 2:
115 if ((tx && (mode & (SPI_TX_DUAL | SPI_TX_QUAD))) ||
116 (!tx && (mode & (SPI_RX_DUAL | SPI_RX_QUAD))))
117 return 0;
118
119 break;
120
121 case 4:
122 if ((tx && (mode & SPI_TX_QUAD)) ||
123 (!tx && (mode & SPI_RX_QUAD)))
124 return 0;
125
126 break;
Vignesh Raghavendrac063ee32019-12-05 15:46:05 +0530127 case 8:
128 if ((tx && (mode & SPI_TX_OCTAL)) ||
129 (!tx && (mode & SPI_RX_OCTAL)))
130 return 0;
131
132 break;
Boris Brezillon32473fe2018-08-16 17:30:11 +0200133
134 default:
135 break;
136 }
137
138 return -ENOTSUPP;
139}
140
141bool spi_mem_default_supports_op(struct spi_slave *slave,
142 const struct spi_mem_op *op)
143{
144 if (spi_check_buswidth_req(slave, op->cmd.buswidth, true))
145 return false;
146
147 if (op->addr.nbytes &&
148 spi_check_buswidth_req(slave, op->addr.buswidth, true))
149 return false;
150
151 if (op->dummy.nbytes &&
152 spi_check_buswidth_req(slave, op->dummy.buswidth, true))
153 return false;
154
155 if (op->data.nbytes &&
156 spi_check_buswidth_req(slave, op->data.buswidth,
157 op->data.dir == SPI_MEM_DATA_OUT))
158 return false;
159
160 return true;
161}
162EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
163
164/**
165 * spi_mem_supports_op() - Check if a memory device and the controller it is
166 * connected to support a specific memory operation
167 * @slave: the SPI device
168 * @op: the memory operation to check
169 *
170 * Some controllers are only supporting Single or Dual IOs, others might only
171 * support specific opcodes, or it can even be that the controller and device
172 * both support Quad IOs but the hardware prevents you from using it because
173 * only 2 IO lines are connected.
174 *
175 * This function checks whether a specific operation is supported.
176 *
177 * Return: true if @op is supported, false otherwise.
178 */
179bool spi_mem_supports_op(struct spi_slave *slave,
180 const struct spi_mem_op *op)
181{
182 struct udevice *bus = slave->dev->parent;
183 struct dm_spi_ops *ops = spi_get_ops(bus);
184
185 if (ops->mem_ops && ops->mem_ops->supports_op)
186 return ops->mem_ops->supports_op(slave, op);
187
188 return spi_mem_default_supports_op(slave, op);
189}
190EXPORT_SYMBOL_GPL(spi_mem_supports_op);
191
192/**
193 * spi_mem_exec_op() - Execute a memory operation
194 * @slave: the SPI device
195 * @op: the memory operation to execute
196 *
197 * Executes a memory operation.
198 *
199 * This function first checks that @op is supported and then tries to execute
200 * it.
201 *
202 * Return: 0 in case of success, a negative error code otherwise.
203 */
204int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
205{
206 struct udevice *bus = slave->dev->parent;
207 struct dm_spi_ops *ops = spi_get_ops(bus);
208 unsigned int pos = 0;
209 const u8 *tx_buf = NULL;
210 u8 *rx_buf = NULL;
Boris Brezillon32473fe2018-08-16 17:30:11 +0200211 int op_len;
212 u32 flag;
213 int ret;
214 int i;
215
216 if (!spi_mem_supports_op(slave, op))
217 return -ENOTSUPP;
218
Vignesh Rcae870e2019-02-05 11:29:14 +0530219 ret = spi_claim_bus(slave);
220 if (ret < 0)
221 return ret;
222
Bernhard Messerklingere8c3d1b2019-03-26 10:01:24 +0100223 if (ops->mem_ops && ops->mem_ops->exec_op) {
Boris Brezillon32473fe2018-08-16 17:30:11 +0200224#ifndef __UBOOT__
225 /*
226 * Flush the message queue before executing our SPI memory
227 * operation to prevent preemption of regular SPI transfers.
228 */
229 spi_flush_queue(ctlr);
230
231 if (ctlr->auto_runtime_pm) {
232 ret = pm_runtime_get_sync(ctlr->dev.parent);
233 if (ret < 0) {
234 dev_err(&ctlr->dev,
235 "Failed to power device: %d\n",
236 ret);
237 return ret;
238 }
239 }
240
241 mutex_lock(&ctlr->bus_lock_mutex);
242 mutex_lock(&ctlr->io_mutex);
243#endif
244 ret = ops->mem_ops->exec_op(slave, op);
Vignesh Rcae870e2019-02-05 11:29:14 +0530245
Boris Brezillon32473fe2018-08-16 17:30:11 +0200246#ifndef __UBOOT__
247 mutex_unlock(&ctlr->io_mutex);
248 mutex_unlock(&ctlr->bus_lock_mutex);
249
250 if (ctlr->auto_runtime_pm)
251 pm_runtime_put(ctlr->dev.parent);
252#endif
253
254 /*
255 * Some controllers only optimize specific paths (typically the
256 * read path) and expect the core to use the regular SPI
257 * interface in other cases.
258 */
Vignesh Rcae870e2019-02-05 11:29:14 +0530259 if (!ret || ret != -ENOTSUPP) {
260 spi_release_bus(slave);
Boris Brezillon32473fe2018-08-16 17:30:11 +0200261 return ret;
Vignesh Rcae870e2019-02-05 11:29:14 +0530262 }
Boris Brezillon32473fe2018-08-16 17:30:11 +0200263 }
264
265#ifndef __UBOOT__
266 tmpbufsize = sizeof(op->cmd.opcode) + op->addr.nbytes +
267 op->dummy.nbytes;
268
269 /*
270 * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so
271 * we're guaranteed that this buffer is DMA-able, as required by the
272 * SPI layer.
273 */
274 tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
275 if (!tmpbuf)
276 return -ENOMEM;
277
278 spi_message_init(&msg);
279
280 tmpbuf[0] = op->cmd.opcode;
281 xfers[xferpos].tx_buf = tmpbuf;
282 xfers[xferpos].len = sizeof(op->cmd.opcode);
283 xfers[xferpos].tx_nbits = op->cmd.buswidth;
284 spi_message_add_tail(&xfers[xferpos], &msg);
285 xferpos++;
286 totalxferlen++;
287
288 if (op->addr.nbytes) {
289 int i;
290
291 for (i = 0; i < op->addr.nbytes; i++)
292 tmpbuf[i + 1] = op->addr.val >>
293 (8 * (op->addr.nbytes - i - 1));
294
295 xfers[xferpos].tx_buf = tmpbuf + 1;
296 xfers[xferpos].len = op->addr.nbytes;
297 xfers[xferpos].tx_nbits = op->addr.buswidth;
298 spi_message_add_tail(&xfers[xferpos], &msg);
299 xferpos++;
300 totalxferlen += op->addr.nbytes;
301 }
302
303 if (op->dummy.nbytes) {
304 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
305 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
306 xfers[xferpos].len = op->dummy.nbytes;
307 xfers[xferpos].tx_nbits = op->dummy.buswidth;
308 spi_message_add_tail(&xfers[xferpos], &msg);
309 xferpos++;
310 totalxferlen += op->dummy.nbytes;
311 }
312
313 if (op->data.nbytes) {
314 if (op->data.dir == SPI_MEM_DATA_IN) {
315 xfers[xferpos].rx_buf = op->data.buf.in;
316 xfers[xferpos].rx_nbits = op->data.buswidth;
317 } else {
318 xfers[xferpos].tx_buf = op->data.buf.out;
319 xfers[xferpos].tx_nbits = op->data.buswidth;
320 }
321
322 xfers[xferpos].len = op->data.nbytes;
323 spi_message_add_tail(&xfers[xferpos], &msg);
324 xferpos++;
325 totalxferlen += op->data.nbytes;
326 }
327
328 ret = spi_sync(slave, &msg);
329
330 kfree(tmpbuf);
331
332 if (ret)
333 return ret;
334
335 if (msg.actual_length != totalxferlen)
336 return -EIO;
337#else
338
Boris Brezillon32473fe2018-08-16 17:30:11 +0200339 if (op->data.nbytes) {
340 if (op->data.dir == SPI_MEM_DATA_IN)
341 rx_buf = op->data.buf.in;
342 else
343 tx_buf = op->data.buf.out;
344 }
345
346 op_len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
Simon Glass6d0d9912019-05-18 11:59:54 -0600347
348 /*
349 * Avoid using malloc() here so that we can use this code in SPL where
350 * simple malloc may be used. That implementation does not allow free()
351 * so repeated calls to this code can exhaust the space.
352 *
353 * The value of op_len is small, since it does not include the actual
354 * data being sent, only the op-code and address. In fact, it should be
355 * possible to just use a small fixed value here instead of op_len.
356 */
357 u8 op_buf[op_len];
Boris Brezillon32473fe2018-08-16 17:30:11 +0200358
Boris Brezillon32473fe2018-08-16 17:30:11 +0200359 op_buf[pos++] = op->cmd.opcode;
360
361 if (op->addr.nbytes) {
362 for (i = 0; i < op->addr.nbytes; i++)
363 op_buf[pos + i] = op->addr.val >>
364 (8 * (op->addr.nbytes - i - 1));
365
366 pos += op->addr.nbytes;
367 }
368
369 if (op->dummy.nbytes)
370 memset(op_buf + pos, 0xff, op->dummy.nbytes);
371
372 /* 1st transfer: opcode + address + dummy cycles */
373 flag = SPI_XFER_BEGIN;
374 /* Make sure to set END bit if no tx or rx data messages follow */
375 if (!tx_buf && !rx_buf)
376 flag |= SPI_XFER_END;
377
378 ret = spi_xfer(slave, op_len * 8, op_buf, NULL, flag);
379 if (ret)
380 return ret;
381
382 /* 2nd transfer: rx or tx data path */
383 if (tx_buf || rx_buf) {
384 ret = spi_xfer(slave, op->data.nbytes * 8, tx_buf,
385 rx_buf, SPI_XFER_END);
386 if (ret)
387 return ret;
388 }
389
390 spi_release_bus(slave);
391
392 for (i = 0; i < pos; i++)
393 debug("%02x ", op_buf[i]);
394 debug("| [%dB %s] ",
395 tx_buf || rx_buf ? op->data.nbytes : 0,
396 tx_buf || rx_buf ? (tx_buf ? "out" : "in") : "-");
397 for (i = 0; i < op->data.nbytes; i++)
398 debug("%02x ", tx_buf ? tx_buf[i] : rx_buf[i]);
399 debug("[ret %d]\n", ret);
400
Boris Brezillon32473fe2018-08-16 17:30:11 +0200401 if (ret < 0)
402 return ret;
403#endif /* __UBOOT__ */
404
405 return 0;
406}
407EXPORT_SYMBOL_GPL(spi_mem_exec_op);
408
409/**
410 * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
411 * match controller limitations
412 * @slave: the SPI device
413 * @op: the operation to adjust
414 *
415 * Some controllers have FIFO limitations and must split a data transfer
416 * operation into multiple ones, others require a specific alignment for
417 * optimized accesses. This function allows SPI mem drivers to split a single
418 * operation into multiple sub-operations when required.
419 *
420 * Return: a negative error code if the controller can't properly adjust @op,
421 * 0 otherwise. Note that @op->data.nbytes will be updated if @op
422 * can't be handled in a single step.
423 */
424int spi_mem_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op)
425{
426 struct udevice *bus = slave->dev->parent;
427 struct dm_spi_ops *ops = spi_get_ops(bus);
428
429 if (ops->mem_ops && ops->mem_ops->adjust_op_size)
430 return ops->mem_ops->adjust_op_size(slave, op);
431
Vignesh Rba3691f2019-02-05 11:29:13 +0530432 if (!ops->mem_ops || !ops->mem_ops->exec_op) {
433 unsigned int len;
434
435 len = sizeof(op->cmd.opcode) + op->addr.nbytes +
436 op->dummy.nbytes;
437 if (slave->max_write_size && len > slave->max_write_size)
438 return -EINVAL;
439
Ye Li3858d522019-07-10 09:23:51 +0000440 if (op->data.dir == SPI_MEM_DATA_IN) {
441 if (slave->max_read_size)
442 op->data.nbytes = min(op->data.nbytes,
Vignesh Rba3691f2019-02-05 11:29:13 +0530443 slave->max_read_size);
Ye Li3858d522019-07-10 09:23:51 +0000444 } else if (slave->max_write_size) {
Vignesh Rba3691f2019-02-05 11:29:13 +0530445 op->data.nbytes = min(op->data.nbytes,
446 slave->max_write_size - len);
Ye Li3858d522019-07-10 09:23:51 +0000447 }
Vignesh Rba3691f2019-02-05 11:29:13 +0530448
449 if (!op->data.nbytes)
450 return -EINVAL;
451 }
452
Boris Brezillon32473fe2018-08-16 17:30:11 +0200453 return 0;
454}
455EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
456
457#ifndef __UBOOT__
458static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
459{
460 return container_of(drv, struct spi_mem_driver, spidrv.driver);
461}
462
463static int spi_mem_probe(struct spi_device *spi)
464{
465 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
466 struct spi_mem *mem;
467
468 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
469 if (!mem)
470 return -ENOMEM;
471
472 mem->spi = spi;
473 spi_set_drvdata(spi, mem);
474
475 return memdrv->probe(mem);
476}
477
478static int spi_mem_remove(struct spi_device *spi)
479{
480 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
481 struct spi_mem *mem = spi_get_drvdata(spi);
482
483 if (memdrv->remove)
484 return memdrv->remove(mem);
485
486 return 0;
487}
488
489static void spi_mem_shutdown(struct spi_device *spi)
490{
491 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
492 struct spi_mem *mem = spi_get_drvdata(spi);
493
494 if (memdrv->shutdown)
495 memdrv->shutdown(mem);
496}
497
498/**
499 * spi_mem_driver_register_with_owner() - Register a SPI memory driver
500 * @memdrv: the SPI memory driver to register
501 * @owner: the owner of this driver
502 *
503 * Registers a SPI memory driver.
504 *
505 * Return: 0 in case of success, a negative error core otherwise.
506 */
507
508int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
509 struct module *owner)
510{
511 memdrv->spidrv.probe = spi_mem_probe;
512 memdrv->spidrv.remove = spi_mem_remove;
513 memdrv->spidrv.shutdown = spi_mem_shutdown;
514
515 return __spi_register_driver(owner, &memdrv->spidrv);
516}
517EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
518
519/**
520 * spi_mem_driver_unregister_with_owner() - Unregister a SPI memory driver
521 * @memdrv: the SPI memory driver to unregister
522 *
523 * Unregisters a SPI memory driver.
524 */
525void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
526{
527 spi_unregister_driver(&memdrv->spidrv);
528}
529EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);
530#endif /* __UBOOT__ */