1 // SPDX-License-Identifier: GPL-2.0
3 * LiteUART serial controller (LiteX) Driver
5 * Copyright (C) 2019-2020 Antmicro <www.antmicro.com>
8 #include <linux/bits.h>
9 #include <linux/console.h>
10 #include <linux/interrupt.h>
11 #include <linux/litex.h>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/serial.h>
16 #include <linux/serial_core.h>
17 #include <linux/slab.h>
18 #include <linux/timer.h>
19 #include <linux/tty_flip.h>
20 #include <linux/xarray.h>
23 * CSRs definitions (base address offsets + width)
25 * The definitions below are true for LiteX SoC configured for 8-bit CSR Bus,
28 * Supporting other configurations might require new definitions or a more
29 * generic way of indexing the LiteX CSRs.
31 * For more details on how CSRs are defined and handled in LiteX, see comments
32 * in the LiteX SoC Driver: drivers/soc/litex/litex_soc_ctrl.c
35 #define OFF_TXFULL 0x04
36 #define OFF_RXEMPTY 0x08
37 #define OFF_EV_STATUS 0x0c
38 #define OFF_EV_PENDING 0x10
39 #define OFF_EV_ENABLE 0x14
45 struct liteuart_port
{
46 struct uart_port port
;
47 struct timer_list timer
;
51 #define to_liteuart_port(port) container_of(port, struct liteuart_port, port)
53 static DEFINE_XARRAY_FLAGS(liteuart_array
, XA_FLAGS_ALLOC
);
55 #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
56 static struct console liteuart_console
;
59 static struct uart_driver liteuart_driver
= {
61 .driver_name
= KBUILD_MODNAME
,
65 .nr
= CONFIG_SERIAL_LITEUART_MAX_PORTS
,
66 #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
67 .cons
= &liteuart_console
,
71 static void liteuart_update_irq_reg(struct uart_port
*port
, bool set
, u8 mask
)
73 struct liteuart_port
*uart
= to_liteuart_port(port
);
76 uart
->irq_reg
|= mask
;
78 uart
->irq_reg
&= ~mask
;
81 litex_write8(port
->membase
+ OFF_EV_ENABLE
, uart
->irq_reg
);
84 static void liteuart_stop_tx(struct uart_port
*port
)
86 liteuart_update_irq_reg(port
, false, EV_TX
);
89 static void liteuart_start_tx(struct uart_port
*port
)
91 liteuart_update_irq_reg(port
, true, EV_TX
);
94 static void liteuart_stop_rx(struct uart_port
*port
)
96 struct liteuart_port
*uart
= to_liteuart_port(port
);
98 /* just delete timer */
99 del_timer(&uart
->timer
);
102 static void liteuart_rx_chars(struct uart_port
*port
)
104 unsigned char __iomem
*membase
= port
->membase
;
107 while (!litex_read8(membase
+ OFF_RXEMPTY
)) {
108 ch
= litex_read8(membase
+ OFF_RXTX
);
111 /* necessary for RXEMPTY to refresh its value */
112 litex_write8(membase
+ OFF_EV_PENDING
, EV_RX
);
114 /* no overflow bits in status */
115 if (!(uart_handle_sysrq_char(port
, ch
)))
116 uart_insert_char(port
, 1, 0, ch
, TTY_NORMAL
);
119 tty_flip_buffer_push(&port
->state
->port
);
122 static void liteuart_tx_chars(struct uart_port
*port
)
126 uart_port_tx(port
, ch
,
127 !litex_read8(port
->membase
+ OFF_TXFULL
),
128 litex_write8(port
->membase
+ OFF_RXTX
, ch
));
131 static irqreturn_t
liteuart_interrupt(int irq
, void *data
)
133 struct liteuart_port
*uart
= data
;
134 struct uart_port
*port
= &uart
->port
;
139 * if polling, the context would be "in_serving_softirq", so use
140 * irq[save|restore] spin_lock variants to cover all possibilities
142 uart_port_lock_irqsave(port
, &flags
);
143 isr
= litex_read8(port
->membase
+ OFF_EV_PENDING
) & uart
->irq_reg
;
145 liteuart_rx_chars(port
);
147 liteuart_tx_chars(port
);
148 uart_port_unlock_irqrestore(port
, flags
);
150 return IRQ_RETVAL(isr
);
153 static void liteuart_timer(struct timer_list
*t
)
155 struct liteuart_port
*uart
= from_timer(uart
, t
, timer
);
156 struct uart_port
*port
= &uart
->port
;
158 liteuart_interrupt(0, port
);
159 mod_timer(&uart
->timer
, jiffies
+ uart_poll_timeout(port
));
162 static unsigned int liteuart_tx_empty(struct uart_port
*port
)
164 /* not really tx empty, just checking if tx is not full */
165 if (!litex_read8(port
->membase
+ OFF_TXFULL
))
171 static void liteuart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
173 /* modem control register is not present in LiteUART */
176 static unsigned int liteuart_get_mctrl(struct uart_port
*port
)
178 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
181 static int liteuart_startup(struct uart_port
*port
)
183 struct liteuart_port
*uart
= to_liteuart_port(port
);
188 ret
= request_irq(port
->irq
, liteuart_interrupt
, 0,
189 KBUILD_MODNAME
, uart
);
192 "line %d irq %d failed: switch to polling\n",
193 port
->line
, port
->irq
);
198 uart_port_lock_irqsave(port
, &flags
);
199 /* only enabling rx irqs during startup */
200 liteuart_update_irq_reg(port
, true, EV_RX
);
201 uart_port_unlock_irqrestore(port
, flags
);
204 timer_setup(&uart
->timer
, liteuart_timer
, 0);
205 mod_timer(&uart
->timer
, jiffies
+ uart_poll_timeout(port
));
211 static void liteuart_shutdown(struct uart_port
*port
)
213 struct liteuart_port
*uart
= to_liteuart_port(port
);
216 uart_port_lock_irqsave(port
, &flags
);
217 liteuart_update_irq_reg(port
, false, EV_RX
| EV_TX
);
218 uart_port_unlock_irqrestore(port
, flags
);
221 free_irq(port
->irq
, port
);
223 del_timer_sync(&uart
->timer
);
226 static void liteuart_set_termios(struct uart_port
*port
, struct ktermios
*new,
227 const struct ktermios
*old
)
232 uart_port_lock_irqsave(port
, &flags
);
234 /* update baudrate */
235 baud
= uart_get_baud_rate(port
, new, old
, 0, 460800);
236 uart_update_timeout(port
, new->c_cflag
, baud
);
238 uart_port_unlock_irqrestore(port
, flags
);
241 static const char *liteuart_type(struct uart_port
*port
)
246 static void liteuart_config_port(struct uart_port
*port
, int flags
)
249 * Driver core for serial ports forces a non-zero value for port type.
250 * Write an arbitrary value here to accommodate the serial core driver,
251 * as ID part of UAPI is redundant.
256 static int liteuart_verify_port(struct uart_port
*port
,
257 struct serial_struct
*ser
)
259 if (port
->type
!= PORT_UNKNOWN
&& ser
->type
!= 1)
265 static const struct uart_ops liteuart_ops
= {
266 .tx_empty
= liteuart_tx_empty
,
267 .set_mctrl
= liteuart_set_mctrl
,
268 .get_mctrl
= liteuart_get_mctrl
,
269 .stop_tx
= liteuart_stop_tx
,
270 .start_tx
= liteuart_start_tx
,
271 .stop_rx
= liteuart_stop_rx
,
272 .startup
= liteuart_startup
,
273 .shutdown
= liteuart_shutdown
,
274 .set_termios
= liteuart_set_termios
,
275 .type
= liteuart_type
,
276 .config_port
= liteuart_config_port
,
277 .verify_port
= liteuart_verify_port
,
280 static int liteuart_probe(struct platform_device
*pdev
)
282 struct liteuart_port
*uart
;
283 struct uart_port
*port
;
284 struct xa_limit limit
;
287 uart
= devm_kzalloc(&pdev
->dev
, sizeof(struct liteuart_port
), GFP_KERNEL
);
294 port
->membase
= devm_platform_get_and_ioremap_resource(pdev
, 0, NULL
);
295 if (IS_ERR(port
->membase
))
296 return PTR_ERR(port
->membase
);
298 ret
= platform_get_irq_optional(pdev
, 0);
299 if (ret
< 0 && ret
!= -ENXIO
)
304 /* look for aliases; auto-enumerate for free index if not found */
305 dev_id
= of_alias_get_id(pdev
->dev
.of_node
, "serial");
307 limit
= XA_LIMIT(0, CONFIG_SERIAL_LITEUART_MAX_PORTS
);
309 limit
= XA_LIMIT(dev_id
, dev_id
);
311 ret
= xa_alloc(&liteuart_array
, &dev_id
, uart
, limit
, GFP_KERNEL
);
315 /* values not from device tree */
316 port
->dev
= &pdev
->dev
;
317 port
->iotype
= UPIO_MEM
;
318 port
->flags
= UPF_BOOT_AUTOCONF
;
319 port
->ops
= &liteuart_ops
;
321 port
->type
= PORT_UNKNOWN
;
323 spin_lock_init(&port
->lock
);
325 platform_set_drvdata(pdev
, port
);
327 ret
= uart_add_one_port(&liteuart_driver
, &uart
->port
);
334 xa_erase(&liteuart_array
, dev_id
);
339 static void liteuart_remove(struct platform_device
*pdev
)
341 struct uart_port
*port
= platform_get_drvdata(pdev
);
342 unsigned int line
= port
->line
;
344 uart_remove_one_port(&liteuart_driver
, port
);
345 xa_erase(&liteuart_array
, line
);
348 static const struct of_device_id liteuart_of_match
[] = {
349 { .compatible
= "litex,liteuart" },
352 MODULE_DEVICE_TABLE(of
, liteuart_of_match
);
354 static struct platform_driver liteuart_platform_driver
= {
355 .probe
= liteuart_probe
,
356 .remove
= liteuart_remove
,
358 .name
= KBUILD_MODNAME
,
359 .of_match_table
= liteuart_of_match
,
363 #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
365 static void liteuart_putchar(struct uart_port
*port
, unsigned char ch
)
367 while (litex_read8(port
->membase
+ OFF_TXFULL
))
370 litex_write8(port
->membase
+ OFF_RXTX
, ch
);
373 static void liteuart_console_write(struct console
*co
, const char *s
,
376 struct liteuart_port
*uart
;
377 struct uart_port
*port
;
380 uart
= (struct liteuart_port
*)xa_load(&liteuart_array
, co
->index
);
383 uart_port_lock_irqsave(port
, &flags
);
384 uart_console_write(port
, s
, count
, liteuart_putchar
);
385 uart_port_unlock_irqrestore(port
, flags
);
388 static int liteuart_console_setup(struct console
*co
, char *options
)
390 struct liteuart_port
*uart
;
391 struct uart_port
*port
;
397 uart
= (struct liteuart_port
*)xa_load(&liteuart_array
, co
->index
);
406 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
408 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
411 static struct console liteuart_console
= {
412 .name
= KBUILD_MODNAME
,
413 .write
= liteuart_console_write
,
414 .device
= uart_console_device
,
415 .setup
= liteuart_console_setup
,
416 .flags
= CON_PRINTBUFFER
,
418 .data
= &liteuart_driver
,
421 static int __init
liteuart_console_init(void)
423 register_console(&liteuart_console
);
427 console_initcall(liteuart_console_init
);
429 static void early_liteuart_write(struct console
*console
, const char *s
,
432 struct earlycon_device
*device
= console
->data
;
433 struct uart_port
*port
= &device
->port
;
435 uart_console_write(port
, s
, count
, liteuart_putchar
);
438 static int __init
early_liteuart_setup(struct earlycon_device
*device
,
441 if (!device
->port
.membase
)
444 device
->con
->write
= early_liteuart_write
;
448 OF_EARLYCON_DECLARE(liteuart
, "litex,liteuart", early_liteuart_setup
);
449 #endif /* CONFIG_SERIAL_LITEUART_CONSOLE */
451 static int __init
liteuart_init(void)
455 res
= uart_register_driver(&liteuart_driver
);
459 res
= platform_driver_register(&liteuart_platform_driver
);
461 uart_unregister_driver(&liteuart_driver
);
466 static void __exit
liteuart_exit(void)
468 platform_driver_unregister(&liteuart_platform_driver
);
469 uart_unregister_driver(&liteuart_driver
);
472 module_init(liteuart_init
);
473 module_exit(liteuart_exit
);
475 MODULE_AUTHOR("Antmicro <www.antmicro.com>");
476 MODULE_DESCRIPTION("LiteUART serial driver");
477 MODULE_LICENSE("GPL v2");
478 MODULE_ALIAS("platform:liteuart");