blob: 72f59a0a5734402b68ada7c78ef94baf0577571b [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
8#include <common.h>
9#include <dm.h>
Simon Glass6980b6b2019-11-14 12:57:45 -070010#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glassd21f34e2016-03-11 22:07:26 -070012#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
27ulong board_get_usable_ram_top(ulong total_size)
28{
29 return mrc_common_board_get_usable_ram_top(total_size);
30}
31
Simon Glass2f949c32017-03-31 08:40:32 -060032int dram_init_banksize(void)
Simon Glassd21f34e2016-03-11 22:07:26 -070033{
34 mrc_common_dram_init_banksize();
Simon Glass2f949c32017-03-31 08:40:32 -060035
36 return 0;
Simon Glassd21f34e2016-03-11 22:07:26 -070037}
38
Simon Glassd21f34e2016-03-11 22:07:26 -070039static 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 */
67static 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
81static 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 Glass91efff52019-12-06 21:42:07 -070087 ret = mrccache_get_region(MRC_TYPE_NORMAL, NULL, &entry);
Simon Glassd21f34e2016-03-11 22:07:26 -070088 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 Glassd21f34e2016-03-11 22:07:26 -0700103int 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 Glass345f3662019-04-25 21:58:48 -0600116 if (ret) {
117 debug("Cannot get ME (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700118 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600119 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700120 intel_me_status(me_dev);
121
122 /* Save ME HSIO version */
Simon Glass345f3662019-04-25 21:58:48 -0600123 ret = uclass_first_device_err(UCLASS_PCH, &pch_dev);
124 if (ret) {
125 debug("Cannot get PCH (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700126 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600127 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700128 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 Glass345f3662019-04-25 21:58:48 -0600135 ret = uclass_first_device_err(UCLASS_NORTHBRIDGE, &dev);
136 if (ret) {
137 debug("Cannot get Northbridge (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700138 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600139 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700140 size = 256;
141 ret = mrc_locate_spd(dev, size, &spd_data);
Simon Glass345f3662019-04-25 21:58:48 -0600142 if (ret) {
143 debug("Cannot locate SPD (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700144 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600145 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700146 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 Glass345f3662019-04-25 21:58:48 -0600155 if (ret) {
156 debug("mrc_common_init() failed(err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700157 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600158 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700159 debug("Memory init done\n");
160
161 ret = sdram_find(dev);
Simon Glass345f3662019-04-25 21:58:48 -0600162 if (ret) {
163 debug("sdram_find() failed (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700164 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600165 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700166 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 Glass91efff52019-12-06 21:42:07 -0700173 struct mrc_output *mrc = &gd->arch.mrc[MRC_TYPE_NORMAL];
174
Simon Glassd21f34e2016-03-11 22:07:26 -0700175 /*
176 * This will be copied to SDRAM in reserve_arch(), then written
177 * to SPI flash in mrccache_save()
178 */
Simon Glass91efff52019-12-06 21:42:07 -0700179 mrc->buf = (char *)pei_data->data_to_save;
180 mrc->len = pei_data->data_to_save_size;
Simon Glassd21f34e2016-03-11 22:07:26 -0700181 }
182 gd->arch.pei_meminfo = pei_data->meminfo;
183
184 return 0;
185}
186
187/* Use this hook to save our SDRAM parameters */
188int 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 Glassd21f34e2016-03-11 22:07:26 -0700201static const struct udevice_id broadwell_syscon_ids[] = {
202 { .compatible = "intel,me", .data = X86_SYSCON_ME },
Simon Glassd21f34e2016-03-11 22:07:26 -0700203 { }
204};
205
206U_BOOT_DRIVER(syscon_intel_me) = {
207 .name = "intel_me_syscon",
208 .id = UCLASS_SYSCON,
209 .of_match = broadwell_syscon_ids,
210};