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/tty_flip.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
21 #include <linux/init.h>
24 #include <linux/of_address.h>
25 #include <linux/of_device.h>
26 #include <linux/of_platform.h>
27 #include <linux/clk.h>
28 #include <linux/pm_runtime.h>
30 #define ULITE_NAME "ttyUL"
31 #define ULITE_MAJOR 204
32 #define ULITE_MINOR 187
34 /* ---------------------------------------------------------------------
35 * Register definitions
37 * For register details see datasheet:
38 * http://www.xilinx.com/support/documentation/ip_documentation/opb_uartlite.pdf
43 #define ULITE_STATUS 0x08
44 #define ULITE_CONTROL 0x0c
46 #define ULITE_REGION 16
48 #define ULITE_STATUS_RXVALID 0x01
49 #define ULITE_STATUS_RXFULL 0x02
50 #define ULITE_STATUS_TXEMPTY 0x04
51 #define ULITE_STATUS_TXFULL 0x08
52 #define ULITE_STATUS_IE 0x10
53 #define ULITE_STATUS_OVERRUN 0x20
54 #define ULITE_STATUS_FRAME 0x40
55 #define ULITE_STATUS_PARITY 0x80
57 #define ULITE_CONTROL_RST_TX 0x01
58 #define ULITE_CONTROL_RST_RX 0x02
59 #define ULITE_CONTROL_IE 0x10
60 #define UART_AUTOSUSPEND_TIMEOUT 3000
62 /* Static pointer to console port */
63 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
64 static struct uart_port *console_port;
67 struct uartlite_data {
68 const struct uartlite_reg_ops *reg_ops;
71 struct uart_driver *ulite_uart_driver;
74 struct uartlite_reg_ops {
75 u32 (*in)(void __iomem *addr);
76 void (*out)(u32 val, void __iomem *addr);
79 static u32 uartlite_inbe32(void __iomem *addr)
81 return ioread32be(addr);
84 static void uartlite_outbe32(u32 val, void __iomem *addr)
86 iowrite32be(val, addr);
89 static const struct uartlite_reg_ops uartlite_be = {
90 .in = uartlite_inbe32,
91 .out = uartlite_outbe32,
94 static u32 uartlite_inle32(void __iomem *addr)
96 return ioread32(addr);
99 static void uartlite_outle32(u32 val, void __iomem *addr)
101 iowrite32(val, addr);
104 static const struct uartlite_reg_ops uartlite_le = {
105 .in = uartlite_inle32,
106 .out = uartlite_outle32,
109 static inline u32 uart_in32(u32 offset, struct uart_port *port)
111 struct uartlite_data *pdata = port->private_data;
113 return pdata->reg_ops->in(port->membase + offset);
116 static inline void uart_out32(u32 val, u32 offset, struct uart_port *port)
118 struct uartlite_data *pdata = port->private_data;
120 pdata->reg_ops->out(val, port->membase + offset);
124 /* ---------------------------------------------------------------------
125 * Core UART driver operations
128 static int ulite_receive(struct uart_port *port, int stat)
130 struct tty_port *tport = &port->state->port;
131 unsigned char ch = 0;
132 char flag = TTY_NORMAL;
134 if ((stat & (ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
135 | ULITE_STATUS_FRAME)) == 0)
139 if (stat & ULITE_STATUS_RXVALID) {
141 ch = uart_in32(ULITE_RX, port);
143 if (stat & ULITE_STATUS_PARITY)
144 port->icount.parity++;
147 if (stat & ULITE_STATUS_OVERRUN)
148 port->icount.overrun++;
150 if (stat & ULITE_STATUS_FRAME)
151 port->icount.frame++;
154 /* drop byte with parity error if IGNPAR specificed */
155 if (stat & port->ignore_status_mask & ULITE_STATUS_PARITY)
156 stat &= ~ULITE_STATUS_RXVALID;
158 stat &= port->read_status_mask;
160 if (stat & ULITE_STATUS_PARITY)
164 stat &= ~port->ignore_status_mask;
166 if (stat & ULITE_STATUS_RXVALID)
167 tty_insert_flip_char(tport, ch, flag);
169 if (stat & ULITE_STATUS_FRAME)
170 tty_insert_flip_char(tport, 0, TTY_FRAME);
172 if (stat & ULITE_STATUS_OVERRUN)
173 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
178 static int ulite_transmit(struct uart_port *port, int stat)
180 struct circ_buf *xmit = &port->state->xmit;
182 if (stat & ULITE_STATUS_TXFULL)
186 uart_out32(port->x_char, ULITE_TX, port);
192 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
195 uart_out32(xmit->buf[xmit->tail], ULITE_TX, port);
196 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
200 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
201 uart_write_wakeup(port);
206 static irqreturn_t ulite_isr(int irq, void *dev_id)
208 struct uart_port *port = dev_id;
209 int stat, busy, n = 0;
213 spin_lock_irqsave(&port->lock, flags);
214 stat = uart_in32(ULITE_STATUS, port);
215 busy = ulite_receive(port, stat);
216 busy |= ulite_transmit(port, stat);
217 spin_unlock_irqrestore(&port->lock, flags);
223 tty_flip_buffer_push(&port->state->port);
230 static unsigned int ulite_tx_empty(struct uart_port *port)
235 spin_lock_irqsave(&port->lock, flags);
236 ret = uart_in32(ULITE_STATUS, port);
237 spin_unlock_irqrestore(&port->lock, flags);
239 return ret & ULITE_STATUS_TXEMPTY ? TIOCSER_TEMT : 0;
242 static unsigned int ulite_get_mctrl(struct uart_port *port)
244 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
247 static void ulite_set_mctrl(struct uart_port *port, unsigned int mctrl)
252 static void ulite_stop_tx(struct uart_port *port)
257 static void ulite_start_tx(struct uart_port *port)
259 ulite_transmit(port, uart_in32(ULITE_STATUS, port));
262 static void ulite_stop_rx(struct uart_port *port)
264 /* don't forward any more data (like !CREAD) */
265 port->ignore_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
266 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
269 static void ulite_break_ctl(struct uart_port *port, int ctl)
274 static int ulite_startup(struct uart_port *port)
276 struct uartlite_data *pdata = port->private_data;
279 ret = clk_enable(pdata->clk);
281 dev_err(port->dev, "Failed to enable clock\n");
285 ret = request_irq(port->irq, ulite_isr, IRQF_SHARED | IRQF_TRIGGER_RISING,
290 uart_out32(ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX,
291 ULITE_CONTROL, port);
292 uart_out32(ULITE_CONTROL_IE, ULITE_CONTROL, port);
297 static void ulite_shutdown(struct uart_port *port)
299 struct uartlite_data *pdata = port->private_data;
301 uart_out32(0, ULITE_CONTROL, port);
302 uart_in32(ULITE_CONTROL, port); /* dummy */
303 free_irq(port->irq, port);
304 clk_disable(pdata->clk);
307 static void ulite_set_termios(struct uart_port *port, struct ktermios *termios,
308 struct ktermios *old)
313 spin_lock_irqsave(&port->lock, flags);
315 port->read_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
316 | ULITE_STATUS_TXFULL;
318 if (termios->c_iflag & INPCK)
319 port->read_status_mask |=
320 ULITE_STATUS_PARITY | ULITE_STATUS_FRAME;
322 port->ignore_status_mask = 0;
323 if (termios->c_iflag & IGNPAR)
324 port->ignore_status_mask |= ULITE_STATUS_PARITY
325 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
327 /* ignore all characters if CREAD is not set */
328 if ((termios->c_cflag & CREAD) == 0)
329 port->ignore_status_mask |=
330 ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
331 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
334 baud = uart_get_baud_rate(port, termios, old, 0, 460800);
335 uart_update_timeout(port, termios->c_cflag, baud);
337 spin_unlock_irqrestore(&port->lock, flags);
340 static const char *ulite_type(struct uart_port *port)
342 return port->type == PORT_UARTLITE ? "uartlite" : NULL;
345 static void ulite_release_port(struct uart_port *port)
347 release_mem_region(port->mapbase, ULITE_REGION);
348 iounmap(port->membase);
349 port->membase = NULL;
352 static int ulite_request_port(struct uart_port *port)
354 struct uartlite_data *pdata = port->private_data;
357 pr_debug("ulite console: port=%p; port->mapbase=%llx\n",
358 port, (unsigned long long) port->mapbase);
360 if (!request_mem_region(port->mapbase, ULITE_REGION, "uartlite")) {
361 dev_err(port->dev, "Memory region busy\n");
365 port->membase = ioremap(port->mapbase, ULITE_REGION);
366 if (!port->membase) {
367 dev_err(port->dev, "Unable to map registers\n");
368 release_mem_region(port->mapbase, ULITE_REGION);
372 pdata->reg_ops = &uartlite_be;
373 ret = uart_in32(ULITE_CONTROL, port);
374 uart_out32(ULITE_CONTROL_RST_TX, ULITE_CONTROL, port);
375 ret = uart_in32(ULITE_STATUS, port);
376 /* Endianess detection */
377 if ((ret & ULITE_STATUS_TXEMPTY) != ULITE_STATUS_TXEMPTY)
378 pdata->reg_ops = &uartlite_le;
383 static void ulite_config_port(struct uart_port *port, int flags)
385 if (!ulite_request_port(port))
386 port->type = PORT_UARTLITE;
389 static int ulite_verify_port(struct uart_port *port, struct serial_struct *ser)
391 /* we don't want the core code to modify any port params */
395 static void ulite_pm(struct uart_port *port, unsigned int state,
396 unsigned int oldstate)
399 pm_runtime_get_sync(port->dev);
401 pm_runtime_mark_last_busy(port->dev);
402 pm_runtime_put_autosuspend(port->dev);
406 #ifdef CONFIG_CONSOLE_POLL
407 static int ulite_get_poll_char(struct uart_port *port)
409 if (!(uart_in32(ULITE_STATUS, port) & ULITE_STATUS_RXVALID))
412 return uart_in32(ULITE_RX, port);
415 static void ulite_put_poll_char(struct uart_port *port, unsigned char ch)
417 while (uart_in32(ULITE_STATUS, port) & ULITE_STATUS_TXFULL)
420 /* write char to device */
421 uart_out32(ch, ULITE_TX, port);
425 static const struct uart_ops ulite_ops = {
426 .tx_empty = ulite_tx_empty,
427 .set_mctrl = ulite_set_mctrl,
428 .get_mctrl = ulite_get_mctrl,
429 .stop_tx = ulite_stop_tx,
430 .start_tx = ulite_start_tx,
431 .stop_rx = ulite_stop_rx,
432 .break_ctl = ulite_break_ctl,
433 .startup = ulite_startup,
434 .shutdown = ulite_shutdown,
435 .set_termios = ulite_set_termios,
437 .release_port = ulite_release_port,
438 .request_port = ulite_request_port,
439 .config_port = ulite_config_port,
440 .verify_port = ulite_verify_port,
442 #ifdef CONFIG_CONSOLE_POLL
443 .poll_get_char = ulite_get_poll_char,
444 .poll_put_char = ulite_put_poll_char,
448 /* ---------------------------------------------------------------------
449 * Console driver operations
452 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
453 static void ulite_console_wait_tx(struct uart_port *port)
456 unsigned long timeout;
459 * Spin waiting for TX fifo to have space available.
460 * When using the Microblaze Debug Module this can take up to 1s
462 timeout = jiffies + msecs_to_jiffies(1000);
464 val = uart_in32(ULITE_STATUS, port);
465 if ((val & ULITE_STATUS_TXFULL) == 0)
467 if (time_after(jiffies, timeout)) {
469 "timeout waiting for TX buffer empty\n");
476 static void ulite_console_putchar(struct uart_port *port, int ch)
478 ulite_console_wait_tx(port);
479 uart_out32(ch, ULITE_TX, port);
482 static void ulite_console_write(struct console *co, const char *s,
485 struct uart_port *port = console_port;
490 if (oops_in_progress) {
491 locked = spin_trylock_irqsave(&port->lock, flags);
493 spin_lock_irqsave(&port->lock, flags);
495 /* save and disable interrupt */
496 ier = uart_in32(ULITE_STATUS, port) & ULITE_STATUS_IE;
497 uart_out32(0, ULITE_CONTROL, port);
499 uart_console_write(port, s, count, ulite_console_putchar);
501 ulite_console_wait_tx(port);
503 /* restore interrupt state */
505 uart_out32(ULITE_CONTROL_IE, ULITE_CONTROL, port);
508 spin_unlock_irqrestore(&port->lock, flags);
511 static int ulite_console_setup(struct console *co, char *options)
513 struct uart_port *port;
522 /* Has the device been initialized yet? */
523 if (!port->mapbase) {
524 pr_debug("console on ttyUL%i not present\n", co->index);
528 /* not initialized yet? */
529 if (!port->membase) {
530 if (ulite_request_port(port))
535 uart_parse_options(options, &baud, &parity, &bits, &flow);
537 return uart_set_options(port, co, baud, parity, bits, flow);
540 static void early_uartlite_putc(struct uart_port *port, int c)
543 * Limit how many times we'll spin waiting for TX FIFO status.
544 * This will prevent lockups if the base address is incorrectly
545 * set, or any other issue on the UARTLITE.
546 * This limit is pretty arbitrary, unless we are at about 10 baud
547 * we'll never timeout on a working UART.
550 unsigned retries = 1000000;
551 /* read status bit - 0x8 offset */
552 while (--retries && (readl(port->membase + 8) & (1 << 3)))
555 /* Only attempt the iowrite if we didn't timeout */
556 /* write to TX_FIFO - 0x4 offset */
558 writel(c & 0xff, port->membase + 4);
561 static void early_uartlite_write(struct console *console,
562 const char *s, unsigned n)
564 struct earlycon_device *device = console->data;
565 uart_console_write(&device->port, s, n, early_uartlite_putc);
568 static int __init early_uartlite_setup(struct earlycon_device *device,
571 if (!device->port.membase)
574 device->con->write = early_uartlite_write;
577 EARLYCON_DECLARE(uartlite, early_uartlite_setup);
578 OF_EARLYCON_DECLARE(uartlite_b, "xlnx,opb-uartlite-1.00.b", early_uartlite_setup);
579 OF_EARLYCON_DECLARE(uartlite_a, "xlnx,xps-uartlite-1.00.a", early_uartlite_setup);
581 #endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
583 /* ---------------------------------------------------------------------
584 * Port assignment functions (mapping devices to uart_port structures)
587 /** ulite_assign: register a uartlite device with the driver
589 * @dev: pointer to device structure
590 * @id: requested id number. Pass -1 for automatic port assignment
591 * @base: base address of uartlite registers
592 * @irq: irq number for uartlite
593 * @pdata: private data for uartlite
595 * Returns: 0 on success, <0 otherwise
597 static int ulite_assign(struct device *dev, int id, u32 base, int irq,
598 struct uartlite_data *pdata)
600 struct uart_port *port;
603 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
607 spin_lock_init(&port->lock);
610 port->iotype = UPIO_MEM;
611 port->iobase = 1; /* mark port in use */
612 port->mapbase = base;
613 port->membase = NULL;
614 port->ops = &ulite_ops;
616 port->flags = UPF_BOOT_AUTOCONF;
618 port->type = PORT_UNKNOWN;
619 port->private_data = pdata;
621 dev_set_drvdata(dev, port);
623 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
625 * If console hasn't been found yet try to assign this port
626 * because it is required to be assigned for console setup function.
627 * If register_console() don't assign value, then console_port pointer
634 /* Register the port */
635 rc = uart_add_one_port(pdata->ulite_uart_driver, port);
637 dev_err(dev, "uart_add_one_port() failed; err=%i\n", rc);
639 dev_set_drvdata(dev, NULL);
643 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
644 /* This is not port which is used for console that's why clean it up */
645 if (console_port == port &&
646 !(pdata->ulite_uart_driver->cons->flags & CON_ENABLED))
653 /** ulite_release: register a uartlite device with the driver
655 * @dev: pointer to device structure
657 static int ulite_release(struct device *dev)
659 struct uart_port *port = dev_get_drvdata(dev);
660 struct uartlite_data *pdata = port->private_data;
664 rc = uart_remove_one_port(pdata->ulite_uart_driver, port);
665 dev_set_drvdata(dev, NULL);
673 * ulite_suspend - Stop the device.
675 * @dev: handle to the device structure.
678 static int __maybe_unused ulite_suspend(struct device *dev)
680 struct uart_port *port = dev_get_drvdata(dev);
681 struct uartlite_data *pdata = port->private_data;
684 uart_suspend_port(pdata->ulite_uart_driver, port);
690 * ulite_resume - Resume the device.
692 * @dev: handle to the device structure.
693 * Return: 0 on success, errno otherwise.
695 static int __maybe_unused ulite_resume(struct device *dev)
697 struct uart_port *port = dev_get_drvdata(dev);
698 struct uartlite_data *pdata = port->private_data;
701 uart_resume_port(pdata->ulite_uart_driver, port);
706 static int __maybe_unused ulite_runtime_suspend(struct device *dev)
708 struct uart_port *port = dev_get_drvdata(dev);
709 struct uartlite_data *pdata = port->private_data;
711 clk_disable(pdata->clk);
715 static int __maybe_unused ulite_runtime_resume(struct device *dev)
717 struct uart_port *port = dev_get_drvdata(dev);
718 struct uartlite_data *pdata = port->private_data;
720 clk_enable(pdata->clk);
723 /* ---------------------------------------------------------------------
724 * Platform bus binding
727 static const struct dev_pm_ops ulite_pm_ops = {
728 SET_SYSTEM_SLEEP_PM_OPS(ulite_suspend, ulite_resume)
729 SET_RUNTIME_PM_OPS(ulite_runtime_suspend,
730 ulite_runtime_resume, NULL)
733 #if defined(CONFIG_OF)
734 /* Match table for of_platform binding */
735 static const struct of_device_id ulite_of_match[] = {
736 { .compatible = "xlnx,opb-uartlite-1.00.b", },
737 { .compatible = "xlnx,xps-uartlite-1.00.a", },
740 MODULE_DEVICE_TABLE(of, ulite_of_match);
741 #endif /* CONFIG_OF */
744 * Maximum number of instances without alias IDs but if there is alias
745 * which target "< MAX_UART_INSTANCES" range this ID can't be used.
747 #define MAX_UART_INSTANCES 256
749 /* Stores static aliases list */
750 static DECLARE_BITMAP(alias_bitmap, MAX_UART_INSTANCES);
751 static int alias_bitmap_initialized;
753 /* Stores actual bitmap of allocated IDs with alias IDs together */
754 static DECLARE_BITMAP(bitmap, MAX_UART_INSTANCES);
755 /* Protect bitmap operations to have unique IDs */
756 static DEFINE_MUTEX(bitmap_lock);
758 static int ulite_get_id(struct platform_device *pdev)
765 prop = of_get_property(pdev->dev.of_node, "port-number", NULL);
767 id = be32_to_cpup(prop);
770 mutex_lock(&bitmap_lock);
772 /* Alias list is stable that's why get alias bitmap only once */
773 if (!alias_bitmap_initialized) {
774 ret = of_alias_get_alias_list(ulite_of_match, "serial",
775 alias_bitmap, MAX_UART_INSTANCES);
777 mutex_unlock(&bitmap_lock);
781 alias_bitmap_initialized++;
784 /* Make sure that alias ID is not taken by instance without alias */
785 bitmap_or(bitmap, bitmap, alias_bitmap, MAX_UART_INSTANCES);
787 dev_dbg(&pdev->dev, "Alias bitmap: %*pb\n",
788 MAX_UART_INSTANCES, bitmap);
790 /* Look for a serialN alias */
792 id = of_alias_get_id(pdev->dev.of_node, "serial");
796 "No serial alias passed. Using the first free id\n");
799 * Start with id 0 and check if there is no serial0 alias
800 * which points to device which is compatible with this driver.
801 * If alias exists then try next free position.
806 dev_info(&pdev->dev, "Checking id %d\n", id);
807 id = find_next_zero_bit(bitmap, MAX_UART_INSTANCES, id);
809 /* No free empty instance */
810 if (id == MAX_UART_INSTANCES) {
811 dev_err(&pdev->dev, "No free ID\n");
812 mutex_unlock(&bitmap_lock);
816 dev_dbg(&pdev->dev, "The empty id is %d\n", id);
817 /* Check if ID is empty */
818 if (!test_and_set_bit(id, bitmap)) {
819 /* Break the loop if bit is taken */
821 "Selected ID %d allocation passed\n",
826 "Selected ID %d allocation failed\n", id);
827 /* if taking bit fails then try next one */
832 mutex_unlock(&bitmap_lock);
837 static int ulite_probe(struct platform_device *pdev)
839 struct resource *res;
840 struct uartlite_data *pdata;
842 struct uart_driver *ulite_uart_driver;
844 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
845 struct console *ulite_console;
848 pdata = devm_kzalloc(&pdev->dev, sizeof(struct uartlite_data),
853 ulite_uart_driver = devm_kzalloc(&pdev->dev,
854 sizeof(*ulite_uart_driver),
856 if (!ulite_uart_driver)
859 pdata->id = ulite_get_id(pdev);
863 /* There is a need to use unique driver name */
864 driver_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s%d",
865 ULITE_NAME, pdata->id);
871 ulite_uart_driver->owner = THIS_MODULE;
872 ulite_uart_driver->driver_name = driver_name;
873 ulite_uart_driver->dev_name = ULITE_NAME;
874 ulite_uart_driver->major = ULITE_MAJOR;
875 ulite_uart_driver->minor = pdata->id;
876 ulite_uart_driver->nr = 1;
877 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
878 ulite_console = devm_kzalloc(&pdev->dev, sizeof(*ulite_console),
880 if (!ulite_console) {
885 strncpy(ulite_console->name, ULITE_NAME,
886 sizeof(ulite_console->name));
887 ulite_console->index = pdata->id;
888 ulite_console->write = ulite_console_write;
889 ulite_console->device = uart_console_device;
890 ulite_console->setup = ulite_console_setup;
891 ulite_console->flags = CON_PRINTBUFFER;
892 ulite_uart_driver->cons = ulite_console;
893 ulite_console->data = ulite_uart_driver;
896 dev_dbg(&pdev->dev, "uartlite: calling uart_register_driver()\n");
897 ret = uart_register_driver(ulite_uart_driver);
899 dev_err(&pdev->dev, "Failed to register driver\n");
903 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
906 goto err_out_unregister_driver;
909 irq = platform_get_irq(pdev, 0);
912 goto err_out_unregister_driver;
915 pdata->clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
916 if (IS_ERR(pdata->clk)) {
917 if (PTR_ERR(pdata->clk) != -ENOENT) {
918 ret = PTR_ERR(pdata->clk);
919 goto err_out_unregister_driver;
923 * Clock framework support is optional, continue on
924 * anyways if we don't find a matching clock.
929 ret = clk_prepare_enable(pdata->clk);
931 dev_err(&pdev->dev, "Failed to prepare clock\n");
932 goto err_out_unregister_driver;
935 pm_runtime_use_autosuspend(&pdev->dev);
936 pm_runtime_set_autosuspend_delay(&pdev->dev, UART_AUTOSUSPEND_TIMEOUT);
937 pm_runtime_set_active(&pdev->dev);
938 pm_runtime_enable(&pdev->dev);
940 ulite_uart_driver->tty_driver->name_base = pdata->id;
941 pdata->ulite_uart_driver = ulite_uart_driver;
942 ret = ulite_assign(&pdev->dev, pdata->id, res->start, irq, pdata);
944 goto err_out_clk_disable;
946 pm_runtime_mark_last_busy(&pdev->dev);
947 pm_runtime_put_autosuspend(&pdev->dev);
951 clk_disable_unprepare(pdata->clk);
952 pm_runtime_disable(&pdev->dev);
953 pm_runtime_set_suspended(&pdev->dev);
954 pm_runtime_dont_use_autosuspend(&pdev->dev);
955 err_out_unregister_driver:
956 uart_unregister_driver(pdata->ulite_uart_driver);
958 mutex_lock(&bitmap_lock);
959 clear_bit(pdata->id, bitmap);
960 mutex_unlock(&bitmap_lock);
964 static int ulite_remove(struct platform_device *pdev)
966 struct uart_port *port = dev_get_drvdata(&pdev->dev);
967 struct uartlite_data *pdata = port->private_data;
970 clk_unprepare(pdata->clk);
971 rc = ulite_release(&pdev->dev);
972 mutex_lock(&bitmap_lock);
973 clear_bit(pdata->id, bitmap);
974 mutex_unlock(&bitmap_lock);
976 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
977 if (console_port == port)
981 uart_unregister_driver(pdata->ulite_uart_driver);
982 pm_runtime_disable(&pdev->dev);
983 pm_runtime_set_suspended(&pdev->dev);
984 pm_runtime_dont_use_autosuspend(&pdev->dev);
988 /* work with hotplug and coldplug */
989 MODULE_ALIAS("platform:uartlite");
991 static struct platform_driver ulite_platform_driver = {
992 .probe = ulite_probe,
993 .remove = ulite_remove,
996 .of_match_table = of_match_ptr(ulite_of_match),
1001 /* ---------------------------------------------------------------------
1002 * Module setup/teardown
1005 static int __init ulite_init(void)
1007 pr_debug("uartlite: calling platform_driver_register()\n");
1008 return platform_driver_register(&ulite_platform_driver);
1011 static void __exit ulite_exit(void)
1013 platform_driver_unregister(&ulite_platform_driver);
1016 module_init(ulite_init);
1017 module_exit(ulite_exit);
1019 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
1020 MODULE_DESCRIPTION("Xilinx uartlite serial driver");
1021 MODULE_LICENSE("GPL");