2 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/clk.h>
16 #include <linux/console.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
24 /* Most (but not all) of UniPhier UART devices have 64-depth FIFO. */
25 #define UNIPHIER_UART_DEFAULT_FIFO_SIZE 64
28 * This hardware is similar to 8250, but its register map is a bit different:
29 * - MMIO32 (regshift = 2)
30 * - FCR is not at 2, but 3
31 * - LCR and MCR are not at 3 and 4, they share 4
32 * - Divisor latch at 9, no divisor latch access bit
35 #define UNIPHIER_UART_REGSHIFT 2
37 /* bit[15:8] = CHAR (not used), bit[7:0] = FCR */
38 #define UNIPHIER_UART_CHAR_FCR (3 << (UNIPHIER_UART_REGSHIFT))
39 /* bit[15:8] = LCR, bit[7:0] = MCR */
40 #define UNIPHIER_UART_LCR_MCR (4 << (UNIPHIER_UART_REGSHIFT))
41 /* Divisor Latch Register */
42 #define UNIPHIER_UART_DLR (9 << (UNIPHIER_UART_REGSHIFT))
44 struct uniphier8250_priv
{
47 spinlock_t atomic_write_lock
;
50 #ifdef CONFIG_SERIAL_8250_CONSOLE
51 static int __init
uniphier_early_console_setup(struct earlycon_device
*device
,
54 if (!device
->port
.membase
)
57 /* This hardware always expects MMIO32 register interface. */
58 device
->port
.iotype
= UPIO_MEM32
;
59 device
->port
.regshift
= UNIPHIER_UART_REGSHIFT
;
62 * Do not touch the divisor register in early_serial8250_setup();
63 * we assume it has been initialized by a boot loader.
67 return early_serial8250_setup(device
, options
);
69 OF_EARLYCON_DECLARE(uniphier
, "socionext,uniphier-uart",
70 uniphier_early_console_setup
);
74 * The register map is slightly different from that of 8250.
75 * IO callbacks must be overridden for correct access to FCR, LCR, and MCR.
77 static unsigned int uniphier_serial_in(struct uart_port
*p
, int offset
)
79 unsigned int valshift
= 0;
86 offset
= UNIPHIER_UART_LCR_MCR
;
89 offset
<<= UNIPHIER_UART_REGSHIFT
;
94 * The return value must be masked with 0xff because LCR and MCR reside
95 * in the same register that must be accessed by 32-bit write/read.
96 * 8 or 16 bit access to this hardware result in unexpected behavior.
98 return (readl(p
->membase
+ offset
) >> valshift
) & 0xff;
101 static void uniphier_serial_out(struct uart_port
*p
, int offset
, int value
)
103 unsigned int valshift
= 0;
108 offset
= UNIPHIER_UART_CHAR_FCR
;
112 /* Divisor latch access bit does not exist. */
113 value
&= ~UART_LCR_DLAB
;
116 offset
= UNIPHIER_UART_LCR_MCR
;
120 offset
<<= UNIPHIER_UART_REGSHIFT
;
125 writel(value
, p
->membase
+ offset
);
128 * Special case: two registers share the same address that
129 * must be 32-bit accessed. As this is not longer atomic safe,
130 * take a lock just in case.
132 struct uniphier8250_priv
*priv
= p
->private_data
;
136 spin_lock_irqsave(&priv
->atomic_write_lock
, flags
);
137 tmp
= readl(p
->membase
+ offset
);
138 tmp
&= ~(0xff << valshift
);
139 tmp
|= value
<< valshift
;
140 writel(tmp
, p
->membase
+ offset
);
141 spin_unlock_irqrestore(&priv
->atomic_write_lock
, flags
);
146 * This hardware does not have the divisor latch access bit.
147 * The divisor latch register exists at different address.
148 * Override dl_read/write callbacks.
150 static int uniphier_serial_dl_read(struct uart_8250_port
*up
)
152 return readl(up
->port
.membase
+ UNIPHIER_UART_DLR
);
155 static void uniphier_serial_dl_write(struct uart_8250_port
*up
, int value
)
157 writel(value
, up
->port
.membase
+ UNIPHIER_UART_DLR
);
160 static int uniphier_of_serial_setup(struct device
*dev
, struct uart_port
*port
,
161 struct uniphier8250_priv
*priv
)
165 struct device_node
*np
= dev
->of_node
;
167 ret
= of_alias_get_id(np
, "serial");
169 dev_err(dev
, "failed to get alias id\n");
172 port
->line
= priv
->line
= ret
;
174 /* Get clk rate through clk driver */
175 priv
->clk
= devm_clk_get(dev
, NULL
);
176 if (IS_ERR(priv
->clk
)) {
177 dev_err(dev
, "failed to get clock\n");
178 return PTR_ERR(priv
->clk
);
181 ret
= clk_prepare_enable(priv
->clk
);
185 port
->uartclk
= clk_get_rate(priv
->clk
);
187 /* Check for fifo size */
188 if (of_property_read_u32(np
, "fifo-size", &prop
) == 0)
189 port
->fifosize
= prop
;
191 port
->fifosize
= UNIPHIER_UART_DEFAULT_FIFO_SIZE
;
196 static int uniphier_uart_probe(struct platform_device
*pdev
)
198 struct device
*dev
= &pdev
->dev
;
199 struct uart_8250_port up
;
200 struct uniphier8250_priv
*priv
;
201 struct resource
*regs
;
202 void __iomem
*membase
;
206 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
208 dev_err(dev
, "failed to get memory resource\n");
212 membase
= devm_ioremap(dev
, regs
->start
, resource_size(regs
));
216 irq
= platform_get_irq(pdev
, 0);
218 dev_err(dev
, "failed to get IRQ number\n");
222 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
226 memset(&up
, 0, sizeof(up
));
228 ret
= uniphier_of_serial_setup(dev
, &up
.port
, priv
);
232 spin_lock_init(&priv
->atomic_write_lock
);
235 up
.port
.private_data
= priv
;
236 up
.port
.mapbase
= regs
->start
;
237 up
.port
.mapsize
= resource_size(regs
);
238 up
.port
.membase
= membase
;
241 up
.port
.type
= PORT_16550A
;
242 up
.port
.iotype
= UPIO_MEM32
;
243 up
.port
.regshift
= UNIPHIER_UART_REGSHIFT
;
244 up
.port
.flags
= UPF_FIXED_PORT
| UPF_FIXED_TYPE
;
245 up
.capabilities
= UART_CAP_FIFO
;
247 up
.port
.serial_in
= uniphier_serial_in
;
248 up
.port
.serial_out
= uniphier_serial_out
;
249 up
.dl_read
= uniphier_serial_dl_read
;
250 up
.dl_write
= uniphier_serial_dl_write
;
252 ret
= serial8250_register_8250_port(&up
);
254 dev_err(dev
, "failed to register 8250 port\n");
255 clk_disable_unprepare(priv
->clk
);
259 platform_set_drvdata(pdev
, priv
);
264 static int uniphier_uart_remove(struct platform_device
*pdev
)
266 struct uniphier8250_priv
*priv
= platform_get_drvdata(pdev
);
268 serial8250_unregister_port(priv
->line
);
269 clk_disable_unprepare(priv
->clk
);
274 static const struct of_device_id uniphier_uart_match
[] = {
275 { .compatible
= "socionext,uniphier-uart" },
278 MODULE_DEVICE_TABLE(of
, uniphier_uart_match
);
280 static struct platform_driver uniphier_uart_platform_driver
= {
281 .probe
= uniphier_uart_probe
,
282 .remove
= uniphier_uart_remove
,
284 .name
= "uniphier-uart",
285 .of_match_table
= uniphier_uart_match
,
288 module_platform_driver(uniphier_uart_platform_driver
);
290 MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
291 MODULE_DESCRIPTION("UniPhier UART driver");
292 MODULE_LICENSE("GPL");