829eb052707a99d1550c17ad125c45795b6391fd
[kugel-rb.git] / firmware / target / arm / s3c2440 / uart-s3c2440.c
blob829eb052707a99d1550c17ad125c45795b6391fd
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"
33 #include "system-target.h"
34 #include "uart-s3c2440.h"
36 #define MAX_PRINTF_BUF 1024
38 /****************************************************************************
39 * serial driver API
40 ****************************************************************************/
41 void serial_setup (void)
43 uart_init();
44 uart_init_device(DEBUG_UART_PORT);
47 int tx_rdy(void)
49 if (uart_tx_ready (DEBUG_UART_PORT))
50 return 1;
51 else
52 return 0;
55 void tx_writec(unsigned char c)
57 uart_send_byte (DEBUG_UART_PORT, c);
61 static int uart_push(void *user_data, unsigned char ch)
63 (void)user_data;
65 uart_send_byte(DEBUG_UART_PORT, ch);
66 if (ch == '\n')
67 uart_send_byte(DEBUG_UART_PORT, '\r');
68 return 1;
71 /****************************************************************************
72 * General purpose debug function
73 ****************************************************************************/
74 void uart_printf (const char *format, ...)
76 static bool debug_uart_init = false;
77 va_list ap;
79 if (!debug_uart_init)
81 uart_init_device(DEBUG_UART_PORT);
82 debug_uart_init = true;
85 va_start(ap, format);
86 vuprintf(uart_push, NULL, format, ap);
87 va_end(ap);
90 /****************************************************************************
91 * Device level functions specific to S3C2440
92 *****************************************************************************/
94 bool uart_init (void)
96 /* anything ? */
97 return true;
100 bool uart_init_device (unsigned dev)
102 /* set GPIOs, clock enable? etc */
104 switch (dev)
106 case 0:
108 S3C2440_GPIO_CONFIG (GPHCON, 2, GPIO_FUNCTION);
109 S3C2440_GPIO_CONFIG (GPHCON, 3, GPIO_FUNCTION);
110 S3C2440_GPIO_PULLUP (GPHUP, 2, GPIO_PULLUP_DISABLE);
111 S3C2440_GPIO_PULLUP (GPHUP, 3, GPIO_PULLUP_DISABLE);
112 break;
114 case 1:
116 S3C2440_GPIO_CONFIG (GPHCON, 4, GPIO_FUNCTION);
117 S3C2440_GPIO_CONFIG (GPHCON, 5, GPIO_FUNCTION);
118 S3C2440_GPIO_PULLUP (GPHUP, 4, GPIO_PULLUP_DISABLE);
119 S3C2440_GPIO_PULLUP (GPHUP, 5, GPIO_PULLUP_DISABLE);
120 break;
122 case 2:
124 S3C2440_GPIO_CONFIG (GPHCON, 6, GPIO_FUNCTION);
125 S3C2440_GPIO_CONFIG (GPHCON, 7, GPIO_FUNCTION);
126 S3C2440_GPIO_PULLUP (GPHUP, 6, GPIO_PULLUP_DISABLE);
127 S3C2440_GPIO_PULLUP (GPHUP, 7, GPIO_PULLUP_DISABLE);
128 break;
130 default:
131 return false;
134 /* set a default configuration */
135 uart_config (dev, 115200, 8, UART_NO_PARITY, UART_1_STOP_BIT);
136 return true;
139 bool uart_config (unsigned dev, unsigned speed, unsigned num_bits,
140 unsigned parity, unsigned stop_bits)
142 switch (dev)
144 case 0:
145 ULCON0 = (parity << 3) + (stop_bits << 2) + (num_bits-5);
146 UCON0 = (1 << 2) + (1 << 0); /* enable TX, RX, use PCLK */
147 UBRDIV0 = PCLK / (speed*16);
148 break;
150 case 1:
151 ULCON1 = (parity << 3) + (stop_bits << 2) + (num_bits-5);
152 UCON1 = (1 << 2) + (1 << 0); /* enable TX, RX, use PCLK */
153 UBRDIV1 = PCLK / (speed*16);
154 break;
156 case 2:
157 ULCON2 = (parity << 3) + (stop_bits << 2) + (num_bits-5);
158 UCON2 = (1 << 2) + (1 << 0); /* enable TX, RX, use PCLK */
159 UBRDIV2 = PCLK / (speed*16);
160 break;
163 return true;
166 /* transmit */
167 bool uart_tx_ready (unsigned dev)
169 /* test if transmit buffer empty */
170 switch (dev)
172 case 0:
173 if (UTRSTAT0 & 0x02)
174 return true;
175 else
176 return false;
177 break;
178 case 1:
179 if (UTRSTAT1 & 0x02)
180 return true;
181 else
182 return false;
183 break;
184 case 2:
185 if (UTRSTAT2 & 0x02)
186 return true;
187 else
188 return false;
189 break;
191 return false;
194 bool uart_send_byte (unsigned dev, char ch)
196 /* wait for transmit buffer empty */
197 while (!uart_tx_ready(dev))
200 switch (dev)
202 case 0:
203 UTXH0 = ch;
204 break;
205 case 1:
206 UTXH1 = ch;
207 break;
208 case 2:
209 UTXH2 = ch;
210 break;
213 return true;
216 /* Receive */
218 bool uart_rx_ready (unsigned dev)
220 /* test receive buffer data ready */
221 switch (dev)
223 case 0:
224 if (UTRSTAT0 & 0x01)
225 return true;
226 else
227 return false;
228 break;
229 case 1:
230 if (UTRSTAT1 & 0x01)
231 return true;
232 else
233 return false;
234 break;
235 case 2:
236 if (UTRSTAT2 & 0x01)
237 return true;
238 else
239 return false;
240 break;
242 return false;
245 char uart_read_byte (unsigned dev)
247 while (!uart_rx_ready(dev))
249 switch (dev)
251 case 0:
252 return URXH0;
253 break;
254 case 1:
255 return URXH1;
256 break;
257 case 2:
258 return URXH2;
259 break;
262 return '\0';
265 /****************************************************************************
266 * General
267 *****************************************************************************/
269 bool uart_send_buf (unsigned dev, char *buf, unsigned len)
271 unsigned index=0;
272 while (index<len)
274 uart_send_byte (dev, buf[index]);
275 index++;
277 return true;
281 /****************************************************************************/