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