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