TEXT_BASE is in board/sandpoint/config.mk so say so...
[u-boot.git] / cpu / lh7a40x / serial.c
1 /*
2  * (C) Copyright 2002
3  * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include <common.h>
22 #include <lh7a40x.h>
23
24 #if defined(CONFIG_CONSOLE_UART1)
25 # define UART_CONSOLE 1
26 #elif defined(CONFIG_CONSOLE_UART2)
27 # define UART_CONSOLE 2
28 #elif defined(CONFIG_CONSOLE_UART3)
29 # define UART_CONSOLE 3
30 #else
31 # error "No console configured ... "
32 #endif
33
34 void serial_setbrg (void)
35 {
36         DECLARE_GLOBAL_DATA_PTR;
37         lh7a40x_uart_t* uart = LH7A40X_UART_PTR(UART_CONSOLE);
38         int i;
39         unsigned int reg = 0;
40
41         /*
42          * userguide 15.1.2.4
43          *
44          * BAUDDIV is (UART_REF_FREQ/(16 X BAUD))-1
45          *
46          *   UART_REF_FREQ = external system clock input / 2 (Hz)
47          *   BAUD is desired baudrate (bits/s)
48          *
49          *   NOTE: we add (divisor/2) to numerator to round for
50          *         more precision
51          */
52         reg = (((get_PLLCLK()/2) + ((16*gd->baudrate)/2)) / (16 * gd->baudrate)) - 1;
53         uart->brcon = reg;
54
55         for (i = 0; i < 100; i++);
56 }
57
58 /*
59  * Initialise the serial port with the given baudrate. The settings
60  * are always 8 data bits, no parity, 1 stop bit, no start bits.
61  *
62  */
63 int serial_init (void)
64 {
65         lh7a40x_uart_t* uart = LH7A40X_UART_PTR(UART_CONSOLE);
66
67         /* UART must be enabled before writing to any config registers */
68         uart->con |= (UART_EN);
69
70 #ifdef CONFIG_CONSOLE_UART1
71         /* infrared disabled */
72         uart->con |= UART_SIRD;
73 #endif
74         /* loopback disabled */
75         uart->con &= ~(UART_LBE);
76
77         /* modem lines and tx/rx polarities */
78         uart->con &= ~(UART_MXP | UART_TXP | UART_RXP);
79
80         /* FIFO enable, N81 */
81         uart->fcon = (UART_WLEN_8 | UART_FEN | UART_STP2_1);
82
83         /* set baudrate */
84         serial_setbrg ();
85
86         /* enable rx interrupt */
87         uart->inten |= UART_RI;
88
89         return (0);
90 }
91
92 /*
93  * Read a single byte from the serial port. Returns 1 on success, 0
94  * otherwise. When the function is succesfull, the character read is
95  * written into its argument c.
96  */
97 int serial_getc (void)
98 {
99         lh7a40x_uart_t* uart = LH7A40X_UART_PTR(UART_CONSOLE);
100
101         /* wait for character to arrive */
102         while (uart->status & UART_RXFE);
103
104         return(uart->data & 0xff);
105 }
106
107 #ifdef CONFIG_HWFLOW
108 static int hwflow = 0; /* turned off by default */
109 int hwflow_onoff(int on)
110 {
111         switch(on) {
112         case 0:
113         default:
114                 break; /* return current */
115         case 1:
116                 hwflow = 1; /* turn on */
117                 break;
118         case -1:
119                 hwflow = 0; /* turn off */
120                 break;
121         }
122         return hwflow;
123 }
124 #endif
125
126 #ifdef CONFIG_MODEM_SUPPORT
127 static int be_quiet = 0;
128 void disable_putc(void)
129 {
130         be_quiet = 1;
131 }
132
133 void enable_putc(void)
134 {
135         be_quiet = 0;
136 }
137 #endif
138
139
140 /*
141  * Output a single byte to the serial port.
142  */
143 void serial_putc (const char c)
144 {
145         lh7a40x_uart_t* uart = LH7A40X_UART_PTR(UART_CONSOLE);
146
147 #ifdef CONFIG_MODEM_SUPPORT
148         if (be_quiet)
149                 return;
150 #endif
151
152         /* wait for room in the tx FIFO */
153         while (!(uart->status & UART_TXFE));
154
155 #ifdef CONFIG_HWFLOW
156         /* Wait for CTS up */
157         while(hwflow && !(uart->status & UART_CTS));
158 #endif
159
160         uart->data = c;
161
162         /* If \n, also do \r */
163         if (c == '\n')
164                 serial_putc ('\r');
165 }
166
167 /*
168  * Test whether a character is in the RX buffer
169  */
170 int serial_tstc (void)
171 {
172         lh7a40x_uart_t* uart = LH7A40X_UART_PTR(UART_CONSOLE);
173
174         return(!(uart->status & UART_RXFE));
175 }
176
177 void
178 serial_puts (const char *s)
179 {
180         while (*s) {
181                 serial_putc (*s++);
182         }
183 }