blob: bfdaf8b2680bd2d157a1bdf3d86ada71865718f0 [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 Krogerus053fac32013-01-16 14:08:15 +0200148#ifdef CONFIG_ACPI
Heikki Krogerus7277b2a2013-01-10 11:25:12 +0200149static bool dw8250_acpi_dma_filter(struct dma_chan *chan, void *parm)
150{
151 return chan->chan_id == *(int *)parm;
152}
153
154static acpi_status
155dw8250_acpi_walk_resource(struct acpi_resource *res, void *data)
156{
157 struct uart_port *p = data;
158 struct uart_8250_port *port;
159 struct uart_8250_dma *dma;
160 struct acpi_resource_fixed_dma *fixed_dma;
161 struct dma_slave_config *slave;
162
163 port = container_of(p, struct uart_8250_port, port);
164
165 switch (res->type) {
166 case ACPI_RESOURCE_TYPE_FIXED_DMA:
167 fixed_dma = &res->data.fixed_dma;
168
169 /* TX comes first */
170 if (!port->dma) {
171 dma = devm_kzalloc(p->dev, sizeof(*dma), GFP_KERNEL);
172 if (!dma)
173 return AE_NO_MEMORY;
174
175 port->dma = dma;
176 slave = &dma->txconf;
177
178 slave->direction = DMA_MEM_TO_DEV;
179 slave->dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
180 slave->slave_id = fixed_dma->request_lines;
181
182 dma->tx_chan_id = fixed_dma->channels;
183 dma->tx_param = &dma->tx_chan_id;
184 dma->fn = dw8250_acpi_dma_filter;
185 } else {
186 dma = port->dma;
187 slave = &dma->rxconf;
188
189 slave->direction = DMA_DEV_TO_MEM;
190 slave->src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
191 slave->slave_id = fixed_dma->request_lines;
192
193 dma->rx_chan_id = fixed_dma->channels;
194 dma->rx_param = &dma->rx_chan_id;
195 }
196
197 break;
198 }
199
200 return AE_OK;
201}
202
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200203static int dw8250_probe_acpi(struct uart_port *p)
204{
205 const struct acpi_device_id *id;
Heikki Krogerus7277b2a2013-01-10 11:25:12 +0200206 acpi_status status;
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200207 u32 reg;
208
209 id = acpi_match_device(p->dev->driver->acpi_match_table, p->dev);
210 if (!id)
211 return -ENODEV;
212
213 p->iotype = UPIO_MEM32;
214 p->serial_in = dw8250_serial_in32;
215 p->serial_out = dw8250_serial_out32;
216 p->regshift = 2;
217 p->uartclk = (unsigned int)id->driver_data;
218
Heikki Krogerus7277b2a2013-01-10 11:25:12 +0200219 status = acpi_walk_resources(ACPI_HANDLE(p->dev), METHOD_NAME__CRS,
220 dw8250_acpi_walk_resource, p);
221 if (ACPI_FAILURE(status)) {
222 dev_err_ratelimited(p->dev, "%s failed \"%s\"\n", __func__,
223 acpi_format_exception(status));
224 return -ENODEV;
225 }
226
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200227 /* Fix Haswell issue where the clocks do not get enabled */
228 if (!strcmp(id->id, "INT33C4") || !strcmp(id->id, "INT33C5")) {
229 reg = readl(p->membase + LPSS_PRV_CLOCK_PARAMS);
230 writel(reg | 1, p->membase + LPSS_PRV_CLOCK_PARAMS);
231 }
232
233 return 0;
234}
Heikki Krogerus053fac32013-01-16 14:08:15 +0200235#else
236static inline int dw8250_probe_acpi(struct uart_port *p)
237{
238 return -ENODEV;
239}
240#endif /* CONFIG_ACPI */
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200241
Heikki Krogerus30046df2013-01-10 11:25:09 +0200242static void dw8250_setup_port(struct uart_8250_port *up)
243{
244 struct uart_port *p = &up->port;
245 u32 reg = readl(p->membase + DW_UART_UCV);
246
247 /*
248 * If the Component Version Register returns zero, we know that
249 * ADDITIONAL_FEATURES are not enabled. No need to go any further.
250 */
251 if (!reg)
252 return;
253
254 dev_dbg_ratelimited(p->dev, "Designware UART version %c.%c%c\n",
255 (reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff);
256
257 reg = readl(p->membase + DW_UART_CPR);
258 if (!reg)
259 return;
260
261 /* Select the type based on fifo */
262 if (reg & DW_UART_CPR_FIFO_MODE) {
263 p->type = PORT_16550A;
264 p->flags |= UPF_FIXED_TYPE;
265 p->fifosize = DW_UART_CPR_FIFO_SIZE(reg);
266 up->tx_loadsz = p->fifosize;
267 }
268}
269
Bill Pemberton9671f092012-11-19 13:21:50 -0500270static int dw8250_probe(struct platform_device *pdev)
Jamie Iles7d4008e2011-08-26 19:04:50 +0100271{
Alan Cox2655a2c2012-07-12 12:59:50 +0100272 struct uart_8250_port uart = {};
Jamie Iles7d4008e2011-08-26 19:04:50 +0100273 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
274 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100275 struct dw8250_data *data;
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200276 int err;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100277
278 if (!regs || !irq) {
279 dev_err(&pdev->dev, "no registers/irq defined\n");
280 return -EINVAL;
281 }
282
Alan Cox2655a2c2012-07-12 12:59:50 +0100283 spin_lock_init(&uart.port.lock);
284 uart.port.mapbase = regs->start;
285 uart.port.irq = irq->start;
286 uart.port.handle_irq = dw8250_handle_irq;
287 uart.port.type = PORT_8250;
Heikki Krogerusf93366f2013-01-10 11:25:07 +0200288 uart.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
Alan Cox2655a2c2012-07-12 12:59:50 +0100289 uart.port.dev = &pdev->dev;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100290
Heikki Krogerusf93366f2013-01-10 11:25:07 +0200291 uart.port.membase = ioremap(regs->start, resource_size(regs));
292 if (!uart.port.membase)
293 return -ENOMEM;
294
Alan Cox2655a2c2012-07-12 12:59:50 +0100295 uart.port.iotype = UPIO_MEM;
296 uart.port.serial_in = dw8250_serial_in;
297 uart.port.serial_out = dw8250_serial_out;
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200298
299 if (pdev->dev.of_node) {
300 err = dw8250_probe_of(&uart.port);
301 if (err)
302 return err;
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200303 } else if (ACPI_HANDLE(&pdev->dev)) {
304 err = dw8250_probe_acpi(&uart.port);
305 if (err)
306 return err;
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200307 } else {
308 return -ENODEV;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100309 }
310
Heikki Krogerus30046df2013-01-10 11:25:09 +0200311 dw8250_setup_port(&uart);
312
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200313 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
314 if (!data)
315 return -ENOMEM;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100316
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200317 uart.port.private_data = data;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100318
Alan Cox2655a2c2012-07-12 12:59:50 +0100319 data->line = serial8250_register_8250_port(&uart);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100320 if (data->line < 0)
321 return data->line;
322
323 platform_set_drvdata(pdev, data);
324
325 return 0;
326}
327
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500328static int dw8250_remove(struct platform_device *pdev)
Jamie Iles7d4008e2011-08-26 19:04:50 +0100329{
330 struct dw8250_data *data = platform_get_drvdata(pdev);
331
332 serial8250_unregister_port(data->line);
333
334 return 0;
335}
336
James Hoganb61c5ed2012-10-15 10:25:58 +0100337#ifdef CONFIG_PM
338static int dw8250_suspend(struct platform_device *pdev, pm_message_t state)
339{
340 struct dw8250_data *data = platform_get_drvdata(pdev);
341
342 serial8250_suspend_port(data->line);
343
344 return 0;
345}
346
347static int dw8250_resume(struct platform_device *pdev)
348{
349 struct dw8250_data *data = platform_get_drvdata(pdev);
350
351 serial8250_resume_port(data->line);
352
353 return 0;
354}
355#else
356#define dw8250_suspend NULL
357#define dw8250_resume NULL
358#endif /* CONFIG_PM */
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[] = {
367 { "INT33C4", 100000000 },
368 { "INT33C5", 100000000 },
369 { },
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 Krogerusa7260c82013-01-10 11:25:08 +0200377 .of_match_table = dw8250_of_match,
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200378 .acpi_match_table = ACPI_PTR(dw8250_acpi_match),
Jamie Iles7d4008e2011-08-26 19:04:50 +0100379 },
380 .probe = dw8250_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500381 .remove = dw8250_remove,
James Hoganb61c5ed2012-10-15 10:25:58 +0100382 .suspend = dw8250_suspend,
383 .resume = dw8250_resume,
Jamie Iles7d4008e2011-08-26 19:04:50 +0100384};
385
Axel Linc8381c152011-11-28 19:22:15 +0800386module_platform_driver(dw8250_platform_driver);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100387
388MODULE_AUTHOR("Jamie Iles");
389MODULE_LICENSE("GPL");
390MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver");