Split up brcm63xx into files/
[openwrt/openwrt.git] / target / linux / brcm63xx-2.6 / files / arch / mips / bcm963xx / ser_init.c
1 /*
2 <:copyright-gpl
3 Copyright 2004 Broadcom Corp. All Rights Reserved.
4
5 This program is free software; you can distribute it and/or modify it
6 under the terms of the GNU General Public License (Version 2) as
7 published by the Free Software Foundation.
8
9 This program is distributed in the hope it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
17 :>
18 */
19 /*
20 * Broadcom bcm63xx serial port initialization, also prepare for printk
21 * by registering with console_init
22 *
23 */
24
25 #include <linux/autoconf.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
28 #include <linux/kernel.h>
29 #include <linux/types.h>
30 #include <linux/console.h>
31 #include <linux/sched.h>
32
33 #include <asm/addrspace.h>
34 #include <asm/irq.h>
35 #include <asm/reboot.h>
36 #include <asm/gdb-stub.h>
37 #include <asm/mc146818rtc.h>
38
39 #include <bcm_map_part.h>
40 #include <board.h>
41
42 #define SER63XX_DEFAULT_BAUD 115200
43 #define BD_BCM63XX_TIMER_CLOCK_INPUT (FPERIPH)
44 #define stUart ((volatile Uart * const) UART_BASE)
45
46 // Transmit interrupts
47 #define TXINT (TXFIFOEMT | TXUNDERR | TXOVFERR)
48 // Receive interrupts
49 #define RXINT (RXFIFONE | RXOVFERR)
50
51 /* --------------------------------------------------------------------------
52 Name: serial_init
53 Purpose: Initalize the UART
54 -------------------------------------------------------------------------- */
55 void __init serial_init(void)
56 {
57 UINT32 tmpVal = SER63XX_DEFAULT_BAUD;
58 ULONG clockFreqHz;
59
60 #if defined(CONFIG_BCM96345)
61 // Make sure clock is ticking
62 PERF->blkEnables |= UART_CLK_EN;
63 #endif
64
65 /* Dissable channel's receiver and transmitter. */
66 stUart->control &= ~(BRGEN|TXEN|RXEN);
67
68 /*--------------------------------------------------------------------*/
69 /* Write the table value to the clock select register. */
70 /* DPullen - this is the equation to use: */
71 /* value = clockFreqHz / baud / 32-1; */
72 /* (snmod) Actually you should also take into account any necessary */
73 /* rounding. Divide by 16, look at lsb, if 0, divide by 2 */
74 /* and subtract 1. If 1, just divide by 2 */
75 /*--------------------------------------------------------------------*/
76 clockFreqHz = BD_BCM63XX_TIMER_CLOCK_INPUT;
77 tmpVal = (clockFreqHz / tmpVal) / 16;
78 if( tmpVal & 0x01 )
79 tmpVal /= 2; //Rounding up, so sub is already accounted for
80 else
81 tmpVal = (tmpVal / 2) - 1; // Rounding down so we must sub 1
82 stUart->baudword = tmpVal;
83
84 /* Finally, re-enable the transmitter and receiver. */
85 stUart->control |= (BRGEN|TXEN|RXEN);
86
87 stUart->config = (BITS8SYM | ONESTOP);
88 // Set the FIFO interrupt depth ... stUart->fifocfg = 0xAA;
89 stUart->fifoctl = RSTTXFIFOS | RSTRXFIFOS;
90 stUart->intMask = 0;
91 stUart->intMask = RXINT | TXINT;
92 }
93
94
95 /* prom_putc()
96 * Output a character to the UART
97 */
98 void prom_putc(char c)
99 {
100 /* Wait for Tx uffer to empty */
101 while (! (READ16(stUart->intStatus) & TXFIFOEMT));
102 /* Send character */
103 stUart->Data = c;
104 }
105
106 /* prom_puts()
107 * Write a string to the UART
108 */
109 void prom_puts(const char *s)
110 {
111 while (*s) {
112 if (*s == '\n') {
113 prom_putc('\r');
114 }
115 prom_putc(*s++);
116 }
117 }
118
119
120 /* prom_getc_nowait()
121 * Returns a character from the UART
122 * Returns -1 if no characters available or corrupted
123 */
124 int prom_getc_nowait(void)
125 {
126 uint16 uStatus;
127 int cData = -1;
128
129 uStatus = READ16(stUart->intStatus);
130
131 if (uStatus & RXFIFONE) { /* Do we have a character? */
132 cData = READ16(stUart->Data) & 0xff; /* Read character */
133 if (uStatus & (RXFRAMERR | RXPARERR)) { /* If we got an error, throw it away */
134 cData = -1;
135 }
136 }
137
138 return cData;
139 }
140
141 /* prom_getc()
142 * Returns a charcter from the serial port
143 * Will block until it receives a valid character
144 */
145 char prom_getc(void)
146 {
147 int cData = -1;
148
149 /* Loop until we get a valid character */
150 while(cData == -1) {
151 cData = prom_getc_nowait();
152 }
153 return (char) cData;
154 }
155
156 /* prom_testc()
157 * Returns 0 if no characters available
158 */
159 int prom_testc(void)
160 {
161 uint16 uStatus;
162
163 uStatus = READ16(stUart->intStatus);
164
165 return (uStatus & RXFIFONE);
166 }
167
168 #if defined (CONFIG_REMOTE_DEBUG)
169 /* Prevent other code from writing to the serial port */
170 void _putc(char c) { }
171 void _puts(const char *ptr) { }
172 #else
173 /* Low level outputs call prom routines */
174 void _putc(char c) {
175 prom_putc(c);
176 }
177 void _puts(const char *ptr) {
178 prom_puts(ptr);
179 }
180 #endif