2 * Copyright 2013 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
11 #include <linux/compiler.h>
12 #include <asm/arch/imx-regs.h>
13 #include <asm/arch/clock.h>
15 #define US1_TDRE (1 << 7)
16 #define US1_RDRF (1 << 5)
17 #define US1_OR (1 << 3)
18 #define UC2_TE (1 << 3)
19 #define UC2_RE (1 << 2)
21 DECLARE_GLOBAL_DATA_PTR;
23 struct lpuart_fsl *base = (struct lpuart_fsl *)LPUART_BASE;
25 static void lpuart_serial_setbrg(void)
27 u32 clk = mxc_get_clock(MXC_UART_CLK);
31 gd->baudrate = CONFIG_BAUDRATE;
33 sbr = (u16)(clk / (16 * gd->baudrate));
34 /* place adjustment later - n/32 BRFA */
36 __raw_writeb(sbr >> 8, &base->ubdh);
37 __raw_writeb(sbr & 0xff, &base->ubdl);
40 static int lpuart_serial_getc(void)
42 while (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR)))
47 return __raw_readb(&base->ud);
50 static void lpuart_serial_putc(const char c)
55 while (!(__raw_readb(&base->us1) & US1_TDRE))
58 __raw_writeb(c, &base->ud);
62 * Test whether a character is in the RX buffer
64 static int lpuart_serial_tstc(void)
66 if (__raw_readb(&base->urcfifo) == 0)
73 * Initialise the serial port with the given baudrate. The settings
74 * are always 8 data bits, no parity, 1 stop bit, no start bits.
76 static int lpuart_serial_init(void)
80 ctrl = __raw_readb(&base->uc2);
83 __raw_writeb(ctrl, &base->uc2);
85 __raw_writeb(0, &base->umodem);
86 __raw_writeb(0, &base->uc1);
88 /* provide data bits, parity, stop bit, etc */
92 __raw_writeb(UC2_RE | UC2_TE, &base->uc2);
97 static struct serial_device lpuart_serial_drv = {
98 .name = "lpuart_serial",
99 .start = lpuart_serial_init,
101 .setbrg = lpuart_serial_setbrg,
102 .putc = lpuart_serial_putc,
103 .puts = default_serial_puts,
104 .getc = lpuart_serial_getc,
105 .tstc = lpuart_serial_tstc,
108 void lpuart_serial_initialize(void)
110 serial_register(&lpuart_serial_drv);
113 __weak struct serial_device *default_serial_console(void)
115 return &lpuart_serial_drv;