blob: 1573f6d8eb48a0cd9e5547af2fd23baa25acfa31 [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
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03004 * Copyright (C) 2013, 2021 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/*
Andy Shevchenko8083d6b2021-05-17 17:03:49 +030043 * For testing SSCR1 changes that require SSP restart, basically
44 * everything except the service and interrupt enables, the PXA270 developer
Ned Forresterb97c74b2008-02-23 15:23:40 -080045 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
Andy Shevchenko8083d6b2021-05-17 17:03:49 +030046 * list, but the PXA255 developer manual says all bits without really meaning
47 * the service and interrupt enables.
Ned Forresterb97c74b2008-02-23 15:23:40 -080048 */
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
Andy Shevchenko3fdb59c2021-05-10 15:41:34 +0300203static bool is_mrfld_ssp(const struct driver_data *drv_data)
204{
205 return drv_data->ssp_type == MRFLD_SSP;
206}
207
Andy Shevchenko1bed3782021-05-10 15:41:30 +0300208static void pxa2xx_spi_update(const struct driver_data *drv_data, u32 reg, u32 mask, u32 value)
209{
210 if ((pxa2xx_spi_read(drv_data, reg) & mask) != value)
211 pxa2xx_spi_write(drv_data, reg, value & mask);
212}
213
Weike Chen4fdb2422014-10-08 08:50:22 -0700214static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
215{
216 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800217 case QUARK_X1000_SSP:
218 return QUARK_X1000_SSCR1_CHANGE_MASK;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300219 case CE4100_SSP:
220 return CE4100_SSCR1_CHANGE_MASK;
Weike Chen4fdb2422014-10-08 08:50:22 -0700221 default:
222 return SSCR1_CHANGE_MASK;
223 }
224}
225
226static u32
227pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
228{
229 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800230 case QUARK_X1000_SSP:
231 return RX_THRESH_QUARK_X1000_DFLT;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300232 case CE4100_SSP:
233 return RX_THRESH_CE4100_DFLT;
Weike Chen4fdb2422014-10-08 08:50:22 -0700234 default:
235 return RX_THRESH_DFLT;
236 }
237}
238
239static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
240{
Weike Chen4fdb2422014-10-08 08:50:22 -0700241 u32 mask;
242
243 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800244 case QUARK_X1000_SSP:
245 mask = QUARK_X1000_SSSR_TFL_MASK;
246 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300247 case CE4100_SSP:
248 mask = CE4100_SSSR_TFL_MASK;
249 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700250 default:
251 mask = SSSR_TFL_MASK;
252 break;
253 }
254
Andy Shevchenko6d380132021-05-10 15:41:32 +0300255 return read_SSSR_bits(drv_data, mask) == mask;
Weike Chen4fdb2422014-10-08 08:50:22 -0700256}
257
258static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
259 u32 *sccr1_reg)
260{
261 u32 mask;
262
263 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800264 case QUARK_X1000_SSP:
265 mask = QUARK_X1000_SSCR1_RFT;
266 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300267 case CE4100_SSP:
268 mask = CE4100_SSCR1_RFT;
269 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700270 default:
271 mask = SSCR1_RFT;
272 break;
273 }
274 *sccr1_reg &= ~mask;
275}
276
277static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
278 u32 *sccr1_reg, u32 threshold)
279{
280 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800281 case QUARK_X1000_SSP:
282 *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
283 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300284 case CE4100_SSP:
285 *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
286 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700287 default:
288 *sccr1_reg |= SSCR1_RxTresh(threshold);
289 break;
290 }
291}
292
293static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
294 u32 clk_div, u8 bits)
295{
296 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800297 case QUARK_X1000_SSP:
298 return clk_div
299 | QUARK_X1000_SSCR0_Motorola
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +0300300 | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits);
Weike Chen4fdb2422014-10-08 08:50:22 -0700301 default:
302 return clk_div
303 | SSCR0_Motorola
304 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
Weike Chen4fdb2422014-10-08 08:50:22 -0700305 | (bits > 16 ? SSCR0_EDSS : 0);
306 }
307}
308
Mika Westerberga0d26422013-01-22 12:26:32 +0200309/*
310 * Read and write LPSS SSP private registers. Caller must first check that
311 * is_lpss_ssp() returns true before these can be called.
312 */
313static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
314{
315 WARN_ON(!drv_data->lpss_base);
316 return readl(drv_data->lpss_base + offset);
317}
318
319static void __lpss_ssp_write_priv(struct driver_data *drv_data,
320 unsigned offset, u32 value)
321{
322 WARN_ON(!drv_data->lpss_base);
323 writel(value, drv_data->lpss_base + offset);
324}
325
326/*
327 * lpss_ssp_setup - perform LPSS SSP specific setup
328 * @drv_data: pointer to the driver private data
329 *
330 * Perform LPSS SSP specific setup. This function must be called first if
331 * one is going to use LPSS SSP private registers.
332 */
333static void lpss_ssp_setup(struct driver_data *drv_data)
334{
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300335 const struct lpss_config *config;
336 u32 value;
Mika Westerberga0d26422013-01-22 12:26:32 +0200337
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300338 config = lpss_get_config(drv_data);
Andy Shevchenko9e43c9a82021-04-23 21:24:29 +0300339 drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset;
Mika Westerberga0d26422013-01-22 12:26:32 +0200340
341 /* Enable software chip select control */
Jarkko Nikula0e897212015-10-22 16:44:42 +0300342 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
Jarkko Nikula624ea722015-10-28 15:13:39 +0200343 value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
344 value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300345 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
Mika Westerberg0054e282013-03-05 12:05:17 +0200346
347 /* Enable multiblock DMA transfers */
Lubomir Rintel51eea522019-01-16 16:13:31 +0100348 if (drv_data->controller_info->enable_dma) {
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300349 __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
Mika Westerberg1de70612013-07-03 13:25:06 +0300350
Jarkko Nikula82ba2c22015-06-04 16:55:12 +0300351 if (config->reg_general >= 0) {
352 value = __lpss_ssp_read_priv(drv_data,
353 config->reg_general);
Jarkko Nikula624ea722015-10-28 15:13:39 +0200354 value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
Jarkko Nikula82ba2c22015-06-04 16:55:12 +0300355 __lpss_ssp_write_priv(drv_data,
356 config->reg_general, value);
357 }
Mika Westerberg1de70612013-07-03 13:25:06 +0300358 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200359}
360
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300361static void lpss_ssp_select_cs(struct spi_device *spi,
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200362 const struct lpss_config *config)
363{
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300364 struct driver_data *drv_data =
365 spi_controller_get_devdata(spi->controller);
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200366 u32 value, cs;
367
368 if (!config->cs_sel_mask)
369 return;
370
371 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
372
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300373 cs = spi->chip_select;
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200374 cs <<= config->cs_sel_shift;
375 if (cs != (value & config->cs_sel_mask)) {
376 /*
377 * When switching another chip select output active the
378 * output must be selected first and wait 2 ssp_clk cycles
379 * before changing state to active. Otherwise a short
380 * glitch will occur on the previous chip select since
381 * output select is latched but state control is not.
382 */
383 value &= ~config->cs_sel_mask;
384 value |= cs;
385 __lpss_ssp_write_priv(drv_data,
386 config->reg_cs_ctrl, value);
387 ndelay(1000000000 /
Lubomir Rintel51eea522019-01-16 16:13:31 +0100388 (drv_data->controller->max_speed_hz / 2));
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200389 }
390}
391
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300392static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
Mika Westerberga0d26422013-01-22 12:26:32 +0200393{
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300394 struct driver_data *drv_data =
395 spi_controller_get_devdata(spi->controller);
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300396 const struct lpss_config *config;
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200397 u32 value;
Mika Westerberga0d26422013-01-22 12:26:32 +0200398
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300399 config = lpss_get_config(drv_data);
400
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200401 if (enable)
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300402 lpss_ssp_select_cs(spi, config);
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200403
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300404 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200405 if (enable)
Jarkko Nikula624ea722015-10-28 15:13:39 +0200406 value &= ~LPSS_CS_CONTROL_CS_HIGH;
Mika Westerbergc1e4a532016-02-08 17:14:30 +0200407 else
Jarkko Nikula624ea722015-10-28 15:13:39 +0200408 value |= LPSS_CS_CONTROL_CS_HIGH;
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300409 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
Evan Green683f65d2020-02-11 14:37:00 -0800410 if (config->cs_clk_stays_gated) {
411 u32 clkgate;
412
413 /*
414 * Changing CS alone when dynamic clock gating is on won't
415 * actually flip CS at that time. This ruins SPI transfers
416 * that specify delays, or have no data. Toggle the clock mode
417 * to force on briefly to poke the CS pin to move.
418 */
419 clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
420 value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
421 LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
422
423 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
424 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
425 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200426}
427
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300428static void cs_assert(struct spi_device *spi)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700429{
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300430 struct chip_data *chip = spi_get_ctldata(spi);
431 struct driver_data *drv_data =
432 spi_controller_get_devdata(spi->controller);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700433
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800434 if (drv_data->ssp_type == CE4100_SSP) {
Andy Shevchenkoccd60b22021-05-17 17:03:46 +0300435 pxa2xx_spi_write(drv_data, SSSR, spi->chip_select);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800436 return;
437 }
438
Eric Miaoa7bb3902009-04-06 19:00:54 -0700439 if (chip->cs_control) {
440 chip->cs_control(PXA2XX_CS_ASSERT);
441 return;
442 }
443
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200444 if (is_lpss_ssp(drv_data))
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300445 lpss_ssp_cs_control(spi, true);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700446}
447
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300448static void cs_deassert(struct spi_device *spi)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700449{
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300450 struct chip_data *chip = spi_get_ctldata(spi);
451 struct driver_data *drv_data =
452 spi_controller_get_devdata(spi->controller);
Jarkko Nikula104e51a2018-02-09 16:31:07 +0200453 unsigned long timeout;
Eric Miaoa7bb3902009-04-06 19:00:54 -0700454
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800455 if (drv_data->ssp_type == CE4100_SSP)
456 return;
457
Jarkko Nikula104e51a2018-02-09 16:31:07 +0200458 /* Wait until SSP becomes idle before deasserting the CS */
459 timeout = jiffies + msecs_to_jiffies(10);
460 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
461 !time_after(jiffies, timeout))
462 cpu_relax();
463
Eric Miaoa7bb3902009-04-06 19:00:54 -0700464 if (chip->cs_control) {
Daniel Ribeiro2b2562d2009-04-08 22:48:03 -0300465 chip->cs_control(PXA2XX_CS_DEASSERT);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700466 return;
467 }
468
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200469 if (is_lpss_ssp(drv_data))
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300470 lpss_ssp_cs_control(spi, false);
471}
472
473static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
474{
475 if (level)
476 cs_deassert(spi);
477 else
478 cs_assert(spi);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700479}
480
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200481int pxa2xx_spi_flush(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800482{
483 unsigned long limit = loops_per_jiffy << 1;
484
Stephen Streete0c99052006-03-07 23:53:24 -0800485 do {
Andy Shevchenko6d380132021-05-10 15:41:32 +0300486 while (read_SSSR_bits(drv_data, SSSR_RNE))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200487 pxa2xx_spi_read(drv_data, SSDR);
488 } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800489 write_SSSR_CS(drv_data, SSSR_ROR);
Stephen Streete0c99052006-03-07 23:53:24 -0800490
491 return limit;
492}
493
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100494static void pxa2xx_spi_off(struct driver_data *drv_data)
495{
Andy Shevchenko41c98842020-02-27 18:25:56 +0200496 /* On MMP, disabling SSE seems to corrupt the Rx FIFO */
497 if (is_mmp2_ssp(drv_data))
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100498 return;
499
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +0300500 pxa_ssp_disable(drv_data->ssp);
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100501}
502
Stephen Street8d94cc52006-12-10 02:18:54 -0800503static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800504{
Stephen Street9708c122006-03-28 14:05:23 -0800505 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800506
Weike Chen4fdb2422014-10-08 08:50:22 -0700507 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800508 || (drv_data->tx == drv_data->tx_end))
509 return 0;
510
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200511 pxa2xx_spi_write(drv_data, SSDR, 0);
Stephen Street8d94cc52006-12-10 02:18:54 -0800512 drv_data->tx += n_bytes;
513
514 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800515}
516
Stephen Street8d94cc52006-12-10 02:18:54 -0800517static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800518{
Stephen Street9708c122006-03-28 14:05:23 -0800519 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800520
Andy Shevchenko6d380132021-05-10 15:41:32 +0300521 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200522 pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800523 drv_data->rx += n_bytes;
524 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800525
526 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800527}
528
Stephen Street8d94cc52006-12-10 02:18:54 -0800529static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800530{
Weike Chen4fdb2422014-10-08 08:50:22 -0700531 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800532 || (drv_data->tx == drv_data->tx_end))
533 return 0;
534
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200535 pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800536 ++drv_data->tx;
537
538 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800539}
540
Stephen Street8d94cc52006-12-10 02:18:54 -0800541static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800542{
Andy Shevchenko6d380132021-05-10 15:41:32 +0300543 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200544 *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800545 ++drv_data->rx;
546 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800547
548 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800549}
550
Stephen Street8d94cc52006-12-10 02:18:54 -0800551static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800552{
Weike Chen4fdb2422014-10-08 08:50:22 -0700553 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800554 || (drv_data->tx == drv_data->tx_end))
555 return 0;
556
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200557 pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800558 drv_data->tx += 2;
559
560 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800561}
562
Stephen Street8d94cc52006-12-10 02:18:54 -0800563static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800564{
Andy Shevchenko6d380132021-05-10 15:41:32 +0300565 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200566 *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800567 drv_data->rx += 2;
568 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800569
570 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800571}
Stephen Street8d94cc52006-12-10 02:18:54 -0800572
573static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800574{
Weike Chen4fdb2422014-10-08 08:50:22 -0700575 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800576 || (drv_data->tx == drv_data->tx_end))
577 return 0;
578
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200579 pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800580 drv_data->tx += 4;
581
582 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800583}
584
Stephen Street8d94cc52006-12-10 02:18:54 -0800585static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800586{
Andy Shevchenko6d380132021-05-10 15:41:32 +0300587 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200588 *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800589 drv_data->rx += 4;
590 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800591
592 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800593}
594
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800595static void reset_sccr1(struct driver_data *drv_data)
596{
Andy Shevchenkoe3aa9ac2021-07-21 15:15:20 +0300597 u32 mask = drv_data->int_cr1 | drv_data->dma_cr1, threshold;
598 struct chip_data *chip;
599
600 if (drv_data->controller->cur_msg) {
601 chip = spi_get_ctldata(drv_data->controller->cur_msg->spi);
602 threshold = chip->threshold;
603 } else {
604 threshold = 0;
605 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800606
Andy Shevchenko152bc192016-07-06 12:08:11 +0300607 switch (drv_data->ssp_type) {
608 case QUARK_X1000_SSP:
Andy Shevchenkoe0a65122021-07-19 10:48:40 +0300609 mask |= QUARK_X1000_SSCR1_RFT;
Andy Shevchenko152bc192016-07-06 12:08:11 +0300610 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300611 case CE4100_SSP:
Andy Shevchenkoe0a65122021-07-19 10:48:40 +0300612 mask |= CE4100_SSCR1_RFT;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300613 break;
Andy Shevchenko152bc192016-07-06 12:08:11 +0300614 default:
Andy Shevchenkoe0a65122021-07-19 10:48:40 +0300615 mask |= SSCR1_RFT;
Andy Shevchenko152bc192016-07-06 12:08:11 +0300616 break;
617 }
Andy Shevchenkoe0a65122021-07-19 10:48:40 +0300618
Andy Shevchenkoe3aa9ac2021-07-21 15:15:20 +0300619 pxa2xx_spi_update(drv_data, SSCR1, mask, threshold);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800620}
621
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300622static void int_stop_and_reset(struct driver_data *drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800623{
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300624 /* Clear and disable interrupts */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800625 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800626 reset_sccr1(drv_data);
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300627 if (pxa25x_ssp_comp(drv_data))
628 return;
629
630 pxa2xx_spi_write(drv_data, SSTO, 0);
631}
632
Andy Shevchenko4761d2e2021-05-10 15:41:28 +0300633static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300634{
635 int_stop_and_reset(drv_data);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200636 pxa2xx_spi_flush(drv_data);
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100637 pxa2xx_spi_off(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800638
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300639 dev_err(drv_data->ssp->dev, "%s\n", msg);
Stephen Street8d94cc52006-12-10 02:18:54 -0800640
Andy Shevchenko4761d2e2021-05-10 15:41:28 +0300641 drv_data->controller->cur_msg->status = err;
Lubomir Rintel51eea522019-01-16 16:13:31 +0100642 spi_finalize_current_transfer(drv_data->controller);
Stephen Street8d94cc52006-12-10 02:18:54 -0800643}
644
645static void int_transfer_complete(struct driver_data *drv_data)
646{
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300647 int_stop_and_reset(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800648
Lubomir Rintel51eea522019-01-16 16:13:31 +0100649 spi_finalize_current_transfer(drv_data->controller);
Stephen Street8d94cc52006-12-10 02:18:54 -0800650}
651
Stephen Streete0c99052006-03-07 23:53:24 -0800652static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
653{
Andy Shevchenko6d380132021-05-10 15:41:32 +0300654 u32 irq_status;
Stephen Street8d94cc52006-12-10 02:18:54 -0800655
Andy Shevchenko6d380132021-05-10 15:41:32 +0300656 irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr);
657 if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE))
658 irq_status &= ~SSSR_TFS;
Stephen Streete0c99052006-03-07 23:53:24 -0800659
Stephen Street8d94cc52006-12-10 02:18:54 -0800660 if (irq_status & SSSR_ROR) {
Andy Shevchenko8083d6b2021-05-17 17:03:49 +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 Shevchenko8083d6b2021-05-17 17:03:49 +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
Andy Shevchenko8083d6b2021-05-17 17:03:49 +0300678 /* Drain Rx FIFO, Fill Tx FIFO and prevent overruns */
Stephen Street8d94cc52006-12-10 02:18:54 -0800679 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 /*
Andy Shevchenko8083d6b2021-05-17 17:03:49 +0300699 * PXA25x_SSP has no timeout, set up Rx threshold for
700 * the 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{
Andy Shevchenko3bbdc082021-07-19 10:48:42 +0300732 int_stop_and_reset(drv_data);
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100733 pxa2xx_spi_off(drv_data);
Jan Kiszkab0312482017-01-16 19:44:54 +0100734
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300735 dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
Jan Kiszkab0312482017-01-16 19:44:54 +0100736}
737
David Howells7d12e782006-10-05 14:55:46 +0100738static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800739{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400740 struct driver_data *drv_data = dev_id;
Mika Westerberg7d94a502013-01-22 12:26:30 +0200741 u32 sccr1_reg;
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800742 u32 mask = drv_data->mask_sr;
743 u32 status;
744
Mika Westerberg7d94a502013-01-22 12:26:30 +0200745 /*
746 * The IRQ might be shared with other peripherals so we must first
747 * check that are we RPM suspended or not. If we are we assume that
748 * the IRQ was not for us (we shouldn't be RPM suspended when the
749 * interrupt is enabled).
750 */
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300751 if (pm_runtime_suspended(drv_data->ssp->dev))
Mika Westerberg7d94a502013-01-22 12:26:30 +0200752 return IRQ_NONE;
753
Mika Westerberg269e4a42013-09-04 13:37:43 +0300754 /*
755 * If the device is not yet in RPM suspended state and we get an
756 * interrupt that is meant for another device, check if status bits
757 * are all set to one. That means that the device is already
758 * powered off.
759 */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200760 status = pxa2xx_spi_read(drv_data, SSSR);
Mika Westerberg269e4a42013-09-04 13:37:43 +0300761 if (status == ~0)
762 return IRQ_NONE;
763
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200764 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800765
766 /* Ignore possible writes if we don't need to write */
767 if (!(sccr1_reg & SSCR1_TIE))
768 mask &= ~SSSR_TFS;
769
Tan, Jui Nee02bc9332015-09-01 10:22:51 +0800770 /* Ignore RX timeout interrupt if it is disabled */
771 if (!(sccr1_reg & SSCR1_TINTE))
772 mask &= ~SSSR_TINT;
773
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800774 if (!(status & mask))
775 return IRQ_NONE;
Stephen Streete0c99052006-03-07 23:53:24 -0800776
Jan Kiszkae51e9b92017-01-21 10:06:38 +0100777 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
778 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
779
Lubomir Rintel51eea522019-01-16 16:13:31 +0100780 if (!drv_data->controller->cur_msg) {
Jan Kiszkab0312482017-01-16 19:44:54 +0100781 handle_bad_msg(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800782 /* Never fail */
783 return IRQ_HANDLED;
784 }
785
786 return drv_data->transfer_handler(drv_data);
787}
788
Weike Chene5262d02014-11-26 02:35:10 -0800789/*
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200790 * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
791 * input frequency by fractions of 2^24. It also has a divider by 5.
792 *
793 * There are formulas to get baud rate value for given input frequency and
794 * divider parameters, such as DDS_CLK_RATE and SCR:
795 *
796 * Fsys = 200MHz
797 *
798 * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
799 * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
800 *
801 * DDS_CLK_RATE either 2^n or 2^n / 5.
802 * SCR is in range 0 .. 255
803 *
804 * Divisor = 5^i * 2^j * 2 * k
805 * i = [0, 1] i = 1 iff j = 0 or j > 3
806 * j = [0, 23] j = 0 iff i = 1
807 * k = [1, 256]
808 * Special case: j = 0, i = 1: Divisor = 2 / 5
809 *
810 * Accordingly to the specification the recommended values for DDS_CLK_RATE
811 * are:
812 * Case 1: 2^n, n = [0, 23]
813 * Case 2: 2^24 * 2 / 5 (0x666666)
814 * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
815 *
816 * In all cases the lowest possible value is better.
817 *
818 * The function calculates parameters for all cases and chooses the one closest
819 * to the asked baud rate.
Weike Chene5262d02014-11-26 02:35:10 -0800820 */
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200821static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
Weike Chene5262d02014-11-26 02:35:10 -0800822{
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200823 unsigned long xtal = 200000000;
824 unsigned long fref = xtal / 2; /* mandatory division by 2,
825 see (2) */
826 /* case 3 */
827 unsigned long fref1 = fref / 2; /* case 1 */
828 unsigned long fref2 = fref * 2 / 5; /* case 2 */
829 unsigned long scale;
830 unsigned long q, q1, q2;
831 long r, r1, r2;
832 u32 mul;
Weike Chene5262d02014-11-26 02:35:10 -0800833
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200834 /* Case 1 */
835
836 /* Set initial value for DDS_CLK_RATE */
837 mul = (1 << 24) >> 1;
838
839 /* Calculate initial quot */
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300840 q1 = DIV_ROUND_UP(fref1, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200841
842 /* Scale q1 if it's too big */
843 if (q1 > 256) {
844 /* Scale q1 to range [1, 512] */
845 scale = fls_long(q1 - 1);
846 if (scale > 9) {
847 q1 >>= scale - 9;
848 mul >>= scale - 9;
849 }
850
851 /* Round the result if we have a remainder */
852 q1 += q1 & 1;
853 }
854
855 /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
856 scale = __ffs(q1);
857 q1 >>= scale;
858 mul >>= scale;
859
860 /* Get the remainder */
861 r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
862
863 /* Case 2 */
864
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300865 q2 = DIV_ROUND_UP(fref2, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200866 r2 = abs(fref2 / q2 - rate);
867
868 /*
869 * Choose the best between two: less remainder we have the better. We
870 * can't go case 2 if q2 is greater than 256 since SCR register can
871 * hold only values 0 .. 255.
872 */
873 if (r2 >= r1 || q2 > 256) {
874 /* case 1 is better */
875 r = r1;
876 q = q1;
877 } else {
878 /* case 2 is better */
879 r = r2;
880 q = q2;
881 mul = (1 << 24) * 2 / 5;
882 }
883
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300884 /* Check case 3 only if the divisor is big enough */
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200885 if (fref / rate >= 80) {
886 u64 fssp;
887 u32 m;
888
889 /* Calculate initial quot */
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300890 q1 = DIV_ROUND_UP(fref, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200891 m = (1 << 24) / q1;
892
893 /* Get the remainder */
894 fssp = (u64)fref * m;
895 do_div(fssp, 1 << 24);
896 r1 = abs(fssp - rate);
897
898 /* Choose this one if it suits better */
899 if (r1 < r) {
900 /* case 3 is better */
901 q = 1;
902 mul = m;
Weike Chene5262d02014-11-26 02:35:10 -0800903 }
904 }
905
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200906 *dds = mul;
907 return q - 1;
Weike Chene5262d02014-11-26 02:35:10 -0800908}
909
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200910static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
eric miao2f1a74e2007-11-21 18:50:53 +0800911{
Lubomir Rintel51eea522019-01-16 16:13:31 +0100912 unsigned long ssp_clk = drv_data->controller->max_speed_hz;
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200913 const struct ssp_device *ssp = drv_data->ssp;
914
915 rate = min_t(int, ssp_clk, rate);
eric miao2f1a74e2007-11-21 18:50:53 +0800916
Flavio Suligoi29f21332019-04-12 09:32:19 +0200917 /*
918 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
Andy Shevchenko8083d6b2021-05-17 17:03:49 +0300919 * that the SSP transmission rate can be greater than the device rate.
Flavio Suligoi29f21332019-04-12 09:32:19 +0200920 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800921 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
Flavio Suligoi29f21332019-04-12 09:32:19 +0200922 return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
eric miao2f1a74e2007-11-21 18:50:53 +0800923 else
Flavio Suligoi29f21332019-04-12 09:32:19 +0200924 return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff;
eric miao2f1a74e2007-11-21 18:50:53 +0800925}
926
Weike Chene5262d02014-11-26 02:35:10 -0800927static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
Andy Shevchenkod2c2f6a2015-10-22 16:44:40 +0300928 int rate)
Weike Chene5262d02014-11-26 02:35:10 -0800929{
Jarkko Nikula96579a42016-09-07 17:04:07 +0300930 struct chip_data *chip =
Lubomir Rintel51eea522019-01-16 16:13:31 +0100931 spi_get_ctldata(drv_data->controller->cur_msg->spi);
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200932 unsigned int clk_div;
Weike Chene5262d02014-11-26 02:35:10 -0800933
934 switch (drv_data->ssp_type) {
935 case QUARK_X1000_SSP:
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200936 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300937 break;
Weike Chene5262d02014-11-26 02:35:10 -0800938 default:
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200939 clk_div = ssp_get_clk_div(drv_data, rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300940 break;
Weike Chene5262d02014-11-26 02:35:10 -0800941 }
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200942 return clk_div << 8;
Weike Chene5262d02014-11-26 02:35:10 -0800943}
944
Lubomir Rintel51eea522019-01-16 16:13:31 +0100945static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300946 struct spi_device *spi,
947 struct spi_transfer *xfer)
948{
949 struct chip_data *chip = spi_get_ctldata(spi);
950
951 return chip->enable_dma &&
952 xfer->len <= MAX_DMA_LEN &&
953 xfer->len >= chip->dma_burst_size;
954}
955
Lubomir Rintel51eea522019-01-16 16:13:31 +0100956static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
kbuild test robot71293a62018-04-18 03:53:23 +0800957 struct spi_device *spi,
958 struct spi_transfer *transfer)
Stephen Streete0c99052006-03-07 23:53:24 -0800959{
Lubomir Rintel51eea522019-01-16 16:13:31 +0100960 struct driver_data *drv_data = spi_controller_get_devdata(controller);
961 struct spi_message *message = controller->cur_msg;
Jarkko Nikula20f4c372019-03-29 15:00:45 +0200962 struct chip_data *chip = spi_get_ctldata(spi);
Jarkko Nikula96579a42016-09-07 17:04:07 +0300963 u32 dma_thresh = chip->dma_threshold;
964 u32 dma_burst = chip->dma_burst_size;
965 u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
Jarkko Nikulabffc9672016-09-07 17:04:05 +0300966 u32 clk_div;
967 u8 bits;
968 u32 speed;
Stephen Street9708c122006-03-28 14:05:23 -0800969 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800970 u32 cr1;
Andy Shevchenko7d1f1bf2016-03-24 15:35:42 +0200971 int err;
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300972 int dma_mapped;
Stephen Streete0c99052006-03-07 23:53:24 -0800973
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200974 /* Check if we can DMA this transfer */
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300975 if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
Ned Forrester7e964452008-09-13 02:33:18 -0700976
Andy Shevchenko8083d6b2021-05-17 17:03:49 +0300977 /* Reject already-mapped transfers; PIO won't always work */
Ned Forrester7e964452008-09-13 02:33:18 -0700978 if (message->is_dma_mapped
979 || transfer->rx_dma || transfer->tx_dma) {
Jarkko Nikula748fbad2019-03-29 15:00:46 +0200980 dev_err(&spi->dev,
Jarkko Nikula8ae55af2018-04-17 17:20:01 +0300981 "Mapped transfer length of %u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700982 transfer->len, MAX_DMA_LEN);
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300983 return -EINVAL;
Ned Forrester7e964452008-09-13 02:33:18 -0700984 }
985
Andy Shevchenko8083d6b2021-05-17 17:03:49 +0300986 /* Warn ... we force this to PIO mode */
Jarkko Nikula20f4c372019-03-29 15:00:45 +0200987 dev_warn_ratelimited(&spi->dev,
Andy Shevchenko684a3ac2021-05-17 17:03:48 +0300988 "DMA disabled for transfer length %u greater than %d\n",
989 transfer->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800990 }
991
Stephen Streete0c99052006-03-07 23:53:24 -0800992 /* Setup the transfer state based on the type of transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200993 if (pxa2xx_spi_flush(drv_data) == 0) {
Jarkko Nikula748fbad2019-03-29 15:00:46 +0200994 dev_err(&spi->dev, "Flush failed\n");
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300995 return -EIO;
Stephen Streete0c99052006-03-07 23:53:24 -0800996 }
Stephen Street9708c122006-03-28 14:05:23 -0800997 drv_data->n_bytes = chip->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800998 drv_data->tx = (void *)transfer->tx_buf;
999 drv_data->tx_end = drv_data->tx + transfer->len;
1000 drv_data->rx = transfer->rx_buf;
1001 drv_data->rx_end = drv_data->rx + transfer->len;
Stephen Streete0c99052006-03-07 23:53:24 -08001002 drv_data->write = drv_data->tx ? chip->write : null_writer;
1003 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -08001004
1005 /* Change speed and bit per word on a per transfer */
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001006 bits = transfer->bits_per_word;
1007 speed = transfer->speed_hz;
Stephen Street9708c122006-03-28 14:05:23 -08001008
Andy Shevchenkod2c2f6a2015-10-22 16:44:40 +03001009 clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
Stephen Street9708c122006-03-28 14:05:23 -08001010
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001011 if (bits <= 8) {
1012 drv_data->n_bytes = 1;
1013 drv_data->read = drv_data->read != null_reader ?
1014 u8_reader : null_reader;
1015 drv_data->write = drv_data->write != null_writer ?
1016 u8_writer : null_writer;
1017 } else if (bits <= 16) {
1018 drv_data->n_bytes = 2;
1019 drv_data->read = drv_data->read != null_reader ?
1020 u16_reader : null_reader;
1021 drv_data->write = drv_data->write != null_writer ?
1022 u16_writer : null_writer;
1023 } else if (bits <= 32) {
1024 drv_data->n_bytes = 4;
1025 drv_data->read = drv_data->read != null_reader ?
1026 u32_reader : null_reader;
1027 drv_data->write = drv_data->write != null_writer ?
1028 u32_writer : null_writer;
Stephen Street9708c122006-03-28 14:05:23 -08001029 }
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001030 /*
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001031 * If bits per word is changed in DMA mode, then must check
1032 * the thresholds and burst also.
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001033 */
1034 if (chip->enable_dma) {
1035 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001036 spi,
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001037 bits, &dma_burst,
1038 &dma_thresh))
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001039 dev_warn_ratelimited(&spi->dev,
Jarkko Nikula8ae55af2018-04-17 17:20:01 +03001040 "DMA burst size reduced to match bits_per_word\n");
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001041 }
1042
Lubomir Rintel51eea522019-01-16 16:13:31 +01001043 dma_mapped = controller->can_dma &&
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001044 controller->can_dma(controller, spi, transfer) &&
Lubomir Rintel51eea522019-01-16 16:13:31 +01001045 controller->cur_msg_mapped;
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001046 if (dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -08001047
1048 /* Ensure we have the correct interrupt handler */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001049 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
Stephen Streete0c99052006-03-07 23:53:24 -08001050
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001051 err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1052 if (err)
1053 return err;
Stephen Streete0c99052006-03-07 23:53:24 -08001054
Stephen Street8d94cc52006-12-10 02:18:54 -08001055 /* Clear status and start DMA engine */
1056 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001057 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001058
1059 pxa2xx_spi_dma_start(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001060 } else {
1061 /* Ensure we have the correct interrupt handler */
1062 drv_data->transfer_handler = interrupt_transfer;
1063
Stephen Street8d94cc52006-12-10 02:18:54 -08001064 /* Clear status */
1065 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001066 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -08001067 }
1068
Jarkko Nikulaee036722016-01-26 15:33:21 +02001069 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1070 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1071 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001072 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
Lubomir Rintel51eea522019-01-16 16:13:31 +01001073 controller->max_speed_hz
Jarkko Nikulaee036722016-01-26 15:33:21 +02001074 / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001075 dma_mapped ? "DMA" : "PIO");
Jarkko Nikulaee036722016-01-26 15:33:21 +02001076 else
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001077 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
Lubomir Rintel51eea522019-01-16 16:13:31 +01001078 controller->max_speed_hz / 2
Jarkko Nikulaee036722016-01-26 15:33:21 +02001079 / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001080 dma_mapped ? "DMA" : "PIO");
Jarkko Nikulaee036722016-01-26 15:33:21 +02001081
Mika Westerberga0d26422013-01-22 12:26:32 +02001082 if (is_lpss_ssp(drv_data)) {
Andy Shevchenko1bed3782021-05-10 15:41:30 +03001083 pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold);
1084 pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold);
Mika Westerberga0d26422013-01-22 12:26:32 +02001085 }
1086
Andy Shevchenko3fdb59c2021-05-10 15:41:34 +03001087 if (is_mrfld_ssp(drv_data)) {
Andy Shevchenko70252442021-05-17 17:03:51 +03001088 u32 mask = SFIFOTT_RFT | SFIFOTT_TFT;
Andy Shevchenko3fdb59c2021-05-10 15:41:34 +03001089 u32 thresh = 0;
1090
1091 thresh |= SFIFOTT_RxThresh(chip->lpss_rx_threshold);
1092 thresh |= SFIFOTT_TxThresh(chip->lpss_tx_threshold);
1093
Andy Shevchenko70252442021-05-17 17:03:51 +03001094 pxa2xx_spi_update(drv_data, SFIFOTT, mask, thresh);
Andy Shevchenko3fdb59c2021-05-10 15:41:34 +03001095 }
1096
Andy Shevchenko1bed3782021-05-10 15:41:30 +03001097 if (is_quark_x1000_ssp(drv_data))
1098 pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate);
Weike Chene5262d02014-11-26 02:35:10 -08001099
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +03001100 /* Stop the SSP */
1101 if (!is_mmp2_ssp(drv_data))
1102 pxa_ssp_disable(drv_data->ssp);
1103
1104 if (!pxa25x_ssp_comp(drv_data))
1105 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1106
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001107 /* First set CR1 without interrupt and service enables */
Andy Shevchenko1bed3782021-05-10 15:41:30 +03001108 pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1);
1109
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001110 /* See if we need to reload the configuration registers */
Andy Shevchenko1bed3782021-05-10 15:41:30 +03001111 pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001112
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +03001113 /* Restart the SSP */
1114 pxa_ssp_enable(drv_data->ssp);
1115
Andy Shevchenko41c98842020-02-27 18:25:56 +02001116 if (is_mmp2_ssp(drv_data)) {
Andy Shevchenko6d380132021-05-10 15:41:32 +03001117 u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8;
Lubomir Rintel82391852018-11-13 11:22:28 +01001118
1119 if (tx_level) {
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001120 /* On MMP2, flipping SSE doesn't to empty Tx FIFO. */
Andy Shevchenko684a3ac2021-05-17 17:03:48 +03001121 dev_warn(&spi->dev, "%u bytes of garbage in Tx FIFO!\n", tx_level);
Lubomir Rintel82391852018-11-13 11:22:28 +01001122 if (tx_level > transfer->len)
1123 tx_level = transfer->len;
1124 drv_data->tx += tx_level;
1125 }
1126 }
1127
Lubomir Rintel51eea522019-01-16 16:13:31 +01001128 if (spi_controller_is_slave(controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001129 while (drv_data->write(drv_data))
1130 ;
Lubomir Rintel77d33892018-11-13 11:22:27 +01001131 if (drv_data->gpiod_ready) {
1132 gpiod_set_value(drv_data->gpiod_ready, 1);
1133 udelay(1);
1134 gpiod_set_value(drv_data->gpiod_ready, 0);
1135 }
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001136 }
1137
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001138 /*
1139 * Release the data by enabling service requests and interrupts,
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001140 * without changing any mode bits.
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001141 */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001142 pxa2xx_spi_write(drv_data, SSCR1, cr1);
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001143
1144 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -08001145}
1146
Lubomir Rintel51eea522019-01-16 16:13:31 +01001147static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001148{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001149 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001150
Andy Shevchenko4761d2e2021-05-10 15:41:28 +03001151 int_error_stop(drv_data, "transfer aborted", -EINTR);
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001152
1153 return 0;
1154}
1155
Lubomir Rintel51eea522019-01-16 16:13:31 +01001156static void pxa2xx_spi_handle_err(struct spi_controller *controller,
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001157 struct spi_message *msg)
Stephen Streete0c99052006-03-07 23:53:24 -08001158{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001159 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001160
Andy Shevchenko3bbdc082021-07-19 10:48:42 +03001161 int_stop_and_reset(drv_data);
1162
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001163 /* Disable the SSP */
Lubomir Rintel29d7e052020-01-18 10:40:31 +01001164 pxa2xx_spi_off(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001165
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001166 /*
1167 * Stop the DMA if running. Note DMA callback handler may have unset
1168 * the dma_running already, which is fine as stopping is not needed
1169 * then but we shouldn't rely this flag for anything else than
1170 * stopping. For instance to differentiate between PIO and DMA
1171 * transfers.
1172 */
1173 if (atomic_read(&drv_data->dma_running))
1174 pxa2xx_spi_dma_stop(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001175}
1176
Lubomir Rintel51eea522019-01-16 16:13:31 +01001177static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
Mika Westerberg7d94a502013-01-22 12:26:30 +02001178{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001179 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Mika Westerberg7d94a502013-01-22 12:26:30 +02001180
1181 /* Disable the SSP now */
Lubomir Rintel29d7e052020-01-18 10:40:31 +01001182 pxa2xx_spi_off(drv_data);
Mika Westerberg7d94a502013-01-22 12:26:30 +02001183
Mika Westerberg7d94a502013-01-22 12:26:30 +02001184 return 0;
1185}
1186
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001187static void cleanup_cs(struct spi_device *spi)
1188{
1189 if (!gpio_is_valid(spi->cs_gpio))
1190 return;
1191
1192 gpio_free(spi->cs_gpio);
1193 spi->cs_gpio = -ENOENT;
1194}
1195
Eric Miaoa7bb3902009-04-06 19:00:54 -07001196static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1197 struct pxa2xx_spi_chip *chip_info)
1198{
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001199 struct driver_data *drv_data = spi_controller_get_devdata(spi->controller);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001200
Mika Westerberg99f499c2016-09-26 15:19:50 +03001201 if (chip == NULL)
1202 return 0;
1203
Mika Westerberg99f499c2016-09-26 15:19:50 +03001204 if (chip_info == NULL)
Eric Miaoa7bb3902009-04-06 19:00:54 -07001205 return 0;
1206
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001207 if (drv_data->ssp_type == CE4100_SSP)
1208 return 0;
1209
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001210 /*
1211 * NOTE: setup() can be called multiple times, possibly with
1212 * different chip_info, release previously requested GPIO.
Eric Miaoa7bb3902009-04-06 19:00:54 -07001213 */
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001214 cleanup_cs(spi);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001215
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001216 /* If ->cs_control() is provided, ignore GPIO chip select */
Eric Miaoa7bb3902009-04-06 19:00:54 -07001217 if (chip_info->cs_control) {
1218 chip->cs_control = chip_info->cs_control;
1219 return 0;
1220 }
1221
1222 if (gpio_is_valid(chip_info->gpio_cs)) {
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001223 int gpio = chip_info->gpio_cs;
1224 int err;
1225
1226 err = gpio_request(gpio, "SPI_CS");
Eric Miaoa7bb3902009-04-06 19:00:54 -07001227 if (err) {
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001228 dev_err(&spi->dev, "failed to request chip select GPIO%d\n", gpio);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001229 return err;
1230 }
1231
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001232 err = gpio_direction_output(gpio, !(spi->mode & SPI_CS_HIGH));
1233 if (err) {
1234 gpio_free(gpio);
1235 return err;
1236 }
Eric Miaoa7bb3902009-04-06 19:00:54 -07001237
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001238 spi->cs_gpio = gpio;
Eric Miaoa7bb3902009-04-06 19:00:54 -07001239 }
1240
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001241 return 0;
Eric Miaoa7bb3902009-04-06 19:00:54 -07001242}
1243
Stephen Streete0c99052006-03-07 23:53:24 -08001244static int setup(struct spi_device *spi)
1245{
Jarkko Nikulabffc9672016-09-07 17:04:05 +03001246 struct pxa2xx_spi_chip *chip_info;
Stephen Streete0c99052006-03-07 23:53:24 -08001247 struct chip_data *chip;
Jarkko Nikuladccf7362015-06-04 16:55:11 +03001248 const struct lpss_config *config;
Jarkko Nikula3cc7b0e2018-02-01 17:17:30 +02001249 struct driver_data *drv_data =
1250 spi_controller_get_devdata(spi->controller);
Mika Westerberga0d26422013-01-22 12:26:32 +02001251 uint tx_thres, tx_hi_thres, rx_thres;
Lukas Wunner2ec6f202021-05-27 23:10:56 +02001252 int err;
Mika Westerberga0d26422013-01-22 12:26:32 +02001253
Weike Chene5262d02014-11-26 02:35:10 -08001254 switch (drv_data->ssp_type) {
1255 case QUARK_X1000_SSP:
1256 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1257 tx_hi_thres = 0;
1258 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1259 break;
Andy Shevchenko3fdb59c2021-05-10 15:41:34 +03001260 case MRFLD_SSP:
1261 tx_thres = TX_THRESH_MRFLD_DFLT;
1262 tx_hi_thres = 0;
1263 rx_thres = RX_THRESH_MRFLD_DFLT;
1264 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001265 case CE4100_SSP:
1266 tx_thres = TX_THRESH_CE4100_DFLT;
1267 tx_hi_thres = 0;
1268 rx_thres = RX_THRESH_CE4100_DFLT;
1269 break;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001270 case LPSS_LPT_SSP:
1271 case LPSS_BYT_SSP:
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001272 case LPSS_BSW_SSP:
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001273 case LPSS_SPT_SSP:
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +02001274 case LPSS_BXT_SSP:
Jarkko Nikulafc0b2ac2017-05-30 17:31:21 +03001275 case LPSS_CNL_SSP:
Jarkko Nikuladccf7362015-06-04 16:55:11 +03001276 config = lpss_get_config(drv_data);
1277 tx_thres = config->tx_threshold_lo;
1278 tx_hi_thres = config->tx_threshold_hi;
1279 rx_thres = config->rx_threshold;
Weike Chene5262d02014-11-26 02:35:10 -08001280 break;
1281 default:
Mika Westerberga0d26422013-01-22 12:26:32 +02001282 tx_hi_thres = 0;
Lubomir Rintel51eea522019-01-16 16:13:31 +01001283 if (spi_controller_is_slave(drv_data->controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001284 tx_thres = 1;
1285 rx_thres = 2;
1286 } else {
1287 tx_thres = TX_THRESH_DFLT;
1288 rx_thres = RX_THRESH_DFLT;
1289 }
Weike Chene5262d02014-11-26 02:35:10 -08001290 break;
Mika Westerberga0d26422013-01-22 12:26:32 +02001291 }
Stephen Streete0c99052006-03-07 23:53:24 -08001292
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001293 /* Only allocate on the first setup */
Stephen Streete0c99052006-03-07 23:53:24 -08001294 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -08001295 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -08001296 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001297 if (!chip)
Stephen Streete0c99052006-03-07 23:53:24 -08001298 return -ENOMEM;
1299
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001300 if (drv_data->ssp_type == CE4100_SSP) {
1301 if (spi->chip_select > 4) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001302 dev_err(&spi->dev,
1303 "failed setup: cs number must not be > 4.\n");
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001304 kfree(chip);
1305 return -EINVAL;
1306 }
Jan Kiszkac18d9252017-08-03 13:40:32 +02001307 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001308 chip->enable_dma = drv_data->controller_info->enable_dma;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001309 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001310 }
1311
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001312 /*
1313 * Protocol drivers may change the chip settings, so...
1314 * if chip_info exists, use it.
1315 */
Stephen Street8d94cc52006-12-10 02:18:54 -08001316 chip_info = spi->controller_data;
1317
Stephen Streete0c99052006-03-07 23:53:24 -08001318 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -08001319 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001320 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001321 if (chip_info->timeout)
1322 chip->timeout = chip_info->timeout;
1323 if (chip_info->tx_threshold)
1324 tx_thres = chip_info->tx_threshold;
Mika Westerberga0d26422013-01-22 12:26:32 +02001325 if (chip_info->tx_hi_threshold)
1326 tx_hi_thres = chip_info->tx_hi_threshold;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001327 if (chip_info->rx_threshold)
1328 rx_thres = chip_info->rx_threshold;
Stephen Streete0c99052006-03-07 23:53:24 -08001329 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001330 if (chip_info->enable_loopback)
1331 chip->cr1 = SSCR1_LBM;
1332 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001333 if (spi_controller_is_slave(drv_data->controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001334 chip->cr1 |= SSCR1_SCFR;
1335 chip->cr1 |= SSCR1_SCLKDIR;
1336 chip->cr1 |= SSCR1_SFRMDIR;
1337 chip->cr1 |= SSCR1_SPH;
1338 }
Stephen Streete0c99052006-03-07 23:53:24 -08001339
Andy Shevchenko3fdb59c2021-05-10 15:41:34 +03001340 if (is_lpss_ssp(drv_data)) {
1341 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1342 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
1343 SSITF_TxHiThresh(tx_hi_thres);
1344 }
1345
1346 if (is_mrfld_ssp(drv_data)) {
1347 chip->lpss_rx_threshold = rx_thres;
1348 chip->lpss_tx_threshold = tx_thres;
1349 }
Mika Westerberga0d26422013-01-22 12:26:32 +02001350
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001351 /*
1352 * Set DMA burst and threshold outside of chip_info path so that if
1353 * chip_info goes away after setting chip->enable_dma, the burst and
1354 * threshold can still respond to changes in bits_per_word.
1355 */
Stephen Street8d94cc52006-12-10 02:18:54 -08001356 if (chip->enable_dma) {
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001357 /* Set up legal burst and threshold for DMA */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001358 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1359 spi->bits_per_word,
Stephen Street8d94cc52006-12-10 02:18:54 -08001360 &chip->dma_burst_size,
1361 &chip->dma_threshold)) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001362 dev_warn(&spi->dev,
1363 "in setup: DMA burst size reduced to match bits_per_word\n");
Stephen Street8d94cc52006-12-10 02:18:54 -08001364 }
Andy Shevchenko000c6af2019-03-19 17:48:43 +02001365 dev_dbg(&spi->dev,
1366 "in setup: DMA burst size set to %u\n",
1367 chip->dma_burst_size);
Stephen Street8d94cc52006-12-10 02:18:54 -08001368 }
1369
Weike Chene5262d02014-11-26 02:35:10 -08001370 switch (drv_data->ssp_type) {
1371 case QUARK_X1000_SSP:
1372 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1373 & QUARK_X1000_SSCR1_RFT)
1374 | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1375 & QUARK_X1000_SSCR1_TFT);
1376 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001377 case CE4100_SSP:
1378 chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
1379 (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
1380 break;
Weike Chene5262d02014-11-26 02:35:10 -08001381 default:
1382 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1383 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1384 break;
1385 }
1386
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -08001387 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
Andy Shevchenkoeb743ec2021-05-17 17:03:47 +03001388 chip->cr1 |= ((spi->mode & SPI_CPHA) ? SSCR1_SPH : 0) |
1389 ((spi->mode & SPI_CPOL) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001390
Mika Westerbergb8331722013-01-22 12:26:31 +02001391 if (spi->mode & SPI_LOOP)
1392 chip->cr1 |= SSCR1_LBM;
1393
Stephen Streete0c99052006-03-07 23:53:24 -08001394 if (spi->bits_per_word <= 8) {
1395 chip->n_bytes = 1;
Stephen Streete0c99052006-03-07 23:53:24 -08001396 chip->read = u8_reader;
1397 chip->write = u8_writer;
1398 } else if (spi->bits_per_word <= 16) {
1399 chip->n_bytes = 2;
Stephen Streete0c99052006-03-07 23:53:24 -08001400 chip->read = u16_reader;
1401 chip->write = u16_writer;
1402 } else if (spi->bits_per_word <= 32) {
Stephen Streete0c99052006-03-07 23:53:24 -08001403 chip->n_bytes = 4;
Stephen Streete0c99052006-03-07 23:53:24 -08001404 chip->read = u32_reader;
1405 chip->write = u32_writer;
Stephen Streete0c99052006-03-07 23:53:24 -08001406 }
Stephen Streete0c99052006-03-07 23:53:24 -08001407
1408 spi_set_ctldata(spi, chip);
1409
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001410 if (drv_data->ssp_type == CE4100_SSP)
1411 return 0;
1412
Lukas Wunner2ec6f202021-05-27 23:10:56 +02001413 err = setup_cs(spi, chip, chip_info);
1414 if (err)
1415 kfree(chip);
1416
1417 return err;
Stephen Streete0c99052006-03-07 23:53:24 -08001418}
1419
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001420static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001421{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001422 struct chip_data *chip = spi_get_ctldata(spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001423
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001424 cleanup_cs(spi);
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;
Andy Shevchenko778c12e2021-05-17 17:03:44 +03001673 int status;
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) {
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001690 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
Andy Shevchenko94acf802021-05-17 17:03:43 +03001709 controller->dev.of_node = dev->of_node;
1710 controller->dev.fwnode = dev->fwnode;
1711
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001712 /* The spi->mode bits understood by this driver: */
Lubomir Rintel51eea522019-01-16 16:13:31 +01001713 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
David Brownelle7db06b2009-06-17 16:26:04 -07001714
Lubomir Rintel51eea522019-01-16 16:13:31 +01001715 controller->bus_num = ssp->port_id;
1716 controller->dma_alignment = DMA_ALIGNMENT;
1717 controller->cleanup = cleanup;
1718 controller->setup = setup;
1719 controller->set_cs = pxa2xx_spi_set_cs;
1720 controller->transfer_one = pxa2xx_spi_transfer_one;
1721 controller->slave_abort = pxa2xx_spi_slave_abort;
1722 controller->handle_err = pxa2xx_spi_handle_err;
1723 controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
1724 controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
1725 controller->auto_runtime_pm = true;
1726 controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
Stephen Streete0c99052006-03-07 23:53:24 -08001727
eric miao2f1a74e2007-11-21 18:50:53 +08001728 drv_data->ssp_type = ssp->type;
Stephen Streete0c99052006-03-07 23:53:24 -08001729
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001730 if (pxa25x_ssp_comp(drv_data)) {
Weike Chene5262d02014-11-26 02:35:10 -08001731 switch (drv_data->ssp_type) {
1732 case QUARK_X1000_SSP:
Lubomir Rintel51eea522019-01-16 16:13:31 +01001733 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Weike Chene5262d02014-11-26 02:35:10 -08001734 break;
1735 default:
Lubomir Rintel51eea522019-01-16 16:13:31 +01001736 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Weike Chene5262d02014-11-26 02:35:10 -08001737 break;
1738 }
1739
Stephen Streete0c99052006-03-07 23:53:24 -08001740 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1741 drv_data->dma_cr1 = 0;
1742 drv_data->clear_sr = SSSR_ROR;
1743 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1744 } else {
Lubomir Rintel51eea522019-01-16 16:13:31 +01001745 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Stephen Streete0c99052006-03-07 23:53:24 -08001746 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
Mika Westerberg59288082013-01-22 12:26:29 +02001747 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
Stephen Streete0c99052006-03-07 23:53:24 -08001748 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001749 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1750 | SSSR_ROR | SSSR_TUR;
Stephen Streete0c99052006-03-07 23:53:24 -08001751 }
1752
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -08001753 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1754 drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001755 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001756 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Lubomir Rintel51eea522019-01-16 16:13:31 +01001757 goto out_error_controller_alloc;
Stephen Streete0c99052006-03-07 23:53:24 -08001758 }
1759
1760 /* Setup DMA if requested */
Stephen Streete0c99052006-03-07 23:53:24 -08001761 if (platform_info->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001762 status = pxa2xx_spi_dma_setup(drv_data);
1763 if (status) {
Flavio Suligoi8b57b112019-04-05 14:40:22 +02001764 dev_warn(dev, "no DMA channels available, using PIO\n");
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001765 platform_info->enable_dma = false;
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001766 } else {
Lubomir Rintel51eea522019-01-16 16:13:31 +01001767 controller->can_dma = pxa2xx_spi_can_dma;
Mark Brownbf9f7422019-02-20 17:58:18 +00001768 controller->max_dma_len = MAX_DMA_LEN;
Daniel Vetterb2662a12019-10-17 08:44:26 +02001769 controller->max_transfer_size =
1770 pxa2xx_spi_max_dma_transfer_size;
Stephen Streete0c99052006-03-07 23:53:24 -08001771 }
Stephen Streete0c99052006-03-07 23:53:24 -08001772 }
1773
1774 /* Enable SOC clock */
Tobias Jordan62bbc862018-04-30 16:30:06 +02001775 status = clk_prepare_enable(ssp->clk);
1776 if (status)
1777 goto out_error_dma_irq_alloc;
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001778
Lubomir Rintel51eea522019-01-16 16:13:31 +01001779 controller->max_speed_hz = clk_get_rate(ssp->clk);
Jarkko Nikula23cdddb2019-06-28 17:07:17 +03001780 /*
1781 * Set minimum speed for all other platforms than Intel Quark which is
1782 * able do under 1 Hz transfers.
1783 */
1784 if (!pxa25x_ssp_comp(drv_data))
1785 controller->min_speed_hz =
1786 DIV_ROUND_UP(controller->max_speed_hz, 4096);
1787 else if (!is_quark_x1000_ssp(drv_data))
1788 controller->min_speed_hz =
1789 DIV_ROUND_UP(controller->max_speed_hz, 512);
Stephen Streete0c99052006-03-07 23:53:24 -08001790
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +03001791 pxa_ssp_disable(ssp);
1792
Stephen Streete0c99052006-03-07 23:53:24 -08001793 /* Load default SSP configuration */
Weike Chene5262d02014-11-26 02:35:10 -08001794 switch (drv_data->ssp_type) {
1795 case QUARK_X1000_SSP:
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001796 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
1797 QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001798 pxa2xx_spi_write(drv_data, SSCR1, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001799
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001800 /* Using the Motorola SPI protocol and use 8 bit frame */
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001801 tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
1802 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001803 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001804 case CE4100_SSP:
1805 tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
1806 CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
1807 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1808 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1809 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Andy Shevchenkoa2dd8af2017-01-02 13:44:28 +02001810 break;
Weike Chene5262d02014-11-26 02:35:10 -08001811 default:
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001812
Lubomir Rintel51eea522019-01-16 16:13:31 +01001813 if (spi_controller_is_slave(controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001814 tmp = SSCR1_SCFR |
1815 SSCR1_SCLKDIR |
1816 SSCR1_SFRMDIR |
1817 SSCR1_RxTresh(2) |
1818 SSCR1_TxTresh(1) |
1819 SSCR1_SPH;
1820 } else {
1821 tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1822 SSCR1_TxTresh(TX_THRESH_DFLT);
1823 }
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001824 pxa2xx_spi_write(drv_data, SSCR1, tmp);
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001825 tmp = SSCR0_Motorola | SSCR0_DataSize(8);
Lubomir Rintel51eea522019-01-16 16:13:31 +01001826 if (!spi_controller_is_slave(controller))
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001827 tmp |= SSCR0_SCR(2);
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001828 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001829 break;
1830 }
1831
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001832 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001833 pxa2xx_spi_write(drv_data, SSTO, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001834
1835 if (!is_quark_x1000_ssp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001836 pxa2xx_spi_write(drv_data, SSPSP, 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001837
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02001838 if (is_lpss_ssp(drv_data)) {
1839 lpss_ssp_setup(drv_data);
1840 config = lpss_get_config(drv_data);
1841 if (config->reg_capabilities >= 0) {
1842 tmp = __lpss_ssp_read_priv(drv_data,
1843 config->reg_capabilities);
1844 tmp &= LPSS_CAPS_CS_EN_MASK;
1845 tmp >>= LPSS_CAPS_CS_EN_SHIFT;
1846 platform_info->num_chipselect = ffz(tmp);
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001847 } else if (config->cs_num) {
1848 platform_info->num_chipselect = config->cs_num;
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02001849 }
1850 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001851 controller->num_chipselect = platform_info->num_chipselect;
Andy Shevchenko778c12e2021-05-17 17:03:44 +03001852 controller->use_gpio_descriptors = true;
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001853
Lubomir Rintel77d33892018-11-13 11:22:27 +01001854 if (platform_info->is_slave) {
1855 drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
1856 "ready", GPIOD_OUT_LOW);
1857 if (IS_ERR(drv_data->gpiod_ready)) {
1858 status = PTR_ERR(drv_data->gpiod_ready);
1859 goto out_error_clock_enabled;
1860 }
1861 }
1862
Antonio Ospite836d1a222014-05-30 18:18:09 +02001863 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1864 pm_runtime_use_autosuspend(&pdev->dev);
1865 pm_runtime_set_active(&pdev->dev);
1866 pm_runtime_enable(&pdev->dev);
1867
Stephen Streete0c99052006-03-07 23:53:24 -08001868 /* Register with the SPI framework */
1869 platform_set_drvdata(pdev, drv_data);
Lukas Wunner32e5b572020-05-25 14:25:02 +02001870 status = spi_register_controller(controller);
Andy Shevchenkoeb743ec2021-05-17 17:03:47 +03001871 if (status) {
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001872 dev_err(&pdev->dev, "problem registering SPI controller\n");
Lubomir Rintel12742042019-07-19 14:27:13 +02001873 goto out_error_pm_runtime_enabled;
Stephen Streete0c99052006-03-07 23:53:24 -08001874 }
1875
1876 return status;
1877
Lubomir Rintel12742042019-07-19 14:27:13 +02001878out_error_pm_runtime_enabled:
Jarkko Nikulae2b714a2018-03-07 17:05:04 +02001879 pm_runtime_disable(&pdev->dev);
Lubomir Rintel12742042019-07-19 14:27:13 +02001880
1881out_error_clock_enabled:
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001882 clk_disable_unprepare(ssp->clk);
Tobias Jordan62bbc862018-04-30 16:30:06 +02001883
1884out_error_dma_irq_alloc:
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001885 pxa2xx_spi_dma_release(drv_data);
eric miao2f1a74e2007-11-21 18:50:53 +08001886 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001887
Lubomir Rintel51eea522019-01-16 16:13:31 +01001888out_error_controller_alloc:
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001889 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001890 return status;
1891}
1892
1893static int pxa2xx_spi_remove(struct platform_device *pdev)
1894{
1895 struct driver_data *drv_data = platform_get_drvdata(pdev);
Andy Shevchenko3d24b2a2020-02-24 17:45:56 +02001896 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001897
Mika Westerberg7d94a502013-01-22 12:26:30 +02001898 pm_runtime_get_sync(&pdev->dev);
1899
Lukas Wunner32e5b572020-05-25 14:25:02 +02001900 spi_unregister_controller(drv_data->controller);
1901
Stephen Streete0c99052006-03-07 23:53:24 -08001902 /* Disable the SSP at the peripheral and SOC level */
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +03001903 pxa_ssp_disable(ssp);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001904 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001905
1906 /* Release DMA */
Lubomir Rintel51eea522019-01-16 16:13:31 +01001907 if (drv_data->controller_info->enable_dma)
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001908 pxa2xx_spi_dma_release(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001909
Mika Westerberg7d94a502013-01-22 12:26:30 +02001910 pm_runtime_put_noidle(&pdev->dev);
1911 pm_runtime_disable(&pdev->dev);
1912
Stephen Streete0c99052006-03-07 23:53:24 -08001913 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001914 free_irq(ssp->irq, drv_data);
1915
1916 /* Release SSP */
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001917 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001918
Stephen Streete0c99052006-03-07 23:53:24 -08001919 return 0;
1920}
1921
Mika Westerberg382cebb2014-01-16 14:50:55 +02001922#ifdef CONFIG_PM_SLEEP
Mike Rapoport86d25932009-07-21 17:50:16 +03001923static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001924{
Mike Rapoport86d25932009-07-21 17:50:16 +03001925 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001926 struct ssp_device *ssp = drv_data->ssp;
Jarkko Nikulabffc9672016-09-07 17:04:05 +03001927 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001928
Lubomir Rintel51eea522019-01-16 16:13:31 +01001929 status = spi_controller_suspend(drv_data->controller);
Andy Shevchenkoeb743ec2021-05-17 17:03:47 +03001930 if (status)
Stephen Streete0c99052006-03-07 23:53:24 -08001931 return status;
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +03001932
1933 pxa_ssp_disable(ssp);
Dmitry Eremin-Solenikov2b9375b2014-11-06 14:08:29 +03001934
1935 if (!pm_runtime_suspended(dev))
1936 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001937
1938 return 0;
1939}
1940
Mike Rapoport86d25932009-07-21 17:50:16 +03001941static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001942{
Mike Rapoport86d25932009-07-21 17:50:16 +03001943 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001944 struct ssp_device *ssp = drv_data->ssp;
Jarkko Nikulabffc9672016-09-07 17:04:05 +03001945 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001946
1947 /* Enable the SSP clock */
Tobias Jordan62bbc862018-04-30 16:30:06 +02001948 if (!pm_runtime_suspended(dev)) {
1949 status = clk_prepare_enable(ssp->clk);
1950 if (status)
1951 return status;
1952 }
Stephen Streete0c99052006-03-07 23:53:24 -08001953
1954 /* Start the queue running */
Lubomir Rintel51eea522019-01-16 16:13:31 +01001955 return spi_controller_resume(drv_data->controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001956}
Mika Westerberg7d94a502013-01-22 12:26:30 +02001957#endif
1958
Rafael J. Wysockiec833052014-12-13 00:41:15 +01001959#ifdef CONFIG_PM
Mika Westerberg7d94a502013-01-22 12:26:30 +02001960static int pxa2xx_spi_runtime_suspend(struct device *dev)
1961{
1962 struct driver_data *drv_data = dev_get_drvdata(dev);
1963
1964 clk_disable_unprepare(drv_data->ssp->clk);
1965 return 0;
1966}
1967
1968static int pxa2xx_spi_runtime_resume(struct device *dev)
1969{
1970 struct driver_data *drv_data = dev_get_drvdata(dev);
Tobias Jordan62bbc862018-04-30 16:30:06 +02001971 int status;
Mika Westerberg7d94a502013-01-22 12:26:30 +02001972
Tobias Jordan62bbc862018-04-30 16:30:06 +02001973 status = clk_prepare_enable(drv_data->ssp->clk);
1974 return status;
Mika Westerberg7d94a502013-01-22 12:26:30 +02001975}
1976#endif
Mike Rapoport86d25932009-07-21 17:50:16 +03001977
Alexey Dobriyan47145212009-12-14 18:00:08 -08001978static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mika Westerberg7d94a502013-01-22 12:26:30 +02001979 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1980 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
1981 pxa2xx_spi_runtime_resume, NULL)
Mike Rapoport86d25932009-07-21 17:50:16 +03001982};
Stephen Streete0c99052006-03-07 23:53:24 -08001983
1984static struct platform_driver driver = {
1985 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001986 .name = "pxa2xx-spi",
Mike Rapoport86d25932009-07-21 17:50:16 +03001987 .pm = &pxa2xx_spi_pm_ops,
Mika Westerberga3496852013-01-22 12:26:33 +02001988 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001989 .of_match_table = of_match_ptr(pxa2xx_spi_of_match),
Stephen Streete0c99052006-03-07 23:53:24 -08001990 },
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001991 .probe = pxa2xx_spi_probe,
David Brownelld1e44d92007-10-16 01:27:46 -07001992 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08001993};
1994
1995static int __init pxa2xx_spi_init(void)
1996{
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001997 return platform_driver_register(&driver);
Stephen Streete0c99052006-03-07 23:53:24 -08001998}
Antonio Ospite5b61a742009-09-22 16:46:10 -07001999subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08002000
2001static void __exit pxa2xx_spi_exit(void)
2002{
2003 platform_driver_unregister(&driver);
2004}
2005module_exit(pxa2xx_spi_exit);
Flavio Suligoi51ebf6a2019-04-10 14:51:36 +02002006
2007MODULE_SOFTDEP("pre: dw_dmac");