Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Serial Port driver for Open Firmware platform devices |
| 4 | * |
| 5 | * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp. |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 6 | */ |
Jingchang Lu | 8ad3b13 | 2014-11-11 15:09:05 +0800 | [diff] [blame] | 7 | #include <linux/console.h> |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 8 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 10 | #include <linux/serial_core.h> |
Dan Williams | bf03f65 | 2012-04-10 14:10:53 -0700 | [diff] [blame] | 11 | #include <linux/serial_reg.h> |
Grant Likely | f1ca09b | 2010-08-16 23:44:49 -0600 | [diff] [blame] | 12 | #include <linux/of_address.h> |
Rob Herring | 73930a8 | 2010-11-17 17:50:23 -0600 | [diff] [blame] | 13 | #include <linux/of_irq.h> |
Stephen Rothwell | c401b04 | 2008-05-23 16:32:54 +1000 | [diff] [blame] | 14 | #include <linux/of_platform.h> |
Franklin S Cooper Jr | a2d23eda | 2017-08-16 15:55:36 -0500 | [diff] [blame] | 15 | #include <linux/pm_runtime.h> |
Murali Karicheri | 0bbeb3c | 2012-10-22 11:58:01 -0400 | [diff] [blame] | 16 | #include <linux/clk.h> |
Joel Stanley | e2860e1 | 2017-05-29 19:27:52 +0930 | [diff] [blame] | 17 | #include <linux/reset.h> |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 18 | |
Peter Ujfalusi | c93a599 | 2015-12-18 15:00:49 +0200 | [diff] [blame] | 19 | #include "8250.h" |
Heikki Krogerus | b0b8c84 | 2013-03-25 15:51:15 +0200 | [diff] [blame] | 20 | |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 21 | struct of_serial_info { |
Murali Karicheri | 0bbeb3c | 2012-10-22 11:58:01 -0400 | [diff] [blame] | 22 | struct clk *clk; |
Joel Stanley | e2860e1 | 2017-05-29 19:27:52 +0930 | [diff] [blame] | 23 | struct reset_control *rst; |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 24 | int type; |
| 25 | int line; |
| 26 | }; |
| 27 | |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 28 | /* |
| 29 | * Fill a struct uart_port for a given device node |
| 30 | */ |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 31 | static int of_platform_serial_setup(struct platform_device *ofdev, |
Lukas Wunner | 058bc10 | 2020-02-28 14:31:06 +0100 | [diff] [blame] | 32 | int type, struct uart_8250_port *up, |
Murali Karicheri | 0bbeb3c | 2012-10-22 11:58:01 -0400 | [diff] [blame] | 33 | struct of_serial_info *info) |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 34 | { |
| 35 | struct resource resource; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 36 | struct device_node *np = ofdev->dev.of_node; |
Lukas Wunner | 058bc10 | 2020-02-28 14:31:06 +0100 | [diff] [blame] | 37 | struct uart_port *port = &up->port; |
Grant Likely | b84e773 | 2011-06-30 12:39:12 -0600 | [diff] [blame] | 38 | u32 clk, spd, prop; |
John Garry | a27d9382 | 2018-08-30 17:08:50 +0800 | [diff] [blame] | 39 | int ret, irq; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 40 | |
| 41 | memset(port, 0, sizeof *port); |
Franklin S Cooper Jr | a2d23eda | 2017-08-16 15:55:36 -0500 | [diff] [blame] | 42 | |
| 43 | pm_runtime_enable(&ofdev->dev); |
| 44 | pm_runtime_get_sync(&ofdev->dev); |
| 45 | |
Grant Likely | b84e773 | 2011-06-30 12:39:12 -0600 | [diff] [blame] | 46 | if (of_property_read_u32(np, "clock-frequency", &clk)) { |
Murali Karicheri | 0bbeb3c | 2012-10-22 11:58:01 -0400 | [diff] [blame] | 47 | |
| 48 | /* Get clk rate through clk driver if present */ |
Masahiro Yamada | 3a63d22 | 2015-05-25 14:57:43 +0900 | [diff] [blame] | 49 | info->clk = devm_clk_get(&ofdev->dev, NULL); |
Wei Yongjun | 76cc438 | 2012-11-01 13:27:34 +0800 | [diff] [blame] | 50 | if (IS_ERR(info->clk)) { |
Franklin S Cooper Jr | a2d23eda | 2017-08-16 15:55:36 -0500 | [diff] [blame] | 51 | ret = PTR_ERR(info->clk); |
Thierry Reding | 87bb008 | 2019-06-05 10:51:42 +0200 | [diff] [blame] | 52 | if (ret != -EPROBE_DEFER) |
| 53 | dev_warn(&ofdev->dev, |
| 54 | "failed to get clock: %d\n", ret); |
Franklin S Cooper Jr | a2d23eda | 2017-08-16 15:55:36 -0500 | [diff] [blame] | 55 | goto err_pmruntime; |
Murali Karicheri | 0bbeb3c | 2012-10-22 11:58:01 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Masahiro Yamada | 6f0c309 | 2015-05-25 15:03:32 +0900 | [diff] [blame] | 58 | ret = clk_prepare_enable(info->clk); |
| 59 | if (ret < 0) |
Franklin S Cooper Jr | a2d23eda | 2017-08-16 15:55:36 -0500 | [diff] [blame] | 60 | goto err_pmruntime; |
Masahiro Yamada | 6f0c309 | 2015-05-25 15:03:32 +0900 | [diff] [blame] | 61 | |
Murali Karicheri | 0bbeb3c | 2012-10-22 11:58:01 -0400 | [diff] [blame] | 62 | clk = clk_get_rate(info->clk); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 63 | } |
Grant Likely | b84e773 | 2011-06-30 12:39:12 -0600 | [diff] [blame] | 64 | /* If current-speed was set, then try not to change it. */ |
| 65 | if (of_property_read_u32(np, "current-speed", &spd) == 0) |
| 66 | port->custom_divisor = clk / (16 * spd); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 67 | |
| 68 | ret = of_address_to_resource(np, 0, &resource); |
| 69 | if (ret) { |
| 70 | dev_warn(&ofdev->dev, "invalid address\n"); |
Alexey Khoroshilov | fa9ba3ac | 2017-07-19 11:32:37 +0300 | [diff] [blame] | 71 | goto err_unprepare; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 72 | } |
| 73 | |
John Garry | aa95947 | 2018-04-27 18:36:06 +0800 | [diff] [blame] | 74 | port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT | |
| 75 | UPF_FIXED_TYPE; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 76 | spin_lock_init(&port->lock); |
John Linn | b912b5e | 2008-04-03 10:22:19 +1100 | [diff] [blame] | 77 | |
John Garry | aa95947 | 2018-04-27 18:36:06 +0800 | [diff] [blame] | 78 | if (resource_type(&resource) == IORESOURCE_IO) { |
| 79 | port->iotype = UPIO_PORT; |
| 80 | port->iobase = resource.start; |
| 81 | } else { |
| 82 | port->mapbase = resource.start; |
| 83 | port->mapsize = resource_size(&resource); |
| 84 | |
| 85 | /* Check for shifted address mapping */ |
| 86 | if (of_property_read_u32(np, "reg-offset", &prop) == 0) |
| 87 | port->mapbase += prop; |
| 88 | |
| 89 | port->iotype = UPIO_MEM; |
| 90 | if (of_property_read_u32(np, "reg-io-width", &prop) == 0) { |
| 91 | switch (prop) { |
| 92 | case 1: |
| 93 | port->iotype = UPIO_MEM; |
| 94 | break; |
| 95 | case 2: |
| 96 | port->iotype = UPIO_MEM16; |
| 97 | break; |
| 98 | case 4: |
| 99 | port->iotype = of_device_is_big_endian(np) ? |
| 100 | UPIO_MEM32BE : UPIO_MEM32; |
| 101 | break; |
| 102 | default: |
| 103 | dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n", |
| 104 | prop); |
| 105 | ret = -EINVAL; |
Alexander Sverdlin | b29330d82 | 2018-07-13 16:58:54 +0200 | [diff] [blame] | 106 | goto err_unprepare; |
John Garry | aa95947 | 2018-04-27 18:36:06 +0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | port->flags |= UPF_IOREMAP; |
| 110 | } |
John Linn | b912b5e | 2008-04-03 10:22:19 +1100 | [diff] [blame] | 111 | |
Lubomir Rintel | f481784 | 2019-02-24 13:00:53 +0100 | [diff] [blame] | 112 | /* Compatibility with the deprecated pxa driver and 8250_pxa drivers. */ |
| 113 | if (of_device_is_compatible(np, "mrvl,mmp-uart")) |
| 114 | port->regshift = 2; |
| 115 | |
John Linn | b912b5e | 2008-04-03 10:22:19 +1100 | [diff] [blame] | 116 | /* Check for registers offset within the devices address range */ |
Grant Likely | b84e773 | 2011-06-30 12:39:12 -0600 | [diff] [blame] | 117 | if (of_property_read_u32(np, "reg-shift", &prop) == 0) |
| 118 | port->regshift = prop; |
John Linn | b912b5e | 2008-04-03 10:22:19 +1100 | [diff] [blame] | 119 | |
Heikki Krogerus | 9f1ca06 | 2013-03-25 13:34:45 +0200 | [diff] [blame] | 120 | /* Check for fifo size */ |
| 121 | if (of_property_read_u32(np, "fifo-size", &prop) == 0) |
| 122 | port->fifosize = prop; |
| 123 | |
Lucas Stach | 3239fd3 | 2014-12-05 20:21:57 +0100 | [diff] [blame] | 124 | /* Check for a fixed line number */ |
| 125 | ret = of_alias_get_id(np, "serial"); |
| 126 | if (ret >= 0) |
| 127 | port->line = ret; |
| 128 | |
John Garry | a27d9382 | 2018-08-30 17:08:50 +0800 | [diff] [blame] | 129 | irq = of_irq_get(np, 0); |
| 130 | if (irq < 0) { |
| 131 | if (irq == -EPROBE_DEFER) { |
| 132 | ret = -EPROBE_DEFER; |
| 133 | goto err_unprepare; |
| 134 | } |
| 135 | /* IRQ support not mandatory */ |
| 136 | irq = 0; |
Alexander Sverdlin | c58caaa | 2018-07-13 17:01:19 +0200 | [diff] [blame] | 137 | } |
Jamie Iles | 7423734 | 2011-06-27 13:32:34 +0100 | [diff] [blame] | 138 | |
John Garry | a27d9382 | 2018-08-30 17:08:50 +0800 | [diff] [blame] | 139 | port->irq = irq; |
| 140 | |
Joel Stanley | e2860e1 | 2017-05-29 19:27:52 +0930 | [diff] [blame] | 141 | info->rst = devm_reset_control_get_optional_shared(&ofdev->dev, NULL); |
Masahiro Yamada | b9820a3 | 2017-12-27 14:21:05 +0900 | [diff] [blame] | 142 | if (IS_ERR(info->rst)) { |
| 143 | ret = PTR_ERR(info->rst); |
John Garry | a27d9382 | 2018-08-30 17:08:50 +0800 | [diff] [blame] | 144 | goto err_unprepare; |
Masahiro Yamada | b9820a3 | 2017-12-27 14:21:05 +0900 | [diff] [blame] | 145 | } |
| 146 | |
Joel Stanley | e2860e1 | 2017-05-29 19:27:52 +0930 | [diff] [blame] | 147 | ret = reset_control_deassert(info->rst); |
| 148 | if (ret) |
John Garry | a27d9382 | 2018-08-30 17:08:50 +0800 | [diff] [blame] | 149 | goto err_unprepare; |
Joel Stanley | e2860e1 | 2017-05-29 19:27:52 +0930 | [diff] [blame] | 150 | |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 151 | port->type = type; |
Grant Likely | b84e773 | 2011-06-30 12:39:12 -0600 | [diff] [blame] | 152 | port->uartclk = clk; |
Gabor Juhos | fde8be2 | 2012-07-17 17:08:31 +0100 | [diff] [blame] | 153 | |
Sergei Shtylyov | 5c98e9c | 2017-08-20 19:51:55 +0300 | [diff] [blame] | 154 | if (of_property_read_bool(np, "no-loopback-test")) |
Gabor Juhos | fde8be2 | 2012-07-17 17:08:31 +0100 | [diff] [blame] | 155 | port->flags |= UPF_SKIP_TEST; |
| 156 | |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 157 | port->dev = &ofdev->dev; |
Lukas Wunner | 283e096 | 2020-02-28 14:31:03 +0100 | [diff] [blame] | 158 | port->rs485_config = serial8250_em485_config; |
Lukas Wunner | 058bc10 | 2020-02-28 14:31:06 +0100 | [diff] [blame] | 159 | up->rs485_start_tx = serial8250_em485_start_tx; |
| 160 | up->rs485_stop_tx = serial8250_em485_stop_tx; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 161 | |
John Crispin | 9b8777e | 2014-10-16 21:48:21 +0200 | [diff] [blame] | 162 | switch (type) { |
John Crispin | 9b8777e | 2014-10-16 21:48:21 +0200 | [diff] [blame] | 163 | case PORT_RT2880: |
| 164 | port->iotype = UPIO_AU; |
| 165 | break; |
| 166 | } |
Dan Williams | bf03f65 | 2012-04-10 14:10:53 -0700 | [diff] [blame] | 167 | |
Scott Wood | d43b54d | 2015-10-07 17:31:21 -0500 | [diff] [blame] | 168 | if (IS_ENABLED(CONFIG_SERIAL_8250_FSL) && |
| 169 | (of_device_is_compatible(np, "fsl,ns16550") || |
Dmitry Safonov | d68fefd | 2019-12-13 00:06:04 +0000 | [diff] [blame] | 170 | of_device_is_compatible(np, "fsl,16550-FIFO64"))) { |
Scott Wood | d43b54d | 2015-10-07 17:31:21 -0500 | [diff] [blame] | 171 | port->handle_irq = fsl8250_handle_irq; |
Dmitry Safonov | d68fefd | 2019-12-13 00:06:04 +0000 | [diff] [blame] | 172 | port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE); |
| 173 | } |
Scott Wood | d43b54d | 2015-10-07 17:31:21 -0500 | [diff] [blame] | 174 | |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 175 | return 0; |
Alexey Khoroshilov | fa9ba3ac | 2017-07-19 11:32:37 +0300 | [diff] [blame] | 176 | err_unprepare: |
Franklin S Cooper Jr | a2d23eda | 2017-08-16 15:55:36 -0500 | [diff] [blame] | 177 | clk_disable_unprepare(info->clk); |
| 178 | err_pmruntime: |
| 179 | pm_runtime_put_sync(&ofdev->dev); |
| 180 | pm_runtime_disable(&ofdev->dev); |
Murali Karicheri | 0bbeb3c | 2012-10-22 11:58:01 -0400 | [diff] [blame] | 181 | return ret; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Try to register a serial port |
| 186 | */ |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 187 | static int of_platform_serial_probe(struct platform_device *ofdev) |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 188 | { |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 189 | struct of_serial_info *info; |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 190 | struct uart_8250_port port8250; |
Andy Shevchenko | 525667c | 2019-04-30 21:45:59 +0300 | [diff] [blame] | 191 | unsigned int port_type; |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 192 | u32 tx_threshold; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 193 | int ret; |
| 194 | |
Jim Quinlan | f5b0838 | 2021-04-23 11:32:04 -0700 | [diff] [blame] | 195 | if (IS_ENABLED(CONFIG_SERIAL_8250_BCM7271) && |
| 196 | of_device_is_compatible(ofdev->dev.of_node, "brcm,bcm7271-uart")) |
| 197 | return -ENODEV; |
| 198 | |
Andy Shevchenko | 525667c | 2019-04-30 21:45:59 +0300 | [diff] [blame] | 199 | port_type = (unsigned long)of_device_get_match_data(&ofdev->dev); |
| 200 | if (port_type == PORT_UNKNOWN) |
Grant Likely | 793218d | 2011-02-22 21:10:26 -0700 | [diff] [blame] | 201 | return -EINVAL; |
| 202 | |
Sergei Shtylyov | 5c98e9c | 2017-08-20 19:51:55 +0300 | [diff] [blame] | 203 | if (of_property_read_bool(ofdev->dev.of_node, "used-by-rtas")) |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 204 | return -EBUSY; |
| 205 | |
Jingchang Lu | 7e12e67 | 2014-10-21 16:50:21 +0800 | [diff] [blame] | 206 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 207 | if (info == NULL) |
| 208 | return -ENOMEM; |
| 209 | |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 210 | memset(&port8250, 0, sizeof(port8250)); |
Lukas Wunner | 058bc10 | 2020-02-28 14:31:06 +0100 | [diff] [blame] | 211 | ret = of_platform_serial_setup(ofdev, port_type, &port8250, info); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 212 | if (ret) |
Alexey Khoroshilov | fa9ba3ac | 2017-07-19 11:32:37 +0300 | [diff] [blame] | 213 | goto err_free; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 214 | |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 215 | if (port8250.port.fifosize) |
| 216 | port8250.capabilities = UART_CAP_FIFO; |
Heikki Krogerus | b0b8c84 | 2013-03-25 15:51:15 +0200 | [diff] [blame] | 217 | |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 218 | /* Check for TX FIFO threshold & set tx_loadsz */ |
| 219 | if ((of_property_read_u32(ofdev->dev.of_node, "tx-threshold", |
| 220 | &tx_threshold) == 0) && |
| 221 | (tx_threshold < port8250.port.fifosize)) |
| 222 | port8250.tx_loadsz = port8250.port.fifosize - tx_threshold; |
Heikki Krogerus | b0b8c84 | 2013-03-25 15:51:15 +0200 | [diff] [blame] | 223 | |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 224 | if (of_property_read_bool(ofdev->dev.of_node, "auto-flow-control")) |
| 225 | port8250.capabilities |= UART_CAP_AFE; |
Thor Thayer | ffea043 | 2016-09-22 14:56:15 -0500 | [diff] [blame] | 226 | |
Darwin Dingel | 6d7f677 | 2018-12-10 11:29:09 +1300 | [diff] [blame] | 227 | if (of_property_read_u32(ofdev->dev.of_node, |
| 228 | "overrun-throttle-ms", |
| 229 | &port8250.overrun_backoff_time_ms) != 0) |
| 230 | port8250.overrun_backoff_time_ms = 0; |
| 231 | |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 232 | ret = serial8250_register_8250_port(&port8250); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 233 | if (ret < 0) |
Alexey Khoroshilov | fa9ba3ac | 2017-07-19 11:32:37 +0300 | [diff] [blame] | 234 | goto err_dispose; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 235 | |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 236 | info->type = port_type; |
| 237 | info->line = ret; |
Jingoo Han | 696faed | 2013-05-23 19:39:36 +0900 | [diff] [blame] | 238 | platform_set_drvdata(ofdev, info); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 239 | return 0; |
Alexey Khoroshilov | fa9ba3ac | 2017-07-19 11:32:37 +0300 | [diff] [blame] | 240 | err_dispose: |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 241 | irq_dispose_mapping(port8250.port.irq); |
Franklin S Cooper Jr | a2d23eda | 2017-08-16 15:55:36 -0500 | [diff] [blame] | 242 | pm_runtime_put_sync(&ofdev->dev); |
| 243 | pm_runtime_disable(&ofdev->dev); |
| 244 | clk_disable_unprepare(info->clk); |
Alexey Khoroshilov | fa9ba3ac | 2017-07-19 11:32:37 +0300 | [diff] [blame] | 245 | err_free: |
| 246 | kfree(info); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Release a line |
| 252 | */ |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 253 | static int of_platform_serial_remove(struct platform_device *ofdev) |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 254 | { |
Jingoo Han | 696faed | 2013-05-23 19:39:36 +0900 | [diff] [blame] | 255 | struct of_serial_info *info = platform_get_drvdata(ofdev); |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 256 | |
| 257 | serial8250_unregister_port(info->line); |
Murali Karicheri | 0bbeb3c | 2012-10-22 11:58:01 -0400 | [diff] [blame] | 258 | |
Joel Stanley | e2860e1 | 2017-05-29 19:27:52 +0930 | [diff] [blame] | 259 | reset_control_assert(info->rst); |
Franklin S Cooper Jr | a2d23eda | 2017-08-16 15:55:36 -0500 | [diff] [blame] | 260 | pm_runtime_put_sync(&ofdev->dev); |
| 261 | pm_runtime_disable(&ofdev->dev); |
| 262 | clk_disable_unprepare(info->clk); |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 263 | kfree(info); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | |
Jingchang Lu | 8ad3b13 | 2014-11-11 15:09:05 +0800 | [diff] [blame] | 267 | #ifdef CONFIG_PM_SLEEP |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 268 | static int of_serial_suspend(struct device *dev) |
Jingchang Lu | 8ad3b13 | 2014-11-11 15:09:05 +0800 | [diff] [blame] | 269 | { |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 270 | struct of_serial_info *info = dev_get_drvdata(dev); |
Jingchang Lu | 8ad3b13 | 2014-11-11 15:09:05 +0800 | [diff] [blame] | 271 | struct uart_8250_port *port8250 = serial8250_get_port(info->line); |
| 272 | struct uart_port *port = &port8250->port; |
| 273 | |
| 274 | serial8250_suspend_port(info->line); |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 275 | |
Franklin S Cooper Jr | a2d23eda | 2017-08-16 15:55:36 -0500 | [diff] [blame] | 276 | if (!uart_console(port) || console_suspend_enabled) { |
| 277 | pm_runtime_put_sync(dev); |
Jingchang Lu | 8ad3b13 | 2014-11-11 15:09:05 +0800 | [diff] [blame] | 278 | clk_disable_unprepare(info->clk); |
Franklin S Cooper Jr | a2d23eda | 2017-08-16 15:55:36 -0500 | [diff] [blame] | 279 | } |
Jingchang Lu | 8ad3b13 | 2014-11-11 15:09:05 +0800 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static int of_serial_resume(struct device *dev) |
| 284 | { |
| 285 | struct of_serial_info *info = dev_get_drvdata(dev); |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 286 | struct uart_8250_port *port8250 = serial8250_get_port(info->line); |
| 287 | struct uart_port *port = &port8250->port; |
Jingchang Lu | 8ad3b13 | 2014-11-11 15:09:05 +0800 | [diff] [blame] | 288 | |
Franklin S Cooper Jr | a2d23eda | 2017-08-16 15:55:36 -0500 | [diff] [blame] | 289 | if (!uart_console(port) || console_suspend_enabled) { |
| 290 | pm_runtime_get_sync(dev); |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 291 | clk_prepare_enable(info->clk); |
Franklin S Cooper Jr | a2d23eda | 2017-08-16 15:55:36 -0500 | [diff] [blame] | 292 | } |
Arnd Bergmann | aa42db4 | 2017-01-25 23:19:01 +0100 | [diff] [blame] | 293 | |
| 294 | serial8250_resume_port(info->line); |
Jingchang Lu | 8ad3b13 | 2014-11-11 15:09:05 +0800 | [diff] [blame] | 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | #endif |
| 299 | static SIMPLE_DEV_PM_OPS(of_serial_pm_ops, of_serial_suspend, of_serial_resume); |
| 300 | |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 301 | /* |
| 302 | * A few common types, add more as needed. |
| 303 | */ |
Fabian Frederick | ed0bb23 | 2015-03-16 20:17:11 +0100 | [diff] [blame] | 304 | static const struct of_device_id of_platform_serial_table[] = { |
Grant Likely | 8c6e911 | 2011-02-22 19:12:21 -0700 | [diff] [blame] | 305 | { .compatible = "ns8250", .data = (void *)PORT_8250, }, |
| 306 | { .compatible = "ns16450", .data = (void *)PORT_16450, }, |
| 307 | { .compatible = "ns16550a", .data = (void *)PORT_16550A, }, |
| 308 | { .compatible = "ns16550", .data = (void *)PORT_16550, }, |
| 309 | { .compatible = "ns16750", .data = (void *)PORT_16750, }, |
| 310 | { .compatible = "ns16850", .data = (void *)PORT_16850, }, |
Roland Stigge | e4305f0 | 2012-06-11 21:57:14 +0200 | [diff] [blame] | 311 | { .compatible = "nxp,lpc3220-uart", .data = (void *)PORT_LPC3220, }, |
John Crispin | 9b8777e | 2014-10-16 21:48:21 +0200 | [diff] [blame] | 312 | { .compatible = "ralink,rt2880-uart", .data = (void *)PORT_RT2880, }, |
Linus Walleij | 3a50365 | 2019-01-27 02:04:04 +0100 | [diff] [blame] | 313 | { .compatible = "intel,xscale-uart", .data = (void *)PORT_XSCALE, }, |
Ley Foon Tan | e06c93c | 2013-03-07 10:28:37 +0800 | [diff] [blame] | 314 | { .compatible = "altr,16550-FIFO32", |
| 315 | .data = (void *)PORT_ALTR_16550_F32, }, |
| 316 | { .compatible = "altr,16550-FIFO64", |
| 317 | .data = (void *)PORT_ALTR_16550_F64, }, |
| 318 | { .compatible = "altr,16550-FIFO128", |
| 319 | .data = (void *)PORT_ALTR_16550_F128, }, |
Sean Wang | 1c16ae6 | 2017-08-21 01:17:56 +0800 | [diff] [blame] | 320 | { .compatible = "mediatek,mtk-btif", |
| 321 | .data = (void *)PORT_MTK_BTIF, }, |
Rob Herring | 6ad991b | 2015-01-26 22:50:08 -0600 | [diff] [blame] | 322 | { .compatible = "mrvl,mmp-uart", |
| 323 | .data = (void *)PORT_XSCALE, }, |
David Lechner | a2d6a98 | 2017-01-05 12:54:18 -0600 | [diff] [blame] | 324 | { .compatible = "ti,da830-uart", .data = (void *)PORT_DA830, }, |
Jonathan Neuschäfer | 8465df7 | 2021-03-20 19:16:05 +0100 | [diff] [blame] | 325 | { .compatible = "nuvoton,wpcm450-uart", .data = (void *)PORT_NPCM, }, |
Joel Stanley | f597fbc | 2018-03-05 22:17:38 +1030 | [diff] [blame] | 326 | { .compatible = "nuvoton,npcm750-uart", .data = (void *)PORT_NPCM, }, |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 327 | { /* end of list */ }, |
| 328 | }; |
Luis de Bethencourt | 8d58db1 | 2015-09-18 20:03:43 +0200 | [diff] [blame] | 329 | MODULE_DEVICE_TABLE(of, of_platform_serial_table); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 330 | |
Grant Likely | 793218d | 2011-02-22 21:10:26 -0700 | [diff] [blame] | 331 | static struct platform_driver of_platform_serial_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 332 | .driver = { |
| 333 | .name = "of_serial", |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 334 | .of_match_table = of_platform_serial_table, |
Wang Dongsheng | 434ba16 | 2016-01-21 15:59:53 +0800 | [diff] [blame] | 335 | .pm = &of_serial_pm_ops, |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 336 | }, |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 337 | .probe = of_platform_serial_probe, |
| 338 | .remove = of_platform_serial_remove, |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 339 | }; |
| 340 | |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 341 | module_platform_driver(of_platform_serial_driver); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 342 | |
| 343 | MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>"); |
| 344 | MODULE_LICENSE("GPL"); |
| 345 | MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices"); |