vuprintf does not belong in stdio.h, causes problems with other versions of stdio.h
[kugel-rb.git] / firmware / target / arm / s3c2440 / uart-s3c2440.c
blob8fe6511253855d3383d3b4f90b1d8646209b5691
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 by Bob Cousins
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 /* Include Standard files */
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include "inttypes.h"
27 #include "string.h"
28 #include "cpu.h"
29 #include "system.h"
30 #include "kernel.h"
31 #include "thread.h"
32 #include "format.h"
34 #include "system-target.h"
35 #include "uart-s3c2440.h"
37 #define MAX_PRINTF_BUF 1024
39 /****************************************************************************
40 * serial driver API
41 ****************************************************************************/
42 void serial_setup (void)
44 uart_init();
45 uart_init_device(DEBUG_UART_PORT);
48 int tx_rdy(void)
50 if (uart_tx_ready (DEBUG_UART_PORT))
51 return 1;
52 else
53 return 0;
56 void tx_writec(unsigned char c)
58 uart_send_byte (DEBUG_UART_PORT, c);
62 static int uart_push(void *user_data, unsigned char ch)
64 (void)user_data;
66 uart_send_byte(DEBUG_UART_PORT, ch);
67 if (ch == '\n')
68 uart_send_byte(DEBUG_UART_PORT, '\r');
69 return 1;
72 /****************************************************************************
73 * General purpose debug function
74 ****************************************************************************/
75 void uart_printf (const char *format, ...)
77 static bool debug_uart_init = false;
78 va_list ap;
80 if (!debug_uart_init)
82 uart_init_device(DEBUG_UART_PORT);
83 debug_uart_init = true;
86 va_start(ap, format);
87 vuprintf(uart_push, NULL, format, ap);
88 va_end(ap);
91 /****************************************************************************
92 * Device level functions specific to S3C2440
93 *****************************************************************************/
95 bool uart_init (void)
97 /* anything ? */
98 return true;
101 bool uart_init_device (unsigned dev)
103 /* set GPIOs, clock enable? etc */
105 switch (dev)
107 case 0:
109 S3C2440_GPIO_CONFIG (GPHCON, 2, GPIO_FUNCTION);
110 S3C2440_GPIO_CONFIG (GPHCON, 3, GPIO_FUNCTION);
111 S3C2440_GPIO_PULLUP (GPHUP, 2, GPIO_PULLUP_DISABLE);
112 S3C2440_GPIO_PULLUP (GPHUP, 3, GPIO_PULLUP_DISABLE);
113 break;
115 case 1:
117 S3C2440_GPIO_CONFIG (GPHCON, 4, GPIO_FUNCTION);
118 S3C2440_GPIO_CONFIG (GPHCON, 5, GPIO_FUNCTION);
119 S3C2440_GPIO_PULLUP (GPHUP, 4, GPIO_PULLUP_DISABLE);
120 S3C2440_GPIO_PULLUP (GPHUP, 5, GPIO_PULLUP_DISABLE);
121 break;
123 case 2:
125 S3C2440_GPIO_CONFIG (GPHCON, 6, GPIO_FUNCTION);
126 S3C2440_GPIO_CONFIG (GPHCON, 7, GPIO_FUNCTION);
127 S3C2440_GPIO_PULLUP (GPHUP, 6, GPIO_PULLUP_DISABLE);
128 S3C2440_GPIO_PULLUP (GPHUP, 7, GPIO_PULLUP_DISABLE);
129 break;
131 default:
132 return false;
135 /* set a default configuration */
136 uart_config (dev, 115200, 8, UART_NO_PARITY, UART_1_STOP_BIT);
137 return true;
140 bool uart_config (unsigned dev, unsigned speed, unsigned num_bits,
141 unsigned parity, unsigned stop_bits)
143 switch (dev)
145 case 0:
146 ULCON0 = (parity << 3) + (stop_bits << 2) + (num_bits-5);
147 UCON0 = (1 << 2) + (1 << 0); /* enable TX, RX, use PCLK */
148 UBRDIV0 = PCLK / (speed*16);
149 break;
151 case 1:
152 ULCON1 = (parity << 3) + (stop_bits << 2) + (num_bits-5);
153 UCON1 = (1 << 2) + (1 << 0); /* enable TX, RX, use PCLK */
154 UBRDIV1 = PCLK / (speed*16);
155 break;
157 case 2:
158 ULCON2 = (parity << 3) + (stop_bits << 2) + (num_bits-5);
159 UCON2 = (1 << 2) + (1 << 0); /* enable TX, RX, use PCLK */
160 UBRDIV2 = PCLK / (speed*16);
161 break;
164 return true;
167 /* transmit */
168 bool uart_tx_ready (unsigned dev)
170 /* test if transmit buffer empty */
171 switch (dev)
173 case 0:
174 if (UTRSTAT0 & 0x02)
175 return true;
176 else
177 return false;
178 break;
179 case 1:
180 if (UTRSTAT1 & 0x02)
181 return true;
182 else
183 return false;
184 break;
185 case 2:
186 if (UTRSTAT2 & 0x02)
187 return true;
188 else
189 return false;
190 break;
192 return false;
195 bool uart_send_byte (unsigned dev, char ch)
197 /* wait for transmit buffer empty */
198 while (!uart_tx_ready(dev))
201 switch (dev)
203 case 0:
204 UTXH0 = ch;
205 break;
206 case 1:
207 UTXH1 = ch;
208 break;
209 case 2:
210 UTXH2 = ch;
211 break;
214 return true;
217 /* Receive */
219 bool uart_rx_ready (unsigned dev)
221 /* test receive buffer data ready */
222 switch (dev)
224 case 0:
225 if (UTRSTAT0 & 0x01)
226 return true;
227 else
228 return false;
229 break;
230 case 1:
231 if (UTRSTAT1 & 0x01)
232 return true;
233 else
234 return false;
235 break;
236 case 2:
237 if (UTRSTAT2 & 0x01)
238 return true;
239 else
240 return false;
241 break;
243 return false;
246 char uart_read_byte (unsigned dev)
248 while (!uart_rx_ready(dev))
250 switch (dev)
252 case 0:
253 return URXH0;
254 break;
255 case 1:
256 return URXH1;
257 break;
258 case 2:
259 return URXH2;
260 break;
263 return '\0';
266 /****************************************************************************
267 * General
268 *****************************************************************************/
270 bool uart_send_buf (unsigned dev, char *buf, unsigned len)
272 unsigned index=0;
273 while (index<len)
275 uart_send_byte (dev, buf[index]);
276 index++;
278 return true;
282 /****************************************************************************/