blob: d30ebee021ead6563ae5e1786a841b9bbff20d6d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Simon Glassd21f34e2016-03-11 22:07:26 -07002/*
3 * Copyright (c) 2016 Google, Inc
4 *
5 * From coreboot src/soc/intel/broadwell/romstage/raminit.c
Simon Glassd21f34e2016-03-11 22:07:26 -07006 */
7
Simon Glassaeacddf2023-09-07 09:58:15 -06008#define LOG_CATEGORY UCLASS_RAM
9
Tom Riniabb9a042024-05-18 20:20:43 -060010#include <common.h>
Simon Glassd21f34e2016-03-11 22:07:26 -070011#include <dm.h>
Simon Glass6980b6b2019-11-14 12:57:45 -070012#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glassd21f34e2016-03-11 22:07:26 -070014#include <pci.h>
15#include <syscon.h>
16#include <asm/cpu.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glassd21f34e2016-03-11 22:07:26 -070018#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 Schuchardt51a9aac2023-08-12 20:16:58 +020030phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
Simon Glassd21f34e2016-03-11 22:07:26 -070031{
32 return mrc_common_board_get_usable_ram_top(total_size);
33}
34
Simon Glass2f949c32017-03-31 08:40:32 -060035int dram_init_banksize(void)
Simon Glassd21f34e2016-03-11 22:07:26 -070036{
37 mrc_common_dram_init_banksize();
Simon Glass2f949c32017-03-31 08:40:32 -060038
39 return 0;
Simon Glassd21f34e2016-03-11 22:07:26 -070040}
41
Simon Glassd21f34e2016-03-11 22:07:26 -070042static 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 */
70static 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
84static 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 Glass91efff52019-12-06 21:42:07 -070090 ret = mrccache_get_region(MRC_TYPE_NORMAL, NULL, &entry);
Simon Glassd21f34e2016-03-11 22:07:26 -070091 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 Glassd21f34e2016-03-11 22:07:26 -0700106int 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 Glass345f3662019-04-25 21:58:48 -0600119 if (ret) {
120 debug("Cannot get ME (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700121 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600122 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700123 intel_me_status(me_dev);
124
125 /* Save ME HSIO version */
Simon Glass345f3662019-04-25 21:58:48 -0600126 ret = uclass_first_device_err(UCLASS_PCH, &pch_dev);
127 if (ret) {
128 debug("Cannot get PCH (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700129 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600130 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700131 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 Glass345f3662019-04-25 21:58:48 -0600138 ret = uclass_first_device_err(UCLASS_NORTHBRIDGE, &dev);
139 if (ret) {
140 debug("Cannot get Northbridge (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700141 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600142 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700143 size = 256;
144 ret = mrc_locate_spd(dev, size, &spd_data);
Simon Glass345f3662019-04-25 21:58:48 -0600145 if (ret) {
146 debug("Cannot locate SPD (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700147 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600148 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700149 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 Glass345f3662019-04-25 21:58:48 -0600158 if (ret) {
159 debug("mrc_common_init() failed(err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700160 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600161 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700162 debug("Memory init done\n");
163
164 ret = sdram_find(dev);
Simon Glass345f3662019-04-25 21:58:48 -0600165 if (ret) {
166 debug("sdram_find() failed (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700167 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600168 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700169 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 Glass91efff52019-12-06 21:42:07 -0700176 struct mrc_output *mrc = &gd->arch.mrc[MRC_TYPE_NORMAL];
177
Simon Glassd21f34e2016-03-11 22:07:26 -0700178 /*
179 * This will be copied to SDRAM in reserve_arch(), then written
180 * to SPI flash in mrccache_save()
181 */
Simon Glass91efff52019-12-06 21:42:07 -0700182 mrc->buf = (char *)pei_data->data_to_save;
183 mrc->len = pei_data->data_to_save_size;
Simon Glassd21f34e2016-03-11 22:07:26 -0700184 }
185 gd->arch.pei_meminfo = pei_data->meminfo;
186
187 return 0;
188}
189
190/* Use this hook to save our SDRAM parameters */
191int 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 Glassd21f34e2016-03-11 22:07:26 -0700204static const struct udevice_id broadwell_syscon_ids[] = {
205 { .compatible = "intel,me", .data = X86_SYSCON_ME },
Simon Glassd21f34e2016-03-11 22:07:26 -0700206 { }
207};
208
209U_BOOT_DRIVER(syscon_intel_me) = {
210 .name = "intel_me_syscon",
211 .id = UCLASS_SYSCON,
212 .of_match = broadwell_syscon_ids,
213};