blob: cd534a17cf15943b48a24d1f989002dd4bdb8477 [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
Simon Glassd21f34e2016-03-11 22:07:26 -070010#include <dm.h>
Simon Glass6980b6b2019-11-14 12:57:45 -070011#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassd21f34e2016-03-11 22:07:26 -070013#include <pci.h>
14#include <syscon.h>
15#include <asm/cpu.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glassd21f34e2016-03-11 22:07:26 -070017#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 Schuchardt51a9aac2023-08-12 20:16:58 +020029phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
Simon Glassd21f34e2016-03-11 22:07:26 -070030{
31 return mrc_common_board_get_usable_ram_top(total_size);
32}
33
Simon Glass2f949c32017-03-31 08:40:32 -060034int dram_init_banksize(void)
Simon Glassd21f34e2016-03-11 22:07:26 -070035{
36 mrc_common_dram_init_banksize();
Simon Glass2f949c32017-03-31 08:40:32 -060037
38 return 0;
Simon Glassd21f34e2016-03-11 22:07:26 -070039}
40
Simon Glassd21f34e2016-03-11 22:07:26 -070041static 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 */
69static 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
83static 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 Glass91efff52019-12-06 21:42:07 -070089 ret = mrccache_get_region(MRC_TYPE_NORMAL, NULL, &entry);
Simon Glassd21f34e2016-03-11 22:07:26 -070090 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 Glassd21f34e2016-03-11 22:07:26 -0700105int 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 Glass345f3662019-04-25 21:58:48 -0600118 if (ret) {
119 debug("Cannot get ME (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700120 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600121 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700122 intel_me_status(me_dev);
123
124 /* Save ME HSIO version */
Simon Glass345f3662019-04-25 21:58:48 -0600125 ret = uclass_first_device_err(UCLASS_PCH, &pch_dev);
126 if (ret) {
127 debug("Cannot get PCH (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700128 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600129 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700130 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 Glass345f3662019-04-25 21:58:48 -0600137 ret = uclass_first_device_err(UCLASS_NORTHBRIDGE, &dev);
138 if (ret) {
139 debug("Cannot get Northbridge (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700140 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600141 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700142 size = 256;
143 ret = mrc_locate_spd(dev, size, &spd_data);
Simon Glass345f3662019-04-25 21:58:48 -0600144 if (ret) {
145 debug("Cannot locate SPD (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700146 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600147 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700148 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 Glass345f3662019-04-25 21:58:48 -0600157 if (ret) {
158 debug("mrc_common_init() failed(err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700159 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600160 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700161 debug("Memory init done\n");
162
163 ret = sdram_find(dev);
Simon Glass345f3662019-04-25 21:58:48 -0600164 if (ret) {
165 debug("sdram_find() failed (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700166 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600167 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700168 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 Glass91efff52019-12-06 21:42:07 -0700175 struct mrc_output *mrc = &gd->arch.mrc[MRC_TYPE_NORMAL];
176
Simon Glassd21f34e2016-03-11 22:07:26 -0700177 /*
178 * This will be copied to SDRAM in reserve_arch(), then written
179 * to SPI flash in mrccache_save()
180 */
Simon Glass91efff52019-12-06 21:42:07 -0700181 mrc->buf = (char *)pei_data->data_to_save;
182 mrc->len = pei_data->data_to_save_size;
Simon Glassd21f34e2016-03-11 22:07:26 -0700183 }
184 gd->arch.pei_meminfo = pei_data->meminfo;
185
186 return 0;
187}
188
189/* Use this hook to save our SDRAM parameters */
190int 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 Glassd21f34e2016-03-11 22:07:26 -0700203static const struct udevice_id broadwell_syscon_ids[] = {
204 { .compatible = "intel,me", .data = X86_SYSCON_ME },
Simon Glassd21f34e2016-03-11 22:07:26 -0700205 { }
206};
207
208U_BOOT_DRIVER(syscon_intel_me) = {
209 .name = "intel_me_syscon",
210 .id = UCLASS_SYSCON,
211 .of_match = broadwell_syscon_ids,
212};