2 * Driver for Amlogic Meson SPI communication controller (SPICC)
4 * Copyright (C) BayLibre, SAS
5 * Author: Neil Armstrong <narmstrong@baylibre.com>
7 * SPDX-License-Identifier: GPL-2.0+
10 #include <linux/bitfield.h>
11 #include <linux/clk.h>
12 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/spi/spi.h>
19 #include <linux/types.h>
20 #include <linux/interrupt.h>
21 #include <linux/reset.h>
22 #include <linux/gpio.h>
25 * The Meson SPICC controller could support DMA based transfers, but is not
26 * implemented by the vendor code, and while having the registers documentation
27 * it has never worked on the GXL Hardware.
28 * The PIO mode is the only mode implemented, and due to badly designed HW :
29 * - all transfers are cutted in 16 words burst because the FIFO hangs on
30 * TX underflow, and there is no TX "Half-Empty" interrupt, so we go by
31 * FIFO max size chunk only
32 * - CS management is dumb, and goes UP between every burst, so is really a
33 * "Data Valid" signal than a Chip Select, GPIO link should be used instead
34 * to have a CS go down over the full transfer
37 #define SPICC_MAX_FREQ 30000000
38 #define SPICC_MAX_BURST 128
41 #define SPICC_RXDATA 0x00
43 #define SPICC_TXDATA 0x04
45 #define SPICC_CONREG 0x08
46 #define SPICC_ENABLE BIT(0)
47 #define SPICC_MODE_MASTER BIT(1)
48 #define SPICC_XCH BIT(2)
49 #define SPICC_SMC BIT(3)
50 #define SPICC_POL BIT(4)
51 #define SPICC_PHA BIT(5)
52 #define SPICC_SSCTL BIT(6)
53 #define SPICC_SSPOL BIT(7)
54 #define SPICC_DRCTL_MASK GENMASK(9, 8)
55 #define SPICC_DRCTL_IGNORE 0
56 #define SPICC_DRCTL_FALLING 1
57 #define SPICC_DRCTL_LOWLEVEL 2
58 #define SPICC_CS_MASK GENMASK(13, 12)
59 #define SPICC_DATARATE_MASK GENMASK(18, 16)
60 #define SPICC_DATARATE_DIV4 0
61 #define SPICC_DATARATE_DIV8 1
62 #define SPICC_DATARATE_DIV16 2
63 #define SPICC_DATARATE_DIV32 3
64 #define SPICC_BITLENGTH_MASK GENMASK(24, 19)
65 #define SPICC_BURSTLENGTH_MASK GENMASK(31, 25)
67 #define SPICC_INTREG 0x0c
68 #define SPICC_TE_EN BIT(0) /* TX FIFO Empty Interrupt */
69 #define SPICC_TH_EN BIT(1) /* TX FIFO Half-Full Interrupt */
70 #define SPICC_TF_EN BIT(2) /* TX FIFO Full Interrupt */
71 #define SPICC_RR_EN BIT(3) /* RX FIFO Ready Interrupt */
72 #define SPICC_RH_EN BIT(4) /* RX FIFO Half-Full Interrupt */
73 #define SPICC_RF_EN BIT(5) /* RX FIFO Full Interrupt */
74 #define SPICC_RO_EN BIT(6) /* RX FIFO Overflow Interrupt */
75 #define SPICC_TC_EN BIT(7) /* Transfert Complete Interrupt */
77 #define SPICC_DMAREG 0x10
78 #define SPICC_DMA_ENABLE BIT(0)
79 #define SPICC_TXFIFO_THRESHOLD_MASK GENMASK(5, 1)
80 #define SPICC_RXFIFO_THRESHOLD_MASK GENMASK(10, 6)
81 #define SPICC_READ_BURST_MASK GENMASK(14, 11)
82 #define SPICC_WRITE_BURST_MASK GENMASK(18, 15)
83 #define SPICC_DMA_URGENT BIT(19)
84 #define SPICC_DMA_THREADID_MASK GENMASK(25, 20)
85 #define SPICC_DMA_BURSTNUM_MASK GENMASK(31, 26)
87 #define SPICC_STATREG 0x14
88 #define SPICC_TE BIT(0) /* TX FIFO Empty Interrupt */
89 #define SPICC_TH BIT(1) /* TX FIFO Half-Full Interrupt */
90 #define SPICC_TF BIT(2) /* TX FIFO Full Interrupt */
91 #define SPICC_RR BIT(3) /* RX FIFO Ready Interrupt */
92 #define SPICC_RH BIT(4) /* RX FIFO Half-Full Interrupt */
93 #define SPICC_RF BIT(5) /* RX FIFO Full Interrupt */
94 #define SPICC_RO BIT(6) /* RX FIFO Overflow Interrupt */
95 #define SPICC_TC BIT(7) /* Transfert Complete Interrupt */
97 #define SPICC_PERIODREG 0x18
98 #define SPICC_PERIOD GENMASK(14, 0) /* Wait cycles */
100 #define SPICC_TESTREG 0x1c
101 #define SPICC_TXCNT_MASK GENMASK(4, 0) /* TX FIFO Counter */
102 #define SPICC_RXCNT_MASK GENMASK(9, 5) /* RX FIFO Counter */
103 #define SPICC_SMSTATUS_MASK GENMASK(12, 10) /* State Machine Status */
104 #define SPICC_LBC_RO BIT(13) /* Loop Back Control Read-Only */
105 #define SPICC_LBC_W1 BIT(14) /* Loop Back Control Write-Only */
106 #define SPICC_SWAP_RO BIT(14) /* RX FIFO Data Swap Read-Only */
107 #define SPICC_SWAP_W1 BIT(15) /* RX FIFO Data Swap Write-Only */
108 #define SPICC_DLYCTL_RO_MASK GENMASK(20, 15) /* Delay Control Read-Only */
109 #define SPICC_DLYCTL_W1_MASK GENMASK(21, 16) /* Delay Control Write-Only */
110 #define SPICC_FIFORST_RO_MASK GENMASK(22, 21) /* FIFO Softreset Read-Only */
111 #define SPICC_FIFORST_W1_MASK GENMASK(23, 22) /* FIFO Softreset Write-Only */
113 #define SPICC_DRADDR 0x20 /* Read Address of DMA */
115 #define SPICC_DWADDR 0x24 /* Write Address of DMA */
117 #define writel_bits_relaxed(mask, val, addr) \
118 writel_relaxed((readl_relaxed(addr) & ~(mask)) | (val), addr)
120 #define SPICC_BURST_MAX 16
121 #define SPICC_FIFO_HALF 10
123 struct meson_spicc_device
{
124 struct spi_master
*master
;
125 struct platform_device
*pdev
;
128 struct spi_message
*message
;
129 struct spi_transfer
*xfer
;
132 unsigned int bytes_per_word
;
133 unsigned long tx_remain
;
134 unsigned long txb_remain
;
135 unsigned long rx_remain
;
136 unsigned long rxb_remain
;
137 unsigned long xfer_remain
;
142 static inline bool meson_spicc_txfull(struct meson_spicc_device
*spicc
)
144 return !!FIELD_GET(SPICC_TF
,
145 readl_relaxed(spicc
->base
+ SPICC_STATREG
));
148 static inline bool meson_spicc_rxready(struct meson_spicc_device
*spicc
)
150 return FIELD_GET(SPICC_RH
| SPICC_RR
| SPICC_RF_EN
,
151 readl_relaxed(spicc
->base
+ SPICC_STATREG
));
154 static inline u32
meson_spicc_pull_data(struct meson_spicc_device
*spicc
)
156 unsigned int bytes
= spicc
->bytes_per_word
;
157 unsigned int byte_shift
= 0;
162 byte
= *spicc
->tx_buf
++;
163 data
|= (byte
& 0xff) << byte_shift
;
171 static inline void meson_spicc_push_data(struct meson_spicc_device
*spicc
,
174 unsigned int bytes
= spicc
->bytes_per_word
;
175 unsigned int byte_shift
= 0;
179 byte
= (data
>> byte_shift
) & 0xff;
180 *spicc
->rx_buf
++ = byte
;
187 static inline void meson_spicc_rx(struct meson_spicc_device
*spicc
)
190 while (spicc
->rx_remain
&&
191 meson_spicc_rxready(spicc
))
192 meson_spicc_push_data(spicc
,
193 readl_relaxed(spicc
->base
+ SPICC_RXDATA
));
196 static inline void meson_spicc_tx(struct meson_spicc_device
*spicc
)
198 /* Fill Up TX FIFO */
199 while (spicc
->tx_remain
&&
200 !meson_spicc_txfull(spicc
))
201 writel_relaxed(meson_spicc_pull_data(spicc
),
202 spicc
->base
+ SPICC_TXDATA
);
205 static inline u32
meson_spicc_setup_rx_irq(struct meson_spicc_device
*spicc
,
208 if (spicc
->rx_remain
> SPICC_FIFO_HALF
)
209 irq_ctrl
|= SPICC_RH_EN
;
211 irq_ctrl
|= SPICC_RR_EN
;
216 static inline void meson_spicc_setup_burst(struct meson_spicc_device
*spicc
,
217 unsigned int burst_len
)
219 /* Setup Xfer variables */
220 spicc
->tx_remain
= burst_len
;
221 spicc
->rx_remain
= burst_len
;
222 spicc
->xfer_remain
-= burst_len
* spicc
->bytes_per_word
;
223 spicc
->is_burst_end
= false;
224 if (burst_len
< SPICC_BURST_MAX
|| !spicc
->xfer_remain
)
225 spicc
->is_last_burst
= true;
227 spicc
->is_last_burst
= false;
229 /* Setup burst length */
230 writel_bits_relaxed(SPICC_BURSTLENGTH_MASK
,
231 FIELD_PREP(SPICC_BURSTLENGTH_MASK
,
233 spicc
->base
+ SPICC_CONREG
);
236 meson_spicc_tx(spicc
);
239 static irqreturn_t
meson_spicc_irq(int irq
, void *data
)
241 struct meson_spicc_device
*spicc
= (void *) data
;
242 u32 ctrl
= readl_relaxed(spicc
->base
+ SPICC_INTREG
);
243 u32 stat
= readl_relaxed(spicc
->base
+ SPICC_STATREG
) & ctrl
;
245 ctrl
&= ~(SPICC_RH_EN
| SPICC_RR_EN
);
248 meson_spicc_rx(spicc
);
250 /* Enable TC interrupt since we transferred everything */
251 if (!spicc
->tx_remain
&& !spicc
->rx_remain
) {
252 spicc
->is_burst_end
= true;
254 /* Enable TC interrupt */
257 /* Reload IRQ status */
258 stat
= readl_relaxed(spicc
->base
+ SPICC_STATREG
) & ctrl
;
261 /* Check transfer complete */
262 if ((stat
& SPICC_TC
) && spicc
->is_burst_end
) {
263 unsigned int burst_len
;
266 writel_relaxed(SPICC_TC
, spicc
->base
+ SPICC_STATREG
);
268 /* Disable TC interrupt */
269 ctrl
&= ~SPICC_TC_EN
;
271 if (spicc
->is_last_burst
) {
272 /* Disable all IRQs */
273 writel(0, spicc
->base
+ SPICC_INTREG
);
275 spi_finalize_current_transfer(spicc
->master
);
280 burst_len
= min_t(unsigned int,
281 spicc
->xfer_remain
/ spicc
->bytes_per_word
,
285 meson_spicc_setup_burst(spicc
, burst_len
);
288 writel_bits_relaxed(SPICC_XCH
, SPICC_XCH
,
289 spicc
->base
+ SPICC_CONREG
);
292 /* Setup RX interrupt trigger */
293 ctrl
= meson_spicc_setup_rx_irq(spicc
, ctrl
);
295 /* Reconfigure interrupts */
296 writel(ctrl
, spicc
->base
+ SPICC_INTREG
);
301 static u32
meson_spicc_setup_speed(struct meson_spicc_device
*spicc
, u32 conf
,
304 unsigned long parent
, value
;
307 parent
= clk_get_rate(spicc
->core
);
309 /* Find closest inferior/equal possible speed */
310 for (i
= 0 ; i
< 7 ; ++i
) {
311 /* 2^(data_rate+2) */
312 value
= parent
>> (i
+ 2);
318 /* If provided speed it lower than max divider, use max divider */
321 dev_warn_once(&spicc
->pdev
->dev
, "unable to get close to speed %u\n",
326 dev_dbg(&spicc
->pdev
->dev
, "parent %lu, speed %u -> %lu (%u)\n",
327 parent
, speed
, value
, div
);
329 conf
&= ~SPICC_DATARATE_MASK
;
330 conf
|= FIELD_PREP(SPICC_DATARATE_MASK
, div
);
335 static void meson_spicc_setup_xfer(struct meson_spicc_device
*spicc
,
336 struct spi_transfer
*xfer
)
340 /* Read original configuration */
341 conf
= conf_orig
= readl_relaxed(spicc
->base
+ SPICC_CONREG
);
343 /* Select closest divider */
344 conf
= meson_spicc_setup_speed(spicc
, conf
, xfer
->speed_hz
);
346 /* Setup word width */
347 conf
&= ~SPICC_BITLENGTH_MASK
;
348 conf
|= FIELD_PREP(SPICC_BITLENGTH_MASK
,
349 (spicc
->bytes_per_word
<< 3) - 1);
351 /* Ignore if unchanged */
352 if (conf
!= conf_orig
)
353 writel_relaxed(conf
, spicc
->base
+ SPICC_CONREG
);
356 static int meson_spicc_transfer_one(struct spi_master
*master
,
357 struct spi_device
*spi
,
358 struct spi_transfer
*xfer
)
360 struct meson_spicc_device
*spicc
= spi_master_get_devdata(master
);
361 unsigned int burst_len
;
364 /* Store current transfer */
367 /* Setup transfer parameters */
368 spicc
->tx_buf
= (u8
*)xfer
->tx_buf
;
369 spicc
->rx_buf
= (u8
*)xfer
->rx_buf
;
370 spicc
->xfer_remain
= xfer
->len
;
372 /* Pre-calculate word size */
373 spicc
->bytes_per_word
=
374 DIV_ROUND_UP(spicc
->xfer
->bits_per_word
, 8);
376 /* Setup transfer parameters */
377 meson_spicc_setup_xfer(spicc
, xfer
);
379 burst_len
= min_t(unsigned int,
380 spicc
->xfer_remain
/ spicc
->bytes_per_word
,
383 meson_spicc_setup_burst(spicc
, burst_len
);
385 irq
= meson_spicc_setup_rx_irq(spicc
, irq
);
388 writel_bits_relaxed(SPICC_XCH
, SPICC_XCH
, spicc
->base
+ SPICC_CONREG
);
390 /* Enable interrupts */
391 writel_relaxed(irq
, spicc
->base
+ SPICC_INTREG
);
396 static int meson_spicc_prepare_message(struct spi_master
*master
,
397 struct spi_message
*message
)
399 struct meson_spicc_device
*spicc
= spi_master_get_devdata(master
);
400 struct spi_device
*spi
= message
->spi
;
403 /* Store current message */
404 spicc
->message
= message
;
407 conf
|= SPICC_ENABLE
;
408 conf
|= SPICC_MODE_MASTER
;
412 /* Setup transfer mode */
413 if (spi
->mode
& SPI_CPOL
)
418 if (spi
->mode
& SPI_CPHA
)
425 if (spi
->mode
& SPI_CS_HIGH
)
428 conf
&= ~SPICC_SSPOL
;
430 if (spi
->mode
& SPI_READY
)
431 conf
|= FIELD_PREP(SPICC_DRCTL_MASK
, SPICC_DRCTL_LOWLEVEL
);
433 conf
|= FIELD_PREP(SPICC_DRCTL_MASK
, SPICC_DRCTL_IGNORE
);
436 conf
|= FIELD_PREP(SPICC_CS_MASK
, spi
->chip_select
);
438 /* Default Clock rate core/4 */
440 /* Default 8bit word */
441 conf
|= FIELD_PREP(SPICC_BITLENGTH_MASK
, 8 - 1);
443 writel_relaxed(conf
, spicc
->base
+ SPICC_CONREG
);
445 /* Setup no wait cycles by default */
446 writel_relaxed(0, spicc
->base
+ SPICC_PERIODREG
);
448 writel_bits_relaxed(BIT(24), BIT(24), spicc
->base
+ SPICC_TESTREG
);
453 static int meson_spicc_unprepare_transfer(struct spi_master
*master
)
455 struct meson_spicc_device
*spicc
= spi_master_get_devdata(master
);
457 /* Disable all IRQs */
458 writel(0, spicc
->base
+ SPICC_INTREG
);
460 /* Disable controller */
461 writel_bits_relaxed(SPICC_ENABLE
, 0, spicc
->base
+ SPICC_CONREG
);
463 device_reset_optional(&spicc
->pdev
->dev
);
468 static int meson_spicc_setup(struct spi_device
*spi
)
472 if (!spi
->controller_state
)
473 spi
->controller_state
= spi_master_get_devdata(spi
->master
);
474 else if (gpio_is_valid(spi
->cs_gpio
))
476 else if (spi
->cs_gpio
== -ENOENT
)
479 if (gpio_is_valid(spi
->cs_gpio
)) {
480 ret
= gpio_request(spi
->cs_gpio
, dev_name(&spi
->dev
));
482 dev_err(&spi
->dev
, "failed to request cs gpio\n");
488 ret
= gpio_direction_output(spi
->cs_gpio
,
489 !(spi
->mode
& SPI_CS_HIGH
));
494 static void meson_spicc_cleanup(struct spi_device
*spi
)
496 if (gpio_is_valid(spi
->cs_gpio
))
497 gpio_free(spi
->cs_gpio
);
499 spi
->controller_state
= NULL
;
502 static int meson_spicc_probe(struct platform_device
*pdev
)
504 struct spi_master
*master
;
505 struct meson_spicc_device
*spicc
;
506 struct resource
*res
;
509 master
= spi_alloc_master(&pdev
->dev
, sizeof(*spicc
));
511 dev_err(&pdev
->dev
, "master allocation failed\n");
514 spicc
= spi_master_get_devdata(master
);
515 spicc
->master
= master
;
518 platform_set_drvdata(pdev
, spicc
);
520 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
521 spicc
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
522 if (IS_ERR(spicc
->base
)) {
523 dev_err(&pdev
->dev
, "io resource mapping failed\n");
524 ret
= PTR_ERR(spicc
->base
);
528 /* Disable all IRQs */
529 writel_relaxed(0, spicc
->base
+ SPICC_INTREG
);
531 irq
= platform_get_irq(pdev
, 0);
532 ret
= devm_request_irq(&pdev
->dev
, irq
, meson_spicc_irq
,
535 dev_err(&pdev
->dev
, "irq request failed\n");
539 spicc
->core
= devm_clk_get(&pdev
->dev
, "core");
540 if (IS_ERR(spicc
->core
)) {
541 dev_err(&pdev
->dev
, "core clock request failed\n");
542 ret
= PTR_ERR(spicc
->core
);
546 ret
= clk_prepare_enable(spicc
->core
);
548 dev_err(&pdev
->dev
, "core clock enable failed\n");
551 rate
= clk_get_rate(spicc
->core
);
553 device_reset_optional(&pdev
->dev
);
555 master
->num_chipselect
= 4;
556 master
->dev
.of_node
= pdev
->dev
.of_node
;
557 master
->mode_bits
= SPI_CPHA
| SPI_CPOL
| SPI_CS_HIGH
;
558 master
->bits_per_word_mask
= SPI_BPW_MASK(32) |
562 master
->flags
= (SPI_MASTER_MUST_RX
| SPI_MASTER_MUST_TX
);
563 master
->min_speed_hz
= rate
>> 9;
564 master
->setup
= meson_spicc_setup
;
565 master
->cleanup
= meson_spicc_cleanup
;
566 master
->prepare_message
= meson_spicc_prepare_message
;
567 master
->unprepare_transfer_hardware
= meson_spicc_unprepare_transfer
;
568 master
->transfer_one
= meson_spicc_transfer_one
;
570 /* Setup max rate according to the Meson GX datasheet */
571 if ((rate
>> 2) > SPICC_MAX_FREQ
)
572 master
->max_speed_hz
= SPICC_MAX_FREQ
;
574 master
->max_speed_hz
= rate
>> 2;
576 ret
= devm_spi_register_master(&pdev
->dev
, master
);
578 dev_err(&pdev
->dev
, "spi master registration failed\n");
585 clk_disable_unprepare(spicc
->core
);
588 spi_master_put(master
);
593 static int meson_spicc_remove(struct platform_device
*pdev
)
595 struct meson_spicc_device
*spicc
= platform_get_drvdata(pdev
);
598 writel(0, spicc
->base
+ SPICC_CONREG
);
600 clk_disable_unprepare(spicc
->core
);
605 static const struct of_device_id meson_spicc_of_match
[] = {
606 { .compatible
= "amlogic,meson-gx-spicc", },
607 { .compatible
= "amlogic,meson-axg-spicc", },
610 MODULE_DEVICE_TABLE(of
, meson_spicc_of_match
);
612 static struct platform_driver meson_spicc_driver
= {
613 .probe
= meson_spicc_probe
,
614 .remove
= meson_spicc_remove
,
616 .name
= "meson-spicc",
617 .of_match_table
= of_match_ptr(meson_spicc_of_match
),
621 module_platform_driver(meson_spicc_driver
);
623 MODULE_DESCRIPTION("Meson SPI Communication Controller driver");
624 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
625 MODULE_LICENSE("GPL");