1 // SPDX-License-Identifier: GPL-2.0+
3 * Serial Port driver for Tegra devices
5 * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
8 #include <linux/acpi.h>
10 #include <linux/console.h>
11 #include <linux/delay.h>
13 #include <linux/module.h>
14 #include <linux/reset.h>
15 #include <linux/slab.h>
21 struct reset_control *rst;
25 static void tegra_uart_handle_break(struct uart_port *p)
27 unsigned int status, tmout = 10000;
30 status = p->serial_in(p, UART_LSR);
31 if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
32 status = p->serial_in(p, UART_RX);
41 static int tegra_uart_probe(struct platform_device *pdev)
43 struct uart_8250_port port8250;
44 struct tegra_uart *uart;
45 struct uart_port *port;
49 uart = devm_kzalloc(&pdev->dev, sizeof(*uart), GFP_KERNEL);
53 memset(&port8250, 0, sizeof(port8250));
55 port = &port8250.port;
56 spin_lock_init(&port->lock);
58 port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT |
60 port->iotype = UPIO_MEM32;
62 port->type = PORT_TEGRA;
63 port->irqflags |= IRQF_SHARED;
64 port->dev = &pdev->dev;
65 port->handle_break = tegra_uart_handle_break;
67 ret = of_alias_get_id(pdev->dev.of_node, "serial");
71 ret = platform_get_irq(pdev, 0);
77 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
81 port->membase = devm_ioremap(&pdev->dev, res->start,
86 port->mapbase = res->start;
87 port->mapsize = resource_size(res);
89 uart->rst = devm_reset_control_get_optional_shared(&pdev->dev, NULL);
90 if (IS_ERR(uart->rst))
91 return PTR_ERR(uart->rst);
93 if (device_property_read_u32(&pdev->dev, "clock-frequency",
95 uart->clk = devm_clk_get(&pdev->dev, NULL);
96 if (IS_ERR(uart->clk)) {
97 dev_err(&pdev->dev, "failed to get clock!\n");
101 ret = clk_prepare_enable(uart->clk);
105 port->uartclk = clk_get_rate(uart->clk);
108 ret = reset_control_deassert(uart->rst);
112 ret = serial8250_register_8250_port(&port8250);
114 goto err_ctrl_assert;
116 platform_set_drvdata(pdev, uart);
122 reset_control_assert(uart->rst);
124 clk_disable_unprepare(uart->clk);
129 static int tegra_uart_remove(struct platform_device *pdev)
131 struct tegra_uart *uart = platform_get_drvdata(pdev);
133 serial8250_unregister_port(uart->line);
134 reset_control_assert(uart->rst);
135 clk_disable_unprepare(uart->clk);
140 #ifdef CONFIG_PM_SLEEP
141 static int tegra_uart_suspend(struct device *dev)
143 struct tegra_uart *uart = dev_get_drvdata(dev);
144 struct uart_8250_port *port8250 = serial8250_get_port(uart->line);
145 struct uart_port *port = &port8250->port;
147 serial8250_suspend_port(uart->line);
149 if (!uart_console(port) || console_suspend_enabled)
150 clk_disable_unprepare(uart->clk);
155 static int tegra_uart_resume(struct device *dev)
157 struct tegra_uart *uart = dev_get_drvdata(dev);
158 struct uart_8250_port *port8250 = serial8250_get_port(uart->line);
159 struct uart_port *port = &port8250->port;
161 if (!uart_console(port) || console_suspend_enabled)
162 clk_prepare_enable(uart->clk);
164 serial8250_resume_port(uart->line);
170 static SIMPLE_DEV_PM_OPS(tegra_uart_pm_ops, tegra_uart_suspend,
173 static const struct of_device_id tegra_uart_of_match[] = {
174 { .compatible = "nvidia,tegra20-uart", },
177 MODULE_DEVICE_TABLE(of, tegra_uart_of_match);
179 static const struct acpi_device_id tegra_uart_acpi_match[] = {
183 MODULE_DEVICE_TABLE(acpi, tegra_uart_acpi_match);
185 static struct platform_driver tegra_uart_driver = {
187 .name = "tegra-uart",
188 .pm = &tegra_uart_pm_ops,
189 .of_match_table = tegra_uart_of_match,
190 .acpi_match_table = ACPI_PTR(tegra_uart_acpi_match),
192 .probe = tegra_uart_probe,
193 .remove = tegra_uart_remove,
196 module_platform_driver(tegra_uart_driver);
198 MODULE_AUTHOR("Jeff Brasen <jbrasen@nvidia.com>");
199 MODULE_DESCRIPTION("NVIDIA Tegra 8250 Driver");
200 MODULE_LICENSE("GPL v2");