xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision 94acf80755c8d8dd066d4f5a2afbdf393f0e2afd)
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 {
12013cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12023cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1203c18d925fSJan Kiszka 	struct gpio_desc *gpiod;
1204ca632f55SGrant Likely 	int err = 0;
1205ca632f55SGrant Likely 
120699f499cdSMika Westerberg 	if (chip == NULL)
120799f499cdSMika Westerberg 		return 0;
120899f499cdSMika Westerberg 
12096ac5a435SAndy Shevchenko 	if (drv_data->cs_gpiods) {
12106ac5a435SAndy Shevchenko 		gpiod = drv_data->cs_gpiods[spi->chip_select];
12116ac5a435SAndy Shevchenko 		if (gpiod) {
1212c18d925fSJan Kiszka 			chip->gpiod_cs = gpiod;
121399f499cdSMika Westerberg 			chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
121499f499cdSMika Westerberg 			gpiod_set_value(gpiod, chip->gpio_cs_inverted);
12156ac5a435SAndy Shevchenko 		}
121699f499cdSMika Westerberg 
121799f499cdSMika Westerberg 		return 0;
121899f499cdSMika Westerberg 	}
121999f499cdSMika Westerberg 
122099f499cdSMika Westerberg 	if (chip_info == NULL)
1221ca632f55SGrant Likely 		return 0;
1222ca632f55SGrant Likely 
1223ca632f55SGrant Likely 	/* NOTE: setup() can be called multiple times, possibly with
1224ca632f55SGrant Likely 	 * different chip_info, release previously requested GPIO
1225ca632f55SGrant Likely 	 */
1226c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
1227a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1228c18d925fSJan Kiszka 		chip->gpiod_cs = NULL;
1229c18d925fSJan Kiszka 	}
1230ca632f55SGrant Likely 
1231ca632f55SGrant Likely 	/* If (*cs_control) is provided, ignore GPIO chip select */
1232ca632f55SGrant Likely 	if (chip_info->cs_control) {
1233ca632f55SGrant Likely 		chip->cs_control = chip_info->cs_control;
1234ca632f55SGrant Likely 		return 0;
1235ca632f55SGrant Likely 	}
1236ca632f55SGrant Likely 
1237ca632f55SGrant Likely 	if (gpio_is_valid(chip_info->gpio_cs)) {
1238ca632f55SGrant Likely 		err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1239ca632f55SGrant Likely 		if (err) {
1240f6bd03a7SJarkko Nikula 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1241f6bd03a7SJarkko Nikula 				chip_info->gpio_cs);
1242ca632f55SGrant Likely 			return err;
1243ca632f55SGrant Likely 		}
1244ca632f55SGrant Likely 
1245c18d925fSJan Kiszka 		gpiod = gpio_to_desc(chip_info->gpio_cs);
1246c18d925fSJan Kiszka 		chip->gpiod_cs = gpiod;
1247ca632f55SGrant Likely 		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1248ca632f55SGrant Likely 
1249c18d925fSJan Kiszka 		err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
1250ca632f55SGrant Likely 	}
1251ca632f55SGrant Likely 
1252ca632f55SGrant Likely 	return err;
1253ca632f55SGrant Likely }
1254ca632f55SGrant Likely 
1255ca632f55SGrant Likely static int setup(struct spi_device *spi)
1256ca632f55SGrant Likely {
1257bffc967eSJarkko Nikula 	struct pxa2xx_spi_chip *chip_info;
1258ca632f55SGrant Likely 	struct chip_data *chip;
1259dccf7369SJarkko Nikula 	const struct lpss_config *config;
12603cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12613cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1262a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1263a0d2642eSMika Westerberg 
1264e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1265e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1266e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1267e5262d05SWeike Chen 		tx_hi_thres = 0;
1268e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1269e5262d05SWeike Chen 		break;
12703fdb59cfSAndy Shevchenko 	case MRFLD_SSP:
12713fdb59cfSAndy Shevchenko 		tx_thres = TX_THRESH_MRFLD_DFLT;
12723fdb59cfSAndy Shevchenko 		tx_hi_thres = 0;
12733fdb59cfSAndy Shevchenko 		rx_thres = RX_THRESH_MRFLD_DFLT;
12743fdb59cfSAndy Shevchenko 		break;
12757c7289a4SAndy Shevchenko 	case CE4100_SSP:
12767c7289a4SAndy Shevchenko 		tx_thres = TX_THRESH_CE4100_DFLT;
12777c7289a4SAndy Shevchenko 		tx_hi_thres = 0;
12787c7289a4SAndy Shevchenko 		rx_thres = RX_THRESH_CE4100_DFLT;
12797c7289a4SAndy Shevchenko 		break;
128003fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
128103fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
128230f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
128334cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1284b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1285fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
1286dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1287dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1288dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1289dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1290e5262d05SWeike Chen 		break;
1291e5262d05SWeike Chen 	default:
1292a0d2642eSMika Westerberg 		tx_hi_thres = 0;
129351eea52dSLubomir Rintel 		if (spi_controller_is_slave(drv_data->controller)) {
1294ec93cb6fSLubomir Rintel 			tx_thres = 1;
1295ec93cb6fSLubomir Rintel 			rx_thres = 2;
1296ec93cb6fSLubomir Rintel 		} else {
1297ec93cb6fSLubomir Rintel 			tx_thres = TX_THRESH_DFLT;
1298a0d2642eSMika Westerberg 			rx_thres = RX_THRESH_DFLT;
1299ec93cb6fSLubomir Rintel 		}
1300e5262d05SWeike Chen 		break;
1301a0d2642eSMika Westerberg 	}
1302ca632f55SGrant Likely 
1303ca632f55SGrant Likely 	/* Only alloc on first setup */
1304ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1305ca632f55SGrant Likely 	if (!chip) {
1306ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
13079deae459SJingoo Han 		if (!chip)
1308ca632f55SGrant Likely 			return -ENOMEM;
1309ca632f55SGrant Likely 
1310ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1311ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1312f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1313f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1314ca632f55SGrant Likely 				kfree(chip);
1315ca632f55SGrant Likely 				return -EINVAL;
1316ca632f55SGrant Likely 			}
1317ca632f55SGrant Likely 
1318ca632f55SGrant Likely 			chip->frm = spi->chip_select;
1319c18d925fSJan Kiszka 		}
132051eea52dSLubomir Rintel 		chip->enable_dma = drv_data->controller_info->enable_dma;
1321ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1322ca632f55SGrant Likely 	}
1323ca632f55SGrant Likely 
1324ca632f55SGrant Likely 	/* protocol drivers may change the chip settings, so...
1325ca632f55SGrant Likely 	 * if chip_info exists, use it */
1326ca632f55SGrant Likely 	chip_info = spi->controller_data;
1327ca632f55SGrant Likely 
1328ca632f55SGrant Likely 	/* chip_info isn't always needed */
1329ca632f55SGrant Likely 	chip->cr1 = 0;
1330ca632f55SGrant Likely 	if (chip_info) {
1331ca632f55SGrant Likely 		if (chip_info->timeout)
1332ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1333ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1334ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1335a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1336a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1337ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1338ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1339ca632f55SGrant Likely 		chip->dma_threshold = 0;
1340ca632f55SGrant Likely 		if (chip_info->enable_loopback)
1341ca632f55SGrant Likely 			chip->cr1 = SSCR1_LBM;
1342ca632f55SGrant Likely 	}
134351eea52dSLubomir Rintel 	if (spi_controller_is_slave(drv_data->controller)) {
1344ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCFR;
1345ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCLKDIR;
1346ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SFRMDIR;
1347ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SPH;
1348ec93cb6fSLubomir Rintel 	}
1349ca632f55SGrant Likely 
13503fdb59cfSAndy Shevchenko 	if (is_lpss_ssp(drv_data)) {
1351a0d2642eSMika Westerberg 		chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
13523fdb59cfSAndy Shevchenko 		chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
13533fdb59cfSAndy Shevchenko 					  SSITF_TxHiThresh(tx_hi_thres);
13543fdb59cfSAndy Shevchenko 	}
13553fdb59cfSAndy Shevchenko 
13563fdb59cfSAndy Shevchenko 	if (is_mrfld_ssp(drv_data)) {
13573fdb59cfSAndy Shevchenko 		chip->lpss_rx_threshold = rx_thres;
13583fdb59cfSAndy Shevchenko 		chip->lpss_tx_threshold = tx_thres;
13593fdb59cfSAndy Shevchenko 	}
1360a0d2642eSMika Westerberg 
1361ca632f55SGrant Likely 	/* set dma burst and threshold outside of chip_info path so that if
1362ca632f55SGrant Likely 	 * chip_info goes away after setting chip->enable_dma, the
1363ca632f55SGrant Likely 	 * burst and threshold can still respond to changes in bits_per_word */
1364ca632f55SGrant Likely 	if (chip->enable_dma) {
1365ca632f55SGrant Likely 		/* set up legal burst and threshold for dma */
1366cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1367cd7bed00SMika Westerberg 						spi->bits_per_word,
1368ca632f55SGrant Likely 						&chip->dma_burst_size,
1369ca632f55SGrant Likely 						&chip->dma_threshold)) {
1370f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1371f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1372ca632f55SGrant Likely 		}
1373000c6af4SAndy Shevchenko 		dev_dbg(&spi->dev,
1374000c6af4SAndy Shevchenko 			"in setup: DMA burst size set to %u\n",
1375000c6af4SAndy Shevchenko 			chip->dma_burst_size);
1376ca632f55SGrant Likely 	}
1377ca632f55SGrant Likely 
1378e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1379e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1380e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1381e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1382e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1383e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1384e5262d05SWeike Chen 		break;
13857c7289a4SAndy Shevchenko 	case CE4100_SSP:
13867c7289a4SAndy Shevchenko 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
13877c7289a4SAndy Shevchenko 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
13887c7289a4SAndy Shevchenko 		break;
1389e5262d05SWeike Chen 	default:
1390e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1391e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1392e5262d05SWeike Chen 		break;
1393e5262d05SWeike Chen 	}
1394e5262d05SWeike Chen 
1395ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1396ca632f55SGrant Likely 	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1397ca632f55SGrant Likely 			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1398ca632f55SGrant Likely 
1399b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1400b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1401b833172fSMika Westerberg 
1402ca632f55SGrant Likely 	if (spi->bits_per_word <= 8) {
1403ca632f55SGrant Likely 		chip->n_bytes = 1;
1404ca632f55SGrant Likely 		chip->read = u8_reader;
1405ca632f55SGrant Likely 		chip->write = u8_writer;
1406ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 16) {
1407ca632f55SGrant Likely 		chip->n_bytes = 2;
1408ca632f55SGrant Likely 		chip->read = u16_reader;
1409ca632f55SGrant Likely 		chip->write = u16_writer;
1410ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 32) {
1411ca632f55SGrant Likely 		chip->n_bytes = 4;
1412ca632f55SGrant Likely 		chip->read = u32_reader;
1413ca632f55SGrant Likely 		chip->write = u32_writer;
1414ca632f55SGrant Likely 	}
1415ca632f55SGrant Likely 
1416ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1417ca632f55SGrant Likely 
1418ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
1419ca632f55SGrant Likely 		return 0;
1420ca632f55SGrant Likely 
1421ca632f55SGrant Likely 	return setup_cs(spi, chip, chip_info);
1422ca632f55SGrant Likely }
1423ca632f55SGrant Likely 
1424ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1425ca632f55SGrant Likely {
1426ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
14273cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
14283cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1429ca632f55SGrant Likely 
1430ca632f55SGrant Likely 	if (!chip)
1431ca632f55SGrant Likely 		return;
1432ca632f55SGrant Likely 
14336ac5a435SAndy Shevchenko 	if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
1434c18d925fSJan Kiszka 	    chip->gpiod_cs)
1435a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1436ca632f55SGrant Likely 
1437ca632f55SGrant Likely 	kfree(chip);
1438ca632f55SGrant Likely }
1439ca632f55SGrant Likely 
14409b2d6119SLee Jones #ifdef CONFIG_ACPI
14418422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
144203fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
144303fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
144403fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
144503fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
144603fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
144730f3a6abSMika Westerberg 	{ "8086228E", LPSS_BSW_SSP },
144803fbf488SJarkko Nikula 	{ },
144903fbf488SJarkko Nikula };
145003fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
14519b2d6119SLee Jones #endif
145203fbf488SJarkko Nikula 
145334cadd9cSJarkko Nikula /*
145434cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
145534cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
145634cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
145734cadd9cSJarkko Nikula  */
145834cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
145934cadd9cSJarkko Nikula 	/* SPT-LP */
146034cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
146134cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
146234cadd9cSJarkko Nikula 	/* SPT-H */
146334cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
146434cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1465704d2b07SMika Westerberg 	/* KBL-H */
1466704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1467704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
14686157d4c2SJarkko Nikula 	/* CML-V */
14696157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
14706157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
1471c1b03f11SJarkko Nikula 	/* BXT A-Step */
1472b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1473b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1474b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1475c1b03f11SJarkko Nikula 	/* BXT B-Step */
1476c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1477c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1478c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1479e18a80acSDavid E. Box 	/* GLK */
1480e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1481e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1482e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
148322d71a50SMika Westerberg 	/* ICL-LP */
148422d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
148522d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
148622d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
14878cc77204SJarkko Nikula 	/* EHL */
14888cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
14898cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
14908cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
14919c7315c9SJarkko Nikula 	/* JSL */
14929c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
14939c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
14949c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
1495cf961fceSJarkko Nikula 	/* TGL-H */
1496cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
1497cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
1498cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
1499cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
1500a402e397SJarkko Nikula 	/* ADL-P */
1501a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51aa), LPSS_CNL_SSP },
1502a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51ab), LPSS_CNL_SSP },
1503a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51fb), LPSS_CNL_SSP },
15048c4ffe4dSJarkko Nikula 	/* ADL-M */
15058c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54aa), LPSS_CNL_SSP },
15068c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54ab), LPSS_CNL_SSP },
15078c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54fb), LPSS_CNL_SSP },
1508b7c08cf8SJarkko Nikula 	/* APL */
1509b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1510b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1511b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1512b8450e01SJarkko Nikula 	/* ADL-S */
1513b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7aaa), LPSS_CNL_SSP },
1514b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7aab), LPSS_CNL_SSP },
1515b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7af9), LPSS_CNL_SSP },
1516b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7afb), LPSS_CNL_SSP },
1517fc0b2accSJarkko Nikula 	/* CNL-LP */
1518fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1519fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1520fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1521fc0b2accSJarkko Nikula 	/* CNL-H */
1522fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1523fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1524fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
152541a91802SEvan Green 	/* CML-LP */
152641a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
152741a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
152841a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
1529f0cf17edSJarkko Nikula 	/* CML-H */
1530f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1531f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1532f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
1533a4127952SJarkko Nikula 	/* TGL-LP */
1534a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1535a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1536a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1537a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1538a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1539a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1540a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
154194e5c23dSAxel Lin 	{ },
154234cadd9cSJarkko Nikula };
154334cadd9cSJarkko Nikula 
154487ae1d2dSLubomir Rintel static const struct of_device_id pxa2xx_spi_of_match[] = {
154587ae1d2dSLubomir Rintel 	{ .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
154687ae1d2dSLubomir Rintel 	{},
154787ae1d2dSLubomir Rintel };
154887ae1d2dSLubomir Rintel MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
154987ae1d2dSLubomir Rintel 
155087ae1d2dSLubomir Rintel #ifdef CONFIG_ACPI
155187ae1d2dSLubomir Rintel 
1552365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
155387ae1d2dSLubomir Rintel {
1554365e856eSAndy Shevchenko 	struct acpi_device *adev;
155587ae1d2dSLubomir Rintel 	unsigned int devid;
155687ae1d2dSLubomir Rintel 	int port_id = -1;
155787ae1d2dSLubomir Rintel 
1558365e856eSAndy Shevchenko 	adev = ACPI_COMPANION(dev);
155987ae1d2dSLubomir Rintel 	if (adev && adev->pnp.unique_id &&
156087ae1d2dSLubomir Rintel 	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
156187ae1d2dSLubomir Rintel 		port_id = devid;
156287ae1d2dSLubomir Rintel 	return port_id;
156387ae1d2dSLubomir Rintel }
156487ae1d2dSLubomir Rintel 
156587ae1d2dSLubomir Rintel #else /* !CONFIG_ACPI */
156687ae1d2dSLubomir Rintel 
1567365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
156887ae1d2dSLubomir Rintel {
156987ae1d2dSLubomir Rintel 	return -1;
157087ae1d2dSLubomir Rintel }
157187ae1d2dSLubomir Rintel 
157287ae1d2dSLubomir Rintel #endif /* CONFIG_ACPI */
157387ae1d2dSLubomir Rintel 
157487ae1d2dSLubomir Rintel 
157587ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
157687ae1d2dSLubomir Rintel 
157734cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
157834cadd9cSJarkko Nikula {
15795ba846b1SAndy Shevchenko 	return param == chan->device->dev;
158034cadd9cSJarkko Nikula }
158134cadd9cSJarkko Nikula 
158287ae1d2dSLubomir Rintel #endif /* CONFIG_PCI */
158387ae1d2dSLubomir Rintel 
158451eea52dSLubomir Rintel static struct pxa2xx_spi_controller *
15850db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1586a3496855SMika Westerberg {
158751eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *pdata;
1588a3496855SMika Westerberg 	struct ssp_device *ssp;
1589a3496855SMika Westerberg 	struct resource *res;
15906fb7427dSAndy Shevchenko 	struct device *parent = pdev->dev.parent;
15916fb7427dSAndy Shevchenko 	struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
159234cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
159355ef8262SLubomir Rintel 	enum pxa_ssp_type type;
1594f2faa3ecSAndy Shevchenko 	const void *match;
1595a3496855SMika Westerberg 
15966fb7427dSAndy Shevchenko 	if (pcidev)
15976fb7427dSAndy Shevchenko 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
1598a3496855SMika Westerberg 
1599f2faa3ecSAndy Shevchenko 	match = device_get_match_data(&pdev->dev);
1600f2faa3ecSAndy Shevchenko 	if (match)
1601f2faa3ecSAndy Shevchenko 		type = (enum pxa_ssp_type)match;
160234cadd9cSJarkko Nikula 	else if (pcidev_id)
160355ef8262SLubomir Rintel 		type = (enum pxa_ssp_type)pcidev_id->driver_data;
160403fbf488SJarkko Nikula 	else
160514af1df3SAndy Shevchenko 		return ERR_PTR(-EINVAL);
160603fbf488SJarkko Nikula 
1607cc0ee987SMika Westerberg 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
16089deae459SJingoo Han 	if (!pdata)
160914af1df3SAndy Shevchenko 		return ERR_PTR(-ENOMEM);
1610a3496855SMika Westerberg 
1611a3496855SMika Westerberg 	ssp = &pdata->ssp;
1612a3496855SMika Westerberg 
161377c544d2SAndy Shevchenko 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1614cbfd6a21SSachin Kamat 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1615cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
161614af1df3SAndy Shevchenko 		return ERR_CAST(ssp->mmio_base);
1617a3496855SMika Westerberg 
161877c544d2SAndy Shevchenko 	ssp->phys_base = res->start;
161977c544d2SAndy Shevchenko 
162087ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
162134cadd9cSJarkko Nikula 	if (pcidev_id) {
16226fb7427dSAndy Shevchenko 		pdata->tx_param = parent;
16236fb7427dSAndy Shevchenko 		pdata->rx_param = parent;
162434cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
162534cadd9cSJarkko Nikula 	}
162687ae1d2dSLubomir Rintel #endif
162734cadd9cSJarkko Nikula 
1628a3496855SMika Westerberg 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
16295eb263efSChuhong Yuan 	if (IS_ERR(ssp->clk))
163014af1df3SAndy Shevchenko 		return ERR_CAST(ssp->clk);
1631a3496855SMika Westerberg 
1632a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
16335eb263efSChuhong Yuan 	if (ssp->irq < 0)
163414af1df3SAndy Shevchenko 		return ERR_PTR(ssp->irq);
16355eb263efSChuhong Yuan 
1636a3496855SMika Westerberg 	ssp->type = type;
16374f3d9577SAndy Shevchenko 	ssp->dev = &pdev->dev;
1638365e856eSAndy Shevchenko 	ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
1639a3496855SMika Westerberg 
1640f2faa3ecSAndy Shevchenko 	pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
1641a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1642cddb339bSMika Westerberg 	pdata->enable_dma = true;
164337821a82SAndy Shevchenko 	pdata->dma_burst_size = 1;
1644a3496855SMika Westerberg 
1645a3496855SMika Westerberg 	return pdata;
1646a3496855SMika Westerberg }
1647a3496855SMika Westerberg 
164851eea52dSLubomir Rintel static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
16493cc7b0e3SJarkko Nikula 				      unsigned int cs)
16500c27d9cfSMika Westerberg {
165151eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
16520c27d9cfSMika Westerberg 
1653c3dce24cSAndy Shevchenko 	if (has_acpi_companion(drv_data->ssp->dev)) {
16540c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
16550c27d9cfSMika Westerberg 		/*
16560c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
16570c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
16580c27d9cfSMika Westerberg 		 * to match what Linux expects.
16590c27d9cfSMika Westerberg 		 */
16600c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
166130f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
16620c27d9cfSMika Westerberg 			return cs - 1;
16630c27d9cfSMika Westerberg 
16640c27d9cfSMika Westerberg 		default:
16650c27d9cfSMika Westerberg 			break;
16660c27d9cfSMika Westerberg 		}
16670c27d9cfSMika Westerberg 	}
16680c27d9cfSMika Westerberg 
16690c27d9cfSMika Westerberg 	return cs;
16700c27d9cfSMika Westerberg }
16710c27d9cfSMika Westerberg 
1672b2662a16SDaniel Vetter static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1673b2662a16SDaniel Vetter {
1674b2662a16SDaniel Vetter 	return MAX_DMA_LEN;
1675b2662a16SDaniel Vetter }
1676b2662a16SDaniel Vetter 
1677fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1678ca632f55SGrant Likely {
1679ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
168051eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *platform_info;
168151eea52dSLubomir Rintel 	struct spi_controller *controller;
1682ca632f55SGrant Likely 	struct driver_data *drv_data;
1683ca632f55SGrant Likely 	struct ssp_device *ssp;
16848b136baaSJarkko Nikula 	const struct lpss_config *config;
168599f499cdSMika Westerberg 	int status, count;
1686c039dd27SJarkko Nikula 	u32 tmp;
1687ca632f55SGrant Likely 
1688851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1689851bacf5SMika Westerberg 	if (!platform_info) {
16900db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
169114af1df3SAndy Shevchenko 		if (IS_ERR(platform_info)) {
1692851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
169314af1df3SAndy Shevchenko 			return PTR_ERR(platform_info);
1694851bacf5SMika Westerberg 		}
1695a3496855SMika Westerberg 	}
1696ca632f55SGrant Likely 
1697ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1698851bacf5SMika Westerberg 	if (!ssp)
1699851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1700851bacf5SMika Westerberg 
1701851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
1702851bacf5SMika Westerberg 		dev_err(&pdev->dev, "failed to get ssp\n");
1703ca632f55SGrant Likely 		return -ENODEV;
1704ca632f55SGrant Likely 	}
1705ca632f55SGrant Likely 
1706ec93cb6fSLubomir Rintel 	if (platform_info->is_slave)
17075626308bSLukas Wunner 		controller = devm_spi_alloc_slave(dev, sizeof(*drv_data));
1708ec93cb6fSLubomir Rintel 	else
17095626308bSLukas Wunner 		controller = devm_spi_alloc_master(dev, sizeof(*drv_data));
1710ec93cb6fSLubomir Rintel 
171151eea52dSLubomir Rintel 	if (!controller) {
171251eea52dSLubomir Rintel 		dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1713f2eed8caSAndy Shevchenko 		status = -ENOMEM;
1714f2eed8caSAndy Shevchenko 		goto out_error_controller_alloc;
1715ca632f55SGrant Likely 	}
171651eea52dSLubomir Rintel 	drv_data = spi_controller_get_devdata(controller);
171751eea52dSLubomir Rintel 	drv_data->controller = controller;
171851eea52dSLubomir Rintel 	drv_data->controller_info = platform_info;
1719ca632f55SGrant Likely 	drv_data->ssp = ssp;
1720ca632f55SGrant Likely 
1721*94acf807SAndy Shevchenko 	controller->dev.of_node = dev->of_node;
1722*94acf807SAndy Shevchenko 	controller->dev.fwnode = dev->fwnode;
1723*94acf807SAndy Shevchenko 
1724ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
172551eea52dSLubomir Rintel 	controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1726ca632f55SGrant Likely 
172751eea52dSLubomir Rintel 	controller->bus_num = ssp->port_id;
172851eea52dSLubomir Rintel 	controller->dma_alignment = DMA_ALIGNMENT;
172951eea52dSLubomir Rintel 	controller->cleanup = cleanup;
173051eea52dSLubomir Rintel 	controller->setup = setup;
173151eea52dSLubomir Rintel 	controller->set_cs = pxa2xx_spi_set_cs;
173251eea52dSLubomir Rintel 	controller->transfer_one = pxa2xx_spi_transfer_one;
173351eea52dSLubomir Rintel 	controller->slave_abort = pxa2xx_spi_slave_abort;
173451eea52dSLubomir Rintel 	controller->handle_err = pxa2xx_spi_handle_err;
173551eea52dSLubomir Rintel 	controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
173651eea52dSLubomir Rintel 	controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
173751eea52dSLubomir Rintel 	controller->auto_runtime_pm = true;
173851eea52dSLubomir Rintel 	controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1739ca632f55SGrant Likely 
1740ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1741ca632f55SGrant Likely 
1742ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1743e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1744e5262d05SWeike Chen 		case QUARK_X1000_SSP:
174551eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1746e5262d05SWeike Chen 			break;
1747e5262d05SWeike Chen 		default:
174851eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1749e5262d05SWeike Chen 			break;
1750e5262d05SWeike Chen 		}
1751e5262d05SWeike Chen 
1752ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1753ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1754ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1755ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1756ca632f55SGrant Likely 	} else {
175751eea52dSLubomir Rintel 		controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1758ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
17595928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1760ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1761ec93cb6fSLubomir Rintel 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1762ec93cb6fSLubomir Rintel 						| SSSR_ROR | SSSR_TUR;
1763ca632f55SGrant Likely 	}
1764ca632f55SGrant Likely 
1765ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1766ca632f55SGrant Likely 			drv_data);
1767ca632f55SGrant Likely 	if (status < 0) {
1768ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
176951eea52dSLubomir Rintel 		goto out_error_controller_alloc;
1770ca632f55SGrant Likely 	}
1771ca632f55SGrant Likely 
1772ca632f55SGrant Likely 	/* Setup DMA if requested */
1773ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1774cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1775cd7bed00SMika Westerberg 		if (status) {
17768b57b11bSFlavio Suligoi 			dev_warn(dev, "no DMA channels available, using PIO\n");
1777cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1778b6ced294SJarkko Nikula 		} else {
177951eea52dSLubomir Rintel 			controller->can_dma = pxa2xx_spi_can_dma;
1780bf9f742cSMark Brown 			controller->max_dma_len = MAX_DMA_LEN;
1781b2662a16SDaniel Vetter 			controller->max_transfer_size =
1782b2662a16SDaniel Vetter 				pxa2xx_spi_max_dma_transfer_size;
1783ca632f55SGrant Likely 		}
1784ca632f55SGrant Likely 	}
1785ca632f55SGrant Likely 
1786ca632f55SGrant Likely 	/* Enable SOC clock */
178762bbc864STobias Jordan 	status = clk_prepare_enable(ssp->clk);
178862bbc864STobias Jordan 	if (status)
178962bbc864STobias Jordan 		goto out_error_dma_irq_alloc;
17903343b7a6SMika Westerberg 
179151eea52dSLubomir Rintel 	controller->max_speed_hz = clk_get_rate(ssp->clk);
179223cdddb2SJarkko Nikula 	/*
179323cdddb2SJarkko Nikula 	 * Set minimum speed for all other platforms than Intel Quark which is
179423cdddb2SJarkko Nikula 	 * able do under 1 Hz transfers.
179523cdddb2SJarkko Nikula 	 */
179623cdddb2SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
179723cdddb2SJarkko Nikula 		controller->min_speed_hz =
179823cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 4096);
179923cdddb2SJarkko Nikula 	else if (!is_quark_x1000_ssp(drv_data))
180023cdddb2SJarkko Nikula 		controller->min_speed_hz =
180123cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 512);
1802ca632f55SGrant Likely 
18030c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
18040c8ccd8bSAndy Shevchenko 
1805ca632f55SGrant Likely 	/* Load default SSP configuration */
1806e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1807e5262d05SWeike Chen 	case QUARK_X1000_SSP:
18087c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
18097c7289a4SAndy Shevchenko 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1810c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1811e5262d05SWeike Chen 
1812e5262d05SWeike Chen 		/* using the Motorola SPI protocol and use 8 bit frame */
18137c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
18147c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1815e5262d05SWeike Chen 		break;
18167c7289a4SAndy Shevchenko 	case CE4100_SSP:
18177c7289a4SAndy Shevchenko 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
18187c7289a4SAndy Shevchenko 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
18197c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
18207c7289a4SAndy Shevchenko 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
18217c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1822a2dd8af0SAndy Shevchenko 		break;
1823e5262d05SWeike Chen 	default:
1824ec93cb6fSLubomir Rintel 
182551eea52dSLubomir Rintel 		if (spi_controller_is_slave(controller)) {
1826ec93cb6fSLubomir Rintel 			tmp = SSCR1_SCFR |
1827ec93cb6fSLubomir Rintel 			      SSCR1_SCLKDIR |
1828ec93cb6fSLubomir Rintel 			      SSCR1_SFRMDIR |
1829ec93cb6fSLubomir Rintel 			      SSCR1_RxTresh(2) |
1830ec93cb6fSLubomir Rintel 			      SSCR1_TxTresh(1) |
1831ec93cb6fSLubomir Rintel 			      SSCR1_SPH;
1832ec93cb6fSLubomir Rintel 		} else {
1833c039dd27SJarkko Nikula 			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1834c039dd27SJarkko Nikula 			      SSCR1_TxTresh(TX_THRESH_DFLT);
1835ec93cb6fSLubomir Rintel 		}
1836c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1837ec93cb6fSLubomir Rintel 		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
183851eea52dSLubomir Rintel 		if (!spi_controller_is_slave(controller))
1839ec93cb6fSLubomir Rintel 			tmp |= SSCR0_SCR(2);
1840c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1841e5262d05SWeike Chen 		break;
1842e5262d05SWeike Chen 	}
1843e5262d05SWeike Chen 
1844ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1845c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1846e5262d05SWeike Chen 
1847e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1848c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1849ca632f55SGrant Likely 
18508b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
18518b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
18528b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
18538b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
18548b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
18558b136baaSJarkko Nikula 						   config->reg_capabilities);
18568b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
18578b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
18588b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
185930f3a6abSMika Westerberg 		} else if (config->cs_num) {
186030f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
18618b136baaSJarkko Nikula 		}
18628b136baaSJarkko Nikula 	}
186351eea52dSLubomir Rintel 	controller->num_chipselect = platform_info->num_chipselect;
18648b136baaSJarkko Nikula 
186599f499cdSMika Westerberg 	count = gpiod_count(&pdev->dev, "cs");
18666ac5a435SAndy Shevchenko 	if (count > 0) {
18676ac5a435SAndy Shevchenko 		int i;
18686ac5a435SAndy Shevchenko 
186951eea52dSLubomir Rintel 		controller->num_chipselect = max_t(int, count,
187051eea52dSLubomir Rintel 			controller->num_chipselect);
187199f499cdSMika Westerberg 
18726ac5a435SAndy Shevchenko 		drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
187351eea52dSLubomir Rintel 			controller->num_chipselect, sizeof(struct gpio_desc *),
18746ac5a435SAndy Shevchenko 			GFP_KERNEL);
18756ac5a435SAndy Shevchenko 		if (!drv_data->cs_gpiods) {
18766ac5a435SAndy Shevchenko 			status = -ENOMEM;
18776ac5a435SAndy Shevchenko 			goto out_error_clock_enabled;
18786ac5a435SAndy Shevchenko 		}
18796ac5a435SAndy Shevchenko 
188051eea52dSLubomir Rintel 		for (i = 0; i < controller->num_chipselect; i++) {
18816ac5a435SAndy Shevchenko 			struct gpio_desc *gpiod;
18826ac5a435SAndy Shevchenko 
1883d35f2dc9SAndy Shevchenko 			gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
18846ac5a435SAndy Shevchenko 			if (IS_ERR(gpiod)) {
18856ac5a435SAndy Shevchenko 				/* Means use native chip select */
18866ac5a435SAndy Shevchenko 				if (PTR_ERR(gpiod) == -ENOENT)
18876ac5a435SAndy Shevchenko 					continue;
18886ac5a435SAndy Shevchenko 
188977d33897SLubomir Rintel 				status = PTR_ERR(gpiod);
18906ac5a435SAndy Shevchenko 				goto out_error_clock_enabled;
18916ac5a435SAndy Shevchenko 			} else {
18926ac5a435SAndy Shevchenko 				drv_data->cs_gpiods[i] = gpiod;
18936ac5a435SAndy Shevchenko 			}
18946ac5a435SAndy Shevchenko 		}
18956ac5a435SAndy Shevchenko 	}
18966ac5a435SAndy Shevchenko 
189777d33897SLubomir Rintel 	if (platform_info->is_slave) {
189877d33897SLubomir Rintel 		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
189977d33897SLubomir Rintel 						"ready", GPIOD_OUT_LOW);
190077d33897SLubomir Rintel 		if (IS_ERR(drv_data->gpiod_ready)) {
190177d33897SLubomir Rintel 			status = PTR_ERR(drv_data->gpiod_ready);
190277d33897SLubomir Rintel 			goto out_error_clock_enabled;
190377d33897SLubomir Rintel 		}
190477d33897SLubomir Rintel 	}
190577d33897SLubomir Rintel 
1906836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1907836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1908836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1909836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1910836d1a22SAntonio Ospite 
1911ca632f55SGrant Likely 	/* Register with the SPI framework */
1912ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
191332e5b572SLukas Wunner 	status = spi_register_controller(controller);
1914ca632f55SGrant Likely 	if (status != 0) {
191551eea52dSLubomir Rintel 		dev_err(&pdev->dev, "problem registering spi controller\n");
191612742045SLubomir Rintel 		goto out_error_pm_runtime_enabled;
1917ca632f55SGrant Likely 	}
1918ca632f55SGrant Likely 
1919ca632f55SGrant Likely 	return status;
1920ca632f55SGrant Likely 
192112742045SLubomir Rintel out_error_pm_runtime_enabled:
1922e2b714afSJarkko Nikula 	pm_runtime_disable(&pdev->dev);
192312742045SLubomir Rintel 
192412742045SLubomir Rintel out_error_clock_enabled:
19253343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
192662bbc864STobias Jordan 
192762bbc864STobias Jordan out_error_dma_irq_alloc:
1928cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1929ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1930ca632f55SGrant Likely 
193151eea52dSLubomir Rintel out_error_controller_alloc:
1932ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1933ca632f55SGrant Likely 	return status;
1934ca632f55SGrant Likely }
1935ca632f55SGrant Likely 
1936ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1937ca632f55SGrant Likely {
1938ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
19393d24b2a4SAndy Shevchenko 	struct ssp_device *ssp = drv_data->ssp;
1940ca632f55SGrant Likely 
19417d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
19427d94a505SMika Westerberg 
194332e5b572SLukas Wunner 	spi_unregister_controller(drv_data->controller);
194432e5b572SLukas Wunner 
1945ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
19460c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
19473343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1948ca632f55SGrant Likely 
1949ca632f55SGrant Likely 	/* Release DMA */
195051eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma)
1951cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1952ca632f55SGrant Likely 
19537d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
19547d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
19557d94a505SMika Westerberg 
1956ca632f55SGrant Likely 	/* Release IRQ */
1957ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1958ca632f55SGrant Likely 
1959ca632f55SGrant Likely 	/* Release SSP */
1960ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1961ca632f55SGrant Likely 
1962ca632f55SGrant Likely 	return 0;
1963ca632f55SGrant Likely }
1964ca632f55SGrant Likely 
1965382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP
1966ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1967ca632f55SGrant Likely {
1968ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1969ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1970bffc967eSJarkko Nikula 	int status;
1971ca632f55SGrant Likely 
197251eea52dSLubomir Rintel 	status = spi_controller_suspend(drv_data->controller);
1973ca632f55SGrant Likely 	if (status != 0)
1974ca632f55SGrant Likely 		return status;
19750c8ccd8bSAndy Shevchenko 
19760c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
19772b9375b9SDmitry Eremin-Solenikov 
19782b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
19793343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1980ca632f55SGrant Likely 
1981ca632f55SGrant Likely 	return 0;
1982ca632f55SGrant Likely }
1983ca632f55SGrant Likely 
1984ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1985ca632f55SGrant Likely {
1986ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1987ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1988bffc967eSJarkko Nikula 	int status;
1989ca632f55SGrant Likely 
1990ca632f55SGrant Likely 	/* Enable the SSP clock */
199162bbc864STobias Jordan 	if (!pm_runtime_suspended(dev)) {
199262bbc864STobias Jordan 		status = clk_prepare_enable(ssp->clk);
199362bbc864STobias Jordan 		if (status)
199462bbc864STobias Jordan 			return status;
199562bbc864STobias Jordan 	}
1996ca632f55SGrant Likely 
1997ca632f55SGrant Likely 	/* Start the queue running */
199851eea52dSLubomir Rintel 	return spi_controller_resume(drv_data->controller);
1999ca632f55SGrant Likely }
20007d94a505SMika Westerberg #endif
20017d94a505SMika Westerberg 
2002ec833050SRafael J. Wysocki #ifdef CONFIG_PM
20037d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
20047d94a505SMika Westerberg {
20057d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
20067d94a505SMika Westerberg 
20077d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
20087d94a505SMika Westerberg 	return 0;
20097d94a505SMika Westerberg }
20107d94a505SMika Westerberg 
20117d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
20127d94a505SMika Westerberg {
20137d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
201462bbc864STobias Jordan 	int status;
20157d94a505SMika Westerberg 
201662bbc864STobias Jordan 	status = clk_prepare_enable(drv_data->ssp->clk);
201762bbc864STobias Jordan 	return status;
20187d94a505SMika Westerberg }
20197d94a505SMika Westerberg #endif
2020ca632f55SGrant Likely 
2021ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
20227d94a505SMika Westerberg 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
20237d94a505SMika Westerberg 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
20247d94a505SMika Westerberg 			   pxa2xx_spi_runtime_resume, NULL)
2025ca632f55SGrant Likely };
2026ca632f55SGrant Likely 
2027ca632f55SGrant Likely static struct platform_driver driver = {
2028ca632f55SGrant Likely 	.driver = {
2029ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
2030ca632f55SGrant Likely 		.pm	= &pxa2xx_spi_pm_ops,
2031a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
203287ae1d2dSLubomir Rintel 		.of_match_table = of_match_ptr(pxa2xx_spi_of_match),
2033ca632f55SGrant Likely 	},
2034ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
2035ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
2036ca632f55SGrant Likely };
2037ca632f55SGrant Likely 
2038ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
2039ca632f55SGrant Likely {
2040ca632f55SGrant Likely 	return platform_driver_register(&driver);
2041ca632f55SGrant Likely }
2042ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
2043ca632f55SGrant Likely 
2044ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
2045ca632f55SGrant Likely {
2046ca632f55SGrant Likely 	platform_driver_unregister(&driver);
2047ca632f55SGrant Likely }
2048ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
204951ebf6acSFlavio Suligoi 
205051ebf6acSFlavio Suligoi MODULE_SOFTDEP("pre: dw_dmac");
2051