spi-dw-mid.c 8.8 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
 */

#include <linux/spi/spi.h>
9
#include <linux/types.h>
10

G
Grant Likely 已提交
11
#include "spi-dw.h"
F
Feng Tang 已提交
12 13

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

20 21 22
#define RX_BUSY		0
#define TX_BUSY		1

23 24
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 已提交
25 26 27

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

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

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

37
static int mid_spi_dma_init_mfld(struct device *dev, struct dw_spi *dws)
F
Feng Tang 已提交
38
{
39
	struct pci_dev *dma_dev;
40 41
	struct dw_dma_slave *tx = dws->dma_tx;
	struct dw_dma_slave *rx = dws->dma_rx;
F
Feng Tang 已提交
42 43 44 45
	dma_cap_mask_t mask;

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

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

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

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

	dws->dma_inited = 1;
	return 0;

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

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
static int mid_spi_dma_init_generic(struct device *dev, struct dw_spi *dws)
{
	dws->rxchan = dma_request_slave_channel(dev, "rx");
	if (!dws->rxchan)
		return -ENODEV;
	dws->master->dma_rx = dws->rxchan;

	dws->txchan = dma_request_slave_channel(dev, "tx");
	if (!dws->txchan) {
		dma_release_channel(dws->rxchan);
		return -ENODEV;
	}
	dws->master->dma_tx = dws->txchan;

	dws->dma_inited = 1;
	return 0;
}

F
Feng Tang 已提交
96 97
static void mid_spi_dma_exit(struct dw_spi *dws)
{
98 99
	if (!dws->dma_inited)
		return;
100

101
	dmaengine_terminate_sync(dws->txchan);
F
Feng Tang 已提交
102
	dma_release_channel(dws->txchan);
103

104
	dmaengine_terminate_sync(dws->rxchan);
F
Feng Tang 已提交
105 106 107
	dma_release_channel(dws->rxchan);
}

108 109
static irqreturn_t dma_transfer(struct dw_spi *dws)
{
110
	u16 irq_status = dw_readl(dws, DW_SPI_ISR);
111 112 113 114

	if (!irq_status)
		return IRQ_NONE;

115
	dw_readl(dws, DW_SPI_ICR);
116 117 118 119 120 121 122 123
	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;
}

124 125
static bool mid_spi_can_dma(struct spi_controller *master,
		struct spi_device *spi, struct spi_transfer *xfer)
126
{
127
	struct dw_spi *dws = spi_controller_get_devdata(master);
128 129 130 131 132 133 134

	if (!dws->dma_inited)
		return false;

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

135 136 137 138 139 140 141 142 143
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 已提交
144
/*
145 146
 * dws->dma_chan_busy is set before the dma transfer starts, callback for tx
 * channel will clear a corresponding bit.
F
Feng Tang 已提交
147
 */
148
static void dw_spi_dma_tx_done(void *arg)
F
Feng Tang 已提交
149 150 151
{
	struct dw_spi *dws = arg;

152 153
	clear_bit(TX_BUSY, &dws->dma_chan_busy);
	if (test_bit(RX_BUSY, &dws->dma_chan_busy))
F
Feng Tang 已提交
154
		return;
155
	spi_finalize_current_transfer(dws->master);
F
Feng Tang 已提交
156 157
}

158 159
static struct dma_async_tx_descriptor *dw_spi_dma_prepare_tx(struct dw_spi *dws,
		struct spi_transfer *xfer)
F
Feng Tang 已提交
160
{
161 162
	struct dma_slave_config txconf;
	struct dma_async_tx_descriptor *txdesc;
F
Feng Tang 已提交
163

164
	if (!xfer->tx_buf)
165 166
		return NULL;

167
	memset(&txconf, 0, sizeof(txconf));
168
	txconf.direction = DMA_MEM_TO_DEV;
F
Feng Tang 已提交
169
	txconf.dst_addr = dws->dma_addr;
170
	txconf.dst_maxburst = 16;
F
Feng Tang 已提交
171
	txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
172
	txconf.dst_addr_width = convert_dma_width(dws->dma_width);
173
	txconf.device_fc = false;
F
Feng Tang 已提交
174

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

177
	txdesc = dmaengine_prep_slave_sg(dws->txchan,
178 179
				xfer->tx_sg.sgl,
				xfer->tx_sg.nents,
180
				DMA_MEM_TO_DEV,
181
				DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
182 183 184
	if (!txdesc)
		return NULL;

185
	txdesc->callback = dw_spi_dma_tx_done;
F
Feng Tang 已提交
186 187
	txdesc->callback_param = dws;

188 189 190
	return txdesc;
}

191 192 193 194 195 196 197 198
/*
 * 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;

199 200
	clear_bit(RX_BUSY, &dws->dma_chan_busy);
	if (test_bit(TX_BUSY, &dws->dma_chan_busy))
201
		return;
202
	spi_finalize_current_transfer(dws->master);
203 204
}

205 206
static struct dma_async_tx_descriptor *dw_spi_dma_prepare_rx(struct dw_spi *dws,
		struct spi_transfer *xfer)
207 208 209 210
{
	struct dma_slave_config rxconf;
	struct dma_async_tx_descriptor *rxdesc;

211
	if (!xfer->rx_buf)
212 213
		return NULL;

214
	memset(&rxconf, 0, sizeof(rxconf));
215
	rxconf.direction = DMA_DEV_TO_MEM;
F
Feng Tang 已提交
216
	rxconf.src_addr = dws->dma_addr;
217
	rxconf.src_maxburst = 16;
F
Feng Tang 已提交
218
	rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
219
	rxconf.src_addr_width = convert_dma_width(dws->dma_width);
220
	rxconf.device_fc = false;
F
Feng Tang 已提交
221

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

224
	rxdesc = dmaengine_prep_slave_sg(dws->rxchan,
225 226
				xfer->rx_sg.sgl,
				xfer->rx_sg.nents,
227
				DMA_DEV_TO_MEM,
228
				DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
229 230 231
	if (!rxdesc)
		return NULL;

232
	rxdesc->callback = dw_spi_dma_rx_done;
F
Feng Tang 已提交
233 234
	rxdesc->callback_param = dws;

235 236 237
	return rxdesc;
}

238
static int mid_spi_dma_setup(struct dw_spi *dws, struct spi_transfer *xfer)
239 240 241
{
	u16 dma_ctrl = 0;

242 243
	dw_writel(dws, DW_SPI_DMARDLR, 0xf);
	dw_writel(dws, DW_SPI_DMATDLR, 0x10);
244

245
	if (xfer->tx_buf)
246
		dma_ctrl |= SPI_DMA_TDMAE;
247
	if (xfer->rx_buf)
248
		dma_ctrl |= SPI_DMA_RDMAE;
249
	dw_writel(dws, DW_SPI_DMACR, dma_ctrl);
250

251 252 253 254 255
	/* Set the interrupt mask */
	spi_umask_intr(dws, SPI_INT_TXOI | SPI_INT_RXUI | SPI_INT_RXOI);

	dws->transfer_handler = dma_transfer;

256
	return 0;
257 258
}

259
static int mid_spi_dma_transfer(struct dw_spi *dws, struct spi_transfer *xfer)
260 261 262
{
	struct dma_async_tx_descriptor *txdesc, *rxdesc;

263
	/* Prepare the TX dma transfer */
264
	txdesc = dw_spi_dma_prepare_tx(dws, xfer);
265

266
	/* Prepare the RX dma transfer */
267
	rxdesc = dw_spi_dma_prepare_rx(dws, xfer);
268

F
Feng Tang 已提交
269
	/* rx must be started before tx due to spi instinct */
270 271 272 273 274 275 276 277 278 279 280
	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);
	}
281

F
Feng Tang 已提交
282 283 284
	return 0;
}

285 286 287
static void mid_spi_dma_stop(struct dw_spi *dws)
{
	if (test_bit(TX_BUSY, &dws->dma_chan_busy)) {
288
		dmaengine_terminate_sync(dws->txchan);
289 290 291
		clear_bit(TX_BUSY, &dws->dma_chan_busy);
	}
	if (test_bit(RX_BUSY, &dws->dma_chan_busy)) {
292
		dmaengine_terminate_sync(dws->rxchan);
293 294 295 296
		clear_bit(RX_BUSY, &dws->dma_chan_busy);
	}
}

297 298
static const struct dw_spi_dma_ops mfld_dma_ops = {
	.dma_init	= mid_spi_dma_init_mfld,
F
Feng Tang 已提交
299
	.dma_exit	= mid_spi_dma_exit,
300
	.dma_setup	= mid_spi_dma_setup,
301
	.can_dma	= mid_spi_can_dma,
F
Feng Tang 已提交
302
	.dma_transfer	= mid_spi_dma_transfer,
303
	.dma_stop	= mid_spi_dma_stop,
F
Feng Tang 已提交
304
};
305 306 307 308 309 310 311

static void dw_spi_mid_setup_dma_mfld(struct dw_spi *dws)
{
	dws->dma_tx = &mid_dma_tx;
	dws->dma_rx = &mid_dma_rx;
	dws->dma_ops = &mfld_dma_ops;
}
312 313 314 315 316 317 318 319 320 321 322 323 324 325

static const struct dw_spi_dma_ops generic_dma_ops = {
	.dma_init	= mid_spi_dma_init_generic,
	.dma_exit	= mid_spi_dma_exit,
	.dma_setup	= mid_spi_dma_setup,
	.can_dma	= mid_spi_can_dma,
	.dma_transfer	= mid_spi_dma_transfer,
	.dma_stop	= mid_spi_dma_stop,
};

static void dw_spi_mid_setup_dma_generic(struct dw_spi *dws)
{
	dws->dma_ops = &generic_dma_ops;
}
326 327
#else	/* CONFIG_SPI_DW_MID_DMA */
static inline void dw_spi_mid_setup_dma_mfld(struct dw_spi *dws) {}
328
static inline void dw_spi_mid_setup_dma_generic(struct dw_spi *dws) {}
F
Feng Tang 已提交
329 330
#endif

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

333
/* HW info for MRST Clk Control Unit, 32b reg per controller */
F
Feng Tang 已提交
334
#define MRST_SPI_CLK_BASE	100000000	/* 100m */
335
#define MRST_CLK_SPI_REG	0xff11d86c
F
Feng Tang 已提交
336 337 338 339 340 341
#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

342
int dw_spi_mid_init_mfld(struct dw_spi *dws)
F
Feng Tang 已提交
343
{
344 345
	void __iomem *clk_reg;
	u32 clk_cdiv;
F
Feng Tang 已提交
346

347
	clk_reg = ioremap(MRST_CLK_SPI_REG, 16);
F
Feng Tang 已提交
348 349 350
	if (!clk_reg)
		return -ENOMEM;

351 352 353 354
	/* 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 已提交
355
	dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1);
356

F
Feng Tang 已提交
357 358
	iounmap(clk_reg);

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

362
	dw_spi_mid_setup_dma_mfld(dws);
F
Feng Tang 已提交
363 364
	return 0;
}
365 366 367 368 369 370 371 372 373

int dw_spi_mid_init_generic(struct dw_spi *dws)
{
	/* Register hook to configure CTRLR0 */
	dws->update_cr0 = dw_spi_update_cr0;

	dw_spi_mid_setup_dma_generic(dws);
	return 0;
}