xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision 778c12e69481d544e6fcfa45b23ae3c5379b5a02)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
4a0d2642eSMika Westerberg  * Copyright (C) 2013, Intel Corporation
5ca632f55SGrant Likely  */
6ca632f55SGrant Likely 
75ce25705SAndy Shevchenko #include <linux/acpi.h>
88b136baaSJarkko Nikula #include <linux/bitops.h>
95ce25705SAndy Shevchenko #include <linux/clk.h>
105ce25705SAndy Shevchenko #include <linux/delay.h>
11ca632f55SGrant Likely #include <linux/device.h>
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 /*
43ca632f55SGrant Likely  * for testing SSCR1 changes that require SSP restart, basically
44ca632f55SGrant Likely  * everything except the service and interrupt enables, the pxa270 developer
45ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
46ca632f55SGrant Likely  * list, but the PXA255 dev man says all bits without really meaning the
47ca632f55SGrant Likely  * service and interrupt enables
48ca632f55SGrant Likely  */
49ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
50ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
51ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
52ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
53ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
54ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
55ca632f55SGrant Likely 
56e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
57e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
58e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
59e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
60e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
61e5262d05SWeike Chen 
627c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
637c7289a4SAndy Shevchenko 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
647c7289a4SAndy Shevchenko 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
657c7289a4SAndy Shevchenko 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
667c7289a4SAndy Shevchenko 				| CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
677c7289a4SAndy Shevchenko 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
687c7289a4SAndy Shevchenko 
69624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
70624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE			BIT(0)
71624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
728b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT			9
738b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
74a0d2642eSMika Westerberg 
75683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE 0x38
76683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
77683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
78683f65deSEvan Green 
79dccf7369SJarkko Nikula struct lpss_config {
80dccf7369SJarkko Nikula 	/* LPSS offset from drv_data->ioaddr */
81dccf7369SJarkko Nikula 	unsigned offset;
82dccf7369SJarkko Nikula 	/* Register offsets from drv_data->lpss_base or -1 */
83dccf7369SJarkko Nikula 	int reg_general;
84dccf7369SJarkko Nikula 	int reg_ssp;
85dccf7369SJarkko Nikula 	int reg_cs_ctrl;
868b136baaSJarkko Nikula 	int reg_capabilities;
87dccf7369SJarkko Nikula 	/* FIFO thresholds */
88dccf7369SJarkko Nikula 	u32 rx_threshold;
89dccf7369SJarkko Nikula 	u32 tx_threshold_lo;
90dccf7369SJarkko Nikula 	u32 tx_threshold_hi;
91c1e4a53cSMika Westerberg 	/* Chip select control */
92c1e4a53cSMika Westerberg 	unsigned cs_sel_shift;
93c1e4a53cSMika Westerberg 	unsigned cs_sel_mask;
9430f3a6abSMika Westerberg 	unsigned cs_num;
95683f65deSEvan Green 	/* Quirks */
96683f65deSEvan Green 	unsigned cs_clk_stays_gated : 1;
97dccf7369SJarkko Nikula };
98dccf7369SJarkko Nikula 
99dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */
100dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = {
101dccf7369SJarkko Nikula 	{	/* LPSS_LPT_SSP */
102dccf7369SJarkko Nikula 		.offset = 0x800,
103dccf7369SJarkko Nikula 		.reg_general = 0x08,
104dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
105dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1068b136baaSJarkko Nikula 		.reg_capabilities = -1,
107dccf7369SJarkko Nikula 		.rx_threshold = 64,
108dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
109dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
110dccf7369SJarkko Nikula 	},
111dccf7369SJarkko Nikula 	{	/* LPSS_BYT_SSP */
112dccf7369SJarkko Nikula 		.offset = 0x400,
113dccf7369SJarkko Nikula 		.reg_general = 0x08,
114dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
115dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1168b136baaSJarkko Nikula 		.reg_capabilities = -1,
117dccf7369SJarkko Nikula 		.rx_threshold = 64,
118dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
119dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
120dccf7369SJarkko Nikula 	},
12130f3a6abSMika Westerberg 	{	/* LPSS_BSW_SSP */
12230f3a6abSMika Westerberg 		.offset = 0x400,
12330f3a6abSMika Westerberg 		.reg_general = 0x08,
12430f3a6abSMika Westerberg 		.reg_ssp = 0x0c,
12530f3a6abSMika Westerberg 		.reg_cs_ctrl = 0x18,
12630f3a6abSMika Westerberg 		.reg_capabilities = -1,
12730f3a6abSMika Westerberg 		.rx_threshold = 64,
12830f3a6abSMika Westerberg 		.tx_threshold_lo = 160,
12930f3a6abSMika Westerberg 		.tx_threshold_hi = 224,
13030f3a6abSMika Westerberg 		.cs_sel_shift = 2,
13130f3a6abSMika Westerberg 		.cs_sel_mask = 1 << 2,
13230f3a6abSMika Westerberg 		.cs_num = 2,
13330f3a6abSMika Westerberg 	},
13434cadd9cSJarkko Nikula 	{	/* LPSS_SPT_SSP */
13534cadd9cSJarkko Nikula 		.offset = 0x200,
13634cadd9cSJarkko Nikula 		.reg_general = -1,
13734cadd9cSJarkko Nikula 		.reg_ssp = 0x20,
13834cadd9cSJarkko Nikula 		.reg_cs_ctrl = 0x24,
13966ec246eSJarkko Nikula 		.reg_capabilities = -1,
14034cadd9cSJarkko Nikula 		.rx_threshold = 1,
14134cadd9cSJarkko Nikula 		.tx_threshold_lo = 32,
14234cadd9cSJarkko Nikula 		.tx_threshold_hi = 56,
14334cadd9cSJarkko Nikula 	},
144b7c08cf8SJarkko Nikula 	{	/* LPSS_BXT_SSP */
145b7c08cf8SJarkko Nikula 		.offset = 0x200,
146b7c08cf8SJarkko Nikula 		.reg_general = -1,
147b7c08cf8SJarkko Nikula 		.reg_ssp = 0x20,
148b7c08cf8SJarkko Nikula 		.reg_cs_ctrl = 0x24,
149b7c08cf8SJarkko Nikula 		.reg_capabilities = 0xfc,
150b7c08cf8SJarkko Nikula 		.rx_threshold = 1,
151b7c08cf8SJarkko Nikula 		.tx_threshold_lo = 16,
152b7c08cf8SJarkko Nikula 		.tx_threshold_hi = 48,
153c1e4a53cSMika Westerberg 		.cs_sel_shift = 8,
154c1e4a53cSMika Westerberg 		.cs_sel_mask = 3 << 8,
1556eefaee4SEvan Green 		.cs_clk_stays_gated = true,
156b7c08cf8SJarkko Nikula 	},
157fc0b2accSJarkko Nikula 	{	/* LPSS_CNL_SSP */
158fc0b2accSJarkko Nikula 		.offset = 0x200,
159fc0b2accSJarkko Nikula 		.reg_general = -1,
160fc0b2accSJarkko Nikula 		.reg_ssp = 0x20,
161fc0b2accSJarkko Nikula 		.reg_cs_ctrl = 0x24,
162fc0b2accSJarkko Nikula 		.reg_capabilities = 0xfc,
163fc0b2accSJarkko Nikula 		.rx_threshold = 1,
164fc0b2accSJarkko Nikula 		.tx_threshold_lo = 32,
165fc0b2accSJarkko Nikula 		.tx_threshold_hi = 56,
166fc0b2accSJarkko Nikula 		.cs_sel_shift = 8,
167fc0b2accSJarkko Nikula 		.cs_sel_mask = 3 << 8,
168683f65deSEvan Green 		.cs_clk_stays_gated = true,
169fc0b2accSJarkko Nikula 	},
170dccf7369SJarkko Nikula };
171dccf7369SJarkko Nikula 
172dccf7369SJarkko Nikula static inline const struct lpss_config
173dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data)
174dccf7369SJarkko Nikula {
175dccf7369SJarkko Nikula 	return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
176dccf7369SJarkko Nikula }
177dccf7369SJarkko Nikula 
178a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data)
179a0d2642eSMika Westerberg {
18003fbf488SJarkko Nikula 	switch (drv_data->ssp_type) {
18103fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
18203fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
18330f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
18434cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
185b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
186fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
18703fbf488SJarkko Nikula 		return true;
18803fbf488SJarkko Nikula 	default:
18903fbf488SJarkko Nikula 		return false;
19003fbf488SJarkko Nikula 	}
191a0d2642eSMika Westerberg }
192a0d2642eSMika Westerberg 
193e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
194e5262d05SWeike Chen {
195e5262d05SWeike Chen 	return drv_data->ssp_type == QUARK_X1000_SSP;
196e5262d05SWeike Chen }
197e5262d05SWeike Chen 
19841c98841SAndy Shevchenko static bool is_mmp2_ssp(const struct driver_data *drv_data)
19941c98841SAndy Shevchenko {
20041c98841SAndy Shevchenko 	return drv_data->ssp_type == MMP2_SSP;
20141c98841SAndy Shevchenko }
20241c98841SAndy Shevchenko 
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) {
43596579a4eSJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, chip->frm);
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 
444c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
445c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
446a0d2642eSMika Westerberg 		return;
447a0d2642eSMika Westerberg 	}
448a0d2642eSMika Westerberg 
4497566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
450d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, true);
451ca632f55SGrant Likely }
452ca632f55SGrant Likely 
453d5898e19SJarkko Nikula static void cs_deassert(struct spi_device *spi)
454ca632f55SGrant Likely {
455d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
456d5898e19SJarkko Nikula 	struct driver_data *drv_data =
457d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
458104e51afSJarkko Nikula 	unsigned long timeout;
459ca632f55SGrant Likely 
460ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
461ca632f55SGrant Likely 		return;
462ca632f55SGrant Likely 
463104e51afSJarkko Nikula 	/* Wait until SSP becomes idle before deasserting the CS */
464104e51afSJarkko Nikula 	timeout = jiffies + msecs_to_jiffies(10);
465104e51afSJarkko Nikula 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
466104e51afSJarkko Nikula 	       !time_after(jiffies, timeout))
467104e51afSJarkko Nikula 		cpu_relax();
468104e51afSJarkko Nikula 
469ca632f55SGrant Likely 	if (chip->cs_control) {
470ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_DEASSERT);
471ca632f55SGrant Likely 		return;
472ca632f55SGrant Likely 	}
473ca632f55SGrant Likely 
474c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
475c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
476a0d2642eSMika Westerberg 		return;
477a0d2642eSMika Westerberg 	}
478a0d2642eSMika Westerberg 
4797566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
480d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, false);
481d5898e19SJarkko Nikula }
482d5898e19SJarkko Nikula 
483d5898e19SJarkko Nikula static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
484d5898e19SJarkko Nikula {
485d5898e19SJarkko Nikula 	if (level)
486d5898e19SJarkko Nikula 		cs_deassert(spi);
487d5898e19SJarkko Nikula 	else
488d5898e19SJarkko Nikula 		cs_assert(spi);
489ca632f55SGrant Likely }
490ca632f55SGrant Likely 
491cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
492ca632f55SGrant Likely {
493ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
494ca632f55SGrant Likely 
495ca632f55SGrant Likely 	do {
4966d380132SAndy Shevchenko 		while (read_SSSR_bits(drv_data, SSSR_RNE))
497c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
498c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
499ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
500ca632f55SGrant Likely 
501ca632f55SGrant Likely 	return limit;
502ca632f55SGrant Likely }
503ca632f55SGrant Likely 
50429d7e05cSLubomir Rintel static void pxa2xx_spi_off(struct driver_data *drv_data)
50529d7e05cSLubomir Rintel {
50641c98841SAndy Shevchenko 	/* On MMP, disabling SSE seems to corrupt the Rx FIFO */
50741c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data))
50829d7e05cSLubomir Rintel 		return;
50929d7e05cSLubomir Rintel 
5100c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(drv_data->ssp);
51129d7e05cSLubomir Rintel }
51229d7e05cSLubomir Rintel 
513ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
514ca632f55SGrant Likely {
515ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
516ca632f55SGrant Likely 
5174fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
518ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
519ca632f55SGrant Likely 		return 0;
520ca632f55SGrant Likely 
521c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
522ca632f55SGrant Likely 	drv_data->tx += n_bytes;
523ca632f55SGrant Likely 
524ca632f55SGrant Likely 	return 1;
525ca632f55SGrant Likely }
526ca632f55SGrant Likely 
527ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
528ca632f55SGrant Likely {
529ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
530ca632f55SGrant Likely 
5316d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
532c039dd27SJarkko Nikula 		pxa2xx_spi_read(drv_data, SSDR);
533ca632f55SGrant Likely 		drv_data->rx += n_bytes;
534ca632f55SGrant Likely 	}
535ca632f55SGrant Likely 
536ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
537ca632f55SGrant Likely }
538ca632f55SGrant Likely 
539ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data)
540ca632f55SGrant Likely {
5414fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
542ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
543ca632f55SGrant Likely 		return 0;
544ca632f55SGrant Likely 
545c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
546ca632f55SGrant Likely 	++drv_data->tx;
547ca632f55SGrant Likely 
548ca632f55SGrant Likely 	return 1;
549ca632f55SGrant Likely }
550ca632f55SGrant Likely 
551ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data)
552ca632f55SGrant Likely {
5536d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
554c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
555ca632f55SGrant Likely 		++drv_data->rx;
556ca632f55SGrant Likely 	}
557ca632f55SGrant Likely 
558ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
559ca632f55SGrant Likely }
560ca632f55SGrant Likely 
561ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
562ca632f55SGrant Likely {
5634fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
564ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
565ca632f55SGrant Likely 		return 0;
566ca632f55SGrant Likely 
567c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
568ca632f55SGrant Likely 	drv_data->tx += 2;
569ca632f55SGrant Likely 
570ca632f55SGrant Likely 	return 1;
571ca632f55SGrant Likely }
572ca632f55SGrant Likely 
573ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
574ca632f55SGrant Likely {
5756d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
576c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
577ca632f55SGrant Likely 		drv_data->rx += 2;
578ca632f55SGrant Likely 	}
579ca632f55SGrant Likely 
580ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
581ca632f55SGrant Likely }
582ca632f55SGrant Likely 
583ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
584ca632f55SGrant Likely {
5854fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
586ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
587ca632f55SGrant Likely 		return 0;
588ca632f55SGrant Likely 
589c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
590ca632f55SGrant Likely 	drv_data->tx += 4;
591ca632f55SGrant Likely 
592ca632f55SGrant Likely 	return 1;
593ca632f55SGrant Likely }
594ca632f55SGrant Likely 
595ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
596ca632f55SGrant Likely {
5976d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
598c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
599ca632f55SGrant Likely 		drv_data->rx += 4;
600ca632f55SGrant Likely 	}
601ca632f55SGrant Likely 
602ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
603ca632f55SGrant Likely }
604ca632f55SGrant Likely 
605ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
606ca632f55SGrant Likely {
60796579a4eSJarkko Nikula 	struct chip_data *chip =
60851eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
609ca632f55SGrant Likely 	u32 sccr1_reg;
610ca632f55SGrant Likely 
611c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
612152bc19eSAndy Shevchenko 	switch (drv_data->ssp_type) {
613152bc19eSAndy Shevchenko 	case QUARK_X1000_SSP:
614152bc19eSAndy Shevchenko 		sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
615152bc19eSAndy Shevchenko 		break;
6167c7289a4SAndy Shevchenko 	case CE4100_SSP:
6177c7289a4SAndy Shevchenko 		sccr1_reg &= ~CE4100_SSCR1_RFT;
6187c7289a4SAndy Shevchenko 		break;
619152bc19eSAndy Shevchenko 	default:
620ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_RFT;
621152bc19eSAndy Shevchenko 		break;
622152bc19eSAndy Shevchenko 	}
623ca632f55SGrant Likely 	sccr1_reg |= chip->threshold;
624c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
625ca632f55SGrant Likely }
626ca632f55SGrant Likely 
627ab77fe89SAndy Shevchenko static void int_stop_and_reset(struct driver_data *drv_data)
628ca632f55SGrant Likely {
629ab77fe89SAndy Shevchenko 	/* Clear and disable interrupts */
630ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
631ca632f55SGrant Likely 	reset_sccr1(drv_data);
632ab77fe89SAndy Shevchenko 	if (pxa25x_ssp_comp(drv_data))
633ab77fe89SAndy Shevchenko 		return;
634ab77fe89SAndy Shevchenko 
635c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSTO, 0);
636ab77fe89SAndy Shevchenko }
637ab77fe89SAndy Shevchenko 
6384761d2e7SAndy Shevchenko static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
639ab77fe89SAndy Shevchenko {
640ab77fe89SAndy Shevchenko 	int_stop_and_reset(drv_data);
641cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
64229d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
643ca632f55SGrant Likely 
644c3dce24cSAndy Shevchenko 	dev_err(drv_data->ssp->dev, "%s\n", msg);
645ca632f55SGrant Likely 
6464761d2e7SAndy Shevchenko 	drv_data->controller->cur_msg->status = err;
64751eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
648ca632f55SGrant Likely }
649ca632f55SGrant Likely 
650ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
651ca632f55SGrant Likely {
652ab77fe89SAndy Shevchenko 	int_stop_and_reset(drv_data);
653ca632f55SGrant Likely 
65451eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
655ca632f55SGrant Likely }
656ca632f55SGrant Likely 
657ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
658ca632f55SGrant Likely {
6596d380132SAndy Shevchenko 	u32 irq_status;
660ca632f55SGrant Likely 
6616d380132SAndy Shevchenko 	irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr);
6626d380132SAndy Shevchenko 	if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE))
6636d380132SAndy Shevchenko 		irq_status &= ~SSSR_TFS;
664ca632f55SGrant Likely 
665ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
6664761d2e7SAndy Shevchenko 		int_error_stop(drv_data, "interrupt_transfer: fifo overrun", -EIO);
667ca632f55SGrant Likely 		return IRQ_HANDLED;
668ca632f55SGrant Likely 	}
669ca632f55SGrant Likely 
670ec93cb6fSLubomir Rintel 	if (irq_status & SSSR_TUR) {
6714761d2e7SAndy Shevchenko 		int_error_stop(drv_data, "interrupt_transfer: fifo underrun", -EIO);
672ec93cb6fSLubomir Rintel 		return IRQ_HANDLED;
673ec93cb6fSLubomir Rintel 	}
674ec93cb6fSLubomir Rintel 
675ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
676c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
677ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
678ca632f55SGrant Likely 			int_transfer_complete(drv_data);
679ca632f55SGrant Likely 			return IRQ_HANDLED;
680ca632f55SGrant Likely 		}
681ca632f55SGrant Likely 	}
682ca632f55SGrant Likely 
683ca632f55SGrant Likely 	/* Drain rx fifo, Fill tx fifo and prevent overruns */
684ca632f55SGrant Likely 	do {
685ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
686ca632f55SGrant Likely 			int_transfer_complete(drv_data);
687ca632f55SGrant Likely 			return IRQ_HANDLED;
688ca632f55SGrant Likely 		}
689ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
690ca632f55SGrant Likely 
691ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
692ca632f55SGrant Likely 		int_transfer_complete(drv_data);
693ca632f55SGrant Likely 		return IRQ_HANDLED;
694ca632f55SGrant Likely 	}
695ca632f55SGrant Likely 
696ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
697ca632f55SGrant Likely 		u32 bytes_left;
698ca632f55SGrant Likely 		u32 sccr1_reg;
699ca632f55SGrant Likely 
700c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
701ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
702ca632f55SGrant Likely 
703ca632f55SGrant Likely 		/*
704ca632f55SGrant Likely 		 * PXA25x_SSP has no timeout, set up rx threshould for the
705ca632f55SGrant Likely 		 * remaining RX bytes.
706ca632f55SGrant Likely 		 */
707ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
7084fdb2424SWeike Chen 			u32 rx_thre;
709ca632f55SGrant Likely 
7104fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
711ca632f55SGrant Likely 
712ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
713ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
714ca632f55SGrant Likely 			case 4:
7152c183376SGustavo A. R. Silva 				bytes_left >>= 2;
7162c183376SGustavo A. R. Silva 				break;
717ca632f55SGrant Likely 			case 2:
718ca632f55SGrant Likely 				bytes_left >>= 1;
7192c183376SGustavo A. R. Silva 				break;
720ca632f55SGrant Likely 			}
721ca632f55SGrant Likely 
7224fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
7234fdb2424SWeike Chen 			if (rx_thre > bytes_left)
7244fdb2424SWeike Chen 				rx_thre = bytes_left;
725ca632f55SGrant Likely 
7264fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
727ca632f55SGrant Likely 		}
728c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
729ca632f55SGrant Likely 	}
730ca632f55SGrant Likely 
731ca632f55SGrant Likely 	/* We did something */
732ca632f55SGrant Likely 	return IRQ_HANDLED;
733ca632f55SGrant Likely }
734ca632f55SGrant Likely 
735b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data)
736b0312482SJan Kiszka {
73729d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
73842c80cd4SAndy Shevchenko 	clear_SSCR1_bits(drv_data, drv_data->int_cr1);
739b0312482SJan Kiszka 	if (!pxa25x_ssp_comp(drv_data))
740b0312482SJan Kiszka 		pxa2xx_spi_write(drv_data, SSTO, 0);
741b0312482SJan Kiszka 	write_SSSR_CS(drv_data, drv_data->clear_sr);
742b0312482SJan Kiszka 
743c3dce24cSAndy Shevchenko 	dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
744b0312482SJan Kiszka }
745b0312482SJan Kiszka 
746ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
747ca632f55SGrant Likely {
748ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
7497d94a505SMika Westerberg 	u32 sccr1_reg;
750ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
751ca632f55SGrant Likely 	u32 status;
752ca632f55SGrant Likely 
7537d94a505SMika Westerberg 	/*
7547d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
7557d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
7567d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
7577d94a505SMika Westerberg 	 * interrupt is enabled).
7587d94a505SMika Westerberg 	 */
759c3dce24cSAndy Shevchenko 	if (pm_runtime_suspended(drv_data->ssp->dev))
7607d94a505SMika Westerberg 		return IRQ_NONE;
7617d94a505SMika Westerberg 
762269e4a41SMika Westerberg 	/*
763269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
764269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
765269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
766269e4a41SMika Westerberg 	 * powered off.
767269e4a41SMika Westerberg 	 */
768c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
769269e4a41SMika Westerberg 	if (status == ~0)
770269e4a41SMika Westerberg 		return IRQ_NONE;
771269e4a41SMika Westerberg 
772c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
773ca632f55SGrant Likely 
774ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
775ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
776ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
777ca632f55SGrant Likely 
77802bc933eSTan, Jui Nee 	/* Ignore RX timeout interrupt if it is disabled */
77902bc933eSTan, Jui Nee 	if (!(sccr1_reg & SSCR1_TINTE))
78002bc933eSTan, Jui Nee 		mask &= ~SSSR_TINT;
78102bc933eSTan, Jui Nee 
782ca632f55SGrant Likely 	if (!(status & mask))
783ca632f55SGrant Likely 		return IRQ_NONE;
784ca632f55SGrant Likely 
785e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
786e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
787e51e9b93SJan Kiszka 
78851eea52dSLubomir Rintel 	if (!drv_data->controller->cur_msg) {
789b0312482SJan Kiszka 		handle_bad_msg(drv_data);
790ca632f55SGrant Likely 		/* Never fail */
791ca632f55SGrant Likely 		return IRQ_HANDLED;
792ca632f55SGrant Likely 	}
793ca632f55SGrant Likely 
794ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
795ca632f55SGrant Likely }
796ca632f55SGrant Likely 
797e5262d05SWeike Chen /*
7989df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
7999df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
8009df461ecSAndy Shevchenko  *
8019df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
8029df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
8039df461ecSAndy Shevchenko  *
8049df461ecSAndy Shevchenko  * Fsys = 200MHz
8059df461ecSAndy Shevchenko  *
8069df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
8079df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
8089df461ecSAndy Shevchenko  *
8099df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
8109df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
8119df461ecSAndy Shevchenko  *
8129df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
8139df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
8149df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
8159df461ecSAndy Shevchenko  *       k = [1, 256]
8169df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
8179df461ecSAndy Shevchenko  *
8189df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
8199df461ecSAndy Shevchenko  * are:
8209df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
8219df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
8229df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
8239df461ecSAndy Shevchenko  *
8249df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
8259df461ecSAndy Shevchenko  *
8269df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
8279df461ecSAndy Shevchenko  * to the asked baud rate.
828e5262d05SWeike Chen  */
8299df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
830e5262d05SWeike Chen {
8319df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
8329df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
8339df461ecSAndy Shevchenko 						   see (2) */
8349df461ecSAndy Shevchenko 						/* case 3 */
8359df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
8369df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
8379df461ecSAndy Shevchenko 	unsigned long scale;
8389df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
8399df461ecSAndy Shevchenko 	long r, r1, r2;
8409df461ecSAndy Shevchenko 	u32 mul;
841e5262d05SWeike Chen 
8429df461ecSAndy Shevchenko 	/* Case 1 */
8439df461ecSAndy Shevchenko 
8449df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
8459df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
8469df461ecSAndy Shevchenko 
8479df461ecSAndy Shevchenko 	/* Calculate initial quot */
8483ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
8499df461ecSAndy Shevchenko 
8509df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
8519df461ecSAndy Shevchenko 	if (q1 > 256) {
8529df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
8539df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
8549df461ecSAndy Shevchenko 		if (scale > 9) {
8559df461ecSAndy Shevchenko 			q1 >>= scale - 9;
8569df461ecSAndy Shevchenko 			mul >>= scale - 9;
8579df461ecSAndy Shevchenko 		}
8589df461ecSAndy Shevchenko 
8599df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
8609df461ecSAndy Shevchenko 		q1 += q1 & 1;
8619df461ecSAndy Shevchenko 	}
8629df461ecSAndy Shevchenko 
8639df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
8649df461ecSAndy Shevchenko 	scale = __ffs(q1);
8659df461ecSAndy Shevchenko 	q1 >>= scale;
8669df461ecSAndy Shevchenko 	mul >>= scale;
8679df461ecSAndy Shevchenko 
8689df461ecSAndy Shevchenko 	/* Get the remainder */
8699df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
8709df461ecSAndy Shevchenko 
8719df461ecSAndy Shevchenko 	/* Case 2 */
8729df461ecSAndy Shevchenko 
8733ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
8749df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
8759df461ecSAndy Shevchenko 
8769df461ecSAndy Shevchenko 	/*
8779df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
8789df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
8799df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
8809df461ecSAndy Shevchenko 	 */
8819df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
8829df461ecSAndy Shevchenko 		/* case 1 is better */
8839df461ecSAndy Shevchenko 		r = r1;
8849df461ecSAndy Shevchenko 		q = q1;
8859df461ecSAndy Shevchenko 	} else {
8869df461ecSAndy Shevchenko 		/* case 2 is better */
8879df461ecSAndy Shevchenko 		r = r2;
8889df461ecSAndy Shevchenko 		q = q2;
8899df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
8909df461ecSAndy Shevchenko 	}
8919df461ecSAndy Shevchenko 
8923ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
8939df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
8949df461ecSAndy Shevchenko 		u64 fssp;
8959df461ecSAndy Shevchenko 		u32 m;
8969df461ecSAndy Shevchenko 
8979df461ecSAndy Shevchenko 		/* Calculate initial quot */
8983ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
8999df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
9009df461ecSAndy Shevchenko 
9019df461ecSAndy Shevchenko 		/* Get the remainder */
9029df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
9039df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
9049df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
9059df461ecSAndy Shevchenko 
9069df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
9079df461ecSAndy Shevchenko 		if (r1 < r) {
9089df461ecSAndy Shevchenko 			/* case 3 is better */
9099df461ecSAndy Shevchenko 			q = 1;
9109df461ecSAndy Shevchenko 			mul = m;
911e5262d05SWeike Chen 		}
912e5262d05SWeike Chen 	}
913e5262d05SWeike Chen 
9149df461ecSAndy Shevchenko 	*dds = mul;
9159df461ecSAndy Shevchenko 	return q - 1;
916e5262d05SWeike Chen }
917e5262d05SWeike Chen 
9183343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
919ca632f55SGrant Likely {
92051eea52dSLubomir Rintel 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
9213343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
9223343b7a6SMika Westerberg 
9233343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
924ca632f55SGrant Likely 
92529f21337SFlavio Suligoi 	/*
92629f21337SFlavio Suligoi 	 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
92729f21337SFlavio Suligoi 	 * that the SSP transmission rate can be greater than the device rate
92829f21337SFlavio Suligoi 	 */
929ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
93029f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
931ca632f55SGrant Likely 	else
93229f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
933ca632f55SGrant Likely }
934ca632f55SGrant Likely 
935e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
936d2c2f6a4SAndy Shevchenko 					   int rate)
937e5262d05SWeike Chen {
93896579a4eSJarkko Nikula 	struct chip_data *chip =
93951eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
940025ffe88SAndy Shevchenko 	unsigned int clk_div;
941e5262d05SWeike Chen 
942e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
943e5262d05SWeike Chen 	case QUARK_X1000_SSP:
9449df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
945eecacf73SDan Carpenter 		break;
946e5262d05SWeike Chen 	default:
947025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
948eecacf73SDan Carpenter 		break;
949e5262d05SWeike Chen 	}
950025ffe88SAndy Shevchenko 	return clk_div << 8;
951e5262d05SWeike Chen }
952e5262d05SWeike Chen 
95351eea52dSLubomir Rintel static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
954b6ced294SJarkko Nikula 			       struct spi_device *spi,
955b6ced294SJarkko Nikula 			       struct spi_transfer *xfer)
956b6ced294SJarkko Nikula {
957b6ced294SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
958b6ced294SJarkko Nikula 
959b6ced294SJarkko Nikula 	return chip->enable_dma &&
960b6ced294SJarkko Nikula 	       xfer->len <= MAX_DMA_LEN &&
961b6ced294SJarkko Nikula 	       xfer->len >= chip->dma_burst_size;
962b6ced294SJarkko Nikula }
963b6ced294SJarkko Nikula 
96451eea52dSLubomir Rintel static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
965d5898e19SJarkko Nikula 				   struct spi_device *spi,
966d5898e19SJarkko Nikula 				   struct spi_transfer *transfer)
967ca632f55SGrant Likely {
96851eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
96951eea52dSLubomir Rintel 	struct spi_message *message = controller->cur_msg;
97020f4c379SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
97196579a4eSJarkko Nikula 	u32 dma_thresh = chip->dma_threshold;
97296579a4eSJarkko Nikula 	u32 dma_burst = chip->dma_burst_size;
97396579a4eSJarkko Nikula 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
974bffc967eSJarkko Nikula 	u32 clk_div;
975bffc967eSJarkko Nikula 	u8 bits;
976bffc967eSJarkko Nikula 	u32 speed;
977ca632f55SGrant Likely 	u32 cr0;
978ca632f55SGrant Likely 	u32 cr1;
9797d1f1bf6SAndy Shevchenko 	int err;
980b6ced294SJarkko Nikula 	int dma_mapped;
981ca632f55SGrant Likely 
982cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
983b6ced294SJarkko Nikula 	if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
984ca632f55SGrant Likely 
985ca632f55SGrant Likely 		/* reject already-mapped transfers; PIO won't always work */
986ca632f55SGrant Likely 		if (message->is_dma_mapped
987ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
988748fbadfSJarkko Nikula 			dev_err(&spi->dev,
9898ae55af3SJarkko Nikula 				"Mapped transfer length of %u is greater than %d\n",
990ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
991d5898e19SJarkko Nikula 			return -EINVAL;
992ca632f55SGrant Likely 		}
993ca632f55SGrant Likely 
994ca632f55SGrant Likely 		/* warn ... we force this to PIO mode */
99520f4c379SJarkko Nikula 		dev_warn_ratelimited(&spi->dev,
9968ae55af3SJarkko Nikula 				     "DMA disabled for transfer length %ld greater than %d\n",
997d5898e19SJarkko Nikula 				     (long)transfer->len, MAX_DMA_LEN);
998ca632f55SGrant Likely 	}
999ca632f55SGrant Likely 
1000ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
1001cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
1002748fbadfSJarkko Nikula 		dev_err(&spi->dev, "Flush failed\n");
1003d5898e19SJarkko Nikula 		return -EIO;
1004ca632f55SGrant Likely 	}
1005ca632f55SGrant Likely 	drv_data->n_bytes = chip->n_bytes;
1006ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
1007ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
1008ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
1009ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
1010ca632f55SGrant Likely 	drv_data->write = drv_data->tx ? chip->write : null_writer;
1011ca632f55SGrant Likely 	drv_data->read = drv_data->rx ? chip->read : null_reader;
1012ca632f55SGrant Likely 
1013ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
1014ca632f55SGrant Likely 	bits = transfer->bits_per_word;
1015ca632f55SGrant Likely 	speed = transfer->speed_hz;
1016ca632f55SGrant Likely 
1017d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
1018ca632f55SGrant Likely 
1019ca632f55SGrant Likely 	if (bits <= 8) {
1020ca632f55SGrant Likely 		drv_data->n_bytes = 1;
1021ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1022ca632f55SGrant Likely 					u8_reader : null_reader;
1023ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1024ca632f55SGrant Likely 					u8_writer : null_writer;
1025ca632f55SGrant Likely 	} else if (bits <= 16) {
1026ca632f55SGrant Likely 		drv_data->n_bytes = 2;
1027ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1028ca632f55SGrant Likely 					u16_reader : null_reader;
1029ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1030ca632f55SGrant Likely 					u16_writer : null_writer;
1031ca632f55SGrant Likely 	} else if (bits <= 32) {
1032ca632f55SGrant Likely 		drv_data->n_bytes = 4;
1033ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1034ca632f55SGrant Likely 					u32_reader : null_reader;
1035ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1036ca632f55SGrant Likely 					u32_writer : null_writer;
1037ca632f55SGrant Likely 	}
1038196b0e2cSJarkko Nikula 	/*
1039196b0e2cSJarkko Nikula 	 * if bits/word is changed in dma mode, then must check the
1040196b0e2cSJarkko Nikula 	 * thresholds and burst also
1041196b0e2cSJarkko Nikula 	 */
1042ca632f55SGrant Likely 	if (chip->enable_dma) {
1043cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
104420f4c379SJarkko Nikula 						spi,
1045ca632f55SGrant Likely 						bits, &dma_burst,
1046ca632f55SGrant Likely 						&dma_thresh))
104720f4c379SJarkko Nikula 			dev_warn_ratelimited(&spi->dev,
10488ae55af3SJarkko Nikula 					     "DMA burst size reduced to match bits_per_word\n");
1049ca632f55SGrant Likely 	}
1050ca632f55SGrant Likely 
105151eea52dSLubomir Rintel 	dma_mapped = controller->can_dma &&
105220f4c379SJarkko Nikula 		     controller->can_dma(controller, spi, transfer) &&
105351eea52dSLubomir Rintel 		     controller->cur_msg_mapped;
1054b6ced294SJarkko Nikula 	if (dma_mapped) {
1055ca632f55SGrant Likely 
1056ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
1057cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1058ca632f55SGrant Likely 
1059d5898e19SJarkko Nikula 		err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1060d5898e19SJarkko Nikula 		if (err)
1061d5898e19SJarkko Nikula 			return err;
1062ca632f55SGrant Likely 
1063ca632f55SGrant Likely 		/* Clear status and start DMA engine */
1064ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1065c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1066cd7bed00SMika Westerberg 
1067cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
1068ca632f55SGrant Likely 	} else {
1069ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
1070ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
1071ca632f55SGrant Likely 
1072ca632f55SGrant Likely 		/* Clear status  */
1073ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1074ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1075ca632f55SGrant Likely 	}
1076ca632f55SGrant Likely 
1077ee03672dSJarkko Nikula 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1078ee03672dSJarkko Nikula 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1079ee03672dSJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
108020f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
108151eea52dSLubomir Rintel 			controller->max_speed_hz
1082ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1083b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1084ee03672dSJarkko Nikula 	else
108520f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
108651eea52dSLubomir Rintel 			controller->max_speed_hz / 2
1087ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1088b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1089ee03672dSJarkko Nikula 
1090a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
10911bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold);
10921bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold);
1093a0d2642eSMika Westerberg 	}
1094a0d2642eSMika Westerberg 
10953fdb59cfSAndy Shevchenko 	if (is_mrfld_ssp(drv_data)) {
10963fdb59cfSAndy Shevchenko 		u32 thresh = 0;
10973fdb59cfSAndy Shevchenko 
10983fdb59cfSAndy Shevchenko 		thresh |= SFIFOTT_RxThresh(chip->lpss_rx_threshold);
10993fdb59cfSAndy Shevchenko 		thresh |= SFIFOTT_TxThresh(chip->lpss_tx_threshold);
11003fdb59cfSAndy Shevchenko 
11013fdb59cfSAndy Shevchenko 		pxa2xx_spi_update(drv_data, SFIFOTT, 0xffffffff, thresh);
11023fdb59cfSAndy Shevchenko 	}
11033fdb59cfSAndy Shevchenko 
11041bed378cSAndy Shevchenko 	if (is_quark_x1000_ssp(drv_data))
11051bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate);
1106e5262d05SWeike Chen 
11070c8ccd8bSAndy Shevchenko 	/* Stop the SSP */
11080c8ccd8bSAndy Shevchenko 	if (!is_mmp2_ssp(drv_data))
11090c8ccd8bSAndy Shevchenko 		pxa_ssp_disable(drv_data->ssp);
11100c8ccd8bSAndy Shevchenko 
11110c8ccd8bSAndy Shevchenko 	if (!pxa25x_ssp_comp(drv_data))
11120c8ccd8bSAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
11130c8ccd8bSAndy Shevchenko 
1114ca632f55SGrant Likely 	/* first set CR1 without interrupt and service enables */
11151bed378cSAndy Shevchenko 	pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1);
11161bed378cSAndy Shevchenko 
11171bed378cSAndy Shevchenko 	/* see if we need to reload the config registers */
11181bed378cSAndy Shevchenko 	pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0);
1119ca632f55SGrant Likely 
11200c8ccd8bSAndy Shevchenko 	/* Restart the SSP */
11210c8ccd8bSAndy Shevchenko 	pxa_ssp_enable(drv_data->ssp);
11220c8ccd8bSAndy Shevchenko 
112341c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data)) {
11246d380132SAndy Shevchenko 		u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8;
112582391856SLubomir Rintel 
112682391856SLubomir Rintel 		if (tx_level) {
112782391856SLubomir Rintel 			/* On MMP2, flipping SSE doesn't to empty TXFIFO. */
112882391856SLubomir Rintel 			dev_warn(&spi->dev, "%d bytes of garbage in TXFIFO!\n",
112982391856SLubomir Rintel 								tx_level);
113082391856SLubomir Rintel 			if (tx_level > transfer->len)
113182391856SLubomir Rintel 				tx_level = transfer->len;
113282391856SLubomir Rintel 			drv_data->tx += tx_level;
113382391856SLubomir Rintel 		}
113482391856SLubomir Rintel 	}
113582391856SLubomir Rintel 
113651eea52dSLubomir Rintel 	if (spi_controller_is_slave(controller)) {
1137ec93cb6fSLubomir Rintel 		while (drv_data->write(drv_data))
1138ec93cb6fSLubomir Rintel 			;
113977d33897SLubomir Rintel 		if (drv_data->gpiod_ready) {
114077d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 1);
114177d33897SLubomir Rintel 			udelay(1);
114277d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 0);
114377d33897SLubomir Rintel 		}
1144ec93cb6fSLubomir Rintel 	}
1145ec93cb6fSLubomir Rintel 
1146d5898e19SJarkko Nikula 	/*
1147d5898e19SJarkko Nikula 	 * Release the data by enabling service requests and interrupts,
1148d5898e19SJarkko Nikula 	 * without changing any mode bits
1149d5898e19SJarkko Nikula 	 */
1150c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1151d5898e19SJarkko Nikula 
1152d5898e19SJarkko Nikula 	return 1;
1153ca632f55SGrant Likely }
1154ca632f55SGrant Likely 
115551eea52dSLubomir Rintel static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
1156ec93cb6fSLubomir Rintel {
115751eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1158ec93cb6fSLubomir Rintel 
11594761d2e7SAndy Shevchenko 	int_error_stop(drv_data, "transfer aborted", -EINTR);
1160ec93cb6fSLubomir Rintel 
1161ec93cb6fSLubomir Rintel 	return 0;
1162ec93cb6fSLubomir Rintel }
1163ec93cb6fSLubomir Rintel 
116451eea52dSLubomir Rintel static void pxa2xx_spi_handle_err(struct spi_controller *controller,
11657f86bde9SMika Westerberg 				 struct spi_message *msg)
1166ca632f55SGrant Likely {
116751eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1168ca632f55SGrant Likely 
1169d5898e19SJarkko Nikula 	/* Disable the SSP */
117029d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
1171d5898e19SJarkko Nikula 	/* Clear and disable interrupts and service requests */
1172d5898e19SJarkko Nikula 	write_SSSR_CS(drv_data, drv_data->clear_sr);
117342c80cd4SAndy Shevchenko 	clear_SSCR1_bits(drv_data, drv_data->int_cr1 | drv_data->dma_cr1);
1174d5898e19SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
1175d5898e19SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1176ca632f55SGrant Likely 
1177d5898e19SJarkko Nikula 	/*
1178d5898e19SJarkko Nikula 	 * Stop the DMA if running. Note DMA callback handler may have unset
1179d5898e19SJarkko Nikula 	 * the dma_running already, which is fine as stopping is not needed
1180d5898e19SJarkko Nikula 	 * then but we shouldn't rely this flag for anything else than
1181d5898e19SJarkko Nikula 	 * stopping. For instance to differentiate between PIO and DMA
1182d5898e19SJarkko Nikula 	 * transfers.
1183d5898e19SJarkko Nikula 	 */
1184d5898e19SJarkko Nikula 	if (atomic_read(&drv_data->dma_running))
1185d5898e19SJarkko Nikula 		pxa2xx_spi_dma_stop(drv_data);
1186ca632f55SGrant Likely }
1187ca632f55SGrant Likely 
118851eea52dSLubomir Rintel static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
11897d94a505SMika Westerberg {
119051eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
11917d94a505SMika Westerberg 
11927d94a505SMika Westerberg 	/* Disable the SSP now */
119329d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
11947d94a505SMika Westerberg 
11957d94a505SMika Westerberg 	return 0;
11967d94a505SMika Westerberg }
11977d94a505SMika Westerberg 
1198ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1199ca632f55SGrant Likely 		    struct pxa2xx_spi_chip *chip_info)
1200ca632f55SGrant Likely {
1201c18d925fSJan Kiszka 	struct gpio_desc *gpiod;
1202ca632f55SGrant Likely 	int err = 0;
1203ca632f55SGrant Likely 
120499f499cdSMika Westerberg 	if (chip == NULL)
120599f499cdSMika Westerberg 		return 0;
120699f499cdSMika Westerberg 
120799f499cdSMika Westerberg 	if (chip_info == NULL)
1208ca632f55SGrant Likely 		return 0;
1209ca632f55SGrant Likely 
1210ca632f55SGrant Likely 	/* NOTE: setup() can be called multiple times, possibly with
1211ca632f55SGrant Likely 	 * different chip_info, release previously requested GPIO
1212ca632f55SGrant Likely 	 */
1213c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
1214a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1215c18d925fSJan Kiszka 		chip->gpiod_cs = NULL;
1216c18d925fSJan Kiszka 	}
1217ca632f55SGrant Likely 
1218ca632f55SGrant Likely 	/* If (*cs_control) is provided, ignore GPIO chip select */
1219ca632f55SGrant Likely 	if (chip_info->cs_control) {
1220ca632f55SGrant Likely 		chip->cs_control = chip_info->cs_control;
1221ca632f55SGrant Likely 		return 0;
1222ca632f55SGrant Likely 	}
1223ca632f55SGrant Likely 
1224ca632f55SGrant Likely 	if (gpio_is_valid(chip_info->gpio_cs)) {
1225ca632f55SGrant Likely 		err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1226ca632f55SGrant Likely 		if (err) {
1227f6bd03a7SJarkko Nikula 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1228f6bd03a7SJarkko Nikula 				chip_info->gpio_cs);
1229ca632f55SGrant Likely 			return err;
1230ca632f55SGrant Likely 		}
1231ca632f55SGrant Likely 
1232c18d925fSJan Kiszka 		gpiod = gpio_to_desc(chip_info->gpio_cs);
1233c18d925fSJan Kiszka 		chip->gpiod_cs = gpiod;
1234ca632f55SGrant Likely 		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1235ca632f55SGrant Likely 
1236c18d925fSJan Kiszka 		err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
1237ca632f55SGrant Likely 	}
1238ca632f55SGrant Likely 
1239ca632f55SGrant Likely 	return err;
1240ca632f55SGrant Likely }
1241ca632f55SGrant Likely 
1242ca632f55SGrant Likely static int setup(struct spi_device *spi)
1243ca632f55SGrant Likely {
1244bffc967eSJarkko Nikula 	struct pxa2xx_spi_chip *chip_info;
1245ca632f55SGrant Likely 	struct chip_data *chip;
1246dccf7369SJarkko Nikula 	const struct lpss_config *config;
12473cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12483cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1249a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1250a0d2642eSMika Westerberg 
1251e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1252e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1253e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1254e5262d05SWeike Chen 		tx_hi_thres = 0;
1255e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1256e5262d05SWeike Chen 		break;
12573fdb59cfSAndy Shevchenko 	case MRFLD_SSP:
12583fdb59cfSAndy Shevchenko 		tx_thres = TX_THRESH_MRFLD_DFLT;
12593fdb59cfSAndy Shevchenko 		tx_hi_thres = 0;
12603fdb59cfSAndy Shevchenko 		rx_thres = RX_THRESH_MRFLD_DFLT;
12613fdb59cfSAndy Shevchenko 		break;
12627c7289a4SAndy Shevchenko 	case CE4100_SSP:
12637c7289a4SAndy Shevchenko 		tx_thres = TX_THRESH_CE4100_DFLT;
12647c7289a4SAndy Shevchenko 		tx_hi_thres = 0;
12657c7289a4SAndy Shevchenko 		rx_thres = RX_THRESH_CE4100_DFLT;
12667c7289a4SAndy Shevchenko 		break;
126703fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
126803fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
126930f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
127034cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1271b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1272fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
1273dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1274dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1275dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1276dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1277e5262d05SWeike Chen 		break;
1278e5262d05SWeike Chen 	default:
1279a0d2642eSMika Westerberg 		tx_hi_thres = 0;
128051eea52dSLubomir Rintel 		if (spi_controller_is_slave(drv_data->controller)) {
1281ec93cb6fSLubomir Rintel 			tx_thres = 1;
1282ec93cb6fSLubomir Rintel 			rx_thres = 2;
1283ec93cb6fSLubomir Rintel 		} else {
1284ec93cb6fSLubomir Rintel 			tx_thres = TX_THRESH_DFLT;
1285a0d2642eSMika Westerberg 			rx_thres = RX_THRESH_DFLT;
1286ec93cb6fSLubomir Rintel 		}
1287e5262d05SWeike Chen 		break;
1288a0d2642eSMika Westerberg 	}
1289ca632f55SGrant Likely 
1290ca632f55SGrant Likely 	/* Only alloc on first setup */
1291ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1292ca632f55SGrant Likely 	if (!chip) {
1293ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
12949deae459SJingoo Han 		if (!chip)
1295ca632f55SGrant Likely 			return -ENOMEM;
1296ca632f55SGrant Likely 
1297ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1298ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1299f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1300f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1301ca632f55SGrant Likely 				kfree(chip);
1302ca632f55SGrant Likely 				return -EINVAL;
1303ca632f55SGrant Likely 			}
1304ca632f55SGrant Likely 
1305ca632f55SGrant Likely 			chip->frm = spi->chip_select;
1306c18d925fSJan Kiszka 		}
130751eea52dSLubomir Rintel 		chip->enable_dma = drv_data->controller_info->enable_dma;
1308ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1309ca632f55SGrant Likely 	}
1310ca632f55SGrant Likely 
1311ca632f55SGrant Likely 	/* protocol drivers may change the chip settings, so...
1312ca632f55SGrant Likely 	 * if chip_info exists, use it */
1313ca632f55SGrant Likely 	chip_info = spi->controller_data;
1314ca632f55SGrant Likely 
1315ca632f55SGrant Likely 	/* chip_info isn't always needed */
1316ca632f55SGrant Likely 	chip->cr1 = 0;
1317ca632f55SGrant Likely 	if (chip_info) {
1318ca632f55SGrant Likely 		if (chip_info->timeout)
1319ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1320ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1321ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1322a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1323a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1324ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1325ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1326ca632f55SGrant Likely 		chip->dma_threshold = 0;
1327ca632f55SGrant Likely 		if (chip_info->enable_loopback)
1328ca632f55SGrant Likely 			chip->cr1 = SSCR1_LBM;
1329ca632f55SGrant Likely 	}
133051eea52dSLubomir Rintel 	if (spi_controller_is_slave(drv_data->controller)) {
1331ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCFR;
1332ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCLKDIR;
1333ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SFRMDIR;
1334ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SPH;
1335ec93cb6fSLubomir Rintel 	}
1336ca632f55SGrant Likely 
13373fdb59cfSAndy Shevchenko 	if (is_lpss_ssp(drv_data)) {
1338a0d2642eSMika Westerberg 		chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
13393fdb59cfSAndy Shevchenko 		chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
13403fdb59cfSAndy Shevchenko 					  SSITF_TxHiThresh(tx_hi_thres);
13413fdb59cfSAndy Shevchenko 	}
13423fdb59cfSAndy Shevchenko 
13433fdb59cfSAndy Shevchenko 	if (is_mrfld_ssp(drv_data)) {
13443fdb59cfSAndy Shevchenko 		chip->lpss_rx_threshold = rx_thres;
13453fdb59cfSAndy Shevchenko 		chip->lpss_tx_threshold = tx_thres;
13463fdb59cfSAndy Shevchenko 	}
1347a0d2642eSMika Westerberg 
1348ca632f55SGrant Likely 	/* set dma burst and threshold outside of chip_info path so that if
1349ca632f55SGrant Likely 	 * chip_info goes away after setting chip->enable_dma, the
1350ca632f55SGrant Likely 	 * burst and threshold can still respond to changes in bits_per_word */
1351ca632f55SGrant Likely 	if (chip->enable_dma) {
1352ca632f55SGrant Likely 		/* set up legal burst and threshold for dma */
1353cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1354cd7bed00SMika Westerberg 						spi->bits_per_word,
1355ca632f55SGrant Likely 						&chip->dma_burst_size,
1356ca632f55SGrant Likely 						&chip->dma_threshold)) {
1357f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1358f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1359ca632f55SGrant Likely 		}
1360000c6af4SAndy Shevchenko 		dev_dbg(&spi->dev,
1361000c6af4SAndy Shevchenko 			"in setup: DMA burst size set to %u\n",
1362000c6af4SAndy Shevchenko 			chip->dma_burst_size);
1363ca632f55SGrant Likely 	}
1364ca632f55SGrant Likely 
1365e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1366e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1367e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1368e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1369e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1370e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1371e5262d05SWeike Chen 		break;
13727c7289a4SAndy Shevchenko 	case CE4100_SSP:
13737c7289a4SAndy Shevchenko 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
13747c7289a4SAndy Shevchenko 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
13757c7289a4SAndy Shevchenko 		break;
1376e5262d05SWeike Chen 	default:
1377e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1378e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1379e5262d05SWeike Chen 		break;
1380e5262d05SWeike Chen 	}
1381e5262d05SWeike Chen 
1382ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1383ca632f55SGrant Likely 	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1384ca632f55SGrant Likely 			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1385ca632f55SGrant Likely 
1386b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1387b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1388b833172fSMika Westerberg 
1389ca632f55SGrant Likely 	if (spi->bits_per_word <= 8) {
1390ca632f55SGrant Likely 		chip->n_bytes = 1;
1391ca632f55SGrant Likely 		chip->read = u8_reader;
1392ca632f55SGrant Likely 		chip->write = u8_writer;
1393ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 16) {
1394ca632f55SGrant Likely 		chip->n_bytes = 2;
1395ca632f55SGrant Likely 		chip->read = u16_reader;
1396ca632f55SGrant Likely 		chip->write = u16_writer;
1397ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 32) {
1398ca632f55SGrant Likely 		chip->n_bytes = 4;
1399ca632f55SGrant Likely 		chip->read = u32_reader;
1400ca632f55SGrant Likely 		chip->write = u32_writer;
1401ca632f55SGrant Likely 	}
1402ca632f55SGrant Likely 
1403ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1404ca632f55SGrant Likely 
1405ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
1406ca632f55SGrant Likely 		return 0;
1407ca632f55SGrant Likely 
1408ca632f55SGrant Likely 	return setup_cs(spi, chip, chip_info);
1409ca632f55SGrant Likely }
1410ca632f55SGrant Likely 
1411ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1412ca632f55SGrant Likely {
1413ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
14143cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
14153cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1416ca632f55SGrant Likely 
1417ca632f55SGrant Likely 	if (!chip)
1418ca632f55SGrant Likely 		return;
1419ca632f55SGrant Likely 
1420*778c12e6SAndy Shevchenko 	if (drv_data->ssp_type != CE4100_SSP && chip->gpiod_cs)
1421a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1422ca632f55SGrant Likely 
1423ca632f55SGrant Likely 	kfree(chip);
1424ca632f55SGrant Likely }
1425ca632f55SGrant Likely 
14269b2d6119SLee Jones #ifdef CONFIG_ACPI
14278422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
142803fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
142903fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
143003fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
143103fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
143203fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
143330f3a6abSMika Westerberg 	{ "8086228E", LPSS_BSW_SSP },
143403fbf488SJarkko Nikula 	{ },
143503fbf488SJarkko Nikula };
143603fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
14379b2d6119SLee Jones #endif
143803fbf488SJarkko Nikula 
143934cadd9cSJarkko Nikula /*
144034cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
144134cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
144234cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
144334cadd9cSJarkko Nikula  */
144434cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
144534cadd9cSJarkko Nikula 	/* SPT-LP */
144634cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
144734cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
144834cadd9cSJarkko Nikula 	/* SPT-H */
144934cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
145034cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1451704d2b07SMika Westerberg 	/* KBL-H */
1452704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1453704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
14546157d4c2SJarkko Nikula 	/* CML-V */
14556157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
14566157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
1457c1b03f11SJarkko Nikula 	/* BXT A-Step */
1458b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1459b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1460b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1461c1b03f11SJarkko Nikula 	/* BXT B-Step */
1462c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1463c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1464c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1465e18a80acSDavid E. Box 	/* GLK */
1466e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1467e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1468e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
146922d71a50SMika Westerberg 	/* ICL-LP */
147022d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
147122d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
147222d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
14738cc77204SJarkko Nikula 	/* EHL */
14748cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
14758cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
14768cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
14779c7315c9SJarkko Nikula 	/* JSL */
14789c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
14799c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
14809c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
1481cf961fceSJarkko Nikula 	/* TGL-H */
1482cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
1483cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
1484cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
1485cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
1486a402e397SJarkko Nikula 	/* ADL-P */
1487a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51aa), LPSS_CNL_SSP },
1488a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51ab), LPSS_CNL_SSP },
1489a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51fb), LPSS_CNL_SSP },
14908c4ffe4dSJarkko Nikula 	/* ADL-M */
14918c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54aa), LPSS_CNL_SSP },
14928c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54ab), LPSS_CNL_SSP },
14938c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54fb), LPSS_CNL_SSP },
1494b7c08cf8SJarkko Nikula 	/* APL */
1495b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1496b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1497b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1498b8450e01SJarkko Nikula 	/* ADL-S */
1499b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7aaa), LPSS_CNL_SSP },
1500b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7aab), LPSS_CNL_SSP },
1501b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7af9), LPSS_CNL_SSP },
1502b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7afb), LPSS_CNL_SSP },
1503fc0b2accSJarkko Nikula 	/* CNL-LP */
1504fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1505fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1506fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1507fc0b2accSJarkko Nikula 	/* CNL-H */
1508fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1509fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1510fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
151141a91802SEvan Green 	/* CML-LP */
151241a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
151341a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
151441a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
1515f0cf17edSJarkko Nikula 	/* CML-H */
1516f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1517f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1518f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
1519a4127952SJarkko Nikula 	/* TGL-LP */
1520a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1521a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1522a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1523a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1524a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1525a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1526a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
152794e5c23dSAxel Lin 	{ },
152834cadd9cSJarkko Nikula };
152934cadd9cSJarkko Nikula 
153087ae1d2dSLubomir Rintel static const struct of_device_id pxa2xx_spi_of_match[] = {
153187ae1d2dSLubomir Rintel 	{ .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
153287ae1d2dSLubomir Rintel 	{},
153387ae1d2dSLubomir Rintel };
153487ae1d2dSLubomir Rintel MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
153587ae1d2dSLubomir Rintel 
153687ae1d2dSLubomir Rintel #ifdef CONFIG_ACPI
153787ae1d2dSLubomir Rintel 
1538365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
153987ae1d2dSLubomir Rintel {
1540365e856eSAndy Shevchenko 	struct acpi_device *adev;
154187ae1d2dSLubomir Rintel 	unsigned int devid;
154287ae1d2dSLubomir Rintel 	int port_id = -1;
154387ae1d2dSLubomir Rintel 
1544365e856eSAndy Shevchenko 	adev = ACPI_COMPANION(dev);
154587ae1d2dSLubomir Rintel 	if (adev && adev->pnp.unique_id &&
154687ae1d2dSLubomir Rintel 	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
154787ae1d2dSLubomir Rintel 		port_id = devid;
154887ae1d2dSLubomir Rintel 	return port_id;
154987ae1d2dSLubomir Rintel }
155087ae1d2dSLubomir Rintel 
155187ae1d2dSLubomir Rintel #else /* !CONFIG_ACPI */
155287ae1d2dSLubomir Rintel 
1553365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
155487ae1d2dSLubomir Rintel {
155587ae1d2dSLubomir Rintel 	return -1;
155687ae1d2dSLubomir Rintel }
155787ae1d2dSLubomir Rintel 
155887ae1d2dSLubomir Rintel #endif /* CONFIG_ACPI */
155987ae1d2dSLubomir Rintel 
156087ae1d2dSLubomir Rintel 
156187ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
156287ae1d2dSLubomir Rintel 
156334cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
156434cadd9cSJarkko Nikula {
15655ba846b1SAndy Shevchenko 	return param == chan->device->dev;
156634cadd9cSJarkko Nikula }
156734cadd9cSJarkko Nikula 
156887ae1d2dSLubomir Rintel #endif /* CONFIG_PCI */
156987ae1d2dSLubomir Rintel 
157051eea52dSLubomir Rintel static struct pxa2xx_spi_controller *
15710db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1572a3496855SMika Westerberg {
157351eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *pdata;
1574a3496855SMika Westerberg 	struct ssp_device *ssp;
1575a3496855SMika Westerberg 	struct resource *res;
15766fb7427dSAndy Shevchenko 	struct device *parent = pdev->dev.parent;
15776fb7427dSAndy Shevchenko 	struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
157834cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
157955ef8262SLubomir Rintel 	enum pxa_ssp_type type;
1580f2faa3ecSAndy Shevchenko 	const void *match;
1581a3496855SMika Westerberg 
15826fb7427dSAndy Shevchenko 	if (pcidev)
15836fb7427dSAndy Shevchenko 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
1584a3496855SMika Westerberg 
1585f2faa3ecSAndy Shevchenko 	match = device_get_match_data(&pdev->dev);
1586f2faa3ecSAndy Shevchenko 	if (match)
1587f2faa3ecSAndy Shevchenko 		type = (enum pxa_ssp_type)match;
158834cadd9cSJarkko Nikula 	else if (pcidev_id)
158955ef8262SLubomir Rintel 		type = (enum pxa_ssp_type)pcidev_id->driver_data;
159003fbf488SJarkko Nikula 	else
159114af1df3SAndy Shevchenko 		return ERR_PTR(-EINVAL);
159203fbf488SJarkko Nikula 
1593cc0ee987SMika Westerberg 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
15949deae459SJingoo Han 	if (!pdata)
159514af1df3SAndy Shevchenko 		return ERR_PTR(-ENOMEM);
1596a3496855SMika Westerberg 
1597a3496855SMika Westerberg 	ssp = &pdata->ssp;
1598a3496855SMika Westerberg 
159977c544d2SAndy Shevchenko 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1600cbfd6a21SSachin Kamat 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1601cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
160214af1df3SAndy Shevchenko 		return ERR_CAST(ssp->mmio_base);
1603a3496855SMika Westerberg 
160477c544d2SAndy Shevchenko 	ssp->phys_base = res->start;
160577c544d2SAndy Shevchenko 
160687ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
160734cadd9cSJarkko Nikula 	if (pcidev_id) {
16086fb7427dSAndy Shevchenko 		pdata->tx_param = parent;
16096fb7427dSAndy Shevchenko 		pdata->rx_param = parent;
161034cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
161134cadd9cSJarkko Nikula 	}
161287ae1d2dSLubomir Rintel #endif
161334cadd9cSJarkko Nikula 
1614a3496855SMika Westerberg 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
16155eb263efSChuhong Yuan 	if (IS_ERR(ssp->clk))
161614af1df3SAndy Shevchenko 		return ERR_CAST(ssp->clk);
1617a3496855SMika Westerberg 
1618a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
16195eb263efSChuhong Yuan 	if (ssp->irq < 0)
162014af1df3SAndy Shevchenko 		return ERR_PTR(ssp->irq);
16215eb263efSChuhong Yuan 
1622a3496855SMika Westerberg 	ssp->type = type;
16234f3d9577SAndy Shevchenko 	ssp->dev = &pdev->dev;
1624365e856eSAndy Shevchenko 	ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
1625a3496855SMika Westerberg 
1626f2faa3ecSAndy Shevchenko 	pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
1627a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1628cddb339bSMika Westerberg 	pdata->enable_dma = true;
162937821a82SAndy Shevchenko 	pdata->dma_burst_size = 1;
1630a3496855SMika Westerberg 
1631a3496855SMika Westerberg 	return pdata;
1632a3496855SMika Westerberg }
1633a3496855SMika Westerberg 
163451eea52dSLubomir Rintel static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
16353cc7b0e3SJarkko Nikula 				      unsigned int cs)
16360c27d9cfSMika Westerberg {
163751eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
16380c27d9cfSMika Westerberg 
1639c3dce24cSAndy Shevchenko 	if (has_acpi_companion(drv_data->ssp->dev)) {
16400c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
16410c27d9cfSMika Westerberg 		/*
16420c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
16430c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
16440c27d9cfSMika Westerberg 		 * to match what Linux expects.
16450c27d9cfSMika Westerberg 		 */
16460c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
164730f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
16480c27d9cfSMika Westerberg 			return cs - 1;
16490c27d9cfSMika Westerberg 
16500c27d9cfSMika Westerberg 		default:
16510c27d9cfSMika Westerberg 			break;
16520c27d9cfSMika Westerberg 		}
16530c27d9cfSMika Westerberg 	}
16540c27d9cfSMika Westerberg 
16550c27d9cfSMika Westerberg 	return cs;
16560c27d9cfSMika Westerberg }
16570c27d9cfSMika Westerberg 
1658b2662a16SDaniel Vetter static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1659b2662a16SDaniel Vetter {
1660b2662a16SDaniel Vetter 	return MAX_DMA_LEN;
1661b2662a16SDaniel Vetter }
1662b2662a16SDaniel Vetter 
1663fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1664ca632f55SGrant Likely {
1665ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
166651eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *platform_info;
166751eea52dSLubomir Rintel 	struct spi_controller *controller;
1668ca632f55SGrant Likely 	struct driver_data *drv_data;
1669ca632f55SGrant Likely 	struct ssp_device *ssp;
16708b136baaSJarkko Nikula 	const struct lpss_config *config;
1671*778c12e6SAndy Shevchenko 	int status;
1672c039dd27SJarkko Nikula 	u32 tmp;
1673ca632f55SGrant Likely 
1674851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1675851bacf5SMika Westerberg 	if (!platform_info) {
16760db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
167714af1df3SAndy Shevchenko 		if (IS_ERR(platform_info)) {
1678851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
167914af1df3SAndy Shevchenko 			return PTR_ERR(platform_info);
1680851bacf5SMika Westerberg 		}
1681a3496855SMika Westerberg 	}
1682ca632f55SGrant Likely 
1683ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1684851bacf5SMika Westerberg 	if (!ssp)
1685851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1686851bacf5SMika Westerberg 
1687851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
1688851bacf5SMika Westerberg 		dev_err(&pdev->dev, "failed to get ssp\n");
1689ca632f55SGrant Likely 		return -ENODEV;
1690ca632f55SGrant Likely 	}
1691ca632f55SGrant Likely 
1692ec93cb6fSLubomir Rintel 	if (platform_info->is_slave)
16935626308bSLukas Wunner 		controller = devm_spi_alloc_slave(dev, sizeof(*drv_data));
1694ec93cb6fSLubomir Rintel 	else
16955626308bSLukas Wunner 		controller = devm_spi_alloc_master(dev, sizeof(*drv_data));
1696ec93cb6fSLubomir Rintel 
169751eea52dSLubomir Rintel 	if (!controller) {
169851eea52dSLubomir Rintel 		dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1699f2eed8caSAndy Shevchenko 		status = -ENOMEM;
1700f2eed8caSAndy Shevchenko 		goto out_error_controller_alloc;
1701ca632f55SGrant Likely 	}
170251eea52dSLubomir Rintel 	drv_data = spi_controller_get_devdata(controller);
170351eea52dSLubomir Rintel 	drv_data->controller = controller;
170451eea52dSLubomir Rintel 	drv_data->controller_info = platform_info;
1705ca632f55SGrant Likely 	drv_data->ssp = ssp;
1706ca632f55SGrant Likely 
170794acf807SAndy Shevchenko 	controller->dev.of_node = dev->of_node;
170894acf807SAndy Shevchenko 	controller->dev.fwnode = dev->fwnode;
170994acf807SAndy Shevchenko 
1710ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
171151eea52dSLubomir Rintel 	controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1712ca632f55SGrant Likely 
171351eea52dSLubomir Rintel 	controller->bus_num = ssp->port_id;
171451eea52dSLubomir Rintel 	controller->dma_alignment = DMA_ALIGNMENT;
171551eea52dSLubomir Rintel 	controller->cleanup = cleanup;
171651eea52dSLubomir Rintel 	controller->setup = setup;
171751eea52dSLubomir Rintel 	controller->set_cs = pxa2xx_spi_set_cs;
171851eea52dSLubomir Rintel 	controller->transfer_one = pxa2xx_spi_transfer_one;
171951eea52dSLubomir Rintel 	controller->slave_abort = pxa2xx_spi_slave_abort;
172051eea52dSLubomir Rintel 	controller->handle_err = pxa2xx_spi_handle_err;
172151eea52dSLubomir Rintel 	controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
172251eea52dSLubomir Rintel 	controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
172351eea52dSLubomir Rintel 	controller->auto_runtime_pm = true;
172451eea52dSLubomir Rintel 	controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1725ca632f55SGrant Likely 
1726ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1727ca632f55SGrant Likely 
1728ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1729e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1730e5262d05SWeike Chen 		case QUARK_X1000_SSP:
173151eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1732e5262d05SWeike Chen 			break;
1733e5262d05SWeike Chen 		default:
173451eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1735e5262d05SWeike Chen 			break;
1736e5262d05SWeike Chen 		}
1737e5262d05SWeike Chen 
1738ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1739ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1740ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1741ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1742ca632f55SGrant Likely 	} else {
174351eea52dSLubomir Rintel 		controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1744ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
17455928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1746ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1747ec93cb6fSLubomir Rintel 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1748ec93cb6fSLubomir Rintel 						| SSSR_ROR | SSSR_TUR;
1749ca632f55SGrant Likely 	}
1750ca632f55SGrant Likely 
1751ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1752ca632f55SGrant Likely 			drv_data);
1753ca632f55SGrant Likely 	if (status < 0) {
1754ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
175551eea52dSLubomir Rintel 		goto out_error_controller_alloc;
1756ca632f55SGrant Likely 	}
1757ca632f55SGrant Likely 
1758ca632f55SGrant Likely 	/* Setup DMA if requested */
1759ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1760cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1761cd7bed00SMika Westerberg 		if (status) {
17628b57b11bSFlavio Suligoi 			dev_warn(dev, "no DMA channels available, using PIO\n");
1763cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1764b6ced294SJarkko Nikula 		} else {
176551eea52dSLubomir Rintel 			controller->can_dma = pxa2xx_spi_can_dma;
1766bf9f742cSMark Brown 			controller->max_dma_len = MAX_DMA_LEN;
1767b2662a16SDaniel Vetter 			controller->max_transfer_size =
1768b2662a16SDaniel Vetter 				pxa2xx_spi_max_dma_transfer_size;
1769ca632f55SGrant Likely 		}
1770ca632f55SGrant Likely 	}
1771ca632f55SGrant Likely 
1772ca632f55SGrant Likely 	/* Enable SOC clock */
177362bbc864STobias Jordan 	status = clk_prepare_enable(ssp->clk);
177462bbc864STobias Jordan 	if (status)
177562bbc864STobias Jordan 		goto out_error_dma_irq_alloc;
17763343b7a6SMika Westerberg 
177751eea52dSLubomir Rintel 	controller->max_speed_hz = clk_get_rate(ssp->clk);
177823cdddb2SJarkko Nikula 	/*
177923cdddb2SJarkko Nikula 	 * Set minimum speed for all other platforms than Intel Quark which is
178023cdddb2SJarkko Nikula 	 * able do under 1 Hz transfers.
178123cdddb2SJarkko Nikula 	 */
178223cdddb2SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
178323cdddb2SJarkko Nikula 		controller->min_speed_hz =
178423cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 4096);
178523cdddb2SJarkko Nikula 	else if (!is_quark_x1000_ssp(drv_data))
178623cdddb2SJarkko Nikula 		controller->min_speed_hz =
178723cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 512);
1788ca632f55SGrant Likely 
17890c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
17900c8ccd8bSAndy Shevchenko 
1791ca632f55SGrant Likely 	/* Load default SSP configuration */
1792e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1793e5262d05SWeike Chen 	case QUARK_X1000_SSP:
17947c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
17957c7289a4SAndy Shevchenko 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1796c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1797e5262d05SWeike Chen 
1798e5262d05SWeike Chen 		/* using the Motorola SPI protocol and use 8 bit frame */
17997c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
18007c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1801e5262d05SWeike Chen 		break;
18027c7289a4SAndy Shevchenko 	case CE4100_SSP:
18037c7289a4SAndy Shevchenko 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
18047c7289a4SAndy Shevchenko 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
18057c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
18067c7289a4SAndy Shevchenko 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
18077c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1808a2dd8af0SAndy Shevchenko 		break;
1809e5262d05SWeike Chen 	default:
1810ec93cb6fSLubomir Rintel 
181151eea52dSLubomir Rintel 		if (spi_controller_is_slave(controller)) {
1812ec93cb6fSLubomir Rintel 			tmp = SSCR1_SCFR |
1813ec93cb6fSLubomir Rintel 			      SSCR1_SCLKDIR |
1814ec93cb6fSLubomir Rintel 			      SSCR1_SFRMDIR |
1815ec93cb6fSLubomir Rintel 			      SSCR1_RxTresh(2) |
1816ec93cb6fSLubomir Rintel 			      SSCR1_TxTresh(1) |
1817ec93cb6fSLubomir Rintel 			      SSCR1_SPH;
1818ec93cb6fSLubomir Rintel 		} else {
1819c039dd27SJarkko Nikula 			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1820c039dd27SJarkko Nikula 			      SSCR1_TxTresh(TX_THRESH_DFLT);
1821ec93cb6fSLubomir Rintel 		}
1822c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1823ec93cb6fSLubomir Rintel 		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
182451eea52dSLubomir Rintel 		if (!spi_controller_is_slave(controller))
1825ec93cb6fSLubomir Rintel 			tmp |= SSCR0_SCR(2);
1826c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1827e5262d05SWeike Chen 		break;
1828e5262d05SWeike Chen 	}
1829e5262d05SWeike Chen 
1830ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1831c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1832e5262d05SWeike Chen 
1833e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1834c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1835ca632f55SGrant Likely 
18368b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
18378b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
18388b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
18398b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
18408b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
18418b136baaSJarkko Nikula 						   config->reg_capabilities);
18428b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
18438b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
18448b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
184530f3a6abSMika Westerberg 		} else if (config->cs_num) {
184630f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
18478b136baaSJarkko Nikula 		}
18488b136baaSJarkko Nikula 	}
184951eea52dSLubomir Rintel 	controller->num_chipselect = platform_info->num_chipselect;
1850*778c12e6SAndy Shevchenko 	controller->use_gpio_descriptors = true;
18516ac5a435SAndy Shevchenko 
185277d33897SLubomir Rintel 	if (platform_info->is_slave) {
185377d33897SLubomir Rintel 		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
185477d33897SLubomir Rintel 						"ready", GPIOD_OUT_LOW);
185577d33897SLubomir Rintel 		if (IS_ERR(drv_data->gpiod_ready)) {
185677d33897SLubomir Rintel 			status = PTR_ERR(drv_data->gpiod_ready);
185777d33897SLubomir Rintel 			goto out_error_clock_enabled;
185877d33897SLubomir Rintel 		}
185977d33897SLubomir Rintel 	}
186077d33897SLubomir Rintel 
1861836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1862836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1863836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1864836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1865836d1a22SAntonio Ospite 
1866ca632f55SGrant Likely 	/* Register with the SPI framework */
1867ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
186832e5b572SLukas Wunner 	status = spi_register_controller(controller);
1869ca632f55SGrant Likely 	if (status != 0) {
187051eea52dSLubomir Rintel 		dev_err(&pdev->dev, "problem registering spi controller\n");
187112742045SLubomir Rintel 		goto out_error_pm_runtime_enabled;
1872ca632f55SGrant Likely 	}
1873ca632f55SGrant Likely 
1874ca632f55SGrant Likely 	return status;
1875ca632f55SGrant Likely 
187612742045SLubomir Rintel out_error_pm_runtime_enabled:
1877e2b714afSJarkko Nikula 	pm_runtime_disable(&pdev->dev);
187812742045SLubomir Rintel 
187912742045SLubomir Rintel out_error_clock_enabled:
18803343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
188162bbc864STobias Jordan 
188262bbc864STobias Jordan out_error_dma_irq_alloc:
1883cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1884ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1885ca632f55SGrant Likely 
188651eea52dSLubomir Rintel out_error_controller_alloc:
1887ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1888ca632f55SGrant Likely 	return status;
1889ca632f55SGrant Likely }
1890ca632f55SGrant Likely 
1891ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1892ca632f55SGrant Likely {
1893ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
18943d24b2a4SAndy Shevchenko 	struct ssp_device *ssp = drv_data->ssp;
1895ca632f55SGrant Likely 
18967d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
18977d94a505SMika Westerberg 
189832e5b572SLukas Wunner 	spi_unregister_controller(drv_data->controller);
189932e5b572SLukas Wunner 
1900ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
19010c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
19023343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1903ca632f55SGrant Likely 
1904ca632f55SGrant Likely 	/* Release DMA */
190551eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma)
1906cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1907ca632f55SGrant Likely 
19087d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
19097d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
19107d94a505SMika Westerberg 
1911ca632f55SGrant Likely 	/* Release IRQ */
1912ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1913ca632f55SGrant Likely 
1914ca632f55SGrant Likely 	/* Release SSP */
1915ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1916ca632f55SGrant Likely 
1917ca632f55SGrant Likely 	return 0;
1918ca632f55SGrant Likely }
1919ca632f55SGrant Likely 
1920382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP
1921ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1922ca632f55SGrant Likely {
1923ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1924ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1925bffc967eSJarkko Nikula 	int status;
1926ca632f55SGrant Likely 
192751eea52dSLubomir Rintel 	status = spi_controller_suspend(drv_data->controller);
1928ca632f55SGrant Likely 	if (status != 0)
1929ca632f55SGrant Likely 		return status;
19300c8ccd8bSAndy Shevchenko 
19310c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
19322b9375b9SDmitry Eremin-Solenikov 
19332b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
19343343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1935ca632f55SGrant Likely 
1936ca632f55SGrant Likely 	return 0;
1937ca632f55SGrant Likely }
1938ca632f55SGrant Likely 
1939ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1940ca632f55SGrant Likely {
1941ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1942ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1943bffc967eSJarkko Nikula 	int status;
1944ca632f55SGrant Likely 
1945ca632f55SGrant Likely 	/* Enable the SSP clock */
194662bbc864STobias Jordan 	if (!pm_runtime_suspended(dev)) {
194762bbc864STobias Jordan 		status = clk_prepare_enable(ssp->clk);
194862bbc864STobias Jordan 		if (status)
194962bbc864STobias Jordan 			return status;
195062bbc864STobias Jordan 	}
1951ca632f55SGrant Likely 
1952ca632f55SGrant Likely 	/* Start the queue running */
195351eea52dSLubomir Rintel 	return spi_controller_resume(drv_data->controller);
1954ca632f55SGrant Likely }
19557d94a505SMika Westerberg #endif
19567d94a505SMika Westerberg 
1957ec833050SRafael J. Wysocki #ifdef CONFIG_PM
19587d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
19597d94a505SMika Westerberg {
19607d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
19617d94a505SMika Westerberg 
19627d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
19637d94a505SMika Westerberg 	return 0;
19647d94a505SMika Westerberg }
19657d94a505SMika Westerberg 
19667d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
19677d94a505SMika Westerberg {
19687d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
196962bbc864STobias Jordan 	int status;
19707d94a505SMika Westerberg 
197162bbc864STobias Jordan 	status = clk_prepare_enable(drv_data->ssp->clk);
197262bbc864STobias Jordan 	return status;
19737d94a505SMika Westerberg }
19747d94a505SMika Westerberg #endif
1975ca632f55SGrant Likely 
1976ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
19777d94a505SMika Westerberg 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
19787d94a505SMika Westerberg 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
19797d94a505SMika Westerberg 			   pxa2xx_spi_runtime_resume, NULL)
1980ca632f55SGrant Likely };
1981ca632f55SGrant Likely 
1982ca632f55SGrant Likely static struct platform_driver driver = {
1983ca632f55SGrant Likely 	.driver = {
1984ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
1985ca632f55SGrant Likely 		.pm	= &pxa2xx_spi_pm_ops,
1986a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
198787ae1d2dSLubomir Rintel 		.of_match_table = of_match_ptr(pxa2xx_spi_of_match),
1988ca632f55SGrant Likely 	},
1989ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
1990ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
1991ca632f55SGrant Likely };
1992ca632f55SGrant Likely 
1993ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
1994ca632f55SGrant Likely {
1995ca632f55SGrant Likely 	return platform_driver_register(&driver);
1996ca632f55SGrant Likely }
1997ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
1998ca632f55SGrant Likely 
1999ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
2000ca632f55SGrant Likely {
2001ca632f55SGrant Likely 	platform_driver_unregister(&driver);
2002ca632f55SGrant Likely }
2003ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
200451ebf6acSFlavio Suligoi 
200551ebf6acSFlavio Suligoi MODULE_SOFTDEP("pre: dw_dmac");
2006