blob: 920468bf4e8336c538e34b36937edde06b63686c [file] [log] [blame]
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +01001/*
2 * Serial Port driver for Open Firmware platform devices
3 *
4 * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Jingchang Lu8ad3b132014-11-11 15:09:05 +080012#include <linux/console.h>
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010013#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Dan Williamsbf03f652012-04-10 14:10:53 -070015#include <linux/delay.h>
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010016#include <linux/serial_core.h>
Dan Williamsbf03f652012-04-10 14:10:53 -070017#include <linux/serial_reg.h>
Grant Likelyf1ca09b2010-08-16 23:44:49 -060018#include <linux/of_address.h>
Rob Herring73930a82010-11-17 17:50:23 -060019#include <linux/of_irq.h>
Stephen Rothwellc401b042008-05-23 16:32:54 +100020#include <linux/of_platform.h>
Benjamin Krill5886188d2009-01-07 10:32:38 +010021#include <linux/nwpserial.h>
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040022#include <linux/clk.h>
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010023
Martin Sperled4492f2015-09-11 11:38:03 +000024#ifdef CONFIG_SERIAL_8250_MODULE
25#define CONFIG_SERIAL_8250 CONFIG_SERIAL_8250_MODULE
26#endif
27
Heikki Krogerusb0b8c842013-03-25 15:51:15 +020028#include "8250/8250.h"
29
Ishizaki Koue34b9c92007-05-31 19:33:04 +100030struct of_serial_info {
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040031 struct clk *clk;
Ishizaki Koue34b9c92007-05-31 19:33:04 +100032 int type;
33 int line;
34};
35
Dan Williamsbf03f652012-04-10 14:10:53 -070036#ifdef CONFIG_ARCH_TEGRA
37void tegra_serial_handle_break(struct uart_port *p)
38{
39 unsigned int status, tmout = 10000;
40
41 do {
42 status = p->serial_in(p, UART_LSR);
43 if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
44 status = p->serial_in(p, UART_RX);
45 else
46 break;
47 if (--tmout == 0)
48 break;
49 udelay(1);
50 } while (1);
51}
Stephen Warrenf26402e2013-01-31 12:01:53 -070052#else
53static inline void tegra_serial_handle_break(struct uart_port *port)
54{
55}
Dan Williamsbf03f652012-04-10 14:10:53 -070056#endif
57
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010058/*
59 * Fill a struct uart_port for a given device node
60 */
Bill Pemberton9671f092012-11-19 13:21:50 -050061static int of_platform_serial_setup(struct platform_device *ofdev,
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040062 int type, struct uart_port *port,
63 struct of_serial_info *info)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010064{
65 struct resource resource;
Grant Likely61c7a082010-04-13 16:12:29 -070066 struct device_node *np = ofdev->dev.of_node;
Grant Likelyb84e7732011-06-30 12:39:12 -060067 u32 clk, spd, prop;
68 int ret;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010069
70 memset(port, 0, sizeof *port);
Grant Likelyb84e7732011-06-30 12:39:12 -060071 if (of_property_read_u32(np, "clock-frequency", &clk)) {
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040072
73 /* Get clk rate through clk driver if present */
Masahiro Yamada3a63d222015-05-25 14:57:43 +090074 info->clk = devm_clk_get(&ofdev->dev, NULL);
Wei Yongjun76cc4382012-11-01 13:27:34 +080075 if (IS_ERR(info->clk)) {
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040076 dev_warn(&ofdev->dev,
77 "clk or clock-frequency not defined\n");
Wei Yongjun76cc4382012-11-01 13:27:34 +080078 return PTR_ERR(info->clk);
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040079 }
80
Masahiro Yamada6f0c3092015-05-25 15:03:32 +090081 ret = clk_prepare_enable(info->clk);
82 if (ret < 0)
83 return ret;
84
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040085 clk = clk_get_rate(info->clk);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010086 }
Grant Likelyb84e7732011-06-30 12:39:12 -060087 /* If current-speed was set, then try not to change it. */
88 if (of_property_read_u32(np, "current-speed", &spd) == 0)
89 port->custom_divisor = clk / (16 * spd);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010090
91 ret = of_address_to_resource(np, 0, &resource);
92 if (ret) {
93 dev_warn(&ofdev->dev, "invalid address\n");
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040094 goto out;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010095 }
96
97 spin_lock_init(&port->lock);
98 port->mapbase = resource.start;
Mans Rullgard07876912015-03-08 14:30:05 +000099 port->mapsize = resource_size(&resource);
John Linnb912b5e2008-04-03 10:22:19 +1100100
101 /* Check for shifted address mapping */
Grant Likelyb84e7732011-06-30 12:39:12 -0600102 if (of_property_read_u32(np, "reg-offset", &prop) == 0)
103 port->mapbase += prop;
John Linnb912b5e2008-04-03 10:22:19 +1100104
105 /* Check for registers offset within the devices address range */
Grant Likelyb84e7732011-06-30 12:39:12 -0600106 if (of_property_read_u32(np, "reg-shift", &prop) == 0)
107 port->regshift = prop;
John Linnb912b5e2008-04-03 10:22:19 +1100108
Heikki Krogerus9f1ca062013-03-25 13:34:45 +0200109 /* Check for fifo size */
110 if (of_property_read_u32(np, "fifo-size", &prop) == 0)
111 port->fifosize = prop;
112
Lucas Stach3239fd32014-12-05 20:21:57 +0100113 /* Check for a fixed line number */
114 ret = of_alias_get_id(np, "serial");
115 if (ret >= 0)
116 port->line = ret;
117
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100118 port->irq = irq_of_parse_and_map(np, 0);
119 port->iotype = UPIO_MEM;
Grant Likelyb84e7732011-06-30 12:39:12 -0600120 if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
121 switch (prop) {
Jamie Iles74237342011-06-27 13:32:34 +0100122 case 1:
123 port->iotype = UPIO_MEM;
124 break;
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900125 case 2:
126 port->iotype = UPIO_MEM16;
127 break;
Jamie Iles74237342011-06-27 13:32:34 +0100128 case 4:
Kevin Cernekeeebc5e202015-04-09 13:05:18 -0700129 port->iotype = of_device_is_big_endian(np) ?
130 UPIO_MEM32BE : UPIO_MEM32;
Jamie Iles74237342011-06-27 13:32:34 +0100131 break;
132 default:
Grant Likelyb84e7732011-06-30 12:39:12 -0600133 dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n",
134 prop);
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -0400135 ret = -EINVAL;
136 goto out;
Jamie Iles74237342011-06-27 13:32:34 +0100137 }
138 }
139
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100140 port->type = type;
Grant Likelyb84e7732011-06-30 12:39:12 -0600141 port->uartclk = clk;
David Gibsonabb4a232007-05-06 14:48:49 -0700142 port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
Dave Mitchelleedacbf2009-06-09 13:39:47 +0000143 | UPF_FIXED_PORT | UPF_FIXED_TYPE;
Gabor Juhosfde8be22012-07-17 17:08:31 +0100144
145 if (of_find_property(np, "no-loopback-test", NULL))
146 port->flags |= UPF_SKIP_TEST;
147
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100148 port->dev = &ofdev->dev;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100149
John Crispin9b8777e2014-10-16 21:48:21 +0200150 switch (type) {
151 case PORT_TEGRA:
Dan Williamsbf03f652012-04-10 14:10:53 -0700152 port->handle_break = tegra_serial_handle_break;
John Crispin9b8777e2014-10-16 21:48:21 +0200153 break;
154
155 case PORT_RT2880:
156 port->iotype = UPIO_AU;
157 break;
158 }
Dan Williamsbf03f652012-04-10 14:10:53 -0700159
Scott Woodd43b54d2015-10-07 17:31:21 -0500160 if (IS_ENABLED(CONFIG_SERIAL_8250_FSL) &&
161 (of_device_is_compatible(np, "fsl,ns16550") ||
162 of_device_is_compatible(np, "fsl,16550-FIFO64")))
163 port->handle_irq = fsl8250_handle_irq;
164
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100165 return 0;
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -0400166out:
167 if (info->clk)
168 clk_disable_unprepare(info->clk);
169 return ret;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100170}
171
172/*
173 * Try to register a serial port
174 */
Fabian Fredericked0bb232015-03-16 20:17:11 +0100175static const struct of_device_id of_platform_serial_table[];
Bill Pemberton9671f092012-11-19 13:21:50 -0500176static int of_platform_serial_probe(struct platform_device *ofdev)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100177{
Grant Likelyb1608d62011-05-18 11:19:24 -0600178 const struct of_device_id *match;
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000179 struct of_serial_info *info;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100180 struct uart_port port;
181 int port_type;
182 int ret;
183
Grant Likelyb1608d62011-05-18 11:19:24 -0600184 match = of_match_device(of_platform_serial_table, &ofdev->dev);
185 if (!match)
Grant Likely793218d2011-02-22 21:10:26 -0700186 return -EINVAL;
187
Grant Likely61c7a082010-04-13 16:12:29 -0700188 if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100189 return -EBUSY;
190
Jingchang Lu7e12e672014-10-21 16:50:21 +0800191 info = kzalloc(sizeof(*info), GFP_KERNEL);
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000192 if (info == NULL)
193 return -ENOMEM;
194
Grant Likelyb1608d62011-05-18 11:19:24 -0600195 port_type = (unsigned long)match->data;
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -0400196 ret = of_platform_serial_setup(ofdev, port_type, &port, info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100197 if (ret)
198 goto out;
199
200 switch (port_type) {
Benjamin Krill5886188d2009-01-07 10:32:38 +0100201#ifdef CONFIG_SERIAL_8250
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100202 case PORT_8250 ... PORT_MAX_8250:
Alan Coxce7240e2012-07-17 17:06:20 +0100203 {
Alan Coxce7240e2012-07-17 17:06:20 +0100204 struct uart_8250_port port8250;
205 memset(&port8250, 0, sizeof(port8250));
206 port8250.port = port;
Heikki Krogerusb0b8c842013-03-25 15:51:15 +0200207
208 if (port.fifosize)
209 port8250.capabilities = UART_CAP_FIFO;
210
211 if (of_property_read_bool(ofdev->dev.of_node,
212 "auto-flow-control"))
213 port8250.capabilities |= UART_CAP_AFE;
214
Alan Coxce7240e2012-07-17 17:06:20 +0100215 ret = serial8250_register_8250_port(&port8250);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100216 break;
Alan Coxce7240e2012-07-17 17:06:20 +0100217 }
Benjamin Krill5886188d2009-01-07 10:32:38 +0100218#endif
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100219 default:
220 /* need to add code for these */
Ishizaki Kou1558f9b2007-05-31 19:30:33 +1000221 case PORT_UNKNOWN:
222 dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100223 ret = -ENODEV;
224 break;
225 }
226 if (ret < 0)
227 goto out;
228
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000229 info->type = port_type;
230 info->line = ret;
Jingoo Han696faed2013-05-23 19:39:36 +0900231 platform_set_drvdata(ofdev, info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100232 return 0;
233out:
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000234 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100235 irq_dispose_mapping(port.irq);
236 return ret;
237}
238
239/*
240 * Release a line
241 */
Grant Likely2dc11582010-08-06 09:25:50 -0600242static int of_platform_serial_remove(struct platform_device *ofdev)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100243{
Jingoo Han696faed2013-05-23 19:39:36 +0900244 struct of_serial_info *info = platform_get_drvdata(ofdev);
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000245 switch (info->type) {
Benjamin Krill5886188d2009-01-07 10:32:38 +0100246#ifdef CONFIG_SERIAL_8250
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000247 case PORT_8250 ... PORT_MAX_8250:
248 serial8250_unregister_port(info->line);
249 break;
Benjamin Krill5886188d2009-01-07 10:32:38 +0100250#endif
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000251 default:
252 /* need to add code for these */
253 break;
254 }
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -0400255
256 if (info->clk)
257 clk_disable_unprepare(info->clk);
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000258 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100259 return 0;
260}
261
Jingchang Lu8ad3b132014-11-11 15:09:05 +0800262#ifdef CONFIG_PM_SLEEP
263#ifdef CONFIG_SERIAL_8250
264static void of_serial_suspend_8250(struct of_serial_info *info)
265{
266 struct uart_8250_port *port8250 = serial8250_get_port(info->line);
267 struct uart_port *port = &port8250->port;
268
269 serial8250_suspend_port(info->line);
270 if (info->clk && (!uart_console(port) || console_suspend_enabled))
271 clk_disable_unprepare(info->clk);
272}
273
274static void of_serial_resume_8250(struct of_serial_info *info)
275{
276 struct uart_8250_port *port8250 = serial8250_get_port(info->line);
277 struct uart_port *port = &port8250->port;
278
279 if (info->clk && (!uart_console(port) || console_suspend_enabled))
280 clk_prepare_enable(info->clk);
281
282 serial8250_resume_port(info->line);
283}
284#else
285static inline void of_serial_suspend_8250(struct of_serial_info *info)
286{
287}
288
289static inline void of_serial_resume_8250(struct of_serial_info *info)
290{
291}
292#endif
293
294static int of_serial_suspend(struct device *dev)
295{
296 struct of_serial_info *info = dev_get_drvdata(dev);
297
298 switch (info->type) {
299 case PORT_8250 ... PORT_MAX_8250:
300 of_serial_suspend_8250(info);
301 break;
302 default:
303 break;
304 }
305
306 return 0;
307}
308
309static int of_serial_resume(struct device *dev)
310{
311 struct of_serial_info *info = dev_get_drvdata(dev);
312
313 switch (info->type) {
314 case PORT_8250 ... PORT_MAX_8250:
315 of_serial_resume_8250(info);
316 break;
317 default:
318 break;
319 }
320
321 return 0;
322}
323#endif
324static SIMPLE_DEV_PM_OPS(of_serial_pm_ops, of_serial_suspend, of_serial_resume);
325
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100326/*
327 * A few common types, add more as needed.
328 */
Fabian Fredericked0bb232015-03-16 20:17:11 +0100329static const struct of_device_id of_platform_serial_table[] = {
Grant Likely8c6e9112011-02-22 19:12:21 -0700330 { .compatible = "ns8250", .data = (void *)PORT_8250, },
331 { .compatible = "ns16450", .data = (void *)PORT_16450, },
332 { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
333 { .compatible = "ns16550", .data = (void *)PORT_16550, },
334 { .compatible = "ns16750", .data = (void *)PORT_16750, },
335 { .compatible = "ns16850", .data = (void *)PORT_16850, },
Grant Likely2e39e5b2011-07-05 23:42:36 -0600336 { .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, },
Roland Stiggee4305f02012-06-11 21:57:14 +0200337 { .compatible = "nxp,lpc3220-uart", .data = (void *)PORT_LPC3220, },
John Crispin9b8777e2014-10-16 21:48:21 +0200338 { .compatible = "ralink,rt2880-uart", .data = (void *)PORT_RT2880, },
Ley Foon Tane06c93c2013-03-07 10:28:37 +0800339 { .compatible = "altr,16550-FIFO32",
340 .data = (void *)PORT_ALTR_16550_F32, },
341 { .compatible = "altr,16550-FIFO64",
342 .data = (void *)PORT_ALTR_16550_F64, },
343 { .compatible = "altr,16550-FIFO128",
344 .data = (void *)PORT_ALTR_16550_F128, },
Rob Herring6ad991b2015-01-26 22:50:08 -0600345 { .compatible = "mrvl,mmp-uart",
346 .data = (void *)PORT_XSCALE, },
347 { .compatible = "mrvl,pxa-uart",
348 .data = (void *)PORT_XSCALE, },
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100349 { /* end of list */ },
350};
Luis de Bethencourt8d58db12015-09-18 20:03:43 +0200351MODULE_DEVICE_TABLE(of, of_platform_serial_table);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100352
Grant Likely793218d2011-02-22 21:10:26 -0700353static struct platform_driver of_platform_serial_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700354 .driver = {
355 .name = "of_serial",
Grant Likely40182942010-04-13 16:13:02 -0700356 .of_match_table = of_platform_serial_table,
357 },
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100358 .probe = of_platform_serial_probe,
359 .remove = of_platform_serial_remove,
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100360};
361
Grant Likely940ab882011-10-05 11:29:49 -0600362module_platform_driver(of_platform_serial_driver);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100363
364MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
365MODULE_LICENSE("GPL");
366MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");