blob: 12e15358554cb3479626b52798655af8f13263f3 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Carlo Caioneff7693d2014-08-17 12:49:49 +02002/*
3 * Based on meson_uart.c, by AMLOGIC, INC.
4 *
5 * Copyright (C) 2014 Carlo Caione <carlo@caione.org>
Carlo Caioneff7693d2014-08-17 12:49:49 +02006 */
7
8#include <linux/clk.h>
9#include <linux/console.h>
10#include <linux/delay.h>
11#include <linux/init.h>
12#include <linux/io.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/of.h>
16#include <linux/platform_device.h>
17#include <linux/serial.h>
18#include <linux/serial_core.h>
19#include <linux/tty.h>
20#include <linux/tty_flip.h>
21
22/* Register offsets */
23#define AML_UART_WFIFO 0x00
24#define AML_UART_RFIFO 0x04
25#define AML_UART_CONTROL 0x08
26#define AML_UART_STATUS 0x0c
27#define AML_UART_MISC 0x10
28#define AML_UART_REG5 0x14
29
30/* AML_UART_CONTROL bits */
31#define AML_UART_TX_EN BIT(12)
32#define AML_UART_RX_EN BIT(13)
Martin Blumenstingl44137e42017-11-17 19:18:00 +010033#define AML_UART_TWO_WIRE_EN BIT(15)
Martin Blumenstinglf8597222017-11-17 19:18:01 +010034#define AML_UART_STOP_BIT_LEN_MASK (0x03 << 16)
35#define AML_UART_STOP_BIT_1SB (0x00 << 16)
36#define AML_UART_STOP_BIT_2SB (0x01 << 16)
Martin Blumenstingl44137e42017-11-17 19:18:00 +010037#define AML_UART_PARITY_TYPE BIT(18)
38#define AML_UART_PARITY_EN BIT(19)
Carlo Caioneff7693d2014-08-17 12:49:49 +020039#define AML_UART_TX_RST BIT(22)
40#define AML_UART_RX_RST BIT(23)
Martin Blumenstingl44137e42017-11-17 19:18:00 +010041#define AML_UART_CLEAR_ERR BIT(24)
Carlo Caioneff7693d2014-08-17 12:49:49 +020042#define AML_UART_RX_INT_EN BIT(27)
43#define AML_UART_TX_INT_EN BIT(28)
44#define AML_UART_DATA_LEN_MASK (0x03 << 20)
45#define AML_UART_DATA_LEN_8BIT (0x00 << 20)
46#define AML_UART_DATA_LEN_7BIT (0x01 << 20)
47#define AML_UART_DATA_LEN_6BIT (0x02 << 20)
48#define AML_UART_DATA_LEN_5BIT (0x03 << 20)
49
50/* AML_UART_STATUS bits */
51#define AML_UART_PARITY_ERR BIT(16)
52#define AML_UART_FRAME_ERR BIT(17)
53#define AML_UART_TX_FIFO_WERR BIT(18)
54#define AML_UART_RX_EMPTY BIT(20)
55#define AML_UART_TX_FULL BIT(21)
56#define AML_UART_TX_EMPTY BIT(22)
Ben Dooks88679732015-11-18 14:41:13 +000057#define AML_UART_XMIT_BUSY BIT(25)
Carlo Caioneff7693d2014-08-17 12:49:49 +020058#define AML_UART_ERR (AML_UART_PARITY_ERR | \
59 AML_UART_FRAME_ERR | \
60 AML_UART_TX_FIFO_WERR)
61
Carlo Caioneff7693d2014-08-17 12:49:49 +020062/* AML_UART_MISC bits */
63#define AML_UART_XMIT_IRQ(c) (((c) & 0xff) << 8)
64#define AML_UART_RECV_IRQ(c) ((c) & 0xff)
65
66/* AML_UART_REG5 bits */
67#define AML_UART_BAUD_MASK 0x7fffff
68#define AML_UART_BAUD_USE BIT(23)
Andreas Färber146f3802016-02-08 13:49:42 +010069#define AML_UART_BAUD_XTAL BIT(24)
Carlo Caioneff7693d2014-08-17 12:49:49 +020070
Loys Olliviera26988e2019-01-14 17:54:26 +010071#define AML_UART_PORT_NUM 12
72#define AML_UART_PORT_OFFSET 6
Carlo Caioneff7693d2014-08-17 12:49:49 +020073#define AML_UART_DEV_NAME "ttyAML"
74
75
76static struct uart_driver meson_uart_driver;
77
78static struct uart_port *meson_ports[AML_UART_PORT_NUM];
79
80static void meson_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
81{
82}
83
84static unsigned int meson_uart_get_mctrl(struct uart_port *port)
85{
86 return TIOCM_CTS;
87}
88
89static unsigned int meson_uart_tx_empty(struct uart_port *port)
90{
91 u32 val;
92
93 val = readl(port->membase + AML_UART_STATUS);
Ben Dooks88679732015-11-18 14:41:13 +000094 val &= (AML_UART_TX_EMPTY | AML_UART_XMIT_BUSY);
95 return (val == AML_UART_TX_EMPTY) ? TIOCSER_TEMT : 0;
Carlo Caioneff7693d2014-08-17 12:49:49 +020096}
97
98static void meson_uart_stop_tx(struct uart_port *port)
99{
100 u32 val;
101
102 val = readl(port->membase + AML_UART_CONTROL);
Ben Dooks855ddca2015-11-18 14:41:15 +0000103 val &= ~AML_UART_TX_INT_EN;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200104 writel(val, port->membase + AML_UART_CONTROL);
105}
106
107static void meson_uart_stop_rx(struct uart_port *port)
108{
109 u32 val;
110
111 val = readl(port->membase + AML_UART_CONTROL);
112 val &= ~AML_UART_RX_EN;
113 writel(val, port->membase + AML_UART_CONTROL);
114}
115
116static void meson_uart_shutdown(struct uart_port *port)
117{
118 unsigned long flags;
119 u32 val;
120
121 free_irq(port->irq, port);
122
123 spin_lock_irqsave(&port->lock, flags);
124
125 val = readl(port->membase + AML_UART_CONTROL);
Ben Dooks855ddca2015-11-18 14:41:15 +0000126 val &= ~AML_UART_RX_EN;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200127 val &= ~(AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
128 writel(val, port->membase + AML_UART_CONTROL);
129
130 spin_unlock_irqrestore(&port->lock, flags);
131}
132
133static void meson_uart_start_tx(struct uart_port *port)
134{
135 struct circ_buf *xmit = &port->state->xmit;
136 unsigned int ch;
Ben Dooksf1dd05c2015-11-18 14:41:18 +0000137 u32 val;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200138
139 if (uart_tx_stopped(port)) {
140 meson_uart_stop_tx(port);
141 return;
142 }
143
144 while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
145 if (port->x_char) {
146 writel(port->x_char, port->membase + AML_UART_WFIFO);
147 port->icount.tx++;
148 port->x_char = 0;
149 continue;
150 }
151
152 if (uart_circ_empty(xmit))
153 break;
154
155 ch = xmit->buf[xmit->tail];
156 writel(ch, port->membase + AML_UART_WFIFO);
157 xmit->tail = (xmit->tail+1) & (SERIAL_XMIT_SIZE - 1);
158 port->icount.tx++;
159 }
160
Ben Dooksf1dd05c2015-11-18 14:41:18 +0000161 if (!uart_circ_empty(xmit)) {
162 val = readl(port->membase + AML_UART_CONTROL);
163 val |= AML_UART_TX_INT_EN;
164 writel(val, port->membase + AML_UART_CONTROL);
165 }
166
Carlo Caioneff7693d2014-08-17 12:49:49 +0200167 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
168 uart_write_wakeup(port);
169}
170
171static void meson_receive_chars(struct uart_port *port)
172{
173 struct tty_port *tport = &port->state->port;
174 char flag;
Yixun Lanb86ac222017-09-06 21:52:39 +0800175 u32 ostatus, status, ch, mode;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200176
177 do {
178 flag = TTY_NORMAL;
179 port->icount.rx++;
Yixun Lanb86ac222017-09-06 21:52:39 +0800180 ostatus = status = readl(port->membase + AML_UART_STATUS);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200181
182 if (status & AML_UART_ERR) {
183 if (status & AML_UART_TX_FIFO_WERR)
184 port->icount.overrun++;
185 else if (status & AML_UART_FRAME_ERR)
186 port->icount.frame++;
187 else if (status & AML_UART_PARITY_ERR)
188 port->icount.frame++;
189
190 mode = readl(port->membase + AML_UART_CONTROL);
191 mode |= AML_UART_CLEAR_ERR;
192 writel(mode, port->membase + AML_UART_CONTROL);
193
194 /* It doesn't clear to 0 automatically */
195 mode &= ~AML_UART_CLEAR_ERR;
196 writel(mode, port->membase + AML_UART_CONTROL);
197
198 status &= port->read_status_mask;
199 if (status & AML_UART_FRAME_ERR)
200 flag = TTY_FRAME;
201 else if (status & AML_UART_PARITY_ERR)
202 flag = TTY_PARITY;
203 }
204
205 ch = readl(port->membase + AML_UART_RFIFO);
206 ch &= 0xff;
207
Yixun Lanb86ac222017-09-06 21:52:39 +0800208 if ((ostatus & AML_UART_FRAME_ERR) && (ch == 0)) {
209 port->icount.brk++;
210 flag = TTY_BREAK;
211 if (uart_handle_break(port))
212 continue;
213 }
214
215 if (uart_handle_sysrq_char(port, ch))
216 continue;
217
Carlo Caioneff7693d2014-08-17 12:49:49 +0200218 if ((status & port->ignore_status_mask) == 0)
219 tty_insert_flip_char(tport, ch, flag);
220
221 if (status & AML_UART_TX_FIFO_WERR)
222 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
223
224 } while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY));
225
226 spin_unlock(&port->lock);
227 tty_flip_buffer_push(tport);
228 spin_lock(&port->lock);
229}
230
231static irqreturn_t meson_uart_interrupt(int irq, void *dev_id)
232{
233 struct uart_port *port = (struct uart_port *)dev_id;
234
235 spin_lock(&port->lock);
236
237 if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY))
238 meson_receive_chars(port);
239
Ben Dooks39469652015-11-18 14:41:19 +0000240 if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
241 if (readl(port->membase + AML_UART_CONTROL) & AML_UART_TX_INT_EN)
242 meson_uart_start_tx(port);
243 }
Carlo Caioneff7693d2014-08-17 12:49:49 +0200244
245 spin_unlock(&port->lock);
246
247 return IRQ_HANDLED;
248}
249
250static const char *meson_uart_type(struct uart_port *port)
251{
252 return (port->type == PORT_MESON) ? "meson_uart" : NULL;
253}
254
Ben Dooks00661dd2015-11-18 14:41:12 +0000255static void meson_uart_reset(struct uart_port *port)
Carlo Caioneff7693d2014-08-17 12:49:49 +0200256{
257 u32 val;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200258
259 val = readl(port->membase + AML_UART_CONTROL);
Martin Blumenstinglc0f0b8c2017-11-17 19:17:59 +0100260 val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLEAR_ERR);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200261 writel(val, port->membase + AML_UART_CONTROL);
262
Martin Blumenstinglc0f0b8c2017-11-17 19:17:59 +0100263 val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLEAR_ERR);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200264 writel(val, port->membase + AML_UART_CONTROL);
Ben Dooks00661dd2015-11-18 14:41:12 +0000265}
266
267static int meson_uart_startup(struct uart_port *port)
268{
269 u32 val;
270 int ret = 0;
271
272 val = readl(port->membase + AML_UART_CONTROL);
Martin Blumenstinglc0f0b8c2017-11-17 19:17:59 +0100273 val |= AML_UART_CLEAR_ERR;
Ben Dooks00661dd2015-11-18 14:41:12 +0000274 writel(val, port->membase + AML_UART_CONTROL);
Martin Blumenstinglc0f0b8c2017-11-17 19:17:59 +0100275 val &= ~AML_UART_CLEAR_ERR;
Ben Dooks00661dd2015-11-18 14:41:12 +0000276 writel(val, port->membase + AML_UART_CONTROL);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200277
278 val |= (AML_UART_RX_EN | AML_UART_TX_EN);
279 writel(val, port->membase + AML_UART_CONTROL);
280
281 val |= (AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
282 writel(val, port->membase + AML_UART_CONTROL);
283
284 val = (AML_UART_RECV_IRQ(1) | AML_UART_XMIT_IRQ(port->fifosize / 2));
285 writel(val, port->membase + AML_UART_MISC);
286
287 ret = request_irq(port->irq, meson_uart_interrupt, 0,
Heiner Kallweit8b7a6b22017-04-19 22:18:16 +0200288 port->name, port);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200289
290 return ret;
291}
292
293static void meson_uart_change_speed(struct uart_port *port, unsigned long baud)
294{
295 u32 val;
296
Ben Dooksf1f5c142015-11-18 14:41:16 +0000297 while (!meson_uart_tx_empty(port))
Carlo Caioneff7693d2014-08-17 12:49:49 +0200298 cpu_relax();
299
Andreas Färber146f3802016-02-08 13:49:42 +0100300 if (port->uartclk == 24000000) {
301 val = ((port->uartclk / 3) / baud) - 1;
302 val |= AML_UART_BAUD_XTAL;
303 } else {
304 val = ((port->uartclk * 10 / (baud * 4) + 5) / 10) - 1;
305 }
Carlo Caioneff7693d2014-08-17 12:49:49 +0200306 val |= AML_UART_BAUD_USE;
307 writel(val, port->membase + AML_UART_REG5);
308}
309
310static void meson_uart_set_termios(struct uart_port *port,
311 struct ktermios *termios,
312 struct ktermios *old)
313{
314 unsigned int cflags, iflags, baud;
315 unsigned long flags;
316 u32 val;
317
318 spin_lock_irqsave(&port->lock, flags);
319
320 cflags = termios->c_cflag;
321 iflags = termios->c_iflag;
322
323 val = readl(port->membase + AML_UART_CONTROL);
324
325 val &= ~AML_UART_DATA_LEN_MASK;
326 switch (cflags & CSIZE) {
327 case CS8:
328 val |= AML_UART_DATA_LEN_8BIT;
329 break;
330 case CS7:
331 val |= AML_UART_DATA_LEN_7BIT;
332 break;
333 case CS6:
334 val |= AML_UART_DATA_LEN_6BIT;
335 break;
336 case CS5:
337 val |= AML_UART_DATA_LEN_5BIT;
338 break;
339 }
340
341 if (cflags & PARENB)
342 val |= AML_UART_PARITY_EN;
343 else
344 val &= ~AML_UART_PARITY_EN;
345
346 if (cflags & PARODD)
347 val |= AML_UART_PARITY_TYPE;
348 else
349 val &= ~AML_UART_PARITY_TYPE;
350
Martin Blumenstinglf8597222017-11-17 19:18:01 +0100351 val &= ~AML_UART_STOP_BIT_LEN_MASK;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200352 if (cflags & CSTOPB)
Martin Blumenstinglf8597222017-11-17 19:18:01 +0100353 val |= AML_UART_STOP_BIT_2SB;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200354 else
Martin Blumenstinglf8597222017-11-17 19:18:01 +0100355 val |= AML_UART_STOP_BIT_1SB;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200356
357 if (cflags & CRTSCTS)
358 val &= ~AML_UART_TWO_WIRE_EN;
359 else
360 val |= AML_UART_TWO_WIRE_EN;
361
362 writel(val, port->membase + AML_UART_CONTROL);
363
Thomas Rohloff9b11f192017-11-05 20:36:30 +0100364 baud = uart_get_baud_rate(port, termios, old, 50, 4000000);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200365 meson_uart_change_speed(port, baud);
366
367 port->read_status_mask = AML_UART_TX_FIFO_WERR;
368 if (iflags & INPCK)
369 port->read_status_mask |= AML_UART_PARITY_ERR |
370 AML_UART_FRAME_ERR;
371
372 port->ignore_status_mask = 0;
373 if (iflags & IGNPAR)
374 port->ignore_status_mask |= AML_UART_PARITY_ERR |
375 AML_UART_FRAME_ERR;
376
377 uart_update_timeout(port, termios->c_cflag, baud);
378 spin_unlock_irqrestore(&port->lock, flags);
379}
380
381static int meson_uart_verify_port(struct uart_port *port,
382 struct serial_struct *ser)
383{
384 int ret = 0;
385
386 if (port->type != PORT_MESON)
387 ret = -EINVAL;
388 if (port->irq != ser->irq)
389 ret = -EINVAL;
390 if (ser->baud_base < 9600)
391 ret = -EINVAL;
392 return ret;
393}
394
395static void meson_uart_release_port(struct uart_port *port)
396{
Heiner Kallweit1b1ecaa2017-04-19 22:17:50 +0200397 devm_iounmap(port->dev, port->membase);
398 port->membase = NULL;
399 devm_release_mem_region(port->dev, port->mapbase, port->mapsize);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200400}
401
402static int meson_uart_request_port(struct uart_port *port)
403{
Heiner Kallweitff3b9ca2017-04-19 22:17:47 +0200404 if (!devm_request_mem_region(port->dev, port->mapbase, port->mapsize,
Carlo Caioneff7693d2014-08-17 12:49:49 +0200405 dev_name(port->dev))) {
406 dev_err(port->dev, "Memory region busy\n");
407 return -EBUSY;
408 }
409
Heiner Kallweit1b1ecaa2017-04-19 22:17:50 +0200410 port->membase = devm_ioremap_nocache(port->dev, port->mapbase,
411 port->mapsize);
412 if (!port->membase)
413 return -ENOMEM;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200414
415 return 0;
416}
417
418static void meson_uart_config_port(struct uart_port *port, int flags)
419{
420 if (flags & UART_CONFIG_TYPE) {
421 port->type = PORT_MESON;
422 meson_uart_request_port(port);
423 }
424}
425
Julia Lawall921469f2017-08-13 08:21:40 +0200426static const struct uart_ops meson_uart_ops = {
Carlo Caioneff7693d2014-08-17 12:49:49 +0200427 .set_mctrl = meson_uart_set_mctrl,
428 .get_mctrl = meson_uart_get_mctrl,
429 .tx_empty = meson_uart_tx_empty,
430 .start_tx = meson_uart_start_tx,
431 .stop_tx = meson_uart_stop_tx,
432 .stop_rx = meson_uart_stop_rx,
433 .startup = meson_uart_startup,
434 .shutdown = meson_uart_shutdown,
435 .set_termios = meson_uart_set_termios,
436 .type = meson_uart_type,
437 .config_port = meson_uart_config_port,
438 .request_port = meson_uart_request_port,
439 .release_port = meson_uart_release_port,
440 .verify_port = meson_uart_verify_port,
441};
442
443#ifdef CONFIG_SERIAL_MESON_CONSOLE
Arnd Bergmann5fa4acc2017-05-22 15:37:03 +0200444static void meson_uart_enable_tx_engine(struct uart_port *port)
445{
446 u32 val;
447
448 val = readl(port->membase + AML_UART_CONTROL);
449 val |= AML_UART_TX_EN;
450 writel(val, port->membase + AML_UART_CONTROL);
451}
Carlo Caioneff7693d2014-08-17 12:49:49 +0200452
453static void meson_console_putchar(struct uart_port *port, int ch)
454{
455 if (!port->membase)
456 return;
457
458 while (readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)
459 cpu_relax();
460 writel(ch, port->membase + AML_UART_WFIFO);
461}
462
Andreas Färber736d5532016-03-06 12:21:24 +0100463static void meson_serial_port_write(struct uart_port *port, const char *s,
464 u_int count)
Carlo Caioneff7693d2014-08-17 12:49:49 +0200465{
Carlo Caioneff7693d2014-08-17 12:49:49 +0200466 unsigned long flags;
467 int locked;
Ben Dooks2561f062015-11-18 14:41:17 +0000468 u32 val, tmp;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200469
Carlo Caioneff7693d2014-08-17 12:49:49 +0200470 local_irq_save(flags);
471 if (port->sysrq) {
472 locked = 0;
473 } else if (oops_in_progress) {
474 locked = spin_trylock(&port->lock);
475 } else {
476 spin_lock(&port->lock);
477 locked = 1;
478 }
479
Ben Dooks41788f02015-11-18 14:41:14 +0000480 val = readl(port->membase + AML_UART_CONTROL);
Ben Dooks2561f062015-11-18 14:41:17 +0000481 tmp = val & ~(AML_UART_TX_INT_EN | AML_UART_RX_INT_EN);
482 writel(tmp, port->membase + AML_UART_CONTROL);
Ben Dooks41788f02015-11-18 14:41:14 +0000483
Carlo Caioneff7693d2014-08-17 12:49:49 +0200484 uart_console_write(port, s, count, meson_console_putchar);
Ben Dooks2561f062015-11-18 14:41:17 +0000485 writel(val, port->membase + AML_UART_CONTROL);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200486
487 if (locked)
488 spin_unlock(&port->lock);
489 local_irq_restore(flags);
490}
491
Andreas Färber736d5532016-03-06 12:21:24 +0100492static void meson_serial_console_write(struct console *co, const char *s,
493 u_int count)
494{
495 struct uart_port *port;
496
497 port = meson_ports[co->index];
498 if (!port)
499 return;
500
501 meson_serial_port_write(port, s, count);
502}
503
Carlo Caioneff7693d2014-08-17 12:49:49 +0200504static int meson_serial_console_setup(struct console *co, char *options)
505{
506 struct uart_port *port;
507 int baud = 115200;
508 int bits = 8;
509 int parity = 'n';
510 int flow = 'n';
511
512 if (co->index < 0 || co->index >= AML_UART_PORT_NUM)
513 return -EINVAL;
514
515 port = meson_ports[co->index];
516 if (!port || !port->membase)
517 return -ENODEV;
518
Heiner Kallweitba50f1d2017-04-19 22:17:44 +0200519 meson_uart_enable_tx_engine(port);
520
Carlo Caioneff7693d2014-08-17 12:49:49 +0200521 if (options)
522 uart_parse_options(options, &baud, &parity, &bits, &flow);
523
524 return uart_set_options(port, co, baud, parity, bits, flow);
525}
526
527static struct console meson_serial_console = {
528 .name = AML_UART_DEV_NAME,
529 .write = meson_serial_console_write,
530 .device = uart_console_device,
531 .setup = meson_serial_console_setup,
532 .flags = CON_PRINTBUFFER,
533 .index = -1,
534 .data = &meson_uart_driver,
535};
536
537static int __init meson_serial_console_init(void)
538{
539 register_console(&meson_serial_console);
540 return 0;
541}
542console_initcall(meson_serial_console_init);
543
Andreas Färber736d5532016-03-06 12:21:24 +0100544static void meson_serial_early_console_write(struct console *co,
545 const char *s,
546 u_int count)
547{
548 struct earlycon_device *dev = co->data;
549
550 meson_serial_port_write(&dev->port, s, count);
551}
552
553static int __init
554meson_serial_early_console_setup(struct earlycon_device *device, const char *opt)
555{
556 if (!device->port.membase)
557 return -ENODEV;
558
Heiner Kallweitba50f1d2017-04-19 22:17:44 +0200559 meson_uart_enable_tx_engine(&device->port);
Andreas Färber736d5532016-03-06 12:21:24 +0100560 device->con->write = meson_serial_early_console_write;
561 return 0;
562}
Helmut Klein9f60e0e2017-06-14 10:29:15 +0200563/* Legacy bindings, should be removed when no more used */
Andreas Färber736d5532016-03-06 12:21:24 +0100564OF_EARLYCON_DECLARE(meson, "amlogic,meson-uart",
565 meson_serial_early_console_setup);
Helmut Klein9f60e0e2017-06-14 10:29:15 +0200566/* Stable bindings */
567OF_EARLYCON_DECLARE(meson, "amlogic,meson-ao-uart",
568 meson_serial_early_console_setup);
Andreas Färber736d5532016-03-06 12:21:24 +0100569
Carlo Caioneff7693d2014-08-17 12:49:49 +0200570#define MESON_SERIAL_CONSOLE (&meson_serial_console)
571#else
572#define MESON_SERIAL_CONSOLE NULL
573#endif
574
575static struct uart_driver meson_uart_driver = {
576 .owner = THIS_MODULE,
577 .driver_name = "meson_uart",
578 .dev_name = AML_UART_DEV_NAME,
579 .nr = AML_UART_PORT_NUM,
580 .cons = MESON_SERIAL_CONSOLE,
581};
582
Helmut Klein9f60e0e2017-06-14 10:29:15 +0200583static inline struct clk *meson_uart_probe_clock(struct device *dev,
584 const char *id)
585{
586 struct clk *clk = NULL;
587 int ret;
588
589 clk = devm_clk_get(dev, id);
590 if (IS_ERR(clk))
591 return clk;
592
593 ret = clk_prepare_enable(clk);
594 if (ret) {
595 dev_err(dev, "couldn't enable clk\n");
596 return ERR_PTR(ret);
597 }
598
599 devm_add_action_or_reset(dev,
600 (void(*)(void *))clk_disable_unprepare,
601 clk);
602
603 return clk;
604}
605
606/*
607 * This function gets clocks in the legacy non-stable DT bindings.
608 * This code will be remove once all the platforms switch to the
609 * new DT bindings.
610 */
611static int meson_uart_probe_clocks_legacy(struct platform_device *pdev,
612 struct uart_port *port)
613{
614 struct clk *clk = NULL;
615
616 clk = meson_uart_probe_clock(&pdev->dev, NULL);
617 if (IS_ERR(clk))
618 return PTR_ERR(clk);
619
620 port->uartclk = clk_get_rate(clk);
621
622 return 0;
623}
624
625static int meson_uart_probe_clocks(struct platform_device *pdev,
626 struct uart_port *port)
627{
628 struct clk *clk_xtal = NULL;
629 struct clk *clk_pclk = NULL;
630 struct clk *clk_baud = NULL;
631
632 clk_pclk = meson_uart_probe_clock(&pdev->dev, "pclk");
633 if (IS_ERR(clk_pclk))
634 return PTR_ERR(clk_pclk);
635
636 clk_xtal = meson_uart_probe_clock(&pdev->dev, "xtal");
637 if (IS_ERR(clk_xtal))
638 return PTR_ERR(clk_xtal);
639
640 clk_baud = meson_uart_probe_clock(&pdev->dev, "baud");
641 if (IS_ERR(clk_baud))
642 return PTR_ERR(clk_baud);
643
644 port->uartclk = clk_get_rate(clk_baud);
645
646 return 0;
647}
648
Carlo Caioneff7693d2014-08-17 12:49:49 +0200649static int meson_uart_probe(struct platform_device *pdev)
650{
651 struct resource *res_mem, *res_irq;
652 struct uart_port *port;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200653 int ret = 0;
Loys Olliviera26988e2019-01-14 17:54:26 +0100654 int id = -1;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200655
656 if (pdev->dev.of_node)
657 pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
658
Loys Olliviera26988e2019-01-14 17:54:26 +0100659 if (pdev->id < 0) {
660 for (id = AML_UART_PORT_OFFSET; id < AML_UART_PORT_NUM; id++) {
661 if (!meson_ports[id]) {
662 pdev->id = id;
663 break;
664 }
665 }
666 }
667
Carlo Caioneff7693d2014-08-17 12:49:49 +0200668 if (pdev->id < 0 || pdev->id >= AML_UART_PORT_NUM)
669 return -EINVAL;
670
671 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
672 if (!res_mem)
673 return -ENODEV;
674
675 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
676 if (!res_irq)
677 return -ENODEV;
678
679 if (meson_ports[pdev->id]) {
680 dev_err(&pdev->dev, "port %d already allocated\n", pdev->id);
681 return -EBUSY;
682 }
683
684 port = devm_kzalloc(&pdev->dev, sizeof(struct uart_port), GFP_KERNEL);
685 if (!port)
686 return -ENOMEM;
687
Helmut Klein9f60e0e2017-06-14 10:29:15 +0200688 /* Use legacy way until all platforms switch to new bindings */
689 if (of_device_is_compatible(pdev->dev.of_node, "amlogic,meson-uart"))
690 ret = meson_uart_probe_clocks_legacy(pdev, port);
691 else
692 ret = meson_uart_probe_clocks(pdev, port);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200693
Helmut Klein9f60e0e2017-06-14 10:29:15 +0200694 if (ret)
695 return ret;
696
Carlo Caioneff7693d2014-08-17 12:49:49 +0200697 port->iotype = UPIO_MEM;
698 port->mapbase = res_mem->start;
Heiner Kallweitff3b9ca2017-04-19 22:17:47 +0200699 port->mapsize = resource_size(res_mem);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200700 port->irq = res_irq->start;
Heiner Kallweit1b1ecaa2017-04-19 22:17:50 +0200701 port->flags = UPF_BOOT_AUTOCONF | UPF_LOW_LATENCY;
Dmitry Safonovdca3ac82019-12-13 00:06:20 +0000702 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MESON_CONSOLE);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200703 port->dev = &pdev->dev;
704 port->line = pdev->id;
705 port->type = PORT_MESON;
706 port->x_char = 0;
707 port->ops = &meson_uart_ops;
708 port->fifosize = 64;
709
710 meson_ports[pdev->id] = port;
711 platform_set_drvdata(pdev, port);
712
Ben Dooks00661dd2015-11-18 14:41:12 +0000713 /* reset port before registering (and possibly registering console) */
714 if (meson_uart_request_port(port) >= 0) {
715 meson_uart_reset(port);
716 meson_uart_release_port(port);
717 }
718
Carlo Caioneff7693d2014-08-17 12:49:49 +0200719 ret = uart_add_one_port(&meson_uart_driver, port);
720 if (ret)
721 meson_ports[pdev->id] = NULL;
722
723 return ret;
724}
725
726static int meson_uart_remove(struct platform_device *pdev)
727{
728 struct uart_port *port;
729
730 port = platform_get_drvdata(pdev);
731 uart_remove_one_port(&meson_uart_driver, port);
732 meson_ports[pdev->id] = NULL;
733
734 return 0;
735}
736
Carlo Caioneff7693d2014-08-17 12:49:49 +0200737static const struct of_device_id meson_uart_dt_match[] = {
Helmut Klein9f60e0e2017-06-14 10:29:15 +0200738 /* Legacy bindings, should be removed when no more used */
Carlo Caioneff7693d2014-08-17 12:49:49 +0200739 { .compatible = "amlogic,meson-uart" },
Helmut Klein9f60e0e2017-06-14 10:29:15 +0200740 /* Stable bindings */
741 { .compatible = "amlogic,meson6-uart" },
742 { .compatible = "amlogic,meson8-uart" },
743 { .compatible = "amlogic,meson8b-uart" },
744 { .compatible = "amlogic,meson-gx-uart" },
Carlo Caioneff7693d2014-08-17 12:49:49 +0200745 { /* sentinel */ },
746};
747MODULE_DEVICE_TABLE(of, meson_uart_dt_match);
748
749static struct platform_driver meson_uart_platform_driver = {
750 .probe = meson_uart_probe,
751 .remove = meson_uart_remove,
752 .driver = {
Carlo Caioneff7693d2014-08-17 12:49:49 +0200753 .name = "meson_uart",
754 .of_match_table = meson_uart_dt_match,
755 },
756};
757
758static int __init meson_uart_init(void)
759{
760 int ret;
761
762 ret = uart_register_driver(&meson_uart_driver);
763 if (ret)
764 return ret;
765
766 ret = platform_driver_register(&meson_uart_platform_driver);
767 if (ret)
768 uart_unregister_driver(&meson_uart_driver);
769
770 return ret;
771}
772
773static void __exit meson_uart_exit(void)
774{
775 platform_driver_unregister(&meson_uart_platform_driver);
776 uart_unregister_driver(&meson_uart_driver);
777}
778
779module_init(meson_uart_init);
780module_exit(meson_uart_exit);
781
782MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
783MODULE_DESCRIPTION("Amlogic Meson serial port driver");
784MODULE_LICENSE("GPL v2");