xref: /openbmc/linux/drivers/spi/spi-ep93xx.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely  * Driver for Cirrus Logic EP93xx SPI controller.
4ca632f55SGrant Likely  *
5e4c8308cSGrant Likely  * Copyright (C) 2010-2011 Mika Westerberg
6ca632f55SGrant Likely  *
7ca632f55SGrant Likely  * Explicit FIFO handling code was inspired by amba-pl022 driver.
8ca632f55SGrant Likely  *
9ca632f55SGrant Likely  * Chip select support using other than built-in GPIOs by H. Hartley Sweeten.
10ca632f55SGrant Likely  *
11ca632f55SGrant Likely  * For more information about the SPI controller see documentation on Cirrus
12ca632f55SGrant Likely  * Logic web site:
133ea4eac3SAlexander A. Klimov  *     https://www.cirrus.com/en/pubs/manual/EP93xx_Users_Guide_UM1.pdf
14ca632f55SGrant Likely  */
15ca632f55SGrant Likely 
16ca632f55SGrant Likely #include <linux/io.h>
17ca632f55SGrant Likely #include <linux/clk.h>
18ca632f55SGrant Likely #include <linux/err.h>
19ca632f55SGrant Likely #include <linux/delay.h>
20ca632f55SGrant Likely #include <linux/device.h>
21e4c8308cSGrant Likely #include <linux/dmaengine.h>
22ca632f55SGrant Likely #include <linux/bitops.h>
23ca632f55SGrant Likely #include <linux/interrupt.h>
245bdb7613SMika Westerberg #include <linux/module.h>
25ca632f55SGrant Likely #include <linux/platform_device.h>
26ca632f55SGrant Likely #include <linux/sched.h>
27e4c8308cSGrant Likely #include <linux/scatterlist.h>
28ca632f55SGrant Likely #include <linux/spi/spi.h>
29ca632f55SGrant Likely 
30a3b29245SArnd Bergmann #include <linux/platform_data/dma-ep93xx.h>
31a3b29245SArnd Bergmann #include <linux/platform_data/spi-ep93xx.h>
32ca632f55SGrant Likely 
33ca632f55SGrant Likely #define SSPCR0			0x0000
3461249ce0SJungseung Lee #define SSPCR0_SPO		BIT(6)
3561249ce0SJungseung Lee #define SSPCR0_SPH		BIT(7)
36ca632f55SGrant Likely #define SSPCR0_SCR_SHIFT	8
37ca632f55SGrant Likely 
38ca632f55SGrant Likely #define SSPCR1			0x0004
39ca632f55SGrant Likely #define SSPCR1_RIE		BIT(0)
40ca632f55SGrant Likely #define SSPCR1_TIE		BIT(1)
41ca632f55SGrant Likely #define SSPCR1_RORIE		BIT(2)
42ca632f55SGrant Likely #define SSPCR1_LBM		BIT(3)
43ca632f55SGrant Likely #define SSPCR1_SSE		BIT(4)
44ca632f55SGrant Likely #define SSPCR1_MS		BIT(5)
45ca632f55SGrant Likely #define SSPCR1_SOD		BIT(6)
46ca632f55SGrant Likely 
47ca632f55SGrant Likely #define SSPDR			0x0008
48ca632f55SGrant Likely 
49ca632f55SGrant Likely #define SSPSR			0x000c
50ca632f55SGrant Likely #define SSPSR_TFE		BIT(0)
51ca632f55SGrant Likely #define SSPSR_TNF		BIT(1)
52ca632f55SGrant Likely #define SSPSR_RNE		BIT(2)
53ca632f55SGrant Likely #define SSPSR_RFF		BIT(3)
54ca632f55SGrant Likely #define SSPSR_BSY		BIT(4)
55ca632f55SGrant Likely #define SSPCPSR			0x0010
56ca632f55SGrant Likely 
57ca632f55SGrant Likely #define SSPIIR			0x0014
58ca632f55SGrant Likely #define SSPIIR_RIS		BIT(0)
59ca632f55SGrant Likely #define SSPIIR_TIS		BIT(1)
60ca632f55SGrant Likely #define SSPIIR_RORIS		BIT(2)
61ca632f55SGrant Likely #define SSPICR			SSPIIR
62ca632f55SGrant Likely 
63ca632f55SGrant Likely /* timeout in milliseconds */
64ca632f55SGrant Likely #define SPI_TIMEOUT		5
65ca632f55SGrant Likely /* maximum depth of RX/TX FIFO */
66ca632f55SGrant Likely #define SPI_FIFO_SIZE		8
67ca632f55SGrant Likely 
68ca632f55SGrant Likely /**
69ca632f55SGrant Likely  * struct ep93xx_spi - EP93xx SPI controller structure
70ca632f55SGrant Likely  * @clk: clock for the controller
711232978aSH Hartley Sweeten  * @mmio: pointer to ioremap()'d registers
72e4c8308cSGrant Likely  * @sspdr_phys: physical address of the SSPDR register
73ca632f55SGrant Likely  * @tx: current byte in transfer to transmit
74ca632f55SGrant Likely  * @rx: current byte in transfer to receive
75ca632f55SGrant Likely  * @fifo_level: how full is FIFO (%0..%SPI_FIFO_SIZE - %1). Receiving one
76ca632f55SGrant Likely  *              frame decreases this level and sending one frame increases it.
77e4c8308cSGrant Likely  * @dma_rx: RX DMA channel
78e4c8308cSGrant Likely  * @dma_tx: TX DMA channel
79e4c8308cSGrant Likely  * @dma_rx_data: RX parameters passed to the DMA engine
80e4c8308cSGrant Likely  * @dma_tx_data: TX parameters passed to the DMA engine
81e4c8308cSGrant Likely  * @rx_sgt: sg table for RX transfers
82e4c8308cSGrant Likely  * @tx_sgt: sg table for TX transfers
83e4c8308cSGrant Likely  * @zeropage: dummy page used as RX buffer when only TX buffer is passed in by
84e4c8308cSGrant Likely  *            the client
85ca632f55SGrant Likely  */
86ca632f55SGrant Likely struct ep93xx_spi {
87ca632f55SGrant Likely 	struct clk			*clk;
881232978aSH Hartley Sweeten 	void __iomem			*mmio;
89e4c8308cSGrant Likely 	unsigned long			sspdr_phys;
90ca632f55SGrant Likely 	size_t				tx;
91ca632f55SGrant Likely 	size_t				rx;
92ca632f55SGrant Likely 	size_t				fifo_level;
93e4c8308cSGrant Likely 	struct dma_chan			*dma_rx;
94e4c8308cSGrant Likely 	struct dma_chan			*dma_tx;
95e4c8308cSGrant Likely 	struct ep93xx_dma_data		dma_rx_data;
96e4c8308cSGrant Likely 	struct ep93xx_dma_data		dma_tx_data;
97e4c8308cSGrant Likely 	struct sg_table			rx_sgt;
98e4c8308cSGrant Likely 	struct sg_table			tx_sgt;
99e4c8308cSGrant Likely 	void				*zeropage;
100ca632f55SGrant Likely };
101ca632f55SGrant Likely 
102ca632f55SGrant Likely /* converts bits per word to CR0.DSS value */
103ca632f55SGrant Likely #define bits_per_word_to_dss(bpw)	((bpw) - 1)
104ca632f55SGrant Likely 
105ca632f55SGrant Likely /**
106ca632f55SGrant Likely  * ep93xx_spi_calc_divisors() - calculates SPI clock divisors
107*24e9b75cSYang Yingliang  * @host: SPI host
108ca632f55SGrant Likely  * @rate: desired SPI output clock rate
109f7ef1da9SH Hartley Sweeten  * @div_cpsr: pointer to return the cpsr (pre-scaler) divider
110f7ef1da9SH Hartley Sweeten  * @div_scr: pointer to return the scr divider
111ca632f55SGrant Likely  */
ep93xx_spi_calc_divisors(struct spi_controller * host,u32 rate,u8 * div_cpsr,u8 * div_scr)112*24e9b75cSYang Yingliang static int ep93xx_spi_calc_divisors(struct spi_controller *host,
11356fc0b42SAxel Lin 				    u32 rate, u8 *div_cpsr, u8 *div_scr)
114ca632f55SGrant Likely {
115*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
116ca632f55SGrant Likely 	unsigned long spi_clk_rate = clk_get_rate(espi->clk);
117ca632f55SGrant Likely 	int cpsr, scr;
118ca632f55SGrant Likely 
119ca632f55SGrant Likely 	/*
120ca632f55SGrant Likely 	 * Make sure that max value is between values supported by the
121d9a01771SH Hartley Sweeten 	 * controller.
122ca632f55SGrant Likely 	 */
123*24e9b75cSYang Yingliang 	rate = clamp(rate, host->min_speed_hz, host->max_speed_hz);
124ca632f55SGrant Likely 
125ca632f55SGrant Likely 	/*
126ca632f55SGrant Likely 	 * Calculate divisors so that we can get speed according the
127ca632f55SGrant Likely 	 * following formula:
128ca632f55SGrant Likely 	 *	rate = spi_clock_rate / (cpsr * (1 + scr))
129ca632f55SGrant Likely 	 *
130ca632f55SGrant Likely 	 * cpsr must be even number and starts from 2, scr can be any number
131ca632f55SGrant Likely 	 * between 0 and 255.
132ca632f55SGrant Likely 	 */
133ca632f55SGrant Likely 	for (cpsr = 2; cpsr <= 254; cpsr += 2) {
134ca632f55SGrant Likely 		for (scr = 0; scr <= 255; scr++) {
135ca632f55SGrant Likely 			if ((spi_clk_rate / (cpsr * (scr + 1))) <= rate) {
136f7ef1da9SH Hartley Sweeten 				*div_scr = (u8)scr;
137f7ef1da9SH Hartley Sweeten 				*div_cpsr = (u8)cpsr;
138ca632f55SGrant Likely 				return 0;
139ca632f55SGrant Likely 			}
140ca632f55SGrant Likely 		}
141ca632f55SGrant Likely 	}
142ca632f55SGrant Likely 
143ca632f55SGrant Likely 	return -EINVAL;
144ca632f55SGrant Likely }
145ca632f55SGrant Likely 
ep93xx_spi_chip_setup(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * xfer)146*24e9b75cSYang Yingliang static int ep93xx_spi_chip_setup(struct spi_controller *host,
14755f0cd3fSH Hartley Sweeten 				 struct spi_device *spi,
14855f0cd3fSH Hartley Sweeten 				 struct spi_transfer *xfer)
149ca632f55SGrant Likely {
150*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
15155f0cd3fSH Hartley Sweeten 	u8 dss = bits_per_word_to_dss(xfer->bits_per_word);
152f7ef1da9SH Hartley Sweeten 	u8 div_cpsr = 0;
153f7ef1da9SH Hartley Sweeten 	u8 div_scr = 0;
154ca632f55SGrant Likely 	u16 cr0;
155f7ef1da9SH Hartley Sweeten 	int err;
156ca632f55SGrant Likely 
157*24e9b75cSYang Yingliang 	err = ep93xx_spi_calc_divisors(host, xfer->speed_hz,
15855f0cd3fSH Hartley Sweeten 				       &div_cpsr, &div_scr);
159f7ef1da9SH Hartley Sweeten 	if (err)
160f7ef1da9SH Hartley Sweeten 		return err;
161f7ef1da9SH Hartley Sweeten 
162f7ef1da9SH Hartley Sweeten 	cr0 = div_scr << SSPCR0_SCR_SHIFT;
16361249ce0SJungseung Lee 	if (spi->mode & SPI_CPOL)
16461249ce0SJungseung Lee 		cr0 |= SSPCR0_SPO;
16561249ce0SJungseung Lee 	if (spi->mode & SPI_CPHA)
16661249ce0SJungseung Lee 		cr0 |= SSPCR0_SPH;
167d9b65dfdSH Hartley Sweeten 	cr0 |= dss;
168ca632f55SGrant Likely 
169*24e9b75cSYang Yingliang 	dev_dbg(&host->dev, "setup: mode %d, cpsr %d, scr %d, dss %d\n",
17055f0cd3fSH Hartley Sweeten 		spi->mode, div_cpsr, div_scr, dss);
171*24e9b75cSYang Yingliang 	dev_dbg(&host->dev, "setup: cr0 %#x\n", cr0);
172ca632f55SGrant Likely 
1738447e478SH Hartley Sweeten 	writel(div_cpsr, espi->mmio + SSPCPSR);
1748447e478SH Hartley Sweeten 	writel(cr0, espi->mmio + SSPCR0);
175f7ef1da9SH Hartley Sweeten 
176f7ef1da9SH Hartley Sweeten 	return 0;
177ca632f55SGrant Likely }
178ca632f55SGrant Likely 
ep93xx_do_write(struct spi_controller * host)179*24e9b75cSYang Yingliang static void ep93xx_do_write(struct spi_controller *host)
180ca632f55SGrant Likely {
181*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
182*24e9b75cSYang Yingliang 	struct spi_transfer *xfer = host->cur_msg->state;
1838447e478SH Hartley Sweeten 	u32 val = 0;
1848447e478SH Hartley Sweeten 
185d9a01771SH Hartley Sweeten 	if (xfer->bits_per_word > 8) {
186d9a01771SH Hartley Sweeten 		if (xfer->tx_buf)
187d9a01771SH Hartley Sweeten 			val = ((u16 *)xfer->tx_buf)[espi->tx];
1888447e478SH Hartley Sweeten 		espi->tx += 2;
189ca632f55SGrant Likely 	} else {
190d9a01771SH Hartley Sweeten 		if (xfer->tx_buf)
191d9a01771SH Hartley Sweeten 			val = ((u8 *)xfer->tx_buf)[espi->tx];
1928447e478SH Hartley Sweeten 		espi->tx += 1;
193ca632f55SGrant Likely 	}
1948447e478SH Hartley Sweeten 	writel(val, espi->mmio + SSPDR);
195ca632f55SGrant Likely }
196ca632f55SGrant Likely 
ep93xx_do_read(struct spi_controller * host)197*24e9b75cSYang Yingliang static void ep93xx_do_read(struct spi_controller *host)
198ca632f55SGrant Likely {
199*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
200*24e9b75cSYang Yingliang 	struct spi_transfer *xfer = host->cur_msg->state;
2018447e478SH Hartley Sweeten 	u32 val;
2028447e478SH Hartley Sweeten 
2038447e478SH Hartley Sweeten 	val = readl(espi->mmio + SSPDR);
204d9a01771SH Hartley Sweeten 	if (xfer->bits_per_word > 8) {
205d9a01771SH Hartley Sweeten 		if (xfer->rx_buf)
206d9a01771SH Hartley Sweeten 			((u16 *)xfer->rx_buf)[espi->rx] = val;
2078447e478SH Hartley Sweeten 		espi->rx += 2;
208ca632f55SGrant Likely 	} else {
209d9a01771SH Hartley Sweeten 		if (xfer->rx_buf)
210d9a01771SH Hartley Sweeten 			((u8 *)xfer->rx_buf)[espi->rx] = val;
2118447e478SH Hartley Sweeten 		espi->rx += 1;
212ca632f55SGrant Likely 	}
213ca632f55SGrant Likely }
214ca632f55SGrant Likely 
215ca632f55SGrant Likely /**
216ca632f55SGrant Likely  * ep93xx_spi_read_write() - perform next RX/TX transfer
217*24e9b75cSYang Yingliang  * @host: SPI host
218ca632f55SGrant Likely  *
219ca632f55SGrant Likely  * This function transfers next bytes (or half-words) to/from RX/TX FIFOs. If
220ca632f55SGrant Likely  * called several times, the whole transfer will be completed. Returns
221ca632f55SGrant Likely  * %-EINPROGRESS when current transfer was not yet completed otherwise %0.
222ca632f55SGrant Likely  *
223ca632f55SGrant Likely  * When this function is finished, RX FIFO should be empty and TX FIFO should be
224ca632f55SGrant Likely  * full.
225ca632f55SGrant Likely  */
ep93xx_spi_read_write(struct spi_controller * host)226*24e9b75cSYang Yingliang static int ep93xx_spi_read_write(struct spi_controller *host)
227ca632f55SGrant Likely {
228*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
229*24e9b75cSYang Yingliang 	struct spi_transfer *xfer = host->cur_msg->state;
230ca632f55SGrant Likely 
231ca632f55SGrant Likely 	/* read as long as RX FIFO has frames in it */
2328447e478SH Hartley Sweeten 	while ((readl(espi->mmio + SSPSR) & SSPSR_RNE)) {
233*24e9b75cSYang Yingliang 		ep93xx_do_read(host);
234ca632f55SGrant Likely 		espi->fifo_level--;
235ca632f55SGrant Likely 	}
236ca632f55SGrant Likely 
237ca632f55SGrant Likely 	/* write as long as TX FIFO has room */
238d9a01771SH Hartley Sweeten 	while (espi->fifo_level < SPI_FIFO_SIZE && espi->tx < xfer->len) {
239*24e9b75cSYang Yingliang 		ep93xx_do_write(host);
240ca632f55SGrant Likely 		espi->fifo_level++;
241ca632f55SGrant Likely 	}
242ca632f55SGrant Likely 
243d9a01771SH Hartley Sweeten 	if (espi->rx == xfer->len)
244ca632f55SGrant Likely 		return 0;
245ca632f55SGrant Likely 
246ca632f55SGrant Likely 	return -EINPROGRESS;
247ca632f55SGrant Likely }
248ca632f55SGrant Likely 
249a1108c7bSNathan Chancellor static enum dma_transfer_direction
ep93xx_dma_data_to_trans_dir(enum dma_data_direction dir)250a1108c7bSNathan Chancellor ep93xx_dma_data_to_trans_dir(enum dma_data_direction dir)
251a1108c7bSNathan Chancellor {
252a1108c7bSNathan Chancellor 	switch (dir) {
253a1108c7bSNathan Chancellor 	case DMA_TO_DEVICE:
254a1108c7bSNathan Chancellor 		return DMA_MEM_TO_DEV;
255a1108c7bSNathan Chancellor 	case DMA_FROM_DEVICE:
256a1108c7bSNathan Chancellor 		return DMA_DEV_TO_MEM;
257a1108c7bSNathan Chancellor 	default:
258a1108c7bSNathan Chancellor 		return DMA_TRANS_NONE;
259a1108c7bSNathan Chancellor 	}
260a1108c7bSNathan Chancellor }
261a1108c7bSNathan Chancellor 
262e4c8308cSGrant Likely /**
263e4c8308cSGrant Likely  * ep93xx_spi_dma_prepare() - prepares a DMA transfer
264*24e9b75cSYang Yingliang  * @host: SPI host
265e4c8308cSGrant Likely  * @dir: DMA transfer direction
266e4c8308cSGrant Likely  *
267e4c8308cSGrant Likely  * Function configures the DMA, maps the buffer and prepares the DMA
268e4c8308cSGrant Likely  * descriptor. Returns a valid DMA descriptor in case of success and ERR_PTR
269e4c8308cSGrant Likely  * in case of failure.
270e4c8308cSGrant Likely  */
271e4c8308cSGrant Likely static struct dma_async_tx_descriptor *
ep93xx_spi_dma_prepare(struct spi_controller * host,enum dma_data_direction dir)272*24e9b75cSYang Yingliang ep93xx_spi_dma_prepare(struct spi_controller *host,
273a1108c7bSNathan Chancellor 		       enum dma_data_direction dir)
274e4c8308cSGrant Likely {
275*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
276*24e9b75cSYang Yingliang 	struct spi_transfer *xfer = host->cur_msg->state;
277e4c8308cSGrant Likely 	struct dma_async_tx_descriptor *txd;
278e4c8308cSGrant Likely 	enum dma_slave_buswidth buswidth;
279e4c8308cSGrant Likely 	struct dma_slave_config conf;
280e4c8308cSGrant Likely 	struct scatterlist *sg;
281e4c8308cSGrant Likely 	struct sg_table *sgt;
282e4c8308cSGrant Likely 	struct dma_chan *chan;
283e4c8308cSGrant Likely 	const void *buf, *pbuf;
284d9a01771SH Hartley Sweeten 	size_t len = xfer->len;
285e4c8308cSGrant Likely 	int i, ret, nents;
286e4c8308cSGrant Likely 
287d9a01771SH Hartley Sweeten 	if (xfer->bits_per_word > 8)
288e4c8308cSGrant Likely 		buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
289e4c8308cSGrant Likely 	else
290e4c8308cSGrant Likely 		buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
291e4c8308cSGrant Likely 
292e4c8308cSGrant Likely 	memset(&conf, 0, sizeof(conf));
293a1108c7bSNathan Chancellor 	conf.direction = ep93xx_dma_data_to_trans_dir(dir);
294e4c8308cSGrant Likely 
295a1108c7bSNathan Chancellor 	if (dir == DMA_FROM_DEVICE) {
296e4c8308cSGrant Likely 		chan = espi->dma_rx;
297d9a01771SH Hartley Sweeten 		buf = xfer->rx_buf;
298e4c8308cSGrant Likely 		sgt = &espi->rx_sgt;
299e4c8308cSGrant Likely 
300e4c8308cSGrant Likely 		conf.src_addr = espi->sspdr_phys;
301e4c8308cSGrant Likely 		conf.src_addr_width = buswidth;
302e4c8308cSGrant Likely 	} else {
303e4c8308cSGrant Likely 		chan = espi->dma_tx;
304d9a01771SH Hartley Sweeten 		buf = xfer->tx_buf;
305e4c8308cSGrant Likely 		sgt = &espi->tx_sgt;
306e4c8308cSGrant Likely 
307e4c8308cSGrant Likely 		conf.dst_addr = espi->sspdr_phys;
308e4c8308cSGrant Likely 		conf.dst_addr_width = buswidth;
309e4c8308cSGrant Likely 	}
310e4c8308cSGrant Likely 
311e4c8308cSGrant Likely 	ret = dmaengine_slave_config(chan, &conf);
312e4c8308cSGrant Likely 	if (ret)
313e4c8308cSGrant Likely 		return ERR_PTR(ret);
314e4c8308cSGrant Likely 
315e4c8308cSGrant Likely 	/*
316e4c8308cSGrant Likely 	 * We need to split the transfer into PAGE_SIZE'd chunks. This is
317e4c8308cSGrant Likely 	 * because we are using @espi->zeropage to provide a zero RX buffer
318e4c8308cSGrant Likely 	 * for the TX transfers and we have only allocated one page for that.
319e4c8308cSGrant Likely 	 *
320e4c8308cSGrant Likely 	 * For performance reasons we allocate a new sg_table only when
321e4c8308cSGrant Likely 	 * needed. Otherwise we will re-use the current one. Eventually the
322e4c8308cSGrant Likely 	 * last sg_table is released in ep93xx_spi_release_dma().
323e4c8308cSGrant Likely 	 */
324e4c8308cSGrant Likely 
325e4c8308cSGrant Likely 	nents = DIV_ROUND_UP(len, PAGE_SIZE);
326e4c8308cSGrant Likely 	if (nents != sgt->nents) {
327e4c8308cSGrant Likely 		sg_free_table(sgt);
328e4c8308cSGrant Likely 
329e4c8308cSGrant Likely 		ret = sg_alloc_table(sgt, nents, GFP_KERNEL);
330e4c8308cSGrant Likely 		if (ret)
331e4c8308cSGrant Likely 			return ERR_PTR(ret);
332e4c8308cSGrant Likely 	}
333e4c8308cSGrant Likely 
334e4c8308cSGrant Likely 	pbuf = buf;
335e4c8308cSGrant Likely 	for_each_sg(sgt->sgl, sg, sgt->nents, i) {
336e4c8308cSGrant Likely 		size_t bytes = min_t(size_t, len, PAGE_SIZE);
337e4c8308cSGrant Likely 
338e4c8308cSGrant Likely 		if (buf) {
339e4c8308cSGrant Likely 			sg_set_page(sg, virt_to_page(pbuf), bytes,
340e4c8308cSGrant Likely 				    offset_in_page(pbuf));
341e4c8308cSGrant Likely 		} else {
342e4c8308cSGrant Likely 			sg_set_page(sg, virt_to_page(espi->zeropage),
343e4c8308cSGrant Likely 				    bytes, 0);
344e4c8308cSGrant Likely 		}
345e4c8308cSGrant Likely 
346e4c8308cSGrant Likely 		pbuf += bytes;
347e4c8308cSGrant Likely 		len -= bytes;
348e4c8308cSGrant Likely 	}
349e4c8308cSGrant Likely 
350e4c8308cSGrant Likely 	if (WARN_ON(len)) {
351*24e9b75cSYang Yingliang 		dev_warn(&host->dev, "len = %zu expected 0!\n", len);
352e4c8308cSGrant Likely 		return ERR_PTR(-EINVAL);
353e4c8308cSGrant Likely 	}
354e4c8308cSGrant Likely 
355e4c8308cSGrant Likely 	nents = dma_map_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
356e4c8308cSGrant Likely 	if (!nents)
357e4c8308cSGrant Likely 		return ERR_PTR(-ENOMEM);
358e4c8308cSGrant Likely 
359a1108c7bSNathan Chancellor 	txd = dmaengine_prep_slave_sg(chan, sgt->sgl, nents, conf.direction,
360a1108c7bSNathan Chancellor 				      DMA_CTRL_ACK);
361e4c8308cSGrant Likely 	if (!txd) {
362e4c8308cSGrant Likely 		dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
363e4c8308cSGrant Likely 		return ERR_PTR(-ENOMEM);
364e4c8308cSGrant Likely 	}
365e4c8308cSGrant Likely 	return txd;
366e4c8308cSGrant Likely }
367e4c8308cSGrant Likely 
368e4c8308cSGrant Likely /**
369e4c8308cSGrant Likely  * ep93xx_spi_dma_finish() - finishes with a DMA transfer
370*24e9b75cSYang Yingliang  * @host: SPI host
371e4c8308cSGrant Likely  * @dir: DMA transfer direction
372e4c8308cSGrant Likely  *
373e4c8308cSGrant Likely  * Function finishes with the DMA transfer. After this, the DMA buffer is
374e4c8308cSGrant Likely  * unmapped.
375e4c8308cSGrant Likely  */
ep93xx_spi_dma_finish(struct spi_controller * host,enum dma_data_direction dir)376*24e9b75cSYang Yingliang static void ep93xx_spi_dma_finish(struct spi_controller *host,
377a1108c7bSNathan Chancellor 				  enum dma_data_direction dir)
378e4c8308cSGrant Likely {
379*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
380e4c8308cSGrant Likely 	struct dma_chan *chan;
381e4c8308cSGrant Likely 	struct sg_table *sgt;
382e4c8308cSGrant Likely 
383a1108c7bSNathan Chancellor 	if (dir == DMA_FROM_DEVICE) {
384e4c8308cSGrant Likely 		chan = espi->dma_rx;
385e4c8308cSGrant Likely 		sgt = &espi->rx_sgt;
386e4c8308cSGrant Likely 	} else {
387e4c8308cSGrant Likely 		chan = espi->dma_tx;
388e4c8308cSGrant Likely 		sgt = &espi->tx_sgt;
389e4c8308cSGrant Likely 	}
390e4c8308cSGrant Likely 
391e4c8308cSGrant Likely 	dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
392e4c8308cSGrant Likely }
393e4c8308cSGrant Likely 
ep93xx_spi_dma_callback(void * callback_param)394e4c8308cSGrant Likely static void ep93xx_spi_dma_callback(void *callback_param)
395e4c8308cSGrant Likely {
396*24e9b75cSYang Yingliang 	struct spi_controller *host = callback_param;
397d9a01771SH Hartley Sweeten 
398*24e9b75cSYang Yingliang 	ep93xx_spi_dma_finish(host, DMA_TO_DEVICE);
399*24e9b75cSYang Yingliang 	ep93xx_spi_dma_finish(host, DMA_FROM_DEVICE);
400d9a01771SH Hartley Sweeten 
401*24e9b75cSYang Yingliang 	spi_finalize_current_transfer(host);
402e4c8308cSGrant Likely }
403e4c8308cSGrant Likely 
ep93xx_spi_dma_transfer(struct spi_controller * host)404*24e9b75cSYang Yingliang static int ep93xx_spi_dma_transfer(struct spi_controller *host)
405e4c8308cSGrant Likely {
406*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
407e4c8308cSGrant Likely 	struct dma_async_tx_descriptor *rxd, *txd;
408e4c8308cSGrant Likely 
409*24e9b75cSYang Yingliang 	rxd = ep93xx_spi_dma_prepare(host, DMA_FROM_DEVICE);
410e4c8308cSGrant Likely 	if (IS_ERR(rxd)) {
411*24e9b75cSYang Yingliang 		dev_err(&host->dev, "DMA RX failed: %ld\n", PTR_ERR(rxd));
412d9a01771SH Hartley Sweeten 		return PTR_ERR(rxd);
413e4c8308cSGrant Likely 	}
414e4c8308cSGrant Likely 
415*24e9b75cSYang Yingliang 	txd = ep93xx_spi_dma_prepare(host, DMA_TO_DEVICE);
416e4c8308cSGrant Likely 	if (IS_ERR(txd)) {
417*24e9b75cSYang Yingliang 		ep93xx_spi_dma_finish(host, DMA_FROM_DEVICE);
418*24e9b75cSYang Yingliang 		dev_err(&host->dev, "DMA TX failed: %ld\n", PTR_ERR(txd));
419d9a01771SH Hartley Sweeten 		return PTR_ERR(txd);
420e4c8308cSGrant Likely 	}
421e4c8308cSGrant Likely 
422e4c8308cSGrant Likely 	/* We are ready when RX is done */
423e4c8308cSGrant Likely 	rxd->callback = ep93xx_spi_dma_callback;
424*24e9b75cSYang Yingliang 	rxd->callback_param = host;
425e4c8308cSGrant Likely 
426d9a01771SH Hartley Sweeten 	/* Now submit both descriptors and start DMA */
427e4c8308cSGrant Likely 	dmaengine_submit(rxd);
428e4c8308cSGrant Likely 	dmaengine_submit(txd);
429e4c8308cSGrant Likely 
430e4c8308cSGrant Likely 	dma_async_issue_pending(espi->dma_rx);
431e4c8308cSGrant Likely 	dma_async_issue_pending(espi->dma_tx);
432e4c8308cSGrant Likely 
433d9a01771SH Hartley Sweeten 	/* signal that we need to wait for completion */
434d9a01771SH Hartley Sweeten 	return 1;
435ca632f55SGrant Likely }
436ca632f55SGrant Likely 
ep93xx_spi_interrupt(int irq,void * dev_id)437ca632f55SGrant Likely static irqreturn_t ep93xx_spi_interrupt(int irq, void *dev_id)
438ca632f55SGrant Likely {
439*24e9b75cSYang Yingliang 	struct spi_controller *host = dev_id;
440*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
441ac8d06dfSH Hartley Sweeten 	u32 val;
442ca632f55SGrant Likely 
443ca632f55SGrant Likely 	/*
444ca632f55SGrant Likely 	 * If we got ROR (receive overrun) interrupt we know that something is
445ca632f55SGrant Likely 	 * wrong. Just abort the message.
446ca632f55SGrant Likely 	 */
4478447e478SH Hartley Sweeten 	if (readl(espi->mmio + SSPIIR) & SSPIIR_RORIS) {
448ca632f55SGrant Likely 		/* clear the overrun interrupt */
4498447e478SH Hartley Sweeten 		writel(0, espi->mmio + SSPICR);
450*24e9b75cSYang Yingliang 		dev_warn(&host->dev,
451ca632f55SGrant Likely 			 "receive overrun, aborting the message\n");
452*24e9b75cSYang Yingliang 		host->cur_msg->status = -EIO;
453ca632f55SGrant Likely 	} else {
454ca632f55SGrant Likely 		/*
455ca632f55SGrant Likely 		 * Interrupt is either RX (RIS) or TX (TIS). For both cases we
456ca632f55SGrant Likely 		 * simply execute next data transfer.
457ca632f55SGrant Likely 		 */
458*24e9b75cSYang Yingliang 		if (ep93xx_spi_read_write(host)) {
459ca632f55SGrant Likely 			/*
460ca632f55SGrant Likely 			 * In normal case, there still is some processing left
461ca632f55SGrant Likely 			 * for current transfer. Let's wait for the next
462ca632f55SGrant Likely 			 * interrupt then.
463ca632f55SGrant Likely 			 */
464ca632f55SGrant Likely 			return IRQ_HANDLED;
465ca632f55SGrant Likely 		}
466ca632f55SGrant Likely 	}
467ca632f55SGrant Likely 
468ca632f55SGrant Likely 	/*
469ca632f55SGrant Likely 	 * Current transfer is finished, either with error or with success. In
470ca632f55SGrant Likely 	 * any case we disable interrupts and notify the worker to handle
471ca632f55SGrant Likely 	 * any post-processing of the message.
472ca632f55SGrant Likely 	 */
473ac8d06dfSH Hartley Sweeten 	val = readl(espi->mmio + SSPCR1);
474ac8d06dfSH Hartley Sweeten 	val &= ~(SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE);
475ac8d06dfSH Hartley Sweeten 	writel(val, espi->mmio + SSPCR1);
476ac8d06dfSH Hartley Sweeten 
477*24e9b75cSYang Yingliang 	spi_finalize_current_transfer(host);
478ac8d06dfSH Hartley Sweeten 
479ca632f55SGrant Likely 	return IRQ_HANDLED;
480ca632f55SGrant Likely }
481ca632f55SGrant Likely 
ep93xx_spi_transfer_one(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * xfer)482*24e9b75cSYang Yingliang static int ep93xx_spi_transfer_one(struct spi_controller *host,
483d9a01771SH Hartley Sweeten 				   struct spi_device *spi,
484d9a01771SH Hartley Sweeten 				   struct spi_transfer *xfer)
485d9a01771SH Hartley Sweeten {
486*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
487d9a01771SH Hartley Sweeten 	u32 val;
488d9a01771SH Hartley Sweeten 	int ret;
489d9a01771SH Hartley Sweeten 
490*24e9b75cSYang Yingliang 	ret = ep93xx_spi_chip_setup(host, spi, xfer);
491d9a01771SH Hartley Sweeten 	if (ret) {
492*24e9b75cSYang Yingliang 		dev_err(&host->dev, "failed to setup chip for transfer\n");
493d9a01771SH Hartley Sweeten 		return ret;
494d9a01771SH Hartley Sweeten 	}
495d9a01771SH Hartley Sweeten 
496*24e9b75cSYang Yingliang 	host->cur_msg->state = xfer;
497d9a01771SH Hartley Sweeten 	espi->rx = 0;
498d9a01771SH Hartley Sweeten 	espi->tx = 0;
499d9a01771SH Hartley Sweeten 
500d9a01771SH Hartley Sweeten 	/*
501d9a01771SH Hartley Sweeten 	 * There is no point of setting up DMA for the transfers which will
502d9a01771SH Hartley Sweeten 	 * fit into the FIFO and can be transferred with a single interrupt.
503d9a01771SH Hartley Sweeten 	 * So in these cases we will be using PIO and don't bother for DMA.
504d9a01771SH Hartley Sweeten 	 */
505d9a01771SH Hartley Sweeten 	if (espi->dma_rx && xfer->len > SPI_FIFO_SIZE)
506*24e9b75cSYang Yingliang 		return ep93xx_spi_dma_transfer(host);
507d9a01771SH Hartley Sweeten 
508d9a01771SH Hartley Sweeten 	/* Using PIO so prime the TX FIFO and enable interrupts */
509*24e9b75cSYang Yingliang 	ep93xx_spi_read_write(host);
510d9a01771SH Hartley Sweeten 
511d9a01771SH Hartley Sweeten 	val = readl(espi->mmio + SSPCR1);
512d9a01771SH Hartley Sweeten 	val |= (SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE);
513d9a01771SH Hartley Sweeten 	writel(val, espi->mmio + SSPCR1);
514d9a01771SH Hartley Sweeten 
515d9a01771SH Hartley Sweeten 	/* signal that we need to wait for completion */
516d9a01771SH Hartley Sweeten 	return 1;
517d9a01771SH Hartley Sweeten }
518d9a01771SH Hartley Sweeten 
ep93xx_spi_prepare_message(struct spi_controller * host,struct spi_message * msg)519*24e9b75cSYang Yingliang static int ep93xx_spi_prepare_message(struct spi_controller *host,
520d9a01771SH Hartley Sweeten 				      struct spi_message *msg)
521d9a01771SH Hartley Sweeten {
522*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
523d9a01771SH Hartley Sweeten 	unsigned long timeout;
524d9a01771SH Hartley Sweeten 
525d9a01771SH Hartley Sweeten 	/*
526d9a01771SH Hartley Sweeten 	 * Just to be sure: flush any data from RX FIFO.
527d9a01771SH Hartley Sweeten 	 */
528d9a01771SH Hartley Sweeten 	timeout = jiffies + msecs_to_jiffies(SPI_TIMEOUT);
529d9a01771SH Hartley Sweeten 	while (readl(espi->mmio + SSPSR) & SSPSR_RNE) {
530d9a01771SH Hartley Sweeten 		if (time_after(jiffies, timeout)) {
531*24e9b75cSYang Yingliang 			dev_warn(&host->dev,
532d9a01771SH Hartley Sweeten 				 "timeout while flushing RX FIFO\n");
533d9a01771SH Hartley Sweeten 			return -ETIMEDOUT;
534d9a01771SH Hartley Sweeten 		}
535d9a01771SH Hartley Sweeten 		readl(espi->mmio + SSPDR);
536d9a01771SH Hartley Sweeten 	}
537d9a01771SH Hartley Sweeten 
538d9a01771SH Hartley Sweeten 	/*
539d9a01771SH Hartley Sweeten 	 * We explicitly handle FIFO level. This way we don't have to check TX
540d9a01771SH Hartley Sweeten 	 * FIFO status using %SSPSR_TNF bit which may cause RX FIFO overruns.
541d9a01771SH Hartley Sweeten 	 */
542d9a01771SH Hartley Sweeten 	espi->fifo_level = 0;
543d9a01771SH Hartley Sweeten 
544d9a01771SH Hartley Sweeten 	return 0;
545d9a01771SH Hartley Sweeten }
546d9a01771SH Hartley Sweeten 
ep93xx_spi_prepare_hardware(struct spi_controller * host)547*24e9b75cSYang Yingliang static int ep93xx_spi_prepare_hardware(struct spi_controller *host)
54816779622SH Hartley Sweeten {
549*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
55016779622SH Hartley Sweeten 	u32 val;
55116779622SH Hartley Sweeten 	int ret;
55216779622SH Hartley Sweeten 
5537c72dc56SAlexander Sverdlin 	ret = clk_prepare_enable(espi->clk);
55416779622SH Hartley Sweeten 	if (ret)
55516779622SH Hartley Sweeten 		return ret;
55616779622SH Hartley Sweeten 
55716779622SH Hartley Sweeten 	val = readl(espi->mmio + SSPCR1);
55816779622SH Hartley Sweeten 	val |= SSPCR1_SSE;
55916779622SH Hartley Sweeten 	writel(val, espi->mmio + SSPCR1);
56016779622SH Hartley Sweeten 
56116779622SH Hartley Sweeten 	return 0;
56216779622SH Hartley Sweeten }
56316779622SH Hartley Sweeten 
ep93xx_spi_unprepare_hardware(struct spi_controller * host)564*24e9b75cSYang Yingliang static int ep93xx_spi_unprepare_hardware(struct spi_controller *host)
56516779622SH Hartley Sweeten {
566*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
56716779622SH Hartley Sweeten 	u32 val;
56816779622SH Hartley Sweeten 
56916779622SH Hartley Sweeten 	val = readl(espi->mmio + SSPCR1);
57016779622SH Hartley Sweeten 	val &= ~SSPCR1_SSE;
57116779622SH Hartley Sweeten 	writel(val, espi->mmio + SSPCR1);
57216779622SH Hartley Sweeten 
5737c72dc56SAlexander Sverdlin 	clk_disable_unprepare(espi->clk);
57416779622SH Hartley Sweeten 
57516779622SH Hartley Sweeten 	return 0;
57616779622SH Hartley Sweeten }
57716779622SH Hartley Sweeten 
ep93xx_spi_dma_filter(struct dma_chan * chan,void * filter_param)578e4c8308cSGrant Likely static bool ep93xx_spi_dma_filter(struct dma_chan *chan, void *filter_param)
579e4c8308cSGrant Likely {
580e4c8308cSGrant Likely 	if (ep93xx_dma_chan_is_m2p(chan))
581e4c8308cSGrant Likely 		return false;
582e4c8308cSGrant Likely 
583e4c8308cSGrant Likely 	chan->private = filter_param;
584e4c8308cSGrant Likely 	return true;
585e4c8308cSGrant Likely }
586e4c8308cSGrant Likely 
ep93xx_spi_setup_dma(struct ep93xx_spi * espi)587e4c8308cSGrant Likely static int ep93xx_spi_setup_dma(struct ep93xx_spi *espi)
588e4c8308cSGrant Likely {
589e4c8308cSGrant Likely 	dma_cap_mask_t mask;
590e4c8308cSGrant Likely 	int ret;
591e4c8308cSGrant Likely 
592e4c8308cSGrant Likely 	espi->zeropage = (void *)get_zeroed_page(GFP_KERNEL);
593e4c8308cSGrant Likely 	if (!espi->zeropage)
594e4c8308cSGrant Likely 		return -ENOMEM;
595e4c8308cSGrant Likely 
596e4c8308cSGrant Likely 	dma_cap_zero(mask);
597e4c8308cSGrant Likely 	dma_cap_set(DMA_SLAVE, mask);
598e4c8308cSGrant Likely 
599e4c8308cSGrant Likely 	espi->dma_rx_data.port = EP93XX_DMA_SSP;
600a485df4bSVinod Koul 	espi->dma_rx_data.direction = DMA_DEV_TO_MEM;
601e4c8308cSGrant Likely 	espi->dma_rx_data.name = "ep93xx-spi-rx";
602e4c8308cSGrant Likely 
603e4c8308cSGrant Likely 	espi->dma_rx = dma_request_channel(mask, ep93xx_spi_dma_filter,
604e4c8308cSGrant Likely 					   &espi->dma_rx_data);
605e4c8308cSGrant Likely 	if (!espi->dma_rx) {
606e4c8308cSGrant Likely 		ret = -ENODEV;
607e4c8308cSGrant Likely 		goto fail_free_page;
608e4c8308cSGrant Likely 	}
609e4c8308cSGrant Likely 
610e4c8308cSGrant Likely 	espi->dma_tx_data.port = EP93XX_DMA_SSP;
611a485df4bSVinod Koul 	espi->dma_tx_data.direction = DMA_MEM_TO_DEV;
612e4c8308cSGrant Likely 	espi->dma_tx_data.name = "ep93xx-spi-tx";
613e4c8308cSGrant Likely 
614e4c8308cSGrant Likely 	espi->dma_tx = dma_request_channel(mask, ep93xx_spi_dma_filter,
615e4c8308cSGrant Likely 					   &espi->dma_tx_data);
616e4c8308cSGrant Likely 	if (!espi->dma_tx) {
617e4c8308cSGrant Likely 		ret = -ENODEV;
618e4c8308cSGrant Likely 		goto fail_release_rx;
619e4c8308cSGrant Likely 	}
620e4c8308cSGrant Likely 
621e4c8308cSGrant Likely 	return 0;
622e4c8308cSGrant Likely 
623e4c8308cSGrant Likely fail_release_rx:
624e4c8308cSGrant Likely 	dma_release_channel(espi->dma_rx);
625e4c8308cSGrant Likely 	espi->dma_rx = NULL;
626e4c8308cSGrant Likely fail_free_page:
627e4c8308cSGrant Likely 	free_page((unsigned long)espi->zeropage);
628e4c8308cSGrant Likely 
629e4c8308cSGrant Likely 	return ret;
630e4c8308cSGrant Likely }
631e4c8308cSGrant Likely 
ep93xx_spi_release_dma(struct ep93xx_spi * espi)632e4c8308cSGrant Likely static void ep93xx_spi_release_dma(struct ep93xx_spi *espi)
633e4c8308cSGrant Likely {
634e4c8308cSGrant Likely 	if (espi->dma_rx) {
635e4c8308cSGrant Likely 		dma_release_channel(espi->dma_rx);
636e4c8308cSGrant Likely 		sg_free_table(&espi->rx_sgt);
637e4c8308cSGrant Likely 	}
638e4c8308cSGrant Likely 	if (espi->dma_tx) {
639e4c8308cSGrant Likely 		dma_release_channel(espi->dma_tx);
640e4c8308cSGrant Likely 		sg_free_table(&espi->tx_sgt);
641e4c8308cSGrant Likely 	}
642e4c8308cSGrant Likely 
643e4c8308cSGrant Likely 	if (espi->zeropage)
644e4c8308cSGrant Likely 		free_page((unsigned long)espi->zeropage);
645e4c8308cSGrant Likely }
646e4c8308cSGrant Likely 
ep93xx_spi_probe(struct platform_device * pdev)647fd4a319bSGrant Likely static int ep93xx_spi_probe(struct platform_device *pdev)
648ca632f55SGrant Likely {
649*24e9b75cSYang Yingliang 	struct spi_controller *host;
650ca632f55SGrant Likely 	struct ep93xx_spi_info *info;
651ca632f55SGrant Likely 	struct ep93xx_spi *espi;
652ca632f55SGrant Likely 	struct resource *res;
6536d6467eeSHannu Heikkinen 	int irq;
654ca632f55SGrant Likely 	int error;
655ca632f55SGrant Likely 
6568074cf06SJingoo Han 	info = dev_get_platdata(&pdev->dev);
65755f0cd3fSH Hartley Sweeten 	if (!info) {
65855f0cd3fSH Hartley Sweeten 		dev_err(&pdev->dev, "missing platform data\n");
65955f0cd3fSH Hartley Sweeten 		return -EINVAL;
66055f0cd3fSH Hartley Sweeten 	}
661ca632f55SGrant Likely 
66248a7776eSH Hartley Sweeten 	irq = platform_get_irq(pdev, 0);
6636b8ac10eSStephen Boyd 	if (irq < 0)
664cb8ea3ddSYangtao Li 		return irq;
66548a7776eSH Hartley Sweeten 
666*24e9b75cSYang Yingliang 	host = spi_alloc_host(&pdev->dev, sizeof(*espi));
667*24e9b75cSYang Yingliang 	if (!host)
668ca632f55SGrant Likely 		return -ENOMEM;
669ca632f55SGrant Likely 
670*24e9b75cSYang Yingliang 	host->use_gpio_descriptors = true;
671*24e9b75cSYang Yingliang 	host->prepare_transfer_hardware = ep93xx_spi_prepare_hardware;
672*24e9b75cSYang Yingliang 	host->unprepare_transfer_hardware = ep93xx_spi_unprepare_hardware;
673*24e9b75cSYang Yingliang 	host->prepare_message = ep93xx_spi_prepare_message;
674*24e9b75cSYang Yingliang 	host->transfer_one = ep93xx_spi_transfer_one;
675*24e9b75cSYang Yingliang 	host->bus_num = pdev->id;
676*24e9b75cSYang Yingliang 	host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
677*24e9b75cSYang Yingliang 	host->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
6781dfbf334SLinus Walleij 	/*
6791dfbf334SLinus Walleij 	 * The SPI core will count the number of GPIO descriptors to figure
6801dfbf334SLinus Walleij 	 * out the number of chip selects available on the platform.
6811dfbf334SLinus Walleij 	 */
682*24e9b75cSYang Yingliang 	host->num_chipselect = 0;
68355f0cd3fSH Hartley Sweeten 
684*24e9b75cSYang Yingliang 	platform_set_drvdata(pdev, host);
685ca632f55SGrant Likely 
686*24e9b75cSYang Yingliang 	espi = spi_controller_get_devdata(host);
687ca632f55SGrant Likely 
688e6eb8d9bSH Hartley Sweeten 	espi->clk = devm_clk_get(&pdev->dev, NULL);
689ca632f55SGrant Likely 	if (IS_ERR(espi->clk)) {
690ca632f55SGrant Likely 		dev_err(&pdev->dev, "unable to get spi clock\n");
691ca632f55SGrant Likely 		error = PTR_ERR(espi->clk);
692*24e9b75cSYang Yingliang 		goto fail_release_host;
693ca632f55SGrant Likely 	}
694ca632f55SGrant Likely 
695ca632f55SGrant Likely 	/*
696ca632f55SGrant Likely 	 * Calculate maximum and minimum supported clock rates
697ca632f55SGrant Likely 	 * for the controller.
698ca632f55SGrant Likely 	 */
699*24e9b75cSYang Yingliang 	host->max_speed_hz = clk_get_rate(espi->clk) / 2;
700*24e9b75cSYang Yingliang 	host->min_speed_hz = clk_get_rate(espi->clk) / (254 * 256);
701ca632f55SGrant Likely 
702cb8ea3ddSYangtao Li 	espi->mmio = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
7031232978aSH Hartley Sweeten 	if (IS_ERR(espi->mmio)) {
7041232978aSH Hartley Sweeten 		error = PTR_ERR(espi->mmio);
705*24e9b75cSYang Yingliang 		goto fail_release_host;
706ca632f55SGrant Likely 	}
707cb8ea3ddSYangtao Li 	espi->sspdr_phys = res->start + SSPDR;
708ca632f55SGrant Likely 
7096d6467eeSHannu Heikkinen 	error = devm_request_irq(&pdev->dev, irq, ep93xx_spi_interrupt,
710*24e9b75cSYang Yingliang 				0, "ep93xx-spi", host);
711ca632f55SGrant Likely 	if (error) {
712ca632f55SGrant Likely 		dev_err(&pdev->dev, "failed to request irq\n");
713*24e9b75cSYang Yingliang 		goto fail_release_host;
714ca632f55SGrant Likely 	}
715ca632f55SGrant Likely 
716e4c8308cSGrant Likely 	if (info->use_dma && ep93xx_spi_setup_dma(espi))
717e4c8308cSGrant Likely 		dev_warn(&pdev->dev, "DMA setup failed. Falling back to PIO\n");
718e4c8308cSGrant Likely 
719ca632f55SGrant Likely 	/* make sure that the hardware is disabled */
7208447e478SH Hartley Sweeten 	writel(0, espi->mmio + SSPCR1);
721ca632f55SGrant Likely 
722*24e9b75cSYang Yingliang 	error = devm_spi_register_controller(&pdev->dev, host);
723ca632f55SGrant Likely 	if (error) {
724*24e9b75cSYang Yingliang 		dev_err(&pdev->dev, "failed to register SPI host\n");
72584ddb3c1SH Hartley Sweeten 		goto fail_free_dma;
726ca632f55SGrant Likely 	}
727ca632f55SGrant Likely 
728ca632f55SGrant Likely 	dev_info(&pdev->dev, "EP93xx SPI Controller at 0x%08lx irq %d\n",
7296d6467eeSHannu Heikkinen 		 (unsigned long)res->start, irq);
730ca632f55SGrant Likely 
731ca632f55SGrant Likely 	return 0;
732ca632f55SGrant Likely 
733e4c8308cSGrant Likely fail_free_dma:
734e4c8308cSGrant Likely 	ep93xx_spi_release_dma(espi);
735*24e9b75cSYang Yingliang fail_release_host:
736*24e9b75cSYang Yingliang 	spi_controller_put(host);
737ca632f55SGrant Likely 
738ca632f55SGrant Likely 	return error;
739ca632f55SGrant Likely }
740ca632f55SGrant Likely 
ep93xx_spi_remove(struct platform_device * pdev)741bb2714d1SUwe Kleine-König static void ep93xx_spi_remove(struct platform_device *pdev)
742ca632f55SGrant Likely {
743*24e9b75cSYang Yingliang 	struct spi_controller *host = platform_get_drvdata(pdev);
744*24e9b75cSYang Yingliang 	struct ep93xx_spi *espi = spi_controller_get_devdata(host);
745ca632f55SGrant Likely 
746e4c8308cSGrant Likely 	ep93xx_spi_release_dma(espi);
747ca632f55SGrant Likely }
748ca632f55SGrant Likely 
749ca632f55SGrant Likely static struct platform_driver ep93xx_spi_driver = {
750ca632f55SGrant Likely 	.driver		= {
751ca632f55SGrant Likely 		.name	= "ep93xx-spi",
752ca632f55SGrant Likely 	},
753940ab889SGrant Likely 	.probe		= ep93xx_spi_probe,
754bb2714d1SUwe Kleine-König 	.remove_new	= ep93xx_spi_remove,
755ca632f55SGrant Likely };
756940ab889SGrant Likely module_platform_driver(ep93xx_spi_driver);
757ca632f55SGrant Likely 
758ca632f55SGrant Likely MODULE_DESCRIPTION("EP93xx SPI Controller driver");
759ca632f55SGrant Likely MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
760ca632f55SGrant Likely MODULE_LICENSE("GPL");
761ca632f55SGrant Likely MODULE_ALIAS("platform:ep93xx-spi");
762