Coding style cleanup
[platform/kernel/u-boot.git] / board / mpc8540ads / mpc8540ads.c
1  /*
2  * Copyright 2004 Freescale Semiconductor.
3  * (C) Copyright 2002,2003, Motorola Inc.
4  * Xianghua Xiao, (X.Xiao@motorola.com)
5  *
6  * (C) Copyright 2002 Scott McNutt <smcnutt@artesyncp.com>
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26
27
28 #include <common.h>
29 #include <pci.h>
30 #include <asm/processor.h>
31 #include <asm/immap_85xx.h>
32 #include <spd.h>
33
34 #if defined(CONFIG_DDR_ECC)
35 extern void ddr_enable_ecc(unsigned int dram_size);
36 #endif
37
38 extern long int spd_sdram(void);
39
40 void local_bus_init(void);
41 void sdram_init(void);
42 long int fixed_sdram(void);
43
44
45 int board_early_init_f (void)
46 {
47     return 0;
48 }
49
50 int checkboard (void)
51 {
52         puts("Board: ADS\n");
53
54 #ifdef CONFIG_PCI
55         printf("    PCI1: 32 bit, %d MHz (compiled)\n",
56                CONFIG_SYS_CLK_FREQ / 1000000);
57 #else
58         printf("    PCI1: disabled\n");
59 #endif
60
61         /*
62          * Initialize local bus.
63          */
64         local_bus_init();
65
66         return 0;
67 }
68
69
70 long int
71 initdram(int board_type)
72 {
73         long dram_size = 0;
74         extern long spd_sdram (void);
75         volatile immap_t *immap = (immap_t *)CFG_IMMR;
76
77         puts("Initializing\n");
78
79 #if defined(CONFIG_DDR_DLL)
80         {
81             volatile ccsr_gur_t *gur= &immap->im_gur;
82             uint temp_ddrdll = 0;
83
84             /*
85              * Work around to stabilize DDR DLL
86              */
87             temp_ddrdll = gur->ddrdllcr;
88             gur->ddrdllcr = ((temp_ddrdll & 0xff) << 16) | 0x80000000;
89             asm("sync;isync;msync");
90         }
91 #endif
92
93 #if defined(CONFIG_SPD_EEPROM)
94         dram_size = spd_sdram ();
95 #else
96         dram_size = fixed_sdram ();
97 #endif
98
99 #if defined(CONFIG_DDR_ECC)
100         /*
101          * Initialize and enable DDR ECC.
102          */
103         ddr_enable_ecc(dram_size);
104 #endif
105
106         /*
107          * Initialize SDRAM.
108          */
109         sdram_init();
110
111         puts("    DDR: ");
112         return dram_size;
113 }
114
115
116 /*
117  * Initialize Local Bus
118  */
119
120 void
121 local_bus_init(void)
122 {
123         volatile immap_t *immap = (immap_t *)CFG_IMMR;
124         volatile ccsr_gur_t *gur = &immap->im_gur;
125         volatile ccsr_lbc_t *lbc = &immap->im_lbc;
126
127         uint clkdiv;
128         uint lbc_hz;
129         sys_info_t sysinfo;
130
131         /*
132          * Errata LBC11.
133          * Fix Local Bus clock glitch when DLL is enabled.
134          *
135          * If localbus freq is < 66Mhz, DLL bypass mode must be used.
136          * If localbus freq is > 133Mhz, DLL can be safely enabled.
137          * Between 66 and 133, the DLL is enabled with an override workaround.
138          */
139
140         get_sys_info(&sysinfo);
141         clkdiv = lbc->lcrr & 0x0f;
142         lbc_hz = sysinfo.freqSystemBus / 1000000 / clkdiv;
143
144         if (lbc_hz < 66) {
145                 lbc->lcrr = CFG_LBC_LCRR | 0x80000000;  /* DLL Bypass */
146
147         } else if (lbc_hz >= 133) {
148                 lbc->lcrr = CFG_LBC_LCRR & (~0x80000000); /* DLL Enabled */
149
150         } else {
151                 /*
152                  * On REV1 boards, need to change CLKDIV before enable DLL.
153                  * Default CLKDIV is 8, change it to 4 temporarily.
154                  */
155                 uint pvr = get_pvr();
156                 uint temp_lbcdll = 0;
157
158                 if (pvr == PVR_85xx_REV1) {
159                         /* FIXME: Justify the high bit here. */
160                         lbc->lcrr = 0x10000004;
161                 }
162
163                 lbc->lcrr = CFG_LBC_LCRR & (~0x80000000); /* DLL Enabled */
164                 udelay(200);
165
166                 /*
167                  * Sample LBC DLL ctrl reg, upshift it to set the
168                  * override bits.
169                  */
170                 temp_lbcdll = gur->lbcdllcr;
171                 gur->lbcdllcr = (((temp_lbcdll & 0xff) << 16) | 0x80000000);
172                 asm("sync;isync;msync");
173         }
174 }
175
176
177 /*
178  * Initialize SDRAM memory on the Local Bus.
179  */
180
181 void
182 sdram_init(void)
183 {
184         volatile immap_t *immap = (immap_t *)CFG_IMMR;
185         volatile ccsr_lbc_t *lbc= &immap->im_lbc;
186         uint *sdram_addr = (uint *)CFG_LBC_SDRAM_BASE;
187
188         puts("    SDRAM: ");
189         print_size (CFG_LBC_SDRAM_SIZE * 1024 * 1024, "\n");
190
191         /*
192          * Setup SDRAM Base and Option Registers
193          */
194         lbc->or2 = CFG_OR2_PRELIM;
195         lbc->br2 = CFG_BR2_PRELIM;
196         lbc->lbcr = CFG_LBC_LBCR;
197         asm("msync");
198
199         lbc->lsrt = CFG_LBC_LSRT;
200         lbc->mrtpr = CFG_LBC_MRTPR;
201         asm("sync");
202
203         /*
204          * Configure the SDRAM controller.
205          */
206         lbc->lsdmr = CFG_LBC_LSDMR_1;
207         asm("sync");
208         *sdram_addr = 0xff;
209         ppcDcbf((unsigned long) sdram_addr);
210         udelay(100);
211
212         lbc->lsdmr = CFG_LBC_LSDMR_2;
213         asm("sync");
214         *sdram_addr = 0xff;
215         ppcDcbf((unsigned long) sdram_addr);
216         udelay(100);
217
218         lbc->lsdmr = CFG_LBC_LSDMR_3;
219         asm("sync");
220         *sdram_addr = 0xff;
221         ppcDcbf((unsigned long) sdram_addr);
222         udelay(100);
223
224         lbc->lsdmr = CFG_LBC_LSDMR_4;
225         asm("sync");
226         *sdram_addr = 0xff;
227         ppcDcbf((unsigned long) sdram_addr);
228         udelay(100);
229
230         lbc->lsdmr = CFG_LBC_LSDMR_5;
231         asm("sync");
232         *sdram_addr = 0xff;
233         ppcDcbf((unsigned long) sdram_addr);
234         udelay(100);
235 }
236
237
238 #if defined(CFG_DRAM_TEST)
239 int testdram (void)
240 {
241         uint *pstart = (uint *) CFG_MEMTEST_START;
242         uint *pend = (uint *) CFG_MEMTEST_END;
243         uint *p;
244
245         printf("SDRAM test phase 1:\n");
246         for (p = pstart; p < pend; p++)
247                 *p = 0xaaaaaaaa;
248
249         for (p = pstart; p < pend; p++) {
250                 if (*p != 0xaaaaaaaa) {
251                         printf ("SDRAM test fails at: %08x\n", (uint) p);
252                         return 1;
253                 }
254         }
255
256         printf("SDRAM test phase 2:\n");
257         for (p = pstart; p < pend; p++)
258                 *p = 0x55555555;
259
260         for (p = pstart; p < pend; p++) {
261                 if (*p != 0x55555555) {
262                         printf ("SDRAM test fails at: %08x\n", (uint) p);
263                         return 1;
264                 }
265         }
266
267         printf("SDRAM test passed.\n");
268         return 0;
269 }
270 #endif
271
272
273 #if !defined(CONFIG_SPD_EEPROM)
274 /*************************************************************************
275  *  fixed sdram init -- doesn't use serial presence detect.
276  ************************************************************************/
277 long int fixed_sdram (void)
278 {
279   #ifndef CFG_RAMBOOT
280         volatile immap_t *immap = (immap_t *)CFG_IMMR;
281         volatile ccsr_ddr_t *ddr= &immap->im_ddr;
282
283         ddr->cs0_bnds = CFG_DDR_CS0_BNDS;
284         ddr->cs0_config = CFG_DDR_CS0_CONFIG;
285         ddr->timing_cfg_1 = CFG_DDR_TIMING_1;
286         ddr->timing_cfg_2 = CFG_DDR_TIMING_2;
287         ddr->sdram_mode = CFG_DDR_MODE;
288         ddr->sdram_interval = CFG_DDR_INTERVAL;
289     #if defined (CONFIG_DDR_ECC)
290         ddr->err_disable = 0x0000000D;
291         ddr->err_sbe = 0x00ff0000;
292     #endif
293         asm("sync;isync;msync");
294         udelay(500);
295     #if defined (CONFIG_DDR_ECC)
296         /* Enable ECC checking */
297         ddr->sdram_cfg = (CFG_DDR_CONTROL | 0x20000000);
298     #else
299         ddr->sdram_cfg = CFG_DDR_CONTROL;
300     #endif
301         asm("sync; isync; msync");
302         udelay(500);
303   #endif
304         return CFG_SDRAM_SIZE * 1024 * 1024;
305 }
306 #endif  /* !defined(CONFIG_SPD_EEPROM) */
307
308
309 #if defined(CONFIG_PCI)
310 /*
311  * Initialize PCI Devices, report devices found.
312  */
313
314 #ifndef CONFIG_PCI_PNP
315 static struct pci_config_table pci_mpc85xxads_config_table[] = {
316     { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
317       PCI_IDSEL_NUMBER, PCI_ANY_ID,
318       pci_cfgfunc_config_device, { PCI_ENET0_IOADDR,
319                                    PCI_ENET0_MEMADDR,
320                                    PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
321       } },
322     { }
323 };
324 #endif
325
326
327 static struct pci_controller hose = {
328 #ifndef CONFIG_PCI_PNP
329         config_table: pci_mpc85xxads_config_table,
330 #endif
331 };
332
333 #endif  /* CONFIG_PCI */
334
335
336 void
337 pci_init_board(void)
338 {
339 #ifdef CONFIG_PCI
340         extern void pci_mpc85xx_init(struct pci_controller *hose);
341
342         pci_mpc85xx_init(&hose);
343 #endif /* CONFIG_PCI */
344 }