]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/serial/uartlite.c
[IPSEC]: Fix uninitialised dst warning in __xfrm_lookup
[linux-2.6-omap-h63xx.git] / drivers / serial / uartlite.c
index c00a627ef89e4a60d7fbd0a52264c71102325070..a85f2d31a686c69f4f0a076358554bb54016325b 100644 (file)
@@ -1,7 +1,8 @@
 /*
  * uartlite.c: Serial driver for Xilinx uartlite serial controller
  *
- * Peter Korsgaard <jacmet@sunsite.dk>
+ * Copyright (C) 2006 Peter Korsgaard <jacmet@sunsite.dk>
+ * Copyright (C) 2007 Secret Lab Technologies Ltd.
  *
  * This file is licensed under the terms of the GNU General Public License
  * version 2.  This program is licensed "as is" without any warranty of any
 #include <linux/delay.h>
 #include <linux/interrupt.h>
 #include <asm/io.h>
+#if defined(CONFIG_OF)
+#include <linux/of_device.h>
+#include <linux/of_platform.h>
+#endif
 
 #define ULITE_NAME             "ttyUL"
 #define ULITE_MAJOR            204
 #define ULITE_MINOR            187
 #define ULITE_NR_UARTS         4
 
-/* For register details see datasheet:
-   http://www.xilinx.com/bvdocs/ipcenter/data_sheet/opb_uartlite.pdf
-*/
+/* ---------------------------------------------------------------------
+ * Register definitions
+ *
+ * For register details see datasheet:
+ * http://www.xilinx.com/bvdocs/ipcenter/data_sheet/opb_uartlite.pdf
+ */
+
 #define ULITE_RX               0x00
 #define ULITE_TX               0x04
 #define ULITE_STATUS           0x08
 
 static struct uart_port ulite_ports[ULITE_NR_UARTS];
 
+/* ---------------------------------------------------------------------
+ * Core UART driver operations
+ */
+
 static int ulite_receive(struct uart_port *port, int stat)
 {
        struct tty_struct *tty = port->info->tty;
@@ -62,7 +75,7 @@ static int ulite_receive(struct uart_port *port, int stat)
        /* stats */
        if (stat & ULITE_STATUS_RXVALID) {
                port->icount.rx++;
-               ch = in_be32((void*)port->membase + ULITE_RX);
+               ch = readb(port->membase + ULITE_RX);
 
                if (stat & ULITE_STATUS_PARITY)
                        port->icount.parity++;
@@ -107,7 +120,7 @@ static int ulite_transmit(struct uart_port *port, int stat)
                return 0;
 
        if (port->x_char) {
-               out_be32((void*)port->membase + ULITE_TX, port->x_char);
+               writeb(port->x_char, port->membase + ULITE_TX);
                port->x_char = 0;
                port->icount.tx++;
                return 1;
@@ -116,7 +129,7 @@ static int ulite_transmit(struct uart_port *port, int stat)
        if (uart_circ_empty(xmit) || uart_tx_stopped(port))
                return 0;
 
-       out_be32((void*)port->membase + ULITE_TX, xmit->buf[xmit->tail]);
+       writeb(xmit->buf[xmit->tail], port->membase + ULITE_TX);
        xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
        port->icount.tx++;
 
@@ -133,7 +146,7 @@ static irqreturn_t ulite_isr(int irq, void *dev_id)
        int busy;
 
        do {
-               int stat = in_be32((void*)port->membase + ULITE_STATUS);
+               int stat = readb(port->membase + ULITE_STATUS);
                busy  = ulite_receive(port, stat);
                busy |= ulite_transmit(port, stat);
        } while (busy);
@@ -149,7 +162,7 @@ static unsigned int ulite_tx_empty(struct uart_port *port)
        unsigned int ret;
 
        spin_lock_irqsave(&port->lock, flags);
-       ret = in_be32((void*)port->membase + ULITE_STATUS);
+       ret = readb(port->membase + ULITE_STATUS);
        spin_unlock_irqrestore(&port->lock, flags);
 
        return ret & ULITE_STATUS_TXEMPTY ? TIOCSER_TEMT : 0;
@@ -172,7 +185,7 @@ static void ulite_stop_tx(struct uart_port *port)
 
 static void ulite_start_tx(struct uart_port *port)
 {
-       ulite_transmit(port, in_be32((void*)port->membase + ULITE_STATUS));
+       ulite_transmit(port, readb(port->membase + ULITE_STATUS));
 }
 
 static void ulite_stop_rx(struct uart_port *port)
@@ -201,17 +214,17 @@ static int ulite_startup(struct uart_port *port)
        if (ret)
                return ret;
 
-       out_be32((void*)port->membase + ULITE_CONTROL,
-                ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX);
-       out_be32((void*)port->membase + ULITE_CONTROL, ULITE_CONTROL_IE);
+       writeb(ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX,
+              port->membase + ULITE_CONTROL);
+       writeb(ULITE_CONTROL_IE, port->membase + ULITE_CONTROL);
 
        return 0;
 }
 
 static void ulite_shutdown(struct uart_port *port)
 {
-       out_be32((void*)port->membase + ULITE_CONTROL, 0);
-       in_be32((void*)port->membase + ULITE_CONTROL); /* dummy */
+       writeb(0, port->membase + ULITE_CONTROL);
+       readb(port->membase + ULITE_CONTROL); /* dummy */
        free_irq(port->irq, port);
 }
 
@@ -308,23 +321,29 @@ static struct uart_ops ulite_ops = {
        .verify_port    = ulite_verify_port
 };
 
+/* ---------------------------------------------------------------------
+ * Console driver operations
+ */
+
 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
 static void ulite_console_wait_tx(struct uart_port *port)
 {
        int i;
+       u8 val;
 
-       /* wait up to 10ms for the character(s) to be sent */
-       for (i = 0; i < 10000; i++) {
-               if (in_be32((void*)port->membase + ULITE_STATUS) & ULITE_STATUS_TXEMPTY)
+       /* Spin waiting for TX fifo to have space available */
+       for (i = 0; i < 100000; i++) {
+               val = readb(port->membase + ULITE_STATUS);
+               if ((val & ULITE_STATUS_TXFULL) == 0)
                        break;
-               udelay(1);
+               cpu_relax();
        }
 }
 
 static void ulite_console_putchar(struct uart_port *port, int ch)
 {
        ulite_console_wait_tx(port);
-       out_be32((void*)port->membase + ULITE_TX, ch);
+       writeb(ch, port->membase + ULITE_TX);
 }
 
 static void ulite_console_write(struct console *co, const char *s,
@@ -341,8 +360,8 @@ static void ulite_console_write(struct console *co, const char *s,
                spin_lock_irqsave(&port->lock, flags);
 
        /* save and disable interrupt */
-       ier = in_be32((void*)port->membase + ULITE_STATUS) & ULITE_STATUS_IE;
-       out_be32((void*)port->membase + ULITE_CONTROL, 0);
+       ier = readb(port->membase + ULITE_STATUS) & ULITE_STATUS_IE;
+       writeb(0, port->membase + ULITE_CONTROL);
 
        uart_console_write(port, s, count, ulite_console_putchar);
 
@@ -350,12 +369,37 @@ static void ulite_console_write(struct console *co, const char *s,
 
        /* restore interrupt state */
        if (ier)
-               out_be32((void*)port->membase + ULITE_CONTROL, ULITE_CONTROL_IE);
+               writeb(ULITE_CONTROL_IE, port->membase + ULITE_CONTROL);
 
        if (locked)
                spin_unlock_irqrestore(&port->lock, flags);
 }
 
+#if defined(CONFIG_OF)
+static inline void __init ulite_console_of_find_device(int id)
+{
+       struct device_node *np;
+       struct resource res;
+       const unsigned int *of_id;
+       int rc;
+
+       for_each_compatible_node(np, NULL, "xilinx,uartlite") {
+               of_id = of_get_property(np, "port-number", NULL);
+               if ((!of_id) || (*of_id != id))
+                       continue;
+
+               rc = of_address_to_resource(np, 0, &res);
+               if (rc)
+                       continue;
+
+               ulite_ports[id].mapbase = res.start;
+               return;
+       }
+}
+#else /* CONFIG_OF */
+static inline void __init ulite_console_of_find_device(int id) { /* do nothing */ }
+#endif /* CONFIG_OF */
+
 static int __init ulite_console_setup(struct console *co, char *options)
 {
        struct uart_port *port;
@@ -369,9 +413,21 @@ static int __init ulite_console_setup(struct console *co, char *options)
 
        port = &ulite_ports[co->index];
 
-       /* not initialized yet? */
-       if (!port->membase)
+       /* Check if it is an OF device */
+       if (!port->mapbase)
+               ulite_console_of_find_device(co->index);
+
+       /* Do we have a device now? */
+       if (!port->mapbase) {
+               pr_debug("console on ttyUL%i not present\n", co->index);
                return -ENODEV;
+       }
+
+       /* not initialized yet? */
+       if (!port->membase) {
+               if (ulite_request_port(port))
+                       return -ENODEV;
+       }
 
        if (options)
                uart_parse_options(options, &baud, &parity, &bits, &flow);
@@ -413,6 +469,19 @@ static struct uart_driver ulite_uart_driver = {
 #endif
 };
 
+/* ---------------------------------------------------------------------
+ * Port assignment functions (mapping devices to uart_port structures)
+ */
+
+/** ulite_assign: register a uartlite device with the driver
+ *
+ * @dev: pointer to device structure
+ * @id: requested id number.  Pass -1 for automatic port assignment
+ * @base: base address of uartlite registers
+ * @irq: irq number for uartlite
+ *
+ * Returns: 0 on success, <0 otherwise
+ */
 static int __devinit ulite_assign(struct device *dev, int id, u32 base, int irq)
 {
        struct uart_port *port;
@@ -429,7 +498,7 @@ static int __devinit ulite_assign(struct device *dev, int id, u32 base, int irq)
                return -EINVAL;
        }
 
-       if (ulite_ports[id].mapbase) {
+       if ((ulite_ports[id].mapbase) && (ulite_ports[id].mapbase != base)) {
                dev_err(dev, "cannot assign to %s%i; it is already in use\n",
                        ULITE_NAME, id);
                return -EBUSY;
@@ -465,6 +534,10 @@ static int __devinit ulite_assign(struct device *dev, int id, u32 base, int irq)
        return 0;
 }
 
+/** ulite_release: register a uartlite device with the driver
+ *
+ * @dev: pointer to device structure
+ */
 static int __devinit ulite_release(struct device *dev)
 {
        struct uart_port *port = dev_get_drvdata(dev);
@@ -479,6 +552,10 @@ static int __devinit ulite_release(struct device *dev)
        return rc;
 }
 
+/* ---------------------------------------------------------------------
+ * Platform bus binding
+ */
+
 static int __devinit ulite_probe(struct platform_device *pdev)
 {
        struct resource *res, *res2;
@@ -508,24 +585,109 @@ static struct platform_driver ulite_platform_driver = {
                   },
 };
 
+/* ---------------------------------------------------------------------
+ * OF bus bindings
+ */
+#if defined(CONFIG_OF)
+static int __devinit
+ulite_of_probe(struct of_device *op, const struct of_device_id *match)
+{
+       struct resource res;
+       const unsigned int *id;
+       int irq, rc;
+
+       dev_dbg(&op->dev, "%s(%p, %p)\n", __FUNCTION__, op, match);
+
+       rc = of_address_to_resource(op->node, 0, &res);
+       if (rc) {
+               dev_err(&op->dev, "invalid address\n");
+               return rc;
+       }
+
+       irq = irq_of_parse_and_map(op->node, 0);
+
+       id = of_get_property(op->node, "port-number", NULL);
+
+       return ulite_assign(&op->dev, id ? *id : -1, res.start+3, irq);
+}
+
+static int __devexit ulite_of_remove(struct of_device *op)
+{
+       return ulite_release(&op->dev);
+}
+
+/* Match table for of_platform binding */
+static struct of_device_id __devinit ulite_of_match[] = {
+       { .type = "serial", .compatible = "xilinx,uartlite", },
+       {},
+};
+MODULE_DEVICE_TABLE(of, ulite_of_match);
+
+static struct of_platform_driver ulite_of_driver = {
+       .owner = THIS_MODULE,
+       .name = "uartlite",
+       .match_table = ulite_of_match,
+       .probe = ulite_of_probe,
+       .remove = __devexit_p(ulite_of_remove),
+       .driver = {
+               .name = "uartlite",
+       },
+};
+
+/* Registration helpers to keep the number of #ifdefs to a minimum */
+static inline int __init ulite_of_register(void)
+{
+       pr_debug("uartlite: calling of_register_platform_driver()\n");
+       return of_register_platform_driver(&ulite_of_driver);
+}
+
+static inline void __exit ulite_of_unregister(void)
+{
+       of_unregister_platform_driver(&ulite_of_driver);
+}
+#else /* CONFIG_OF */
+/* CONFIG_OF not enabled; do nothing helpers */
+static inline int __init ulite_of_register(void) { return 0; }
+static inline void __exit ulite_of_unregister(void) { }
+#endif /* CONFIG_OF */
+
+/* ---------------------------------------------------------------------
+ * Module setup/teardown
+ */
+
 int __init ulite_init(void)
 {
        int ret;
 
+       pr_debug("uartlite: calling uart_register_driver()\n");
        ret = uart_register_driver(&ulite_uart_driver);
        if (ret)
-               return ret;
+               goto err_uart;
+
+       ret = ulite_of_register();
+       if (ret)
+               goto err_of;
 
+       pr_debug("uartlite: calling platform_driver_register()\n");
        ret = platform_driver_register(&ulite_platform_driver);
        if (ret)
-               uart_unregister_driver(&ulite_uart_driver);
+               goto err_plat;
 
+       return 0;
+
+err_plat:
+       ulite_of_unregister();
+err_of:
+       uart_unregister_driver(&ulite_uart_driver);
+err_uart:
+       printk(KERN_ERR "registering uartlite driver failed: err=%i", ret);
        return ret;
 }
 
 void __exit ulite_exit(void)
 {
        platform_driver_unregister(&ulite_platform_driver);
+       ulite_of_unregister();
        uart_unregister_driver(&ulite_uart_driver);
 }