xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision 1a1864cd879a1497fd46f6ea2f31ddcde8385585)
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/init.h>
17ca632f55SGrant Likely #include <linux/interrupt.h>
185ce25705SAndy Shevchenko #include <linux/ioport.h>
199df461ecSAndy Shevchenko #include <linux/kernel.h>
205ce25705SAndy Shevchenko #include <linux/module.h>
21ae8fbf1dSAndy Shevchenko #include <linux/mod_devicetable.h>
22ae8fbf1dSAndy Shevchenko #include <linux/of.h>
2334cadd9cSJarkko Nikula #include <linux/pci.h>
24ca632f55SGrant Likely #include <linux/platform_device.h>
255ce25705SAndy Shevchenko #include <linux/pm_runtime.h>
26f2faa3ecSAndy Shevchenko #include <linux/property.h>
275ce25705SAndy Shevchenko #include <linux/slab.h>
280e476871SAndy Shevchenko 
29ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h>
30ca632f55SGrant Likely #include <linux/spi/spi.h>
31ca632f55SGrant Likely 
32cd7bed00SMika Westerberg #include "spi-pxa2xx.h"
33ca632f55SGrant Likely 
34ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street");
35ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
36ca632f55SGrant Likely MODULE_LICENSE("GPL");
37ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi");
38ca632f55SGrant Likely 
39ca632f55SGrant Likely #define TIMOUT_DFLT		1000
40ca632f55SGrant Likely 
41ca632f55SGrant Likely /*
428083d6b8SAndy Shevchenko  * For testing SSCR1 changes that require SSP restart, basically
438083d6b8SAndy Shevchenko  * everything except the service and interrupt enables, the PXA270 developer
44ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
458083d6b8SAndy Shevchenko  * list, but the PXA255 developer manual says all bits without really meaning
468083d6b8SAndy Shevchenko  * the service and interrupt enables.
47ca632f55SGrant Likely  */
48ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
49ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
50ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
51ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
52ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
53ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
54ca632f55SGrant Likely 
55e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
56e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
57e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
58e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
59e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
60e5262d05SWeike Chen 
617c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
627c7289a4SAndy Shevchenko 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
637c7289a4SAndy Shevchenko 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
647c7289a4SAndy Shevchenko 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
657c7289a4SAndy Shevchenko 				| CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
667c7289a4SAndy Shevchenko 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
677c7289a4SAndy Shevchenko 
68624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
69624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE			BIT(0)
70624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
718b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT			9
728b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
73a0d2642eSMika Westerberg 
74683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE 0x38
75683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
76683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
77683f65deSEvan Green 
78dccf7369SJarkko Nikula struct lpss_config {
79dccf7369SJarkko Nikula 	/* LPSS offset from drv_data->ioaddr */
80dccf7369SJarkko Nikula 	unsigned offset;
81dccf7369SJarkko Nikula 	/* Register offsets from drv_data->lpss_base or -1 */
82dccf7369SJarkko Nikula 	int reg_general;
83dccf7369SJarkko Nikula 	int reg_ssp;
84dccf7369SJarkko Nikula 	int reg_cs_ctrl;
858b136baaSJarkko Nikula 	int reg_capabilities;
86dccf7369SJarkko Nikula 	/* FIFO thresholds */
87dccf7369SJarkko Nikula 	u32 rx_threshold;
88dccf7369SJarkko Nikula 	u32 tx_threshold_lo;
89dccf7369SJarkko Nikula 	u32 tx_threshold_hi;
90c1e4a53cSMika Westerberg 	/* Chip select control */
91c1e4a53cSMika Westerberg 	unsigned cs_sel_shift;
92c1e4a53cSMika Westerberg 	unsigned cs_sel_mask;
9330f3a6abSMika Westerberg 	unsigned cs_num;
94683f65deSEvan Green 	/* Quirks */
95683f65deSEvan Green 	unsigned cs_clk_stays_gated : 1;
96dccf7369SJarkko Nikula };
97dccf7369SJarkko Nikula 
98dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */
99dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = {
100dccf7369SJarkko Nikula 	{	/* LPSS_LPT_SSP */
101dccf7369SJarkko Nikula 		.offset = 0x800,
102dccf7369SJarkko Nikula 		.reg_general = 0x08,
103dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
104dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1058b136baaSJarkko Nikula 		.reg_capabilities = -1,
106dccf7369SJarkko Nikula 		.rx_threshold = 64,
107dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
108dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
109dccf7369SJarkko Nikula 	},
110dccf7369SJarkko Nikula 	{	/* LPSS_BYT_SSP */
111dccf7369SJarkko Nikula 		.offset = 0x400,
112dccf7369SJarkko Nikula 		.reg_general = 0x08,
113dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
114dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1158b136baaSJarkko Nikula 		.reg_capabilities = -1,
116dccf7369SJarkko Nikula 		.rx_threshold = 64,
117dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
118dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
119dccf7369SJarkko Nikula 	},
12030f3a6abSMika Westerberg 	{	/* LPSS_BSW_SSP */
12130f3a6abSMika Westerberg 		.offset = 0x400,
12230f3a6abSMika Westerberg 		.reg_general = 0x08,
12330f3a6abSMika Westerberg 		.reg_ssp = 0x0c,
12430f3a6abSMika Westerberg 		.reg_cs_ctrl = 0x18,
12530f3a6abSMika Westerberg 		.reg_capabilities = -1,
12630f3a6abSMika Westerberg 		.rx_threshold = 64,
12730f3a6abSMika Westerberg 		.tx_threshold_lo = 160,
12830f3a6abSMika Westerberg 		.tx_threshold_hi = 224,
12930f3a6abSMika Westerberg 		.cs_sel_shift = 2,
13030f3a6abSMika Westerberg 		.cs_sel_mask = 1 << 2,
13130f3a6abSMika Westerberg 		.cs_num = 2,
13230f3a6abSMika Westerberg 	},
13334cadd9cSJarkko Nikula 	{	/* LPSS_SPT_SSP */
13434cadd9cSJarkko Nikula 		.offset = 0x200,
13534cadd9cSJarkko Nikula 		.reg_general = -1,
13634cadd9cSJarkko Nikula 		.reg_ssp = 0x20,
13734cadd9cSJarkko Nikula 		.reg_cs_ctrl = 0x24,
13866ec246eSJarkko Nikula 		.reg_capabilities = -1,
13934cadd9cSJarkko Nikula 		.rx_threshold = 1,
14034cadd9cSJarkko Nikula 		.tx_threshold_lo = 32,
14134cadd9cSJarkko Nikula 		.tx_threshold_hi = 56,
14234cadd9cSJarkko Nikula 	},
143b7c08cf8SJarkko Nikula 	{	/* LPSS_BXT_SSP */
144b7c08cf8SJarkko Nikula 		.offset = 0x200,
145b7c08cf8SJarkko Nikula 		.reg_general = -1,
146b7c08cf8SJarkko Nikula 		.reg_ssp = 0x20,
147b7c08cf8SJarkko Nikula 		.reg_cs_ctrl = 0x24,
148b7c08cf8SJarkko Nikula 		.reg_capabilities = 0xfc,
149b7c08cf8SJarkko Nikula 		.rx_threshold = 1,
150b7c08cf8SJarkko Nikula 		.tx_threshold_lo = 16,
151b7c08cf8SJarkko Nikula 		.tx_threshold_hi = 48,
152c1e4a53cSMika Westerberg 		.cs_sel_shift = 8,
153c1e4a53cSMika Westerberg 		.cs_sel_mask = 3 << 8,
1546eefaee4SEvan Green 		.cs_clk_stays_gated = true,
155b7c08cf8SJarkko Nikula 	},
156fc0b2accSJarkko Nikula 	{	/* LPSS_CNL_SSP */
157fc0b2accSJarkko Nikula 		.offset = 0x200,
158fc0b2accSJarkko Nikula 		.reg_general = -1,
159fc0b2accSJarkko Nikula 		.reg_ssp = 0x20,
160fc0b2accSJarkko Nikula 		.reg_cs_ctrl = 0x24,
161fc0b2accSJarkko Nikula 		.reg_capabilities = 0xfc,
162fc0b2accSJarkko Nikula 		.rx_threshold = 1,
163fc0b2accSJarkko Nikula 		.tx_threshold_lo = 32,
164fc0b2accSJarkko Nikula 		.tx_threshold_hi = 56,
165fc0b2accSJarkko Nikula 		.cs_sel_shift = 8,
166fc0b2accSJarkko Nikula 		.cs_sel_mask = 3 << 8,
167683f65deSEvan Green 		.cs_clk_stays_gated = true,
168fc0b2accSJarkko Nikula 	},
169dccf7369SJarkko Nikula };
170dccf7369SJarkko Nikula 
171dccf7369SJarkko Nikula static inline const struct lpss_config
172dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data)
173dccf7369SJarkko Nikula {
174dccf7369SJarkko Nikula 	return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
175dccf7369SJarkko Nikula }
176dccf7369SJarkko Nikula 
177a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data)
178a0d2642eSMika Westerberg {
17903fbf488SJarkko Nikula 	switch (drv_data->ssp_type) {
18003fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
18103fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
18230f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
18334cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
184b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
185fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
18603fbf488SJarkko Nikula 		return true;
18703fbf488SJarkko Nikula 	default:
18803fbf488SJarkko Nikula 		return false;
18903fbf488SJarkko Nikula 	}
190a0d2642eSMika Westerberg }
191a0d2642eSMika Westerberg 
192e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
193e5262d05SWeike Chen {
194e5262d05SWeike Chen 	return drv_data->ssp_type == QUARK_X1000_SSP;
195e5262d05SWeike Chen }
196e5262d05SWeike Chen 
19741c98841SAndy Shevchenko static bool is_mmp2_ssp(const struct driver_data *drv_data)
19841c98841SAndy Shevchenko {
19941c98841SAndy Shevchenko 	return drv_data->ssp_type == MMP2_SSP;
20041c98841SAndy Shevchenko }
20141c98841SAndy Shevchenko 
2023fdb59cfSAndy Shevchenko static bool is_mrfld_ssp(const struct driver_data *drv_data)
2033fdb59cfSAndy Shevchenko {
2043fdb59cfSAndy Shevchenko 	return drv_data->ssp_type == MRFLD_SSP;
2053fdb59cfSAndy Shevchenko }
2063fdb59cfSAndy Shevchenko 
2071bed378cSAndy Shevchenko static void pxa2xx_spi_update(const struct driver_data *drv_data, u32 reg, u32 mask, u32 value)
2081bed378cSAndy Shevchenko {
2091bed378cSAndy Shevchenko 	if ((pxa2xx_spi_read(drv_data, reg) & mask) != value)
2101bed378cSAndy Shevchenko 		pxa2xx_spi_write(drv_data, reg, value & mask);
2111bed378cSAndy Shevchenko }
2121bed378cSAndy Shevchenko 
2134fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
2144fdb2424SWeike Chen {
2154fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
216e5262d05SWeike Chen 	case QUARK_X1000_SSP:
217e5262d05SWeike Chen 		return QUARK_X1000_SSCR1_CHANGE_MASK;
2187c7289a4SAndy Shevchenko 	case CE4100_SSP:
2197c7289a4SAndy Shevchenko 		return CE4100_SSCR1_CHANGE_MASK;
2204fdb2424SWeike Chen 	default:
2214fdb2424SWeike Chen 		return SSCR1_CHANGE_MASK;
2224fdb2424SWeike Chen 	}
2234fdb2424SWeike Chen }
2244fdb2424SWeike Chen 
2254fdb2424SWeike Chen static u32
2264fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
2274fdb2424SWeike Chen {
2284fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
229e5262d05SWeike Chen 	case QUARK_X1000_SSP:
230e5262d05SWeike Chen 		return RX_THRESH_QUARK_X1000_DFLT;
2317c7289a4SAndy Shevchenko 	case CE4100_SSP:
2327c7289a4SAndy Shevchenko 		return RX_THRESH_CE4100_DFLT;
2334fdb2424SWeike Chen 	default:
2344fdb2424SWeike Chen 		return RX_THRESH_DFLT;
2354fdb2424SWeike Chen 	}
2364fdb2424SWeike Chen }
2374fdb2424SWeike Chen 
2384fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
2394fdb2424SWeike Chen {
2404fdb2424SWeike Chen 	u32 mask;
2414fdb2424SWeike Chen 
2424fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
243e5262d05SWeike Chen 	case QUARK_X1000_SSP:
244e5262d05SWeike Chen 		mask = QUARK_X1000_SSSR_TFL_MASK;
245e5262d05SWeike Chen 		break;
2467c7289a4SAndy Shevchenko 	case CE4100_SSP:
2477c7289a4SAndy Shevchenko 		mask = CE4100_SSSR_TFL_MASK;
2487c7289a4SAndy Shevchenko 		break;
2494fdb2424SWeike Chen 	default:
2504fdb2424SWeike Chen 		mask = SSSR_TFL_MASK;
2514fdb2424SWeike Chen 		break;
2524fdb2424SWeike Chen 	}
2534fdb2424SWeike Chen 
2546d380132SAndy Shevchenko 	return read_SSSR_bits(drv_data, mask) == mask;
2554fdb2424SWeike Chen }
2564fdb2424SWeike Chen 
2574fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
2584fdb2424SWeike Chen 				     u32 *sccr1_reg)
2594fdb2424SWeike Chen {
2604fdb2424SWeike Chen 	u32 mask;
2614fdb2424SWeike Chen 
2624fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
263e5262d05SWeike Chen 	case QUARK_X1000_SSP:
264e5262d05SWeike Chen 		mask = QUARK_X1000_SSCR1_RFT;
265e5262d05SWeike Chen 		break;
2667c7289a4SAndy Shevchenko 	case CE4100_SSP:
2677c7289a4SAndy Shevchenko 		mask = CE4100_SSCR1_RFT;
2687c7289a4SAndy Shevchenko 		break;
2694fdb2424SWeike Chen 	default:
2704fdb2424SWeike Chen 		mask = SSCR1_RFT;
2714fdb2424SWeike Chen 		break;
2724fdb2424SWeike Chen 	}
2734fdb2424SWeike Chen 	*sccr1_reg &= ~mask;
2744fdb2424SWeike Chen }
2754fdb2424SWeike Chen 
2764fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
2774fdb2424SWeike Chen 				   u32 *sccr1_reg, u32 threshold)
2784fdb2424SWeike Chen {
2794fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
280e5262d05SWeike Chen 	case QUARK_X1000_SSP:
281e5262d05SWeike Chen 		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
282e5262d05SWeike Chen 		break;
2837c7289a4SAndy Shevchenko 	case CE4100_SSP:
2847c7289a4SAndy Shevchenko 		*sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
2857c7289a4SAndy Shevchenko 		break;
2864fdb2424SWeike Chen 	default:
2874fdb2424SWeike Chen 		*sccr1_reg |= SSCR1_RxTresh(threshold);
2884fdb2424SWeike Chen 		break;
2894fdb2424SWeike Chen 	}
2904fdb2424SWeike Chen }
2914fdb2424SWeike Chen 
2924fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
2934fdb2424SWeike Chen 				  u32 clk_div, u8 bits)
2944fdb2424SWeike Chen {
2954fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
296e5262d05SWeike Chen 	case QUARK_X1000_SSP:
297e5262d05SWeike Chen 		return clk_div
298e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_Motorola
2990c8ccd8bSAndy Shevchenko 			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits);
3004fdb2424SWeike Chen 	default:
3014fdb2424SWeike Chen 		return clk_div
3024fdb2424SWeike Chen 			| SSCR0_Motorola
3034fdb2424SWeike Chen 			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
3044fdb2424SWeike Chen 			| (bits > 16 ? SSCR0_EDSS : 0);
3054fdb2424SWeike Chen 	}
3064fdb2424SWeike Chen }
3074fdb2424SWeike Chen 
308a0d2642eSMika Westerberg /*
309a0d2642eSMika Westerberg  * Read and write LPSS SSP private registers. Caller must first check that
310a0d2642eSMika Westerberg  * is_lpss_ssp() returns true before these can be called.
311a0d2642eSMika Westerberg  */
312a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
313a0d2642eSMika Westerberg {
314a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
315a0d2642eSMika Westerberg 	return readl(drv_data->lpss_base + offset);
316a0d2642eSMika Westerberg }
317a0d2642eSMika Westerberg 
318a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data,
319a0d2642eSMika Westerberg 				  unsigned offset, u32 value)
320a0d2642eSMika Westerberg {
321a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
322a0d2642eSMika Westerberg 	writel(value, drv_data->lpss_base + offset);
323a0d2642eSMika Westerberg }
324a0d2642eSMika Westerberg 
325a0d2642eSMika Westerberg /*
326a0d2642eSMika Westerberg  * lpss_ssp_setup - perform LPSS SSP specific setup
327a0d2642eSMika Westerberg  * @drv_data: pointer to the driver private data
328a0d2642eSMika Westerberg  *
329a0d2642eSMika Westerberg  * Perform LPSS SSP specific setup. This function must be called first if
330a0d2642eSMika Westerberg  * one is going to use LPSS SSP private registers.
331a0d2642eSMika Westerberg  */
332a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data)
333a0d2642eSMika Westerberg {
334dccf7369SJarkko Nikula 	const struct lpss_config *config;
335dccf7369SJarkko Nikula 	u32 value;
336a0d2642eSMika Westerberg 
337dccf7369SJarkko Nikula 	config = lpss_get_config(drv_data);
3389e43c9a8SAndy Shevchenko 	drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset;
339a0d2642eSMika Westerberg 
340a0d2642eSMika Westerberg 	/* Enable software chip select control */
3410e897218SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
342624ea72eSJarkko Nikula 	value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
343624ea72eSJarkko Nikula 	value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
344dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
3450054e28dSMika Westerberg 
3460054e28dSMika Westerberg 	/* Enable multiblock DMA transfers */
34751eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma) {
348dccf7369SJarkko Nikula 		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
3491de70612SMika Westerberg 
35082ba2c2aSJarkko Nikula 		if (config->reg_general >= 0) {
35182ba2c2aSJarkko Nikula 			value = __lpss_ssp_read_priv(drv_data,
35282ba2c2aSJarkko Nikula 						     config->reg_general);
353624ea72eSJarkko Nikula 			value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
35482ba2c2aSJarkko Nikula 			__lpss_ssp_write_priv(drv_data,
35582ba2c2aSJarkko Nikula 					      config->reg_general, value);
35682ba2c2aSJarkko Nikula 		}
3571de70612SMika Westerberg 	}
358a0d2642eSMika Westerberg }
359a0d2642eSMika Westerberg 
360d5898e19SJarkko Nikula static void lpss_ssp_select_cs(struct spi_device *spi,
361c1e4a53cSMika Westerberg 			       const struct lpss_config *config)
362a0d2642eSMika Westerberg {
363d5898e19SJarkko Nikula 	struct driver_data *drv_data =
364d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
365d0283eb2SJarkko Nikula 	u32 value, cs;
366a0d2642eSMika Westerberg 
367c1e4a53cSMika Westerberg 	if (!config->cs_sel_mask)
368c1e4a53cSMika Westerberg 		return;
369dccf7369SJarkko Nikula 
370dccf7369SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
371c1e4a53cSMika Westerberg 
372d5898e19SJarkko Nikula 	cs = spi->chip_select;
373c1e4a53cSMika Westerberg 	cs <<= config->cs_sel_shift;
374c1e4a53cSMika Westerberg 	if (cs != (value & config->cs_sel_mask)) {
375d0283eb2SJarkko Nikula 		/*
376c1e4a53cSMika Westerberg 		 * When switching another chip select output active the
377c1e4a53cSMika Westerberg 		 * output must be selected first and wait 2 ssp_clk cycles
378c1e4a53cSMika Westerberg 		 * before changing state to active. Otherwise a short
379c1e4a53cSMika Westerberg 		 * glitch will occur on the previous chip select since
380c1e4a53cSMika Westerberg 		 * output select is latched but state control is not.
381d0283eb2SJarkko Nikula 		 */
382c1e4a53cSMika Westerberg 		value &= ~config->cs_sel_mask;
383d0283eb2SJarkko Nikula 		value |= cs;
384d0283eb2SJarkko Nikula 		__lpss_ssp_write_priv(drv_data,
385d0283eb2SJarkko Nikula 				      config->reg_cs_ctrl, value);
386d0283eb2SJarkko Nikula 		ndelay(1000000000 /
38751eea52dSLubomir Rintel 		       (drv_data->controller->max_speed_hz / 2));
388d0283eb2SJarkko Nikula 	}
389d0283eb2SJarkko Nikula }
390c1e4a53cSMika Westerberg 
391d5898e19SJarkko Nikula static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
392c1e4a53cSMika Westerberg {
393d5898e19SJarkko Nikula 	struct driver_data *drv_data =
394d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
395c1e4a53cSMika Westerberg 	const struct lpss_config *config;
396c1e4a53cSMika Westerberg 	u32 value;
397c1e4a53cSMika Westerberg 
398c1e4a53cSMika Westerberg 	config = lpss_get_config(drv_data);
399c1e4a53cSMika Westerberg 
400c1e4a53cSMika Westerberg 	if (enable)
401d5898e19SJarkko Nikula 		lpss_ssp_select_cs(spi, config);
402c1e4a53cSMika Westerberg 
403c1e4a53cSMika Westerberg 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
404c1e4a53cSMika Westerberg 	if (enable)
405c1e4a53cSMika Westerberg 		value &= ~LPSS_CS_CONTROL_CS_HIGH;
406c1e4a53cSMika Westerberg 	else
407c1e4a53cSMika Westerberg 		value |= LPSS_CS_CONTROL_CS_HIGH;
408dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
409683f65deSEvan Green 	if (config->cs_clk_stays_gated) {
410683f65deSEvan Green 		u32 clkgate;
411683f65deSEvan Green 
412683f65deSEvan Green 		/*
413683f65deSEvan Green 		 * Changing CS alone when dynamic clock gating is on won't
414683f65deSEvan Green 		 * actually flip CS at that time. This ruins SPI transfers
415683f65deSEvan Green 		 * that specify delays, or have no data. Toggle the clock mode
416683f65deSEvan Green 		 * to force on briefly to poke the CS pin to move.
417683f65deSEvan Green 		 */
418683f65deSEvan Green 		clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
419683f65deSEvan Green 		value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
420683f65deSEvan Green 			LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
421683f65deSEvan Green 
422683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
423683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
424683f65deSEvan Green 	}
425a0d2642eSMika Westerberg }
426a0d2642eSMika Westerberg 
427d5898e19SJarkko Nikula static void cs_assert(struct spi_device *spi)
428ca632f55SGrant Likely {
429d5898e19SJarkko Nikula 	struct driver_data *drv_data =
430d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
431ca632f55SGrant Likely 
432ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP) {
433ccd60b20SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSSR, spi->chip_select);
434ca632f55SGrant Likely 		return;
435ca632f55SGrant Likely 	}
436ca632f55SGrant Likely 
4377566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
438d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, true);
439ca632f55SGrant Likely }
440ca632f55SGrant Likely 
441d5898e19SJarkko Nikula static void cs_deassert(struct spi_device *spi)
442ca632f55SGrant Likely {
443d5898e19SJarkko Nikula 	struct driver_data *drv_data =
444d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
445104e51afSJarkko Nikula 	unsigned long timeout;
446ca632f55SGrant Likely 
447ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
448ca632f55SGrant Likely 		return;
449ca632f55SGrant Likely 
450104e51afSJarkko Nikula 	/* Wait until SSP becomes idle before deasserting the CS */
451104e51afSJarkko Nikula 	timeout = jiffies + msecs_to_jiffies(10);
452104e51afSJarkko Nikula 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
453104e51afSJarkko Nikula 	       !time_after(jiffies, timeout))
454104e51afSJarkko Nikula 		cpu_relax();
455104e51afSJarkko Nikula 
4567566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
457d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, false);
458d5898e19SJarkko Nikula }
459d5898e19SJarkko Nikula 
460d5898e19SJarkko Nikula static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
461d5898e19SJarkko Nikula {
462d5898e19SJarkko Nikula 	if (level)
463d5898e19SJarkko Nikula 		cs_deassert(spi);
464d5898e19SJarkko Nikula 	else
465d5898e19SJarkko Nikula 		cs_assert(spi);
466ca632f55SGrant Likely }
467ca632f55SGrant Likely 
468cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
469ca632f55SGrant Likely {
470ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
471ca632f55SGrant Likely 
472ca632f55SGrant Likely 	do {
4736d380132SAndy Shevchenko 		while (read_SSSR_bits(drv_data, SSSR_RNE))
474c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
475c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
476ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
477ca632f55SGrant Likely 
478ca632f55SGrant Likely 	return limit;
479ca632f55SGrant Likely }
480ca632f55SGrant Likely 
48129d7e05cSLubomir Rintel static void pxa2xx_spi_off(struct driver_data *drv_data)
48229d7e05cSLubomir Rintel {
48341c98841SAndy Shevchenko 	/* On MMP, disabling SSE seems to corrupt the Rx FIFO */
48441c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data))
48529d7e05cSLubomir Rintel 		return;
48629d7e05cSLubomir Rintel 
4870c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(drv_data->ssp);
48829d7e05cSLubomir Rintel }
48929d7e05cSLubomir Rintel 
490ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
491ca632f55SGrant Likely {
492ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
493ca632f55SGrant Likely 
4944fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
495ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
496ca632f55SGrant Likely 		return 0;
497ca632f55SGrant Likely 
498c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
499ca632f55SGrant Likely 	drv_data->tx += n_bytes;
500ca632f55SGrant Likely 
501ca632f55SGrant Likely 	return 1;
502ca632f55SGrant Likely }
503ca632f55SGrant Likely 
504ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
505ca632f55SGrant Likely {
506ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
507ca632f55SGrant Likely 
5086d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
509c039dd27SJarkko Nikula 		pxa2xx_spi_read(drv_data, SSDR);
510ca632f55SGrant Likely 		drv_data->rx += n_bytes;
511ca632f55SGrant Likely 	}
512ca632f55SGrant Likely 
513ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
514ca632f55SGrant Likely }
515ca632f55SGrant Likely 
516ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data)
517ca632f55SGrant Likely {
5184fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
519ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
520ca632f55SGrant Likely 		return 0;
521ca632f55SGrant Likely 
522c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
523ca632f55SGrant Likely 	++drv_data->tx;
524ca632f55SGrant Likely 
525ca632f55SGrant Likely 	return 1;
526ca632f55SGrant Likely }
527ca632f55SGrant Likely 
528ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data)
529ca632f55SGrant Likely {
5306d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
531c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
532ca632f55SGrant Likely 		++drv_data->rx;
533ca632f55SGrant Likely 	}
534ca632f55SGrant Likely 
535ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
536ca632f55SGrant Likely }
537ca632f55SGrant Likely 
538ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
539ca632f55SGrant Likely {
5404fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
541ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
542ca632f55SGrant Likely 		return 0;
543ca632f55SGrant Likely 
544c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
545ca632f55SGrant Likely 	drv_data->tx += 2;
546ca632f55SGrant Likely 
547ca632f55SGrant Likely 	return 1;
548ca632f55SGrant Likely }
549ca632f55SGrant Likely 
550ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
551ca632f55SGrant Likely {
5526d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
553c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
554ca632f55SGrant Likely 		drv_data->rx += 2;
555ca632f55SGrant Likely 	}
556ca632f55SGrant Likely 
557ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
558ca632f55SGrant Likely }
559ca632f55SGrant Likely 
560ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
561ca632f55SGrant Likely {
5624fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
563ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
564ca632f55SGrant Likely 		return 0;
565ca632f55SGrant Likely 
566c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
567ca632f55SGrant Likely 	drv_data->tx += 4;
568ca632f55SGrant Likely 
569ca632f55SGrant Likely 	return 1;
570ca632f55SGrant Likely }
571ca632f55SGrant Likely 
572ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
573ca632f55SGrant Likely {
5746d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
575c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
576ca632f55SGrant Likely 		drv_data->rx += 4;
577ca632f55SGrant Likely 	}
578ca632f55SGrant Likely 
579ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
580ca632f55SGrant Likely }
581ca632f55SGrant Likely 
582ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
583ca632f55SGrant Likely {
584e3aa9accSAndy Shevchenko 	u32 mask = drv_data->int_cr1 | drv_data->dma_cr1, threshold;
585e3aa9accSAndy Shevchenko 	struct chip_data *chip;
586e3aa9accSAndy Shevchenko 
587e3aa9accSAndy Shevchenko 	if (drv_data->controller->cur_msg) {
588e3aa9accSAndy Shevchenko 		chip = spi_get_ctldata(drv_data->controller->cur_msg->spi);
589e3aa9accSAndy Shevchenko 		threshold = chip->threshold;
590e3aa9accSAndy Shevchenko 	} else {
591e3aa9accSAndy Shevchenko 		threshold = 0;
592e3aa9accSAndy Shevchenko 	}
593ca632f55SGrant Likely 
594152bc19eSAndy Shevchenko 	switch (drv_data->ssp_type) {
595152bc19eSAndy Shevchenko 	case QUARK_X1000_SSP:
596e0a6512dSAndy Shevchenko 		mask |= QUARK_X1000_SSCR1_RFT;
597152bc19eSAndy Shevchenko 		break;
5987c7289a4SAndy Shevchenko 	case CE4100_SSP:
599e0a6512dSAndy Shevchenko 		mask |= CE4100_SSCR1_RFT;
6007c7289a4SAndy Shevchenko 		break;
601152bc19eSAndy Shevchenko 	default:
602e0a6512dSAndy Shevchenko 		mask |= SSCR1_RFT;
603152bc19eSAndy Shevchenko 		break;
604152bc19eSAndy Shevchenko 	}
605e0a6512dSAndy Shevchenko 
606e3aa9accSAndy Shevchenko 	pxa2xx_spi_update(drv_data, SSCR1, mask, threshold);
607ca632f55SGrant Likely }
608ca632f55SGrant Likely 
609ab77fe89SAndy Shevchenko static void int_stop_and_reset(struct driver_data *drv_data)
610ca632f55SGrant Likely {
611ab77fe89SAndy Shevchenko 	/* Clear and disable interrupts */
612ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
613ca632f55SGrant Likely 	reset_sccr1(drv_data);
614ab77fe89SAndy Shevchenko 	if (pxa25x_ssp_comp(drv_data))
615ab77fe89SAndy Shevchenko 		return;
616ab77fe89SAndy Shevchenko 
617c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSTO, 0);
618ab77fe89SAndy Shevchenko }
619ab77fe89SAndy Shevchenko 
6204761d2e7SAndy Shevchenko static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
621ab77fe89SAndy Shevchenko {
622ab77fe89SAndy Shevchenko 	int_stop_and_reset(drv_data);
623cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
62429d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
625ca632f55SGrant Likely 
626c3dce24cSAndy Shevchenko 	dev_err(drv_data->ssp->dev, "%s\n", msg);
627ca632f55SGrant Likely 
6284761d2e7SAndy Shevchenko 	drv_data->controller->cur_msg->status = err;
62951eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
630ca632f55SGrant Likely }
631ca632f55SGrant Likely 
632ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
633ca632f55SGrant Likely {
634ab77fe89SAndy Shevchenko 	int_stop_and_reset(drv_data);
635ca632f55SGrant Likely 
63651eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
637ca632f55SGrant Likely }
638ca632f55SGrant Likely 
639ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
640ca632f55SGrant Likely {
6416d380132SAndy Shevchenko 	u32 irq_status;
642ca632f55SGrant Likely 
6436d380132SAndy Shevchenko 	irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr);
6446d380132SAndy Shevchenko 	if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE))
6456d380132SAndy Shevchenko 		irq_status &= ~SSSR_TFS;
646ca632f55SGrant Likely 
647ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
6488083d6b8SAndy Shevchenko 		int_error_stop(drv_data, "interrupt_transfer: FIFO overrun", -EIO);
649ca632f55SGrant Likely 		return IRQ_HANDLED;
650ca632f55SGrant Likely 	}
651ca632f55SGrant Likely 
652ec93cb6fSLubomir Rintel 	if (irq_status & SSSR_TUR) {
6538083d6b8SAndy Shevchenko 		int_error_stop(drv_data, "interrupt_transfer: FIFO underrun", -EIO);
654ec93cb6fSLubomir Rintel 		return IRQ_HANDLED;
655ec93cb6fSLubomir Rintel 	}
656ec93cb6fSLubomir Rintel 
657ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
658c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
659ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
660ca632f55SGrant Likely 			int_transfer_complete(drv_data);
661ca632f55SGrant Likely 			return IRQ_HANDLED;
662ca632f55SGrant Likely 		}
663ca632f55SGrant Likely 	}
664ca632f55SGrant Likely 
6658083d6b8SAndy Shevchenko 	/* Drain Rx FIFO, Fill Tx FIFO and prevent overruns */
666ca632f55SGrant Likely 	do {
667ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
668ca632f55SGrant Likely 			int_transfer_complete(drv_data);
669ca632f55SGrant Likely 			return IRQ_HANDLED;
670ca632f55SGrant Likely 		}
671ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
672ca632f55SGrant Likely 
673ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
674ca632f55SGrant Likely 		int_transfer_complete(drv_data);
675ca632f55SGrant Likely 		return IRQ_HANDLED;
676ca632f55SGrant Likely 	}
677ca632f55SGrant Likely 
678ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
679ca632f55SGrant Likely 		u32 bytes_left;
680ca632f55SGrant Likely 		u32 sccr1_reg;
681ca632f55SGrant Likely 
682c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
683ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
684ca632f55SGrant Likely 
685ca632f55SGrant Likely 		/*
6868083d6b8SAndy Shevchenko 		 * PXA25x_SSP has no timeout, set up Rx threshold for
6878083d6b8SAndy Shevchenko 		 * the remaining Rx bytes.
688ca632f55SGrant Likely 		 */
689ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
6904fdb2424SWeike Chen 			u32 rx_thre;
691ca632f55SGrant Likely 
6924fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
693ca632f55SGrant Likely 
694ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
695ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
696ca632f55SGrant Likely 			case 4:
6972c183376SGustavo A. R. Silva 				bytes_left >>= 2;
6982c183376SGustavo A. R. Silva 				break;
699ca632f55SGrant Likely 			case 2:
700ca632f55SGrant Likely 				bytes_left >>= 1;
7012c183376SGustavo A. R. Silva 				break;
702ca632f55SGrant Likely 			}
703ca632f55SGrant Likely 
7044fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
7054fdb2424SWeike Chen 			if (rx_thre > bytes_left)
7064fdb2424SWeike Chen 				rx_thre = bytes_left;
707ca632f55SGrant Likely 
7084fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
709ca632f55SGrant Likely 		}
710c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
711ca632f55SGrant Likely 	}
712ca632f55SGrant Likely 
713ca632f55SGrant Likely 	/* We did something */
714ca632f55SGrant Likely 	return IRQ_HANDLED;
715ca632f55SGrant Likely }
716ca632f55SGrant Likely 
717b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data)
718b0312482SJan Kiszka {
7193bbdc083SAndy Shevchenko 	int_stop_and_reset(drv_data);
72029d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
721b0312482SJan Kiszka 
722c3dce24cSAndy Shevchenko 	dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
723b0312482SJan Kiszka }
724b0312482SJan Kiszka 
725ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
726ca632f55SGrant Likely {
727ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
7287d94a505SMika Westerberg 	u32 sccr1_reg;
729ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
730ca632f55SGrant Likely 	u32 status;
731ca632f55SGrant Likely 
7327d94a505SMika Westerberg 	/*
7337d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
7347d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
7357d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
7367d94a505SMika Westerberg 	 * interrupt is enabled).
7377d94a505SMika Westerberg 	 */
738c3dce24cSAndy Shevchenko 	if (pm_runtime_suspended(drv_data->ssp->dev))
7397d94a505SMika Westerberg 		return IRQ_NONE;
7407d94a505SMika Westerberg 
741269e4a41SMika Westerberg 	/*
742269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
743269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
744269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
745269e4a41SMika Westerberg 	 * powered off.
746269e4a41SMika Westerberg 	 */
747c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
748269e4a41SMika Westerberg 	if (status == ~0)
749269e4a41SMika Westerberg 		return IRQ_NONE;
750269e4a41SMika Westerberg 
751c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
752ca632f55SGrant Likely 
753ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
754ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
755ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
756ca632f55SGrant Likely 
75702bc933eSTan, Jui Nee 	/* Ignore RX timeout interrupt if it is disabled */
75802bc933eSTan, Jui Nee 	if (!(sccr1_reg & SSCR1_TINTE))
75902bc933eSTan, Jui Nee 		mask &= ~SSSR_TINT;
76002bc933eSTan, Jui Nee 
761ca632f55SGrant Likely 	if (!(status & mask))
762ca632f55SGrant Likely 		return IRQ_NONE;
763ca632f55SGrant Likely 
764e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
765e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
766e51e9b93SJan Kiszka 
76751eea52dSLubomir Rintel 	if (!drv_data->controller->cur_msg) {
768b0312482SJan Kiszka 		handle_bad_msg(drv_data);
769ca632f55SGrant Likely 		/* Never fail */
770ca632f55SGrant Likely 		return IRQ_HANDLED;
771ca632f55SGrant Likely 	}
772ca632f55SGrant Likely 
773ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
774ca632f55SGrant Likely }
775ca632f55SGrant Likely 
776e5262d05SWeike Chen /*
7779df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
7789df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
7799df461ecSAndy Shevchenko  *
7809df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
7819df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
7829df461ecSAndy Shevchenko  *
7839df461ecSAndy Shevchenko  * Fsys = 200MHz
7849df461ecSAndy Shevchenko  *
7859df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
7869df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
7879df461ecSAndy Shevchenko  *
7889df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
7899df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
7909df461ecSAndy Shevchenko  *
7919df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
7929df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
7939df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
7949df461ecSAndy Shevchenko  *       k = [1, 256]
7959df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
7969df461ecSAndy Shevchenko  *
7979df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
7989df461ecSAndy Shevchenko  * are:
7999df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
8009df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
8019df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
8029df461ecSAndy Shevchenko  *
8039df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
8049df461ecSAndy Shevchenko  *
8059df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
8069df461ecSAndy Shevchenko  * to the asked baud rate.
807e5262d05SWeike Chen  */
8089df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
809e5262d05SWeike Chen {
8109df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
8119df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
8129df461ecSAndy Shevchenko 						   see (2) */
8139df461ecSAndy Shevchenko 						/* case 3 */
8149df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
8159df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
8169df461ecSAndy Shevchenko 	unsigned long scale;
8179df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
8189df461ecSAndy Shevchenko 	long r, r1, r2;
8199df461ecSAndy Shevchenko 	u32 mul;
820e5262d05SWeike Chen 
8219df461ecSAndy Shevchenko 	/* Case 1 */
8229df461ecSAndy Shevchenko 
8239df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
8249df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
8259df461ecSAndy Shevchenko 
8269df461ecSAndy Shevchenko 	/* Calculate initial quot */
8273ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
8289df461ecSAndy Shevchenko 
8299df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
8309df461ecSAndy Shevchenko 	if (q1 > 256) {
8319df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
8329df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
8339df461ecSAndy Shevchenko 		if (scale > 9) {
8349df461ecSAndy Shevchenko 			q1 >>= scale - 9;
8359df461ecSAndy Shevchenko 			mul >>= scale - 9;
8369df461ecSAndy Shevchenko 		}
8379df461ecSAndy Shevchenko 
8389df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
8399df461ecSAndy Shevchenko 		q1 += q1 & 1;
8409df461ecSAndy Shevchenko 	}
8419df461ecSAndy Shevchenko 
8429df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
8439df461ecSAndy Shevchenko 	scale = __ffs(q1);
8449df461ecSAndy Shevchenko 	q1 >>= scale;
8459df461ecSAndy Shevchenko 	mul >>= scale;
8469df461ecSAndy Shevchenko 
8479df461ecSAndy Shevchenko 	/* Get the remainder */
8489df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
8499df461ecSAndy Shevchenko 
8509df461ecSAndy Shevchenko 	/* Case 2 */
8519df461ecSAndy Shevchenko 
8523ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
8539df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
8549df461ecSAndy Shevchenko 
8559df461ecSAndy Shevchenko 	/*
8569df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
8579df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
8589df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
8599df461ecSAndy Shevchenko 	 */
8609df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
8619df461ecSAndy Shevchenko 		/* case 1 is better */
8629df461ecSAndy Shevchenko 		r = r1;
8639df461ecSAndy Shevchenko 		q = q1;
8649df461ecSAndy Shevchenko 	} else {
8659df461ecSAndy Shevchenko 		/* case 2 is better */
8669df461ecSAndy Shevchenko 		r = r2;
8679df461ecSAndy Shevchenko 		q = q2;
8689df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
8699df461ecSAndy Shevchenko 	}
8709df461ecSAndy Shevchenko 
8713ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
8729df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
8739df461ecSAndy Shevchenko 		u64 fssp;
8749df461ecSAndy Shevchenko 		u32 m;
8759df461ecSAndy Shevchenko 
8769df461ecSAndy Shevchenko 		/* Calculate initial quot */
8773ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
8789df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
8799df461ecSAndy Shevchenko 
8809df461ecSAndy Shevchenko 		/* Get the remainder */
8819df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
8829df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
8839df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
8849df461ecSAndy Shevchenko 
8859df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
8869df461ecSAndy Shevchenko 		if (r1 < r) {
8879df461ecSAndy Shevchenko 			/* case 3 is better */
8889df461ecSAndy Shevchenko 			q = 1;
8899df461ecSAndy Shevchenko 			mul = m;
890e5262d05SWeike Chen 		}
891e5262d05SWeike Chen 	}
892e5262d05SWeike Chen 
8939df461ecSAndy Shevchenko 	*dds = mul;
8949df461ecSAndy Shevchenko 	return q - 1;
895e5262d05SWeike Chen }
896e5262d05SWeike Chen 
8973343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
898ca632f55SGrant Likely {
89951eea52dSLubomir Rintel 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
9003343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
9013343b7a6SMika Westerberg 
9023343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
903ca632f55SGrant Likely 
90429f21337SFlavio Suligoi 	/*
90529f21337SFlavio Suligoi 	 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
9068083d6b8SAndy Shevchenko 	 * that the SSP transmission rate can be greater than the device rate.
90729f21337SFlavio Suligoi 	 */
908ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
90929f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
910ca632f55SGrant Likely 	else
91129f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
912ca632f55SGrant Likely }
913ca632f55SGrant Likely 
914e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
915d2c2f6a4SAndy Shevchenko 					   int rate)
916e5262d05SWeike Chen {
91796579a4eSJarkko Nikula 	struct chip_data *chip =
91851eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
919025ffe88SAndy Shevchenko 	unsigned int clk_div;
920e5262d05SWeike Chen 
921e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
922e5262d05SWeike Chen 	case QUARK_X1000_SSP:
9239df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
924eecacf73SDan Carpenter 		break;
925e5262d05SWeike Chen 	default:
926025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
927eecacf73SDan Carpenter 		break;
928e5262d05SWeike Chen 	}
929025ffe88SAndy Shevchenko 	return clk_div << 8;
930e5262d05SWeike Chen }
931e5262d05SWeike Chen 
93251eea52dSLubomir Rintel static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
933b6ced294SJarkko Nikula 			       struct spi_device *spi,
934b6ced294SJarkko Nikula 			       struct spi_transfer *xfer)
935b6ced294SJarkko Nikula {
936b6ced294SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
937b6ced294SJarkko Nikula 
938b6ced294SJarkko Nikula 	return chip->enable_dma &&
939b6ced294SJarkko Nikula 	       xfer->len <= MAX_DMA_LEN &&
940b6ced294SJarkko Nikula 	       xfer->len >= chip->dma_burst_size;
941b6ced294SJarkko Nikula }
942b6ced294SJarkko Nikula 
94351eea52dSLubomir Rintel static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
944d5898e19SJarkko Nikula 				   struct spi_device *spi,
945d5898e19SJarkko Nikula 				   struct spi_transfer *transfer)
946ca632f55SGrant Likely {
94751eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
94851eea52dSLubomir Rintel 	struct spi_message *message = controller->cur_msg;
94920f4c379SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
95096579a4eSJarkko Nikula 	u32 dma_thresh = chip->dma_threshold;
95196579a4eSJarkko Nikula 	u32 dma_burst = chip->dma_burst_size;
95296579a4eSJarkko Nikula 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
953bffc967eSJarkko Nikula 	u32 clk_div;
954bffc967eSJarkko Nikula 	u8 bits;
955bffc967eSJarkko Nikula 	u32 speed;
956ca632f55SGrant Likely 	u32 cr0;
957ca632f55SGrant Likely 	u32 cr1;
9587d1f1bf6SAndy Shevchenko 	int err;
959b6ced294SJarkko Nikula 	int dma_mapped;
960ca632f55SGrant Likely 
961cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
962b6ced294SJarkko Nikula 	if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
963ca632f55SGrant Likely 
9648083d6b8SAndy Shevchenko 		/* Reject already-mapped transfers; PIO won't always work */
965ca632f55SGrant Likely 		if (message->is_dma_mapped
966ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
967748fbadfSJarkko Nikula 			dev_err(&spi->dev,
9688ae55af3SJarkko Nikula 				"Mapped transfer length of %u is greater than %d\n",
969ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
970d5898e19SJarkko Nikula 			return -EINVAL;
971ca632f55SGrant Likely 		}
972ca632f55SGrant Likely 
9738083d6b8SAndy Shevchenko 		/* Warn ... we force this to PIO mode */
97420f4c379SJarkko Nikula 		dev_warn_ratelimited(&spi->dev,
975684a3ac7SAndy Shevchenko 				     "DMA disabled for transfer length %u greater than %d\n",
976684a3ac7SAndy Shevchenko 				     transfer->len, MAX_DMA_LEN);
977ca632f55SGrant Likely 	}
978ca632f55SGrant Likely 
979ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
980cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
981748fbadfSJarkko Nikula 		dev_err(&spi->dev, "Flush failed\n");
982d5898e19SJarkko Nikula 		return -EIO;
983ca632f55SGrant Likely 	}
984ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
985ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
986ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
987ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
988ca632f55SGrant Likely 
989ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
990ca632f55SGrant Likely 	bits = transfer->bits_per_word;
991ca632f55SGrant Likely 	speed = transfer->speed_hz;
992ca632f55SGrant Likely 
993d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
994ca632f55SGrant Likely 
995ca632f55SGrant Likely 	if (bits <= 8) {
996ca632f55SGrant Likely 		drv_data->n_bytes = 1;
99744ec41b7SAndy Shevchenko 		drv_data->read = drv_data->rx ? u8_reader : null_reader;
99844ec41b7SAndy Shevchenko 		drv_data->write = drv_data->tx ? u8_writer : null_writer;
999ca632f55SGrant Likely 	} else if (bits <= 16) {
1000ca632f55SGrant Likely 		drv_data->n_bytes = 2;
100144ec41b7SAndy Shevchenko 		drv_data->read = drv_data->rx ? u16_reader : null_reader;
100244ec41b7SAndy Shevchenko 		drv_data->write = drv_data->tx ? u16_writer : null_writer;
1003ca632f55SGrant Likely 	} else if (bits <= 32) {
1004ca632f55SGrant Likely 		drv_data->n_bytes = 4;
100544ec41b7SAndy Shevchenko 		drv_data->read = drv_data->rx ? u32_reader : null_reader;
100644ec41b7SAndy Shevchenko 		drv_data->write = drv_data->tx ? u32_writer : null_writer;
1007ca632f55SGrant Likely 	}
1008196b0e2cSJarkko Nikula 	/*
10098083d6b8SAndy Shevchenko 	 * If bits per word is changed in DMA mode, then must check
10108083d6b8SAndy Shevchenko 	 * the thresholds and burst also.
1011196b0e2cSJarkko Nikula 	 */
1012ca632f55SGrant Likely 	if (chip->enable_dma) {
1013cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
101420f4c379SJarkko Nikula 						spi,
1015ca632f55SGrant Likely 						bits, &dma_burst,
1016ca632f55SGrant Likely 						&dma_thresh))
101720f4c379SJarkko Nikula 			dev_warn_ratelimited(&spi->dev,
10188ae55af3SJarkko Nikula 					     "DMA burst size reduced to match bits_per_word\n");
1019ca632f55SGrant Likely 	}
1020ca632f55SGrant Likely 
102151eea52dSLubomir Rintel 	dma_mapped = controller->can_dma &&
102220f4c379SJarkko Nikula 		     controller->can_dma(controller, spi, transfer) &&
102351eea52dSLubomir Rintel 		     controller->cur_msg_mapped;
1024b6ced294SJarkko Nikula 	if (dma_mapped) {
1025ca632f55SGrant Likely 
1026ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
1027cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1028ca632f55SGrant Likely 
1029d5898e19SJarkko Nikula 		err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1030d5898e19SJarkko Nikula 		if (err)
1031d5898e19SJarkko Nikula 			return err;
1032ca632f55SGrant Likely 
1033ca632f55SGrant Likely 		/* Clear status and start DMA engine */
1034ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1035c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1036cd7bed00SMika Westerberg 
1037cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
1038ca632f55SGrant Likely 	} else {
1039ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
1040ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
1041ca632f55SGrant Likely 
1042ca632f55SGrant Likely 		/* Clear status  */
1043ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1044ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1045ca632f55SGrant Likely 	}
1046ca632f55SGrant Likely 
1047ee03672dSJarkko Nikula 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1048ee03672dSJarkko Nikula 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1049ee03672dSJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
105020f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
105151eea52dSLubomir Rintel 			controller->max_speed_hz
1052ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1053b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1054ee03672dSJarkko Nikula 	else
105520f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
105651eea52dSLubomir Rintel 			controller->max_speed_hz / 2
1057ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1058b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1059ee03672dSJarkko Nikula 
1060a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
10611bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold);
10621bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold);
1063a0d2642eSMika Westerberg 	}
1064a0d2642eSMika Westerberg 
10653fdb59cfSAndy Shevchenko 	if (is_mrfld_ssp(drv_data)) {
106670252440SAndy Shevchenko 		u32 mask = SFIFOTT_RFT | SFIFOTT_TFT;
10673fdb59cfSAndy Shevchenko 		u32 thresh = 0;
10683fdb59cfSAndy Shevchenko 
10693fdb59cfSAndy Shevchenko 		thresh |= SFIFOTT_RxThresh(chip->lpss_rx_threshold);
10703fdb59cfSAndy Shevchenko 		thresh |= SFIFOTT_TxThresh(chip->lpss_tx_threshold);
10713fdb59cfSAndy Shevchenko 
107270252440SAndy Shevchenko 		pxa2xx_spi_update(drv_data, SFIFOTT, mask, thresh);
10733fdb59cfSAndy Shevchenko 	}
10743fdb59cfSAndy Shevchenko 
10751bed378cSAndy Shevchenko 	if (is_quark_x1000_ssp(drv_data))
10761bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate);
1077e5262d05SWeike Chen 
10780c8ccd8bSAndy Shevchenko 	/* Stop the SSP */
10790c8ccd8bSAndy Shevchenko 	if (!is_mmp2_ssp(drv_data))
10800c8ccd8bSAndy Shevchenko 		pxa_ssp_disable(drv_data->ssp);
10810c8ccd8bSAndy Shevchenko 
10820c8ccd8bSAndy Shevchenko 	if (!pxa25x_ssp_comp(drv_data))
10830c8ccd8bSAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
10840c8ccd8bSAndy Shevchenko 
10858083d6b8SAndy Shevchenko 	/* First set CR1 without interrupt and service enables */
10861bed378cSAndy Shevchenko 	pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1);
10871bed378cSAndy Shevchenko 
10888083d6b8SAndy Shevchenko 	/* See if we need to reload the configuration registers */
10891bed378cSAndy Shevchenko 	pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0);
1090ca632f55SGrant Likely 
10910c8ccd8bSAndy Shevchenko 	/* Restart the SSP */
10920c8ccd8bSAndy Shevchenko 	pxa_ssp_enable(drv_data->ssp);
10930c8ccd8bSAndy Shevchenko 
109441c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data)) {
10956d380132SAndy Shevchenko 		u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8;
109682391856SLubomir Rintel 
109782391856SLubomir Rintel 		if (tx_level) {
10988083d6b8SAndy Shevchenko 			/* On MMP2, flipping SSE doesn't to empty Tx FIFO. */
1099684a3ac7SAndy Shevchenko 			dev_warn(&spi->dev, "%u bytes of garbage in Tx FIFO!\n", tx_level);
110082391856SLubomir Rintel 			if (tx_level > transfer->len)
110182391856SLubomir Rintel 				tx_level = transfer->len;
110282391856SLubomir Rintel 			drv_data->tx += tx_level;
110382391856SLubomir Rintel 		}
110482391856SLubomir Rintel 	}
110582391856SLubomir Rintel 
110651eea52dSLubomir Rintel 	if (spi_controller_is_slave(controller)) {
1107ec93cb6fSLubomir Rintel 		while (drv_data->write(drv_data))
1108ec93cb6fSLubomir Rintel 			;
110977d33897SLubomir Rintel 		if (drv_data->gpiod_ready) {
111077d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 1);
111177d33897SLubomir Rintel 			udelay(1);
111277d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 0);
111377d33897SLubomir Rintel 		}
1114ec93cb6fSLubomir Rintel 	}
1115ec93cb6fSLubomir Rintel 
1116d5898e19SJarkko Nikula 	/*
1117d5898e19SJarkko Nikula 	 * Release the data by enabling service requests and interrupts,
11188083d6b8SAndy Shevchenko 	 * without changing any mode bits.
1119d5898e19SJarkko Nikula 	 */
1120c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1121d5898e19SJarkko Nikula 
1122d5898e19SJarkko Nikula 	return 1;
1123ca632f55SGrant Likely }
1124ca632f55SGrant Likely 
112551eea52dSLubomir Rintel static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
1126ec93cb6fSLubomir Rintel {
112751eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1128ec93cb6fSLubomir Rintel 
11294761d2e7SAndy Shevchenko 	int_error_stop(drv_data, "transfer aborted", -EINTR);
1130ec93cb6fSLubomir Rintel 
1131ec93cb6fSLubomir Rintel 	return 0;
1132ec93cb6fSLubomir Rintel }
1133ec93cb6fSLubomir Rintel 
113451eea52dSLubomir Rintel static void pxa2xx_spi_handle_err(struct spi_controller *controller,
11357f86bde9SMika Westerberg 				 struct spi_message *msg)
1136ca632f55SGrant Likely {
113751eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1138ca632f55SGrant Likely 
11393bbdc083SAndy Shevchenko 	int_stop_and_reset(drv_data);
11403bbdc083SAndy Shevchenko 
1141d5898e19SJarkko Nikula 	/* Disable the SSP */
114229d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
1143ca632f55SGrant Likely 
1144d5898e19SJarkko Nikula 	/*
1145d5898e19SJarkko Nikula 	 * Stop the DMA if running. Note DMA callback handler may have unset
1146d5898e19SJarkko Nikula 	 * the dma_running already, which is fine as stopping is not needed
1147d5898e19SJarkko Nikula 	 * then but we shouldn't rely this flag for anything else than
1148d5898e19SJarkko Nikula 	 * stopping. For instance to differentiate between PIO and DMA
1149d5898e19SJarkko Nikula 	 * transfers.
1150d5898e19SJarkko Nikula 	 */
1151d5898e19SJarkko Nikula 	if (atomic_read(&drv_data->dma_running))
1152d5898e19SJarkko Nikula 		pxa2xx_spi_dma_stop(drv_data);
1153ca632f55SGrant Likely }
1154ca632f55SGrant Likely 
115551eea52dSLubomir Rintel static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
11567d94a505SMika Westerberg {
115751eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
11587d94a505SMika Westerberg 
11597d94a505SMika Westerberg 	/* Disable the SSP now */
116029d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
11617d94a505SMika Westerberg 
11627d94a505SMika Westerberg 	return 0;
11637d94a505SMika Westerberg }
11647d94a505SMika Westerberg 
1165ca632f55SGrant Likely static int setup(struct spi_device *spi)
1166ca632f55SGrant Likely {
1167bffc967eSJarkko Nikula 	struct pxa2xx_spi_chip *chip_info;
1168ca632f55SGrant Likely 	struct chip_data *chip;
1169dccf7369SJarkko Nikula 	const struct lpss_config *config;
11703cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
11713cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1172a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1173a0d2642eSMika Westerberg 
1174e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1175e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1176e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1177e5262d05SWeike Chen 		tx_hi_thres = 0;
1178e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1179e5262d05SWeike Chen 		break;
11803fdb59cfSAndy Shevchenko 	case MRFLD_SSP:
11813fdb59cfSAndy Shevchenko 		tx_thres = TX_THRESH_MRFLD_DFLT;
11823fdb59cfSAndy Shevchenko 		tx_hi_thres = 0;
11833fdb59cfSAndy Shevchenko 		rx_thres = RX_THRESH_MRFLD_DFLT;
11843fdb59cfSAndy Shevchenko 		break;
11857c7289a4SAndy Shevchenko 	case CE4100_SSP:
11867c7289a4SAndy Shevchenko 		tx_thres = TX_THRESH_CE4100_DFLT;
11877c7289a4SAndy Shevchenko 		tx_hi_thres = 0;
11887c7289a4SAndy Shevchenko 		rx_thres = RX_THRESH_CE4100_DFLT;
11897c7289a4SAndy Shevchenko 		break;
119003fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
119103fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
119230f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
119334cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1194b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1195fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
1196dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1197dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1198dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1199dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1200e5262d05SWeike Chen 		break;
1201e5262d05SWeike Chen 	default:
1202a0d2642eSMika Westerberg 		tx_hi_thres = 0;
120351eea52dSLubomir Rintel 		if (spi_controller_is_slave(drv_data->controller)) {
1204ec93cb6fSLubomir Rintel 			tx_thres = 1;
1205ec93cb6fSLubomir Rintel 			rx_thres = 2;
1206ec93cb6fSLubomir Rintel 		} else {
1207ec93cb6fSLubomir Rintel 			tx_thres = TX_THRESH_DFLT;
1208a0d2642eSMika Westerberg 			rx_thres = RX_THRESH_DFLT;
1209ec93cb6fSLubomir Rintel 		}
1210e5262d05SWeike Chen 		break;
1211a0d2642eSMika Westerberg 	}
1212ca632f55SGrant Likely 
12138083d6b8SAndy Shevchenko 	/* Only allocate on the first setup */
1214ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1215ca632f55SGrant Likely 	if (!chip) {
1216ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
12179deae459SJingoo Han 		if (!chip)
1218ca632f55SGrant Likely 			return -ENOMEM;
1219ca632f55SGrant Likely 
1220ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1221ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1222f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1223f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1224ca632f55SGrant Likely 				kfree(chip);
1225ca632f55SGrant Likely 				return -EINVAL;
1226ca632f55SGrant Likely 			}
1227c18d925fSJan Kiszka 		}
122851eea52dSLubomir Rintel 		chip->enable_dma = drv_data->controller_info->enable_dma;
1229ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1230ca632f55SGrant Likely 	}
1231ca632f55SGrant Likely 
12328083d6b8SAndy Shevchenko 	/*
12338083d6b8SAndy Shevchenko 	 * Protocol drivers may change the chip settings, so...
12348083d6b8SAndy Shevchenko 	 * if chip_info exists, use it.
12358083d6b8SAndy Shevchenko 	 */
1236ca632f55SGrant Likely 	chip_info = spi->controller_data;
1237ca632f55SGrant Likely 
1238ca632f55SGrant Likely 	/* chip_info isn't always needed */
1239ca632f55SGrant Likely 	if (chip_info) {
1240ca632f55SGrant Likely 		if (chip_info->timeout)
1241ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1242ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1243ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1244a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1245a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1246ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1247ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1248ca632f55SGrant Likely 		chip->dma_threshold = 0;
1249ca632f55SGrant Likely 	}
12508393961cSAndy Shevchenko 
12518393961cSAndy Shevchenko 	chip->cr1 = 0;
125251eea52dSLubomir Rintel 	if (spi_controller_is_slave(drv_data->controller)) {
1253ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCFR;
1254ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCLKDIR;
1255ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SFRMDIR;
1256ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SPH;
1257ec93cb6fSLubomir Rintel 	}
1258ca632f55SGrant Likely 
12593fdb59cfSAndy Shevchenko 	if (is_lpss_ssp(drv_data)) {
1260a0d2642eSMika Westerberg 		chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
12613fdb59cfSAndy Shevchenko 		chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
12623fdb59cfSAndy Shevchenko 					  SSITF_TxHiThresh(tx_hi_thres);
12633fdb59cfSAndy Shevchenko 	}
12643fdb59cfSAndy Shevchenko 
12653fdb59cfSAndy Shevchenko 	if (is_mrfld_ssp(drv_data)) {
12663fdb59cfSAndy Shevchenko 		chip->lpss_rx_threshold = rx_thres;
12673fdb59cfSAndy Shevchenko 		chip->lpss_tx_threshold = tx_thres;
12683fdb59cfSAndy Shevchenko 	}
1269a0d2642eSMika Westerberg 
12708083d6b8SAndy Shevchenko 	/*
12718083d6b8SAndy Shevchenko 	 * Set DMA burst and threshold outside of chip_info path so that if
12728083d6b8SAndy Shevchenko 	 * chip_info goes away after setting chip->enable_dma, the burst and
12738083d6b8SAndy Shevchenko 	 * threshold can still respond to changes in bits_per_word.
12748083d6b8SAndy Shevchenko 	 */
1275ca632f55SGrant Likely 	if (chip->enable_dma) {
12768083d6b8SAndy Shevchenko 		/* Set up legal burst and threshold for DMA */
1277cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1278cd7bed00SMika Westerberg 						spi->bits_per_word,
1279ca632f55SGrant Likely 						&chip->dma_burst_size,
1280ca632f55SGrant Likely 						&chip->dma_threshold)) {
1281f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1282f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1283ca632f55SGrant Likely 		}
1284000c6af4SAndy Shevchenko 		dev_dbg(&spi->dev,
1285000c6af4SAndy Shevchenko 			"in setup: DMA burst size set to %u\n",
1286000c6af4SAndy Shevchenko 			chip->dma_burst_size);
1287ca632f55SGrant Likely 	}
1288ca632f55SGrant Likely 
1289e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1290e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1291e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1292e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1293e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1294e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1295e5262d05SWeike Chen 		break;
12967c7289a4SAndy Shevchenko 	case CE4100_SSP:
12977c7289a4SAndy Shevchenko 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
12987c7289a4SAndy Shevchenko 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
12997c7289a4SAndy Shevchenko 		break;
1300e5262d05SWeike Chen 	default:
1301e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1302e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1303e5262d05SWeike Chen 		break;
1304e5262d05SWeike Chen 	}
1305e5262d05SWeike Chen 
1306ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1307eb743ec6SAndy Shevchenko 	chip->cr1 |= ((spi->mode & SPI_CPHA) ? SSCR1_SPH : 0) |
1308eb743ec6SAndy Shevchenko 		     ((spi->mode & SPI_CPOL) ? SSCR1_SPO : 0);
1309ca632f55SGrant Likely 
1310b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1311b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1312b833172fSMika Westerberg 
1313ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1314ca632f55SGrant Likely 
1315ca632f55SGrant Likely 	return 0;
1316ca632f55SGrant Likely }
1317ca632f55SGrant Likely 
1318ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1319ca632f55SGrant Likely {
1320ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
1321ca632f55SGrant Likely 
1322ca632f55SGrant Likely 	kfree(chip);
1323ca632f55SGrant Likely }
1324ca632f55SGrant Likely 
13259b2d6119SLee Jones #ifdef CONFIG_ACPI
13268422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
132703fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
132803fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
132903fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
133003fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
133103fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
133230f3a6abSMika Westerberg 	{ "8086228E", LPSS_BSW_SSP },
133303fbf488SJarkko Nikula 	{ },
133403fbf488SJarkko Nikula };
133503fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
13369b2d6119SLee Jones #endif
133703fbf488SJarkko Nikula 
133834cadd9cSJarkko Nikula /*
133934cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
134034cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
134134cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
134234cadd9cSJarkko Nikula  */
134334cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
134434cadd9cSJarkko Nikula 	/* SPT-LP */
134534cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
134634cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
134734cadd9cSJarkko Nikula 	/* SPT-H */
134834cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
134934cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1350704d2b07SMika Westerberg 	/* KBL-H */
1351704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1352704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
13536157d4c2SJarkko Nikula 	/* CML-V */
13546157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
13556157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
1356c1b03f11SJarkko Nikula 	/* BXT A-Step */
1357b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1358b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1359b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1360c1b03f11SJarkko Nikula 	/* BXT B-Step */
1361c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1362c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1363c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1364e18a80acSDavid E. Box 	/* GLK */
1365e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1366e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1367e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
136822d71a50SMika Westerberg 	/* ICL-LP */
136922d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
137022d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
137122d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
13728cc77204SJarkko Nikula 	/* EHL */
13738cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
13748cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
13758cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
13769c7315c9SJarkko Nikula 	/* JSL */
13779c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
13789c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
13799c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
1380cf961fceSJarkko Nikula 	/* TGL-H */
1381cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
1382cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
1383cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
1384cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
1385a402e397SJarkko Nikula 	/* ADL-P */
1386a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51aa), LPSS_CNL_SSP },
1387a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51ab), LPSS_CNL_SSP },
1388a402e397SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x51fb), LPSS_CNL_SSP },
13898c4ffe4dSJarkko Nikula 	/* ADL-M */
13908c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54aa), LPSS_CNL_SSP },
13918c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54ab), LPSS_CNL_SSP },
13928c4ffe4dSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x54fb), LPSS_CNL_SSP },
1393b7c08cf8SJarkko Nikula 	/* APL */
1394b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1395b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1396b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
139754d0fd06SJarkko Nikula 	/* RPL-S */
139854d0fd06SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7a2a), LPSS_CNL_SSP },
139954d0fd06SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7a2b), LPSS_CNL_SSP },
140054d0fd06SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7a79), LPSS_CNL_SSP },
140154d0fd06SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7a7b), LPSS_CNL_SSP },
1402b8450e01SJarkko Nikula 	/* ADL-S */
1403b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7aaa), LPSS_CNL_SSP },
1404b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7aab), LPSS_CNL_SSP },
1405b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7af9), LPSS_CNL_SSP },
1406b8450e01SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7afb), LPSS_CNL_SSP },
14073190d4beSJarkko Nikula 	/* MTL-P */
14083190d4beSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7e27), LPSS_CNL_SSP },
14093190d4beSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7e30), LPSS_CNL_SSP },
14103190d4beSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x7e46), LPSS_CNL_SSP },
1411fc0b2accSJarkko Nikula 	/* CNL-LP */
1412fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1413fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1414fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1415fc0b2accSJarkko Nikula 	/* CNL-H */
1416fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1417fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1418fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
141941a91802SEvan Green 	/* CML-LP */
142041a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
142141a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
142241a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
1423f0cf17edSJarkko Nikula 	/* CML-H */
1424f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1425f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1426f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
1427a4127952SJarkko Nikula 	/* TGL-LP */
1428a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1429a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1430a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1431a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1432a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1433a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1434a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
143594e5c23dSAxel Lin 	{ },
143634cadd9cSJarkko Nikula };
143734cadd9cSJarkko Nikula 
143887ae1d2dSLubomir Rintel static const struct of_device_id pxa2xx_spi_of_match[] = {
143987ae1d2dSLubomir Rintel 	{ .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
144087ae1d2dSLubomir Rintel 	{},
144187ae1d2dSLubomir Rintel };
144287ae1d2dSLubomir Rintel MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
144387ae1d2dSLubomir Rintel 
144487ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
144587ae1d2dSLubomir Rintel 
144634cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
144734cadd9cSJarkko Nikula {
14485ba846b1SAndy Shevchenko 	return param == chan->device->dev;
144934cadd9cSJarkko Nikula }
145034cadd9cSJarkko Nikula 
145187ae1d2dSLubomir Rintel #endif /* CONFIG_PCI */
145287ae1d2dSLubomir Rintel 
145351eea52dSLubomir Rintel static struct pxa2xx_spi_controller *
14540db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1455a3496855SMika Westerberg {
145651eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *pdata;
14572990f3a8SAndy Shevchenko 	struct device *dev = &pdev->dev;
14582990f3a8SAndy Shevchenko 	struct device *parent = dev->parent;
1459a3496855SMika Westerberg 	struct ssp_device *ssp;
1460a3496855SMika Westerberg 	struct resource *res;
14616fb7427dSAndy Shevchenko 	struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
146234cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
146388a94721SAndy Shevchenko 	enum pxa_ssp_type type = SSP_UNDEFINED;
1464f2faa3ecSAndy Shevchenko 	const void *match;
1465*1a1864cdSAndy Shevchenko 	bool is_lpss_priv;
14662990f3a8SAndy Shevchenko 	int status;
14672990f3a8SAndy Shevchenko 	u64 uid;
1468a3496855SMika Westerberg 
1469*1a1864cdSAndy Shevchenko 	is_lpss_priv = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lpss_priv");
1470*1a1864cdSAndy Shevchenko 
14716fb7427dSAndy Shevchenko 	if (pcidev)
14726fb7427dSAndy Shevchenko 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
1473a3496855SMika Westerberg 
14748fc8250aSAndy Shevchenko 	match = device_get_match_data(dev);
1475f2faa3ecSAndy Shevchenko 	if (match)
1476f2faa3ecSAndy Shevchenko 		type = (enum pxa_ssp_type)match;
147734cadd9cSJarkko Nikula 	else if (pcidev_id)
147855ef8262SLubomir Rintel 		type = (enum pxa_ssp_type)pcidev_id->driver_data;
1479*1a1864cdSAndy Shevchenko 	else if (is_lpss_priv) {
1480*1a1864cdSAndy Shevchenko 		u32 value;
1481*1a1864cdSAndy Shevchenko 
1482*1a1864cdSAndy Shevchenko 		status = device_property_read_u32(dev, "intel,spi-pxa2xx-type", &value);
1483*1a1864cdSAndy Shevchenko 		if (status)
1484*1a1864cdSAndy Shevchenko 			return ERR_PTR(status);
1485*1a1864cdSAndy Shevchenko 
1486*1a1864cdSAndy Shevchenko 		type = (enum pxa_ssp_type)value;
1487*1a1864cdSAndy Shevchenko 	}
148888a94721SAndy Shevchenko 
148988a94721SAndy Shevchenko 	/* Validate the SSP type correctness */
149088a94721SAndy Shevchenko 	if (!(type > SSP_UNDEFINED && type < SSP_MAX))
149114af1df3SAndy Shevchenko 		return ERR_PTR(-EINVAL);
149203fbf488SJarkko Nikula 
14938fc8250aSAndy Shevchenko 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
14949deae459SJingoo Han 	if (!pdata)
149514af1df3SAndy Shevchenko 		return ERR_PTR(-ENOMEM);
1496a3496855SMika Westerberg 
1497a3496855SMika Westerberg 	ssp = &pdata->ssp;
1498a3496855SMika Westerberg 
1499e3b7fca3SAndy Shevchenko 	ssp->mmio_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1500cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
150114af1df3SAndy Shevchenko 		return ERR_CAST(ssp->mmio_base);
1502a3496855SMika Westerberg 
150377c544d2SAndy Shevchenko 	ssp->phys_base = res->start;
150477c544d2SAndy Shevchenko 
150587ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
150634cadd9cSJarkko Nikula 	if (pcidev_id) {
15076fb7427dSAndy Shevchenko 		pdata->tx_param = parent;
15086fb7427dSAndy Shevchenko 		pdata->rx_param = parent;
150934cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
151034cadd9cSJarkko Nikula 	}
151187ae1d2dSLubomir Rintel #endif
151234cadd9cSJarkko Nikula 
15138fc8250aSAndy Shevchenko 	ssp->clk = devm_clk_get(dev, NULL);
15145eb263efSChuhong Yuan 	if (IS_ERR(ssp->clk))
151514af1df3SAndy Shevchenko 		return ERR_CAST(ssp->clk);
1516a3496855SMika Westerberg 
1517a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
15185eb263efSChuhong Yuan 	if (ssp->irq < 0)
151914af1df3SAndy Shevchenko 		return ERR_PTR(ssp->irq);
15205eb263efSChuhong Yuan 
1521a3496855SMika Westerberg 	ssp->type = type;
15228fc8250aSAndy Shevchenko 	ssp->dev = dev;
15232990f3a8SAndy Shevchenko 
15242990f3a8SAndy Shevchenko 	status = acpi_dev_uid_to_integer(ACPI_COMPANION(dev), &uid);
15252990f3a8SAndy Shevchenko 	if (status)
15262990f3a8SAndy Shevchenko 		ssp->port_id = -1;
15272990f3a8SAndy Shevchenko 	else
15282990f3a8SAndy Shevchenko 		ssp->port_id = uid;
1529a3496855SMika Westerberg 
15308fc8250aSAndy Shevchenko 	pdata->is_slave = device_property_read_bool(dev, "spi-slave");
1531a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1532cddb339bSMika Westerberg 	pdata->enable_dma = true;
153337821a82SAndy Shevchenko 	pdata->dma_burst_size = 1;
1534a3496855SMika Westerberg 
1535a3496855SMika Westerberg 	return pdata;
1536a3496855SMika Westerberg }
1537a3496855SMika Westerberg 
153851eea52dSLubomir Rintel static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
15393cc7b0e3SJarkko Nikula 				      unsigned int cs)
15400c27d9cfSMika Westerberg {
154151eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
15420c27d9cfSMika Westerberg 
1543c3dce24cSAndy Shevchenko 	if (has_acpi_companion(drv_data->ssp->dev)) {
15440c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
15450c27d9cfSMika Westerberg 		/*
15460c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
15470c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
15480c27d9cfSMika Westerberg 		 * to match what Linux expects.
15490c27d9cfSMika Westerberg 		 */
15500c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
155130f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
15520c27d9cfSMika Westerberg 			return cs - 1;
15530c27d9cfSMika Westerberg 
15540c27d9cfSMika Westerberg 		default:
15550c27d9cfSMika Westerberg 			break;
15560c27d9cfSMika Westerberg 		}
15570c27d9cfSMika Westerberg 	}
15580c27d9cfSMika Westerberg 
15590c27d9cfSMika Westerberg 	return cs;
15600c27d9cfSMika Westerberg }
15610c27d9cfSMika Westerberg 
1562b2662a16SDaniel Vetter static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1563b2662a16SDaniel Vetter {
1564b2662a16SDaniel Vetter 	return MAX_DMA_LEN;
1565b2662a16SDaniel Vetter }
1566b2662a16SDaniel Vetter 
1567fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1568ca632f55SGrant Likely {
1569ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
157051eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *platform_info;
157151eea52dSLubomir Rintel 	struct spi_controller *controller;
1572ca632f55SGrant Likely 	struct driver_data *drv_data;
1573ca632f55SGrant Likely 	struct ssp_device *ssp;
15748b136baaSJarkko Nikula 	const struct lpss_config *config;
1575778c12e6SAndy Shevchenko 	int status;
1576c039dd27SJarkko Nikula 	u32 tmp;
1577ca632f55SGrant Likely 
1578851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1579851bacf5SMika Westerberg 	if (!platform_info) {
15800db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
158114af1df3SAndy Shevchenko 		if (IS_ERR(platform_info)) {
1582851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
158314af1df3SAndy Shevchenko 			return PTR_ERR(platform_info);
1584851bacf5SMika Westerberg 		}
1585a3496855SMika Westerberg 	}
1586ca632f55SGrant Likely 
1587ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1588851bacf5SMika Westerberg 	if (!ssp)
1589851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1590851bacf5SMika Westerberg 
1591851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
15928083d6b8SAndy Shevchenko 		dev_err(&pdev->dev, "failed to get SSP\n");
1593ca632f55SGrant Likely 		return -ENODEV;
1594ca632f55SGrant Likely 	}
1595ca632f55SGrant Likely 
1596ec93cb6fSLubomir Rintel 	if (platform_info->is_slave)
15975626308bSLukas Wunner 		controller = devm_spi_alloc_slave(dev, sizeof(*drv_data));
1598ec93cb6fSLubomir Rintel 	else
15995626308bSLukas Wunner 		controller = devm_spi_alloc_master(dev, sizeof(*drv_data));
1600ec93cb6fSLubomir Rintel 
160151eea52dSLubomir Rintel 	if (!controller) {
160251eea52dSLubomir Rintel 		dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1603f2eed8caSAndy Shevchenko 		status = -ENOMEM;
1604f2eed8caSAndy Shevchenko 		goto out_error_controller_alloc;
1605ca632f55SGrant Likely 	}
160651eea52dSLubomir Rintel 	drv_data = spi_controller_get_devdata(controller);
160751eea52dSLubomir Rintel 	drv_data->controller = controller;
160851eea52dSLubomir Rintel 	drv_data->controller_info = platform_info;
1609ca632f55SGrant Likely 	drv_data->ssp = ssp;
1610ca632f55SGrant Likely 
161112baee68SAndy Shevchenko 	device_set_node(&controller->dev, dev_fwnode(dev));
161294acf807SAndy Shevchenko 
16138083d6b8SAndy Shevchenko 	/* The spi->mode bits understood by this driver: */
161451eea52dSLubomir Rintel 	controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1615ca632f55SGrant Likely 
161651eea52dSLubomir Rintel 	controller->bus_num = ssp->port_id;
161751eea52dSLubomir Rintel 	controller->dma_alignment = DMA_ALIGNMENT;
161851eea52dSLubomir Rintel 	controller->cleanup = cleanup;
161951eea52dSLubomir Rintel 	controller->setup = setup;
162051eea52dSLubomir Rintel 	controller->set_cs = pxa2xx_spi_set_cs;
162151eea52dSLubomir Rintel 	controller->transfer_one = pxa2xx_spi_transfer_one;
162251eea52dSLubomir Rintel 	controller->slave_abort = pxa2xx_spi_slave_abort;
162351eea52dSLubomir Rintel 	controller->handle_err = pxa2xx_spi_handle_err;
162451eea52dSLubomir Rintel 	controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
162551eea52dSLubomir Rintel 	controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
162651eea52dSLubomir Rintel 	controller->auto_runtime_pm = true;
162751eea52dSLubomir Rintel 	controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1628ca632f55SGrant Likely 
1629ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1630ca632f55SGrant Likely 
1631ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1632e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1633e5262d05SWeike Chen 		case QUARK_X1000_SSP:
163451eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1635e5262d05SWeike Chen 			break;
1636e5262d05SWeike Chen 		default:
163751eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1638e5262d05SWeike Chen 			break;
1639e5262d05SWeike Chen 		}
1640e5262d05SWeike Chen 
1641ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1642ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1643ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1644ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1645ca632f55SGrant Likely 	} else {
164651eea52dSLubomir Rintel 		controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1647ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
16485928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1649ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1650ec93cb6fSLubomir Rintel 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1651ec93cb6fSLubomir Rintel 						| SSSR_ROR | SSSR_TUR;
1652ca632f55SGrant Likely 	}
1653ca632f55SGrant Likely 
1654ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1655ca632f55SGrant Likely 			drv_data);
1656ca632f55SGrant Likely 	if (status < 0) {
1657ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
165851eea52dSLubomir Rintel 		goto out_error_controller_alloc;
1659ca632f55SGrant Likely 	}
1660ca632f55SGrant Likely 
1661ca632f55SGrant Likely 	/* Setup DMA if requested */
1662ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1663cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1664cd7bed00SMika Westerberg 		if (status) {
16658b57b11bSFlavio Suligoi 			dev_warn(dev, "no DMA channels available, using PIO\n");
1666cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1667b6ced294SJarkko Nikula 		} else {
166851eea52dSLubomir Rintel 			controller->can_dma = pxa2xx_spi_can_dma;
1669bf9f742cSMark Brown 			controller->max_dma_len = MAX_DMA_LEN;
1670b2662a16SDaniel Vetter 			controller->max_transfer_size =
1671b2662a16SDaniel Vetter 				pxa2xx_spi_max_dma_transfer_size;
1672ca632f55SGrant Likely 		}
1673ca632f55SGrant Likely 	}
1674ca632f55SGrant Likely 
1675ca632f55SGrant Likely 	/* Enable SOC clock */
167662bbc864STobias Jordan 	status = clk_prepare_enable(ssp->clk);
167762bbc864STobias Jordan 	if (status)
167862bbc864STobias Jordan 		goto out_error_dma_irq_alloc;
16793343b7a6SMika Westerberg 
168051eea52dSLubomir Rintel 	controller->max_speed_hz = clk_get_rate(ssp->clk);
168123cdddb2SJarkko Nikula 	/*
168223cdddb2SJarkko Nikula 	 * Set minimum speed for all other platforms than Intel Quark which is
168323cdddb2SJarkko Nikula 	 * able do under 1 Hz transfers.
168423cdddb2SJarkko Nikula 	 */
168523cdddb2SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
168623cdddb2SJarkko Nikula 		controller->min_speed_hz =
168723cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 4096);
168823cdddb2SJarkko Nikula 	else if (!is_quark_x1000_ssp(drv_data))
168923cdddb2SJarkko Nikula 		controller->min_speed_hz =
169023cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 512);
1691ca632f55SGrant Likely 
16920c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
16930c8ccd8bSAndy Shevchenko 
1694ca632f55SGrant Likely 	/* Load default SSP configuration */
1695e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1696e5262d05SWeike Chen 	case QUARK_X1000_SSP:
16977c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
16987c7289a4SAndy Shevchenko 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1699c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1700e5262d05SWeike Chen 
17018083d6b8SAndy Shevchenko 		/* Using the Motorola SPI protocol and use 8 bit frame */
17027c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
17037c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1704e5262d05SWeike Chen 		break;
17057c7289a4SAndy Shevchenko 	case CE4100_SSP:
17067c7289a4SAndy Shevchenko 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
17077c7289a4SAndy Shevchenko 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
17087c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
17097c7289a4SAndy Shevchenko 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
17107c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1711a2dd8af0SAndy Shevchenko 		break;
1712e5262d05SWeike Chen 	default:
1713ec93cb6fSLubomir Rintel 
171451eea52dSLubomir Rintel 		if (spi_controller_is_slave(controller)) {
1715ec93cb6fSLubomir Rintel 			tmp = SSCR1_SCFR |
1716ec93cb6fSLubomir Rintel 			      SSCR1_SCLKDIR |
1717ec93cb6fSLubomir Rintel 			      SSCR1_SFRMDIR |
1718ec93cb6fSLubomir Rintel 			      SSCR1_RxTresh(2) |
1719ec93cb6fSLubomir Rintel 			      SSCR1_TxTresh(1) |
1720ec93cb6fSLubomir Rintel 			      SSCR1_SPH;
1721ec93cb6fSLubomir Rintel 		} else {
1722c039dd27SJarkko Nikula 			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1723c039dd27SJarkko Nikula 			      SSCR1_TxTresh(TX_THRESH_DFLT);
1724ec93cb6fSLubomir Rintel 		}
1725c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1726ec93cb6fSLubomir Rintel 		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
172751eea52dSLubomir Rintel 		if (!spi_controller_is_slave(controller))
1728ec93cb6fSLubomir Rintel 			tmp |= SSCR0_SCR(2);
1729c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1730e5262d05SWeike Chen 		break;
1731e5262d05SWeike Chen 	}
1732e5262d05SWeike Chen 
1733ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1734c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1735e5262d05SWeike Chen 
1736e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1737c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1738ca632f55SGrant Likely 
17398b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
17408b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
17418b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
17428b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
17438b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
17448b136baaSJarkko Nikula 						   config->reg_capabilities);
17458b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
17468b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
17478b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
174830f3a6abSMika Westerberg 		} else if (config->cs_num) {
174930f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
17508b136baaSJarkko Nikula 		}
17518b136baaSJarkko Nikula 	}
175251eea52dSLubomir Rintel 	controller->num_chipselect = platform_info->num_chipselect;
1753778c12e6SAndy Shevchenko 	controller->use_gpio_descriptors = true;
17546ac5a435SAndy Shevchenko 
175577d33897SLubomir Rintel 	if (platform_info->is_slave) {
175677d33897SLubomir Rintel 		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
175777d33897SLubomir Rintel 						"ready", GPIOD_OUT_LOW);
175877d33897SLubomir Rintel 		if (IS_ERR(drv_data->gpiod_ready)) {
175977d33897SLubomir Rintel 			status = PTR_ERR(drv_data->gpiod_ready);
176077d33897SLubomir Rintel 			goto out_error_clock_enabled;
176177d33897SLubomir Rintel 		}
176277d33897SLubomir Rintel 	}
176377d33897SLubomir Rintel 
1764836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1765836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1766836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1767836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1768836d1a22SAntonio Ospite 
1769ca632f55SGrant Likely 	/* Register with the SPI framework */
1770ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
177132e5b572SLukas Wunner 	status = spi_register_controller(controller);
1772eb743ec6SAndy Shevchenko 	if (status) {
17738083d6b8SAndy Shevchenko 		dev_err(&pdev->dev, "problem registering SPI controller\n");
177412742045SLubomir Rintel 		goto out_error_pm_runtime_enabled;
1775ca632f55SGrant Likely 	}
1776ca632f55SGrant Likely 
1777ca632f55SGrant Likely 	return status;
1778ca632f55SGrant Likely 
177912742045SLubomir Rintel out_error_pm_runtime_enabled:
1780e2b714afSJarkko Nikula 	pm_runtime_disable(&pdev->dev);
178112742045SLubomir Rintel 
178212742045SLubomir Rintel out_error_clock_enabled:
17833343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
178462bbc864STobias Jordan 
178562bbc864STobias Jordan out_error_dma_irq_alloc:
1786cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1787ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1788ca632f55SGrant Likely 
178951eea52dSLubomir Rintel out_error_controller_alloc:
1790ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1791ca632f55SGrant Likely 	return status;
1792ca632f55SGrant Likely }
1793ca632f55SGrant Likely 
1794ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1795ca632f55SGrant Likely {
1796ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
17973d24b2a4SAndy Shevchenko 	struct ssp_device *ssp = drv_data->ssp;
1798ca632f55SGrant Likely 
17997d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
18007d94a505SMika Westerberg 
180132e5b572SLukas Wunner 	spi_unregister_controller(drv_data->controller);
180232e5b572SLukas Wunner 
1803ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
18040c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
18053343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1806ca632f55SGrant Likely 
1807ca632f55SGrant Likely 	/* Release DMA */
180851eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma)
1809cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1810ca632f55SGrant Likely 
18117d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
18127d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
18137d94a505SMika Westerberg 
1814ca632f55SGrant Likely 	/* Release IRQ */
1815ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1816ca632f55SGrant Likely 
1817ca632f55SGrant Likely 	/* Release SSP */
1818ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1819ca632f55SGrant Likely 
1820ca632f55SGrant Likely 	return 0;
1821ca632f55SGrant Likely }
1822ca632f55SGrant Likely 
1823ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1824ca632f55SGrant Likely {
1825ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1826ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1827bffc967eSJarkko Nikula 	int status;
1828ca632f55SGrant Likely 
182951eea52dSLubomir Rintel 	status = spi_controller_suspend(drv_data->controller);
1830eb743ec6SAndy Shevchenko 	if (status)
1831ca632f55SGrant Likely 		return status;
18320c8ccd8bSAndy Shevchenko 
18330c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
18342b9375b9SDmitry Eremin-Solenikov 
18352b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
18363343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1837ca632f55SGrant Likely 
1838ca632f55SGrant Likely 	return 0;
1839ca632f55SGrant Likely }
1840ca632f55SGrant Likely 
1841ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1842ca632f55SGrant Likely {
1843ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1844ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1845bffc967eSJarkko Nikula 	int status;
1846ca632f55SGrant Likely 
1847ca632f55SGrant Likely 	/* Enable the SSP clock */
184862bbc864STobias Jordan 	if (!pm_runtime_suspended(dev)) {
184962bbc864STobias Jordan 		status = clk_prepare_enable(ssp->clk);
185062bbc864STobias Jordan 		if (status)
185162bbc864STobias Jordan 			return status;
185262bbc864STobias Jordan 	}
1853ca632f55SGrant Likely 
1854ca632f55SGrant Likely 	/* Start the queue running */
185551eea52dSLubomir Rintel 	return spi_controller_resume(drv_data->controller);
1856ca632f55SGrant Likely }
18577d94a505SMika Westerberg 
18587d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
18597d94a505SMika Westerberg {
18607d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
18617d94a505SMika Westerberg 
18627d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
18637d94a505SMika Westerberg 	return 0;
18647d94a505SMika Westerberg }
18657d94a505SMika Westerberg 
18667d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
18677d94a505SMika Westerberg {
18687d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
18697d94a505SMika Westerberg 
1870d294e99cSye xingchen 	return clk_prepare_enable(drv_data->ssp->clk);
18717d94a505SMika Westerberg }
1872ca632f55SGrant Likely 
1873ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
18746c3c438cSAndy Shevchenko 	SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
18756c3c438cSAndy Shevchenko 	RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend, pxa2xx_spi_runtime_resume, NULL)
1876ca632f55SGrant Likely };
1877ca632f55SGrant Likely 
1878ca632f55SGrant Likely static struct platform_driver driver = {
1879ca632f55SGrant Likely 	.driver = {
1880ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
18816c3c438cSAndy Shevchenko 		.pm	= pm_ptr(&pxa2xx_spi_pm_ops),
1882a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
188387ae1d2dSLubomir Rintel 		.of_match_table = of_match_ptr(pxa2xx_spi_of_match),
1884ca632f55SGrant Likely 	},
1885ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
1886ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
1887ca632f55SGrant Likely };
1888ca632f55SGrant Likely 
1889ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
1890ca632f55SGrant Likely {
1891ca632f55SGrant Likely 	return platform_driver_register(&driver);
1892ca632f55SGrant Likely }
1893ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
1894ca632f55SGrant Likely 
1895ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
1896ca632f55SGrant Likely {
1897ca632f55SGrant Likely 	platform_driver_unregister(&driver);
1898ca632f55SGrant Likely }
1899ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
190051ebf6acSFlavio Suligoi 
190151ebf6acSFlavio Suligoi MODULE_SOFTDEP("pre: dw_dmac");
1902