blob: ceacf5e36f2efe4110bb97c408f0e3ac9273d044 [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>
Jamie Iles7d4008e2011-08-26 19:04:50 +010029
Heikki Krogerus7277b2a2013-01-10 11:25:12 +020030#include "8250.h"
31
Heikki Krogerus30046df2013-01-10 11:25:09 +020032/* Offsets for the DesignWare specific registers */
33#define DW_UART_USR 0x1f /* UART Status Register */
34#define DW_UART_CPR 0xf4 /* Component Parameter Register */
35#define DW_UART_UCV 0xf8 /* UART Component Version */
36
Heikki Krogerus6a7320c2013-01-10 11:25:10 +020037/* Intel Low Power Subsystem specific */
38#define LPSS_PRV_CLOCK_PARAMS 0x800
39
Heikki Krogerus30046df2013-01-10 11:25:09 +020040/* Component Parameter Register bits */
41#define DW_UART_CPR_ABP_DATA_WIDTH (3 << 0)
42#define DW_UART_CPR_AFCE_MODE (1 << 4)
43#define DW_UART_CPR_THRE_MODE (1 << 5)
44#define DW_UART_CPR_SIR_MODE (1 << 6)
45#define DW_UART_CPR_SIR_LP_MODE (1 << 7)
46#define DW_UART_CPR_ADDITIONAL_FEATURES (1 << 8)
47#define DW_UART_CPR_FIFO_ACCESS (1 << 9)
48#define DW_UART_CPR_FIFO_STAT (1 << 10)
49#define DW_UART_CPR_SHADOW (1 << 11)
50#define DW_UART_CPR_ENCODED_PARMS (1 << 12)
51#define DW_UART_CPR_DMA_EXTRA (1 << 13)
52#define DW_UART_CPR_FIFO_MODE (0xff << 16)
53/* Helper for fifo size calculation */
54#define DW_UART_CPR_FIFO_SIZE(a) (((a >> 16) & 0xff) * 16)
55
56
Jamie Iles7d4008e2011-08-26 19:04:50 +010057struct dw8250_data {
58 int last_lcr;
59 int line;
60};
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);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100108 p->serial_out(p, d->last_lcr, UART_LCR);
109
110 return 1;
111 }
112
113 return 0;
114}
115
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200116static int dw8250_probe_of(struct uart_port *p)
117{
118 struct device_node *np = p->dev->of_node;
119 u32 val;
120
121 if (!of_property_read_u32(np, "reg-io-width", &val)) {
122 switch (val) {
123 case 1:
124 break;
125 case 4:
126 p->iotype = UPIO_MEM32;
127 p->serial_in = dw8250_serial_in32;
128 p->serial_out = dw8250_serial_out32;
129 break;
130 default:
131 dev_err(p->dev, "unsupported reg-io-width (%u)\n", val);
132 return -EINVAL;
133 }
134 }
135
136 if (!of_property_read_u32(np, "reg-shift", &val))
137 p->regshift = val;
138
139 if (of_property_read_u32(np, "clock-frequency", &val)) {
140 dev_err(p->dev, "no clock-frequency property set\n");
141 return -EINVAL;
142 }
143 p->uartclk = val;
144
145 return 0;
146}
147
Heikki Krogerus7277b2a2013-01-10 11:25:12 +0200148static bool dw8250_acpi_dma_filter(struct dma_chan *chan, void *parm)
149{
150 return chan->chan_id == *(int *)parm;
151}
152
153static acpi_status
154dw8250_acpi_walk_resource(struct acpi_resource *res, void *data)
155{
156 struct uart_port *p = data;
157 struct uart_8250_port *port;
158 struct uart_8250_dma *dma;
159 struct acpi_resource_fixed_dma *fixed_dma;
160 struct dma_slave_config *slave;
161
162 port = container_of(p, struct uart_8250_port, port);
163
164 switch (res->type) {
165 case ACPI_RESOURCE_TYPE_FIXED_DMA:
166 fixed_dma = &res->data.fixed_dma;
167
168 /* TX comes first */
169 if (!port->dma) {
170 dma = devm_kzalloc(p->dev, sizeof(*dma), GFP_KERNEL);
171 if (!dma)
172 return AE_NO_MEMORY;
173
174 port->dma = dma;
175 slave = &dma->txconf;
176
177 slave->direction = DMA_MEM_TO_DEV;
178 slave->dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
179 slave->slave_id = fixed_dma->request_lines;
180
181 dma->tx_chan_id = fixed_dma->channels;
182 dma->tx_param = &dma->tx_chan_id;
183 dma->fn = dw8250_acpi_dma_filter;
184 } else {
185 dma = port->dma;
186 slave = &dma->rxconf;
187
188 slave->direction = DMA_DEV_TO_MEM;
189 slave->src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
190 slave->slave_id = fixed_dma->request_lines;
191
192 dma->rx_chan_id = fixed_dma->channels;
193 dma->rx_param = &dma->rx_chan_id;
194 }
195
196 break;
197 }
198
199 return AE_OK;
200}
201
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200202static int dw8250_probe_acpi(struct uart_port *p)
203{
204 const struct acpi_device_id *id;
Heikki Krogerus7277b2a2013-01-10 11:25:12 +0200205 acpi_status status;
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200206 u32 reg;
207
208 id = acpi_match_device(p->dev->driver->acpi_match_table, p->dev);
209 if (!id)
210 return -ENODEV;
211
212 p->iotype = UPIO_MEM32;
213 p->serial_in = dw8250_serial_in32;
214 p->serial_out = dw8250_serial_out32;
215 p->regshift = 2;
216 p->uartclk = (unsigned int)id->driver_data;
217
Heikki Krogerus7277b2a2013-01-10 11:25:12 +0200218 status = acpi_walk_resources(ACPI_HANDLE(p->dev), METHOD_NAME__CRS,
219 dw8250_acpi_walk_resource, p);
220 if (ACPI_FAILURE(status)) {
221 dev_err_ratelimited(p->dev, "%s failed \"%s\"\n", __func__,
222 acpi_format_exception(status));
223 return -ENODEV;
224 }
225
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200226 /* Fix Haswell issue where the clocks do not get enabled */
227 if (!strcmp(id->id, "INT33C4") || !strcmp(id->id, "INT33C5")) {
228 reg = readl(p->membase + LPSS_PRV_CLOCK_PARAMS);
229 writel(reg | 1, p->membase + LPSS_PRV_CLOCK_PARAMS);
230 }
231
232 return 0;
233}
234
Heikki Krogerus30046df2013-01-10 11:25:09 +0200235static void dw8250_setup_port(struct uart_8250_port *up)
236{
237 struct uart_port *p = &up->port;
238 u32 reg = readl(p->membase + DW_UART_UCV);
239
240 /*
241 * If the Component Version Register returns zero, we know that
242 * ADDITIONAL_FEATURES are not enabled. No need to go any further.
243 */
244 if (!reg)
245 return;
246
247 dev_dbg_ratelimited(p->dev, "Designware UART version %c.%c%c\n",
248 (reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff);
249
250 reg = readl(p->membase + DW_UART_CPR);
251 if (!reg)
252 return;
253
254 /* Select the type based on fifo */
255 if (reg & DW_UART_CPR_FIFO_MODE) {
256 p->type = PORT_16550A;
257 p->flags |= UPF_FIXED_TYPE;
258 p->fifosize = DW_UART_CPR_FIFO_SIZE(reg);
259 up->tx_loadsz = p->fifosize;
260 }
261}
262
Bill Pemberton9671f092012-11-19 13:21:50 -0500263static int dw8250_probe(struct platform_device *pdev)
Jamie Iles7d4008e2011-08-26 19:04:50 +0100264{
Alan Cox2655a2c2012-07-12 12:59:50 +0100265 struct uart_8250_port uart = {};
Jamie Iles7d4008e2011-08-26 19:04:50 +0100266 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
267 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100268 struct dw8250_data *data;
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200269 int err;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100270
271 if (!regs || !irq) {
272 dev_err(&pdev->dev, "no registers/irq defined\n");
273 return -EINVAL;
274 }
275
Alan Cox2655a2c2012-07-12 12:59:50 +0100276 spin_lock_init(&uart.port.lock);
277 uart.port.mapbase = regs->start;
278 uart.port.irq = irq->start;
279 uart.port.handle_irq = dw8250_handle_irq;
280 uart.port.type = PORT_8250;
Heikki Krogerusf93366f2013-01-10 11:25:07 +0200281 uart.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
Alan Cox2655a2c2012-07-12 12:59:50 +0100282 uart.port.dev = &pdev->dev;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100283
Heikki Krogerusf93366f2013-01-10 11:25:07 +0200284 uart.port.membase = ioremap(regs->start, resource_size(regs));
285 if (!uart.port.membase)
286 return -ENOMEM;
287
Alan Cox2655a2c2012-07-12 12:59:50 +0100288 uart.port.iotype = UPIO_MEM;
289 uart.port.serial_in = dw8250_serial_in;
290 uart.port.serial_out = dw8250_serial_out;
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200291
292 if (pdev->dev.of_node) {
293 err = dw8250_probe_of(&uart.port);
294 if (err)
295 return err;
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200296 } else if (ACPI_HANDLE(&pdev->dev)) {
297 err = dw8250_probe_acpi(&uart.port);
298 if (err)
299 return err;
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200300 } else {
301 return -ENODEV;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100302 }
303
Heikki Krogerus30046df2013-01-10 11:25:09 +0200304 dw8250_setup_port(&uart);
305
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200306 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
307 if (!data)
308 return -ENOMEM;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100309
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200310 uart.port.private_data = data;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100311
Alan Cox2655a2c2012-07-12 12:59:50 +0100312 data->line = serial8250_register_8250_port(&uart);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100313 if (data->line < 0)
314 return data->line;
315
316 platform_set_drvdata(pdev, data);
317
318 return 0;
319}
320
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500321static int dw8250_remove(struct platform_device *pdev)
Jamie Iles7d4008e2011-08-26 19:04:50 +0100322{
323 struct dw8250_data *data = platform_get_drvdata(pdev);
324
325 serial8250_unregister_port(data->line);
326
327 return 0;
328}
329
James Hoganb61c5ed2012-10-15 10:25:58 +0100330#ifdef CONFIG_PM
331static int dw8250_suspend(struct platform_device *pdev, pm_message_t state)
332{
333 struct dw8250_data *data = platform_get_drvdata(pdev);
334
335 serial8250_suspend_port(data->line);
336
337 return 0;
338}
339
340static int dw8250_resume(struct platform_device *pdev)
341{
342 struct dw8250_data *data = platform_get_drvdata(pdev);
343
344 serial8250_resume_port(data->line);
345
346 return 0;
347}
348#else
349#define dw8250_suspend NULL
350#define dw8250_resume NULL
351#endif /* CONFIG_PM */
352
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200353static const struct of_device_id dw8250_of_match[] = {
Jamie Iles7d4008e2011-08-26 19:04:50 +0100354 { .compatible = "snps,dw-apb-uart" },
355 { /* Sentinel */ }
356};
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200357MODULE_DEVICE_TABLE(of, dw8250_of_match);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100358
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200359static const struct acpi_device_id dw8250_acpi_match[] = {
360 { "INT33C4", 100000000 },
361 { "INT33C5", 100000000 },
362 { },
363};
364MODULE_DEVICE_TABLE(acpi, dw8250_acpi_match);
365
Jamie Iles7d4008e2011-08-26 19:04:50 +0100366static struct platform_driver dw8250_platform_driver = {
367 .driver = {
368 .name = "dw-apb-uart",
369 .owner = THIS_MODULE,
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200370 .of_match_table = dw8250_of_match,
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200371 .acpi_match_table = ACPI_PTR(dw8250_acpi_match),
Jamie Iles7d4008e2011-08-26 19:04:50 +0100372 },
373 .probe = dw8250_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500374 .remove = dw8250_remove,
James Hoganb61c5ed2012-10-15 10:25:58 +0100375 .suspend = dw8250_suspend,
376 .resume = dw8250_resume,
Jamie Iles7d4008e2011-08-26 19:04:50 +0100377};
378
Axel Linc8381c152011-11-28 19:22:15 +0800379module_platform_driver(dw8250_platform_driver);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100380
381MODULE_AUTHOR("Jamie Iles");
382MODULE_LICENSE("GPL");
383MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver");