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 | c0e2c81 | 2019-10-20 21:37:49 -0600 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 9 | #include <malloc.h> |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 10 | #include <spl.h> |
Simon Glass | c0e2c81 | 2019-10-20 21:37:49 -0600 | [diff] [blame] | 11 | #include <syscon.h> |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 12 | #include <asm/cpu.h> |
Simon Glass | c0e2c81 | 2019-10-20 21:37:49 -0600 | [diff] [blame] | 13 | #include <asm/cpu_common.h> |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 14 | #include <asm/mrccache.h> |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 15 | #include <asm/mtrr.h> |
Simon Glass | c0e2c81 | 2019-10-20 21:37:49 -0600 | [diff] [blame] | 16 | #include <asm/pci.h> |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 17 | #include <asm/processor.h> |
Simon Glass | daade11 | 2019-09-25 08:11:39 -0600 | [diff] [blame] | 18 | #include <asm/spl.h> |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 19 | #include <asm-generic/sections.h> |
| 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Bin Meng | 8f60ea0 | 2017-01-18 03:32:53 -0800 | [diff] [blame] | 23 | __weak int arch_cpu_init_dm(void) |
| 24 | { |
| 25 | return 0; |
| 26 | } |
| 27 | |
Simon Glass | c0e2c81 | 2019-10-20 21:37:49 -0600 | [diff] [blame] | 28 | #ifdef CONFIG_TPL |
| 29 | |
| 30 | static int set_max_freq(void) |
| 31 | { |
| 32 | if (cpu_get_burst_mode_state() == BURST_MODE_UNAVAILABLE) { |
| 33 | /* |
| 34 | * Burst Mode has been factory-configured as disabled and is not |
| 35 | * available in this physical processor package |
| 36 | */ |
| 37 | debug("Burst Mode is factory-disabled\n"); |
| 38 | return -ENOENT; |
| 39 | } |
| 40 | |
| 41 | /* Enable burst mode */ |
| 42 | cpu_set_burst_mode(true); |
| 43 | |
| 44 | /* Enable speed step */ |
| 45 | cpu_set_eist(true); |
| 46 | |
| 47 | /* Set P-State ratio */ |
| 48 | cpu_set_p_state_to_turbo_ratio(); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | #endif |
| 53 | |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 54 | static int x86_spl_init(void) |
| 55 | { |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 56 | #ifndef CONFIG_TPL |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 57 | /* |
| 58 | * TODO(sjg@chromium.org): We use this area of RAM for the stack |
| 59 | * and global_data in SPL. Once U-Boot starts up and releocates it |
| 60 | * is not needed. We could make this a CONFIG option or perhaps |
| 61 | * place it immediately below CONFIG_SYS_TEXT_BASE. |
| 62 | */ |
| 63 | char *ptr = (char *)0x110000; |
Simon Glass | c0e2c81 | 2019-10-20 21:37:49 -0600 | [diff] [blame] | 64 | #else |
| 65 | struct udevice *punit; |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 66 | #endif |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 67 | int ret; |
| 68 | |
| 69 | debug("%s starting\n", __func__); |
Simon Glass | 0e72ac7 | 2019-10-20 21:37:55 -0600 | [diff] [blame] | 70 | if (IS_ENABLED(TPL)) |
| 71 | ret = x86_cpu_reinit_f(); |
| 72 | else |
| 73 | ret = x86_cpu_init_f(); |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 74 | ret = spl_init(); |
| 75 | if (ret) { |
| 76 | debug("%s: spl_init() failed\n", __func__); |
| 77 | return ret; |
| 78 | } |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 79 | ret = arch_cpu_init(); |
| 80 | if (ret) { |
| 81 | debug("%s: arch_cpu_init() failed\n", __func__); |
| 82 | return ret; |
| 83 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 84 | #ifndef CONFIG_TPL |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 85 | ret = arch_cpu_init_dm(); |
| 86 | if (ret) { |
| 87 | debug("%s: arch_cpu_init_dm() failed\n", __func__); |
| 88 | return ret; |
| 89 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 90 | #endif |
Simon Glass | 3ff0900 | 2017-03-19 12:59:21 -0600 | [diff] [blame] | 91 | preloader_console_init(); |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 92 | #ifndef CONFIG_TPL |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 93 | ret = print_cpuinfo(); |
| 94 | if (ret) { |
| 95 | debug("%s: print_cpuinfo() failed\n", __func__); |
| 96 | return ret; |
| 97 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 98 | #endif |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 99 | ret = dram_init(); |
| 100 | if (ret) { |
| 101 | debug("%s: dram_init() failed\n", __func__); |
| 102 | return ret; |
| 103 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 104 | if (IS_ENABLED(CONFIG_ENABLE_MRC_CACHE)) { |
| 105 | ret = mrccache_spl_save(); |
| 106 | if (ret) |
| 107 | debug("%s: Failed to write to mrccache (err=%d)\n", |
| 108 | __func__, ret); |
| 109 | } |
| 110 | |
| 111 | #ifndef CONFIG_TPL |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 112 | memset(&__bss_start, 0, (ulong)&__bss_end - (ulong)&__bss_start); |
| 113 | |
| 114 | /* TODO(sjg@chromium.org): Consider calling cpu_init_r() here */ |
| 115 | ret = interrupt_init(); |
| 116 | if (ret) { |
| 117 | debug("%s: interrupt_init() failed\n", __func__); |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * The stack grows down from ptr. Put the global data at ptr. This |
| 123 | * will only be used for SPL. Once SPL loads U-Boot proper it will |
| 124 | * set up its own stack. |
| 125 | */ |
| 126 | gd->new_gd = (struct global_data *)ptr; |
| 127 | memcpy(gd->new_gd, gd, sizeof(*gd)); |
| 128 | arch_setup_gd(gd->new_gd); |
| 129 | gd->start_addr_sp = (ulong)ptr; |
| 130 | |
| 131 | /* Cache the SPI flash. Otherwise copying the code to RAM takes ages */ |
| 132 | ret = mtrr_add_request(MTRR_TYPE_WRBACK, |
| 133 | (1ULL << 32) - CONFIG_XIP_ROM_SIZE, |
| 134 | CONFIG_XIP_ROM_SIZE); |
| 135 | if (ret) { |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 136 | debug("%s: SPI cache setup failed (err=%d)\n", __func__, ret); |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 137 | return ret; |
| 138 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 139 | mtrr_commit(true); |
Simon Glass | c0e2c81 | 2019-10-20 21:37:49 -0600 | [diff] [blame] | 140 | #else |
| 141 | ret = syscon_get_by_driver_data(X86_SYSCON_PUNIT, &punit); |
| 142 | if (ret) |
| 143 | debug("Could not find PUNIT (err=%d)\n", ret); |
| 144 | |
| 145 | ret = set_max_freq(); |
| 146 | if (ret) |
| 147 | debug("Failed to set CPU frequency (err=%d)\n", ret); |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 148 | #endif |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | void board_init_f(ulong flags) |
| 154 | { |
| 155 | int ret; |
| 156 | |
| 157 | ret = x86_spl_init(); |
| 158 | if (ret) { |
| 159 | debug("Error %d\n", ret); |
Simon Glass | 3d95688 | 2019-09-25 08:56:51 -0600 | [diff] [blame] | 160 | panic("x86_spl_init fail"); |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 161 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 162 | #ifdef CONFIG_TPL |
| 163 | gd->bd = malloc(sizeof(*gd->bd)); |
| 164 | if (!gd->bd) { |
| 165 | printf("Out of memory for bd_info size %x\n", sizeof(*gd->bd)); |
| 166 | hang(); |
| 167 | } |
| 168 | board_init_r(gd, 0); |
| 169 | #else |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 170 | /* Uninit CAR and jump to board_init_f_r() */ |
| 171 | board_init_f_r_trampoline(gd->start_addr_sp); |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 172 | #endif |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void board_init_f_r(void) |
| 176 | { |
| 177 | init_cache_f_r(); |
| 178 | gd->flags &= ~GD_FLG_SERIAL_READY; |
| 179 | debug("cache status %d\n", dcache_status()); |
| 180 | board_init_r(gd, 0); |
| 181 | } |
| 182 | |
| 183 | u32 spl_boot_device(void) |
| 184 | { |
Simon Glass | daade11 | 2019-09-25 08:11:39 -0600 | [diff] [blame] | 185 | return BOOT_DEVICE_SPI_MMAP; |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | int spl_start_uboot(void) |
| 189 | { |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | void spl_board_announce_boot_device(void) |
| 194 | { |
| 195 | printf("SPI flash"); |
| 196 | } |
| 197 | |
| 198 | static int spl_board_load_image(struct spl_image_info *spl_image, |
| 199 | struct spl_boot_device *bootdev) |
| 200 | { |
| 201 | spl_image->size = CONFIG_SYS_MONITOR_LEN; |
| 202 | spl_image->entry_point = CONFIG_SYS_TEXT_BASE; |
| 203 | spl_image->load_addr = CONFIG_SYS_TEXT_BASE; |
| 204 | spl_image->os = IH_OS_U_BOOT; |
| 205 | spl_image->name = "U-Boot"; |
| 206 | |
| 207 | debug("Loading to %lx\n", spl_image->load_addr); |
| 208 | |
| 209 | return 0; |
| 210 | } |
Simon Glass | daade11 | 2019-09-25 08:11:39 -0600 | [diff] [blame] | 211 | 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] | 212 | |
| 213 | int spl_spi_load_image(void) |
| 214 | { |
| 215 | return -EPERM; |
| 216 | } |
| 217 | |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 218 | #ifdef CONFIG_X86_RUN_64BIT |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 219 | void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image) |
| 220 | { |
| 221 | int ret; |
| 222 | |
| 223 | printf("Jumping to 64-bit U-Boot: Note many features are missing\n"); |
| 224 | ret = cpu_jump_to_64bit_uboot(spl_image->entry_point); |
| 225 | debug("ret=%d\n", ret); |
Simon Glass | 14dd93b | 2019-09-25 08:11:38 -0600 | [diff] [blame] | 226 | hang(); |
Simon Glass | 4bbc024 | 2017-01-16 07:03:56 -0700 | [diff] [blame] | 227 | } |
Simon Glass | 7c03caf | 2019-05-02 10:52:12 -0600 | [diff] [blame] | 228 | #endif |
| 229 | |
| 230 | void spl_board_init(void) |
| 231 | { |
| 232 | #ifndef CONFIG_TPL |
| 233 | preloader_console_init(); |
| 234 | #endif |
| 235 | } |