blob: c32e73b71462ffa7f64f360f53596a72a22c55db [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002
3 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include <common.h>
22#if defined(CONFIG_S3C2400) || defined(CONFIG_TRAB)
23#include <s3c2400.h>
24#elif defined(CONFIG_S3C2410)
25#include <s3c2410.h>
26#endif
27
28
29void serial_setbrg (void)
30{
31 DECLARE_GLOBAL_DATA_PTR;
32
33 int i;
34 unsigned int reg = 0;
35
36 /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
37 reg = get_PCLK() / (16 * gd->baudrate) - 1;
38
39#ifdef CONFIG_SERIAL1
40 /* FIFO enable, Tx/Rx FIFO clear */
41 rUFCON0 = 0x07;
42 rUMCON0 = 0x0;
43 /* Normal,No parity,1 stop,8 bit */
44 rULCON0 = 0x3;
45 /*
46 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
47 * normal,interrupt or polling
48 */
49 rUCON0 = 0x245;
50 rUBRDIV0 = reg;
51
52#ifdef CONFIG_HWFLOW
53 rUMCON0 = 0x1; /* RTS up */
54#endif
55 for (i = 0; i < 100; i++);
56#elif CONFIG_SERIAL2
57# if defined(CONFIG_TRAB)
58# #error "TRAB supports only CONFIG_SERIAL1"
59# endif
60 /* FIFO enable, Tx/Rx FIFO clear */
61 rUFCON1 = 0x06;
62 rUMCON1 = 0x0;
63 /* Normal,No parity,1 stop,8 bit */
64 rULCON1 = 0x3;
65 /*
66 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
67 * normal,interrupt or polling
68 */
69 rUCON1 = 0x245;
70 rUBRDIV1 = reg;
71
72#ifdef CONFIG_HWFLOW
73 rUMCON1 = 0x1; /* RTS up */
74#endif
75 for (i = 0; i < 100; i++);
76#else
77#error "Bad: you didn't configure serial ..."
78#endif
79}
80
81/*
82 * Initialise the serial port with the given baudrate. The settings
83 * are always 8 data bits, no parity, 1 stop bit, no start bits.
84 *
85 */
86int serial_init (void)
87{
88 serial_setbrg ();
89
90 return (0);
91}
92
93/*
94 * Read a single byte from the serial port. Returns 1 on success, 0
95 * otherwise. When the function is succesfull, the character read is
96 * written into its argument c.
97 */
98int serial_getc (void)
99{
100#ifdef CONFIG_SERIAL1
101 while (!(rUTRSTAT0 & 0x1));
102
103 return rURXH0 & 0xff;
104#elif CONFIG_SERIAL2
105 while (!(rUTRSTAT1 & 0x1));
106
107 return rURXH1 & 0xff;
108#endif
109}
110
111#ifdef CONFIG_HWFLOW
112static int hwflow = 0; /* turned off by default */
113int hwflow_onoff(int on)
114{
115 switch(on) {
116 case 0:
117 default:
118 break; /* return current */
119 case 1:
120 hwflow = 1; /* turn on */
121 break;
122 case -1:
123 hwflow = 0; /* turn off */
124 break;
125 }
126 return hwflow;
127}
128#endif
129
130#ifdef CONFIG_MODEM_SUPPORT
131static int be_quiet = 0;
132void disable_putc(void)
133{
134 be_quiet = 1;
135}
136
137void enable_putc(void)
138{
139 be_quiet = 0;
140}
141#endif
142
143
144/*
145 * Output a single byte to the serial port.
146 */
147void serial_putc (const char c)
148{
149#ifdef CONFIG_MODEM_SUPPORT
150 if (be_quiet)
151 return;
152#endif
153
154#ifdef CONFIG_SERIAL1
155 /* wait for room in the tx FIFO on SERIAL1 */
156 while (!(rUTRSTAT0 & 0x2));
157
158#ifdef CONFIG_HWFLOW
159 /* Wait for CTS up */
160 while(hwflow && !(rUMSTAT0 & 0x1))
161 ;
162#endif
163
164 rUTXH0 = c;
165#elif CONFIG_SERIAL2
166 /* wait for room in the tx FIFO on SERIAL2 */
167 while (!(rUTRSTAT1 & 0x2));
168
169#ifdef CONFIG_HWFLOW
170 /* Wait for CTS up */
171 while(hwflow && !(rUMSTAT1 & 0x1))
172 ;
173#endif
174 rUTXH1 = c;
175#endif
176
177 /* If \n, also do \r */
178 if (c == '\n')
179 serial_putc ('\r');
180}
181
182/*
183 * Test whether a character is in the RX buffer
184 */
185int serial_tstc (void)
186{
187#ifdef CONFIG_SERIAL1
188 return rUTRSTAT0 & 0x1;
189#elif CONFIG_SERIAL2
190 return rUTRSTAT1 & 0x1;
191#endif
192}
193
194void
195serial_puts (const char *s)
196{
197 while (*s) {
198 serial_putc (*s++);
199 }
200}