]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - cpu/mcf52x2/serial.c
Add Altera Nios-II boards EP1C20, EP1S10 and EP1S40
[karo-tx-uboot.git] / cpu / mcf52x2 / serial.c
1 /*
2  * (C) Copyright 2000-2004
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <command.h>
26
27 #include <asm/mcfuart.h>
28
29 #ifdef CONFIG_M5272
30 #include <asm/m5272.h>
31 #endif
32
33 #ifdef CONFIG_M5282
34 #include <asm/m5282.h>
35 #endif
36
37 #ifdef CONFIG_M5249
38 #include <asm/m5249.h>
39 #endif
40
41 DECLARE_GLOBAL_DATA_PTR;
42
43 #ifdef CONFIG_M5249
44 #define DoubleClock(a) ((double)(CFG_CLK/2) / 32.0 / (double)(a))
45 #else
46 #define DoubleClock(a) ((double)(CFG_CLK) / 32.0 / (double)(a))
47 #endif
48
49 void rs_serial_setbaudrate(int port,int baudrate)
50 {
51 #if defined(CONFIG_M5272) || defined(CONFIG_M5249)
52         volatile unsigned char  *uartp;
53         double clock, fraction;
54
55         if (port == 0)
56           uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
57         else
58           uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2);
59
60         clock = DoubleClock(baudrate);      /* Set baud above */
61
62         fraction = ((clock - (int)clock) * 16.0) + 0.5;
63
64         uartp[MCFUART_UBG1] = (((int)clock >> 8) & 0xff);  /* set msb baud */
65         uartp[MCFUART_UBG2] = ((int)clock & 0xff);  /* set lsb baud */
66         uartp[MCFUART_UFPD] = ((int)fraction & 0xf);  /* set baud fraction adjust */
67 #endif
68 #if  defined(CONFIG_M5282)
69         volatile unsigned char  *uartp;
70         long clock;
71
72         switch (port)
73         {
74          case 1:
75           uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2);
76           break;
77          case 2:
78           uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE3);
79           break;
80          default:
81           uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
82         }
83
84         clock = (long) CFG_CLK / ((long) 32 * baudrate);      /* Set baud above */
85
86         uartp[MCFUART_UBG1] = (((int)clock >> 8) & 0xff);  /* set msb baud */
87         uartp[MCFUART_UBG2] = ((int) clock & 0xff);  /* set lsb baud */
88
89 #endif
90 };
91
92 void rs_serial_init(int port,int baudrate)
93 {
94         volatile unsigned char  *uartp;
95
96         /*
97          *      Reset UART, get it into known state...
98          */
99         switch (port)
100         {
101          case 1:
102           uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2);
103           break;
104         #if  defined(CONFIG_M5282)
105          case 2:
106           uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE3);
107           break;
108          #endif
109          default:
110           uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
111         }
112
113         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;  /* reset RX */
114         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;  /* reset TX */
115         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR;  /* reset MR pointer */
116         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;  /* reset Error pointer */
117
118         /*
119          * Set port for CONSOLE_BAUD_RATE, 8 data bits, 1 stop bit, no parity.
120          */
121         uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
122         uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
123
124         rs_serial_setbaudrate(port,baudrate);
125
126         uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
127         uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
128
129         return;
130 }
131
132 /****************************************************************************/
133 /*
134  *      Output a single character, using UART polled mode.
135  *      This is used for console output.
136  */
137
138 void rs_put_char(char ch)
139 {
140         volatile unsigned char  *uartp;
141         int                     i;
142
143         uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
144
145         for (i = 0; (i < 0x10000); i++) {
146                 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
147                         break;
148         }
149         uartp[MCFUART_UTB] = ch;
150         return;
151 }
152
153 int rs_is_char(void)
154 {
155         volatile unsigned char  *uartp;
156
157         uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
158         return((uartp[MCFUART_USR] & MCFUART_USR_RXREADY) ? 1 : 0);
159 }
160
161 int rs_get_char(void)
162 {
163         volatile unsigned char  *uartp;
164
165         uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
166         return(uartp[MCFUART_URB]);
167 }
168
169 void serial_setbrg(void) {
170         rs_serial_setbaudrate(0,gd->bd->bi_baudrate);
171 }
172
173 int serial_init(void) {
174         rs_serial_init(0,gd->baudrate);
175         return 0;
176 }
177
178
179 void serial_putc(const char c) {
180         if (c == '\n')
181                 serial_putc ('\r');
182         rs_put_char(c);
183 }
184
185 void serial_puts (const char *s) {
186         while (*s) {
187                 serial_putc(*s++);
188         }
189 }
190
191 int serial_getc(void) {
192         while(!rs_is_char());
193         return rs_get_char();
194 }
195
196 int serial_tstc() {
197         return rs_is_char();
198 }