2 * uartlite.c: Serial driver for Xilinx uartlite serial controller
4 * Copyright (C) 2006 Peter Korsgaard <jacmet@sunsite.dk>
5 * Copyright (C) 2007 Secret Lab Technologies Ltd.
7 * This file is licensed under the terms of the GNU General Public License
8 * version 2. This program is licensed "as is" without any warranty of any
9 * kind, whether express or implied.
12 #include <linux/platform_device.h>
13 #include <linux/module.h>
14 #include <linux/console.h>
15 #include <linux/serial.h>
16 #include <linux/serial_core.h>
17 #include <linux/tty.h>
18 #include <linux/delay.h>
19 #include <linux/interrupt.h>
20 #include <linux/init.h>
22 #if defined(CONFIG_OF) && (defined(CONFIG_PPC32) || defined(CONFIG_MICROBLAZE))
24 #include <linux/of_address.h>
25 #include <linux/of_device.h>
26 #include <linux/of_platform.h>
28 /* Match table for of_platform binding */
29 static struct of_device_id ulite_of_match
[] __devinitdata
= {
30 { .compatible
= "xlnx,opb-uartlite-1.00.b", },
31 { .compatible
= "xlnx,xps-uartlite-1.00.a", },
34 MODULE_DEVICE_TABLE(of
, ulite_of_match
);
38 #define ULITE_NAME "ttyUL"
39 #define ULITE_MAJOR 204
40 #define ULITE_MINOR 187
41 #define ULITE_NR_UARTS 4
43 /* ---------------------------------------------------------------------
44 * Register definitions
46 * For register details see datasheet:
47 * http://www.xilinx.com/support/documentation/ip_documentation/opb_uartlite.pdf
52 #define ULITE_STATUS 0x08
53 #define ULITE_CONTROL 0x0c
55 #define ULITE_REGION 16
57 #define ULITE_STATUS_RXVALID 0x01
58 #define ULITE_STATUS_RXFULL 0x02
59 #define ULITE_STATUS_TXEMPTY 0x04
60 #define ULITE_STATUS_TXFULL 0x08
61 #define ULITE_STATUS_IE 0x10
62 #define ULITE_STATUS_OVERRUN 0x20
63 #define ULITE_STATUS_FRAME 0x40
64 #define ULITE_STATUS_PARITY 0x80
66 #define ULITE_CONTROL_RST_TX 0x01
67 #define ULITE_CONTROL_RST_RX 0x02
68 #define ULITE_CONTROL_IE 0x10
71 static struct uart_port ulite_ports
[ULITE_NR_UARTS
];
73 /* ---------------------------------------------------------------------
74 * Core UART driver operations
77 static int ulite_receive(struct uart_port
*port
, int stat
)
79 struct tty_struct
*tty
= port
->state
->port
.tty
;
81 char flag
= TTY_NORMAL
;
83 if ((stat
& (ULITE_STATUS_RXVALID
| ULITE_STATUS_OVERRUN
84 | ULITE_STATUS_FRAME
)) == 0)
88 if (stat
& ULITE_STATUS_RXVALID
) {
90 ch
= ioread32be(port
->membase
+ ULITE_RX
);
92 if (stat
& ULITE_STATUS_PARITY
)
93 port
->icount
.parity
++;
96 if (stat
& ULITE_STATUS_OVERRUN
)
97 port
->icount
.overrun
++;
99 if (stat
& ULITE_STATUS_FRAME
)
100 port
->icount
.frame
++;
103 /* drop byte with parity error if IGNPAR specificed */
104 if (stat
& port
->ignore_status_mask
& ULITE_STATUS_PARITY
)
105 stat
&= ~ULITE_STATUS_RXVALID
;
107 stat
&= port
->read_status_mask
;
109 if (stat
& ULITE_STATUS_PARITY
)
113 stat
&= ~port
->ignore_status_mask
;
115 if (stat
& ULITE_STATUS_RXVALID
)
116 tty_insert_flip_char(tty
, ch
, flag
);
118 if (stat
& ULITE_STATUS_FRAME
)
119 tty_insert_flip_char(tty
, 0, TTY_FRAME
);
121 if (stat
& ULITE_STATUS_OVERRUN
)
122 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
127 static int ulite_transmit(struct uart_port
*port
, int stat
)
129 struct circ_buf
*xmit
= &port
->state
->xmit
;
131 if (stat
& ULITE_STATUS_TXFULL
)
135 iowrite32be(port
->x_char
, port
->membase
+ ULITE_TX
);
141 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
144 iowrite32be(xmit
->buf
[xmit
->tail
], port
->membase
+ ULITE_TX
);
145 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
-1);
149 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
150 uart_write_wakeup(port
);
155 static irqreturn_t
ulite_isr(int irq
, void *dev_id
)
157 struct uart_port
*port
= dev_id
;
161 int stat
= ioread32be(port
->membase
+ ULITE_STATUS
);
162 busy
= ulite_receive(port
, stat
);
163 busy
|= ulite_transmit(port
, stat
);
169 tty_flip_buffer_push(port
->state
->port
.tty
);
176 static unsigned int ulite_tx_empty(struct uart_port
*port
)
181 spin_lock_irqsave(&port
->lock
, flags
);
182 ret
= ioread32be(port
->membase
+ ULITE_STATUS
);
183 spin_unlock_irqrestore(&port
->lock
, flags
);
185 return ret
& ULITE_STATUS_TXEMPTY
? TIOCSER_TEMT
: 0;
188 static unsigned int ulite_get_mctrl(struct uart_port
*port
)
190 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
193 static void ulite_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
198 static void ulite_stop_tx(struct uart_port
*port
)
203 static void ulite_start_tx(struct uart_port
*port
)
205 ulite_transmit(port
, ioread32be(port
->membase
+ ULITE_STATUS
));
208 static void ulite_stop_rx(struct uart_port
*port
)
210 /* don't forward any more data (like !CREAD) */
211 port
->ignore_status_mask
= ULITE_STATUS_RXVALID
| ULITE_STATUS_PARITY
212 | ULITE_STATUS_FRAME
| ULITE_STATUS_OVERRUN
;
215 static void ulite_enable_ms(struct uart_port
*port
)
220 static void ulite_break_ctl(struct uart_port
*port
, int ctl
)
225 static int ulite_startup(struct uart_port
*port
)
229 ret
= request_irq(port
->irq
, ulite_isr
,
230 IRQF_SHARED
| IRQF_SAMPLE_RANDOM
, "uartlite", port
);
234 iowrite32be(ULITE_CONTROL_RST_RX
| ULITE_CONTROL_RST_TX
,
235 port
->membase
+ ULITE_CONTROL
);
236 iowrite32be(ULITE_CONTROL_IE
, port
->membase
+ ULITE_CONTROL
);
241 static void ulite_shutdown(struct uart_port
*port
)
243 iowrite32be(0, port
->membase
+ ULITE_CONTROL
);
244 ioread32be(port
->membase
+ ULITE_CONTROL
); /* dummy */
245 free_irq(port
->irq
, port
);
248 static void ulite_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
249 struct ktermios
*old
)
254 spin_lock_irqsave(&port
->lock
, flags
);
256 port
->read_status_mask
= ULITE_STATUS_RXVALID
| ULITE_STATUS_OVERRUN
257 | ULITE_STATUS_TXFULL
;
259 if (termios
->c_iflag
& INPCK
)
260 port
->read_status_mask
|=
261 ULITE_STATUS_PARITY
| ULITE_STATUS_FRAME
;
263 port
->ignore_status_mask
= 0;
264 if (termios
->c_iflag
& IGNPAR
)
265 port
->ignore_status_mask
|= ULITE_STATUS_PARITY
266 | ULITE_STATUS_FRAME
| ULITE_STATUS_OVERRUN
;
268 /* ignore all characters if CREAD is not set */
269 if ((termios
->c_cflag
& CREAD
) == 0)
270 port
->ignore_status_mask
|=
271 ULITE_STATUS_RXVALID
| ULITE_STATUS_PARITY
272 | ULITE_STATUS_FRAME
| ULITE_STATUS_OVERRUN
;
275 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 460800);
276 uart_update_timeout(port
, termios
->c_cflag
, baud
);
278 spin_unlock_irqrestore(&port
->lock
, flags
);
281 static const char *ulite_type(struct uart_port
*port
)
283 return port
->type
== PORT_UARTLITE
? "uartlite" : NULL
;
286 static void ulite_release_port(struct uart_port
*port
)
288 release_mem_region(port
->mapbase
, ULITE_REGION
);
289 iounmap(port
->membase
);
290 port
->membase
= NULL
;
293 static int ulite_request_port(struct uart_port
*port
)
295 pr_debug("ulite console: port=%p; port->mapbase=%llx\n",
296 port
, (unsigned long long) port
->mapbase
);
298 if (!request_mem_region(port
->mapbase
, ULITE_REGION
, "uartlite")) {
299 dev_err(port
->dev
, "Memory region busy\n");
303 port
->membase
= ioremap(port
->mapbase
, ULITE_REGION
);
304 if (!port
->membase
) {
305 dev_err(port
->dev
, "Unable to map registers\n");
306 release_mem_region(port
->mapbase
, ULITE_REGION
);
313 static void ulite_config_port(struct uart_port
*port
, int flags
)
315 if (!ulite_request_port(port
))
316 port
->type
= PORT_UARTLITE
;
319 static int ulite_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
321 /* we don't want the core code to modify any port params */
325 #ifdef CONFIG_CONSOLE_POLL
326 static int ulite_get_poll_char(struct uart_port
*port
)
328 if (!(ioread32be(port
->membase
+ ULITE_STATUS
)
329 & ULITE_STATUS_RXVALID
))
332 return ioread32be(port
->membase
+ ULITE_RX
);
335 static void ulite_put_poll_char(struct uart_port
*port
, unsigned char ch
)
337 while (ioread32be(port
->membase
+ ULITE_STATUS
) & ULITE_STATUS_TXFULL
)
340 /* write char to device */
341 iowrite32be(ch
, port
->membase
+ ULITE_TX
);
345 static struct uart_ops ulite_ops
= {
346 .tx_empty
= ulite_tx_empty
,
347 .set_mctrl
= ulite_set_mctrl
,
348 .get_mctrl
= ulite_get_mctrl
,
349 .stop_tx
= ulite_stop_tx
,
350 .start_tx
= ulite_start_tx
,
351 .stop_rx
= ulite_stop_rx
,
352 .enable_ms
= ulite_enable_ms
,
353 .break_ctl
= ulite_break_ctl
,
354 .startup
= ulite_startup
,
355 .shutdown
= ulite_shutdown
,
356 .set_termios
= ulite_set_termios
,
358 .release_port
= ulite_release_port
,
359 .request_port
= ulite_request_port
,
360 .config_port
= ulite_config_port
,
361 .verify_port
= ulite_verify_port
,
362 #ifdef CONFIG_CONSOLE_POLL
363 .poll_get_char
= ulite_get_poll_char
,
364 .poll_put_char
= ulite_put_poll_char
,
368 /* ---------------------------------------------------------------------
369 * Console driver operations
372 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
373 static void ulite_console_wait_tx(struct uart_port
*port
)
378 /* Spin waiting for TX fifo to have space available */
379 for (i
= 0; i
< 100000; i
++) {
380 val
= ioread32be(port
->membase
+ ULITE_STATUS
);
381 if ((val
& ULITE_STATUS_TXFULL
) == 0)
387 static void ulite_console_putchar(struct uart_port
*port
, int ch
)
389 ulite_console_wait_tx(port
);
390 iowrite32be(ch
, port
->membase
+ ULITE_TX
);
393 static void ulite_console_write(struct console
*co
, const char *s
,
396 struct uart_port
*port
= &ulite_ports
[co
->index
];
401 if (oops_in_progress
) {
402 locked
= spin_trylock_irqsave(&port
->lock
, flags
);
404 spin_lock_irqsave(&port
->lock
, flags
);
406 /* save and disable interrupt */
407 ier
= ioread32be(port
->membase
+ ULITE_STATUS
) & ULITE_STATUS_IE
;
408 iowrite32be(0, port
->membase
+ ULITE_CONTROL
);
410 uart_console_write(port
, s
, count
, ulite_console_putchar
);
412 ulite_console_wait_tx(port
);
414 /* restore interrupt state */
416 iowrite32be(ULITE_CONTROL_IE
, port
->membase
+ ULITE_CONTROL
);
419 spin_unlock_irqrestore(&port
->lock
, flags
);
422 static int __devinit
ulite_console_setup(struct console
*co
, char *options
)
424 struct uart_port
*port
;
430 if (co
->index
< 0 || co
->index
>= ULITE_NR_UARTS
)
433 port
= &ulite_ports
[co
->index
];
435 /* Has the device been initialized yet? */
436 if (!port
->mapbase
) {
437 pr_debug("console on ttyUL%i not present\n", co
->index
);
441 /* not initialized yet? */
442 if (!port
->membase
) {
443 if (ulite_request_port(port
))
448 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
450 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
453 static struct uart_driver ulite_uart_driver
;
455 static struct console ulite_console
= {
457 .write
= ulite_console_write
,
458 .device
= uart_console_device
,
459 .setup
= ulite_console_setup
,
460 .flags
= CON_PRINTBUFFER
,
461 .index
= -1, /* Specified on the cmdline (e.g. console=ttyUL0 ) */
462 .data
= &ulite_uart_driver
,
465 static int __init
ulite_console_init(void)
467 register_console(&ulite_console
);
471 console_initcall(ulite_console_init
);
473 #endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
475 static struct uart_driver ulite_uart_driver
= {
476 .owner
= THIS_MODULE
,
477 .driver_name
= "uartlite",
478 .dev_name
= ULITE_NAME
,
479 .major
= ULITE_MAJOR
,
480 .minor
= ULITE_MINOR
,
481 .nr
= ULITE_NR_UARTS
,
482 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
483 .cons
= &ulite_console
,
487 /* ---------------------------------------------------------------------
488 * Port assignment functions (mapping devices to uart_port structures)
491 /** ulite_assign: register a uartlite device with the driver
493 * @dev: pointer to device structure
494 * @id: requested id number. Pass -1 for automatic port assignment
495 * @base: base address of uartlite registers
496 * @irq: irq number for uartlite
498 * Returns: 0 on success, <0 otherwise
500 static int __devinit
ulite_assign(struct device
*dev
, int id
, u32 base
, int irq
)
502 struct uart_port
*port
;
505 /* if id = -1; then scan for a free id and use that */
507 for (id
= 0; id
< ULITE_NR_UARTS
; id
++)
508 if (ulite_ports
[id
].mapbase
== 0)
511 if (id
< 0 || id
>= ULITE_NR_UARTS
) {
512 dev_err(dev
, "%s%i too large\n", ULITE_NAME
, id
);
516 if ((ulite_ports
[id
].mapbase
) && (ulite_ports
[id
].mapbase
!= base
)) {
517 dev_err(dev
, "cannot assign to %s%i; it is already in use\n",
522 port
= &ulite_ports
[id
];
524 spin_lock_init(&port
->lock
);
527 port
->iotype
= UPIO_MEM
;
528 port
->iobase
= 1; /* mark port in use */
529 port
->mapbase
= base
;
530 port
->membase
= NULL
;
531 port
->ops
= &ulite_ops
;
533 port
->flags
= UPF_BOOT_AUTOCONF
;
535 port
->type
= PORT_UNKNOWN
;
538 dev_set_drvdata(dev
, port
);
540 /* Register the port */
541 rc
= uart_add_one_port(&ulite_uart_driver
, port
);
543 dev_err(dev
, "uart_add_one_port() failed; err=%i\n", rc
);
545 dev_set_drvdata(dev
, NULL
);
552 /** ulite_release: register a uartlite device with the driver
554 * @dev: pointer to device structure
556 static int __devexit
ulite_release(struct device
*dev
)
558 struct uart_port
*port
= dev_get_drvdata(dev
);
562 rc
= uart_remove_one_port(&ulite_uart_driver
, port
);
563 dev_set_drvdata(dev
, NULL
);
570 /* ---------------------------------------------------------------------
571 * Platform bus binding
574 static int __devinit
ulite_probe(struct platform_device
*pdev
)
576 struct resource
*res
, *res2
;
578 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
582 res2
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
586 return ulite_assign(&pdev
->dev
, pdev
->id
, res
->start
, res2
->start
);
589 static int __devexit
ulite_remove(struct platform_device
*pdev
)
591 return ulite_release(&pdev
->dev
);
594 /* work with hotplug and coldplug */
595 MODULE_ALIAS("platform:uartlite");
597 static struct platform_driver ulite_platform_driver
= {
598 .probe
= ulite_probe
,
599 .remove
= __devexit_p(ulite_remove
),
601 .owner
= THIS_MODULE
,
606 /* ---------------------------------------------------------------------
609 #if defined(CONFIG_OF) && (defined(CONFIG_PPC32) || defined(CONFIG_MICROBLAZE))
611 ulite_of_probe(struct platform_device
*op
, const struct of_device_id
*match
)
614 const unsigned int *id
;
617 dev_dbg(&op
->dev
, "%s(%p, %p)\n", __func__
, op
, match
);
619 rc
= of_address_to_resource(op
->dev
.of_node
, 0, &res
);
621 dev_err(&op
->dev
, "invalid address\n");
625 irq
= irq_of_parse_and_map(op
->dev
.of_node
, 0);
627 id
= of_get_property(op
->dev
.of_node
, "port-number", NULL
);
629 return ulite_assign(&op
->dev
, id
? *id
: -1, res
.start
, irq
);
632 static int __devexit
ulite_of_remove(struct platform_device
*op
)
634 return ulite_release(&op
->dev
);
637 static struct of_platform_driver ulite_of_driver
= {
638 .probe
= ulite_of_probe
,
639 .remove
= __devexit_p(ulite_of_remove
),
642 .owner
= THIS_MODULE
,
643 .of_match_table
= ulite_of_match
,
647 /* Registration helpers to keep the number of #ifdefs to a minimum */
648 static inline int __init
ulite_of_register(void)
650 pr_debug("uartlite: calling of_register_platform_driver()\n");
651 return of_register_platform_driver(&ulite_of_driver
);
654 static inline void __exit
ulite_of_unregister(void)
656 of_unregister_platform_driver(&ulite_of_driver
);
658 #else /* CONFIG_OF && (CONFIG_PPC32 || CONFIG_MICROBLAZE) */
659 /* Appropriate config not enabled; do nothing helpers */
660 static inline int __init
ulite_of_register(void) { return 0; }
661 static inline void __exit
ulite_of_unregister(void) { }
662 #endif /* CONFIG_OF && (CONFIG_PPC32 || CONFIG_MICROBLAZE) */
664 /* ---------------------------------------------------------------------
665 * Module setup/teardown
668 int __init
ulite_init(void)
672 pr_debug("uartlite: calling uart_register_driver()\n");
673 ret
= uart_register_driver(&ulite_uart_driver
);
677 ret
= ulite_of_register();
681 pr_debug("uartlite: calling platform_driver_register()\n");
682 ret
= platform_driver_register(&ulite_platform_driver
);
689 ulite_of_unregister();
691 uart_unregister_driver(&ulite_uart_driver
);
693 printk(KERN_ERR
"registering uartlite driver failed: err=%i", ret
);
697 void __exit
ulite_exit(void)
699 platform_driver_unregister(&ulite_platform_driver
);
700 ulite_of_unregister();
701 uart_unregister_driver(&ulite_uart_driver
);
704 module_init(ulite_init
);
705 module_exit(ulite_exit
);
707 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
708 MODULE_DESCRIPTION("Xilinx uartlite serial driver");
709 MODULE_LICENSE("GPL");