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 DECLARE_GLOBAL_DATA_PTR
;
22 struct lpuart_fsl
*base
= (struct lpuart_fsl
*)LPUART_BASE
;
24 static void lpuart_serial_setbrg(void)
26 u32 clk
= mxc_get_clock(MXC_UART_CLK
);
30 gd
->baudrate
= CONFIG_BAUDRATE
;
32 sbr
= (u16
)(clk
/ (16 * gd
->baudrate
));
33 /* place adjustment later - n/32 BRFA */
35 __raw_writeb(sbr
>> 8, &base
->ubdh
);
36 __raw_writeb(sbr
& 0xff, &base
->ubdl
);
39 static int lpuart_serial_getc(void)
43 while (!(__raw_readb(&base
->us1
) & US1_RDRF
))
46 status
= __raw_readb(&base
->us1
);
48 __raw_writeb(status
, &base
->us1
);
50 return __raw_readb(&base
->ud
);
53 static void lpuart_serial_putc(const char c
)
58 while (!(__raw_readb(&base
->us1
) & US1_TDRE
))
61 __raw_writeb(c
, &base
->ud
);
65 * Test whether a character is in the RX buffer
67 static int lpuart_serial_tstc(void)
69 if (__raw_readb(&base
->urcfifo
) == 0)
76 * Initialise the serial port with the given baudrate. The settings
77 * are always 8 data bits, no parity, 1 stop bit, no start bits.
79 static int lpuart_serial_init(void)
83 ctrl
= __raw_readb(&base
->uc2
);
86 __raw_writeb(ctrl
, &base
->uc2
);
88 __raw_writeb(0, &base
->umodem
);
89 __raw_writeb(0, &base
->uc1
);
91 /* provide data bits, parity, stop bit, etc */
95 __raw_writeb(UC2_RE
| UC2_TE
, &base
->uc2
);
100 static struct serial_device lpuart_serial_drv
= {
101 .name
= "lpuart_serial",
102 .start
= lpuart_serial_init
,
104 .setbrg
= lpuart_serial_setbrg
,
105 .putc
= lpuart_serial_putc
,
106 .puts
= default_serial_puts
,
107 .getc
= lpuart_serial_getc
,
108 .tstc
= lpuart_serial_tstc
,
111 void lpuart_serial_initialize(void)
113 serial_register(&lpuart_serial_drv
);
116 __weak
struct serial_device
*default_serial_console(void)
118 return &lpuart_serial_drv
;