xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision 683f65ded66a9a7ff01ed7280804d2132ebfdf7e)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
4a0d2642eSMika Westerberg  * Copyright (C) 2013, Intel Corporation
5ca632f55SGrant Likely  */
6ca632f55SGrant Likely 
75ce25705SAndy Shevchenko #include <linux/acpi.h>
88b136baaSJarkko Nikula #include <linux/bitops.h>
95ce25705SAndy Shevchenko #include <linux/clk.h>
105ce25705SAndy Shevchenko #include <linux/delay.h>
11ca632f55SGrant Likely #include <linux/device.h>
12cbfd6a21SSachin Kamat #include <linux/err.h>
135ce25705SAndy Shevchenko #include <linux/errno.h>
145ce25705SAndy Shevchenko #include <linux/gpio/consumer.h>
155ce25705SAndy Shevchenko #include <linux/gpio.h>
165ce25705SAndy Shevchenko #include <linux/init.h>
17ca632f55SGrant Likely #include <linux/interrupt.h>
185ce25705SAndy Shevchenko #include <linux/ioport.h>
199df461ecSAndy Shevchenko #include <linux/kernel.h>
205ce25705SAndy Shevchenko #include <linux/module.h>
21ae8fbf1dSAndy Shevchenko #include <linux/mod_devicetable.h>
22ae8fbf1dSAndy Shevchenko #include <linux/of.h>
2334cadd9cSJarkko Nikula #include <linux/pci.h>
24ca632f55SGrant Likely #include <linux/platform_device.h>
255ce25705SAndy Shevchenko #include <linux/pm_runtime.h>
26f2faa3ecSAndy Shevchenko #include <linux/property.h>
275ce25705SAndy Shevchenko #include <linux/slab.h>
28ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h>
29ca632f55SGrant Likely #include <linux/spi/spi.h>
30ca632f55SGrant Likely 
31cd7bed00SMika Westerberg #include "spi-pxa2xx.h"
32ca632f55SGrant Likely 
33ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street");
34ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
35ca632f55SGrant Likely MODULE_LICENSE("GPL");
36ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi");
37ca632f55SGrant Likely 
38ca632f55SGrant Likely #define TIMOUT_DFLT		1000
39ca632f55SGrant Likely 
40ca632f55SGrant Likely /*
41ca632f55SGrant Likely  * for testing SSCR1 changes that require SSP restart, basically
42ca632f55SGrant Likely  * everything except the service and interrupt enables, the pxa270 developer
43ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
44ca632f55SGrant Likely  * list, but the PXA255 dev man says all bits without really meaning the
45ca632f55SGrant Likely  * service and interrupt enables
46ca632f55SGrant Likely  */
47ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
48ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
49ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
50ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
51ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
52ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
53ca632f55SGrant Likely 
54e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
55e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
56e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
57e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
58e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
59e5262d05SWeike Chen 
607c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
617c7289a4SAndy Shevchenko 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
627c7289a4SAndy Shevchenko 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
637c7289a4SAndy Shevchenko 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
647c7289a4SAndy Shevchenko 				| CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
657c7289a4SAndy Shevchenko 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
667c7289a4SAndy Shevchenko 
67624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
68624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE			BIT(0)
69624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
708b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT			9
718b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
72a0d2642eSMika Westerberg 
73*683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE 0x38
74*683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
75*683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
76*683f65deSEvan Green 
77dccf7369SJarkko Nikula struct lpss_config {
78dccf7369SJarkko Nikula 	/* LPSS offset from drv_data->ioaddr */
79dccf7369SJarkko Nikula 	unsigned offset;
80dccf7369SJarkko Nikula 	/* Register offsets from drv_data->lpss_base or -1 */
81dccf7369SJarkko Nikula 	int reg_general;
82dccf7369SJarkko Nikula 	int reg_ssp;
83dccf7369SJarkko Nikula 	int reg_cs_ctrl;
848b136baaSJarkko Nikula 	int reg_capabilities;
85dccf7369SJarkko Nikula 	/* FIFO thresholds */
86dccf7369SJarkko Nikula 	u32 rx_threshold;
87dccf7369SJarkko Nikula 	u32 tx_threshold_lo;
88dccf7369SJarkko Nikula 	u32 tx_threshold_hi;
89c1e4a53cSMika Westerberg 	/* Chip select control */
90c1e4a53cSMika Westerberg 	unsigned cs_sel_shift;
91c1e4a53cSMika Westerberg 	unsigned cs_sel_mask;
9230f3a6abSMika Westerberg 	unsigned cs_num;
93*683f65deSEvan Green 	/* Quirks */
94*683f65deSEvan Green 	unsigned cs_clk_stays_gated : 1;
95dccf7369SJarkko Nikula };
96dccf7369SJarkko Nikula 
97dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */
98dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = {
99dccf7369SJarkko Nikula 	{	/* LPSS_LPT_SSP */
100dccf7369SJarkko Nikula 		.offset = 0x800,
101dccf7369SJarkko Nikula 		.reg_general = 0x08,
102dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
103dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1048b136baaSJarkko Nikula 		.reg_capabilities = -1,
105dccf7369SJarkko Nikula 		.rx_threshold = 64,
106dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
107dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
108dccf7369SJarkko Nikula 	},
109dccf7369SJarkko Nikula 	{	/* LPSS_BYT_SSP */
110dccf7369SJarkko Nikula 		.offset = 0x400,
111dccf7369SJarkko Nikula 		.reg_general = 0x08,
112dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
113dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1148b136baaSJarkko Nikula 		.reg_capabilities = -1,
115dccf7369SJarkko Nikula 		.rx_threshold = 64,
116dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
117dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
118dccf7369SJarkko Nikula 	},
11930f3a6abSMika Westerberg 	{	/* LPSS_BSW_SSP */
12030f3a6abSMika Westerberg 		.offset = 0x400,
12130f3a6abSMika Westerberg 		.reg_general = 0x08,
12230f3a6abSMika Westerberg 		.reg_ssp = 0x0c,
12330f3a6abSMika Westerberg 		.reg_cs_ctrl = 0x18,
12430f3a6abSMika Westerberg 		.reg_capabilities = -1,
12530f3a6abSMika Westerberg 		.rx_threshold = 64,
12630f3a6abSMika Westerberg 		.tx_threshold_lo = 160,
12730f3a6abSMika Westerberg 		.tx_threshold_hi = 224,
12830f3a6abSMika Westerberg 		.cs_sel_shift = 2,
12930f3a6abSMika Westerberg 		.cs_sel_mask = 1 << 2,
13030f3a6abSMika Westerberg 		.cs_num = 2,
13130f3a6abSMika Westerberg 	},
13234cadd9cSJarkko Nikula 	{	/* LPSS_SPT_SSP */
13334cadd9cSJarkko Nikula 		.offset = 0x200,
13434cadd9cSJarkko Nikula 		.reg_general = -1,
13534cadd9cSJarkko Nikula 		.reg_ssp = 0x20,
13634cadd9cSJarkko Nikula 		.reg_cs_ctrl = 0x24,
13766ec246eSJarkko Nikula 		.reg_capabilities = -1,
13834cadd9cSJarkko Nikula 		.rx_threshold = 1,
13934cadd9cSJarkko Nikula 		.tx_threshold_lo = 32,
14034cadd9cSJarkko Nikula 		.tx_threshold_hi = 56,
14134cadd9cSJarkko Nikula 	},
142b7c08cf8SJarkko Nikula 	{	/* LPSS_BXT_SSP */
143b7c08cf8SJarkko Nikula 		.offset = 0x200,
144b7c08cf8SJarkko Nikula 		.reg_general = -1,
145b7c08cf8SJarkko Nikula 		.reg_ssp = 0x20,
146b7c08cf8SJarkko Nikula 		.reg_cs_ctrl = 0x24,
147b7c08cf8SJarkko Nikula 		.reg_capabilities = 0xfc,
148b7c08cf8SJarkko Nikula 		.rx_threshold = 1,
149b7c08cf8SJarkko Nikula 		.tx_threshold_lo = 16,
150b7c08cf8SJarkko Nikula 		.tx_threshold_hi = 48,
151c1e4a53cSMika Westerberg 		.cs_sel_shift = 8,
152c1e4a53cSMika Westerberg 		.cs_sel_mask = 3 << 8,
153b7c08cf8SJarkko Nikula 	},
154fc0b2accSJarkko Nikula 	{	/* LPSS_CNL_SSP */
155fc0b2accSJarkko Nikula 		.offset = 0x200,
156fc0b2accSJarkko Nikula 		.reg_general = -1,
157fc0b2accSJarkko Nikula 		.reg_ssp = 0x20,
158fc0b2accSJarkko Nikula 		.reg_cs_ctrl = 0x24,
159fc0b2accSJarkko Nikula 		.reg_capabilities = 0xfc,
160fc0b2accSJarkko Nikula 		.rx_threshold = 1,
161fc0b2accSJarkko Nikula 		.tx_threshold_lo = 32,
162fc0b2accSJarkko Nikula 		.tx_threshold_hi = 56,
163fc0b2accSJarkko Nikula 		.cs_sel_shift = 8,
164fc0b2accSJarkko Nikula 		.cs_sel_mask = 3 << 8,
165*683f65deSEvan Green 		.cs_clk_stays_gated = true,
166fc0b2accSJarkko Nikula 	},
167dccf7369SJarkko Nikula };
168dccf7369SJarkko Nikula 
169dccf7369SJarkko Nikula static inline const struct lpss_config
170dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data)
171dccf7369SJarkko Nikula {
172dccf7369SJarkko Nikula 	return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
173dccf7369SJarkko Nikula }
174dccf7369SJarkko Nikula 
175a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data)
176a0d2642eSMika Westerberg {
17703fbf488SJarkko Nikula 	switch (drv_data->ssp_type) {
17803fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
17903fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
18030f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
18134cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
182b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
183fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
18403fbf488SJarkko Nikula 		return true;
18503fbf488SJarkko Nikula 	default:
18603fbf488SJarkko Nikula 		return false;
18703fbf488SJarkko Nikula 	}
188a0d2642eSMika Westerberg }
189a0d2642eSMika Westerberg 
190e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
191e5262d05SWeike Chen {
192e5262d05SWeike Chen 	return drv_data->ssp_type == QUARK_X1000_SSP;
193e5262d05SWeike Chen }
194e5262d05SWeike Chen 
1954fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
1964fdb2424SWeike Chen {
1974fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
198e5262d05SWeike Chen 	case QUARK_X1000_SSP:
199e5262d05SWeike Chen 		return QUARK_X1000_SSCR1_CHANGE_MASK;
2007c7289a4SAndy Shevchenko 	case CE4100_SSP:
2017c7289a4SAndy Shevchenko 		return CE4100_SSCR1_CHANGE_MASK;
2024fdb2424SWeike Chen 	default:
2034fdb2424SWeike Chen 		return SSCR1_CHANGE_MASK;
2044fdb2424SWeike Chen 	}
2054fdb2424SWeike Chen }
2064fdb2424SWeike Chen 
2074fdb2424SWeike Chen static u32
2084fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
2094fdb2424SWeike Chen {
2104fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
211e5262d05SWeike Chen 	case QUARK_X1000_SSP:
212e5262d05SWeike Chen 		return RX_THRESH_QUARK_X1000_DFLT;
2137c7289a4SAndy Shevchenko 	case CE4100_SSP:
2147c7289a4SAndy Shevchenko 		return RX_THRESH_CE4100_DFLT;
2154fdb2424SWeike Chen 	default:
2164fdb2424SWeike Chen 		return RX_THRESH_DFLT;
2174fdb2424SWeike Chen 	}
2184fdb2424SWeike Chen }
2194fdb2424SWeike Chen 
2204fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
2214fdb2424SWeike Chen {
2224fdb2424SWeike Chen 	u32 mask;
2234fdb2424SWeike Chen 
2244fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
225e5262d05SWeike Chen 	case QUARK_X1000_SSP:
226e5262d05SWeike Chen 		mask = QUARK_X1000_SSSR_TFL_MASK;
227e5262d05SWeike Chen 		break;
2287c7289a4SAndy Shevchenko 	case CE4100_SSP:
2297c7289a4SAndy Shevchenko 		mask = CE4100_SSSR_TFL_MASK;
2307c7289a4SAndy Shevchenko 		break;
2314fdb2424SWeike Chen 	default:
2324fdb2424SWeike Chen 		mask = SSSR_TFL_MASK;
2334fdb2424SWeike Chen 		break;
2344fdb2424SWeike Chen 	}
2354fdb2424SWeike Chen 
236c039dd27SJarkko Nikula 	return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
2374fdb2424SWeike Chen }
2384fdb2424SWeike Chen 
2394fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
2404fdb2424SWeike Chen 				     u32 *sccr1_reg)
2414fdb2424SWeike Chen {
2424fdb2424SWeike Chen 	u32 mask;
2434fdb2424SWeike Chen 
2444fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
245e5262d05SWeike Chen 	case QUARK_X1000_SSP:
246e5262d05SWeike Chen 		mask = QUARK_X1000_SSCR1_RFT;
247e5262d05SWeike Chen 		break;
2487c7289a4SAndy Shevchenko 	case CE4100_SSP:
2497c7289a4SAndy Shevchenko 		mask = CE4100_SSCR1_RFT;
2507c7289a4SAndy Shevchenko 		break;
2514fdb2424SWeike Chen 	default:
2524fdb2424SWeike Chen 		mask = SSCR1_RFT;
2534fdb2424SWeike Chen 		break;
2544fdb2424SWeike Chen 	}
2554fdb2424SWeike Chen 	*sccr1_reg &= ~mask;
2564fdb2424SWeike Chen }
2574fdb2424SWeike Chen 
2584fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
2594fdb2424SWeike Chen 				   u32 *sccr1_reg, u32 threshold)
2604fdb2424SWeike Chen {
2614fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
262e5262d05SWeike Chen 	case QUARK_X1000_SSP:
263e5262d05SWeike Chen 		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
264e5262d05SWeike Chen 		break;
2657c7289a4SAndy Shevchenko 	case CE4100_SSP:
2667c7289a4SAndy Shevchenko 		*sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
2677c7289a4SAndy Shevchenko 		break;
2684fdb2424SWeike Chen 	default:
2694fdb2424SWeike Chen 		*sccr1_reg |= SSCR1_RxTresh(threshold);
2704fdb2424SWeike Chen 		break;
2714fdb2424SWeike Chen 	}
2724fdb2424SWeike Chen }
2734fdb2424SWeike Chen 
2744fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
2754fdb2424SWeike Chen 				  u32 clk_div, u8 bits)
2764fdb2424SWeike Chen {
2774fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
278e5262d05SWeike Chen 	case QUARK_X1000_SSP:
279e5262d05SWeike Chen 		return clk_div
280e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_Motorola
281e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
282e5262d05SWeike Chen 			| SSCR0_SSE;
2834fdb2424SWeike Chen 	default:
2844fdb2424SWeike Chen 		return clk_div
2854fdb2424SWeike Chen 			| SSCR0_Motorola
2864fdb2424SWeike Chen 			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
2874fdb2424SWeike Chen 			| SSCR0_SSE
2884fdb2424SWeike Chen 			| (bits > 16 ? SSCR0_EDSS : 0);
2894fdb2424SWeike Chen 	}
2904fdb2424SWeike Chen }
2914fdb2424SWeike Chen 
292a0d2642eSMika Westerberg /*
293a0d2642eSMika Westerberg  * Read and write LPSS SSP private registers. Caller must first check that
294a0d2642eSMika Westerberg  * is_lpss_ssp() returns true before these can be called.
295a0d2642eSMika Westerberg  */
296a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
297a0d2642eSMika Westerberg {
298a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
299a0d2642eSMika Westerberg 	return readl(drv_data->lpss_base + offset);
300a0d2642eSMika Westerberg }
301a0d2642eSMika Westerberg 
302a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data,
303a0d2642eSMika Westerberg 				  unsigned offset, u32 value)
304a0d2642eSMika Westerberg {
305a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
306a0d2642eSMika Westerberg 	writel(value, drv_data->lpss_base + offset);
307a0d2642eSMika Westerberg }
308a0d2642eSMika Westerberg 
309a0d2642eSMika Westerberg /*
310a0d2642eSMika Westerberg  * lpss_ssp_setup - perform LPSS SSP specific setup
311a0d2642eSMika Westerberg  * @drv_data: pointer to the driver private data
312a0d2642eSMika Westerberg  *
313a0d2642eSMika Westerberg  * Perform LPSS SSP specific setup. This function must be called first if
314a0d2642eSMika Westerberg  * one is going to use LPSS SSP private registers.
315a0d2642eSMika Westerberg  */
316a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data)
317a0d2642eSMika Westerberg {
318dccf7369SJarkko Nikula 	const struct lpss_config *config;
319dccf7369SJarkko Nikula 	u32 value;
320a0d2642eSMika Westerberg 
321dccf7369SJarkko Nikula 	config = lpss_get_config(drv_data);
322dccf7369SJarkko Nikula 	drv_data->lpss_base = drv_data->ioaddr + config->offset;
323a0d2642eSMika Westerberg 
324a0d2642eSMika Westerberg 	/* Enable software chip select control */
3250e897218SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
326624ea72eSJarkko Nikula 	value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
327624ea72eSJarkko Nikula 	value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
328dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
3290054e28dSMika Westerberg 
3300054e28dSMika Westerberg 	/* Enable multiblock DMA transfers */
33151eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma) {
332dccf7369SJarkko Nikula 		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
3331de70612SMika Westerberg 
33482ba2c2aSJarkko Nikula 		if (config->reg_general >= 0) {
33582ba2c2aSJarkko Nikula 			value = __lpss_ssp_read_priv(drv_data,
33682ba2c2aSJarkko Nikula 						     config->reg_general);
337624ea72eSJarkko Nikula 			value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
33882ba2c2aSJarkko Nikula 			__lpss_ssp_write_priv(drv_data,
33982ba2c2aSJarkko Nikula 					      config->reg_general, value);
34082ba2c2aSJarkko Nikula 		}
3411de70612SMika Westerberg 	}
342a0d2642eSMika Westerberg }
343a0d2642eSMika Westerberg 
344d5898e19SJarkko Nikula static void lpss_ssp_select_cs(struct spi_device *spi,
345c1e4a53cSMika Westerberg 			       const struct lpss_config *config)
346a0d2642eSMika Westerberg {
347d5898e19SJarkko Nikula 	struct driver_data *drv_data =
348d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
349d0283eb2SJarkko Nikula 	u32 value, cs;
350a0d2642eSMika Westerberg 
351c1e4a53cSMika Westerberg 	if (!config->cs_sel_mask)
352c1e4a53cSMika Westerberg 		return;
353dccf7369SJarkko Nikula 
354dccf7369SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
355c1e4a53cSMika Westerberg 
356d5898e19SJarkko Nikula 	cs = spi->chip_select;
357c1e4a53cSMika Westerberg 	cs <<= config->cs_sel_shift;
358c1e4a53cSMika Westerberg 	if (cs != (value & config->cs_sel_mask)) {
359d0283eb2SJarkko Nikula 		/*
360c1e4a53cSMika Westerberg 		 * When switching another chip select output active the
361c1e4a53cSMika Westerberg 		 * output must be selected first and wait 2 ssp_clk cycles
362c1e4a53cSMika Westerberg 		 * before changing state to active. Otherwise a short
363c1e4a53cSMika Westerberg 		 * glitch will occur on the previous chip select since
364c1e4a53cSMika Westerberg 		 * output select is latched but state control is not.
365d0283eb2SJarkko Nikula 		 */
366c1e4a53cSMika Westerberg 		value &= ~config->cs_sel_mask;
367d0283eb2SJarkko Nikula 		value |= cs;
368d0283eb2SJarkko Nikula 		__lpss_ssp_write_priv(drv_data,
369d0283eb2SJarkko Nikula 				      config->reg_cs_ctrl, value);
370d0283eb2SJarkko Nikula 		ndelay(1000000000 /
37151eea52dSLubomir Rintel 		       (drv_data->controller->max_speed_hz / 2));
372d0283eb2SJarkko Nikula 	}
373d0283eb2SJarkko Nikula }
374c1e4a53cSMika Westerberg 
375d5898e19SJarkko Nikula static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
376c1e4a53cSMika Westerberg {
377d5898e19SJarkko Nikula 	struct driver_data *drv_data =
378d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
379c1e4a53cSMika Westerberg 	const struct lpss_config *config;
380c1e4a53cSMika Westerberg 	u32 value;
381c1e4a53cSMika Westerberg 
382c1e4a53cSMika Westerberg 	config = lpss_get_config(drv_data);
383c1e4a53cSMika Westerberg 
384c1e4a53cSMika Westerberg 	if (enable)
385d5898e19SJarkko Nikula 		lpss_ssp_select_cs(spi, config);
386c1e4a53cSMika Westerberg 
387c1e4a53cSMika Westerberg 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
388c1e4a53cSMika Westerberg 	if (enable)
389c1e4a53cSMika Westerberg 		value &= ~LPSS_CS_CONTROL_CS_HIGH;
390c1e4a53cSMika Westerberg 	else
391c1e4a53cSMika Westerberg 		value |= LPSS_CS_CONTROL_CS_HIGH;
392dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
393*683f65deSEvan Green 	if (config->cs_clk_stays_gated) {
394*683f65deSEvan Green 		u32 clkgate;
395*683f65deSEvan Green 
396*683f65deSEvan Green 		/*
397*683f65deSEvan Green 		 * Changing CS alone when dynamic clock gating is on won't
398*683f65deSEvan Green 		 * actually flip CS at that time. This ruins SPI transfers
399*683f65deSEvan Green 		 * that specify delays, or have no data. Toggle the clock mode
400*683f65deSEvan Green 		 * to force on briefly to poke the CS pin to move.
401*683f65deSEvan Green 		 */
402*683f65deSEvan Green 		clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
403*683f65deSEvan Green 		value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
404*683f65deSEvan Green 			LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
405*683f65deSEvan Green 
406*683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
407*683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
408*683f65deSEvan Green 	}
409a0d2642eSMika Westerberg }
410a0d2642eSMika Westerberg 
411d5898e19SJarkko Nikula static void cs_assert(struct spi_device *spi)
412ca632f55SGrant Likely {
413d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
414d5898e19SJarkko Nikula 	struct driver_data *drv_data =
415d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
416ca632f55SGrant Likely 
417ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP) {
41896579a4eSJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, chip->frm);
419ca632f55SGrant Likely 		return;
420ca632f55SGrant Likely 	}
421ca632f55SGrant Likely 
422ca632f55SGrant Likely 	if (chip->cs_control) {
423ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_ASSERT);
424ca632f55SGrant Likely 		return;
425ca632f55SGrant Likely 	}
426ca632f55SGrant Likely 
427c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
428c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
429a0d2642eSMika Westerberg 		return;
430a0d2642eSMika Westerberg 	}
431a0d2642eSMika Westerberg 
4327566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
433d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, true);
434ca632f55SGrant Likely }
435ca632f55SGrant Likely 
436d5898e19SJarkko Nikula static void cs_deassert(struct spi_device *spi)
437ca632f55SGrant Likely {
438d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
439d5898e19SJarkko Nikula 	struct driver_data *drv_data =
440d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
441104e51afSJarkko Nikula 	unsigned long timeout;
442ca632f55SGrant Likely 
443ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
444ca632f55SGrant Likely 		return;
445ca632f55SGrant Likely 
446104e51afSJarkko Nikula 	/* Wait until SSP becomes idle before deasserting the CS */
447104e51afSJarkko Nikula 	timeout = jiffies + msecs_to_jiffies(10);
448104e51afSJarkko Nikula 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
449104e51afSJarkko Nikula 	       !time_after(jiffies, timeout))
450104e51afSJarkko Nikula 		cpu_relax();
451104e51afSJarkko Nikula 
452ca632f55SGrant Likely 	if (chip->cs_control) {
453ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_DEASSERT);
454ca632f55SGrant Likely 		return;
455ca632f55SGrant Likely 	}
456ca632f55SGrant Likely 
457c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
458c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
459a0d2642eSMika Westerberg 		return;
460a0d2642eSMika Westerberg 	}
461a0d2642eSMika Westerberg 
4627566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
463d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, false);
464d5898e19SJarkko Nikula }
465d5898e19SJarkko Nikula 
466d5898e19SJarkko Nikula static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
467d5898e19SJarkko Nikula {
468d5898e19SJarkko Nikula 	if (level)
469d5898e19SJarkko Nikula 		cs_deassert(spi);
470d5898e19SJarkko Nikula 	else
471d5898e19SJarkko Nikula 		cs_assert(spi);
472ca632f55SGrant Likely }
473ca632f55SGrant Likely 
474cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
475ca632f55SGrant Likely {
476ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
477ca632f55SGrant Likely 
478ca632f55SGrant Likely 	do {
479c039dd27SJarkko Nikula 		while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
480c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
481c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
482ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
483ca632f55SGrant Likely 
484ca632f55SGrant Likely 	return limit;
485ca632f55SGrant Likely }
486ca632f55SGrant Likely 
487ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
488ca632f55SGrant Likely {
489ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
490ca632f55SGrant Likely 
4914fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
492ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
493ca632f55SGrant Likely 		return 0;
494ca632f55SGrant Likely 
495c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
496ca632f55SGrant Likely 	drv_data->tx += n_bytes;
497ca632f55SGrant Likely 
498ca632f55SGrant Likely 	return 1;
499ca632f55SGrant Likely }
500ca632f55SGrant Likely 
501ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
502ca632f55SGrant Likely {
503ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
504ca632f55SGrant Likely 
505c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
506ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
507c039dd27SJarkko Nikula 		pxa2xx_spi_read(drv_data, SSDR);
508ca632f55SGrant Likely 		drv_data->rx += n_bytes;
509ca632f55SGrant Likely 	}
510ca632f55SGrant Likely 
511ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
512ca632f55SGrant Likely }
513ca632f55SGrant Likely 
514ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data)
515ca632f55SGrant Likely {
5164fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
517ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
518ca632f55SGrant Likely 		return 0;
519ca632f55SGrant Likely 
520c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
521ca632f55SGrant Likely 	++drv_data->tx;
522ca632f55SGrant Likely 
523ca632f55SGrant Likely 	return 1;
524ca632f55SGrant Likely }
525ca632f55SGrant Likely 
526ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data)
527ca632f55SGrant Likely {
528c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
529ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
530c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
531ca632f55SGrant Likely 		++drv_data->rx;
532ca632f55SGrant Likely 	}
533ca632f55SGrant Likely 
534ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
535ca632f55SGrant Likely }
536ca632f55SGrant Likely 
537ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
538ca632f55SGrant Likely {
5394fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
540ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
541ca632f55SGrant Likely 		return 0;
542ca632f55SGrant Likely 
543c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
544ca632f55SGrant Likely 	drv_data->tx += 2;
545ca632f55SGrant Likely 
546ca632f55SGrant Likely 	return 1;
547ca632f55SGrant Likely }
548ca632f55SGrant Likely 
549ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
550ca632f55SGrant Likely {
551c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
552ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
553c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
554ca632f55SGrant Likely 		drv_data->rx += 2;
555ca632f55SGrant Likely 	}
556ca632f55SGrant Likely 
557ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
558ca632f55SGrant Likely }
559ca632f55SGrant Likely 
560ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
561ca632f55SGrant Likely {
5624fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
563ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
564ca632f55SGrant Likely 		return 0;
565ca632f55SGrant Likely 
566c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
567ca632f55SGrant Likely 	drv_data->tx += 4;
568ca632f55SGrant Likely 
569ca632f55SGrant Likely 	return 1;
570ca632f55SGrant Likely }
571ca632f55SGrant Likely 
572ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
573ca632f55SGrant Likely {
574c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
575ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
576c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
577ca632f55SGrant Likely 		drv_data->rx += 4;
578ca632f55SGrant Likely 	}
579ca632f55SGrant Likely 
580ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
581ca632f55SGrant Likely }
582ca632f55SGrant Likely 
583ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
584ca632f55SGrant Likely {
58596579a4eSJarkko Nikula 	struct chip_data *chip =
58651eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
587ca632f55SGrant Likely 	u32 sccr1_reg;
588ca632f55SGrant Likely 
589c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
590152bc19eSAndy Shevchenko 	switch (drv_data->ssp_type) {
591152bc19eSAndy Shevchenko 	case QUARK_X1000_SSP:
592152bc19eSAndy Shevchenko 		sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
593152bc19eSAndy Shevchenko 		break;
5947c7289a4SAndy Shevchenko 	case CE4100_SSP:
5957c7289a4SAndy Shevchenko 		sccr1_reg &= ~CE4100_SSCR1_RFT;
5967c7289a4SAndy Shevchenko 		break;
597152bc19eSAndy Shevchenko 	default:
598ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_RFT;
599152bc19eSAndy Shevchenko 		break;
600152bc19eSAndy Shevchenko 	}
601ca632f55SGrant Likely 	sccr1_reg |= chip->threshold;
602c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
603ca632f55SGrant Likely }
604ca632f55SGrant Likely 
605ca632f55SGrant Likely static void int_error_stop(struct driver_data *drv_data, const char* msg)
606ca632f55SGrant Likely {
607ca632f55SGrant Likely 	/* Stop and reset SSP */
608ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
609ca632f55SGrant Likely 	reset_sccr1(drv_data);
610ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
611c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
612cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
613c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0,
614c039dd27SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
615ca632f55SGrant Likely 
616ca632f55SGrant Likely 	dev_err(&drv_data->pdev->dev, "%s\n", msg);
617ca632f55SGrant Likely 
61851eea52dSLubomir Rintel 	drv_data->controller->cur_msg->status = -EIO;
61951eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
620ca632f55SGrant Likely }
621ca632f55SGrant Likely 
622ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
623ca632f55SGrant Likely {
62407550df0SJarkko Nikula 	/* Clear and disable interrupts */
625ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
626ca632f55SGrant Likely 	reset_sccr1(drv_data);
627ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
628c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
629ca632f55SGrant Likely 
63051eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
631ca632f55SGrant Likely }
632ca632f55SGrant Likely 
633ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
634ca632f55SGrant Likely {
635c039dd27SJarkko Nikula 	u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
636ca632f55SGrant Likely 		       drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
637ca632f55SGrant Likely 
638c039dd27SJarkko Nikula 	u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
639ca632f55SGrant Likely 
640ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
641ca632f55SGrant Likely 		int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
642ca632f55SGrant Likely 		return IRQ_HANDLED;
643ca632f55SGrant Likely 	}
644ca632f55SGrant Likely 
645ec93cb6fSLubomir Rintel 	if (irq_status & SSSR_TUR) {
646ec93cb6fSLubomir Rintel 		int_error_stop(drv_data, "interrupt_transfer: fifo underrun");
647ec93cb6fSLubomir Rintel 		return IRQ_HANDLED;
648ec93cb6fSLubomir Rintel 	}
649ec93cb6fSLubomir Rintel 
650ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
651c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
652ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
653ca632f55SGrant Likely 			int_transfer_complete(drv_data);
654ca632f55SGrant Likely 			return IRQ_HANDLED;
655ca632f55SGrant Likely 		}
656ca632f55SGrant Likely 	}
657ca632f55SGrant Likely 
658ca632f55SGrant Likely 	/* Drain rx fifo, Fill tx fifo and prevent overruns */
659ca632f55SGrant Likely 	do {
660ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
661ca632f55SGrant Likely 			int_transfer_complete(drv_data);
662ca632f55SGrant Likely 			return IRQ_HANDLED;
663ca632f55SGrant Likely 		}
664ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
665ca632f55SGrant Likely 
666ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
667ca632f55SGrant Likely 		int_transfer_complete(drv_data);
668ca632f55SGrant Likely 		return IRQ_HANDLED;
669ca632f55SGrant Likely 	}
670ca632f55SGrant Likely 
671ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
672ca632f55SGrant Likely 		u32 bytes_left;
673ca632f55SGrant Likely 		u32 sccr1_reg;
674ca632f55SGrant Likely 
675c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
676ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
677ca632f55SGrant Likely 
678ca632f55SGrant Likely 		/*
679ca632f55SGrant Likely 		 * PXA25x_SSP has no timeout, set up rx threshould for the
680ca632f55SGrant Likely 		 * remaining RX bytes.
681ca632f55SGrant Likely 		 */
682ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
6834fdb2424SWeike Chen 			u32 rx_thre;
684ca632f55SGrant Likely 
6854fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
686ca632f55SGrant Likely 
687ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
688ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
689ca632f55SGrant Likely 			case 4:
6902c183376SGustavo A. R. Silva 				bytes_left >>= 2;
6912c183376SGustavo A. R. Silva 				break;
692ca632f55SGrant Likely 			case 2:
693ca632f55SGrant Likely 				bytes_left >>= 1;
6942c183376SGustavo A. R. Silva 				break;
695ca632f55SGrant Likely 			}
696ca632f55SGrant Likely 
6974fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
6984fdb2424SWeike Chen 			if (rx_thre > bytes_left)
6994fdb2424SWeike Chen 				rx_thre = bytes_left;
700ca632f55SGrant Likely 
7014fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
702ca632f55SGrant Likely 		}
703c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
704ca632f55SGrant Likely 	}
705ca632f55SGrant Likely 
706ca632f55SGrant Likely 	/* We did something */
707ca632f55SGrant Likely 	return IRQ_HANDLED;
708ca632f55SGrant Likely }
709ca632f55SGrant Likely 
710b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data)
711b0312482SJan Kiszka {
712b0312482SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR0,
713b0312482SJan Kiszka 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
714b0312482SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1,
715b0312482SJan Kiszka 			 pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
716b0312482SJan Kiszka 	if (!pxa25x_ssp_comp(drv_data))
717b0312482SJan Kiszka 		pxa2xx_spi_write(drv_data, SSTO, 0);
718b0312482SJan Kiszka 	write_SSSR_CS(drv_data, drv_data->clear_sr);
719b0312482SJan Kiszka 
720b0312482SJan Kiszka 	dev_err(&drv_data->pdev->dev,
721b0312482SJan Kiszka 		"bad message state in interrupt handler\n");
722b0312482SJan Kiszka }
723b0312482SJan Kiszka 
724ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
725ca632f55SGrant Likely {
726ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
7277d94a505SMika Westerberg 	u32 sccr1_reg;
728ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
729ca632f55SGrant Likely 	u32 status;
730ca632f55SGrant Likely 
7317d94a505SMika Westerberg 	/*
7327d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
7337d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
7347d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
7357d94a505SMika Westerberg 	 * interrupt is enabled).
7367d94a505SMika Westerberg 	 */
7377d94a505SMika Westerberg 	if (pm_runtime_suspended(&drv_data->pdev->dev))
7387d94a505SMika Westerberg 		return IRQ_NONE;
7397d94a505SMika Westerberg 
740269e4a41SMika Westerberg 	/*
741269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
742269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
743269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
744269e4a41SMika Westerberg 	 * powered off.
745269e4a41SMika Westerberg 	 */
746c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
747269e4a41SMika Westerberg 	if (status == ~0)
748269e4a41SMika Westerberg 		return IRQ_NONE;
749269e4a41SMika Westerberg 
750c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
751ca632f55SGrant Likely 
752ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
753ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
754ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
755ca632f55SGrant Likely 
75602bc933eSTan, Jui Nee 	/* Ignore RX timeout interrupt if it is disabled */
75702bc933eSTan, Jui Nee 	if (!(sccr1_reg & SSCR1_TINTE))
75802bc933eSTan, Jui Nee 		mask &= ~SSSR_TINT;
75902bc933eSTan, Jui Nee 
760ca632f55SGrant Likely 	if (!(status & mask))
761ca632f55SGrant Likely 		return IRQ_NONE;
762ca632f55SGrant Likely 
763e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
764e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
765e51e9b93SJan Kiszka 
76651eea52dSLubomir Rintel 	if (!drv_data->controller->cur_msg) {
767b0312482SJan Kiszka 		handle_bad_msg(drv_data);
768ca632f55SGrant Likely 		/* Never fail */
769ca632f55SGrant Likely 		return IRQ_HANDLED;
770ca632f55SGrant Likely 	}
771ca632f55SGrant Likely 
772ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
773ca632f55SGrant Likely }
774ca632f55SGrant Likely 
775e5262d05SWeike Chen /*
7769df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
7779df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
7789df461ecSAndy Shevchenko  *
7799df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
7809df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
7819df461ecSAndy Shevchenko  *
7829df461ecSAndy Shevchenko  * Fsys = 200MHz
7839df461ecSAndy Shevchenko  *
7849df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
7859df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
7869df461ecSAndy Shevchenko  *
7879df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
7889df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
7899df461ecSAndy Shevchenko  *
7909df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
7919df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
7929df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
7939df461ecSAndy Shevchenko  *       k = [1, 256]
7949df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
7959df461ecSAndy Shevchenko  *
7969df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
7979df461ecSAndy Shevchenko  * are:
7989df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
7999df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
8009df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
8019df461ecSAndy Shevchenko  *
8029df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
8039df461ecSAndy Shevchenko  *
8049df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
8059df461ecSAndy Shevchenko  * to the asked baud rate.
806e5262d05SWeike Chen  */
8079df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
808e5262d05SWeike Chen {
8099df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
8109df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
8119df461ecSAndy Shevchenko 						   see (2) */
8129df461ecSAndy Shevchenko 						/* case 3 */
8139df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
8149df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
8159df461ecSAndy Shevchenko 	unsigned long scale;
8169df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
8179df461ecSAndy Shevchenko 	long r, r1, r2;
8189df461ecSAndy Shevchenko 	u32 mul;
819e5262d05SWeike Chen 
8209df461ecSAndy Shevchenko 	/* Case 1 */
8219df461ecSAndy Shevchenko 
8229df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
8239df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
8249df461ecSAndy Shevchenko 
8259df461ecSAndy Shevchenko 	/* Calculate initial quot */
8263ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
8279df461ecSAndy Shevchenko 
8289df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
8299df461ecSAndy Shevchenko 	if (q1 > 256) {
8309df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
8319df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
8329df461ecSAndy Shevchenko 		if (scale > 9) {
8339df461ecSAndy Shevchenko 			q1 >>= scale - 9;
8349df461ecSAndy Shevchenko 			mul >>= scale - 9;
8359df461ecSAndy Shevchenko 		}
8369df461ecSAndy Shevchenko 
8379df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
8389df461ecSAndy Shevchenko 		q1 += q1 & 1;
8399df461ecSAndy Shevchenko 	}
8409df461ecSAndy Shevchenko 
8419df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
8429df461ecSAndy Shevchenko 	scale = __ffs(q1);
8439df461ecSAndy Shevchenko 	q1 >>= scale;
8449df461ecSAndy Shevchenko 	mul >>= scale;
8459df461ecSAndy Shevchenko 
8469df461ecSAndy Shevchenko 	/* Get the remainder */
8479df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
8489df461ecSAndy Shevchenko 
8499df461ecSAndy Shevchenko 	/* Case 2 */
8509df461ecSAndy Shevchenko 
8513ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
8529df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
8539df461ecSAndy Shevchenko 
8549df461ecSAndy Shevchenko 	/*
8559df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
8569df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
8579df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
8589df461ecSAndy Shevchenko 	 */
8599df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
8609df461ecSAndy Shevchenko 		/* case 1 is better */
8619df461ecSAndy Shevchenko 		r = r1;
8629df461ecSAndy Shevchenko 		q = q1;
8639df461ecSAndy Shevchenko 	} else {
8649df461ecSAndy Shevchenko 		/* case 2 is better */
8659df461ecSAndy Shevchenko 		r = r2;
8669df461ecSAndy Shevchenko 		q = q2;
8679df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
8689df461ecSAndy Shevchenko 	}
8699df461ecSAndy Shevchenko 
8703ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
8719df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
8729df461ecSAndy Shevchenko 		u64 fssp;
8739df461ecSAndy Shevchenko 		u32 m;
8749df461ecSAndy Shevchenko 
8759df461ecSAndy Shevchenko 		/* Calculate initial quot */
8763ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
8779df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
8789df461ecSAndy Shevchenko 
8799df461ecSAndy Shevchenko 		/* Get the remainder */
8809df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
8819df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
8829df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
8839df461ecSAndy Shevchenko 
8849df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
8859df461ecSAndy Shevchenko 		if (r1 < r) {
8869df461ecSAndy Shevchenko 			/* case 3 is better */
8879df461ecSAndy Shevchenko 			q = 1;
8889df461ecSAndy Shevchenko 			mul = m;
889e5262d05SWeike Chen 		}
890e5262d05SWeike Chen 	}
891e5262d05SWeike Chen 
8929df461ecSAndy Shevchenko 	*dds = mul;
8939df461ecSAndy Shevchenko 	return q - 1;
894e5262d05SWeike Chen }
895e5262d05SWeike Chen 
8963343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
897ca632f55SGrant Likely {
89851eea52dSLubomir Rintel 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
8993343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
9003343b7a6SMika Westerberg 
9013343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
902ca632f55SGrant Likely 
90329f21337SFlavio Suligoi 	/*
90429f21337SFlavio Suligoi 	 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
90529f21337SFlavio Suligoi 	 * that the SSP transmission rate can be greater than the device rate
90629f21337SFlavio Suligoi 	 */
907ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
90829f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
909ca632f55SGrant Likely 	else
91029f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
911ca632f55SGrant Likely }
912ca632f55SGrant Likely 
913e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
914d2c2f6a4SAndy Shevchenko 					   int rate)
915e5262d05SWeike Chen {
91696579a4eSJarkko Nikula 	struct chip_data *chip =
91751eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
918025ffe88SAndy Shevchenko 	unsigned int clk_div;
919e5262d05SWeike Chen 
920e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
921e5262d05SWeike Chen 	case QUARK_X1000_SSP:
9229df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
923eecacf73SDan Carpenter 		break;
924e5262d05SWeike Chen 	default:
925025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
926eecacf73SDan Carpenter 		break;
927e5262d05SWeike Chen 	}
928025ffe88SAndy Shevchenko 	return clk_div << 8;
929e5262d05SWeike Chen }
930e5262d05SWeike Chen 
93151eea52dSLubomir Rintel static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
932b6ced294SJarkko Nikula 			       struct spi_device *spi,
933b6ced294SJarkko Nikula 			       struct spi_transfer *xfer)
934b6ced294SJarkko Nikula {
935b6ced294SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
936b6ced294SJarkko Nikula 
937b6ced294SJarkko Nikula 	return chip->enable_dma &&
938b6ced294SJarkko Nikula 	       xfer->len <= MAX_DMA_LEN &&
939b6ced294SJarkko Nikula 	       xfer->len >= chip->dma_burst_size;
940b6ced294SJarkko Nikula }
941b6ced294SJarkko Nikula 
94251eea52dSLubomir Rintel static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
943d5898e19SJarkko Nikula 				   struct spi_device *spi,
944d5898e19SJarkko Nikula 				   struct spi_transfer *transfer)
945ca632f55SGrant Likely {
94651eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
94751eea52dSLubomir Rintel 	struct spi_message *message = controller->cur_msg;
94820f4c379SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
94996579a4eSJarkko Nikula 	u32 dma_thresh = chip->dma_threshold;
95096579a4eSJarkko Nikula 	u32 dma_burst = chip->dma_burst_size;
95196579a4eSJarkko Nikula 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
952bffc967eSJarkko Nikula 	u32 clk_div;
953bffc967eSJarkko Nikula 	u8 bits;
954bffc967eSJarkko Nikula 	u32 speed;
955ca632f55SGrant Likely 	u32 cr0;
956ca632f55SGrant Likely 	u32 cr1;
9577d1f1bf6SAndy Shevchenko 	int err;
958b6ced294SJarkko Nikula 	int dma_mapped;
959ca632f55SGrant Likely 
960cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
961b6ced294SJarkko Nikula 	if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
962ca632f55SGrant Likely 
963ca632f55SGrant Likely 		/* reject already-mapped transfers; PIO won't always work */
964ca632f55SGrant Likely 		if (message->is_dma_mapped
965ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
966748fbadfSJarkko Nikula 			dev_err(&spi->dev,
9678ae55af3SJarkko Nikula 				"Mapped transfer length of %u is greater than %d\n",
968ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
969d5898e19SJarkko Nikula 			return -EINVAL;
970ca632f55SGrant Likely 		}
971ca632f55SGrant Likely 
972ca632f55SGrant Likely 		/* warn ... we force this to PIO mode */
97320f4c379SJarkko Nikula 		dev_warn_ratelimited(&spi->dev,
9748ae55af3SJarkko Nikula 				     "DMA disabled for transfer length %ld greater than %d\n",
975d5898e19SJarkko Nikula 				     (long)transfer->len, MAX_DMA_LEN);
976ca632f55SGrant Likely 	}
977ca632f55SGrant Likely 
978ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
979cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
980748fbadfSJarkko Nikula 		dev_err(&spi->dev, "Flush failed\n");
981d5898e19SJarkko Nikula 		return -EIO;
982ca632f55SGrant Likely 	}
983ca632f55SGrant Likely 	drv_data->n_bytes = chip->n_bytes;
984ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
985ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
986ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
987ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
988ca632f55SGrant Likely 	drv_data->write = drv_data->tx ? chip->write : null_writer;
989ca632f55SGrant Likely 	drv_data->read = drv_data->rx ? chip->read : null_reader;
990ca632f55SGrant Likely 
991ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
992ca632f55SGrant Likely 	bits = transfer->bits_per_word;
993ca632f55SGrant Likely 	speed = transfer->speed_hz;
994ca632f55SGrant Likely 
995d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
996ca632f55SGrant Likely 
997ca632f55SGrant Likely 	if (bits <= 8) {
998ca632f55SGrant Likely 		drv_data->n_bytes = 1;
999ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1000ca632f55SGrant Likely 					u8_reader : null_reader;
1001ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1002ca632f55SGrant Likely 					u8_writer : null_writer;
1003ca632f55SGrant Likely 	} else if (bits <= 16) {
1004ca632f55SGrant Likely 		drv_data->n_bytes = 2;
1005ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1006ca632f55SGrant Likely 					u16_reader : null_reader;
1007ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1008ca632f55SGrant Likely 					u16_writer : null_writer;
1009ca632f55SGrant Likely 	} else if (bits <= 32) {
1010ca632f55SGrant Likely 		drv_data->n_bytes = 4;
1011ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1012ca632f55SGrant Likely 					u32_reader : null_reader;
1013ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1014ca632f55SGrant Likely 					u32_writer : null_writer;
1015ca632f55SGrant Likely 	}
1016196b0e2cSJarkko Nikula 	/*
1017196b0e2cSJarkko Nikula 	 * if bits/word is changed in dma mode, then must check the
1018196b0e2cSJarkko Nikula 	 * thresholds and burst also
1019196b0e2cSJarkko Nikula 	 */
1020ca632f55SGrant Likely 	if (chip->enable_dma) {
1021cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
102220f4c379SJarkko Nikula 						spi,
1023ca632f55SGrant Likely 						bits, &dma_burst,
1024ca632f55SGrant Likely 						&dma_thresh))
102520f4c379SJarkko Nikula 			dev_warn_ratelimited(&spi->dev,
10268ae55af3SJarkko Nikula 					     "DMA burst size reduced to match bits_per_word\n");
1027ca632f55SGrant Likely 	}
1028ca632f55SGrant Likely 
102951eea52dSLubomir Rintel 	dma_mapped = controller->can_dma &&
103020f4c379SJarkko Nikula 		     controller->can_dma(controller, spi, transfer) &&
103151eea52dSLubomir Rintel 		     controller->cur_msg_mapped;
1032b6ced294SJarkko Nikula 	if (dma_mapped) {
1033ca632f55SGrant Likely 
1034ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
1035cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1036ca632f55SGrant Likely 
1037d5898e19SJarkko Nikula 		err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1038d5898e19SJarkko Nikula 		if (err)
1039d5898e19SJarkko Nikula 			return err;
1040ca632f55SGrant Likely 
1041ca632f55SGrant Likely 		/* Clear status and start DMA engine */
1042ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1043c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1044cd7bed00SMika Westerberg 
1045cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
1046ca632f55SGrant Likely 	} else {
1047ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
1048ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
1049ca632f55SGrant Likely 
1050ca632f55SGrant Likely 		/* Clear status  */
1051ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1052ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1053ca632f55SGrant Likely 	}
1054ca632f55SGrant Likely 
1055ee03672dSJarkko Nikula 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1056ee03672dSJarkko Nikula 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1057ee03672dSJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
105820f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
105951eea52dSLubomir Rintel 			controller->max_speed_hz
1060ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1061b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1062ee03672dSJarkko Nikula 	else
106320f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
106451eea52dSLubomir Rintel 			controller->max_speed_hz / 2
1065ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1066b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1067ee03672dSJarkko Nikula 
1068a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
1069c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
1070c039dd27SJarkko Nikula 		    != chip->lpss_rx_threshold)
1071c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSIRF,
1072c039dd27SJarkko Nikula 					 chip->lpss_rx_threshold);
1073c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
1074c039dd27SJarkko Nikula 		    != chip->lpss_tx_threshold)
1075c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSITF,
1076c039dd27SJarkko Nikula 					 chip->lpss_tx_threshold);
1077a0d2642eSMika Westerberg 	}
1078a0d2642eSMika Westerberg 
1079e5262d05SWeike Chen 	if (is_quark_x1000_ssp(drv_data) &&
1080c039dd27SJarkko Nikula 	    (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
1081c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
1082e5262d05SWeike Chen 
1083ca632f55SGrant Likely 	/* see if we need to reload the config registers */
1084c039dd27SJarkko Nikula 	if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1085c039dd27SJarkko Nikula 	    || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1086c039dd27SJarkko Nikula 	    != (cr1 & change_mask)) {
1087ca632f55SGrant Likely 		/* stop the SSP, and update the other bits */
1088c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
1089ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1090c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1091ca632f55SGrant Likely 		/* first set CR1 without interrupt and service enables */
1092c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
1093ca632f55SGrant Likely 		/* restart the SSP */
1094c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0);
1095ca632f55SGrant Likely 
1096ca632f55SGrant Likely 	} else {
1097ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1098c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1099ca632f55SGrant Likely 	}
1100ca632f55SGrant Likely 
110182391856SLubomir Rintel 	if (drv_data->ssp_type == MMP2_SSP) {
110282391856SLubomir Rintel 		u8 tx_level = (pxa2xx_spi_read(drv_data, SSSR)
110382391856SLubomir Rintel 					& SSSR_TFL_MASK) >> 8;
110482391856SLubomir Rintel 
110582391856SLubomir Rintel 		if (tx_level) {
110682391856SLubomir Rintel 			/* On MMP2, flipping SSE doesn't to empty TXFIFO. */
110782391856SLubomir Rintel 			dev_warn(&spi->dev, "%d bytes of garbage in TXFIFO!\n",
110882391856SLubomir Rintel 								tx_level);
110982391856SLubomir Rintel 			if (tx_level > transfer->len)
111082391856SLubomir Rintel 				tx_level = transfer->len;
111182391856SLubomir Rintel 			drv_data->tx += tx_level;
111282391856SLubomir Rintel 		}
111382391856SLubomir Rintel 	}
111482391856SLubomir Rintel 
111551eea52dSLubomir Rintel 	if (spi_controller_is_slave(controller)) {
1116ec93cb6fSLubomir Rintel 		while (drv_data->write(drv_data))
1117ec93cb6fSLubomir Rintel 			;
111877d33897SLubomir Rintel 		if (drv_data->gpiod_ready) {
111977d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 1);
112077d33897SLubomir Rintel 			udelay(1);
112177d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 0);
112277d33897SLubomir Rintel 		}
1123ec93cb6fSLubomir Rintel 	}
1124ec93cb6fSLubomir Rintel 
1125d5898e19SJarkko Nikula 	/*
1126d5898e19SJarkko Nikula 	 * Release the data by enabling service requests and interrupts,
1127d5898e19SJarkko Nikula 	 * without changing any mode bits
1128d5898e19SJarkko Nikula 	 */
1129c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1130d5898e19SJarkko Nikula 
1131d5898e19SJarkko Nikula 	return 1;
1132ca632f55SGrant Likely }
1133ca632f55SGrant Likely 
113451eea52dSLubomir Rintel static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
1135ec93cb6fSLubomir Rintel {
113651eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1137ec93cb6fSLubomir Rintel 
1138ec93cb6fSLubomir Rintel 	/* Stop and reset SSP */
1139ec93cb6fSLubomir Rintel 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1140ec93cb6fSLubomir Rintel 	reset_sccr1(drv_data);
1141ec93cb6fSLubomir Rintel 	if (!pxa25x_ssp_comp(drv_data))
1142ec93cb6fSLubomir Rintel 		pxa2xx_spi_write(drv_data, SSTO, 0);
1143ec93cb6fSLubomir Rintel 	pxa2xx_spi_flush(drv_data);
1144ec93cb6fSLubomir Rintel 	pxa2xx_spi_write(drv_data, SSCR0,
1145ec93cb6fSLubomir Rintel 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
1146ec93cb6fSLubomir Rintel 
1147ec93cb6fSLubomir Rintel 	dev_dbg(&drv_data->pdev->dev, "transfer aborted\n");
1148ec93cb6fSLubomir Rintel 
114951eea52dSLubomir Rintel 	drv_data->controller->cur_msg->status = -EINTR;
115051eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
1151ec93cb6fSLubomir Rintel 
1152ec93cb6fSLubomir Rintel 	return 0;
1153ec93cb6fSLubomir Rintel }
1154ec93cb6fSLubomir Rintel 
115551eea52dSLubomir Rintel static void pxa2xx_spi_handle_err(struct spi_controller *controller,
11567f86bde9SMika Westerberg 				 struct spi_message *msg)
1157ca632f55SGrant Likely {
115851eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1159ca632f55SGrant Likely 
1160d5898e19SJarkko Nikula 	/* Disable the SSP */
1161d5898e19SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0,
1162d5898e19SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
1163d5898e19SJarkko Nikula 	/* Clear and disable interrupts and service requests */
1164d5898e19SJarkko Nikula 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1165d5898e19SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1,
1166d5898e19SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR1)
1167d5898e19SJarkko Nikula 			 & ~(drv_data->int_cr1 | drv_data->dma_cr1));
1168d5898e19SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
1169d5898e19SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1170ca632f55SGrant Likely 
1171d5898e19SJarkko Nikula 	/*
1172d5898e19SJarkko Nikula 	 * Stop the DMA if running. Note DMA callback handler may have unset
1173d5898e19SJarkko Nikula 	 * the dma_running already, which is fine as stopping is not needed
1174d5898e19SJarkko Nikula 	 * then but we shouldn't rely this flag for anything else than
1175d5898e19SJarkko Nikula 	 * stopping. For instance to differentiate between PIO and DMA
1176d5898e19SJarkko Nikula 	 * transfers.
1177d5898e19SJarkko Nikula 	 */
1178d5898e19SJarkko Nikula 	if (atomic_read(&drv_data->dma_running))
1179d5898e19SJarkko Nikula 		pxa2xx_spi_dma_stop(drv_data);
1180ca632f55SGrant Likely }
1181ca632f55SGrant Likely 
118251eea52dSLubomir Rintel static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
11837d94a505SMika Westerberg {
118451eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
11857d94a505SMika Westerberg 
11867d94a505SMika Westerberg 	/* Disable the SSP now */
1187c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0,
1188c039dd27SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
11897d94a505SMika Westerberg 
11907d94a505SMika Westerberg 	return 0;
11917d94a505SMika Westerberg }
11927d94a505SMika Westerberg 
1193ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1194ca632f55SGrant Likely 		    struct pxa2xx_spi_chip *chip_info)
1195ca632f55SGrant Likely {
11963cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
11973cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1198c18d925fSJan Kiszka 	struct gpio_desc *gpiod;
1199ca632f55SGrant Likely 	int err = 0;
1200ca632f55SGrant Likely 
120199f499cdSMika Westerberg 	if (chip == NULL)
120299f499cdSMika Westerberg 		return 0;
120399f499cdSMika Westerberg 
12046ac5a435SAndy Shevchenko 	if (drv_data->cs_gpiods) {
12056ac5a435SAndy Shevchenko 		gpiod = drv_data->cs_gpiods[spi->chip_select];
12066ac5a435SAndy Shevchenko 		if (gpiod) {
1207c18d925fSJan Kiszka 			chip->gpiod_cs = gpiod;
120899f499cdSMika Westerberg 			chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
120999f499cdSMika Westerberg 			gpiod_set_value(gpiod, chip->gpio_cs_inverted);
12106ac5a435SAndy Shevchenko 		}
121199f499cdSMika Westerberg 
121299f499cdSMika Westerberg 		return 0;
121399f499cdSMika Westerberg 	}
121499f499cdSMika Westerberg 
121599f499cdSMika Westerberg 	if (chip_info == NULL)
1216ca632f55SGrant Likely 		return 0;
1217ca632f55SGrant Likely 
1218ca632f55SGrant Likely 	/* NOTE: setup() can be called multiple times, possibly with
1219ca632f55SGrant Likely 	 * different chip_info, release previously requested GPIO
1220ca632f55SGrant Likely 	 */
1221c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
1222a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1223c18d925fSJan Kiszka 		chip->gpiod_cs = NULL;
1224c18d925fSJan Kiszka 	}
1225ca632f55SGrant Likely 
1226ca632f55SGrant Likely 	/* If (*cs_control) is provided, ignore GPIO chip select */
1227ca632f55SGrant Likely 	if (chip_info->cs_control) {
1228ca632f55SGrant Likely 		chip->cs_control = chip_info->cs_control;
1229ca632f55SGrant Likely 		return 0;
1230ca632f55SGrant Likely 	}
1231ca632f55SGrant Likely 
1232ca632f55SGrant Likely 	if (gpio_is_valid(chip_info->gpio_cs)) {
1233ca632f55SGrant Likely 		err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1234ca632f55SGrant Likely 		if (err) {
1235f6bd03a7SJarkko Nikula 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1236f6bd03a7SJarkko Nikula 				chip_info->gpio_cs);
1237ca632f55SGrant Likely 			return err;
1238ca632f55SGrant Likely 		}
1239ca632f55SGrant Likely 
1240c18d925fSJan Kiszka 		gpiod = gpio_to_desc(chip_info->gpio_cs);
1241c18d925fSJan Kiszka 		chip->gpiod_cs = gpiod;
1242ca632f55SGrant Likely 		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1243ca632f55SGrant Likely 
1244c18d925fSJan Kiszka 		err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
1245ca632f55SGrant Likely 	}
1246ca632f55SGrant Likely 
1247ca632f55SGrant Likely 	return err;
1248ca632f55SGrant Likely }
1249ca632f55SGrant Likely 
1250ca632f55SGrant Likely static int setup(struct spi_device *spi)
1251ca632f55SGrant Likely {
1252bffc967eSJarkko Nikula 	struct pxa2xx_spi_chip *chip_info;
1253ca632f55SGrant Likely 	struct chip_data *chip;
1254dccf7369SJarkko Nikula 	const struct lpss_config *config;
12553cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12563cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1257a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1258a0d2642eSMika Westerberg 
1259e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1260e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1261e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1262e5262d05SWeike Chen 		tx_hi_thres = 0;
1263e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1264e5262d05SWeike Chen 		break;
12657c7289a4SAndy Shevchenko 	case CE4100_SSP:
12667c7289a4SAndy Shevchenko 		tx_thres = TX_THRESH_CE4100_DFLT;
12677c7289a4SAndy Shevchenko 		tx_hi_thres = 0;
12687c7289a4SAndy Shevchenko 		rx_thres = RX_THRESH_CE4100_DFLT;
12697c7289a4SAndy Shevchenko 		break;
127003fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
127103fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
127230f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
127334cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1274b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1275fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
1276dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1277dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1278dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1279dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1280e5262d05SWeike Chen 		break;
1281e5262d05SWeike Chen 	default:
1282a0d2642eSMika Westerberg 		tx_hi_thres = 0;
128351eea52dSLubomir Rintel 		if (spi_controller_is_slave(drv_data->controller)) {
1284ec93cb6fSLubomir Rintel 			tx_thres = 1;
1285ec93cb6fSLubomir Rintel 			rx_thres = 2;
1286ec93cb6fSLubomir Rintel 		} else {
1287ec93cb6fSLubomir Rintel 			tx_thres = TX_THRESH_DFLT;
1288a0d2642eSMika Westerberg 			rx_thres = RX_THRESH_DFLT;
1289ec93cb6fSLubomir Rintel 		}
1290e5262d05SWeike Chen 		break;
1291a0d2642eSMika Westerberg 	}
1292ca632f55SGrant Likely 
1293ca632f55SGrant Likely 	/* Only alloc on first setup */
1294ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1295ca632f55SGrant Likely 	if (!chip) {
1296ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
12979deae459SJingoo Han 		if (!chip)
1298ca632f55SGrant Likely 			return -ENOMEM;
1299ca632f55SGrant Likely 
1300ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1301ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1302f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1303f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1304ca632f55SGrant Likely 				kfree(chip);
1305ca632f55SGrant Likely 				return -EINVAL;
1306ca632f55SGrant Likely 			}
1307ca632f55SGrant Likely 
1308ca632f55SGrant Likely 			chip->frm = spi->chip_select;
1309c18d925fSJan Kiszka 		}
131051eea52dSLubomir Rintel 		chip->enable_dma = drv_data->controller_info->enable_dma;
1311ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1312ca632f55SGrant Likely 	}
1313ca632f55SGrant Likely 
1314ca632f55SGrant Likely 	/* protocol drivers may change the chip settings, so...
1315ca632f55SGrant Likely 	 * if chip_info exists, use it */
1316ca632f55SGrant Likely 	chip_info = spi->controller_data;
1317ca632f55SGrant Likely 
1318ca632f55SGrant Likely 	/* chip_info isn't always needed */
1319ca632f55SGrant Likely 	chip->cr1 = 0;
1320ca632f55SGrant Likely 	if (chip_info) {
1321ca632f55SGrant Likely 		if (chip_info->timeout)
1322ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1323ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1324ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1325a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1326a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1327ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1328ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1329ca632f55SGrant Likely 		chip->dma_threshold = 0;
1330ca632f55SGrant Likely 		if (chip_info->enable_loopback)
1331ca632f55SGrant Likely 			chip->cr1 = SSCR1_LBM;
1332ca632f55SGrant Likely 	}
133351eea52dSLubomir Rintel 	if (spi_controller_is_slave(drv_data->controller)) {
1334ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCFR;
1335ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCLKDIR;
1336ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SFRMDIR;
1337ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SPH;
1338ec93cb6fSLubomir Rintel 	}
1339ca632f55SGrant Likely 
1340a0d2642eSMika Westerberg 	chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1341a0d2642eSMika Westerberg 	chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1342a0d2642eSMika Westerberg 				| SSITF_TxHiThresh(tx_hi_thres);
1343a0d2642eSMika Westerberg 
1344ca632f55SGrant Likely 	/* set dma burst and threshold outside of chip_info path so that if
1345ca632f55SGrant Likely 	 * chip_info goes away after setting chip->enable_dma, the
1346ca632f55SGrant Likely 	 * burst and threshold can still respond to changes in bits_per_word */
1347ca632f55SGrant Likely 	if (chip->enable_dma) {
1348ca632f55SGrant Likely 		/* set up legal burst and threshold for dma */
1349cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1350cd7bed00SMika Westerberg 						spi->bits_per_word,
1351ca632f55SGrant Likely 						&chip->dma_burst_size,
1352ca632f55SGrant Likely 						&chip->dma_threshold)) {
1353f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1354f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1355ca632f55SGrant Likely 		}
1356000c6af4SAndy Shevchenko 		dev_dbg(&spi->dev,
1357000c6af4SAndy Shevchenko 			"in setup: DMA burst size set to %u\n",
1358000c6af4SAndy Shevchenko 			chip->dma_burst_size);
1359ca632f55SGrant Likely 	}
1360ca632f55SGrant Likely 
1361e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1362e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1363e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1364e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1365e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1366e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1367e5262d05SWeike Chen 		break;
13687c7289a4SAndy Shevchenko 	case CE4100_SSP:
13697c7289a4SAndy Shevchenko 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
13707c7289a4SAndy Shevchenko 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
13717c7289a4SAndy Shevchenko 		break;
1372e5262d05SWeike Chen 	default:
1373e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1374e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1375e5262d05SWeike Chen 		break;
1376e5262d05SWeike Chen 	}
1377e5262d05SWeike Chen 
1378ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1379ca632f55SGrant Likely 	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1380ca632f55SGrant Likely 			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1381ca632f55SGrant Likely 
1382b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1383b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1384b833172fSMika Westerberg 
1385ca632f55SGrant Likely 	if (spi->bits_per_word <= 8) {
1386ca632f55SGrant Likely 		chip->n_bytes = 1;
1387ca632f55SGrant Likely 		chip->read = u8_reader;
1388ca632f55SGrant Likely 		chip->write = u8_writer;
1389ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 16) {
1390ca632f55SGrant Likely 		chip->n_bytes = 2;
1391ca632f55SGrant Likely 		chip->read = u16_reader;
1392ca632f55SGrant Likely 		chip->write = u16_writer;
1393ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 32) {
1394ca632f55SGrant Likely 		chip->n_bytes = 4;
1395ca632f55SGrant Likely 		chip->read = u32_reader;
1396ca632f55SGrant Likely 		chip->write = u32_writer;
1397ca632f55SGrant Likely 	}
1398ca632f55SGrant Likely 
1399ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1400ca632f55SGrant Likely 
1401ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
1402ca632f55SGrant Likely 		return 0;
1403ca632f55SGrant Likely 
1404ca632f55SGrant Likely 	return setup_cs(spi, chip, chip_info);
1405ca632f55SGrant Likely }
1406ca632f55SGrant Likely 
1407ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1408ca632f55SGrant Likely {
1409ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
14103cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
14113cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1412ca632f55SGrant Likely 
1413ca632f55SGrant Likely 	if (!chip)
1414ca632f55SGrant Likely 		return;
1415ca632f55SGrant Likely 
14166ac5a435SAndy Shevchenko 	if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
1417c18d925fSJan Kiszka 	    chip->gpiod_cs)
1418a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1419ca632f55SGrant Likely 
1420ca632f55SGrant Likely 	kfree(chip);
1421ca632f55SGrant Likely }
1422ca632f55SGrant Likely 
14238422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
142403fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
142503fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
142603fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
142703fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
142803fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
142930f3a6abSMika Westerberg 	{ "8086228E", LPSS_BSW_SSP },
143003fbf488SJarkko Nikula 	{ },
143103fbf488SJarkko Nikula };
143203fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
143303fbf488SJarkko Nikula 
143434cadd9cSJarkko Nikula /*
143534cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
143634cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
143734cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
143834cadd9cSJarkko Nikula  */
143934cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
144034cadd9cSJarkko Nikula 	/* SPT-LP */
144134cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
144234cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
144334cadd9cSJarkko Nikula 	/* SPT-H */
144434cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
144534cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1446704d2b07SMika Westerberg 	/* KBL-H */
1447704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1448704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
1449c1b03f11SJarkko Nikula 	/* BXT A-Step */
1450b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1451b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1452b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1453c1b03f11SJarkko Nikula 	/* BXT B-Step */
1454c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1455c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1456c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1457e18a80acSDavid E. Box 	/* GLK */
1458e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1459e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1460e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
146122d71a50SMika Westerberg 	/* ICL-LP */
146222d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
146322d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
146422d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
14658cc77204SJarkko Nikula 	/* EHL */
14668cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
14678cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
14688cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
14699c7315c9SJarkko Nikula 	/* JSL */
14709c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
14719c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
14729c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
1473b7c08cf8SJarkko Nikula 	/* APL */
1474b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1475b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1476b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1477fc0b2accSJarkko Nikula 	/* CNL-LP */
1478fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1479fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1480fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1481fc0b2accSJarkko Nikula 	/* CNL-H */
1482fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1483fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1484fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
148541a91802SEvan Green 	/* CML-LP */
148641a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
148741a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
148841a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
1489f0cf17edSJarkko Nikula 	/* CML-H */
1490f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1491f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1492f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
1493a4127952SJarkko Nikula 	/* TGL-LP */
1494a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1495a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1496a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1497a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1498a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1499a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1500a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
150194e5c23dSAxel Lin 	{ },
150234cadd9cSJarkko Nikula };
150334cadd9cSJarkko Nikula 
150487ae1d2dSLubomir Rintel static const struct of_device_id pxa2xx_spi_of_match[] = {
150587ae1d2dSLubomir Rintel 	{ .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
150687ae1d2dSLubomir Rintel 	{},
150787ae1d2dSLubomir Rintel };
150887ae1d2dSLubomir Rintel MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
150987ae1d2dSLubomir Rintel 
151087ae1d2dSLubomir Rintel #ifdef CONFIG_ACPI
151187ae1d2dSLubomir Rintel 
1512365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
151387ae1d2dSLubomir Rintel {
1514365e856eSAndy Shevchenko 	struct acpi_device *adev;
151587ae1d2dSLubomir Rintel 	unsigned int devid;
151687ae1d2dSLubomir Rintel 	int port_id = -1;
151787ae1d2dSLubomir Rintel 
1518365e856eSAndy Shevchenko 	adev = ACPI_COMPANION(dev);
151987ae1d2dSLubomir Rintel 	if (adev && adev->pnp.unique_id &&
152087ae1d2dSLubomir Rintel 	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
152187ae1d2dSLubomir Rintel 		port_id = devid;
152287ae1d2dSLubomir Rintel 	return port_id;
152387ae1d2dSLubomir Rintel }
152487ae1d2dSLubomir Rintel 
152587ae1d2dSLubomir Rintel #else /* !CONFIG_ACPI */
152687ae1d2dSLubomir Rintel 
1527365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
152887ae1d2dSLubomir Rintel {
152987ae1d2dSLubomir Rintel 	return -1;
153087ae1d2dSLubomir Rintel }
153187ae1d2dSLubomir Rintel 
153287ae1d2dSLubomir Rintel #endif /* CONFIG_ACPI */
153387ae1d2dSLubomir Rintel 
153487ae1d2dSLubomir Rintel 
153587ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
153687ae1d2dSLubomir Rintel 
153734cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
153834cadd9cSJarkko Nikula {
15395ba846b1SAndy Shevchenko 	return param == chan->device->dev;
154034cadd9cSJarkko Nikula }
154134cadd9cSJarkko Nikula 
154287ae1d2dSLubomir Rintel #endif /* CONFIG_PCI */
154387ae1d2dSLubomir Rintel 
154451eea52dSLubomir Rintel static struct pxa2xx_spi_controller *
15450db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1546a3496855SMika Westerberg {
154751eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *pdata;
1548a3496855SMika Westerberg 	struct ssp_device *ssp;
1549a3496855SMika Westerberg 	struct resource *res;
15506fb7427dSAndy Shevchenko 	struct device *parent = pdev->dev.parent;
15516fb7427dSAndy Shevchenko 	struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
155234cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
155355ef8262SLubomir Rintel 	enum pxa_ssp_type type;
1554f2faa3ecSAndy Shevchenko 	const void *match;
1555a3496855SMika Westerberg 
15566fb7427dSAndy Shevchenko 	if (pcidev)
15576fb7427dSAndy Shevchenko 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
1558a3496855SMika Westerberg 
1559f2faa3ecSAndy Shevchenko 	match = device_get_match_data(&pdev->dev);
1560f2faa3ecSAndy Shevchenko 	if (match)
1561f2faa3ecSAndy Shevchenko 		type = (enum pxa_ssp_type)match;
156234cadd9cSJarkko Nikula 	else if (pcidev_id)
156355ef8262SLubomir Rintel 		type = (enum pxa_ssp_type)pcidev_id->driver_data;
156403fbf488SJarkko Nikula 	else
156503fbf488SJarkko Nikula 		return NULL;
156603fbf488SJarkko Nikula 
1567cc0ee987SMika Westerberg 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
15689deae459SJingoo Han 	if (!pdata)
1569a3496855SMika Westerberg 		return NULL;
1570a3496855SMika Westerberg 
1571a3496855SMika Westerberg 	ssp = &pdata->ssp;
1572a3496855SMika Westerberg 
157377c544d2SAndy Shevchenko 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1574cbfd6a21SSachin Kamat 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1575cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
15766dc81f6fSMika Westerberg 		return NULL;
1577a3496855SMika Westerberg 
157877c544d2SAndy Shevchenko 	ssp->phys_base = res->start;
157977c544d2SAndy Shevchenko 
158087ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
158134cadd9cSJarkko Nikula 	if (pcidev_id) {
15826fb7427dSAndy Shevchenko 		pdata->tx_param = parent;
15836fb7427dSAndy Shevchenko 		pdata->rx_param = parent;
158434cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
158534cadd9cSJarkko Nikula 	}
158687ae1d2dSLubomir Rintel #endif
158734cadd9cSJarkko Nikula 
1588a3496855SMika Westerberg 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
15895eb263efSChuhong Yuan 	if (IS_ERR(ssp->clk))
15905eb263efSChuhong Yuan 		return NULL;
1591a3496855SMika Westerberg 
1592a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
15935eb263efSChuhong Yuan 	if (ssp->irq < 0)
15945eb263efSChuhong Yuan 		return NULL;
15955eb263efSChuhong Yuan 
1596a3496855SMika Westerberg 	ssp->type = type;
15974f3d9577SAndy Shevchenko 	ssp->dev = &pdev->dev;
1598365e856eSAndy Shevchenko 	ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
1599a3496855SMika Westerberg 
1600f2faa3ecSAndy Shevchenko 	pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
1601a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1602cddb339bSMika Westerberg 	pdata->enable_dma = true;
160337821a82SAndy Shevchenko 	pdata->dma_burst_size = 1;
1604a3496855SMika Westerberg 
1605a3496855SMika Westerberg 	return pdata;
1606a3496855SMika Westerberg }
1607a3496855SMika Westerberg 
160851eea52dSLubomir Rintel static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
16093cc7b0e3SJarkko Nikula 				      unsigned int cs)
16100c27d9cfSMika Westerberg {
161151eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
16120c27d9cfSMika Westerberg 
16130c27d9cfSMika Westerberg 	if (has_acpi_companion(&drv_data->pdev->dev)) {
16140c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
16150c27d9cfSMika Westerberg 		/*
16160c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
16170c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
16180c27d9cfSMika Westerberg 		 * to match what Linux expects.
16190c27d9cfSMika Westerberg 		 */
16200c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
162130f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
16220c27d9cfSMika Westerberg 			return cs - 1;
16230c27d9cfSMika Westerberg 
16240c27d9cfSMika Westerberg 		default:
16250c27d9cfSMika Westerberg 			break;
16260c27d9cfSMika Westerberg 		}
16270c27d9cfSMika Westerberg 	}
16280c27d9cfSMika Westerberg 
16290c27d9cfSMika Westerberg 	return cs;
16300c27d9cfSMika Westerberg }
16310c27d9cfSMika Westerberg 
1632b2662a16SDaniel Vetter static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1633b2662a16SDaniel Vetter {
1634b2662a16SDaniel Vetter 	return MAX_DMA_LEN;
1635b2662a16SDaniel Vetter }
1636b2662a16SDaniel Vetter 
1637fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1638ca632f55SGrant Likely {
1639ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
164051eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *platform_info;
164151eea52dSLubomir Rintel 	struct spi_controller *controller;
1642ca632f55SGrant Likely 	struct driver_data *drv_data;
1643ca632f55SGrant Likely 	struct ssp_device *ssp;
16448b136baaSJarkko Nikula 	const struct lpss_config *config;
164599f499cdSMika Westerberg 	int status, count;
1646c039dd27SJarkko Nikula 	u32 tmp;
1647ca632f55SGrant Likely 
1648851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1649851bacf5SMika Westerberg 	if (!platform_info) {
16500db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
1651a3496855SMika Westerberg 		if (!platform_info) {
1652851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
1653851bacf5SMika Westerberg 			return -ENODEV;
1654851bacf5SMika Westerberg 		}
1655a3496855SMika Westerberg 	}
1656ca632f55SGrant Likely 
1657ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1658851bacf5SMika Westerberg 	if (!ssp)
1659851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1660851bacf5SMika Westerberg 
1661851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
1662851bacf5SMika Westerberg 		dev_err(&pdev->dev, "failed to get ssp\n");
1663ca632f55SGrant Likely 		return -ENODEV;
1664ca632f55SGrant Likely 	}
1665ca632f55SGrant Likely 
1666ec93cb6fSLubomir Rintel 	if (platform_info->is_slave)
166751eea52dSLubomir Rintel 		controller = spi_alloc_slave(dev, sizeof(struct driver_data));
1668ec93cb6fSLubomir Rintel 	else
166951eea52dSLubomir Rintel 		controller = spi_alloc_master(dev, sizeof(struct driver_data));
1670ec93cb6fSLubomir Rintel 
167151eea52dSLubomir Rintel 	if (!controller) {
167251eea52dSLubomir Rintel 		dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1673ca632f55SGrant Likely 		pxa_ssp_free(ssp);
1674ca632f55SGrant Likely 		return -ENOMEM;
1675ca632f55SGrant Likely 	}
167651eea52dSLubomir Rintel 	drv_data = spi_controller_get_devdata(controller);
167751eea52dSLubomir Rintel 	drv_data->controller = controller;
167851eea52dSLubomir Rintel 	drv_data->controller_info = platform_info;
1679ca632f55SGrant Likely 	drv_data->pdev = pdev;
1680ca632f55SGrant Likely 	drv_data->ssp = ssp;
1681ca632f55SGrant Likely 
168251eea52dSLubomir Rintel 	controller->dev.of_node = pdev->dev.of_node;
1683ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
168451eea52dSLubomir Rintel 	controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1685ca632f55SGrant Likely 
168651eea52dSLubomir Rintel 	controller->bus_num = ssp->port_id;
168751eea52dSLubomir Rintel 	controller->dma_alignment = DMA_ALIGNMENT;
168851eea52dSLubomir Rintel 	controller->cleanup = cleanup;
168951eea52dSLubomir Rintel 	controller->setup = setup;
169051eea52dSLubomir Rintel 	controller->set_cs = pxa2xx_spi_set_cs;
169151eea52dSLubomir Rintel 	controller->transfer_one = pxa2xx_spi_transfer_one;
169251eea52dSLubomir Rintel 	controller->slave_abort = pxa2xx_spi_slave_abort;
169351eea52dSLubomir Rintel 	controller->handle_err = pxa2xx_spi_handle_err;
169451eea52dSLubomir Rintel 	controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
169551eea52dSLubomir Rintel 	controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
169651eea52dSLubomir Rintel 	controller->auto_runtime_pm = true;
169751eea52dSLubomir Rintel 	controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1698ca632f55SGrant Likely 
1699ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1700ca632f55SGrant Likely 
1701ca632f55SGrant Likely 	drv_data->ioaddr = ssp->mmio_base;
1702ca632f55SGrant Likely 	drv_data->ssdr_physical = ssp->phys_base + SSDR;
1703ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1704e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1705e5262d05SWeike Chen 		case QUARK_X1000_SSP:
170651eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1707e5262d05SWeike Chen 			break;
1708e5262d05SWeike Chen 		default:
170951eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1710e5262d05SWeike Chen 			break;
1711e5262d05SWeike Chen 		}
1712e5262d05SWeike Chen 
1713ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1714ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1715ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1716ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1717ca632f55SGrant Likely 	} else {
171851eea52dSLubomir Rintel 		controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1719ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
17205928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1721ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1722ec93cb6fSLubomir Rintel 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1723ec93cb6fSLubomir Rintel 						| SSSR_ROR | SSSR_TUR;
1724ca632f55SGrant Likely 	}
1725ca632f55SGrant Likely 
1726ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1727ca632f55SGrant Likely 			drv_data);
1728ca632f55SGrant Likely 	if (status < 0) {
1729ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
173051eea52dSLubomir Rintel 		goto out_error_controller_alloc;
1731ca632f55SGrant Likely 	}
1732ca632f55SGrant Likely 
1733ca632f55SGrant Likely 	/* Setup DMA if requested */
1734ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1735cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1736cd7bed00SMika Westerberg 		if (status) {
17378b57b11bSFlavio Suligoi 			dev_warn(dev, "no DMA channels available, using PIO\n");
1738cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1739b6ced294SJarkko Nikula 		} else {
174051eea52dSLubomir Rintel 			controller->can_dma = pxa2xx_spi_can_dma;
1741bf9f742cSMark Brown 			controller->max_dma_len = MAX_DMA_LEN;
1742b2662a16SDaniel Vetter 			controller->max_transfer_size =
1743b2662a16SDaniel Vetter 				pxa2xx_spi_max_dma_transfer_size;
1744ca632f55SGrant Likely 		}
1745ca632f55SGrant Likely 	}
1746ca632f55SGrant Likely 
1747ca632f55SGrant Likely 	/* Enable SOC clock */
174862bbc864STobias Jordan 	status = clk_prepare_enable(ssp->clk);
174962bbc864STobias Jordan 	if (status)
175062bbc864STobias Jordan 		goto out_error_dma_irq_alloc;
17513343b7a6SMika Westerberg 
175251eea52dSLubomir Rintel 	controller->max_speed_hz = clk_get_rate(ssp->clk);
175323cdddb2SJarkko Nikula 	/*
175423cdddb2SJarkko Nikula 	 * Set minimum speed for all other platforms than Intel Quark which is
175523cdddb2SJarkko Nikula 	 * able do under 1 Hz transfers.
175623cdddb2SJarkko Nikula 	 */
175723cdddb2SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
175823cdddb2SJarkko Nikula 		controller->min_speed_hz =
175923cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 4096);
176023cdddb2SJarkko Nikula 	else if (!is_quark_x1000_ssp(drv_data))
176123cdddb2SJarkko Nikula 		controller->min_speed_hz =
176223cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 512);
1763ca632f55SGrant Likely 
1764ca632f55SGrant Likely 	/* Load default SSP configuration */
1765c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
1766e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1767e5262d05SWeike Chen 	case QUARK_X1000_SSP:
17687c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
17697c7289a4SAndy Shevchenko 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1770c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1771e5262d05SWeike Chen 
1772e5262d05SWeike Chen 		/* using the Motorola SPI protocol and use 8 bit frame */
17737c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
17747c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1775e5262d05SWeike Chen 		break;
17767c7289a4SAndy Shevchenko 	case CE4100_SSP:
17777c7289a4SAndy Shevchenko 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
17787c7289a4SAndy Shevchenko 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
17797c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
17807c7289a4SAndy Shevchenko 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
17817c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1782a2dd8af0SAndy Shevchenko 		break;
1783e5262d05SWeike Chen 	default:
1784ec93cb6fSLubomir Rintel 
178551eea52dSLubomir Rintel 		if (spi_controller_is_slave(controller)) {
1786ec93cb6fSLubomir Rintel 			tmp = SSCR1_SCFR |
1787ec93cb6fSLubomir Rintel 			      SSCR1_SCLKDIR |
1788ec93cb6fSLubomir Rintel 			      SSCR1_SFRMDIR |
1789ec93cb6fSLubomir Rintel 			      SSCR1_RxTresh(2) |
1790ec93cb6fSLubomir Rintel 			      SSCR1_TxTresh(1) |
1791ec93cb6fSLubomir Rintel 			      SSCR1_SPH;
1792ec93cb6fSLubomir Rintel 		} else {
1793c039dd27SJarkko Nikula 			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1794c039dd27SJarkko Nikula 			      SSCR1_TxTresh(TX_THRESH_DFLT);
1795ec93cb6fSLubomir Rintel 		}
1796c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1797ec93cb6fSLubomir Rintel 		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
179851eea52dSLubomir Rintel 		if (!spi_controller_is_slave(controller))
1799ec93cb6fSLubomir Rintel 			tmp |= SSCR0_SCR(2);
1800c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1801e5262d05SWeike Chen 		break;
1802e5262d05SWeike Chen 	}
1803e5262d05SWeike Chen 
1804ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1805c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1806e5262d05SWeike Chen 
1807e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1808c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1809ca632f55SGrant Likely 
18108b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
18118b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
18128b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
18138b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
18148b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
18158b136baaSJarkko Nikula 						   config->reg_capabilities);
18168b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
18178b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
18188b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
181930f3a6abSMika Westerberg 		} else if (config->cs_num) {
182030f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
18218b136baaSJarkko Nikula 		}
18228b136baaSJarkko Nikula 	}
182351eea52dSLubomir Rintel 	controller->num_chipselect = platform_info->num_chipselect;
18248b136baaSJarkko Nikula 
182599f499cdSMika Westerberg 	count = gpiod_count(&pdev->dev, "cs");
18266ac5a435SAndy Shevchenko 	if (count > 0) {
18276ac5a435SAndy Shevchenko 		int i;
18286ac5a435SAndy Shevchenko 
182951eea52dSLubomir Rintel 		controller->num_chipselect = max_t(int, count,
183051eea52dSLubomir Rintel 			controller->num_chipselect);
183199f499cdSMika Westerberg 
18326ac5a435SAndy Shevchenko 		drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
183351eea52dSLubomir Rintel 			controller->num_chipselect, sizeof(struct gpio_desc *),
18346ac5a435SAndy Shevchenko 			GFP_KERNEL);
18356ac5a435SAndy Shevchenko 		if (!drv_data->cs_gpiods) {
18366ac5a435SAndy Shevchenko 			status = -ENOMEM;
18376ac5a435SAndy Shevchenko 			goto out_error_clock_enabled;
18386ac5a435SAndy Shevchenko 		}
18396ac5a435SAndy Shevchenko 
184051eea52dSLubomir Rintel 		for (i = 0; i < controller->num_chipselect; i++) {
18416ac5a435SAndy Shevchenko 			struct gpio_desc *gpiod;
18426ac5a435SAndy Shevchenko 
1843d35f2dc9SAndy Shevchenko 			gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
18446ac5a435SAndy Shevchenko 			if (IS_ERR(gpiod)) {
18456ac5a435SAndy Shevchenko 				/* Means use native chip select */
18466ac5a435SAndy Shevchenko 				if (PTR_ERR(gpiod) == -ENOENT)
18476ac5a435SAndy Shevchenko 					continue;
18486ac5a435SAndy Shevchenko 
184977d33897SLubomir Rintel 				status = PTR_ERR(gpiod);
18506ac5a435SAndy Shevchenko 				goto out_error_clock_enabled;
18516ac5a435SAndy Shevchenko 			} else {
18526ac5a435SAndy Shevchenko 				drv_data->cs_gpiods[i] = gpiod;
18536ac5a435SAndy Shevchenko 			}
18546ac5a435SAndy Shevchenko 		}
18556ac5a435SAndy Shevchenko 	}
18566ac5a435SAndy Shevchenko 
185777d33897SLubomir Rintel 	if (platform_info->is_slave) {
185877d33897SLubomir Rintel 		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
185977d33897SLubomir Rintel 						"ready", GPIOD_OUT_LOW);
186077d33897SLubomir Rintel 		if (IS_ERR(drv_data->gpiod_ready)) {
186177d33897SLubomir Rintel 			status = PTR_ERR(drv_data->gpiod_ready);
186277d33897SLubomir Rintel 			goto out_error_clock_enabled;
186377d33897SLubomir Rintel 		}
186477d33897SLubomir Rintel 	}
186577d33897SLubomir Rintel 
1866836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1867836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1868836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1869836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1870836d1a22SAntonio Ospite 
1871ca632f55SGrant Likely 	/* Register with the SPI framework */
1872ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
187351eea52dSLubomir Rintel 	status = devm_spi_register_controller(&pdev->dev, controller);
1874ca632f55SGrant Likely 	if (status != 0) {
187551eea52dSLubomir Rintel 		dev_err(&pdev->dev, "problem registering spi controller\n");
187612742045SLubomir Rintel 		goto out_error_pm_runtime_enabled;
1877ca632f55SGrant Likely 	}
1878ca632f55SGrant Likely 
1879ca632f55SGrant Likely 	return status;
1880ca632f55SGrant Likely 
188112742045SLubomir Rintel out_error_pm_runtime_enabled:
1882e2b714afSJarkko Nikula 	pm_runtime_put_noidle(&pdev->dev);
1883e2b714afSJarkko Nikula 	pm_runtime_disable(&pdev->dev);
188412742045SLubomir Rintel 
188512742045SLubomir Rintel out_error_clock_enabled:
18863343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
188762bbc864STobias Jordan 
188862bbc864STobias Jordan out_error_dma_irq_alloc:
1889cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1890ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1891ca632f55SGrant Likely 
189251eea52dSLubomir Rintel out_error_controller_alloc:
189351eea52dSLubomir Rintel 	spi_controller_put(controller);
1894ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1895ca632f55SGrant Likely 	return status;
1896ca632f55SGrant Likely }
1897ca632f55SGrant Likely 
1898ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1899ca632f55SGrant Likely {
1900ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
1901ca632f55SGrant Likely 	struct ssp_device *ssp;
1902ca632f55SGrant Likely 
1903ca632f55SGrant Likely 	if (!drv_data)
1904ca632f55SGrant Likely 		return 0;
1905ca632f55SGrant Likely 	ssp = drv_data->ssp;
1906ca632f55SGrant Likely 
19077d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
19087d94a505SMika Westerberg 
1909ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
1910c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
19113343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1912ca632f55SGrant Likely 
1913ca632f55SGrant Likely 	/* Release DMA */
191451eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma)
1915cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1916ca632f55SGrant Likely 
19177d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
19187d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
19197d94a505SMika Westerberg 
1920ca632f55SGrant Likely 	/* Release IRQ */
1921ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1922ca632f55SGrant Likely 
1923ca632f55SGrant Likely 	/* Release SSP */
1924ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1925ca632f55SGrant Likely 
1926ca632f55SGrant Likely 	return 0;
1927ca632f55SGrant Likely }
1928ca632f55SGrant Likely 
1929382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP
1930ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1931ca632f55SGrant Likely {
1932ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1933ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1934bffc967eSJarkko Nikula 	int status;
1935ca632f55SGrant Likely 
193651eea52dSLubomir Rintel 	status = spi_controller_suspend(drv_data->controller);
1937ca632f55SGrant Likely 	if (status != 0)
1938ca632f55SGrant Likely 		return status;
1939c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
19402b9375b9SDmitry Eremin-Solenikov 
19412b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
19423343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1943ca632f55SGrant Likely 
1944ca632f55SGrant Likely 	return 0;
1945ca632f55SGrant Likely }
1946ca632f55SGrant Likely 
1947ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1948ca632f55SGrant Likely {
1949ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1950ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1951bffc967eSJarkko Nikula 	int status;
1952ca632f55SGrant Likely 
1953ca632f55SGrant Likely 	/* Enable the SSP clock */
195462bbc864STobias Jordan 	if (!pm_runtime_suspended(dev)) {
195562bbc864STobias Jordan 		status = clk_prepare_enable(ssp->clk);
195662bbc864STobias Jordan 		if (status)
195762bbc864STobias Jordan 			return status;
195862bbc864STobias Jordan 	}
1959ca632f55SGrant Likely 
1960ca632f55SGrant Likely 	/* Start the queue running */
196151eea52dSLubomir Rintel 	return spi_controller_resume(drv_data->controller);
1962ca632f55SGrant Likely }
19637d94a505SMika Westerberg #endif
19647d94a505SMika Westerberg 
1965ec833050SRafael J. Wysocki #ifdef CONFIG_PM
19667d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
19677d94a505SMika Westerberg {
19687d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
19697d94a505SMika Westerberg 
19707d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
19717d94a505SMika Westerberg 	return 0;
19727d94a505SMika Westerberg }
19737d94a505SMika Westerberg 
19747d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
19757d94a505SMika Westerberg {
19767d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
197762bbc864STobias Jordan 	int status;
19787d94a505SMika Westerberg 
197962bbc864STobias Jordan 	status = clk_prepare_enable(drv_data->ssp->clk);
198062bbc864STobias Jordan 	return status;
19817d94a505SMika Westerberg }
19827d94a505SMika Westerberg #endif
1983ca632f55SGrant Likely 
1984ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
19857d94a505SMika Westerberg 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
19867d94a505SMika Westerberg 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
19877d94a505SMika Westerberg 			   pxa2xx_spi_runtime_resume, NULL)
1988ca632f55SGrant Likely };
1989ca632f55SGrant Likely 
1990ca632f55SGrant Likely static struct platform_driver driver = {
1991ca632f55SGrant Likely 	.driver = {
1992ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
1993ca632f55SGrant Likely 		.pm	= &pxa2xx_spi_pm_ops,
1994a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
199587ae1d2dSLubomir Rintel 		.of_match_table = of_match_ptr(pxa2xx_spi_of_match),
1996ca632f55SGrant Likely 	},
1997ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
1998ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
1999ca632f55SGrant Likely };
2000ca632f55SGrant Likely 
2001ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
2002ca632f55SGrant Likely {
2003ca632f55SGrant Likely 	return platform_driver_register(&driver);
2004ca632f55SGrant Likely }
2005ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
2006ca632f55SGrant Likely 
2007ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
2008ca632f55SGrant Likely {
2009ca632f55SGrant Likely 	platform_driver_unregister(&driver);
2010ca632f55SGrant Likely }
2011ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
201251ebf6acSFlavio Suligoi 
201351ebf6acSFlavio Suligoi MODULE_SOFTDEP("pre: dw_dmac");
2014