| 1 | // SPDX-License-Identifier: GPL-2.0+ | 
|---|
| 2 | /* | 
|---|
| 3 | * Serial base bus layer for controllers | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/ | 
|---|
| 6 | * Author: Tony Lindgren <tony@atomide.com> | 
|---|
| 7 | * | 
|---|
| 8 | * The serial core bus manages the serial core controller instances. | 
|---|
| 9 | */ | 
|---|
| 10 |  | 
|---|
| 11 | #include <linux/cleanup.h> | 
|---|
| 12 | #include <linux/container_of.h> | 
|---|
| 13 | #include <linux/device.h> | 
|---|
| 14 | #include <linux/idr.h> | 
|---|
| 15 | #include <linux/module.h> | 
|---|
| 16 | #include <linux/of.h> | 
|---|
| 17 | #include <linux/serial_core.h> | 
|---|
| 18 | #include <linux/slab.h> | 
|---|
| 19 | #include <linux/spinlock.h> | 
|---|
| 20 |  | 
|---|
| 21 | #include "serial_base.h" | 
|---|
| 22 |  | 
|---|
| 23 | static bool serial_base_initialized; | 
|---|
| 24 |  | 
|---|
| 25 | static const struct device_type serial_ctrl_type = { | 
|---|
| 26 | .name = "ctrl", | 
|---|
| 27 | }; | 
|---|
| 28 |  | 
|---|
| 29 | static const struct device_type serial_port_type = { | 
|---|
| 30 | .name = "port", | 
|---|
| 31 | }; | 
|---|
| 32 |  | 
|---|
| 33 | static int serial_base_match(struct device *dev, const struct device_driver *drv) | 
|---|
| 34 | { | 
|---|
| 35 | if (dev->type == &serial_ctrl_type && | 
|---|
| 36 | str_has_prefix(str: drv->name, prefix: serial_ctrl_type.name)) | 
|---|
| 37 | return 1; | 
|---|
| 38 |  | 
|---|
| 39 | if (dev->type == &serial_port_type && | 
|---|
| 40 | str_has_prefix(str: drv->name, prefix: serial_port_type.name)) | 
|---|
| 41 | return 1; | 
|---|
| 42 |  | 
|---|
| 43 | return 0; | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | static const struct bus_type serial_base_bus_type = { | 
|---|
| 47 | .name = "serial-base", | 
|---|
| 48 | .match = serial_base_match, | 
|---|
| 49 | }; | 
|---|
| 50 |  | 
|---|
| 51 | int serial_base_driver_register(struct device_driver *driver) | 
|---|
| 52 | { | 
|---|
| 53 | driver->bus = &serial_base_bus_type; | 
|---|
| 54 |  | 
|---|
| 55 | return driver_register(drv: driver); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | void serial_base_driver_unregister(struct device_driver *driver) | 
|---|
| 59 | { | 
|---|
| 60 | driver_unregister(drv: driver); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | static int serial_base_device_init(struct uart_port *port, | 
|---|
| 64 | struct device *dev, | 
|---|
| 65 | struct device *parent_dev, | 
|---|
| 66 | const struct device_type *type, | 
|---|
| 67 | void (*release)(struct device *dev), | 
|---|
| 68 | unsigned int ctrl_id, | 
|---|
| 69 | unsigned int port_id) | 
|---|
| 70 | { | 
|---|
| 71 | device_initialize(dev); | 
|---|
| 72 | dev->type = type; | 
|---|
| 73 | dev->parent = parent_dev; | 
|---|
| 74 | dev->bus = &serial_base_bus_type; | 
|---|
| 75 | dev->release = release; | 
|---|
| 76 | device_set_of_node_from_dev(dev, dev2: parent_dev); | 
|---|
| 77 |  | 
|---|
| 78 | if (!serial_base_initialized) { | 
|---|
| 79 | dev_dbg(port->dev, "uart_add_one_port() called before arch_initcall()?\n"); | 
|---|
| 80 | return -EPROBE_DEFER; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | if (type == &serial_ctrl_type) | 
|---|
| 84 | return dev_set_name(dev, name: "%s:%d", dev_name(dev: port->dev), ctrl_id); | 
|---|
| 85 |  | 
|---|
| 86 | if (type == &serial_port_type) | 
|---|
| 87 | return dev_set_name(dev, name: "%s:%d.%d", dev_name(dev: port->dev), | 
|---|
| 88 | ctrl_id, port_id); | 
|---|
| 89 |  | 
|---|
| 90 | return -EINVAL; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | static void serial_base_ctrl_release(struct device *dev) | 
|---|
| 94 | { | 
|---|
| 95 | struct serial_ctrl_device *ctrl_dev = to_serial_base_ctrl_device(dev); | 
|---|
| 96 |  | 
|---|
| 97 | of_node_put(node: dev->of_node); | 
|---|
| 98 | kfree(objp: ctrl_dev); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev) | 
|---|
| 102 | { | 
|---|
| 103 | if (!ctrl_dev) | 
|---|
| 104 | return; | 
|---|
| 105 |  | 
|---|
| 106 | device_del(dev: &ctrl_dev->dev); | 
|---|
| 107 | put_device(dev: &ctrl_dev->dev); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port, | 
|---|
| 111 | struct device *parent) | 
|---|
| 112 | { | 
|---|
| 113 | struct serial_ctrl_device *ctrl_dev; | 
|---|
| 114 | int err; | 
|---|
| 115 |  | 
|---|
| 116 | ctrl_dev = kzalloc(sizeof(*ctrl_dev), GFP_KERNEL); | 
|---|
| 117 | if (!ctrl_dev) | 
|---|
| 118 | return ERR_PTR(error: -ENOMEM); | 
|---|
| 119 |  | 
|---|
| 120 | ida_init(ida: &ctrl_dev->port_ida); | 
|---|
| 121 |  | 
|---|
| 122 | err = serial_base_device_init(port, dev: &ctrl_dev->dev, | 
|---|
| 123 | parent_dev: parent, type: &serial_ctrl_type, | 
|---|
| 124 | release: serial_base_ctrl_release, | 
|---|
| 125 | ctrl_id: port->ctrl_id, port_id: 0); | 
|---|
| 126 | if (err) | 
|---|
| 127 | goto err_put_device; | 
|---|
| 128 |  | 
|---|
| 129 | err = device_add(dev: &ctrl_dev->dev); | 
|---|
| 130 | if (err) | 
|---|
| 131 | goto err_put_device; | 
|---|
| 132 |  | 
|---|
| 133 | return ctrl_dev; | 
|---|
| 134 |  | 
|---|
| 135 | err_put_device: | 
|---|
| 136 | put_device(dev: &ctrl_dev->dev); | 
|---|
| 137 |  | 
|---|
| 138 | return ERR_PTR(error: err); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | static void serial_base_port_release(struct device *dev) | 
|---|
| 142 | { | 
|---|
| 143 | struct serial_port_device *port_dev = to_serial_base_port_device(dev); | 
|---|
| 144 |  | 
|---|
| 145 | of_node_put(node: dev->of_node); | 
|---|
| 146 | kfree(objp: port_dev); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | struct serial_port_device *serial_base_port_add(struct uart_port *port, | 
|---|
| 150 | struct serial_ctrl_device *ctrl_dev) | 
|---|
| 151 | { | 
|---|
| 152 | struct serial_port_device *port_dev; | 
|---|
| 153 | int min = 0, max = -1;	/* Use -1 for max to apply IDA defaults */ | 
|---|
| 154 | int err; | 
|---|
| 155 |  | 
|---|
| 156 | port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL); | 
|---|
| 157 | if (!port_dev) | 
|---|
| 158 | return ERR_PTR(error: -ENOMEM); | 
|---|
| 159 |  | 
|---|
| 160 | /* Device driver specified port_id vs automatic assignment? */ | 
|---|
| 161 | if (port->port_id) { | 
|---|
| 162 | min = port->port_id; | 
|---|
| 163 | max = port->port_id; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | err = ida_alloc_range(&ctrl_dev->port_ida, min, max, GFP_KERNEL); | 
|---|
| 167 | if (err < 0) { | 
|---|
| 168 | kfree(objp: port_dev); | 
|---|
| 169 | return ERR_PTR(error: err); | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | port->port_id = err; | 
|---|
| 173 |  | 
|---|
| 174 | err = serial_base_device_init(port, dev: &port_dev->dev, | 
|---|
| 175 | parent_dev: &ctrl_dev->dev, type: &serial_port_type, | 
|---|
| 176 | release: serial_base_port_release, | 
|---|
| 177 | ctrl_id: port->ctrl_id, port_id: port->port_id); | 
|---|
| 178 | if (err) | 
|---|
| 179 | goto err_put_device; | 
|---|
| 180 |  | 
|---|
| 181 | port_dev->port = port; | 
|---|
| 182 |  | 
|---|
| 183 | err = device_add(dev: &port_dev->dev); | 
|---|
| 184 | if (err) | 
|---|
| 185 | goto err_put_device; | 
|---|
| 186 |  | 
|---|
| 187 | return port_dev; | 
|---|
| 188 |  | 
|---|
| 189 | err_put_device: | 
|---|
| 190 | put_device(dev: &port_dev->dev); | 
|---|
| 191 | ida_free(&ctrl_dev->port_ida, id: port->port_id); | 
|---|
| 192 |  | 
|---|
| 193 | return ERR_PTR(error: err); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | void serial_base_port_device_remove(struct serial_port_device *port_dev) | 
|---|
| 197 | { | 
|---|
| 198 | struct serial_ctrl_device *ctrl_dev; | 
|---|
| 199 | struct device *parent; | 
|---|
| 200 |  | 
|---|
| 201 | if (!port_dev) | 
|---|
| 202 | return; | 
|---|
| 203 |  | 
|---|
| 204 | parent = port_dev->dev.parent; | 
|---|
| 205 | ctrl_dev = to_serial_base_ctrl_device(parent); | 
|---|
| 206 |  | 
|---|
| 207 | device_del(dev: &port_dev->dev); | 
|---|
| 208 | ida_free(&ctrl_dev->port_ida, id: port_dev->port->port_id); | 
|---|
| 209 | put_device(dev: &port_dev->dev); | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | #ifdef CONFIG_SERIAL_CORE_CONSOLE | 
|---|
| 213 |  | 
|---|
| 214 | /** | 
|---|
| 215 | * serial_base_match_and_update_preferred_console - Match and update a preferred console | 
|---|
| 216 | * @drv: Serial port device driver | 
|---|
| 217 | * @port: Serial port instance | 
|---|
| 218 | * | 
|---|
| 219 | * Tries to match and update the preferred console for a serial port for | 
|---|
| 220 | * the kernel command line option console=DEVNAME:0.0. | 
|---|
| 221 | * | 
|---|
| 222 | * Cannot be called early for ISA ports, depends on struct device. | 
|---|
| 223 | * | 
|---|
| 224 | * Return: 0 on success, negative error code on failure. | 
|---|
| 225 | */ | 
|---|
| 226 | int serial_base_match_and_update_preferred_console(struct uart_driver *drv, | 
|---|
| 227 | struct uart_port *port) | 
|---|
| 228 | { | 
|---|
| 229 | const char *port_match __free(kfree) = NULL; | 
|---|
| 230 | int ret; | 
|---|
| 231 |  | 
|---|
| 232 | port_match = kasprintf(GFP_KERNEL, fmt: "%s:%d.%d", dev_name(dev: port->dev), | 
|---|
| 233 | port->ctrl_id, port->port_id); | 
|---|
| 234 | if (!port_match) | 
|---|
| 235 | return -ENOMEM; | 
|---|
| 236 |  | 
|---|
| 237 | ret = match_devname_and_update_preferred_console(match: port_match, | 
|---|
| 238 | name: drv->dev_name, | 
|---|
| 239 | idx: port->line); | 
|---|
| 240 | if (ret == -ENOENT) | 
|---|
| 241 | return 0; | 
|---|
| 242 |  | 
|---|
| 243 | return ret; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | #endif | 
|---|
| 247 |  | 
|---|
| 248 | static int serial_base_init(void) | 
|---|
| 249 | { | 
|---|
| 250 | int ret; | 
|---|
| 251 |  | 
|---|
| 252 | ret = bus_register(bus: &serial_base_bus_type); | 
|---|
| 253 | if (ret) | 
|---|
| 254 | return ret; | 
|---|
| 255 |  | 
|---|
| 256 | ret = serial_base_ctrl_init(); | 
|---|
| 257 | if (ret) | 
|---|
| 258 | goto err_bus_unregister; | 
|---|
| 259 |  | 
|---|
| 260 | ret = serial_base_port_init(); | 
|---|
| 261 | if (ret) | 
|---|
| 262 | goto err_ctrl_exit; | 
|---|
| 263 |  | 
|---|
| 264 | serial_base_initialized = true; | 
|---|
| 265 |  | 
|---|
| 266 | return 0; | 
|---|
| 267 |  | 
|---|
| 268 | err_ctrl_exit: | 
|---|
| 269 | serial_base_ctrl_exit(); | 
|---|
| 270 |  | 
|---|
| 271 | err_bus_unregister: | 
|---|
| 272 | bus_unregister(bus: &serial_base_bus_type); | 
|---|
| 273 |  | 
|---|
| 274 | return ret; | 
|---|
| 275 | } | 
|---|
| 276 | arch_initcall(serial_base_init); | 
|---|
| 277 |  | 
|---|
| 278 | static void serial_base_exit(void) | 
|---|
| 279 | { | 
|---|
| 280 | serial_base_port_exit(); | 
|---|
| 281 | serial_base_ctrl_exit(); | 
|---|
| 282 | bus_unregister(bus: &serial_base_bus_type); | 
|---|
| 283 | } | 
|---|
| 284 | module_exit(serial_base_exit); | 
|---|
| 285 |  | 
|---|
| 286 | MODULE_AUTHOR( "Tony Lindgren <tony@atomide.com>"); | 
|---|
| 287 | MODULE_DESCRIPTION( "Serial core bus"); | 
|---|
| 288 | MODULE_LICENSE( "GPL"); | 
|---|
| 289 |  | 
|---|