2 * Atheros AR933X SoC built-in UART driver
4 * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/ioport.h>
15 #include <linux/init.h>
16 #include <linux/console.h>
17 #include <linux/sysrq.h>
18 #include <linux/delay.h>
19 #include <linux/platform_device.h>
21 #include <linux/of_platform.h>
22 #include <linux/tty.h>
23 #include <linux/tty_flip.h>
24 #include <linux/serial_core.h>
25 #include <linux/serial.h>
26 #include <linux/slab.h>
28 #include <linux/irq.h>
29 #include <linux/clk.h>
31 #include <asm/div64.h>
33 #include <asm/mach-ath79/ar933x_uart.h>
35 #define DRIVER_NAME "ar933x-uart"
37 #define AR933X_UART_MAX_SCALE 0xff
38 #define AR933X_UART_MAX_STEP 0xffff
40 #define AR933X_UART_MIN_BAUD 300
41 #define AR933X_UART_MAX_BAUD 3000000
43 #define AR933X_DUMMY_STATUS_RD 0x01
45 static struct uart_driver ar933x_uart_driver
;
47 struct ar933x_uart_port
{
48 struct uart_port port
;
49 unsigned int ier
; /* shadow Interrupt Enable Register */
50 unsigned int min_baud
;
51 unsigned int max_baud
;
55 static inline bool ar933x_uart_console_enabled(void)
57 return config_enabled(CONFIG_SERIAL_AR933X_CONSOLE
);
60 static inline unsigned int ar933x_uart_read(struct ar933x_uart_port
*up
,
63 return readl(up
->port
.membase
+ offset
);
66 static inline void ar933x_uart_write(struct ar933x_uart_port
*up
,
67 int offset
, unsigned int value
)
69 writel(value
, up
->port
.membase
+ offset
);
72 static inline void ar933x_uart_rmw(struct ar933x_uart_port
*up
,
79 t
= ar933x_uart_read(up
, offset
);
82 ar933x_uart_write(up
, offset
, t
);
85 static inline void ar933x_uart_rmw_set(struct ar933x_uart_port
*up
,
89 ar933x_uart_rmw(up
, offset
, 0, val
);
92 static inline void ar933x_uart_rmw_clear(struct ar933x_uart_port
*up
,
96 ar933x_uart_rmw(up
, offset
, val
, 0);
99 static inline void ar933x_uart_start_tx_interrupt(struct ar933x_uart_port
*up
)
101 up
->ier
|= AR933X_UART_INT_TX_EMPTY
;
102 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
105 static inline void ar933x_uart_stop_tx_interrupt(struct ar933x_uart_port
*up
)
107 up
->ier
&= ~AR933X_UART_INT_TX_EMPTY
;
108 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
111 static inline void ar933x_uart_putc(struct ar933x_uart_port
*up
, int ch
)
115 rdata
= ch
& AR933X_UART_DATA_TX_RX_MASK
;
116 rdata
|= AR933X_UART_DATA_TX_CSR
;
117 ar933x_uart_write(up
, AR933X_UART_DATA_REG
, rdata
);
120 static unsigned int ar933x_uart_tx_empty(struct uart_port
*port
)
122 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
126 spin_lock_irqsave(&up
->port
.lock
, flags
);
127 rdata
= ar933x_uart_read(up
, AR933X_UART_DATA_REG
);
128 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
130 return (rdata
& AR933X_UART_DATA_TX_CSR
) ? 0 : TIOCSER_TEMT
;
133 static unsigned int ar933x_uart_get_mctrl(struct uart_port
*port
)
138 static void ar933x_uart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
142 static void ar933x_uart_start_tx(struct uart_port
*port
)
144 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
146 ar933x_uart_start_tx_interrupt(up
);
149 static void ar933x_uart_stop_tx(struct uart_port
*port
)
151 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
153 ar933x_uart_stop_tx_interrupt(up
);
156 static void ar933x_uart_stop_rx(struct uart_port
*port
)
158 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
160 up
->ier
&= ~AR933X_UART_INT_RX_VALID
;
161 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
164 static void ar933x_uart_break_ctl(struct uart_port
*port
, int break_state
)
166 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
169 spin_lock_irqsave(&up
->port
.lock
, flags
);
170 if (break_state
== -1)
171 ar933x_uart_rmw_set(up
, AR933X_UART_CS_REG
,
172 AR933X_UART_CS_TX_BREAK
);
174 ar933x_uart_rmw_clear(up
, AR933X_UART_CS_REG
,
175 AR933X_UART_CS_TX_BREAK
);
176 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
179 static void ar933x_uart_enable_ms(struct uart_port
*port
)
184 * baudrate = (clk / (scale + 1)) * (step * (1 / 2^17))
186 static unsigned long ar933x_uart_get_baud(unsigned int clk
,
193 div
= (2 << 16) * (scale
+ 1);
202 static void ar933x_uart_get_scale_step(unsigned int clk
,
214 for (tscale
= 0; tscale
< AR933X_UART_MAX_SCALE
; tscale
++) {
218 tstep
= baud
* (tscale
+ 1);
222 if (tstep
> AR933X_UART_MAX_STEP
)
225 diff
= abs(ar933x_uart_get_baud(clk
, tscale
, tstep
) - baud
);
226 if (diff
< min_diff
) {
234 static void ar933x_uart_set_termios(struct uart_port
*port
,
235 struct ktermios
*new,
236 struct ktermios
*old
)
238 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
241 unsigned int baud
, scale
, step
;
243 /* Only CS8 is supported */
244 new->c_cflag
&= ~CSIZE
;
247 /* Only one stop bit is supported */
248 new->c_cflag
&= ~CSTOPB
;
251 if (new->c_cflag
& PARENB
) {
252 if (!(new->c_cflag
& PARODD
))
253 cs
|= AR933X_UART_CS_PARITY_EVEN
;
255 cs
|= AR933X_UART_CS_PARITY_ODD
;
257 cs
|= AR933X_UART_CS_PARITY_NONE
;
260 /* Mark/space parity is not supported */
261 new->c_cflag
&= ~CMSPAR
;
263 baud
= uart_get_baud_rate(port
, new, old
, up
->min_baud
, up
->max_baud
);
264 ar933x_uart_get_scale_step(port
->uartclk
, baud
, &scale
, &step
);
267 * Ok, we're now changing the port state. Do it with
268 * interrupts disabled.
270 spin_lock_irqsave(&up
->port
.lock
, flags
);
272 /* disable the UART */
273 ar933x_uart_rmw_clear(up
, AR933X_UART_CS_REG
,
274 AR933X_UART_CS_IF_MODE_M
<< AR933X_UART_CS_IF_MODE_S
);
276 /* Update the per-port timeout. */
277 uart_update_timeout(port
, new->c_cflag
, baud
);
279 up
->port
.ignore_status_mask
= 0;
281 /* ignore all characters if CREAD is not set */
282 if ((new->c_cflag
& CREAD
) == 0)
283 up
->port
.ignore_status_mask
|= AR933X_DUMMY_STATUS_RD
;
285 ar933x_uart_write(up
, AR933X_UART_CLOCK_REG
,
286 scale
<< AR933X_UART_CLOCK_SCALE_S
| step
);
288 /* setup configuration register */
289 ar933x_uart_rmw(up
, AR933X_UART_CS_REG
, AR933X_UART_CS_PARITY_M
, cs
);
291 /* enable host interrupt */
292 ar933x_uart_rmw_set(up
, AR933X_UART_CS_REG
,
293 AR933X_UART_CS_HOST_INT_EN
);
295 /* reenable the UART */
296 ar933x_uart_rmw(up
, AR933X_UART_CS_REG
,
297 AR933X_UART_CS_IF_MODE_M
<< AR933X_UART_CS_IF_MODE_S
,
298 AR933X_UART_CS_IF_MODE_DCE
<< AR933X_UART_CS_IF_MODE_S
);
300 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
302 if (tty_termios_baud_rate(new))
303 tty_termios_encode_baud_rate(new, baud
, baud
);
306 static void ar933x_uart_rx_chars(struct ar933x_uart_port
*up
)
308 struct tty_port
*port
= &up
->port
.state
->port
;
315 rdata
= ar933x_uart_read(up
, AR933X_UART_DATA_REG
);
316 if ((rdata
& AR933X_UART_DATA_RX_CSR
) == 0)
319 /* remove the character from the FIFO */
320 ar933x_uart_write(up
, AR933X_UART_DATA_REG
,
321 AR933X_UART_DATA_RX_CSR
);
323 up
->port
.icount
.rx
++;
324 ch
= rdata
& AR933X_UART_DATA_TX_RX_MASK
;
326 if (uart_handle_sysrq_char(&up
->port
, ch
))
329 if ((up
->port
.ignore_status_mask
& AR933X_DUMMY_STATUS_RD
) == 0)
330 tty_insert_flip_char(port
, ch
, TTY_NORMAL
);
331 } while (max_count
-- > 0);
333 spin_unlock(&up
->port
.lock
);
334 tty_flip_buffer_push(port
);
335 spin_lock(&up
->port
.lock
);
338 static void ar933x_uart_tx_chars(struct ar933x_uart_port
*up
)
340 struct circ_buf
*xmit
= &up
->port
.state
->xmit
;
343 if (uart_tx_stopped(&up
->port
))
346 count
= up
->port
.fifosize
;
350 rdata
= ar933x_uart_read(up
, AR933X_UART_DATA_REG
);
351 if ((rdata
& AR933X_UART_DATA_TX_CSR
) == 0)
354 if (up
->port
.x_char
) {
355 ar933x_uart_putc(up
, up
->port
.x_char
);
356 up
->port
.icount
.tx
++;
361 if (uart_circ_empty(xmit
))
364 ar933x_uart_putc(up
, xmit
->buf
[xmit
->tail
]);
366 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
367 up
->port
.icount
.tx
++;
368 } while (--count
> 0);
370 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
371 uart_write_wakeup(&up
->port
);
373 if (!uart_circ_empty(xmit
))
374 ar933x_uart_start_tx_interrupt(up
);
377 static irqreturn_t
ar933x_uart_interrupt(int irq
, void *dev_id
)
379 struct ar933x_uart_port
*up
= dev_id
;
382 status
= ar933x_uart_read(up
, AR933X_UART_CS_REG
);
383 if ((status
& AR933X_UART_CS_HOST_INT
) == 0)
386 spin_lock(&up
->port
.lock
);
388 status
= ar933x_uart_read(up
, AR933X_UART_INT_REG
);
389 status
&= ar933x_uart_read(up
, AR933X_UART_INT_EN_REG
);
391 if (status
& AR933X_UART_INT_RX_VALID
) {
392 ar933x_uart_write(up
, AR933X_UART_INT_REG
,
393 AR933X_UART_INT_RX_VALID
);
394 ar933x_uart_rx_chars(up
);
397 if (status
& AR933X_UART_INT_TX_EMPTY
) {
398 ar933x_uart_write(up
, AR933X_UART_INT_REG
,
399 AR933X_UART_INT_TX_EMPTY
);
400 ar933x_uart_stop_tx_interrupt(up
);
401 ar933x_uart_tx_chars(up
);
404 spin_unlock(&up
->port
.lock
);
409 static int ar933x_uart_startup(struct uart_port
*port
)
411 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
415 ret
= request_irq(up
->port
.irq
, ar933x_uart_interrupt
,
416 up
->port
.irqflags
, dev_name(up
->port
.dev
), up
);
420 spin_lock_irqsave(&up
->port
.lock
, flags
);
422 /* Enable HOST interrupts */
423 ar933x_uart_rmw_set(up
, AR933X_UART_CS_REG
,
424 AR933X_UART_CS_HOST_INT_EN
);
426 /* Enable RX interrupts */
427 up
->ier
= AR933X_UART_INT_RX_VALID
;
428 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
430 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
435 static void ar933x_uart_shutdown(struct uart_port
*port
)
437 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
439 /* Disable all interrupts */
441 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
443 /* Disable break condition */
444 ar933x_uart_rmw_clear(up
, AR933X_UART_CS_REG
,
445 AR933X_UART_CS_TX_BREAK
);
447 free_irq(up
->port
.irq
, up
);
450 static const char *ar933x_uart_type(struct uart_port
*port
)
452 return (port
->type
== PORT_AR933X
) ? "AR933X UART" : NULL
;
455 static void ar933x_uart_release_port(struct uart_port
*port
)
457 /* Nothing to release ... */
460 static int ar933x_uart_request_port(struct uart_port
*port
)
462 /* UARTs always present */
466 static void ar933x_uart_config_port(struct uart_port
*port
, int flags
)
468 if (flags
& UART_CONFIG_TYPE
)
469 port
->type
= PORT_AR933X
;
472 static int ar933x_uart_verify_port(struct uart_port
*port
,
473 struct serial_struct
*ser
)
475 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
477 if (ser
->type
!= PORT_UNKNOWN
&&
478 ser
->type
!= PORT_AR933X
)
481 if (ser
->irq
< 0 || ser
->irq
>= NR_IRQS
)
484 if (ser
->baud_base
< up
->min_baud
||
485 ser
->baud_base
> up
->max_baud
)
491 static struct uart_ops ar933x_uart_ops
= {
492 .tx_empty
= ar933x_uart_tx_empty
,
493 .set_mctrl
= ar933x_uart_set_mctrl
,
494 .get_mctrl
= ar933x_uart_get_mctrl
,
495 .stop_tx
= ar933x_uart_stop_tx
,
496 .start_tx
= ar933x_uart_start_tx
,
497 .stop_rx
= ar933x_uart_stop_rx
,
498 .enable_ms
= ar933x_uart_enable_ms
,
499 .break_ctl
= ar933x_uart_break_ctl
,
500 .startup
= ar933x_uart_startup
,
501 .shutdown
= ar933x_uart_shutdown
,
502 .set_termios
= ar933x_uart_set_termios
,
503 .type
= ar933x_uart_type
,
504 .release_port
= ar933x_uart_release_port
,
505 .request_port
= ar933x_uart_request_port
,
506 .config_port
= ar933x_uart_config_port
,
507 .verify_port
= ar933x_uart_verify_port
,
510 static struct ar933x_uart_port
*
511 ar933x_console_ports
[CONFIG_SERIAL_AR933X_NR_UARTS
];
513 static void ar933x_uart_wait_xmitr(struct ar933x_uart_port
*up
)
516 unsigned int timeout
= 60000;
518 /* Wait up to 60ms for the character(s) to be sent. */
520 status
= ar933x_uart_read(up
, AR933X_UART_DATA_REG
);
524 } while ((status
& AR933X_UART_DATA_TX_CSR
) == 0);
527 static void ar933x_uart_console_putchar(struct uart_port
*port
, int ch
)
529 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
531 ar933x_uart_wait_xmitr(up
);
532 ar933x_uart_putc(up
, ch
);
535 static void ar933x_uart_console_write(struct console
*co
, const char *s
,
538 struct ar933x_uart_port
*up
= ar933x_console_ports
[co
->index
];
543 local_irq_save(flags
);
547 else if (oops_in_progress
)
548 locked
= spin_trylock(&up
->port
.lock
);
550 spin_lock(&up
->port
.lock
);
553 * First save the IER then disable the interrupts
555 int_en
= ar933x_uart_read(up
, AR933X_UART_INT_EN_REG
);
556 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, 0);
558 uart_console_write(&up
->port
, s
, count
, ar933x_uart_console_putchar
);
561 * Finally, wait for transmitter to become empty
562 * and restore the IER
564 ar933x_uart_wait_xmitr(up
);
565 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, int_en
);
567 ar933x_uart_write(up
, AR933X_UART_INT_REG
, AR933X_UART_INT_ALLINTS
);
570 spin_unlock(&up
->port
.lock
);
572 local_irq_restore(flags
);
575 static int ar933x_uart_console_setup(struct console
*co
, char *options
)
577 struct ar933x_uart_port
*up
;
583 if (co
->index
< 0 || co
->index
>= CONFIG_SERIAL_AR933X_NR_UARTS
)
586 up
= ar933x_console_ports
[co
->index
];
591 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
593 return uart_set_options(&up
->port
, co
, baud
, parity
, bits
, flow
);
596 static struct console ar933x_uart_console
= {
598 .write
= ar933x_uart_console_write
,
599 .device
= uart_console_device
,
600 .setup
= ar933x_uart_console_setup
,
601 .flags
= CON_PRINTBUFFER
,
603 .data
= &ar933x_uart_driver
,
606 static void ar933x_uart_add_console_port(struct ar933x_uart_port
*up
)
608 if (!ar933x_uart_console_enabled())
611 ar933x_console_ports
[up
->port
.line
] = up
;
614 static struct uart_driver ar933x_uart_driver
= {
615 .owner
= THIS_MODULE
,
616 .driver_name
= DRIVER_NAME
,
617 .dev_name
= "ttyATH",
618 .nr
= CONFIG_SERIAL_AR933X_NR_UARTS
,
619 .cons
= NULL
, /* filled in runtime */
622 static int ar933x_uart_probe(struct platform_device
*pdev
)
624 struct ar933x_uart_port
*up
;
625 struct uart_port
*port
;
626 struct resource
*mem_res
;
627 struct resource
*irq_res
;
628 struct device_node
*np
;
633 np
= pdev
->dev
.of_node
;
634 if (config_enabled(CONFIG_OF
) && np
) {
635 id
= of_alias_get_id(np
, "serial");
637 dev_err(&pdev
->dev
, "unable to get alias id, err=%d\n",
647 if (id
> CONFIG_SERIAL_AR933X_NR_UARTS
)
650 irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
652 dev_err(&pdev
->dev
, "no IRQ resource\n");
656 up
= devm_kzalloc(&pdev
->dev
, sizeof(struct ar933x_uart_port
),
661 up
->clk
= devm_clk_get(&pdev
->dev
, "uart");
662 if (IS_ERR(up
->clk
)) {
663 dev_err(&pdev
->dev
, "unable to get UART clock\n");
664 return PTR_ERR(up
->clk
);
669 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
670 port
->membase
= devm_ioremap_resource(&pdev
->dev
, mem_res
);
671 if (IS_ERR(port
->membase
))
672 return PTR_ERR(port
->membase
);
674 ret
= clk_prepare_enable(up
->clk
);
678 port
->uartclk
= clk_get_rate(up
->clk
);
679 if (!port
->uartclk
) {
681 goto err_disable_clk
;
684 port
->mapbase
= mem_res
->start
;
686 port
->irq
= irq_res
->start
;
687 port
->dev
= &pdev
->dev
;
688 port
->type
= PORT_AR933X
;
689 port
->iotype
= UPIO_MEM32
;
692 port
->fifosize
= AR933X_UART_FIFO_SIZE
;
693 port
->ops
= &ar933x_uart_ops
;
695 baud
= ar933x_uart_get_baud(port
->uartclk
, AR933X_UART_MAX_SCALE
, 1);
696 up
->min_baud
= max_t(unsigned int, baud
, AR933X_UART_MIN_BAUD
);
698 baud
= ar933x_uart_get_baud(port
->uartclk
, 0, AR933X_UART_MAX_STEP
);
699 up
->max_baud
= min_t(unsigned int, baud
, AR933X_UART_MAX_BAUD
);
701 ar933x_uart_add_console_port(up
);
703 ret
= uart_add_one_port(&ar933x_uart_driver
, &up
->port
);
705 goto err_disable_clk
;
707 platform_set_drvdata(pdev
, up
);
711 clk_disable_unprepare(up
->clk
);
715 static int ar933x_uart_remove(struct platform_device
*pdev
)
717 struct ar933x_uart_port
*up
;
719 up
= platform_get_drvdata(pdev
);
722 uart_remove_one_port(&ar933x_uart_driver
, &up
->port
);
723 clk_disable_unprepare(up
->clk
);
730 static const struct of_device_id ar933x_uart_of_ids
[] = {
731 { .compatible
= "qca,ar9330-uart" },
734 MODULE_DEVICE_TABLE(of
, ar933x_uart_of_ids
);
737 static struct platform_driver ar933x_uart_platform_driver
= {
738 .probe
= ar933x_uart_probe
,
739 .remove
= ar933x_uart_remove
,
742 .owner
= THIS_MODULE
,
743 .of_match_table
= of_match_ptr(ar933x_uart_of_ids
),
747 static int __init
ar933x_uart_init(void)
751 if (ar933x_uart_console_enabled())
752 ar933x_uart_driver
.cons
= &ar933x_uart_console
;
754 ret
= uart_register_driver(&ar933x_uart_driver
);
758 ret
= platform_driver_register(&ar933x_uart_platform_driver
);
760 goto err_unregister_uart_driver
;
764 err_unregister_uart_driver
:
765 uart_unregister_driver(&ar933x_uart_driver
);
770 static void __exit
ar933x_uart_exit(void)
772 platform_driver_unregister(&ar933x_uart_platform_driver
);
773 uart_unregister_driver(&ar933x_uart_driver
);
776 module_init(ar933x_uart_init
);
777 module_exit(ar933x_uart_exit
);
779 MODULE_DESCRIPTION("Atheros AR933X UART driver");
780 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
781 MODULE_LICENSE("GPL v2");
782 MODULE_ALIAS("platform:" DRIVER_NAME
);