1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Synopsys DesignWare 8250 driver.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright 2011 Picochip, Jamie Iles.
6*4882a593Smuzhiyun * Copyright 2013 Intel Corporation
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * The Synopsys DesignWare 8250 has an extra feature whereby it detects if the
9*4882a593Smuzhiyun * LCR is written whilst busy. If it is, then a busy detect interrupt is
10*4882a593Smuzhiyun * raised, the LCR needs to be rewritten and the uart status register read.
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun #include <linux/delay.h>
13*4882a593Smuzhiyun #include <linux/device.h>
14*4882a593Smuzhiyun #include <linux/io.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/serial_8250.h>
17*4882a593Smuzhiyun #include <linux/serial_reg.h>
18*4882a593Smuzhiyun #include <linux/of.h>
19*4882a593Smuzhiyun #include <linux/of_irq.h>
20*4882a593Smuzhiyun #include <linux/of_platform.h>
21*4882a593Smuzhiyun #include <linux/platform_device.h>
22*4882a593Smuzhiyun #include <linux/workqueue.h>
23*4882a593Smuzhiyun #include <linux/notifier.h>
24*4882a593Smuzhiyun #include <linux/slab.h>
25*4882a593Smuzhiyun #include <linux/acpi.h>
26*4882a593Smuzhiyun #include <linux/clk.h>
27*4882a593Smuzhiyun #include <linux/reset.h>
28*4882a593Smuzhiyun #include <linux/pm_runtime.h>
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #include <asm/byteorder.h>
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #ifdef MODULE
33*4882a593Smuzhiyun #include "8250_dwlib.c"
34*4882a593Smuzhiyun #else
35*4882a593Smuzhiyun #include "8250_dwlib.h"
36*4882a593Smuzhiyun #endif
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /* Offsets for the DesignWare specific registers */
39*4882a593Smuzhiyun #define DW_UART_USR 0x1f /* UART Status Register */
40*4882a593Smuzhiyun #define DW_UART_RFL 0x21 /* UART Receive Fifo Level Register */
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* DesignWare specific register fields */
43*4882a593Smuzhiyun #define DW_UART_MCR_SIRE BIT(6)
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun struct dw8250_data {
46*4882a593Smuzhiyun struct dw8250_port_data data;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun u8 usr_reg;
49*4882a593Smuzhiyun int msr_mask_on;
50*4882a593Smuzhiyun int msr_mask_off;
51*4882a593Smuzhiyun struct clk *clk;
52*4882a593Smuzhiyun struct clk *pclk;
53*4882a593Smuzhiyun struct notifier_block clk_notifier;
54*4882a593Smuzhiyun struct work_struct clk_work;
55*4882a593Smuzhiyun struct reset_control *rst;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun #ifdef CONFIG_ARCH_ROCKCHIP
58*4882a593Smuzhiyun int irq;
59*4882a593Smuzhiyun int irq_wake;
60*4882a593Smuzhiyun int enable_wakeup;
61*4882a593Smuzhiyun #endif
62*4882a593Smuzhiyun unsigned int skip_autocfg:1;
63*4882a593Smuzhiyun unsigned int uart_16550_compatible:1;
64*4882a593Smuzhiyun };
65*4882a593Smuzhiyun
to_dw8250_data(struct dw8250_port_data * data)66*4882a593Smuzhiyun static inline struct dw8250_data *to_dw8250_data(struct dw8250_port_data *data)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun return container_of(data, struct dw8250_data, data);
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun
clk_to_dw8250_data(struct notifier_block * nb)71*4882a593Smuzhiyun static inline struct dw8250_data *clk_to_dw8250_data(struct notifier_block *nb)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun return container_of(nb, struct dw8250_data, clk_notifier);
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
work_to_dw8250_data(struct work_struct * work)76*4882a593Smuzhiyun static inline struct dw8250_data *work_to_dw8250_data(struct work_struct *work)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun return container_of(work, struct dw8250_data, clk_work);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
dw8250_modify_msr(struct uart_port * p,int offset,int value)81*4882a593Smuzhiyun static inline int dw8250_modify_msr(struct uart_port *p, int offset, int value)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun struct dw8250_data *d = to_dw8250_data(p->private_data);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /* Override any modem control signals if needed */
86*4882a593Smuzhiyun if (offset == UART_MSR) {
87*4882a593Smuzhiyun value |= d->msr_mask_on;
88*4882a593Smuzhiyun value &= ~d->msr_mask_off;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun return value;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
dw8250_force_idle(struct uart_port * p)94*4882a593Smuzhiyun static void dw8250_force_idle(struct uart_port *p)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun struct uart_8250_port *up = up_to_u8250p(p);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun serial8250_clear_and_reinit_fifos(up);
99*4882a593Smuzhiyun (void)p->serial_in(p, UART_RX);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
dw8250_check_lcr(struct uart_port * p,int value)102*4882a593Smuzhiyun static void dw8250_check_lcr(struct uart_port *p, int value)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun void __iomem *offset = p->membase + (UART_LCR << p->regshift);
105*4882a593Smuzhiyun int tries = 1000;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /* Make sure LCR write wasn't ignored */
108*4882a593Smuzhiyun while (tries--) {
109*4882a593Smuzhiyun unsigned int lcr = p->serial_in(p, UART_LCR);
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun if ((value & ~UART_LCR_SPAR) == (lcr & ~UART_LCR_SPAR))
112*4882a593Smuzhiyun return;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun dw8250_force_idle(p);
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun #ifdef CONFIG_64BIT
117*4882a593Smuzhiyun if (p->type == PORT_OCTEON)
118*4882a593Smuzhiyun __raw_writeq(value & 0xff, offset);
119*4882a593Smuzhiyun else
120*4882a593Smuzhiyun #endif
121*4882a593Smuzhiyun if (p->iotype == UPIO_MEM32)
122*4882a593Smuzhiyun writel(value, offset);
123*4882a593Smuzhiyun else if (p->iotype == UPIO_MEM32BE)
124*4882a593Smuzhiyun iowrite32be(value, offset);
125*4882a593Smuzhiyun else
126*4882a593Smuzhiyun writeb(value, offset);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun * FIXME: this deadlocks if port->lock is already held
130*4882a593Smuzhiyun * dev_err(p->dev, "Couldn't set LCR to %d\n", value);
131*4882a593Smuzhiyun */
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /* Returns once the transmitter is empty or we run out of retries */
dw8250_tx_wait_empty(struct uart_port * p)135*4882a593Smuzhiyun static void dw8250_tx_wait_empty(struct uart_port *p)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun struct uart_8250_port *up = up_to_u8250p(p);
138*4882a593Smuzhiyun unsigned int tries = 20000;
139*4882a593Smuzhiyun unsigned int delay_threshold = tries - 1000;
140*4882a593Smuzhiyun unsigned int lsr;
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun while (tries--) {
143*4882a593Smuzhiyun lsr = readb (p->membase + (UART_LSR << p->regshift));
144*4882a593Smuzhiyun up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun if (lsr & UART_LSR_TEMT)
147*4882a593Smuzhiyun break;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /* The device is first given a chance to empty without delay,
150*4882a593Smuzhiyun * to avoid slowdowns at high bitrates. If after 1000 tries
151*4882a593Smuzhiyun * the buffer has still not emptied, allow more time for low-
152*4882a593Smuzhiyun * speed links. */
153*4882a593Smuzhiyun if (tries < delay_threshold)
154*4882a593Smuzhiyun udelay (1);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
dw8250_serial_out38x(struct uart_port * p,int offset,int value)158*4882a593Smuzhiyun static void dw8250_serial_out38x(struct uart_port *p, int offset, int value)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun struct dw8250_data *d = to_dw8250_data(p->private_data);
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /* Allow the TX to drain before we reconfigure */
163*4882a593Smuzhiyun if (offset == UART_LCR)
164*4882a593Smuzhiyun dw8250_tx_wait_empty(p);
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun writeb(value, p->membase + (offset << p->regshift));
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun if (offset == UART_LCR && !d->uart_16550_compatible)
169*4882a593Smuzhiyun dw8250_check_lcr(p, value);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun
dw8250_serial_out(struct uart_port * p,int offset,int value)173*4882a593Smuzhiyun static void dw8250_serial_out(struct uart_port *p, int offset, int value)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun struct dw8250_data *d = to_dw8250_data(p->private_data);
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun writeb(value, p->membase + (offset << p->regshift));
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun if (offset == UART_LCR && !d->uart_16550_compatible)
180*4882a593Smuzhiyun dw8250_check_lcr(p, value);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
dw8250_serial_in(struct uart_port * p,int offset)183*4882a593Smuzhiyun static unsigned int dw8250_serial_in(struct uart_port *p, int offset)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun unsigned int value = readb(p->membase + (offset << p->regshift));
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun return dw8250_modify_msr(p, offset, value);
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun #ifdef CONFIG_64BIT
dw8250_serial_inq(struct uart_port * p,int offset)191*4882a593Smuzhiyun static unsigned int dw8250_serial_inq(struct uart_port *p, int offset)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun unsigned int value;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun value = (u8)__raw_readq(p->membase + (offset << p->regshift));
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun return dw8250_modify_msr(p, offset, value);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
dw8250_serial_outq(struct uart_port * p,int offset,int value)200*4882a593Smuzhiyun static void dw8250_serial_outq(struct uart_port *p, int offset, int value)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun struct dw8250_data *d = to_dw8250_data(p->private_data);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun value &= 0xff;
205*4882a593Smuzhiyun __raw_writeq(value, p->membase + (offset << p->regshift));
206*4882a593Smuzhiyun /* Read back to ensure register write ordering. */
207*4882a593Smuzhiyun __raw_readq(p->membase + (UART_LCR << p->regshift));
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (offset == UART_LCR && !d->uart_16550_compatible)
210*4882a593Smuzhiyun dw8250_check_lcr(p, value);
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun #endif /* CONFIG_64BIT */
213*4882a593Smuzhiyun
dw8250_serial_out32(struct uart_port * p,int offset,int value)214*4882a593Smuzhiyun static void dw8250_serial_out32(struct uart_port *p, int offset, int value)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun struct dw8250_data *d = to_dw8250_data(p->private_data);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun writel(value, p->membase + (offset << p->regshift));
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun if (offset == UART_LCR && !d->uart_16550_compatible)
221*4882a593Smuzhiyun dw8250_check_lcr(p, value);
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
dw8250_serial_in32(struct uart_port * p,int offset)224*4882a593Smuzhiyun static unsigned int dw8250_serial_in32(struct uart_port *p, int offset)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun unsigned int value = readl(p->membase + (offset << p->regshift));
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun return dw8250_modify_msr(p, offset, value);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
dw8250_serial_out32be(struct uart_port * p,int offset,int value)231*4882a593Smuzhiyun static void dw8250_serial_out32be(struct uart_port *p, int offset, int value)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun struct dw8250_data *d = to_dw8250_data(p->private_data);
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun iowrite32be(value, p->membase + (offset << p->regshift));
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun if (offset == UART_LCR && !d->uart_16550_compatible)
238*4882a593Smuzhiyun dw8250_check_lcr(p, value);
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun
dw8250_serial_in32be(struct uart_port * p,int offset)241*4882a593Smuzhiyun static unsigned int dw8250_serial_in32be(struct uart_port *p, int offset)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun unsigned int value = ioread32be(p->membase + (offset << p->regshift));
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun return dw8250_modify_msr(p, offset, value);
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun
dw8250_handle_irq(struct uart_port * p)249*4882a593Smuzhiyun static int dw8250_handle_irq(struct uart_port *p)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun struct dw8250_data *d = to_dw8250_data(p->private_data);
252*4882a593Smuzhiyun unsigned int iir = p->serial_in(p, UART_IIR);
253*4882a593Smuzhiyun unsigned int status, usr, rfl;
254*4882a593Smuzhiyun unsigned long flags;
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun /*
257*4882a593Smuzhiyun * There are ways to get Designware-based UARTs into a state where
258*4882a593Smuzhiyun * they are asserting UART_IIR_RX_TIMEOUT but there is no actual
259*4882a593Smuzhiyun * data available. If we see such a case then we'll do a bogus
260*4882a593Smuzhiyun * read. If we don't do this then the "RX TIMEOUT" interrupt will
261*4882a593Smuzhiyun * fire forever.
262*4882a593Smuzhiyun */
263*4882a593Smuzhiyun if ((iir & 0x3f) == UART_IIR_RX_TIMEOUT) {
264*4882a593Smuzhiyun spin_lock_irqsave(&p->lock, flags);
265*4882a593Smuzhiyun usr = p->serial_in(p, d->usr_reg);
266*4882a593Smuzhiyun status = p->serial_in(p, UART_LSR);
267*4882a593Smuzhiyun rfl = p->serial_in(p, DW_UART_RFL);
268*4882a593Smuzhiyun if (!(status & (UART_LSR_DR | UART_LSR_BI)) && !(usr & 0x1) && (rfl == 0))
269*4882a593Smuzhiyun (void) p->serial_in(p, UART_RX);
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun spin_unlock_irqrestore(&p->lock, flags);
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun if (serial8250_handle_irq(p, iir))
275*4882a593Smuzhiyun return 1;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
278*4882a593Smuzhiyun /* Clear the USR */
279*4882a593Smuzhiyun (void)p->serial_in(p, d->usr_reg);
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun return 1;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun return 0;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
dw8250_clk_work_cb(struct work_struct * work)287*4882a593Smuzhiyun static void dw8250_clk_work_cb(struct work_struct *work)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun struct dw8250_data *d = work_to_dw8250_data(work);
290*4882a593Smuzhiyun struct uart_8250_port *up;
291*4882a593Smuzhiyun unsigned long rate;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun rate = clk_get_rate(d->clk);
294*4882a593Smuzhiyun if (rate <= 0)
295*4882a593Smuzhiyun return;
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun up = serial8250_get_port(d->data.line);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun serial8250_update_uartclk(&up->port, rate);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
dw8250_clk_notifier_cb(struct notifier_block * nb,unsigned long event,void * data)302*4882a593Smuzhiyun static int dw8250_clk_notifier_cb(struct notifier_block *nb,
303*4882a593Smuzhiyun unsigned long event, void *data)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun struct dw8250_data *d = clk_to_dw8250_data(nb);
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun /*
308*4882a593Smuzhiyun * We have no choice but to defer the uartclk update due to two
309*4882a593Smuzhiyun * deadlocks. First one is caused by a recursive mutex lock which
310*4882a593Smuzhiyun * happens when clk_set_rate() is called from dw8250_set_termios().
311*4882a593Smuzhiyun * Second deadlock is more tricky and is caused by an inverted order of
312*4882a593Smuzhiyun * the clk and tty-port mutexes lock. It happens if clock rate change
313*4882a593Smuzhiyun * is requested asynchronously while set_termios() is executed between
314*4882a593Smuzhiyun * tty-port mutex lock and clk_set_rate() function invocation and
315*4882a593Smuzhiyun * vise-versa. Anyway if we didn't have the reference clock alteration
316*4882a593Smuzhiyun * in the dw8250_set_termios() method we wouldn't have needed this
317*4882a593Smuzhiyun * deferred event handling complication.
318*4882a593Smuzhiyun */
319*4882a593Smuzhiyun if (event == POST_RATE_CHANGE) {
320*4882a593Smuzhiyun queue_work(system_unbound_wq, &d->clk_work);
321*4882a593Smuzhiyun return NOTIFY_OK;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun return NOTIFY_DONE;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun static void
dw8250_do_pm(struct uart_port * port,unsigned int state,unsigned int old)328*4882a593Smuzhiyun dw8250_do_pm(struct uart_port *port, unsigned int state, unsigned int old)
329*4882a593Smuzhiyun {
330*4882a593Smuzhiyun if (!state)
331*4882a593Smuzhiyun pm_runtime_get_sync(port->dev);
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun serial8250_do_pm(port, state, old);
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun if (state)
336*4882a593Smuzhiyun pm_runtime_put_sync_suspend(port->dev);
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun
dw8250_set_termios(struct uart_port * p,struct ktermios * termios,struct ktermios * old)339*4882a593Smuzhiyun static void dw8250_set_termios(struct uart_port *p, struct ktermios *termios,
340*4882a593Smuzhiyun struct ktermios *old)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun #ifndef CONFIG_ARCH_ROCKCHIP
343*4882a593Smuzhiyun unsigned long newrate = tty_termios_baud_rate(termios) * 16;
344*4882a593Smuzhiyun #endif
345*4882a593Smuzhiyun struct dw8250_data *d = to_dw8250_data(p->private_data);
346*4882a593Smuzhiyun long rate;
347*4882a593Smuzhiyun #ifdef CONFIG_ARCH_ROCKCHIP
348*4882a593Smuzhiyun unsigned int baud = tty_termios_baud_rate(termios);
349*4882a593Smuzhiyun unsigned int rate_temp, diff;
350*4882a593Smuzhiyun #endif
351*4882a593Smuzhiyun int ret;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun clk_disable_unprepare(d->clk);
354*4882a593Smuzhiyun #ifdef CONFIG_ARCH_ROCKCHIP
355*4882a593Smuzhiyun if (d->clk) {
356*4882a593Smuzhiyun if (baud <= 115200)
357*4882a593Smuzhiyun rate = 24000000;
358*4882a593Smuzhiyun else if (baud == 230400)
359*4882a593Smuzhiyun rate = baud * 16 * 2;
360*4882a593Smuzhiyun else if (baud == 1152000)
361*4882a593Smuzhiyun rate = baud * 16 * 2;
362*4882a593Smuzhiyun else
363*4882a593Smuzhiyun rate = baud * 16;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun ret = clk_set_rate(d->clk, rate);
366*4882a593Smuzhiyun rate_temp = clk_get_rate(d->clk);
367*4882a593Smuzhiyun diff = rate * 20 / 1000;
368*4882a593Smuzhiyun /*
369*4882a593Smuzhiyun * If rate_temp is not equal to rate, is means fractional frequency
370*4882a593Smuzhiyun * division is failed. Then use Integer frequency division, and
371*4882a593Smuzhiyun * the baud rate error must be under -+2%
372*4882a593Smuzhiyun */
373*4882a593Smuzhiyun if ((rate_temp < rate) && ((rate - rate_temp) > diff)) {
374*4882a593Smuzhiyun ret = clk_set_rate(d->clk, rate + diff);
375*4882a593Smuzhiyun rate_temp = clk_get_rate(d->clk);
376*4882a593Smuzhiyun if ((rate_temp < rate) && ((rate - rate_temp) > diff))
377*4882a593Smuzhiyun dev_info(p->dev, "set rate:%ld, but get rate:%d\n",
378*4882a593Smuzhiyun rate, rate_temp);
379*4882a593Smuzhiyun else if ((rate < rate_temp) && ((rate_temp - rate) > diff))
380*4882a593Smuzhiyun dev_info(p->dev, "set rate:%ld, but get rate:%d\n",
381*4882a593Smuzhiyun rate, rate_temp);
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun if (!ret)
384*4882a593Smuzhiyun p->uartclk = rate;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun #else
387*4882a593Smuzhiyun rate = clk_round_rate(d->clk, newrate);
388*4882a593Smuzhiyun if (rate > 0) {
389*4882a593Smuzhiyun /*
390*4882a593Smuzhiyun * Premilinary set the uartclk to the new clock rate so the
391*4882a593Smuzhiyun * clock update event handler caused by the clk_set_rate()
392*4882a593Smuzhiyun * calling wouldn't actually update the UART divisor since
393*4882a593Smuzhiyun * we about to do this anyway.
394*4882a593Smuzhiyun */
395*4882a593Smuzhiyun swap(p->uartclk, rate);
396*4882a593Smuzhiyun ret = clk_set_rate(d->clk, newrate);
397*4882a593Smuzhiyun if (ret)
398*4882a593Smuzhiyun swap(p->uartclk, rate);
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun #endif
401*4882a593Smuzhiyun clk_prepare_enable(d->clk);
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun p->status &= ~UPSTAT_AUTOCTS;
404*4882a593Smuzhiyun if (termios->c_cflag & CRTSCTS)
405*4882a593Smuzhiyun p->status |= UPSTAT_AUTOCTS;
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun serial8250_do_set_termios(p, termios, old);
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
dw8250_set_ldisc(struct uart_port * p,struct ktermios * termios)410*4882a593Smuzhiyun static void dw8250_set_ldisc(struct uart_port *p, struct ktermios *termios)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun struct uart_8250_port *up = up_to_u8250p(p);
413*4882a593Smuzhiyun unsigned int mcr = p->serial_in(p, UART_MCR);
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun if (up->capabilities & UART_CAP_IRDA) {
416*4882a593Smuzhiyun if (termios->c_line == N_IRDA)
417*4882a593Smuzhiyun mcr |= DW_UART_MCR_SIRE;
418*4882a593Smuzhiyun else
419*4882a593Smuzhiyun mcr &= ~DW_UART_MCR_SIRE;
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun p->serial_out(p, UART_MCR, mcr);
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun serial8250_do_set_ldisc(p, termios);
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun /*
427*4882a593Smuzhiyun * dw8250_fallback_dma_filter will prevent the UART from getting just any free
428*4882a593Smuzhiyun * channel on platforms that have DMA engines, but don't have any channels
429*4882a593Smuzhiyun * assigned to the UART.
430*4882a593Smuzhiyun *
431*4882a593Smuzhiyun * REVISIT: This is a work around for limitation in the DMA Engine API. Once the
432*4882a593Smuzhiyun * core problem is fixed, this function is no longer needed.
433*4882a593Smuzhiyun */
dw8250_fallback_dma_filter(struct dma_chan * chan,void * param)434*4882a593Smuzhiyun static bool dw8250_fallback_dma_filter(struct dma_chan *chan, void *param)
435*4882a593Smuzhiyun {
436*4882a593Smuzhiyun return false;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun
dw8250_idma_filter(struct dma_chan * chan,void * param)439*4882a593Smuzhiyun static bool dw8250_idma_filter(struct dma_chan *chan, void *param)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun return param == chan->device->dev;
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun
dw8250_quirks(struct uart_port * p,struct dw8250_data * data)444*4882a593Smuzhiyun static void dw8250_quirks(struct uart_port *p, struct dw8250_data *data)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun if (p->dev->of_node) {
447*4882a593Smuzhiyun struct device_node *np = p->dev->of_node;
448*4882a593Smuzhiyun int id;
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun /* get index of serial line, if found in DT aliases */
451*4882a593Smuzhiyun id = of_alias_get_id(np, "serial");
452*4882a593Smuzhiyun if (id >= 0)
453*4882a593Smuzhiyun p->line = id;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_ROCKCHIP_MINI_KERNEL))
456*4882a593Smuzhiyun return;
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun #ifdef CONFIG_64BIT
459*4882a593Smuzhiyun if (of_device_is_compatible(np, "cavium,octeon-3860-uart")) {
460*4882a593Smuzhiyun p->serial_in = dw8250_serial_inq;
461*4882a593Smuzhiyun p->serial_out = dw8250_serial_outq;
462*4882a593Smuzhiyun p->flags = UPF_SKIP_TEST | UPF_SHARE_IRQ | UPF_FIXED_TYPE;
463*4882a593Smuzhiyun p->type = PORT_OCTEON;
464*4882a593Smuzhiyun data->usr_reg = 0x27;
465*4882a593Smuzhiyun data->skip_autocfg = true;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun #endif
468*4882a593Smuzhiyun if (of_device_is_big_endian(p->dev->of_node)) {
469*4882a593Smuzhiyun p->iotype = UPIO_MEM32BE;
470*4882a593Smuzhiyun p->serial_in = dw8250_serial_in32be;
471*4882a593Smuzhiyun p->serial_out = dw8250_serial_out32be;
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun if (of_device_is_compatible(np, "marvell,armada-38x-uart"))
474*4882a593Smuzhiyun p->serial_out = dw8250_serial_out38x;
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun } else if (acpi_dev_present("APMC0D08", NULL, -1)) {
477*4882a593Smuzhiyun p->iotype = UPIO_MEM32;
478*4882a593Smuzhiyun p->regshift = 2;
479*4882a593Smuzhiyun p->serial_in = dw8250_serial_in32;
480*4882a593Smuzhiyun data->uart_16550_compatible = true;
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_ROCKCHIP_MINI_KERNEL))
484*4882a593Smuzhiyun return;
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun /* Platforms with iDMA 64-bit */
487*4882a593Smuzhiyun if (platform_get_resource_byname(to_platform_device(p->dev),
488*4882a593Smuzhiyun IORESOURCE_MEM, "lpss_priv")) {
489*4882a593Smuzhiyun data->data.dma.rx_param = p->dev->parent;
490*4882a593Smuzhiyun data->data.dma.tx_param = p->dev->parent;
491*4882a593Smuzhiyun data->data.dma.fn = dw8250_idma_filter;
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun
dw8250_probe(struct platform_device * pdev)495*4882a593Smuzhiyun static int dw8250_probe(struct platform_device *pdev)
496*4882a593Smuzhiyun {
497*4882a593Smuzhiyun struct uart_8250_port uart = {}, *up = &uart;
498*4882a593Smuzhiyun struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
499*4882a593Smuzhiyun struct uart_port *p = &up->port;
500*4882a593Smuzhiyun struct device *dev = &pdev->dev;
501*4882a593Smuzhiyun struct dw8250_data *data;
502*4882a593Smuzhiyun int irq;
503*4882a593Smuzhiyun int err;
504*4882a593Smuzhiyun u32 val;
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun if (!regs) {
507*4882a593Smuzhiyun dev_err(dev, "no registers defined\n");
508*4882a593Smuzhiyun return -EINVAL;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun irq = platform_get_irq(pdev, 0);
512*4882a593Smuzhiyun if (irq < 0)
513*4882a593Smuzhiyun return irq;
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun spin_lock_init(&p->lock);
516*4882a593Smuzhiyun p->mapbase = regs->start;
517*4882a593Smuzhiyun p->irq = irq;
518*4882a593Smuzhiyun p->handle_irq = dw8250_handle_irq;
519*4882a593Smuzhiyun p->pm = dw8250_do_pm;
520*4882a593Smuzhiyun p->type = PORT_8250;
521*4882a593Smuzhiyun p->flags = UPF_SHARE_IRQ | UPF_FIXED_PORT;
522*4882a593Smuzhiyun p->dev = dev;
523*4882a593Smuzhiyun p->iotype = UPIO_MEM;
524*4882a593Smuzhiyun p->serial_in = dw8250_serial_in;
525*4882a593Smuzhiyun p->serial_out = dw8250_serial_out;
526*4882a593Smuzhiyun p->set_ldisc = dw8250_set_ldisc;
527*4882a593Smuzhiyun p->set_termios = dw8250_set_termios;
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun p->membase = devm_ioremap(dev, regs->start, resource_size(regs));
530*4882a593Smuzhiyun if (!p->membase)
531*4882a593Smuzhiyun return -ENOMEM;
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
534*4882a593Smuzhiyun if (!data)
535*4882a593Smuzhiyun return -ENOMEM;
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun data->data.dma.fn = dw8250_fallback_dma_filter;
538*4882a593Smuzhiyun data->usr_reg = DW_UART_USR;
539*4882a593Smuzhiyun p->private_data = &data->data;
540*4882a593Smuzhiyun #ifdef CONFIG_ARCH_ROCKCHIP
541*4882a593Smuzhiyun data->irq = irq;
542*4882a593Smuzhiyun #endif
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun data->uart_16550_compatible = device_property_read_bool(dev,
545*4882a593Smuzhiyun "snps,uart-16550-compatible");
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun err = device_property_read_u32(dev, "reg-shift", &val);
548*4882a593Smuzhiyun if (!err)
549*4882a593Smuzhiyun p->regshift = val;
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun err = device_property_read_u32(dev, "reg-io-width", &val);
552*4882a593Smuzhiyun if (!err && val == 4) {
553*4882a593Smuzhiyun p->iotype = UPIO_MEM32;
554*4882a593Smuzhiyun p->serial_in = dw8250_serial_in32;
555*4882a593Smuzhiyun p->serial_out = dw8250_serial_out32;
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun if (device_property_read_bool(dev, "dcd-override")) {
559*4882a593Smuzhiyun /* Always report DCD as active */
560*4882a593Smuzhiyun data->msr_mask_on |= UART_MSR_DCD;
561*4882a593Smuzhiyun data->msr_mask_off |= UART_MSR_DDCD;
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun if (device_property_read_bool(dev, "dsr-override")) {
565*4882a593Smuzhiyun /* Always report DSR as active */
566*4882a593Smuzhiyun data->msr_mask_on |= UART_MSR_DSR;
567*4882a593Smuzhiyun data->msr_mask_off |= UART_MSR_DDSR;
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun if (device_property_read_bool(dev, "cts-override")) {
571*4882a593Smuzhiyun /* Always report CTS as active */
572*4882a593Smuzhiyun data->msr_mask_on |= UART_MSR_CTS;
573*4882a593Smuzhiyun data->msr_mask_off |= UART_MSR_DCTS;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun if (device_property_read_bool(dev, "ri-override")) {
577*4882a593Smuzhiyun /* Always report Ring indicator as inactive */
578*4882a593Smuzhiyun data->msr_mask_off |= UART_MSR_RI;
579*4882a593Smuzhiyun data->msr_mask_off |= UART_MSR_TERI;
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun #ifdef CONFIG_ARCH_ROCKCHIP
583*4882a593Smuzhiyun if (device_property_read_bool(p->dev, "wakeup-source"))
584*4882a593Smuzhiyun data->enable_wakeup = 1;
585*4882a593Smuzhiyun else
586*4882a593Smuzhiyun data->enable_wakeup = 0;
587*4882a593Smuzhiyun #endif
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun /* Always ask for fixed clock rate from a property. */
590*4882a593Smuzhiyun device_property_read_u32(dev, "clock-frequency", &p->uartclk);
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun /* If there is separate baudclk, get the rate from it. */
593*4882a593Smuzhiyun data->clk = devm_clk_get_optional(dev, "baudclk");
594*4882a593Smuzhiyun if (data->clk == NULL)
595*4882a593Smuzhiyun data->clk = devm_clk_get_optional(dev, NULL);
596*4882a593Smuzhiyun if (IS_ERR(data->clk))
597*4882a593Smuzhiyun return PTR_ERR(data->clk);
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun INIT_WORK(&data->clk_work, dw8250_clk_work_cb);
600*4882a593Smuzhiyun data->clk_notifier.notifier_call = dw8250_clk_notifier_cb;
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun err = clk_prepare_enable(data->clk);
603*4882a593Smuzhiyun if (err)
604*4882a593Smuzhiyun dev_warn(dev, "could not enable optional baudclk: %d\n", err);
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun if (data->clk)
607*4882a593Smuzhiyun p->uartclk = clk_get_rate(data->clk);
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun /* If no clock rate is defined, fail. */
610*4882a593Smuzhiyun if (!p->uartclk) {
611*4882a593Smuzhiyun dev_err(dev, "clock rate not defined\n");
612*4882a593Smuzhiyun err = -EINVAL;
613*4882a593Smuzhiyun goto err_clk;
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun data->pclk = devm_clk_get_optional(dev, "apb_pclk");
617*4882a593Smuzhiyun if (IS_ERR(data->pclk)) {
618*4882a593Smuzhiyun err = PTR_ERR(data->pclk);
619*4882a593Smuzhiyun goto err_clk;
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun err = clk_prepare_enable(data->pclk);
623*4882a593Smuzhiyun if (err) {
624*4882a593Smuzhiyun dev_err(dev, "could not enable apb_pclk\n");
625*4882a593Smuzhiyun goto err_clk;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun data->rst = devm_reset_control_get_optional_exclusive(dev, NULL);
629*4882a593Smuzhiyun if (IS_ERR(data->rst)) {
630*4882a593Smuzhiyun err = PTR_ERR(data->rst);
631*4882a593Smuzhiyun goto err_pclk;
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun reset_control_deassert(data->rst);
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun dw8250_quirks(p, data);
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun /* If the Busy Functionality is not implemented, don't handle it */
638*4882a593Smuzhiyun if (data->uart_16550_compatible)
639*4882a593Smuzhiyun p->handle_irq = NULL;
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun if (!data->skip_autocfg)
642*4882a593Smuzhiyun dw8250_setup_port(p);
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun /* If we have a valid fifosize, try hooking up DMA */
645*4882a593Smuzhiyun if (p->fifosize) {
646*4882a593Smuzhiyun data->data.dma.rxconf.src_maxburst = p->fifosize / 4;
647*4882a593Smuzhiyun data->data.dma.txconf.dst_maxburst = p->fifosize / 4;
648*4882a593Smuzhiyun up->dma = &data->data.dma;
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun data->data.line = serial8250_register_8250_port(up);
652*4882a593Smuzhiyun if (data->data.line < 0) {
653*4882a593Smuzhiyun err = data->data.line;
654*4882a593Smuzhiyun goto err_reset;
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun /*
658*4882a593Smuzhiyun * Some platforms may provide a reference clock shared between several
659*4882a593Smuzhiyun * devices. In this case any clock state change must be known to the
660*4882a593Smuzhiyun * UART port at least post factum.
661*4882a593Smuzhiyun */
662*4882a593Smuzhiyun if (data->clk) {
663*4882a593Smuzhiyun err = clk_notifier_register(data->clk, &data->clk_notifier);
664*4882a593Smuzhiyun if (err)
665*4882a593Smuzhiyun dev_warn(p->dev, "Failed to set the clock notifier\n");
666*4882a593Smuzhiyun else
667*4882a593Smuzhiyun queue_work(system_unbound_wq, &data->clk_work);
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun #ifdef CONFIG_ARCH_ROCKCHIP
670*4882a593Smuzhiyun if (data->enable_wakeup)
671*4882a593Smuzhiyun device_init_wakeup(&pdev->dev, true);
672*4882a593Smuzhiyun #endif
673*4882a593Smuzhiyun platform_set_drvdata(pdev, data);
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun pm_runtime_set_active(dev);
676*4882a593Smuzhiyun pm_runtime_enable(dev);
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun return 0;
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun err_reset:
681*4882a593Smuzhiyun reset_control_assert(data->rst);
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun err_pclk:
684*4882a593Smuzhiyun clk_disable_unprepare(data->pclk);
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun err_clk:
687*4882a593Smuzhiyun clk_disable_unprepare(data->clk);
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun return err;
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun
dw8250_remove(struct platform_device * pdev)692*4882a593Smuzhiyun static int dw8250_remove(struct platform_device *pdev)
693*4882a593Smuzhiyun {
694*4882a593Smuzhiyun struct dw8250_data *data = platform_get_drvdata(pdev);
695*4882a593Smuzhiyun struct device *dev = &pdev->dev;
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun pm_runtime_get_sync(dev);
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun if (data->clk) {
700*4882a593Smuzhiyun clk_notifier_unregister(data->clk, &data->clk_notifier);
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun flush_work(&data->clk_work);
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun serial8250_unregister_port(data->data.line);
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun reset_control_assert(data->rst);
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun clk_disable_unprepare(data->pclk);
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun clk_disable_unprepare(data->clk);
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun pm_runtime_disable(dev);
714*4882a593Smuzhiyun pm_runtime_put_noidle(dev);
715*4882a593Smuzhiyun #ifdef CONFIG_ARCH_ROCKCHIP
716*4882a593Smuzhiyun if (data->enable_wakeup)
717*4882a593Smuzhiyun device_init_wakeup(&pdev->dev, false);
718*4882a593Smuzhiyun #endif
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun return 0;
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
dw8250_suspend(struct device * dev)724*4882a593Smuzhiyun static int dw8250_suspend(struct device *dev)
725*4882a593Smuzhiyun {
726*4882a593Smuzhiyun struct dw8250_data *data = dev_get_drvdata(dev);
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun #ifdef CONFIG_ARCH_ROCKCHIP
729*4882a593Smuzhiyun if (device_may_wakeup(dev)) {
730*4882a593Smuzhiyun if (!enable_irq_wake(data->irq))
731*4882a593Smuzhiyun data->irq_wake = 1;
732*4882a593Smuzhiyun return 0;
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun #endif
735*4882a593Smuzhiyun serial8250_suspend_port(data->data.line);
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun return 0;
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun
dw8250_resume(struct device * dev)740*4882a593Smuzhiyun static int dw8250_resume(struct device *dev)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun struct dw8250_data *data = dev_get_drvdata(dev);
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun #ifdef CONFIG_ARCH_ROCKCHIP
745*4882a593Smuzhiyun if (device_may_wakeup(dev)) {
746*4882a593Smuzhiyun if (data->irq_wake) {
747*4882a593Smuzhiyun disable_irq_wake(data->irq);
748*4882a593Smuzhiyun data->irq_wake = 0;
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun return 0;
751*4882a593Smuzhiyun }
752*4882a593Smuzhiyun #endif
753*4882a593Smuzhiyun serial8250_resume_port(data->data.line);
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun return 0;
756*4882a593Smuzhiyun }
757*4882a593Smuzhiyun #endif /* CONFIG_PM_SLEEP */
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun #ifdef CONFIG_PM
dw8250_runtime_suspend(struct device * dev)760*4882a593Smuzhiyun static int dw8250_runtime_suspend(struct device *dev)
761*4882a593Smuzhiyun {
762*4882a593Smuzhiyun struct dw8250_data *data = dev_get_drvdata(dev);
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun clk_disable_unprepare(data->clk);
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun clk_disable_unprepare(data->pclk);
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun return 0;
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun
dw8250_runtime_resume(struct device * dev)771*4882a593Smuzhiyun static int dw8250_runtime_resume(struct device *dev)
772*4882a593Smuzhiyun {
773*4882a593Smuzhiyun struct dw8250_data *data = dev_get_drvdata(dev);
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun clk_prepare_enable(data->pclk);
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun clk_prepare_enable(data->clk);
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun return 0;
780*4882a593Smuzhiyun }
781*4882a593Smuzhiyun #endif
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun static const struct dev_pm_ops dw8250_pm_ops = {
784*4882a593Smuzhiyun SET_SYSTEM_SLEEP_PM_OPS(dw8250_suspend, dw8250_resume)
785*4882a593Smuzhiyun SET_RUNTIME_PM_OPS(dw8250_runtime_suspend, dw8250_runtime_resume, NULL)
786*4882a593Smuzhiyun };
787*4882a593Smuzhiyun
788*4882a593Smuzhiyun static const struct of_device_id dw8250_of_match[] = {
789*4882a593Smuzhiyun { .compatible = "snps,dw-apb-uart" },
790*4882a593Smuzhiyun #ifndef CONFIG_ROCKCHIP_MINI_KERNEL
791*4882a593Smuzhiyun { .compatible = "cavium,octeon-3860-uart" },
792*4882a593Smuzhiyun { .compatible = "marvell,armada-38x-uart" },
793*4882a593Smuzhiyun { .compatible = "renesas,rzn1-uart" },
794*4882a593Smuzhiyun #endif
795*4882a593Smuzhiyun { /* Sentinel */ }
796*4882a593Smuzhiyun };
797*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, dw8250_of_match);
798*4882a593Smuzhiyun
799*4882a593Smuzhiyun static const struct acpi_device_id dw8250_acpi_match[] = {
800*4882a593Smuzhiyun { "INT33C4", 0 },
801*4882a593Smuzhiyun { "INT33C5", 0 },
802*4882a593Smuzhiyun { "INT3434", 0 },
803*4882a593Smuzhiyun { "INT3435", 0 },
804*4882a593Smuzhiyun { "80860F0A", 0 },
805*4882a593Smuzhiyun { "8086228A", 0 },
806*4882a593Smuzhiyun { "APMC0D08", 0},
807*4882a593Smuzhiyun { "AMD0020", 0 },
808*4882a593Smuzhiyun { "AMDI0020", 0 },
809*4882a593Smuzhiyun { "AMDI0022", 0 },
810*4882a593Smuzhiyun { "BRCM2032", 0 },
811*4882a593Smuzhiyun { "HISI0031", 0 },
812*4882a593Smuzhiyun { },
813*4882a593Smuzhiyun };
814*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, dw8250_acpi_match);
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun static struct platform_driver dw8250_platform_driver = {
817*4882a593Smuzhiyun .driver = {
818*4882a593Smuzhiyun .name = "dw-apb-uart",
819*4882a593Smuzhiyun .pm = &dw8250_pm_ops,
820*4882a593Smuzhiyun .of_match_table = dw8250_of_match,
821*4882a593Smuzhiyun .acpi_match_table = dw8250_acpi_match,
822*4882a593Smuzhiyun },
823*4882a593Smuzhiyun .probe = dw8250_probe,
824*4882a593Smuzhiyun .remove = dw8250_remove,
825*4882a593Smuzhiyun };
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun module_platform_driver(dw8250_platform_driver);
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun MODULE_AUTHOR("Jamie Iles");
830*4882a593Smuzhiyun MODULE_LICENSE("GPL");
831*4882a593Smuzhiyun MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver");
832*4882a593Smuzhiyun MODULE_ALIAS("platform:dw-apb-uart");
833