xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision 29d7e05c5f75fc2ebf3608743b6809aa0ff56800)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
4a0d2642eSMika Westerberg  * Copyright (C) 2013, Intel Corporation
5ca632f55SGrant Likely  */
6ca632f55SGrant Likely 
75ce25705SAndy Shevchenko #include <linux/acpi.h>
88b136baaSJarkko Nikula #include <linux/bitops.h>
95ce25705SAndy Shevchenko #include <linux/clk.h>
105ce25705SAndy Shevchenko #include <linux/delay.h>
11ca632f55SGrant Likely #include <linux/device.h>
12cbfd6a21SSachin Kamat #include <linux/err.h>
135ce25705SAndy Shevchenko #include <linux/errno.h>
145ce25705SAndy Shevchenko #include <linux/gpio/consumer.h>
155ce25705SAndy Shevchenko #include <linux/gpio.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>
28ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h>
29ca632f55SGrant Likely #include <linux/spi/spi.h>
30ca632f55SGrant Likely 
31cd7bed00SMika Westerberg #include "spi-pxa2xx.h"
32ca632f55SGrant Likely 
33ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street");
34ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
35ca632f55SGrant Likely MODULE_LICENSE("GPL");
36ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi");
37ca632f55SGrant Likely 
38ca632f55SGrant Likely #define TIMOUT_DFLT		1000
39ca632f55SGrant Likely 
40ca632f55SGrant Likely /*
41ca632f55SGrant Likely  * for testing SSCR1 changes that require SSP restart, basically
42ca632f55SGrant Likely  * everything except the service and interrupt enables, the pxa270 developer
43ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
44ca632f55SGrant Likely  * list, but the PXA255 dev man says all bits without really meaning the
45ca632f55SGrant Likely  * service and interrupt enables
46ca632f55SGrant Likely  */
47ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
48ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
49ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
50ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
51ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
52ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
53ca632f55SGrant Likely 
54e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
55e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
56e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
57e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
58e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
59e5262d05SWeike Chen 
607c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
617c7289a4SAndy Shevchenko 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
627c7289a4SAndy Shevchenko 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
637c7289a4SAndy Shevchenko 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
647c7289a4SAndy Shevchenko 				| CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
657c7289a4SAndy Shevchenko 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
667c7289a4SAndy Shevchenko 
67624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
68624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE			BIT(0)
69624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
708b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT			9
718b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
72a0d2642eSMika Westerberg 
73dccf7369SJarkko Nikula struct lpss_config {
74dccf7369SJarkko Nikula 	/* LPSS offset from drv_data->ioaddr */
75dccf7369SJarkko Nikula 	unsigned offset;
76dccf7369SJarkko Nikula 	/* Register offsets from drv_data->lpss_base or -1 */
77dccf7369SJarkko Nikula 	int reg_general;
78dccf7369SJarkko Nikula 	int reg_ssp;
79dccf7369SJarkko Nikula 	int reg_cs_ctrl;
808b136baaSJarkko Nikula 	int reg_capabilities;
81dccf7369SJarkko Nikula 	/* FIFO thresholds */
82dccf7369SJarkko Nikula 	u32 rx_threshold;
83dccf7369SJarkko Nikula 	u32 tx_threshold_lo;
84dccf7369SJarkko Nikula 	u32 tx_threshold_hi;
85c1e4a53cSMika Westerberg 	/* Chip select control */
86c1e4a53cSMika Westerberg 	unsigned cs_sel_shift;
87c1e4a53cSMika Westerberg 	unsigned cs_sel_mask;
8830f3a6abSMika Westerberg 	unsigned cs_num;
89dccf7369SJarkko Nikula };
90dccf7369SJarkko Nikula 
91dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */
92dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = {
93dccf7369SJarkko Nikula 	{	/* LPSS_LPT_SSP */
94dccf7369SJarkko Nikula 		.offset = 0x800,
95dccf7369SJarkko Nikula 		.reg_general = 0x08,
96dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
97dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
988b136baaSJarkko Nikula 		.reg_capabilities = -1,
99dccf7369SJarkko Nikula 		.rx_threshold = 64,
100dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
101dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
102dccf7369SJarkko Nikula 	},
103dccf7369SJarkko Nikula 	{	/* LPSS_BYT_SSP */
104dccf7369SJarkko Nikula 		.offset = 0x400,
105dccf7369SJarkko Nikula 		.reg_general = 0x08,
106dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
107dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1088b136baaSJarkko Nikula 		.reg_capabilities = -1,
109dccf7369SJarkko Nikula 		.rx_threshold = 64,
110dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
111dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
112dccf7369SJarkko Nikula 	},
11330f3a6abSMika Westerberg 	{	/* LPSS_BSW_SSP */
11430f3a6abSMika Westerberg 		.offset = 0x400,
11530f3a6abSMika Westerberg 		.reg_general = 0x08,
11630f3a6abSMika Westerberg 		.reg_ssp = 0x0c,
11730f3a6abSMika Westerberg 		.reg_cs_ctrl = 0x18,
11830f3a6abSMika Westerberg 		.reg_capabilities = -1,
11930f3a6abSMika Westerberg 		.rx_threshold = 64,
12030f3a6abSMika Westerberg 		.tx_threshold_lo = 160,
12130f3a6abSMika Westerberg 		.tx_threshold_hi = 224,
12230f3a6abSMika Westerberg 		.cs_sel_shift = 2,
12330f3a6abSMika Westerberg 		.cs_sel_mask = 1 << 2,
12430f3a6abSMika Westerberg 		.cs_num = 2,
12530f3a6abSMika Westerberg 	},
12634cadd9cSJarkko Nikula 	{	/* LPSS_SPT_SSP */
12734cadd9cSJarkko Nikula 		.offset = 0x200,
12834cadd9cSJarkko Nikula 		.reg_general = -1,
12934cadd9cSJarkko Nikula 		.reg_ssp = 0x20,
13034cadd9cSJarkko Nikula 		.reg_cs_ctrl = 0x24,
13166ec246eSJarkko Nikula 		.reg_capabilities = -1,
13234cadd9cSJarkko Nikula 		.rx_threshold = 1,
13334cadd9cSJarkko Nikula 		.tx_threshold_lo = 32,
13434cadd9cSJarkko Nikula 		.tx_threshold_hi = 56,
13534cadd9cSJarkko Nikula 	},
136b7c08cf8SJarkko Nikula 	{	/* LPSS_BXT_SSP */
137b7c08cf8SJarkko Nikula 		.offset = 0x200,
138b7c08cf8SJarkko Nikula 		.reg_general = -1,
139b7c08cf8SJarkko Nikula 		.reg_ssp = 0x20,
140b7c08cf8SJarkko Nikula 		.reg_cs_ctrl = 0x24,
141b7c08cf8SJarkko Nikula 		.reg_capabilities = 0xfc,
142b7c08cf8SJarkko Nikula 		.rx_threshold = 1,
143b7c08cf8SJarkko Nikula 		.tx_threshold_lo = 16,
144b7c08cf8SJarkko Nikula 		.tx_threshold_hi = 48,
145c1e4a53cSMika Westerberg 		.cs_sel_shift = 8,
146c1e4a53cSMika Westerberg 		.cs_sel_mask = 3 << 8,
147b7c08cf8SJarkko Nikula 	},
148fc0b2accSJarkko Nikula 	{	/* LPSS_CNL_SSP */
149fc0b2accSJarkko Nikula 		.offset = 0x200,
150fc0b2accSJarkko Nikula 		.reg_general = -1,
151fc0b2accSJarkko Nikula 		.reg_ssp = 0x20,
152fc0b2accSJarkko Nikula 		.reg_cs_ctrl = 0x24,
153fc0b2accSJarkko Nikula 		.reg_capabilities = 0xfc,
154fc0b2accSJarkko Nikula 		.rx_threshold = 1,
155fc0b2accSJarkko Nikula 		.tx_threshold_lo = 32,
156fc0b2accSJarkko Nikula 		.tx_threshold_hi = 56,
157fc0b2accSJarkko Nikula 		.cs_sel_shift = 8,
158fc0b2accSJarkko Nikula 		.cs_sel_mask = 3 << 8,
159fc0b2accSJarkko Nikula 	},
160dccf7369SJarkko Nikula };
161dccf7369SJarkko Nikula 
162dccf7369SJarkko Nikula static inline const struct lpss_config
163dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data)
164dccf7369SJarkko Nikula {
165dccf7369SJarkko Nikula 	return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
166dccf7369SJarkko Nikula }
167dccf7369SJarkko Nikula 
168a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data)
169a0d2642eSMika Westerberg {
17003fbf488SJarkko Nikula 	switch (drv_data->ssp_type) {
17103fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
17203fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
17330f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
17434cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
175b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
176fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
17703fbf488SJarkko Nikula 		return true;
17803fbf488SJarkko Nikula 	default:
17903fbf488SJarkko Nikula 		return false;
18003fbf488SJarkko Nikula 	}
181a0d2642eSMika Westerberg }
182a0d2642eSMika Westerberg 
183e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
184e5262d05SWeike Chen {
185e5262d05SWeike Chen 	return drv_data->ssp_type == QUARK_X1000_SSP;
186e5262d05SWeike Chen }
187e5262d05SWeike Chen 
1884fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
1894fdb2424SWeike Chen {
1904fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
191e5262d05SWeike Chen 	case QUARK_X1000_SSP:
192e5262d05SWeike Chen 		return QUARK_X1000_SSCR1_CHANGE_MASK;
1937c7289a4SAndy Shevchenko 	case CE4100_SSP:
1947c7289a4SAndy Shevchenko 		return CE4100_SSCR1_CHANGE_MASK;
1954fdb2424SWeike Chen 	default:
1964fdb2424SWeike Chen 		return SSCR1_CHANGE_MASK;
1974fdb2424SWeike Chen 	}
1984fdb2424SWeike Chen }
1994fdb2424SWeike Chen 
2004fdb2424SWeike Chen static u32
2014fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
2024fdb2424SWeike Chen {
2034fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
204e5262d05SWeike Chen 	case QUARK_X1000_SSP:
205e5262d05SWeike Chen 		return RX_THRESH_QUARK_X1000_DFLT;
2067c7289a4SAndy Shevchenko 	case CE4100_SSP:
2077c7289a4SAndy Shevchenko 		return RX_THRESH_CE4100_DFLT;
2084fdb2424SWeike Chen 	default:
2094fdb2424SWeike Chen 		return RX_THRESH_DFLT;
2104fdb2424SWeike Chen 	}
2114fdb2424SWeike Chen }
2124fdb2424SWeike Chen 
2134fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
2144fdb2424SWeike Chen {
2154fdb2424SWeike Chen 	u32 mask;
2164fdb2424SWeike Chen 
2174fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
218e5262d05SWeike Chen 	case QUARK_X1000_SSP:
219e5262d05SWeike Chen 		mask = QUARK_X1000_SSSR_TFL_MASK;
220e5262d05SWeike Chen 		break;
2217c7289a4SAndy Shevchenko 	case CE4100_SSP:
2227c7289a4SAndy Shevchenko 		mask = CE4100_SSSR_TFL_MASK;
2237c7289a4SAndy Shevchenko 		break;
2244fdb2424SWeike Chen 	default:
2254fdb2424SWeike Chen 		mask = SSSR_TFL_MASK;
2264fdb2424SWeike Chen 		break;
2274fdb2424SWeike Chen 	}
2284fdb2424SWeike Chen 
229c039dd27SJarkko Nikula 	return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
2304fdb2424SWeike Chen }
2314fdb2424SWeike Chen 
2324fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
2334fdb2424SWeike Chen 				     u32 *sccr1_reg)
2344fdb2424SWeike Chen {
2354fdb2424SWeike Chen 	u32 mask;
2364fdb2424SWeike Chen 
2374fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
238e5262d05SWeike Chen 	case QUARK_X1000_SSP:
239e5262d05SWeike Chen 		mask = QUARK_X1000_SSCR1_RFT;
240e5262d05SWeike Chen 		break;
2417c7289a4SAndy Shevchenko 	case CE4100_SSP:
2427c7289a4SAndy Shevchenko 		mask = CE4100_SSCR1_RFT;
2437c7289a4SAndy Shevchenko 		break;
2444fdb2424SWeike Chen 	default:
2454fdb2424SWeike Chen 		mask = SSCR1_RFT;
2464fdb2424SWeike Chen 		break;
2474fdb2424SWeike Chen 	}
2484fdb2424SWeike Chen 	*sccr1_reg &= ~mask;
2494fdb2424SWeike Chen }
2504fdb2424SWeike Chen 
2514fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
2524fdb2424SWeike Chen 				   u32 *sccr1_reg, u32 threshold)
2534fdb2424SWeike Chen {
2544fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
255e5262d05SWeike Chen 	case QUARK_X1000_SSP:
256e5262d05SWeike Chen 		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
257e5262d05SWeike Chen 		break;
2587c7289a4SAndy Shevchenko 	case CE4100_SSP:
2597c7289a4SAndy Shevchenko 		*sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
2607c7289a4SAndy Shevchenko 		break;
2614fdb2424SWeike Chen 	default:
2624fdb2424SWeike Chen 		*sccr1_reg |= SSCR1_RxTresh(threshold);
2634fdb2424SWeike Chen 		break;
2644fdb2424SWeike Chen 	}
2654fdb2424SWeike Chen }
2664fdb2424SWeike Chen 
2674fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
2684fdb2424SWeike Chen 				  u32 clk_div, u8 bits)
2694fdb2424SWeike Chen {
2704fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
271e5262d05SWeike Chen 	case QUARK_X1000_SSP:
272e5262d05SWeike Chen 		return clk_div
273e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_Motorola
274e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
275e5262d05SWeike Chen 			| SSCR0_SSE;
2764fdb2424SWeike Chen 	default:
2774fdb2424SWeike Chen 		return clk_div
2784fdb2424SWeike Chen 			| SSCR0_Motorola
2794fdb2424SWeike Chen 			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
2804fdb2424SWeike Chen 			| SSCR0_SSE
2814fdb2424SWeike Chen 			| (bits > 16 ? SSCR0_EDSS : 0);
2824fdb2424SWeike Chen 	}
2834fdb2424SWeike Chen }
2844fdb2424SWeike Chen 
285a0d2642eSMika Westerberg /*
286a0d2642eSMika Westerberg  * Read and write LPSS SSP private registers. Caller must first check that
287a0d2642eSMika Westerberg  * is_lpss_ssp() returns true before these can be called.
288a0d2642eSMika Westerberg  */
289a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
290a0d2642eSMika Westerberg {
291a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
292a0d2642eSMika Westerberg 	return readl(drv_data->lpss_base + offset);
293a0d2642eSMika Westerberg }
294a0d2642eSMika Westerberg 
295a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data,
296a0d2642eSMika Westerberg 				  unsigned offset, u32 value)
297a0d2642eSMika Westerberg {
298a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
299a0d2642eSMika Westerberg 	writel(value, drv_data->lpss_base + offset);
300a0d2642eSMika Westerberg }
301a0d2642eSMika Westerberg 
302a0d2642eSMika Westerberg /*
303a0d2642eSMika Westerberg  * lpss_ssp_setup - perform LPSS SSP specific setup
304a0d2642eSMika Westerberg  * @drv_data: pointer to the driver private data
305a0d2642eSMika Westerberg  *
306a0d2642eSMika Westerberg  * Perform LPSS SSP specific setup. This function must be called first if
307a0d2642eSMika Westerberg  * one is going to use LPSS SSP private registers.
308a0d2642eSMika Westerberg  */
309a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data)
310a0d2642eSMika Westerberg {
311dccf7369SJarkko Nikula 	const struct lpss_config *config;
312dccf7369SJarkko Nikula 	u32 value;
313a0d2642eSMika Westerberg 
314dccf7369SJarkko Nikula 	config = lpss_get_config(drv_data);
315dccf7369SJarkko Nikula 	drv_data->lpss_base = drv_data->ioaddr + config->offset;
316a0d2642eSMika Westerberg 
317a0d2642eSMika Westerberg 	/* Enable software chip select control */
3180e897218SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
319624ea72eSJarkko Nikula 	value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
320624ea72eSJarkko Nikula 	value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
321dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
3220054e28dSMika Westerberg 
3230054e28dSMika Westerberg 	/* Enable multiblock DMA transfers */
32451eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma) {
325dccf7369SJarkko Nikula 		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
3261de70612SMika Westerberg 
32782ba2c2aSJarkko Nikula 		if (config->reg_general >= 0) {
32882ba2c2aSJarkko Nikula 			value = __lpss_ssp_read_priv(drv_data,
32982ba2c2aSJarkko Nikula 						     config->reg_general);
330624ea72eSJarkko Nikula 			value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
33182ba2c2aSJarkko Nikula 			__lpss_ssp_write_priv(drv_data,
33282ba2c2aSJarkko Nikula 					      config->reg_general, value);
33382ba2c2aSJarkko Nikula 		}
3341de70612SMika Westerberg 	}
335a0d2642eSMika Westerberg }
336a0d2642eSMika Westerberg 
337d5898e19SJarkko Nikula static void lpss_ssp_select_cs(struct spi_device *spi,
338c1e4a53cSMika Westerberg 			       const struct lpss_config *config)
339a0d2642eSMika Westerberg {
340d5898e19SJarkko Nikula 	struct driver_data *drv_data =
341d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
342d0283eb2SJarkko Nikula 	u32 value, cs;
343a0d2642eSMika Westerberg 
344c1e4a53cSMika Westerberg 	if (!config->cs_sel_mask)
345c1e4a53cSMika Westerberg 		return;
346dccf7369SJarkko Nikula 
347dccf7369SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
348c1e4a53cSMika Westerberg 
349d5898e19SJarkko Nikula 	cs = spi->chip_select;
350c1e4a53cSMika Westerberg 	cs <<= config->cs_sel_shift;
351c1e4a53cSMika Westerberg 	if (cs != (value & config->cs_sel_mask)) {
352d0283eb2SJarkko Nikula 		/*
353c1e4a53cSMika Westerberg 		 * When switching another chip select output active the
354c1e4a53cSMika Westerberg 		 * output must be selected first and wait 2 ssp_clk cycles
355c1e4a53cSMika Westerberg 		 * before changing state to active. Otherwise a short
356c1e4a53cSMika Westerberg 		 * glitch will occur on the previous chip select since
357c1e4a53cSMika Westerberg 		 * output select is latched but state control is not.
358d0283eb2SJarkko Nikula 		 */
359c1e4a53cSMika Westerberg 		value &= ~config->cs_sel_mask;
360d0283eb2SJarkko Nikula 		value |= cs;
361d0283eb2SJarkko Nikula 		__lpss_ssp_write_priv(drv_data,
362d0283eb2SJarkko Nikula 				      config->reg_cs_ctrl, value);
363d0283eb2SJarkko Nikula 		ndelay(1000000000 /
36451eea52dSLubomir Rintel 		       (drv_data->controller->max_speed_hz / 2));
365d0283eb2SJarkko Nikula 	}
366d0283eb2SJarkko Nikula }
367c1e4a53cSMika Westerberg 
368d5898e19SJarkko Nikula static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
369c1e4a53cSMika Westerberg {
370d5898e19SJarkko Nikula 	struct driver_data *drv_data =
371d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
372c1e4a53cSMika Westerberg 	const struct lpss_config *config;
373c1e4a53cSMika Westerberg 	u32 value;
374c1e4a53cSMika Westerberg 
375c1e4a53cSMika Westerberg 	config = lpss_get_config(drv_data);
376c1e4a53cSMika Westerberg 
377c1e4a53cSMika Westerberg 	if (enable)
378d5898e19SJarkko Nikula 		lpss_ssp_select_cs(spi, config);
379c1e4a53cSMika Westerberg 
380c1e4a53cSMika Westerberg 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
381c1e4a53cSMika Westerberg 	if (enable)
382c1e4a53cSMika Westerberg 		value &= ~LPSS_CS_CONTROL_CS_HIGH;
383c1e4a53cSMika Westerberg 	else
384c1e4a53cSMika Westerberg 		value |= LPSS_CS_CONTROL_CS_HIGH;
385dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
386a0d2642eSMika Westerberg }
387a0d2642eSMika Westerberg 
388d5898e19SJarkko Nikula static void cs_assert(struct spi_device *spi)
389ca632f55SGrant Likely {
390d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
391d5898e19SJarkko Nikula 	struct driver_data *drv_data =
392d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
393ca632f55SGrant Likely 
394ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP) {
39596579a4eSJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, chip->frm);
396ca632f55SGrant Likely 		return;
397ca632f55SGrant Likely 	}
398ca632f55SGrant Likely 
399ca632f55SGrant Likely 	if (chip->cs_control) {
400ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_ASSERT);
401ca632f55SGrant Likely 		return;
402ca632f55SGrant Likely 	}
403ca632f55SGrant Likely 
404c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
405c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
406a0d2642eSMika Westerberg 		return;
407a0d2642eSMika Westerberg 	}
408a0d2642eSMika Westerberg 
4097566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
410d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, true);
411ca632f55SGrant Likely }
412ca632f55SGrant Likely 
413d5898e19SJarkko Nikula static void cs_deassert(struct spi_device *spi)
414ca632f55SGrant Likely {
415d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
416d5898e19SJarkko Nikula 	struct driver_data *drv_data =
417d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
418104e51afSJarkko Nikula 	unsigned long timeout;
419ca632f55SGrant Likely 
420ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
421ca632f55SGrant Likely 		return;
422ca632f55SGrant Likely 
423104e51afSJarkko Nikula 	/* Wait until SSP becomes idle before deasserting the CS */
424104e51afSJarkko Nikula 	timeout = jiffies + msecs_to_jiffies(10);
425104e51afSJarkko Nikula 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
426104e51afSJarkko Nikula 	       !time_after(jiffies, timeout))
427104e51afSJarkko Nikula 		cpu_relax();
428104e51afSJarkko Nikula 
429ca632f55SGrant Likely 	if (chip->cs_control) {
430ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_DEASSERT);
431ca632f55SGrant Likely 		return;
432ca632f55SGrant Likely 	}
433ca632f55SGrant Likely 
434c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
435c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
436a0d2642eSMika Westerberg 		return;
437a0d2642eSMika Westerberg 	}
438a0d2642eSMika Westerberg 
4397566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
440d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, false);
441d5898e19SJarkko Nikula }
442d5898e19SJarkko Nikula 
443d5898e19SJarkko Nikula static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
444d5898e19SJarkko Nikula {
445d5898e19SJarkko Nikula 	if (level)
446d5898e19SJarkko Nikula 		cs_deassert(spi);
447d5898e19SJarkko Nikula 	else
448d5898e19SJarkko Nikula 		cs_assert(spi);
449ca632f55SGrant Likely }
450ca632f55SGrant Likely 
451cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
452ca632f55SGrant Likely {
453ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
454ca632f55SGrant Likely 
455ca632f55SGrant Likely 	do {
456c039dd27SJarkko Nikula 		while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
457c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
458c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
459ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
460ca632f55SGrant Likely 
461ca632f55SGrant Likely 	return limit;
462ca632f55SGrant Likely }
463ca632f55SGrant Likely 
464*29d7e05cSLubomir Rintel static void pxa2xx_spi_off(struct driver_data *drv_data)
465*29d7e05cSLubomir Rintel {
466*29d7e05cSLubomir Rintel 	/* On MMP, disabling SSE seems to corrupt the rx fifo */
467*29d7e05cSLubomir Rintel 	if (drv_data->ssp_type == MMP2_SSP)
468*29d7e05cSLubomir Rintel 		return;
469*29d7e05cSLubomir Rintel 
470*29d7e05cSLubomir Rintel 	pxa2xx_spi_write(drv_data, SSCR0,
471*29d7e05cSLubomir Rintel 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
472*29d7e05cSLubomir Rintel }
473*29d7e05cSLubomir Rintel 
474ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
475ca632f55SGrant Likely {
476ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
477ca632f55SGrant Likely 
4784fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
479ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
480ca632f55SGrant Likely 		return 0;
481ca632f55SGrant Likely 
482c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
483ca632f55SGrant Likely 	drv_data->tx += n_bytes;
484ca632f55SGrant Likely 
485ca632f55SGrant Likely 	return 1;
486ca632f55SGrant Likely }
487ca632f55SGrant Likely 
488ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
489ca632f55SGrant Likely {
490ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
491ca632f55SGrant Likely 
492c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
493ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
494c039dd27SJarkko Nikula 		pxa2xx_spi_read(drv_data, SSDR);
495ca632f55SGrant Likely 		drv_data->rx += n_bytes;
496ca632f55SGrant Likely 	}
497ca632f55SGrant Likely 
498ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
499ca632f55SGrant Likely }
500ca632f55SGrant Likely 
501ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data)
502ca632f55SGrant Likely {
5034fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
504ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
505ca632f55SGrant Likely 		return 0;
506ca632f55SGrant Likely 
507c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
508ca632f55SGrant Likely 	++drv_data->tx;
509ca632f55SGrant Likely 
510ca632f55SGrant Likely 	return 1;
511ca632f55SGrant Likely }
512ca632f55SGrant Likely 
513ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data)
514ca632f55SGrant Likely {
515c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
516ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
517c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
518ca632f55SGrant Likely 		++drv_data->rx;
519ca632f55SGrant Likely 	}
520ca632f55SGrant Likely 
521ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
522ca632f55SGrant Likely }
523ca632f55SGrant Likely 
524ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
525ca632f55SGrant Likely {
5264fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
527ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
528ca632f55SGrant Likely 		return 0;
529ca632f55SGrant Likely 
530c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
531ca632f55SGrant Likely 	drv_data->tx += 2;
532ca632f55SGrant Likely 
533ca632f55SGrant Likely 	return 1;
534ca632f55SGrant Likely }
535ca632f55SGrant Likely 
536ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
537ca632f55SGrant Likely {
538c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
539ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
540c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
541ca632f55SGrant Likely 		drv_data->rx += 2;
542ca632f55SGrant Likely 	}
543ca632f55SGrant Likely 
544ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
545ca632f55SGrant Likely }
546ca632f55SGrant Likely 
547ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
548ca632f55SGrant Likely {
5494fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
550ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
551ca632f55SGrant Likely 		return 0;
552ca632f55SGrant Likely 
553c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
554ca632f55SGrant Likely 	drv_data->tx += 4;
555ca632f55SGrant Likely 
556ca632f55SGrant Likely 	return 1;
557ca632f55SGrant Likely }
558ca632f55SGrant Likely 
559ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
560ca632f55SGrant Likely {
561c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
562ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
563c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
564ca632f55SGrant Likely 		drv_data->rx += 4;
565ca632f55SGrant Likely 	}
566ca632f55SGrant Likely 
567ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
568ca632f55SGrant Likely }
569ca632f55SGrant Likely 
570ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
571ca632f55SGrant Likely {
57296579a4eSJarkko Nikula 	struct chip_data *chip =
57351eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
574ca632f55SGrant Likely 	u32 sccr1_reg;
575ca632f55SGrant Likely 
576c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
577152bc19eSAndy Shevchenko 	switch (drv_data->ssp_type) {
578152bc19eSAndy Shevchenko 	case QUARK_X1000_SSP:
579152bc19eSAndy Shevchenko 		sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
580152bc19eSAndy Shevchenko 		break;
5817c7289a4SAndy Shevchenko 	case CE4100_SSP:
5827c7289a4SAndy Shevchenko 		sccr1_reg &= ~CE4100_SSCR1_RFT;
5837c7289a4SAndy Shevchenko 		break;
584152bc19eSAndy Shevchenko 	default:
585ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_RFT;
586152bc19eSAndy Shevchenko 		break;
587152bc19eSAndy Shevchenko 	}
588ca632f55SGrant Likely 	sccr1_reg |= chip->threshold;
589c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
590ca632f55SGrant Likely }
591ca632f55SGrant Likely 
592ca632f55SGrant Likely static void int_error_stop(struct driver_data *drv_data, const char* msg)
593ca632f55SGrant Likely {
594ca632f55SGrant Likely 	/* Stop and reset SSP */
595ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
596ca632f55SGrant Likely 	reset_sccr1(drv_data);
597ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
598c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
599cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
600*29d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
601ca632f55SGrant Likely 
602ca632f55SGrant Likely 	dev_err(&drv_data->pdev->dev, "%s\n", msg);
603ca632f55SGrant Likely 
60451eea52dSLubomir Rintel 	drv_data->controller->cur_msg->status = -EIO;
60551eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
606ca632f55SGrant Likely }
607ca632f55SGrant Likely 
608ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
609ca632f55SGrant Likely {
61007550df0SJarkko Nikula 	/* Clear and disable interrupts */
611ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
612ca632f55SGrant Likely 	reset_sccr1(drv_data);
613ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
614c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
615ca632f55SGrant Likely 
61651eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
617ca632f55SGrant Likely }
618ca632f55SGrant Likely 
619ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
620ca632f55SGrant Likely {
621c039dd27SJarkko Nikula 	u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
622ca632f55SGrant Likely 		       drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
623ca632f55SGrant Likely 
624c039dd27SJarkko Nikula 	u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
625ca632f55SGrant Likely 
626ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
627ca632f55SGrant Likely 		int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
628ca632f55SGrant Likely 		return IRQ_HANDLED;
629ca632f55SGrant Likely 	}
630ca632f55SGrant Likely 
631ec93cb6fSLubomir Rintel 	if (irq_status & SSSR_TUR) {
632ec93cb6fSLubomir Rintel 		int_error_stop(drv_data, "interrupt_transfer: fifo underrun");
633ec93cb6fSLubomir Rintel 		return IRQ_HANDLED;
634ec93cb6fSLubomir Rintel 	}
635ec93cb6fSLubomir Rintel 
636ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
637c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
638ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
639ca632f55SGrant Likely 			int_transfer_complete(drv_data);
640ca632f55SGrant Likely 			return IRQ_HANDLED;
641ca632f55SGrant Likely 		}
642ca632f55SGrant Likely 	}
643ca632f55SGrant Likely 
644ca632f55SGrant Likely 	/* Drain rx fifo, Fill tx fifo and prevent overruns */
645ca632f55SGrant Likely 	do {
646ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
647ca632f55SGrant Likely 			int_transfer_complete(drv_data);
648ca632f55SGrant Likely 			return IRQ_HANDLED;
649ca632f55SGrant Likely 		}
650ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
651ca632f55SGrant Likely 
652ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
653ca632f55SGrant Likely 		int_transfer_complete(drv_data);
654ca632f55SGrant Likely 		return IRQ_HANDLED;
655ca632f55SGrant Likely 	}
656ca632f55SGrant Likely 
657ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
658ca632f55SGrant Likely 		u32 bytes_left;
659ca632f55SGrant Likely 		u32 sccr1_reg;
660ca632f55SGrant Likely 
661c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
662ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
663ca632f55SGrant Likely 
664ca632f55SGrant Likely 		/*
665ca632f55SGrant Likely 		 * PXA25x_SSP has no timeout, set up rx threshould for the
666ca632f55SGrant Likely 		 * remaining RX bytes.
667ca632f55SGrant Likely 		 */
668ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
6694fdb2424SWeike Chen 			u32 rx_thre;
670ca632f55SGrant Likely 
6714fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
672ca632f55SGrant Likely 
673ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
674ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
675ca632f55SGrant Likely 			case 4:
6762c183376SGustavo A. R. Silva 				bytes_left >>= 2;
6772c183376SGustavo A. R. Silva 				break;
678ca632f55SGrant Likely 			case 2:
679ca632f55SGrant Likely 				bytes_left >>= 1;
6802c183376SGustavo A. R. Silva 				break;
681ca632f55SGrant Likely 			}
682ca632f55SGrant Likely 
6834fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
6844fdb2424SWeike Chen 			if (rx_thre > bytes_left)
6854fdb2424SWeike Chen 				rx_thre = bytes_left;
686ca632f55SGrant Likely 
6874fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
688ca632f55SGrant Likely 		}
689c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
690ca632f55SGrant Likely 	}
691ca632f55SGrant Likely 
692ca632f55SGrant Likely 	/* We did something */
693ca632f55SGrant Likely 	return IRQ_HANDLED;
694ca632f55SGrant Likely }
695ca632f55SGrant Likely 
696b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data)
697b0312482SJan Kiszka {
698*29d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
699b0312482SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1,
700b0312482SJan Kiszka 			 pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
701b0312482SJan Kiszka 	if (!pxa25x_ssp_comp(drv_data))
702b0312482SJan Kiszka 		pxa2xx_spi_write(drv_data, SSTO, 0);
703b0312482SJan Kiszka 	write_SSSR_CS(drv_data, drv_data->clear_sr);
704b0312482SJan Kiszka 
705b0312482SJan Kiszka 	dev_err(&drv_data->pdev->dev,
706b0312482SJan Kiszka 		"bad message state in interrupt handler\n");
707b0312482SJan Kiszka }
708b0312482SJan Kiszka 
709ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
710ca632f55SGrant Likely {
711ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
7127d94a505SMika Westerberg 	u32 sccr1_reg;
713ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
714ca632f55SGrant Likely 	u32 status;
715ca632f55SGrant Likely 
7167d94a505SMika Westerberg 	/*
7177d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
7187d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
7197d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
7207d94a505SMika Westerberg 	 * interrupt is enabled).
7217d94a505SMika Westerberg 	 */
7227d94a505SMika Westerberg 	if (pm_runtime_suspended(&drv_data->pdev->dev))
7237d94a505SMika Westerberg 		return IRQ_NONE;
7247d94a505SMika Westerberg 
725269e4a41SMika Westerberg 	/*
726269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
727269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
728269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
729269e4a41SMika Westerberg 	 * powered off.
730269e4a41SMika Westerberg 	 */
731c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
732269e4a41SMika Westerberg 	if (status == ~0)
733269e4a41SMika Westerberg 		return IRQ_NONE;
734269e4a41SMika Westerberg 
735c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
736ca632f55SGrant Likely 
737ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
738ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
739ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
740ca632f55SGrant Likely 
74102bc933eSTan, Jui Nee 	/* Ignore RX timeout interrupt if it is disabled */
74202bc933eSTan, Jui Nee 	if (!(sccr1_reg & SSCR1_TINTE))
74302bc933eSTan, Jui Nee 		mask &= ~SSSR_TINT;
74402bc933eSTan, Jui Nee 
745ca632f55SGrant Likely 	if (!(status & mask))
746ca632f55SGrant Likely 		return IRQ_NONE;
747ca632f55SGrant Likely 
748e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
749e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
750e51e9b93SJan Kiszka 
75151eea52dSLubomir Rintel 	if (!drv_data->controller->cur_msg) {
752b0312482SJan Kiszka 		handle_bad_msg(drv_data);
753ca632f55SGrant Likely 		/* Never fail */
754ca632f55SGrant Likely 		return IRQ_HANDLED;
755ca632f55SGrant Likely 	}
756ca632f55SGrant Likely 
757ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
758ca632f55SGrant Likely }
759ca632f55SGrant Likely 
760e5262d05SWeike Chen /*
7619df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
7629df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
7639df461ecSAndy Shevchenko  *
7649df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
7659df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
7669df461ecSAndy Shevchenko  *
7679df461ecSAndy Shevchenko  * Fsys = 200MHz
7689df461ecSAndy Shevchenko  *
7699df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
7709df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
7719df461ecSAndy Shevchenko  *
7729df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
7739df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
7749df461ecSAndy Shevchenko  *
7759df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
7769df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
7779df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
7789df461ecSAndy Shevchenko  *       k = [1, 256]
7799df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
7809df461ecSAndy Shevchenko  *
7819df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
7829df461ecSAndy Shevchenko  * are:
7839df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
7849df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
7859df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
7869df461ecSAndy Shevchenko  *
7879df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
7889df461ecSAndy Shevchenko  *
7899df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
7909df461ecSAndy Shevchenko  * to the asked baud rate.
791e5262d05SWeike Chen  */
7929df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
793e5262d05SWeike Chen {
7949df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
7959df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
7969df461ecSAndy Shevchenko 						   see (2) */
7979df461ecSAndy Shevchenko 						/* case 3 */
7989df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
7999df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
8009df461ecSAndy Shevchenko 	unsigned long scale;
8019df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
8029df461ecSAndy Shevchenko 	long r, r1, r2;
8039df461ecSAndy Shevchenko 	u32 mul;
804e5262d05SWeike Chen 
8059df461ecSAndy Shevchenko 	/* Case 1 */
8069df461ecSAndy Shevchenko 
8079df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
8089df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
8099df461ecSAndy Shevchenko 
8109df461ecSAndy Shevchenko 	/* Calculate initial quot */
8113ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
8129df461ecSAndy Shevchenko 
8139df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
8149df461ecSAndy Shevchenko 	if (q1 > 256) {
8159df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
8169df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
8179df461ecSAndy Shevchenko 		if (scale > 9) {
8189df461ecSAndy Shevchenko 			q1 >>= scale - 9;
8199df461ecSAndy Shevchenko 			mul >>= scale - 9;
8209df461ecSAndy Shevchenko 		}
8219df461ecSAndy Shevchenko 
8229df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
8239df461ecSAndy Shevchenko 		q1 += q1 & 1;
8249df461ecSAndy Shevchenko 	}
8259df461ecSAndy Shevchenko 
8269df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
8279df461ecSAndy Shevchenko 	scale = __ffs(q1);
8289df461ecSAndy Shevchenko 	q1 >>= scale;
8299df461ecSAndy Shevchenko 	mul >>= scale;
8309df461ecSAndy Shevchenko 
8319df461ecSAndy Shevchenko 	/* Get the remainder */
8329df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
8339df461ecSAndy Shevchenko 
8349df461ecSAndy Shevchenko 	/* Case 2 */
8359df461ecSAndy Shevchenko 
8363ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
8379df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
8389df461ecSAndy Shevchenko 
8399df461ecSAndy Shevchenko 	/*
8409df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
8419df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
8429df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
8439df461ecSAndy Shevchenko 	 */
8449df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
8459df461ecSAndy Shevchenko 		/* case 1 is better */
8469df461ecSAndy Shevchenko 		r = r1;
8479df461ecSAndy Shevchenko 		q = q1;
8489df461ecSAndy Shevchenko 	} else {
8499df461ecSAndy Shevchenko 		/* case 2 is better */
8509df461ecSAndy Shevchenko 		r = r2;
8519df461ecSAndy Shevchenko 		q = q2;
8529df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
8539df461ecSAndy Shevchenko 	}
8549df461ecSAndy Shevchenko 
8553ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
8569df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
8579df461ecSAndy Shevchenko 		u64 fssp;
8589df461ecSAndy Shevchenko 		u32 m;
8599df461ecSAndy Shevchenko 
8609df461ecSAndy Shevchenko 		/* Calculate initial quot */
8613ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
8629df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
8639df461ecSAndy Shevchenko 
8649df461ecSAndy Shevchenko 		/* Get the remainder */
8659df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
8669df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
8679df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
8689df461ecSAndy Shevchenko 
8699df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
8709df461ecSAndy Shevchenko 		if (r1 < r) {
8719df461ecSAndy Shevchenko 			/* case 3 is better */
8729df461ecSAndy Shevchenko 			q = 1;
8739df461ecSAndy Shevchenko 			mul = m;
874e5262d05SWeike Chen 		}
875e5262d05SWeike Chen 	}
876e5262d05SWeike Chen 
8779df461ecSAndy Shevchenko 	*dds = mul;
8789df461ecSAndy Shevchenko 	return q - 1;
879e5262d05SWeike Chen }
880e5262d05SWeike Chen 
8813343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
882ca632f55SGrant Likely {
88351eea52dSLubomir Rintel 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
8843343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
8853343b7a6SMika Westerberg 
8863343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
887ca632f55SGrant Likely 
88829f21337SFlavio Suligoi 	/*
88929f21337SFlavio Suligoi 	 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
89029f21337SFlavio Suligoi 	 * that the SSP transmission rate can be greater than the device rate
89129f21337SFlavio Suligoi 	 */
892ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
89329f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
894ca632f55SGrant Likely 	else
89529f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
896ca632f55SGrant Likely }
897ca632f55SGrant Likely 
898e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
899d2c2f6a4SAndy Shevchenko 					   int rate)
900e5262d05SWeike Chen {
90196579a4eSJarkko Nikula 	struct chip_data *chip =
90251eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
903025ffe88SAndy Shevchenko 	unsigned int clk_div;
904e5262d05SWeike Chen 
905e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
906e5262d05SWeike Chen 	case QUARK_X1000_SSP:
9079df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
908eecacf73SDan Carpenter 		break;
909e5262d05SWeike Chen 	default:
910025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
911eecacf73SDan Carpenter 		break;
912e5262d05SWeike Chen 	}
913025ffe88SAndy Shevchenko 	return clk_div << 8;
914e5262d05SWeike Chen }
915e5262d05SWeike Chen 
91651eea52dSLubomir Rintel static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
917b6ced294SJarkko Nikula 			       struct spi_device *spi,
918b6ced294SJarkko Nikula 			       struct spi_transfer *xfer)
919b6ced294SJarkko Nikula {
920b6ced294SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
921b6ced294SJarkko Nikula 
922b6ced294SJarkko Nikula 	return chip->enable_dma &&
923b6ced294SJarkko Nikula 	       xfer->len <= MAX_DMA_LEN &&
924b6ced294SJarkko Nikula 	       xfer->len >= chip->dma_burst_size;
925b6ced294SJarkko Nikula }
926b6ced294SJarkko Nikula 
92751eea52dSLubomir Rintel static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
928d5898e19SJarkko Nikula 				   struct spi_device *spi,
929d5898e19SJarkko Nikula 				   struct spi_transfer *transfer)
930ca632f55SGrant Likely {
93151eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
93251eea52dSLubomir Rintel 	struct spi_message *message = controller->cur_msg;
93320f4c379SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
93496579a4eSJarkko Nikula 	u32 dma_thresh = chip->dma_threshold;
93596579a4eSJarkko Nikula 	u32 dma_burst = chip->dma_burst_size;
93696579a4eSJarkko Nikula 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
937bffc967eSJarkko Nikula 	u32 clk_div;
938bffc967eSJarkko Nikula 	u8 bits;
939bffc967eSJarkko Nikula 	u32 speed;
940ca632f55SGrant Likely 	u32 cr0;
941ca632f55SGrant Likely 	u32 cr1;
9427d1f1bf6SAndy Shevchenko 	int err;
943b6ced294SJarkko Nikula 	int dma_mapped;
944ca632f55SGrant Likely 
945cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
946b6ced294SJarkko Nikula 	if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
947ca632f55SGrant Likely 
948ca632f55SGrant Likely 		/* reject already-mapped transfers; PIO won't always work */
949ca632f55SGrant Likely 		if (message->is_dma_mapped
950ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
951748fbadfSJarkko Nikula 			dev_err(&spi->dev,
9528ae55af3SJarkko Nikula 				"Mapped transfer length of %u is greater than %d\n",
953ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
954d5898e19SJarkko Nikula 			return -EINVAL;
955ca632f55SGrant Likely 		}
956ca632f55SGrant Likely 
957ca632f55SGrant Likely 		/* warn ... we force this to PIO mode */
95820f4c379SJarkko Nikula 		dev_warn_ratelimited(&spi->dev,
9598ae55af3SJarkko Nikula 				     "DMA disabled for transfer length %ld greater than %d\n",
960d5898e19SJarkko Nikula 				     (long)transfer->len, MAX_DMA_LEN);
961ca632f55SGrant Likely 	}
962ca632f55SGrant Likely 
963ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
964cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
965748fbadfSJarkko Nikula 		dev_err(&spi->dev, "Flush failed\n");
966d5898e19SJarkko Nikula 		return -EIO;
967ca632f55SGrant Likely 	}
968ca632f55SGrant Likely 	drv_data->n_bytes = chip->n_bytes;
969ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
970ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
971ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
972ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
973ca632f55SGrant Likely 	drv_data->write = drv_data->tx ? chip->write : null_writer;
974ca632f55SGrant Likely 	drv_data->read = drv_data->rx ? chip->read : null_reader;
975ca632f55SGrant Likely 
976ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
977ca632f55SGrant Likely 	bits = transfer->bits_per_word;
978ca632f55SGrant Likely 	speed = transfer->speed_hz;
979ca632f55SGrant Likely 
980d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
981ca632f55SGrant Likely 
982ca632f55SGrant Likely 	if (bits <= 8) {
983ca632f55SGrant Likely 		drv_data->n_bytes = 1;
984ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
985ca632f55SGrant Likely 					u8_reader : null_reader;
986ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
987ca632f55SGrant Likely 					u8_writer : null_writer;
988ca632f55SGrant Likely 	} else if (bits <= 16) {
989ca632f55SGrant Likely 		drv_data->n_bytes = 2;
990ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
991ca632f55SGrant Likely 					u16_reader : null_reader;
992ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
993ca632f55SGrant Likely 					u16_writer : null_writer;
994ca632f55SGrant Likely 	} else if (bits <= 32) {
995ca632f55SGrant Likely 		drv_data->n_bytes = 4;
996ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
997ca632f55SGrant Likely 					u32_reader : null_reader;
998ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
999ca632f55SGrant Likely 					u32_writer : null_writer;
1000ca632f55SGrant Likely 	}
1001196b0e2cSJarkko Nikula 	/*
1002196b0e2cSJarkko Nikula 	 * if bits/word is changed in dma mode, then must check the
1003196b0e2cSJarkko Nikula 	 * thresholds and burst also
1004196b0e2cSJarkko Nikula 	 */
1005ca632f55SGrant Likely 	if (chip->enable_dma) {
1006cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
100720f4c379SJarkko Nikula 						spi,
1008ca632f55SGrant Likely 						bits, &dma_burst,
1009ca632f55SGrant Likely 						&dma_thresh))
101020f4c379SJarkko Nikula 			dev_warn_ratelimited(&spi->dev,
10118ae55af3SJarkko Nikula 					     "DMA burst size reduced to match bits_per_word\n");
1012ca632f55SGrant Likely 	}
1013ca632f55SGrant Likely 
101451eea52dSLubomir Rintel 	dma_mapped = controller->can_dma &&
101520f4c379SJarkko Nikula 		     controller->can_dma(controller, spi, transfer) &&
101651eea52dSLubomir Rintel 		     controller->cur_msg_mapped;
1017b6ced294SJarkko Nikula 	if (dma_mapped) {
1018ca632f55SGrant Likely 
1019ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
1020cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1021ca632f55SGrant Likely 
1022d5898e19SJarkko Nikula 		err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1023d5898e19SJarkko Nikula 		if (err)
1024d5898e19SJarkko Nikula 			return err;
1025ca632f55SGrant Likely 
1026ca632f55SGrant Likely 		/* Clear status and start DMA engine */
1027ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1028c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1029cd7bed00SMika Westerberg 
1030cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
1031ca632f55SGrant Likely 	} else {
1032ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
1033ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
1034ca632f55SGrant Likely 
1035ca632f55SGrant Likely 		/* Clear status  */
1036ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1037ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1038ca632f55SGrant Likely 	}
1039ca632f55SGrant Likely 
1040ee03672dSJarkko Nikula 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1041ee03672dSJarkko Nikula 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1042ee03672dSJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
104320f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
104451eea52dSLubomir Rintel 			controller->max_speed_hz
1045ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1046b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1047ee03672dSJarkko Nikula 	else
104820f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
104951eea52dSLubomir Rintel 			controller->max_speed_hz / 2
1050ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1051b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1052ee03672dSJarkko Nikula 
1053a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
1054c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
1055c039dd27SJarkko Nikula 		    != chip->lpss_rx_threshold)
1056c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSIRF,
1057c039dd27SJarkko Nikula 					 chip->lpss_rx_threshold);
1058c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
1059c039dd27SJarkko Nikula 		    != chip->lpss_tx_threshold)
1060c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSITF,
1061c039dd27SJarkko Nikula 					 chip->lpss_tx_threshold);
1062a0d2642eSMika Westerberg 	}
1063a0d2642eSMika Westerberg 
1064e5262d05SWeike Chen 	if (is_quark_x1000_ssp(drv_data) &&
1065c039dd27SJarkko Nikula 	    (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
1066c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
1067e5262d05SWeike Chen 
1068ca632f55SGrant Likely 	/* see if we need to reload the config registers */
1069c039dd27SJarkko Nikula 	if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1070c039dd27SJarkko Nikula 	    || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1071c039dd27SJarkko Nikula 	    != (cr1 & change_mask)) {
1072ca632f55SGrant Likely 		/* stop the SSP, and update the other bits */
1073*29d7e05cSLubomir Rintel 		if (drv_data->ssp_type != MMP2_SSP)
1074c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
1075ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1076c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1077ca632f55SGrant Likely 		/* first set CR1 without interrupt and service enables */
1078c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
1079ca632f55SGrant Likely 		/* restart the SSP */
1080c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0);
1081ca632f55SGrant Likely 
1082ca632f55SGrant Likely 	} else {
1083ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1084c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1085ca632f55SGrant Likely 	}
1086ca632f55SGrant Likely 
108782391856SLubomir Rintel 	if (drv_data->ssp_type == MMP2_SSP) {
108882391856SLubomir Rintel 		u8 tx_level = (pxa2xx_spi_read(drv_data, SSSR)
108982391856SLubomir Rintel 					& SSSR_TFL_MASK) >> 8;
109082391856SLubomir Rintel 
109182391856SLubomir Rintel 		if (tx_level) {
109282391856SLubomir Rintel 			/* On MMP2, flipping SSE doesn't to empty TXFIFO. */
109382391856SLubomir Rintel 			dev_warn(&spi->dev, "%d bytes of garbage in TXFIFO!\n",
109482391856SLubomir Rintel 								tx_level);
109582391856SLubomir Rintel 			if (tx_level > transfer->len)
109682391856SLubomir Rintel 				tx_level = transfer->len;
109782391856SLubomir Rintel 			drv_data->tx += tx_level;
109882391856SLubomir Rintel 		}
109982391856SLubomir Rintel 	}
110082391856SLubomir Rintel 
110151eea52dSLubomir Rintel 	if (spi_controller_is_slave(controller)) {
1102ec93cb6fSLubomir Rintel 		while (drv_data->write(drv_data))
1103ec93cb6fSLubomir Rintel 			;
110477d33897SLubomir Rintel 		if (drv_data->gpiod_ready) {
110577d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 1);
110677d33897SLubomir Rintel 			udelay(1);
110777d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 0);
110877d33897SLubomir Rintel 		}
1109ec93cb6fSLubomir Rintel 	}
1110ec93cb6fSLubomir Rintel 
1111d5898e19SJarkko Nikula 	/*
1112d5898e19SJarkko Nikula 	 * Release the data by enabling service requests and interrupts,
1113d5898e19SJarkko Nikula 	 * without changing any mode bits
1114d5898e19SJarkko Nikula 	 */
1115c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1116d5898e19SJarkko Nikula 
1117d5898e19SJarkko Nikula 	return 1;
1118ca632f55SGrant Likely }
1119ca632f55SGrant Likely 
112051eea52dSLubomir Rintel static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
1121ec93cb6fSLubomir Rintel {
112251eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1123ec93cb6fSLubomir Rintel 
1124ec93cb6fSLubomir Rintel 	/* Stop and reset SSP */
1125ec93cb6fSLubomir Rintel 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1126ec93cb6fSLubomir Rintel 	reset_sccr1(drv_data);
1127ec93cb6fSLubomir Rintel 	if (!pxa25x_ssp_comp(drv_data))
1128ec93cb6fSLubomir Rintel 		pxa2xx_spi_write(drv_data, SSTO, 0);
1129ec93cb6fSLubomir Rintel 	pxa2xx_spi_flush(drv_data);
1130*29d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
1131ec93cb6fSLubomir Rintel 
1132ec93cb6fSLubomir Rintel 	dev_dbg(&drv_data->pdev->dev, "transfer aborted\n");
1133ec93cb6fSLubomir Rintel 
113451eea52dSLubomir Rintel 	drv_data->controller->cur_msg->status = -EINTR;
113551eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
1136ec93cb6fSLubomir Rintel 
1137ec93cb6fSLubomir Rintel 	return 0;
1138ec93cb6fSLubomir Rintel }
1139ec93cb6fSLubomir Rintel 
114051eea52dSLubomir Rintel static void pxa2xx_spi_handle_err(struct spi_controller *controller,
11417f86bde9SMika Westerberg 				 struct spi_message *msg)
1142ca632f55SGrant Likely {
114351eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1144ca632f55SGrant Likely 
1145d5898e19SJarkko Nikula 	/* Disable the SSP */
1146*29d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
1147d5898e19SJarkko Nikula 	/* Clear and disable interrupts and service requests */
1148d5898e19SJarkko Nikula 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1149d5898e19SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1,
1150d5898e19SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR1)
1151d5898e19SJarkko Nikula 			 & ~(drv_data->int_cr1 | drv_data->dma_cr1));
1152d5898e19SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
1153d5898e19SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1154ca632f55SGrant Likely 
1155d5898e19SJarkko Nikula 	/*
1156d5898e19SJarkko Nikula 	 * Stop the DMA if running. Note DMA callback handler may have unset
1157d5898e19SJarkko Nikula 	 * the dma_running already, which is fine as stopping is not needed
1158d5898e19SJarkko Nikula 	 * then but we shouldn't rely this flag for anything else than
1159d5898e19SJarkko Nikula 	 * stopping. For instance to differentiate between PIO and DMA
1160d5898e19SJarkko Nikula 	 * transfers.
1161d5898e19SJarkko Nikula 	 */
1162d5898e19SJarkko Nikula 	if (atomic_read(&drv_data->dma_running))
1163d5898e19SJarkko Nikula 		pxa2xx_spi_dma_stop(drv_data);
1164ca632f55SGrant Likely }
1165ca632f55SGrant Likely 
116651eea52dSLubomir Rintel static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
11677d94a505SMika Westerberg {
116851eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
11697d94a505SMika Westerberg 
11707d94a505SMika Westerberg 	/* Disable the SSP now */
1171*29d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
11727d94a505SMika Westerberg 
11737d94a505SMika Westerberg 	return 0;
11747d94a505SMika Westerberg }
11757d94a505SMika Westerberg 
1176ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1177ca632f55SGrant Likely 		    struct pxa2xx_spi_chip *chip_info)
1178ca632f55SGrant Likely {
11793cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
11803cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1181c18d925fSJan Kiszka 	struct gpio_desc *gpiod;
1182ca632f55SGrant Likely 	int err = 0;
1183ca632f55SGrant Likely 
118499f499cdSMika Westerberg 	if (chip == NULL)
118599f499cdSMika Westerberg 		return 0;
118699f499cdSMika Westerberg 
11876ac5a435SAndy Shevchenko 	if (drv_data->cs_gpiods) {
11886ac5a435SAndy Shevchenko 		gpiod = drv_data->cs_gpiods[spi->chip_select];
11896ac5a435SAndy Shevchenko 		if (gpiod) {
1190c18d925fSJan Kiszka 			chip->gpiod_cs = gpiod;
119199f499cdSMika Westerberg 			chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
119299f499cdSMika Westerberg 			gpiod_set_value(gpiod, chip->gpio_cs_inverted);
11936ac5a435SAndy Shevchenko 		}
119499f499cdSMika Westerberg 
119599f499cdSMika Westerberg 		return 0;
119699f499cdSMika Westerberg 	}
119799f499cdSMika Westerberg 
119899f499cdSMika Westerberg 	if (chip_info == NULL)
1199ca632f55SGrant Likely 		return 0;
1200ca632f55SGrant Likely 
1201ca632f55SGrant Likely 	/* NOTE: setup() can be called multiple times, possibly with
1202ca632f55SGrant Likely 	 * different chip_info, release previously requested GPIO
1203ca632f55SGrant Likely 	 */
1204c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
1205a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1206c18d925fSJan Kiszka 		chip->gpiod_cs = NULL;
1207c18d925fSJan Kiszka 	}
1208ca632f55SGrant Likely 
1209ca632f55SGrant Likely 	/* If (*cs_control) is provided, ignore GPIO chip select */
1210ca632f55SGrant Likely 	if (chip_info->cs_control) {
1211ca632f55SGrant Likely 		chip->cs_control = chip_info->cs_control;
1212ca632f55SGrant Likely 		return 0;
1213ca632f55SGrant Likely 	}
1214ca632f55SGrant Likely 
1215ca632f55SGrant Likely 	if (gpio_is_valid(chip_info->gpio_cs)) {
1216ca632f55SGrant Likely 		err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1217ca632f55SGrant Likely 		if (err) {
1218f6bd03a7SJarkko Nikula 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1219f6bd03a7SJarkko Nikula 				chip_info->gpio_cs);
1220ca632f55SGrant Likely 			return err;
1221ca632f55SGrant Likely 		}
1222ca632f55SGrant Likely 
1223c18d925fSJan Kiszka 		gpiod = gpio_to_desc(chip_info->gpio_cs);
1224c18d925fSJan Kiszka 		chip->gpiod_cs = gpiod;
1225ca632f55SGrant Likely 		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1226ca632f55SGrant Likely 
1227c18d925fSJan Kiszka 		err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
1228ca632f55SGrant Likely 	}
1229ca632f55SGrant Likely 
1230ca632f55SGrant Likely 	return err;
1231ca632f55SGrant Likely }
1232ca632f55SGrant Likely 
1233ca632f55SGrant Likely static int setup(struct spi_device *spi)
1234ca632f55SGrant Likely {
1235bffc967eSJarkko Nikula 	struct pxa2xx_spi_chip *chip_info;
1236ca632f55SGrant Likely 	struct chip_data *chip;
1237dccf7369SJarkko Nikula 	const struct lpss_config *config;
12383cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12393cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1240a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1241a0d2642eSMika Westerberg 
1242e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1243e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1244e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1245e5262d05SWeike Chen 		tx_hi_thres = 0;
1246e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1247e5262d05SWeike Chen 		break;
12487c7289a4SAndy Shevchenko 	case CE4100_SSP:
12497c7289a4SAndy Shevchenko 		tx_thres = TX_THRESH_CE4100_DFLT;
12507c7289a4SAndy Shevchenko 		tx_hi_thres = 0;
12517c7289a4SAndy Shevchenko 		rx_thres = RX_THRESH_CE4100_DFLT;
12527c7289a4SAndy Shevchenko 		break;
125303fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
125403fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
125530f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
125634cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1257b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1258fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
1259dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1260dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1261dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1262dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1263e5262d05SWeike Chen 		break;
1264e5262d05SWeike Chen 	default:
1265a0d2642eSMika Westerberg 		tx_hi_thres = 0;
126651eea52dSLubomir Rintel 		if (spi_controller_is_slave(drv_data->controller)) {
1267ec93cb6fSLubomir Rintel 			tx_thres = 1;
1268ec93cb6fSLubomir Rintel 			rx_thres = 2;
1269ec93cb6fSLubomir Rintel 		} else {
1270ec93cb6fSLubomir Rintel 			tx_thres = TX_THRESH_DFLT;
1271a0d2642eSMika Westerberg 			rx_thres = RX_THRESH_DFLT;
1272ec93cb6fSLubomir Rintel 		}
1273e5262d05SWeike Chen 		break;
1274a0d2642eSMika Westerberg 	}
1275ca632f55SGrant Likely 
1276ca632f55SGrant Likely 	/* Only alloc on first setup */
1277ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1278ca632f55SGrant Likely 	if (!chip) {
1279ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
12809deae459SJingoo Han 		if (!chip)
1281ca632f55SGrant Likely 			return -ENOMEM;
1282ca632f55SGrant Likely 
1283ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1284ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1285f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1286f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1287ca632f55SGrant Likely 				kfree(chip);
1288ca632f55SGrant Likely 				return -EINVAL;
1289ca632f55SGrant Likely 			}
1290ca632f55SGrant Likely 
1291ca632f55SGrant Likely 			chip->frm = spi->chip_select;
1292c18d925fSJan Kiszka 		}
129351eea52dSLubomir Rintel 		chip->enable_dma = drv_data->controller_info->enable_dma;
1294ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1295ca632f55SGrant Likely 	}
1296ca632f55SGrant Likely 
1297ca632f55SGrant Likely 	/* protocol drivers may change the chip settings, so...
1298ca632f55SGrant Likely 	 * if chip_info exists, use it */
1299ca632f55SGrant Likely 	chip_info = spi->controller_data;
1300ca632f55SGrant Likely 
1301ca632f55SGrant Likely 	/* chip_info isn't always needed */
1302ca632f55SGrant Likely 	chip->cr1 = 0;
1303ca632f55SGrant Likely 	if (chip_info) {
1304ca632f55SGrant Likely 		if (chip_info->timeout)
1305ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1306ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1307ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1308a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1309a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1310ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1311ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1312ca632f55SGrant Likely 		chip->dma_threshold = 0;
1313ca632f55SGrant Likely 		if (chip_info->enable_loopback)
1314ca632f55SGrant Likely 			chip->cr1 = SSCR1_LBM;
1315ca632f55SGrant Likely 	}
131651eea52dSLubomir Rintel 	if (spi_controller_is_slave(drv_data->controller)) {
1317ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCFR;
1318ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCLKDIR;
1319ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SFRMDIR;
1320ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SPH;
1321ec93cb6fSLubomir Rintel 	}
1322ca632f55SGrant Likely 
1323a0d2642eSMika Westerberg 	chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1324a0d2642eSMika Westerberg 	chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1325a0d2642eSMika Westerberg 				| SSITF_TxHiThresh(tx_hi_thres);
1326a0d2642eSMika Westerberg 
1327ca632f55SGrant Likely 	/* set dma burst and threshold outside of chip_info path so that if
1328ca632f55SGrant Likely 	 * chip_info goes away after setting chip->enable_dma, the
1329ca632f55SGrant Likely 	 * burst and threshold can still respond to changes in bits_per_word */
1330ca632f55SGrant Likely 	if (chip->enable_dma) {
1331ca632f55SGrant Likely 		/* set up legal burst and threshold for dma */
1332cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1333cd7bed00SMika Westerberg 						spi->bits_per_word,
1334ca632f55SGrant Likely 						&chip->dma_burst_size,
1335ca632f55SGrant Likely 						&chip->dma_threshold)) {
1336f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1337f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1338ca632f55SGrant Likely 		}
1339000c6af4SAndy Shevchenko 		dev_dbg(&spi->dev,
1340000c6af4SAndy Shevchenko 			"in setup: DMA burst size set to %u\n",
1341000c6af4SAndy Shevchenko 			chip->dma_burst_size);
1342ca632f55SGrant Likely 	}
1343ca632f55SGrant Likely 
1344e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1345e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1346e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1347e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1348e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1349e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1350e5262d05SWeike Chen 		break;
13517c7289a4SAndy Shevchenko 	case CE4100_SSP:
13527c7289a4SAndy Shevchenko 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
13537c7289a4SAndy Shevchenko 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
13547c7289a4SAndy Shevchenko 		break;
1355e5262d05SWeike Chen 	default:
1356e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1357e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1358e5262d05SWeike Chen 		break;
1359e5262d05SWeike Chen 	}
1360e5262d05SWeike Chen 
1361ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1362ca632f55SGrant Likely 	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1363ca632f55SGrant Likely 			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1364ca632f55SGrant Likely 
1365b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1366b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1367b833172fSMika Westerberg 
1368ca632f55SGrant Likely 	if (spi->bits_per_word <= 8) {
1369ca632f55SGrant Likely 		chip->n_bytes = 1;
1370ca632f55SGrant Likely 		chip->read = u8_reader;
1371ca632f55SGrant Likely 		chip->write = u8_writer;
1372ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 16) {
1373ca632f55SGrant Likely 		chip->n_bytes = 2;
1374ca632f55SGrant Likely 		chip->read = u16_reader;
1375ca632f55SGrant Likely 		chip->write = u16_writer;
1376ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 32) {
1377ca632f55SGrant Likely 		chip->n_bytes = 4;
1378ca632f55SGrant Likely 		chip->read = u32_reader;
1379ca632f55SGrant Likely 		chip->write = u32_writer;
1380ca632f55SGrant Likely 	}
1381ca632f55SGrant Likely 
1382ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1383ca632f55SGrant Likely 
1384ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
1385ca632f55SGrant Likely 		return 0;
1386ca632f55SGrant Likely 
1387ca632f55SGrant Likely 	return setup_cs(spi, chip, chip_info);
1388ca632f55SGrant Likely }
1389ca632f55SGrant Likely 
1390ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1391ca632f55SGrant Likely {
1392ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
13933cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
13943cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1395ca632f55SGrant Likely 
1396ca632f55SGrant Likely 	if (!chip)
1397ca632f55SGrant Likely 		return;
1398ca632f55SGrant Likely 
13996ac5a435SAndy Shevchenko 	if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
1400c18d925fSJan Kiszka 	    chip->gpiod_cs)
1401a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1402ca632f55SGrant Likely 
1403ca632f55SGrant Likely 	kfree(chip);
1404ca632f55SGrant Likely }
1405ca632f55SGrant Likely 
14068422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
140703fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
140803fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
140903fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
141003fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
141103fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
141230f3a6abSMika Westerberg 	{ "8086228E", LPSS_BSW_SSP },
141303fbf488SJarkko Nikula 	{ },
141403fbf488SJarkko Nikula };
141503fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
141603fbf488SJarkko Nikula 
141734cadd9cSJarkko Nikula /*
141834cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
141934cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
142034cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
142134cadd9cSJarkko Nikula  */
142234cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
142334cadd9cSJarkko Nikula 	/* SPT-LP */
142434cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
142534cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
142634cadd9cSJarkko Nikula 	/* SPT-H */
142734cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
142834cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1429704d2b07SMika Westerberg 	/* KBL-H */
1430704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1431704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
14326157d4c2SJarkko Nikula 	/* CML-V */
14336157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
14346157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
1435c1b03f11SJarkko Nikula 	/* BXT A-Step */
1436b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1437b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1438b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1439c1b03f11SJarkko Nikula 	/* BXT B-Step */
1440c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1441c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1442c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1443e18a80acSDavid E. Box 	/* GLK */
1444e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1445e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1446e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
144722d71a50SMika Westerberg 	/* ICL-LP */
144822d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
144922d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
145022d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
14518cc77204SJarkko Nikula 	/* EHL */
14528cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
14538cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
14548cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
14559c7315c9SJarkko Nikula 	/* JSL */
14569c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
14579c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
14589c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
1459b7c08cf8SJarkko Nikula 	/* APL */
1460b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1461b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1462b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1463fc0b2accSJarkko Nikula 	/* CNL-LP */
1464fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1465fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1466fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1467fc0b2accSJarkko Nikula 	/* CNL-H */
1468fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1469fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1470fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
147141a91802SEvan Green 	/* CML-LP */
147241a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
147341a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
147441a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
1475a4127952SJarkko Nikula 	/* TGL-LP */
1476a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1477a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1478a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1479a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1480a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1481a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1482a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
148394e5c23dSAxel Lin 	{ },
148434cadd9cSJarkko Nikula };
148534cadd9cSJarkko Nikula 
148687ae1d2dSLubomir Rintel static const struct of_device_id pxa2xx_spi_of_match[] = {
148787ae1d2dSLubomir Rintel 	{ .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
148887ae1d2dSLubomir Rintel 	{},
148987ae1d2dSLubomir Rintel };
149087ae1d2dSLubomir Rintel MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
149187ae1d2dSLubomir Rintel 
149287ae1d2dSLubomir Rintel #ifdef CONFIG_ACPI
149387ae1d2dSLubomir Rintel 
1494365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
149587ae1d2dSLubomir Rintel {
1496365e856eSAndy Shevchenko 	struct acpi_device *adev;
149787ae1d2dSLubomir Rintel 	unsigned int devid;
149887ae1d2dSLubomir Rintel 	int port_id = -1;
149987ae1d2dSLubomir Rintel 
1500365e856eSAndy Shevchenko 	adev = ACPI_COMPANION(dev);
150187ae1d2dSLubomir Rintel 	if (adev && adev->pnp.unique_id &&
150287ae1d2dSLubomir Rintel 	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
150387ae1d2dSLubomir Rintel 		port_id = devid;
150487ae1d2dSLubomir Rintel 	return port_id;
150587ae1d2dSLubomir Rintel }
150687ae1d2dSLubomir Rintel 
150787ae1d2dSLubomir Rintel #else /* !CONFIG_ACPI */
150887ae1d2dSLubomir Rintel 
1509365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
151087ae1d2dSLubomir Rintel {
151187ae1d2dSLubomir Rintel 	return -1;
151287ae1d2dSLubomir Rintel }
151387ae1d2dSLubomir Rintel 
151487ae1d2dSLubomir Rintel #endif /* CONFIG_ACPI */
151587ae1d2dSLubomir Rintel 
151687ae1d2dSLubomir Rintel 
151787ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
151887ae1d2dSLubomir Rintel 
151934cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
152034cadd9cSJarkko Nikula {
15215ba846b1SAndy Shevchenko 	return param == chan->device->dev;
152234cadd9cSJarkko Nikula }
152334cadd9cSJarkko Nikula 
152487ae1d2dSLubomir Rintel #endif /* CONFIG_PCI */
152587ae1d2dSLubomir Rintel 
152651eea52dSLubomir Rintel static struct pxa2xx_spi_controller *
15270db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1528a3496855SMika Westerberg {
152951eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *pdata;
1530a3496855SMika Westerberg 	struct ssp_device *ssp;
1531a3496855SMika Westerberg 	struct resource *res;
15326fb7427dSAndy Shevchenko 	struct device *parent = pdev->dev.parent;
15336fb7427dSAndy Shevchenko 	struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
153434cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
153555ef8262SLubomir Rintel 	enum pxa_ssp_type type;
1536f2faa3ecSAndy Shevchenko 	const void *match;
1537a3496855SMika Westerberg 
15386fb7427dSAndy Shevchenko 	if (pcidev)
15396fb7427dSAndy Shevchenko 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
154034cadd9cSJarkko Nikula 
1541f2faa3ecSAndy Shevchenko 	match = device_get_match_data(&pdev->dev);
1542f2faa3ecSAndy Shevchenko 	if (match)
1543f2faa3ecSAndy Shevchenko 		type = (enum pxa_ssp_type)match;
154434cadd9cSJarkko Nikula 	else if (pcidev_id)
154555ef8262SLubomir Rintel 		type = (enum pxa_ssp_type)pcidev_id->driver_data;
154603fbf488SJarkko Nikula 	else
154703fbf488SJarkko Nikula 		return NULL;
154803fbf488SJarkko Nikula 
1549cc0ee987SMika Westerberg 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
15509deae459SJingoo Han 	if (!pdata)
1551a3496855SMika Westerberg 		return NULL;
1552a3496855SMika Westerberg 
1553a3496855SMika Westerberg 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1554a3496855SMika Westerberg 	if (!res)
1555a3496855SMika Westerberg 		return NULL;
1556a3496855SMika Westerberg 
1557a3496855SMika Westerberg 	ssp = &pdata->ssp;
1558a3496855SMika Westerberg 
1559a3496855SMika Westerberg 	ssp->phys_base = res->start;
1560cbfd6a21SSachin Kamat 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1561cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
15626dc81f6fSMika Westerberg 		return NULL;
1563a3496855SMika Westerberg 
156487ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
156534cadd9cSJarkko Nikula 	if (pcidev_id) {
15666fb7427dSAndy Shevchenko 		pdata->tx_param = parent;
15676fb7427dSAndy Shevchenko 		pdata->rx_param = parent;
156834cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
156934cadd9cSJarkko Nikula 	}
157087ae1d2dSLubomir Rintel #endif
157134cadd9cSJarkko Nikula 
1572a3496855SMika Westerberg 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
15735eb263efSChuhong Yuan 	if (IS_ERR(ssp->clk))
15745eb263efSChuhong Yuan 		return NULL;
15755eb263efSChuhong Yuan 
1576a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
15775eb263efSChuhong Yuan 	if (ssp->irq < 0)
15785eb263efSChuhong Yuan 		return NULL;
15795eb263efSChuhong Yuan 
158003fbf488SJarkko Nikula 	ssp->type = type;
15814f3d9577SAndy Shevchenko 	ssp->dev = &pdev->dev;
1582365e856eSAndy Shevchenko 	ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
1583a3496855SMika Westerberg 
1584f2faa3ecSAndy Shevchenko 	pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
1585a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1586cddb339bSMika Westerberg 	pdata->enable_dma = true;
158737821a82SAndy Shevchenko 	pdata->dma_burst_size = 1;
1588a3496855SMika Westerberg 
1589a3496855SMika Westerberg 	return pdata;
1590a3496855SMika Westerberg }
1591a3496855SMika Westerberg 
159251eea52dSLubomir Rintel static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
15933cc7b0e3SJarkko Nikula 				      unsigned int cs)
15940c27d9cfSMika Westerberg {
159551eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
15960c27d9cfSMika Westerberg 
15970c27d9cfSMika Westerberg 	if (has_acpi_companion(&drv_data->pdev->dev)) {
15980c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
15990c27d9cfSMika Westerberg 		/*
16000c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
16010c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
16020c27d9cfSMika Westerberg 		 * to match what Linux expects.
16030c27d9cfSMika Westerberg 		 */
16040c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
160530f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
16060c27d9cfSMika Westerberg 			return cs - 1;
16070c27d9cfSMika Westerberg 
16080c27d9cfSMika Westerberg 		default:
16090c27d9cfSMika Westerberg 			break;
16100c27d9cfSMika Westerberg 		}
16110c27d9cfSMika Westerberg 	}
16120c27d9cfSMika Westerberg 
16130c27d9cfSMika Westerberg 	return cs;
16140c27d9cfSMika Westerberg }
16150c27d9cfSMika Westerberg 
1616fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1617ca632f55SGrant Likely {
1618ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
161951eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *platform_info;
162051eea52dSLubomir Rintel 	struct spi_controller *controller;
1621ca632f55SGrant Likely 	struct driver_data *drv_data;
1622ca632f55SGrant Likely 	struct ssp_device *ssp;
16238b136baaSJarkko Nikula 	const struct lpss_config *config;
162499f499cdSMika Westerberg 	int status, count;
1625c039dd27SJarkko Nikula 	u32 tmp;
1626ca632f55SGrant Likely 
1627851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1628851bacf5SMika Westerberg 	if (!platform_info) {
16290db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
1630a3496855SMika Westerberg 		if (!platform_info) {
1631851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
1632851bacf5SMika Westerberg 			return -ENODEV;
1633851bacf5SMika Westerberg 		}
1634a3496855SMika Westerberg 	}
1635ca632f55SGrant Likely 
1636ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1637851bacf5SMika Westerberg 	if (!ssp)
1638851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1639851bacf5SMika Westerberg 
1640851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
1641851bacf5SMika Westerberg 		dev_err(&pdev->dev, "failed to get ssp\n");
1642ca632f55SGrant Likely 		return -ENODEV;
1643ca632f55SGrant Likely 	}
1644ca632f55SGrant Likely 
1645ec93cb6fSLubomir Rintel 	if (platform_info->is_slave)
164651eea52dSLubomir Rintel 		controller = spi_alloc_slave(dev, sizeof(struct driver_data));
1647ec93cb6fSLubomir Rintel 	else
164851eea52dSLubomir Rintel 		controller = spi_alloc_master(dev, sizeof(struct driver_data));
1649ec93cb6fSLubomir Rintel 
165051eea52dSLubomir Rintel 	if (!controller) {
165151eea52dSLubomir Rintel 		dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1652ca632f55SGrant Likely 		pxa_ssp_free(ssp);
1653ca632f55SGrant Likely 		return -ENOMEM;
1654ca632f55SGrant Likely 	}
165551eea52dSLubomir Rintel 	drv_data = spi_controller_get_devdata(controller);
165651eea52dSLubomir Rintel 	drv_data->controller = controller;
165751eea52dSLubomir Rintel 	drv_data->controller_info = platform_info;
1658ca632f55SGrant Likely 	drv_data->pdev = pdev;
1659ca632f55SGrant Likely 	drv_data->ssp = ssp;
1660ca632f55SGrant Likely 
166151eea52dSLubomir Rintel 	controller->dev.of_node = pdev->dev.of_node;
1662ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
166351eea52dSLubomir Rintel 	controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1664ca632f55SGrant Likely 
166551eea52dSLubomir Rintel 	controller->bus_num = ssp->port_id;
166651eea52dSLubomir Rintel 	controller->dma_alignment = DMA_ALIGNMENT;
166751eea52dSLubomir Rintel 	controller->cleanup = cleanup;
166851eea52dSLubomir Rintel 	controller->setup = setup;
166951eea52dSLubomir Rintel 	controller->set_cs = pxa2xx_spi_set_cs;
167051eea52dSLubomir Rintel 	controller->transfer_one = pxa2xx_spi_transfer_one;
167151eea52dSLubomir Rintel 	controller->slave_abort = pxa2xx_spi_slave_abort;
167251eea52dSLubomir Rintel 	controller->handle_err = pxa2xx_spi_handle_err;
167351eea52dSLubomir Rintel 	controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
167451eea52dSLubomir Rintel 	controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
167551eea52dSLubomir Rintel 	controller->auto_runtime_pm = true;
167651eea52dSLubomir Rintel 	controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1677ca632f55SGrant Likely 
1678ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1679ca632f55SGrant Likely 
1680ca632f55SGrant Likely 	drv_data->ioaddr = ssp->mmio_base;
1681ca632f55SGrant Likely 	drv_data->ssdr_physical = ssp->phys_base + SSDR;
1682ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1683e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1684e5262d05SWeike Chen 		case QUARK_X1000_SSP:
168551eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1686e5262d05SWeike Chen 			break;
1687e5262d05SWeike Chen 		default:
168851eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1689e5262d05SWeike Chen 			break;
1690e5262d05SWeike Chen 		}
1691e5262d05SWeike Chen 
1692ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1693ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1694ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1695ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1696ca632f55SGrant Likely 	} else {
169751eea52dSLubomir Rintel 		controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1698ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
16995928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1700ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1701ec93cb6fSLubomir Rintel 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1702ec93cb6fSLubomir Rintel 						| SSSR_ROR | SSSR_TUR;
1703ca632f55SGrant Likely 	}
1704ca632f55SGrant Likely 
1705ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1706ca632f55SGrant Likely 			drv_data);
1707ca632f55SGrant Likely 	if (status < 0) {
1708ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
170951eea52dSLubomir Rintel 		goto out_error_controller_alloc;
1710ca632f55SGrant Likely 	}
1711ca632f55SGrant Likely 
1712ca632f55SGrant Likely 	/* Setup DMA if requested */
1713ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1714cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1715cd7bed00SMika Westerberg 		if (status) {
17168b57b11bSFlavio Suligoi 			dev_warn(dev, "no DMA channels available, using PIO\n");
1717cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1718b6ced294SJarkko Nikula 		} else {
171951eea52dSLubomir Rintel 			controller->can_dma = pxa2xx_spi_can_dma;
1720bf9f742cSMark Brown 			controller->max_dma_len = MAX_DMA_LEN;
1721ca632f55SGrant Likely 		}
1722ca632f55SGrant Likely 	}
1723ca632f55SGrant Likely 
1724ca632f55SGrant Likely 	/* Enable SOC clock */
172562bbc864STobias Jordan 	status = clk_prepare_enable(ssp->clk);
172662bbc864STobias Jordan 	if (status)
172762bbc864STobias Jordan 		goto out_error_dma_irq_alloc;
17283343b7a6SMika Westerberg 
172951eea52dSLubomir Rintel 	controller->max_speed_hz = clk_get_rate(ssp->clk);
173023cdddb2SJarkko Nikula 	/*
173123cdddb2SJarkko Nikula 	 * Set minimum speed for all other platforms than Intel Quark which is
173223cdddb2SJarkko Nikula 	 * able do under 1 Hz transfers.
173323cdddb2SJarkko Nikula 	 */
173423cdddb2SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
173523cdddb2SJarkko Nikula 		controller->min_speed_hz =
173623cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 4096);
173723cdddb2SJarkko Nikula 	else if (!is_quark_x1000_ssp(drv_data))
173823cdddb2SJarkko Nikula 		controller->min_speed_hz =
173923cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 512);
1740ca632f55SGrant Likely 
1741ca632f55SGrant Likely 	/* Load default SSP configuration */
1742c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
1743e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1744e5262d05SWeike Chen 	case QUARK_X1000_SSP:
17457c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
17467c7289a4SAndy Shevchenko 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1747c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1748e5262d05SWeike Chen 
1749e5262d05SWeike Chen 		/* using the Motorola SPI protocol and use 8 bit frame */
17507c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
17517c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1752e5262d05SWeike Chen 		break;
17537c7289a4SAndy Shevchenko 	case CE4100_SSP:
17547c7289a4SAndy Shevchenko 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
17557c7289a4SAndy Shevchenko 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
17567c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
17577c7289a4SAndy Shevchenko 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
17587c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1759a2dd8af0SAndy Shevchenko 		break;
1760e5262d05SWeike Chen 	default:
1761ec93cb6fSLubomir Rintel 
176251eea52dSLubomir Rintel 		if (spi_controller_is_slave(controller)) {
1763ec93cb6fSLubomir Rintel 			tmp = SSCR1_SCFR |
1764ec93cb6fSLubomir Rintel 			      SSCR1_SCLKDIR |
1765ec93cb6fSLubomir Rintel 			      SSCR1_SFRMDIR |
1766ec93cb6fSLubomir Rintel 			      SSCR1_RxTresh(2) |
1767ec93cb6fSLubomir Rintel 			      SSCR1_TxTresh(1) |
1768ec93cb6fSLubomir Rintel 			      SSCR1_SPH;
1769ec93cb6fSLubomir Rintel 		} else {
1770c039dd27SJarkko Nikula 			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1771c039dd27SJarkko Nikula 			      SSCR1_TxTresh(TX_THRESH_DFLT);
1772ec93cb6fSLubomir Rintel 		}
1773c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1774ec93cb6fSLubomir Rintel 		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
177551eea52dSLubomir Rintel 		if (!spi_controller_is_slave(controller))
1776ec93cb6fSLubomir Rintel 			tmp |= SSCR0_SCR(2);
1777c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1778e5262d05SWeike Chen 		break;
1779e5262d05SWeike Chen 	}
1780e5262d05SWeike Chen 
1781ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1782c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1783e5262d05SWeike Chen 
1784e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1785c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1786ca632f55SGrant Likely 
17878b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
17888b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
17898b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
17908b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
17918b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
17928b136baaSJarkko Nikula 						   config->reg_capabilities);
17938b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
17948b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
17958b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
179630f3a6abSMika Westerberg 		} else if (config->cs_num) {
179730f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
17988b136baaSJarkko Nikula 		}
17998b136baaSJarkko Nikula 	}
180051eea52dSLubomir Rintel 	controller->num_chipselect = platform_info->num_chipselect;
18018b136baaSJarkko Nikula 
180299f499cdSMika Westerberg 	count = gpiod_count(&pdev->dev, "cs");
18036ac5a435SAndy Shevchenko 	if (count > 0) {
18046ac5a435SAndy Shevchenko 		int i;
18056ac5a435SAndy Shevchenko 
180651eea52dSLubomir Rintel 		controller->num_chipselect = max_t(int, count,
180751eea52dSLubomir Rintel 			controller->num_chipselect);
180899f499cdSMika Westerberg 
18096ac5a435SAndy Shevchenko 		drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
181051eea52dSLubomir Rintel 			controller->num_chipselect, sizeof(struct gpio_desc *),
18116ac5a435SAndy Shevchenko 			GFP_KERNEL);
18126ac5a435SAndy Shevchenko 		if (!drv_data->cs_gpiods) {
18136ac5a435SAndy Shevchenko 			status = -ENOMEM;
18146ac5a435SAndy Shevchenko 			goto out_error_clock_enabled;
18156ac5a435SAndy Shevchenko 		}
18166ac5a435SAndy Shevchenko 
181751eea52dSLubomir Rintel 		for (i = 0; i < controller->num_chipselect; i++) {
18186ac5a435SAndy Shevchenko 			struct gpio_desc *gpiod;
18196ac5a435SAndy Shevchenko 
1820d35f2dc9SAndy Shevchenko 			gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
18216ac5a435SAndy Shevchenko 			if (IS_ERR(gpiod)) {
18226ac5a435SAndy Shevchenko 				/* Means use native chip select */
18236ac5a435SAndy Shevchenko 				if (PTR_ERR(gpiod) == -ENOENT)
18246ac5a435SAndy Shevchenko 					continue;
18256ac5a435SAndy Shevchenko 
182677d33897SLubomir Rintel 				status = PTR_ERR(gpiod);
18276ac5a435SAndy Shevchenko 				goto out_error_clock_enabled;
18286ac5a435SAndy Shevchenko 			} else {
18296ac5a435SAndy Shevchenko 				drv_data->cs_gpiods[i] = gpiod;
18306ac5a435SAndy Shevchenko 			}
18316ac5a435SAndy Shevchenko 		}
18326ac5a435SAndy Shevchenko 	}
18336ac5a435SAndy Shevchenko 
183477d33897SLubomir Rintel 	if (platform_info->is_slave) {
183577d33897SLubomir Rintel 		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
183677d33897SLubomir Rintel 						"ready", GPIOD_OUT_LOW);
183777d33897SLubomir Rintel 		if (IS_ERR(drv_data->gpiod_ready)) {
183877d33897SLubomir Rintel 			status = PTR_ERR(drv_data->gpiod_ready);
183977d33897SLubomir Rintel 			goto out_error_clock_enabled;
184077d33897SLubomir Rintel 		}
184177d33897SLubomir Rintel 	}
184277d33897SLubomir Rintel 
1843836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1844836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1845836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1846836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1847836d1a22SAntonio Ospite 
1848ca632f55SGrant Likely 	/* Register with the SPI framework */
1849ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
185051eea52dSLubomir Rintel 	status = devm_spi_register_controller(&pdev->dev, controller);
1851ca632f55SGrant Likely 	if (status != 0) {
185251eea52dSLubomir Rintel 		dev_err(&pdev->dev, "problem registering spi controller\n");
185312742045SLubomir Rintel 		goto out_error_pm_runtime_enabled;
1854ca632f55SGrant Likely 	}
1855ca632f55SGrant Likely 
1856ca632f55SGrant Likely 	return status;
1857ca632f55SGrant Likely 
185812742045SLubomir Rintel out_error_pm_runtime_enabled:
1859e2b714afSJarkko Nikula 	pm_runtime_put_noidle(&pdev->dev);
1860e2b714afSJarkko Nikula 	pm_runtime_disable(&pdev->dev);
186112742045SLubomir Rintel 
186212742045SLubomir Rintel out_error_clock_enabled:
18633343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
186462bbc864STobias Jordan 
186562bbc864STobias Jordan out_error_dma_irq_alloc:
1866cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1867ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1868ca632f55SGrant Likely 
186951eea52dSLubomir Rintel out_error_controller_alloc:
187051eea52dSLubomir Rintel 	spi_controller_put(controller);
1871ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1872ca632f55SGrant Likely 	return status;
1873ca632f55SGrant Likely }
1874ca632f55SGrant Likely 
1875ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1876ca632f55SGrant Likely {
1877ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
1878ca632f55SGrant Likely 	struct ssp_device *ssp;
1879ca632f55SGrant Likely 
1880ca632f55SGrant Likely 	if (!drv_data)
1881ca632f55SGrant Likely 		return 0;
1882ca632f55SGrant Likely 	ssp = drv_data->ssp;
1883ca632f55SGrant Likely 
18847d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
18857d94a505SMika Westerberg 
1886ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
1887c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
18883343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1889ca632f55SGrant Likely 
1890ca632f55SGrant Likely 	/* Release DMA */
189151eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma)
1892cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1893ca632f55SGrant Likely 
18947d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
18957d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
18967d94a505SMika Westerberg 
1897ca632f55SGrant Likely 	/* Release IRQ */
1898ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1899ca632f55SGrant Likely 
1900ca632f55SGrant Likely 	/* Release SSP */
1901ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1902ca632f55SGrant Likely 
1903ca632f55SGrant Likely 	return 0;
1904ca632f55SGrant Likely }
1905ca632f55SGrant Likely 
1906382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP
1907ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1908ca632f55SGrant Likely {
1909ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1910ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1911bffc967eSJarkko Nikula 	int status;
1912ca632f55SGrant Likely 
191351eea52dSLubomir Rintel 	status = spi_controller_suspend(drv_data->controller);
1914ca632f55SGrant Likely 	if (status != 0)
1915ca632f55SGrant Likely 		return status;
1916c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
19172b9375b9SDmitry Eremin-Solenikov 
19182b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
19193343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1920ca632f55SGrant Likely 
1921ca632f55SGrant Likely 	return 0;
1922ca632f55SGrant Likely }
1923ca632f55SGrant Likely 
1924ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1925ca632f55SGrant Likely {
1926ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1927ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1928bffc967eSJarkko Nikula 	int status;
1929ca632f55SGrant Likely 
1930ca632f55SGrant Likely 	/* Enable the SSP clock */
193162bbc864STobias Jordan 	if (!pm_runtime_suspended(dev)) {
193262bbc864STobias Jordan 		status = clk_prepare_enable(ssp->clk);
193362bbc864STobias Jordan 		if (status)
193462bbc864STobias Jordan 			return status;
193562bbc864STobias Jordan 	}
1936ca632f55SGrant Likely 
1937ca632f55SGrant Likely 	/* Start the queue running */
193851eea52dSLubomir Rintel 	return spi_controller_resume(drv_data->controller);
1939ca632f55SGrant Likely }
19407d94a505SMika Westerberg #endif
19417d94a505SMika Westerberg 
1942ec833050SRafael J. Wysocki #ifdef CONFIG_PM
19437d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
19447d94a505SMika Westerberg {
19457d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
19467d94a505SMika Westerberg 
19477d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
19487d94a505SMika Westerberg 	return 0;
19497d94a505SMika Westerberg }
19507d94a505SMika Westerberg 
19517d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
19527d94a505SMika Westerberg {
19537d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
195462bbc864STobias Jordan 	int status;
19557d94a505SMika Westerberg 
195662bbc864STobias Jordan 	status = clk_prepare_enable(drv_data->ssp->clk);
195762bbc864STobias Jordan 	return status;
19587d94a505SMika Westerberg }
19597d94a505SMika Westerberg #endif
1960ca632f55SGrant Likely 
1961ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
19627d94a505SMika Westerberg 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
19637d94a505SMika Westerberg 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
19647d94a505SMika Westerberg 			   pxa2xx_spi_runtime_resume, NULL)
1965ca632f55SGrant Likely };
1966ca632f55SGrant Likely 
1967ca632f55SGrant Likely static struct platform_driver driver = {
1968ca632f55SGrant Likely 	.driver = {
1969ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
1970ca632f55SGrant Likely 		.pm	= &pxa2xx_spi_pm_ops,
1971a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
197287ae1d2dSLubomir Rintel 		.of_match_table = of_match_ptr(pxa2xx_spi_of_match),
1973ca632f55SGrant Likely 	},
1974ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
1975ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
1976ca632f55SGrant Likely };
1977ca632f55SGrant Likely 
1978ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
1979ca632f55SGrant Likely {
1980ca632f55SGrant Likely 	return platform_driver_register(&driver);
1981ca632f55SGrant Likely }
1982ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
1983ca632f55SGrant Likely 
1984ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
1985ca632f55SGrant Likely {
1986ca632f55SGrant Likely 	platform_driver_unregister(&driver);
1987ca632f55SGrant Likely }
1988ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
198951ebf6acSFlavio Suligoi 
199051ebf6acSFlavio Suligoi MODULE_SOFTDEP("pre: dw_dmac");
1991