blob: 02defb68618d922bbdeed76a6863e0bd27f172ad [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Feng Tang7063c0d2010-12-24 13:59:11 +08002/*
Grant Likelyca632f52011-06-06 01:16:30 -06003 * Special handling for DW core on Intel MID platform
Feng Tang7063c0d2010-12-24 13:59:11 +08004 *
Andy Shevchenko197e96b2014-09-12 15:12:01 +03005 * Copyright (c) 2009, 2014 Intel Corporation.
Feng Tang7063c0d2010-12-24 13:59:11 +08006 */
7
8#include <linux/dma-mapping.h>
9#include <linux/dmaengine.h>
Feng Tang7063c0d2010-12-24 13:59:11 +080010#include <linux/slab.h>
11#include <linux/spi/spi.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053012#include <linux/types.h>
Grant Likely568a60e2011-02-28 12:47:12 -070013
Grant Likelyca632f52011-06-06 01:16:30 -060014#include "spi-dw.h"
Feng Tang7063c0d2010-12-24 13:59:11 +080015
16#ifdef CONFIG_SPI_DW_MID_DMA
Andy Shevchenkoe62a15d2020-05-06 18:30:21 +030017#include <linux/irqreturn.h>
Feng Tang7063c0d2010-12-24 13:59:11 +080018#include <linux/pci.h>
Andy Shevchenkod744f822015-03-09 16:48:50 +020019#include <linux/platform_data/dma-dw.h>
Feng Tang7063c0d2010-12-24 13:59:11 +080020
Andy Shevchenko30c8eb52014-10-28 18:25:02 +020021#define RX_BUSY 0
22#define TX_BUSY 1
23
Andy Shevchenkod744f822015-03-09 16:48:50 +020024static struct dw_dma_slave mid_dma_tx = { .dst_id = 1 };
25static struct dw_dma_slave mid_dma_rx = { .src_id = 0 };
Feng Tang7063c0d2010-12-24 13:59:11 +080026
27static bool mid_spi_dma_chan_filter(struct dma_chan *chan, void *param)
28{
Andy Shevchenkod744f822015-03-09 16:48:50 +020029 struct dw_dma_slave *s = param;
Feng Tang7063c0d2010-12-24 13:59:11 +080030
Andy Shevchenkod744f822015-03-09 16:48:50 +020031 if (s->dma_dev != chan->device->dev)
32 return false;
33
34 chan->private = s;
35 return true;
Feng Tang7063c0d2010-12-24 13:59:11 +080036}
37
38static int mid_spi_dma_init(struct dw_spi *dws)
39{
Andy Shevchenkob89e9c82014-09-12 15:12:00 +030040 struct pci_dev *dma_dev;
Andy Shevchenkod744f822015-03-09 16:48:50 +020041 struct dw_dma_slave *tx = dws->dma_tx;
42 struct dw_dma_slave *rx = dws->dma_rx;
Feng Tang7063c0d2010-12-24 13:59:11 +080043 dma_cap_mask_t mask;
44
45 /*
46 * Get pci device for DMA controller, currently it could only
Andy Shevchenkoea092452014-09-12 15:11:59 +030047 * be the DMA controller of Medfield
Feng Tang7063c0d2010-12-24 13:59:11 +080048 */
Andy Shevchenkob89e9c82014-09-12 15:12:00 +030049 dma_dev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x0827, NULL);
50 if (!dma_dev)
51 return -ENODEV;
52
Feng Tang7063c0d2010-12-24 13:59:11 +080053 dma_cap_zero(mask);
54 dma_cap_set(DMA_SLAVE, mask);
55
56 /* 1. Init rx channel */
Andy Shevchenkod744f822015-03-09 16:48:50 +020057 rx->dma_dev = &dma_dev->dev;
58 dws->rxchan = dma_request_channel(mask, mid_spi_dma_chan_filter, rx);
Feng Tang7063c0d2010-12-24 13:59:11 +080059 if (!dws->rxchan)
60 goto err_exit;
Andy Shevchenkof89a6d82015-03-09 16:48:49 +020061 dws->master->dma_rx = dws->rxchan;
Feng Tang7063c0d2010-12-24 13:59:11 +080062
63 /* 2. Init tx channel */
Andy Shevchenkod744f822015-03-09 16:48:50 +020064 tx->dma_dev = &dma_dev->dev;
65 dws->txchan = dma_request_channel(mask, mid_spi_dma_chan_filter, tx);
Feng Tang7063c0d2010-12-24 13:59:11 +080066 if (!dws->txchan)
67 goto free_rxchan;
Andy Shevchenkof89a6d82015-03-09 16:48:49 +020068 dws->master->dma_tx = dws->txchan;
Feng Tang7063c0d2010-12-24 13:59:11 +080069
70 dws->dma_inited = 1;
71 return 0;
72
73free_rxchan:
74 dma_release_channel(dws->rxchan);
75err_exit:
Andy Shevchenkob89e9c82014-09-12 15:12:00 +030076 return -EBUSY;
Feng Tang7063c0d2010-12-24 13:59:11 +080077}
78
79static void mid_spi_dma_exit(struct dw_spi *dws)
80{
Andy Shevchenkofb578622014-09-12 15:11:58 +030081 if (!dws->dma_inited)
82 return;
Andy Shevchenko8e45ef62014-09-18 20:08:53 +030083
Andy Shevchenkoa3ff9582016-02-05 16:46:26 +020084 dmaengine_terminate_sync(dws->txchan);
Feng Tang7063c0d2010-12-24 13:59:11 +080085 dma_release_channel(dws->txchan);
Andy Shevchenko8e45ef62014-09-18 20:08:53 +030086
Andy Shevchenkoa3ff9582016-02-05 16:46:26 +020087 dmaengine_terminate_sync(dws->rxchan);
Feng Tang7063c0d2010-12-24 13:59:11 +080088 dma_release_channel(dws->rxchan);
89}
90
Andy Shevchenkof051fc82015-03-09 16:48:47 +020091static irqreturn_t dma_transfer(struct dw_spi *dws)
92{
Thor Thayerdd114442015-03-12 14:19:31 -050093 u16 irq_status = dw_readl(dws, DW_SPI_ISR);
Andy Shevchenkof051fc82015-03-09 16:48:47 +020094
95 if (!irq_status)
96 return IRQ_NONE;
97
Thor Thayerdd114442015-03-12 14:19:31 -050098 dw_readl(dws, DW_SPI_ICR);
Andy Shevchenkof051fc82015-03-09 16:48:47 +020099 spi_reset_chip(dws);
100
101 dev_err(&dws->master->dev, "%s: FIFO overrun/underrun\n", __func__);
102 dws->master->cur_msg->status = -EIO;
103 spi_finalize_current_transfer(dws->master);
104 return IRQ_HANDLED;
105}
106
Jarkko Nikula721483e2018-02-01 17:17:29 +0200107static bool mid_spi_can_dma(struct spi_controller *master,
108 struct spi_device *spi, struct spi_transfer *xfer)
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200109{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200110 struct dw_spi *dws = spi_controller_get_devdata(master);
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200111
112 if (!dws->dma_inited)
113 return false;
114
115 return xfer->len > dws->fifo_len;
116}
117
Andy Shevchenkoe31abce2015-03-09 16:48:45 +0200118static enum dma_slave_buswidth convert_dma_width(u32 dma_width) {
119 if (dma_width == 1)
120 return DMA_SLAVE_BUSWIDTH_1_BYTE;
121 else if (dma_width == 2)
122 return DMA_SLAVE_BUSWIDTH_2_BYTES;
123
124 return DMA_SLAVE_BUSWIDTH_UNDEFINED;
125}
126
Feng Tang7063c0d2010-12-24 13:59:11 +0800127/*
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200128 * dws->dma_chan_busy is set before the dma transfer starts, callback for tx
129 * channel will clear a corresponding bit.
Feng Tang7063c0d2010-12-24 13:59:11 +0800130 */
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200131static void dw_spi_dma_tx_done(void *arg)
Feng Tang7063c0d2010-12-24 13:59:11 +0800132{
133 struct dw_spi *dws = arg;
134
Andy Shevchenko854d2f22015-03-06 14:42:01 +0200135 clear_bit(TX_BUSY, &dws->dma_chan_busy);
136 if (test_bit(RX_BUSY, &dws->dma_chan_busy))
Feng Tang7063c0d2010-12-24 13:59:11 +0800137 return;
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200138 spi_finalize_current_transfer(dws->master);
Feng Tang7063c0d2010-12-24 13:59:11 +0800139}
140
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200141static struct dma_async_tx_descriptor *dw_spi_dma_prepare_tx(struct dw_spi *dws,
142 struct spi_transfer *xfer)
Feng Tang7063c0d2010-12-24 13:59:11 +0800143{
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200144 struct dma_slave_config txconf;
145 struct dma_async_tx_descriptor *txdesc;
Feng Tang7063c0d2010-12-24 13:59:11 +0800146
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200147 if (!xfer->tx_buf)
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200148 return NULL;
149
Andy Shevchenko3cb97e22020-05-06 18:30:18 +0300150 memset(&txconf, 0, sizeof(txconf));
Vinod Koula485df42011-10-14 10:47:38 +0530151 txconf.direction = DMA_MEM_TO_DEV;
Feng Tang7063c0d2010-12-24 13:59:11 +0800152 txconf.dst_addr = dws->dma_addr;
Andy Shevchenkod744f822015-03-09 16:48:50 +0200153 txconf.dst_maxburst = 16;
Feng Tang7063c0d2010-12-24 13:59:11 +0800154 txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Andy Shevchenkoe31abce2015-03-09 16:48:45 +0200155 txconf.dst_addr_width = convert_dma_width(dws->dma_width);
Viresh Kumar258aea72012-02-01 16:12:19 +0530156 txconf.device_fc = false;
Feng Tang7063c0d2010-12-24 13:59:11 +0800157
Andy Shevchenko2a285292014-10-02 16:31:08 +0300158 dmaengine_slave_config(dws->txchan, &txconf);
Feng Tang7063c0d2010-12-24 13:59:11 +0800159
Andy Shevchenko2a285292014-10-02 16:31:08 +0300160 txdesc = dmaengine_prep_slave_sg(dws->txchan,
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200161 xfer->tx_sg.sgl,
162 xfer->tx_sg.nents,
Vinod Koula485df42011-10-14 10:47:38 +0530163 DMA_MEM_TO_DEV,
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300164 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Andy Shevchenkoc9dafb22015-03-02 20:15:58 +0200165 if (!txdesc)
166 return NULL;
167
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200168 txdesc->callback = dw_spi_dma_tx_done;
Feng Tang7063c0d2010-12-24 13:59:11 +0800169 txdesc->callback_param = dws;
170
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200171 return txdesc;
172}
173
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200174/*
175 * dws->dma_chan_busy is set before the dma transfer starts, callback for rx
176 * channel will clear a corresponding bit.
177 */
178static void dw_spi_dma_rx_done(void *arg)
179{
180 struct dw_spi *dws = arg;
181
Andy Shevchenko854d2f22015-03-06 14:42:01 +0200182 clear_bit(RX_BUSY, &dws->dma_chan_busy);
183 if (test_bit(TX_BUSY, &dws->dma_chan_busy))
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200184 return;
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200185 spi_finalize_current_transfer(dws->master);
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200186}
187
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200188static struct dma_async_tx_descriptor *dw_spi_dma_prepare_rx(struct dw_spi *dws,
189 struct spi_transfer *xfer)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200190{
191 struct dma_slave_config rxconf;
192 struct dma_async_tx_descriptor *rxdesc;
193
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200194 if (!xfer->rx_buf)
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200195 return NULL;
196
Andy Shevchenko3cb97e22020-05-06 18:30:18 +0300197 memset(&rxconf, 0, sizeof(rxconf));
Vinod Koula485df42011-10-14 10:47:38 +0530198 rxconf.direction = DMA_DEV_TO_MEM;
Feng Tang7063c0d2010-12-24 13:59:11 +0800199 rxconf.src_addr = dws->dma_addr;
Andy Shevchenkod744f822015-03-09 16:48:50 +0200200 rxconf.src_maxburst = 16;
Feng Tang7063c0d2010-12-24 13:59:11 +0800201 rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Andy Shevchenkoe31abce2015-03-09 16:48:45 +0200202 rxconf.src_addr_width = convert_dma_width(dws->dma_width);
Viresh Kumar258aea72012-02-01 16:12:19 +0530203 rxconf.device_fc = false;
Feng Tang7063c0d2010-12-24 13:59:11 +0800204
Andy Shevchenko2a285292014-10-02 16:31:08 +0300205 dmaengine_slave_config(dws->rxchan, &rxconf);
Feng Tang7063c0d2010-12-24 13:59:11 +0800206
Andy Shevchenko2a285292014-10-02 16:31:08 +0300207 rxdesc = dmaengine_prep_slave_sg(dws->rxchan,
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200208 xfer->rx_sg.sgl,
209 xfer->rx_sg.nents,
Vinod Koula485df42011-10-14 10:47:38 +0530210 DMA_DEV_TO_MEM,
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300211 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Andy Shevchenkoc9dafb22015-03-02 20:15:58 +0200212 if (!rxdesc)
213 return NULL;
214
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200215 rxdesc->callback = dw_spi_dma_rx_done;
Feng Tang7063c0d2010-12-24 13:59:11 +0800216 rxdesc->callback_param = dws;
217
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200218 return rxdesc;
219}
220
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200221static int mid_spi_dma_setup(struct dw_spi *dws, struct spi_transfer *xfer)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200222{
223 u16 dma_ctrl = 0;
224
Thor Thayerdd114442015-03-12 14:19:31 -0500225 dw_writel(dws, DW_SPI_DMARDLR, 0xf);
226 dw_writel(dws, DW_SPI_DMATDLR, 0x10);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200227
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200228 if (xfer->tx_buf)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200229 dma_ctrl |= SPI_DMA_TDMAE;
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200230 if (xfer->rx_buf)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200231 dma_ctrl |= SPI_DMA_RDMAE;
Thor Thayerdd114442015-03-12 14:19:31 -0500232 dw_writel(dws, DW_SPI_DMACR, dma_ctrl);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200233
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200234 /* Set the interrupt mask */
235 spi_umask_intr(dws, SPI_INT_TXOI | SPI_INT_RXUI | SPI_INT_RXOI);
236
237 dws->transfer_handler = dma_transfer;
238
Andy Shevchenko9f145382015-03-09 16:48:46 +0200239 return 0;
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200240}
241
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200242static int mid_spi_dma_transfer(struct dw_spi *dws, struct spi_transfer *xfer)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200243{
244 struct dma_async_tx_descriptor *txdesc, *rxdesc;
245
Andy Shevchenko9f145382015-03-09 16:48:46 +0200246 /* Prepare the TX dma transfer */
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200247 txdesc = dw_spi_dma_prepare_tx(dws, xfer);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200248
Andy Shevchenko9f145382015-03-09 16:48:46 +0200249 /* Prepare the RX dma transfer */
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200250 rxdesc = dw_spi_dma_prepare_rx(dws, xfer);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200251
Feng Tang7063c0d2010-12-24 13:59:11 +0800252 /* rx must be started before tx due to spi instinct */
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200253 if (rxdesc) {
254 set_bit(RX_BUSY, &dws->dma_chan_busy);
255 dmaengine_submit(rxdesc);
256 dma_async_issue_pending(dws->rxchan);
257 }
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300258
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200259 if (txdesc) {
260 set_bit(TX_BUSY, &dws->dma_chan_busy);
261 dmaengine_submit(txdesc);
262 dma_async_issue_pending(dws->txchan);
263 }
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300264
Feng Tang7063c0d2010-12-24 13:59:11 +0800265 return 0;
266}
267
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200268static void mid_spi_dma_stop(struct dw_spi *dws)
269{
270 if (test_bit(TX_BUSY, &dws->dma_chan_busy)) {
Andy Shevchenkocf1716e2017-01-03 15:48:20 +0200271 dmaengine_terminate_sync(dws->txchan);
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200272 clear_bit(TX_BUSY, &dws->dma_chan_busy);
273 }
274 if (test_bit(RX_BUSY, &dws->dma_chan_busy)) {
Andy Shevchenkocf1716e2017-01-03 15:48:20 +0200275 dmaengine_terminate_sync(dws->rxchan);
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200276 clear_bit(RX_BUSY, &dws->dma_chan_busy);
277 }
278}
279
Julia Lawall4fe338c2015-11-28 15:09:38 +0100280static const struct dw_spi_dma_ops mid_dma_ops = {
Feng Tang7063c0d2010-12-24 13:59:11 +0800281 .dma_init = mid_spi_dma_init,
282 .dma_exit = mid_spi_dma_exit,
Andy Shevchenko9f145382015-03-09 16:48:46 +0200283 .dma_setup = mid_spi_dma_setup,
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200284 .can_dma = mid_spi_can_dma,
Feng Tang7063c0d2010-12-24 13:59:11 +0800285 .dma_transfer = mid_spi_dma_transfer,
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200286 .dma_stop = mid_spi_dma_stop,
Feng Tang7063c0d2010-12-24 13:59:11 +0800287};
288#endif
289
Andy Shevchenkoea092452014-09-12 15:11:59 +0300290/* Some specific info for SPI0 controller on Intel MID */
Feng Tang7063c0d2010-12-24 13:59:11 +0800291
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200292/* HW info for MRST Clk Control Unit, 32b reg per controller */
Feng Tang7063c0d2010-12-24 13:59:11 +0800293#define MRST_SPI_CLK_BASE 100000000 /* 100m */
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200294#define MRST_CLK_SPI_REG 0xff11d86c
Feng Tang7063c0d2010-12-24 13:59:11 +0800295#define CLK_SPI_BDIV_OFFSET 0
296#define CLK_SPI_BDIV_MASK 0x00000007
297#define CLK_SPI_CDIV_OFFSET 9
298#define CLK_SPI_CDIV_MASK 0x00000e00
299#define CLK_SPI_DISABLE_OFFSET 8
300
301int dw_spi_mid_init(struct dw_spi *dws)
302{
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -0700303 void __iomem *clk_reg;
304 u32 clk_cdiv;
Feng Tang7063c0d2010-12-24 13:59:11 +0800305
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100306 clk_reg = ioremap(MRST_CLK_SPI_REG, 16);
Feng Tang7063c0d2010-12-24 13:59:11 +0800307 if (!clk_reg)
308 return -ENOMEM;
309
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200310 /* Get SPI controller operating freq info */
311 clk_cdiv = readl(clk_reg + dws->bus_num * sizeof(u32));
312 clk_cdiv &= CLK_SPI_CDIV_MASK;
313 clk_cdiv >>= CLK_SPI_CDIV_OFFSET;
Feng Tang7063c0d2010-12-24 13:59:11 +0800314 dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1);
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200315
Feng Tang7063c0d2010-12-24 13:59:11 +0800316 iounmap(clk_reg);
317
Feng Tang7063c0d2010-12-24 13:59:11 +0800318#ifdef CONFIG_SPI_DW_MID_DMA
Andy Shevchenkod744f822015-03-09 16:48:50 +0200319 dws->dma_tx = &mid_dma_tx;
320 dws->dma_rx = &mid_dma_rx;
Feng Tang7063c0d2010-12-24 13:59:11 +0800321 dws->dma_ops = &mid_dma_ops;
322#endif
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800323
324 /* Register hook to configure CTRLR0 */
325 dws->update_cr0 = dw_spi_update_cr0;
326
Feng Tang7063c0d2010-12-24 13:59:11 +0800327 return 0;
328}