blob: 3489fbcb7313a1e1cc59e6591beccf313f865eb0 [file] [log] [blame]
Matthias Bruggerb4756f42014-09-09 17:31:42 +02001/*
2 * Mediatek 8250 driver.
3 *
4 * Copyright (c) 2014 MundoReader S.L.
5 * Author: Matthias Brugger <matthias.bgg@gmail.com>
6 *
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.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17#include <linux/clk.h>
18#include <linux/io.h>
Arnd Bergmann88725e92016-02-17 16:02:31 +010019#include <linux/module.h>
Matthias Bruggerb4756f42014-09-09 17:31:42 +020020#include <linux/of_irq.h>
21#include <linux/of_platform.h>
22#include <linux/platform_device.h>
23#include <linux/pm_runtime.h>
24#include <linux/serial_8250.h>
25#include <linux/serial_reg.h>
26
27#include "8250.h"
28
29#define UART_MTK_HIGHS 0x09 /* Highspeed register */
30#define UART_MTK_SAMPLE_COUNT 0x0a /* Sample count register */
31#define UART_MTK_SAMPLE_POINT 0x0b /* Sample point register */
32#define MTK_UART_RATE_FIX 0x0d /* UART Rate Fix Register */
33
34struct mtk8250_data {
35 int line;
36 struct clk *uart_clk;
Sascha Hauerc1c325d2015-04-27 08:49:57 +020037 struct clk *bus_clk;
Matthias Bruggerb4756f42014-09-09 17:31:42 +020038};
39
40static void
41mtk8250_set_termios(struct uart_port *port, struct ktermios *termios,
42 struct ktermios *old)
43{
Andy Shevchenko013e3582016-02-18 21:22:59 +020044 struct uart_8250_port *up = up_to_u8250p(port);
Matthias Bruggerb4756f42014-09-09 17:31:42 +020045 unsigned long flags;
46 unsigned int baud, quot;
47
Matthias Bruggerb4756f42014-09-09 17:31:42 +020048 serial8250_do_set_termios(port, termios, old);
49
50 /*
51 * Mediatek UARTs use an extra highspeed register (UART_MTK_HIGHS)
52 *
53 * We need to recalcualte the quot register, as the claculation depends
54 * on the vaule in the highspeed register.
55 *
56 * Some baudrates are not supported by the chip, so we use the next
57 * lower rate supported and update termios c_flag.
58 *
59 * If highspeed register is set to 3, we need to specify sample count
60 * and sample point to increase accuracy. If not, we reset the
61 * registers to their default values.
62 */
63 baud = uart_get_baud_rate(port, termios, old,
64 port->uartclk / 16 / 0xffff,
65 port->uartclk / 16);
66
67 if (baud <= 115200) {
68 serial_port_out(port, UART_MTK_HIGHS, 0x0);
69 quot = uart_get_divisor(port, baud);
70 } else if (baud <= 576000) {
71 serial_port_out(port, UART_MTK_HIGHS, 0x2);
72
73 /* Set to next lower baudrate supported */
74 if ((baud == 500000) || (baud == 576000))
75 baud = 460800;
Eddie Huang2a768262014-10-22 21:12:07 +080076 quot = DIV_ROUND_UP(port->uartclk, 4 * baud);
Matthias Bruggerb4756f42014-09-09 17:31:42 +020077 } else {
78 serial_port_out(port, UART_MTK_HIGHS, 0x3);
79
80 /* Set to highest baudrate supported */
81 if (baud >= 1152000)
82 baud = 921600;
Eddie Huang2a768262014-10-22 21:12:07 +080083 quot = DIV_ROUND_UP(port->uartclk, 256 * baud);
Matthias Bruggerb4756f42014-09-09 17:31:42 +020084 }
85
86 /*
87 * Ok, we're now changing the port state. Do it with
88 * interrupts disabled.
89 */
90 spin_lock_irqsave(&port->lock, flags);
91
92 /* set DLAB we have cval saved in up->lcr from the call to the core */
93 serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
94 serial_dl_write(up, quot);
95
96 /* reset DLAB */
97 serial_port_out(port, UART_LCR, up->lcr);
98
99 if (baud > 460800) {
100 unsigned int tmp;
101
102 tmp = DIV_ROUND_CLOSEST(port->uartclk, quot * baud);
103 serial_port_out(port, UART_MTK_SAMPLE_COUNT, tmp - 1);
104 serial_port_out(port, UART_MTK_SAMPLE_POINT,
105 (tmp - 2) >> 1);
106 } else {
107 serial_port_out(port, UART_MTK_SAMPLE_COUNT, 0x00);
108 serial_port_out(port, UART_MTK_SAMPLE_POINT, 0xff);
109 }
110
111 spin_unlock_irqrestore(&port->lock, flags);
112 /* Don't rewrite B0 */
113 if (tty_termios_baud_rate(termios))
114 tty_termios_encode_baud_rate(termios, baud, baud);
115}
116
Arnd Bergmann9db669f2016-02-08 14:36:13 +0100117static int __maybe_unused mtk8250_runtime_suspend(struct device *dev)
Sascha Hauer68e5fc42015-04-27 08:49:56 +0200118{
119 struct mtk8250_data *data = dev_get_drvdata(dev);
120
121 clk_disable_unprepare(data->uart_clk);
Sascha Hauerc1c325d2015-04-27 08:49:57 +0200122 clk_disable_unprepare(data->bus_clk);
Sascha Hauer68e5fc42015-04-27 08:49:56 +0200123
124 return 0;
125}
126
Arnd Bergmann9db669f2016-02-08 14:36:13 +0100127static int __maybe_unused mtk8250_runtime_resume(struct device *dev)
Sascha Hauer68e5fc42015-04-27 08:49:56 +0200128{
129 struct mtk8250_data *data = dev_get_drvdata(dev);
130 int err;
131
132 err = clk_prepare_enable(data->uart_clk);
133 if (err) {
134 dev_warn(dev, "Can't enable clock\n");
135 return err;
136 }
137
Sascha Hauerc1c325d2015-04-27 08:49:57 +0200138 err = clk_prepare_enable(data->bus_clk);
139 if (err) {
140 dev_warn(dev, "Can't enable bus clock\n");
141 return err;
142 }
143
Sascha Hauer68e5fc42015-04-27 08:49:56 +0200144 return 0;
145}
146
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200147static void
148mtk8250_do_pm(struct uart_port *port, unsigned int state, unsigned int old)
149{
150 if (!state)
151 pm_runtime_get_sync(port->dev);
152
153 serial8250_do_pm(port, state, old);
154
155 if (state)
156 pm_runtime_put_sync_suspend(port->dev);
157}
158
159static int mtk8250_probe_of(struct platform_device *pdev, struct uart_port *p,
160 struct mtk8250_data *data)
161{
Sascha Hauerc1c325d2015-04-27 08:49:57 +0200162 data->uart_clk = devm_clk_get(&pdev->dev, "baud");
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200163 if (IS_ERR(data->uart_clk)) {
Sascha Hauerc1c325d2015-04-27 08:49:57 +0200164 /*
165 * For compatibility with older device trees try unnamed
166 * clk when no baud clk can be found.
167 */
168 data->uart_clk = devm_clk_get(&pdev->dev, NULL);
169 if (IS_ERR(data->uart_clk)) {
170 dev_warn(&pdev->dev, "Can't get uart clock\n");
171 return PTR_ERR(data->uart_clk);
172 }
173
174 return 0;
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200175 }
176
Sascha Hauerc1c325d2015-04-27 08:49:57 +0200177 data->bus_clk = devm_clk_get(&pdev->dev, "bus");
178 if (IS_ERR(data->bus_clk))
179 return PTR_ERR(data->bus_clk);
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200180
181 return 0;
182}
183
184static int mtk8250_probe(struct platform_device *pdev)
185{
186 struct uart_8250_port uart = {};
187 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
188 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
189 struct mtk8250_data *data;
190 int err;
191
192 if (!regs || !irq) {
193 dev_err(&pdev->dev, "no registers/irq defined\n");
194 return -EINVAL;
195 }
196
197 uart.port.membase = devm_ioremap(&pdev->dev, regs->start,
198 resource_size(regs));
199 if (!uart.port.membase)
200 return -ENOMEM;
201
202 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
203 if (!data)
204 return -ENOMEM;
205
206 if (pdev->dev.of_node) {
207 err = mtk8250_probe_of(pdev, &uart.port, data);
208 if (err)
209 return err;
210 } else
211 return -ENODEV;
212
213 spin_lock_init(&uart.port.lock);
214 uart.port.mapbase = regs->start;
215 uart.port.irq = irq->start;
216 uart.port.pm = mtk8250_do_pm;
217 uart.port.type = PORT_16550;
218 uart.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
219 uart.port.dev = &pdev->dev;
220 uart.port.iotype = UPIO_MEM32;
221 uart.port.regshift = 2;
222 uart.port.private_data = data;
223 uart.port.set_termios = mtk8250_set_termios;
Sascha Hauerc1c325d2015-04-27 08:49:57 +0200224 uart.port.uartclk = clk_get_rate(data->uart_clk);
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200225
226 /* Disable Rate Fix function */
227 writel(0x0, uart.port.membase +
228 (MTK_UART_RATE_FIX << uart.port.regshift));
229
Sascha Hauer68e5fc42015-04-27 08:49:56 +0200230 platform_set_drvdata(pdev, data);
231
232 pm_runtime_enable(&pdev->dev);
233 if (!pm_runtime_enabled(&pdev->dev)) {
234 err = mtk8250_runtime_resume(&pdev->dev);
235 if (err)
236 return err;
237 }
238
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200239 data->line = serial8250_register_8250_port(&uart);
240 if (data->line < 0)
241 return data->line;
242
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200243 return 0;
244}
245
Arnd Bergmann88725e92016-02-17 16:02:31 +0100246static int mtk8250_remove(struct platform_device *pdev)
247{
248 struct mtk8250_data *data = platform_get_drvdata(pdev);
249
250 pm_runtime_get_sync(&pdev->dev);
251
252 serial8250_unregister_port(data->line);
253
254 pm_runtime_disable(&pdev->dev);
255 pm_runtime_put_noidle(&pdev->dev);
256
257 if (!pm_runtime_status_suspended(&pdev->dev))
258 mtk8250_runtime_suspend(&pdev->dev);
259
260 return 0;
261}
262
Arnd Bergmann9db669f2016-02-08 14:36:13 +0100263static int __maybe_unused mtk8250_suspend(struct device *dev)
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200264{
265 struct mtk8250_data *data = dev_get_drvdata(dev);
266
267 serial8250_suspend_port(data->line);
268
269 return 0;
270}
271
Arnd Bergmann9db669f2016-02-08 14:36:13 +0100272static int __maybe_unused mtk8250_resume(struct device *dev)
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200273{
274 struct mtk8250_data *data = dev_get_drvdata(dev);
275
276 serial8250_resume_port(data->line);
277
278 return 0;
279}
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200280
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200281static const struct dev_pm_ops mtk8250_pm_ops = {
282 SET_SYSTEM_SLEEP_PM_OPS(mtk8250_suspend, mtk8250_resume)
283 SET_RUNTIME_PM_OPS(mtk8250_runtime_suspend, mtk8250_runtime_resume,
284 NULL)
285};
286
287static const struct of_device_id mtk8250_of_match[] = {
288 { .compatible = "mediatek,mt6577-uart" },
289 { /* Sentinel */ }
290};
Arnd Bergmann88725e92016-02-17 16:02:31 +0100291MODULE_DEVICE_TABLE(of, mtk8250_of_match);
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200292
293static struct platform_driver mtk8250_platform_driver = {
294 .driver = {
Arnd Bergmann88725e92016-02-17 16:02:31 +0100295 .name = "mt6577-uart",
296 .pm = &mtk8250_pm_ops,
297 .of_match_table = mtk8250_of_match,
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200298 },
299 .probe = mtk8250_probe,
Arnd Bergmann88725e92016-02-17 16:02:31 +0100300 .remove = mtk8250_remove,
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200301};
Arnd Bergmann88725e92016-02-17 16:02:31 +0100302module_platform_driver(mtk8250_platform_driver);
Matthias Bruggerb4756f42014-09-09 17:31:42 +0200303
Arnd Bergmann3f5921a2016-02-17 16:02:35 +0100304#if defined(CONFIG_SERIAL_8250_CONSOLE) && !defined(MODULE)
Eddie Huang2c40b572015-04-28 21:40:33 +0800305static int __init early_mtk8250_setup(struct earlycon_device *device,
306 const char *options)
307{
308 if (!device->port.membase)
309 return -ENODEV;
310
311 device->port.iotype = UPIO_MEM32;
312
313 return early_serial8250_setup(device, NULL);
314}
315
316OF_EARLYCON_DECLARE(mtk8250, "mediatek,mt6577-uart", early_mtk8250_setup);
Arnd Bergmann7798ede2015-05-19 22:11:09 +0200317#endif
Arnd Bergmann88725e92016-02-17 16:02:31 +0100318
319MODULE_AUTHOR("Matthias Brugger");
320MODULE_LICENSE("GPL");
321MODULE_DESCRIPTION("Mediatek 8250 serial port driver");