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/mach-ar71xx/ar933x_uart.h>
29 #include <asm/mach-ar71xx/ar933x_uart_platform.h>
31 #define DRIVER_NAME "ar933x-uart"
33 #define AR933X_DUMMY_STATUS_RD 0x01
35 static struct uart_driver ar933x_uart_driver
;
37 struct ar933x_uart_port
{
38 struct uart_port port
;
39 unsigned int ier
; /* shadow Interrupt Enable Register */
42 static inline unsigned int ar933x_uart_read(struct ar933x_uart_port
*up
,
45 return readl(up
->port
.membase
+ offset
);
48 static inline void ar933x_uart_write(struct ar933x_uart_port
*up
,
49 int offset
, unsigned int value
)
51 writel(value
, up
->port
.membase
+ offset
);
54 static inline void ar933x_uart_rmw(struct ar933x_uart_port
*up
,
61 t
= ar933x_uart_read(up
, offset
);
64 ar933x_uart_write(up
, offset
, t
);
67 static inline void ar933x_uart_rmw_set(struct ar933x_uart_port
*up
,
71 ar933x_uart_rmw(up
, offset
, 0, val
);
74 static inline void ar933x_uart_rmw_clear(struct ar933x_uart_port
*up
,
78 ar933x_uart_rmw(up
, offset
, val
, 0);
81 static inline void ar933x_uart_start_tx_interrupt(struct ar933x_uart_port
*up
)
83 up
->ier
|= AR933X_UART_INT_TX_EMPTY
;
84 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
87 static inline void ar933x_uart_stop_tx_interrupt(struct ar933x_uart_port
*up
)
89 up
->ier
&= ~AR933X_UART_INT_TX_EMPTY
;
90 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
,up
->ier
);
93 static inline void ar933x_uart_putc(struct ar933x_uart_port
*up
, int ch
)
97 rdata
= ch
& AR933X_UART_DATA_TX_RX_MASK
;
98 rdata
|= AR933X_UART_DATA_TX_CSR
;
99 ar933x_uart_write(up
, AR933X_UART_DATA_REG
, rdata
);
102 static unsigned int ar933x_uart_tx_empty(struct uart_port
*port
)
104 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
108 spin_lock_irqsave(&up
->port
.lock
, flags
);
109 rdata
= ar933x_uart_read(up
, AR933X_UART_DATA_REG
);
110 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
112 return (rdata
& AR933X_UART_DATA_TX_CSR
) ? 0 : TIOCSER_TEMT
;
115 static unsigned int ar933x_uart_get_mctrl(struct uart_port
*port
)
120 static void ar933x_uart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
124 static void ar933x_uart_start_tx(struct uart_port
*port
)
126 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
128 ar933x_uart_start_tx_interrupt(up
);
131 static void ar933x_uart_stop_tx(struct uart_port
*port
)
133 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
135 ar933x_uart_stop_tx_interrupt(up
);
138 static void ar933x_uart_stop_rx(struct uart_port
*port
)
140 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
142 up
->ier
&= ~AR933X_UART_INT_RX_VALID
;
143 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
146 static void ar933x_uart_break_ctl(struct uart_port
*port
, int break_state
)
148 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
151 spin_lock_irqsave(&up
->port
.lock
, flags
);
152 if (break_state
== -1)
153 ar933x_uart_rmw_set(up
, AR933X_UART_CS_REG
,
154 AR933X_UART_CS_TX_BREAK
);
156 ar933x_uart_rmw_clear(up
, AR933X_UART_CS_REG
,
157 AR933X_UART_CS_TX_BREAK
);
158 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
161 static void ar933x_uart_enable_ms(struct uart_port
*port
)
165 static void ar933x_uart_set_termios(struct uart_port
*port
,
166 struct ktermios
*new,
167 struct ktermios
*old
)
169 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
172 unsigned int baud
, scale
;
174 /* Only CS8 is supported */
175 new->c_cflag
&= ~CSIZE
;
178 /* Only one stop bit is supported */
179 new->c_cflag
&= ~CSTOPB
;
182 if (new->c_cflag
& PARENB
) {
183 if (!(new->c_cflag
& PARODD
))
184 cs
|= AR933X_UART_CS_PARITY_EVEN
;
186 cs
|= AR933X_UART_CS_PARITY_ODD
;
188 cs
|= AR933X_UART_CS_PARITY_NONE
;
191 /* Mark/space parity is not supported */
192 new->c_cflag
&= ~CMSPAR
;
194 baud
= uart_get_baud_rate(port
, new, old
, 0, port
->uartclk
/ 16);
195 scale
= (port
->uartclk
/ (16 * baud
)) - 1;
198 * Ok, we're now changing the port state. Do it with
199 * interrupts disabled.
201 spin_lock_irqsave(&up
->port
.lock
, flags
);
203 /* Update the per-port timeout. */
204 uart_update_timeout(port
, new->c_cflag
, baud
);
206 up
->port
.ignore_status_mask
= 0;
208 /* ignore all characters if CREAD is not set */
209 if ((new->c_cflag
& CREAD
) == 0)
210 up
->port
.ignore_status_mask
|= AR933X_DUMMY_STATUS_RD
;
212 ar933x_uart_write(up
, AR933X_UART_CLOCK_REG
,
213 scale
<< AR933X_UART_CLOCK_SCALE_S
| 8192);
215 /* setup configuration register */
216 ar933x_uart_rmw(up
, AR933X_UART_CS_REG
, AR933X_UART_CS_PARITY_M
, cs
);
218 /* enable host interrupt */
219 ar933x_uart_rmw_set(up
, AR933X_UART_CS_REG
,
220 AR933X_UART_CS_HOST_INT_EN
);
222 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
224 if (tty_termios_baud_rate(new))
225 tty_termios_encode_baud_rate(new, baud
, baud
);
228 static void ar933x_uart_rx_chars(struct ar933x_uart_port
*up
)
230 struct tty_struct
*tty
= up
->port
.state
->port
.tty
;
237 rdata
= ar933x_uart_read(up
, AR933X_UART_DATA_REG
);
238 if ((rdata
& AR933X_UART_DATA_RX_CSR
) == 0)
241 /* remove the character from the FIFO */
242 ar933x_uart_write(up
, AR933X_UART_DATA_REG
,
243 AR933X_UART_DATA_RX_CSR
);
245 up
->port
.icount
.rx
++;
246 ch
= rdata
& AR933X_UART_DATA_TX_RX_MASK
;
248 if (uart_handle_sysrq_char(&up
->port
, ch
))
251 if ((up
->port
.ignore_status_mask
& AR933X_DUMMY_STATUS_RD
) == 0)
252 tty_insert_flip_char(tty
, ch
, TTY_NORMAL
);
253 } while (max_count
-- > 0);
255 tty_flip_buffer_push(tty
);
258 static void ar933x_uart_tx_chars(struct ar933x_uart_port
*up
)
260 struct circ_buf
*xmit
= &up
->port
.state
->xmit
;
263 if (uart_tx_stopped(&up
->port
))
266 count
= up
->port
.fifosize
;
270 rdata
= ar933x_uart_read(up
, AR933X_UART_DATA_REG
);
271 if ((rdata
& AR933X_UART_DATA_TX_CSR
) == 0)
274 if (up
->port
.x_char
) {
275 ar933x_uart_putc(up
, up
->port
.x_char
);
276 up
->port
.icount
.tx
++;
281 if (uart_circ_empty(xmit
))
284 ar933x_uart_putc(up
, xmit
->buf
[xmit
->tail
]);
286 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
287 up
->port
.icount
.tx
++;
288 } while (--count
> 0);
290 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
291 uart_write_wakeup(&up
->port
);
293 if (!uart_circ_empty(xmit
))
294 ar933x_uart_start_tx_interrupt(up
);
297 static irqreturn_t
ar933x_uart_interrupt(int irq
, void *dev_id
)
299 struct ar933x_uart_port
*up
= dev_id
;
302 status
= ar933x_uart_read(up
, AR933X_UART_CS_REG
);
303 if ((status
& AR933X_UART_CS_HOST_INT
) == 0)
306 spin_lock(&up
->port
.lock
);
308 status
= ar933x_uart_read(up
, AR933X_UART_INT_REG
);
309 status
&= ar933x_uart_read(up
, AR933X_UART_INT_EN_REG
);
311 if (status
& AR933X_UART_INT_RX_VALID
) {
312 ar933x_uart_write(up
, AR933X_UART_INT_REG
,
313 AR933X_UART_INT_RX_VALID
);
314 ar933x_uart_rx_chars(up
);
317 if (status
& AR933X_UART_INT_TX_EMPTY
) {
318 ar933x_uart_write(up
, AR933X_UART_INT_REG
,
319 AR933X_UART_INT_TX_EMPTY
);
320 ar933x_uart_stop_tx_interrupt(up
);
321 ar933x_uart_tx_chars(up
);
324 spin_unlock(&up
->port
.lock
);
329 static int ar933x_uart_startup(struct uart_port
*port
)
331 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
335 ret
= request_irq(up
->port
.irq
, ar933x_uart_interrupt
,
336 up
->port
.irqflags
, dev_name(up
->port
.dev
), up
);
340 spin_lock_irqsave(&up
->port
.lock
, flags
);
342 /* Enable HOST interrupts */
343 ar933x_uart_rmw_set(up
, AR933X_UART_CS_REG
,
344 AR933X_UART_CS_HOST_INT_EN
);
346 /* Enable RX interrupts */
347 up
->ier
= AR933X_UART_INT_RX_VALID
;
348 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
350 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
355 static void ar933x_uart_shutdown(struct uart_port
*port
)
357 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
359 /* Disable all interrupts */
361 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, up
->ier
);
363 /* Disable break condition */
364 ar933x_uart_rmw_clear(up
, AR933X_UART_CS_REG
,
365 AR933X_UART_CS_TX_BREAK
);
367 free_irq(up
->port
.irq
, up
);
370 static const char *ar933x_uart_type(struct uart_port
*port
)
372 return (port
->type
== PORT_AR933X
) ? "AR933X UART" : NULL
;
375 static void ar933x_uart_release_port(struct uart_port
*port
)
377 /* Nothing to release ... */
380 static int ar933x_uart_request_port(struct uart_port
*port
)
382 /* UARTs always present */
386 static void ar933x_uart_config_port(struct uart_port
*port
, int flags
)
388 if (flags
& UART_CONFIG_TYPE
)
389 port
->type
= PORT_AR933X
;
392 static int ar933x_uart_verify_port(struct uart_port
*port
,
393 struct serial_struct
*ser
)
395 if (ser
->type
!= PORT_UNKNOWN
&&
396 ser
->type
!= PORT_AR933X
)
399 if (ser
->irq
< 0 || ser
->irq
>= NR_IRQS
)
402 if (ser
->baud_base
< 28800)
408 static struct uart_ops ar933x_uart_ops
= {
409 .tx_empty
= ar933x_uart_tx_empty
,
410 .set_mctrl
= ar933x_uart_set_mctrl
,
411 .get_mctrl
= ar933x_uart_get_mctrl
,
412 .stop_tx
= ar933x_uart_stop_tx
,
413 .start_tx
= ar933x_uart_start_tx
,
414 .stop_rx
= ar933x_uart_stop_rx
,
415 .enable_ms
= ar933x_uart_enable_ms
,
416 .break_ctl
= ar933x_uart_break_ctl
,
417 .startup
= ar933x_uart_startup
,
418 .shutdown
= ar933x_uart_shutdown
,
419 .set_termios
= ar933x_uart_set_termios
,
420 .type
= ar933x_uart_type
,
421 .release_port
= ar933x_uart_release_port
,
422 .request_port
= ar933x_uart_request_port
,
423 .config_port
= ar933x_uart_config_port
,
424 .verify_port
= ar933x_uart_verify_port
,
427 #ifdef CONFIG_SERIAL_AR933X_CONSOLE
429 static struct ar933x_uart_port
*ar933x_console_ports
[CONFIG_SERIAL_AR933X_NR_UARTS
];
431 static void ar933x_uart_wait_xmitr(struct ar933x_uart_port
*up
)
434 unsigned int timeout
= 60000;
436 /* Wait up to 60ms for the character(s) to be sent. */
438 status
= ar933x_uart_read(up
, AR933X_UART_DATA_REG
);
442 } while ((status
& AR933X_UART_DATA_TX_CSR
) == 0);
445 static void ar933x_uart_console_putchar(struct uart_port
*port
, int ch
)
447 struct ar933x_uart_port
*up
= (struct ar933x_uart_port
*) port
;
449 ar933x_uart_wait_xmitr(up
);
450 ar933x_uart_putc(up
, ch
);
453 static void ar933x_uart_console_write(struct console
*co
, const char *s
,
456 struct ar933x_uart_port
*up
= ar933x_console_ports
[co
->index
];
461 local_irq_save(flags
);
465 else if (oops_in_progress
)
466 locked
= spin_trylock(&up
->port
.lock
);
468 spin_lock(&up
->port
.lock
);
471 * First save the IER then disable the interrupts
473 int_en
= ar933x_uart_read(up
, AR933X_UART_INT_EN_REG
);
474 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, 0);
476 uart_console_write(&up
->port
, s
, count
, ar933x_uart_console_putchar
);
479 * Finally, wait for transmitter to become empty
480 * and restore the IER
482 ar933x_uart_wait_xmitr(up
);
483 ar933x_uart_write(up
, AR933X_UART_INT_EN_REG
, int_en
);
485 ar933x_uart_write(up
, AR933X_UART_INT_REG
, AR933X_UART_INT_ALLINTS
);
488 spin_unlock(&up
->port
.lock
);
490 local_irq_restore(flags
);
493 static int ar933x_uart_console_setup(struct console
*co
, char *options
)
495 struct ar933x_uart_port
*up
;
501 if (co
->index
< 0 || co
->index
>= CONFIG_SERIAL_AR933X_NR_UARTS
)
504 up
= ar933x_console_ports
[co
->index
];
509 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
511 return uart_set_options(&up
->port
, co
, baud
, parity
, bits
, flow
);
514 static struct console ar933x_uart_console
= {
516 .write
= ar933x_uart_console_write
,
517 .device
= uart_console_device
,
518 .setup
= ar933x_uart_console_setup
,
519 .flags
= CON_PRINTBUFFER
,
521 .data
= &ar933x_uart_driver
,
524 static void ar933x_uart_add_console_port(struct ar933x_uart_port
*up
)
526 ar933x_console_ports
[up
->port
.line
] = up
;
529 #define AR933X_SERIAL_CONSOLE &ar933x_uart_console
533 static inline void ar933x_uart_add_console_port(struct ar933x_uart_port
*up
) {}
535 #define AR933X_SERIAL_CONSOLE NULL
537 #endif /* CONFIG_SERIAL_AR933X_CONSOLE */
539 static struct uart_driver ar933x_uart_driver
= {
540 .owner
= THIS_MODULE
,
541 .driver_name
= DRIVER_NAME
,
542 .dev_name
= "ttyATH",
543 .nr
= CONFIG_SERIAL_AR933X_NR_UARTS
,
544 .cons
= AR933X_SERIAL_CONSOLE
,
547 static int __devinit
ar933x_uart_probe(struct platform_device
*pdev
)
549 struct ar933x_uart_platform_data
*pdata
;
550 struct ar933x_uart_port
*up
;
551 struct uart_port
*port
;
552 struct resource
*mem_res
;
553 struct resource
*irq_res
;
557 pdata
= pdev
->dev
.platform_data
;
565 if (id
> CONFIG_SERIAL_AR933X_NR_UARTS
)
568 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
570 dev_err(&pdev
->dev
, "no MEM resource\n");
574 irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
576 dev_err(&pdev
->dev
, "no IRQ resource\n");
580 up
= kzalloc(sizeof(struct ar933x_uart_port
), GFP_KERNEL
);
585 port
->mapbase
= mem_res
->start
;
587 port
->membase
= ioremap(mem_res
->start
, AR933X_UART_REGS_SIZE
);
588 if (!port
->membase
) {
594 port
->irq
= irq_res
->start
;
595 port
->dev
= &pdev
->dev
;
596 port
->type
= PORT_AR933X
;
597 port
->iotype
= UPIO_MEM32
;
598 port
->uartclk
= pdata
->uartclk
;
601 port
->fifosize
= AR933X_UART_FIFO_SIZE
;
602 port
->ops
= &ar933x_uart_ops
;
604 ar933x_uart_add_console_port(up
);
606 ret
= uart_add_one_port(&ar933x_uart_driver
, &up
->port
);
610 platform_set_drvdata(pdev
, up
);
614 iounmap(up
->port
.membase
);
620 static int __devexit
ar933x_uart_remove(struct platform_device
*pdev
)
622 struct ar933x_uart_port
*up
;
624 up
= platform_get_drvdata(pdev
);
625 platform_set_drvdata(pdev
, NULL
);
628 uart_remove_one_port(&ar933x_uart_driver
, &up
->port
);
629 iounmap(up
->port
.membase
);
636 static struct platform_driver ar933x_uart_platform_driver
= {
637 .probe
= ar933x_uart_probe
,
638 .remove
= __devexit_p(ar933x_uart_remove
),
641 .owner
= THIS_MODULE
,
645 static int __init
ar933x_uart_init(void)
649 ar933x_uart_driver
.nr
= CONFIG_SERIAL_AR933X_NR_UARTS
;
650 ret
= uart_register_driver(&ar933x_uart_driver
);
654 ret
= platform_driver_register(&ar933x_uart_platform_driver
);
656 goto err_unregister_uart_driver
;
660 err_unregister_uart_driver
:
661 uart_unregister_driver(&ar933x_uart_driver
);
666 static void __exit
ar933x_uart_exit(void)
668 platform_driver_unregister(&ar933x_uart_platform_driver
);
669 uart_unregister_driver(&ar933x_uart_driver
);
672 module_init(ar933x_uart_init
);
673 module_exit(ar933x_uart_exit
);
675 MODULE_DESCRIPTION("Atheros AR933X UART driver");
676 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
677 MODULE_LICENSE("GPL v2");
678 MODULE_ALIAS("platform:" DRIVER_NAME
);