xref: /openbmc/linux/drivers/spi/spi-pxa2xx.c (revision 1bed378c6b9116c51ae59b970cf3d9b4e9e62ced)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
4  * Copyright (C) 2013, Intel Corporation
5  */
6 
7 #include <linux/acpi.h>
8 #include <linux/bitops.h>
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/dmaengine.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/gpio.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/of.h>
24 #include <linux/pci.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/property.h>
28 #include <linux/slab.h>
29 
30 #include <linux/spi/pxa2xx_spi.h>
31 #include <linux/spi/spi.h>
32 
33 #include "spi-pxa2xx.h"
34 
35 MODULE_AUTHOR("Stephen Street");
36 MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
37 MODULE_LICENSE("GPL");
38 MODULE_ALIAS("platform:pxa2xx-spi");
39 
40 #define TIMOUT_DFLT		1000
41 
42 /*
43  * for testing SSCR1 changes that require SSP restart, basically
44  * everything except the service and interrupt enables, the pxa270 developer
45  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
46  * list, but the PXA255 dev man says all bits without really meaning the
47  * service and interrupt enables
48  */
49 #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
50 				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
51 				| 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)
55 
56 #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 
62 #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 
69 #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)
72 #define LPSS_CAPS_CS_EN_SHIFT			9
73 #define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
74 
75 #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 
79 struct 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;
86 	int reg_capabilities;
87 	/* FIFO thresholds */
88 	u32 rx_threshold;
89 	u32 tx_threshold_lo;
90 	u32 tx_threshold_hi;
91 	/* Chip select control */
92 	unsigned cs_sel_shift;
93 	unsigned cs_sel_mask;
94 	unsigned cs_num;
95 	/* Quirks */
96 	unsigned cs_clk_stays_gated : 1;
97 };
98 
99 /* Keep these sorted with enum pxa_ssp_type */
100 static 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,
106 		.reg_capabilities = -1,
107 		.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,
116 		.reg_capabilities = -1,
117 		.rx_threshold = 64,
118 		.tx_threshold_lo = 160,
119 		.tx_threshold_hi = 224,
120 	},
121 	{	/* 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 	},
134 	{	/* LPSS_SPT_SSP */
135 		.offset = 0x200,
136 		.reg_general = -1,
137 		.reg_ssp = 0x20,
138 		.reg_cs_ctrl = 0x24,
139 		.reg_capabilities = -1,
140 		.rx_threshold = 1,
141 		.tx_threshold_lo = 32,
142 		.tx_threshold_hi = 56,
143 	},
144 	{	/* 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,
153 		.cs_sel_shift = 8,
154 		.cs_sel_mask = 3 << 8,
155 		.cs_clk_stays_gated = true,
156 	},
157 	{	/* 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,
168 		.cs_clk_stays_gated = true,
169 	},
170 };
171 
172 static 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 
178 static bool is_lpss_ssp(const struct driver_data *drv_data)
179 {
180 	switch (drv_data->ssp_type) {
181 	case LPSS_LPT_SSP:
182 	case LPSS_BYT_SSP:
183 	case LPSS_BSW_SSP:
184 	case LPSS_SPT_SSP:
185 	case LPSS_BXT_SSP:
186 	case LPSS_CNL_SSP:
187 		return true;
188 	default:
189 		return false;
190 	}
191 }
192 
193 static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
194 {
195 	return drv_data->ssp_type == QUARK_X1000_SSP;
196 }
197 
198 static bool is_mmp2_ssp(const struct driver_data *drv_data)
199 {
200 	return drv_data->ssp_type == MMP2_SSP;
201 }
202 
203 static void pxa2xx_spi_update(const struct driver_data *drv_data, u32 reg, u32 mask, u32 value)
204 {
205 	if ((pxa2xx_spi_read(drv_data, reg) & mask) != value)
206 		pxa2xx_spi_write(drv_data, reg, value & mask);
207 }
208 
209 static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
210 {
211 	switch (drv_data->ssp_type) {
212 	case QUARK_X1000_SSP:
213 		return QUARK_X1000_SSCR1_CHANGE_MASK;
214 	case CE4100_SSP:
215 		return CE4100_SSCR1_CHANGE_MASK;
216 	default:
217 		return SSCR1_CHANGE_MASK;
218 	}
219 }
220 
221 static u32
222 pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
223 {
224 	switch (drv_data->ssp_type) {
225 	case QUARK_X1000_SSP:
226 		return RX_THRESH_QUARK_X1000_DFLT;
227 	case CE4100_SSP:
228 		return RX_THRESH_CE4100_DFLT;
229 	default:
230 		return RX_THRESH_DFLT;
231 	}
232 }
233 
234 static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
235 {
236 	u32 mask;
237 
238 	switch (drv_data->ssp_type) {
239 	case QUARK_X1000_SSP:
240 		mask = QUARK_X1000_SSSR_TFL_MASK;
241 		break;
242 	case CE4100_SSP:
243 		mask = CE4100_SSSR_TFL_MASK;
244 		break;
245 	default:
246 		mask = SSSR_TFL_MASK;
247 		break;
248 	}
249 
250 	return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
251 }
252 
253 static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
254 				     u32 *sccr1_reg)
255 {
256 	u32 mask;
257 
258 	switch (drv_data->ssp_type) {
259 	case QUARK_X1000_SSP:
260 		mask = QUARK_X1000_SSCR1_RFT;
261 		break;
262 	case CE4100_SSP:
263 		mask = CE4100_SSCR1_RFT;
264 		break;
265 	default:
266 		mask = SSCR1_RFT;
267 		break;
268 	}
269 	*sccr1_reg &= ~mask;
270 }
271 
272 static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
273 				   u32 *sccr1_reg, u32 threshold)
274 {
275 	switch (drv_data->ssp_type) {
276 	case QUARK_X1000_SSP:
277 		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
278 		break;
279 	case CE4100_SSP:
280 		*sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
281 		break;
282 	default:
283 		*sccr1_reg |= SSCR1_RxTresh(threshold);
284 		break;
285 	}
286 }
287 
288 static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
289 				  u32 clk_div, u8 bits)
290 {
291 	switch (drv_data->ssp_type) {
292 	case QUARK_X1000_SSP:
293 		return clk_div
294 			| QUARK_X1000_SSCR0_Motorola
295 			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits);
296 	default:
297 		return clk_div
298 			| SSCR0_Motorola
299 			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
300 			| (bits > 16 ? SSCR0_EDSS : 0);
301 	}
302 }
303 
304 /*
305  * Read and write LPSS SSP private registers. Caller must first check that
306  * is_lpss_ssp() returns true before these can be called.
307  */
308 static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
309 {
310 	WARN_ON(!drv_data->lpss_base);
311 	return readl(drv_data->lpss_base + offset);
312 }
313 
314 static void __lpss_ssp_write_priv(struct driver_data *drv_data,
315 				  unsigned offset, u32 value)
316 {
317 	WARN_ON(!drv_data->lpss_base);
318 	writel(value, drv_data->lpss_base + offset);
319 }
320 
321 /*
322  * lpss_ssp_setup - perform LPSS SSP specific setup
323  * @drv_data: pointer to the driver private data
324  *
325  * Perform LPSS SSP specific setup. This function must be called first if
326  * one is going to use LPSS SSP private registers.
327  */
328 static void lpss_ssp_setup(struct driver_data *drv_data)
329 {
330 	const struct lpss_config *config;
331 	u32 value;
332 
333 	config = lpss_get_config(drv_data);
334 	drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset;
335 
336 	/* Enable software chip select control */
337 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
338 	value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
339 	value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
340 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
341 
342 	/* Enable multiblock DMA transfers */
343 	if (drv_data->controller_info->enable_dma) {
344 		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
345 
346 		if (config->reg_general >= 0) {
347 			value = __lpss_ssp_read_priv(drv_data,
348 						     config->reg_general);
349 			value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
350 			__lpss_ssp_write_priv(drv_data,
351 					      config->reg_general, value);
352 		}
353 	}
354 }
355 
356 static void lpss_ssp_select_cs(struct spi_device *spi,
357 			       const struct lpss_config *config)
358 {
359 	struct driver_data *drv_data =
360 		spi_controller_get_devdata(spi->controller);
361 	u32 value, cs;
362 
363 	if (!config->cs_sel_mask)
364 		return;
365 
366 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
367 
368 	cs = spi->chip_select;
369 	cs <<= config->cs_sel_shift;
370 	if (cs != (value & config->cs_sel_mask)) {
371 		/*
372 		 * When switching another chip select output active the
373 		 * output must be selected first and wait 2 ssp_clk cycles
374 		 * before changing state to active. Otherwise a short
375 		 * glitch will occur on the previous chip select since
376 		 * output select is latched but state control is not.
377 		 */
378 		value &= ~config->cs_sel_mask;
379 		value |= cs;
380 		__lpss_ssp_write_priv(drv_data,
381 				      config->reg_cs_ctrl, value);
382 		ndelay(1000000000 /
383 		       (drv_data->controller->max_speed_hz / 2));
384 	}
385 }
386 
387 static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
388 {
389 	struct driver_data *drv_data =
390 		spi_controller_get_devdata(spi->controller);
391 	const struct lpss_config *config;
392 	u32 value;
393 
394 	config = lpss_get_config(drv_data);
395 
396 	if (enable)
397 		lpss_ssp_select_cs(spi, config);
398 
399 	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
400 	if (enable)
401 		value &= ~LPSS_CS_CONTROL_CS_HIGH;
402 	else
403 		value |= LPSS_CS_CONTROL_CS_HIGH;
404 	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
405 	if (config->cs_clk_stays_gated) {
406 		u32 clkgate;
407 
408 		/*
409 		 * Changing CS alone when dynamic clock gating is on won't
410 		 * actually flip CS at that time. This ruins SPI transfers
411 		 * that specify delays, or have no data. Toggle the clock mode
412 		 * to force on briefly to poke the CS pin to move.
413 		 */
414 		clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
415 		value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
416 			LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
417 
418 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
419 		__lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
420 	}
421 }
422 
423 static void cs_assert(struct spi_device *spi)
424 {
425 	struct chip_data *chip = spi_get_ctldata(spi);
426 	struct driver_data *drv_data =
427 		spi_controller_get_devdata(spi->controller);
428 
429 	if (drv_data->ssp_type == CE4100_SSP) {
430 		pxa2xx_spi_write(drv_data, SSSR, chip->frm);
431 		return;
432 	}
433 
434 	if (chip->cs_control) {
435 		chip->cs_control(PXA2XX_CS_ASSERT);
436 		return;
437 	}
438 
439 	if (chip->gpiod_cs) {
440 		gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
441 		return;
442 	}
443 
444 	if (is_lpss_ssp(drv_data))
445 		lpss_ssp_cs_control(spi, true);
446 }
447 
448 static void cs_deassert(struct spi_device *spi)
449 {
450 	struct chip_data *chip = spi_get_ctldata(spi);
451 	struct driver_data *drv_data =
452 		spi_controller_get_devdata(spi->controller);
453 	unsigned long timeout;
454 
455 	if (drv_data->ssp_type == CE4100_SSP)
456 		return;
457 
458 	/* Wait until SSP becomes idle before deasserting the CS */
459 	timeout = jiffies + msecs_to_jiffies(10);
460 	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
461 	       !time_after(jiffies, timeout))
462 		cpu_relax();
463 
464 	if (chip->cs_control) {
465 		chip->cs_control(PXA2XX_CS_DEASSERT);
466 		return;
467 	}
468 
469 	if (chip->gpiod_cs) {
470 		gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
471 		return;
472 	}
473 
474 	if (is_lpss_ssp(drv_data))
475 		lpss_ssp_cs_control(spi, false);
476 }
477 
478 static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
479 {
480 	if (level)
481 		cs_deassert(spi);
482 	else
483 		cs_assert(spi);
484 }
485 
486 int pxa2xx_spi_flush(struct driver_data *drv_data)
487 {
488 	unsigned long limit = loops_per_jiffy << 1;
489 
490 	do {
491 		while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
492 			pxa2xx_spi_read(drv_data, SSDR);
493 	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
494 	write_SSSR_CS(drv_data, SSSR_ROR);
495 
496 	return limit;
497 }
498 
499 static void pxa2xx_spi_off(struct driver_data *drv_data)
500 {
501 	/* On MMP, disabling SSE seems to corrupt the Rx FIFO */
502 	if (is_mmp2_ssp(drv_data))
503 		return;
504 
505 	pxa_ssp_disable(drv_data->ssp);
506 }
507 
508 static int null_writer(struct driver_data *drv_data)
509 {
510 	u8 n_bytes = drv_data->n_bytes;
511 
512 	if (pxa2xx_spi_txfifo_full(drv_data)
513 		|| (drv_data->tx == drv_data->tx_end))
514 		return 0;
515 
516 	pxa2xx_spi_write(drv_data, SSDR, 0);
517 	drv_data->tx += n_bytes;
518 
519 	return 1;
520 }
521 
522 static int null_reader(struct driver_data *drv_data)
523 {
524 	u8 n_bytes = drv_data->n_bytes;
525 
526 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
527 	       && (drv_data->rx < drv_data->rx_end)) {
528 		pxa2xx_spi_read(drv_data, SSDR);
529 		drv_data->rx += n_bytes;
530 	}
531 
532 	return drv_data->rx == drv_data->rx_end;
533 }
534 
535 static int u8_writer(struct driver_data *drv_data)
536 {
537 	if (pxa2xx_spi_txfifo_full(drv_data)
538 		|| (drv_data->tx == drv_data->tx_end))
539 		return 0;
540 
541 	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
542 	++drv_data->tx;
543 
544 	return 1;
545 }
546 
547 static int u8_reader(struct driver_data *drv_data)
548 {
549 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
550 	       && (drv_data->rx < drv_data->rx_end)) {
551 		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
552 		++drv_data->rx;
553 	}
554 
555 	return drv_data->rx == drv_data->rx_end;
556 }
557 
558 static int u16_writer(struct driver_data *drv_data)
559 {
560 	if (pxa2xx_spi_txfifo_full(drv_data)
561 		|| (drv_data->tx == drv_data->tx_end))
562 		return 0;
563 
564 	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
565 	drv_data->tx += 2;
566 
567 	return 1;
568 }
569 
570 static int u16_reader(struct driver_data *drv_data)
571 {
572 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
573 	       && (drv_data->rx < drv_data->rx_end)) {
574 		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
575 		drv_data->rx += 2;
576 	}
577 
578 	return drv_data->rx == drv_data->rx_end;
579 }
580 
581 static int u32_writer(struct driver_data *drv_data)
582 {
583 	if (pxa2xx_spi_txfifo_full(drv_data)
584 		|| (drv_data->tx == drv_data->tx_end))
585 		return 0;
586 
587 	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
588 	drv_data->tx += 4;
589 
590 	return 1;
591 }
592 
593 static int u32_reader(struct driver_data *drv_data)
594 {
595 	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
596 	       && (drv_data->rx < drv_data->rx_end)) {
597 		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
598 		drv_data->rx += 4;
599 	}
600 
601 	return drv_data->rx == drv_data->rx_end;
602 }
603 
604 static void reset_sccr1(struct driver_data *drv_data)
605 {
606 	struct chip_data *chip =
607 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
608 	u32 sccr1_reg;
609 
610 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
611 	switch (drv_data->ssp_type) {
612 	case QUARK_X1000_SSP:
613 		sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
614 		break;
615 	case CE4100_SSP:
616 		sccr1_reg &= ~CE4100_SSCR1_RFT;
617 		break;
618 	default:
619 		sccr1_reg &= ~SSCR1_RFT;
620 		break;
621 	}
622 	sccr1_reg |= chip->threshold;
623 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
624 }
625 
626 static void int_stop_and_reset(struct driver_data *drv_data)
627 {
628 	/* Clear and disable interrupts */
629 	write_SSSR_CS(drv_data, drv_data->clear_sr);
630 	reset_sccr1(drv_data);
631 	if (pxa25x_ssp_comp(drv_data))
632 		return;
633 
634 	pxa2xx_spi_write(drv_data, SSTO, 0);
635 }
636 
637 static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
638 {
639 	int_stop_and_reset(drv_data);
640 	pxa2xx_spi_flush(drv_data);
641 	pxa2xx_spi_off(drv_data);
642 
643 	dev_err(drv_data->ssp->dev, "%s\n", msg);
644 
645 	drv_data->controller->cur_msg->status = err;
646 	spi_finalize_current_transfer(drv_data->controller);
647 }
648 
649 static void int_transfer_complete(struct driver_data *drv_data)
650 {
651 	int_stop_and_reset(drv_data);
652 
653 	spi_finalize_current_transfer(drv_data->controller);
654 }
655 
656 static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
657 {
658 	u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
659 		       drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
660 
661 	u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
662 
663 	if (irq_status & SSSR_ROR) {
664 		int_error_stop(drv_data, "interrupt_transfer: fifo overrun", -EIO);
665 		return IRQ_HANDLED;
666 	}
667 
668 	if (irq_status & SSSR_TUR) {
669 		int_error_stop(drv_data, "interrupt_transfer: fifo underrun", -EIO);
670 		return IRQ_HANDLED;
671 	}
672 
673 	if (irq_status & SSSR_TINT) {
674 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
675 		if (drv_data->read(drv_data)) {
676 			int_transfer_complete(drv_data);
677 			return IRQ_HANDLED;
678 		}
679 	}
680 
681 	/* Drain rx fifo, Fill tx fifo and prevent overruns */
682 	do {
683 		if (drv_data->read(drv_data)) {
684 			int_transfer_complete(drv_data);
685 			return IRQ_HANDLED;
686 		}
687 	} while (drv_data->write(drv_data));
688 
689 	if (drv_data->read(drv_data)) {
690 		int_transfer_complete(drv_data);
691 		return IRQ_HANDLED;
692 	}
693 
694 	if (drv_data->tx == drv_data->tx_end) {
695 		u32 bytes_left;
696 		u32 sccr1_reg;
697 
698 		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
699 		sccr1_reg &= ~SSCR1_TIE;
700 
701 		/*
702 		 * PXA25x_SSP has no timeout, set up rx threshould for the
703 		 * remaining RX bytes.
704 		 */
705 		if (pxa25x_ssp_comp(drv_data)) {
706 			u32 rx_thre;
707 
708 			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
709 
710 			bytes_left = drv_data->rx_end - drv_data->rx;
711 			switch (drv_data->n_bytes) {
712 			case 4:
713 				bytes_left >>= 2;
714 				break;
715 			case 2:
716 				bytes_left >>= 1;
717 				break;
718 			}
719 
720 			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
721 			if (rx_thre > bytes_left)
722 				rx_thre = bytes_left;
723 
724 			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
725 		}
726 		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
727 	}
728 
729 	/* We did something */
730 	return IRQ_HANDLED;
731 }
732 
733 static void handle_bad_msg(struct driver_data *drv_data)
734 {
735 	pxa2xx_spi_off(drv_data);
736 	pxa2xx_spi_write(drv_data, SSCR1,
737 			 pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
738 	if (!pxa25x_ssp_comp(drv_data))
739 		pxa2xx_spi_write(drv_data, SSTO, 0);
740 	write_SSSR_CS(drv_data, drv_data->clear_sr);
741 
742 	dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
743 }
744 
745 static irqreturn_t ssp_int(int irq, void *dev_id)
746 {
747 	struct driver_data *drv_data = dev_id;
748 	u32 sccr1_reg;
749 	u32 mask = drv_data->mask_sr;
750 	u32 status;
751 
752 	/*
753 	 * The IRQ might be shared with other peripherals so we must first
754 	 * check that are we RPM suspended or not. If we are we assume that
755 	 * the IRQ was not for us (we shouldn't be RPM suspended when the
756 	 * interrupt is enabled).
757 	 */
758 	if (pm_runtime_suspended(drv_data->ssp->dev))
759 		return IRQ_NONE;
760 
761 	/*
762 	 * If the device is not yet in RPM suspended state and we get an
763 	 * interrupt that is meant for another device, check if status bits
764 	 * are all set to one. That means that the device is already
765 	 * powered off.
766 	 */
767 	status = pxa2xx_spi_read(drv_data, SSSR);
768 	if (status == ~0)
769 		return IRQ_NONE;
770 
771 	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
772 
773 	/* Ignore possible writes if we don't need to write */
774 	if (!(sccr1_reg & SSCR1_TIE))
775 		mask &= ~SSSR_TFS;
776 
777 	/* Ignore RX timeout interrupt if it is disabled */
778 	if (!(sccr1_reg & SSCR1_TINTE))
779 		mask &= ~SSSR_TINT;
780 
781 	if (!(status & mask))
782 		return IRQ_NONE;
783 
784 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
785 	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
786 
787 	if (!drv_data->controller->cur_msg) {
788 		handle_bad_msg(drv_data);
789 		/* Never fail */
790 		return IRQ_HANDLED;
791 	}
792 
793 	return drv_data->transfer_handler(drv_data);
794 }
795 
796 /*
797  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
798  * input frequency by fractions of 2^24. It also has a divider by 5.
799  *
800  * There are formulas to get baud rate value for given input frequency and
801  * divider parameters, such as DDS_CLK_RATE and SCR:
802  *
803  * Fsys = 200MHz
804  *
805  * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
806  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
807  *
808  * DDS_CLK_RATE either 2^n or 2^n / 5.
809  * SCR is in range 0 .. 255
810  *
811  * Divisor = 5^i * 2^j * 2 * k
812  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
813  *       j = [0, 23]     j = 0 iff i = 1
814  *       k = [1, 256]
815  * Special case: j = 0, i = 1: Divisor = 2 / 5
816  *
817  * Accordingly to the specification the recommended values for DDS_CLK_RATE
818  * are:
819  *	Case 1:		2^n, n = [0, 23]
820  *	Case 2:		2^24 * 2 / 5 (0x666666)
821  *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
822  *
823  * In all cases the lowest possible value is better.
824  *
825  * The function calculates parameters for all cases and chooses the one closest
826  * to the asked baud rate.
827  */
828 static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
829 {
830 	unsigned long xtal = 200000000;
831 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
832 						   see (2) */
833 						/* case 3 */
834 	unsigned long fref1 = fref / 2;		/* case 1 */
835 	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
836 	unsigned long scale;
837 	unsigned long q, q1, q2;
838 	long r, r1, r2;
839 	u32 mul;
840 
841 	/* Case 1 */
842 
843 	/* Set initial value for DDS_CLK_RATE */
844 	mul = (1 << 24) >> 1;
845 
846 	/* Calculate initial quot */
847 	q1 = DIV_ROUND_UP(fref1, rate);
848 
849 	/* Scale q1 if it's too big */
850 	if (q1 > 256) {
851 		/* Scale q1 to range [1, 512] */
852 		scale = fls_long(q1 - 1);
853 		if (scale > 9) {
854 			q1 >>= scale - 9;
855 			mul >>= scale - 9;
856 		}
857 
858 		/* Round the result if we have a remainder */
859 		q1 += q1 & 1;
860 	}
861 
862 	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
863 	scale = __ffs(q1);
864 	q1 >>= scale;
865 	mul >>= scale;
866 
867 	/* Get the remainder */
868 	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
869 
870 	/* Case 2 */
871 
872 	q2 = DIV_ROUND_UP(fref2, rate);
873 	r2 = abs(fref2 / q2 - rate);
874 
875 	/*
876 	 * Choose the best between two: less remainder we have the better. We
877 	 * can't go case 2 if q2 is greater than 256 since SCR register can
878 	 * hold only values 0 .. 255.
879 	 */
880 	if (r2 >= r1 || q2 > 256) {
881 		/* case 1 is better */
882 		r = r1;
883 		q = q1;
884 	} else {
885 		/* case 2 is better */
886 		r = r2;
887 		q = q2;
888 		mul = (1 << 24) * 2 / 5;
889 	}
890 
891 	/* Check case 3 only if the divisor is big enough */
892 	if (fref / rate >= 80) {
893 		u64 fssp;
894 		u32 m;
895 
896 		/* Calculate initial quot */
897 		q1 = DIV_ROUND_UP(fref, rate);
898 		m = (1 << 24) / q1;
899 
900 		/* Get the remainder */
901 		fssp = (u64)fref * m;
902 		do_div(fssp, 1 << 24);
903 		r1 = abs(fssp - rate);
904 
905 		/* Choose this one if it suits better */
906 		if (r1 < r) {
907 			/* case 3 is better */
908 			q = 1;
909 			mul = m;
910 		}
911 	}
912 
913 	*dds = mul;
914 	return q - 1;
915 }
916 
917 static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
918 {
919 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
920 	const struct ssp_device *ssp = drv_data->ssp;
921 
922 	rate = min_t(int, ssp_clk, rate);
923 
924 	/*
925 	 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
926 	 * that the SSP transmission rate can be greater than the device rate
927 	 */
928 	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
929 		return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
930 	else
931 		return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
932 }
933 
934 static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
935 					   int rate)
936 {
937 	struct chip_data *chip =
938 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
939 	unsigned int clk_div;
940 
941 	switch (drv_data->ssp_type) {
942 	case QUARK_X1000_SSP:
943 		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
944 		break;
945 	default:
946 		clk_div = ssp_get_clk_div(drv_data, rate);
947 		break;
948 	}
949 	return clk_div << 8;
950 }
951 
952 static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
953 			       struct spi_device *spi,
954 			       struct spi_transfer *xfer)
955 {
956 	struct chip_data *chip = spi_get_ctldata(spi);
957 
958 	return chip->enable_dma &&
959 	       xfer->len <= MAX_DMA_LEN &&
960 	       xfer->len >= chip->dma_burst_size;
961 }
962 
963 static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
964 				   struct spi_device *spi,
965 				   struct spi_transfer *transfer)
966 {
967 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
968 	struct spi_message *message = controller->cur_msg;
969 	struct chip_data *chip = spi_get_ctldata(spi);
970 	u32 dma_thresh = chip->dma_threshold;
971 	u32 dma_burst = chip->dma_burst_size;
972 	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
973 	u32 clk_div;
974 	u8 bits;
975 	u32 speed;
976 	u32 cr0;
977 	u32 cr1;
978 	int err;
979 	int dma_mapped;
980 
981 	/* Check if we can DMA this transfer */
982 	if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
983 
984 		/* reject already-mapped transfers; PIO won't always work */
985 		if (message->is_dma_mapped
986 				|| transfer->rx_dma || transfer->tx_dma) {
987 			dev_err(&spi->dev,
988 				"Mapped transfer length of %u is greater than %d\n",
989 				transfer->len, MAX_DMA_LEN);
990 			return -EINVAL;
991 		}
992 
993 		/* warn ... we force this to PIO mode */
994 		dev_warn_ratelimited(&spi->dev,
995 				     "DMA disabled for transfer length %ld greater than %d\n",
996 				     (long)transfer->len, MAX_DMA_LEN);
997 	}
998 
999 	/* Setup the transfer state based on the type of transfer */
1000 	if (pxa2xx_spi_flush(drv_data) == 0) {
1001 		dev_err(&spi->dev, "Flush failed\n");
1002 		return -EIO;
1003 	}
1004 	drv_data->n_bytes = chip->n_bytes;
1005 	drv_data->tx = (void *)transfer->tx_buf;
1006 	drv_data->tx_end = drv_data->tx + transfer->len;
1007 	drv_data->rx = transfer->rx_buf;
1008 	drv_data->rx_end = drv_data->rx + transfer->len;
1009 	drv_data->write = drv_data->tx ? chip->write : null_writer;
1010 	drv_data->read = drv_data->rx ? chip->read : null_reader;
1011 
1012 	/* Change speed and bit per word on a per transfer */
1013 	bits = transfer->bits_per_word;
1014 	speed = transfer->speed_hz;
1015 
1016 	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
1017 
1018 	if (bits <= 8) {
1019 		drv_data->n_bytes = 1;
1020 		drv_data->read = drv_data->read != null_reader ?
1021 					u8_reader : null_reader;
1022 		drv_data->write = drv_data->write != null_writer ?
1023 					u8_writer : null_writer;
1024 	} else if (bits <= 16) {
1025 		drv_data->n_bytes = 2;
1026 		drv_data->read = drv_data->read != null_reader ?
1027 					u16_reader : null_reader;
1028 		drv_data->write = drv_data->write != null_writer ?
1029 					u16_writer : null_writer;
1030 	} else if (bits <= 32) {
1031 		drv_data->n_bytes = 4;
1032 		drv_data->read = drv_data->read != null_reader ?
1033 					u32_reader : null_reader;
1034 		drv_data->write = drv_data->write != null_writer ?
1035 					u32_writer : null_writer;
1036 	}
1037 	/*
1038 	 * if bits/word is changed in dma mode, then must check the
1039 	 * thresholds and burst also
1040 	 */
1041 	if (chip->enable_dma) {
1042 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
1043 						spi,
1044 						bits, &dma_burst,
1045 						&dma_thresh))
1046 			dev_warn_ratelimited(&spi->dev,
1047 					     "DMA burst size reduced to match bits_per_word\n");
1048 	}
1049 
1050 	dma_mapped = controller->can_dma &&
1051 		     controller->can_dma(controller, spi, transfer) &&
1052 		     controller->cur_msg_mapped;
1053 	if (dma_mapped) {
1054 
1055 		/* Ensure we have the correct interrupt handler */
1056 		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1057 
1058 		err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1059 		if (err)
1060 			return err;
1061 
1062 		/* Clear status and start DMA engine */
1063 		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1064 		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1065 
1066 		pxa2xx_spi_dma_start(drv_data);
1067 	} else {
1068 		/* Ensure we have the correct interrupt handler	*/
1069 		drv_data->transfer_handler = interrupt_transfer;
1070 
1071 		/* Clear status  */
1072 		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1073 		write_SSSR_CS(drv_data, drv_data->clear_sr);
1074 	}
1075 
1076 	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1077 	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1078 	if (!pxa25x_ssp_comp(drv_data))
1079 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
1080 			controller->max_speed_hz
1081 				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1082 			dma_mapped ? "DMA" : "PIO");
1083 	else
1084 		dev_dbg(&spi->dev, "%u Hz actual, %s\n",
1085 			controller->max_speed_hz / 2
1086 				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1087 			dma_mapped ? "DMA" : "PIO");
1088 
1089 	if (is_lpss_ssp(drv_data)) {
1090 		pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold);
1091 		pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold);
1092 	}
1093 
1094 	if (is_quark_x1000_ssp(drv_data))
1095 		pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate);
1096 
1097 	/* Stop the SSP */
1098 	if (!is_mmp2_ssp(drv_data))
1099 		pxa_ssp_disable(drv_data->ssp);
1100 
1101 	if (!pxa25x_ssp_comp(drv_data))
1102 		pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1103 
1104 	/* first set CR1 without interrupt and service enables */
1105 	pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1);
1106 
1107 	/* see if we need to reload the config registers */
1108 	pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0);
1109 
1110 	/* Restart the SSP */
1111 	pxa_ssp_enable(drv_data->ssp);
1112 
1113 	if (is_mmp2_ssp(drv_data)) {
1114 		u8 tx_level = (pxa2xx_spi_read(drv_data, SSSR)
1115 					& SSSR_TFL_MASK) >> 8;
1116 
1117 		if (tx_level) {
1118 			/* On MMP2, flipping SSE doesn't to empty TXFIFO. */
1119 			dev_warn(&spi->dev, "%d bytes of garbage in TXFIFO!\n",
1120 								tx_level);
1121 			if (tx_level > transfer->len)
1122 				tx_level = transfer->len;
1123 			drv_data->tx += tx_level;
1124 		}
1125 	}
1126 
1127 	if (spi_controller_is_slave(controller)) {
1128 		while (drv_data->write(drv_data))
1129 			;
1130 		if (drv_data->gpiod_ready) {
1131 			gpiod_set_value(drv_data->gpiod_ready, 1);
1132 			udelay(1);
1133 			gpiod_set_value(drv_data->gpiod_ready, 0);
1134 		}
1135 	}
1136 
1137 	/*
1138 	 * Release the data by enabling service requests and interrupts,
1139 	 * without changing any mode bits
1140 	 */
1141 	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1142 
1143 	return 1;
1144 }
1145 
1146 static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
1147 {
1148 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1149 
1150 	int_error_stop(drv_data, "transfer aborted", -EINTR);
1151 
1152 	return 0;
1153 }
1154 
1155 static void pxa2xx_spi_handle_err(struct spi_controller *controller,
1156 				 struct spi_message *msg)
1157 {
1158 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1159 
1160 	/* Disable the SSP */
1161 	pxa2xx_spi_off(drv_data);
1162 	/* Clear and disable interrupts and service requests */
1163 	write_SSSR_CS(drv_data, drv_data->clear_sr);
1164 	pxa2xx_spi_write(drv_data, SSCR1,
1165 			 pxa2xx_spi_read(drv_data, SSCR1)
1166 			 & ~(drv_data->int_cr1 | drv_data->dma_cr1));
1167 	if (!pxa25x_ssp_comp(drv_data))
1168 		pxa2xx_spi_write(drv_data, SSTO, 0);
1169 
1170 	/*
1171 	 * Stop the DMA if running. Note DMA callback handler may have unset
1172 	 * the dma_running already, which is fine as stopping is not needed
1173 	 * then but we shouldn't rely this flag for anything else than
1174 	 * stopping. For instance to differentiate between PIO and DMA
1175 	 * transfers.
1176 	 */
1177 	if (atomic_read(&drv_data->dma_running))
1178 		pxa2xx_spi_dma_stop(drv_data);
1179 }
1180 
1181 static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
1182 {
1183 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1184 
1185 	/* Disable the SSP now */
1186 	pxa2xx_spi_off(drv_data);
1187 
1188 	return 0;
1189 }
1190 
1191 static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1192 		    struct pxa2xx_spi_chip *chip_info)
1193 {
1194 	struct driver_data *drv_data =
1195 		spi_controller_get_devdata(spi->controller);
1196 	struct gpio_desc *gpiod;
1197 	int err = 0;
1198 
1199 	if (chip == NULL)
1200 		return 0;
1201 
1202 	if (drv_data->cs_gpiods) {
1203 		gpiod = drv_data->cs_gpiods[spi->chip_select];
1204 		if (gpiod) {
1205 			chip->gpiod_cs = gpiod;
1206 			chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1207 			gpiod_set_value(gpiod, chip->gpio_cs_inverted);
1208 		}
1209 
1210 		return 0;
1211 	}
1212 
1213 	if (chip_info == NULL)
1214 		return 0;
1215 
1216 	/* NOTE: setup() can be called multiple times, possibly with
1217 	 * different chip_info, release previously requested GPIO
1218 	 */
1219 	if (chip->gpiod_cs) {
1220 		gpiod_put(chip->gpiod_cs);
1221 		chip->gpiod_cs = NULL;
1222 	}
1223 
1224 	/* If (*cs_control) is provided, ignore GPIO chip select */
1225 	if (chip_info->cs_control) {
1226 		chip->cs_control = chip_info->cs_control;
1227 		return 0;
1228 	}
1229 
1230 	if (gpio_is_valid(chip_info->gpio_cs)) {
1231 		err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1232 		if (err) {
1233 			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1234 				chip_info->gpio_cs);
1235 			return err;
1236 		}
1237 
1238 		gpiod = gpio_to_desc(chip_info->gpio_cs);
1239 		chip->gpiod_cs = gpiod;
1240 		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1241 
1242 		err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
1243 	}
1244 
1245 	return err;
1246 }
1247 
1248 static int setup(struct spi_device *spi)
1249 {
1250 	struct pxa2xx_spi_chip *chip_info;
1251 	struct chip_data *chip;
1252 	const struct lpss_config *config;
1253 	struct driver_data *drv_data =
1254 		spi_controller_get_devdata(spi->controller);
1255 	uint tx_thres, tx_hi_thres, rx_thres;
1256 
1257 	switch (drv_data->ssp_type) {
1258 	case QUARK_X1000_SSP:
1259 		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1260 		tx_hi_thres = 0;
1261 		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1262 		break;
1263 	case CE4100_SSP:
1264 		tx_thres = TX_THRESH_CE4100_DFLT;
1265 		tx_hi_thres = 0;
1266 		rx_thres = RX_THRESH_CE4100_DFLT;
1267 		break;
1268 	case LPSS_LPT_SSP:
1269 	case LPSS_BYT_SSP:
1270 	case LPSS_BSW_SSP:
1271 	case LPSS_SPT_SSP:
1272 	case LPSS_BXT_SSP:
1273 	case LPSS_CNL_SSP:
1274 		config = lpss_get_config(drv_data);
1275 		tx_thres = config->tx_threshold_lo;
1276 		tx_hi_thres = config->tx_threshold_hi;
1277 		rx_thres = config->rx_threshold;
1278 		break;
1279 	default:
1280 		tx_hi_thres = 0;
1281 		if (spi_controller_is_slave(drv_data->controller)) {
1282 			tx_thres = 1;
1283 			rx_thres = 2;
1284 		} else {
1285 			tx_thres = TX_THRESH_DFLT;
1286 			rx_thres = RX_THRESH_DFLT;
1287 		}
1288 		break;
1289 	}
1290 
1291 	/* Only alloc on first setup */
1292 	chip = spi_get_ctldata(spi);
1293 	if (!chip) {
1294 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1295 		if (!chip)
1296 			return -ENOMEM;
1297 
1298 		if (drv_data->ssp_type == CE4100_SSP) {
1299 			if (spi->chip_select > 4) {
1300 				dev_err(&spi->dev,
1301 					"failed setup: cs number must not be > 4.\n");
1302 				kfree(chip);
1303 				return -EINVAL;
1304 			}
1305 
1306 			chip->frm = spi->chip_select;
1307 		}
1308 		chip->enable_dma = drv_data->controller_info->enable_dma;
1309 		chip->timeout = TIMOUT_DFLT;
1310 	}
1311 
1312 	/* protocol drivers may change the chip settings, so...
1313 	 * if chip_info exists, use it */
1314 	chip_info = spi->controller_data;
1315 
1316 	/* chip_info isn't always needed */
1317 	chip->cr1 = 0;
1318 	if (chip_info) {
1319 		if (chip_info->timeout)
1320 			chip->timeout = chip_info->timeout;
1321 		if (chip_info->tx_threshold)
1322 			tx_thres = chip_info->tx_threshold;
1323 		if (chip_info->tx_hi_threshold)
1324 			tx_hi_thres = chip_info->tx_hi_threshold;
1325 		if (chip_info->rx_threshold)
1326 			rx_thres = chip_info->rx_threshold;
1327 		chip->dma_threshold = 0;
1328 		if (chip_info->enable_loopback)
1329 			chip->cr1 = SSCR1_LBM;
1330 	}
1331 	if (spi_controller_is_slave(drv_data->controller)) {
1332 		chip->cr1 |= SSCR1_SCFR;
1333 		chip->cr1 |= SSCR1_SCLKDIR;
1334 		chip->cr1 |= SSCR1_SFRMDIR;
1335 		chip->cr1 |= SSCR1_SPH;
1336 	}
1337 
1338 	chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1339 	chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1340 				| SSITF_TxHiThresh(tx_hi_thres);
1341 
1342 	/* set dma burst and threshold outside of chip_info path so that if
1343 	 * chip_info goes away after setting chip->enable_dma, the
1344 	 * burst and threshold can still respond to changes in bits_per_word */
1345 	if (chip->enable_dma) {
1346 		/* set up legal burst and threshold for dma */
1347 		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1348 						spi->bits_per_word,
1349 						&chip->dma_burst_size,
1350 						&chip->dma_threshold)) {
1351 			dev_warn(&spi->dev,
1352 				 "in setup: DMA burst size reduced to match bits_per_word\n");
1353 		}
1354 		dev_dbg(&spi->dev,
1355 			"in setup: DMA burst size set to %u\n",
1356 			chip->dma_burst_size);
1357 	}
1358 
1359 	switch (drv_data->ssp_type) {
1360 	case QUARK_X1000_SSP:
1361 		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1362 				   & QUARK_X1000_SSCR1_RFT)
1363 				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1364 				   & QUARK_X1000_SSCR1_TFT);
1365 		break;
1366 	case CE4100_SSP:
1367 		chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
1368 			(CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
1369 		break;
1370 	default:
1371 		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1372 			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1373 		break;
1374 	}
1375 
1376 	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1377 	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1378 			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1379 
1380 	if (spi->mode & SPI_LOOP)
1381 		chip->cr1 |= SSCR1_LBM;
1382 
1383 	if (spi->bits_per_word <= 8) {
1384 		chip->n_bytes = 1;
1385 		chip->read = u8_reader;
1386 		chip->write = u8_writer;
1387 	} else if (spi->bits_per_word <= 16) {
1388 		chip->n_bytes = 2;
1389 		chip->read = u16_reader;
1390 		chip->write = u16_writer;
1391 	} else if (spi->bits_per_word <= 32) {
1392 		chip->n_bytes = 4;
1393 		chip->read = u32_reader;
1394 		chip->write = u32_writer;
1395 	}
1396 
1397 	spi_set_ctldata(spi, chip);
1398 
1399 	if (drv_data->ssp_type == CE4100_SSP)
1400 		return 0;
1401 
1402 	return setup_cs(spi, chip, chip_info);
1403 }
1404 
1405 static void cleanup(struct spi_device *spi)
1406 {
1407 	struct chip_data *chip = spi_get_ctldata(spi);
1408 	struct driver_data *drv_data =
1409 		spi_controller_get_devdata(spi->controller);
1410 
1411 	if (!chip)
1412 		return;
1413 
1414 	if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
1415 	    chip->gpiod_cs)
1416 		gpiod_put(chip->gpiod_cs);
1417 
1418 	kfree(chip);
1419 }
1420 
1421 #ifdef CONFIG_ACPI
1422 static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
1423 	{ "INT33C0", LPSS_LPT_SSP },
1424 	{ "INT33C1", LPSS_LPT_SSP },
1425 	{ "INT3430", LPSS_LPT_SSP },
1426 	{ "INT3431", LPSS_LPT_SSP },
1427 	{ "80860F0E", LPSS_BYT_SSP },
1428 	{ "8086228E", LPSS_BSW_SSP },
1429 	{ },
1430 };
1431 MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1432 #endif
1433 
1434 /*
1435  * PCI IDs of compound devices that integrate both host controller and private
1436  * integrated DMA engine. Please note these are not used in module
1437  * autoloading and probing in this module but matching the LPSS SSP type.
1438  */
1439 static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
1440 	/* SPT-LP */
1441 	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
1442 	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
1443 	/* SPT-H */
1444 	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
1445 	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1446 	/* KBL-H */
1447 	{ PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1448 	{ PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
1449 	/* CML-V */
1450 	{ PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
1451 	{ PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
1452 	/* BXT A-Step */
1453 	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1454 	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1455 	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1456 	/* BXT B-Step */
1457 	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1458 	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1459 	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1460 	/* GLK */
1461 	{ PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1462 	{ PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1463 	{ PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
1464 	/* ICL-LP */
1465 	{ PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
1466 	{ PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
1467 	{ PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
1468 	/* EHL */
1469 	{ PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
1470 	{ PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
1471 	{ PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
1472 	/* JSL */
1473 	{ PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
1474 	{ PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
1475 	{ PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
1476 	/* TGL-H */
1477 	{ PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
1478 	{ PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
1479 	{ PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
1480 	{ PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
1481 	/* ADL-P */
1482 	{ PCI_VDEVICE(INTEL, 0x51aa), LPSS_CNL_SSP },
1483 	{ PCI_VDEVICE(INTEL, 0x51ab), LPSS_CNL_SSP },
1484 	{ PCI_VDEVICE(INTEL, 0x51fb), LPSS_CNL_SSP },
1485 	/* ADL-M */
1486 	{ PCI_VDEVICE(INTEL, 0x54aa), LPSS_CNL_SSP },
1487 	{ PCI_VDEVICE(INTEL, 0x54ab), LPSS_CNL_SSP },
1488 	{ PCI_VDEVICE(INTEL, 0x54fb), LPSS_CNL_SSP },
1489 	/* APL */
1490 	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1491 	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1492 	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1493 	/* ADL-S */
1494 	{ PCI_VDEVICE(INTEL, 0x7aaa), LPSS_CNL_SSP },
1495 	{ PCI_VDEVICE(INTEL, 0x7aab), LPSS_CNL_SSP },
1496 	{ PCI_VDEVICE(INTEL, 0x7af9), LPSS_CNL_SSP },
1497 	{ PCI_VDEVICE(INTEL, 0x7afb), LPSS_CNL_SSP },
1498 	/* CNL-LP */
1499 	{ PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1500 	{ PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1501 	{ PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1502 	/* CNL-H */
1503 	{ PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1504 	{ PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1505 	{ PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
1506 	/* CML-LP */
1507 	{ PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
1508 	{ PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
1509 	{ PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
1510 	/* CML-H */
1511 	{ PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1512 	{ PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1513 	{ PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
1514 	/* TGL-LP */
1515 	{ PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1516 	{ PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1517 	{ PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1518 	{ PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1519 	{ PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1520 	{ PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1521 	{ PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
1522 	{ },
1523 };
1524 
1525 static const struct of_device_id pxa2xx_spi_of_match[] = {
1526 	{ .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
1527 	{},
1528 };
1529 MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
1530 
1531 #ifdef CONFIG_ACPI
1532 
1533 static int pxa2xx_spi_get_port_id(struct device *dev)
1534 {
1535 	struct acpi_device *adev;
1536 	unsigned int devid;
1537 	int port_id = -1;
1538 
1539 	adev = ACPI_COMPANION(dev);
1540 	if (adev && adev->pnp.unique_id &&
1541 	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
1542 		port_id = devid;
1543 	return port_id;
1544 }
1545 
1546 #else /* !CONFIG_ACPI */
1547 
1548 static int pxa2xx_spi_get_port_id(struct device *dev)
1549 {
1550 	return -1;
1551 }
1552 
1553 #endif /* CONFIG_ACPI */
1554 
1555 
1556 #ifdef CONFIG_PCI
1557 
1558 static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
1559 {
1560 	return param == chan->device->dev;
1561 }
1562 
1563 #endif /* CONFIG_PCI */
1564 
1565 static struct pxa2xx_spi_controller *
1566 pxa2xx_spi_init_pdata(struct platform_device *pdev)
1567 {
1568 	struct pxa2xx_spi_controller *pdata;
1569 	struct ssp_device *ssp;
1570 	struct resource *res;
1571 	struct device *parent = pdev->dev.parent;
1572 	struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
1573 	const struct pci_device_id *pcidev_id = NULL;
1574 	enum pxa_ssp_type type;
1575 	const void *match;
1576 
1577 	if (pcidev)
1578 		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
1579 
1580 	match = device_get_match_data(&pdev->dev);
1581 	if (match)
1582 		type = (enum pxa_ssp_type)match;
1583 	else if (pcidev_id)
1584 		type = (enum pxa_ssp_type)pcidev_id->driver_data;
1585 	else
1586 		return ERR_PTR(-EINVAL);
1587 
1588 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1589 	if (!pdata)
1590 		return ERR_PTR(-ENOMEM);
1591 
1592 	ssp = &pdata->ssp;
1593 
1594 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1595 	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1596 	if (IS_ERR(ssp->mmio_base))
1597 		return ERR_CAST(ssp->mmio_base);
1598 
1599 	ssp->phys_base = res->start;
1600 
1601 #ifdef CONFIG_PCI
1602 	if (pcidev_id) {
1603 		pdata->tx_param = parent;
1604 		pdata->rx_param = parent;
1605 		pdata->dma_filter = pxa2xx_spi_idma_filter;
1606 	}
1607 #endif
1608 
1609 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
1610 	if (IS_ERR(ssp->clk))
1611 		return ERR_CAST(ssp->clk);
1612 
1613 	ssp->irq = platform_get_irq(pdev, 0);
1614 	if (ssp->irq < 0)
1615 		return ERR_PTR(ssp->irq);
1616 
1617 	ssp->type = type;
1618 	ssp->dev = &pdev->dev;
1619 	ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
1620 
1621 	pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
1622 	pdata->num_chipselect = 1;
1623 	pdata->enable_dma = true;
1624 	pdata->dma_burst_size = 1;
1625 
1626 	return pdata;
1627 }
1628 
1629 static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
1630 				      unsigned int cs)
1631 {
1632 	struct driver_data *drv_data = spi_controller_get_devdata(controller);
1633 
1634 	if (has_acpi_companion(drv_data->ssp->dev)) {
1635 		switch (drv_data->ssp_type) {
1636 		/*
1637 		 * For Atoms the ACPI DeviceSelection used by the Windows
1638 		 * driver starts from 1 instead of 0 so translate it here
1639 		 * to match what Linux expects.
1640 		 */
1641 		case LPSS_BYT_SSP:
1642 		case LPSS_BSW_SSP:
1643 			return cs - 1;
1644 
1645 		default:
1646 			break;
1647 		}
1648 	}
1649 
1650 	return cs;
1651 }
1652 
1653 static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1654 {
1655 	return MAX_DMA_LEN;
1656 }
1657 
1658 static int pxa2xx_spi_probe(struct platform_device *pdev)
1659 {
1660 	struct device *dev = &pdev->dev;
1661 	struct pxa2xx_spi_controller *platform_info;
1662 	struct spi_controller *controller;
1663 	struct driver_data *drv_data;
1664 	struct ssp_device *ssp;
1665 	const struct lpss_config *config;
1666 	int status, count;
1667 	u32 tmp;
1668 
1669 	platform_info = dev_get_platdata(dev);
1670 	if (!platform_info) {
1671 		platform_info = pxa2xx_spi_init_pdata(pdev);
1672 		if (IS_ERR(platform_info)) {
1673 			dev_err(&pdev->dev, "missing platform data\n");
1674 			return PTR_ERR(platform_info);
1675 		}
1676 	}
1677 
1678 	ssp = pxa_ssp_request(pdev->id, pdev->name);
1679 	if (!ssp)
1680 		ssp = &platform_info->ssp;
1681 
1682 	if (!ssp->mmio_base) {
1683 		dev_err(&pdev->dev, "failed to get ssp\n");
1684 		return -ENODEV;
1685 	}
1686 
1687 	if (platform_info->is_slave)
1688 		controller = devm_spi_alloc_slave(dev, sizeof(*drv_data));
1689 	else
1690 		controller = devm_spi_alloc_master(dev, sizeof(*drv_data));
1691 
1692 	if (!controller) {
1693 		dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1694 		status = -ENOMEM;
1695 		goto out_error_controller_alloc;
1696 	}
1697 	drv_data = spi_controller_get_devdata(controller);
1698 	drv_data->controller = controller;
1699 	drv_data->controller_info = platform_info;
1700 	drv_data->ssp = ssp;
1701 
1702 	controller->dev.of_node = pdev->dev.of_node;
1703 	/* the spi->mode bits understood by this driver: */
1704 	controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1705 
1706 	controller->bus_num = ssp->port_id;
1707 	controller->dma_alignment = DMA_ALIGNMENT;
1708 	controller->cleanup = cleanup;
1709 	controller->setup = setup;
1710 	controller->set_cs = pxa2xx_spi_set_cs;
1711 	controller->transfer_one = pxa2xx_spi_transfer_one;
1712 	controller->slave_abort = pxa2xx_spi_slave_abort;
1713 	controller->handle_err = pxa2xx_spi_handle_err;
1714 	controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
1715 	controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
1716 	controller->auto_runtime_pm = true;
1717 	controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1718 
1719 	drv_data->ssp_type = ssp->type;
1720 
1721 	if (pxa25x_ssp_comp(drv_data)) {
1722 		switch (drv_data->ssp_type) {
1723 		case QUARK_X1000_SSP:
1724 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1725 			break;
1726 		default:
1727 			controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1728 			break;
1729 		}
1730 
1731 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1732 		drv_data->dma_cr1 = 0;
1733 		drv_data->clear_sr = SSSR_ROR;
1734 		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1735 	} else {
1736 		controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1737 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1738 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1739 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1740 		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1741 						| SSSR_ROR | SSSR_TUR;
1742 	}
1743 
1744 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1745 			drv_data);
1746 	if (status < 0) {
1747 		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
1748 		goto out_error_controller_alloc;
1749 	}
1750 
1751 	/* Setup DMA if requested */
1752 	if (platform_info->enable_dma) {
1753 		status = pxa2xx_spi_dma_setup(drv_data);
1754 		if (status) {
1755 			dev_warn(dev, "no DMA channels available, using PIO\n");
1756 			platform_info->enable_dma = false;
1757 		} else {
1758 			controller->can_dma = pxa2xx_spi_can_dma;
1759 			controller->max_dma_len = MAX_DMA_LEN;
1760 			controller->max_transfer_size =
1761 				pxa2xx_spi_max_dma_transfer_size;
1762 		}
1763 	}
1764 
1765 	/* Enable SOC clock */
1766 	status = clk_prepare_enable(ssp->clk);
1767 	if (status)
1768 		goto out_error_dma_irq_alloc;
1769 
1770 	controller->max_speed_hz = clk_get_rate(ssp->clk);
1771 	/*
1772 	 * Set minimum speed for all other platforms than Intel Quark which is
1773 	 * able do under 1 Hz transfers.
1774 	 */
1775 	if (!pxa25x_ssp_comp(drv_data))
1776 		controller->min_speed_hz =
1777 			DIV_ROUND_UP(controller->max_speed_hz, 4096);
1778 	else if (!is_quark_x1000_ssp(drv_data))
1779 		controller->min_speed_hz =
1780 			DIV_ROUND_UP(controller->max_speed_hz, 512);
1781 
1782 	pxa_ssp_disable(ssp);
1783 
1784 	/* Load default SSP configuration */
1785 	switch (drv_data->ssp_type) {
1786 	case QUARK_X1000_SSP:
1787 		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
1788 		      QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1789 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1790 
1791 		/* using the Motorola SPI protocol and use 8 bit frame */
1792 		tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
1793 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1794 		break;
1795 	case CE4100_SSP:
1796 		tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
1797 		      CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
1798 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1799 		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1800 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1801 		break;
1802 	default:
1803 
1804 		if (spi_controller_is_slave(controller)) {
1805 			tmp = SSCR1_SCFR |
1806 			      SSCR1_SCLKDIR |
1807 			      SSCR1_SFRMDIR |
1808 			      SSCR1_RxTresh(2) |
1809 			      SSCR1_TxTresh(1) |
1810 			      SSCR1_SPH;
1811 		} else {
1812 			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1813 			      SSCR1_TxTresh(TX_THRESH_DFLT);
1814 		}
1815 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1816 		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
1817 		if (!spi_controller_is_slave(controller))
1818 			tmp |= SSCR0_SCR(2);
1819 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1820 		break;
1821 	}
1822 
1823 	if (!pxa25x_ssp_comp(drv_data))
1824 		pxa2xx_spi_write(drv_data, SSTO, 0);
1825 
1826 	if (!is_quark_x1000_ssp(drv_data))
1827 		pxa2xx_spi_write(drv_data, SSPSP, 0);
1828 
1829 	if (is_lpss_ssp(drv_data)) {
1830 		lpss_ssp_setup(drv_data);
1831 		config = lpss_get_config(drv_data);
1832 		if (config->reg_capabilities >= 0) {
1833 			tmp = __lpss_ssp_read_priv(drv_data,
1834 						   config->reg_capabilities);
1835 			tmp &= LPSS_CAPS_CS_EN_MASK;
1836 			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
1837 			platform_info->num_chipselect = ffz(tmp);
1838 		} else if (config->cs_num) {
1839 			platform_info->num_chipselect = config->cs_num;
1840 		}
1841 	}
1842 	controller->num_chipselect = platform_info->num_chipselect;
1843 
1844 	count = gpiod_count(&pdev->dev, "cs");
1845 	if (count > 0) {
1846 		int i;
1847 
1848 		controller->num_chipselect = max_t(int, count,
1849 			controller->num_chipselect);
1850 
1851 		drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
1852 			controller->num_chipselect, sizeof(struct gpio_desc *),
1853 			GFP_KERNEL);
1854 		if (!drv_data->cs_gpiods) {
1855 			status = -ENOMEM;
1856 			goto out_error_clock_enabled;
1857 		}
1858 
1859 		for (i = 0; i < controller->num_chipselect; i++) {
1860 			struct gpio_desc *gpiod;
1861 
1862 			gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
1863 			if (IS_ERR(gpiod)) {
1864 				/* Means use native chip select */
1865 				if (PTR_ERR(gpiod) == -ENOENT)
1866 					continue;
1867 
1868 				status = PTR_ERR(gpiod);
1869 				goto out_error_clock_enabled;
1870 			} else {
1871 				drv_data->cs_gpiods[i] = gpiod;
1872 			}
1873 		}
1874 	}
1875 
1876 	if (platform_info->is_slave) {
1877 		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
1878 						"ready", GPIOD_OUT_LOW);
1879 		if (IS_ERR(drv_data->gpiod_ready)) {
1880 			status = PTR_ERR(drv_data->gpiod_ready);
1881 			goto out_error_clock_enabled;
1882 		}
1883 	}
1884 
1885 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1886 	pm_runtime_use_autosuspend(&pdev->dev);
1887 	pm_runtime_set_active(&pdev->dev);
1888 	pm_runtime_enable(&pdev->dev);
1889 
1890 	/* Register with the SPI framework */
1891 	platform_set_drvdata(pdev, drv_data);
1892 	status = spi_register_controller(controller);
1893 	if (status != 0) {
1894 		dev_err(&pdev->dev, "problem registering spi controller\n");
1895 		goto out_error_pm_runtime_enabled;
1896 	}
1897 
1898 	return status;
1899 
1900 out_error_pm_runtime_enabled:
1901 	pm_runtime_disable(&pdev->dev);
1902 
1903 out_error_clock_enabled:
1904 	clk_disable_unprepare(ssp->clk);
1905 
1906 out_error_dma_irq_alloc:
1907 	pxa2xx_spi_dma_release(drv_data);
1908 	free_irq(ssp->irq, drv_data);
1909 
1910 out_error_controller_alloc:
1911 	pxa_ssp_free(ssp);
1912 	return status;
1913 }
1914 
1915 static int pxa2xx_spi_remove(struct platform_device *pdev)
1916 {
1917 	struct driver_data *drv_data = platform_get_drvdata(pdev);
1918 	struct ssp_device *ssp = drv_data->ssp;
1919 
1920 	pm_runtime_get_sync(&pdev->dev);
1921 
1922 	spi_unregister_controller(drv_data->controller);
1923 
1924 	/* Disable the SSP at the peripheral and SOC level */
1925 	pxa_ssp_disable(ssp);
1926 	clk_disable_unprepare(ssp->clk);
1927 
1928 	/* Release DMA */
1929 	if (drv_data->controller_info->enable_dma)
1930 		pxa2xx_spi_dma_release(drv_data);
1931 
1932 	pm_runtime_put_noidle(&pdev->dev);
1933 	pm_runtime_disable(&pdev->dev);
1934 
1935 	/* Release IRQ */
1936 	free_irq(ssp->irq, drv_data);
1937 
1938 	/* Release SSP */
1939 	pxa_ssp_free(ssp);
1940 
1941 	return 0;
1942 }
1943 
1944 #ifdef CONFIG_PM_SLEEP
1945 static int pxa2xx_spi_suspend(struct device *dev)
1946 {
1947 	struct driver_data *drv_data = dev_get_drvdata(dev);
1948 	struct ssp_device *ssp = drv_data->ssp;
1949 	int status;
1950 
1951 	status = spi_controller_suspend(drv_data->controller);
1952 	if (status != 0)
1953 		return status;
1954 
1955 	pxa_ssp_disable(ssp);
1956 
1957 	if (!pm_runtime_suspended(dev))
1958 		clk_disable_unprepare(ssp->clk);
1959 
1960 	return 0;
1961 }
1962 
1963 static int pxa2xx_spi_resume(struct device *dev)
1964 {
1965 	struct driver_data *drv_data = dev_get_drvdata(dev);
1966 	struct ssp_device *ssp = drv_data->ssp;
1967 	int status;
1968 
1969 	/* Enable the SSP clock */
1970 	if (!pm_runtime_suspended(dev)) {
1971 		status = clk_prepare_enable(ssp->clk);
1972 		if (status)
1973 			return status;
1974 	}
1975 
1976 	/* Start the queue running */
1977 	return spi_controller_resume(drv_data->controller);
1978 }
1979 #endif
1980 
1981 #ifdef CONFIG_PM
1982 static int pxa2xx_spi_runtime_suspend(struct device *dev)
1983 {
1984 	struct driver_data *drv_data = dev_get_drvdata(dev);
1985 
1986 	clk_disable_unprepare(drv_data->ssp->clk);
1987 	return 0;
1988 }
1989 
1990 static int pxa2xx_spi_runtime_resume(struct device *dev)
1991 {
1992 	struct driver_data *drv_data = dev_get_drvdata(dev);
1993 	int status;
1994 
1995 	status = clk_prepare_enable(drv_data->ssp->clk);
1996 	return status;
1997 }
1998 #endif
1999 
2000 static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
2001 	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
2002 	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
2003 			   pxa2xx_spi_runtime_resume, NULL)
2004 };
2005 
2006 static struct platform_driver driver = {
2007 	.driver = {
2008 		.name	= "pxa2xx-spi",
2009 		.pm	= &pxa2xx_spi_pm_ops,
2010 		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
2011 		.of_match_table = of_match_ptr(pxa2xx_spi_of_match),
2012 	},
2013 	.probe = pxa2xx_spi_probe,
2014 	.remove = pxa2xx_spi_remove,
2015 };
2016 
2017 static int __init pxa2xx_spi_init(void)
2018 {
2019 	return platform_driver_register(&driver);
2020 }
2021 subsys_initcall(pxa2xx_spi_init);
2022 
2023 static void __exit pxa2xx_spi_exit(void)
2024 {
2025 	platform_driver_unregister(&driver);
2026 }
2027 module_exit(pxa2xx_spi_exit);
2028 
2029 MODULE_SOFTDEP("pre: dw_dmac");
2030