vserver 1.9.3
[linux-2.6.git] / arch / ppc / syslib / mpc52xx_setup.c
1 /*
2  * arch/ppc/syslib/mpc52xx_setup.c
3  *
4  * Common code for the boards based on Freescale MPC52xx embedded CPU.
5  *
6  * 
7  * Maintainer : Sylvain Munaut <tnt@246tNt.com>
8  *
9  * Support for other bootloaders than UBoot by Dale Farnsworth 
10  * <dfarnsworth@mvista.com>
11  * 
12  * Copyright (C) 2004 Sylvain Munaut <tnt@246tNt.com>
13  * Copyright (C) 2003 Montavista Software, Inc
14  * 
15  * This file is licensed under the terms of the GNU General Public License
16  * version 2. This program is licensed "as is" without any warranty of any
17  * kind, whether express or implied.
18  */
19
20 #include <linux/config.h>
21
22 #include <asm/time.h>
23 #include <asm/mpc52xx.h>
24 #include <asm/mpc52xx_psc.h>
25 #include <asm/ocp.h>
26 #include <asm/pgtable.h>
27 #include <asm/ppcboot.h>
28
29 extern bd_t __res;
30
31 static int core_mult[] = {              /* CPU Frequency multiplier, taken    */
32         0,  0,  0,  10, 20, 20, 25, 45, /* from the datasheet used to compute */
33         30, 55, 40, 50, 0,  60, 35, 0,  /* CPU frequency from XLB freq and    */
34         30, 25, 65, 10, 70, 20, 75, 45, /* external jumper config             */
35         0,  55, 40, 50, 80, 60, 35, 0
36 };
37
38 void
39 mpc52xx_restart(char *cmd)
40 {
41         struct mpc52xx_gpt* gpt0 = (struct mpc52xx_gpt*) MPC52xx_GPTx(0);
42
43         local_irq_disable();
44
45         /* Turn on the watchdog and wait for it to expire. It effectively
46           does a reset */
47         if (gpt0 != NULL) {
48                 out_be32(&gpt0->count, 0x000000ff);
49                 out_be32(&gpt0->mode, 0x00009004);
50         } else
51                 printk(KERN_ERR "mpc52xx_restart: Unable to ioremap GPT0 registers, -> looping ...");
52
53         while (1);
54 }
55
56 void
57 mpc52xx_halt(void)
58 {
59         local_irq_disable();
60
61         while (1);
62 }
63
64 void
65 mpc52xx_power_off(void)
66 {
67         /* By default we don't have any way of shut down.
68            If a specific board wants to, it can set the power down
69            code to any hardware implementation dependent code */
70         mpc52xx_halt();
71 }
72
73
74 void __init
75 mpc52xx_set_bat(void)
76 {
77         /* Set BAT 2 to map the 0xf0000000 area */
78         /* This mapping is used during mpc52xx_progress,
79          * mpc52xx_find_end_of_memory, and UARTs/GPIO access for debug
80          */
81         mb();
82         mtspr(DBAT2U, 0xf0001ffe);
83         mtspr(DBAT2L, 0xf000002a);
84         mb();
85 }
86
87 void __init
88 mpc52xx_map_io(void)
89 {
90         /* Here we only map the MBAR */
91         io_block_mapping(
92                 MPC52xx_MBAR_VIRT, MPC52xx_MBAR, MPC52xx_MBAR_SIZE, _PAGE_IO);
93 }
94
95
96 #ifdef CONFIG_SERIAL_TEXT_DEBUG
97 #ifdef MPC52xx_PF_CONSOLE_PORT
98 #define MPC52xx_CONSOLE MPC52xx_PSCx(MPC52xx_PF_CONSOLE_PORT)
99 #else
100 #error "mpc52xx PSC for console not selected"
101 #endif
102
103 static void
104 mpc52xx_psc_putc(struct mpc52xx_psc * psc, unsigned char c)
105 {
106         while (!(in_be16(&psc->mpc52xx_psc_status) &
107                  MPC52xx_PSC_SR_TXRDY));
108         out_8(&psc->mpc52xx_psc_buffer_8, c);
109 }
110
111 void
112 mpc52xx_progress(char *s, unsigned short hex)
113 {
114         struct mpc52xx_psc *psc = (struct mpc52xx_psc *)MPC52xx_CONSOLE;
115         char c;
116
117         while ((c = *s++) != 0) {
118                 if (c == '\n')
119                         mpc52xx_psc_putc(psc, '\r');
120                 mpc52xx_psc_putc(psc, c);
121         }
122
123         mpc52xx_psc_putc(psc, '\r');
124         mpc52xx_psc_putc(psc, '\n');
125 }
126
127 #endif  /* CONFIG_SERIAL_TEXT_DEBUG */
128
129
130 unsigned long __init
131 mpc52xx_find_end_of_memory(void)
132 {
133         u32 ramsize = __res.bi_memsize;
134
135         /*
136          * if bootloader passed a memsize, just use it
137          * else get size from sdram config registers
138          */
139         if (ramsize == 0) {
140                 struct mpc52xx_mmap_ctl *mmap_ctl;
141                 u32 sdram_config_0, sdram_config_1;
142
143                 /* Temp BAT2 mapping active when this is called ! */
144                 mmap_ctl = (struct mpc52xx_mmap_ctl*) MPC52xx_MMAP_CTL;
145
146                 sdram_config_0 = in_be32(&mmap_ctl->sdram0);
147                 sdram_config_1 = in_be32(&mmap_ctl->sdram1);
148
149                 if ((sdram_config_0 & 0x1f) >= 0x13)
150                         ramsize = 1 << ((sdram_config_0 & 0xf) + 17);
151
152                 if (((sdram_config_1 & 0x1f) >= 0x13) &&
153                                 ((sdram_config_1 & 0xfff00000) == ramsize))
154                         ramsize += 1 << ((sdram_config_1 & 0xf) + 17);
155         }
156
157         return ramsize;
158 }
159
160 void __init
161 mpc52xx_calibrate_decr(void)
162 {
163         int current_time, previous_time;
164         int tbl_start, tbl_end;
165         unsigned int xlbfreq, cpufreq, ipbfreq, pcifreq, divisor;
166
167         xlbfreq = __res.bi_busfreq;
168         /* if bootloader didn't pass bus frequencies, calculate them */
169         if (xlbfreq == 0) {
170                 /* Get RTC & Clock manager modules */
171                 struct mpc52xx_rtc *rtc;
172                 struct mpc52xx_cdm *cdm;
173
174                 rtc = (struct mpc52xx_rtc*)
175                         ioremap(MPC52xx_RTC, sizeof(struct mpc52xx_rtc));
176                 cdm = (struct mpc52xx_cdm*)
177                         ioremap(MPC52xx_CDM, sizeof(struct mpc52xx_cdm));
178
179                 if ((rtc==NULL) || (cdm==NULL))
180                         panic("Can't ioremap RTC/CDM while computing bus freq");
181
182                 /* Count bus clock during 1/64 sec */
183                 out_be32(&rtc->dividers, 0x8f1f0000);   /* Set RTC 64x faster */
184                 previous_time = in_be32(&rtc->time);
185                 while ((current_time = in_be32(&rtc->time)) == previous_time) ;
186                 tbl_start = get_tbl();
187                 previous_time = current_time;
188                 while ((current_time = in_be32(&rtc->time)) == previous_time) ;
189                 tbl_end = get_tbl();
190                 out_be32(&rtc->dividers, 0xffff0000);   /* Restore RTC */
191
192                 /* Compute all frequency from that & CDM settings */
193                 xlbfreq = (tbl_end - tbl_start) << 8;
194                 cpufreq = (xlbfreq * core_mult[in_be32(&cdm->rstcfg)&0x1f])/10;
195                 ipbfreq = (in_8(&cdm->ipb_clk_sel) & 1) ?
196                                         xlbfreq / 2 : xlbfreq;
197                 switch (in_8(&cdm->pci_clk_sel) & 3) {
198                 case 0:
199                         pcifreq = ipbfreq;
200                         break;
201                 case 1:
202                         pcifreq = ipbfreq / 2;
203                         break;
204                 default:
205                         pcifreq = xlbfreq / 4;
206                         break;
207                 }
208                 __res.bi_busfreq = xlbfreq;
209                 __res.bi_intfreq = cpufreq;
210                 __res.bi_ipbfreq = ipbfreq;
211                 __res.bi_pcifreq = pcifreq;
212
213                 /* Release mapping */
214                 iounmap((void*)rtc);
215                 iounmap((void*)cdm);
216         }
217
218         divisor = 4;
219
220         tb_ticks_per_jiffy = xlbfreq / HZ / divisor;
221         tb_to_us = mulhwu_scale_factor(xlbfreq / divisor, 1000000);
222 }
223
224
225 void __init
226 mpc52xx_add_board_devices(struct ocp_def board_ocp[]) {
227         while (board_ocp->vendor != OCP_VENDOR_INVALID)
228                 if(ocp_add_one_device(board_ocp++))
229                         printk("mpc5200-ocp: Failed to add board device !\n");
230 }
231