blob: 15bfc5811cb4dc397ec129ff3745982c3ce7df7a [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 Glassd21f34e2016-03-11 22:07:26 -070011#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
26ulong board_get_usable_ram_top(ulong total_size)
27{
28 return mrc_common_board_get_usable_ram_top(total_size);
29}
30
Simon Glass2f949c32017-03-31 08:40:32 -060031int dram_init_banksize(void)
Simon Glassd21f34e2016-03-11 22:07:26 -070032{
33 mrc_common_dram_init_banksize();
Simon Glass2f949c32017-03-31 08:40:32 -060034
35 return 0;
Simon Glassd21f34e2016-03-11 22:07:26 -070036}
37
Simon Glassd21f34e2016-03-11 22:07:26 -070038static 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 */
66static 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
80static 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 Glass91efff52019-12-06 21:42:07 -070086 ret = mrccache_get_region(MRC_TYPE_NORMAL, NULL, &entry);
Simon Glassd21f34e2016-03-11 22:07:26 -070087 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 Glassd21f34e2016-03-11 22:07:26 -0700102int 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 Glass345f3662019-04-25 21:58:48 -0600115 if (ret) {
116 debug("Cannot get ME (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700117 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600118 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700119 intel_me_status(me_dev);
120
121 /* Save ME HSIO version */
Simon Glass345f3662019-04-25 21:58:48 -0600122 ret = uclass_first_device_err(UCLASS_PCH, &pch_dev);
123 if (ret) {
124 debug("Cannot get PCH (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700125 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600126 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700127 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 Glass345f3662019-04-25 21:58:48 -0600134 ret = uclass_first_device_err(UCLASS_NORTHBRIDGE, &dev);
135 if (ret) {
136 debug("Cannot get Northbridge (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700137 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600138 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700139 size = 256;
140 ret = mrc_locate_spd(dev, size, &spd_data);
Simon Glass345f3662019-04-25 21:58:48 -0600141 if (ret) {
142 debug("Cannot locate SPD (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700143 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600144 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700145 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 Glass345f3662019-04-25 21:58:48 -0600154 if (ret) {
155 debug("mrc_common_init() failed(err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700156 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600157 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700158 debug("Memory init done\n");
159
160 ret = sdram_find(dev);
Simon Glass345f3662019-04-25 21:58:48 -0600161 if (ret) {
162 debug("sdram_find() failed (err=%d)\n", ret);
Simon Glassd21f34e2016-03-11 22:07:26 -0700163 return ret;
Simon Glass345f3662019-04-25 21:58:48 -0600164 }
Simon Glassd21f34e2016-03-11 22:07:26 -0700165 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 Glass91efff52019-12-06 21:42:07 -0700172 struct mrc_output *mrc = &gd->arch.mrc[MRC_TYPE_NORMAL];
173
Simon Glassd21f34e2016-03-11 22:07:26 -0700174 /*
175 * This will be copied to SDRAM in reserve_arch(), then written
176 * to SPI flash in mrccache_save()
177 */
Simon Glass91efff52019-12-06 21:42:07 -0700178 mrc->buf = (char *)pei_data->data_to_save;
179 mrc->len = pei_data->data_to_save_size;
Simon Glassd21f34e2016-03-11 22:07:26 -0700180 }
181 gd->arch.pei_meminfo = pei_data->meminfo;
182
183 return 0;
184}
185
186/* Use this hook to save our SDRAM parameters */
187int 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 Glassd21f34e2016-03-11 22:07:26 -0700200static const struct udevice_id broadwell_syscon_ids[] = {
201 { .compatible = "intel,me", .data = X86_SYSCON_ME },
Simon Glassd21f34e2016-03-11 22:07:26 -0700202 { }
203};
204
205U_BOOT_DRIVER(syscon_intel_me) = {
206 .name = "intel_me_syscon",
207 .id = UCLASS_SYSCON,
208 .of_match = broadwell_syscon_ids,
209};