1 // SPDX-License-Identifier: GPL-2.0
3 * Based on meson_uart.c, by AMLOGIC, INC.
5 * Copyright (C) 2014 Carlo Caione <carlo@caione.org>
9 #include <linux/console.h>
10 #include <linux/delay.h>
11 #include <linux/init.h>
13 #include <linux/iopoll.h>
14 #include <linux/module.h>
15 #include <linux/kernel.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>
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
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)
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)
63 /* AML_UART_MISC bits */
64 #define AML_UART_XMIT_IRQ(c) (((c) & 0xff) << 8)
65 #define AML_UART_RECV_IRQ(c) ((c) & 0xff)
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)
73 #define AML_UART_PORT_NUM 12
74 #define AML_UART_PORT_OFFSET 6
76 #define AML_UART_POLL_USEC 5
77 #define AML_UART_TIMEOUT_USEC 10000
79 static struct uart_driver meson_uart_driver_ttyAML
;
80 static struct uart_driver meson_uart_driver_ttyS
;
82 static struct uart_port
*meson_ports
[AML_UART_PORT_NUM
];
84 struct meson_uart_data
{
85 struct uart_driver
*uart_driver
;
89 static void meson_uart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
93 static unsigned int meson_uart_get_mctrl(struct uart_port
*port
)
98 static unsigned int meson_uart_tx_empty(struct uart_port
*port
)
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;
107 static void meson_uart_stop_tx(struct uart_port
*port
)
111 val
= readl(port
->membase
+ AML_UART_CONTROL
);
112 val
&= ~AML_UART_TX_INT_EN
;
113 writel(val
, port
->membase
+ AML_UART_CONTROL
);
116 static void meson_uart_stop_rx(struct uart_port
*port
)
120 val
= readl(port
->membase
+ AML_UART_CONTROL
);
121 val
&= ~AML_UART_RX_EN
;
122 writel(val
, port
->membase
+ AML_UART_CONTROL
);
125 static void meson_uart_shutdown(struct uart_port
*port
)
130 free_irq(port
->irq
, port
);
132 uart_port_lock_irqsave(port
, &flags
);
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
);
139 uart_port_unlock_irqrestore(port
, flags
);
142 static void meson_uart_start_tx(struct uart_port
*port
)
144 struct tty_port
*tport
= &port
->state
->port
;
148 if (uart_tx_stopped(port
)) {
149 meson_uart_stop_tx(port
);
153 while (!(readl(port
->membase
+ AML_UART_STATUS
) & AML_UART_TX_FULL
)) {
155 writel(port
->x_char
, port
->membase
+ AML_UART_WFIFO
);
161 if (!uart_fifo_get(port
, &ch
))
164 writel(ch
, port
->membase
+ AML_UART_WFIFO
);
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
);
173 if (kfifo_len(&tport
->xmit_fifo
) < WAKEUP_CHARS
)
174 uart_write_wakeup(port
);
177 static void meson_receive_chars(struct uart_port
*port
)
179 struct tty_port
*tport
= &port
->state
->port
;
181 u32 ostatus
, status
, ch
, mode
;
186 ostatus
= status
= readl(port
->membase
+ AML_UART_STATUS
);
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
++;
196 mode
= readl(port
->membase
+ AML_UART_CONTROL
);
197 mode
|= AML_UART_CLEAR_ERR
;
198 writel(mode
, port
->membase
+ AML_UART_CONTROL
);
200 /* It doesn't clear to 0 automatically */
201 mode
&= ~AML_UART_CLEAR_ERR
;
202 writel(mode
, port
->membase
+ AML_UART_CONTROL
);
204 status
&= port
->read_status_mask
;
205 if (status
& AML_UART_FRAME_ERR
)
207 else if (status
& AML_UART_PARITY_ERR
)
211 ch
= readl(port
->membase
+ AML_UART_RFIFO
);
214 if ((ostatus
& AML_UART_FRAME_ERR
) && (ch
== 0)) {
217 if (uart_handle_break(port
))
221 if (uart_prepare_sysrq_char(port
, ch
))
224 if ((status
& port
->ignore_status_mask
) == 0)
225 tty_insert_flip_char(tport
, ch
, flag
);
227 if (status
& AML_UART_TX_FIFO_WERR
)
228 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
230 } while (!(readl(port
->membase
+ AML_UART_STATUS
) & AML_UART_RX_EMPTY
));
232 tty_flip_buffer_push(tport
);
235 static irqreturn_t
meson_uart_interrupt(int irq
, void *dev_id
)
237 struct uart_port
*port
= (struct uart_port
*)dev_id
;
239 uart_port_lock(port
);
241 if (!(readl(port
->membase
+ AML_UART_STATUS
) & AML_UART_RX_EMPTY
))
242 meson_receive_chars(port
);
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
);
249 uart_unlock_and_check_sysrq(port
);
254 static const char *meson_uart_type(struct uart_port
*port
)
256 return (port
->type
== PORT_MESON
) ? "meson_uart" : NULL
;
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.)
267 static void meson_uart_reset(struct uart_port
*port
)
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
);
275 val
&= ~(AML_UART_RX_RST
| AML_UART_TX_RST
| AML_UART_CLEAR_ERR
);
276 writel(val
, port
->membase
+ AML_UART_CONTROL
);
279 static int meson_uart_startup(struct uart_port
*port
)
285 uart_port_lock_irqsave(port
, &flags
);
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
);
293 val
|= (AML_UART_RX_EN
| AML_UART_TX_EN
);
294 writel(val
, port
->membase
+ AML_UART_CONTROL
);
296 val
|= (AML_UART_RX_INT_EN
| AML_UART_TX_INT_EN
);
297 writel(val
, port
->membase
+ AML_UART_CONTROL
);
299 val
= (AML_UART_RECV_IRQ(1) | AML_UART_XMIT_IRQ(port
->fifosize
/ 2));
300 writel(val
, port
->membase
+ AML_UART_MISC
);
302 uart_port_unlock_irqrestore(port
, flags
);
304 ret
= request_irq(port
->irq
, meson_uart_interrupt
, 0,
310 static void meson_uart_change_speed(struct uart_port
*port
, unsigned long baud
)
312 const struct meson_uart_data
*private_data
= port
->private_data
;
315 while (!meson_uart_tx_empty(port
))
318 if (port
->uartclk
== 24000000) {
319 unsigned int xtal_div
= 3;
321 if (private_data
&& private_data
->has_xtal_div2
) {
323 val
|= AML_UART_BAUD_XTAL_DIV2
;
325 val
|= DIV_ROUND_CLOSEST(port
->uartclk
/ xtal_div
, baud
) - 1;
326 val
|= AML_UART_BAUD_XTAL
;
328 val
= DIV_ROUND_CLOSEST(port
->uartclk
/ 4, baud
) - 1;
330 val
|= AML_UART_BAUD_USE
;
331 writel(val
, port
->membase
+ AML_UART_REG5
);
334 static void meson_uart_set_termios(struct uart_port
*port
,
335 struct ktermios
*termios
,
336 const struct ktermios
*old
)
338 unsigned int cflags
, iflags
, baud
;
342 uart_port_lock_irqsave(port
, &flags
);
344 cflags
= termios
->c_cflag
;
345 iflags
= termios
->c_iflag
;
347 val
= readl(port
->membase
+ AML_UART_CONTROL
);
349 val
&= ~AML_UART_DATA_LEN_MASK
;
350 switch (cflags
& CSIZE
) {
352 val
|= AML_UART_DATA_LEN_8BIT
;
355 val
|= AML_UART_DATA_LEN_7BIT
;
358 val
|= AML_UART_DATA_LEN_6BIT
;
361 val
|= AML_UART_DATA_LEN_5BIT
;
366 val
|= AML_UART_PARITY_EN
;
368 val
&= ~AML_UART_PARITY_EN
;
371 val
|= AML_UART_PARITY_TYPE
;
373 val
&= ~AML_UART_PARITY_TYPE
;
375 val
&= ~AML_UART_STOP_BIT_LEN_MASK
;
377 val
|= AML_UART_STOP_BIT_2SB
;
379 val
|= AML_UART_STOP_BIT_1SB
;
381 if (cflags
& CRTSCTS
) {
382 if (port
->flags
& UPF_HARD_FLOW
)
383 val
&= ~AML_UART_TWO_WIRE_EN
;
385 termios
->c_cflag
&= ~CRTSCTS
;
387 val
|= AML_UART_TWO_WIRE_EN
;
390 writel(val
, port
->membase
+ AML_UART_CONTROL
);
392 baud
= uart_get_baud_rate(port
, termios
, old
, 50, 4000000);
393 meson_uart_change_speed(port
, baud
);
395 port
->read_status_mask
= AML_UART_TX_FIFO_WERR
;
397 port
->read_status_mask
|= AML_UART_PARITY_ERR
|
400 port
->ignore_status_mask
= 0;
402 port
->ignore_status_mask
|= AML_UART_PARITY_ERR
|
405 uart_update_timeout(port
, termios
->c_cflag
, baud
);
406 uart_port_unlock_irqrestore(port
, flags
);
409 static int meson_uart_verify_port(struct uart_port
*port
,
410 struct serial_struct
*ser
)
414 if (port
->type
!= PORT_MESON
)
416 if (port
->irq
!= ser
->irq
)
418 if (ser
->baud_base
< 9600)
423 static void meson_uart_release_port(struct uart_port
*port
)
425 devm_iounmap(port
->dev
, port
->membase
);
426 port
->membase
= NULL
;
427 devm_release_mem_region(port
->dev
, port
->mapbase
, port
->mapsize
);
430 static int meson_uart_request_port(struct uart_port
*port
)
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");
438 port
->membase
= devm_ioremap(port
->dev
, port
->mapbase
,
446 static void meson_uart_config_port(struct uart_port
*port
, int flags
)
448 if (flags
& UART_CONFIG_TYPE
) {
449 port
->type
= PORT_MESON
;
450 meson_uart_request_port(port
);
454 #ifdef CONFIG_CONSOLE_POLL
456 * Console polling routines for writing and reading from the uart while
457 * in an interrupt or debug context (i.e. kgdb).
460 static int meson_uart_poll_get_char(struct uart_port
*port
)
465 uart_port_lock_irqsave(port
, &flags
);
467 if (readl(port
->membase
+ AML_UART_STATUS
) & AML_UART_RX_EMPTY
)
470 c
= readl(port
->membase
+ AML_UART_RFIFO
);
472 uart_port_unlock_irqrestore(port
, flags
);
477 static void meson_uart_poll_put_char(struct uart_port
*port
, unsigned char c
)
483 uart_port_lock_irqsave(port
, &flags
);
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
,
489 AML_UART_TIMEOUT_USEC
);
490 if (ret
== -ETIMEDOUT
) {
491 dev_err(port
->dev
, "Timeout waiting for UART TX EMPTY\n");
495 /* Write the character */
496 writel(c
, port
->membase
+ AML_UART_WFIFO
);
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
,
502 AML_UART_TIMEOUT_USEC
);
503 if (ret
== -ETIMEDOUT
)
504 dev_err(port
->dev
, "Timeout waiting for UART TX EMPTY\n");
507 uart_port_unlock_irqrestore(port
, flags
);
510 #endif /* CONFIG_CONSOLE_POLL */
512 static 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
,
533 #ifdef CONFIG_SERIAL_MESON_CONSOLE
534 static void meson_uart_enable_tx_engine(struct uart_port
*port
)
538 val
= readl(port
->membase
+ AML_UART_CONTROL
);
539 val
|= AML_UART_TX_EN
;
540 writel(val
, port
->membase
+ AML_UART_CONTROL
);
543 static void meson_console_putchar(struct uart_port
*port
, unsigned char ch
)
548 while (readl(port
->membase
+ AML_UART_STATUS
) & AML_UART_TX_FULL
)
550 writel(ch
, port
->membase
+ AML_UART_WFIFO
);
553 static void meson_serial_port_write(struct uart_port
*port
, const char *s
,
560 if (oops_in_progress
)
561 locked
= uart_port_trylock_irqsave(port
, &flags
);
563 uart_port_lock_irqsave(port
, &flags
);
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
);
569 uart_console_write(port
, s
, count
, meson_console_putchar
);
570 writel(val
, port
->membase
+ AML_UART_CONTROL
);
573 uart_port_unlock_irqrestore(port
, flags
);
576 static void meson_serial_console_write(struct console
*co
, const char *s
,
579 struct uart_port
*port
;
581 port
= meson_ports
[co
->index
];
585 meson_serial_port_write(port
, s
, count
);
588 static int meson_serial_console_setup(struct console
*co
, char *options
)
590 struct uart_port
*port
;
596 if (co
->index
< 0 || co
->index
>= AML_UART_PORT_NUM
)
599 port
= meson_ports
[co
->index
];
600 if (!port
|| !port
->membase
)
603 meson_uart_enable_tx_engine(port
);
606 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
608 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
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, \
619 .data = &meson_uart_driver_##_devname, \
622 MESON_SERIAL_CONSOLE(ttyAML
);
623 MESON_SERIAL_CONSOLE(ttyS
);
625 static void meson_serial_early_console_write(struct console
*co
,
629 struct earlycon_device
*dev
= co
->data
;
631 meson_serial_port_write(&dev
->port
, s
, count
);
635 meson_serial_early_console_setup(struct earlycon_device
*device
, const char *opt
)
637 if (!device
->port
.membase
)
640 meson_uart_enable_tx_engine(&device
->port
);
641 device
->con
->write
= meson_serial_early_console_write
;
645 OF_EARLYCON_DECLARE(meson
, "amlogic,meson-ao-uart", meson_serial_early_console_setup
);
646 OF_EARLYCON_DECLARE(meson
, "amlogic,meson-s4-uart", meson_serial_early_console_setup
);
648 #define MESON_SERIAL_CONSOLE_PTR(_devname) (&meson_serial_console_##_devname)
650 #define MESON_SERIAL_CONSOLE_PTR(_devname) (NULL)
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), \
662 MESON_UART_DRIVER(ttyAML
);
663 MESON_UART_DRIVER(ttyS
);
665 static int meson_uart_probe_clocks(struct platform_device
*pdev
,
666 struct uart_port
*port
)
668 struct clk
*clk_xtal
= NULL
;
669 struct clk
*clk_pclk
= NULL
;
670 struct clk
*clk_baud
= NULL
;
672 clk_pclk
= devm_clk_get_enabled(&pdev
->dev
, "pclk");
673 if (IS_ERR(clk_pclk
))
674 return PTR_ERR(clk_pclk
);
676 clk_xtal
= devm_clk_get_enabled(&pdev
->dev
, "xtal");
677 if (IS_ERR(clk_xtal
))
678 return PTR_ERR(clk_xtal
);
680 clk_baud
= devm_clk_get_enabled(&pdev
->dev
, "baud");
681 if (IS_ERR(clk_baud
))
682 return PTR_ERR(clk_baud
);
684 port
->uartclk
= clk_get_rate(clk_baud
);
689 static struct uart_driver
*meson_uart_current(const struct meson_uart_data
*pd
)
691 return (pd
&& pd
->uart_driver
) ?
692 pd
->uart_driver
: &meson_uart_driver_ttyAML
;
695 static int meson_uart_probe(struct platform_device
*pdev
)
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 */
706 if (pdev
->dev
.of_node
)
707 pdev
->id
= of_alias_get_id(pdev
->dev
.of_node
, "serial");
712 for (id
= AML_UART_PORT_OFFSET
; id
< AML_UART_PORT_NUM
; id
++) {
713 if (!meson_ports
[id
]) {
720 if (pdev
->id
< 0 || pdev
->id
>= AML_UART_PORT_NUM
)
723 res_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
727 irq
= platform_get_irq(pdev
, 0);
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");
734 if (meson_ports
[pdev
->id
]) {
735 return dev_err_probe(&pdev
->dev
, -EBUSY
,
736 "port %d already allocated\n", pdev
->id
);
739 port
= devm_kzalloc(&pdev
->dev
, sizeof(struct uart_port
), GFP_KERNEL
);
743 ret
= meson_uart_probe_clocks(pdev
, port
);
747 priv_data
= device_get_match_data(&pdev
->dev
);
749 uart_driver
= meson_uart_current(priv_data
);
751 if (!uart_driver
->state
) {
752 ret
= uart_register_driver(uart_driver
);
754 return dev_err_probe(&pdev
->dev
, ret
,
755 "can't register uart driver\n");
758 port
->iotype
= UPIO_MEM
;
759 port
->mapbase
= res_mem
->start
;
760 port
->mapsize
= resource_size(res_mem
);
762 port
->flags
= UPF_BOOT_AUTOCONF
| UPF_LOW_LATENCY
;
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
;
770 port
->ops
= &meson_uart_ops
;
771 port
->fifosize
= fifosize
;
772 port
->private_data
= (void *)priv_data
;
774 meson_ports
[pdev
->id
] = port
;
775 platform_set_drvdata(pdev
, port
);
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
);
783 ret
= uart_add_one_port(uart_driver
, port
);
785 meson_ports
[pdev
->id
] = NULL
;
790 static void meson_uart_remove(struct platform_device
*pdev
)
792 struct uart_driver
*uart_driver
;
793 struct uart_port
*port
;
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
;
800 for (int id
= 0; id
< AML_UART_PORT_NUM
; id
++)
804 /* No more available uart ports, unregister uart driver */
805 uart_unregister_driver(uart_driver
);
808 static struct meson_uart_data meson_g12a_uart_data
= {
809 .has_xtal_div2
= true,
812 static struct meson_uart_data meson_a1_uart_data
= {
813 .uart_driver
= &meson_uart_driver_ttyS
,
814 .has_xtal_div2
= false,
817 static struct meson_uart_data meson_s4_uart_data
= {
818 .uart_driver
= &meson_uart_driver_ttyS
,
819 .has_xtal_div2
= true,
822 static 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" },
828 .compatible
= "amlogic,meson-g12a-uart",
829 .data
= (void *)&meson_g12a_uart_data
,
832 .compatible
= "amlogic,meson-s4-uart",
833 .data
= (void *)&meson_s4_uart_data
,
836 .compatible
= "amlogic,meson-a1-uart",
837 .data
= (void *)&meson_a1_uart_data
,
841 MODULE_DEVICE_TABLE(of
, meson_uart_dt_match
);
843 static struct platform_driver meson_uart_platform_driver
= {
844 .probe
= meson_uart_probe
,
845 .remove
= meson_uart_remove
,
847 .name
= "meson_uart",
848 .of_match_table
= meson_uart_dt_match
,
852 module_platform_driver(meson_uart_platform_driver
);
854 MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
855 MODULE_DESCRIPTION("Amlogic Meson serial port driver");
856 MODULE_LICENSE("GPL v2");