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