blob: be563f0dd03a2abe2b28761b4f4435de6cd61ded [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mika Westerberg59288082013-01-22 12:26:29 +02002/*
3 * PXA2xx SPI DMA engine support.
4 *
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03005 * Copyright (C) 2013, 2021 Intel Corporation
Mika Westerberg59288082013-01-22 12:26:29 +02006 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
Mika Westerberg59288082013-01-22 12:26:29 +02007 */
8
Mika Westerberg59288082013-01-22 12:26:29 +02009#include <linux/device.h>
10#include <linux/dma-mapping.h>
11#include <linux/dmaengine.h>
Mika Westerberg59288082013-01-22 12:26:29 +020012#include <linux/scatterlist.h>
13#include <linux/sizes.h>
Andy Shevchenko0e476872021-04-23 21:24:31 +030014
Mika Westerberg59288082013-01-22 12:26:29 +020015#include <linux/spi/pxa2xx_spi.h>
Andy Shevchenko0e476872021-04-23 21:24:31 +030016#include <linux/spi/spi.h>
Mika Westerberg59288082013-01-22 12:26:29 +020017
18#include "spi-pxa2xx.h"
19
Mika Westerberg59288082013-01-22 12:26:29 +020020static void pxa2xx_spi_dma_transfer_complete(struct driver_data *drv_data,
21 bool error)
22{
Lubomir Rintel51eea522019-01-16 16:13:31 +010023 struct spi_message *msg = drv_data->controller->cur_msg;
Mika Westerberg59288082013-01-22 12:26:29 +020024
25 /*
26 * It is possible that one CPU is handling ROR interrupt and other
27 * just gets DMA completion. Calling pump_transfers() twice for the
28 * same transfer leads to problems thus we prevent concurrent calls
Andy Shevchenko8083d6b2021-05-17 17:03:49 +030029 * by using dma_running.
Mika Westerberg59288082013-01-22 12:26:29 +020030 */
31 if (atomic_dec_and_test(&drv_data->dma_running)) {
Mika Westerberg59288082013-01-22 12:26:29 +020032 /*
33 * If the other CPU is still handling the ROR interrupt we
34 * might not know about the error yet. So we re-check the
35 * ROR bit here before we clear the status register.
36 */
Andy Shevchenko6d380132021-05-10 15:41:32 +030037 if (!error)
38 error = read_SSSR_bits(drv_data, drv_data->mask_sr) & SSSR_ROR;
Mika Westerberg59288082013-01-22 12:26:29 +020039
40 /* Clear status & disable interrupts */
Andy Shevchenko42c80cd2021-05-10 15:41:31 +030041 clear_SSCR1_bits(drv_data, drv_data->dma_cr1);
Mika Westerberg59288082013-01-22 12:26:29 +020042 write_SSSR_CS(drv_data, drv_data->clear_sr);
43 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +020044 pxa2xx_spi_write(drv_data, SSTO, 0);
Mika Westerberg59288082013-01-22 12:26:29 +020045
Jarkko Nikulad5898e12018-04-17 17:20:02 +030046 if (error) {
Mika Westerberg59288082013-01-22 12:26:29 +020047 /* In case we got an error we disable the SSP now */
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +030048 pxa_ssp_disable(drv_data->ssp);
Jarkko Nikulad5898e12018-04-17 17:20:02 +030049 msg->status = -EIO;
Mika Westerberg59288082013-01-22 12:26:29 +020050 }
51
Lubomir Rintel51eea522019-01-16 16:13:31 +010052 spi_finalize_current_transfer(drv_data->controller);
Mika Westerberg59288082013-01-22 12:26:29 +020053 }
54}
55
56static void pxa2xx_spi_dma_callback(void *data)
57{
58 pxa2xx_spi_dma_transfer_complete(data, false);
59}
60
61static struct dma_async_tx_descriptor *
62pxa2xx_spi_dma_prepare_one(struct driver_data *drv_data,
Jarkko Nikulad5898e12018-04-17 17:20:02 +030063 enum dma_transfer_direction dir,
64 struct spi_transfer *xfer)
Mika Westerberg59288082013-01-22 12:26:29 +020065{
Jarkko Nikula96579a42016-09-07 17:04:07 +030066 struct chip_data *chip =
Lubomir Rintel51eea522019-01-16 16:13:31 +010067 spi_get_ctldata(drv_data->controller->cur_msg->spi);
Mika Westerberg59288082013-01-22 12:26:29 +020068 enum dma_slave_buswidth width;
69 struct dma_slave_config cfg;
70 struct dma_chan *chan;
71 struct sg_table *sgt;
Jarkko Nikulab6ced292016-06-21 13:21:34 +030072 int ret;
Mika Westerberg59288082013-01-22 12:26:29 +020073
74 switch (drv_data->n_bytes) {
75 case 1:
76 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
77 break;
78 case 2:
79 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
80 break;
81 default:
82 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
83 break;
84 }
85
86 memset(&cfg, 0, sizeof(cfg));
87 cfg.direction = dir;
88
89 if (dir == DMA_MEM_TO_DEV) {
Andy Shevchenko9e43c9a82021-04-23 21:24:29 +030090 cfg.dst_addr = drv_data->ssp->phys_base + SSDR;
Mika Westerberg59288082013-01-22 12:26:29 +020091 cfg.dst_addr_width = width;
92 cfg.dst_maxburst = chip->dma_burst_size;
Mika Westerberg59288082013-01-22 12:26:29 +020093
Jarkko Nikulab6ced292016-06-21 13:21:34 +030094 sgt = &xfer->tx_sg;
Lubomir Rintel51eea522019-01-16 16:13:31 +010095 chan = drv_data->controller->dma_tx;
Mika Westerberg59288082013-01-22 12:26:29 +020096 } else {
Andy Shevchenko9e43c9a82021-04-23 21:24:29 +030097 cfg.src_addr = drv_data->ssp->phys_base + SSDR;
Mika Westerberg59288082013-01-22 12:26:29 +020098 cfg.src_addr_width = width;
99 cfg.src_maxburst = chip->dma_burst_size;
Mika Westerberg59288082013-01-22 12:26:29 +0200100
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300101 sgt = &xfer->rx_sg;
Lubomir Rintel51eea522019-01-16 16:13:31 +0100102 chan = drv_data->controller->dma_rx;
Mika Westerberg59288082013-01-22 12:26:29 +0200103 }
104
105 ret = dmaengine_slave_config(chan, &cfg);
106 if (ret) {
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300107 dev_warn(drv_data->ssp->dev, "DMA slave config failed\n");
Mika Westerberg59288082013-01-22 12:26:29 +0200108 return NULL;
109 }
110
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300111 return dmaengine_prep_slave_sg(chan, sgt->sgl, sgt->nents, dir,
Mika Westerberg59288082013-01-22 12:26:29 +0200112 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
113}
114
Mika Westerberg59288082013-01-22 12:26:29 +0200115irqreturn_t pxa2xx_spi_dma_transfer(struct driver_data *drv_data)
116{
117 u32 status;
118
Andy Shevchenko6d380132021-05-10 15:41:32 +0300119 status = read_SSSR_bits(drv_data, drv_data->mask_sr);
Mika Westerberg59288082013-01-22 12:26:29 +0200120 if (status & SSSR_ROR) {
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300121 dev_err(drv_data->ssp->dev, "FIFO overrun\n");
Mika Westerberg59288082013-01-22 12:26:29 +0200122
Lubomir Rintel51eea522019-01-16 16:13:31 +0100123 dmaengine_terminate_async(drv_data->controller->dma_rx);
124 dmaengine_terminate_async(drv_data->controller->dma_tx);
Mika Westerberg59288082013-01-22 12:26:29 +0200125
126 pxa2xx_spi_dma_transfer_complete(drv_data, true);
127 return IRQ_HANDLED;
128 }
129
130 return IRQ_NONE;
131}
132
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300133int pxa2xx_spi_dma_prepare(struct driver_data *drv_data,
134 struct spi_transfer *xfer)
Mika Westerberg59288082013-01-22 12:26:29 +0200135{
136 struct dma_async_tx_descriptor *tx_desc, *rx_desc;
Jarkko Nikulabffc9672016-09-07 17:04:05 +0300137 int err;
Mika Westerberg59288082013-01-22 12:26:29 +0200138
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300139 tx_desc = pxa2xx_spi_dma_prepare_one(drv_data, DMA_MEM_TO_DEV, xfer);
Mika Westerberg59288082013-01-22 12:26:29 +0200140 if (!tx_desc) {
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300141 dev_err(drv_data->ssp->dev, "failed to get DMA TX descriptor\n");
Andy Shevchenko7d1f1bf2016-03-24 15:35:42 +0200142 err = -EBUSY;
143 goto err_tx;
Mika Westerberg59288082013-01-22 12:26:29 +0200144 }
145
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300146 rx_desc = pxa2xx_spi_dma_prepare_one(drv_data, DMA_DEV_TO_MEM, xfer);
Mika Westerberg59288082013-01-22 12:26:29 +0200147 if (!rx_desc) {
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300148 dev_err(drv_data->ssp->dev, "failed to get DMA RX descriptor\n");
Andy Shevchenko7d1f1bf2016-03-24 15:35:42 +0200149 err = -EBUSY;
150 goto err_rx;
Mika Westerberg59288082013-01-22 12:26:29 +0200151 }
152
153 /* We are ready when RX completes */
154 rx_desc->callback = pxa2xx_spi_dma_callback;
155 rx_desc->callback_param = drv_data;
156
157 dmaengine_submit(rx_desc);
158 dmaengine_submit(tx_desc);
159 return 0;
Andy Shevchenko7d1f1bf2016-03-24 15:35:42 +0200160
161err_rx:
Lubomir Rintel51eea522019-01-16 16:13:31 +0100162 dmaengine_terminate_async(drv_data->controller->dma_tx);
Andy Shevchenko7d1f1bf2016-03-24 15:35:42 +0200163err_tx:
Andy Shevchenko7d1f1bf2016-03-24 15:35:42 +0200164 return err;
Mika Westerberg59288082013-01-22 12:26:29 +0200165}
166
167void pxa2xx_spi_dma_start(struct driver_data *drv_data)
168{
Lubomir Rintel51eea522019-01-16 16:13:31 +0100169 dma_async_issue_pending(drv_data->controller->dma_rx);
170 dma_async_issue_pending(drv_data->controller->dma_tx);
Mika Westerberg59288082013-01-22 12:26:29 +0200171
172 atomic_set(&drv_data->dma_running, 1);
173}
174
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300175void pxa2xx_spi_dma_stop(struct driver_data *drv_data)
176{
177 atomic_set(&drv_data->dma_running, 0);
Lubomir Rintel51eea522019-01-16 16:13:31 +0100178 dmaengine_terminate_sync(drv_data->controller->dma_rx);
179 dmaengine_terminate_sync(drv_data->controller->dma_tx);
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300180}
181
Mika Westerberg59288082013-01-22 12:26:29 +0200182int pxa2xx_spi_dma_setup(struct driver_data *drv_data)
183{
Lubomir Rintel51eea522019-01-16 16:13:31 +0100184 struct pxa2xx_spi_controller *pdata = drv_data->controller_info;
Lubomir Rintel51eea522019-01-16 16:13:31 +0100185 struct spi_controller *controller = drv_data->controller;
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300186 struct device *dev = drv_data->ssp->dev;
Mika Westerberg59288082013-01-22 12:26:29 +0200187 dma_cap_mask_t mask;
188
189 dma_cap_zero(mask);
190 dma_cap_set(DMA_SLAVE, mask);
191
Lubomir Rintel51eea522019-01-16 16:13:31 +0100192 controller->dma_tx = dma_request_slave_channel_compat(mask,
Mika Westerbergb729bf32014-08-19 20:29:19 +0300193 pdata->dma_filter, pdata->tx_param, dev, "tx");
Lubomir Rintel51eea522019-01-16 16:13:31 +0100194 if (!controller->dma_tx)
Mika Westerberg59288082013-01-22 12:26:29 +0200195 return -ENODEV;
196
Lubomir Rintel51eea522019-01-16 16:13:31 +0100197 controller->dma_rx = dma_request_slave_channel_compat(mask,
Mika Westerbergb729bf32014-08-19 20:29:19 +0300198 pdata->dma_filter, pdata->rx_param, dev, "rx");
Lubomir Rintel51eea522019-01-16 16:13:31 +0100199 if (!controller->dma_rx) {
200 dma_release_channel(controller->dma_tx);
201 controller->dma_tx = NULL;
Mika Westerberg59288082013-01-22 12:26:29 +0200202 return -ENODEV;
203 }
204
205 return 0;
206}
207
208void pxa2xx_spi_dma_release(struct driver_data *drv_data)
209{
Lubomir Rintel51eea522019-01-16 16:13:31 +0100210 struct spi_controller *controller = drv_data->controller;
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300211
Lubomir Rintel51eea522019-01-16 16:13:31 +0100212 if (controller->dma_rx) {
213 dmaengine_terminate_sync(controller->dma_rx);
214 dma_release_channel(controller->dma_rx);
215 controller->dma_rx = NULL;
Mika Westerberg59288082013-01-22 12:26:29 +0200216 }
Lubomir Rintel51eea522019-01-16 16:13:31 +0100217 if (controller->dma_tx) {
218 dmaengine_terminate_sync(controller->dma_tx);
219 dma_release_channel(controller->dma_tx);
220 controller->dma_tx = NULL;
Mika Westerberg59288082013-01-22 12:26:29 +0200221 }
222}
223
Mika Westerberg59288082013-01-22 12:26:29 +0200224int pxa2xx_spi_set_dma_burst_and_threshold(struct chip_data *chip,
225 struct spi_device *spi,
226 u8 bits_per_word, u32 *burst_code,
227 u32 *threshold)
228{
229 struct pxa2xx_spi_chip *chip_info = spi->controller_data;
Andy Shevchenko37821a822019-03-19 17:48:42 +0200230 struct driver_data *drv_data = spi_controller_get_devdata(spi->controller);
231 u32 dma_burst_size = drv_data->controller_info->dma_burst_size;
Mika Westerberg59288082013-01-22 12:26:29 +0200232
233 /*
234 * If the DMA burst size is given in chip_info we use that,
235 * otherwise we use the default. Also we use the default FIFO
236 * thresholds for now.
237 */
Andy Shevchenko37821a822019-03-19 17:48:42 +0200238 *burst_code = chip_info ? chip_info->dma_burst_size : dma_burst_size;
Mika Westerberg59288082013-01-22 12:26:29 +0200239 *threshold = SSCR1_RxTresh(RX_THRESH_DFLT)
240 | SSCR1_TxTresh(TX_THRESH_DFLT);
241
242 return 0;
243}