xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
48083d6b8SAndy Shevchenko  * Copyright (C) 2013, 2021 Intel Corporation
5ca632f55SGrant Likely  */
6ca632f55SGrant Likely 
75ce25705SAndy Shevchenko #include <linux/acpi.h>
88b136baaSJarkko Nikula #include <linux/bitops.h>
95ce25705SAndy Shevchenko #include <linux/clk.h>
105ce25705SAndy Shevchenko #include <linux/delay.h>
11ca632f55SGrant Likely #include <linux/device.h>
120e476871SAndy Shevchenko #include <linux/dmaengine.h>
13cbfd6a21SSachin Kamat #include <linux/err.h>
145ce25705SAndy Shevchenko #include <linux/errno.h>
155ce25705SAndy Shevchenko #include <linux/gpio/consumer.h>
165ce25705SAndy Shevchenko #include <linux/init.h>
17ca632f55SGrant Likely #include <linux/interrupt.h>
185ce25705SAndy Shevchenko #include <linux/ioport.h>
199df461ecSAndy Shevchenko #include <linux/kernel.h>
205ce25705SAndy Shevchenko #include <linux/module.h>
21ae8fbf1dSAndy Shevchenko #include <linux/mod_devicetable.h>
22ae8fbf1dSAndy Shevchenko #include <linux/of.h>
23ca632f55SGrant Likely #include <linux/platform_device.h>
245ce25705SAndy Shevchenko #include <linux/pm_runtime.h>
25f2faa3ecSAndy Shevchenko #include <linux/property.h>
265ce25705SAndy Shevchenko #include <linux/slab.h>
270e476871SAndy Shevchenko 
28ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h>
29ca632f55SGrant Likely #include <linux/spi/spi.h>
30ca632f55SGrant Likely 
31cd7bed00SMika Westerberg #include "spi-pxa2xx.h"
32ca632f55SGrant Likely 
33ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street");
34ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
35ca632f55SGrant Likely MODULE_LICENSE("GPL");
36ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi");
37ca632f55SGrant Likely 
38ca632f55SGrant Likely #define TIMOUT_DFLT		1000
39ca632f55SGrant Likely 
40ca632f55SGrant Likely /*
418083d6b8SAndy Shevchenko  * For testing SSCR1 changes that require SSP restart, basically
428083d6b8SAndy Shevchenko  * everything except the service and interrupt enables, the PXA270 developer
43ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
448083d6b8SAndy Shevchenko  * list, but the PXA255 developer manual says all bits without really meaning
458083d6b8SAndy Shevchenko  * the service and interrupt enables.
46ca632f55SGrant Likely  */
47ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
48ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
49ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
50ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
51ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
52ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
53ca632f55SGrant Likely 
54e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
55e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
56e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
57e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
58e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
59e5262d05SWeike Chen 
607c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
617c7289a4SAndy Shevchenko 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
627c7289a4SAndy Shevchenko 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
637c7289a4SAndy Shevchenko 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
647c7289a4SAndy Shevchenko 				| CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
657c7289a4SAndy Shevchenko 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
667c7289a4SAndy Shevchenko 
67624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
68624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE			BIT(0)
69624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
708b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT			9
718b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
72a0d2642eSMika Westerberg 
73683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE 0x38
74683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
75683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
76683f65deSEvan Green 
77dccf7369SJarkko Nikula struct lpss_config {
78dccf7369SJarkko Nikula 	/* LPSS offset from drv_data->ioaddr */
79dccf7369SJarkko Nikula 	unsigned offset;
80dccf7369SJarkko Nikula 	/* Register offsets from drv_data->lpss_base or -1 */
81dccf7369SJarkko Nikula 	int reg_general;
82dccf7369SJarkko Nikula 	int reg_ssp;
83dccf7369SJarkko Nikula 	int reg_cs_ctrl;
848b136baaSJarkko Nikula 	int reg_capabilities;
85dccf7369SJarkko Nikula 	/* FIFO thresholds */
86dccf7369SJarkko Nikula 	u32 rx_threshold;
87dccf7369SJarkko Nikula 	u32 tx_threshold_lo;
88dccf7369SJarkko Nikula 	u32 tx_threshold_hi;
89c1e4a53cSMika Westerberg 	/* Chip select control */
90c1e4a53cSMika Westerberg 	unsigned cs_sel_shift;
91c1e4a53cSMika Westerberg 	unsigned cs_sel_mask;
9230f3a6abSMika Westerberg 	unsigned cs_num;
93683f65deSEvan Green 	/* Quirks */
94683f65deSEvan Green 	unsigned cs_clk_stays_gated : 1;
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,
1536eefaee4SEvan Green 		.cs_clk_stays_gated = true,
154b7c08cf8SJarkko Nikula 	},
155fc0b2accSJarkko Nikula 	{	/* LPSS_CNL_SSP */
156fc0b2accSJarkko Nikula 		.offset = 0x200,
157fc0b2accSJarkko Nikula 		.reg_general = -1,
158fc0b2accSJarkko Nikula 		.reg_ssp = 0x20,
159fc0b2accSJarkko Nikula 		.reg_cs_ctrl = 0x24,
160fc0b2accSJarkko Nikula 		.reg_capabilities = 0xfc,
161fc0b2accSJarkko Nikula 		.rx_threshold = 1,
162fc0b2accSJarkko Nikula 		.tx_threshold_lo = 32,
163fc0b2accSJarkko Nikula 		.tx_threshold_hi = 56,
164fc0b2accSJarkko Nikula 		.cs_sel_shift = 8,
165fc0b2accSJarkko Nikula 		.cs_sel_mask = 3 << 8,
166683f65deSEvan Green 		.cs_clk_stays_gated = true,
167fc0b2accSJarkko Nikula 	},
168dccf7369SJarkko Nikula };
169dccf7369SJarkko Nikula 
170dccf7369SJarkko Nikula static inline const struct lpss_config
lpss_get_config(const struct driver_data * drv_data)171dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data)
172dccf7369SJarkko Nikula {
173dccf7369SJarkko Nikula 	return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
174dccf7369SJarkko Nikula }
175dccf7369SJarkko Nikula 
is_lpss_ssp(const struct driver_data * drv_data)176a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data)
177a0d2642eSMika Westerberg {
17803fbf488SJarkko Nikula 	switch (drv_data->ssp_type) {
17903fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
18003fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
18130f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
18234cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
183b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
184fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
18503fbf488SJarkko Nikula 		return true;
18603fbf488SJarkko Nikula 	default:
18703fbf488SJarkko Nikula 		return false;
18803fbf488SJarkko Nikula 	}
189a0d2642eSMika Westerberg }
190a0d2642eSMika Westerberg 
is_quark_x1000_ssp(const struct driver_data * drv_data)191e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
192e5262d05SWeike Chen {
193e5262d05SWeike Chen 	return drv_data->ssp_type == QUARK_X1000_SSP;
194e5262d05SWeike Chen }
195e5262d05SWeike Chen 
is_mmp2_ssp(const struct driver_data * drv_data)19641c98841SAndy Shevchenko static bool is_mmp2_ssp(const struct driver_data *drv_data)
19741c98841SAndy Shevchenko {
19841c98841SAndy Shevchenko 	return drv_data->ssp_type == MMP2_SSP;
19941c98841SAndy Shevchenko }
20041c98841SAndy Shevchenko 
is_mrfld_ssp(const struct driver_data * drv_data)2013fdb59cfSAndy Shevchenko static bool is_mrfld_ssp(const struct driver_data *drv_data)
2023fdb59cfSAndy Shevchenko {
2033fdb59cfSAndy Shevchenko 	return drv_data->ssp_type == MRFLD_SSP;
2043fdb59cfSAndy Shevchenko }
2053fdb59cfSAndy Shevchenko 
pxa2xx_spi_update(const struct driver_data * drv_data,u32 reg,u32 mask,u32 value)2061bed378cSAndy Shevchenko static void pxa2xx_spi_update(const struct driver_data *drv_data, u32 reg, u32 mask, u32 value)
2071bed378cSAndy Shevchenko {
2081bed378cSAndy Shevchenko 	if ((pxa2xx_spi_read(drv_data, reg) & mask) != value)
2091bed378cSAndy Shevchenko 		pxa2xx_spi_write(drv_data, reg, value & mask);
2101bed378cSAndy Shevchenko }
2111bed378cSAndy Shevchenko 
pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data * drv_data)2124fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
2134fdb2424SWeike Chen {
2144fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
215e5262d05SWeike Chen 	case QUARK_X1000_SSP:
216e5262d05SWeike Chen 		return QUARK_X1000_SSCR1_CHANGE_MASK;
2177c7289a4SAndy Shevchenko 	case CE4100_SSP:
2187c7289a4SAndy Shevchenko 		return CE4100_SSCR1_CHANGE_MASK;
2194fdb2424SWeike Chen 	default:
2204fdb2424SWeike Chen 		return SSCR1_CHANGE_MASK;
2214fdb2424SWeike Chen 	}
2224fdb2424SWeike Chen }
2234fdb2424SWeike Chen 
2244fdb2424SWeike Chen static u32
pxa2xx_spi_get_rx_default_thre(const struct driver_data * drv_data)2254fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
2264fdb2424SWeike Chen {
2274fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
228e5262d05SWeike Chen 	case QUARK_X1000_SSP:
229e5262d05SWeike Chen 		return RX_THRESH_QUARK_X1000_DFLT;
2307c7289a4SAndy Shevchenko 	case CE4100_SSP:
2317c7289a4SAndy Shevchenko 		return RX_THRESH_CE4100_DFLT;
2324fdb2424SWeike Chen 	default:
2334fdb2424SWeike Chen 		return RX_THRESH_DFLT;
2344fdb2424SWeike Chen 	}
2354fdb2424SWeike Chen }
2364fdb2424SWeike Chen 
pxa2xx_spi_txfifo_full(const struct driver_data * drv_data)2374fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
2384fdb2424SWeike Chen {
2394fdb2424SWeike Chen 	u32 mask;
2404fdb2424SWeike Chen 
2414fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
242e5262d05SWeike Chen 	case QUARK_X1000_SSP:
243e5262d05SWeike Chen 		mask = QUARK_X1000_SSSR_TFL_MASK;
244e5262d05SWeike Chen 		break;
2457c7289a4SAndy Shevchenko 	case CE4100_SSP:
2467c7289a4SAndy Shevchenko 		mask = CE4100_SSSR_TFL_MASK;
2477c7289a4SAndy Shevchenko 		break;
2484fdb2424SWeike Chen 	default:
2494fdb2424SWeike Chen 		mask = SSSR_TFL_MASK;
2504fdb2424SWeike Chen 		break;
2514fdb2424SWeike Chen 	}
2524fdb2424SWeike Chen 
2536d380132SAndy Shevchenko 	return read_SSSR_bits(drv_data, mask) == mask;
2544fdb2424SWeike Chen }
2554fdb2424SWeike Chen 
pxa2xx_spi_clear_rx_thre(const struct driver_data * drv_data,u32 * sccr1_reg)2564fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
2574fdb2424SWeike Chen 				     u32 *sccr1_reg)
2584fdb2424SWeike Chen {
2594fdb2424SWeike Chen 	u32 mask;
2604fdb2424SWeike Chen 
2614fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
262e5262d05SWeike Chen 	case QUARK_X1000_SSP:
263e5262d05SWeike Chen 		mask = QUARK_X1000_SSCR1_RFT;
264e5262d05SWeike Chen 		break;
2657c7289a4SAndy Shevchenko 	case CE4100_SSP:
2667c7289a4SAndy Shevchenko 		mask = CE4100_SSCR1_RFT;
2677c7289a4SAndy Shevchenko 		break;
2684fdb2424SWeike Chen 	default:
2694fdb2424SWeike Chen 		mask = SSCR1_RFT;
2704fdb2424SWeike Chen 		break;
2714fdb2424SWeike Chen 	}
2724fdb2424SWeike Chen 	*sccr1_reg &= ~mask;
2734fdb2424SWeike Chen }
2744fdb2424SWeike Chen 
pxa2xx_spi_set_rx_thre(const struct driver_data * drv_data,u32 * sccr1_reg,u32 threshold)2754fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
2764fdb2424SWeike Chen 				   u32 *sccr1_reg, u32 threshold)
2774fdb2424SWeike Chen {
2784fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
279e5262d05SWeike Chen 	case QUARK_X1000_SSP:
280e5262d05SWeike Chen 		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
281e5262d05SWeike Chen 		break;
2827c7289a4SAndy Shevchenko 	case CE4100_SSP:
2837c7289a4SAndy Shevchenko 		*sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
2847c7289a4SAndy Shevchenko 		break;
2854fdb2424SWeike Chen 	default:
2864fdb2424SWeike Chen 		*sccr1_reg |= SSCR1_RxTresh(threshold);
2874fdb2424SWeike Chen 		break;
2884fdb2424SWeike Chen 	}
2894fdb2424SWeike Chen }
2904fdb2424SWeike Chen 
pxa2xx_configure_sscr0(const struct driver_data * drv_data,u32 clk_div,u8 bits)2914fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
2924fdb2424SWeike Chen 				  u32 clk_div, u8 bits)
2934fdb2424SWeike Chen {
2944fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
295e5262d05SWeike Chen 	case QUARK_X1000_SSP:
296e5262d05SWeike Chen 		return clk_div
297e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_Motorola
2980c8ccd8bSAndy Shevchenko 			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits);
2994fdb2424SWeike Chen 	default:
3004fdb2424SWeike Chen 		return clk_div
3014fdb2424SWeike Chen 			| SSCR0_Motorola
3024fdb2424SWeike Chen 			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
3034fdb2424SWeike Chen 			| (bits > 16 ? SSCR0_EDSS : 0);
3044fdb2424SWeike Chen 	}
3054fdb2424SWeike Chen }
3064fdb2424SWeike Chen 
307a0d2642eSMika Westerberg /*
308a0d2642eSMika Westerberg  * Read and write LPSS SSP private registers. Caller must first check that
309a0d2642eSMika Westerberg  * is_lpss_ssp() returns true before these can be called.
310a0d2642eSMika Westerberg  */
__lpss_ssp_read_priv(struct driver_data * drv_data,unsigned offset)311a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
312a0d2642eSMika Westerberg {
313a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
314a0d2642eSMika Westerberg 	return readl(drv_data->lpss_base + offset);
315a0d2642eSMika Westerberg }
316a0d2642eSMika Westerberg 
__lpss_ssp_write_priv(struct driver_data * drv_data,unsigned offset,u32 value)317a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data,
318a0d2642eSMika Westerberg 				  unsigned offset, u32 value)
319a0d2642eSMika Westerberg {
320a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
321a0d2642eSMika Westerberg 	writel(value, drv_data->lpss_base + offset);
322a0d2642eSMika Westerberg }
323a0d2642eSMika Westerberg 
324a0d2642eSMika Westerberg /*
325a0d2642eSMika Westerberg  * lpss_ssp_setup - perform LPSS SSP specific setup
326a0d2642eSMika Westerberg  * @drv_data: pointer to the driver private data
327a0d2642eSMika Westerberg  *
328a0d2642eSMika Westerberg  * Perform LPSS SSP specific setup. This function must be called first if
329a0d2642eSMika Westerberg  * one is going to use LPSS SSP private registers.
330a0d2642eSMika Westerberg  */
lpss_ssp_setup(struct driver_data * drv_data)331a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data)
332a0d2642eSMika Westerberg {
333dccf7369SJarkko Nikula 	const struct lpss_config *config;
334dccf7369SJarkko Nikula 	u32 value;
335a0d2642eSMika Westerberg 
336dccf7369SJarkko Nikula 	config = lpss_get_config(drv_data);
3379e43c9a8SAndy Shevchenko 	drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset;
338a0d2642eSMika Westerberg 
339a0d2642eSMika Westerberg 	/* Enable software chip select control */
3400e897218SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
341624ea72eSJarkko Nikula 	value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
342624ea72eSJarkko Nikula 	value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
343dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
3440054e28dSMika Westerberg 
3450054e28dSMika Westerberg 	/* Enable multiblock DMA transfers */
34651eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma) {
347dccf7369SJarkko Nikula 		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
3481de70612SMika Westerberg 
34982ba2c2aSJarkko Nikula 		if (config->reg_general >= 0) {
35082ba2c2aSJarkko Nikula 			value = __lpss_ssp_read_priv(drv_data,
35182ba2c2aSJarkko Nikula 						     config->reg_general);
352624ea72eSJarkko Nikula 			value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
35382ba2c2aSJarkko Nikula 			__lpss_ssp_write_priv(drv_data,
35482ba2c2aSJarkko Nikula 					      config->reg_general, value);
35582ba2c2aSJarkko Nikula 		}
3561de70612SMika Westerberg 	}
357a0d2642eSMika Westerberg }
358a0d2642eSMika Westerberg 
lpss_ssp_select_cs(struct spi_device * spi,const struct lpss_config * config)359d5898e19SJarkko Nikula static void lpss_ssp_select_cs(struct spi_device *spi,
360c1e4a53cSMika Westerberg 			       const struct lpss_config *config)
361a0d2642eSMika Westerberg {
362d5898e19SJarkko Nikula 	struct driver_data *drv_data =
363d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
364d0283eb2SJarkko Nikula 	u32 value, cs;
365a0d2642eSMika Westerberg 
366c1e4a53cSMika Westerberg 	if (!config->cs_sel_mask)
367c1e4a53cSMika Westerberg 		return;
368dccf7369SJarkko Nikula 
369dccf7369SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
370c1e4a53cSMika Westerberg 
3719e264f3fSAmit Kumar Mahapatra via Alsa-devel 	cs = spi_get_chipselect(spi, 0);
372c1e4a53cSMika Westerberg 	cs <<= config->cs_sel_shift;
373c1e4a53cSMika Westerberg 	if (cs != (value & config->cs_sel_mask)) {
374d0283eb2SJarkko Nikula 		/*
375c1e4a53cSMika Westerberg 		 * When switching another chip select output active the
376c1e4a53cSMika Westerberg 		 * output must be selected first and wait 2 ssp_clk cycles
377c1e4a53cSMika Westerberg 		 * before changing state to active. Otherwise a short
378c1e4a53cSMika Westerberg 		 * glitch will occur on the previous chip select since
379c1e4a53cSMika Westerberg 		 * output select is latched but state control is not.
380d0283eb2SJarkko Nikula 		 */
381c1e4a53cSMika Westerberg 		value &= ~config->cs_sel_mask;
382d0283eb2SJarkko Nikula 		value |= cs;
383d0283eb2SJarkko Nikula 		__lpss_ssp_write_priv(drv_data,
384d0283eb2SJarkko Nikula 				      config->reg_cs_ctrl, value);
385d0283eb2SJarkko Nikula 		ndelay(1000000000 /
38651eea52dSLubomir Rintel 		       (drv_data->controller->max_speed_hz / 2));
387d0283eb2SJarkko Nikula 	}
388d0283eb2SJarkko Nikula }
389c1e4a53cSMika Westerberg 
lpss_ssp_cs_control(struct spi_device * spi,bool enable)390d5898e19SJarkko Nikula static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
391c1e4a53cSMika Westerberg {
392d5898e19SJarkko Nikula 	struct driver_data *drv_data =
393d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
394c1e4a53cSMika Westerberg 	const struct lpss_config *config;
395c1e4a53cSMika Westerberg 	u32 value;
396c1e4a53cSMika Westerberg 
397c1e4a53cSMika Westerberg 	config = lpss_get_config(drv_data);
398c1e4a53cSMika Westerberg 
399c1e4a53cSMika Westerberg 	if (enable)
400d5898e19SJarkko Nikula 		lpss_ssp_select_cs(spi, config);
401c1e4a53cSMika Westerberg 
402c1e4a53cSMika Westerberg 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
403c1e4a53cSMika Westerberg 	if (enable)
404c1e4a53cSMika Westerberg 		value &= ~LPSS_CS_CONTROL_CS_HIGH;
405c1e4a53cSMika Westerberg 	else
406c1e4a53cSMika Westerberg 		value |= LPSS_CS_CONTROL_CS_HIGH;
407dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
408683f65deSEvan Green 	if (config->cs_clk_stays_gated) {
409683f65deSEvan Green 		u32 clkgate;
410683f65deSEvan Green 
411683f65deSEvan Green 		/*
412683f65deSEvan Green 		 * Changing CS alone when dynamic clock gating is on won't
413683f65deSEvan Green 		 * actually flip CS at that time. This ruins SPI transfers
414683f65deSEvan Green 		 * that specify delays, or have no data. Toggle the clock mode
415683f65deSEvan Green 		 * to force on briefly to poke the CS pin to move.
416683f65deSEvan Green 		 */
417683f65deSEvan Green 		clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
418683f65deSEvan Green 		value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
419683f65deSEvan Green 			LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
420683f65deSEvan Green 
421683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
422683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
423683f65deSEvan Green 	}
424a0d2642eSMika Westerberg }
425a0d2642eSMika Westerberg 
cs_assert(struct spi_device * spi)426d5898e19SJarkko Nikula static void cs_assert(struct spi_device *spi)
427ca632f55SGrant Likely {
428d5898e19SJarkko Nikula 	struct driver_data *drv_data =
429d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
430ca632f55SGrant Likely 
431ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP) {
4329e264f3fSAmit Kumar Mahapatra via Alsa-devel 		pxa2xx_spi_write(drv_data, SSSR, spi_get_chipselect(spi, 0));
433ca632f55SGrant Likely 		return;
434ca632f55SGrant Likely 	}
435ca632f55SGrant Likely 
4367566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
437d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, true);
438ca632f55SGrant Likely }
439ca632f55SGrant Likely 
cs_deassert(struct spi_device * spi)440d5898e19SJarkko Nikula static void cs_deassert(struct spi_device *spi)
441ca632f55SGrant Likely {
442d5898e19SJarkko Nikula 	struct driver_data *drv_data =
443d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
444104e51afSJarkko Nikula 	unsigned long timeout;
445ca632f55SGrant Likely 
446ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
447ca632f55SGrant Likely 		return;
448ca632f55SGrant Likely 
449104e51afSJarkko Nikula 	/* Wait until SSP becomes idle before deasserting the CS */
450104e51afSJarkko Nikula 	timeout = jiffies + msecs_to_jiffies(10);
451104e51afSJarkko Nikula 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
452104e51afSJarkko Nikula 	       !time_after(jiffies, timeout))
453104e51afSJarkko Nikula 		cpu_relax();
454104e51afSJarkko Nikula 
4557566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
456d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, false);
457d5898e19SJarkko Nikula }
458d5898e19SJarkko Nikula 
pxa2xx_spi_set_cs(struct spi_device * spi,bool level)459d5898e19SJarkko Nikula static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
460d5898e19SJarkko Nikula {
461d5898e19SJarkko Nikula 	if (level)
462d5898e19SJarkko Nikula 		cs_deassert(spi);
463d5898e19SJarkko Nikula 	else
464d5898e19SJarkko Nikula 		cs_assert(spi);
465ca632f55SGrant Likely }
466ca632f55SGrant Likely 
pxa2xx_spi_flush(struct driver_data * drv_data)467cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
468ca632f55SGrant Likely {
469ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
470ca632f55SGrant Likely 
471ca632f55SGrant Likely 	do {
4726d380132SAndy Shevchenko 		while (read_SSSR_bits(drv_data, SSSR_RNE))
473c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
474c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
475ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
476ca632f55SGrant Likely 
477ca632f55SGrant Likely 	return limit;
478ca632f55SGrant Likely }
479ca632f55SGrant Likely 
pxa2xx_spi_off(struct driver_data * drv_data)48029d7e05cSLubomir Rintel static void pxa2xx_spi_off(struct driver_data *drv_data)
48129d7e05cSLubomir Rintel {
48241c98841SAndy Shevchenko 	/* On MMP, disabling SSE seems to corrupt the Rx FIFO */
48341c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data))
48429d7e05cSLubomir Rintel 		return;
48529d7e05cSLubomir Rintel 
4860c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(drv_data->ssp);
48729d7e05cSLubomir Rintel }
48829d7e05cSLubomir Rintel 
null_writer(struct driver_data * drv_data)489ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
490ca632f55SGrant Likely {
491ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
492ca632f55SGrant Likely 
4934fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
494ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
495ca632f55SGrant Likely 		return 0;
496ca632f55SGrant Likely 
497c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
498ca632f55SGrant Likely 	drv_data->tx += n_bytes;
499ca632f55SGrant Likely 
500ca632f55SGrant Likely 	return 1;
501ca632f55SGrant Likely }
502ca632f55SGrant Likely 
null_reader(struct driver_data * drv_data)503ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
504ca632f55SGrant Likely {
505ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
506ca632f55SGrant Likely 
5076d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
508c039dd27SJarkko Nikula 		pxa2xx_spi_read(drv_data, SSDR);
509ca632f55SGrant Likely 		drv_data->rx += n_bytes;
510ca632f55SGrant Likely 	}
511ca632f55SGrant Likely 
512ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
513ca632f55SGrant Likely }
514ca632f55SGrant Likely 
u8_writer(struct driver_data * drv_data)515ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data)
516ca632f55SGrant Likely {
5174fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
518ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
519ca632f55SGrant Likely 		return 0;
520ca632f55SGrant Likely 
521c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
522ca632f55SGrant Likely 	++drv_data->tx;
523ca632f55SGrant Likely 
524ca632f55SGrant Likely 	return 1;
525ca632f55SGrant Likely }
526ca632f55SGrant Likely 
u8_reader(struct driver_data * drv_data)527ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data)
528ca632f55SGrant Likely {
5296d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
530c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
531ca632f55SGrant Likely 		++drv_data->rx;
532ca632f55SGrant Likely 	}
533ca632f55SGrant Likely 
534ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
535ca632f55SGrant Likely }
536ca632f55SGrant Likely 
u16_writer(struct driver_data * drv_data)537ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
538ca632f55SGrant Likely {
5394fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
540ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
541ca632f55SGrant Likely 		return 0;
542ca632f55SGrant Likely 
543c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
544ca632f55SGrant Likely 	drv_data->tx += 2;
545ca632f55SGrant Likely 
546ca632f55SGrant Likely 	return 1;
547ca632f55SGrant Likely }
548ca632f55SGrant Likely 
u16_reader(struct driver_data * drv_data)549ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
550ca632f55SGrant Likely {
5516d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
552c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
553ca632f55SGrant Likely 		drv_data->rx += 2;
554ca632f55SGrant Likely 	}
555ca632f55SGrant Likely 
556ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
557ca632f55SGrant Likely }
558ca632f55SGrant Likely 
u32_writer(struct driver_data * drv_data)559ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
560ca632f55SGrant Likely {
5614fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
562ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
563ca632f55SGrant Likely 		return 0;
564ca632f55SGrant Likely 
565c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
566ca632f55SGrant Likely 	drv_data->tx += 4;
567ca632f55SGrant Likely 
568ca632f55SGrant Likely 	return 1;
569ca632f55SGrant Likely }
570ca632f55SGrant Likely 
u32_reader(struct driver_data * drv_data)571ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
572ca632f55SGrant Likely {
5736d380132SAndy Shevchenko 	while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
574c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
575ca632f55SGrant Likely 		drv_data->rx += 4;
576ca632f55SGrant Likely 	}
577ca632f55SGrant Likely 
578ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
579ca632f55SGrant Likely }
580ca632f55SGrant Likely 
reset_sccr1(struct driver_data * drv_data)581ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
582ca632f55SGrant Likely {
583e3aa9accSAndy Shevchenko 	u32 mask = drv_data->int_cr1 | drv_data->dma_cr1, threshold;
584e3aa9accSAndy Shevchenko 	struct chip_data *chip;
585e3aa9accSAndy Shevchenko 
586e3aa9accSAndy Shevchenko 	if (drv_data->controller->cur_msg) {
587e3aa9accSAndy Shevchenko 		chip = spi_get_ctldata(drv_data->controller->cur_msg->spi);
588e3aa9accSAndy Shevchenko 		threshold = chip->threshold;
589e3aa9accSAndy Shevchenko 	} else {
590e3aa9accSAndy Shevchenko 		threshold = 0;
591e3aa9accSAndy Shevchenko 	}
592ca632f55SGrant Likely 
593152bc19eSAndy Shevchenko 	switch (drv_data->ssp_type) {
594152bc19eSAndy Shevchenko 	case QUARK_X1000_SSP:
595e0a6512dSAndy Shevchenko 		mask |= QUARK_X1000_SSCR1_RFT;
596152bc19eSAndy Shevchenko 		break;
5977c7289a4SAndy Shevchenko 	case CE4100_SSP:
598e0a6512dSAndy Shevchenko 		mask |= CE4100_SSCR1_RFT;
5997c7289a4SAndy Shevchenko 		break;
600152bc19eSAndy Shevchenko 	default:
601e0a6512dSAndy Shevchenko 		mask |= SSCR1_RFT;
602152bc19eSAndy Shevchenko 		break;
603152bc19eSAndy Shevchenko 	}
604e0a6512dSAndy Shevchenko 
605e3aa9accSAndy Shevchenko 	pxa2xx_spi_update(drv_data, SSCR1, mask, threshold);
606ca632f55SGrant Likely }
607ca632f55SGrant Likely 
int_stop_and_reset(struct driver_data * drv_data)608ab77fe89SAndy Shevchenko static void int_stop_and_reset(struct driver_data *drv_data)
609ca632f55SGrant Likely {
610ab77fe89SAndy Shevchenko 	/* Clear and disable interrupts */
611ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
612ca632f55SGrant Likely 	reset_sccr1(drv_data);
613ab77fe89SAndy Shevchenko 	if (pxa25x_ssp_comp(drv_data))
614ab77fe89SAndy Shevchenko 		return;
615ab77fe89SAndy Shevchenko 
616c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSTO, 0);
617ab77fe89SAndy Shevchenko }
618ab77fe89SAndy Shevchenko 
int_error_stop(struct driver_data * drv_data,const char * msg,int err)6194761d2e7SAndy Shevchenko static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
620ab77fe89SAndy Shevchenko {
621ab77fe89SAndy Shevchenko 	int_stop_and_reset(drv_data);
622cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
62329d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
624ca632f55SGrant Likely 
625c3dce24cSAndy Shevchenko 	dev_err(drv_data->ssp->dev, "%s\n", msg);
626ca632f55SGrant Likely 
6274761d2e7SAndy Shevchenko 	drv_data->controller->cur_msg->status = err;
62851eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
629ca632f55SGrant Likely }
630ca632f55SGrant Likely 
int_transfer_complete(struct driver_data * drv_data)631ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
632ca632f55SGrant Likely {
633ab77fe89SAndy Shevchenko 	int_stop_and_reset(drv_data);
634ca632f55SGrant Likely 
63551eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
636ca632f55SGrant Likely }
637ca632f55SGrant Likely 
interrupt_transfer(struct driver_data * drv_data)638ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
639ca632f55SGrant Likely {
6406d380132SAndy Shevchenko 	u32 irq_status;
641ca632f55SGrant Likely 
6426d380132SAndy Shevchenko 	irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr);
6436d380132SAndy Shevchenko 	if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE))
6446d380132SAndy Shevchenko 		irq_status &= ~SSSR_TFS;
645ca632f55SGrant Likely 
646ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
6478083d6b8SAndy Shevchenko 		int_error_stop(drv_data, "interrupt_transfer: FIFO overrun", -EIO);
648ca632f55SGrant Likely 		return IRQ_HANDLED;
649ca632f55SGrant Likely 	}
650ca632f55SGrant Likely 
651ec93cb6fSLubomir Rintel 	if (irq_status & SSSR_TUR) {
6528083d6b8SAndy Shevchenko 		int_error_stop(drv_data, "interrupt_transfer: FIFO underrun", -EIO);
653ec93cb6fSLubomir Rintel 		return IRQ_HANDLED;
654ec93cb6fSLubomir Rintel 	}
655ec93cb6fSLubomir Rintel 
656ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
657c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
658ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
659ca632f55SGrant Likely 			int_transfer_complete(drv_data);
660ca632f55SGrant Likely 			return IRQ_HANDLED;
661ca632f55SGrant Likely 		}
662ca632f55SGrant Likely 	}
663ca632f55SGrant Likely 
6648083d6b8SAndy Shevchenko 	/* Drain Rx FIFO, Fill Tx FIFO and prevent overruns */
665ca632f55SGrant Likely 	do {
666ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
667ca632f55SGrant Likely 			int_transfer_complete(drv_data);
668ca632f55SGrant Likely 			return IRQ_HANDLED;
669ca632f55SGrant Likely 		}
670ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
671ca632f55SGrant Likely 
672ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
673ca632f55SGrant Likely 		int_transfer_complete(drv_data);
674ca632f55SGrant Likely 		return IRQ_HANDLED;
675ca632f55SGrant Likely 	}
676ca632f55SGrant Likely 
677ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
678ca632f55SGrant Likely 		u32 bytes_left;
679ca632f55SGrant Likely 		u32 sccr1_reg;
680ca632f55SGrant Likely 
681c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
682ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
683ca632f55SGrant Likely 
684ca632f55SGrant Likely 		/*
6858083d6b8SAndy Shevchenko 		 * PXA25x_SSP has no timeout, set up Rx threshold for
6868083d6b8SAndy Shevchenko 		 * the remaining Rx bytes.
687ca632f55SGrant Likely 		 */
688ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
6894fdb2424SWeike Chen 			u32 rx_thre;
690ca632f55SGrant Likely 
6914fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
692ca632f55SGrant Likely 
693ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
694ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
695ca632f55SGrant Likely 			case 4:
6962c183376SGustavo A. R. Silva 				bytes_left >>= 2;
6972c183376SGustavo A. R. Silva 				break;
698ca632f55SGrant Likely 			case 2:
699ca632f55SGrant Likely 				bytes_left >>= 1;
7002c183376SGustavo A. R. Silva 				break;
701ca632f55SGrant Likely 			}
702ca632f55SGrant Likely 
7034fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
7044fdb2424SWeike Chen 			if (rx_thre > bytes_left)
7054fdb2424SWeike Chen 				rx_thre = bytes_left;
706ca632f55SGrant Likely 
7074fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
708ca632f55SGrant Likely 		}
709c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
710ca632f55SGrant Likely 	}
711ca632f55SGrant Likely 
712ca632f55SGrant Likely 	/* We did something */
713ca632f55SGrant Likely 	return IRQ_HANDLED;
714ca632f55SGrant Likely }
715ca632f55SGrant Likely 
handle_bad_msg(struct driver_data * drv_data)716b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data)
717b0312482SJan Kiszka {
7183bbdc083SAndy Shevchenko 	int_stop_and_reset(drv_data);
71929d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
720b0312482SJan Kiszka 
721c3dce24cSAndy Shevchenko 	dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
722b0312482SJan Kiszka }
723b0312482SJan Kiszka 
ssp_int(int irq,void * dev_id)724ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
725ca632f55SGrant Likely {
726ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
7277d94a505SMika Westerberg 	u32 sccr1_reg;
728ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
729ca632f55SGrant Likely 	u32 status;
730ca632f55SGrant Likely 
7317d94a505SMika Westerberg 	/*
7327d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
7337d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
7347d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
7357d94a505SMika Westerberg 	 * interrupt is enabled).
7367d94a505SMika Westerberg 	 */
737c3dce24cSAndy Shevchenko 	if (pm_runtime_suspended(drv_data->ssp->dev))
7387d94a505SMika Westerberg 		return IRQ_NONE;
7397d94a505SMika Westerberg 
740269e4a41SMika Westerberg 	/*
741269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
742269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
743269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
744269e4a41SMika Westerberg 	 * powered off.
745269e4a41SMika Westerberg 	 */
746c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
747269e4a41SMika Westerberg 	if (status == ~0)
748269e4a41SMika Westerberg 		return IRQ_NONE;
749269e4a41SMika Westerberg 
750c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
751ca632f55SGrant Likely 
752ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
753ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
754ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
755ca632f55SGrant Likely 
75602bc933eSTan, Jui Nee 	/* Ignore RX timeout interrupt if it is disabled */
75702bc933eSTan, Jui Nee 	if (!(sccr1_reg & SSCR1_TINTE))
75802bc933eSTan, Jui Nee 		mask &= ~SSSR_TINT;
75902bc933eSTan, Jui Nee 
760ca632f55SGrant Likely 	if (!(status & mask))
761ca632f55SGrant Likely 		return IRQ_NONE;
762ca632f55SGrant Likely 
763e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
764e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
765e51e9b93SJan Kiszka 
76651eea52dSLubomir Rintel 	if (!drv_data->controller->cur_msg) {
767b0312482SJan Kiszka 		handle_bad_msg(drv_data);
768ca632f55SGrant Likely 		/* Never fail */
769ca632f55SGrant Likely 		return IRQ_HANDLED;
770ca632f55SGrant Likely 	}
771ca632f55SGrant Likely 
772ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
773ca632f55SGrant Likely }
774ca632f55SGrant Likely 
775e5262d05SWeike Chen /*
7769df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
7779df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
7789df461ecSAndy Shevchenko  *
7799df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
7809df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
7819df461ecSAndy Shevchenko  *
7829df461ecSAndy Shevchenko  * Fsys = 200MHz
7839df461ecSAndy Shevchenko  *
7849df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
7859df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
7869df461ecSAndy Shevchenko  *
7879df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
7889df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
7899df461ecSAndy Shevchenko  *
7909df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
7919df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
7929df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
7939df461ecSAndy Shevchenko  *       k = [1, 256]
7949df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
7959df461ecSAndy Shevchenko  *
7969df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
7979df461ecSAndy Shevchenko  * are:
7989df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
7999df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
8009df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
8019df461ecSAndy Shevchenko  *
8029df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
8039df461ecSAndy Shevchenko  *
8049df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
8059df461ecSAndy Shevchenko  * to the asked baud rate.
806e5262d05SWeike Chen  */
quark_x1000_get_clk_div(int rate,u32 * dds)8079df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
808e5262d05SWeike Chen {
8099df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
8109df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
8119df461ecSAndy Shevchenko 						   see (2) */
8129df461ecSAndy Shevchenko 						/* case 3 */
8139df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
8149df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
8159df461ecSAndy Shevchenko 	unsigned long scale;
8169df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
8179df461ecSAndy Shevchenko 	long r, r1, r2;
8189df461ecSAndy Shevchenko 	u32 mul;
819e5262d05SWeike Chen 
8209df461ecSAndy Shevchenko 	/* Case 1 */
8219df461ecSAndy Shevchenko 
8229df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
8239df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
8249df461ecSAndy Shevchenko 
8259df461ecSAndy Shevchenko 	/* Calculate initial quot */
8263ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
8279df461ecSAndy Shevchenko 
8289df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
8299df461ecSAndy Shevchenko 	if (q1 > 256) {
8309df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
8319df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
8329df461ecSAndy Shevchenko 		if (scale > 9) {
8339df461ecSAndy Shevchenko 			q1 >>= scale - 9;
8349df461ecSAndy Shevchenko 			mul >>= scale - 9;
8359df461ecSAndy Shevchenko 		}
8369df461ecSAndy Shevchenko 
8379df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
8389df461ecSAndy Shevchenko 		q1 += q1 & 1;
8399df461ecSAndy Shevchenko 	}
8409df461ecSAndy Shevchenko 
8419df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
8429df461ecSAndy Shevchenko 	scale = __ffs(q1);
8439df461ecSAndy Shevchenko 	q1 >>= scale;
8449df461ecSAndy Shevchenko 	mul >>= scale;
8459df461ecSAndy Shevchenko 
8469df461ecSAndy Shevchenko 	/* Get the remainder */
8479df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
8489df461ecSAndy Shevchenko 
8499df461ecSAndy Shevchenko 	/* Case 2 */
8509df461ecSAndy Shevchenko 
8513ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
8529df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
8539df461ecSAndy Shevchenko 
8549df461ecSAndy Shevchenko 	/*
8559df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
8569df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
8579df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
8589df461ecSAndy Shevchenko 	 */
8599df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
8609df461ecSAndy Shevchenko 		/* case 1 is better */
8619df461ecSAndy Shevchenko 		r = r1;
8629df461ecSAndy Shevchenko 		q = q1;
8639df461ecSAndy Shevchenko 	} else {
8649df461ecSAndy Shevchenko 		/* case 2 is better */
8659df461ecSAndy Shevchenko 		r = r2;
8669df461ecSAndy Shevchenko 		q = q2;
8679df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
8689df461ecSAndy Shevchenko 	}
8699df461ecSAndy Shevchenko 
8703ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
8719df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
8729df461ecSAndy Shevchenko 		u64 fssp;
8739df461ecSAndy Shevchenko 		u32 m;
8749df461ecSAndy Shevchenko 
8759df461ecSAndy Shevchenko 		/* Calculate initial quot */
8763ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
8779df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
8789df461ecSAndy Shevchenko 
8799df461ecSAndy Shevchenko 		/* Get the remainder */
8809df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
8819df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
8829df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
8839df461ecSAndy Shevchenko 
8849df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
8859df461ecSAndy Shevchenko 		if (r1 < r) {
8869df461ecSAndy Shevchenko 			/* case 3 is better */
8879df461ecSAndy Shevchenko 			q = 1;
8889df461ecSAndy Shevchenko 			mul = m;
889e5262d05SWeike Chen 		}
890e5262d05SWeike Chen 	}
891e5262d05SWeike Chen 
8929df461ecSAndy Shevchenko 	*dds = mul;
8939df461ecSAndy Shevchenko 	return q - 1;
894e5262d05SWeike Chen }
895e5262d05SWeike Chen 
ssp_get_clk_div(struct driver_data * drv_data,int rate)8963343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
897ca632f55SGrant Likely {
89851eea52dSLubomir Rintel 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
8993343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
9003343b7a6SMika Westerberg 
9013343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
902ca632f55SGrant Likely 
90329f21337SFlavio Suligoi 	/*
90429f21337SFlavio Suligoi 	 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
9058083d6b8SAndy Shevchenko 	 * that the SSP transmission rate can be greater than the device rate.
90629f21337SFlavio Suligoi 	 */
907ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
90829f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
909ca632f55SGrant Likely 	else
91029f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
911ca632f55SGrant Likely }
912ca632f55SGrant Likely 
pxa2xx_ssp_get_clk_div(struct driver_data * drv_data,int rate)913e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
914d2c2f6a4SAndy Shevchenko 					   int rate)
915e5262d05SWeike Chen {
91696579a4eSJarkko Nikula 	struct chip_data *chip =
91751eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
918025ffe88SAndy Shevchenko 	unsigned int clk_div;
919e5262d05SWeike Chen 
920e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
921e5262d05SWeike Chen 	case QUARK_X1000_SSP:
9229df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
923eecacf73SDan Carpenter 		break;
924e5262d05SWeike Chen 	default:
925025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
926eecacf73SDan Carpenter 		break;
927e5262d05SWeike Chen 	}
928025ffe88SAndy Shevchenko 	return clk_div << 8;
929e5262d05SWeike Chen }
930e5262d05SWeike Chen 
pxa2xx_spi_can_dma(struct spi_controller * controller,struct spi_device * spi,struct spi_transfer * xfer)93151eea52dSLubomir Rintel static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
932b6ced294SJarkko Nikula 			       struct spi_device *spi,
933b6ced294SJarkko Nikula 			       struct spi_transfer *xfer)
934b6ced294SJarkko Nikula {
935b6ced294SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
936b6ced294SJarkko Nikula 
937b6ced294SJarkko Nikula 	return chip->enable_dma &&
938b6ced294SJarkko Nikula 	       xfer->len <= MAX_DMA_LEN &&
939b6ced294SJarkko Nikula 	       xfer->len >= chip->dma_burst_size;
940b6ced294SJarkko Nikula }
941b6ced294SJarkko Nikula 
pxa2xx_spi_transfer_one(struct spi_controller * controller,struct spi_device * spi,struct spi_transfer * transfer)94251eea52dSLubomir Rintel static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
943d5898e19SJarkko Nikula 				   struct spi_device *spi,
944d5898e19SJarkko Nikula 				   struct spi_transfer *transfer)
945ca632f55SGrant Likely {
94651eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
94751eea52dSLubomir Rintel 	struct spi_message *message = controller->cur_msg;
94820f4c379SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
94996579a4eSJarkko Nikula 	u32 dma_thresh = chip->dma_threshold;
95096579a4eSJarkko Nikula 	u32 dma_burst = chip->dma_burst_size;
95196579a4eSJarkko Nikula 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
952bffc967eSJarkko Nikula 	u32 clk_div;
953bffc967eSJarkko Nikula 	u8 bits;
954bffc967eSJarkko Nikula 	u32 speed;
955ca632f55SGrant Likely 	u32 cr0;
956ca632f55SGrant Likely 	u32 cr1;
9577d1f1bf6SAndy Shevchenko 	int err;
958b6ced294SJarkko Nikula 	int dma_mapped;
959ca632f55SGrant Likely 
960cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
961b6ced294SJarkko Nikula 	if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
962ca632f55SGrant Likely 
9638083d6b8SAndy Shevchenko 		/* Reject already-mapped transfers; PIO won't always work */
964ca632f55SGrant Likely 		if (message->is_dma_mapped
965ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
966748fbadfSJarkko Nikula 			dev_err(&spi->dev,
9678ae55af3SJarkko Nikula 				"Mapped transfer length of %u is greater than %d\n",
968ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
969d5898e19SJarkko Nikula 			return -EINVAL;
970ca632f55SGrant Likely 		}
971ca632f55SGrant Likely 
9728083d6b8SAndy Shevchenko 		/* Warn ... we force this to PIO mode */
97320f4c379SJarkko Nikula 		dev_warn_ratelimited(&spi->dev,
974684a3ac7SAndy Shevchenko 				     "DMA disabled for transfer length %u greater than %d\n",
975684a3ac7SAndy Shevchenko 				     transfer->len, MAX_DMA_LEN);
976ca632f55SGrant Likely 	}
977ca632f55SGrant Likely 
978ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
979cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
980748fbadfSJarkko Nikula 		dev_err(&spi->dev, "Flush failed\n");
981d5898e19SJarkko Nikula 		return -EIO;
982ca632f55SGrant Likely 	}
983ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
984ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
985ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
986ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
987ca632f55SGrant Likely 
988ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
989ca632f55SGrant Likely 	bits = transfer->bits_per_word;
990ca632f55SGrant Likely 	speed = transfer->speed_hz;
991ca632f55SGrant Likely 
992d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
993ca632f55SGrant Likely 
994ca632f55SGrant Likely 	if (bits <= 8) {
995ca632f55SGrant Likely 		drv_data->n_bytes = 1;
99644ec41b7SAndy Shevchenko 		drv_data->read = drv_data->rx ? u8_reader : null_reader;
99744ec41b7SAndy Shevchenko 		drv_data->write = drv_data->tx ? u8_writer : null_writer;
998ca632f55SGrant Likely 	} else if (bits <= 16) {
999ca632f55SGrant Likely 		drv_data->n_bytes = 2;
100044ec41b7SAndy Shevchenko 		drv_data->read = drv_data->rx ? u16_reader : null_reader;
100144ec41b7SAndy Shevchenko 		drv_data->write = drv_data->tx ? u16_writer : null_writer;
1002ca632f55SGrant Likely 	} else if (bits <= 32) {
1003ca632f55SGrant Likely 		drv_data->n_bytes = 4;
100444ec41b7SAndy Shevchenko 		drv_data->read = drv_data->rx ? u32_reader : null_reader;
100544ec41b7SAndy Shevchenko 		drv_data->write = drv_data->tx ? u32_writer : null_writer;
1006ca632f55SGrant Likely 	}
1007196b0e2cSJarkko Nikula 	/*
10088083d6b8SAndy Shevchenko 	 * If bits per word is changed in DMA mode, then must check
10098083d6b8SAndy Shevchenko 	 * the thresholds and burst also.
1010196b0e2cSJarkko Nikula 	 */
1011ca632f55SGrant Likely 	if (chip->enable_dma) {
1012cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
101320f4c379SJarkko Nikula 						spi,
1014ca632f55SGrant Likely 						bits, &dma_burst,
1015ca632f55SGrant Likely 						&dma_thresh))
101620f4c379SJarkko Nikula 			dev_warn_ratelimited(&spi->dev,
10178ae55af3SJarkko Nikula 					     "DMA burst size reduced to match bits_per_word\n");
1018ca632f55SGrant Likely 	}
1019ca632f55SGrant Likely 
102051eea52dSLubomir Rintel 	dma_mapped = controller->can_dma &&
102120f4c379SJarkko Nikula 		     controller->can_dma(controller, spi, transfer) &&
102251eea52dSLubomir Rintel 		     controller->cur_msg_mapped;
1023b6ced294SJarkko Nikula 	if (dma_mapped) {
1024ca632f55SGrant Likely 
1025ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
1026cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1027ca632f55SGrant Likely 
1028d5898e19SJarkko Nikula 		err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1029d5898e19SJarkko Nikula 		if (err)
1030d5898e19SJarkko Nikula 			return err;
1031ca632f55SGrant Likely 
1032ca632f55SGrant Likely 		/* Clear status and start DMA engine */
1033ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1034c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1035cd7bed00SMika Westerberg 
1036cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
1037ca632f55SGrant Likely 	} else {
1038ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
1039ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
1040ca632f55SGrant Likely 
1041ca632f55SGrant Likely 		/* Clear status  */
1042ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1043ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1044ca632f55SGrant Likely 	}
1045ca632f55SGrant Likely 
1046ee03672dSJarkko Nikula 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1047ee03672dSJarkko Nikula 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1048ee03672dSJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
104920f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
105051eea52dSLubomir Rintel 			controller->max_speed_hz
1051ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1052b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1053ee03672dSJarkko Nikula 	else
105420f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
105551eea52dSLubomir Rintel 			controller->max_speed_hz / 2
1056ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1057b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1058ee03672dSJarkko Nikula 
1059a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
10601bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold);
10611bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold);
1062a0d2642eSMika Westerberg 	}
1063a0d2642eSMika Westerberg 
10643fdb59cfSAndy Shevchenko 	if (is_mrfld_ssp(drv_data)) {
106570252440SAndy Shevchenko 		u32 mask = SFIFOTT_RFT | SFIFOTT_TFT;
10663fdb59cfSAndy Shevchenko 		u32 thresh = 0;
10673fdb59cfSAndy Shevchenko 
10683fdb59cfSAndy Shevchenko 		thresh |= SFIFOTT_RxThresh(chip->lpss_rx_threshold);
10693fdb59cfSAndy Shevchenko 		thresh |= SFIFOTT_TxThresh(chip->lpss_tx_threshold);
10703fdb59cfSAndy Shevchenko 
107170252440SAndy Shevchenko 		pxa2xx_spi_update(drv_data, SFIFOTT, mask, thresh);
10723fdb59cfSAndy Shevchenko 	}
10733fdb59cfSAndy Shevchenko 
10741bed378cSAndy Shevchenko 	if (is_quark_x1000_ssp(drv_data))
10751bed378cSAndy Shevchenko 		pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate);
1076e5262d05SWeike Chen 
10770c8ccd8bSAndy Shevchenko 	/* Stop the SSP */
10780c8ccd8bSAndy Shevchenko 	if (!is_mmp2_ssp(drv_data))
10790c8ccd8bSAndy Shevchenko 		pxa_ssp_disable(drv_data->ssp);
10800c8ccd8bSAndy Shevchenko 
10810c8ccd8bSAndy Shevchenko 	if (!pxa25x_ssp_comp(drv_data))
10820c8ccd8bSAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
10830c8ccd8bSAndy Shevchenko 
10848083d6b8SAndy Shevchenko 	/* First set CR1 without interrupt and service enables */
10851bed378cSAndy Shevchenko 	pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1);
10861bed378cSAndy Shevchenko 
10878083d6b8SAndy Shevchenko 	/* See if we need to reload the configuration registers */
10881bed378cSAndy Shevchenko 	pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0);
1089ca632f55SGrant Likely 
10900c8ccd8bSAndy Shevchenko 	/* Restart the SSP */
10910c8ccd8bSAndy Shevchenko 	pxa_ssp_enable(drv_data->ssp);
10920c8ccd8bSAndy Shevchenko 
109341c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data)) {
10946d380132SAndy Shevchenko 		u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8;
109582391856SLubomir Rintel 
109682391856SLubomir Rintel 		if (tx_level) {
10978083d6b8SAndy Shevchenko 			/* On MMP2, flipping SSE doesn't to empty Tx FIFO. */
1098684a3ac7SAndy Shevchenko 			dev_warn(&spi->dev, "%u bytes of garbage in Tx FIFO!\n", tx_level);
109982391856SLubomir Rintel 			if (tx_level > transfer->len)
110082391856SLubomir Rintel 				tx_level = transfer->len;
110182391856SLubomir Rintel 			drv_data->tx += tx_level;
110282391856SLubomir Rintel 		}
110382391856SLubomir Rintel 	}
110482391856SLubomir Rintel 
1105*60ba4431SYang Yingliang 	if (spi_controller_is_target(controller)) {
1106ec93cb6fSLubomir Rintel 		while (drv_data->write(drv_data))
1107ec93cb6fSLubomir Rintel 			;
110877d33897SLubomir Rintel 		if (drv_data->gpiod_ready) {
110977d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 1);
111077d33897SLubomir Rintel 			udelay(1);
111177d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 0);
111277d33897SLubomir Rintel 		}
1113ec93cb6fSLubomir Rintel 	}
1114ec93cb6fSLubomir Rintel 
1115d5898e19SJarkko Nikula 	/*
1116d5898e19SJarkko Nikula 	 * Release the data by enabling service requests and interrupts,
11178083d6b8SAndy Shevchenko 	 * without changing any mode bits.
1118d5898e19SJarkko Nikula 	 */
1119c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1120d5898e19SJarkko Nikula 
1121d5898e19SJarkko Nikula 	return 1;
1122ca632f55SGrant Likely }
1123ca632f55SGrant Likely 
pxa2xx_spi_target_abort(struct spi_controller * controller)1124*60ba4431SYang Yingliang static int pxa2xx_spi_target_abort(struct spi_controller *controller)
1125ec93cb6fSLubomir Rintel {
112651eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1127ec93cb6fSLubomir Rintel 
11284761d2e7SAndy Shevchenko 	int_error_stop(drv_data, "transfer aborted", -EINTR);
1129ec93cb6fSLubomir Rintel 
1130ec93cb6fSLubomir Rintel 	return 0;
1131ec93cb6fSLubomir Rintel }
1132ec93cb6fSLubomir Rintel 
pxa2xx_spi_handle_err(struct spi_controller * controller,struct spi_message * msg)113351eea52dSLubomir Rintel static void pxa2xx_spi_handle_err(struct spi_controller *controller,
11347f86bde9SMika Westerberg 				 struct spi_message *msg)
1135ca632f55SGrant Likely {
113651eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1137ca632f55SGrant Likely 
11383bbdc083SAndy Shevchenko 	int_stop_and_reset(drv_data);
11393bbdc083SAndy Shevchenko 
1140d5898e19SJarkko Nikula 	/* Disable the SSP */
114129d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
1142ca632f55SGrant Likely 
1143d5898e19SJarkko Nikula 	/*
1144d5898e19SJarkko Nikula 	 * Stop the DMA if running. Note DMA callback handler may have unset
1145d5898e19SJarkko Nikula 	 * the dma_running already, which is fine as stopping is not needed
1146d5898e19SJarkko Nikula 	 * then but we shouldn't rely this flag for anything else than
1147d5898e19SJarkko Nikula 	 * stopping. For instance to differentiate between PIO and DMA
1148d5898e19SJarkko Nikula 	 * transfers.
1149d5898e19SJarkko Nikula 	 */
1150d5898e19SJarkko Nikula 	if (atomic_read(&drv_data->dma_running))
1151d5898e19SJarkko Nikula 		pxa2xx_spi_dma_stop(drv_data);
1152ca632f55SGrant Likely }
1153ca632f55SGrant Likely 
pxa2xx_spi_unprepare_transfer(struct spi_controller * controller)115451eea52dSLubomir Rintel static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
11557d94a505SMika Westerberg {
115651eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
11577d94a505SMika Westerberg 
11587d94a505SMika Westerberg 	/* Disable the SSP now */
115929d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
11607d94a505SMika Westerberg 
11617d94a505SMika Westerberg 	return 0;
11627d94a505SMika Westerberg }
11637d94a505SMika Westerberg 
setup(struct spi_device * spi)1164ca632f55SGrant Likely static int setup(struct spi_device *spi)
1165ca632f55SGrant Likely {
1166bffc967eSJarkko Nikula 	struct pxa2xx_spi_chip *chip_info;
1167ca632f55SGrant Likely 	struct chip_data *chip;
1168dccf7369SJarkko Nikula 	const struct lpss_config *config;
11693cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
11703cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1171a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1172a0d2642eSMika Westerberg 
1173e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1174e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1175e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1176e5262d05SWeike Chen 		tx_hi_thres = 0;
1177e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1178e5262d05SWeike Chen 		break;
11793fdb59cfSAndy Shevchenko 	case MRFLD_SSP:
11803fdb59cfSAndy Shevchenko 		tx_thres = TX_THRESH_MRFLD_DFLT;
11813fdb59cfSAndy Shevchenko 		tx_hi_thres = 0;
11823fdb59cfSAndy Shevchenko 		rx_thres = RX_THRESH_MRFLD_DFLT;
11833fdb59cfSAndy Shevchenko 		break;
11847c7289a4SAndy Shevchenko 	case CE4100_SSP:
11857c7289a4SAndy Shevchenko 		tx_thres = TX_THRESH_CE4100_DFLT;
11867c7289a4SAndy Shevchenko 		tx_hi_thres = 0;
11877c7289a4SAndy Shevchenko 		rx_thres = RX_THRESH_CE4100_DFLT;
11887c7289a4SAndy Shevchenko 		break;
118903fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
119003fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
119130f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
119234cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1193b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1194fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
1195dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1196dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1197dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1198dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1199e5262d05SWeike Chen 		break;
1200e5262d05SWeike Chen 	default:
1201a0d2642eSMika Westerberg 		tx_hi_thres = 0;
1202*60ba4431SYang Yingliang 		if (spi_controller_is_target(drv_data->controller)) {
1203ec93cb6fSLubomir Rintel 			tx_thres = 1;
1204ec93cb6fSLubomir Rintel 			rx_thres = 2;
1205ec93cb6fSLubomir Rintel 		} else {
1206ec93cb6fSLubomir Rintel 			tx_thres = TX_THRESH_DFLT;
1207a0d2642eSMika Westerberg 			rx_thres = RX_THRESH_DFLT;
1208ec93cb6fSLubomir Rintel 		}
1209e5262d05SWeike Chen 		break;
1210a0d2642eSMika Westerberg 	}
1211ca632f55SGrant Likely 
12128083d6b8SAndy Shevchenko 	/* Only allocate on the first setup */
1213ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1214ca632f55SGrant Likely 	if (!chip) {
1215ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
12169deae459SJingoo Han 		if (!chip)
1217ca632f55SGrant Likely 			return -ENOMEM;
1218ca632f55SGrant Likely 
1219ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
12209e264f3fSAmit Kumar Mahapatra via Alsa-devel 			if (spi_get_chipselect(spi, 0) > 4) {
1221f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1222f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1223ca632f55SGrant Likely 				kfree(chip);
1224ca632f55SGrant Likely 				return -EINVAL;
1225ca632f55SGrant Likely 			}
1226c18d925fSJan Kiszka 		}
122751eea52dSLubomir Rintel 		chip->enable_dma = drv_data->controller_info->enable_dma;
1228ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1229ca632f55SGrant Likely 	}
1230ca632f55SGrant Likely 
12318083d6b8SAndy Shevchenko 	/*
12328083d6b8SAndy Shevchenko 	 * Protocol drivers may change the chip settings, so...
12338083d6b8SAndy Shevchenko 	 * if chip_info exists, use it.
12348083d6b8SAndy Shevchenko 	 */
1235ca632f55SGrant Likely 	chip_info = spi->controller_data;
1236ca632f55SGrant Likely 
1237ca632f55SGrant Likely 	/* chip_info isn't always needed */
1238ca632f55SGrant Likely 	if (chip_info) {
1239ca632f55SGrant Likely 		if (chip_info->timeout)
1240ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1241ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1242ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1243a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1244a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1245ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1246ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1247ca632f55SGrant Likely 		chip->dma_threshold = 0;
1248ca632f55SGrant Likely 	}
12498393961cSAndy Shevchenko 
12508393961cSAndy Shevchenko 	chip->cr1 = 0;
1251*60ba4431SYang Yingliang 	if (spi_controller_is_target(drv_data->controller)) {
1252ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCFR;
1253ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCLKDIR;
1254ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SFRMDIR;
1255ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SPH;
1256ec93cb6fSLubomir Rintel 	}
1257ca632f55SGrant Likely 
12583fdb59cfSAndy Shevchenko 	if (is_lpss_ssp(drv_data)) {
1259a0d2642eSMika Westerberg 		chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
12603fdb59cfSAndy Shevchenko 		chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
12613fdb59cfSAndy Shevchenko 					  SSITF_TxHiThresh(tx_hi_thres);
12623fdb59cfSAndy Shevchenko 	}
12633fdb59cfSAndy Shevchenko 
12643fdb59cfSAndy Shevchenko 	if (is_mrfld_ssp(drv_data)) {
12653fdb59cfSAndy Shevchenko 		chip->lpss_rx_threshold = rx_thres;
12663fdb59cfSAndy Shevchenko 		chip->lpss_tx_threshold = tx_thres;
12673fdb59cfSAndy Shevchenko 	}
1268a0d2642eSMika Westerberg 
12698083d6b8SAndy Shevchenko 	/*
12708083d6b8SAndy Shevchenko 	 * Set DMA burst and threshold outside of chip_info path so that if
12718083d6b8SAndy Shevchenko 	 * chip_info goes away after setting chip->enable_dma, the burst and
12728083d6b8SAndy Shevchenko 	 * threshold can still respond to changes in bits_per_word.
12738083d6b8SAndy Shevchenko 	 */
1274ca632f55SGrant Likely 	if (chip->enable_dma) {
12758083d6b8SAndy Shevchenko 		/* Set up legal burst and threshold for DMA */
1276cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1277cd7bed00SMika Westerberg 						spi->bits_per_word,
1278ca632f55SGrant Likely 						&chip->dma_burst_size,
1279ca632f55SGrant Likely 						&chip->dma_threshold)) {
1280f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1281f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1282ca632f55SGrant Likely 		}
1283000c6af4SAndy Shevchenko 		dev_dbg(&spi->dev,
1284000c6af4SAndy Shevchenko 			"in setup: DMA burst size set to %u\n",
1285000c6af4SAndy Shevchenko 			chip->dma_burst_size);
1286ca632f55SGrant Likely 	}
1287ca632f55SGrant Likely 
1288e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1289e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1290e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1291e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1292e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1293e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1294e5262d05SWeike Chen 		break;
12957c7289a4SAndy Shevchenko 	case CE4100_SSP:
12967c7289a4SAndy Shevchenko 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
12977c7289a4SAndy Shevchenko 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
12987c7289a4SAndy Shevchenko 		break;
1299e5262d05SWeike Chen 	default:
1300e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1301e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1302e5262d05SWeike Chen 		break;
1303e5262d05SWeike Chen 	}
1304e5262d05SWeike Chen 
1305ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1306eb743ec6SAndy Shevchenko 	chip->cr1 |= ((spi->mode & SPI_CPHA) ? SSCR1_SPH : 0) |
1307eb743ec6SAndy Shevchenko 		     ((spi->mode & SPI_CPOL) ? SSCR1_SPO : 0);
1308ca632f55SGrant Likely 
1309b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1310b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1311b833172fSMika Westerberg 
1312ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1313ca632f55SGrant Likely 
1314ca632f55SGrant Likely 	return 0;
1315ca632f55SGrant Likely }
1316ca632f55SGrant Likely 
cleanup(struct spi_device * spi)1317ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1318ca632f55SGrant Likely {
1319ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
1320ca632f55SGrant Likely 
1321ca632f55SGrant Likely 	kfree(chip);
1322ca632f55SGrant Likely }
1323ca632f55SGrant Likely 
pxa2xx_spi_idma_filter(struct dma_chan * chan,void * param)132434cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
132534cadd9cSJarkko Nikula {
13265ba846b1SAndy Shevchenko 	return param == chan->device->dev;
132734cadd9cSJarkko Nikula }
132834cadd9cSJarkko Nikula 
132951eea52dSLubomir Rintel static struct pxa2xx_spi_controller *
pxa2xx_spi_init_pdata(struct platform_device * pdev)13300db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1331a3496855SMika Westerberg {
133251eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *pdata;
13332990f3a8SAndy Shevchenko 	struct device *dev = &pdev->dev;
13342990f3a8SAndy Shevchenko 	struct device *parent = dev->parent;
1335a3496855SMika Westerberg 	struct ssp_device *ssp;
1336a3496855SMika Westerberg 	struct resource *res;
133788a94721SAndy Shevchenko 	enum pxa_ssp_type type = SSP_UNDEFINED;
1338f2faa3ecSAndy Shevchenko 	const void *match;
13391a1864cdSAndy Shevchenko 	bool is_lpss_priv;
13402990f3a8SAndy Shevchenko 	int status;
13412990f3a8SAndy Shevchenko 	u64 uid;
1342a3496855SMika Westerberg 
13431a1864cdSAndy Shevchenko 	is_lpss_priv = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lpss_priv");
13441a1864cdSAndy Shevchenko 
13458fc8250aSAndy Shevchenko 	match = device_get_match_data(dev);
1346f2faa3ecSAndy Shevchenko 	if (match)
134794d6cf7eSKrzysztof Kozlowski 		type = (uintptr_t)match;
13481a1864cdSAndy Shevchenko 	else if (is_lpss_priv) {
13491a1864cdSAndy Shevchenko 		u32 value;
13501a1864cdSAndy Shevchenko 
13511a1864cdSAndy Shevchenko 		status = device_property_read_u32(dev, "intel,spi-pxa2xx-type", &value);
13521a1864cdSAndy Shevchenko 		if (status)
13531a1864cdSAndy Shevchenko 			return ERR_PTR(status);
13541a1864cdSAndy Shevchenko 
13551a1864cdSAndy Shevchenko 		type = (enum pxa_ssp_type)value;
13561a1864cdSAndy Shevchenko 	}
135788a94721SAndy Shevchenko 
135888a94721SAndy Shevchenko 	/* Validate the SSP type correctness */
135988a94721SAndy Shevchenko 	if (!(type > SSP_UNDEFINED && type < SSP_MAX))
136014af1df3SAndy Shevchenko 		return ERR_PTR(-EINVAL);
136103fbf488SJarkko Nikula 
13628fc8250aSAndy Shevchenko 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
13639deae459SJingoo Han 	if (!pdata)
136414af1df3SAndy Shevchenko 		return ERR_PTR(-ENOMEM);
1365a3496855SMika Westerberg 
1366a3496855SMika Westerberg 	ssp = &pdata->ssp;
1367a3496855SMika Westerberg 
1368e3b7fca3SAndy Shevchenko 	ssp->mmio_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1369cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
137014af1df3SAndy Shevchenko 		return ERR_CAST(ssp->mmio_base);
1371a3496855SMika Westerberg 
137277c544d2SAndy Shevchenko 	ssp->phys_base = res->start;
137377c544d2SAndy Shevchenko 
137407c33792SAndy Shevchenko 	/* Platforms with iDMA 64-bit */
137507c33792SAndy Shevchenko 	if (is_lpss_priv) {
13766fb7427dSAndy Shevchenko 		pdata->tx_param = parent;
13776fb7427dSAndy Shevchenko 		pdata->rx_param = parent;
137834cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
137934cadd9cSJarkko Nikula 	}
138034cadd9cSJarkko Nikula 
13818fc8250aSAndy Shevchenko 	ssp->clk = devm_clk_get(dev, NULL);
13825eb263efSChuhong Yuan 	if (IS_ERR(ssp->clk))
138314af1df3SAndy Shevchenko 		return ERR_CAST(ssp->clk);
1384a3496855SMika Westerberg 
1385a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
13865eb263efSChuhong Yuan 	if (ssp->irq < 0)
138714af1df3SAndy Shevchenko 		return ERR_PTR(ssp->irq);
13885eb263efSChuhong Yuan 
1389a3496855SMika Westerberg 	ssp->type = type;
13908fc8250aSAndy Shevchenko 	ssp->dev = dev;
13912990f3a8SAndy Shevchenko 
13922990f3a8SAndy Shevchenko 	status = acpi_dev_uid_to_integer(ACPI_COMPANION(dev), &uid);
13932990f3a8SAndy Shevchenko 	if (status)
13942990f3a8SAndy Shevchenko 		ssp->port_id = -1;
13952990f3a8SAndy Shevchenko 	else
13962990f3a8SAndy Shevchenko 		ssp->port_id = uid;
1397a3496855SMika Westerberg 
1398*60ba4431SYang Yingliang 	pdata->is_target = device_property_read_bool(dev, "spi-slave");
1399a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1400cddb339bSMika Westerberg 	pdata->enable_dma = true;
140137821a82SAndy Shevchenko 	pdata->dma_burst_size = 1;
1402a3496855SMika Westerberg 
1403a3496855SMika Westerberg 	return pdata;
1404a3496855SMika Westerberg }
1405a3496855SMika Westerberg 
pxa2xx_spi_fw_translate_cs(struct spi_controller * controller,unsigned int cs)140651eea52dSLubomir Rintel static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
14073cc7b0e3SJarkko Nikula 				      unsigned int cs)
14080c27d9cfSMika Westerberg {
140951eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
14100c27d9cfSMika Westerberg 
1411c3dce24cSAndy Shevchenko 	if (has_acpi_companion(drv_data->ssp->dev)) {
14120c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
14130c27d9cfSMika Westerberg 		/*
14140c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
14150c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
14160c27d9cfSMika Westerberg 		 * to match what Linux expects.
14170c27d9cfSMika Westerberg 		 */
14180c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
141930f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
14200c27d9cfSMika Westerberg 			return cs - 1;
14210c27d9cfSMika Westerberg 
14220c27d9cfSMika Westerberg 		default:
14230c27d9cfSMika Westerberg 			break;
14240c27d9cfSMika Westerberg 		}
14250c27d9cfSMika Westerberg 	}
14260c27d9cfSMika Westerberg 
14270c27d9cfSMika Westerberg 	return cs;
14280c27d9cfSMika Westerberg }
14290c27d9cfSMika Westerberg 
pxa2xx_spi_max_dma_transfer_size(struct spi_device * spi)1430b2662a16SDaniel Vetter static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1431b2662a16SDaniel Vetter {
1432b2662a16SDaniel Vetter 	return MAX_DMA_LEN;
1433b2662a16SDaniel Vetter }
1434b2662a16SDaniel Vetter 
pxa2xx_spi_probe(struct platform_device * pdev)1435fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1436ca632f55SGrant Likely {
1437ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
143851eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *platform_info;
143951eea52dSLubomir Rintel 	struct spi_controller *controller;
1440ca632f55SGrant Likely 	struct driver_data *drv_data;
1441ca632f55SGrant Likely 	struct ssp_device *ssp;
14428b136baaSJarkko Nikula 	const struct lpss_config *config;
1443778c12e6SAndy Shevchenko 	int status;
1444c039dd27SJarkko Nikula 	u32 tmp;
1445ca632f55SGrant Likely 
1446851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1447851bacf5SMika Westerberg 	if (!platform_info) {
14480db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
144914af1df3SAndy Shevchenko 		if (IS_ERR(platform_info)) {
1450851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
145114af1df3SAndy Shevchenko 			return PTR_ERR(platform_info);
1452851bacf5SMika Westerberg 		}
1453a3496855SMika Westerberg 	}
1454ca632f55SGrant Likely 
1455ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1456851bacf5SMika Westerberg 	if (!ssp)
1457851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1458851bacf5SMika Westerberg 
1459851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
14608083d6b8SAndy Shevchenko 		dev_err(&pdev->dev, "failed to get SSP\n");
1461ca632f55SGrant Likely 		return -ENODEV;
1462ca632f55SGrant Likely 	}
1463ca632f55SGrant Likely 
1464*60ba4431SYang Yingliang 	if (platform_info->is_target)
1465*60ba4431SYang Yingliang 		controller = devm_spi_alloc_target(dev, sizeof(*drv_data));
1466ec93cb6fSLubomir Rintel 	else
1467*60ba4431SYang Yingliang 		controller = devm_spi_alloc_host(dev, sizeof(*drv_data));
1468ec93cb6fSLubomir Rintel 
146951eea52dSLubomir Rintel 	if (!controller) {
147051eea52dSLubomir Rintel 		dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1471f2eed8caSAndy Shevchenko 		status = -ENOMEM;
1472f2eed8caSAndy Shevchenko 		goto out_error_controller_alloc;
1473ca632f55SGrant Likely 	}
147451eea52dSLubomir Rintel 	drv_data = spi_controller_get_devdata(controller);
147551eea52dSLubomir Rintel 	drv_data->controller = controller;
147651eea52dSLubomir Rintel 	drv_data->controller_info = platform_info;
1477ca632f55SGrant Likely 	drv_data->ssp = ssp;
1478ca632f55SGrant Likely 
147912baee68SAndy Shevchenko 	device_set_node(&controller->dev, dev_fwnode(dev));
148094acf807SAndy Shevchenko 
14818083d6b8SAndy Shevchenko 	/* The spi->mode bits understood by this driver: */
148251eea52dSLubomir Rintel 	controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1483ca632f55SGrant Likely 
148451eea52dSLubomir Rintel 	controller->bus_num = ssp->port_id;
148551eea52dSLubomir Rintel 	controller->dma_alignment = DMA_ALIGNMENT;
148651eea52dSLubomir Rintel 	controller->cleanup = cleanup;
148751eea52dSLubomir Rintel 	controller->setup = setup;
148851eea52dSLubomir Rintel 	controller->set_cs = pxa2xx_spi_set_cs;
148951eea52dSLubomir Rintel 	controller->transfer_one = pxa2xx_spi_transfer_one;
1490*60ba4431SYang Yingliang 	controller->target_abort = pxa2xx_spi_target_abort;
149151eea52dSLubomir Rintel 	controller->handle_err = pxa2xx_spi_handle_err;
149251eea52dSLubomir Rintel 	controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
149351eea52dSLubomir Rintel 	controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
149451eea52dSLubomir Rintel 	controller->auto_runtime_pm = true;
149551eea52dSLubomir Rintel 	controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1496ca632f55SGrant Likely 
1497ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1498ca632f55SGrant Likely 
1499ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1500e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1501e5262d05SWeike Chen 		case QUARK_X1000_SSP:
150251eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1503e5262d05SWeike Chen 			break;
1504e5262d05SWeike Chen 		default:
150551eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1506e5262d05SWeike Chen 			break;
1507e5262d05SWeike Chen 		}
1508e5262d05SWeike Chen 
1509ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1510ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1511ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1512ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1513ca632f55SGrant Likely 	} else {
151451eea52dSLubomir Rintel 		controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1515ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
15165928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1517ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1518ec93cb6fSLubomir Rintel 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1519ec93cb6fSLubomir Rintel 						| SSSR_ROR | SSSR_TUR;
1520ca632f55SGrant Likely 	}
1521ca632f55SGrant Likely 
1522ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1523ca632f55SGrant Likely 			drv_data);
1524ca632f55SGrant Likely 	if (status < 0) {
1525ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
152651eea52dSLubomir Rintel 		goto out_error_controller_alloc;
1527ca632f55SGrant Likely 	}
1528ca632f55SGrant Likely 
1529ca632f55SGrant Likely 	/* Setup DMA if requested */
1530ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1531cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1532cd7bed00SMika Westerberg 		if (status) {
15338b57b11bSFlavio Suligoi 			dev_warn(dev, "no DMA channels available, using PIO\n");
1534cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1535b6ced294SJarkko Nikula 		} else {
153651eea52dSLubomir Rintel 			controller->can_dma = pxa2xx_spi_can_dma;
1537bf9f742cSMark Brown 			controller->max_dma_len = MAX_DMA_LEN;
1538b2662a16SDaniel Vetter 			controller->max_transfer_size =
1539b2662a16SDaniel Vetter 				pxa2xx_spi_max_dma_transfer_size;
1540ca632f55SGrant Likely 		}
1541ca632f55SGrant Likely 	}
1542ca632f55SGrant Likely 
1543ca632f55SGrant Likely 	/* Enable SOC clock */
154462bbc864STobias Jordan 	status = clk_prepare_enable(ssp->clk);
154562bbc864STobias Jordan 	if (status)
154662bbc864STobias Jordan 		goto out_error_dma_irq_alloc;
15473343b7a6SMika Westerberg 
154851eea52dSLubomir Rintel 	controller->max_speed_hz = clk_get_rate(ssp->clk);
154923cdddb2SJarkko Nikula 	/*
155023cdddb2SJarkko Nikula 	 * Set minimum speed for all other platforms than Intel Quark which is
155123cdddb2SJarkko Nikula 	 * able do under 1 Hz transfers.
155223cdddb2SJarkko Nikula 	 */
155323cdddb2SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
155423cdddb2SJarkko Nikula 		controller->min_speed_hz =
155523cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 4096);
155623cdddb2SJarkko Nikula 	else if (!is_quark_x1000_ssp(drv_data))
155723cdddb2SJarkko Nikula 		controller->min_speed_hz =
155823cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 512);
1559ca632f55SGrant Likely 
15600c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
15610c8ccd8bSAndy Shevchenko 
1562ca632f55SGrant Likely 	/* Load default SSP configuration */
1563e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1564e5262d05SWeike Chen 	case QUARK_X1000_SSP:
15657c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
15667c7289a4SAndy Shevchenko 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1567c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1568e5262d05SWeike Chen 
15698083d6b8SAndy Shevchenko 		/* Using the Motorola SPI protocol and use 8 bit frame */
15707c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
15717c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1572e5262d05SWeike Chen 		break;
15737c7289a4SAndy Shevchenko 	case CE4100_SSP:
15747c7289a4SAndy Shevchenko 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
15757c7289a4SAndy Shevchenko 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
15767c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
15777c7289a4SAndy Shevchenko 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
15787c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1579a2dd8af0SAndy Shevchenko 		break;
1580e5262d05SWeike Chen 	default:
1581ec93cb6fSLubomir Rintel 
1582*60ba4431SYang Yingliang 		if (spi_controller_is_target(controller)) {
1583ec93cb6fSLubomir Rintel 			tmp = SSCR1_SCFR |
1584ec93cb6fSLubomir Rintel 			      SSCR1_SCLKDIR |
1585ec93cb6fSLubomir Rintel 			      SSCR1_SFRMDIR |
1586ec93cb6fSLubomir Rintel 			      SSCR1_RxTresh(2) |
1587ec93cb6fSLubomir Rintel 			      SSCR1_TxTresh(1) |
1588ec93cb6fSLubomir Rintel 			      SSCR1_SPH;
1589ec93cb6fSLubomir Rintel 		} else {
1590c039dd27SJarkko Nikula 			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1591c039dd27SJarkko Nikula 			      SSCR1_TxTresh(TX_THRESH_DFLT);
1592ec93cb6fSLubomir Rintel 		}
1593c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1594ec93cb6fSLubomir Rintel 		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
1595*60ba4431SYang Yingliang 		if (!spi_controller_is_target(controller))
1596ec93cb6fSLubomir Rintel 			tmp |= SSCR0_SCR(2);
1597c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1598e5262d05SWeike Chen 		break;
1599e5262d05SWeike Chen 	}
1600e5262d05SWeike Chen 
1601ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1602c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1603e5262d05SWeike Chen 
1604e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1605c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1606ca632f55SGrant Likely 
16078b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
16088b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
16098b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
16108b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
16118b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
16128b136baaSJarkko Nikula 						   config->reg_capabilities);
16138b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
16148b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
16158b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
161630f3a6abSMika Westerberg 		} else if (config->cs_num) {
161730f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
16188b136baaSJarkko Nikula 		}
16198b136baaSJarkko Nikula 	}
162051eea52dSLubomir Rintel 	controller->num_chipselect = platform_info->num_chipselect;
1621778c12e6SAndy Shevchenko 	controller->use_gpio_descriptors = true;
16226ac5a435SAndy Shevchenko 
1623*60ba4431SYang Yingliang 	if (platform_info->is_target) {
162477d33897SLubomir Rintel 		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
162577d33897SLubomir Rintel 						"ready", GPIOD_OUT_LOW);
162677d33897SLubomir Rintel 		if (IS_ERR(drv_data->gpiod_ready)) {
162777d33897SLubomir Rintel 			status = PTR_ERR(drv_data->gpiod_ready);
162877d33897SLubomir Rintel 			goto out_error_clock_enabled;
162977d33897SLubomir Rintel 		}
163077d33897SLubomir Rintel 	}
163177d33897SLubomir Rintel 
1632836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1633836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1634836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1635836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1636836d1a22SAntonio Ospite 
1637ca632f55SGrant Likely 	/* Register with the SPI framework */
1638ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
163932e5b572SLukas Wunner 	status = spi_register_controller(controller);
1640eb743ec6SAndy Shevchenko 	if (status) {
16418083d6b8SAndy Shevchenko 		dev_err(&pdev->dev, "problem registering SPI controller\n");
164212742045SLubomir Rintel 		goto out_error_pm_runtime_enabled;
1643ca632f55SGrant Likely 	}
1644ca632f55SGrant Likely 
1645ca632f55SGrant Likely 	return status;
1646ca632f55SGrant Likely 
164712742045SLubomir Rintel out_error_pm_runtime_enabled:
1648e2b714afSJarkko Nikula 	pm_runtime_disable(&pdev->dev);
164912742045SLubomir Rintel 
165012742045SLubomir Rintel out_error_clock_enabled:
16513343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
165262bbc864STobias Jordan 
165362bbc864STobias Jordan out_error_dma_irq_alloc:
1654cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1655ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1656ca632f55SGrant Likely 
165751eea52dSLubomir Rintel out_error_controller_alloc:
1658ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1659ca632f55SGrant Likely 	return status;
1660ca632f55SGrant Likely }
1661ca632f55SGrant Likely 
pxa2xx_spi_remove(struct platform_device * pdev)166231f6d96dSUwe Kleine-König static void pxa2xx_spi_remove(struct platform_device *pdev)
1663ca632f55SGrant Likely {
1664ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
16653d24b2a4SAndy Shevchenko 	struct ssp_device *ssp = drv_data->ssp;
1666ca632f55SGrant Likely 
16677d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
16687d94a505SMika Westerberg 
166932e5b572SLukas Wunner 	spi_unregister_controller(drv_data->controller);
167032e5b572SLukas Wunner 
1671ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
16720c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
16733343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1674ca632f55SGrant Likely 
1675ca632f55SGrant Likely 	/* Release DMA */
167651eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma)
1677cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1678ca632f55SGrant Likely 
16797d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
16807d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
16817d94a505SMika Westerberg 
1682ca632f55SGrant Likely 	/* Release IRQ */
1683ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1684ca632f55SGrant Likely 
1685ca632f55SGrant Likely 	/* Release SSP */
1686ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1687ca632f55SGrant Likely }
1688ca632f55SGrant Likely 
pxa2xx_spi_suspend(struct device * dev)1689ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1690ca632f55SGrant Likely {
1691ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1692ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1693bffc967eSJarkko Nikula 	int status;
1694ca632f55SGrant Likely 
169551eea52dSLubomir Rintel 	status = spi_controller_suspend(drv_data->controller);
1696eb743ec6SAndy Shevchenko 	if (status)
1697ca632f55SGrant Likely 		return status;
16980c8ccd8bSAndy Shevchenko 
16990c8ccd8bSAndy Shevchenko 	pxa_ssp_disable(ssp);
17002b9375b9SDmitry Eremin-Solenikov 
17012b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
17023343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1703ca632f55SGrant Likely 
1704ca632f55SGrant Likely 	return 0;
1705ca632f55SGrant Likely }
1706ca632f55SGrant Likely 
pxa2xx_spi_resume(struct device * dev)1707ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1708ca632f55SGrant Likely {
1709ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1710ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1711bffc967eSJarkko Nikula 	int status;
1712ca632f55SGrant Likely 
1713ca632f55SGrant Likely 	/* Enable the SSP clock */
171462bbc864STobias Jordan 	if (!pm_runtime_suspended(dev)) {
171562bbc864STobias Jordan 		status = clk_prepare_enable(ssp->clk);
171662bbc864STobias Jordan 		if (status)
171762bbc864STobias Jordan 			return status;
171862bbc864STobias Jordan 	}
1719ca632f55SGrant Likely 
1720ca632f55SGrant Likely 	/* Start the queue running */
172151eea52dSLubomir Rintel 	return spi_controller_resume(drv_data->controller);
1722ca632f55SGrant Likely }
17237d94a505SMika Westerberg 
pxa2xx_spi_runtime_suspend(struct device * dev)17247d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
17257d94a505SMika Westerberg {
17267d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
17277d94a505SMika Westerberg 
17287d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
17297d94a505SMika Westerberg 	return 0;
17307d94a505SMika Westerberg }
17317d94a505SMika Westerberg 
pxa2xx_spi_runtime_resume(struct device * dev)17327d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
17337d94a505SMika Westerberg {
17347d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
17357d94a505SMika Westerberg 
1736d294e99cSye xingchen 	return clk_prepare_enable(drv_data->ssp->clk);
17377d94a505SMika Westerberg }
1738ca632f55SGrant Likely 
1739ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
17406c3c438cSAndy Shevchenko 	SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
17416c3c438cSAndy Shevchenko 	RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend, pxa2xx_spi_runtime_resume, NULL)
1742ca632f55SGrant Likely };
1743ca632f55SGrant Likely 
17440e1f0b1cSAndy Shevchenko #ifdef CONFIG_ACPI
17450e1f0b1cSAndy Shevchenko static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
17460e1f0b1cSAndy Shevchenko 	{ "80860F0E", LPSS_BYT_SSP },
17470e1f0b1cSAndy Shevchenko 	{ "8086228E", LPSS_BSW_SSP },
17480e1f0b1cSAndy Shevchenko 	{ "INT33C0", LPSS_LPT_SSP },
17490e1f0b1cSAndy Shevchenko 	{ "INT33C1", LPSS_LPT_SSP },
17500e1f0b1cSAndy Shevchenko 	{ "INT3430", LPSS_LPT_SSP },
17510e1f0b1cSAndy Shevchenko 	{ "INT3431", LPSS_LPT_SSP },
17520e1f0b1cSAndy Shevchenko 	{}
17530e1f0b1cSAndy Shevchenko };
17540e1f0b1cSAndy Shevchenko MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
17550e1f0b1cSAndy Shevchenko #endif
17560e1f0b1cSAndy Shevchenko 
1757d94df25eSKrzysztof Kozlowski static const struct of_device_id pxa2xx_spi_of_match[] __maybe_unused = {
17580e1f0b1cSAndy Shevchenko 	{ .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
17590e1f0b1cSAndy Shevchenko 	{}
17600e1f0b1cSAndy Shevchenko };
17610e1f0b1cSAndy Shevchenko MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
17620e1f0b1cSAndy Shevchenko 
1763ca632f55SGrant Likely static struct platform_driver driver = {
1764ca632f55SGrant Likely 	.driver = {
1765ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
17666c3c438cSAndy Shevchenko 		.pm	= pm_ptr(&pxa2xx_spi_pm_ops),
1767a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
176887ae1d2dSLubomir Rintel 		.of_match_table = of_match_ptr(pxa2xx_spi_of_match),
1769ca632f55SGrant Likely 	},
1770ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
177131f6d96dSUwe Kleine-König 	.remove_new = pxa2xx_spi_remove,
1772ca632f55SGrant Likely };
1773ca632f55SGrant Likely 
pxa2xx_spi_init(void)1774ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
1775ca632f55SGrant Likely {
1776ca632f55SGrant Likely 	return platform_driver_register(&driver);
1777ca632f55SGrant Likely }
1778ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
1779ca632f55SGrant Likely 
pxa2xx_spi_exit(void)1780ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
1781ca632f55SGrant Likely {
1782ca632f55SGrant Likely 	platform_driver_unregister(&driver);
1783ca632f55SGrant Likely }
1784ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
178551ebf6acSFlavio Suligoi 
178651ebf6acSFlavio Suligoi MODULE_SOFTDEP("pre: dw_dmac");
1787