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