xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision 22d71a5097ec7059b6cbbee678a4f88484695941)
1ca632f55SGrant Likely /*
2ca632f55SGrant Likely  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
3a0d2642eSMika Westerberg  * Copyright (C) 2013, Intel Corporation
4ca632f55SGrant Likely  *
5ca632f55SGrant Likely  * This program is free software; you can redistribute it and/or modify
6ca632f55SGrant Likely  * it under the terms of the GNU General Public License as published by
7ca632f55SGrant Likely  * the Free Software Foundation; either version 2 of the License, or
8ca632f55SGrant Likely  * (at your option) any later version.
9ca632f55SGrant Likely  *
10ca632f55SGrant Likely  * This program is distributed in the hope that it will be useful,
11ca632f55SGrant Likely  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ca632f55SGrant Likely  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13ca632f55SGrant Likely  * GNU General Public License for more details.
14ca632f55SGrant Likely  */
15ca632f55SGrant Likely 
168b136baaSJarkko Nikula #include <linux/bitops.h>
17ca632f55SGrant Likely #include <linux/init.h>
18ca632f55SGrant Likely #include <linux/module.h>
19ca632f55SGrant Likely #include <linux/device.h>
20ca632f55SGrant Likely #include <linux/ioport.h>
21ca632f55SGrant Likely #include <linux/errno.h>
22cbfd6a21SSachin Kamat #include <linux/err.h>
23ca632f55SGrant Likely #include <linux/interrupt.h>
249df461ecSAndy Shevchenko #include <linux/kernel.h>
2534cadd9cSJarkko Nikula #include <linux/pci.h>
26ca632f55SGrant Likely #include <linux/platform_device.h>
27ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h>
28ca632f55SGrant Likely #include <linux/spi/spi.h>
29ca632f55SGrant Likely #include <linux/delay.h>
30ca632f55SGrant Likely #include <linux/gpio.h>
31089bd46dSMika Westerberg #include <linux/gpio/consumer.h>
32ca632f55SGrant Likely #include <linux/slab.h>
333343b7a6SMika Westerberg #include <linux/clk.h>
347d94a505SMika Westerberg #include <linux/pm_runtime.h>
35a3496855SMika Westerberg #include <linux/acpi.h>
36ca632f55SGrant Likely 
37cd7bed00SMika Westerberg #include "spi-pxa2xx.h"
38ca632f55SGrant Likely 
39ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street");
40ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
41ca632f55SGrant Likely MODULE_LICENSE("GPL");
42ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi");
43ca632f55SGrant Likely 
44ca632f55SGrant Likely #define TIMOUT_DFLT		1000
45ca632f55SGrant Likely 
46ca632f55SGrant Likely /*
47ca632f55SGrant Likely  * for testing SSCR1 changes that require SSP restart, basically
48ca632f55SGrant Likely  * everything except the service and interrupt enables, the pxa270 developer
49ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
50ca632f55SGrant Likely  * list, but the PXA255 dev man says all bits without really meaning the
51ca632f55SGrant Likely  * service and interrupt enables
52ca632f55SGrant Likely  */
53ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
54ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
55ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
56ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
57ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
58ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
59ca632f55SGrant Likely 
60e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
61e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
62e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
63e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
64e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
65e5262d05SWeike Chen 
667c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
677c7289a4SAndy Shevchenko 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
687c7289a4SAndy Shevchenko 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
697c7289a4SAndy Shevchenko 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
707c7289a4SAndy Shevchenko 				| CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
717c7289a4SAndy Shevchenko 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
727c7289a4SAndy Shevchenko 
73624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
74624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE			BIT(0)
75624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
768b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT			9
778b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
78a0d2642eSMika Westerberg 
79dccf7369SJarkko Nikula struct lpss_config {
80dccf7369SJarkko Nikula 	/* LPSS offset from drv_data->ioaddr */
81dccf7369SJarkko Nikula 	unsigned offset;
82dccf7369SJarkko Nikula 	/* Register offsets from drv_data->lpss_base or -1 */
83dccf7369SJarkko Nikula 	int reg_general;
84dccf7369SJarkko Nikula 	int reg_ssp;
85dccf7369SJarkko Nikula 	int reg_cs_ctrl;
868b136baaSJarkko Nikula 	int reg_capabilities;
87dccf7369SJarkko Nikula 	/* FIFO thresholds */
88dccf7369SJarkko Nikula 	u32 rx_threshold;
89dccf7369SJarkko Nikula 	u32 tx_threshold_lo;
90dccf7369SJarkko Nikula 	u32 tx_threshold_hi;
91c1e4a53cSMika Westerberg 	/* Chip select control */
92c1e4a53cSMika Westerberg 	unsigned cs_sel_shift;
93c1e4a53cSMika Westerberg 	unsigned cs_sel_mask;
9430f3a6abSMika Westerberg 	unsigned cs_num;
95dccf7369SJarkko Nikula };
96dccf7369SJarkko Nikula 
97dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */
98dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = {
99dccf7369SJarkko Nikula 	{	/* LPSS_LPT_SSP */
100dccf7369SJarkko Nikula 		.offset = 0x800,
101dccf7369SJarkko Nikula 		.reg_general = 0x08,
102dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
103dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1048b136baaSJarkko Nikula 		.reg_capabilities = -1,
105dccf7369SJarkko Nikula 		.rx_threshold = 64,
106dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
107dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
108dccf7369SJarkko Nikula 	},
109dccf7369SJarkko Nikula 	{	/* LPSS_BYT_SSP */
110dccf7369SJarkko Nikula 		.offset = 0x400,
111dccf7369SJarkko Nikula 		.reg_general = 0x08,
112dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
113dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1148b136baaSJarkko Nikula 		.reg_capabilities = -1,
115dccf7369SJarkko Nikula 		.rx_threshold = 64,
116dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
117dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
118dccf7369SJarkko Nikula 	},
11930f3a6abSMika Westerberg 	{	/* LPSS_BSW_SSP */
12030f3a6abSMika Westerberg 		.offset = 0x400,
12130f3a6abSMika Westerberg 		.reg_general = 0x08,
12230f3a6abSMika Westerberg 		.reg_ssp = 0x0c,
12330f3a6abSMika Westerberg 		.reg_cs_ctrl = 0x18,
12430f3a6abSMika Westerberg 		.reg_capabilities = -1,
12530f3a6abSMika Westerberg 		.rx_threshold = 64,
12630f3a6abSMika Westerberg 		.tx_threshold_lo = 160,
12730f3a6abSMika Westerberg 		.tx_threshold_hi = 224,
12830f3a6abSMika Westerberg 		.cs_sel_shift = 2,
12930f3a6abSMika Westerberg 		.cs_sel_mask = 1 << 2,
13030f3a6abSMika Westerberg 		.cs_num = 2,
13130f3a6abSMika Westerberg 	},
13234cadd9cSJarkko Nikula 	{	/* LPSS_SPT_SSP */
13334cadd9cSJarkko Nikula 		.offset = 0x200,
13434cadd9cSJarkko Nikula 		.reg_general = -1,
13534cadd9cSJarkko Nikula 		.reg_ssp = 0x20,
13634cadd9cSJarkko Nikula 		.reg_cs_ctrl = 0x24,
13766ec246eSJarkko Nikula 		.reg_capabilities = -1,
13834cadd9cSJarkko Nikula 		.rx_threshold = 1,
13934cadd9cSJarkko Nikula 		.tx_threshold_lo = 32,
14034cadd9cSJarkko Nikula 		.tx_threshold_hi = 56,
14134cadd9cSJarkko Nikula 	},
142b7c08cf8SJarkko Nikula 	{	/* LPSS_BXT_SSP */
143b7c08cf8SJarkko Nikula 		.offset = 0x200,
144b7c08cf8SJarkko Nikula 		.reg_general = -1,
145b7c08cf8SJarkko Nikula 		.reg_ssp = 0x20,
146b7c08cf8SJarkko Nikula 		.reg_cs_ctrl = 0x24,
147b7c08cf8SJarkko Nikula 		.reg_capabilities = 0xfc,
148b7c08cf8SJarkko Nikula 		.rx_threshold = 1,
149b7c08cf8SJarkko Nikula 		.tx_threshold_lo = 16,
150b7c08cf8SJarkko Nikula 		.tx_threshold_hi = 48,
151c1e4a53cSMika Westerberg 		.cs_sel_shift = 8,
152c1e4a53cSMika Westerberg 		.cs_sel_mask = 3 << 8,
153b7c08cf8SJarkko Nikula 	},
154fc0b2accSJarkko Nikula 	{	/* LPSS_CNL_SSP */
155fc0b2accSJarkko Nikula 		.offset = 0x200,
156fc0b2accSJarkko Nikula 		.reg_general = -1,
157fc0b2accSJarkko Nikula 		.reg_ssp = 0x20,
158fc0b2accSJarkko Nikula 		.reg_cs_ctrl = 0x24,
159fc0b2accSJarkko Nikula 		.reg_capabilities = 0xfc,
160fc0b2accSJarkko Nikula 		.rx_threshold = 1,
161fc0b2accSJarkko Nikula 		.tx_threshold_lo = 32,
162fc0b2accSJarkko Nikula 		.tx_threshold_hi = 56,
163fc0b2accSJarkko Nikula 		.cs_sel_shift = 8,
164fc0b2accSJarkko Nikula 		.cs_sel_mask = 3 << 8,
165fc0b2accSJarkko Nikula 	},
166dccf7369SJarkko Nikula };
167dccf7369SJarkko Nikula 
168dccf7369SJarkko Nikula static inline const struct lpss_config
169dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data)
170dccf7369SJarkko Nikula {
171dccf7369SJarkko Nikula 	return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
172dccf7369SJarkko Nikula }
173dccf7369SJarkko Nikula 
174a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data)
175a0d2642eSMika Westerberg {
17603fbf488SJarkko Nikula 	switch (drv_data->ssp_type) {
17703fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
17803fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
17930f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
18034cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
181b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
182fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
18303fbf488SJarkko Nikula 		return true;
18403fbf488SJarkko Nikula 	default:
18503fbf488SJarkko Nikula 		return false;
18603fbf488SJarkko Nikula 	}
187a0d2642eSMika Westerberg }
188a0d2642eSMika Westerberg 
189e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
190e5262d05SWeike Chen {
191e5262d05SWeike Chen 	return drv_data->ssp_type == QUARK_X1000_SSP;
192e5262d05SWeike Chen }
193e5262d05SWeike Chen 
1944fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
1954fdb2424SWeike Chen {
1964fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
197e5262d05SWeike Chen 	case QUARK_X1000_SSP:
198e5262d05SWeike Chen 		return QUARK_X1000_SSCR1_CHANGE_MASK;
1997c7289a4SAndy Shevchenko 	case CE4100_SSP:
2007c7289a4SAndy Shevchenko 		return CE4100_SSCR1_CHANGE_MASK;
2014fdb2424SWeike Chen 	default:
2024fdb2424SWeike Chen 		return SSCR1_CHANGE_MASK;
2034fdb2424SWeike Chen 	}
2044fdb2424SWeike Chen }
2054fdb2424SWeike Chen 
2064fdb2424SWeike Chen static u32
2074fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
2084fdb2424SWeike Chen {
2094fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
210e5262d05SWeike Chen 	case QUARK_X1000_SSP:
211e5262d05SWeike Chen 		return RX_THRESH_QUARK_X1000_DFLT;
2127c7289a4SAndy Shevchenko 	case CE4100_SSP:
2137c7289a4SAndy Shevchenko 		return RX_THRESH_CE4100_DFLT;
2144fdb2424SWeike Chen 	default:
2154fdb2424SWeike Chen 		return RX_THRESH_DFLT;
2164fdb2424SWeike Chen 	}
2174fdb2424SWeike Chen }
2184fdb2424SWeike Chen 
2194fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
2204fdb2424SWeike Chen {
2214fdb2424SWeike Chen 	u32 mask;
2224fdb2424SWeike Chen 
2234fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
224e5262d05SWeike Chen 	case QUARK_X1000_SSP:
225e5262d05SWeike Chen 		mask = QUARK_X1000_SSSR_TFL_MASK;
226e5262d05SWeike Chen 		break;
2277c7289a4SAndy Shevchenko 	case CE4100_SSP:
2287c7289a4SAndy Shevchenko 		mask = CE4100_SSSR_TFL_MASK;
2297c7289a4SAndy Shevchenko 		break;
2304fdb2424SWeike Chen 	default:
2314fdb2424SWeike Chen 		mask = SSSR_TFL_MASK;
2324fdb2424SWeike Chen 		break;
2334fdb2424SWeike Chen 	}
2344fdb2424SWeike Chen 
235c039dd27SJarkko Nikula 	return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
2364fdb2424SWeike Chen }
2374fdb2424SWeike Chen 
2384fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
2394fdb2424SWeike Chen 				     u32 *sccr1_reg)
2404fdb2424SWeike Chen {
2414fdb2424SWeike Chen 	u32 mask;
2424fdb2424SWeike Chen 
2434fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
244e5262d05SWeike Chen 	case QUARK_X1000_SSP:
245e5262d05SWeike Chen 		mask = QUARK_X1000_SSCR1_RFT;
246e5262d05SWeike Chen 		break;
2477c7289a4SAndy Shevchenko 	case CE4100_SSP:
2487c7289a4SAndy Shevchenko 		mask = CE4100_SSCR1_RFT;
2497c7289a4SAndy Shevchenko 		break;
2504fdb2424SWeike Chen 	default:
2514fdb2424SWeike Chen 		mask = SSCR1_RFT;
2524fdb2424SWeike Chen 		break;
2534fdb2424SWeike Chen 	}
2544fdb2424SWeike Chen 	*sccr1_reg &= ~mask;
2554fdb2424SWeike Chen }
2564fdb2424SWeike Chen 
2574fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
2584fdb2424SWeike Chen 				   u32 *sccr1_reg, u32 threshold)
2594fdb2424SWeike Chen {
2604fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
261e5262d05SWeike Chen 	case QUARK_X1000_SSP:
262e5262d05SWeike Chen 		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
263e5262d05SWeike Chen 		break;
2647c7289a4SAndy Shevchenko 	case CE4100_SSP:
2657c7289a4SAndy Shevchenko 		*sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
2667c7289a4SAndy Shevchenko 		break;
2674fdb2424SWeike Chen 	default:
2684fdb2424SWeike Chen 		*sccr1_reg |= SSCR1_RxTresh(threshold);
2694fdb2424SWeike Chen 		break;
2704fdb2424SWeike Chen 	}
2714fdb2424SWeike Chen }
2724fdb2424SWeike Chen 
2734fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
2744fdb2424SWeike Chen 				  u32 clk_div, u8 bits)
2754fdb2424SWeike Chen {
2764fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
277e5262d05SWeike Chen 	case QUARK_X1000_SSP:
278e5262d05SWeike Chen 		return clk_div
279e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_Motorola
280e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
281e5262d05SWeike Chen 			| SSCR0_SSE;
2824fdb2424SWeike Chen 	default:
2834fdb2424SWeike Chen 		return clk_div
2844fdb2424SWeike Chen 			| SSCR0_Motorola
2854fdb2424SWeike Chen 			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
2864fdb2424SWeike Chen 			| SSCR0_SSE
2874fdb2424SWeike Chen 			| (bits > 16 ? SSCR0_EDSS : 0);
2884fdb2424SWeike Chen 	}
2894fdb2424SWeike Chen }
2904fdb2424SWeike Chen 
291a0d2642eSMika Westerberg /*
292a0d2642eSMika Westerberg  * Read and write LPSS SSP private registers. Caller must first check that
293a0d2642eSMika Westerberg  * is_lpss_ssp() returns true before these can be called.
294a0d2642eSMika Westerberg  */
295a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
296a0d2642eSMika Westerberg {
297a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
298a0d2642eSMika Westerberg 	return readl(drv_data->lpss_base + offset);
299a0d2642eSMika Westerberg }
300a0d2642eSMika Westerberg 
301a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data,
302a0d2642eSMika Westerberg 				  unsigned offset, u32 value)
303a0d2642eSMika Westerberg {
304a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
305a0d2642eSMika Westerberg 	writel(value, drv_data->lpss_base + offset);
306a0d2642eSMika Westerberg }
307a0d2642eSMika Westerberg 
308a0d2642eSMika Westerberg /*
309a0d2642eSMika Westerberg  * lpss_ssp_setup - perform LPSS SSP specific setup
310a0d2642eSMika Westerberg  * @drv_data: pointer to the driver private data
311a0d2642eSMika Westerberg  *
312a0d2642eSMika Westerberg  * Perform LPSS SSP specific setup. This function must be called first if
313a0d2642eSMika Westerberg  * one is going to use LPSS SSP private registers.
314a0d2642eSMika Westerberg  */
315a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data)
316a0d2642eSMika Westerberg {
317dccf7369SJarkko Nikula 	const struct lpss_config *config;
318dccf7369SJarkko Nikula 	u32 value;
319a0d2642eSMika Westerberg 
320dccf7369SJarkko Nikula 	config = lpss_get_config(drv_data);
321dccf7369SJarkko Nikula 	drv_data->lpss_base = drv_data->ioaddr + config->offset;
322a0d2642eSMika Westerberg 
323a0d2642eSMika Westerberg 	/* Enable software chip select control */
3240e897218SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
325624ea72eSJarkko Nikula 	value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
326624ea72eSJarkko Nikula 	value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
327dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
3280054e28dSMika Westerberg 
3290054e28dSMika Westerberg 	/* Enable multiblock DMA transfers */
3301de70612SMika Westerberg 	if (drv_data->master_info->enable_dma) {
331dccf7369SJarkko Nikula 		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
3321de70612SMika Westerberg 
33382ba2c2aSJarkko Nikula 		if (config->reg_general >= 0) {
33482ba2c2aSJarkko Nikula 			value = __lpss_ssp_read_priv(drv_data,
33582ba2c2aSJarkko Nikula 						     config->reg_general);
336624ea72eSJarkko Nikula 			value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
33782ba2c2aSJarkko Nikula 			__lpss_ssp_write_priv(drv_data,
33882ba2c2aSJarkko Nikula 					      config->reg_general, value);
33982ba2c2aSJarkko Nikula 		}
3401de70612SMika Westerberg 	}
341a0d2642eSMika Westerberg }
342a0d2642eSMika Westerberg 
343d5898e19SJarkko Nikula static void lpss_ssp_select_cs(struct spi_device *spi,
344c1e4a53cSMika Westerberg 			       const struct lpss_config *config)
345a0d2642eSMika Westerberg {
346d5898e19SJarkko Nikula 	struct driver_data *drv_data =
347d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
348d0283eb2SJarkko Nikula 	u32 value, cs;
349a0d2642eSMika Westerberg 
350c1e4a53cSMika Westerberg 	if (!config->cs_sel_mask)
351c1e4a53cSMika Westerberg 		return;
352dccf7369SJarkko Nikula 
353dccf7369SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
354c1e4a53cSMika Westerberg 
355d5898e19SJarkko Nikula 	cs = spi->chip_select;
356c1e4a53cSMika Westerberg 	cs <<= config->cs_sel_shift;
357c1e4a53cSMika Westerberg 	if (cs != (value & config->cs_sel_mask)) {
358d0283eb2SJarkko Nikula 		/*
359c1e4a53cSMika Westerberg 		 * When switching another chip select output active the
360c1e4a53cSMika Westerberg 		 * output must be selected first and wait 2 ssp_clk cycles
361c1e4a53cSMika Westerberg 		 * before changing state to active. Otherwise a short
362c1e4a53cSMika Westerberg 		 * glitch will occur on the previous chip select since
363c1e4a53cSMika Westerberg 		 * output select is latched but state control is not.
364d0283eb2SJarkko Nikula 		 */
365c1e4a53cSMika Westerberg 		value &= ~config->cs_sel_mask;
366d0283eb2SJarkko Nikula 		value |= cs;
367d0283eb2SJarkko Nikula 		__lpss_ssp_write_priv(drv_data,
368d0283eb2SJarkko Nikula 				      config->reg_cs_ctrl, value);
369d0283eb2SJarkko Nikula 		ndelay(1000000000 /
370d0283eb2SJarkko Nikula 		       (drv_data->master->max_speed_hz / 2));
371d0283eb2SJarkko Nikula 	}
372d0283eb2SJarkko Nikula }
373c1e4a53cSMika Westerberg 
374d5898e19SJarkko Nikula static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
375c1e4a53cSMika Westerberg {
376d5898e19SJarkko Nikula 	struct driver_data *drv_data =
377d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
378c1e4a53cSMika Westerberg 	const struct lpss_config *config;
379c1e4a53cSMika Westerberg 	u32 value;
380c1e4a53cSMika Westerberg 
381c1e4a53cSMika Westerberg 	config = lpss_get_config(drv_data);
382c1e4a53cSMika Westerberg 
383c1e4a53cSMika Westerberg 	if (enable)
384d5898e19SJarkko Nikula 		lpss_ssp_select_cs(spi, config);
385c1e4a53cSMika Westerberg 
386c1e4a53cSMika Westerberg 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
387c1e4a53cSMika Westerberg 	if (enable)
388c1e4a53cSMika Westerberg 		value &= ~LPSS_CS_CONTROL_CS_HIGH;
389c1e4a53cSMika Westerberg 	else
390c1e4a53cSMika Westerberg 		value |= LPSS_CS_CONTROL_CS_HIGH;
391dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
392a0d2642eSMika Westerberg }
393a0d2642eSMika Westerberg 
394d5898e19SJarkko Nikula static void cs_assert(struct spi_device *spi)
395ca632f55SGrant Likely {
396d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
397d5898e19SJarkko Nikula 	struct driver_data *drv_data =
398d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
399ca632f55SGrant Likely 
400ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP) {
40196579a4eSJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, chip->frm);
402ca632f55SGrant Likely 		return;
403ca632f55SGrant Likely 	}
404ca632f55SGrant Likely 
405ca632f55SGrant Likely 	if (chip->cs_control) {
406ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_ASSERT);
407ca632f55SGrant Likely 		return;
408ca632f55SGrant Likely 	}
409ca632f55SGrant Likely 
410c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
411c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
412a0d2642eSMika Westerberg 		return;
413a0d2642eSMika Westerberg 	}
414a0d2642eSMika Westerberg 
4157566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
416d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, true);
417ca632f55SGrant Likely }
418ca632f55SGrant Likely 
419d5898e19SJarkko Nikula static void cs_deassert(struct spi_device *spi)
420ca632f55SGrant Likely {
421d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
422d5898e19SJarkko Nikula 	struct driver_data *drv_data =
423d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
424104e51afSJarkko Nikula 	unsigned long timeout;
425ca632f55SGrant Likely 
426ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
427ca632f55SGrant Likely 		return;
428ca632f55SGrant Likely 
429104e51afSJarkko Nikula 	/* Wait until SSP becomes idle before deasserting the CS */
430104e51afSJarkko Nikula 	timeout = jiffies + msecs_to_jiffies(10);
431104e51afSJarkko Nikula 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
432104e51afSJarkko Nikula 	       !time_after(jiffies, timeout))
433104e51afSJarkko Nikula 		cpu_relax();
434104e51afSJarkko Nikula 
435ca632f55SGrant Likely 	if (chip->cs_control) {
436ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_DEASSERT);
437ca632f55SGrant Likely 		return;
438ca632f55SGrant Likely 	}
439ca632f55SGrant Likely 
440c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
441c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
442a0d2642eSMika Westerberg 		return;
443a0d2642eSMika Westerberg 	}
444a0d2642eSMika Westerberg 
4457566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
446d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, false);
447d5898e19SJarkko Nikula }
448d5898e19SJarkko Nikula 
449d5898e19SJarkko Nikula static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
450d5898e19SJarkko Nikula {
451d5898e19SJarkko Nikula 	if (level)
452d5898e19SJarkko Nikula 		cs_deassert(spi);
453d5898e19SJarkko Nikula 	else
454d5898e19SJarkko Nikula 		cs_assert(spi);
455ca632f55SGrant Likely }
456ca632f55SGrant Likely 
457cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
458ca632f55SGrant Likely {
459ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
460ca632f55SGrant Likely 
461ca632f55SGrant Likely 	do {
462c039dd27SJarkko Nikula 		while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
463c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
464c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
465ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
466ca632f55SGrant Likely 
467ca632f55SGrant Likely 	return limit;
468ca632f55SGrant Likely }
469ca632f55SGrant Likely 
470ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
471ca632f55SGrant Likely {
472ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
473ca632f55SGrant Likely 
4744fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
475ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
476ca632f55SGrant Likely 		return 0;
477ca632f55SGrant Likely 
478c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
479ca632f55SGrant Likely 	drv_data->tx += n_bytes;
480ca632f55SGrant Likely 
481ca632f55SGrant Likely 	return 1;
482ca632f55SGrant Likely }
483ca632f55SGrant Likely 
484ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
485ca632f55SGrant Likely {
486ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
487ca632f55SGrant Likely 
488c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
489ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
490c039dd27SJarkko Nikula 		pxa2xx_spi_read(drv_data, SSDR);
491ca632f55SGrant Likely 		drv_data->rx += n_bytes;
492ca632f55SGrant Likely 	}
493ca632f55SGrant Likely 
494ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
495ca632f55SGrant Likely }
496ca632f55SGrant Likely 
497ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data)
498ca632f55SGrant Likely {
4994fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
500ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
501ca632f55SGrant Likely 		return 0;
502ca632f55SGrant Likely 
503c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
504ca632f55SGrant Likely 	++drv_data->tx;
505ca632f55SGrant Likely 
506ca632f55SGrant Likely 	return 1;
507ca632f55SGrant Likely }
508ca632f55SGrant Likely 
509ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data)
510ca632f55SGrant Likely {
511c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
512ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
513c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
514ca632f55SGrant Likely 		++drv_data->rx;
515ca632f55SGrant Likely 	}
516ca632f55SGrant Likely 
517ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
518ca632f55SGrant Likely }
519ca632f55SGrant Likely 
520ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
521ca632f55SGrant Likely {
5224fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
523ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
524ca632f55SGrant Likely 		return 0;
525ca632f55SGrant Likely 
526c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
527ca632f55SGrant Likely 	drv_data->tx += 2;
528ca632f55SGrant Likely 
529ca632f55SGrant Likely 	return 1;
530ca632f55SGrant Likely }
531ca632f55SGrant Likely 
532ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
533ca632f55SGrant Likely {
534c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
535ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
536c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
537ca632f55SGrant Likely 		drv_data->rx += 2;
538ca632f55SGrant Likely 	}
539ca632f55SGrant Likely 
540ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
541ca632f55SGrant Likely }
542ca632f55SGrant Likely 
543ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
544ca632f55SGrant Likely {
5454fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
546ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
547ca632f55SGrant Likely 		return 0;
548ca632f55SGrant Likely 
549c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
550ca632f55SGrant Likely 	drv_data->tx += 4;
551ca632f55SGrant Likely 
552ca632f55SGrant Likely 	return 1;
553ca632f55SGrant Likely }
554ca632f55SGrant Likely 
555ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
556ca632f55SGrant Likely {
557c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
558ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
559c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
560ca632f55SGrant Likely 		drv_data->rx += 4;
561ca632f55SGrant Likely 	}
562ca632f55SGrant Likely 
563ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
564ca632f55SGrant Likely }
565ca632f55SGrant Likely 
566ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
567ca632f55SGrant Likely {
56896579a4eSJarkko Nikula 	struct chip_data *chip =
56996579a4eSJarkko Nikula 		spi_get_ctldata(drv_data->master->cur_msg->spi);
570ca632f55SGrant Likely 	u32 sccr1_reg;
571ca632f55SGrant Likely 
572c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
573152bc19eSAndy Shevchenko 	switch (drv_data->ssp_type) {
574152bc19eSAndy Shevchenko 	case QUARK_X1000_SSP:
575152bc19eSAndy Shevchenko 		sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
576152bc19eSAndy Shevchenko 		break;
5777c7289a4SAndy Shevchenko 	case CE4100_SSP:
5787c7289a4SAndy Shevchenko 		sccr1_reg &= ~CE4100_SSCR1_RFT;
5797c7289a4SAndy Shevchenko 		break;
580152bc19eSAndy Shevchenko 	default:
581ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_RFT;
582152bc19eSAndy Shevchenko 		break;
583152bc19eSAndy Shevchenko 	}
584ca632f55SGrant Likely 	sccr1_reg |= chip->threshold;
585c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
586ca632f55SGrant Likely }
587ca632f55SGrant Likely 
588ca632f55SGrant Likely static void int_error_stop(struct driver_data *drv_data, const char* msg)
589ca632f55SGrant Likely {
590ca632f55SGrant Likely 	/* Stop and reset SSP */
591ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
592ca632f55SGrant Likely 	reset_sccr1(drv_data);
593ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
594c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
595cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
596c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0,
597c039dd27SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
598ca632f55SGrant Likely 
599ca632f55SGrant Likely 	dev_err(&drv_data->pdev->dev, "%s\n", msg);
600ca632f55SGrant Likely 
601d5898e19SJarkko Nikula 	drv_data->master->cur_msg->status = -EIO;
602d5898e19SJarkko Nikula 	spi_finalize_current_transfer(drv_data->master);
603ca632f55SGrant Likely }
604ca632f55SGrant Likely 
605ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
606ca632f55SGrant Likely {
60707550df0SJarkko Nikula 	/* Clear and disable interrupts */
608ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
609ca632f55SGrant Likely 	reset_sccr1(drv_data);
610ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
611c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
612ca632f55SGrant Likely 
613d5898e19SJarkko Nikula 	spi_finalize_current_transfer(drv_data->master);
614ca632f55SGrant Likely }
615ca632f55SGrant Likely 
616ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
617ca632f55SGrant Likely {
618c039dd27SJarkko Nikula 	u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
619ca632f55SGrant Likely 		       drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
620ca632f55SGrant Likely 
621c039dd27SJarkko Nikula 	u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
622ca632f55SGrant Likely 
623ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
624ca632f55SGrant Likely 		int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
625ca632f55SGrant Likely 		return IRQ_HANDLED;
626ca632f55SGrant Likely 	}
627ca632f55SGrant Likely 
628ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
629c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
630ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
631ca632f55SGrant Likely 			int_transfer_complete(drv_data);
632ca632f55SGrant Likely 			return IRQ_HANDLED;
633ca632f55SGrant Likely 		}
634ca632f55SGrant Likely 	}
635ca632f55SGrant Likely 
636ca632f55SGrant Likely 	/* Drain rx fifo, Fill tx fifo and prevent overruns */
637ca632f55SGrant Likely 	do {
638ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
639ca632f55SGrant Likely 			int_transfer_complete(drv_data);
640ca632f55SGrant Likely 			return IRQ_HANDLED;
641ca632f55SGrant Likely 		}
642ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
643ca632f55SGrant Likely 
644ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
645ca632f55SGrant Likely 		int_transfer_complete(drv_data);
646ca632f55SGrant Likely 		return IRQ_HANDLED;
647ca632f55SGrant Likely 	}
648ca632f55SGrant Likely 
649ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
650ca632f55SGrant Likely 		u32 bytes_left;
651ca632f55SGrant Likely 		u32 sccr1_reg;
652ca632f55SGrant Likely 
653c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
654ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
655ca632f55SGrant Likely 
656ca632f55SGrant Likely 		/*
657ca632f55SGrant Likely 		 * PXA25x_SSP has no timeout, set up rx threshould for the
658ca632f55SGrant Likely 		 * remaining RX bytes.
659ca632f55SGrant Likely 		 */
660ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
6614fdb2424SWeike Chen 			u32 rx_thre;
662ca632f55SGrant Likely 
6634fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
664ca632f55SGrant Likely 
665ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
666ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
667ca632f55SGrant Likely 			case 4:
668ca632f55SGrant Likely 				bytes_left >>= 1;
669ca632f55SGrant Likely 			case 2:
670ca632f55SGrant Likely 				bytes_left >>= 1;
671ca632f55SGrant Likely 			}
672ca632f55SGrant Likely 
6734fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
6744fdb2424SWeike Chen 			if (rx_thre > bytes_left)
6754fdb2424SWeike Chen 				rx_thre = bytes_left;
676ca632f55SGrant Likely 
6774fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
678ca632f55SGrant Likely 		}
679c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
680ca632f55SGrant Likely 	}
681ca632f55SGrant Likely 
682ca632f55SGrant Likely 	/* We did something */
683ca632f55SGrant Likely 	return IRQ_HANDLED;
684ca632f55SGrant Likely }
685ca632f55SGrant Likely 
686b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data)
687b0312482SJan Kiszka {
688b0312482SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR0,
689b0312482SJan Kiszka 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
690b0312482SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1,
691b0312482SJan Kiszka 			 pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
692b0312482SJan Kiszka 	if (!pxa25x_ssp_comp(drv_data))
693b0312482SJan Kiszka 		pxa2xx_spi_write(drv_data, SSTO, 0);
694b0312482SJan Kiszka 	write_SSSR_CS(drv_data, drv_data->clear_sr);
695b0312482SJan Kiszka 
696b0312482SJan Kiszka 	dev_err(&drv_data->pdev->dev,
697b0312482SJan Kiszka 		"bad message state in interrupt handler\n");
698b0312482SJan Kiszka }
699b0312482SJan Kiszka 
700ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
701ca632f55SGrant Likely {
702ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
7037d94a505SMika Westerberg 	u32 sccr1_reg;
704ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
705ca632f55SGrant Likely 	u32 status;
706ca632f55SGrant Likely 
7077d94a505SMika Westerberg 	/*
7087d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
7097d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
7107d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
7117d94a505SMika Westerberg 	 * interrupt is enabled).
7127d94a505SMika Westerberg 	 */
7137d94a505SMika Westerberg 	if (pm_runtime_suspended(&drv_data->pdev->dev))
7147d94a505SMika Westerberg 		return IRQ_NONE;
7157d94a505SMika Westerberg 
716269e4a41SMika Westerberg 	/*
717269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
718269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
719269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
720269e4a41SMika Westerberg 	 * powered off.
721269e4a41SMika Westerberg 	 */
722c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
723269e4a41SMika Westerberg 	if (status == ~0)
724269e4a41SMika Westerberg 		return IRQ_NONE;
725269e4a41SMika Westerberg 
726c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
727ca632f55SGrant Likely 
728ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
729ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
730ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
731ca632f55SGrant Likely 
73202bc933eSTan, Jui Nee 	/* Ignore RX timeout interrupt if it is disabled */
73302bc933eSTan, Jui Nee 	if (!(sccr1_reg & SSCR1_TINTE))
73402bc933eSTan, Jui Nee 		mask &= ~SSSR_TINT;
73502bc933eSTan, Jui Nee 
736ca632f55SGrant Likely 	if (!(status & mask))
737ca632f55SGrant Likely 		return IRQ_NONE;
738ca632f55SGrant Likely 
739e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
740e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
741e51e9b93SJan Kiszka 
7424fc0caacSJarkko Nikula 	if (!drv_data->master->cur_msg) {
743b0312482SJan Kiszka 		handle_bad_msg(drv_data);
744ca632f55SGrant Likely 		/* Never fail */
745ca632f55SGrant Likely 		return IRQ_HANDLED;
746ca632f55SGrant Likely 	}
747ca632f55SGrant Likely 
748ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
749ca632f55SGrant Likely }
750ca632f55SGrant Likely 
751e5262d05SWeike Chen /*
7529df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
7539df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
7549df461ecSAndy Shevchenko  *
7559df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
7569df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
7579df461ecSAndy Shevchenko  *
7589df461ecSAndy Shevchenko  * Fsys = 200MHz
7599df461ecSAndy Shevchenko  *
7609df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
7619df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
7629df461ecSAndy Shevchenko  *
7639df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
7649df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
7659df461ecSAndy Shevchenko  *
7669df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
7679df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
7689df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
7699df461ecSAndy Shevchenko  *       k = [1, 256]
7709df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
7719df461ecSAndy Shevchenko  *
7729df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
7739df461ecSAndy Shevchenko  * are:
7749df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
7759df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
7769df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
7779df461ecSAndy Shevchenko  *
7789df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
7799df461ecSAndy Shevchenko  *
7809df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
7819df461ecSAndy Shevchenko  * to the asked baud rate.
782e5262d05SWeike Chen  */
7839df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
784e5262d05SWeike Chen {
7859df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
7869df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
7879df461ecSAndy Shevchenko 						   see (2) */
7889df461ecSAndy Shevchenko 						/* case 3 */
7899df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
7909df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
7919df461ecSAndy Shevchenko 	unsigned long scale;
7929df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
7939df461ecSAndy Shevchenko 	long r, r1, r2;
7949df461ecSAndy Shevchenko 	u32 mul;
795e5262d05SWeike Chen 
7969df461ecSAndy Shevchenko 	/* Case 1 */
7979df461ecSAndy Shevchenko 
7989df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
7999df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
8009df461ecSAndy Shevchenko 
8019df461ecSAndy Shevchenko 	/* Calculate initial quot */
8023ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
8039df461ecSAndy Shevchenko 
8049df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
8059df461ecSAndy Shevchenko 	if (q1 > 256) {
8069df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
8079df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
8089df461ecSAndy Shevchenko 		if (scale > 9) {
8099df461ecSAndy Shevchenko 			q1 >>= scale - 9;
8109df461ecSAndy Shevchenko 			mul >>= scale - 9;
8119df461ecSAndy Shevchenko 		}
8129df461ecSAndy Shevchenko 
8139df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
8149df461ecSAndy Shevchenko 		q1 += q1 & 1;
8159df461ecSAndy Shevchenko 	}
8169df461ecSAndy Shevchenko 
8179df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
8189df461ecSAndy Shevchenko 	scale = __ffs(q1);
8199df461ecSAndy Shevchenko 	q1 >>= scale;
8209df461ecSAndy Shevchenko 	mul >>= scale;
8219df461ecSAndy Shevchenko 
8229df461ecSAndy Shevchenko 	/* Get the remainder */
8239df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
8249df461ecSAndy Shevchenko 
8259df461ecSAndy Shevchenko 	/* Case 2 */
8269df461ecSAndy Shevchenko 
8273ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
8289df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
8299df461ecSAndy Shevchenko 
8309df461ecSAndy Shevchenko 	/*
8319df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
8329df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
8339df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
8349df461ecSAndy Shevchenko 	 */
8359df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
8369df461ecSAndy Shevchenko 		/* case 1 is better */
8379df461ecSAndy Shevchenko 		r = r1;
8389df461ecSAndy Shevchenko 		q = q1;
8399df461ecSAndy Shevchenko 	} else {
8409df461ecSAndy Shevchenko 		/* case 2 is better */
8419df461ecSAndy Shevchenko 		r = r2;
8429df461ecSAndy Shevchenko 		q = q2;
8439df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
8449df461ecSAndy Shevchenko 	}
8459df461ecSAndy Shevchenko 
8463ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
8479df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
8489df461ecSAndy Shevchenko 		u64 fssp;
8499df461ecSAndy Shevchenko 		u32 m;
8509df461ecSAndy Shevchenko 
8519df461ecSAndy Shevchenko 		/* Calculate initial quot */
8523ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
8539df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
8549df461ecSAndy Shevchenko 
8559df461ecSAndy Shevchenko 		/* Get the remainder */
8569df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
8579df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
8589df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
8599df461ecSAndy Shevchenko 
8609df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
8619df461ecSAndy Shevchenko 		if (r1 < r) {
8629df461ecSAndy Shevchenko 			/* case 3 is better */
8639df461ecSAndy Shevchenko 			q = 1;
8649df461ecSAndy Shevchenko 			mul = m;
865e5262d05SWeike Chen 		}
866e5262d05SWeike Chen 	}
867e5262d05SWeike Chen 
8689df461ecSAndy Shevchenko 	*dds = mul;
8699df461ecSAndy Shevchenko 	return q - 1;
870e5262d05SWeike Chen }
871e5262d05SWeike Chen 
8723343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
873ca632f55SGrant Likely {
8740eca7cf2SJarkko Nikula 	unsigned long ssp_clk = drv_data->master->max_speed_hz;
8753343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
8763343b7a6SMika Westerberg 
8773343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
878ca632f55SGrant Likely 
879ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
880025ffe88SAndy Shevchenko 		return (ssp_clk / (2 * rate) - 1) & 0xff;
881ca632f55SGrant Likely 	else
882025ffe88SAndy Shevchenko 		return (ssp_clk / rate - 1) & 0xfff;
883ca632f55SGrant Likely }
884ca632f55SGrant Likely 
885e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
886d2c2f6a4SAndy Shevchenko 					   int rate)
887e5262d05SWeike Chen {
88896579a4eSJarkko Nikula 	struct chip_data *chip =
88996579a4eSJarkko Nikula 		spi_get_ctldata(drv_data->master->cur_msg->spi);
890025ffe88SAndy Shevchenko 	unsigned int clk_div;
891e5262d05SWeike Chen 
892e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
893e5262d05SWeike Chen 	case QUARK_X1000_SSP:
8949df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
895eecacf73SDan Carpenter 		break;
896e5262d05SWeike Chen 	default:
897025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
898eecacf73SDan Carpenter 		break;
899e5262d05SWeike Chen 	}
900025ffe88SAndy Shevchenko 	return clk_div << 8;
901e5262d05SWeike Chen }
902e5262d05SWeike Chen 
9033cc7b0e3SJarkko Nikula static bool pxa2xx_spi_can_dma(struct spi_controller *master,
904b6ced294SJarkko Nikula 			       struct spi_device *spi,
905b6ced294SJarkko Nikula 			       struct spi_transfer *xfer)
906b6ced294SJarkko Nikula {
907b6ced294SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
908b6ced294SJarkko Nikula 
909b6ced294SJarkko Nikula 	return chip->enable_dma &&
910b6ced294SJarkko Nikula 	       xfer->len <= MAX_DMA_LEN &&
911b6ced294SJarkko Nikula 	       xfer->len >= chip->dma_burst_size;
912b6ced294SJarkko Nikula }
913b6ced294SJarkko Nikula 
91471293a60Skbuild test robot static int pxa2xx_spi_transfer_one(struct spi_controller *master,
915d5898e19SJarkko Nikula 				   struct spi_device *spi,
916d5898e19SJarkko Nikula 				   struct spi_transfer *transfer)
917ca632f55SGrant Likely {
918d5898e19SJarkko Nikula 	struct driver_data *drv_data = spi_controller_get_devdata(master);
9194fc0caacSJarkko Nikula 	struct spi_message *message = master->cur_msg;
92096579a4eSJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(message->spi);
92196579a4eSJarkko Nikula 	u32 dma_thresh = chip->dma_threshold;
92296579a4eSJarkko Nikula 	u32 dma_burst = chip->dma_burst_size;
92396579a4eSJarkko Nikula 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
924bffc967eSJarkko Nikula 	u32 clk_div;
925bffc967eSJarkko Nikula 	u8 bits;
926bffc967eSJarkko Nikula 	u32 speed;
927ca632f55SGrant Likely 	u32 cr0;
928ca632f55SGrant Likely 	u32 cr1;
9297d1f1bf6SAndy Shevchenko 	int err;
930b6ced294SJarkko Nikula 	int dma_mapped;
931ca632f55SGrant Likely 
932cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
933b6ced294SJarkko Nikula 	if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
934ca632f55SGrant Likely 
935ca632f55SGrant Likely 		/* reject already-mapped transfers; PIO won't always work */
936ca632f55SGrant Likely 		if (message->is_dma_mapped
937ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
938ca632f55SGrant Likely 			dev_err(&drv_data->pdev->dev,
9398ae55af3SJarkko Nikula 				"Mapped transfer length of %u is greater than %d\n",
940ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
941d5898e19SJarkko Nikula 			return -EINVAL;
942ca632f55SGrant Likely 		}
943ca632f55SGrant Likely 
944ca632f55SGrant Likely 		/* warn ... we force this to PIO mode */
945f6bd03a7SJarkko Nikula 		dev_warn_ratelimited(&message->spi->dev,
9468ae55af3SJarkko Nikula 				     "DMA disabled for transfer length %ld greater than %d\n",
947d5898e19SJarkko Nikula 				     (long)transfer->len, MAX_DMA_LEN);
948ca632f55SGrant Likely 	}
949ca632f55SGrant Likely 
950ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
951cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
9528ae55af3SJarkko Nikula 		dev_err(&drv_data->pdev->dev, "Flush failed\n");
953d5898e19SJarkko Nikula 		return -EIO;
954ca632f55SGrant Likely 	}
955ca632f55SGrant Likely 	drv_data->n_bytes = chip->n_bytes;
956ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
957ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
958ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
959ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
960ca632f55SGrant Likely 	drv_data->write = drv_data->tx ? chip->write : null_writer;
961ca632f55SGrant Likely 	drv_data->read = drv_data->rx ? chip->read : null_reader;
962ca632f55SGrant Likely 
963ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
964ca632f55SGrant Likely 	bits = transfer->bits_per_word;
965ca632f55SGrant Likely 	speed = transfer->speed_hz;
966ca632f55SGrant Likely 
967d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
968ca632f55SGrant Likely 
969ca632f55SGrant Likely 	if (bits <= 8) {
970ca632f55SGrant Likely 		drv_data->n_bytes = 1;
971ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
972ca632f55SGrant Likely 					u8_reader : null_reader;
973ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
974ca632f55SGrant Likely 					u8_writer : null_writer;
975ca632f55SGrant Likely 	} else if (bits <= 16) {
976ca632f55SGrant Likely 		drv_data->n_bytes = 2;
977ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
978ca632f55SGrant Likely 					u16_reader : null_reader;
979ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
980ca632f55SGrant Likely 					u16_writer : null_writer;
981ca632f55SGrant Likely 	} else if (bits <= 32) {
982ca632f55SGrant Likely 		drv_data->n_bytes = 4;
983ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
984ca632f55SGrant Likely 					u32_reader : null_reader;
985ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
986ca632f55SGrant Likely 					u32_writer : null_writer;
987ca632f55SGrant Likely 	}
988196b0e2cSJarkko Nikula 	/*
989196b0e2cSJarkko Nikula 	 * if bits/word is changed in dma mode, then must check the
990196b0e2cSJarkko Nikula 	 * thresholds and burst also
991196b0e2cSJarkko Nikula 	 */
992ca632f55SGrant Likely 	if (chip->enable_dma) {
993cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
994cd7bed00SMika Westerberg 						message->spi,
995ca632f55SGrant Likely 						bits, &dma_burst,
996ca632f55SGrant Likely 						&dma_thresh))
997f6bd03a7SJarkko Nikula 			dev_warn_ratelimited(&message->spi->dev,
9988ae55af3SJarkko Nikula 					     "DMA burst size reduced to match bits_per_word\n");
999ca632f55SGrant Likely 	}
1000ca632f55SGrant Likely 
1001b6ced294SJarkko Nikula 	dma_mapped = master->can_dma &&
1002b6ced294SJarkko Nikula 		     master->can_dma(master, message->spi, transfer) &&
1003b6ced294SJarkko Nikula 		     master->cur_msg_mapped;
1004b6ced294SJarkko Nikula 	if (dma_mapped) {
1005ca632f55SGrant Likely 
1006ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
1007cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1008ca632f55SGrant Likely 
1009d5898e19SJarkko Nikula 		err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1010d5898e19SJarkko Nikula 		if (err)
1011d5898e19SJarkko Nikula 			return err;
1012ca632f55SGrant Likely 
1013ca632f55SGrant Likely 		/* Clear status and start DMA engine */
1014ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1015c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1016cd7bed00SMika Westerberg 
1017cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
1018ca632f55SGrant Likely 	} else {
1019ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
1020ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
1021ca632f55SGrant Likely 
1022ca632f55SGrant Likely 		/* Clear status  */
1023ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1024ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1025ca632f55SGrant Likely 	}
1026ca632f55SGrant Likely 
1027ee03672dSJarkko Nikula 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1028ee03672dSJarkko Nikula 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1029ee03672dSJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
1030ee03672dSJarkko Nikula 		dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
10312d7537d8SJarkko Nikula 			master->max_speed_hz
1032ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1033b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1034ee03672dSJarkko Nikula 	else
1035ee03672dSJarkko Nikula 		dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
10362d7537d8SJarkko Nikula 			master->max_speed_hz / 2
1037ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1038b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1039ee03672dSJarkko Nikula 
1040a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
1041c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
1042c039dd27SJarkko Nikula 		    != chip->lpss_rx_threshold)
1043c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSIRF,
1044c039dd27SJarkko Nikula 					 chip->lpss_rx_threshold);
1045c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
1046c039dd27SJarkko Nikula 		    != chip->lpss_tx_threshold)
1047c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSITF,
1048c039dd27SJarkko Nikula 					 chip->lpss_tx_threshold);
1049a0d2642eSMika Westerberg 	}
1050a0d2642eSMika Westerberg 
1051e5262d05SWeike Chen 	if (is_quark_x1000_ssp(drv_data) &&
1052c039dd27SJarkko Nikula 	    (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
1053c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
1054e5262d05SWeike Chen 
1055ca632f55SGrant Likely 	/* see if we need to reload the config registers */
1056c039dd27SJarkko Nikula 	if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1057c039dd27SJarkko Nikula 	    || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1058c039dd27SJarkko Nikula 	    != (cr1 & change_mask)) {
1059ca632f55SGrant Likely 		/* stop the SSP, and update the other bits */
1060c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
1061ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1062c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1063ca632f55SGrant Likely 		/* first set CR1 without interrupt and service enables */
1064c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
1065ca632f55SGrant Likely 		/* restart the SSP */
1066c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0);
1067ca632f55SGrant Likely 
1068ca632f55SGrant Likely 	} else {
1069ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1070c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1071ca632f55SGrant Likely 	}
1072ca632f55SGrant Likely 
1073d5898e19SJarkko Nikula 	/*
1074d5898e19SJarkko Nikula 	 * Release the data by enabling service requests and interrupts,
1075d5898e19SJarkko Nikula 	 * without changing any mode bits
1076d5898e19SJarkko Nikula 	 */
1077c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1078d5898e19SJarkko Nikula 
1079d5898e19SJarkko Nikula 	return 1;
1080ca632f55SGrant Likely }
1081ca632f55SGrant Likely 
1082d5898e19SJarkko Nikula static void pxa2xx_spi_handle_err(struct spi_controller *master,
10837f86bde9SMika Westerberg 				 struct spi_message *msg)
1084ca632f55SGrant Likely {
10853cc7b0e3SJarkko Nikula 	struct driver_data *drv_data = spi_controller_get_devdata(master);
1086ca632f55SGrant Likely 
1087d5898e19SJarkko Nikula 	/* Disable the SSP */
1088d5898e19SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0,
1089d5898e19SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
1090d5898e19SJarkko Nikula 	/* Clear and disable interrupts and service requests */
1091d5898e19SJarkko Nikula 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1092d5898e19SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1,
1093d5898e19SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR1)
1094d5898e19SJarkko Nikula 			 & ~(drv_data->int_cr1 | drv_data->dma_cr1));
1095d5898e19SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
1096d5898e19SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1097ca632f55SGrant Likely 
1098d5898e19SJarkko Nikula 	/*
1099d5898e19SJarkko Nikula 	 * Stop the DMA if running. Note DMA callback handler may have unset
1100d5898e19SJarkko Nikula 	 * the dma_running already, which is fine as stopping is not needed
1101d5898e19SJarkko Nikula 	 * then but we shouldn't rely this flag for anything else than
1102d5898e19SJarkko Nikula 	 * stopping. For instance to differentiate between PIO and DMA
1103d5898e19SJarkko Nikula 	 * transfers.
1104d5898e19SJarkko Nikula 	 */
1105d5898e19SJarkko Nikula 	if (atomic_read(&drv_data->dma_running))
1106d5898e19SJarkko Nikula 		pxa2xx_spi_dma_stop(drv_data);
1107ca632f55SGrant Likely }
1108ca632f55SGrant Likely 
11093cc7b0e3SJarkko Nikula static int pxa2xx_spi_unprepare_transfer(struct spi_controller *master)
11107d94a505SMika Westerberg {
11113cc7b0e3SJarkko Nikula 	struct driver_data *drv_data = spi_controller_get_devdata(master);
11127d94a505SMika Westerberg 
11137d94a505SMika Westerberg 	/* Disable the SSP now */
1114c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0,
1115c039dd27SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
11167d94a505SMika Westerberg 
11177d94a505SMika Westerberg 	return 0;
11187d94a505SMika Westerberg }
11197d94a505SMika Westerberg 
1120ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1121ca632f55SGrant Likely 		    struct pxa2xx_spi_chip *chip_info)
1122ca632f55SGrant Likely {
11233cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
11243cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1125c18d925fSJan Kiszka 	struct gpio_desc *gpiod;
1126ca632f55SGrant Likely 	int err = 0;
1127ca632f55SGrant Likely 
112899f499cdSMika Westerberg 	if (chip == NULL)
112999f499cdSMika Westerberg 		return 0;
113099f499cdSMika Westerberg 
11316ac5a435SAndy Shevchenko 	if (drv_data->cs_gpiods) {
11326ac5a435SAndy Shevchenko 		gpiod = drv_data->cs_gpiods[spi->chip_select];
11336ac5a435SAndy Shevchenko 		if (gpiod) {
1134c18d925fSJan Kiszka 			chip->gpiod_cs = gpiod;
113599f499cdSMika Westerberg 			chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
113699f499cdSMika Westerberg 			gpiod_set_value(gpiod, chip->gpio_cs_inverted);
11376ac5a435SAndy Shevchenko 		}
113899f499cdSMika Westerberg 
113999f499cdSMika Westerberg 		return 0;
114099f499cdSMika Westerberg 	}
114199f499cdSMika Westerberg 
114299f499cdSMika Westerberg 	if (chip_info == NULL)
1143ca632f55SGrant Likely 		return 0;
1144ca632f55SGrant Likely 
1145ca632f55SGrant Likely 	/* NOTE: setup() can be called multiple times, possibly with
1146ca632f55SGrant Likely 	 * different chip_info, release previously requested GPIO
1147ca632f55SGrant Likely 	 */
1148c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
1149a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1150c18d925fSJan Kiszka 		chip->gpiod_cs = NULL;
1151c18d925fSJan Kiszka 	}
1152ca632f55SGrant Likely 
1153ca632f55SGrant Likely 	/* If (*cs_control) is provided, ignore GPIO chip select */
1154ca632f55SGrant Likely 	if (chip_info->cs_control) {
1155ca632f55SGrant Likely 		chip->cs_control = chip_info->cs_control;
1156ca632f55SGrant Likely 		return 0;
1157ca632f55SGrant Likely 	}
1158ca632f55SGrant Likely 
1159ca632f55SGrant Likely 	if (gpio_is_valid(chip_info->gpio_cs)) {
1160ca632f55SGrant Likely 		err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1161ca632f55SGrant Likely 		if (err) {
1162f6bd03a7SJarkko Nikula 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1163f6bd03a7SJarkko Nikula 				chip_info->gpio_cs);
1164ca632f55SGrant Likely 			return err;
1165ca632f55SGrant Likely 		}
1166ca632f55SGrant Likely 
1167c18d925fSJan Kiszka 		gpiod = gpio_to_desc(chip_info->gpio_cs);
1168c18d925fSJan Kiszka 		chip->gpiod_cs = gpiod;
1169ca632f55SGrant Likely 		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1170ca632f55SGrant Likely 
1171c18d925fSJan Kiszka 		err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
1172ca632f55SGrant Likely 	}
1173ca632f55SGrant Likely 
1174ca632f55SGrant Likely 	return err;
1175ca632f55SGrant Likely }
1176ca632f55SGrant Likely 
1177ca632f55SGrant Likely static int setup(struct spi_device *spi)
1178ca632f55SGrant Likely {
1179bffc967eSJarkko Nikula 	struct pxa2xx_spi_chip *chip_info;
1180ca632f55SGrant Likely 	struct chip_data *chip;
1181dccf7369SJarkko Nikula 	const struct lpss_config *config;
11823cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
11833cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1184a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1185a0d2642eSMika Westerberg 
1186e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1187e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1188e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1189e5262d05SWeike Chen 		tx_hi_thres = 0;
1190e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1191e5262d05SWeike Chen 		break;
11927c7289a4SAndy Shevchenko 	case CE4100_SSP:
11937c7289a4SAndy Shevchenko 		tx_thres = TX_THRESH_CE4100_DFLT;
11947c7289a4SAndy Shevchenko 		tx_hi_thres = 0;
11957c7289a4SAndy Shevchenko 		rx_thres = RX_THRESH_CE4100_DFLT;
11967c7289a4SAndy Shevchenko 		break;
119703fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
119803fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
119930f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
120034cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1201b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1202fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
1203dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1204dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1205dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1206dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1207e5262d05SWeike Chen 		break;
1208e5262d05SWeike Chen 	default:
1209a0d2642eSMika Westerberg 		tx_thres = TX_THRESH_DFLT;
1210a0d2642eSMika Westerberg 		tx_hi_thres = 0;
1211a0d2642eSMika Westerberg 		rx_thres = RX_THRESH_DFLT;
1212e5262d05SWeike Chen 		break;
1213a0d2642eSMika Westerberg 	}
1214ca632f55SGrant Likely 
1215ca632f55SGrant Likely 	/* Only alloc on first setup */
1216ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1217ca632f55SGrant Likely 	if (!chip) {
1218ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
12199deae459SJingoo Han 		if (!chip)
1220ca632f55SGrant Likely 			return -ENOMEM;
1221ca632f55SGrant Likely 
1222ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1223ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1224f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1225f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1226ca632f55SGrant Likely 				kfree(chip);
1227ca632f55SGrant Likely 				return -EINVAL;
1228ca632f55SGrant Likely 			}
1229ca632f55SGrant Likely 
1230ca632f55SGrant Likely 			chip->frm = spi->chip_select;
1231c18d925fSJan Kiszka 		}
1232c64e1265SDan O'Donovan 		chip->enable_dma = drv_data->master_info->enable_dma;
1233ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1234ca632f55SGrant Likely 	}
1235ca632f55SGrant Likely 
1236ca632f55SGrant Likely 	/* protocol drivers may change the chip settings, so...
1237ca632f55SGrant Likely 	 * if chip_info exists, use it */
1238ca632f55SGrant Likely 	chip_info = spi->controller_data;
1239ca632f55SGrant Likely 
1240ca632f55SGrant Likely 	/* chip_info isn't always needed */
1241ca632f55SGrant Likely 	chip->cr1 = 0;
1242ca632f55SGrant Likely 	if (chip_info) {
1243ca632f55SGrant Likely 		if (chip_info->timeout)
1244ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1245ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1246ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1247a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1248a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1249ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1250ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1251ca632f55SGrant Likely 		chip->dma_threshold = 0;
1252ca632f55SGrant Likely 		if (chip_info->enable_loopback)
1253ca632f55SGrant Likely 			chip->cr1 = SSCR1_LBM;
1254ca632f55SGrant Likely 	}
1255ca632f55SGrant Likely 
1256a0d2642eSMika Westerberg 	chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1257a0d2642eSMika Westerberg 	chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1258a0d2642eSMika Westerberg 				| SSITF_TxHiThresh(tx_hi_thres);
1259a0d2642eSMika Westerberg 
1260ca632f55SGrant Likely 	/* set dma burst and threshold outside of chip_info path so that if
1261ca632f55SGrant Likely 	 * chip_info goes away after setting chip->enable_dma, the
1262ca632f55SGrant Likely 	 * burst and threshold can still respond to changes in bits_per_word */
1263ca632f55SGrant Likely 	if (chip->enable_dma) {
1264ca632f55SGrant Likely 		/* set up legal burst and threshold for dma */
1265cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1266cd7bed00SMika Westerberg 						spi->bits_per_word,
1267ca632f55SGrant Likely 						&chip->dma_burst_size,
1268ca632f55SGrant Likely 						&chip->dma_threshold)) {
1269f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1270f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1271ca632f55SGrant Likely 		}
1272ca632f55SGrant Likely 	}
1273ca632f55SGrant Likely 
1274e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1275e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1276e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1277e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1278e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1279e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1280e5262d05SWeike Chen 		break;
12817c7289a4SAndy Shevchenko 	case CE4100_SSP:
12827c7289a4SAndy Shevchenko 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
12837c7289a4SAndy Shevchenko 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
12847c7289a4SAndy Shevchenko 		break;
1285e5262d05SWeike Chen 	default:
1286e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1287e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1288e5262d05SWeike Chen 		break;
1289e5262d05SWeike Chen 	}
1290e5262d05SWeike Chen 
1291ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1292ca632f55SGrant Likely 	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1293ca632f55SGrant Likely 			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1294ca632f55SGrant Likely 
1295b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1296b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1297b833172fSMika Westerberg 
1298ca632f55SGrant Likely 	if (spi->bits_per_word <= 8) {
1299ca632f55SGrant Likely 		chip->n_bytes = 1;
1300ca632f55SGrant Likely 		chip->read = u8_reader;
1301ca632f55SGrant Likely 		chip->write = u8_writer;
1302ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 16) {
1303ca632f55SGrant Likely 		chip->n_bytes = 2;
1304ca632f55SGrant Likely 		chip->read = u16_reader;
1305ca632f55SGrant Likely 		chip->write = u16_writer;
1306ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 32) {
1307ca632f55SGrant Likely 		chip->n_bytes = 4;
1308ca632f55SGrant Likely 		chip->read = u32_reader;
1309ca632f55SGrant Likely 		chip->write = u32_writer;
1310ca632f55SGrant Likely 	}
1311ca632f55SGrant Likely 
1312ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1313ca632f55SGrant Likely 
1314ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
1315ca632f55SGrant Likely 		return 0;
1316ca632f55SGrant Likely 
1317ca632f55SGrant Likely 	return setup_cs(spi, chip, chip_info);
1318ca632f55SGrant Likely }
1319ca632f55SGrant Likely 
1320ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1321ca632f55SGrant Likely {
1322ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
13233cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
13243cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1325ca632f55SGrant Likely 
1326ca632f55SGrant Likely 	if (!chip)
1327ca632f55SGrant Likely 		return;
1328ca632f55SGrant Likely 
13296ac5a435SAndy Shevchenko 	if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
1330c18d925fSJan Kiszka 	    chip->gpiod_cs)
1331a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1332ca632f55SGrant Likely 
1333ca632f55SGrant Likely 	kfree(chip);
1334ca632f55SGrant Likely }
1335ca632f55SGrant Likely 
13360db64215SJarkko Nikula #ifdef CONFIG_PCI
1337a3496855SMika Westerberg #ifdef CONFIG_ACPI
133803fbf488SJarkko Nikula 
13398422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
134003fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
134103fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
134203fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
134303fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
134403fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
134530f3a6abSMika Westerberg 	{ "8086228E", LPSS_BSW_SSP },
134603fbf488SJarkko Nikula 	{ },
134703fbf488SJarkko Nikula };
134803fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
134903fbf488SJarkko Nikula 
13500db64215SJarkko Nikula static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
13510db64215SJarkko Nikula {
13520db64215SJarkko Nikula 	unsigned int devid;
13530db64215SJarkko Nikula 	int port_id = -1;
13540db64215SJarkko Nikula 
13550db64215SJarkko Nikula 	if (adev && adev->pnp.unique_id &&
13560db64215SJarkko Nikula 	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
13570db64215SJarkko Nikula 		port_id = devid;
13580db64215SJarkko Nikula 	return port_id;
13590db64215SJarkko Nikula }
13600db64215SJarkko Nikula #else /* !CONFIG_ACPI */
13610db64215SJarkko Nikula static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
13620db64215SJarkko Nikula {
13630db64215SJarkko Nikula 	return -1;
13640db64215SJarkko Nikula }
13650db64215SJarkko Nikula #endif
13660db64215SJarkko Nikula 
136734cadd9cSJarkko Nikula /*
136834cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
136934cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
137034cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
137134cadd9cSJarkko Nikula  */
137234cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
137334cadd9cSJarkko Nikula 	/* SPT-LP */
137434cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
137534cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
137634cadd9cSJarkko Nikula 	/* SPT-H */
137734cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
137834cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1379704d2b07SMika Westerberg 	/* KBL-H */
1380704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1381704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
1382c1b03f11SJarkko Nikula 	/* BXT A-Step */
1383b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1384b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1385b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1386c1b03f11SJarkko Nikula 	/* BXT B-Step */
1387c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1388c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1389c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1390e18a80acSDavid E. Box 	/* GLK */
1391e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1392e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1393e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
1394*22d71a50SMika Westerberg 	/* ICL-LP */
1395*22d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
1396*22d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
1397*22d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
1398b7c08cf8SJarkko Nikula 	/* APL */
1399b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1400b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1401b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1402fc0b2accSJarkko Nikula 	/* CNL-LP */
1403fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1404fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1405fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1406fc0b2accSJarkko Nikula 	/* CNL-H */
1407fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1408fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1409fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
141094e5c23dSAxel Lin 	{ },
141134cadd9cSJarkko Nikula };
141234cadd9cSJarkko Nikula 
141334cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
141434cadd9cSJarkko Nikula {
141534cadd9cSJarkko Nikula 	struct device *dev = param;
141634cadd9cSJarkko Nikula 
141734cadd9cSJarkko Nikula 	if (dev != chan->device->dev->parent)
141834cadd9cSJarkko Nikula 		return false;
141934cadd9cSJarkko Nikula 
142034cadd9cSJarkko Nikula 	return true;
142134cadd9cSJarkko Nikula }
142234cadd9cSJarkko Nikula 
1423a3496855SMika Westerberg static struct pxa2xx_spi_master *
14240db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1425a3496855SMika Westerberg {
1426a3496855SMika Westerberg 	struct pxa2xx_spi_master *pdata;
1427a3496855SMika Westerberg 	struct acpi_device *adev;
1428a3496855SMika Westerberg 	struct ssp_device *ssp;
1429a3496855SMika Westerberg 	struct resource *res;
143034cadd9cSJarkko Nikula 	const struct acpi_device_id *adev_id = NULL;
143134cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
14323b8b6d05SJarkko Nikula 	int type;
1433a3496855SMika Westerberg 
1434b9f6940aSJarkko Nikula 	adev = ACPI_COMPANION(&pdev->dev);
1435a3496855SMika Westerberg 
143634cadd9cSJarkko Nikula 	if (dev_is_pci(pdev->dev.parent))
143734cadd9cSJarkko Nikula 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
143834cadd9cSJarkko Nikula 					 to_pci_dev(pdev->dev.parent));
14390db64215SJarkko Nikula 	else if (adev)
144034cadd9cSJarkko Nikula 		adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
144134cadd9cSJarkko Nikula 					    &pdev->dev);
14420db64215SJarkko Nikula 	else
14430db64215SJarkko Nikula 		return NULL;
144434cadd9cSJarkko Nikula 
144534cadd9cSJarkko Nikula 	if (adev_id)
144634cadd9cSJarkko Nikula 		type = (int)adev_id->driver_data;
144734cadd9cSJarkko Nikula 	else if (pcidev_id)
144834cadd9cSJarkko Nikula 		type = (int)pcidev_id->driver_data;
144903fbf488SJarkko Nikula 	else
145003fbf488SJarkko Nikula 		return NULL;
145103fbf488SJarkko Nikula 
1452cc0ee987SMika Westerberg 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
14539deae459SJingoo Han 	if (!pdata)
1454a3496855SMika Westerberg 		return NULL;
1455a3496855SMika Westerberg 
1456a3496855SMika Westerberg 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1457a3496855SMika Westerberg 	if (!res)
1458a3496855SMika Westerberg 		return NULL;
1459a3496855SMika Westerberg 
1460a3496855SMika Westerberg 	ssp = &pdata->ssp;
1461a3496855SMika Westerberg 
1462a3496855SMika Westerberg 	ssp->phys_base = res->start;
1463cbfd6a21SSachin Kamat 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1464cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
14656dc81f6fSMika Westerberg 		return NULL;
1466a3496855SMika Westerberg 
146734cadd9cSJarkko Nikula 	if (pcidev_id) {
146834cadd9cSJarkko Nikula 		pdata->tx_param = pdev->dev.parent;
146934cadd9cSJarkko Nikula 		pdata->rx_param = pdev->dev.parent;
147034cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
147134cadd9cSJarkko Nikula 	}
147234cadd9cSJarkko Nikula 
1473a3496855SMika Westerberg 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
1474a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
147503fbf488SJarkko Nikula 	ssp->type = type;
1476a3496855SMika Westerberg 	ssp->pdev = pdev;
14770db64215SJarkko Nikula 	ssp->port_id = pxa2xx_spi_get_port_id(adev);
1478a3496855SMika Westerberg 
1479a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1480cddb339bSMika Westerberg 	pdata->enable_dma = true;
1481a3496855SMika Westerberg 
1482a3496855SMika Westerberg 	return pdata;
1483a3496855SMika Westerberg }
1484a3496855SMika Westerberg 
14850db64215SJarkko Nikula #else /* !CONFIG_PCI */
1486a3496855SMika Westerberg static inline struct pxa2xx_spi_master *
14870db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1488a3496855SMika Westerberg {
1489a3496855SMika Westerberg 	return NULL;
1490a3496855SMika Westerberg }
1491a3496855SMika Westerberg #endif
1492a3496855SMika Westerberg 
14933cc7b0e3SJarkko Nikula static int pxa2xx_spi_fw_translate_cs(struct spi_controller *master,
14943cc7b0e3SJarkko Nikula 				      unsigned int cs)
14950c27d9cfSMika Westerberg {
14963cc7b0e3SJarkko Nikula 	struct driver_data *drv_data = spi_controller_get_devdata(master);
14970c27d9cfSMika Westerberg 
14980c27d9cfSMika Westerberg 	if (has_acpi_companion(&drv_data->pdev->dev)) {
14990c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
15000c27d9cfSMika Westerberg 		/*
15010c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
15020c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
15030c27d9cfSMika Westerberg 		 * to match what Linux expects.
15040c27d9cfSMika Westerberg 		 */
15050c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
150630f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
15070c27d9cfSMika Westerberg 			return cs - 1;
15080c27d9cfSMika Westerberg 
15090c27d9cfSMika Westerberg 		default:
15100c27d9cfSMika Westerberg 			break;
15110c27d9cfSMika Westerberg 		}
15120c27d9cfSMika Westerberg 	}
15130c27d9cfSMika Westerberg 
15140c27d9cfSMika Westerberg 	return cs;
15150c27d9cfSMika Westerberg }
15160c27d9cfSMika Westerberg 
1517fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1518ca632f55SGrant Likely {
1519ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
1520ca632f55SGrant Likely 	struct pxa2xx_spi_master *platform_info;
15213cc7b0e3SJarkko Nikula 	struct spi_controller *master;
1522ca632f55SGrant Likely 	struct driver_data *drv_data;
1523ca632f55SGrant Likely 	struct ssp_device *ssp;
15248b136baaSJarkko Nikula 	const struct lpss_config *config;
152599f499cdSMika Westerberg 	int status, count;
1526c039dd27SJarkko Nikula 	u32 tmp;
1527ca632f55SGrant Likely 
1528851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1529851bacf5SMika Westerberg 	if (!platform_info) {
15300db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
1531a3496855SMika Westerberg 		if (!platform_info) {
1532851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
1533851bacf5SMika Westerberg 			return -ENODEV;
1534851bacf5SMika Westerberg 		}
1535a3496855SMika Westerberg 	}
1536ca632f55SGrant Likely 
1537ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1538851bacf5SMika Westerberg 	if (!ssp)
1539851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1540851bacf5SMika Westerberg 
1541851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
1542851bacf5SMika Westerberg 		dev_err(&pdev->dev, "failed to get ssp\n");
1543ca632f55SGrant Likely 		return -ENODEV;
1544ca632f55SGrant Likely 	}
1545ca632f55SGrant Likely 
1546757fe8d5SJarkko Nikula 	master = spi_alloc_master(dev, sizeof(struct driver_data));
1547ca632f55SGrant Likely 	if (!master) {
1548ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot alloc spi_master\n");
1549ca632f55SGrant Likely 		pxa_ssp_free(ssp);
1550ca632f55SGrant Likely 		return -ENOMEM;
1551ca632f55SGrant Likely 	}
15523cc7b0e3SJarkko Nikula 	drv_data = spi_controller_get_devdata(master);
1553ca632f55SGrant Likely 	drv_data->master = master;
1554ca632f55SGrant Likely 	drv_data->master_info = platform_info;
1555ca632f55SGrant Likely 	drv_data->pdev = pdev;
1556ca632f55SGrant Likely 	drv_data->ssp = ssp;
1557ca632f55SGrant Likely 
1558ca632f55SGrant Likely 	master->dev.of_node = pdev->dev.of_node;
1559ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
1560b833172fSMika Westerberg 	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1561ca632f55SGrant Likely 
1562851bacf5SMika Westerberg 	master->bus_num = ssp->port_id;
1563ca632f55SGrant Likely 	master->dma_alignment = DMA_ALIGNMENT;
1564ca632f55SGrant Likely 	master->cleanup = cleanup;
1565ca632f55SGrant Likely 	master->setup = setup;
1566d5898e19SJarkko Nikula 	master->set_cs = pxa2xx_spi_set_cs;
1567d5898e19SJarkko Nikula 	master->transfer_one = pxa2xx_spi_transfer_one;
1568d5898e19SJarkko Nikula 	master->handle_err = pxa2xx_spi_handle_err;
15697d94a505SMika Westerberg 	master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
15700c27d9cfSMika Westerberg 	master->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
15717dd62787SMark Brown 	master->auto_runtime_pm = true;
15723cc7b0e3SJarkko Nikula 	master->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1573ca632f55SGrant Likely 
1574ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1575ca632f55SGrant Likely 
1576ca632f55SGrant Likely 	drv_data->ioaddr = ssp->mmio_base;
1577ca632f55SGrant Likely 	drv_data->ssdr_physical = ssp->phys_base + SSDR;
1578ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1579e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1580e5262d05SWeike Chen 		case QUARK_X1000_SSP:
1581e5262d05SWeike Chen 			master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1582e5262d05SWeike Chen 			break;
1583e5262d05SWeike Chen 		default:
158424778be2SStephen Warren 			master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1585e5262d05SWeike Chen 			break;
1586e5262d05SWeike Chen 		}
1587e5262d05SWeike Chen 
1588ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1589ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1590ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1591ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1592ca632f55SGrant Likely 	} else {
159324778be2SStephen Warren 		master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1594ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
15955928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1596ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1597ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1598ca632f55SGrant Likely 	}
1599ca632f55SGrant Likely 
1600ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1601ca632f55SGrant Likely 			drv_data);
1602ca632f55SGrant Likely 	if (status < 0) {
1603ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
1604ca632f55SGrant Likely 		goto out_error_master_alloc;
1605ca632f55SGrant Likely 	}
1606ca632f55SGrant Likely 
1607ca632f55SGrant Likely 	/* Setup DMA if requested */
1608ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1609cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1610cd7bed00SMika Westerberg 		if (status) {
1611cddb339bSMika Westerberg 			dev_dbg(dev, "no DMA channels available, using PIO\n");
1612cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1613b6ced294SJarkko Nikula 		} else {
1614b6ced294SJarkko Nikula 			master->can_dma = pxa2xx_spi_can_dma;
1615ca632f55SGrant Likely 		}
1616ca632f55SGrant Likely 	}
1617ca632f55SGrant Likely 
1618ca632f55SGrant Likely 	/* Enable SOC clock */
161962bbc864STobias Jordan 	status = clk_prepare_enable(ssp->clk);
162062bbc864STobias Jordan 	if (status)
162162bbc864STobias Jordan 		goto out_error_dma_irq_alloc;
16223343b7a6SMika Westerberg 
16230eca7cf2SJarkko Nikula 	master->max_speed_hz = clk_get_rate(ssp->clk);
1624ca632f55SGrant Likely 
1625ca632f55SGrant Likely 	/* Load default SSP configuration */
1626c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
1627e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1628e5262d05SWeike Chen 	case QUARK_X1000_SSP:
16297c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
16307c7289a4SAndy Shevchenko 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1631c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1632e5262d05SWeike Chen 
1633e5262d05SWeike Chen 		/* using the Motorola SPI protocol and use 8 bit frame */
16347c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
16357c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1636e5262d05SWeike Chen 		break;
16377c7289a4SAndy Shevchenko 	case CE4100_SSP:
16387c7289a4SAndy Shevchenko 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
16397c7289a4SAndy Shevchenko 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
16407c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
16417c7289a4SAndy Shevchenko 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
16427c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1643a2dd8af0SAndy Shevchenko 		break;
1644e5262d05SWeike Chen 	default:
1645c039dd27SJarkko Nikula 		tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1646c039dd27SJarkko Nikula 		      SSCR1_TxTresh(TX_THRESH_DFLT);
1647c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1648c039dd27SJarkko Nikula 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1649c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1650e5262d05SWeike Chen 		break;
1651e5262d05SWeike Chen 	}
1652e5262d05SWeike Chen 
1653ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1654c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1655e5262d05SWeike Chen 
1656e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1657c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1658ca632f55SGrant Likely 
16598b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
16608b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
16618b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
16628b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
16638b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
16648b136baaSJarkko Nikula 						   config->reg_capabilities);
16658b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
16668b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
16678b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
166830f3a6abSMika Westerberg 		} else if (config->cs_num) {
166930f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
16708b136baaSJarkko Nikula 		}
16718b136baaSJarkko Nikula 	}
16728b136baaSJarkko Nikula 	master->num_chipselect = platform_info->num_chipselect;
16738b136baaSJarkko Nikula 
167499f499cdSMika Westerberg 	count = gpiod_count(&pdev->dev, "cs");
16756ac5a435SAndy Shevchenko 	if (count > 0) {
16766ac5a435SAndy Shevchenko 		int i;
16776ac5a435SAndy Shevchenko 
167899f499cdSMika Westerberg 		master->num_chipselect = max_t(int, count,
167999f499cdSMika Westerberg 			master->num_chipselect);
168099f499cdSMika Westerberg 
16816ac5a435SAndy Shevchenko 		drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
16826ac5a435SAndy Shevchenko 			master->num_chipselect, sizeof(struct gpio_desc *),
16836ac5a435SAndy Shevchenko 			GFP_KERNEL);
16846ac5a435SAndy Shevchenko 		if (!drv_data->cs_gpiods) {
16856ac5a435SAndy Shevchenko 			status = -ENOMEM;
16866ac5a435SAndy Shevchenko 			goto out_error_clock_enabled;
16876ac5a435SAndy Shevchenko 		}
16886ac5a435SAndy Shevchenko 
16896ac5a435SAndy Shevchenko 		for (i = 0; i < master->num_chipselect; i++) {
16906ac5a435SAndy Shevchenko 			struct gpio_desc *gpiod;
16916ac5a435SAndy Shevchenko 
1692d35f2dc9SAndy Shevchenko 			gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
16936ac5a435SAndy Shevchenko 			if (IS_ERR(gpiod)) {
16946ac5a435SAndy Shevchenko 				/* Means use native chip select */
16956ac5a435SAndy Shevchenko 				if (PTR_ERR(gpiod) == -ENOENT)
16966ac5a435SAndy Shevchenko 					continue;
16976ac5a435SAndy Shevchenko 
16986ac5a435SAndy Shevchenko 				status = (int)PTR_ERR(gpiod);
16996ac5a435SAndy Shevchenko 				goto out_error_clock_enabled;
17006ac5a435SAndy Shevchenko 			} else {
17016ac5a435SAndy Shevchenko 				drv_data->cs_gpiods[i] = gpiod;
17026ac5a435SAndy Shevchenko 			}
17036ac5a435SAndy Shevchenko 		}
17046ac5a435SAndy Shevchenko 	}
17056ac5a435SAndy Shevchenko 
1706836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1707836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1708836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1709836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1710836d1a22SAntonio Ospite 
1711ca632f55SGrant Likely 	/* Register with the SPI framework */
1712ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
17133cc7b0e3SJarkko Nikula 	status = devm_spi_register_controller(&pdev->dev, master);
1714ca632f55SGrant Likely 	if (status != 0) {
1715ca632f55SGrant Likely 		dev_err(&pdev->dev, "problem registering spi master\n");
17167f86bde9SMika Westerberg 		goto out_error_clock_enabled;
1717ca632f55SGrant Likely 	}
1718ca632f55SGrant Likely 
1719ca632f55SGrant Likely 	return status;
1720ca632f55SGrant Likely 
1721ca632f55SGrant Likely out_error_clock_enabled:
1722e2b714afSJarkko Nikula 	pm_runtime_put_noidle(&pdev->dev);
1723e2b714afSJarkko Nikula 	pm_runtime_disable(&pdev->dev);
17243343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
172562bbc864STobias Jordan 
172662bbc864STobias Jordan out_error_dma_irq_alloc:
1727cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1728ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1729ca632f55SGrant Likely 
1730ca632f55SGrant Likely out_error_master_alloc:
17313cc7b0e3SJarkko Nikula 	spi_controller_put(master);
1732ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1733ca632f55SGrant Likely 	return status;
1734ca632f55SGrant Likely }
1735ca632f55SGrant Likely 
1736ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1737ca632f55SGrant Likely {
1738ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
1739ca632f55SGrant Likely 	struct ssp_device *ssp;
1740ca632f55SGrant Likely 
1741ca632f55SGrant Likely 	if (!drv_data)
1742ca632f55SGrant Likely 		return 0;
1743ca632f55SGrant Likely 	ssp = drv_data->ssp;
1744ca632f55SGrant Likely 
17457d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
17467d94a505SMika Westerberg 
1747ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
1748c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
17493343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1750ca632f55SGrant Likely 
1751ca632f55SGrant Likely 	/* Release DMA */
1752cd7bed00SMika Westerberg 	if (drv_data->master_info->enable_dma)
1753cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1754ca632f55SGrant Likely 
17557d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
17567d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
17577d94a505SMika Westerberg 
1758ca632f55SGrant Likely 	/* Release IRQ */
1759ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1760ca632f55SGrant Likely 
1761ca632f55SGrant Likely 	/* Release SSP */
1762ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1763ca632f55SGrant Likely 
1764ca632f55SGrant Likely 	return 0;
1765ca632f55SGrant Likely }
1766ca632f55SGrant Likely 
1767ca632f55SGrant Likely static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1768ca632f55SGrant Likely {
1769ca632f55SGrant Likely 	int status = 0;
1770ca632f55SGrant Likely 
1771ca632f55SGrant Likely 	if ((status = pxa2xx_spi_remove(pdev)) != 0)
1772ca632f55SGrant Likely 		dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1773ca632f55SGrant Likely }
1774ca632f55SGrant Likely 
1775382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP
1776ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1777ca632f55SGrant Likely {
1778ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1779ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1780bffc967eSJarkko Nikula 	int status;
1781ca632f55SGrant Likely 
17823cc7b0e3SJarkko Nikula 	status = spi_controller_suspend(drv_data->master);
1783ca632f55SGrant Likely 	if (status != 0)
1784ca632f55SGrant Likely 		return status;
1785c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
17862b9375b9SDmitry Eremin-Solenikov 
17872b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
17883343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1789ca632f55SGrant Likely 
1790ca632f55SGrant Likely 	return 0;
1791ca632f55SGrant Likely }
1792ca632f55SGrant Likely 
1793ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1794ca632f55SGrant Likely {
1795ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1796ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1797bffc967eSJarkko Nikula 	int status;
1798ca632f55SGrant Likely 
1799ca632f55SGrant Likely 	/* Enable the SSP clock */
180062bbc864STobias Jordan 	if (!pm_runtime_suspended(dev)) {
180162bbc864STobias Jordan 		status = clk_prepare_enable(ssp->clk);
180262bbc864STobias Jordan 		if (status)
180362bbc864STobias Jordan 			return status;
180462bbc864STobias Jordan 	}
1805ca632f55SGrant Likely 
1806c50325f7SChew, Chiau Ee 	/* Restore LPSS private register bits */
180748421adfSJarkko Nikula 	if (is_lpss_ssp(drv_data))
1808c50325f7SChew, Chiau Ee 		lpss_ssp_setup(drv_data);
1809c50325f7SChew, Chiau Ee 
1810ca632f55SGrant Likely 	/* Start the queue running */
18113cc7b0e3SJarkko Nikula 	status = spi_controller_resume(drv_data->master);
1812ca632f55SGrant Likely 	if (status != 0) {
1813ca632f55SGrant Likely 		dev_err(dev, "problem starting queue (%d)\n", status);
1814ca632f55SGrant Likely 		return status;
1815ca632f55SGrant Likely 	}
1816ca632f55SGrant Likely 
1817ca632f55SGrant Likely 	return 0;
1818ca632f55SGrant Likely }
18197d94a505SMika Westerberg #endif
18207d94a505SMika Westerberg 
1821ec833050SRafael J. Wysocki #ifdef CONFIG_PM
18227d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
18237d94a505SMika Westerberg {
18247d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
18257d94a505SMika Westerberg 
18267d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
18277d94a505SMika Westerberg 	return 0;
18287d94a505SMika Westerberg }
18297d94a505SMika Westerberg 
18307d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
18317d94a505SMika Westerberg {
18327d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
183362bbc864STobias Jordan 	int status;
18347d94a505SMika Westerberg 
183562bbc864STobias Jordan 	status = clk_prepare_enable(drv_data->ssp->clk);
183662bbc864STobias Jordan 	return status;
18377d94a505SMika Westerberg }
18387d94a505SMika Westerberg #endif
1839ca632f55SGrant Likely 
1840ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
18417d94a505SMika Westerberg 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
18427d94a505SMika Westerberg 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
18437d94a505SMika Westerberg 			   pxa2xx_spi_runtime_resume, NULL)
1844ca632f55SGrant Likely };
1845ca632f55SGrant Likely 
1846ca632f55SGrant Likely static struct platform_driver driver = {
1847ca632f55SGrant Likely 	.driver = {
1848ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
1849ca632f55SGrant Likely 		.pm	= &pxa2xx_spi_pm_ops,
1850a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
1851ca632f55SGrant Likely 	},
1852ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
1853ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
1854ca632f55SGrant Likely 	.shutdown = pxa2xx_spi_shutdown,
1855ca632f55SGrant Likely };
1856ca632f55SGrant Likely 
1857ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
1858ca632f55SGrant Likely {
1859ca632f55SGrant Likely 	return platform_driver_register(&driver);
1860ca632f55SGrant Likely }
1861ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
1862ca632f55SGrant Likely 
1863ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
1864ca632f55SGrant Likely {
1865ca632f55SGrant Likely 	platform_driver_unregister(&driver);
1866ca632f55SGrant Likely }
1867ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
1868