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/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of_gpio.h>
15 #include <linux/platform_device.h>
17 #include <linux/spi/spi.h>
27 #define US_VERSION 0xFC
29 #define US_CR_RSTRX BIT(2)
30 #define US_CR_RSTTX BIT(3)
31 #define US_CR_RXEN BIT(4)
32 #define US_CR_RXDIS BIT(5)
33 #define US_CR_TXEN BIT(6)
34 #define US_CR_TXDIS BIT(7)
36 #define US_MR_SPI_MASTER 0x0E
37 #define US_MR_CHRL GENMASK(7, 6)
38 #define US_MR_CPHA BIT(8)
39 #define US_MR_CPOL BIT(16)
40 #define US_MR_CLKO BIT(18)
41 #define US_MR_WRDBT BIT(20)
42 #define US_MR_LOOP BIT(15)
44 #define US_IR_RXRDY BIT(0)
45 #define US_IR_TXRDY BIT(1)
46 #define US_IR_OVRE BIT(5)
48 #define US_BRGR_SIZE BIT(16)
50 #define US_MIN_CLK_DIV 0x06
51 #define US_MAX_CLK_DIV BIT(16)
53 #define US_RESET (US_CR_RSTRX | US_CR_RSTTX)
54 #define US_DISABLE (US_CR_RXDIS | US_CR_TXDIS)
55 #define US_ENABLE (US_CR_RXEN | US_CR_TXEN)
56 #define US_OVRE_RXRDY_IRQS (US_IR_OVRE | US_IR_RXRDY)
59 (US_MR_SPI_MASTER | US_MR_CHRL | US_MR_CLKO | US_MR_WRDBT)
61 /* Register access macros */
62 #define at91_usart_spi_readl(port, reg) \
63 readl_relaxed((port)->regs + US_##reg)
64 #define at91_usart_spi_writel(port, reg, value) \
65 writel_relaxed((value), (port)->regs + US_##reg)
67 #define at91_usart_spi_readb(port, reg) \
68 readb_relaxed((port)->regs + US_##reg)
69 #define at91_usart_spi_writeb(port, reg, value) \
70 writeb_relaxed((value), (port)->regs + US_##reg)
72 struct at91_usart_spi
{
73 struct spi_transfer
*current_transfer
;
78 /*used in interrupt to protect data reading*/
82 unsigned int current_tx_remaining_bytes
;
83 unsigned int current_rx_remaining_bytes
;
91 static inline u32
at91_usart_spi_tx_ready(struct at91_usart_spi
*aus
)
93 return aus
->status
& US_IR_TXRDY
;
96 static inline u32
at91_usart_spi_rx_ready(struct at91_usart_spi
*aus
)
98 return aus
->status
& US_IR_RXRDY
;
101 static inline u32
at91_usart_spi_check_overrun(struct at91_usart_spi
*aus
)
103 return aus
->status
& US_IR_OVRE
;
106 static inline u32
at91_usart_spi_read_status(struct at91_usart_spi
*aus
)
108 aus
->status
= at91_usart_spi_readl(aus
, CSR
);
112 static inline void at91_usart_spi_tx(struct at91_usart_spi
*aus
)
114 unsigned int len
= aus
->current_transfer
->len
;
115 unsigned int remaining
= aus
->current_tx_remaining_bytes
;
116 const u8
*tx_buf
= aus
->current_transfer
->tx_buf
;
121 if (at91_usart_spi_tx_ready(aus
)) {
122 at91_usart_spi_writeb(aus
, THR
, tx_buf
[len
- remaining
]);
123 aus
->current_tx_remaining_bytes
--;
127 static inline void at91_usart_spi_rx(struct at91_usart_spi
*aus
)
129 int len
= aus
->current_transfer
->len
;
130 int remaining
= aus
->current_rx_remaining_bytes
;
131 u8
*rx_buf
= aus
->current_transfer
->rx_buf
;
136 rx_buf
[len
- remaining
] = at91_usart_spi_readb(aus
, RHR
);
137 aus
->current_rx_remaining_bytes
--;
141 at91_usart_spi_set_xfer_speed(struct at91_usart_spi
*aus
,
142 struct spi_transfer
*xfer
)
144 at91_usart_spi_writel(aus
, BRGR
,
145 DIV_ROUND_UP(aus
->spi_clk
, xfer
->speed_hz
));
148 static irqreturn_t
at91_usart_spi_interrupt(int irq
, void *dev_id
)
150 struct spi_controller
*controller
= dev_id
;
151 struct at91_usart_spi
*aus
= spi_master_get_devdata(controller
);
153 spin_lock(&aus
->lock
);
154 at91_usart_spi_read_status(aus
);
156 if (at91_usart_spi_check_overrun(aus
)) {
157 aus
->xfer_failed
= true;
158 at91_usart_spi_writel(aus
, IDR
, US_IR_OVRE
| US_IR_RXRDY
);
159 spin_unlock(&aus
->lock
);
163 if (at91_usart_spi_rx_ready(aus
)) {
164 at91_usart_spi_rx(aus
);
165 spin_unlock(&aus
->lock
);
169 spin_unlock(&aus
->lock
);
174 static int at91_usart_spi_setup(struct spi_device
*spi
)
176 struct at91_usart_spi
*aus
= spi_master_get_devdata(spi
->controller
);
177 u32
*ausd
= spi
->controller_state
;
178 unsigned int mr
= at91_usart_spi_readl(aus
, MR
);
179 u8 bits
= spi
->bits_per_word
;
182 dev_dbg(&spi
->dev
, "Only 8 bits per word are supported\n");
186 if (spi
->mode
& SPI_CPOL
)
191 if (spi
->mode
& SPI_CPHA
)
196 if (spi
->mode
& SPI_LOOP
)
202 ausd
= kzalloc(sizeof(*ausd
), GFP_KERNEL
);
206 spi
->controller_state
= ausd
;
212 "setup: bpw %u mode 0x%x -> mr %d %08x\n",
213 bits
, spi
->mode
, spi
->chip_select
, mr
);
218 static int at91_usart_spi_transfer_one(struct spi_controller
*ctlr
,
219 struct spi_device
*spi
,
220 struct spi_transfer
*xfer
)
222 struct at91_usart_spi
*aus
= spi_master_get_devdata(ctlr
);
224 at91_usart_spi_set_xfer_speed(aus
, xfer
);
225 aus
->xfer_failed
= false;
226 aus
->current_transfer
= xfer
;
227 aus
->current_tx_remaining_bytes
= xfer
->len
;
228 aus
->current_rx_remaining_bytes
= xfer
->len
;
230 while ((aus
->current_tx_remaining_bytes
||
231 aus
->current_rx_remaining_bytes
) && !aus
->xfer_failed
) {
232 at91_usart_spi_read_status(aus
);
233 at91_usart_spi_tx(aus
);
237 if (aus
->xfer_failed
) {
238 dev_err(aus
->dev
, "Overrun!\n");
245 static int at91_usart_spi_prepare_message(struct spi_controller
*ctlr
,
246 struct spi_message
*message
)
248 struct at91_usart_spi
*aus
= spi_master_get_devdata(ctlr
);
249 struct spi_device
*spi
= message
->spi
;
250 u32
*ausd
= spi
->controller_state
;
252 at91_usart_spi_writel(aus
, CR
, US_ENABLE
);
253 at91_usart_spi_writel(aus
, IER
, US_OVRE_RXRDY_IRQS
);
254 at91_usart_spi_writel(aus
, MR
, *ausd
);
259 static int at91_usart_spi_unprepare_message(struct spi_controller
*ctlr
,
260 struct spi_message
*message
)
262 struct at91_usart_spi
*aus
= spi_master_get_devdata(ctlr
);
264 at91_usart_spi_writel(aus
, CR
, US_RESET
| US_DISABLE
);
265 at91_usart_spi_writel(aus
, IDR
, US_OVRE_RXRDY_IRQS
);
270 static void at91_usart_spi_cleanup(struct spi_device
*spi
)
272 struct at91_usart_spi_device
*ausd
= spi
->controller_state
;
274 spi
->controller_state
= NULL
;
278 static void at91_usart_spi_init(struct at91_usart_spi
*aus
)
280 at91_usart_spi_writel(aus
, MR
, US_INIT
);
281 at91_usart_spi_writel(aus
, CR
, US_RESET
| US_DISABLE
);
284 static int at91_usart_gpio_setup(struct platform_device
*pdev
)
286 struct device_node
*np
= pdev
->dev
.parent
->of_node
;
294 nb
= of_gpio_named_count(np
, "cs-gpios");
295 for (i
= 0; i
< nb
; i
++) {
296 int cs_gpio
= of_get_named_gpio(np
, "cs-gpios", i
);
301 if (gpio_is_valid(cs_gpio
)) {
302 ret
= devm_gpio_request_one(&pdev
->dev
, cs_gpio
,
304 dev_name(&pdev
->dev
));
313 static int at91_usart_spi_probe(struct platform_device
*pdev
)
315 struct resource
*regs
;
316 struct spi_controller
*controller
;
317 struct at91_usart_spi
*aus
;
322 regs
= platform_get_resource(to_platform_device(pdev
->dev
.parent
),
327 irq
= platform_get_irq(to_platform_device(pdev
->dev
.parent
), 0);
331 clk
= devm_clk_get(pdev
->dev
.parent
, "usart");
336 controller
= spi_alloc_master(&pdev
->dev
, sizeof(*aus
));
338 goto at91_usart_spi_probe_fail
;
340 ret
= at91_usart_gpio_setup(pdev
);
342 goto at91_usart_spi_probe_fail
;
344 controller
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_LOOP
| SPI_CS_HIGH
;
345 controller
->dev
.of_node
= pdev
->dev
.parent
->of_node
;
346 controller
->bits_per_word_mask
= SPI_BPW_MASK(8);
347 controller
->setup
= at91_usart_spi_setup
;
348 controller
->flags
= SPI_MASTER_MUST_RX
| SPI_MASTER_MUST_TX
;
349 controller
->transfer_one
= at91_usart_spi_transfer_one
;
350 controller
->prepare_message
= at91_usart_spi_prepare_message
;
351 controller
->unprepare_message
= at91_usart_spi_unprepare_message
;
352 controller
->cleanup
= at91_usart_spi_cleanup
;
353 controller
->max_speed_hz
= DIV_ROUND_UP(clk_get_rate(clk
),
355 controller
->min_speed_hz
= DIV_ROUND_UP(clk_get_rate(clk
),
357 platform_set_drvdata(pdev
, controller
);
359 aus
= spi_master_get_devdata(controller
);
361 aus
->dev
= &pdev
->dev
;
362 aus
->regs
= devm_ioremap_resource(&pdev
->dev
, regs
);
363 if (IS_ERR(aus
->regs
)) {
364 ret
= PTR_ERR(aus
->regs
);
365 goto at91_usart_spi_probe_fail
;
371 ret
= devm_request_irq(&pdev
->dev
, irq
, at91_usart_spi_interrupt
, 0,
372 dev_name(&pdev
->dev
), controller
);
374 goto at91_usart_spi_probe_fail
;
376 ret
= clk_prepare_enable(clk
);
378 goto at91_usart_spi_probe_fail
;
380 aus
->spi_clk
= clk_get_rate(clk
);
381 at91_usart_spi_init(aus
);
383 spin_lock_init(&aus
->lock
);
384 ret
= devm_spi_register_master(&pdev
->dev
, controller
);
386 goto at91_usart_fail_register_master
;
389 "AT91 USART SPI Controller version 0x%x at %pa (irq %d)\n",
390 at91_usart_spi_readl(aus
, VERSION
),
395 at91_usart_fail_register_master
:
396 clk_disable_unprepare(clk
);
397 at91_usart_spi_probe_fail
:
398 spi_master_put(controller
);
402 static int at91_usart_spi_remove(struct platform_device
*pdev
)
404 struct spi_controller
*ctlr
= platform_get_drvdata(pdev
);
405 struct at91_usart_spi
*aus
= spi_master_get_devdata(ctlr
);
407 clk_disable_unprepare(aus
->clk
);
412 static const struct of_device_id at91_usart_spi_dt_ids
[] = {
413 { .compatible
= "microchip,at91sam9g45-usart-spi"},
417 MODULE_DEVICE_TABLE(of
, at91_usart_spi_dt_ids
);
419 static struct platform_driver at91_usart_spi_driver
= {
421 .name
= "at91_usart_spi",
423 .probe
= at91_usart_spi_probe
,
424 .remove
= at91_usart_spi_remove
,
427 module_platform_driver(at91_usart_spi_driver
);
429 MODULE_DESCRIPTION("Microchip AT91 USART SPI Controller driver");
430 MODULE_AUTHOR("Radu Pirea <radu.pirea@microchip.com>");
431 MODULE_LICENSE("GPL v2");
432 MODULE_ALIAS("platform:at91_usart_spi");