blob: 814268405ab0b86b003ab5909811ae2adcabb6cb [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Stephen Streete0c99052006-03-07 23:53:24 -08002/*
3 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
Mika Westerberga0d26422013-01-22 12:26:32 +02004 * Copyright (C) 2013, Intel Corporation
Stephen Streete0c99052006-03-07 23:53:24 -08005 */
6
Andy Shevchenko5ce25702019-10-18 13:54:26 +03007#include <linux/acpi.h>
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02008#include <linux/bitops.h>
Andy Shevchenko5ce25702019-10-18 13:54:26 +03009#include <linux/clk.h>
10#include <linux/delay.h>
Stephen Streete0c99052006-03-07 23:53:24 -080011#include <linux/device.h>
Sachin Kamatcbfd6a22013-04-08 15:49:33 +053012#include <linux/err.h>
Andy Shevchenko5ce25702019-10-18 13:54:26 +030013#include <linux/errno.h>
14#include <linux/gpio/consumer.h>
15#include <linux/gpio.h>
16#include <linux/init.h>
Stephen Streete0c99052006-03-07 23:53:24 -080017#include <linux/interrupt.h>
Andy Shevchenko5ce25702019-10-18 13:54:26 +030018#include <linux/ioport.h>
Andy Shevchenko9df461e2015-03-25 15:06:16 +020019#include <linux/kernel.h>
Andy Shevchenko5ce25702019-10-18 13:54:26 +030020#include <linux/module.h>
Andy Shevchenkoae8fbf12019-10-18 13:54:29 +030021#include <linux/mod_devicetable.h>
22#include <linux/of.h>
Jarkko Nikula34cadd92015-07-30 16:30:07 +030023#include <linux/pci.h>
Stephen Streete0c99052006-03-07 23:53:24 -080024#include <linux/platform_device.h>
Andy Shevchenko5ce25702019-10-18 13:54:26 +030025#include <linux/pm_runtime.h>
Andy Shevchenkof2faa3e2019-10-18 13:54:28 +030026#include <linux/property.h>
Andy Shevchenko5ce25702019-10-18 13:54:26 +030027#include <linux/slab.h>
Sebastian Andrzej Siewior8348c252010-11-22 17:12:15 -080028#include <linux/spi/pxa2xx_spi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080029#include <linux/spi/spi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080030
Mika Westerbergcd7bed02013-01-22 12:26:28 +020031#include "spi-pxa2xx.h"
Stephen Streete0c99052006-03-07 23:53:24 -080032
33MODULE_AUTHOR("Stephen Street");
Will Newton037cdaf2007-12-10 15:49:25 -080034MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
Stephen Streete0c99052006-03-07 23:53:24 -080035MODULE_LICENSE("GPL");
Kay Sievers7e38c3c2008-04-10 21:29:20 -070036MODULE_ALIAS("platform:pxa2xx-spi");
Stephen Streete0c99052006-03-07 23:53:24 -080037
Vernon Sauderf1f640a2008-10-15 22:02:43 -070038#define TIMOUT_DFLT 1000
39
Ned Forresterb97c74b2008-02-23 15:23:40 -080040/*
41 * for testing SSCR1 changes that require SSP restart, basically
42 * everything except the service and interrupt enables, the pxa270 developer
43 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
44 * list, but the PXA255 dev man says all bits without really meaning the
45 * service and interrupt enables
46 */
47#define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
Stephen Street8d94cc52006-12-10 02:18:54 -080048 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
Ned Forresterb97c74b2008-02-23 15:23:40 -080049 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
50 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
51 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
52 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
Stephen Street8d94cc52006-12-10 02:18:54 -080053
Weike Chene5262d02014-11-26 02:35:10 -080054#define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
55 | QUARK_X1000_SSCR1_EFWR \
56 | QUARK_X1000_SSCR1_RFT \
57 | QUARK_X1000_SSCR1_TFT \
58 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
59
Andy Shevchenko7c7289a2016-09-07 15:43:22 +030060#define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
61 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
62 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
63 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
64 | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
65 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
66
Jarkko Nikula624ea722015-10-28 15:13:39 +020067#define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
68#define LPSS_CS_CONTROL_SW_MODE BIT(0)
69#define LPSS_CS_CONTROL_CS_HIGH BIT(1)
Jarkko Nikula8b136ba2015-10-28 15:13:41 +020070#define LPSS_CAPS_CS_EN_SHIFT 9
71#define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
Mika Westerberga0d26422013-01-22 12:26:32 +020072
Evan Green683f65d2020-02-11 14:37:00 -080073#define LPSS_PRIV_CLOCK_GATE 0x38
74#define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
75#define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
76
Jarkko Nikuladccf7362015-06-04 16:55:11 +030077struct lpss_config {
78 /* LPSS offset from drv_data->ioaddr */
79 unsigned offset;
80 /* Register offsets from drv_data->lpss_base or -1 */
81 int reg_general;
82 int reg_ssp;
83 int reg_cs_ctrl;
Jarkko Nikula8b136ba2015-10-28 15:13:41 +020084 int reg_capabilities;
Jarkko Nikuladccf7362015-06-04 16:55:11 +030085 /* FIFO thresholds */
86 u32 rx_threshold;
87 u32 tx_threshold_lo;
88 u32 tx_threshold_hi;
Mika Westerbergc1e4a532016-02-08 17:14:30 +020089 /* Chip select control */
90 unsigned cs_sel_shift;
91 unsigned cs_sel_mask;
Mika Westerberg30f3a6a2016-02-08 17:14:31 +020092 unsigned cs_num;
Evan Green683f65d2020-02-11 14:37:00 -080093 /* Quirks */
94 unsigned cs_clk_stays_gated : 1;
Jarkko Nikuladccf7362015-06-04 16:55:11 +030095};
96
97/* Keep these sorted with enum pxa_ssp_type */
98static const struct lpss_config lpss_platforms[] = {
99 { /* LPSS_LPT_SSP */
100 .offset = 0x800,
101 .reg_general = 0x08,
102 .reg_ssp = 0x0c,
103 .reg_cs_ctrl = 0x18,
Jarkko Nikula8b136ba2015-10-28 15:13:41 +0200104 .reg_capabilities = -1,
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300105 .rx_threshold = 64,
106 .tx_threshold_lo = 160,
107 .tx_threshold_hi = 224,
108 },
109 { /* LPSS_BYT_SSP */
110 .offset = 0x400,
111 .reg_general = 0x08,
112 .reg_ssp = 0x0c,
113 .reg_cs_ctrl = 0x18,
Jarkko Nikula8b136ba2015-10-28 15:13:41 +0200114 .reg_capabilities = -1,
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300115 .rx_threshold = 64,
116 .tx_threshold_lo = 160,
117 .tx_threshold_hi = 224,
118 },
Mika Westerberg30f3a6a2016-02-08 17:14:31 +0200119 { /* LPSS_BSW_SSP */
120 .offset = 0x400,
121 .reg_general = 0x08,
122 .reg_ssp = 0x0c,
123 .reg_cs_ctrl = 0x18,
124 .reg_capabilities = -1,
125 .rx_threshold = 64,
126 .tx_threshold_lo = 160,
127 .tx_threshold_hi = 224,
128 .cs_sel_shift = 2,
129 .cs_sel_mask = 1 << 2,
130 .cs_num = 2,
131 },
Jarkko Nikula34cadd92015-07-30 16:30:07 +0300132 { /* LPSS_SPT_SSP */
133 .offset = 0x200,
134 .reg_general = -1,
135 .reg_ssp = 0x20,
136 .reg_cs_ctrl = 0x24,
Jarkko Nikula66ec2462016-04-26 10:08:26 +0300137 .reg_capabilities = -1,
Jarkko Nikula34cadd92015-07-30 16:30:07 +0300138 .rx_threshold = 1,
139 .tx_threshold_lo = 32,
140 .tx_threshold_hi = 56,
141 },
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +0200142 { /* LPSS_BXT_SSP */
143 .offset = 0x200,
144 .reg_general = -1,
145 .reg_ssp = 0x20,
146 .reg_cs_ctrl = 0x24,
147 .reg_capabilities = 0xfc,
148 .rx_threshold = 1,
149 .tx_threshold_lo = 16,
150 .tx_threshold_hi = 48,
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200151 .cs_sel_shift = 8,
152 .cs_sel_mask = 3 << 8,
Evan Green6eefaee2020-04-27 16:32:48 -0700153 .cs_clk_stays_gated = true,
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +0200154 },
Jarkko Nikulafc0b2ac2017-05-30 17:31:21 +0300155 { /* LPSS_CNL_SSP */
156 .offset = 0x200,
157 .reg_general = -1,
158 .reg_ssp = 0x20,
159 .reg_cs_ctrl = 0x24,
160 .reg_capabilities = 0xfc,
161 .rx_threshold = 1,
162 .tx_threshold_lo = 32,
163 .tx_threshold_hi = 56,
164 .cs_sel_shift = 8,
165 .cs_sel_mask = 3 << 8,
Evan Green683f65d2020-02-11 14:37:00 -0800166 .cs_clk_stays_gated = true,
Jarkko Nikulafc0b2ac2017-05-30 17:31:21 +0300167 },
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300168};
169
170static inline const struct lpss_config
171*lpss_get_config(const struct driver_data *drv_data)
172{
173 return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
174}
175
Mika Westerberga0d26422013-01-22 12:26:32 +0200176static bool is_lpss_ssp(const struct driver_data *drv_data)
177{
Jarkko Nikula03fbf482015-06-04 16:55:10 +0300178 switch (drv_data->ssp_type) {
179 case LPSS_LPT_SSP:
180 case LPSS_BYT_SSP:
Mika Westerberg30f3a6a2016-02-08 17:14:31 +0200181 case LPSS_BSW_SSP:
Jarkko Nikula34cadd92015-07-30 16:30:07 +0300182 case LPSS_SPT_SSP:
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +0200183 case LPSS_BXT_SSP:
Jarkko Nikulafc0b2ac2017-05-30 17:31:21 +0300184 case LPSS_CNL_SSP:
Jarkko Nikula03fbf482015-06-04 16:55:10 +0300185 return true;
186 default:
187 return false;
188 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200189}
190
Weike Chene5262d02014-11-26 02:35:10 -0800191static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
192{
193 return drv_data->ssp_type == QUARK_X1000_SSP;
194}
195
Andy Shevchenko41c98842020-02-27 18:25:56 +0200196static bool is_mmp2_ssp(const struct driver_data *drv_data)
197{
198 return drv_data->ssp_type == MMP2_SSP;
199}
200
Weike Chen4fdb2422014-10-08 08:50:22 -0700201static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
202{
203 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800204 case QUARK_X1000_SSP:
205 return QUARK_X1000_SSCR1_CHANGE_MASK;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300206 case CE4100_SSP:
207 return CE4100_SSCR1_CHANGE_MASK;
Weike Chen4fdb2422014-10-08 08:50:22 -0700208 default:
209 return SSCR1_CHANGE_MASK;
210 }
211}
212
213static u32
214pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
215{
216 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800217 case QUARK_X1000_SSP:
218 return RX_THRESH_QUARK_X1000_DFLT;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300219 case CE4100_SSP:
220 return RX_THRESH_CE4100_DFLT;
Weike Chen4fdb2422014-10-08 08:50:22 -0700221 default:
222 return RX_THRESH_DFLT;
223 }
224}
225
226static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
227{
Weike Chen4fdb2422014-10-08 08:50:22 -0700228 u32 mask;
229
230 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800231 case QUARK_X1000_SSP:
232 mask = QUARK_X1000_SSSR_TFL_MASK;
233 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300234 case CE4100_SSP:
235 mask = CE4100_SSSR_TFL_MASK;
236 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700237 default:
238 mask = SSSR_TFL_MASK;
239 break;
240 }
241
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200242 return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
Weike Chen4fdb2422014-10-08 08:50:22 -0700243}
244
245static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
246 u32 *sccr1_reg)
247{
248 u32 mask;
249
250 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800251 case QUARK_X1000_SSP:
252 mask = QUARK_X1000_SSCR1_RFT;
253 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300254 case CE4100_SSP:
255 mask = CE4100_SSCR1_RFT;
256 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700257 default:
258 mask = SSCR1_RFT;
259 break;
260 }
261 *sccr1_reg &= ~mask;
262}
263
264static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
265 u32 *sccr1_reg, u32 threshold)
266{
267 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800268 case QUARK_X1000_SSP:
269 *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
270 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300271 case CE4100_SSP:
272 *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
273 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700274 default:
275 *sccr1_reg |= SSCR1_RxTresh(threshold);
276 break;
277 }
278}
279
280static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
281 u32 clk_div, u8 bits)
282{
283 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800284 case QUARK_X1000_SSP:
285 return clk_div
286 | QUARK_X1000_SSCR0_Motorola
287 | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
288 | SSCR0_SSE;
Weike Chen4fdb2422014-10-08 08:50:22 -0700289 default:
290 return clk_div
291 | SSCR0_Motorola
292 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
293 | SSCR0_SSE
294 | (bits > 16 ? SSCR0_EDSS : 0);
295 }
296}
297
Mika Westerberga0d26422013-01-22 12:26:32 +0200298/*
299 * Read and write LPSS SSP private registers. Caller must first check that
300 * is_lpss_ssp() returns true before these can be called.
301 */
302static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
303{
304 WARN_ON(!drv_data->lpss_base);
305 return readl(drv_data->lpss_base + offset);
306}
307
308static void __lpss_ssp_write_priv(struct driver_data *drv_data,
309 unsigned offset, u32 value)
310{
311 WARN_ON(!drv_data->lpss_base);
312 writel(value, drv_data->lpss_base + offset);
313}
314
315/*
316 * lpss_ssp_setup - perform LPSS SSP specific setup
317 * @drv_data: pointer to the driver private data
318 *
319 * Perform LPSS SSP specific setup. This function must be called first if
320 * one is going to use LPSS SSP private registers.
321 */
322static void lpss_ssp_setup(struct driver_data *drv_data)
323{
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300324 const struct lpss_config *config;
325 u32 value;
Mika Westerberga0d26422013-01-22 12:26:32 +0200326
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300327 config = lpss_get_config(drv_data);
328 drv_data->lpss_base = drv_data->ioaddr + config->offset;
Mika Westerberga0d26422013-01-22 12:26:32 +0200329
330 /* Enable software chip select control */
Jarkko Nikula0e897212015-10-22 16:44:42 +0300331 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
Jarkko Nikula624ea722015-10-28 15:13:39 +0200332 value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
333 value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300334 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
Mika Westerberg0054e282013-03-05 12:05:17 +0200335
336 /* Enable multiblock DMA transfers */
Lubomir Rintel51eea522019-01-16 16:13:31 +0100337 if (drv_data->controller_info->enable_dma) {
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300338 __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
Mika Westerberg1de70612013-07-03 13:25:06 +0300339
Jarkko Nikula82ba2c22015-06-04 16:55:12 +0300340 if (config->reg_general >= 0) {
341 value = __lpss_ssp_read_priv(drv_data,
342 config->reg_general);
Jarkko Nikula624ea722015-10-28 15:13:39 +0200343 value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
Jarkko Nikula82ba2c22015-06-04 16:55:12 +0300344 __lpss_ssp_write_priv(drv_data,
345 config->reg_general, value);
346 }
Mika Westerberg1de70612013-07-03 13:25:06 +0300347 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200348}
349
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300350static void lpss_ssp_select_cs(struct spi_device *spi,
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200351 const struct lpss_config *config)
352{
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300353 struct driver_data *drv_data =
354 spi_controller_get_devdata(spi->controller);
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200355 u32 value, cs;
356
357 if (!config->cs_sel_mask)
358 return;
359
360 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
361
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300362 cs = spi->chip_select;
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200363 cs <<= config->cs_sel_shift;
364 if (cs != (value & config->cs_sel_mask)) {
365 /*
366 * When switching another chip select output active the
367 * output must be selected first and wait 2 ssp_clk cycles
368 * before changing state to active. Otherwise a short
369 * glitch will occur on the previous chip select since
370 * output select is latched but state control is not.
371 */
372 value &= ~config->cs_sel_mask;
373 value |= cs;
374 __lpss_ssp_write_priv(drv_data,
375 config->reg_cs_ctrl, value);
376 ndelay(1000000000 /
Lubomir Rintel51eea522019-01-16 16:13:31 +0100377 (drv_data->controller->max_speed_hz / 2));
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200378 }
379}
380
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300381static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
Mika Westerberga0d26422013-01-22 12:26:32 +0200382{
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300383 struct driver_data *drv_data =
384 spi_controller_get_devdata(spi->controller);
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300385 const struct lpss_config *config;
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200386 u32 value;
Mika Westerberga0d26422013-01-22 12:26:32 +0200387
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300388 config = lpss_get_config(drv_data);
389
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200390 if (enable)
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300391 lpss_ssp_select_cs(spi, config);
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200392
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300393 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200394 if (enable)
Jarkko Nikula624ea722015-10-28 15:13:39 +0200395 value &= ~LPSS_CS_CONTROL_CS_HIGH;
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200396 else
Jarkko Nikula624ea722015-10-28 15:13:39 +0200397 value |= LPSS_CS_CONTROL_CS_HIGH;
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300398 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
Evan Green683f65d2020-02-11 14:37:00 -0800399 if (config->cs_clk_stays_gated) {
400 u32 clkgate;
401
402 /*
403 * Changing CS alone when dynamic clock gating is on won't
404 * actually flip CS at that time. This ruins SPI transfers
405 * that specify delays, or have no data. Toggle the clock mode
406 * to force on briefly to poke the CS pin to move.
407 */
408 clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
409 value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
410 LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
411
412 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
413 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
414 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200415}
416
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300417static void cs_assert(struct spi_device *spi)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700418{
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300419 struct chip_data *chip = spi_get_ctldata(spi);
420 struct driver_data *drv_data =
421 spi_controller_get_devdata(spi->controller);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700422
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800423 if (drv_data->ssp_type == CE4100_SSP) {
Jarkko Nikula96579a42016-09-07 17:04:07 +0300424 pxa2xx_spi_write(drv_data, SSSR, chip->frm);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800425 return;
426 }
427
Eric Miaoa7bb3902009-04-06 19:00:54 -0700428 if (chip->cs_control) {
429 chip->cs_control(PXA2XX_CS_ASSERT);
430 return;
431 }
432
Jan Kiszkac18d9252017-08-03 13:40:32 +0200433 if (chip->gpiod_cs) {
434 gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200435 return;
436 }
437
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200438 if (is_lpss_ssp(drv_data))
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300439 lpss_ssp_cs_control(spi, true);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700440}
441
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300442static void cs_deassert(struct spi_device *spi)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700443{
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300444 struct chip_data *chip = spi_get_ctldata(spi);
445 struct driver_data *drv_data =
446 spi_controller_get_devdata(spi->controller);
Jarkko Nikula104e51a2018-02-09 16:31:07 +0200447 unsigned long timeout;
Eric Miaoa7bb3902009-04-06 19:00:54 -0700448
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800449 if (drv_data->ssp_type == CE4100_SSP)
450 return;
451
Jarkko Nikula104e51a2018-02-09 16:31:07 +0200452 /* Wait until SSP becomes idle before deasserting the CS */
453 timeout = jiffies + msecs_to_jiffies(10);
454 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
455 !time_after(jiffies, timeout))
456 cpu_relax();
457
Eric Miaoa7bb3902009-04-06 19:00:54 -0700458 if (chip->cs_control) {
Daniel Ribeiro2b2562d2009-04-08 22:48:03 -0300459 chip->cs_control(PXA2XX_CS_DEASSERT);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700460 return;
461 }
462
Jan Kiszkac18d9252017-08-03 13:40:32 +0200463 if (chip->gpiod_cs) {
464 gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200465 return;
466 }
467
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200468 if (is_lpss_ssp(drv_data))
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300469 lpss_ssp_cs_control(spi, false);
470}
471
472static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
473{
474 if (level)
475 cs_deassert(spi);
476 else
477 cs_assert(spi);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700478}
479
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200480int pxa2xx_spi_flush(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800481{
482 unsigned long limit = loops_per_jiffy << 1;
483
Stephen Streete0c99052006-03-07 23:53:24 -0800484 do {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200485 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
486 pxa2xx_spi_read(drv_data, SSDR);
487 } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800488 write_SSSR_CS(drv_data, SSSR_ROR);
Stephen Streete0c99052006-03-07 23:53:24 -0800489
490 return limit;
491}
492
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100493static void pxa2xx_spi_off(struct driver_data *drv_data)
494{
Andy Shevchenko41c98842020-02-27 18:25:56 +0200495 /* On MMP, disabling SSE seems to corrupt the Rx FIFO */
496 if (is_mmp2_ssp(drv_data))
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100497 return;
498
499 pxa2xx_spi_write(drv_data, SSCR0,
500 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
501}
502
Stephen Street8d94cc52006-12-10 02:18:54 -0800503static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800504{
Stephen Street9708c122006-03-28 14:05:23 -0800505 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800506
Weike Chen4fdb2422014-10-08 08:50:22 -0700507 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800508 || (drv_data->tx == drv_data->tx_end))
509 return 0;
510
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200511 pxa2xx_spi_write(drv_data, SSDR, 0);
Stephen Street8d94cc52006-12-10 02:18:54 -0800512 drv_data->tx += n_bytes;
513
514 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800515}
516
Stephen Street8d94cc52006-12-10 02:18:54 -0800517static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800518{
Stephen Street9708c122006-03-28 14:05:23 -0800519 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800520
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200521 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
522 && (drv_data->rx < drv_data->rx_end)) {
523 pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800524 drv_data->rx += n_bytes;
525 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800526
527 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800528}
529
Stephen Street8d94cc52006-12-10 02:18:54 -0800530static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800531{
Weike Chen4fdb2422014-10-08 08:50:22 -0700532 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800533 || (drv_data->tx == drv_data->tx_end))
534 return 0;
535
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200536 pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800537 ++drv_data->tx;
538
539 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800540}
541
Stephen Street8d94cc52006-12-10 02:18:54 -0800542static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800543{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200544 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
545 && (drv_data->rx < drv_data->rx_end)) {
546 *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800547 ++drv_data->rx;
548 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800549
550 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800551}
552
Stephen Street8d94cc52006-12-10 02:18:54 -0800553static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800554{
Weike Chen4fdb2422014-10-08 08:50:22 -0700555 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800556 || (drv_data->tx == drv_data->tx_end))
557 return 0;
558
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200559 pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800560 drv_data->tx += 2;
561
562 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800563}
564
Stephen Street8d94cc52006-12-10 02:18:54 -0800565static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800566{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200567 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
568 && (drv_data->rx < drv_data->rx_end)) {
569 *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800570 drv_data->rx += 2;
571 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800572
573 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800574}
Stephen Street8d94cc52006-12-10 02:18:54 -0800575
576static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800577{
Weike Chen4fdb2422014-10-08 08:50:22 -0700578 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800579 || (drv_data->tx == drv_data->tx_end))
580 return 0;
581
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200582 pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800583 drv_data->tx += 4;
584
585 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800586}
587
Stephen Street8d94cc52006-12-10 02:18:54 -0800588static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800589{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200590 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
591 && (drv_data->rx < drv_data->rx_end)) {
592 *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800593 drv_data->rx += 4;
594 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800595
596 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800597}
598
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800599static void reset_sccr1(struct driver_data *drv_data)
600{
Jarkko Nikula96579a42016-09-07 17:04:07 +0300601 struct chip_data *chip =
Lubomir Rintel51eea522019-01-16 16:13:31 +0100602 spi_get_ctldata(drv_data->controller->cur_msg->spi);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800603 u32 sccr1_reg;
604
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200605 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
Andy Shevchenko152bc192016-07-06 12:08:11 +0300606 switch (drv_data->ssp_type) {
607 case QUARK_X1000_SSP:
608 sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
609 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300610 case CE4100_SSP:
611 sccr1_reg &= ~CE4100_SSCR1_RFT;
612 break;
Andy Shevchenko152bc192016-07-06 12:08:11 +0300613 default:
614 sccr1_reg &= ~SSCR1_RFT;
615 break;
616 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800617 sccr1_reg |= chip->threshold;
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200618 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800619}
620
Stephen Street8d94cc52006-12-10 02:18:54 -0800621static void int_error_stop(struct driver_data *drv_data, const char* msg)
622{
Stephen Street8d94cc52006-12-10 02:18:54 -0800623 /* Stop and reset SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800624 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800625 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800626 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200627 pxa2xx_spi_write(drv_data, SSTO, 0);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200628 pxa2xx_spi_flush(drv_data);
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100629 pxa2xx_spi_off(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800630
631 dev_err(&drv_data->pdev->dev, "%s\n", msg);
632
Lubomir Rintel51eea522019-01-16 16:13:31 +0100633 drv_data->controller->cur_msg->status = -EIO;
634 spi_finalize_current_transfer(drv_data->controller);
Stephen Street8d94cc52006-12-10 02:18:54 -0800635}
636
637static void int_transfer_complete(struct driver_data *drv_data)
638{
Jarkko Nikula07550df2016-02-04 12:30:56 +0200639 /* Clear and disable interrupts */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800640 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800641 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800642 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200643 pxa2xx_spi_write(drv_data, SSTO, 0);
Stephen Street8d94cc52006-12-10 02:18:54 -0800644
Lubomir Rintel51eea522019-01-16 16:13:31 +0100645 spi_finalize_current_transfer(drv_data->controller);
Stephen Street8d94cc52006-12-10 02:18:54 -0800646}
647
Stephen Streete0c99052006-03-07 23:53:24 -0800648static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
649{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200650 u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
651 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
Stephen Street8d94cc52006-12-10 02:18:54 -0800652
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200653 u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
Stephen Streete0c99052006-03-07 23:53:24 -0800654
Stephen Street8d94cc52006-12-10 02:18:54 -0800655 if (irq_status & SSSR_ROR) {
656 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
657 return IRQ_HANDLED;
658 }
Stephen Streete0c99052006-03-07 23:53:24 -0800659
Lubomir Rintelec93cb62018-11-13 11:22:25 +0100660 if (irq_status & SSSR_TUR) {
661 int_error_stop(drv_data, "interrupt_transfer: fifo underrun");
662 return IRQ_HANDLED;
663 }
664
Stephen Street8d94cc52006-12-10 02:18:54 -0800665 if (irq_status & SSSR_TINT) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200666 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
Stephen Street8d94cc52006-12-10 02:18:54 -0800667 if (drv_data->read(drv_data)) {
668 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800669 return IRQ_HANDLED;
670 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800671 }
Stephen Streete0c99052006-03-07 23:53:24 -0800672
Stephen Street8d94cc52006-12-10 02:18:54 -0800673 /* Drain rx fifo, Fill tx fifo and prevent overruns */
674 do {
675 if (drv_data->read(drv_data)) {
676 int_transfer_complete(drv_data);
677 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800678 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800679 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800680
Stephen Street8d94cc52006-12-10 02:18:54 -0800681 if (drv_data->read(drv_data)) {
682 int_transfer_complete(drv_data);
683 return IRQ_HANDLED;
684 }
Stephen Streete0c99052006-03-07 23:53:24 -0800685
Stephen Street8d94cc52006-12-10 02:18:54 -0800686 if (drv_data->tx == drv_data->tx_end) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800687 u32 bytes_left;
688 u32 sccr1_reg;
689
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200690 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800691 sccr1_reg &= ~SSCR1_TIE;
692
693 /*
694 * PXA25x_SSP has no timeout, set up rx threshould for the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300695 * remaining RX bytes.
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800696 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800697 if (pxa25x_ssp_comp(drv_data)) {
Weike Chen4fdb2422014-10-08 08:50:22 -0700698 u32 rx_thre;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800699
Weike Chen4fdb2422014-10-08 08:50:22 -0700700 pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800701
702 bytes_left = drv_data->rx_end - drv_data->rx;
703 switch (drv_data->n_bytes) {
704 case 4:
Gustavo A. R. Silva2c183372018-10-03 17:55:22 +0200705 bytes_left >>= 2;
706 break;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800707 case 2:
708 bytes_left >>= 1;
Gustavo A. R. Silva2c183372018-10-03 17:55:22 +0200709 break;
Stephen Street8d94cc52006-12-10 02:18:54 -0800710 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800711
Weike Chen4fdb2422014-10-08 08:50:22 -0700712 rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
713 if (rx_thre > bytes_left)
714 rx_thre = bytes_left;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800715
Weike Chen4fdb2422014-10-08 08:50:22 -0700716 pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
Stephen Streete0c99052006-03-07 23:53:24 -0800717 }
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200718 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800719 }
720
Stephen Street5daa3ba2006-05-20 15:00:19 -0700721 /* We did something */
722 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800723}
724
Jan Kiszkab0312482017-01-16 19:44:54 +0100725static void handle_bad_msg(struct driver_data *drv_data)
726{
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100727 pxa2xx_spi_off(drv_data);
Jan Kiszkab0312482017-01-16 19:44:54 +0100728 pxa2xx_spi_write(drv_data, SSCR1,
729 pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
730 if (!pxa25x_ssp_comp(drv_data))
731 pxa2xx_spi_write(drv_data, SSTO, 0);
732 write_SSSR_CS(drv_data, drv_data->clear_sr);
733
734 dev_err(&drv_data->pdev->dev,
735 "bad message state in interrupt handler\n");
736}
737
David Howells7d12e782006-10-05 14:55:46 +0100738static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800739{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400740 struct driver_data *drv_data = dev_id;
Mika Westerberg7d94a502013-01-22 12:26:30 +0200741 u32 sccr1_reg;
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800742 u32 mask = drv_data->mask_sr;
743 u32 status;
744
Mika Westerberg7d94a502013-01-22 12:26:30 +0200745 /*
746 * The IRQ might be shared with other peripherals so we must first
747 * check that are we RPM suspended or not. If we are we assume that
748 * the IRQ was not for us (we shouldn't be RPM suspended when the
749 * interrupt is enabled).
750 */
751 if (pm_runtime_suspended(&drv_data->pdev->dev))
752 return IRQ_NONE;
753
Mika Westerberg269e4a42013-09-04 13:37:43 +0300754 /*
755 * If the device is not yet in RPM suspended state and we get an
756 * interrupt that is meant for another device, check if status bits
757 * are all set to one. That means that the device is already
758 * powered off.
759 */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200760 status = pxa2xx_spi_read(drv_data, SSSR);
Mika Westerberg269e4a42013-09-04 13:37:43 +0300761 if (status == ~0)
762 return IRQ_NONE;
763
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200764 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800765
766 /* Ignore possible writes if we don't need to write */
767 if (!(sccr1_reg & SSCR1_TIE))
768 mask &= ~SSSR_TFS;
769
Tan, Jui Nee02bc9332015-09-01 10:22:51 +0800770 /* Ignore RX timeout interrupt if it is disabled */
771 if (!(sccr1_reg & SSCR1_TINTE))
772 mask &= ~SSSR_TINT;
773
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800774 if (!(status & mask))
775 return IRQ_NONE;
Stephen Streete0c99052006-03-07 23:53:24 -0800776
Jan Kiszkae51e9b92017-01-21 10:06:38 +0100777 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
778 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
779
Lubomir Rintel51eea522019-01-16 16:13:31 +0100780 if (!drv_data->controller->cur_msg) {
Jan Kiszkab0312482017-01-16 19:44:54 +0100781 handle_bad_msg(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800782 /* Never fail */
783 return IRQ_HANDLED;
784 }
785
786 return drv_data->transfer_handler(drv_data);
787}
788
Weike Chene5262d02014-11-26 02:35:10 -0800789/*
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200790 * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
791 * input frequency by fractions of 2^24. It also has a divider by 5.
792 *
793 * There are formulas to get baud rate value for given input frequency and
794 * divider parameters, such as DDS_CLK_RATE and SCR:
795 *
796 * Fsys = 200MHz
797 *
798 * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
799 * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
800 *
801 * DDS_CLK_RATE either 2^n or 2^n / 5.
802 * SCR is in range 0 .. 255
803 *
804 * Divisor = 5^i * 2^j * 2 * k
805 * i = [0, 1] i = 1 iff j = 0 or j > 3
806 * j = [0, 23] j = 0 iff i = 1
807 * k = [1, 256]
808 * Special case: j = 0, i = 1: Divisor = 2 / 5
809 *
810 * Accordingly to the specification the recommended values for DDS_CLK_RATE
811 * are:
812 * Case 1: 2^n, n = [0, 23]
813 * Case 2: 2^24 * 2 / 5 (0x666666)
814 * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
815 *
816 * In all cases the lowest possible value is better.
817 *
818 * The function calculates parameters for all cases and chooses the one closest
819 * to the asked baud rate.
Weike Chene5262d02014-11-26 02:35:10 -0800820 */
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200821static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
Weike Chene5262d02014-11-26 02:35:10 -0800822{
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200823 unsigned long xtal = 200000000;
824 unsigned long fref = xtal / 2; /* mandatory division by 2,
825 see (2) */
826 /* case 3 */
827 unsigned long fref1 = fref / 2; /* case 1 */
828 unsigned long fref2 = fref * 2 / 5; /* case 2 */
829 unsigned long scale;
830 unsigned long q, q1, q2;
831 long r, r1, r2;
832 u32 mul;
Weike Chene5262d02014-11-26 02:35:10 -0800833
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200834 /* Case 1 */
835
836 /* Set initial value for DDS_CLK_RATE */
837 mul = (1 << 24) >> 1;
838
839 /* Calculate initial quot */
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300840 q1 = DIV_ROUND_UP(fref1, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200841
842 /* Scale q1 if it's too big */
843 if (q1 > 256) {
844 /* Scale q1 to range [1, 512] */
845 scale = fls_long(q1 - 1);
846 if (scale > 9) {
847 q1 >>= scale - 9;
848 mul >>= scale - 9;
849 }
850
851 /* Round the result if we have a remainder */
852 q1 += q1 & 1;
853 }
854
855 /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
856 scale = __ffs(q1);
857 q1 >>= scale;
858 mul >>= scale;
859
860 /* Get the remainder */
861 r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
862
863 /* Case 2 */
864
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300865 q2 = DIV_ROUND_UP(fref2, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200866 r2 = abs(fref2 / q2 - rate);
867
868 /*
869 * Choose the best between two: less remainder we have the better. We
870 * can't go case 2 if q2 is greater than 256 since SCR register can
871 * hold only values 0 .. 255.
872 */
873 if (r2 >= r1 || q2 > 256) {
874 /* case 1 is better */
875 r = r1;
876 q = q1;
877 } else {
878 /* case 2 is better */
879 r = r2;
880 q = q2;
881 mul = (1 << 24) * 2 / 5;
882 }
883
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300884 /* Check case 3 only if the divisor is big enough */
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200885 if (fref / rate >= 80) {
886 u64 fssp;
887 u32 m;
888
889 /* Calculate initial quot */
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300890 q1 = DIV_ROUND_UP(fref, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200891 m = (1 << 24) / q1;
892
893 /* Get the remainder */
894 fssp = (u64)fref * m;
895 do_div(fssp, 1 << 24);
896 r1 = abs(fssp - rate);
897
898 /* Choose this one if it suits better */
899 if (r1 < r) {
900 /* case 3 is better */
901 q = 1;
902 mul = m;
Weike Chene5262d02014-11-26 02:35:10 -0800903 }
904 }
905
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200906 *dds = mul;
907 return q - 1;
Weike Chene5262d02014-11-26 02:35:10 -0800908}
909
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200910static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
eric miao2f1a74e2007-11-21 18:50:53 +0800911{
Lubomir Rintel51eea522019-01-16 16:13:31 +0100912 unsigned long ssp_clk = drv_data->controller->max_speed_hz;
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200913 const struct ssp_device *ssp = drv_data->ssp;
914
915 rate = min_t(int, ssp_clk, rate);
eric miao2f1a74e2007-11-21 18:50:53 +0800916
Flavio Suligoi29f21332019-04-12 09:32:19 +0200917 /*
918 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
919 * that the SSP transmission rate can be greater than the device rate
920 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800921 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
Flavio Suligoi29f21332019-04-12 09:32:19 +0200922 return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
eric miao2f1a74e2007-11-21 18:50:53 +0800923 else
Flavio Suligoi29f21332019-04-12 09:32:19 +0200924 return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff;
eric miao2f1a74e2007-11-21 18:50:53 +0800925}
926
Weike Chene5262d02014-11-26 02:35:10 -0800927static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
Andy Shevchenkod2c2f6a2015-10-22 16:44:40 +0300928 int rate)
Weike Chene5262d02014-11-26 02:35:10 -0800929{
Jarkko Nikula96579a42016-09-07 17:04:07 +0300930 struct chip_data *chip =
Lubomir Rintel51eea522019-01-16 16:13:31 +0100931 spi_get_ctldata(drv_data->controller->cur_msg->spi);
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200932 unsigned int clk_div;
Weike Chene5262d02014-11-26 02:35:10 -0800933
934 switch (drv_data->ssp_type) {
935 case QUARK_X1000_SSP:
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200936 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300937 break;
Weike Chene5262d02014-11-26 02:35:10 -0800938 default:
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200939 clk_div = ssp_get_clk_div(drv_data, rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300940 break;
Weike Chene5262d02014-11-26 02:35:10 -0800941 }
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200942 return clk_div << 8;
Weike Chene5262d02014-11-26 02:35:10 -0800943}
944
Lubomir Rintel51eea522019-01-16 16:13:31 +0100945static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300946 struct spi_device *spi,
947 struct spi_transfer *xfer)
948{
949 struct chip_data *chip = spi_get_ctldata(spi);
950
951 return chip->enable_dma &&
952 xfer->len <= MAX_DMA_LEN &&
953 xfer->len >= chip->dma_burst_size;
954}
955
Lubomir Rintel51eea522019-01-16 16:13:31 +0100956static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
kbuild test robot71293a62018-04-18 03:53:23 +0800957 struct spi_device *spi,
958 struct spi_transfer *transfer)
Stephen Streete0c99052006-03-07 23:53:24 -0800959{
Lubomir Rintel51eea522019-01-16 16:13:31 +0100960 struct driver_data *drv_data = spi_controller_get_devdata(controller);
961 struct spi_message *message = controller->cur_msg;
Jarkko Nikula20f4c372019-03-29 15:00:45 +0200962 struct chip_data *chip = spi_get_ctldata(spi);
Jarkko Nikula96579a42016-09-07 17:04:07 +0300963 u32 dma_thresh = chip->dma_threshold;
964 u32 dma_burst = chip->dma_burst_size;
965 u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
Jarkko Nikulabffc9672016-09-07 17:04:05 +0300966 u32 clk_div;
967 u8 bits;
968 u32 speed;
Stephen Street9708c122006-03-28 14:05:23 -0800969 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800970 u32 cr1;
Andy Shevchenko7d1f1bf2016-03-24 15:35:42 +0200971 int err;
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300972 int dma_mapped;
Stephen Streete0c99052006-03-07 23:53:24 -0800973
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200974 /* Check if we can DMA this transfer */
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300975 if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
Ned Forrester7e964452008-09-13 02:33:18 -0700976
977 /* reject already-mapped transfers; PIO won't always work */
978 if (message->is_dma_mapped
979 || transfer->rx_dma || transfer->tx_dma) {
Jarkko Nikula748fbad2019-03-29 15:00:46 +0200980 dev_err(&spi->dev,
Jarkko Nikula8ae55af2018-04-17 17:20:01 +0300981 "Mapped transfer length of %u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700982 transfer->len, MAX_DMA_LEN);
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300983 return -EINVAL;
Ned Forrester7e964452008-09-13 02:33:18 -0700984 }
985
986 /* warn ... we force this to PIO mode */
Jarkko Nikula20f4c372019-03-29 15:00:45 +0200987 dev_warn_ratelimited(&spi->dev,
Jarkko Nikula8ae55af2018-04-17 17:20:01 +0300988 "DMA disabled for transfer length %ld greater than %d\n",
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300989 (long)transfer->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800990 }
991
Stephen Streete0c99052006-03-07 23:53:24 -0800992 /* Setup the transfer state based on the type of transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200993 if (pxa2xx_spi_flush(drv_data) == 0) {
Jarkko Nikula748fbad2019-03-29 15:00:46 +0200994 dev_err(&spi->dev, "Flush failed\n");
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300995 return -EIO;
Stephen Streete0c99052006-03-07 23:53:24 -0800996 }
Stephen Street9708c122006-03-28 14:05:23 -0800997 drv_data->n_bytes = chip->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800998 drv_data->tx = (void *)transfer->tx_buf;
999 drv_data->tx_end = drv_data->tx + transfer->len;
1000 drv_data->rx = transfer->rx_buf;
1001 drv_data->rx_end = drv_data->rx + transfer->len;
Stephen Streete0c99052006-03-07 23:53:24 -08001002 drv_data->write = drv_data->tx ? chip->write : null_writer;
1003 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -08001004
1005 /* Change speed and bit per word on a per transfer */
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001006 bits = transfer->bits_per_word;
1007 speed = transfer->speed_hz;
Stephen Street9708c122006-03-28 14:05:23 -08001008
Andy Shevchenkod2c2f6a2015-10-22 16:44:40 +03001009 clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
Stephen Street9708c122006-03-28 14:05:23 -08001010
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001011 if (bits <= 8) {
1012 drv_data->n_bytes = 1;
1013 drv_data->read = drv_data->read != null_reader ?
1014 u8_reader : null_reader;
1015 drv_data->write = drv_data->write != null_writer ?
1016 u8_writer : null_writer;
1017 } else if (bits <= 16) {
1018 drv_data->n_bytes = 2;
1019 drv_data->read = drv_data->read != null_reader ?
1020 u16_reader : null_reader;
1021 drv_data->write = drv_data->write != null_writer ?
1022 u16_writer : null_writer;
1023 } else if (bits <= 32) {
1024 drv_data->n_bytes = 4;
1025 drv_data->read = drv_data->read != null_reader ?
1026 u32_reader : null_reader;
1027 drv_data->write = drv_data->write != null_writer ?
1028 u32_writer : null_writer;
Stephen Street9708c122006-03-28 14:05:23 -08001029 }
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001030 /*
1031 * if bits/word is changed in dma mode, then must check the
1032 * thresholds and burst also
1033 */
1034 if (chip->enable_dma) {
1035 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001036 spi,
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001037 bits, &dma_burst,
1038 &dma_thresh))
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001039 dev_warn_ratelimited(&spi->dev,
Jarkko Nikula8ae55af2018-04-17 17:20:01 +03001040 "DMA burst size reduced to match bits_per_word\n");
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001041 }
1042
Lubomir Rintel51eea522019-01-16 16:13:31 +01001043 dma_mapped = controller->can_dma &&
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001044 controller->can_dma(controller, spi, transfer) &&
Lubomir Rintel51eea522019-01-16 16:13:31 +01001045 controller->cur_msg_mapped;
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001046 if (dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -08001047
1048 /* Ensure we have the correct interrupt handler */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001049 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
Stephen Streete0c99052006-03-07 23:53:24 -08001050
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001051 err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1052 if (err)
1053 return err;
Stephen Streete0c99052006-03-07 23:53:24 -08001054
Stephen Street8d94cc52006-12-10 02:18:54 -08001055 /* Clear status and start DMA engine */
1056 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001057 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001058
1059 pxa2xx_spi_dma_start(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001060 } else {
1061 /* Ensure we have the correct interrupt handler */
1062 drv_data->transfer_handler = interrupt_transfer;
1063
Stephen Street8d94cc52006-12-10 02:18:54 -08001064 /* Clear status */
1065 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001066 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -08001067 }
1068
Jarkko Nikulaee036722016-01-26 15:33:21 +02001069 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1070 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1071 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001072 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
Lubomir Rintel51eea522019-01-16 16:13:31 +01001073 controller->max_speed_hz
Jarkko Nikulaee036722016-01-26 15:33:21 +02001074 / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001075 dma_mapped ? "DMA" : "PIO");
Jarkko Nikulaee036722016-01-26 15:33:21 +02001076 else
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001077 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
Lubomir Rintel51eea522019-01-16 16:13:31 +01001078 controller->max_speed_hz / 2
Jarkko Nikulaee036722016-01-26 15:33:21 +02001079 / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001080 dma_mapped ? "DMA" : "PIO");
Jarkko Nikulaee036722016-01-26 15:33:21 +02001081
Mika Westerberga0d26422013-01-22 12:26:32 +02001082 if (is_lpss_ssp(drv_data)) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001083 if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
1084 != chip->lpss_rx_threshold)
1085 pxa2xx_spi_write(drv_data, SSIRF,
1086 chip->lpss_rx_threshold);
1087 if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
1088 != chip->lpss_tx_threshold)
1089 pxa2xx_spi_write(drv_data, SSITF,
1090 chip->lpss_tx_threshold);
Mika Westerberga0d26422013-01-22 12:26:32 +02001091 }
1092
Weike Chene5262d02014-11-26 02:35:10 -08001093 if (is_quark_x1000_ssp(drv_data) &&
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001094 (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
1095 pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
Weike Chene5262d02014-11-26 02:35:10 -08001096
Stephen Street8d94cc52006-12-10 02:18:54 -08001097 /* see if we need to reload the config registers */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001098 if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1099 || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1100 != (cr1 & change_mask)) {
Ned Forresterb97c74b2008-02-23 15:23:40 -08001101 /* stop the SSP, and update the other bits */
Andy Shevchenko41c98842020-02-27 18:25:56 +02001102 if (!is_mmp2_ssp(drv_data))
Lubomir Rintel29d7e052020-01-18 10:40:31 +01001103 pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001104 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001105 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001106 /* first set CR1 without interrupt and service enables */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001107 pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001108 /* restart the SSP */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001109 pxa2xx_spi_write(drv_data, SSCR0, cr0);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001110
Stephen Street8d94cc52006-12-10 02:18:54 -08001111 } else {
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001112 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001113 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
Stephen Streete0c99052006-03-07 23:53:24 -08001114 }
Ned Forresterb97c74b2008-02-23 15:23:40 -08001115
Andy Shevchenko41c98842020-02-27 18:25:56 +02001116 if (is_mmp2_ssp(drv_data)) {
Lubomir Rintel82391852018-11-13 11:22:28 +01001117 u8 tx_level = (pxa2xx_spi_read(drv_data, SSSR)
1118 & SSSR_TFL_MASK) >> 8;
1119
1120 if (tx_level) {
1121 /* On MMP2, flipping SSE doesn't to empty TXFIFO. */
1122 dev_warn(&spi->dev, "%d bytes of garbage in TXFIFO!\n",
1123 tx_level);
1124 if (tx_level > transfer->len)
1125 tx_level = transfer->len;
1126 drv_data->tx += tx_level;
1127 }
1128 }
1129
Lubomir Rintel51eea522019-01-16 16:13:31 +01001130 if (spi_controller_is_slave(controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001131 while (drv_data->write(drv_data))
1132 ;
Lubomir Rintel77d33892018-11-13 11:22:27 +01001133 if (drv_data->gpiod_ready) {
1134 gpiod_set_value(drv_data->gpiod_ready, 1);
1135 udelay(1);
1136 gpiod_set_value(drv_data->gpiod_ready, 0);
1137 }
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001138 }
1139
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001140 /*
1141 * Release the data by enabling service requests and interrupts,
1142 * without changing any mode bits
1143 */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001144 pxa2xx_spi_write(drv_data, SSCR1, cr1);
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001145
1146 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -08001147}
1148
Lubomir Rintel51eea522019-01-16 16:13:31 +01001149static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001150{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001151 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001152
1153 /* Stop and reset SSP */
1154 write_SSSR_CS(drv_data, drv_data->clear_sr);
1155 reset_sccr1(drv_data);
1156 if (!pxa25x_ssp_comp(drv_data))
1157 pxa2xx_spi_write(drv_data, SSTO, 0);
1158 pxa2xx_spi_flush(drv_data);
Lubomir Rintel29d7e052020-01-18 10:40:31 +01001159 pxa2xx_spi_off(drv_data);
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001160
1161 dev_dbg(&drv_data->pdev->dev, "transfer aborted\n");
1162
Lubomir Rintel51eea522019-01-16 16:13:31 +01001163 drv_data->controller->cur_msg->status = -EINTR;
1164 spi_finalize_current_transfer(drv_data->controller);
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001165
1166 return 0;
1167}
1168
Lubomir Rintel51eea522019-01-16 16:13:31 +01001169static void pxa2xx_spi_handle_err(struct spi_controller *controller,
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001170 struct spi_message *msg)
Stephen Streete0c99052006-03-07 23:53:24 -08001171{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001172 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001173
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001174 /* Disable the SSP */
Lubomir Rintel29d7e052020-01-18 10:40:31 +01001175 pxa2xx_spi_off(drv_data);
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001176 /* Clear and disable interrupts and service requests */
1177 write_SSSR_CS(drv_data, drv_data->clear_sr);
1178 pxa2xx_spi_write(drv_data, SSCR1,
1179 pxa2xx_spi_read(drv_data, SSCR1)
1180 & ~(drv_data->int_cr1 | drv_data->dma_cr1));
1181 if (!pxa25x_ssp_comp(drv_data))
1182 pxa2xx_spi_write(drv_data, SSTO, 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001183
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001184 /*
1185 * Stop the DMA if running. Note DMA callback handler may have unset
1186 * the dma_running already, which is fine as stopping is not needed
1187 * then but we shouldn't rely this flag for anything else than
1188 * stopping. For instance to differentiate between PIO and DMA
1189 * transfers.
1190 */
1191 if (atomic_read(&drv_data->dma_running))
1192 pxa2xx_spi_dma_stop(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001193}
1194
Lubomir Rintel51eea522019-01-16 16:13:31 +01001195static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
Mika Westerberg7d94a502013-01-22 12:26:30 +02001196{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001197 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Mika Westerberg7d94a502013-01-22 12:26:30 +02001198
1199 /* Disable the SSP now */
Lubomir Rintel29d7e052020-01-18 10:40:31 +01001200 pxa2xx_spi_off(drv_data);
Mika Westerberg7d94a502013-01-22 12:26:30 +02001201
Mika Westerberg7d94a502013-01-22 12:26:30 +02001202 return 0;
1203}
1204
Eric Miaoa7bb3902009-04-06 19:00:54 -07001205static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1206 struct pxa2xx_spi_chip *chip_info)
1207{
Jarkko Nikula3cc7b0e2018-02-01 17:17:30 +02001208 struct driver_data *drv_data =
1209 spi_controller_get_devdata(spi->controller);
Jan Kiszkac18d9252017-08-03 13:40:32 +02001210 struct gpio_desc *gpiod;
Eric Miaoa7bb3902009-04-06 19:00:54 -07001211 int err = 0;
1212
Mika Westerberg99f499c2016-09-26 15:19:50 +03001213 if (chip == NULL)
1214 return 0;
1215
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001216 if (drv_data->cs_gpiods) {
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001217 gpiod = drv_data->cs_gpiods[spi->chip_select];
1218 if (gpiod) {
Jan Kiszkac18d9252017-08-03 13:40:32 +02001219 chip->gpiod_cs = gpiod;
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001220 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1221 gpiod_set_value(gpiod, chip->gpio_cs_inverted);
Mika Westerberg99f499c2016-09-26 15:19:50 +03001222 }
1223
1224 return 0;
1225 }
1226
1227 if (chip_info == NULL)
Eric Miaoa7bb3902009-04-06 19:00:54 -07001228 return 0;
1229
1230 /* NOTE: setup() can be called multiple times, possibly with
1231 * different chip_info, release previously requested GPIO
1232 */
Jan Kiszkac18d9252017-08-03 13:40:32 +02001233 if (chip->gpiod_cs) {
Mark Browna885eeb2017-12-22 16:15:36 +00001234 gpiod_put(chip->gpiod_cs);
Jan Kiszkac18d9252017-08-03 13:40:32 +02001235 chip->gpiod_cs = NULL;
1236 }
Eric Miaoa7bb3902009-04-06 19:00:54 -07001237
1238 /* If (*cs_control) is provided, ignore GPIO chip select */
1239 if (chip_info->cs_control) {
1240 chip->cs_control = chip_info->cs_control;
1241 return 0;
1242 }
1243
1244 if (gpio_is_valid(chip_info->gpio_cs)) {
1245 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1246 if (err) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001247 dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1248 chip_info->gpio_cs);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001249 return err;
1250 }
1251
Jan Kiszkac18d9252017-08-03 13:40:32 +02001252 gpiod = gpio_to_desc(chip_info->gpio_cs);
1253 chip->gpiod_cs = gpiod;
Eric Miaoa7bb3902009-04-06 19:00:54 -07001254 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1255
Jan Kiszkac18d9252017-08-03 13:40:32 +02001256 err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001257 }
1258
1259 return err;
1260}
1261
Stephen Streete0c99052006-03-07 23:53:24 -08001262static int setup(struct spi_device *spi)
1263{
Jarkko Nikulabffc9672016-09-07 17:04:05 +03001264 struct pxa2xx_spi_chip *chip_info;
Stephen Streete0c99052006-03-07 23:53:24 -08001265 struct chip_data *chip;
Jarkko Nikuladccf7362015-06-04 16:55:11 +03001266 const struct lpss_config *config;
Jarkko Nikula3cc7b0e2018-02-01 17:17:30 +02001267 struct driver_data *drv_data =
1268 spi_controller_get_devdata(spi->controller);
Mika Westerberga0d26422013-01-22 12:26:32 +02001269 uint tx_thres, tx_hi_thres, rx_thres;
1270
Weike Chene5262d02014-11-26 02:35:10 -08001271 switch (drv_data->ssp_type) {
1272 case QUARK_X1000_SSP:
1273 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1274 tx_hi_thres = 0;
1275 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1276 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001277 case CE4100_SSP:
1278 tx_thres = TX_THRESH_CE4100_DFLT;
1279 tx_hi_thres = 0;
1280 rx_thres = RX_THRESH_CE4100_DFLT;
1281 break;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001282 case LPSS_LPT_SSP:
1283 case LPSS_BYT_SSP:
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001284 case LPSS_BSW_SSP:
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001285 case LPSS_SPT_SSP:
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +02001286 case LPSS_BXT_SSP:
Jarkko Nikulafc0b2ac2017-05-30 17:31:21 +03001287 case LPSS_CNL_SSP:
Jarkko Nikuladccf7362015-06-04 16:55:11 +03001288 config = lpss_get_config(drv_data);
1289 tx_thres = config->tx_threshold_lo;
1290 tx_hi_thres = config->tx_threshold_hi;
1291 rx_thres = config->rx_threshold;
Weike Chene5262d02014-11-26 02:35:10 -08001292 break;
1293 default:
Mika Westerberga0d26422013-01-22 12:26:32 +02001294 tx_hi_thres = 0;
Lubomir Rintel51eea522019-01-16 16:13:31 +01001295 if (spi_controller_is_slave(drv_data->controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001296 tx_thres = 1;
1297 rx_thres = 2;
1298 } else {
1299 tx_thres = TX_THRESH_DFLT;
1300 rx_thres = RX_THRESH_DFLT;
1301 }
Weike Chene5262d02014-11-26 02:35:10 -08001302 break;
Mika Westerberga0d26422013-01-22 12:26:32 +02001303 }
Stephen Streete0c99052006-03-07 23:53:24 -08001304
Stephen Street8d94cc52006-12-10 02:18:54 -08001305 /* Only alloc on first setup */
Stephen Streete0c99052006-03-07 23:53:24 -08001306 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -08001307 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -08001308 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001309 if (!chip)
Stephen Streete0c99052006-03-07 23:53:24 -08001310 return -ENOMEM;
1311
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001312 if (drv_data->ssp_type == CE4100_SSP) {
1313 if (spi->chip_select > 4) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001314 dev_err(&spi->dev,
1315 "failed setup: cs number must not be > 4.\n");
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001316 kfree(chip);
1317 return -EINVAL;
1318 }
1319
1320 chip->frm = spi->chip_select;
Jan Kiszkac18d9252017-08-03 13:40:32 +02001321 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001322 chip->enable_dma = drv_data->controller_info->enable_dma;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001323 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001324 }
1325
Stephen Street8d94cc52006-12-10 02:18:54 -08001326 /* protocol drivers may change the chip settings, so...
1327 * if chip_info exists, use it */
1328 chip_info = spi->controller_data;
1329
Stephen Streete0c99052006-03-07 23:53:24 -08001330 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -08001331 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001332 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001333 if (chip_info->timeout)
1334 chip->timeout = chip_info->timeout;
1335 if (chip_info->tx_threshold)
1336 tx_thres = chip_info->tx_threshold;
Mika Westerberga0d26422013-01-22 12:26:32 +02001337 if (chip_info->tx_hi_threshold)
1338 tx_hi_thres = chip_info->tx_hi_threshold;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001339 if (chip_info->rx_threshold)
1340 rx_thres = chip_info->rx_threshold;
Stephen Streete0c99052006-03-07 23:53:24 -08001341 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001342 if (chip_info->enable_loopback)
1343 chip->cr1 = SSCR1_LBM;
1344 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001345 if (spi_controller_is_slave(drv_data->controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001346 chip->cr1 |= SSCR1_SCFR;
1347 chip->cr1 |= SSCR1_SCLKDIR;
1348 chip->cr1 |= SSCR1_SFRMDIR;
1349 chip->cr1 |= SSCR1_SPH;
1350 }
Stephen Streete0c99052006-03-07 23:53:24 -08001351
Mika Westerberga0d26422013-01-22 12:26:32 +02001352 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1353 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1354 | SSITF_TxHiThresh(tx_hi_thres);
1355
Stephen Street8d94cc52006-12-10 02:18:54 -08001356 /* set dma burst and threshold outside of chip_info path so that if
1357 * chip_info goes away after setting chip->enable_dma, the
1358 * burst and threshold can still respond to changes in bits_per_word */
1359 if (chip->enable_dma) {
1360 /* set up legal burst and threshold for dma */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001361 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1362 spi->bits_per_word,
Stephen Street8d94cc52006-12-10 02:18:54 -08001363 &chip->dma_burst_size,
1364 &chip->dma_threshold)) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001365 dev_warn(&spi->dev,
1366 "in setup: DMA burst size reduced to match bits_per_word\n");
Stephen Street8d94cc52006-12-10 02:18:54 -08001367 }
Andy Shevchenko000c6af2019-03-19 17:48:43 +02001368 dev_dbg(&spi->dev,
1369 "in setup: DMA burst size set to %u\n",
1370 chip->dma_burst_size);
Stephen Street8d94cc52006-12-10 02:18:54 -08001371 }
1372
Weike Chene5262d02014-11-26 02:35:10 -08001373 switch (drv_data->ssp_type) {
1374 case QUARK_X1000_SSP:
1375 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1376 & QUARK_X1000_SSCR1_RFT)
1377 | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1378 & QUARK_X1000_SSCR1_TFT);
1379 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001380 case CE4100_SSP:
1381 chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
1382 (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
1383 break;
Weike Chene5262d02014-11-26 02:35:10 -08001384 default:
1385 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1386 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1387 break;
1388 }
1389
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -08001390 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1391 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1392 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001393
Mika Westerbergb8331722013-01-22 12:26:31 +02001394 if (spi->mode & SPI_LOOP)
1395 chip->cr1 |= SSCR1_LBM;
1396
Stephen Streete0c99052006-03-07 23:53:24 -08001397 if (spi->bits_per_word <= 8) {
1398 chip->n_bytes = 1;
Stephen Streete0c99052006-03-07 23:53:24 -08001399 chip->read = u8_reader;
1400 chip->write = u8_writer;
1401 } else if (spi->bits_per_word <= 16) {
1402 chip->n_bytes = 2;
Stephen Streete0c99052006-03-07 23:53:24 -08001403 chip->read = u16_reader;
1404 chip->write = u16_writer;
1405 } else if (spi->bits_per_word <= 32) {
Stephen Streete0c99052006-03-07 23:53:24 -08001406 chip->n_bytes = 4;
Stephen Streete0c99052006-03-07 23:53:24 -08001407 chip->read = u32_reader;
1408 chip->write = u32_writer;
Stephen Streete0c99052006-03-07 23:53:24 -08001409 }
Stephen Streete0c99052006-03-07 23:53:24 -08001410
1411 spi_set_ctldata(spi, chip);
1412
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001413 if (drv_data->ssp_type == CE4100_SSP)
1414 return 0;
1415
Eric Miaoa7bb3902009-04-06 19:00:54 -07001416 return setup_cs(spi, chip, chip_info);
Stephen Streete0c99052006-03-07 23:53:24 -08001417}
1418
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001419static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001420{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001421 struct chip_data *chip = spi_get_ctldata(spi);
Jarkko Nikula3cc7b0e2018-02-01 17:17:30 +02001422 struct driver_data *drv_data =
1423 spi_controller_get_devdata(spi->controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001424
Daniel Ribeiro7348d822009-05-12 13:19:36 -07001425 if (!chip)
1426 return;
1427
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001428 if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
Jan Kiszkac18d9252017-08-03 13:40:32 +02001429 chip->gpiod_cs)
Mark Browna885eeb2017-12-22 16:15:36 +00001430 gpiod_put(chip->gpiod_cs);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001431
Stephen Streete0c99052006-03-07 23:53:24 -08001432 kfree(chip);
1433}
1434
Lee Jones9b2d6112020-07-17 14:54:23 +01001435#ifdef CONFIG_ACPI
Mathias Krause8422ddf2015-06-13 14:22:14 +02001436static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001437 { "INT33C0", LPSS_LPT_SSP },
1438 { "INT33C1", LPSS_LPT_SSP },
1439 { "INT3430", LPSS_LPT_SSP },
1440 { "INT3431", LPSS_LPT_SSP },
1441 { "80860F0E", LPSS_BYT_SSP },
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001442 { "8086228E", LPSS_BSW_SSP },
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001443 { },
1444};
1445MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
Lee Jones9b2d6112020-07-17 14:54:23 +01001446#endif
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001447
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001448/*
1449 * PCI IDs of compound devices that integrate both host controller and private
1450 * integrated DMA engine. Please note these are not used in module
1451 * autoloading and probing in this module but matching the LPSS SSP type.
1452 */
1453static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
1454 /* SPT-LP */
1455 { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
1456 { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
1457 /* SPT-H */
1458 { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
1459 { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
Mika Westerberg704d2b02016-07-04 13:21:07 +03001460 /* KBL-H */
1461 { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1462 { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
Jarkko Nikula6157d4c2020-01-16 11:10:35 +02001463 /* CML-V */
1464 { PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
1465 { PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
Jarkko Nikulac1b03f12016-03-02 09:54:14 +02001466 /* BXT A-Step */
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +02001467 { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1468 { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1469 { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
Jarkko Nikulac1b03f12016-03-02 09:54:14 +02001470 /* BXT B-Step */
1471 { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1472 { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1473 { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
David E. Boxe18a80a2017-01-19 16:25:21 +02001474 /* GLK */
1475 { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1476 { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1477 { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
Mika Westerberg22d71a502018-06-28 13:52:23 +03001478 /* ICL-LP */
1479 { PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
1480 { PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
1481 { PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
Jarkko Nikula8cc77202019-07-03 14:46:03 +03001482 /* EHL */
1483 { PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
1484 { PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
1485 { PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
Jarkko Nikula9c7315c2019-11-25 14:51:59 +02001486 /* JSL */
1487 { PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
1488 { PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
1489 { PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
Jarkko Nikulacf961fc2020-06-25 17:00:41 +03001490 /* TGL-H */
1491 { PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
1492 { PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
1493 { PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
1494 { PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +02001495 /* APL */
1496 { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1497 { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1498 { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
Jarkko Nikulafc0b2ac2017-05-30 17:31:21 +03001499 /* CNL-LP */
1500 { PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1501 { PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1502 { PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1503 /* CNL-H */
1504 { PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1505 { PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1506 { PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
Evan Green41a91802019-04-15 20:27:43 -07001507 /* CML-LP */
1508 { PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
1509 { PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
1510 { PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
Jarkko Nikulaf0cf17e2019-10-29 13:58:02 +02001511 /* CML-H */
1512 { PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1513 { PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1514 { PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
Jarkko Nikulaa4127952019-08-01 16:49:01 +03001515 /* TGL-LP */
1516 { PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1517 { PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1518 { PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1519 { PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1520 { PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1521 { PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1522 { PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
Axel Lin94e5c232015-08-04 13:52:22 +08001523 { },
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001524};
1525
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001526static const struct of_device_id pxa2xx_spi_of_match[] = {
1527 { .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
1528 {},
1529};
1530MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
1531
1532#ifdef CONFIG_ACPI
1533
Andy Shevchenko365e8562019-10-18 13:54:27 +03001534static int pxa2xx_spi_get_port_id(struct device *dev)
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001535{
Andy Shevchenko365e8562019-10-18 13:54:27 +03001536 struct acpi_device *adev;
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001537 unsigned int devid;
1538 int port_id = -1;
1539
Andy Shevchenko365e8562019-10-18 13:54:27 +03001540 adev = ACPI_COMPANION(dev);
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001541 if (adev && adev->pnp.unique_id &&
1542 !kstrtouint(adev->pnp.unique_id, 0, &devid))
1543 port_id = devid;
1544 return port_id;
1545}
1546
1547#else /* !CONFIG_ACPI */
1548
Andy Shevchenko365e8562019-10-18 13:54:27 +03001549static int pxa2xx_spi_get_port_id(struct device *dev)
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001550{
1551 return -1;
1552}
1553
1554#endif /* CONFIG_ACPI */
1555
1556
1557#ifdef CONFIG_PCI
1558
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001559static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
1560{
Andy Shevchenko5ba846b2019-03-18 18:39:30 +03001561 return param == chan->device->dev;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001562}
1563
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001564#endif /* CONFIG_PCI */
1565
Lubomir Rintel51eea522019-01-16 16:13:31 +01001566static struct pxa2xx_spi_controller *
Jarkko Nikula0db64212015-10-28 15:13:43 +02001567pxa2xx_spi_init_pdata(struct platform_device *pdev)
Mika Westerberga3496852013-01-22 12:26:33 +02001568{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001569 struct pxa2xx_spi_controller *pdata;
Mika Westerberga3496852013-01-22 12:26:33 +02001570 struct ssp_device *ssp;
1571 struct resource *res;
Andy Shevchenko6fb74272019-10-21 13:36:24 +03001572 struct device *parent = pdev->dev.parent;
1573 struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001574 const struct pci_device_id *pcidev_id = NULL;
Lubomir Rintel55ef8262018-10-10 19:09:28 +02001575 enum pxa_ssp_type type;
Andy Shevchenkof2faa3e2019-10-18 13:54:28 +03001576 const void *match;
Mika Westerberga3496852013-01-22 12:26:33 +02001577
Andy Shevchenko6fb74272019-10-21 13:36:24 +03001578 if (pcidev)
1579 pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
Mika Westerberga3496852013-01-22 12:26:33 +02001580
Andy Shevchenkof2faa3e2019-10-18 13:54:28 +03001581 match = device_get_match_data(&pdev->dev);
1582 if (match)
1583 type = (enum pxa_ssp_type)match;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001584 else if (pcidev_id)
Lubomir Rintel55ef8262018-10-10 19:09:28 +02001585 type = (enum pxa_ssp_type)pcidev_id->driver_data;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001586 else
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001587 return ERR_PTR(-EINVAL);
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001588
Mika Westerbergcc0ee982013-06-20 17:44:22 +03001589 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001590 if (!pdata)
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001591 return ERR_PTR(-ENOMEM);
Mika Westerberga3496852013-01-22 12:26:33 +02001592
Mika Westerberga3496852013-01-22 12:26:33 +02001593 ssp = &pdata->ssp;
1594
Andy Shevchenko77c544d2019-10-21 13:36:25 +03001595 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sachin Kamatcbfd6a22013-04-08 15:49:33 +05301596 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1597 if (IS_ERR(ssp->mmio_base))
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001598 return ERR_CAST(ssp->mmio_base);
Mika Westerberga3496852013-01-22 12:26:33 +02001599
Andy Shevchenko77c544d2019-10-21 13:36:25 +03001600 ssp->phys_base = res->start;
1601
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001602#ifdef CONFIG_PCI
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001603 if (pcidev_id) {
Andy Shevchenko6fb74272019-10-21 13:36:24 +03001604 pdata->tx_param = parent;
1605 pdata->rx_param = parent;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001606 pdata->dma_filter = pxa2xx_spi_idma_filter;
1607 }
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001608#endif
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001609
Mika Westerberga3496852013-01-22 12:26:33 +02001610 ssp->clk = devm_clk_get(&pdev->dev, NULL);
Chuhong Yuan5eb263e2019-11-09 16:09:43 +08001611 if (IS_ERR(ssp->clk))
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001612 return ERR_CAST(ssp->clk);
Mika Westerberga3496852013-01-22 12:26:33 +02001613
Mika Westerberga3496852013-01-22 12:26:33 +02001614 ssp->irq = platform_get_irq(pdev, 0);
Chuhong Yuan5eb263e2019-11-09 16:09:43 +08001615 if (ssp->irq < 0)
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001616 return ERR_PTR(ssp->irq);
Chuhong Yuan5eb263e2019-11-09 16:09:43 +08001617
Mika Westerberga3496852013-01-22 12:26:33 +02001618 ssp->type = type;
Andy Shevchenko4f3d9572019-10-18 13:54:25 +03001619 ssp->dev = &pdev->dev;
Andy Shevchenko365e8562019-10-18 13:54:27 +03001620 ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
Mika Westerberga3496852013-01-22 12:26:33 +02001621
Andy Shevchenkof2faa3e2019-10-18 13:54:28 +03001622 pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
Mika Westerberga3496852013-01-22 12:26:33 +02001623 pdata->num_chipselect = 1;
Mika Westerbergcddb3392013-05-13 13:45:10 +03001624 pdata->enable_dma = true;
Andy Shevchenko37821a822019-03-19 17:48:42 +02001625 pdata->dma_burst_size = 1;
Mika Westerberga3496852013-01-22 12:26:33 +02001626
1627 return pdata;
1628}
1629
Lubomir Rintel51eea522019-01-16 16:13:31 +01001630static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
Jarkko Nikula3cc7b0e2018-02-01 17:17:30 +02001631 unsigned int cs)
Mika Westerberg0c27d9c2016-02-08 17:14:29 +02001632{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001633 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Mika Westerberg0c27d9c2016-02-08 17:14:29 +02001634
1635 if (has_acpi_companion(&drv_data->pdev->dev)) {
1636 switch (drv_data->ssp_type) {
1637 /*
1638 * For Atoms the ACPI DeviceSelection used by the Windows
1639 * driver starts from 1 instead of 0 so translate it here
1640 * to match what Linux expects.
1641 */
1642 case LPSS_BYT_SSP:
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001643 case LPSS_BSW_SSP:
Mika Westerberg0c27d9c2016-02-08 17:14:29 +02001644 return cs - 1;
1645
1646 default:
1647 break;
1648 }
1649 }
1650
1651 return cs;
1652}
1653
Daniel Vetterb2662a12019-10-17 08:44:26 +02001654static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1655{
1656 return MAX_DMA_LEN;
1657}
1658
Grant Likelyfd4a3192012-12-07 16:57:14 +00001659static int pxa2xx_spi_probe(struct platform_device *pdev)
Stephen Streete0c99052006-03-07 23:53:24 -08001660{
1661 struct device *dev = &pdev->dev;
Lubomir Rintel51eea522019-01-16 16:13:31 +01001662 struct pxa2xx_spi_controller *platform_info;
1663 struct spi_controller *controller;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001664 struct driver_data *drv_data;
eric miao2f1a74e2007-11-21 18:50:53 +08001665 struct ssp_device *ssp;
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02001666 const struct lpss_config *config;
Mika Westerberg99f499c2016-09-26 15:19:50 +03001667 int status, count;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001668 u32 tmp;
Stephen Streete0c99052006-03-07 23:53:24 -08001669
Mika Westerberg851bacf2013-01-07 12:44:33 +02001670 platform_info = dev_get_platdata(dev);
1671 if (!platform_info) {
Jarkko Nikula0db64212015-10-28 15:13:43 +02001672 platform_info = pxa2xx_spi_init_pdata(pdev);
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001673 if (IS_ERR(platform_info)) {
Mika Westerberga3496852013-01-22 12:26:33 +02001674 dev_err(&pdev->dev, "missing platform data\n");
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001675 return PTR_ERR(platform_info);
Mika Westerberga3496852013-01-22 12:26:33 +02001676 }
Mika Westerberg851bacf2013-01-07 12:44:33 +02001677 }
Stephen Streete0c99052006-03-07 23:53:24 -08001678
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001679 ssp = pxa_ssp_request(pdev->id, pdev->name);
Mika Westerberg851bacf2013-01-07 12:44:33 +02001680 if (!ssp)
1681 ssp = &platform_info->ssp;
1682
1683 if (!ssp->mmio_base) {
1684 dev_err(&pdev->dev, "failed to get ssp\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001685 return -ENODEV;
1686 }
1687
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001688 if (platform_info->is_slave)
Lubomir Rintel51eea522019-01-16 16:13:31 +01001689 controller = spi_alloc_slave(dev, sizeof(struct driver_data));
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001690 else
Lubomir Rintel51eea522019-01-16 16:13:31 +01001691 controller = spi_alloc_master(dev, sizeof(struct driver_data));
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001692
Lubomir Rintel51eea522019-01-16 16:13:31 +01001693 if (!controller) {
1694 dev_err(&pdev->dev, "cannot alloc spi_controller\n");
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001695 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001696 return -ENOMEM;
1697 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001698 drv_data = spi_controller_get_devdata(controller);
1699 drv_data->controller = controller;
1700 drv_data->controller_info = platform_info;
Stephen Streete0c99052006-03-07 23:53:24 -08001701 drv_data->pdev = pdev;
eric miao2f1a74e2007-11-21 18:50:53 +08001702 drv_data->ssp = ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001703
Lubomir Rintel51eea522019-01-16 16:13:31 +01001704 controller->dev.of_node = pdev->dev.of_node;
David Brownelle7db06b2009-06-17 16:26:04 -07001705 /* the spi->mode bits understood by this driver: */
Lubomir Rintel51eea522019-01-16 16:13:31 +01001706 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
David Brownelle7db06b2009-06-17 16:26:04 -07001707
Lubomir Rintel51eea522019-01-16 16:13:31 +01001708 controller->bus_num = ssp->port_id;
1709 controller->dma_alignment = DMA_ALIGNMENT;
1710 controller->cleanup = cleanup;
1711 controller->setup = setup;
1712 controller->set_cs = pxa2xx_spi_set_cs;
1713 controller->transfer_one = pxa2xx_spi_transfer_one;
1714 controller->slave_abort = pxa2xx_spi_slave_abort;
1715 controller->handle_err = pxa2xx_spi_handle_err;
1716 controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
1717 controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
1718 controller->auto_runtime_pm = true;
1719 controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
Stephen Streete0c99052006-03-07 23:53:24 -08001720
eric miao2f1a74e2007-11-21 18:50:53 +08001721 drv_data->ssp_type = ssp->type;
Stephen Streete0c99052006-03-07 23:53:24 -08001722
eric miao2f1a74e2007-11-21 18:50:53 +08001723 drv_data->ioaddr = ssp->mmio_base;
1724 drv_data->ssdr_physical = ssp->phys_base + SSDR;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001725 if (pxa25x_ssp_comp(drv_data)) {
Weike Chene5262d02014-11-26 02:35:10 -08001726 switch (drv_data->ssp_type) {
1727 case QUARK_X1000_SSP:
Lubomir Rintel51eea522019-01-16 16:13:31 +01001728 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Weike Chene5262d02014-11-26 02:35:10 -08001729 break;
1730 default:
Lubomir Rintel51eea522019-01-16 16:13:31 +01001731 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Weike Chene5262d02014-11-26 02:35:10 -08001732 break;
1733 }
1734
Stephen Streete0c99052006-03-07 23:53:24 -08001735 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1736 drv_data->dma_cr1 = 0;
1737 drv_data->clear_sr = SSSR_ROR;
1738 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1739 } else {
Lubomir Rintel51eea522019-01-16 16:13:31 +01001740 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Stephen Streete0c99052006-03-07 23:53:24 -08001741 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
Mika Westerberg59288082013-01-22 12:26:29 +02001742 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
Stephen Streete0c99052006-03-07 23:53:24 -08001743 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001744 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1745 | SSSR_ROR | SSSR_TUR;
Stephen Streete0c99052006-03-07 23:53:24 -08001746 }
1747
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -08001748 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1749 drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001750 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001751 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Lubomir Rintel51eea522019-01-16 16:13:31 +01001752 goto out_error_controller_alloc;
Stephen Streete0c99052006-03-07 23:53:24 -08001753 }
1754
1755 /* Setup DMA if requested */
Stephen Streete0c99052006-03-07 23:53:24 -08001756 if (platform_info->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001757 status = pxa2xx_spi_dma_setup(drv_data);
1758 if (status) {
Flavio Suligoi8b57b112019-04-05 14:40:22 +02001759 dev_warn(dev, "no DMA channels available, using PIO\n");
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001760 platform_info->enable_dma = false;
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001761 } else {
Lubomir Rintel51eea522019-01-16 16:13:31 +01001762 controller->can_dma = pxa2xx_spi_can_dma;
Mark Brownbf9f7422019-02-20 17:58:18 +00001763 controller->max_dma_len = MAX_DMA_LEN;
Daniel Vetterb2662a12019-10-17 08:44:26 +02001764 controller->max_transfer_size =
1765 pxa2xx_spi_max_dma_transfer_size;
Stephen Streete0c99052006-03-07 23:53:24 -08001766 }
Stephen Streete0c99052006-03-07 23:53:24 -08001767 }
1768
1769 /* Enable SOC clock */
Tobias Jordan62bbc862018-04-30 16:30:06 +02001770 status = clk_prepare_enable(ssp->clk);
1771 if (status)
1772 goto out_error_dma_irq_alloc;
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001773
Lubomir Rintel51eea522019-01-16 16:13:31 +01001774 controller->max_speed_hz = clk_get_rate(ssp->clk);
Jarkko Nikula23cdddb2019-06-28 17:07:17 +03001775 /*
1776 * Set minimum speed for all other platforms than Intel Quark which is
1777 * able do under 1 Hz transfers.
1778 */
1779 if (!pxa25x_ssp_comp(drv_data))
1780 controller->min_speed_hz =
1781 DIV_ROUND_UP(controller->max_speed_hz, 4096);
1782 else if (!is_quark_x1000_ssp(drv_data))
1783 controller->min_speed_hz =
1784 DIV_ROUND_UP(controller->max_speed_hz, 512);
Stephen Streete0c99052006-03-07 23:53:24 -08001785
1786 /* Load default SSP configuration */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001787 pxa2xx_spi_write(drv_data, SSCR0, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001788 switch (drv_data->ssp_type) {
1789 case QUARK_X1000_SSP:
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001790 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
1791 QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001792 pxa2xx_spi_write(drv_data, SSCR1, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001793
1794 /* using the Motorola SPI protocol and use 8 bit frame */
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001795 tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
1796 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001797 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001798 case CE4100_SSP:
1799 tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
1800 CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
1801 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1802 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1803 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Andy Shevchenkoa2dd8af2017-01-02 13:44:28 +02001804 break;
Weike Chene5262d02014-11-26 02:35:10 -08001805 default:
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001806
Lubomir Rintel51eea522019-01-16 16:13:31 +01001807 if (spi_controller_is_slave(controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001808 tmp = SSCR1_SCFR |
1809 SSCR1_SCLKDIR |
1810 SSCR1_SFRMDIR |
1811 SSCR1_RxTresh(2) |
1812 SSCR1_TxTresh(1) |
1813 SSCR1_SPH;
1814 } else {
1815 tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1816 SSCR1_TxTresh(TX_THRESH_DFLT);
1817 }
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001818 pxa2xx_spi_write(drv_data, SSCR1, tmp);
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001819 tmp = SSCR0_Motorola | SSCR0_DataSize(8);
Lubomir Rintel51eea522019-01-16 16:13:31 +01001820 if (!spi_controller_is_slave(controller))
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001821 tmp |= SSCR0_SCR(2);
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001822 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001823 break;
1824 }
1825
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001826 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001827 pxa2xx_spi_write(drv_data, SSTO, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001828
1829 if (!is_quark_x1000_ssp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001830 pxa2xx_spi_write(drv_data, SSPSP, 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001831
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02001832 if (is_lpss_ssp(drv_data)) {
1833 lpss_ssp_setup(drv_data);
1834 config = lpss_get_config(drv_data);
1835 if (config->reg_capabilities >= 0) {
1836 tmp = __lpss_ssp_read_priv(drv_data,
1837 config->reg_capabilities);
1838 tmp &= LPSS_CAPS_CS_EN_MASK;
1839 tmp >>= LPSS_CAPS_CS_EN_SHIFT;
1840 platform_info->num_chipselect = ffz(tmp);
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001841 } else if (config->cs_num) {
1842 platform_info->num_chipselect = config->cs_num;
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02001843 }
1844 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001845 controller->num_chipselect = platform_info->num_chipselect;
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02001846
Mika Westerberg99f499c2016-09-26 15:19:50 +03001847 count = gpiod_count(&pdev->dev, "cs");
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001848 if (count > 0) {
1849 int i;
1850
Lubomir Rintel51eea522019-01-16 16:13:31 +01001851 controller->num_chipselect = max_t(int, count,
1852 controller->num_chipselect);
Mika Westerberg99f499c2016-09-26 15:19:50 +03001853
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001854 drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
Lubomir Rintel51eea522019-01-16 16:13:31 +01001855 controller->num_chipselect, sizeof(struct gpio_desc *),
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001856 GFP_KERNEL);
1857 if (!drv_data->cs_gpiods) {
1858 status = -ENOMEM;
1859 goto out_error_clock_enabled;
1860 }
1861
Lubomir Rintel51eea522019-01-16 16:13:31 +01001862 for (i = 0; i < controller->num_chipselect; i++) {
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001863 struct gpio_desc *gpiod;
1864
Andy Shevchenkod35f2dc2017-07-27 18:49:33 +03001865 gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001866 if (IS_ERR(gpiod)) {
1867 /* Means use native chip select */
1868 if (PTR_ERR(gpiod) == -ENOENT)
1869 continue;
1870
Lubomir Rintel77d33892018-11-13 11:22:27 +01001871 status = PTR_ERR(gpiod);
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001872 goto out_error_clock_enabled;
1873 } else {
1874 drv_data->cs_gpiods[i] = gpiod;
1875 }
1876 }
1877 }
1878
Lubomir Rintel77d33892018-11-13 11:22:27 +01001879 if (platform_info->is_slave) {
1880 drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
1881 "ready", GPIOD_OUT_LOW);
1882 if (IS_ERR(drv_data->gpiod_ready)) {
1883 status = PTR_ERR(drv_data->gpiod_ready);
1884 goto out_error_clock_enabled;
1885 }
1886 }
1887
Antonio Ospite836d1a222014-05-30 18:18:09 +02001888 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1889 pm_runtime_use_autosuspend(&pdev->dev);
1890 pm_runtime_set_active(&pdev->dev);
1891 pm_runtime_enable(&pdev->dev);
1892
Stephen Streete0c99052006-03-07 23:53:24 -08001893 /* Register with the SPI framework */
1894 platform_set_drvdata(pdev, drv_data);
Lukas Wunner32e5b572020-05-25 14:25:02 +02001895 status = spi_register_controller(controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001896 if (status != 0) {
Lubomir Rintel51eea522019-01-16 16:13:31 +01001897 dev_err(&pdev->dev, "problem registering spi controller\n");
Lubomir Rintel12742042019-07-19 14:27:13 +02001898 goto out_error_pm_runtime_enabled;
Stephen Streete0c99052006-03-07 23:53:24 -08001899 }
1900
1901 return status;
1902
Lubomir Rintel12742042019-07-19 14:27:13 +02001903out_error_pm_runtime_enabled:
Jarkko Nikulae2b714a2018-03-07 17:05:04 +02001904 pm_runtime_disable(&pdev->dev);
Lubomir Rintel12742042019-07-19 14:27:13 +02001905
1906out_error_clock_enabled:
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001907 clk_disable_unprepare(ssp->clk);
Tobias Jordan62bbc862018-04-30 16:30:06 +02001908
1909out_error_dma_irq_alloc:
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001910 pxa2xx_spi_dma_release(drv_data);
eric miao2f1a74e2007-11-21 18:50:53 +08001911 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001912
Lubomir Rintel51eea522019-01-16 16:13:31 +01001913out_error_controller_alloc:
1914 spi_controller_put(controller);
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001915 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001916 return status;
1917}
1918
1919static int pxa2xx_spi_remove(struct platform_device *pdev)
1920{
1921 struct driver_data *drv_data = platform_get_drvdata(pdev);
Andy Shevchenko3d24b2a2020-02-24 17:45:56 +02001922 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001923
Mika Westerberg7d94a502013-01-22 12:26:30 +02001924 pm_runtime_get_sync(&pdev->dev);
1925
Lukas Wunner32e5b572020-05-25 14:25:02 +02001926 spi_unregister_controller(drv_data->controller);
1927
Stephen Streete0c99052006-03-07 23:53:24 -08001928 /* Disable the SSP at the peripheral and SOC level */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001929 pxa2xx_spi_write(drv_data, SSCR0, 0);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001930 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001931
1932 /* Release DMA */
Lubomir Rintel51eea522019-01-16 16:13:31 +01001933 if (drv_data->controller_info->enable_dma)
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001934 pxa2xx_spi_dma_release(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001935
Mika Westerberg7d94a502013-01-22 12:26:30 +02001936 pm_runtime_put_noidle(&pdev->dev);
1937 pm_runtime_disable(&pdev->dev);
1938
Stephen Streete0c99052006-03-07 23:53:24 -08001939 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001940 free_irq(ssp->irq, drv_data);
1941
1942 /* Release SSP */
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001943 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001944
Stephen Streete0c99052006-03-07 23:53:24 -08001945 return 0;
1946}
1947
Mika Westerberg382cebb2014-01-16 14:50:55 +02001948#ifdef CONFIG_PM_SLEEP
Mike Rapoport86d25932009-07-21 17:50:16 +03001949static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001950{
Mike Rapoport86d25932009-07-21 17:50:16 +03001951 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001952 struct ssp_device *ssp = drv_data->ssp;
Jarkko Nikulabffc9672016-09-07 17:04:05 +03001953 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001954
Lubomir Rintel51eea522019-01-16 16:13:31 +01001955 status = spi_controller_suspend(drv_data->controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001956 if (status != 0)
1957 return status;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001958 pxa2xx_spi_write(drv_data, SSCR0, 0);
Dmitry Eremin-Solenikov2b9375b2014-11-06 14:08:29 +03001959
1960 if (!pm_runtime_suspended(dev))
1961 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001962
1963 return 0;
1964}
1965
Mike Rapoport86d25932009-07-21 17:50:16 +03001966static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001967{
Mike Rapoport86d25932009-07-21 17:50:16 +03001968 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001969 struct ssp_device *ssp = drv_data->ssp;
Jarkko Nikulabffc9672016-09-07 17:04:05 +03001970 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001971
1972 /* Enable the SSP clock */
Tobias Jordan62bbc862018-04-30 16:30:06 +02001973 if (!pm_runtime_suspended(dev)) {
1974 status = clk_prepare_enable(ssp->clk);
1975 if (status)
1976 return status;
1977 }
Stephen Streete0c99052006-03-07 23:53:24 -08001978
1979 /* Start the queue running */
Lubomir Rintel51eea522019-01-16 16:13:31 +01001980 return spi_controller_resume(drv_data->controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001981}
Mika Westerberg7d94a502013-01-22 12:26:30 +02001982#endif
1983
Rafael J. Wysockiec833052014-12-13 00:41:15 +01001984#ifdef CONFIG_PM
Mika Westerberg7d94a502013-01-22 12:26:30 +02001985static int pxa2xx_spi_runtime_suspend(struct device *dev)
1986{
1987 struct driver_data *drv_data = dev_get_drvdata(dev);
1988
1989 clk_disable_unprepare(drv_data->ssp->clk);
1990 return 0;
1991}
1992
1993static int pxa2xx_spi_runtime_resume(struct device *dev)
1994{
1995 struct driver_data *drv_data = dev_get_drvdata(dev);
Tobias Jordan62bbc862018-04-30 16:30:06 +02001996 int status;
Mika Westerberg7d94a502013-01-22 12:26:30 +02001997
Tobias Jordan62bbc862018-04-30 16:30:06 +02001998 status = clk_prepare_enable(drv_data->ssp->clk);
1999 return status;
Mika Westerberg7d94a502013-01-22 12:26:30 +02002000}
2001#endif
Mike Rapoport86d25932009-07-21 17:50:16 +03002002
Alexey Dobriyan47145212009-12-14 18:00:08 -08002003static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mika Westerberg7d94a502013-01-22 12:26:30 +02002004 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
2005 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
2006 pxa2xx_spi_runtime_resume, NULL)
Mike Rapoport86d25932009-07-21 17:50:16 +03002007};
Stephen Streete0c99052006-03-07 23:53:24 -08002008
2009static struct platform_driver driver = {
2010 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03002011 .name = "pxa2xx-spi",
Mike Rapoport86d25932009-07-21 17:50:16 +03002012 .pm = &pxa2xx_spi_pm_ops,
Mika Westerberga3496852013-01-22 12:26:33 +02002013 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02002014 .of_match_table = of_match_ptr(pxa2xx_spi_of_match),
Stephen Streete0c99052006-03-07 23:53:24 -08002015 },
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08002016 .probe = pxa2xx_spi_probe,
David Brownelld1e44d92007-10-16 01:27:46 -07002017 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08002018};
2019
2020static int __init pxa2xx_spi_init(void)
2021{
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08002022 return platform_driver_register(&driver);
Stephen Streete0c99052006-03-07 23:53:24 -08002023}
Antonio Ospite5b61a742009-09-22 16:46:10 -07002024subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08002025
2026static void __exit pxa2xx_spi_exit(void)
2027{
2028 platform_driver_unregister(&driver);
2029}
2030module_exit(pxa2xx_spi_exit);
Flavio Suligoi51ebf6a2019-04-10 14:51:36 +02002031
2032MODULE_SOFTDEP("pre: dw_dmac");