blob: d190d69532819e220b21cb05c59fd2d9ca9d41ad [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>
Jamie Iles7d4008e2011-08-26 19:04:50 +010017#include <linux/io.h>
18#include <linux/module.h>
19#include <linux/serial_8250.h>
Jamie Iles7d4008e2011-08-26 19:04:50 +010020#include <linux/serial_reg.h>
21#include <linux/of.h>
22#include <linux/of_irq.h>
23#include <linux/of_platform.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
Heikki Krogerus6a7320c2013-01-10 11:25:10 +020026#include <linux/acpi.h>
Emilio Lópeze302cd92013-03-29 00:15:49 +010027#include <linux/clk.h>
Chen-Yu Tsai7fe090b2014-07-23 23:33:06 +080028#include <linux/reset.h>
Heikki Krogerusffc3ae62013-04-10 16:58:28 +030029#include <linux/pm_runtime.h>
Jamie Iles7d4008e2011-08-26 19:04:50 +010030
David Daneyd5f1af72013-06-19 20:37:27 +000031#include <asm/byteorder.h>
32
Heikki Krogerus7277b2a2013-01-10 11:25:12 +020033#include "8250.h"
34
Heikki Krogerus30046df2013-01-10 11:25:09 +020035/* Offsets for the DesignWare specific registers */
36#define DW_UART_USR 0x1f /* UART Status Register */
37#define DW_UART_CPR 0xf4 /* Component Parameter Register */
38#define DW_UART_UCV 0xf8 /* UART Component Version */
39
40/* 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 {
Heikki Krogerusfe95855532013-09-05 17:34:53 +030058 u8 usr_reg;
Heikki Krogerusfe95855532013-09-05 17:34:53 +030059 int line;
Desmond Liudfd37662015-02-26 16:35:57 -080060 int msr_mask_on;
61 int msr_mask_off;
Heikki Krogerusfe95855532013-09-05 17:34:53 +030062 struct clk *clk;
Heiko Stübner7d78cbe2014-06-16 15:25:17 +020063 struct clk *pclk;
Chen-Yu Tsai7fe090b2014-07-23 23:33:06 +080064 struct reset_control *rst;
Heikki Krogerusfe95855532013-09-05 17:34:53 +030065 struct uart_8250_dma dma;
Heikki Krogerus4f042052015-09-21 14:17:27 +030066
67 unsigned int skip_autocfg:1;
Heikki Krogerusc73942e2015-09-21 14:17:29 +030068 unsigned int uart_16550_compatible:1;
Jamie Iles7d4008e2011-08-26 19:04:50 +010069};
70
Loic Poulainc439c332014-04-24 11:46:14 +020071#define BYT_PRV_CLK 0x800
72#define BYT_PRV_CLK_EN (1 << 0)
73#define BYT_PRV_CLK_M_VAL_SHIFT 1
74#define BYT_PRV_CLK_N_VAL_SHIFT 16
75#define BYT_PRV_CLK_UPDATE (1 << 31)
76
Tim Kryger33acbb82013-08-16 13:50:15 -070077static inline int dw8250_modify_msr(struct uart_port *p, int offset, int value)
78{
79 struct dw8250_data *d = p->private_data;
80
Desmond Liudfd37662015-02-26 16:35:57 -080081 /* Override any modem control signals if needed */
82 if (offset == UART_MSR) {
83 value |= d->msr_mask_on;
84 value &= ~d->msr_mask_off;
85 }
86
Tim Kryger33acbb82013-08-16 13:50:15 -070087 return value;
88}
89
Tim Krygerc49436b2013-10-01 10:18:08 -070090static void dw8250_force_idle(struct uart_port *p)
91{
Andy Shevchenkob1261c82014-07-14 14:26:14 +030092 struct uart_8250_port *up = up_to_u8250p(p);
93
94 serial8250_clear_and_reinit_fifos(up);
Tim Krygerc49436b2013-10-01 10:18:08 -070095 (void)p->serial_in(p, UART_RX);
96}
97
Noam Camuscdcea052015-12-12 19:18:25 +020098static void dw8250_check_lcr(struct uart_port *p, int value)
Jamie Iles7d4008e2011-08-26 19:04:50 +010099{
Noam Camuscdcea052015-12-12 19:18:25 +0200100 void __iomem *offset = p->membase + (UART_LCR << p->regshift);
101 int tries = 1000;
Tim Krygerc49436b2013-10-01 10:18:08 -0700102
103 /* Make sure LCR write wasn't ignored */
Noam Camuscdcea052015-12-12 19:18:25 +0200104 while (tries--) {
105 unsigned int lcr = p->serial_in(p, UART_LCR);
106
107 if ((value & ~UART_LCR_SPAR) == (lcr & ~UART_LCR_SPAR))
108 return;
109
110 dw8250_force_idle(p);
111
112#ifdef CONFIG_64BIT
113 __raw_writeq(value & 0xff, offset);
114#else
115 if (p->iotype == UPIO_MEM32)
116 writel(value, offset);
Noam Camus5a431402015-12-12 19:18:27 +0200117 else if (p->iotype == UPIO_MEM32BE)
118 iowrite32be(value, offset);
Noam Camuscdcea052015-12-12 19:18:25 +0200119 else
120 writeb(value, offset);
121#endif
Tim Krygerc49436b2013-10-01 10:18:08 -0700122 }
Noam Camuscdcea052015-12-12 19:18:25 +0200123 /*
124 * FIXME: this deadlocks if port->lock is already held
125 * dev_err(p->dev, "Couldn't set LCR to %d\n", value);
126 */
127}
128
129static void dw8250_serial_out(struct uart_port *p, int offset, int value)
130{
131 struct dw8250_data *d = p->private_data;
132
133 writeb(value, p->membase + (offset << p->regshift));
134
135 if (offset == UART_LCR && !d->uart_16550_compatible)
136 dw8250_check_lcr(p, value);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100137}
138
139static unsigned int dw8250_serial_in(struct uart_port *p, int offset)
140{
Tim Kryger33acbb82013-08-16 13:50:15 -0700141 unsigned int value = readb(p->membase + (offset << p->regshift));
Jamie Iles7d4008e2011-08-26 19:04:50 +0100142
Tim Kryger33acbb82013-08-16 13:50:15 -0700143 return dw8250_modify_msr(p, offset, value);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100144}
145
David Daneybca20922014-11-14 17:26:19 +0300146#ifdef CONFIG_64BIT
147static unsigned int dw8250_serial_inq(struct uart_port *p, int offset)
David Daneyd5f1af72013-06-19 20:37:27 +0000148{
David Daneybca20922014-11-14 17:26:19 +0300149 unsigned int value;
150
151 value = (u8)__raw_readq(p->membase + (offset << p->regshift));
152
153 return dw8250_modify_msr(p, offset, value);
David Daneyd5f1af72013-06-19 20:37:27 +0000154}
155
David Daneybca20922014-11-14 17:26:19 +0300156static void dw8250_serial_outq(struct uart_port *p, int offset, int value)
157{
Noam Camuscdcea052015-12-12 19:18:25 +0200158 struct dw8250_data *d = p->private_data;
159
David Daneybca20922014-11-14 17:26:19 +0300160 value &= 0xff;
161 __raw_writeq(value, p->membase + (offset << p->regshift));
162 /* Read back to ensure register write ordering. */
163 __raw_readq(p->membase + (UART_LCR << p->regshift));
164
Noam Camuscdcea052015-12-12 19:18:25 +0200165 if (offset == UART_LCR && !d->uart_16550_compatible)
166 dw8250_check_lcr(p, value);
David Daneybca20922014-11-14 17:26:19 +0300167}
168#endif /* CONFIG_64BIT */
169
Jamie Iles7d4008e2011-08-26 19:04:50 +0100170static void dw8250_serial_out32(struct uart_port *p, int offset, int value)
171{
Noam Camuscdcea052015-12-12 19:18:25 +0200172 struct dw8250_data *d = p->private_data;
173
Tim Kryger33acbb82013-08-16 13:50:15 -0700174 writel(value, p->membase + (offset << p->regshift));
Tim Krygerc49436b2013-10-01 10:18:08 -0700175
Noam Camuscdcea052015-12-12 19:18:25 +0200176 if (offset == UART_LCR && !d->uart_16550_compatible)
177 dw8250_check_lcr(p, value);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100178}
179
180static unsigned int dw8250_serial_in32(struct uart_port *p, int offset)
181{
Tim Kryger33acbb82013-08-16 13:50:15 -0700182 unsigned int value = readl(p->membase + (offset << p->regshift));
Jamie Iles7d4008e2011-08-26 19:04:50 +0100183
Tim Kryger33acbb82013-08-16 13:50:15 -0700184 return dw8250_modify_msr(p, offset, value);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100185}
186
Noam Camus46250902015-12-12 19:18:26 +0200187static void dw8250_serial_out32be(struct uart_port *p, int offset, int value)
188{
189 struct dw8250_data *d = p->private_data;
190
191 iowrite32be(value, p->membase + (offset << p->regshift));
192
193 if (offset == UART_LCR && !d->uart_16550_compatible)
194 dw8250_check_lcr(p, value);
195}
196
197static unsigned int dw8250_serial_in32be(struct uart_port *p, int offset)
198{
199 unsigned int value = ioread32be(p->membase + (offset << p->regshift));
200
201 return dw8250_modify_msr(p, offset, value);
202}
203
204
Jamie Iles7d4008e2011-08-26 19:04:50 +0100205static int dw8250_handle_irq(struct uart_port *p)
206{
207 struct dw8250_data *d = p->private_data;
208 unsigned int iir = p->serial_in(p, UART_IIR);
209
Andy Shevchenko34eefb592016-02-15 17:38:45 +0200210 if (serial8250_handle_irq(p, iir))
Jamie Iles7d4008e2011-08-26 19:04:50 +0100211 return 1;
Andy Shevchenko34eefb592016-02-15 17:38:45 +0200212
213 if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
Tim Krygerc49436b2013-10-01 10:18:08 -0700214 /* Clear the USR */
David Daneyd5f1af72013-06-19 20:37:27 +0000215 (void)p->serial_in(p, d->usr_reg);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100216
217 return 1;
218 }
219
220 return 0;
221}
222
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300223static void
224dw8250_do_pm(struct uart_port *port, unsigned int state, unsigned int old)
225{
226 if (!state)
227 pm_runtime_get_sync(port->dev);
228
229 serial8250_do_pm(port, state, old);
230
231 if (state)
232 pm_runtime_put_sync_suspend(port->dev);
233}
234
Heikki Krogerus4e26b132014-06-05 16:51:40 +0300235static void dw8250_set_termios(struct uart_port *p, struct ktermios *termios,
236 struct ktermios *old)
237{
238 unsigned int baud = tty_termios_baud_rate(termios);
239 struct dw8250_data *d = p->private_data;
240 unsigned int rate;
241 int ret;
242
243 if (IS_ERR(d->clk) || !old)
244 goto out;
245
Heikki Krogerus4e26b132014-06-05 16:51:40 +0300246 clk_disable_unprepare(d->clk);
247 rate = clk_round_rate(d->clk, baud * 16);
248 ret = clk_set_rate(d->clk, rate);
249 clk_prepare_enable(d->clk);
250
251 if (!ret)
252 p->uartclk = rate;
Qipeng Zha0a6c3012015-07-29 18:23:32 +0800253
254 p->status &= ~UPSTAT_AUTOCTS;
255 if (termios->c_cflag & CRTSCTS)
256 p->status |= UPSTAT_AUTOCTS;
257
Heikki Krogerus4e26b132014-06-05 16:51:40 +0300258out:
259 serial8250_do_set_termios(p, termios, old);
260}
261
Heikki Krogerus1edb3cf2015-09-21 14:17:30 +0300262/*
263 * dw8250_fallback_dma_filter will prevent the UART from getting just any free
264 * channel on platforms that have DMA engines, but don't have any channels
265 * assigned to the UART.
266 *
267 * REVISIT: This is a work around for limitation in the DMA Engine API. Once the
268 * core problem is fixed, this function is no longer needed.
269 */
270static bool dw8250_fallback_dma_filter(struct dma_chan *chan, void *param)
Heikki Krogerus7fb8c562013-09-05 17:34:54 +0300271{
Andy Shevchenko9a1870c2014-08-19 20:29:22 +0300272 return false;
Heikki Krogerus7fb8c562013-09-05 17:34:54 +0300273}
274
Heikki Krogerus0788c392015-05-26 15:59:32 +0300275static bool dw8250_idma_filter(struct dma_chan *chan, void *param)
276{
Heikki Krogerus83ce95e2015-09-21 14:17:31 +0300277 return param == chan->device->dev->parent;
Heikki Krogerus0788c392015-05-26 15:59:32 +0300278}
279
Heikki Krogerus9e08fa52015-09-21 14:17:28 +0300280static void dw8250_quirks(struct uart_port *p, struct dw8250_data *data)
David Daneyd5f1af72013-06-19 20:37:27 +0000281{
Heikki Krogerus9e08fa52015-09-21 14:17:28 +0300282 if (p->dev->of_node) {
283 struct device_node *np = p->dev->of_node;
284 int id;
David Daneyd5f1af72013-06-19 20:37:27 +0000285
Heikki Krogerus9e08fa52015-09-21 14:17:28 +0300286 /* get index of serial line, if found in DT aliases */
287 id = of_alias_get_id(np, "serial");
288 if (id >= 0)
289 p->line = id;
290#ifdef CONFIG_64BIT
291 if (of_device_is_compatible(np, "cavium,octeon-3860-uart")) {
292 p->serial_in = dw8250_serial_inq;
293 p->serial_out = dw8250_serial_outq;
294 p->flags = UPF_SKIP_TEST | UPF_SHARE_IRQ | UPF_FIXED_TYPE;
295 p->type = PORT_OCTEON;
296 data->usr_reg = 0x27;
297 data->skip_autocfg = true;
298 }
299#endif
Noam Camus46250902015-12-12 19:18:26 +0200300 if (of_device_is_big_endian(p->dev->of_node)) {
301 p->iotype = UPIO_MEM32BE;
302 p->serial_in = dw8250_serial_in32be;
303 p->serial_out = dw8250_serial_out32be;
304 }
Heikki Krogerus9e08fa52015-09-21 14:17:28 +0300305 } else if (has_acpi_companion(p->dev)) {
306 p->iotype = UPIO_MEM32;
307 p->regshift = 2;
308 p->serial_in = dw8250_serial_in32;
Heikki Krogerus9e08fa52015-09-21 14:17:28 +0300309 p->set_termios = dw8250_set_termios;
Heikki Krogerusc73942e2015-09-21 14:17:29 +0300310 /* So far none of there implement the Busy Functionality */
311 data->uart_16550_compatible = true;
Heikki Krogerus0788c392015-05-26 15:59:32 +0300312 }
David Daneyd5f1af72013-06-19 20:37:27 +0000313
Heikki Krogerus9e08fa52015-09-21 14:17:28 +0300314 /* Platforms with iDMA */
315 if (platform_get_resource_byname(to_platform_device(p->dev),
316 IORESOURCE_MEM, "lpss_priv")) {
317 p->set_termios = dw8250_set_termios;
318 data->dma.rx_param = p->dev->parent;
319 data->dma.tx_param = p->dev->parent;
320 data->dma.fn = dw8250_idma_filter;
321 }
David Daneyd5f1af72013-06-19 20:37:27 +0000322}
David Daneyd5f1af72013-06-19 20:37:27 +0000323
Heikki Krogerus2338a752015-09-21 14:17:32 +0300324static void dw8250_setup_port(struct uart_port *p)
325{
326 struct uart_8250_port *up = up_to_u8250p(p);
327 u32 reg;
328
329 /*
330 * If the Component Version Register returns zero, we know that
331 * ADDITIONAL_FEATURES are not enabled. No need to go any further.
332 */
Noam Camus5a431402015-12-12 19:18:27 +0200333 if (p->iotype == UPIO_MEM32BE)
334 reg = ioread32be(p->membase + DW_UART_UCV);
335 else
336 reg = readl(p->membase + DW_UART_UCV);
Heikki Krogerus2338a752015-09-21 14:17:32 +0300337 if (!reg)
338 return;
339
340 dev_dbg(p->dev, "Designware UART version %c.%c%c\n",
341 (reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff);
342
Noam Camus5a431402015-12-12 19:18:27 +0200343 if (p->iotype == UPIO_MEM32BE)
344 reg = ioread32be(p->membase + DW_UART_CPR);
345 else
346 reg = readl(p->membase + DW_UART_CPR);
Heikki Krogerus2338a752015-09-21 14:17:32 +0300347 if (!reg)
348 return;
349
350 /* Select the type based on fifo */
351 if (reg & DW_UART_CPR_FIFO_MODE) {
352 p->type = PORT_16550A;
353 p->flags |= UPF_FIXED_TYPE;
354 p->fifosize = DW_UART_CPR_FIFO_SIZE(reg);
355 up->capabilities = UART_CAP_FIFO;
356 }
357
358 if (reg & DW_UART_CPR_AFCE_MODE)
359 up->capabilities |= UART_CAP_AFE;
360}
361
Bill Pemberton9671f092012-11-19 13:21:50 -0500362static int dw8250_probe(struct platform_device *pdev)
Jamie Iles7d4008e2011-08-26 19:04:50 +0100363{
Alan Cox2655a2c2012-07-12 12:59:50 +0100364 struct uart_8250_port uart = {};
Jamie Iles7d4008e2011-08-26 19:04:50 +0100365 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Alexey Brodkin833b1f72015-03-03 18:11:14 +0300366 int irq = platform_get_irq(pdev, 0);
Heikki Krogerus78d3da72015-09-21 14:17:24 +0300367 struct uart_port *p = &uart.port;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100368 struct dw8250_data *data;
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200369 int err;
Heikki Krogerus1bd8edb2015-09-21 14:17:25 +0300370 u32 val;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100371
Alexey Brodkin833b1f72015-03-03 18:11:14 +0300372 if (!regs) {
373 dev_err(&pdev->dev, "no registers defined\n");
Jamie Iles7d4008e2011-08-26 19:04:50 +0100374 return -EINVAL;
375 }
376
Alexey Brodkin833b1f72015-03-03 18:11:14 +0300377 if (irq < 0) {
378 if (irq != -EPROBE_DEFER)
379 dev_err(&pdev->dev, "cannot get irq\n");
380 return irq;
381 }
382
Heikki Krogerus78d3da72015-09-21 14:17:24 +0300383 spin_lock_init(&p->lock);
384 p->mapbase = regs->start;
385 p->irq = irq;
386 p->handle_irq = dw8250_handle_irq;
387 p->pm = dw8250_do_pm;
388 p->type = PORT_8250;
Heikki Krogerus7693c792015-09-21 14:17:33 +0300389 p->flags = UPF_SHARE_IRQ | UPF_FIXED_PORT;
Heikki Krogerus78d3da72015-09-21 14:17:24 +0300390 p->dev = &pdev->dev;
391 p->iotype = UPIO_MEM;
392 p->serial_in = dw8250_serial_in;
393 p->serial_out = dw8250_serial_out;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100394
Heikki Krogerus78d3da72015-09-21 14:17:24 +0300395 p->membase = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
396 if (!p->membase)
Heikki Krogerusf93366f2013-01-10 11:25:07 +0200397 return -ENOMEM;
398
Emilio Lópeze302cd92013-03-29 00:15:49 +0100399 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
400 if (!data)
401 return -ENOMEM;
402
Heikki Krogerus1edb3cf2015-09-21 14:17:30 +0300403 data->dma.fn = dw8250_fallback_dma_filter;
David Daneyd5f1af72013-06-19 20:37:27 +0000404 data->usr_reg = DW_UART_USR;
Heikki Krogerus78d3da72015-09-21 14:17:24 +0300405 p->private_data = data;
Heikki Krogerus23f5b3f2015-03-18 12:55:13 +0200406
Heikki Krogerusc73942e2015-09-21 14:17:29 +0300407 data->uart_16550_compatible = device_property_read_bool(p->dev,
408 "snps,uart-16550-compatible");
409
Heikki Krogerus1bd8edb2015-09-21 14:17:25 +0300410 err = device_property_read_u32(p->dev, "reg-shift", &val);
411 if (!err)
412 p->regshift = val;
413
414 err = device_property_read_u32(p->dev, "reg-io-width", &val);
415 if (!err && val == 4) {
416 p->iotype = UPIO_MEM32;
417 p->serial_in = dw8250_serial_in32;
418 p->serial_out = dw8250_serial_out32;
419 }
420
421 if (device_property_read_bool(p->dev, "dcd-override")) {
422 /* Always report DCD as active */
423 data->msr_mask_on |= UART_MSR_DCD;
424 data->msr_mask_off |= UART_MSR_DDCD;
425 }
426
427 if (device_property_read_bool(p->dev, "dsr-override")) {
428 /* Always report DSR as active */
429 data->msr_mask_on |= UART_MSR_DSR;
430 data->msr_mask_off |= UART_MSR_DDSR;
431 }
432
433 if (device_property_read_bool(p->dev, "cts-override")) {
434 /* Always report CTS as active */
435 data->msr_mask_on |= UART_MSR_CTS;
436 data->msr_mask_off |= UART_MSR_DCTS;
437 }
438
439 if (device_property_read_bool(p->dev, "ri-override")) {
440 /* Always report Ring indicator as inactive */
441 data->msr_mask_off |= UART_MSR_RI;
442 data->msr_mask_off |= UART_MSR_TERI;
443 }
444
Heikki Krogerus23f5b3f2015-03-18 12:55:13 +0200445 /* Always ask for fixed clock rate from a property. */
Heikki Krogerus78d3da72015-09-21 14:17:24 +0300446 device_property_read_u32(p->dev, "clock-frequency", &p->uartclk);
Heikki Krogerus23f5b3f2015-03-18 12:55:13 +0200447
448 /* If there is separate baudclk, get the rate from it. */
Heiko Stübner7d78cbe2014-06-16 15:25:17 +0200449 data->clk = devm_clk_get(&pdev->dev, "baudclk");
Chen-Yu Tsaic8ed99d2014-07-23 23:33:07 +0800450 if (IS_ERR(data->clk) && PTR_ERR(data->clk) != -EPROBE_DEFER)
Heiko Stübner7d78cbe2014-06-16 15:25:17 +0200451 data->clk = devm_clk_get(&pdev->dev, NULL);
Chen-Yu Tsaic8ed99d2014-07-23 23:33:07 +0800452 if (IS_ERR(data->clk) && PTR_ERR(data->clk) == -EPROBE_DEFER)
453 return -EPROBE_DEFER;
Heikki Krogerus23f5b3f2015-03-18 12:55:13 +0200454 if (!IS_ERR_OR_NULL(data->clk)) {
Heiko Stübner7d78cbe2014-06-16 15:25:17 +0200455 err = clk_prepare_enable(data->clk);
456 if (err)
457 dev_warn(&pdev->dev, "could not enable optional baudclk: %d\n",
458 err);
459 else
Heikki Krogerus78d3da72015-09-21 14:17:24 +0300460 p->uartclk = clk_get_rate(data->clk);
Heiko Stübner7d78cbe2014-06-16 15:25:17 +0200461 }
462
Heikki Krogerus23f5b3f2015-03-18 12:55:13 +0200463 /* If no clock rate is defined, fail. */
Heikki Krogerus78d3da72015-09-21 14:17:24 +0300464 if (!p->uartclk) {
Heikki Krogerus23f5b3f2015-03-18 12:55:13 +0200465 dev_err(&pdev->dev, "clock rate not defined\n");
466 return -EINVAL;
467 }
468
Heiko Stübner7d78cbe2014-06-16 15:25:17 +0200469 data->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
Chen-Yu Tsaic8ed99d2014-07-23 23:33:07 +0800470 if (IS_ERR(data->clk) && PTR_ERR(data->clk) == -EPROBE_DEFER) {
471 err = -EPROBE_DEFER;
472 goto err_clk;
473 }
Heiko Stübner7d78cbe2014-06-16 15:25:17 +0200474 if (!IS_ERR(data->pclk)) {
475 err = clk_prepare_enable(data->pclk);
476 if (err) {
477 dev_err(&pdev->dev, "could not enable apb_pclk\n");
Chen-Yu Tsaic8ed99d2014-07-23 23:33:07 +0800478 goto err_clk;
Heiko Stübner7d78cbe2014-06-16 15:25:17 +0200479 }
Emilio Lópeze302cd92013-03-29 00:15:49 +0100480 }
481
Chen-Yu Tsai7fe090b2014-07-23 23:33:06 +0800482 data->rst = devm_reset_control_get_optional(&pdev->dev, NULL);
Chen-Yu Tsaic8ed99d2014-07-23 23:33:07 +0800483 if (IS_ERR(data->rst) && PTR_ERR(data->rst) == -EPROBE_DEFER) {
484 err = -EPROBE_DEFER;
485 goto err_pclk;
486 }
Chen-Yu Tsai7fe090b2014-07-23 23:33:06 +0800487 if (!IS_ERR(data->rst))
488 reset_control_deassert(data->rst);
489
Heikki Krogerus9e08fa52015-09-21 14:17:28 +0300490 dw8250_quirks(p, data);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100491
Heikki Krogerusc73942e2015-09-21 14:17:29 +0300492 /* If the Busy Functionality is not implemented, don't handle it */
Noam Camuscdcea052015-12-12 19:18:25 +0200493 if (data->uart_16550_compatible)
Heikki Krogerusc73942e2015-09-21 14:17:29 +0300494 p->handle_irq = NULL;
Heikki Krogerusc73942e2015-09-21 14:17:29 +0300495
Heikki Krogerus4f042052015-09-21 14:17:27 +0300496 if (!data->skip_autocfg)
Heikki Krogerus2338a752015-09-21 14:17:32 +0300497 dw8250_setup_port(p);
Heikki Krogerus4f042052015-09-21 14:17:27 +0300498
Heikki Krogerus25593182015-09-21 14:17:26 +0300499 /* If we have a valid fifosize, try hooking up DMA */
500 if (p->fifosize) {
501 data->dma.rxconf.src_maxburst = p->fifosize / 4;
502 data->dma.txconf.dst_maxburst = p->fifosize / 4;
503 uart.dma = &data->dma;
504 }
505
Alan Cox2655a2c2012-07-12 12:59:50 +0100506 data->line = serial8250_register_8250_port(&uart);
Chen-Yu Tsaic8ed99d2014-07-23 23:33:07 +0800507 if (data->line < 0) {
508 err = data->line;
509 goto err_reset;
510 }
Jamie Iles7d4008e2011-08-26 19:04:50 +0100511
512 platform_set_drvdata(pdev, data);
513
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300514 pm_runtime_set_active(&pdev->dev);
515 pm_runtime_enable(&pdev->dev);
516
Jamie Iles7d4008e2011-08-26 19:04:50 +0100517 return 0;
Chen-Yu Tsaic8ed99d2014-07-23 23:33:07 +0800518
519err_reset:
520 if (!IS_ERR(data->rst))
521 reset_control_assert(data->rst);
522
523err_pclk:
524 if (!IS_ERR(data->pclk))
525 clk_disable_unprepare(data->pclk);
526
527err_clk:
528 if (!IS_ERR(data->clk))
529 clk_disable_unprepare(data->clk);
530
531 return err;
Jamie Iles7d4008e2011-08-26 19:04:50 +0100532}
533
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500534static int dw8250_remove(struct platform_device *pdev)
Jamie Iles7d4008e2011-08-26 19:04:50 +0100535{
536 struct dw8250_data *data = platform_get_drvdata(pdev);
537
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300538 pm_runtime_get_sync(&pdev->dev);
539
Jamie Iles7d4008e2011-08-26 19:04:50 +0100540 serial8250_unregister_port(data->line);
541
Chen-Yu Tsai7fe090b2014-07-23 23:33:06 +0800542 if (!IS_ERR(data->rst))
543 reset_control_assert(data->rst);
544
Heiko Stübner7d78cbe2014-06-16 15:25:17 +0200545 if (!IS_ERR(data->pclk))
546 clk_disable_unprepare(data->pclk);
547
Emilio Lópeze302cd92013-03-29 00:15:49 +0100548 if (!IS_ERR(data->clk))
549 clk_disable_unprepare(data->clk);
550
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300551 pm_runtime_disable(&pdev->dev);
552 pm_runtime_put_noidle(&pdev->dev);
553
Jamie Iles7d4008e2011-08-26 19:04:50 +0100554 return 0;
555}
556
Mika Westerberg13b949f2014-01-16 14:55:57 +0200557#ifdef CONFIG_PM_SLEEP
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300558static int dw8250_suspend(struct device *dev)
James Hoganb61c5ed2012-10-15 10:25:58 +0100559{
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300560 struct dw8250_data *data = dev_get_drvdata(dev);
James Hoganb61c5ed2012-10-15 10:25:58 +0100561
562 serial8250_suspend_port(data->line);
563
564 return 0;
565}
566
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300567static int dw8250_resume(struct device *dev)
James Hoganb61c5ed2012-10-15 10:25:58 +0100568{
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300569 struct dw8250_data *data = dev_get_drvdata(dev);
James Hoganb61c5ed2012-10-15 10:25:58 +0100570
571 serial8250_resume_port(data->line);
572
573 return 0;
574}
Mika Westerberg13b949f2014-01-16 14:55:57 +0200575#endif /* CONFIG_PM_SLEEP */
James Hoganb61c5ed2012-10-15 10:25:58 +0100576
Rafael J. Wysockid39fe4e2014-12-13 00:41:36 +0100577#ifdef CONFIG_PM
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300578static int dw8250_runtime_suspend(struct device *dev)
579{
580 struct dw8250_data *data = dev_get_drvdata(dev);
581
Ezequiel Garciadbd2df82013-05-07 08:27:16 -0300582 if (!IS_ERR(data->clk))
583 clk_disable_unprepare(data->clk);
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300584
Heiko Stübner7d78cbe2014-06-16 15:25:17 +0200585 if (!IS_ERR(data->pclk))
586 clk_disable_unprepare(data->pclk);
587
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300588 return 0;
589}
590
591static int dw8250_runtime_resume(struct device *dev)
592{
593 struct dw8250_data *data = dev_get_drvdata(dev);
594
Heiko Stübner7d78cbe2014-06-16 15:25:17 +0200595 if (!IS_ERR(data->pclk))
596 clk_prepare_enable(data->pclk);
597
Ezequiel Garciadbd2df82013-05-07 08:27:16 -0300598 if (!IS_ERR(data->clk))
599 clk_prepare_enable(data->clk);
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300600
601 return 0;
602}
603#endif
604
605static const struct dev_pm_ops dw8250_pm_ops = {
606 SET_SYSTEM_SLEEP_PM_OPS(dw8250_suspend, dw8250_resume)
607 SET_RUNTIME_PM_OPS(dw8250_runtime_suspend, dw8250_runtime_resume, NULL)
608};
609
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200610static const struct of_device_id dw8250_of_match[] = {
Jamie Iles7d4008e2011-08-26 19:04:50 +0100611 { .compatible = "snps,dw-apb-uart" },
David Daneyd5f1af72013-06-19 20:37:27 +0000612 { .compatible = "cavium,octeon-3860-uart" },
Jamie Iles7d4008e2011-08-26 19:04:50 +0100613 { /* Sentinel */ }
614};
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200615MODULE_DEVICE_TABLE(of, dw8250_of_match);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100616
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200617static const struct acpi_device_id dw8250_acpi_match[] = {
Heikki Krogerusaea02e82013-04-10 16:58:29 +0300618 { "INT33C4", 0 },
619 { "INT33C5", 0 },
Mika Westerbergd24c1952013-12-10 12:56:59 +0200620 { "INT3434", 0 },
621 { "INT3435", 0 },
Heikki Krogerus4e26b132014-06-05 16:51:40 +0300622 { "80860F0A", 0 },
Alan Coxf1744422014-08-19 16:34:49 +0300623 { "8086228A", 0 },
Feng Kan5e1aeea2014-12-05 17:45:57 -0800624 { "APMC0D08", 0},
Ken Xue5ef86b72015-03-09 17:10:13 +0800625 { "AMD0020", 0 },
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200626 { },
627};
628MODULE_DEVICE_TABLE(acpi, dw8250_acpi_match);
629
Jamie Iles7d4008e2011-08-26 19:04:50 +0100630static struct platform_driver dw8250_platform_driver = {
631 .driver = {
632 .name = "dw-apb-uart",
Heikki Krogerusffc3ae62013-04-10 16:58:28 +0300633 .pm = &dw8250_pm_ops,
Heikki Krogerusa7260c82013-01-10 11:25:08 +0200634 .of_match_table = dw8250_of_match,
Heikki Krogerus6a7320c2013-01-10 11:25:10 +0200635 .acpi_match_table = ACPI_PTR(dw8250_acpi_match),
Jamie Iles7d4008e2011-08-26 19:04:50 +0100636 },
637 .probe = dw8250_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500638 .remove = dw8250_remove,
Jamie Iles7d4008e2011-08-26 19:04:50 +0100639};
640
Axel Linc8381c152011-11-28 19:22:15 +0800641module_platform_driver(dw8250_platform_driver);
Jamie Iles7d4008e2011-08-26 19:04:50 +0100642
643MODULE_AUTHOR("Jamie Iles");
644MODULE_LICENSE("GPL");
645MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver");
Mika Westerbergf3ac3fc2015-02-04 15:03:48 +0200646MODULE_ALIAS("platform:dw-apb-uart");