Many more drivers for mini2440. Now the main binary compiles and runs.
[kugel-rb.git] / firmware / target / arm / s3c2440 / uart-s3c2440.c
blob84282f731a3955f16e0f9d24c3973e9eb4e5a16a
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 /****************************************************************************
62 * General purpose debug function
63 ****************************************************************************/
65 void uart_printf (const char *format, ...)
67 static bool debug_uart_init = false;
68 static char tx_buf [MAX_PRINTF_BUF];
70 int len;
71 unsigned char *ptr;
72 int j;
74 va_list ap;
75 va_start(ap, format);
77 ptr = tx_buf;
78 len = vsnprintf(ptr, sizeof(tx_buf), format, ap);
79 va_end(ap);
81 if (!debug_uart_init)
83 uart_init_device(DEBUG_UART_PORT);
84 debug_uart_init = true;
87 for (j=0; j<len; j++)
89 uart_send_byte (DEBUG_UART_PORT, tx_buf[j]);
90 if ( tx_buf[j] == '\n')
91 uart_send_byte (DEBUG_UART_PORT, '\r');
95 /****************************************************************************
96 * Device level functions specific to S3C2440
97 *****************************************************************************/
99 bool uart_init (void)
101 /* anything ? */
102 return true;
105 bool uart_init_device (unsigned dev)
107 /* set GPIOs, clock enable? etc */
109 switch (dev)
111 case 0:
113 S3C2440_GPIO_CONFIG (GPHCON, 2, GPIO_FUNCTION);
114 S3C2440_GPIO_CONFIG (GPHCON, 3, GPIO_FUNCTION);
115 S3C2440_GPIO_PULLUP (GPHUP, 2, GPIO_PULLUP_DISABLE);
116 S3C2440_GPIO_PULLUP (GPHUP, 3, GPIO_PULLUP_DISABLE);
117 break;
119 case 1:
121 S3C2440_GPIO_CONFIG (GPHCON, 4, GPIO_FUNCTION);
122 S3C2440_GPIO_CONFIG (GPHCON, 5, GPIO_FUNCTION);
123 S3C2440_GPIO_PULLUP (GPHUP, 4, GPIO_PULLUP_DISABLE);
124 S3C2440_GPIO_PULLUP (GPHUP, 5, GPIO_PULLUP_DISABLE);
125 break;
127 case 2:
129 S3C2440_GPIO_CONFIG (GPHCON, 6, GPIO_FUNCTION);
130 S3C2440_GPIO_CONFIG (GPHCON, 7, GPIO_FUNCTION);
131 S3C2440_GPIO_PULLUP (GPHUP, 6, GPIO_PULLUP_DISABLE);
132 S3C2440_GPIO_PULLUP (GPHUP, 7, GPIO_PULLUP_DISABLE);
133 break;
135 default:
136 return false;
139 /* set a default configuration */
140 uart_config (dev, 115200, 8, UART_NO_PARITY, UART_1_STOP_BIT);
141 return true;
144 bool uart_config (unsigned dev, unsigned speed, unsigned num_bits,
145 unsigned parity, unsigned stop_bits)
147 switch (dev)
149 case 0:
150 ULCON0 = (parity << 3) + (stop_bits << 2) + (num_bits-5);
151 UCON0 = (1 << 2) + (1 << 0); /* enable TX, RX, use PCLK */
152 UBRDIV0 = PCLK / (speed*16);
153 break;
155 case 1:
156 ULCON1 = (parity << 3) + (stop_bits << 2) + (num_bits-5);
157 UCON1 = (1 << 2) + (1 << 0); /* enable TX, RX, use PCLK */
158 UBRDIV1 = PCLK / (speed*16);
159 break;
161 case 2:
162 ULCON2 = (parity << 3) + (stop_bits << 2) + (num_bits-5);
163 UCON2 = (1 << 2) + (1 << 0); /* enable TX, RX, use PCLK */
164 UBRDIV2 = PCLK / (speed*16);
165 break;
168 return true;
171 /* transmit */
172 bool uart_tx_ready (unsigned dev)
174 /* test if transmit buffer empty */
175 switch (dev)
177 case 0:
178 if (UTRSTAT0 & 0x02)
179 return true;
180 else
181 return false;
182 break;
183 case 1:
184 if (UTRSTAT1 & 0x02)
185 return true;
186 else
187 return false;
188 break;
189 case 2:
190 if (UTRSTAT2 & 0x02)
191 return true;
192 else
193 return false;
194 break;
196 return false;
199 bool uart_send_byte (unsigned dev, char ch)
201 /* wait for transmit buffer empty */
202 while (!uart_tx_ready(dev))
205 switch (dev)
207 case 0:
208 UTXH0 = ch;
209 break;
210 case 1:
211 UTXH1 = ch;
212 break;
213 case 2:
214 UTXH2 = ch;
215 break;
218 return true;
221 /* Receive */
223 bool uart_rx_ready (unsigned dev)
225 /* test receive buffer data ready */
226 switch (dev)
228 case 0:
229 if (UTRSTAT0 & 0x01)
230 return true;
231 else
232 return false;
233 break;
234 case 1:
235 if (UTRSTAT1 & 0x01)
236 return true;
237 else
238 return false;
239 break;
240 case 2:
241 if (UTRSTAT2 & 0x01)
242 return true;
243 else
244 return false;
245 break;
247 return false;
250 char uart_read_byte (unsigned dev)
252 while (!uart_rx_ready(dev))
254 switch (dev)
256 case 0:
257 return URXH0;
258 break;
259 case 1:
260 return URXH1;
261 break;
262 case 2:
263 return URXH2;
264 break;
267 return '\0';
270 /****************************************************************************
271 * General
272 *****************************************************************************/
274 bool uart_send_buf (unsigned dev, char *buf, unsigned len)
276 unsigned index=0;
277 while (index<len)
279 uart_send_byte (dev, buf[index]);
280 index++;
282 return true;
286 /****************************************************************************/