Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Google, Inc |
| 4 | * |
| 5 | * From coreboot src/soc/intel/broadwell/romstage/raminit.c |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | aeacddf | 2023-09-07 09:58:15 -0600 | [diff] [blame] | 8 | #define LOG_CATEGORY UCLASS_RAM |
| 9 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | 6980b6b | 2019-11-14 12:57:45 -0700 | [diff] [blame] | 12 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 14 | #include <pci.h> |
| 15 | #include <syscon.h> |
| 16 | #include <asm/cpu.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 18 | #include <asm/io.h> |
| 19 | #include <asm/lpc_common.h> |
| 20 | #include <asm/mrccache.h> |
| 21 | #include <asm/mrc_common.h> |
| 22 | #include <asm/mtrr.h> |
| 23 | #include <asm/pci.h> |
| 24 | #include <asm/arch/iomap.h> |
| 25 | #include <asm/arch/me.h> |
| 26 | #include <asm/arch/pch.h> |
| 27 | #include <asm/arch/pei_data.h> |
| 28 | #include <asm/arch/pm.h> |
| 29 | |
Heinrich Schuchardt | 51a9aac | 2023-08-12 20:16:58 +0200 | [diff] [blame] | 30 | phys_addr_t board_get_usable_ram_top(phys_size_t total_size) |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 31 | { |
| 32 | return mrc_common_board_get_usable_ram_top(total_size); |
| 33 | } |
| 34 | |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 35 | int dram_init_banksize(void) |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 36 | { |
| 37 | mrc_common_dram_init_banksize(); |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 38 | |
| 39 | return 0; |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 42 | static unsigned long get_top_of_ram(struct udevice *dev) |
| 43 | { |
| 44 | /* |
| 45 | * Base of DPR is top of usable DRAM below 4GiB. The register has |
| 46 | * 1 MiB alignment and reports the TOP of the range, the base |
| 47 | * must be calculated from the size in MiB in bits 11:4. |
| 48 | */ |
| 49 | u32 dpr, tom; |
| 50 | |
| 51 | dm_pci_read_config32(dev, DPR, &dpr); |
| 52 | tom = dpr & ~((1 << 20) - 1); |
| 53 | |
| 54 | debug("dpt %08x tom %08x\n", dpr, tom); |
| 55 | /* Subtract DMA Protected Range size if enabled */ |
| 56 | if (dpr & DPR_EPM) |
| 57 | tom -= (dpr & DPR_SIZE_MASK) << 16; |
| 58 | |
| 59 | return (unsigned long)tom; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * sdram_find() - Find available memory |
| 64 | * |
| 65 | * This is a bit complicated since on x86 there are system memory holes all |
| 66 | * over the place. We create a list of available memory blocks |
| 67 | * |
| 68 | * @dev: Northbridge device |
| 69 | */ |
| 70 | static int sdram_find(struct udevice *dev) |
| 71 | { |
| 72 | struct memory_info *info = &gd->arch.meminfo; |
| 73 | ulong top_of_ram; |
| 74 | |
| 75 | top_of_ram = get_top_of_ram(dev); |
| 76 | mrc_add_memory_area(info, 0, top_of_ram); |
| 77 | |
| 78 | /* Add MTRRs for memory */ |
| 79 | mtrr_add_request(MTRR_TYPE_WRBACK, 0, 2ULL << 30); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int prepare_mrc_cache(struct pei_data *pei_data) |
| 85 | { |
| 86 | struct mrc_data_container *mrc_cache; |
| 87 | struct mrc_region entry; |
| 88 | int ret; |
| 89 | |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 90 | ret = mrccache_get_region(MRC_TYPE_NORMAL, NULL, &entry); |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 91 | if (ret) |
| 92 | return ret; |
| 93 | mrc_cache = mrccache_find_current(&entry); |
| 94 | if (!mrc_cache) |
| 95 | return -ENOENT; |
| 96 | |
| 97 | pei_data->saved_data = mrc_cache->data; |
| 98 | pei_data->saved_data_size = mrc_cache->data_size; |
| 99 | debug("%s: at %p, size %x checksum %04x\n", __func__, |
| 100 | pei_data->saved_data, pei_data->saved_data_size, |
| 101 | mrc_cache->checksum); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 106 | int dram_init(void) |
| 107 | { |
| 108 | struct pei_data _pei_data __aligned(8); |
| 109 | struct pei_data *pei_data = &_pei_data; |
| 110 | struct udevice *dev, *me_dev, *pch_dev; |
| 111 | struct chipset_power_state ps; |
| 112 | const void *spd_data; |
| 113 | int ret, size; |
| 114 | |
| 115 | memset(pei_data, '\0', sizeof(struct pei_data)); |
| 116 | |
| 117 | /* Print ME state before MRC */ |
| 118 | ret = syscon_get_by_driver_data(X86_SYSCON_ME, &me_dev); |
Simon Glass | 345f366 | 2019-04-25 21:58:48 -0600 | [diff] [blame] | 119 | if (ret) { |
| 120 | debug("Cannot get ME (err=%d)\n", ret); |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 121 | return ret; |
Simon Glass | 345f366 | 2019-04-25 21:58:48 -0600 | [diff] [blame] | 122 | } |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 123 | intel_me_status(me_dev); |
| 124 | |
| 125 | /* Save ME HSIO version */ |
Simon Glass | 345f366 | 2019-04-25 21:58:48 -0600 | [diff] [blame] | 126 | ret = uclass_first_device_err(UCLASS_PCH, &pch_dev); |
| 127 | if (ret) { |
| 128 | debug("Cannot get PCH (err=%d)\n", ret); |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 129 | return ret; |
Simon Glass | 345f366 | 2019-04-25 21:58:48 -0600 | [diff] [blame] | 130 | } |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 131 | power_state_get(pch_dev, &ps); |
| 132 | |
| 133 | intel_me_hsio_version(me_dev, &ps.hsio_version, &ps.hsio_checksum); |
| 134 | |
| 135 | broadwell_fill_pei_data(pei_data); |
| 136 | mainboard_fill_pei_data(pei_data); |
| 137 | |
Simon Glass | 345f366 | 2019-04-25 21:58:48 -0600 | [diff] [blame] | 138 | ret = uclass_first_device_err(UCLASS_NORTHBRIDGE, &dev); |
| 139 | if (ret) { |
| 140 | debug("Cannot get Northbridge (err=%d)\n", ret); |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 141 | return ret; |
Simon Glass | 345f366 | 2019-04-25 21:58:48 -0600 | [diff] [blame] | 142 | } |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 143 | size = 256; |
| 144 | ret = mrc_locate_spd(dev, size, &spd_data); |
Simon Glass | 345f366 | 2019-04-25 21:58:48 -0600 | [diff] [blame] | 145 | if (ret) { |
| 146 | debug("Cannot locate SPD (err=%d)\n", ret); |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 147 | return ret; |
Simon Glass | 345f366 | 2019-04-25 21:58:48 -0600 | [diff] [blame] | 148 | } |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 149 | memcpy(pei_data->spd_data[0][0], spd_data, size); |
| 150 | memcpy(pei_data->spd_data[1][0], spd_data, size); |
| 151 | |
| 152 | ret = prepare_mrc_cache(pei_data); |
| 153 | if (ret) |
| 154 | debug("prepare_mrc_cache failed: %d\n", ret); |
| 155 | |
| 156 | debug("PEI version %#x\n", pei_data->pei_version); |
| 157 | ret = mrc_common_init(dev, pei_data, true); |
Simon Glass | 345f366 | 2019-04-25 21:58:48 -0600 | [diff] [blame] | 158 | if (ret) { |
| 159 | debug("mrc_common_init() failed(err=%d)\n", ret); |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 160 | return ret; |
Simon Glass | 345f366 | 2019-04-25 21:58:48 -0600 | [diff] [blame] | 161 | } |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 162 | debug("Memory init done\n"); |
| 163 | |
| 164 | ret = sdram_find(dev); |
Simon Glass | 345f366 | 2019-04-25 21:58:48 -0600 | [diff] [blame] | 165 | if (ret) { |
| 166 | debug("sdram_find() failed (err=%d)\n", ret); |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 167 | return ret; |
Simon Glass | 345f366 | 2019-04-25 21:58:48 -0600 | [diff] [blame] | 168 | } |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 169 | gd->ram_size = gd->arch.meminfo.total_32bit_memory; |
| 170 | debug("RAM size %llx\n", (unsigned long long)gd->ram_size); |
| 171 | |
| 172 | debug("MRC output data length %#x at %p\n", pei_data->data_to_save_size, |
| 173 | pei_data->data_to_save); |
| 174 | /* S3 resume: don't save scrambler seed or MRC data */ |
| 175 | if (pei_data->boot_mode != SLEEP_STATE_S3) { |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 176 | struct mrc_output *mrc = &gd->arch.mrc[MRC_TYPE_NORMAL]; |
| 177 | |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 178 | /* |
| 179 | * This will be copied to SDRAM in reserve_arch(), then written |
| 180 | * to SPI flash in mrccache_save() |
| 181 | */ |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 182 | mrc->buf = (char *)pei_data->data_to_save; |
| 183 | mrc->len = pei_data->data_to_save_size; |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 184 | } |
| 185 | gd->arch.pei_meminfo = pei_data->meminfo; |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* Use this hook to save our SDRAM parameters */ |
| 191 | int misc_init_r(void) |
| 192 | { |
| 193 | int ret; |
| 194 | |
| 195 | ret = mrccache_save(); |
| 196 | if (ret) |
| 197 | printf("Unable to save MRC data: %d\n", ret); |
| 198 | else |
| 199 | debug("Saved MRC cache data\n"); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 204 | static const struct udevice_id broadwell_syscon_ids[] = { |
| 205 | { .compatible = "intel,me", .data = X86_SYSCON_ME }, |
Simon Glass | d21f34e | 2016-03-11 22:07:26 -0700 | [diff] [blame] | 206 | { } |
| 207 | }; |
| 208 | |
| 209 | U_BOOT_DRIVER(syscon_intel_me) = { |
| 210 | .name = "intel_me_syscon", |
| 211 | .id = UCLASS_SYSCON, |
| 212 | .of_match = broadwell_syscon_ids, |
| 213 | }; |