Merge tag 'efi-2020-07-rc3' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / arch / arm / cpu / arm926ejs / mxs / spl_boot.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Freescale i.MX28 Boot setup
4  *
5  * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
6  * on behalf of DENX Software Engineering GmbH
7  */
8
9 #include <common.h>
10 #include <config.h>
11 #include <serial.h>
12 #include <asm/io.h>
13 #include <asm/arch/imx-regs.h>
14 #include <asm/arch/sys_proto.h>
15 #include <asm/gpio.h>
16 #include <linux/compiler.h>
17
18 #include "mxs_init.h"
19
20 DECLARE_GLOBAL_DATA_PTR;
21 static gd_t gdata __section(".data");
22 #ifdef CONFIG_SPL_SERIAL_SUPPORT
23 static bd_t bdata __section(".data");
24 #endif
25
26 /*
27  * This delay function is intended to be used only in early stage of boot, where
28  * clock are not set up yet.
29  */
30 void early_delay(int delay)
31 {
32         struct mxs_digctl_regs *digctl_regs =
33                 (struct mxs_digctl_regs *)MXS_DIGCTL_BASE;
34
35         uint32_t st = readl(&digctl_regs->hw_digctl_microseconds);
36         while (readl(&digctl_regs->hw_digctl_microseconds) - st <= delay)
37                 ;
38 }
39
40 #if defined(CONFIG_MX23)
41 #define MUX_CONFIG_BOOTMODE_PAD (MXS_PAD_3V3 | MXS_PAD_4MA | MXS_PAD_NOPULL)
42 static const iomux_cfg_t iomux_boot[] = {
43         MX23_PAD_LCD_D00__GPIO_1_0 | MUX_CONFIG_BOOTMODE_PAD,
44         MX23_PAD_LCD_D01__GPIO_1_1 | MUX_CONFIG_BOOTMODE_PAD,
45         MX23_PAD_LCD_D02__GPIO_1_2 | MUX_CONFIG_BOOTMODE_PAD,
46         MX23_PAD_LCD_D03__GPIO_1_3 | MUX_CONFIG_BOOTMODE_PAD,
47         MX23_PAD_LCD_D04__GPIO_1_4 | MUX_CONFIG_BOOTMODE_PAD,
48         MX23_PAD_LCD_D05__GPIO_1_5 | MUX_CONFIG_BOOTMODE_PAD,
49 };
50 #endif
51
52 static uint8_t mxs_get_bootmode_index(void)
53 {
54         uint8_t bootmode = 0;
55         int i;
56         uint8_t masked;
57
58 #if defined(CONFIG_MX23)
59         /* Setup IOMUX of bootmode pads to GPIO */
60         mxs_iomux_setup_multiple_pads(iomux_boot, ARRAY_SIZE(iomux_boot));
61
62         /* Setup bootmode pins as GPIO input */
63         gpio_direction_input(MX23_PAD_LCD_D00__GPIO_1_0);
64         gpio_direction_input(MX23_PAD_LCD_D01__GPIO_1_1);
65         gpio_direction_input(MX23_PAD_LCD_D02__GPIO_1_2);
66         gpio_direction_input(MX23_PAD_LCD_D03__GPIO_1_3);
67         gpio_direction_input(MX23_PAD_LCD_D05__GPIO_1_5);
68
69         /* Read bootmode pads */
70         bootmode |= (gpio_get_value(MX23_PAD_LCD_D00__GPIO_1_0) ? 1 : 0) << 0;
71         bootmode |= (gpio_get_value(MX23_PAD_LCD_D01__GPIO_1_1) ? 1 : 0) << 1;
72         bootmode |= (gpio_get_value(MX23_PAD_LCD_D02__GPIO_1_2) ? 1 : 0) << 2;
73         bootmode |= (gpio_get_value(MX23_PAD_LCD_D03__GPIO_1_3) ? 1 : 0) << 3;
74         bootmode |= (gpio_get_value(MX23_PAD_LCD_D05__GPIO_1_5) ? 1 : 0) << 5;
75 #elif defined(CONFIG_MX28)
76         /* The global boot mode will be detected by ROM code and its value
77          * is stored at the fixed address 0x00019BF0 in OCRAM.
78          */
79 #define GLOBAL_BOOT_MODE_ADDR 0x00019BF0
80         bootmode = __raw_readl(GLOBAL_BOOT_MODE_ADDR);
81 #endif
82
83         for (i = 0; i < ARRAY_SIZE(mxs_boot_modes); i++) {
84                 masked = bootmode & mxs_boot_modes[i].boot_mask;
85                 if (masked == mxs_boot_modes[i].boot_pads)
86                         break;
87         }
88
89         return i;
90 }
91
92 static void mxs_spl_fixup_vectors(void)
93 {
94         /*
95          * Copy our vector table to 0x0, since due to HAB, we cannot
96          * be loaded to 0x0. We want to have working vectoring though,
97          * thus this fixup. Our vectoring table is PIC, so copying is
98          * fine.
99          */
100         extern uint32_t _start;
101
102         /* cppcheck-suppress nullPointer */
103         memcpy(0x0, &_start, 0x60);
104 }
105
106 static void mxs_spl_console_init(void)
107 {
108 #ifdef CONFIG_SPL_SERIAL_SUPPORT
109         gd->bd = &bdata;
110         gd->baudrate = CONFIG_BAUDRATE;
111         serial_init();
112         gd->have_console = 1;
113 #endif
114 }
115
116 void mxs_common_spl_init(const uint32_t arg, const uint32_t *resptr,
117                          const iomux_cfg_t *iomux_setup,
118                          const unsigned int iomux_size)
119 {
120         struct mxs_spl_data *data = MXS_SPL_DATA;
121         uint8_t bootmode = mxs_get_bootmode_index();
122         gd = &gdata;
123
124         mxs_spl_fixup_vectors();
125
126         mxs_iomux_setup_multiple_pads(iomux_setup, iomux_size);
127
128         mxs_spl_console_init();
129         debug("SPL: Serial Console Initialised\n");
130
131         mxs_power_init();
132
133         mxs_mem_init();
134         data->mem_dram_size = mxs_mem_get_size();
135
136         data->boot_mode_idx = bootmode;
137
138         mxs_power_wait_pswitch();
139
140         if (mxs_boot_modes[data->boot_mode_idx].boot_pads == MXS_BM_JTAG) {
141                 debug("SPL: Waiting for JTAG user\n");
142                 asm volatile ("x: b x");
143         }
144 }
145
146 #ifndef CONFIG_SPL_FRAMEWORK
147 /* Support aparatus */
148 inline void board_init_f(unsigned long bootflag)
149 {
150         for (;;)
151                 ;
152 }
153
154 inline void board_init_r(gd_t *id, ulong dest_addr)
155 {
156         for (;;)
157                 ;
158 }
159 #endif