1
2
3
4
5
6#include <linux/clk.h>
7#include <linux/console.h>
8#include <linux/io.h>
9#include <linux/module.h>
10#include <linux/of.h>
11#include <linux/platform_device.h>
12
13#include "8250.h"
14
15
16
17
18
19
20
21
22
23
24#define UNIPHIER_UART_REGSHIFT 2
25
26
27#define UNIPHIER_UART_CHAR_FCR (3 << (UNIPHIER_UART_REGSHIFT))
28
29#define UNIPHIER_UART_LCR_MCR (4 << (UNIPHIER_UART_REGSHIFT))
30
31#define UNIPHIER_UART_DLR (9 << (UNIPHIER_UART_REGSHIFT))
32
33struct uniphier8250_priv {
34 int line;
35 struct clk *clk;
36 spinlock_t atomic_write_lock;
37};
38
39#ifdef CONFIG_SERIAL_8250_CONSOLE
40static int __init uniphier_early_console_setup(struct earlycon_device *device,
41 const char *options)
42{
43 if (!device->port.membase)
44 return -ENODEV;
45
46
47 device->port.iotype = UPIO_MEM32;
48 device->port.regshift = UNIPHIER_UART_REGSHIFT;
49
50
51
52
53
54 device->baud = 0;
55
56 return early_serial8250_setup(device, options);
57}
58OF_EARLYCON_DECLARE(uniphier, "socionext,uniphier-uart",
59 uniphier_early_console_setup);
60#endif
61
62
63
64
65
66static unsigned int uniphier_serial_in(struct uart_port *p, int offset)
67{
68 unsigned int valshift = 0;
69
70 switch (offset) {
71 case UART_SCR:
72
73 valshift = 8;
74 offset = UNIPHIER_UART_CHAR_FCR;
75 break;
76 case UART_LCR:
77 valshift = 8;
78
79 case UART_MCR:
80 offset = UNIPHIER_UART_LCR_MCR;
81 break;
82 default:
83 offset <<= UNIPHIER_UART_REGSHIFT;
84 break;
85 }
86
87
88
89
90
91
92 return (readl(p->membase + offset) >> valshift) & 0xff;
93}
94
95static void uniphier_serial_out(struct uart_port *p, int offset, int value)
96{
97 unsigned int valshift = 0;
98 bool normal = false;
99
100 switch (offset) {
101 case UART_SCR:
102
103 valshift = 8;
104
105 case UART_FCR:
106 offset = UNIPHIER_UART_CHAR_FCR;
107 break;
108 case UART_LCR:
109 valshift = 8;
110
111 value &= ~UART_LCR_DLAB;
112
113 case UART_MCR:
114 offset = UNIPHIER_UART_LCR_MCR;
115 break;
116 default:
117 offset <<= UNIPHIER_UART_REGSHIFT;
118 normal = true;
119 break;
120 }
121
122 if (normal) {
123 writel(value, p->membase + offset);
124 } else {
125
126
127
128
129
130 struct uniphier8250_priv *priv = p->private_data;
131 unsigned long flags;
132 u32 tmp;
133
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);
140 }
141}
142
143
144
145
146
147
148static int uniphier_serial_dl_read(struct uart_8250_port *up)
149{
150 return readl(up->port.membase + UNIPHIER_UART_DLR);
151}
152
153static void uniphier_serial_dl_write(struct uart_8250_port *up, int value)
154{
155 writel(value, up->port.membase + UNIPHIER_UART_DLR);
156}
157
158static int uniphier_uart_probe(struct platform_device *pdev)
159{
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;
165 int irq;
166 int ret;
167
168 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
169 if (!regs) {
170 dev_err(dev, "failed to get memory resource\n");
171 return -EINVAL;
172 }
173
174 membase = devm_ioremap(dev, regs->start, resource_size(regs));
175 if (!membase)
176 return -ENOMEM;
177
178 irq = platform_get_irq(pdev, 0);
179 if (irq < 0) {
180 dev_err(dev, "failed to get IRQ number\n");
181 return irq;
182 }
183
184 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
185 if (!priv)
186 return -ENOMEM;
187
188 memset(&up, 0, sizeof(up));
189
190 ret = of_alias_get_id(dev->of_node, "serial");
191 if (ret < 0) {
192 dev_err(dev, "failed to get alias id\n");
193 return ret;
194 }
195 up.port.line = ret;
196
197 priv->clk = devm_clk_get(dev, NULL);
198 if (IS_ERR(priv->clk)) {
199 dev_err(dev, "failed to get clock\n");
200 return PTR_ERR(priv->clk);
201 }
202
203 ret = clk_prepare_enable(priv->clk);
204 if (ret)
205 return ret;
206
207 up.port.uartclk = clk_get_rate(priv->clk);
208
209 spin_lock_init(&priv->atomic_write_lock);
210
211 up.port.dev = dev;
212 up.port.private_data = priv;
213 up.port.mapbase = regs->start;
214 up.port.mapsize = resource_size(regs);
215 up.port.membase = membase;
216 up.port.irq = irq;
217
218 up.port.type = PORT_16550A;
219 up.port.iotype = UPIO_MEM32;
220 up.port.fifosize = 64;
221 up.port.regshift = UNIPHIER_UART_REGSHIFT;
222 up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE;
223 up.capabilities = UART_CAP_FIFO;
224
225 if (of_property_read_bool(dev->of_node, "auto-flow-control"))
226 up.capabilities |= UART_CAP_AFE;
227
228 up.port.serial_in = uniphier_serial_in;
229 up.port.serial_out = uniphier_serial_out;
230 up.dl_read = uniphier_serial_dl_read;
231 up.dl_write = uniphier_serial_dl_write;
232
233 ret = serial8250_register_8250_port(&up);
234 if (ret < 0) {
235 dev_err(dev, "failed to register 8250 port\n");
236 clk_disable_unprepare(priv->clk);
237 return ret;
238 }
239 priv->line = ret;
240
241 platform_set_drvdata(pdev, priv);
242
243 return 0;
244}
245
246static int uniphier_uart_remove(struct platform_device *pdev)
247{
248 struct uniphier8250_priv *priv = platform_get_drvdata(pdev);
249
250 serial8250_unregister_port(priv->line);
251 clk_disable_unprepare(priv->clk);
252
253 return 0;
254}
255
256static int __maybe_unused uniphier_uart_suspend(struct device *dev)
257{
258 struct uniphier8250_priv *priv = dev_get_drvdata(dev);
259 struct uart_8250_port *up = serial8250_get_port(priv->line);
260
261 serial8250_suspend_port(priv->line);
262
263 if (!uart_console(&up->port) || console_suspend_enabled)
264 clk_disable_unprepare(priv->clk);
265
266 return 0;
267}
268
269static int __maybe_unused uniphier_uart_resume(struct device *dev)
270{
271 struct uniphier8250_priv *priv = dev_get_drvdata(dev);
272 struct uart_8250_port *up = serial8250_get_port(priv->line);
273 int ret;
274
275 if (!uart_console(&up->port) || console_suspend_enabled) {
276 ret = clk_prepare_enable(priv->clk);
277 if (ret)
278 return ret;
279 }
280
281 serial8250_resume_port(priv->line);
282
283 return 0;
284}
285
286static const struct dev_pm_ops uniphier_uart_pm_ops = {
287 SET_SYSTEM_SLEEP_PM_OPS(uniphier_uart_suspend, uniphier_uart_resume)
288};
289
290static const struct of_device_id uniphier_uart_match[] = {
291 { .compatible = "socionext,uniphier-uart" },
292 { }
293};
294MODULE_DEVICE_TABLE(of, uniphier_uart_match);
295
296static struct platform_driver uniphier_uart_platform_driver = {
297 .probe = uniphier_uart_probe,
298 .remove = uniphier_uart_remove,
299 .driver = {
300 .name = "uniphier-uart",
301 .of_match_table = uniphier_uart_match,
302 .pm = &uniphier_uart_pm_ops,
303 },
304};
305module_platform_driver(uniphier_uart_platform_driver);
306
307MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
308MODULE_DESCRIPTION("UniPhier UART driver");
309MODULE_LICENSE("GPL");
310