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