xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision cf961fce30f8269d0c2662c48b2618005b67dfd4)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
4a0d2642eSMika Westerberg  * Copyright (C) 2013, Intel Corporation
5ca632f55SGrant Likely  */
6ca632f55SGrant Likely 
75ce25705SAndy Shevchenko #include <linux/acpi.h>
88b136baaSJarkko Nikula #include <linux/bitops.h>
95ce25705SAndy Shevchenko #include <linux/clk.h>
105ce25705SAndy Shevchenko #include <linux/delay.h>
11ca632f55SGrant Likely #include <linux/device.h>
12cbfd6a21SSachin Kamat #include <linux/err.h>
135ce25705SAndy Shevchenko #include <linux/errno.h>
145ce25705SAndy Shevchenko #include <linux/gpio/consumer.h>
155ce25705SAndy Shevchenko #include <linux/gpio.h>
165ce25705SAndy Shevchenko #include <linux/init.h>
17ca632f55SGrant Likely #include <linux/interrupt.h>
185ce25705SAndy Shevchenko #include <linux/ioport.h>
199df461ecSAndy Shevchenko #include <linux/kernel.h>
205ce25705SAndy Shevchenko #include <linux/module.h>
21ae8fbf1dSAndy Shevchenko #include <linux/mod_devicetable.h>
22ae8fbf1dSAndy Shevchenko #include <linux/of.h>
2334cadd9cSJarkko Nikula #include <linux/pci.h>
24ca632f55SGrant Likely #include <linux/platform_device.h>
255ce25705SAndy Shevchenko #include <linux/pm_runtime.h>
26f2faa3ecSAndy Shevchenko #include <linux/property.h>
275ce25705SAndy Shevchenko #include <linux/slab.h>
28ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h>
29ca632f55SGrant Likely #include <linux/spi/spi.h>
30ca632f55SGrant Likely 
31cd7bed00SMika Westerberg #include "spi-pxa2xx.h"
32ca632f55SGrant Likely 
33ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street");
34ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
35ca632f55SGrant Likely MODULE_LICENSE("GPL");
36ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi");
37ca632f55SGrant Likely 
38ca632f55SGrant Likely #define TIMOUT_DFLT		1000
39ca632f55SGrant Likely 
40ca632f55SGrant Likely /*
41ca632f55SGrant Likely  * for testing SSCR1 changes that require SSP restart, basically
42ca632f55SGrant Likely  * everything except the service and interrupt enables, the pxa270 developer
43ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
44ca632f55SGrant Likely  * list, but the PXA255 dev man says all bits without really meaning the
45ca632f55SGrant Likely  * service and interrupt enables
46ca632f55SGrant Likely  */
47ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
48ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
49ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
50ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
51ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
52ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
53ca632f55SGrant Likely 
54e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
55e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
56e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
57e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
58e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
59e5262d05SWeike Chen 
607c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
617c7289a4SAndy Shevchenko 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
627c7289a4SAndy Shevchenko 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
637c7289a4SAndy Shevchenko 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
647c7289a4SAndy Shevchenko 				| CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
657c7289a4SAndy Shevchenko 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
667c7289a4SAndy Shevchenko 
67624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
68624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE			BIT(0)
69624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
708b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT			9
718b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
72a0d2642eSMika Westerberg 
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
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 
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 
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 
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 
2014fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
2024fdb2424SWeike Chen {
2034fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
204e5262d05SWeike Chen 	case QUARK_X1000_SSP:
205e5262d05SWeike Chen 		return QUARK_X1000_SSCR1_CHANGE_MASK;
2067c7289a4SAndy Shevchenko 	case CE4100_SSP:
2077c7289a4SAndy Shevchenko 		return CE4100_SSCR1_CHANGE_MASK;
2084fdb2424SWeike Chen 	default:
2094fdb2424SWeike Chen 		return SSCR1_CHANGE_MASK;
2104fdb2424SWeike Chen 	}
2114fdb2424SWeike Chen }
2124fdb2424SWeike Chen 
2134fdb2424SWeike Chen static u32
2144fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
2154fdb2424SWeike Chen {
2164fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
217e5262d05SWeike Chen 	case QUARK_X1000_SSP:
218e5262d05SWeike Chen 		return RX_THRESH_QUARK_X1000_DFLT;
2197c7289a4SAndy Shevchenko 	case CE4100_SSP:
2207c7289a4SAndy Shevchenko 		return RX_THRESH_CE4100_DFLT;
2214fdb2424SWeike Chen 	default:
2224fdb2424SWeike Chen 		return RX_THRESH_DFLT;
2234fdb2424SWeike Chen 	}
2244fdb2424SWeike Chen }
2254fdb2424SWeike Chen 
2264fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
2274fdb2424SWeike Chen {
2284fdb2424SWeike Chen 	u32 mask;
2294fdb2424SWeike Chen 
2304fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
231e5262d05SWeike Chen 	case QUARK_X1000_SSP:
232e5262d05SWeike Chen 		mask = QUARK_X1000_SSSR_TFL_MASK;
233e5262d05SWeike Chen 		break;
2347c7289a4SAndy Shevchenko 	case CE4100_SSP:
2357c7289a4SAndy Shevchenko 		mask = CE4100_SSSR_TFL_MASK;
2367c7289a4SAndy Shevchenko 		break;
2374fdb2424SWeike Chen 	default:
2384fdb2424SWeike Chen 		mask = SSSR_TFL_MASK;
2394fdb2424SWeike Chen 		break;
2404fdb2424SWeike Chen 	}
2414fdb2424SWeike Chen 
242c039dd27SJarkko Nikula 	return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
2434fdb2424SWeike Chen }
2444fdb2424SWeike Chen 
2454fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
2464fdb2424SWeike Chen 				     u32 *sccr1_reg)
2474fdb2424SWeike Chen {
2484fdb2424SWeike Chen 	u32 mask;
2494fdb2424SWeike Chen 
2504fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
251e5262d05SWeike Chen 	case QUARK_X1000_SSP:
252e5262d05SWeike Chen 		mask = QUARK_X1000_SSCR1_RFT;
253e5262d05SWeike Chen 		break;
2547c7289a4SAndy Shevchenko 	case CE4100_SSP:
2557c7289a4SAndy Shevchenko 		mask = CE4100_SSCR1_RFT;
2567c7289a4SAndy Shevchenko 		break;
2574fdb2424SWeike Chen 	default:
2584fdb2424SWeike Chen 		mask = SSCR1_RFT;
2594fdb2424SWeike Chen 		break;
2604fdb2424SWeike Chen 	}
2614fdb2424SWeike Chen 	*sccr1_reg &= ~mask;
2624fdb2424SWeike Chen }
2634fdb2424SWeike Chen 
2644fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
2654fdb2424SWeike Chen 				   u32 *sccr1_reg, u32 threshold)
2664fdb2424SWeike Chen {
2674fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
268e5262d05SWeike Chen 	case QUARK_X1000_SSP:
269e5262d05SWeike Chen 		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
270e5262d05SWeike Chen 		break;
2717c7289a4SAndy Shevchenko 	case CE4100_SSP:
2727c7289a4SAndy Shevchenko 		*sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
2737c7289a4SAndy Shevchenko 		break;
2744fdb2424SWeike Chen 	default:
2754fdb2424SWeike Chen 		*sccr1_reg |= SSCR1_RxTresh(threshold);
2764fdb2424SWeike Chen 		break;
2774fdb2424SWeike Chen 	}
2784fdb2424SWeike Chen }
2794fdb2424SWeike Chen 
2804fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
2814fdb2424SWeike Chen 				  u32 clk_div, u8 bits)
2824fdb2424SWeike Chen {
2834fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
284e5262d05SWeike Chen 	case QUARK_X1000_SSP:
285e5262d05SWeike Chen 		return clk_div
286e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_Motorola
287e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
288e5262d05SWeike Chen 			| SSCR0_SSE;
2894fdb2424SWeike Chen 	default:
2904fdb2424SWeike Chen 		return clk_div
2914fdb2424SWeike Chen 			| SSCR0_Motorola
2924fdb2424SWeike Chen 			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
2934fdb2424SWeike Chen 			| SSCR0_SSE
2944fdb2424SWeike Chen 			| (bits > 16 ? SSCR0_EDSS : 0);
2954fdb2424SWeike Chen 	}
2964fdb2424SWeike Chen }
2974fdb2424SWeike Chen 
298a0d2642eSMika Westerberg /*
299a0d2642eSMika Westerberg  * Read and write LPSS SSP private registers. Caller must first check that
300a0d2642eSMika Westerberg  * is_lpss_ssp() returns true before these can be called.
301a0d2642eSMika Westerberg  */
302a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
303a0d2642eSMika Westerberg {
304a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
305a0d2642eSMika Westerberg 	return readl(drv_data->lpss_base + offset);
306a0d2642eSMika Westerberg }
307a0d2642eSMika Westerberg 
308a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data,
309a0d2642eSMika Westerberg 				  unsigned offset, u32 value)
310a0d2642eSMika Westerberg {
311a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
312a0d2642eSMika Westerberg 	writel(value, drv_data->lpss_base + offset);
313a0d2642eSMika Westerberg }
314a0d2642eSMika Westerberg 
315a0d2642eSMika Westerberg /*
316a0d2642eSMika Westerberg  * lpss_ssp_setup - perform LPSS SSP specific setup
317a0d2642eSMika Westerberg  * @drv_data: pointer to the driver private data
318a0d2642eSMika Westerberg  *
319a0d2642eSMika Westerberg  * Perform LPSS SSP specific setup. This function must be called first if
320a0d2642eSMika Westerberg  * one is going to use LPSS SSP private registers.
321a0d2642eSMika Westerberg  */
322a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data)
323a0d2642eSMika Westerberg {
324dccf7369SJarkko Nikula 	const struct lpss_config *config;
325dccf7369SJarkko Nikula 	u32 value;
326a0d2642eSMika Westerberg 
327dccf7369SJarkko Nikula 	config = lpss_get_config(drv_data);
328dccf7369SJarkko Nikula 	drv_data->lpss_base = drv_data->ioaddr + config->offset;
329a0d2642eSMika Westerberg 
330a0d2642eSMika Westerberg 	/* Enable software chip select control */
3310e897218SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
332624ea72eSJarkko Nikula 	value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
333624ea72eSJarkko Nikula 	value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
334dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
3350054e28dSMika Westerberg 
3360054e28dSMika Westerberg 	/* Enable multiblock DMA transfers */
33751eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma) {
338dccf7369SJarkko Nikula 		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
3391de70612SMika Westerberg 
34082ba2c2aSJarkko Nikula 		if (config->reg_general >= 0) {
34182ba2c2aSJarkko Nikula 			value = __lpss_ssp_read_priv(drv_data,
34282ba2c2aSJarkko Nikula 						     config->reg_general);
343624ea72eSJarkko Nikula 			value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
34482ba2c2aSJarkko Nikula 			__lpss_ssp_write_priv(drv_data,
34582ba2c2aSJarkko Nikula 					      config->reg_general, value);
34682ba2c2aSJarkko Nikula 		}
3471de70612SMika Westerberg 	}
348a0d2642eSMika Westerberg }
349a0d2642eSMika Westerberg 
350d5898e19SJarkko Nikula static void lpss_ssp_select_cs(struct spi_device *spi,
351c1e4a53cSMika Westerberg 			       const struct lpss_config *config)
352a0d2642eSMika Westerberg {
353d5898e19SJarkko Nikula 	struct driver_data *drv_data =
354d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
355d0283eb2SJarkko Nikula 	u32 value, cs;
356a0d2642eSMika Westerberg 
357c1e4a53cSMika Westerberg 	if (!config->cs_sel_mask)
358c1e4a53cSMika Westerberg 		return;
359dccf7369SJarkko Nikula 
360dccf7369SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
361c1e4a53cSMika Westerberg 
362d5898e19SJarkko Nikula 	cs = spi->chip_select;
363c1e4a53cSMika Westerberg 	cs <<= config->cs_sel_shift;
364c1e4a53cSMika Westerberg 	if (cs != (value & config->cs_sel_mask)) {
365d0283eb2SJarkko Nikula 		/*
366c1e4a53cSMika Westerberg 		 * When switching another chip select output active the
367c1e4a53cSMika Westerberg 		 * output must be selected first and wait 2 ssp_clk cycles
368c1e4a53cSMika Westerberg 		 * before changing state to active. Otherwise a short
369c1e4a53cSMika Westerberg 		 * glitch will occur on the previous chip select since
370c1e4a53cSMika Westerberg 		 * output select is latched but state control is not.
371d0283eb2SJarkko Nikula 		 */
372c1e4a53cSMika Westerberg 		value &= ~config->cs_sel_mask;
373d0283eb2SJarkko Nikula 		value |= cs;
374d0283eb2SJarkko Nikula 		__lpss_ssp_write_priv(drv_data,
375d0283eb2SJarkko Nikula 				      config->reg_cs_ctrl, value);
376d0283eb2SJarkko Nikula 		ndelay(1000000000 /
37751eea52dSLubomir Rintel 		       (drv_data->controller->max_speed_hz / 2));
378d0283eb2SJarkko Nikula 	}
379d0283eb2SJarkko Nikula }
380c1e4a53cSMika Westerberg 
381d5898e19SJarkko Nikula static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
382c1e4a53cSMika Westerberg {
383d5898e19SJarkko Nikula 	struct driver_data *drv_data =
384d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
385c1e4a53cSMika Westerberg 	const struct lpss_config *config;
386c1e4a53cSMika Westerberg 	u32 value;
387c1e4a53cSMika Westerberg 
388c1e4a53cSMika Westerberg 	config = lpss_get_config(drv_data);
389c1e4a53cSMika Westerberg 
390c1e4a53cSMika Westerberg 	if (enable)
391d5898e19SJarkko Nikula 		lpss_ssp_select_cs(spi, config);
392c1e4a53cSMika Westerberg 
393c1e4a53cSMika Westerberg 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
394c1e4a53cSMika Westerberg 	if (enable)
395c1e4a53cSMika Westerberg 		value &= ~LPSS_CS_CONTROL_CS_HIGH;
396c1e4a53cSMika Westerberg 	else
397c1e4a53cSMika Westerberg 		value |= LPSS_CS_CONTROL_CS_HIGH;
398dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
399683f65deSEvan Green 	if (config->cs_clk_stays_gated) {
400683f65deSEvan Green 		u32 clkgate;
401683f65deSEvan Green 
402683f65deSEvan Green 		/*
403683f65deSEvan Green 		 * Changing CS alone when dynamic clock gating is on won't
404683f65deSEvan Green 		 * actually flip CS at that time. This ruins SPI transfers
405683f65deSEvan Green 		 * that specify delays, or have no data. Toggle the clock mode
406683f65deSEvan Green 		 * to force on briefly to poke the CS pin to move.
407683f65deSEvan Green 		 */
408683f65deSEvan Green 		clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
409683f65deSEvan Green 		value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
410683f65deSEvan Green 			LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
411683f65deSEvan Green 
412683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
413683f65deSEvan Green 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
414683f65deSEvan Green 	}
415a0d2642eSMika Westerberg }
416a0d2642eSMika Westerberg 
417d5898e19SJarkko Nikula static void cs_assert(struct spi_device *spi)
418ca632f55SGrant Likely {
419d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
420d5898e19SJarkko Nikula 	struct driver_data *drv_data =
421d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
422ca632f55SGrant Likely 
423ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP) {
42496579a4eSJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, chip->frm);
425ca632f55SGrant Likely 		return;
426ca632f55SGrant Likely 	}
427ca632f55SGrant Likely 
428ca632f55SGrant Likely 	if (chip->cs_control) {
429ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_ASSERT);
430ca632f55SGrant Likely 		return;
431ca632f55SGrant Likely 	}
432ca632f55SGrant Likely 
433c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
434c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
435a0d2642eSMika Westerberg 		return;
436a0d2642eSMika Westerberg 	}
437a0d2642eSMika Westerberg 
4387566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
439d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, true);
440ca632f55SGrant Likely }
441ca632f55SGrant Likely 
442d5898e19SJarkko Nikula static void cs_deassert(struct spi_device *spi)
443ca632f55SGrant Likely {
444d5898e19SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
445d5898e19SJarkko Nikula 	struct driver_data *drv_data =
446d5898e19SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
447104e51afSJarkko Nikula 	unsigned long timeout;
448ca632f55SGrant Likely 
449ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
450ca632f55SGrant Likely 		return;
451ca632f55SGrant Likely 
452104e51afSJarkko Nikula 	/* Wait until SSP becomes idle before deasserting the CS */
453104e51afSJarkko Nikula 	timeout = jiffies + msecs_to_jiffies(10);
454104e51afSJarkko Nikula 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
455104e51afSJarkko Nikula 	       !time_after(jiffies, timeout))
456104e51afSJarkko Nikula 		cpu_relax();
457104e51afSJarkko Nikula 
458ca632f55SGrant Likely 	if (chip->cs_control) {
459ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_DEASSERT);
460ca632f55SGrant Likely 		return;
461ca632f55SGrant Likely 	}
462ca632f55SGrant Likely 
463c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
464c18d925fSJan Kiszka 		gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
465a0d2642eSMika Westerberg 		return;
466a0d2642eSMika Westerberg 	}
467a0d2642eSMika Westerberg 
4687566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
469d5898e19SJarkko Nikula 		lpss_ssp_cs_control(spi, false);
470d5898e19SJarkko Nikula }
471d5898e19SJarkko Nikula 
472d5898e19SJarkko Nikula static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
473d5898e19SJarkko Nikula {
474d5898e19SJarkko Nikula 	if (level)
475d5898e19SJarkko Nikula 		cs_deassert(spi);
476d5898e19SJarkko Nikula 	else
477d5898e19SJarkko Nikula 		cs_assert(spi);
478ca632f55SGrant Likely }
479ca632f55SGrant Likely 
480cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
481ca632f55SGrant Likely {
482ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
483ca632f55SGrant Likely 
484ca632f55SGrant Likely 	do {
485c039dd27SJarkko Nikula 		while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
486c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
487c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
488ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
489ca632f55SGrant Likely 
490ca632f55SGrant Likely 	return limit;
491ca632f55SGrant Likely }
492ca632f55SGrant Likely 
49329d7e05cSLubomir Rintel static void pxa2xx_spi_off(struct driver_data *drv_data)
49429d7e05cSLubomir Rintel {
49541c98841SAndy Shevchenko 	/* On MMP, disabling SSE seems to corrupt the Rx FIFO */
49641c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data))
49729d7e05cSLubomir Rintel 		return;
49829d7e05cSLubomir Rintel 
49929d7e05cSLubomir Rintel 	pxa2xx_spi_write(drv_data, SSCR0,
50029d7e05cSLubomir Rintel 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
50129d7e05cSLubomir Rintel }
50229d7e05cSLubomir Rintel 
503ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
504ca632f55SGrant Likely {
505ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
506ca632f55SGrant Likely 
5074fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
508ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
509ca632f55SGrant Likely 		return 0;
510ca632f55SGrant Likely 
511c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
512ca632f55SGrant Likely 	drv_data->tx += n_bytes;
513ca632f55SGrant Likely 
514ca632f55SGrant Likely 	return 1;
515ca632f55SGrant Likely }
516ca632f55SGrant Likely 
517ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
518ca632f55SGrant Likely {
519ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
520ca632f55SGrant Likely 
521c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
522ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
523c039dd27SJarkko Nikula 		pxa2xx_spi_read(drv_data, SSDR);
524ca632f55SGrant Likely 		drv_data->rx += n_bytes;
525ca632f55SGrant Likely 	}
526ca632f55SGrant Likely 
527ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
528ca632f55SGrant Likely }
529ca632f55SGrant Likely 
530ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data)
531ca632f55SGrant Likely {
5324fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
533ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
534ca632f55SGrant Likely 		return 0;
535ca632f55SGrant Likely 
536c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
537ca632f55SGrant Likely 	++drv_data->tx;
538ca632f55SGrant Likely 
539ca632f55SGrant Likely 	return 1;
540ca632f55SGrant Likely }
541ca632f55SGrant Likely 
542ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data)
543ca632f55SGrant Likely {
544c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
545ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
546c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
547ca632f55SGrant Likely 		++drv_data->rx;
548ca632f55SGrant Likely 	}
549ca632f55SGrant Likely 
550ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
551ca632f55SGrant Likely }
552ca632f55SGrant Likely 
553ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
554ca632f55SGrant Likely {
5554fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
556ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
557ca632f55SGrant Likely 		return 0;
558ca632f55SGrant Likely 
559c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
560ca632f55SGrant Likely 	drv_data->tx += 2;
561ca632f55SGrant Likely 
562ca632f55SGrant Likely 	return 1;
563ca632f55SGrant Likely }
564ca632f55SGrant Likely 
565ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
566ca632f55SGrant Likely {
567c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
568ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
569c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
570ca632f55SGrant Likely 		drv_data->rx += 2;
571ca632f55SGrant Likely 	}
572ca632f55SGrant Likely 
573ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
574ca632f55SGrant Likely }
575ca632f55SGrant Likely 
576ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
577ca632f55SGrant Likely {
5784fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
579ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
580ca632f55SGrant Likely 		return 0;
581ca632f55SGrant Likely 
582c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
583ca632f55SGrant Likely 	drv_data->tx += 4;
584ca632f55SGrant Likely 
585ca632f55SGrant Likely 	return 1;
586ca632f55SGrant Likely }
587ca632f55SGrant Likely 
588ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
589ca632f55SGrant Likely {
590c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
591ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
592c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
593ca632f55SGrant Likely 		drv_data->rx += 4;
594ca632f55SGrant Likely 	}
595ca632f55SGrant Likely 
596ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
597ca632f55SGrant Likely }
598ca632f55SGrant Likely 
599ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
600ca632f55SGrant Likely {
60196579a4eSJarkko Nikula 	struct chip_data *chip =
60251eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
603ca632f55SGrant Likely 	u32 sccr1_reg;
604ca632f55SGrant Likely 
605c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
606152bc19eSAndy Shevchenko 	switch (drv_data->ssp_type) {
607152bc19eSAndy Shevchenko 	case QUARK_X1000_SSP:
608152bc19eSAndy Shevchenko 		sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
609152bc19eSAndy Shevchenko 		break;
6107c7289a4SAndy Shevchenko 	case CE4100_SSP:
6117c7289a4SAndy Shevchenko 		sccr1_reg &= ~CE4100_SSCR1_RFT;
6127c7289a4SAndy Shevchenko 		break;
613152bc19eSAndy Shevchenko 	default:
614ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_RFT;
615152bc19eSAndy Shevchenko 		break;
616152bc19eSAndy Shevchenko 	}
617ca632f55SGrant Likely 	sccr1_reg |= chip->threshold;
618c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
619ca632f55SGrant Likely }
620ca632f55SGrant Likely 
621ca632f55SGrant Likely static void int_error_stop(struct driver_data *drv_data, const char* msg)
622ca632f55SGrant Likely {
623ca632f55SGrant Likely 	/* Stop and reset SSP */
624ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
625ca632f55SGrant Likely 	reset_sccr1(drv_data);
626ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
627c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
628cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
62929d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
630ca632f55SGrant Likely 
631ca632f55SGrant Likely 	dev_err(&drv_data->pdev->dev, "%s\n", msg);
632ca632f55SGrant Likely 
63351eea52dSLubomir Rintel 	drv_data->controller->cur_msg->status = -EIO;
63451eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
635ca632f55SGrant Likely }
636ca632f55SGrant Likely 
637ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
638ca632f55SGrant Likely {
63907550df0SJarkko Nikula 	/* Clear and disable interrupts */
640ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
641ca632f55SGrant Likely 	reset_sccr1(drv_data);
642ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
643c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
644ca632f55SGrant Likely 
64551eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
646ca632f55SGrant Likely }
647ca632f55SGrant Likely 
648ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
649ca632f55SGrant Likely {
650c039dd27SJarkko Nikula 	u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
651ca632f55SGrant Likely 		       drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
652ca632f55SGrant Likely 
653c039dd27SJarkko Nikula 	u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
654ca632f55SGrant Likely 
655ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
656ca632f55SGrant Likely 		int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
657ca632f55SGrant Likely 		return IRQ_HANDLED;
658ca632f55SGrant Likely 	}
659ca632f55SGrant Likely 
660ec93cb6fSLubomir Rintel 	if (irq_status & SSSR_TUR) {
661ec93cb6fSLubomir Rintel 		int_error_stop(drv_data, "interrupt_transfer: fifo underrun");
662ec93cb6fSLubomir Rintel 		return IRQ_HANDLED;
663ec93cb6fSLubomir Rintel 	}
664ec93cb6fSLubomir Rintel 
665ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
666c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
667ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
668ca632f55SGrant Likely 			int_transfer_complete(drv_data);
669ca632f55SGrant Likely 			return IRQ_HANDLED;
670ca632f55SGrant Likely 		}
671ca632f55SGrant Likely 	}
672ca632f55SGrant Likely 
673ca632f55SGrant Likely 	/* Drain rx fifo, Fill tx fifo and prevent overruns */
674ca632f55SGrant Likely 	do {
675ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
676ca632f55SGrant Likely 			int_transfer_complete(drv_data);
677ca632f55SGrant Likely 			return IRQ_HANDLED;
678ca632f55SGrant Likely 		}
679ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
680ca632f55SGrant Likely 
681ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
682ca632f55SGrant Likely 		int_transfer_complete(drv_data);
683ca632f55SGrant Likely 		return IRQ_HANDLED;
684ca632f55SGrant Likely 	}
685ca632f55SGrant Likely 
686ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
687ca632f55SGrant Likely 		u32 bytes_left;
688ca632f55SGrant Likely 		u32 sccr1_reg;
689ca632f55SGrant Likely 
690c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
691ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
692ca632f55SGrant Likely 
693ca632f55SGrant Likely 		/*
694ca632f55SGrant Likely 		 * PXA25x_SSP has no timeout, set up rx threshould for the
695ca632f55SGrant Likely 		 * remaining RX bytes.
696ca632f55SGrant Likely 		 */
697ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
6984fdb2424SWeike Chen 			u32 rx_thre;
699ca632f55SGrant Likely 
7004fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
701ca632f55SGrant Likely 
702ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
703ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
704ca632f55SGrant Likely 			case 4:
7052c183376SGustavo A. R. Silva 				bytes_left >>= 2;
7062c183376SGustavo A. R. Silva 				break;
707ca632f55SGrant Likely 			case 2:
708ca632f55SGrant Likely 				bytes_left >>= 1;
7092c183376SGustavo A. R. Silva 				break;
710ca632f55SGrant Likely 			}
711ca632f55SGrant Likely 
7124fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
7134fdb2424SWeike Chen 			if (rx_thre > bytes_left)
7144fdb2424SWeike Chen 				rx_thre = bytes_left;
715ca632f55SGrant Likely 
7164fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
717ca632f55SGrant Likely 		}
718c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
719ca632f55SGrant Likely 	}
720ca632f55SGrant Likely 
721ca632f55SGrant Likely 	/* We did something */
722ca632f55SGrant Likely 	return IRQ_HANDLED;
723ca632f55SGrant Likely }
724ca632f55SGrant Likely 
725b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data)
726b0312482SJan Kiszka {
72729d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
728b0312482SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1,
729b0312482SJan Kiszka 			 pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
730b0312482SJan Kiszka 	if (!pxa25x_ssp_comp(drv_data))
731b0312482SJan Kiszka 		pxa2xx_spi_write(drv_data, SSTO, 0);
732b0312482SJan Kiszka 	write_SSSR_CS(drv_data, drv_data->clear_sr);
733b0312482SJan Kiszka 
734b0312482SJan Kiszka 	dev_err(&drv_data->pdev->dev,
735b0312482SJan Kiszka 		"bad message state in interrupt handler\n");
736b0312482SJan Kiszka }
737b0312482SJan Kiszka 
738ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
739ca632f55SGrant Likely {
740ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
7417d94a505SMika Westerberg 	u32 sccr1_reg;
742ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
743ca632f55SGrant Likely 	u32 status;
744ca632f55SGrant Likely 
7457d94a505SMika Westerberg 	/*
7467d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
7477d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
7487d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
7497d94a505SMika Westerberg 	 * interrupt is enabled).
7507d94a505SMika Westerberg 	 */
7517d94a505SMika Westerberg 	if (pm_runtime_suspended(&drv_data->pdev->dev))
7527d94a505SMika Westerberg 		return IRQ_NONE;
7537d94a505SMika Westerberg 
754269e4a41SMika Westerberg 	/*
755269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
756269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
757269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
758269e4a41SMika Westerberg 	 * powered off.
759269e4a41SMika Westerberg 	 */
760c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
761269e4a41SMika Westerberg 	if (status == ~0)
762269e4a41SMika Westerberg 		return IRQ_NONE;
763269e4a41SMika Westerberg 
764c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
765ca632f55SGrant Likely 
766ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
767ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
768ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
769ca632f55SGrant Likely 
77002bc933eSTan, Jui Nee 	/* Ignore RX timeout interrupt if it is disabled */
77102bc933eSTan, Jui Nee 	if (!(sccr1_reg & SSCR1_TINTE))
77202bc933eSTan, Jui Nee 		mask &= ~SSSR_TINT;
77302bc933eSTan, Jui Nee 
774ca632f55SGrant Likely 	if (!(status & mask))
775ca632f55SGrant Likely 		return IRQ_NONE;
776ca632f55SGrant Likely 
777e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
778e51e9b93SJan Kiszka 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
779e51e9b93SJan Kiszka 
78051eea52dSLubomir Rintel 	if (!drv_data->controller->cur_msg) {
781b0312482SJan Kiszka 		handle_bad_msg(drv_data);
782ca632f55SGrant Likely 		/* Never fail */
783ca632f55SGrant Likely 		return IRQ_HANDLED;
784ca632f55SGrant Likely 	}
785ca632f55SGrant Likely 
786ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
787ca632f55SGrant Likely }
788ca632f55SGrant Likely 
789e5262d05SWeike Chen /*
7909df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
7919df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
7929df461ecSAndy Shevchenko  *
7939df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
7949df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
7959df461ecSAndy Shevchenko  *
7969df461ecSAndy Shevchenko  * Fsys = 200MHz
7979df461ecSAndy Shevchenko  *
7989df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
7999df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
8009df461ecSAndy Shevchenko  *
8019df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
8029df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
8039df461ecSAndy Shevchenko  *
8049df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
8059df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
8069df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
8079df461ecSAndy Shevchenko  *       k = [1, 256]
8089df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
8099df461ecSAndy Shevchenko  *
8109df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
8119df461ecSAndy Shevchenko  * are:
8129df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
8139df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
8149df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
8159df461ecSAndy Shevchenko  *
8169df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
8179df461ecSAndy Shevchenko  *
8189df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
8199df461ecSAndy Shevchenko  * to the asked baud rate.
820e5262d05SWeike Chen  */
8219df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
822e5262d05SWeike Chen {
8239df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
8249df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
8259df461ecSAndy Shevchenko 						   see (2) */
8269df461ecSAndy Shevchenko 						/* case 3 */
8279df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
8289df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
8299df461ecSAndy Shevchenko 	unsigned long scale;
8309df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
8319df461ecSAndy Shevchenko 	long r, r1, r2;
8329df461ecSAndy Shevchenko 	u32 mul;
833e5262d05SWeike Chen 
8349df461ecSAndy Shevchenko 	/* Case 1 */
8359df461ecSAndy Shevchenko 
8369df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
8379df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
8389df461ecSAndy Shevchenko 
8399df461ecSAndy Shevchenko 	/* Calculate initial quot */
8403ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
8419df461ecSAndy Shevchenko 
8429df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
8439df461ecSAndy Shevchenko 	if (q1 > 256) {
8449df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
8459df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
8469df461ecSAndy Shevchenko 		if (scale > 9) {
8479df461ecSAndy Shevchenko 			q1 >>= scale - 9;
8489df461ecSAndy Shevchenko 			mul >>= scale - 9;
8499df461ecSAndy Shevchenko 		}
8509df461ecSAndy Shevchenko 
8519df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
8529df461ecSAndy Shevchenko 		q1 += q1 & 1;
8539df461ecSAndy Shevchenko 	}
8549df461ecSAndy Shevchenko 
8559df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
8569df461ecSAndy Shevchenko 	scale = __ffs(q1);
8579df461ecSAndy Shevchenko 	q1 >>= scale;
8589df461ecSAndy Shevchenko 	mul >>= scale;
8599df461ecSAndy Shevchenko 
8609df461ecSAndy Shevchenko 	/* Get the remainder */
8619df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
8629df461ecSAndy Shevchenko 
8639df461ecSAndy Shevchenko 	/* Case 2 */
8649df461ecSAndy Shevchenko 
8653ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
8669df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
8679df461ecSAndy Shevchenko 
8689df461ecSAndy Shevchenko 	/*
8699df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
8709df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
8719df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
8729df461ecSAndy Shevchenko 	 */
8739df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
8749df461ecSAndy Shevchenko 		/* case 1 is better */
8759df461ecSAndy Shevchenko 		r = r1;
8769df461ecSAndy Shevchenko 		q = q1;
8779df461ecSAndy Shevchenko 	} else {
8789df461ecSAndy Shevchenko 		/* case 2 is better */
8799df461ecSAndy Shevchenko 		r = r2;
8809df461ecSAndy Shevchenko 		q = q2;
8819df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
8829df461ecSAndy Shevchenko 	}
8839df461ecSAndy Shevchenko 
8843ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
8859df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
8869df461ecSAndy Shevchenko 		u64 fssp;
8879df461ecSAndy Shevchenko 		u32 m;
8889df461ecSAndy Shevchenko 
8899df461ecSAndy Shevchenko 		/* Calculate initial quot */
8903ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
8919df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
8929df461ecSAndy Shevchenko 
8939df461ecSAndy Shevchenko 		/* Get the remainder */
8949df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
8959df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
8969df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
8979df461ecSAndy Shevchenko 
8989df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
8999df461ecSAndy Shevchenko 		if (r1 < r) {
9009df461ecSAndy Shevchenko 			/* case 3 is better */
9019df461ecSAndy Shevchenko 			q = 1;
9029df461ecSAndy Shevchenko 			mul = m;
903e5262d05SWeike Chen 		}
904e5262d05SWeike Chen 	}
905e5262d05SWeike Chen 
9069df461ecSAndy Shevchenko 	*dds = mul;
9079df461ecSAndy Shevchenko 	return q - 1;
908e5262d05SWeike Chen }
909e5262d05SWeike Chen 
9103343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
911ca632f55SGrant Likely {
91251eea52dSLubomir Rintel 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
9133343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
9143343b7a6SMika Westerberg 
9153343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
916ca632f55SGrant Likely 
91729f21337SFlavio Suligoi 	/*
91829f21337SFlavio Suligoi 	 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
91929f21337SFlavio Suligoi 	 * that the SSP transmission rate can be greater than the device rate
92029f21337SFlavio Suligoi 	 */
921ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
92229f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
923ca632f55SGrant Likely 	else
92429f21337SFlavio Suligoi 		return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
925ca632f55SGrant Likely }
926ca632f55SGrant Likely 
927e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
928d2c2f6a4SAndy Shevchenko 					   int rate)
929e5262d05SWeike Chen {
93096579a4eSJarkko Nikula 	struct chip_data *chip =
93151eea52dSLubomir Rintel 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
932025ffe88SAndy Shevchenko 	unsigned int clk_div;
933e5262d05SWeike Chen 
934e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
935e5262d05SWeike Chen 	case QUARK_X1000_SSP:
9369df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
937eecacf73SDan Carpenter 		break;
938e5262d05SWeike Chen 	default:
939025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
940eecacf73SDan Carpenter 		break;
941e5262d05SWeike Chen 	}
942025ffe88SAndy Shevchenko 	return clk_div << 8;
943e5262d05SWeike Chen }
944e5262d05SWeike Chen 
94551eea52dSLubomir Rintel static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
946b6ced294SJarkko Nikula 			       struct spi_device *spi,
947b6ced294SJarkko Nikula 			       struct spi_transfer *xfer)
948b6ced294SJarkko Nikula {
949b6ced294SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
950b6ced294SJarkko Nikula 
951b6ced294SJarkko Nikula 	return chip->enable_dma &&
952b6ced294SJarkko Nikula 	       xfer->len <= MAX_DMA_LEN &&
953b6ced294SJarkko Nikula 	       xfer->len >= chip->dma_burst_size;
954b6ced294SJarkko Nikula }
955b6ced294SJarkko Nikula 
95651eea52dSLubomir Rintel static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
957d5898e19SJarkko Nikula 				   struct spi_device *spi,
958d5898e19SJarkko Nikula 				   struct spi_transfer *transfer)
959ca632f55SGrant Likely {
96051eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
96151eea52dSLubomir Rintel 	struct spi_message *message = controller->cur_msg;
96220f4c379SJarkko Nikula 	struct chip_data *chip = spi_get_ctldata(spi);
96396579a4eSJarkko Nikula 	u32 dma_thresh = chip->dma_threshold;
96496579a4eSJarkko Nikula 	u32 dma_burst = chip->dma_burst_size;
96596579a4eSJarkko Nikula 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
966bffc967eSJarkko Nikula 	u32 clk_div;
967bffc967eSJarkko Nikula 	u8 bits;
968bffc967eSJarkko Nikula 	u32 speed;
969ca632f55SGrant Likely 	u32 cr0;
970ca632f55SGrant Likely 	u32 cr1;
9717d1f1bf6SAndy Shevchenko 	int err;
972b6ced294SJarkko Nikula 	int dma_mapped;
973ca632f55SGrant Likely 
974cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
975b6ced294SJarkko Nikula 	if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
976ca632f55SGrant Likely 
977ca632f55SGrant Likely 		/* reject already-mapped transfers; PIO won't always work */
978ca632f55SGrant Likely 		if (message->is_dma_mapped
979ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
980748fbadfSJarkko Nikula 			dev_err(&spi->dev,
9818ae55af3SJarkko Nikula 				"Mapped transfer length of %u is greater than %d\n",
982ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
983d5898e19SJarkko Nikula 			return -EINVAL;
984ca632f55SGrant Likely 		}
985ca632f55SGrant Likely 
986ca632f55SGrant Likely 		/* warn ... we force this to PIO mode */
98720f4c379SJarkko Nikula 		dev_warn_ratelimited(&spi->dev,
9888ae55af3SJarkko Nikula 				     "DMA disabled for transfer length %ld greater than %d\n",
989d5898e19SJarkko Nikula 				     (long)transfer->len, MAX_DMA_LEN);
990ca632f55SGrant Likely 	}
991ca632f55SGrant Likely 
992ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
993cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
994748fbadfSJarkko Nikula 		dev_err(&spi->dev, "Flush failed\n");
995d5898e19SJarkko Nikula 		return -EIO;
996ca632f55SGrant Likely 	}
997ca632f55SGrant Likely 	drv_data->n_bytes = chip->n_bytes;
998ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
999ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
1000ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
1001ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
1002ca632f55SGrant Likely 	drv_data->write = drv_data->tx ? chip->write : null_writer;
1003ca632f55SGrant Likely 	drv_data->read = drv_data->rx ? chip->read : null_reader;
1004ca632f55SGrant Likely 
1005ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
1006ca632f55SGrant Likely 	bits = transfer->bits_per_word;
1007ca632f55SGrant Likely 	speed = transfer->speed_hz;
1008ca632f55SGrant Likely 
1009d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
1010ca632f55SGrant Likely 
1011ca632f55SGrant Likely 	if (bits <= 8) {
1012ca632f55SGrant Likely 		drv_data->n_bytes = 1;
1013ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1014ca632f55SGrant Likely 					u8_reader : null_reader;
1015ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1016ca632f55SGrant Likely 					u8_writer : null_writer;
1017ca632f55SGrant Likely 	} else if (bits <= 16) {
1018ca632f55SGrant Likely 		drv_data->n_bytes = 2;
1019ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1020ca632f55SGrant Likely 					u16_reader : null_reader;
1021ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1022ca632f55SGrant Likely 					u16_writer : null_writer;
1023ca632f55SGrant Likely 	} else if (bits <= 32) {
1024ca632f55SGrant Likely 		drv_data->n_bytes = 4;
1025ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1026ca632f55SGrant Likely 					u32_reader : null_reader;
1027ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1028ca632f55SGrant Likely 					u32_writer : null_writer;
1029ca632f55SGrant Likely 	}
1030196b0e2cSJarkko Nikula 	/*
1031196b0e2cSJarkko Nikula 	 * if bits/word is changed in dma mode, then must check the
1032196b0e2cSJarkko Nikula 	 * thresholds and burst also
1033196b0e2cSJarkko Nikula 	 */
1034ca632f55SGrant Likely 	if (chip->enable_dma) {
1035cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
103620f4c379SJarkko Nikula 						spi,
1037ca632f55SGrant Likely 						bits, &dma_burst,
1038ca632f55SGrant Likely 						&dma_thresh))
103920f4c379SJarkko Nikula 			dev_warn_ratelimited(&spi->dev,
10408ae55af3SJarkko Nikula 					     "DMA burst size reduced to match bits_per_word\n");
1041ca632f55SGrant Likely 	}
1042ca632f55SGrant Likely 
104351eea52dSLubomir Rintel 	dma_mapped = controller->can_dma &&
104420f4c379SJarkko Nikula 		     controller->can_dma(controller, spi, transfer) &&
104551eea52dSLubomir Rintel 		     controller->cur_msg_mapped;
1046b6ced294SJarkko Nikula 	if (dma_mapped) {
1047ca632f55SGrant Likely 
1048ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
1049cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1050ca632f55SGrant Likely 
1051d5898e19SJarkko Nikula 		err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1052d5898e19SJarkko Nikula 		if (err)
1053d5898e19SJarkko Nikula 			return err;
1054ca632f55SGrant Likely 
1055ca632f55SGrant Likely 		/* Clear status and start DMA engine */
1056ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1057c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1058cd7bed00SMika Westerberg 
1059cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
1060ca632f55SGrant Likely 	} else {
1061ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
1062ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
1063ca632f55SGrant Likely 
1064ca632f55SGrant Likely 		/* Clear status  */
1065ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1066ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1067ca632f55SGrant Likely 	}
1068ca632f55SGrant Likely 
1069ee03672dSJarkko Nikula 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1070ee03672dSJarkko Nikula 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1071ee03672dSJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
107220f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
107351eea52dSLubomir Rintel 			controller->max_speed_hz
1074ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1075b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1076ee03672dSJarkko Nikula 	else
107720f4c379SJarkko Nikula 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
107851eea52dSLubomir Rintel 			controller->max_speed_hz / 2
1079ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1080b6ced294SJarkko Nikula 			dma_mapped ? "DMA" : "PIO");
1081ee03672dSJarkko Nikula 
1082a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
1083c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
1084c039dd27SJarkko Nikula 		    != chip->lpss_rx_threshold)
1085c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSIRF,
1086c039dd27SJarkko Nikula 					 chip->lpss_rx_threshold);
1087c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
1088c039dd27SJarkko Nikula 		    != chip->lpss_tx_threshold)
1089c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSITF,
1090c039dd27SJarkko Nikula 					 chip->lpss_tx_threshold);
1091a0d2642eSMika Westerberg 	}
1092a0d2642eSMika Westerberg 
1093e5262d05SWeike Chen 	if (is_quark_x1000_ssp(drv_data) &&
1094c039dd27SJarkko Nikula 	    (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
1095c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
1096e5262d05SWeike Chen 
1097ca632f55SGrant Likely 	/* see if we need to reload the config registers */
1098c039dd27SJarkko Nikula 	if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1099c039dd27SJarkko Nikula 	    || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1100c039dd27SJarkko Nikula 	    != (cr1 & change_mask)) {
1101ca632f55SGrant Likely 		/* stop the SSP, and update the other bits */
110241c98841SAndy Shevchenko 		if (!is_mmp2_ssp(drv_data))
1103c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
1104ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1105c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1106ca632f55SGrant Likely 		/* first set CR1 without interrupt and service enables */
1107c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
1108ca632f55SGrant Likely 		/* restart the SSP */
1109c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0);
1110ca632f55SGrant Likely 
1111ca632f55SGrant Likely 	} else {
1112ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1113c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1114ca632f55SGrant Likely 	}
1115ca632f55SGrant Likely 
111641c98841SAndy Shevchenko 	if (is_mmp2_ssp(drv_data)) {
111782391856SLubomir Rintel 		u8 tx_level = (pxa2xx_spi_read(drv_data, SSSR)
111882391856SLubomir Rintel 					& SSSR_TFL_MASK) >> 8;
111982391856SLubomir Rintel 
112082391856SLubomir Rintel 		if (tx_level) {
112182391856SLubomir Rintel 			/* On MMP2, flipping SSE doesn't to empty TXFIFO. */
112282391856SLubomir Rintel 			dev_warn(&spi->dev, "%d bytes of garbage in TXFIFO!\n",
112382391856SLubomir Rintel 								tx_level);
112482391856SLubomir Rintel 			if (tx_level > transfer->len)
112582391856SLubomir Rintel 				tx_level = transfer->len;
112682391856SLubomir Rintel 			drv_data->tx += tx_level;
112782391856SLubomir Rintel 		}
112882391856SLubomir Rintel 	}
112982391856SLubomir Rintel 
113051eea52dSLubomir Rintel 	if (spi_controller_is_slave(controller)) {
1131ec93cb6fSLubomir Rintel 		while (drv_data->write(drv_data))
1132ec93cb6fSLubomir Rintel 			;
113377d33897SLubomir Rintel 		if (drv_data->gpiod_ready) {
113477d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 1);
113577d33897SLubomir Rintel 			udelay(1);
113677d33897SLubomir Rintel 			gpiod_set_value(drv_data->gpiod_ready, 0);
113777d33897SLubomir Rintel 		}
1138ec93cb6fSLubomir Rintel 	}
1139ec93cb6fSLubomir Rintel 
1140d5898e19SJarkko Nikula 	/*
1141d5898e19SJarkko Nikula 	 * Release the data by enabling service requests and interrupts,
1142d5898e19SJarkko Nikula 	 * without changing any mode bits
1143d5898e19SJarkko Nikula 	 */
1144c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1145d5898e19SJarkko Nikula 
1146d5898e19SJarkko Nikula 	return 1;
1147ca632f55SGrant Likely }
1148ca632f55SGrant Likely 
114951eea52dSLubomir Rintel static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
1150ec93cb6fSLubomir Rintel {
115151eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1152ec93cb6fSLubomir Rintel 
1153ec93cb6fSLubomir Rintel 	/* Stop and reset SSP */
1154ec93cb6fSLubomir Rintel 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1155ec93cb6fSLubomir Rintel 	reset_sccr1(drv_data);
1156ec93cb6fSLubomir Rintel 	if (!pxa25x_ssp_comp(drv_data))
1157ec93cb6fSLubomir Rintel 		pxa2xx_spi_write(drv_data, SSTO, 0);
1158ec93cb6fSLubomir Rintel 	pxa2xx_spi_flush(drv_data);
115929d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
1160ec93cb6fSLubomir Rintel 
1161ec93cb6fSLubomir Rintel 	dev_dbg(&drv_data->pdev->dev, "transfer aborted\n");
1162ec93cb6fSLubomir Rintel 
116351eea52dSLubomir Rintel 	drv_data->controller->cur_msg->status = -EINTR;
116451eea52dSLubomir Rintel 	spi_finalize_current_transfer(drv_data->controller);
1165ec93cb6fSLubomir Rintel 
1166ec93cb6fSLubomir Rintel 	return 0;
1167ec93cb6fSLubomir Rintel }
1168ec93cb6fSLubomir Rintel 
116951eea52dSLubomir Rintel static void pxa2xx_spi_handle_err(struct spi_controller *controller,
11707f86bde9SMika Westerberg 				 struct spi_message *msg)
1171ca632f55SGrant Likely {
117251eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1173ca632f55SGrant Likely 
1174d5898e19SJarkko Nikula 	/* Disable the SSP */
117529d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
1176d5898e19SJarkko Nikula 	/* Clear and disable interrupts and service requests */
1177d5898e19SJarkko Nikula 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1178d5898e19SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1,
1179d5898e19SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR1)
1180d5898e19SJarkko Nikula 			 & ~(drv_data->int_cr1 | drv_data->dma_cr1));
1181d5898e19SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
1182d5898e19SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1183ca632f55SGrant Likely 
1184d5898e19SJarkko Nikula 	/*
1185d5898e19SJarkko Nikula 	 * Stop the DMA if running. Note DMA callback handler may have unset
1186d5898e19SJarkko Nikula 	 * the dma_running already, which is fine as stopping is not needed
1187d5898e19SJarkko Nikula 	 * then but we shouldn't rely this flag for anything else than
1188d5898e19SJarkko Nikula 	 * stopping. For instance to differentiate between PIO and DMA
1189d5898e19SJarkko Nikula 	 * transfers.
1190d5898e19SJarkko Nikula 	 */
1191d5898e19SJarkko Nikula 	if (atomic_read(&drv_data->dma_running))
1192d5898e19SJarkko Nikula 		pxa2xx_spi_dma_stop(drv_data);
1193ca632f55SGrant Likely }
1194ca632f55SGrant Likely 
119551eea52dSLubomir Rintel static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
11967d94a505SMika Westerberg {
119751eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
11987d94a505SMika Westerberg 
11997d94a505SMika Westerberg 	/* Disable the SSP now */
120029d7e05cSLubomir Rintel 	pxa2xx_spi_off(drv_data);
12017d94a505SMika Westerberg 
12027d94a505SMika Westerberg 	return 0;
12037d94a505SMika Westerberg }
12047d94a505SMika Westerberg 
1205ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1206ca632f55SGrant Likely 		    struct pxa2xx_spi_chip *chip_info)
1207ca632f55SGrant Likely {
12083cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12093cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1210c18d925fSJan Kiszka 	struct gpio_desc *gpiod;
1211ca632f55SGrant Likely 	int err = 0;
1212ca632f55SGrant Likely 
121399f499cdSMika Westerberg 	if (chip == NULL)
121499f499cdSMika Westerberg 		return 0;
121599f499cdSMika Westerberg 
12166ac5a435SAndy Shevchenko 	if (drv_data->cs_gpiods) {
12176ac5a435SAndy Shevchenko 		gpiod = drv_data->cs_gpiods[spi->chip_select];
12186ac5a435SAndy Shevchenko 		if (gpiod) {
1219c18d925fSJan Kiszka 			chip->gpiod_cs = gpiod;
122099f499cdSMika Westerberg 			chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
122199f499cdSMika Westerberg 			gpiod_set_value(gpiod, chip->gpio_cs_inverted);
12226ac5a435SAndy Shevchenko 		}
122399f499cdSMika Westerberg 
122499f499cdSMika Westerberg 		return 0;
122599f499cdSMika Westerberg 	}
122699f499cdSMika Westerberg 
122799f499cdSMika Westerberg 	if (chip_info == NULL)
1228ca632f55SGrant Likely 		return 0;
1229ca632f55SGrant Likely 
1230ca632f55SGrant Likely 	/* NOTE: setup() can be called multiple times, possibly with
1231ca632f55SGrant Likely 	 * different chip_info, release previously requested GPIO
1232ca632f55SGrant Likely 	 */
1233c18d925fSJan Kiszka 	if (chip->gpiod_cs) {
1234a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1235c18d925fSJan Kiszka 		chip->gpiod_cs = NULL;
1236c18d925fSJan Kiszka 	}
1237ca632f55SGrant Likely 
1238ca632f55SGrant Likely 	/* If (*cs_control) is provided, ignore GPIO chip select */
1239ca632f55SGrant Likely 	if (chip_info->cs_control) {
1240ca632f55SGrant Likely 		chip->cs_control = chip_info->cs_control;
1241ca632f55SGrant Likely 		return 0;
1242ca632f55SGrant Likely 	}
1243ca632f55SGrant Likely 
1244ca632f55SGrant Likely 	if (gpio_is_valid(chip_info->gpio_cs)) {
1245ca632f55SGrant Likely 		err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1246ca632f55SGrant Likely 		if (err) {
1247f6bd03a7SJarkko Nikula 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1248f6bd03a7SJarkko Nikula 				chip_info->gpio_cs);
1249ca632f55SGrant Likely 			return err;
1250ca632f55SGrant Likely 		}
1251ca632f55SGrant Likely 
1252c18d925fSJan Kiszka 		gpiod = gpio_to_desc(chip_info->gpio_cs);
1253c18d925fSJan Kiszka 		chip->gpiod_cs = gpiod;
1254ca632f55SGrant Likely 		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1255ca632f55SGrant Likely 
1256c18d925fSJan Kiszka 		err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
1257ca632f55SGrant Likely 	}
1258ca632f55SGrant Likely 
1259ca632f55SGrant Likely 	return err;
1260ca632f55SGrant Likely }
1261ca632f55SGrant Likely 
1262ca632f55SGrant Likely static int setup(struct spi_device *spi)
1263ca632f55SGrant Likely {
1264bffc967eSJarkko Nikula 	struct pxa2xx_spi_chip *chip_info;
1265ca632f55SGrant Likely 	struct chip_data *chip;
1266dccf7369SJarkko Nikula 	const struct lpss_config *config;
12673cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
12683cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1269a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1270a0d2642eSMika Westerberg 
1271e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1272e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1273e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1274e5262d05SWeike Chen 		tx_hi_thres = 0;
1275e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1276e5262d05SWeike Chen 		break;
12777c7289a4SAndy Shevchenko 	case CE4100_SSP:
12787c7289a4SAndy Shevchenko 		tx_thres = TX_THRESH_CE4100_DFLT;
12797c7289a4SAndy Shevchenko 		tx_hi_thres = 0;
12807c7289a4SAndy Shevchenko 		rx_thres = RX_THRESH_CE4100_DFLT;
12817c7289a4SAndy Shevchenko 		break;
128203fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
128303fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
128430f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
128534cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1286b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1287fc0b2accSJarkko Nikula 	case LPSS_CNL_SSP:
1288dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1289dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1290dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1291dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1292e5262d05SWeike Chen 		break;
1293e5262d05SWeike Chen 	default:
1294a0d2642eSMika Westerberg 		tx_hi_thres = 0;
129551eea52dSLubomir Rintel 		if (spi_controller_is_slave(drv_data->controller)) {
1296ec93cb6fSLubomir Rintel 			tx_thres = 1;
1297ec93cb6fSLubomir Rintel 			rx_thres = 2;
1298ec93cb6fSLubomir Rintel 		} else {
1299ec93cb6fSLubomir Rintel 			tx_thres = TX_THRESH_DFLT;
1300a0d2642eSMika Westerberg 			rx_thres = RX_THRESH_DFLT;
1301ec93cb6fSLubomir Rintel 		}
1302e5262d05SWeike Chen 		break;
1303a0d2642eSMika Westerberg 	}
1304ca632f55SGrant Likely 
1305ca632f55SGrant Likely 	/* Only alloc on first setup */
1306ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1307ca632f55SGrant Likely 	if (!chip) {
1308ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
13099deae459SJingoo Han 		if (!chip)
1310ca632f55SGrant Likely 			return -ENOMEM;
1311ca632f55SGrant Likely 
1312ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1313ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1314f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1315f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1316ca632f55SGrant Likely 				kfree(chip);
1317ca632f55SGrant Likely 				return -EINVAL;
1318ca632f55SGrant Likely 			}
1319ca632f55SGrant Likely 
1320ca632f55SGrant Likely 			chip->frm = spi->chip_select;
1321c18d925fSJan Kiszka 		}
132251eea52dSLubomir Rintel 		chip->enable_dma = drv_data->controller_info->enable_dma;
1323ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1324ca632f55SGrant Likely 	}
1325ca632f55SGrant Likely 
1326ca632f55SGrant Likely 	/* protocol drivers may change the chip settings, so...
1327ca632f55SGrant Likely 	 * if chip_info exists, use it */
1328ca632f55SGrant Likely 	chip_info = spi->controller_data;
1329ca632f55SGrant Likely 
1330ca632f55SGrant Likely 	/* chip_info isn't always needed */
1331ca632f55SGrant Likely 	chip->cr1 = 0;
1332ca632f55SGrant Likely 	if (chip_info) {
1333ca632f55SGrant Likely 		if (chip_info->timeout)
1334ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1335ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1336ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1337a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1338a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1339ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1340ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1341ca632f55SGrant Likely 		chip->dma_threshold = 0;
1342ca632f55SGrant Likely 		if (chip_info->enable_loopback)
1343ca632f55SGrant Likely 			chip->cr1 = SSCR1_LBM;
1344ca632f55SGrant Likely 	}
134551eea52dSLubomir Rintel 	if (spi_controller_is_slave(drv_data->controller)) {
1346ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCFR;
1347ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SCLKDIR;
1348ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SFRMDIR;
1349ec93cb6fSLubomir Rintel 		chip->cr1 |= SSCR1_SPH;
1350ec93cb6fSLubomir Rintel 	}
1351ca632f55SGrant Likely 
1352a0d2642eSMika Westerberg 	chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1353a0d2642eSMika Westerberg 	chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1354a0d2642eSMika Westerberg 				| SSITF_TxHiThresh(tx_hi_thres);
1355a0d2642eSMika Westerberg 
1356ca632f55SGrant Likely 	/* set dma burst and threshold outside of chip_info path so that if
1357ca632f55SGrant Likely 	 * chip_info goes away after setting chip->enable_dma, the
1358ca632f55SGrant Likely 	 * burst and threshold can still respond to changes in bits_per_word */
1359ca632f55SGrant Likely 	if (chip->enable_dma) {
1360ca632f55SGrant Likely 		/* set up legal burst and threshold for dma */
1361cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1362cd7bed00SMika Westerberg 						spi->bits_per_word,
1363ca632f55SGrant Likely 						&chip->dma_burst_size,
1364ca632f55SGrant Likely 						&chip->dma_threshold)) {
1365f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1366f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1367ca632f55SGrant Likely 		}
1368000c6af4SAndy Shevchenko 		dev_dbg(&spi->dev,
1369000c6af4SAndy Shevchenko 			"in setup: DMA burst size set to %u\n",
1370000c6af4SAndy Shevchenko 			chip->dma_burst_size);
1371ca632f55SGrant Likely 	}
1372ca632f55SGrant Likely 
1373e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1374e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1375e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1376e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1377e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1378e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1379e5262d05SWeike Chen 		break;
13807c7289a4SAndy Shevchenko 	case CE4100_SSP:
13817c7289a4SAndy Shevchenko 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
13827c7289a4SAndy Shevchenko 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
13837c7289a4SAndy Shevchenko 		break;
1384e5262d05SWeike Chen 	default:
1385e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1386e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1387e5262d05SWeike Chen 		break;
1388e5262d05SWeike Chen 	}
1389e5262d05SWeike Chen 
1390ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1391ca632f55SGrant Likely 	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1392ca632f55SGrant Likely 			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1393ca632f55SGrant Likely 
1394b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1395b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1396b833172fSMika Westerberg 
1397ca632f55SGrant Likely 	if (spi->bits_per_word <= 8) {
1398ca632f55SGrant Likely 		chip->n_bytes = 1;
1399ca632f55SGrant Likely 		chip->read = u8_reader;
1400ca632f55SGrant Likely 		chip->write = u8_writer;
1401ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 16) {
1402ca632f55SGrant Likely 		chip->n_bytes = 2;
1403ca632f55SGrant Likely 		chip->read = u16_reader;
1404ca632f55SGrant Likely 		chip->write = u16_writer;
1405ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 32) {
1406ca632f55SGrant Likely 		chip->n_bytes = 4;
1407ca632f55SGrant Likely 		chip->read = u32_reader;
1408ca632f55SGrant Likely 		chip->write = u32_writer;
1409ca632f55SGrant Likely 	}
1410ca632f55SGrant Likely 
1411ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1412ca632f55SGrant Likely 
1413ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
1414ca632f55SGrant Likely 		return 0;
1415ca632f55SGrant Likely 
1416ca632f55SGrant Likely 	return setup_cs(spi, chip, chip_info);
1417ca632f55SGrant Likely }
1418ca632f55SGrant Likely 
1419ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1420ca632f55SGrant Likely {
1421ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
14223cc7b0e3SJarkko Nikula 	struct driver_data *drv_data =
14233cc7b0e3SJarkko Nikula 		spi_controller_get_devdata(spi->controller);
1424ca632f55SGrant Likely 
1425ca632f55SGrant Likely 	if (!chip)
1426ca632f55SGrant Likely 		return;
1427ca632f55SGrant Likely 
14286ac5a435SAndy Shevchenko 	if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
1429c18d925fSJan Kiszka 	    chip->gpiod_cs)
1430a885eebcSMark Brown 		gpiod_put(chip->gpiod_cs);
1431ca632f55SGrant Likely 
1432ca632f55SGrant Likely 	kfree(chip);
1433ca632f55SGrant Likely }
1434ca632f55SGrant Likely 
14358422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
143603fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
143703fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
143803fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
143903fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
144003fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
144130f3a6abSMika Westerberg 	{ "8086228E", LPSS_BSW_SSP },
144203fbf488SJarkko Nikula 	{ },
144303fbf488SJarkko Nikula };
144403fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
144503fbf488SJarkko Nikula 
144634cadd9cSJarkko Nikula /*
144734cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
144834cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
144934cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
145034cadd9cSJarkko Nikula  */
145134cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
145234cadd9cSJarkko Nikula 	/* SPT-LP */
145334cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
145434cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
145534cadd9cSJarkko Nikula 	/* SPT-H */
145634cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
145734cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1458704d2b07SMika Westerberg 	/* KBL-H */
1459704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1460704d2b07SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
14616157d4c2SJarkko Nikula 	/* CML-V */
14626157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
14636157d4c2SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
1464c1b03f11SJarkko Nikula 	/* BXT A-Step */
1465b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1466b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1467b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1468c1b03f11SJarkko Nikula 	/* BXT B-Step */
1469c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1470c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1471c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1472e18a80acSDavid E. Box 	/* GLK */
1473e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1474e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1475e18a80acSDavid E. Box 	{ PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
147622d71a50SMika Westerberg 	/* ICL-LP */
147722d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
147822d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
147922d71a50SMika Westerberg 	{ PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
14808cc77204SJarkko Nikula 	/* EHL */
14818cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
14828cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
14838cc77204SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
14849c7315c9SJarkko Nikula 	/* JSL */
14859c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
14869c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
14879c7315c9SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
1488*cf961fceSJarkko Nikula 	/* TGL-H */
1489*cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
1490*cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
1491*cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
1492*cf961fceSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
1493b7c08cf8SJarkko Nikula 	/* APL */
1494b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1495b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1496b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1497fc0b2accSJarkko Nikula 	/* CNL-LP */
1498fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1499fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1500fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1501fc0b2accSJarkko Nikula 	/* CNL-H */
1502fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1503fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1504fc0b2accSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
150541a91802SEvan Green 	/* CML-LP */
150641a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
150741a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
150841a91802SEvan Green 	{ PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
1509f0cf17edSJarkko Nikula 	/* CML-H */
1510f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1511f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1512f0cf17edSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
1513a4127952SJarkko Nikula 	/* TGL-LP */
1514a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1515a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1516a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1517a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1518a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1519a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1520a4127952SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
152194e5c23dSAxel Lin 	{ },
152234cadd9cSJarkko Nikula };
152334cadd9cSJarkko Nikula 
152487ae1d2dSLubomir Rintel static const struct of_device_id pxa2xx_spi_of_match[] = {
152587ae1d2dSLubomir Rintel 	{ .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
152687ae1d2dSLubomir Rintel 	{},
152787ae1d2dSLubomir Rintel };
152887ae1d2dSLubomir Rintel MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
152987ae1d2dSLubomir Rintel 
153087ae1d2dSLubomir Rintel #ifdef CONFIG_ACPI
153187ae1d2dSLubomir Rintel 
1532365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
153387ae1d2dSLubomir Rintel {
1534365e856eSAndy Shevchenko 	struct acpi_device *adev;
153587ae1d2dSLubomir Rintel 	unsigned int devid;
153687ae1d2dSLubomir Rintel 	int port_id = -1;
153787ae1d2dSLubomir Rintel 
1538365e856eSAndy Shevchenko 	adev = ACPI_COMPANION(dev);
153987ae1d2dSLubomir Rintel 	if (adev && adev->pnp.unique_id &&
154087ae1d2dSLubomir Rintel 	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
154187ae1d2dSLubomir Rintel 		port_id = devid;
154287ae1d2dSLubomir Rintel 	return port_id;
154387ae1d2dSLubomir Rintel }
154487ae1d2dSLubomir Rintel 
154587ae1d2dSLubomir Rintel #else /* !CONFIG_ACPI */
154687ae1d2dSLubomir Rintel 
1547365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev)
154887ae1d2dSLubomir Rintel {
154987ae1d2dSLubomir Rintel 	return -1;
155087ae1d2dSLubomir Rintel }
155187ae1d2dSLubomir Rintel 
155287ae1d2dSLubomir Rintel #endif /* CONFIG_ACPI */
155387ae1d2dSLubomir Rintel 
155487ae1d2dSLubomir Rintel 
155587ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
155687ae1d2dSLubomir Rintel 
155734cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
155834cadd9cSJarkko Nikula {
15595ba846b1SAndy Shevchenko 	return param == chan->device->dev;
156034cadd9cSJarkko Nikula }
156134cadd9cSJarkko Nikula 
156287ae1d2dSLubomir Rintel #endif /* CONFIG_PCI */
156387ae1d2dSLubomir Rintel 
156451eea52dSLubomir Rintel static struct pxa2xx_spi_controller *
15650db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1566a3496855SMika Westerberg {
156751eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *pdata;
1568a3496855SMika Westerberg 	struct ssp_device *ssp;
1569a3496855SMika Westerberg 	struct resource *res;
15706fb7427dSAndy Shevchenko 	struct device *parent = pdev->dev.parent;
15716fb7427dSAndy Shevchenko 	struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
157234cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
157355ef8262SLubomir Rintel 	enum pxa_ssp_type type;
1574f2faa3ecSAndy Shevchenko 	const void *match;
1575a3496855SMika Westerberg 
15766fb7427dSAndy Shevchenko 	if (pcidev)
15776fb7427dSAndy Shevchenko 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
1578a3496855SMika Westerberg 
1579f2faa3ecSAndy Shevchenko 	match = device_get_match_data(&pdev->dev);
1580f2faa3ecSAndy Shevchenko 	if (match)
1581f2faa3ecSAndy Shevchenko 		type = (enum pxa_ssp_type)match;
158234cadd9cSJarkko Nikula 	else if (pcidev_id)
158355ef8262SLubomir Rintel 		type = (enum pxa_ssp_type)pcidev_id->driver_data;
158403fbf488SJarkko Nikula 	else
158514af1df3SAndy Shevchenko 		return ERR_PTR(-EINVAL);
158603fbf488SJarkko Nikula 
1587cc0ee987SMika Westerberg 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
15889deae459SJingoo Han 	if (!pdata)
158914af1df3SAndy Shevchenko 		return ERR_PTR(-ENOMEM);
1590a3496855SMika Westerberg 
1591a3496855SMika Westerberg 	ssp = &pdata->ssp;
1592a3496855SMika Westerberg 
159377c544d2SAndy Shevchenko 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1594cbfd6a21SSachin Kamat 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1595cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
159614af1df3SAndy Shevchenko 		return ERR_CAST(ssp->mmio_base);
1597a3496855SMika Westerberg 
159877c544d2SAndy Shevchenko 	ssp->phys_base = res->start;
159977c544d2SAndy Shevchenko 
160087ae1d2dSLubomir Rintel #ifdef CONFIG_PCI
160134cadd9cSJarkko Nikula 	if (pcidev_id) {
16026fb7427dSAndy Shevchenko 		pdata->tx_param = parent;
16036fb7427dSAndy Shevchenko 		pdata->rx_param = parent;
160434cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
160534cadd9cSJarkko Nikula 	}
160687ae1d2dSLubomir Rintel #endif
160734cadd9cSJarkko Nikula 
1608a3496855SMika Westerberg 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
16095eb263efSChuhong Yuan 	if (IS_ERR(ssp->clk))
161014af1df3SAndy Shevchenko 		return ERR_CAST(ssp->clk);
1611a3496855SMika Westerberg 
1612a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
16135eb263efSChuhong Yuan 	if (ssp->irq < 0)
161414af1df3SAndy Shevchenko 		return ERR_PTR(ssp->irq);
16155eb263efSChuhong Yuan 
1616a3496855SMika Westerberg 	ssp->type = type;
16174f3d9577SAndy Shevchenko 	ssp->dev = &pdev->dev;
1618365e856eSAndy Shevchenko 	ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
1619a3496855SMika Westerberg 
1620f2faa3ecSAndy Shevchenko 	pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
1621a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1622cddb339bSMika Westerberg 	pdata->enable_dma = true;
162337821a82SAndy Shevchenko 	pdata->dma_burst_size = 1;
1624a3496855SMika Westerberg 
1625a3496855SMika Westerberg 	return pdata;
1626a3496855SMika Westerberg }
1627a3496855SMika Westerberg 
162851eea52dSLubomir Rintel static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
16293cc7b0e3SJarkko Nikula 				      unsigned int cs)
16300c27d9cfSMika Westerberg {
163151eea52dSLubomir Rintel 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
16320c27d9cfSMika Westerberg 
16330c27d9cfSMika Westerberg 	if (has_acpi_companion(&drv_data->pdev->dev)) {
16340c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
16350c27d9cfSMika Westerberg 		/*
16360c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
16370c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
16380c27d9cfSMika Westerberg 		 * to match what Linux expects.
16390c27d9cfSMika Westerberg 		 */
16400c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
164130f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
16420c27d9cfSMika Westerberg 			return cs - 1;
16430c27d9cfSMika Westerberg 
16440c27d9cfSMika Westerberg 		default:
16450c27d9cfSMika Westerberg 			break;
16460c27d9cfSMika Westerberg 		}
16470c27d9cfSMika Westerberg 	}
16480c27d9cfSMika Westerberg 
16490c27d9cfSMika Westerberg 	return cs;
16500c27d9cfSMika Westerberg }
16510c27d9cfSMika Westerberg 
1652b2662a16SDaniel Vetter static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1653b2662a16SDaniel Vetter {
1654b2662a16SDaniel Vetter 	return MAX_DMA_LEN;
1655b2662a16SDaniel Vetter }
1656b2662a16SDaniel Vetter 
1657fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1658ca632f55SGrant Likely {
1659ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
166051eea52dSLubomir Rintel 	struct pxa2xx_spi_controller *platform_info;
166151eea52dSLubomir Rintel 	struct spi_controller *controller;
1662ca632f55SGrant Likely 	struct driver_data *drv_data;
1663ca632f55SGrant Likely 	struct ssp_device *ssp;
16648b136baaSJarkko Nikula 	const struct lpss_config *config;
166599f499cdSMika Westerberg 	int status, count;
1666c039dd27SJarkko Nikula 	u32 tmp;
1667ca632f55SGrant Likely 
1668851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1669851bacf5SMika Westerberg 	if (!platform_info) {
16700db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
167114af1df3SAndy Shevchenko 		if (IS_ERR(platform_info)) {
1672851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
167314af1df3SAndy Shevchenko 			return PTR_ERR(platform_info);
1674851bacf5SMika Westerberg 		}
1675a3496855SMika Westerberg 	}
1676ca632f55SGrant Likely 
1677ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1678851bacf5SMika Westerberg 	if (!ssp)
1679851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1680851bacf5SMika Westerberg 
1681851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
1682851bacf5SMika Westerberg 		dev_err(&pdev->dev, "failed to get ssp\n");
1683ca632f55SGrant Likely 		return -ENODEV;
1684ca632f55SGrant Likely 	}
1685ca632f55SGrant Likely 
1686ec93cb6fSLubomir Rintel 	if (platform_info->is_slave)
168751eea52dSLubomir Rintel 		controller = spi_alloc_slave(dev, sizeof(struct driver_data));
1688ec93cb6fSLubomir Rintel 	else
168951eea52dSLubomir Rintel 		controller = spi_alloc_master(dev, sizeof(struct driver_data));
1690ec93cb6fSLubomir Rintel 
169151eea52dSLubomir Rintel 	if (!controller) {
169251eea52dSLubomir Rintel 		dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1693ca632f55SGrant Likely 		pxa_ssp_free(ssp);
1694ca632f55SGrant Likely 		return -ENOMEM;
1695ca632f55SGrant Likely 	}
169651eea52dSLubomir Rintel 	drv_data = spi_controller_get_devdata(controller);
169751eea52dSLubomir Rintel 	drv_data->controller = controller;
169851eea52dSLubomir Rintel 	drv_data->controller_info = platform_info;
1699ca632f55SGrant Likely 	drv_data->pdev = pdev;
1700ca632f55SGrant Likely 	drv_data->ssp = ssp;
1701ca632f55SGrant Likely 
170251eea52dSLubomir Rintel 	controller->dev.of_node = pdev->dev.of_node;
1703ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
170451eea52dSLubomir Rintel 	controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1705ca632f55SGrant Likely 
170651eea52dSLubomir Rintel 	controller->bus_num = ssp->port_id;
170751eea52dSLubomir Rintel 	controller->dma_alignment = DMA_ALIGNMENT;
170851eea52dSLubomir Rintel 	controller->cleanup = cleanup;
170951eea52dSLubomir Rintel 	controller->setup = setup;
171051eea52dSLubomir Rintel 	controller->set_cs = pxa2xx_spi_set_cs;
171151eea52dSLubomir Rintel 	controller->transfer_one = pxa2xx_spi_transfer_one;
171251eea52dSLubomir Rintel 	controller->slave_abort = pxa2xx_spi_slave_abort;
171351eea52dSLubomir Rintel 	controller->handle_err = pxa2xx_spi_handle_err;
171451eea52dSLubomir Rintel 	controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
171551eea52dSLubomir Rintel 	controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
171651eea52dSLubomir Rintel 	controller->auto_runtime_pm = true;
171751eea52dSLubomir Rintel 	controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1718ca632f55SGrant Likely 
1719ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1720ca632f55SGrant Likely 
1721ca632f55SGrant Likely 	drv_data->ioaddr = ssp->mmio_base;
1722ca632f55SGrant Likely 	drv_data->ssdr_physical = ssp->phys_base + SSDR;
1723ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1724e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1725e5262d05SWeike Chen 		case QUARK_X1000_SSP:
172651eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1727e5262d05SWeike Chen 			break;
1728e5262d05SWeike Chen 		default:
172951eea52dSLubomir Rintel 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1730e5262d05SWeike Chen 			break;
1731e5262d05SWeike Chen 		}
1732e5262d05SWeike Chen 
1733ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1734ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1735ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1736ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1737ca632f55SGrant Likely 	} else {
173851eea52dSLubomir Rintel 		controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1739ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
17405928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1741ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1742ec93cb6fSLubomir Rintel 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1743ec93cb6fSLubomir Rintel 						| SSSR_ROR | SSSR_TUR;
1744ca632f55SGrant Likely 	}
1745ca632f55SGrant Likely 
1746ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1747ca632f55SGrant Likely 			drv_data);
1748ca632f55SGrant Likely 	if (status < 0) {
1749ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
175051eea52dSLubomir Rintel 		goto out_error_controller_alloc;
1751ca632f55SGrant Likely 	}
1752ca632f55SGrant Likely 
1753ca632f55SGrant Likely 	/* Setup DMA if requested */
1754ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1755cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1756cd7bed00SMika Westerberg 		if (status) {
17578b57b11bSFlavio Suligoi 			dev_warn(dev, "no DMA channels available, using PIO\n");
1758cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1759b6ced294SJarkko Nikula 		} else {
176051eea52dSLubomir Rintel 			controller->can_dma = pxa2xx_spi_can_dma;
1761bf9f742cSMark Brown 			controller->max_dma_len = MAX_DMA_LEN;
1762b2662a16SDaniel Vetter 			controller->max_transfer_size =
1763b2662a16SDaniel Vetter 				pxa2xx_spi_max_dma_transfer_size;
1764ca632f55SGrant Likely 		}
1765ca632f55SGrant Likely 	}
1766ca632f55SGrant Likely 
1767ca632f55SGrant Likely 	/* Enable SOC clock */
176862bbc864STobias Jordan 	status = clk_prepare_enable(ssp->clk);
176962bbc864STobias Jordan 	if (status)
177062bbc864STobias Jordan 		goto out_error_dma_irq_alloc;
17713343b7a6SMika Westerberg 
177251eea52dSLubomir Rintel 	controller->max_speed_hz = clk_get_rate(ssp->clk);
177323cdddb2SJarkko Nikula 	/*
177423cdddb2SJarkko Nikula 	 * Set minimum speed for all other platforms than Intel Quark which is
177523cdddb2SJarkko Nikula 	 * able do under 1 Hz transfers.
177623cdddb2SJarkko Nikula 	 */
177723cdddb2SJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
177823cdddb2SJarkko Nikula 		controller->min_speed_hz =
177923cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 4096);
178023cdddb2SJarkko Nikula 	else if (!is_quark_x1000_ssp(drv_data))
178123cdddb2SJarkko Nikula 		controller->min_speed_hz =
178223cdddb2SJarkko Nikula 			DIV_ROUND_UP(controller->max_speed_hz, 512);
1783ca632f55SGrant Likely 
1784ca632f55SGrant Likely 	/* Load default SSP configuration */
1785c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
1786e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1787e5262d05SWeike Chen 	case QUARK_X1000_SSP:
17887c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
17897c7289a4SAndy Shevchenko 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1790c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1791e5262d05SWeike Chen 
1792e5262d05SWeike Chen 		/* using the Motorola SPI protocol and use 8 bit frame */
17937c7289a4SAndy Shevchenko 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
17947c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1795e5262d05SWeike Chen 		break;
17967c7289a4SAndy Shevchenko 	case CE4100_SSP:
17977c7289a4SAndy Shevchenko 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
17987c7289a4SAndy Shevchenko 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
17997c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
18007c7289a4SAndy Shevchenko 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
18017c7289a4SAndy Shevchenko 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1802a2dd8af0SAndy Shevchenko 		break;
1803e5262d05SWeike Chen 	default:
1804ec93cb6fSLubomir Rintel 
180551eea52dSLubomir Rintel 		if (spi_controller_is_slave(controller)) {
1806ec93cb6fSLubomir Rintel 			tmp = SSCR1_SCFR |
1807ec93cb6fSLubomir Rintel 			      SSCR1_SCLKDIR |
1808ec93cb6fSLubomir Rintel 			      SSCR1_SFRMDIR |
1809ec93cb6fSLubomir Rintel 			      SSCR1_RxTresh(2) |
1810ec93cb6fSLubomir Rintel 			      SSCR1_TxTresh(1) |
1811ec93cb6fSLubomir Rintel 			      SSCR1_SPH;
1812ec93cb6fSLubomir Rintel 		} else {
1813c039dd27SJarkko Nikula 			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1814c039dd27SJarkko Nikula 			      SSCR1_TxTresh(TX_THRESH_DFLT);
1815ec93cb6fSLubomir Rintel 		}
1816c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1817ec93cb6fSLubomir Rintel 		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
181851eea52dSLubomir Rintel 		if (!spi_controller_is_slave(controller))
1819ec93cb6fSLubomir Rintel 			tmp |= SSCR0_SCR(2);
1820c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1821e5262d05SWeike Chen 		break;
1822e5262d05SWeike Chen 	}
1823e5262d05SWeike Chen 
1824ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1825c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1826e5262d05SWeike Chen 
1827e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1828c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1829ca632f55SGrant Likely 
18308b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
18318b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
18328b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
18338b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
18348b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
18358b136baaSJarkko Nikula 						   config->reg_capabilities);
18368b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
18378b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
18388b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
183930f3a6abSMika Westerberg 		} else if (config->cs_num) {
184030f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
18418b136baaSJarkko Nikula 		}
18428b136baaSJarkko Nikula 	}
184351eea52dSLubomir Rintel 	controller->num_chipselect = platform_info->num_chipselect;
18448b136baaSJarkko Nikula 
184599f499cdSMika Westerberg 	count = gpiod_count(&pdev->dev, "cs");
18466ac5a435SAndy Shevchenko 	if (count > 0) {
18476ac5a435SAndy Shevchenko 		int i;
18486ac5a435SAndy Shevchenko 
184951eea52dSLubomir Rintel 		controller->num_chipselect = max_t(int, count,
185051eea52dSLubomir Rintel 			controller->num_chipselect);
185199f499cdSMika Westerberg 
18526ac5a435SAndy Shevchenko 		drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
185351eea52dSLubomir Rintel 			controller->num_chipselect, sizeof(struct gpio_desc *),
18546ac5a435SAndy Shevchenko 			GFP_KERNEL);
18556ac5a435SAndy Shevchenko 		if (!drv_data->cs_gpiods) {
18566ac5a435SAndy Shevchenko 			status = -ENOMEM;
18576ac5a435SAndy Shevchenko 			goto out_error_clock_enabled;
18586ac5a435SAndy Shevchenko 		}
18596ac5a435SAndy Shevchenko 
186051eea52dSLubomir Rintel 		for (i = 0; i < controller->num_chipselect; i++) {
18616ac5a435SAndy Shevchenko 			struct gpio_desc *gpiod;
18626ac5a435SAndy Shevchenko 
1863d35f2dc9SAndy Shevchenko 			gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
18646ac5a435SAndy Shevchenko 			if (IS_ERR(gpiod)) {
18656ac5a435SAndy Shevchenko 				/* Means use native chip select */
18666ac5a435SAndy Shevchenko 				if (PTR_ERR(gpiod) == -ENOENT)
18676ac5a435SAndy Shevchenko 					continue;
18686ac5a435SAndy Shevchenko 
186977d33897SLubomir Rintel 				status = PTR_ERR(gpiod);
18706ac5a435SAndy Shevchenko 				goto out_error_clock_enabled;
18716ac5a435SAndy Shevchenko 			} else {
18726ac5a435SAndy Shevchenko 				drv_data->cs_gpiods[i] = gpiod;
18736ac5a435SAndy Shevchenko 			}
18746ac5a435SAndy Shevchenko 		}
18756ac5a435SAndy Shevchenko 	}
18766ac5a435SAndy Shevchenko 
187777d33897SLubomir Rintel 	if (platform_info->is_slave) {
187877d33897SLubomir Rintel 		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
187977d33897SLubomir Rintel 						"ready", GPIOD_OUT_LOW);
188077d33897SLubomir Rintel 		if (IS_ERR(drv_data->gpiod_ready)) {
188177d33897SLubomir Rintel 			status = PTR_ERR(drv_data->gpiod_ready);
188277d33897SLubomir Rintel 			goto out_error_clock_enabled;
188377d33897SLubomir Rintel 		}
188477d33897SLubomir Rintel 	}
188577d33897SLubomir Rintel 
1886836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1887836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1888836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1889836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1890836d1a22SAntonio Ospite 
1891ca632f55SGrant Likely 	/* Register with the SPI framework */
1892ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
189351eea52dSLubomir Rintel 	status = devm_spi_register_controller(&pdev->dev, controller);
1894ca632f55SGrant Likely 	if (status != 0) {
189551eea52dSLubomir Rintel 		dev_err(&pdev->dev, "problem registering spi controller\n");
189612742045SLubomir Rintel 		goto out_error_pm_runtime_enabled;
1897ca632f55SGrant Likely 	}
1898ca632f55SGrant Likely 
1899ca632f55SGrant Likely 	return status;
1900ca632f55SGrant Likely 
190112742045SLubomir Rintel out_error_pm_runtime_enabled:
1902e2b714afSJarkko Nikula 	pm_runtime_put_noidle(&pdev->dev);
1903e2b714afSJarkko Nikula 	pm_runtime_disable(&pdev->dev);
190412742045SLubomir Rintel 
190512742045SLubomir Rintel out_error_clock_enabled:
19063343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
190762bbc864STobias Jordan 
190862bbc864STobias Jordan out_error_dma_irq_alloc:
1909cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1910ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1911ca632f55SGrant Likely 
191251eea52dSLubomir Rintel out_error_controller_alloc:
191351eea52dSLubomir Rintel 	spi_controller_put(controller);
1914ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1915ca632f55SGrant Likely 	return status;
1916ca632f55SGrant Likely }
1917ca632f55SGrant Likely 
1918ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1919ca632f55SGrant Likely {
1920ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
19213d24b2a4SAndy Shevchenko 	struct ssp_device *ssp = drv_data->ssp;
1922ca632f55SGrant Likely 
19237d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
19247d94a505SMika Westerberg 
1925ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
1926c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
19273343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1928ca632f55SGrant Likely 
1929ca632f55SGrant Likely 	/* Release DMA */
193051eea52dSLubomir Rintel 	if (drv_data->controller_info->enable_dma)
1931cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1932ca632f55SGrant Likely 
19337d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
19347d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
19357d94a505SMika Westerberg 
1936ca632f55SGrant Likely 	/* Release IRQ */
1937ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1938ca632f55SGrant Likely 
1939ca632f55SGrant Likely 	/* Release SSP */
1940ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1941ca632f55SGrant Likely 
1942ca632f55SGrant Likely 	return 0;
1943ca632f55SGrant Likely }
1944ca632f55SGrant Likely 
1945382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP
1946ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1947ca632f55SGrant Likely {
1948ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1949ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1950bffc967eSJarkko Nikula 	int status;
1951ca632f55SGrant Likely 
195251eea52dSLubomir Rintel 	status = spi_controller_suspend(drv_data->controller);
1953ca632f55SGrant Likely 	if (status != 0)
1954ca632f55SGrant Likely 		return status;
1955c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
19562b9375b9SDmitry Eremin-Solenikov 
19572b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
19583343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1959ca632f55SGrant Likely 
1960ca632f55SGrant Likely 	return 0;
1961ca632f55SGrant Likely }
1962ca632f55SGrant Likely 
1963ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1964ca632f55SGrant Likely {
1965ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1966ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1967bffc967eSJarkko Nikula 	int status;
1968ca632f55SGrant Likely 
1969ca632f55SGrant Likely 	/* Enable the SSP clock */
197062bbc864STobias Jordan 	if (!pm_runtime_suspended(dev)) {
197162bbc864STobias Jordan 		status = clk_prepare_enable(ssp->clk);
197262bbc864STobias Jordan 		if (status)
197362bbc864STobias Jordan 			return status;
197462bbc864STobias Jordan 	}
1975ca632f55SGrant Likely 
1976ca632f55SGrant Likely 	/* Start the queue running */
197751eea52dSLubomir Rintel 	return spi_controller_resume(drv_data->controller);
1978ca632f55SGrant Likely }
19797d94a505SMika Westerberg #endif
19807d94a505SMika Westerberg 
1981ec833050SRafael J. Wysocki #ifdef CONFIG_PM
19827d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
19837d94a505SMika Westerberg {
19847d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
19857d94a505SMika Westerberg 
19867d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
19877d94a505SMika Westerberg 	return 0;
19887d94a505SMika Westerberg }
19897d94a505SMika Westerberg 
19907d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
19917d94a505SMika Westerberg {
19927d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
199362bbc864STobias Jordan 	int status;
19947d94a505SMika Westerberg 
199562bbc864STobias Jordan 	status = clk_prepare_enable(drv_data->ssp->clk);
199662bbc864STobias Jordan 	return status;
19977d94a505SMika Westerberg }
19987d94a505SMika Westerberg #endif
1999ca632f55SGrant Likely 
2000ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
20017d94a505SMika Westerberg 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
20027d94a505SMika Westerberg 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
20037d94a505SMika Westerberg 			   pxa2xx_spi_runtime_resume, NULL)
2004ca632f55SGrant Likely };
2005ca632f55SGrant Likely 
2006ca632f55SGrant Likely static struct platform_driver driver = {
2007ca632f55SGrant Likely 	.driver = {
2008ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
2009ca632f55SGrant Likely 		.pm	= &pxa2xx_spi_pm_ops,
2010a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
201187ae1d2dSLubomir Rintel 		.of_match_table = of_match_ptr(pxa2xx_spi_of_match),
2012ca632f55SGrant Likely 	},
2013ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
2014ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
2015ca632f55SGrant Likely };
2016ca632f55SGrant Likely 
2017ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
2018ca632f55SGrant Likely {
2019ca632f55SGrant Likely 	return platform_driver_register(&driver);
2020ca632f55SGrant Likely }
2021ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
2022ca632f55SGrant Likely 
2023ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
2024ca632f55SGrant Likely {
2025ca632f55SGrant Likely 	platform_driver_unregister(&driver);
2026ca632f55SGrant Likely }
2027ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
202851ebf6acSFlavio Suligoi 
202951ebf6acSFlavio Suligoi MODULE_SOFTDEP("pre: dw_dmac");
2030