1 // SPDX-License-Identifier: GPL-2.0+
3 * Synopsys DesignWare 8250 driver.
5 * Copyright 2011 Picochip, Jamie Iles.
6 * Copyright 2013 Intel Corporation
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.
12 #include <linux/device.h>
14 #include <linux/module.h>
15 #include <linux/serial_8250.h>
16 #include <linux/serial_reg.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/acpi.h>
23 #include <linux/clk.h>
24 #include <linux/reset.h>
25 #include <linux/pm_runtime.h>
27 #include <asm/byteorder.h>
31 /* Offsets for the DesignWare specific registers */
32 #define DW_UART_USR 0x1f /* UART Status Register */
33 #define DW_UART_CPR 0xf4 /* Component Parameter Register */
34 #define DW_UART_UCV 0xf8 /* UART Component Version */
36 /* Component Parameter Register bits */
37 #define DW_UART_CPR_ABP_DATA_WIDTH (3 << 0)
38 #define DW_UART_CPR_AFCE_MODE (1 << 4)
39 #define DW_UART_CPR_THRE_MODE (1 << 5)
40 #define DW_UART_CPR_SIR_MODE (1 << 6)
41 #define DW_UART_CPR_SIR_LP_MODE (1 << 7)
42 #define DW_UART_CPR_ADDITIONAL_FEATURES (1 << 8)
43 #define DW_UART_CPR_FIFO_ACCESS (1 << 9)
44 #define DW_UART_CPR_FIFO_STAT (1 << 10)
45 #define DW_UART_CPR_SHADOW (1 << 11)
46 #define DW_UART_CPR_ENCODED_PARMS (1 << 12)
47 #define DW_UART_CPR_DMA_EXTRA (1 << 13)
48 #define DW_UART_CPR_FIFO_MODE (0xff << 16)
49 /* Helper for fifo size calculation */
50 #define DW_UART_CPR_FIFO_SIZE(a) (((a >> 16) & 0xff) * 16)
52 /* DesignWare specific register fields */
53 #define DW_UART_MCR_SIRE BIT(6)
62 struct reset_control
*rst
;
63 struct uart_8250_dma dma
;
65 unsigned int skip_autocfg
:1;
66 unsigned int uart_16550_compatible
:1;
69 static inline int dw8250_modify_msr(struct uart_port
*p
, int offset
, int value
)
71 struct dw8250_data
*d
= p
->private_data
;
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
;
82 static void dw8250_force_idle(struct uart_port
*p
)
84 struct uart_8250_port
*up
= up_to_u8250p(p
);
86 serial8250_clear_and_reinit_fifos(up
);
87 (void)p
->serial_in(p
, UART_RX
);
90 static void dw8250_check_lcr(struct uart_port
*p
, int value
)
92 void __iomem
*offset
= p
->membase
+ (UART_LCR
<< p
->regshift
);
95 /* Make sure LCR write wasn't ignored */
97 unsigned int lcr
= p
->serial_in(p
, UART_LCR
);
99 if ((value
& ~UART_LCR_SPAR
) == (lcr
& ~UART_LCR_SPAR
))
102 dw8250_force_idle(p
);
105 if (p
->type
== PORT_OCTEON
)
106 __raw_writeq(value
& 0xff, offset
);
109 if (p
->iotype
== UPIO_MEM32
)
110 writel(value
, offset
);
111 else if (p
->iotype
== UPIO_MEM32BE
)
112 iowrite32be(value
, offset
);
114 writeb(value
, offset
);
117 * FIXME: this deadlocks if port->lock is already held
118 * dev_err(p->dev, "Couldn't set LCR to %d\n", value);
122 static void dw8250_serial_out(struct uart_port
*p
, int offset
, int value
)
124 struct dw8250_data
*d
= p
->private_data
;
126 writeb(value
, p
->membase
+ (offset
<< p
->regshift
));
128 if (offset
== UART_LCR
&& !d
->uart_16550_compatible
)
129 dw8250_check_lcr(p
, value
);
132 static unsigned int dw8250_serial_in(struct uart_port
*p
, int offset
)
134 unsigned int value
= readb(p
->membase
+ (offset
<< p
->regshift
));
136 return dw8250_modify_msr(p
, offset
, value
);
140 static unsigned int dw8250_serial_inq(struct uart_port
*p
, int offset
)
144 value
= (u8
)__raw_readq(p
->membase
+ (offset
<< p
->regshift
));
146 return dw8250_modify_msr(p
, offset
, value
);
149 static void dw8250_serial_outq(struct uart_port
*p
, int offset
, int value
)
151 struct dw8250_data
*d
= p
->private_data
;
154 __raw_writeq(value
, p
->membase
+ (offset
<< p
->regshift
));
155 /* Read back to ensure register write ordering. */
156 __raw_readq(p
->membase
+ (UART_LCR
<< p
->regshift
));
158 if (offset
== UART_LCR
&& !d
->uart_16550_compatible
)
159 dw8250_check_lcr(p
, value
);
161 #endif /* CONFIG_64BIT */
163 static void dw8250_serial_out32(struct uart_port
*p
, int offset
, int value
)
165 struct dw8250_data
*d
= p
->private_data
;
167 writel(value
, p
->membase
+ (offset
<< p
->regshift
));
169 if (offset
== UART_LCR
&& !d
->uart_16550_compatible
)
170 dw8250_check_lcr(p
, value
);
173 static unsigned int dw8250_serial_in32(struct uart_port
*p
, int offset
)
175 unsigned int value
= readl(p
->membase
+ (offset
<< p
->regshift
));
177 return dw8250_modify_msr(p
, offset
, value
);
180 static void dw8250_serial_out32be(struct uart_port
*p
, int offset
, int value
)
182 struct dw8250_data
*d
= p
->private_data
;
184 iowrite32be(value
, p
->membase
+ (offset
<< p
->regshift
));
186 if (offset
== UART_LCR
&& !d
->uart_16550_compatible
)
187 dw8250_check_lcr(p
, value
);
190 static unsigned int dw8250_serial_in32be(struct uart_port
*p
, int offset
)
192 unsigned int value
= ioread32be(p
->membase
+ (offset
<< p
->regshift
));
194 return dw8250_modify_msr(p
, offset
, value
);
198 static int dw8250_handle_irq(struct uart_port
*p
)
200 struct uart_8250_port
*up
= up_to_u8250p(p
);
201 struct dw8250_data
*d
= p
->private_data
;
202 unsigned int iir
= p
->serial_in(p
, UART_IIR
);
207 * There are ways to get Designware-based UARTs into a state where
208 * they are asserting UART_IIR_RX_TIMEOUT but there is no actual
209 * data available. If we see such a case then we'll do a bogus
210 * read. If we don't do this then the "RX TIMEOUT" interrupt will
213 * This problem has only been observed so far when not in DMA mode
214 * so we limit the workaround only to non-DMA mode.
216 if (!up
->dma
&& ((iir
& 0x3f) == UART_IIR_RX_TIMEOUT
)) {
217 spin_lock_irqsave(&p
->lock
, flags
);
218 status
= p
->serial_in(p
, UART_LSR
);
220 if (!(status
& (UART_LSR_DR
| UART_LSR_BI
)))
221 (void) p
->serial_in(p
, UART_RX
);
223 spin_unlock_irqrestore(&p
->lock
, flags
);
226 if (serial8250_handle_irq(p
, iir
))
229 if ((iir
& UART_IIR_BUSY
) == UART_IIR_BUSY
) {
231 (void)p
->serial_in(p
, d
->usr_reg
);
240 dw8250_do_pm(struct uart_port
*port
, unsigned int state
, unsigned int old
)
243 pm_runtime_get_sync(port
->dev
);
245 serial8250_do_pm(port
, state
, old
);
248 pm_runtime_put_sync_suspend(port
->dev
);
251 static void dw8250_set_termios(struct uart_port
*p
, struct ktermios
*termios
,
252 struct ktermios
*old
)
254 unsigned int baud
= tty_termios_baud_rate(termios
);
255 struct dw8250_data
*d
= p
->private_data
;
259 if (IS_ERR(d
->clk
) || !old
)
262 clk_disable_unprepare(d
->clk
);
263 rate
= clk_round_rate(d
->clk
, baud
* 16);
269 ret
= clk_set_rate(d
->clk
, rate
);
270 clk_prepare_enable(d
->clk
);
276 p
->status
&= ~UPSTAT_AUTOCTS
;
277 if (termios
->c_cflag
& CRTSCTS
)
278 p
->status
|= UPSTAT_AUTOCTS
;
280 serial8250_do_set_termios(p
, termios
, old
);
283 static void dw8250_set_ldisc(struct uart_port
*p
, struct ktermios
*termios
)
285 struct uart_8250_port
*up
= up_to_u8250p(p
);
286 unsigned int mcr
= p
->serial_in(p
, UART_MCR
);
288 if (up
->capabilities
& UART_CAP_IRDA
) {
289 if (termios
->c_line
== N_IRDA
)
290 mcr
|= DW_UART_MCR_SIRE
;
292 mcr
&= ~DW_UART_MCR_SIRE
;
294 p
->serial_out(p
, UART_MCR
, mcr
);
296 serial8250_do_set_ldisc(p
, termios
);
300 * dw8250_fallback_dma_filter will prevent the UART from getting just any free
301 * channel on platforms that have DMA engines, but don't have any channels
302 * assigned to the UART.
304 * REVISIT: This is a work around for limitation in the DMA Engine API. Once the
305 * core problem is fixed, this function is no longer needed.
307 static bool dw8250_fallback_dma_filter(struct dma_chan
*chan
, void *param
)
312 static bool dw8250_idma_filter(struct dma_chan
*chan
, void *param
)
314 return param
== chan
->device
->dev
->parent
;
317 static void dw8250_quirks(struct uart_port
*p
, struct dw8250_data
*data
)
319 if (p
->dev
->of_node
) {
320 struct device_node
*np
= p
->dev
->of_node
;
323 /* get index of serial line, if found in DT aliases */
324 id
= of_alias_get_id(np
, "serial");
328 if (of_device_is_compatible(np
, "cavium,octeon-3860-uart")) {
329 p
->serial_in
= dw8250_serial_inq
;
330 p
->serial_out
= dw8250_serial_outq
;
331 p
->flags
= UPF_SKIP_TEST
| UPF_SHARE_IRQ
| UPF_FIXED_TYPE
;
332 p
->type
= PORT_OCTEON
;
333 data
->usr_reg
= 0x27;
334 data
->skip_autocfg
= true;
337 if (of_device_is_big_endian(p
->dev
->of_node
)) {
338 p
->iotype
= UPIO_MEM32BE
;
339 p
->serial_in
= dw8250_serial_in32be
;
340 p
->serial_out
= dw8250_serial_out32be
;
342 } else if (has_acpi_companion(p
->dev
)) {
343 const struct acpi_device_id
*id
;
345 id
= acpi_match_device(p
->dev
->driver
->acpi_match_table
,
347 if (id
&& !strcmp(id
->id
, "APMC0D08")) {
348 p
->iotype
= UPIO_MEM32
;
350 p
->serial_in
= dw8250_serial_in32
;
351 data
->uart_16550_compatible
= true;
355 /* Platforms with iDMA */
356 if (platform_get_resource_byname(to_platform_device(p
->dev
),
357 IORESOURCE_MEM
, "lpss_priv")) {
358 data
->dma
.rx_param
= p
->dev
->parent
;
359 data
->dma
.tx_param
= p
->dev
->parent
;
360 data
->dma
.fn
= dw8250_idma_filter
;
364 static void dw8250_setup_port(struct uart_port
*p
)
366 struct uart_8250_port
*up
= up_to_u8250p(p
);
370 * If the Component Version Register returns zero, we know that
371 * ADDITIONAL_FEATURES are not enabled. No need to go any further.
373 if (p
->iotype
== UPIO_MEM32BE
)
374 reg
= ioread32be(p
->membase
+ DW_UART_UCV
);
376 reg
= readl(p
->membase
+ DW_UART_UCV
);
380 dev_dbg(p
->dev
, "Designware UART version %c.%c%c\n",
381 (reg
>> 24) & 0xff, (reg
>> 16) & 0xff, (reg
>> 8) & 0xff);
383 if (p
->iotype
== UPIO_MEM32BE
)
384 reg
= ioread32be(p
->membase
+ DW_UART_CPR
);
386 reg
= readl(p
->membase
+ DW_UART_CPR
);
390 /* Select the type based on fifo */
391 if (reg
& DW_UART_CPR_FIFO_MODE
) {
392 p
->type
= PORT_16550A
;
393 p
->flags
|= UPF_FIXED_TYPE
;
394 p
->fifosize
= DW_UART_CPR_FIFO_SIZE(reg
);
395 up
->capabilities
= UART_CAP_FIFO
;
398 if (reg
& DW_UART_CPR_AFCE_MODE
)
399 up
->capabilities
|= UART_CAP_AFE
;
401 if (reg
& DW_UART_CPR_SIR_MODE
)
402 up
->capabilities
|= UART_CAP_IRDA
;
405 static int dw8250_probe(struct platform_device
*pdev
)
407 struct uart_8250_port uart
= {};
408 struct resource
*regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
409 int irq
= platform_get_irq(pdev
, 0);
410 struct uart_port
*p
= &uart
.port
;
411 struct device
*dev
= &pdev
->dev
;
412 struct dw8250_data
*data
;
417 dev_err(dev
, "no registers defined\n");
422 if (irq
!= -EPROBE_DEFER
)
423 dev_err(dev
, "cannot get irq\n");
427 spin_lock_init(&p
->lock
);
428 p
->mapbase
= regs
->start
;
430 p
->handle_irq
= dw8250_handle_irq
;
431 p
->pm
= dw8250_do_pm
;
433 p
->flags
= UPF_SHARE_IRQ
| UPF_FIXED_PORT
;
435 p
->iotype
= UPIO_MEM
;
436 p
->serial_in
= dw8250_serial_in
;
437 p
->serial_out
= dw8250_serial_out
;
438 p
->set_ldisc
= dw8250_set_ldisc
;
439 p
->set_termios
= dw8250_set_termios
;
441 p
->membase
= devm_ioremap(dev
, regs
->start
, resource_size(regs
));
445 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
449 data
->dma
.fn
= dw8250_fallback_dma_filter
;
450 data
->usr_reg
= DW_UART_USR
;
451 p
->private_data
= data
;
453 data
->uart_16550_compatible
= device_property_read_bool(dev
,
454 "snps,uart-16550-compatible");
456 err
= device_property_read_u32(dev
, "reg-shift", &val
);
460 err
= device_property_read_u32(dev
, "reg-io-width", &val
);
461 if (!err
&& val
== 4) {
462 p
->iotype
= UPIO_MEM32
;
463 p
->serial_in
= dw8250_serial_in32
;
464 p
->serial_out
= dw8250_serial_out32
;
467 if (device_property_read_bool(dev
, "dcd-override")) {
468 /* Always report DCD as active */
469 data
->msr_mask_on
|= UART_MSR_DCD
;
470 data
->msr_mask_off
|= UART_MSR_DDCD
;
473 if (device_property_read_bool(dev
, "dsr-override")) {
474 /* Always report DSR as active */
475 data
->msr_mask_on
|= UART_MSR_DSR
;
476 data
->msr_mask_off
|= UART_MSR_DDSR
;
479 if (device_property_read_bool(dev
, "cts-override")) {
480 /* Always report CTS as active */
481 data
->msr_mask_on
|= UART_MSR_CTS
;
482 data
->msr_mask_off
|= UART_MSR_DCTS
;
485 if (device_property_read_bool(dev
, "ri-override")) {
486 /* Always report Ring indicator as inactive */
487 data
->msr_mask_off
|= UART_MSR_RI
;
488 data
->msr_mask_off
|= UART_MSR_TERI
;
491 /* Always ask for fixed clock rate from a property. */
492 device_property_read_u32(dev
, "clock-frequency", &p
->uartclk
);
494 /* If there is separate baudclk, get the rate from it. */
495 data
->clk
= devm_clk_get(dev
, "baudclk");
496 if (IS_ERR(data
->clk
) && PTR_ERR(data
->clk
) != -EPROBE_DEFER
)
497 data
->clk
= devm_clk_get(dev
, NULL
);
498 if (IS_ERR(data
->clk
) && PTR_ERR(data
->clk
) == -EPROBE_DEFER
)
499 return -EPROBE_DEFER
;
500 if (!IS_ERR_OR_NULL(data
->clk
)) {
501 err
= clk_prepare_enable(data
->clk
);
503 dev_warn(dev
, "could not enable optional baudclk: %d\n",
506 p
->uartclk
= clk_get_rate(data
->clk
);
509 /* If no clock rate is defined, fail. */
511 dev_err(dev
, "clock rate not defined\n");
516 data
->pclk
= devm_clk_get(dev
, "apb_pclk");
517 if (IS_ERR(data
->pclk
) && PTR_ERR(data
->pclk
) == -EPROBE_DEFER
) {
521 if (!IS_ERR(data
->pclk
)) {
522 err
= clk_prepare_enable(data
->pclk
);
524 dev_err(dev
, "could not enable apb_pclk\n");
529 data
->rst
= devm_reset_control_get_optional_exclusive(dev
, NULL
);
530 if (IS_ERR(data
->rst
)) {
531 err
= PTR_ERR(data
->rst
);
534 reset_control_deassert(data
->rst
);
536 dw8250_quirks(p
, data
);
538 /* If the Busy Functionality is not implemented, don't handle it */
539 if (data
->uart_16550_compatible
)
540 p
->handle_irq
= NULL
;
542 if (!data
->skip_autocfg
)
543 dw8250_setup_port(p
);
545 /* If we have a valid fifosize, try hooking up DMA */
547 data
->dma
.rxconf
.src_maxburst
= p
->fifosize
/ 4;
548 data
->dma
.txconf
.dst_maxburst
= p
->fifosize
/ 4;
549 uart
.dma
= &data
->dma
;
552 data
->line
= serial8250_register_8250_port(&uart
);
553 if (data
->line
< 0) {
558 platform_set_drvdata(pdev
, data
);
560 pm_runtime_set_active(dev
);
561 pm_runtime_enable(dev
);
566 reset_control_assert(data
->rst
);
569 if (!IS_ERR(data
->pclk
))
570 clk_disable_unprepare(data
->pclk
);
573 if (!IS_ERR(data
->clk
))
574 clk_disable_unprepare(data
->clk
);
579 static int dw8250_remove(struct platform_device
*pdev
)
581 struct dw8250_data
*data
= platform_get_drvdata(pdev
);
583 pm_runtime_get_sync(&pdev
->dev
);
585 serial8250_unregister_port(data
->line
);
587 reset_control_assert(data
->rst
);
589 if (!IS_ERR(data
->pclk
))
590 clk_disable_unprepare(data
->pclk
);
592 if (!IS_ERR(data
->clk
))
593 clk_disable_unprepare(data
->clk
);
595 pm_runtime_disable(&pdev
->dev
);
596 pm_runtime_put_noidle(&pdev
->dev
);
601 #ifdef CONFIG_PM_SLEEP
602 static int dw8250_suspend(struct device
*dev
)
604 struct dw8250_data
*data
= dev_get_drvdata(dev
);
606 serial8250_suspend_port(data
->line
);
611 static int dw8250_resume(struct device
*dev
)
613 struct dw8250_data
*data
= dev_get_drvdata(dev
);
615 serial8250_resume_port(data
->line
);
619 #endif /* CONFIG_PM_SLEEP */
622 static int dw8250_runtime_suspend(struct device
*dev
)
624 struct dw8250_data
*data
= dev_get_drvdata(dev
);
626 if (!IS_ERR(data
->clk
))
627 clk_disable_unprepare(data
->clk
);
629 if (!IS_ERR(data
->pclk
))
630 clk_disable_unprepare(data
->pclk
);
635 static int dw8250_runtime_resume(struct device
*dev
)
637 struct dw8250_data
*data
= dev_get_drvdata(dev
);
639 if (!IS_ERR(data
->pclk
))
640 clk_prepare_enable(data
->pclk
);
642 if (!IS_ERR(data
->clk
))
643 clk_prepare_enable(data
->clk
);
649 static const struct dev_pm_ops dw8250_pm_ops
= {
650 SET_SYSTEM_SLEEP_PM_OPS(dw8250_suspend
, dw8250_resume
)
651 SET_RUNTIME_PM_OPS(dw8250_runtime_suspend
, dw8250_runtime_resume
, NULL
)
654 static const struct of_device_id dw8250_of_match
[] = {
655 { .compatible
= "snps,dw-apb-uart" },
656 { .compatible
= "cavium,octeon-3860-uart" },
659 MODULE_DEVICE_TABLE(of
, dw8250_of_match
);
661 static const struct acpi_device_id dw8250_acpi_match
[] = {
674 MODULE_DEVICE_TABLE(acpi
, dw8250_acpi_match
);
676 static struct platform_driver dw8250_platform_driver
= {
678 .name
= "dw-apb-uart",
679 .pm
= &dw8250_pm_ops
,
680 .of_match_table
= dw8250_of_match
,
681 .acpi_match_table
= ACPI_PTR(dw8250_acpi_match
),
683 .probe
= dw8250_probe
,
684 .remove
= dw8250_remove
,
687 module_platform_driver(dw8250_platform_driver
);
689 MODULE_AUTHOR("Jamie Iles");
690 MODULE_LICENSE("GPL");
691 MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver");
692 MODULE_ALIAS("platform:dw-apb-uart");