1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
4 * Copyright (C) 2013, Intel Corporation
7 #include <linux/bitops.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/device.h>
11 #include <linux/ioport.h>
12 #include <linux/errno.h>
13 #include <linux/err.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/platform_device.h>
18 #include <linux/spi/pxa2xx_spi.h>
19 #include <linux/spi/spi.h>
20 #include <linux/delay.h>
21 #include <linux/gpio.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/slab.h>
24 #include <linux/clk.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/acpi.h>
27 #include <linux/of_device.h>
29 #include "spi-pxa2xx.h"
31 MODULE_AUTHOR("Stephen Street");
32 MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
33 MODULE_LICENSE("GPL");
34 MODULE_ALIAS("platform:pxa2xx-spi");
36 #define TIMOUT_DFLT 1000
39 * for testing SSCR1 changes that require SSP restart, basically
40 * everything except the service and interrupt enables, the pxa270 developer
41 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
42 * list, but the PXA255 dev man says all bits without really meaning the
43 * service and interrupt enables
45 #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
46 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
47 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
48 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
49 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
50 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
52 #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
53 | QUARK_X1000_SSCR1_EFWR \
54 | QUARK_X1000_SSCR1_RFT \
55 | QUARK_X1000_SSCR1_TFT \
56 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
58 #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
59 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
60 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
61 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
62 | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
63 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
65 #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
66 #define LPSS_CS_CONTROL_SW_MODE BIT(0)
67 #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
68 #define LPSS_CAPS_CS_EN_SHIFT 9
69 #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
71 #define LPSS_PRIV_CLOCK_GATE 0x38
72 #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
73 #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
76 /* LPSS offset from drv_data->ioaddr */
78 /* Register offsets from drv_data->lpss_base or -1 */
87 /* Chip select control */
88 unsigned cs_sel_shift
;
92 unsigned cs_clk_stays_gated
: 1;
95 /* Keep these sorted with enum pxa_ssp_type */
96 static const struct lpss_config lpss_platforms
[] = {
102 .reg_capabilities
= -1,
104 .tx_threshold_lo
= 160,
105 .tx_threshold_hi
= 224,
112 .reg_capabilities
= -1,
114 .tx_threshold_lo
= 160,
115 .tx_threshold_hi
= 224,
122 .reg_capabilities
= -1,
124 .tx_threshold_lo
= 160,
125 .tx_threshold_hi
= 224,
127 .cs_sel_mask
= 1 << 2,
135 .reg_capabilities
= -1,
137 .tx_threshold_lo
= 32,
138 .tx_threshold_hi
= 56,
145 .reg_capabilities
= 0xfc,
147 .tx_threshold_lo
= 16,
148 .tx_threshold_hi
= 48,
150 .cs_sel_mask
= 3 << 8,
151 .cs_clk_stays_gated
= true,
158 .reg_capabilities
= 0xfc,
160 .tx_threshold_lo
= 32,
161 .tx_threshold_hi
= 56,
163 .cs_sel_mask
= 3 << 8,
164 .cs_clk_stays_gated
= true,
168 static inline const struct lpss_config
169 *lpss_get_config(const struct driver_data
*drv_data
)
171 return &lpss_platforms
[drv_data
->ssp_type
- LPSS_LPT_SSP
];
174 static bool is_lpss_ssp(const struct driver_data
*drv_data
)
176 switch (drv_data
->ssp_type
) {
189 static bool is_quark_x1000_ssp(const struct driver_data
*drv_data
)
191 return drv_data
->ssp_type
== QUARK_X1000_SSP
;
194 static u32
pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data
*drv_data
)
196 switch (drv_data
->ssp_type
) {
197 case QUARK_X1000_SSP
:
198 return QUARK_X1000_SSCR1_CHANGE_MASK
;
200 return CE4100_SSCR1_CHANGE_MASK
;
202 return SSCR1_CHANGE_MASK
;
207 pxa2xx_spi_get_rx_default_thre(const struct driver_data
*drv_data
)
209 switch (drv_data
->ssp_type
) {
210 case QUARK_X1000_SSP
:
211 return RX_THRESH_QUARK_X1000_DFLT
;
213 return RX_THRESH_CE4100_DFLT
;
215 return RX_THRESH_DFLT
;
219 static bool pxa2xx_spi_txfifo_full(const struct driver_data
*drv_data
)
223 switch (drv_data
->ssp_type
) {
224 case QUARK_X1000_SSP
:
225 mask
= QUARK_X1000_SSSR_TFL_MASK
;
228 mask
= CE4100_SSSR_TFL_MASK
;
231 mask
= SSSR_TFL_MASK
;
235 return (pxa2xx_spi_read(drv_data
, SSSR
) & mask
) == mask
;
238 static void pxa2xx_spi_clear_rx_thre(const struct driver_data
*drv_data
,
243 switch (drv_data
->ssp_type
) {
244 case QUARK_X1000_SSP
:
245 mask
= QUARK_X1000_SSCR1_RFT
;
248 mask
= CE4100_SSCR1_RFT
;
257 static void pxa2xx_spi_set_rx_thre(const struct driver_data
*drv_data
,
258 u32
*sccr1_reg
, u32 threshold
)
260 switch (drv_data
->ssp_type
) {
261 case QUARK_X1000_SSP
:
262 *sccr1_reg
|= QUARK_X1000_SSCR1_RxTresh(threshold
);
265 *sccr1_reg
|= CE4100_SSCR1_RxTresh(threshold
);
268 *sccr1_reg
|= SSCR1_RxTresh(threshold
);
273 static u32
pxa2xx_configure_sscr0(const struct driver_data
*drv_data
,
274 u32 clk_div
, u8 bits
)
276 switch (drv_data
->ssp_type
) {
277 case QUARK_X1000_SSP
:
279 | QUARK_X1000_SSCR0_Motorola
280 | QUARK_X1000_SSCR0_DataSize(bits
> 32 ? 8 : bits
)
285 | SSCR0_DataSize(bits
> 16 ? bits
- 16 : bits
)
287 | (bits
> 16 ? SSCR0_EDSS
: 0);
292 * Read and write LPSS SSP private registers. Caller must first check that
293 * is_lpss_ssp() returns true before these can be called.
295 static u32
__lpss_ssp_read_priv(struct driver_data
*drv_data
, unsigned offset
)
297 WARN_ON(!drv_data
->lpss_base
);
298 return readl(drv_data
->lpss_base
+ offset
);
301 static void __lpss_ssp_write_priv(struct driver_data
*drv_data
,
302 unsigned offset
, u32 value
)
304 WARN_ON(!drv_data
->lpss_base
);
305 writel(value
, drv_data
->lpss_base
+ offset
);
309 * lpss_ssp_setup - perform LPSS SSP specific setup
310 * @drv_data: pointer to the driver private data
312 * Perform LPSS SSP specific setup. This function must be called first if
313 * one is going to use LPSS SSP private registers.
315 static void lpss_ssp_setup(struct driver_data
*drv_data
)
317 const struct lpss_config
*config
;
320 config
= lpss_get_config(drv_data
);
321 drv_data
->lpss_base
= drv_data
->ioaddr
+ config
->offset
;
323 /* Enable software chip select control */
324 value
= __lpss_ssp_read_priv(drv_data
, config
->reg_cs_ctrl
);
325 value
&= ~(LPSS_CS_CONTROL_SW_MODE
| LPSS_CS_CONTROL_CS_HIGH
);
326 value
|= LPSS_CS_CONTROL_SW_MODE
| LPSS_CS_CONTROL_CS_HIGH
;
327 __lpss_ssp_write_priv(drv_data
, config
->reg_cs_ctrl
, value
);
329 /* Enable multiblock DMA transfers */
330 if (drv_data
->controller_info
->enable_dma
) {
331 __lpss_ssp_write_priv(drv_data
, config
->reg_ssp
, 1);
333 if (config
->reg_general
>= 0) {
334 value
= __lpss_ssp_read_priv(drv_data
,
335 config
->reg_general
);
336 value
|= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE
;
337 __lpss_ssp_write_priv(drv_data
,
338 config
->reg_general
, value
);
343 static void lpss_ssp_select_cs(struct spi_device
*spi
,
344 const struct lpss_config
*config
)
346 struct driver_data
*drv_data
=
347 spi_controller_get_devdata(spi
->controller
);
350 if (!config
->cs_sel_mask
)
353 value
= __lpss_ssp_read_priv(drv_data
, config
->reg_cs_ctrl
);
355 cs
= spi
->chip_select
;
356 cs
<<= config
->cs_sel_shift
;
357 if (cs
!= (value
& config
->cs_sel_mask
)) {
359 * When switching another chip select output active the
360 * output must be selected first and wait 2 ssp_clk cycles
361 * before changing state to active. Otherwise a short
362 * glitch will occur on the previous chip select since
363 * output select is latched but state control is not.
365 value
&= ~config
->cs_sel_mask
;
367 __lpss_ssp_write_priv(drv_data
,
368 config
->reg_cs_ctrl
, value
);
370 (drv_data
->controller
->max_speed_hz
/ 2));
374 static void lpss_ssp_cs_control(struct spi_device
*spi
, bool enable
)
376 struct driver_data
*drv_data
=
377 spi_controller_get_devdata(spi
->controller
);
378 const struct lpss_config
*config
;
381 config
= lpss_get_config(drv_data
);
384 lpss_ssp_select_cs(spi
, config
);
386 value
= __lpss_ssp_read_priv(drv_data
, config
->reg_cs_ctrl
);
388 value
&= ~LPSS_CS_CONTROL_CS_HIGH
;
390 value
|= LPSS_CS_CONTROL_CS_HIGH
;
391 __lpss_ssp_write_priv(drv_data
, config
->reg_cs_ctrl
, value
);
392 if (config
->cs_clk_stays_gated
) {
396 * Changing CS alone when dynamic clock gating is on won't
397 * actually flip CS at that time. This ruins SPI transfers
398 * that specify delays, or have no data. Toggle the clock mode
399 * to force on briefly to poke the CS pin to move.
401 clkgate
= __lpss_ssp_read_priv(drv_data
, LPSS_PRIV_CLOCK_GATE
);
402 value
= (clkgate
& ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK
) |
403 LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON
;
405 __lpss_ssp_write_priv(drv_data
, LPSS_PRIV_CLOCK_GATE
, value
);
406 __lpss_ssp_write_priv(drv_data
, LPSS_PRIV_CLOCK_GATE
, clkgate
);
410 static void cs_assert(struct spi_device
*spi
)
412 struct chip_data
*chip
= spi_get_ctldata(spi
);
413 struct driver_data
*drv_data
=
414 spi_controller_get_devdata(spi
->controller
);
416 if (drv_data
->ssp_type
== CE4100_SSP
) {
417 pxa2xx_spi_write(drv_data
, SSSR
, chip
->frm
);
421 if (chip
->cs_control
) {
422 chip
->cs_control(PXA2XX_CS_ASSERT
);
426 if (chip
->gpiod_cs
) {
427 gpiod_set_value(chip
->gpiod_cs
, chip
->gpio_cs_inverted
);
431 if (is_lpss_ssp(drv_data
))
432 lpss_ssp_cs_control(spi
, true);
435 static void cs_deassert(struct spi_device
*spi
)
437 struct chip_data
*chip
= spi_get_ctldata(spi
);
438 struct driver_data
*drv_data
=
439 spi_controller_get_devdata(spi
->controller
);
440 unsigned long timeout
;
442 if (drv_data
->ssp_type
== CE4100_SSP
)
445 /* Wait until SSP becomes idle before deasserting the CS */
446 timeout
= jiffies
+ msecs_to_jiffies(10);
447 while (pxa2xx_spi_read(drv_data
, SSSR
) & SSSR_BSY
&&
448 !time_after(jiffies
, timeout
))
451 if (chip
->cs_control
) {
452 chip
->cs_control(PXA2XX_CS_DEASSERT
);
456 if (chip
->gpiod_cs
) {
457 gpiod_set_value(chip
->gpiod_cs
, !chip
->gpio_cs_inverted
);
461 if (is_lpss_ssp(drv_data
))
462 lpss_ssp_cs_control(spi
, false);
465 static void pxa2xx_spi_set_cs(struct spi_device
*spi
, bool level
)
473 int pxa2xx_spi_flush(struct driver_data
*drv_data
)
475 unsigned long limit
= loops_per_jiffy
<< 1;
478 while (pxa2xx_spi_read(drv_data
, SSSR
) & SSSR_RNE
)
479 pxa2xx_spi_read(drv_data
, SSDR
);
480 } while ((pxa2xx_spi_read(drv_data
, SSSR
) & SSSR_BSY
) && --limit
);
481 write_SSSR_CS(drv_data
, SSSR_ROR
);
486 static int null_writer(struct driver_data
*drv_data
)
488 u8 n_bytes
= drv_data
->n_bytes
;
490 if (pxa2xx_spi_txfifo_full(drv_data
)
491 || (drv_data
->tx
== drv_data
->tx_end
))
494 pxa2xx_spi_write(drv_data
, SSDR
, 0);
495 drv_data
->tx
+= n_bytes
;
500 static int null_reader(struct driver_data
*drv_data
)
502 u8 n_bytes
= drv_data
->n_bytes
;
504 while ((pxa2xx_spi_read(drv_data
, SSSR
) & SSSR_RNE
)
505 && (drv_data
->rx
< drv_data
->rx_end
)) {
506 pxa2xx_spi_read(drv_data
, SSDR
);
507 drv_data
->rx
+= n_bytes
;
510 return drv_data
->rx
== drv_data
->rx_end
;
513 static int u8_writer(struct driver_data
*drv_data
)
515 if (pxa2xx_spi_txfifo_full(drv_data
)
516 || (drv_data
->tx
== drv_data
->tx_end
))
519 pxa2xx_spi_write(drv_data
, SSDR
, *(u8
*)(drv_data
->tx
));
525 static int u8_reader(struct driver_data
*drv_data
)
527 while ((pxa2xx_spi_read(drv_data
, SSSR
) & SSSR_RNE
)
528 && (drv_data
->rx
< drv_data
->rx_end
)) {
529 *(u8
*)(drv_data
->rx
) = pxa2xx_spi_read(drv_data
, SSDR
);
533 return drv_data
->rx
== drv_data
->rx_end
;
536 static int u16_writer(struct driver_data
*drv_data
)
538 if (pxa2xx_spi_txfifo_full(drv_data
)
539 || (drv_data
->tx
== drv_data
->tx_end
))
542 pxa2xx_spi_write(drv_data
, SSDR
, *(u16
*)(drv_data
->tx
));
548 static int u16_reader(struct driver_data
*drv_data
)
550 while ((pxa2xx_spi_read(drv_data
, SSSR
) & SSSR_RNE
)
551 && (drv_data
->rx
< drv_data
->rx_end
)) {
552 *(u16
*)(drv_data
->rx
) = pxa2xx_spi_read(drv_data
, SSDR
);
556 return drv_data
->rx
== drv_data
->rx_end
;
559 static int u32_writer(struct driver_data
*drv_data
)
561 if (pxa2xx_spi_txfifo_full(drv_data
)
562 || (drv_data
->tx
== drv_data
->tx_end
))
565 pxa2xx_spi_write(drv_data
, SSDR
, *(u32
*)(drv_data
->tx
));
571 static int u32_reader(struct driver_data
*drv_data
)
573 while ((pxa2xx_spi_read(drv_data
, SSSR
) & SSSR_RNE
)
574 && (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 struct chip_data
*chip
=
585 spi_get_ctldata(drv_data
->controller
->cur_msg
->spi
);
588 sccr1_reg
= pxa2xx_spi_read(drv_data
, SSCR1
) & ~drv_data
->int_cr1
;
589 switch (drv_data
->ssp_type
) {
590 case QUARK_X1000_SSP
:
591 sccr1_reg
&= ~QUARK_X1000_SSCR1_RFT
;
594 sccr1_reg
&= ~CE4100_SSCR1_RFT
;
597 sccr1_reg
&= ~SSCR1_RFT
;
600 sccr1_reg
|= chip
->threshold
;
601 pxa2xx_spi_write(drv_data
, SSCR1
, sccr1_reg
);
604 static void int_error_stop(struct driver_data
*drv_data
, const char* msg
)
606 /* Stop and reset SSP */
607 write_SSSR_CS(drv_data
, drv_data
->clear_sr
);
608 reset_sccr1(drv_data
);
609 if (!pxa25x_ssp_comp(drv_data
))
610 pxa2xx_spi_write(drv_data
, SSTO
, 0);
611 pxa2xx_spi_flush(drv_data
);
612 pxa2xx_spi_write(drv_data
, SSCR0
,
613 pxa2xx_spi_read(drv_data
, SSCR0
) & ~SSCR0_SSE
);
615 dev_err(&drv_data
->pdev
->dev
, "%s\n", msg
);
617 drv_data
->controller
->cur_msg
->status
= -EIO
;
618 spi_finalize_current_transfer(drv_data
->controller
);
621 static void int_transfer_complete(struct driver_data
*drv_data
)
623 /* Clear and disable interrupts */
624 write_SSSR_CS(drv_data
, drv_data
->clear_sr
);
625 reset_sccr1(drv_data
);
626 if (!pxa25x_ssp_comp(drv_data
))
627 pxa2xx_spi_write(drv_data
, SSTO
, 0);
629 spi_finalize_current_transfer(drv_data
->controller
);
632 static irqreturn_t
interrupt_transfer(struct driver_data
*drv_data
)
634 u32 irq_mask
= (pxa2xx_spi_read(drv_data
, SSCR1
) & SSCR1_TIE
) ?
635 drv_data
->mask_sr
: drv_data
->mask_sr
& ~SSSR_TFS
;
637 u32 irq_status
= pxa2xx_spi_read(drv_data
, SSSR
) & irq_mask
;
639 if (irq_status
& SSSR_ROR
) {
640 int_error_stop(drv_data
, "interrupt_transfer: fifo overrun");
644 if (irq_status
& SSSR_TUR
) {
645 int_error_stop(drv_data
, "interrupt_transfer: fifo underrun");
649 if (irq_status
& SSSR_TINT
) {
650 pxa2xx_spi_write(drv_data
, SSSR
, SSSR_TINT
);
651 if (drv_data
->read(drv_data
)) {
652 int_transfer_complete(drv_data
);
657 /* Drain rx fifo, Fill tx fifo and prevent overruns */
659 if (drv_data
->read(drv_data
)) {
660 int_transfer_complete(drv_data
);
663 } while (drv_data
->write(drv_data
));
665 if (drv_data
->read(drv_data
)) {
666 int_transfer_complete(drv_data
);
670 if (drv_data
->tx
== drv_data
->tx_end
) {
674 sccr1_reg
= pxa2xx_spi_read(drv_data
, SSCR1
);
675 sccr1_reg
&= ~SSCR1_TIE
;
678 * PXA25x_SSP has no timeout, set up rx threshould for the
679 * remaining RX bytes.
681 if (pxa25x_ssp_comp(drv_data
)) {
684 pxa2xx_spi_clear_rx_thre(drv_data
, &sccr1_reg
);
686 bytes_left
= drv_data
->rx_end
- drv_data
->rx
;
687 switch (drv_data
->n_bytes
) {
696 rx_thre
= pxa2xx_spi_get_rx_default_thre(drv_data
);
697 if (rx_thre
> bytes_left
)
698 rx_thre
= bytes_left
;
700 pxa2xx_spi_set_rx_thre(drv_data
, &sccr1_reg
, rx_thre
);
702 pxa2xx_spi_write(drv_data
, SSCR1
, sccr1_reg
);
705 /* We did something */
709 static void handle_bad_msg(struct driver_data
*drv_data
)
711 pxa2xx_spi_write(drv_data
, SSCR0
,
712 pxa2xx_spi_read(drv_data
, SSCR0
) & ~SSCR0_SSE
);
713 pxa2xx_spi_write(drv_data
, SSCR1
,
714 pxa2xx_spi_read(drv_data
, SSCR1
) & ~drv_data
->int_cr1
);
715 if (!pxa25x_ssp_comp(drv_data
))
716 pxa2xx_spi_write(drv_data
, SSTO
, 0);
717 write_SSSR_CS(drv_data
, drv_data
->clear_sr
);
719 dev_err(&drv_data
->pdev
->dev
,
720 "bad message state in interrupt handler\n");
723 static irqreturn_t
ssp_int(int irq
, void *dev_id
)
725 struct driver_data
*drv_data
= dev_id
;
727 u32 mask
= drv_data
->mask_sr
;
731 * The IRQ might be shared with other peripherals so we must first
732 * check that are we RPM suspended or not. If we are we assume that
733 * the IRQ was not for us (we shouldn't be RPM suspended when the
734 * interrupt is enabled).
736 if (pm_runtime_suspended(&drv_data
->pdev
->dev
))
740 * If the device is not yet in RPM suspended state and we get an
741 * interrupt that is meant for another device, check if status bits
742 * are all set to one. That means that the device is already
745 status
= pxa2xx_spi_read(drv_data
, SSSR
);
749 sccr1_reg
= pxa2xx_spi_read(drv_data
, SSCR1
);
751 /* Ignore possible writes if we don't need to write */
752 if (!(sccr1_reg
& SSCR1_TIE
))
755 /* Ignore RX timeout interrupt if it is disabled */
756 if (!(sccr1_reg
& SSCR1_TINTE
))
759 if (!(status
& mask
))
762 pxa2xx_spi_write(drv_data
, SSCR1
, sccr1_reg
& ~drv_data
->int_cr1
);
763 pxa2xx_spi_write(drv_data
, SSCR1
, sccr1_reg
);
765 if (!drv_data
->controller
->cur_msg
) {
766 handle_bad_msg(drv_data
);
771 return drv_data
->transfer_handler(drv_data
);
775 * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
776 * input frequency by fractions of 2^24. It also has a divider by 5.
778 * There are formulas to get baud rate value for given input frequency and
779 * divider parameters, such as DDS_CLK_RATE and SCR:
783 * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
784 * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
786 * DDS_CLK_RATE either 2^n or 2^n / 5.
787 * SCR is in range 0 .. 255
789 * Divisor = 5^i * 2^j * 2 * k
790 * i = [0, 1] i = 1 iff j = 0 or j > 3
791 * j = [0, 23] j = 0 iff i = 1
793 * Special case: j = 0, i = 1: Divisor = 2 / 5
795 * Accordingly to the specification the recommended values for DDS_CLK_RATE
797 * Case 1: 2^n, n = [0, 23]
798 * Case 2: 2^24 * 2 / 5 (0x666666)
799 * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
801 * In all cases the lowest possible value is better.
803 * The function calculates parameters for all cases and chooses the one closest
804 * to the asked baud rate.
806 static unsigned int quark_x1000_get_clk_div(int rate
, u32
*dds
)
808 unsigned long xtal
= 200000000;
809 unsigned long fref
= xtal
/ 2; /* mandatory division by 2,
812 unsigned long fref1
= fref
/ 2; /* case 1 */
813 unsigned long fref2
= fref
* 2 / 5; /* case 2 */
815 unsigned long q
, q1
, q2
;
821 /* Set initial value for DDS_CLK_RATE */
822 mul
= (1 << 24) >> 1;
824 /* Calculate initial quot */
825 q1
= DIV_ROUND_UP(fref1
, rate
);
827 /* Scale q1 if it's too big */
829 /* Scale q1 to range [1, 512] */
830 scale
= fls_long(q1
- 1);
836 /* Round the result if we have a remainder */
840 /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
845 /* Get the remainder */
846 r1
= abs(fref1
/ (1 << (24 - fls_long(mul
))) / q1
- rate
);
850 q2
= DIV_ROUND_UP(fref2
, rate
);
851 r2
= abs(fref2
/ q2
- rate
);
854 * Choose the best between two: less remainder we have the better. We
855 * can't go case 2 if q2 is greater than 256 since SCR register can
856 * hold only values 0 .. 255.
858 if (r2
>= r1
|| q2
> 256) {
859 /* case 1 is better */
863 /* case 2 is better */
866 mul
= (1 << 24) * 2 / 5;
869 /* Check case 3 only if the divisor is big enough */
870 if (fref
/ rate
>= 80) {
874 /* Calculate initial quot */
875 q1
= DIV_ROUND_UP(fref
, rate
);
878 /* Get the remainder */
879 fssp
= (u64
)fref
* m
;
880 do_div(fssp
, 1 << 24);
881 r1
= abs(fssp
- rate
);
883 /* Choose this one if it suits better */
885 /* case 3 is better */
895 static unsigned int ssp_get_clk_div(struct driver_data
*drv_data
, int rate
)
897 unsigned long ssp_clk
= drv_data
->controller
->max_speed_hz
;
898 const struct ssp_device
*ssp
= drv_data
->ssp
;
900 rate
= min_t(int, ssp_clk
, rate
);
903 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
904 * that the SSP transmission rate can be greater than the device rate
906 if (ssp
->type
== PXA25x_SSP
|| ssp
->type
== CE4100_SSP
)
907 return (DIV_ROUND_UP(ssp_clk
, 2 * rate
) - 1) & 0xff;
909 return (DIV_ROUND_UP(ssp_clk
, rate
) - 1) & 0xfff;
912 static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data
*drv_data
,
915 struct chip_data
*chip
=
916 spi_get_ctldata(drv_data
->controller
->cur_msg
->spi
);
917 unsigned int clk_div
;
919 switch (drv_data
->ssp_type
) {
920 case QUARK_X1000_SSP
:
921 clk_div
= quark_x1000_get_clk_div(rate
, &chip
->dds_rate
);
924 clk_div
= ssp_get_clk_div(drv_data
, rate
);
930 static bool pxa2xx_spi_can_dma(struct spi_controller
*controller
,
931 struct spi_device
*spi
,
932 struct spi_transfer
*xfer
)
934 struct chip_data
*chip
= spi_get_ctldata(spi
);
936 return chip
->enable_dma
&&
937 xfer
->len
<= MAX_DMA_LEN
&&
938 xfer
->len
>= chip
->dma_burst_size
;
941 static int pxa2xx_spi_transfer_one(struct spi_controller
*controller
,
942 struct spi_device
*spi
,
943 struct spi_transfer
*transfer
)
945 struct driver_data
*drv_data
= spi_controller_get_devdata(controller
);
946 struct spi_message
*message
= controller
->cur_msg
;
947 struct chip_data
*chip
= spi_get_ctldata(spi
);
948 u32 dma_thresh
= chip
->dma_threshold
;
949 u32 dma_burst
= chip
->dma_burst_size
;
950 u32 change_mask
= pxa2xx_spi_get_ssrc1_change_mask(drv_data
);
959 /* Check if we can DMA this transfer */
960 if (transfer
->len
> MAX_DMA_LEN
&& chip
->enable_dma
) {
962 /* reject already-mapped transfers; PIO won't always work */
963 if (message
->is_dma_mapped
964 || transfer
->rx_dma
|| transfer
->tx_dma
) {
966 "Mapped transfer length of %u is greater than %d\n",
967 transfer
->len
, MAX_DMA_LEN
);
971 /* warn ... we force this to PIO mode */
972 dev_warn_ratelimited(&spi
->dev
,
973 "DMA disabled for transfer length %ld greater than %d\n",
974 (long)transfer
->len
, MAX_DMA_LEN
);
977 /* Setup the transfer state based on the type of transfer */
978 if (pxa2xx_spi_flush(drv_data
) == 0) {
979 dev_err(&spi
->dev
, "Flush failed\n");
982 drv_data
->n_bytes
= chip
->n_bytes
;
983 drv_data
->tx
= (void *)transfer
->tx_buf
;
984 drv_data
->tx_end
= drv_data
->tx
+ transfer
->len
;
985 drv_data
->rx
= transfer
->rx_buf
;
986 drv_data
->rx_end
= drv_data
->rx
+ transfer
->len
;
987 drv_data
->write
= drv_data
->tx
? chip
->write
: null_writer
;
988 drv_data
->read
= drv_data
->rx
? chip
->read
: null_reader
;
990 /* Change speed and bit per word on a per transfer */
991 bits
= transfer
->bits_per_word
;
992 speed
= transfer
->speed_hz
;
994 clk_div
= pxa2xx_ssp_get_clk_div(drv_data
, speed
);
997 drv_data
->n_bytes
= 1;
998 drv_data
->read
= drv_data
->read
!= null_reader
?
999 u8_reader
: null_reader
;
1000 drv_data
->write
= drv_data
->write
!= null_writer
?
1001 u8_writer
: null_writer
;
1002 } else if (bits
<= 16) {
1003 drv_data
->n_bytes
= 2;
1004 drv_data
->read
= drv_data
->read
!= null_reader
?
1005 u16_reader
: null_reader
;
1006 drv_data
->write
= drv_data
->write
!= null_writer
?
1007 u16_writer
: null_writer
;
1008 } else if (bits
<= 32) {
1009 drv_data
->n_bytes
= 4;
1010 drv_data
->read
= drv_data
->read
!= null_reader
?
1011 u32_reader
: null_reader
;
1012 drv_data
->write
= drv_data
->write
!= null_writer
?
1013 u32_writer
: null_writer
;
1016 * if bits/word is changed in dma mode, then must check the
1017 * thresholds and burst also
1019 if (chip
->enable_dma
) {
1020 if (pxa2xx_spi_set_dma_burst_and_threshold(chip
,
1024 dev_warn_ratelimited(&spi
->dev
,
1025 "DMA burst size reduced to match bits_per_word\n");
1028 dma_mapped
= controller
->can_dma
&&
1029 controller
->can_dma(controller
, spi
, transfer
) &&
1030 controller
->cur_msg_mapped
;
1033 /* Ensure we have the correct interrupt handler */
1034 drv_data
->transfer_handler
= pxa2xx_spi_dma_transfer
;
1036 err
= pxa2xx_spi_dma_prepare(drv_data
, transfer
);
1040 /* Clear status and start DMA engine */
1041 cr1
= chip
->cr1
| dma_thresh
| drv_data
->dma_cr1
;
1042 pxa2xx_spi_write(drv_data
, SSSR
, drv_data
->clear_sr
);
1044 pxa2xx_spi_dma_start(drv_data
);
1046 /* Ensure we have the correct interrupt handler */
1047 drv_data
->transfer_handler
= interrupt_transfer
;
1050 cr1
= chip
->cr1
| chip
->threshold
| drv_data
->int_cr1
;
1051 write_SSSR_CS(drv_data
, drv_data
->clear_sr
);
1054 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1055 cr0
= pxa2xx_configure_sscr0(drv_data
, clk_div
, bits
);
1056 if (!pxa25x_ssp_comp(drv_data
))
1057 dev_dbg(&spi
->dev
, "%u Hz actual, %s\n",
1058 controller
->max_speed_hz
1059 / (1 + ((cr0
& SSCR0_SCR(0xfff)) >> 8)),
1060 dma_mapped
? "DMA" : "PIO");
1062 dev_dbg(&spi
->dev
, "%u Hz actual, %s\n",
1063 controller
->max_speed_hz
/ 2
1064 / (1 + ((cr0
& SSCR0_SCR(0x0ff)) >> 8)),
1065 dma_mapped
? "DMA" : "PIO");
1067 if (is_lpss_ssp(drv_data
)) {
1068 if ((pxa2xx_spi_read(drv_data
, SSIRF
) & 0xff)
1069 != chip
->lpss_rx_threshold
)
1070 pxa2xx_spi_write(drv_data
, SSIRF
,
1071 chip
->lpss_rx_threshold
);
1072 if ((pxa2xx_spi_read(drv_data
, SSITF
) & 0xffff)
1073 != chip
->lpss_tx_threshold
)
1074 pxa2xx_spi_write(drv_data
, SSITF
,
1075 chip
->lpss_tx_threshold
);
1078 if (is_quark_x1000_ssp(drv_data
) &&
1079 (pxa2xx_spi_read(drv_data
, DDS_RATE
) != chip
->dds_rate
))
1080 pxa2xx_spi_write(drv_data
, DDS_RATE
, chip
->dds_rate
);
1082 /* see if we need to reload the config registers */
1083 if ((pxa2xx_spi_read(drv_data
, SSCR0
) != cr0
)
1084 || (pxa2xx_spi_read(drv_data
, SSCR1
) & change_mask
)
1085 != (cr1
& change_mask
)) {
1086 /* stop the SSP, and update the other bits */
1087 pxa2xx_spi_write(drv_data
, SSCR0
, cr0
& ~SSCR0_SSE
);
1088 if (!pxa25x_ssp_comp(drv_data
))
1089 pxa2xx_spi_write(drv_data
, SSTO
, chip
->timeout
);
1090 /* first set CR1 without interrupt and service enables */
1091 pxa2xx_spi_write(drv_data
, SSCR1
, cr1
& change_mask
);
1092 /* restart the SSP */
1093 pxa2xx_spi_write(drv_data
, SSCR0
, cr0
);
1096 if (!pxa25x_ssp_comp(drv_data
))
1097 pxa2xx_spi_write(drv_data
, SSTO
, chip
->timeout
);
1100 if (drv_data
->ssp_type
== MMP2_SSP
) {
1101 u8 tx_level
= (pxa2xx_spi_read(drv_data
, SSSR
)
1102 & SSSR_TFL_MASK
) >> 8;
1105 /* On MMP2, flipping SSE doesn't to empty TXFIFO. */
1106 dev_warn(&spi
->dev
, "%d bytes of garbage in TXFIFO!\n",
1108 if (tx_level
> transfer
->len
)
1109 tx_level
= transfer
->len
;
1110 drv_data
->tx
+= tx_level
;
1114 if (spi_controller_is_slave(controller
)) {
1115 while (drv_data
->write(drv_data
))
1117 if (drv_data
->gpiod_ready
) {
1118 gpiod_set_value(drv_data
->gpiod_ready
, 1);
1120 gpiod_set_value(drv_data
->gpiod_ready
, 0);
1125 * Release the data by enabling service requests and interrupts,
1126 * without changing any mode bits
1128 pxa2xx_spi_write(drv_data
, SSCR1
, cr1
);
1133 static int pxa2xx_spi_slave_abort(struct spi_controller
*controller
)
1135 struct driver_data
*drv_data
= spi_controller_get_devdata(controller
);
1137 /* Stop and reset SSP */
1138 write_SSSR_CS(drv_data
, drv_data
->clear_sr
);
1139 reset_sccr1(drv_data
);
1140 if (!pxa25x_ssp_comp(drv_data
))
1141 pxa2xx_spi_write(drv_data
, SSTO
, 0);
1142 pxa2xx_spi_flush(drv_data
);
1143 pxa2xx_spi_write(drv_data
, SSCR0
,
1144 pxa2xx_spi_read(drv_data
, SSCR0
) & ~SSCR0_SSE
);
1146 dev_dbg(&drv_data
->pdev
->dev
, "transfer aborted\n");
1148 drv_data
->controller
->cur_msg
->status
= -EINTR
;
1149 spi_finalize_current_transfer(drv_data
->controller
);
1154 static void pxa2xx_spi_handle_err(struct spi_controller
*controller
,
1155 struct spi_message
*msg
)
1157 struct driver_data
*drv_data
= spi_controller_get_devdata(controller
);
1159 /* Disable the SSP */
1160 pxa2xx_spi_write(drv_data
, SSCR0
,
1161 pxa2xx_spi_read(drv_data
, SSCR0
) & ~SSCR0_SSE
);
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);
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
1177 if (atomic_read(&drv_data
->dma_running
))
1178 pxa2xx_spi_dma_stop(drv_data
);
1181 static int pxa2xx_spi_unprepare_transfer(struct spi_controller
*controller
)
1183 struct driver_data
*drv_data
= spi_controller_get_devdata(controller
);
1185 /* Disable the SSP now */
1186 pxa2xx_spi_write(drv_data
, SSCR0
,
1187 pxa2xx_spi_read(drv_data
, SSCR0
) & ~SSCR0_SSE
);
1192 static int setup_cs(struct spi_device
*spi
, struct chip_data
*chip
,
1193 struct pxa2xx_spi_chip
*chip_info
)
1195 struct driver_data
*drv_data
=
1196 spi_controller_get_devdata(spi
->controller
);
1197 struct gpio_desc
*gpiod
;
1203 if (drv_data
->cs_gpiods
) {
1204 gpiod
= drv_data
->cs_gpiods
[spi
->chip_select
];
1206 chip
->gpiod_cs
= gpiod
;
1207 chip
->gpio_cs_inverted
= spi
->mode
& SPI_CS_HIGH
;
1208 gpiod_set_value(gpiod
, chip
->gpio_cs_inverted
);
1214 if (chip_info
== NULL
)
1217 /* NOTE: setup() can be called multiple times, possibly with
1218 * different chip_info, release previously requested GPIO
1220 if (chip
->gpiod_cs
) {
1221 gpiod_put(chip
->gpiod_cs
);
1222 chip
->gpiod_cs
= NULL
;
1225 /* If (*cs_control) is provided, ignore GPIO chip select */
1226 if (chip_info
->cs_control
) {
1227 chip
->cs_control
= chip_info
->cs_control
;
1231 if (gpio_is_valid(chip_info
->gpio_cs
)) {
1232 err
= gpio_request(chip_info
->gpio_cs
, "SPI_CS");
1234 dev_err(&spi
->dev
, "failed to request chip select GPIO%d\n",
1235 chip_info
->gpio_cs
);
1239 gpiod
= gpio_to_desc(chip_info
->gpio_cs
);
1240 chip
->gpiod_cs
= gpiod
;
1241 chip
->gpio_cs_inverted
= spi
->mode
& SPI_CS_HIGH
;
1243 err
= gpiod_direction_output(gpiod
, !chip
->gpio_cs_inverted
);
1249 static int setup(struct spi_device
*spi
)
1251 struct pxa2xx_spi_chip
*chip_info
;
1252 struct chip_data
*chip
;
1253 const struct lpss_config
*config
;
1254 struct driver_data
*drv_data
=
1255 spi_controller_get_devdata(spi
->controller
);
1256 uint tx_thres
, tx_hi_thres
, rx_thres
;
1258 switch (drv_data
->ssp_type
) {
1259 case QUARK_X1000_SSP
:
1260 tx_thres
= TX_THRESH_QUARK_X1000_DFLT
;
1262 rx_thres
= RX_THRESH_QUARK_X1000_DFLT
;
1265 tx_thres
= TX_THRESH_CE4100_DFLT
;
1267 rx_thres
= RX_THRESH_CE4100_DFLT
;
1275 config
= lpss_get_config(drv_data
);
1276 tx_thres
= config
->tx_threshold_lo
;
1277 tx_hi_thres
= config
->tx_threshold_hi
;
1278 rx_thres
= config
->rx_threshold
;
1282 if (spi_controller_is_slave(drv_data
->controller
)) {
1286 tx_thres
= TX_THRESH_DFLT
;
1287 rx_thres
= RX_THRESH_DFLT
;
1292 /* Only alloc on first setup */
1293 chip
= spi_get_ctldata(spi
);
1295 chip
= kzalloc(sizeof(struct chip_data
), GFP_KERNEL
);
1299 if (drv_data
->ssp_type
== CE4100_SSP
) {
1300 if (spi
->chip_select
> 4) {
1302 "failed setup: cs number must not be > 4.\n");
1307 chip
->frm
= spi
->chip_select
;
1309 chip
->enable_dma
= drv_data
->controller_info
->enable_dma
;
1310 chip
->timeout
= TIMOUT_DFLT
;
1313 /* protocol drivers may change the chip settings, so...
1314 * if chip_info exists, use it */
1315 chip_info
= spi
->controller_data
;
1317 /* chip_info isn't always needed */
1320 if (chip_info
->timeout
)
1321 chip
->timeout
= chip_info
->timeout
;
1322 if (chip_info
->tx_threshold
)
1323 tx_thres
= chip_info
->tx_threshold
;
1324 if (chip_info
->tx_hi_threshold
)
1325 tx_hi_thres
= chip_info
->tx_hi_threshold
;
1326 if (chip_info
->rx_threshold
)
1327 rx_thres
= chip_info
->rx_threshold
;
1328 chip
->dma_threshold
= 0;
1329 if (chip_info
->enable_loopback
)
1330 chip
->cr1
= SSCR1_LBM
;
1332 if (spi_controller_is_slave(drv_data
->controller
)) {
1333 chip
->cr1
|= SSCR1_SCFR
;
1334 chip
->cr1
|= SSCR1_SCLKDIR
;
1335 chip
->cr1
|= SSCR1_SFRMDIR
;
1336 chip
->cr1
|= SSCR1_SPH
;
1339 chip
->lpss_rx_threshold
= SSIRF_RxThresh(rx_thres
);
1340 chip
->lpss_tx_threshold
= SSITF_TxLoThresh(tx_thres
)
1341 | SSITF_TxHiThresh(tx_hi_thres
);
1343 /* set dma burst and threshold outside of chip_info path so that if
1344 * chip_info goes away after setting chip->enable_dma, the
1345 * burst and threshold can still respond to changes in bits_per_word */
1346 if (chip
->enable_dma
) {
1347 /* set up legal burst and threshold for dma */
1348 if (pxa2xx_spi_set_dma_burst_and_threshold(chip
, spi
,
1350 &chip
->dma_burst_size
,
1351 &chip
->dma_threshold
)) {
1353 "in setup: DMA burst size reduced to match bits_per_word\n");
1356 "in setup: DMA burst size set to %u\n",
1357 chip
->dma_burst_size
);
1360 switch (drv_data
->ssp_type
) {
1361 case QUARK_X1000_SSP
:
1362 chip
->threshold
= (QUARK_X1000_SSCR1_RxTresh(rx_thres
)
1363 & QUARK_X1000_SSCR1_RFT
)
1364 | (QUARK_X1000_SSCR1_TxTresh(tx_thres
)
1365 & QUARK_X1000_SSCR1_TFT
);
1368 chip
->threshold
= (CE4100_SSCR1_RxTresh(rx_thres
) & CE4100_SSCR1_RFT
) |
1369 (CE4100_SSCR1_TxTresh(tx_thres
) & CE4100_SSCR1_TFT
);
1372 chip
->threshold
= (SSCR1_RxTresh(rx_thres
) & SSCR1_RFT
) |
1373 (SSCR1_TxTresh(tx_thres
) & SSCR1_TFT
);
1377 chip
->cr1
&= ~(SSCR1_SPO
| SSCR1_SPH
);
1378 chip
->cr1
|= (((spi
->mode
& SPI_CPHA
) != 0) ? SSCR1_SPH
: 0)
1379 | (((spi
->mode
& SPI_CPOL
) != 0) ? SSCR1_SPO
: 0);
1381 if (spi
->mode
& SPI_LOOP
)
1382 chip
->cr1
|= SSCR1_LBM
;
1384 if (spi
->bits_per_word
<= 8) {
1386 chip
->read
= u8_reader
;
1387 chip
->write
= u8_writer
;
1388 } else if (spi
->bits_per_word
<= 16) {
1390 chip
->read
= u16_reader
;
1391 chip
->write
= u16_writer
;
1392 } else if (spi
->bits_per_word
<= 32) {
1394 chip
->read
= u32_reader
;
1395 chip
->write
= u32_writer
;
1398 spi_set_ctldata(spi
, chip
);
1400 if (drv_data
->ssp_type
== CE4100_SSP
)
1403 return setup_cs(spi
, chip
, chip_info
);
1406 static void cleanup(struct spi_device
*spi
)
1408 struct chip_data
*chip
= spi_get_ctldata(spi
);
1409 struct driver_data
*drv_data
=
1410 spi_controller_get_devdata(spi
->controller
);
1415 if (drv_data
->ssp_type
!= CE4100_SSP
&& !drv_data
->cs_gpiods
&&
1417 gpiod_put(chip
->gpiod_cs
);
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
},
1431 MODULE_DEVICE_TABLE(acpi
, pxa2xx_spi_acpi_match
);
1434 * PCI IDs of compound devices that integrate both host controller and private
1435 * integrated DMA engine. Please note these are not used in module
1436 * autoloading and probing in this module but matching the LPSS SSP type.
1438 static const struct pci_device_id pxa2xx_spi_pci_compound_match
[] = {
1440 { PCI_VDEVICE(INTEL
, 0x9d29), LPSS_SPT_SSP
},
1441 { PCI_VDEVICE(INTEL
, 0x9d2a), LPSS_SPT_SSP
},
1443 { PCI_VDEVICE(INTEL
, 0xa129), LPSS_SPT_SSP
},
1444 { PCI_VDEVICE(INTEL
, 0xa12a), LPSS_SPT_SSP
},
1446 { PCI_VDEVICE(INTEL
, 0xa2a9), LPSS_SPT_SSP
},
1447 { PCI_VDEVICE(INTEL
, 0xa2aa), LPSS_SPT_SSP
},
1449 { PCI_VDEVICE(INTEL
, 0x0ac2), LPSS_BXT_SSP
},
1450 { PCI_VDEVICE(INTEL
, 0x0ac4), LPSS_BXT_SSP
},
1451 { PCI_VDEVICE(INTEL
, 0x0ac6), LPSS_BXT_SSP
},
1453 { PCI_VDEVICE(INTEL
, 0x1ac2), LPSS_BXT_SSP
},
1454 { PCI_VDEVICE(INTEL
, 0x1ac4), LPSS_BXT_SSP
},
1455 { PCI_VDEVICE(INTEL
, 0x1ac6), LPSS_BXT_SSP
},
1457 { PCI_VDEVICE(INTEL
, 0x31c2), LPSS_BXT_SSP
},
1458 { PCI_VDEVICE(INTEL
, 0x31c4), LPSS_BXT_SSP
},
1459 { PCI_VDEVICE(INTEL
, 0x31c6), LPSS_BXT_SSP
},
1461 { PCI_VDEVICE(INTEL
, 0x34aa), LPSS_CNL_SSP
},
1462 { PCI_VDEVICE(INTEL
, 0x34ab), LPSS_CNL_SSP
},
1463 { PCI_VDEVICE(INTEL
, 0x34fb), LPSS_CNL_SSP
},
1465 { PCI_VDEVICE(INTEL
, 0x4b2a), LPSS_BXT_SSP
},
1466 { PCI_VDEVICE(INTEL
, 0x4b2b), LPSS_BXT_SSP
},
1467 { PCI_VDEVICE(INTEL
, 0x4b37), LPSS_BXT_SSP
},
1469 { PCI_VDEVICE(INTEL
, 0x4daa), LPSS_CNL_SSP
},
1470 { PCI_VDEVICE(INTEL
, 0x4dab), LPSS_CNL_SSP
},
1471 { PCI_VDEVICE(INTEL
, 0x4dfb), LPSS_CNL_SSP
},
1473 { PCI_VDEVICE(INTEL
, 0x5ac2), LPSS_BXT_SSP
},
1474 { PCI_VDEVICE(INTEL
, 0x5ac4), LPSS_BXT_SSP
},
1475 { PCI_VDEVICE(INTEL
, 0x5ac6), LPSS_BXT_SSP
},
1477 { PCI_VDEVICE(INTEL
, 0x9daa), LPSS_CNL_SSP
},
1478 { PCI_VDEVICE(INTEL
, 0x9dab), LPSS_CNL_SSP
},
1479 { PCI_VDEVICE(INTEL
, 0x9dfb), LPSS_CNL_SSP
},
1481 { PCI_VDEVICE(INTEL
, 0xa32a), LPSS_CNL_SSP
},
1482 { PCI_VDEVICE(INTEL
, 0xa32b), LPSS_CNL_SSP
},
1483 { PCI_VDEVICE(INTEL
, 0xa37b), LPSS_CNL_SSP
},
1485 { PCI_VDEVICE(INTEL
, 0x02aa), LPSS_CNL_SSP
},
1486 { PCI_VDEVICE(INTEL
, 0x02ab), LPSS_CNL_SSP
},
1487 { PCI_VDEVICE(INTEL
, 0x02fb), LPSS_CNL_SSP
},
1489 { PCI_VDEVICE(INTEL
, 0x06aa), LPSS_CNL_SSP
},
1490 { PCI_VDEVICE(INTEL
, 0x06ab), LPSS_CNL_SSP
},
1491 { PCI_VDEVICE(INTEL
, 0x06fb), LPSS_CNL_SSP
},
1493 { PCI_VDEVICE(INTEL
, 0xa0aa), LPSS_CNL_SSP
},
1494 { PCI_VDEVICE(INTEL
, 0xa0ab), LPSS_CNL_SSP
},
1495 { PCI_VDEVICE(INTEL
, 0xa0de), LPSS_CNL_SSP
},
1496 { PCI_VDEVICE(INTEL
, 0xa0df), LPSS_CNL_SSP
},
1497 { PCI_VDEVICE(INTEL
, 0xa0fb), LPSS_CNL_SSP
},
1498 { PCI_VDEVICE(INTEL
, 0xa0fd), LPSS_CNL_SSP
},
1499 { PCI_VDEVICE(INTEL
, 0xa0fe), LPSS_CNL_SSP
},
1503 static const struct of_device_id pxa2xx_spi_of_match
[] = {
1504 { .compatible
= "marvell,mmp2-ssp", .data
= (void *)MMP2_SSP
},
1507 MODULE_DEVICE_TABLE(of
, pxa2xx_spi_of_match
);
1511 static int pxa2xx_spi_get_port_id(struct acpi_device
*adev
)
1516 if (adev
&& adev
->pnp
.unique_id
&&
1517 !kstrtouint(adev
->pnp
.unique_id
, 0, &devid
))
1522 #else /* !CONFIG_ACPI */
1524 static int pxa2xx_spi_get_port_id(struct acpi_device
*adev
)
1529 #endif /* CONFIG_ACPI */
1534 static bool pxa2xx_spi_idma_filter(struct dma_chan
*chan
, void *param
)
1536 return param
== chan
->device
->dev
;
1539 #endif /* CONFIG_PCI */
1541 static struct pxa2xx_spi_controller
*
1542 pxa2xx_spi_init_pdata(struct platform_device
*pdev
)
1544 struct pxa2xx_spi_controller
*pdata
;
1545 struct acpi_device
*adev
;
1546 struct ssp_device
*ssp
;
1547 struct resource
*res
;
1548 const struct acpi_device_id
*adev_id
= NULL
;
1549 const struct pci_device_id
*pcidev_id
= NULL
;
1550 const struct of_device_id
*of_id
= NULL
;
1551 enum pxa_ssp_type type
;
1553 adev
= ACPI_COMPANION(&pdev
->dev
);
1555 if (pdev
->dev
.of_node
)
1556 of_id
= of_match_device(pdev
->dev
.driver
->of_match_table
,
1558 else if (dev_is_pci(pdev
->dev
.parent
))
1559 pcidev_id
= pci_match_id(pxa2xx_spi_pci_compound_match
,
1560 to_pci_dev(pdev
->dev
.parent
));
1562 adev_id
= acpi_match_device(pdev
->dev
.driver
->acpi_match_table
,
1568 type
= (enum pxa_ssp_type
)adev_id
->driver_data
;
1570 type
= (enum pxa_ssp_type
)pcidev_id
->driver_data
;
1572 type
= (enum pxa_ssp_type
)of_id
->data
;
1576 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
1580 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1586 ssp
->phys_base
= res
->start
;
1587 ssp
->mmio_base
= devm_ioremap_resource(&pdev
->dev
, res
);
1588 if (IS_ERR(ssp
->mmio_base
))
1593 pdata
->tx_param
= pdev
->dev
.parent
;
1594 pdata
->rx_param
= pdev
->dev
.parent
;
1595 pdata
->dma_filter
= pxa2xx_spi_idma_filter
;
1599 ssp
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
1600 if (IS_ERR(ssp
->clk
))
1603 ssp
->irq
= platform_get_irq(pdev
, 0);
1609 ssp
->port_id
= pxa2xx_spi_get_port_id(adev
);
1611 pdata
->is_slave
= of_property_read_bool(pdev
->dev
.of_node
, "spi-slave");
1612 pdata
->num_chipselect
= 1;
1613 pdata
->enable_dma
= true;
1614 pdata
->dma_burst_size
= 1;
1619 static int pxa2xx_spi_fw_translate_cs(struct spi_controller
*controller
,
1622 struct driver_data
*drv_data
= spi_controller_get_devdata(controller
);
1624 if (has_acpi_companion(&drv_data
->pdev
->dev
)) {
1625 switch (drv_data
->ssp_type
) {
1627 * For Atoms the ACPI DeviceSelection used by the Windows
1628 * driver starts from 1 instead of 0 so translate it here
1629 * to match what Linux expects.
1643 static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device
*spi
)
1648 static int pxa2xx_spi_probe(struct platform_device
*pdev
)
1650 struct device
*dev
= &pdev
->dev
;
1651 struct pxa2xx_spi_controller
*platform_info
;
1652 struct spi_controller
*controller
;
1653 struct driver_data
*drv_data
;
1654 struct ssp_device
*ssp
;
1655 const struct lpss_config
*config
;
1659 platform_info
= dev_get_platdata(dev
);
1660 if (!platform_info
) {
1661 platform_info
= pxa2xx_spi_init_pdata(pdev
);
1662 if (!platform_info
) {
1663 dev_err(&pdev
->dev
, "missing platform data\n");
1668 ssp
= pxa_ssp_request(pdev
->id
, pdev
->name
);
1670 ssp
= &platform_info
->ssp
;
1672 if (!ssp
->mmio_base
) {
1673 dev_err(&pdev
->dev
, "failed to get ssp\n");
1677 if (platform_info
->is_slave
)
1678 controller
= spi_alloc_slave(dev
, sizeof(struct driver_data
));
1680 controller
= spi_alloc_master(dev
, sizeof(struct driver_data
));
1683 dev_err(&pdev
->dev
, "cannot alloc spi_controller\n");
1687 drv_data
= spi_controller_get_devdata(controller
);
1688 drv_data
->controller
= controller
;
1689 drv_data
->controller_info
= platform_info
;
1690 drv_data
->pdev
= pdev
;
1691 drv_data
->ssp
= ssp
;
1693 controller
->dev
.of_node
= pdev
->dev
.of_node
;
1694 /* the spi->mode bits understood by this driver: */
1695 controller
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
| SPI_LOOP
;
1697 controller
->bus_num
= ssp
->port_id
;
1698 controller
->dma_alignment
= DMA_ALIGNMENT
;
1699 controller
->cleanup
= cleanup
;
1700 controller
->setup
= setup
;
1701 controller
->set_cs
= pxa2xx_spi_set_cs
;
1702 controller
->transfer_one
= pxa2xx_spi_transfer_one
;
1703 controller
->slave_abort
= pxa2xx_spi_slave_abort
;
1704 controller
->handle_err
= pxa2xx_spi_handle_err
;
1705 controller
->unprepare_transfer_hardware
= pxa2xx_spi_unprepare_transfer
;
1706 controller
->fw_translate_cs
= pxa2xx_spi_fw_translate_cs
;
1707 controller
->auto_runtime_pm
= true;
1708 controller
->flags
= SPI_CONTROLLER_MUST_RX
| SPI_CONTROLLER_MUST_TX
;
1710 drv_data
->ssp_type
= ssp
->type
;
1712 drv_data
->ioaddr
= ssp
->mmio_base
;
1713 drv_data
->ssdr_physical
= ssp
->phys_base
+ SSDR
;
1714 if (pxa25x_ssp_comp(drv_data
)) {
1715 switch (drv_data
->ssp_type
) {
1716 case QUARK_X1000_SSP
:
1717 controller
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 32);
1720 controller
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 16);
1724 drv_data
->int_cr1
= SSCR1_TIE
| SSCR1_RIE
;
1725 drv_data
->dma_cr1
= 0;
1726 drv_data
->clear_sr
= SSSR_ROR
;
1727 drv_data
->mask_sr
= SSSR_RFS
| SSSR_TFS
| SSSR_ROR
;
1729 controller
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 32);
1730 drv_data
->int_cr1
= SSCR1_TIE
| SSCR1_RIE
| SSCR1_TINTE
;
1731 drv_data
->dma_cr1
= DEFAULT_DMA_CR1
;
1732 drv_data
->clear_sr
= SSSR_ROR
| SSSR_TINT
;
1733 drv_data
->mask_sr
= SSSR_TINT
| SSSR_RFS
| SSSR_TFS
1734 | SSSR_ROR
| SSSR_TUR
;
1737 status
= request_irq(ssp
->irq
, ssp_int
, IRQF_SHARED
, dev_name(dev
),
1740 dev_err(&pdev
->dev
, "cannot get IRQ %d\n", ssp
->irq
);
1741 goto out_error_controller_alloc
;
1744 /* Setup DMA if requested */
1745 if (platform_info
->enable_dma
) {
1746 status
= pxa2xx_spi_dma_setup(drv_data
);
1748 dev_warn(dev
, "no DMA channels available, using PIO\n");
1749 platform_info
->enable_dma
= false;
1751 controller
->can_dma
= pxa2xx_spi_can_dma
;
1752 controller
->max_dma_len
= MAX_DMA_LEN
;
1753 controller
->max_transfer_size
=
1754 pxa2xx_spi_max_dma_transfer_size
;
1758 /* Enable SOC clock */
1759 status
= clk_prepare_enable(ssp
->clk
);
1761 goto out_error_dma_irq_alloc
;
1763 controller
->max_speed_hz
= clk_get_rate(ssp
->clk
);
1765 * Set minimum speed for all other platforms than Intel Quark which is
1766 * able do under 1 Hz transfers.
1768 if (!pxa25x_ssp_comp(drv_data
))
1769 controller
->min_speed_hz
=
1770 DIV_ROUND_UP(controller
->max_speed_hz
, 4096);
1771 else if (!is_quark_x1000_ssp(drv_data
))
1772 controller
->min_speed_hz
=
1773 DIV_ROUND_UP(controller
->max_speed_hz
, 512);
1775 /* Load default SSP configuration */
1776 pxa2xx_spi_write(drv_data
, SSCR0
, 0);
1777 switch (drv_data
->ssp_type
) {
1778 case QUARK_X1000_SSP
:
1779 tmp
= QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT
) |
1780 QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT
);
1781 pxa2xx_spi_write(drv_data
, SSCR1
, tmp
);
1783 /* using the Motorola SPI protocol and use 8 bit frame */
1784 tmp
= QUARK_X1000_SSCR0_Motorola
| QUARK_X1000_SSCR0_DataSize(8);
1785 pxa2xx_spi_write(drv_data
, SSCR0
, tmp
);
1788 tmp
= CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT
) |
1789 CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT
);
1790 pxa2xx_spi_write(drv_data
, SSCR1
, tmp
);
1791 tmp
= SSCR0_SCR(2) | SSCR0_Motorola
| SSCR0_DataSize(8);
1792 pxa2xx_spi_write(drv_data
, SSCR0
, tmp
);
1796 if (spi_controller_is_slave(controller
)) {
1804 tmp
= SSCR1_RxTresh(RX_THRESH_DFLT
) |
1805 SSCR1_TxTresh(TX_THRESH_DFLT
);
1807 pxa2xx_spi_write(drv_data
, SSCR1
, tmp
);
1808 tmp
= SSCR0_Motorola
| SSCR0_DataSize(8);
1809 if (!spi_controller_is_slave(controller
))
1810 tmp
|= SSCR0_SCR(2);
1811 pxa2xx_spi_write(drv_data
, SSCR0
, tmp
);
1815 if (!pxa25x_ssp_comp(drv_data
))
1816 pxa2xx_spi_write(drv_data
, SSTO
, 0);
1818 if (!is_quark_x1000_ssp(drv_data
))
1819 pxa2xx_spi_write(drv_data
, SSPSP
, 0);
1821 if (is_lpss_ssp(drv_data
)) {
1822 lpss_ssp_setup(drv_data
);
1823 config
= lpss_get_config(drv_data
);
1824 if (config
->reg_capabilities
>= 0) {
1825 tmp
= __lpss_ssp_read_priv(drv_data
,
1826 config
->reg_capabilities
);
1827 tmp
&= LPSS_CAPS_CS_EN_MASK
;
1828 tmp
>>= LPSS_CAPS_CS_EN_SHIFT
;
1829 platform_info
->num_chipselect
= ffz(tmp
);
1830 } else if (config
->cs_num
) {
1831 platform_info
->num_chipselect
= config
->cs_num
;
1834 controller
->num_chipselect
= platform_info
->num_chipselect
;
1836 count
= gpiod_count(&pdev
->dev
, "cs");
1840 controller
->num_chipselect
= max_t(int, count
,
1841 controller
->num_chipselect
);
1843 drv_data
->cs_gpiods
= devm_kcalloc(&pdev
->dev
,
1844 controller
->num_chipselect
, sizeof(struct gpio_desc
*),
1846 if (!drv_data
->cs_gpiods
) {
1848 goto out_error_clock_enabled
;
1851 for (i
= 0; i
< controller
->num_chipselect
; i
++) {
1852 struct gpio_desc
*gpiod
;
1854 gpiod
= devm_gpiod_get_index(dev
, "cs", i
, GPIOD_ASIS
);
1855 if (IS_ERR(gpiod
)) {
1856 /* Means use native chip select */
1857 if (PTR_ERR(gpiod
) == -ENOENT
)
1860 status
= PTR_ERR(gpiod
);
1861 goto out_error_clock_enabled
;
1863 drv_data
->cs_gpiods
[i
] = gpiod
;
1868 if (platform_info
->is_slave
) {
1869 drv_data
->gpiod_ready
= devm_gpiod_get_optional(dev
,
1870 "ready", GPIOD_OUT_LOW
);
1871 if (IS_ERR(drv_data
->gpiod_ready
)) {
1872 status
= PTR_ERR(drv_data
->gpiod_ready
);
1873 goto out_error_clock_enabled
;
1877 pm_runtime_set_autosuspend_delay(&pdev
->dev
, 50);
1878 pm_runtime_use_autosuspend(&pdev
->dev
);
1879 pm_runtime_set_active(&pdev
->dev
);
1880 pm_runtime_enable(&pdev
->dev
);
1882 /* Register with the SPI framework */
1883 platform_set_drvdata(pdev
, drv_data
);
1884 status
= spi_register_controller(controller
);
1886 dev_err(&pdev
->dev
, "problem registering spi controller\n");
1887 goto out_error_pm_runtime_enabled
;
1892 out_error_pm_runtime_enabled
:
1893 pm_runtime_disable(&pdev
->dev
);
1895 out_error_clock_enabled
:
1896 clk_disable_unprepare(ssp
->clk
);
1898 out_error_dma_irq_alloc
:
1899 pxa2xx_spi_dma_release(drv_data
);
1900 free_irq(ssp
->irq
, drv_data
);
1902 out_error_controller_alloc
:
1903 spi_controller_put(controller
);
1908 static int pxa2xx_spi_remove(struct platform_device
*pdev
)
1910 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1911 struct ssp_device
*ssp
;
1915 ssp
= drv_data
->ssp
;
1917 pm_runtime_get_sync(&pdev
->dev
);
1919 spi_unregister_controller(drv_data
->controller
);
1921 /* Disable the SSP at the peripheral and SOC level */
1922 pxa2xx_spi_write(drv_data
, SSCR0
, 0);
1923 clk_disable_unprepare(ssp
->clk
);
1926 if (drv_data
->controller_info
->enable_dma
)
1927 pxa2xx_spi_dma_release(drv_data
);
1929 pm_runtime_put_noidle(&pdev
->dev
);
1930 pm_runtime_disable(&pdev
->dev
);
1933 free_irq(ssp
->irq
, drv_data
);
1941 #ifdef CONFIG_PM_SLEEP
1942 static int pxa2xx_spi_suspend(struct device
*dev
)
1944 struct driver_data
*drv_data
= dev_get_drvdata(dev
);
1945 struct ssp_device
*ssp
= drv_data
->ssp
;
1948 status
= spi_controller_suspend(drv_data
->controller
);
1951 pxa2xx_spi_write(drv_data
, SSCR0
, 0);
1953 if (!pm_runtime_suspended(dev
))
1954 clk_disable_unprepare(ssp
->clk
);
1959 static int pxa2xx_spi_resume(struct device
*dev
)
1961 struct driver_data
*drv_data
= dev_get_drvdata(dev
);
1962 struct ssp_device
*ssp
= drv_data
->ssp
;
1965 /* Enable the SSP clock */
1966 if (!pm_runtime_suspended(dev
)) {
1967 status
= clk_prepare_enable(ssp
->clk
);
1972 /* Start the queue running */
1973 return spi_controller_resume(drv_data
->controller
);
1978 static int pxa2xx_spi_runtime_suspend(struct device
*dev
)
1980 struct driver_data
*drv_data
= dev_get_drvdata(dev
);
1982 clk_disable_unprepare(drv_data
->ssp
->clk
);
1986 static int pxa2xx_spi_runtime_resume(struct device
*dev
)
1988 struct driver_data
*drv_data
= dev_get_drvdata(dev
);
1991 status
= clk_prepare_enable(drv_data
->ssp
->clk
);
1996 static const struct dev_pm_ops pxa2xx_spi_pm_ops
= {
1997 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend
, pxa2xx_spi_resume
)
1998 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend
,
1999 pxa2xx_spi_runtime_resume
, NULL
)
2002 static struct platform_driver driver
= {
2004 .name
= "pxa2xx-spi",
2005 .pm
= &pxa2xx_spi_pm_ops
,
2006 .acpi_match_table
= ACPI_PTR(pxa2xx_spi_acpi_match
),
2007 .of_match_table
= of_match_ptr(pxa2xx_spi_of_match
),
2009 .probe
= pxa2xx_spi_probe
,
2010 .remove
= pxa2xx_spi_remove
,
2013 static int __init
pxa2xx_spi_init(void)
2015 return platform_driver_register(&driver
);
2017 subsys_initcall(pxa2xx_spi_init
);
2019 static void __exit
pxa2xx_spi_exit(void)
2021 platform_driver_unregister(&driver
);
2023 module_exit(pxa2xx_spi_exit
);
2025 MODULE_SOFTDEP("pre: dw_dmac");