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 UC2_TE (1 << 3)
18 #define UC2_RE (1 << 2)
20 #define STAT_LBKDIF (1 << 31)
21 #define STAT_RXEDGIF (1 << 30)
22 #define STAT_TDRE (1 << 23)
23 #define STAT_RDRF (1 << 21)
24 #define STAT_IDLE (1 << 20)
25 #define STAT_OR (1 << 19)
26 #define STAT_NF (1 << 18)
27 #define STAT_FE (1 << 17)
28 #define STAT_PF (1 << 16)
29 #define STAT_MA1F (1 << 15)
30 #define STAT_MA2F (1 << 14)
31 #define STAT_FLAGS (STAT_LBKDIF | STAT_RXEDGIF | STAT_IDLE | STAT_OR | \
32 STAT_NF | STAT_FE | STAT_PF | STAT_MA1F | STAT_MA2F)
34 #define CTRL_TE (1 << 19)
35 #define CTRL_RE (1 << 18)
37 #define FIFO_TXFE 0x80
38 #define FIFO_RXFE 0x40
40 #define WATER_TXWATER_OFF 1
41 #define WATER_RXWATER_OFF 16
43 DECLARE_GLOBAL_DATA_PTR;
45 struct lpuart_fsl *base = (struct lpuart_fsl *)LPUART_BASE;
47 #ifndef CONFIG_LPUART_32B_REG
48 static void lpuart_serial_setbrg(void)
50 u32 clk = mxc_get_clock(MXC_UART_CLK);
54 gd->baudrate = CONFIG_BAUDRATE;
56 sbr = (u16)(clk / (16 * gd->baudrate));
57 /* place adjustment later - n/32 BRFA */
59 __raw_writeb(sbr >> 8, &base->ubdh);
60 __raw_writeb(sbr & 0xff, &base->ubdl);
63 static int lpuart_serial_getc(void)
67 while (!(__raw_readb(&base->us1) & US1_RDRF))
70 status = __raw_readb(&base->us1);
72 __raw_writeb(status, &base->us1);
74 return __raw_readb(&base->ud);
77 static void lpuart_serial_putc(const char c)
82 while (!(__raw_readb(&base->us1) & US1_TDRE))
85 __raw_writeb(c, &base->ud);
89 * Test whether a character is in the RX buffer
91 static int lpuart_serial_tstc(void)
93 if (__raw_readb(&base->urcfifo) == 0)
100 * Initialise the serial port with the given baudrate. The settings
101 * are always 8 data bits, no parity, 1 stop bit, no start bits.
103 static int lpuart_serial_init(void)
107 ctrl = __raw_readb(&base->uc2);
110 __raw_writeb(ctrl, &base->uc2);
112 __raw_writeb(0, &base->umodem);
113 __raw_writeb(0, &base->uc1);
115 /* provide data bits, parity, stop bit, etc */
119 __raw_writeb(UC2_RE | UC2_TE, &base->uc2);
124 static struct serial_device lpuart_serial_drv = {
125 .name = "lpuart_serial",
126 .start = lpuart_serial_init,
128 .setbrg = lpuart_serial_setbrg,
129 .putc = lpuart_serial_putc,
130 .puts = default_serial_puts,
131 .getc = lpuart_serial_getc,
132 .tstc = lpuart_serial_tstc,
135 static void lpuart32_serial_setbrg(void)
137 u32 clk = CONFIG_SYS_CLK_FREQ;
141 gd->baudrate = CONFIG_BAUDRATE;
143 sbr = (clk / (16 * gd->baudrate));
144 /* place adjustment later - n/32 BRFA */
146 out_be32(&base->baud, sbr);
149 static int lpuart32_serial_getc(void)
153 while (((stat = in_be32(&base->stat)) & STAT_RDRF) == 0) {
154 out_be32(&base->stat, STAT_FLAGS);
158 return in_be32(&base->data) & 0x3ff;
161 static void lpuart32_serial_putc(const char c)
166 while (!(in_be32(&base->stat) & STAT_TDRE))
169 out_be32(&base->data, c);
173 * Test whether a character is in the RX buffer
175 static int lpuart32_serial_tstc(void)
177 if ((in_be32(&base->water) >> 24) == 0)
184 * Initialise the serial port with the given baudrate. The settings
185 * are always 8 data bits, no parity, 1 stop bit, no start bits.
187 static int lpuart32_serial_init(void)
191 ctrl = in_be32(&base->ctrl);
194 out_be32(&base->ctrl, ctrl);
196 out_be32(&base->modir, 0);
197 out_be32(&base->fifo, ~(FIFO_TXFE | FIFO_RXFE));
199 out_be32(&base->match, 0);
200 /* provide data bits, parity, stop bit, etc */
204 out_be32(&base->ctrl, CTRL_RE | CTRL_TE);
209 static struct serial_device lpuart32_serial_drv = {
210 .name = "lpuart32_serial",
211 .start = lpuart32_serial_init,
213 .setbrg = lpuart32_serial_setbrg,
214 .putc = lpuart32_serial_putc,
215 .puts = default_serial_puts,
216 .getc = lpuart32_serial_getc,
217 .tstc = lpuart32_serial_tstc,
221 void lpuart_serial_initialize(void)
223 #ifdef CONFIG_LPUART_32B_REG
224 serial_register(&lpuart32_serial_drv);
226 serial_register(&lpuart_serial_drv);
230 __weak struct serial_device *default_serial_console(void)
232 #ifdef CONFIG_LPUART_32B_REG
233 return &lpuart32_serial_drv;
235 return &lpuart_serial_drv;