serial: uartps: Register own uart console and driver structures
[linux-2.6-microblaze.git] / drivers / tty / serial / xilinx_uartps.c
index 382f11c..2f1df9d 100644 (file)
@@ -30,7 +30,6 @@
 #define CDNS_UART_TTY_NAME     "ttyPS"
 #define CDNS_UART_NAME         "xuartps"
 #define CDNS_UART_MAJOR                0       /* use dynamic node allocation */
-#define CDNS_UART_MINOR                0       /* works best with devtmpfs */
 #define CDNS_UART_NR_PORTS     2
 #define CDNS_UART_FIFO_SIZE    64      /* FIFO size */
 #define CDNS_UART_REGISTER_SPACE       0x1000
@@ -1239,32 +1238,8 @@ static int cdns_uart_console_setup(struct console *co, char *options)
 
        return uart_set_options(port, co, baud, parity, bits, flow);
 }
-
-static struct uart_driver cdns_uart_uart_driver;
-
-static struct console cdns_uart_console = {
-       .name   = CDNS_UART_TTY_NAME,
-       .write  = cdns_uart_console_write,
-       .device = uart_console_device,
-       .setup  = cdns_uart_console_setup,
-       .flags  = CON_PRINTBUFFER,
-       .index  = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
-       .data   = &cdns_uart_uart_driver,
-};
 #endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
 
-static struct uart_driver cdns_uart_uart_driver = {
-       .owner          = THIS_MODULE,
-       .driver_name    = CDNS_UART_NAME,
-       .dev_name       = CDNS_UART_TTY_NAME,
-       .major          = CDNS_UART_MAJOR,
-       .minor          = CDNS_UART_MINOR,
-       .nr             = CDNS_UART_NR_PORTS,
-#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
-       .cons           = &cdns_uart_console,
-#endif
-};
-
 #ifdef CONFIG_PM_SLEEP
 /**
  * cdns_uart_suspend - suspend event
@@ -1394,9 +1369,6 @@ static const struct of_device_id cdns_uart_of_match[] = {
 };
 MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
 
-/* Temporary variable for storing number of instances */
-static int instances;
-
 /**
  * cdns_uart_probe - Platform driver probe
  * @pdev: Pointer to the platform device structure
@@ -1410,6 +1382,11 @@ static int cdns_uart_probe(struct platform_device *pdev)
        struct resource *res;
        struct cdns_uart *cdns_uart_data;
        const struct of_device_id *match;
+       struct uart_driver *cdns_uart_uart_driver;
+       char *driver_name;
+#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
+       struct console *cdns_uart_console;
+#endif
 
        cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
                        GFP_KERNEL);
@@ -1419,6 +1396,12 @@ static int cdns_uart_probe(struct platform_device *pdev)
        if (!port)
                return -ENOMEM;
 
+       cdns_uart_uart_driver = devm_kzalloc(&pdev->dev,
+                                            sizeof(*cdns_uart_uart_driver),
+                                            GFP_KERNEL);
+       if (!cdns_uart_uart_driver)
+               return -ENOMEM;
+
        /* Look for a serialN alias */
        id = of_alias_get_id(pdev->dev.of_node, "serial");
        if (id < 0)
@@ -1429,15 +1412,50 @@ static int cdns_uart_probe(struct platform_device *pdev)
                return -ENODEV;
        }
 
-       if (!cdns_uart_uart_driver.state) {
-               rc = uart_register_driver(&cdns_uart_uart_driver);
-               if (rc < 0) {
-                       dev_err(&pdev->dev, "Failed to register driver\n");
-                       return rc;
-               }
+       /* There is a need to use unique driver name */
+       driver_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s%d",
+                                    CDNS_UART_NAME, id);
+       if (!driver_name)
+               return -ENOMEM;
+
+       cdns_uart_uart_driver->owner = THIS_MODULE;
+       cdns_uart_uart_driver->driver_name = driver_name;
+       cdns_uart_uart_driver->dev_name = CDNS_UART_TTY_NAME;
+       cdns_uart_uart_driver->major = CDNS_UART_MAJOR;
+       cdns_uart_uart_driver->minor = id;
+       cdns_uart_uart_driver->nr = 1;
+
+#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
+       cdns_uart_console = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_console),
+                                        GFP_KERNEL);
+       if (!cdns_uart_console)
+               return -ENOMEM;
+
+       strncpy(cdns_uart_console->name, CDNS_UART_TTY_NAME,
+               sizeof(cdns_uart_console->name));
+       cdns_uart_console->index = id;
+       cdns_uart_console->write = cdns_uart_console_write;
+       cdns_uart_console->device = uart_console_device;
+       cdns_uart_console->setup = cdns_uart_console_setup;
+       cdns_uart_console->flags = CON_PRINTBUFFER;
+       cdns_uart_console->data = cdns_uart_uart_driver;
+       cdns_uart_uart_driver->cons = cdns_uart_console;
+#endif
+
+       rc = uart_register_driver(cdns_uart_uart_driver);
+       if (rc < 0) {
+               dev_err(&pdev->dev, "Failed to register driver\n");
+               return rc;
        }
 
-       cdns_uart_data->cdns_uart_driver = &cdns_uart_uart_driver;
+       cdns_uart_data->cdns_uart_driver = cdns_uart_uart_driver;
+
+       /*
+        * Setting up proper name_base needs to be done after uart
+        * registration because tty_driver structure is not filled.
+        * name_base is 0 by default.
+        */
+       cdns_uart_uart_driver->tty_driver->name_base = id;
 
        match = of_match_node(cdns_uart_of_match, pdev->dev.of_node);
        if (match && match->data) {
@@ -1508,7 +1526,6 @@ static int cdns_uart_probe(struct platform_device *pdev)
        port->flags     = UPF_BOOT_AUTOCONF;
        port->ops       = &cdns_uart_ops;
        port->fifosize  = CDNS_UART_FIFO_SIZE;
-       port->line      = id;
 
        /*
         * Register the port.
@@ -1535,11 +1552,11 @@ static int cdns_uart_probe(struct platform_device *pdev)
         * If register_console() don't assign value, then console_port pointer
         * is cleanup.
         */
-       if (cdns_uart_uart_driver.cons->index == -1)
+       if (!console_port)
                console_port = port;
 #endif
 
-       rc = uart_add_one_port(&cdns_uart_uart_driver, port);
+       rc = uart_add_one_port(cdns_uart_uart_driver, port);
        if (rc) {
                dev_err(&pdev->dev,
                        "uart_add_one_port() failed; err=%i\n", rc);
@@ -1548,11 +1565,11 @@ static int cdns_uart_probe(struct platform_device *pdev)
 
 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
        /* This is not port which is used for console that's why clean it up */
-       if (cdns_uart_uart_driver.cons->index == -1)
+       if (console_port == port &&
+           !(cdns_uart_uart_driver->cons->flags & CON_ENABLED))
                console_port = NULL;
 #endif
 
-       instances++;
        return 0;
 
 err_out_pm_disable:
@@ -1568,8 +1585,8 @@ err_out_clk_disable:
 err_out_clk_dis_pclk:
        clk_disable_unprepare(cdns_uart_data->pclk);
 err_out_unregister_driver:
-       if (!instances)
-               uart_unregister_driver(cdns_uart_data->cdns_uart_driver);
+       uart_unregister_driver(cdns_uart_data->cdns_uart_driver);
+
        return rc;
 }
 
@@ -1597,8 +1614,13 @@ static int cdns_uart_remove(struct platform_device *pdev)
        pm_runtime_disable(&pdev->dev);
        pm_runtime_set_suspended(&pdev->dev);
        pm_runtime_dont_use_autosuspend(&pdev->dev);
-       if (!--instances)
-               uart_unregister_driver(cdns_uart_data->cdns_uart_driver);
+
+#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
+       if (console_port == port)
+               console_port = NULL;
+#endif
+
+       uart_unregister_driver(cdns_uart_data->cdns_uart_driver);
        return rc;
 }