Linux 3.4.102
[linux/fpc-iii.git] / drivers / tty / serial / ar933x_uart.c
blobe4f60e2b87f32e7c9087dd480c8fd5aa4e031873
1 /*
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>
25 #include <linux/io.h>
26 #include <linux/irq.h>
28 #include <asm/mach-ath79/ar933x_uart.h>
29 #include <asm/mach-ath79/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,
43 int offset)
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,
55 unsigned int offset,
56 unsigned int mask,
57 unsigned int val)
59 unsigned int t;
61 t = ar933x_uart_read(up, offset);
62 t &= ~mask;
63 t |= val;
64 ar933x_uart_write(up, offset, t);
67 static inline void ar933x_uart_rmw_set(struct ar933x_uart_port *up,
68 unsigned int offset,
69 unsigned int val)
71 ar933x_uart_rmw(up, offset, 0, val);
74 static inline void ar933x_uart_rmw_clear(struct ar933x_uart_port *up,
75 unsigned int offset,
76 unsigned int val)
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)
95 unsigned int rdata;
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;
105 unsigned long flags;
106 unsigned int rdata;
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)
117 return TIOCM_CAR;
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;
149 unsigned long flags;
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);
155 else
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;
170 unsigned int cs;
171 unsigned long flags;
172 unsigned int baud, scale;
174 /* Only CS8 is supported */
175 new->c_cflag &= ~CSIZE;
176 new->c_cflag |= CS8;
178 /* Only one stop bit is supported */
179 new->c_cflag &= ~CSTOPB;
181 cs = 0;
182 if (new->c_cflag & PARENB) {
183 if (!(new->c_cflag & PARODD))
184 cs |= AR933X_UART_CS_PARITY_EVEN;
185 else
186 cs |= AR933X_UART_CS_PARITY_ODD;
187 } else {
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;
231 int max_count = 256;
233 tty = tty_port_tty_get(&up->port.state->port);
234 do {
235 unsigned int rdata;
236 unsigned char ch;
238 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
239 if ((rdata & AR933X_UART_DATA_RX_CSR) == 0)
240 break;
242 /* remove the character from the FIFO */
243 ar933x_uart_write(up, AR933X_UART_DATA_REG,
244 AR933X_UART_DATA_RX_CSR);
246 if (!tty) {
247 /* discard the data if no tty available */
248 continue;
251 up->port.icount.rx++;
252 ch = rdata & AR933X_UART_DATA_TX_RX_MASK;
254 if (uart_handle_sysrq_char(&up->port, ch))
255 continue;
257 if ((up->port.ignore_status_mask & AR933X_DUMMY_STATUS_RD) == 0)
258 tty_insert_flip_char(tty, ch, TTY_NORMAL);
259 } while (max_count-- > 0);
261 if (tty) {
262 tty_flip_buffer_push(tty);
263 tty_kref_put(tty);
267 static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
269 struct circ_buf *xmit = &up->port.state->xmit;
270 int count;
272 if (uart_tx_stopped(&up->port))
273 return;
275 count = up->port.fifosize;
276 do {
277 unsigned int rdata;
279 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
280 if ((rdata & AR933X_UART_DATA_TX_CSR) == 0)
281 break;
283 if (up->port.x_char) {
284 ar933x_uart_putc(up, up->port.x_char);
285 up->port.icount.tx++;
286 up->port.x_char = 0;
287 continue;
290 if (uart_circ_empty(xmit))
291 break;
293 ar933x_uart_putc(up, xmit->buf[xmit->tail]);
295 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
296 up->port.icount.tx++;
297 } while (--count > 0);
299 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
300 uart_write_wakeup(&up->port);
302 if (!uart_circ_empty(xmit))
303 ar933x_uart_start_tx_interrupt(up);
306 static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id)
308 struct ar933x_uart_port *up = dev_id;
309 unsigned int status;
311 status = ar933x_uart_read(up, AR933X_UART_CS_REG);
312 if ((status & AR933X_UART_CS_HOST_INT) == 0)
313 return IRQ_NONE;
315 spin_lock(&up->port.lock);
317 status = ar933x_uart_read(up, AR933X_UART_INT_REG);
318 status &= ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
320 if (status & AR933X_UART_INT_RX_VALID) {
321 ar933x_uart_write(up, AR933X_UART_INT_REG,
322 AR933X_UART_INT_RX_VALID);
323 ar933x_uart_rx_chars(up);
326 if (status & AR933X_UART_INT_TX_EMPTY) {
327 ar933x_uart_write(up, AR933X_UART_INT_REG,
328 AR933X_UART_INT_TX_EMPTY);
329 ar933x_uart_stop_tx_interrupt(up);
330 ar933x_uart_tx_chars(up);
333 spin_unlock(&up->port.lock);
335 return IRQ_HANDLED;
338 static int ar933x_uart_startup(struct uart_port *port)
340 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
341 unsigned long flags;
342 int ret;
344 ret = request_irq(up->port.irq, ar933x_uart_interrupt,
345 up->port.irqflags, dev_name(up->port.dev), up);
346 if (ret)
347 return ret;
349 spin_lock_irqsave(&up->port.lock, flags);
351 /* Enable HOST interrupts */
352 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
353 AR933X_UART_CS_HOST_INT_EN);
355 /* Enable RX interrupts */
356 up->ier = AR933X_UART_INT_RX_VALID;
357 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
359 spin_unlock_irqrestore(&up->port.lock, flags);
361 return 0;
364 static void ar933x_uart_shutdown(struct uart_port *port)
366 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
368 /* Disable all interrupts */
369 up->ier = 0;
370 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
372 /* Disable break condition */
373 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
374 AR933X_UART_CS_TX_BREAK);
376 free_irq(up->port.irq, up);
379 static const char *ar933x_uart_type(struct uart_port *port)
381 return (port->type == PORT_AR933X) ? "AR933X UART" : NULL;
384 static void ar933x_uart_release_port(struct uart_port *port)
386 /* Nothing to release ... */
389 static int ar933x_uart_request_port(struct uart_port *port)
391 /* UARTs always present */
392 return 0;
395 static void ar933x_uart_config_port(struct uart_port *port, int flags)
397 if (flags & UART_CONFIG_TYPE)
398 port->type = PORT_AR933X;
401 static int ar933x_uart_verify_port(struct uart_port *port,
402 struct serial_struct *ser)
404 if (ser->type != PORT_UNKNOWN &&
405 ser->type != PORT_AR933X)
406 return -EINVAL;
408 if (ser->irq < 0 || ser->irq >= NR_IRQS)
409 return -EINVAL;
411 if (ser->baud_base < 28800)
412 return -EINVAL;
414 return 0;
417 static struct uart_ops ar933x_uart_ops = {
418 .tx_empty = ar933x_uart_tx_empty,
419 .set_mctrl = ar933x_uart_set_mctrl,
420 .get_mctrl = ar933x_uart_get_mctrl,
421 .stop_tx = ar933x_uart_stop_tx,
422 .start_tx = ar933x_uart_start_tx,
423 .stop_rx = ar933x_uart_stop_rx,
424 .enable_ms = ar933x_uart_enable_ms,
425 .break_ctl = ar933x_uart_break_ctl,
426 .startup = ar933x_uart_startup,
427 .shutdown = ar933x_uart_shutdown,
428 .set_termios = ar933x_uart_set_termios,
429 .type = ar933x_uart_type,
430 .release_port = ar933x_uart_release_port,
431 .request_port = ar933x_uart_request_port,
432 .config_port = ar933x_uart_config_port,
433 .verify_port = ar933x_uart_verify_port,
436 #ifdef CONFIG_SERIAL_AR933X_CONSOLE
438 static struct ar933x_uart_port *
439 ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS];
441 static void ar933x_uart_wait_xmitr(struct ar933x_uart_port *up)
443 unsigned int status;
444 unsigned int timeout = 60000;
446 /* Wait up to 60ms for the character(s) to be sent. */
447 do {
448 status = ar933x_uart_read(up, AR933X_UART_DATA_REG);
449 if (--timeout == 0)
450 break;
451 udelay(1);
452 } while ((status & AR933X_UART_DATA_TX_CSR) == 0);
455 static void ar933x_uart_console_putchar(struct uart_port *port, int ch)
457 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
459 ar933x_uart_wait_xmitr(up);
460 ar933x_uart_putc(up, ch);
463 static void ar933x_uart_console_write(struct console *co, const char *s,
464 unsigned int count)
466 struct ar933x_uart_port *up = ar933x_console_ports[co->index];
467 unsigned long flags;
468 unsigned int int_en;
469 int locked = 1;
471 local_irq_save(flags);
473 if (up->port.sysrq)
474 locked = 0;
475 else if (oops_in_progress)
476 locked = spin_trylock(&up->port.lock);
477 else
478 spin_lock(&up->port.lock);
481 * First save the IER then disable the interrupts
483 int_en = ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
484 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0);
486 uart_console_write(&up->port, s, count, ar933x_uart_console_putchar);
489 * Finally, wait for transmitter to become empty
490 * and restore the IER
492 ar933x_uart_wait_xmitr(up);
493 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, int_en);
495 ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_ALLINTS);
497 if (locked)
498 spin_unlock(&up->port.lock);
500 local_irq_restore(flags);
503 static int ar933x_uart_console_setup(struct console *co, char *options)
505 struct ar933x_uart_port *up;
506 int baud = 115200;
507 int bits = 8;
508 int parity = 'n';
509 int flow = 'n';
511 if (co->index < 0 || co->index >= CONFIG_SERIAL_AR933X_NR_UARTS)
512 return -EINVAL;
514 up = ar933x_console_ports[co->index];
515 if (!up)
516 return -ENODEV;
518 if (options)
519 uart_parse_options(options, &baud, &parity, &bits, &flow);
521 return uart_set_options(&up->port, co, baud, parity, bits, flow);
524 static struct console ar933x_uart_console = {
525 .name = "ttyATH",
526 .write = ar933x_uart_console_write,
527 .device = uart_console_device,
528 .setup = ar933x_uart_console_setup,
529 .flags = CON_PRINTBUFFER,
530 .index = -1,
531 .data = &ar933x_uart_driver,
534 static void ar933x_uart_add_console_port(struct ar933x_uart_port *up)
536 ar933x_console_ports[up->port.line] = up;
539 #define AR933X_SERIAL_CONSOLE (&ar933x_uart_console)
541 #else
543 static inline void ar933x_uart_add_console_port(struct ar933x_uart_port *up) {}
545 #define AR933X_SERIAL_CONSOLE NULL
547 #endif /* CONFIG_SERIAL_AR933X_CONSOLE */
549 static struct uart_driver ar933x_uart_driver = {
550 .owner = THIS_MODULE,
551 .driver_name = DRIVER_NAME,
552 .dev_name = "ttyATH",
553 .nr = CONFIG_SERIAL_AR933X_NR_UARTS,
554 .cons = AR933X_SERIAL_CONSOLE,
557 static int __devinit ar933x_uart_probe(struct platform_device *pdev)
559 struct ar933x_uart_platform_data *pdata;
560 struct ar933x_uart_port *up;
561 struct uart_port *port;
562 struct resource *mem_res;
563 struct resource *irq_res;
564 int id;
565 int ret;
567 pdata = pdev->dev.platform_data;
568 if (!pdata)
569 return -EINVAL;
571 id = pdev->id;
572 if (id == -1)
573 id = 0;
575 if (id > CONFIG_SERIAL_AR933X_NR_UARTS)
576 return -EINVAL;
578 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
579 if (!mem_res) {
580 dev_err(&pdev->dev, "no MEM resource\n");
581 return -EINVAL;
584 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
585 if (!irq_res) {
586 dev_err(&pdev->dev, "no IRQ resource\n");
587 return -EINVAL;
590 up = kzalloc(sizeof(struct ar933x_uart_port), GFP_KERNEL);
591 if (!up)
592 return -ENOMEM;
594 port = &up->port;
595 port->mapbase = mem_res->start;
597 port->membase = ioremap(mem_res->start, AR933X_UART_REGS_SIZE);
598 if (!port->membase) {
599 ret = -ENOMEM;
600 goto err_free_up;
603 port->line = id;
604 port->irq = irq_res->start;
605 port->dev = &pdev->dev;
606 port->type = PORT_AR933X;
607 port->iotype = UPIO_MEM32;
608 port->uartclk = pdata->uartclk;
610 port->regshift = 2;
611 port->fifosize = AR933X_UART_FIFO_SIZE;
612 port->ops = &ar933x_uart_ops;
614 ar933x_uart_add_console_port(up);
616 ret = uart_add_one_port(&ar933x_uart_driver, &up->port);
617 if (ret)
618 goto err_unmap;
620 platform_set_drvdata(pdev, up);
621 return 0;
623 err_unmap:
624 iounmap(up->port.membase);
625 err_free_up:
626 kfree(up);
627 return ret;
630 static int __devexit ar933x_uart_remove(struct platform_device *pdev)
632 struct ar933x_uart_port *up;
634 up = platform_get_drvdata(pdev);
635 platform_set_drvdata(pdev, NULL);
637 if (up) {
638 uart_remove_one_port(&ar933x_uart_driver, &up->port);
639 iounmap(up->port.membase);
640 kfree(up);
643 return 0;
646 static struct platform_driver ar933x_uart_platform_driver = {
647 .probe = ar933x_uart_probe,
648 .remove = __devexit_p(ar933x_uart_remove),
649 .driver = {
650 .name = DRIVER_NAME,
651 .owner = THIS_MODULE,
655 static int __init ar933x_uart_init(void)
657 int ret;
659 ar933x_uart_driver.nr = CONFIG_SERIAL_AR933X_NR_UARTS;
660 ret = uart_register_driver(&ar933x_uart_driver);
661 if (ret)
662 goto err_out;
664 ret = platform_driver_register(&ar933x_uart_platform_driver);
665 if (ret)
666 goto err_unregister_uart_driver;
668 return 0;
670 err_unregister_uart_driver:
671 uart_unregister_driver(&ar933x_uart_driver);
672 err_out:
673 return ret;
676 static void __exit ar933x_uart_exit(void)
678 platform_driver_unregister(&ar933x_uart_platform_driver);
679 uart_unregister_driver(&ar933x_uart_driver);
682 module_init(ar933x_uart_init);
683 module_exit(ar933x_uart_exit);
685 MODULE_DESCRIPTION("Atheros AR933X UART driver");
686 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
687 MODULE_LICENSE("GPL v2");
688 MODULE_ALIAS("platform:" DRIVER_NAME);