blob: 9b9be44c528d3916b06f28267ec9ad8406befabf [file] [log] [blame]
wdenk281e00a2004-08-01 22:48:16 +00001/*
2 * (c) 2004 Sascha Hauer <sascha@saschahauer.de>
3 *
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.
8 *
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.
13 *
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
17 *
18 */
19
20#include <common.h>
wdenk281e00a2004-08-01 22:48:16 +000021#include <asm/arch/imx-regs.h>
Marek Vasut7882e7c2012-09-14 22:34:51 +020022#include <serial.h>
23#include <linux/compiler.h>
wdenk281e00a2004-08-01 22:48:16 +000024
wdenk281e00a2004-08-01 22:48:16 +000025#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
29#else
30#error "define CONFIG_IMX_SERIAL1, CONFIG_IMX_SERIAL2 or CONFIG_IMX_SERIAL_NONE"
31#endif
32
33struct imx_serial {
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;
51};
52
Andrew Dyer48fed402008-09-12 02:20:46 +020053DECLARE_GLOBAL_DATA_PTR;
54
Marek Vasut7882e7c2012-09-14 22:34:51 +020055static void imx_serial_setbrg(void)
wdenk281e00a2004-08-01 22:48:16 +000056{
57 serial_init();
58}
59
60extern void imx_gpio_mode(int gpio_mode);
61
62/*
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.
65 *
66 */
Marek Vasut7882e7c2012-09-14 22:34:51 +020067static int imx_serial_init(void)
wdenk281e00a2004-08-01 22:48:16 +000068{
69 volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
Andrew Dyer48fed402008-09-12 02:20:46 +020070 unsigned int ufcr_rfdiv;
71 unsigned int refclk;
72
wdenk281e00a2004-08-01 22:48:16 +000073#ifdef CONFIG_IMX_SERIAL1
74 imx_gpio_mode(PC11_PF_UART1_TXD);
75 imx_gpio_mode(PC12_PF_UART1_RXD);
76#else
77 imx_gpio_mode(PB30_PF_UART2_TXD);
78 imx_gpio_mode(PB31_PF_UART2_RXD);
79#endif
80
81 /* Disable UART */
82 base->ucr1 &= ~UCR1_UARTEN;
83
84 /* Set to default POR state */
85
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;
95 /* Set clocks */
96 base->ucr4 |= UCR4_REF16;
97
98 /* Configure FIFOs */
99 base->ufcr = 0xa81;
100
Andrew Dyer48fed402008-09-12 02:20:46 +0200101 /* set the baud rate.
102 *
103 * baud * 16 x
104 * --------- = -
105 * refclk y
106 *
107 * x - 1 = UBIR
108 * y - 1 = UBMR
109 *
110 * each register is 16 bits wide. refclk max is 96 MHz
111 *
112 */
113
114 ufcr_rfdiv = ((base->ufcr) & UFCR_RFDIV) >> 7;
115 if (ufcr_rfdiv == 6)
116 ufcr_rfdiv = 7;
117 else
118 ufcr_rfdiv = 6 - ufcr_rfdiv;
119
120 refclk = get_PERCLK1();
121 refclk /= ufcr_rfdiv;
122
wdenk281e00a2004-08-01 22:48:16 +0000123 /* Set the numerator value minus one of the BRM ratio */
Andrew Dyer48fed402008-09-12 02:20:46 +0200124 base->ubir = (gd->baudrate / 100) - 1;
wdenk281e00a2004-08-01 22:48:16 +0000125
126 /* Set the denominator value minus one of the BRM ratio */
Andrew Dyer48fed402008-09-12 02:20:46 +0200127 base->ubmr = (refclk/(16 * 100)) - 1;
wdenk281e00a2004-08-01 22:48:16 +0000128
129 /* Set to 8N1 */
130 base->ucr2 &= ~UCR2_PREN;
131 base->ucr2 |= UCR2_WS;
132 base->ucr2 &= ~UCR2_STPB;
133
134 /* Ignore RTS */
135 base->ucr2 |= UCR2_IRTS;
136
137 /* Enable UART */
138 base->ucr1 |= UCR1_UARTEN | UCR1_UARTCLKEN;
139
140 /* Enable FIFOs */
141 base->ucr2 |= UCR2_SRST | UCR2_RXEN | UCR2_TXEN;
142
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200143 /* Clear status flags */
wdenk281e00a2004-08-01 22:48:16 +0000144 base->usr2 |= USR2_ADET |
Andrew Dyer48fed402008-09-12 02:20:46 +0200145 USR2_DTRF |
146 USR2_IDLE |
147 USR2_IRINT |
148 USR2_WAKE |
149 USR2_RTSF |
150 USR2_BRCD |
151 USR2_ORE;
wdenk281e00a2004-08-01 22:48:16 +0000152
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200153 /* Clear status flags */
wdenk281e00a2004-08-01 22:48:16 +0000154 base->usr1 |= USR1_PARITYERR |
Andrew Dyer48fed402008-09-12 02:20:46 +0200155 USR1_RTSD |
156 USR1_ESCF |
157 USR1_FRAMERR |
158 USR1_AIRINT |
159 USR1_AWAKE;
wdenk281e00a2004-08-01 22:48:16 +0000160 return (0);
161}
162
163/*
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.
167 */
Marek Vasut7882e7c2012-09-14 22:34:51 +0200168static int imx_serial_getc(void)
wdenk281e00a2004-08-01 22:48:16 +0000169{
170 volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
171 unsigned char ch;
172
173 while(base->uts & UTS_RXEMPTY);
174
175 ch = (char)base->urxd[0];
176
177 return ch;
178}
179
180#ifdef CONFIG_HWFLOW
181static int hwflow = 0; /* turned off by default */
182int hwflow_onoff(int on)
183{
184}
185#endif
186
187/*
188 * Output a single byte to the serial port.
189 */
Marek Vasut7882e7c2012-09-14 22:34:51 +0200190static void imx_serial_putc(const char c)
wdenk281e00a2004-08-01 22:48:16 +0000191{
192 volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
193
194 /* Wait for Tx FIFO not full */
195 while (base->uts & UTS_TXFULL);
196
197 base->utxd[0] = c;
198
199 /* If \n, also do \r */
200 if (c == '\n')
201 serial_putc ('\r');
202}
203
204/*
205 * Test whether a character is in the RX buffer
206 */
Marek Vasut7882e7c2012-09-14 22:34:51 +0200207static int imx_serial_tstc(void)
wdenk281e00a2004-08-01 22:48:16 +0000208{
209 volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
210
211 /* If receive fifo is empty, return false */
212 if (base->uts & UTS_RXEMPTY)
213 return 0;
214 return 1;
215}
216
Marek Vasut7882e7c2012-09-14 22:34:51 +0200217static struct serial_device imx_serial_drv = {
218 .name = "imx_serial",
219 .start = imx_serial_init,
220 .stop = NULL,
221 .setbrg = imx_serial_setbrg,
222 .putc = imx_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000223 .puts = default_serial_puts,
Marek Vasut7882e7c2012-09-14 22:34:51 +0200224 .getc = imx_serial_getc,
225 .tstc = imx_serial_tstc,
226};
227
228void imx_serial_initialize(void)
229{
230 serial_register(&imx_serial_drv);
231}
232
233__weak struct serial_device *default_serial_console(void)
234{
235 return &imx_serial_drv;
236}