blob: e88f86274eeb0943f448f1c7302d3b357a80d3d4 [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 driver_data *drv_data =
431 spi_controller_get_devdata(spi->controller);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700432
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800433 if (drv_data->ssp_type == CE4100_SSP) {
Andy Shevchenkoccd60b22021-05-17 17:03:46 +0300434 pxa2xx_spi_write(drv_data, SSSR, spi->chip_select);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800435 return;
436 }
437
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200438 if (is_lpss_ssp(drv_data))
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300439 lpss_ssp_cs_control(spi, true);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700440}
441
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300442static void cs_deassert(struct spi_device *spi)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700443{
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300444 struct driver_data *drv_data =
445 spi_controller_get_devdata(spi->controller);
Jarkko Nikula104e51a2018-02-09 16:31:07 +0200446 unsigned long timeout;
Eric Miaoa7bb3902009-04-06 19:00:54 -0700447
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800448 if (drv_data->ssp_type == CE4100_SSP)
449 return;
450
Jarkko Nikula104e51a2018-02-09 16:31:07 +0200451 /* Wait until SSP becomes idle before deasserting the CS */
452 timeout = jiffies + msecs_to_jiffies(10);
453 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
454 !time_after(jiffies, timeout))
455 cpu_relax();
456
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200457 if (is_lpss_ssp(drv_data))
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300458 lpss_ssp_cs_control(spi, false);
459}
460
461static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
462{
463 if (level)
464 cs_deassert(spi);
465 else
466 cs_assert(spi);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700467}
468
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200469int pxa2xx_spi_flush(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800470{
471 unsigned long limit = loops_per_jiffy << 1;
472
Stephen Streete0c99052006-03-07 23:53:24 -0800473 do {
Andy Shevchenko6d380132021-05-10 15:41:32 +0300474 while (read_SSSR_bits(drv_data, SSSR_RNE))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200475 pxa2xx_spi_read(drv_data, SSDR);
476 } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800477 write_SSSR_CS(drv_data, SSSR_ROR);
Stephen Streete0c99052006-03-07 23:53:24 -0800478
479 return limit;
480}
481
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100482static void pxa2xx_spi_off(struct driver_data *drv_data)
483{
Andy Shevchenko41c98842020-02-27 18:25:56 +0200484 /* On MMP, disabling SSE seems to corrupt the Rx FIFO */
485 if (is_mmp2_ssp(drv_data))
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100486 return;
487
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +0300488 pxa_ssp_disable(drv_data->ssp);
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100489}
490
Stephen Street8d94cc52006-12-10 02:18:54 -0800491static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800492{
Stephen Street9708c122006-03-28 14:05:23 -0800493 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800494
Weike Chen4fdb2422014-10-08 08:50:22 -0700495 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800496 || (drv_data->tx == drv_data->tx_end))
497 return 0;
498
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200499 pxa2xx_spi_write(drv_data, SSDR, 0);
Stephen Street8d94cc52006-12-10 02:18:54 -0800500 drv_data->tx += n_bytes;
501
502 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800503}
504
Stephen Street8d94cc52006-12-10 02:18:54 -0800505static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800506{
Stephen Street9708c122006-03-28 14:05:23 -0800507 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800508
Andy Shevchenko6d380132021-05-10 15:41:32 +0300509 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200510 pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800511 drv_data->rx += n_bytes;
512 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800513
514 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800515}
516
Stephen Street8d94cc52006-12-10 02:18:54 -0800517static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800518{
Weike Chen4fdb2422014-10-08 08:50:22 -0700519 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800520 || (drv_data->tx == drv_data->tx_end))
521 return 0;
522
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200523 pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800524 ++drv_data->tx;
525
526 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800527}
528
Stephen Street8d94cc52006-12-10 02:18:54 -0800529static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800530{
Andy Shevchenko6d380132021-05-10 15:41:32 +0300531 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200532 *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800533 ++drv_data->rx;
534 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800535
536 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800537}
538
Stephen Street8d94cc52006-12-10 02:18:54 -0800539static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800540{
Weike Chen4fdb2422014-10-08 08:50:22 -0700541 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800542 || (drv_data->tx == drv_data->tx_end))
543 return 0;
544
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200545 pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800546 drv_data->tx += 2;
547
548 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800549}
550
Stephen Street8d94cc52006-12-10 02:18:54 -0800551static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800552{
Andy Shevchenko6d380132021-05-10 15:41:32 +0300553 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200554 *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800555 drv_data->rx += 2;
556 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800557
558 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800559}
Stephen Street8d94cc52006-12-10 02:18:54 -0800560
561static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800562{
Weike Chen4fdb2422014-10-08 08:50:22 -0700563 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800564 || (drv_data->tx == drv_data->tx_end))
565 return 0;
566
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200567 pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800568 drv_data->tx += 4;
569
570 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800571}
572
Stephen Street8d94cc52006-12-10 02:18:54 -0800573static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800574{
Andy Shevchenko6d380132021-05-10 15:41:32 +0300575 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200576 *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800577 drv_data->rx += 4;
578 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800579
580 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800581}
582
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800583static void reset_sccr1(struct driver_data *drv_data)
584{
Andy Shevchenkoe3aa9ac2021-07-21 15:15:20 +0300585 u32 mask = drv_data->int_cr1 | drv_data->dma_cr1, threshold;
586 struct chip_data *chip;
587
588 if (drv_data->controller->cur_msg) {
589 chip = spi_get_ctldata(drv_data->controller->cur_msg->spi);
590 threshold = chip->threshold;
591 } else {
592 threshold = 0;
593 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800594
Andy Shevchenko152bc192016-07-06 12:08:11 +0300595 switch (drv_data->ssp_type) {
596 case QUARK_X1000_SSP:
Andy Shevchenkoe0a65122021-07-19 10:48:40 +0300597 mask |= QUARK_X1000_SSCR1_RFT;
Andy Shevchenko152bc192016-07-06 12:08:11 +0300598 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300599 case CE4100_SSP:
Andy Shevchenkoe0a65122021-07-19 10:48:40 +0300600 mask |= CE4100_SSCR1_RFT;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +0300601 break;
Andy Shevchenko152bc192016-07-06 12:08:11 +0300602 default:
Andy Shevchenkoe0a65122021-07-19 10:48:40 +0300603 mask |= SSCR1_RFT;
Andy Shevchenko152bc192016-07-06 12:08:11 +0300604 break;
605 }
Andy Shevchenkoe0a65122021-07-19 10:48:40 +0300606
Andy Shevchenkoe3aa9ac2021-07-21 15:15:20 +0300607 pxa2xx_spi_update(drv_data, SSCR1, mask, threshold);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800608}
609
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300610static void int_stop_and_reset(struct driver_data *drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800611{
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300612 /* Clear and disable interrupts */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800613 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800614 reset_sccr1(drv_data);
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300615 if (pxa25x_ssp_comp(drv_data))
616 return;
617
618 pxa2xx_spi_write(drv_data, SSTO, 0);
619}
620
Andy Shevchenko4761d2e2021-05-10 15:41:28 +0300621static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300622{
623 int_stop_and_reset(drv_data);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200624 pxa2xx_spi_flush(drv_data);
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100625 pxa2xx_spi_off(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800626
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300627 dev_err(drv_data->ssp->dev, "%s\n", msg);
Stephen Street8d94cc52006-12-10 02:18:54 -0800628
Andy Shevchenko4761d2e2021-05-10 15:41:28 +0300629 drv_data->controller->cur_msg->status = err;
Lubomir Rintel51eea522019-01-16 16:13:31 +0100630 spi_finalize_current_transfer(drv_data->controller);
Stephen Street8d94cc52006-12-10 02:18:54 -0800631}
632
633static void int_transfer_complete(struct driver_data *drv_data)
634{
Andy Shevchenkoab77fe82021-05-10 15:41:27 +0300635 int_stop_and_reset(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800636
Lubomir Rintel51eea522019-01-16 16:13:31 +0100637 spi_finalize_current_transfer(drv_data->controller);
Stephen Street8d94cc52006-12-10 02:18:54 -0800638}
639
Stephen Streete0c99052006-03-07 23:53:24 -0800640static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
641{
Andy Shevchenko6d380132021-05-10 15:41:32 +0300642 u32 irq_status;
Stephen Street8d94cc52006-12-10 02:18:54 -0800643
Andy Shevchenko6d380132021-05-10 15:41:32 +0300644 irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr);
645 if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE))
646 irq_status &= ~SSSR_TFS;
Stephen Streete0c99052006-03-07 23:53:24 -0800647
Stephen Street8d94cc52006-12-10 02:18:54 -0800648 if (irq_status & SSSR_ROR) {
Andy Shevchenko8083d6b2021-05-17 17:03:49 +0300649 int_error_stop(drv_data, "interrupt_transfer: FIFO overrun", -EIO);
Stephen Street8d94cc52006-12-10 02:18:54 -0800650 return IRQ_HANDLED;
651 }
Stephen Streete0c99052006-03-07 23:53:24 -0800652
Lubomir Rintelec93cb62018-11-13 11:22:25 +0100653 if (irq_status & SSSR_TUR) {
Andy Shevchenko8083d6b2021-05-17 17:03:49 +0300654 int_error_stop(drv_data, "interrupt_transfer: FIFO underrun", -EIO);
Lubomir Rintelec93cb62018-11-13 11:22:25 +0100655 return IRQ_HANDLED;
656 }
657
Stephen Street8d94cc52006-12-10 02:18:54 -0800658 if (irq_status & SSSR_TINT) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200659 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
Stephen Street8d94cc52006-12-10 02:18:54 -0800660 if (drv_data->read(drv_data)) {
661 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800662 return IRQ_HANDLED;
663 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800664 }
Stephen Streete0c99052006-03-07 23:53:24 -0800665
Andy Shevchenko8083d6b2021-05-17 17:03:49 +0300666 /* Drain Rx FIFO, Fill Tx FIFO and prevent overruns */
Stephen Street8d94cc52006-12-10 02:18:54 -0800667 do {
668 if (drv_data->read(drv_data)) {
669 int_transfer_complete(drv_data);
670 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800671 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800672 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800673
Stephen Street8d94cc52006-12-10 02:18:54 -0800674 if (drv_data->read(drv_data)) {
675 int_transfer_complete(drv_data);
676 return IRQ_HANDLED;
677 }
Stephen Streete0c99052006-03-07 23:53:24 -0800678
Stephen Street8d94cc52006-12-10 02:18:54 -0800679 if (drv_data->tx == drv_data->tx_end) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800680 u32 bytes_left;
681 u32 sccr1_reg;
682
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200683 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800684 sccr1_reg &= ~SSCR1_TIE;
685
686 /*
Andy Shevchenko8083d6b2021-05-17 17:03:49 +0300687 * PXA25x_SSP has no timeout, set up Rx threshold for
688 * the remaining Rx bytes.
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800689 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800690 if (pxa25x_ssp_comp(drv_data)) {
Weike Chen4fdb2422014-10-08 08:50:22 -0700691 u32 rx_thre;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800692
Weike Chen4fdb2422014-10-08 08:50:22 -0700693 pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800694
695 bytes_left = drv_data->rx_end - drv_data->rx;
696 switch (drv_data->n_bytes) {
697 case 4:
Gustavo A. R. Silva2c183372018-10-03 17:55:22 +0200698 bytes_left >>= 2;
699 break;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800700 case 2:
701 bytes_left >>= 1;
Gustavo A. R. Silva2c183372018-10-03 17:55:22 +0200702 break;
Stephen Street8d94cc52006-12-10 02:18:54 -0800703 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800704
Weike Chen4fdb2422014-10-08 08:50:22 -0700705 rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
706 if (rx_thre > bytes_left)
707 rx_thre = bytes_left;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800708
Weike Chen4fdb2422014-10-08 08:50:22 -0700709 pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
Stephen Streete0c99052006-03-07 23:53:24 -0800710 }
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200711 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800712 }
713
Stephen Street5daa3ba2006-05-20 15:00:19 -0700714 /* We did something */
715 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800716}
717
Jan Kiszkab0312482017-01-16 19:44:54 +0100718static void handle_bad_msg(struct driver_data *drv_data)
719{
Andy Shevchenko3bbdc082021-07-19 10:48:42 +0300720 int_stop_and_reset(drv_data);
Lubomir Rintel29d7e052020-01-18 10:40:31 +0100721 pxa2xx_spi_off(drv_data);
Jan Kiszkab0312482017-01-16 19:44:54 +0100722
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300723 dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
Jan Kiszkab0312482017-01-16 19:44:54 +0100724}
725
David Howells7d12e782006-10-05 14:55:46 +0100726static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800727{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400728 struct driver_data *drv_data = dev_id;
Mika Westerberg7d94a502013-01-22 12:26:30 +0200729 u32 sccr1_reg;
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800730 u32 mask = drv_data->mask_sr;
731 u32 status;
732
Mika Westerberg7d94a502013-01-22 12:26:30 +0200733 /*
734 * The IRQ might be shared with other peripherals so we must first
735 * check that are we RPM suspended or not. If we are we assume that
736 * the IRQ was not for us (we shouldn't be RPM suspended when the
737 * interrupt is enabled).
738 */
Andy Shevchenkoc3dce242021-04-23 21:24:30 +0300739 if (pm_runtime_suspended(drv_data->ssp->dev))
Mika Westerberg7d94a502013-01-22 12:26:30 +0200740 return IRQ_NONE;
741
Mika Westerberg269e4a42013-09-04 13:37:43 +0300742 /*
743 * If the device is not yet in RPM suspended state and we get an
744 * interrupt that is meant for another device, check if status bits
745 * are all set to one. That means that the device is already
746 * powered off.
747 */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200748 status = pxa2xx_spi_read(drv_data, SSSR);
Mika Westerberg269e4a42013-09-04 13:37:43 +0300749 if (status == ~0)
750 return IRQ_NONE;
751
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200752 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800753
754 /* Ignore possible writes if we don't need to write */
755 if (!(sccr1_reg & SSCR1_TIE))
756 mask &= ~SSSR_TFS;
757
Tan, Jui Nee02bc9332015-09-01 10:22:51 +0800758 /* Ignore RX timeout interrupt if it is disabled */
759 if (!(sccr1_reg & SSCR1_TINTE))
760 mask &= ~SSSR_TINT;
761
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800762 if (!(status & mask))
763 return IRQ_NONE;
Stephen Streete0c99052006-03-07 23:53:24 -0800764
Jan Kiszkae51e9b92017-01-21 10:06:38 +0100765 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
766 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
767
Lubomir Rintel51eea522019-01-16 16:13:31 +0100768 if (!drv_data->controller->cur_msg) {
Jan Kiszkab0312482017-01-16 19:44:54 +0100769 handle_bad_msg(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800770 /* Never fail */
771 return IRQ_HANDLED;
772 }
773
774 return drv_data->transfer_handler(drv_data);
775}
776
Weike Chene5262d02014-11-26 02:35:10 -0800777/*
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200778 * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
779 * input frequency by fractions of 2^24. It also has a divider by 5.
780 *
781 * There are formulas to get baud rate value for given input frequency and
782 * divider parameters, such as DDS_CLK_RATE and SCR:
783 *
784 * Fsys = 200MHz
785 *
786 * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
787 * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
788 *
789 * DDS_CLK_RATE either 2^n or 2^n / 5.
790 * SCR is in range 0 .. 255
791 *
792 * Divisor = 5^i * 2^j * 2 * k
793 * i = [0, 1] i = 1 iff j = 0 or j > 3
794 * j = [0, 23] j = 0 iff i = 1
795 * k = [1, 256]
796 * Special case: j = 0, i = 1: Divisor = 2 / 5
797 *
798 * Accordingly to the specification the recommended values for DDS_CLK_RATE
799 * are:
800 * Case 1: 2^n, n = [0, 23]
801 * Case 2: 2^24 * 2 / 5 (0x666666)
802 * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
803 *
804 * In all cases the lowest possible value is better.
805 *
806 * The function calculates parameters for all cases and chooses the one closest
807 * to the asked baud rate.
Weike Chene5262d02014-11-26 02:35:10 -0800808 */
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200809static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
Weike Chene5262d02014-11-26 02:35:10 -0800810{
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200811 unsigned long xtal = 200000000;
812 unsigned long fref = xtal / 2; /* mandatory division by 2,
813 see (2) */
814 /* case 3 */
815 unsigned long fref1 = fref / 2; /* case 1 */
816 unsigned long fref2 = fref * 2 / 5; /* case 2 */
817 unsigned long scale;
818 unsigned long q, q1, q2;
819 long r, r1, r2;
820 u32 mul;
Weike Chene5262d02014-11-26 02:35:10 -0800821
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200822 /* Case 1 */
823
824 /* Set initial value for DDS_CLK_RATE */
825 mul = (1 << 24) >> 1;
826
827 /* Calculate initial quot */
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300828 q1 = DIV_ROUND_UP(fref1, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200829
830 /* Scale q1 if it's too big */
831 if (q1 > 256) {
832 /* Scale q1 to range [1, 512] */
833 scale = fls_long(q1 - 1);
834 if (scale > 9) {
835 q1 >>= scale - 9;
836 mul >>= scale - 9;
837 }
838
839 /* Round the result if we have a remainder */
840 q1 += q1 & 1;
841 }
842
843 /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
844 scale = __ffs(q1);
845 q1 >>= scale;
846 mul >>= scale;
847
848 /* Get the remainder */
849 r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
850
851 /* Case 2 */
852
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300853 q2 = DIV_ROUND_UP(fref2, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200854 r2 = abs(fref2 / q2 - rate);
855
856 /*
857 * Choose the best between two: less remainder we have the better. We
858 * can't go case 2 if q2 is greater than 256 since SCR register can
859 * hold only values 0 .. 255.
860 */
861 if (r2 >= r1 || q2 > 256) {
862 /* case 1 is better */
863 r = r1;
864 q = q1;
865 } else {
866 /* case 2 is better */
867 r = r2;
868 q = q2;
869 mul = (1 << 24) * 2 / 5;
870 }
871
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300872 /* Check case 3 only if the divisor is big enough */
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200873 if (fref / rate >= 80) {
874 u64 fssp;
875 u32 m;
876
877 /* Calculate initial quot */
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300878 q1 = DIV_ROUND_UP(fref, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200879 m = (1 << 24) / q1;
880
881 /* Get the remainder */
882 fssp = (u64)fref * m;
883 do_div(fssp, 1 << 24);
884 r1 = abs(fssp - rate);
885
886 /* Choose this one if it suits better */
887 if (r1 < r) {
888 /* case 3 is better */
889 q = 1;
890 mul = m;
Weike Chene5262d02014-11-26 02:35:10 -0800891 }
892 }
893
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200894 *dds = mul;
895 return q - 1;
Weike Chene5262d02014-11-26 02:35:10 -0800896}
897
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200898static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
eric miao2f1a74e2007-11-21 18:50:53 +0800899{
Lubomir Rintel51eea522019-01-16 16:13:31 +0100900 unsigned long ssp_clk = drv_data->controller->max_speed_hz;
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200901 const struct ssp_device *ssp = drv_data->ssp;
902
903 rate = min_t(int, ssp_clk, rate);
eric miao2f1a74e2007-11-21 18:50:53 +0800904
Flavio Suligoi29f21332019-04-12 09:32:19 +0200905 /*
906 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
Andy Shevchenko8083d6b2021-05-17 17:03:49 +0300907 * that the SSP transmission rate can be greater than the device rate.
Flavio Suligoi29f21332019-04-12 09:32:19 +0200908 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800909 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
Flavio Suligoi29f21332019-04-12 09:32:19 +0200910 return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
eric miao2f1a74e2007-11-21 18:50:53 +0800911 else
Flavio Suligoi29f21332019-04-12 09:32:19 +0200912 return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff;
eric miao2f1a74e2007-11-21 18:50:53 +0800913}
914
Weike Chene5262d02014-11-26 02:35:10 -0800915static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
Andy Shevchenkod2c2f6a2015-10-22 16:44:40 +0300916 int rate)
Weike Chene5262d02014-11-26 02:35:10 -0800917{
Jarkko Nikula96579a42016-09-07 17:04:07 +0300918 struct chip_data *chip =
Lubomir Rintel51eea522019-01-16 16:13:31 +0100919 spi_get_ctldata(drv_data->controller->cur_msg->spi);
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200920 unsigned int clk_div;
Weike Chene5262d02014-11-26 02:35:10 -0800921
922 switch (drv_data->ssp_type) {
923 case QUARK_X1000_SSP:
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200924 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300925 break;
Weike Chene5262d02014-11-26 02:35:10 -0800926 default:
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200927 clk_div = ssp_get_clk_div(drv_data, rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300928 break;
Weike Chene5262d02014-11-26 02:35:10 -0800929 }
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200930 return clk_div << 8;
Weike Chene5262d02014-11-26 02:35:10 -0800931}
932
Lubomir Rintel51eea522019-01-16 16:13:31 +0100933static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300934 struct spi_device *spi,
935 struct spi_transfer *xfer)
936{
937 struct chip_data *chip = spi_get_ctldata(spi);
938
939 return chip->enable_dma &&
940 xfer->len <= MAX_DMA_LEN &&
941 xfer->len >= chip->dma_burst_size;
942}
943
Lubomir Rintel51eea522019-01-16 16:13:31 +0100944static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
kbuild test robot71293a62018-04-18 03:53:23 +0800945 struct spi_device *spi,
946 struct spi_transfer *transfer)
Stephen Streete0c99052006-03-07 23:53:24 -0800947{
Lubomir Rintel51eea522019-01-16 16:13:31 +0100948 struct driver_data *drv_data = spi_controller_get_devdata(controller);
949 struct spi_message *message = controller->cur_msg;
Jarkko Nikula20f4c372019-03-29 15:00:45 +0200950 struct chip_data *chip = spi_get_ctldata(spi);
Jarkko Nikula96579a42016-09-07 17:04:07 +0300951 u32 dma_thresh = chip->dma_threshold;
952 u32 dma_burst = chip->dma_burst_size;
953 u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
Jarkko Nikulabffc9672016-09-07 17:04:05 +0300954 u32 clk_div;
955 u8 bits;
956 u32 speed;
Stephen Street9708c122006-03-28 14:05:23 -0800957 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800958 u32 cr1;
Andy Shevchenko7d1f1bf2016-03-24 15:35:42 +0200959 int err;
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300960 int dma_mapped;
Stephen Streete0c99052006-03-07 23:53:24 -0800961
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200962 /* Check if we can DMA this transfer */
Jarkko Nikulab6ced292016-06-21 13:21:34 +0300963 if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
Ned Forrester7e964452008-09-13 02:33:18 -0700964
Andy Shevchenko8083d6b2021-05-17 17:03:49 +0300965 /* Reject already-mapped transfers; PIO won't always work */
Ned Forrester7e964452008-09-13 02:33:18 -0700966 if (message->is_dma_mapped
967 || transfer->rx_dma || transfer->tx_dma) {
Jarkko Nikula748fbad2019-03-29 15:00:46 +0200968 dev_err(&spi->dev,
Jarkko Nikula8ae55af2018-04-17 17:20:01 +0300969 "Mapped transfer length of %u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700970 transfer->len, MAX_DMA_LEN);
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300971 return -EINVAL;
Ned Forrester7e964452008-09-13 02:33:18 -0700972 }
973
Andy Shevchenko8083d6b2021-05-17 17:03:49 +0300974 /* Warn ... we force this to PIO mode */
Jarkko Nikula20f4c372019-03-29 15:00:45 +0200975 dev_warn_ratelimited(&spi->dev,
Andy Shevchenko684a3ac2021-05-17 17:03:48 +0300976 "DMA disabled for transfer length %u greater than %d\n",
977 transfer->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800978 }
979
Stephen Streete0c99052006-03-07 23:53:24 -0800980 /* Setup the transfer state based on the type of transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200981 if (pxa2xx_spi_flush(drv_data) == 0) {
Jarkko Nikula748fbad2019-03-29 15:00:46 +0200982 dev_err(&spi->dev, "Flush failed\n");
Jarkko Nikulad5898e12018-04-17 17:20:02 +0300983 return -EIO;
Stephen Streete0c99052006-03-07 23:53:24 -0800984 }
Stephen Streete0c99052006-03-07 23:53:24 -0800985 drv_data->tx = (void *)transfer->tx_buf;
986 drv_data->tx_end = drv_data->tx + transfer->len;
987 drv_data->rx = transfer->rx_buf;
988 drv_data->rx_end = drv_data->rx + transfer->len;
Stephen Street9708c122006-03-28 14:05:23 -0800989
990 /* Change speed and bit per word on a per transfer */
Jarkko Nikula196b0e22015-09-15 16:26:27 +0300991 bits = transfer->bits_per_word;
992 speed = transfer->speed_hz;
Stephen Street9708c122006-03-28 14:05:23 -0800993
Andy Shevchenkod2c2f6a2015-10-22 16:44:40 +0300994 clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
Stephen Street9708c122006-03-28 14:05:23 -0800995
Jarkko Nikula196b0e22015-09-15 16:26:27 +0300996 if (bits <= 8) {
997 drv_data->n_bytes = 1;
Andy Shevchenko44ec41b2021-11-22 22:06:22 +0200998 drv_data->read = drv_data->rx ? u8_reader : null_reader;
999 drv_data->write = drv_data->tx ? u8_writer : null_writer;
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001000 } else if (bits <= 16) {
1001 drv_data->n_bytes = 2;
Andy Shevchenko44ec41b2021-11-22 22:06:22 +02001002 drv_data->read = drv_data->rx ? u16_reader : null_reader;
1003 drv_data->write = drv_data->tx ? u16_writer : null_writer;
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001004 } else if (bits <= 32) {
1005 drv_data->n_bytes = 4;
Andy Shevchenko44ec41b2021-11-22 22:06:22 +02001006 drv_data->read = drv_data->rx ? u32_reader : null_reader;
1007 drv_data->write = drv_data->tx ? u32_writer : null_writer;
Stephen Street9708c122006-03-28 14:05:23 -08001008 }
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001009 /*
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001010 * If bits per word is changed in DMA mode, then must check
1011 * the thresholds and burst also.
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001012 */
1013 if (chip->enable_dma) {
1014 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001015 spi,
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001016 bits, &dma_burst,
1017 &dma_thresh))
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001018 dev_warn_ratelimited(&spi->dev,
Jarkko Nikula8ae55af2018-04-17 17:20:01 +03001019 "DMA burst size reduced to match bits_per_word\n");
Jarkko Nikula196b0e22015-09-15 16:26:27 +03001020 }
1021
Lubomir Rintel51eea522019-01-16 16:13:31 +01001022 dma_mapped = controller->can_dma &&
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001023 controller->can_dma(controller, spi, transfer) &&
Lubomir Rintel51eea522019-01-16 16:13:31 +01001024 controller->cur_msg_mapped;
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001025 if (dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -08001026
1027 /* Ensure we have the correct interrupt handler */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001028 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
Stephen Streete0c99052006-03-07 23:53:24 -08001029
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001030 err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1031 if (err)
1032 return err;
Stephen Streete0c99052006-03-07 23:53:24 -08001033
Stephen Street8d94cc52006-12-10 02:18:54 -08001034 /* Clear status and start DMA engine */
1035 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001036 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001037
1038 pxa2xx_spi_dma_start(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001039 } else {
1040 /* Ensure we have the correct interrupt handler */
1041 drv_data->transfer_handler = interrupt_transfer;
1042
Stephen Street8d94cc52006-12-10 02:18:54 -08001043 /* Clear status */
1044 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001045 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -08001046 }
1047
Jarkko Nikulaee036722016-01-26 15:33:21 +02001048 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1049 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1050 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001051 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
Lubomir Rintel51eea522019-01-16 16:13:31 +01001052 controller->max_speed_hz
Jarkko Nikulaee036722016-01-26 15:33:21 +02001053 / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001054 dma_mapped ? "DMA" : "PIO");
Jarkko Nikulaee036722016-01-26 15:33:21 +02001055 else
Jarkko Nikula20f4c372019-03-29 15:00:45 +02001056 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
Lubomir Rintel51eea522019-01-16 16:13:31 +01001057 controller->max_speed_hz / 2
Jarkko Nikulaee036722016-01-26 15:33:21 +02001058 / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001059 dma_mapped ? "DMA" : "PIO");
Jarkko Nikulaee036722016-01-26 15:33:21 +02001060
Mika Westerberga0d26422013-01-22 12:26:32 +02001061 if (is_lpss_ssp(drv_data)) {
Andy Shevchenko1bed3782021-05-10 15:41:30 +03001062 pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold);
1063 pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold);
Mika Westerberga0d26422013-01-22 12:26:32 +02001064 }
1065
Andy Shevchenko3fdb59c2021-05-10 15:41:34 +03001066 if (is_mrfld_ssp(drv_data)) {
Andy Shevchenko70252442021-05-17 17:03:51 +03001067 u32 mask = SFIFOTT_RFT | SFIFOTT_TFT;
Andy Shevchenko3fdb59c2021-05-10 15:41:34 +03001068 u32 thresh = 0;
1069
1070 thresh |= SFIFOTT_RxThresh(chip->lpss_rx_threshold);
1071 thresh |= SFIFOTT_TxThresh(chip->lpss_tx_threshold);
1072
Andy Shevchenko70252442021-05-17 17:03:51 +03001073 pxa2xx_spi_update(drv_data, SFIFOTT, mask, thresh);
Andy Shevchenko3fdb59c2021-05-10 15:41:34 +03001074 }
1075
Andy Shevchenko1bed3782021-05-10 15:41:30 +03001076 if (is_quark_x1000_ssp(drv_data))
1077 pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate);
Weike Chene5262d02014-11-26 02:35:10 -08001078
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +03001079 /* Stop the SSP */
1080 if (!is_mmp2_ssp(drv_data))
1081 pxa_ssp_disable(drv_data->ssp);
1082
1083 if (!pxa25x_ssp_comp(drv_data))
1084 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1085
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001086 /* First set CR1 without interrupt and service enables */
Andy Shevchenko1bed3782021-05-10 15:41:30 +03001087 pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1);
1088
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001089 /* See if we need to reload the configuration registers */
Andy Shevchenko1bed3782021-05-10 15:41:30 +03001090 pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001091
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +03001092 /* Restart the SSP */
1093 pxa_ssp_enable(drv_data->ssp);
1094
Andy Shevchenko41c98842020-02-27 18:25:56 +02001095 if (is_mmp2_ssp(drv_data)) {
Andy Shevchenko6d380132021-05-10 15:41:32 +03001096 u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8;
Lubomir Rintel82391852018-11-13 11:22:28 +01001097
1098 if (tx_level) {
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001099 /* On MMP2, flipping SSE doesn't to empty Tx FIFO. */
Andy Shevchenko684a3ac2021-05-17 17:03:48 +03001100 dev_warn(&spi->dev, "%u bytes of garbage in Tx FIFO!\n", tx_level);
Lubomir Rintel82391852018-11-13 11:22:28 +01001101 if (tx_level > transfer->len)
1102 tx_level = transfer->len;
1103 drv_data->tx += tx_level;
1104 }
1105 }
1106
Lubomir Rintel51eea522019-01-16 16:13:31 +01001107 if (spi_controller_is_slave(controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001108 while (drv_data->write(drv_data))
1109 ;
Lubomir Rintel77d33892018-11-13 11:22:27 +01001110 if (drv_data->gpiod_ready) {
1111 gpiod_set_value(drv_data->gpiod_ready, 1);
1112 udelay(1);
1113 gpiod_set_value(drv_data->gpiod_ready, 0);
1114 }
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001115 }
1116
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001117 /*
1118 * Release the data by enabling service requests and interrupts,
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001119 * without changing any mode bits.
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001120 */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001121 pxa2xx_spi_write(drv_data, SSCR1, cr1);
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001122
1123 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -08001124}
1125
Lubomir Rintel51eea522019-01-16 16:13:31 +01001126static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001127{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001128 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001129
Andy Shevchenko4761d2e2021-05-10 15:41:28 +03001130 int_error_stop(drv_data, "transfer aborted", -EINTR);
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001131
1132 return 0;
1133}
1134
Lubomir Rintel51eea522019-01-16 16:13:31 +01001135static void pxa2xx_spi_handle_err(struct spi_controller *controller,
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001136 struct spi_message *msg)
Stephen Streete0c99052006-03-07 23:53:24 -08001137{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001138 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001139
Andy Shevchenko3bbdc082021-07-19 10:48:42 +03001140 int_stop_and_reset(drv_data);
1141
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001142 /* Disable the SSP */
Lubomir Rintel29d7e052020-01-18 10:40:31 +01001143 pxa2xx_spi_off(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001144
Jarkko Nikulad5898e12018-04-17 17:20:02 +03001145 /*
1146 * Stop the DMA if running. Note DMA callback handler may have unset
1147 * the dma_running already, which is fine as stopping is not needed
1148 * then but we shouldn't rely this flag for anything else than
1149 * stopping. For instance to differentiate between PIO and DMA
1150 * transfers.
1151 */
1152 if (atomic_read(&drv_data->dma_running))
1153 pxa2xx_spi_dma_stop(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001154}
1155
Lubomir Rintel51eea522019-01-16 16:13:31 +01001156static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
Mika Westerberg7d94a502013-01-22 12:26:30 +02001157{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001158 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Mika Westerberg7d94a502013-01-22 12:26:30 +02001159
1160 /* Disable the SSP now */
Lubomir Rintel29d7e052020-01-18 10:40:31 +01001161 pxa2xx_spi_off(drv_data);
Mika Westerberg7d94a502013-01-22 12:26:30 +02001162
Mika Westerberg7d94a502013-01-22 12:26:30 +02001163 return 0;
1164}
1165
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001166static void cleanup_cs(struct spi_device *spi)
1167{
1168 if (!gpio_is_valid(spi->cs_gpio))
1169 return;
1170
1171 gpio_free(spi->cs_gpio);
1172 spi->cs_gpio = -ENOENT;
1173}
1174
Eric Miaoa7bb3902009-04-06 19:00:54 -07001175static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1176 struct pxa2xx_spi_chip *chip_info)
1177{
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001178 struct driver_data *drv_data = spi_controller_get_devdata(spi->controller);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001179
Mika Westerberg99f499c2016-09-26 15:19:50 +03001180 if (chip == NULL)
1181 return 0;
1182
Mika Westerberg99f499c2016-09-26 15:19:50 +03001183 if (chip_info == NULL)
Eric Miaoa7bb3902009-04-06 19:00:54 -07001184 return 0;
1185
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001186 if (drv_data->ssp_type == CE4100_SSP)
1187 return 0;
1188
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001189 /*
1190 * NOTE: setup() can be called multiple times, possibly with
1191 * different chip_info, release previously requested GPIO.
Eric Miaoa7bb3902009-04-06 19:00:54 -07001192 */
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001193 cleanup_cs(spi);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001194
Eric Miaoa7bb3902009-04-06 19:00:54 -07001195 if (gpio_is_valid(chip_info->gpio_cs)) {
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001196 int gpio = chip_info->gpio_cs;
1197 int err;
1198
1199 err = gpio_request(gpio, "SPI_CS");
Eric Miaoa7bb3902009-04-06 19:00:54 -07001200 if (err) {
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001201 dev_err(&spi->dev, "failed to request chip select GPIO%d\n", gpio);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001202 return err;
1203 }
1204
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001205 err = gpio_direction_output(gpio, !(spi->mode & SPI_CS_HIGH));
1206 if (err) {
1207 gpio_free(gpio);
1208 return err;
1209 }
Eric Miaoa7bb3902009-04-06 19:00:54 -07001210
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001211 spi->cs_gpio = gpio;
Eric Miaoa7bb3902009-04-06 19:00:54 -07001212 }
1213
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001214 return 0;
Eric Miaoa7bb3902009-04-06 19:00:54 -07001215}
1216
Stephen Streete0c99052006-03-07 23:53:24 -08001217static int setup(struct spi_device *spi)
1218{
Jarkko Nikulabffc9672016-09-07 17:04:05 +03001219 struct pxa2xx_spi_chip *chip_info;
Stephen Streete0c99052006-03-07 23:53:24 -08001220 struct chip_data *chip;
Jarkko Nikuladccf7362015-06-04 16:55:11 +03001221 const struct lpss_config *config;
Jarkko Nikula3cc7b0e2018-02-01 17:17:30 +02001222 struct driver_data *drv_data =
1223 spi_controller_get_devdata(spi->controller);
Mika Westerberga0d26422013-01-22 12:26:32 +02001224 uint tx_thres, tx_hi_thres, rx_thres;
Lukas Wunner2ec6f202021-05-27 23:10:56 +02001225 int err;
Mika Westerberga0d26422013-01-22 12:26:32 +02001226
Weike Chene5262d02014-11-26 02:35:10 -08001227 switch (drv_data->ssp_type) {
1228 case QUARK_X1000_SSP:
1229 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1230 tx_hi_thres = 0;
1231 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1232 break;
Andy Shevchenko3fdb59c2021-05-10 15:41:34 +03001233 case MRFLD_SSP:
1234 tx_thres = TX_THRESH_MRFLD_DFLT;
1235 tx_hi_thres = 0;
1236 rx_thres = RX_THRESH_MRFLD_DFLT;
1237 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001238 case CE4100_SSP:
1239 tx_thres = TX_THRESH_CE4100_DFLT;
1240 tx_hi_thres = 0;
1241 rx_thres = RX_THRESH_CE4100_DFLT;
1242 break;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001243 case LPSS_LPT_SSP:
1244 case LPSS_BYT_SSP:
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001245 case LPSS_BSW_SSP:
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001246 case LPSS_SPT_SSP:
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +02001247 case LPSS_BXT_SSP:
Jarkko Nikulafc0b2ac2017-05-30 17:31:21 +03001248 case LPSS_CNL_SSP:
Jarkko Nikuladccf7362015-06-04 16:55:11 +03001249 config = lpss_get_config(drv_data);
1250 tx_thres = config->tx_threshold_lo;
1251 tx_hi_thres = config->tx_threshold_hi;
1252 rx_thres = config->rx_threshold;
Weike Chene5262d02014-11-26 02:35:10 -08001253 break;
1254 default:
Mika Westerberga0d26422013-01-22 12:26:32 +02001255 tx_hi_thres = 0;
Lubomir Rintel51eea522019-01-16 16:13:31 +01001256 if (spi_controller_is_slave(drv_data->controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001257 tx_thres = 1;
1258 rx_thres = 2;
1259 } else {
1260 tx_thres = TX_THRESH_DFLT;
1261 rx_thres = RX_THRESH_DFLT;
1262 }
Weike Chene5262d02014-11-26 02:35:10 -08001263 break;
Mika Westerberga0d26422013-01-22 12:26:32 +02001264 }
Stephen Streete0c99052006-03-07 23:53:24 -08001265
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001266 /* Only allocate on the first setup */
Stephen Streete0c99052006-03-07 23:53:24 -08001267 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -08001268 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -08001269 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001270 if (!chip)
Stephen Streete0c99052006-03-07 23:53:24 -08001271 return -ENOMEM;
1272
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001273 if (drv_data->ssp_type == CE4100_SSP) {
1274 if (spi->chip_select > 4) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001275 dev_err(&spi->dev,
1276 "failed setup: cs number must not be > 4.\n");
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001277 kfree(chip);
1278 return -EINVAL;
1279 }
Jan Kiszkac18d9252017-08-03 13:40:32 +02001280 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001281 chip->enable_dma = drv_data->controller_info->enable_dma;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001282 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001283 }
1284
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001285 /*
1286 * Protocol drivers may change the chip settings, so...
1287 * if chip_info exists, use it.
1288 */
Stephen Street8d94cc52006-12-10 02:18:54 -08001289 chip_info = spi->controller_data;
1290
Stephen Streete0c99052006-03-07 23:53:24 -08001291 /* chip_info isn't always needed */
1292 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001293 if (chip_info->timeout)
1294 chip->timeout = chip_info->timeout;
1295 if (chip_info->tx_threshold)
1296 tx_thres = chip_info->tx_threshold;
Mika Westerberga0d26422013-01-22 12:26:32 +02001297 if (chip_info->tx_hi_threshold)
1298 tx_hi_thres = chip_info->tx_hi_threshold;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001299 if (chip_info->rx_threshold)
1300 rx_thres = chip_info->rx_threshold;
Stephen Streete0c99052006-03-07 23:53:24 -08001301 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001302 }
Andy Shevchenko83939612021-11-23 21:27:23 +02001303
1304 chip->cr1 = 0;
Lubomir Rintel51eea522019-01-16 16:13:31 +01001305 if (spi_controller_is_slave(drv_data->controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001306 chip->cr1 |= SSCR1_SCFR;
1307 chip->cr1 |= SSCR1_SCLKDIR;
1308 chip->cr1 |= SSCR1_SFRMDIR;
1309 chip->cr1 |= SSCR1_SPH;
1310 }
Stephen Streete0c99052006-03-07 23:53:24 -08001311
Andy Shevchenko3fdb59c2021-05-10 15:41:34 +03001312 if (is_lpss_ssp(drv_data)) {
1313 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1314 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
1315 SSITF_TxHiThresh(tx_hi_thres);
1316 }
1317
1318 if (is_mrfld_ssp(drv_data)) {
1319 chip->lpss_rx_threshold = rx_thres;
1320 chip->lpss_tx_threshold = tx_thres;
1321 }
Mika Westerberga0d26422013-01-22 12:26:32 +02001322
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001323 /*
1324 * Set DMA burst and threshold outside of chip_info path so that if
1325 * chip_info goes away after setting chip->enable_dma, the burst and
1326 * threshold can still respond to changes in bits_per_word.
1327 */
Stephen Street8d94cc52006-12-10 02:18:54 -08001328 if (chip->enable_dma) {
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001329 /* Set up legal burst and threshold for DMA */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001330 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1331 spi->bits_per_word,
Stephen Street8d94cc52006-12-10 02:18:54 -08001332 &chip->dma_burst_size,
1333 &chip->dma_threshold)) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001334 dev_warn(&spi->dev,
1335 "in setup: DMA burst size reduced to match bits_per_word\n");
Stephen Street8d94cc52006-12-10 02:18:54 -08001336 }
Andy Shevchenko000c6af2019-03-19 17:48:43 +02001337 dev_dbg(&spi->dev,
1338 "in setup: DMA burst size set to %u\n",
1339 chip->dma_burst_size);
Stephen Street8d94cc52006-12-10 02:18:54 -08001340 }
1341
Weike Chene5262d02014-11-26 02:35:10 -08001342 switch (drv_data->ssp_type) {
1343 case QUARK_X1000_SSP:
1344 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1345 & QUARK_X1000_SSCR1_RFT)
1346 | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1347 & QUARK_X1000_SSCR1_TFT);
1348 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001349 case CE4100_SSP:
1350 chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
1351 (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
1352 break;
Weike Chene5262d02014-11-26 02:35:10 -08001353 default:
1354 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1355 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1356 break;
1357 }
1358
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -08001359 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
Andy Shevchenkoeb743ec2021-05-17 17:03:47 +03001360 chip->cr1 |= ((spi->mode & SPI_CPHA) ? SSCR1_SPH : 0) |
1361 ((spi->mode & SPI_CPOL) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001362
Mika Westerbergb8331722013-01-22 12:26:31 +02001363 if (spi->mode & SPI_LOOP)
1364 chip->cr1 |= SSCR1_LBM;
1365
Stephen Streete0c99052006-03-07 23:53:24 -08001366 spi_set_ctldata(spi, chip);
1367
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001368 if (drv_data->ssp_type == CE4100_SSP)
1369 return 0;
1370
Lukas Wunner2ec6f202021-05-27 23:10:56 +02001371 err = setup_cs(spi, chip, chip_info);
1372 if (err)
1373 kfree(chip);
1374
1375 return err;
Stephen Streete0c99052006-03-07 23:53:24 -08001376}
1377
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001378static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001379{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001380 struct chip_data *chip = spi_get_ctldata(spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001381
Andy Shevchenkode6926f2021-05-17 17:03:45 +03001382 cleanup_cs(spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001383 kfree(chip);
1384}
1385
Lee Jones9b2d6112020-07-17 14:54:23 +01001386#ifdef CONFIG_ACPI
Mathias Krause8422ddf2015-06-13 14:22:14 +02001387static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001388 { "INT33C0", LPSS_LPT_SSP },
1389 { "INT33C1", LPSS_LPT_SSP },
1390 { "INT3430", LPSS_LPT_SSP },
1391 { "INT3431", LPSS_LPT_SSP },
1392 { "80860F0E", LPSS_BYT_SSP },
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001393 { "8086228E", LPSS_BSW_SSP },
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001394 { },
1395};
1396MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
Lee Jones9b2d6112020-07-17 14:54:23 +01001397#endif
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001398
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001399/*
1400 * PCI IDs of compound devices that integrate both host controller and private
1401 * integrated DMA engine. Please note these are not used in module
1402 * autoloading and probing in this module but matching the LPSS SSP type.
1403 */
1404static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
1405 /* SPT-LP */
1406 { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
1407 { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
1408 /* SPT-H */
1409 { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
1410 { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
Mika Westerberg704d2b02016-07-04 13:21:07 +03001411 /* KBL-H */
1412 { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1413 { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
Jarkko Nikula6157d4c2020-01-16 11:10:35 +02001414 /* CML-V */
1415 { PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
1416 { PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
Jarkko Nikulac1b03f12016-03-02 09:54:14 +02001417 /* BXT A-Step */
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +02001418 { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1419 { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1420 { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
Jarkko Nikulac1b03f12016-03-02 09:54:14 +02001421 /* BXT B-Step */
1422 { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1423 { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1424 { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
David E. Boxe18a80a2017-01-19 16:25:21 +02001425 /* GLK */
1426 { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1427 { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1428 { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
Mika Westerberg22d71a502018-06-28 13:52:23 +03001429 /* ICL-LP */
1430 { PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
1431 { PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
1432 { PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
Jarkko Nikula8cc77202019-07-03 14:46:03 +03001433 /* EHL */
1434 { PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
1435 { PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
1436 { PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
Jarkko Nikula9c7315c2019-11-25 14:51:59 +02001437 /* JSL */
1438 { PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
1439 { PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
1440 { PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
Jarkko Nikulacf961fc2020-06-25 17:00:41 +03001441 /* TGL-H */
1442 { PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
1443 { PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
1444 { PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
1445 { PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
Jarkko Nikulaa402e392021-01-14 16:40:21 +02001446 /* ADL-P */
1447 { PCI_VDEVICE(INTEL, 0x51aa), LPSS_CNL_SSP },
1448 { PCI_VDEVICE(INTEL, 0x51ab), LPSS_CNL_SSP },
1449 { PCI_VDEVICE(INTEL, 0x51fb), LPSS_CNL_SSP },
Jarkko Nikula8c4ffe42021-04-15 16:59:17 +03001450 /* ADL-M */
1451 { PCI_VDEVICE(INTEL, 0x54aa), LPSS_CNL_SSP },
1452 { PCI_VDEVICE(INTEL, 0x54ab), LPSS_CNL_SSP },
1453 { PCI_VDEVICE(INTEL, 0x54fb), LPSS_CNL_SSP },
Jarkko Nikulab7c08cf2015-10-28 15:13:42 +02001454 /* APL */
1455 { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1456 { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1457 { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
Jarkko Nikulab8450e02020-12-04 10:24:09 +02001458 /* ADL-S */
1459 { PCI_VDEVICE(INTEL, 0x7aaa), LPSS_CNL_SSP },
1460 { PCI_VDEVICE(INTEL, 0x7aab), LPSS_CNL_SSP },
1461 { PCI_VDEVICE(INTEL, 0x7af9), LPSS_CNL_SSP },
1462 { PCI_VDEVICE(INTEL, 0x7afb), LPSS_CNL_SSP },
Jarkko Nikulafc0b2ac2017-05-30 17:31:21 +03001463 /* CNL-LP */
1464 { PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1465 { PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1466 { PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1467 /* CNL-H */
1468 { PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1469 { PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1470 { PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
Evan Green41a91802019-04-15 20:27:43 -07001471 /* CML-LP */
1472 { PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
1473 { PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
1474 { PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
Jarkko Nikulaf0cf17e2019-10-29 13:58:02 +02001475 /* CML-H */
1476 { PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1477 { PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1478 { PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
Jarkko Nikulaa4127952019-08-01 16:49:01 +03001479 /* TGL-LP */
1480 { PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1481 { PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1482 { PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1483 { PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1484 { PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1485 { PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1486 { PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
Axel Lin94e5c232015-08-04 13:52:22 +08001487 { },
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001488};
1489
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001490static const struct of_device_id pxa2xx_spi_of_match[] = {
1491 { .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
1492 {},
1493};
1494MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
1495
1496#ifdef CONFIG_ACPI
1497
Andy Shevchenko365e8562019-10-18 13:54:27 +03001498static int pxa2xx_spi_get_port_id(struct device *dev)
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001499{
Andy Shevchenko365e8562019-10-18 13:54:27 +03001500 struct acpi_device *adev;
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001501 unsigned int devid;
1502 int port_id = -1;
1503
Andy Shevchenko365e8562019-10-18 13:54:27 +03001504 adev = ACPI_COMPANION(dev);
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001505 if (adev && adev->pnp.unique_id &&
1506 !kstrtouint(adev->pnp.unique_id, 0, &devid))
1507 port_id = devid;
1508 return port_id;
1509}
1510
1511#else /* !CONFIG_ACPI */
1512
Andy Shevchenko365e8562019-10-18 13:54:27 +03001513static int pxa2xx_spi_get_port_id(struct device *dev)
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001514{
1515 return -1;
1516}
1517
1518#endif /* CONFIG_ACPI */
1519
1520
1521#ifdef CONFIG_PCI
1522
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001523static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
1524{
Andy Shevchenko5ba846b2019-03-18 18:39:30 +03001525 return param == chan->device->dev;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001526}
1527
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001528#endif /* CONFIG_PCI */
1529
Lubomir Rintel51eea522019-01-16 16:13:31 +01001530static struct pxa2xx_spi_controller *
Jarkko Nikula0db64212015-10-28 15:13:43 +02001531pxa2xx_spi_init_pdata(struct platform_device *pdev)
Mika Westerberga3496852013-01-22 12:26:33 +02001532{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001533 struct pxa2xx_spi_controller *pdata;
Mika Westerberga3496852013-01-22 12:26:33 +02001534 struct ssp_device *ssp;
1535 struct resource *res;
Andy Shevchenko6fb74272019-10-21 13:36:24 +03001536 struct device *parent = pdev->dev.parent;
1537 struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001538 const struct pci_device_id *pcidev_id = NULL;
Lubomir Rintel55ef8262018-10-10 19:09:28 +02001539 enum pxa_ssp_type type;
Andy Shevchenkof2faa3e2019-10-18 13:54:28 +03001540 const void *match;
Mika Westerberga3496852013-01-22 12:26:33 +02001541
Andy Shevchenko6fb74272019-10-21 13:36:24 +03001542 if (pcidev)
1543 pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
Mika Westerberga3496852013-01-22 12:26:33 +02001544
Andy Shevchenkof2faa3e2019-10-18 13:54:28 +03001545 match = device_get_match_data(&pdev->dev);
1546 if (match)
1547 type = (enum pxa_ssp_type)match;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001548 else if (pcidev_id)
Lubomir Rintel55ef8262018-10-10 19:09:28 +02001549 type = (enum pxa_ssp_type)pcidev_id->driver_data;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001550 else
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001551 return ERR_PTR(-EINVAL);
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001552
Mika Westerbergcc0ee982013-06-20 17:44:22 +03001553 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001554 if (!pdata)
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001555 return ERR_PTR(-ENOMEM);
Mika Westerberga3496852013-01-22 12:26:33 +02001556
Mika Westerberga3496852013-01-22 12:26:33 +02001557 ssp = &pdata->ssp;
1558
Andy Shevchenko77c544d2019-10-21 13:36:25 +03001559 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sachin Kamatcbfd6a22013-04-08 15:49:33 +05301560 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1561 if (IS_ERR(ssp->mmio_base))
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001562 return ERR_CAST(ssp->mmio_base);
Mika Westerberga3496852013-01-22 12:26:33 +02001563
Andy Shevchenko77c544d2019-10-21 13:36:25 +03001564 ssp->phys_base = res->start;
1565
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001566#ifdef CONFIG_PCI
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001567 if (pcidev_id) {
Andy Shevchenko6fb74272019-10-21 13:36:24 +03001568 pdata->tx_param = parent;
1569 pdata->rx_param = parent;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001570 pdata->dma_filter = pxa2xx_spi_idma_filter;
1571 }
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001572#endif
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001573
Mika Westerberga3496852013-01-22 12:26:33 +02001574 ssp->clk = devm_clk_get(&pdev->dev, NULL);
Chuhong Yuan5eb263e2019-11-09 16:09:43 +08001575 if (IS_ERR(ssp->clk))
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001576 return ERR_CAST(ssp->clk);
Mika Westerberga3496852013-01-22 12:26:33 +02001577
Mika Westerberga3496852013-01-22 12:26:33 +02001578 ssp->irq = platform_get_irq(pdev, 0);
Chuhong Yuan5eb263e2019-11-09 16:09:43 +08001579 if (ssp->irq < 0)
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001580 return ERR_PTR(ssp->irq);
Chuhong Yuan5eb263e2019-11-09 16:09:43 +08001581
Mika Westerberga3496852013-01-22 12:26:33 +02001582 ssp->type = type;
Andy Shevchenko4f3d9572019-10-18 13:54:25 +03001583 ssp->dev = &pdev->dev;
Andy Shevchenko365e8562019-10-18 13:54:27 +03001584 ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
Mika Westerberga3496852013-01-22 12:26:33 +02001585
Andy Shevchenkof2faa3e2019-10-18 13:54:28 +03001586 pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
Mika Westerberga3496852013-01-22 12:26:33 +02001587 pdata->num_chipselect = 1;
Mika Westerbergcddb3392013-05-13 13:45:10 +03001588 pdata->enable_dma = true;
Andy Shevchenko37821a822019-03-19 17:48:42 +02001589 pdata->dma_burst_size = 1;
Mika Westerberga3496852013-01-22 12:26:33 +02001590
1591 return pdata;
1592}
1593
Lubomir Rintel51eea522019-01-16 16:13:31 +01001594static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
Jarkko Nikula3cc7b0e2018-02-01 17:17:30 +02001595 unsigned int cs)
Mika Westerberg0c27d9c2016-02-08 17:14:29 +02001596{
Lubomir Rintel51eea522019-01-16 16:13:31 +01001597 struct driver_data *drv_data = spi_controller_get_devdata(controller);
Mika Westerberg0c27d9c2016-02-08 17:14:29 +02001598
Andy Shevchenkoc3dce242021-04-23 21:24:30 +03001599 if (has_acpi_companion(drv_data->ssp->dev)) {
Mika Westerberg0c27d9c2016-02-08 17:14:29 +02001600 switch (drv_data->ssp_type) {
1601 /*
1602 * For Atoms the ACPI DeviceSelection used by the Windows
1603 * driver starts from 1 instead of 0 so translate it here
1604 * to match what Linux expects.
1605 */
1606 case LPSS_BYT_SSP:
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001607 case LPSS_BSW_SSP:
Mika Westerberg0c27d9c2016-02-08 17:14:29 +02001608 return cs - 1;
1609
1610 default:
1611 break;
1612 }
1613 }
1614
1615 return cs;
1616}
1617
Daniel Vetterb2662a12019-10-17 08:44:26 +02001618static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1619{
1620 return MAX_DMA_LEN;
1621}
1622
Grant Likelyfd4a3192012-12-07 16:57:14 +00001623static int pxa2xx_spi_probe(struct platform_device *pdev)
Stephen Streete0c99052006-03-07 23:53:24 -08001624{
1625 struct device *dev = &pdev->dev;
Lubomir Rintel51eea522019-01-16 16:13:31 +01001626 struct pxa2xx_spi_controller *platform_info;
1627 struct spi_controller *controller;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001628 struct driver_data *drv_data;
eric miao2f1a74e2007-11-21 18:50:53 +08001629 struct ssp_device *ssp;
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02001630 const struct lpss_config *config;
Andy Shevchenko778c12e2021-05-17 17:03:44 +03001631 int status;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001632 u32 tmp;
Stephen Streete0c99052006-03-07 23:53:24 -08001633
Mika Westerberg851bacf2013-01-07 12:44:33 +02001634 platform_info = dev_get_platdata(dev);
1635 if (!platform_info) {
Jarkko Nikula0db64212015-10-28 15:13:43 +02001636 platform_info = pxa2xx_spi_init_pdata(pdev);
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001637 if (IS_ERR(platform_info)) {
Mika Westerberga3496852013-01-22 12:26:33 +02001638 dev_err(&pdev->dev, "missing platform data\n");
Andy Shevchenko14af1df2020-02-24 17:45:55 +02001639 return PTR_ERR(platform_info);
Mika Westerberga3496852013-01-22 12:26:33 +02001640 }
Mika Westerberg851bacf2013-01-07 12:44:33 +02001641 }
Stephen Streete0c99052006-03-07 23:53:24 -08001642
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001643 ssp = pxa_ssp_request(pdev->id, pdev->name);
Mika Westerberg851bacf2013-01-07 12:44:33 +02001644 if (!ssp)
1645 ssp = &platform_info->ssp;
1646
1647 if (!ssp->mmio_base) {
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001648 dev_err(&pdev->dev, "failed to get SSP\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001649 return -ENODEV;
1650 }
1651
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001652 if (platform_info->is_slave)
Lukas Wunner56263082020-12-07 09:17:05 +01001653 controller = devm_spi_alloc_slave(dev, sizeof(*drv_data));
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001654 else
Lukas Wunner56263082020-12-07 09:17:05 +01001655 controller = devm_spi_alloc_master(dev, sizeof(*drv_data));
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001656
Lubomir Rintel51eea522019-01-16 16:13:31 +01001657 if (!controller) {
1658 dev_err(&pdev->dev, "cannot alloc spi_controller\n");
Andy Shevchenkof2eed8c2021-04-23 21:24:28 +03001659 status = -ENOMEM;
1660 goto out_error_controller_alloc;
Stephen Streete0c99052006-03-07 23:53:24 -08001661 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001662 drv_data = spi_controller_get_devdata(controller);
1663 drv_data->controller = controller;
1664 drv_data->controller_info = platform_info;
eric miao2f1a74e2007-11-21 18:50:53 +08001665 drv_data->ssp = ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001666
Andy Shevchenko12baee62021-12-22 17:57:39 +02001667 device_set_node(&controller->dev, dev_fwnode(dev));
Andy Shevchenko94acf802021-05-17 17:03:43 +03001668
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001669 /* The spi->mode bits understood by this driver: */
Lubomir Rintel51eea522019-01-16 16:13:31 +01001670 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
David Brownelle7db06b2009-06-17 16:26:04 -07001671
Lubomir Rintel51eea522019-01-16 16:13:31 +01001672 controller->bus_num = ssp->port_id;
1673 controller->dma_alignment = DMA_ALIGNMENT;
1674 controller->cleanup = cleanup;
1675 controller->setup = setup;
1676 controller->set_cs = pxa2xx_spi_set_cs;
1677 controller->transfer_one = pxa2xx_spi_transfer_one;
1678 controller->slave_abort = pxa2xx_spi_slave_abort;
1679 controller->handle_err = pxa2xx_spi_handle_err;
1680 controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
1681 controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
1682 controller->auto_runtime_pm = true;
1683 controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
Stephen Streete0c99052006-03-07 23:53:24 -08001684
eric miao2f1a74e2007-11-21 18:50:53 +08001685 drv_data->ssp_type = ssp->type;
Stephen Streete0c99052006-03-07 23:53:24 -08001686
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001687 if (pxa25x_ssp_comp(drv_data)) {
Weike Chene5262d02014-11-26 02:35:10 -08001688 switch (drv_data->ssp_type) {
1689 case QUARK_X1000_SSP:
Lubomir Rintel51eea522019-01-16 16:13:31 +01001690 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Weike Chene5262d02014-11-26 02:35:10 -08001691 break;
1692 default:
Lubomir Rintel51eea522019-01-16 16:13:31 +01001693 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Weike Chene5262d02014-11-26 02:35:10 -08001694 break;
1695 }
1696
Stephen Streete0c99052006-03-07 23:53:24 -08001697 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1698 drv_data->dma_cr1 = 0;
1699 drv_data->clear_sr = SSSR_ROR;
1700 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1701 } else {
Lubomir Rintel51eea522019-01-16 16:13:31 +01001702 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Stephen Streete0c99052006-03-07 23:53:24 -08001703 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
Mika Westerberg59288082013-01-22 12:26:29 +02001704 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
Stephen Streete0c99052006-03-07 23:53:24 -08001705 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001706 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1707 | SSSR_ROR | SSSR_TUR;
Stephen Streete0c99052006-03-07 23:53:24 -08001708 }
1709
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -08001710 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1711 drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001712 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001713 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Lubomir Rintel51eea522019-01-16 16:13:31 +01001714 goto out_error_controller_alloc;
Stephen Streete0c99052006-03-07 23:53:24 -08001715 }
1716
1717 /* Setup DMA if requested */
Stephen Streete0c99052006-03-07 23:53:24 -08001718 if (platform_info->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001719 status = pxa2xx_spi_dma_setup(drv_data);
1720 if (status) {
Flavio Suligoi8b57b112019-04-05 14:40:22 +02001721 dev_warn(dev, "no DMA channels available, using PIO\n");
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001722 platform_info->enable_dma = false;
Jarkko Nikulab6ced292016-06-21 13:21:34 +03001723 } else {
Lubomir Rintel51eea522019-01-16 16:13:31 +01001724 controller->can_dma = pxa2xx_spi_can_dma;
Mark Brownbf9f7422019-02-20 17:58:18 +00001725 controller->max_dma_len = MAX_DMA_LEN;
Daniel Vetterb2662a12019-10-17 08:44:26 +02001726 controller->max_transfer_size =
1727 pxa2xx_spi_max_dma_transfer_size;
Stephen Streete0c99052006-03-07 23:53:24 -08001728 }
Stephen Streete0c99052006-03-07 23:53:24 -08001729 }
1730
1731 /* Enable SOC clock */
Tobias Jordan62bbc862018-04-30 16:30:06 +02001732 status = clk_prepare_enable(ssp->clk);
1733 if (status)
1734 goto out_error_dma_irq_alloc;
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001735
Lubomir Rintel51eea522019-01-16 16:13:31 +01001736 controller->max_speed_hz = clk_get_rate(ssp->clk);
Jarkko Nikula23cdddb2019-06-28 17:07:17 +03001737 /*
1738 * Set minimum speed for all other platforms than Intel Quark which is
1739 * able do under 1 Hz transfers.
1740 */
1741 if (!pxa25x_ssp_comp(drv_data))
1742 controller->min_speed_hz =
1743 DIV_ROUND_UP(controller->max_speed_hz, 4096);
1744 else if (!is_quark_x1000_ssp(drv_data))
1745 controller->min_speed_hz =
1746 DIV_ROUND_UP(controller->max_speed_hz, 512);
Stephen Streete0c99052006-03-07 23:53:24 -08001747
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +03001748 pxa_ssp_disable(ssp);
1749
Stephen Streete0c99052006-03-07 23:53:24 -08001750 /* Load default SSP configuration */
Weike Chene5262d02014-11-26 02:35:10 -08001751 switch (drv_data->ssp_type) {
1752 case QUARK_X1000_SSP:
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001753 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
1754 QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001755 pxa2xx_spi_write(drv_data, SSCR1, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001756
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001757 /* Using the Motorola SPI protocol and use 8 bit frame */
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001758 tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
1759 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001760 break;
Andy Shevchenko7c7289a2016-09-07 15:43:22 +03001761 case CE4100_SSP:
1762 tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
1763 CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
1764 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1765 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1766 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Andy Shevchenkoa2dd8af2017-01-02 13:44:28 +02001767 break;
Weike Chene5262d02014-11-26 02:35:10 -08001768 default:
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001769
Lubomir Rintel51eea522019-01-16 16:13:31 +01001770 if (spi_controller_is_slave(controller)) {
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001771 tmp = SSCR1_SCFR |
1772 SSCR1_SCLKDIR |
1773 SSCR1_SFRMDIR |
1774 SSCR1_RxTresh(2) |
1775 SSCR1_TxTresh(1) |
1776 SSCR1_SPH;
1777 } else {
1778 tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1779 SSCR1_TxTresh(TX_THRESH_DFLT);
1780 }
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001781 pxa2xx_spi_write(drv_data, SSCR1, tmp);
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001782 tmp = SSCR0_Motorola | SSCR0_DataSize(8);
Lubomir Rintel51eea522019-01-16 16:13:31 +01001783 if (!spi_controller_is_slave(controller))
Lubomir Rintelec93cb62018-11-13 11:22:25 +01001784 tmp |= SSCR0_SCR(2);
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001785 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001786 break;
1787 }
1788
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001789 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001790 pxa2xx_spi_write(drv_data, SSTO, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001791
1792 if (!is_quark_x1000_ssp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001793 pxa2xx_spi_write(drv_data, SSPSP, 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001794
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02001795 if (is_lpss_ssp(drv_data)) {
1796 lpss_ssp_setup(drv_data);
1797 config = lpss_get_config(drv_data);
1798 if (config->reg_capabilities >= 0) {
1799 tmp = __lpss_ssp_read_priv(drv_data,
1800 config->reg_capabilities);
1801 tmp &= LPSS_CAPS_CS_EN_MASK;
1802 tmp >>= LPSS_CAPS_CS_EN_SHIFT;
1803 platform_info->num_chipselect = ffz(tmp);
Mika Westerberg30f3a6a2016-02-08 17:14:31 +02001804 } else if (config->cs_num) {
1805 platform_info->num_chipselect = config->cs_num;
Jarkko Nikula8b136ba2015-10-28 15:13:41 +02001806 }
1807 }
Lubomir Rintel51eea522019-01-16 16:13:31 +01001808 controller->num_chipselect = platform_info->num_chipselect;
Andy Shevchenko778c12e2021-05-17 17:03:44 +03001809 controller->use_gpio_descriptors = true;
Andy Shevchenko6ac5a432017-07-27 14:37:08 +03001810
Lubomir Rintel77d33892018-11-13 11:22:27 +01001811 if (platform_info->is_slave) {
1812 drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
1813 "ready", GPIOD_OUT_LOW);
1814 if (IS_ERR(drv_data->gpiod_ready)) {
1815 status = PTR_ERR(drv_data->gpiod_ready);
1816 goto out_error_clock_enabled;
1817 }
1818 }
1819
Antonio Ospite836d1a222014-05-30 18:18:09 +02001820 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1821 pm_runtime_use_autosuspend(&pdev->dev);
1822 pm_runtime_set_active(&pdev->dev);
1823 pm_runtime_enable(&pdev->dev);
1824
Stephen Streete0c99052006-03-07 23:53:24 -08001825 /* Register with the SPI framework */
1826 platform_set_drvdata(pdev, drv_data);
Lukas Wunner32e5b572020-05-25 14:25:02 +02001827 status = spi_register_controller(controller);
Andy Shevchenkoeb743ec2021-05-17 17:03:47 +03001828 if (status) {
Andy Shevchenko8083d6b2021-05-17 17:03:49 +03001829 dev_err(&pdev->dev, "problem registering SPI controller\n");
Lubomir Rintel12742042019-07-19 14:27:13 +02001830 goto out_error_pm_runtime_enabled;
Stephen Streete0c99052006-03-07 23:53:24 -08001831 }
1832
1833 return status;
1834
Lubomir Rintel12742042019-07-19 14:27:13 +02001835out_error_pm_runtime_enabled:
Jarkko Nikulae2b714a2018-03-07 17:05:04 +02001836 pm_runtime_disable(&pdev->dev);
Lubomir Rintel12742042019-07-19 14:27:13 +02001837
1838out_error_clock_enabled:
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001839 clk_disable_unprepare(ssp->clk);
Tobias Jordan62bbc862018-04-30 16:30:06 +02001840
1841out_error_dma_irq_alloc:
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001842 pxa2xx_spi_dma_release(drv_data);
eric miao2f1a74e2007-11-21 18:50:53 +08001843 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001844
Lubomir Rintel51eea522019-01-16 16:13:31 +01001845out_error_controller_alloc:
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001846 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001847 return status;
1848}
1849
1850static int pxa2xx_spi_remove(struct platform_device *pdev)
1851{
1852 struct driver_data *drv_data = platform_get_drvdata(pdev);
Andy Shevchenko3d24b2a2020-02-24 17:45:56 +02001853 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001854
Mika Westerberg7d94a502013-01-22 12:26:30 +02001855 pm_runtime_get_sync(&pdev->dev);
1856
Lukas Wunner32e5b572020-05-25 14:25:02 +02001857 spi_unregister_controller(drv_data->controller);
1858
Stephen Streete0c99052006-03-07 23:53:24 -08001859 /* Disable the SSP at the peripheral and SOC level */
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +03001860 pxa_ssp_disable(ssp);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001861 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001862
1863 /* Release DMA */
Lubomir Rintel51eea522019-01-16 16:13:31 +01001864 if (drv_data->controller_info->enable_dma)
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001865 pxa2xx_spi_dma_release(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001866
Mika Westerberg7d94a502013-01-22 12:26:30 +02001867 pm_runtime_put_noidle(&pdev->dev);
1868 pm_runtime_disable(&pdev->dev);
1869
Stephen Streete0c99052006-03-07 23:53:24 -08001870 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001871 free_irq(ssp->irq, drv_data);
1872
1873 /* Release SSP */
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001874 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001875
Stephen Streete0c99052006-03-07 23:53:24 -08001876 return 0;
1877}
1878
Mika Westerberg382cebb2014-01-16 14:50:55 +02001879#ifdef CONFIG_PM_SLEEP
Mike Rapoport86d25932009-07-21 17:50:16 +03001880static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001881{
Mike Rapoport86d25932009-07-21 17:50:16 +03001882 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001883 struct ssp_device *ssp = drv_data->ssp;
Jarkko Nikulabffc9672016-09-07 17:04:05 +03001884 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001885
Lubomir Rintel51eea522019-01-16 16:13:31 +01001886 status = spi_controller_suspend(drv_data->controller);
Andy Shevchenkoeb743ec2021-05-17 17:03:47 +03001887 if (status)
Stephen Streete0c99052006-03-07 23:53:24 -08001888 return status;
Andy Shevchenko0c8ccd82021-05-10 15:41:29 +03001889
1890 pxa_ssp_disable(ssp);
Dmitry Eremin-Solenikov2b9375b2014-11-06 14:08:29 +03001891
1892 if (!pm_runtime_suspended(dev))
1893 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001894
1895 return 0;
1896}
1897
Mike Rapoport86d25932009-07-21 17:50:16 +03001898static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001899{
Mike Rapoport86d25932009-07-21 17:50:16 +03001900 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001901 struct ssp_device *ssp = drv_data->ssp;
Jarkko Nikulabffc9672016-09-07 17:04:05 +03001902 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001903
1904 /* Enable the SSP clock */
Tobias Jordan62bbc862018-04-30 16:30:06 +02001905 if (!pm_runtime_suspended(dev)) {
1906 status = clk_prepare_enable(ssp->clk);
1907 if (status)
1908 return status;
1909 }
Stephen Streete0c99052006-03-07 23:53:24 -08001910
1911 /* Start the queue running */
Lubomir Rintel51eea522019-01-16 16:13:31 +01001912 return spi_controller_resume(drv_data->controller);
Stephen Streete0c99052006-03-07 23:53:24 -08001913}
Mika Westerberg7d94a502013-01-22 12:26:30 +02001914#endif
1915
Rafael J. Wysockiec833052014-12-13 00:41:15 +01001916#ifdef CONFIG_PM
Mika Westerberg7d94a502013-01-22 12:26:30 +02001917static int pxa2xx_spi_runtime_suspend(struct device *dev)
1918{
1919 struct driver_data *drv_data = dev_get_drvdata(dev);
1920
1921 clk_disable_unprepare(drv_data->ssp->clk);
1922 return 0;
1923}
1924
1925static int pxa2xx_spi_runtime_resume(struct device *dev)
1926{
1927 struct driver_data *drv_data = dev_get_drvdata(dev);
Tobias Jordan62bbc862018-04-30 16:30:06 +02001928 int status;
Mika Westerberg7d94a502013-01-22 12:26:30 +02001929
Tobias Jordan62bbc862018-04-30 16:30:06 +02001930 status = clk_prepare_enable(drv_data->ssp->clk);
1931 return status;
Mika Westerberg7d94a502013-01-22 12:26:30 +02001932}
1933#endif
Mike Rapoport86d25932009-07-21 17:50:16 +03001934
Alexey Dobriyan47145212009-12-14 18:00:08 -08001935static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mika Westerberg7d94a502013-01-22 12:26:30 +02001936 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1937 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
1938 pxa2xx_spi_runtime_resume, NULL)
Mike Rapoport86d25932009-07-21 17:50:16 +03001939};
Stephen Streete0c99052006-03-07 23:53:24 -08001940
1941static struct platform_driver driver = {
1942 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001943 .name = "pxa2xx-spi",
Mike Rapoport86d25932009-07-21 17:50:16 +03001944 .pm = &pxa2xx_spi_pm_ops,
Mika Westerberga3496852013-01-22 12:26:33 +02001945 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
Lubomir Rintel87ae1d22018-10-10 19:09:29 +02001946 .of_match_table = of_match_ptr(pxa2xx_spi_of_match),
Stephen Streete0c99052006-03-07 23:53:24 -08001947 },
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001948 .probe = pxa2xx_spi_probe,
David Brownelld1e44d92007-10-16 01:27:46 -07001949 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08001950};
1951
1952static int __init pxa2xx_spi_init(void)
1953{
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001954 return platform_driver_register(&driver);
Stephen Streete0c99052006-03-07 23:53:24 -08001955}
Antonio Ospite5b61a742009-09-22 16:46:10 -07001956subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08001957
1958static void __exit pxa2xx_spi_exit(void)
1959{
1960 platform_driver_unregister(&driver);
1961}
1962module_exit(pxa2xx_spi_exit);
Flavio Suligoi51ebf6a2019-04-10 14:51:36 +02001963
1964MODULE_SOFTDEP("pre: dw_dmac");