1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
7 #include <linux/console.h>
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
16 * This hardware is similar to 8250, but its register map is a bit different:
17 * - MMIO32 (regshift = 2)
18 * - FCR is not at 2, but 3
19 * - LCR and MCR are not at 3 and 4, they share 4
20 * - No SCR (Instead, CHAR can be used as a scratch register)
21 * - Divisor latch at 9, no divisor latch access bit
24 #define UNIPHIER_UART_REGSHIFT 2
26 /* bit[15:8] = CHAR, bit[7:0] = FCR */
27 #define UNIPHIER_UART_CHAR_FCR (3 << (UNIPHIER_UART_REGSHIFT))
28 /* bit[15:8] = LCR, bit[7:0] = MCR */
29 #define UNIPHIER_UART_LCR_MCR (4 << (UNIPHIER_UART_REGSHIFT))
30 /* Divisor Latch Register */
31 #define UNIPHIER_UART_DLR (9 << (UNIPHIER_UART_REGSHIFT))
33 struct uniphier8250_priv {
36 spinlock_t atomic_write_lock;
39 #ifdef CONFIG_SERIAL_8250_CONSOLE
40 static int __init uniphier_early_console_setup(struct earlycon_device *device,
43 if (!device->port.membase)
46 /* This hardware always expects MMIO32 register interface. */
47 device->port.iotype = UPIO_MEM32;
48 device->port.regshift = UNIPHIER_UART_REGSHIFT;
51 * Do not touch the divisor register in early_serial8250_setup();
52 * we assume it has been initialized by a boot loader.
56 return early_serial8250_setup(device, options);
58 OF_EARLYCON_DECLARE(uniphier, "socionext,uniphier-uart",
59 uniphier_early_console_setup);
63 * The register map is slightly different from that of 8250.
64 * IO callbacks must be overridden for correct access to FCR, LCR, MCR and SCR.
66 static unsigned int uniphier_serial_in(struct uart_port *p, int offset)
68 unsigned int valshift = 0;
72 /* No SCR for this hardware. Use CHAR as a scratch register */
74 offset = UNIPHIER_UART_CHAR_FCR;
80 offset = UNIPHIER_UART_LCR_MCR;
83 offset <<= UNIPHIER_UART_REGSHIFT;
88 * The return value must be masked with 0xff because some registers
89 * share the same offset that must be accessed by 32-bit write/read.
90 * 8 or 16 bit access to this hardware result in unexpected behavior.
92 return (readl(p->membase + offset) >> valshift) & 0xff;
95 static void uniphier_serial_out(struct uart_port *p, int offset, int value)
97 unsigned int valshift = 0;
102 /* No SCR for this hardware. Use CHAR as a scratch register */
106 offset = UNIPHIER_UART_CHAR_FCR;
110 /* Divisor latch access bit does not exist. */
111 value &= ~UART_LCR_DLAB;
114 offset = UNIPHIER_UART_LCR_MCR;
117 offset <<= UNIPHIER_UART_REGSHIFT;
123 writel(value, p->membase + offset);
126 * Special case: two registers share the same address that
127 * must be 32-bit accessed. As this is not longer atomic safe,
128 * take a lock just in case.
130 struct uniphier8250_priv *priv = p->private_data;
134 spin_lock_irqsave(&priv->atomic_write_lock, flags);
135 tmp = readl(p->membase + offset);
136 tmp &= ~(0xff << valshift);
137 tmp |= value << valshift;
138 writel(tmp, p->membase + offset);
139 spin_unlock_irqrestore(&priv->atomic_write_lock, flags);
144 * This hardware does not have the divisor latch access bit.
145 * The divisor latch register exists at different address.
146 * Override dl_read/write callbacks.
148 static u32 uniphier_serial_dl_read(struct uart_8250_port *up)
150 return readl(up->port.membase + UNIPHIER_UART_DLR);
153 static void uniphier_serial_dl_write(struct uart_8250_port *up, u32 value)
155 writel(value, up->port.membase + UNIPHIER_UART_DLR);
158 static int uniphier_uart_probe(struct platform_device *pdev)
160 struct device *dev = &pdev->dev;
161 struct uart_8250_port up;
162 struct uniphier8250_priv *priv;
163 struct resource *regs;
164 void __iomem *membase;
168 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
170 dev_err(dev, "failed to get memory resource\n");
174 membase = devm_ioremap(dev, regs->start, resource_size(regs));
178 irq = platform_get_irq(pdev, 0);
182 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
186 memset(&up, 0, sizeof(up));
188 ret = of_alias_get_id(dev->of_node, "serial");
190 dev_err(dev, "failed to get alias id\n");
195 priv->clk = devm_clk_get(dev, NULL);
196 if (IS_ERR(priv->clk)) {
197 dev_err(dev, "failed to get clock\n");
198 return PTR_ERR(priv->clk);
201 ret = clk_prepare_enable(priv->clk);
205 up.port.uartclk = clk_get_rate(priv->clk);
207 spin_lock_init(&priv->atomic_write_lock);
210 up.port.private_data = priv;
211 up.port.mapbase = regs->start;
212 up.port.mapsize = resource_size(regs);
213 up.port.membase = membase;
216 up.port.type = PORT_16550A;
217 up.port.iotype = UPIO_MEM32;
218 up.port.fifosize = 64;
219 up.port.regshift = UNIPHIER_UART_REGSHIFT;
220 up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE;
221 up.capabilities = UART_CAP_FIFO;
223 if (of_property_read_bool(dev->of_node, "auto-flow-control"))
224 up.capabilities |= UART_CAP_AFE;
226 up.port.serial_in = uniphier_serial_in;
227 up.port.serial_out = uniphier_serial_out;
228 up.dl_read = uniphier_serial_dl_read;
229 up.dl_write = uniphier_serial_dl_write;
231 ret = serial8250_register_8250_port(&up);
233 dev_err(dev, "failed to register 8250 port\n");
234 clk_disable_unprepare(priv->clk);
239 platform_set_drvdata(pdev, priv);
244 static void uniphier_uart_remove(struct platform_device *pdev)
246 struct uniphier8250_priv *priv = platform_get_drvdata(pdev);
248 serial8250_unregister_port(priv->line);
249 clk_disable_unprepare(priv->clk);
252 static int __maybe_unused uniphier_uart_suspend(struct device *dev)
254 struct uniphier8250_priv *priv = dev_get_drvdata(dev);
255 struct uart_8250_port *up = serial8250_get_port(priv->line);
257 serial8250_suspend_port(priv->line);
259 if (!uart_console(&up->port) || console_suspend_enabled)
260 clk_disable_unprepare(priv->clk);
265 static int __maybe_unused uniphier_uart_resume(struct device *dev)
267 struct uniphier8250_priv *priv = dev_get_drvdata(dev);
268 struct uart_8250_port *up = serial8250_get_port(priv->line);
271 if (!uart_console(&up->port) || console_suspend_enabled) {
272 ret = clk_prepare_enable(priv->clk);
277 serial8250_resume_port(priv->line);
282 static const struct dev_pm_ops uniphier_uart_pm_ops = {
283 SET_SYSTEM_SLEEP_PM_OPS(uniphier_uart_suspend, uniphier_uart_resume)
286 static const struct of_device_id uniphier_uart_match[] = {
287 { .compatible = "socionext,uniphier-uart" },
290 MODULE_DEVICE_TABLE(of, uniphier_uart_match);
292 static struct platform_driver uniphier_uart_platform_driver = {
293 .probe = uniphier_uart_probe,
294 .remove_new = uniphier_uart_remove,
296 .name = "uniphier-uart",
297 .of_match_table = uniphier_uart_match,
298 .pm = &uniphier_uart_pm_ops,
301 module_platform_driver(uniphier_uart_platform_driver);
303 MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
304 MODULE_DESCRIPTION("UniPhier UART driver");
305 MODULE_LICENSE("GPL");