blob: 6823df99bd7685db295b94599e6bd1b7f7a78057 [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
Heikki Krogerusb0b8c842013-03-25 15:51:15 +020024#include "8250/8250.h"
25
Ishizaki Koue34b9c92007-05-31 19:33:04 +100026struct of_serial_info {
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040027 struct clk *clk;
Ishizaki Koue34b9c92007-05-31 19:33:04 +100028 int type;
29 int line;
30};
31
Dan Williamsbf03f652012-04-10 14:10:53 -070032#ifdef CONFIG_ARCH_TEGRA
33void tegra_serial_handle_break(struct uart_port *p)
34{
35 unsigned int status, tmout = 10000;
36
37 do {
38 status = p->serial_in(p, UART_LSR);
39 if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
40 status = p->serial_in(p, UART_RX);
41 else
42 break;
43 if (--tmout == 0)
44 break;
45 udelay(1);
46 } while (1);
47}
Stephen Warrenf26402e2013-01-31 12:01:53 -070048#else
49static inline void tegra_serial_handle_break(struct uart_port *port)
50{
51}
Dan Williamsbf03f652012-04-10 14:10:53 -070052#endif
53
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010054/*
55 * Fill a struct uart_port for a given device node
56 */
Bill Pemberton9671f092012-11-19 13:21:50 -050057static int of_platform_serial_setup(struct platform_device *ofdev,
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040058 int type, struct uart_port *port,
59 struct of_serial_info *info)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010060{
61 struct resource resource;
Grant Likely61c7a082010-04-13 16:12:29 -070062 struct device_node *np = ofdev->dev.of_node;
Grant Likelyb84e7732011-06-30 12:39:12 -060063 u32 clk, spd, prop;
64 int ret;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010065
66 memset(port, 0, sizeof *port);
Grant Likelyb84e7732011-06-30 12:39:12 -060067 if (of_property_read_u32(np, "clock-frequency", &clk)) {
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040068
69 /* Get clk rate through clk driver if present */
Masahiro Yamada3a63d222015-05-25 14:57:43 +090070 info->clk = devm_clk_get(&ofdev->dev, NULL);
Wei Yongjun76cc4382012-11-01 13:27:34 +080071 if (IS_ERR(info->clk)) {
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040072 dev_warn(&ofdev->dev,
73 "clk or clock-frequency not defined\n");
Wei Yongjun76cc4382012-11-01 13:27:34 +080074 return PTR_ERR(info->clk);
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040075 }
76
Masahiro Yamada6f0c3092015-05-25 15:03:32 +090077 ret = clk_prepare_enable(info->clk);
78 if (ret < 0)
79 return ret;
80
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040081 clk = clk_get_rate(info->clk);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010082 }
Grant Likelyb84e7732011-06-30 12:39:12 -060083 /* If current-speed was set, then try not to change it. */
84 if (of_property_read_u32(np, "current-speed", &spd) == 0)
85 port->custom_divisor = clk / (16 * spd);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010086
87 ret = of_address_to_resource(np, 0, &resource);
88 if (ret) {
89 dev_warn(&ofdev->dev, "invalid address\n");
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -040090 goto out;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010091 }
92
93 spin_lock_init(&port->lock);
94 port->mapbase = resource.start;
Mans Rullgard07876912015-03-08 14:30:05 +000095 port->mapsize = resource_size(&resource);
John Linnb912b5e2008-04-03 10:22:19 +110096
97 /* Check for shifted address mapping */
Grant Likelyb84e7732011-06-30 12:39:12 -060098 if (of_property_read_u32(np, "reg-offset", &prop) == 0)
99 port->mapbase += prop;
John Linnb912b5e2008-04-03 10:22:19 +1100100
101 /* Check for registers offset within the devices address range */
Grant Likelyb84e7732011-06-30 12:39:12 -0600102 if (of_property_read_u32(np, "reg-shift", &prop) == 0)
103 port->regshift = prop;
John Linnb912b5e2008-04-03 10:22:19 +1100104
Heikki Krogerus9f1ca062013-03-25 13:34:45 +0200105 /* Check for fifo size */
106 if (of_property_read_u32(np, "fifo-size", &prop) == 0)
107 port->fifosize = prop;
108
Lucas Stach3239fd32014-12-05 20:21:57 +0100109 /* Check for a fixed line number */
110 ret = of_alias_get_id(np, "serial");
111 if (ret >= 0)
112 port->line = ret;
113
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100114 port->irq = irq_of_parse_and_map(np, 0);
115 port->iotype = UPIO_MEM;
Grant Likelyb84e7732011-06-30 12:39:12 -0600116 if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
117 switch (prop) {
Jamie Iles74237342011-06-27 13:32:34 +0100118 case 1:
119 port->iotype = UPIO_MEM;
120 break;
121 case 4:
Kevin Cernekeeebc5e202015-04-09 13:05:18 -0700122 port->iotype = of_device_is_big_endian(np) ?
123 UPIO_MEM32BE : UPIO_MEM32;
Jamie Iles74237342011-06-27 13:32:34 +0100124 break;
125 default:
Grant Likelyb84e7732011-06-30 12:39:12 -0600126 dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n",
127 prop);
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -0400128 ret = -EINVAL;
129 goto out;
Jamie Iles74237342011-06-27 13:32:34 +0100130 }
131 }
132
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100133 port->type = type;
Grant Likelyb84e7732011-06-30 12:39:12 -0600134 port->uartclk = clk;
David Gibsonabb4a232007-05-06 14:48:49 -0700135 port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
Dave Mitchelleedacbf2009-06-09 13:39:47 +0000136 | UPF_FIXED_PORT | UPF_FIXED_TYPE;
Gabor Juhosfde8be22012-07-17 17:08:31 +0100137
138 if (of_find_property(np, "no-loopback-test", NULL))
139 port->flags |= UPF_SKIP_TEST;
140
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100141 port->dev = &ofdev->dev;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100142
John Crispin9b8777e2014-10-16 21:48:21 +0200143 switch (type) {
144 case PORT_TEGRA:
Dan Williamsbf03f652012-04-10 14:10:53 -0700145 port->handle_break = tegra_serial_handle_break;
John Crispin9b8777e2014-10-16 21:48:21 +0200146 break;
147
148 case PORT_RT2880:
149 port->iotype = UPIO_AU;
150 break;
151 }
Dan Williamsbf03f652012-04-10 14:10:53 -0700152
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100153 return 0;
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -0400154out:
155 if (info->clk)
156 clk_disable_unprepare(info->clk);
157 return ret;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100158}
159
160/*
161 * Try to register a serial port
162 */
Fabian Fredericked0bb232015-03-16 20:17:11 +0100163static const struct of_device_id of_platform_serial_table[];
Bill Pemberton9671f092012-11-19 13:21:50 -0500164static int of_platform_serial_probe(struct platform_device *ofdev)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100165{
Grant Likelyb1608d62011-05-18 11:19:24 -0600166 const struct of_device_id *match;
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000167 struct of_serial_info *info;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100168 struct uart_port port;
169 int port_type;
170 int ret;
171
Grant Likelyb1608d62011-05-18 11:19:24 -0600172 match = of_match_device(of_platform_serial_table, &ofdev->dev);
173 if (!match)
Grant Likely793218d2011-02-22 21:10:26 -0700174 return -EINVAL;
175
Grant Likely61c7a082010-04-13 16:12:29 -0700176 if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100177 return -EBUSY;
178
Jingchang Lu7e12e672014-10-21 16:50:21 +0800179 info = kzalloc(sizeof(*info), GFP_KERNEL);
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000180 if (info == NULL)
181 return -ENOMEM;
182
Grant Likelyb1608d62011-05-18 11:19:24 -0600183 port_type = (unsigned long)match->data;
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -0400184 ret = of_platform_serial_setup(ofdev, port_type, &port, info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100185 if (ret)
186 goto out;
187
188 switch (port_type) {
Benjamin Krill5886188d2009-01-07 10:32:38 +0100189#ifdef CONFIG_SERIAL_8250
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100190 case PORT_8250 ... PORT_MAX_8250:
Alan Coxce7240e2012-07-17 17:06:20 +0100191 {
Alan Coxce7240e2012-07-17 17:06:20 +0100192 struct uart_8250_port port8250;
193 memset(&port8250, 0, sizeof(port8250));
194 port8250.port = port;
Heikki Krogerusb0b8c842013-03-25 15:51:15 +0200195
196 if (port.fifosize)
197 port8250.capabilities = UART_CAP_FIFO;
198
199 if (of_property_read_bool(ofdev->dev.of_node,
200 "auto-flow-control"))
201 port8250.capabilities |= UART_CAP_AFE;
202
Alan Coxce7240e2012-07-17 17:06:20 +0100203 ret = serial8250_register_8250_port(&port8250);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100204 break;
Alan Coxce7240e2012-07-17 17:06:20 +0100205 }
Benjamin Krill5886188d2009-01-07 10:32:38 +0100206#endif
207#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
208 case PORT_NWPSERIAL:
209 ret = nwpserial_register_port(&port);
210 break;
211#endif
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100212 default:
213 /* need to add code for these */
Ishizaki Kou1558f9b2007-05-31 19:30:33 +1000214 case PORT_UNKNOWN:
215 dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100216 ret = -ENODEV;
217 break;
218 }
219 if (ret < 0)
220 goto out;
221
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000222 info->type = port_type;
223 info->line = ret;
Jingoo Han696faed2013-05-23 19:39:36 +0900224 platform_set_drvdata(ofdev, info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100225 return 0;
226out:
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000227 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100228 irq_dispose_mapping(port.irq);
229 return ret;
230}
231
232/*
233 * Release a line
234 */
Grant Likely2dc11582010-08-06 09:25:50 -0600235static int of_platform_serial_remove(struct platform_device *ofdev)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100236{
Jingoo Han696faed2013-05-23 19:39:36 +0900237 struct of_serial_info *info = platform_get_drvdata(ofdev);
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000238 switch (info->type) {
Benjamin Krill5886188d2009-01-07 10:32:38 +0100239#ifdef CONFIG_SERIAL_8250
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000240 case PORT_8250 ... PORT_MAX_8250:
241 serial8250_unregister_port(info->line);
242 break;
Benjamin Krill5886188d2009-01-07 10:32:38 +0100243#endif
244#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
245 case PORT_NWPSERIAL:
246 nwpserial_unregister_port(info->line);
247 break;
248#endif
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000249 default:
250 /* need to add code for these */
251 break;
252 }
Murali Karicheri0bbeb3c2012-10-22 11:58:01 -0400253
254 if (info->clk)
255 clk_disable_unprepare(info->clk);
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000256 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100257 return 0;
258}
259
Jingchang Lu8ad3b132014-11-11 15:09:05 +0800260#ifdef CONFIG_PM_SLEEP
261#ifdef CONFIG_SERIAL_8250
262static void of_serial_suspend_8250(struct of_serial_info *info)
263{
264 struct uart_8250_port *port8250 = serial8250_get_port(info->line);
265 struct uart_port *port = &port8250->port;
266
267 serial8250_suspend_port(info->line);
268 if (info->clk && (!uart_console(port) || console_suspend_enabled))
269 clk_disable_unprepare(info->clk);
270}
271
272static void of_serial_resume_8250(struct of_serial_info *info)
273{
274 struct uart_8250_port *port8250 = serial8250_get_port(info->line);
275 struct uart_port *port = &port8250->port;
276
277 if (info->clk && (!uart_console(port) || console_suspend_enabled))
278 clk_prepare_enable(info->clk);
279
280 serial8250_resume_port(info->line);
281}
282#else
283static inline void of_serial_suspend_8250(struct of_serial_info *info)
284{
285}
286
287static inline void of_serial_resume_8250(struct of_serial_info *info)
288{
289}
290#endif
291
292static int of_serial_suspend(struct device *dev)
293{
294 struct of_serial_info *info = dev_get_drvdata(dev);
295
296 switch (info->type) {
297 case PORT_8250 ... PORT_MAX_8250:
298 of_serial_suspend_8250(info);
299 break;
300 default:
301 break;
302 }
303
304 return 0;
305}
306
307static int of_serial_resume(struct device *dev)
308{
309 struct of_serial_info *info = dev_get_drvdata(dev);
310
311 switch (info->type) {
312 case PORT_8250 ... PORT_MAX_8250:
313 of_serial_resume_8250(info);
314 break;
315 default:
316 break;
317 }
318
319 return 0;
320}
321#endif
322static SIMPLE_DEV_PM_OPS(of_serial_pm_ops, of_serial_suspend, of_serial_resume);
323
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100324/*
325 * A few common types, add more as needed.
326 */
Fabian Fredericked0bb232015-03-16 20:17:11 +0100327static const struct of_device_id of_platform_serial_table[] = {
Grant Likely8c6e9112011-02-22 19:12:21 -0700328 { .compatible = "ns8250", .data = (void *)PORT_8250, },
329 { .compatible = "ns16450", .data = (void *)PORT_16450, },
330 { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
331 { .compatible = "ns16550", .data = (void *)PORT_16550, },
332 { .compatible = "ns16750", .data = (void *)PORT_16750, },
333 { .compatible = "ns16850", .data = (void *)PORT_16850, },
Grant Likely2e39e5b2011-07-05 23:42:36 -0600334 { .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, },
Roland Stiggee4305f02012-06-11 21:57:14 +0200335 { .compatible = "nxp,lpc3220-uart", .data = (void *)PORT_LPC3220, },
John Crispin9b8777e2014-10-16 21:48:21 +0200336 { .compatible = "ralink,rt2880-uart", .data = (void *)PORT_RT2880, },
Ley Foon Tane06c93c2013-03-07 10:28:37 +0800337 { .compatible = "altr,16550-FIFO32",
338 .data = (void *)PORT_ALTR_16550_F32, },
339 { .compatible = "altr,16550-FIFO64",
340 .data = (void *)PORT_ALTR_16550_F64, },
341 { .compatible = "altr,16550-FIFO128",
342 .data = (void *)PORT_ALTR_16550_F128, },
Rob Herring6ad991b2015-01-26 22:50:08 -0600343 { .compatible = "mrvl,mmp-uart",
344 .data = (void *)PORT_XSCALE, },
345 { .compatible = "mrvl,pxa-uart",
346 .data = (void *)PORT_XSCALE, },
Benjamin Krill5886188d2009-01-07 10:32:38 +0100347#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
Grant Likely8c6e9112011-02-22 19:12:21 -0700348 { .compatible = "ibm,qpace-nwp-serial",
349 .data = (void *)PORT_NWPSERIAL, },
Benjamin Krill5886188d2009-01-07 10:32:38 +0100350#endif
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100351 { /* end of list */ },
352};
353
Grant Likely793218d2011-02-22 21:10:26 -0700354static struct platform_driver of_platform_serial_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700355 .driver = {
356 .name = "of_serial",
Grant Likely40182942010-04-13 16:13:02 -0700357 .of_match_table = of_platform_serial_table,
358 },
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100359 .probe = of_platform_serial_probe,
360 .remove = of_platform_serial_remove,
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100361};
362
Grant Likely940ab882011-10-05 11:29:49 -0600363module_platform_driver(of_platform_serial_driver);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100364
365MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
366MODULE_LICENSE("GPL");
367MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");