2 * Synopsys DesignWare 8250 driver.
4 * Copyright 2011 Picochip, Jamie Iles.
5 * Copyright 2013 Intel Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * The Synopsys DesignWare 8250 has an extra feature whereby it detects if the
13 * LCR is written whilst busy. If it is, then a busy detect interrupt is
14 * raised, the LCR needs to be rewritten and the uart status register read.
16 #include <linux/device.h>
17 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/serial_8250.h>
21 #include <linux/serial_core.h>
22 #include <linux/serial_reg.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_platform.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 #include <linux/acpi.h>
29 #include <linux/clk.h>
30 #include <linux/pm_runtime.h>
32 #include <asm/byteorder.h>
36 /* Offsets for the DesignWare specific registers */
37 #define DW_UART_USR 0x1f /* UART Status Register */
38 #define DW_UART_CPR 0xf4 /* Component Parameter Register */
39 #define DW_UART_UCV 0xf8 /* UART Component Version */
41 /* Component Parameter Register bits */
42 #define DW_UART_CPR_ABP_DATA_WIDTH (3 << 0)
43 #define DW_UART_CPR_AFCE_MODE (1 << 4)
44 #define DW_UART_CPR_THRE_MODE (1 << 5)
45 #define DW_UART_CPR_SIR_MODE (1 << 6)
46 #define DW_UART_CPR_SIR_LP_MODE (1 << 7)
47 #define DW_UART_CPR_ADDITIONAL_FEATURES (1 << 8)
48 #define DW_UART_CPR_FIFO_ACCESS (1 << 9)
49 #define DW_UART_CPR_FIFO_STAT (1 << 10)
50 #define DW_UART_CPR_SHADOW (1 << 11)
51 #define DW_UART_CPR_ENCODED_PARMS (1 << 12)
52 #define DW_UART_CPR_DMA_EXTRA (1 << 13)
53 #define DW_UART_CPR_FIFO_MODE (0xff << 16)
54 /* Helper for fifo size calculation */
55 #define DW_UART_CPR_FIFO_SIZE(a) (((a >> 16) & 0xff) * 16)
65 static inline int dw8250_modify_msr(struct uart_port
*p
, int offset
, int value
)
67 struct dw8250_data
*d
= p
->private_data
;
69 /* If reading MSR, report CTS asserted when auto-CTS/RTS enabled */
70 if (offset
== UART_MSR
&& d
->last_mcr
& UART_MCR_AFE
) {
71 value
|= UART_MSR_CTS
;
72 value
&= ~UART_MSR_DCTS
;
78 static void dw8250_force_idle(struct uart_port
*p
)
80 serial8250_clear_and_reinit_fifos(container_of
81 (p
, struct uart_8250_port
, port
));
82 (void)p
->serial_in(p
, UART_RX
);
85 static void dw8250_serial_out(struct uart_port
*p
, int offset
, int value
)
87 struct dw8250_data
*d
= p
->private_data
;
89 if (offset
== UART_MCR
)
92 writeb(value
, p
->membase
+ (offset
<< p
->regshift
));
94 /* Make sure LCR write wasn't ignored */
95 if (offset
== UART_LCR
) {
98 unsigned int lcr
= p
->serial_in(p
, UART_LCR
);
99 if ((value
& ~UART_LCR_SPAR
) == (lcr
& ~UART_LCR_SPAR
))
101 dw8250_force_idle(p
);
102 writeb(value
, p
->membase
+ (UART_LCR
<< p
->regshift
));
104 dev_err(p
->dev
, "Couldn't set LCR to %d\n", value
);
108 static unsigned int dw8250_serial_in(struct uart_port
*p
, int offset
)
110 unsigned int value
= readb(p
->membase
+ (offset
<< p
->regshift
));
112 return dw8250_modify_msr(p
, offset
, value
);
115 /* Read Back (rb) version to ensure register access ording. */
116 static void dw8250_serial_out_rb(struct uart_port
*p
, int offset
, int value
)
118 dw8250_serial_out(p
, offset
, value
);
119 dw8250_serial_in(p
, UART_LCR
);
122 static void dw8250_serial_out32(struct uart_port
*p
, int offset
, int value
)
124 struct dw8250_data
*d
= p
->private_data
;
126 if (offset
== UART_MCR
)
129 writel(value
, p
->membase
+ (offset
<< p
->regshift
));
131 /* Make sure LCR write wasn't ignored */
132 if (offset
== UART_LCR
) {
135 unsigned int lcr
= p
->serial_in(p
, UART_LCR
);
136 if ((value
& ~UART_LCR_SPAR
) == (lcr
& ~UART_LCR_SPAR
))
138 dw8250_force_idle(p
);
139 writel(value
, p
->membase
+ (UART_LCR
<< p
->regshift
));
141 dev_err(p
->dev
, "Couldn't set LCR to %d\n", value
);
145 static unsigned int dw8250_serial_in32(struct uart_port
*p
, int offset
)
147 unsigned int value
= readl(p
->membase
+ (offset
<< p
->regshift
));
149 return dw8250_modify_msr(p
, offset
, value
);
152 static int dw8250_handle_irq(struct uart_port
*p
)
154 struct dw8250_data
*d
= p
->private_data
;
155 unsigned int iir
= p
->serial_in(p
, UART_IIR
);
157 if (serial8250_handle_irq(p
, iir
)) {
159 } else if ((iir
& UART_IIR_BUSY
) == UART_IIR_BUSY
) {
161 (void)p
->serial_in(p
, d
->usr_reg
);
170 dw8250_do_pm(struct uart_port
*port
, unsigned int state
, unsigned int old
)
173 pm_runtime_get_sync(port
->dev
);
175 serial8250_do_pm(port
, state
, old
);
178 pm_runtime_put_sync_suspend(port
->dev
);
181 static void dw8250_setup_port(struct uart_8250_port
*up
)
183 struct uart_port
*p
= &up
->port
;
184 u32 reg
= readl(p
->membase
+ DW_UART_UCV
);
187 * If the Component Version Register returns zero, we know that
188 * ADDITIONAL_FEATURES are not enabled. No need to go any further.
193 dev_dbg_ratelimited(p
->dev
, "Designware UART version %c.%c%c\n",
194 (reg
>> 24) & 0xff, (reg
>> 16) & 0xff, (reg
>> 8) & 0xff);
196 reg
= readl(p
->membase
+ DW_UART_CPR
);
200 /* Select the type based on fifo */
201 if (reg
& DW_UART_CPR_FIFO_MODE
) {
202 p
->type
= PORT_16550A
;
203 p
->flags
|= UPF_FIXED_TYPE
;
204 p
->fifosize
= DW_UART_CPR_FIFO_SIZE(reg
);
205 up
->tx_loadsz
= p
->fifosize
;
206 up
->capabilities
= UART_CAP_FIFO
;
209 if (reg
& DW_UART_CPR_AFCE_MODE
)
210 up
->capabilities
|= UART_CAP_AFE
;
213 static int dw8250_probe_of(struct uart_port
*p
,
214 struct dw8250_data
*data
)
216 struct device_node
*np
= p
->dev
->of_node
;
220 if (of_device_is_compatible(np
, "cavium,octeon-3860-uart")) {
223 * Low order bits of these 64-bit registers, when
224 * accessed as a byte, are 7 bytes further down in the
225 * address space in big endian mode.
229 p
->serial_out
= dw8250_serial_out_rb
;
230 p
->flags
= ASYNC_SKIP_TEST
| UPF_SHARE_IRQ
| UPF_FIXED_TYPE
;
231 p
->type
= PORT_OCTEON
;
232 data
->usr_reg
= 0x27;
234 } else if (!of_property_read_u32(np
, "reg-io-width", &val
)) {
239 p
->iotype
= UPIO_MEM32
;
240 p
->serial_in
= dw8250_serial_in32
;
241 p
->serial_out
= dw8250_serial_out32
;
244 dev_err(p
->dev
, "unsupported reg-io-width (%u)\n", val
);
249 dw8250_setup_port(container_of(p
, struct uart_8250_port
, port
));
251 if (!of_property_read_u32(np
, "reg-shift", &val
))
254 /* clock got configured through clk api, all done */
258 /* try to find out clock frequency from DT as fallback */
259 if (of_property_read_u32(np
, "clock-frequency", &val
)) {
260 dev_err(p
->dev
, "clk or clock-frequency not defined\n");
269 static int dw8250_probe_acpi(struct uart_8250_port
*up
)
271 const struct acpi_device_id
*id
;
272 struct uart_port
*p
= &up
->port
;
274 dw8250_setup_port(up
);
276 id
= acpi_match_device(p
->dev
->driver
->acpi_match_table
, p
->dev
);
280 p
->iotype
= UPIO_MEM32
;
281 p
->serial_in
= dw8250_serial_in32
;
282 p
->serial_out
= dw8250_serial_out32
;
286 p
->uartclk
= (unsigned int)id
->driver_data
;
288 up
->dma
= devm_kzalloc(p
->dev
, sizeof(*up
->dma
), GFP_KERNEL
);
292 up
->dma
->rxconf
.src_maxburst
= p
->fifosize
/ 4;
293 up
->dma
->txconf
.dst_maxburst
= p
->fifosize
/ 4;
298 static inline int dw8250_probe_acpi(struct uart_8250_port
*up
)
302 #endif /* CONFIG_ACPI */
304 static int dw8250_probe(struct platform_device
*pdev
)
306 struct uart_8250_port uart
= {};
307 struct resource
*regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
308 struct resource
*irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
309 struct dw8250_data
*data
;
313 dev_err(&pdev
->dev
, "no registers/irq defined\n");
317 spin_lock_init(&uart
.port
.lock
);
318 uart
.port
.mapbase
= regs
->start
;
319 uart
.port
.irq
= irq
->start
;
320 uart
.port
.handle_irq
= dw8250_handle_irq
;
321 uart
.port
.pm
= dw8250_do_pm
;
322 uart
.port
.type
= PORT_8250
;
323 uart
.port
.flags
= UPF_SHARE_IRQ
| UPF_BOOT_AUTOCONF
| UPF_FIXED_PORT
;
324 uart
.port
.dev
= &pdev
->dev
;
326 uart
.port
.membase
= devm_ioremap(&pdev
->dev
, regs
->start
,
327 resource_size(regs
));
328 if (!uart
.port
.membase
)
331 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
335 data
->usr_reg
= DW_UART_USR
;
336 data
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
337 if (!IS_ERR(data
->clk
)) {
338 clk_prepare_enable(data
->clk
);
339 uart
.port
.uartclk
= clk_get_rate(data
->clk
);
342 uart
.port
.iotype
= UPIO_MEM
;
343 uart
.port
.serial_in
= dw8250_serial_in
;
344 uart
.port
.serial_out
= dw8250_serial_out
;
345 uart
.port
.private_data
= data
;
347 if (pdev
->dev
.of_node
) {
348 err
= dw8250_probe_of(&uart
.port
, data
);
351 } else if (ACPI_HANDLE(&pdev
->dev
)) {
352 err
= dw8250_probe_acpi(&uart
);
359 data
->line
= serial8250_register_8250_port(&uart
);
363 platform_set_drvdata(pdev
, data
);
365 pm_runtime_set_active(&pdev
->dev
);
366 pm_runtime_enable(&pdev
->dev
);
371 static int dw8250_remove(struct platform_device
*pdev
)
373 struct dw8250_data
*data
= platform_get_drvdata(pdev
);
375 pm_runtime_get_sync(&pdev
->dev
);
377 serial8250_unregister_port(data
->line
);
379 if (!IS_ERR(data
->clk
))
380 clk_disable_unprepare(data
->clk
);
382 pm_runtime_disable(&pdev
->dev
);
383 pm_runtime_put_noidle(&pdev
->dev
);
389 static int dw8250_suspend(struct device
*dev
)
391 struct dw8250_data
*data
= dev_get_drvdata(dev
);
393 serial8250_suspend_port(data
->line
);
398 static int dw8250_resume(struct device
*dev
)
400 struct dw8250_data
*data
= dev_get_drvdata(dev
);
402 serial8250_resume_port(data
->line
);
406 #endif /* CONFIG_PM */
408 #ifdef CONFIG_PM_RUNTIME
409 static int dw8250_runtime_suspend(struct device
*dev
)
411 struct dw8250_data
*data
= dev_get_drvdata(dev
);
413 if (!IS_ERR(data
->clk
))
414 clk_disable_unprepare(data
->clk
);
419 static int dw8250_runtime_resume(struct device
*dev
)
421 struct dw8250_data
*data
= dev_get_drvdata(dev
);
423 if (!IS_ERR(data
->clk
))
424 clk_prepare_enable(data
->clk
);
430 static const struct dev_pm_ops dw8250_pm_ops
= {
431 SET_SYSTEM_SLEEP_PM_OPS(dw8250_suspend
, dw8250_resume
)
432 SET_RUNTIME_PM_OPS(dw8250_runtime_suspend
, dw8250_runtime_resume
, NULL
)
435 static const struct of_device_id dw8250_of_match
[] = {
436 { .compatible
= "snps,dw-apb-uart" },
437 { .compatible
= "cavium,octeon-3860-uart" },
440 MODULE_DEVICE_TABLE(of
, dw8250_of_match
);
442 static const struct acpi_device_id dw8250_acpi_match
[] = {
450 MODULE_DEVICE_TABLE(acpi
, dw8250_acpi_match
);
452 static struct platform_driver dw8250_platform_driver
= {
454 .name
= "dw-apb-uart",
455 .owner
= THIS_MODULE
,
456 .pm
= &dw8250_pm_ops
,
457 .of_match_table
= dw8250_of_match
,
458 .acpi_match_table
= ACPI_PTR(dw8250_acpi_match
),
460 .probe
= dw8250_probe
,
461 .remove
= dw8250_remove
,
464 module_platform_driver(dw8250_platform_driver
);
466 MODULE_AUTHOR("Jamie Iles");
467 MODULE_LICENSE("GPL");
468 MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver");