summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/s3c2440/uart-s3c2440.c
blob: 2a61b61a3945531135d19db84062cad2f92478bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2009 by Bob Cousins
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

/* Include Standard files */
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include "inttypes.h"
#include "string.h"
#include "cpu.h"
#include "system.h"
#include "kernel.h"
#include "thread.h"

#include "uart-s3c2440.h"

#define FCLK 405000000
#define HCLK (FCLK/4)   /* = 101,250,000 */
#define PCLK (HCLK/2)   /* =  50,625,000 */

#define MAX_TX_BUF        1024


/****************************************************************************
 * General purpose debug function
 ****************************************************************************/

void uart_printf (const char *format, ...)
{
    static bool debug_uart_init = false;
    static char tx_buf [MAX_TX_BUF];

    int len;
    unsigned char *ptr;
    va_list ap;
    va_start(ap, format);

    ptr = tx_buf;
    len = vsnprintf(ptr, sizeof(tx_buf), format, ap);
    va_end(ap);

    if (!debug_uart_init)
    {
      uart_init_device(UART_DEBUG);
      debug_uart_init = true;
    }
    
    uart_send (UART_DEBUG, tx_buf, len);
}

/****************************************************************************
 * Device level functions specific to S3C2440
 *****************************************************************************/

bool uart_init (void)
{
    /* anything ? */
    return true;
}

bool uart_init_device (unsigned dev)
{
    /* set GPIOs, clock enable? etc */

    switch (dev)
    {
        case 0:
        {
            S3C2440_GPIO_CONFIG (GPHCON, 2, GPIO_FUNCTION);
            S3C2440_GPIO_CONFIG (GPHCON, 3, GPIO_FUNCTION);
            S3C2440_GPIO_PULLUP (GPHUP, 2, GPIO_PULLUP_DISABLE);
            S3C2440_GPIO_PULLUP (GPHUP, 3, GPIO_PULLUP_DISABLE);
            break;
        }
        case 1:
        {
            S3C2440_GPIO_CONFIG (GPHCON, 4, GPIO_FUNCTION);
            S3C2440_GPIO_CONFIG (GPHCON, 5, GPIO_FUNCTION);
            S3C2440_GPIO_PULLUP (GPHUP, 4, GPIO_PULLUP_DISABLE);
            S3C2440_GPIO_PULLUP (GPHUP, 5, GPIO_PULLUP_DISABLE);
            break;
        }
        case 2:
        {
            S3C2440_GPIO_CONFIG (GPHCON, 6, GPIO_FUNCTION);
            S3C2440_GPIO_CONFIG (GPHCON, 7, GPIO_FUNCTION);
            S3C2440_GPIO_PULLUP (GPHUP, 6, GPIO_PULLUP_DISABLE);
            S3C2440_GPIO_PULLUP (GPHUP, 7, GPIO_PULLUP_DISABLE);
            break;
        }
        default:
            return false;
    }
    
    /* set a default configuration */
    uart_config (dev, 115200, 8, UART_NO_PARITY, UART_1_STOP_BIT); 
    return true;
}

bool uart_config (unsigned dev, unsigned speed, unsigned num_bits, 
                  unsigned parity, unsigned stop_bits)
{
    switch (dev)
    {
    case 0:
        ULCON0  = (parity << 3) + (stop_bits << 2) + (num_bits-5);
        UCON0   = (1 << 2) + (1 << 0);   /* enable TX, RX, use PCLK */
        UBRDIV0 = PCLK / (speed*16);
        break;

    case 1:
        ULCON1  = (parity << 3) + (stop_bits << 2) + (num_bits-5);
        UCON1   = (1 << 2) + (1 << 0);   /* enable TX, RX, use PCLK */
        UBRDIV1 = PCLK / (speed*16);
        break;

    case 2:
        ULCON2  = (parity << 3) + (stop_bits << 2) + (num_bits-5);
        UCON2   = (1 << 2) + (1 << 0);   /* enable TX, RX, use PCLK */
        UBRDIV2 = PCLK / (speed*16);
        break;
    }
    
    return true;
}

bool uart_send_byte (unsigned dev, char ch)
{
    switch (dev)
    {
    case 0:
        /* wait for transmit buffer empty */
        while ((UTRSTAT0 & 0x02) == 0)
          ;
        UTXH0 = ch;
        break;

    case 1:
        /* wait for transmit buffer empty */
        while ((UTRSTAT1 & 0x02) == 0)
          ;
        UTXH1 = ch;
        break;

    case 2:
        /* wait for transmit buffer empty */
        while ((UTRSTAT2 & 0x02) == 0)
          ;
        UTXH2 = ch;
        break;
    }
    
    return true;
}

char uart_rx_ready (unsigned dev)
{
    switch (dev)
    {
    case 0:
        /* wait for receive buffer data ready */
        if (UTRSTAT0 & 0x01)
            return true;
        else
            return false;
        break;
    case 1:
        /* wait for receive buffer data ready */
        if (UTRSTAT1 & 0x01)
            return true;
        else
            return false;
        break;
    case 2:
        /* wait for receive buffer data ready */
        if (UTRSTAT2 & 0x01)
            return true;
        else
            return false;
        break;
    }
    return false;
}

char uart_read_byte (unsigned dev)
{
    switch (dev)
    {
    case 0:
        while (!uart_rx_ready(dev))
            ;
        return URXH0;
        break;
    case 1:
        while (!uart_rx_ready(dev))
            ;
        return URXH1;
        break;
    case 2:
        while (!uart_rx_ready(dev))
            ;
        return URXH2;
        break;
    }
    
    return true;
}

/****************************************************************************
 * General
 *****************************************************************************/

bool uart_send (unsigned dev, char *buf, unsigned len)
{
    unsigned index=0;
    while (index<len)
    {
        uart_send_byte (dev, buf[index]);
        
        /* hack for ASCII terminals */
        if (buf[index] == '\n')
          uart_send_byte (dev, '\r');
          
        index++;
    }
    return true;
}


/****************************************************************************/