spi-dw-mid.c 7.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
F
Feng Tang 已提交
2
/*
G
Grant Likely 已提交
3
 * Special handling for DW core on Intel MID platform
F
Feng Tang 已提交
4
 *
5
 * Copyright (c) 2009, 2014 Intel Corporation.
F
Feng Tang 已提交
6 7 8 9 10 11
 */

#include <linux/dma-mapping.h>
#include <linux/dmaengine.h>
#include <linux/slab.h>
#include <linux/spi/spi.h>
12
#include <linux/types.h>
13

G
Grant Likely 已提交
14
#include "spi-dw.h"
F
Feng Tang 已提交
15 16

#ifdef CONFIG_SPI_DW_MID_DMA
17
#include <linux/irqreturn.h>
F
Feng Tang 已提交
18
#include <linux/pci.h>
19
#include <linux/platform_data/dma-dw.h>
F
Feng Tang 已提交
20

21 22 23
#define RX_BUSY		0
#define TX_BUSY		1

24 25
static struct dw_dma_slave mid_dma_tx = { .dst_id = 1 };
static struct dw_dma_slave mid_dma_rx = { .src_id = 0 };
F
Feng Tang 已提交
26 27 28

static bool mid_spi_dma_chan_filter(struct dma_chan *chan, void *param)
{
29 30 31 32
	struct dw_dma_slave *s = param;

	if (s->dma_dev != chan->device->dev)
		return false;
F
Feng Tang 已提交
33

34 35
	chan->private = s;
	return true;
F
Feng Tang 已提交
36 37 38 39
}

static int mid_spi_dma_init(struct dw_spi *dws)
{
40
	struct pci_dev *dma_dev;
41 42
	struct dw_dma_slave *tx = dws->dma_tx;
	struct dw_dma_slave *rx = dws->dma_rx;
F
Feng Tang 已提交
43 44 45 46
	dma_cap_mask_t mask;

	/*
	 * Get pci device for DMA controller, currently it could only
47
	 * be the DMA controller of Medfield
F
Feng Tang 已提交
48
	 */
49 50 51 52
	dma_dev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x0827, NULL);
	if (!dma_dev)
		return -ENODEV;

F
Feng Tang 已提交
53 54 55 56
	dma_cap_zero(mask);
	dma_cap_set(DMA_SLAVE, mask);

	/* 1. Init rx channel */
57 58
	rx->dma_dev = &dma_dev->dev;
	dws->rxchan = dma_request_channel(mask, mid_spi_dma_chan_filter, rx);
F
Feng Tang 已提交
59 60
	if (!dws->rxchan)
		goto err_exit;
61
	dws->master->dma_rx = dws->rxchan;
F
Feng Tang 已提交
62 63

	/* 2. Init tx channel */
64 65
	tx->dma_dev = &dma_dev->dev;
	dws->txchan = dma_request_channel(mask, mid_spi_dma_chan_filter, tx);
F
Feng Tang 已提交
66 67
	if (!dws->txchan)
		goto free_rxchan;
68
	dws->master->dma_tx = dws->txchan;
F
Feng Tang 已提交
69 70 71 72 73 74 75

	dws->dma_inited = 1;
	return 0;

free_rxchan:
	dma_release_channel(dws->rxchan);
err_exit:
76
	return -EBUSY;
F
Feng Tang 已提交
77 78 79 80
}

static void mid_spi_dma_exit(struct dw_spi *dws)
{
81 82
	if (!dws->dma_inited)
		return;
83

84
	dmaengine_terminate_sync(dws->txchan);
F
Feng Tang 已提交
85
	dma_release_channel(dws->txchan);
86

87
	dmaengine_terminate_sync(dws->rxchan);
F
Feng Tang 已提交
88 89 90
	dma_release_channel(dws->rxchan);
}

91 92
static irqreturn_t dma_transfer(struct dw_spi *dws)
{
93
	u16 irq_status = dw_readl(dws, DW_SPI_ISR);
94 95 96 97

	if (!irq_status)
		return IRQ_NONE;

98
	dw_readl(dws, DW_SPI_ICR);
99 100 101 102 103 104 105 106
	spi_reset_chip(dws);

	dev_err(&dws->master->dev, "%s: FIFO overrun/underrun\n", __func__);
	dws->master->cur_msg->status = -EIO;
	spi_finalize_current_transfer(dws->master);
	return IRQ_HANDLED;
}

107 108
static bool mid_spi_can_dma(struct spi_controller *master,
		struct spi_device *spi, struct spi_transfer *xfer)
109
{
110
	struct dw_spi *dws = spi_controller_get_devdata(master);
111 112 113 114 115 116 117

	if (!dws->dma_inited)
		return false;

	return xfer->len > dws->fifo_len;
}

118 119 120 121 122 123 124 125 126
static enum dma_slave_buswidth convert_dma_width(u32 dma_width) {
	if (dma_width == 1)
		return DMA_SLAVE_BUSWIDTH_1_BYTE;
	else if (dma_width == 2)
		return DMA_SLAVE_BUSWIDTH_2_BYTES;

	return DMA_SLAVE_BUSWIDTH_UNDEFINED;
}

F
Feng Tang 已提交
127
/*
128 129
 * dws->dma_chan_busy is set before the dma transfer starts, callback for tx
 * channel will clear a corresponding bit.
F
Feng Tang 已提交
130
 */
131
static void dw_spi_dma_tx_done(void *arg)
F
Feng Tang 已提交
132 133 134
{
	struct dw_spi *dws = arg;

135 136
	clear_bit(TX_BUSY, &dws->dma_chan_busy);
	if (test_bit(RX_BUSY, &dws->dma_chan_busy))
F
Feng Tang 已提交
137
		return;
138
	spi_finalize_current_transfer(dws->master);
F
Feng Tang 已提交
139 140
}

141 142
static struct dma_async_tx_descriptor *dw_spi_dma_prepare_tx(struct dw_spi *dws,
		struct spi_transfer *xfer)
F
Feng Tang 已提交
143
{
144 145
	struct dma_slave_config txconf;
	struct dma_async_tx_descriptor *txdesc;
F
Feng Tang 已提交
146

147
	if (!xfer->tx_buf)
148 149
		return NULL;

150
	memset(&txconf, 0, sizeof(txconf));
151
	txconf.direction = DMA_MEM_TO_DEV;
F
Feng Tang 已提交
152
	txconf.dst_addr = dws->dma_addr;
153
	txconf.dst_maxburst = 16;
F
Feng Tang 已提交
154
	txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
155
	txconf.dst_addr_width = convert_dma_width(dws->dma_width);
156
	txconf.device_fc = false;
F
Feng Tang 已提交
157

158
	dmaengine_slave_config(dws->txchan, &txconf);
F
Feng Tang 已提交
159

160
	txdesc = dmaengine_prep_slave_sg(dws->txchan,
161 162
				xfer->tx_sg.sgl,
				xfer->tx_sg.nents,
163
				DMA_MEM_TO_DEV,
164
				DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
165 166 167
	if (!txdesc)
		return NULL;

168
	txdesc->callback = dw_spi_dma_tx_done;
F
Feng Tang 已提交
169 170
	txdesc->callback_param = dws;

171 172 173
	return txdesc;
}

174 175 176 177 178 179 180 181
/*
 * dws->dma_chan_busy is set before the dma transfer starts, callback for rx
 * channel will clear a corresponding bit.
 */
static void dw_spi_dma_rx_done(void *arg)
{
	struct dw_spi *dws = arg;

182 183
	clear_bit(RX_BUSY, &dws->dma_chan_busy);
	if (test_bit(TX_BUSY, &dws->dma_chan_busy))
184
		return;
185
	spi_finalize_current_transfer(dws->master);
186 187
}

188 189
static struct dma_async_tx_descriptor *dw_spi_dma_prepare_rx(struct dw_spi *dws,
		struct spi_transfer *xfer)
190 191 192 193
{
	struct dma_slave_config rxconf;
	struct dma_async_tx_descriptor *rxdesc;

194
	if (!xfer->rx_buf)
195 196
		return NULL;

197
	memset(&rxconf, 0, sizeof(rxconf));
198
	rxconf.direction = DMA_DEV_TO_MEM;
F
Feng Tang 已提交
199
	rxconf.src_addr = dws->dma_addr;
200
	rxconf.src_maxburst = 16;
F
Feng Tang 已提交
201
	rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
202
	rxconf.src_addr_width = convert_dma_width(dws->dma_width);
203
	rxconf.device_fc = false;
F
Feng Tang 已提交
204

205
	dmaengine_slave_config(dws->rxchan, &rxconf);
F
Feng Tang 已提交
206

207
	rxdesc = dmaengine_prep_slave_sg(dws->rxchan,
208 209
				xfer->rx_sg.sgl,
				xfer->rx_sg.nents,
210
				DMA_DEV_TO_MEM,
211
				DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
212 213 214
	if (!rxdesc)
		return NULL;

215
	rxdesc->callback = dw_spi_dma_rx_done;
F
Feng Tang 已提交
216 217
	rxdesc->callback_param = dws;

218 219 220
	return rxdesc;
}

221
static int mid_spi_dma_setup(struct dw_spi *dws, struct spi_transfer *xfer)
222 223 224
{
	u16 dma_ctrl = 0;

225 226
	dw_writel(dws, DW_SPI_DMARDLR, 0xf);
	dw_writel(dws, DW_SPI_DMATDLR, 0x10);
227

228
	if (xfer->tx_buf)
229
		dma_ctrl |= SPI_DMA_TDMAE;
230
	if (xfer->rx_buf)
231
		dma_ctrl |= SPI_DMA_RDMAE;
232
	dw_writel(dws, DW_SPI_DMACR, dma_ctrl);
233

234 235 236 237 238
	/* Set the interrupt mask */
	spi_umask_intr(dws, SPI_INT_TXOI | SPI_INT_RXUI | SPI_INT_RXOI);

	dws->transfer_handler = dma_transfer;

239
	return 0;
240 241
}

242
static int mid_spi_dma_transfer(struct dw_spi *dws, struct spi_transfer *xfer)
243 244 245
{
	struct dma_async_tx_descriptor *txdesc, *rxdesc;

246
	/* Prepare the TX dma transfer */
247
	txdesc = dw_spi_dma_prepare_tx(dws, xfer);
248

249
	/* Prepare the RX dma transfer */
250
	rxdesc = dw_spi_dma_prepare_rx(dws, xfer);
251

F
Feng Tang 已提交
252
	/* rx must be started before tx due to spi instinct */
253 254 255 256 257 258 259 260 261 262 263
	if (rxdesc) {
		set_bit(RX_BUSY, &dws->dma_chan_busy);
		dmaengine_submit(rxdesc);
		dma_async_issue_pending(dws->rxchan);
	}

	if (txdesc) {
		set_bit(TX_BUSY, &dws->dma_chan_busy);
		dmaengine_submit(txdesc);
		dma_async_issue_pending(dws->txchan);
	}
264

F
Feng Tang 已提交
265 266 267
	return 0;
}

268 269 270
static void mid_spi_dma_stop(struct dw_spi *dws)
{
	if (test_bit(TX_BUSY, &dws->dma_chan_busy)) {
271
		dmaengine_terminate_sync(dws->txchan);
272 273 274
		clear_bit(TX_BUSY, &dws->dma_chan_busy);
	}
	if (test_bit(RX_BUSY, &dws->dma_chan_busy)) {
275
		dmaengine_terminate_sync(dws->rxchan);
276 277 278 279
		clear_bit(RX_BUSY, &dws->dma_chan_busy);
	}
}

280
static const struct dw_spi_dma_ops mid_dma_ops = {
F
Feng Tang 已提交
281 282
	.dma_init	= mid_spi_dma_init,
	.dma_exit	= mid_spi_dma_exit,
283
	.dma_setup	= mid_spi_dma_setup,
284
	.can_dma	= mid_spi_can_dma,
F
Feng Tang 已提交
285
	.dma_transfer	= mid_spi_dma_transfer,
286
	.dma_stop	= mid_spi_dma_stop,
F
Feng Tang 已提交
287 288 289
};
#endif

290
/* Some specific info for SPI0 controller on Intel MID */
F
Feng Tang 已提交
291

292
/* HW info for MRST Clk Control Unit, 32b reg per controller */
F
Feng Tang 已提交
293
#define MRST_SPI_CLK_BASE	100000000	/* 100m */
294
#define MRST_CLK_SPI_REG	0xff11d86c
F
Feng Tang 已提交
295 296 297 298 299 300 301 302
#define CLK_SPI_BDIV_OFFSET	0
#define CLK_SPI_BDIV_MASK	0x00000007
#define CLK_SPI_CDIV_OFFSET	9
#define CLK_SPI_CDIV_MASK	0x00000e00
#define CLK_SPI_DISABLE_OFFSET	8

int dw_spi_mid_init(struct dw_spi *dws)
{
303 304
	void __iomem *clk_reg;
	u32 clk_cdiv;
F
Feng Tang 已提交
305

306
	clk_reg = ioremap(MRST_CLK_SPI_REG, 16);
F
Feng Tang 已提交
307 308 309
	if (!clk_reg)
		return -ENOMEM;

310 311 312 313
	/* Get SPI controller operating freq info */
	clk_cdiv = readl(clk_reg + dws->bus_num * sizeof(u32));
	clk_cdiv &= CLK_SPI_CDIV_MASK;
	clk_cdiv >>= CLK_SPI_CDIV_OFFSET;
F
Feng Tang 已提交
314
	dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1);
315

F
Feng Tang 已提交
316 317 318
	iounmap(clk_reg);

#ifdef CONFIG_SPI_DW_MID_DMA
319 320
	dws->dma_tx = &mid_dma_tx;
	dws->dma_rx = &mid_dma_rx;
F
Feng Tang 已提交
321 322
	dws->dma_ops = &mid_dma_ops;
#endif
323 324 325 326

	/* Register hook to configure CTRLR0 */
	dws->update_cr0 = dw_spi_update_cr0;

F
Feng Tang 已提交
327 328
	return 0;
}