Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Based on meson_uart.c, by AMLOGIC, INC.
4 *
5 * Copyright (C) 2014 Carlo Caione <carlo@caione.org>
6 */
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/iopoll.h>
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/of.h>
17#include <linux/platform_device.h>
18#include <linux/serial.h>
19#include <linux/serial_core.h>
20#include <linux/tty.h>
21#include <linux/tty_flip.h>
22
23/* Register offsets */
24#define AML_UART_WFIFO 0x00
25#define AML_UART_RFIFO 0x04
26#define AML_UART_CONTROL 0x08
27#define AML_UART_STATUS 0x0c
28#define AML_UART_MISC 0x10
29#define AML_UART_REG5 0x14
30
31/* AML_UART_CONTROL bits */
32#define AML_UART_TX_EN BIT(12)
33#define AML_UART_RX_EN BIT(13)
34#define AML_UART_TWO_WIRE_EN BIT(15)
35#define AML_UART_STOP_BIT_LEN_MASK (0x03 << 16)
36#define AML_UART_STOP_BIT_1SB (0x00 << 16)
37#define AML_UART_STOP_BIT_2SB (0x01 << 16)
38#define AML_UART_PARITY_TYPE BIT(18)
39#define AML_UART_PARITY_EN BIT(19)
40#define AML_UART_TX_RST BIT(22)
41#define AML_UART_RX_RST BIT(23)
42#define AML_UART_CLEAR_ERR BIT(24)
43#define AML_UART_RX_INT_EN BIT(27)
44#define AML_UART_TX_INT_EN BIT(28)
45#define AML_UART_DATA_LEN_MASK (0x03 << 20)
46#define AML_UART_DATA_LEN_8BIT (0x00 << 20)
47#define AML_UART_DATA_LEN_7BIT (0x01 << 20)
48#define AML_UART_DATA_LEN_6BIT (0x02 << 20)
49#define AML_UART_DATA_LEN_5BIT (0x03 << 20)
50
51/* AML_UART_STATUS bits */
52#define AML_UART_PARITY_ERR BIT(16)
53#define AML_UART_FRAME_ERR BIT(17)
54#define AML_UART_TX_FIFO_WERR BIT(18)
55#define AML_UART_RX_EMPTY BIT(20)
56#define AML_UART_TX_FULL BIT(21)
57#define AML_UART_TX_EMPTY BIT(22)
58#define AML_UART_XMIT_BUSY BIT(25)
59#define AML_UART_ERR (AML_UART_PARITY_ERR | \
60 AML_UART_FRAME_ERR | \
61 AML_UART_TX_FIFO_WERR)
62
63/* AML_UART_MISC bits */
64#define AML_UART_XMIT_IRQ(c) (((c) & 0xff) << 8)
65#define AML_UART_RECV_IRQ(c) ((c) & 0xff)
66
67/* AML_UART_REG5 bits */
68#define AML_UART_BAUD_MASK 0x7fffff
69#define AML_UART_BAUD_USE BIT(23)
70#define AML_UART_BAUD_XTAL BIT(24)
71#define AML_UART_BAUD_XTAL_DIV2 BIT(27)
72
73#define AML_UART_PORT_NUM 12
74#define AML_UART_PORT_OFFSET 6
75#define AML_UART_DEV_NAME "ttyAML"
76
77#define AML_UART_POLL_USEC 5
78#define AML_UART_TIMEOUT_USEC 10000
79
80static struct uart_driver meson_uart_driver;
81
82static struct uart_port *meson_ports[AML_UART_PORT_NUM];
83
84struct meson_uart_data {
85 bool has_xtal_div2;
86};
87
88static void meson_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
89{
90}
91
92static unsigned int meson_uart_get_mctrl(struct uart_port *port)
93{
94 return TIOCM_CTS;
95}
96
97static unsigned int meson_uart_tx_empty(struct uart_port *port)
98{
99 u32 val;
100
101 val = readl(port->membase + AML_UART_STATUS);
102 val &= (AML_UART_TX_EMPTY | AML_UART_XMIT_BUSY);
103 return (val == AML_UART_TX_EMPTY) ? TIOCSER_TEMT : 0;
104}
105
106static void meson_uart_stop_tx(struct uart_port *port)
107{
108 u32 val;
109
110 val = readl(port->membase + AML_UART_CONTROL);
111 val &= ~AML_UART_TX_INT_EN;
112 writel(val, port->membase + AML_UART_CONTROL);
113}
114
115static void meson_uart_stop_rx(struct uart_port *port)
116{
117 u32 val;
118
119 val = readl(port->membase + AML_UART_CONTROL);
120 val &= ~AML_UART_RX_EN;
121 writel(val, port->membase + AML_UART_CONTROL);
122}
123
124static void meson_uart_shutdown(struct uart_port *port)
125{
126 unsigned long flags;
127 u32 val;
128
129 free_irq(port->irq, port);
130
131 spin_lock_irqsave(&port->lock, flags);
132
133 val = readl(port->membase + AML_UART_CONTROL);
134 val &= ~AML_UART_RX_EN;
135 val &= ~(AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
136 writel(val, port->membase + AML_UART_CONTROL);
137
138 spin_unlock_irqrestore(&port->lock, flags);
139}
140
141static void meson_uart_start_tx(struct uart_port *port)
142{
143 struct circ_buf *xmit = &port->state->xmit;
144 unsigned int ch;
145 u32 val;
146
147 if (uart_tx_stopped(port)) {
148 meson_uart_stop_tx(port);
149 return;
150 }
151
152 while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
153 if (port->x_char) {
154 writel(port->x_char, port->membase + AML_UART_WFIFO);
155 port->icount.tx++;
156 port->x_char = 0;
157 continue;
158 }
159
160 if (uart_circ_empty(xmit))
161 break;
162
163 ch = xmit->buf[xmit->tail];
164 writel(ch, port->membase + AML_UART_WFIFO);
165 uart_xmit_advance(port, 1);
166 }
167
168 if (!uart_circ_empty(xmit)) {
169 val = readl(port->membase + AML_UART_CONTROL);
170 val |= AML_UART_TX_INT_EN;
171 writel(val, port->membase + AML_UART_CONTROL);
172 }
173
174 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
175 uart_write_wakeup(port);
176}
177
178static void meson_receive_chars(struct uart_port *port)
179{
180 struct tty_port *tport = &port->state->port;
181 char flag;
182 u32 ostatus, status, ch, mode;
183
184 do {
185 flag = TTY_NORMAL;
186 port->icount.rx++;
187 ostatus = status = readl(port->membase + AML_UART_STATUS);
188
189 if (status & AML_UART_ERR) {
190 if (status & AML_UART_TX_FIFO_WERR)
191 port->icount.overrun++;
192 else if (status & AML_UART_FRAME_ERR)
193 port->icount.frame++;
194 else if (status & AML_UART_PARITY_ERR)
195 port->icount.frame++;
196
197 mode = readl(port->membase + AML_UART_CONTROL);
198 mode |= AML_UART_CLEAR_ERR;
199 writel(mode, port->membase + AML_UART_CONTROL);
200
201 /* It doesn't clear to 0 automatically */
202 mode &= ~AML_UART_CLEAR_ERR;
203 writel(mode, port->membase + AML_UART_CONTROL);
204
205 status &= port->read_status_mask;
206 if (status & AML_UART_FRAME_ERR)
207 flag = TTY_FRAME;
208 else if (status & AML_UART_PARITY_ERR)
209 flag = TTY_PARITY;
210 }
211
212 ch = readl(port->membase + AML_UART_RFIFO);
213 ch &= 0xff;
214
215 if ((ostatus & AML_UART_FRAME_ERR) && (ch == 0)) {
216 port->icount.brk++;
217 flag = TTY_BREAK;
218 if (uart_handle_break(port))
219 continue;
220 }
221
222 if (uart_handle_sysrq_char(port, ch))
223 continue;
224
225 if ((status & port->ignore_status_mask) == 0)
226 tty_insert_flip_char(tport, ch, flag);
227
228 if (status & AML_UART_TX_FIFO_WERR)
229 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
230
231 } while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY));
232
233 tty_flip_buffer_push(tport);
234}
235
236static irqreturn_t meson_uart_interrupt(int irq, void *dev_id)
237{
238 struct uart_port *port = (struct uart_port *)dev_id;
239
240 spin_lock(&port->lock);
241
242 if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY))
243 meson_receive_chars(port);
244
245 if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
246 if (readl(port->membase + AML_UART_CONTROL) & AML_UART_TX_INT_EN)
247 meson_uart_start_tx(port);
248 }
249
250 spin_unlock(&port->lock);
251
252 return IRQ_HANDLED;
253}
254
255static const char *meson_uart_type(struct uart_port *port)
256{
257 return (port->type == PORT_MESON) ? "meson_uart" : NULL;
258}
259
260/*
261 * This function is called only from probe() using a temporary io mapping
262 * in order to perform a reset before setting up the device. Since the
263 * temporarily mapped region was successfully requested, there can be no
264 * console on this port at this time. Hence it is not necessary for this
265 * function to acquire the port->lock. (Since there is no console on this
266 * port at this time, the port->lock is not initialized yet.)
267 */
268static void meson_uart_reset(struct uart_port *port)
269{
270 u32 val;
271
272 val = readl(port->membase + AML_UART_CONTROL);
273 val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLEAR_ERR);
274 writel(val, port->membase + AML_UART_CONTROL);
275
276 val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLEAR_ERR);
277 writel(val, port->membase + AML_UART_CONTROL);
278}
279
280static int meson_uart_startup(struct uart_port *port)
281{
282 unsigned long flags;
283 u32 val;
284 int ret = 0;
285
286 spin_lock_irqsave(&port->lock, flags);
287
288 val = readl(port->membase + AML_UART_CONTROL);
289 val |= AML_UART_CLEAR_ERR;
290 writel(val, port->membase + AML_UART_CONTROL);
291 val &= ~AML_UART_CLEAR_ERR;
292 writel(val, port->membase + AML_UART_CONTROL);
293
294 val |= (AML_UART_RX_EN | AML_UART_TX_EN);
295 writel(val, port->membase + AML_UART_CONTROL);
296
297 val |= (AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
298 writel(val, port->membase + AML_UART_CONTROL);
299
300 val = (AML_UART_RECV_IRQ(1) | AML_UART_XMIT_IRQ(port->fifosize / 2));
301 writel(val, port->membase + AML_UART_MISC);
302
303 spin_unlock_irqrestore(&port->lock, flags);
304
305 ret = request_irq(port->irq, meson_uart_interrupt, 0,
306 port->name, port);
307
308 return ret;
309}
310
311static void meson_uart_change_speed(struct uart_port *port, unsigned long baud)
312{
313 const struct meson_uart_data *private_data = port->private_data;
314 u32 val = 0;
315
316 while (!meson_uart_tx_empty(port))
317 cpu_relax();
318
319 if (port->uartclk == 24000000) {
320 unsigned int xtal_div = 3;
321
322 if (private_data && private_data->has_xtal_div2) {
323 xtal_div = 2;
324 val |= AML_UART_BAUD_XTAL_DIV2;
325 }
326 val |= DIV_ROUND_CLOSEST(port->uartclk / xtal_div, baud) - 1;
327 val |= AML_UART_BAUD_XTAL;
328 } else {
329 val = DIV_ROUND_CLOSEST(port->uartclk / 4, baud) - 1;
330 }
331 val |= AML_UART_BAUD_USE;
332 writel(val, port->membase + AML_UART_REG5);
333}
334
335static void meson_uart_set_termios(struct uart_port *port,
336 struct ktermios *termios,
337 const struct ktermios *old)
338{
339 unsigned int cflags, iflags, baud;
340 unsigned long flags;
341 u32 val;
342
343 spin_lock_irqsave(&port->lock, flags);
344
345 cflags = termios->c_cflag;
346 iflags = termios->c_iflag;
347
348 val = readl(port->membase + AML_UART_CONTROL);
349
350 val &= ~AML_UART_DATA_LEN_MASK;
351 switch (cflags & CSIZE) {
352 case CS8:
353 val |= AML_UART_DATA_LEN_8BIT;
354 break;
355 case CS7:
356 val |= AML_UART_DATA_LEN_7BIT;
357 break;
358 case CS6:
359 val |= AML_UART_DATA_LEN_6BIT;
360 break;
361 case CS5:
362 val |= AML_UART_DATA_LEN_5BIT;
363 break;
364 }
365
366 if (cflags & PARENB)
367 val |= AML_UART_PARITY_EN;
368 else
369 val &= ~AML_UART_PARITY_EN;
370
371 if (cflags & PARODD)
372 val |= AML_UART_PARITY_TYPE;
373 else
374 val &= ~AML_UART_PARITY_TYPE;
375
376 val &= ~AML_UART_STOP_BIT_LEN_MASK;
377 if (cflags & CSTOPB)
378 val |= AML_UART_STOP_BIT_2SB;
379 else
380 val |= AML_UART_STOP_BIT_1SB;
381
382 if (cflags & CRTSCTS)
383 val &= ~AML_UART_TWO_WIRE_EN;
384 else
385 val |= AML_UART_TWO_WIRE_EN;
386
387 writel(val, port->membase + AML_UART_CONTROL);
388
389 baud = uart_get_baud_rate(port, termios, old, 50, 4000000);
390 meson_uart_change_speed(port, baud);
391
392 port->read_status_mask = AML_UART_TX_FIFO_WERR;
393 if (iflags & INPCK)
394 port->read_status_mask |= AML_UART_PARITY_ERR |
395 AML_UART_FRAME_ERR;
396
397 port->ignore_status_mask = 0;
398 if (iflags & IGNPAR)
399 port->ignore_status_mask |= AML_UART_PARITY_ERR |
400 AML_UART_FRAME_ERR;
401
402 uart_update_timeout(port, termios->c_cflag, baud);
403 spin_unlock_irqrestore(&port->lock, flags);
404}
405
406static int meson_uart_verify_port(struct uart_port *port,
407 struct serial_struct *ser)
408{
409 int ret = 0;
410
411 if (port->type != PORT_MESON)
412 ret = -EINVAL;
413 if (port->irq != ser->irq)
414 ret = -EINVAL;
415 if (ser->baud_base < 9600)
416 ret = -EINVAL;
417 return ret;
418}
419
420static void meson_uart_release_port(struct uart_port *port)
421{
422 devm_iounmap(port->dev, port->membase);
423 port->membase = NULL;
424 devm_release_mem_region(port->dev, port->mapbase, port->mapsize);
425}
426
427static int meson_uart_request_port(struct uart_port *port)
428{
429 if (!devm_request_mem_region(port->dev, port->mapbase, port->mapsize,
430 dev_name(port->dev))) {
431 dev_err(port->dev, "Memory region busy\n");
432 return -EBUSY;
433 }
434
435 port->membase = devm_ioremap(port->dev, port->mapbase,
436 port->mapsize);
437 if (!port->membase)
438 return -ENOMEM;
439
440 return 0;
441}
442
443static void meson_uart_config_port(struct uart_port *port, int flags)
444{
445 if (flags & UART_CONFIG_TYPE) {
446 port->type = PORT_MESON;
447 meson_uart_request_port(port);
448 }
449}
450
451#ifdef CONFIG_CONSOLE_POLL
452/*
453 * Console polling routines for writing and reading from the uart while
454 * in an interrupt or debug context (i.e. kgdb).
455 */
456
457static int meson_uart_poll_get_char(struct uart_port *port)
458{
459 u32 c;
460 unsigned long flags;
461
462 spin_lock_irqsave(&port->lock, flags);
463
464 if (readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY)
465 c = NO_POLL_CHAR;
466 else
467 c = readl(port->membase + AML_UART_RFIFO);
468
469 spin_unlock_irqrestore(&port->lock, flags);
470
471 return c;
472}
473
474static void meson_uart_poll_put_char(struct uart_port *port, unsigned char c)
475{
476 unsigned long flags;
477 u32 reg;
478 int ret;
479
480 spin_lock_irqsave(&port->lock, flags);
481
482 /* Wait until FIFO is empty or timeout */
483 ret = readl_poll_timeout_atomic(port->membase + AML_UART_STATUS, reg,
484 reg & AML_UART_TX_EMPTY,
485 AML_UART_POLL_USEC,
486 AML_UART_TIMEOUT_USEC);
487 if (ret == -ETIMEDOUT) {
488 dev_err(port->dev, "Timeout waiting for UART TX EMPTY\n");
489 goto out;
490 }
491
492 /* Write the character */
493 writel(c, port->membase + AML_UART_WFIFO);
494
495 /* Wait until FIFO is empty or timeout */
496 ret = readl_poll_timeout_atomic(port->membase + AML_UART_STATUS, reg,
497 reg & AML_UART_TX_EMPTY,
498 AML_UART_POLL_USEC,
499 AML_UART_TIMEOUT_USEC);
500 if (ret == -ETIMEDOUT)
501 dev_err(port->dev, "Timeout waiting for UART TX EMPTY\n");
502
503out:
504 spin_unlock_irqrestore(&port->lock, flags);
505}
506
507#endif /* CONFIG_CONSOLE_POLL */
508
509static const struct uart_ops meson_uart_ops = {
510 .set_mctrl = meson_uart_set_mctrl,
511 .get_mctrl = meson_uart_get_mctrl,
512 .tx_empty = meson_uart_tx_empty,
513 .start_tx = meson_uart_start_tx,
514 .stop_tx = meson_uart_stop_tx,
515 .stop_rx = meson_uart_stop_rx,
516 .startup = meson_uart_startup,
517 .shutdown = meson_uart_shutdown,
518 .set_termios = meson_uart_set_termios,
519 .type = meson_uart_type,
520 .config_port = meson_uart_config_port,
521 .request_port = meson_uart_request_port,
522 .release_port = meson_uart_release_port,
523 .verify_port = meson_uart_verify_port,
524#ifdef CONFIG_CONSOLE_POLL
525 .poll_get_char = meson_uart_poll_get_char,
526 .poll_put_char = meson_uart_poll_put_char,
527#endif
528};
529
530#ifdef CONFIG_SERIAL_MESON_CONSOLE
531static void meson_uart_enable_tx_engine(struct uart_port *port)
532{
533 u32 val;
534
535 val = readl(port->membase + AML_UART_CONTROL);
536 val |= AML_UART_TX_EN;
537 writel(val, port->membase + AML_UART_CONTROL);
538}
539
540static void meson_console_putchar(struct uart_port *port, unsigned char ch)
541{
542 if (!port->membase)
543 return;
544
545 while (readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)
546 cpu_relax();
547 writel(ch, port->membase + AML_UART_WFIFO);
548}
549
550static void meson_serial_port_write(struct uart_port *port, const char *s,
551 u_int count)
552{
553 unsigned long flags;
554 int locked;
555 u32 val, tmp;
556
557 local_irq_save(flags);
558 if (port->sysrq) {
559 locked = 0;
560 } else if (oops_in_progress) {
561 locked = spin_trylock(&port->lock);
562 } else {
563 spin_lock(&port->lock);
564 locked = 1;
565 }
566
567 val = readl(port->membase + AML_UART_CONTROL);
568 tmp = val & ~(AML_UART_TX_INT_EN | AML_UART_RX_INT_EN);
569 writel(tmp, port->membase + AML_UART_CONTROL);
570
571 uart_console_write(port, s, count, meson_console_putchar);
572 writel(val, port->membase + AML_UART_CONTROL);
573
574 if (locked)
575 spin_unlock(&port->lock);
576 local_irq_restore(flags);
577}
578
579static void meson_serial_console_write(struct console *co, const char *s,
580 u_int count)
581{
582 struct uart_port *port;
583
584 port = meson_ports[co->index];
585 if (!port)
586 return;
587
588 meson_serial_port_write(port, s, count);
589}
590
591static int meson_serial_console_setup(struct console *co, char *options)
592{
593 struct uart_port *port;
594 int baud = 115200;
595 int bits = 8;
596 int parity = 'n';
597 int flow = 'n';
598
599 if (co->index < 0 || co->index >= AML_UART_PORT_NUM)
600 return -EINVAL;
601
602 port = meson_ports[co->index];
603 if (!port || !port->membase)
604 return -ENODEV;
605
606 meson_uart_enable_tx_engine(port);
607
608 if (options)
609 uart_parse_options(options, &baud, &parity, &bits, &flow);
610
611 return uart_set_options(port, co, baud, parity, bits, flow);
612}
613
614static struct console meson_serial_console = {
615 .name = AML_UART_DEV_NAME,
616 .write = meson_serial_console_write,
617 .device = uart_console_device,
618 .setup = meson_serial_console_setup,
619 .flags = CON_PRINTBUFFER,
620 .index = -1,
621 .data = &meson_uart_driver,
622};
623
624static int __init meson_serial_console_init(void)
625{
626 register_console(&meson_serial_console);
627 return 0;
628}
629
630static void meson_serial_early_console_write(struct console *co,
631 const char *s,
632 u_int count)
633{
634 struct earlycon_device *dev = co->data;
635
636 meson_serial_port_write(&dev->port, s, count);
637}
638
639static int __init
640meson_serial_early_console_setup(struct earlycon_device *device, const char *opt)
641{
642 if (!device->port.membase)
643 return -ENODEV;
644
645 meson_uart_enable_tx_engine(&device->port);
646 device->con->write = meson_serial_early_console_write;
647 return 0;
648}
649
650OF_EARLYCON_DECLARE(meson, "amlogic,meson-ao-uart",
651 meson_serial_early_console_setup);
652
653#define MESON_SERIAL_CONSOLE (&meson_serial_console)
654#else
655static int __init meson_serial_console_init(void) {
656 return 0;
657}
658#define MESON_SERIAL_CONSOLE NULL
659#endif
660
661static struct uart_driver meson_uart_driver = {
662 .owner = THIS_MODULE,
663 .driver_name = "meson_uart",
664 .dev_name = AML_UART_DEV_NAME,
665 .nr = AML_UART_PORT_NUM,
666 .cons = MESON_SERIAL_CONSOLE,
667};
668
669static int meson_uart_probe_clocks(struct platform_device *pdev,
670 struct uart_port *port)
671{
672 struct clk *clk_xtal = NULL;
673 struct clk *clk_pclk = NULL;
674 struct clk *clk_baud = NULL;
675
676 clk_pclk = devm_clk_get_enabled(&pdev->dev, "pclk");
677 if (IS_ERR(clk_pclk))
678 return PTR_ERR(clk_pclk);
679
680 clk_xtal = devm_clk_get_enabled(&pdev->dev, "xtal");
681 if (IS_ERR(clk_xtal))
682 return PTR_ERR(clk_xtal);
683
684 clk_baud = devm_clk_get_enabled(&pdev->dev, "baud");
685 if (IS_ERR(clk_baud))
686 return PTR_ERR(clk_baud);
687
688 port->uartclk = clk_get_rate(clk_baud);
689
690 return 0;
691}
692
693static int meson_uart_probe(struct platform_device *pdev)
694{
695 struct resource *res_mem;
696 struct uart_port *port;
697 u32 fifosize = 64; /* Default is 64, 128 for EE UART_0 */
698 int ret = 0;
699 int irq;
700
701 if (pdev->dev.of_node)
702 pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
703
704 if (pdev->id < 0) {
705 int id;
706
707 for (id = AML_UART_PORT_OFFSET; id < AML_UART_PORT_NUM; id++) {
708 if (!meson_ports[id]) {
709 pdev->id = id;
710 break;
711 }
712 }
713 }
714
715 if (pdev->id < 0 || pdev->id >= AML_UART_PORT_NUM)
716 return -EINVAL;
717
718 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
719 if (!res_mem)
720 return -ENODEV;
721
722 irq = platform_get_irq(pdev, 0);
723 if (irq < 0)
724 return irq;
725
726 of_property_read_u32(pdev->dev.of_node, "fifo-size", &fifosize);
727
728 if (meson_ports[pdev->id]) {
729 dev_err(&pdev->dev, "port %d already allocated\n", pdev->id);
730 return -EBUSY;
731 }
732
733 port = devm_kzalloc(&pdev->dev, sizeof(struct uart_port), GFP_KERNEL);
734 if (!port)
735 return -ENOMEM;
736
737 ret = meson_uart_probe_clocks(pdev, port);
738 if (ret)
739 return ret;
740
741 port->iotype = UPIO_MEM;
742 port->mapbase = res_mem->start;
743 port->mapsize = resource_size(res_mem);
744 port->irq = irq;
745 port->flags = UPF_BOOT_AUTOCONF | UPF_LOW_LATENCY;
746 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MESON_CONSOLE);
747 port->dev = &pdev->dev;
748 port->line = pdev->id;
749 port->type = PORT_MESON;
750 port->x_char = 0;
751 port->ops = &meson_uart_ops;
752 port->fifosize = fifosize;
753 port->private_data = (void *)device_get_match_data(&pdev->dev);
754
755 meson_ports[pdev->id] = port;
756 platform_set_drvdata(pdev, port);
757
758 /* reset port before registering (and possibly registering console) */
759 if (meson_uart_request_port(port) >= 0) {
760 meson_uart_reset(port);
761 meson_uart_release_port(port);
762 }
763
764 ret = uart_add_one_port(&meson_uart_driver, port);
765 if (ret)
766 meson_ports[pdev->id] = NULL;
767
768 return ret;
769}
770
771static int meson_uart_remove(struct platform_device *pdev)
772{
773 struct uart_port *port;
774
775 port = platform_get_drvdata(pdev);
776 uart_remove_one_port(&meson_uart_driver, port);
777 meson_ports[pdev->id] = NULL;
778
779 return 0;
780}
781
782static struct meson_uart_data s4_uart_data = {
783 .has_xtal_div2 = true,
784};
785
786static const struct of_device_id meson_uart_dt_match[] = {
787 { .compatible = "amlogic,meson6-uart" },
788 { .compatible = "amlogic,meson8-uart" },
789 { .compatible = "amlogic,meson8b-uart" },
790 { .compatible = "amlogic,meson-gx-uart" },
791 {
792 .compatible = "amlogic,meson-s4-uart",
793 .data = (void *)&s4_uart_data,
794 },
795 { /* sentinel */ },
796};
797MODULE_DEVICE_TABLE(of, meson_uart_dt_match);
798
799static struct platform_driver meson_uart_platform_driver = {
800 .probe = meson_uart_probe,
801 .remove = meson_uart_remove,
802 .driver = {
803 .name = "meson_uart",
804 .of_match_table = meson_uart_dt_match,
805 },
806};
807
808static int __init meson_uart_init(void)
809{
810 int ret;
811
812 ret = meson_serial_console_init();
813 if (ret)
814 return ret;
815
816 ret = uart_register_driver(&meson_uart_driver);
817 if (ret)
818 return ret;
819
820 ret = platform_driver_register(&meson_uart_platform_driver);
821 if (ret)
822 uart_unregister_driver(&meson_uart_driver);
823
824 return ret;
825}
826
827static void __exit meson_uart_exit(void)
828{
829 platform_driver_unregister(&meson_uart_platform_driver);
830 uart_unregister_driver(&meson_uart_driver);
831}
832
833module_init(meson_uart_init);
834module_exit(meson_uart_exit);
835
836MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
837MODULE_DESCRIPTION("Amlogic Meson serial port driver");
838MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Based on meson_uart.c, by AMLOGIC, INC.
4 *
5 * Copyright (C) 2014 Carlo Caione <carlo@caione.org>
6 */
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/iopoll.h>
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/of.h>
17#include <linux/platform_device.h>
18#include <linux/serial.h>
19#include <linux/serial_core.h>
20#include <linux/tty.h>
21#include <linux/tty_flip.h>
22
23/* Register offsets */
24#define AML_UART_WFIFO 0x00
25#define AML_UART_RFIFO 0x04
26#define AML_UART_CONTROL 0x08
27#define AML_UART_STATUS 0x0c
28#define AML_UART_MISC 0x10
29#define AML_UART_REG5 0x14
30
31/* AML_UART_CONTROL bits */
32#define AML_UART_TX_EN BIT(12)
33#define AML_UART_RX_EN BIT(13)
34#define AML_UART_TWO_WIRE_EN BIT(15)
35#define AML_UART_STOP_BIT_LEN_MASK (0x03 << 16)
36#define AML_UART_STOP_BIT_1SB (0x00 << 16)
37#define AML_UART_STOP_BIT_2SB (0x01 << 16)
38#define AML_UART_PARITY_TYPE BIT(18)
39#define AML_UART_PARITY_EN BIT(19)
40#define AML_UART_TX_RST BIT(22)
41#define AML_UART_RX_RST BIT(23)
42#define AML_UART_CLEAR_ERR BIT(24)
43#define AML_UART_RX_INT_EN BIT(27)
44#define AML_UART_TX_INT_EN BIT(28)
45#define AML_UART_DATA_LEN_MASK (0x03 << 20)
46#define AML_UART_DATA_LEN_8BIT (0x00 << 20)
47#define AML_UART_DATA_LEN_7BIT (0x01 << 20)
48#define AML_UART_DATA_LEN_6BIT (0x02 << 20)
49#define AML_UART_DATA_LEN_5BIT (0x03 << 20)
50
51/* AML_UART_STATUS bits */
52#define AML_UART_PARITY_ERR BIT(16)
53#define AML_UART_FRAME_ERR BIT(17)
54#define AML_UART_TX_FIFO_WERR BIT(18)
55#define AML_UART_RX_EMPTY BIT(20)
56#define AML_UART_TX_FULL BIT(21)
57#define AML_UART_TX_EMPTY BIT(22)
58#define AML_UART_XMIT_BUSY BIT(25)
59#define AML_UART_ERR (AML_UART_PARITY_ERR | \
60 AML_UART_FRAME_ERR | \
61 AML_UART_TX_FIFO_WERR)
62
63/* AML_UART_MISC bits */
64#define AML_UART_XMIT_IRQ(c) (((c) & 0xff) << 8)
65#define AML_UART_RECV_IRQ(c) ((c) & 0xff)
66
67/* AML_UART_REG5 bits */
68#define AML_UART_BAUD_MASK 0x7fffff
69#define AML_UART_BAUD_USE BIT(23)
70#define AML_UART_BAUD_XTAL BIT(24)
71#define AML_UART_BAUD_XTAL_DIV2 BIT(27)
72
73#define AML_UART_PORT_NUM 12
74#define AML_UART_PORT_OFFSET 6
75
76#define AML_UART_POLL_USEC 5
77#define AML_UART_TIMEOUT_USEC 10000
78
79static struct uart_driver meson_uart_driver_ttyAML;
80static struct uart_driver meson_uart_driver_ttyS;
81
82static struct uart_port *meson_ports[AML_UART_PORT_NUM];
83
84struct meson_uart_data {
85 struct uart_driver *uart_driver;
86 bool has_xtal_div2;
87};
88
89static void meson_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
90{
91}
92
93static unsigned int meson_uart_get_mctrl(struct uart_port *port)
94{
95 return TIOCM_CTS;
96}
97
98static unsigned int meson_uart_tx_empty(struct uart_port *port)
99{
100 u32 val;
101
102 val = readl(port->membase + AML_UART_STATUS);
103 val &= (AML_UART_TX_EMPTY | AML_UART_XMIT_BUSY);
104 return (val == AML_UART_TX_EMPTY) ? TIOCSER_TEMT : 0;
105}
106
107static void meson_uart_stop_tx(struct uart_port *port)
108{
109 u32 val;
110
111 val = readl(port->membase + AML_UART_CONTROL);
112 val &= ~AML_UART_TX_INT_EN;
113 writel(val, port->membase + AML_UART_CONTROL);
114}
115
116static void meson_uart_stop_rx(struct uart_port *port)
117{
118 u32 val;
119
120 val = readl(port->membase + AML_UART_CONTROL);
121 val &= ~AML_UART_RX_EN;
122 writel(val, port->membase + AML_UART_CONTROL);
123}
124
125static void meson_uart_shutdown(struct uart_port *port)
126{
127 unsigned long flags;
128 u32 val;
129
130 free_irq(port->irq, port);
131
132 uart_port_lock_irqsave(port, &flags);
133
134 val = readl(port->membase + AML_UART_CONTROL);
135 val &= ~AML_UART_RX_EN;
136 val &= ~(AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
137 writel(val, port->membase + AML_UART_CONTROL);
138
139 uart_port_unlock_irqrestore(port, flags);
140}
141
142static void meson_uart_start_tx(struct uart_port *port)
143{
144 struct tty_port *tport = &port->state->port;
145 unsigned char ch;
146 u32 val;
147
148 if (uart_tx_stopped(port)) {
149 meson_uart_stop_tx(port);
150 return;
151 }
152
153 while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
154 if (port->x_char) {
155 writel(port->x_char, port->membase + AML_UART_WFIFO);
156 port->icount.tx++;
157 port->x_char = 0;
158 continue;
159 }
160
161 if (!uart_fifo_get(port, &ch))
162 break;
163
164 writel(ch, port->membase + AML_UART_WFIFO);
165 }
166
167 if (!kfifo_is_empty(&tport->xmit_fifo)) {
168 val = readl(port->membase + AML_UART_CONTROL);
169 val |= AML_UART_TX_INT_EN;
170 writel(val, port->membase + AML_UART_CONTROL);
171 }
172
173 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
174 uart_write_wakeup(port);
175}
176
177static void meson_receive_chars(struct uart_port *port)
178{
179 struct tty_port *tport = &port->state->port;
180 char flag;
181 u32 ostatus, status, ch, mode;
182
183 do {
184 flag = TTY_NORMAL;
185 port->icount.rx++;
186 ostatus = status = readl(port->membase + AML_UART_STATUS);
187
188 if (status & AML_UART_ERR) {
189 if (status & AML_UART_TX_FIFO_WERR)
190 port->icount.overrun++;
191 else if (status & AML_UART_FRAME_ERR)
192 port->icount.frame++;
193 else if (status & AML_UART_PARITY_ERR)
194 port->icount.frame++;
195
196 mode = readl(port->membase + AML_UART_CONTROL);
197 mode |= AML_UART_CLEAR_ERR;
198 writel(mode, port->membase + AML_UART_CONTROL);
199
200 /* It doesn't clear to 0 automatically */
201 mode &= ~AML_UART_CLEAR_ERR;
202 writel(mode, port->membase + AML_UART_CONTROL);
203
204 status &= port->read_status_mask;
205 if (status & AML_UART_FRAME_ERR)
206 flag = TTY_FRAME;
207 else if (status & AML_UART_PARITY_ERR)
208 flag = TTY_PARITY;
209 }
210
211 ch = readl(port->membase + AML_UART_RFIFO);
212 ch &= 0xff;
213
214 if ((ostatus & AML_UART_FRAME_ERR) && (ch == 0)) {
215 port->icount.brk++;
216 flag = TTY_BREAK;
217 if (uart_handle_break(port))
218 continue;
219 }
220
221 if (uart_prepare_sysrq_char(port, ch))
222 continue;
223
224 if ((status & port->ignore_status_mask) == 0)
225 tty_insert_flip_char(tport, ch, flag);
226
227 if (status & AML_UART_TX_FIFO_WERR)
228 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
229
230 } while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY));
231
232 tty_flip_buffer_push(tport);
233}
234
235static irqreturn_t meson_uart_interrupt(int irq, void *dev_id)
236{
237 struct uart_port *port = (struct uart_port *)dev_id;
238
239 uart_port_lock(port);
240
241 if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY))
242 meson_receive_chars(port);
243
244 if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
245 if (readl(port->membase + AML_UART_CONTROL) & AML_UART_TX_INT_EN)
246 meson_uart_start_tx(port);
247 }
248
249 uart_unlock_and_check_sysrq(port);
250
251 return IRQ_HANDLED;
252}
253
254static const char *meson_uart_type(struct uart_port *port)
255{
256 return (port->type == PORT_MESON) ? "meson_uart" : NULL;
257}
258
259/*
260 * This function is called only from probe() using a temporary io mapping
261 * in order to perform a reset before setting up the device. Since the
262 * temporarily mapped region was successfully requested, there can be no
263 * console on this port at this time. Hence it is not necessary for this
264 * function to acquire the port->lock. (Since there is no console on this
265 * port at this time, the port->lock is not initialized yet.)
266 */
267static void meson_uart_reset(struct uart_port *port)
268{
269 u32 val;
270
271 val = readl(port->membase + AML_UART_CONTROL);
272 val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLEAR_ERR);
273 writel(val, port->membase + AML_UART_CONTROL);
274
275 val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLEAR_ERR);
276 writel(val, port->membase + AML_UART_CONTROL);
277}
278
279static int meson_uart_startup(struct uart_port *port)
280{
281 unsigned long flags;
282 u32 val;
283 int ret = 0;
284
285 uart_port_lock_irqsave(port, &flags);
286
287 val = readl(port->membase + AML_UART_CONTROL);
288 val |= AML_UART_CLEAR_ERR;
289 writel(val, port->membase + AML_UART_CONTROL);
290 val &= ~AML_UART_CLEAR_ERR;
291 writel(val, port->membase + AML_UART_CONTROL);
292
293 val |= (AML_UART_RX_EN | AML_UART_TX_EN);
294 writel(val, port->membase + AML_UART_CONTROL);
295
296 val |= (AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
297 writel(val, port->membase + AML_UART_CONTROL);
298
299 val = (AML_UART_RECV_IRQ(1) | AML_UART_XMIT_IRQ(port->fifosize / 2));
300 writel(val, port->membase + AML_UART_MISC);
301
302 uart_port_unlock_irqrestore(port, flags);
303
304 ret = request_irq(port->irq, meson_uart_interrupt, 0,
305 port->name, port);
306
307 return ret;
308}
309
310static void meson_uart_change_speed(struct uart_port *port, unsigned long baud)
311{
312 const struct meson_uart_data *private_data = port->private_data;
313 u32 val = 0;
314
315 while (!meson_uart_tx_empty(port))
316 cpu_relax();
317
318 if (port->uartclk == 24000000) {
319 unsigned int xtal_div = 3;
320
321 if (private_data && private_data->has_xtal_div2) {
322 xtal_div = 2;
323 val |= AML_UART_BAUD_XTAL_DIV2;
324 }
325 val |= DIV_ROUND_CLOSEST(port->uartclk / xtal_div, baud) - 1;
326 val |= AML_UART_BAUD_XTAL;
327 } else {
328 val = DIV_ROUND_CLOSEST(port->uartclk / 4, baud) - 1;
329 }
330 val |= AML_UART_BAUD_USE;
331 writel(val, port->membase + AML_UART_REG5);
332}
333
334static void meson_uart_set_termios(struct uart_port *port,
335 struct ktermios *termios,
336 const struct ktermios *old)
337{
338 unsigned int cflags, iflags, baud;
339 unsigned long flags;
340 u32 val;
341
342 uart_port_lock_irqsave(port, &flags);
343
344 cflags = termios->c_cflag;
345 iflags = termios->c_iflag;
346
347 val = readl(port->membase + AML_UART_CONTROL);
348
349 val &= ~AML_UART_DATA_LEN_MASK;
350 switch (cflags & CSIZE) {
351 case CS8:
352 val |= AML_UART_DATA_LEN_8BIT;
353 break;
354 case CS7:
355 val |= AML_UART_DATA_LEN_7BIT;
356 break;
357 case CS6:
358 val |= AML_UART_DATA_LEN_6BIT;
359 break;
360 case CS5:
361 val |= AML_UART_DATA_LEN_5BIT;
362 break;
363 }
364
365 if (cflags & PARENB)
366 val |= AML_UART_PARITY_EN;
367 else
368 val &= ~AML_UART_PARITY_EN;
369
370 if (cflags & PARODD)
371 val |= AML_UART_PARITY_TYPE;
372 else
373 val &= ~AML_UART_PARITY_TYPE;
374
375 val &= ~AML_UART_STOP_BIT_LEN_MASK;
376 if (cflags & CSTOPB)
377 val |= AML_UART_STOP_BIT_2SB;
378 else
379 val |= AML_UART_STOP_BIT_1SB;
380
381 if (cflags & CRTSCTS) {
382 if (port->flags & UPF_HARD_FLOW)
383 val &= ~AML_UART_TWO_WIRE_EN;
384 else
385 termios->c_cflag &= ~CRTSCTS;
386 } else {
387 val |= AML_UART_TWO_WIRE_EN;
388 }
389
390 writel(val, port->membase + AML_UART_CONTROL);
391
392 baud = uart_get_baud_rate(port, termios, old, 50, 4000000);
393 meson_uart_change_speed(port, baud);
394
395 port->read_status_mask = AML_UART_TX_FIFO_WERR;
396 if (iflags & INPCK)
397 port->read_status_mask |= AML_UART_PARITY_ERR |
398 AML_UART_FRAME_ERR;
399
400 port->ignore_status_mask = 0;
401 if (iflags & IGNPAR)
402 port->ignore_status_mask |= AML_UART_PARITY_ERR |
403 AML_UART_FRAME_ERR;
404
405 uart_update_timeout(port, termios->c_cflag, baud);
406 uart_port_unlock_irqrestore(port, flags);
407}
408
409static int meson_uart_verify_port(struct uart_port *port,
410 struct serial_struct *ser)
411{
412 int ret = 0;
413
414 if (port->type != PORT_MESON)
415 ret = -EINVAL;
416 if (port->irq != ser->irq)
417 ret = -EINVAL;
418 if (ser->baud_base < 9600)
419 ret = -EINVAL;
420 return ret;
421}
422
423static void meson_uart_release_port(struct uart_port *port)
424{
425 devm_iounmap(port->dev, port->membase);
426 port->membase = NULL;
427 devm_release_mem_region(port->dev, port->mapbase, port->mapsize);
428}
429
430static int meson_uart_request_port(struct uart_port *port)
431{
432 if (!devm_request_mem_region(port->dev, port->mapbase, port->mapsize,
433 dev_name(port->dev))) {
434 dev_err(port->dev, "Memory region busy\n");
435 return -EBUSY;
436 }
437
438 port->membase = devm_ioremap(port->dev, port->mapbase,
439 port->mapsize);
440 if (!port->membase)
441 return -ENOMEM;
442
443 return 0;
444}
445
446static void meson_uart_config_port(struct uart_port *port, int flags)
447{
448 if (flags & UART_CONFIG_TYPE) {
449 port->type = PORT_MESON;
450 meson_uart_request_port(port);
451 }
452}
453
454#ifdef CONFIG_CONSOLE_POLL
455/*
456 * Console polling routines for writing and reading from the uart while
457 * in an interrupt or debug context (i.e. kgdb).
458 */
459
460static int meson_uart_poll_get_char(struct uart_port *port)
461{
462 u32 c;
463 unsigned long flags;
464
465 uart_port_lock_irqsave(port, &flags);
466
467 if (readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY)
468 c = NO_POLL_CHAR;
469 else
470 c = readl(port->membase + AML_UART_RFIFO);
471
472 uart_port_unlock_irqrestore(port, flags);
473
474 return c;
475}
476
477static void meson_uart_poll_put_char(struct uart_port *port, unsigned char c)
478{
479 unsigned long flags;
480 u32 reg;
481 int ret;
482
483 uart_port_lock_irqsave(port, &flags);
484
485 /* Wait until FIFO is empty or timeout */
486 ret = readl_poll_timeout_atomic(port->membase + AML_UART_STATUS, reg,
487 reg & AML_UART_TX_EMPTY,
488 AML_UART_POLL_USEC,
489 AML_UART_TIMEOUT_USEC);
490 if (ret == -ETIMEDOUT) {
491 dev_err(port->dev, "Timeout waiting for UART TX EMPTY\n");
492 goto out;
493 }
494
495 /* Write the character */
496 writel(c, port->membase + AML_UART_WFIFO);
497
498 /* Wait until FIFO is empty or timeout */
499 ret = readl_poll_timeout_atomic(port->membase + AML_UART_STATUS, reg,
500 reg & AML_UART_TX_EMPTY,
501 AML_UART_POLL_USEC,
502 AML_UART_TIMEOUT_USEC);
503 if (ret == -ETIMEDOUT)
504 dev_err(port->dev, "Timeout waiting for UART TX EMPTY\n");
505
506out:
507 uart_port_unlock_irqrestore(port, flags);
508}
509
510#endif /* CONFIG_CONSOLE_POLL */
511
512static const struct uart_ops meson_uart_ops = {
513 .set_mctrl = meson_uart_set_mctrl,
514 .get_mctrl = meson_uart_get_mctrl,
515 .tx_empty = meson_uart_tx_empty,
516 .start_tx = meson_uart_start_tx,
517 .stop_tx = meson_uart_stop_tx,
518 .stop_rx = meson_uart_stop_rx,
519 .startup = meson_uart_startup,
520 .shutdown = meson_uart_shutdown,
521 .set_termios = meson_uart_set_termios,
522 .type = meson_uart_type,
523 .config_port = meson_uart_config_port,
524 .request_port = meson_uart_request_port,
525 .release_port = meson_uart_release_port,
526 .verify_port = meson_uart_verify_port,
527#ifdef CONFIG_CONSOLE_POLL
528 .poll_get_char = meson_uart_poll_get_char,
529 .poll_put_char = meson_uart_poll_put_char,
530#endif
531};
532
533#ifdef CONFIG_SERIAL_MESON_CONSOLE
534static void meson_uart_enable_tx_engine(struct uart_port *port)
535{
536 u32 val;
537
538 val = readl(port->membase + AML_UART_CONTROL);
539 val |= AML_UART_TX_EN;
540 writel(val, port->membase + AML_UART_CONTROL);
541}
542
543static void meson_console_putchar(struct uart_port *port, unsigned char ch)
544{
545 if (!port->membase)
546 return;
547
548 while (readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)
549 cpu_relax();
550 writel(ch, port->membase + AML_UART_WFIFO);
551}
552
553static void meson_serial_port_write(struct uart_port *port, const char *s,
554 u_int count)
555{
556 unsigned long flags;
557 int locked = 1;
558 u32 val, tmp;
559
560 if (oops_in_progress)
561 locked = uart_port_trylock_irqsave(port, &flags);
562 else
563 uart_port_lock_irqsave(port, &flags);
564
565 val = readl(port->membase + AML_UART_CONTROL);
566 tmp = val & ~(AML_UART_TX_INT_EN | AML_UART_RX_INT_EN);
567 writel(tmp, port->membase + AML_UART_CONTROL);
568
569 uart_console_write(port, s, count, meson_console_putchar);
570 writel(val, port->membase + AML_UART_CONTROL);
571
572 if (locked)
573 uart_port_unlock_irqrestore(port, flags);
574}
575
576static void meson_serial_console_write(struct console *co, const char *s,
577 u_int count)
578{
579 struct uart_port *port;
580
581 port = meson_ports[co->index];
582 if (!port)
583 return;
584
585 meson_serial_port_write(port, s, count);
586}
587
588static int meson_serial_console_setup(struct console *co, char *options)
589{
590 struct uart_port *port;
591 int baud = 115200;
592 int bits = 8;
593 int parity = 'n';
594 int flow = 'n';
595
596 if (co->index < 0 || co->index >= AML_UART_PORT_NUM)
597 return -EINVAL;
598
599 port = meson_ports[co->index];
600 if (!port || !port->membase)
601 return -ENODEV;
602
603 meson_uart_enable_tx_engine(port);
604
605 if (options)
606 uart_parse_options(options, &baud, &parity, &bits, &flow);
607
608 return uart_set_options(port, co, baud, parity, bits, flow);
609}
610
611#define MESON_SERIAL_CONSOLE(_devname) \
612 static struct console meson_serial_console_##_devname = { \
613 .name = __stringify(_devname), \
614 .write = meson_serial_console_write, \
615 .device = uart_console_device, \
616 .setup = meson_serial_console_setup, \
617 .flags = CON_PRINTBUFFER, \
618 .index = -1, \
619 .data = &meson_uart_driver_##_devname, \
620 }
621
622MESON_SERIAL_CONSOLE(ttyAML);
623MESON_SERIAL_CONSOLE(ttyS);
624
625static void meson_serial_early_console_write(struct console *co,
626 const char *s,
627 u_int count)
628{
629 struct earlycon_device *dev = co->data;
630
631 meson_serial_port_write(&dev->port, s, count);
632}
633
634static int __init
635meson_serial_early_console_setup(struct earlycon_device *device, const char *opt)
636{
637 if (!device->port.membase)
638 return -ENODEV;
639
640 meson_uart_enable_tx_engine(&device->port);
641 device->con->write = meson_serial_early_console_write;
642 return 0;
643}
644
645OF_EARLYCON_DECLARE(meson, "amlogic,meson-ao-uart", meson_serial_early_console_setup);
646OF_EARLYCON_DECLARE(meson, "amlogic,meson-s4-uart", meson_serial_early_console_setup);
647
648#define MESON_SERIAL_CONSOLE_PTR(_devname) (&meson_serial_console_##_devname)
649#else
650#define MESON_SERIAL_CONSOLE_PTR(_devname) (NULL)
651#endif
652
653#define MESON_UART_DRIVER(_devname) \
654 static struct uart_driver meson_uart_driver_##_devname = { \
655 .owner = THIS_MODULE, \
656 .driver_name = "meson_uart", \
657 .dev_name = __stringify(_devname), \
658 .nr = AML_UART_PORT_NUM, \
659 .cons = MESON_SERIAL_CONSOLE_PTR(_devname), \
660 }
661
662MESON_UART_DRIVER(ttyAML);
663MESON_UART_DRIVER(ttyS);
664
665static int meson_uart_probe_clocks(struct platform_device *pdev,
666 struct uart_port *port)
667{
668 struct clk *clk_xtal = NULL;
669 struct clk *clk_pclk = NULL;
670 struct clk *clk_baud = NULL;
671
672 clk_pclk = devm_clk_get_enabled(&pdev->dev, "pclk");
673 if (IS_ERR(clk_pclk))
674 return PTR_ERR(clk_pclk);
675
676 clk_xtal = devm_clk_get_enabled(&pdev->dev, "xtal");
677 if (IS_ERR(clk_xtal))
678 return PTR_ERR(clk_xtal);
679
680 clk_baud = devm_clk_get_enabled(&pdev->dev, "baud");
681 if (IS_ERR(clk_baud))
682 return PTR_ERR(clk_baud);
683
684 port->uartclk = clk_get_rate(clk_baud);
685
686 return 0;
687}
688
689static struct uart_driver *meson_uart_current(const struct meson_uart_data *pd)
690{
691 return (pd && pd->uart_driver) ?
692 pd->uart_driver : &meson_uart_driver_ttyAML;
693}
694
695static int meson_uart_probe(struct platform_device *pdev)
696{
697 const struct meson_uart_data *priv_data;
698 struct uart_driver *uart_driver;
699 struct resource *res_mem;
700 struct uart_port *port;
701 u32 fifosize = 64; /* Default is 64, 128 for EE UART_0 */
702 int ret = 0;
703 int irq;
704 bool has_rtscts;
705
706 if (pdev->dev.of_node)
707 pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
708
709 if (pdev->id < 0) {
710 int id;
711
712 for (id = AML_UART_PORT_OFFSET; id < AML_UART_PORT_NUM; id++) {
713 if (!meson_ports[id]) {
714 pdev->id = id;
715 break;
716 }
717 }
718 }
719
720 if (pdev->id < 0 || pdev->id >= AML_UART_PORT_NUM)
721 return -EINVAL;
722
723 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
724 if (!res_mem)
725 return -ENODEV;
726
727 irq = platform_get_irq(pdev, 0);
728 if (irq < 0)
729 return irq;
730
731 of_property_read_u32(pdev->dev.of_node, "fifo-size", &fifosize);
732 has_rtscts = of_property_read_bool(pdev->dev.of_node, "uart-has-rtscts");
733
734 if (meson_ports[pdev->id]) {
735 return dev_err_probe(&pdev->dev, -EBUSY,
736 "port %d already allocated\n", pdev->id);
737 }
738
739 port = devm_kzalloc(&pdev->dev, sizeof(struct uart_port), GFP_KERNEL);
740 if (!port)
741 return -ENOMEM;
742
743 ret = meson_uart_probe_clocks(pdev, port);
744 if (ret)
745 return ret;
746
747 priv_data = device_get_match_data(&pdev->dev);
748
749 uart_driver = meson_uart_current(priv_data);
750
751 if (!uart_driver->state) {
752 ret = uart_register_driver(uart_driver);
753 if (ret)
754 return dev_err_probe(&pdev->dev, ret,
755 "can't register uart driver\n");
756 }
757
758 port->iotype = UPIO_MEM;
759 port->mapbase = res_mem->start;
760 port->mapsize = resource_size(res_mem);
761 port->irq = irq;
762 port->flags = UPF_BOOT_AUTOCONF | UPF_LOW_LATENCY;
763 if (has_rtscts)
764 port->flags |= UPF_HARD_FLOW;
765 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MESON_CONSOLE);
766 port->dev = &pdev->dev;
767 port->line = pdev->id;
768 port->type = PORT_MESON;
769 port->x_char = 0;
770 port->ops = &meson_uart_ops;
771 port->fifosize = fifosize;
772 port->private_data = (void *)priv_data;
773
774 meson_ports[pdev->id] = port;
775 platform_set_drvdata(pdev, port);
776
777 /* reset port before registering (and possibly registering console) */
778 if (meson_uart_request_port(port) >= 0) {
779 meson_uart_reset(port);
780 meson_uart_release_port(port);
781 }
782
783 ret = uart_add_one_port(uart_driver, port);
784 if (ret)
785 meson_ports[pdev->id] = NULL;
786
787 return ret;
788}
789
790static void meson_uart_remove(struct platform_device *pdev)
791{
792 struct uart_driver *uart_driver;
793 struct uart_port *port;
794
795 port = platform_get_drvdata(pdev);
796 uart_driver = meson_uart_current(port->private_data);
797 uart_remove_one_port(uart_driver, port);
798 meson_ports[pdev->id] = NULL;
799
800 for (int id = 0; id < AML_UART_PORT_NUM; id++)
801 if (meson_ports[id])
802 return;
803
804 /* No more available uart ports, unregister uart driver */
805 uart_unregister_driver(uart_driver);
806}
807
808static struct meson_uart_data meson_g12a_uart_data = {
809 .has_xtal_div2 = true,
810};
811
812static struct meson_uart_data meson_a1_uart_data = {
813 .uart_driver = &meson_uart_driver_ttyS,
814 .has_xtal_div2 = false,
815};
816
817static struct meson_uart_data meson_s4_uart_data = {
818 .uart_driver = &meson_uart_driver_ttyS,
819 .has_xtal_div2 = true,
820};
821
822static const struct of_device_id meson_uart_dt_match[] = {
823 { .compatible = "amlogic,meson6-uart" },
824 { .compatible = "amlogic,meson8-uart" },
825 { .compatible = "amlogic,meson8b-uart" },
826 { .compatible = "amlogic,meson-gx-uart" },
827 {
828 .compatible = "amlogic,meson-g12a-uart",
829 .data = (void *)&meson_g12a_uart_data,
830 },
831 {
832 .compatible = "amlogic,meson-s4-uart",
833 .data = (void *)&meson_s4_uart_data,
834 },
835 {
836 .compatible = "amlogic,meson-a1-uart",
837 .data = (void *)&meson_a1_uart_data,
838 },
839 { /* sentinel */ },
840};
841MODULE_DEVICE_TABLE(of, meson_uart_dt_match);
842
843static struct platform_driver meson_uart_platform_driver = {
844 .probe = meson_uart_probe,
845 .remove = meson_uart_remove,
846 .driver = {
847 .name = "meson_uart",
848 .of_match_table = meson_uart_dt_match,
849 },
850};
851
852module_platform_driver(meson_uart_platform_driver);
853
854MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
855MODULE_DESCRIPTION("Amlogic Meson serial port driver");
856MODULE_LICENSE("GPL v2");