www.usr.com/support/gpl/USR9113_release1.0.tar.gz
[bcm963xx.git] / kernel / linux / arch / mips / brcm-boards / 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/config.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     /* Dissable channel's receiver and transmitter.                */
61     stUart->control &= ~(BRGEN|TXEN|RXEN);
62                 
63     /*--------------------------------------------------------------------*/
64     /* Write the table value to the clock select register.                */
65     /* DPullen - this is the equation to use:                             */
66     /*       value = clockFreqHz / baud / 32-1;                           */
67     /*   (snmod) Actually you should also take into account any necessary */
68     /*           rounding.  Divide by 16, look at lsb, if 0, divide by 2  */
69     /*           and subtract 1.  If 1, just divide by 2                  */
70     /*--------------------------------------------------------------------*/
71     clockFreqHz = BD_BCM63XX_TIMER_CLOCK_INPUT;
72     tmpVal = (clockFreqHz / tmpVal) / 16;
73     if( tmpVal & 0x01 )
74         tmpVal /= 2;  //Rounding up, so sub is already accounted for
75     else
76         tmpVal = (tmpVal / 2) - 1; // Rounding down so we must sub 1
77     stUart->baudword = tmpVal;
78         
79     /* Finally, re-enable the transmitter and receiver.            */
80     stUart->control |= (BRGEN|TXEN|RXEN);
81
82     stUart->config   = (BITS8SYM | ONESTOP);
83     // Set the FIFO interrupt depth ... stUart->fifocfg  = 0xAA;
84     stUart->fifoctl  =  RSTTXFIFOS | RSTRXFIFOS;
85     stUart->intMask  = 0;       
86     stUart->intMask = RXINT | TXINT;
87 }
88
89
90 /* prom_putc()
91  * Output a character to the UART
92  */
93 void prom_putc(char c)
94 {
95         /* Wait for Tx uffer to empty */
96         while (! (READ16(stUart->intStatus) & TXFIFOEMT));
97         /* Send character */
98         stUart->Data = c;
99 }
100
101 /* prom_puts()
102  * Write a string to the UART
103  */
104 void prom_puts(const char *s)
105 {
106         while (*s) {
107                 if (*s == '\n') {
108                         prom_putc('\r');
109                 }
110                 prom_putc(*s++);
111         }
112 }
113
114
115 /* prom_getc_nowait()
116  * Returns a character from the UART
117  * Returns -1 if no characters available or corrupted
118  */
119 int prom_getc_nowait(void)
120 {
121     uint16  uStatus;
122     int    cData = -1;
123
124      uStatus = READ16(stUart->intStatus);
125
126      if (uStatus & RXFIFONE) { /* Do we have a character? */
127            cData =  READ16(stUart->Data) & 0xff; /* Read character */
128            if (uStatus & (RXFRAMERR | RXPARERR)) {  /* If we got an error, throw it away */
129                cData = -1;
130            }
131   }
132
133    return cData;
134 }
135
136 /* prom_getc()
137  * Returns a charcter from the serial port
138  * Will block until it receives a valid character
139 */
140 char prom_getc(void)
141 {
142     int    cData = -1;
143
144     /* Loop until we get a valid character */
145     while(cData == -1) {
146         cData = prom_getc_nowait();
147     }
148    return (char) cData;
149 }
150
151 /* prom_testc()
152  * Returns 0 if no characters available
153  */
154 int prom_testc(void)
155 {
156     uint16  uStatus;
157
158      uStatus = READ16(stUart->intStatus);
159
160      return (uStatus & RXFIFONE);
161 }
162
163 #if CONFIG_REMOTE_DEBUG
164 /* Prevent other code from writing to the serial port */
165 void _putc(char c) { }
166 void _puts(const char *ptr) { }
167 #else
168 /* Low level outputs call prom routines */
169 void _putc(char c) {
170         prom_putc(c);
171 }
172 void _puts(const char *ptr) {
173         prom_puts(ptr);
174 }
175 #endif