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>
21 #if defined(CONFIG_OF)
22 #include <linux/of_device.h>
23 #include <linux/of_platform.h>
26 #define ULITE_NAME "ttyUL"
27 #define ULITE_MAJOR 204
28 #define ULITE_MINOR 187
29 #define ULITE_NR_UARTS 4
31 /* ---------------------------------------------------------------------
32 * Register definitions
34 * For register details see datasheet:
35 * http://www.xilinx.com/bvdocs/ipcenter/data_sheet/opb_uartlite.pdf
40 #define ULITE_STATUS 0x08
41 #define ULITE_CONTROL 0x0c
43 #define ULITE_REGION 16
45 #define ULITE_STATUS_RXVALID 0x01
46 #define ULITE_STATUS_RXFULL 0x02
47 #define ULITE_STATUS_TXEMPTY 0x04
48 #define ULITE_STATUS_TXFULL 0x08
49 #define ULITE_STATUS_IE 0x10
50 #define ULITE_STATUS_OVERRUN 0x20
51 #define ULITE_STATUS_FRAME 0x40
52 #define ULITE_STATUS_PARITY 0x80
54 #define ULITE_CONTROL_RST_TX 0x01
55 #define ULITE_CONTROL_RST_RX 0x02
56 #define ULITE_CONTROL_IE 0x10
59 static struct uart_port ulite_ports
[ULITE_NR_UARTS
];
61 /* ---------------------------------------------------------------------
62 * Core UART driver operations
65 static int ulite_receive(struct uart_port
*port
, int stat
)
67 struct tty_struct
*tty
= port
->info
->tty
;
69 char flag
= TTY_NORMAL
;
71 if ((stat
& (ULITE_STATUS_RXVALID
| ULITE_STATUS_OVERRUN
72 | ULITE_STATUS_FRAME
)) == 0)
76 if (stat
& ULITE_STATUS_RXVALID
) {
78 ch
= readb(port
->membase
+ ULITE_RX
);
80 if (stat
& ULITE_STATUS_PARITY
)
81 port
->icount
.parity
++;
84 if (stat
& ULITE_STATUS_OVERRUN
)
85 port
->icount
.overrun
++;
87 if (stat
& ULITE_STATUS_FRAME
)
91 /* drop byte with parity error if IGNPAR specificed */
92 if (stat
& port
->ignore_status_mask
& ULITE_STATUS_PARITY
)
93 stat
&= ~ULITE_STATUS_RXVALID
;
95 stat
&= port
->read_status_mask
;
97 if (stat
& ULITE_STATUS_PARITY
)
101 stat
&= ~port
->ignore_status_mask
;
103 if (stat
& ULITE_STATUS_RXVALID
)
104 tty_insert_flip_char(tty
, ch
, flag
);
106 if (stat
& ULITE_STATUS_FRAME
)
107 tty_insert_flip_char(tty
, 0, TTY_FRAME
);
109 if (stat
& ULITE_STATUS_OVERRUN
)
110 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
115 static int ulite_transmit(struct uart_port
*port
, int stat
)
117 struct circ_buf
*xmit
= &port
->info
->xmit
;
119 if (stat
& ULITE_STATUS_TXFULL
)
123 writeb(port
->x_char
, port
->membase
+ ULITE_TX
);
129 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
132 writeb(xmit
->buf
[xmit
->tail
], port
->membase
+ ULITE_TX
);
133 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
-1);
137 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
138 uart_write_wakeup(port
);
143 static irqreturn_t
ulite_isr(int irq
, void *dev_id
)
145 struct uart_port
*port
= (struct uart_port
*)dev_id
;
149 int stat
= readb(port
->membase
+ ULITE_STATUS
);
150 busy
= ulite_receive(port
, stat
);
151 busy
|= ulite_transmit(port
, stat
);
154 tty_flip_buffer_push(port
->info
->tty
);
159 static unsigned int ulite_tx_empty(struct uart_port
*port
)
164 spin_lock_irqsave(&port
->lock
, flags
);
165 ret
= readb(port
->membase
+ ULITE_STATUS
);
166 spin_unlock_irqrestore(&port
->lock
, flags
);
168 return ret
& ULITE_STATUS_TXEMPTY
? TIOCSER_TEMT
: 0;
171 static unsigned int ulite_get_mctrl(struct uart_port
*port
)
173 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
176 static void ulite_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
181 static void ulite_stop_tx(struct uart_port
*port
)
186 static void ulite_start_tx(struct uart_port
*port
)
188 ulite_transmit(port
, readb(port
->membase
+ ULITE_STATUS
));
191 static void ulite_stop_rx(struct uart_port
*port
)
193 /* don't forward any more data (like !CREAD) */
194 port
->ignore_status_mask
= ULITE_STATUS_RXVALID
| ULITE_STATUS_PARITY
195 | ULITE_STATUS_FRAME
| ULITE_STATUS_OVERRUN
;
198 static void ulite_enable_ms(struct uart_port
*port
)
203 static void ulite_break_ctl(struct uart_port
*port
, int ctl
)
208 static int ulite_startup(struct uart_port
*port
)
212 ret
= request_irq(port
->irq
, ulite_isr
,
213 IRQF_DISABLED
| IRQF_SAMPLE_RANDOM
, "uartlite", port
);
217 writeb(ULITE_CONTROL_RST_RX
| ULITE_CONTROL_RST_TX
,
218 port
->membase
+ ULITE_CONTROL
);
219 writeb(ULITE_CONTROL_IE
, port
->membase
+ ULITE_CONTROL
);
224 static void ulite_shutdown(struct uart_port
*port
)
226 writeb(0, port
->membase
+ ULITE_CONTROL
);
227 readb(port
->membase
+ ULITE_CONTROL
); /* dummy */
228 free_irq(port
->irq
, port
);
231 static void ulite_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
232 struct ktermios
*old
)
237 spin_lock_irqsave(&port
->lock
, flags
);
239 port
->read_status_mask
= ULITE_STATUS_RXVALID
| ULITE_STATUS_OVERRUN
240 | ULITE_STATUS_TXFULL
;
242 if (termios
->c_iflag
& INPCK
)
243 port
->read_status_mask
|=
244 ULITE_STATUS_PARITY
| ULITE_STATUS_FRAME
;
246 port
->ignore_status_mask
= 0;
247 if (termios
->c_iflag
& IGNPAR
)
248 port
->ignore_status_mask
|= ULITE_STATUS_PARITY
249 | ULITE_STATUS_FRAME
| ULITE_STATUS_OVERRUN
;
251 /* ignore all characters if CREAD is not set */
252 if ((termios
->c_cflag
& CREAD
) == 0)
253 port
->ignore_status_mask
|=
254 ULITE_STATUS_RXVALID
| ULITE_STATUS_PARITY
255 | ULITE_STATUS_FRAME
| ULITE_STATUS_OVERRUN
;
258 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 460800);
259 uart_update_timeout(port
, termios
->c_cflag
, baud
);
261 spin_unlock_irqrestore(&port
->lock
, flags
);
264 static const char *ulite_type(struct uart_port
*port
)
266 return port
->type
== PORT_UARTLITE
? "uartlite" : NULL
;
269 static void ulite_release_port(struct uart_port
*port
)
271 release_mem_region(port
->mapbase
, ULITE_REGION
);
272 iounmap(port
->membase
);
273 port
->membase
= NULL
;
276 static int ulite_request_port(struct uart_port
*port
)
278 if (!request_mem_region(port
->mapbase
, ULITE_REGION
, "uartlite")) {
279 dev_err(port
->dev
, "Memory region busy\n");
283 port
->membase
= ioremap(port
->mapbase
, ULITE_REGION
);
284 if (!port
->membase
) {
285 dev_err(port
->dev
, "Unable to map registers\n");
286 release_mem_region(port
->mapbase
, ULITE_REGION
);
293 static void ulite_config_port(struct uart_port
*port
, int flags
)
295 if (!ulite_request_port(port
))
296 port
->type
= PORT_UARTLITE
;
299 static int ulite_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
301 /* we don't want the core code to modify any port params */
305 static struct uart_ops ulite_ops
= {
306 .tx_empty
= ulite_tx_empty
,
307 .set_mctrl
= ulite_set_mctrl
,
308 .get_mctrl
= ulite_get_mctrl
,
309 .stop_tx
= ulite_stop_tx
,
310 .start_tx
= ulite_start_tx
,
311 .stop_rx
= ulite_stop_rx
,
312 .enable_ms
= ulite_enable_ms
,
313 .break_ctl
= ulite_break_ctl
,
314 .startup
= ulite_startup
,
315 .shutdown
= ulite_shutdown
,
316 .set_termios
= ulite_set_termios
,
318 .release_port
= ulite_release_port
,
319 .request_port
= ulite_request_port
,
320 .config_port
= ulite_config_port
,
321 .verify_port
= ulite_verify_port
324 /* ---------------------------------------------------------------------
325 * Console driver operations
328 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
329 static void ulite_console_wait_tx(struct uart_port
*port
)
334 /* Spin waiting for TX fifo to have space available */
335 for (i
= 0; i
< 100000; i
++) {
336 val
= readb(port
->membase
+ ULITE_STATUS
);
337 if ((val
& ULITE_STATUS_TXFULL
) == 0)
343 static void ulite_console_putchar(struct uart_port
*port
, int ch
)
345 ulite_console_wait_tx(port
);
346 writeb(ch
, port
->membase
+ ULITE_TX
);
349 static void ulite_console_write(struct console
*co
, const char *s
,
352 struct uart_port
*port
= &ulite_ports
[co
->index
];
357 if (oops_in_progress
) {
358 locked
= spin_trylock_irqsave(&port
->lock
, flags
);
360 spin_lock_irqsave(&port
->lock
, flags
);
362 /* save and disable interrupt */
363 ier
= readb(port
->membase
+ ULITE_STATUS
) & ULITE_STATUS_IE
;
364 writeb(0, port
->membase
+ ULITE_CONTROL
);
366 uart_console_write(port
, s
, count
, ulite_console_putchar
);
368 ulite_console_wait_tx(port
);
370 /* restore interrupt state */
372 writeb(ULITE_CONTROL_IE
, port
->membase
+ ULITE_CONTROL
);
375 spin_unlock_irqrestore(&port
->lock
, flags
);
378 #if defined(CONFIG_OF)
379 static inline void __init
ulite_console_of_find_device(int id
)
381 struct device_node
*np
;
383 const unsigned int *of_id
;
386 for_each_compatible_node(np
, NULL
, "xilinx,uartlite") {
387 of_id
= of_get_property(np
, "port-number", NULL
);
388 if ((!of_id
) || (*of_id
!= id
))
391 rc
= of_address_to_resource(np
, 0, &res
);
395 ulite_ports
[id
].mapbase
= res
.start
;
399 #else /* CONFIG_OF */
400 static inline void __init
ulite_console_of_find_device(int id
) { /* do nothing */ }
401 #endif /* CONFIG_OF */
403 static int __init
ulite_console_setup(struct console
*co
, char *options
)
405 struct uart_port
*port
;
411 if (co
->index
< 0 || co
->index
>= ULITE_NR_UARTS
)
414 port
= &ulite_ports
[co
->index
];
416 /* Check if it is an OF device */
418 ulite_console_of_find_device(co
->index
);
420 /* Do we have a device now? */
421 if (!port
->mapbase
) {
422 pr_debug("console on ttyUL%i not present\n", co
->index
);
426 /* not initialized yet? */
427 if (!port
->membase
) {
428 if (ulite_request_port(port
))
433 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
435 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
438 static struct uart_driver ulite_uart_driver
;
440 static struct console ulite_console
= {
442 .write
= ulite_console_write
,
443 .device
= uart_console_device
,
444 .setup
= ulite_console_setup
,
445 .flags
= CON_PRINTBUFFER
,
446 .index
= -1, /* Specified on the cmdline (e.g. console=ttyUL0 ) */
447 .data
= &ulite_uart_driver
,
450 static int __init
ulite_console_init(void)
452 register_console(&ulite_console
);
456 console_initcall(ulite_console_init
);
458 #endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
460 static struct uart_driver ulite_uart_driver
= {
461 .owner
= THIS_MODULE
,
462 .driver_name
= "uartlite",
463 .dev_name
= ULITE_NAME
,
464 .major
= ULITE_MAJOR
,
465 .minor
= ULITE_MINOR
,
466 .nr
= ULITE_NR_UARTS
,
467 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
468 .cons
= &ulite_console
,
472 /* ---------------------------------------------------------------------
473 * Port assignment functions (mapping devices to uart_port structures)
476 /** ulite_assign: register a uartlite device with the driver
478 * @dev: pointer to device structure
479 * @id: requested id number. Pass -1 for automatic port assignment
480 * @base: base address of uartlite registers
481 * @irq: irq number for uartlite
483 * Returns: 0 on success, <0 otherwise
485 static int __devinit
ulite_assign(struct device
*dev
, int id
, u32 base
, int irq
)
487 struct uart_port
*port
;
490 /* if id = -1; then scan for a free id and use that */
492 for (id
= 0; id
< ULITE_NR_UARTS
; id
++)
493 if (ulite_ports
[id
].mapbase
== 0)
496 if (id
< 0 || id
>= ULITE_NR_UARTS
) {
497 dev_err(dev
, "%s%i too large\n", ULITE_NAME
, id
);
501 if ((ulite_ports
[id
].mapbase
) && (ulite_ports
[id
].mapbase
!= base
)) {
502 dev_err(dev
, "cannot assign to %s%i; it is already in use\n",
507 port
= &ulite_ports
[id
];
509 spin_lock_init(&port
->lock
);
512 port
->iotype
= UPIO_MEM
;
513 port
->iobase
= 1; /* mark port in use */
514 port
->mapbase
= base
;
515 port
->membase
= NULL
;
516 port
->ops
= &ulite_ops
;
518 port
->flags
= UPF_BOOT_AUTOCONF
;
520 port
->type
= PORT_UNKNOWN
;
523 dev_set_drvdata(dev
, port
);
525 /* Register the port */
526 rc
= uart_add_one_port(&ulite_uart_driver
, port
);
528 dev_err(dev
, "uart_add_one_port() failed; err=%i\n", rc
);
530 dev_set_drvdata(dev
, NULL
);
537 /** ulite_release: register a uartlite device with the driver
539 * @dev: pointer to device structure
541 static int __devinit
ulite_release(struct device
*dev
)
543 struct uart_port
*port
= dev_get_drvdata(dev
);
547 rc
= uart_remove_one_port(&ulite_uart_driver
, port
);
548 dev_set_drvdata(dev
, NULL
);
555 /* ---------------------------------------------------------------------
556 * Platform bus binding
559 static int __devinit
ulite_probe(struct platform_device
*pdev
)
561 struct resource
*res
, *res2
;
563 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
567 res2
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
571 return ulite_assign(&pdev
->dev
, pdev
->id
, res
->start
, res2
->start
);
574 static int ulite_remove(struct platform_device
*pdev
)
576 return ulite_release(&pdev
->dev
);
579 static struct platform_driver ulite_platform_driver
= {
580 .probe
= ulite_probe
,
581 .remove
= ulite_remove
,
583 .owner
= THIS_MODULE
,
588 /* ---------------------------------------------------------------------
591 #if defined(CONFIG_OF)
593 ulite_of_probe(struct of_device
*op
, const struct of_device_id
*match
)
596 const unsigned int *id
;
599 dev_dbg(&op
->dev
, "%s(%p, %p)\n", __FUNCTION__
, op
, match
);
601 rc
= of_address_to_resource(op
->node
, 0, &res
);
603 dev_err(&op
->dev
, "invalid address\n");
607 irq
= irq_of_parse_and_map(op
->node
, 0);
609 id
= of_get_property(op
->node
, "port-number", NULL
);
611 return ulite_assign(&op
->dev
, id
? *id
: -1, res
.start
+3, irq
);
614 static int __devexit
ulite_of_remove(struct of_device
*op
)
616 return ulite_release(&op
->dev
);
619 /* Match table for of_platform binding */
620 static struct of_device_id __devinit ulite_of_match
[] = {
621 { .type
= "serial", .compatible
= "xilinx,uartlite", },
624 MODULE_DEVICE_TABLE(of
, ulite_of_match
);
626 static struct of_platform_driver ulite_of_driver
= {
627 .owner
= THIS_MODULE
,
629 .match_table
= ulite_of_match
,
630 .probe
= ulite_of_probe
,
631 .remove
= __devexit_p(ulite_of_remove
),
637 /* Registration helpers to keep the number of #ifdefs to a minimum */
638 static inline int __init
ulite_of_register(void)
640 pr_debug("uartlite: calling of_register_platform_driver()\n");
641 return of_register_platform_driver(&ulite_of_driver
);
644 static inline void __exit
ulite_of_unregister(void)
646 of_unregister_platform_driver(&ulite_of_driver
);
648 #else /* CONFIG_OF */
649 /* CONFIG_OF not enabled; do nothing helpers */
650 static inline int __init
ulite_of_register(void) { return 0; }
651 static inline void __exit
ulite_of_unregister(void) { }
652 #endif /* CONFIG_OF */
654 /* ---------------------------------------------------------------------
655 * Module setup/teardown
658 int __init
ulite_init(void)
662 pr_debug("uartlite: calling uart_register_driver()\n");
663 ret
= uart_register_driver(&ulite_uart_driver
);
667 ret
= ulite_of_register();
671 pr_debug("uartlite: calling platform_driver_register()\n");
672 ret
= platform_driver_register(&ulite_platform_driver
);
679 ulite_of_unregister();
681 uart_unregister_driver(&ulite_uart_driver
);
683 printk(KERN_ERR
"registering uartlite driver failed: err=%i", ret
);
687 void __exit
ulite_exit(void)
689 platform_driver_unregister(&ulite_platform_driver
);
690 ulite_of_unregister();
691 uart_unregister_driver(&ulite_uart_driver
);
694 module_init(ulite_init
);
695 module_exit(ulite_exit
);
697 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
698 MODULE_DESCRIPTION("Xilinx uartlite serial driver");
699 MODULE_LICENSE("GPL");