xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision 0e4768713e71dd224633fd7e00ad358bc48f433a)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
4a0d2642eSMika Westerberg  * Copyright (C) 2013, Intel Corporation
5ca632f55SGrant Likely  */
6ca632f55SGrant Likely 
75ce25705SAndy Shevchenko #include <linux/acpi.h>
88b136baaSJarkko Nikula #include <linux/bitops.h>
95ce25705SAndy Shevchenko #include <linux/clk.h>
105ce25705SAndy Shevchenko #include <linux/delay.h>
11ca632f55SGrant Likely #include <linux/device.h>
12*0e476871SAndy Shevchenko #include <linux/dmaengine.h>
13cbfd6a21SSachin Kamat #include <linux/err.h>
145ce25705SAndy Shevchenko #include <linux/errno.h>
155ce25705SAndy Shevchenko #include <linux/gpio/consumer.h>
165ce25705SAndy Shevchenko #include <linux/gpio.h>
175ce25705SAndy Shevchenko #include <linux/init.h>
18ca632f55SGrant Likely #include <linux/interrupt.h>
195ce25705SAndy Shevchenko #include <linux/ioport.h>
209df461ecSAndy Shevchenko #include <linux/kernel.h>
215ce25705SAndy Shevchenko #include <linux/module.h>
22ae8fbf1dSAndy Shevchenko #include <linux/mod_devicetable.h>
23ae8fbf1dSAndy Shevchenko #include <linux/of.h>
2434cadd9cSJarkko Nikula #include <linux/pci.h>
25ca632f55SGrant Likely #include <linux/platform_device.h>
265ce25705SAndy Shevchenko #include <linux/pm_runtime.h>
27f2faa3ecSAndy Shevchenko #include <linux/property.h>
285ce25705SAndy Shevchenko #include <linux/slab.h>
29*0e476871SAndy Shevchenko 
30ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h>
31ca632f55SGrant Likely #include <linux/spi/spi.h>
32ca632f55SGrant Likely 
33cd7bed00SMika Westerberg #include "spi-pxa2xx.h"
34ca632f55SGrant Likely 
35ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street");
36ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
37ca632f55SGrant Likely MODULE_LICENSE("GPL");
38ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi");
39ca632f55SGrant Likely 
40ca632f55SGrant Likely #define TIMOUT_DFLT		1000
41ca632f55SGrant Likely 
42ca632f55SGrant Likely /*
43ca632f55SGrant Likely  * for testing SSCR1 changes that require SSP restart, basically
44ca632f55SGrant Likely  * everything except the service and interrupt enables, the pxa270 developer
45ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
46ca632f55SGrant Likely  * list, but the PXA255 dev man says all bits without really meaning the
47ca632f55SGrant Likely  * service and interrupt enables
48ca632f55SGrant Likely  */
49ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
50ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
51ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
52ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
53ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
54ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
55ca632f55SGrant Likely 
56e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
57e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
58e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
59e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
60e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
61e5262d05SWeike Chen 
627c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
637c7289a4SAndy Shevchenko 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
647c7289a4SAndy Shevchenko 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
657c7289a4SAndy Shevchenko 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
667c7289a4SAndy Shevchenko 				| CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
677c7289a4SAndy Shevchenko 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
687c7289a4SAndy Shevchenko 
69624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
70624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE			BIT(0)
71624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
728b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT			9
738b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
74a0d2642eSMika Westerberg 
75683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE 0x38
76683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
77683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
78683f65deSEvan Green 
79dccf7369SJarkko Nikula struct lpss_config {
80dccf7369SJarkko Nikula 	/* LPSS offset from drv_data->ioaddr */
81dccf7369SJarkko Nikula 	unsigned offset;
82dccf7369SJarkko Nikula 	/* Register offsets from drv_data->lpss_base or -1 */
83dccf7369SJarkko Nikula 	int reg_general;
84dccf7369SJarkko Nikula 	int reg_ssp;
85dccf7369SJarkko Nikula 	int reg_cs_ctrl;
868b136baaSJarkko Nikula 	int reg_capabilities;
87dccf7369SJarkko Nikula 	/* FIFO thresholds */
88dccf7369SJarkko Nikula 	u32 rx_threshold;
89dccf7369SJarkko Nikula 	u32 tx_threshold_lo;
90dccf7369SJarkko Nikula 	u32 tx_threshold_hi;
91c1e4a53cSMika Westerberg 	/* Chip select control */
92c1e4a53cSMika Westerberg 	unsigned cs_sel_shift;
93c1e4a53cSMika Westerberg 	unsigned cs_sel_mask;
9430f3a6abSMika Westerberg 	unsigned cs_num;
95683f65deSEvan Green 	/* Quirks */
96683f65deSEvan Green 	unsigned cs_clk_stays_gated : 1;
97dccf7369SJarkko Nikula };
98dccf7369SJarkko Nikula 
99dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */
100dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = {
101dccf7369SJarkko Nikula 	{	/* LPSS_LPT_SSP */
102dccf7369SJarkko Nikula 		.offset = 0x800,
103dccf7369SJarkko Nikula 		.reg_general = 0x08,
104dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
105dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1068b136baaSJarkko Nikula 		.reg_capabilities = -1,
107dccf7369SJarkko Nikula 		.rx_threshold = 64,
108dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
109dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
110dccf7369SJarkko Nikula 	},
111dccf7369SJarkko Nikula 	{	/* LPSS_BYT_SSP */
112dccf7369SJarkko Nikula 		.offset = 0x400,
113dccf7369SJarkko Nikula 		.reg_general = 0x08,
114dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
115dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1168b136baaSJarkko Nikula 		.reg_capabilities = -1,
117dccf7369SJarkko Nikula 		.rx_threshold = 64,
118dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
119dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
120dccf7369SJarkko Nikula 	},
12130f3a6abSMika Westerberg 	{	/* LPSS_BSW_SSP */
12230f3a6abSMika Westerberg 		.offset = 0x400,
12330f3a6abSMika Westerberg 		.reg_general = 0x08,
12430f3a6abSMika Westerberg 		.reg_ssp = 0x0c,
12530f3a6abSMika Westerberg 		.reg_cs_ctrl = 0x18,
12630f3a6abSMika Westerberg 		.reg_capabilities = -1,
12730f3a6abSMika Westerberg 		.rx_threshold = 64,
12830f3a6abSMika Westerberg 		.tx_threshold_lo = 160,
12930f3a6abSMika Westerberg 		.tx_threshold_hi = 224,
13030f3a6abSMika Westerberg 		.cs_sel_shift = 2,
13130f3a6abSMika Westerberg 		.cs_sel_mask = 1 << 2,
13230f3a6abSMika Westerberg 		.cs_num = 2,
13330f3a6abSMika Westerberg 	},
13434cadd9cSJarkko Nikula 	{	/* LPSS_SPT_SSP */
13534cadd9cSJarkko Nikula 		.offset = 0x200,
13634cadd9cSJarkko Nikula 		.reg_general = -1,
13734cadd9cSJarkko Nikula 		.reg_ssp = 0x20,
13834cadd9cSJarkko Nikula 		.reg_cs_ctrl = 0x24,
13966ec246eSJarkko Nikula 		.reg_capabilities = -1,
14034cadd9cSJarkko Nikula 		.rx_threshold = 1,
14134cadd9cSJarkko Nikula 		.tx_threshold_lo = 32,
14234cadd9cSJarkko Nikula 		.tx_threshold_hi = 56,
14334cadd9cSJarkko Nikula 	},
144b7c08cf8SJarkko Nikula 	{	/* LPSS_BXT_SSP */
145b7c08cf8SJarkko Nikula 		.offset = 0x200,
146b7c08cf8SJarkko Nikula 		.reg_general = -1,
147b7c08cf8SJarkko Nikula 		.reg_ssp = 0x20,
148b7c08cf8SJarkko Nikula 		.reg_cs_ctrl = 0x24,
149b7c08cf8SJarkko Nikula 		.reg_capabilities = 0xfc,
150b7c08cf8SJarkko Nikula 		.rx_threshold = 1,
151b7c08cf8SJarkko Nikula 		.tx_threshold_lo = 16,
152b7c08cf8SJarkko Nikula 		.tx_threshold_hi = 48,
153c1e4a53cSMika Westerberg 		.cs_sel_shift = 8,
154c1e4a53cSMika Westerberg 		.cs_sel_mask = 3 << 8,
1556eefaee4SEvan Green 		.cs_clk_stays_gated = true,
156b7c08cf8SJarkko Nikula 	},
157fc0b2accSJarkko Nikula 	{	/* LPSS_CNL_SSP */
158fc0b2accSJarkko Nikula 		.offset = 0x200,
159fc0b2accSJarkko Nikula 		.reg_general = -1,
160fc0b2accSJarkko Nikula 		.reg_ssp = 0x20,
161fc0b2accSJarkko Nikula 		.reg_cs_ctrl = 0x24,
162fc0b2accSJarkko Nikula 		.reg_capabilities = 0xfc,
163fc0b2accSJarkko Nikula 		.rx_threshold = 1,
164fc0b2accSJarkko Nikula 		.tx_threshold_lo = 32,
165fc0b2accSJarkko Nikula 		.tx_threshold_hi = 56,
166fc0b2accSJarkko Nikula 		.cs_sel_shift = 8,
167fc0b2accSJarkko Nikula 		.cs_sel_mask = 3 << 8,
168683f65deSEvan Green 		.cs_clk_stays_gated = true,
169fc0b2accSJarkko Nikula 	},
170dccf7369SJarkko Nikula };
171dccf7369SJarkko Nikula 
172dccf7369SJarkko Nikula static inline const struct lpss_config
173dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data)
174dccf7369SJarkko Nikula {
175dccf7369SJarkko Nikula 	return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
176dccf7369SJarkko Nikula }
177dccf7369SJarkko Nikula 
178a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data)
179a0d2642eSMika Westerberg {
18003fbf488SJarkko Nikula 	switch (drv_data->ssp_type) {
18103fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
18203fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
18330f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
18434cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
185b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
186fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
18703fbf488SJarkko Nikula 		return true;
18803fbf488SJarkko Nikula 	default:
18903fbf488SJarkko Nikula 		return false;
19003fbf488SJarkko Nikula 	}
191a0d2642eSMika Westerberg }
192a0d2642eSMika Westerberg 
193e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
194e5262d05SWeike Chen {
195e5262d05SWeike Chen 	return drv_data->ssp_type == QUARK_X1000_SSP;
196e5262d05SWeike Chen }
197e5262d05SWeike Chen 
19841c98841SAndy Shevchenko static bool is_mmp2_ssp(const struct driver_data *drv_data)
19941c98841SAndy Shevchenko {
20041c98841SAndy Shevchenko 	return drv_data->ssp_type == MMP2_SSP;
20141c98841SAndy Shevchenko }
20241c98841SAndy Shevchenko 
2034fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
2044fdb2424SWeike Chen {
2054fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
206e5262d05SWeike Chen 	case QUARK_X1000_SSP:
207e5262d05SWeike Chen 		return QUARK_X1000_SSCR1_CHANGE_MASK;
2087c7289a4SAndy Shevchenko 	case CE4100_SSP:
2097c7289a4SAndy Shevchenko 		return CE4100_SSCR1_CHANGE_MASK;
2104fdb2424SWeike Chen 	default:
2114fdb2424SWeike Chen 		return SSCR1_CHANGE_MASK;
2124fdb2424SWeike Chen 	}
2134fdb2424SWeike Chen }
2144fdb2424SWeike Chen 
2154fdb2424SWeike Chen static u32
2164fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
2174fdb2424SWeike Chen {
2184fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
219e5262d05SWeike Chen 	case QUARK_X1000_SSP:
220e5262d05SWeike Chen 		return RX_THRESH_QUARK_X1000_DFLT;
2217c7289a4SAndy Shevchenko 	case CE4100_SSP:
2227c7289a4SAndy Shevchenko 		return RX_THRESH_CE4100_DFLT;
2234fdb2424SWeike Chen 	default:
2244fdb2424SWeike Chen 		return RX_THRESH_DFLT;
2254fdb2424SWeike Chen 	}
2264fdb2424SWeike Chen }
2274fdb2424SWeike Chen 
2284fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
2294fdb2424SWeike Chen {
2304fdb2424SWeike Chen 	u32 mask;
2314fdb2424SWeike Chen 
2324fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
233e5262d05SWeike Chen 	case QUARK_X1000_SSP:
234e5262d05SWeike Chen 		mask = QUARK_X1000_SSSR_TFL_MASK;
235e5262d05SWeike Chen 		break;
2367c7289a4SAndy Shevchenko 	case CE4100_SSP:
2377c7289a4SAndy Shevchenko 		mask = CE4100_SSSR_TFL_MASK;
2387c7289a4SAndy Shevchenko 		break;
2394fdb2424SWeike Chen 	default:
2404fdb2424SWeike Chen 		mask = SSSR_TFL_MASK;
2414fdb2424SWeike Chen 		break;
2424fdb2424SWeike Chen 	}
2434fdb2424SWeike Chen 
244c039dd27SJarkko Nikula 	return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
2454fdb2424SWeike Chen }
2464fdb2424SWeike Chen 
2474fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
2484fdb2424SWeike Chen 				     u32 *sccr1_reg)
2494fdb2424SWeike Chen {
2504fdb2424SWeike Chen 	u32 mask;
2514fdb2424SWeike Chen 
2524fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
253e5262d05SWeike Chen 	case QUARK_X1000_SSP:
254e5262d05SWeike Chen 		mask = QUARK_X1000_SSCR1_RFT;
255e5262d05SWeike Chen 		break;
2567c7289a4SAndy Shevchenko 	case CE4100_SSP:
2577c7289a4SAndy Shevchenko 		mask = CE4100_SSCR1_RFT;
2587c7289a4SAndy Shevchenko 		break;
2594fdb2424SWeike Chen 	default:
2604fdb2424SWeike Chen 		mask = SSCR1_RFT;
2614fdb2424SWeike Chen 		break;
2624fdb2424SWeike Chen 	}
2634fdb2424SWeike Chen 	*sccr1_reg &= ~mask;
2644fdb2424SWeike Chen }
2654fdb2424SWeike Chen 
2664fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
2674fdb2424SWeike Chen 				   u32 *sccr1_reg, u32 threshold)
2684fdb2424SWeike Chen {
2694fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
270e5262d05SWeike Chen 	case QUARK_X1000_SSP:
271e5262d05SWeike Chen 		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
272e5262d05SWeike Chen 		break;
2737c7289a4SAndy Shevchenko 	case CE4100_SSP:
2747c7289a4SAndy Shevchenko 		*sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
2757c7289a4SAndy Shevchenko 		break;
2764fdb2424SWeike Chen 	default:
2774fdb2424SWeike Chen 		*sccr1_reg |= SSCR1_RxTresh(threshold);
2784fdb2424SWeike Chen 		break;
2794fdb2424SWeike Chen 	}
2804fdb2424SWeike Chen }
2814fdb2424SWeike Chen 
2824fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
2834fdb2424SWeike Chen 				  u32 clk_div, u8 bits)
2844fdb2424SWeike Chen {
2854fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
286e5262d05SWeike Chen 	case QUARK_X1000_SSP:
287e5262d05SWeike Chen 		return clk_div
288e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_Motorola
289e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
290e5262d05SWeike Chen 			| SSCR0_SSE;
2914fdb2424SWeike Chen 	default:
2924fdb2424SWeike Chen 		return clk_div
2934fdb2424SWeike Chen 			| SSCR0_Motorola
2944fdb2424SWeike Chen 			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
2954fdb2424SWeike Chen 			| SSCR0_SSE
2964fdb2424SWeike Chen 			| (bits > 16 ? SSCR0_EDSS : 0);
2974fdb2424SWeike Chen 	}
2984fdb2424SWeike Chen }
2994fdb2424SWeike Chen 
300a0d2642eSMika Westerberg /*
301a0d2642eSMika Westerberg  * Read and write LPSS SSP private registers. Caller must first check that
302a0d2642eSMika Westerberg  * is_lpss_ssp() returns true before these can be called.
303a0d2642eSMika Westerberg  */
304a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
305a0d2642eSMika Westerberg {
306a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
307a0d2642eSMika Westerberg 	return readl(drv_data->lpss_base + offset);
308a0d2642eSMika Westerberg }
309a0d2642eSMika Westerberg 
310a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data,
311a0d2642eSMika Westerberg 				  unsigned offset, u32 value)
312a0d2642eSMika Westerberg {
313a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
314a0d2642eSMika Westerberg 	writel(value, drv_data->lpss_base + offset);
315a0d2642eSMika Westerberg }
316a0d2642eSMika Westerberg 
317a0d2642eSMika Westerberg /*
318a0d2642eSMika Westerberg  * lpss_ssp_setup - perform LPSS SSP specific setup
319a0d2642eSMika Westerberg  * @drv_data: pointer to the driver private data
320a0d2642eSMika Westerberg  *
321a0d2642eSMika Westerberg  * Perform LPSS SSP specific setup. This function must be called first if
322a0d2642eSMika Westerberg  * one is going to use LPSS SSP private registers.
323a0d2642eSMika Westerberg  */
324a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data)
325a0d2642eSMika Westerberg {
326dccf7369SJarkko Nikula 	const struct lpss_config *config;
327dccf7369SJarkko Nikula 	u32 value;
328a0d2642eSMika Westerberg 
329dccf7369SJarkko Nikula 	config = lpss_get_config(drv_data);
3309e43c9a8SAndy Shevchenko 	drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset;
331a0d2642eSMika Westerberg 
332a0d2642eSMika Westerberg 	/* Enable software chip select control */
3330e897218SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
334624ea72eSJarkko Nikula 	value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
335624ea72eSJarkko Nikula 	value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
336dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
3370054e28dSMika Westerberg 
3380054e28dSMika Westerberg 	/* Enable multiblock DMA transfers */
33951eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma) {
340dccf7369SJarkko Nikula 		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
3411de70612SMika Westerberg 
34282ba2c2aSJarkko Nikula 		if (config->reg_general >= 0) {
34382ba2c2aSJarkko Nikula 			value = __lpss_ssp_read_priv(drv_data,
34482ba2c2aSJarkko Nikula 						     config->reg_general);
345624ea72eSJarkko Nikula 			value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
34682ba2c2aSJarkko Nikula 			__lpss_ssp_write_priv(drv_data,
34782ba2c2aSJarkko Nikula 					      config->reg_general, value);
34882ba2c2aSJarkko Nikula 		}
3491de70612SMika Westerberg 	}
350a0d2642eSMika Westerberg }
351a0d2642eSMika Westerberg 
352d5898e19SJarkko Nikula static void lpss_ssp_select_cs(struct spi_device *spi,
353c1e4a53cSMika Westerberg 			       const struct lpss_config *config)
354a0d2642eSMika Westerberg {
355d5898e19SJarkko Nikula 	struct driver_data *drv_data =
356d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
357d0283eb2SJarkko Nikula 	u32 value, cs;
358a0d2642eSMika Westerberg 
359c1e4a53cSMika Westerberg 	if (!config->cs_sel_mask)
360c1e4a53cSMika Westerberg 		return;
361dccf7369SJarkko Nikula 
362dccf7369SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
363c1e4a53cSMika Westerberg 
364d5898e19SJarkko Nikula 	cs = spi->chip_select;
365c1e4a53cSMika Westerberg 	cs <<= config->cs_sel_shift;
366c1e4a53cSMika Westerberg 	if (cs != (value & config->cs_sel_mask)) {
367d0283eb2SJarkko Nikula 		/*
368c1e4a53cSMika Westerberg 		 * When switching another chip select output active the
369c1e4a53cSMika Westerberg 		 * output must be selected first and wait 2 ssp_clk cycles
370c1e4a53cSMika Westerberg 		 * before changing state to active. Otherwise a short
371c1e4a53cSMika Westerberg 		 * glitch will occur on the previous chip select since
372c1e4a53cSMika Westerberg 		 * output select is latched but state control is not.
373d0283eb2SJarkko Nikula 		 */
374c1e4a53cSMika Westerberg 		value &= ~config->cs_sel_mask;
375d0283eb2SJarkko Nikula 		value |= cs;
376d0283eb2SJarkko Nikula 		__lpss_ssp_write_priv(drv_data,
377d0283eb2SJarkko Nikula 				      config->reg_cs_ctrl, value);
378d0283eb2SJarkko Nikula 		ndelay(1000000000 /
37951eea52dSLubomir Rintel 		       (drv_data->controller->max_speed_hz / 2));
380d0283eb2SJarkko Nikula 	}
381d0283eb2SJarkko Nikula }
382c1e4a53cSMika Westerberg 
383d5898e19SJarkko Nikula static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
384c1e4a53cSMika Westerberg {
385d5898e19SJarkko Nikula 	struct driver_data *drv_data =
386d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
387c1e4a53cSMika Westerberg 	const struct lpss_config *config;
388c1e4a53cSMika Westerberg 	u32 value;
389c1e4a53cSMika Westerberg 
390c1e4a53cSMika Westerberg 	config = lpss_get_config(drv_data);
391c1e4a53cSMika Westerberg 
392c1e4a53cSMika Westerberg 	if (enable)
393d5898e19SJarkko Nikula 		lpss_ssp_select_cs(spi, config);
394c1e4a53cSMika Westerberg 
395c1e4a53cSMika Westerberg 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
396c1e4a53cSMika Westerberg 	if (enable)
397c1e4a53cSMika Westerberg 		value &= ~LPSS_CS_CONTROL_CS_HIGH;
398c1e4a53cSMika Westerberg 	else
399c1e4a53cSMika Westerberg 		value |= LPSS_CS_CONTROL_CS_HIGH;
400dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
401683f65deSEvan Green 	if (config->cs_clk_stays_gated) {
402683f65deSEvan Green 		u32 clkgate;
403683f65deSEvan Green 
404683f65deSEvan Green 		/*
405683f65deSEvan Green 		 * Changing CS alone when dynamic clock gating is on won't
406683f65deSEvan Green 		 * actually flip CS at that time. This ruins SPI transfers
407683f65deSEvan Green 		 * that specify delays, or have no data. Toggle the clock mode
408683f65deSEvan Green 		 * to force on briefly to poke the CS pin to move.
409683f65deSEvan Green 		 */
410683f65deSEvan Green 		clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
411683f65deSEvan Green 		value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
412683f65deSEvan Green 			LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
413683f65deSEvan Green 
414683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
415683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
416683f65deSEvan Green 	}
417a0d2642eSMika Westerberg }
418a0d2642eSMika Westerberg 
419d5898e19SJarkko Nikula static void cs_assert(struct spi_device *spi)
420ca632f55SGrant Likely {
421d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
422d5898e19SJarkko Nikula 	struct driver_data *drv_data =
423d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
424ca632f55SGrant Likely 
425ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP) {
42696579a4eSJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, chip->frm);
427ca632f55SGrant Likely 		return;
428ca632f55SGrant Likely 	}
429ca632f55SGrant Likely 
430ca632f55SGrant Likely 	if (chip->cs_control) {
431ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_ASSERT);
432ca632f55SGrant Likely 		return;
433ca632f55SGrant Likely 	}
434ca632f55SGrant Likely 
435c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
436c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
437a0d2642eSMika Westerberg 		return;
438a0d2642eSMika Westerberg 	}
439a0d2642eSMika Westerberg 
4407566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
441d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, true);
442ca632f55SGrant Likely }
443ca632f55SGrant Likely 
444d5898e19SJarkko Nikula static void cs_deassert(struct spi_device *spi)
445ca632f55SGrant Likely {
446d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
447d5898e19SJarkko Nikula 	struct driver_data *drv_data =
448d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
449104e51afSJarkko Nikula 	unsigned long timeout;
450ca632f55SGrant Likely 
451ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
452ca632f55SGrant Likely 		return;
453ca632f55SGrant Likely 
454104e51afSJarkko Nikula 	/* Wait until SSP becomes idle before deasserting the CS */
455104e51afSJarkko Nikula 	timeout = jiffies + msecs_to_jiffies(10);
456104e51afSJarkko Nikula 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
457104e51afSJarkko Nikula 	       !time_after(jiffies, timeout))
458104e51afSJarkko Nikula 		cpu_relax();
459104e51afSJarkko Nikula 
460ca632f55SGrant Likely 	if (chip->cs_control) {
461ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_DEASSERT);
462ca632f55SGrant Likely 		return;
463ca632f55SGrant Likely 	}
464ca632f55SGrant Likely 
465c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
466c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
467a0d2642eSMika Westerberg 		return;
468a0d2642eSMika Westerberg 	}
469a0d2642eSMika Westerberg 
4707566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
471d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, false);
472d5898e19SJarkko Nikula }
473d5898e19SJarkko Nikula 
474d5898e19SJarkko Nikula static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
475d5898e19SJarkko Nikula {
476d5898e19SJarkko Nikula 	if (level)
477d5898e19SJarkko Nikula 		cs_deassert(spi);
478d5898e19SJarkko Nikula 	else
479d5898e19SJarkko Nikula 		cs_assert(spi);
480ca632f55SGrant Likely }
481ca632f55SGrant Likely 
482cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
483ca632f55SGrant Likely {
484ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
485ca632f55SGrant Likely 
486ca632f55SGrant Likely 	do {
487c039dd27SJarkko Nikula 		while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
488c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
489c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
490ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
491ca632f55SGrant Likely 
492ca632f55SGrant Likely 	return limit;
493ca632f55SGrant Likely }
494ca632f55SGrant Likely 
49529d7e05cSLubomir Rintel static void pxa2xx_spi_off(struct driver_data *drv_data)
49629d7e05cSLubomir Rintel {
49741c98841SAndy Shevchenko 	/* On MMP, disabling SSE seems to corrupt the Rx FIFO */
49841c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data))
49929d7e05cSLubomir Rintel 		return;
50029d7e05cSLubomir Rintel 
50129d7e05cSLubomir Rintel 	pxa2xx_spi_write(drv_data, SSCR0,
50229d7e05cSLubomir Rintel 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
50329d7e05cSLubomir Rintel }
50429d7e05cSLubomir Rintel 
505ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
506ca632f55SGrant Likely {
507ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
508ca632f55SGrant Likely 
5094fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
510ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
511ca632f55SGrant Likely 		return 0;
512ca632f55SGrant Likely 
513c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
514ca632f55SGrant Likely 	drv_data->tx += n_bytes;
515ca632f55SGrant Likely 
516ca632f55SGrant Likely 	return 1;
517ca632f55SGrant Likely }
518ca632f55SGrant Likely 
519ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
520ca632f55SGrant Likely {
521ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
522ca632f55SGrant Likely 
523c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
524ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
525c039dd27SJarkko Nikula 		pxa2xx_spi_read(drv_data, SSDR);
526ca632f55SGrant Likely 		drv_data->rx += n_bytes;
527ca632f55SGrant Likely 	}
528ca632f55SGrant Likely 
529ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
530ca632f55SGrant Likely }
531ca632f55SGrant Likely 
532ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data)
533ca632f55SGrant Likely {
5344fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
535ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
536ca632f55SGrant Likely 		return 0;
537ca632f55SGrant Likely 
538c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
539ca632f55SGrant Likely 	++drv_data->tx;
540ca632f55SGrant Likely 
541ca632f55SGrant Likely 	return 1;
542ca632f55SGrant Likely }
543ca632f55SGrant Likely 
544ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data)
545ca632f55SGrant Likely {
546c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
547ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
548c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
549ca632f55SGrant Likely 		++drv_data->rx;
550ca632f55SGrant Likely 	}
551ca632f55SGrant Likely 
552ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
553ca632f55SGrant Likely }
554ca632f55SGrant Likely 
555ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
556ca632f55SGrant Likely {
5574fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
558ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
559ca632f55SGrant Likely 		return 0;
560ca632f55SGrant Likely 
561c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
562ca632f55SGrant Likely 	drv_data->tx += 2;
563ca632f55SGrant Likely 
564ca632f55SGrant Likely 	return 1;
565ca632f55SGrant Likely }
566ca632f55SGrant Likely 
567ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
568ca632f55SGrant Likely {
569c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
570ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
571c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
572ca632f55SGrant Likely 		drv_data->rx += 2;
573ca632f55SGrant Likely 	}
574ca632f55SGrant Likely 
575ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
576ca632f55SGrant Likely }
577ca632f55SGrant Likely 
578ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
579ca632f55SGrant Likely {
5804fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
581ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
582ca632f55SGrant Likely 		return 0;
583ca632f55SGrant Likely 
584c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
585ca632f55SGrant Likely 	drv_data->tx += 4;
586ca632f55SGrant Likely 
587ca632f55SGrant Likely 	return 1;
588ca632f55SGrant Likely }
589ca632f55SGrant Likely 
590ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
591ca632f55SGrant Likely {
592c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
593ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
594c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
595ca632f55SGrant Likely 		drv_data->rx += 4;
596ca632f55SGrant Likely 	}
597ca632f55SGrant Likely 
598ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
599ca632f55SGrant Likely }
600ca632f55SGrant Likely 
601ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
602ca632f55SGrant Likely {
60396579a4eSJarkko Nikula 	struct chip_data *chip =
60451eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
605ca632f55SGrant Likely 	u32 sccr1_reg;
606ca632f55SGrant Likely 
607c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
608152bc19eSAndy Shevchenko 	switch (drv_data->ssp_type) {
609152bc19eSAndy Shevchenko 	case QUARK_X1000_SSP:
610152bc19eSAndy Shevchenko 		sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
611152bc19eSAndy Shevchenko 		break;
6127c7289a4SAndy Shevchenko 	case CE4100_SSP:
6137c7289a4SAndy Shevchenko 		sccr1_reg &= ~CE4100_SSCR1_RFT;
6147c7289a4SAndy Shevchenko 		break;
615152bc19eSAndy Shevchenko 	default:
616ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_RFT;
617152bc19eSAndy Shevchenko 		break;
618152bc19eSAndy Shevchenko 	}
619ca632f55SGrant Likely 	sccr1_reg |= chip->threshold;
620c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
621ca632f55SGrant Likely }
622ca632f55SGrant Likely 
623ca632f55SGrant Likely static void int_error_stop(struct driver_data *drv_data, const char *msg)
624ca632f55SGrant Likely {
625ca632f55SGrant Likely 	/* Stop and reset SSP */
626ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
627ca632f55SGrant Likely 	reset_sccr1(drv_data);
628ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
629c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
630cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
63129d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
632ca632f55SGrant Likely 
633c3dce24cSAndy Shevchenko 	dev_err(drv_data->ssp->dev, "%s\n", msg);
634ca632f55SGrant Likely 
63551eea52dSLubomir Rintel 	drv_data->controller->cur_msg->status = -EIO;
63651eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
637ca632f55SGrant Likely }
638ca632f55SGrant Likely 
639ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
640ca632f55SGrant Likely {
64107550df0SJarkko Nikula 	/* Clear and disable interrupts */
642ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
643ca632f55SGrant Likely 	reset_sccr1(drv_data);
644ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
645c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
646ca632f55SGrant Likely 
64751eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
648ca632f55SGrant Likely }
649ca632f55SGrant Likely 
650ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
651ca632f55SGrant Likely {
652c039dd27SJarkko Nikula 	u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
653ca632f55SGrant Likely 		       drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
654ca632f55SGrant Likely 
655c039dd27SJarkko Nikula 	u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
656ca632f55SGrant Likely 
657ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
658ca632f55SGrant Likely 		int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
659ca632f55SGrant Likely 		return IRQ_HANDLED;
660ca632f55SGrant Likely 	}
661ca632f55SGrant Likely 
662ec93cb6fSLubomir Rintel 	if (irq_status & SSSR_TUR) {
663ec93cb6fSLubomir Rintel 		int_error_stop(drv_data, "interrupt_transfer: fifo underrun");
664ec93cb6fSLubomir Rintel 		return IRQ_HANDLED;
665ec93cb6fSLubomir Rintel 	}
666ec93cb6fSLubomir Rintel 
667ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
668c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
669ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
670ca632f55SGrant Likely 			int_transfer_complete(drv_data);
671ca632f55SGrant Likely 			return IRQ_HANDLED;
672ca632f55SGrant Likely 		}
673ca632f55SGrant Likely 	}
674ca632f55SGrant Likely 
675ca632f55SGrant Likely 	/* Drain rx fifo, Fill tx fifo and prevent overruns */
676ca632f55SGrant Likely 	do {
677ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
678ca632f55SGrant Likely 			int_transfer_complete(drv_data);
679ca632f55SGrant Likely 			return IRQ_HANDLED;
680ca632f55SGrant Likely 		}
681ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
682ca632f55SGrant Likely 
683ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
684ca632f55SGrant Likely 		int_transfer_complete(drv_data);
685ca632f55SGrant Likely 		return IRQ_HANDLED;
686ca632f55SGrant Likely 	}
687ca632f55SGrant Likely 
688ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
689ca632f55SGrant Likely 		u32 bytes_left;
690ca632f55SGrant Likely 		u32 sccr1_reg;
691ca632f55SGrant Likely 
692c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
693ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
694ca632f55SGrant Likely 
695ca632f55SGrant Likely 		/*
696ca632f55SGrant Likely 		 * PXA25x_SSP has no timeout, set up rx threshould for the
697ca632f55SGrant Likely 		 * remaining RX bytes.
698ca632f55SGrant Likely 		 */
699ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
7004fdb2424SWeike Chen 			u32 rx_thre;
701ca632f55SGrant Likely 
7024fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
703ca632f55SGrant Likely 
704ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
705ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
706ca632f55SGrant Likely 			case 4:
7072c183376SGustavo A. R. Silva 				bytes_left >>= 2;
7082c183376SGustavo A. R. Silva 				break;
709ca632f55SGrant Likely 			case 2:
710ca632f55SGrant Likely 				bytes_left >>= 1;
7112c183376SGustavo A. R. Silva 				break;
712ca632f55SGrant Likely 			}
713ca632f55SGrant Likely 
7144fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
7154fdb2424SWeike Chen 			if (rx_thre > bytes_left)
7164fdb2424SWeike Chen 				rx_thre = bytes_left;
717ca632f55SGrant Likely 
7184fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
719ca632f55SGrant Likely 		}
720c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
721ca632f55SGrant Likely 	}
722ca632f55SGrant Likely 
723ca632f55SGrant Likely 	/* We did something */
724ca632f55SGrant Likely 	return IRQ_HANDLED;
725ca632f55SGrant Likely }
726ca632f55SGrant Likely 
727b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data)
728b0312482SJan Kiszka {
72929d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
730b0312482SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1,
731b0312482SJan Kiszka 			 pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
732b0312482SJan Kiszka 	if (!pxa25x_ssp_comp(drv_data))
733b0312482SJan Kiszka 		pxa2xx_spi_write(drv_data, SSTO, 0);
734b0312482SJan Kiszka 	write_SSSR_CS(drv_data, drv_data->clear_sr);
735b0312482SJan Kiszka 
736c3dce24cSAndy Shevchenko 	dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
737b0312482SJan Kiszka }
738b0312482SJan Kiszka 
739ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
740ca632f55SGrant Likely {
741ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
7427d94a505SMika Westerberg 	u32 sccr1_reg;
743ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
744ca632f55SGrant Likely 	u32 status;
745ca632f55SGrant Likely 
7467d94a505SMika Westerberg 	/*
7477d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
7487d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
7497d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
7507d94a505SMika Westerberg 	 * interrupt is enabled).
7517d94a505SMika Westerberg 	 */
752c3dce24cSAndy Shevchenko 	if (pm_runtime_suspended(drv_data->ssp->dev))
7537d94a505SMika Westerberg 		return IRQ_NONE;
7547d94a505SMika Westerberg 
755269e4a41SMika Westerberg 	/*
756269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
757269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
758269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
759269e4a41SMika Westerberg 	 * powered off.
760269e4a41SMika Westerberg 	 */
761c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
762269e4a41SMika Westerberg 	if (status == ~0)
763269e4a41SMika Westerberg 		return IRQ_NONE;
764269e4a41SMika Westerberg 
765c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
766ca632f55SGrant Likely 
767ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
768ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
769ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
770ca632f55SGrant Likely 
77102bc933eSTan, Jui Nee 	/* Ignore RX timeout interrupt if it is disabled */
77202bc933eSTan, Jui Nee 	if (!(sccr1_reg & SSCR1_TINTE))
77302bc933eSTan, Jui Nee 		mask &= ~SSSR_TINT;
77402bc933eSTan, Jui Nee 
775ca632f55SGrant Likely 	if (!(status & mask))
776ca632f55SGrant Likely 		return IRQ_NONE;
777ca632f55SGrant Likely 
778e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
779e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
780e51e9b93SJan Kiszka 
78151eea52dSLubomir Rintel 	if (!drv_data->controller->cur_msg) {
782b0312482SJan Kiszka 		handle_bad_msg(drv_data);
783ca632f55SGrant Likely 		/* Never fail */
784ca632f55SGrant Likely 		return IRQ_HANDLED;
785ca632f55SGrant Likely 	}
786ca632f55SGrant Likely 
787ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
788ca632f55SGrant Likely }
789ca632f55SGrant Likely 
790e5262d05SWeike Chen /*
7919df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
7929df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
7939df461ecSAndy Shevchenko  *
7949df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
7959df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
7969df461ecSAndy Shevchenko  *
7979df461ecSAndy Shevchenko  * Fsys = 200MHz
7989df461ecSAndy Shevchenko  *
7999df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
8009df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
8019df461ecSAndy Shevchenko  *
8029df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
8039df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
8049df461ecSAndy Shevchenko  *
8059df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
8069df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
8079df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
8089df461ecSAndy Shevchenko  *       k = [1, 256]
8099df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
8109df461ecSAndy Shevchenko  *
8119df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
8129df461ecSAndy Shevchenko  * are:
8139df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
8149df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
8159df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
8169df461ecSAndy Shevchenko  *
8179df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
8189df461ecSAndy Shevchenko  *
8199df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
8209df461ecSAndy Shevchenko  * to the asked baud rate.
821e5262d05SWeike Chen  */
8229df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
823e5262d05SWeike Chen {
8249df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
8259df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
8269df461ecSAndy Shevchenko 						   see (2) */
8279df461ecSAndy Shevchenko 						/* case 3 */
8289df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
8299df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
8309df461ecSAndy Shevchenko 	unsigned long scale;
8319df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
8329df461ecSAndy Shevchenko 	long r, r1, r2;
8339df461ecSAndy Shevchenko 	u32 mul;
834e5262d05SWeike Chen 
8359df461ecSAndy Shevchenko 	/* Case 1 */
8369df461ecSAndy Shevchenko 
8379df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
8389df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
8399df461ecSAndy Shevchenko 
8409df461ecSAndy Shevchenko 	/* Calculate initial quot */
8413ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
8429df461ecSAndy Shevchenko 
8439df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
8449df461ecSAndy Shevchenko 	if (q1 > 256) {
8459df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
8469df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
8479df461ecSAndy Shevchenko 		if (scale > 9) {
8489df461ecSAndy Shevchenko 			q1 >>= scale - 9;
8499df461ecSAndy Shevchenko 			mul >>= scale - 9;
8509df461ecSAndy Shevchenko 		}
8519df461ecSAndy Shevchenko 
8529df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
8539df461ecSAndy Shevchenko 		q1 += q1 & 1;
8549df461ecSAndy Shevchenko 	}
8559df461ecSAndy Shevchenko 
8569df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
8579df461ecSAndy Shevchenko 	scale = __ffs(q1);
8589df461ecSAndy Shevchenko 	q1 >>= scale;
8599df461ecSAndy Shevchenko 	mul >>= scale;
8609df461ecSAndy Shevchenko 
8619df461ecSAndy Shevchenko 	/* Get the remainder */
8629df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
8639df461ecSAndy Shevchenko 
8649df461ecSAndy Shevchenko 	/* Case 2 */
8659df461ecSAndy Shevchenko 
8663ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
8679df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
8689df461ecSAndy Shevchenko 
8699df461ecSAndy Shevchenko 	/*
8709df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
8719df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
8729df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
8739df461ecSAndy Shevchenko 	 */
8749df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
8759df461ecSAndy Shevchenko 		/* case 1 is better */
8769df461ecSAndy Shevchenko 		r = r1;
8779df461ecSAndy Shevchenko 		q = q1;
8789df461ecSAndy Shevchenko 	} else {
8799df461ecSAndy Shevchenko 		/* case 2 is better */
8809df461ecSAndy Shevchenko 		r = r2;
8819df461ecSAndy Shevchenko 		q = q2;
8829df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
8839df461ecSAndy Shevchenko 	}
8849df461ecSAndy Shevchenko 
8853ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
8869df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
8879df461ecSAndy Shevchenko 		u64 fssp;
8889df461ecSAndy Shevchenko 		u32 m;
8899df461ecSAndy Shevchenko 
8909df461ecSAndy Shevchenko 		/* Calculate initial quot */
8913ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
8929df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
8939df461ecSAndy Shevchenko 
8949df461ecSAndy Shevchenko 		/* Get the remainder */
8959df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
8969df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
8979df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
8989df461ecSAndy Shevchenko 
8999df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
9009df461ecSAndy Shevchenko 		if (r1 < r) {
9019df461ecSAndy Shevchenko 			/* case 3 is better */
9029df461ecSAndy Shevchenko 			q = 1;
9039df461ecSAndy Shevchenko 			mul = m;
904e5262d05SWeike Chen 		}
905e5262d05SWeike Chen 	}
906e5262d05SWeike Chen 
9079df461ecSAndy Shevchenko 	*dds = mul;
9089df461ecSAndy Shevchenko 	return q - 1;
909e5262d05SWeike Chen }
910e5262d05SWeike Chen 
9113343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
912ca632f55SGrant Likely {
91351eea52dSLubomir Rintel 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
9143343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
9153343b7a6SMika Westerberg 
9163343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
917ca632f55SGrant Likely 
91829f21337SFlavio Suligoi 	/*
91929f21337SFlavio Suligoi 	 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
92029f21337SFlavio Suligoi 	 * that the SSP transmission rate can be greater than the device rate
92129f21337SFlavio Suligoi 	 */
922ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
92329f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
924ca632f55SGrant Likely 	else
92529f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
926ca632f55SGrant Likely }
927ca632f55SGrant Likely 
928e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
929d2c2f6a4SAndy Shevchenko 					   int rate)
930e5262d05SWeike Chen {
93196579a4eSJarkko Nikula 	struct chip_data *chip =
93251eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
933025ffe88SAndy Shevchenko 	unsigned int clk_div;
934e5262d05SWeike Chen 
935e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
936e5262d05SWeike Chen 	case QUARK_X1000_SSP:
9379df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
938eecacf73SDan Carpenter 		break;
939e5262d05SWeike Chen 	default:
940025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
941eecacf73SDan Carpenter 		break;
942e5262d05SWeike Chen 	}
943025ffe88SAndy Shevchenko 	return clk_div << 8;
944e5262d05SWeike Chen }
945e5262d05SWeike Chen 
94651eea52dSLubomir Rintel static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
947b6ced294SJarkko Nikula 			       struct spi_device *spi,
948b6ced294SJarkko Nikula 			       struct spi_transfer *xfer)
949b6ced294SJarkko Nikula {
950b6ced294SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
951b6ced294SJarkko Nikula 
952b6ced294SJarkko Nikula 	return chip->enable_dma &&
953b6ced294SJarkko Nikula 	       xfer->len <= MAX_DMA_LEN &&
954b6ced294SJarkko Nikula 	       xfer->len >= chip->dma_burst_size;
955b6ced294SJarkko Nikula }
956b6ced294SJarkko Nikula 
95751eea52dSLubomir Rintel static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
958d5898e19SJarkko Nikula 				   struct spi_device *spi,
959d5898e19SJarkko Nikula 				   struct spi_transfer *transfer)
960ca632f55SGrant Likely {
96151eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
96251eea52dSLubomir Rintel 	struct spi_message *message = controller->cur_msg;
96320f4c379SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
96496579a4eSJarkko Nikula 	u32 dma_thresh = chip->dma_threshold;
96596579a4eSJarkko Nikula 	u32 dma_burst = chip->dma_burst_size;
96696579a4eSJarkko Nikula 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
967bffc967eSJarkko Nikula 	u32 clk_div;
968bffc967eSJarkko Nikula 	u8 bits;
969bffc967eSJarkko Nikula 	u32 speed;
970ca632f55SGrant Likely 	u32 cr0;
971ca632f55SGrant Likely 	u32 cr1;
9727d1f1bf6SAndy Shevchenko 	int err;
973b6ced294SJarkko Nikula 	int dma_mapped;
974ca632f55SGrant Likely 
975cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
976b6ced294SJarkko Nikula 	if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
977ca632f55SGrant Likely 
978ca632f55SGrant Likely 		/* reject already-mapped transfers; PIO won't always work */
979ca632f55SGrant Likely 		if (message->is_dma_mapped
980ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
981748fbadfSJarkko Nikula 			dev_err(&spi->dev,
9828ae55af3SJarkko Nikula 				"Mapped transfer length of %u is greater than %d\n",
983ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
984d5898e19SJarkko Nikula 			return -EINVAL;
985ca632f55SGrant Likely 		}
986ca632f55SGrant Likely 
987ca632f55SGrant Likely 		/* warn ... we force this to PIO mode */
98820f4c379SJarkko Nikula 		dev_warn_ratelimited(&spi->dev,
9898ae55af3SJarkko Nikula 				     "DMA disabled for transfer length %ld greater than %d\n",
990d5898e19SJarkko Nikula 				     (long)transfer->len, MAX_DMA_LEN);
991ca632f55SGrant Likely 	}
992ca632f55SGrant Likely 
993ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
994cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
995748fbadfSJarkko Nikula 		dev_err(&spi->dev, "Flush failed\n");
996d5898e19SJarkko Nikula 		return -EIO;
997ca632f55SGrant Likely 	}
998ca632f55SGrant Likely 	drv_data->n_bytes = chip->n_bytes;
999ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
1000ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
1001ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
1002ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
1003ca632f55SGrant Likely 	drv_data->write = drv_data->tx ? chip->write : null_writer;
1004ca632f55SGrant Likely 	drv_data->read = drv_data->rx ? chip->read : null_reader;
1005ca632f55SGrant Likely 
1006ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
1007ca632f55SGrant Likely 	bits = transfer->bits_per_word;
1008ca632f55SGrant Likely 	speed = transfer->speed_hz;
1009ca632f55SGrant Likely 
1010d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
1011ca632f55SGrant Likely 
1012ca632f55SGrant Likely 	if (bits <= 8) {
1013ca632f55SGrant Likely 		drv_data->n_bytes = 1;
1014ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1015ca632f55SGrant Likely 					u8_reader : null_reader;
1016ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1017ca632f55SGrant Likely 					u8_writer : null_writer;
1018ca632f55SGrant Likely 	} else if (bits <= 16) {
1019ca632f55SGrant Likely 		drv_data->n_bytes = 2;
1020ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1021ca632f55SGrant Likely 					u16_reader : null_reader;
1022ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1023ca632f55SGrant Likely 					u16_writer : null_writer;
1024ca632f55SGrant Likely 	} else if (bits <= 32) {
1025ca632f55SGrant Likely 		drv_data->n_bytes = 4;
1026ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1027ca632f55SGrant Likely 					u32_reader : null_reader;
1028ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1029ca632f55SGrant Likely 					u32_writer : null_writer;
1030ca632f55SGrant Likely 	}
1031196b0e2cSJarkko Nikula 	/*
1032196b0e2cSJarkko Nikula 	 * if bits/word is changed in dma mode, then must check the
1033196b0e2cSJarkko Nikula 	 * thresholds and burst also
1034196b0e2cSJarkko Nikula 	 */
1035ca632f55SGrant Likely 	if (chip->enable_dma) {
1036cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
103720f4c379SJarkko Nikula 						spi,
1038ca632f55SGrant Likely 						bits, &dma_burst,
1039ca632f55SGrant Likely 						&dma_thresh))
104020f4c379SJarkko Nikula 			dev_warn_ratelimited(&spi->dev,
10418ae55af3SJarkko Nikula 					     "DMA burst size reduced to match bits_per_word\n");
1042ca632f55SGrant Likely 	}
1043ca632f55SGrant Likely 
104451eea52dSLubomir Rintel 	dma_mapped = controller->can_dma &&
104520f4c379SJarkko Nikula 		     controller->can_dma(controller, spi, transfer) &&
104651eea52dSLubomir Rintel 		     controller->cur_msg_mapped;
1047b6ced294SJarkko Nikula 	if (dma_mapped) {
1048ca632f55SGrant Likely 
1049ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
1050cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1051ca632f55SGrant Likely 
1052d5898e19SJarkko Nikula 		err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1053d5898e19SJarkko Nikula 		if (err)
1054d5898e19SJarkko Nikula 			return err;
1055ca632f55SGrant Likely 
1056ca632f55SGrant Likely 		/* Clear status and start DMA engine */
1057ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1058c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1059cd7bed00SMika Westerberg 
1060cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
1061ca632f55SGrant Likely 	} else {
1062ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
1063ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
1064ca632f55SGrant Likely 
1065ca632f55SGrant Likely 		/* Clear status  */
1066ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1067ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1068ca632f55SGrant Likely 	}
1069ca632f55SGrant Likely 
1070ee03672dSJarkko Nikula 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1071ee03672dSJarkko Nikula 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1072ee03672dSJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
107320f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
107451eea52dSLubomir Rintel 			controller->max_speed_hz
1075ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1076b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1077ee03672dSJarkko Nikula 	else
107820f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
107951eea52dSLubomir Rintel 			controller->max_speed_hz / 2
1080ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1081b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1082ee03672dSJarkko Nikula 
1083a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
1084c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
1085c039dd27SJarkko Nikula 		    != chip->lpss_rx_threshold)
1086c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSIRF,
1087c039dd27SJarkko Nikula 					 chip->lpss_rx_threshold);
1088c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
1089c039dd27SJarkko Nikula 		    != chip->lpss_tx_threshold)
1090c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSITF,
1091c039dd27SJarkko Nikula 					 chip->lpss_tx_threshold);
1092a0d2642eSMika Westerberg 	}
1093a0d2642eSMika Westerberg 
1094e5262d05SWeike Chen 	if (is_quark_x1000_ssp(drv_data) &&
1095c039dd27SJarkko Nikula 	    (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
1096c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
1097e5262d05SWeike Chen 
1098ca632f55SGrant Likely 	/* see if we need to reload the config registers */
1099c039dd27SJarkko Nikula 	if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1100c039dd27SJarkko Nikula 	    || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1101c039dd27SJarkko Nikula 	    != (cr1 & change_mask)) {
1102ca632f55SGrant Likely 		/* stop the SSP, and update the other bits */
110341c98841SAndy Shevchenko 		if (!is_mmp2_ssp(drv_data))
1104c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
1105ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1106c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1107ca632f55SGrant Likely 		/* first set CR1 without interrupt and service enables */
1108c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
1109ca632f55SGrant Likely 		/* restart the SSP */
1110c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0);
1111ca632f55SGrant Likely 
1112ca632f55SGrant Likely 	} else {
1113ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1114c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1115ca632f55SGrant Likely 	}
1116ca632f55SGrant Likely 
111741c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data)) {
111882391856SLubomir Rintel 		u8 tx_level = (pxa2xx_spi_read(drv_data, SSSR)
111982391856SLubomir Rintel 					& SSSR_TFL_MASK) >> 8;
112082391856SLubomir Rintel 
112182391856SLubomir Rintel 		if (tx_level) {
112282391856SLubomir Rintel 			/* On MMP2, flipping SSE doesn't to empty TXFIFO. */
112382391856SLubomir Rintel 			dev_warn(&spi->dev, "%d bytes of garbage in TXFIFO!\n",
112482391856SLubomir Rintel 								tx_level);
112582391856SLubomir Rintel 			if (tx_level > transfer->len)
112682391856SLubomir Rintel 				tx_level = transfer->len;
112782391856SLubomir Rintel 			drv_data->tx += tx_level;
112882391856SLubomir Rintel 		}
112982391856SLubomir Rintel 	}
113082391856SLubomir Rintel 
113151eea52dSLubomir Rintel 	if (spi_controller_is_slave(controller)) {
1132ec93cb6fSLubomir Rintel 		while (drv_data->write(drv_data))
1133ec93cb6fSLubomir Rintel 			;
113477d33897SLubomir Rintel 		if (drv_data->gpiod_ready) {
113577d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 1);
113677d33897SLubomir Rintel 			udelay(1);
113777d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 0);
113877d33897SLubomir Rintel 		}
1139ec93cb6fSLubomir Rintel 	}
1140ec93cb6fSLubomir Rintel 
1141d5898e19SJarkko Nikula 	/*
1142d5898e19SJarkko Nikula 	 * Release the data by enabling service requests and interrupts,
1143d5898e19SJarkko Nikula 	 * without changing any mode bits
1144d5898e19SJarkko Nikula 	 */
1145c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1146d5898e19SJarkko Nikula 
1147d5898e19SJarkko Nikula 	return 1;
1148ca632f55SGrant Likely }
1149ca632f55SGrant Likely 
115051eea52dSLubomir Rintel static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
1151ec93cb6fSLubomir Rintel {
115251eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1153ec93cb6fSLubomir Rintel 
1154ec93cb6fSLubomir Rintel 	/* Stop and reset SSP */
1155ec93cb6fSLubomir Rintel 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1156ec93cb6fSLubomir Rintel 	reset_sccr1(drv_data);
1157ec93cb6fSLubomir Rintel 	if (!pxa25x_ssp_comp(drv_data))
1158ec93cb6fSLubomir Rintel 		pxa2xx_spi_write(drv_data, SSTO, 0);
1159ec93cb6fSLubomir Rintel 	pxa2xx_spi_flush(drv_data);
116029d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
1161ec93cb6fSLubomir Rintel 
1162c3dce24cSAndy Shevchenko 	dev_dbg(drv_data->ssp->dev, "transfer aborted\n");
1163ec93cb6fSLubomir Rintel 
116451eea52dSLubomir Rintel 	drv_data->controller->cur_msg->status = -EINTR;
116551eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
1166ec93cb6fSLubomir Rintel 
1167ec93cb6fSLubomir Rintel 	return 0;
1168ec93cb6fSLubomir Rintel }
1169ec93cb6fSLubomir Rintel 
117051eea52dSLubomir Rintel static void pxa2xx_spi_handle_err(struct spi_controller *controller,
11717f86bde9SMika Westerberg 				 struct spi_message *msg)
1172ca632f55SGrant Likely {
117351eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1174ca632f55SGrant Likely 
1175d5898e19SJarkko Nikula 	/* Disable the SSP */
117629d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
1177d5898e19SJarkko Nikula 	/* Clear and disable interrupts and service requests */
1178d5898e19SJarkko Nikula 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1179d5898e19SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1,
1180d5898e19SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR1)
1181d5898e19SJarkko Nikula 			 & ~(drv_data->int_cr1 | drv_data->dma_cr1));
1182d5898e19SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
1183d5898e19SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1184ca632f55SGrant Likely 
1185d5898e19SJarkko Nikula 	/*
1186d5898e19SJarkko Nikula 	 * Stop the DMA if running. Note DMA callback handler may have unset
1187d5898e19SJarkko Nikula 	 * the dma_running already, which is fine as stopping is not needed
1188d5898e19SJarkko Nikula 	 * then but we shouldn't rely this flag for anything else than
1189d5898e19SJarkko Nikula 	 * stopping. For instance to differentiate between PIO and DMA
1190d5898e19SJarkko Nikula 	 * transfers.
1191d5898e19SJarkko Nikula 	 */
1192d5898e19SJarkko Nikula 	if (atomic_read(&drv_data->dma_running))
1193d5898e19SJarkko Nikula 		pxa2xx_spi_dma_stop(drv_data);
1194ca632f55SGrant Likely }
1195ca632f55SGrant Likely 
119651eea52dSLubomir Rintel static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
11977d94a505SMika Westerberg {
119851eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
11997d94a505SMika Westerberg 
12007d94a505SMika Westerberg 	/* Disable the SSP now */
120129d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
12027d94a505SMika Westerberg 
12037d94a505SMika Westerberg 	return 0;
12047d94a505SMika Westerberg }
12057d94a505SMika Westerberg 
1206ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1207ca632f55SGrant Likely 		    struct pxa2xx_spi_chip *chip_info)
1208ca632f55SGrant Likely {
12093cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12103cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1211c18d925fSJan Kiszka 	struct gpio_desc *gpiod;
1212ca632f55SGrant Likely 	int err = 0;
1213ca632f55SGrant Likely 
121499f499cdSMika Westerberg 	if (chip == NULL)
121599f499cdSMika Westerberg 		return 0;
121699f499cdSMika Westerberg 
12176ac5a435SAndy Shevchenko 	if (drv_data->cs_gpiods) {
12186ac5a435SAndy Shevchenko 		gpiod = drv_data->cs_gpiods[spi->chip_select];
12196ac5a435SAndy Shevchenko 		if (gpiod) {
1220c18d925fSJan Kiszka 			chip->gpiod_cs = gpiod;
122199f499cdSMika Westerberg 			chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
122299f499cdSMika Westerberg 			gpiod_set_value(gpiod, chip->gpio_cs_inverted);
12236ac5a435SAndy Shevchenko 		}
122499f499cdSMika Westerberg 
122599f499cdSMika Westerberg 		return 0;
122699f499cdSMika Westerberg 	}
122799f499cdSMika Westerberg 
122899f499cdSMika Westerberg 	if (chip_info == NULL)
1229ca632f55SGrant Likely 		return 0;
1230ca632f55SGrant Likely 
1231ca632f55SGrant Likely 	/* NOTE: setup() can be called multiple times, possibly with
1232ca632f55SGrant Likely 	 * different chip_info, release previously requested GPIO
1233ca632f55SGrant Likely 	 */
1234c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
1235a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1236c18d925fSJan Kiszka 		chip->gpiod_cs = NULL;
1237c18d925fSJan Kiszka 	}
1238ca632f55SGrant Likely 
1239ca632f55SGrant Likely 	/* If (*cs_control) is provided, ignore GPIO chip select */
1240ca632f55SGrant Likely 	if (chip_info->cs_control) {
1241ca632f55SGrant Likely 		chip->cs_control = chip_info->cs_control;
1242ca632f55SGrant Likely 		return 0;
1243ca632f55SGrant Likely 	}
1244ca632f55SGrant Likely 
1245ca632f55SGrant Likely 	if (gpio_is_valid(chip_info->gpio_cs)) {
1246ca632f55SGrant Likely 		err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1247ca632f55SGrant Likely 		if (err) {
1248f6bd03a7SJarkko Nikula 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1249f6bd03a7SJarkko Nikula 				chip_info->gpio_cs);
1250ca632f55SGrant Likely 			return err;
1251ca632f55SGrant Likely 		}
1252ca632f55SGrant Likely 
1253c18d925fSJan Kiszka 		gpiod = gpio_to_desc(chip_info->gpio_cs);
1254c18d925fSJan Kiszka 		chip->gpiod_cs = gpiod;
1255ca632f55SGrant Likely 		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1256ca632f55SGrant Likely 
1257c18d925fSJan Kiszka 		err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
1258ca632f55SGrant Likely 	}
1259ca632f55SGrant Likely 
1260ca632f55SGrant Likely 	return err;
1261ca632f55SGrant Likely }
1262ca632f55SGrant Likely 
1263ca632f55SGrant Likely static int setup(struct spi_device *spi)
1264ca632f55SGrant Likely {
1265bffc967eSJarkko Nikula 	struct pxa2xx_spi_chip *chip_info;
1266ca632f55SGrant Likely 	struct chip_data *chip;
1267dccf7369SJarkko Nikula 	const struct lpss_config *config;
12683cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12693cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1270a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1271a0d2642eSMika Westerberg 
1272e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1273e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1274e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1275e5262d05SWeike Chen 		tx_hi_thres = 0;
1276e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1277e5262d05SWeike Chen 		break;
12787c7289a4SAndy Shevchenko 	case CE4100_SSP:
12797c7289a4SAndy Shevchenko 		tx_thres = TX_THRESH_CE4100_DFLT;
12807c7289a4SAndy Shevchenko 		tx_hi_thres = 0;
12817c7289a4SAndy Shevchenko 		rx_thres = RX_THRESH_CE4100_DFLT;
12827c7289a4SAndy Shevchenko 		break;
128303fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
128403fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
128530f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
128634cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1287b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1288fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
1289dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1290dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1291dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1292dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1293e5262d05SWeike Chen 		break;
1294e5262d05SWeike Chen 	default:
1295a0d2642eSMika Westerberg 		tx_hi_thres = 0;
129651eea52dSLubomir Rintel 		if (spi_controller_is_slave(drv_data->controller)) {
1297ec93cb6fSLubomir Rintel 			tx_thres = 1;
1298ec93cb6fSLubomir Rintel 			rx_thres = 2;
1299ec93cb6fSLubomir Rintel 		} else {
1300ec93cb6fSLubomir Rintel 			tx_thres = TX_THRESH_DFLT;
1301a0d2642eSMika Westerberg 			rx_thres = RX_THRESH_DFLT;
1302ec93cb6fSLubomir Rintel 		}
1303e5262d05SWeike Chen 		break;
1304a0d2642eSMika Westerberg 	}
1305ca632f55SGrant Likely 
1306ca632f55SGrant Likely 	/* Only alloc on first setup */
1307ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1308ca632f55SGrant Likely 	if (!chip) {
1309ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
13109deae459SJingoo Han 		if (!chip)
1311ca632f55SGrant Likely 			return -ENOMEM;
1312ca632f55SGrant Likely 
1313ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1314ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1315f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1316f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1317ca632f55SGrant Likely 				kfree(chip);
1318ca632f55SGrant Likely 				return -EINVAL;
1319ca632f55SGrant Likely 			}
1320ca632f55SGrant Likely 
1321ca632f55SGrant Likely 			chip->frm = spi->chip_select;
1322c18d925fSJan Kiszka 		}
132351eea52dSLubomir Rintel 		chip->enable_dma = drv_data->controller_info->enable_dma;
1324ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1325ca632f55SGrant Likely 	}
1326ca632f55SGrant Likely 
1327ca632f55SGrant Likely 	/* protocol drivers may change the chip settings, so...
1328ca632f55SGrant Likely 	 * if chip_info exists, use it */
1329ca632f55SGrant Likely 	chip_info = spi->controller_data;
1330ca632f55SGrant Likely 
1331ca632f55SGrant Likely 	/* chip_info isn't always needed */
1332ca632f55SGrant Likely 	chip->cr1 = 0;
1333ca632f55SGrant Likely 	if (chip_info) {
1334ca632f55SGrant Likely 		if (chip_info->timeout)
1335ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1336ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1337ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1338a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1339a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1340ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1341ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1342ca632f55SGrant Likely 		chip->dma_threshold = 0;
1343ca632f55SGrant Likely 		if (chip_info->enable_loopback)
1344ca632f55SGrant Likely 			chip->cr1 = SSCR1_LBM;
1345ca632f55SGrant Likely 	}
134651eea52dSLubomir Rintel 	if (spi_controller_is_slave(drv_data->controller)) {
1347ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCFR;
1348ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCLKDIR;
1349ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SFRMDIR;
1350ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SPH;
1351ec93cb6fSLubomir Rintel 	}
1352ca632f55SGrant Likely 
1353a0d2642eSMika Westerberg 	chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1354a0d2642eSMika Westerberg 	chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1355a0d2642eSMika Westerberg 				| SSITF_TxHiThresh(tx_hi_thres);
1356a0d2642eSMika Westerberg 
1357ca632f55SGrant Likely 	/* set dma burst and threshold outside of chip_info path so that if
1358ca632f55SGrant Likely 	 * chip_info goes away after setting chip->enable_dma, the
1359ca632f55SGrant Likely 	 * burst and threshold can still respond to changes in bits_per_word */
1360ca632f55SGrant Likely 	if (chip->enable_dma) {
1361ca632f55SGrant Likely 		/* set up legal burst and threshold for dma */
1362cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1363cd7bed00SMika Westerberg 						spi->bits_per_word,
1364ca632f55SGrant Likely 						&chip->dma_burst_size,
1365ca632f55SGrant Likely 						&chip->dma_threshold)) {
1366f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1367f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1368ca632f55SGrant Likely 		}
1369000c6af4SAndy Shevchenko 		dev_dbg(&spi->dev,
1370000c6af4SAndy Shevchenko 			"in setup: DMA burst size set to %u\n",
1371000c6af4SAndy Shevchenko 			chip->dma_burst_size);
1372ca632f55SGrant Likely 	}
1373ca632f55SGrant Likely 
1374e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1375e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1376e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1377e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1378e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1379e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1380e5262d05SWeike Chen 		break;
13817c7289a4SAndy Shevchenko 	case CE4100_SSP:
13827c7289a4SAndy Shevchenko 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
13837c7289a4SAndy Shevchenko 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
13847c7289a4SAndy Shevchenko 		break;
1385e5262d05SWeike Chen 	default:
1386e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1387e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1388e5262d05SWeike Chen 		break;
1389e5262d05SWeike Chen 	}
1390e5262d05SWeike Chen 
1391ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1392ca632f55SGrant Likely 	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1393ca632f55SGrant Likely 			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1394ca632f55SGrant Likely 
1395b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1396b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1397b833172fSMika Westerberg 
1398ca632f55SGrant Likely 	if (spi->bits_per_word <= 8) {
1399ca632f55SGrant Likely 		chip->n_bytes = 1;
1400ca632f55SGrant Likely 		chip->read = u8_reader;
1401ca632f55SGrant Likely 		chip->write = u8_writer;
1402ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 16) {
1403ca632f55SGrant Likely 		chip->n_bytes = 2;
1404ca632f55SGrant Likely 		chip->read = u16_reader;
1405ca632f55SGrant Likely 		chip->write = u16_writer;
1406ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 32) {
1407ca632f55SGrant Likely 		chip->n_bytes = 4;
1408ca632f55SGrant Likely 		chip->read = u32_reader;
1409ca632f55SGrant Likely 		chip->write = u32_writer;
1410ca632f55SGrant Likely 	}
1411ca632f55SGrant Likely 
1412ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1413ca632f55SGrant Likely 
1414ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
1415ca632f55SGrant Likely 		return 0;
1416ca632f55SGrant Likely 
1417ca632f55SGrant Likely 	return setup_cs(spi, chip, chip_info);
1418ca632f55SGrant Likely }
1419ca632f55SGrant Likely 
1420ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1421ca632f55SGrant Likely {
1422ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
14233cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
14243cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1425ca632f55SGrant Likely 
1426ca632f55SGrant Likely 	if (!chip)
1427ca632f55SGrant Likely 		return;
1428ca632f55SGrant Likely 
14296ac5a435SAndy Shevchenko 	if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
1430c18d925fSJan Kiszka 	    chip->gpiod_cs)
1431a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1432ca632f55SGrant Likely 
1433ca632f55SGrant Likely 	kfree(chip);
1434ca632f55SGrant Likely }
1435ca632f55SGrant Likely 
14369b2d6119SLee Jones #ifdef CONFIG_ACPI
14378422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
143803fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
143903fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
144003fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
144103fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
144203fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
144330f3a6abSMika Westerberg 	{ "8086228E", LPSS_BSW_SSP },
144403fbf488SJarkko Nikula 	{ },
144503fbf488SJarkko Nikula };
144603fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
14479b2d6119SLee Jones #endif
144803fbf488SJarkko Nikula 
144934cadd9cSJarkko Nikula /*
145034cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
145134cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
145234cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
145334cadd9cSJarkko Nikula  */
145434cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
145534cadd9cSJarkko Nikula 	/* SPT-LP */
145634cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
145734cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
145834cadd9cSJarkko Nikula 	/* SPT-H */
145934cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
146034cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1461704d2b07SMika Westerberg 	/* KBL-H */
1462704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1463704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
14646157d4c2SJarkko Nikula 	/* CML-V */
14656157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
14666157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
1467c1b03f11SJarkko Nikula 	/* BXT A-Step */
1468b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1469b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1470b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1471c1b03f11SJarkko Nikula 	/* BXT B-Step */
1472c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1473c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1474c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1475e18a80acSDavid E. Box 	/* GLK */
1476e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1477e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1478e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
147922d71a50SMika Westerberg 	/* ICL-LP */
148022d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
148122d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
148222d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
14838cc77204SJarkko Nikula 	/* EHL */
14848cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
14858cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
14868cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
14879c7315c9SJarkko Nikula 	/* JSL */
14889c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
14899c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
14909c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
1491cf961fceSJarkko Nikula 	/* TGL-H */
1492cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
1493cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
1494cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
1495cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
1496a402e397SJarkko Nikula 	/* ADL-P */
1497a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51aa), LPSS_CNL_SSP },
1498a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51ab), LPSS_CNL_SSP },
1499a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51fb), LPSS_CNL_SSP },
15008c4ffe4dSJarkko Nikula 	/* ADL-M */
15018c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54aa), LPSS_CNL_SSP },
15028c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54ab), LPSS_CNL_SSP },
15038c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54fb), LPSS_CNL_SSP },
1504b7c08cf8SJarkko Nikula 	/* APL */
1505b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1506b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1507b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1508b8450e01SJarkko Nikula 	/* ADL-S */
1509b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7aaa), LPSS_CNL_SSP },
1510b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7aab), LPSS_CNL_SSP },
1511b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7af9), LPSS_CNL_SSP },
1512b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7afb), LPSS_CNL_SSP },
1513fc0b2accSJarkko Nikula 	/* CNL-LP */
1514fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1515fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1516fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1517fc0b2accSJarkko Nikula 	/* CNL-H */
1518fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1519fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1520fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
152141a91802SEvan Green 	/* CML-LP */
152241a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
152341a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
152441a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
1525f0cf17edSJarkko Nikula 	/* CML-H */
1526f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1527f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1528f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
1529a4127952SJarkko Nikula 	/* TGL-LP */
1530a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1531a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1532a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1533a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1534a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1535a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1536a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
153794e5c23dSAxel Lin 	{ },
153834cadd9cSJarkko Nikula };
153934cadd9cSJarkko Nikula 
154087ae1d2dSLubomir Rintel static const struct of_device_id pxa2xx_spi_of_match[] = {
154187ae1d2dSLubomir Rintel 	{ .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
154287ae1d2dSLubomir Rintel 	{},
154387ae1d2dSLubomir Rintel };
154487ae1d2dSLubomir Rintel MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
154587ae1d2dSLubomir Rintel 
154687ae1d2dSLubomir Rintel #ifdef CONFIG_ACPI
154787ae1d2dSLubomir Rintel 
1548365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
154987ae1d2dSLubomir Rintel {
1550365e856eSAndy Shevchenko 	struct acpi_device *adev;
155187ae1d2dSLubomir Rintel 	unsigned int devid;
155287ae1d2dSLubomir Rintel 	int port_id = -1;
155387ae1d2dSLubomir Rintel 
1554365e856eSAndy Shevchenko 	adev = ACPI_COMPANION(dev);
155587ae1d2dSLubomir Rintel 	if (adev && adev->pnp.unique_id &&
155687ae1d2dSLubomir Rintel 	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
155787ae1d2dSLubomir Rintel 		port_id = devid;
155887ae1d2dSLubomir Rintel 	return port_id;
155987ae1d2dSLubomir Rintel }
156087ae1d2dSLubomir Rintel 
156187ae1d2dSLubomir Rintel #else /* !CONFIG_ACPI */
156287ae1d2dSLubomir Rintel 
1563365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
156487ae1d2dSLubomir Rintel {
156587ae1d2dSLubomir Rintel 	return -1;
156687ae1d2dSLubomir Rintel }
156787ae1d2dSLubomir Rintel 
156887ae1d2dSLubomir Rintel #endif /* CONFIG_ACPI */
156987ae1d2dSLubomir Rintel 
157087ae1d2dSLubomir Rintel 
157187ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
157287ae1d2dSLubomir Rintel 
157334cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
157434cadd9cSJarkko Nikula {
15755ba846b1SAndy Shevchenko 	return param == chan->device->dev;
157634cadd9cSJarkko Nikula }
157734cadd9cSJarkko Nikula 
157887ae1d2dSLubomir Rintel #endif /* CONFIG_PCI */
157987ae1d2dSLubomir Rintel 
158051eea52dSLubomir Rintel static struct pxa2xx_spi_controller *
15810db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1582a3496855SMika Westerberg {
158351eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *pdata;
1584a3496855SMika Westerberg 	struct ssp_device *ssp;
1585a3496855SMika Westerberg 	struct resource *res;
15866fb7427dSAndy Shevchenko 	struct device *parent = pdev->dev.parent;
15876fb7427dSAndy Shevchenko 	struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
158834cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
158955ef8262SLubomir Rintel 	enum pxa_ssp_type type;
1590f2faa3ecSAndy Shevchenko 	const void *match;
1591a3496855SMika Westerberg 
15926fb7427dSAndy Shevchenko 	if (pcidev)
15936fb7427dSAndy Shevchenko 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
1594a3496855SMika Westerberg 
1595f2faa3ecSAndy Shevchenko 	match = device_get_match_data(&pdev->dev);
1596f2faa3ecSAndy Shevchenko 	if (match)
1597f2faa3ecSAndy Shevchenko 		type = (enum pxa_ssp_type)match;
159834cadd9cSJarkko Nikula 	else if (pcidev_id)
159955ef8262SLubomir Rintel 		type = (enum pxa_ssp_type)pcidev_id->driver_data;
160003fbf488SJarkko Nikula 	else
160114af1df3SAndy Shevchenko 		return ERR_PTR(-EINVAL);
160203fbf488SJarkko Nikula 
1603cc0ee987SMika Westerberg 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
16049deae459SJingoo Han 	if (!pdata)
160514af1df3SAndy Shevchenko 		return ERR_PTR(-ENOMEM);
1606a3496855SMika Westerberg 
1607a3496855SMika Westerberg 	ssp = &pdata->ssp;
1608a3496855SMika Westerberg 
160977c544d2SAndy Shevchenko 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1610cbfd6a21SSachin Kamat 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1611cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
161214af1df3SAndy Shevchenko 		return ERR_CAST(ssp->mmio_base);
1613a3496855SMika Westerberg 
161477c544d2SAndy Shevchenko 	ssp->phys_base = res->start;
161577c544d2SAndy Shevchenko 
161687ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
161734cadd9cSJarkko Nikula 	if (pcidev_id) {
16186fb7427dSAndy Shevchenko 		pdata->tx_param = parent;
16196fb7427dSAndy Shevchenko 		pdata->rx_param = parent;
162034cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
162134cadd9cSJarkko Nikula 	}
162287ae1d2dSLubomir Rintel #endif
162334cadd9cSJarkko Nikula 
1624a3496855SMika Westerberg 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
16255eb263efSChuhong Yuan 	if (IS_ERR(ssp->clk))
162614af1df3SAndy Shevchenko 		return ERR_CAST(ssp->clk);
1627a3496855SMika Westerberg 
1628a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
16295eb263efSChuhong Yuan 	if (ssp->irq < 0)
163014af1df3SAndy Shevchenko 		return ERR_PTR(ssp->irq);
16315eb263efSChuhong Yuan 
1632a3496855SMika Westerberg 	ssp->type = type;
16334f3d9577SAndy Shevchenko 	ssp->dev = &pdev->dev;
1634365e856eSAndy Shevchenko 	ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
1635a3496855SMika Westerberg 
1636f2faa3ecSAndy Shevchenko 	pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
1637a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1638cddb339bSMika Westerberg 	pdata->enable_dma = true;
163937821a82SAndy Shevchenko 	pdata->dma_burst_size = 1;
1640a3496855SMika Westerberg 
1641a3496855SMika Westerberg 	return pdata;
1642a3496855SMika Westerberg }
1643a3496855SMika Westerberg 
164451eea52dSLubomir Rintel static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
16453cc7b0e3SJarkko Nikula 				      unsigned int cs)
16460c27d9cfSMika Westerberg {
164751eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
16480c27d9cfSMika Westerberg 
1649c3dce24cSAndy Shevchenko 	if (has_acpi_companion(drv_data->ssp->dev)) {
16500c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
16510c27d9cfSMika Westerberg 		/*
16520c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
16530c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
16540c27d9cfSMika Westerberg 		 * to match what Linux expects.
16550c27d9cfSMika Westerberg 		 */
16560c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
165730f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
16580c27d9cfSMika Westerberg 			return cs - 1;
16590c27d9cfSMika Westerberg 
16600c27d9cfSMika Westerberg 		default:
16610c27d9cfSMika Westerberg 			break;
16620c27d9cfSMika Westerberg 		}
16630c27d9cfSMika Westerberg 	}
16640c27d9cfSMika Westerberg 
16650c27d9cfSMika Westerberg 	return cs;
16660c27d9cfSMika Westerberg }
16670c27d9cfSMika Westerberg 
1668b2662a16SDaniel Vetter static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1669b2662a16SDaniel Vetter {
1670b2662a16SDaniel Vetter 	return MAX_DMA_LEN;
1671b2662a16SDaniel Vetter }
1672b2662a16SDaniel Vetter 
1673fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1674ca632f55SGrant Likely {
1675ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
167651eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *platform_info;
167751eea52dSLubomir Rintel 	struct spi_controller *controller;
1678ca632f55SGrant Likely 	struct driver_data *drv_data;
1679ca632f55SGrant Likely 	struct ssp_device *ssp;
16808b136baaSJarkko Nikula 	const struct lpss_config *config;
168199f499cdSMika Westerberg 	int status, count;
1682c039dd27SJarkko Nikula 	u32 tmp;
1683ca632f55SGrant Likely 
1684851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1685851bacf5SMika Westerberg 	if (!platform_info) {
16860db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
168714af1df3SAndy Shevchenko 		if (IS_ERR(platform_info)) {
1688851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
168914af1df3SAndy Shevchenko 			return PTR_ERR(platform_info);
1690851bacf5SMika Westerberg 		}
1691a3496855SMika Westerberg 	}
1692ca632f55SGrant Likely 
1693ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1694851bacf5SMika Westerberg 	if (!ssp)
1695851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1696851bacf5SMika Westerberg 
1697851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
1698851bacf5SMika Westerberg 		dev_err(&pdev->dev, "failed to get ssp\n");
1699ca632f55SGrant Likely 		return -ENODEV;
1700ca632f55SGrant Likely 	}
1701ca632f55SGrant Likely 
1702ec93cb6fSLubomir Rintel 	if (platform_info->is_slave)
17035626308bSLukas Wunner 		controller = devm_spi_alloc_slave(dev, sizeof(*drv_data));
1704ec93cb6fSLubomir Rintel 	else
17055626308bSLukas Wunner 		controller = devm_spi_alloc_master(dev, sizeof(*drv_data));
1706ec93cb6fSLubomir Rintel 
170751eea52dSLubomir Rintel 	if (!controller) {
170851eea52dSLubomir Rintel 		dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1709f2eed8caSAndy Shevchenko 		status = -ENOMEM;
1710f2eed8caSAndy Shevchenko 		goto out_error_controller_alloc;
1711ca632f55SGrant Likely 	}
171251eea52dSLubomir Rintel 	drv_data = spi_controller_get_devdata(controller);
171351eea52dSLubomir Rintel 	drv_data->controller = controller;
171451eea52dSLubomir Rintel 	drv_data->controller_info = platform_info;
1715ca632f55SGrant Likely 	drv_data->ssp = ssp;
1716ca632f55SGrant Likely 
171751eea52dSLubomir Rintel 	controller->dev.of_node = pdev->dev.of_node;
1718ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
171951eea52dSLubomir Rintel 	controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1720ca632f55SGrant Likely 
172151eea52dSLubomir Rintel 	controller->bus_num = ssp->port_id;
172251eea52dSLubomir Rintel 	controller->dma_alignment = DMA_ALIGNMENT;
172351eea52dSLubomir Rintel 	controller->cleanup = cleanup;
172451eea52dSLubomir Rintel 	controller->setup = setup;
172551eea52dSLubomir Rintel 	controller->set_cs = pxa2xx_spi_set_cs;
172651eea52dSLubomir Rintel 	controller->transfer_one = pxa2xx_spi_transfer_one;
172751eea52dSLubomir Rintel 	controller->slave_abort = pxa2xx_spi_slave_abort;
172851eea52dSLubomir Rintel 	controller->handle_err = pxa2xx_spi_handle_err;
172951eea52dSLubomir Rintel 	controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
173051eea52dSLubomir Rintel 	controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
173151eea52dSLubomir Rintel 	controller->auto_runtime_pm = true;
173251eea52dSLubomir Rintel 	controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1733ca632f55SGrant Likely 
1734ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1735ca632f55SGrant Likely 
1736ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1737e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1738e5262d05SWeike Chen 		case QUARK_X1000_SSP:
173951eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1740e5262d05SWeike Chen 			break;
1741e5262d05SWeike Chen 		default:
174251eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1743e5262d05SWeike Chen 			break;
1744e5262d05SWeike Chen 		}
1745e5262d05SWeike Chen 
1746ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1747ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1748ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1749ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1750ca632f55SGrant Likely 	} else {
175151eea52dSLubomir Rintel 		controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1752ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
17535928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1754ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1755ec93cb6fSLubomir Rintel 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1756ec93cb6fSLubomir Rintel 						| SSSR_ROR | SSSR_TUR;
1757ca632f55SGrant Likely 	}
1758ca632f55SGrant Likely 
1759ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1760ca632f55SGrant Likely 			drv_data);
1761ca632f55SGrant Likely 	if (status < 0) {
1762ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
176351eea52dSLubomir Rintel 		goto out_error_controller_alloc;
1764ca632f55SGrant Likely 	}
1765ca632f55SGrant Likely 
1766ca632f55SGrant Likely 	/* Setup DMA if requested */
1767ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1768cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1769cd7bed00SMika Westerberg 		if (status) {
17708b57b11bSFlavio Suligoi 			dev_warn(dev, "no DMA channels available, using PIO\n");
1771cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1772b6ced294SJarkko Nikula 		} else {
177351eea52dSLubomir Rintel 			controller->can_dma = pxa2xx_spi_can_dma;
1774bf9f742cSMark Brown 			controller->max_dma_len = MAX_DMA_LEN;
1775b2662a16SDaniel Vetter 			controller->max_transfer_size =
1776b2662a16SDaniel Vetter 				pxa2xx_spi_max_dma_transfer_size;
1777ca632f55SGrant Likely 		}
1778ca632f55SGrant Likely 	}
1779ca632f55SGrant Likely 
1780ca632f55SGrant Likely 	/* Enable SOC clock */
178162bbc864STobias Jordan 	status = clk_prepare_enable(ssp->clk);
178262bbc864STobias Jordan 	if (status)
178362bbc864STobias Jordan 		goto out_error_dma_irq_alloc;
17843343b7a6SMika Westerberg 
178551eea52dSLubomir Rintel 	controller->max_speed_hz = clk_get_rate(ssp->clk);
178623cdddb2SJarkko Nikula 	/*
178723cdddb2SJarkko Nikula 	 * Set minimum speed for all other platforms than Intel Quark which is
178823cdddb2SJarkko Nikula 	 * able do under 1 Hz transfers.
178923cdddb2SJarkko Nikula 	 */
179023cdddb2SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
179123cdddb2SJarkko Nikula 		controller->min_speed_hz =
179223cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 4096);
179323cdddb2SJarkko Nikula 	else if (!is_quark_x1000_ssp(drv_data))
179423cdddb2SJarkko Nikula 		controller->min_speed_hz =
179523cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 512);
1796ca632f55SGrant Likely 
1797ca632f55SGrant Likely 	/* Load default SSP configuration */
1798c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
1799e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1800e5262d05SWeike Chen 	case QUARK_X1000_SSP:
18017c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
18027c7289a4SAndy Shevchenko 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1803c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1804e5262d05SWeike Chen 
1805e5262d05SWeike Chen 		/* using the Motorola SPI protocol and use 8 bit frame */
18067c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
18077c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1808e5262d05SWeike Chen 		break;
18097c7289a4SAndy Shevchenko 	case CE4100_SSP:
18107c7289a4SAndy Shevchenko 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
18117c7289a4SAndy Shevchenko 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
18127c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
18137c7289a4SAndy Shevchenko 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
18147c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1815a2dd8af0SAndy Shevchenko 		break;
1816e5262d05SWeike Chen 	default:
1817ec93cb6fSLubomir Rintel 
181851eea52dSLubomir Rintel 		if (spi_controller_is_slave(controller)) {
1819ec93cb6fSLubomir Rintel 			tmp = SSCR1_SCFR |
1820ec93cb6fSLubomir Rintel 			      SSCR1_SCLKDIR |
1821ec93cb6fSLubomir Rintel 			      SSCR1_SFRMDIR |
1822ec93cb6fSLubomir Rintel 			      SSCR1_RxTresh(2) |
1823ec93cb6fSLubomir Rintel 			      SSCR1_TxTresh(1) |
1824ec93cb6fSLubomir Rintel 			      SSCR1_SPH;
1825ec93cb6fSLubomir Rintel 		} else {
1826c039dd27SJarkko Nikula 			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1827c039dd27SJarkko Nikula 			      SSCR1_TxTresh(TX_THRESH_DFLT);
1828ec93cb6fSLubomir Rintel 		}
1829c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1830ec93cb6fSLubomir Rintel 		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
183151eea52dSLubomir Rintel 		if (!spi_controller_is_slave(controller))
1832ec93cb6fSLubomir Rintel 			tmp |= SSCR0_SCR(2);
1833c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1834e5262d05SWeike Chen 		break;
1835e5262d05SWeike Chen 	}
1836e5262d05SWeike Chen 
1837ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1838c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1839e5262d05SWeike Chen 
1840e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1841c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1842ca632f55SGrant Likely 
18438b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
18448b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
18458b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
18468b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
18478b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
18488b136baaSJarkko Nikula 						   config->reg_capabilities);
18498b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
18508b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
18518b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
185230f3a6abSMika Westerberg 		} else if (config->cs_num) {
185330f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
18548b136baaSJarkko Nikula 		}
18558b136baaSJarkko Nikula 	}
185651eea52dSLubomir Rintel 	controller->num_chipselect = platform_info->num_chipselect;
18578b136baaSJarkko Nikula 
185899f499cdSMika Westerberg 	count = gpiod_count(&pdev->dev, "cs");
18596ac5a435SAndy Shevchenko 	if (count > 0) {
18606ac5a435SAndy Shevchenko 		int i;
18616ac5a435SAndy Shevchenko 
186251eea52dSLubomir Rintel 		controller->num_chipselect = max_t(int, count,
186351eea52dSLubomir Rintel 			controller->num_chipselect);
186499f499cdSMika Westerberg 
18656ac5a435SAndy Shevchenko 		drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
186651eea52dSLubomir Rintel 			controller->num_chipselect, sizeof(struct gpio_desc *),
18676ac5a435SAndy Shevchenko 			GFP_KERNEL);
18686ac5a435SAndy Shevchenko 		if (!drv_data->cs_gpiods) {
18696ac5a435SAndy Shevchenko 			status = -ENOMEM;
18706ac5a435SAndy Shevchenko 			goto out_error_clock_enabled;
18716ac5a435SAndy Shevchenko 		}
18726ac5a435SAndy Shevchenko 
187351eea52dSLubomir Rintel 		for (i = 0; i < controller->num_chipselect; i++) {
18746ac5a435SAndy Shevchenko 			struct gpio_desc *gpiod;
18756ac5a435SAndy Shevchenko 
1876d35f2dc9SAndy Shevchenko 			gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
18776ac5a435SAndy Shevchenko 			if (IS_ERR(gpiod)) {
18786ac5a435SAndy Shevchenko 				/* Means use native chip select */
18796ac5a435SAndy Shevchenko 				if (PTR_ERR(gpiod) == -ENOENT)
18806ac5a435SAndy Shevchenko 					continue;
18816ac5a435SAndy Shevchenko 
188277d33897SLubomir Rintel 				status = PTR_ERR(gpiod);
18836ac5a435SAndy Shevchenko 				goto out_error_clock_enabled;
18846ac5a435SAndy Shevchenko 			} else {
18856ac5a435SAndy Shevchenko 				drv_data->cs_gpiods[i] = gpiod;
18866ac5a435SAndy Shevchenko 			}
18876ac5a435SAndy Shevchenko 		}
18886ac5a435SAndy Shevchenko 	}
18896ac5a435SAndy Shevchenko 
189077d33897SLubomir Rintel 	if (platform_info->is_slave) {
189177d33897SLubomir Rintel 		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
189277d33897SLubomir Rintel 						"ready", GPIOD_OUT_LOW);
189377d33897SLubomir Rintel 		if (IS_ERR(drv_data->gpiod_ready)) {
189477d33897SLubomir Rintel 			status = PTR_ERR(drv_data->gpiod_ready);
189577d33897SLubomir Rintel 			goto out_error_clock_enabled;
189677d33897SLubomir Rintel 		}
189777d33897SLubomir Rintel 	}
189877d33897SLubomir Rintel 
1899836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1900836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1901836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1902836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1903836d1a22SAntonio Ospite 
1904ca632f55SGrant Likely 	/* Register with the SPI framework */
1905ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
190632e5b572SLukas Wunner 	status = spi_register_controller(controller);
1907ca632f55SGrant Likely 	if (status != 0) {
190851eea52dSLubomir Rintel 		dev_err(&pdev->dev, "problem registering spi controller\n");
190912742045SLubomir Rintel 		goto out_error_pm_runtime_enabled;
1910ca632f55SGrant Likely 	}
1911ca632f55SGrant Likely 
1912ca632f55SGrant Likely 	return status;
1913ca632f55SGrant Likely 
191412742045SLubomir Rintel out_error_pm_runtime_enabled:
1915e2b714afSJarkko Nikula 	pm_runtime_disable(&pdev->dev);
191612742045SLubomir Rintel 
191712742045SLubomir Rintel out_error_clock_enabled:
19183343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
191962bbc864STobias Jordan 
192062bbc864STobias Jordan out_error_dma_irq_alloc:
1921cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1922ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1923ca632f55SGrant Likely 
192451eea52dSLubomir Rintel out_error_controller_alloc:
1925ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1926ca632f55SGrant Likely 	return status;
1927ca632f55SGrant Likely }
1928ca632f55SGrant Likely 
1929ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1930ca632f55SGrant Likely {
1931ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
19323d24b2a4SAndy Shevchenko 	struct ssp_device *ssp = drv_data->ssp;
1933ca632f55SGrant Likely 
19347d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
19357d94a505SMika Westerberg 
193632e5b572SLukas Wunner 	spi_unregister_controller(drv_data->controller);
193732e5b572SLukas Wunner 
1938ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
1939c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
19403343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1941ca632f55SGrant Likely 
1942ca632f55SGrant Likely 	/* Release DMA */
194351eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma)
1944cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1945ca632f55SGrant Likely 
19467d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
19477d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
19487d94a505SMika Westerberg 
1949ca632f55SGrant Likely 	/* Release IRQ */
1950ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1951ca632f55SGrant Likely 
1952ca632f55SGrant Likely 	/* Release SSP */
1953ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1954ca632f55SGrant Likely 
1955ca632f55SGrant Likely 	return 0;
1956ca632f55SGrant Likely }
1957ca632f55SGrant Likely 
1958382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP
1959ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1960ca632f55SGrant Likely {
1961ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1962ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1963bffc967eSJarkko Nikula 	int status;
1964ca632f55SGrant Likely 
196551eea52dSLubomir Rintel 	status = spi_controller_suspend(drv_data->controller);
1966ca632f55SGrant Likely 	if (status != 0)
1967ca632f55SGrant Likely 		return status;
1968c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
19692b9375b9SDmitry Eremin-Solenikov 
19702b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
19713343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1972ca632f55SGrant Likely 
1973ca632f55SGrant Likely 	return 0;
1974ca632f55SGrant Likely }
1975ca632f55SGrant Likely 
1976ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1977ca632f55SGrant Likely {
1978ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1979ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1980bffc967eSJarkko Nikula 	int status;
1981ca632f55SGrant Likely 
1982ca632f55SGrant Likely 	/* Enable the SSP clock */
198362bbc864STobias Jordan 	if (!pm_runtime_suspended(dev)) {
198462bbc864STobias Jordan 		status = clk_prepare_enable(ssp->clk);
198562bbc864STobias Jordan 		if (status)
198662bbc864STobias Jordan 			return status;
198762bbc864STobias Jordan 	}
1988ca632f55SGrant Likely 
1989ca632f55SGrant Likely 	/* Start the queue running */
199051eea52dSLubomir Rintel 	return spi_controller_resume(drv_data->controller);
1991ca632f55SGrant Likely }
19927d94a505SMika Westerberg #endif
19937d94a505SMika Westerberg 
1994ec833050SRafael J. Wysocki #ifdef CONFIG_PM
19957d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
19967d94a505SMika Westerberg {
19977d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
19987d94a505SMika Westerberg 
19997d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
20007d94a505SMika Westerberg 	return 0;
20017d94a505SMika Westerberg }
20027d94a505SMika Westerberg 
20037d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
20047d94a505SMika Westerberg {
20057d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
200662bbc864STobias Jordan 	int status;
20077d94a505SMika Westerberg 
200862bbc864STobias Jordan 	status = clk_prepare_enable(drv_data->ssp->clk);
200962bbc864STobias Jordan 	return status;
20107d94a505SMika Westerberg }
20117d94a505SMika Westerberg #endif
2012ca632f55SGrant Likely 
2013ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
20147d94a505SMika Westerberg 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
20157d94a505SMika Westerberg 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
20167d94a505SMika Westerberg 			   pxa2xx_spi_runtime_resume, NULL)
2017ca632f55SGrant Likely };
2018ca632f55SGrant Likely 
2019ca632f55SGrant Likely static struct platform_driver driver = {
2020ca632f55SGrant Likely 	.driver = {
2021ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
2022ca632f55SGrant Likely 		.pm	= &pxa2xx_spi_pm_ops,
2023a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
202487ae1d2dSLubomir Rintel 		.of_match_table = of_match_ptr(pxa2xx_spi_of_match),
2025ca632f55SGrant Likely 	},
2026ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
2027ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
2028ca632f55SGrant Likely };
2029ca632f55SGrant Likely 
2030ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
2031ca632f55SGrant Likely {
2032ca632f55SGrant Likely 	return platform_driver_register(&driver);
2033ca632f55SGrant Likely }
2034ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
2035ca632f55SGrant Likely 
2036ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
2037ca632f55SGrant Likely {
2038ca632f55SGrant Likely 	platform_driver_unregister(&driver);
2039ca632f55SGrant Likely }
2040ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
204151ebf6acSFlavio Suligoi 
204251ebf6acSFlavio Suligoi MODULE_SOFTDEP("pre: dw_dmac");
2043