blob: 851e5eb19b98cc5900f30229249601ebb9abbeda [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +01002/*
3 * Serial Port driver for Open Firmware platform devices
4 *
5 * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
Jingchang Lu8ad3b132014-11-11 15:09:05 +080013#include <linux/console.h>
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010014#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Dan Williamsbf03f652012-04-10 14:10:53 -070016#include <linux/delay.h>
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010017#include <linux/serial_core.h>
Dan Williamsbf03f652012-04-10 14:10:53 -070018#include <linux/serial_reg.h>
Grant Likelyf1ca09b2010-08-16 23:44:49 -060019#include <linux/of_address.h>
Rob Herring73930a82010-11-17 17:50:23 -060020#include <linux/of_irq.h>
Stephen Rothwellc401b042008-05-23 16:32:54 +100021#include <linux/of_platform.h>
Franklin S Cooper Jra2d23eda2017-08-16 15:55:36 -050022#include <linux/pm_runtime.h>
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040023#include <linux/clk.h>
Joel Stanleye2860e12017-05-29 19:27:52 +093024#include <linux/reset.h>
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010025
Peter Ujfalusic93a5992015-12-18 15:00:49 +020026#include "8250.h"
Heikki Krogerusb0b8c842013-03-25 15:51:15 +020027
Ishizaki Koue34b9c92007-05-31 19:33:04 +100028struct of_serial_info {
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040029 struct clk *clk;
Joel Stanleye2860e12017-05-29 19:27:52 +093030 struct reset_control *rst;
Ishizaki Koue34b9c92007-05-31 19:33:04 +100031 int type;
32 int line;
33};
34
Dan Williamsbf03f652012-04-10 14:10:53 -070035#ifdef CONFIG_ARCH_TEGRA
Thierry Reding28264eb62016-04-28 14:47:24 +020036static void tegra_serial_handle_break(struct uart_port *p)
Dan Williamsbf03f652012-04-10 14:10:53 -070037{
38 unsigned int status, tmout = 10000;
39
40 do {
41 status = p->serial_in(p, UART_LSR);
42 if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
43 status = p->serial_in(p, UART_RX);
44 else
45 break;
46 if (--tmout == 0)
47 break;
48 udelay(1);
49 } while (1);
50}
Stephen Warrenf26402e2013-01-31 12:01:53 -070051#else
52static inline void tegra_serial_handle_break(struct uart_port *port)
53{
54}
Dan Williamsbf03f652012-04-10 14:10:53 -070055#endif
56
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010057/*
58 * Fill a struct uart_port for a given device node
59 */
Bill Pemberton9671f092012-11-19 13:21:50 -050060static int of_platform_serial_setup(struct platform_device *ofdev,
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040061 int type, struct uart_port *port,
62 struct of_serial_info *info)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010063{
64 struct resource resource;
Grant Likely61c7a082010-04-13 16:12:29 -070065 struct device_node *np = ofdev->dev.of_node;
Grant Likelyb84e7732011-06-30 12:39:12 -060066 u32 clk, spd, prop;
67 int ret;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010068
69 memset(port, 0, sizeof *port);
Franklin S Cooper Jra2d23eda2017-08-16 15:55:36 -050070
71 pm_runtime_enable(&ofdev->dev);
72 pm_runtime_get_sync(&ofdev->dev);
73
Grant Likelyb84e7732011-06-30 12:39:12 -060074 if (of_property_read_u32(np, "clock-frequency", &clk)) {
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040075
76 /* Get clk rate through clk driver if present */
Masahiro Yamada3a63d222015-05-25 14:57:43 +090077 info->clk = devm_clk_get(&ofdev->dev, NULL);
Wei Yongjun76cc4382012-11-01 13:27:34 +080078 if (IS_ERR(info->clk)) {
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040079 dev_warn(&ofdev->dev,
80 "clk or clock-frequency not defined\n");
Franklin S Cooper Jra2d23eda2017-08-16 15:55:36 -050081 ret = PTR_ERR(info->clk);
82 goto err_pmruntime;
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040083 }
84
Masahiro Yamada6f0c3092015-05-25 15:03:32 +090085 ret = clk_prepare_enable(info->clk);
86 if (ret < 0)
Franklin S Cooper Jra2d23eda2017-08-16 15:55:36 -050087 goto err_pmruntime;
Masahiro Yamada6f0c3092015-05-25 15:03:32 +090088
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040089 clk = clk_get_rate(info->clk);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010090 }
Grant Likelyb84e7732011-06-30 12:39:12 -060091 /* If current-speed was set, then try not to change it. */
92 if (of_property_read_u32(np, "current-speed", &spd) == 0)
93 port->custom_divisor = clk / (16 * spd);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010094
95 ret = of_address_to_resource(np, 0, &resource);
96 if (ret) {
97 dev_warn(&ofdev->dev, "invalid address\n");
Alexey Khoroshilovfa9ba3ac2017-07-19 11:32:37 +030098 goto err_unprepare;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010099 }
100
101 spin_lock_init(&port->lock);
102 port->mapbase = resource.start;
Mans Rullgard07876912015-03-08 14:30:05 +0000103 port->mapsize = resource_size(&resource);
John Linnb912b5e2008-04-03 10:22:19 +1100104
105 /* Check for shifted address mapping */
Grant Likelyb84e7732011-06-30 12:39:12 -0600106 if (of_property_read_u32(np, "reg-offset", &prop) == 0)
107 port->mapbase += prop;
John Linnb912b5e2008-04-03 10:22:19 +1100108
109 /* Check for registers offset within the devices address range */
Grant Likelyb84e7732011-06-30 12:39:12 -0600110 if (of_property_read_u32(np, "reg-shift", &prop) == 0)
111 port->regshift = prop;
John Linnb912b5e2008-04-03 10:22:19 +1100112
Heikki Krogerus9f1ca062013-03-25 13:34:45 +0200113 /* Check for fifo size */
114 if (of_property_read_u32(np, "fifo-size", &prop) == 0)
115 port->fifosize = prop;
116
Lucas Stach3239fd32014-12-05 20:21:57 +0100117 /* Check for a fixed line number */
118 ret = of_alias_get_id(np, "serial");
119 if (ret >= 0)
120 port->line = ret;
121
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100122 port->irq = irq_of_parse_and_map(np, 0);
123 port->iotype = UPIO_MEM;
Grant Likelyb84e7732011-06-30 12:39:12 -0600124 if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
125 switch (prop) {
Jamie Iles74237342011-06-27 13:32:34 +0100126 case 1:
127 port->iotype = UPIO_MEM;
128 break;
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900129 case 2:
130 port->iotype = UPIO_MEM16;
131 break;
Jamie Iles74237342011-06-27 13:32:34 +0100132 case 4:
Kevin Cernekeeebc5e202015-04-09 13:05:18 -0700133 port->iotype = of_device_is_big_endian(np) ?
134 UPIO_MEM32BE : UPIO_MEM32;
Jamie Iles74237342011-06-27 13:32:34 +0100135 break;
136 default:
Grant Likelyb84e7732011-06-30 12:39:12 -0600137 dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n",
138 prop);
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -0400139 ret = -EINVAL;
Alexey Khoroshilovfa9ba3ac2017-07-19 11:32:37 +0300140 goto err_dispose;
Jamie Iles74237342011-06-27 13:32:34 +0100141 }
142 }
143
Joel Stanleye2860e12017-05-29 19:27:52 +0930144 info->rst = devm_reset_control_get_optional_shared(&ofdev->dev, NULL);
145 if (IS_ERR(info->rst))
Alexey Khoroshilovfa9ba3ac2017-07-19 11:32:37 +0300146 goto err_dispose;
Joel Stanleye2860e12017-05-29 19:27:52 +0930147 ret = reset_control_deassert(info->rst);
148 if (ret)
Alexey Khoroshilovfa9ba3ac2017-07-19 11:32:37 +0300149 goto err_dispose;
Joel Stanleye2860e12017-05-29 19:27:52 +0930150
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100151 port->type = type;
Grant Likelyb84e7732011-06-30 12:39:12 -0600152 port->uartclk = clk;
David Gibsonabb4a232007-05-06 14:48:49 -0700153 port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
Dave Mitchelleedacbf2009-06-09 13:39:47 +0000154 | UPF_FIXED_PORT | UPF_FIXED_TYPE;
Gabor Juhosfde8be22012-07-17 17:08:31 +0100155
Sergei Shtylyov5c98e9c2017-08-20 19:51:55 +0300156 if (of_property_read_bool(np, "no-loopback-test"))
Gabor Juhosfde8be22012-07-17 17:08:31 +0100157 port->flags |= UPF_SKIP_TEST;
158
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100159 port->dev = &ofdev->dev;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100160
John Crispin9b8777e2014-10-16 21:48:21 +0200161 switch (type) {
162 case PORT_TEGRA:
Dan Williamsbf03f652012-04-10 14:10:53 -0700163 port->handle_break = tegra_serial_handle_break;
John Crispin9b8777e2014-10-16 21:48:21 +0200164 break;
165
166 case PORT_RT2880:
167 port->iotype = UPIO_AU;
168 break;
169 }
Dan Williamsbf03f652012-04-10 14:10:53 -0700170
Scott Woodd43b54d2015-10-07 17:31:21 -0500171 if (IS_ENABLED(CONFIG_SERIAL_8250_FSL) &&
172 (of_device_is_compatible(np, "fsl,ns16550") ||
173 of_device_is_compatible(np, "fsl,16550-FIFO64")))
174 port->handle_irq = fsl8250_handle_irq;
175
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100176 return 0;
Alexey Khoroshilovfa9ba3ac2017-07-19 11:32:37 +0300177err_dispose:
178 irq_dispose_mapping(port->irq);
179err_unprepare:
Franklin S Cooper Jra2d23eda2017-08-16 15:55:36 -0500180 clk_disable_unprepare(info->clk);
181err_pmruntime:
182 pm_runtime_put_sync(&ofdev->dev);
183 pm_runtime_disable(&ofdev->dev);
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -0400184 return ret;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100185}
186
187/*
188 * Try to register a serial port
189 */
Fabian Fredericked0bb232015-03-16 20:17:11 +0100190static const struct of_device_id of_platform_serial_table[];
Bill Pemberton9671f092012-11-19 13:21:50 -0500191static int of_platform_serial_probe(struct platform_device *ofdev)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100192{
Grant Likelyb1608d62011-05-18 11:19:24 -0600193 const struct of_device_id *match;
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000194 struct of_serial_info *info;
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100195 struct uart_8250_port port8250;
196 u32 tx_threshold;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100197 int port_type;
198 int ret;
199
Grant Likelyb1608d62011-05-18 11:19:24 -0600200 match = of_match_device(of_platform_serial_table, &ofdev->dev);
201 if (!match)
Grant Likely793218d2011-02-22 21:10:26 -0700202 return -EINVAL;
203
Sergei Shtylyov5c98e9c2017-08-20 19:51:55 +0300204 if (of_property_read_bool(ofdev->dev.of_node, "used-by-rtas"))
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100205 return -EBUSY;
206
Jingchang Lu7e12e672014-10-21 16:50:21 +0800207 info = kzalloc(sizeof(*info), GFP_KERNEL);
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000208 if (info == NULL)
209 return -ENOMEM;
210
Grant Likelyb1608d62011-05-18 11:19:24 -0600211 port_type = (unsigned long)match->data;
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100212 memset(&port8250, 0, sizeof(port8250));
213 ret = of_platform_serial_setup(ofdev, port_type, &port8250.port, info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100214 if (ret)
Alexey Khoroshilovfa9ba3ac2017-07-19 11:32:37 +0300215 goto err_free;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100216
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100217 if (port8250.port.fifosize)
218 port8250.capabilities = UART_CAP_FIFO;
Heikki Krogerusb0b8c842013-03-25 15:51:15 +0200219
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100220 /* Check for TX FIFO threshold & set tx_loadsz */
221 if ((of_property_read_u32(ofdev->dev.of_node, "tx-threshold",
222 &tx_threshold) == 0) &&
223 (tx_threshold < port8250.port.fifosize))
224 port8250.tx_loadsz = port8250.port.fifosize - tx_threshold;
Heikki Krogerusb0b8c842013-03-25 15:51:15 +0200225
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100226 if (of_property_read_bool(ofdev->dev.of_node, "auto-flow-control"))
227 port8250.capabilities |= UART_CAP_AFE;
Thor Thayerffea0432016-09-22 14:56:15 -0500228
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100229 ret = serial8250_register_8250_port(&port8250);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100230 if (ret < 0)
Alexey Khoroshilovfa9ba3ac2017-07-19 11:32:37 +0300231 goto err_dispose;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100232
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000233 info->type = port_type;
234 info->line = ret;
Jingoo Han696faed2013-05-23 19:39:36 +0900235 platform_set_drvdata(ofdev, info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100236 return 0;
Alexey Khoroshilovfa9ba3ac2017-07-19 11:32:37 +0300237err_dispose:
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100238 irq_dispose_mapping(port8250.port.irq);
Franklin S Cooper Jra2d23eda2017-08-16 15:55:36 -0500239 pm_runtime_put_sync(&ofdev->dev);
240 pm_runtime_disable(&ofdev->dev);
241 clk_disable_unprepare(info->clk);
Alexey Khoroshilovfa9ba3ac2017-07-19 11:32:37 +0300242err_free:
243 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100244 return ret;
245}
246
247/*
248 * Release a line
249 */
Grant Likely2dc11582010-08-06 09:25:50 -0600250static int of_platform_serial_remove(struct platform_device *ofdev)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100251{
Jingoo Han696faed2013-05-23 19:39:36 +0900252 struct of_serial_info *info = platform_get_drvdata(ofdev);
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100253
254 serial8250_unregister_port(info->line);
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -0400255
Joel Stanleye2860e12017-05-29 19:27:52 +0930256 reset_control_assert(info->rst);
Franklin S Cooper Jra2d23eda2017-08-16 15:55:36 -0500257 pm_runtime_put_sync(&ofdev->dev);
258 pm_runtime_disable(&ofdev->dev);
259 clk_disable_unprepare(info->clk);
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000260 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100261 return 0;
262}
263
Jingchang Lu8ad3b132014-11-11 15:09:05 +0800264#ifdef CONFIG_PM_SLEEP
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100265static int of_serial_suspend(struct device *dev)
Jingchang Lu8ad3b132014-11-11 15:09:05 +0800266{
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100267 struct of_serial_info *info = dev_get_drvdata(dev);
Jingchang Lu8ad3b132014-11-11 15:09:05 +0800268 struct uart_8250_port *port8250 = serial8250_get_port(info->line);
269 struct uart_port *port = &port8250->port;
270
271 serial8250_suspend_port(info->line);
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100272
Franklin S Cooper Jra2d23eda2017-08-16 15:55:36 -0500273 if (!uart_console(port) || console_suspend_enabled) {
274 pm_runtime_put_sync(dev);
Jingchang Lu8ad3b132014-11-11 15:09:05 +0800275 clk_disable_unprepare(info->clk);
Franklin S Cooper Jra2d23eda2017-08-16 15:55:36 -0500276 }
Jingchang Lu8ad3b132014-11-11 15:09:05 +0800277 return 0;
278}
279
280static int of_serial_resume(struct device *dev)
281{
282 struct of_serial_info *info = dev_get_drvdata(dev);
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100283 struct uart_8250_port *port8250 = serial8250_get_port(info->line);
284 struct uart_port *port = &port8250->port;
Jingchang Lu8ad3b132014-11-11 15:09:05 +0800285
Franklin S Cooper Jra2d23eda2017-08-16 15:55:36 -0500286 if (!uart_console(port) || console_suspend_enabled) {
287 pm_runtime_get_sync(dev);
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100288 clk_prepare_enable(info->clk);
Franklin S Cooper Jra2d23eda2017-08-16 15:55:36 -0500289 }
Arnd Bergmannaa42db42017-01-25 23:19:01 +0100290
291 serial8250_resume_port(info->line);
Jingchang Lu8ad3b132014-11-11 15:09:05 +0800292
293 return 0;
294}
295#endif
296static SIMPLE_DEV_PM_OPS(of_serial_pm_ops, of_serial_suspend, of_serial_resume);
297
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100298/*
299 * A few common types, add more as needed.
300 */
Fabian Fredericked0bb232015-03-16 20:17:11 +0100301static const struct of_device_id of_platform_serial_table[] = {
Grant Likely8c6e9112011-02-22 19:12:21 -0700302 { .compatible = "ns8250", .data = (void *)PORT_8250, },
303 { .compatible = "ns16450", .data = (void *)PORT_16450, },
304 { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
305 { .compatible = "ns16550", .data = (void *)PORT_16550, },
306 { .compatible = "ns16750", .data = (void *)PORT_16750, },
307 { .compatible = "ns16850", .data = (void *)PORT_16850, },
Grant Likely2e39e5b2011-07-05 23:42:36 -0600308 { .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, },
Roland Stiggee4305f02012-06-11 21:57:14 +0200309 { .compatible = "nxp,lpc3220-uart", .data = (void *)PORT_LPC3220, },
John Crispin9b8777e2014-10-16 21:48:21 +0200310 { .compatible = "ralink,rt2880-uart", .data = (void *)PORT_RT2880, },
Ley Foon Tane06c93c2013-03-07 10:28:37 +0800311 { .compatible = "altr,16550-FIFO32",
312 .data = (void *)PORT_ALTR_16550_F32, },
313 { .compatible = "altr,16550-FIFO64",
314 .data = (void *)PORT_ALTR_16550_F64, },
315 { .compatible = "altr,16550-FIFO128",
316 .data = (void *)PORT_ALTR_16550_F128, },
Sean Wang1c16ae62017-08-21 01:17:56 +0800317 { .compatible = "mediatek,mtk-btif",
318 .data = (void *)PORT_MTK_BTIF, },
Rob Herring6ad991b2015-01-26 22:50:08 -0600319 { .compatible = "mrvl,mmp-uart",
320 .data = (void *)PORT_XSCALE, },
David Lechnera2d6a982017-01-05 12:54:18 -0600321 { .compatible = "ti,da830-uart", .data = (void *)PORT_DA830, },
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100322 { /* end of list */ },
323};
Luis de Bethencourt8d58db12015-09-18 20:03:43 +0200324MODULE_DEVICE_TABLE(of, of_platform_serial_table);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100325
Grant Likely793218d2011-02-22 21:10:26 -0700326static struct platform_driver of_platform_serial_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700327 .driver = {
328 .name = "of_serial",
Grant Likely40182942010-04-13 16:13:02 -0700329 .of_match_table = of_platform_serial_table,
Wang Dongsheng434ba162016-01-21 15:59:53 +0800330 .pm = &of_serial_pm_ops,
Grant Likely40182942010-04-13 16:13:02 -0700331 },
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100332 .probe = of_platform_serial_probe,
333 .remove = of_platform_serial_remove,
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100334};
335
Grant Likely940ab882011-10-05 11:29:49 -0600336module_platform_driver(of_platform_serial_driver);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100337
338MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
339MODULE_LICENSE("GPL");
340MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");