Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 1 | /* |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 2 | * Special handling for DW core on Intel MID platform |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 3 | * |
Andy Shevchenko | 197e96b | 2014-09-12 15:12:01 +0300 | [diff] [blame] | 4 | * Copyright (c) 2009, 2014 Intel Corporation. |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/dma-mapping.h> |
| 17 | #include <linux/dmaengine.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/spi/spi.h> |
Viresh Kumar | 258aea7 | 2012-02-01 16:12:19 +0530 | [diff] [blame] | 21 | #include <linux/types.h> |
Grant Likely | 568a60e | 2011-02-28 12:47:12 -0700 | [diff] [blame] | 22 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 23 | #include "spi-dw.h" |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 24 | |
| 25 | #ifdef CONFIG_SPI_DW_MID_DMA |
| 26 | #include <linux/intel_mid_dma.h> |
| 27 | #include <linux/pci.h> |
| 28 | |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 29 | #define RX_BUSY 0 |
| 30 | #define TX_BUSY 1 |
| 31 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 32 | struct mid_dma { |
| 33 | struct intel_mid_dma_slave dmas_tx; |
| 34 | struct intel_mid_dma_slave dmas_rx; |
| 35 | }; |
| 36 | |
| 37 | static bool mid_spi_dma_chan_filter(struct dma_chan *chan, void *param) |
| 38 | { |
| 39 | struct dw_spi *dws = param; |
| 40 | |
Andy Shevchenko | b89e9c8 | 2014-09-12 15:12:00 +0300 | [diff] [blame] | 41 | return dws->dma_dev == chan->device->dev; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static int mid_spi_dma_init(struct dw_spi *dws) |
| 45 | { |
| 46 | struct mid_dma *dw_dma = dws->dma_priv; |
Andy Shevchenko | b89e9c8 | 2014-09-12 15:12:00 +0300 | [diff] [blame] | 47 | struct pci_dev *dma_dev; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 48 | struct intel_mid_dma_slave *rxs, *txs; |
| 49 | dma_cap_mask_t mask; |
| 50 | |
| 51 | /* |
| 52 | * Get pci device for DMA controller, currently it could only |
Andy Shevchenko | ea09245 | 2014-09-12 15:11:59 +0300 | [diff] [blame] | 53 | * be the DMA controller of Medfield |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 54 | */ |
Andy Shevchenko | b89e9c8 | 2014-09-12 15:12:00 +0300 | [diff] [blame] | 55 | dma_dev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x0827, NULL); |
| 56 | if (!dma_dev) |
| 57 | return -ENODEV; |
| 58 | |
| 59 | dws->dma_dev = &dma_dev->dev; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 60 | |
| 61 | dma_cap_zero(mask); |
| 62 | dma_cap_set(DMA_SLAVE, mask); |
| 63 | |
| 64 | /* 1. Init rx channel */ |
| 65 | dws->rxchan = dma_request_channel(mask, mid_spi_dma_chan_filter, dws); |
| 66 | if (!dws->rxchan) |
| 67 | goto err_exit; |
| 68 | rxs = &dw_dma->dmas_rx; |
| 69 | rxs->hs_mode = LNW_DMA_HW_HS; |
| 70 | rxs->cfg_mode = LNW_DMA_PER_TO_MEM; |
| 71 | dws->rxchan->private = rxs; |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 72 | dws->master->dma_rx = dws->rxchan; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 73 | |
| 74 | /* 2. Init tx channel */ |
| 75 | dws->txchan = dma_request_channel(mask, mid_spi_dma_chan_filter, dws); |
| 76 | if (!dws->txchan) |
| 77 | goto free_rxchan; |
| 78 | txs = &dw_dma->dmas_tx; |
| 79 | txs->hs_mode = LNW_DMA_HW_HS; |
| 80 | txs->cfg_mode = LNW_DMA_MEM_TO_PER; |
| 81 | dws->txchan->private = txs; |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 82 | dws->master->dma_tx = dws->txchan; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 83 | |
| 84 | dws->dma_inited = 1; |
| 85 | return 0; |
| 86 | |
| 87 | free_rxchan: |
| 88 | dma_release_channel(dws->rxchan); |
| 89 | err_exit: |
Andy Shevchenko | b89e9c8 | 2014-09-12 15:12:00 +0300 | [diff] [blame] | 90 | return -EBUSY; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static void mid_spi_dma_exit(struct dw_spi *dws) |
| 94 | { |
Andy Shevchenko | fb57862 | 2014-09-12 15:11:58 +0300 | [diff] [blame] | 95 | if (!dws->dma_inited) |
| 96 | return; |
Andy Shevchenko | 8e45ef6 | 2014-09-18 20:08:53 +0300 | [diff] [blame] | 97 | |
| 98 | dmaengine_terminate_all(dws->txchan); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 99 | dma_release_channel(dws->txchan); |
Andy Shevchenko | 8e45ef6 | 2014-09-18 20:08:53 +0300 | [diff] [blame] | 100 | |
| 101 | dmaengine_terminate_all(dws->rxchan); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 102 | dma_release_channel(dws->rxchan); |
| 103 | } |
| 104 | |
Andy Shevchenko | f051fc8 | 2015-03-09 16:48:47 +0200 | [diff] [blame] | 105 | static irqreturn_t dma_transfer(struct dw_spi *dws) |
| 106 | { |
| 107 | u16 irq_status = dw_readw(dws, DW_SPI_ISR); |
| 108 | |
| 109 | if (!irq_status) |
| 110 | return IRQ_NONE; |
| 111 | |
| 112 | dw_readw(dws, DW_SPI_ICR); |
| 113 | spi_reset_chip(dws); |
| 114 | |
| 115 | dev_err(&dws->master->dev, "%s: FIFO overrun/underrun\n", __func__); |
| 116 | dws->master->cur_msg->status = -EIO; |
| 117 | spi_finalize_current_transfer(dws->master); |
| 118 | return IRQ_HANDLED; |
| 119 | } |
| 120 | |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 121 | static bool mid_spi_can_dma(struct spi_master *master, struct spi_device *spi, |
| 122 | struct spi_transfer *xfer) |
| 123 | { |
| 124 | struct dw_spi *dws = spi_master_get_devdata(master); |
| 125 | |
| 126 | if (!dws->dma_inited) |
| 127 | return false; |
| 128 | |
| 129 | return xfer->len > dws->fifo_len; |
| 130 | } |
| 131 | |
Andy Shevchenko | e31abce | 2015-03-09 16:48:45 +0200 | [diff] [blame] | 132 | static enum dma_slave_buswidth convert_dma_width(u32 dma_width) { |
| 133 | if (dma_width == 1) |
| 134 | return DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 135 | else if (dma_width == 2) |
| 136 | return DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 137 | |
| 138 | return DMA_SLAVE_BUSWIDTH_UNDEFINED; |
| 139 | } |
| 140 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 141 | /* |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 142 | * dws->dma_chan_busy is set before the dma transfer starts, callback for tx |
| 143 | * channel will clear a corresponding bit. |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 144 | */ |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 145 | static void dw_spi_dma_tx_done(void *arg) |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 146 | { |
| 147 | struct dw_spi *dws = arg; |
| 148 | |
Andy Shevchenko | 854d2f2 | 2015-03-06 14:42:01 +0200 | [diff] [blame] | 149 | clear_bit(TX_BUSY, &dws->dma_chan_busy); |
| 150 | if (test_bit(RX_BUSY, &dws->dma_chan_busy)) |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 151 | return; |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 152 | spi_finalize_current_transfer(dws->master); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 153 | } |
| 154 | |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 155 | static struct dma_async_tx_descriptor *dw_spi_dma_prepare_tx(struct dw_spi *dws, |
| 156 | struct spi_transfer *xfer) |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 157 | { |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 158 | struct dma_slave_config txconf; |
| 159 | struct dma_async_tx_descriptor *txdesc; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 160 | |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 161 | if (!xfer->tx_buf) |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 162 | return NULL; |
| 163 | |
Vinod Koul | a485df4 | 2011-10-14 10:47:38 +0530 | [diff] [blame] | 164 | txconf.direction = DMA_MEM_TO_DEV; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 165 | txconf.dst_addr = dws->dma_addr; |
| 166 | txconf.dst_maxburst = LNW_DMA_MSIZE_16; |
| 167 | txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
Andy Shevchenko | e31abce | 2015-03-09 16:48:45 +0200 | [diff] [blame] | 168 | txconf.dst_addr_width = convert_dma_width(dws->dma_width); |
Viresh Kumar | 258aea7 | 2012-02-01 16:12:19 +0530 | [diff] [blame] | 169 | txconf.device_fc = false; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 170 | |
Andy Shevchenko | 2a28529 | 2014-10-02 16:31:08 +0300 | [diff] [blame] | 171 | dmaengine_slave_config(dws->txchan, &txconf); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 172 | |
Andy Shevchenko | 2a28529 | 2014-10-02 16:31:08 +0300 | [diff] [blame] | 173 | txdesc = dmaengine_prep_slave_sg(dws->txchan, |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 174 | xfer->tx_sg.sgl, |
| 175 | xfer->tx_sg.nents, |
Vinod Koul | a485df4 | 2011-10-14 10:47:38 +0530 | [diff] [blame] | 176 | DMA_MEM_TO_DEV, |
Andy Shevchenko | f7477c2 | 2014-10-02 16:31:09 +0300 | [diff] [blame] | 177 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Andy Shevchenko | c9dafb2 | 2015-03-02 20:15:58 +0200 | [diff] [blame] | 178 | if (!txdesc) |
| 179 | return NULL; |
| 180 | |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 181 | txdesc->callback = dw_spi_dma_tx_done; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 182 | txdesc->callback_param = dws; |
| 183 | |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 184 | return txdesc; |
| 185 | } |
| 186 | |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 187 | /* |
| 188 | * dws->dma_chan_busy is set before the dma transfer starts, callback for rx |
| 189 | * channel will clear a corresponding bit. |
| 190 | */ |
| 191 | static void dw_spi_dma_rx_done(void *arg) |
| 192 | { |
| 193 | struct dw_spi *dws = arg; |
| 194 | |
Andy Shevchenko | 854d2f2 | 2015-03-06 14:42:01 +0200 | [diff] [blame] | 195 | clear_bit(RX_BUSY, &dws->dma_chan_busy); |
| 196 | if (test_bit(TX_BUSY, &dws->dma_chan_busy)) |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 197 | return; |
Andy Shevchenko | c22c62d | 2015-03-02 14:58:57 +0200 | [diff] [blame] | 198 | spi_finalize_current_transfer(dws->master); |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 201 | static struct dma_async_tx_descriptor *dw_spi_dma_prepare_rx(struct dw_spi *dws, |
| 202 | struct spi_transfer *xfer) |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 203 | { |
| 204 | struct dma_slave_config rxconf; |
| 205 | struct dma_async_tx_descriptor *rxdesc; |
| 206 | |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 207 | if (!xfer->rx_buf) |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 208 | return NULL; |
| 209 | |
Vinod Koul | a485df4 | 2011-10-14 10:47:38 +0530 | [diff] [blame] | 210 | rxconf.direction = DMA_DEV_TO_MEM; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 211 | rxconf.src_addr = dws->dma_addr; |
| 212 | rxconf.src_maxburst = LNW_DMA_MSIZE_16; |
| 213 | rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
Andy Shevchenko | e31abce | 2015-03-09 16:48:45 +0200 | [diff] [blame] | 214 | rxconf.src_addr_width = convert_dma_width(dws->dma_width); |
Viresh Kumar | 258aea7 | 2012-02-01 16:12:19 +0530 | [diff] [blame] | 215 | rxconf.device_fc = false; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 216 | |
Andy Shevchenko | 2a28529 | 2014-10-02 16:31:08 +0300 | [diff] [blame] | 217 | dmaengine_slave_config(dws->rxchan, &rxconf); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 218 | |
Andy Shevchenko | 2a28529 | 2014-10-02 16:31:08 +0300 | [diff] [blame] | 219 | rxdesc = dmaengine_prep_slave_sg(dws->rxchan, |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 220 | xfer->rx_sg.sgl, |
| 221 | xfer->rx_sg.nents, |
Vinod Koul | a485df4 | 2011-10-14 10:47:38 +0530 | [diff] [blame] | 222 | DMA_DEV_TO_MEM, |
Andy Shevchenko | f7477c2 | 2014-10-02 16:31:09 +0300 | [diff] [blame] | 223 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Andy Shevchenko | c9dafb2 | 2015-03-02 20:15:58 +0200 | [diff] [blame] | 224 | if (!rxdesc) |
| 225 | return NULL; |
| 226 | |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 227 | rxdesc->callback = dw_spi_dma_rx_done; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 228 | rxdesc->callback_param = dws; |
| 229 | |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 230 | return rxdesc; |
| 231 | } |
| 232 | |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 233 | static int mid_spi_dma_setup(struct dw_spi *dws, struct spi_transfer *xfer) |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 234 | { |
| 235 | u16 dma_ctrl = 0; |
| 236 | |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 237 | dw_writew(dws, DW_SPI_DMARDLR, 0xf); |
| 238 | dw_writew(dws, DW_SPI_DMATDLR, 0x10); |
| 239 | |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 240 | if (xfer->tx_buf) |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 241 | dma_ctrl |= SPI_DMA_TDMAE; |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 242 | if (xfer->rx_buf) |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 243 | dma_ctrl |= SPI_DMA_RDMAE; |
| 244 | dw_writew(dws, DW_SPI_DMACR, dma_ctrl); |
| 245 | |
Andy Shevchenko | f051fc8 | 2015-03-09 16:48:47 +0200 | [diff] [blame] | 246 | /* Set the interrupt mask */ |
| 247 | spi_umask_intr(dws, SPI_INT_TXOI | SPI_INT_RXUI | SPI_INT_RXOI); |
| 248 | |
| 249 | dws->transfer_handler = dma_transfer; |
| 250 | |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 251 | return 0; |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 254 | static int mid_spi_dma_transfer(struct dw_spi *dws, struct spi_transfer *xfer) |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 255 | { |
| 256 | struct dma_async_tx_descriptor *txdesc, *rxdesc; |
| 257 | |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 258 | /* Prepare the TX dma transfer */ |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 259 | txdesc = dw_spi_dma_prepare_tx(dws, xfer); |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 260 | |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 261 | /* Prepare the RX dma transfer */ |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 262 | rxdesc = dw_spi_dma_prepare_rx(dws, xfer); |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 263 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 264 | /* rx must be started before tx due to spi instinct */ |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 265 | if (rxdesc) { |
| 266 | set_bit(RX_BUSY, &dws->dma_chan_busy); |
| 267 | dmaengine_submit(rxdesc); |
| 268 | dma_async_issue_pending(dws->rxchan); |
| 269 | } |
Andy Shevchenko | f7477c2 | 2014-10-02 16:31:09 +0300 | [diff] [blame] | 270 | |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 271 | if (txdesc) { |
| 272 | set_bit(TX_BUSY, &dws->dma_chan_busy); |
| 273 | dmaengine_submit(txdesc); |
| 274 | dma_async_issue_pending(dws->txchan); |
| 275 | } |
Andy Shevchenko | f7477c2 | 2014-10-02 16:31:09 +0300 | [diff] [blame] | 276 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
Andy Shevchenko | 4d5ac1e | 2015-03-09 16:48:48 +0200 | [diff] [blame] | 280 | static void mid_spi_dma_stop(struct dw_spi *dws) |
| 281 | { |
| 282 | if (test_bit(TX_BUSY, &dws->dma_chan_busy)) { |
| 283 | dmaengine_terminate_all(dws->txchan); |
| 284 | clear_bit(TX_BUSY, &dws->dma_chan_busy); |
| 285 | } |
| 286 | if (test_bit(RX_BUSY, &dws->dma_chan_busy)) { |
| 287 | dmaengine_terminate_all(dws->rxchan); |
| 288 | clear_bit(RX_BUSY, &dws->dma_chan_busy); |
| 289 | } |
| 290 | } |
| 291 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 292 | static struct dw_spi_dma_ops mid_dma_ops = { |
| 293 | .dma_init = mid_spi_dma_init, |
| 294 | .dma_exit = mid_spi_dma_exit, |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 295 | .dma_setup = mid_spi_dma_setup, |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame^] | 296 | .can_dma = mid_spi_can_dma, |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 297 | .dma_transfer = mid_spi_dma_transfer, |
Andy Shevchenko | 4d5ac1e | 2015-03-09 16:48:48 +0200 | [diff] [blame] | 298 | .dma_stop = mid_spi_dma_stop, |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 299 | }; |
| 300 | #endif |
| 301 | |
Andy Shevchenko | ea09245 | 2014-09-12 15:11:59 +0300 | [diff] [blame] | 302 | /* Some specific info for SPI0 controller on Intel MID */ |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 303 | |
Andy Shevchenko | d9c1474 | 2015-01-22 17:59:34 +0200 | [diff] [blame] | 304 | /* HW info for MRST Clk Control Unit, 32b reg per controller */ |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 305 | #define MRST_SPI_CLK_BASE 100000000 /* 100m */ |
Andy Shevchenko | d9c1474 | 2015-01-22 17:59:34 +0200 | [diff] [blame] | 306 | #define MRST_CLK_SPI_REG 0xff11d86c |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 307 | #define CLK_SPI_BDIV_OFFSET 0 |
| 308 | #define CLK_SPI_BDIV_MASK 0x00000007 |
| 309 | #define CLK_SPI_CDIV_OFFSET 9 |
| 310 | #define CLK_SPI_CDIV_MASK 0x00000e00 |
| 311 | #define CLK_SPI_DISABLE_OFFSET 8 |
| 312 | |
| 313 | int dw_spi_mid_init(struct dw_spi *dws) |
| 314 | { |
H Hartley Sweeten | 7eb187b | 2011-09-20 11:06:17 -0700 | [diff] [blame] | 315 | void __iomem *clk_reg; |
| 316 | u32 clk_cdiv; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 317 | |
Andy Shevchenko | d9c1474 | 2015-01-22 17:59:34 +0200 | [diff] [blame] | 318 | clk_reg = ioremap_nocache(MRST_CLK_SPI_REG, 16); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 319 | if (!clk_reg) |
| 320 | return -ENOMEM; |
| 321 | |
Andy Shevchenko | d9c1474 | 2015-01-22 17:59:34 +0200 | [diff] [blame] | 322 | /* Get SPI controller operating freq info */ |
| 323 | clk_cdiv = readl(clk_reg + dws->bus_num * sizeof(u32)); |
| 324 | clk_cdiv &= CLK_SPI_CDIV_MASK; |
| 325 | clk_cdiv >>= CLK_SPI_CDIV_OFFSET; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 326 | dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1); |
Andy Shevchenko | d9c1474 | 2015-01-22 17:59:34 +0200 | [diff] [blame] | 327 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 328 | iounmap(clk_reg); |
| 329 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 330 | #ifdef CONFIG_SPI_DW_MID_DMA |
| 331 | dws->dma_priv = kzalloc(sizeof(struct mid_dma), GFP_KERNEL); |
| 332 | if (!dws->dma_priv) |
| 333 | return -ENOMEM; |
| 334 | dws->dma_ops = &mid_dma_ops; |
| 335 | #endif |
| 336 | return 0; |
| 337 | } |