summaryrefslogtreecommitdiff
path: root/cleopatre/u-boot-1.1.6/cpu/spc300/serial.c
blob: b503c4a611920dcdbf4fafd3002022d51889c101 (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
/*
 * cpu/spc300/serial.c
 *
 * Copyright (C) 2009 SPiDCOM Technologies
 *
 * 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 program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <config.h>
#include <asm/io.h>
#include <asm/arch/hardware.h>
#include <asm/arch/nvram.h>
#include <asm/arch/ips/uart.h>

#if !defined(CONFIG_USART0)
#error must define CONFIG_USART0
#endif

int get_master_clock(gd_t* gd)
{
        ulong masterclk;

#ifdef CONFIG_CHIP_FEATURE_FIXED_MASTER_CLOCK
        masterclk = CONFIG_CHIP_MAX_MASTER_CLOCK;
#else
        bd_t *bd = gd->bd;
        spc300_nvram_t *nvram = (spc300_nvram_t*)bd->bi_nvram_addr;

        switch(NVRAM_BFEXT(FREQ, nvram->pkg_cfg))
        {
        case NVRAM_FREQ_100:
            masterclk = 100000000;
            break;
        case NVRAM_FREQ_125:
            masterclk = 125000000;
            break;
        case NVRAM_FREQ_133:
            masterclk = 133000000;
            break;
        case NVRAM_FREQ_150:
        default:
            masterclk = 147000000;
            break;
        }
#endif

        return masterclk;
}

/**
 * Function:     serial_setbrg
 * Parameters:   void
 * Purpose:      Set baudrate
 * Return Value: void
 */
void serial_setbrg (void)
{
	DECLARE_GLOBAL_DATA_PTR;
	int baudrate;
	int baud_divisor;

	if ((baudrate = gd->baudrate) <= 0)
		baudrate = CONFIG_BAUDRATE;

	baud_divisor = (get_master_clock((gd_t*)gd) + 8*baudrate) / (16 * baudrate);
	writel(readl(UART_LCR_1) | 0x80, UART_LCR_1);
	writel(baud_divisor & 0x00ff, UART_DLL_1);
	writel((baud_divisor & 0xff00) >> 8, UART_DLH_1);
	writel(readl(UART_LCR_1) & ~0x80, UART_LCR_1);
} /* serial_setbrg */


/**
 * Function:     serial_init
 * Parameters:   void
 * Purpose:      Initilize serial
 * Return Value: int
 */
int serial_init (void)
{
	/* disable interrupts */
	writel(0, UART_IER_1);
	/* set mode 8 bits, 1 stop, no parity */
	writel(UART_DLS_8, UART_LCR_1);
	/* set FIFO RX & TX to half trigger */
	writel(UART_RT_HALF | UART_TET_QUARTER | UART_XFIFOR | UART_RFIFOR | UART_FIFOE, UART_FCR_1);

	serial_setbrg ();
	return (0);
} /* serial_init */

/**
 * Function:     serial_putc
 * Parameters:   const char c
 * Purpose:      Send character on serial
 * Return Value: 
 */
void serial_putc (const char c)
{

	if (c == '\n')
		serial_putc ('\r');
	while(!(readl(UART_LSR_1) & UART_TEMT));
	writel(c, UART_THR_1);
} /* serial_putc */

/**
 * Function:     serial_puts
 * Parameters:   const char *s
 * Purpose:      Send string on serial
 * Return Value: void
 */
void serial_puts (const char *s)
{
	while (*s) {
		serial_putc (*s++);
	}
} /* serial_puts */

/**
 * Function:     serial_getc
 * Parameters:   void
 * Purpose:      Receive character
 * Return Value: int
 */
int serial_getc (void)
{
	int val;

	while(!(readl(UART_LSR_1) & UART_DR));
	val = readl(UART_RBR_1);

	return val;
} /* serial_getc */

/**
 * Function:     serial_tstc
 * Parameters:   void
 * Purpose:      Test if a character is received
 * Return Value: int
 */
int serial_tstc (void)
{
	if(readl(UART_LSR_1) & UART_DR)
		return 1;
	else
		return 0;
} /* serial_tstc */