blob: 087c84e605b94496a3bb0cc22295c5fe9613ff44 [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>
Andy Shevchenko0e476872021-04-23 21:24:31 +030012#include <linux/dmaengine.h>
Sachin Kamatcbfd6a22013-04-08 15:49:33 +053013#include <linux/err.h>
Andy Shevchenko5ce25702019-10-18 13:54:26 +030014#include <linux/errno.h>
15#include <linux/gpio/consumer.h>
16#include <linux/gpio.h>
17#include <linux/init.h>
Stephen Streete0c99052006-03-07 23:53:24 -080018#include <linux/interrupt.h>
Andy Shevchenko5ce25702019-10-18 13:54:26 +030019#include <linux/ioport.h>
Andy Shevchenko9df461e2015-03-25 15:06:16 +020020#include <linux/kernel.h>
Andy Shevchenko5ce25702019-10-18 13:54:26 +030021#include <linux/module.h>
Andy Shevchenkoae8fbf12019-10-18 13:54:29 +030022#include <linux/mod_devicetable.h>
23#include <linux/of.h>
Jarkko Nikula34cadd92015-07-30 16:30:07 +030024#include <linux/pci.h>
Stephen Streete0c99052006-03-07 23:53:24 -080025#include <linux/platform_device.h>
Andy Shevchenko5ce25702019-10-18 13:54:26 +030026#include <linux/pm_runtime.h>
Andy Shevchenkof2faa3e2019-10-18 13:54:28 +030027#include <linux/property.h>
Andy Shevchenko5ce25702019-10-18 13:54:26 +030028#include <linux/slab.h>
Andy Shevchenko0e476872021-04-23 21:24:31 +030029
Sebastian Andrzej Siewior8348c252010-11-22 17:12:15 -080030#include <linux/spi/pxa2xx_spi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080031#include <linux/spi/spi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080032
Mika Westerbergcd7bed02013-01-22 12:26:28 +020033#include "spi-pxa2xx.h"
Stephen Streete0c99052006-03-07 23:53:24 -080034
35MODULE_AUTHOR("Stephen Street");
Will Newton037cdaf2007-12-10 15:49:25 -080036MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
Stephen Streete0c99052006-03-07 23:53:24 -080037MODULE_LICENSE("GPL");
Kay Sievers7e38c3c2008-04-10 21:29:20 -070038MODULE_ALIAS("platform:pxa2xx-spi");
Stephen Streete0c99052006-03-07 23:53:24 -080039
Vernon Sauderf1f640a2008-10-15 22:02:43 -070040#define TIMOUT_DFLT 1000
41
Ned Forresterb97c74b2008-02-23 15:23:40 -080042/*
43 * for testing SSCR1 changes that require SSP restart, basically
44 * everything except the service and interrupt enables, the pxa270 developer
45 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
46 * list, but the PXA255 dev man says all bits without really meaning the
47 * service and interrupt enables
48 */
49#define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
Stephen Street8d94cc52006-12-10 02:18:54 -080050 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
Ned Forresterb97c74b2008-02-23 15:23:40 -080051 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
52 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
53 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
54 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
Stephen Street8d94cc52006-12-10 02:18:54 -080055
Weike Chene5262d02014-11-26 02:35:10 -080056#define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
57 | QUARK_X1000_SSCR1_EFWR \
58 | QUARK_X1000_SSCR1_RFT \
59 | QUARK_X1000_SSCR1_TFT \
60 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
61
Andy Shevchenko7c7289a2016-09-07 15:43:22 +030062#define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
63 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
64 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
65 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
66 | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
67 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
68
Jarkko Nikula624ea722015-10-28 15:13:39 +020069#define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
70#define LPSS_CS_CONTROL_SW_MODE BIT(0)
71#define LPSS_CS_CONTROL_CS_HIGH BIT(1)
Jarkko Nikula8b136ba2015-10-28 15:13:41 +020072#define LPSS_CAPS_CS_EN_SHIFT 9
73#define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
Mika Westerberga0d26422013-01-22 12:26:32 +020074
Evan Green683f65d2020-02-11 14:37:00 -080075#define LPSS_PRIV_CLOCK_GATE 0x38
76#define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
77#define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
78
Jarkko Nikuladccf7362015-06-04 16:55:11 +030079struct lpss_config {
80 /* LPSS offset from drv_data->ioaddr */
81 unsigned offset;
82 /* Register offsets from drv_data->lpss_base or -1 */
83 int reg_general;
84 int reg_ssp;
85 int reg_cs_ctrl;
Jarkko Nikula8b136ba2015-10-28 15:13:41 +020086 int reg_capabilities;
Jarkko Nikuladccf7362015-06-04 16:55:11 +030087 /* FIFO thresholds */
88 u32 rx_threshold;
89 u32 tx_threshold_lo;
90 u32 tx_threshold_hi;
Mika Westerbergc1e4a532016-02-08 17:14:30 +020091 /* Chip select control */
92 unsigned cs_sel_shift;
93 unsigned cs_sel_mask;
Mika Westerberg30f3a6a2016-02-08 17:14:31 +020094 unsigned cs_num;
Evan Green683f65d2020-02-11 14:37:00 -080095 /* Quirks */
96 unsigned cs_clk_stays_gated : 1;
Jarkko Nikuladccf7362015-06-04 16:55:11 +030097};
98
99/* Keep these sorted with enum pxa_ssp_type */
100static const struct lpss_config lpss_platforms[] = {
101 { /* LPSS_LPT_SSP */
102 .offset = 0x800,
103 .reg_general = 0x08,
104 .reg_ssp = 0x0c,
105 .reg_cs_ctrl = 0x18,
Jarkko Nikula8b136ba2015-10-28 15:13:41 +0200106 .reg_capabilities = -1,
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300107 .rx_threshold = 64,
108 .tx_threshold_lo = 160,
109 .tx_threshold_hi = 224,
110 },
111 { /* LPSS_BYT_SSP */
112 .offset = 0x400,
113 .reg_general = 0x08,
114 .reg_ssp = 0x0c,
115 .reg_cs_ctrl = 0x18,
Jarkko Nikula8b136ba2015-10-28 15:13:41 +0200116 .reg_capabilities = -1,
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300117 .rx_threshold = 64,
118 .tx_threshold_lo = 160,
119 .tx_threshold_hi = 224,
120 },
Mika Westerberg30f3a6a2016-02-08 17:14:31 +0200121 { /* LPSS_BSW_SSP */
122 .offset = 0x400,
123 .reg_general = 0x08,
124 .reg_ssp = 0x0c,
125 .reg_cs_ctrl = 0x18,
126 .reg_capabilities = -1,
127 .rx_threshold = 64,
128 .tx_threshold_lo = 160,
129 .tx_threshold_hi = 224,
130 .cs_sel_shift = 2,
131 .cs_sel_mask = 1 << 2,
132 .cs_num = 2,
133 },
Jarkko Nikula34cadd92015-07-30 16:30:07 +0300134 { /* LPSS_SPT_SSP */
135 .offset = 0x200,
136 .reg_general = -1,
137 .reg_ssp = 0x20,
138 .reg_cs_ctrl = 0x24,
Jarkko Nikula66ec2462016-04-26 10:08:26 +0300139 .reg_capabilities = -1,
Jarkko Nikula34cadd92015-07-30 16:30:07 +0300140 .rx_threshold = 1,
141 .tx_threshold_lo = 32,
142 .tx_threshold_hi = 56,
143 },
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +0200144 { /* LPSS_BXT_SSP */
145 .offset = 0x200,
146 .reg_general = -1,
147 .reg_ssp = 0x20,
148 .reg_cs_ctrl = 0x24,
149 .reg_capabilities = 0xfc,
150 .rx_threshold = 1,
151 .tx_threshold_lo = 16,
152 .tx_threshold_hi = 48,
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200153 .cs_sel_shift = 8,
154 .cs_sel_mask = 3 << 8,
Evan Green6eefaee2020-04-27 16:32:48 -0700155 .cs_clk_stays_gated = true,
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +0200156 },
Jarkko Nikulafc0b2ac2017-05-30 17:31:21 +0300157 { /* LPSS_CNL_SSP */
158 .offset = 0x200,
159 .reg_general = -1,
160 .reg_ssp = 0x20,
161 .reg_cs_ctrl = 0x24,
162 .reg_capabilities = 0xfc,
163 .rx_threshold = 1,
164 .tx_threshold_lo = 32,
165 .tx_threshold_hi = 56,
166 .cs_sel_shift = 8,
167 .cs_sel_mask = 3 << 8,
Evan Green683f65d2020-02-11 14:37:00 -0800168 .cs_clk_stays_gated = true,
Jarkko Nikulafc0b2ac2017-05-30 17:31:21 +0300169 },
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300170};
171
172static inline const struct lpss_config
173*lpss_get_config(const struct driver_data *drv_data)
174{
175 return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
176}
177
Mika Westerberga0d26422013-01-22 12:26:32 +0200178static bool is_lpss_ssp(const struct driver_data *drv_data)
179{
Jarkko Nikula03fbf482015-06-04 16:55:10 +0300180 switch (drv_data->ssp_type) {
181 case LPSS_LPT_SSP:
182 case LPSS_BYT_SSP:
Mika Westerberg30f3a6a2016-02-08 17:14:31 +0200183 case LPSS_BSW_SSP:
Jarkko Nikula34cadd92015-07-30 16:30:07 +0300184 case LPSS_SPT_SSP:
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +0200185 case LPSS_BXT_SSP:
Jarkko Nikulafc0b2ac2017-05-30 17:31:21 +0300186 case LPSS_CNL_SSP:
Jarkko Nikula03fbf482015-06-04 16:55:10 +0300187 return true;
188 default:
189 return false;
190 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200191}
192
Weike Chene5262d02014-11-26 02:35:10 -0800193static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
194{
195 return drv_data->ssp_type == QUARK_X1000_SSP;
196}
197
Andy Shevchenko41c98842020-02-27 18:25:56 +0200198static bool is_mmp2_ssp(const struct driver_data *drv_data)
199{
200 return drv_data->ssp_type == MMP2_SSP;
201}
202
Weike Chen4fdb2422014-10-08 08:50:22 -0700203static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
204{
205 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800206 case QUARK_X1000_SSP:
207 return QUARK_X1000_SSCR1_CHANGE_MASK;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300208 case CE4100_SSP:
209 return CE4100_SSCR1_CHANGE_MASK;
Weike Chen4fdb2422014-10-08 08:50:22 -0700210 default:
211 return SSCR1_CHANGE_MASK;
212 }
213}
214
215static u32
216pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
217{
218 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800219 case QUARK_X1000_SSP:
220 return RX_THRESH_QUARK_X1000_DFLT;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300221 case CE4100_SSP:
222 return RX_THRESH_CE4100_DFLT;
Weike Chen4fdb2422014-10-08 08:50:22 -0700223 default:
224 return RX_THRESH_DFLT;
225 }
226}
227
228static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
229{
Weike Chen4fdb2422014-10-08 08:50:22 -0700230 u32 mask;
231
232 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800233 case QUARK_X1000_SSP:
234 mask = QUARK_X1000_SSSR_TFL_MASK;
235 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300236 case CE4100_SSP:
237 mask = CE4100_SSSR_TFL_MASK;
238 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700239 default:
240 mask = SSSR_TFL_MASK;
241 break;
242 }
243
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200244 return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
Weike Chen4fdb2422014-10-08 08:50:22 -0700245}
246
247static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
248 u32 *sccr1_reg)
249{
250 u32 mask;
251
252 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800253 case QUARK_X1000_SSP:
254 mask = QUARK_X1000_SSCR1_RFT;
255 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300256 case CE4100_SSP:
257 mask = CE4100_SSCR1_RFT;
258 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700259 default:
260 mask = SSCR1_RFT;
261 break;
262 }
263 *sccr1_reg &= ~mask;
264}
265
266static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
267 u32 *sccr1_reg, u32 threshold)
268{
269 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800270 case QUARK_X1000_SSP:
271 *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
272 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300273 case CE4100_SSP:
274 *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
275 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700276 default:
277 *sccr1_reg |= SSCR1_RxTresh(threshold);
278 break;
279 }
280}
281
282static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
283 u32 clk_div, u8 bits)
284{
285 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800286 case QUARK_X1000_SSP:
287 return clk_div
288 | QUARK_X1000_SSCR0_Motorola
289 | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
290 | SSCR0_SSE;
Weike Chen4fdb2422014-10-08 08:50:22 -0700291 default:
292 return clk_div
293 | SSCR0_Motorola
294 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
295 | SSCR0_SSE
296 | (bits > 16 ? SSCR0_EDSS : 0);
297 }
298}
299
Mika Westerberga0d26422013-01-22 12:26:32 +0200300/*
301 * Read and write LPSS SSP private registers. Caller must first check that
302 * is_lpss_ssp() returns true before these can be called.
303 */
304static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
305{
306 WARN_ON(!drv_data->lpss_base);
307 return readl(drv_data->lpss_base + offset);
308}
309
310static void __lpss_ssp_write_priv(struct driver_data *drv_data,
311 unsigned offset, u32 value)
312{
313 WARN_ON(!drv_data->lpss_base);
314 writel(value, drv_data->lpss_base + offset);
315}
316
317/*
318 * lpss_ssp_setup - perform LPSS SSP specific setup
319 * @drv_data: pointer to the driver private data
320 *
321 * Perform LPSS SSP specific setup. This function must be called first if
322 * one is going to use LPSS SSP private registers.
323 */
324static void lpss_ssp_setup(struct driver_data *drv_data)
325{
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300326 const struct lpss_config *config;
327 u32 value;
Mika Westerberga0d26422013-01-22 12:26:32 +0200328
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300329 config = lpss_get_config(drv_data);
Andy Shevchenko9e43c9a82021-04-23 21:24:29 +0300330 drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset;
Mika Westerberga0d26422013-01-22 12:26:32 +0200331
332 /* Enable software chip select control */
Jarkko Nikula0e897212015-10-22 16:44:42 +0300333 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
Jarkko Nikula624ea722015-10-28 15:13:39 +0200334 value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
335 value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300336 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
Mika Westerberg0054e282013-03-05 12:05:17 +0200337
338 /* Enable multiblock DMA transfers */
Lubomir Rintel51eea522019-01-16 16:13:31 +0100339 if (drv_data->controller_info->enable_dma) {
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300340 __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
Mika Westerberg1de70612013-07-03 13:25:06 +0300341
Jarkko Nikula82ba2c22015-06-04 16:55:12 +0300342 if (config->reg_general >= 0) {
343 value = __lpss_ssp_read_priv(drv_data,
344 config->reg_general);
Jarkko Nikula624ea722015-10-28 15:13:39 +0200345 value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
Jarkko Nikula82ba2c22015-06-04 16:55:12 +0300346 __lpss_ssp_write_priv(drv_data,
347 config->reg_general, value);
348 }
Mika Westerberg1de70612013-07-03 13:25:06 +0300349 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200350}
351
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300352static void lpss_ssp_select_cs(struct spi_device *spi,
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200353 const struct lpss_config *config)
354{
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300355 struct driver_data *drv_data =
356 spi_controller_get_devdata(spi->controller);
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200357 u32 value, cs;
358
359 if (!config->cs_sel_mask)
360 return;
361
362 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
363
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300364 cs = spi->chip_select;
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200365 cs <<= config->cs_sel_shift;
366 if (cs != (value & config->cs_sel_mask)) {
367 /*
368 * When switching another chip select output active the
369 * output must be selected first and wait 2 ssp_clk cycles
370 * before changing state to active. Otherwise a short
371 * glitch will occur on the previous chip select since
372 * output select is latched but state control is not.
373 */
374 value &= ~config->cs_sel_mask;
375 value |= cs;
376 __lpss_ssp_write_priv(drv_data,
377 config->reg_cs_ctrl, value);
378 ndelay(1000000000 /
Lubomir Rintel51eea522019-01-16 16:13:31 +0100379 (drv_data->controller->max_speed_hz / 2));
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200380 }
381}
382
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300383static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
Mika Westerberga0d26422013-01-22 12:26:32 +0200384{
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300385 struct driver_data *drv_data =
386 spi_controller_get_devdata(spi->controller);
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300387 const struct lpss_config *config;
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200388 u32 value;
Mika Westerberga0d26422013-01-22 12:26:32 +0200389
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300390 config = lpss_get_config(drv_data);
391
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200392 if (enable)
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300393 lpss_ssp_select_cs(spi, config);
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200394
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300395 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200396 if (enable)
Jarkko Nikula624ea722015-10-28 15:13:39 +0200397 value &= ~LPSS_CS_CONTROL_CS_HIGH;
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200398 else
Jarkko Nikula624ea722015-10-28 15:13:39 +0200399 value |= LPSS_CS_CONTROL_CS_HIGH;
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300400 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
Evan Green683f65d2020-02-11 14:37:00 -0800401 if (config->cs_clk_stays_gated) {
402 u32 clkgate;
403
404 /*
405 * Changing CS alone when dynamic clock gating is on won't
406 * actually flip CS at that time. This ruins SPI transfers
407 * that specify delays, or have no data. Toggle the clock mode
408 * to force on briefly to poke the CS pin to move.
409 */
410 clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
411 value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
412 LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
413
414 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
415 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
416 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200417}
418
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300419static void cs_assert(struct spi_device *spi)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700420{
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300421 struct chip_data *chip = spi_get_ctldata(spi);
422 struct driver_data *drv_data =
423 spi_controller_get_devdata(spi->controller);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700424
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800425 if (drv_data->ssp_type == CE4100_SSP) {
Jarkko Nikula96579a42016-09-07 17:04:07 +0300426 pxa2xx_spi_write(drv_data, SSSR, chip->frm);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800427 return;
428 }
429
Eric Miaoa7bb3902009-04-06 19:00:54 -0700430 if (chip->cs_control) {
431 chip->cs_control(PXA2XX_CS_ASSERT);
432 return;
433 }
434
Jan Kiszkac18d9252017-08-03 13:40:32 +0200435 if (chip->gpiod_cs) {
436 gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200437 return;
438 }
439
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200440 if (is_lpss_ssp(drv_data))
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300441 lpss_ssp_cs_control(spi, true);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700442}
443
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300444static void cs_deassert(struct spi_device *spi)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700445{
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300446 struct chip_data *chip = spi_get_ctldata(spi);
447 struct driver_data *drv_data =
448 spi_controller_get_devdata(spi->controller);
Jarkko Nikula104e51a2018-02-09 16:31:07 +0200449 unsigned long timeout;
Eric Miaoa7bb3902009-04-06 19:00:54 -0700450
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800451 if (drv_data->ssp_type == CE4100_SSP)
452 return;
453
Jarkko Nikula104e51a2018-02-09 16:31:07 +0200454 /* Wait until SSP becomes idle before deasserting the CS */
455 timeout = jiffies + msecs_to_jiffies(10);
456 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
457 !time_after(jiffies, timeout))
458 cpu_relax();
459
Eric Miaoa7bb3902009-04-06 19:00:54 -0700460 if (chip->cs_control) {
Daniel Ribeiro2b2562d2009-04-08 22:48:03 -0300461 chip->cs_control(PXA2XX_CS_DEASSERT);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700462 return;
463 }
464
Jan Kiszkac18d9252017-08-03 13:40:32 +0200465 if (chip->gpiod_cs) {
466 gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200467 return;
468 }
469
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200470 if (is_lpss_ssp(drv_data))
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300471 lpss_ssp_cs_control(spi, false);
472}
473
474static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
475{
476 if (level)
477 cs_deassert(spi);
478 else
479 cs_assert(spi);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700480}
481
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200482int pxa2xx_spi_flush(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800483{
484 unsigned long limit = loops_per_jiffy << 1;
485
Stephen Streete0c99052006-03-07 23:53:24 -0800486 do {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200487 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
488 pxa2xx_spi_read(drv_data, SSDR);
489 } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800490 write_SSSR_CS(drv_data, SSSR_ROR);
Stephen Streete0c99052006-03-07 23:53:24 -0800491
492 return limit;
493}
494
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100495static void pxa2xx_spi_off(struct driver_data *drv_data)
496{
Andy Shevchenko41c98842020-02-27 18:25:56 +0200497 /* On MMP, disabling SSE seems to corrupt the Rx FIFO */
498 if (is_mmp2_ssp(drv_data))
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100499 return;
500
501 pxa2xx_spi_write(drv_data, SSCR0,
502 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
503}
504
Stephen Street8d94cc52006-12-10 02:18:54 -0800505static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800506{
Stephen Street9708c122006-03-28 14:05:23 -0800507 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800508
Weike Chen4fdb2422014-10-08 08:50:22 -0700509 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800510 || (drv_data->tx == drv_data->tx_end))
511 return 0;
512
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200513 pxa2xx_spi_write(drv_data, SSDR, 0);
Stephen Street8d94cc52006-12-10 02:18:54 -0800514 drv_data->tx += n_bytes;
515
516 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800517}
518
Stephen Street8d94cc52006-12-10 02:18:54 -0800519static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800520{
Stephen Street9708c122006-03-28 14:05:23 -0800521 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800522
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200523 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
524 && (drv_data->rx < drv_data->rx_end)) {
525 pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800526 drv_data->rx += n_bytes;
527 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800528
529 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800530}
531
Stephen Street8d94cc52006-12-10 02:18:54 -0800532static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800533{
Weike Chen4fdb2422014-10-08 08:50:22 -0700534 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800535 || (drv_data->tx == drv_data->tx_end))
536 return 0;
537
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200538 pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800539 ++drv_data->tx;
540
541 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800542}
543
Stephen Street8d94cc52006-12-10 02:18:54 -0800544static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800545{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200546 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
547 && (drv_data->rx < drv_data->rx_end)) {
548 *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800549 ++drv_data->rx;
550 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800551
552 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800553}
554
Stephen Street8d94cc52006-12-10 02:18:54 -0800555static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800556{
Weike Chen4fdb2422014-10-08 08:50:22 -0700557 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800558 || (drv_data->tx == drv_data->tx_end))
559 return 0;
560
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200561 pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800562 drv_data->tx += 2;
563
564 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800565}
566
Stephen Street8d94cc52006-12-10 02:18:54 -0800567static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800568{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200569 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
570 && (drv_data->rx < drv_data->rx_end)) {
571 *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800572 drv_data->rx += 2;
573 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800574
575 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800576}
Stephen Street8d94cc52006-12-10 02:18:54 -0800577
578static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800579{
Weike Chen4fdb2422014-10-08 08:50:22 -0700580 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800581 || (drv_data->tx == drv_data->tx_end))
582 return 0;
583
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200584 pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800585 drv_data->tx += 4;
586
587 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800588}
589
Stephen Street8d94cc52006-12-10 02:18:54 -0800590static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800591{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200592 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
593 && (drv_data->rx < drv_data->rx_end)) {
594 *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800595 drv_data->rx += 4;
596 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800597
598 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800599}
600
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800601static void reset_sccr1(struct driver_data *drv_data)
602{
Jarkko Nikula96579a42016-09-07 17:04:07 +0300603 struct chip_data *chip =
Lubomir Rintel51eea522019-01-16 16:13:31 +0100604 spi_get_ctldata(drv_data->controller->cur_msg->spi);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800605 u32 sccr1_reg;
606
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200607 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
Andy Shevchenko152bc192016-07-06 12:08:11 +0300608 switch (drv_data->ssp_type) {
609 case QUARK_X1000_SSP:
610 sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
611 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300612 case CE4100_SSP:
613 sccr1_reg &= ~CE4100_SSCR1_RFT;
614 break;
Andy Shevchenko152bc192016-07-06 12:08:11 +0300615 default:
616 sccr1_reg &= ~SSCR1_RFT;
617 break;
618 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800619 sccr1_reg |= chip->threshold;
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200620 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800621}
622
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300623static void int_stop_and_reset(struct driver_data *drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800624{
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300625 /* Clear and disable interrupts */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800626 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800627 reset_sccr1(drv_data);
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300628 if (pxa25x_ssp_comp(drv_data))
629 return;
630
631 pxa2xx_spi_write(drv_data, SSTO, 0);
632}
633
Andy Shevchenko4761d2e2021-05-10 15:41:28 +0300634static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300635{
636 int_stop_and_reset(drv_data);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200637 pxa2xx_spi_flush(drv_data);
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100638 pxa2xx_spi_off(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800639
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300640 dev_err(drv_data->ssp->dev, "%s\n", msg);
Stephen Street8d94cc52006-12-10 02:18:54 -0800641
Andy Shevchenko4761d2e2021-05-10 15:41:28 +0300642 drv_data->controller->cur_msg->status = err;
Lubomir Rintel51eea522019-01-16 16:13:31 +0100643 spi_finalize_current_transfer(drv_data->controller);
Stephen Street8d94cc52006-12-10 02:18:54 -0800644}
645
646static void int_transfer_complete(struct driver_data *drv_data)
647{
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300648 int_stop_and_reset(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800649
Lubomir Rintel51eea522019-01-16 16:13:31 +0100650 spi_finalize_current_transfer(drv_data->controller);
Stephen Street8d94cc52006-12-10 02:18:54 -0800651}
652
Stephen Streete0c99052006-03-07 23:53:24 -0800653static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
654{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200655 u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
656 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
Stephen Street8d94cc52006-12-10 02:18:54 -0800657
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200658 u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
Stephen Streete0c99052006-03-07 23:53:24 -0800659
Stephen Street8d94cc52006-12-10 02:18:54 -0800660 if (irq_status & SSSR_ROR) {
Andy Shevchenko4761d2e2021-05-10 15:41:28 +0300661 int_error_stop(drv_data, "interrupt_transfer: fifo overrun", -EIO);
Stephen Street8d94cc52006-12-10 02:18:54 -0800662 return IRQ_HANDLED;
663 }
Stephen Streete0c99052006-03-07 23:53:24 -0800664
Lubomir Rintelec93cb62018-11-13 11:22:25 +0100665 if (irq_status & SSSR_TUR) {
Andy Shevchenko4761d2e2021-05-10 15:41:28 +0300666 int_error_stop(drv_data, "interrupt_transfer: fifo underrun", -EIO);
Lubomir Rintelec93cb62018-11-13 11:22:25 +0100667 return IRQ_HANDLED;
668 }
669
Stephen Street8d94cc52006-12-10 02:18:54 -0800670 if (irq_status & SSSR_TINT) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200671 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
Stephen Street8d94cc52006-12-10 02:18:54 -0800672 if (drv_data->read(drv_data)) {
673 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800674 return IRQ_HANDLED;
675 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800676 }
Stephen Streete0c99052006-03-07 23:53:24 -0800677
Stephen Street8d94cc52006-12-10 02:18:54 -0800678 /* Drain rx fifo, Fill tx fifo and prevent overruns */
679 do {
680 if (drv_data->read(drv_data)) {
681 int_transfer_complete(drv_data);
682 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800683 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800684 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800685
Stephen Street8d94cc52006-12-10 02:18:54 -0800686 if (drv_data->read(drv_data)) {
687 int_transfer_complete(drv_data);
688 return IRQ_HANDLED;
689 }
Stephen Streete0c99052006-03-07 23:53:24 -0800690
Stephen Street8d94cc52006-12-10 02:18:54 -0800691 if (drv_data->tx == drv_data->tx_end) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800692 u32 bytes_left;
693 u32 sccr1_reg;
694
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200695 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800696 sccr1_reg &= ~SSCR1_TIE;
697
698 /*
699 * PXA25x_SSP has no timeout, set up rx threshould for the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300700 * remaining RX bytes.
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800701 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800702 if (pxa25x_ssp_comp(drv_data)) {
Weike Chen4fdb2422014-10-08 08:50:22 -0700703 u32 rx_thre;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800704
Weike Chen4fdb2422014-10-08 08:50:22 -0700705 pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800706
707 bytes_left = drv_data->rx_end - drv_data->rx;
708 switch (drv_data->n_bytes) {
709 case 4:
Gustavo A. R. Silva2c183372018-10-03 17:55:22 +0200710 bytes_left >>= 2;
711 break;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800712 case 2:
713 bytes_left >>= 1;
Gustavo A. R. Silva2c183372018-10-03 17:55:22 +0200714 break;
Stephen Street8d94cc52006-12-10 02:18:54 -0800715 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800716
Weike Chen4fdb2422014-10-08 08:50:22 -0700717 rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
718 if (rx_thre > bytes_left)
719 rx_thre = bytes_left;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800720
Weike Chen4fdb2422014-10-08 08:50:22 -0700721 pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
Stephen Streete0c99052006-03-07 23:53:24 -0800722 }
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200723 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800724 }
725
Stephen Street5daa3ba2006-05-20 15:00:19 -0700726 /* We did something */
727 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800728}
729
Jan Kiszkab0312482017-01-16 19:44:54 +0100730static void handle_bad_msg(struct driver_data *drv_data)
731{
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100732 pxa2xx_spi_off(drv_data);
Jan Kiszkab0312482017-01-16 19:44:54 +0100733 pxa2xx_spi_write(drv_data, SSCR1,
734 pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
735 if (!pxa25x_ssp_comp(drv_data))
736 pxa2xx_spi_write(drv_data, SSTO, 0);
737 write_SSSR_CS(drv_data, drv_data->clear_sr);
738
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300739 dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
Jan Kiszkab0312482017-01-16 19:44:54 +0100740}
741
David Howells7d12e782006-10-05 14:55:46 +0100742static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800743{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400744 struct driver_data *drv_data = dev_id;
Mika Westerberg7d94a502013-01-22 12:26:30 +0200745 u32 sccr1_reg;
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800746 u32 mask = drv_data->mask_sr;
747 u32 status;
748
Mika Westerberg7d94a502013-01-22 12:26:30 +0200749 /*
750 * The IRQ might be shared with other peripherals so we must first
751 * check that are we RPM suspended or not. If we are we assume that
752 * the IRQ was not for us (we shouldn't be RPM suspended when the
753 * interrupt is enabled).
754 */
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300755 if (pm_runtime_suspended(drv_data->ssp->dev))
Mika Westerberg7d94a502013-01-22 12:26:30 +0200756 return IRQ_NONE;
757
Mika Westerberg269e4a42013-09-04 13:37:43 +0300758 /*
759 * If the device is not yet in RPM suspended state and we get an
760 * interrupt that is meant for another device, check if status bits
761 * are all set to one. That means that the device is already
762 * powered off.
763 */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200764 status = pxa2xx_spi_read(drv_data, SSSR);
Mika Westerberg269e4a42013-09-04 13:37:43 +0300765 if (status == ~0)
766 return IRQ_NONE;
767
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200768 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800769
770 /* Ignore possible writes if we don't need to write */
771 if (!(sccr1_reg & SSCR1_TIE))
772 mask &= ~SSSR_TFS;
773
Tan, Jui Nee02bc9332015-09-01 10:22:51 +0800774 /* Ignore RX timeout interrupt if it is disabled */
775 if (!(sccr1_reg & SSCR1_TINTE))
776 mask &= ~SSSR_TINT;
777
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800778 if (!(status & mask))
779 return IRQ_NONE;
Stephen Streete0c99052006-03-07 23:53:24 -0800780
Jan Kiszkae51e9b92017-01-21 10:06:38 +0100781 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
782 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
783
Lubomir Rintel51eea522019-01-16 16:13:31 +0100784 if (!drv_data->controller->cur_msg) {
Jan Kiszkab0312482017-01-16 19:44:54 +0100785 handle_bad_msg(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800786 /* Never fail */
787 return IRQ_HANDLED;
788 }
789
790 return drv_data->transfer_handler(drv_data);
791}
792
Weike Chene5262d02014-11-26 02:35:10 -0800793/*
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200794 * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
795 * input frequency by fractions of 2^24. It also has a divider by 5.
796 *
797 * There are formulas to get baud rate value for given input frequency and
798 * divider parameters, such as DDS_CLK_RATE and SCR:
799 *
800 * Fsys = 200MHz
801 *
802 * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
803 * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
804 *
805 * DDS_CLK_RATE either 2^n or 2^n / 5.
806 * SCR is in range 0 .. 255
807 *
808 * Divisor = 5^i * 2^j * 2 * k
809 * i = [0, 1] i = 1 iff j = 0 or j > 3
810 * j = [0, 23] j = 0 iff i = 1
811 * k = [1, 256]
812 * Special case: j = 0, i = 1: Divisor = 2 / 5
813 *
814 * Accordingly to the specification the recommended values for DDS_CLK_RATE
815 * are:
816 * Case 1: 2^n, n = [0, 23]
817 * Case 2: 2^24 * 2 / 5 (0x666666)
818 * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
819 *
820 * In all cases the lowest possible value is better.
821 *
822 * The function calculates parameters for all cases and chooses the one closest
823 * to the asked baud rate.
Weike Chene5262d02014-11-26 02:35:10 -0800824 */
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200825static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
Weike Chene5262d02014-11-26 02:35:10 -0800826{
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200827 unsigned long xtal = 200000000;
828 unsigned long fref = xtal / 2; /* mandatory division by 2,
829 see (2) */
830 /* case 3 */
831 unsigned long fref1 = fref / 2; /* case 1 */
832 unsigned long fref2 = fref * 2 / 5; /* case 2 */
833 unsigned long scale;
834 unsigned long q, q1, q2;
835 long r, r1, r2;
836 u32 mul;
Weike Chene5262d02014-11-26 02:35:10 -0800837
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200838 /* Case 1 */
839
840 /* Set initial value for DDS_CLK_RATE */
841 mul = (1 << 24) >> 1;
842
843 /* Calculate initial quot */
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300844 q1 = DIV_ROUND_UP(fref1, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200845
846 /* Scale q1 if it's too big */
847 if (q1 > 256) {
848 /* Scale q1 to range [1, 512] */
849 scale = fls_long(q1 - 1);
850 if (scale > 9) {
851 q1 >>= scale - 9;
852 mul >>= scale - 9;
853 }
854
855 /* Round the result if we have a remainder */
856 q1 += q1 & 1;
857 }
858
859 /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
860 scale = __ffs(q1);
861 q1 >>= scale;
862 mul >>= scale;
863
864 /* Get the remainder */
865 r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
866
867 /* Case 2 */
868
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300869 q2 = DIV_ROUND_UP(fref2, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200870 r2 = abs(fref2 / q2 - rate);
871
872 /*
873 * Choose the best between two: less remainder we have the better. We
874 * can't go case 2 if q2 is greater than 256 since SCR register can
875 * hold only values 0 .. 255.
876 */
877 if (r2 >= r1 || q2 > 256) {
878 /* case 1 is better */
879 r = r1;
880 q = q1;
881 } else {
882 /* case 2 is better */
883 r = r2;
884 q = q2;
885 mul = (1 << 24) * 2 / 5;
886 }
887
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300888 /* Check case 3 only if the divisor is big enough */
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200889 if (fref / rate >= 80) {
890 u64 fssp;
891 u32 m;
892
893 /* Calculate initial quot */
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300894 q1 = DIV_ROUND_UP(fref, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200895 m = (1 << 24) / q1;
896
897 /* Get the remainder */
898 fssp = (u64)fref * m;
899 do_div(fssp, 1 << 24);
900 r1 = abs(fssp - rate);
901
902 /* Choose this one if it suits better */
903 if (r1 < r) {
904 /* case 3 is better */
905 q = 1;
906 mul = m;
Weike Chene5262d02014-11-26 02:35:10 -0800907 }
908 }
909
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200910 *dds = mul;
911 return q - 1;
Weike Chene5262d02014-11-26 02:35:10 -0800912}
913
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200914static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
eric miao2f1a74e2007-11-21 18:50:53 +0800915{
Lubomir Rintel51eea522019-01-16 16:13:31 +0100916 unsigned long ssp_clk = drv_data->controller->max_speed_hz;
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200917 const struct ssp_device *ssp = drv_data->ssp;
918
919 rate = min_t(int, ssp_clk, rate);
eric miao2f1a74e2007-11-21 18:50:53 +0800920
Flavio Suligoi29f21332019-04-12 09:32:19 +0200921 /*
922 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
923 * that the SSP transmission rate can be greater than the device rate
924 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800925 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
Flavio Suligoi29f21332019-04-12 09:32:19 +0200926 return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
eric miao2f1a74e2007-11-21 18:50:53 +0800927 else
Flavio Suligoi29f21332019-04-12 09:32:19 +0200928 return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff;
eric miao2f1a74e2007-11-21 18:50:53 +0800929}
930
Weike Chene5262d02014-11-26 02:35:10 -0800931static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
Andy Shevchenkod2c2f6a2015-10-22 16:44:40 +0300932 int rate)
Weike Chene5262d02014-11-26 02:35:10 -0800933{
Jarkko Nikula96579a42016-09-07 17:04:07 +0300934 struct chip_data *chip =
Lubomir Rintel51eea522019-01-16 16:13:31 +0100935 spi_get_ctldata(drv_data->controller->cur_msg->spi);
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200936 unsigned int clk_div;
Weike Chene5262d02014-11-26 02:35:10 -0800937
938 switch (drv_data->ssp_type) {
939 case QUARK_X1000_SSP:
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200940 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300941 break;
Weike Chene5262d02014-11-26 02:35:10 -0800942 default:
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200943 clk_div = ssp_get_clk_div(drv_data, rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300944 break;
Weike Chene5262d02014-11-26 02:35:10 -0800945 }
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200946 return clk_div << 8;
Weike Chene5262d02014-11-26 02:35:10 -0800947}
948
Lubomir Rintel51eea522019-01-16 16:13:31 +0100949static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300950 struct spi_device *spi,
951 struct spi_transfer *xfer)
952{
953 struct chip_data *chip = spi_get_ctldata(spi);
954
955 return chip->enable_dma &&
956 xfer->len <= MAX_DMA_LEN &&
957 xfer->len >= chip->dma_burst_size;
958}
959
Lubomir Rintel51eea522019-01-16 16:13:31 +0100960static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
kbuild test robot71293a62018-04-18 03:53:23 +0800961 struct spi_device *spi,
962 struct spi_transfer *transfer)
Stephen Streete0c99052006-03-07 23:53:24 -0800963{
Lubomir Rintel51eea522019-01-16 16:13:31 +0100964 struct driver_data *drv_data = spi_controller_get_devdata(controller);
965 struct spi_message *message = controller->cur_msg;
Jarkko Nikula20f4c372019-03-29 15:00:45 +0200966 struct chip_data *chip = spi_get_ctldata(spi);
Jarkko Nikula96579a42016-09-07 17:04:07 +0300967 u32 dma_thresh = chip->dma_threshold;
968 u32 dma_burst = chip->dma_burst_size;
969 u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
Jarkko Nikulabffc9672016-09-07 17:04:05 +0300970 u32 clk_div;
971 u8 bits;
972 u32 speed;
Stephen Street9708c122006-03-28 14:05:23 -0800973 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800974 u32 cr1;
Andy Shevchenko7d1f1bf2016-03-24 15:35:42 +0200975 int err;
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300976 int dma_mapped;
Stephen Streete0c99052006-03-07 23:53:24 -0800977
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200978 /* Check if we can DMA this transfer */
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300979 if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
Ned Forrester7e964452008-09-13 02:33:18 -0700980
981 /* reject already-mapped transfers; PIO won't always work */
982 if (message->is_dma_mapped
983 || transfer->rx_dma || transfer->tx_dma) {
Jarkko Nikula748fbad2019-03-29 15:00:46 +0200984 dev_err(&spi->dev,
Jarkko Nikula8ae55af2018-04-17 17:20:01 +0300985 "Mapped transfer length of %u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700986 transfer->len, MAX_DMA_LEN);
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300987 return -EINVAL;
Ned Forrester7e964452008-09-13 02:33:18 -0700988 }
989
990 /* warn ... we force this to PIO mode */
Jarkko Nikula20f4c372019-03-29 15:00:45 +0200991 dev_warn_ratelimited(&spi->dev,
Jarkko Nikula8ae55af2018-04-17 17:20:01 +0300992 "DMA disabled for transfer length %ld greater than %d\n",
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300993 (long)transfer->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800994 }
995
Stephen Streete0c99052006-03-07 23:53:24 -0800996 /* Setup the transfer state based on the type of transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200997 if (pxa2xx_spi_flush(drv_data) == 0) {
Jarkko Nikula748fbad2019-03-29 15:00:46 +0200998 dev_err(&spi->dev, "Flush failed\n");
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300999 return -EIO;
Stephen Streete0c99052006-03-07 23:53:24 -08001000 }
Stephen Street9708c122006-03-28 14:05:23 -08001001 drv_data->n_bytes = chip->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -08001002 drv_data->tx = (void *)transfer->tx_buf;
1003 drv_data->tx_end = drv_data->tx + transfer->len;
1004 drv_data->rx = transfer->rx_buf;
1005 drv_data->rx_end = drv_data->rx + transfer->len;
Stephen Streete0c99052006-03-07 23:53:24 -08001006 drv_data->write = drv_data->tx ? chip->write : null_writer;
1007 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -08001008
1009 /* Change speed and bit per word on a per transfer */
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001010 bits = transfer->bits_per_word;
1011 speed = transfer->speed_hz;
Stephen Street9708c122006-03-28 14:05:23 -08001012
Andy Shevchenkod2c2f6a2015-10-22 16:44:40 +03001013 clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
Stephen Street9708c122006-03-28 14:05:23 -08001014
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001015 if (bits <= 8) {
1016 drv_data->n_bytes = 1;
1017 drv_data->read = drv_data->read != null_reader ?
1018 u8_reader : null_reader;
1019 drv_data->write = drv_data->write != null_writer ?
1020 u8_writer : null_writer;
1021 } else if (bits <= 16) {
1022 drv_data->n_bytes = 2;
1023 drv_data->read = drv_data->read != null_reader ?
1024 u16_reader : null_reader;
1025 drv_data->write = drv_data->write != null_writer ?
1026 u16_writer : null_writer;
1027 } else if (bits <= 32) {
1028 drv_data->n_bytes = 4;
1029 drv_data->read = drv_data->read != null_reader ?
1030 u32_reader : null_reader;
1031 drv_data->write = drv_data->write != null_writer ?
1032 u32_writer : null_writer;
Stephen Street9708c122006-03-28 14:05:23 -08001033 }
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001034 /*
1035 * if bits/word is changed in dma mode, then must check the
1036 * thresholds and burst also
1037 */
1038 if (chip->enable_dma) {
1039 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001040 spi,
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001041 bits, &dma_burst,
1042 &dma_thresh))
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001043 dev_warn_ratelimited(&spi->dev,
Jarkko Nikula8ae55af2018-04-17 17:20:01 +03001044 "DMA burst size reduced to match bits_per_word\n");
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001045 }
1046
Lubomir Rintel51eea522019-01-16 16:13:31 +01001047 dma_mapped = controller->can_dma &&
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001048 controller->can_dma(controller, spi, transfer) &&
Lubomir Rintel51eea522019-01-16 16:13:31 +01001049 controller->cur_msg_mapped;
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001050 if (dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -08001051
1052 /* Ensure we have the correct interrupt handler */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001053 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
Stephen Streete0c99052006-03-07 23:53:24 -08001054
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001055 err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1056 if (err)
1057 return err;
Stephen Streete0c99052006-03-07 23:53:24 -08001058
Stephen Street8d94cc52006-12-10 02:18:54 -08001059 /* Clear status and start DMA engine */
1060 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001061 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001062
1063 pxa2xx_spi_dma_start(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001064 } else {
1065 /* Ensure we have the correct interrupt handler */
1066 drv_data->transfer_handler = interrupt_transfer;
1067
Stephen Street8d94cc52006-12-10 02:18:54 -08001068 /* Clear status */
1069 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001070 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -08001071 }
1072
Jarkko Nikulaee036722016-01-26 15:33:21 +02001073 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1074 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1075 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001076 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
Lubomir Rintel51eea522019-01-16 16:13:31 +01001077 controller->max_speed_hz
Jarkko Nikulaee036722016-01-26 15:33:21 +02001078 / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001079 dma_mapped ? "DMA" : "PIO");
Jarkko Nikulaee036722016-01-26 15:33:21 +02001080 else
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001081 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
Lubomir Rintel51eea522019-01-16 16:13:31 +01001082 controller->max_speed_hz / 2
Jarkko Nikulaee036722016-01-26 15:33:21 +02001083 / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001084 dma_mapped ? "DMA" : "PIO");
Jarkko Nikulaee036722016-01-26 15:33:21 +02001085
Mika Westerberga0d26422013-01-22 12:26:32 +02001086 if (is_lpss_ssp(drv_data)) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001087 if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
1088 != chip->lpss_rx_threshold)
1089 pxa2xx_spi_write(drv_data, SSIRF,
1090 chip->lpss_rx_threshold);
1091 if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
1092 != chip->lpss_tx_threshold)
1093 pxa2xx_spi_write(drv_data, SSITF,
1094 chip->lpss_tx_threshold);
Mika Westerberga0d26422013-01-22 12:26:32 +02001095 }
1096
Weike Chene5262d02014-11-26 02:35:10 -08001097 if (is_quark_x1000_ssp(drv_data) &&
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001098 (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
1099 pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
Weike Chene5262d02014-11-26 02:35:10 -08001100
Stephen Street8d94cc52006-12-10 02:18:54 -08001101 /* see if we need to reload the config registers */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001102 if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1103 || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1104 != (cr1 & change_mask)) {
Ned Forresterb97c74b2008-02-23 15:23:40 -08001105 /* stop the SSP, and update the other bits */
Andy Shevchenko41c98842020-02-27 18:25:56 +02001106 if (!is_mmp2_ssp(drv_data))
Lubomir Rintel29d7e052020-01-18 10:40:31 +01001107 pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001108 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001109 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001110 /* first set CR1 without interrupt and service enables */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001111 pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001112 /* restart the SSP */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001113 pxa2xx_spi_write(drv_data, SSCR0, cr0);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001114
Stephen Street8d94cc52006-12-10 02:18:54 -08001115 } else {
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001116 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001117 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
Stephen Streete0c99052006-03-07 23:53:24 -08001118 }
Ned Forresterb97c74b2008-02-23 15:23:40 -08001119
Andy Shevchenko41c98842020-02-27 18:25:56 +02001120 if (is_mmp2_ssp(drv_data)) {
Lubomir Rintel82391852018-11-13 11:22:28 +01001121 u8 tx_level = (pxa2xx_spi_read(drv_data, SSSR)
1122 & SSSR_TFL_MASK) >> 8;
1123
1124 if (tx_level) {
1125 /* On MMP2, flipping SSE doesn't to empty TXFIFO. */
1126 dev_warn(&spi->dev, "%d bytes of garbage in TXFIFO!\n",
1127 tx_level);
1128 if (tx_level > transfer->len)
1129 tx_level = transfer->len;
1130 drv_data->tx += tx_level;
1131 }
1132 }
1133
Lubomir Rintel51eea522019-01-16 16:13:31 +01001134 if (spi_controller_is_slave(controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001135 while (drv_data->write(drv_data))
1136 ;
Lubomir Rintel77d33892018-11-13 11:22:27 +01001137 if (drv_data->gpiod_ready) {
1138 gpiod_set_value(drv_data->gpiod_ready, 1);
1139 udelay(1);
1140 gpiod_set_value(drv_data->gpiod_ready, 0);
1141 }
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001142 }
1143
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001144 /*
1145 * Release the data by enabling service requests and interrupts,
1146 * without changing any mode bits
1147 */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001148 pxa2xx_spi_write(drv_data, SSCR1, cr1);
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001149
1150 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -08001151}
1152
Lubomir Rintel51eea522019-01-16 16:13:31 +01001153static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001154{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001155 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001156
Andy Shevchenko4761d2e2021-05-10 15:41:28 +03001157 int_error_stop(drv_data, "transfer aborted", -EINTR);
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001158
1159 return 0;
1160}
1161
Lubomir Rintel51eea522019-01-16 16:13:31 +01001162static void pxa2xx_spi_handle_err(struct spi_controller *controller,
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001163 struct spi_message *msg)
Stephen Streete0c99052006-03-07 23:53:24 -08001164{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001165 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001166
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001167 /* Disable the SSP */
Lubomir Rintel29d7e052020-01-18 10:40:31 +01001168 pxa2xx_spi_off(drv_data);
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001169 /* Clear and disable interrupts and service requests */
1170 write_SSSR_CS(drv_data, drv_data->clear_sr);
1171 pxa2xx_spi_write(drv_data, SSCR1,
1172 pxa2xx_spi_read(drv_data, SSCR1)
1173 & ~(drv_data->int_cr1 | drv_data->dma_cr1));
1174 if (!pxa25x_ssp_comp(drv_data))
1175 pxa2xx_spi_write(drv_data, SSTO, 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001176
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001177 /*
1178 * Stop the DMA if running. Note DMA callback handler may have unset
1179 * the dma_running already, which is fine as stopping is not needed
1180 * then but we shouldn't rely this flag for anything else than
1181 * stopping. For instance to differentiate between PIO and DMA
1182 * transfers.
1183 */
1184 if (atomic_read(&drv_data->dma_running))
1185 pxa2xx_spi_dma_stop(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001186}
1187
Lubomir Rintel51eea522019-01-16 16:13:31 +01001188static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
Mika Westerberg7d94a502013-01-22 12:26:30 +02001189{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001190 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Mika Westerberg7d94a502013-01-22 12:26:30 +02001191
1192 /* Disable the SSP now */
Lubomir Rintel29d7e052020-01-18 10:40:31 +01001193 pxa2xx_spi_off(drv_data);
Mika Westerberg7d94a502013-01-22 12:26:30 +02001194
Mika Westerberg7d94a502013-01-22 12:26:30 +02001195 return 0;
1196}
1197
Eric Miaoa7bb3902009-04-06 19:00:54 -07001198static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1199 struct pxa2xx_spi_chip *chip_info)
1200{
Jarkko Nikula3cc7b0e2018-02-01 17:17:30 +02001201 struct driver_data *drv_data =
1202 spi_controller_get_devdata(spi->controller);
Jan Kiszkac18d9252017-08-03 13:40:32 +02001203 struct gpio_desc *gpiod;
Eric Miaoa7bb3902009-04-06 19:00:54 -07001204 int err = 0;
1205
Mika Westerberg99f499c2016-09-26 15:19:50 +03001206 if (chip == NULL)
1207 return 0;
1208
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001209 if (drv_data->cs_gpiods) {
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001210 gpiod = drv_data->cs_gpiods[spi->chip_select];
1211 if (gpiod) {
Jan Kiszkac18d9252017-08-03 13:40:32 +02001212 chip->gpiod_cs = gpiod;
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001213 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1214 gpiod_set_value(gpiod, chip->gpio_cs_inverted);
Mika Westerberg99f499c2016-09-26 15:19:50 +03001215 }
1216
1217 return 0;
1218 }
1219
1220 if (chip_info == NULL)
Eric Miaoa7bb3902009-04-06 19:00:54 -07001221 return 0;
1222
1223 /* NOTE: setup() can be called multiple times, possibly with
1224 * different chip_info, release previously requested GPIO
1225 */
Jan Kiszkac18d9252017-08-03 13:40:32 +02001226 if (chip->gpiod_cs) {
Mark Browna885eeb2017-12-22 16:15:36 +00001227 gpiod_put(chip->gpiod_cs);
Jan Kiszkac18d9252017-08-03 13:40:32 +02001228 chip->gpiod_cs = NULL;
1229 }
Eric Miaoa7bb3902009-04-06 19:00:54 -07001230
1231 /* If (*cs_control) is provided, ignore GPIO chip select */
1232 if (chip_info->cs_control) {
1233 chip->cs_control = chip_info->cs_control;
1234 return 0;
1235 }
1236
1237 if (gpio_is_valid(chip_info->gpio_cs)) {
1238 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1239 if (err) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001240 dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1241 chip_info->gpio_cs);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001242 return err;
1243 }
1244
Jan Kiszkac18d9252017-08-03 13:40:32 +02001245 gpiod = gpio_to_desc(chip_info->gpio_cs);
1246 chip->gpiod_cs = gpiod;
Eric Miaoa7bb3902009-04-06 19:00:54 -07001247 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1248
Jan Kiszkac18d9252017-08-03 13:40:32 +02001249 err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001250 }
1251
1252 return err;
1253}
1254
Stephen Streete0c99052006-03-07 23:53:24 -08001255static int setup(struct spi_device *spi)
1256{
Jarkko Nikulabffc9672016-09-07 17:04:05 +03001257 struct pxa2xx_spi_chip *chip_info;
Stephen Streete0c99052006-03-07 23:53:24 -08001258 struct chip_data *chip;
Jarkko Nikuladccf7362015-06-04 16:55:11 +03001259 const struct lpss_config *config;
Jarkko Nikula3cc7b0e2018-02-01 17:17:30 +02001260 struct driver_data *drv_data =
1261 spi_controller_get_devdata(spi->controller);
Mika Westerberga0d26422013-01-22 12:26:32 +02001262 uint tx_thres, tx_hi_thres, rx_thres;
1263
Weike Chene5262d02014-11-26 02:35:10 -08001264 switch (drv_data->ssp_type) {
1265 case QUARK_X1000_SSP:
1266 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1267 tx_hi_thres = 0;
1268 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1269 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001270 case CE4100_SSP:
1271 tx_thres = TX_THRESH_CE4100_DFLT;
1272 tx_hi_thres = 0;
1273 rx_thres = RX_THRESH_CE4100_DFLT;
1274 break;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001275 case LPSS_LPT_SSP:
1276 case LPSS_BYT_SSP:
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001277 case LPSS_BSW_SSP:
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001278 case LPSS_SPT_SSP:
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +02001279 case LPSS_BXT_SSP:
Jarkko Nikulafc0b2ac2017-05-30 17:31:21 +03001280 case LPSS_CNL_SSP:
Jarkko Nikuladccf7362015-06-04 16:55:11 +03001281 config = lpss_get_config(drv_data);
1282 tx_thres = config->tx_threshold_lo;
1283 tx_hi_thres = config->tx_threshold_hi;
1284 rx_thres = config->rx_threshold;
Weike Chene5262d02014-11-26 02:35:10 -08001285 break;
1286 default:
Mika Westerberga0d26422013-01-22 12:26:32 +02001287 tx_hi_thres = 0;
Lubomir Rintel51eea522019-01-16 16:13:31 +01001288 if (spi_controller_is_slave(drv_data->controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001289 tx_thres = 1;
1290 rx_thres = 2;
1291 } else {
1292 tx_thres = TX_THRESH_DFLT;
1293 rx_thres = RX_THRESH_DFLT;
1294 }
Weike Chene5262d02014-11-26 02:35:10 -08001295 break;
Mika Westerberga0d26422013-01-22 12:26:32 +02001296 }
Stephen Streete0c99052006-03-07 23:53:24 -08001297
Stephen Street8d94cc52006-12-10 02:18:54 -08001298 /* Only alloc on first setup */
Stephen Streete0c99052006-03-07 23:53:24 -08001299 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -08001300 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -08001301 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001302 if (!chip)
Stephen Streete0c99052006-03-07 23:53:24 -08001303 return -ENOMEM;
1304
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001305 if (drv_data->ssp_type == CE4100_SSP) {
1306 if (spi->chip_select > 4) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001307 dev_err(&spi->dev,
1308 "failed setup: cs number must not be > 4.\n");
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001309 kfree(chip);
1310 return -EINVAL;
1311 }
1312
1313 chip->frm = spi->chip_select;
Jan Kiszkac18d9252017-08-03 13:40:32 +02001314 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001315 chip->enable_dma = drv_data->controller_info->enable_dma;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001316 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001317 }
1318
Stephen Street8d94cc52006-12-10 02:18:54 -08001319 /* protocol drivers may change the chip settings, so...
1320 * if chip_info exists, use it */
1321 chip_info = spi->controller_data;
1322
Stephen Streete0c99052006-03-07 23:53:24 -08001323 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -08001324 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001325 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001326 if (chip_info->timeout)
1327 chip->timeout = chip_info->timeout;
1328 if (chip_info->tx_threshold)
1329 tx_thres = chip_info->tx_threshold;
Mika Westerberga0d26422013-01-22 12:26:32 +02001330 if (chip_info->tx_hi_threshold)
1331 tx_hi_thres = chip_info->tx_hi_threshold;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001332 if (chip_info->rx_threshold)
1333 rx_thres = chip_info->rx_threshold;
Stephen Streete0c99052006-03-07 23:53:24 -08001334 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001335 if (chip_info->enable_loopback)
1336 chip->cr1 = SSCR1_LBM;
1337 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001338 if (spi_controller_is_slave(drv_data->controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001339 chip->cr1 |= SSCR1_SCFR;
1340 chip->cr1 |= SSCR1_SCLKDIR;
1341 chip->cr1 |= SSCR1_SFRMDIR;
1342 chip->cr1 |= SSCR1_SPH;
1343 }
Stephen Streete0c99052006-03-07 23:53:24 -08001344
Mika Westerberga0d26422013-01-22 12:26:32 +02001345 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1346 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1347 | SSITF_TxHiThresh(tx_hi_thres);
1348
Stephen Street8d94cc52006-12-10 02:18:54 -08001349 /* set dma burst and threshold outside of chip_info path so that if
1350 * chip_info goes away after setting chip->enable_dma, the
1351 * burst and threshold can still respond to changes in bits_per_word */
1352 if (chip->enable_dma) {
1353 /* set up legal burst and threshold for dma */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001354 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1355 spi->bits_per_word,
Stephen Street8d94cc52006-12-10 02:18:54 -08001356 &chip->dma_burst_size,
1357 &chip->dma_threshold)) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001358 dev_warn(&spi->dev,
1359 "in setup: DMA burst size reduced to match bits_per_word\n");
Stephen Street8d94cc52006-12-10 02:18:54 -08001360 }
Andy Shevchenko000c6af2019-03-19 17:48:43 +02001361 dev_dbg(&spi->dev,
1362 "in setup: DMA burst size set to %u\n",
1363 chip->dma_burst_size);
Stephen Street8d94cc52006-12-10 02:18:54 -08001364 }
1365
Weike Chene5262d02014-11-26 02:35:10 -08001366 switch (drv_data->ssp_type) {
1367 case QUARK_X1000_SSP:
1368 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1369 & QUARK_X1000_SSCR1_RFT)
1370 | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1371 & QUARK_X1000_SSCR1_TFT);
1372 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001373 case CE4100_SSP:
1374 chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
1375 (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
1376 break;
Weike Chene5262d02014-11-26 02:35:10 -08001377 default:
1378 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1379 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1380 break;
1381 }
1382
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -08001383 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1384 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1385 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001386
Mika Westerbergb8331722013-01-22 12:26:31 +02001387 if (spi->mode & SPI_LOOP)
1388 chip->cr1 |= SSCR1_LBM;
1389
Stephen Streete0c99052006-03-07 23:53:24 -08001390 if (spi->bits_per_word <= 8) {
1391 chip->n_bytes = 1;
Stephen Streete0c99052006-03-07 23:53:24 -08001392 chip->read = u8_reader;
1393 chip->write = u8_writer;
1394 } else if (spi->bits_per_word <= 16) {
1395 chip->n_bytes = 2;
Stephen Streete0c99052006-03-07 23:53:24 -08001396 chip->read = u16_reader;
1397 chip->write = u16_writer;
1398 } else if (spi->bits_per_word <= 32) {
Stephen Streete0c99052006-03-07 23:53:24 -08001399 chip->n_bytes = 4;
Stephen Streete0c99052006-03-07 23:53:24 -08001400 chip->read = u32_reader;
1401 chip->write = u32_writer;
Stephen Streete0c99052006-03-07 23:53:24 -08001402 }
Stephen Streete0c99052006-03-07 23:53:24 -08001403
1404 spi_set_ctldata(spi, chip);
1405
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001406 if (drv_data->ssp_type == CE4100_SSP)
1407 return 0;
1408
Eric Miaoa7bb3902009-04-06 19:00:54 -07001409 return setup_cs(spi, chip, chip_info);
Stephen Streete0c99052006-03-07 23:53:24 -08001410}
1411
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001412static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001413{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001414 struct chip_data *chip = spi_get_ctldata(spi);
Jarkko Nikula3cc7b0e2018-02-01 17:17:30 +02001415 struct driver_data *drv_data =
1416 spi_controller_get_devdata(spi->controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001417
Daniel Ribeiro7348d822009-05-12 13:19:36 -07001418 if (!chip)
1419 return;
1420
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001421 if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
Jan Kiszkac18d9252017-08-03 13:40:32 +02001422 chip->gpiod_cs)
Mark Browna885eeb2017-12-22 16:15:36 +00001423 gpiod_put(chip->gpiod_cs);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001424
Stephen Streete0c99052006-03-07 23:53:24 -08001425 kfree(chip);
1426}
1427
Lee Jones9b2d6112020-07-17 14:54:23 +01001428#ifdef CONFIG_ACPI
Mathias Krause8422ddf2015-06-13 14:22:14 +02001429static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001430 { "INT33C0", LPSS_LPT_SSP },
1431 { "INT33C1", LPSS_LPT_SSP },
1432 { "INT3430", LPSS_LPT_SSP },
1433 { "INT3431", LPSS_LPT_SSP },
1434 { "80860F0E", LPSS_BYT_SSP },
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001435 { "8086228E", LPSS_BSW_SSP },
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001436 { },
1437};
1438MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
Lee Jones9b2d6112020-07-17 14:54:23 +01001439#endif
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001440
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001441/*
1442 * PCI IDs of compound devices that integrate both host controller and private
1443 * integrated DMA engine. Please note these are not used in module
1444 * autoloading and probing in this module but matching the LPSS SSP type.
1445 */
1446static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
1447 /* SPT-LP */
1448 { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
1449 { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
1450 /* SPT-H */
1451 { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
1452 { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
Mika Westerberg704d2b02016-07-04 13:21:07 +03001453 /* KBL-H */
1454 { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1455 { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
Jarkko Nikula6157d4c2020-01-16 11:10:35 +02001456 /* CML-V */
1457 { PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
1458 { PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
Jarkko Nikulac1b03f12016-03-02 09:54:14 +02001459 /* BXT A-Step */
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +02001460 { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1461 { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1462 { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
Jarkko Nikulac1b03f12016-03-02 09:54:14 +02001463 /* BXT B-Step */
1464 { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1465 { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1466 { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
David E. Boxe18a80a2017-01-19 16:25:21 +02001467 /* GLK */
1468 { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1469 { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1470 { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
Mika Westerberg22d71a502018-06-28 13:52:23 +03001471 /* ICL-LP */
1472 { PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
1473 { PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
1474 { PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
Jarkko Nikula8cc77202019-07-03 14:46:03 +03001475 /* EHL */
1476 { PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
1477 { PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
1478 { PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
Jarkko Nikula9c7315c2019-11-25 14:51:59 +02001479 /* JSL */
1480 { PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
1481 { PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
1482 { PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
Jarkko Nikulacf961fc2020-06-25 17:00:41 +03001483 /* TGL-H */
1484 { PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
1485 { PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
1486 { PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
1487 { PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
Jarkko Nikulaa402e392021-01-14 16:40:21 +02001488 /* ADL-P */
1489 { PCI_VDEVICE(INTEL, 0x51aa), LPSS_CNL_SSP },
1490 { PCI_VDEVICE(INTEL, 0x51ab), LPSS_CNL_SSP },
1491 { PCI_VDEVICE(INTEL, 0x51fb), LPSS_CNL_SSP },
Jarkko Nikula8c4ffe42021-04-15 16:59:17 +03001492 /* ADL-M */
1493 { PCI_VDEVICE(INTEL, 0x54aa), LPSS_CNL_SSP },
1494 { PCI_VDEVICE(INTEL, 0x54ab), LPSS_CNL_SSP },
1495 { PCI_VDEVICE(INTEL, 0x54fb), LPSS_CNL_SSP },
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +02001496 /* APL */
1497 { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1498 { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1499 { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
Jarkko Nikulab8450e02020-12-04 10:24:09 +02001500 /* ADL-S */
1501 { PCI_VDEVICE(INTEL, 0x7aaa), LPSS_CNL_SSP },
1502 { PCI_VDEVICE(INTEL, 0x7aab), LPSS_CNL_SSP },
1503 { PCI_VDEVICE(INTEL, 0x7af9), LPSS_CNL_SSP },
1504 { PCI_VDEVICE(INTEL, 0x7afb), LPSS_CNL_SSP },
Jarkko Nikulafc0b2ac2017-05-30 17:31:21 +03001505 /* CNL-LP */
1506 { PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1507 { PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1508 { PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1509 /* CNL-H */
1510 { PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1511 { PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1512 { PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
Evan Green41a91802019-04-15 20:27:43 -07001513 /* CML-LP */
1514 { PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
1515 { PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
1516 { PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
Jarkko Nikulaf0cf17e2019-10-29 13:58:02 +02001517 /* CML-H */
1518 { PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1519 { PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1520 { PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
Jarkko Nikulaa4127952019-08-01 16:49:01 +03001521 /* TGL-LP */
1522 { PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1523 { PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1524 { PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1525 { PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1526 { PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1527 { PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1528 { PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
Axel Lin94e5c232015-08-04 13:52:22 +08001529 { },
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001530};
1531
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001532static const struct of_device_id pxa2xx_spi_of_match[] = {
1533 { .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
1534 {},
1535};
1536MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
1537
1538#ifdef CONFIG_ACPI
1539
Andy Shevchenko365e8562019-10-18 13:54:27 +03001540static int pxa2xx_spi_get_port_id(struct device *dev)
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001541{
Andy Shevchenko365e8562019-10-18 13:54:27 +03001542 struct acpi_device *adev;
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001543 unsigned int devid;
1544 int port_id = -1;
1545
Andy Shevchenko365e8562019-10-18 13:54:27 +03001546 adev = ACPI_COMPANION(dev);
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001547 if (adev && adev->pnp.unique_id &&
1548 !kstrtouint(adev->pnp.unique_id, 0, &devid))
1549 port_id = devid;
1550 return port_id;
1551}
1552
1553#else /* !CONFIG_ACPI */
1554
Andy Shevchenko365e8562019-10-18 13:54:27 +03001555static int pxa2xx_spi_get_port_id(struct device *dev)
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001556{
1557 return -1;
1558}
1559
1560#endif /* CONFIG_ACPI */
1561
1562
1563#ifdef CONFIG_PCI
1564
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001565static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
1566{
Andy Shevchenko5ba846b2019-03-18 18:39:30 +03001567 return param == chan->device->dev;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001568}
1569
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001570#endif /* CONFIG_PCI */
1571
Lubomir Rintel51eea522019-01-16 16:13:31 +01001572static struct pxa2xx_spi_controller *
Jarkko Nikula0db64212015-10-28 15:13:43 +02001573pxa2xx_spi_init_pdata(struct platform_device *pdev)
Mika Westerberga3496852013-01-22 12:26:33 +02001574{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001575 struct pxa2xx_spi_controller *pdata;
Mika Westerberga3496852013-01-22 12:26:33 +02001576 struct ssp_device *ssp;
1577 struct resource *res;
Andy Shevchenko6fb74272019-10-21 13:36:24 +03001578 struct device *parent = pdev->dev.parent;
1579 struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001580 const struct pci_device_id *pcidev_id = NULL;
Lubomir Rintel55ef8262018-10-10 19:09:28 +02001581 enum pxa_ssp_type type;
Andy Shevchenkof2faa3e2019-10-18 13:54:28 +03001582 const void *match;
Mika Westerberga3496852013-01-22 12:26:33 +02001583
Andy Shevchenko6fb74272019-10-21 13:36:24 +03001584 if (pcidev)
1585 pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
Mika Westerberga3496852013-01-22 12:26:33 +02001586
Andy Shevchenkof2faa3e2019-10-18 13:54:28 +03001587 match = device_get_match_data(&pdev->dev);
1588 if (match)
1589 type = (enum pxa_ssp_type)match;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001590 else if (pcidev_id)
Lubomir Rintel55ef8262018-10-10 19:09:28 +02001591 type = (enum pxa_ssp_type)pcidev_id->driver_data;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001592 else
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001593 return ERR_PTR(-EINVAL);
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001594
Mika Westerbergcc0ee982013-06-20 17:44:22 +03001595 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001596 if (!pdata)
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001597 return ERR_PTR(-ENOMEM);
Mika Westerberga3496852013-01-22 12:26:33 +02001598
Mika Westerberga3496852013-01-22 12:26:33 +02001599 ssp = &pdata->ssp;
1600
Andy Shevchenko77c544d2019-10-21 13:36:25 +03001601 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sachin Kamatcbfd6a22013-04-08 15:49:33 +05301602 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1603 if (IS_ERR(ssp->mmio_base))
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001604 return ERR_CAST(ssp->mmio_base);
Mika Westerberga3496852013-01-22 12:26:33 +02001605
Andy Shevchenko77c544d2019-10-21 13:36:25 +03001606 ssp->phys_base = res->start;
1607
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001608#ifdef CONFIG_PCI
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001609 if (pcidev_id) {
Andy Shevchenko6fb74272019-10-21 13:36:24 +03001610 pdata->tx_param = parent;
1611 pdata->rx_param = parent;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001612 pdata->dma_filter = pxa2xx_spi_idma_filter;
1613 }
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001614#endif
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001615
Mika Westerberga3496852013-01-22 12:26:33 +02001616 ssp->clk = devm_clk_get(&pdev->dev, NULL);
Chuhong Yuan5eb263e2019-11-09 16:09:43 +08001617 if (IS_ERR(ssp->clk))
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001618 return ERR_CAST(ssp->clk);
Mika Westerberga3496852013-01-22 12:26:33 +02001619
Mika Westerberga3496852013-01-22 12:26:33 +02001620 ssp->irq = platform_get_irq(pdev, 0);
Chuhong Yuan5eb263e2019-11-09 16:09:43 +08001621 if (ssp->irq < 0)
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001622 return ERR_PTR(ssp->irq);
Chuhong Yuan5eb263e2019-11-09 16:09:43 +08001623
Mika Westerberga3496852013-01-22 12:26:33 +02001624 ssp->type = type;
Andy Shevchenko4f3d9572019-10-18 13:54:25 +03001625 ssp->dev = &pdev->dev;
Andy Shevchenko365e8562019-10-18 13:54:27 +03001626 ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
Mika Westerberga3496852013-01-22 12:26:33 +02001627
Andy Shevchenkof2faa3e2019-10-18 13:54:28 +03001628 pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
Mika Westerberga3496852013-01-22 12:26:33 +02001629 pdata->num_chipselect = 1;
Mika Westerbergcddb3392013-05-13 13:45:10 +03001630 pdata->enable_dma = true;
Andy Shevchenko37821a822019-03-19 17:48:42 +02001631 pdata->dma_burst_size = 1;
Mika Westerberga3496852013-01-22 12:26:33 +02001632
1633 return pdata;
1634}
1635
Lubomir Rintel51eea522019-01-16 16:13:31 +01001636static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
Jarkko Nikula3cc7b0e2018-02-01 17:17:30 +02001637 unsigned int cs)
Mika Westerberg0c27d9c2016-02-08 17:14:29 +02001638{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001639 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Mika Westerberg0c27d9c2016-02-08 17:14:29 +02001640
Andy Shevchenkoc3dce242021-04-23 21:24:30 +03001641 if (has_acpi_companion(drv_data->ssp->dev)) {
Mika Westerberg0c27d9c2016-02-08 17:14:29 +02001642 switch (drv_data->ssp_type) {
1643 /*
1644 * For Atoms the ACPI DeviceSelection used by the Windows
1645 * driver starts from 1 instead of 0 so translate it here
1646 * to match what Linux expects.
1647 */
1648 case LPSS_BYT_SSP:
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001649 case LPSS_BSW_SSP:
Mika Westerberg0c27d9c2016-02-08 17:14:29 +02001650 return cs - 1;
1651
1652 default:
1653 break;
1654 }
1655 }
1656
1657 return cs;
1658}
1659
Daniel Vetterb2662a12019-10-17 08:44:26 +02001660static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1661{
1662 return MAX_DMA_LEN;
1663}
1664
Grant Likelyfd4a3192012-12-07 16:57:14 +00001665static int pxa2xx_spi_probe(struct platform_device *pdev)
Stephen Streete0c99052006-03-07 23:53:24 -08001666{
1667 struct device *dev = &pdev->dev;
Lubomir Rintel51eea522019-01-16 16:13:31 +01001668 struct pxa2xx_spi_controller *platform_info;
1669 struct spi_controller *controller;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001670 struct driver_data *drv_data;
eric miao2f1a74e2007-11-21 18:50:53 +08001671 struct ssp_device *ssp;
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02001672 const struct lpss_config *config;
Mika Westerberg99f499c2016-09-26 15:19:50 +03001673 int status, count;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001674 u32 tmp;
Stephen Streete0c99052006-03-07 23:53:24 -08001675
Mika Westerberg851bacf2013-01-07 12:44:33 +02001676 platform_info = dev_get_platdata(dev);
1677 if (!platform_info) {
Jarkko Nikula0db64212015-10-28 15:13:43 +02001678 platform_info = pxa2xx_spi_init_pdata(pdev);
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001679 if (IS_ERR(platform_info)) {
Mika Westerberga3496852013-01-22 12:26:33 +02001680 dev_err(&pdev->dev, "missing platform data\n");
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001681 return PTR_ERR(platform_info);
Mika Westerberga3496852013-01-22 12:26:33 +02001682 }
Mika Westerberg851bacf2013-01-07 12:44:33 +02001683 }
Stephen Streete0c99052006-03-07 23:53:24 -08001684
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001685 ssp = pxa_ssp_request(pdev->id, pdev->name);
Mika Westerberg851bacf2013-01-07 12:44:33 +02001686 if (!ssp)
1687 ssp = &platform_info->ssp;
1688
1689 if (!ssp->mmio_base) {
1690 dev_err(&pdev->dev, "failed to get ssp\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001691 return -ENODEV;
1692 }
1693
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001694 if (platform_info->is_slave)
Lukas Wunner56263082020-12-07 09:17:05 +01001695 controller = devm_spi_alloc_slave(dev, sizeof(*drv_data));
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001696 else
Lukas Wunner56263082020-12-07 09:17:05 +01001697 controller = devm_spi_alloc_master(dev, sizeof(*drv_data));
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001698
Lubomir Rintel51eea522019-01-16 16:13:31 +01001699 if (!controller) {
1700 dev_err(&pdev->dev, "cannot alloc spi_controller\n");
Andy Shevchenkof2eed8c2021-04-23 21:24:28 +03001701 status = -ENOMEM;
1702 goto out_error_controller_alloc;
Stephen Streete0c99052006-03-07 23:53:24 -08001703 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001704 drv_data = spi_controller_get_devdata(controller);
1705 drv_data->controller = controller;
1706 drv_data->controller_info = platform_info;
eric miao2f1a74e2007-11-21 18:50:53 +08001707 drv_data->ssp = ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001708
Lubomir Rintel51eea522019-01-16 16:13:31 +01001709 controller->dev.of_node = pdev->dev.of_node;
David Brownelle7db06b2009-06-17 16:26:04 -07001710 /* the spi->mode bits understood by this driver: */
Lubomir Rintel51eea522019-01-16 16:13:31 +01001711 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
David Brownelle7db06b2009-06-17 16:26:04 -07001712
Lubomir Rintel51eea522019-01-16 16:13:31 +01001713 controller->bus_num = ssp->port_id;
1714 controller->dma_alignment = DMA_ALIGNMENT;
1715 controller->cleanup = cleanup;
1716 controller->setup = setup;
1717 controller->set_cs = pxa2xx_spi_set_cs;
1718 controller->transfer_one = pxa2xx_spi_transfer_one;
1719 controller->slave_abort = pxa2xx_spi_slave_abort;
1720 controller->handle_err = pxa2xx_spi_handle_err;
1721 controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
1722 controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
1723 controller->auto_runtime_pm = true;
1724 controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
Stephen Streete0c99052006-03-07 23:53:24 -08001725
eric miao2f1a74e2007-11-21 18:50:53 +08001726 drv_data->ssp_type = ssp->type;
Stephen Streete0c99052006-03-07 23:53:24 -08001727
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001728 if (pxa25x_ssp_comp(drv_data)) {
Weike Chene5262d02014-11-26 02:35:10 -08001729 switch (drv_data->ssp_type) {
1730 case QUARK_X1000_SSP:
Lubomir Rintel51eea522019-01-16 16:13:31 +01001731 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Weike Chene5262d02014-11-26 02:35:10 -08001732 break;
1733 default:
Lubomir Rintel51eea522019-01-16 16:13:31 +01001734 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Weike Chene5262d02014-11-26 02:35:10 -08001735 break;
1736 }
1737
Stephen Streete0c99052006-03-07 23:53:24 -08001738 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1739 drv_data->dma_cr1 = 0;
1740 drv_data->clear_sr = SSSR_ROR;
1741 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1742 } else {
Lubomir Rintel51eea522019-01-16 16:13:31 +01001743 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Stephen Streete0c99052006-03-07 23:53:24 -08001744 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
Mika Westerberg59288082013-01-22 12:26:29 +02001745 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
Stephen Streete0c99052006-03-07 23:53:24 -08001746 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001747 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1748 | SSSR_ROR | SSSR_TUR;
Stephen Streete0c99052006-03-07 23:53:24 -08001749 }
1750
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -08001751 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1752 drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001753 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001754 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Lubomir Rintel51eea522019-01-16 16:13:31 +01001755 goto out_error_controller_alloc;
Stephen Streete0c99052006-03-07 23:53:24 -08001756 }
1757
1758 /* Setup DMA if requested */
Stephen Streete0c99052006-03-07 23:53:24 -08001759 if (platform_info->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001760 status = pxa2xx_spi_dma_setup(drv_data);
1761 if (status) {
Flavio Suligoi8b57b112019-04-05 14:40:22 +02001762 dev_warn(dev, "no DMA channels available, using PIO\n");
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001763 platform_info->enable_dma = false;
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001764 } else {
Lubomir Rintel51eea522019-01-16 16:13:31 +01001765 controller->can_dma = pxa2xx_spi_can_dma;
Mark Brownbf9f7422019-02-20 17:58:18 +00001766 controller->max_dma_len = MAX_DMA_LEN;
Daniel Vetterb2662a12019-10-17 08:44:26 +02001767 controller->max_transfer_size =
1768 pxa2xx_spi_max_dma_transfer_size;
Stephen Streete0c99052006-03-07 23:53:24 -08001769 }
Stephen Streete0c99052006-03-07 23:53:24 -08001770 }
1771
1772 /* Enable SOC clock */
Tobias Jordan62bbc862018-04-30 16:30:06 +02001773 status = clk_prepare_enable(ssp->clk);
1774 if (status)
1775 goto out_error_dma_irq_alloc;
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001776
Lubomir Rintel51eea522019-01-16 16:13:31 +01001777 controller->max_speed_hz = clk_get_rate(ssp->clk);
Jarkko Nikula23cdddb2019-06-28 17:07:17 +03001778 /*
1779 * Set minimum speed for all other platforms than Intel Quark which is
1780 * able do under 1 Hz transfers.
1781 */
1782 if (!pxa25x_ssp_comp(drv_data))
1783 controller->min_speed_hz =
1784 DIV_ROUND_UP(controller->max_speed_hz, 4096);
1785 else if (!is_quark_x1000_ssp(drv_data))
1786 controller->min_speed_hz =
1787 DIV_ROUND_UP(controller->max_speed_hz, 512);
Stephen Streete0c99052006-03-07 23:53:24 -08001788
1789 /* Load default SSP configuration */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001790 pxa2xx_spi_write(drv_data, SSCR0, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001791 switch (drv_data->ssp_type) {
1792 case QUARK_X1000_SSP:
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001793 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
1794 QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001795 pxa2xx_spi_write(drv_data, SSCR1, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001796
1797 /* using the Motorola SPI protocol and use 8 bit frame */
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001798 tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
1799 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001800 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001801 case CE4100_SSP:
1802 tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
1803 CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
1804 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1805 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1806 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Andy Shevchenkoa2dd8af2017-01-02 13:44:28 +02001807 break;
Weike Chene5262d02014-11-26 02:35:10 -08001808 default:
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001809
Lubomir Rintel51eea522019-01-16 16:13:31 +01001810 if (spi_controller_is_slave(controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001811 tmp = SSCR1_SCFR |
1812 SSCR1_SCLKDIR |
1813 SSCR1_SFRMDIR |
1814 SSCR1_RxTresh(2) |
1815 SSCR1_TxTresh(1) |
1816 SSCR1_SPH;
1817 } else {
1818 tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1819 SSCR1_TxTresh(TX_THRESH_DFLT);
1820 }
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001821 pxa2xx_spi_write(drv_data, SSCR1, tmp);
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001822 tmp = SSCR0_Motorola | SSCR0_DataSize(8);
Lubomir Rintel51eea522019-01-16 16:13:31 +01001823 if (!spi_controller_is_slave(controller))
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001824 tmp |= SSCR0_SCR(2);
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001825 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001826 break;
1827 }
1828
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001829 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001830 pxa2xx_spi_write(drv_data, SSTO, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001831
1832 if (!is_quark_x1000_ssp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001833 pxa2xx_spi_write(drv_data, SSPSP, 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001834
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02001835 if (is_lpss_ssp(drv_data)) {
1836 lpss_ssp_setup(drv_data);
1837 config = lpss_get_config(drv_data);
1838 if (config->reg_capabilities >= 0) {
1839 tmp = __lpss_ssp_read_priv(drv_data,
1840 config->reg_capabilities);
1841 tmp &= LPSS_CAPS_CS_EN_MASK;
1842 tmp >>= LPSS_CAPS_CS_EN_SHIFT;
1843 platform_info->num_chipselect = ffz(tmp);
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001844 } else if (config->cs_num) {
1845 platform_info->num_chipselect = config->cs_num;
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02001846 }
1847 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001848 controller->num_chipselect = platform_info->num_chipselect;
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02001849
Mika Westerberg99f499c2016-09-26 15:19:50 +03001850 count = gpiod_count(&pdev->dev, "cs");
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001851 if (count > 0) {
1852 int i;
1853
Lubomir Rintel51eea522019-01-16 16:13:31 +01001854 controller->num_chipselect = max_t(int, count,
1855 controller->num_chipselect);
Mika Westerberg99f499c2016-09-26 15:19:50 +03001856
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001857 drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
Lubomir Rintel51eea522019-01-16 16:13:31 +01001858 controller->num_chipselect, sizeof(struct gpio_desc *),
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001859 GFP_KERNEL);
1860 if (!drv_data->cs_gpiods) {
1861 status = -ENOMEM;
1862 goto out_error_clock_enabled;
1863 }
1864
Lubomir Rintel51eea522019-01-16 16:13:31 +01001865 for (i = 0; i < controller->num_chipselect; i++) {
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001866 struct gpio_desc *gpiod;
1867
Andy Shevchenkod35f2dc2017-07-27 18:49:33 +03001868 gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001869 if (IS_ERR(gpiod)) {
1870 /* Means use native chip select */
1871 if (PTR_ERR(gpiod) == -ENOENT)
1872 continue;
1873
Lubomir Rintel77d33892018-11-13 11:22:27 +01001874 status = PTR_ERR(gpiod);
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001875 goto out_error_clock_enabled;
1876 } else {
1877 drv_data->cs_gpiods[i] = gpiod;
1878 }
1879 }
1880 }
1881
Lubomir Rintel77d33892018-11-13 11:22:27 +01001882 if (platform_info->is_slave) {
1883 drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
1884 "ready", GPIOD_OUT_LOW);
1885 if (IS_ERR(drv_data->gpiod_ready)) {
1886 status = PTR_ERR(drv_data->gpiod_ready);
1887 goto out_error_clock_enabled;
1888 }
1889 }
1890
Antonio Ospite836d1a222014-05-30 18:18:09 +02001891 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1892 pm_runtime_use_autosuspend(&pdev->dev);
1893 pm_runtime_set_active(&pdev->dev);
1894 pm_runtime_enable(&pdev->dev);
1895
Stephen Streete0c99052006-03-07 23:53:24 -08001896 /* Register with the SPI framework */
1897 platform_set_drvdata(pdev, drv_data);
Lukas Wunner32e5b572020-05-25 14:25:02 +02001898 status = spi_register_controller(controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001899 if (status != 0) {
Lubomir Rintel51eea522019-01-16 16:13:31 +01001900 dev_err(&pdev->dev, "problem registering spi controller\n");
Lubomir Rintel12742042019-07-19 14:27:13 +02001901 goto out_error_pm_runtime_enabled;
Stephen Streete0c99052006-03-07 23:53:24 -08001902 }
1903
1904 return status;
1905
Lubomir Rintel12742042019-07-19 14:27:13 +02001906out_error_pm_runtime_enabled:
Jarkko Nikulae2b714a2018-03-07 17:05:04 +02001907 pm_runtime_disable(&pdev->dev);
Lubomir Rintel12742042019-07-19 14:27:13 +02001908
1909out_error_clock_enabled:
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001910 clk_disable_unprepare(ssp->clk);
Tobias Jordan62bbc862018-04-30 16:30:06 +02001911
1912out_error_dma_irq_alloc:
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001913 pxa2xx_spi_dma_release(drv_data);
eric miao2f1a74e2007-11-21 18:50:53 +08001914 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001915
Lubomir Rintel51eea522019-01-16 16:13:31 +01001916out_error_controller_alloc:
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001917 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001918 return status;
1919}
1920
1921static int pxa2xx_spi_remove(struct platform_device *pdev)
1922{
1923 struct driver_data *drv_data = platform_get_drvdata(pdev);
Andy Shevchenko3d24b2a2020-02-24 17:45:56 +02001924 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001925
Mika Westerberg7d94a502013-01-22 12:26:30 +02001926 pm_runtime_get_sync(&pdev->dev);
1927
Lukas Wunner32e5b572020-05-25 14:25:02 +02001928 spi_unregister_controller(drv_data->controller);
1929
Stephen Streete0c99052006-03-07 23:53:24 -08001930 /* Disable the SSP at the peripheral and SOC level */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001931 pxa2xx_spi_write(drv_data, SSCR0, 0);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001932 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001933
1934 /* Release DMA */
Lubomir Rintel51eea522019-01-16 16:13:31 +01001935 if (drv_data->controller_info->enable_dma)
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001936 pxa2xx_spi_dma_release(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001937
Mika Westerberg7d94a502013-01-22 12:26:30 +02001938 pm_runtime_put_noidle(&pdev->dev);
1939 pm_runtime_disable(&pdev->dev);
1940
Stephen Streete0c99052006-03-07 23:53:24 -08001941 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001942 free_irq(ssp->irq, drv_data);
1943
1944 /* Release SSP */
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001945 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001946
Stephen Streete0c99052006-03-07 23:53:24 -08001947 return 0;
1948}
1949
Mika Westerberg382cebb2014-01-16 14:50:55 +02001950#ifdef CONFIG_PM_SLEEP
Mike Rapoport86d25932009-07-21 17:50:16 +03001951static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001952{
Mike Rapoport86d25932009-07-21 17:50:16 +03001953 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001954 struct ssp_device *ssp = drv_data->ssp;
Jarkko Nikulabffc9672016-09-07 17:04:05 +03001955 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001956
Lubomir Rintel51eea522019-01-16 16:13:31 +01001957 status = spi_controller_suspend(drv_data->controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001958 if (status != 0)
1959 return status;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001960 pxa2xx_spi_write(drv_data, SSCR0, 0);
Dmitry Eremin-Solenikov2b9375b2014-11-06 14:08:29 +03001961
1962 if (!pm_runtime_suspended(dev))
1963 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001964
1965 return 0;
1966}
1967
Mike Rapoport86d25932009-07-21 17:50:16 +03001968static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001969{
Mike Rapoport86d25932009-07-21 17:50:16 +03001970 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001971 struct ssp_device *ssp = drv_data->ssp;
Jarkko Nikulabffc9672016-09-07 17:04:05 +03001972 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001973
1974 /* Enable the SSP clock */
Tobias Jordan62bbc862018-04-30 16:30:06 +02001975 if (!pm_runtime_suspended(dev)) {
1976 status = clk_prepare_enable(ssp->clk);
1977 if (status)
1978 return status;
1979 }
Stephen Streete0c99052006-03-07 23:53:24 -08001980
1981 /* Start the queue running */
Lubomir Rintel51eea522019-01-16 16:13:31 +01001982 return spi_controller_resume(drv_data->controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001983}
Mika Westerberg7d94a502013-01-22 12:26:30 +02001984#endif
1985
Rafael J. Wysockiec833052014-12-13 00:41:15 +01001986#ifdef CONFIG_PM
Mika Westerberg7d94a502013-01-22 12:26:30 +02001987static int pxa2xx_spi_runtime_suspend(struct device *dev)
1988{
1989 struct driver_data *drv_data = dev_get_drvdata(dev);
1990
1991 clk_disable_unprepare(drv_data->ssp->clk);
1992 return 0;
1993}
1994
1995static int pxa2xx_spi_runtime_resume(struct device *dev)
1996{
1997 struct driver_data *drv_data = dev_get_drvdata(dev);
Tobias Jordan62bbc862018-04-30 16:30:06 +02001998 int status;
Mika Westerberg7d94a502013-01-22 12:26:30 +02001999
Tobias Jordan62bbc862018-04-30 16:30:06 +02002000 status = clk_prepare_enable(drv_data->ssp->clk);
2001 return status;
Mika Westerberg7d94a502013-01-22 12:26:30 +02002002}
2003#endif
Mike Rapoport86d25932009-07-21 17:50:16 +03002004
Alexey Dobriyan47145212009-12-14 18:00:08 -08002005static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mika Westerberg7d94a502013-01-22 12:26:30 +02002006 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
2007 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
2008 pxa2xx_spi_runtime_resume, NULL)
Mike Rapoport86d25932009-07-21 17:50:16 +03002009};
Stephen Streete0c99052006-03-07 23:53:24 -08002010
2011static struct platform_driver driver = {
2012 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03002013 .name = "pxa2xx-spi",
Mike Rapoport86d25932009-07-21 17:50:16 +03002014 .pm = &pxa2xx_spi_pm_ops,
Mika Westerberga3496852013-01-22 12:26:33 +02002015 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02002016 .of_match_table = of_match_ptr(pxa2xx_spi_of_match),
Stephen Streete0c99052006-03-07 23:53:24 -08002017 },
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08002018 .probe = pxa2xx_spi_probe,
David Brownelld1e44d92007-10-16 01:27:46 -07002019 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08002020};
2021
2022static int __init pxa2xx_spi_init(void)
2023{
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08002024 return platform_driver_register(&driver);
Stephen Streete0c99052006-03-07 23:53:24 -08002025}
Antonio Ospite5b61a742009-09-22 16:46:10 -07002026subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08002027
2028static void __exit pxa2xx_spi_exit(void)
2029{
2030 platform_driver_unregister(&driver);
2031}
2032module_exit(pxa2xx_spi_exit);
Flavio Suligoi51ebf6a2019-04-10 14:51:36 +02002033
2034MODULE_SOFTDEP("pre: dw_dmac");