xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision 3b8b6d05942ef5dd952674e7420600f762166e22)
1ca632f55SGrant Likely /*
2ca632f55SGrant Likely  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
3a0d2642eSMika Westerberg  * Copyright (C) 2013, Intel Corporation
4ca632f55SGrant Likely  *
5ca632f55SGrant Likely  * This program is free software; you can redistribute it and/or modify
6ca632f55SGrant Likely  * it under the terms of the GNU General Public License as published by
7ca632f55SGrant Likely  * the Free Software Foundation; either version 2 of the License, or
8ca632f55SGrant Likely  * (at your option) any later version.
9ca632f55SGrant Likely  *
10ca632f55SGrant Likely  * This program is distributed in the hope that it will be useful,
11ca632f55SGrant Likely  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ca632f55SGrant Likely  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13ca632f55SGrant Likely  * GNU General Public License for more details.
14ca632f55SGrant Likely  */
15ca632f55SGrant Likely 
16ca632f55SGrant Likely #include <linux/init.h>
17ca632f55SGrant Likely #include <linux/module.h>
18ca632f55SGrant Likely #include <linux/device.h>
19ca632f55SGrant Likely #include <linux/ioport.h>
20ca632f55SGrant Likely #include <linux/errno.h>
21cbfd6a21SSachin Kamat #include <linux/err.h>
22ca632f55SGrant Likely #include <linux/interrupt.h>
239df461ecSAndy Shevchenko #include <linux/kernel.h>
2434cadd9cSJarkko Nikula #include <linux/pci.h>
25ca632f55SGrant Likely #include <linux/platform_device.h>
26ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h>
27ca632f55SGrant Likely #include <linux/spi/spi.h>
28ca632f55SGrant Likely #include <linux/delay.h>
29ca632f55SGrant Likely #include <linux/gpio.h>
30ca632f55SGrant Likely #include <linux/slab.h>
313343b7a6SMika Westerberg #include <linux/clk.h>
327d94a505SMika Westerberg #include <linux/pm_runtime.h>
33a3496855SMika Westerberg #include <linux/acpi.h>
34ca632f55SGrant Likely 
35cd7bed00SMika Westerberg #include "spi-pxa2xx.h"
36ca632f55SGrant Likely 
37ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street");
38ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
39ca632f55SGrant Likely MODULE_LICENSE("GPL");
40ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi");
41ca632f55SGrant Likely 
42ca632f55SGrant Likely #define TIMOUT_DFLT		1000
43ca632f55SGrant Likely 
44ca632f55SGrant Likely /*
45ca632f55SGrant Likely  * for testing SSCR1 changes that require SSP restart, basically
46ca632f55SGrant Likely  * everything except the service and interrupt enables, the pxa270 developer
47ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
48ca632f55SGrant Likely  * list, but the PXA255 dev man says all bits without really meaning the
49ca632f55SGrant Likely  * service and interrupt enables
50ca632f55SGrant Likely  */
51ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
52ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
53ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
54ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
55ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
56ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
57ca632f55SGrant Likely 
58e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
59e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
60e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
61e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
62e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
63e5262d05SWeike Chen 
641de70612SMika Westerberg #define GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
65a0d2642eSMika Westerberg #define SPI_CS_CONTROL_SW_MODE	BIT(0)
66a0d2642eSMika Westerberg #define SPI_CS_CONTROL_CS_HIGH	BIT(1)
67a0d2642eSMika Westerberg 
68dccf7369SJarkko Nikula struct lpss_config {
69dccf7369SJarkko Nikula 	/* LPSS offset from drv_data->ioaddr */
70dccf7369SJarkko Nikula 	unsigned offset;
71dccf7369SJarkko Nikula 	/* Register offsets from drv_data->lpss_base or -1 */
72dccf7369SJarkko Nikula 	int reg_general;
73dccf7369SJarkko Nikula 	int reg_ssp;
74dccf7369SJarkko Nikula 	int reg_cs_ctrl;
75dccf7369SJarkko Nikula 	/* FIFO thresholds */
76dccf7369SJarkko Nikula 	u32 rx_threshold;
77dccf7369SJarkko Nikula 	u32 tx_threshold_lo;
78dccf7369SJarkko Nikula 	u32 tx_threshold_hi;
79dccf7369SJarkko Nikula };
80dccf7369SJarkko Nikula 
81dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */
82dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = {
83dccf7369SJarkko Nikula 	{	/* LPSS_LPT_SSP */
84dccf7369SJarkko Nikula 		.offset = 0x800,
85dccf7369SJarkko Nikula 		.reg_general = 0x08,
86dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
87dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
88dccf7369SJarkko Nikula 		.rx_threshold = 64,
89dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
90dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
91dccf7369SJarkko Nikula 	},
92dccf7369SJarkko Nikula 	{	/* LPSS_BYT_SSP */
93dccf7369SJarkko Nikula 		.offset = 0x400,
94dccf7369SJarkko Nikula 		.reg_general = 0x08,
95dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
96dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
97dccf7369SJarkko Nikula 		.rx_threshold = 64,
98dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
99dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
100dccf7369SJarkko Nikula 	},
10134cadd9cSJarkko Nikula 	{	/* LPSS_SPT_SSP */
10234cadd9cSJarkko Nikula 		.offset = 0x200,
10334cadd9cSJarkko Nikula 		.reg_general = -1,
10434cadd9cSJarkko Nikula 		.reg_ssp = 0x20,
10534cadd9cSJarkko Nikula 		.reg_cs_ctrl = 0x24,
10634cadd9cSJarkko Nikula 		.rx_threshold = 1,
10734cadd9cSJarkko Nikula 		.tx_threshold_lo = 32,
10834cadd9cSJarkko Nikula 		.tx_threshold_hi = 56,
10934cadd9cSJarkko Nikula 	},
110dccf7369SJarkko Nikula };
111dccf7369SJarkko Nikula 
112dccf7369SJarkko Nikula static inline const struct lpss_config
113dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data)
114dccf7369SJarkko Nikula {
115dccf7369SJarkko Nikula 	return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
116dccf7369SJarkko Nikula }
117dccf7369SJarkko Nikula 
118a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data)
119a0d2642eSMika Westerberg {
12003fbf488SJarkko Nikula 	switch (drv_data->ssp_type) {
12103fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
12203fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
12334cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
12403fbf488SJarkko Nikula 		return true;
12503fbf488SJarkko Nikula 	default:
12603fbf488SJarkko Nikula 		return false;
12703fbf488SJarkko Nikula 	}
128a0d2642eSMika Westerberg }
129a0d2642eSMika Westerberg 
130e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
131e5262d05SWeike Chen {
132e5262d05SWeike Chen 	return drv_data->ssp_type == QUARK_X1000_SSP;
133e5262d05SWeike Chen }
134e5262d05SWeike Chen 
1354fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
1364fdb2424SWeike Chen {
1374fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
138e5262d05SWeike Chen 	case QUARK_X1000_SSP:
139e5262d05SWeike Chen 		return QUARK_X1000_SSCR1_CHANGE_MASK;
1404fdb2424SWeike Chen 	default:
1414fdb2424SWeike Chen 		return SSCR1_CHANGE_MASK;
1424fdb2424SWeike Chen 	}
1434fdb2424SWeike Chen }
1444fdb2424SWeike Chen 
1454fdb2424SWeike Chen static u32
1464fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
1474fdb2424SWeike Chen {
1484fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
149e5262d05SWeike Chen 	case QUARK_X1000_SSP:
150e5262d05SWeike Chen 		return RX_THRESH_QUARK_X1000_DFLT;
1514fdb2424SWeike Chen 	default:
1524fdb2424SWeike Chen 		return RX_THRESH_DFLT;
1534fdb2424SWeike Chen 	}
1544fdb2424SWeike Chen }
1554fdb2424SWeike Chen 
1564fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
1574fdb2424SWeike Chen {
1584fdb2424SWeike Chen 	u32 mask;
1594fdb2424SWeike Chen 
1604fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
161e5262d05SWeike Chen 	case QUARK_X1000_SSP:
162e5262d05SWeike Chen 		mask = QUARK_X1000_SSSR_TFL_MASK;
163e5262d05SWeike Chen 		break;
1644fdb2424SWeike Chen 	default:
1654fdb2424SWeike Chen 		mask = SSSR_TFL_MASK;
1664fdb2424SWeike Chen 		break;
1674fdb2424SWeike Chen 	}
1684fdb2424SWeike Chen 
169c039dd27SJarkko Nikula 	return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
1704fdb2424SWeike Chen }
1714fdb2424SWeike Chen 
1724fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
1734fdb2424SWeike Chen 				     u32 *sccr1_reg)
1744fdb2424SWeike Chen {
1754fdb2424SWeike Chen 	u32 mask;
1764fdb2424SWeike Chen 
1774fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
178e5262d05SWeike Chen 	case QUARK_X1000_SSP:
179e5262d05SWeike Chen 		mask = QUARK_X1000_SSCR1_RFT;
180e5262d05SWeike Chen 		break;
1814fdb2424SWeike Chen 	default:
1824fdb2424SWeike Chen 		mask = SSCR1_RFT;
1834fdb2424SWeike Chen 		break;
1844fdb2424SWeike Chen 	}
1854fdb2424SWeike Chen 	*sccr1_reg &= ~mask;
1864fdb2424SWeike Chen }
1874fdb2424SWeike Chen 
1884fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
1894fdb2424SWeike Chen 				   u32 *sccr1_reg, u32 threshold)
1904fdb2424SWeike Chen {
1914fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
192e5262d05SWeike Chen 	case QUARK_X1000_SSP:
193e5262d05SWeike Chen 		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
194e5262d05SWeike Chen 		break;
1954fdb2424SWeike Chen 	default:
1964fdb2424SWeike Chen 		*sccr1_reg |= SSCR1_RxTresh(threshold);
1974fdb2424SWeike Chen 		break;
1984fdb2424SWeike Chen 	}
1994fdb2424SWeike Chen }
2004fdb2424SWeike Chen 
2014fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
2024fdb2424SWeike Chen 				  u32 clk_div, u8 bits)
2034fdb2424SWeike Chen {
2044fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
205e5262d05SWeike Chen 	case QUARK_X1000_SSP:
206e5262d05SWeike Chen 		return clk_div
207e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_Motorola
208e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
209e5262d05SWeike Chen 			| SSCR0_SSE;
2104fdb2424SWeike Chen 	default:
2114fdb2424SWeike Chen 		return clk_div
2124fdb2424SWeike Chen 			| SSCR0_Motorola
2134fdb2424SWeike Chen 			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
2144fdb2424SWeike Chen 			| SSCR0_SSE
2154fdb2424SWeike Chen 			| (bits > 16 ? SSCR0_EDSS : 0);
2164fdb2424SWeike Chen 	}
2174fdb2424SWeike Chen }
2184fdb2424SWeike Chen 
219a0d2642eSMika Westerberg /*
220a0d2642eSMika Westerberg  * Read and write LPSS SSP private registers. Caller must first check that
221a0d2642eSMika Westerberg  * is_lpss_ssp() returns true before these can be called.
222a0d2642eSMika Westerberg  */
223a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
224a0d2642eSMika Westerberg {
225a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
226a0d2642eSMika Westerberg 	return readl(drv_data->lpss_base + offset);
227a0d2642eSMika Westerberg }
228a0d2642eSMika Westerberg 
229a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data,
230a0d2642eSMika Westerberg 				  unsigned offset, u32 value)
231a0d2642eSMika Westerberg {
232a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
233a0d2642eSMika Westerberg 	writel(value, drv_data->lpss_base + offset);
234a0d2642eSMika Westerberg }
235a0d2642eSMika Westerberg 
236a0d2642eSMika Westerberg /*
237a0d2642eSMika Westerberg  * lpss_ssp_setup - perform LPSS SSP specific setup
238a0d2642eSMika Westerberg  * @drv_data: pointer to the driver private data
239a0d2642eSMika Westerberg  *
240a0d2642eSMika Westerberg  * Perform LPSS SSP specific setup. This function must be called first if
241a0d2642eSMika Westerberg  * one is going to use LPSS SSP private registers.
242a0d2642eSMika Westerberg  */
243a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data)
244a0d2642eSMika Westerberg {
245dccf7369SJarkko Nikula 	const struct lpss_config *config;
246dccf7369SJarkko Nikula 	u32 value;
247a0d2642eSMika Westerberg 
248dccf7369SJarkko Nikula 	config = lpss_get_config(drv_data);
249dccf7369SJarkko Nikula 	drv_data->lpss_base = drv_data->ioaddr + config->offset;
250a0d2642eSMika Westerberg 
251a0d2642eSMika Westerberg 	/* Enable software chip select control */
252a0d2642eSMika Westerberg 	value = SPI_CS_CONTROL_SW_MODE | SPI_CS_CONTROL_CS_HIGH;
253dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
2540054e28dSMika Westerberg 
2550054e28dSMika Westerberg 	/* Enable multiblock DMA transfers */
2561de70612SMika Westerberg 	if (drv_data->master_info->enable_dma) {
257dccf7369SJarkko Nikula 		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
2581de70612SMika Westerberg 
25982ba2c2aSJarkko Nikula 		if (config->reg_general >= 0) {
26082ba2c2aSJarkko Nikula 			value = __lpss_ssp_read_priv(drv_data,
26182ba2c2aSJarkko Nikula 						     config->reg_general);
2621de70612SMika Westerberg 			value |= GENERAL_REG_RXTO_HOLDOFF_DISABLE;
26382ba2c2aSJarkko Nikula 			__lpss_ssp_write_priv(drv_data,
26482ba2c2aSJarkko Nikula 					      config->reg_general, value);
26582ba2c2aSJarkko Nikula 		}
2661de70612SMika Westerberg 	}
267a0d2642eSMika Westerberg }
268a0d2642eSMika Westerberg 
269a0d2642eSMika Westerberg static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
270a0d2642eSMika Westerberg {
271dccf7369SJarkko Nikula 	const struct lpss_config *config;
272a0d2642eSMika Westerberg 	u32 value;
273a0d2642eSMika Westerberg 
274dccf7369SJarkko Nikula 	config = lpss_get_config(drv_data);
275dccf7369SJarkko Nikula 
276dccf7369SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
277a0d2642eSMika Westerberg 	if (enable)
278a0d2642eSMika Westerberg 		value &= ~SPI_CS_CONTROL_CS_HIGH;
279a0d2642eSMika Westerberg 	else
280a0d2642eSMika Westerberg 		value |= SPI_CS_CONTROL_CS_HIGH;
281dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
282a0d2642eSMika Westerberg }
283a0d2642eSMika Westerberg 
284ca632f55SGrant Likely static void cs_assert(struct driver_data *drv_data)
285ca632f55SGrant Likely {
286ca632f55SGrant Likely 	struct chip_data *chip = drv_data->cur_chip;
287ca632f55SGrant Likely 
288ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP) {
289c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm);
290ca632f55SGrant Likely 		return;
291ca632f55SGrant Likely 	}
292ca632f55SGrant Likely 
293ca632f55SGrant Likely 	if (chip->cs_control) {
294ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_ASSERT);
295ca632f55SGrant Likely 		return;
296ca632f55SGrant Likely 	}
297ca632f55SGrant Likely 
298a0d2642eSMika Westerberg 	if (gpio_is_valid(chip->gpio_cs)) {
299ca632f55SGrant Likely 		gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
300a0d2642eSMika Westerberg 		return;
301a0d2642eSMika Westerberg 	}
302a0d2642eSMika Westerberg 
3037566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
304a0d2642eSMika Westerberg 		lpss_ssp_cs_control(drv_data, true);
305ca632f55SGrant Likely }
306ca632f55SGrant Likely 
307ca632f55SGrant Likely static void cs_deassert(struct driver_data *drv_data)
308ca632f55SGrant Likely {
309ca632f55SGrant Likely 	struct chip_data *chip = drv_data->cur_chip;
310ca632f55SGrant Likely 
311ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
312ca632f55SGrant Likely 		return;
313ca632f55SGrant Likely 
314ca632f55SGrant Likely 	if (chip->cs_control) {
315ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_DEASSERT);
316ca632f55SGrant Likely 		return;
317ca632f55SGrant Likely 	}
318ca632f55SGrant Likely 
319a0d2642eSMika Westerberg 	if (gpio_is_valid(chip->gpio_cs)) {
320ca632f55SGrant Likely 		gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
321a0d2642eSMika Westerberg 		return;
322a0d2642eSMika Westerberg 	}
323a0d2642eSMika Westerberg 
3247566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
325a0d2642eSMika Westerberg 		lpss_ssp_cs_control(drv_data, false);
326ca632f55SGrant Likely }
327ca632f55SGrant Likely 
328cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
329ca632f55SGrant Likely {
330ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
331ca632f55SGrant Likely 
332ca632f55SGrant Likely 	do {
333c039dd27SJarkko Nikula 		while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
334c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
335c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
336ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
337ca632f55SGrant Likely 
338ca632f55SGrant Likely 	return limit;
339ca632f55SGrant Likely }
340ca632f55SGrant Likely 
341ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
342ca632f55SGrant Likely {
343ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
344ca632f55SGrant Likely 
3454fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
346ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
347ca632f55SGrant Likely 		return 0;
348ca632f55SGrant Likely 
349c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
350ca632f55SGrant Likely 	drv_data->tx += n_bytes;
351ca632f55SGrant Likely 
352ca632f55SGrant Likely 	return 1;
353ca632f55SGrant Likely }
354ca632f55SGrant Likely 
355ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
356ca632f55SGrant Likely {
357ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
358ca632f55SGrant Likely 
359c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
360ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
361c039dd27SJarkko Nikula 		pxa2xx_spi_read(drv_data, SSDR);
362ca632f55SGrant Likely 		drv_data->rx += n_bytes;
363ca632f55SGrant Likely 	}
364ca632f55SGrant Likely 
365ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
366ca632f55SGrant Likely }
367ca632f55SGrant Likely 
368ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data)
369ca632f55SGrant Likely {
3704fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
371ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
372ca632f55SGrant Likely 		return 0;
373ca632f55SGrant Likely 
374c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
375ca632f55SGrant Likely 	++drv_data->tx;
376ca632f55SGrant Likely 
377ca632f55SGrant Likely 	return 1;
378ca632f55SGrant Likely }
379ca632f55SGrant Likely 
380ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data)
381ca632f55SGrant Likely {
382c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
383ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
384c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
385ca632f55SGrant Likely 		++drv_data->rx;
386ca632f55SGrant Likely 	}
387ca632f55SGrant Likely 
388ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
389ca632f55SGrant Likely }
390ca632f55SGrant Likely 
391ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
392ca632f55SGrant Likely {
3934fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
394ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
395ca632f55SGrant Likely 		return 0;
396ca632f55SGrant Likely 
397c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
398ca632f55SGrant Likely 	drv_data->tx += 2;
399ca632f55SGrant Likely 
400ca632f55SGrant Likely 	return 1;
401ca632f55SGrant Likely }
402ca632f55SGrant Likely 
403ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
404ca632f55SGrant Likely {
405c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
406ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
407c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
408ca632f55SGrant Likely 		drv_data->rx += 2;
409ca632f55SGrant Likely 	}
410ca632f55SGrant Likely 
411ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
412ca632f55SGrant Likely }
413ca632f55SGrant Likely 
414ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
415ca632f55SGrant Likely {
4164fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
417ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
418ca632f55SGrant Likely 		return 0;
419ca632f55SGrant Likely 
420c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
421ca632f55SGrant Likely 	drv_data->tx += 4;
422ca632f55SGrant Likely 
423ca632f55SGrant Likely 	return 1;
424ca632f55SGrant Likely }
425ca632f55SGrant Likely 
426ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
427ca632f55SGrant Likely {
428c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
429ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
430c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
431ca632f55SGrant Likely 		drv_data->rx += 4;
432ca632f55SGrant Likely 	}
433ca632f55SGrant Likely 
434ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
435ca632f55SGrant Likely }
436ca632f55SGrant Likely 
437cd7bed00SMika Westerberg void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
438ca632f55SGrant Likely {
439ca632f55SGrant Likely 	struct spi_message *msg = drv_data->cur_msg;
440ca632f55SGrant Likely 	struct spi_transfer *trans = drv_data->cur_transfer;
441ca632f55SGrant Likely 
442ca632f55SGrant Likely 	/* Move to next transfer */
443ca632f55SGrant Likely 	if (trans->transfer_list.next != &msg->transfers) {
444ca632f55SGrant Likely 		drv_data->cur_transfer =
445ca632f55SGrant Likely 			list_entry(trans->transfer_list.next,
446ca632f55SGrant Likely 					struct spi_transfer,
447ca632f55SGrant Likely 					transfer_list);
448ca632f55SGrant Likely 		return RUNNING_STATE;
449ca632f55SGrant Likely 	} else
450ca632f55SGrant Likely 		return DONE_STATE;
451ca632f55SGrant Likely }
452ca632f55SGrant Likely 
453ca632f55SGrant Likely /* caller already set message->status; dma and pio irqs are blocked */
454ca632f55SGrant Likely static void giveback(struct driver_data *drv_data)
455ca632f55SGrant Likely {
456ca632f55SGrant Likely 	struct spi_transfer* last_transfer;
457ca632f55SGrant Likely 	struct spi_message *msg;
458ca632f55SGrant Likely 
459ca632f55SGrant Likely 	msg = drv_data->cur_msg;
460ca632f55SGrant Likely 	drv_data->cur_msg = NULL;
461ca632f55SGrant Likely 	drv_data->cur_transfer = NULL;
462ca632f55SGrant Likely 
46323e2c2aaSAxel Lin 	last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
464ca632f55SGrant Likely 					transfer_list);
465ca632f55SGrant Likely 
466ca632f55SGrant Likely 	/* Delay if requested before any change in chip select */
467ca632f55SGrant Likely 	if (last_transfer->delay_usecs)
468ca632f55SGrant Likely 		udelay(last_transfer->delay_usecs);
469ca632f55SGrant Likely 
470ca632f55SGrant Likely 	/* Drop chip select UNLESS cs_change is true or we are returning
471ca632f55SGrant Likely 	 * a message with an error, or next message is for another chip
472ca632f55SGrant Likely 	 */
473ca632f55SGrant Likely 	if (!last_transfer->cs_change)
474ca632f55SGrant Likely 		cs_deassert(drv_data);
475ca632f55SGrant Likely 	else {
476ca632f55SGrant Likely 		struct spi_message *next_msg;
477ca632f55SGrant Likely 
478ca632f55SGrant Likely 		/* Holding of cs was hinted, but we need to make sure
479ca632f55SGrant Likely 		 * the next message is for the same chip.  Don't waste
480ca632f55SGrant Likely 		 * time with the following tests unless this was hinted.
481ca632f55SGrant Likely 		 *
482ca632f55SGrant Likely 		 * We cannot postpone this until pump_messages, because
483ca632f55SGrant Likely 		 * after calling msg->complete (below) the driver that
484ca632f55SGrant Likely 		 * sent the current message could be unloaded, which
485ca632f55SGrant Likely 		 * could invalidate the cs_control() callback...
486ca632f55SGrant Likely 		 */
487ca632f55SGrant Likely 
488ca632f55SGrant Likely 		/* get a pointer to the next message, if any */
4897f86bde9SMika Westerberg 		next_msg = spi_get_next_queued_message(drv_data->master);
490ca632f55SGrant Likely 
491ca632f55SGrant Likely 		/* see if the next and current messages point
492ca632f55SGrant Likely 		 * to the same chip
493ca632f55SGrant Likely 		 */
494ca632f55SGrant Likely 		if (next_msg && next_msg->spi != msg->spi)
495ca632f55SGrant Likely 			next_msg = NULL;
496ca632f55SGrant Likely 		if (!next_msg || msg->state == ERROR_STATE)
497ca632f55SGrant Likely 			cs_deassert(drv_data);
498ca632f55SGrant Likely 	}
499ca632f55SGrant Likely 
500ca632f55SGrant Likely 	drv_data->cur_chip = NULL;
501c957e8f0SMika Westerberg 	spi_finalize_current_message(drv_data->master);
502ca632f55SGrant Likely }
503ca632f55SGrant Likely 
504ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
505ca632f55SGrant Likely {
506ca632f55SGrant Likely 	struct chip_data *chip = drv_data->cur_chip;
507ca632f55SGrant Likely 	u32 sccr1_reg;
508ca632f55SGrant Likely 
509c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
510ca632f55SGrant Likely 	sccr1_reg &= ~SSCR1_RFT;
511ca632f55SGrant Likely 	sccr1_reg |= chip->threshold;
512c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
513ca632f55SGrant Likely }
514ca632f55SGrant Likely 
515ca632f55SGrant Likely static void int_error_stop(struct driver_data *drv_data, const char* msg)
516ca632f55SGrant Likely {
517ca632f55SGrant Likely 	/* Stop and reset SSP */
518ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
519ca632f55SGrant Likely 	reset_sccr1(drv_data);
520ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
521c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
522cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
523c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0,
524c039dd27SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
525ca632f55SGrant Likely 
526ca632f55SGrant Likely 	dev_err(&drv_data->pdev->dev, "%s\n", msg);
527ca632f55SGrant Likely 
528ca632f55SGrant Likely 	drv_data->cur_msg->state = ERROR_STATE;
529ca632f55SGrant Likely 	tasklet_schedule(&drv_data->pump_transfers);
530ca632f55SGrant Likely }
531ca632f55SGrant Likely 
532ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
533ca632f55SGrant Likely {
534ca632f55SGrant Likely 	/* Stop SSP */
535ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
536ca632f55SGrant Likely 	reset_sccr1(drv_data);
537ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
538c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
539ca632f55SGrant Likely 
540ca632f55SGrant Likely 	/* Update total byte transferred return count actual bytes read */
541ca632f55SGrant Likely 	drv_data->cur_msg->actual_length += drv_data->len -
542ca632f55SGrant Likely 				(drv_data->rx_end - drv_data->rx);
543ca632f55SGrant Likely 
544ca632f55SGrant Likely 	/* Transfer delays and chip select release are
545ca632f55SGrant Likely 	 * handled in pump_transfers or giveback
546ca632f55SGrant Likely 	 */
547ca632f55SGrant Likely 
548ca632f55SGrant Likely 	/* Move to next transfer */
549cd7bed00SMika Westerberg 	drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
550ca632f55SGrant Likely 
551ca632f55SGrant Likely 	/* Schedule transfer tasklet */
552ca632f55SGrant Likely 	tasklet_schedule(&drv_data->pump_transfers);
553ca632f55SGrant Likely }
554ca632f55SGrant Likely 
555ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
556ca632f55SGrant Likely {
557c039dd27SJarkko Nikula 	u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
558ca632f55SGrant Likely 		       drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
559ca632f55SGrant Likely 
560c039dd27SJarkko Nikula 	u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
561ca632f55SGrant Likely 
562ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
563ca632f55SGrant Likely 		int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
564ca632f55SGrant Likely 		return IRQ_HANDLED;
565ca632f55SGrant Likely 	}
566ca632f55SGrant Likely 
567ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
568c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
569ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
570ca632f55SGrant Likely 			int_transfer_complete(drv_data);
571ca632f55SGrant Likely 			return IRQ_HANDLED;
572ca632f55SGrant Likely 		}
573ca632f55SGrant Likely 	}
574ca632f55SGrant Likely 
575ca632f55SGrant Likely 	/* Drain rx fifo, Fill tx fifo and prevent overruns */
576ca632f55SGrant Likely 	do {
577ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
578ca632f55SGrant Likely 			int_transfer_complete(drv_data);
579ca632f55SGrant Likely 			return IRQ_HANDLED;
580ca632f55SGrant Likely 		}
581ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
582ca632f55SGrant Likely 
583ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
584ca632f55SGrant Likely 		int_transfer_complete(drv_data);
585ca632f55SGrant Likely 		return IRQ_HANDLED;
586ca632f55SGrant Likely 	}
587ca632f55SGrant Likely 
588ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
589ca632f55SGrant Likely 		u32 bytes_left;
590ca632f55SGrant Likely 		u32 sccr1_reg;
591ca632f55SGrant Likely 
592c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
593ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
594ca632f55SGrant Likely 
595ca632f55SGrant Likely 		/*
596ca632f55SGrant Likely 		 * PXA25x_SSP has no timeout, set up rx threshould for the
597ca632f55SGrant Likely 		 * remaining RX bytes.
598ca632f55SGrant Likely 		 */
599ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
6004fdb2424SWeike Chen 			u32 rx_thre;
601ca632f55SGrant Likely 
6024fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
603ca632f55SGrant Likely 
604ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
605ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
606ca632f55SGrant Likely 			case 4:
607ca632f55SGrant Likely 				bytes_left >>= 1;
608ca632f55SGrant Likely 			case 2:
609ca632f55SGrant Likely 				bytes_left >>= 1;
610ca632f55SGrant Likely 			}
611ca632f55SGrant Likely 
6124fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
6134fdb2424SWeike Chen 			if (rx_thre > bytes_left)
6144fdb2424SWeike Chen 				rx_thre = bytes_left;
615ca632f55SGrant Likely 
6164fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
617ca632f55SGrant Likely 		}
618c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
619ca632f55SGrant Likely 	}
620ca632f55SGrant Likely 
621ca632f55SGrant Likely 	/* We did something */
622ca632f55SGrant Likely 	return IRQ_HANDLED;
623ca632f55SGrant Likely }
624ca632f55SGrant Likely 
625ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
626ca632f55SGrant Likely {
627ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
6287d94a505SMika Westerberg 	u32 sccr1_reg;
629ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
630ca632f55SGrant Likely 	u32 status;
631ca632f55SGrant Likely 
6327d94a505SMika Westerberg 	/*
6337d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
6347d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
6357d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
6367d94a505SMika Westerberg 	 * interrupt is enabled).
6377d94a505SMika Westerberg 	 */
6387d94a505SMika Westerberg 	if (pm_runtime_suspended(&drv_data->pdev->dev))
6397d94a505SMika Westerberg 		return IRQ_NONE;
6407d94a505SMika Westerberg 
641269e4a41SMika Westerberg 	/*
642269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
643269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
644269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
645269e4a41SMika Westerberg 	 * powered off.
646269e4a41SMika Westerberg 	 */
647c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
648269e4a41SMika Westerberg 	if (status == ~0)
649269e4a41SMika Westerberg 		return IRQ_NONE;
650269e4a41SMika Westerberg 
651c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
652ca632f55SGrant Likely 
653ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
654ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
655ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
656ca632f55SGrant Likely 
657ca632f55SGrant Likely 	if (!(status & mask))
658ca632f55SGrant Likely 		return IRQ_NONE;
659ca632f55SGrant Likely 
660ca632f55SGrant Likely 	if (!drv_data->cur_msg) {
661ca632f55SGrant Likely 
662c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0,
663c039dd27SJarkko Nikula 				 pxa2xx_spi_read(drv_data, SSCR0)
664c039dd27SJarkko Nikula 				 & ~SSCR0_SSE);
665c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1,
666c039dd27SJarkko Nikula 				 pxa2xx_spi_read(drv_data, SSCR1)
667c039dd27SJarkko Nikula 				 & ~drv_data->int_cr1);
668ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
669c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, 0);
670ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
671ca632f55SGrant Likely 
672f6bd03a7SJarkko Nikula 		dev_err(&drv_data->pdev->dev,
673f6bd03a7SJarkko Nikula 			"bad message state in interrupt handler\n");
674ca632f55SGrant Likely 
675ca632f55SGrant Likely 		/* Never fail */
676ca632f55SGrant Likely 		return IRQ_HANDLED;
677ca632f55SGrant Likely 	}
678ca632f55SGrant Likely 
679ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
680ca632f55SGrant Likely }
681ca632f55SGrant Likely 
682e5262d05SWeike Chen /*
6839df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
6849df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
6859df461ecSAndy Shevchenko  *
6869df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
6879df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
6889df461ecSAndy Shevchenko  *
6899df461ecSAndy Shevchenko  * Fsys = 200MHz
6909df461ecSAndy Shevchenko  *
6919df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
6929df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
6939df461ecSAndy Shevchenko  *
6949df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
6959df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
6969df461ecSAndy Shevchenko  *
6979df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
6989df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
6999df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
7009df461ecSAndy Shevchenko  *       k = [1, 256]
7019df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
7029df461ecSAndy Shevchenko  *
7039df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
7049df461ecSAndy Shevchenko  * are:
7059df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
7069df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
7079df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
7089df461ecSAndy Shevchenko  *
7099df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
7109df461ecSAndy Shevchenko  *
7119df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
7129df461ecSAndy Shevchenko  * to the asked baud rate.
713e5262d05SWeike Chen  */
7149df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
715e5262d05SWeike Chen {
7169df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
7179df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
7189df461ecSAndy Shevchenko 						   see (2) */
7199df461ecSAndy Shevchenko 						/* case 3 */
7209df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
7219df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
7229df461ecSAndy Shevchenko 	unsigned long scale;
7239df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
7249df461ecSAndy Shevchenko 	long r, r1, r2;
7259df461ecSAndy Shevchenko 	u32 mul;
726e5262d05SWeike Chen 
7279df461ecSAndy Shevchenko 	/* Case 1 */
7289df461ecSAndy Shevchenko 
7299df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
7309df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
7319df461ecSAndy Shevchenko 
7329df461ecSAndy Shevchenko 	/* Calculate initial quot */
7333ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
7349df461ecSAndy Shevchenko 
7359df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
7369df461ecSAndy Shevchenko 	if (q1 > 256) {
7379df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
7389df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
7399df461ecSAndy Shevchenko 		if (scale > 9) {
7409df461ecSAndy Shevchenko 			q1 >>= scale - 9;
7419df461ecSAndy Shevchenko 			mul >>= scale - 9;
7429df461ecSAndy Shevchenko 		}
7439df461ecSAndy Shevchenko 
7449df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
7459df461ecSAndy Shevchenko 		q1 += q1 & 1;
7469df461ecSAndy Shevchenko 	}
7479df461ecSAndy Shevchenko 
7489df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
7499df461ecSAndy Shevchenko 	scale = __ffs(q1);
7509df461ecSAndy Shevchenko 	q1 >>= scale;
7519df461ecSAndy Shevchenko 	mul >>= scale;
7529df461ecSAndy Shevchenko 
7539df461ecSAndy Shevchenko 	/* Get the remainder */
7549df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
7559df461ecSAndy Shevchenko 
7569df461ecSAndy Shevchenko 	/* Case 2 */
7579df461ecSAndy Shevchenko 
7583ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
7599df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
7609df461ecSAndy Shevchenko 
7619df461ecSAndy Shevchenko 	/*
7629df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
7639df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
7649df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
7659df461ecSAndy Shevchenko 	 */
7669df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
7679df461ecSAndy Shevchenko 		/* case 1 is better */
7689df461ecSAndy Shevchenko 		r = r1;
7699df461ecSAndy Shevchenko 		q = q1;
7709df461ecSAndy Shevchenko 	} else {
7719df461ecSAndy Shevchenko 		/* case 2 is better */
7729df461ecSAndy Shevchenko 		r = r2;
7739df461ecSAndy Shevchenko 		q = q2;
7749df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
7759df461ecSAndy Shevchenko 	}
7769df461ecSAndy Shevchenko 
7773ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
7789df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
7799df461ecSAndy Shevchenko 		u64 fssp;
7809df461ecSAndy Shevchenko 		u32 m;
7819df461ecSAndy Shevchenko 
7829df461ecSAndy Shevchenko 		/* Calculate initial quot */
7833ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
7849df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
7859df461ecSAndy Shevchenko 
7869df461ecSAndy Shevchenko 		/* Get the remainder */
7879df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
7889df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
7899df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
7909df461ecSAndy Shevchenko 
7919df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
7929df461ecSAndy Shevchenko 		if (r1 < r) {
7939df461ecSAndy Shevchenko 			/* case 3 is better */
7949df461ecSAndy Shevchenko 			q = 1;
7959df461ecSAndy Shevchenko 			mul = m;
796e5262d05SWeike Chen 		}
797e5262d05SWeike Chen 	}
798e5262d05SWeike Chen 
7999df461ecSAndy Shevchenko 	*dds = mul;
8009df461ecSAndy Shevchenko 	return q - 1;
801e5262d05SWeike Chen }
802e5262d05SWeike Chen 
8033343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
804ca632f55SGrant Likely {
8050eca7cf2SJarkko Nikula 	unsigned long ssp_clk = drv_data->master->max_speed_hz;
8063343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
8073343b7a6SMika Westerberg 
8083343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
809ca632f55SGrant Likely 
810ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
811025ffe88SAndy Shevchenko 		return (ssp_clk / (2 * rate) - 1) & 0xff;
812ca632f55SGrant Likely 	else
813025ffe88SAndy Shevchenko 		return (ssp_clk / rate - 1) & 0xfff;
814ca632f55SGrant Likely }
815ca632f55SGrant Likely 
816e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
817d2c2f6a4SAndy Shevchenko 					   int rate)
818e5262d05SWeike Chen {
819d2c2f6a4SAndy Shevchenko 	struct chip_data *chip = drv_data->cur_chip;
820025ffe88SAndy Shevchenko 	unsigned int clk_div;
821e5262d05SWeike Chen 
822e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
823e5262d05SWeike Chen 	case QUARK_X1000_SSP:
8249df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
825eecacf73SDan Carpenter 		break;
826e5262d05SWeike Chen 	default:
827025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
828eecacf73SDan Carpenter 		break;
829e5262d05SWeike Chen 	}
830025ffe88SAndy Shevchenko 	return clk_div << 8;
831e5262d05SWeike Chen }
832e5262d05SWeike Chen 
833ca632f55SGrant Likely static void pump_transfers(unsigned long data)
834ca632f55SGrant Likely {
835ca632f55SGrant Likely 	struct driver_data *drv_data = (struct driver_data *)data;
836ca632f55SGrant Likely 	struct spi_message *message = NULL;
837ca632f55SGrant Likely 	struct spi_transfer *transfer = NULL;
838ca632f55SGrant Likely 	struct spi_transfer *previous = NULL;
839ca632f55SGrant Likely 	struct chip_data *chip = NULL;
840ca632f55SGrant Likely 	u32 clk_div = 0;
841ca632f55SGrant Likely 	u8 bits = 0;
842ca632f55SGrant Likely 	u32 speed = 0;
843ca632f55SGrant Likely 	u32 cr0;
844ca632f55SGrant Likely 	u32 cr1;
845ca632f55SGrant Likely 	u32 dma_thresh = drv_data->cur_chip->dma_threshold;
846ca632f55SGrant Likely 	u32 dma_burst = drv_data->cur_chip->dma_burst_size;
8474fdb2424SWeike Chen 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
848ca632f55SGrant Likely 
849ca632f55SGrant Likely 	/* Get current state information */
850ca632f55SGrant Likely 	message = drv_data->cur_msg;
851ca632f55SGrant Likely 	transfer = drv_data->cur_transfer;
852ca632f55SGrant Likely 	chip = drv_data->cur_chip;
853ca632f55SGrant Likely 
854ca632f55SGrant Likely 	/* Handle for abort */
855ca632f55SGrant Likely 	if (message->state == ERROR_STATE) {
856ca632f55SGrant Likely 		message->status = -EIO;
857ca632f55SGrant Likely 		giveback(drv_data);
858ca632f55SGrant Likely 		return;
859ca632f55SGrant Likely 	}
860ca632f55SGrant Likely 
861ca632f55SGrant Likely 	/* Handle end of message */
862ca632f55SGrant Likely 	if (message->state == DONE_STATE) {
863ca632f55SGrant Likely 		message->status = 0;
864ca632f55SGrant Likely 		giveback(drv_data);
865ca632f55SGrant Likely 		return;
866ca632f55SGrant Likely 	}
867ca632f55SGrant Likely 
868ca632f55SGrant Likely 	/* Delay if requested at end of transfer before CS change */
869ca632f55SGrant Likely 	if (message->state == RUNNING_STATE) {
870ca632f55SGrant Likely 		previous = list_entry(transfer->transfer_list.prev,
871ca632f55SGrant Likely 					struct spi_transfer,
872ca632f55SGrant Likely 					transfer_list);
873ca632f55SGrant Likely 		if (previous->delay_usecs)
874ca632f55SGrant Likely 			udelay(previous->delay_usecs);
875ca632f55SGrant Likely 
876ca632f55SGrant Likely 		/* Drop chip select only if cs_change is requested */
877ca632f55SGrant Likely 		if (previous->cs_change)
878ca632f55SGrant Likely 			cs_deassert(drv_data);
879ca632f55SGrant Likely 	}
880ca632f55SGrant Likely 
881cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
882cd7bed00SMika Westerberg 	if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
883ca632f55SGrant Likely 
884ca632f55SGrant Likely 		/* reject already-mapped transfers; PIO won't always work */
885ca632f55SGrant Likely 		if (message->is_dma_mapped
886ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
887ca632f55SGrant Likely 			dev_err(&drv_data->pdev->dev,
888f6bd03a7SJarkko Nikula 				"pump_transfers: mapped transfer length of "
889f6bd03a7SJarkko Nikula 				"%u is greater than %d\n",
890ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
891ca632f55SGrant Likely 			message->status = -EINVAL;
892ca632f55SGrant Likely 			giveback(drv_data);
893ca632f55SGrant Likely 			return;
894ca632f55SGrant Likely 		}
895ca632f55SGrant Likely 
896ca632f55SGrant Likely 		/* warn ... we force this to PIO mode */
897f6bd03a7SJarkko Nikula 		dev_warn_ratelimited(&message->spi->dev,
898f6bd03a7SJarkko Nikula 				     "pump_transfers: DMA disabled for transfer length %ld "
899ca632f55SGrant Likely 				     "greater than %d\n",
900ca632f55SGrant Likely 				     (long)drv_data->len, MAX_DMA_LEN);
901ca632f55SGrant Likely 	}
902ca632f55SGrant Likely 
903ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
904cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
905ca632f55SGrant Likely 		dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
906ca632f55SGrant Likely 		message->status = -EIO;
907ca632f55SGrant Likely 		giveback(drv_data);
908ca632f55SGrant Likely 		return;
909ca632f55SGrant Likely 	}
910ca632f55SGrant Likely 	drv_data->n_bytes = chip->n_bytes;
911ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
912ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
913ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
914ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
915ca632f55SGrant Likely 	drv_data->rx_dma = transfer->rx_dma;
916ca632f55SGrant Likely 	drv_data->tx_dma = transfer->tx_dma;
917cd7bed00SMika Westerberg 	drv_data->len = transfer->len;
918ca632f55SGrant Likely 	drv_data->write = drv_data->tx ? chip->write : null_writer;
919ca632f55SGrant Likely 	drv_data->read = drv_data->rx ? chip->read : null_reader;
920ca632f55SGrant Likely 
921ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
922ca632f55SGrant Likely 	bits = transfer->bits_per_word;
9234f1474b3SJarkko Nikula 	speed = transfer->speed_hz;
924ca632f55SGrant Likely 
925d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
926ca632f55SGrant Likely 
927ca632f55SGrant Likely 	if (bits <= 8) {
928ca632f55SGrant Likely 		drv_data->n_bytes = 1;
929ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
930ca632f55SGrant Likely 					u8_reader : null_reader;
931ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
932ca632f55SGrant Likely 					u8_writer : null_writer;
933ca632f55SGrant Likely 	} else if (bits <= 16) {
934ca632f55SGrant Likely 		drv_data->n_bytes = 2;
935ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
936ca632f55SGrant Likely 					u16_reader : null_reader;
937ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
938ca632f55SGrant Likely 					u16_writer : null_writer;
939ca632f55SGrant Likely 	} else if (bits <= 32) {
940ca632f55SGrant Likely 		drv_data->n_bytes = 4;
941ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
942ca632f55SGrant Likely 					u32_reader : null_reader;
943ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
944ca632f55SGrant Likely 					u32_writer : null_writer;
945ca632f55SGrant Likely 	}
946196b0e2cSJarkko Nikula 	/*
947196b0e2cSJarkko Nikula 	 * if bits/word is changed in dma mode, then must check the
948196b0e2cSJarkko Nikula 	 * thresholds and burst also
949196b0e2cSJarkko Nikula 	 */
950ca632f55SGrant Likely 	if (chip->enable_dma) {
951cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
952cd7bed00SMika Westerberg 						message->spi,
953ca632f55SGrant Likely 						bits, &dma_burst,
954ca632f55SGrant Likely 						&dma_thresh))
955f6bd03a7SJarkko Nikula 			dev_warn_ratelimited(&message->spi->dev,
956f6bd03a7SJarkko Nikula 					     "pump_transfers: DMA burst size reduced to match bits_per_word\n");
957ca632f55SGrant Likely 	}
958ca632f55SGrant Likely 
959d74c4b1cSAndy Shevchenko 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
9604fdb2424SWeike Chen 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
961d74c4b1cSAndy Shevchenko 	if (!pxa25x_ssp_comp(drv_data))
962d74c4b1cSAndy Shevchenko 		dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
963d74c4b1cSAndy Shevchenko 			drv_data->master->max_speed_hz
964d74c4b1cSAndy Shevchenko 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
965d74c4b1cSAndy Shevchenko 			chip->enable_dma ? "DMA" : "PIO");
966d74c4b1cSAndy Shevchenko 	else
967d74c4b1cSAndy Shevchenko 		dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
968d74c4b1cSAndy Shevchenko 			drv_data->master->max_speed_hz / 2
969d74c4b1cSAndy Shevchenko 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
970d74c4b1cSAndy Shevchenko 			chip->enable_dma ? "DMA" : "PIO");
971ca632f55SGrant Likely 
972ca632f55SGrant Likely 	message->state = RUNNING_STATE;
973ca632f55SGrant Likely 
974ca632f55SGrant Likely 	drv_data->dma_mapped = 0;
975cd7bed00SMika Westerberg 	if (pxa2xx_spi_dma_is_possible(drv_data->len))
976cd7bed00SMika Westerberg 		drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
977ca632f55SGrant Likely 	if (drv_data->dma_mapped) {
978ca632f55SGrant Likely 
979ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
980cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
981ca632f55SGrant Likely 
982cd7bed00SMika Westerberg 		pxa2xx_spi_dma_prepare(drv_data, dma_burst);
983ca632f55SGrant Likely 
984ca632f55SGrant Likely 		/* Clear status and start DMA engine */
985ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
986c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
987cd7bed00SMika Westerberg 
988cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
989ca632f55SGrant Likely 	} else {
990ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
991ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
992ca632f55SGrant Likely 
993ca632f55SGrant Likely 		/* Clear status  */
994ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
995ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
996ca632f55SGrant Likely 	}
997ca632f55SGrant Likely 
998a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
999c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
1000c039dd27SJarkko Nikula 		    != chip->lpss_rx_threshold)
1001c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSIRF,
1002c039dd27SJarkko Nikula 					 chip->lpss_rx_threshold);
1003c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
1004c039dd27SJarkko Nikula 		    != chip->lpss_tx_threshold)
1005c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSITF,
1006c039dd27SJarkko Nikula 					 chip->lpss_tx_threshold);
1007a0d2642eSMika Westerberg 	}
1008a0d2642eSMika Westerberg 
1009e5262d05SWeike Chen 	if (is_quark_x1000_ssp(drv_data) &&
1010c039dd27SJarkko Nikula 	    (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
1011c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
1012e5262d05SWeike Chen 
1013ca632f55SGrant Likely 	/* see if we need to reload the config registers */
1014c039dd27SJarkko Nikula 	if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1015c039dd27SJarkko Nikula 	    || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1016c039dd27SJarkko Nikula 	    != (cr1 & change_mask)) {
1017ca632f55SGrant Likely 		/* stop the SSP, and update the other bits */
1018c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
1019ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1020c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1021ca632f55SGrant Likely 		/* first set CR1 without interrupt and service enables */
1022c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
1023ca632f55SGrant Likely 		/* restart the SSP */
1024c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0);
1025ca632f55SGrant Likely 
1026ca632f55SGrant Likely 	} else {
1027ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1028c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1029ca632f55SGrant Likely 	}
1030ca632f55SGrant Likely 
1031ca632f55SGrant Likely 	cs_assert(drv_data);
1032ca632f55SGrant Likely 
1033ca632f55SGrant Likely 	/* after chip select, release the data by enabling service
1034ca632f55SGrant Likely 	 * requests and interrupts, without changing any mode bits */
1035c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1036ca632f55SGrant Likely }
1037ca632f55SGrant Likely 
10387f86bde9SMika Westerberg static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
10397f86bde9SMika Westerberg 					   struct spi_message *msg)
1040ca632f55SGrant Likely {
10417f86bde9SMika Westerberg 	struct driver_data *drv_data = spi_master_get_devdata(master);
1042ca632f55SGrant Likely 
10437f86bde9SMika Westerberg 	drv_data->cur_msg = msg;
1044ca632f55SGrant Likely 	/* Initial message state*/
1045ca632f55SGrant Likely 	drv_data->cur_msg->state = START_STATE;
1046ca632f55SGrant Likely 	drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1047ca632f55SGrant Likely 						struct spi_transfer,
1048ca632f55SGrant Likely 						transfer_list);
1049ca632f55SGrant Likely 
1050ca632f55SGrant Likely 	/* prepare to setup the SSP, in pump_transfers, using the per
1051ca632f55SGrant Likely 	 * chip configuration */
1052ca632f55SGrant Likely 	drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
1053ca632f55SGrant Likely 
1054ca632f55SGrant Likely 	/* Mark as busy and launch transfers */
1055ca632f55SGrant Likely 	tasklet_schedule(&drv_data->pump_transfers);
1056ca632f55SGrant Likely 	return 0;
1057ca632f55SGrant Likely }
1058ca632f55SGrant Likely 
10597d94a505SMika Westerberg static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
10607d94a505SMika Westerberg {
10617d94a505SMika Westerberg 	struct driver_data *drv_data = spi_master_get_devdata(master);
10627d94a505SMika Westerberg 
10637d94a505SMika Westerberg 	/* Disable the SSP now */
1064c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0,
1065c039dd27SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
10667d94a505SMika Westerberg 
10677d94a505SMika Westerberg 	return 0;
10687d94a505SMika Westerberg }
10697d94a505SMika Westerberg 
1070ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1071ca632f55SGrant Likely 		    struct pxa2xx_spi_chip *chip_info)
1072ca632f55SGrant Likely {
1073ca632f55SGrant Likely 	int err = 0;
1074ca632f55SGrant Likely 
1075ca632f55SGrant Likely 	if (chip == NULL || chip_info == NULL)
1076ca632f55SGrant Likely 		return 0;
1077ca632f55SGrant Likely 
1078ca632f55SGrant Likely 	/* NOTE: setup() can be called multiple times, possibly with
1079ca632f55SGrant Likely 	 * different chip_info, release previously requested GPIO
1080ca632f55SGrant Likely 	 */
1081ca632f55SGrant Likely 	if (gpio_is_valid(chip->gpio_cs))
1082ca632f55SGrant Likely 		gpio_free(chip->gpio_cs);
1083ca632f55SGrant Likely 
1084ca632f55SGrant Likely 	/* If (*cs_control) is provided, ignore GPIO chip select */
1085ca632f55SGrant Likely 	if (chip_info->cs_control) {
1086ca632f55SGrant Likely 		chip->cs_control = chip_info->cs_control;
1087ca632f55SGrant Likely 		return 0;
1088ca632f55SGrant Likely 	}
1089ca632f55SGrant Likely 
1090ca632f55SGrant Likely 	if (gpio_is_valid(chip_info->gpio_cs)) {
1091ca632f55SGrant Likely 		err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1092ca632f55SGrant Likely 		if (err) {
1093f6bd03a7SJarkko Nikula 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1094f6bd03a7SJarkko Nikula 				chip_info->gpio_cs);
1095ca632f55SGrant Likely 			return err;
1096ca632f55SGrant Likely 		}
1097ca632f55SGrant Likely 
1098ca632f55SGrant Likely 		chip->gpio_cs = chip_info->gpio_cs;
1099ca632f55SGrant Likely 		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1100ca632f55SGrant Likely 
1101ca632f55SGrant Likely 		err = gpio_direction_output(chip->gpio_cs,
1102ca632f55SGrant Likely 					!chip->gpio_cs_inverted);
1103ca632f55SGrant Likely 	}
1104ca632f55SGrant Likely 
1105ca632f55SGrant Likely 	return err;
1106ca632f55SGrant Likely }
1107ca632f55SGrant Likely 
1108ca632f55SGrant Likely static int setup(struct spi_device *spi)
1109ca632f55SGrant Likely {
1110ca632f55SGrant Likely 	struct pxa2xx_spi_chip *chip_info = NULL;
1111ca632f55SGrant Likely 	struct chip_data *chip;
1112dccf7369SJarkko Nikula 	const struct lpss_config *config;
1113ca632f55SGrant Likely 	struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1114a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1115a0d2642eSMika Westerberg 
1116e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1117e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1118e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1119e5262d05SWeike Chen 		tx_hi_thres = 0;
1120e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1121e5262d05SWeike Chen 		break;
112203fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
112303fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
112434cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1125dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1126dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1127dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1128dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1129e5262d05SWeike Chen 		break;
1130e5262d05SWeike Chen 	default:
1131a0d2642eSMika Westerberg 		tx_thres = TX_THRESH_DFLT;
1132a0d2642eSMika Westerberg 		tx_hi_thres = 0;
1133a0d2642eSMika Westerberg 		rx_thres = RX_THRESH_DFLT;
1134e5262d05SWeike Chen 		break;
1135a0d2642eSMika Westerberg 	}
1136ca632f55SGrant Likely 
1137ca632f55SGrant Likely 	/* Only alloc on first setup */
1138ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1139ca632f55SGrant Likely 	if (!chip) {
1140ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
11419deae459SJingoo Han 		if (!chip)
1142ca632f55SGrant Likely 			return -ENOMEM;
1143ca632f55SGrant Likely 
1144ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1145ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1146f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1147f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1148ca632f55SGrant Likely 				kfree(chip);
1149ca632f55SGrant Likely 				return -EINVAL;
1150ca632f55SGrant Likely 			}
1151ca632f55SGrant Likely 
1152ca632f55SGrant Likely 			chip->frm = spi->chip_select;
1153ca632f55SGrant Likely 		} else
1154ca632f55SGrant Likely 			chip->gpio_cs = -1;
1155ca632f55SGrant Likely 		chip->enable_dma = 0;
1156ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1157ca632f55SGrant Likely 	}
1158ca632f55SGrant Likely 
1159ca632f55SGrant Likely 	/* protocol drivers may change the chip settings, so...
1160ca632f55SGrant Likely 	 * if chip_info exists, use it */
1161ca632f55SGrant Likely 	chip_info = spi->controller_data;
1162ca632f55SGrant Likely 
1163ca632f55SGrant Likely 	/* chip_info isn't always needed */
1164ca632f55SGrant Likely 	chip->cr1 = 0;
1165ca632f55SGrant Likely 	if (chip_info) {
1166ca632f55SGrant Likely 		if (chip_info->timeout)
1167ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1168ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1169ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1170a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1171a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1172ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1173ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1174ca632f55SGrant Likely 		chip->enable_dma = drv_data->master_info->enable_dma;
1175ca632f55SGrant Likely 		chip->dma_threshold = 0;
1176ca632f55SGrant Likely 		if (chip_info->enable_loopback)
1177ca632f55SGrant Likely 			chip->cr1 = SSCR1_LBM;
1178a3496855SMika Westerberg 	} else if (ACPI_HANDLE(&spi->dev)) {
1179a3496855SMika Westerberg 		/*
1180a3496855SMika Westerberg 		 * Slave devices enumerated from ACPI namespace don't
1181a3496855SMika Westerberg 		 * usually have chip_info but we still might want to use
1182a3496855SMika Westerberg 		 * DMA with them.
1183a3496855SMika Westerberg 		 */
1184a3496855SMika Westerberg 		chip->enable_dma = drv_data->master_info->enable_dma;
1185ca632f55SGrant Likely 	}
1186ca632f55SGrant Likely 
1187a0d2642eSMika Westerberg 	chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1188a0d2642eSMika Westerberg 	chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1189a0d2642eSMika Westerberg 				| SSITF_TxHiThresh(tx_hi_thres);
1190a0d2642eSMika Westerberg 
1191ca632f55SGrant Likely 	/* set dma burst and threshold outside of chip_info path so that if
1192ca632f55SGrant Likely 	 * chip_info goes away after setting chip->enable_dma, the
1193ca632f55SGrant Likely 	 * burst and threshold can still respond to changes in bits_per_word */
1194ca632f55SGrant Likely 	if (chip->enable_dma) {
1195ca632f55SGrant Likely 		/* set up legal burst and threshold for dma */
1196cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1197cd7bed00SMika Westerberg 						spi->bits_per_word,
1198ca632f55SGrant Likely 						&chip->dma_burst_size,
1199ca632f55SGrant Likely 						&chip->dma_threshold)) {
1200f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1201f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1202ca632f55SGrant Likely 		}
1203ca632f55SGrant Likely 	}
1204ca632f55SGrant Likely 
1205e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1206e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1207e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1208e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1209e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1210e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1211e5262d05SWeike Chen 		break;
1212e5262d05SWeike Chen 	default:
1213e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1214e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1215e5262d05SWeike Chen 		break;
1216e5262d05SWeike Chen 	}
1217e5262d05SWeike Chen 
1218ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1219ca632f55SGrant Likely 	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1220ca632f55SGrant Likely 			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1221ca632f55SGrant Likely 
1222b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1223b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1224b833172fSMika Westerberg 
1225ca632f55SGrant Likely 	if (spi->bits_per_word <= 8) {
1226ca632f55SGrant Likely 		chip->n_bytes = 1;
1227ca632f55SGrant Likely 		chip->read = u8_reader;
1228ca632f55SGrant Likely 		chip->write = u8_writer;
1229ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 16) {
1230ca632f55SGrant Likely 		chip->n_bytes = 2;
1231ca632f55SGrant Likely 		chip->read = u16_reader;
1232ca632f55SGrant Likely 		chip->write = u16_writer;
1233ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 32) {
1234ca632f55SGrant Likely 		chip->n_bytes = 4;
1235ca632f55SGrant Likely 		chip->read = u32_reader;
1236ca632f55SGrant Likely 		chip->write = u32_writer;
1237ca632f55SGrant Likely 	}
1238ca632f55SGrant Likely 
1239ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1240ca632f55SGrant Likely 
1241ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
1242ca632f55SGrant Likely 		return 0;
1243ca632f55SGrant Likely 
1244ca632f55SGrant Likely 	return setup_cs(spi, chip, chip_info);
1245ca632f55SGrant Likely }
1246ca632f55SGrant Likely 
1247ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1248ca632f55SGrant Likely {
1249ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
1250ca632f55SGrant Likely 	struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1251ca632f55SGrant Likely 
1252ca632f55SGrant Likely 	if (!chip)
1253ca632f55SGrant Likely 		return;
1254ca632f55SGrant Likely 
1255ca632f55SGrant Likely 	if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
1256ca632f55SGrant Likely 		gpio_free(chip->gpio_cs);
1257ca632f55SGrant Likely 
1258ca632f55SGrant Likely 	kfree(chip);
1259ca632f55SGrant Likely }
1260ca632f55SGrant Likely 
1261a3496855SMika Westerberg #ifdef CONFIG_ACPI
126203fbf488SJarkko Nikula 
12638422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
126403fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
126503fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
126603fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
126703fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
126803fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
126903fbf488SJarkko Nikula 	{ "8086228E", LPSS_BYT_SSP },
127003fbf488SJarkko Nikula 	{ },
127103fbf488SJarkko Nikula };
127203fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
127303fbf488SJarkko Nikula 
127434cadd9cSJarkko Nikula /*
127534cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
127634cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
127734cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
127834cadd9cSJarkko Nikula  */
127934cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
128034cadd9cSJarkko Nikula 	/* SPT-LP */
128134cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
128234cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
128334cadd9cSJarkko Nikula 	/* SPT-H */
128434cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
128534cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
128694e5c23dSAxel Lin 	{ },
128734cadd9cSJarkko Nikula };
128834cadd9cSJarkko Nikula 
128934cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
129034cadd9cSJarkko Nikula {
129134cadd9cSJarkko Nikula 	struct device *dev = param;
129234cadd9cSJarkko Nikula 
129334cadd9cSJarkko Nikula 	if (dev != chan->device->dev->parent)
129434cadd9cSJarkko Nikula 		return false;
129534cadd9cSJarkko Nikula 
129634cadd9cSJarkko Nikula 	return true;
129734cadd9cSJarkko Nikula }
129834cadd9cSJarkko Nikula 
1299a3496855SMika Westerberg static struct pxa2xx_spi_master *
1300a3496855SMika Westerberg pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1301a3496855SMika Westerberg {
1302a3496855SMika Westerberg 	struct pxa2xx_spi_master *pdata;
1303a3496855SMika Westerberg 	struct acpi_device *adev;
1304a3496855SMika Westerberg 	struct ssp_device *ssp;
1305a3496855SMika Westerberg 	struct resource *res;
130634cadd9cSJarkko Nikula 	const struct acpi_device_id *adev_id = NULL;
130734cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
1308*3b8b6d05SJarkko Nikula 	unsigned int devid;
1309*3b8b6d05SJarkko Nikula 	int type;
1310a3496855SMika Westerberg 
1311b9f6940aSJarkko Nikula 	adev = ACPI_COMPANION(&pdev->dev);
1312b9f6940aSJarkko Nikula 	if (!adev)
1313a3496855SMika Westerberg 		return NULL;
1314a3496855SMika Westerberg 
131534cadd9cSJarkko Nikula 	if (dev_is_pci(pdev->dev.parent))
131634cadd9cSJarkko Nikula 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
131734cadd9cSJarkko Nikula 					 to_pci_dev(pdev->dev.parent));
131834cadd9cSJarkko Nikula 	else
131934cadd9cSJarkko Nikula 		adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
132034cadd9cSJarkko Nikula 					    &pdev->dev);
132134cadd9cSJarkko Nikula 
132234cadd9cSJarkko Nikula 	if (adev_id)
132334cadd9cSJarkko Nikula 		type = (int)adev_id->driver_data;
132434cadd9cSJarkko Nikula 	else if (pcidev_id)
132534cadd9cSJarkko Nikula 		type = (int)pcidev_id->driver_data;
132603fbf488SJarkko Nikula 	else
132703fbf488SJarkko Nikula 		return NULL;
132803fbf488SJarkko Nikula 
1329cc0ee987SMika Westerberg 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
13309deae459SJingoo Han 	if (!pdata)
1331a3496855SMika Westerberg 		return NULL;
1332a3496855SMika Westerberg 
1333a3496855SMika Westerberg 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1334a3496855SMika Westerberg 	if (!res)
1335a3496855SMika Westerberg 		return NULL;
1336a3496855SMika Westerberg 
1337a3496855SMika Westerberg 	ssp = &pdata->ssp;
1338a3496855SMika Westerberg 
1339a3496855SMika Westerberg 	ssp->phys_base = res->start;
1340cbfd6a21SSachin Kamat 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1341cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
13426dc81f6fSMika Westerberg 		return NULL;
1343a3496855SMika Westerberg 
134434cadd9cSJarkko Nikula 	if (pcidev_id) {
134534cadd9cSJarkko Nikula 		pdata->tx_param = pdev->dev.parent;
134634cadd9cSJarkko Nikula 		pdata->rx_param = pdev->dev.parent;
134734cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
134834cadd9cSJarkko Nikula 	}
134934cadd9cSJarkko Nikula 
1350a3496855SMika Westerberg 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
1351a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
135203fbf488SJarkko Nikula 	ssp->type = type;
1353a3496855SMika Westerberg 	ssp->pdev = pdev;
1354a3496855SMika Westerberg 
1355a3496855SMika Westerberg 	ssp->port_id = -1;
1356*3b8b6d05SJarkko Nikula 	if (adev->pnp.unique_id && !kstrtouint(adev->pnp.unique_id, 0, &devid))
1357a3496855SMika Westerberg 		ssp->port_id = devid;
1358a3496855SMika Westerberg 
1359a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1360cddb339bSMika Westerberg 	pdata->enable_dma = true;
1361a3496855SMika Westerberg 
1362a3496855SMika Westerberg 	return pdata;
1363a3496855SMika Westerberg }
1364a3496855SMika Westerberg 
1365a3496855SMika Westerberg #else
1366a3496855SMika Westerberg static inline struct pxa2xx_spi_master *
1367a3496855SMika Westerberg pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1368a3496855SMika Westerberg {
1369a3496855SMika Westerberg 	return NULL;
1370a3496855SMika Westerberg }
1371a3496855SMika Westerberg #endif
1372a3496855SMika Westerberg 
1373fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1374ca632f55SGrant Likely {
1375ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
1376ca632f55SGrant Likely 	struct pxa2xx_spi_master *platform_info;
1377ca632f55SGrant Likely 	struct spi_master *master;
1378ca632f55SGrant Likely 	struct driver_data *drv_data;
1379ca632f55SGrant Likely 	struct ssp_device *ssp;
1380ca632f55SGrant Likely 	int status;
1381c039dd27SJarkko Nikula 	u32 tmp;
1382ca632f55SGrant Likely 
1383851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1384851bacf5SMika Westerberg 	if (!platform_info) {
1385a3496855SMika Westerberg 		platform_info = pxa2xx_spi_acpi_get_pdata(pdev);
1386a3496855SMika Westerberg 		if (!platform_info) {
1387851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
1388851bacf5SMika Westerberg 			return -ENODEV;
1389851bacf5SMika Westerberg 		}
1390a3496855SMika Westerberg 	}
1391ca632f55SGrant Likely 
1392ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1393851bacf5SMika Westerberg 	if (!ssp)
1394851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1395851bacf5SMika Westerberg 
1396851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
1397851bacf5SMika Westerberg 		dev_err(&pdev->dev, "failed to get ssp\n");
1398ca632f55SGrant Likely 		return -ENODEV;
1399ca632f55SGrant Likely 	}
1400ca632f55SGrant Likely 
1401757fe8d5SJarkko Nikula 	master = spi_alloc_master(dev, sizeof(struct driver_data));
1402ca632f55SGrant Likely 	if (!master) {
1403ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot alloc spi_master\n");
1404ca632f55SGrant Likely 		pxa_ssp_free(ssp);
1405ca632f55SGrant Likely 		return -ENOMEM;
1406ca632f55SGrant Likely 	}
1407ca632f55SGrant Likely 	drv_data = spi_master_get_devdata(master);
1408ca632f55SGrant Likely 	drv_data->master = master;
1409ca632f55SGrant Likely 	drv_data->master_info = platform_info;
1410ca632f55SGrant Likely 	drv_data->pdev = pdev;
1411ca632f55SGrant Likely 	drv_data->ssp = ssp;
1412ca632f55SGrant Likely 
1413ca632f55SGrant Likely 	master->dev.parent = &pdev->dev;
1414ca632f55SGrant Likely 	master->dev.of_node = pdev->dev.of_node;
1415ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
1416b833172fSMika Westerberg 	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1417ca632f55SGrant Likely 
1418851bacf5SMika Westerberg 	master->bus_num = ssp->port_id;
1419ca632f55SGrant Likely 	master->num_chipselect = platform_info->num_chipselect;
1420ca632f55SGrant Likely 	master->dma_alignment = DMA_ALIGNMENT;
1421ca632f55SGrant Likely 	master->cleanup = cleanup;
1422ca632f55SGrant Likely 	master->setup = setup;
14237f86bde9SMika Westerberg 	master->transfer_one_message = pxa2xx_spi_transfer_one_message;
14247d94a505SMika Westerberg 	master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
14257dd62787SMark Brown 	master->auto_runtime_pm = true;
1426ca632f55SGrant Likely 
1427ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1428ca632f55SGrant Likely 
1429ca632f55SGrant Likely 	drv_data->ioaddr = ssp->mmio_base;
1430ca632f55SGrant Likely 	drv_data->ssdr_physical = ssp->phys_base + SSDR;
1431ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1432e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1433e5262d05SWeike Chen 		case QUARK_X1000_SSP:
1434e5262d05SWeike Chen 			master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1435e5262d05SWeike Chen 			break;
1436e5262d05SWeike Chen 		default:
143724778be2SStephen Warren 			master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1438e5262d05SWeike Chen 			break;
1439e5262d05SWeike Chen 		}
1440e5262d05SWeike Chen 
1441ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1442ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1443ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1444ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1445ca632f55SGrant Likely 	} else {
144624778be2SStephen Warren 		master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1447ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
14485928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1449ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1450ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1451ca632f55SGrant Likely 	}
1452ca632f55SGrant Likely 
1453ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1454ca632f55SGrant Likely 			drv_data);
1455ca632f55SGrant Likely 	if (status < 0) {
1456ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
1457ca632f55SGrant Likely 		goto out_error_master_alloc;
1458ca632f55SGrant Likely 	}
1459ca632f55SGrant Likely 
1460ca632f55SGrant Likely 	/* Setup DMA if requested */
1461ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1462cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1463cd7bed00SMika Westerberg 		if (status) {
1464cddb339bSMika Westerberg 			dev_dbg(dev, "no DMA channels available, using PIO\n");
1465cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1466ca632f55SGrant Likely 		}
1467ca632f55SGrant Likely 	}
1468ca632f55SGrant Likely 
1469ca632f55SGrant Likely 	/* Enable SOC clock */
14703343b7a6SMika Westerberg 	clk_prepare_enable(ssp->clk);
14713343b7a6SMika Westerberg 
14720eca7cf2SJarkko Nikula 	master->max_speed_hz = clk_get_rate(ssp->clk);
1473ca632f55SGrant Likely 
1474ca632f55SGrant Likely 	/* Load default SSP configuration */
1475c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
1476e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1477e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1478c039dd27SJarkko Nikula 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT)
1479c039dd27SJarkko Nikula 		      | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1480c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1481e5262d05SWeike Chen 
1482e5262d05SWeike Chen 		/* using the Motorola SPI protocol and use 8 bit frame */
1483c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0,
1484c039dd27SJarkko Nikula 				 QUARK_X1000_SSCR0_Motorola
1485c039dd27SJarkko Nikula 				 | QUARK_X1000_SSCR0_DataSize(8));
1486e5262d05SWeike Chen 		break;
1487e5262d05SWeike Chen 	default:
1488c039dd27SJarkko Nikula 		tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1489c039dd27SJarkko Nikula 		      SSCR1_TxTresh(TX_THRESH_DFLT);
1490c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1491c039dd27SJarkko Nikula 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1492c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1493e5262d05SWeike Chen 		break;
1494e5262d05SWeike Chen 	}
1495e5262d05SWeike Chen 
1496ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1497c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1498e5262d05SWeike Chen 
1499e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1500c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1501ca632f55SGrant Likely 
15027566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
1503a0d2642eSMika Westerberg 		lpss_ssp_setup(drv_data);
1504a0d2642eSMika Westerberg 
15057f86bde9SMika Westerberg 	tasklet_init(&drv_data->pump_transfers, pump_transfers,
15067f86bde9SMika Westerberg 		     (unsigned long)drv_data);
1507ca632f55SGrant Likely 
1508836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1509836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1510836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1511836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1512836d1a22SAntonio Ospite 
1513ca632f55SGrant Likely 	/* Register with the SPI framework */
1514ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
1515a807fcd0SJingoo Han 	status = devm_spi_register_master(&pdev->dev, master);
1516ca632f55SGrant Likely 	if (status != 0) {
1517ca632f55SGrant Likely 		dev_err(&pdev->dev, "problem registering spi master\n");
15187f86bde9SMika Westerberg 		goto out_error_clock_enabled;
1519ca632f55SGrant Likely 	}
1520ca632f55SGrant Likely 
1521ca632f55SGrant Likely 	return status;
1522ca632f55SGrant Likely 
1523ca632f55SGrant Likely out_error_clock_enabled:
15243343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1525cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1526ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1527ca632f55SGrant Likely 
1528ca632f55SGrant Likely out_error_master_alloc:
1529ca632f55SGrant Likely 	spi_master_put(master);
1530ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1531ca632f55SGrant Likely 	return status;
1532ca632f55SGrant Likely }
1533ca632f55SGrant Likely 
1534ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1535ca632f55SGrant Likely {
1536ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
1537ca632f55SGrant Likely 	struct ssp_device *ssp;
1538ca632f55SGrant Likely 
1539ca632f55SGrant Likely 	if (!drv_data)
1540ca632f55SGrant Likely 		return 0;
1541ca632f55SGrant Likely 	ssp = drv_data->ssp;
1542ca632f55SGrant Likely 
15437d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
15447d94a505SMika Westerberg 
1545ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
1546c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
15473343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1548ca632f55SGrant Likely 
1549ca632f55SGrant Likely 	/* Release DMA */
1550cd7bed00SMika Westerberg 	if (drv_data->master_info->enable_dma)
1551cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1552ca632f55SGrant Likely 
15537d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
15547d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
15557d94a505SMika Westerberg 
1556ca632f55SGrant Likely 	/* Release IRQ */
1557ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1558ca632f55SGrant Likely 
1559ca632f55SGrant Likely 	/* Release SSP */
1560ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1561ca632f55SGrant Likely 
1562ca632f55SGrant Likely 	return 0;
1563ca632f55SGrant Likely }
1564ca632f55SGrant Likely 
1565ca632f55SGrant Likely static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1566ca632f55SGrant Likely {
1567ca632f55SGrant Likely 	int status = 0;
1568ca632f55SGrant Likely 
1569ca632f55SGrant Likely 	if ((status = pxa2xx_spi_remove(pdev)) != 0)
1570ca632f55SGrant Likely 		dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1571ca632f55SGrant Likely }
1572ca632f55SGrant Likely 
1573382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP
1574ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1575ca632f55SGrant Likely {
1576ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1577ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1578ca632f55SGrant Likely 	int status = 0;
1579ca632f55SGrant Likely 
15807f86bde9SMika Westerberg 	status = spi_master_suspend(drv_data->master);
1581ca632f55SGrant Likely 	if (status != 0)
1582ca632f55SGrant Likely 		return status;
1583c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
15842b9375b9SDmitry Eremin-Solenikov 
15852b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
15863343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1587ca632f55SGrant Likely 
1588ca632f55SGrant Likely 	return 0;
1589ca632f55SGrant Likely }
1590ca632f55SGrant Likely 
1591ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1592ca632f55SGrant Likely {
1593ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1594ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1595ca632f55SGrant Likely 	int status = 0;
1596ca632f55SGrant Likely 
1597ca632f55SGrant Likely 	/* Enable the SSP clock */
15982b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
15993343b7a6SMika Westerberg 		clk_prepare_enable(ssp->clk);
1600ca632f55SGrant Likely 
1601c50325f7SChew, Chiau Ee 	/* Restore LPSS private register bits */
160248421adfSJarkko Nikula 	if (is_lpss_ssp(drv_data))
1603c50325f7SChew, Chiau Ee 		lpss_ssp_setup(drv_data);
1604c50325f7SChew, Chiau Ee 
1605ca632f55SGrant Likely 	/* Start the queue running */
16067f86bde9SMika Westerberg 	status = spi_master_resume(drv_data->master);
1607ca632f55SGrant Likely 	if (status != 0) {
1608ca632f55SGrant Likely 		dev_err(dev, "problem starting queue (%d)\n", status);
1609ca632f55SGrant Likely 		return status;
1610ca632f55SGrant Likely 	}
1611ca632f55SGrant Likely 
1612ca632f55SGrant Likely 	return 0;
1613ca632f55SGrant Likely }
16147d94a505SMika Westerberg #endif
16157d94a505SMika Westerberg 
1616ec833050SRafael J. Wysocki #ifdef CONFIG_PM
16177d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
16187d94a505SMika Westerberg {
16197d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
16207d94a505SMika Westerberg 
16217d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
16227d94a505SMika Westerberg 	return 0;
16237d94a505SMika Westerberg }
16247d94a505SMika Westerberg 
16257d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
16267d94a505SMika Westerberg {
16277d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
16287d94a505SMika Westerberg 
16297d94a505SMika Westerberg 	clk_prepare_enable(drv_data->ssp->clk);
16307d94a505SMika Westerberg 	return 0;
16317d94a505SMika Westerberg }
16327d94a505SMika Westerberg #endif
1633ca632f55SGrant Likely 
1634ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
16357d94a505SMika Westerberg 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
16367d94a505SMika Westerberg 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
16377d94a505SMika Westerberg 			   pxa2xx_spi_runtime_resume, NULL)
1638ca632f55SGrant Likely };
1639ca632f55SGrant Likely 
1640ca632f55SGrant Likely static struct platform_driver driver = {
1641ca632f55SGrant Likely 	.driver = {
1642ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
1643ca632f55SGrant Likely 		.pm	= &pxa2xx_spi_pm_ops,
1644a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
1645ca632f55SGrant Likely 	},
1646ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
1647ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
1648ca632f55SGrant Likely 	.shutdown = pxa2xx_spi_shutdown,
1649ca632f55SGrant Likely };
1650ca632f55SGrant Likely 
1651ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
1652ca632f55SGrant Likely {
1653ca632f55SGrant Likely 	return platform_driver_register(&driver);
1654ca632f55SGrant Likely }
1655ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
1656ca632f55SGrant Likely 
1657ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
1658ca632f55SGrant Likely {
1659ca632f55SGrant Likely 	platform_driver_unregister(&driver);
1660ca632f55SGrant Likely }
1661ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
1662