Merge git://oss.sgi.com:8090/oss/git/xfs-2.6
[sfrench/cifs-2.6.git] / arch / ppc / boot / simple / mpc52xx_tty.c
1 /*
2  * Minimal serial functions needed to send messages out a MPC52xx
3  * Programmable Serial Controller (PSC).
4  *
5  * Author: Dale Farnsworth <dfarnsworth@mvista.com>
6  *
7  * 2003-2004 (c) MontaVista, Software, Inc.  This file is licensed under the
8  * terms of the GNU General Public License version 2.  This program is licensed
9  * "as is" without any warranty of any kind, whether express or implied.
10  */
11
12 #include <linux/config.h>
13 #include <linux/types.h>
14 #include <asm/uaccess.h>
15 #include <asm/mpc52xx.h>
16 #include <asm/mpc52xx_psc.h>
17 #include <asm/serial.h>
18 #include <asm/io.h>
19 #include <asm/time.h>
20
21
22 #ifdef MPC52xx_PF_CONSOLE_PORT
23 #define MPC52xx_CONSOLE MPC52xx_PSCx_OFFSET(MPC52xx_PF_CONSOLE_PORT)
24 #define MPC52xx_PSC_CONFIG_SHIFT ((MPC52xx_PF_CONSOLE_PORT-1)<<2)
25 #else
26 #error "MPC52xx_PF_CONSOLE_PORT not defined"
27 #endif
28
29 static struct mpc52xx_psc __iomem *psc =
30         (struct mpc52xx_psc __iomem *) MPC52xx_PA(MPC52xx_CONSOLE);
31
32 /* The decrementer counts at the system bus clock frequency
33  * divided by four.  The most accurate time base is connected to the
34  * rtc.  We read the decrementer change during one rtc tick
35  * and multiply by 4 to get the system bus clock frequency. Since a
36  * rtc tick is one seconds, and that's pretty long, we change the rtc
37  * dividers temporarly to set them 64x faster ;)
38  */
39 static int
40 mpc52xx_ipbfreq(void)
41 {
42         struct mpc52xx_rtc __iomem *rtc =
43                 (struct mpc52xx_rtc __iomem *) MPC52xx_PA(MPC52xx_RTC_OFFSET);
44         struct mpc52xx_cdm __iomem *cdm =
45                 (struct mpc52xx_cdm __iomem *) MPC52xx_PA(MPC52xx_CDM_OFFSET);
46         int current_time, previous_time;
47         int tbl_start, tbl_end;
48         int xlbfreq, ipbfreq;
49
50         out_be32(&rtc->dividers, 0x8f1f0000);   /* Set RTC 64x faster */
51         previous_time = in_be32(&rtc->time);
52         while ((current_time = in_be32(&rtc->time)) == previous_time) ;
53         tbl_start = get_tbl();
54         previous_time = current_time;
55         while ((current_time = in_be32(&rtc->time)) == previous_time) ;
56         tbl_end = get_tbl();
57         out_be32(&rtc->dividers, 0xffff0000);   /* Restore RTC */
58
59         xlbfreq = (tbl_end - tbl_start) << 8;
60         ipbfreq = (in_8(&cdm->ipb_clk_sel) & 1) ? xlbfreq / 2 : xlbfreq;
61
62         return ipbfreq;
63 }
64
65 unsigned long
66 serial_init(int ignored, void *ignored2)
67 {
68         struct mpc52xx_gpio __iomem *gpio =
69                 (struct mpc52xx_gpio __iomem *) MPC52xx_PA(MPC52xx_GPIO_OFFSET);
70         int divisor;
71         int mode1;
72         int mode2;
73         u32 val32;
74
75         static int been_here = 0;
76
77         if (been_here)
78                 return 0;
79
80         been_here = 1;
81
82         val32 = in_be32(&gpio->port_config);
83         val32 &= ~(0x7 << MPC52xx_PSC_CONFIG_SHIFT);
84         val32 |= MPC52xx_GPIO_PSC_CONFIG_UART_WITHOUT_CD
85                                 << MPC52xx_PSC_CONFIG_SHIFT;
86         out_be32(&gpio->port_config, val32);
87
88         out_8(&psc->command, MPC52xx_PSC_RST_TX
89                         | MPC52xx_PSC_RX_DISABLE | MPC52xx_PSC_TX_ENABLE);
90         out_8(&psc->command, MPC52xx_PSC_RST_RX);
91
92         out_be32(&psc->sicr, 0x0);
93         out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00);
94         out_be16(&psc->tfalarm, 0xf8);
95
96         out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1
97                         | MPC52xx_PSC_RX_ENABLE
98                         | MPC52xx_PSC_TX_ENABLE);
99
100         divisor = ((mpc52xx_ipbfreq()
101                         / (CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD * 16)) + 1) >> 1;
102
103         mode1 = MPC52xx_PSC_MODE_8_BITS | MPC52xx_PSC_MODE_PARNONE
104                         | MPC52xx_PSC_MODE_ERR;
105         mode2 = MPC52xx_PSC_MODE_ONE_STOP;
106
107         out_8(&psc->ctur, divisor>>8);
108         out_8(&psc->ctlr, divisor);
109         out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
110         out_8(&psc->mode, mode1);
111         out_8(&psc->mode, mode2);
112
113         return 0;       /* ignored */
114 }
115
116 void
117 serial_putc(void *ignored, const char c)
118 {
119         serial_init(0, NULL);
120
121         while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP)) ;
122         out_8(&psc->mpc52xx_psc_buffer_8, c);
123         while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP)) ;
124 }
125
126 char
127 serial_getc(void *ignored)
128 {
129         while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY)) ;
130
131         return in_8(&psc->mpc52xx_psc_buffer_8);
132 }
133
134 int
135 serial_tstc(void *ignored)
136 {
137         return (in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY) != 0;
138 }