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