1 // SPDX-License-Identifier: GPL-2.0+
3 * Serial core port device driver
5 * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
6 * Author: Tony Lindgren <tony@atomide.com>
9 #include <linux/device.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/property.h>
15 #include <linux/serial_core.h>
16 #include <linux/spinlock.h>
18 #include "serial_base.h"
20 #define SERIAL_PORT_AUTOSUSPEND_DELAY_MS 500
22 /* Only considers pending TX for now. Caller must take care of locking */
23 static int __serial_port_busy(struct uart_port *port)
25 return !uart_tx_stopped(port) &&
26 uart_circ_chars_pending(&port->state->xmit);
29 static int serial_port_runtime_resume(struct device *dev)
31 struct serial_port_device *port_dev = to_serial_base_port_device(dev);
32 struct uart_port *port;
35 port = port_dev->port;
37 if (port->flags & UPF_DEAD)
40 /* Flush any pending TX for the port */
41 uart_port_lock_irqsave(port, &flags);
42 if (__serial_port_busy(port))
43 port->ops->start_tx(port);
44 uart_port_unlock_irqrestore(port, flags);
47 pm_runtime_mark_last_busy(dev);
52 static int serial_port_runtime_suspend(struct device *dev)
54 struct serial_port_device *port_dev = to_serial_base_port_device(dev);
55 struct uart_port *port = port_dev->port;
59 if (port->flags & UPF_DEAD)
62 uart_port_lock_irqsave(port, &flags);
63 busy = __serial_port_busy(port);
65 port->ops->start_tx(port);
66 uart_port_unlock_irqrestore(port, flags);
69 pm_runtime_mark_last_busy(dev);
71 return busy ? -EBUSY : 0;
74 static DEFINE_RUNTIME_DEV_PM_OPS(serial_port_pm,
75 serial_port_runtime_suspend,
76 serial_port_runtime_resume, NULL);
78 static int serial_port_probe(struct device *dev)
80 pm_runtime_enable(dev);
81 pm_runtime_set_autosuspend_delay(dev, SERIAL_PORT_AUTOSUSPEND_DELAY_MS);
82 pm_runtime_use_autosuspend(dev);
87 static int serial_port_remove(struct device *dev)
89 pm_runtime_dont_use_autosuspend(dev);
90 pm_runtime_disable(dev);
96 * Serial core port device init functions. Note that the physical serial
97 * port device driver may not have completed probe at this point.
99 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
101 return serial_ctrl_register_port(drv, port);
103 EXPORT_SYMBOL(uart_add_one_port);
105 void uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
107 serial_ctrl_unregister_port(drv, port);
109 EXPORT_SYMBOL(uart_remove_one_port);
112 * __uart_read_properties - read firmware properties of the given UART port
113 * @port: corresponding port
114 * @use_defaults: apply defaults (when %true) or validate the values (when %false)
116 * The following device properties are supported:
117 * - clock-frequency (optional)
118 * - fifo-size (optional)
119 * - no-loopback-test (optional)
120 * - reg-shift (defaults may apply)
121 * - reg-offset (value may be validated)
122 * - reg-io-width (defaults may apply or value may be validated)
123 * - interrupts (OF only)
124 * - serial [alias ID] (OF only)
126 * If the port->dev is of struct platform_device type the interrupt line
127 * will be retrieved via platform_get_irq() call against that device.
128 * Otherwise it will be assigned by fwnode_irq_get() call. In both cases
129 * the index 0 of the resource is used.
131 * The caller is responsible to initialize the following fields of the @port
132 * ->dev (must be valid)
136 * ->regshift (if @use_defaults is false)
137 * before calling this function. Alternatively the above mentioned fields
138 * may be zeroed, in such case the only ones, that have associated properties
139 * found, will be set to the respective values.
141 * If no error happened, the ->irq, ->mapbase, ->mapsize will be altered.
142 * The ->iotype is always altered.
144 * When @use_defaults is true and the respective property is not found
145 * the following values will be applied:
147 * In this case IRQ must be provided, otherwise an error will be returned.
149 * When @use_defaults is false and the respective property is found
150 * the following values will be validated:
151 * - reg-io-width (->iotype)
152 * - reg-offset (->mapsize against ->mapbase)
154 * Returns: 0 on success or negative errno on failure
156 static int __uart_read_properties(struct uart_port *port, bool use_defaults)
158 struct device *dev = port->dev;
162 /* Read optional UART functional clock frequency */
163 device_property_read_u32(dev, "clock-frequency", &port->uartclk);
165 /* Read the registers alignment (default: 8-bit) */
166 ret = device_property_read_u32(dev, "reg-shift", &value);
168 port->regshift = use_defaults ? 0 : port->regshift;
170 port->regshift = value;
172 /* Read the registers I/O access type (default: MMIO 8-bit) */
173 ret = device_property_read_u32(dev, "reg-io-width", &value);
175 port->iotype = UPIO_MEM;
179 port->iotype = UPIO_MEM;
182 port->iotype = UPIO_MEM16;
185 port->iotype = device_is_big_endian(dev) ? UPIO_MEM32BE : UPIO_MEM32;
189 dev_err(dev, "Unsupported reg-io-width (%u)\n", value);
192 port->iotype = UPIO_UNKNOWN;
197 /* Read the address mapping base offset (default: no offset) */
198 ret = device_property_read_u32(dev, "reg-offset", &value);
202 /* Check for shifted address mapping overflow */
203 if (!use_defaults && port->mapsize < value) {
204 dev_err(dev, "reg-offset %u exceeds region size %pa\n", value, &port->mapsize);
208 port->mapbase += value;
209 port->mapsize -= value;
211 /* Read optional FIFO size */
212 device_property_read_u32(dev, "fifo-size", &port->fifosize);
214 if (device_property_read_bool(dev, "no-loopback-test"))
215 port->flags |= UPF_SKIP_TEST;
217 /* Get index of serial line, if found in DT aliases */
218 ret = of_alias_get_id(dev_of_node(dev), "serial");
222 if (dev_is_platform(dev))
223 ret = platform_get_irq(to_platform_device(dev), 0);
225 ret = fwnode_irq_get(dev_fwnode(dev), 0);
226 if (ret == -EPROBE_DEFER)
230 else if (use_defaults)
231 /* By default IRQ support is mandatory */
236 port->flags |= UPF_SHARE_IRQ;
241 int uart_read_port_properties(struct uart_port *port)
243 return __uart_read_properties(port, true);
245 EXPORT_SYMBOL_GPL(uart_read_port_properties);
247 int uart_read_and_validate_port_properties(struct uart_port *port)
249 return __uart_read_properties(port, false);
251 EXPORT_SYMBOL_GPL(uart_read_and_validate_port_properties);
253 static struct device_driver serial_port_driver = {
255 .suppress_bind_attrs = true,
256 .probe = serial_port_probe,
257 .remove = serial_port_remove,
258 .pm = pm_ptr(&serial_port_pm),
261 int serial_base_port_init(void)
263 return serial_base_driver_register(&serial_port_driver);
266 void serial_base_port_exit(void)
268 serial_base_driver_unregister(&serial_port_driver);
271 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
272 MODULE_DESCRIPTION("Serial controller port driver");
273 MODULE_LICENSE("GPL");