xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision 32e5b57232c0411e7dea96625c415510430ac079)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
4a0d2642eSMika Westerberg  * Copyright (C) 2013, Intel Corporation
5ca632f55SGrant Likely  */
6ca632f55SGrant Likely 
75ce25705SAndy Shevchenko #include <linux/acpi.h>
88b136baaSJarkko Nikula #include <linux/bitops.h>
95ce25705SAndy Shevchenko #include <linux/clk.h>
105ce25705SAndy Shevchenko #include <linux/delay.h>
11ca632f55SGrant Likely #include <linux/device.h>
12cbfd6a21SSachin Kamat #include <linux/err.h>
135ce25705SAndy Shevchenko #include <linux/errno.h>
145ce25705SAndy Shevchenko #include <linux/gpio/consumer.h>
155ce25705SAndy Shevchenko #include <linux/gpio.h>
165ce25705SAndy Shevchenko #include <linux/init.h>
17ca632f55SGrant Likely #include <linux/interrupt.h>
185ce25705SAndy Shevchenko #include <linux/ioport.h>
199df461ecSAndy Shevchenko #include <linux/kernel.h>
205ce25705SAndy Shevchenko #include <linux/module.h>
21ae8fbf1dSAndy Shevchenko #include <linux/mod_devicetable.h>
22ae8fbf1dSAndy Shevchenko #include <linux/of.h>
2334cadd9cSJarkko Nikula #include <linux/pci.h>
24ca632f55SGrant Likely #include <linux/platform_device.h>
255ce25705SAndy Shevchenko #include <linux/pm_runtime.h>
26f2faa3ecSAndy Shevchenko #include <linux/property.h>
275ce25705SAndy Shevchenko #include <linux/slab.h>
28ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h>
29ca632f55SGrant Likely #include <linux/spi/spi.h>
30ca632f55SGrant Likely 
31cd7bed00SMika Westerberg #include "spi-pxa2xx.h"
32ca632f55SGrant Likely 
33ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street");
34ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
35ca632f55SGrant Likely MODULE_LICENSE("GPL");
36ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi");
37ca632f55SGrant Likely 
38ca632f55SGrant Likely #define TIMOUT_DFLT		1000
39ca632f55SGrant Likely 
40ca632f55SGrant Likely /*
41ca632f55SGrant Likely  * for testing SSCR1 changes that require SSP restart, basically
42ca632f55SGrant Likely  * everything except the service and interrupt enables, the pxa270 developer
43ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
44ca632f55SGrant Likely  * list, but the PXA255 dev man says all bits without really meaning the
45ca632f55SGrant Likely  * service and interrupt enables
46ca632f55SGrant Likely  */
47ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
48ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
49ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
50ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
51ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
52ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
53ca632f55SGrant Likely 
54e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
55e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
56e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
57e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
58e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
59e5262d05SWeike Chen 
607c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
617c7289a4SAndy Shevchenko 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
627c7289a4SAndy Shevchenko 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
637c7289a4SAndy Shevchenko 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
647c7289a4SAndy Shevchenko 				| CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
657c7289a4SAndy Shevchenko 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
667c7289a4SAndy Shevchenko 
67624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
68624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE			BIT(0)
69624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
708b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT			9
718b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
72a0d2642eSMika Westerberg 
73683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE 0x38
74683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
75683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
76683f65deSEvan Green 
77dccf7369SJarkko Nikula struct lpss_config {
78dccf7369SJarkko Nikula 	/* LPSS offset from drv_data->ioaddr */
79dccf7369SJarkko Nikula 	unsigned offset;
80dccf7369SJarkko Nikula 	/* Register offsets from drv_data->lpss_base or -1 */
81dccf7369SJarkko Nikula 	int reg_general;
82dccf7369SJarkko Nikula 	int reg_ssp;
83dccf7369SJarkko Nikula 	int reg_cs_ctrl;
848b136baaSJarkko Nikula 	int reg_capabilities;
85dccf7369SJarkko Nikula 	/* FIFO thresholds */
86dccf7369SJarkko Nikula 	u32 rx_threshold;
87dccf7369SJarkko Nikula 	u32 tx_threshold_lo;
88dccf7369SJarkko Nikula 	u32 tx_threshold_hi;
89c1e4a53cSMika Westerberg 	/* Chip select control */
90c1e4a53cSMika Westerberg 	unsigned cs_sel_shift;
91c1e4a53cSMika Westerberg 	unsigned cs_sel_mask;
9230f3a6abSMika Westerberg 	unsigned cs_num;
93683f65deSEvan Green 	/* Quirks */
94683f65deSEvan Green 	unsigned cs_clk_stays_gated : 1;
95dccf7369SJarkko Nikula };
96dccf7369SJarkko Nikula 
97dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */
98dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = {
99dccf7369SJarkko Nikula 	{	/* LPSS_LPT_SSP */
100dccf7369SJarkko Nikula 		.offset = 0x800,
101dccf7369SJarkko Nikula 		.reg_general = 0x08,
102dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
103dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1048b136baaSJarkko Nikula 		.reg_capabilities = -1,
105dccf7369SJarkko Nikula 		.rx_threshold = 64,
106dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
107dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
108dccf7369SJarkko Nikula 	},
109dccf7369SJarkko Nikula 	{	/* LPSS_BYT_SSP */
110dccf7369SJarkko Nikula 		.offset = 0x400,
111dccf7369SJarkko Nikula 		.reg_general = 0x08,
112dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
113dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1148b136baaSJarkko Nikula 		.reg_capabilities = -1,
115dccf7369SJarkko Nikula 		.rx_threshold = 64,
116dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
117dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
118dccf7369SJarkko Nikula 	},
11930f3a6abSMika Westerberg 	{	/* LPSS_BSW_SSP */
12030f3a6abSMika Westerberg 		.offset = 0x400,
12130f3a6abSMika Westerberg 		.reg_general = 0x08,
12230f3a6abSMika Westerberg 		.reg_ssp = 0x0c,
12330f3a6abSMika Westerberg 		.reg_cs_ctrl = 0x18,
12430f3a6abSMika Westerberg 		.reg_capabilities = -1,
12530f3a6abSMika Westerberg 		.rx_threshold = 64,
12630f3a6abSMika Westerberg 		.tx_threshold_lo = 160,
12730f3a6abSMika Westerberg 		.tx_threshold_hi = 224,
12830f3a6abSMika Westerberg 		.cs_sel_shift = 2,
12930f3a6abSMika Westerberg 		.cs_sel_mask = 1 << 2,
13030f3a6abSMika Westerberg 		.cs_num = 2,
13130f3a6abSMika Westerberg 	},
13234cadd9cSJarkko Nikula 	{	/* LPSS_SPT_SSP */
13334cadd9cSJarkko Nikula 		.offset = 0x200,
13434cadd9cSJarkko Nikula 		.reg_general = -1,
13534cadd9cSJarkko Nikula 		.reg_ssp = 0x20,
13634cadd9cSJarkko Nikula 		.reg_cs_ctrl = 0x24,
13766ec246eSJarkko Nikula 		.reg_capabilities = -1,
13834cadd9cSJarkko Nikula 		.rx_threshold = 1,
13934cadd9cSJarkko Nikula 		.tx_threshold_lo = 32,
14034cadd9cSJarkko Nikula 		.tx_threshold_hi = 56,
14134cadd9cSJarkko Nikula 	},
142b7c08cf8SJarkko Nikula 	{	/* LPSS_BXT_SSP */
143b7c08cf8SJarkko Nikula 		.offset = 0x200,
144b7c08cf8SJarkko Nikula 		.reg_general = -1,
145b7c08cf8SJarkko Nikula 		.reg_ssp = 0x20,
146b7c08cf8SJarkko Nikula 		.reg_cs_ctrl = 0x24,
147b7c08cf8SJarkko Nikula 		.reg_capabilities = 0xfc,
148b7c08cf8SJarkko Nikula 		.rx_threshold = 1,
149b7c08cf8SJarkko Nikula 		.tx_threshold_lo = 16,
150b7c08cf8SJarkko Nikula 		.tx_threshold_hi = 48,
151c1e4a53cSMika Westerberg 		.cs_sel_shift = 8,
152c1e4a53cSMika Westerberg 		.cs_sel_mask = 3 << 8,
153b7c08cf8SJarkko Nikula 	},
154fc0b2accSJarkko Nikula 	{	/* LPSS_CNL_SSP */
155fc0b2accSJarkko Nikula 		.offset = 0x200,
156fc0b2accSJarkko Nikula 		.reg_general = -1,
157fc0b2accSJarkko Nikula 		.reg_ssp = 0x20,
158fc0b2accSJarkko Nikula 		.reg_cs_ctrl = 0x24,
159fc0b2accSJarkko Nikula 		.reg_capabilities = 0xfc,
160fc0b2accSJarkko Nikula 		.rx_threshold = 1,
161fc0b2accSJarkko Nikula 		.tx_threshold_lo = 32,
162fc0b2accSJarkko Nikula 		.tx_threshold_hi = 56,
163fc0b2accSJarkko Nikula 		.cs_sel_shift = 8,
164fc0b2accSJarkko Nikula 		.cs_sel_mask = 3 << 8,
165683f65deSEvan Green 		.cs_clk_stays_gated = true,
166fc0b2accSJarkko Nikula 	},
167dccf7369SJarkko Nikula };
168dccf7369SJarkko Nikula 
169dccf7369SJarkko Nikula static inline const struct lpss_config
170dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data)
171dccf7369SJarkko Nikula {
172dccf7369SJarkko Nikula 	return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
173dccf7369SJarkko Nikula }
174dccf7369SJarkko Nikula 
175a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data)
176a0d2642eSMika Westerberg {
17703fbf488SJarkko Nikula 	switch (drv_data->ssp_type) {
17803fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
17903fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
18030f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
18134cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
182b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
183fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
18403fbf488SJarkko Nikula 		return true;
18503fbf488SJarkko Nikula 	default:
18603fbf488SJarkko Nikula 		return false;
18703fbf488SJarkko Nikula 	}
188a0d2642eSMika Westerberg }
189a0d2642eSMika Westerberg 
190e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
191e5262d05SWeike Chen {
192e5262d05SWeike Chen 	return drv_data->ssp_type == QUARK_X1000_SSP;
193e5262d05SWeike Chen }
194e5262d05SWeike Chen 
19541c98841SAndy Shevchenko static bool is_mmp2_ssp(const struct driver_data *drv_data)
19641c98841SAndy Shevchenko {
19741c98841SAndy Shevchenko 	return drv_data->ssp_type == MMP2_SSP;
19841c98841SAndy Shevchenko }
19941c98841SAndy Shevchenko 
2004fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
2014fdb2424SWeike Chen {
2024fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
203e5262d05SWeike Chen 	case QUARK_X1000_SSP:
204e5262d05SWeike Chen 		return QUARK_X1000_SSCR1_CHANGE_MASK;
2057c7289a4SAndy Shevchenko 	case CE4100_SSP:
2067c7289a4SAndy Shevchenko 		return CE4100_SSCR1_CHANGE_MASK;
2074fdb2424SWeike Chen 	default:
2084fdb2424SWeike Chen 		return SSCR1_CHANGE_MASK;
2094fdb2424SWeike Chen 	}
2104fdb2424SWeike Chen }
2114fdb2424SWeike Chen 
2124fdb2424SWeike Chen static u32
2134fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
2144fdb2424SWeike Chen {
2154fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
216e5262d05SWeike Chen 	case QUARK_X1000_SSP:
217e5262d05SWeike Chen 		return RX_THRESH_QUARK_X1000_DFLT;
2187c7289a4SAndy Shevchenko 	case CE4100_SSP:
2197c7289a4SAndy Shevchenko 		return RX_THRESH_CE4100_DFLT;
2204fdb2424SWeike Chen 	default:
2214fdb2424SWeike Chen 		return RX_THRESH_DFLT;
2224fdb2424SWeike Chen 	}
2234fdb2424SWeike Chen }
2244fdb2424SWeike Chen 
2254fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
2264fdb2424SWeike Chen {
2274fdb2424SWeike Chen 	u32 mask;
2284fdb2424SWeike Chen 
2294fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
230e5262d05SWeike Chen 	case QUARK_X1000_SSP:
231e5262d05SWeike Chen 		mask = QUARK_X1000_SSSR_TFL_MASK;
232e5262d05SWeike Chen 		break;
2337c7289a4SAndy Shevchenko 	case CE4100_SSP:
2347c7289a4SAndy Shevchenko 		mask = CE4100_SSSR_TFL_MASK;
2357c7289a4SAndy Shevchenko 		break;
2364fdb2424SWeike Chen 	default:
2374fdb2424SWeike Chen 		mask = SSSR_TFL_MASK;
2384fdb2424SWeike Chen 		break;
2394fdb2424SWeike Chen 	}
2404fdb2424SWeike Chen 
241c039dd27SJarkko Nikula 	return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
2424fdb2424SWeike Chen }
2434fdb2424SWeike Chen 
2444fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
2454fdb2424SWeike Chen 				     u32 *sccr1_reg)
2464fdb2424SWeike Chen {
2474fdb2424SWeike Chen 	u32 mask;
2484fdb2424SWeike Chen 
2494fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
250e5262d05SWeike Chen 	case QUARK_X1000_SSP:
251e5262d05SWeike Chen 		mask = QUARK_X1000_SSCR1_RFT;
252e5262d05SWeike Chen 		break;
2537c7289a4SAndy Shevchenko 	case CE4100_SSP:
2547c7289a4SAndy Shevchenko 		mask = CE4100_SSCR1_RFT;
2557c7289a4SAndy Shevchenko 		break;
2564fdb2424SWeike Chen 	default:
2574fdb2424SWeike Chen 		mask = SSCR1_RFT;
2584fdb2424SWeike Chen 		break;
2594fdb2424SWeike Chen 	}
2604fdb2424SWeike Chen 	*sccr1_reg &= ~mask;
2614fdb2424SWeike Chen }
2624fdb2424SWeike Chen 
2634fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
2644fdb2424SWeike Chen 				   u32 *sccr1_reg, u32 threshold)
2654fdb2424SWeike Chen {
2664fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
267e5262d05SWeike Chen 	case QUARK_X1000_SSP:
268e5262d05SWeike Chen 		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
269e5262d05SWeike Chen 		break;
2707c7289a4SAndy Shevchenko 	case CE4100_SSP:
2717c7289a4SAndy Shevchenko 		*sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
2727c7289a4SAndy Shevchenko 		break;
2734fdb2424SWeike Chen 	default:
2744fdb2424SWeike Chen 		*sccr1_reg |= SSCR1_RxTresh(threshold);
2754fdb2424SWeike Chen 		break;
2764fdb2424SWeike Chen 	}
2774fdb2424SWeike Chen }
2784fdb2424SWeike Chen 
2794fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
2804fdb2424SWeike Chen 				  u32 clk_div, u8 bits)
2814fdb2424SWeike Chen {
2824fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
283e5262d05SWeike Chen 	case QUARK_X1000_SSP:
284e5262d05SWeike Chen 		return clk_div
285e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_Motorola
286e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
287e5262d05SWeike Chen 			| SSCR0_SSE;
2884fdb2424SWeike Chen 	default:
2894fdb2424SWeike Chen 		return clk_div
2904fdb2424SWeike Chen 			| SSCR0_Motorola
2914fdb2424SWeike Chen 			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
2924fdb2424SWeike Chen 			| SSCR0_SSE
2934fdb2424SWeike Chen 			| (bits > 16 ? SSCR0_EDSS : 0);
2944fdb2424SWeike Chen 	}
2954fdb2424SWeike Chen }
2964fdb2424SWeike Chen 
297a0d2642eSMika Westerberg /*
298a0d2642eSMika Westerberg  * Read and write LPSS SSP private registers. Caller must first check that
299a0d2642eSMika Westerberg  * is_lpss_ssp() returns true before these can be called.
300a0d2642eSMika Westerberg  */
301a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
302a0d2642eSMika Westerberg {
303a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
304a0d2642eSMika Westerberg 	return readl(drv_data->lpss_base + offset);
305a0d2642eSMika Westerberg }
306a0d2642eSMika Westerberg 
307a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data,
308a0d2642eSMika Westerberg 				  unsigned offset, u32 value)
309a0d2642eSMika Westerberg {
310a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
311a0d2642eSMika Westerberg 	writel(value, drv_data->lpss_base + offset);
312a0d2642eSMika Westerberg }
313a0d2642eSMika Westerberg 
314a0d2642eSMika Westerberg /*
315a0d2642eSMika Westerberg  * lpss_ssp_setup - perform LPSS SSP specific setup
316a0d2642eSMika Westerberg  * @drv_data: pointer to the driver private data
317a0d2642eSMika Westerberg  *
318a0d2642eSMika Westerberg  * Perform LPSS SSP specific setup. This function must be called first if
319a0d2642eSMika Westerberg  * one is going to use LPSS SSP private registers.
320a0d2642eSMika Westerberg  */
321a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data)
322a0d2642eSMika Westerberg {
323dccf7369SJarkko Nikula 	const struct lpss_config *config;
324dccf7369SJarkko Nikula 	u32 value;
325a0d2642eSMika Westerberg 
326dccf7369SJarkko Nikula 	config = lpss_get_config(drv_data);
327dccf7369SJarkko Nikula 	drv_data->lpss_base = drv_data->ioaddr + config->offset;
328a0d2642eSMika Westerberg 
329a0d2642eSMika Westerberg 	/* Enable software chip select control */
3300e897218SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
331624ea72eSJarkko Nikula 	value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
332624ea72eSJarkko Nikula 	value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
333dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
3340054e28dSMika Westerberg 
3350054e28dSMika Westerberg 	/* Enable multiblock DMA transfers */
33651eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma) {
337dccf7369SJarkko Nikula 		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
3381de70612SMika Westerberg 
33982ba2c2aSJarkko Nikula 		if (config->reg_general >= 0) {
34082ba2c2aSJarkko Nikula 			value = __lpss_ssp_read_priv(drv_data,
34182ba2c2aSJarkko Nikula 						     config->reg_general);
342624ea72eSJarkko Nikula 			value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
34382ba2c2aSJarkko Nikula 			__lpss_ssp_write_priv(drv_data,
34482ba2c2aSJarkko Nikula 					      config->reg_general, value);
34582ba2c2aSJarkko Nikula 		}
3461de70612SMika Westerberg 	}
347a0d2642eSMika Westerberg }
348a0d2642eSMika Westerberg 
349d5898e19SJarkko Nikula static void lpss_ssp_select_cs(struct spi_device *spi,
350c1e4a53cSMika Westerberg 			       const struct lpss_config *config)
351a0d2642eSMika Westerberg {
352d5898e19SJarkko Nikula 	struct driver_data *drv_data =
353d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
354d0283eb2SJarkko Nikula 	u32 value, cs;
355a0d2642eSMika Westerberg 
356c1e4a53cSMika Westerberg 	if (!config->cs_sel_mask)
357c1e4a53cSMika Westerberg 		return;
358dccf7369SJarkko Nikula 
359dccf7369SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
360c1e4a53cSMika Westerberg 
361d5898e19SJarkko Nikula 	cs = spi->chip_select;
362c1e4a53cSMika Westerberg 	cs <<= config->cs_sel_shift;
363c1e4a53cSMika Westerberg 	if (cs != (value & config->cs_sel_mask)) {
364d0283eb2SJarkko Nikula 		/*
365c1e4a53cSMika Westerberg 		 * When switching another chip select output active the
366c1e4a53cSMika Westerberg 		 * output must be selected first and wait 2 ssp_clk cycles
367c1e4a53cSMika Westerberg 		 * before changing state to active. Otherwise a short
368c1e4a53cSMika Westerberg 		 * glitch will occur on the previous chip select since
369c1e4a53cSMika Westerberg 		 * output select is latched but state control is not.
370d0283eb2SJarkko Nikula 		 */
371c1e4a53cSMika Westerberg 		value &= ~config->cs_sel_mask;
372d0283eb2SJarkko Nikula 		value |= cs;
373d0283eb2SJarkko Nikula 		__lpss_ssp_write_priv(drv_data,
374d0283eb2SJarkko Nikula 				      config->reg_cs_ctrl, value);
375d0283eb2SJarkko Nikula 		ndelay(1000000000 /
37651eea52dSLubomir Rintel 		       (drv_data->controller->max_speed_hz / 2));
377d0283eb2SJarkko Nikula 	}
378d0283eb2SJarkko Nikula }
379c1e4a53cSMika Westerberg 
380d5898e19SJarkko Nikula static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
381c1e4a53cSMika Westerberg {
382d5898e19SJarkko Nikula 	struct driver_data *drv_data =
383d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
384c1e4a53cSMika Westerberg 	const struct lpss_config *config;
385c1e4a53cSMika Westerberg 	u32 value;
386c1e4a53cSMika Westerberg 
387c1e4a53cSMika Westerberg 	config = lpss_get_config(drv_data);
388c1e4a53cSMika Westerberg 
389c1e4a53cSMika Westerberg 	if (enable)
390d5898e19SJarkko Nikula 		lpss_ssp_select_cs(spi, config);
391c1e4a53cSMika Westerberg 
392c1e4a53cSMika Westerberg 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
393c1e4a53cSMika Westerberg 	if (enable)
394c1e4a53cSMika Westerberg 		value &= ~LPSS_CS_CONTROL_CS_HIGH;
395c1e4a53cSMika Westerberg 	else
396c1e4a53cSMika Westerberg 		value |= LPSS_CS_CONTROL_CS_HIGH;
397dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
398683f65deSEvan Green 	if (config->cs_clk_stays_gated) {
399683f65deSEvan Green 		u32 clkgate;
400683f65deSEvan Green 
401683f65deSEvan Green 		/*
402683f65deSEvan Green 		 * Changing CS alone when dynamic clock gating is on won't
403683f65deSEvan Green 		 * actually flip CS at that time. This ruins SPI transfers
404683f65deSEvan Green 		 * that specify delays, or have no data. Toggle the clock mode
405683f65deSEvan Green 		 * to force on briefly to poke the CS pin to move.
406683f65deSEvan Green 		 */
407683f65deSEvan Green 		clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
408683f65deSEvan Green 		value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
409683f65deSEvan Green 			LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
410683f65deSEvan Green 
411683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
412683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
413683f65deSEvan Green 	}
414a0d2642eSMika Westerberg }
415a0d2642eSMika Westerberg 
416d5898e19SJarkko Nikula static void cs_assert(struct spi_device *spi)
417ca632f55SGrant Likely {
418d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
419d5898e19SJarkko Nikula 	struct driver_data *drv_data =
420d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
421ca632f55SGrant Likely 
422ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP) {
42396579a4eSJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, chip->frm);
424ca632f55SGrant Likely 		return;
425ca632f55SGrant Likely 	}
426ca632f55SGrant Likely 
427ca632f55SGrant Likely 	if (chip->cs_control) {
428ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_ASSERT);
429ca632f55SGrant Likely 		return;
430ca632f55SGrant Likely 	}
431ca632f55SGrant Likely 
432c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
433c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
434a0d2642eSMika Westerberg 		return;
435a0d2642eSMika Westerberg 	}
436a0d2642eSMika Westerberg 
4377566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
438d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, true);
439ca632f55SGrant Likely }
440ca632f55SGrant Likely 
441d5898e19SJarkko Nikula static void cs_deassert(struct spi_device *spi)
442ca632f55SGrant Likely {
443d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
444d5898e19SJarkko Nikula 	struct driver_data *drv_data =
445d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
446104e51afSJarkko Nikula 	unsigned long timeout;
447ca632f55SGrant Likely 
448ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
449ca632f55SGrant Likely 		return;
450ca632f55SGrant Likely 
451104e51afSJarkko Nikula 	/* Wait until SSP becomes idle before deasserting the CS */
452104e51afSJarkko Nikula 	timeout = jiffies + msecs_to_jiffies(10);
453104e51afSJarkko Nikula 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
454104e51afSJarkko Nikula 	       !time_after(jiffies, timeout))
455104e51afSJarkko Nikula 		cpu_relax();
456104e51afSJarkko Nikula 
457ca632f55SGrant Likely 	if (chip->cs_control) {
458ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_DEASSERT);
459ca632f55SGrant Likely 		return;
460ca632f55SGrant Likely 	}
461ca632f55SGrant Likely 
462c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
463c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
464a0d2642eSMika Westerberg 		return;
465a0d2642eSMika Westerberg 	}
466a0d2642eSMika Westerberg 
4677566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
468d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, false);
469d5898e19SJarkko Nikula }
470d5898e19SJarkko Nikula 
471d5898e19SJarkko Nikula static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
472d5898e19SJarkko Nikula {
473d5898e19SJarkko Nikula 	if (level)
474d5898e19SJarkko Nikula 		cs_deassert(spi);
475d5898e19SJarkko Nikula 	else
476d5898e19SJarkko Nikula 		cs_assert(spi);
477ca632f55SGrant Likely }
478ca632f55SGrant Likely 
479cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
480ca632f55SGrant Likely {
481ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
482ca632f55SGrant Likely 
483ca632f55SGrant Likely 	do {
484c039dd27SJarkko Nikula 		while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
485c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
486c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
487ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
488ca632f55SGrant Likely 
489ca632f55SGrant Likely 	return limit;
490ca632f55SGrant Likely }
491ca632f55SGrant Likely 
49229d7e05cSLubomir Rintel static void pxa2xx_spi_off(struct driver_data *drv_data)
49329d7e05cSLubomir Rintel {
49441c98841SAndy Shevchenko 	/* On MMP, disabling SSE seems to corrupt the Rx FIFO */
49541c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data))
49629d7e05cSLubomir Rintel 		return;
49729d7e05cSLubomir Rintel 
49829d7e05cSLubomir Rintel 	pxa2xx_spi_write(drv_data, SSCR0,
49929d7e05cSLubomir Rintel 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
50029d7e05cSLubomir Rintel }
50129d7e05cSLubomir Rintel 
502ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
503ca632f55SGrant Likely {
504ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
505ca632f55SGrant Likely 
5064fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
507ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
508ca632f55SGrant Likely 		return 0;
509ca632f55SGrant Likely 
510c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
511ca632f55SGrant Likely 	drv_data->tx += n_bytes;
512ca632f55SGrant Likely 
513ca632f55SGrant Likely 	return 1;
514ca632f55SGrant Likely }
515ca632f55SGrant Likely 
516ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
517ca632f55SGrant Likely {
518ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
519ca632f55SGrant Likely 
520c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
521ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
522c039dd27SJarkko Nikula 		pxa2xx_spi_read(drv_data, SSDR);
523ca632f55SGrant Likely 		drv_data->rx += n_bytes;
524ca632f55SGrant Likely 	}
525ca632f55SGrant Likely 
526ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
527ca632f55SGrant Likely }
528ca632f55SGrant Likely 
529ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data)
530ca632f55SGrant Likely {
5314fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
532ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
533ca632f55SGrant Likely 		return 0;
534ca632f55SGrant Likely 
535c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
536ca632f55SGrant Likely 	++drv_data->tx;
537ca632f55SGrant Likely 
538ca632f55SGrant Likely 	return 1;
539ca632f55SGrant Likely }
540ca632f55SGrant Likely 
541ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data)
542ca632f55SGrant Likely {
543c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
544ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
545c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
546ca632f55SGrant Likely 		++drv_data->rx;
547ca632f55SGrant Likely 	}
548ca632f55SGrant Likely 
549ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
550ca632f55SGrant Likely }
551ca632f55SGrant Likely 
552ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
553ca632f55SGrant Likely {
5544fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
555ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
556ca632f55SGrant Likely 		return 0;
557ca632f55SGrant Likely 
558c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
559ca632f55SGrant Likely 	drv_data->tx += 2;
560ca632f55SGrant Likely 
561ca632f55SGrant Likely 	return 1;
562ca632f55SGrant Likely }
563ca632f55SGrant Likely 
564ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
565ca632f55SGrant Likely {
566c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
567ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
568c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
569ca632f55SGrant Likely 		drv_data->rx += 2;
570ca632f55SGrant Likely 	}
571ca632f55SGrant Likely 
572ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
573ca632f55SGrant Likely }
574ca632f55SGrant Likely 
575ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
576ca632f55SGrant Likely {
5774fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
578ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
579ca632f55SGrant Likely 		return 0;
580ca632f55SGrant Likely 
581c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
582ca632f55SGrant Likely 	drv_data->tx += 4;
583ca632f55SGrant Likely 
584ca632f55SGrant Likely 	return 1;
585ca632f55SGrant Likely }
586ca632f55SGrant Likely 
587ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
588ca632f55SGrant Likely {
589c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
590ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
591c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
592ca632f55SGrant Likely 		drv_data->rx += 4;
593ca632f55SGrant Likely 	}
594ca632f55SGrant Likely 
595ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
596ca632f55SGrant Likely }
597ca632f55SGrant Likely 
598ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
599ca632f55SGrant Likely {
60096579a4eSJarkko Nikula 	struct chip_data *chip =
60151eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
602ca632f55SGrant Likely 	u32 sccr1_reg;
603ca632f55SGrant Likely 
604c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
605152bc19eSAndy Shevchenko 	switch (drv_data->ssp_type) {
606152bc19eSAndy Shevchenko 	case QUARK_X1000_SSP:
607152bc19eSAndy Shevchenko 		sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
608152bc19eSAndy Shevchenko 		break;
6097c7289a4SAndy Shevchenko 	case CE4100_SSP:
6107c7289a4SAndy Shevchenko 		sccr1_reg &= ~CE4100_SSCR1_RFT;
6117c7289a4SAndy Shevchenko 		break;
612152bc19eSAndy Shevchenko 	default:
613ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_RFT;
614152bc19eSAndy Shevchenko 		break;
615152bc19eSAndy Shevchenko 	}
616ca632f55SGrant Likely 	sccr1_reg |= chip->threshold;
617c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
618ca632f55SGrant Likely }
619ca632f55SGrant Likely 
620ca632f55SGrant Likely static void int_error_stop(struct driver_data *drv_data, const char* msg)
621ca632f55SGrant Likely {
622ca632f55SGrant Likely 	/* Stop and reset SSP */
623ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
624ca632f55SGrant Likely 	reset_sccr1(drv_data);
625ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
626c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
627cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
62829d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
629ca632f55SGrant Likely 
630ca632f55SGrant Likely 	dev_err(&drv_data->pdev->dev, "%s\n", msg);
631ca632f55SGrant Likely 
63251eea52dSLubomir Rintel 	drv_data->controller->cur_msg->status = -EIO;
63351eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
634ca632f55SGrant Likely }
635ca632f55SGrant Likely 
636ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
637ca632f55SGrant Likely {
63807550df0SJarkko Nikula 	/* Clear and disable interrupts */
639ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
640ca632f55SGrant Likely 	reset_sccr1(drv_data);
641ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
642c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
643ca632f55SGrant Likely 
64451eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
645ca632f55SGrant Likely }
646ca632f55SGrant Likely 
647ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
648ca632f55SGrant Likely {
649c039dd27SJarkko Nikula 	u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
650ca632f55SGrant Likely 		       drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
651ca632f55SGrant Likely 
652c039dd27SJarkko Nikula 	u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
653ca632f55SGrant Likely 
654ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
655ca632f55SGrant Likely 		int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
656ca632f55SGrant Likely 		return IRQ_HANDLED;
657ca632f55SGrant Likely 	}
658ca632f55SGrant Likely 
659ec93cb6fSLubomir Rintel 	if (irq_status & SSSR_TUR) {
660ec93cb6fSLubomir Rintel 		int_error_stop(drv_data, "interrupt_transfer: fifo underrun");
661ec93cb6fSLubomir Rintel 		return IRQ_HANDLED;
662ec93cb6fSLubomir Rintel 	}
663ec93cb6fSLubomir Rintel 
664ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
665c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
666ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
667ca632f55SGrant Likely 			int_transfer_complete(drv_data);
668ca632f55SGrant Likely 			return IRQ_HANDLED;
669ca632f55SGrant Likely 		}
670ca632f55SGrant Likely 	}
671ca632f55SGrant Likely 
672ca632f55SGrant Likely 	/* Drain rx fifo, Fill tx fifo and prevent overruns */
673ca632f55SGrant Likely 	do {
674ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
675ca632f55SGrant Likely 			int_transfer_complete(drv_data);
676ca632f55SGrant Likely 			return IRQ_HANDLED;
677ca632f55SGrant Likely 		}
678ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
679ca632f55SGrant Likely 
680ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
681ca632f55SGrant Likely 		int_transfer_complete(drv_data);
682ca632f55SGrant Likely 		return IRQ_HANDLED;
683ca632f55SGrant Likely 	}
684ca632f55SGrant Likely 
685ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
686ca632f55SGrant Likely 		u32 bytes_left;
687ca632f55SGrant Likely 		u32 sccr1_reg;
688ca632f55SGrant Likely 
689c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
690ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
691ca632f55SGrant Likely 
692ca632f55SGrant Likely 		/*
693ca632f55SGrant Likely 		 * PXA25x_SSP has no timeout, set up rx threshould for the
694ca632f55SGrant Likely 		 * remaining RX bytes.
695ca632f55SGrant Likely 		 */
696ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
6974fdb2424SWeike Chen 			u32 rx_thre;
698ca632f55SGrant Likely 
6994fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
700ca632f55SGrant Likely 
701ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
702ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
703ca632f55SGrant Likely 			case 4:
7042c183376SGustavo A. R. Silva 				bytes_left >>= 2;
7052c183376SGustavo A. R. Silva 				break;
706ca632f55SGrant Likely 			case 2:
707ca632f55SGrant Likely 				bytes_left >>= 1;
7082c183376SGustavo A. R. Silva 				break;
709ca632f55SGrant Likely 			}
710ca632f55SGrant Likely 
7114fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
7124fdb2424SWeike Chen 			if (rx_thre > bytes_left)
7134fdb2424SWeike Chen 				rx_thre = bytes_left;
714ca632f55SGrant Likely 
7154fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
716ca632f55SGrant Likely 		}
717c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
718ca632f55SGrant Likely 	}
719ca632f55SGrant Likely 
720ca632f55SGrant Likely 	/* We did something */
721ca632f55SGrant Likely 	return IRQ_HANDLED;
722ca632f55SGrant Likely }
723ca632f55SGrant Likely 
724b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data)
725b0312482SJan Kiszka {
72629d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
727b0312482SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1,
728b0312482SJan Kiszka 			 pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
729b0312482SJan Kiszka 	if (!pxa25x_ssp_comp(drv_data))
730b0312482SJan Kiszka 		pxa2xx_spi_write(drv_data, SSTO, 0);
731b0312482SJan Kiszka 	write_SSSR_CS(drv_data, drv_data->clear_sr);
732b0312482SJan Kiszka 
733b0312482SJan Kiszka 	dev_err(&drv_data->pdev->dev,
734b0312482SJan Kiszka 		"bad message state in interrupt handler\n");
735b0312482SJan Kiszka }
736b0312482SJan Kiszka 
737ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
738ca632f55SGrant Likely {
739ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
7407d94a505SMika Westerberg 	u32 sccr1_reg;
741ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
742ca632f55SGrant Likely 	u32 status;
743ca632f55SGrant Likely 
7447d94a505SMika Westerberg 	/*
7457d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
7467d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
7477d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
7487d94a505SMika Westerberg 	 * interrupt is enabled).
7497d94a505SMika Westerberg 	 */
7507d94a505SMika Westerberg 	if (pm_runtime_suspended(&drv_data->pdev->dev))
7517d94a505SMika Westerberg 		return IRQ_NONE;
7527d94a505SMika Westerberg 
753269e4a41SMika Westerberg 	/*
754269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
755269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
756269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
757269e4a41SMika Westerberg 	 * powered off.
758269e4a41SMika Westerberg 	 */
759c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
760269e4a41SMika Westerberg 	if (status == ~0)
761269e4a41SMika Westerberg 		return IRQ_NONE;
762269e4a41SMika Westerberg 
763c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
764ca632f55SGrant Likely 
765ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
766ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
767ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
768ca632f55SGrant Likely 
76902bc933eSTan, Jui Nee 	/* Ignore RX timeout interrupt if it is disabled */
77002bc933eSTan, Jui Nee 	if (!(sccr1_reg & SSCR1_TINTE))
77102bc933eSTan, Jui Nee 		mask &= ~SSSR_TINT;
77202bc933eSTan, Jui Nee 
773ca632f55SGrant Likely 	if (!(status & mask))
774ca632f55SGrant Likely 		return IRQ_NONE;
775ca632f55SGrant Likely 
776e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
777e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
778e51e9b93SJan Kiszka 
77951eea52dSLubomir Rintel 	if (!drv_data->controller->cur_msg) {
780b0312482SJan Kiszka 		handle_bad_msg(drv_data);
781ca632f55SGrant Likely 		/* Never fail */
782ca632f55SGrant Likely 		return IRQ_HANDLED;
783ca632f55SGrant Likely 	}
784ca632f55SGrant Likely 
785ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
786ca632f55SGrant Likely }
787ca632f55SGrant Likely 
788e5262d05SWeike Chen /*
7899df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
7909df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
7919df461ecSAndy Shevchenko  *
7929df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
7939df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
7949df461ecSAndy Shevchenko  *
7959df461ecSAndy Shevchenko  * Fsys = 200MHz
7969df461ecSAndy Shevchenko  *
7979df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
7989df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
7999df461ecSAndy Shevchenko  *
8009df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
8019df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
8029df461ecSAndy Shevchenko  *
8039df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
8049df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
8059df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
8069df461ecSAndy Shevchenko  *       k = [1, 256]
8079df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
8089df461ecSAndy Shevchenko  *
8099df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
8109df461ecSAndy Shevchenko  * are:
8119df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
8129df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
8139df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
8149df461ecSAndy Shevchenko  *
8159df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
8169df461ecSAndy Shevchenko  *
8179df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
8189df461ecSAndy Shevchenko  * to the asked baud rate.
819e5262d05SWeike Chen  */
8209df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
821e5262d05SWeike Chen {
8229df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
8239df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
8249df461ecSAndy Shevchenko 						   see (2) */
8259df461ecSAndy Shevchenko 						/* case 3 */
8269df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
8279df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
8289df461ecSAndy Shevchenko 	unsigned long scale;
8299df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
8309df461ecSAndy Shevchenko 	long r, r1, r2;
8319df461ecSAndy Shevchenko 	u32 mul;
832e5262d05SWeike Chen 
8339df461ecSAndy Shevchenko 	/* Case 1 */
8349df461ecSAndy Shevchenko 
8359df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
8369df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
8379df461ecSAndy Shevchenko 
8389df461ecSAndy Shevchenko 	/* Calculate initial quot */
8393ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
8409df461ecSAndy Shevchenko 
8419df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
8429df461ecSAndy Shevchenko 	if (q1 > 256) {
8439df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
8449df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
8459df461ecSAndy Shevchenko 		if (scale > 9) {
8469df461ecSAndy Shevchenko 			q1 >>= scale - 9;
8479df461ecSAndy Shevchenko 			mul >>= scale - 9;
8489df461ecSAndy Shevchenko 		}
8499df461ecSAndy Shevchenko 
8509df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
8519df461ecSAndy Shevchenko 		q1 += q1 & 1;
8529df461ecSAndy Shevchenko 	}
8539df461ecSAndy Shevchenko 
8549df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
8559df461ecSAndy Shevchenko 	scale = __ffs(q1);
8569df461ecSAndy Shevchenko 	q1 >>= scale;
8579df461ecSAndy Shevchenko 	mul >>= scale;
8589df461ecSAndy Shevchenko 
8599df461ecSAndy Shevchenko 	/* Get the remainder */
8609df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
8619df461ecSAndy Shevchenko 
8629df461ecSAndy Shevchenko 	/* Case 2 */
8639df461ecSAndy Shevchenko 
8643ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
8659df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
8669df461ecSAndy Shevchenko 
8679df461ecSAndy Shevchenko 	/*
8689df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
8699df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
8709df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
8719df461ecSAndy Shevchenko 	 */
8729df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
8739df461ecSAndy Shevchenko 		/* case 1 is better */
8749df461ecSAndy Shevchenko 		r = r1;
8759df461ecSAndy Shevchenko 		q = q1;
8769df461ecSAndy Shevchenko 	} else {
8779df461ecSAndy Shevchenko 		/* case 2 is better */
8789df461ecSAndy Shevchenko 		r = r2;
8799df461ecSAndy Shevchenko 		q = q2;
8809df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
8819df461ecSAndy Shevchenko 	}
8829df461ecSAndy Shevchenko 
8833ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
8849df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
8859df461ecSAndy Shevchenko 		u64 fssp;
8869df461ecSAndy Shevchenko 		u32 m;
8879df461ecSAndy Shevchenko 
8889df461ecSAndy Shevchenko 		/* Calculate initial quot */
8893ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
8909df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
8919df461ecSAndy Shevchenko 
8929df461ecSAndy Shevchenko 		/* Get the remainder */
8939df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
8949df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
8959df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
8969df461ecSAndy Shevchenko 
8979df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
8989df461ecSAndy Shevchenko 		if (r1 < r) {
8999df461ecSAndy Shevchenko 			/* case 3 is better */
9009df461ecSAndy Shevchenko 			q = 1;
9019df461ecSAndy Shevchenko 			mul = m;
902e5262d05SWeike Chen 		}
903e5262d05SWeike Chen 	}
904e5262d05SWeike Chen 
9059df461ecSAndy Shevchenko 	*dds = mul;
9069df461ecSAndy Shevchenko 	return q - 1;
907e5262d05SWeike Chen }
908e5262d05SWeike Chen 
9093343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
910ca632f55SGrant Likely {
91151eea52dSLubomir Rintel 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
9123343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
9133343b7a6SMika Westerberg 
9143343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
915ca632f55SGrant Likely 
91629f21337SFlavio Suligoi 	/*
91729f21337SFlavio Suligoi 	 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
91829f21337SFlavio Suligoi 	 * that the SSP transmission rate can be greater than the device rate
91929f21337SFlavio Suligoi 	 */
920ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
92129f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
922ca632f55SGrant Likely 	else
92329f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
924ca632f55SGrant Likely }
925ca632f55SGrant Likely 
926e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
927d2c2f6a4SAndy Shevchenko 					   int rate)
928e5262d05SWeike Chen {
92996579a4eSJarkko Nikula 	struct chip_data *chip =
93051eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
931025ffe88SAndy Shevchenko 	unsigned int clk_div;
932e5262d05SWeike Chen 
933e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
934e5262d05SWeike Chen 	case QUARK_X1000_SSP:
9359df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
936eecacf73SDan Carpenter 		break;
937e5262d05SWeike Chen 	default:
938025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
939eecacf73SDan Carpenter 		break;
940e5262d05SWeike Chen 	}
941025ffe88SAndy Shevchenko 	return clk_div << 8;
942e5262d05SWeike Chen }
943e5262d05SWeike Chen 
94451eea52dSLubomir Rintel static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
945b6ced294SJarkko Nikula 			       struct spi_device *spi,
946b6ced294SJarkko Nikula 			       struct spi_transfer *xfer)
947b6ced294SJarkko Nikula {
948b6ced294SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
949b6ced294SJarkko Nikula 
950b6ced294SJarkko Nikula 	return chip->enable_dma &&
951b6ced294SJarkko Nikula 	       xfer->len <= MAX_DMA_LEN &&
952b6ced294SJarkko Nikula 	       xfer->len >= chip->dma_burst_size;
953b6ced294SJarkko Nikula }
954b6ced294SJarkko Nikula 
95551eea52dSLubomir Rintel static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
956d5898e19SJarkko Nikula 				   struct spi_device *spi,
957d5898e19SJarkko Nikula 				   struct spi_transfer *transfer)
958ca632f55SGrant Likely {
95951eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
96051eea52dSLubomir Rintel 	struct spi_message *message = controller->cur_msg;
96120f4c379SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
96296579a4eSJarkko Nikula 	u32 dma_thresh = chip->dma_threshold;
96396579a4eSJarkko Nikula 	u32 dma_burst = chip->dma_burst_size;
96496579a4eSJarkko Nikula 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
965bffc967eSJarkko Nikula 	u32 clk_div;
966bffc967eSJarkko Nikula 	u8 bits;
967bffc967eSJarkko Nikula 	u32 speed;
968ca632f55SGrant Likely 	u32 cr0;
969ca632f55SGrant Likely 	u32 cr1;
9707d1f1bf6SAndy Shevchenko 	int err;
971b6ced294SJarkko Nikula 	int dma_mapped;
972ca632f55SGrant Likely 
973cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
974b6ced294SJarkko Nikula 	if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
975ca632f55SGrant Likely 
976ca632f55SGrant Likely 		/* reject already-mapped transfers; PIO won't always work */
977ca632f55SGrant Likely 		if (message->is_dma_mapped
978ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
979748fbadfSJarkko Nikula 			dev_err(&spi->dev,
9808ae55af3SJarkko Nikula 				"Mapped transfer length of %u is greater than %d\n",
981ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
982d5898e19SJarkko Nikula 			return -EINVAL;
983ca632f55SGrant Likely 		}
984ca632f55SGrant Likely 
985ca632f55SGrant Likely 		/* warn ... we force this to PIO mode */
98620f4c379SJarkko Nikula 		dev_warn_ratelimited(&spi->dev,
9878ae55af3SJarkko Nikula 				     "DMA disabled for transfer length %ld greater than %d\n",
988d5898e19SJarkko Nikula 				     (long)transfer->len, MAX_DMA_LEN);
989ca632f55SGrant Likely 	}
990ca632f55SGrant Likely 
991ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
992cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
993748fbadfSJarkko Nikula 		dev_err(&spi->dev, "Flush failed\n");
994d5898e19SJarkko Nikula 		return -EIO;
995ca632f55SGrant Likely 	}
996ca632f55SGrant Likely 	drv_data->n_bytes = chip->n_bytes;
997ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
998ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
999ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
1000ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
1001ca632f55SGrant Likely 	drv_data->write = drv_data->tx ? chip->write : null_writer;
1002ca632f55SGrant Likely 	drv_data->read = drv_data->rx ? chip->read : null_reader;
1003ca632f55SGrant Likely 
1004ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
1005ca632f55SGrant Likely 	bits = transfer->bits_per_word;
1006ca632f55SGrant Likely 	speed = transfer->speed_hz;
1007ca632f55SGrant Likely 
1008d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
1009ca632f55SGrant Likely 
1010ca632f55SGrant Likely 	if (bits <= 8) {
1011ca632f55SGrant Likely 		drv_data->n_bytes = 1;
1012ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1013ca632f55SGrant Likely 					u8_reader : null_reader;
1014ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1015ca632f55SGrant Likely 					u8_writer : null_writer;
1016ca632f55SGrant Likely 	} else if (bits <= 16) {
1017ca632f55SGrant Likely 		drv_data->n_bytes = 2;
1018ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1019ca632f55SGrant Likely 					u16_reader : null_reader;
1020ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1021ca632f55SGrant Likely 					u16_writer : null_writer;
1022ca632f55SGrant Likely 	} else if (bits <= 32) {
1023ca632f55SGrant Likely 		drv_data->n_bytes = 4;
1024ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1025ca632f55SGrant Likely 					u32_reader : null_reader;
1026ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1027ca632f55SGrant Likely 					u32_writer : null_writer;
1028ca632f55SGrant Likely 	}
1029196b0e2cSJarkko Nikula 	/*
1030196b0e2cSJarkko Nikula 	 * if bits/word is changed in dma mode, then must check the
1031196b0e2cSJarkko Nikula 	 * thresholds and burst also
1032196b0e2cSJarkko Nikula 	 */
1033ca632f55SGrant Likely 	if (chip->enable_dma) {
1034cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
103520f4c379SJarkko Nikula 						spi,
1036ca632f55SGrant Likely 						bits, &dma_burst,
1037ca632f55SGrant Likely 						&dma_thresh))
103820f4c379SJarkko Nikula 			dev_warn_ratelimited(&spi->dev,
10398ae55af3SJarkko Nikula 					     "DMA burst size reduced to match bits_per_word\n");
1040ca632f55SGrant Likely 	}
1041ca632f55SGrant Likely 
104251eea52dSLubomir Rintel 	dma_mapped = controller->can_dma &&
104320f4c379SJarkko Nikula 		     controller->can_dma(controller, spi, transfer) &&
104451eea52dSLubomir Rintel 		     controller->cur_msg_mapped;
1045b6ced294SJarkko Nikula 	if (dma_mapped) {
1046ca632f55SGrant Likely 
1047ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
1048cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1049ca632f55SGrant Likely 
1050d5898e19SJarkko Nikula 		err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1051d5898e19SJarkko Nikula 		if (err)
1052d5898e19SJarkko Nikula 			return err;
1053ca632f55SGrant Likely 
1054ca632f55SGrant Likely 		/* Clear status and start DMA engine */
1055ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1056c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1057cd7bed00SMika Westerberg 
1058cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
1059ca632f55SGrant Likely 	} else {
1060ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
1061ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
1062ca632f55SGrant Likely 
1063ca632f55SGrant Likely 		/* Clear status  */
1064ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1065ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1066ca632f55SGrant Likely 	}
1067ca632f55SGrant Likely 
1068ee03672dSJarkko Nikula 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1069ee03672dSJarkko Nikula 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1070ee03672dSJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
107120f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
107251eea52dSLubomir Rintel 			controller->max_speed_hz
1073ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1074b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1075ee03672dSJarkko Nikula 	else
107620f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
107751eea52dSLubomir Rintel 			controller->max_speed_hz / 2
1078ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1079b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1080ee03672dSJarkko Nikula 
1081a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
1082c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
1083c039dd27SJarkko Nikula 		    != chip->lpss_rx_threshold)
1084c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSIRF,
1085c039dd27SJarkko Nikula 					 chip->lpss_rx_threshold);
1086c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
1087c039dd27SJarkko Nikula 		    != chip->lpss_tx_threshold)
1088c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSITF,
1089c039dd27SJarkko Nikula 					 chip->lpss_tx_threshold);
1090a0d2642eSMika Westerberg 	}
1091a0d2642eSMika Westerberg 
1092e5262d05SWeike Chen 	if (is_quark_x1000_ssp(drv_data) &&
1093c039dd27SJarkko Nikula 	    (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
1094c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
1095e5262d05SWeike Chen 
1096ca632f55SGrant Likely 	/* see if we need to reload the config registers */
1097c039dd27SJarkko Nikula 	if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1098c039dd27SJarkko Nikula 	    || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1099c039dd27SJarkko Nikula 	    != (cr1 & change_mask)) {
1100ca632f55SGrant Likely 		/* stop the SSP, and update the other bits */
110141c98841SAndy Shevchenko 		if (!is_mmp2_ssp(drv_data))
1102c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
1103ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1104c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1105ca632f55SGrant Likely 		/* first set CR1 without interrupt and service enables */
1106c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
1107ca632f55SGrant Likely 		/* restart the SSP */
1108c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0);
1109ca632f55SGrant Likely 
1110ca632f55SGrant Likely 	} else {
1111ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1112c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1113ca632f55SGrant Likely 	}
1114ca632f55SGrant Likely 
111541c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data)) {
111682391856SLubomir Rintel 		u8 tx_level = (pxa2xx_spi_read(drv_data, SSSR)
111782391856SLubomir Rintel 					& SSSR_TFL_MASK) >> 8;
111882391856SLubomir Rintel 
111982391856SLubomir Rintel 		if (tx_level) {
112082391856SLubomir Rintel 			/* On MMP2, flipping SSE doesn't to empty TXFIFO. */
112182391856SLubomir Rintel 			dev_warn(&spi->dev, "%d bytes of garbage in TXFIFO!\n",
112282391856SLubomir Rintel 								tx_level);
112382391856SLubomir Rintel 			if (tx_level > transfer->len)
112482391856SLubomir Rintel 				tx_level = transfer->len;
112582391856SLubomir Rintel 			drv_data->tx += tx_level;
112682391856SLubomir Rintel 		}
112782391856SLubomir Rintel 	}
112882391856SLubomir Rintel 
112951eea52dSLubomir Rintel 	if (spi_controller_is_slave(controller)) {
1130ec93cb6fSLubomir Rintel 		while (drv_data->write(drv_data))
1131ec93cb6fSLubomir Rintel 			;
113277d33897SLubomir Rintel 		if (drv_data->gpiod_ready) {
113377d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 1);
113477d33897SLubomir Rintel 			udelay(1);
113577d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 0);
113677d33897SLubomir Rintel 		}
1137ec93cb6fSLubomir Rintel 	}
1138ec93cb6fSLubomir Rintel 
1139d5898e19SJarkko Nikula 	/*
1140d5898e19SJarkko Nikula 	 * Release the data by enabling service requests and interrupts,
1141d5898e19SJarkko Nikula 	 * without changing any mode bits
1142d5898e19SJarkko Nikula 	 */
1143c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1144d5898e19SJarkko Nikula 
1145d5898e19SJarkko Nikula 	return 1;
1146ca632f55SGrant Likely }
1147ca632f55SGrant Likely 
114851eea52dSLubomir Rintel static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
1149ec93cb6fSLubomir Rintel {
115051eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1151ec93cb6fSLubomir Rintel 
1152ec93cb6fSLubomir Rintel 	/* Stop and reset SSP */
1153ec93cb6fSLubomir Rintel 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1154ec93cb6fSLubomir Rintel 	reset_sccr1(drv_data);
1155ec93cb6fSLubomir Rintel 	if (!pxa25x_ssp_comp(drv_data))
1156ec93cb6fSLubomir Rintel 		pxa2xx_spi_write(drv_data, SSTO, 0);
1157ec93cb6fSLubomir Rintel 	pxa2xx_spi_flush(drv_data);
115829d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
1159ec93cb6fSLubomir Rintel 
1160ec93cb6fSLubomir Rintel 	dev_dbg(&drv_data->pdev->dev, "transfer aborted\n");
1161ec93cb6fSLubomir Rintel 
116251eea52dSLubomir Rintel 	drv_data->controller->cur_msg->status = -EINTR;
116351eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
1164ec93cb6fSLubomir Rintel 
1165ec93cb6fSLubomir Rintel 	return 0;
1166ec93cb6fSLubomir Rintel }
1167ec93cb6fSLubomir Rintel 
116851eea52dSLubomir Rintel static void pxa2xx_spi_handle_err(struct spi_controller *controller,
11697f86bde9SMika Westerberg 				 struct spi_message *msg)
1170ca632f55SGrant Likely {
117151eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1172ca632f55SGrant Likely 
1173d5898e19SJarkko Nikula 	/* Disable the SSP */
117429d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
1175d5898e19SJarkko Nikula 	/* Clear and disable interrupts and service requests */
1176d5898e19SJarkko Nikula 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1177d5898e19SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1,
1178d5898e19SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR1)
1179d5898e19SJarkko Nikula 			 & ~(drv_data->int_cr1 | drv_data->dma_cr1));
1180d5898e19SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
1181d5898e19SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1182ca632f55SGrant Likely 
1183d5898e19SJarkko Nikula 	/*
1184d5898e19SJarkko Nikula 	 * Stop the DMA if running. Note DMA callback handler may have unset
1185d5898e19SJarkko Nikula 	 * the dma_running already, which is fine as stopping is not needed
1186d5898e19SJarkko Nikula 	 * then but we shouldn't rely this flag for anything else than
1187d5898e19SJarkko Nikula 	 * stopping. For instance to differentiate between PIO and DMA
1188d5898e19SJarkko Nikula 	 * transfers.
1189d5898e19SJarkko Nikula 	 */
1190d5898e19SJarkko Nikula 	if (atomic_read(&drv_data->dma_running))
1191d5898e19SJarkko Nikula 		pxa2xx_spi_dma_stop(drv_data);
1192ca632f55SGrant Likely }
1193ca632f55SGrant Likely 
119451eea52dSLubomir Rintel static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
11957d94a505SMika Westerberg {
119651eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
11977d94a505SMika Westerberg 
11987d94a505SMika Westerberg 	/* Disable the SSP now */
119929d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
12007d94a505SMika Westerberg 
12017d94a505SMika Westerberg 	return 0;
12027d94a505SMika Westerberg }
12037d94a505SMika Westerberg 
1204ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1205ca632f55SGrant Likely 		    struct pxa2xx_spi_chip *chip_info)
1206ca632f55SGrant Likely {
12073cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12083cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1209c18d925fSJan Kiszka 	struct gpio_desc *gpiod;
1210ca632f55SGrant Likely 	int err = 0;
1211ca632f55SGrant Likely 
121299f499cdSMika Westerberg 	if (chip == NULL)
121399f499cdSMika Westerberg 		return 0;
121499f499cdSMika Westerberg 
12156ac5a435SAndy Shevchenko 	if (drv_data->cs_gpiods) {
12166ac5a435SAndy Shevchenko 		gpiod = drv_data->cs_gpiods[spi->chip_select];
12176ac5a435SAndy Shevchenko 		if (gpiod) {
1218c18d925fSJan Kiszka 			chip->gpiod_cs = gpiod;
121999f499cdSMika Westerberg 			chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
122099f499cdSMika Westerberg 			gpiod_set_value(gpiod, chip->gpio_cs_inverted);
12216ac5a435SAndy Shevchenko 		}
122299f499cdSMika Westerberg 
122399f499cdSMika Westerberg 		return 0;
122499f499cdSMika Westerberg 	}
122599f499cdSMika Westerberg 
122699f499cdSMika Westerberg 	if (chip_info == NULL)
1227ca632f55SGrant Likely 		return 0;
1228ca632f55SGrant Likely 
1229ca632f55SGrant Likely 	/* NOTE: setup() can be called multiple times, possibly with
1230ca632f55SGrant Likely 	 * different chip_info, release previously requested GPIO
1231ca632f55SGrant Likely 	 */
1232c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
1233a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1234c18d925fSJan Kiszka 		chip->gpiod_cs = NULL;
1235c18d925fSJan Kiszka 	}
1236ca632f55SGrant Likely 
1237ca632f55SGrant Likely 	/* If (*cs_control) is provided, ignore GPIO chip select */
1238ca632f55SGrant Likely 	if (chip_info->cs_control) {
1239ca632f55SGrant Likely 		chip->cs_control = chip_info->cs_control;
1240ca632f55SGrant Likely 		return 0;
1241ca632f55SGrant Likely 	}
1242ca632f55SGrant Likely 
1243ca632f55SGrant Likely 	if (gpio_is_valid(chip_info->gpio_cs)) {
1244ca632f55SGrant Likely 		err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1245ca632f55SGrant Likely 		if (err) {
1246f6bd03a7SJarkko Nikula 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1247f6bd03a7SJarkko Nikula 				chip_info->gpio_cs);
1248ca632f55SGrant Likely 			return err;
1249ca632f55SGrant Likely 		}
1250ca632f55SGrant Likely 
1251c18d925fSJan Kiszka 		gpiod = gpio_to_desc(chip_info->gpio_cs);
1252c18d925fSJan Kiszka 		chip->gpiod_cs = gpiod;
1253ca632f55SGrant Likely 		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1254ca632f55SGrant Likely 
1255c18d925fSJan Kiszka 		err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
1256ca632f55SGrant Likely 	}
1257ca632f55SGrant Likely 
1258ca632f55SGrant Likely 	return err;
1259ca632f55SGrant Likely }
1260ca632f55SGrant Likely 
1261ca632f55SGrant Likely static int setup(struct spi_device *spi)
1262ca632f55SGrant Likely {
1263bffc967eSJarkko Nikula 	struct pxa2xx_spi_chip *chip_info;
1264ca632f55SGrant Likely 	struct chip_data *chip;
1265dccf7369SJarkko Nikula 	const struct lpss_config *config;
12663cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12673cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1268a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1269a0d2642eSMika Westerberg 
1270e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1271e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1272e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1273e5262d05SWeike Chen 		tx_hi_thres = 0;
1274e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1275e5262d05SWeike Chen 		break;
12767c7289a4SAndy Shevchenko 	case CE4100_SSP:
12777c7289a4SAndy Shevchenko 		tx_thres = TX_THRESH_CE4100_DFLT;
12787c7289a4SAndy Shevchenko 		tx_hi_thres = 0;
12797c7289a4SAndy Shevchenko 		rx_thres = RX_THRESH_CE4100_DFLT;
12807c7289a4SAndy Shevchenko 		break;
128103fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
128203fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
128330f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
128434cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1285b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1286fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
1287dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1288dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1289dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1290dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1291e5262d05SWeike Chen 		break;
1292e5262d05SWeike Chen 	default:
1293a0d2642eSMika Westerberg 		tx_hi_thres = 0;
129451eea52dSLubomir Rintel 		if (spi_controller_is_slave(drv_data->controller)) {
1295ec93cb6fSLubomir Rintel 			tx_thres = 1;
1296ec93cb6fSLubomir Rintel 			rx_thres = 2;
1297ec93cb6fSLubomir Rintel 		} else {
1298ec93cb6fSLubomir Rintel 			tx_thres = TX_THRESH_DFLT;
1299a0d2642eSMika Westerberg 			rx_thres = RX_THRESH_DFLT;
1300ec93cb6fSLubomir Rintel 		}
1301e5262d05SWeike Chen 		break;
1302a0d2642eSMika Westerberg 	}
1303ca632f55SGrant Likely 
1304ca632f55SGrant Likely 	/* Only alloc on first setup */
1305ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1306ca632f55SGrant Likely 	if (!chip) {
1307ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
13089deae459SJingoo Han 		if (!chip)
1309ca632f55SGrant Likely 			return -ENOMEM;
1310ca632f55SGrant Likely 
1311ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1312ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1313f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1314f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1315ca632f55SGrant Likely 				kfree(chip);
1316ca632f55SGrant Likely 				return -EINVAL;
1317ca632f55SGrant Likely 			}
1318ca632f55SGrant Likely 
1319ca632f55SGrant Likely 			chip->frm = spi->chip_select;
1320c18d925fSJan Kiszka 		}
132151eea52dSLubomir Rintel 		chip->enable_dma = drv_data->controller_info->enable_dma;
1322ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1323ca632f55SGrant Likely 	}
1324ca632f55SGrant Likely 
1325ca632f55SGrant Likely 	/* protocol drivers may change the chip settings, so...
1326ca632f55SGrant Likely 	 * if chip_info exists, use it */
1327ca632f55SGrant Likely 	chip_info = spi->controller_data;
1328ca632f55SGrant Likely 
1329ca632f55SGrant Likely 	/* chip_info isn't always needed */
1330ca632f55SGrant Likely 	chip->cr1 = 0;
1331ca632f55SGrant Likely 	if (chip_info) {
1332ca632f55SGrant Likely 		if (chip_info->timeout)
1333ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1334ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1335ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1336a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1337a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1338ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1339ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1340ca632f55SGrant Likely 		chip->dma_threshold = 0;
1341ca632f55SGrant Likely 		if (chip_info->enable_loopback)
1342ca632f55SGrant Likely 			chip->cr1 = SSCR1_LBM;
1343ca632f55SGrant Likely 	}
134451eea52dSLubomir Rintel 	if (spi_controller_is_slave(drv_data->controller)) {
1345ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCFR;
1346ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCLKDIR;
1347ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SFRMDIR;
1348ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SPH;
1349ec93cb6fSLubomir Rintel 	}
1350ca632f55SGrant Likely 
1351a0d2642eSMika Westerberg 	chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1352a0d2642eSMika Westerberg 	chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1353a0d2642eSMika Westerberg 				| SSITF_TxHiThresh(tx_hi_thres);
1354a0d2642eSMika Westerberg 
1355ca632f55SGrant Likely 	/* set dma burst and threshold outside of chip_info path so that if
1356ca632f55SGrant Likely 	 * chip_info goes away after setting chip->enable_dma, the
1357ca632f55SGrant Likely 	 * burst and threshold can still respond to changes in bits_per_word */
1358ca632f55SGrant Likely 	if (chip->enable_dma) {
1359ca632f55SGrant Likely 		/* set up legal burst and threshold for dma */
1360cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1361cd7bed00SMika Westerberg 						spi->bits_per_word,
1362ca632f55SGrant Likely 						&chip->dma_burst_size,
1363ca632f55SGrant Likely 						&chip->dma_threshold)) {
1364f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1365f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1366ca632f55SGrant Likely 		}
1367000c6af4SAndy Shevchenko 		dev_dbg(&spi->dev,
1368000c6af4SAndy Shevchenko 			"in setup: DMA burst size set to %u\n",
1369000c6af4SAndy Shevchenko 			chip->dma_burst_size);
1370ca632f55SGrant Likely 	}
1371ca632f55SGrant Likely 
1372e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1373e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1374e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1375e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1376e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1377e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1378e5262d05SWeike Chen 		break;
13797c7289a4SAndy Shevchenko 	case CE4100_SSP:
13807c7289a4SAndy Shevchenko 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
13817c7289a4SAndy Shevchenko 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
13827c7289a4SAndy Shevchenko 		break;
1383e5262d05SWeike Chen 	default:
1384e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1385e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1386e5262d05SWeike Chen 		break;
1387e5262d05SWeike Chen 	}
1388e5262d05SWeike Chen 
1389ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1390ca632f55SGrant Likely 	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1391ca632f55SGrant Likely 			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1392ca632f55SGrant Likely 
1393b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1394b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1395b833172fSMika Westerberg 
1396ca632f55SGrant Likely 	if (spi->bits_per_word <= 8) {
1397ca632f55SGrant Likely 		chip->n_bytes = 1;
1398ca632f55SGrant Likely 		chip->read = u8_reader;
1399ca632f55SGrant Likely 		chip->write = u8_writer;
1400ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 16) {
1401ca632f55SGrant Likely 		chip->n_bytes = 2;
1402ca632f55SGrant Likely 		chip->read = u16_reader;
1403ca632f55SGrant Likely 		chip->write = u16_writer;
1404ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 32) {
1405ca632f55SGrant Likely 		chip->n_bytes = 4;
1406ca632f55SGrant Likely 		chip->read = u32_reader;
1407ca632f55SGrant Likely 		chip->write = u32_writer;
1408ca632f55SGrant Likely 	}
1409ca632f55SGrant Likely 
1410ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1411ca632f55SGrant Likely 
1412ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
1413ca632f55SGrant Likely 		return 0;
1414ca632f55SGrant Likely 
1415ca632f55SGrant Likely 	return setup_cs(spi, chip, chip_info);
1416ca632f55SGrant Likely }
1417ca632f55SGrant Likely 
1418ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1419ca632f55SGrant Likely {
1420ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
14213cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
14223cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1423ca632f55SGrant Likely 
1424ca632f55SGrant Likely 	if (!chip)
1425ca632f55SGrant Likely 		return;
1426ca632f55SGrant Likely 
14276ac5a435SAndy Shevchenko 	if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
1428c18d925fSJan Kiszka 	    chip->gpiod_cs)
1429a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1430ca632f55SGrant Likely 
1431ca632f55SGrant Likely 	kfree(chip);
1432ca632f55SGrant Likely }
1433ca632f55SGrant Likely 
14348422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
143503fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
143603fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
143703fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
143803fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
143903fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
144030f3a6abSMika Westerberg 	{ "8086228E", LPSS_BSW_SSP },
144103fbf488SJarkko Nikula 	{ },
144203fbf488SJarkko Nikula };
144303fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
144403fbf488SJarkko Nikula 
144534cadd9cSJarkko Nikula /*
144634cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
144734cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
144834cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
144934cadd9cSJarkko Nikula  */
145034cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
145134cadd9cSJarkko Nikula 	/* SPT-LP */
145234cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
145334cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
145434cadd9cSJarkko Nikula 	/* SPT-H */
145534cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
145634cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1457704d2b07SMika Westerberg 	/* KBL-H */
1458704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1459704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
14606157d4c2SJarkko Nikula 	/* CML-V */
14616157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
14626157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
1463c1b03f11SJarkko Nikula 	/* BXT A-Step */
1464b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1465b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1466b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1467c1b03f11SJarkko Nikula 	/* BXT B-Step */
1468c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1469c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1470c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1471e18a80acSDavid E. Box 	/* GLK */
1472e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1473e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1474e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
147522d71a50SMika Westerberg 	/* ICL-LP */
147622d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
147722d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
147822d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
14798cc77204SJarkko Nikula 	/* EHL */
14808cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
14818cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
14828cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
14839c7315c9SJarkko Nikula 	/* JSL */
14849c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
14859c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
14869c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
1487b7c08cf8SJarkko Nikula 	/* APL */
1488b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1489b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1490b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1491fc0b2accSJarkko Nikula 	/* CNL-LP */
1492fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1493fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1494fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1495fc0b2accSJarkko Nikula 	/* CNL-H */
1496fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1497fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1498fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
149941a91802SEvan Green 	/* CML-LP */
150041a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
150141a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
150241a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
1503f0cf17edSJarkko Nikula 	/* CML-H */
1504f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1505f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1506f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
1507a4127952SJarkko Nikula 	/* TGL-LP */
1508a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1509a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1510a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1511a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1512a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1513a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1514a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
151594e5c23dSAxel Lin 	{ },
151634cadd9cSJarkko Nikula };
151734cadd9cSJarkko Nikula 
151887ae1d2dSLubomir Rintel static const struct of_device_id pxa2xx_spi_of_match[] = {
151987ae1d2dSLubomir Rintel 	{ .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
152087ae1d2dSLubomir Rintel 	{},
152187ae1d2dSLubomir Rintel };
152287ae1d2dSLubomir Rintel MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
152387ae1d2dSLubomir Rintel 
152487ae1d2dSLubomir Rintel #ifdef CONFIG_ACPI
152587ae1d2dSLubomir Rintel 
1526365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
152787ae1d2dSLubomir Rintel {
1528365e856eSAndy Shevchenko 	struct acpi_device *adev;
152987ae1d2dSLubomir Rintel 	unsigned int devid;
153087ae1d2dSLubomir Rintel 	int port_id = -1;
153187ae1d2dSLubomir Rintel 
1532365e856eSAndy Shevchenko 	adev = ACPI_COMPANION(dev);
153387ae1d2dSLubomir Rintel 	if (adev && adev->pnp.unique_id &&
153487ae1d2dSLubomir Rintel 	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
153587ae1d2dSLubomir Rintel 		port_id = devid;
153687ae1d2dSLubomir Rintel 	return port_id;
153787ae1d2dSLubomir Rintel }
153887ae1d2dSLubomir Rintel 
153987ae1d2dSLubomir Rintel #else /* !CONFIG_ACPI */
154087ae1d2dSLubomir Rintel 
1541365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
154287ae1d2dSLubomir Rintel {
154387ae1d2dSLubomir Rintel 	return -1;
154487ae1d2dSLubomir Rintel }
154587ae1d2dSLubomir Rintel 
154687ae1d2dSLubomir Rintel #endif /* CONFIG_ACPI */
154787ae1d2dSLubomir Rintel 
154887ae1d2dSLubomir Rintel 
154987ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
155087ae1d2dSLubomir Rintel 
155134cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
155234cadd9cSJarkko Nikula {
15535ba846b1SAndy Shevchenko 	return param == chan->device->dev;
155434cadd9cSJarkko Nikula }
155534cadd9cSJarkko Nikula 
155687ae1d2dSLubomir Rintel #endif /* CONFIG_PCI */
155787ae1d2dSLubomir Rintel 
155851eea52dSLubomir Rintel static struct pxa2xx_spi_controller *
15590db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1560a3496855SMika Westerberg {
156151eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *pdata;
1562a3496855SMika Westerberg 	struct ssp_device *ssp;
1563a3496855SMika Westerberg 	struct resource *res;
15646fb7427dSAndy Shevchenko 	struct device *parent = pdev->dev.parent;
15656fb7427dSAndy Shevchenko 	struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
156634cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
156755ef8262SLubomir Rintel 	enum pxa_ssp_type type;
1568f2faa3ecSAndy Shevchenko 	const void *match;
1569a3496855SMika Westerberg 
15706fb7427dSAndy Shevchenko 	if (pcidev)
15716fb7427dSAndy Shevchenko 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
1572a3496855SMika Westerberg 
1573f2faa3ecSAndy Shevchenko 	match = device_get_match_data(&pdev->dev);
1574f2faa3ecSAndy Shevchenko 	if (match)
1575f2faa3ecSAndy Shevchenko 		type = (enum pxa_ssp_type)match;
157634cadd9cSJarkko Nikula 	else if (pcidev_id)
157755ef8262SLubomir Rintel 		type = (enum pxa_ssp_type)pcidev_id->driver_data;
157803fbf488SJarkko Nikula 	else
157914af1df3SAndy Shevchenko 		return ERR_PTR(-EINVAL);
158003fbf488SJarkko Nikula 
1581cc0ee987SMika Westerberg 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
15829deae459SJingoo Han 	if (!pdata)
158314af1df3SAndy Shevchenko 		return ERR_PTR(-ENOMEM);
1584a3496855SMika Westerberg 
1585a3496855SMika Westerberg 	ssp = &pdata->ssp;
1586a3496855SMika Westerberg 
158777c544d2SAndy Shevchenko 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1588cbfd6a21SSachin Kamat 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1589cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
159014af1df3SAndy Shevchenko 		return ERR_CAST(ssp->mmio_base);
1591a3496855SMika Westerberg 
159277c544d2SAndy Shevchenko 	ssp->phys_base = res->start;
159377c544d2SAndy Shevchenko 
159487ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
159534cadd9cSJarkko Nikula 	if (pcidev_id) {
15966fb7427dSAndy Shevchenko 		pdata->tx_param = parent;
15976fb7427dSAndy Shevchenko 		pdata->rx_param = parent;
159834cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
159934cadd9cSJarkko Nikula 	}
160087ae1d2dSLubomir Rintel #endif
160134cadd9cSJarkko Nikula 
1602a3496855SMika Westerberg 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
16035eb263efSChuhong Yuan 	if (IS_ERR(ssp->clk))
160414af1df3SAndy Shevchenko 		return ERR_CAST(ssp->clk);
1605a3496855SMika Westerberg 
1606a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
16075eb263efSChuhong Yuan 	if (ssp->irq < 0)
160814af1df3SAndy Shevchenko 		return ERR_PTR(ssp->irq);
16095eb263efSChuhong Yuan 
1610a3496855SMika Westerberg 	ssp->type = type;
16114f3d9577SAndy Shevchenko 	ssp->dev = &pdev->dev;
1612365e856eSAndy Shevchenko 	ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
1613a3496855SMika Westerberg 
1614f2faa3ecSAndy Shevchenko 	pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
1615a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1616cddb339bSMika Westerberg 	pdata->enable_dma = true;
161737821a82SAndy Shevchenko 	pdata->dma_burst_size = 1;
1618a3496855SMika Westerberg 
1619a3496855SMika Westerberg 	return pdata;
1620a3496855SMika Westerberg }
1621a3496855SMika Westerberg 
162251eea52dSLubomir Rintel static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
16233cc7b0e3SJarkko Nikula 				      unsigned int cs)
16240c27d9cfSMika Westerberg {
162551eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
16260c27d9cfSMika Westerberg 
16270c27d9cfSMika Westerberg 	if (has_acpi_companion(&drv_data->pdev->dev)) {
16280c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
16290c27d9cfSMika Westerberg 		/*
16300c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
16310c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
16320c27d9cfSMika Westerberg 		 * to match what Linux expects.
16330c27d9cfSMika Westerberg 		 */
16340c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
163530f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
16360c27d9cfSMika Westerberg 			return cs - 1;
16370c27d9cfSMika Westerberg 
16380c27d9cfSMika Westerberg 		default:
16390c27d9cfSMika Westerberg 			break;
16400c27d9cfSMika Westerberg 		}
16410c27d9cfSMika Westerberg 	}
16420c27d9cfSMika Westerberg 
16430c27d9cfSMika Westerberg 	return cs;
16440c27d9cfSMika Westerberg }
16450c27d9cfSMika Westerberg 
1646b2662a16SDaniel Vetter static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1647b2662a16SDaniel Vetter {
1648b2662a16SDaniel Vetter 	return MAX_DMA_LEN;
1649b2662a16SDaniel Vetter }
1650b2662a16SDaniel Vetter 
1651fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1652ca632f55SGrant Likely {
1653ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
165451eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *platform_info;
165551eea52dSLubomir Rintel 	struct spi_controller *controller;
1656ca632f55SGrant Likely 	struct driver_data *drv_data;
1657ca632f55SGrant Likely 	struct ssp_device *ssp;
16588b136baaSJarkko Nikula 	const struct lpss_config *config;
165999f499cdSMika Westerberg 	int status, count;
1660c039dd27SJarkko Nikula 	u32 tmp;
1661ca632f55SGrant Likely 
1662851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1663851bacf5SMika Westerberg 	if (!platform_info) {
16640db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
166514af1df3SAndy Shevchenko 		if (IS_ERR(platform_info)) {
1666851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
166714af1df3SAndy Shevchenko 			return PTR_ERR(platform_info);
1668851bacf5SMika Westerberg 		}
1669a3496855SMika Westerberg 	}
1670ca632f55SGrant Likely 
1671ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1672851bacf5SMika Westerberg 	if (!ssp)
1673851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1674851bacf5SMika Westerberg 
1675851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
1676851bacf5SMika Westerberg 		dev_err(&pdev->dev, "failed to get ssp\n");
1677ca632f55SGrant Likely 		return -ENODEV;
1678ca632f55SGrant Likely 	}
1679ca632f55SGrant Likely 
1680ec93cb6fSLubomir Rintel 	if (platform_info->is_slave)
168151eea52dSLubomir Rintel 		controller = spi_alloc_slave(dev, sizeof(struct driver_data));
1682ec93cb6fSLubomir Rintel 	else
168351eea52dSLubomir Rintel 		controller = spi_alloc_master(dev, sizeof(struct driver_data));
1684ec93cb6fSLubomir Rintel 
168551eea52dSLubomir Rintel 	if (!controller) {
168651eea52dSLubomir Rintel 		dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1687ca632f55SGrant Likely 		pxa_ssp_free(ssp);
1688ca632f55SGrant Likely 		return -ENOMEM;
1689ca632f55SGrant Likely 	}
169051eea52dSLubomir Rintel 	drv_data = spi_controller_get_devdata(controller);
169151eea52dSLubomir Rintel 	drv_data->controller = controller;
169251eea52dSLubomir Rintel 	drv_data->controller_info = platform_info;
1693ca632f55SGrant Likely 	drv_data->pdev = pdev;
1694ca632f55SGrant Likely 	drv_data->ssp = ssp;
1695ca632f55SGrant Likely 
169651eea52dSLubomir Rintel 	controller->dev.of_node = pdev->dev.of_node;
1697ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
169851eea52dSLubomir Rintel 	controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1699ca632f55SGrant Likely 
170051eea52dSLubomir Rintel 	controller->bus_num = ssp->port_id;
170151eea52dSLubomir Rintel 	controller->dma_alignment = DMA_ALIGNMENT;
170251eea52dSLubomir Rintel 	controller->cleanup = cleanup;
170351eea52dSLubomir Rintel 	controller->setup = setup;
170451eea52dSLubomir Rintel 	controller->set_cs = pxa2xx_spi_set_cs;
170551eea52dSLubomir Rintel 	controller->transfer_one = pxa2xx_spi_transfer_one;
170651eea52dSLubomir Rintel 	controller->slave_abort = pxa2xx_spi_slave_abort;
170751eea52dSLubomir Rintel 	controller->handle_err = pxa2xx_spi_handle_err;
170851eea52dSLubomir Rintel 	controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
170951eea52dSLubomir Rintel 	controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
171051eea52dSLubomir Rintel 	controller->auto_runtime_pm = true;
171151eea52dSLubomir Rintel 	controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1712ca632f55SGrant Likely 
1713ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1714ca632f55SGrant Likely 
1715ca632f55SGrant Likely 	drv_data->ioaddr = ssp->mmio_base;
1716ca632f55SGrant Likely 	drv_data->ssdr_physical = ssp->phys_base + SSDR;
1717ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1718e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1719e5262d05SWeike Chen 		case QUARK_X1000_SSP:
172051eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1721e5262d05SWeike Chen 			break;
1722e5262d05SWeike Chen 		default:
172351eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1724e5262d05SWeike Chen 			break;
1725e5262d05SWeike Chen 		}
1726e5262d05SWeike Chen 
1727ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1728ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1729ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1730ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1731ca632f55SGrant Likely 	} else {
173251eea52dSLubomir Rintel 		controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1733ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
17345928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1735ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1736ec93cb6fSLubomir Rintel 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1737ec93cb6fSLubomir Rintel 						| SSSR_ROR | SSSR_TUR;
1738ca632f55SGrant Likely 	}
1739ca632f55SGrant Likely 
1740ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1741ca632f55SGrant Likely 			drv_data);
1742ca632f55SGrant Likely 	if (status < 0) {
1743ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
174451eea52dSLubomir Rintel 		goto out_error_controller_alloc;
1745ca632f55SGrant Likely 	}
1746ca632f55SGrant Likely 
1747ca632f55SGrant Likely 	/* Setup DMA if requested */
1748ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1749cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1750cd7bed00SMika Westerberg 		if (status) {
17518b57b11bSFlavio Suligoi 			dev_warn(dev, "no DMA channels available, using PIO\n");
1752cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1753b6ced294SJarkko Nikula 		} else {
175451eea52dSLubomir Rintel 			controller->can_dma = pxa2xx_spi_can_dma;
1755bf9f742cSMark Brown 			controller->max_dma_len = MAX_DMA_LEN;
1756b2662a16SDaniel Vetter 			controller->max_transfer_size =
1757b2662a16SDaniel Vetter 				pxa2xx_spi_max_dma_transfer_size;
1758ca632f55SGrant Likely 		}
1759ca632f55SGrant Likely 	}
1760ca632f55SGrant Likely 
1761ca632f55SGrant Likely 	/* Enable SOC clock */
176262bbc864STobias Jordan 	status = clk_prepare_enable(ssp->clk);
176362bbc864STobias Jordan 	if (status)
176462bbc864STobias Jordan 		goto out_error_dma_irq_alloc;
17653343b7a6SMika Westerberg 
176651eea52dSLubomir Rintel 	controller->max_speed_hz = clk_get_rate(ssp->clk);
176723cdddb2SJarkko Nikula 	/*
176823cdddb2SJarkko Nikula 	 * Set minimum speed for all other platforms than Intel Quark which is
176923cdddb2SJarkko Nikula 	 * able do under 1 Hz transfers.
177023cdddb2SJarkko Nikula 	 */
177123cdddb2SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
177223cdddb2SJarkko Nikula 		controller->min_speed_hz =
177323cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 4096);
177423cdddb2SJarkko Nikula 	else if (!is_quark_x1000_ssp(drv_data))
177523cdddb2SJarkko Nikula 		controller->min_speed_hz =
177623cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 512);
1777ca632f55SGrant Likely 
1778ca632f55SGrant Likely 	/* Load default SSP configuration */
1779c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
1780e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1781e5262d05SWeike Chen 	case QUARK_X1000_SSP:
17827c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
17837c7289a4SAndy Shevchenko 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1784c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1785e5262d05SWeike Chen 
1786e5262d05SWeike Chen 		/* using the Motorola SPI protocol and use 8 bit frame */
17877c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
17887c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1789e5262d05SWeike Chen 		break;
17907c7289a4SAndy Shevchenko 	case CE4100_SSP:
17917c7289a4SAndy Shevchenko 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
17927c7289a4SAndy Shevchenko 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
17937c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
17947c7289a4SAndy Shevchenko 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
17957c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1796a2dd8af0SAndy Shevchenko 		break;
1797e5262d05SWeike Chen 	default:
1798ec93cb6fSLubomir Rintel 
179951eea52dSLubomir Rintel 		if (spi_controller_is_slave(controller)) {
1800ec93cb6fSLubomir Rintel 			tmp = SSCR1_SCFR |
1801ec93cb6fSLubomir Rintel 			      SSCR1_SCLKDIR |
1802ec93cb6fSLubomir Rintel 			      SSCR1_SFRMDIR |
1803ec93cb6fSLubomir Rintel 			      SSCR1_RxTresh(2) |
1804ec93cb6fSLubomir Rintel 			      SSCR1_TxTresh(1) |
1805ec93cb6fSLubomir Rintel 			      SSCR1_SPH;
1806ec93cb6fSLubomir Rintel 		} else {
1807c039dd27SJarkko Nikula 			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1808c039dd27SJarkko Nikula 			      SSCR1_TxTresh(TX_THRESH_DFLT);
1809ec93cb6fSLubomir Rintel 		}
1810c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1811ec93cb6fSLubomir Rintel 		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
181251eea52dSLubomir Rintel 		if (!spi_controller_is_slave(controller))
1813ec93cb6fSLubomir Rintel 			tmp |= SSCR0_SCR(2);
1814c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1815e5262d05SWeike Chen 		break;
1816e5262d05SWeike Chen 	}
1817e5262d05SWeike Chen 
1818ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1819c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1820e5262d05SWeike Chen 
1821e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1822c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1823ca632f55SGrant Likely 
18248b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
18258b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
18268b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
18278b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
18288b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
18298b136baaSJarkko Nikula 						   config->reg_capabilities);
18308b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
18318b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
18328b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
183330f3a6abSMika Westerberg 		} else if (config->cs_num) {
183430f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
18358b136baaSJarkko Nikula 		}
18368b136baaSJarkko Nikula 	}
183751eea52dSLubomir Rintel 	controller->num_chipselect = platform_info->num_chipselect;
18388b136baaSJarkko Nikula 
183999f499cdSMika Westerberg 	count = gpiod_count(&pdev->dev, "cs");
18406ac5a435SAndy Shevchenko 	if (count > 0) {
18416ac5a435SAndy Shevchenko 		int i;
18426ac5a435SAndy Shevchenko 
184351eea52dSLubomir Rintel 		controller->num_chipselect = max_t(int, count,
184451eea52dSLubomir Rintel 			controller->num_chipselect);
184599f499cdSMika Westerberg 
18466ac5a435SAndy Shevchenko 		drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
184751eea52dSLubomir Rintel 			controller->num_chipselect, sizeof(struct gpio_desc *),
18486ac5a435SAndy Shevchenko 			GFP_KERNEL);
18496ac5a435SAndy Shevchenko 		if (!drv_data->cs_gpiods) {
18506ac5a435SAndy Shevchenko 			status = -ENOMEM;
18516ac5a435SAndy Shevchenko 			goto out_error_clock_enabled;
18526ac5a435SAndy Shevchenko 		}
18536ac5a435SAndy Shevchenko 
185451eea52dSLubomir Rintel 		for (i = 0; i < controller->num_chipselect; i++) {
18556ac5a435SAndy Shevchenko 			struct gpio_desc *gpiod;
18566ac5a435SAndy Shevchenko 
1857d35f2dc9SAndy Shevchenko 			gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
18586ac5a435SAndy Shevchenko 			if (IS_ERR(gpiod)) {
18596ac5a435SAndy Shevchenko 				/* Means use native chip select */
18606ac5a435SAndy Shevchenko 				if (PTR_ERR(gpiod) == -ENOENT)
18616ac5a435SAndy Shevchenko 					continue;
18626ac5a435SAndy Shevchenko 
186377d33897SLubomir Rintel 				status = PTR_ERR(gpiod);
18646ac5a435SAndy Shevchenko 				goto out_error_clock_enabled;
18656ac5a435SAndy Shevchenko 			} else {
18666ac5a435SAndy Shevchenko 				drv_data->cs_gpiods[i] = gpiod;
18676ac5a435SAndy Shevchenko 			}
18686ac5a435SAndy Shevchenko 		}
18696ac5a435SAndy Shevchenko 	}
18706ac5a435SAndy Shevchenko 
187177d33897SLubomir Rintel 	if (platform_info->is_slave) {
187277d33897SLubomir Rintel 		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
187377d33897SLubomir Rintel 						"ready", GPIOD_OUT_LOW);
187477d33897SLubomir Rintel 		if (IS_ERR(drv_data->gpiod_ready)) {
187577d33897SLubomir Rintel 			status = PTR_ERR(drv_data->gpiod_ready);
187677d33897SLubomir Rintel 			goto out_error_clock_enabled;
187777d33897SLubomir Rintel 		}
187877d33897SLubomir Rintel 	}
187977d33897SLubomir Rintel 
1880836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1881836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1882836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1883836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1884836d1a22SAntonio Ospite 
1885ca632f55SGrant Likely 	/* Register with the SPI framework */
1886ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
1887*32e5b572SLukas Wunner 	status = spi_register_controller(controller);
1888ca632f55SGrant Likely 	if (status != 0) {
188951eea52dSLubomir Rintel 		dev_err(&pdev->dev, "problem registering spi controller\n");
189012742045SLubomir Rintel 		goto out_error_pm_runtime_enabled;
1891ca632f55SGrant Likely 	}
1892ca632f55SGrant Likely 
1893ca632f55SGrant Likely 	return status;
1894ca632f55SGrant Likely 
189512742045SLubomir Rintel out_error_pm_runtime_enabled:
1896e2b714afSJarkko Nikula 	pm_runtime_put_noidle(&pdev->dev);
1897e2b714afSJarkko Nikula 	pm_runtime_disable(&pdev->dev);
189812742045SLubomir Rintel 
189912742045SLubomir Rintel out_error_clock_enabled:
19003343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
190162bbc864STobias Jordan 
190262bbc864STobias Jordan out_error_dma_irq_alloc:
1903cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1904ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1905ca632f55SGrant Likely 
190651eea52dSLubomir Rintel out_error_controller_alloc:
190751eea52dSLubomir Rintel 	spi_controller_put(controller);
1908ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1909ca632f55SGrant Likely 	return status;
1910ca632f55SGrant Likely }
1911ca632f55SGrant Likely 
1912ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1913ca632f55SGrant Likely {
1914ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
19153d24b2a4SAndy Shevchenko 	struct ssp_device *ssp = drv_data->ssp;
1916ca632f55SGrant Likely 
19177d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
19187d94a505SMika Westerberg 
1919*32e5b572SLukas Wunner 	spi_unregister_controller(drv_data->controller);
1920*32e5b572SLukas Wunner 
1921ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
1922c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
19233343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1924ca632f55SGrant Likely 
1925ca632f55SGrant Likely 	/* Release DMA */
192651eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma)
1927cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1928ca632f55SGrant Likely 
19297d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
19307d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
19317d94a505SMika Westerberg 
1932ca632f55SGrant Likely 	/* Release IRQ */
1933ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1934ca632f55SGrant Likely 
1935ca632f55SGrant Likely 	/* Release SSP */
1936ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1937ca632f55SGrant Likely 
1938ca632f55SGrant Likely 	return 0;
1939ca632f55SGrant Likely }
1940ca632f55SGrant Likely 
1941382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP
1942ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1943ca632f55SGrant Likely {
1944ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1945ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1946bffc967eSJarkko Nikula 	int status;
1947ca632f55SGrant Likely 
194851eea52dSLubomir Rintel 	status = spi_controller_suspend(drv_data->controller);
1949ca632f55SGrant Likely 	if (status != 0)
1950ca632f55SGrant Likely 		return status;
1951c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
19522b9375b9SDmitry Eremin-Solenikov 
19532b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
19543343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1955ca632f55SGrant Likely 
1956ca632f55SGrant Likely 	return 0;
1957ca632f55SGrant Likely }
1958ca632f55SGrant Likely 
1959ca632f55SGrant Likely static int pxa2xx_spi_resume(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 
1965ca632f55SGrant Likely 	/* Enable the SSP clock */
196662bbc864STobias Jordan 	if (!pm_runtime_suspended(dev)) {
196762bbc864STobias Jordan 		status = clk_prepare_enable(ssp->clk);
196862bbc864STobias Jordan 		if (status)
196962bbc864STobias Jordan 			return status;
197062bbc864STobias Jordan 	}
1971ca632f55SGrant Likely 
1972ca632f55SGrant Likely 	/* Start the queue running */
197351eea52dSLubomir Rintel 	return spi_controller_resume(drv_data->controller);
1974ca632f55SGrant Likely }
19757d94a505SMika Westerberg #endif
19767d94a505SMika Westerberg 
1977ec833050SRafael J. Wysocki #ifdef CONFIG_PM
19787d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
19797d94a505SMika Westerberg {
19807d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
19817d94a505SMika Westerberg 
19827d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
19837d94a505SMika Westerberg 	return 0;
19847d94a505SMika Westerberg }
19857d94a505SMika Westerberg 
19867d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
19877d94a505SMika Westerberg {
19887d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
198962bbc864STobias Jordan 	int status;
19907d94a505SMika Westerberg 
199162bbc864STobias Jordan 	status = clk_prepare_enable(drv_data->ssp->clk);
199262bbc864STobias Jordan 	return status;
19937d94a505SMika Westerberg }
19947d94a505SMika Westerberg #endif
1995ca632f55SGrant Likely 
1996ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
19977d94a505SMika Westerberg 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
19987d94a505SMika Westerberg 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
19997d94a505SMika Westerberg 			   pxa2xx_spi_runtime_resume, NULL)
2000ca632f55SGrant Likely };
2001ca632f55SGrant Likely 
2002ca632f55SGrant Likely static struct platform_driver driver = {
2003ca632f55SGrant Likely 	.driver = {
2004ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
2005ca632f55SGrant Likely 		.pm	= &pxa2xx_spi_pm_ops,
2006a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
200787ae1d2dSLubomir Rintel 		.of_match_table = of_match_ptr(pxa2xx_spi_of_match),
2008ca632f55SGrant Likely 	},
2009ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
2010ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
2011ca632f55SGrant Likely };
2012ca632f55SGrant Likely 
2013ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
2014ca632f55SGrant Likely {
2015ca632f55SGrant Likely 	return platform_driver_register(&driver);
2016ca632f55SGrant Likely }
2017ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
2018ca632f55SGrant Likely 
2019ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
2020ca632f55SGrant Likely {
2021ca632f55SGrant Likely 	platform_driver_unregister(&driver);
2022ca632f55SGrant Likely }
2023ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
202451ebf6acSFlavio Suligoi 
202551ebf6acSFlavio Suligoi MODULE_SOFTDEP("pre: dw_dmac");
2026