2 * (c) 2004 Sascha Hauer <sascha@saschahauer.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <asm/arch/imx-regs.h>
23 #include <linux/compiler.h>
25 #if defined CONFIG_IMX_SERIAL1
26 #define UART_BASE IMX_UART1_BASE
27 #elif defined CONFIG_IMX_SERIAL2
28 #define UART_BASE IMX_UART2_BASE
30 #error "define CONFIG_IMX_SERIAL1, CONFIG_IMX_SERIAL2 or CONFIG_IMX_SERIAL_NONE"
34 volatile uint32_t urxd[16];
35 volatile uint32_t utxd[16];
36 volatile uint32_t ucr1;
37 volatile uint32_t ucr2;
38 volatile uint32_t ucr3;
39 volatile uint32_t ucr4;
40 volatile uint32_t ufcr;
41 volatile uint32_t usr1;
42 volatile uint32_t usr2;
43 volatile uint32_t uesc;
44 volatile uint32_t utim;
45 volatile uint32_t ubir;
46 volatile uint32_t ubmr;
47 volatile uint32_t ubrc;
48 volatile uint32_t bipr[4];
49 volatile uint32_t bmpr[4];
50 volatile uint32_t uts;
53 DECLARE_GLOBAL_DATA_PTR;
55 static void imx_serial_setbrg(void)
60 extern void imx_gpio_mode(int gpio_mode);
63 * Initialise the serial port with the given baudrate. The settings
64 * are always 8 data bits, no parity, 1 stop bit, no start bits.
67 static int imx_serial_init(void)
69 volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
70 unsigned int ufcr_rfdiv;
73 #ifdef CONFIG_IMX_SERIAL1
74 imx_gpio_mode(PC11_PF_UART1_TXD);
75 imx_gpio_mode(PC12_PF_UART1_RXD);
77 imx_gpio_mode(PB30_PF_UART2_TXD);
78 imx_gpio_mode(PB31_PF_UART2_RXD);
82 base->ucr1 &= ~UCR1_UARTEN;
84 /* Set to default POR state */
86 base->ucr1 = 0x00000004;
87 base->ucr2 = 0x00000000;
88 base->ucr3 = 0x00000000;
89 base->ucr4 = 0x00008040;
90 base->uesc = 0x0000002B;
91 base->utim = 0x00000000;
92 base->ubir = 0x00000000;
93 base->ubmr = 0x00000000;
94 base->uts = 0x00000000;
96 base->ucr4 |= UCR4_REF16;
101 /* set the baud rate.
110 * each register is 16 bits wide. refclk max is 96 MHz
114 ufcr_rfdiv = ((base->ufcr) & UFCR_RFDIV) >> 7;
118 ufcr_rfdiv = 6 - ufcr_rfdiv;
120 refclk = get_PERCLK1();
121 refclk /= ufcr_rfdiv;
123 /* Set the numerator value minus one of the BRM ratio */
124 base->ubir = (gd->baudrate / 100) - 1;
126 /* Set the denominator value minus one of the BRM ratio */
127 base->ubmr = (refclk/(16 * 100)) - 1;
130 base->ucr2 &= ~UCR2_PREN;
131 base->ucr2 |= UCR2_WS;
132 base->ucr2 &= ~UCR2_STPB;
135 base->ucr2 |= UCR2_IRTS;
138 base->ucr1 |= UCR1_UARTEN | UCR1_UARTCLKEN;
141 base->ucr2 |= UCR2_SRST | UCR2_RXEN | UCR2_TXEN;
143 /* Clear status flags */
144 base->usr2 |= USR2_ADET |
153 /* Clear status flags */
154 base->usr1 |= USR1_PARITYERR |
164 * Read a single byte from the serial port. Returns 1 on success, 0
165 * otherwise. When the function is successful, the character read is
166 * written into its argument c.
168 static int imx_serial_getc(void)
170 volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
173 while(base->uts & UTS_RXEMPTY);
175 ch = (char)base->urxd[0];
181 static int hwflow = 0; /* turned off by default */
182 int hwflow_onoff(int on)
188 * Output a single byte to the serial port.
190 static void imx_serial_putc(const char c)
192 volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
194 /* Wait for Tx FIFO not full */
195 while (base->uts & UTS_TXFULL);
199 /* If \n, also do \r */
205 * Test whether a character is in the RX buffer
207 static int imx_serial_tstc(void)
209 volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
211 /* If receive fifo is empty, return false */
212 if (base->uts & UTS_RXEMPTY)
217 static struct serial_device imx_serial_drv = {
218 .name = "imx_serial",
219 .start = imx_serial_init,
221 .setbrg = imx_serial_setbrg,
222 .putc = imx_serial_putc,
223 .puts = default_serial_puts,
224 .getc = imx_serial_getc,
225 .tstc = imx_serial_tstc,
228 void imx_serial_initialize(void)
230 serial_register(&imx_serial_drv);
233 __weak struct serial_device *default_serial_console(void)
235 return &imx_serial_drv;