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