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