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