spi-dw-mid.c 7.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 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
}

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

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

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

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

	if (!irq_status)
		return IRQ_NONE;

97
	dw_readl(dws, DW_SPI_ICR);
98 99 100 101 102 103 104 105
	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;
}

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

	if (!dws->dma_inited)
		return false;

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

117 118 119 120 121 122 123 124 125
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 已提交
126
/*
127 128
 * dws->dma_chan_busy is set before the dma transfer starts, callback for tx
 * channel will clear a corresponding bit.
F
Feng Tang 已提交
129
 */
130
static void dw_spi_dma_tx_done(void *arg)
F
Feng Tang 已提交
131 132 133
{
	struct dw_spi *dws = arg;

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

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

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

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

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

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

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

170 171 172
	return txdesc;
}

173 174 175 176 177 178 179 180
/*
 * 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;

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

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

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

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

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

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

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

217 218 219
	return rxdesc;
}

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

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

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

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

	dws->transfer_handler = dma_transfer;

238
	return 0;
239 240
}

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

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

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

F
Feng Tang 已提交
251
	/* rx must be started before tx due to spi instinct */
252 253 254 255 256 257 258 259 260 261 262
	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);
	}
263

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

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

279 280
static const struct dw_spi_dma_ops mfld_dma_ops = {
	.dma_init	= mid_spi_dma_init_mfld,
F
Feng Tang 已提交
281
	.dma_exit	= mid_spi_dma_exit,
282
	.dma_setup	= mid_spi_dma_setup,
283
	.can_dma	= mid_spi_can_dma,
F
Feng Tang 已提交
284
	.dma_transfer	= mid_spi_dma_transfer,
285
	.dma_stop	= mid_spi_dma_stop,
F
Feng Tang 已提交
286
};
287 288 289 290 291 292 293 294 295

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;
}
#else	/* CONFIG_SPI_DW_MID_DMA */
static inline void dw_spi_mid_setup_dma_mfld(struct dw_spi *dws) {}
F
Feng Tang 已提交
296 297
#endif

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

300
/* HW info for MRST Clk Control Unit, 32b reg per controller */
F
Feng Tang 已提交
301
#define MRST_SPI_CLK_BASE	100000000	/* 100m */
302
#define MRST_CLK_SPI_REG	0xff11d86c
F
Feng Tang 已提交
303 304 305 306 307 308
#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

309
int dw_spi_mid_init_mfld(struct dw_spi *dws)
F
Feng Tang 已提交
310
{
311 312
	void __iomem *clk_reg;
	u32 clk_cdiv;
F
Feng Tang 已提交
313

314
	clk_reg = ioremap(MRST_CLK_SPI_REG, 16);
F
Feng Tang 已提交
315 316 317
	if (!clk_reg)
		return -ENOMEM;

318 319 320 321
	/* 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 已提交
322
	dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1);
323

F
Feng Tang 已提交
324 325
	iounmap(clk_reg);

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

329
	dw_spi_mid_setup_dma_mfld(dws);
F
Feng Tang 已提交
330 331
	return 0;
}