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