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