]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - cpu/mcf52x2/serial.c
Add support for Freescale M5271: Merge with /work/u-boot.mcf5271
[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_M5271
30 #include <asm/m5271.h>
31 #endif
32
33 #ifdef CONFIG_M5272
34 #include <asm/m5272.h>
35 #endif
36
37 #ifdef CONFIG_M5282
38 #include <asm/m5282.h>
39 #endif
40
41 #ifdef CONFIG_M5249
42 #include <asm/m5249.h>
43 #endif
44
45 DECLARE_GLOBAL_DATA_PTR;
46
47 #ifdef CONFIG_M5249
48 #define DoubleClock(a) ((double)(CFG_CLK/2) / 32.0 / (double)(a))
49 #else
50 #define DoubleClock(a) ((double)(CFG_CLK) / 32.0 / (double)(a))
51 #endif
52
53 void rs_serial_setbaudrate(int port,int baudrate)
54 {
55 #if defined(CONFIG_M5272) || defined(CONFIG_M5249) || defined(CONFIG_M5271)
56         volatile unsigned char  *uartp;
57         double clock, fraction;
58
59         if (port == 0)
60           uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
61         else
62           uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2);
63
64         clock = DoubleClock(baudrate);      /* Set baud above */
65
66         fraction = ((clock - (int)clock) * 16.0) + 0.5;
67
68         uartp[MCFUART_UBG1] = (((int)clock >> 8) & 0xff);  /* set msb baud */
69         uartp[MCFUART_UBG2] = ((int)clock & 0xff);  /* set lsb baud */
70 #ifndef CONFIG_M5271
71         uartp[MCFUART_UFPD] = ((int)fraction & 0xf);  /* set baud fraction adjust */
72 #endif
73 #endif
74 };
75
76 void rs_serial_init(int port,int baudrate)
77 {
78         volatile unsigned char  *uartp;
79
80         /*
81          *      Reset UART, get it into known state...
82          */
83         if (port == 0)
84                 uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
85         else
86                 uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2);
87
88         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;  /* reset RX */
89         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;  /* reset TX */
90         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR;  /* reset MR pointer */
91         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;  /* reset Error pointer */
92
93         /*
94          * Set port for CONSOLE_BAUD_RATE, 8 data bits, 1 stop bit, no parity.
95          */
96         uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
97         uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
98
99         rs_serial_setbaudrate(port,baudrate);
100
101         uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
102         uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
103
104         return;
105 }
106
107 /****************************************************************************/
108 /*
109  *      Output a single character, using UART polled mode.
110  *      This is used for console output.
111  */
112
113 void rs_put_char(char ch)
114 {
115         volatile unsigned char  *uartp;
116         int                     i;
117
118         uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
119
120         for (i = 0; (i < 0x10000); i++) {
121                 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
122                         break;
123         }
124         uartp[MCFUART_UTB] = ch;
125         return;
126 }
127
128 int rs_is_char(void)
129 {
130         volatile unsigned char  *uartp;
131
132         uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
133         return((uartp[MCFUART_USR] & MCFUART_USR_RXREADY) ? 1 : 0);
134 }
135
136 int rs_get_char(void)
137 {
138         volatile unsigned char  *uartp;
139
140         uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
141         return(uartp[MCFUART_URB]);
142 }
143
144 void serial_setbrg(void) {
145         rs_serial_setbaudrate(0,gd->bd->bi_baudrate);
146 }
147
148 int serial_init(void) {
149         rs_serial_init(0,gd->baudrate);
150         return 0;
151 }
152
153
154 void serial_putc(const char c) {
155         if (c == '\n')
156                 serial_putc ('\r');
157         rs_put_char(c);
158 }
159
160 void serial_puts (const char *s) {
161         while (*s) {
162                 serial_putc(*s++);
163         }
164 }
165
166 int serial_getc(void) {
167         while(!rs_is_char());
168         return rs_get_char();
169 }
170
171 int serial_tstc() {
172         return rs_is_char();
173 }