Merge branch 'next'
[u-boot/qq2440-u-boot.git] / drivers / serial / serial_imx.c
blobd43a5fedcc87ed782542b88191d4d162b869a481
1 /*
2 * (c) 2004 Sascha Hauer <sascha@saschahauer.de>
4 * SPDX-License-Identifier: GPL-2.0+
5 */
7 #include <common.h>
8 #include <asm/arch/imx-regs.h>
9 #include <serial.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
16 #else
17 #error "define CONFIG_IMX_SERIAL1, CONFIG_IMX_SERIAL2 or CONFIG_IMX_SERIAL_NONE"
18 #endif
20 struct imx_serial {
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)
44 serial_init();
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;
58 unsigned int refclk;
60 #ifdef CONFIG_IMX_SERIAL1
61 imx_gpio_mode(PC11_PF_UART1_TXD);
62 imx_gpio_mode(PC12_PF_UART1_RXD);
63 #else
64 imx_gpio_mode(PB30_PF_UART2_TXD);
65 imx_gpio_mode(PB31_PF_UART2_RXD);
66 #endif
68 /* Disable UART */
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;
82 /* Set clocks */
83 base->ucr4 |= UCR4_REF16;
85 /* Configure FIFOs */
86 base->ufcr = 0xa81;
88 /* set the baud rate.
90 * baud * 16 x
91 * --------- = -
92 * refclk y
94 * x - 1 = UBIR
95 * y - 1 = UBMR
97 * each register is 16 bits wide. refclk max is 96 MHz
101 ufcr_rfdiv = ((base->ufcr) & UFCR_RFDIV) >> 7;
102 if (ufcr_rfdiv == 6)
103 ufcr_rfdiv = 7;
104 else
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;
116 /* Set to 8N1 */
117 base->ucr2 &= ~UCR2_PREN;
118 base->ucr2 |= UCR2_WS;
119 base->ucr2 &= ~UCR2_STPB;
121 /* Ignore RTS */
122 base->ucr2 |= UCR2_IRTS;
124 /* Enable UART */
125 base->ucr1 |= UCR1_UARTEN | UCR1_UARTCLKEN;
127 /* Enable FIFOs */
128 base->ucr2 |= UCR2_SRST | UCR2_RXEN | UCR2_TXEN;
130 /* Clear status flags */
131 base->usr2 |= USR2_ADET |
132 USR2_DTRF |
133 USR2_IDLE |
134 USR2_IRINT |
135 USR2_WAKE |
136 USR2_RTSF |
137 USR2_BRCD |
138 USR2_ORE;
140 /* Clear status flags */
141 base->usr1 |= USR1_PARITYERR |
142 USR1_RTSD |
143 USR1_ESCF |
144 USR1_FRAMERR |
145 USR1_AIRINT |
146 USR1_AWAKE;
147 return (0);
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;
158 unsigned char ch;
160 while(base->uts & UTS_RXEMPTY);
162 ch = (char)base->urxd[0];
164 return ch;
167 #ifdef CONFIG_HWFLOW
168 static int hwflow = 0; /* turned off by default */
169 int hwflow_onoff(int on)
172 #endif
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);
184 base->utxd[0] = c;
186 /* If \n, also do \r */
187 if (c == '\n')
188 serial_putc ('\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)
200 return 0;
201 return 1;
204 static struct serial_device imx_serial_drv = {
205 .name = "imx_serial",
206 .start = imx_serial_init,
207 .stop = NULL,
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;