blob: b7c7d6a6cad75c47d95d2f02f0992500ed193976 [file] [log] [blame]
Jamie Iles7d4008e2011-08-26 19:04:50 +01001/*
2 * Synopsys DesignWare 8250 driver.
3 *
4 * Copyright 2011 Picochip, Jamie Iles.
Heikki Krogerus6a7320c2013-01-10 11:25:10 +02005 * Copyright 2013 Intel Corporation
Jamie Iles7d4008e2011-08-26 19:04:50 +01006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * The Synopsys DesignWare 8250 has an extra feature whereby it detects if the
13 * LCR is written whilst busy. If it is, then a busy detect interrupt is
14 * raised, the LCR needs to be rewritten and the uart status register read.
15 */
16#include <linux/device.h>
17#include <linux/init.h>
18#include <linux/io.h>
19#include <linux/module.h>
20#include <linux/serial_8250.h>
21#include <linux/serial_core.h>
22#include <linux/serial_reg.h>
23#include <linux/of.h>
24#include <linux/of_irq.h>
25#include <linux/of_platform.h>
26#include <linux/platform_device.h>
27#include <linux/slab.h>
Heikki Krogerus6a7320c2013-01-10 11:25:10 +020028#include <linux/acpi.h>
Emilio Lópeze302cd92013-03-29 00:15:49 +010029#include <linux/clk.h>
Heikki Krogerusffc3ae62013-04-10 16:58:28 +030030#include <linux/pm_runtime.h>
Jamie Iles7d4008e2011-08-26 19:04:50 +010031
Heikki Krogerus7277b2a2013-01-10 11:25:12 +020032#include "8250.h"
33
Heikki Krogerus30046df2013-01-10 11:25:09 +020034/* Offsets for the DesignWare specific registers */
35#define DW_UART_USR 0x1f /* UART Status Register */
36#define DW_UART_CPR 0xf4 /* Component Parameter Register */
37#define DW_UART_UCV 0xf8 /* UART Component Version */
38
39/* Component Parameter Register bits */
40#define DW_UART_CPR_ABP_DATA_WIDTH (3 << 0)
41#define DW_UART_CPR_AFCE_MODE (1 << 4)
42#define DW_UART_CPR_THRE_MODE (1 << 5)
43#define DW_UART_CPR_SIR_MODE (1 << 6)
44#define DW_UART_CPR_SIR_LP_MODE (1 << 7)
45#define DW_UART_CPR_ADDITIONAL_FEATURES (1 << 8)
46#define DW_UART_CPR_FIFO_ACCESS (1 << 9)
47#define DW_UART_CPR_FIFO_STAT (1 << 10)
48#define DW_UART_CPR_SHADOW (1 << 11)
49#define DW_UART_CPR_ENCODED_PARMS (1 << 12)
50#define DW_UART_CPR_DMA_EXTRA (1 << 13)
51#define DW_UART_CPR_FIFO_MODE (0xff << 16)
52/* Helper for fifo size calculation */
53#define DW_UART_CPR_FIFO_SIZE(a) (((a >> 16) & 0xff) * 16)
54
55
Jamie Iles7d4008e2011-08-26 19:04:50 +010056struct dw8250_data {
Emilio Lópeze302cd92013-03-29 00:15:49 +010057 int last_lcr;
58 int line;
59 struct clk *clk;
Jamie Iles7d4008e2011-08-26 19:04:50 +010060};
61
62static void dw8250_serial_out(struct uart_port *p, int offset, int value)
63{
64 struct dw8250_data *d = p->private_data;
65
66 if (offset == UART_LCR)
67 d->last_lcr = value;
68
69 offset <<= p->regshift;
70 writeb(value, p->membase + offset);
71}
72
73static unsigned int dw8250_serial_in(struct uart_port *p, int offset)
74{
75 offset <<= p->regshift;
76
77 return readb(p->membase + offset);
78}
79
80static void dw8250_serial_out32(struct uart_port *p, int offset, int value)
81{
82 struct dw8250_data *d = p->private_data;
83
84 if (offset == UART_LCR)
85 d->last_lcr = value;
86
87 offset <<= p->regshift;
88 writel(value, p->membase + offset);
89}
90
91static unsigned int dw8250_serial_in32(struct uart_port *p, int offset)
92{
93 offset <<= p->regshift;
94
95 return readl(p->membase + offset);
96}
97
Jamie Iles7d4008e2011-08-26 19:04:50 +010098static int dw8250_handle_irq(struct uart_port *p)
99{
100 struct dw8250_data *d = p->private_data;
101 unsigned int iir = p->serial_in(p, UART_IIR);
102
103 if (serial8250_handle_irq(p, iir)) {
104 return 1;
105 } else if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
106 /* Clear the USR and write the LCR again. */
Heikki Krogerus30046df2013-01-10 11:25:09 +0200107 (void)p->serial_in(p, DW_UART_USR);
Maxime Ripard68e56cb2013-01-14 20:09:26 +0100108 p->serial_out(p, UART_LCR, d->last_lcr);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100109
110 return 1;
111 }
112
113 return 0;
114}
115
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300116static void
117dw8250_do_pm(struct uart_port *port, unsigned int state, unsigned int old)
118{
119 if (!state)
120 pm_runtime_get_sync(port->dev);
121
122 serial8250_do_pm(port, state, old);
123
124 if (state)
125 pm_runtime_put_sync_suspend(port->dev);
126}
127
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200128static int dw8250_probe_of(struct uart_port *p)
129{
130 struct device_node *np = p->dev->of_node;
131 u32 val;
132
133 if (!of_property_read_u32(np, "reg-io-width", &val)) {
134 switch (val) {
135 case 1:
136 break;
137 case 4:
138 p->iotype = UPIO_MEM32;
139 p->serial_in = dw8250_serial_in32;
140 p->serial_out = dw8250_serial_out32;
141 break;
142 default:
143 dev_err(p->dev, "unsupported reg-io-width (%u)\n", val);
144 return -EINVAL;
145 }
146 }
147
148 if (!of_property_read_u32(np, "reg-shift", &val))
149 p->regshift = val;
150
Emilio Lópeze302cd92013-03-29 00:15:49 +0100151 /* clock got configured through clk api, all done */
152 if (p->uartclk)
153 return 0;
154
155 /* try to find out clock frequency from DT as fallback */
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200156 if (of_property_read_u32(np, "clock-frequency", &val)) {
Emilio Lópeze302cd92013-03-29 00:15:49 +0100157 dev_err(p->dev, "clk or clock-frequency not defined\n");
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200158 return -EINVAL;
159 }
160 p->uartclk = val;
161
162 return 0;
163}
164
Heikki Krogerus053fac32013-01-16 14:08:15 +0200165#ifdef CONFIG_ACPI
Heikki Krogerus94b2b472013-04-10 16:58:30 +0300166static int dw8250_probe_acpi(struct uart_8250_port *up)
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200167{
168 const struct acpi_device_id *id;
Heikki Krogerus94b2b472013-04-10 16:58:30 +0300169 struct uart_port *p = &up->port;
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200170
171 id = acpi_match_device(p->dev->driver->acpi_match_table, p->dev);
172 if (!id)
173 return -ENODEV;
174
175 p->iotype = UPIO_MEM32;
176 p->serial_in = dw8250_serial_in32;
177 p->serial_out = dw8250_serial_out32;
178 p->regshift = 2;
Heikki Krogerusaea02e82013-04-10 16:58:29 +0300179
180 if (!p->uartclk)
181 p->uartclk = (unsigned int)id->driver_data;
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200182
Heikki Krogerus94b2b472013-04-10 16:58:30 +0300183 up->dma = devm_kzalloc(p->dev, sizeof(*up->dma), GFP_KERNEL);
184 if (!up->dma)
185 return -ENOMEM;
186
187 up->dma->rxconf.src_maxburst = p->fifosize / 4;
188 up->dma->txconf.dst_maxburst = p->fifosize / 4;
Heikki Krogerus7277b2a2013-01-10 11:25:12 +0200189
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200190 return 0;
191}
Heikki Krogerus053fac32013-01-16 14:08:15 +0200192#else
193static inline int dw8250_probe_acpi(struct uart_port *p)
194{
195 return -ENODEV;
196}
197#endif /* CONFIG_ACPI */
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200198
Heikki Krogerus30046df2013-01-10 11:25:09 +0200199static void dw8250_setup_port(struct uart_8250_port *up)
200{
201 struct uart_port *p = &up->port;
202 u32 reg = readl(p->membase + DW_UART_UCV);
203
204 /*
205 * If the Component Version Register returns zero, we know that
206 * ADDITIONAL_FEATURES are not enabled. No need to go any further.
207 */
208 if (!reg)
209 return;
210
211 dev_dbg_ratelimited(p->dev, "Designware UART version %c.%c%c\n",
212 (reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff);
213
214 reg = readl(p->membase + DW_UART_CPR);
215 if (!reg)
216 return;
217
218 /* Select the type based on fifo */
219 if (reg & DW_UART_CPR_FIFO_MODE) {
220 p->type = PORT_16550A;
221 p->flags |= UPF_FIXED_TYPE;
222 p->fifosize = DW_UART_CPR_FIFO_SIZE(reg);
223 up->tx_loadsz = p->fifosize;
Heikki Krogerus2920adb2013-04-10 16:58:31 +0300224 up->capabilities = UART_CAP_FIFO;
Heikki Krogerus30046df2013-01-10 11:25:09 +0200225 }
Heikki Krogerus2920adb2013-04-10 16:58:31 +0300226
227 if (reg & DW_UART_CPR_AFCE_MODE)
228 up->capabilities |= UART_CAP_AFE;
Heikki Krogerus30046df2013-01-10 11:25:09 +0200229}
230
Bill Pemberton9671f092012-11-19 13:21:50 -0500231static int dw8250_probe(struct platform_device *pdev)
Jamie Iles7d4008e2011-08-26 19:04:50 +0100232{
Alan Cox2655a2c2012-07-12 12:59:50 +0100233 struct uart_8250_port uart = {};
Jamie Iles7d4008e2011-08-26 19:04:50 +0100234 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
235 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100236 struct dw8250_data *data;
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200237 int err;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100238
239 if (!regs || !irq) {
240 dev_err(&pdev->dev, "no registers/irq defined\n");
241 return -EINVAL;
242 }
243
Alan Cox2655a2c2012-07-12 12:59:50 +0100244 spin_lock_init(&uart.port.lock);
245 uart.port.mapbase = regs->start;
246 uart.port.irq = irq->start;
247 uart.port.handle_irq = dw8250_handle_irq;
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300248 uart.port.pm = dw8250_do_pm;
Alan Cox2655a2c2012-07-12 12:59:50 +0100249 uart.port.type = PORT_8250;
Heikki Krogerusf93366f2013-01-10 11:25:07 +0200250 uart.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
Alan Cox2655a2c2012-07-12 12:59:50 +0100251 uart.port.dev = &pdev->dev;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100252
Heikki Krogerusf93366f2013-01-10 11:25:07 +0200253 uart.port.membase = ioremap(regs->start, resource_size(regs));
254 if (!uart.port.membase)
255 return -ENOMEM;
256
Emilio Lópeze302cd92013-03-29 00:15:49 +0100257 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
258 if (!data)
259 return -ENOMEM;
260
261 data->clk = devm_clk_get(&pdev->dev, NULL);
262 if (!IS_ERR(data->clk)) {
263 clk_prepare_enable(data->clk);
264 uart.port.uartclk = clk_get_rate(data->clk);
265 }
266
Alan Cox2655a2c2012-07-12 12:59:50 +0100267 uart.port.iotype = UPIO_MEM;
268 uart.port.serial_in = dw8250_serial_in;
269 uart.port.serial_out = dw8250_serial_out;
Emilio Lópeze302cd92013-03-29 00:15:49 +0100270 uart.port.private_data = data;
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200271
Heikki Krogerusf5836a52013-01-16 14:08:16 +0200272 dw8250_setup_port(&uart);
273
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200274 if (pdev->dev.of_node) {
275 err = dw8250_probe_of(&uart.port);
276 if (err)
277 return err;
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200278 } else if (ACPI_HANDLE(&pdev->dev)) {
Heikki Krogerus94b2b472013-04-10 16:58:30 +0300279 err = dw8250_probe_acpi(&uart);
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200280 if (err)
281 return err;
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200282 } else {
283 return -ENODEV;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100284 }
285
Alan Cox2655a2c2012-07-12 12:59:50 +0100286 data->line = serial8250_register_8250_port(&uart);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100287 if (data->line < 0)
288 return data->line;
289
290 platform_set_drvdata(pdev, data);
291
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300292 pm_runtime_set_active(&pdev->dev);
293 pm_runtime_enable(&pdev->dev);
294
Jamie Iles7d4008e2011-08-26 19:04:50 +0100295 return 0;
296}
297
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500298static int dw8250_remove(struct platform_device *pdev)
Jamie Iles7d4008e2011-08-26 19:04:50 +0100299{
300 struct dw8250_data *data = platform_get_drvdata(pdev);
301
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300302 pm_runtime_get_sync(&pdev->dev);
303
Jamie Iles7d4008e2011-08-26 19:04:50 +0100304 serial8250_unregister_port(data->line);
305
Emilio Lópeze302cd92013-03-29 00:15:49 +0100306 if (!IS_ERR(data->clk))
307 clk_disable_unprepare(data->clk);
308
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300309 pm_runtime_disable(&pdev->dev);
310 pm_runtime_put_noidle(&pdev->dev);
311
Jamie Iles7d4008e2011-08-26 19:04:50 +0100312 return 0;
313}
314
James Hoganb61c5ed2012-10-15 10:25:58 +0100315#ifdef CONFIG_PM
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300316static int dw8250_suspend(struct device *dev)
James Hoganb61c5ed2012-10-15 10:25:58 +0100317{
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300318 struct dw8250_data *data = dev_get_drvdata(dev);
James Hoganb61c5ed2012-10-15 10:25:58 +0100319
320 serial8250_suspend_port(data->line);
321
322 return 0;
323}
324
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300325static int dw8250_resume(struct device *dev)
James Hoganb61c5ed2012-10-15 10:25:58 +0100326{
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300327 struct dw8250_data *data = dev_get_drvdata(dev);
James Hoganb61c5ed2012-10-15 10:25:58 +0100328
329 serial8250_resume_port(data->line);
330
331 return 0;
332}
James Hoganb61c5ed2012-10-15 10:25:58 +0100333#endif /* CONFIG_PM */
334
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300335#ifdef CONFIG_PM_RUNTIME
336static int dw8250_runtime_suspend(struct device *dev)
337{
338 struct dw8250_data *data = dev_get_drvdata(dev);
339
340 clk_disable_unprepare(data->clk);
341
342 return 0;
343}
344
345static int dw8250_runtime_resume(struct device *dev)
346{
347 struct dw8250_data *data = dev_get_drvdata(dev);
348
349 clk_prepare_enable(data->clk);
350
351 return 0;
352}
353#endif
354
355static const struct dev_pm_ops dw8250_pm_ops = {
356 SET_SYSTEM_SLEEP_PM_OPS(dw8250_suspend, dw8250_resume)
357 SET_RUNTIME_PM_OPS(dw8250_runtime_suspend, dw8250_runtime_resume, NULL)
358};
359
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200360static const struct of_device_id dw8250_of_match[] = {
Jamie Iles7d4008e2011-08-26 19:04:50 +0100361 { .compatible = "snps,dw-apb-uart" },
362 { /* Sentinel */ }
363};
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200364MODULE_DEVICE_TABLE(of, dw8250_of_match);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100365
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200366static const struct acpi_device_id dw8250_acpi_match[] = {
Heikki Krogerusaea02e82013-04-10 16:58:29 +0300367 { "INT33C4", 0 },
368 { "INT33C5", 0 },
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200369 { },
370};
371MODULE_DEVICE_TABLE(acpi, dw8250_acpi_match);
372
Jamie Iles7d4008e2011-08-26 19:04:50 +0100373static struct platform_driver dw8250_platform_driver = {
374 .driver = {
375 .name = "dw-apb-uart",
376 .owner = THIS_MODULE,
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300377 .pm = &dw8250_pm_ops,
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200378 .of_match_table = dw8250_of_match,
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200379 .acpi_match_table = ACPI_PTR(dw8250_acpi_match),
Jamie Iles7d4008e2011-08-26 19:04:50 +0100380 },
381 .probe = dw8250_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500382 .remove = dw8250_remove,
Jamie Iles7d4008e2011-08-26 19:04:50 +0100383};
384
Axel Linc8381c152011-11-28 19:22:15 +0800385module_platform_driver(dw8250_platform_driver);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100386
387MODULE_AUTHOR("Jamie Iles");
388MODULE_LICENSE("GPL");
389MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver");