2 * (c) 2004 Sascha Hauer <sascha@saschahauer.de>
4 * SPDX-License-Identifier: GPL-2.0+
8 #include <asm/arch/imx-regs.h>
10 #include <linux/compiler.h>
12 #if defined CONFIG_IMX_SERIAL1
13 #define UART_BASE IMX_UART1_BASE
14 #elif defined CONFIG_IMX_SERIAL2
15 #define UART_BASE IMX_UART2_BASE
17 #error "define CONFIG_IMX_SERIAL1, CONFIG_IMX_SERIAL2 or CONFIG_IMX_SERIAL_NONE"
21 volatile uint32_t urxd
[16];
22 volatile uint32_t utxd
[16];
23 volatile uint32_t ucr1
;
24 volatile uint32_t ucr2
;
25 volatile uint32_t ucr3
;
26 volatile uint32_t ucr4
;
27 volatile uint32_t ufcr
;
28 volatile uint32_t usr1
;
29 volatile uint32_t usr2
;
30 volatile uint32_t uesc
;
31 volatile uint32_t utim
;
32 volatile uint32_t ubir
;
33 volatile uint32_t ubmr
;
34 volatile uint32_t ubrc
;
35 volatile uint32_t bipr
[4];
36 volatile uint32_t bmpr
[4];
37 volatile uint32_t uts
;
40 DECLARE_GLOBAL_DATA_PTR
;
42 static void imx_serial_setbrg(void)
47 extern void imx_gpio_mode(int gpio_mode
);
50 * Initialise the serial port with the given baudrate. The settings
51 * are always 8 data bits, no parity, 1 stop bit, no start bits.
54 static int imx_serial_init(void)
56 volatile struct imx_serial
* base
= (struct imx_serial
*)UART_BASE
;
57 unsigned int ufcr_rfdiv
;
60 #ifdef CONFIG_IMX_SERIAL1
61 imx_gpio_mode(PC11_PF_UART1_TXD
);
62 imx_gpio_mode(PC12_PF_UART1_RXD
);
64 imx_gpio_mode(PB30_PF_UART2_TXD
);
65 imx_gpio_mode(PB31_PF_UART2_RXD
);
69 base
->ucr1
&= ~UCR1_UARTEN
;
71 /* Set to default POR state */
73 base
->ucr1
= 0x00000004;
74 base
->ucr2
= 0x00000000;
75 base
->ucr3
= 0x00000000;
76 base
->ucr4
= 0x00008040;
77 base
->uesc
= 0x0000002B;
78 base
->utim
= 0x00000000;
79 base
->ubir
= 0x00000000;
80 base
->ubmr
= 0x00000000;
81 base
->uts
= 0x00000000;
83 base
->ucr4
|= UCR4_REF16
;
97 * each register is 16 bits wide. refclk max is 96 MHz
101 ufcr_rfdiv
= ((base
->ufcr
) & UFCR_RFDIV
) >> 7;
105 ufcr_rfdiv
= 6 - ufcr_rfdiv
;
107 refclk
= get_PERCLK1();
108 refclk
/= ufcr_rfdiv
;
110 /* Set the numerator value minus one of the BRM ratio */
111 base
->ubir
= (gd
->baudrate
/ 100) - 1;
113 /* Set the denominator value minus one of the BRM ratio */
114 base
->ubmr
= (refclk
/(16 * 100)) - 1;
117 base
->ucr2
&= ~UCR2_PREN
;
118 base
->ucr2
|= UCR2_WS
;
119 base
->ucr2
&= ~UCR2_STPB
;
122 base
->ucr2
|= UCR2_IRTS
;
125 base
->ucr1
|= UCR1_UARTEN
| UCR1_UARTCLKEN
;
128 base
->ucr2
|= UCR2_SRST
| UCR2_RXEN
| UCR2_TXEN
;
130 /* Clear status flags */
131 base
->usr2
|= USR2_ADET
|
140 /* Clear status flags */
141 base
->usr1
|= USR1_PARITYERR
|
151 * Read a single byte from the serial port. Returns 1 on success, 0
152 * otherwise. When the function is successful, the character read is
153 * written into its argument c.
155 static int imx_serial_getc(void)
157 volatile struct imx_serial
* base
= (struct imx_serial
*)UART_BASE
;
160 while(base
->uts
& UTS_RXEMPTY
);
162 ch
= (char)base
->urxd
[0];
168 static int hwflow
= 0; /* turned off by default */
169 int hwflow_onoff(int on
)
175 * Output a single byte to the serial port.
177 static void imx_serial_putc(const char c
)
179 volatile struct imx_serial
* base
= (struct imx_serial
*)UART_BASE
;
181 /* Wait for Tx FIFO not full */
182 while (base
->uts
& UTS_TXFULL
);
186 /* If \n, also do \r */
192 * Test whether a character is in the RX buffer
194 static int imx_serial_tstc(void)
196 volatile struct imx_serial
* base
= (struct imx_serial
*)UART_BASE
;
198 /* If receive fifo is empty, return false */
199 if (base
->uts
& UTS_RXEMPTY
)
204 static struct serial_device imx_serial_drv
= {
205 .name
= "imx_serial",
206 .start
= imx_serial_init
,
208 .setbrg
= imx_serial_setbrg
,
209 .putc
= imx_serial_putc
,
210 .puts
= default_serial_puts
,
211 .getc
= imx_serial_getc
,
212 .tstc
= imx_serial_tstc
,
215 void imx_serial_initialize(void)
217 serial_register(&imx_serial_drv
);
220 __weak
struct serial_device
*default_serial_console(void)
222 return &imx_serial_drv
;