xref: /openbmc/linux/drivers/tty/serial/serial_port.c (revision 93696d8f)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Serial core port device driver
4  *
5  * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
6  * Author: Tony Lindgren <tony@atomide.com>
7  */
8 
9 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/of.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>
17 
18 #include "serial_base.h"
19 
20 #define SERIAL_PORT_AUTOSUSPEND_DELAY_MS	500
21 
22 /* Only considers pending TX for now. Caller must take care of locking */
23 static int __serial_port_busy(struct uart_port *port)
24 {
25 	return !uart_tx_stopped(port) &&
26 		uart_circ_chars_pending(&port->state->xmit);
27 }
28 
29 static int serial_port_runtime_resume(struct device *dev)
30 {
31 	struct serial_port_device *port_dev = to_serial_base_port_device(dev);
32 	struct uart_port *port;
33 	unsigned long flags;
34 
35 	port = port_dev->port;
36 
37 	if (port->flags & UPF_DEAD)
38 		goto out;
39 
40 	/* Flush any pending TX for the port */
41 	spin_lock_irqsave(&port->lock, flags);
42 	if (!port_dev->tx_enabled)
43 		goto unlock;
44 	if (__serial_port_busy(port))
45 		port->ops->start_tx(port);
46 
47 unlock:
48 	spin_unlock_irqrestore(&port->lock, flags);
49 
50 out:
51 	pm_runtime_mark_last_busy(dev);
52 
53 	return 0;
54 }
55 
56 static int serial_port_runtime_suspend(struct device *dev)
57 {
58 	struct serial_port_device *port_dev = to_serial_base_port_device(dev);
59 	struct uart_port *port = port_dev->port;
60 	unsigned long flags;
61 	bool busy;
62 
63 	if (port->flags & UPF_DEAD)
64 		return 0;
65 
66 	/*
67 	 * Nothing to do on pm_runtime_force_suspend(), see
68 	 * DEFINE_RUNTIME_DEV_PM_OPS.
69 	 */
70 	if (!pm_runtime_enabled(dev))
71 		return 0;
72 
73 	uart_port_lock_irqsave(port, &flags);
74 	if (!port_dev->tx_enabled) {
75 		uart_port_unlock_irqrestore(port, flags);
76 		return 0;
77 	}
78 
79 	busy = __serial_port_busy(port);
80 	if (busy)
81 		port->ops->start_tx(port);
82 	uart_port_unlock_irqrestore(port, flags);
83 
84 	if (busy)
85 		pm_runtime_mark_last_busy(dev);
86 
87 	return busy ? -EBUSY : 0;
88 }
89 
90 static void serial_base_port_set_tx(struct uart_port *port,
91 				    struct serial_port_device *port_dev,
92 				    bool enabled)
93 {
94 	unsigned long flags;
95 
96 	uart_port_lock_irqsave(port, &flags);
97 	port_dev->tx_enabled = enabled;
98 	uart_port_unlock_irqrestore(port, flags);
99 }
100 
101 void serial_base_port_startup(struct uart_port *port)
102 {
103 	struct serial_port_device *port_dev = port->port_dev;
104 
105 	serial_base_port_set_tx(port, port_dev, true);
106 }
107 
108 void serial_base_port_shutdown(struct uart_port *port)
109 {
110 	struct serial_port_device *port_dev = port->port_dev;
111 
112 	serial_base_port_set_tx(port, port_dev, false);
113 }
114 
115 static DEFINE_RUNTIME_DEV_PM_OPS(serial_port_pm,
116 				 serial_port_runtime_suspend,
117 				 serial_port_runtime_resume, NULL);
118 
119 static int serial_port_probe(struct device *dev)
120 {
121 	pm_runtime_enable(dev);
122 	pm_runtime_set_autosuspend_delay(dev, SERIAL_PORT_AUTOSUSPEND_DELAY_MS);
123 	pm_runtime_use_autosuspend(dev);
124 
125 	return 0;
126 }
127 
128 static int serial_port_remove(struct device *dev)
129 {
130 	pm_runtime_dont_use_autosuspend(dev);
131 	pm_runtime_disable(dev);
132 
133 	return 0;
134 }
135 
136 /*
137  * Serial core port device init functions. Note that the physical serial
138  * port device driver may not have completed probe at this point.
139  */
140 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
141 {
142 	return serial_ctrl_register_port(drv, port);
143 }
144 EXPORT_SYMBOL(uart_add_one_port);
145 
146 void uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
147 {
148 	serial_ctrl_unregister_port(drv, port);
149 }
150 EXPORT_SYMBOL(uart_remove_one_port);
151 
152 /**
153  * __uart_read_properties - read firmware properties of the given UART port
154  * @port: corresponding port
155  * @use_defaults: apply defaults (when %true) or validate the values (when %false)
156  *
157  * The following device properties are supported:
158  *   - clock-frequency (optional)
159  *   - fifo-size (optional)
160  *   - no-loopback-test (optional)
161  *   - reg-shift (defaults may apply)
162  *   - reg-offset (value may be validated)
163  *   - reg-io-width (defaults may apply or value may be validated)
164  *   - interrupts (OF only)
165  *   - serial [alias ID] (OF only)
166  *
167  * If the port->dev is of struct platform_device type the interrupt line
168  * will be retrieved via platform_get_irq() call against that device.
169  * Otherwise it will be assigned by fwnode_irq_get() call. In both cases
170  * the index 0 of the resource is used.
171  *
172  * The caller is responsible to initialize the following fields of the @port
173  *   ->dev (must be valid)
174  *   ->flags
175  *   ->mapbase
176  *   ->mapsize
177  *   ->regshift (if @use_defaults is false)
178  * before calling this function. Alternatively the above mentioned fields
179  * may be zeroed, in such case the only ones, that have associated properties
180  * found, will be set to the respective values.
181  *
182  * If no error happened, the ->irq, ->mapbase, ->mapsize will be altered.
183  * The ->iotype is always altered.
184  *
185  * When @use_defaults is true and the respective property is not found
186  * the following values will be applied:
187  *   ->regshift = 0
188  * In this case IRQ must be provided, otherwise an error will be returned.
189  *
190  * When @use_defaults is false and the respective property is found
191  * the following values will be validated:
192  *   - reg-io-width (->iotype)
193  *   - reg-offset (->mapsize against ->mapbase)
194  *
195  * Returns: 0 on success or negative errno on failure
196  */
197 static int __uart_read_properties(struct uart_port *port, bool use_defaults)
198 {
199 	struct device *dev = port->dev;
200 	u32 value;
201 	int ret;
202 
203 	/* Read optional UART functional clock frequency */
204 	device_property_read_u32(dev, "clock-frequency", &port->uartclk);
205 
206 	/* Read the registers alignment (default: 8-bit) */
207 	ret = device_property_read_u32(dev, "reg-shift", &value);
208 	if (ret)
209 		port->regshift = use_defaults ? 0 : port->regshift;
210 	else
211 		port->regshift = value;
212 
213 	/* Read the registers I/O access type (default: MMIO 8-bit) */
214 	ret = device_property_read_u32(dev, "reg-io-width", &value);
215 	if (ret) {
216 		port->iotype = UPIO_MEM;
217 	} else {
218 		switch (value) {
219 		case 1:
220 			port->iotype = UPIO_MEM;
221 			break;
222 		case 2:
223 			port->iotype = UPIO_MEM16;
224 			break;
225 		case 4:
226 			port->iotype = device_is_big_endian(dev) ? UPIO_MEM32BE : UPIO_MEM32;
227 			break;
228 		default:
229 			if (!use_defaults) {
230 				dev_err(dev, "Unsupported reg-io-width (%u)\n", value);
231 				return -EINVAL;
232 			}
233 			port->iotype = UPIO_UNKNOWN;
234 			break;
235 		}
236 	}
237 
238 	/* Read the address mapping base offset (default: no offset) */
239 	ret = device_property_read_u32(dev, "reg-offset", &value);
240 	if (ret)
241 		value = 0;
242 
243 	/* Check for shifted address mapping overflow */
244 	if (!use_defaults && port->mapsize < value) {
245 		dev_err(dev, "reg-offset %u exceeds region size %pa\n", value, &port->mapsize);
246 		return -EINVAL;
247 	}
248 
249 	port->mapbase += value;
250 	port->mapsize -= value;
251 
252 	/* Read optional FIFO size */
253 	device_property_read_u32(dev, "fifo-size", &port->fifosize);
254 
255 	if (device_property_read_bool(dev, "no-loopback-test"))
256 		port->flags |= UPF_SKIP_TEST;
257 
258 	/* Get index of serial line, if found in DT aliases */
259 	ret = of_alias_get_id(dev_of_node(dev), "serial");
260 	if (ret >= 0)
261 		port->line = ret;
262 
263 	if (dev_is_platform(dev))
264 		ret = platform_get_irq(to_platform_device(dev), 0);
265 	else
266 		ret = fwnode_irq_get(dev_fwnode(dev), 0);
267 	if (ret == -EPROBE_DEFER)
268 		return ret;
269 	if (ret > 0)
270 		port->irq = ret;
271 	else if (use_defaults)
272 		/* By default IRQ support is mandatory */
273 		return ret;
274 	else
275 		port->irq = 0;
276 
277 	port->flags |= UPF_SHARE_IRQ;
278 
279 	return 0;
280 }
281 
282 int uart_read_port_properties(struct uart_port *port)
283 {
284 	return __uart_read_properties(port, true);
285 }
286 EXPORT_SYMBOL_GPL(uart_read_port_properties);
287 
288 int uart_read_and_validate_port_properties(struct uart_port *port)
289 {
290 	return __uart_read_properties(port, false);
291 }
292 EXPORT_SYMBOL_GPL(uart_read_and_validate_port_properties);
293 
294 static struct device_driver serial_port_driver = {
295 	.name = "port",
296 	.suppress_bind_attrs = true,
297 	.probe = serial_port_probe,
298 	.remove = serial_port_remove,
299 	.pm = pm_ptr(&serial_port_pm),
300 };
301 
302 int serial_base_port_init(void)
303 {
304 	return serial_base_driver_register(&serial_port_driver);
305 }
306 
307 void serial_base_port_exit(void)
308 {
309 	serial_base_driver_unregister(&serial_port_driver);
310 }
311 
312 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
313 MODULE_DESCRIPTION("Serial controller port driver");
314 MODULE_LICENSE("GPL");
315