xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision c64e1265ae8516c0282b45e099ac5fbdb8486c4c)
1ca632f55SGrant Likely /*
2ca632f55SGrant Likely  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
3a0d2642eSMika Westerberg  * Copyright (C) 2013, Intel Corporation
4ca632f55SGrant Likely  *
5ca632f55SGrant Likely  * This program is free software; you can redistribute it and/or modify
6ca632f55SGrant Likely  * it under the terms of the GNU General Public License as published by
7ca632f55SGrant Likely  * the Free Software Foundation; either version 2 of the License, or
8ca632f55SGrant Likely  * (at your option) any later version.
9ca632f55SGrant Likely  *
10ca632f55SGrant Likely  * This program is distributed in the hope that it will be useful,
11ca632f55SGrant Likely  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ca632f55SGrant Likely  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13ca632f55SGrant Likely  * GNU General Public License for more details.
14ca632f55SGrant Likely  */
15ca632f55SGrant Likely 
168b136baaSJarkko Nikula #include <linux/bitops.h>
17ca632f55SGrant Likely #include <linux/init.h>
18ca632f55SGrant Likely #include <linux/module.h>
19ca632f55SGrant Likely #include <linux/device.h>
20ca632f55SGrant Likely #include <linux/ioport.h>
21ca632f55SGrant Likely #include <linux/errno.h>
22cbfd6a21SSachin Kamat #include <linux/err.h>
23ca632f55SGrant Likely #include <linux/interrupt.h>
249df461ecSAndy Shevchenko #include <linux/kernel.h>
2534cadd9cSJarkko Nikula #include <linux/pci.h>
26ca632f55SGrant Likely #include <linux/platform_device.h>
27ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h>
28ca632f55SGrant Likely #include <linux/spi/spi.h>
29ca632f55SGrant Likely #include <linux/delay.h>
30ca632f55SGrant Likely #include <linux/gpio.h>
31ca632f55SGrant Likely #include <linux/slab.h>
323343b7a6SMika Westerberg #include <linux/clk.h>
337d94a505SMika Westerberg #include <linux/pm_runtime.h>
34a3496855SMika Westerberg #include <linux/acpi.h>
35ca632f55SGrant Likely 
36cd7bed00SMika Westerberg #include "spi-pxa2xx.h"
37ca632f55SGrant Likely 
38ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street");
39ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
40ca632f55SGrant Likely MODULE_LICENSE("GPL");
41ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi");
42ca632f55SGrant Likely 
43ca632f55SGrant Likely #define TIMOUT_DFLT		1000
44ca632f55SGrant Likely 
45ca632f55SGrant Likely /*
46ca632f55SGrant Likely  * for testing SSCR1 changes that require SSP restart, basically
47ca632f55SGrant Likely  * everything except the service and interrupt enables, the pxa270 developer
48ca632f55SGrant Likely  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
49ca632f55SGrant Likely  * list, but the PXA255 dev man says all bits without really meaning the
50ca632f55SGrant Likely  * service and interrupt enables
51ca632f55SGrant Likely  */
52ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
53ca632f55SGrant Likely 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
54ca632f55SGrant Likely 				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
55ca632f55SGrant Likely 				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
56ca632f55SGrant Likely 				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
57ca632f55SGrant Likely 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
58ca632f55SGrant Likely 
59e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
60e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_EFWR	\
61e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_RFT		\
62e5262d05SWeike Chen 				| QUARK_X1000_SSCR1_TFT		\
63e5262d05SWeike Chen 				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
64e5262d05SWeike Chen 
65624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
66624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE			BIT(0)
67624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
688b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT			9
698b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
70a0d2642eSMika Westerberg 
71dccf7369SJarkko Nikula struct lpss_config {
72dccf7369SJarkko Nikula 	/* LPSS offset from drv_data->ioaddr */
73dccf7369SJarkko Nikula 	unsigned offset;
74dccf7369SJarkko Nikula 	/* Register offsets from drv_data->lpss_base or -1 */
75dccf7369SJarkko Nikula 	int reg_general;
76dccf7369SJarkko Nikula 	int reg_ssp;
77dccf7369SJarkko Nikula 	int reg_cs_ctrl;
788b136baaSJarkko Nikula 	int reg_capabilities;
79dccf7369SJarkko Nikula 	/* FIFO thresholds */
80dccf7369SJarkko Nikula 	u32 rx_threshold;
81dccf7369SJarkko Nikula 	u32 tx_threshold_lo;
82dccf7369SJarkko Nikula 	u32 tx_threshold_hi;
83c1e4a53cSMika Westerberg 	/* Chip select control */
84c1e4a53cSMika Westerberg 	unsigned cs_sel_shift;
85c1e4a53cSMika Westerberg 	unsigned cs_sel_mask;
8630f3a6abSMika Westerberg 	unsigned cs_num;
87dccf7369SJarkko Nikula };
88dccf7369SJarkko Nikula 
89dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */
90dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = {
91dccf7369SJarkko Nikula 	{	/* LPSS_LPT_SSP */
92dccf7369SJarkko Nikula 		.offset = 0x800,
93dccf7369SJarkko Nikula 		.reg_general = 0x08,
94dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
95dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
968b136baaSJarkko Nikula 		.reg_capabilities = -1,
97dccf7369SJarkko Nikula 		.rx_threshold = 64,
98dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
99dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
100dccf7369SJarkko Nikula 	},
101dccf7369SJarkko Nikula 	{	/* LPSS_BYT_SSP */
102dccf7369SJarkko Nikula 		.offset = 0x400,
103dccf7369SJarkko Nikula 		.reg_general = 0x08,
104dccf7369SJarkko Nikula 		.reg_ssp = 0x0c,
105dccf7369SJarkko Nikula 		.reg_cs_ctrl = 0x18,
1068b136baaSJarkko Nikula 		.reg_capabilities = -1,
107dccf7369SJarkko Nikula 		.rx_threshold = 64,
108dccf7369SJarkko Nikula 		.tx_threshold_lo = 160,
109dccf7369SJarkko Nikula 		.tx_threshold_hi = 224,
110dccf7369SJarkko Nikula 	},
11130f3a6abSMika Westerberg 	{	/* LPSS_BSW_SSP */
11230f3a6abSMika Westerberg 		.offset = 0x400,
11330f3a6abSMika Westerberg 		.reg_general = 0x08,
11430f3a6abSMika Westerberg 		.reg_ssp = 0x0c,
11530f3a6abSMika Westerberg 		.reg_cs_ctrl = 0x18,
11630f3a6abSMika Westerberg 		.reg_capabilities = -1,
11730f3a6abSMika Westerberg 		.rx_threshold = 64,
11830f3a6abSMika Westerberg 		.tx_threshold_lo = 160,
11930f3a6abSMika Westerberg 		.tx_threshold_hi = 224,
12030f3a6abSMika Westerberg 		.cs_sel_shift = 2,
12130f3a6abSMika Westerberg 		.cs_sel_mask = 1 << 2,
12230f3a6abSMika Westerberg 		.cs_num = 2,
12330f3a6abSMika Westerberg 	},
12434cadd9cSJarkko Nikula 	{	/* LPSS_SPT_SSP */
12534cadd9cSJarkko Nikula 		.offset = 0x200,
12634cadd9cSJarkko Nikula 		.reg_general = -1,
12734cadd9cSJarkko Nikula 		.reg_ssp = 0x20,
12834cadd9cSJarkko Nikula 		.reg_cs_ctrl = 0x24,
12966ec246eSJarkko Nikula 		.reg_capabilities = -1,
13034cadd9cSJarkko Nikula 		.rx_threshold = 1,
13134cadd9cSJarkko Nikula 		.tx_threshold_lo = 32,
13234cadd9cSJarkko Nikula 		.tx_threshold_hi = 56,
13334cadd9cSJarkko Nikula 	},
134b7c08cf8SJarkko Nikula 	{	/* LPSS_BXT_SSP */
135b7c08cf8SJarkko Nikula 		.offset = 0x200,
136b7c08cf8SJarkko Nikula 		.reg_general = -1,
137b7c08cf8SJarkko Nikula 		.reg_ssp = 0x20,
138b7c08cf8SJarkko Nikula 		.reg_cs_ctrl = 0x24,
139b7c08cf8SJarkko Nikula 		.reg_capabilities = 0xfc,
140b7c08cf8SJarkko Nikula 		.rx_threshold = 1,
141b7c08cf8SJarkko Nikula 		.tx_threshold_lo = 16,
142b7c08cf8SJarkko Nikula 		.tx_threshold_hi = 48,
143c1e4a53cSMika Westerberg 		.cs_sel_shift = 8,
144c1e4a53cSMika Westerberg 		.cs_sel_mask = 3 << 8,
145b7c08cf8SJarkko Nikula 	},
146dccf7369SJarkko Nikula };
147dccf7369SJarkko Nikula 
148dccf7369SJarkko Nikula static inline const struct lpss_config
149dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data)
150dccf7369SJarkko Nikula {
151dccf7369SJarkko Nikula 	return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
152dccf7369SJarkko Nikula }
153dccf7369SJarkko Nikula 
154a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data)
155a0d2642eSMika Westerberg {
15603fbf488SJarkko Nikula 	switch (drv_data->ssp_type) {
15703fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
15803fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
15930f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
16034cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
161b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
16203fbf488SJarkko Nikula 		return true;
16303fbf488SJarkko Nikula 	default:
16403fbf488SJarkko Nikula 		return false;
16503fbf488SJarkko Nikula 	}
166a0d2642eSMika Westerberg }
167a0d2642eSMika Westerberg 
168e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
169e5262d05SWeike Chen {
170e5262d05SWeike Chen 	return drv_data->ssp_type == QUARK_X1000_SSP;
171e5262d05SWeike Chen }
172e5262d05SWeike Chen 
1734fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
1744fdb2424SWeike Chen {
1754fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
176e5262d05SWeike Chen 	case QUARK_X1000_SSP:
177e5262d05SWeike Chen 		return QUARK_X1000_SSCR1_CHANGE_MASK;
1784fdb2424SWeike Chen 	default:
1794fdb2424SWeike Chen 		return SSCR1_CHANGE_MASK;
1804fdb2424SWeike Chen 	}
1814fdb2424SWeike Chen }
1824fdb2424SWeike Chen 
1834fdb2424SWeike Chen static u32
1844fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
1854fdb2424SWeike Chen {
1864fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
187e5262d05SWeike Chen 	case QUARK_X1000_SSP:
188e5262d05SWeike Chen 		return RX_THRESH_QUARK_X1000_DFLT;
1894fdb2424SWeike Chen 	default:
1904fdb2424SWeike Chen 		return RX_THRESH_DFLT;
1914fdb2424SWeike Chen 	}
1924fdb2424SWeike Chen }
1934fdb2424SWeike Chen 
1944fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
1954fdb2424SWeike Chen {
1964fdb2424SWeike Chen 	u32 mask;
1974fdb2424SWeike Chen 
1984fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
199e5262d05SWeike Chen 	case QUARK_X1000_SSP:
200e5262d05SWeike Chen 		mask = QUARK_X1000_SSSR_TFL_MASK;
201e5262d05SWeike Chen 		break;
2024fdb2424SWeike Chen 	default:
2034fdb2424SWeike Chen 		mask = SSSR_TFL_MASK;
2044fdb2424SWeike Chen 		break;
2054fdb2424SWeike Chen 	}
2064fdb2424SWeike Chen 
207c039dd27SJarkko Nikula 	return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
2084fdb2424SWeike Chen }
2094fdb2424SWeike Chen 
2104fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
2114fdb2424SWeike Chen 				     u32 *sccr1_reg)
2124fdb2424SWeike Chen {
2134fdb2424SWeike Chen 	u32 mask;
2144fdb2424SWeike Chen 
2154fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
216e5262d05SWeike Chen 	case QUARK_X1000_SSP:
217e5262d05SWeike Chen 		mask = QUARK_X1000_SSCR1_RFT;
218e5262d05SWeike Chen 		break;
2194fdb2424SWeike Chen 	default:
2204fdb2424SWeike Chen 		mask = SSCR1_RFT;
2214fdb2424SWeike Chen 		break;
2224fdb2424SWeike Chen 	}
2234fdb2424SWeike Chen 	*sccr1_reg &= ~mask;
2244fdb2424SWeike Chen }
2254fdb2424SWeike Chen 
2264fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
2274fdb2424SWeike Chen 				   u32 *sccr1_reg, u32 threshold)
2284fdb2424SWeike Chen {
2294fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
230e5262d05SWeike Chen 	case QUARK_X1000_SSP:
231e5262d05SWeike Chen 		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
232e5262d05SWeike Chen 		break;
2334fdb2424SWeike Chen 	default:
2344fdb2424SWeike Chen 		*sccr1_reg |= SSCR1_RxTresh(threshold);
2354fdb2424SWeike Chen 		break;
2364fdb2424SWeike Chen 	}
2374fdb2424SWeike Chen }
2384fdb2424SWeike Chen 
2394fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
2404fdb2424SWeike Chen 				  u32 clk_div, u8 bits)
2414fdb2424SWeike Chen {
2424fdb2424SWeike Chen 	switch (drv_data->ssp_type) {
243e5262d05SWeike Chen 	case QUARK_X1000_SSP:
244e5262d05SWeike Chen 		return clk_div
245e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_Motorola
246e5262d05SWeike Chen 			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
247e5262d05SWeike Chen 			| SSCR0_SSE;
2484fdb2424SWeike Chen 	default:
2494fdb2424SWeike Chen 		return clk_div
2504fdb2424SWeike Chen 			| SSCR0_Motorola
2514fdb2424SWeike Chen 			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
2524fdb2424SWeike Chen 			| SSCR0_SSE
2534fdb2424SWeike Chen 			| (bits > 16 ? SSCR0_EDSS : 0);
2544fdb2424SWeike Chen 	}
2554fdb2424SWeike Chen }
2564fdb2424SWeike Chen 
257a0d2642eSMika Westerberg /*
258a0d2642eSMika Westerberg  * Read and write LPSS SSP private registers. Caller must first check that
259a0d2642eSMika Westerberg  * is_lpss_ssp() returns true before these can be called.
260a0d2642eSMika Westerberg  */
261a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
262a0d2642eSMika Westerberg {
263a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
264a0d2642eSMika Westerberg 	return readl(drv_data->lpss_base + offset);
265a0d2642eSMika Westerberg }
266a0d2642eSMika Westerberg 
267a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data,
268a0d2642eSMika Westerberg 				  unsigned offset, u32 value)
269a0d2642eSMika Westerberg {
270a0d2642eSMika Westerberg 	WARN_ON(!drv_data->lpss_base);
271a0d2642eSMika Westerberg 	writel(value, drv_data->lpss_base + offset);
272a0d2642eSMika Westerberg }
273a0d2642eSMika Westerberg 
274a0d2642eSMika Westerberg /*
275a0d2642eSMika Westerberg  * lpss_ssp_setup - perform LPSS SSP specific setup
276a0d2642eSMika Westerberg  * @drv_data: pointer to the driver private data
277a0d2642eSMika Westerberg  *
278a0d2642eSMika Westerberg  * Perform LPSS SSP specific setup. This function must be called first if
279a0d2642eSMika Westerberg  * one is going to use LPSS SSP private registers.
280a0d2642eSMika Westerberg  */
281a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data)
282a0d2642eSMika Westerberg {
283dccf7369SJarkko Nikula 	const struct lpss_config *config;
284dccf7369SJarkko Nikula 	u32 value;
285a0d2642eSMika Westerberg 
286dccf7369SJarkko Nikula 	config = lpss_get_config(drv_data);
287dccf7369SJarkko Nikula 	drv_data->lpss_base = drv_data->ioaddr + config->offset;
288a0d2642eSMika Westerberg 
289a0d2642eSMika Westerberg 	/* Enable software chip select control */
2900e897218SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
291624ea72eSJarkko Nikula 	value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
292624ea72eSJarkko Nikula 	value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
293dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
2940054e28dSMika Westerberg 
2950054e28dSMika Westerberg 	/* Enable multiblock DMA transfers */
2961de70612SMika Westerberg 	if (drv_data->master_info->enable_dma) {
297dccf7369SJarkko Nikula 		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
2981de70612SMika Westerberg 
29982ba2c2aSJarkko Nikula 		if (config->reg_general >= 0) {
30082ba2c2aSJarkko Nikula 			value = __lpss_ssp_read_priv(drv_data,
30182ba2c2aSJarkko Nikula 						     config->reg_general);
302624ea72eSJarkko Nikula 			value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
30382ba2c2aSJarkko Nikula 			__lpss_ssp_write_priv(drv_data,
30482ba2c2aSJarkko Nikula 					      config->reg_general, value);
30582ba2c2aSJarkko Nikula 		}
3061de70612SMika Westerberg 	}
307a0d2642eSMika Westerberg }
308a0d2642eSMika Westerberg 
309c1e4a53cSMika Westerberg static void lpss_ssp_select_cs(struct driver_data *drv_data,
310c1e4a53cSMika Westerberg 			       const struct lpss_config *config)
311a0d2642eSMika Westerberg {
312d0283eb2SJarkko Nikula 	u32 value, cs;
313a0d2642eSMika Westerberg 
314c1e4a53cSMika Westerberg 	if (!config->cs_sel_mask)
315c1e4a53cSMika Westerberg 		return;
316dccf7369SJarkko Nikula 
317dccf7369SJarkko Nikula 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
318c1e4a53cSMika Westerberg 
319d0283eb2SJarkko Nikula 	cs = drv_data->cur_msg->spi->chip_select;
320c1e4a53cSMika Westerberg 	cs <<= config->cs_sel_shift;
321c1e4a53cSMika Westerberg 	if (cs != (value & config->cs_sel_mask)) {
322d0283eb2SJarkko Nikula 		/*
323c1e4a53cSMika Westerberg 		 * When switching another chip select output active the
324c1e4a53cSMika Westerberg 		 * output must be selected first and wait 2 ssp_clk cycles
325c1e4a53cSMika Westerberg 		 * before changing state to active. Otherwise a short
326c1e4a53cSMika Westerberg 		 * glitch will occur on the previous chip select since
327c1e4a53cSMika Westerberg 		 * output select is latched but state control is not.
328d0283eb2SJarkko Nikula 		 */
329c1e4a53cSMika Westerberg 		value &= ~config->cs_sel_mask;
330d0283eb2SJarkko Nikula 		value |= cs;
331d0283eb2SJarkko Nikula 		__lpss_ssp_write_priv(drv_data,
332d0283eb2SJarkko Nikula 				      config->reg_cs_ctrl, value);
333d0283eb2SJarkko Nikula 		ndelay(1000000000 /
334d0283eb2SJarkko Nikula 		       (drv_data->master->max_speed_hz / 2));
335d0283eb2SJarkko Nikula 	}
336d0283eb2SJarkko Nikula }
337c1e4a53cSMika Westerberg 
338c1e4a53cSMika Westerberg static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
339c1e4a53cSMika Westerberg {
340c1e4a53cSMika Westerberg 	const struct lpss_config *config;
341c1e4a53cSMika Westerberg 	u32 value;
342c1e4a53cSMika Westerberg 
343c1e4a53cSMika Westerberg 	config = lpss_get_config(drv_data);
344c1e4a53cSMika Westerberg 
345c1e4a53cSMika Westerberg 	if (enable)
346c1e4a53cSMika Westerberg 		lpss_ssp_select_cs(drv_data, config);
347c1e4a53cSMika Westerberg 
348c1e4a53cSMika Westerberg 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
349c1e4a53cSMika Westerberg 	if (enable)
350c1e4a53cSMika Westerberg 		value &= ~LPSS_CS_CONTROL_CS_HIGH;
351c1e4a53cSMika Westerberg 	else
352c1e4a53cSMika Westerberg 		value |= LPSS_CS_CONTROL_CS_HIGH;
353dccf7369SJarkko Nikula 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
354a0d2642eSMika Westerberg }
355a0d2642eSMika Westerberg 
356ca632f55SGrant Likely static void cs_assert(struct driver_data *drv_data)
357ca632f55SGrant Likely {
358ca632f55SGrant Likely 	struct chip_data *chip = drv_data->cur_chip;
359ca632f55SGrant Likely 
360ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP) {
361c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm);
362ca632f55SGrant Likely 		return;
363ca632f55SGrant Likely 	}
364ca632f55SGrant Likely 
365ca632f55SGrant Likely 	if (chip->cs_control) {
366ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_ASSERT);
367ca632f55SGrant Likely 		return;
368ca632f55SGrant Likely 	}
369ca632f55SGrant Likely 
370a0d2642eSMika Westerberg 	if (gpio_is_valid(chip->gpio_cs)) {
371ca632f55SGrant Likely 		gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
372a0d2642eSMika Westerberg 		return;
373a0d2642eSMika Westerberg 	}
374a0d2642eSMika Westerberg 
3757566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
376a0d2642eSMika Westerberg 		lpss_ssp_cs_control(drv_data, true);
377ca632f55SGrant Likely }
378ca632f55SGrant Likely 
379ca632f55SGrant Likely static void cs_deassert(struct driver_data *drv_data)
380ca632f55SGrant Likely {
381ca632f55SGrant Likely 	struct chip_data *chip = drv_data->cur_chip;
382ca632f55SGrant Likely 
383ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
384ca632f55SGrant Likely 		return;
385ca632f55SGrant Likely 
386ca632f55SGrant Likely 	if (chip->cs_control) {
387ca632f55SGrant Likely 		chip->cs_control(PXA2XX_CS_DEASSERT);
388ca632f55SGrant Likely 		return;
389ca632f55SGrant Likely 	}
390ca632f55SGrant Likely 
391a0d2642eSMika Westerberg 	if (gpio_is_valid(chip->gpio_cs)) {
392ca632f55SGrant Likely 		gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
393a0d2642eSMika Westerberg 		return;
394a0d2642eSMika Westerberg 	}
395a0d2642eSMika Westerberg 
3967566bcc7SJarkko Nikula 	if (is_lpss_ssp(drv_data))
397a0d2642eSMika Westerberg 		lpss_ssp_cs_control(drv_data, false);
398ca632f55SGrant Likely }
399ca632f55SGrant Likely 
400cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data)
401ca632f55SGrant Likely {
402ca632f55SGrant Likely 	unsigned long limit = loops_per_jiffy << 1;
403ca632f55SGrant Likely 
404ca632f55SGrant Likely 	do {
405c039dd27SJarkko Nikula 		while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
406c039dd27SJarkko Nikula 			pxa2xx_spi_read(drv_data, SSDR);
407c039dd27SJarkko Nikula 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
408ca632f55SGrant Likely 	write_SSSR_CS(drv_data, SSSR_ROR);
409ca632f55SGrant Likely 
410ca632f55SGrant Likely 	return limit;
411ca632f55SGrant Likely }
412ca632f55SGrant Likely 
413ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data)
414ca632f55SGrant Likely {
415ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
416ca632f55SGrant Likely 
4174fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
418ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
419ca632f55SGrant Likely 		return 0;
420ca632f55SGrant Likely 
421c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, 0);
422ca632f55SGrant Likely 	drv_data->tx += n_bytes;
423ca632f55SGrant Likely 
424ca632f55SGrant Likely 	return 1;
425ca632f55SGrant Likely }
426ca632f55SGrant Likely 
427ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data)
428ca632f55SGrant Likely {
429ca632f55SGrant Likely 	u8 n_bytes = drv_data->n_bytes;
430ca632f55SGrant Likely 
431c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
432ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
433c039dd27SJarkko Nikula 		pxa2xx_spi_read(drv_data, SSDR);
434ca632f55SGrant Likely 		drv_data->rx += n_bytes;
435ca632f55SGrant Likely 	}
436ca632f55SGrant Likely 
437ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
438ca632f55SGrant Likely }
439ca632f55SGrant Likely 
440ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data)
441ca632f55SGrant Likely {
4424fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
443ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
444ca632f55SGrant Likely 		return 0;
445ca632f55SGrant Likely 
446c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
447ca632f55SGrant Likely 	++drv_data->tx;
448ca632f55SGrant Likely 
449ca632f55SGrant Likely 	return 1;
450ca632f55SGrant Likely }
451ca632f55SGrant Likely 
452ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data)
453ca632f55SGrant Likely {
454c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
455ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
456c039dd27SJarkko Nikula 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
457ca632f55SGrant Likely 		++drv_data->rx;
458ca632f55SGrant Likely 	}
459ca632f55SGrant Likely 
460ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
461ca632f55SGrant Likely }
462ca632f55SGrant Likely 
463ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data)
464ca632f55SGrant Likely {
4654fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
466ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
467ca632f55SGrant Likely 		return 0;
468ca632f55SGrant Likely 
469c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
470ca632f55SGrant Likely 	drv_data->tx += 2;
471ca632f55SGrant Likely 
472ca632f55SGrant Likely 	return 1;
473ca632f55SGrant Likely }
474ca632f55SGrant Likely 
475ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data)
476ca632f55SGrant Likely {
477c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
478ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
479c039dd27SJarkko Nikula 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
480ca632f55SGrant Likely 		drv_data->rx += 2;
481ca632f55SGrant Likely 	}
482ca632f55SGrant Likely 
483ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
484ca632f55SGrant Likely }
485ca632f55SGrant Likely 
486ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data)
487ca632f55SGrant Likely {
4884fdb2424SWeike Chen 	if (pxa2xx_spi_txfifo_full(drv_data)
489ca632f55SGrant Likely 		|| (drv_data->tx == drv_data->tx_end))
490ca632f55SGrant Likely 		return 0;
491ca632f55SGrant Likely 
492c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
493ca632f55SGrant Likely 	drv_data->tx += 4;
494ca632f55SGrant Likely 
495ca632f55SGrant Likely 	return 1;
496ca632f55SGrant Likely }
497ca632f55SGrant Likely 
498ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data)
499ca632f55SGrant Likely {
500c039dd27SJarkko Nikula 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
501ca632f55SGrant Likely 	       && (drv_data->rx < drv_data->rx_end)) {
502c039dd27SJarkko Nikula 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
503ca632f55SGrant Likely 		drv_data->rx += 4;
504ca632f55SGrant Likely 	}
505ca632f55SGrant Likely 
506ca632f55SGrant Likely 	return drv_data->rx == drv_data->rx_end;
507ca632f55SGrant Likely }
508ca632f55SGrant Likely 
509cd7bed00SMika Westerberg void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
510ca632f55SGrant Likely {
511ca632f55SGrant Likely 	struct spi_message *msg = drv_data->cur_msg;
512ca632f55SGrant Likely 	struct spi_transfer *trans = drv_data->cur_transfer;
513ca632f55SGrant Likely 
514ca632f55SGrant Likely 	/* Move to next transfer */
515ca632f55SGrant Likely 	if (trans->transfer_list.next != &msg->transfers) {
516ca632f55SGrant Likely 		drv_data->cur_transfer =
517ca632f55SGrant Likely 			list_entry(trans->transfer_list.next,
518ca632f55SGrant Likely 					struct spi_transfer,
519ca632f55SGrant Likely 					transfer_list);
520ca632f55SGrant Likely 		return RUNNING_STATE;
521ca632f55SGrant Likely 	} else
522ca632f55SGrant Likely 		return DONE_STATE;
523ca632f55SGrant Likely }
524ca632f55SGrant Likely 
525ca632f55SGrant Likely /* caller already set message->status; dma and pio irqs are blocked */
526ca632f55SGrant Likely static void giveback(struct driver_data *drv_data)
527ca632f55SGrant Likely {
528ca632f55SGrant Likely 	struct spi_transfer* last_transfer;
529ca632f55SGrant Likely 	struct spi_message *msg;
5307a8d44bcSJarkko Nikula 	unsigned long timeout;
531ca632f55SGrant Likely 
532ca632f55SGrant Likely 	msg = drv_data->cur_msg;
533ca632f55SGrant Likely 	drv_data->cur_msg = NULL;
534ca632f55SGrant Likely 	drv_data->cur_transfer = NULL;
535ca632f55SGrant Likely 
53623e2c2aaSAxel Lin 	last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
537ca632f55SGrant Likely 					transfer_list);
538ca632f55SGrant Likely 
539ca632f55SGrant Likely 	/* Delay if requested before any change in chip select */
540ca632f55SGrant Likely 	if (last_transfer->delay_usecs)
541ca632f55SGrant Likely 		udelay(last_transfer->delay_usecs);
542ca632f55SGrant Likely 
5437a8d44bcSJarkko Nikula 	/* Wait until SSP becomes idle before deasserting the CS */
5447a8d44bcSJarkko Nikula 	timeout = jiffies + msecs_to_jiffies(10);
5457a8d44bcSJarkko Nikula 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
5467a8d44bcSJarkko Nikula 	       !time_after(jiffies, timeout))
5477a8d44bcSJarkko Nikula 		cpu_relax();
5487a8d44bcSJarkko Nikula 
549ca632f55SGrant Likely 	/* Drop chip select UNLESS cs_change is true or we are returning
550ca632f55SGrant Likely 	 * a message with an error, or next message is for another chip
551ca632f55SGrant Likely 	 */
552ca632f55SGrant Likely 	if (!last_transfer->cs_change)
553ca632f55SGrant Likely 		cs_deassert(drv_data);
554ca632f55SGrant Likely 	else {
555ca632f55SGrant Likely 		struct spi_message *next_msg;
556ca632f55SGrant Likely 
557ca632f55SGrant Likely 		/* Holding of cs was hinted, but we need to make sure
558ca632f55SGrant Likely 		 * the next message is for the same chip.  Don't waste
559ca632f55SGrant Likely 		 * time with the following tests unless this was hinted.
560ca632f55SGrant Likely 		 *
561ca632f55SGrant Likely 		 * We cannot postpone this until pump_messages, because
562ca632f55SGrant Likely 		 * after calling msg->complete (below) the driver that
563ca632f55SGrant Likely 		 * sent the current message could be unloaded, which
564ca632f55SGrant Likely 		 * could invalidate the cs_control() callback...
565ca632f55SGrant Likely 		 */
566ca632f55SGrant Likely 
567ca632f55SGrant Likely 		/* get a pointer to the next message, if any */
5687f86bde9SMika Westerberg 		next_msg = spi_get_next_queued_message(drv_data->master);
569ca632f55SGrant Likely 
570ca632f55SGrant Likely 		/* see if the next and current messages point
571ca632f55SGrant Likely 		 * to the same chip
572ca632f55SGrant Likely 		 */
573a52db659SChristophe Ricard 		if ((next_msg && next_msg->spi != msg->spi) ||
574a52db659SChristophe Ricard 		    msg->state == ERROR_STATE)
575ca632f55SGrant Likely 			cs_deassert(drv_data);
576ca632f55SGrant Likely 	}
577ca632f55SGrant Likely 
578ca632f55SGrant Likely 	drv_data->cur_chip = NULL;
579c957e8f0SMika Westerberg 	spi_finalize_current_message(drv_data->master);
580ca632f55SGrant Likely }
581ca632f55SGrant Likely 
582ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data)
583ca632f55SGrant Likely {
584ca632f55SGrant Likely 	struct chip_data *chip = drv_data->cur_chip;
585ca632f55SGrant Likely 	u32 sccr1_reg;
586ca632f55SGrant Likely 
587c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
588ca632f55SGrant Likely 	sccr1_reg &= ~SSCR1_RFT;
589ca632f55SGrant Likely 	sccr1_reg |= chip->threshold;
590c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
591ca632f55SGrant Likely }
592ca632f55SGrant Likely 
593ca632f55SGrant Likely static void int_error_stop(struct driver_data *drv_data, const char* msg)
594ca632f55SGrant Likely {
595ca632f55SGrant Likely 	/* Stop and reset SSP */
596ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
597ca632f55SGrant Likely 	reset_sccr1(drv_data);
598ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
599c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
600cd7bed00SMika Westerberg 	pxa2xx_spi_flush(drv_data);
601c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0,
602c039dd27SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
603ca632f55SGrant Likely 
604ca632f55SGrant Likely 	dev_err(&drv_data->pdev->dev, "%s\n", msg);
605ca632f55SGrant Likely 
606ca632f55SGrant Likely 	drv_data->cur_msg->state = ERROR_STATE;
607ca632f55SGrant Likely 	tasklet_schedule(&drv_data->pump_transfers);
608ca632f55SGrant Likely }
609ca632f55SGrant Likely 
610ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data)
611ca632f55SGrant Likely {
61207550df0SJarkko Nikula 	/* Clear and disable interrupts */
613ca632f55SGrant Likely 	write_SSSR_CS(drv_data, drv_data->clear_sr);
614ca632f55SGrant Likely 	reset_sccr1(drv_data);
615ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
616c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
617ca632f55SGrant Likely 
618ca632f55SGrant Likely 	/* Update total byte transferred return count actual bytes read */
619ca632f55SGrant Likely 	drv_data->cur_msg->actual_length += drv_data->len -
620ca632f55SGrant Likely 				(drv_data->rx_end - drv_data->rx);
621ca632f55SGrant Likely 
622ca632f55SGrant Likely 	/* Transfer delays and chip select release are
623ca632f55SGrant Likely 	 * handled in pump_transfers or giveback
624ca632f55SGrant Likely 	 */
625ca632f55SGrant Likely 
626ca632f55SGrant Likely 	/* Move to next transfer */
627cd7bed00SMika Westerberg 	drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
628ca632f55SGrant Likely 
629ca632f55SGrant Likely 	/* Schedule transfer tasklet */
630ca632f55SGrant Likely 	tasklet_schedule(&drv_data->pump_transfers);
631ca632f55SGrant Likely }
632ca632f55SGrant Likely 
633ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
634ca632f55SGrant Likely {
635c039dd27SJarkko Nikula 	u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
636ca632f55SGrant Likely 		       drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
637ca632f55SGrant Likely 
638c039dd27SJarkko Nikula 	u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
639ca632f55SGrant Likely 
640ca632f55SGrant Likely 	if (irq_status & SSSR_ROR) {
641ca632f55SGrant Likely 		int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
642ca632f55SGrant Likely 		return IRQ_HANDLED;
643ca632f55SGrant Likely 	}
644ca632f55SGrant Likely 
645ca632f55SGrant Likely 	if (irq_status & SSSR_TINT) {
646c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
647ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
648ca632f55SGrant Likely 			int_transfer_complete(drv_data);
649ca632f55SGrant Likely 			return IRQ_HANDLED;
650ca632f55SGrant Likely 		}
651ca632f55SGrant Likely 	}
652ca632f55SGrant Likely 
653ca632f55SGrant Likely 	/* Drain rx fifo, Fill tx fifo and prevent overruns */
654ca632f55SGrant Likely 	do {
655ca632f55SGrant Likely 		if (drv_data->read(drv_data)) {
656ca632f55SGrant Likely 			int_transfer_complete(drv_data);
657ca632f55SGrant Likely 			return IRQ_HANDLED;
658ca632f55SGrant Likely 		}
659ca632f55SGrant Likely 	} while (drv_data->write(drv_data));
660ca632f55SGrant Likely 
661ca632f55SGrant Likely 	if (drv_data->read(drv_data)) {
662ca632f55SGrant Likely 		int_transfer_complete(drv_data);
663ca632f55SGrant Likely 		return IRQ_HANDLED;
664ca632f55SGrant Likely 	}
665ca632f55SGrant Likely 
666ca632f55SGrant Likely 	if (drv_data->tx == drv_data->tx_end) {
667ca632f55SGrant Likely 		u32 bytes_left;
668ca632f55SGrant Likely 		u32 sccr1_reg;
669ca632f55SGrant Likely 
670c039dd27SJarkko Nikula 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
671ca632f55SGrant Likely 		sccr1_reg &= ~SSCR1_TIE;
672ca632f55SGrant Likely 
673ca632f55SGrant Likely 		/*
674ca632f55SGrant Likely 		 * PXA25x_SSP has no timeout, set up rx threshould for the
675ca632f55SGrant Likely 		 * remaining RX bytes.
676ca632f55SGrant Likely 		 */
677ca632f55SGrant Likely 		if (pxa25x_ssp_comp(drv_data)) {
6784fdb2424SWeike Chen 			u32 rx_thre;
679ca632f55SGrant Likely 
6804fdb2424SWeike Chen 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
681ca632f55SGrant Likely 
682ca632f55SGrant Likely 			bytes_left = drv_data->rx_end - drv_data->rx;
683ca632f55SGrant Likely 			switch (drv_data->n_bytes) {
684ca632f55SGrant Likely 			case 4:
685ca632f55SGrant Likely 				bytes_left >>= 1;
686ca632f55SGrant Likely 			case 2:
687ca632f55SGrant Likely 				bytes_left >>= 1;
688ca632f55SGrant Likely 			}
689ca632f55SGrant Likely 
6904fdb2424SWeike Chen 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
6914fdb2424SWeike Chen 			if (rx_thre > bytes_left)
6924fdb2424SWeike Chen 				rx_thre = bytes_left;
693ca632f55SGrant Likely 
6944fdb2424SWeike Chen 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
695ca632f55SGrant Likely 		}
696c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
697ca632f55SGrant Likely 	}
698ca632f55SGrant Likely 
699ca632f55SGrant Likely 	/* We did something */
700ca632f55SGrant Likely 	return IRQ_HANDLED;
701ca632f55SGrant Likely }
702ca632f55SGrant Likely 
703ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id)
704ca632f55SGrant Likely {
705ca632f55SGrant Likely 	struct driver_data *drv_data = dev_id;
7067d94a505SMika Westerberg 	u32 sccr1_reg;
707ca632f55SGrant Likely 	u32 mask = drv_data->mask_sr;
708ca632f55SGrant Likely 	u32 status;
709ca632f55SGrant Likely 
7107d94a505SMika Westerberg 	/*
7117d94a505SMika Westerberg 	 * The IRQ might be shared with other peripherals so we must first
7127d94a505SMika Westerberg 	 * check that are we RPM suspended or not. If we are we assume that
7137d94a505SMika Westerberg 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
7147d94a505SMika Westerberg 	 * interrupt is enabled).
7157d94a505SMika Westerberg 	 */
7167d94a505SMika Westerberg 	if (pm_runtime_suspended(&drv_data->pdev->dev))
7177d94a505SMika Westerberg 		return IRQ_NONE;
7187d94a505SMika Westerberg 
719269e4a41SMika Westerberg 	/*
720269e4a41SMika Westerberg 	 * If the device is not yet in RPM suspended state and we get an
721269e4a41SMika Westerberg 	 * interrupt that is meant for another device, check if status bits
722269e4a41SMika Westerberg 	 * are all set to one. That means that the device is already
723269e4a41SMika Westerberg 	 * powered off.
724269e4a41SMika Westerberg 	 */
725c039dd27SJarkko Nikula 	status = pxa2xx_spi_read(drv_data, SSSR);
726269e4a41SMika Westerberg 	if (status == ~0)
727269e4a41SMika Westerberg 		return IRQ_NONE;
728269e4a41SMika Westerberg 
729c039dd27SJarkko Nikula 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
730ca632f55SGrant Likely 
731ca632f55SGrant Likely 	/* Ignore possible writes if we don't need to write */
732ca632f55SGrant Likely 	if (!(sccr1_reg & SSCR1_TIE))
733ca632f55SGrant Likely 		mask &= ~SSSR_TFS;
734ca632f55SGrant Likely 
73502bc933eSTan, Jui Nee 	/* Ignore RX timeout interrupt if it is disabled */
73602bc933eSTan, Jui Nee 	if (!(sccr1_reg & SSCR1_TINTE))
73702bc933eSTan, Jui Nee 		mask &= ~SSSR_TINT;
73802bc933eSTan, Jui Nee 
739ca632f55SGrant Likely 	if (!(status & mask))
740ca632f55SGrant Likely 		return IRQ_NONE;
741ca632f55SGrant Likely 
742ca632f55SGrant Likely 	if (!drv_data->cur_msg) {
743ca632f55SGrant Likely 
744c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0,
745c039dd27SJarkko Nikula 				 pxa2xx_spi_read(drv_data, SSCR0)
746c039dd27SJarkko Nikula 				 & ~SSCR0_SSE);
747c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1,
748c039dd27SJarkko Nikula 				 pxa2xx_spi_read(drv_data, SSCR1)
749c039dd27SJarkko Nikula 				 & ~drv_data->int_cr1);
750ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
751c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, 0);
752ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
753ca632f55SGrant Likely 
754f6bd03a7SJarkko Nikula 		dev_err(&drv_data->pdev->dev,
755f6bd03a7SJarkko Nikula 			"bad message state in interrupt handler\n");
756ca632f55SGrant Likely 
757ca632f55SGrant Likely 		/* Never fail */
758ca632f55SGrant Likely 		return IRQ_HANDLED;
759ca632f55SGrant Likely 	}
760ca632f55SGrant Likely 
761ca632f55SGrant Likely 	return drv_data->transfer_handler(drv_data);
762ca632f55SGrant Likely }
763ca632f55SGrant Likely 
764e5262d05SWeike Chen /*
7659df461ecSAndy Shevchenko  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
7669df461ecSAndy Shevchenko  * input frequency by fractions of 2^24. It also has a divider by 5.
7679df461ecSAndy Shevchenko  *
7689df461ecSAndy Shevchenko  * There are formulas to get baud rate value for given input frequency and
7699df461ecSAndy Shevchenko  * divider parameters, such as DDS_CLK_RATE and SCR:
7709df461ecSAndy Shevchenko  *
7719df461ecSAndy Shevchenko  * Fsys = 200MHz
7729df461ecSAndy Shevchenko  *
7739df461ecSAndy Shevchenko  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
7749df461ecSAndy Shevchenko  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
7759df461ecSAndy Shevchenko  *
7769df461ecSAndy Shevchenko  * DDS_CLK_RATE either 2^n or 2^n / 5.
7779df461ecSAndy Shevchenko  * SCR is in range 0 .. 255
7789df461ecSAndy Shevchenko  *
7799df461ecSAndy Shevchenko  * Divisor = 5^i * 2^j * 2 * k
7809df461ecSAndy Shevchenko  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
7819df461ecSAndy Shevchenko  *       j = [0, 23]     j = 0 iff i = 1
7829df461ecSAndy Shevchenko  *       k = [1, 256]
7839df461ecSAndy Shevchenko  * Special case: j = 0, i = 1: Divisor = 2 / 5
7849df461ecSAndy Shevchenko  *
7859df461ecSAndy Shevchenko  * Accordingly to the specification the recommended values for DDS_CLK_RATE
7869df461ecSAndy Shevchenko  * are:
7879df461ecSAndy Shevchenko  *	Case 1:		2^n, n = [0, 23]
7889df461ecSAndy Shevchenko  *	Case 2:		2^24 * 2 / 5 (0x666666)
7899df461ecSAndy Shevchenko  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
7909df461ecSAndy Shevchenko  *
7919df461ecSAndy Shevchenko  * In all cases the lowest possible value is better.
7929df461ecSAndy Shevchenko  *
7939df461ecSAndy Shevchenko  * The function calculates parameters for all cases and chooses the one closest
7949df461ecSAndy Shevchenko  * to the asked baud rate.
795e5262d05SWeike Chen  */
7969df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
797e5262d05SWeike Chen {
7989df461ecSAndy Shevchenko 	unsigned long xtal = 200000000;
7999df461ecSAndy Shevchenko 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
8009df461ecSAndy Shevchenko 						   see (2) */
8019df461ecSAndy Shevchenko 						/* case 3 */
8029df461ecSAndy Shevchenko 	unsigned long fref1 = fref / 2;		/* case 1 */
8039df461ecSAndy Shevchenko 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
8049df461ecSAndy Shevchenko 	unsigned long scale;
8059df461ecSAndy Shevchenko 	unsigned long q, q1, q2;
8069df461ecSAndy Shevchenko 	long r, r1, r2;
8079df461ecSAndy Shevchenko 	u32 mul;
808e5262d05SWeike Chen 
8099df461ecSAndy Shevchenko 	/* Case 1 */
8109df461ecSAndy Shevchenko 
8119df461ecSAndy Shevchenko 	/* Set initial value for DDS_CLK_RATE */
8129df461ecSAndy Shevchenko 	mul = (1 << 24) >> 1;
8139df461ecSAndy Shevchenko 
8149df461ecSAndy Shevchenko 	/* Calculate initial quot */
8153ad48062SAndy Shevchenko 	q1 = DIV_ROUND_UP(fref1, rate);
8169df461ecSAndy Shevchenko 
8179df461ecSAndy Shevchenko 	/* Scale q1 if it's too big */
8189df461ecSAndy Shevchenko 	if (q1 > 256) {
8199df461ecSAndy Shevchenko 		/* Scale q1 to range [1, 512] */
8209df461ecSAndy Shevchenko 		scale = fls_long(q1 - 1);
8219df461ecSAndy Shevchenko 		if (scale > 9) {
8229df461ecSAndy Shevchenko 			q1 >>= scale - 9;
8239df461ecSAndy Shevchenko 			mul >>= scale - 9;
8249df461ecSAndy Shevchenko 		}
8259df461ecSAndy Shevchenko 
8269df461ecSAndy Shevchenko 		/* Round the result if we have a remainder */
8279df461ecSAndy Shevchenko 		q1 += q1 & 1;
8289df461ecSAndy Shevchenko 	}
8299df461ecSAndy Shevchenko 
8309df461ecSAndy Shevchenko 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
8319df461ecSAndy Shevchenko 	scale = __ffs(q1);
8329df461ecSAndy Shevchenko 	q1 >>= scale;
8339df461ecSAndy Shevchenko 	mul >>= scale;
8349df461ecSAndy Shevchenko 
8359df461ecSAndy Shevchenko 	/* Get the remainder */
8369df461ecSAndy Shevchenko 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
8379df461ecSAndy Shevchenko 
8389df461ecSAndy Shevchenko 	/* Case 2 */
8399df461ecSAndy Shevchenko 
8403ad48062SAndy Shevchenko 	q2 = DIV_ROUND_UP(fref2, rate);
8419df461ecSAndy Shevchenko 	r2 = abs(fref2 / q2 - rate);
8429df461ecSAndy Shevchenko 
8439df461ecSAndy Shevchenko 	/*
8449df461ecSAndy Shevchenko 	 * Choose the best between two: less remainder we have the better. We
8459df461ecSAndy Shevchenko 	 * can't go case 2 if q2 is greater than 256 since SCR register can
8469df461ecSAndy Shevchenko 	 * hold only values 0 .. 255.
8479df461ecSAndy Shevchenko 	 */
8489df461ecSAndy Shevchenko 	if (r2 >= r1 || q2 > 256) {
8499df461ecSAndy Shevchenko 		/* case 1 is better */
8509df461ecSAndy Shevchenko 		r = r1;
8519df461ecSAndy Shevchenko 		q = q1;
8529df461ecSAndy Shevchenko 	} else {
8539df461ecSAndy Shevchenko 		/* case 2 is better */
8549df461ecSAndy Shevchenko 		r = r2;
8559df461ecSAndy Shevchenko 		q = q2;
8569df461ecSAndy Shevchenko 		mul = (1 << 24) * 2 / 5;
8579df461ecSAndy Shevchenko 	}
8589df461ecSAndy Shevchenko 
8593ad48062SAndy Shevchenko 	/* Check case 3 only if the divisor is big enough */
8609df461ecSAndy Shevchenko 	if (fref / rate >= 80) {
8619df461ecSAndy Shevchenko 		u64 fssp;
8629df461ecSAndy Shevchenko 		u32 m;
8639df461ecSAndy Shevchenko 
8649df461ecSAndy Shevchenko 		/* Calculate initial quot */
8653ad48062SAndy Shevchenko 		q1 = DIV_ROUND_UP(fref, rate);
8669df461ecSAndy Shevchenko 		m = (1 << 24) / q1;
8679df461ecSAndy Shevchenko 
8689df461ecSAndy Shevchenko 		/* Get the remainder */
8699df461ecSAndy Shevchenko 		fssp = (u64)fref * m;
8709df461ecSAndy Shevchenko 		do_div(fssp, 1 << 24);
8719df461ecSAndy Shevchenko 		r1 = abs(fssp - rate);
8729df461ecSAndy Shevchenko 
8739df461ecSAndy Shevchenko 		/* Choose this one if it suits better */
8749df461ecSAndy Shevchenko 		if (r1 < r) {
8759df461ecSAndy Shevchenko 			/* case 3 is better */
8769df461ecSAndy Shevchenko 			q = 1;
8779df461ecSAndy Shevchenko 			mul = m;
878e5262d05SWeike Chen 		}
879e5262d05SWeike Chen 	}
880e5262d05SWeike Chen 
8819df461ecSAndy Shevchenko 	*dds = mul;
8829df461ecSAndy Shevchenko 	return q - 1;
883e5262d05SWeike Chen }
884e5262d05SWeike Chen 
8853343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
886ca632f55SGrant Likely {
8870eca7cf2SJarkko Nikula 	unsigned long ssp_clk = drv_data->master->max_speed_hz;
8883343b7a6SMika Westerberg 	const struct ssp_device *ssp = drv_data->ssp;
8893343b7a6SMika Westerberg 
8903343b7a6SMika Westerberg 	rate = min_t(int, ssp_clk, rate);
891ca632f55SGrant Likely 
892ca632f55SGrant Likely 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
893025ffe88SAndy Shevchenko 		return (ssp_clk / (2 * rate) - 1) & 0xff;
894ca632f55SGrant Likely 	else
895025ffe88SAndy Shevchenko 		return (ssp_clk / rate - 1) & 0xfff;
896ca632f55SGrant Likely }
897ca632f55SGrant Likely 
898e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
899d2c2f6a4SAndy Shevchenko 					   int rate)
900e5262d05SWeike Chen {
901d2c2f6a4SAndy Shevchenko 	struct chip_data *chip = drv_data->cur_chip;
902025ffe88SAndy Shevchenko 	unsigned int clk_div;
903e5262d05SWeike Chen 
904e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
905e5262d05SWeike Chen 	case QUARK_X1000_SSP:
9069df461ecSAndy Shevchenko 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
907eecacf73SDan Carpenter 		break;
908e5262d05SWeike Chen 	default:
909025ffe88SAndy Shevchenko 		clk_div = ssp_get_clk_div(drv_data, rate);
910eecacf73SDan Carpenter 		break;
911e5262d05SWeike Chen 	}
912025ffe88SAndy Shevchenko 	return clk_div << 8;
913e5262d05SWeike Chen }
914e5262d05SWeike Chen 
915ca632f55SGrant Likely static void pump_transfers(unsigned long data)
916ca632f55SGrant Likely {
917ca632f55SGrant Likely 	struct driver_data *drv_data = (struct driver_data *)data;
918ca632f55SGrant Likely 	struct spi_message *message = NULL;
919ca632f55SGrant Likely 	struct spi_transfer *transfer = NULL;
920ca632f55SGrant Likely 	struct spi_transfer *previous = NULL;
921ca632f55SGrant Likely 	struct chip_data *chip = NULL;
922ca632f55SGrant Likely 	u32 clk_div = 0;
923ca632f55SGrant Likely 	u8 bits = 0;
924ca632f55SGrant Likely 	u32 speed = 0;
925ca632f55SGrant Likely 	u32 cr0;
926ca632f55SGrant Likely 	u32 cr1;
927ca632f55SGrant Likely 	u32 dma_thresh = drv_data->cur_chip->dma_threshold;
928ca632f55SGrant Likely 	u32 dma_burst = drv_data->cur_chip->dma_burst_size;
9294fdb2424SWeike Chen 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
9307d1f1bf6SAndy Shevchenko 	int err;
931ca632f55SGrant Likely 
932ca632f55SGrant Likely 	/* Get current state information */
933ca632f55SGrant Likely 	message = drv_data->cur_msg;
934ca632f55SGrant Likely 	transfer = drv_data->cur_transfer;
935ca632f55SGrant Likely 	chip = drv_data->cur_chip;
936ca632f55SGrant Likely 
937ca632f55SGrant Likely 	/* Handle for abort */
938ca632f55SGrant Likely 	if (message->state == ERROR_STATE) {
939ca632f55SGrant Likely 		message->status = -EIO;
940ca632f55SGrant Likely 		giveback(drv_data);
941ca632f55SGrant Likely 		return;
942ca632f55SGrant Likely 	}
943ca632f55SGrant Likely 
944ca632f55SGrant Likely 	/* Handle end of message */
945ca632f55SGrant Likely 	if (message->state == DONE_STATE) {
946ca632f55SGrant Likely 		message->status = 0;
947ca632f55SGrant Likely 		giveback(drv_data);
948ca632f55SGrant Likely 		return;
949ca632f55SGrant Likely 	}
950ca632f55SGrant Likely 
951ca632f55SGrant Likely 	/* Delay if requested at end of transfer before CS change */
952ca632f55SGrant Likely 	if (message->state == RUNNING_STATE) {
953ca632f55SGrant Likely 		previous = list_entry(transfer->transfer_list.prev,
954ca632f55SGrant Likely 					struct spi_transfer,
955ca632f55SGrant Likely 					transfer_list);
956ca632f55SGrant Likely 		if (previous->delay_usecs)
957ca632f55SGrant Likely 			udelay(previous->delay_usecs);
958ca632f55SGrant Likely 
959ca632f55SGrant Likely 		/* Drop chip select only if cs_change is requested */
960ca632f55SGrant Likely 		if (previous->cs_change)
961ca632f55SGrant Likely 			cs_deassert(drv_data);
962ca632f55SGrant Likely 	}
963ca632f55SGrant Likely 
964cd7bed00SMika Westerberg 	/* Check if we can DMA this transfer */
965cd7bed00SMika Westerberg 	if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
966ca632f55SGrant Likely 
967ca632f55SGrant Likely 		/* reject already-mapped transfers; PIO won't always work */
968ca632f55SGrant Likely 		if (message->is_dma_mapped
969ca632f55SGrant Likely 				|| transfer->rx_dma || transfer->tx_dma) {
970ca632f55SGrant Likely 			dev_err(&drv_data->pdev->dev,
971f6bd03a7SJarkko Nikula 				"pump_transfers: mapped transfer length of "
972f6bd03a7SJarkko Nikula 				"%u is greater than %d\n",
973ca632f55SGrant Likely 				transfer->len, MAX_DMA_LEN);
974ca632f55SGrant Likely 			message->status = -EINVAL;
975ca632f55SGrant Likely 			giveback(drv_data);
976ca632f55SGrant Likely 			return;
977ca632f55SGrant Likely 		}
978ca632f55SGrant Likely 
979ca632f55SGrant Likely 		/* warn ... we force this to PIO mode */
980f6bd03a7SJarkko Nikula 		dev_warn_ratelimited(&message->spi->dev,
981f6bd03a7SJarkko Nikula 				     "pump_transfers: DMA disabled for transfer length %ld "
982ca632f55SGrant Likely 				     "greater than %d\n",
983ca632f55SGrant Likely 				     (long)drv_data->len, MAX_DMA_LEN);
984ca632f55SGrant Likely 	}
985ca632f55SGrant Likely 
986ca632f55SGrant Likely 	/* Setup the transfer state based on the type of transfer */
987cd7bed00SMika Westerberg 	if (pxa2xx_spi_flush(drv_data) == 0) {
988ca632f55SGrant Likely 		dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
989ca632f55SGrant Likely 		message->status = -EIO;
990ca632f55SGrant Likely 		giveback(drv_data);
991ca632f55SGrant Likely 		return;
992ca632f55SGrant Likely 	}
993ca632f55SGrant Likely 	drv_data->n_bytes = chip->n_bytes;
994ca632f55SGrant Likely 	drv_data->tx = (void *)transfer->tx_buf;
995ca632f55SGrant Likely 	drv_data->tx_end = drv_data->tx + transfer->len;
996ca632f55SGrant Likely 	drv_data->rx = transfer->rx_buf;
997ca632f55SGrant Likely 	drv_data->rx_end = drv_data->rx + transfer->len;
998cd7bed00SMika Westerberg 	drv_data->len = transfer->len;
999ca632f55SGrant Likely 	drv_data->write = drv_data->tx ? chip->write : null_writer;
1000ca632f55SGrant Likely 	drv_data->read = drv_data->rx ? chip->read : null_reader;
1001ca632f55SGrant Likely 
1002ca632f55SGrant Likely 	/* Change speed and bit per word on a per transfer */
1003ca632f55SGrant Likely 	bits = transfer->bits_per_word;
1004ca632f55SGrant Likely 	speed = transfer->speed_hz;
1005ca632f55SGrant Likely 
1006d2c2f6a4SAndy Shevchenko 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
1007ca632f55SGrant Likely 
1008ca632f55SGrant Likely 	if (bits <= 8) {
1009ca632f55SGrant Likely 		drv_data->n_bytes = 1;
1010ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1011ca632f55SGrant Likely 					u8_reader : null_reader;
1012ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1013ca632f55SGrant Likely 					u8_writer : null_writer;
1014ca632f55SGrant Likely 	} else if (bits <= 16) {
1015ca632f55SGrant Likely 		drv_data->n_bytes = 2;
1016ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1017ca632f55SGrant Likely 					u16_reader : null_reader;
1018ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1019ca632f55SGrant Likely 					u16_writer : null_writer;
1020ca632f55SGrant Likely 	} else if (bits <= 32) {
1021ca632f55SGrant Likely 		drv_data->n_bytes = 4;
1022ca632f55SGrant Likely 		drv_data->read = drv_data->read != null_reader ?
1023ca632f55SGrant Likely 					u32_reader : null_reader;
1024ca632f55SGrant Likely 		drv_data->write = drv_data->write != null_writer ?
1025ca632f55SGrant Likely 					u32_writer : null_writer;
1026ca632f55SGrant Likely 	}
1027196b0e2cSJarkko Nikula 	/*
1028196b0e2cSJarkko Nikula 	 * if bits/word is changed in dma mode, then must check the
1029196b0e2cSJarkko Nikula 	 * thresholds and burst also
1030196b0e2cSJarkko Nikula 	 */
1031ca632f55SGrant Likely 	if (chip->enable_dma) {
1032cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
1033cd7bed00SMika Westerberg 						message->spi,
1034ca632f55SGrant Likely 						bits, &dma_burst,
1035ca632f55SGrant Likely 						&dma_thresh))
1036f6bd03a7SJarkko Nikula 			dev_warn_ratelimited(&message->spi->dev,
1037f6bd03a7SJarkko Nikula 					     "pump_transfers: DMA burst size reduced to match bits_per_word\n");
1038ca632f55SGrant Likely 	}
1039ca632f55SGrant Likely 
1040ca632f55SGrant Likely 	message->state = RUNNING_STATE;
1041ca632f55SGrant Likely 
1042ca632f55SGrant Likely 	drv_data->dma_mapped = 0;
1043cd7bed00SMika Westerberg 	if (pxa2xx_spi_dma_is_possible(drv_data->len))
1044cd7bed00SMika Westerberg 		drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
1045ca632f55SGrant Likely 	if (drv_data->dma_mapped) {
1046ca632f55SGrant Likely 
1047ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler */
1048cd7bed00SMika Westerberg 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1049ca632f55SGrant Likely 
10507d1f1bf6SAndy Shevchenko 		err = pxa2xx_spi_dma_prepare(drv_data, dma_burst);
10517d1f1bf6SAndy Shevchenko 		if (err) {
10527d1f1bf6SAndy Shevchenko 			message->status = err;
10537d1f1bf6SAndy Shevchenko 			giveback(drv_data);
10547d1f1bf6SAndy Shevchenko 			return;
10557d1f1bf6SAndy Shevchenko 		}
1056ca632f55SGrant Likely 
1057ca632f55SGrant Likely 		/* Clear status and start DMA engine */
1058ca632f55SGrant Likely 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1059c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1060cd7bed00SMika Westerberg 
1061cd7bed00SMika Westerberg 		pxa2xx_spi_dma_start(drv_data);
1062ca632f55SGrant Likely 	} else {
1063ca632f55SGrant Likely 		/* Ensure we have the correct interrupt handler	*/
1064ca632f55SGrant Likely 		drv_data->transfer_handler = interrupt_transfer;
1065ca632f55SGrant Likely 
1066ca632f55SGrant Likely 		/* Clear status  */
1067ca632f55SGrant Likely 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1068ca632f55SGrant Likely 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1069ca632f55SGrant Likely 	}
1070ca632f55SGrant Likely 
1071ee03672dSJarkko Nikula 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1072ee03672dSJarkko Nikula 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1073ee03672dSJarkko Nikula 	if (!pxa25x_ssp_comp(drv_data))
1074ee03672dSJarkko Nikula 		dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
1075ee03672dSJarkko Nikula 			drv_data->master->max_speed_hz
1076ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1077ee03672dSJarkko Nikula 			drv_data->dma_mapped ? "DMA" : "PIO");
1078ee03672dSJarkko Nikula 	else
1079ee03672dSJarkko Nikula 		dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
1080ee03672dSJarkko Nikula 			drv_data->master->max_speed_hz / 2
1081ee03672dSJarkko Nikula 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1082ee03672dSJarkko Nikula 			drv_data->dma_mapped ? "DMA" : "PIO");
1083ee03672dSJarkko Nikula 
1084a0d2642eSMika Westerberg 	if (is_lpss_ssp(drv_data)) {
1085c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
1086c039dd27SJarkko Nikula 		    != chip->lpss_rx_threshold)
1087c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSIRF,
1088c039dd27SJarkko Nikula 					 chip->lpss_rx_threshold);
1089c039dd27SJarkko Nikula 		if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
1090c039dd27SJarkko Nikula 		    != chip->lpss_tx_threshold)
1091c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSITF,
1092c039dd27SJarkko Nikula 					 chip->lpss_tx_threshold);
1093a0d2642eSMika Westerberg 	}
1094a0d2642eSMika Westerberg 
1095e5262d05SWeike Chen 	if (is_quark_x1000_ssp(drv_data) &&
1096c039dd27SJarkko Nikula 	    (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
1097c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
1098e5262d05SWeike Chen 
1099ca632f55SGrant Likely 	/* see if we need to reload the config registers */
1100c039dd27SJarkko Nikula 	if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1101c039dd27SJarkko Nikula 	    || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1102c039dd27SJarkko Nikula 	    != (cr1 & change_mask)) {
1103ca632f55SGrant Likely 		/* stop the SSP, and update the other bits */
1104c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
1105ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1106c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1107ca632f55SGrant Likely 		/* first set CR1 without interrupt and service enables */
1108c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
1109ca632f55SGrant Likely 		/* restart the SSP */
1110c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, cr0);
1111ca632f55SGrant Likely 
1112ca632f55SGrant Likely 	} else {
1113ca632f55SGrant Likely 		if (!pxa25x_ssp_comp(drv_data))
1114c039dd27SJarkko Nikula 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1115ca632f55SGrant Likely 	}
1116ca632f55SGrant Likely 
1117ca632f55SGrant Likely 	cs_assert(drv_data);
1118ca632f55SGrant Likely 
1119ca632f55SGrant Likely 	/* after chip select, release the data by enabling service
1120ca632f55SGrant Likely 	 * requests and interrupts, without changing any mode bits */
1121c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1122ca632f55SGrant Likely }
1123ca632f55SGrant Likely 
11247f86bde9SMika Westerberg static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
11257f86bde9SMika Westerberg 					   struct spi_message *msg)
1126ca632f55SGrant Likely {
11277f86bde9SMika Westerberg 	struct driver_data *drv_data = spi_master_get_devdata(master);
1128ca632f55SGrant Likely 
11297f86bde9SMika Westerberg 	drv_data->cur_msg = msg;
1130ca632f55SGrant Likely 	/* Initial message state*/
1131ca632f55SGrant Likely 	drv_data->cur_msg->state = START_STATE;
1132ca632f55SGrant Likely 	drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1133ca632f55SGrant Likely 						struct spi_transfer,
1134ca632f55SGrant Likely 						transfer_list);
1135ca632f55SGrant Likely 
1136ca632f55SGrant Likely 	/* prepare to setup the SSP, in pump_transfers, using the per
1137ca632f55SGrant Likely 	 * chip configuration */
1138ca632f55SGrant Likely 	drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
1139ca632f55SGrant Likely 
1140ca632f55SGrant Likely 	/* Mark as busy and launch transfers */
1141ca632f55SGrant Likely 	tasklet_schedule(&drv_data->pump_transfers);
1142ca632f55SGrant Likely 	return 0;
1143ca632f55SGrant Likely }
1144ca632f55SGrant Likely 
11457d94a505SMika Westerberg static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
11467d94a505SMika Westerberg {
11477d94a505SMika Westerberg 	struct driver_data *drv_data = spi_master_get_devdata(master);
11487d94a505SMika Westerberg 
11497d94a505SMika Westerberg 	/* Disable the SSP now */
1150c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0,
1151c039dd27SJarkko Nikula 			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
11527d94a505SMika Westerberg 
11537d94a505SMika Westerberg 	return 0;
11547d94a505SMika Westerberg }
11557d94a505SMika Westerberg 
1156ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1157ca632f55SGrant Likely 		    struct pxa2xx_spi_chip *chip_info)
1158ca632f55SGrant Likely {
1159ca632f55SGrant Likely 	int err = 0;
1160ca632f55SGrant Likely 
1161ca632f55SGrant Likely 	if (chip == NULL || chip_info == NULL)
1162ca632f55SGrant Likely 		return 0;
1163ca632f55SGrant Likely 
1164ca632f55SGrant Likely 	/* NOTE: setup() can be called multiple times, possibly with
1165ca632f55SGrant Likely 	 * different chip_info, release previously requested GPIO
1166ca632f55SGrant Likely 	 */
1167ca632f55SGrant Likely 	if (gpio_is_valid(chip->gpio_cs))
1168ca632f55SGrant Likely 		gpio_free(chip->gpio_cs);
1169ca632f55SGrant Likely 
1170ca632f55SGrant Likely 	/* If (*cs_control) is provided, ignore GPIO chip select */
1171ca632f55SGrant Likely 	if (chip_info->cs_control) {
1172ca632f55SGrant Likely 		chip->cs_control = chip_info->cs_control;
1173ca632f55SGrant Likely 		return 0;
1174ca632f55SGrant Likely 	}
1175ca632f55SGrant Likely 
1176ca632f55SGrant Likely 	if (gpio_is_valid(chip_info->gpio_cs)) {
1177ca632f55SGrant Likely 		err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1178ca632f55SGrant Likely 		if (err) {
1179f6bd03a7SJarkko Nikula 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1180f6bd03a7SJarkko Nikula 				chip_info->gpio_cs);
1181ca632f55SGrant Likely 			return err;
1182ca632f55SGrant Likely 		}
1183ca632f55SGrant Likely 
1184ca632f55SGrant Likely 		chip->gpio_cs = chip_info->gpio_cs;
1185ca632f55SGrant Likely 		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1186ca632f55SGrant Likely 
1187ca632f55SGrant Likely 		err = gpio_direction_output(chip->gpio_cs,
1188ca632f55SGrant Likely 					!chip->gpio_cs_inverted);
1189ca632f55SGrant Likely 	}
1190ca632f55SGrant Likely 
1191ca632f55SGrant Likely 	return err;
1192ca632f55SGrant Likely }
1193ca632f55SGrant Likely 
1194ca632f55SGrant Likely static int setup(struct spi_device *spi)
1195ca632f55SGrant Likely {
1196ca632f55SGrant Likely 	struct pxa2xx_spi_chip *chip_info = NULL;
1197ca632f55SGrant Likely 	struct chip_data *chip;
1198dccf7369SJarkko Nikula 	const struct lpss_config *config;
1199ca632f55SGrant Likely 	struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1200a0d2642eSMika Westerberg 	uint tx_thres, tx_hi_thres, rx_thres;
1201a0d2642eSMika Westerberg 
1202e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1203e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1204e5262d05SWeike Chen 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1205e5262d05SWeike Chen 		tx_hi_thres = 0;
1206e5262d05SWeike Chen 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1207e5262d05SWeike Chen 		break;
120803fbf488SJarkko Nikula 	case LPSS_LPT_SSP:
120903fbf488SJarkko Nikula 	case LPSS_BYT_SSP:
121030f3a6abSMika Westerberg 	case LPSS_BSW_SSP:
121134cadd9cSJarkko Nikula 	case LPSS_SPT_SSP:
1212b7c08cf8SJarkko Nikula 	case LPSS_BXT_SSP:
1213dccf7369SJarkko Nikula 		config = lpss_get_config(drv_data);
1214dccf7369SJarkko Nikula 		tx_thres = config->tx_threshold_lo;
1215dccf7369SJarkko Nikula 		tx_hi_thres = config->tx_threshold_hi;
1216dccf7369SJarkko Nikula 		rx_thres = config->rx_threshold;
1217e5262d05SWeike Chen 		break;
1218e5262d05SWeike Chen 	default:
1219a0d2642eSMika Westerberg 		tx_thres = TX_THRESH_DFLT;
1220a0d2642eSMika Westerberg 		tx_hi_thres = 0;
1221a0d2642eSMika Westerberg 		rx_thres = RX_THRESH_DFLT;
1222e5262d05SWeike Chen 		break;
1223a0d2642eSMika Westerberg 	}
1224ca632f55SGrant Likely 
1225ca632f55SGrant Likely 	/* Only alloc on first setup */
1226ca632f55SGrant Likely 	chip = spi_get_ctldata(spi);
1227ca632f55SGrant Likely 	if (!chip) {
1228ca632f55SGrant Likely 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
12299deae459SJingoo Han 		if (!chip)
1230ca632f55SGrant Likely 			return -ENOMEM;
1231ca632f55SGrant Likely 
1232ca632f55SGrant Likely 		if (drv_data->ssp_type == CE4100_SSP) {
1233ca632f55SGrant Likely 			if (spi->chip_select > 4) {
1234f6bd03a7SJarkko Nikula 				dev_err(&spi->dev,
1235f6bd03a7SJarkko Nikula 					"failed setup: cs number must not be > 4.\n");
1236ca632f55SGrant Likely 				kfree(chip);
1237ca632f55SGrant Likely 				return -EINVAL;
1238ca632f55SGrant Likely 			}
1239ca632f55SGrant Likely 
1240ca632f55SGrant Likely 			chip->frm = spi->chip_select;
1241ca632f55SGrant Likely 		} else
1242ca632f55SGrant Likely 			chip->gpio_cs = -1;
1243*c64e1265SDan O'Donovan 		chip->enable_dma = drv_data->master_info->enable_dma;
1244ca632f55SGrant Likely 		chip->timeout = TIMOUT_DFLT;
1245ca632f55SGrant Likely 	}
1246ca632f55SGrant Likely 
1247ca632f55SGrant Likely 	/* protocol drivers may change the chip settings, so...
1248ca632f55SGrant Likely 	 * if chip_info exists, use it */
1249ca632f55SGrant Likely 	chip_info = spi->controller_data;
1250ca632f55SGrant Likely 
1251ca632f55SGrant Likely 	/* chip_info isn't always needed */
1252ca632f55SGrant Likely 	chip->cr1 = 0;
1253ca632f55SGrant Likely 	if (chip_info) {
1254ca632f55SGrant Likely 		if (chip_info->timeout)
1255ca632f55SGrant Likely 			chip->timeout = chip_info->timeout;
1256ca632f55SGrant Likely 		if (chip_info->tx_threshold)
1257ca632f55SGrant Likely 			tx_thres = chip_info->tx_threshold;
1258a0d2642eSMika Westerberg 		if (chip_info->tx_hi_threshold)
1259a0d2642eSMika Westerberg 			tx_hi_thres = chip_info->tx_hi_threshold;
1260ca632f55SGrant Likely 		if (chip_info->rx_threshold)
1261ca632f55SGrant Likely 			rx_thres = chip_info->rx_threshold;
1262ca632f55SGrant Likely 		chip->dma_threshold = 0;
1263ca632f55SGrant Likely 		if (chip_info->enable_loopback)
1264ca632f55SGrant Likely 			chip->cr1 = SSCR1_LBM;
1265ca632f55SGrant Likely 	}
1266ca632f55SGrant Likely 
1267a0d2642eSMika Westerberg 	chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1268a0d2642eSMika Westerberg 	chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1269a0d2642eSMika Westerberg 				| SSITF_TxHiThresh(tx_hi_thres);
1270a0d2642eSMika Westerberg 
1271ca632f55SGrant Likely 	/* set dma burst and threshold outside of chip_info path so that if
1272ca632f55SGrant Likely 	 * chip_info goes away after setting chip->enable_dma, the
1273ca632f55SGrant Likely 	 * burst and threshold can still respond to changes in bits_per_word */
1274ca632f55SGrant Likely 	if (chip->enable_dma) {
1275ca632f55SGrant Likely 		/* set up legal burst and threshold for dma */
1276cd7bed00SMika Westerberg 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1277cd7bed00SMika Westerberg 						spi->bits_per_word,
1278ca632f55SGrant Likely 						&chip->dma_burst_size,
1279ca632f55SGrant Likely 						&chip->dma_threshold)) {
1280f6bd03a7SJarkko Nikula 			dev_warn(&spi->dev,
1281f6bd03a7SJarkko Nikula 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1282ca632f55SGrant Likely 		}
1283ca632f55SGrant Likely 	}
1284ca632f55SGrant Likely 
1285e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1286e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1287e5262d05SWeike Chen 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1288e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_RFT)
1289e5262d05SWeike Chen 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1290e5262d05SWeike Chen 				   & QUARK_X1000_SSCR1_TFT);
1291e5262d05SWeike Chen 		break;
1292e5262d05SWeike Chen 	default:
1293e5262d05SWeike Chen 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1294e5262d05SWeike Chen 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1295e5262d05SWeike Chen 		break;
1296e5262d05SWeike Chen 	}
1297e5262d05SWeike Chen 
1298ca632f55SGrant Likely 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1299ca632f55SGrant Likely 	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1300ca632f55SGrant Likely 			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1301ca632f55SGrant Likely 
1302b833172fSMika Westerberg 	if (spi->mode & SPI_LOOP)
1303b833172fSMika Westerberg 		chip->cr1 |= SSCR1_LBM;
1304b833172fSMika Westerberg 
1305ca632f55SGrant Likely 	if (spi->bits_per_word <= 8) {
1306ca632f55SGrant Likely 		chip->n_bytes = 1;
1307ca632f55SGrant Likely 		chip->read = u8_reader;
1308ca632f55SGrant Likely 		chip->write = u8_writer;
1309ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 16) {
1310ca632f55SGrant Likely 		chip->n_bytes = 2;
1311ca632f55SGrant Likely 		chip->read = u16_reader;
1312ca632f55SGrant Likely 		chip->write = u16_writer;
1313ca632f55SGrant Likely 	} else if (spi->bits_per_word <= 32) {
1314ca632f55SGrant Likely 		chip->n_bytes = 4;
1315ca632f55SGrant Likely 		chip->read = u32_reader;
1316ca632f55SGrant Likely 		chip->write = u32_writer;
1317ca632f55SGrant Likely 	}
1318ca632f55SGrant Likely 
1319ca632f55SGrant Likely 	spi_set_ctldata(spi, chip);
1320ca632f55SGrant Likely 
1321ca632f55SGrant Likely 	if (drv_data->ssp_type == CE4100_SSP)
1322ca632f55SGrant Likely 		return 0;
1323ca632f55SGrant Likely 
1324ca632f55SGrant Likely 	return setup_cs(spi, chip, chip_info);
1325ca632f55SGrant Likely }
1326ca632f55SGrant Likely 
1327ca632f55SGrant Likely static void cleanup(struct spi_device *spi)
1328ca632f55SGrant Likely {
1329ca632f55SGrant Likely 	struct chip_data *chip = spi_get_ctldata(spi);
1330ca632f55SGrant Likely 	struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1331ca632f55SGrant Likely 
1332ca632f55SGrant Likely 	if (!chip)
1333ca632f55SGrant Likely 		return;
1334ca632f55SGrant Likely 
1335ca632f55SGrant Likely 	if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
1336ca632f55SGrant Likely 		gpio_free(chip->gpio_cs);
1337ca632f55SGrant Likely 
1338ca632f55SGrant Likely 	kfree(chip);
1339ca632f55SGrant Likely }
1340ca632f55SGrant Likely 
13410db64215SJarkko Nikula #ifdef CONFIG_PCI
1342a3496855SMika Westerberg #ifdef CONFIG_ACPI
134303fbf488SJarkko Nikula 
13448422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
134503fbf488SJarkko Nikula 	{ "INT33C0", LPSS_LPT_SSP },
134603fbf488SJarkko Nikula 	{ "INT33C1", LPSS_LPT_SSP },
134703fbf488SJarkko Nikula 	{ "INT3430", LPSS_LPT_SSP },
134803fbf488SJarkko Nikula 	{ "INT3431", LPSS_LPT_SSP },
134903fbf488SJarkko Nikula 	{ "80860F0E", LPSS_BYT_SSP },
135030f3a6abSMika Westerberg 	{ "8086228E", LPSS_BSW_SSP },
135103fbf488SJarkko Nikula 	{ },
135203fbf488SJarkko Nikula };
135303fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
135403fbf488SJarkko Nikula 
13550db64215SJarkko Nikula static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
13560db64215SJarkko Nikula {
13570db64215SJarkko Nikula 	unsigned int devid;
13580db64215SJarkko Nikula 	int port_id = -1;
13590db64215SJarkko Nikula 
13600db64215SJarkko Nikula 	if (adev && adev->pnp.unique_id &&
13610db64215SJarkko Nikula 	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
13620db64215SJarkko Nikula 		port_id = devid;
13630db64215SJarkko Nikula 	return port_id;
13640db64215SJarkko Nikula }
13650db64215SJarkko Nikula #else /* !CONFIG_ACPI */
13660db64215SJarkko Nikula static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
13670db64215SJarkko Nikula {
13680db64215SJarkko Nikula 	return -1;
13690db64215SJarkko Nikula }
13700db64215SJarkko Nikula #endif
13710db64215SJarkko Nikula 
137234cadd9cSJarkko Nikula /*
137334cadd9cSJarkko Nikula  * PCI IDs of compound devices that integrate both host controller and private
137434cadd9cSJarkko Nikula  * integrated DMA engine. Please note these are not used in module
137534cadd9cSJarkko Nikula  * autoloading and probing in this module but matching the LPSS SSP type.
137634cadd9cSJarkko Nikula  */
137734cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
137834cadd9cSJarkko Nikula 	/* SPT-LP */
137934cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
138034cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
138134cadd9cSJarkko Nikula 	/* SPT-H */
138234cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
138334cadd9cSJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1384c1b03f11SJarkko Nikula 	/* BXT A-Step */
1385b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1386b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1387b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1388c1b03f11SJarkko Nikula 	/* BXT B-Step */
1389c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1390c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1391c1b03f11SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1392b7c08cf8SJarkko Nikula 	/* APL */
1393b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1394b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1395b7c08cf8SJarkko Nikula 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
139694e5c23dSAxel Lin 	{ },
139734cadd9cSJarkko Nikula };
139834cadd9cSJarkko Nikula 
139934cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
140034cadd9cSJarkko Nikula {
140134cadd9cSJarkko Nikula 	struct device *dev = param;
140234cadd9cSJarkko Nikula 
140334cadd9cSJarkko Nikula 	if (dev != chan->device->dev->parent)
140434cadd9cSJarkko Nikula 		return false;
140534cadd9cSJarkko Nikula 
140634cadd9cSJarkko Nikula 	return true;
140734cadd9cSJarkko Nikula }
140834cadd9cSJarkko Nikula 
1409a3496855SMika Westerberg static struct pxa2xx_spi_master *
14100db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1411a3496855SMika Westerberg {
1412a3496855SMika Westerberg 	struct pxa2xx_spi_master *pdata;
1413a3496855SMika Westerberg 	struct acpi_device *adev;
1414a3496855SMika Westerberg 	struct ssp_device *ssp;
1415a3496855SMika Westerberg 	struct resource *res;
141634cadd9cSJarkko Nikula 	const struct acpi_device_id *adev_id = NULL;
141734cadd9cSJarkko Nikula 	const struct pci_device_id *pcidev_id = NULL;
14183b8b6d05SJarkko Nikula 	int type;
1419a3496855SMika Westerberg 
1420b9f6940aSJarkko Nikula 	adev = ACPI_COMPANION(&pdev->dev);
1421a3496855SMika Westerberg 
142234cadd9cSJarkko Nikula 	if (dev_is_pci(pdev->dev.parent))
142334cadd9cSJarkko Nikula 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
142434cadd9cSJarkko Nikula 					 to_pci_dev(pdev->dev.parent));
14250db64215SJarkko Nikula 	else if (adev)
142634cadd9cSJarkko Nikula 		adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
142734cadd9cSJarkko Nikula 					    &pdev->dev);
14280db64215SJarkko Nikula 	else
14290db64215SJarkko Nikula 		return NULL;
143034cadd9cSJarkko Nikula 
143134cadd9cSJarkko Nikula 	if (adev_id)
143234cadd9cSJarkko Nikula 		type = (int)adev_id->driver_data;
143334cadd9cSJarkko Nikula 	else if (pcidev_id)
143434cadd9cSJarkko Nikula 		type = (int)pcidev_id->driver_data;
143503fbf488SJarkko Nikula 	else
143603fbf488SJarkko Nikula 		return NULL;
143703fbf488SJarkko Nikula 
1438cc0ee987SMika Westerberg 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
14399deae459SJingoo Han 	if (!pdata)
1440a3496855SMika Westerberg 		return NULL;
1441a3496855SMika Westerberg 
1442a3496855SMika Westerberg 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1443a3496855SMika Westerberg 	if (!res)
1444a3496855SMika Westerberg 		return NULL;
1445a3496855SMika Westerberg 
1446a3496855SMika Westerberg 	ssp = &pdata->ssp;
1447a3496855SMika Westerberg 
1448a3496855SMika Westerberg 	ssp->phys_base = res->start;
1449cbfd6a21SSachin Kamat 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1450cbfd6a21SSachin Kamat 	if (IS_ERR(ssp->mmio_base))
14516dc81f6fSMika Westerberg 		return NULL;
1452a3496855SMika Westerberg 
145334cadd9cSJarkko Nikula 	if (pcidev_id) {
145434cadd9cSJarkko Nikula 		pdata->tx_param = pdev->dev.parent;
145534cadd9cSJarkko Nikula 		pdata->rx_param = pdev->dev.parent;
145634cadd9cSJarkko Nikula 		pdata->dma_filter = pxa2xx_spi_idma_filter;
145734cadd9cSJarkko Nikula 	}
145834cadd9cSJarkko Nikula 
1459a3496855SMika Westerberg 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
1460a3496855SMika Westerberg 	ssp->irq = platform_get_irq(pdev, 0);
146103fbf488SJarkko Nikula 	ssp->type = type;
1462a3496855SMika Westerberg 	ssp->pdev = pdev;
14630db64215SJarkko Nikula 	ssp->port_id = pxa2xx_spi_get_port_id(adev);
1464a3496855SMika Westerberg 
1465a3496855SMika Westerberg 	pdata->num_chipselect = 1;
1466cddb339bSMika Westerberg 	pdata->enable_dma = true;
1467a3496855SMika Westerberg 
1468a3496855SMika Westerberg 	return pdata;
1469a3496855SMika Westerberg }
1470a3496855SMika Westerberg 
14710db64215SJarkko Nikula #else /* !CONFIG_PCI */
1472a3496855SMika Westerberg static inline struct pxa2xx_spi_master *
14730db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev)
1474a3496855SMika Westerberg {
1475a3496855SMika Westerberg 	return NULL;
1476a3496855SMika Westerberg }
1477a3496855SMika Westerberg #endif
1478a3496855SMika Westerberg 
14790c27d9cfSMika Westerberg static int pxa2xx_spi_fw_translate_cs(struct spi_master *master, unsigned cs)
14800c27d9cfSMika Westerberg {
14810c27d9cfSMika Westerberg 	struct driver_data *drv_data = spi_master_get_devdata(master);
14820c27d9cfSMika Westerberg 
14830c27d9cfSMika Westerberg 	if (has_acpi_companion(&drv_data->pdev->dev)) {
14840c27d9cfSMika Westerberg 		switch (drv_data->ssp_type) {
14850c27d9cfSMika Westerberg 		/*
14860c27d9cfSMika Westerberg 		 * For Atoms the ACPI DeviceSelection used by the Windows
14870c27d9cfSMika Westerberg 		 * driver starts from 1 instead of 0 so translate it here
14880c27d9cfSMika Westerberg 		 * to match what Linux expects.
14890c27d9cfSMika Westerberg 		 */
14900c27d9cfSMika Westerberg 		case LPSS_BYT_SSP:
149130f3a6abSMika Westerberg 		case LPSS_BSW_SSP:
14920c27d9cfSMika Westerberg 			return cs - 1;
14930c27d9cfSMika Westerberg 
14940c27d9cfSMika Westerberg 		default:
14950c27d9cfSMika Westerberg 			break;
14960c27d9cfSMika Westerberg 		}
14970c27d9cfSMika Westerberg 	}
14980c27d9cfSMika Westerberg 
14990c27d9cfSMika Westerberg 	return cs;
15000c27d9cfSMika Westerberg }
15010c27d9cfSMika Westerberg 
1502fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev)
1503ca632f55SGrant Likely {
1504ca632f55SGrant Likely 	struct device *dev = &pdev->dev;
1505ca632f55SGrant Likely 	struct pxa2xx_spi_master *platform_info;
1506ca632f55SGrant Likely 	struct spi_master *master;
1507ca632f55SGrant Likely 	struct driver_data *drv_data;
1508ca632f55SGrant Likely 	struct ssp_device *ssp;
15098b136baaSJarkko Nikula 	const struct lpss_config *config;
1510ca632f55SGrant Likely 	int status;
1511c039dd27SJarkko Nikula 	u32 tmp;
1512ca632f55SGrant Likely 
1513851bacf5SMika Westerberg 	platform_info = dev_get_platdata(dev);
1514851bacf5SMika Westerberg 	if (!platform_info) {
15150db64215SJarkko Nikula 		platform_info = pxa2xx_spi_init_pdata(pdev);
1516a3496855SMika Westerberg 		if (!platform_info) {
1517851bacf5SMika Westerberg 			dev_err(&pdev->dev, "missing platform data\n");
1518851bacf5SMika Westerberg 			return -ENODEV;
1519851bacf5SMika Westerberg 		}
1520a3496855SMika Westerberg 	}
1521ca632f55SGrant Likely 
1522ca632f55SGrant Likely 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1523851bacf5SMika Westerberg 	if (!ssp)
1524851bacf5SMika Westerberg 		ssp = &platform_info->ssp;
1525851bacf5SMika Westerberg 
1526851bacf5SMika Westerberg 	if (!ssp->mmio_base) {
1527851bacf5SMika Westerberg 		dev_err(&pdev->dev, "failed to get ssp\n");
1528ca632f55SGrant Likely 		return -ENODEV;
1529ca632f55SGrant Likely 	}
1530ca632f55SGrant Likely 
1531757fe8d5SJarkko Nikula 	master = spi_alloc_master(dev, sizeof(struct driver_data));
1532ca632f55SGrant Likely 	if (!master) {
1533ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot alloc spi_master\n");
1534ca632f55SGrant Likely 		pxa_ssp_free(ssp);
1535ca632f55SGrant Likely 		return -ENOMEM;
1536ca632f55SGrant Likely 	}
1537ca632f55SGrant Likely 	drv_data = spi_master_get_devdata(master);
1538ca632f55SGrant Likely 	drv_data->master = master;
1539ca632f55SGrant Likely 	drv_data->master_info = platform_info;
1540ca632f55SGrant Likely 	drv_data->pdev = pdev;
1541ca632f55SGrant Likely 	drv_data->ssp = ssp;
1542ca632f55SGrant Likely 
1543ca632f55SGrant Likely 	master->dev.of_node = pdev->dev.of_node;
1544ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
1545b833172fSMika Westerberg 	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1546ca632f55SGrant Likely 
1547851bacf5SMika Westerberg 	master->bus_num = ssp->port_id;
1548ca632f55SGrant Likely 	master->dma_alignment = DMA_ALIGNMENT;
1549ca632f55SGrant Likely 	master->cleanup = cleanup;
1550ca632f55SGrant Likely 	master->setup = setup;
15517f86bde9SMika Westerberg 	master->transfer_one_message = pxa2xx_spi_transfer_one_message;
15527d94a505SMika Westerberg 	master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
15530c27d9cfSMika Westerberg 	master->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
15547dd62787SMark Brown 	master->auto_runtime_pm = true;
15558c3ad488SJarkko Nikula 	master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
1556ca632f55SGrant Likely 
1557ca632f55SGrant Likely 	drv_data->ssp_type = ssp->type;
1558ca632f55SGrant Likely 
1559ca632f55SGrant Likely 	drv_data->ioaddr = ssp->mmio_base;
1560ca632f55SGrant Likely 	drv_data->ssdr_physical = ssp->phys_base + SSDR;
1561ca632f55SGrant Likely 	if (pxa25x_ssp_comp(drv_data)) {
1562e5262d05SWeike Chen 		switch (drv_data->ssp_type) {
1563e5262d05SWeike Chen 		case QUARK_X1000_SSP:
1564e5262d05SWeike Chen 			master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1565e5262d05SWeike Chen 			break;
1566e5262d05SWeike Chen 		default:
156724778be2SStephen Warren 			master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1568e5262d05SWeike Chen 			break;
1569e5262d05SWeike Chen 		}
1570e5262d05SWeike Chen 
1571ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1572ca632f55SGrant Likely 		drv_data->dma_cr1 = 0;
1573ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR;
1574ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1575ca632f55SGrant Likely 	} else {
157624778be2SStephen Warren 		master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1577ca632f55SGrant Likely 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
15785928808eSMika Westerberg 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1579ca632f55SGrant Likely 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1580ca632f55SGrant Likely 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1581ca632f55SGrant Likely 	}
1582ca632f55SGrant Likely 
1583ca632f55SGrant Likely 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1584ca632f55SGrant Likely 			drv_data);
1585ca632f55SGrant Likely 	if (status < 0) {
1586ca632f55SGrant Likely 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
1587ca632f55SGrant Likely 		goto out_error_master_alloc;
1588ca632f55SGrant Likely 	}
1589ca632f55SGrant Likely 
1590ca632f55SGrant Likely 	/* Setup DMA if requested */
1591ca632f55SGrant Likely 	if (platform_info->enable_dma) {
1592cd7bed00SMika Westerberg 		status = pxa2xx_spi_dma_setup(drv_data);
1593cd7bed00SMika Westerberg 		if (status) {
1594cddb339bSMika Westerberg 			dev_dbg(dev, "no DMA channels available, using PIO\n");
1595cd7bed00SMika Westerberg 			platform_info->enable_dma = false;
1596ca632f55SGrant Likely 		}
1597ca632f55SGrant Likely 	}
1598ca632f55SGrant Likely 
1599ca632f55SGrant Likely 	/* Enable SOC clock */
16003343b7a6SMika Westerberg 	clk_prepare_enable(ssp->clk);
16013343b7a6SMika Westerberg 
16020eca7cf2SJarkko Nikula 	master->max_speed_hz = clk_get_rate(ssp->clk);
1603ca632f55SGrant Likely 
1604ca632f55SGrant Likely 	/* Load default SSP configuration */
1605c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
1606e5262d05SWeike Chen 	switch (drv_data->ssp_type) {
1607e5262d05SWeike Chen 	case QUARK_X1000_SSP:
1608c039dd27SJarkko Nikula 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT)
1609c039dd27SJarkko Nikula 		      | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1610c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1611e5262d05SWeike Chen 
1612e5262d05SWeike Chen 		/* using the Motorola SPI protocol and use 8 bit frame */
1613c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0,
1614c039dd27SJarkko Nikula 				 QUARK_X1000_SSCR0_Motorola
1615c039dd27SJarkko Nikula 				 | QUARK_X1000_SSCR0_DataSize(8));
1616e5262d05SWeike Chen 		break;
1617e5262d05SWeike Chen 	default:
1618c039dd27SJarkko Nikula 		tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1619c039dd27SJarkko Nikula 		      SSCR1_TxTresh(TX_THRESH_DFLT);
1620c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1621c039dd27SJarkko Nikula 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1622c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1623e5262d05SWeike Chen 		break;
1624e5262d05SWeike Chen 	}
1625e5262d05SWeike Chen 
1626ca632f55SGrant Likely 	if (!pxa25x_ssp_comp(drv_data))
1627c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSTO, 0);
1628e5262d05SWeike Chen 
1629e5262d05SWeike Chen 	if (!is_quark_x1000_ssp(drv_data))
1630c039dd27SJarkko Nikula 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1631ca632f55SGrant Likely 
16328b136baaSJarkko Nikula 	if (is_lpss_ssp(drv_data)) {
16338b136baaSJarkko Nikula 		lpss_ssp_setup(drv_data);
16348b136baaSJarkko Nikula 		config = lpss_get_config(drv_data);
16358b136baaSJarkko Nikula 		if (config->reg_capabilities >= 0) {
16368b136baaSJarkko Nikula 			tmp = __lpss_ssp_read_priv(drv_data,
16378b136baaSJarkko Nikula 						   config->reg_capabilities);
16388b136baaSJarkko Nikula 			tmp &= LPSS_CAPS_CS_EN_MASK;
16398b136baaSJarkko Nikula 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
16408b136baaSJarkko Nikula 			platform_info->num_chipselect = ffz(tmp);
164130f3a6abSMika Westerberg 		} else if (config->cs_num) {
164230f3a6abSMika Westerberg 			platform_info->num_chipselect = config->cs_num;
16438b136baaSJarkko Nikula 		}
16448b136baaSJarkko Nikula 	}
16458b136baaSJarkko Nikula 	master->num_chipselect = platform_info->num_chipselect;
16468b136baaSJarkko Nikula 
16477f86bde9SMika Westerberg 	tasklet_init(&drv_data->pump_transfers, pump_transfers,
16487f86bde9SMika Westerberg 		     (unsigned long)drv_data);
1649ca632f55SGrant Likely 
1650836d1a22SAntonio Ospite 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1651836d1a22SAntonio Ospite 	pm_runtime_use_autosuspend(&pdev->dev);
1652836d1a22SAntonio Ospite 	pm_runtime_set_active(&pdev->dev);
1653836d1a22SAntonio Ospite 	pm_runtime_enable(&pdev->dev);
1654836d1a22SAntonio Ospite 
1655ca632f55SGrant Likely 	/* Register with the SPI framework */
1656ca632f55SGrant Likely 	platform_set_drvdata(pdev, drv_data);
1657a807fcd0SJingoo Han 	status = devm_spi_register_master(&pdev->dev, master);
1658ca632f55SGrant Likely 	if (status != 0) {
1659ca632f55SGrant Likely 		dev_err(&pdev->dev, "problem registering spi master\n");
16607f86bde9SMika Westerberg 		goto out_error_clock_enabled;
1661ca632f55SGrant Likely 	}
1662ca632f55SGrant Likely 
1663ca632f55SGrant Likely 	return status;
1664ca632f55SGrant Likely 
1665ca632f55SGrant Likely out_error_clock_enabled:
16663343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1667cd7bed00SMika Westerberg 	pxa2xx_spi_dma_release(drv_data);
1668ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1669ca632f55SGrant Likely 
1670ca632f55SGrant Likely out_error_master_alloc:
1671ca632f55SGrant Likely 	spi_master_put(master);
1672ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1673ca632f55SGrant Likely 	return status;
1674ca632f55SGrant Likely }
1675ca632f55SGrant Likely 
1676ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev)
1677ca632f55SGrant Likely {
1678ca632f55SGrant Likely 	struct driver_data *drv_data = platform_get_drvdata(pdev);
1679ca632f55SGrant Likely 	struct ssp_device *ssp;
1680ca632f55SGrant Likely 
1681ca632f55SGrant Likely 	if (!drv_data)
1682ca632f55SGrant Likely 		return 0;
1683ca632f55SGrant Likely 	ssp = drv_data->ssp;
1684ca632f55SGrant Likely 
16857d94a505SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
16867d94a505SMika Westerberg 
1687ca632f55SGrant Likely 	/* Disable the SSP at the peripheral and SOC level */
1688c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
16893343b7a6SMika Westerberg 	clk_disable_unprepare(ssp->clk);
1690ca632f55SGrant Likely 
1691ca632f55SGrant Likely 	/* Release DMA */
1692cd7bed00SMika Westerberg 	if (drv_data->master_info->enable_dma)
1693cd7bed00SMika Westerberg 		pxa2xx_spi_dma_release(drv_data);
1694ca632f55SGrant Likely 
16957d94a505SMika Westerberg 	pm_runtime_put_noidle(&pdev->dev);
16967d94a505SMika Westerberg 	pm_runtime_disable(&pdev->dev);
16977d94a505SMika Westerberg 
1698ca632f55SGrant Likely 	/* Release IRQ */
1699ca632f55SGrant Likely 	free_irq(ssp->irq, drv_data);
1700ca632f55SGrant Likely 
1701ca632f55SGrant Likely 	/* Release SSP */
1702ca632f55SGrant Likely 	pxa_ssp_free(ssp);
1703ca632f55SGrant Likely 
1704ca632f55SGrant Likely 	return 0;
1705ca632f55SGrant Likely }
1706ca632f55SGrant Likely 
1707ca632f55SGrant Likely static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1708ca632f55SGrant Likely {
1709ca632f55SGrant Likely 	int status = 0;
1710ca632f55SGrant Likely 
1711ca632f55SGrant Likely 	if ((status = pxa2xx_spi_remove(pdev)) != 0)
1712ca632f55SGrant Likely 		dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1713ca632f55SGrant Likely }
1714ca632f55SGrant Likely 
1715382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP
1716ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev)
1717ca632f55SGrant Likely {
1718ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1719ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1720ca632f55SGrant Likely 	int status = 0;
1721ca632f55SGrant Likely 
17227f86bde9SMika Westerberg 	status = spi_master_suspend(drv_data->master);
1723ca632f55SGrant Likely 	if (status != 0)
1724ca632f55SGrant Likely 		return status;
1725c039dd27SJarkko Nikula 	pxa2xx_spi_write(drv_data, SSCR0, 0);
17262b9375b9SDmitry Eremin-Solenikov 
17272b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
17283343b7a6SMika Westerberg 		clk_disable_unprepare(ssp->clk);
1729ca632f55SGrant Likely 
1730ca632f55SGrant Likely 	return 0;
1731ca632f55SGrant Likely }
1732ca632f55SGrant Likely 
1733ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev)
1734ca632f55SGrant Likely {
1735ca632f55SGrant Likely 	struct driver_data *drv_data = dev_get_drvdata(dev);
1736ca632f55SGrant Likely 	struct ssp_device *ssp = drv_data->ssp;
1737ca632f55SGrant Likely 	int status = 0;
1738ca632f55SGrant Likely 
1739ca632f55SGrant Likely 	/* Enable the SSP clock */
17402b9375b9SDmitry Eremin-Solenikov 	if (!pm_runtime_suspended(dev))
17413343b7a6SMika Westerberg 		clk_prepare_enable(ssp->clk);
1742ca632f55SGrant Likely 
1743c50325f7SChew, Chiau Ee 	/* Restore LPSS private register bits */
174448421adfSJarkko Nikula 	if (is_lpss_ssp(drv_data))
1745c50325f7SChew, Chiau Ee 		lpss_ssp_setup(drv_data);
1746c50325f7SChew, Chiau Ee 
1747ca632f55SGrant Likely 	/* Start the queue running */
17487f86bde9SMika Westerberg 	status = spi_master_resume(drv_data->master);
1749ca632f55SGrant Likely 	if (status != 0) {
1750ca632f55SGrant Likely 		dev_err(dev, "problem starting queue (%d)\n", status);
1751ca632f55SGrant Likely 		return status;
1752ca632f55SGrant Likely 	}
1753ca632f55SGrant Likely 
1754ca632f55SGrant Likely 	return 0;
1755ca632f55SGrant Likely }
17567d94a505SMika Westerberg #endif
17577d94a505SMika Westerberg 
1758ec833050SRafael J. Wysocki #ifdef CONFIG_PM
17597d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev)
17607d94a505SMika Westerberg {
17617d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
17627d94a505SMika Westerberg 
17637d94a505SMika Westerberg 	clk_disable_unprepare(drv_data->ssp->clk);
17647d94a505SMika Westerberg 	return 0;
17657d94a505SMika Westerberg }
17667d94a505SMika Westerberg 
17677d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev)
17687d94a505SMika Westerberg {
17697d94a505SMika Westerberg 	struct driver_data *drv_data = dev_get_drvdata(dev);
17707d94a505SMika Westerberg 
17717d94a505SMika Westerberg 	clk_prepare_enable(drv_data->ssp->clk);
17727d94a505SMika Westerberg 	return 0;
17737d94a505SMika Westerberg }
17747d94a505SMika Westerberg #endif
1775ca632f55SGrant Likely 
1776ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
17777d94a505SMika Westerberg 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
17787d94a505SMika Westerberg 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
17797d94a505SMika Westerberg 			   pxa2xx_spi_runtime_resume, NULL)
1780ca632f55SGrant Likely };
1781ca632f55SGrant Likely 
1782ca632f55SGrant Likely static struct platform_driver driver = {
1783ca632f55SGrant Likely 	.driver = {
1784ca632f55SGrant Likely 		.name	= "pxa2xx-spi",
1785ca632f55SGrant Likely 		.pm	= &pxa2xx_spi_pm_ops,
1786a3496855SMika Westerberg 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
1787ca632f55SGrant Likely 	},
1788ca632f55SGrant Likely 	.probe = pxa2xx_spi_probe,
1789ca632f55SGrant Likely 	.remove = pxa2xx_spi_remove,
1790ca632f55SGrant Likely 	.shutdown = pxa2xx_spi_shutdown,
1791ca632f55SGrant Likely };
1792ca632f55SGrant Likely 
1793ca632f55SGrant Likely static int __init pxa2xx_spi_init(void)
1794ca632f55SGrant Likely {
1795ca632f55SGrant Likely 	return platform_driver_register(&driver);
1796ca632f55SGrant Likely }
1797ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init);
1798ca632f55SGrant Likely 
1799ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void)
1800ca632f55SGrant Likely {
1801ca632f55SGrant Likely 	platform_driver_unregister(&driver);
1802ca632f55SGrant Likely }
1803ca632f55SGrant Likely module_exit(pxa2xx_spi_exit);
1804