Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Google, Inc |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <debug_uart.h> |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 8 | #include <malloc.h> |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 9 | #include <spl.h> |
| 10 | #include <asm/cpu.h> |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 11 | #include <asm/mrccache.h> |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 12 | #include <asm/mtrr.h> |
| 13 | #include <asm/processor.h> |
Simon Glass | daade11 | 2019-09-25 08:11:39 -0600 | [diff] [blame^] | 14 | #include <asm/spl.h> |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 15 | #include <asm-generic/sections.h> |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Bin Meng | 8f60ea0 | 2017-01-18 03:32:53 -0800 | [diff] [blame] | 19 | __weak int arch_cpu_init_dm(void) |
| 20 | { |
| 21 | return 0; |
| 22 | } |
| 23 | |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 24 | static int x86_spl_init(void) |
| 25 | { |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 26 | #ifndef CONFIG_TPL |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 27 | /* |
| 28 | * TODO(sjg@chromium.org): We use this area of RAM for the stack |
| 29 | * and global_data in SPL. Once U-Boot starts up and releocates it |
| 30 | * is not needed. We could make this a CONFIG option or perhaps |
| 31 | * place it immediately below CONFIG_SYS_TEXT_BASE. |
| 32 | */ |
| 33 | char *ptr = (char *)0x110000; |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 34 | #endif |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 35 | int ret; |
| 36 | |
| 37 | debug("%s starting\n", __func__); |
| 38 | ret = spl_init(); |
| 39 | if (ret) { |
| 40 | debug("%s: spl_init() failed\n", __func__); |
| 41 | return ret; |
| 42 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 43 | #ifdef CONFIG_TPL |
| 44 | /* Do a mini-init if TPL has already done the full init */ |
| 45 | ret = x86_cpu_reinit_f(); |
| 46 | #else |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 47 | ret = arch_cpu_init(); |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 48 | #endif |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 49 | if (ret) { |
| 50 | debug("%s: arch_cpu_init() failed\n", __func__); |
| 51 | return ret; |
| 52 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 53 | #ifndef CONFIG_TPL |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 54 | ret = arch_cpu_init_dm(); |
| 55 | if (ret) { |
| 56 | debug("%s: arch_cpu_init_dm() failed\n", __func__); |
| 57 | return ret; |
| 58 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 59 | #endif |
Simon Glass | 3ff0900 | 2017-03-19 12:59:21 -0600 | [diff] [blame] | 60 | preloader_console_init(); |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 61 | #ifndef CONFIG_TPL |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 62 | ret = print_cpuinfo(); |
| 63 | if (ret) { |
| 64 | debug("%s: print_cpuinfo() failed\n", __func__); |
| 65 | return ret; |
| 66 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 67 | #endif |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 68 | ret = dram_init(); |
| 69 | if (ret) { |
| 70 | debug("%s: dram_init() failed\n", __func__); |
| 71 | return ret; |
| 72 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 73 | if (IS_ENABLED(CONFIG_ENABLE_MRC_CACHE)) { |
| 74 | ret = mrccache_spl_save(); |
| 75 | if (ret) |
| 76 | debug("%s: Failed to write to mrccache (err=%d)\n", |
| 77 | __func__, ret); |
| 78 | } |
| 79 | |
| 80 | #ifndef CONFIG_TPL |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 81 | memset(&__bss_start, 0, (ulong)&__bss_end - (ulong)&__bss_start); |
| 82 | |
| 83 | /* TODO(sjg@chromium.org): Consider calling cpu_init_r() here */ |
| 84 | ret = interrupt_init(); |
| 85 | if (ret) { |
| 86 | debug("%s: interrupt_init() failed\n", __func__); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * The stack grows down from ptr. Put the global data at ptr. This |
| 92 | * will only be used for SPL. Once SPL loads U-Boot proper it will |
| 93 | * set up its own stack. |
| 94 | */ |
| 95 | gd->new_gd = (struct global_data *)ptr; |
| 96 | memcpy(gd->new_gd, gd, sizeof(*gd)); |
| 97 | arch_setup_gd(gd->new_gd); |
| 98 | gd->start_addr_sp = (ulong)ptr; |
| 99 | |
| 100 | /* Cache the SPI flash. Otherwise copying the code to RAM takes ages */ |
| 101 | ret = mtrr_add_request(MTRR_TYPE_WRBACK, |
| 102 | (1ULL << 32) - CONFIG_XIP_ROM_SIZE, |
| 103 | CONFIG_XIP_ROM_SIZE); |
| 104 | if (ret) { |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 105 | debug("%s: SPI cache setup failed (err=%d)\n", __func__, ret); |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 106 | return ret; |
| 107 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 108 | mtrr_commit(true); |
| 109 | #endif |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | void board_init_f(ulong flags) |
| 115 | { |
| 116 | int ret; |
| 117 | |
| 118 | ret = x86_spl_init(); |
| 119 | if (ret) { |
| 120 | debug("Error %d\n", ret); |
| 121 | hang(); |
| 122 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 123 | #ifdef CONFIG_TPL |
| 124 | gd->bd = malloc(sizeof(*gd->bd)); |
| 125 | if (!gd->bd) { |
| 126 | printf("Out of memory for bd_info size %x\n", sizeof(*gd->bd)); |
| 127 | hang(); |
| 128 | } |
| 129 | board_init_r(gd, 0); |
| 130 | #else |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 131 | /* Uninit CAR and jump to board_init_f_r() */ |
| 132 | board_init_f_r_trampoline(gd->start_addr_sp); |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 133 | #endif |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void board_init_f_r(void) |
| 137 | { |
| 138 | init_cache_f_r(); |
| 139 | gd->flags &= ~GD_FLG_SERIAL_READY; |
| 140 | debug("cache status %d\n", dcache_status()); |
| 141 | board_init_r(gd, 0); |
| 142 | } |
| 143 | |
| 144 | u32 spl_boot_device(void) |
| 145 | { |
Simon Glass | daade11 | 2019-09-25 08:11:39 -0600 | [diff] [blame^] | 146 | return BOOT_DEVICE_SPI_MMAP; |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int spl_start_uboot(void) |
| 150 | { |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | void spl_board_announce_boot_device(void) |
| 155 | { |
| 156 | printf("SPI flash"); |
| 157 | } |
| 158 | |
| 159 | static int spl_board_load_image(struct spl_image_info *spl_image, |
| 160 | struct spl_boot_device *bootdev) |
| 161 | { |
| 162 | spl_image->size = CONFIG_SYS_MONITOR_LEN; |
| 163 | spl_image->entry_point = CONFIG_SYS_TEXT_BASE; |
| 164 | spl_image->load_addr = CONFIG_SYS_TEXT_BASE; |
| 165 | spl_image->os = IH_OS_U_BOOT; |
| 166 | spl_image->name = "U-Boot"; |
| 167 | |
| 168 | debug("Loading to %lx\n", spl_image->load_addr); |
| 169 | |
| 170 | return 0; |
| 171 | } |
Simon Glass | daade11 | 2019-09-25 08:11:39 -0600 | [diff] [blame^] | 172 | SPL_LOAD_IMAGE_METHOD("SPI", 5, BOOT_DEVICE_SPI_MMAP, spl_board_load_image); |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 173 | |
| 174 | int spl_spi_load_image(void) |
| 175 | { |
| 176 | return -EPERM; |
| 177 | } |
| 178 | |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 179 | #ifdef CONFIG_X86_RUN_64BIT |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 180 | void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image) |
| 181 | { |
| 182 | int ret; |
| 183 | |
| 184 | printf("Jumping to 64-bit U-Boot: Note many features are missing\n"); |
| 185 | ret = cpu_jump_to_64bit_uboot(spl_image->entry_point); |
| 186 | debug("ret=%d\n", ret); |
Simon Glass | 14dd93b | 2019-09-25 08:11:38 -0600 | [diff] [blame] | 187 | hang(); |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 188 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 189 | #endif |
| 190 | |
| 191 | void spl_board_init(void) |
| 192 | { |
| 193 | #ifndef CONFIG_TPL |
| 194 | preloader_console_init(); |
| 195 | #endif |
| 196 | } |