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/pnp.h>
15 #include <linux/property.h>
16 #include <linux/serial_core.h>
17 #include <linux/spinlock.h>
19 #include "serial_base.h"
21 #define SERIAL_PORT_AUTOSUSPEND_DELAY_MS 500
23 /* Only considers pending TX for now. Caller must take care of locking */
24 static int __serial_port_busy(struct uart_port
*port
)
26 return !uart_tx_stopped(port
) &&
27 !kfifo_is_empty(&port
->state
->port
.xmit_fifo
);
30 static int serial_port_runtime_resume(struct device
*dev
)
32 struct serial_port_device
*port_dev
= to_serial_base_port_device(dev
);
33 struct uart_port
*port
;
36 port
= port_dev
->port
;
38 if (port
->flags
& UPF_DEAD
)
41 /* Flush any pending TX for the port */
42 uart_port_lock_irqsave(port
, &flags
);
43 if (!port_dev
->tx_enabled
)
45 if (__serial_port_busy(port
))
46 port
->ops
->start_tx(port
);
49 uart_port_unlock_irqrestore(port
, flags
);
52 pm_runtime_mark_last_busy(dev
);
57 static int serial_port_runtime_suspend(struct device
*dev
)
59 struct serial_port_device
*port_dev
= to_serial_base_port_device(dev
);
60 struct uart_port
*port
= port_dev
->port
;
64 if (port
->flags
& UPF_DEAD
)
68 * Nothing to do on pm_runtime_force_suspend(), see
69 * DEFINE_RUNTIME_DEV_PM_OPS.
71 if (!pm_runtime_enabled(dev
))
74 uart_port_lock_irqsave(port
, &flags
);
75 if (!port_dev
->tx_enabled
) {
76 uart_port_unlock_irqrestore(port
, flags
);
80 busy
= __serial_port_busy(port
);
82 port
->ops
->start_tx(port
);
83 uart_port_unlock_irqrestore(port
, flags
);
86 pm_runtime_mark_last_busy(dev
);
88 return busy
? -EBUSY
: 0;
91 static void serial_base_port_set_tx(struct uart_port
*port
,
92 struct serial_port_device
*port_dev
,
97 uart_port_lock_irqsave(port
, &flags
);
98 port_dev
->tx_enabled
= enabled
;
99 uart_port_unlock_irqrestore(port
, flags
);
102 void serial_base_port_startup(struct uart_port
*port
)
104 struct serial_port_device
*port_dev
= port
->port_dev
;
106 serial_base_port_set_tx(port
, port_dev
, true);
109 void serial_base_port_shutdown(struct uart_port
*port
)
111 struct serial_port_device
*port_dev
= port
->port_dev
;
113 serial_base_port_set_tx(port
, port_dev
, false);
116 static DEFINE_RUNTIME_DEV_PM_OPS(serial_port_pm
,
117 serial_port_runtime_suspend
,
118 serial_port_runtime_resume
, NULL
);
120 static int serial_port_probe(struct device
*dev
)
122 pm_runtime_enable(dev
);
123 pm_runtime_set_autosuspend_delay(dev
, SERIAL_PORT_AUTOSUSPEND_DELAY_MS
);
124 pm_runtime_use_autosuspend(dev
);
129 static int serial_port_remove(struct device
*dev
)
131 pm_runtime_dont_use_autosuspend(dev
);
132 pm_runtime_disable(dev
);
138 * Serial core port device init functions. Note that the physical serial
139 * port device driver may not have completed probe at this point.
141 int uart_add_one_port(struct uart_driver
*drv
, struct uart_port
*port
)
143 return serial_ctrl_register_port(drv
, port
);
145 EXPORT_SYMBOL(uart_add_one_port
);
147 void uart_remove_one_port(struct uart_driver
*drv
, struct uart_port
*port
)
149 serial_ctrl_unregister_port(drv
, port
);
151 EXPORT_SYMBOL(uart_remove_one_port
);
154 * __uart_read_properties - read firmware properties of the given UART port
155 * @port: corresponding port
156 * @use_defaults: apply defaults (when %true) or validate the values (when %false)
158 * The following device properties are supported:
159 * - clock-frequency (optional)
160 * - fifo-size (optional)
161 * - no-loopback-test (optional)
162 * - reg-shift (defaults may apply)
163 * - reg-offset (value may be validated)
164 * - reg-io-width (defaults may apply or value may be validated)
165 * - interrupts (OF only)
166 * - serial [alias ID] (OF only)
168 * If the port->dev is of struct platform_device type the interrupt line
169 * will be retrieved via platform_get_irq() call against that device.
170 * Otherwise it will be assigned by fwnode_irq_get() call. In both cases
171 * the index 0 of the resource is used.
173 * The caller is responsible to initialize the following fields of the @port
174 * ->dev (must be valid)
178 * ->regshift (if @use_defaults is false)
179 * before calling this function. Alternatively the above mentioned fields
180 * may be zeroed, in such case the only ones, that have associated properties
181 * found, will be set to the respective values.
183 * If no error happened, the ->irq, ->mapbase, ->mapsize will be altered.
184 * The ->iotype is always altered.
186 * When @use_defaults is true and the respective property is not found
187 * the following values will be applied:
189 * In this case IRQ must be provided, otherwise an error will be returned.
191 * When @use_defaults is false and the respective property is found
192 * the following values will be validated:
193 * - reg-io-width (->iotype)
194 * - reg-offset (->mapsize against ->mapbase)
196 * Returns: 0 on success or negative errno on failure
198 static int __uart_read_properties(struct uart_port
*port
, bool use_defaults
)
200 struct device
*dev
= port
->dev
;
204 /* Read optional UART functional clock frequency */
205 device_property_read_u32(dev
, "clock-frequency", &port
->uartclk
);
207 /* Read the registers alignment (default: 8-bit) */
208 ret
= device_property_read_u32(dev
, "reg-shift", &value
);
210 port
->regshift
= use_defaults
? 0 : port
->regshift
;
212 port
->regshift
= value
;
214 /* Read the registers I/O access type (default: MMIO 8-bit) */
215 ret
= device_property_read_u32(dev
, "reg-io-width", &value
);
217 port
->iotype
= UPIO_MEM
;
221 port
->iotype
= UPIO_MEM
;
224 port
->iotype
= UPIO_MEM16
;
227 port
->iotype
= device_is_big_endian(dev
) ? UPIO_MEM32BE
: UPIO_MEM32
;
231 dev_err(dev
, "Unsupported reg-io-width (%u)\n", value
);
234 port
->iotype
= UPIO_UNKNOWN
;
239 /* Read the address mapping base offset (default: no offset) */
240 ret
= device_property_read_u32(dev
, "reg-offset", &value
);
244 /* Check for shifted address mapping overflow */
245 if (!use_defaults
&& port
->mapsize
< value
) {
246 dev_err(dev
, "reg-offset %u exceeds region size %pa\n", value
, &port
->mapsize
);
250 port
->mapbase
+= value
;
251 port
->mapsize
-= value
;
253 /* Read optional FIFO size */
254 device_property_read_u32(dev
, "fifo-size", &port
->fifosize
);
256 if (device_property_read_bool(dev
, "no-loopback-test"))
257 port
->flags
|= UPF_SKIP_TEST
;
259 /* Get index of serial line, if found in DT aliases */
260 ret
= of_alias_get_id(dev_of_node(dev
), "serial");
264 if (dev_is_platform(dev
))
265 ret
= platform_get_irq(to_platform_device(dev
), 0);
266 else if (dev_is_pnp(dev
)) {
267 ret
= pnp_irq(to_pnp_dev(dev
), 0);
271 ret
= fwnode_irq_get(dev_fwnode(dev
), 0);
272 if (ret
== -EPROBE_DEFER
)
276 else if (use_defaults
)
277 /* By default IRQ support is mandatory */
282 port
->flags
|= UPF_SHARE_IRQ
;
287 int uart_read_port_properties(struct uart_port
*port
)
289 return __uart_read_properties(port
, true);
291 EXPORT_SYMBOL_GPL(uart_read_port_properties
);
293 int uart_read_and_validate_port_properties(struct uart_port
*port
)
295 return __uart_read_properties(port
, false);
297 EXPORT_SYMBOL_GPL(uart_read_and_validate_port_properties
);
299 static struct device_driver serial_port_driver
= {
301 .suppress_bind_attrs
= true,
302 .probe
= serial_port_probe
,
303 .remove
= serial_port_remove
,
304 .pm
= pm_ptr(&serial_port_pm
),
307 int serial_base_port_init(void)
309 return serial_base_driver_register(&serial_port_driver
);
312 void serial_base_port_exit(void)
314 serial_base_driver_unregister(&serial_port_driver
);
317 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
318 MODULE_DESCRIPTION("Serial controller port driver");
319 MODULE_LICENSE("GPL");