xref: /openbmc/u-boot/arch/x86/cpu/ivybridge/sdram.c (revision e0ed8332fa2fe684b4c8ba1caab991663730cbf0)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0
28ef07571SSimon Glass /*
38ef07571SSimon Glass  * Copyright (c) 2011 The Chromium OS Authors.
48ef07571SSimon Glass  * (C) Copyright 2010,2011
58ef07571SSimon Glass  * Graeme Russ, <graeme.russ@gmail.com>
68ef07571SSimon Glass  *
78ef07571SSimon Glass  * Portions from Coreboot mainboard/google/link/romstage.c
88ef07571SSimon Glass  * Copyright (C) 2007-2010 coresystems GmbH
98ef07571SSimon Glass  * Copyright (C) 2011 Google Inc.
108ef07571SSimon Glass  */
118ef07571SSimon Glass 
128ef07571SSimon Glass #include <common.h>
1365dd74a6SSimon Glass #include <errno.h>
1465dd74a6SSimon Glass #include <fdtdec.h>
1565dd74a6SSimon Glass #include <malloc.h>
16191c008aSSimon Glass #include <net.h>
17191c008aSSimon Glass #include <rtc.h>
18191c008aSSimon Glass #include <spi.h>
19191c008aSSimon Glass #include <spi_flash.h>
2098655f3aSSimon Glass #include <syscon.h>
21*b37b7b20SBin Meng #include <sysreset.h>
2298655f3aSSimon Glass #include <asm/cpu.h>
2365dd74a6SSimon Glass #include <asm/processor.h>
2465dd74a6SSimon Glass #include <asm/gpio.h>
2565dd74a6SSimon Glass #include <asm/global_data.h>
2606d336ccSSimon Glass #include <asm/intel_regs.h>
27f6220f1aSBin Meng #include <asm/mrccache.h>
28147ba41dSSimon Glass #include <asm/mrc_common.h>
29aaafcd6cSSimon Glass #include <asm/mtrr.h>
3065dd74a6SSimon Glass #include <asm/pci.h>
318b900a41SSimon Glass #include <asm/report_platform.h>
3265dd74a6SSimon Glass #include <asm/arch/me.h>
3365dd74a6SSimon Glass #include <asm/arch/pei_data.h>
3465dd74a6SSimon Glass #include <asm/arch/pch.h>
3565dd74a6SSimon Glass #include <asm/post.h>
3665dd74a6SSimon Glass #include <asm/arch/sandybridge.h>
3765dd74a6SSimon Glass 
3865dd74a6SSimon Glass DECLARE_GLOBAL_DATA_PTR;
3965dd74a6SSimon Glass 
40191c008aSSimon Glass #define CMOS_OFFSET_MRC_SEED		152
41191c008aSSimon Glass #define CMOS_OFFSET_MRC_SEED_S3		156
42191c008aSSimon Glass #define CMOS_OFFSET_MRC_SEED_CHK	160
43191c008aSSimon Glass 
board_get_usable_ram_top(ulong total_size)4465dd74a6SSimon Glass ulong board_get_usable_ram_top(ulong total_size)
4565dd74a6SSimon Glass {
46147ba41dSSimon Glass 	return mrc_common_board_get_usable_ram_top(total_size);
4765dd74a6SSimon Glass }
4865dd74a6SSimon Glass 
dram_init_banksize(void)4976b00acaSSimon Glass int dram_init_banksize(void)
5065dd74a6SSimon Glass {
51147ba41dSSimon Glass 	mrc_common_dram_init_banksize();
5276b00acaSSimon Glass 
5376b00acaSSimon Glass 	return 0;
5465dd74a6SSimon Glass }
5565dd74a6SSimon Glass 
read_seed_from_cmos(struct pei_data * pei_data)56191c008aSSimon Glass static int read_seed_from_cmos(struct pei_data *pei_data)
57191c008aSSimon Glass {
58191c008aSSimon Glass 	u16 c1, c2, checksum, seed_checksum;
5993f8a311SBin Meng 	struct udevice *dev;
6053327d3eSSimon Glass 	int ret = 0;
6193f8a311SBin Meng 
6253327d3eSSimon Glass 	ret = uclass_get_device(UCLASS_RTC, 0, &dev);
6353327d3eSSimon Glass 	if (ret) {
6453327d3eSSimon Glass 		debug("Cannot find RTC: err=%d\n", ret);
6593f8a311SBin Meng 		return -ENODEV;
6693f8a311SBin Meng 	}
67191c008aSSimon Glass 
68191c008aSSimon Glass 	/*
69191c008aSSimon Glass 	 * Read scrambler seeds from CMOS RAM. We don't want to store them in
70191c008aSSimon Glass 	 * SPI flash since they change on every boot and that would wear down
71191c008aSSimon Glass 	 * the flash too much. So we store these in CMOS and the large MRC
72191c008aSSimon Glass 	 * data in SPI flash.
73191c008aSSimon Glass 	 */
749fbc5ccdSSimon Glass 	ret = rtc_read32(dev, CMOS_OFFSET_MRC_SEED, &pei_data->scrambler_seed);
759fbc5ccdSSimon Glass 	if (!ret) {
769fbc5ccdSSimon Glass 		ret = rtc_read32(dev, CMOS_OFFSET_MRC_SEED_S3,
779fbc5ccdSSimon Glass 				 &pei_data->scrambler_seed_s3);
789fbc5ccdSSimon Glass 	}
799fbc5ccdSSimon Glass 	if (ret) {
809fbc5ccdSSimon Glass 		debug("Failed to read from RTC %s\n", dev->name);
819fbc5ccdSSimon Glass 		return ret;
829fbc5ccdSSimon Glass 	}
839fbc5ccdSSimon Glass 
84191c008aSSimon Glass 	debug("Read scrambler seed    0x%08x from CMOS 0x%02x\n",
85191c008aSSimon Glass 	      pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
86191c008aSSimon Glass 	debug("Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
87191c008aSSimon Glass 	      pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
88191c008aSSimon Glass 
89191c008aSSimon Glass 	/* Compute seed checksum and compare */
90191c008aSSimon Glass 	c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed,
91191c008aSSimon Glass 				 sizeof(u32));
92191c008aSSimon Glass 	c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3,
93191c008aSSimon Glass 				 sizeof(u32));
94191c008aSSimon Glass 	checksum = add_ip_checksums(sizeof(u32), c1, c2);
95191c008aSSimon Glass 
9693f8a311SBin Meng 	seed_checksum = rtc_read8(dev, CMOS_OFFSET_MRC_SEED_CHK);
9793f8a311SBin Meng 	seed_checksum |= rtc_read8(dev, CMOS_OFFSET_MRC_SEED_CHK + 1) << 8;
98191c008aSSimon Glass 
99191c008aSSimon Glass 	if (checksum != seed_checksum) {
100191c008aSSimon Glass 		debug("%s: invalid seed checksum\n", __func__);
101191c008aSSimon Glass 		pei_data->scrambler_seed = 0;
102191c008aSSimon Glass 		pei_data->scrambler_seed_s3 = 0;
103191c008aSSimon Glass 		return -EINVAL;
104191c008aSSimon Glass 	}
105191c008aSSimon Glass 
106191c008aSSimon Glass 	return 0;
107191c008aSSimon Glass }
108191c008aSSimon Glass 
prepare_mrc_cache(struct pei_data * pei_data)109191c008aSSimon Glass static int prepare_mrc_cache(struct pei_data *pei_data)
110191c008aSSimon Glass {
111191c008aSSimon Glass 	struct mrc_data_container *mrc_cache;
1124b9f6a66SBin Meng 	struct mrc_region entry;
113191c008aSSimon Glass 	int ret;
114191c008aSSimon Glass 
115191c008aSSimon Glass 	ret = read_seed_from_cmos(pei_data);
116191c008aSSimon Glass 	if (ret)
117191c008aSSimon Glass 		return ret;
11842913a1cSBin Meng 	ret = mrccache_get_region(NULL, &entry);
119191c008aSSimon Glass 	if (ret)
120191c008aSSimon Glass 		return ret;
121191c008aSSimon Glass 	mrc_cache = mrccache_find_current(&entry);
122191c008aSSimon Glass 	if (!mrc_cache)
123191c008aSSimon Glass 		return -ENOENT;
124191c008aSSimon Glass 
125191c008aSSimon Glass 	pei_data->mrc_input = mrc_cache->data;
126191c008aSSimon Glass 	pei_data->mrc_input_len = mrc_cache->data_size;
127191c008aSSimon Glass 	debug("%s: at %p, size %x checksum %04x\n", __func__,
128191c008aSSimon Glass 	      pei_data->mrc_input, pei_data->mrc_input_len,
129191c008aSSimon Glass 	      mrc_cache->checksum);
130191c008aSSimon Glass 
131191c008aSSimon Glass 	return 0;
132191c008aSSimon Glass }
133191c008aSSimon Glass 
write_seeds_to_cmos(struct pei_data * pei_data)134191c008aSSimon Glass static int write_seeds_to_cmos(struct pei_data *pei_data)
135191c008aSSimon Glass {
136191c008aSSimon Glass 	u16 c1, c2, checksum;
13793f8a311SBin Meng 	struct udevice *dev;
13853327d3eSSimon Glass 	int ret = 0;
13993f8a311SBin Meng 
14053327d3eSSimon Glass 	ret = uclass_get_device(UCLASS_RTC, 0, &dev);
14153327d3eSSimon Glass 	if (ret) {
14253327d3eSSimon Glass 		debug("Cannot find RTC: err=%d\n", ret);
14393f8a311SBin Meng 		return -ENODEV;
14493f8a311SBin Meng 	}
145191c008aSSimon Glass 
146191c008aSSimon Glass 	/* Save the MRC seed values to CMOS */
14793f8a311SBin Meng 	rtc_write32(dev, CMOS_OFFSET_MRC_SEED, pei_data->scrambler_seed);
148191c008aSSimon Glass 	debug("Save scrambler seed    0x%08x to CMOS 0x%02x\n",
149191c008aSSimon Glass 	      pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
150191c008aSSimon Glass 
15193f8a311SBin Meng 	rtc_write32(dev, CMOS_OFFSET_MRC_SEED_S3, pei_data->scrambler_seed_s3);
152191c008aSSimon Glass 	debug("Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
153191c008aSSimon Glass 	      pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
154191c008aSSimon Glass 
155191c008aSSimon Glass 	/* Save a simple checksum of the seed values */
156191c008aSSimon Glass 	c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed,
157191c008aSSimon Glass 				 sizeof(u32));
158191c008aSSimon Glass 	c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3,
159191c008aSSimon Glass 				 sizeof(u32));
160191c008aSSimon Glass 	checksum = add_ip_checksums(sizeof(u32), c1, c2);
161191c008aSSimon Glass 
16293f8a311SBin Meng 	rtc_write8(dev, CMOS_OFFSET_MRC_SEED_CHK, checksum & 0xff);
16393f8a311SBin Meng 	rtc_write8(dev, CMOS_OFFSET_MRC_SEED_CHK + 1, (checksum >> 8) & 0xff);
164191c008aSSimon Glass 
165191c008aSSimon Glass 	return 0;
166191c008aSSimon Glass }
167191c008aSSimon Glass 
168191c008aSSimon Glass /* Use this hook to save our SDRAM parameters */
misc_init_r(void)169191c008aSSimon Glass int misc_init_r(void)
170191c008aSSimon Glass {
171191c008aSSimon Glass 	int ret;
172191c008aSSimon Glass 
17342913a1cSBin Meng 	ret = mrccache_save();
174191c008aSSimon Glass 	if (ret)
175191c008aSSimon Glass 		printf("Unable to save MRC data: %d\n", ret);
176191c008aSSimon Glass 
177191c008aSSimon Glass 	return 0;
178191c008aSSimon Glass }
179191c008aSSimon Glass 
post_system_agent_init(struct udevice * dev,struct udevice * me_dev,struct pei_data * pei_data)180147ba41dSSimon Glass static void post_system_agent_init(struct udevice *dev, struct udevice *me_dev,
181c02a4242SSimon Glass 				   struct pei_data *pei_data)
18265dd74a6SSimon Glass {
18365dd74a6SSimon Glass 	uint16_t done;
18465dd74a6SSimon Glass 
18565dd74a6SSimon Glass 	/*
18665dd74a6SSimon Glass 	 * Send ME init done for SandyBridge here.  This is done inside the
18765dd74a6SSimon Glass 	 * SystemAgent binary on IvyBridge
18865dd74a6SSimon Glass 	 */
1891641bb8cSSimon Glass 	dm_pci_read_config16(dev, PCI_DEVICE_ID, &done);
19065dd74a6SSimon Glass 	done &= BASE_REV_MASK;
19165dd74a6SSimon Glass 	if (BASE_REV_SNB == done)
192c02a4242SSimon Glass 		intel_early_me_init_done(dev, me_dev, ME_INIT_STATUS_SUCCESS);
19365dd74a6SSimon Glass 	else
1948b900a41SSimon Glass 		intel_me_status(me_dev);
19565dd74a6SSimon Glass 
196147ba41dSSimon Glass 	/* If PCIe init is skipped, set the PEG clock gating */
197147ba41dSSimon Glass 	if (!pei_data->pcie_init)
198147ba41dSSimon Glass 		setbits_le32(MCHBAR_REG(0x7010), 1);
199191c008aSSimon Glass }
200191c008aSSimon Glass 
recovery_mode_enabled(void)201147ba41dSSimon Glass static int recovery_mode_enabled(void)
202147ba41dSSimon Glass {
203147ba41dSSimon Glass 	return false;
204191c008aSSimon Glass }
205191c008aSSimon Glass 
copy_spd(struct udevice * dev,struct pei_data * peid)206147ba41dSSimon Glass static int copy_spd(struct udevice *dev, struct pei_data *peid)
20765dd74a6SSimon Glass {
208147ba41dSSimon Glass 	const void *data;
209147ba41dSSimon Glass 	int ret;
21065dd74a6SSimon Glass 
211147ba41dSSimon Glass 	ret = mrc_locate_spd(dev, sizeof(peid->spd_data[0]), &data);
2128d8f3acdSSimon Glass 	if (ret) {
2138d8f3acdSSimon Glass 		debug("%s: Could not locate SPD (ret=%d)\n", __func__, ret);
21465dd74a6SSimon Glass 		return ret;
2158d8f3acdSSimon Glass 	}
21665dd74a6SSimon Glass 
21765dd74a6SSimon Glass 	memcpy(peid->spd_data[0], data, sizeof(peid->spd_data[0]));
21865dd74a6SSimon Glass 
21965dd74a6SSimon Glass 	return 0;
22065dd74a6SSimon Glass }
22165dd74a6SSimon Glass 
22265dd74a6SSimon Glass /**
22365dd74a6SSimon Glass  * sdram_find() - Find available memory
22465dd74a6SSimon Glass  *
22565dd74a6SSimon Glass  * This is a bit complicated since on x86 there are system memory holes all
22665dd74a6SSimon Glass  * over the place. We create a list of available memory blocks
2272588e711SSimon Glass  *
2282588e711SSimon Glass  * @dev:	Northbridge device
22965dd74a6SSimon Glass  */
sdram_find(struct udevice * dev)2302588e711SSimon Glass static int sdram_find(struct udevice *dev)
23165dd74a6SSimon Glass {
23265dd74a6SSimon Glass 	struct memory_info *info = &gd->arch.meminfo;
23365dd74a6SSimon Glass 	uint32_t tseg_base, uma_size, tolud;
23465dd74a6SSimon Glass 	uint64_t tom, me_base, touud;
23565dd74a6SSimon Glass 	uint64_t uma_memory_base = 0;
23665dd74a6SSimon Glass 	unsigned long long tomk;
23765dd74a6SSimon Glass 	uint16_t ggc;
2382588e711SSimon Glass 	u32 val;
23965dd74a6SSimon Glass 
24065dd74a6SSimon Glass 	/* Total Memory 2GB example:
24165dd74a6SSimon Glass 	 *
24265dd74a6SSimon Glass 	 *  00000000  0000MB-1992MB  1992MB  RAM     (writeback)
24365dd74a6SSimon Glass 	 *  7c800000  1992MB-2000MB     8MB  TSEG    (SMRR)
24465dd74a6SSimon Glass 	 *  7d000000  2000MB-2002MB     2MB  GFX GTT (uncached)
24565dd74a6SSimon Glass 	 *  7d200000  2002MB-2034MB    32MB  GFX UMA (uncached)
24665dd74a6SSimon Glass 	 *  7f200000   2034MB TOLUD
24765dd74a6SSimon Glass 	 *  7f800000   2040MB MEBASE
24865dd74a6SSimon Glass 	 *  7f800000  2040MB-2048MB     8MB  ME UMA  (uncached)
24965dd74a6SSimon Glass 	 *  80000000   2048MB TOM
25065dd74a6SSimon Glass 	 * 100000000  4096MB-4102MB     6MB  RAM     (writeback)
25165dd74a6SSimon Glass 	 *
25265dd74a6SSimon Glass 	 * Total Memory 4GB example:
25365dd74a6SSimon Glass 	 *
25465dd74a6SSimon Glass 	 *  00000000  0000MB-2768MB  2768MB  RAM     (writeback)
25565dd74a6SSimon Glass 	 *  ad000000  2768MB-2776MB     8MB  TSEG    (SMRR)
25665dd74a6SSimon Glass 	 *  ad800000  2776MB-2778MB     2MB  GFX GTT (uncached)
25765dd74a6SSimon Glass 	 *  ada00000  2778MB-2810MB    32MB  GFX UMA (uncached)
25865dd74a6SSimon Glass 	 *  afa00000   2810MB TOLUD
25965dd74a6SSimon Glass 	 *  ff800000   4088MB MEBASE
26065dd74a6SSimon Glass 	 *  ff800000  4088MB-4096MB     8MB  ME UMA  (uncached)
26165dd74a6SSimon Glass 	 * 100000000   4096MB TOM
26265dd74a6SSimon Glass 	 * 100000000  4096MB-5374MB  1278MB  RAM     (writeback)
26365dd74a6SSimon Glass 	 * 14fe00000   5368MB TOUUD
26465dd74a6SSimon Glass 	 */
26565dd74a6SSimon Glass 
26665dd74a6SSimon Glass 	/* Top of Upper Usable DRAM, including remap */
2672588e711SSimon Glass 	dm_pci_read_config32(dev, TOUUD + 4, &val);
2682588e711SSimon Glass 	touud = (uint64_t)val << 32;
2692588e711SSimon Glass 	dm_pci_read_config32(dev, TOUUD, &val);
2702588e711SSimon Glass 	touud |= val;
27165dd74a6SSimon Glass 
27265dd74a6SSimon Glass 	/* Top of Lower Usable DRAM */
2732588e711SSimon Glass 	dm_pci_read_config32(dev, TOLUD, &tolud);
27465dd74a6SSimon Glass 
27565dd74a6SSimon Glass 	/* Top of Memory - does not account for any UMA */
2762588e711SSimon Glass 	dm_pci_read_config32(dev, 0xa4, &val);
2772588e711SSimon Glass 	tom = (uint64_t)val << 32;
2782588e711SSimon Glass 	dm_pci_read_config32(dev, 0xa0, &val);
2792588e711SSimon Glass 	tom |= val;
28065dd74a6SSimon Glass 
28165dd74a6SSimon Glass 	debug("TOUUD %llx TOLUD %08x TOM %llx\n", touud, tolud, tom);
28265dd74a6SSimon Glass 
28365dd74a6SSimon Glass 	/* ME UMA needs excluding if total memory <4GB */
2842588e711SSimon Glass 	dm_pci_read_config32(dev, 0x74, &val);
2852588e711SSimon Glass 	me_base = (uint64_t)val << 32;
2862588e711SSimon Glass 	dm_pci_read_config32(dev, 0x70, &val);
2872588e711SSimon Glass 	me_base |= val;
28865dd74a6SSimon Glass 
28965dd74a6SSimon Glass 	debug("MEBASE %llx\n", me_base);
29065dd74a6SSimon Glass 
29165dd74a6SSimon Glass 	/* TODO: Get rid of all this shifting by 10 bits */
29265dd74a6SSimon Glass 	tomk = tolud >> 10;
29365dd74a6SSimon Glass 	if (me_base == tolud) {
29465dd74a6SSimon Glass 		/* ME is from MEBASE-TOM */
29565dd74a6SSimon Glass 		uma_size = (tom - me_base) >> 10;
29665dd74a6SSimon Glass 		/* Increment TOLUD to account for ME as RAM */
29765dd74a6SSimon Glass 		tolud += uma_size << 10;
29865dd74a6SSimon Glass 		/* UMA starts at old TOLUD */
29965dd74a6SSimon Glass 		uma_memory_base = tomk * 1024ULL;
30065dd74a6SSimon Glass 		debug("ME UMA base %llx size %uM\n", me_base, uma_size >> 10);
30165dd74a6SSimon Glass 	}
30265dd74a6SSimon Glass 
30365dd74a6SSimon Glass 	/* Graphics memory comes next */
3042588e711SSimon Glass 	dm_pci_read_config16(dev, GGC, &ggc);
30565dd74a6SSimon Glass 	if (!(ggc & 2)) {
30665dd74a6SSimon Glass 		debug("IGD decoded, subtracting ");
30765dd74a6SSimon Glass 
30865dd74a6SSimon Glass 		/* Graphics memory */
30965dd74a6SSimon Glass 		uma_size = ((ggc >> 3) & 0x1f) * 32 * 1024ULL;
31065dd74a6SSimon Glass 		debug("%uM UMA", uma_size >> 10);
31165dd74a6SSimon Glass 		tomk -= uma_size;
31265dd74a6SSimon Glass 		uma_memory_base = tomk * 1024ULL;
31365dd74a6SSimon Glass 
31465dd74a6SSimon Glass 		/* GTT Graphics Stolen Memory Size (GGMS) */
31565dd74a6SSimon Glass 		uma_size = ((ggc >> 8) & 0x3) * 1024ULL;
31665dd74a6SSimon Glass 		tomk -= uma_size;
31765dd74a6SSimon Glass 		uma_memory_base = tomk * 1024ULL;
31865dd74a6SSimon Glass 		debug(" and %uM GTT\n", uma_size >> 10);
31965dd74a6SSimon Glass 	}
32065dd74a6SSimon Glass 
32165dd74a6SSimon Glass 	/* Calculate TSEG size from its base which must be below GTT */
3222588e711SSimon Glass 	dm_pci_read_config32(dev, 0xb8, &tseg_base);
32365dd74a6SSimon Glass 	uma_size = (uma_memory_base - tseg_base) >> 10;
32465dd74a6SSimon Glass 	tomk -= uma_size;
32565dd74a6SSimon Glass 	uma_memory_base = tomk * 1024ULL;
32665dd74a6SSimon Glass 	debug("TSEG base 0x%08x size %uM\n", tseg_base, uma_size >> 10);
32765dd74a6SSimon Glass 
32865dd74a6SSimon Glass 	debug("Available memory below 4GB: %lluM\n", tomk >> 10);
32965dd74a6SSimon Glass 
33065dd74a6SSimon Glass 	/* Report the memory regions */
331147ba41dSSimon Glass 	mrc_add_memory_area(info, 1 << 20, 2 << 28);
332147ba41dSSimon Glass 	mrc_add_memory_area(info, (2 << 28) + (2 << 20), 4 << 28);
333147ba41dSSimon Glass 	mrc_add_memory_area(info, (4 << 28) + (2 << 20), tseg_base);
334147ba41dSSimon Glass 	mrc_add_memory_area(info, 1ULL << 32, touud);
335aaafcd6cSSimon Glass 
336aaafcd6cSSimon Glass 	/* Add MTRRs for memory */
337aaafcd6cSSimon Glass 	mtrr_add_request(MTRR_TYPE_WRBACK, 0, 2ULL << 30);
338aaafcd6cSSimon Glass 	mtrr_add_request(MTRR_TYPE_WRBACK, 2ULL << 30, 512 << 20);
339aaafcd6cSSimon Glass 	mtrr_add_request(MTRR_TYPE_WRBACK, 0xaULL << 28, 256 << 20);
340aaafcd6cSSimon Glass 	mtrr_add_request(MTRR_TYPE_UNCACHEABLE, tseg_base, 16 << 20);
341aaafcd6cSSimon Glass 	mtrr_add_request(MTRR_TYPE_UNCACHEABLE, tseg_base + (16 << 20),
342aaafcd6cSSimon Glass 			 32 << 20);
343aaafcd6cSSimon Glass 
34465dd74a6SSimon Glass 	/*
34565dd74a6SSimon Glass 	 * If >= 4GB installed then memory from TOLUD to 4GB
34665dd74a6SSimon Glass 	 * is remapped above TOM, TOUUD will account for both
34765dd74a6SSimon Glass 	 */
34865dd74a6SSimon Glass 	if (touud > (1ULL << 32ULL)) {
34965dd74a6SSimon Glass 		debug("Available memory above 4GB: %lluM\n",
35065dd74a6SSimon Glass 		      (touud >> 20) - 4096);
35165dd74a6SSimon Glass 	}
35265dd74a6SSimon Glass 
35365dd74a6SSimon Glass 	return 0;
35465dd74a6SSimon Glass }
35565dd74a6SSimon Glass 
rcba_config(void)35665dd74a6SSimon Glass static void rcba_config(void)
35765dd74a6SSimon Glass {
35865dd74a6SSimon Glass 	/*
35965dd74a6SSimon Glass 	 *             GFX    INTA -> PIRQA (MSI)
36065dd74a6SSimon Glass 	 * D28IP_P3IP  WLAN   INTA -> PIRQB
36165dd74a6SSimon Glass 	 * D29IP_E1P   EHCI1  INTA -> PIRQD
36265dd74a6SSimon Glass 	 * D26IP_E2P   EHCI2  INTA -> PIRQF
36365dd74a6SSimon Glass 	 * D31IP_SIP   SATA   INTA -> PIRQF (MSI)
36465dd74a6SSimon Glass 	 * D31IP_SMIP  SMBUS  INTB -> PIRQH
36565dd74a6SSimon Glass 	 * D31IP_TTIP  THRT   INTC -> PIRQA
36665dd74a6SSimon Glass 	 * D27IP_ZIP   HDA    INTA -> PIRQA (MSI)
36765dd74a6SSimon Glass 	 *
36865dd74a6SSimon Glass 	 * TRACKPAD                -> PIRQE (Edge Triggered)
36965dd74a6SSimon Glass 	 * TOUCHSCREEN             -> PIRQG (Edge Triggered)
37065dd74a6SSimon Glass 	 */
37165dd74a6SSimon Glass 
37265dd74a6SSimon Glass 	/* Device interrupt pin register (board specific) */
37365dd74a6SSimon Glass 	writel((INTC << D31IP_TTIP) | (NOINT << D31IP_SIP2) |
37465dd74a6SSimon Glass 	       (INTB << D31IP_SMIP) | (INTA << D31IP_SIP), RCB_REG(D31IP));
37565dd74a6SSimon Glass 	writel(NOINT << D30IP_PIP, RCB_REG(D30IP));
37665dd74a6SSimon Glass 	writel(INTA << D29IP_E1P, RCB_REG(D29IP));
37765dd74a6SSimon Glass 	writel(INTA << D28IP_P3IP, RCB_REG(D28IP));
37865dd74a6SSimon Glass 	writel(INTA << D27IP_ZIP, RCB_REG(D27IP));
37965dd74a6SSimon Glass 	writel(INTA << D26IP_E2P, RCB_REG(D26IP));
38065dd74a6SSimon Glass 	writel(NOINT << D25IP_LIP, RCB_REG(D25IP));
38165dd74a6SSimon Glass 	writel(NOINT << D22IP_MEI1IP, RCB_REG(D22IP));
38265dd74a6SSimon Glass 
38365dd74a6SSimon Glass 	/* Device interrupt route registers */
38465dd74a6SSimon Glass 	writel(DIR_ROUTE(PIRQB, PIRQH, PIRQA, PIRQC), RCB_REG(D31IR));
38565dd74a6SSimon Glass 	writel(DIR_ROUTE(PIRQD, PIRQE, PIRQF, PIRQG), RCB_REG(D29IR));
38665dd74a6SSimon Glass 	writel(DIR_ROUTE(PIRQB, PIRQC, PIRQD, PIRQE), RCB_REG(D28IR));
38765dd74a6SSimon Glass 	writel(DIR_ROUTE(PIRQA, PIRQH, PIRQA, PIRQB), RCB_REG(D27IR));
38865dd74a6SSimon Glass 	writel(DIR_ROUTE(PIRQF, PIRQE, PIRQG, PIRQH), RCB_REG(D26IR));
38965dd74a6SSimon Glass 	writel(DIR_ROUTE(PIRQA, PIRQB, PIRQC, PIRQD), RCB_REG(D25IR));
39065dd74a6SSimon Glass 	writel(DIR_ROUTE(PIRQA, PIRQB, PIRQC, PIRQD), RCB_REG(D22IR));
39165dd74a6SSimon Glass 
39265dd74a6SSimon Glass 	/* Enable IOAPIC (generic) */
39365dd74a6SSimon Glass 	writew(0x0100, RCB_REG(OIC));
39465dd74a6SSimon Glass 	/* PCH BWG says to read back the IOAPIC enable register */
39565dd74a6SSimon Glass 	(void)readw(RCB_REG(OIC));
39665dd74a6SSimon Glass 
39765dd74a6SSimon Glass 	/* Disable unused devices (board specific) */
39865dd74a6SSimon Glass 	setbits_le32(RCB_REG(FD), PCH_DISABLE_ALWAYS);
39965dd74a6SSimon Glass }
4008ef07571SSimon Glass 
dram_init(void)4018ef07571SSimon Glass int dram_init(void)
4028ef07571SSimon Glass {
403147ba41dSSimon Glass 	struct pei_data _pei_data __aligned(8) = {
40465dd74a6SSimon Glass 		.pei_version = PEI_VERSION,
40506d336ccSSimon Glass 		.mchbar = MCH_BASE_ADDRESS,
40665dd74a6SSimon Glass 		.dmibar = DEFAULT_DMIBAR,
40765dd74a6SSimon Glass 		.epbar = DEFAULT_EPBAR,
4082d934e57SSimon Glass 		.pciexbar = CONFIG_PCIE_ECAM_BASE,
40965dd74a6SSimon Glass 		.smbusbar = SMBUS_IO_BASE,
41065dd74a6SSimon Glass 		.wdbbar = 0x4000000,
41165dd74a6SSimon Glass 		.wdbsize = 0x1000,
41265dd74a6SSimon Glass 		.hpet_address = CONFIG_HPET_ADDRESS,
41365dd74a6SSimon Glass 		.rcba = DEFAULT_RCBABASE,
41465dd74a6SSimon Glass 		.pmbase = DEFAULT_PMBASE,
41565dd74a6SSimon Glass 		.gpiobase = DEFAULT_GPIOBASE,
41665dd74a6SSimon Glass 		.thermalbase = 0xfed08000,
41765dd74a6SSimon Glass 		.system_type = 0, /* 0 Mobile, 1 Desktop/Server */
41865dd74a6SSimon Glass 		.tseg_size = CONFIG_SMM_TSEG_SIZE,
41965dd74a6SSimon Glass 		.ts_addresses = { 0x00, 0x00, 0x00, 0x00 },
42065dd74a6SSimon Glass 		.ec_present = 1,
42165dd74a6SSimon Glass 		.ddr3lv_support = 1,
42265dd74a6SSimon Glass 		/*
42365dd74a6SSimon Glass 		 * 0 = leave channel enabled
42465dd74a6SSimon Glass 		 * 1 = disable dimm 0 on channel
42565dd74a6SSimon Glass 		 * 2 = disable dimm 1 on channel
42665dd74a6SSimon Glass 		 * 3 = disable dimm 0+1 on channel
42765dd74a6SSimon Glass 		 */
42865dd74a6SSimon Glass 		.dimm_channel0_disabled = 2,
42965dd74a6SSimon Glass 		.dimm_channel1_disabled = 2,
43065dd74a6SSimon Glass 		.max_ddr3_freq = 1600,
43165dd74a6SSimon Glass 		.usb_port_config = {
43265dd74a6SSimon Glass 			/*
43365dd74a6SSimon Glass 			 * Empty and onboard Ports 0-7, set to un-used pin
43465dd74a6SSimon Glass 			 * OC3
43565dd74a6SSimon Glass 			 */
43665dd74a6SSimon Glass 			{ 0, 3, 0x0000 }, /* P0= Empty */
43765dd74a6SSimon Glass 			{ 1, 0, 0x0040 }, /* P1= Left USB 1  (OC0) */
43865dd74a6SSimon Glass 			{ 1, 1, 0x0040 }, /* P2= Left USB 2  (OC1) */
43965dd74a6SSimon Glass 			{ 1, 3, 0x0040 }, /* P3= SDCARD      (no OC) */
44065dd74a6SSimon Glass 			{ 0, 3, 0x0000 }, /* P4= Empty */
44165dd74a6SSimon Glass 			{ 1, 3, 0x0040 }, /* P5= WWAN        (no OC) */
44265dd74a6SSimon Glass 			{ 0, 3, 0x0000 }, /* P6= Empty */
44365dd74a6SSimon Glass 			{ 0, 3, 0x0000 }, /* P7= Empty */
44465dd74a6SSimon Glass 			/*
44565dd74a6SSimon Glass 			 * Empty and onboard Ports 8-13, set to un-used pin
44665dd74a6SSimon Glass 			 * OC4
44765dd74a6SSimon Glass 			 */
44865dd74a6SSimon Glass 			{ 1, 4, 0x0040 }, /* P8= Camera      (no OC) */
44965dd74a6SSimon Glass 			{ 1, 4, 0x0040 }, /* P9= Bluetooth   (no OC) */
45065dd74a6SSimon Glass 			{ 0, 4, 0x0000 }, /* P10= Empty */
45165dd74a6SSimon Glass 			{ 0, 4, 0x0000 }, /* P11= Empty */
45265dd74a6SSimon Glass 			{ 0, 4, 0x0000 }, /* P12= Empty */
45365dd74a6SSimon Glass 			{ 0, 4, 0x0000 }, /* P13= Empty */
45465dd74a6SSimon Glass 		},
45565dd74a6SSimon Glass 	};
456147ba41dSSimon Glass 	struct pei_data *pei_data = &_pei_data;
457c02a4242SSimon Glass 	struct udevice *dev, *me_dev;
45865dd74a6SSimon Glass 	int ret;
45965dd74a6SSimon Glass 
4609532fe3bSSimon Glass 	/* We need the pinctrl set up early */
4619532fe3bSSimon Glass 	ret = syscon_get_by_driver_data(X86_SYSCON_PINCONF, &dev);
4628d8f3acdSSimon Glass 	if (ret) {
4638d8f3acdSSimon Glass 		debug("%s: Could not get pinconf (ret=%d)\n", __func__, ret);
4649532fe3bSSimon Glass 		return ret;
4658d8f3acdSSimon Glass 	}
4669532fe3bSSimon Glass 
4673f603cbbSSimon Glass 	ret = uclass_first_device_err(UCLASS_NORTHBRIDGE, &dev);
4688d8f3acdSSimon Glass 	if (ret) {
4698d8f3acdSSimon Glass 		debug("%s: Could not get northbridge (ret=%d)\n", __func__,
4708d8f3acdSSimon Glass 		      ret);
4711641bb8cSSimon Glass 		return ret;
4728d8f3acdSSimon Glass 	}
47398655f3aSSimon Glass 	ret = syscon_get_by_driver_data(X86_SYSCON_ME, &me_dev);
4748d8f3acdSSimon Glass 	if (ret) {
4758d8f3acdSSimon Glass 		debug("%s: Could not get ME (ret=%d)\n", __func__, ret);
476c02a4242SSimon Glass 		return ret;
4778d8f3acdSSimon Glass 	}
478147ba41dSSimon Glass 	ret = copy_spd(dev, pei_data);
4798d8f3acdSSimon Glass 	if (ret) {
4808d8f3acdSSimon Glass 		debug("%s: Could not get SPD (ret=%d)\n", __func__, ret);
48165dd74a6SSimon Glass 		return ret;
4828d8f3acdSSimon Glass 	}
483147ba41dSSimon Glass 	pei_data->boot_mode = gd->arch.pei_boot_mode;
484147ba41dSSimon Glass 	debug("Boot mode %d\n", gd->arch.pei_boot_mode);
485147ba41dSSimon Glass 	debug("mrc_input %p\n", pei_data->mrc_input);
48665dd74a6SSimon Glass 
487147ba41dSSimon Glass 	/*
488147ba41dSSimon Glass 	 * Do not pass MRC data in for recovery mode boot,
489147ba41dSSimon Glass 	 * Always pass it in for S3 resume.
490147ba41dSSimon Glass 	 */
491147ba41dSSimon Glass 	if (!recovery_mode_enabled() ||
492147ba41dSSimon Glass 	    pei_data->boot_mode == PEI_BOOT_RESUME) {
493147ba41dSSimon Glass 		ret = prepare_mrc_cache(pei_data);
494147ba41dSSimon Glass 		if (ret)
495147ba41dSSimon Glass 			debug("prepare_mrc_cache failed: %d\n", ret);
496147ba41dSSimon Glass 	}
49765dd74a6SSimon Glass 
498147ba41dSSimon Glass 	/* If MRC data is not found we cannot continue S3 resume. */
499147ba41dSSimon Glass 	if (pei_data->boot_mode == PEI_BOOT_RESUME && !pei_data->mrc_input) {
500147ba41dSSimon Glass 		debug("Giving up in sdram_initialize: No MRC data\n");
501*b37b7b20SBin Meng 		sysreset_walk_halt(SYSRESET_COLD);
502147ba41dSSimon Glass 	}
50365dd74a6SSimon Glass 
504147ba41dSSimon Glass 	/* Pass console handler in pei_data */
505147ba41dSSimon Glass 	pei_data->tx_byte = sdram_console_tx_byte;
506147ba41dSSimon Glass 
507147ba41dSSimon Glass 	/* Wait for ME to be ready */
508147ba41dSSimon Glass 	ret = intel_early_me_init(me_dev);
5098d8f3acdSSimon Glass 	if (ret) {
5108d8f3acdSSimon Glass 		debug("%s: Could not init ME (ret=%d)\n", __func__, ret);
511147ba41dSSimon Glass 		return ret;
5128d8f3acdSSimon Glass 	}
513147ba41dSSimon Glass 	ret = intel_early_me_uma_size(me_dev);
5148d8f3acdSSimon Glass 	if (ret < 0) {
5158d8f3acdSSimon Glass 		debug("%s: Could not get UMA size (ret=%d)\n", __func__, ret);
516147ba41dSSimon Glass 		return ret;
5178d8f3acdSSimon Glass 	}
518147ba41dSSimon Glass 
519147ba41dSSimon Glass 	ret = mrc_common_init(dev, pei_data, false);
5208d8f3acdSSimon Glass 	if (ret) {
5218d8f3acdSSimon Glass 		debug("%s: mrc_common_init() failed (ret=%d)\n", __func__, ret);
522147ba41dSSimon Glass 		return ret;
5238d8f3acdSSimon Glass 	}
52465dd74a6SSimon Glass 
52565dd74a6SSimon Glass 	ret = sdram_find(dev);
5268d8f3acdSSimon Glass 	if (ret) {
5278d8f3acdSSimon Glass 		debug("%s: sdram_find() failed (ret=%d)\n", __func__, ret);
52865dd74a6SSimon Glass 		return ret;
5298d8f3acdSSimon Glass 	}
53065dd74a6SSimon Glass 	gd->ram_size = gd->arch.meminfo.total_32bit_memory;
5318ef07571SSimon Glass 
532147ba41dSSimon Glass 	debug("MRC output data length %#x at %p\n", pei_data->mrc_output_len,
533147ba41dSSimon Glass 	      pei_data->mrc_output);
534147ba41dSSimon Glass 
535147ba41dSSimon Glass 	post_system_agent_init(dev, me_dev, pei_data);
536147ba41dSSimon Glass 	report_memory_config();
537147ba41dSSimon Glass 
538147ba41dSSimon Glass 	/* S3 resume: don't save scrambler seed or MRC data */
539147ba41dSSimon Glass 	if (pei_data->boot_mode != PEI_BOOT_RESUME) {
540147ba41dSSimon Glass 		/*
541147ba41dSSimon Glass 		 * This will be copied to SDRAM in reserve_arch(), then written
542147ba41dSSimon Glass 		 * to SPI flash in mrccache_save()
543147ba41dSSimon Glass 		 */
544147ba41dSSimon Glass 		gd->arch.mrc_output = (char *)pei_data->mrc_output;
545147ba41dSSimon Glass 		gd->arch.mrc_output_len = pei_data->mrc_output_len;
546147ba41dSSimon Glass 		ret = write_seeds_to_cmos(pei_data);
547147ba41dSSimon Glass 		if (ret)
548147ba41dSSimon Glass 			debug("Failed to write seeds to CMOS: %d\n", ret);
549147ba41dSSimon Glass 	}
550147ba41dSSimon Glass 
551147ba41dSSimon Glass 	writew(0xCAFE, MCHBAR_REG(SSKPD));
552147ba41dSSimon Glass 	if (ret)
553147ba41dSSimon Glass 		return ret;
554147ba41dSSimon Glass 
555147ba41dSSimon Glass 	rcba_config();
556147ba41dSSimon Glass 
5578ef07571SSimon Glass 	return 0;
5588ef07571SSimon Glass }
559