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