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