ddcd591f84a3b0bd08c12bf890cf89b750d850d5
[platform/kernel/u-boot.git] / drivers / serial / s3c4510b_uart.c
1 /*
2  * Copyright (c) 2004   Cucy Systems (http://www.cucy.com)
3  * Curt Brune <curt@cucy.com>
4  *
5  * (C) Copyright 2004
6  * DAVE Srl
7  * http://www.dave-tech.it
8  * http://www.wawnet.biz
9  * mailto:info@wawnet.biz
10  *
11  * (C) Copyright 2002-2004
12  * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
13  *
14  * (C) Copyright 2002
15  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
16  * Marius Groeger <mgroeger@sysgo.de>
17  *
18  * (C) Copyright 2002
19  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
20  * Alex Zuepke <azu@sysgo.de>
21  *
22  * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program; if not, write to the Free Software
36  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
37  *
38  * MODULE:        $Id:$
39  * Description:   UART/Serial interface for Samsung S3C4510B SoC
40  * Runtime Env:   ARM7TDMI
41  * Change History:
42  *     03-02-04    Create (Curt Brune) curt@cucy.com
43  *
44  */
45
46 #include <common.h>
47
48 #ifdef CONFIG_DRIVER_S3C4510_UART
49
50 #include <asm/hardware.h>
51 #include "s3c4510b_uart.h"
52
53 DECLARE_GLOBAL_DATA_PTR;
54
55 static UART    *uart;
56
57 /* flush serial input queue. returns 0 on success or negative error
58  * number otherwise
59  */
60 static int serial_flush_input(void)
61 {
62         volatile u32 tmp;
63
64         /* keep on reading as long as the receiver is not empty */
65         while( uart->m_stat.bf.rxReady) {
66                 tmp = uart->m_rx;
67         }
68
69         return 0;
70 }
71
72
73 /* flush output queue. returns 0 on success or negative error number
74  * otherwise
75  */
76 static int serial_flush_output(void)
77 {
78         /* wait until the transmitter is no longer busy */
79         while( !uart->m_stat.bf.txBufEmpty);
80
81         return 0;
82 }
83
84
85 void serial_setbrg (void)
86 {
87         UART_LINE_CTRL ulctrl;
88         UART_CTRL      uctrl;
89         UART_BAUD_DIV  ubd;
90
91         serial_flush_output();
92         serial_flush_input();
93
94         /* control register */
95         uctrl.ui = 0x0;
96         uctrl.bf.rxMode = 0x1;
97         uctrl.bf.rxIrq = 0x0;
98         uctrl.bf.txMode = 0x1;
99         uctrl.bf.DSR = 0x0;
100         uctrl.bf.sendBreak = 0x0;
101         uctrl.bf.loopBack = 0x0;
102         uart->m_ctrl.ui = uctrl.ui;
103
104         /* line control register */
105         ulctrl.ui  = 0x0;
106         ulctrl.bf.wordLen   = 0x3; /* 8 bit data */
107         ulctrl.bf.nStop     = 0x0; /* 1 stop bit */
108         ulctrl.bf.parity    = 0x0; /* no parity */
109         ulctrl.bf.clk       = 0x0; /* internal clock */
110         ulctrl.bf.infra_red = 0x0; /* no infra_red */
111         uart->m_lineCtrl.ui = ulctrl.ui;
112
113         ubd.ui = 0x0;
114
115         /* see table on page 10-15 in SAMSUNG S3C4510B manual */
116         /* get correct divisor */
117         switch(gd->baudrate) {
118         case   1200:    ubd.bf.cnt0 = 1301;     break;
119         case   2400:    ubd.bf.cnt0 =  650;     break;
120         case   4800:    ubd.bf.cnt0 =  324;     break;
121         case   9600:    ubd.bf.cnt0 =  162;     break;
122         case  19200:    ubd.bf.cnt0 =   80;     break;
123         case  38400:    ubd.bf.cnt0 =   40;     break;
124         case  57600:    ubd.bf.cnt0 =   26;     break;
125         case 115200:    ubd.bf.cnt0 =   13;     break;
126         }
127
128         uart->m_baudDiv.ui = ubd.ui;
129         uart->m_baudCnt = 0x0;
130         uart->m_baudClk = 0x0;
131
132 }
133
134
135 /*
136  * Initialise the serial port with the given baudrate. The settings
137  * are always 8 data bits, no parity, 1 stop bit, no start bits.
138  *
139  */
140 int serial_init (void)
141 {
142
143 #if   CONFIG_SERIAL1 == 1
144         uart = (UART *)UART0_BASE;
145 #elif CONFIG_SERIAL1 == 2
146         uart = (UART *)UART1_BASE;
147 #else
148 #error CONFIG_SERIAL1 not equal to 1 or 2
149 #endif
150
151         serial_setbrg ();
152
153         return (0);
154 }
155
156
157 /*
158  * Output a single byte to the serial port.
159  */
160 void serial_putc (const char c)
161 {
162         /* wait for room in the transmit FIFO */
163         while( !uart->m_stat.bf.txBufEmpty);
164
165         uart->m_tx = c;
166
167         /*
168                 to be polite with serial console add a line feed
169                 to the carriage return character
170         */
171         if (c=='\n')
172                 serial_putc('\r');
173 }
174
175 /*
176  * Test if an input byte is ready from the serial port. Returns non-zero on
177  * success, 0 otherwise.
178  */
179 int serial_tstc (void)
180 {
181         return uart->m_stat.bf.rxReady;
182 }
183
184 /*
185  * Read a single byte from the serial port. Returns 1 on success, 0
186  * otherwise. When the function is succesfull, the character read is
187  * written into its argument c.
188  */
189 int serial_getc (void)
190 {
191         int rv;
192
193         for(;;) {
194                 rv = serial_tstc();
195
196                 if (rv) {
197                         return uart->m_rx & 0xFF;
198                 }
199         }
200 }
201
202 void serial_puts (const char *s)
203 {
204         while (*s) {
205                 serial_putc (*s++);
206         }
207
208         /* busy wait for tx complete */
209         while ( !uart->m_stat.bf.txComplete);
210
211         /* clear break */
212         uart->m_ctrl.bf.sendBreak = 0;
213
214 }
215
216 #endif