2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * ADM8668 serial driver, totally ripped the source from BCM63xx and changed
7 * all the registers to fit our hardware, and removed all the features that
8 * I didn't know because our GPL'd serial driver was way lame.
10 * Copyright (C) 2010 Scott Nicholas <neutronscott@scottn.us>
11 * Derived directly from bcm63xx_uart
12 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
16 #if defined(CONFIG_SERIAL_ADM8668_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
20 #include <linux/kernel.h>
21 #include <linux/platform_device.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/module.h>
25 #include <linux/console.h>
26 #include <linux/clk.h>
27 #include <linux/tty.h>
28 #include <linux/tty_flip.h>
29 #include <linux/sysrq.h>
30 #include <linux/serial.h>
31 #include <linux/serial_core.h>
34 #define ADM8668_NR_UARTS 1
36 static struct uart_port ports[ADM8668_NR_UARTS];
39 * handy uart register accessor
41 static inline unsigned int adm_uart_readl(struct uart_port *port,
44 return (*(volatile unsigned int *)(port->membase + offset));
47 static inline void adm_uart_writel(struct uart_port *port,
48 unsigned int value, unsigned int offset)
50 (*((volatile unsigned int *)(port->membase + offset))) = value;
54 * serial core request to check if uart tx fifo is empty
56 static unsigned int adm_uart_tx_empty(struct uart_port *port)
58 /* we always wait for completion, no buffer is made... */
63 * serial core request to set RTS and DTR pin state and loopback mode
65 static void adm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
70 * serial core request to return RI, CTS, DCD and DSR pin state
72 static unsigned int adm_uart_get_mctrl(struct uart_port *port)
78 * serial core request to disable tx ASAP (used for flow control)
80 static void adm_uart_stop_tx(struct uart_port *port)
84 val = adm_uart_readl(port, UART_CR_REG);
85 val &= ~(UART_TX_INT_EN);
86 adm_uart_writel(port, val, UART_CR_REG);
90 * serial core request to (re)enable tx
92 static void adm_uart_start_tx(struct uart_port *port)
96 val = adm_uart_readl(port, UART_CR_REG);
97 val |= UART_TX_INT_EN;
98 adm_uart_writel(port, val, UART_CR_REG);
102 * serial core request to stop rx, called before port shutdown
104 static void adm_uart_stop_rx(struct uart_port *port)
108 val = adm_uart_readl(port, UART_CR_REG);
109 val &= ~UART_RX_INT_EN;
110 adm_uart_writel(port, val, UART_CR_REG);
114 * serial core request to enable modem status interrupt reporting
116 static void adm_uart_enable_ms(struct uart_port *port)
121 * serial core request to start/stop emitting break char
123 static void adm_uart_break_ctl(struct uart_port *port, int ctl)
128 * return port type in string format
130 static const char *adm_uart_type(struct uart_port *port)
132 return (port->type == PORT_ADM8668) ? "adm8668_uart" : NULL;
136 * read all chars in rx fifo and send them to core
138 static void adm_uart_do_rx(struct uart_port *port)
140 struct tty_struct *tty;
141 unsigned int max_count;
143 /* limit number of char read in interrupt, should not be
144 * higher than fifo size anyway since we're much faster than
147 tty = port->state->port.tty;
149 unsigned int iestat, c, cstat;
152 /* get overrun/fifo empty information from ier
154 iestat = adm_uart_readl(port, UART_FR_REG);
155 if (iestat & UART_RX_FIFO_EMPTY)
159 cstat = adm_uart_readl(port, UART_RSR_REG);
161 adm_uart_writel(port, cstat, UART_RSR_REG);
163 c = adm_uart_readl(port, UART_DR_REG);
167 if (unlikely((cstat & UART_RX_STATUS_MASK))) {
169 if (cstat & UART_BREAK_ERR) {
171 if (uart_handle_break(port))
175 if (cstat & UART_PARITY_ERR)
176 port->icount.parity++;
177 if (cstat & UART_FRAMING_ERR)
178 port->icount.frame++;
179 if (cstat & UART_OVERRUN_ERR) {
180 port->icount.overrun++;
181 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
184 /* update flag wrt read_status_mask */
185 cstat &= port->read_status_mask;
186 if (cstat & UART_BREAK_ERR)
188 if (cstat & UART_FRAMING_ERR)
190 if (cstat & UART_PARITY_ERR)
194 if (uart_handle_sysrq_char(port, c))
198 if ((cstat & port->ignore_status_mask) == 0)
199 tty_insert_flip_char(tty, c, flag);
200 } while (max_count--);
202 tty_flip_buffer_push(tty);
206 * fill tx fifo with chars to send, stop when fifo is about to be full
207 * or when all chars have been sent.
209 static void adm_uart_do_tx(struct uart_port *port)
211 struct circ_buf *xmit;
214 adm_uart_writel(port, port->x_char, UART_DR_REG);
220 if (uart_tx_stopped(port))
221 adm_uart_stop_tx(port);
223 xmit = &port->state->xmit;
225 if (uart_circ_empty(xmit))
229 while ((adm_uart_readl(port, UART_FR_REG) & UART_TX_FIFO_FULL) != 0)
231 adm_uart_writel(port, xmit->buf[xmit->tail], UART_DR_REG);
232 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
234 if (uart_circ_empty(xmit))
238 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
239 uart_write_wakeup(port);
241 if (uart_circ_empty(xmit))
247 adm_uart_stop_tx(port);
251 * process uart interrupt
253 static irqreturn_t adm_uart_interrupt(int irq, void *dev_id)
255 struct uart_port *port;
256 unsigned int irqstat;
259 spin_lock(&port->lock);
261 irqstat = adm_uart_readl(port, UART_IIR_REG);
263 if (irqstat & (UART_RX_INT|UART_RX_TIMEOUT_INT)) {
264 adm_uart_do_rx(port);
267 if (irqstat & UART_TX_INT) {
268 adm_uart_do_tx(port);
270 spin_unlock(&port->lock);
275 * enable rx & tx operation on uart
277 static void adm_uart_enable(struct uart_port *port)
281 val = adm_uart_readl(port, UART_CR_REG);
283 val |= (UART_RX_INT_EN | UART_RX_TIMEOUT_INT_EN);
284 adm_uart_writel(port, val, UART_CR_REG);
288 * disable rx & tx operation on uart
290 static void adm_uart_disable(struct uart_port *port)
294 val = adm_uart_readl(port, UART_CR_REG);
295 val &= ~(UART_TX_INT_EN | UART_RX_INT_EN | UART_RX_TIMEOUT_INT_EN);
296 adm_uart_writel(port, val, UART_CR_REG);
300 * clear all unread data in rx fifo and unsent data in tx fifo
302 static void adm_uart_flush(struct uart_port *port)
304 /* read any pending char to make sure all irq status are
306 (void)adm_uart_readl(port, UART_DR_REG);
310 * serial core request to initialize uart and start rx operation
312 static int adm_uart_startup(struct uart_port *port)
316 /* clear any pending external input interrupt */
317 (void)adm_uart_readl(port, UART_IIR_REG);
319 /* register irq and enable rx interrupts */
320 ret = request_irq(port->irq, adm_uart_interrupt, 0,
321 adm_uart_type(port), port);
325 adm_uart_enable(port);
331 * serial core request to flush & disable uart
333 static void adm_uart_shutdown(struct uart_port *port)
337 spin_lock_irqsave(&port->lock, flags);
338 // adm_uart_writel(port, 0, UART_CR_REG);
339 spin_unlock_irqrestore(&port->lock, flags);
341 adm_uart_disable(port);
342 adm_uart_flush(port);
343 free_irq(port->irq, port);
347 * serial core request to change current uart setting
349 static void adm_uart_set_termios(struct uart_port *port,
350 struct ktermios *new,
351 struct ktermios *old)
353 port->ignore_status_mask = 0;
354 uart_update_timeout(port, new->c_cflag, 115200);
358 * serial core request to claim uart iomem
360 static int adm_uart_request_port(struct uart_port *port)
362 unsigned int size = 0xf;
363 if (!request_mem_region(port->mapbase, size, "adm8668")) {
364 dev_err(port->dev, "Memory region busy\n");
368 port->membase = ioremap(port->mapbase, size);
369 if (!port->membase) {
370 dev_err(port->dev, "Unable to map registers\n");
371 release_mem_region(port->mapbase, size);
378 * serial core request to release uart iomem
380 static void adm_uart_release_port(struct uart_port *port)
382 release_mem_region(port->mapbase, 0xf);
383 iounmap(port->membase);
387 * serial core request to do any port required autoconfiguration
389 static void adm_uart_config_port(struct uart_port *port, int flags)
391 if (flags & UART_CONFIG_TYPE) {
392 if (adm_uart_request_port(port))
394 port->type = PORT_ADM8668;
399 * serial core request to check that port information in serinfo are
402 static int adm_uart_verify_port(struct uart_port *port,
403 struct serial_struct *serinfo)
405 if (port->type != PORT_ADM8668)
407 if (port->irq != serinfo->irq)
409 if (port->iotype != serinfo->io_type)
411 if (port->mapbase != (unsigned long)serinfo->iomem_base)
416 /* serial core callbacks */
417 static struct uart_ops adm_uart_ops = {
418 .tx_empty = adm_uart_tx_empty,
419 .get_mctrl = adm_uart_get_mctrl,
420 .set_mctrl = adm_uart_set_mctrl,
421 .start_tx = adm_uart_start_tx,
422 .stop_tx = adm_uart_stop_tx,
423 .stop_rx = adm_uart_stop_rx,
424 .enable_ms = adm_uart_enable_ms,
425 .break_ctl = adm_uart_break_ctl,
426 .startup = adm_uart_startup,
427 .shutdown = adm_uart_shutdown,
428 .set_termios = adm_uart_set_termios,
429 .type = adm_uart_type,
430 .release_port = adm_uart_release_port,
431 .request_port = adm_uart_request_port,
432 .config_port = adm_uart_config_port,
433 .verify_port = adm_uart_verify_port,
436 #ifdef CONFIG_SERIAL_ADM8668_CONSOLE
437 static inline void wait_for_xmitr(struct uart_port *port)
439 while ((adm_uart_readl(port, UART_FR_REG) & UART_TX_FIFO_FULL) != 0)
446 static void adm_console_putchar(struct uart_port *port, int ch)
448 wait_for_xmitr(port);
449 adm_uart_writel(port, ch, UART_DR_REG);
453 * console core request to output given string
455 static void adm_console_write(struct console *co, const char *s,
458 struct uart_port *port;
462 port = &ports[co->index];
464 local_irq_save(flags);
466 /* adm_uart_interrupt() already took the lock */
468 } else if (oops_in_progress) {
469 locked = spin_trylock(&port->lock);
471 spin_lock(&port->lock);
475 /* call helper to deal with \r\n */
476 uart_console_write(port, s, count, adm_console_putchar);
478 /* and wait for char to be transmitted */
479 wait_for_xmitr(port);
482 spin_unlock(&port->lock);
483 local_irq_restore(flags);
487 * console core request to setup given console, find matching uart
490 static int adm_console_setup(struct console *co, char *options)
492 struct uart_port *port;
498 if (co->index < 0 || co->index >= ADM8668_NR_UARTS)
500 port = &ports[co->index];
505 uart_parse_options(options, &baud, &parity, &bits, &flow);
507 return uart_set_options(port, co, baud, parity, bits, flow);
510 static struct uart_driver adm_uart_driver;
512 static struct console adm8668_console = {
514 .write = adm_console_write,
515 .device = uart_console_device,
516 .setup = adm_console_setup,
517 .flags = CON_PRINTBUFFER,
519 .data = &adm_uart_driver,
522 static int __init adm8668_console_init(void)
524 register_console(&adm8668_console);
528 console_initcall(adm8668_console_init);
530 #define ADM8668_CONSOLE (&adm8668_console)
532 #define ADM8668_CONSOLE NULL
533 #endif /* CONFIG_SERIAL_ADM8668_CONSOLE */
535 static struct uart_driver adm_uart_driver = {
536 .owner = THIS_MODULE,
537 .driver_name = "adm8668_uart",
542 .cons = ADM8668_CONSOLE,
546 * platform driver probe/remove callback
548 static int __devinit adm_uart_probe(struct platform_device *pdev)
550 struct resource *res_mem, *res_irq;
551 struct uart_port *port;
554 if (pdev->id < 0 || pdev->id >= ADM8668_NR_UARTS)
557 if (ports[pdev->id].membase)
560 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
564 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
568 port = &ports[pdev->id];
569 memset(port, 0, sizeof(*port));
570 port->iotype = UPIO_MEM;
571 port->mapbase = res_mem->start;
572 port->irq = res_irq->start;
573 port->ops = &adm_uart_ops;
574 port->flags = UPF_BOOT_AUTOCONF;
575 port->dev = &pdev->dev;
577 port->uartclk = ADM8668_UARTCLK_FREQ;
579 ret = uart_add_one_port(&adm_uart_driver, port);
581 ports[pdev->id].membase = 0;
584 platform_set_drvdata(pdev, port);
588 static int __devexit adm_uart_remove(struct platform_device *pdev)
590 struct uart_port *port;
592 port = platform_get_drvdata(pdev);
593 uart_remove_one_port(&adm_uart_driver, port);
594 platform_set_drvdata(pdev, NULL);
595 /* mark port as free */
596 ports[pdev->id].membase = 0;
601 * platform driver stuff
603 static struct platform_driver adm_uart_platform_driver = {
604 .probe = adm_uart_probe,
605 .remove = __devexit_p(adm_uart_remove),
607 .owner = THIS_MODULE,
608 .name = "adm8668_uart",
612 static int __init adm_uart_init(void)
616 ret = uart_register_driver(&adm_uart_driver);
620 ret = platform_driver_register(&adm_uart_platform_driver);
622 uart_unregister_driver(&adm_uart_driver);
627 static void __exit adm_uart_exit(void)
629 platform_driver_unregister(&adm_uart_platform_driver);
630 uart_unregister_driver(&adm_uart_driver);
633 module_init(adm_uart_init);
634 module_exit(adm_uart_exit);
636 MODULE_AUTHOR("Scott Nicholas <neutronscott@scottn.us>");
637 MODULE_DESCRIPTION("ADM8668 integrated uart driver");
638 MODULE_LICENSE("GPL");