xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision 20f4c379c39d54a8912a2a472cee41782a3a312a)
1ca632f55SGrant Likely /*
2ca632f55SGrant Likely  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
3a0d2642eSMika Westerberg  * Copyright (C) 2013, Intel Corporation
4ca632f55SGrant Likely  *
5ca632f55SGrant Likely  * This program is free software; you can redistribute it and/or modify
6ca632f55SGrant Likely  * it under the terms of the GNU General Public License as published by
7ca632f55SGrant Likely  * the Free Software Foundation; either version 2 of the License, or
8ca632f55SGrant Likely  * (at your option) any later version.
9ca632f55SGrant Likely  *
10ca632f55SGrant Likely  * This program is distributed in the hope that it will be useful,
11ca632f55SGrant Likely  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ca632f55SGrant Likely  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13ca632f55SGrant Likely  * GNU General Public License for more details.
14ca632f55SGrant Likely  */
15ca632f55SGrant Likely 
168b136baaSJarkko Nikula #include <linux/bitops.h>
17ca632f55SGrant Likely #include <linux/init.h>
18ca632f55SGrant Likely #include <linux/module.h>
19ca632f55SGrant Likely #include <linux/device.h>
20ca632f55SGrant Likely #include <linux/ioport.h>
21ca632f55SGrant Likely #include <linux/errno.h>
22cbfd6a21SSachin Kamat #include <linux/err.h>
23ca632f55SGrant Likely #include <linux/interrupt.h>
249df461ecSAndy Shevchenko #include <linux/kernel.h>
2534cadd9cSJarkko Nikula #include <linux/pci.h>
26ca632f55SGrant Likely #include <linux/platform_device.h>
27ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h>
28ca632f55SGrant Likely #include <linux/spi/spi.h>
29ca632f55SGrant Likely #include <linux/delay.h>
30ca632f55SGrant Likely #include <linux/gpio.h>
31089bd46dSMika Westerberg #include <linux/gpio/consumer.h>
32ca632f55SGrant Likely #include <linux/slab.h>
333343b7a6SMika Westerberg #include <linux/clk.h>
347d94a505SMika Westerberg #include <linux/pm_runtime.h>
35a3496855SMika Westerberg #include <linux/acpi.h>
3687ae1d2dSLubomir Rintel #include <linux/of_device.h>
37ca632f55SGrant Likely 
38cd7bed00SMika Westerberg #include "spi-pxa2xx.h"
39ca632f55SGrant Likely 
40ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street");
41ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
42ca632f55SGrant Likely MODULE_LICENSE("GPL");
43ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi");
44ca632f55SGrant Likely 
45ca632f55SGrant Likely #define TIMOUT_DFLT		1000
46ca632f55SGrant Likely 
47ca632f55SGrant Likely /*
48ca632f55SGrant Likely  * for testing SSCR1 changes that require SSP restart, basically
49ca632f55SGrant Likely  * everything except the service and interrupt enables, the pxa270 developer
50ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
51ca632f55SGrant Likely  * list, but the PXA255 dev man says all bits without really meaning the
52ca632f55SGrant Likely  * service and interrupt enables
53ca632f55SGrant Likely  */
54ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
55ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
56ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
57ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
58ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
59ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
60ca632f55SGrant Likely 
61e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
62e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
63e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
64e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
65e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
66e5262d05SWeike Chen 
677c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
687c7289a4SAndy Shevchenko 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
697c7289a4SAndy Shevchenko 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
707c7289a4SAndy Shevchenko 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
717c7289a4SAndy Shevchenko 				| CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
727c7289a4SAndy Shevchenko 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
737c7289a4SAndy Shevchenko 
74624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
75624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE			BIT(0)
76624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
778b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT			9
788b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
79a0d2642eSMika Westerberg 
80dccf7369SJarkko Nikula struct lpss_config {
81dccf7369SJarkko Nikula 	/* LPSS offset from drv_data->ioaddr */
82dccf7369SJarkko Nikula 	unsigned offset;
83dccf7369SJarkko Nikula 	/* Register offsets from drv_data->lpss_base or -1 */
84dccf7369SJarkko Nikula 	int reg_general;
85dccf7369SJarkko Nikula 	int reg_ssp;
86dccf7369SJarkko Nikula 	int reg_cs_ctrl;
878b136baaSJarkko Nikula 	int reg_capabilities;
88dccf7369SJarkko Nikula 	/* FIFO thresholds */
89dccf7369SJarkko Nikula 	u32 rx_threshold;
90dccf7369SJarkko Nikula 	u32 tx_threshold_lo;
91dccf7369SJarkko Nikula 	u32 tx_threshold_hi;
92c1e4a53cSMika Westerberg 	/* Chip select control */
93c1e4a53cSMika Westerberg 	unsigned cs_sel_shift;
94c1e4a53cSMika Westerberg 	unsigned cs_sel_mask;
9530f3a6abSMika Westerberg 	unsigned cs_num;
96dccf7369SJarkko Nikula };
97dccf7369SJarkko Nikula 
98dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */
99dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = {
100dccf7369SJarkko Nikula 	{	/* LPSS_LPT_SSP */
101dccf7369SJarkko Nikula 		.offset = 0x800,
102dccf7369SJarkko Nikula 		.reg_general = 0x08,
103dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
104dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1058b136baaSJarkko Nikula 		.reg_capabilities = -1,
106dccf7369SJarkko Nikula 		.rx_threshold = 64,
107dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
108dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
109dccf7369SJarkko Nikula 	},
110dccf7369SJarkko Nikula 	{	/* LPSS_BYT_SSP */
111dccf7369SJarkko Nikula 		.offset = 0x400,
112dccf7369SJarkko Nikula 		.reg_general = 0x08,
113dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
114dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1158b136baaSJarkko Nikula 		.reg_capabilities = -1,
116dccf7369SJarkko Nikula 		.rx_threshold = 64,
117dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
118dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
119dccf7369SJarkko Nikula 	},
12030f3a6abSMika Westerberg 	{	/* LPSS_BSW_SSP */
12130f3a6abSMika Westerberg 		.offset = 0x400,
12230f3a6abSMika Westerberg 		.reg_general = 0x08,
12330f3a6abSMika Westerberg 		.reg_ssp = 0x0c,
12430f3a6abSMika Westerberg 		.reg_cs_ctrl = 0x18,
12530f3a6abSMika Westerberg 		.reg_capabilities = -1,
12630f3a6abSMika Westerberg 		.rx_threshold = 64,
12730f3a6abSMika Westerberg 		.tx_threshold_lo = 160,
12830f3a6abSMika Westerberg 		.tx_threshold_hi = 224,
12930f3a6abSMika Westerberg 		.cs_sel_shift = 2,
13030f3a6abSMika Westerberg 		.cs_sel_mask = 1 << 2,
13130f3a6abSMika Westerberg 		.cs_num = 2,
13230f3a6abSMika Westerberg 	},
13334cadd9cSJarkko Nikula 	{	/* LPSS_SPT_SSP */
13434cadd9cSJarkko Nikula 		.offset = 0x200,
13534cadd9cSJarkko Nikula 		.reg_general = -1,
13634cadd9cSJarkko Nikula 		.reg_ssp = 0x20,
13734cadd9cSJarkko Nikula 		.reg_cs_ctrl = 0x24,
13866ec246eSJarkko Nikula 		.reg_capabilities = -1,
13934cadd9cSJarkko Nikula 		.rx_threshold = 1,
14034cadd9cSJarkko Nikula 		.tx_threshold_lo = 32,
14134cadd9cSJarkko Nikula 		.tx_threshold_hi = 56,
14234cadd9cSJarkko Nikula 	},
143b7c08cf8SJarkko Nikula 	{	/* LPSS_BXT_SSP */
144b7c08cf8SJarkko Nikula 		.offset = 0x200,
145b7c08cf8SJarkko Nikula 		.reg_general = -1,
146b7c08cf8SJarkko Nikula 		.reg_ssp = 0x20,
147b7c08cf8SJarkko Nikula 		.reg_cs_ctrl = 0x24,
148b7c08cf8SJarkko Nikula 		.reg_capabilities = 0xfc,
149b7c08cf8SJarkko Nikula 		.rx_threshold = 1,
150b7c08cf8SJarkko Nikula 		.tx_threshold_lo = 16,
151b7c08cf8SJarkko Nikula 		.tx_threshold_hi = 48,
152c1e4a53cSMika Westerberg 		.cs_sel_shift = 8,
153c1e4a53cSMika Westerberg 		.cs_sel_mask = 3 << 8,
154b7c08cf8SJarkko Nikula 	},
155fc0b2accSJarkko Nikula 	{	/* LPSS_CNL_SSP */
156fc0b2accSJarkko Nikula 		.offset = 0x200,
157fc0b2accSJarkko Nikula 		.reg_general = -1,
158fc0b2accSJarkko Nikula 		.reg_ssp = 0x20,
159fc0b2accSJarkko Nikula 		.reg_cs_ctrl = 0x24,
160fc0b2accSJarkko Nikula 		.reg_capabilities = 0xfc,
161fc0b2accSJarkko Nikula 		.rx_threshold = 1,
162fc0b2accSJarkko Nikula 		.tx_threshold_lo = 32,
163fc0b2accSJarkko Nikula 		.tx_threshold_hi = 56,
164fc0b2accSJarkko Nikula 		.cs_sel_shift = 8,
165fc0b2accSJarkko Nikula 		.cs_sel_mask = 3 << 8,
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);
393a0d2642eSMika Westerberg }
394a0d2642eSMika Westerberg 
395d5898e19SJarkko Nikula static void cs_assert(struct spi_device *spi)
396ca632f55SGrant Likely {
397d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
398d5898e19SJarkko Nikula 	struct driver_data *drv_data =
399d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
400ca632f55SGrant Likely 
401ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP) {
40296579a4eSJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, chip->frm);
403ca632f55SGrant Likely 		return;
404ca632f55SGrant Likely 	}
405ca632f55SGrant Likely 
406ca632f55SGrant Likely 	if (chip->cs_control) {
407ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_ASSERT);
408ca632f55SGrant Likely 		return;
409ca632f55SGrant Likely 	}
410ca632f55SGrant Likely 
411c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
412c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
413a0d2642eSMika Westerberg 		return;
414a0d2642eSMika Westerberg 	}
415a0d2642eSMika Westerberg 
4167566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
417d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, true);
418ca632f55SGrant Likely }
419ca632f55SGrant Likely 
420d5898e19SJarkko Nikula static void cs_deassert(struct spi_device *spi)
421ca632f55SGrant Likely {
422d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
423d5898e19SJarkko Nikula 	struct driver_data *drv_data =
424d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
425104e51afSJarkko Nikula 	unsigned long timeout;
426ca632f55SGrant Likely 
427ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
428ca632f55SGrant Likely 		return;
429ca632f55SGrant Likely 
430104e51afSJarkko Nikula 	/* Wait until SSP becomes idle before deasserting the CS */
431104e51afSJarkko Nikula 	timeout = jiffies + msecs_to_jiffies(10);
432104e51afSJarkko Nikula 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
433104e51afSJarkko Nikula 	       !time_after(jiffies, timeout))
434104e51afSJarkko Nikula 		cpu_relax();
435104e51afSJarkko Nikula 
436ca632f55SGrant Likely 	if (chip->cs_control) {
437ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_DEASSERT);
438ca632f55SGrant Likely 		return;
439ca632f55SGrant Likely 	}
440ca632f55SGrant Likely 
441c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
442c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
443a0d2642eSMika Westerberg 		return;
444a0d2642eSMika Westerberg 	}
445a0d2642eSMika Westerberg 
4467566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
447d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, false);
448d5898e19SJarkko Nikula }
449d5898e19SJarkko Nikula 
450d5898e19SJarkko Nikula static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
451d5898e19SJarkko Nikula {
452d5898e19SJarkko Nikula 	if (level)
453d5898e19SJarkko Nikula 		cs_deassert(spi);
454d5898e19SJarkko Nikula 	else
455d5898e19SJarkko Nikula 		cs_assert(spi);
456ca632f55SGrant Likely }
457ca632f55SGrant Likely 
458cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
459ca632f55SGrant Likely {
460ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
461ca632f55SGrant Likely 
462ca632f55SGrant Likely 	do {
463c039dd27SJarkko Nikula 		while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
464c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
465c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
466ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
467ca632f55SGrant Likely 
468ca632f55SGrant Likely 	return limit;
469ca632f55SGrant Likely }
470ca632f55SGrant Likely 
471ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
472ca632f55SGrant Likely {
473ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
474ca632f55SGrant Likely 
4754fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
476ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
477ca632f55SGrant Likely 		return 0;
478ca632f55SGrant Likely 
479c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
480ca632f55SGrant Likely 	drv_data->tx += n_bytes;
481ca632f55SGrant Likely 
482ca632f55SGrant Likely 	return 1;
483ca632f55SGrant Likely }
484ca632f55SGrant Likely 
485ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
486ca632f55SGrant Likely {
487ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
488ca632f55SGrant Likely 
489c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
490ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
491c039dd27SJarkko Nikula 		pxa2xx_spi_read(drv_data, SSDR);
492ca632f55SGrant Likely 		drv_data->rx += n_bytes;
493ca632f55SGrant Likely 	}
494ca632f55SGrant Likely 
495ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
496ca632f55SGrant Likely }
497ca632f55SGrant Likely 
498ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data)
499ca632f55SGrant Likely {
5004fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
501ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
502ca632f55SGrant Likely 		return 0;
503ca632f55SGrant Likely 
504c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
505ca632f55SGrant Likely 	++drv_data->tx;
506ca632f55SGrant Likely 
507ca632f55SGrant Likely 	return 1;
508ca632f55SGrant Likely }
509ca632f55SGrant Likely 
510ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data)
511ca632f55SGrant Likely {
512c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
513ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
514c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
515ca632f55SGrant Likely 		++drv_data->rx;
516ca632f55SGrant Likely 	}
517ca632f55SGrant Likely 
518ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
519ca632f55SGrant Likely }
520ca632f55SGrant Likely 
521ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
522ca632f55SGrant Likely {
5234fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
524ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
525ca632f55SGrant Likely 		return 0;
526ca632f55SGrant Likely 
527c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
528ca632f55SGrant Likely 	drv_data->tx += 2;
529ca632f55SGrant Likely 
530ca632f55SGrant Likely 	return 1;
531ca632f55SGrant Likely }
532ca632f55SGrant Likely 
533ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
534ca632f55SGrant Likely {
535c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
536ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
537c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
538ca632f55SGrant Likely 		drv_data->rx += 2;
539ca632f55SGrant Likely 	}
540ca632f55SGrant Likely 
541ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
542ca632f55SGrant Likely }
543ca632f55SGrant Likely 
544ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
545ca632f55SGrant Likely {
5464fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
547ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
548ca632f55SGrant Likely 		return 0;
549ca632f55SGrant Likely 
550c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
551ca632f55SGrant Likely 	drv_data->tx += 4;
552ca632f55SGrant Likely 
553ca632f55SGrant Likely 	return 1;
554ca632f55SGrant Likely }
555ca632f55SGrant Likely 
556ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
557ca632f55SGrant Likely {
558c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
559ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
560c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
561ca632f55SGrant Likely 		drv_data->rx += 4;
562ca632f55SGrant Likely 	}
563ca632f55SGrant Likely 
564ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
565ca632f55SGrant Likely }
566ca632f55SGrant Likely 
567ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
568ca632f55SGrant Likely {
56996579a4eSJarkko Nikula 	struct chip_data *chip =
57051eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
571ca632f55SGrant Likely 	u32 sccr1_reg;
572ca632f55SGrant Likely 
573c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
574152bc19eSAndy Shevchenko 	switch (drv_data->ssp_type) {
575152bc19eSAndy Shevchenko 	case QUARK_X1000_SSP:
576152bc19eSAndy Shevchenko 		sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
577152bc19eSAndy Shevchenko 		break;
5787c7289a4SAndy Shevchenko 	case CE4100_SSP:
5797c7289a4SAndy Shevchenko 		sccr1_reg &= ~CE4100_SSCR1_RFT;
5807c7289a4SAndy Shevchenko 		break;
581152bc19eSAndy Shevchenko 	default:
582ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_RFT;
583152bc19eSAndy Shevchenko 		break;
584152bc19eSAndy Shevchenko 	}
585ca632f55SGrant Likely 	sccr1_reg |= chip->threshold;
586c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
587ca632f55SGrant Likely }
588ca632f55SGrant Likely 
589ca632f55SGrant Likely static void int_error_stop(struct driver_data *drv_data, const char* msg)
590ca632f55SGrant Likely {
591ca632f55SGrant Likely 	/* Stop and reset SSP */
592ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
593ca632f55SGrant Likely 	reset_sccr1(drv_data);
594ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
595c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
596cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
597c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0,
598c039dd27SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
599ca632f55SGrant Likely 
600ca632f55SGrant Likely 	dev_err(&drv_data->pdev->dev, "%s\n", msg);
601ca632f55SGrant Likely 
60251eea52dSLubomir Rintel 	drv_data->controller->cur_msg->status = -EIO;
60351eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
604ca632f55SGrant Likely }
605ca632f55SGrant Likely 
606ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
607ca632f55SGrant Likely {
60807550df0SJarkko Nikula 	/* Clear and disable interrupts */
609ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
610ca632f55SGrant Likely 	reset_sccr1(drv_data);
611ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
612c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
613ca632f55SGrant Likely 
61451eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
615ca632f55SGrant Likely }
616ca632f55SGrant Likely 
617ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
618ca632f55SGrant Likely {
619c039dd27SJarkko Nikula 	u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
620ca632f55SGrant Likely 		       drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
621ca632f55SGrant Likely 
622c039dd27SJarkko Nikula 	u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
623ca632f55SGrant Likely 
624ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
625ca632f55SGrant Likely 		int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
626ca632f55SGrant Likely 		return IRQ_HANDLED;
627ca632f55SGrant Likely 	}
628ca632f55SGrant Likely 
629ec93cb6fSLubomir Rintel 	if (irq_status & SSSR_TUR) {
630ec93cb6fSLubomir Rintel 		int_error_stop(drv_data, "interrupt_transfer: fifo underrun");
631ec93cb6fSLubomir Rintel 		return IRQ_HANDLED;
632ec93cb6fSLubomir Rintel 	}
633ec93cb6fSLubomir Rintel 
634ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
635c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
636ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
637ca632f55SGrant Likely 			int_transfer_complete(drv_data);
638ca632f55SGrant Likely 			return IRQ_HANDLED;
639ca632f55SGrant Likely 		}
640ca632f55SGrant Likely 	}
641ca632f55SGrant Likely 
642ca632f55SGrant Likely 	/* Drain rx fifo, Fill tx fifo and prevent overruns */
643ca632f55SGrant Likely 	do {
644ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
645ca632f55SGrant Likely 			int_transfer_complete(drv_data);
646ca632f55SGrant Likely 			return IRQ_HANDLED;
647ca632f55SGrant Likely 		}
648ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
649ca632f55SGrant Likely 
650ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
651ca632f55SGrant Likely 		int_transfer_complete(drv_data);
652ca632f55SGrant Likely 		return IRQ_HANDLED;
653ca632f55SGrant Likely 	}
654ca632f55SGrant Likely 
655ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
656ca632f55SGrant Likely 		u32 bytes_left;
657ca632f55SGrant Likely 		u32 sccr1_reg;
658ca632f55SGrant Likely 
659c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
660ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
661ca632f55SGrant Likely 
662ca632f55SGrant Likely 		/*
663ca632f55SGrant Likely 		 * PXA25x_SSP has no timeout, set up rx threshould for the
664ca632f55SGrant Likely 		 * remaining RX bytes.
665ca632f55SGrant Likely 		 */
666ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
6674fdb2424SWeike Chen 			u32 rx_thre;
668ca632f55SGrant Likely 
6694fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
670ca632f55SGrant Likely 
671ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
672ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
673ca632f55SGrant Likely 			case 4:
6742c183376SGustavo A. R. Silva 				bytes_left >>= 2;
6752c183376SGustavo A. R. Silva 				break;
676ca632f55SGrant Likely 			case 2:
677ca632f55SGrant Likely 				bytes_left >>= 1;
6782c183376SGustavo A. R. Silva 				break;
679ca632f55SGrant Likely 			}
680ca632f55SGrant Likely 
6814fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
6824fdb2424SWeike Chen 			if (rx_thre > bytes_left)
6834fdb2424SWeike Chen 				rx_thre = bytes_left;
684ca632f55SGrant Likely 
6854fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
686ca632f55SGrant Likely 		}
687c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
688ca632f55SGrant Likely 	}
689ca632f55SGrant Likely 
690ca632f55SGrant Likely 	/* We did something */
691ca632f55SGrant Likely 	return IRQ_HANDLED;
692ca632f55SGrant Likely }
693ca632f55SGrant Likely 
694b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data)
695b0312482SJan Kiszka {
696b0312482SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR0,
697b0312482SJan Kiszka 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
698b0312482SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1,
699b0312482SJan Kiszka 			 pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
700b0312482SJan Kiszka 	if (!pxa25x_ssp_comp(drv_data))
701b0312482SJan Kiszka 		pxa2xx_spi_write(drv_data, SSTO, 0);
702b0312482SJan Kiszka 	write_SSSR_CS(drv_data, drv_data->clear_sr);
703b0312482SJan Kiszka 
704b0312482SJan Kiszka 	dev_err(&drv_data->pdev->dev,
705b0312482SJan Kiszka 		"bad message state in interrupt handler\n");
706b0312482SJan Kiszka }
707b0312482SJan Kiszka 
708ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
709ca632f55SGrant Likely {
710ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
7117d94a505SMika Westerberg 	u32 sccr1_reg;
712ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
713ca632f55SGrant Likely 	u32 status;
714ca632f55SGrant Likely 
7157d94a505SMika Westerberg 	/*
7167d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
7177d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
7187d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
7197d94a505SMika Westerberg 	 * interrupt is enabled).
7207d94a505SMika Westerberg 	 */
7217d94a505SMika Westerberg 	if (pm_runtime_suspended(&drv_data->pdev->dev))
7227d94a505SMika Westerberg 		return IRQ_NONE;
7237d94a505SMika Westerberg 
724269e4a41SMika Westerberg 	/*
725269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
726269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
727269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
728269e4a41SMika Westerberg 	 * powered off.
729269e4a41SMika Westerberg 	 */
730c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
731269e4a41SMika Westerberg 	if (status == ~0)
732269e4a41SMika Westerberg 		return IRQ_NONE;
733269e4a41SMika Westerberg 
734c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
735ca632f55SGrant Likely 
736ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
737ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
738ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
739ca632f55SGrant Likely 
74002bc933eSTan, Jui Nee 	/* Ignore RX timeout interrupt if it is disabled */
74102bc933eSTan, Jui Nee 	if (!(sccr1_reg & SSCR1_TINTE))
74202bc933eSTan, Jui Nee 		mask &= ~SSSR_TINT;
74302bc933eSTan, Jui Nee 
744ca632f55SGrant Likely 	if (!(status & mask))
745ca632f55SGrant Likely 		return IRQ_NONE;
746ca632f55SGrant Likely 
747e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
748e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
749e51e9b93SJan Kiszka 
75051eea52dSLubomir Rintel 	if (!drv_data->controller->cur_msg) {
751b0312482SJan Kiszka 		handle_bad_msg(drv_data);
752ca632f55SGrant Likely 		/* Never fail */
753ca632f55SGrant Likely 		return IRQ_HANDLED;
754ca632f55SGrant Likely 	}
755ca632f55SGrant Likely 
756ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
757ca632f55SGrant Likely }
758ca632f55SGrant Likely 
759e5262d05SWeike Chen /*
7609df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
7619df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
7629df461ecSAndy Shevchenko  *
7639df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
7649df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
7659df461ecSAndy Shevchenko  *
7669df461ecSAndy Shevchenko  * Fsys = 200MHz
7679df461ecSAndy Shevchenko  *
7689df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
7699df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
7709df461ecSAndy Shevchenko  *
7719df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
7729df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
7739df461ecSAndy Shevchenko  *
7749df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
7759df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
7769df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
7779df461ecSAndy Shevchenko  *       k = [1, 256]
7789df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
7799df461ecSAndy Shevchenko  *
7809df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
7819df461ecSAndy Shevchenko  * are:
7829df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
7839df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
7849df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
7859df461ecSAndy Shevchenko  *
7869df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
7879df461ecSAndy Shevchenko  *
7889df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
7899df461ecSAndy Shevchenko  * to the asked baud rate.
790e5262d05SWeike Chen  */
7919df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
792e5262d05SWeike Chen {
7939df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
7949df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
7959df461ecSAndy Shevchenko 						   see (2) */
7969df461ecSAndy Shevchenko 						/* case 3 */
7979df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
7989df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
7999df461ecSAndy Shevchenko 	unsigned long scale;
8009df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
8019df461ecSAndy Shevchenko 	long r, r1, r2;
8029df461ecSAndy Shevchenko 	u32 mul;
803e5262d05SWeike Chen 
8049df461ecSAndy Shevchenko 	/* Case 1 */
8059df461ecSAndy Shevchenko 
8069df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
8079df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
8089df461ecSAndy Shevchenko 
8099df461ecSAndy Shevchenko 	/* Calculate initial quot */
8103ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
8119df461ecSAndy Shevchenko 
8129df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
8139df461ecSAndy Shevchenko 	if (q1 > 256) {
8149df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
8159df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
8169df461ecSAndy Shevchenko 		if (scale > 9) {
8179df461ecSAndy Shevchenko 			q1 >>= scale - 9;
8189df461ecSAndy Shevchenko 			mul >>= scale - 9;
8199df461ecSAndy Shevchenko 		}
8209df461ecSAndy Shevchenko 
8219df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
8229df461ecSAndy Shevchenko 		q1 += q1 & 1;
8239df461ecSAndy Shevchenko 	}
8249df461ecSAndy Shevchenko 
8259df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
8269df461ecSAndy Shevchenko 	scale = __ffs(q1);
8279df461ecSAndy Shevchenko 	q1 >>= scale;
8289df461ecSAndy Shevchenko 	mul >>= scale;
8299df461ecSAndy Shevchenko 
8309df461ecSAndy Shevchenko 	/* Get the remainder */
8319df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
8329df461ecSAndy Shevchenko 
8339df461ecSAndy Shevchenko 	/* Case 2 */
8349df461ecSAndy Shevchenko 
8353ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
8369df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
8379df461ecSAndy Shevchenko 
8389df461ecSAndy Shevchenko 	/*
8399df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
8409df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
8419df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
8429df461ecSAndy Shevchenko 	 */
8439df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
8449df461ecSAndy Shevchenko 		/* case 1 is better */
8459df461ecSAndy Shevchenko 		r = r1;
8469df461ecSAndy Shevchenko 		q = q1;
8479df461ecSAndy Shevchenko 	} else {
8489df461ecSAndy Shevchenko 		/* case 2 is better */
8499df461ecSAndy Shevchenko 		r = r2;
8509df461ecSAndy Shevchenko 		q = q2;
8519df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
8529df461ecSAndy Shevchenko 	}
8539df461ecSAndy Shevchenko 
8543ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
8559df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
8569df461ecSAndy Shevchenko 		u64 fssp;
8579df461ecSAndy Shevchenko 		u32 m;
8589df461ecSAndy Shevchenko 
8599df461ecSAndy Shevchenko 		/* Calculate initial quot */
8603ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
8619df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
8629df461ecSAndy Shevchenko 
8639df461ecSAndy Shevchenko 		/* Get the remainder */
8649df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
8659df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
8669df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
8679df461ecSAndy Shevchenko 
8689df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
8699df461ecSAndy Shevchenko 		if (r1 < r) {
8709df461ecSAndy Shevchenko 			/* case 3 is better */
8719df461ecSAndy Shevchenko 			q = 1;
8729df461ecSAndy Shevchenko 			mul = m;
873e5262d05SWeike Chen 		}
874e5262d05SWeike Chen 	}
875e5262d05SWeike Chen 
8769df461ecSAndy Shevchenko 	*dds = mul;
8779df461ecSAndy Shevchenko 	return q - 1;
878e5262d05SWeike Chen }
879e5262d05SWeike Chen 
8803343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
881ca632f55SGrant Likely {
88251eea52dSLubomir Rintel 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
8833343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
8843343b7a6SMika Westerberg 
8853343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
886ca632f55SGrant Likely 
887ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
888025ffe88SAndy Shevchenko 		return (ssp_clk / (2 * rate) - 1) & 0xff;
889ca632f55SGrant Likely 	else
890025ffe88SAndy Shevchenko 		return (ssp_clk / rate - 1) & 0xfff;
891ca632f55SGrant Likely }
892ca632f55SGrant Likely 
893e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
894d2c2f6a4SAndy Shevchenko 					   int rate)
895e5262d05SWeike Chen {
89696579a4eSJarkko Nikula 	struct chip_data *chip =
89751eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
898025ffe88SAndy Shevchenko 	unsigned int clk_div;
899e5262d05SWeike Chen 
900e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
901e5262d05SWeike Chen 	case QUARK_X1000_SSP:
9029df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
903eecacf73SDan Carpenter 		break;
904e5262d05SWeike Chen 	default:
905025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
906eecacf73SDan Carpenter 		break;
907e5262d05SWeike Chen 	}
908025ffe88SAndy Shevchenko 	return clk_div << 8;
909e5262d05SWeike Chen }
910e5262d05SWeike Chen 
91151eea52dSLubomir Rintel static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
912b6ced294SJarkko Nikula 			       struct spi_device *spi,
913b6ced294SJarkko Nikula 			       struct spi_transfer *xfer)
914b6ced294SJarkko Nikula {
915b6ced294SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
916b6ced294SJarkko Nikula 
917b6ced294SJarkko Nikula 	return chip->enable_dma &&
918b6ced294SJarkko Nikula 	       xfer->len <= MAX_DMA_LEN &&
919b6ced294SJarkko Nikula 	       xfer->len >= chip->dma_burst_size;
920b6ced294SJarkko Nikula }
921b6ced294SJarkko Nikula 
92251eea52dSLubomir Rintel static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
923d5898e19SJarkko Nikula 				   struct spi_device *spi,
924d5898e19SJarkko Nikula 				   struct spi_transfer *transfer)
925ca632f55SGrant Likely {
92651eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
92751eea52dSLubomir Rintel 	struct spi_message *message = controller->cur_msg;
928*20f4c379SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
92996579a4eSJarkko Nikula 	u32 dma_thresh = chip->dma_threshold;
93096579a4eSJarkko Nikula 	u32 dma_burst = chip->dma_burst_size;
93196579a4eSJarkko Nikula 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
932bffc967eSJarkko Nikula 	u32 clk_div;
933bffc967eSJarkko Nikula 	u8 bits;
934bffc967eSJarkko Nikula 	u32 speed;
935ca632f55SGrant Likely 	u32 cr0;
936ca632f55SGrant Likely 	u32 cr1;
9377d1f1bf6SAndy Shevchenko 	int err;
938b6ced294SJarkko Nikula 	int dma_mapped;
939ca632f55SGrant Likely 
940cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
941b6ced294SJarkko Nikula 	if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
942ca632f55SGrant Likely 
943ca632f55SGrant Likely 		/* reject already-mapped transfers; PIO won't always work */
944ca632f55SGrant Likely 		if (message->is_dma_mapped
945ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
946ca632f55SGrant Likely 			dev_err(&drv_data->pdev->dev,
9478ae55af3SJarkko Nikula 				"Mapped transfer length of %u is greater than %d\n",
948ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
949d5898e19SJarkko Nikula 			return -EINVAL;
950ca632f55SGrant Likely 		}
951ca632f55SGrant Likely 
952ca632f55SGrant Likely 		/* warn ... we force this to PIO mode */
953*20f4c379SJarkko Nikula 		dev_warn_ratelimited(&spi->dev,
9548ae55af3SJarkko Nikula 				     "DMA disabled for transfer length %ld greater than %d\n",
955d5898e19SJarkko Nikula 				     (long)transfer->len, MAX_DMA_LEN);
956ca632f55SGrant Likely 	}
957ca632f55SGrant Likely 
958ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
959cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
9608ae55af3SJarkko Nikula 		dev_err(&drv_data->pdev->dev, "Flush failed\n");
961d5898e19SJarkko Nikula 		return -EIO;
962ca632f55SGrant Likely 	}
963ca632f55SGrant Likely 	drv_data->n_bytes = chip->n_bytes;
964ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
965ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
966ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
967ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
968ca632f55SGrant Likely 	drv_data->write = drv_data->tx ? chip->write : null_writer;
969ca632f55SGrant Likely 	drv_data->read = drv_data->rx ? chip->read : null_reader;
970ca632f55SGrant Likely 
971ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
972ca632f55SGrant Likely 	bits = transfer->bits_per_word;
973ca632f55SGrant Likely 	speed = transfer->speed_hz;
974ca632f55SGrant Likely 
975d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
976ca632f55SGrant Likely 
977ca632f55SGrant Likely 	if (bits <= 8) {
978ca632f55SGrant Likely 		drv_data->n_bytes = 1;
979ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
980ca632f55SGrant Likely 					u8_reader : null_reader;
981ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
982ca632f55SGrant Likely 					u8_writer : null_writer;
983ca632f55SGrant Likely 	} else if (bits <= 16) {
984ca632f55SGrant Likely 		drv_data->n_bytes = 2;
985ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
986ca632f55SGrant Likely 					u16_reader : null_reader;
987ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
988ca632f55SGrant Likely 					u16_writer : null_writer;
989ca632f55SGrant Likely 	} else if (bits <= 32) {
990ca632f55SGrant Likely 		drv_data->n_bytes = 4;
991ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
992ca632f55SGrant Likely 					u32_reader : null_reader;
993ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
994ca632f55SGrant Likely 					u32_writer : null_writer;
995ca632f55SGrant Likely 	}
996196b0e2cSJarkko Nikula 	/*
997196b0e2cSJarkko Nikula 	 * if bits/word is changed in dma mode, then must check the
998196b0e2cSJarkko Nikula 	 * thresholds and burst also
999196b0e2cSJarkko Nikula 	 */
1000ca632f55SGrant Likely 	if (chip->enable_dma) {
1001cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
1002*20f4c379SJarkko Nikula 						spi,
1003ca632f55SGrant Likely 						bits, &dma_burst,
1004ca632f55SGrant Likely 						&dma_thresh))
1005*20f4c379SJarkko Nikula 			dev_warn_ratelimited(&spi->dev,
10068ae55af3SJarkko Nikula 					     "DMA burst size reduced to match bits_per_word\n");
1007ca632f55SGrant Likely 	}
1008ca632f55SGrant Likely 
100951eea52dSLubomir Rintel 	dma_mapped = controller->can_dma &&
1010*20f4c379SJarkko Nikula 		     controller->can_dma(controller, spi, transfer) &&
101151eea52dSLubomir Rintel 		     controller->cur_msg_mapped;
1012b6ced294SJarkko Nikula 	if (dma_mapped) {
1013ca632f55SGrant Likely 
1014ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
1015cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1016ca632f55SGrant Likely 
1017d5898e19SJarkko Nikula 		err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1018d5898e19SJarkko Nikula 		if (err)
1019d5898e19SJarkko Nikula 			return err;
1020ca632f55SGrant Likely 
1021ca632f55SGrant Likely 		/* Clear status and start DMA engine */
1022ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1023c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1024cd7bed00SMika Westerberg 
1025cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
1026ca632f55SGrant Likely 	} else {
1027ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
1028ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
1029ca632f55SGrant Likely 
1030ca632f55SGrant Likely 		/* Clear status  */
1031ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1032ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1033ca632f55SGrant Likely 	}
1034ca632f55SGrant Likely 
1035ee03672dSJarkko Nikula 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1036ee03672dSJarkko Nikula 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1037ee03672dSJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
1038*20f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
103951eea52dSLubomir Rintel 			controller->max_speed_hz
1040ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1041b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1042ee03672dSJarkko Nikula 	else
1043*20f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
104451eea52dSLubomir Rintel 			controller->max_speed_hz / 2
1045ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1046b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1047ee03672dSJarkko Nikula 
1048a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
1049c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
1050c039dd27SJarkko Nikula 		    != chip->lpss_rx_threshold)
1051c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSIRF,
1052c039dd27SJarkko Nikula 					 chip->lpss_rx_threshold);
1053c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
1054c039dd27SJarkko Nikula 		    != chip->lpss_tx_threshold)
1055c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSITF,
1056c039dd27SJarkko Nikula 					 chip->lpss_tx_threshold);
1057a0d2642eSMika Westerberg 	}
1058a0d2642eSMika Westerberg 
1059e5262d05SWeike Chen 	if (is_quark_x1000_ssp(drv_data) &&
1060c039dd27SJarkko Nikula 	    (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
1061c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
1062e5262d05SWeike Chen 
1063ca632f55SGrant Likely 	/* see if we need to reload the config registers */
1064c039dd27SJarkko Nikula 	if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1065c039dd27SJarkko Nikula 	    || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1066c039dd27SJarkko Nikula 	    != (cr1 & change_mask)) {
1067ca632f55SGrant Likely 		/* stop the SSP, and update the other bits */
1068c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
1069ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1070c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1071ca632f55SGrant Likely 		/* first set CR1 without interrupt and service enables */
1072c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
1073ca632f55SGrant Likely 		/* restart the SSP */
1074c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0);
1075ca632f55SGrant Likely 
1076ca632f55SGrant Likely 	} else {
1077ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1078c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1079ca632f55SGrant Likely 	}
1080ca632f55SGrant Likely 
108182391856SLubomir Rintel 	if (drv_data->ssp_type == MMP2_SSP) {
108282391856SLubomir Rintel 		u8 tx_level = (pxa2xx_spi_read(drv_data, SSSR)
108382391856SLubomir Rintel 					& SSSR_TFL_MASK) >> 8;
108482391856SLubomir Rintel 
108582391856SLubomir Rintel 		if (tx_level) {
108682391856SLubomir Rintel 			/* On MMP2, flipping SSE doesn't to empty TXFIFO. */
108782391856SLubomir Rintel 			dev_warn(&spi->dev, "%d bytes of garbage in TXFIFO!\n",
108882391856SLubomir Rintel 								tx_level);
108982391856SLubomir Rintel 			if (tx_level > transfer->len)
109082391856SLubomir Rintel 				tx_level = transfer->len;
109182391856SLubomir Rintel 			drv_data->tx += tx_level;
109282391856SLubomir Rintel 		}
109382391856SLubomir Rintel 	}
109482391856SLubomir Rintel 
109551eea52dSLubomir Rintel 	if (spi_controller_is_slave(controller)) {
1096ec93cb6fSLubomir Rintel 		while (drv_data->write(drv_data))
1097ec93cb6fSLubomir Rintel 			;
109877d33897SLubomir Rintel 		if (drv_data->gpiod_ready) {
109977d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 1);
110077d33897SLubomir Rintel 			udelay(1);
110177d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 0);
110277d33897SLubomir Rintel 		}
1103ec93cb6fSLubomir Rintel 	}
1104ec93cb6fSLubomir Rintel 
1105d5898e19SJarkko Nikula 	/*
1106d5898e19SJarkko Nikula 	 * Release the data by enabling service requests and interrupts,
1107d5898e19SJarkko Nikula 	 * without changing any mode bits
1108d5898e19SJarkko Nikula 	 */
1109c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1110d5898e19SJarkko Nikula 
1111d5898e19SJarkko Nikula 	return 1;
1112ca632f55SGrant Likely }
1113ca632f55SGrant Likely 
111451eea52dSLubomir Rintel static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
1115ec93cb6fSLubomir Rintel {
111651eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1117ec93cb6fSLubomir Rintel 
1118ec93cb6fSLubomir Rintel 	/* Stop and reset SSP */
1119ec93cb6fSLubomir Rintel 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1120ec93cb6fSLubomir Rintel 	reset_sccr1(drv_data);
1121ec93cb6fSLubomir Rintel 	if (!pxa25x_ssp_comp(drv_data))
1122ec93cb6fSLubomir Rintel 		pxa2xx_spi_write(drv_data, SSTO, 0);
1123ec93cb6fSLubomir Rintel 	pxa2xx_spi_flush(drv_data);
1124ec93cb6fSLubomir Rintel 	pxa2xx_spi_write(drv_data, SSCR0,
1125ec93cb6fSLubomir Rintel 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
1126ec93cb6fSLubomir Rintel 
1127ec93cb6fSLubomir Rintel 	dev_dbg(&drv_data->pdev->dev, "transfer aborted\n");
1128ec93cb6fSLubomir Rintel 
112951eea52dSLubomir Rintel 	drv_data->controller->cur_msg->status = -EINTR;
113051eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
1131ec93cb6fSLubomir Rintel 
1132ec93cb6fSLubomir Rintel 	return 0;
1133ec93cb6fSLubomir Rintel }
1134ec93cb6fSLubomir Rintel 
113551eea52dSLubomir Rintel static void pxa2xx_spi_handle_err(struct spi_controller *controller,
11367f86bde9SMika Westerberg 				 struct spi_message *msg)
1137ca632f55SGrant Likely {
113851eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1139ca632f55SGrant Likely 
1140d5898e19SJarkko Nikula 	/* Disable the SSP */
1141d5898e19SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0,
1142d5898e19SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
1143d5898e19SJarkko Nikula 	/* Clear and disable interrupts and service requests */
1144d5898e19SJarkko Nikula 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1145d5898e19SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1,
1146d5898e19SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR1)
1147d5898e19SJarkko Nikula 			 & ~(drv_data->int_cr1 | drv_data->dma_cr1));
1148d5898e19SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
1149d5898e19SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1150ca632f55SGrant Likely 
1151d5898e19SJarkko Nikula 	/*
1152d5898e19SJarkko Nikula 	 * Stop the DMA if running. Note DMA callback handler may have unset
1153d5898e19SJarkko Nikula 	 * the dma_running already, which is fine as stopping is not needed
1154d5898e19SJarkko Nikula 	 * then but we shouldn't rely this flag for anything else than
1155d5898e19SJarkko Nikula 	 * stopping. For instance to differentiate between PIO and DMA
1156d5898e19SJarkko Nikula 	 * transfers.
1157d5898e19SJarkko Nikula 	 */
1158d5898e19SJarkko Nikula 	if (atomic_read(&drv_data->dma_running))
1159d5898e19SJarkko Nikula 		pxa2xx_spi_dma_stop(drv_data);
1160ca632f55SGrant Likely }
1161ca632f55SGrant Likely 
116251eea52dSLubomir Rintel static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
11637d94a505SMika Westerberg {
116451eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
11657d94a505SMika Westerberg 
11667d94a505SMika Westerberg 	/* Disable the SSP now */
1167c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0,
1168c039dd27SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
11697d94a505SMika Westerberg 
11707d94a505SMika Westerberg 	return 0;
11717d94a505SMika Westerberg }
11727d94a505SMika Westerberg 
1173ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1174ca632f55SGrant Likely 		    struct pxa2xx_spi_chip *chip_info)
1175ca632f55SGrant Likely {
11763cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
11773cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1178c18d925fSJan Kiszka 	struct gpio_desc *gpiod;
1179ca632f55SGrant Likely 	int err = 0;
1180ca632f55SGrant Likely 
118199f499cdSMika Westerberg 	if (chip == NULL)
118299f499cdSMika Westerberg 		return 0;
118399f499cdSMika Westerberg 
11846ac5a435SAndy Shevchenko 	if (drv_data->cs_gpiods) {
11856ac5a435SAndy Shevchenko 		gpiod = drv_data->cs_gpiods[spi->chip_select];
11866ac5a435SAndy Shevchenko 		if (gpiod) {
1187c18d925fSJan Kiszka 			chip->gpiod_cs = gpiod;
118899f499cdSMika Westerberg 			chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
118999f499cdSMika Westerberg 			gpiod_set_value(gpiod, chip->gpio_cs_inverted);
11906ac5a435SAndy Shevchenko 		}
119199f499cdSMika Westerberg 
119299f499cdSMika Westerberg 		return 0;
119399f499cdSMika Westerberg 	}
119499f499cdSMika Westerberg 
119599f499cdSMika Westerberg 	if (chip_info == NULL)
1196ca632f55SGrant Likely 		return 0;
1197ca632f55SGrant Likely 
1198ca632f55SGrant Likely 	/* NOTE: setup() can be called multiple times, possibly with
1199ca632f55SGrant Likely 	 * different chip_info, release previously requested GPIO
1200ca632f55SGrant Likely 	 */
1201c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
1202a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1203c18d925fSJan Kiszka 		chip->gpiod_cs = NULL;
1204c18d925fSJan Kiszka 	}
1205ca632f55SGrant Likely 
1206ca632f55SGrant Likely 	/* If (*cs_control) is provided, ignore GPIO chip select */
1207ca632f55SGrant Likely 	if (chip_info->cs_control) {
1208ca632f55SGrant Likely 		chip->cs_control = chip_info->cs_control;
1209ca632f55SGrant Likely 		return 0;
1210ca632f55SGrant Likely 	}
1211ca632f55SGrant Likely 
1212ca632f55SGrant Likely 	if (gpio_is_valid(chip_info->gpio_cs)) {
1213ca632f55SGrant Likely 		err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1214ca632f55SGrant Likely 		if (err) {
1215f6bd03a7SJarkko Nikula 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1216f6bd03a7SJarkko Nikula 				chip_info->gpio_cs);
1217ca632f55SGrant Likely 			return err;
1218ca632f55SGrant Likely 		}
1219ca632f55SGrant Likely 
1220c18d925fSJan Kiszka 		gpiod = gpio_to_desc(chip_info->gpio_cs);
1221c18d925fSJan Kiszka 		chip->gpiod_cs = gpiod;
1222ca632f55SGrant Likely 		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1223ca632f55SGrant Likely 
1224c18d925fSJan Kiszka 		err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
1225ca632f55SGrant Likely 	}
1226ca632f55SGrant Likely 
1227ca632f55SGrant Likely 	return err;
1228ca632f55SGrant Likely }
1229ca632f55SGrant Likely 
1230ca632f55SGrant Likely static int setup(struct spi_device *spi)
1231ca632f55SGrant Likely {
1232bffc967eSJarkko Nikula 	struct pxa2xx_spi_chip *chip_info;
1233ca632f55SGrant Likely 	struct chip_data *chip;
1234dccf7369SJarkko Nikula 	const struct lpss_config *config;
12353cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12363cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1237a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1238a0d2642eSMika Westerberg 
1239e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1240e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1241e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1242e5262d05SWeike Chen 		tx_hi_thres = 0;
1243e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1244e5262d05SWeike Chen 		break;
12457c7289a4SAndy Shevchenko 	case CE4100_SSP:
12467c7289a4SAndy Shevchenko 		tx_thres = TX_THRESH_CE4100_DFLT;
12477c7289a4SAndy Shevchenko 		tx_hi_thres = 0;
12487c7289a4SAndy Shevchenko 		rx_thres = RX_THRESH_CE4100_DFLT;
12497c7289a4SAndy Shevchenko 		break;
125003fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
125103fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
125230f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
125334cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1254b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1255fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
1256dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1257dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1258dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1259dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1260e5262d05SWeike Chen 		break;
1261e5262d05SWeike Chen 	default:
1262a0d2642eSMika Westerberg 		tx_hi_thres = 0;
126351eea52dSLubomir Rintel 		if (spi_controller_is_slave(drv_data->controller)) {
1264ec93cb6fSLubomir Rintel 			tx_thres = 1;
1265ec93cb6fSLubomir Rintel 			rx_thres = 2;
1266ec93cb6fSLubomir Rintel 		} else {
1267ec93cb6fSLubomir Rintel 			tx_thres = TX_THRESH_DFLT;
1268a0d2642eSMika Westerberg 			rx_thres = RX_THRESH_DFLT;
1269ec93cb6fSLubomir Rintel 		}
1270e5262d05SWeike Chen 		break;
1271a0d2642eSMika Westerberg 	}
1272ca632f55SGrant Likely 
1273ca632f55SGrant Likely 	/* Only alloc on first setup */
1274ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1275ca632f55SGrant Likely 	if (!chip) {
1276ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
12779deae459SJingoo Han 		if (!chip)
1278ca632f55SGrant Likely 			return -ENOMEM;
1279ca632f55SGrant Likely 
1280ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1281ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1282f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1283f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1284ca632f55SGrant Likely 				kfree(chip);
1285ca632f55SGrant Likely 				return -EINVAL;
1286ca632f55SGrant Likely 			}
1287ca632f55SGrant Likely 
1288ca632f55SGrant Likely 			chip->frm = spi->chip_select;
1289c18d925fSJan Kiszka 		}
129051eea52dSLubomir Rintel 		chip->enable_dma = drv_data->controller_info->enable_dma;
1291ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1292ca632f55SGrant Likely 	}
1293ca632f55SGrant Likely 
1294ca632f55SGrant Likely 	/* protocol drivers may change the chip settings, so...
1295ca632f55SGrant Likely 	 * if chip_info exists, use it */
1296ca632f55SGrant Likely 	chip_info = spi->controller_data;
1297ca632f55SGrant Likely 
1298ca632f55SGrant Likely 	/* chip_info isn't always needed */
1299ca632f55SGrant Likely 	chip->cr1 = 0;
1300ca632f55SGrant Likely 	if (chip_info) {
1301ca632f55SGrant Likely 		if (chip_info->timeout)
1302ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1303ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1304ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1305a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1306a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1307ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1308ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1309ca632f55SGrant Likely 		chip->dma_threshold = 0;
1310ca632f55SGrant Likely 		if (chip_info->enable_loopback)
1311ca632f55SGrant Likely 			chip->cr1 = SSCR1_LBM;
1312ca632f55SGrant Likely 	}
131351eea52dSLubomir Rintel 	if (spi_controller_is_slave(drv_data->controller)) {
1314ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCFR;
1315ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCLKDIR;
1316ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SFRMDIR;
1317ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SPH;
1318ec93cb6fSLubomir Rintel 	}
1319ca632f55SGrant Likely 
1320a0d2642eSMika Westerberg 	chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1321a0d2642eSMika Westerberg 	chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1322a0d2642eSMika Westerberg 				| SSITF_TxHiThresh(tx_hi_thres);
1323a0d2642eSMika Westerberg 
1324ca632f55SGrant Likely 	/* set dma burst and threshold outside of chip_info path so that if
1325ca632f55SGrant Likely 	 * chip_info goes away after setting chip->enable_dma, the
1326ca632f55SGrant Likely 	 * burst and threshold can still respond to changes in bits_per_word */
1327ca632f55SGrant Likely 	if (chip->enable_dma) {
1328ca632f55SGrant Likely 		/* set up legal burst and threshold for dma */
1329cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1330cd7bed00SMika Westerberg 						spi->bits_per_word,
1331ca632f55SGrant Likely 						&chip->dma_burst_size,
1332ca632f55SGrant Likely 						&chip->dma_threshold)) {
1333f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1334f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1335ca632f55SGrant Likely 		}
1336000c6af4SAndy Shevchenko 		dev_dbg(&spi->dev,
1337000c6af4SAndy Shevchenko 			"in setup: DMA burst size set to %u\n",
1338000c6af4SAndy Shevchenko 			chip->dma_burst_size);
1339ca632f55SGrant Likely 	}
1340ca632f55SGrant Likely 
1341e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1342e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1343e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1344e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1345e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1346e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1347e5262d05SWeike Chen 		break;
13487c7289a4SAndy Shevchenko 	case CE4100_SSP:
13497c7289a4SAndy Shevchenko 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
13507c7289a4SAndy Shevchenko 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
13517c7289a4SAndy Shevchenko 		break;
1352e5262d05SWeike Chen 	default:
1353e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1354e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1355e5262d05SWeike Chen 		break;
1356e5262d05SWeike Chen 	}
1357e5262d05SWeike Chen 
1358ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1359ca632f55SGrant Likely 	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1360ca632f55SGrant Likely 			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1361ca632f55SGrant Likely 
1362b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1363b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1364b833172fSMika Westerberg 
1365ca632f55SGrant Likely 	if (spi->bits_per_word <= 8) {
1366ca632f55SGrant Likely 		chip->n_bytes = 1;
1367ca632f55SGrant Likely 		chip->read = u8_reader;
1368ca632f55SGrant Likely 		chip->write = u8_writer;
1369ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 16) {
1370ca632f55SGrant Likely 		chip->n_bytes = 2;
1371ca632f55SGrant Likely 		chip->read = u16_reader;
1372ca632f55SGrant Likely 		chip->write = u16_writer;
1373ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 32) {
1374ca632f55SGrant Likely 		chip->n_bytes = 4;
1375ca632f55SGrant Likely 		chip->read = u32_reader;
1376ca632f55SGrant Likely 		chip->write = u32_writer;
1377ca632f55SGrant Likely 	}
1378ca632f55SGrant Likely 
1379ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1380ca632f55SGrant Likely 
1381ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
1382ca632f55SGrant Likely 		return 0;
1383ca632f55SGrant Likely 
1384ca632f55SGrant Likely 	return setup_cs(spi, chip, chip_info);
1385ca632f55SGrant Likely }
1386ca632f55SGrant Likely 
1387ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1388ca632f55SGrant Likely {
1389ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
13903cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
13913cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1392ca632f55SGrant Likely 
1393ca632f55SGrant Likely 	if (!chip)
1394ca632f55SGrant Likely 		return;
1395ca632f55SGrant Likely 
13966ac5a435SAndy Shevchenko 	if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
1397c18d925fSJan Kiszka 	    chip->gpiod_cs)
1398a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1399ca632f55SGrant Likely 
1400ca632f55SGrant Likely 	kfree(chip);
1401ca632f55SGrant Likely }
1402ca632f55SGrant Likely 
14038422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
140403fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
140503fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
140603fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
140703fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
140803fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
140930f3a6abSMika Westerberg 	{ "8086228E", LPSS_BSW_SSP },
141003fbf488SJarkko Nikula 	{ },
141103fbf488SJarkko Nikula };
141203fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
141303fbf488SJarkko Nikula 
141434cadd9cSJarkko Nikula /*
141534cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
141634cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
141734cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
141834cadd9cSJarkko Nikula  */
141934cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
142034cadd9cSJarkko Nikula 	/* SPT-LP */
142134cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
142234cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
142334cadd9cSJarkko Nikula 	/* SPT-H */
142434cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
142534cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1426704d2b07SMika Westerberg 	/* KBL-H */
1427704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1428704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
1429c1b03f11SJarkko Nikula 	/* BXT A-Step */
1430b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1431b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1432b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1433c1b03f11SJarkko Nikula 	/* BXT B-Step */
1434c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1435c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1436c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1437e18a80acSDavid E. Box 	/* GLK */
1438e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1439e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1440e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
144122d71a50SMika Westerberg 	/* ICL-LP */
144222d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
144322d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
144422d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
1445b7c08cf8SJarkko Nikula 	/* APL */
1446b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1447b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1448b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1449fc0b2accSJarkko Nikula 	/* CNL-LP */
1450fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1451fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1452fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1453fc0b2accSJarkko Nikula 	/* CNL-H */
1454fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1455fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1456fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
145794e5c23dSAxel Lin 	{ },
145834cadd9cSJarkko Nikula };
145934cadd9cSJarkko Nikula 
146087ae1d2dSLubomir Rintel static const struct of_device_id pxa2xx_spi_of_match[] = {
146187ae1d2dSLubomir Rintel 	{ .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
146287ae1d2dSLubomir Rintel 	{},
146387ae1d2dSLubomir Rintel };
146487ae1d2dSLubomir Rintel MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
146587ae1d2dSLubomir Rintel 
146687ae1d2dSLubomir Rintel #ifdef CONFIG_ACPI
146787ae1d2dSLubomir Rintel 
146887ae1d2dSLubomir Rintel static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
146987ae1d2dSLubomir Rintel {
147087ae1d2dSLubomir Rintel 	unsigned int devid;
147187ae1d2dSLubomir Rintel 	int port_id = -1;
147287ae1d2dSLubomir Rintel 
147387ae1d2dSLubomir Rintel 	if (adev && adev->pnp.unique_id &&
147487ae1d2dSLubomir Rintel 	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
147587ae1d2dSLubomir Rintel 		port_id = devid;
147687ae1d2dSLubomir Rintel 	return port_id;
147787ae1d2dSLubomir Rintel }
147887ae1d2dSLubomir Rintel 
147987ae1d2dSLubomir Rintel #else /* !CONFIG_ACPI */
148087ae1d2dSLubomir Rintel 
148187ae1d2dSLubomir Rintel static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
148287ae1d2dSLubomir Rintel {
148387ae1d2dSLubomir Rintel 	return -1;
148487ae1d2dSLubomir Rintel }
148587ae1d2dSLubomir Rintel 
148687ae1d2dSLubomir Rintel #endif /* CONFIG_ACPI */
148787ae1d2dSLubomir Rintel 
148887ae1d2dSLubomir Rintel 
148987ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
149087ae1d2dSLubomir Rintel 
149134cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
149234cadd9cSJarkko Nikula {
149334cadd9cSJarkko Nikula 	struct device *dev = param;
149434cadd9cSJarkko Nikula 
149534cadd9cSJarkko Nikula 	if (dev != chan->device->dev->parent)
149634cadd9cSJarkko Nikula 		return false;
149734cadd9cSJarkko Nikula 
149834cadd9cSJarkko Nikula 	return true;
149934cadd9cSJarkko Nikula }
150034cadd9cSJarkko Nikula 
150187ae1d2dSLubomir Rintel #endif /* CONFIG_PCI */
150287ae1d2dSLubomir Rintel 
150351eea52dSLubomir Rintel static struct pxa2xx_spi_controller *
15040db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1505a3496855SMika Westerberg {
150651eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *pdata;
1507a3496855SMika Westerberg 	struct acpi_device *adev;
1508a3496855SMika Westerberg 	struct ssp_device *ssp;
1509a3496855SMika Westerberg 	struct resource *res;
151034cadd9cSJarkko Nikula 	const struct acpi_device_id *adev_id = NULL;
151134cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
151287ae1d2dSLubomir Rintel 	const struct of_device_id *of_id = NULL;
151355ef8262SLubomir Rintel 	enum pxa_ssp_type type;
1514a3496855SMika Westerberg 
1515b9f6940aSJarkko Nikula 	adev = ACPI_COMPANION(&pdev->dev);
1516a3496855SMika Westerberg 
151787ae1d2dSLubomir Rintel 	if (pdev->dev.of_node)
151887ae1d2dSLubomir Rintel 		of_id = of_match_device(pdev->dev.driver->of_match_table,
151987ae1d2dSLubomir Rintel 					&pdev->dev);
152087ae1d2dSLubomir Rintel 	else if (dev_is_pci(pdev->dev.parent))
152134cadd9cSJarkko Nikula 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
152234cadd9cSJarkko Nikula 					 to_pci_dev(pdev->dev.parent));
15230db64215SJarkko Nikula 	else if (adev)
152434cadd9cSJarkko Nikula 		adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
152534cadd9cSJarkko Nikula 					    &pdev->dev);
15260db64215SJarkko Nikula 	else
15270db64215SJarkko Nikula 		return NULL;
152834cadd9cSJarkko Nikula 
152934cadd9cSJarkko Nikula 	if (adev_id)
153055ef8262SLubomir Rintel 		type = (enum pxa_ssp_type)adev_id->driver_data;
153134cadd9cSJarkko Nikula 	else if (pcidev_id)
153255ef8262SLubomir Rintel 		type = (enum pxa_ssp_type)pcidev_id->driver_data;
153387ae1d2dSLubomir Rintel 	else if (of_id)
153487ae1d2dSLubomir Rintel 		type = (enum pxa_ssp_type)of_id->data;
153503fbf488SJarkko Nikula 	else
153603fbf488SJarkko Nikula 		return NULL;
153703fbf488SJarkko Nikula 
1538cc0ee987SMika Westerberg 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
15399deae459SJingoo Han 	if (!pdata)
1540a3496855SMika Westerberg 		return NULL;
1541a3496855SMika Westerberg 
1542a3496855SMika Westerberg 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1543a3496855SMika Westerberg 	if (!res)
1544a3496855SMika Westerberg 		return NULL;
1545a3496855SMika Westerberg 
1546a3496855SMika Westerberg 	ssp = &pdata->ssp;
1547a3496855SMika Westerberg 
1548a3496855SMika Westerberg 	ssp->phys_base = res->start;
1549cbfd6a21SSachin Kamat 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1550cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
15516dc81f6fSMika Westerberg 		return NULL;
1552a3496855SMika Westerberg 
155387ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
155434cadd9cSJarkko Nikula 	if (pcidev_id) {
155534cadd9cSJarkko Nikula 		pdata->tx_param = pdev->dev.parent;
155634cadd9cSJarkko Nikula 		pdata->rx_param = pdev->dev.parent;
155734cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
155834cadd9cSJarkko Nikula 	}
155987ae1d2dSLubomir Rintel #endif
156034cadd9cSJarkko Nikula 
1561a3496855SMika Westerberg 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
1562a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
156303fbf488SJarkko Nikula 	ssp->type = type;
1564a3496855SMika Westerberg 	ssp->pdev = pdev;
15650db64215SJarkko Nikula 	ssp->port_id = pxa2xx_spi_get_port_id(adev);
1566a3496855SMika Westerberg 
1567f0915dfcSLubomir Rintel 	pdata->is_slave = of_property_read_bool(pdev->dev.of_node, "spi-slave");
1568a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1569cddb339bSMika Westerberg 	pdata->enable_dma = true;
157037821a82SAndy Shevchenko 	pdata->dma_burst_size = 1;
1571a3496855SMika Westerberg 
1572a3496855SMika Westerberg 	return pdata;
1573a3496855SMika Westerberg }
1574a3496855SMika Westerberg 
157551eea52dSLubomir Rintel static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
15763cc7b0e3SJarkko Nikula 				      unsigned int cs)
15770c27d9cfSMika Westerberg {
157851eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
15790c27d9cfSMika Westerberg 
15800c27d9cfSMika Westerberg 	if (has_acpi_companion(&drv_data->pdev->dev)) {
15810c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
15820c27d9cfSMika Westerberg 		/*
15830c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
15840c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
15850c27d9cfSMika Westerberg 		 * to match what Linux expects.
15860c27d9cfSMika Westerberg 		 */
15870c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
158830f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
15890c27d9cfSMika Westerberg 			return cs - 1;
15900c27d9cfSMika Westerberg 
15910c27d9cfSMika Westerberg 		default:
15920c27d9cfSMika Westerberg 			break;
15930c27d9cfSMika Westerberg 		}
15940c27d9cfSMika Westerberg 	}
15950c27d9cfSMika Westerberg 
15960c27d9cfSMika Westerberg 	return cs;
15970c27d9cfSMika Westerberg }
15980c27d9cfSMika Westerberg 
1599fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1600ca632f55SGrant Likely {
1601ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
160251eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *platform_info;
160351eea52dSLubomir Rintel 	struct spi_controller *controller;
1604ca632f55SGrant Likely 	struct driver_data *drv_data;
1605ca632f55SGrant Likely 	struct ssp_device *ssp;
16068b136baaSJarkko Nikula 	const struct lpss_config *config;
160799f499cdSMika Westerberg 	int status, count;
1608c039dd27SJarkko Nikula 	u32 tmp;
1609ca632f55SGrant Likely 
1610851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1611851bacf5SMika Westerberg 	if (!platform_info) {
16120db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
1613a3496855SMika Westerberg 		if (!platform_info) {
1614851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
1615851bacf5SMika Westerberg 			return -ENODEV;
1616851bacf5SMika Westerberg 		}
1617a3496855SMika Westerberg 	}
1618ca632f55SGrant Likely 
1619ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1620851bacf5SMika Westerberg 	if (!ssp)
1621851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1622851bacf5SMika Westerberg 
1623851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
1624851bacf5SMika Westerberg 		dev_err(&pdev->dev, "failed to get ssp\n");
1625ca632f55SGrant Likely 		return -ENODEV;
1626ca632f55SGrant Likely 	}
1627ca632f55SGrant Likely 
1628ec93cb6fSLubomir Rintel 	if (platform_info->is_slave)
162951eea52dSLubomir Rintel 		controller = spi_alloc_slave(dev, sizeof(struct driver_data));
1630ec93cb6fSLubomir Rintel 	else
163151eea52dSLubomir Rintel 		controller = spi_alloc_master(dev, sizeof(struct driver_data));
1632ec93cb6fSLubomir Rintel 
163351eea52dSLubomir Rintel 	if (!controller) {
163451eea52dSLubomir Rintel 		dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1635ca632f55SGrant Likely 		pxa_ssp_free(ssp);
1636ca632f55SGrant Likely 		return -ENOMEM;
1637ca632f55SGrant Likely 	}
163851eea52dSLubomir Rintel 	drv_data = spi_controller_get_devdata(controller);
163951eea52dSLubomir Rintel 	drv_data->controller = controller;
164051eea52dSLubomir Rintel 	drv_data->controller_info = platform_info;
1641ca632f55SGrant Likely 	drv_data->pdev = pdev;
1642ca632f55SGrant Likely 	drv_data->ssp = ssp;
1643ca632f55SGrant Likely 
164451eea52dSLubomir Rintel 	controller->dev.of_node = pdev->dev.of_node;
1645ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
164651eea52dSLubomir Rintel 	controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1647ca632f55SGrant Likely 
164851eea52dSLubomir Rintel 	controller->bus_num = ssp->port_id;
164951eea52dSLubomir Rintel 	controller->dma_alignment = DMA_ALIGNMENT;
165051eea52dSLubomir Rintel 	controller->cleanup = cleanup;
165151eea52dSLubomir Rintel 	controller->setup = setup;
165251eea52dSLubomir Rintel 	controller->set_cs = pxa2xx_spi_set_cs;
165351eea52dSLubomir Rintel 	controller->transfer_one = pxa2xx_spi_transfer_one;
165451eea52dSLubomir Rintel 	controller->slave_abort = pxa2xx_spi_slave_abort;
165551eea52dSLubomir Rintel 	controller->handle_err = pxa2xx_spi_handle_err;
165651eea52dSLubomir Rintel 	controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
165751eea52dSLubomir Rintel 	controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
165851eea52dSLubomir Rintel 	controller->auto_runtime_pm = true;
165951eea52dSLubomir Rintel 	controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1660ca632f55SGrant Likely 
1661ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1662ca632f55SGrant Likely 
1663ca632f55SGrant Likely 	drv_data->ioaddr = ssp->mmio_base;
1664ca632f55SGrant Likely 	drv_data->ssdr_physical = ssp->phys_base + SSDR;
1665ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1666e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1667e5262d05SWeike Chen 		case QUARK_X1000_SSP:
166851eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1669e5262d05SWeike Chen 			break;
1670e5262d05SWeike Chen 		default:
167151eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1672e5262d05SWeike Chen 			break;
1673e5262d05SWeike Chen 		}
1674e5262d05SWeike Chen 
1675ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1676ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1677ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1678ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1679ca632f55SGrant Likely 	} else {
168051eea52dSLubomir Rintel 		controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1681ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
16825928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1683ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1684ec93cb6fSLubomir Rintel 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1685ec93cb6fSLubomir Rintel 						| SSSR_ROR | SSSR_TUR;
1686ca632f55SGrant Likely 	}
1687ca632f55SGrant Likely 
1688ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1689ca632f55SGrant Likely 			drv_data);
1690ca632f55SGrant Likely 	if (status < 0) {
1691ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
169251eea52dSLubomir Rintel 		goto out_error_controller_alloc;
1693ca632f55SGrant Likely 	}
1694ca632f55SGrant Likely 
1695ca632f55SGrant Likely 	/* Setup DMA if requested */
1696ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1697cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1698cd7bed00SMika Westerberg 		if (status) {
1699cddb339bSMika Westerberg 			dev_dbg(dev, "no DMA channels available, using PIO\n");
1700cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1701b6ced294SJarkko Nikula 		} else {
170251eea52dSLubomir Rintel 			controller->can_dma = pxa2xx_spi_can_dma;
1703bf9f742cSMark Brown 			controller->max_dma_len = MAX_DMA_LEN;
1704ca632f55SGrant Likely 		}
1705ca632f55SGrant Likely 	}
1706ca632f55SGrant Likely 
1707ca632f55SGrant Likely 	/* Enable SOC clock */
170862bbc864STobias Jordan 	status = clk_prepare_enable(ssp->clk);
170962bbc864STobias Jordan 	if (status)
171062bbc864STobias Jordan 		goto out_error_dma_irq_alloc;
17113343b7a6SMika Westerberg 
171251eea52dSLubomir Rintel 	controller->max_speed_hz = clk_get_rate(ssp->clk);
1713ca632f55SGrant Likely 
1714ca632f55SGrant Likely 	/* Load default SSP configuration */
1715c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
1716e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1717e5262d05SWeike Chen 	case QUARK_X1000_SSP:
17187c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
17197c7289a4SAndy Shevchenko 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1720c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1721e5262d05SWeike Chen 
1722e5262d05SWeike Chen 		/* using the Motorola SPI protocol and use 8 bit frame */
17237c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
17247c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1725e5262d05SWeike Chen 		break;
17267c7289a4SAndy Shevchenko 	case CE4100_SSP:
17277c7289a4SAndy Shevchenko 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
17287c7289a4SAndy Shevchenko 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
17297c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
17307c7289a4SAndy Shevchenko 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
17317c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1732a2dd8af0SAndy Shevchenko 		break;
1733e5262d05SWeike Chen 	default:
1734ec93cb6fSLubomir Rintel 
173551eea52dSLubomir Rintel 		if (spi_controller_is_slave(controller)) {
1736ec93cb6fSLubomir Rintel 			tmp = SSCR1_SCFR |
1737ec93cb6fSLubomir Rintel 			      SSCR1_SCLKDIR |
1738ec93cb6fSLubomir Rintel 			      SSCR1_SFRMDIR |
1739ec93cb6fSLubomir Rintel 			      SSCR1_RxTresh(2) |
1740ec93cb6fSLubomir Rintel 			      SSCR1_TxTresh(1) |
1741ec93cb6fSLubomir Rintel 			      SSCR1_SPH;
1742ec93cb6fSLubomir Rintel 		} else {
1743c039dd27SJarkko Nikula 			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1744c039dd27SJarkko Nikula 			      SSCR1_TxTresh(TX_THRESH_DFLT);
1745ec93cb6fSLubomir Rintel 		}
1746c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1747ec93cb6fSLubomir Rintel 		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
174851eea52dSLubomir Rintel 		if (!spi_controller_is_slave(controller))
1749ec93cb6fSLubomir Rintel 			tmp |= SSCR0_SCR(2);
1750c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1751e5262d05SWeike Chen 		break;
1752e5262d05SWeike Chen 	}
1753e5262d05SWeike Chen 
1754ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1755c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1756e5262d05SWeike Chen 
1757e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1758c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1759ca632f55SGrant Likely 
17608b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
17618b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
17628b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
17638b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
17648b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
17658b136baaSJarkko Nikula 						   config->reg_capabilities);
17668b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
17678b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
17688b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
176930f3a6abSMika Westerberg 		} else if (config->cs_num) {
177030f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
17718b136baaSJarkko Nikula 		}
17728b136baaSJarkko Nikula 	}
177351eea52dSLubomir Rintel 	controller->num_chipselect = platform_info->num_chipselect;
17748b136baaSJarkko Nikula 
177599f499cdSMika Westerberg 	count = gpiod_count(&pdev->dev, "cs");
17766ac5a435SAndy Shevchenko 	if (count > 0) {
17776ac5a435SAndy Shevchenko 		int i;
17786ac5a435SAndy Shevchenko 
177951eea52dSLubomir Rintel 		controller->num_chipselect = max_t(int, count,
178051eea52dSLubomir Rintel 			controller->num_chipselect);
178199f499cdSMika Westerberg 
17826ac5a435SAndy Shevchenko 		drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
178351eea52dSLubomir Rintel 			controller->num_chipselect, sizeof(struct gpio_desc *),
17846ac5a435SAndy Shevchenko 			GFP_KERNEL);
17856ac5a435SAndy Shevchenko 		if (!drv_data->cs_gpiods) {
17866ac5a435SAndy Shevchenko 			status = -ENOMEM;
17876ac5a435SAndy Shevchenko 			goto out_error_clock_enabled;
17886ac5a435SAndy Shevchenko 		}
17896ac5a435SAndy Shevchenko 
179051eea52dSLubomir Rintel 		for (i = 0; i < controller->num_chipselect; i++) {
17916ac5a435SAndy Shevchenko 			struct gpio_desc *gpiod;
17926ac5a435SAndy Shevchenko 
1793d35f2dc9SAndy Shevchenko 			gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
17946ac5a435SAndy Shevchenko 			if (IS_ERR(gpiod)) {
17956ac5a435SAndy Shevchenko 				/* Means use native chip select */
17966ac5a435SAndy Shevchenko 				if (PTR_ERR(gpiod) == -ENOENT)
17976ac5a435SAndy Shevchenko 					continue;
17986ac5a435SAndy Shevchenko 
179977d33897SLubomir Rintel 				status = PTR_ERR(gpiod);
18006ac5a435SAndy Shevchenko 				goto out_error_clock_enabled;
18016ac5a435SAndy Shevchenko 			} else {
18026ac5a435SAndy Shevchenko 				drv_data->cs_gpiods[i] = gpiod;
18036ac5a435SAndy Shevchenko 			}
18046ac5a435SAndy Shevchenko 		}
18056ac5a435SAndy Shevchenko 	}
18066ac5a435SAndy Shevchenko 
180777d33897SLubomir Rintel 	if (platform_info->is_slave) {
180877d33897SLubomir Rintel 		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
180977d33897SLubomir Rintel 						"ready", GPIOD_OUT_LOW);
181077d33897SLubomir Rintel 		if (IS_ERR(drv_data->gpiod_ready)) {
181177d33897SLubomir Rintel 			status = PTR_ERR(drv_data->gpiod_ready);
181277d33897SLubomir Rintel 			goto out_error_clock_enabled;
181377d33897SLubomir Rintel 		}
181477d33897SLubomir Rintel 	}
181577d33897SLubomir Rintel 
1816836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1817836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1818836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1819836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1820836d1a22SAntonio Ospite 
1821ca632f55SGrant Likely 	/* Register with the SPI framework */
1822ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
182351eea52dSLubomir Rintel 	status = devm_spi_register_controller(&pdev->dev, controller);
1824ca632f55SGrant Likely 	if (status != 0) {
182551eea52dSLubomir Rintel 		dev_err(&pdev->dev, "problem registering spi controller\n");
18267f86bde9SMika Westerberg 		goto out_error_clock_enabled;
1827ca632f55SGrant Likely 	}
1828ca632f55SGrant Likely 
1829ca632f55SGrant Likely 	return status;
1830ca632f55SGrant Likely 
1831ca632f55SGrant Likely out_error_clock_enabled:
1832e2b714afSJarkko Nikula 	pm_runtime_put_noidle(&pdev->dev);
1833e2b714afSJarkko Nikula 	pm_runtime_disable(&pdev->dev);
18343343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
183562bbc864STobias Jordan 
183662bbc864STobias Jordan out_error_dma_irq_alloc:
1837cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1838ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1839ca632f55SGrant Likely 
184051eea52dSLubomir Rintel out_error_controller_alloc:
184151eea52dSLubomir Rintel 	spi_controller_put(controller);
1842ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1843ca632f55SGrant Likely 	return status;
1844ca632f55SGrant Likely }
1845ca632f55SGrant Likely 
1846ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1847ca632f55SGrant Likely {
1848ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
1849ca632f55SGrant Likely 	struct ssp_device *ssp;
1850ca632f55SGrant Likely 
1851ca632f55SGrant Likely 	if (!drv_data)
1852ca632f55SGrant Likely 		return 0;
1853ca632f55SGrant Likely 	ssp = drv_data->ssp;
1854ca632f55SGrant Likely 
18557d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
18567d94a505SMika Westerberg 
1857ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
1858c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
18593343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1860ca632f55SGrant Likely 
1861ca632f55SGrant Likely 	/* Release DMA */
186251eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma)
1863cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1864ca632f55SGrant Likely 
18657d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
18667d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
18677d94a505SMika Westerberg 
1868ca632f55SGrant Likely 	/* Release IRQ */
1869ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1870ca632f55SGrant Likely 
1871ca632f55SGrant Likely 	/* Release SSP */
1872ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1873ca632f55SGrant Likely 
1874ca632f55SGrant Likely 	return 0;
1875ca632f55SGrant Likely }
1876ca632f55SGrant Likely 
1877382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP
1878ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1879ca632f55SGrant Likely {
1880ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1881ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1882bffc967eSJarkko Nikula 	int status;
1883ca632f55SGrant Likely 
188451eea52dSLubomir Rintel 	status = spi_controller_suspend(drv_data->controller);
1885ca632f55SGrant Likely 	if (status != 0)
1886ca632f55SGrant Likely 		return status;
1887c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
18882b9375b9SDmitry Eremin-Solenikov 
18892b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
18903343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1891ca632f55SGrant Likely 
1892ca632f55SGrant Likely 	return 0;
1893ca632f55SGrant Likely }
1894ca632f55SGrant Likely 
1895ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1896ca632f55SGrant Likely {
1897ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1898ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1899bffc967eSJarkko Nikula 	int status;
1900ca632f55SGrant Likely 
1901ca632f55SGrant Likely 	/* Enable the SSP clock */
190262bbc864STobias Jordan 	if (!pm_runtime_suspended(dev)) {
190362bbc864STobias Jordan 		status = clk_prepare_enable(ssp->clk);
190462bbc864STobias Jordan 		if (status)
190562bbc864STobias Jordan 			return status;
190662bbc864STobias Jordan 	}
1907ca632f55SGrant Likely 
1908ca632f55SGrant Likely 	/* Start the queue running */
190951eea52dSLubomir Rintel 	return spi_controller_resume(drv_data->controller);
1910ca632f55SGrant Likely }
19117d94a505SMika Westerberg #endif
19127d94a505SMika Westerberg 
1913ec833050SRafael J. Wysocki #ifdef CONFIG_PM
19147d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
19157d94a505SMika Westerberg {
19167d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
19177d94a505SMika Westerberg 
19187d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
19197d94a505SMika Westerberg 	return 0;
19207d94a505SMika Westerberg }
19217d94a505SMika Westerberg 
19227d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
19237d94a505SMika Westerberg {
19247d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
192562bbc864STobias Jordan 	int status;
19267d94a505SMika Westerberg 
192762bbc864STobias Jordan 	status = clk_prepare_enable(drv_data->ssp->clk);
192862bbc864STobias Jordan 	return status;
19297d94a505SMika Westerberg }
19307d94a505SMika Westerberg #endif
1931ca632f55SGrant Likely 
1932ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
19337d94a505SMika Westerberg 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
19347d94a505SMika Westerberg 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
19357d94a505SMika Westerberg 			   pxa2xx_spi_runtime_resume, NULL)
1936ca632f55SGrant Likely };
1937ca632f55SGrant Likely 
1938ca632f55SGrant Likely static struct platform_driver driver = {
1939ca632f55SGrant Likely 	.driver = {
1940ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
1941ca632f55SGrant Likely 		.pm	= &pxa2xx_spi_pm_ops,
1942a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
194387ae1d2dSLubomir Rintel 		.of_match_table = of_match_ptr(pxa2xx_spi_of_match),
1944ca632f55SGrant Likely 	},
1945ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
1946ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
1947ca632f55SGrant Likely };
1948ca632f55SGrant Likely 
1949ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
1950ca632f55SGrant Likely {
1951ca632f55SGrant Likely 	return platform_driver_register(&driver);
1952ca632f55SGrant Likely }
1953ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
1954ca632f55SGrant Likely 
1955ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
1956ca632f55SGrant Likely {
1957ca632f55SGrant Likely 	platform_driver_unregister(&driver);
1958ca632f55SGrant Likely }
1959ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
1960