• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
19 #include <linux/module.h>
20 #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 
34 struct mtk8250_data {
35 	int			line;
36 	struct clk		*uart_clk;
37 	struct clk		*bus_clk;
38 };
39 
40 static void
mtk8250_set_termios(struct uart_port * port,struct ktermios * termios,struct ktermios * old)41 mtk8250_set_termios(struct uart_port *port, struct ktermios *termios,
42 			struct ktermios *old)
43 {
44 	unsigned long flags;
45 	unsigned int baud, quot;
46 
47 	struct uart_8250_port *up =
48 		container_of(port, struct uart_8250_port, port);
49 
50 	/*
51 	 * Store the requested baud rate before calling the generic 8250
52 	 * set_termios method. Standard 8250 port expects bauds to be
53 	 * no higher than (uartclk / 16) so the baud will be clamped if it
54 	 * gets out of that bound. Mediatek 8250 port supports speed
55 	 * higher than that, therefore we'll get original baud rate back
56 	 * after calling the generic set_termios method and recalculate
57 	 * the speed later in this method.
58 	 */
59 	baud = tty_termios_baud_rate(termios);
60 
61 	serial8250_do_set_termios(port, termios, NULL);
62 
63 	tty_termios_encode_baud_rate(termios, baud, baud);
64 
65 	/*
66 	 * Mediatek UARTs use an extra highspeed register (UART_MTK_HIGHS)
67 	 *
68 	 * We need to recalcualte the quot register, as the claculation depends
69 	 * on the vaule in the highspeed register.
70 	 *
71 	 * Some baudrates are not supported by the chip, so we use the next
72 	 * lower rate supported and update termios c_flag.
73 	 *
74 	 * If highspeed register is set to 3, we need to specify sample count
75 	 * and sample point to increase accuracy. If not, we reset the
76 	 * registers to their default values.
77 	 */
78 	baud = uart_get_baud_rate(port, termios, old,
79 				  port->uartclk / 16 / 0xffff,
80 				  port->uartclk / 16);
81 
82 	if (baud <= 115200) {
83 		serial_port_out(port, UART_MTK_HIGHS, 0x0);
84 		quot = uart_get_divisor(port, baud);
85 	} else if (baud <= 576000) {
86 		serial_port_out(port, UART_MTK_HIGHS, 0x2);
87 
88 		/* Set to next lower baudrate supported */
89 		if ((baud == 500000) || (baud == 576000))
90 			baud = 460800;
91 		quot = DIV_ROUND_UP(port->uartclk, 4 * baud);
92 	} else {
93 		serial_port_out(port, UART_MTK_HIGHS, 0x3);
94 
95 		/* Set to highest baudrate supported */
96 		if (baud >= 1152000)
97 			baud = 921600;
98 		quot = DIV_ROUND_UP(port->uartclk, 256 * baud);
99 	}
100 
101 	/*
102 	 * Ok, we're now changing the port state.  Do it with
103 	 * interrupts disabled.
104 	 */
105 	spin_lock_irqsave(&port->lock, flags);
106 
107 	/*
108 	 * Update the per-port timeout.
109 	 */
110 	uart_update_timeout(port, termios->c_cflag, baud);
111 
112 	/* set DLAB we have cval saved in up->lcr from the call to the core */
113 	serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
114 	serial_dl_write(up, quot);
115 
116 	/* reset DLAB */
117 	serial_port_out(port, UART_LCR, up->lcr);
118 
119 	if (baud > 460800) {
120 		unsigned int tmp;
121 
122 		tmp = DIV_ROUND_CLOSEST(port->uartclk, quot * baud);
123 		serial_port_out(port, UART_MTK_SAMPLE_COUNT, tmp - 1);
124 		serial_port_out(port, UART_MTK_SAMPLE_POINT,
125 					(tmp - 2) >> 1);
126 	} else {
127 		serial_port_out(port, UART_MTK_SAMPLE_COUNT, 0x00);
128 		serial_port_out(port, UART_MTK_SAMPLE_POINT, 0xff);
129 	}
130 
131 	spin_unlock_irqrestore(&port->lock, flags);
132 	/* Don't rewrite B0 */
133 	if (tty_termios_baud_rate(termios))
134 		tty_termios_encode_baud_rate(termios, baud, baud);
135 }
136 
mtk8250_runtime_suspend(struct device * dev)137 static int mtk8250_runtime_suspend(struct device *dev)
138 {
139 	struct mtk8250_data *data = dev_get_drvdata(dev);
140 
141 	clk_disable_unprepare(data->uart_clk);
142 	clk_disable_unprepare(data->bus_clk);
143 
144 	return 0;
145 }
146 
mtk8250_runtime_resume(struct device * dev)147 static int mtk8250_runtime_resume(struct device *dev)
148 {
149 	struct mtk8250_data *data = dev_get_drvdata(dev);
150 	int err;
151 
152 	err = clk_prepare_enable(data->uart_clk);
153 	if (err) {
154 		dev_warn(dev, "Can't enable clock\n");
155 		return err;
156 	}
157 
158 	err = clk_prepare_enable(data->bus_clk);
159 	if (err) {
160 		dev_warn(dev, "Can't enable bus clock\n");
161 		return err;
162 	}
163 
164 	return 0;
165 }
166 
167 static void
mtk8250_do_pm(struct uart_port * port,unsigned int state,unsigned int old)168 mtk8250_do_pm(struct uart_port *port, unsigned int state, unsigned int old)
169 {
170 	if (!state)
171 		pm_runtime_get_sync(port->dev);
172 
173 	serial8250_do_pm(port, state, old);
174 
175 	if (state)
176 		pm_runtime_put_sync_suspend(port->dev);
177 }
178 
mtk8250_probe_of(struct platform_device * pdev,struct uart_port * p,struct mtk8250_data * data)179 static int mtk8250_probe_of(struct platform_device *pdev, struct uart_port *p,
180 			   struct mtk8250_data *data)
181 {
182 	data->uart_clk = devm_clk_get(&pdev->dev, "baud");
183 	if (IS_ERR(data->uart_clk)) {
184 		/*
185 		 * For compatibility with older device trees try unnamed
186 		 * clk when no baud clk can be found.
187 		 */
188 		data->uart_clk = devm_clk_get(&pdev->dev, NULL);
189 		if (IS_ERR(data->uart_clk)) {
190 			dev_warn(&pdev->dev, "Can't get uart clock\n");
191 			return PTR_ERR(data->uart_clk);
192 		}
193 
194 		return 0;
195 	}
196 
197 	data->bus_clk = devm_clk_get(&pdev->dev, "bus");
198 	if (IS_ERR(data->bus_clk))
199 		return PTR_ERR(data->bus_clk);
200 
201 	return 0;
202 }
203 
mtk8250_probe(struct platform_device * pdev)204 static int mtk8250_probe(struct platform_device *pdev)
205 {
206 	struct uart_8250_port uart = {};
207 	struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
208 	struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
209 	struct mtk8250_data *data;
210 	int err;
211 
212 	if (!regs || !irq) {
213 		dev_err(&pdev->dev, "no registers/irq defined\n");
214 		return -EINVAL;
215 	}
216 
217 	uart.port.membase = devm_ioremap(&pdev->dev, regs->start,
218 					 resource_size(regs));
219 	if (!uart.port.membase)
220 		return -ENOMEM;
221 
222 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
223 	if (!data)
224 		return -ENOMEM;
225 
226 	if (pdev->dev.of_node) {
227 		err = mtk8250_probe_of(pdev, &uart.port, data);
228 		if (err)
229 			return err;
230 	} else
231 		return -ENODEV;
232 
233 	spin_lock_init(&uart.port.lock);
234 	uart.port.mapbase = regs->start;
235 	uart.port.irq = irq->start;
236 	uart.port.pm = mtk8250_do_pm;
237 	uart.port.type = PORT_16550;
238 	uart.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
239 	uart.port.dev = &pdev->dev;
240 	uart.port.iotype = UPIO_MEM32;
241 	uart.port.regshift = 2;
242 	uart.port.private_data = data;
243 	uart.port.set_termios = mtk8250_set_termios;
244 	uart.port.uartclk = clk_get_rate(data->uart_clk);
245 
246 	/* Disable Rate Fix function */
247 	writel(0x0, uart.port.membase +
248 			(MTK_UART_RATE_FIX << uart.port.regshift));
249 
250 	platform_set_drvdata(pdev, data);
251 
252 	err = mtk8250_runtime_resume(&pdev->dev);
253 	if (err)
254 		return err;
255 
256 	data->line = serial8250_register_8250_port(&uart);
257 	if (data->line < 0)
258 		return data->line;
259 
260 	pm_runtime_set_active(&pdev->dev);
261 	pm_runtime_enable(&pdev->dev);
262 
263 	return 0;
264 }
265 
mtk8250_remove(struct platform_device * pdev)266 static int mtk8250_remove(struct platform_device *pdev)
267 {
268 	struct mtk8250_data *data = platform_get_drvdata(pdev);
269 
270 	pm_runtime_get_sync(&pdev->dev);
271 
272 	serial8250_unregister_port(data->line);
273 	mtk8250_runtime_suspend(&pdev->dev);
274 
275 	pm_runtime_disable(&pdev->dev);
276 	pm_runtime_put_noidle(&pdev->dev);
277 
278 	return 0;
279 }
280 
281 #ifdef CONFIG_PM_SLEEP
mtk8250_suspend(struct device * dev)282 static int mtk8250_suspend(struct device *dev)
283 {
284 	struct mtk8250_data *data = dev_get_drvdata(dev);
285 
286 	serial8250_suspend_port(data->line);
287 
288 	return 0;
289 }
290 
mtk8250_resume(struct device * dev)291 static int mtk8250_resume(struct device *dev)
292 {
293 	struct mtk8250_data *data = dev_get_drvdata(dev);
294 
295 	serial8250_resume_port(data->line);
296 
297 	return 0;
298 }
299 #endif /* CONFIG_PM_SLEEP */
300 
301 static const struct dev_pm_ops mtk8250_pm_ops = {
302 	SET_SYSTEM_SLEEP_PM_OPS(mtk8250_suspend, mtk8250_resume)
303 	SET_RUNTIME_PM_OPS(mtk8250_runtime_suspend, mtk8250_runtime_resume,
304 				NULL)
305 };
306 
307 static const struct of_device_id mtk8250_of_match[] = {
308 	{ .compatible = "mediatek,mt6577-uart" },
309 	{ /* Sentinel */ }
310 };
311 MODULE_DEVICE_TABLE(of, mtk8250_of_match);
312 
313 static struct platform_driver mtk8250_platform_driver = {
314 	.driver = {
315 		.name		= "mt6577-uart",
316 		.pm		= &mtk8250_pm_ops,
317 		.of_match_table	= mtk8250_of_match,
318 	},
319 	.probe			= mtk8250_probe,
320 	.remove			= mtk8250_remove,
321 };
322 module_platform_driver(mtk8250_platform_driver);
323 
324 #ifdef CONFIG_SERIAL_8250_CONSOLE
early_mtk8250_setup(struct earlycon_device * device,const char * options)325 static int __init early_mtk8250_setup(struct earlycon_device *device,
326 					const char *options)
327 {
328 	if (!device->port.membase)
329 		return -ENODEV;
330 
331 	device->port.iotype = UPIO_MEM32;
332 
333 	return early_serial8250_setup(device, NULL);
334 }
335 
336 OF_EARLYCON_DECLARE(mtk8250, "mediatek,mt6577-uart", early_mtk8250_setup);
337 #endif
338 
339 MODULE_AUTHOR("Matthias Brugger");
340 MODULE_LICENSE("GPL");
341 MODULE_DESCRIPTION("Mediatek 8250 serial port driver");
342