]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/freescale/p2020ds/p2020ds.c
fsl: improve the PIXIS code and fix a few bugs
[karo-tx-uboot.git] / board / freescale / p2020ds / p2020ds.c
1 /*
2  * Copyright 2007-2009 Freescale Semiconductor, Inc.
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22
23 #include <common.h>
24 #include <command.h>
25 #include <pci.h>
26 #include <asm/processor.h>
27 #include <asm/mmu.h>
28 #include <asm/cache.h>
29 #include <asm/immap_85xx.h>
30 #include <asm/fsl_pci.h>
31 #include <asm/fsl_ddr_sdram.h>
32 #include <asm/io.h>
33 #include <miiphy.h>
34 #include <libfdt.h>
35 #include <fdt_support.h>
36 #include <tsec.h>
37 #include <asm/fsl_law.h>
38 #include <asm/mp.h>
39 #include <netdev.h>
40
41 #include "../common/sgmii_riser.h"
42
43 DECLARE_GLOBAL_DATA_PTR;
44
45 phys_size_t fixed_sdram(void);
46
47 int checkboard(void)
48 {
49         u8 sw7;
50         u8 *pixis_base = (u8 *)PIXIS_BASE;
51
52         puts("Board: P2020DS ");
53 #ifdef CONFIG_PHYS_64BIT
54         puts("(36-bit addrmap) ");
55 #endif
56
57         printf("Sys ID: 0x%02x, "
58                 "Sys Ver: 0x%02x, FPGA Ver: 0x%02x, ",
59                 in_8(pixis_base + PIXIS_ID), in_8(pixis_base + PIXIS_VER),
60                 in_8(pixis_base + PIXIS_PVER));
61
62         sw7 = in_8(pixis_base + PIXIS_SW(7));
63         switch ((sw7 & PIXIS_SW7_LBMAP) >> 6) {
64                 case 0:
65                 case 1:
66                         printf ("vBank: %d\n", ((sw7 & PIXIS_SW7_VBANK) >> 4));
67                         break;
68                 case 2:
69                 case 3:
70                         puts ("Promjet\n");
71                         break;
72         }
73
74         return 0;
75 }
76
77 phys_size_t initdram(int board_type)
78 {
79         phys_size_t dram_size = 0;
80
81         puts("Initializing....");
82
83 #ifdef CONFIG_SPD_EEPROM
84         dram_size = fsl_ddr_sdram();
85 #else
86         dram_size = fixed_sdram();
87
88         if (set_ddr_laws(CONFIG_SYS_DDR_SDRAM_BASE,
89                          dram_size,
90                          LAW_TRGT_IF_DDR) < 0) {
91                 printf("ERROR setting Local Access Windows for DDR\n");
92                 return 0;
93         };
94 #endif
95         dram_size = setup_ddr_tlbs(dram_size / 0x100000);
96         dram_size *= 0x100000;
97
98         puts("    DDR: ");
99         return dram_size;
100 }
101
102 #if !defined(CONFIG_SPD_EEPROM)
103 /*
104  * Fixed sdram init -- doesn't use serial presence detect.
105  */
106
107 phys_size_t fixed_sdram(void)
108 {
109         volatile ccsr_ddr_t *ddr = (ccsr_ddr_t *)CONFIG_SYS_MPC85xx_DDR_ADDR;
110         uint d_init;
111
112         ddr->cs0_config = CONFIG_SYS_DDR_CS0_CONFIG;
113         ddr->timing_cfg_3 = CONFIG_SYS_DDR_TIMING_3;
114         ddr->timing_cfg_0 = CONFIG_SYS_DDR_TIMING_0;
115         ddr->sdram_mode = CONFIG_SYS_DDR_MODE_1;
116         ddr->sdram_mode_2 = CONFIG_SYS_DDR_MODE_2;
117         ddr->sdram_md_cntl = CONFIG_SYS_DDR_MODE_CTRL;
118         ddr->sdram_interval = CONFIG_SYS_DDR_INTERVAL;
119         ddr->sdram_data_init = CONFIG_SYS_DDR_DATA_INIT;
120         ddr->sdram_clk_cntl = CONFIG_SYS_DDR_CLK_CTRL;
121         ddr->sdram_cfg_2 = CONFIG_SYS_DDR_CONTROL2;
122         ddr->ddr_zq_cntl = CONFIG_SYS_DDR_ZQ_CNTL;
123         ddr->ddr_wrlvl_cntl = CONFIG_SYS_DDR_WRLVL_CNTL;
124         ddr->ddr_cdr1 = CONFIG_SYS_DDR_CDR1;
125         ddr->timing_cfg_4 = CONFIG_SYS_DDR_TIMING_4;
126         ddr->timing_cfg_5 = CONFIG_SYS_DDR_TIMING_5;
127
128         if (!strcmp("performance", getenv("perf_mode"))) {
129                 /* Performance Mode Values */
130
131                 ddr->cs1_config = CONFIG_SYS_DDR_CS1_CONFIG_PERF;
132                 ddr->cs0_bnds = CONFIG_SYS_DDR_CS0_BNDS_PERF;
133                 ddr->cs1_bnds = CONFIG_SYS_DDR_CS1_BNDS_PERF;
134                 ddr->timing_cfg_1 = CONFIG_SYS_DDR_TIMING_1_PERF;
135                 ddr->timing_cfg_2 = CONFIG_SYS_DDR_TIMING_2_PERF;
136
137                 asm("sync;isync");
138
139                 udelay(500);
140
141                 ddr->sdram_cfg = CONFIG_SYS_DDR_CONTROL_PERF;
142         } else {
143                 /* Stable Mode Values */
144
145                 ddr->cs1_config = CONFIG_SYS_DDR_CS1_CONFIG;
146                 ddr->cs0_bnds = CONFIG_SYS_DDR_CS0_BNDS;
147                 ddr->cs1_bnds = CONFIG_SYS_DDR_CS1_BNDS;
148                 ddr->timing_cfg_1 = CONFIG_SYS_DDR_TIMING_1;
149                 ddr->timing_cfg_2 = CONFIG_SYS_DDR_TIMING_2;
150
151                 /* ECC will be assumed in stable mode */
152                 ddr->err_int_en = CONFIG_SYS_DDR_ERR_INT_EN;
153                 ddr->err_disable = CONFIG_SYS_DDR_ERR_DIS;
154                 ddr->err_sbe = CONFIG_SYS_DDR_SBE;
155
156                 asm("sync;isync");
157
158                 udelay(500);
159
160                 ddr->sdram_cfg = CONFIG_SYS_DDR_CONTROL;
161         }
162
163 #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
164         d_init = 1;
165         debug("DDR - 1st controller: memory initializing\n");
166         /*
167          * Poll until memory is initialized.
168          * 512 Meg at 400 might hit this 200 times or so.
169          */
170         while ((ddr->sdram_cfg_2 & (d_init << 4)) != 0)
171                 udelay(1000);
172         debug("DDR: memory initialized\n\n");
173         asm("sync; isync");
174         udelay(500);
175 #endif
176
177         return CONFIG_SYS_SDRAM_SIZE * 1024 * 1024;
178 }
179
180 #endif
181
182 #ifdef CONFIG_PCIE1
183 static struct pci_controller pcie1_hose;
184 #endif
185
186 #ifdef CONFIG_PCIE2
187 static struct pci_controller pcie2_hose;
188 #endif
189
190 #ifdef CONFIG_PCIE3
191 static struct pci_controller pcie3_hose;
192 #endif
193
194 #ifdef CONFIG_PCI
195 void pci_init_board(void)
196 {
197         volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
198         struct fsl_pci_info pci_info[3];
199         u32 devdisr, pordevsr, io_sel;
200         int first_free_busno = 0;
201         int num = 0;
202
203         int pcie_ep, pcie_configured;
204
205         devdisr = in_be32(&gur->devdisr);
206         pordevsr = in_be32(&gur->pordevsr);
207         io_sel = (pordevsr & MPC85xx_PORDEVSR_IO_SEL) >> 19;
208
209         debug ("   pci_init_board: devdisr=%x, io_sel=%x\n", devdisr, io_sel);
210
211         if (!(pordevsr & MPC85xx_PORDEVSR_SGMII2_DIS))
212                 printf("    eTSEC2 is in sgmii mode.\n");
213         if (!(pordevsr & MPC85xx_PORDEVSR_SGMII3_DIS))
214                 printf("    eTSEC3 is in sgmii mode.\n");
215
216         puts("\n");
217 #ifdef CONFIG_PCIE2
218         pcie_configured = is_fsl_pci_cfg(LAW_TRGT_IF_PCIE_2, io_sel);
219
220         if (pcie_configured && !(devdisr & MPC85xx_DEVDISR_PCIE2)) {
221                 SET_STD_PCIE_INFO(pci_info[num], 2);
222                 pcie_ep = fsl_setup_hose(&pcie2_hose, pci_info[num].regs);
223                 printf("    PCIE2 connected to ULI as %s (base addr %lx)\n",
224                                 pcie_ep ? "Endpoint" : "Root Complex",
225                                 pci_info[num].regs);
226                 first_free_busno = fsl_pci_init_port(&pci_info[num++],
227                                         &pcie2_hose, first_free_busno);
228
229                 /*
230                  * The workaround doesn't work on p2020 because the location
231                  * we try and read isn't valid on p2020, fix this later
232                  */
233 #if 0
234                 /*
235                  * Activate ULI1575 legacy chip by performing a fake
236                  * memory access.  Needed to make ULI RTC work.
237                  * Device 1d has the first on-board memory BAR.
238                  */
239
240                 pci_hose_read_config_dword(hose, PCI_BDF(2, 0x1d, 0),
241                                 PCI_BASE_ADDRESS_1, &temp32);
242                 if (temp32 >= CONFIG_SYS_PCIE3_MEM_BUS) {
243                         void *p = pci_mem_to_virt(PCI_BDF(2, 0x1d, 0),
244                                                         temp32, 4, 0);
245                         debug(" uli1575 read to %p\n", p);
246                         in_be32(p);
247                 }
248 #endif
249         } else {
250                 printf("    PCIE2: disabled\n");
251         }
252         puts("\n");
253 #else
254         setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_PCIE2); /* disable */
255 #endif
256
257 #ifdef CONFIG_PCIE3
258         pcie_configured = is_fsl_pci_cfg(LAW_TRGT_IF_PCIE_3, io_sel);
259
260         if (pcie_configured && !(devdisr & MPC85xx_DEVDISR_PCIE3)) {
261                 SET_STD_PCIE_INFO(pci_info[num], 3);
262                 pcie_ep = fsl_setup_hose(&pcie3_hose, pci_info[num].regs);
263                 printf("    PCIE3 connected to Slot 1 as %s (base addr %lx)\n",
264                                 pcie_ep ? "Endpoint" : "Root Complex",
265                                 pci_info[num].regs);
266                 first_free_busno = fsl_pci_init_port(&pci_info[num++],
267                                         &pcie3_hose, first_free_busno);
268         } else {
269                 printf("    PCIE3: disabled\n");
270         }
271         puts("\n");
272 #else
273         setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_PCIE3); /* disable */
274 #endif
275
276 #ifdef CONFIG_PCIE1
277         pcie_configured = is_fsl_pci_cfg(LAW_TRGT_IF_PCIE_1, io_sel);
278
279         if (pcie_configured && !(devdisr & MPC85xx_DEVDISR_PCIE)) {
280                 SET_STD_PCIE_INFO(pci_info[num], 1);
281                 pcie_ep = fsl_setup_hose(&pcie1_hose, pci_info[num].regs);
282                 printf("    PCIE1 connected to Slot 2 as %s (base addr %lx)\n",
283                                 pcie_ep ? "Endpoint" : "Root Complex",
284                                 pci_info[num].regs);
285                 first_free_busno = fsl_pci_init_port(&pci_info[num++],
286                                         &pcie1_hose, first_free_busno);
287         } else {
288                 printf("    PCIE1: disabled\n");
289         }
290         puts("\n");
291 #else
292         setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_PCIE); /* disable */
293 #endif
294 }
295 #endif
296
297 int board_early_init_r(void)
298 {
299         const unsigned int flashbase = CONFIG_SYS_FLASH_BASE;
300         const u8 flash_esel = find_tlb_idx((void *)flashbase, 1);
301
302         /*
303          * Remap Boot flash + PROMJET region to caching-inhibited
304          * so that flash can be erased properly.
305          */
306
307         /* Flush d-cache and invalidate i-cache of any FLASH data */
308         flush_dcache();
309         invalidate_icache();
310
311         /* invalidate existing TLB entry for flash + promjet */
312         disable_tlb(flash_esel);
313
314         set_tlb(1, flashbase, CONFIG_SYS_FLASH_BASE_PHYS,
315                         MAS3_SX|MAS3_SW|MAS3_SR, MAS2_I|MAS2_G,
316                         0, flash_esel, BOOKE_PAGESZ_256M, 1);
317
318         return 0;
319 }
320
321 #ifdef CONFIG_GET_CLK_FROM_ICS307
322 /* decode S[0-2] to Output Divider (OD) */
323 static unsigned char ics307_S_to_OD[] = {
324         10, 2, 8, 4, 5, 7, 3, 6
325 };
326
327 /* Calculate frequency being generated by ICS307-02 clock chip based upon
328  * the control bytes being programmed into it. */
329 /* XXX: This function should probably go into a common library */
330 static unsigned long
331 ics307_clk_freq(unsigned char cw0, unsigned char cw1, unsigned char cw2)
332 {
333         const unsigned long InputFrequency = CONFIG_ICS307_REFCLK_HZ;
334         unsigned long VDW = ((cw1 << 1) & 0x1FE) + ((cw2 >> 7) & 1);
335         unsigned long RDW = cw2 & 0x7F;
336         unsigned long OD = ics307_S_to_OD[cw0 & 0x7];
337         unsigned long freq;
338
339         /* CLK1Frequency = InputFrequency * 2 * (VDW + 8) / ((RDW + 2) * OD) */
340
341         /* cw0:  C1 C0 TTL F1 F0 S2 S1 S0
342          * cw1:  V8 V7 V6 V5 V4 V3 V2 V1
343          * cw2:  V0 R6 R5 R4 R3 R2 R1 R0
344          *
345          * R6:R0 = Reference Divider Word (RDW)
346          * V8:V0 = VCO Divider Word (VDW)
347          * S2:S0 = Output Divider Select (OD)
348          * F1:F0 = Function of CLK2 Output
349          * TTL = duty cycle
350          * C1:C0 = internal load capacitance for cyrstal
351          */
352
353         /* Adding 1 to get a "nicely" rounded number, but this needs
354          * more tweaking to get a "properly" rounded number. */
355
356         freq = 1 + (InputFrequency * 2 * (VDW + 8) / ((RDW + 2) * OD));
357
358         debug("ICS307: CW[0-2]: %02X %02X %02X => %lu Hz\n", cw0, cw1, cw2,
359                         freq);
360         return freq;
361 }
362
363 unsigned long get_board_sys_clk(ulong dummy)
364 {
365         return gd->bus_clk;
366 }
367
368 unsigned long get_board_ddr_clk(ulong dummy)
369 {
370         return gd->mem_clk;
371 }
372
373 unsigned long
374 calculate_board_sys_clk(ulong dummy)
375 {
376         ulong val;
377         u8 *pixis_base = (u8 *)PIXIS_BASE;
378
379         val = ics307_clk_freq(
380             in_8(pixis_base + PIXIS_VSYSCLK0),
381             in_8(pixis_base + PIXIS_VSYSCLK1),
382             in_8(pixis_base + PIXIS_VSYSCLK2));
383         debug("sysclk val = %lu\n", val);
384         return val;
385 }
386
387 unsigned long
388 calculate_board_ddr_clk(ulong dummy)
389 {
390         ulong val;
391         u8 *pixis_base = (u8 *)PIXIS_BASE;
392
393         val = ics307_clk_freq(
394             in_8(pixis_base + PIXIS_VDDRCLK0),
395             in_8(pixis_base + PIXIS_VDDRCLK1),
396             in_8(pixis_base + PIXIS_VDDRCLK2));
397         debug("ddrclk val = %lu\n", val);
398         return val;
399 }
400 #else
401 unsigned long get_board_sys_clk(ulong dummy)
402 {
403         u8 i;
404         ulong val = 0;
405         u8 *pixis_base = (u8 *)PIXIS_BASE;
406
407         i = in_8(pixis_base + PIXIS_SPD);
408         i &= 0x07;
409
410         switch (i) {
411                 case 0:
412                         val = 33333333;
413                         break;
414                 case 1:
415                         val = 40000000;
416                         break;
417                 case 2:
418                         val = 50000000;
419                         break;
420                 case 3:
421                         val = 66666666;
422                         break;
423                 case 4:
424                         val = 83333333;
425                         break;
426                 case 5:
427                         val = 100000000;
428                         break;
429                 case 6:
430                         val = 133333333;
431                         break;
432                 case 7:
433                         val = 166666666;
434                         break;
435         }
436
437         return val;
438 }
439
440 unsigned long get_board_ddr_clk(ulong dummy)
441 {
442         u8 i;
443         ulong val = 0;
444         u8 *pixis_base = (u8 *)PIXIS_BASE;
445
446         i = in_8(pixis_base + PIXIS_SPD);
447         i &= 0x38;
448         i >>= 3;
449
450         switch (i) {
451                 case 0:
452                         val = 33333333;
453                         break;
454                 case 1:
455                         val = 40000000;
456                         break;
457                 case 2:
458                         val = 50000000;
459                         break;
460                 case 3:
461                         val = 66666666;
462                         break;
463                 case 4:
464                         val = 83333333;
465                         break;
466                 case 5:
467                         val = 100000000;
468                         break;
469                 case 6:
470                         val = 133333333;
471                         break;
472                 case 7:
473                         val = 166666666;
474                         break;
475         }
476         return val;
477 }
478 #endif
479
480 #ifdef CONFIG_TSEC_ENET
481 int board_eth_init(bd_t *bis)
482 {
483         struct tsec_info_struct tsec_info[4];
484         volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
485         int num = 0;
486
487 #ifdef CONFIG_TSEC1
488         SET_STD_TSEC_INFO(tsec_info[num], 1);
489         num++;
490 #endif
491 #ifdef CONFIG_TSEC2
492         SET_STD_TSEC_INFO(tsec_info[num], 2);
493         if (!(gur->pordevsr & MPC85xx_PORDEVSR_SGMII2_DIS))
494                 tsec_info[num].flags |= TSEC_SGMII;
495         num++;
496 #endif
497 #ifdef CONFIG_TSEC3
498         SET_STD_TSEC_INFO(tsec_info[num], 3);
499         if (!(gur->pordevsr & MPC85xx_PORDEVSR_SGMII3_DIS))
500                 tsec_info[num].flags |= TSEC_SGMII;
501         num++;
502 #endif
503
504         if (!num) {
505                 printf("No TSECs initialized\n");
506
507                 return 0;
508         }
509
510 #ifdef CONFIG_FSL_SGMII_RISER
511         fsl_sgmii_riser_init(tsec_info, num);
512 #endif
513
514         tsec_eth_init(bis, tsec_info, num);
515
516         return pci_eth_init(bis);
517 }
518 #endif
519
520 #if defined(CONFIG_OF_BOARD_SETUP)
521 void ft_board_setup(void *blob, bd_t *bd)
522 {
523         phys_addr_t base;
524         phys_size_t size;
525
526         ft_cpu_setup(blob, bd);
527
528         base = getenv_bootm_low();
529         size = getenv_bootm_size();
530
531         fdt_fixup_memory(blob, (u64)base, (u64)size);
532
533 #ifdef CONFIG_PCIE3
534         ft_fsl_pci_setup(blob, "pci0", &pcie3_hose);
535 #endif
536 #ifdef CONFIG_PCIE2
537         ft_fsl_pci_setup(blob, "pci1", &pcie2_hose);
538 #endif
539 #ifdef CONFIG_PCIE1
540         ft_fsl_pci_setup(blob, "pci2", &pcie1_hose);
541 #endif
542 #ifdef CONFIG_FSL_SGMII_RISER
543         fsl_sgmii_riser_fdt_fixup(blob);
544 #endif
545 }
546 #endif
547
548 #ifdef CONFIG_MP
549 void board_lmb_reserve(struct lmb *lmb)
550 {
551         cpu_mp_lmb_reserve(lmb);
552 }
553 #endif