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>
34 /* Offsets for the DesignWare specific registers */
35 #define DW_UART_USR 0x1f /* UART Status Register */
36 #define DW_UART_CPR 0xf4 /* Component Parameter Register */
37 #define DW_UART_UCV 0xf8 /* UART Component Version */
39 /* Component Parameter Register bits */
40 #define DW_UART_CPR_ABP_DATA_WIDTH (3 << 0)
41 #define DW_UART_CPR_AFCE_MODE (1 << 4)
42 #define DW_UART_CPR_THRE_MODE (1 << 5)
43 #define DW_UART_CPR_SIR_MODE (1 << 6)
44 #define DW_UART_CPR_SIR_LP_MODE (1 << 7)
45 #define DW_UART_CPR_ADDITIONAL_FEATURES (1 << 8)
46 #define DW_UART_CPR_FIFO_ACCESS (1 << 9)
47 #define DW_UART_CPR_FIFO_STAT (1 << 10)
48 #define DW_UART_CPR_SHADOW (1 << 11)
49 #define DW_UART_CPR_ENCODED_PARMS (1 << 12)
50 #define DW_UART_CPR_DMA_EXTRA (1 << 13)
51 #define DW_UART_CPR_FIFO_MODE (0xff << 16)
52 /* Helper for fifo size calculation */
53 #define DW_UART_CPR_FIFO_SIZE(a) (((a >> 16) & 0xff) * 16)
62 static void dw8250_serial_out(struct uart_port *p, int offset, int value)
64 struct dw8250_data *d = p->private_data;
66 if (offset == UART_LCR)
69 offset <<= p->regshift;
70 writeb(value, p->membase + offset);
73 static unsigned int dw8250_serial_in(struct uart_port *p, int offset)
75 offset <<= p->regshift;
77 return readb(p->membase + offset);
80 static void dw8250_serial_out32(struct uart_port *p, int offset, int value)
82 struct dw8250_data *d = p->private_data;
84 if (offset == UART_LCR)
87 offset <<= p->regshift;
88 writel(value, p->membase + offset);
91 static unsigned int dw8250_serial_in32(struct uart_port *p, int offset)
93 offset <<= p->regshift;
95 return readl(p->membase + offset);
98 static int dw8250_handle_irq(struct uart_port *p)
100 struct dw8250_data *d = p->private_data;
101 unsigned int iir = p->serial_in(p, UART_IIR);
103 if (serial8250_handle_irq(p, iir)) {
105 } else if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
106 /* Clear the USR and write the LCR again. */
107 (void)p->serial_in(p, DW_UART_USR);
108 p->serial_out(p, UART_LCR, d->last_lcr);
117 dw8250_do_pm(struct uart_port *port, unsigned int state, unsigned int old)
120 pm_runtime_get_sync(port->dev);
122 serial8250_do_pm(port, state, old);
125 pm_runtime_put_sync_suspend(port->dev);
128 static int dw8250_probe_of(struct uart_port *p)
130 struct device_node *np = p->dev->of_node;
133 if (!of_property_read_u32(np, "reg-io-width", &val)) {
138 p->iotype = UPIO_MEM32;
139 p->serial_in = dw8250_serial_in32;
140 p->serial_out = dw8250_serial_out32;
143 dev_err(p->dev, "unsupported reg-io-width (%u)\n", val);
148 if (!of_property_read_u32(np, "reg-shift", &val))
151 /* clock got configured through clk api, all done */
155 /* try to find out clock frequency from DT as fallback */
156 if (of_property_read_u32(np, "clock-frequency", &val)) {
157 dev_err(p->dev, "clk or clock-frequency not defined\n");
166 static int dw8250_probe_acpi(struct uart_8250_port *up)
168 const struct acpi_device_id *id;
169 struct uart_port *p = &up->port;
171 id = acpi_match_device(p->dev->driver->acpi_match_table, p->dev);
175 p->iotype = UPIO_MEM32;
176 p->serial_in = dw8250_serial_in32;
177 p->serial_out = dw8250_serial_out32;
181 p->uartclk = (unsigned int)id->driver_data;
183 up->dma = devm_kzalloc(p->dev, sizeof(*up->dma), GFP_KERNEL);
187 up->dma->rxconf.src_maxburst = p->fifosize / 4;
188 up->dma->txconf.dst_maxburst = p->fifosize / 4;
193 static inline int dw8250_probe_acpi(struct uart_8250_port *up)
197 #endif /* CONFIG_ACPI */
199 static void dw8250_setup_port(struct uart_8250_port *up)
201 struct uart_port *p = &up->port;
202 u32 reg = readl(p->membase + DW_UART_UCV);
205 * If the Component Version Register returns zero, we know that
206 * ADDITIONAL_FEATURES are not enabled. No need to go any further.
211 dev_dbg_ratelimited(p->dev, "Designware UART version %c.%c%c\n",
212 (reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff);
214 reg = readl(p->membase + DW_UART_CPR);
218 /* Select the type based on fifo */
219 if (reg & DW_UART_CPR_FIFO_MODE) {
220 p->type = PORT_16550A;
221 p->flags |= UPF_FIXED_TYPE;
222 p->fifosize = DW_UART_CPR_FIFO_SIZE(reg);
223 up->tx_loadsz = p->fifosize;
224 up->capabilities = UART_CAP_FIFO;
227 if (reg & DW_UART_CPR_AFCE_MODE)
228 up->capabilities |= UART_CAP_AFE;
231 static int dw8250_probe(struct platform_device *pdev)
233 struct uart_8250_port uart = {};
234 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
235 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
236 struct dw8250_data *data;
240 dev_err(&pdev->dev, "no registers/irq defined\n");
244 spin_lock_init(&uart.port.lock);
245 uart.port.mapbase = regs->start;
246 uart.port.irq = irq->start;
247 uart.port.handle_irq = dw8250_handle_irq;
248 uart.port.pm = dw8250_do_pm;
249 uart.port.type = PORT_8250;
250 uart.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
251 uart.port.dev = &pdev->dev;
253 uart.port.membase = devm_ioremap(&pdev->dev, regs->start,
254 resource_size(regs));
255 if (!uart.port.membase)
258 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
262 data->clk = devm_clk_get(&pdev->dev, NULL);
263 if (!IS_ERR(data->clk)) {
264 clk_prepare_enable(data->clk);
265 uart.port.uartclk = clk_get_rate(data->clk);
268 uart.port.iotype = UPIO_MEM;
269 uart.port.serial_in = dw8250_serial_in;
270 uart.port.serial_out = dw8250_serial_out;
271 uart.port.private_data = data;
273 dw8250_setup_port(&uart);
275 if (pdev->dev.of_node) {
276 err = dw8250_probe_of(&uart.port);
279 } else if (ACPI_HANDLE(&pdev->dev)) {
280 err = dw8250_probe_acpi(&uart);
287 data->line = serial8250_register_8250_port(&uart);
291 platform_set_drvdata(pdev, data);
293 pm_runtime_set_active(&pdev->dev);
294 pm_runtime_enable(&pdev->dev);
299 static int dw8250_remove(struct platform_device *pdev)
301 struct dw8250_data *data = platform_get_drvdata(pdev);
303 pm_runtime_get_sync(&pdev->dev);
305 serial8250_unregister_port(data->line);
307 if (!IS_ERR(data->clk))
308 clk_disable_unprepare(data->clk);
310 pm_runtime_disable(&pdev->dev);
311 pm_runtime_put_noidle(&pdev->dev);
317 static int dw8250_suspend(struct device *dev)
319 struct dw8250_data *data = dev_get_drvdata(dev);
321 serial8250_suspend_port(data->line);
326 static int dw8250_resume(struct device *dev)
328 struct dw8250_data *data = dev_get_drvdata(dev);
330 serial8250_resume_port(data->line);
334 #endif /* CONFIG_PM */
336 #ifdef CONFIG_PM_RUNTIME
337 static int dw8250_runtime_suspend(struct device *dev)
339 struct dw8250_data *data = dev_get_drvdata(dev);
341 if (!IS_ERR(data->clk))
342 clk_disable_unprepare(data->clk);
347 static int dw8250_runtime_resume(struct device *dev)
349 struct dw8250_data *data = dev_get_drvdata(dev);
351 if (!IS_ERR(data->clk))
352 clk_prepare_enable(data->clk);
358 static const struct dev_pm_ops dw8250_pm_ops = {
359 SET_SYSTEM_SLEEP_PM_OPS(dw8250_suspend, dw8250_resume)
360 SET_RUNTIME_PM_OPS(dw8250_runtime_suspend, dw8250_runtime_resume, NULL)
363 static const struct of_device_id dw8250_of_match[] = {
364 { .compatible = "snps,dw-apb-uart" },
367 MODULE_DEVICE_TABLE(of, dw8250_of_match);
369 static const struct acpi_device_id dw8250_acpi_match[] = {
375 MODULE_DEVICE_TABLE(acpi, dw8250_acpi_match);
377 static struct platform_driver dw8250_platform_driver = {
379 .name = "dw-apb-uart",
380 .owner = THIS_MODULE,
381 .pm = &dw8250_pm_ops,
382 .of_match_table = dw8250_of_match,
383 .acpi_match_table = ACPI_PTR(dw8250_acpi_match),
385 .probe = dw8250_probe,
386 .remove = dw8250_remove,
389 module_platform_driver(dw8250_platform_driver);
391 MODULE_AUTHOR("Jamie Iles");
392 MODULE_LICENSE("GPL");
393 MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver");