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>
20 #include <linux/tty.h>
21 #include <linux/tty_flip.h>
22 #include <linux/serial_core.h>
23 #include <linux/serial.h>
24 #include <linux/slab.h>
26 #include <linux/irq.h>
28 #include <asm/div64.h>
30 #include <asm/mach-ath79/ar933x_uart.h>
31 #include <asm/mach-ath79/ar933x_uart_platform.h>
33 #define DRIVER_NAME "ar933x-uart"
35 #define AR933X_UART_MAX_SCALE 0xff
36 #define AR933X_UART_MAX_STEP 0xffff
38 #define AR933X_UART_MIN_BAUD 300
39 #define AR933X_UART_MAX_BAUD 3000000
41 #define AR933X_DUMMY_STATUS_RD 0x01
43 static struct uart_driver ar933x_uart_driver
;
45 struct ar933x_uart_port
{
46 struct uart_port port
;
47 unsigned int ier
; /* shadow Interrupt Enable Register */
48 unsigned int min_baud
;
49 unsigned int max_baud
;
52 static inline unsigned int ar933x_uart_read(struct ar933x_uart_port
*up
,
55 return readl(up
->port
.membase
+ offset
);
58 static inline void ar933x_uart_write(struct ar933x_uart_port
*up
,
59 int offset
, unsigned int value
)
61 writel(value
, up
->port
.membase
+ offset
);
64 static inline void ar933x_uart_rmw(struct ar933x_uart_port
*up
,
71 t
= ar933x_uart_read(up
, offset
);
74 ar933x_uart_write(up
, offset
, t
);
77 static inline void ar933x_uart_rmw_set(struct ar933x_uart_port
*up
,
81 ar933x_uart_rmw(up
, offset
, 0, val
);
84 static inline void ar933x_uart_rmw_clear(struct ar933x_uart_port
*up
,
88 ar933x_uart_rmw(up
, offset
, val
, 0);
91 static inline void ar933x_uart_start_tx_interrupt(struct ar933x_uart_port
*up
)
93 up
->ier
|= AR933X_UART_INT_TX_EMPTY
;
94 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
97 static inline void ar933x_uart_stop_tx_interrupt(struct ar933x_uart_port
*up
)
99 up
->ier
&= ~AR933X_UART_INT_TX_EMPTY
;
100 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
103 static inline void ar933x_uart_putc(struct ar933x_uart_port
*up
, int ch
)
107 rdata
= ch
& AR933X_UART_DATA_TX_RX_MASK
;
108 rdata
|= AR933X_UART_DATA_TX_CSR
;
109 ar933x_uart_write(up
, AR933X_UART_DATA_REG
, rdata
);
112 static unsigned int ar933x_uart_tx_empty(struct uart_port
*port
)
114 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
118 spin_lock_irqsave(&up
->port
.lock
, flags
);
119 rdata
= ar933x_uart_read(up
, AR933X_UART_DATA_REG
);
120 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
122 return (rdata
& AR933X_UART_DATA_TX_CSR
) ? 0 : TIOCSER_TEMT
;
125 static unsigned int ar933x_uart_get_mctrl(struct uart_port
*port
)
130 static void ar933x_uart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
134 static void ar933x_uart_start_tx(struct uart_port
*port
)
136 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
138 ar933x_uart_start_tx_interrupt(up
);
141 static void ar933x_uart_stop_tx(struct uart_port
*port
)
143 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
145 ar933x_uart_stop_tx_interrupt(up
);
148 static void ar933x_uart_stop_rx(struct uart_port
*port
)
150 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
152 up
->ier
&= ~AR933X_UART_INT_RX_VALID
;
153 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
156 static void ar933x_uart_break_ctl(struct uart_port
*port
, int break_state
)
158 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
161 spin_lock_irqsave(&up
->port
.lock
, flags
);
162 if (break_state
== -1)
163 ar933x_uart_rmw_set(up
, AR933X_UART_CS_REG
,
164 AR933X_UART_CS_TX_BREAK
);
166 ar933x_uart_rmw_clear(up
, AR933X_UART_CS_REG
,
167 AR933X_UART_CS_TX_BREAK
);
168 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
171 static void ar933x_uart_enable_ms(struct uart_port
*port
)
176 * baudrate = (clk / (scale + 1)) * (step * (1 / 2^17))
178 static unsigned long ar933x_uart_get_baud(unsigned int clk
,
185 div
= (2 << 16) * (scale
+ 1);
194 static void ar933x_uart_get_scale_step(unsigned int clk
,
206 for (tscale
= 0; tscale
< AR933X_UART_MAX_SCALE
; tscale
++) {
210 tstep
= baud
* (tscale
+ 1);
214 if (tstep
> AR933X_UART_MAX_STEP
)
217 diff
= abs(ar933x_uart_get_baud(clk
, tscale
, tstep
) - baud
);
218 if (diff
< min_diff
) {
226 static void ar933x_uart_set_termios(struct uart_port
*port
,
227 struct ktermios
*new,
228 struct ktermios
*old
)
230 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
233 unsigned int baud
, scale
, step
;
235 /* Only CS8 is supported */
236 new->c_cflag
&= ~CSIZE
;
239 /* Only one stop bit is supported */
240 new->c_cflag
&= ~CSTOPB
;
243 if (new->c_cflag
& PARENB
) {
244 if (!(new->c_cflag
& PARODD
))
245 cs
|= AR933X_UART_CS_PARITY_EVEN
;
247 cs
|= AR933X_UART_CS_PARITY_ODD
;
249 cs
|= AR933X_UART_CS_PARITY_NONE
;
252 /* Mark/space parity is not supported */
253 new->c_cflag
&= ~CMSPAR
;
255 baud
= uart_get_baud_rate(port
, new, old
, up
->min_baud
, up
->max_baud
);
256 ar933x_uart_get_scale_step(port
->uartclk
, baud
, &scale
, &step
);
259 * Ok, we're now changing the port state. Do it with
260 * interrupts disabled.
262 spin_lock_irqsave(&up
->port
.lock
, flags
);
264 /* disable the UART */
265 ar933x_uart_rmw_clear(up
, AR933X_UART_CS_REG
,
266 AR933X_UART_CS_IF_MODE_M
<< AR933X_UART_CS_IF_MODE_S
);
268 /* Update the per-port timeout. */
269 uart_update_timeout(port
, new->c_cflag
, baud
);
271 up
->port
.ignore_status_mask
= 0;
273 /* ignore all characters if CREAD is not set */
274 if ((new->c_cflag
& CREAD
) == 0)
275 up
->port
.ignore_status_mask
|= AR933X_DUMMY_STATUS_RD
;
277 ar933x_uart_write(up
, AR933X_UART_CLOCK_REG
,
278 scale
<< AR933X_UART_CLOCK_SCALE_S
| step
);
280 /* setup configuration register */
281 ar933x_uart_rmw(up
, AR933X_UART_CS_REG
, AR933X_UART_CS_PARITY_M
, cs
);
283 /* enable host interrupt */
284 ar933x_uart_rmw_set(up
, AR933X_UART_CS_REG
,
285 AR933X_UART_CS_HOST_INT_EN
);
287 /* reenable the UART */
288 ar933x_uart_rmw(up
, AR933X_UART_CS_REG
,
289 AR933X_UART_CS_IF_MODE_M
<< AR933X_UART_CS_IF_MODE_S
,
290 AR933X_UART_CS_IF_MODE_DCE
<< AR933X_UART_CS_IF_MODE_S
);
292 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
294 if (tty_termios_baud_rate(new))
295 tty_termios_encode_baud_rate(new, baud
, baud
);
298 static void ar933x_uart_rx_chars(struct ar933x_uart_port
*up
)
300 struct tty_struct
*tty
;
303 tty
= tty_port_tty_get(&up
->port
.state
->port
);
308 rdata
= ar933x_uart_read(up
, AR933X_UART_DATA_REG
);
309 if ((rdata
& AR933X_UART_DATA_RX_CSR
) == 0)
312 /* remove the character from the FIFO */
313 ar933x_uart_write(up
, AR933X_UART_DATA_REG
,
314 AR933X_UART_DATA_RX_CSR
);
317 /* discard the data if no tty available */
321 up
->port
.icount
.rx
++;
322 ch
= rdata
& AR933X_UART_DATA_TX_RX_MASK
;
324 if (uart_handle_sysrq_char(&up
->port
, ch
))
327 if ((up
->port
.ignore_status_mask
& AR933X_DUMMY_STATUS_RD
) == 0)
328 tty_insert_flip_char(tty
, ch
, TTY_NORMAL
);
329 } while (max_count
-- > 0);
332 tty_flip_buffer_push(tty
);
337 static void ar933x_uart_tx_chars(struct ar933x_uart_port
*up
)
339 struct circ_buf
*xmit
= &up
->port
.state
->xmit
;
342 if (uart_tx_stopped(&up
->port
))
345 count
= up
->port
.fifosize
;
349 rdata
= ar933x_uart_read(up
, AR933X_UART_DATA_REG
);
350 if ((rdata
& AR933X_UART_DATA_TX_CSR
) == 0)
353 if (up
->port
.x_char
) {
354 ar933x_uart_putc(up
, up
->port
.x_char
);
355 up
->port
.icount
.tx
++;
360 if (uart_circ_empty(xmit
))
363 ar933x_uart_putc(up
, xmit
->buf
[xmit
->tail
]);
365 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
366 up
->port
.icount
.tx
++;
367 } while (--count
> 0);
369 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
370 uart_write_wakeup(&up
->port
);
372 if (!uart_circ_empty(xmit
))
373 ar933x_uart_start_tx_interrupt(up
);
376 static irqreturn_t
ar933x_uart_interrupt(int irq
, void *dev_id
)
378 struct ar933x_uart_port
*up
= dev_id
;
381 status
= ar933x_uart_read(up
, AR933X_UART_CS_REG
);
382 if ((status
& AR933X_UART_CS_HOST_INT
) == 0)
385 spin_lock(&up
->port
.lock
);
387 status
= ar933x_uart_read(up
, AR933X_UART_INT_REG
);
388 status
&= ar933x_uart_read(up
, AR933X_UART_INT_EN_REG
);
390 if (status
& AR933X_UART_INT_RX_VALID
) {
391 ar933x_uart_write(up
, AR933X_UART_INT_REG
,
392 AR933X_UART_INT_RX_VALID
);
393 ar933x_uart_rx_chars(up
);
396 if (status
& AR933X_UART_INT_TX_EMPTY
) {
397 ar933x_uart_write(up
, AR933X_UART_INT_REG
,
398 AR933X_UART_INT_TX_EMPTY
);
399 ar933x_uart_stop_tx_interrupt(up
);
400 ar933x_uart_tx_chars(up
);
403 spin_unlock(&up
->port
.lock
);
408 static int ar933x_uart_startup(struct uart_port
*port
)
410 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
414 ret
= request_irq(up
->port
.irq
, ar933x_uart_interrupt
,
415 up
->port
.irqflags
, dev_name(up
->port
.dev
), up
);
419 spin_lock_irqsave(&up
->port
.lock
, flags
);
421 /* Enable HOST interrupts */
422 ar933x_uart_rmw_set(up
, AR933X_UART_CS_REG
,
423 AR933X_UART_CS_HOST_INT_EN
);
425 /* Enable RX interrupts */
426 up
->ier
= AR933X_UART_INT_RX_VALID
;
427 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
429 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
434 static void ar933x_uart_shutdown(struct uart_port
*port
)
436 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
438 /* Disable all interrupts */
440 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
442 /* Disable break condition */
443 ar933x_uart_rmw_clear(up
, AR933X_UART_CS_REG
,
444 AR933X_UART_CS_TX_BREAK
);
446 free_irq(up
->port
.irq
, up
);
449 static const char *ar933x_uart_type(struct uart_port
*port
)
451 return (port
->type
== PORT_AR933X
) ? "AR933X UART" : NULL
;
454 static void ar933x_uart_release_port(struct uart_port
*port
)
456 /* Nothing to release ... */
459 static int ar933x_uart_request_port(struct uart_port
*port
)
461 /* UARTs always present */
465 static void ar933x_uart_config_port(struct uart_port
*port
, int flags
)
467 if (flags
& UART_CONFIG_TYPE
)
468 port
->type
= PORT_AR933X
;
471 static int ar933x_uart_verify_port(struct uart_port
*port
,
472 struct serial_struct
*ser
)
474 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
476 if (ser
->type
!= PORT_UNKNOWN
&&
477 ser
->type
!= PORT_AR933X
)
480 if (ser
->irq
< 0 || ser
->irq
>= NR_IRQS
)
483 if (ser
->baud_base
< up
->min_baud
||
484 ser
->baud_base
> up
->max_baud
)
490 static struct uart_ops ar933x_uart_ops
= {
491 .tx_empty
= ar933x_uart_tx_empty
,
492 .set_mctrl
= ar933x_uart_set_mctrl
,
493 .get_mctrl
= ar933x_uart_get_mctrl
,
494 .stop_tx
= ar933x_uart_stop_tx
,
495 .start_tx
= ar933x_uart_start_tx
,
496 .stop_rx
= ar933x_uart_stop_rx
,
497 .enable_ms
= ar933x_uart_enable_ms
,
498 .break_ctl
= ar933x_uart_break_ctl
,
499 .startup
= ar933x_uart_startup
,
500 .shutdown
= ar933x_uart_shutdown
,
501 .set_termios
= ar933x_uart_set_termios
,
502 .type
= ar933x_uart_type
,
503 .release_port
= ar933x_uart_release_port
,
504 .request_port
= ar933x_uart_request_port
,
505 .config_port
= ar933x_uart_config_port
,
506 .verify_port
= ar933x_uart_verify_port
,
509 #ifdef CONFIG_SERIAL_AR933X_CONSOLE
511 static struct ar933x_uart_port
*
512 ar933x_console_ports
[CONFIG_SERIAL_AR933X_NR_UARTS
];
514 static void ar933x_uart_wait_xmitr(struct ar933x_uart_port
*up
)
517 unsigned int timeout
= 60000;
519 /* Wait up to 60ms for the character(s) to be sent. */
521 status
= ar933x_uart_read(up
, AR933X_UART_DATA_REG
);
525 } while ((status
& AR933X_UART_DATA_TX_CSR
) == 0);
528 static void ar933x_uart_console_putchar(struct uart_port
*port
, int ch
)
530 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
532 ar933x_uart_wait_xmitr(up
);
533 ar933x_uart_putc(up
, ch
);
536 static void ar933x_uart_console_write(struct console
*co
, const char *s
,
539 struct ar933x_uart_port
*up
= ar933x_console_ports
[co
->index
];
544 local_irq_save(flags
);
548 else if (oops_in_progress
)
549 locked
= spin_trylock(&up
->port
.lock
);
551 spin_lock(&up
->port
.lock
);
554 * First save the IER then disable the interrupts
556 int_en
= ar933x_uart_read(up
, AR933X_UART_INT_EN_REG
);
557 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, 0);
559 uart_console_write(&up
->port
, s
, count
, ar933x_uart_console_putchar
);
562 * Finally, wait for transmitter to become empty
563 * and restore the IER
565 ar933x_uart_wait_xmitr(up
);
566 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, int_en
);
568 ar933x_uart_write(up
, AR933X_UART_INT_REG
, AR933X_UART_INT_ALLINTS
);
571 spin_unlock(&up
->port
.lock
);
573 local_irq_restore(flags
);
576 static int ar933x_uart_console_setup(struct console
*co
, char *options
)
578 struct ar933x_uart_port
*up
;
584 if (co
->index
< 0 || co
->index
>= CONFIG_SERIAL_AR933X_NR_UARTS
)
587 up
= ar933x_console_ports
[co
->index
];
592 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
594 return uart_set_options(&up
->port
, co
, baud
, parity
, bits
, flow
);
597 static struct console ar933x_uart_console
= {
599 .write
= ar933x_uart_console_write
,
600 .device
= uart_console_device
,
601 .setup
= ar933x_uart_console_setup
,
602 .flags
= CON_PRINTBUFFER
,
604 .data
= &ar933x_uart_driver
,
607 static void ar933x_uart_add_console_port(struct ar933x_uart_port
*up
)
609 ar933x_console_ports
[up
->port
.line
] = up
;
612 #define AR933X_SERIAL_CONSOLE (&ar933x_uart_console)
616 static inline void ar933x_uart_add_console_port(struct ar933x_uart_port
*up
) {}
618 #define AR933X_SERIAL_CONSOLE NULL
620 #endif /* CONFIG_SERIAL_AR933X_CONSOLE */
622 static struct uart_driver ar933x_uart_driver
= {
623 .owner
= THIS_MODULE
,
624 .driver_name
= DRIVER_NAME
,
625 .dev_name
= "ttyATH",
626 .nr
= CONFIG_SERIAL_AR933X_NR_UARTS
,
627 .cons
= AR933X_SERIAL_CONSOLE
,
630 static int ar933x_uart_probe(struct platform_device
*pdev
)
632 struct ar933x_uart_platform_data
*pdata
;
633 struct ar933x_uart_port
*up
;
634 struct uart_port
*port
;
635 struct resource
*mem_res
;
636 struct resource
*irq_res
;
641 pdata
= pdev
->dev
.platform_data
;
649 if (id
> CONFIG_SERIAL_AR933X_NR_UARTS
)
652 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
654 dev_err(&pdev
->dev
, "no MEM resource\n");
658 irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
660 dev_err(&pdev
->dev
, "no IRQ resource\n");
664 up
= kzalloc(sizeof(struct ar933x_uart_port
), GFP_KERNEL
);
669 port
->mapbase
= mem_res
->start
;
671 port
->membase
= ioremap(mem_res
->start
, AR933X_UART_REGS_SIZE
);
672 if (!port
->membase
) {
678 port
->irq
= irq_res
->start
;
679 port
->dev
= &pdev
->dev
;
680 port
->type
= PORT_AR933X
;
681 port
->iotype
= UPIO_MEM32
;
682 port
->uartclk
= pdata
->uartclk
;
685 port
->fifosize
= AR933X_UART_FIFO_SIZE
;
686 port
->ops
= &ar933x_uart_ops
;
688 baud
= ar933x_uart_get_baud(port
->uartclk
, AR933X_UART_MAX_SCALE
, 1);
689 up
->min_baud
= max_t(unsigned int, baud
, AR933X_UART_MIN_BAUD
);
691 baud
= ar933x_uart_get_baud(port
->uartclk
, 0, AR933X_UART_MAX_STEP
);
692 up
->max_baud
= min_t(unsigned int, baud
, AR933X_UART_MAX_BAUD
);
694 ar933x_uart_add_console_port(up
);
696 ret
= uart_add_one_port(&ar933x_uart_driver
, &up
->port
);
700 platform_set_drvdata(pdev
, up
);
704 iounmap(up
->port
.membase
);
710 static int ar933x_uart_remove(struct platform_device
*pdev
)
712 struct ar933x_uart_port
*up
;
714 up
= platform_get_drvdata(pdev
);
715 platform_set_drvdata(pdev
, NULL
);
718 uart_remove_one_port(&ar933x_uart_driver
, &up
->port
);
719 iounmap(up
->port
.membase
);
726 static struct platform_driver ar933x_uart_platform_driver
= {
727 .probe
= ar933x_uart_probe
,
728 .remove
= ar933x_uart_remove
,
731 .owner
= THIS_MODULE
,
735 static int __init
ar933x_uart_init(void)
739 ar933x_uart_driver
.nr
= CONFIG_SERIAL_AR933X_NR_UARTS
;
740 ret
= uart_register_driver(&ar933x_uart_driver
);
744 ret
= platform_driver_register(&ar933x_uart_platform_driver
);
746 goto err_unregister_uart_driver
;
750 err_unregister_uart_driver
:
751 uart_unregister_driver(&ar933x_uart_driver
);
756 static void __exit
ar933x_uart_exit(void)
758 platform_driver_unregister(&ar933x_uart_platform_driver
);
759 uart_unregister_driver(&ar933x_uart_driver
);
762 module_init(ar933x_uart_init
);
763 module_exit(ar933x_uart_exit
);
765 MODULE_DESCRIPTION("Atheros AR933X UART driver");
766 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
767 MODULE_LICENSE("GPL v2");
768 MODULE_ALIAS("platform:" DRIVER_NAME
);