1 // SPDX-License-Identifier: GPL-2.0
3 // Driver for AT91 USART Controllers as SPI
5 // Copyright (C) 2018 Microchip Technology Inc.
7 // Author: Radu Pirea <radu.pirea@microchip.com>
10 #include <linux/delay.h>
11 #include <linux/dmaengine.h>
12 #include <linux/dma-direction.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
21 #include <linux/spi/spi.h>
31 #define US_VERSION 0xFC
33 #define US_CR_RSTRX BIT(2)
34 #define US_CR_RSTTX BIT(3)
35 #define US_CR_RXEN BIT(4)
36 #define US_CR_RXDIS BIT(5)
37 #define US_CR_TXEN BIT(6)
38 #define US_CR_TXDIS BIT(7)
40 #define US_MR_SPI_HOST 0x0E
41 #define US_MR_CHRL GENMASK(7, 6)
42 #define US_MR_CPHA BIT(8)
43 #define US_MR_CPOL BIT(16)
44 #define US_MR_CLKO BIT(18)
45 #define US_MR_WRDBT BIT(20)
46 #define US_MR_LOOP BIT(15)
48 #define US_IR_RXRDY BIT(0)
49 #define US_IR_TXRDY BIT(1)
50 #define US_IR_OVRE BIT(5)
52 #define US_BRGR_SIZE BIT(16)
54 #define US_MIN_CLK_DIV 0x06
55 #define US_MAX_CLK_DIV BIT(16)
57 #define US_RESET (US_CR_RSTRX | US_CR_RSTTX)
58 #define US_DISABLE (US_CR_RXDIS | US_CR_TXDIS)
59 #define US_ENABLE (US_CR_RXEN | US_CR_TXEN)
60 #define US_OVRE_RXRDY_IRQS (US_IR_OVRE | US_IR_RXRDY)
63 (US_MR_SPI_HOST | US_MR_CHRL | US_MR_CLKO | US_MR_WRDBT)
64 #define US_DMA_MIN_BYTES 16
65 #define US_DMA_TIMEOUT (msecs_to_jiffies(1000))
67 /* Register access macros */
68 #define at91_usart_spi_readl(port, reg) \
69 readl_relaxed((port)->regs + US_##reg)
70 #define at91_usart_spi_writel(port, reg, value) \
71 writel_relaxed((value), (port)->regs + US_##reg)
73 #define at91_usart_spi_readb(port, reg) \
74 readb_relaxed((port)->regs + US_##reg)
75 #define at91_usart_spi_writeb(port, reg, value) \
76 writeb_relaxed((value), (port)->regs + US_##reg)
78 struct at91_usart_spi
{
79 struct platform_device
*mpdev
;
80 struct spi_transfer
*current_transfer
;
85 struct completion xfer_completion
;
87 /*used in interrupt to protect data reading*/
93 unsigned int current_tx_remaining_bytes
;
94 unsigned int current_rx_remaining_bytes
;
103 static void dma_callback(void *data
)
105 struct spi_controller
*ctlr
= data
;
106 struct at91_usart_spi
*aus
= spi_controller_get_devdata(ctlr
);
108 at91_usart_spi_writel(aus
, IER
, US_IR_RXRDY
);
109 aus
->current_rx_remaining_bytes
= 0;
110 complete(&aus
->xfer_completion
);
113 static bool at91_usart_spi_can_dma(struct spi_controller
*ctrl
,
114 struct spi_device
*spi
,
115 struct spi_transfer
*xfer
)
117 struct at91_usart_spi
*aus
= spi_controller_get_devdata(ctrl
);
119 return aus
->use_dma
&& xfer
->len
>= US_DMA_MIN_BYTES
;
122 static int at91_usart_spi_configure_dma(struct spi_controller
*ctlr
,
123 struct at91_usart_spi
*aus
)
125 struct dma_slave_config slave_config
;
126 struct device
*dev
= &aus
->mpdev
->dev
;
127 phys_addr_t phybase
= aus
->phybase
;
132 dma_cap_set(DMA_SLAVE
, mask
);
134 ctlr
->dma_tx
= dma_request_chan(dev
, "tx");
135 if (IS_ERR(ctlr
->dma_tx
)) {
136 err
= PTR_ERR(ctlr
->dma_tx
);
137 goto at91_usart_spi_error_clear
;
140 ctlr
->dma_rx
= dma_request_chan(dev
, "rx");
141 if (IS_ERR(ctlr
->dma_rx
)) {
142 err
= PTR_ERR(ctlr
->dma_rx
);
143 goto at91_usart_spi_error
;
146 slave_config
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
147 slave_config
.src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
148 slave_config
.dst_addr
= (dma_addr_t
)phybase
+ US_THR
;
149 slave_config
.src_addr
= (dma_addr_t
)phybase
+ US_RHR
;
150 slave_config
.src_maxburst
= 1;
151 slave_config
.dst_maxburst
= 1;
152 slave_config
.device_fc
= false;
154 slave_config
.direction
= DMA_DEV_TO_MEM
;
155 if (dmaengine_slave_config(ctlr
->dma_rx
, &slave_config
)) {
157 "failed to configure rx dma channel\n");
159 goto at91_usart_spi_error
;
162 slave_config
.direction
= DMA_MEM_TO_DEV
;
163 if (dmaengine_slave_config(ctlr
->dma_tx
, &slave_config
)) {
165 "failed to configure tx dma channel\n");
167 goto at91_usart_spi_error
;
173 at91_usart_spi_error
:
174 if (!IS_ERR_OR_NULL(ctlr
->dma_tx
))
175 dma_release_channel(ctlr
->dma_tx
);
176 if (!IS_ERR_OR_NULL(ctlr
->dma_rx
))
177 dma_release_channel(ctlr
->dma_rx
);
181 at91_usart_spi_error_clear
:
185 static void at91_usart_spi_release_dma(struct spi_controller
*ctlr
)
188 dma_release_channel(ctlr
->dma_rx
);
190 dma_release_channel(ctlr
->dma_tx
);
193 static void at91_usart_spi_stop_dma(struct spi_controller
*ctlr
)
196 dmaengine_terminate_all(ctlr
->dma_rx
);
198 dmaengine_terminate_all(ctlr
->dma_tx
);
201 static int at91_usart_spi_dma_transfer(struct spi_controller
*ctlr
,
202 struct spi_transfer
*xfer
)
204 struct at91_usart_spi
*aus
= spi_controller_get_devdata(ctlr
);
205 struct dma_chan
*rxchan
= ctlr
->dma_rx
;
206 struct dma_chan
*txchan
= ctlr
->dma_tx
;
207 struct dma_async_tx_descriptor
*rxdesc
;
208 struct dma_async_tx_descriptor
*txdesc
;
211 /* Disable RX interrupt */
212 at91_usart_spi_writel(aus
, IDR
, US_IR_RXRDY
);
214 rxdesc
= dmaengine_prep_slave_sg(rxchan
,
221 goto at91_usart_spi_err_dma
;
223 txdesc
= dmaengine_prep_slave_sg(txchan
,
230 goto at91_usart_spi_err_dma
;
232 rxdesc
->callback
= dma_callback
;
233 rxdesc
->callback_param
= ctlr
;
235 cookie
= rxdesc
->tx_submit(rxdesc
);
236 if (dma_submit_error(cookie
))
237 goto at91_usart_spi_err_dma
;
239 cookie
= txdesc
->tx_submit(txdesc
);
240 if (dma_submit_error(cookie
))
241 goto at91_usart_spi_err_dma
;
243 rxchan
->device
->device_issue_pending(rxchan
);
244 txchan
->device
->device_issue_pending(txchan
);
248 at91_usart_spi_err_dma
:
249 /* Enable RX interrupt if something fails and fallback to PIO */
250 at91_usart_spi_writel(aus
, IER
, US_IR_RXRDY
);
251 at91_usart_spi_stop_dma(ctlr
);
256 static unsigned long at91_usart_spi_dma_timeout(struct at91_usart_spi
*aus
)
258 return wait_for_completion_timeout(&aus
->xfer_completion
,
262 static inline u32
at91_usart_spi_tx_ready(struct at91_usart_spi
*aus
)
264 return aus
->status
& US_IR_TXRDY
;
267 static inline u32
at91_usart_spi_rx_ready(struct at91_usart_spi
*aus
)
269 return aus
->status
& US_IR_RXRDY
;
272 static inline u32
at91_usart_spi_check_overrun(struct at91_usart_spi
*aus
)
274 return aus
->status
& US_IR_OVRE
;
277 static inline u32
at91_usart_spi_read_status(struct at91_usart_spi
*aus
)
279 aus
->status
= at91_usart_spi_readl(aus
, CSR
);
283 static inline void at91_usart_spi_tx(struct at91_usart_spi
*aus
)
285 unsigned int len
= aus
->current_transfer
->len
;
286 unsigned int remaining
= aus
->current_tx_remaining_bytes
;
287 const u8
*tx_buf
= aus
->current_transfer
->tx_buf
;
292 if (at91_usart_spi_tx_ready(aus
)) {
293 at91_usart_spi_writeb(aus
, THR
, tx_buf
[len
- remaining
]);
294 aus
->current_tx_remaining_bytes
--;
298 static inline void at91_usart_spi_rx(struct at91_usart_spi
*aus
)
300 int len
= aus
->current_transfer
->len
;
301 int remaining
= aus
->current_rx_remaining_bytes
;
302 u8
*rx_buf
= aus
->current_transfer
->rx_buf
;
307 rx_buf
[len
- remaining
] = at91_usart_spi_readb(aus
, RHR
);
308 aus
->current_rx_remaining_bytes
--;
312 at91_usart_spi_set_xfer_speed(struct at91_usart_spi
*aus
,
313 struct spi_transfer
*xfer
)
315 at91_usart_spi_writel(aus
, BRGR
,
316 DIV_ROUND_UP(aus
->spi_clk
, xfer
->speed_hz
));
319 static irqreturn_t
at91_usart_spi_interrupt(int irq
, void *dev_id
)
321 struct spi_controller
*controller
= dev_id
;
322 struct at91_usart_spi
*aus
= spi_controller_get_devdata(controller
);
324 spin_lock(&aus
->lock
);
325 at91_usart_spi_read_status(aus
);
327 if (at91_usart_spi_check_overrun(aus
)) {
328 aus
->xfer_failed
= true;
329 at91_usart_spi_writel(aus
, IDR
, US_IR_OVRE
| US_IR_RXRDY
);
330 spin_unlock(&aus
->lock
);
334 if (at91_usart_spi_rx_ready(aus
)) {
335 at91_usart_spi_rx(aus
);
336 spin_unlock(&aus
->lock
);
340 spin_unlock(&aus
->lock
);
345 static int at91_usart_spi_setup(struct spi_device
*spi
)
347 struct at91_usart_spi
*aus
= spi_controller_get_devdata(spi
->controller
);
348 u32
*ausd
= spi
->controller_state
;
349 unsigned int mr
= at91_usart_spi_readl(aus
, MR
);
351 if (spi
->mode
& SPI_CPOL
)
356 if (spi
->mode
& SPI_CPHA
)
361 if (spi
->mode
& SPI_LOOP
)
367 ausd
= kzalloc(sizeof(*ausd
), GFP_KERNEL
);
371 spi
->controller_state
= ausd
;
377 "setup: bpw %u mode 0x%x -> mr %d %08x\n",
378 spi
->bits_per_word
, spi
->mode
, spi_get_chipselect(spi
, 0), mr
);
383 static int at91_usart_spi_transfer_one(struct spi_controller
*ctlr
,
384 struct spi_device
*spi
,
385 struct spi_transfer
*xfer
)
387 struct at91_usart_spi
*aus
= spi_controller_get_devdata(ctlr
);
388 unsigned long dma_timeout
= 0;
391 at91_usart_spi_set_xfer_speed(aus
, xfer
);
392 aus
->xfer_failed
= false;
393 aus
->current_transfer
= xfer
;
394 aus
->current_tx_remaining_bytes
= xfer
->len
;
395 aus
->current_rx_remaining_bytes
= xfer
->len
;
397 while ((aus
->current_tx_remaining_bytes
||
398 aus
->current_rx_remaining_bytes
) && !aus
->xfer_failed
) {
399 reinit_completion(&aus
->xfer_completion
);
400 if (at91_usart_spi_can_dma(ctlr
, spi
, xfer
) &&
402 ret
= at91_usart_spi_dma_transfer(ctlr
, xfer
);
406 dma_timeout
= at91_usart_spi_dma_timeout(aus
);
408 if (WARN_ON(dma_timeout
== 0)) {
409 dev_err(&spi
->dev
, "DMA transfer timeout\n");
412 aus
->current_tx_remaining_bytes
= 0;
414 at91_usart_spi_read_status(aus
);
415 at91_usart_spi_tx(aus
);
421 if (aus
->xfer_failed
) {
422 dev_err(aus
->dev
, "Overrun!\n");
429 static int at91_usart_spi_prepare_message(struct spi_controller
*ctlr
,
430 struct spi_message
*message
)
432 struct at91_usart_spi
*aus
= spi_controller_get_devdata(ctlr
);
433 struct spi_device
*spi
= message
->spi
;
434 u32
*ausd
= spi
->controller_state
;
436 at91_usart_spi_writel(aus
, CR
, US_ENABLE
);
437 at91_usart_spi_writel(aus
, IER
, US_OVRE_RXRDY_IRQS
);
438 at91_usart_spi_writel(aus
, MR
, *ausd
);
443 static int at91_usart_spi_unprepare_message(struct spi_controller
*ctlr
,
444 struct spi_message
*message
)
446 struct at91_usart_spi
*aus
= spi_controller_get_devdata(ctlr
);
448 at91_usart_spi_writel(aus
, CR
, US_RESET
| US_DISABLE
);
449 at91_usart_spi_writel(aus
, IDR
, US_OVRE_RXRDY_IRQS
);
454 static void at91_usart_spi_cleanup(struct spi_device
*spi
)
456 struct at91_usart_spi_device
*ausd
= spi
->controller_state
;
458 spi
->controller_state
= NULL
;
462 static void at91_usart_spi_init(struct at91_usart_spi
*aus
)
464 at91_usart_spi_writel(aus
, MR
, US_INIT
);
465 at91_usart_spi_writel(aus
, CR
, US_RESET
| US_DISABLE
);
468 static int at91_usart_gpio_setup(struct platform_device
*pdev
)
470 struct gpio_descs
*cs_gpios
;
472 cs_gpios
= devm_gpiod_get_array_optional(&pdev
->dev
, "cs", GPIOD_OUT_LOW
);
474 return PTR_ERR_OR_ZERO(cs_gpios
);
477 static int at91_usart_spi_probe(struct platform_device
*pdev
)
479 struct resource
*regs
;
480 struct spi_controller
*controller
;
481 struct at91_usart_spi
*aus
;
486 regs
= platform_get_resource(to_platform_device(pdev
->dev
.parent
),
491 irq
= platform_get_irq(to_platform_device(pdev
->dev
.parent
), 0);
495 clk
= devm_clk_get(pdev
->dev
.parent
, "usart");
500 controller
= spi_alloc_host(&pdev
->dev
, sizeof(*aus
));
502 goto at91_usart_spi_probe_fail
;
504 ret
= at91_usart_gpio_setup(pdev
);
506 goto at91_usart_spi_probe_fail
;
508 controller
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_LOOP
| SPI_CS_HIGH
;
509 controller
->dev
.of_node
= pdev
->dev
.parent
->of_node
;
510 controller
->bits_per_word_mask
= SPI_BPW_MASK(8);
511 controller
->setup
= at91_usart_spi_setup
;
512 controller
->flags
= SPI_CONTROLLER_MUST_RX
| SPI_CONTROLLER_MUST_TX
;
513 controller
->transfer_one
= at91_usart_spi_transfer_one
;
514 controller
->prepare_message
= at91_usart_spi_prepare_message
;
515 controller
->unprepare_message
= at91_usart_spi_unprepare_message
;
516 controller
->can_dma
= at91_usart_spi_can_dma
;
517 controller
->cleanup
= at91_usart_spi_cleanup
;
518 controller
->max_speed_hz
= DIV_ROUND_UP(clk_get_rate(clk
),
520 controller
->min_speed_hz
= DIV_ROUND_UP(clk_get_rate(clk
),
522 platform_set_drvdata(pdev
, controller
);
524 aus
= spi_controller_get_devdata(controller
);
526 aus
->dev
= &pdev
->dev
;
527 aus
->regs
= devm_ioremap_resource(&pdev
->dev
, regs
);
528 if (IS_ERR(aus
->regs
)) {
529 ret
= PTR_ERR(aus
->regs
);
530 goto at91_usart_spi_probe_fail
;
536 ret
= devm_request_irq(&pdev
->dev
, irq
, at91_usart_spi_interrupt
, 0,
537 dev_name(&pdev
->dev
), controller
);
539 goto at91_usart_spi_probe_fail
;
541 ret
= clk_prepare_enable(clk
);
543 goto at91_usart_spi_probe_fail
;
545 aus
->spi_clk
= clk_get_rate(clk
);
546 at91_usart_spi_init(aus
);
548 aus
->phybase
= regs
->start
;
550 aus
->mpdev
= to_platform_device(pdev
->dev
.parent
);
552 ret
= at91_usart_spi_configure_dma(controller
, aus
);
554 goto at91_usart_fail_dma
;
556 spin_lock_init(&aus
->lock
);
557 init_completion(&aus
->xfer_completion
);
559 ret
= devm_spi_register_controller(&pdev
->dev
, controller
);
561 goto at91_usart_fail_register_controller
;
564 "AT91 USART SPI Controller version 0x%x at %pa (irq %d)\n",
565 at91_usart_spi_readl(aus
, VERSION
),
570 at91_usart_fail_register_controller
:
571 at91_usart_spi_release_dma(controller
);
573 clk_disable_unprepare(clk
);
574 at91_usart_spi_probe_fail
:
575 spi_controller_put(controller
);
579 __maybe_unused
static int at91_usart_spi_runtime_suspend(struct device
*dev
)
581 struct spi_controller
*ctlr
= dev_get_drvdata(dev
);
582 struct at91_usart_spi
*aus
= spi_controller_get_devdata(ctlr
);
584 clk_disable_unprepare(aus
->clk
);
585 pinctrl_pm_select_sleep_state(dev
);
590 __maybe_unused
static int at91_usart_spi_runtime_resume(struct device
*dev
)
592 struct spi_controller
*ctrl
= dev_get_drvdata(dev
);
593 struct at91_usart_spi
*aus
= spi_controller_get_devdata(ctrl
);
595 pinctrl_pm_select_default_state(dev
);
597 return clk_prepare_enable(aus
->clk
);
600 __maybe_unused
static int at91_usart_spi_suspend(struct device
*dev
)
602 struct spi_controller
*ctrl
= dev_get_drvdata(dev
);
605 ret
= spi_controller_suspend(ctrl
);
609 if (!pm_runtime_suspended(dev
))
610 at91_usart_spi_runtime_suspend(dev
);
615 __maybe_unused
static int at91_usart_spi_resume(struct device
*dev
)
617 struct spi_controller
*ctrl
= dev_get_drvdata(dev
);
618 struct at91_usart_spi
*aus
= spi_controller_get_devdata(ctrl
);
621 if (!pm_runtime_suspended(dev
)) {
622 ret
= at91_usart_spi_runtime_resume(dev
);
627 at91_usart_spi_init(aus
);
629 return spi_controller_resume(ctrl
);
632 static void at91_usart_spi_remove(struct platform_device
*pdev
)
634 struct spi_controller
*ctlr
= platform_get_drvdata(pdev
);
635 struct at91_usart_spi
*aus
= spi_controller_get_devdata(ctlr
);
637 at91_usart_spi_release_dma(ctlr
);
638 clk_disable_unprepare(aus
->clk
);
641 static const struct dev_pm_ops at91_usart_spi_pm_ops
= {
642 SET_SYSTEM_SLEEP_PM_OPS(at91_usart_spi_suspend
, at91_usart_spi_resume
)
643 SET_RUNTIME_PM_OPS(at91_usart_spi_runtime_suspend
,
644 at91_usart_spi_runtime_resume
, NULL
)
647 static struct platform_driver at91_usart_spi_driver
= {
649 .name
= "at91_usart_spi",
650 .pm
= &at91_usart_spi_pm_ops
,
652 .probe
= at91_usart_spi_probe
,
653 .remove
= at91_usart_spi_remove
,
656 module_platform_driver(at91_usart_spi_driver
);
658 MODULE_DESCRIPTION("Microchip AT91 USART SPI Controller driver");
659 MODULE_AUTHOR("Radu Pirea <radu.pirea@microchip.com>");
660 MODULE_LICENSE("GPL v2");
661 MODULE_ALIAS("platform:at91_usart_spi");