1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
4 * Copyright (C) 2013, 2021 Intel Corporation
7 #include <linux/acpi.h>
8 #include <linux/bitops.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/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/ioport.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/mod_devicetable.h>
23 #include <linux/pci.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/property.h>
27 #include <linux/slab.h>
29 #include <linux/spi/pxa2xx_spi.h>
30 #include <linux/spi/spi.h>
32 #include "spi-pxa2xx.h"
34 MODULE_AUTHOR("Stephen Street");
35 MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
36 MODULE_LICENSE("GPL");
37 MODULE_ALIAS("platform:pxa2xx-spi");
39 #define TIMOUT_DFLT 1000
42 * For testing SSCR1 changes that require SSP restart, basically
43 * everything except the service and interrupt enables, the PXA270 developer
44 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
45 * list, but the PXA255 developer manual says all bits without really meaning
46 * the service and interrupt enables.
48 #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
49 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
50 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
51 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
52 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
53 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
55 #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
56 | QUARK_X1000_SSCR1_EFWR \
57 | QUARK_X1000_SSCR1_RFT \
58 | QUARK_X1000_SSCR1_TFT \
59 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
61 #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
62 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
63 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
64 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
65 | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
66 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
68 #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
69 #define LPSS_CS_CONTROL_SW_MODE BIT(0)
70 #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
71 #define LPSS_CAPS_CS_EN_SHIFT 9
72 #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
74 #define LPSS_PRIV_CLOCK_GATE 0x38
75 #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
76 #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
79 /* LPSS offset from drv_data->ioaddr */
81 /* Register offsets from drv_data->lpss_base or -1 */
90 /* Chip select control */
91 unsigned cs_sel_shift;
95 unsigned cs_clk_stays_gated : 1;
98 /* Keep these sorted with enum pxa_ssp_type */
99 static const struct lpss_config lpss_platforms[] = {
105 .reg_capabilities = -1,
107 .tx_threshold_lo = 160,
108 .tx_threshold_hi = 224,
115 .reg_capabilities = -1,
117 .tx_threshold_lo = 160,
118 .tx_threshold_hi = 224,
125 .reg_capabilities = -1,
127 .tx_threshold_lo = 160,
128 .tx_threshold_hi = 224,
130 .cs_sel_mask = 1 << 2,
138 .reg_capabilities = -1,
140 .tx_threshold_lo = 32,
141 .tx_threshold_hi = 56,
148 .reg_capabilities = 0xfc,
150 .tx_threshold_lo = 16,
151 .tx_threshold_hi = 48,
153 .cs_sel_mask = 3 << 8,
154 .cs_clk_stays_gated = true,
161 .reg_capabilities = 0xfc,
163 .tx_threshold_lo = 32,
164 .tx_threshold_hi = 56,
166 .cs_sel_mask = 3 << 8,
167 .cs_clk_stays_gated = true,
171 static inline const struct lpss_config
172 *lpss_get_config(const struct driver_data *drv_data)
174 return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
177 static bool is_lpss_ssp(const struct driver_data *drv_data)
179 switch (drv_data->ssp_type) {
192 static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
194 return drv_data->ssp_type == QUARK_X1000_SSP;
197 static bool is_mmp2_ssp(const struct driver_data *drv_data)
199 return drv_data->ssp_type == MMP2_SSP;
202 static bool is_mrfld_ssp(const struct driver_data *drv_data)
204 return drv_data->ssp_type == MRFLD_SSP;
207 static void pxa2xx_spi_update(const struct driver_data *drv_data, u32 reg, u32 mask, u32 value)
209 if ((pxa2xx_spi_read(drv_data, reg) & mask) != value)
210 pxa2xx_spi_write(drv_data, reg, value & mask);
213 static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
215 switch (drv_data->ssp_type) {
216 case QUARK_X1000_SSP:
217 return QUARK_X1000_SSCR1_CHANGE_MASK;
219 return CE4100_SSCR1_CHANGE_MASK;
221 return SSCR1_CHANGE_MASK;
226 pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
228 switch (drv_data->ssp_type) {
229 case QUARK_X1000_SSP:
230 return RX_THRESH_QUARK_X1000_DFLT;
232 return RX_THRESH_CE4100_DFLT;
234 return RX_THRESH_DFLT;
238 static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
242 switch (drv_data->ssp_type) {
243 case QUARK_X1000_SSP:
244 mask = QUARK_X1000_SSSR_TFL_MASK;
247 mask = CE4100_SSSR_TFL_MASK;
250 mask = SSSR_TFL_MASK;
254 return read_SSSR_bits(drv_data, mask) == mask;
257 static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
262 switch (drv_data->ssp_type) {
263 case QUARK_X1000_SSP:
264 mask = QUARK_X1000_SSCR1_RFT;
267 mask = CE4100_SSCR1_RFT;
276 static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
277 u32 *sccr1_reg, u32 threshold)
279 switch (drv_data->ssp_type) {
280 case QUARK_X1000_SSP:
281 *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
284 *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
287 *sccr1_reg |= SSCR1_RxTresh(threshold);
292 static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
293 u32 clk_div, u8 bits)
295 switch (drv_data->ssp_type) {
296 case QUARK_X1000_SSP:
298 | QUARK_X1000_SSCR0_Motorola
299 | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits);
303 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
304 | (bits > 16 ? SSCR0_EDSS : 0);
309 * Read and write LPSS SSP private registers. Caller must first check that
310 * is_lpss_ssp() returns true before these can be called.
312 static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
314 WARN_ON(!drv_data->lpss_base);
315 return readl(drv_data->lpss_base + offset);
318 static void __lpss_ssp_write_priv(struct driver_data *drv_data,
319 unsigned offset, u32 value)
321 WARN_ON(!drv_data->lpss_base);
322 writel(value, drv_data->lpss_base + offset);
326 * lpss_ssp_setup - perform LPSS SSP specific setup
327 * @drv_data: pointer to the driver private data
329 * Perform LPSS SSP specific setup. This function must be called first if
330 * one is going to use LPSS SSP private registers.
332 static void lpss_ssp_setup(struct driver_data *drv_data)
334 const struct lpss_config *config;
337 config = lpss_get_config(drv_data);
338 drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset;
340 /* Enable software chip select control */
341 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
342 value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
343 value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
344 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
346 /* Enable multiblock DMA transfers */
347 if (drv_data->controller_info->enable_dma) {
348 __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
350 if (config->reg_general >= 0) {
351 value = __lpss_ssp_read_priv(drv_data,
352 config->reg_general);
353 value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
354 __lpss_ssp_write_priv(drv_data,
355 config->reg_general, value);
360 static void lpss_ssp_select_cs(struct spi_device *spi,
361 const struct lpss_config *config)
363 struct driver_data *drv_data =
364 spi_controller_get_devdata(spi->controller);
367 if (!config->cs_sel_mask)
370 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
372 cs = spi->chip_select;
373 cs <<= config->cs_sel_shift;
374 if (cs != (value & config->cs_sel_mask)) {
376 * When switching another chip select output active the
377 * output must be selected first and wait 2 ssp_clk cycles
378 * before changing state to active. Otherwise a short
379 * glitch will occur on the previous chip select since
380 * output select is latched but state control is not.
382 value &= ~config->cs_sel_mask;
384 __lpss_ssp_write_priv(drv_data,
385 config->reg_cs_ctrl, value);
387 (drv_data->controller->max_speed_hz / 2));
391 static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
393 struct driver_data *drv_data =
394 spi_controller_get_devdata(spi->controller);
395 const struct lpss_config *config;
398 config = lpss_get_config(drv_data);
401 lpss_ssp_select_cs(spi, config);
403 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
405 value &= ~LPSS_CS_CONTROL_CS_HIGH;
407 value |= LPSS_CS_CONTROL_CS_HIGH;
408 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
409 if (config->cs_clk_stays_gated) {
413 * Changing CS alone when dynamic clock gating is on won't
414 * actually flip CS at that time. This ruins SPI transfers
415 * that specify delays, or have no data. Toggle the clock mode
416 * to force on briefly to poke the CS pin to move.
418 clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
419 value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
420 LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
422 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
423 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
427 static void cs_assert(struct spi_device *spi)
429 struct driver_data *drv_data =
430 spi_controller_get_devdata(spi->controller);
432 if (drv_data->ssp_type == CE4100_SSP) {
433 pxa2xx_spi_write(drv_data, SSSR, spi->chip_select);
437 if (is_lpss_ssp(drv_data))
438 lpss_ssp_cs_control(spi, true);
441 static void cs_deassert(struct spi_device *spi)
443 struct driver_data *drv_data =
444 spi_controller_get_devdata(spi->controller);
445 unsigned long timeout;
447 if (drv_data->ssp_type == CE4100_SSP)
450 /* Wait until SSP becomes idle before deasserting the CS */
451 timeout = jiffies + msecs_to_jiffies(10);
452 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
453 !time_after(jiffies, timeout))
456 if (is_lpss_ssp(drv_data))
457 lpss_ssp_cs_control(spi, false);
460 static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
468 int pxa2xx_spi_flush(struct driver_data *drv_data)
470 unsigned long limit = loops_per_jiffy << 1;
473 while (read_SSSR_bits(drv_data, SSSR_RNE))
474 pxa2xx_spi_read(drv_data, SSDR);
475 } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
476 write_SSSR_CS(drv_data, SSSR_ROR);
481 static void pxa2xx_spi_off(struct driver_data *drv_data)
483 /* On MMP, disabling SSE seems to corrupt the Rx FIFO */
484 if (is_mmp2_ssp(drv_data))
487 pxa_ssp_disable(drv_data->ssp);
490 static int null_writer(struct driver_data *drv_data)
492 u8 n_bytes = drv_data->n_bytes;
494 if (pxa2xx_spi_txfifo_full(drv_data)
495 || (drv_data->tx == drv_data->tx_end))
498 pxa2xx_spi_write(drv_data, SSDR, 0);
499 drv_data->tx += n_bytes;
504 static int null_reader(struct driver_data *drv_data)
506 u8 n_bytes = drv_data->n_bytes;
508 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
509 pxa2xx_spi_read(drv_data, SSDR);
510 drv_data->rx += n_bytes;
513 return drv_data->rx == drv_data->rx_end;
516 static int u8_writer(struct driver_data *drv_data)
518 if (pxa2xx_spi_txfifo_full(drv_data)
519 || (drv_data->tx == drv_data->tx_end))
522 pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
528 static int u8_reader(struct driver_data *drv_data)
530 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
531 *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
535 return drv_data->rx == drv_data->rx_end;
538 static int u16_writer(struct driver_data *drv_data)
540 if (pxa2xx_spi_txfifo_full(drv_data)
541 || (drv_data->tx == drv_data->tx_end))
544 pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
550 static int u16_reader(struct driver_data *drv_data)
552 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
553 *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
557 return drv_data->rx == drv_data->rx_end;
560 static int u32_writer(struct driver_data *drv_data)
562 if (pxa2xx_spi_txfifo_full(drv_data)
563 || (drv_data->tx == drv_data->tx_end))
566 pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
572 static int u32_reader(struct driver_data *drv_data)
574 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
575 *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
579 return drv_data->rx == drv_data->rx_end;
582 static void reset_sccr1(struct driver_data *drv_data)
584 u32 mask = drv_data->int_cr1 | drv_data->dma_cr1, threshold;
585 struct chip_data *chip;
587 if (drv_data->controller->cur_msg) {
588 chip = spi_get_ctldata(drv_data->controller->cur_msg->spi);
589 threshold = chip->threshold;
594 switch (drv_data->ssp_type) {
595 case QUARK_X1000_SSP:
596 mask |= QUARK_X1000_SSCR1_RFT;
599 mask |= CE4100_SSCR1_RFT;
606 pxa2xx_spi_update(drv_data, SSCR1, mask, threshold);
609 static void int_stop_and_reset(struct driver_data *drv_data)
611 /* Clear and disable interrupts */
612 write_SSSR_CS(drv_data, drv_data->clear_sr);
613 reset_sccr1(drv_data);
614 if (pxa25x_ssp_comp(drv_data))
617 pxa2xx_spi_write(drv_data, SSTO, 0);
620 static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
622 int_stop_and_reset(drv_data);
623 pxa2xx_spi_flush(drv_data);
624 pxa2xx_spi_off(drv_data);
626 dev_err(drv_data->ssp->dev, "%s\n", msg);
628 drv_data->controller->cur_msg->status = err;
629 spi_finalize_current_transfer(drv_data->controller);
632 static void int_transfer_complete(struct driver_data *drv_data)
634 int_stop_and_reset(drv_data);
636 spi_finalize_current_transfer(drv_data->controller);
639 static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
643 irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr);
644 if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE))
645 irq_status &= ~SSSR_TFS;
647 if (irq_status & SSSR_ROR) {
648 int_error_stop(drv_data, "interrupt_transfer: FIFO overrun", -EIO);
652 if (irq_status & SSSR_TUR) {
653 int_error_stop(drv_data, "interrupt_transfer: FIFO underrun", -EIO);
657 if (irq_status & SSSR_TINT) {
658 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
659 if (drv_data->read(drv_data)) {
660 int_transfer_complete(drv_data);
665 /* Drain Rx FIFO, Fill Tx FIFO and prevent overruns */
667 if (drv_data->read(drv_data)) {
668 int_transfer_complete(drv_data);
671 } while (drv_data->write(drv_data));
673 if (drv_data->read(drv_data)) {
674 int_transfer_complete(drv_data);
678 if (drv_data->tx == drv_data->tx_end) {
682 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
683 sccr1_reg &= ~SSCR1_TIE;
686 * PXA25x_SSP has no timeout, set up Rx threshold for
687 * the remaining Rx bytes.
689 if (pxa25x_ssp_comp(drv_data)) {
692 pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
694 bytes_left = drv_data->rx_end - drv_data->rx;
695 switch (drv_data->n_bytes) {
704 rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
705 if (rx_thre > bytes_left)
706 rx_thre = bytes_left;
708 pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
710 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
713 /* We did something */
717 static void handle_bad_msg(struct driver_data *drv_data)
719 int_stop_and_reset(drv_data);
720 pxa2xx_spi_off(drv_data);
722 dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
725 static irqreturn_t ssp_int(int irq, void *dev_id)
727 struct driver_data *drv_data = dev_id;
729 u32 mask = drv_data->mask_sr;
733 * The IRQ might be shared with other peripherals so we must first
734 * check that are we RPM suspended or not. If we are we assume that
735 * the IRQ was not for us (we shouldn't be RPM suspended when the
736 * interrupt is enabled).
738 if (pm_runtime_suspended(drv_data->ssp->dev))
742 * If the device is not yet in RPM suspended state and we get an
743 * interrupt that is meant for another device, check if status bits
744 * are all set to one. That means that the device is already
747 status = pxa2xx_spi_read(drv_data, SSSR);
751 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
753 /* Ignore possible writes if we don't need to write */
754 if (!(sccr1_reg & SSCR1_TIE))
757 /* Ignore RX timeout interrupt if it is disabled */
758 if (!(sccr1_reg & SSCR1_TINTE))
761 if (!(status & mask))
764 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
765 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
767 if (!drv_data->controller->cur_msg) {
768 handle_bad_msg(drv_data);
773 return drv_data->transfer_handler(drv_data);
777 * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
778 * input frequency by fractions of 2^24. It also has a divider by 5.
780 * There are formulas to get baud rate value for given input frequency and
781 * divider parameters, such as DDS_CLK_RATE and SCR:
785 * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
786 * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
788 * DDS_CLK_RATE either 2^n or 2^n / 5.
789 * SCR is in range 0 .. 255
791 * Divisor = 5^i * 2^j * 2 * k
792 * i = [0, 1] i = 1 iff j = 0 or j > 3
793 * j = [0, 23] j = 0 iff i = 1
795 * Special case: j = 0, i = 1: Divisor = 2 / 5
797 * Accordingly to the specification the recommended values for DDS_CLK_RATE
799 * Case 1: 2^n, n = [0, 23]
800 * Case 2: 2^24 * 2 / 5 (0x666666)
801 * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
803 * In all cases the lowest possible value is better.
805 * The function calculates parameters for all cases and chooses the one closest
806 * to the asked baud rate.
808 static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
810 unsigned long xtal = 200000000;
811 unsigned long fref = xtal / 2; /* mandatory division by 2,
814 unsigned long fref1 = fref / 2; /* case 1 */
815 unsigned long fref2 = fref * 2 / 5; /* case 2 */
817 unsigned long q, q1, q2;
823 /* Set initial value for DDS_CLK_RATE */
824 mul = (1 << 24) >> 1;
826 /* Calculate initial quot */
827 q1 = DIV_ROUND_UP(fref1, rate);
829 /* Scale q1 if it's too big */
831 /* Scale q1 to range [1, 512] */
832 scale = fls_long(q1 - 1);
838 /* Round the result if we have a remainder */
842 /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
847 /* Get the remainder */
848 r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
852 q2 = DIV_ROUND_UP(fref2, rate);
853 r2 = abs(fref2 / q2 - rate);
856 * Choose the best between two: less remainder we have the better. We
857 * can't go case 2 if q2 is greater than 256 since SCR register can
858 * hold only values 0 .. 255.
860 if (r2 >= r1 || q2 > 256) {
861 /* case 1 is better */
865 /* case 2 is better */
868 mul = (1 << 24) * 2 / 5;
871 /* Check case 3 only if the divisor is big enough */
872 if (fref / rate >= 80) {
876 /* Calculate initial quot */
877 q1 = DIV_ROUND_UP(fref, rate);
880 /* Get the remainder */
881 fssp = (u64)fref * m;
882 do_div(fssp, 1 << 24);
883 r1 = abs(fssp - rate);
885 /* Choose this one if it suits better */
887 /* case 3 is better */
897 static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
899 unsigned long ssp_clk = drv_data->controller->max_speed_hz;
900 const struct ssp_device *ssp = drv_data->ssp;
902 rate = min_t(int, ssp_clk, rate);
905 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
906 * that the SSP transmission rate can be greater than the device rate.
908 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
909 return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
911 return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff;
914 static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
917 struct chip_data *chip =
918 spi_get_ctldata(drv_data->controller->cur_msg->spi);
919 unsigned int clk_div;
921 switch (drv_data->ssp_type) {
922 case QUARK_X1000_SSP:
923 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
926 clk_div = ssp_get_clk_div(drv_data, rate);
932 static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
933 struct spi_device *spi,
934 struct spi_transfer *xfer)
936 struct chip_data *chip = spi_get_ctldata(spi);
938 return chip->enable_dma &&
939 xfer->len <= MAX_DMA_LEN &&
940 xfer->len >= chip->dma_burst_size;
943 static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
944 struct spi_device *spi,
945 struct spi_transfer *transfer)
947 struct driver_data *drv_data = spi_controller_get_devdata(controller);
948 struct spi_message *message = controller->cur_msg;
949 struct chip_data *chip = spi_get_ctldata(spi);
950 u32 dma_thresh = chip->dma_threshold;
951 u32 dma_burst = chip->dma_burst_size;
952 u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
961 /* Check if we can DMA this transfer */
962 if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
964 /* Reject already-mapped transfers; PIO won't always work */
965 if (message->is_dma_mapped
966 || transfer->rx_dma || transfer->tx_dma) {
968 "Mapped transfer length of %u is greater than %d\n",
969 transfer->len, MAX_DMA_LEN);
973 /* Warn ... we force this to PIO mode */
974 dev_warn_ratelimited(&spi->dev,
975 "DMA disabled for transfer length %u greater than %d\n",
976 transfer->len, MAX_DMA_LEN);
979 /* Setup the transfer state based on the type of transfer */
980 if (pxa2xx_spi_flush(drv_data) == 0) {
981 dev_err(&spi->dev, "Flush failed\n");
984 drv_data->tx = (void *)transfer->tx_buf;
985 drv_data->tx_end = drv_data->tx + transfer->len;
986 drv_data->rx = transfer->rx_buf;
987 drv_data->rx_end = drv_data->rx + transfer->len;
989 /* Change speed and bit per word on a per transfer */
990 bits = transfer->bits_per_word;
991 speed = transfer->speed_hz;
993 clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
996 drv_data->n_bytes = 1;
997 drv_data->read = drv_data->rx ? u8_reader : null_reader;
998 drv_data->write = drv_data->tx ? u8_writer : null_writer;
999 } else if (bits <= 16) {
1000 drv_data->n_bytes = 2;
1001 drv_data->read = drv_data->rx ? u16_reader : null_reader;
1002 drv_data->write = drv_data->tx ? u16_writer : null_writer;
1003 } else if (bits <= 32) {
1004 drv_data->n_bytes = 4;
1005 drv_data->read = drv_data->rx ? u32_reader : null_reader;
1006 drv_data->write = drv_data->tx ? u32_writer : null_writer;
1009 * If bits per word is changed in DMA mode, then must check
1010 * the thresholds and burst also.
1012 if (chip->enable_dma) {
1013 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
1017 dev_warn_ratelimited(&spi->dev,
1018 "DMA burst size reduced to match bits_per_word\n");
1021 dma_mapped = controller->can_dma &&
1022 controller->can_dma(controller, spi, transfer) &&
1023 controller->cur_msg_mapped;
1026 /* Ensure we have the correct interrupt handler */
1027 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1029 err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1033 /* Clear status and start DMA engine */
1034 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1035 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1037 pxa2xx_spi_dma_start(drv_data);
1039 /* Ensure we have the correct interrupt handler */
1040 drv_data->transfer_handler = interrupt_transfer;
1043 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1044 write_SSSR_CS(drv_data, drv_data->clear_sr);
1047 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1048 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1049 if (!pxa25x_ssp_comp(drv_data))
1050 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
1051 controller->max_speed_hz
1052 / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1053 dma_mapped ? "DMA" : "PIO");
1055 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
1056 controller->max_speed_hz / 2
1057 / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1058 dma_mapped ? "DMA" : "PIO");
1060 if (is_lpss_ssp(drv_data)) {
1061 pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold);
1062 pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold);
1065 if (is_mrfld_ssp(drv_data)) {
1066 u32 mask = SFIFOTT_RFT | SFIFOTT_TFT;
1069 thresh |= SFIFOTT_RxThresh(chip->lpss_rx_threshold);
1070 thresh |= SFIFOTT_TxThresh(chip->lpss_tx_threshold);
1072 pxa2xx_spi_update(drv_data, SFIFOTT, mask, thresh);
1075 if (is_quark_x1000_ssp(drv_data))
1076 pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate);
1079 if (!is_mmp2_ssp(drv_data))
1080 pxa_ssp_disable(drv_data->ssp);
1082 if (!pxa25x_ssp_comp(drv_data))
1083 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1085 /* First set CR1 without interrupt and service enables */
1086 pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1);
1088 /* See if we need to reload the configuration registers */
1089 pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0);
1091 /* Restart the SSP */
1092 pxa_ssp_enable(drv_data->ssp);
1094 if (is_mmp2_ssp(drv_data)) {
1095 u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8;
1098 /* On MMP2, flipping SSE doesn't to empty Tx FIFO. */
1099 dev_warn(&spi->dev, "%u bytes of garbage in Tx FIFO!\n", tx_level);
1100 if (tx_level > transfer->len)
1101 tx_level = transfer->len;
1102 drv_data->tx += tx_level;
1106 if (spi_controller_is_slave(controller)) {
1107 while (drv_data->write(drv_data))
1109 if (drv_data->gpiod_ready) {
1110 gpiod_set_value(drv_data->gpiod_ready, 1);
1112 gpiod_set_value(drv_data->gpiod_ready, 0);
1117 * Release the data by enabling service requests and interrupts,
1118 * without changing any mode bits.
1120 pxa2xx_spi_write(drv_data, SSCR1, cr1);
1125 static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
1127 struct driver_data *drv_data = spi_controller_get_devdata(controller);
1129 int_error_stop(drv_data, "transfer aborted", -EINTR);
1134 static void pxa2xx_spi_handle_err(struct spi_controller *controller,
1135 struct spi_message *msg)
1137 struct driver_data *drv_data = spi_controller_get_devdata(controller);
1139 int_stop_and_reset(drv_data);
1141 /* Disable the SSP */
1142 pxa2xx_spi_off(drv_data);
1145 * Stop the DMA if running. Note DMA callback handler may have unset
1146 * the dma_running already, which is fine as stopping is not needed
1147 * then but we shouldn't rely this flag for anything else than
1148 * stopping. For instance to differentiate between PIO and DMA
1151 if (atomic_read(&drv_data->dma_running))
1152 pxa2xx_spi_dma_stop(drv_data);
1155 static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
1157 struct driver_data *drv_data = spi_controller_get_devdata(controller);
1159 /* Disable the SSP now */
1160 pxa2xx_spi_off(drv_data);
1165 static int setup(struct spi_device *spi)
1167 struct pxa2xx_spi_chip *chip_info;
1168 struct chip_data *chip;
1169 const struct lpss_config *config;
1170 struct driver_data *drv_data =
1171 spi_controller_get_devdata(spi->controller);
1172 uint tx_thres, tx_hi_thres, rx_thres;
1174 switch (drv_data->ssp_type) {
1175 case QUARK_X1000_SSP:
1176 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1178 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1181 tx_thres = TX_THRESH_MRFLD_DFLT;
1183 rx_thres = RX_THRESH_MRFLD_DFLT;
1186 tx_thres = TX_THRESH_CE4100_DFLT;
1188 rx_thres = RX_THRESH_CE4100_DFLT;
1196 config = lpss_get_config(drv_data);
1197 tx_thres = config->tx_threshold_lo;
1198 tx_hi_thres = config->tx_threshold_hi;
1199 rx_thres = config->rx_threshold;
1203 if (spi_controller_is_slave(drv_data->controller)) {
1207 tx_thres = TX_THRESH_DFLT;
1208 rx_thres = RX_THRESH_DFLT;
1213 /* Only allocate on the first setup */
1214 chip = spi_get_ctldata(spi);
1216 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1220 if (drv_data->ssp_type == CE4100_SSP) {
1221 if (spi->chip_select > 4) {
1223 "failed setup: cs number must not be > 4.\n");
1228 chip->enable_dma = drv_data->controller_info->enable_dma;
1229 chip->timeout = TIMOUT_DFLT;
1233 * Protocol drivers may change the chip settings, so...
1234 * if chip_info exists, use it.
1236 chip_info = spi->controller_data;
1238 /* chip_info isn't always needed */
1240 if (chip_info->timeout)
1241 chip->timeout = chip_info->timeout;
1242 if (chip_info->tx_threshold)
1243 tx_thres = chip_info->tx_threshold;
1244 if (chip_info->tx_hi_threshold)
1245 tx_hi_thres = chip_info->tx_hi_threshold;
1246 if (chip_info->rx_threshold)
1247 rx_thres = chip_info->rx_threshold;
1248 chip->dma_threshold = 0;
1252 if (spi_controller_is_slave(drv_data->controller)) {
1253 chip->cr1 |= SSCR1_SCFR;
1254 chip->cr1 |= SSCR1_SCLKDIR;
1255 chip->cr1 |= SSCR1_SFRMDIR;
1256 chip->cr1 |= SSCR1_SPH;
1259 if (is_lpss_ssp(drv_data)) {
1260 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1261 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
1262 SSITF_TxHiThresh(tx_hi_thres);
1265 if (is_mrfld_ssp(drv_data)) {
1266 chip->lpss_rx_threshold = rx_thres;
1267 chip->lpss_tx_threshold = tx_thres;
1271 * Set DMA burst and threshold outside of chip_info path so that if
1272 * chip_info goes away after setting chip->enable_dma, the burst and
1273 * threshold can still respond to changes in bits_per_word.
1275 if (chip->enable_dma) {
1276 /* Set up legal burst and threshold for DMA */
1277 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1279 &chip->dma_burst_size,
1280 &chip->dma_threshold)) {
1282 "in setup: DMA burst size reduced to match bits_per_word\n");
1285 "in setup: DMA burst size set to %u\n",
1286 chip->dma_burst_size);
1289 switch (drv_data->ssp_type) {
1290 case QUARK_X1000_SSP:
1291 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1292 & QUARK_X1000_SSCR1_RFT)
1293 | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1294 & QUARK_X1000_SSCR1_TFT);
1297 chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
1298 (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
1301 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1302 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1306 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1307 chip->cr1 |= ((spi->mode & SPI_CPHA) ? SSCR1_SPH : 0) |
1308 ((spi->mode & SPI_CPOL) ? SSCR1_SPO : 0);
1310 if (spi->mode & SPI_LOOP)
1311 chip->cr1 |= SSCR1_LBM;
1313 spi_set_ctldata(spi, chip);
1318 static void cleanup(struct spi_device *spi)
1320 struct chip_data *chip = spi_get_ctldata(spi);
1326 static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
1327 { "INT33C0", LPSS_LPT_SSP },
1328 { "INT33C1", LPSS_LPT_SSP },
1329 { "INT3430", LPSS_LPT_SSP },
1330 { "INT3431", LPSS_LPT_SSP },
1331 { "80860F0E", LPSS_BYT_SSP },
1332 { "8086228E", LPSS_BSW_SSP },
1335 MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1339 * PCI IDs of compound devices that integrate both host controller and private
1340 * integrated DMA engine. Please note these are not used in module
1341 * autoloading and probing in this module but matching the LPSS SSP type.
1343 static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
1345 { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
1346 { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
1348 { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
1349 { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1351 { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1352 { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
1354 { PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
1355 { PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
1357 { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1358 { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1359 { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1361 { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1362 { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1363 { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1365 { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1366 { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1367 { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
1369 { PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
1370 { PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
1371 { PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
1373 { PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
1374 { PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
1375 { PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
1377 { PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
1378 { PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
1379 { PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
1381 { PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
1382 { PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
1383 { PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
1384 { PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
1386 { PCI_VDEVICE(INTEL, 0x51aa), LPSS_CNL_SSP },
1387 { PCI_VDEVICE(INTEL, 0x51ab), LPSS_CNL_SSP },
1388 { PCI_VDEVICE(INTEL, 0x51fb), LPSS_CNL_SSP },
1390 { PCI_VDEVICE(INTEL, 0x54aa), LPSS_CNL_SSP },
1391 { PCI_VDEVICE(INTEL, 0x54ab), LPSS_CNL_SSP },
1392 { PCI_VDEVICE(INTEL, 0x54fb), LPSS_CNL_SSP },
1394 { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1395 { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1396 { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1398 { PCI_VDEVICE(INTEL, 0x7a2a), LPSS_CNL_SSP },
1399 { PCI_VDEVICE(INTEL, 0x7a2b), LPSS_CNL_SSP },
1400 { PCI_VDEVICE(INTEL, 0x7a79), LPSS_CNL_SSP },
1401 { PCI_VDEVICE(INTEL, 0x7a7b), LPSS_CNL_SSP },
1403 { PCI_VDEVICE(INTEL, 0x7aaa), LPSS_CNL_SSP },
1404 { PCI_VDEVICE(INTEL, 0x7aab), LPSS_CNL_SSP },
1405 { PCI_VDEVICE(INTEL, 0x7af9), LPSS_CNL_SSP },
1406 { PCI_VDEVICE(INTEL, 0x7afb), LPSS_CNL_SSP },
1408 { PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1409 { PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1410 { PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1412 { PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1413 { PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1414 { PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
1416 { PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
1417 { PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
1418 { PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
1420 { PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1421 { PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1422 { PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
1424 { PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1425 { PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1426 { PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1427 { PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1428 { PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1429 { PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1430 { PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
1434 static const struct of_device_id pxa2xx_spi_of_match[] = {
1435 { .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
1438 MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
1442 static int pxa2xx_spi_get_port_id(struct device *dev)
1444 struct acpi_device *adev;
1448 adev = ACPI_COMPANION(dev);
1449 if (adev && adev->pnp.unique_id &&
1450 !kstrtouint(adev->pnp.unique_id, 0, &devid))
1455 #else /* !CONFIG_ACPI */
1457 static int pxa2xx_spi_get_port_id(struct device *dev)
1462 #endif /* CONFIG_ACPI */
1467 static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
1469 return param == chan->device->dev;
1472 #endif /* CONFIG_PCI */
1474 static struct pxa2xx_spi_controller *
1475 pxa2xx_spi_init_pdata(struct platform_device *pdev)
1477 struct pxa2xx_spi_controller *pdata;
1478 struct ssp_device *ssp;
1479 struct resource *res;
1480 struct device *parent = pdev->dev.parent;
1481 struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
1482 const struct pci_device_id *pcidev_id = NULL;
1483 enum pxa_ssp_type type;
1487 pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
1489 match = device_get_match_data(&pdev->dev);
1491 type = (enum pxa_ssp_type)match;
1493 type = (enum pxa_ssp_type)pcidev_id->driver_data;
1495 return ERR_PTR(-EINVAL);
1497 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1499 return ERR_PTR(-ENOMEM);
1503 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1504 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1505 if (IS_ERR(ssp->mmio_base))
1506 return ERR_CAST(ssp->mmio_base);
1508 ssp->phys_base = res->start;
1512 pdata->tx_param = parent;
1513 pdata->rx_param = parent;
1514 pdata->dma_filter = pxa2xx_spi_idma_filter;
1518 ssp->clk = devm_clk_get(&pdev->dev, NULL);
1519 if (IS_ERR(ssp->clk))
1520 return ERR_CAST(ssp->clk);
1522 ssp->irq = platform_get_irq(pdev, 0);
1524 return ERR_PTR(ssp->irq);
1527 ssp->dev = &pdev->dev;
1528 ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
1530 pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
1531 pdata->num_chipselect = 1;
1532 pdata->enable_dma = true;
1533 pdata->dma_burst_size = 1;
1538 static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
1541 struct driver_data *drv_data = spi_controller_get_devdata(controller);
1543 if (has_acpi_companion(drv_data->ssp->dev)) {
1544 switch (drv_data->ssp_type) {
1546 * For Atoms the ACPI DeviceSelection used by the Windows
1547 * driver starts from 1 instead of 0 so translate it here
1548 * to match what Linux expects.
1562 static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1567 static int pxa2xx_spi_probe(struct platform_device *pdev)
1569 struct device *dev = &pdev->dev;
1570 struct pxa2xx_spi_controller *platform_info;
1571 struct spi_controller *controller;
1572 struct driver_data *drv_data;
1573 struct ssp_device *ssp;
1574 const struct lpss_config *config;
1578 platform_info = dev_get_platdata(dev);
1579 if (!platform_info) {
1580 platform_info = pxa2xx_spi_init_pdata(pdev);
1581 if (IS_ERR(platform_info)) {
1582 dev_err(&pdev->dev, "missing platform data\n");
1583 return PTR_ERR(platform_info);
1587 ssp = pxa_ssp_request(pdev->id, pdev->name);
1589 ssp = &platform_info->ssp;
1591 if (!ssp->mmio_base) {
1592 dev_err(&pdev->dev, "failed to get SSP\n");
1596 if (platform_info->is_slave)
1597 controller = devm_spi_alloc_slave(dev, sizeof(*drv_data));
1599 controller = devm_spi_alloc_master(dev, sizeof(*drv_data));
1602 dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1604 goto out_error_controller_alloc;
1606 drv_data = spi_controller_get_devdata(controller);
1607 drv_data->controller = controller;
1608 drv_data->controller_info = platform_info;
1609 drv_data->ssp = ssp;
1611 device_set_node(&controller->dev, dev_fwnode(dev));
1613 /* The spi->mode bits understood by this driver: */
1614 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1616 controller->bus_num = ssp->port_id;
1617 controller->dma_alignment = DMA_ALIGNMENT;
1618 controller->cleanup = cleanup;
1619 controller->setup = setup;
1620 controller->set_cs = pxa2xx_spi_set_cs;
1621 controller->transfer_one = pxa2xx_spi_transfer_one;
1622 controller->slave_abort = pxa2xx_spi_slave_abort;
1623 controller->handle_err = pxa2xx_spi_handle_err;
1624 controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
1625 controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
1626 controller->auto_runtime_pm = true;
1627 controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1629 drv_data->ssp_type = ssp->type;
1631 if (pxa25x_ssp_comp(drv_data)) {
1632 switch (drv_data->ssp_type) {
1633 case QUARK_X1000_SSP:
1634 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1637 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1641 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1642 drv_data->dma_cr1 = 0;
1643 drv_data->clear_sr = SSSR_ROR;
1644 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1646 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1647 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1648 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1649 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1650 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1651 | SSSR_ROR | SSSR_TUR;
1654 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1657 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
1658 goto out_error_controller_alloc;
1661 /* Setup DMA if requested */
1662 if (platform_info->enable_dma) {
1663 status = pxa2xx_spi_dma_setup(drv_data);
1665 dev_warn(dev, "no DMA channels available, using PIO\n");
1666 platform_info->enable_dma = false;
1668 controller->can_dma = pxa2xx_spi_can_dma;
1669 controller->max_dma_len = MAX_DMA_LEN;
1670 controller->max_transfer_size =
1671 pxa2xx_spi_max_dma_transfer_size;
1675 /* Enable SOC clock */
1676 status = clk_prepare_enable(ssp->clk);
1678 goto out_error_dma_irq_alloc;
1680 controller->max_speed_hz = clk_get_rate(ssp->clk);
1682 * Set minimum speed for all other platforms than Intel Quark which is
1683 * able do under 1 Hz transfers.
1685 if (!pxa25x_ssp_comp(drv_data))
1686 controller->min_speed_hz =
1687 DIV_ROUND_UP(controller->max_speed_hz, 4096);
1688 else if (!is_quark_x1000_ssp(drv_data))
1689 controller->min_speed_hz =
1690 DIV_ROUND_UP(controller->max_speed_hz, 512);
1692 pxa_ssp_disable(ssp);
1694 /* Load default SSP configuration */
1695 switch (drv_data->ssp_type) {
1696 case QUARK_X1000_SSP:
1697 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
1698 QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1699 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1701 /* Using the Motorola SPI protocol and use 8 bit frame */
1702 tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
1703 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1706 tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
1707 CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
1708 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1709 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1710 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1714 if (spi_controller_is_slave(controller)) {
1722 tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1723 SSCR1_TxTresh(TX_THRESH_DFLT);
1725 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1726 tmp = SSCR0_Motorola | SSCR0_DataSize(8);
1727 if (!spi_controller_is_slave(controller))
1728 tmp |= SSCR0_SCR(2);
1729 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1733 if (!pxa25x_ssp_comp(drv_data))
1734 pxa2xx_spi_write(drv_data, SSTO, 0);
1736 if (!is_quark_x1000_ssp(drv_data))
1737 pxa2xx_spi_write(drv_data, SSPSP, 0);
1739 if (is_lpss_ssp(drv_data)) {
1740 lpss_ssp_setup(drv_data);
1741 config = lpss_get_config(drv_data);
1742 if (config->reg_capabilities >= 0) {
1743 tmp = __lpss_ssp_read_priv(drv_data,
1744 config->reg_capabilities);
1745 tmp &= LPSS_CAPS_CS_EN_MASK;
1746 tmp >>= LPSS_CAPS_CS_EN_SHIFT;
1747 platform_info->num_chipselect = ffz(tmp);
1748 } else if (config->cs_num) {
1749 platform_info->num_chipselect = config->cs_num;
1752 controller->num_chipselect = platform_info->num_chipselect;
1753 controller->use_gpio_descriptors = true;
1755 if (platform_info->is_slave) {
1756 drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
1757 "ready", GPIOD_OUT_LOW);
1758 if (IS_ERR(drv_data->gpiod_ready)) {
1759 status = PTR_ERR(drv_data->gpiod_ready);
1760 goto out_error_clock_enabled;
1764 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1765 pm_runtime_use_autosuspend(&pdev->dev);
1766 pm_runtime_set_active(&pdev->dev);
1767 pm_runtime_enable(&pdev->dev);
1769 /* Register with the SPI framework */
1770 platform_set_drvdata(pdev, drv_data);
1771 status = spi_register_controller(controller);
1773 dev_err(&pdev->dev, "problem registering SPI controller\n");
1774 goto out_error_pm_runtime_enabled;
1779 out_error_pm_runtime_enabled:
1780 pm_runtime_disable(&pdev->dev);
1782 out_error_clock_enabled:
1783 clk_disable_unprepare(ssp->clk);
1785 out_error_dma_irq_alloc:
1786 pxa2xx_spi_dma_release(drv_data);
1787 free_irq(ssp->irq, drv_data);
1789 out_error_controller_alloc:
1794 static int pxa2xx_spi_remove(struct platform_device *pdev)
1796 struct driver_data *drv_data = platform_get_drvdata(pdev);
1797 struct ssp_device *ssp = drv_data->ssp;
1799 pm_runtime_get_sync(&pdev->dev);
1801 spi_unregister_controller(drv_data->controller);
1803 /* Disable the SSP at the peripheral and SOC level */
1804 pxa_ssp_disable(ssp);
1805 clk_disable_unprepare(ssp->clk);
1808 if (drv_data->controller_info->enable_dma)
1809 pxa2xx_spi_dma_release(drv_data);
1811 pm_runtime_put_noidle(&pdev->dev);
1812 pm_runtime_disable(&pdev->dev);
1815 free_irq(ssp->irq, drv_data);
1823 #ifdef CONFIG_PM_SLEEP
1824 static int pxa2xx_spi_suspend(struct device *dev)
1826 struct driver_data *drv_data = dev_get_drvdata(dev);
1827 struct ssp_device *ssp = drv_data->ssp;
1830 status = spi_controller_suspend(drv_data->controller);
1834 pxa_ssp_disable(ssp);
1836 if (!pm_runtime_suspended(dev))
1837 clk_disable_unprepare(ssp->clk);
1842 static int pxa2xx_spi_resume(struct device *dev)
1844 struct driver_data *drv_data = dev_get_drvdata(dev);
1845 struct ssp_device *ssp = drv_data->ssp;
1848 /* Enable the SSP clock */
1849 if (!pm_runtime_suspended(dev)) {
1850 status = clk_prepare_enable(ssp->clk);
1855 /* Start the queue running */
1856 return spi_controller_resume(drv_data->controller);
1861 static int pxa2xx_spi_runtime_suspend(struct device *dev)
1863 struct driver_data *drv_data = dev_get_drvdata(dev);
1865 clk_disable_unprepare(drv_data->ssp->clk);
1869 static int pxa2xx_spi_runtime_resume(struct device *dev)
1871 struct driver_data *drv_data = dev_get_drvdata(dev);
1874 status = clk_prepare_enable(drv_data->ssp->clk);
1879 static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
1880 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1881 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
1882 pxa2xx_spi_runtime_resume, NULL)
1885 static struct platform_driver driver = {
1887 .name = "pxa2xx-spi",
1888 .pm = &pxa2xx_spi_pm_ops,
1889 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
1890 .of_match_table = of_match_ptr(pxa2xx_spi_of_match),
1892 .probe = pxa2xx_spi_probe,
1893 .remove = pxa2xx_spi_remove,
1896 static int __init pxa2xx_spi_init(void)
1898 return platform_driver_register(&driver);
1900 subsys_initcall(pxa2xx_spi_init);
1902 static void __exit pxa2xx_spi_exit(void)
1904 platform_driver_unregister(&driver);
1906 module_exit(pxa2xx_spi_exit);
1908 MODULE_SOFTDEP("pre: dw_dmac");