xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision 70252440b2b6337d03f2b95bc475fedbea79072f)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
48083d6b8SAndy Shevchenko  * Copyright (C) 2013, 2021 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>
120e476871SAndy Shevchenko #include <linux/dmaengine.h>
13cbfd6a21SSachin Kamat #include <linux/err.h>
145ce25705SAndy Shevchenko #include <linux/errno.h>
155ce25705SAndy Shevchenko #include <linux/gpio/consumer.h>
165ce25705SAndy Shevchenko #include <linux/gpio.h>
175ce25705SAndy Shevchenko #include <linux/init.h>
18ca632f55SGrant Likely #include <linux/interrupt.h>
195ce25705SAndy Shevchenko #include <linux/ioport.h>
209df461ecSAndy Shevchenko #include <linux/kernel.h>
215ce25705SAndy Shevchenko #include <linux/module.h>
22ae8fbf1dSAndy Shevchenko #include <linux/mod_devicetable.h>
23ae8fbf1dSAndy Shevchenko #include <linux/of.h>
2434cadd9cSJarkko Nikula #include <linux/pci.h>
25ca632f55SGrant Likely #include <linux/platform_device.h>
265ce25705SAndy Shevchenko #include <linux/pm_runtime.h>
27f2faa3ecSAndy Shevchenko #include <linux/property.h>
285ce25705SAndy Shevchenko #include <linux/slab.h>
290e476871SAndy Shevchenko 
30ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h>
31ca632f55SGrant Likely #include <linux/spi/spi.h>
32ca632f55SGrant Likely 
33cd7bed00SMika Westerberg #include "spi-pxa2xx.h"
34ca632f55SGrant Likely 
35ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street");
36ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
37ca632f55SGrant Likely MODULE_LICENSE("GPL");
38ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi");
39ca632f55SGrant Likely 
40ca632f55SGrant Likely #define TIMOUT_DFLT		1000
41ca632f55SGrant Likely 
42ca632f55SGrant Likely /*
438083d6b8SAndy Shevchenko  * For testing SSCR1 changes that require SSP restart, basically
448083d6b8SAndy Shevchenko  * everything except the service and interrupt enables, the PXA270 developer
45ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
468083d6b8SAndy Shevchenko  * list, but the PXA255 developer manual says all bits without really meaning
478083d6b8SAndy Shevchenko  * the service and interrupt enables.
48ca632f55SGrant Likely  */
49ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
50ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
51ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
52ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
53ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
54ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
55ca632f55SGrant Likely 
56e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
57e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
58e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
59e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
60e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
61e5262d05SWeike Chen 
627c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
637c7289a4SAndy Shevchenko 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
647c7289a4SAndy Shevchenko 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
657c7289a4SAndy Shevchenko 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
667c7289a4SAndy Shevchenko 				| CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
677c7289a4SAndy Shevchenko 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
687c7289a4SAndy Shevchenko 
69624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
70624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE			BIT(0)
71624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
728b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT			9
738b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
74a0d2642eSMika Westerberg 
75683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE 0x38
76683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
77683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
78683f65deSEvan Green 
79dccf7369SJarkko Nikula struct lpss_config {
80dccf7369SJarkko Nikula 	/* LPSS offset from drv_data->ioaddr */
81dccf7369SJarkko Nikula 	unsigned offset;
82dccf7369SJarkko Nikula 	/* Register offsets from drv_data->lpss_base or -1 */
83dccf7369SJarkko Nikula 	int reg_general;
84dccf7369SJarkko Nikula 	int reg_ssp;
85dccf7369SJarkko Nikula 	int reg_cs_ctrl;
868b136baaSJarkko Nikula 	int reg_capabilities;
87dccf7369SJarkko Nikula 	/* FIFO thresholds */
88dccf7369SJarkko Nikula 	u32 rx_threshold;
89dccf7369SJarkko Nikula 	u32 tx_threshold_lo;
90dccf7369SJarkko Nikula 	u32 tx_threshold_hi;
91c1e4a53cSMika Westerberg 	/* Chip select control */
92c1e4a53cSMika Westerberg 	unsigned cs_sel_shift;
93c1e4a53cSMika Westerberg 	unsigned cs_sel_mask;
9430f3a6abSMika Westerberg 	unsigned cs_num;
95683f65deSEvan Green 	/* Quirks */
96683f65deSEvan Green 	unsigned cs_clk_stays_gated : 1;
97dccf7369SJarkko Nikula };
98dccf7369SJarkko Nikula 
99dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */
100dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = {
101dccf7369SJarkko Nikula 	{	/* LPSS_LPT_SSP */
102dccf7369SJarkko Nikula 		.offset = 0x800,
103dccf7369SJarkko Nikula 		.reg_general = 0x08,
104dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
105dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1068b136baaSJarkko Nikula 		.reg_capabilities = -1,
107dccf7369SJarkko Nikula 		.rx_threshold = 64,
108dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
109dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
110dccf7369SJarkko Nikula 	},
111dccf7369SJarkko Nikula 	{	/* LPSS_BYT_SSP */
112dccf7369SJarkko Nikula 		.offset = 0x400,
113dccf7369SJarkko Nikula 		.reg_general = 0x08,
114dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
115dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1168b136baaSJarkko Nikula 		.reg_capabilities = -1,
117dccf7369SJarkko Nikula 		.rx_threshold = 64,
118dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
119dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
120dccf7369SJarkko Nikula 	},
12130f3a6abSMika Westerberg 	{	/* LPSS_BSW_SSP */
12230f3a6abSMika Westerberg 		.offset = 0x400,
12330f3a6abSMika Westerberg 		.reg_general = 0x08,
12430f3a6abSMika Westerberg 		.reg_ssp = 0x0c,
12530f3a6abSMika Westerberg 		.reg_cs_ctrl = 0x18,
12630f3a6abSMika Westerberg 		.reg_capabilities = -1,
12730f3a6abSMika Westerberg 		.rx_threshold = 64,
12830f3a6abSMika Westerberg 		.tx_threshold_lo = 160,
12930f3a6abSMika Westerberg 		.tx_threshold_hi = 224,
13030f3a6abSMika Westerberg 		.cs_sel_shift = 2,
13130f3a6abSMika Westerberg 		.cs_sel_mask = 1 << 2,
13230f3a6abSMika Westerberg 		.cs_num = 2,
13330f3a6abSMika Westerberg 	},
13434cadd9cSJarkko Nikula 	{	/* LPSS_SPT_SSP */
13534cadd9cSJarkko Nikula 		.offset = 0x200,
13634cadd9cSJarkko Nikula 		.reg_general = -1,
13734cadd9cSJarkko Nikula 		.reg_ssp = 0x20,
13834cadd9cSJarkko Nikula 		.reg_cs_ctrl = 0x24,
13966ec246eSJarkko Nikula 		.reg_capabilities = -1,
14034cadd9cSJarkko Nikula 		.rx_threshold = 1,
14134cadd9cSJarkko Nikula 		.tx_threshold_lo = 32,
14234cadd9cSJarkko Nikula 		.tx_threshold_hi = 56,
14334cadd9cSJarkko Nikula 	},
144b7c08cf8SJarkko Nikula 	{	/* LPSS_BXT_SSP */
145b7c08cf8SJarkko Nikula 		.offset = 0x200,
146b7c08cf8SJarkko Nikula 		.reg_general = -1,
147b7c08cf8SJarkko Nikula 		.reg_ssp = 0x20,
148b7c08cf8SJarkko Nikula 		.reg_cs_ctrl = 0x24,
149b7c08cf8SJarkko Nikula 		.reg_capabilities = 0xfc,
150b7c08cf8SJarkko Nikula 		.rx_threshold = 1,
151b7c08cf8SJarkko Nikula 		.tx_threshold_lo = 16,
152b7c08cf8SJarkko Nikula 		.tx_threshold_hi = 48,
153c1e4a53cSMika Westerberg 		.cs_sel_shift = 8,
154c1e4a53cSMika Westerberg 		.cs_sel_mask = 3 << 8,
1556eefaee4SEvan Green 		.cs_clk_stays_gated = true,
156b7c08cf8SJarkko Nikula 	},
157fc0b2accSJarkko Nikula 	{	/* LPSS_CNL_SSP */
158fc0b2accSJarkko Nikula 		.offset = 0x200,
159fc0b2accSJarkko Nikula 		.reg_general = -1,
160fc0b2accSJarkko Nikula 		.reg_ssp = 0x20,
161fc0b2accSJarkko Nikula 		.reg_cs_ctrl = 0x24,
162fc0b2accSJarkko Nikula 		.reg_capabilities = 0xfc,
163fc0b2accSJarkko Nikula 		.rx_threshold = 1,
164fc0b2accSJarkko Nikula 		.tx_threshold_lo = 32,
165fc0b2accSJarkko Nikula 		.tx_threshold_hi = 56,
166fc0b2accSJarkko Nikula 		.cs_sel_shift = 8,
167fc0b2accSJarkko Nikula 		.cs_sel_mask = 3 << 8,
168683f65deSEvan Green 		.cs_clk_stays_gated = true,
169fc0b2accSJarkko Nikula 	},
170dccf7369SJarkko Nikula };
171dccf7369SJarkko Nikula 
172dccf7369SJarkko Nikula static inline const struct lpss_config
173dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data)
174dccf7369SJarkko Nikula {
175dccf7369SJarkko Nikula 	return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
176dccf7369SJarkko Nikula }
177dccf7369SJarkko Nikula 
178a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data)
179a0d2642eSMika Westerberg {
18003fbf488SJarkko Nikula 	switch (drv_data->ssp_type) {
18103fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
18203fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
18330f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
18434cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
185b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
186fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
18703fbf488SJarkko Nikula 		return true;
18803fbf488SJarkko Nikula 	default:
18903fbf488SJarkko Nikula 		return false;
19003fbf488SJarkko Nikula 	}
191a0d2642eSMika Westerberg }
192a0d2642eSMika Westerberg 
193e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
194e5262d05SWeike Chen {
195e5262d05SWeike Chen 	return drv_data->ssp_type == QUARK_X1000_SSP;
196e5262d05SWeike Chen }
197e5262d05SWeike Chen 
19841c98841SAndy Shevchenko static bool is_mmp2_ssp(const struct driver_data *drv_data)
19941c98841SAndy Shevchenko {
20041c98841SAndy Shevchenko 	return drv_data->ssp_type == MMP2_SSP;
20141c98841SAndy Shevchenko }
20241c98841SAndy Shevchenko 
2033fdb59cfSAndy Shevchenko static bool is_mrfld_ssp(const struct driver_data *drv_data)
2043fdb59cfSAndy Shevchenko {
2053fdb59cfSAndy Shevchenko 	return drv_data->ssp_type == MRFLD_SSP;
2063fdb59cfSAndy Shevchenko }
2073fdb59cfSAndy Shevchenko 
2081bed378cSAndy Shevchenko static void pxa2xx_spi_update(const struct driver_data *drv_data, u32 reg, u32 mask, u32 value)
2091bed378cSAndy Shevchenko {
2101bed378cSAndy Shevchenko 	if ((pxa2xx_spi_read(drv_data, reg) & mask) != value)
2111bed378cSAndy Shevchenko 		pxa2xx_spi_write(drv_data, reg, value & mask);
2121bed378cSAndy Shevchenko }
2131bed378cSAndy Shevchenko 
2144fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
2154fdb2424SWeike Chen {
2164fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
217e5262d05SWeike Chen 	case QUARK_X1000_SSP:
218e5262d05SWeike Chen 		return QUARK_X1000_SSCR1_CHANGE_MASK;
2197c7289a4SAndy Shevchenko 	case CE4100_SSP:
2207c7289a4SAndy Shevchenko 		return CE4100_SSCR1_CHANGE_MASK;
2214fdb2424SWeike Chen 	default:
2224fdb2424SWeike Chen 		return SSCR1_CHANGE_MASK;
2234fdb2424SWeike Chen 	}
2244fdb2424SWeike Chen }
2254fdb2424SWeike Chen 
2264fdb2424SWeike Chen static u32
2274fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
2284fdb2424SWeike Chen {
2294fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
230e5262d05SWeike Chen 	case QUARK_X1000_SSP:
231e5262d05SWeike Chen 		return RX_THRESH_QUARK_X1000_DFLT;
2327c7289a4SAndy Shevchenko 	case CE4100_SSP:
2337c7289a4SAndy Shevchenko 		return RX_THRESH_CE4100_DFLT;
2344fdb2424SWeike Chen 	default:
2354fdb2424SWeike Chen 		return RX_THRESH_DFLT;
2364fdb2424SWeike Chen 	}
2374fdb2424SWeike Chen }
2384fdb2424SWeike Chen 
2394fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
2404fdb2424SWeike Chen {
2414fdb2424SWeike Chen 	u32 mask;
2424fdb2424SWeike Chen 
2434fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
244e5262d05SWeike Chen 	case QUARK_X1000_SSP:
245e5262d05SWeike Chen 		mask = QUARK_X1000_SSSR_TFL_MASK;
246e5262d05SWeike Chen 		break;
2477c7289a4SAndy Shevchenko 	case CE4100_SSP:
2487c7289a4SAndy Shevchenko 		mask = CE4100_SSSR_TFL_MASK;
2497c7289a4SAndy Shevchenko 		break;
2504fdb2424SWeike Chen 	default:
2514fdb2424SWeike Chen 		mask = SSSR_TFL_MASK;
2524fdb2424SWeike Chen 		break;
2534fdb2424SWeike Chen 	}
2544fdb2424SWeike Chen 
2556d380132SAndy Shevchenko 	return read_SSSR_bits(drv_data, mask) == mask;
2564fdb2424SWeike Chen }
2574fdb2424SWeike Chen 
2584fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
2594fdb2424SWeike Chen 				     u32 *sccr1_reg)
2604fdb2424SWeike Chen {
2614fdb2424SWeike Chen 	u32 mask;
2624fdb2424SWeike Chen 
2634fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
264e5262d05SWeike Chen 	case QUARK_X1000_SSP:
265e5262d05SWeike Chen 		mask = QUARK_X1000_SSCR1_RFT;
266e5262d05SWeike Chen 		break;
2677c7289a4SAndy Shevchenko 	case CE4100_SSP:
2687c7289a4SAndy Shevchenko 		mask = CE4100_SSCR1_RFT;
2697c7289a4SAndy Shevchenko 		break;
2704fdb2424SWeike Chen 	default:
2714fdb2424SWeike Chen 		mask = SSCR1_RFT;
2724fdb2424SWeike Chen 		break;
2734fdb2424SWeike Chen 	}
2744fdb2424SWeike Chen 	*sccr1_reg &= ~mask;
2754fdb2424SWeike Chen }
2764fdb2424SWeike Chen 
2774fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
2784fdb2424SWeike Chen 				   u32 *sccr1_reg, u32 threshold)
2794fdb2424SWeike Chen {
2804fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
281e5262d05SWeike Chen 	case QUARK_X1000_SSP:
282e5262d05SWeike Chen 		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
283e5262d05SWeike Chen 		break;
2847c7289a4SAndy Shevchenko 	case CE4100_SSP:
2857c7289a4SAndy Shevchenko 		*sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
2867c7289a4SAndy Shevchenko 		break;
2874fdb2424SWeike Chen 	default:
2884fdb2424SWeike Chen 		*sccr1_reg |= SSCR1_RxTresh(threshold);
2894fdb2424SWeike Chen 		break;
2904fdb2424SWeike Chen 	}
2914fdb2424SWeike Chen }
2924fdb2424SWeike Chen 
2934fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
2944fdb2424SWeike Chen 				  u32 clk_div, u8 bits)
2954fdb2424SWeike Chen {
2964fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
297e5262d05SWeike Chen 	case QUARK_X1000_SSP:
298e5262d05SWeike Chen 		return clk_div
299e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_Motorola
3000c8ccd8bSAndy Shevchenko 			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits);
3014fdb2424SWeike Chen 	default:
3024fdb2424SWeike Chen 		return clk_div
3034fdb2424SWeike Chen 			| SSCR0_Motorola
3044fdb2424SWeike Chen 			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
3054fdb2424SWeike Chen 			| (bits > 16 ? SSCR0_EDSS : 0);
3064fdb2424SWeike Chen 	}
3074fdb2424SWeike Chen }
3084fdb2424SWeike Chen 
309a0d2642eSMika Westerberg /*
310a0d2642eSMika Westerberg  * Read and write LPSS SSP private registers. Caller must first check that
311a0d2642eSMika Westerberg  * is_lpss_ssp() returns true before these can be called.
312a0d2642eSMika Westerberg  */
313a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
314a0d2642eSMika Westerberg {
315a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
316a0d2642eSMika Westerberg 	return readl(drv_data->lpss_base + offset);
317a0d2642eSMika Westerberg }
318a0d2642eSMika Westerberg 
319a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data,
320a0d2642eSMika Westerberg 				  unsigned offset, u32 value)
321a0d2642eSMika Westerberg {
322a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
323a0d2642eSMika Westerberg 	writel(value, drv_data->lpss_base + offset);
324a0d2642eSMika Westerberg }
325a0d2642eSMika Westerberg 
326a0d2642eSMika Westerberg /*
327a0d2642eSMika Westerberg  * lpss_ssp_setup - perform LPSS SSP specific setup
328a0d2642eSMika Westerberg  * @drv_data: pointer to the driver private data
329a0d2642eSMika Westerberg  *
330a0d2642eSMika Westerberg  * Perform LPSS SSP specific setup. This function must be called first if
331a0d2642eSMika Westerberg  * one is going to use LPSS SSP private registers.
332a0d2642eSMika Westerberg  */
333a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data)
334a0d2642eSMika Westerberg {
335dccf7369SJarkko Nikula 	const struct lpss_config *config;
336dccf7369SJarkko Nikula 	u32 value;
337a0d2642eSMika Westerberg 
338dccf7369SJarkko Nikula 	config = lpss_get_config(drv_data);
3399e43c9a8SAndy Shevchenko 	drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset;
340a0d2642eSMika Westerberg 
341a0d2642eSMika Westerberg 	/* Enable software chip select control */
3420e897218SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
343624ea72eSJarkko Nikula 	value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
344624ea72eSJarkko Nikula 	value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
345dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
3460054e28dSMika Westerberg 
3470054e28dSMika Westerberg 	/* Enable multiblock DMA transfers */
34851eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma) {
349dccf7369SJarkko Nikula 		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
3501de70612SMika Westerberg 
35182ba2c2aSJarkko Nikula 		if (config->reg_general >= 0) {
35282ba2c2aSJarkko Nikula 			value = __lpss_ssp_read_priv(drv_data,
35382ba2c2aSJarkko Nikula 						     config->reg_general);
354624ea72eSJarkko Nikula 			value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
35582ba2c2aSJarkko Nikula 			__lpss_ssp_write_priv(drv_data,
35682ba2c2aSJarkko Nikula 					      config->reg_general, value);
35782ba2c2aSJarkko Nikula 		}
3581de70612SMika Westerberg 	}
359a0d2642eSMika Westerberg }
360a0d2642eSMika Westerberg 
361d5898e19SJarkko Nikula static void lpss_ssp_select_cs(struct spi_device *spi,
362c1e4a53cSMika Westerberg 			       const struct lpss_config *config)
363a0d2642eSMika Westerberg {
364d5898e19SJarkko Nikula 	struct driver_data *drv_data =
365d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
366d0283eb2SJarkko Nikula 	u32 value, cs;
367a0d2642eSMika Westerberg 
368c1e4a53cSMika Westerberg 	if (!config->cs_sel_mask)
369c1e4a53cSMika Westerberg 		return;
370dccf7369SJarkko Nikula 
371dccf7369SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
372c1e4a53cSMika Westerberg 
373d5898e19SJarkko Nikula 	cs = spi->chip_select;
374c1e4a53cSMika Westerberg 	cs <<= config->cs_sel_shift;
375c1e4a53cSMika Westerberg 	if (cs != (value & config->cs_sel_mask)) {
376d0283eb2SJarkko Nikula 		/*
377c1e4a53cSMika Westerberg 		 * When switching another chip select output active the
378c1e4a53cSMika Westerberg 		 * output must be selected first and wait 2 ssp_clk cycles
379c1e4a53cSMika Westerberg 		 * before changing state to active. Otherwise a short
380c1e4a53cSMika Westerberg 		 * glitch will occur on the previous chip select since
381c1e4a53cSMika Westerberg 		 * output select is latched but state control is not.
382d0283eb2SJarkko Nikula 		 */
383c1e4a53cSMika Westerberg 		value &= ~config->cs_sel_mask;
384d0283eb2SJarkko Nikula 		value |= cs;
385d0283eb2SJarkko Nikula 		__lpss_ssp_write_priv(drv_data,
386d0283eb2SJarkko Nikula 				      config->reg_cs_ctrl, value);
387d0283eb2SJarkko Nikula 		ndelay(1000000000 /
38851eea52dSLubomir Rintel 		       (drv_data->controller->max_speed_hz / 2));
389d0283eb2SJarkko Nikula 	}
390d0283eb2SJarkko Nikula }
391c1e4a53cSMika Westerberg 
392d5898e19SJarkko Nikula static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
393c1e4a53cSMika Westerberg {
394d5898e19SJarkko Nikula 	struct driver_data *drv_data =
395d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
396c1e4a53cSMika Westerberg 	const struct lpss_config *config;
397c1e4a53cSMika Westerberg 	u32 value;
398c1e4a53cSMika Westerberg 
399c1e4a53cSMika Westerberg 	config = lpss_get_config(drv_data);
400c1e4a53cSMika Westerberg 
401c1e4a53cSMika Westerberg 	if (enable)
402d5898e19SJarkko Nikula 		lpss_ssp_select_cs(spi, config);
403c1e4a53cSMika Westerberg 
404c1e4a53cSMika Westerberg 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
405c1e4a53cSMika Westerberg 	if (enable)
406c1e4a53cSMika Westerberg 		value &= ~LPSS_CS_CONTROL_CS_HIGH;
407c1e4a53cSMika Westerberg 	else
408c1e4a53cSMika Westerberg 		value |= LPSS_CS_CONTROL_CS_HIGH;
409dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
410683f65deSEvan Green 	if (config->cs_clk_stays_gated) {
411683f65deSEvan Green 		u32 clkgate;
412683f65deSEvan Green 
413683f65deSEvan Green 		/*
414683f65deSEvan Green 		 * Changing CS alone when dynamic clock gating is on won't
415683f65deSEvan Green 		 * actually flip CS at that time. This ruins SPI transfers
416683f65deSEvan Green 		 * that specify delays, or have no data. Toggle the clock mode
417683f65deSEvan Green 		 * to force on briefly to poke the CS pin to move.
418683f65deSEvan Green 		 */
419683f65deSEvan Green 		clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
420683f65deSEvan Green 		value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
421683f65deSEvan Green 			LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
422683f65deSEvan Green 
423683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
424683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
425683f65deSEvan Green 	}
426a0d2642eSMika Westerberg }
427a0d2642eSMika Westerberg 
428d5898e19SJarkko Nikula static void cs_assert(struct spi_device *spi)
429ca632f55SGrant Likely {
430d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
431d5898e19SJarkko Nikula 	struct driver_data *drv_data =
432d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
433ca632f55SGrant Likely 
434ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP) {
435ccd60b20SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSSR, spi->chip_select);
436ca632f55SGrant Likely 		return;
437ca632f55SGrant Likely 	}
438ca632f55SGrant Likely 
439ca632f55SGrant Likely 	if (chip->cs_control) {
440ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_ASSERT);
441ca632f55SGrant Likely 		return;
442ca632f55SGrant Likely 	}
443ca632f55SGrant Likely 
4447566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
445d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, true);
446ca632f55SGrant Likely }
447ca632f55SGrant Likely 
448d5898e19SJarkko Nikula static void cs_deassert(struct spi_device *spi)
449ca632f55SGrant Likely {
450d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
451d5898e19SJarkko Nikula 	struct driver_data *drv_data =
452d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
453104e51afSJarkko Nikula 	unsigned long timeout;
454ca632f55SGrant Likely 
455ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
456ca632f55SGrant Likely 		return;
457ca632f55SGrant Likely 
458104e51afSJarkko Nikula 	/* Wait until SSP becomes idle before deasserting the CS */
459104e51afSJarkko Nikula 	timeout = jiffies + msecs_to_jiffies(10);
460104e51afSJarkko Nikula 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
461104e51afSJarkko Nikula 	       !time_after(jiffies, timeout))
462104e51afSJarkko Nikula 		cpu_relax();
463104e51afSJarkko Nikula 
464ca632f55SGrant Likely 	if (chip->cs_control) {
465ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_DEASSERT);
466ca632f55SGrant Likely 		return;
467ca632f55SGrant Likely 	}
468ca632f55SGrant Likely 
4697566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
470d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, false);
471d5898e19SJarkko Nikula }
472d5898e19SJarkko Nikula 
473d5898e19SJarkko Nikula static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
474d5898e19SJarkko Nikula {
475d5898e19SJarkko Nikula 	if (level)
476d5898e19SJarkko Nikula 		cs_deassert(spi);
477d5898e19SJarkko Nikula 	else
478d5898e19SJarkko Nikula 		cs_assert(spi);
479ca632f55SGrant Likely }
480ca632f55SGrant Likely 
481cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
482ca632f55SGrant Likely {
483ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
484ca632f55SGrant Likely 
485ca632f55SGrant Likely 	do {
4866d380132SAndy Shevchenko 		while (read_SSSR_bits(drv_data, SSSR_RNE))
487c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
488c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
489ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
490ca632f55SGrant Likely 
491ca632f55SGrant Likely 	return limit;
492ca632f55SGrant Likely }
493ca632f55SGrant Likely 
49429d7e05cSLubomir Rintel static void pxa2xx_spi_off(struct driver_data *drv_data)
49529d7e05cSLubomir Rintel {
49641c98841SAndy Shevchenko 	/* On MMP, disabling SSE seems to corrupt the Rx FIFO */
49741c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data))
49829d7e05cSLubomir Rintel 		return;
49929d7e05cSLubomir Rintel 
5000c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(drv_data->ssp);
50129d7e05cSLubomir Rintel }
50229d7e05cSLubomir Rintel 
503ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
504ca632f55SGrant Likely {
505ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
506ca632f55SGrant Likely 
5074fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
508ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
509ca632f55SGrant Likely 		return 0;
510ca632f55SGrant Likely 
511c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
512ca632f55SGrant Likely 	drv_data->tx += n_bytes;
513ca632f55SGrant Likely 
514ca632f55SGrant Likely 	return 1;
515ca632f55SGrant Likely }
516ca632f55SGrant Likely 
517ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
518ca632f55SGrant Likely {
519ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
520ca632f55SGrant Likely 
5216d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && 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 {
5436d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
544c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
545ca632f55SGrant Likely 		++drv_data->rx;
546ca632f55SGrant Likely 	}
547ca632f55SGrant Likely 
548ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
549ca632f55SGrant Likely }
550ca632f55SGrant Likely 
551ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
552ca632f55SGrant Likely {
5534fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
554ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
555ca632f55SGrant Likely 		return 0;
556ca632f55SGrant Likely 
557c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
558ca632f55SGrant Likely 	drv_data->tx += 2;
559ca632f55SGrant Likely 
560ca632f55SGrant Likely 	return 1;
561ca632f55SGrant Likely }
562ca632f55SGrant Likely 
563ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
564ca632f55SGrant Likely {
5656d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
566c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
567ca632f55SGrant Likely 		drv_data->rx += 2;
568ca632f55SGrant Likely 	}
569ca632f55SGrant Likely 
570ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
571ca632f55SGrant Likely }
572ca632f55SGrant Likely 
573ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
574ca632f55SGrant Likely {
5754fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
576ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
577ca632f55SGrant Likely 		return 0;
578ca632f55SGrant Likely 
579c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
580ca632f55SGrant Likely 	drv_data->tx += 4;
581ca632f55SGrant Likely 
582ca632f55SGrant Likely 	return 1;
583ca632f55SGrant Likely }
584ca632f55SGrant Likely 
585ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
586ca632f55SGrant Likely {
5876d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
588c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
589ca632f55SGrant Likely 		drv_data->rx += 4;
590ca632f55SGrant Likely 	}
591ca632f55SGrant Likely 
592ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
593ca632f55SGrant Likely }
594ca632f55SGrant Likely 
595ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
596ca632f55SGrant Likely {
59796579a4eSJarkko Nikula 	struct chip_data *chip =
59851eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
599ca632f55SGrant Likely 	u32 sccr1_reg;
600ca632f55SGrant Likely 
601c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
602152bc19eSAndy Shevchenko 	switch (drv_data->ssp_type) {
603152bc19eSAndy Shevchenko 	case QUARK_X1000_SSP:
604152bc19eSAndy Shevchenko 		sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
605152bc19eSAndy Shevchenko 		break;
6067c7289a4SAndy Shevchenko 	case CE4100_SSP:
6077c7289a4SAndy Shevchenko 		sccr1_reg &= ~CE4100_SSCR1_RFT;
6087c7289a4SAndy Shevchenko 		break;
609152bc19eSAndy Shevchenko 	default:
610ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_RFT;
611152bc19eSAndy Shevchenko 		break;
612152bc19eSAndy Shevchenko 	}
613ca632f55SGrant Likely 	sccr1_reg |= chip->threshold;
614c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
615ca632f55SGrant Likely }
616ca632f55SGrant Likely 
617ab77fe89SAndy Shevchenko static void int_stop_and_reset(struct driver_data *drv_data)
618ca632f55SGrant Likely {
619ab77fe89SAndy Shevchenko 	/* Clear and disable interrupts */
620ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
621ca632f55SGrant Likely 	reset_sccr1(drv_data);
622ab77fe89SAndy Shevchenko 	if (pxa25x_ssp_comp(drv_data))
623ab77fe89SAndy Shevchenko 		return;
624ab77fe89SAndy Shevchenko 
625c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSTO, 0);
626ab77fe89SAndy Shevchenko }
627ab77fe89SAndy Shevchenko 
6284761d2e7SAndy Shevchenko static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
629ab77fe89SAndy Shevchenko {
630ab77fe89SAndy Shevchenko 	int_stop_and_reset(drv_data);
631cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
63229d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
633ca632f55SGrant Likely 
634c3dce24cSAndy Shevchenko 	dev_err(drv_data->ssp->dev, "%s\n", msg);
635ca632f55SGrant Likely 
6364761d2e7SAndy Shevchenko 	drv_data->controller->cur_msg->status = err;
63751eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
638ca632f55SGrant Likely }
639ca632f55SGrant Likely 
640ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
641ca632f55SGrant Likely {
642ab77fe89SAndy Shevchenko 	int_stop_and_reset(drv_data);
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 {
6496d380132SAndy Shevchenko 	u32 irq_status;
650ca632f55SGrant Likely 
6516d380132SAndy Shevchenko 	irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr);
6526d380132SAndy Shevchenko 	if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE))
6536d380132SAndy Shevchenko 		irq_status &= ~SSSR_TFS;
654ca632f55SGrant Likely 
655ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
6568083d6b8SAndy Shevchenko 		int_error_stop(drv_data, "interrupt_transfer: FIFO overrun", -EIO);
657ca632f55SGrant Likely 		return IRQ_HANDLED;
658ca632f55SGrant Likely 	}
659ca632f55SGrant Likely 
660ec93cb6fSLubomir Rintel 	if (irq_status & SSSR_TUR) {
6618083d6b8SAndy Shevchenko 		int_error_stop(drv_data, "interrupt_transfer: FIFO underrun", -EIO);
662ec93cb6fSLubomir Rintel 		return IRQ_HANDLED;
663ec93cb6fSLubomir Rintel 	}
664ec93cb6fSLubomir Rintel 
665ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
666c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
667ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
668ca632f55SGrant Likely 			int_transfer_complete(drv_data);
669ca632f55SGrant Likely 			return IRQ_HANDLED;
670ca632f55SGrant Likely 		}
671ca632f55SGrant Likely 	}
672ca632f55SGrant Likely 
6738083d6b8SAndy Shevchenko 	/* Drain Rx FIFO, Fill Tx FIFO and prevent overruns */
674ca632f55SGrant Likely 	do {
675ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
676ca632f55SGrant Likely 			int_transfer_complete(drv_data);
677ca632f55SGrant Likely 			return IRQ_HANDLED;
678ca632f55SGrant Likely 		}
679ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
680ca632f55SGrant Likely 
681ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
682ca632f55SGrant Likely 		int_transfer_complete(drv_data);
683ca632f55SGrant Likely 		return IRQ_HANDLED;
684ca632f55SGrant Likely 	}
685ca632f55SGrant Likely 
686ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
687ca632f55SGrant Likely 		u32 bytes_left;
688ca632f55SGrant Likely 		u32 sccr1_reg;
689ca632f55SGrant Likely 
690c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
691ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
692ca632f55SGrant Likely 
693ca632f55SGrant Likely 		/*
6948083d6b8SAndy Shevchenko 		 * PXA25x_SSP has no timeout, set up Rx threshold for
6958083d6b8SAndy Shevchenko 		 * the remaining Rx bytes.
696ca632f55SGrant Likely 		 */
697ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
6984fdb2424SWeike Chen 			u32 rx_thre;
699ca632f55SGrant Likely 
7004fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
701ca632f55SGrant Likely 
702ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
703ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
704ca632f55SGrant Likely 			case 4:
7052c183376SGustavo A. R. Silva 				bytes_left >>= 2;
7062c183376SGustavo A. R. Silva 				break;
707ca632f55SGrant Likely 			case 2:
708ca632f55SGrant Likely 				bytes_left >>= 1;
7092c183376SGustavo A. R. Silva 				break;
710ca632f55SGrant Likely 			}
711ca632f55SGrant Likely 
7124fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
7134fdb2424SWeike Chen 			if (rx_thre > bytes_left)
7144fdb2424SWeike Chen 				rx_thre = bytes_left;
715ca632f55SGrant Likely 
7164fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
717ca632f55SGrant Likely 		}
718c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
719ca632f55SGrant Likely 	}
720ca632f55SGrant Likely 
721ca632f55SGrant Likely 	/* We did something */
722ca632f55SGrant Likely 	return IRQ_HANDLED;
723ca632f55SGrant Likely }
724ca632f55SGrant Likely 
725b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data)
726b0312482SJan Kiszka {
72729d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
72842c80cd4SAndy Shevchenko 	clear_SSCR1_bits(drv_data, 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 
733c3dce24cSAndy Shevchenko 	dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
734b0312482SJan Kiszka }
735b0312482SJan Kiszka 
736ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
737ca632f55SGrant Likely {
738ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
7397d94a505SMika Westerberg 	u32 sccr1_reg;
740ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
741ca632f55SGrant Likely 	u32 status;
742ca632f55SGrant Likely 
7437d94a505SMika Westerberg 	/*
7447d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
7457d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
7467d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
7477d94a505SMika Westerberg 	 * interrupt is enabled).
7487d94a505SMika Westerberg 	 */
749c3dce24cSAndy Shevchenko 	if (pm_runtime_suspended(drv_data->ssp->dev))
7507d94a505SMika Westerberg 		return IRQ_NONE;
7517d94a505SMika Westerberg 
752269e4a41SMika Westerberg 	/*
753269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
754269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
755269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
756269e4a41SMika Westerberg 	 * powered off.
757269e4a41SMika Westerberg 	 */
758c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
759269e4a41SMika Westerberg 	if (status == ~0)
760269e4a41SMika Westerberg 		return IRQ_NONE;
761269e4a41SMika Westerberg 
762c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
763ca632f55SGrant Likely 
764ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
765ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
766ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
767ca632f55SGrant Likely 
76802bc933eSTan, Jui Nee 	/* Ignore RX timeout interrupt if it is disabled */
76902bc933eSTan, Jui Nee 	if (!(sccr1_reg & SSCR1_TINTE))
77002bc933eSTan, Jui Nee 		mask &= ~SSSR_TINT;
77102bc933eSTan, Jui Nee 
772ca632f55SGrant Likely 	if (!(status & mask))
773ca632f55SGrant Likely 		return IRQ_NONE;
774ca632f55SGrant Likely 
775e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
776e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
777e51e9b93SJan Kiszka 
77851eea52dSLubomir Rintel 	if (!drv_data->controller->cur_msg) {
779b0312482SJan Kiszka 		handle_bad_msg(drv_data);
780ca632f55SGrant Likely 		/* Never fail */
781ca632f55SGrant Likely 		return IRQ_HANDLED;
782ca632f55SGrant Likely 	}
783ca632f55SGrant Likely 
784ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
785ca632f55SGrant Likely }
786ca632f55SGrant Likely 
787e5262d05SWeike Chen /*
7889df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
7899df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
7909df461ecSAndy Shevchenko  *
7919df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
7929df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
7939df461ecSAndy Shevchenko  *
7949df461ecSAndy Shevchenko  * Fsys = 200MHz
7959df461ecSAndy Shevchenko  *
7969df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
7979df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
7989df461ecSAndy Shevchenko  *
7999df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
8009df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
8019df461ecSAndy Shevchenko  *
8029df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
8039df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
8049df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
8059df461ecSAndy Shevchenko  *       k = [1, 256]
8069df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
8079df461ecSAndy Shevchenko  *
8089df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
8099df461ecSAndy Shevchenko  * are:
8109df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
8119df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
8129df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
8139df461ecSAndy Shevchenko  *
8149df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
8159df461ecSAndy Shevchenko  *
8169df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
8179df461ecSAndy Shevchenko  * to the asked baud rate.
818e5262d05SWeike Chen  */
8199df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
820e5262d05SWeike Chen {
8219df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
8229df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
8239df461ecSAndy Shevchenko 						   see (2) */
8249df461ecSAndy Shevchenko 						/* case 3 */
8259df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
8269df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
8279df461ecSAndy Shevchenko 	unsigned long scale;
8289df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
8299df461ecSAndy Shevchenko 	long r, r1, r2;
8309df461ecSAndy Shevchenko 	u32 mul;
831e5262d05SWeike Chen 
8329df461ecSAndy Shevchenko 	/* Case 1 */
8339df461ecSAndy Shevchenko 
8349df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
8359df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
8369df461ecSAndy Shevchenko 
8379df461ecSAndy Shevchenko 	/* Calculate initial quot */
8383ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
8399df461ecSAndy Shevchenko 
8409df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
8419df461ecSAndy Shevchenko 	if (q1 > 256) {
8429df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
8439df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
8449df461ecSAndy Shevchenko 		if (scale > 9) {
8459df461ecSAndy Shevchenko 			q1 >>= scale - 9;
8469df461ecSAndy Shevchenko 			mul >>= scale - 9;
8479df461ecSAndy Shevchenko 		}
8489df461ecSAndy Shevchenko 
8499df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
8509df461ecSAndy Shevchenko 		q1 += q1 & 1;
8519df461ecSAndy Shevchenko 	}
8529df461ecSAndy Shevchenko 
8539df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
8549df461ecSAndy Shevchenko 	scale = __ffs(q1);
8559df461ecSAndy Shevchenko 	q1 >>= scale;
8569df461ecSAndy Shevchenko 	mul >>= scale;
8579df461ecSAndy Shevchenko 
8589df461ecSAndy Shevchenko 	/* Get the remainder */
8599df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
8609df461ecSAndy Shevchenko 
8619df461ecSAndy Shevchenko 	/* Case 2 */
8629df461ecSAndy Shevchenko 
8633ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
8649df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
8659df461ecSAndy Shevchenko 
8669df461ecSAndy Shevchenko 	/*
8679df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
8689df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
8699df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
8709df461ecSAndy Shevchenko 	 */
8719df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
8729df461ecSAndy Shevchenko 		/* case 1 is better */
8739df461ecSAndy Shevchenko 		r = r1;
8749df461ecSAndy Shevchenko 		q = q1;
8759df461ecSAndy Shevchenko 	} else {
8769df461ecSAndy Shevchenko 		/* case 2 is better */
8779df461ecSAndy Shevchenko 		r = r2;
8789df461ecSAndy Shevchenko 		q = q2;
8799df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
8809df461ecSAndy Shevchenko 	}
8819df461ecSAndy Shevchenko 
8823ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
8839df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
8849df461ecSAndy Shevchenko 		u64 fssp;
8859df461ecSAndy Shevchenko 		u32 m;
8869df461ecSAndy Shevchenko 
8879df461ecSAndy Shevchenko 		/* Calculate initial quot */
8883ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
8899df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
8909df461ecSAndy Shevchenko 
8919df461ecSAndy Shevchenko 		/* Get the remainder */
8929df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
8939df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
8949df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
8959df461ecSAndy Shevchenko 
8969df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
8979df461ecSAndy Shevchenko 		if (r1 < r) {
8989df461ecSAndy Shevchenko 			/* case 3 is better */
8999df461ecSAndy Shevchenko 			q = 1;
9009df461ecSAndy Shevchenko 			mul = m;
901e5262d05SWeike Chen 		}
902e5262d05SWeike Chen 	}
903e5262d05SWeike Chen 
9049df461ecSAndy Shevchenko 	*dds = mul;
9059df461ecSAndy Shevchenko 	return q - 1;
906e5262d05SWeike Chen }
907e5262d05SWeike Chen 
9083343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
909ca632f55SGrant Likely {
91051eea52dSLubomir Rintel 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
9113343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
9123343b7a6SMika Westerberg 
9133343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
914ca632f55SGrant Likely 
91529f21337SFlavio Suligoi 	/*
91629f21337SFlavio Suligoi 	 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
9178083d6b8SAndy Shevchenko 	 * that the SSP transmission rate can be greater than the device rate.
91829f21337SFlavio Suligoi 	 */
919ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
92029f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
921ca632f55SGrant Likely 	else
92229f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
923ca632f55SGrant Likely }
924ca632f55SGrant Likely 
925e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
926d2c2f6a4SAndy Shevchenko 					   int rate)
927e5262d05SWeike Chen {
92896579a4eSJarkko Nikula 	struct chip_data *chip =
92951eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
930025ffe88SAndy Shevchenko 	unsigned int clk_div;
931e5262d05SWeike Chen 
932e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
933e5262d05SWeike Chen 	case QUARK_X1000_SSP:
9349df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
935eecacf73SDan Carpenter 		break;
936e5262d05SWeike Chen 	default:
937025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
938eecacf73SDan Carpenter 		break;
939e5262d05SWeike Chen 	}
940025ffe88SAndy Shevchenko 	return clk_div << 8;
941e5262d05SWeike Chen }
942e5262d05SWeike Chen 
94351eea52dSLubomir Rintel static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
944b6ced294SJarkko Nikula 			       struct spi_device *spi,
945b6ced294SJarkko Nikula 			       struct spi_transfer *xfer)
946b6ced294SJarkko Nikula {
947b6ced294SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
948b6ced294SJarkko Nikula 
949b6ced294SJarkko Nikula 	return chip->enable_dma &&
950b6ced294SJarkko Nikula 	       xfer->len <= MAX_DMA_LEN &&
951b6ced294SJarkko Nikula 	       xfer->len >= chip->dma_burst_size;
952b6ced294SJarkko Nikula }
953b6ced294SJarkko Nikula 
95451eea52dSLubomir Rintel static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
955d5898e19SJarkko Nikula 				   struct spi_device *spi,
956d5898e19SJarkko Nikula 				   struct spi_transfer *transfer)
957ca632f55SGrant Likely {
95851eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
95951eea52dSLubomir Rintel 	struct spi_message *message = controller->cur_msg;
96020f4c379SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
96196579a4eSJarkko Nikula 	u32 dma_thresh = chip->dma_threshold;
96296579a4eSJarkko Nikula 	u32 dma_burst = chip->dma_burst_size;
96396579a4eSJarkko Nikula 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
964bffc967eSJarkko Nikula 	u32 clk_div;
965bffc967eSJarkko Nikula 	u8 bits;
966bffc967eSJarkko Nikula 	u32 speed;
967ca632f55SGrant Likely 	u32 cr0;
968ca632f55SGrant Likely 	u32 cr1;
9697d1f1bf6SAndy Shevchenko 	int err;
970b6ced294SJarkko Nikula 	int dma_mapped;
971ca632f55SGrant Likely 
972cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
973b6ced294SJarkko Nikula 	if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
974ca632f55SGrant Likely 
9758083d6b8SAndy Shevchenko 		/* Reject already-mapped transfers; PIO won't always work */
976ca632f55SGrant Likely 		if (message->is_dma_mapped
977ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
978748fbadfSJarkko Nikula 			dev_err(&spi->dev,
9798ae55af3SJarkko Nikula 				"Mapped transfer length of %u is greater than %d\n",
980ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
981d5898e19SJarkko Nikula 			return -EINVAL;
982ca632f55SGrant Likely 		}
983ca632f55SGrant Likely 
9848083d6b8SAndy Shevchenko 		/* Warn ... we force this to PIO mode */
98520f4c379SJarkko Nikula 		dev_warn_ratelimited(&spi->dev,
986684a3ac7SAndy Shevchenko 				     "DMA disabled for transfer length %u greater than %d\n",
987684a3ac7SAndy Shevchenko 				     transfer->len, MAX_DMA_LEN);
988ca632f55SGrant Likely 	}
989ca632f55SGrant Likely 
990ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
991cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
992748fbadfSJarkko Nikula 		dev_err(&spi->dev, "Flush failed\n");
993d5898e19SJarkko Nikula 		return -EIO;
994ca632f55SGrant Likely 	}
995ca632f55SGrant Likely 	drv_data->n_bytes = chip->n_bytes;
996ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
997ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
998ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
999ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
1000ca632f55SGrant Likely 	drv_data->write = drv_data->tx ? chip->write : null_writer;
1001ca632f55SGrant Likely 	drv_data->read = drv_data->rx ? chip->read : null_reader;
1002ca632f55SGrant Likely 
1003ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
1004ca632f55SGrant Likely 	bits = transfer->bits_per_word;
1005ca632f55SGrant Likely 	speed = transfer->speed_hz;
1006ca632f55SGrant Likely 
1007d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
1008ca632f55SGrant Likely 
1009ca632f55SGrant Likely 	if (bits <= 8) {
1010ca632f55SGrant Likely 		drv_data->n_bytes = 1;
1011ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1012ca632f55SGrant Likely 					u8_reader : null_reader;
1013ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1014ca632f55SGrant Likely 					u8_writer : null_writer;
1015ca632f55SGrant Likely 	} else if (bits <= 16) {
1016ca632f55SGrant Likely 		drv_data->n_bytes = 2;
1017ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1018ca632f55SGrant Likely 					u16_reader : null_reader;
1019ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1020ca632f55SGrant Likely 					u16_writer : null_writer;
1021ca632f55SGrant Likely 	} else if (bits <= 32) {
1022ca632f55SGrant Likely 		drv_data->n_bytes = 4;
1023ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1024ca632f55SGrant Likely 					u32_reader : null_reader;
1025ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1026ca632f55SGrant Likely 					u32_writer : null_writer;
1027ca632f55SGrant Likely 	}
1028196b0e2cSJarkko Nikula 	/*
10298083d6b8SAndy Shevchenko 	 * If bits per word is changed in DMA mode, then must check
10308083d6b8SAndy Shevchenko 	 * the thresholds and burst also.
1031196b0e2cSJarkko Nikula 	 */
1032ca632f55SGrant Likely 	if (chip->enable_dma) {
1033cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
103420f4c379SJarkko Nikula 						spi,
1035ca632f55SGrant Likely 						bits, &dma_burst,
1036ca632f55SGrant Likely 						&dma_thresh))
103720f4c379SJarkko Nikula 			dev_warn_ratelimited(&spi->dev,
10388ae55af3SJarkko Nikula 					     "DMA burst size reduced to match bits_per_word\n");
1039ca632f55SGrant Likely 	}
1040ca632f55SGrant Likely 
104151eea52dSLubomir Rintel 	dma_mapped = controller->can_dma &&
104220f4c379SJarkko Nikula 		     controller->can_dma(controller, spi, transfer) &&
104351eea52dSLubomir Rintel 		     controller->cur_msg_mapped;
1044b6ced294SJarkko Nikula 	if (dma_mapped) {
1045ca632f55SGrant Likely 
1046ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
1047cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1048ca632f55SGrant Likely 
1049d5898e19SJarkko Nikula 		err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1050d5898e19SJarkko Nikula 		if (err)
1051d5898e19SJarkko Nikula 			return err;
1052ca632f55SGrant Likely 
1053ca632f55SGrant Likely 		/* Clear status and start DMA engine */
1054ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1055c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1056cd7bed00SMika Westerberg 
1057cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
1058ca632f55SGrant Likely 	} else {
1059ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
1060ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
1061ca632f55SGrant Likely 
1062ca632f55SGrant Likely 		/* Clear status  */
1063ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1064ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1065ca632f55SGrant Likely 	}
1066ca632f55SGrant Likely 
1067ee03672dSJarkko Nikula 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1068ee03672dSJarkko Nikula 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1069ee03672dSJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
107020f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
107151eea52dSLubomir Rintel 			controller->max_speed_hz
1072ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1073b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1074ee03672dSJarkko Nikula 	else
107520f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
107651eea52dSLubomir Rintel 			controller->max_speed_hz / 2
1077ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1078b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1079ee03672dSJarkko Nikula 
1080a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
10811bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold);
10821bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold);
1083a0d2642eSMika Westerberg 	}
1084a0d2642eSMika Westerberg 
10853fdb59cfSAndy Shevchenko 	if (is_mrfld_ssp(drv_data)) {
1086*70252440SAndy Shevchenko 		u32 mask = SFIFOTT_RFT | SFIFOTT_TFT;
10873fdb59cfSAndy Shevchenko 		u32 thresh = 0;
10883fdb59cfSAndy Shevchenko 
10893fdb59cfSAndy Shevchenko 		thresh |= SFIFOTT_RxThresh(chip->lpss_rx_threshold);
10903fdb59cfSAndy Shevchenko 		thresh |= SFIFOTT_TxThresh(chip->lpss_tx_threshold);
10913fdb59cfSAndy Shevchenko 
1092*70252440SAndy Shevchenko 		pxa2xx_spi_update(drv_data, SFIFOTT, mask, thresh);
10933fdb59cfSAndy Shevchenko 	}
10943fdb59cfSAndy Shevchenko 
10951bed378cSAndy Shevchenko 	if (is_quark_x1000_ssp(drv_data))
10961bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate);
1097e5262d05SWeike Chen 
10980c8ccd8bSAndy Shevchenko 	/* Stop the SSP */
10990c8ccd8bSAndy Shevchenko 	if (!is_mmp2_ssp(drv_data))
11000c8ccd8bSAndy Shevchenko 		pxa_ssp_disable(drv_data->ssp);
11010c8ccd8bSAndy Shevchenko 
11020c8ccd8bSAndy Shevchenko 	if (!pxa25x_ssp_comp(drv_data))
11030c8ccd8bSAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
11040c8ccd8bSAndy Shevchenko 
11058083d6b8SAndy Shevchenko 	/* First set CR1 without interrupt and service enables */
11061bed378cSAndy Shevchenko 	pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1);
11071bed378cSAndy Shevchenko 
11088083d6b8SAndy Shevchenko 	/* See if we need to reload the configuration registers */
11091bed378cSAndy Shevchenko 	pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0);
1110ca632f55SGrant Likely 
11110c8ccd8bSAndy Shevchenko 	/* Restart the SSP */
11120c8ccd8bSAndy Shevchenko 	pxa_ssp_enable(drv_data->ssp);
11130c8ccd8bSAndy Shevchenko 
111441c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data)) {
11156d380132SAndy Shevchenko 		u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8;
111682391856SLubomir Rintel 
111782391856SLubomir Rintel 		if (tx_level) {
11188083d6b8SAndy Shevchenko 			/* On MMP2, flipping SSE doesn't to empty Tx FIFO. */
1119684a3ac7SAndy Shevchenko 			dev_warn(&spi->dev, "%u bytes of garbage in Tx FIFO!\n", tx_level);
112082391856SLubomir Rintel 			if (tx_level > transfer->len)
112182391856SLubomir Rintel 				tx_level = transfer->len;
112282391856SLubomir Rintel 			drv_data->tx += tx_level;
112382391856SLubomir Rintel 		}
112482391856SLubomir Rintel 	}
112582391856SLubomir Rintel 
112651eea52dSLubomir Rintel 	if (spi_controller_is_slave(controller)) {
1127ec93cb6fSLubomir Rintel 		while (drv_data->write(drv_data))
1128ec93cb6fSLubomir Rintel 			;
112977d33897SLubomir Rintel 		if (drv_data->gpiod_ready) {
113077d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 1);
113177d33897SLubomir Rintel 			udelay(1);
113277d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 0);
113377d33897SLubomir Rintel 		}
1134ec93cb6fSLubomir Rintel 	}
1135ec93cb6fSLubomir Rintel 
1136d5898e19SJarkko Nikula 	/*
1137d5898e19SJarkko Nikula 	 * Release the data by enabling service requests and interrupts,
11388083d6b8SAndy Shevchenko 	 * without changing any mode bits.
1139d5898e19SJarkko Nikula 	 */
1140c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1141d5898e19SJarkko Nikula 
1142d5898e19SJarkko Nikula 	return 1;
1143ca632f55SGrant Likely }
1144ca632f55SGrant Likely 
114551eea52dSLubomir Rintel static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
1146ec93cb6fSLubomir Rintel {
114751eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1148ec93cb6fSLubomir Rintel 
11494761d2e7SAndy Shevchenko 	int_error_stop(drv_data, "transfer aborted", -EINTR);
1150ec93cb6fSLubomir Rintel 
1151ec93cb6fSLubomir Rintel 	return 0;
1152ec93cb6fSLubomir Rintel }
1153ec93cb6fSLubomir Rintel 
115451eea52dSLubomir Rintel static void pxa2xx_spi_handle_err(struct spi_controller *controller,
11557f86bde9SMika Westerberg 				 struct spi_message *msg)
1156ca632f55SGrant Likely {
115751eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1158ca632f55SGrant Likely 
1159d5898e19SJarkko Nikula 	/* Disable the SSP */
116029d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
1161d5898e19SJarkko Nikula 	/* Clear and disable interrupts and service requests */
1162d5898e19SJarkko Nikula 	write_SSSR_CS(drv_data, drv_data->clear_sr);
116342c80cd4SAndy Shevchenko 	clear_SSCR1_bits(drv_data, drv_data->int_cr1 | drv_data->dma_cr1);
1164d5898e19SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
1165d5898e19SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1166ca632f55SGrant Likely 
1167d5898e19SJarkko Nikula 	/*
1168d5898e19SJarkko Nikula 	 * Stop the DMA if running. Note DMA callback handler may have unset
1169d5898e19SJarkko Nikula 	 * the dma_running already, which is fine as stopping is not needed
1170d5898e19SJarkko Nikula 	 * then but we shouldn't rely this flag for anything else than
1171d5898e19SJarkko Nikula 	 * stopping. For instance to differentiate between PIO and DMA
1172d5898e19SJarkko Nikula 	 * transfers.
1173d5898e19SJarkko Nikula 	 */
1174d5898e19SJarkko Nikula 	if (atomic_read(&drv_data->dma_running))
1175d5898e19SJarkko Nikula 		pxa2xx_spi_dma_stop(drv_data);
1176ca632f55SGrant Likely }
1177ca632f55SGrant Likely 
117851eea52dSLubomir Rintel static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
11797d94a505SMika Westerberg {
118051eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
11817d94a505SMika Westerberg 
11827d94a505SMika Westerberg 	/* Disable the SSP now */
118329d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
11847d94a505SMika Westerberg 
11857d94a505SMika Westerberg 	return 0;
11867d94a505SMika Westerberg }
11877d94a505SMika Westerberg 
1188de6926f3SAndy Shevchenko static void cleanup_cs(struct spi_device *spi)
1189de6926f3SAndy Shevchenko {
1190de6926f3SAndy Shevchenko 	if (!gpio_is_valid(spi->cs_gpio))
1191de6926f3SAndy Shevchenko 		return;
1192de6926f3SAndy Shevchenko 
1193de6926f3SAndy Shevchenko 	gpio_free(spi->cs_gpio);
1194de6926f3SAndy Shevchenko 	spi->cs_gpio = -ENOENT;
1195de6926f3SAndy Shevchenko }
1196de6926f3SAndy Shevchenko 
1197ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1198ca632f55SGrant Likely 		    struct pxa2xx_spi_chip *chip_info)
1199ca632f55SGrant Likely {
1200de6926f3SAndy Shevchenko 	struct driver_data *drv_data = spi_controller_get_devdata(spi->controller);
1201ca632f55SGrant Likely 
120299f499cdSMika Westerberg 	if (chip == NULL)
120399f499cdSMika Westerberg 		return 0;
120499f499cdSMika Westerberg 
120599f499cdSMika Westerberg 	if (chip_info == NULL)
1206ca632f55SGrant Likely 		return 0;
1207ca632f55SGrant Likely 
1208de6926f3SAndy Shevchenko 	if (drv_data->ssp_type == CE4100_SSP)
1209de6926f3SAndy Shevchenko 		return 0;
1210de6926f3SAndy Shevchenko 
12118083d6b8SAndy Shevchenko 	/*
12128083d6b8SAndy Shevchenko 	 * NOTE: setup() can be called multiple times, possibly with
12138083d6b8SAndy Shevchenko 	 * different chip_info, release previously requested GPIO.
1214ca632f55SGrant Likely 	 */
1215de6926f3SAndy Shevchenko 	cleanup_cs(spi);
1216ca632f55SGrant Likely 
12178083d6b8SAndy Shevchenko 	/* If ->cs_control() is provided, ignore GPIO chip select */
1218ca632f55SGrant Likely 	if (chip_info->cs_control) {
1219ca632f55SGrant Likely 		chip->cs_control = chip_info->cs_control;
1220ca632f55SGrant Likely 		return 0;
1221ca632f55SGrant Likely 	}
1222ca632f55SGrant Likely 
1223ca632f55SGrant Likely 	if (gpio_is_valid(chip_info->gpio_cs)) {
1224de6926f3SAndy Shevchenko 		int gpio = chip_info->gpio_cs;
1225de6926f3SAndy Shevchenko 		int err;
1226de6926f3SAndy Shevchenko 
1227de6926f3SAndy Shevchenko 		err = gpio_request(gpio, "SPI_CS");
1228ca632f55SGrant Likely 		if (err) {
1229de6926f3SAndy Shevchenko 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n", gpio);
1230ca632f55SGrant Likely 			return err;
1231ca632f55SGrant Likely 		}
1232ca632f55SGrant Likely 
1233de6926f3SAndy Shevchenko 		err = gpio_direction_output(gpio, !(spi->mode & SPI_CS_HIGH));
1234de6926f3SAndy Shevchenko 		if (err) {
1235de6926f3SAndy Shevchenko 			gpio_free(gpio);
1236de6926f3SAndy Shevchenko 			return err;
1237ca632f55SGrant Likely 		}
1238ca632f55SGrant Likely 
1239de6926f3SAndy Shevchenko 		spi->cs_gpio = gpio;
1240de6926f3SAndy Shevchenko 	}
1241de6926f3SAndy Shevchenko 
1242de6926f3SAndy Shevchenko 	return 0;
1243ca632f55SGrant Likely }
1244ca632f55SGrant Likely 
1245ca632f55SGrant Likely static int setup(struct spi_device *spi)
1246ca632f55SGrant Likely {
1247bffc967eSJarkko Nikula 	struct pxa2xx_spi_chip *chip_info;
1248ca632f55SGrant Likely 	struct chip_data *chip;
1249dccf7369SJarkko Nikula 	const struct lpss_config *config;
12503cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12513cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1252a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1253a0d2642eSMika Westerberg 
1254e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1255e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1256e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1257e5262d05SWeike Chen 		tx_hi_thres = 0;
1258e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1259e5262d05SWeike Chen 		break;
12603fdb59cfSAndy Shevchenko 	case MRFLD_SSP:
12613fdb59cfSAndy Shevchenko 		tx_thres = TX_THRESH_MRFLD_DFLT;
12623fdb59cfSAndy Shevchenko 		tx_hi_thres = 0;
12633fdb59cfSAndy Shevchenko 		rx_thres = RX_THRESH_MRFLD_DFLT;
12643fdb59cfSAndy Shevchenko 		break;
12657c7289a4SAndy Shevchenko 	case CE4100_SSP:
12667c7289a4SAndy Shevchenko 		tx_thres = TX_THRESH_CE4100_DFLT;
12677c7289a4SAndy Shevchenko 		tx_hi_thres = 0;
12687c7289a4SAndy Shevchenko 		rx_thres = RX_THRESH_CE4100_DFLT;
12697c7289a4SAndy Shevchenko 		break;
127003fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
127103fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
127230f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
127334cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1274b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1275fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
1276dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1277dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1278dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1279dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1280e5262d05SWeike Chen 		break;
1281e5262d05SWeike Chen 	default:
1282a0d2642eSMika Westerberg 		tx_hi_thres = 0;
128351eea52dSLubomir Rintel 		if (spi_controller_is_slave(drv_data->controller)) {
1284ec93cb6fSLubomir Rintel 			tx_thres = 1;
1285ec93cb6fSLubomir Rintel 			rx_thres = 2;
1286ec93cb6fSLubomir Rintel 		} else {
1287ec93cb6fSLubomir Rintel 			tx_thres = TX_THRESH_DFLT;
1288a0d2642eSMika Westerberg 			rx_thres = RX_THRESH_DFLT;
1289ec93cb6fSLubomir Rintel 		}
1290e5262d05SWeike Chen 		break;
1291a0d2642eSMika Westerberg 	}
1292ca632f55SGrant Likely 
12938083d6b8SAndy Shevchenko 	/* Only allocate on the first setup */
1294ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1295ca632f55SGrant Likely 	if (!chip) {
1296ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
12979deae459SJingoo Han 		if (!chip)
1298ca632f55SGrant Likely 			return -ENOMEM;
1299ca632f55SGrant Likely 
1300ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1301ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1302f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1303f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1304ca632f55SGrant Likely 				kfree(chip);
1305ca632f55SGrant Likely 				return -EINVAL;
1306ca632f55SGrant Likely 			}
1307c18d925fSJan Kiszka 		}
130851eea52dSLubomir Rintel 		chip->enable_dma = drv_data->controller_info->enable_dma;
1309ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1310ca632f55SGrant Likely 	}
1311ca632f55SGrant Likely 
13128083d6b8SAndy Shevchenko 	/*
13138083d6b8SAndy Shevchenko 	 * Protocol drivers may change the chip settings, so...
13148083d6b8SAndy Shevchenko 	 * if chip_info exists, use it.
13158083d6b8SAndy Shevchenko 	 */
1316ca632f55SGrant Likely 	chip_info = spi->controller_data;
1317ca632f55SGrant Likely 
1318ca632f55SGrant Likely 	/* chip_info isn't always needed */
1319ca632f55SGrant Likely 	chip->cr1 = 0;
1320ca632f55SGrant Likely 	if (chip_info) {
1321ca632f55SGrant Likely 		if (chip_info->timeout)
1322ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1323ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1324ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1325a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1326a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1327ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1328ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1329ca632f55SGrant Likely 		chip->dma_threshold = 0;
1330ca632f55SGrant Likely 		if (chip_info->enable_loopback)
1331ca632f55SGrant Likely 			chip->cr1 = SSCR1_LBM;
1332ca632f55SGrant Likely 	}
133351eea52dSLubomir Rintel 	if (spi_controller_is_slave(drv_data->controller)) {
1334ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCFR;
1335ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCLKDIR;
1336ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SFRMDIR;
1337ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SPH;
1338ec93cb6fSLubomir Rintel 	}
1339ca632f55SGrant Likely 
13403fdb59cfSAndy Shevchenko 	if (is_lpss_ssp(drv_data)) {
1341a0d2642eSMika Westerberg 		chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
13423fdb59cfSAndy Shevchenko 		chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
13433fdb59cfSAndy Shevchenko 					  SSITF_TxHiThresh(tx_hi_thres);
13443fdb59cfSAndy Shevchenko 	}
13453fdb59cfSAndy Shevchenko 
13463fdb59cfSAndy Shevchenko 	if (is_mrfld_ssp(drv_data)) {
13473fdb59cfSAndy Shevchenko 		chip->lpss_rx_threshold = rx_thres;
13483fdb59cfSAndy Shevchenko 		chip->lpss_tx_threshold = tx_thres;
13493fdb59cfSAndy Shevchenko 	}
1350a0d2642eSMika Westerberg 
13518083d6b8SAndy Shevchenko 	/*
13528083d6b8SAndy Shevchenko 	 * Set DMA burst and threshold outside of chip_info path so that if
13538083d6b8SAndy Shevchenko 	 * chip_info goes away after setting chip->enable_dma, the burst and
13548083d6b8SAndy Shevchenko 	 * threshold can still respond to changes in bits_per_word.
13558083d6b8SAndy Shevchenko 	 */
1356ca632f55SGrant Likely 	if (chip->enable_dma) {
13578083d6b8SAndy Shevchenko 		/* Set up legal burst and threshold for DMA */
1358cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1359cd7bed00SMika Westerberg 						spi->bits_per_word,
1360ca632f55SGrant Likely 						&chip->dma_burst_size,
1361ca632f55SGrant Likely 						&chip->dma_threshold)) {
1362f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1363f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1364ca632f55SGrant Likely 		}
1365000c6af4SAndy Shevchenko 		dev_dbg(&spi->dev,
1366000c6af4SAndy Shevchenko 			"in setup: DMA burst size set to %u\n",
1367000c6af4SAndy Shevchenko 			chip->dma_burst_size);
1368ca632f55SGrant Likely 	}
1369ca632f55SGrant Likely 
1370e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1371e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1372e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1373e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1374e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1375e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1376e5262d05SWeike Chen 		break;
13777c7289a4SAndy Shevchenko 	case CE4100_SSP:
13787c7289a4SAndy Shevchenko 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
13797c7289a4SAndy Shevchenko 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
13807c7289a4SAndy Shevchenko 		break;
1381e5262d05SWeike Chen 	default:
1382e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1383e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1384e5262d05SWeike Chen 		break;
1385e5262d05SWeike Chen 	}
1386e5262d05SWeike Chen 
1387ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1388eb743ec6SAndy Shevchenko 	chip->cr1 |= ((spi->mode & SPI_CPHA) ? SSCR1_SPH : 0) |
1389eb743ec6SAndy Shevchenko 		     ((spi->mode & SPI_CPOL) ? SSCR1_SPO : 0);
1390ca632f55SGrant Likely 
1391b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1392b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1393b833172fSMika Westerberg 
1394ca632f55SGrant Likely 	if (spi->bits_per_word <= 8) {
1395ca632f55SGrant Likely 		chip->n_bytes = 1;
1396ca632f55SGrant Likely 		chip->read = u8_reader;
1397ca632f55SGrant Likely 		chip->write = u8_writer;
1398ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 16) {
1399ca632f55SGrant Likely 		chip->n_bytes = 2;
1400ca632f55SGrant Likely 		chip->read = u16_reader;
1401ca632f55SGrant Likely 		chip->write = u16_writer;
1402ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 32) {
1403ca632f55SGrant Likely 		chip->n_bytes = 4;
1404ca632f55SGrant Likely 		chip->read = u32_reader;
1405ca632f55SGrant Likely 		chip->write = u32_writer;
1406ca632f55SGrant Likely 	}
1407ca632f55SGrant Likely 
1408ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1409ca632f55SGrant Likely 
1410ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
1411ca632f55SGrant Likely 		return 0;
1412ca632f55SGrant Likely 
1413ca632f55SGrant Likely 	return setup_cs(spi, chip, chip_info);
1414ca632f55SGrant Likely }
1415ca632f55SGrant Likely 
1416ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1417ca632f55SGrant Likely {
1418ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
1419ca632f55SGrant Likely 
1420de6926f3SAndy Shevchenko 	cleanup_cs(spi);
1421ca632f55SGrant Likely 	kfree(chip);
1422ca632f55SGrant Likely }
1423ca632f55SGrant Likely 
14249b2d6119SLee Jones #ifdef CONFIG_ACPI
14258422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
142603fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
142703fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
142803fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
142903fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
143003fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
143130f3a6abSMika Westerberg 	{ "8086228E", LPSS_BSW_SSP },
143203fbf488SJarkko Nikula 	{ },
143303fbf488SJarkko Nikula };
143403fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
14359b2d6119SLee Jones #endif
143603fbf488SJarkko Nikula 
143734cadd9cSJarkko Nikula /*
143834cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
143934cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
144034cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
144134cadd9cSJarkko Nikula  */
144234cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
144334cadd9cSJarkko Nikula 	/* SPT-LP */
144434cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
144534cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
144634cadd9cSJarkko Nikula 	/* SPT-H */
144734cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
144834cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1449704d2b07SMika Westerberg 	/* KBL-H */
1450704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1451704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
14526157d4c2SJarkko Nikula 	/* CML-V */
14536157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
14546157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
1455c1b03f11SJarkko Nikula 	/* BXT A-Step */
1456b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1457b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1458b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1459c1b03f11SJarkko Nikula 	/* BXT B-Step */
1460c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1461c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1462c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1463e18a80acSDavid E. Box 	/* GLK */
1464e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1465e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1466e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
146722d71a50SMika Westerberg 	/* ICL-LP */
146822d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
146922d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
147022d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
14718cc77204SJarkko Nikula 	/* EHL */
14728cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
14738cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
14748cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
14759c7315c9SJarkko Nikula 	/* JSL */
14769c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
14779c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
14789c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
1479cf961fceSJarkko Nikula 	/* TGL-H */
1480cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
1481cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
1482cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
1483cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
1484a402e397SJarkko Nikula 	/* ADL-P */
1485a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51aa), LPSS_CNL_SSP },
1486a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51ab), LPSS_CNL_SSP },
1487a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51fb), LPSS_CNL_SSP },
14888c4ffe4dSJarkko Nikula 	/* ADL-M */
14898c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54aa), LPSS_CNL_SSP },
14908c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54ab), LPSS_CNL_SSP },
14918c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54fb), LPSS_CNL_SSP },
1492b7c08cf8SJarkko Nikula 	/* APL */
1493b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1494b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1495b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1496b8450e01SJarkko Nikula 	/* ADL-S */
1497b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7aaa), LPSS_CNL_SSP },
1498b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7aab), LPSS_CNL_SSP },
1499b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7af9), LPSS_CNL_SSP },
1500b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7afb), LPSS_CNL_SSP },
1501fc0b2accSJarkko Nikula 	/* CNL-LP */
1502fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1503fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1504fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1505fc0b2accSJarkko Nikula 	/* CNL-H */
1506fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1507fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1508fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
150941a91802SEvan Green 	/* CML-LP */
151041a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
151141a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
151241a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
1513f0cf17edSJarkko Nikula 	/* CML-H */
1514f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1515f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1516f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
1517a4127952SJarkko Nikula 	/* TGL-LP */
1518a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1519a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1520a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1521a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1522a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1523a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1524a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
152594e5c23dSAxel Lin 	{ },
152634cadd9cSJarkko Nikula };
152734cadd9cSJarkko Nikula 
152887ae1d2dSLubomir Rintel static const struct of_device_id pxa2xx_spi_of_match[] = {
152987ae1d2dSLubomir Rintel 	{ .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
153087ae1d2dSLubomir Rintel 	{},
153187ae1d2dSLubomir Rintel };
153287ae1d2dSLubomir Rintel MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
153387ae1d2dSLubomir Rintel 
153487ae1d2dSLubomir Rintel #ifdef CONFIG_ACPI
153587ae1d2dSLubomir Rintel 
1536365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
153787ae1d2dSLubomir Rintel {
1538365e856eSAndy Shevchenko 	struct acpi_device *adev;
153987ae1d2dSLubomir Rintel 	unsigned int devid;
154087ae1d2dSLubomir Rintel 	int port_id = -1;
154187ae1d2dSLubomir Rintel 
1542365e856eSAndy Shevchenko 	adev = ACPI_COMPANION(dev);
154387ae1d2dSLubomir Rintel 	if (adev && adev->pnp.unique_id &&
154487ae1d2dSLubomir Rintel 	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
154587ae1d2dSLubomir Rintel 		port_id = devid;
154687ae1d2dSLubomir Rintel 	return port_id;
154787ae1d2dSLubomir Rintel }
154887ae1d2dSLubomir Rintel 
154987ae1d2dSLubomir Rintel #else /* !CONFIG_ACPI */
155087ae1d2dSLubomir Rintel 
1551365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
155287ae1d2dSLubomir Rintel {
155387ae1d2dSLubomir Rintel 	return -1;
155487ae1d2dSLubomir Rintel }
155587ae1d2dSLubomir Rintel 
155687ae1d2dSLubomir Rintel #endif /* CONFIG_ACPI */
155787ae1d2dSLubomir Rintel 
155887ae1d2dSLubomir Rintel 
155987ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
156087ae1d2dSLubomir Rintel 
156134cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
156234cadd9cSJarkko Nikula {
15635ba846b1SAndy Shevchenko 	return param == chan->device->dev;
156434cadd9cSJarkko Nikula }
156534cadd9cSJarkko Nikula 
156687ae1d2dSLubomir Rintel #endif /* CONFIG_PCI */
156787ae1d2dSLubomir Rintel 
156851eea52dSLubomir Rintel static struct pxa2xx_spi_controller *
15690db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1570a3496855SMika Westerberg {
157151eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *pdata;
1572a3496855SMika Westerberg 	struct ssp_device *ssp;
1573a3496855SMika Westerberg 	struct resource *res;
15746fb7427dSAndy Shevchenko 	struct device *parent = pdev->dev.parent;
15756fb7427dSAndy Shevchenko 	struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
157634cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
157755ef8262SLubomir Rintel 	enum pxa_ssp_type type;
1578f2faa3ecSAndy Shevchenko 	const void *match;
1579a3496855SMika Westerberg 
15806fb7427dSAndy Shevchenko 	if (pcidev)
15816fb7427dSAndy Shevchenko 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
1582a3496855SMika Westerberg 
1583f2faa3ecSAndy Shevchenko 	match = device_get_match_data(&pdev->dev);
1584f2faa3ecSAndy Shevchenko 	if (match)
1585f2faa3ecSAndy Shevchenko 		type = (enum pxa_ssp_type)match;
158634cadd9cSJarkko Nikula 	else if (pcidev_id)
158755ef8262SLubomir Rintel 		type = (enum pxa_ssp_type)pcidev_id->driver_data;
158803fbf488SJarkko Nikula 	else
158914af1df3SAndy Shevchenko 		return ERR_PTR(-EINVAL);
159003fbf488SJarkko Nikula 
1591cc0ee987SMika Westerberg 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
15929deae459SJingoo Han 	if (!pdata)
159314af1df3SAndy Shevchenko 		return ERR_PTR(-ENOMEM);
1594a3496855SMika Westerberg 
1595a3496855SMika Westerberg 	ssp = &pdata->ssp;
1596a3496855SMika Westerberg 
159777c544d2SAndy Shevchenko 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1598cbfd6a21SSachin Kamat 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1599cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
160014af1df3SAndy Shevchenko 		return ERR_CAST(ssp->mmio_base);
1601a3496855SMika Westerberg 
160277c544d2SAndy Shevchenko 	ssp->phys_base = res->start;
160377c544d2SAndy Shevchenko 
160487ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
160534cadd9cSJarkko Nikula 	if (pcidev_id) {
16066fb7427dSAndy Shevchenko 		pdata->tx_param = parent;
16076fb7427dSAndy Shevchenko 		pdata->rx_param = parent;
160834cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
160934cadd9cSJarkko Nikula 	}
161087ae1d2dSLubomir Rintel #endif
161134cadd9cSJarkko Nikula 
1612a3496855SMika Westerberg 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
16135eb263efSChuhong Yuan 	if (IS_ERR(ssp->clk))
161414af1df3SAndy Shevchenko 		return ERR_CAST(ssp->clk);
1615a3496855SMika Westerberg 
1616a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
16175eb263efSChuhong Yuan 	if (ssp->irq < 0)
161814af1df3SAndy Shevchenko 		return ERR_PTR(ssp->irq);
16195eb263efSChuhong Yuan 
1620a3496855SMika Westerberg 	ssp->type = type;
16214f3d9577SAndy Shevchenko 	ssp->dev = &pdev->dev;
1622365e856eSAndy Shevchenko 	ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
1623a3496855SMika Westerberg 
1624f2faa3ecSAndy Shevchenko 	pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
1625a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1626cddb339bSMika Westerberg 	pdata->enable_dma = true;
162737821a82SAndy Shevchenko 	pdata->dma_burst_size = 1;
1628a3496855SMika Westerberg 
1629a3496855SMika Westerberg 	return pdata;
1630a3496855SMika Westerberg }
1631a3496855SMika Westerberg 
163251eea52dSLubomir Rintel static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
16333cc7b0e3SJarkko Nikula 				      unsigned int cs)
16340c27d9cfSMika Westerberg {
163551eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
16360c27d9cfSMika Westerberg 
1637c3dce24cSAndy Shevchenko 	if (has_acpi_companion(drv_data->ssp->dev)) {
16380c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
16390c27d9cfSMika Westerberg 		/*
16400c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
16410c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
16420c27d9cfSMika Westerberg 		 * to match what Linux expects.
16430c27d9cfSMika Westerberg 		 */
16440c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
164530f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
16460c27d9cfSMika Westerberg 			return cs - 1;
16470c27d9cfSMika Westerberg 
16480c27d9cfSMika Westerberg 		default:
16490c27d9cfSMika Westerberg 			break;
16500c27d9cfSMika Westerberg 		}
16510c27d9cfSMika Westerberg 	}
16520c27d9cfSMika Westerberg 
16530c27d9cfSMika Westerberg 	return cs;
16540c27d9cfSMika Westerberg }
16550c27d9cfSMika Westerberg 
1656b2662a16SDaniel Vetter static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1657b2662a16SDaniel Vetter {
1658b2662a16SDaniel Vetter 	return MAX_DMA_LEN;
1659b2662a16SDaniel Vetter }
1660b2662a16SDaniel Vetter 
1661fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1662ca632f55SGrant Likely {
1663ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
166451eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *platform_info;
166551eea52dSLubomir Rintel 	struct spi_controller *controller;
1666ca632f55SGrant Likely 	struct driver_data *drv_data;
1667ca632f55SGrant Likely 	struct ssp_device *ssp;
16688b136baaSJarkko Nikula 	const struct lpss_config *config;
1669778c12e6SAndy Shevchenko 	int status;
1670c039dd27SJarkko Nikula 	u32 tmp;
1671ca632f55SGrant Likely 
1672851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1673851bacf5SMika Westerberg 	if (!platform_info) {
16740db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
167514af1df3SAndy Shevchenko 		if (IS_ERR(platform_info)) {
1676851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
167714af1df3SAndy Shevchenko 			return PTR_ERR(platform_info);
1678851bacf5SMika Westerberg 		}
1679a3496855SMika Westerberg 	}
1680ca632f55SGrant Likely 
1681ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1682851bacf5SMika Westerberg 	if (!ssp)
1683851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1684851bacf5SMika Westerberg 
1685851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
16868083d6b8SAndy Shevchenko 		dev_err(&pdev->dev, "failed to get SSP\n");
1687ca632f55SGrant Likely 		return -ENODEV;
1688ca632f55SGrant Likely 	}
1689ca632f55SGrant Likely 
1690ec93cb6fSLubomir Rintel 	if (platform_info->is_slave)
16915626308bSLukas Wunner 		controller = devm_spi_alloc_slave(dev, sizeof(*drv_data));
1692ec93cb6fSLubomir Rintel 	else
16935626308bSLukas Wunner 		controller = devm_spi_alloc_master(dev, sizeof(*drv_data));
1694ec93cb6fSLubomir Rintel 
169551eea52dSLubomir Rintel 	if (!controller) {
169651eea52dSLubomir Rintel 		dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1697f2eed8caSAndy Shevchenko 		status = -ENOMEM;
1698f2eed8caSAndy Shevchenko 		goto out_error_controller_alloc;
1699ca632f55SGrant Likely 	}
170051eea52dSLubomir Rintel 	drv_data = spi_controller_get_devdata(controller);
170151eea52dSLubomir Rintel 	drv_data->controller = controller;
170251eea52dSLubomir Rintel 	drv_data->controller_info = platform_info;
1703ca632f55SGrant Likely 	drv_data->ssp = ssp;
1704ca632f55SGrant Likely 
170594acf807SAndy Shevchenko 	controller->dev.of_node = dev->of_node;
170694acf807SAndy Shevchenko 	controller->dev.fwnode = dev->fwnode;
170794acf807SAndy Shevchenko 
17088083d6b8SAndy Shevchenko 	/* The spi->mode bits understood by this driver: */
170951eea52dSLubomir Rintel 	controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1710ca632f55SGrant Likely 
171151eea52dSLubomir Rintel 	controller->bus_num = ssp->port_id;
171251eea52dSLubomir Rintel 	controller->dma_alignment = DMA_ALIGNMENT;
171351eea52dSLubomir Rintel 	controller->cleanup = cleanup;
171451eea52dSLubomir Rintel 	controller->setup = setup;
171551eea52dSLubomir Rintel 	controller->set_cs = pxa2xx_spi_set_cs;
171651eea52dSLubomir Rintel 	controller->transfer_one = pxa2xx_spi_transfer_one;
171751eea52dSLubomir Rintel 	controller->slave_abort = pxa2xx_spi_slave_abort;
171851eea52dSLubomir Rintel 	controller->handle_err = pxa2xx_spi_handle_err;
171951eea52dSLubomir Rintel 	controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
172051eea52dSLubomir Rintel 	controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
172151eea52dSLubomir Rintel 	controller->auto_runtime_pm = true;
172251eea52dSLubomir Rintel 	controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1723ca632f55SGrant Likely 
1724ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1725ca632f55SGrant Likely 
1726ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1727e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1728e5262d05SWeike Chen 		case QUARK_X1000_SSP:
172951eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1730e5262d05SWeike Chen 			break;
1731e5262d05SWeike Chen 		default:
173251eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1733e5262d05SWeike Chen 			break;
1734e5262d05SWeike Chen 		}
1735e5262d05SWeike Chen 
1736ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1737ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1738ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1739ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1740ca632f55SGrant Likely 	} else {
174151eea52dSLubomir Rintel 		controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1742ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
17435928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1744ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1745ec93cb6fSLubomir Rintel 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1746ec93cb6fSLubomir Rintel 						| SSSR_ROR | SSSR_TUR;
1747ca632f55SGrant Likely 	}
1748ca632f55SGrant Likely 
1749ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1750ca632f55SGrant Likely 			drv_data);
1751ca632f55SGrant Likely 	if (status < 0) {
1752ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
175351eea52dSLubomir Rintel 		goto out_error_controller_alloc;
1754ca632f55SGrant Likely 	}
1755ca632f55SGrant Likely 
1756ca632f55SGrant Likely 	/* Setup DMA if requested */
1757ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1758cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1759cd7bed00SMika Westerberg 		if (status) {
17608b57b11bSFlavio Suligoi 			dev_warn(dev, "no DMA channels available, using PIO\n");
1761cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1762b6ced294SJarkko Nikula 		} else {
176351eea52dSLubomir Rintel 			controller->can_dma = pxa2xx_spi_can_dma;
1764bf9f742cSMark Brown 			controller->max_dma_len = MAX_DMA_LEN;
1765b2662a16SDaniel Vetter 			controller->max_transfer_size =
1766b2662a16SDaniel Vetter 				pxa2xx_spi_max_dma_transfer_size;
1767ca632f55SGrant Likely 		}
1768ca632f55SGrant Likely 	}
1769ca632f55SGrant Likely 
1770ca632f55SGrant Likely 	/* Enable SOC clock */
177162bbc864STobias Jordan 	status = clk_prepare_enable(ssp->clk);
177262bbc864STobias Jordan 	if (status)
177362bbc864STobias Jordan 		goto out_error_dma_irq_alloc;
17743343b7a6SMika Westerberg 
177551eea52dSLubomir Rintel 	controller->max_speed_hz = clk_get_rate(ssp->clk);
177623cdddb2SJarkko Nikula 	/*
177723cdddb2SJarkko Nikula 	 * Set minimum speed for all other platforms than Intel Quark which is
177823cdddb2SJarkko Nikula 	 * able do under 1 Hz transfers.
177923cdddb2SJarkko Nikula 	 */
178023cdddb2SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
178123cdddb2SJarkko Nikula 		controller->min_speed_hz =
178223cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 4096);
178323cdddb2SJarkko Nikula 	else if (!is_quark_x1000_ssp(drv_data))
178423cdddb2SJarkko Nikula 		controller->min_speed_hz =
178523cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 512);
1786ca632f55SGrant Likely 
17870c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
17880c8ccd8bSAndy Shevchenko 
1789ca632f55SGrant Likely 	/* Load default SSP configuration */
1790e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1791e5262d05SWeike Chen 	case QUARK_X1000_SSP:
17927c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
17937c7289a4SAndy Shevchenko 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1794c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1795e5262d05SWeike Chen 
17968083d6b8SAndy Shevchenko 		/* Using the Motorola SPI protocol and use 8 bit frame */
17977c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
17987c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1799e5262d05SWeike Chen 		break;
18007c7289a4SAndy Shevchenko 	case CE4100_SSP:
18017c7289a4SAndy Shevchenko 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
18027c7289a4SAndy Shevchenko 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
18037c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
18047c7289a4SAndy Shevchenko 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
18057c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1806a2dd8af0SAndy Shevchenko 		break;
1807e5262d05SWeike Chen 	default:
1808ec93cb6fSLubomir Rintel 
180951eea52dSLubomir Rintel 		if (spi_controller_is_slave(controller)) {
1810ec93cb6fSLubomir Rintel 			tmp = SSCR1_SCFR |
1811ec93cb6fSLubomir Rintel 			      SSCR1_SCLKDIR |
1812ec93cb6fSLubomir Rintel 			      SSCR1_SFRMDIR |
1813ec93cb6fSLubomir Rintel 			      SSCR1_RxTresh(2) |
1814ec93cb6fSLubomir Rintel 			      SSCR1_TxTresh(1) |
1815ec93cb6fSLubomir Rintel 			      SSCR1_SPH;
1816ec93cb6fSLubomir Rintel 		} else {
1817c039dd27SJarkko Nikula 			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1818c039dd27SJarkko Nikula 			      SSCR1_TxTresh(TX_THRESH_DFLT);
1819ec93cb6fSLubomir Rintel 		}
1820c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1821ec93cb6fSLubomir Rintel 		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
182251eea52dSLubomir Rintel 		if (!spi_controller_is_slave(controller))
1823ec93cb6fSLubomir Rintel 			tmp |= SSCR0_SCR(2);
1824c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1825e5262d05SWeike Chen 		break;
1826e5262d05SWeike Chen 	}
1827e5262d05SWeike Chen 
1828ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1829c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1830e5262d05SWeike Chen 
1831e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1832c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1833ca632f55SGrant Likely 
18348b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
18358b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
18368b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
18378b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
18388b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
18398b136baaSJarkko Nikula 						   config->reg_capabilities);
18408b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
18418b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
18428b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
184330f3a6abSMika Westerberg 		} else if (config->cs_num) {
184430f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
18458b136baaSJarkko Nikula 		}
18468b136baaSJarkko Nikula 	}
184751eea52dSLubomir Rintel 	controller->num_chipselect = platform_info->num_chipselect;
1848778c12e6SAndy Shevchenko 	controller->use_gpio_descriptors = true;
18496ac5a435SAndy Shevchenko 
185077d33897SLubomir Rintel 	if (platform_info->is_slave) {
185177d33897SLubomir Rintel 		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
185277d33897SLubomir Rintel 						"ready", GPIOD_OUT_LOW);
185377d33897SLubomir Rintel 		if (IS_ERR(drv_data->gpiod_ready)) {
185477d33897SLubomir Rintel 			status = PTR_ERR(drv_data->gpiod_ready);
185577d33897SLubomir Rintel 			goto out_error_clock_enabled;
185677d33897SLubomir Rintel 		}
185777d33897SLubomir Rintel 	}
185877d33897SLubomir Rintel 
1859836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1860836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1861836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1862836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1863836d1a22SAntonio Ospite 
1864ca632f55SGrant Likely 	/* Register with the SPI framework */
1865ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
186632e5b572SLukas Wunner 	status = spi_register_controller(controller);
1867eb743ec6SAndy Shevchenko 	if (status) {
18688083d6b8SAndy Shevchenko 		dev_err(&pdev->dev, "problem registering SPI controller\n");
186912742045SLubomir Rintel 		goto out_error_pm_runtime_enabled;
1870ca632f55SGrant Likely 	}
1871ca632f55SGrant Likely 
1872ca632f55SGrant Likely 	return status;
1873ca632f55SGrant Likely 
187412742045SLubomir Rintel out_error_pm_runtime_enabled:
1875e2b714afSJarkko Nikula 	pm_runtime_disable(&pdev->dev);
187612742045SLubomir Rintel 
187712742045SLubomir Rintel out_error_clock_enabled:
18783343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
187962bbc864STobias Jordan 
188062bbc864STobias Jordan out_error_dma_irq_alloc:
1881cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1882ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1883ca632f55SGrant Likely 
188451eea52dSLubomir Rintel out_error_controller_alloc:
1885ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1886ca632f55SGrant Likely 	return status;
1887ca632f55SGrant Likely }
1888ca632f55SGrant Likely 
1889ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1890ca632f55SGrant Likely {
1891ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
18923d24b2a4SAndy Shevchenko 	struct ssp_device *ssp = drv_data->ssp;
1893ca632f55SGrant Likely 
18947d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
18957d94a505SMika Westerberg 
189632e5b572SLukas Wunner 	spi_unregister_controller(drv_data->controller);
189732e5b572SLukas Wunner 
1898ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
18990c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
19003343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1901ca632f55SGrant Likely 
1902ca632f55SGrant Likely 	/* Release DMA */
190351eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma)
1904cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1905ca632f55SGrant Likely 
19067d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
19077d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
19087d94a505SMika Westerberg 
1909ca632f55SGrant Likely 	/* Release IRQ */
1910ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1911ca632f55SGrant Likely 
1912ca632f55SGrant Likely 	/* Release SSP */
1913ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1914ca632f55SGrant Likely 
1915ca632f55SGrant Likely 	return 0;
1916ca632f55SGrant Likely }
1917ca632f55SGrant Likely 
1918382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP
1919ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1920ca632f55SGrant Likely {
1921ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1922ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1923bffc967eSJarkko Nikula 	int status;
1924ca632f55SGrant Likely 
192551eea52dSLubomir Rintel 	status = spi_controller_suspend(drv_data->controller);
1926eb743ec6SAndy Shevchenko 	if (status)
1927ca632f55SGrant Likely 		return status;
19280c8ccd8bSAndy Shevchenko 
19290c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
19302b9375b9SDmitry Eremin-Solenikov 
19312b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
19323343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1933ca632f55SGrant Likely 
1934ca632f55SGrant Likely 	return 0;
1935ca632f55SGrant Likely }
1936ca632f55SGrant Likely 
1937ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1938ca632f55SGrant Likely {
1939ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1940ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1941bffc967eSJarkko Nikula 	int status;
1942ca632f55SGrant Likely 
1943ca632f55SGrant Likely 	/* Enable the SSP clock */
194462bbc864STobias Jordan 	if (!pm_runtime_suspended(dev)) {
194562bbc864STobias Jordan 		status = clk_prepare_enable(ssp->clk);
194662bbc864STobias Jordan 		if (status)
194762bbc864STobias Jordan 			return status;
194862bbc864STobias Jordan 	}
1949ca632f55SGrant Likely 
1950ca632f55SGrant Likely 	/* Start the queue running */
195151eea52dSLubomir Rintel 	return spi_controller_resume(drv_data->controller);
1952ca632f55SGrant Likely }
19537d94a505SMika Westerberg #endif
19547d94a505SMika Westerberg 
1955ec833050SRafael J. Wysocki #ifdef CONFIG_PM
19567d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
19577d94a505SMika Westerberg {
19587d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
19597d94a505SMika Westerberg 
19607d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
19617d94a505SMika Westerberg 	return 0;
19627d94a505SMika Westerberg }
19637d94a505SMika Westerberg 
19647d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
19657d94a505SMika Westerberg {
19667d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
196762bbc864STobias Jordan 	int status;
19687d94a505SMika Westerberg 
196962bbc864STobias Jordan 	status = clk_prepare_enable(drv_data->ssp->clk);
197062bbc864STobias Jordan 	return status;
19717d94a505SMika Westerberg }
19727d94a505SMika Westerberg #endif
1973ca632f55SGrant Likely 
1974ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
19757d94a505SMika Westerberg 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
19767d94a505SMika Westerberg 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
19777d94a505SMika Westerberg 			   pxa2xx_spi_runtime_resume, NULL)
1978ca632f55SGrant Likely };
1979ca632f55SGrant Likely 
1980ca632f55SGrant Likely static struct platform_driver driver = {
1981ca632f55SGrant Likely 	.driver = {
1982ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
1983ca632f55SGrant Likely 		.pm	= &pxa2xx_spi_pm_ops,
1984a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
198587ae1d2dSLubomir Rintel 		.of_match_table = of_match_ptr(pxa2xx_spi_of_match),
1986ca632f55SGrant Likely 	},
1987ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
1988ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
1989ca632f55SGrant Likely };
1990ca632f55SGrant Likely 
1991ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
1992ca632f55SGrant Likely {
1993ca632f55SGrant Likely 	return platform_driver_register(&driver);
1994ca632f55SGrant Likely }
1995ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
1996ca632f55SGrant Likely 
1997ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
1998ca632f55SGrant Likely {
1999ca632f55SGrant Likely 	platform_driver_unregister(&driver);
2000ca632f55SGrant Likely }
2001ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
200251ebf6acSFlavio Suligoi 
200351ebf6acSFlavio Suligoi MODULE_SOFTDEP("pre: dw_dmac");
2004