39e1bee6f361cca5854b897b608ac78e084390dd
[oweals/u-boot.git] / board / freescale / p1022ds / spl.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2013 Freescale Semiconductor, Inc.
4  */
5
6 #include <common.h>
7 #include <clock_legacy.h>
8 #include <console.h>
9 #include <env.h>
10 #include <env_internal.h>
11 #include <init.h>
12 #include <ns16550.h>
13 #include <malloc.h>
14 #include <mmc.h>
15 #include <nand.h>
16 #include <i2c.h>
17 #include "../common/ngpixis.h"
18 #include <fsl_esdhc.h>
19 #include <spi_flash.h>
20 #include "../common/spl.h"
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 static const u32 sysclk_tbl[] = {
25         66666000, 7499900, 83332500, 8999900,
26         99999000, 11111000, 12499800, 13333200
27 };
28
29 phys_size_t get_effective_memsize(void)
30 {
31         return CONFIG_SYS_L2_SIZE;
32 }
33
34 void board_init_f(ulong bootflag)
35 {
36         int px_spd;
37         u32 plat_ratio, sys_clk, bus_clk;
38         ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
39
40         console_init_f();
41
42         /* Set pmuxcr to allow both i2c1 and i2c2 */
43         setbits_be32(&gur->pmuxcr, in_be32(&gur->pmuxcr) | 0x1000);
44         setbits_be32(&gur->pmuxcr,
45                      in_be32(&gur->pmuxcr) | MPC85xx_PMUXCR_SD_DATA);
46
47 #ifdef CONFIG_SPL_SPI_BOOT
48         /* Enable the SPI */
49         clrsetbits_8(&pixis->brdcfg0, PIXIS_ELBC_SPI_MASK, PIXIS_SPI);
50 #endif
51
52         /* Read back the register to synchronize the write. */
53         in_be32(&gur->pmuxcr);
54
55         /* initialize selected port with appropriate baud rate */
56         px_spd = in_8((unsigned char *)(PIXIS_BASE + PIXIS_SPD));
57         sys_clk = sysclk_tbl[px_spd & PIXIS_SPD_SYSCLK_MASK];
58         plat_ratio = in_be32(&gur->porpllsr) & MPC85xx_PORPLLSR_PLAT_RATIO;
59         bus_clk = sys_clk * plat_ratio / 2;
60
61         NS16550_init((NS16550_t)CONFIG_SYS_NS16550_COM1,
62                      bus_clk / 16 / CONFIG_BAUDRATE);
63 #ifdef CONFIG_SPL_MMC_BOOT
64         puts("\nSD boot...\n");
65 #elif defined(CONFIG_SPL_SPI_BOOT)
66         puts("\nSPI Flash boot...\n");
67 #endif
68
69         /* copy code to RAM and jump to it - this should not return */
70         /* NOTE - code has to be copied out of NAND buffer before
71          * other blocks can be read.
72          */
73         relocate_code(CONFIG_SPL_RELOC_STACK, 0, CONFIG_SPL_RELOC_TEXT_BASE);
74 }
75
76 void board_init_r(gd_t *gd, ulong dest_addr)
77 {
78         /* Pointer is writable since we allocated a register for it */
79         gd = (gd_t *)CONFIG_SPL_GD_ADDR;
80         bd_t *bd;
81
82         memset(gd, 0, sizeof(gd_t));
83         bd = (bd_t *)(CONFIG_SPL_GD_ADDR + sizeof(gd_t));
84         memset(bd, 0, sizeof(bd_t));
85         gd->bd = bd;
86         bd->bi_memstart = CONFIG_SYS_INIT_L2_ADDR;
87         bd->bi_memsize = CONFIG_SYS_L2_SIZE;
88
89         arch_cpu_init();
90         get_clocks();
91         mem_malloc_init(CONFIG_SPL_RELOC_MALLOC_ADDR,
92                         CONFIG_SPL_RELOC_MALLOC_SIZE);
93         gd->flags |= GD_FLG_FULL_MALLOC_INIT;
94 #ifndef CONFIG_SPL_NAND_BOOT
95         env_init();
96 #endif
97 #ifdef CONFIG_SPL_MMC_BOOT
98         mmc_initialize(bd);
99 #endif
100         /* relocate environment function pointers etc. */
101 #ifdef CONFIG_SPL_NAND_BOOT
102         nand_spl_load_image(CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
103                             (uchar *)SPL_ENV_ADDR);
104
105         gd->env_addr  = (ulong)(SPL_ENV_ADDR);
106         gd->env_valid = ENV_VALID;
107 #else
108         env_relocate();
109 #endif
110
111 #ifdef CONFIG_SYS_I2C
112         i2c_init_all();
113 #else
114         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
115 #endif
116
117         dram_init();
118 #ifdef CONFIG_SPL_NAND_BOOT
119         puts("Tertiary program loader running in sram...");
120 #else
121         puts("Second program loader running in sram...\n");
122 #endif
123
124 #ifdef CONFIG_SPL_MMC_BOOT
125         mmc_boot();
126 #elif defined(CONFIG_SPL_SPI_BOOT)
127         fsl_spi_boot();
128 #elif defined(CONFIG_SPL_NAND_BOOT)
129         nand_boot();
130 #endif
131 }