xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision 44ec41b7f7831f91c79a06de5e45f2d7ce6e4fbd)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
48083d6b8SAndy Shevchenko  * Copyright (C) 2013, 2021 Intel Corporation
5ca632f55SGrant Likely  */
6ca632f55SGrant Likely 
75ce25705SAndy Shevchenko #include <linux/acpi.h>
88b136baaSJarkko Nikula #include <linux/bitops.h>
95ce25705SAndy Shevchenko #include <linux/clk.h>
105ce25705SAndy Shevchenko #include <linux/delay.h>
11ca632f55SGrant Likely #include <linux/device.h>
120e476871SAndy Shevchenko #include <linux/dmaengine.h>
13cbfd6a21SSachin Kamat #include <linux/err.h>
145ce25705SAndy Shevchenko #include <linux/errno.h>
155ce25705SAndy Shevchenko #include <linux/gpio/consumer.h>
165ce25705SAndy Shevchenko #include <linux/gpio.h>
175ce25705SAndy Shevchenko #include <linux/init.h>
18ca632f55SGrant Likely #include <linux/interrupt.h>
195ce25705SAndy Shevchenko #include <linux/ioport.h>
209df461ecSAndy Shevchenko #include <linux/kernel.h>
215ce25705SAndy Shevchenko #include <linux/module.h>
22ae8fbf1dSAndy Shevchenko #include <linux/mod_devicetable.h>
23ae8fbf1dSAndy Shevchenko #include <linux/of.h>
2434cadd9cSJarkko Nikula #include <linux/pci.h>
25ca632f55SGrant Likely #include <linux/platform_device.h>
265ce25705SAndy Shevchenko #include <linux/pm_runtime.h>
27f2faa3ecSAndy Shevchenko #include <linux/property.h>
285ce25705SAndy Shevchenko #include <linux/slab.h>
290e476871SAndy Shevchenko 
30ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h>
31ca632f55SGrant Likely #include <linux/spi/spi.h>
32ca632f55SGrant Likely 
33cd7bed00SMika Westerberg #include "spi-pxa2xx.h"
34ca632f55SGrant Likely 
35ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street");
36ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
37ca632f55SGrant Likely MODULE_LICENSE("GPL");
38ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi");
39ca632f55SGrant Likely 
40ca632f55SGrant Likely #define TIMOUT_DFLT		1000
41ca632f55SGrant Likely 
42ca632f55SGrant Likely /*
438083d6b8SAndy Shevchenko  * For testing SSCR1 changes that require SSP restart, basically
448083d6b8SAndy Shevchenko  * everything except the service and interrupt enables, the PXA270 developer
45ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
468083d6b8SAndy Shevchenko  * list, but the PXA255 developer manual says all bits without really meaning
478083d6b8SAndy Shevchenko  * the service and interrupt enables.
48ca632f55SGrant Likely  */
49ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
50ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
51ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
52ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
53ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
54ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
55ca632f55SGrant Likely 
56e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
57e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
58e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
59e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
60e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
61e5262d05SWeike Chen 
627c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
637c7289a4SAndy Shevchenko 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
647c7289a4SAndy Shevchenko 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
657c7289a4SAndy Shevchenko 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
667c7289a4SAndy Shevchenko 				| CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
677c7289a4SAndy Shevchenko 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
687c7289a4SAndy Shevchenko 
69624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
70624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE			BIT(0)
71624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
728b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT			9
738b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
74a0d2642eSMika Westerberg 
75683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE 0x38
76683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
77683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
78683f65deSEvan Green 
79dccf7369SJarkko Nikula struct lpss_config {
80dccf7369SJarkko Nikula 	/* LPSS offset from drv_data->ioaddr */
81dccf7369SJarkko Nikula 	unsigned offset;
82dccf7369SJarkko Nikula 	/* Register offsets from drv_data->lpss_base or -1 */
83dccf7369SJarkko Nikula 	int reg_general;
84dccf7369SJarkko Nikula 	int reg_ssp;
85dccf7369SJarkko Nikula 	int reg_cs_ctrl;
868b136baaSJarkko Nikula 	int reg_capabilities;
87dccf7369SJarkko Nikula 	/* FIFO thresholds */
88dccf7369SJarkko Nikula 	u32 rx_threshold;
89dccf7369SJarkko Nikula 	u32 tx_threshold_lo;
90dccf7369SJarkko Nikula 	u32 tx_threshold_hi;
91c1e4a53cSMika Westerberg 	/* Chip select control */
92c1e4a53cSMika Westerberg 	unsigned cs_sel_shift;
93c1e4a53cSMika Westerberg 	unsigned cs_sel_mask;
9430f3a6abSMika Westerberg 	unsigned cs_num;
95683f65deSEvan Green 	/* Quirks */
96683f65deSEvan Green 	unsigned cs_clk_stays_gated : 1;
97dccf7369SJarkko Nikula };
98dccf7369SJarkko Nikula 
99dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */
100dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = {
101dccf7369SJarkko Nikula 	{	/* LPSS_LPT_SSP */
102dccf7369SJarkko Nikula 		.offset = 0x800,
103dccf7369SJarkko Nikula 		.reg_general = 0x08,
104dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
105dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1068b136baaSJarkko Nikula 		.reg_capabilities = -1,
107dccf7369SJarkko Nikula 		.rx_threshold = 64,
108dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
109dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
110dccf7369SJarkko Nikula 	},
111dccf7369SJarkko Nikula 	{	/* LPSS_BYT_SSP */
112dccf7369SJarkko Nikula 		.offset = 0x400,
113dccf7369SJarkko Nikula 		.reg_general = 0x08,
114dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
115dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1168b136baaSJarkko Nikula 		.reg_capabilities = -1,
117dccf7369SJarkko Nikula 		.rx_threshold = 64,
118dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
119dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
120dccf7369SJarkko Nikula 	},
12130f3a6abSMika Westerberg 	{	/* LPSS_BSW_SSP */
12230f3a6abSMika Westerberg 		.offset = 0x400,
12330f3a6abSMika Westerberg 		.reg_general = 0x08,
12430f3a6abSMika Westerberg 		.reg_ssp = 0x0c,
12530f3a6abSMika Westerberg 		.reg_cs_ctrl = 0x18,
12630f3a6abSMika Westerberg 		.reg_capabilities = -1,
12730f3a6abSMika Westerberg 		.rx_threshold = 64,
12830f3a6abSMika Westerberg 		.tx_threshold_lo = 160,
12930f3a6abSMika Westerberg 		.tx_threshold_hi = 224,
13030f3a6abSMika Westerberg 		.cs_sel_shift = 2,
13130f3a6abSMika Westerberg 		.cs_sel_mask = 1 << 2,
13230f3a6abSMika Westerberg 		.cs_num = 2,
13330f3a6abSMika Westerberg 	},
13434cadd9cSJarkko Nikula 	{	/* LPSS_SPT_SSP */
13534cadd9cSJarkko Nikula 		.offset = 0x200,
13634cadd9cSJarkko Nikula 		.reg_general = -1,
13734cadd9cSJarkko Nikula 		.reg_ssp = 0x20,
13834cadd9cSJarkko Nikula 		.reg_cs_ctrl = 0x24,
13966ec246eSJarkko Nikula 		.reg_capabilities = -1,
14034cadd9cSJarkko Nikula 		.rx_threshold = 1,
14134cadd9cSJarkko Nikula 		.tx_threshold_lo = 32,
14234cadd9cSJarkko Nikula 		.tx_threshold_hi = 56,
14334cadd9cSJarkko Nikula 	},
144b7c08cf8SJarkko Nikula 	{	/* LPSS_BXT_SSP */
145b7c08cf8SJarkko Nikula 		.offset = 0x200,
146b7c08cf8SJarkko Nikula 		.reg_general = -1,
147b7c08cf8SJarkko Nikula 		.reg_ssp = 0x20,
148b7c08cf8SJarkko Nikula 		.reg_cs_ctrl = 0x24,
149b7c08cf8SJarkko Nikula 		.reg_capabilities = 0xfc,
150b7c08cf8SJarkko Nikula 		.rx_threshold = 1,
151b7c08cf8SJarkko Nikula 		.tx_threshold_lo = 16,
152b7c08cf8SJarkko Nikula 		.tx_threshold_hi = 48,
153c1e4a53cSMika Westerberg 		.cs_sel_shift = 8,
154c1e4a53cSMika Westerberg 		.cs_sel_mask = 3 << 8,
1556eefaee4SEvan Green 		.cs_clk_stays_gated = true,
156b7c08cf8SJarkko Nikula 	},
157fc0b2accSJarkko Nikula 	{	/* LPSS_CNL_SSP */
158fc0b2accSJarkko Nikula 		.offset = 0x200,
159fc0b2accSJarkko Nikula 		.reg_general = -1,
160fc0b2accSJarkko Nikula 		.reg_ssp = 0x20,
161fc0b2accSJarkko Nikula 		.reg_cs_ctrl = 0x24,
162fc0b2accSJarkko Nikula 		.reg_capabilities = 0xfc,
163fc0b2accSJarkko Nikula 		.rx_threshold = 1,
164fc0b2accSJarkko Nikula 		.tx_threshold_lo = 32,
165fc0b2accSJarkko Nikula 		.tx_threshold_hi = 56,
166fc0b2accSJarkko Nikula 		.cs_sel_shift = 8,
167fc0b2accSJarkko Nikula 		.cs_sel_mask = 3 << 8,
168683f65deSEvan Green 		.cs_clk_stays_gated = true,
169fc0b2accSJarkko Nikula 	},
170dccf7369SJarkko Nikula };
171dccf7369SJarkko Nikula 
172dccf7369SJarkko Nikula static inline const struct lpss_config
173dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data)
174dccf7369SJarkko Nikula {
175dccf7369SJarkko Nikula 	return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
176dccf7369SJarkko Nikula }
177dccf7369SJarkko Nikula 
178a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data)
179a0d2642eSMika Westerberg {
18003fbf488SJarkko Nikula 	switch (drv_data->ssp_type) {
18103fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
18203fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
18330f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
18434cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
185b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
186fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
18703fbf488SJarkko Nikula 		return true;
18803fbf488SJarkko Nikula 	default:
18903fbf488SJarkko Nikula 		return false;
19003fbf488SJarkko Nikula 	}
191a0d2642eSMika Westerberg }
192a0d2642eSMika Westerberg 
193e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
194e5262d05SWeike Chen {
195e5262d05SWeike Chen 	return drv_data->ssp_type == QUARK_X1000_SSP;
196e5262d05SWeike Chen }
197e5262d05SWeike Chen 
19841c98841SAndy Shevchenko static bool is_mmp2_ssp(const struct driver_data *drv_data)
19941c98841SAndy Shevchenko {
20041c98841SAndy Shevchenko 	return drv_data->ssp_type == MMP2_SSP;
20141c98841SAndy Shevchenko }
20241c98841SAndy Shevchenko 
2033fdb59cfSAndy Shevchenko static bool is_mrfld_ssp(const struct driver_data *drv_data)
2043fdb59cfSAndy Shevchenko {
2053fdb59cfSAndy Shevchenko 	return drv_data->ssp_type == MRFLD_SSP;
2063fdb59cfSAndy Shevchenko }
2073fdb59cfSAndy Shevchenko 
2081bed378cSAndy Shevchenko static void pxa2xx_spi_update(const struct driver_data *drv_data, u32 reg, u32 mask, u32 value)
2091bed378cSAndy Shevchenko {
2101bed378cSAndy Shevchenko 	if ((pxa2xx_spi_read(drv_data, reg) & mask) != value)
2111bed378cSAndy Shevchenko 		pxa2xx_spi_write(drv_data, reg, value & mask);
2121bed378cSAndy Shevchenko }
2131bed378cSAndy Shevchenko 
2144fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
2154fdb2424SWeike Chen {
2164fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
217e5262d05SWeike Chen 	case QUARK_X1000_SSP:
218e5262d05SWeike Chen 		return QUARK_X1000_SSCR1_CHANGE_MASK;
2197c7289a4SAndy Shevchenko 	case CE4100_SSP:
2207c7289a4SAndy Shevchenko 		return CE4100_SSCR1_CHANGE_MASK;
2214fdb2424SWeike Chen 	default:
2224fdb2424SWeike Chen 		return SSCR1_CHANGE_MASK;
2234fdb2424SWeike Chen 	}
2244fdb2424SWeike Chen }
2254fdb2424SWeike Chen 
2264fdb2424SWeike Chen static u32
2274fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
2284fdb2424SWeike Chen {
2294fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
230e5262d05SWeike Chen 	case QUARK_X1000_SSP:
231e5262d05SWeike Chen 		return RX_THRESH_QUARK_X1000_DFLT;
2327c7289a4SAndy Shevchenko 	case CE4100_SSP:
2337c7289a4SAndy Shevchenko 		return RX_THRESH_CE4100_DFLT;
2344fdb2424SWeike Chen 	default:
2354fdb2424SWeike Chen 		return RX_THRESH_DFLT;
2364fdb2424SWeike Chen 	}
2374fdb2424SWeike Chen }
2384fdb2424SWeike Chen 
2394fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
2404fdb2424SWeike Chen {
2414fdb2424SWeike Chen 	u32 mask;
2424fdb2424SWeike Chen 
2434fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
244e5262d05SWeike Chen 	case QUARK_X1000_SSP:
245e5262d05SWeike Chen 		mask = QUARK_X1000_SSSR_TFL_MASK;
246e5262d05SWeike Chen 		break;
2477c7289a4SAndy Shevchenko 	case CE4100_SSP:
2487c7289a4SAndy Shevchenko 		mask = CE4100_SSSR_TFL_MASK;
2497c7289a4SAndy Shevchenko 		break;
2504fdb2424SWeike Chen 	default:
2514fdb2424SWeike Chen 		mask = SSSR_TFL_MASK;
2524fdb2424SWeike Chen 		break;
2534fdb2424SWeike Chen 	}
2544fdb2424SWeike Chen 
2556d380132SAndy Shevchenko 	return read_SSSR_bits(drv_data, mask) == mask;
2564fdb2424SWeike Chen }
2574fdb2424SWeike Chen 
2584fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
2594fdb2424SWeike Chen 				     u32 *sccr1_reg)
2604fdb2424SWeike Chen {
2614fdb2424SWeike Chen 	u32 mask;
2624fdb2424SWeike Chen 
2634fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
264e5262d05SWeike Chen 	case QUARK_X1000_SSP:
265e5262d05SWeike Chen 		mask = QUARK_X1000_SSCR1_RFT;
266e5262d05SWeike Chen 		break;
2677c7289a4SAndy Shevchenko 	case CE4100_SSP:
2687c7289a4SAndy Shevchenko 		mask = CE4100_SSCR1_RFT;
2697c7289a4SAndy Shevchenko 		break;
2704fdb2424SWeike Chen 	default:
2714fdb2424SWeike Chen 		mask = SSCR1_RFT;
2724fdb2424SWeike Chen 		break;
2734fdb2424SWeike Chen 	}
2744fdb2424SWeike Chen 	*sccr1_reg &= ~mask;
2754fdb2424SWeike Chen }
2764fdb2424SWeike Chen 
2774fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
2784fdb2424SWeike Chen 				   u32 *sccr1_reg, u32 threshold)
2794fdb2424SWeike Chen {
2804fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
281e5262d05SWeike Chen 	case QUARK_X1000_SSP:
282e5262d05SWeike Chen 		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
283e5262d05SWeike Chen 		break;
2847c7289a4SAndy Shevchenko 	case CE4100_SSP:
2857c7289a4SAndy Shevchenko 		*sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
2867c7289a4SAndy Shevchenko 		break;
2874fdb2424SWeike Chen 	default:
2884fdb2424SWeike Chen 		*sccr1_reg |= SSCR1_RxTresh(threshold);
2894fdb2424SWeike Chen 		break;
2904fdb2424SWeike Chen 	}
2914fdb2424SWeike Chen }
2924fdb2424SWeike Chen 
2934fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
2944fdb2424SWeike Chen 				  u32 clk_div, u8 bits)
2954fdb2424SWeike Chen {
2964fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
297e5262d05SWeike Chen 	case QUARK_X1000_SSP:
298e5262d05SWeike Chen 		return clk_div
299e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_Motorola
3000c8ccd8bSAndy Shevchenko 			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits);
3014fdb2424SWeike Chen 	default:
3024fdb2424SWeike Chen 		return clk_div
3034fdb2424SWeike Chen 			| SSCR0_Motorola
3044fdb2424SWeike Chen 			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
3054fdb2424SWeike Chen 			| (bits > 16 ? SSCR0_EDSS : 0);
3064fdb2424SWeike Chen 	}
3074fdb2424SWeike Chen }
3084fdb2424SWeike Chen 
309a0d2642eSMika Westerberg /*
310a0d2642eSMika Westerberg  * Read and write LPSS SSP private registers. Caller must first check that
311a0d2642eSMika Westerberg  * is_lpss_ssp() returns true before these can be called.
312a0d2642eSMika Westerberg  */
313a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
314a0d2642eSMika Westerberg {
315a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
316a0d2642eSMika Westerberg 	return readl(drv_data->lpss_base + offset);
317a0d2642eSMika Westerberg }
318a0d2642eSMika Westerberg 
319a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data,
320a0d2642eSMika Westerberg 				  unsigned offset, u32 value)
321a0d2642eSMika Westerberg {
322a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
323a0d2642eSMika Westerberg 	writel(value, drv_data->lpss_base + offset);
324a0d2642eSMika Westerberg }
325a0d2642eSMika Westerberg 
326a0d2642eSMika Westerberg /*
327a0d2642eSMika Westerberg  * lpss_ssp_setup - perform LPSS SSP specific setup
328a0d2642eSMika Westerberg  * @drv_data: pointer to the driver private data
329a0d2642eSMika Westerberg  *
330a0d2642eSMika Westerberg  * Perform LPSS SSP specific setup. This function must be called first if
331a0d2642eSMika Westerberg  * one is going to use LPSS SSP private registers.
332a0d2642eSMika Westerberg  */
333a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data)
334a0d2642eSMika Westerberg {
335dccf7369SJarkko Nikula 	const struct lpss_config *config;
336dccf7369SJarkko Nikula 	u32 value;
337a0d2642eSMika Westerberg 
338dccf7369SJarkko Nikula 	config = lpss_get_config(drv_data);
3399e43c9a8SAndy Shevchenko 	drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset;
340a0d2642eSMika Westerberg 
341a0d2642eSMika Westerberg 	/* Enable software chip select control */
3420e897218SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
343624ea72eSJarkko Nikula 	value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
344624ea72eSJarkko Nikula 	value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
345dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
3460054e28dSMika Westerberg 
3470054e28dSMika Westerberg 	/* Enable multiblock DMA transfers */
34851eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma) {
349dccf7369SJarkko Nikula 		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
3501de70612SMika Westerberg 
35182ba2c2aSJarkko Nikula 		if (config->reg_general >= 0) {
35282ba2c2aSJarkko Nikula 			value = __lpss_ssp_read_priv(drv_data,
35382ba2c2aSJarkko Nikula 						     config->reg_general);
354624ea72eSJarkko Nikula 			value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
35582ba2c2aSJarkko Nikula 			__lpss_ssp_write_priv(drv_data,
35682ba2c2aSJarkko Nikula 					      config->reg_general, value);
35782ba2c2aSJarkko Nikula 		}
3581de70612SMika Westerberg 	}
359a0d2642eSMika Westerberg }
360a0d2642eSMika Westerberg 
361d5898e19SJarkko Nikula static void lpss_ssp_select_cs(struct spi_device *spi,
362c1e4a53cSMika Westerberg 			       const struct lpss_config *config)
363a0d2642eSMika Westerberg {
364d5898e19SJarkko Nikula 	struct driver_data *drv_data =
365d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
366d0283eb2SJarkko Nikula 	u32 value, cs;
367a0d2642eSMika Westerberg 
368c1e4a53cSMika Westerberg 	if (!config->cs_sel_mask)
369c1e4a53cSMika Westerberg 		return;
370dccf7369SJarkko Nikula 
371dccf7369SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
372c1e4a53cSMika Westerberg 
373d5898e19SJarkko Nikula 	cs = spi->chip_select;
374c1e4a53cSMika Westerberg 	cs <<= config->cs_sel_shift;
375c1e4a53cSMika Westerberg 	if (cs != (value & config->cs_sel_mask)) {
376d0283eb2SJarkko Nikula 		/*
377c1e4a53cSMika Westerberg 		 * When switching another chip select output active the
378c1e4a53cSMika Westerberg 		 * output must be selected first and wait 2 ssp_clk cycles
379c1e4a53cSMika Westerberg 		 * before changing state to active. Otherwise a short
380c1e4a53cSMika Westerberg 		 * glitch will occur on the previous chip select since
381c1e4a53cSMika Westerberg 		 * output select is latched but state control is not.
382d0283eb2SJarkko Nikula 		 */
383c1e4a53cSMika Westerberg 		value &= ~config->cs_sel_mask;
384d0283eb2SJarkko Nikula 		value |= cs;
385d0283eb2SJarkko Nikula 		__lpss_ssp_write_priv(drv_data,
386d0283eb2SJarkko Nikula 				      config->reg_cs_ctrl, value);
387d0283eb2SJarkko Nikula 		ndelay(1000000000 /
38851eea52dSLubomir Rintel 		       (drv_data->controller->max_speed_hz / 2));
389d0283eb2SJarkko Nikula 	}
390d0283eb2SJarkko Nikula }
391c1e4a53cSMika Westerberg 
392d5898e19SJarkko Nikula static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
393c1e4a53cSMika Westerberg {
394d5898e19SJarkko Nikula 	struct driver_data *drv_data =
395d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
396c1e4a53cSMika Westerberg 	const struct lpss_config *config;
397c1e4a53cSMika Westerberg 	u32 value;
398c1e4a53cSMika Westerberg 
399c1e4a53cSMika Westerberg 	config = lpss_get_config(drv_data);
400c1e4a53cSMika Westerberg 
401c1e4a53cSMika Westerberg 	if (enable)
402d5898e19SJarkko Nikula 		lpss_ssp_select_cs(spi, config);
403c1e4a53cSMika Westerberg 
404c1e4a53cSMika Westerberg 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
405c1e4a53cSMika Westerberg 	if (enable)
406c1e4a53cSMika Westerberg 		value &= ~LPSS_CS_CONTROL_CS_HIGH;
407c1e4a53cSMika Westerberg 	else
408c1e4a53cSMika Westerberg 		value |= LPSS_CS_CONTROL_CS_HIGH;
409dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
410683f65deSEvan Green 	if (config->cs_clk_stays_gated) {
411683f65deSEvan Green 		u32 clkgate;
412683f65deSEvan Green 
413683f65deSEvan Green 		/*
414683f65deSEvan Green 		 * Changing CS alone when dynamic clock gating is on won't
415683f65deSEvan Green 		 * actually flip CS at that time. This ruins SPI transfers
416683f65deSEvan Green 		 * that specify delays, or have no data. Toggle the clock mode
417683f65deSEvan Green 		 * to force on briefly to poke the CS pin to move.
418683f65deSEvan Green 		 */
419683f65deSEvan Green 		clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
420683f65deSEvan Green 		value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
421683f65deSEvan Green 			LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
422683f65deSEvan Green 
423683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
424683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
425683f65deSEvan Green 	}
426a0d2642eSMika Westerberg }
427a0d2642eSMika Westerberg 
428d5898e19SJarkko Nikula static void cs_assert(struct spi_device *spi)
429ca632f55SGrant Likely {
430d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
431d5898e19SJarkko Nikula 	struct driver_data *drv_data =
432d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
433ca632f55SGrant Likely 
434ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP) {
435ccd60b20SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSSR, spi->chip_select);
436ca632f55SGrant Likely 		return;
437ca632f55SGrant Likely 	}
438ca632f55SGrant Likely 
439ca632f55SGrant Likely 	if (chip->cs_control) {
440ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_ASSERT);
441ca632f55SGrant Likely 		return;
442ca632f55SGrant Likely 	}
443ca632f55SGrant Likely 
4447566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
445d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, true);
446ca632f55SGrant Likely }
447ca632f55SGrant Likely 
448d5898e19SJarkko Nikula static void cs_deassert(struct spi_device *spi)
449ca632f55SGrant Likely {
450d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
451d5898e19SJarkko Nikula 	struct driver_data *drv_data =
452d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
453104e51afSJarkko Nikula 	unsigned long timeout;
454ca632f55SGrant Likely 
455ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
456ca632f55SGrant Likely 		return;
457ca632f55SGrant Likely 
458104e51afSJarkko Nikula 	/* Wait until SSP becomes idle before deasserting the CS */
459104e51afSJarkko Nikula 	timeout = jiffies + msecs_to_jiffies(10);
460104e51afSJarkko Nikula 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
461104e51afSJarkko Nikula 	       !time_after(jiffies, timeout))
462104e51afSJarkko Nikula 		cpu_relax();
463104e51afSJarkko Nikula 
464ca632f55SGrant Likely 	if (chip->cs_control) {
465ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_DEASSERT);
466ca632f55SGrant Likely 		return;
467ca632f55SGrant Likely 	}
468ca632f55SGrant Likely 
4697566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
470d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, false);
471d5898e19SJarkko Nikula }
472d5898e19SJarkko Nikula 
473d5898e19SJarkko Nikula static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
474d5898e19SJarkko Nikula {
475d5898e19SJarkko Nikula 	if (level)
476d5898e19SJarkko Nikula 		cs_deassert(spi);
477d5898e19SJarkko Nikula 	else
478d5898e19SJarkko Nikula 		cs_assert(spi);
479ca632f55SGrant Likely }
480ca632f55SGrant Likely 
481cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
482ca632f55SGrant Likely {
483ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
484ca632f55SGrant Likely 
485ca632f55SGrant Likely 	do {
4866d380132SAndy Shevchenko 		while (read_SSSR_bits(drv_data, SSSR_RNE))
487c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
488c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
489ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
490ca632f55SGrant Likely 
491ca632f55SGrant Likely 	return limit;
492ca632f55SGrant Likely }
493ca632f55SGrant Likely 
49429d7e05cSLubomir Rintel static void pxa2xx_spi_off(struct driver_data *drv_data)
49529d7e05cSLubomir Rintel {
49641c98841SAndy Shevchenko 	/* On MMP, disabling SSE seems to corrupt the Rx FIFO */
49741c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data))
49829d7e05cSLubomir Rintel 		return;
49929d7e05cSLubomir Rintel 
5000c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(drv_data->ssp);
50129d7e05cSLubomir Rintel }
50229d7e05cSLubomir Rintel 
503ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
504ca632f55SGrant Likely {
505ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
506ca632f55SGrant Likely 
5074fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
508ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
509ca632f55SGrant Likely 		return 0;
510ca632f55SGrant Likely 
511c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
512ca632f55SGrant Likely 	drv_data->tx += n_bytes;
513ca632f55SGrant Likely 
514ca632f55SGrant Likely 	return 1;
515ca632f55SGrant Likely }
516ca632f55SGrant Likely 
517ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
518ca632f55SGrant Likely {
519ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
520ca632f55SGrant Likely 
5216d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
522c039dd27SJarkko Nikula 		pxa2xx_spi_read(drv_data, SSDR);
523ca632f55SGrant Likely 		drv_data->rx += n_bytes;
524ca632f55SGrant Likely 	}
525ca632f55SGrant Likely 
526ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
527ca632f55SGrant Likely }
528ca632f55SGrant Likely 
529ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data)
530ca632f55SGrant Likely {
5314fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
532ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
533ca632f55SGrant Likely 		return 0;
534ca632f55SGrant Likely 
535c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
536ca632f55SGrant Likely 	++drv_data->tx;
537ca632f55SGrant Likely 
538ca632f55SGrant Likely 	return 1;
539ca632f55SGrant Likely }
540ca632f55SGrant Likely 
541ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data)
542ca632f55SGrant Likely {
5436d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
544c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
545ca632f55SGrant Likely 		++drv_data->rx;
546ca632f55SGrant Likely 	}
547ca632f55SGrant Likely 
548ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
549ca632f55SGrant Likely }
550ca632f55SGrant Likely 
551ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
552ca632f55SGrant Likely {
5534fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
554ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
555ca632f55SGrant Likely 		return 0;
556ca632f55SGrant Likely 
557c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
558ca632f55SGrant Likely 	drv_data->tx += 2;
559ca632f55SGrant Likely 
560ca632f55SGrant Likely 	return 1;
561ca632f55SGrant Likely }
562ca632f55SGrant Likely 
563ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
564ca632f55SGrant Likely {
5656d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
566c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
567ca632f55SGrant Likely 		drv_data->rx += 2;
568ca632f55SGrant Likely 	}
569ca632f55SGrant Likely 
570ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
571ca632f55SGrant Likely }
572ca632f55SGrant Likely 
573ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
574ca632f55SGrant Likely {
5754fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
576ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
577ca632f55SGrant Likely 		return 0;
578ca632f55SGrant Likely 
579c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
580ca632f55SGrant Likely 	drv_data->tx += 4;
581ca632f55SGrant Likely 
582ca632f55SGrant Likely 	return 1;
583ca632f55SGrant Likely }
584ca632f55SGrant Likely 
585ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
586ca632f55SGrant Likely {
5876d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
588c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
589ca632f55SGrant Likely 		drv_data->rx += 4;
590ca632f55SGrant Likely 	}
591ca632f55SGrant Likely 
592ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
593ca632f55SGrant Likely }
594ca632f55SGrant Likely 
595ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
596ca632f55SGrant Likely {
597e3aa9accSAndy Shevchenko 	u32 mask = drv_data->int_cr1 | drv_data->dma_cr1, threshold;
598e3aa9accSAndy Shevchenko 	struct chip_data *chip;
599e3aa9accSAndy Shevchenko 
600e3aa9accSAndy Shevchenko 	if (drv_data->controller->cur_msg) {
601e3aa9accSAndy Shevchenko 		chip = spi_get_ctldata(drv_data->controller->cur_msg->spi);
602e3aa9accSAndy Shevchenko 		threshold = chip->threshold;
603e3aa9accSAndy Shevchenko 	} else {
604e3aa9accSAndy Shevchenko 		threshold = 0;
605e3aa9accSAndy Shevchenko 	}
606ca632f55SGrant Likely 
607152bc19eSAndy Shevchenko 	switch (drv_data->ssp_type) {
608152bc19eSAndy Shevchenko 	case QUARK_X1000_SSP:
609e0a6512dSAndy Shevchenko 		mask |= QUARK_X1000_SSCR1_RFT;
610152bc19eSAndy Shevchenko 		break;
6117c7289a4SAndy Shevchenko 	case CE4100_SSP:
612e0a6512dSAndy Shevchenko 		mask |= CE4100_SSCR1_RFT;
6137c7289a4SAndy Shevchenko 		break;
614152bc19eSAndy Shevchenko 	default:
615e0a6512dSAndy Shevchenko 		mask |= SSCR1_RFT;
616152bc19eSAndy Shevchenko 		break;
617152bc19eSAndy Shevchenko 	}
618e0a6512dSAndy Shevchenko 
619e3aa9accSAndy Shevchenko 	pxa2xx_spi_update(drv_data, SSCR1, mask, threshold);
620ca632f55SGrant Likely }
621ca632f55SGrant Likely 
622ab77fe89SAndy Shevchenko static void int_stop_and_reset(struct driver_data *drv_data)
623ca632f55SGrant Likely {
624ab77fe89SAndy Shevchenko 	/* Clear and disable interrupts */
625ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
626ca632f55SGrant Likely 	reset_sccr1(drv_data);
627ab77fe89SAndy Shevchenko 	if (pxa25x_ssp_comp(drv_data))
628ab77fe89SAndy Shevchenko 		return;
629ab77fe89SAndy Shevchenko 
630c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSTO, 0);
631ab77fe89SAndy Shevchenko }
632ab77fe89SAndy Shevchenko 
6334761d2e7SAndy Shevchenko static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
634ab77fe89SAndy Shevchenko {
635ab77fe89SAndy Shevchenko 	int_stop_and_reset(drv_data);
636cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
63729d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
638ca632f55SGrant Likely 
639c3dce24cSAndy Shevchenko 	dev_err(drv_data->ssp->dev, "%s\n", msg);
640ca632f55SGrant Likely 
6414761d2e7SAndy Shevchenko 	drv_data->controller->cur_msg->status = err;
64251eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
643ca632f55SGrant Likely }
644ca632f55SGrant Likely 
645ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
646ca632f55SGrant Likely {
647ab77fe89SAndy Shevchenko 	int_stop_and_reset(drv_data);
648ca632f55SGrant Likely 
64951eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
650ca632f55SGrant Likely }
651ca632f55SGrant Likely 
652ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
653ca632f55SGrant Likely {
6546d380132SAndy Shevchenko 	u32 irq_status;
655ca632f55SGrant Likely 
6566d380132SAndy Shevchenko 	irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr);
6576d380132SAndy Shevchenko 	if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE))
6586d380132SAndy Shevchenko 		irq_status &= ~SSSR_TFS;
659ca632f55SGrant Likely 
660ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
6618083d6b8SAndy Shevchenko 		int_error_stop(drv_data, "interrupt_transfer: FIFO overrun", -EIO);
662ca632f55SGrant Likely 		return IRQ_HANDLED;
663ca632f55SGrant Likely 	}
664ca632f55SGrant Likely 
665ec93cb6fSLubomir Rintel 	if (irq_status & SSSR_TUR) {
6668083d6b8SAndy Shevchenko 		int_error_stop(drv_data, "interrupt_transfer: FIFO underrun", -EIO);
667ec93cb6fSLubomir Rintel 		return IRQ_HANDLED;
668ec93cb6fSLubomir Rintel 	}
669ec93cb6fSLubomir Rintel 
670ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
671c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
672ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
673ca632f55SGrant Likely 			int_transfer_complete(drv_data);
674ca632f55SGrant Likely 			return IRQ_HANDLED;
675ca632f55SGrant Likely 		}
676ca632f55SGrant Likely 	}
677ca632f55SGrant Likely 
6788083d6b8SAndy Shevchenko 	/* Drain Rx FIFO, Fill Tx FIFO and prevent overruns */
679ca632f55SGrant Likely 	do {
680ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
681ca632f55SGrant Likely 			int_transfer_complete(drv_data);
682ca632f55SGrant Likely 			return IRQ_HANDLED;
683ca632f55SGrant Likely 		}
684ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
685ca632f55SGrant Likely 
686ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
687ca632f55SGrant Likely 		int_transfer_complete(drv_data);
688ca632f55SGrant Likely 		return IRQ_HANDLED;
689ca632f55SGrant Likely 	}
690ca632f55SGrant Likely 
691ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
692ca632f55SGrant Likely 		u32 bytes_left;
693ca632f55SGrant Likely 		u32 sccr1_reg;
694ca632f55SGrant Likely 
695c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
696ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
697ca632f55SGrant Likely 
698ca632f55SGrant Likely 		/*
6998083d6b8SAndy Shevchenko 		 * PXA25x_SSP has no timeout, set up Rx threshold for
7008083d6b8SAndy Shevchenko 		 * the remaining Rx bytes.
701ca632f55SGrant Likely 		 */
702ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
7034fdb2424SWeike Chen 			u32 rx_thre;
704ca632f55SGrant Likely 
7054fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
706ca632f55SGrant Likely 
707ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
708ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
709ca632f55SGrant Likely 			case 4:
7102c183376SGustavo A. R. Silva 				bytes_left >>= 2;
7112c183376SGustavo A. R. Silva 				break;
712ca632f55SGrant Likely 			case 2:
713ca632f55SGrant Likely 				bytes_left >>= 1;
7142c183376SGustavo A. R. Silva 				break;
715ca632f55SGrant Likely 			}
716ca632f55SGrant Likely 
7174fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
7184fdb2424SWeike Chen 			if (rx_thre > bytes_left)
7194fdb2424SWeike Chen 				rx_thre = bytes_left;
720ca632f55SGrant Likely 
7214fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
722ca632f55SGrant Likely 		}
723c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
724ca632f55SGrant Likely 	}
725ca632f55SGrant Likely 
726ca632f55SGrant Likely 	/* We did something */
727ca632f55SGrant Likely 	return IRQ_HANDLED;
728ca632f55SGrant Likely }
729ca632f55SGrant Likely 
730b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data)
731b0312482SJan Kiszka {
7323bbdc083SAndy Shevchenko 	int_stop_and_reset(drv_data);
73329d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
734b0312482SJan Kiszka 
735c3dce24cSAndy Shevchenko 	dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
736b0312482SJan Kiszka }
737b0312482SJan Kiszka 
738ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
739ca632f55SGrant Likely {
740ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
7417d94a505SMika Westerberg 	u32 sccr1_reg;
742ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
743ca632f55SGrant Likely 	u32 status;
744ca632f55SGrant Likely 
7457d94a505SMika Westerberg 	/*
7467d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
7477d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
7487d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
7497d94a505SMika Westerberg 	 * interrupt is enabled).
7507d94a505SMika Westerberg 	 */
751c3dce24cSAndy Shevchenko 	if (pm_runtime_suspended(drv_data->ssp->dev))
7527d94a505SMika Westerberg 		return IRQ_NONE;
7537d94a505SMika Westerberg 
754269e4a41SMika Westerberg 	/*
755269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
756269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
757269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
758269e4a41SMika Westerberg 	 * powered off.
759269e4a41SMika Westerberg 	 */
760c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
761269e4a41SMika Westerberg 	if (status == ~0)
762269e4a41SMika Westerberg 		return IRQ_NONE;
763269e4a41SMika Westerberg 
764c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
765ca632f55SGrant Likely 
766ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
767ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
768ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
769ca632f55SGrant Likely 
77002bc933eSTan, Jui Nee 	/* Ignore RX timeout interrupt if it is disabled */
77102bc933eSTan, Jui Nee 	if (!(sccr1_reg & SSCR1_TINTE))
77202bc933eSTan, Jui Nee 		mask &= ~SSSR_TINT;
77302bc933eSTan, Jui Nee 
774ca632f55SGrant Likely 	if (!(status & mask))
775ca632f55SGrant Likely 		return IRQ_NONE;
776ca632f55SGrant Likely 
777e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
778e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
779e51e9b93SJan Kiszka 
78051eea52dSLubomir Rintel 	if (!drv_data->controller->cur_msg) {
781b0312482SJan Kiszka 		handle_bad_msg(drv_data);
782ca632f55SGrant Likely 		/* Never fail */
783ca632f55SGrant Likely 		return IRQ_HANDLED;
784ca632f55SGrant Likely 	}
785ca632f55SGrant Likely 
786ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
787ca632f55SGrant Likely }
788ca632f55SGrant Likely 
789e5262d05SWeike Chen /*
7909df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
7919df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
7929df461ecSAndy Shevchenko  *
7939df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
7949df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
7959df461ecSAndy Shevchenko  *
7969df461ecSAndy Shevchenko  * Fsys = 200MHz
7979df461ecSAndy Shevchenko  *
7989df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
7999df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
8009df461ecSAndy Shevchenko  *
8019df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
8029df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
8039df461ecSAndy Shevchenko  *
8049df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
8059df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
8069df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
8079df461ecSAndy Shevchenko  *       k = [1, 256]
8089df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
8099df461ecSAndy Shevchenko  *
8109df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
8119df461ecSAndy Shevchenko  * are:
8129df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
8139df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
8149df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
8159df461ecSAndy Shevchenko  *
8169df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
8179df461ecSAndy Shevchenko  *
8189df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
8199df461ecSAndy Shevchenko  * to the asked baud rate.
820e5262d05SWeike Chen  */
8219df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
822e5262d05SWeike Chen {
8239df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
8249df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
8259df461ecSAndy Shevchenko 						   see (2) */
8269df461ecSAndy Shevchenko 						/* case 3 */
8279df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
8289df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
8299df461ecSAndy Shevchenko 	unsigned long scale;
8309df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
8319df461ecSAndy Shevchenko 	long r, r1, r2;
8329df461ecSAndy Shevchenko 	u32 mul;
833e5262d05SWeike Chen 
8349df461ecSAndy Shevchenko 	/* Case 1 */
8359df461ecSAndy Shevchenko 
8369df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
8379df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
8389df461ecSAndy Shevchenko 
8399df461ecSAndy Shevchenko 	/* Calculate initial quot */
8403ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
8419df461ecSAndy Shevchenko 
8429df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
8439df461ecSAndy Shevchenko 	if (q1 > 256) {
8449df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
8459df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
8469df461ecSAndy Shevchenko 		if (scale > 9) {
8479df461ecSAndy Shevchenko 			q1 >>= scale - 9;
8489df461ecSAndy Shevchenko 			mul >>= scale - 9;
8499df461ecSAndy Shevchenko 		}
8509df461ecSAndy Shevchenko 
8519df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
8529df461ecSAndy Shevchenko 		q1 += q1 & 1;
8539df461ecSAndy Shevchenko 	}
8549df461ecSAndy Shevchenko 
8559df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
8569df461ecSAndy Shevchenko 	scale = __ffs(q1);
8579df461ecSAndy Shevchenko 	q1 >>= scale;
8589df461ecSAndy Shevchenko 	mul >>= scale;
8599df461ecSAndy Shevchenko 
8609df461ecSAndy Shevchenko 	/* Get the remainder */
8619df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
8629df461ecSAndy Shevchenko 
8639df461ecSAndy Shevchenko 	/* Case 2 */
8649df461ecSAndy Shevchenko 
8653ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
8669df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
8679df461ecSAndy Shevchenko 
8689df461ecSAndy Shevchenko 	/*
8699df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
8709df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
8719df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
8729df461ecSAndy Shevchenko 	 */
8739df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
8749df461ecSAndy Shevchenko 		/* case 1 is better */
8759df461ecSAndy Shevchenko 		r = r1;
8769df461ecSAndy Shevchenko 		q = q1;
8779df461ecSAndy Shevchenko 	} else {
8789df461ecSAndy Shevchenko 		/* case 2 is better */
8799df461ecSAndy Shevchenko 		r = r2;
8809df461ecSAndy Shevchenko 		q = q2;
8819df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
8829df461ecSAndy Shevchenko 	}
8839df461ecSAndy Shevchenko 
8843ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
8859df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
8869df461ecSAndy Shevchenko 		u64 fssp;
8879df461ecSAndy Shevchenko 		u32 m;
8889df461ecSAndy Shevchenko 
8899df461ecSAndy Shevchenko 		/* Calculate initial quot */
8903ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
8919df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
8929df461ecSAndy Shevchenko 
8939df461ecSAndy Shevchenko 		/* Get the remainder */
8949df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
8959df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
8969df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
8979df461ecSAndy Shevchenko 
8989df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
8999df461ecSAndy Shevchenko 		if (r1 < r) {
9009df461ecSAndy Shevchenko 			/* case 3 is better */
9019df461ecSAndy Shevchenko 			q = 1;
9029df461ecSAndy Shevchenko 			mul = m;
903e5262d05SWeike Chen 		}
904e5262d05SWeike Chen 	}
905e5262d05SWeike Chen 
9069df461ecSAndy Shevchenko 	*dds = mul;
9079df461ecSAndy Shevchenko 	return q - 1;
908e5262d05SWeike Chen }
909e5262d05SWeike Chen 
9103343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
911ca632f55SGrant Likely {
91251eea52dSLubomir Rintel 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
9133343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
9143343b7a6SMika Westerberg 
9153343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
916ca632f55SGrant Likely 
91729f21337SFlavio Suligoi 	/*
91829f21337SFlavio Suligoi 	 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
9198083d6b8SAndy Shevchenko 	 * that the SSP transmission rate can be greater than the device rate.
92029f21337SFlavio Suligoi 	 */
921ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
92229f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
923ca632f55SGrant Likely 	else
92429f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
925ca632f55SGrant Likely }
926ca632f55SGrant Likely 
927e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
928d2c2f6a4SAndy Shevchenko 					   int rate)
929e5262d05SWeike Chen {
93096579a4eSJarkko Nikula 	struct chip_data *chip =
93151eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
932025ffe88SAndy Shevchenko 	unsigned int clk_div;
933e5262d05SWeike Chen 
934e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
935e5262d05SWeike Chen 	case QUARK_X1000_SSP:
9369df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
937eecacf73SDan Carpenter 		break;
938e5262d05SWeike Chen 	default:
939025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
940eecacf73SDan Carpenter 		break;
941e5262d05SWeike Chen 	}
942025ffe88SAndy Shevchenko 	return clk_div << 8;
943e5262d05SWeike Chen }
944e5262d05SWeike Chen 
94551eea52dSLubomir Rintel static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
946b6ced294SJarkko Nikula 			       struct spi_device *spi,
947b6ced294SJarkko Nikula 			       struct spi_transfer *xfer)
948b6ced294SJarkko Nikula {
949b6ced294SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
950b6ced294SJarkko Nikula 
951b6ced294SJarkko Nikula 	return chip->enable_dma &&
952b6ced294SJarkko Nikula 	       xfer->len <= MAX_DMA_LEN &&
953b6ced294SJarkko Nikula 	       xfer->len >= chip->dma_burst_size;
954b6ced294SJarkko Nikula }
955b6ced294SJarkko Nikula 
95651eea52dSLubomir Rintel static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
957d5898e19SJarkko Nikula 				   struct spi_device *spi,
958d5898e19SJarkko Nikula 				   struct spi_transfer *transfer)
959ca632f55SGrant Likely {
96051eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
96151eea52dSLubomir Rintel 	struct spi_message *message = controller->cur_msg;
96220f4c379SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
96396579a4eSJarkko Nikula 	u32 dma_thresh = chip->dma_threshold;
96496579a4eSJarkko Nikula 	u32 dma_burst = chip->dma_burst_size;
96596579a4eSJarkko Nikula 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
966bffc967eSJarkko Nikula 	u32 clk_div;
967bffc967eSJarkko Nikula 	u8 bits;
968bffc967eSJarkko Nikula 	u32 speed;
969ca632f55SGrant Likely 	u32 cr0;
970ca632f55SGrant Likely 	u32 cr1;
9717d1f1bf6SAndy Shevchenko 	int err;
972b6ced294SJarkko Nikula 	int dma_mapped;
973ca632f55SGrant Likely 
974cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
975b6ced294SJarkko Nikula 	if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
976ca632f55SGrant Likely 
9778083d6b8SAndy Shevchenko 		/* Reject already-mapped transfers; PIO won't always work */
978ca632f55SGrant Likely 		if (message->is_dma_mapped
979ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
980748fbadfSJarkko Nikula 			dev_err(&spi->dev,
9818ae55af3SJarkko Nikula 				"Mapped transfer length of %u is greater than %d\n",
982ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
983d5898e19SJarkko Nikula 			return -EINVAL;
984ca632f55SGrant Likely 		}
985ca632f55SGrant Likely 
9868083d6b8SAndy Shevchenko 		/* Warn ... we force this to PIO mode */
98720f4c379SJarkko Nikula 		dev_warn_ratelimited(&spi->dev,
988684a3ac7SAndy Shevchenko 				     "DMA disabled for transfer length %u greater than %d\n",
989684a3ac7SAndy Shevchenko 				     transfer->len, MAX_DMA_LEN);
990ca632f55SGrant Likely 	}
991ca632f55SGrant Likely 
992ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
993cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
994748fbadfSJarkko Nikula 		dev_err(&spi->dev, "Flush failed\n");
995d5898e19SJarkko Nikula 		return -EIO;
996ca632f55SGrant Likely 	}
997ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
998ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
999ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
1000ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
1001ca632f55SGrant Likely 
1002ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
1003ca632f55SGrant Likely 	bits = transfer->bits_per_word;
1004ca632f55SGrant Likely 	speed = transfer->speed_hz;
1005ca632f55SGrant Likely 
1006d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
1007ca632f55SGrant Likely 
1008ca632f55SGrant Likely 	if (bits <= 8) {
1009ca632f55SGrant Likely 		drv_data->n_bytes = 1;
1010*44ec41b7SAndy Shevchenko 		drv_data->read = drv_data->rx ? u8_reader : null_reader;
1011*44ec41b7SAndy Shevchenko 		drv_data->write = drv_data->tx ? u8_writer : null_writer;
1012ca632f55SGrant Likely 	} else if (bits <= 16) {
1013ca632f55SGrant Likely 		drv_data->n_bytes = 2;
1014*44ec41b7SAndy Shevchenko 		drv_data->read = drv_data->rx ? u16_reader : null_reader;
1015*44ec41b7SAndy Shevchenko 		drv_data->write = drv_data->tx ? u16_writer : null_writer;
1016ca632f55SGrant Likely 	} else if (bits <= 32) {
1017ca632f55SGrant Likely 		drv_data->n_bytes = 4;
1018*44ec41b7SAndy Shevchenko 		drv_data->read = drv_data->rx ? u32_reader : null_reader;
1019*44ec41b7SAndy Shevchenko 		drv_data->write = drv_data->tx ? u32_writer : null_writer;
1020ca632f55SGrant Likely 	}
1021196b0e2cSJarkko Nikula 	/*
10228083d6b8SAndy Shevchenko 	 * If bits per word is changed in DMA mode, then must check
10238083d6b8SAndy Shevchenko 	 * the thresholds and burst also.
1024196b0e2cSJarkko Nikula 	 */
1025ca632f55SGrant Likely 	if (chip->enable_dma) {
1026cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
102720f4c379SJarkko Nikula 						spi,
1028ca632f55SGrant Likely 						bits, &dma_burst,
1029ca632f55SGrant Likely 						&dma_thresh))
103020f4c379SJarkko Nikula 			dev_warn_ratelimited(&spi->dev,
10318ae55af3SJarkko Nikula 					     "DMA burst size reduced to match bits_per_word\n");
1032ca632f55SGrant Likely 	}
1033ca632f55SGrant Likely 
103451eea52dSLubomir Rintel 	dma_mapped = controller->can_dma &&
103520f4c379SJarkko Nikula 		     controller->can_dma(controller, spi, transfer) &&
103651eea52dSLubomir Rintel 		     controller->cur_msg_mapped;
1037b6ced294SJarkko Nikula 	if (dma_mapped) {
1038ca632f55SGrant Likely 
1039ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
1040cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1041ca632f55SGrant Likely 
1042d5898e19SJarkko Nikula 		err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1043d5898e19SJarkko Nikula 		if (err)
1044d5898e19SJarkko Nikula 			return err;
1045ca632f55SGrant Likely 
1046ca632f55SGrant Likely 		/* Clear status and start DMA engine */
1047ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1048c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1049cd7bed00SMika Westerberg 
1050cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
1051ca632f55SGrant Likely 	} else {
1052ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
1053ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
1054ca632f55SGrant Likely 
1055ca632f55SGrant Likely 		/* Clear status  */
1056ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1057ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1058ca632f55SGrant Likely 	}
1059ca632f55SGrant Likely 
1060ee03672dSJarkko Nikula 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1061ee03672dSJarkko Nikula 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1062ee03672dSJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
106320f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
106451eea52dSLubomir Rintel 			controller->max_speed_hz
1065ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1066b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1067ee03672dSJarkko Nikula 	else
106820f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
106951eea52dSLubomir Rintel 			controller->max_speed_hz / 2
1070ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1071b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1072ee03672dSJarkko Nikula 
1073a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
10741bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold);
10751bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold);
1076a0d2642eSMika Westerberg 	}
1077a0d2642eSMika Westerberg 
10783fdb59cfSAndy Shevchenko 	if (is_mrfld_ssp(drv_data)) {
107970252440SAndy Shevchenko 		u32 mask = SFIFOTT_RFT | SFIFOTT_TFT;
10803fdb59cfSAndy Shevchenko 		u32 thresh = 0;
10813fdb59cfSAndy Shevchenko 
10823fdb59cfSAndy Shevchenko 		thresh |= SFIFOTT_RxThresh(chip->lpss_rx_threshold);
10833fdb59cfSAndy Shevchenko 		thresh |= SFIFOTT_TxThresh(chip->lpss_tx_threshold);
10843fdb59cfSAndy Shevchenko 
108570252440SAndy Shevchenko 		pxa2xx_spi_update(drv_data, SFIFOTT, mask, thresh);
10863fdb59cfSAndy Shevchenko 	}
10873fdb59cfSAndy Shevchenko 
10881bed378cSAndy Shevchenko 	if (is_quark_x1000_ssp(drv_data))
10891bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate);
1090e5262d05SWeike Chen 
10910c8ccd8bSAndy Shevchenko 	/* Stop the SSP */
10920c8ccd8bSAndy Shevchenko 	if (!is_mmp2_ssp(drv_data))
10930c8ccd8bSAndy Shevchenko 		pxa_ssp_disable(drv_data->ssp);
10940c8ccd8bSAndy Shevchenko 
10950c8ccd8bSAndy Shevchenko 	if (!pxa25x_ssp_comp(drv_data))
10960c8ccd8bSAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
10970c8ccd8bSAndy Shevchenko 
10988083d6b8SAndy Shevchenko 	/* First set CR1 without interrupt and service enables */
10991bed378cSAndy Shevchenko 	pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1);
11001bed378cSAndy Shevchenko 
11018083d6b8SAndy Shevchenko 	/* See if we need to reload the configuration registers */
11021bed378cSAndy Shevchenko 	pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0);
1103ca632f55SGrant Likely 
11040c8ccd8bSAndy Shevchenko 	/* Restart the SSP */
11050c8ccd8bSAndy Shevchenko 	pxa_ssp_enable(drv_data->ssp);
11060c8ccd8bSAndy Shevchenko 
110741c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data)) {
11086d380132SAndy Shevchenko 		u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8;
110982391856SLubomir Rintel 
111082391856SLubomir Rintel 		if (tx_level) {
11118083d6b8SAndy Shevchenko 			/* On MMP2, flipping SSE doesn't to empty Tx FIFO. */
1112684a3ac7SAndy Shevchenko 			dev_warn(&spi->dev, "%u bytes of garbage in Tx FIFO!\n", tx_level);
111382391856SLubomir Rintel 			if (tx_level > transfer->len)
111482391856SLubomir Rintel 				tx_level = transfer->len;
111582391856SLubomir Rintel 			drv_data->tx += tx_level;
111682391856SLubomir Rintel 		}
111782391856SLubomir Rintel 	}
111882391856SLubomir Rintel 
111951eea52dSLubomir Rintel 	if (spi_controller_is_slave(controller)) {
1120ec93cb6fSLubomir Rintel 		while (drv_data->write(drv_data))
1121ec93cb6fSLubomir Rintel 			;
112277d33897SLubomir Rintel 		if (drv_data->gpiod_ready) {
112377d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 1);
112477d33897SLubomir Rintel 			udelay(1);
112577d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 0);
112677d33897SLubomir Rintel 		}
1127ec93cb6fSLubomir Rintel 	}
1128ec93cb6fSLubomir Rintel 
1129d5898e19SJarkko Nikula 	/*
1130d5898e19SJarkko Nikula 	 * Release the data by enabling service requests and interrupts,
11318083d6b8SAndy Shevchenko 	 * without changing any mode bits.
1132d5898e19SJarkko Nikula 	 */
1133c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1134d5898e19SJarkko Nikula 
1135d5898e19SJarkko Nikula 	return 1;
1136ca632f55SGrant Likely }
1137ca632f55SGrant Likely 
113851eea52dSLubomir Rintel static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
1139ec93cb6fSLubomir Rintel {
114051eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1141ec93cb6fSLubomir Rintel 
11424761d2e7SAndy Shevchenko 	int_error_stop(drv_data, "transfer aborted", -EINTR);
1143ec93cb6fSLubomir Rintel 
1144ec93cb6fSLubomir Rintel 	return 0;
1145ec93cb6fSLubomir Rintel }
1146ec93cb6fSLubomir Rintel 
114751eea52dSLubomir Rintel static void pxa2xx_spi_handle_err(struct spi_controller *controller,
11487f86bde9SMika Westerberg 				 struct spi_message *msg)
1149ca632f55SGrant Likely {
115051eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1151ca632f55SGrant Likely 
11523bbdc083SAndy Shevchenko 	int_stop_and_reset(drv_data);
11533bbdc083SAndy Shevchenko 
1154d5898e19SJarkko Nikula 	/* Disable the SSP */
115529d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
1156ca632f55SGrant Likely 
1157d5898e19SJarkko Nikula 	/*
1158d5898e19SJarkko Nikula 	 * Stop the DMA if running. Note DMA callback handler may have unset
1159d5898e19SJarkko Nikula 	 * the dma_running already, which is fine as stopping is not needed
1160d5898e19SJarkko Nikula 	 * then but we shouldn't rely this flag for anything else than
1161d5898e19SJarkko Nikula 	 * stopping. For instance to differentiate between PIO and DMA
1162d5898e19SJarkko Nikula 	 * transfers.
1163d5898e19SJarkko Nikula 	 */
1164d5898e19SJarkko Nikula 	if (atomic_read(&drv_data->dma_running))
1165d5898e19SJarkko Nikula 		pxa2xx_spi_dma_stop(drv_data);
1166ca632f55SGrant Likely }
1167ca632f55SGrant Likely 
116851eea52dSLubomir Rintel static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
11697d94a505SMika Westerberg {
117051eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
11717d94a505SMika Westerberg 
11727d94a505SMika Westerberg 	/* Disable the SSP now */
117329d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
11747d94a505SMika Westerberg 
11757d94a505SMika Westerberg 	return 0;
11767d94a505SMika Westerberg }
11777d94a505SMika Westerberg 
1178de6926f3SAndy Shevchenko static void cleanup_cs(struct spi_device *spi)
1179de6926f3SAndy Shevchenko {
1180de6926f3SAndy Shevchenko 	if (!gpio_is_valid(spi->cs_gpio))
1181de6926f3SAndy Shevchenko 		return;
1182de6926f3SAndy Shevchenko 
1183de6926f3SAndy Shevchenko 	gpio_free(spi->cs_gpio);
1184de6926f3SAndy Shevchenko 	spi->cs_gpio = -ENOENT;
1185de6926f3SAndy Shevchenko }
1186de6926f3SAndy Shevchenko 
1187ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1188ca632f55SGrant Likely 		    struct pxa2xx_spi_chip *chip_info)
1189ca632f55SGrant Likely {
1190de6926f3SAndy Shevchenko 	struct driver_data *drv_data = spi_controller_get_devdata(spi->controller);
1191ca632f55SGrant Likely 
119299f499cdSMika Westerberg 	if (chip == NULL)
119399f499cdSMika Westerberg 		return 0;
119499f499cdSMika Westerberg 
119599f499cdSMika Westerberg 	if (chip_info == NULL)
1196ca632f55SGrant Likely 		return 0;
1197ca632f55SGrant Likely 
1198de6926f3SAndy Shevchenko 	if (drv_data->ssp_type == CE4100_SSP)
1199de6926f3SAndy Shevchenko 		return 0;
1200de6926f3SAndy Shevchenko 
12018083d6b8SAndy Shevchenko 	/*
12028083d6b8SAndy Shevchenko 	 * NOTE: setup() can be called multiple times, possibly with
12038083d6b8SAndy Shevchenko 	 * different chip_info, release previously requested GPIO.
1204ca632f55SGrant Likely 	 */
1205de6926f3SAndy Shevchenko 	cleanup_cs(spi);
1206ca632f55SGrant Likely 
12078083d6b8SAndy Shevchenko 	/* If ->cs_control() is provided, ignore GPIO chip select */
1208ca632f55SGrant Likely 	if (chip_info->cs_control) {
1209ca632f55SGrant Likely 		chip->cs_control = chip_info->cs_control;
1210ca632f55SGrant Likely 		return 0;
1211ca632f55SGrant Likely 	}
1212ca632f55SGrant Likely 
1213ca632f55SGrant Likely 	if (gpio_is_valid(chip_info->gpio_cs)) {
1214de6926f3SAndy Shevchenko 		int gpio = chip_info->gpio_cs;
1215de6926f3SAndy Shevchenko 		int err;
1216de6926f3SAndy Shevchenko 
1217de6926f3SAndy Shevchenko 		err = gpio_request(gpio, "SPI_CS");
1218ca632f55SGrant Likely 		if (err) {
1219de6926f3SAndy Shevchenko 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n", gpio);
1220ca632f55SGrant Likely 			return err;
1221ca632f55SGrant Likely 		}
1222ca632f55SGrant Likely 
1223de6926f3SAndy Shevchenko 		err = gpio_direction_output(gpio, !(spi->mode & SPI_CS_HIGH));
1224de6926f3SAndy Shevchenko 		if (err) {
1225de6926f3SAndy Shevchenko 			gpio_free(gpio);
1226de6926f3SAndy Shevchenko 			return err;
1227ca632f55SGrant Likely 		}
1228ca632f55SGrant Likely 
1229de6926f3SAndy Shevchenko 		spi->cs_gpio = gpio;
1230de6926f3SAndy Shevchenko 	}
1231de6926f3SAndy Shevchenko 
1232de6926f3SAndy Shevchenko 	return 0;
1233ca632f55SGrant Likely }
1234ca632f55SGrant Likely 
1235ca632f55SGrant Likely static int setup(struct spi_device *spi)
1236ca632f55SGrant Likely {
1237bffc967eSJarkko Nikula 	struct pxa2xx_spi_chip *chip_info;
1238ca632f55SGrant Likely 	struct chip_data *chip;
1239dccf7369SJarkko Nikula 	const struct lpss_config *config;
12403cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12413cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1242a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
12432ec6f20bSLukas Wunner 	int err;
1244a0d2642eSMika Westerberg 
1245e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1246e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1247e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1248e5262d05SWeike Chen 		tx_hi_thres = 0;
1249e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1250e5262d05SWeike Chen 		break;
12513fdb59cfSAndy Shevchenko 	case MRFLD_SSP:
12523fdb59cfSAndy Shevchenko 		tx_thres = TX_THRESH_MRFLD_DFLT;
12533fdb59cfSAndy Shevchenko 		tx_hi_thres = 0;
12543fdb59cfSAndy Shevchenko 		rx_thres = RX_THRESH_MRFLD_DFLT;
12553fdb59cfSAndy Shevchenko 		break;
12567c7289a4SAndy Shevchenko 	case CE4100_SSP:
12577c7289a4SAndy Shevchenko 		tx_thres = TX_THRESH_CE4100_DFLT;
12587c7289a4SAndy Shevchenko 		tx_hi_thres = 0;
12597c7289a4SAndy Shevchenko 		rx_thres = RX_THRESH_CE4100_DFLT;
12607c7289a4SAndy Shevchenko 		break;
126103fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
126203fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
126330f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
126434cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1265b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1266fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
1267dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1268dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1269dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1270dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1271e5262d05SWeike Chen 		break;
1272e5262d05SWeike Chen 	default:
1273a0d2642eSMika Westerberg 		tx_hi_thres = 0;
127451eea52dSLubomir Rintel 		if (spi_controller_is_slave(drv_data->controller)) {
1275ec93cb6fSLubomir Rintel 			tx_thres = 1;
1276ec93cb6fSLubomir Rintel 			rx_thres = 2;
1277ec93cb6fSLubomir Rintel 		} else {
1278ec93cb6fSLubomir Rintel 			tx_thres = TX_THRESH_DFLT;
1279a0d2642eSMika Westerberg 			rx_thres = RX_THRESH_DFLT;
1280ec93cb6fSLubomir Rintel 		}
1281e5262d05SWeike Chen 		break;
1282a0d2642eSMika Westerberg 	}
1283ca632f55SGrant Likely 
12848083d6b8SAndy Shevchenko 	/* Only allocate on the first setup */
1285ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1286ca632f55SGrant Likely 	if (!chip) {
1287ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
12889deae459SJingoo Han 		if (!chip)
1289ca632f55SGrant Likely 			return -ENOMEM;
1290ca632f55SGrant Likely 
1291ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1292ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1293f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1294f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1295ca632f55SGrant Likely 				kfree(chip);
1296ca632f55SGrant Likely 				return -EINVAL;
1297ca632f55SGrant Likely 			}
1298c18d925fSJan Kiszka 		}
129951eea52dSLubomir Rintel 		chip->enable_dma = drv_data->controller_info->enable_dma;
1300ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1301ca632f55SGrant Likely 	}
1302ca632f55SGrant Likely 
13038083d6b8SAndy Shevchenko 	/*
13048083d6b8SAndy Shevchenko 	 * Protocol drivers may change the chip settings, so...
13058083d6b8SAndy Shevchenko 	 * if chip_info exists, use it.
13068083d6b8SAndy Shevchenko 	 */
1307ca632f55SGrant Likely 	chip_info = spi->controller_data;
1308ca632f55SGrant Likely 
1309ca632f55SGrant Likely 	/* chip_info isn't always needed */
1310ca632f55SGrant Likely 	chip->cr1 = 0;
1311ca632f55SGrant Likely 	if (chip_info) {
1312ca632f55SGrant Likely 		if (chip_info->timeout)
1313ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1314ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1315ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1316a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1317a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1318ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1319ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1320ca632f55SGrant Likely 		chip->dma_threshold = 0;
1321ca632f55SGrant Likely 		if (chip_info->enable_loopback)
1322ca632f55SGrant Likely 			chip->cr1 = SSCR1_LBM;
1323ca632f55SGrant Likely 	}
132451eea52dSLubomir Rintel 	if (spi_controller_is_slave(drv_data->controller)) {
1325ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCFR;
1326ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCLKDIR;
1327ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SFRMDIR;
1328ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SPH;
1329ec93cb6fSLubomir Rintel 	}
1330ca632f55SGrant Likely 
13313fdb59cfSAndy Shevchenko 	if (is_lpss_ssp(drv_data)) {
1332a0d2642eSMika Westerberg 		chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
13333fdb59cfSAndy Shevchenko 		chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
13343fdb59cfSAndy Shevchenko 					  SSITF_TxHiThresh(tx_hi_thres);
13353fdb59cfSAndy Shevchenko 	}
13363fdb59cfSAndy Shevchenko 
13373fdb59cfSAndy Shevchenko 	if (is_mrfld_ssp(drv_data)) {
13383fdb59cfSAndy Shevchenko 		chip->lpss_rx_threshold = rx_thres;
13393fdb59cfSAndy Shevchenko 		chip->lpss_tx_threshold = tx_thres;
13403fdb59cfSAndy Shevchenko 	}
1341a0d2642eSMika Westerberg 
13428083d6b8SAndy Shevchenko 	/*
13438083d6b8SAndy Shevchenko 	 * Set DMA burst and threshold outside of chip_info path so that if
13448083d6b8SAndy Shevchenko 	 * chip_info goes away after setting chip->enable_dma, the burst and
13458083d6b8SAndy Shevchenko 	 * threshold can still respond to changes in bits_per_word.
13468083d6b8SAndy Shevchenko 	 */
1347ca632f55SGrant Likely 	if (chip->enable_dma) {
13488083d6b8SAndy Shevchenko 		/* Set up legal burst and threshold for DMA */
1349cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1350cd7bed00SMika Westerberg 						spi->bits_per_word,
1351ca632f55SGrant Likely 						&chip->dma_burst_size,
1352ca632f55SGrant Likely 						&chip->dma_threshold)) {
1353f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1354f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1355ca632f55SGrant Likely 		}
1356000c6af4SAndy Shevchenko 		dev_dbg(&spi->dev,
1357000c6af4SAndy Shevchenko 			"in setup: DMA burst size set to %u\n",
1358000c6af4SAndy Shevchenko 			chip->dma_burst_size);
1359ca632f55SGrant Likely 	}
1360ca632f55SGrant Likely 
1361e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1362e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1363e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1364e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1365e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1366e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1367e5262d05SWeike Chen 		break;
13687c7289a4SAndy Shevchenko 	case CE4100_SSP:
13697c7289a4SAndy Shevchenko 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
13707c7289a4SAndy Shevchenko 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
13717c7289a4SAndy Shevchenko 		break;
1372e5262d05SWeike Chen 	default:
1373e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1374e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1375e5262d05SWeike Chen 		break;
1376e5262d05SWeike Chen 	}
1377e5262d05SWeike Chen 
1378ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1379eb743ec6SAndy Shevchenko 	chip->cr1 |= ((spi->mode & SPI_CPHA) ? SSCR1_SPH : 0) |
1380eb743ec6SAndy Shevchenko 		     ((spi->mode & SPI_CPOL) ? SSCR1_SPO : 0);
1381ca632f55SGrant Likely 
1382b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1383b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1384b833172fSMika Westerberg 
1385ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1386ca632f55SGrant Likely 
1387ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
1388ca632f55SGrant Likely 		return 0;
1389ca632f55SGrant Likely 
13902ec6f20bSLukas Wunner 	err = setup_cs(spi, chip, chip_info);
13912ec6f20bSLukas Wunner 	if (err)
13922ec6f20bSLukas Wunner 		kfree(chip);
13932ec6f20bSLukas Wunner 
13942ec6f20bSLukas Wunner 	return err;
1395ca632f55SGrant Likely }
1396ca632f55SGrant Likely 
1397ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1398ca632f55SGrant Likely {
1399ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
1400ca632f55SGrant Likely 
1401de6926f3SAndy Shevchenko 	cleanup_cs(spi);
1402ca632f55SGrant Likely 	kfree(chip);
1403ca632f55SGrant Likely }
1404ca632f55SGrant Likely 
14059b2d6119SLee Jones #ifdef CONFIG_ACPI
14068422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
140703fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
140803fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
140903fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
141003fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
141103fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
141230f3a6abSMika Westerberg 	{ "8086228E", LPSS_BSW_SSP },
141303fbf488SJarkko Nikula 	{ },
141403fbf488SJarkko Nikula };
141503fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
14169b2d6119SLee Jones #endif
141703fbf488SJarkko Nikula 
141834cadd9cSJarkko Nikula /*
141934cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
142034cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
142134cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
142234cadd9cSJarkko Nikula  */
142334cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
142434cadd9cSJarkko Nikula 	/* SPT-LP */
142534cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
142634cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
142734cadd9cSJarkko Nikula 	/* SPT-H */
142834cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
142934cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1430704d2b07SMika Westerberg 	/* KBL-H */
1431704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1432704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
14336157d4c2SJarkko Nikula 	/* CML-V */
14346157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
14356157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
1436c1b03f11SJarkko Nikula 	/* BXT A-Step */
1437b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1438b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1439b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1440c1b03f11SJarkko Nikula 	/* BXT B-Step */
1441c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1442c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1443c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1444e18a80acSDavid E. Box 	/* GLK */
1445e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1446e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1447e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
144822d71a50SMika Westerberg 	/* ICL-LP */
144922d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
145022d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
145122d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
14528cc77204SJarkko Nikula 	/* EHL */
14538cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
14548cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
14558cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
14569c7315c9SJarkko Nikula 	/* JSL */
14579c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
14589c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
14599c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
1460cf961fceSJarkko Nikula 	/* TGL-H */
1461cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
1462cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
1463cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
1464cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
1465a402e397SJarkko Nikula 	/* ADL-P */
1466a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51aa), LPSS_CNL_SSP },
1467a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51ab), LPSS_CNL_SSP },
1468a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51fb), LPSS_CNL_SSP },
14698c4ffe4dSJarkko Nikula 	/* ADL-M */
14708c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54aa), LPSS_CNL_SSP },
14718c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54ab), LPSS_CNL_SSP },
14728c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54fb), LPSS_CNL_SSP },
1473b7c08cf8SJarkko Nikula 	/* APL */
1474b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1475b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1476b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1477b8450e01SJarkko Nikula 	/* ADL-S */
1478b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7aaa), LPSS_CNL_SSP },
1479b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7aab), LPSS_CNL_SSP },
1480b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7af9), LPSS_CNL_SSP },
1481b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7afb), LPSS_CNL_SSP },
1482fc0b2accSJarkko Nikula 	/* CNL-LP */
1483fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1484fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1485fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1486fc0b2accSJarkko Nikula 	/* CNL-H */
1487fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1488fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1489fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
149041a91802SEvan Green 	/* CML-LP */
149141a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
149241a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
149341a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
1494f0cf17edSJarkko Nikula 	/* CML-H */
1495f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1496f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1497f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
1498a4127952SJarkko Nikula 	/* TGL-LP */
1499a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1500a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1501a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1502a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1503a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1504a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1505a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
150694e5c23dSAxel Lin 	{ },
150734cadd9cSJarkko Nikula };
150834cadd9cSJarkko Nikula 
150987ae1d2dSLubomir Rintel static const struct of_device_id pxa2xx_spi_of_match[] = {
151087ae1d2dSLubomir Rintel 	{ .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
151187ae1d2dSLubomir Rintel 	{},
151287ae1d2dSLubomir Rintel };
151387ae1d2dSLubomir Rintel MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
151487ae1d2dSLubomir Rintel 
151587ae1d2dSLubomir Rintel #ifdef CONFIG_ACPI
151687ae1d2dSLubomir Rintel 
1517365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
151887ae1d2dSLubomir Rintel {
1519365e856eSAndy Shevchenko 	struct acpi_device *adev;
152087ae1d2dSLubomir Rintel 	unsigned int devid;
152187ae1d2dSLubomir Rintel 	int port_id = -1;
152287ae1d2dSLubomir Rintel 
1523365e856eSAndy Shevchenko 	adev = ACPI_COMPANION(dev);
152487ae1d2dSLubomir Rintel 	if (adev && adev->pnp.unique_id &&
152587ae1d2dSLubomir Rintel 	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
152687ae1d2dSLubomir Rintel 		port_id = devid;
152787ae1d2dSLubomir Rintel 	return port_id;
152887ae1d2dSLubomir Rintel }
152987ae1d2dSLubomir Rintel 
153087ae1d2dSLubomir Rintel #else /* !CONFIG_ACPI */
153187ae1d2dSLubomir Rintel 
1532365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
153387ae1d2dSLubomir Rintel {
153487ae1d2dSLubomir Rintel 	return -1;
153587ae1d2dSLubomir Rintel }
153687ae1d2dSLubomir Rintel 
153787ae1d2dSLubomir Rintel #endif /* CONFIG_ACPI */
153887ae1d2dSLubomir Rintel 
153987ae1d2dSLubomir Rintel 
154087ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
154187ae1d2dSLubomir Rintel 
154234cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
154334cadd9cSJarkko Nikula {
15445ba846b1SAndy Shevchenko 	return param == chan->device->dev;
154534cadd9cSJarkko Nikula }
154634cadd9cSJarkko Nikula 
154787ae1d2dSLubomir Rintel #endif /* CONFIG_PCI */
154887ae1d2dSLubomir Rintel 
154951eea52dSLubomir Rintel static struct pxa2xx_spi_controller *
15500db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1551a3496855SMika Westerberg {
155251eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *pdata;
1553a3496855SMika Westerberg 	struct ssp_device *ssp;
1554a3496855SMika Westerberg 	struct resource *res;
15556fb7427dSAndy Shevchenko 	struct device *parent = pdev->dev.parent;
15566fb7427dSAndy Shevchenko 	struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
155734cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
155855ef8262SLubomir Rintel 	enum pxa_ssp_type type;
1559f2faa3ecSAndy Shevchenko 	const void *match;
1560a3496855SMika Westerberg 
15616fb7427dSAndy Shevchenko 	if (pcidev)
15626fb7427dSAndy Shevchenko 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
1563a3496855SMika Westerberg 
1564f2faa3ecSAndy Shevchenko 	match = device_get_match_data(&pdev->dev);
1565f2faa3ecSAndy Shevchenko 	if (match)
1566f2faa3ecSAndy Shevchenko 		type = (enum pxa_ssp_type)match;
156734cadd9cSJarkko Nikula 	else if (pcidev_id)
156855ef8262SLubomir Rintel 		type = (enum pxa_ssp_type)pcidev_id->driver_data;
156903fbf488SJarkko Nikula 	else
157014af1df3SAndy Shevchenko 		return ERR_PTR(-EINVAL);
157103fbf488SJarkko Nikula 
1572cc0ee987SMika Westerberg 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
15739deae459SJingoo Han 	if (!pdata)
157414af1df3SAndy Shevchenko 		return ERR_PTR(-ENOMEM);
1575a3496855SMika Westerberg 
1576a3496855SMika Westerberg 	ssp = &pdata->ssp;
1577a3496855SMika Westerberg 
157877c544d2SAndy Shevchenko 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1579cbfd6a21SSachin Kamat 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1580cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
158114af1df3SAndy Shevchenko 		return ERR_CAST(ssp->mmio_base);
1582a3496855SMika Westerberg 
158377c544d2SAndy Shevchenko 	ssp->phys_base = res->start;
158477c544d2SAndy Shevchenko 
158587ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
158634cadd9cSJarkko Nikula 	if (pcidev_id) {
15876fb7427dSAndy Shevchenko 		pdata->tx_param = parent;
15886fb7427dSAndy Shevchenko 		pdata->rx_param = parent;
158934cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
159034cadd9cSJarkko Nikula 	}
159187ae1d2dSLubomir Rintel #endif
159234cadd9cSJarkko Nikula 
1593a3496855SMika Westerberg 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
15945eb263efSChuhong Yuan 	if (IS_ERR(ssp->clk))
159514af1df3SAndy Shevchenko 		return ERR_CAST(ssp->clk);
1596a3496855SMika Westerberg 
1597a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
15985eb263efSChuhong Yuan 	if (ssp->irq < 0)
159914af1df3SAndy Shevchenko 		return ERR_PTR(ssp->irq);
16005eb263efSChuhong Yuan 
1601a3496855SMika Westerberg 	ssp->type = type;
16024f3d9577SAndy Shevchenko 	ssp->dev = &pdev->dev;
1603365e856eSAndy Shevchenko 	ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
1604a3496855SMika Westerberg 
1605f2faa3ecSAndy Shevchenko 	pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
1606a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1607cddb339bSMika Westerberg 	pdata->enable_dma = true;
160837821a82SAndy Shevchenko 	pdata->dma_burst_size = 1;
1609a3496855SMika Westerberg 
1610a3496855SMika Westerberg 	return pdata;
1611a3496855SMika Westerberg }
1612a3496855SMika Westerberg 
161351eea52dSLubomir Rintel static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
16143cc7b0e3SJarkko Nikula 				      unsigned int cs)
16150c27d9cfSMika Westerberg {
161651eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
16170c27d9cfSMika Westerberg 
1618c3dce24cSAndy Shevchenko 	if (has_acpi_companion(drv_data->ssp->dev)) {
16190c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
16200c27d9cfSMika Westerberg 		/*
16210c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
16220c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
16230c27d9cfSMika Westerberg 		 * to match what Linux expects.
16240c27d9cfSMika Westerberg 		 */
16250c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
162630f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
16270c27d9cfSMika Westerberg 			return cs - 1;
16280c27d9cfSMika Westerberg 
16290c27d9cfSMika Westerberg 		default:
16300c27d9cfSMika Westerberg 			break;
16310c27d9cfSMika Westerberg 		}
16320c27d9cfSMika Westerberg 	}
16330c27d9cfSMika Westerberg 
16340c27d9cfSMika Westerberg 	return cs;
16350c27d9cfSMika Westerberg }
16360c27d9cfSMika Westerberg 
1637b2662a16SDaniel Vetter static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1638b2662a16SDaniel Vetter {
1639b2662a16SDaniel Vetter 	return MAX_DMA_LEN;
1640b2662a16SDaniel Vetter }
1641b2662a16SDaniel Vetter 
1642fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1643ca632f55SGrant Likely {
1644ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
164551eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *platform_info;
164651eea52dSLubomir Rintel 	struct spi_controller *controller;
1647ca632f55SGrant Likely 	struct driver_data *drv_data;
1648ca632f55SGrant Likely 	struct ssp_device *ssp;
16498b136baaSJarkko Nikula 	const struct lpss_config *config;
1650778c12e6SAndy Shevchenko 	int status;
1651c039dd27SJarkko Nikula 	u32 tmp;
1652ca632f55SGrant Likely 
1653851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1654851bacf5SMika Westerberg 	if (!platform_info) {
16550db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
165614af1df3SAndy Shevchenko 		if (IS_ERR(platform_info)) {
1657851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
165814af1df3SAndy Shevchenko 			return PTR_ERR(platform_info);
1659851bacf5SMika Westerberg 		}
1660a3496855SMika Westerberg 	}
1661ca632f55SGrant Likely 
1662ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1663851bacf5SMika Westerberg 	if (!ssp)
1664851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1665851bacf5SMika Westerberg 
1666851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
16678083d6b8SAndy Shevchenko 		dev_err(&pdev->dev, "failed to get SSP\n");
1668ca632f55SGrant Likely 		return -ENODEV;
1669ca632f55SGrant Likely 	}
1670ca632f55SGrant Likely 
1671ec93cb6fSLubomir Rintel 	if (platform_info->is_slave)
16725626308bSLukas Wunner 		controller = devm_spi_alloc_slave(dev, sizeof(*drv_data));
1673ec93cb6fSLubomir Rintel 	else
16745626308bSLukas Wunner 		controller = devm_spi_alloc_master(dev, sizeof(*drv_data));
1675ec93cb6fSLubomir Rintel 
167651eea52dSLubomir Rintel 	if (!controller) {
167751eea52dSLubomir Rintel 		dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1678f2eed8caSAndy Shevchenko 		status = -ENOMEM;
1679f2eed8caSAndy Shevchenko 		goto out_error_controller_alloc;
1680ca632f55SGrant Likely 	}
168151eea52dSLubomir Rintel 	drv_data = spi_controller_get_devdata(controller);
168251eea52dSLubomir Rintel 	drv_data->controller = controller;
168351eea52dSLubomir Rintel 	drv_data->controller_info = platform_info;
1684ca632f55SGrant Likely 	drv_data->ssp = ssp;
1685ca632f55SGrant Likely 
168694acf807SAndy Shevchenko 	controller->dev.of_node = dev->of_node;
168794acf807SAndy Shevchenko 	controller->dev.fwnode = dev->fwnode;
168894acf807SAndy Shevchenko 
16898083d6b8SAndy Shevchenko 	/* The spi->mode bits understood by this driver: */
169051eea52dSLubomir Rintel 	controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1691ca632f55SGrant Likely 
169251eea52dSLubomir Rintel 	controller->bus_num = ssp->port_id;
169351eea52dSLubomir Rintel 	controller->dma_alignment = DMA_ALIGNMENT;
169451eea52dSLubomir Rintel 	controller->cleanup = cleanup;
169551eea52dSLubomir Rintel 	controller->setup = setup;
169651eea52dSLubomir Rintel 	controller->set_cs = pxa2xx_spi_set_cs;
169751eea52dSLubomir Rintel 	controller->transfer_one = pxa2xx_spi_transfer_one;
169851eea52dSLubomir Rintel 	controller->slave_abort = pxa2xx_spi_slave_abort;
169951eea52dSLubomir Rintel 	controller->handle_err = pxa2xx_spi_handle_err;
170051eea52dSLubomir Rintel 	controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
170151eea52dSLubomir Rintel 	controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
170251eea52dSLubomir Rintel 	controller->auto_runtime_pm = true;
170351eea52dSLubomir Rintel 	controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1704ca632f55SGrant Likely 
1705ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1706ca632f55SGrant Likely 
1707ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1708e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1709e5262d05SWeike Chen 		case QUARK_X1000_SSP:
171051eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1711e5262d05SWeike Chen 			break;
1712e5262d05SWeike Chen 		default:
171351eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1714e5262d05SWeike Chen 			break;
1715e5262d05SWeike Chen 		}
1716e5262d05SWeike Chen 
1717ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1718ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1719ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1720ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1721ca632f55SGrant Likely 	} else {
172251eea52dSLubomir Rintel 		controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1723ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
17245928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1725ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1726ec93cb6fSLubomir Rintel 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1727ec93cb6fSLubomir Rintel 						| SSSR_ROR | SSSR_TUR;
1728ca632f55SGrant Likely 	}
1729ca632f55SGrant Likely 
1730ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1731ca632f55SGrant Likely 			drv_data);
1732ca632f55SGrant Likely 	if (status < 0) {
1733ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
173451eea52dSLubomir Rintel 		goto out_error_controller_alloc;
1735ca632f55SGrant Likely 	}
1736ca632f55SGrant Likely 
1737ca632f55SGrant Likely 	/* Setup DMA if requested */
1738ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1739cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1740cd7bed00SMika Westerberg 		if (status) {
17418b57b11bSFlavio Suligoi 			dev_warn(dev, "no DMA channels available, using PIO\n");
1742cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1743b6ced294SJarkko Nikula 		} else {
174451eea52dSLubomir Rintel 			controller->can_dma = pxa2xx_spi_can_dma;
1745bf9f742cSMark Brown 			controller->max_dma_len = MAX_DMA_LEN;
1746b2662a16SDaniel Vetter 			controller->max_transfer_size =
1747b2662a16SDaniel Vetter 				pxa2xx_spi_max_dma_transfer_size;
1748ca632f55SGrant Likely 		}
1749ca632f55SGrant Likely 	}
1750ca632f55SGrant Likely 
1751ca632f55SGrant Likely 	/* Enable SOC clock */
175262bbc864STobias Jordan 	status = clk_prepare_enable(ssp->clk);
175362bbc864STobias Jordan 	if (status)
175462bbc864STobias Jordan 		goto out_error_dma_irq_alloc;
17553343b7a6SMika Westerberg 
175651eea52dSLubomir Rintel 	controller->max_speed_hz = clk_get_rate(ssp->clk);
175723cdddb2SJarkko Nikula 	/*
175823cdddb2SJarkko Nikula 	 * Set minimum speed for all other platforms than Intel Quark which is
175923cdddb2SJarkko Nikula 	 * able do under 1 Hz transfers.
176023cdddb2SJarkko Nikula 	 */
176123cdddb2SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
176223cdddb2SJarkko Nikula 		controller->min_speed_hz =
176323cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 4096);
176423cdddb2SJarkko Nikula 	else if (!is_quark_x1000_ssp(drv_data))
176523cdddb2SJarkko Nikula 		controller->min_speed_hz =
176623cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 512);
1767ca632f55SGrant Likely 
17680c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
17690c8ccd8bSAndy Shevchenko 
1770ca632f55SGrant Likely 	/* Load default SSP configuration */
1771e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1772e5262d05SWeike Chen 	case QUARK_X1000_SSP:
17737c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
17747c7289a4SAndy Shevchenko 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1775c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1776e5262d05SWeike Chen 
17778083d6b8SAndy Shevchenko 		/* Using the Motorola SPI protocol and use 8 bit frame */
17787c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
17797c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1780e5262d05SWeike Chen 		break;
17817c7289a4SAndy Shevchenko 	case CE4100_SSP:
17827c7289a4SAndy Shevchenko 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
17837c7289a4SAndy Shevchenko 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
17847c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
17857c7289a4SAndy Shevchenko 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
17867c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1787a2dd8af0SAndy Shevchenko 		break;
1788e5262d05SWeike Chen 	default:
1789ec93cb6fSLubomir Rintel 
179051eea52dSLubomir Rintel 		if (spi_controller_is_slave(controller)) {
1791ec93cb6fSLubomir Rintel 			tmp = SSCR1_SCFR |
1792ec93cb6fSLubomir Rintel 			      SSCR1_SCLKDIR |
1793ec93cb6fSLubomir Rintel 			      SSCR1_SFRMDIR |
1794ec93cb6fSLubomir Rintel 			      SSCR1_RxTresh(2) |
1795ec93cb6fSLubomir Rintel 			      SSCR1_TxTresh(1) |
1796ec93cb6fSLubomir Rintel 			      SSCR1_SPH;
1797ec93cb6fSLubomir Rintel 		} else {
1798c039dd27SJarkko Nikula 			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1799c039dd27SJarkko Nikula 			      SSCR1_TxTresh(TX_THRESH_DFLT);
1800ec93cb6fSLubomir Rintel 		}
1801c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1802ec93cb6fSLubomir Rintel 		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
180351eea52dSLubomir Rintel 		if (!spi_controller_is_slave(controller))
1804ec93cb6fSLubomir Rintel 			tmp |= SSCR0_SCR(2);
1805c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1806e5262d05SWeike Chen 		break;
1807e5262d05SWeike Chen 	}
1808e5262d05SWeike Chen 
1809ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1810c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1811e5262d05SWeike Chen 
1812e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1813c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1814ca632f55SGrant Likely 
18158b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
18168b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
18178b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
18188b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
18198b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
18208b136baaSJarkko Nikula 						   config->reg_capabilities);
18218b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
18228b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
18238b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
182430f3a6abSMika Westerberg 		} else if (config->cs_num) {
182530f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
18268b136baaSJarkko Nikula 		}
18278b136baaSJarkko Nikula 	}
182851eea52dSLubomir Rintel 	controller->num_chipselect = platform_info->num_chipselect;
1829778c12e6SAndy Shevchenko 	controller->use_gpio_descriptors = true;
18306ac5a435SAndy Shevchenko 
183177d33897SLubomir Rintel 	if (platform_info->is_slave) {
183277d33897SLubomir Rintel 		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
183377d33897SLubomir Rintel 						"ready", GPIOD_OUT_LOW);
183477d33897SLubomir Rintel 		if (IS_ERR(drv_data->gpiod_ready)) {
183577d33897SLubomir Rintel 			status = PTR_ERR(drv_data->gpiod_ready);
183677d33897SLubomir Rintel 			goto out_error_clock_enabled;
183777d33897SLubomir Rintel 		}
183877d33897SLubomir Rintel 	}
183977d33897SLubomir Rintel 
1840836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1841836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1842836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1843836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1844836d1a22SAntonio Ospite 
1845ca632f55SGrant Likely 	/* Register with the SPI framework */
1846ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
184732e5b572SLukas Wunner 	status = spi_register_controller(controller);
1848eb743ec6SAndy Shevchenko 	if (status) {
18498083d6b8SAndy Shevchenko 		dev_err(&pdev->dev, "problem registering SPI controller\n");
185012742045SLubomir Rintel 		goto out_error_pm_runtime_enabled;
1851ca632f55SGrant Likely 	}
1852ca632f55SGrant Likely 
1853ca632f55SGrant Likely 	return status;
1854ca632f55SGrant Likely 
185512742045SLubomir Rintel out_error_pm_runtime_enabled:
1856e2b714afSJarkko Nikula 	pm_runtime_disable(&pdev->dev);
185712742045SLubomir Rintel 
185812742045SLubomir Rintel out_error_clock_enabled:
18593343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
186062bbc864STobias Jordan 
186162bbc864STobias Jordan out_error_dma_irq_alloc:
1862cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1863ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1864ca632f55SGrant Likely 
186551eea52dSLubomir Rintel out_error_controller_alloc:
1866ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1867ca632f55SGrant Likely 	return status;
1868ca632f55SGrant Likely }
1869ca632f55SGrant Likely 
1870ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1871ca632f55SGrant Likely {
1872ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
18733d24b2a4SAndy Shevchenko 	struct ssp_device *ssp = drv_data->ssp;
1874ca632f55SGrant Likely 
18757d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
18767d94a505SMika Westerberg 
187732e5b572SLukas Wunner 	spi_unregister_controller(drv_data->controller);
187832e5b572SLukas Wunner 
1879ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
18800c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
18813343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1882ca632f55SGrant Likely 
1883ca632f55SGrant Likely 	/* Release DMA */
188451eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma)
1885cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1886ca632f55SGrant Likely 
18877d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
18887d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
18897d94a505SMika Westerberg 
1890ca632f55SGrant Likely 	/* Release IRQ */
1891ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1892ca632f55SGrant Likely 
1893ca632f55SGrant Likely 	/* Release SSP */
1894ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1895ca632f55SGrant Likely 
1896ca632f55SGrant Likely 	return 0;
1897ca632f55SGrant Likely }
1898ca632f55SGrant Likely 
1899382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP
1900ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1901ca632f55SGrant Likely {
1902ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1903ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1904bffc967eSJarkko Nikula 	int status;
1905ca632f55SGrant Likely 
190651eea52dSLubomir Rintel 	status = spi_controller_suspend(drv_data->controller);
1907eb743ec6SAndy Shevchenko 	if (status)
1908ca632f55SGrant Likely 		return status;
19090c8ccd8bSAndy Shevchenko 
19100c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
19112b9375b9SDmitry Eremin-Solenikov 
19122b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
19133343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1914ca632f55SGrant Likely 
1915ca632f55SGrant Likely 	return 0;
1916ca632f55SGrant Likely }
1917ca632f55SGrant Likely 
1918ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1919ca632f55SGrant Likely {
1920ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1921ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1922bffc967eSJarkko Nikula 	int status;
1923ca632f55SGrant Likely 
1924ca632f55SGrant Likely 	/* Enable the SSP clock */
192562bbc864STobias Jordan 	if (!pm_runtime_suspended(dev)) {
192662bbc864STobias Jordan 		status = clk_prepare_enable(ssp->clk);
192762bbc864STobias Jordan 		if (status)
192862bbc864STobias Jordan 			return status;
192962bbc864STobias Jordan 	}
1930ca632f55SGrant Likely 
1931ca632f55SGrant Likely 	/* Start the queue running */
193251eea52dSLubomir Rintel 	return spi_controller_resume(drv_data->controller);
1933ca632f55SGrant Likely }
19347d94a505SMika Westerberg #endif
19357d94a505SMika Westerberg 
1936ec833050SRafael J. Wysocki #ifdef CONFIG_PM
19377d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
19387d94a505SMika Westerberg {
19397d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
19407d94a505SMika Westerberg 
19417d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
19427d94a505SMika Westerberg 	return 0;
19437d94a505SMika Westerberg }
19447d94a505SMika Westerberg 
19457d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
19467d94a505SMika Westerberg {
19477d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
194862bbc864STobias Jordan 	int status;
19497d94a505SMika Westerberg 
195062bbc864STobias Jordan 	status = clk_prepare_enable(drv_data->ssp->clk);
195162bbc864STobias Jordan 	return status;
19527d94a505SMika Westerberg }
19537d94a505SMika Westerberg #endif
1954ca632f55SGrant Likely 
1955ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
19567d94a505SMika Westerberg 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
19577d94a505SMika Westerberg 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
19587d94a505SMika Westerberg 			   pxa2xx_spi_runtime_resume, NULL)
1959ca632f55SGrant Likely };
1960ca632f55SGrant Likely 
1961ca632f55SGrant Likely static struct platform_driver driver = {
1962ca632f55SGrant Likely 	.driver = {
1963ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
1964ca632f55SGrant Likely 		.pm	= &pxa2xx_spi_pm_ops,
1965a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
196687ae1d2dSLubomir Rintel 		.of_match_table = of_match_ptr(pxa2xx_spi_of_match),
1967ca632f55SGrant Likely 	},
1968ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
1969ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
1970ca632f55SGrant Likely };
1971ca632f55SGrant Likely 
1972ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
1973ca632f55SGrant Likely {
1974ca632f55SGrant Likely 	return platform_driver_register(&driver);
1975ca632f55SGrant Likely }
1976ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
1977ca632f55SGrant Likely 
1978ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
1979ca632f55SGrant Likely {
1980ca632f55SGrant Likely 	platform_driver_unregister(&driver);
1981ca632f55SGrant Likely }
1982ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
198351ebf6acSFlavio Suligoi 
198451ebf6acSFlavio Suligoi MODULE_SOFTDEP("pre: dw_dmac");
1985