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 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); 45 46 out: 47 pm_runtime_mark_last_busy(dev); 48 49 return 0; 50 } 51 52 static int serial_port_runtime_suspend(struct device *dev) 53 { 54 struct serial_port_device *port_dev = to_serial_base_port_device(dev); 55 struct uart_port *port = port_dev->port; 56 unsigned long flags; 57 bool busy; 58 59 if (port->flags & UPF_DEAD) 60 return 0; 61 62 uart_port_lock_irqsave(port, &flags); 63 busy = __serial_port_busy(port); 64 if (busy) 65 port->ops->start_tx(port); 66 uart_port_unlock_irqrestore(port, flags); 67 68 if (busy) 69 pm_runtime_mark_last_busy(dev); 70 71 return busy ? -EBUSY : 0; 72 } 73 74 static DEFINE_RUNTIME_DEV_PM_OPS(serial_port_pm, 75 serial_port_runtime_suspend, 76 serial_port_runtime_resume, NULL); 77 78 static int serial_port_probe(struct device *dev) 79 { 80 pm_runtime_enable(dev); 81 pm_runtime_set_autosuspend_delay(dev, SERIAL_PORT_AUTOSUSPEND_DELAY_MS); 82 pm_runtime_use_autosuspend(dev); 83 84 return 0; 85 } 86 87 static int serial_port_remove(struct device *dev) 88 { 89 pm_runtime_dont_use_autosuspend(dev); 90 pm_runtime_disable(dev); 91 92 return 0; 93 } 94 95 /* 96 * Serial core port device init functions. Note that the physical serial 97 * port device driver may not have completed probe at this point. 98 */ 99 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port) 100 { 101 return serial_ctrl_register_port(drv, port); 102 } 103 EXPORT_SYMBOL(uart_add_one_port); 104 105 void uart_remove_one_port(struct uart_driver *drv, struct uart_port *port) 106 { 107 serial_ctrl_unregister_port(drv, port); 108 } 109 EXPORT_SYMBOL(uart_remove_one_port); 110 111 /** 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) 115 * 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) 125 * 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. 130 * 131 * The caller is responsible to initialize the following fields of the @port 132 * ->dev (must be valid) 133 * ->flags 134 * ->mapbase 135 * ->mapsize 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. 140 * 141 * If no error happened, the ->irq, ->mapbase, ->mapsize will be altered. 142 * The ->iotype is always altered. 143 * 144 * When @use_defaults is true and the respective property is not found 145 * the following values will be applied: 146 * ->regshift = 0 147 * In this case IRQ must be provided, otherwise an error will be returned. 148 * 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) 153 * 154 * Returns: 0 on success or negative errno on failure 155 */ 156 static int __uart_read_properties(struct uart_port *port, bool use_defaults) 157 { 158 struct device *dev = port->dev; 159 u32 value; 160 int ret; 161 162 /* Read optional UART functional clock frequency */ 163 device_property_read_u32(dev, "clock-frequency", &port->uartclk); 164 165 /* Read the registers alignment (default: 8-bit) */ 166 ret = device_property_read_u32(dev, "reg-shift", &value); 167 if (ret) 168 port->regshift = use_defaults ? 0 : port->regshift; 169 else 170 port->regshift = value; 171 172 /* Read the registers I/O access type (default: MMIO 8-bit) */ 173 ret = device_property_read_u32(dev, "reg-io-width", &value); 174 if (ret) { 175 port->iotype = UPIO_MEM; 176 } else { 177 switch (value) { 178 case 1: 179 port->iotype = UPIO_MEM; 180 break; 181 case 2: 182 port->iotype = UPIO_MEM16; 183 break; 184 case 4: 185 port->iotype = device_is_big_endian(dev) ? UPIO_MEM32BE : UPIO_MEM32; 186 break; 187 default: 188 if (!use_defaults) { 189 dev_err(dev, "Unsupported reg-io-width (%u)\n", value); 190 return -EINVAL; 191 } 192 port->iotype = UPIO_UNKNOWN; 193 break; 194 } 195 } 196 197 /* Read the address mapping base offset (default: no offset) */ 198 ret = device_property_read_u32(dev, "reg-offset", &value); 199 if (ret) 200 value = 0; 201 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); 205 return -EINVAL; 206 } 207 208 port->mapbase += value; 209 port->mapsize -= value; 210 211 /* Read optional FIFO size */ 212 device_property_read_u32(dev, "fifo-size", &port->fifosize); 213 214 if (device_property_read_bool(dev, "no-loopback-test")) 215 port->flags |= UPF_SKIP_TEST; 216 217 /* Get index of serial line, if found in DT aliases */ 218 ret = of_alias_get_id(dev_of_node(dev), "serial"); 219 if (ret >= 0) 220 port->line = ret; 221 222 if (dev_is_platform(dev)) 223 ret = platform_get_irq(to_platform_device(dev), 0); 224 else 225 ret = fwnode_irq_get(dev_fwnode(dev), 0); 226 if (ret == -EPROBE_DEFER) 227 return ret; 228 if (ret > 0) 229 port->irq = ret; 230 else if (use_defaults) 231 /* By default IRQ support is mandatory */ 232 return ret; 233 else 234 port->irq = 0; 235 236 port->flags |= UPF_SHARE_IRQ; 237 238 return 0; 239 } 240 241 int uart_read_port_properties(struct uart_port *port) 242 { 243 return __uart_read_properties(port, true); 244 } 245 EXPORT_SYMBOL_GPL(uart_read_port_properties); 246 247 int uart_read_and_validate_port_properties(struct uart_port *port) 248 { 249 return __uart_read_properties(port, false); 250 } 251 EXPORT_SYMBOL_GPL(uart_read_and_validate_port_properties); 252 253 static struct device_driver serial_port_driver = { 254 .name = "port", 255 .suppress_bind_attrs = true, 256 .probe = serial_port_probe, 257 .remove = serial_port_remove, 258 .pm = pm_ptr(&serial_port_pm), 259 }; 260 261 int serial_base_port_init(void) 262 { 263 return serial_base_driver_register(&serial_port_driver); 264 } 265 266 void serial_base_port_exit(void) 267 { 268 serial_base_driver_unregister(&serial_port_driver); 269 } 270 271 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); 272 MODULE_DESCRIPTION("Serial controller port driver"); 273 MODULE_LICENSE("GPL"); 274