blob: 6104e7528e3aef3d00ee6166ad0e1635c8efd6da [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
wdenk48b42612003-06-19 23:01:32 +000028#ifdef CONFIG_SERIAL1
29#define UART_NR S3C24X0_UART0
30
31#elif CONFIG_SERIAL2
32# if defined(CONFIG_TRAB)
wdenkf54ebdf2003-09-17 15:10:32 +000033# error "TRAB supports only CONFIG_SERIAL1"
wdenk48b42612003-06-19 23:01:32 +000034# endif
35#define UART_NR S3C24X0_UART1
36
37#elif CONFIG_SERIAL3
38# if defined(CONFIG_TRAB)
39# #error "TRAB supports only CONFIG_SERIAL1"
40# endif
41#define UART_NR S3C24X0_UART2
42
43#else
44#error "Bad: you didn't configure serial ..."
45#endif
wdenkc6097192002-11-03 00:24:07 +000046
47void serial_setbrg (void)
48{
49 DECLARE_GLOBAL_DATA_PTR;
wdenk48b42612003-06-19 23:01:32 +000050 S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR);
wdenkc6097192002-11-03 00:24:07 +000051 int i;
52 unsigned int reg = 0;
53
54 /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
55 reg = get_PCLK() / (16 * gd->baudrate) - 1;
56
wdenkc6097192002-11-03 00:24:07 +000057 /* FIFO enable, Tx/Rx FIFO clear */
wdenk48b42612003-06-19 23:01:32 +000058 uart->UFCON = 0x07;
59 uart->UMCON = 0x0;
wdenkc6097192002-11-03 00:24:07 +000060 /* Normal,No parity,1 stop,8 bit */
wdenk48b42612003-06-19 23:01:32 +000061 uart->ULCON = 0x3;
wdenkc6097192002-11-03 00:24:07 +000062 /*
63 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
64 * normal,interrupt or polling
65 */
wdenk48b42612003-06-19 23:01:32 +000066 uart->UCON = 0x245;
67 uart->UBRDIV = reg;
wdenkc6097192002-11-03 00:24:07 +000068
69#ifdef CONFIG_HWFLOW
wdenk48b42612003-06-19 23:01:32 +000070 uart->UMCON = 0x1; /* RTS up */
wdenkc6097192002-11-03 00:24:07 +000071#endif
72 for (i = 0; i < 100; i++);
wdenkc6097192002-11-03 00:24:07 +000073}
74
75/*
76 * Initialise the serial port with the given baudrate. The settings
77 * are always 8 data bits, no parity, 1 stop bit, no start bits.
78 *
79 */
80int serial_init (void)
81{
82 serial_setbrg ();
83
84 return (0);
85}
86
87/*
88 * Read a single byte from the serial port. Returns 1 on success, 0
89 * otherwise. When the function is succesfull, the character read is
90 * written into its argument c.
91 */
92int serial_getc (void)
93{
wdenk48b42612003-06-19 23:01:32 +000094 S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR);
wdenk8bde7f72003-06-27 21:31:46 +000095
wdenk48b42612003-06-19 23:01:32 +000096 /* wait for character to arrive */
97 while (!(uart->UTRSTAT & 0x1));
wdenkc6097192002-11-03 00:24:07 +000098
wdenk48b42612003-06-19 23:01:32 +000099 return uart->URXH & 0xff;
wdenkc6097192002-11-03 00:24:07 +0000100}
101
102#ifdef CONFIG_HWFLOW
103static int hwflow = 0; /* turned off by default */
104int hwflow_onoff(int on)
105{
106 switch(on) {
107 case 0:
108 default:
109 break; /* return current */
110 case 1:
111 hwflow = 1; /* turn on */
112 break;
113 case -1:
114 hwflow = 0; /* turn off */
115 break;
116 }
117 return hwflow;
118}
119#endif
120
121#ifdef CONFIG_MODEM_SUPPORT
122static int be_quiet = 0;
123void disable_putc(void)
124{
125 be_quiet = 1;
126}
127
128void enable_putc(void)
129{
130 be_quiet = 0;
131}
132#endif
133
134
135/*
136 * Output a single byte to the serial port.
137 */
138void serial_putc (const char c)
139{
wdenk48b42612003-06-19 23:01:32 +0000140 S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR);
wdenkc6097192002-11-03 00:24:07 +0000141#ifdef CONFIG_MODEM_SUPPORT
142 if (be_quiet)
143 return;
144#endif
145
wdenk48b42612003-06-19 23:01:32 +0000146 /* wait for room in the tx FIFO */
147 while (!(uart->UTRSTAT & 0x2));
wdenkc6097192002-11-03 00:24:07 +0000148
149#ifdef CONFIG_HWFLOW
150 /* Wait for CTS up */
wdenk48b42612003-06-19 23:01:32 +0000151 while(hwflow && !(uart->UMSTAT & 0x1))
wdenkc6097192002-11-03 00:24:07 +0000152 ;
153#endif
154
wdenk48b42612003-06-19 23:01:32 +0000155 uart->UTXH = c;
wdenkc6097192002-11-03 00:24:07 +0000156
157 /* If \n, also do \r */
158 if (c == '\n')
159 serial_putc ('\r');
160}
161
162/*
163 * Test whether a character is in the RX buffer
164 */
165int serial_tstc (void)
166{
wdenk48b42612003-06-19 23:01:32 +0000167 S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR);
168
169 return uart->UTRSTAT & 0x1;
wdenkc6097192002-11-03 00:24:07 +0000170}
171
172void
173serial_puts (const char *s)
174{
175 while (*s) {
176 serial_putc (*s++);
177 }
178}