blob: 20c2e6891087e356912b05ec2c6571d4c86d212e [file] [log] [blame]
Simon Glass0b36ecd2014-11-12 22:42:07 -07001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
3 * (C) Copyright 2010,2011
4 * Graeme Russ, <graeme.russ@gmail.com>
5 *
6 * Portions from Coreboot mainboard/google/link/romstage.c
7 * Copyright (C) 2007-2010 coresystems GmbH
8 * Copyright (C) 2011 Google Inc.
9 *
10 * SPDX-License-Identifier: GPL-2.0
11 */
12
13#include <common.h>
Simon Glass268eefd2014-11-12 22:42:28 -070014#include <errno.h>
15#include <fdtdec.h>
16#include <malloc.h>
Simon Glass428dfa42015-01-19 22:16:14 -070017#include <net.h>
18#include <rtc.h>
19#include <spi.h>
20#include <spi_flash.h>
Simon Glass268eefd2014-11-12 22:42:28 -070021#include <asm/processor.h>
22#include <asm/gpio.h>
23#include <asm/global_data.h>
Bin Meng21666cf2015-10-11 21:37:36 -070024#include <asm/mrccache.h>
Simon Glassa754b952015-01-01 16:18:10 -070025#include <asm/mtrr.h>
Simon Glass268eefd2014-11-12 22:42:28 -070026#include <asm/pci.h>
27#include <asm/arch/me.h>
28#include <asm/arch/pei_data.h>
29#include <asm/arch/pch.h>
30#include <asm/post.h>
31#include <asm/arch/sandybridge.h>
32
33DECLARE_GLOBAL_DATA_PTR;
34
Simon Glass428dfa42015-01-19 22:16:14 -070035#define CMOS_OFFSET_MRC_SEED 152
36#define CMOS_OFFSET_MRC_SEED_S3 156
37#define CMOS_OFFSET_MRC_SEED_CHK 160
38
Simon Glass268eefd2014-11-12 22:42:28 -070039/*
40 * This function looks for the highest region of memory lower than 4GB which
41 * has enough space for U-Boot where U-Boot is aligned on a page boundary.
42 * It overrides the default implementation found elsewhere which simply
43 * picks the end of ram, wherever that may be. The location of the stack,
44 * the relocation address, and how far U-Boot is moved by relocation are
45 * set in the global data structure.
46 */
47ulong board_get_usable_ram_top(ulong total_size)
48{
49 struct memory_info *info = &gd->arch.meminfo;
50 uintptr_t dest_addr = 0;
51 struct memory_area *largest = NULL;
52 int i;
53
54 /* Find largest area of memory below 4GB */
55
56 for (i = 0; i < info->num_areas; i++) {
57 struct memory_area *area = &info->area[i];
58
59 if (area->start >= 1ULL << 32)
60 continue;
61 if (!largest || area->size > largest->size)
62 largest = area;
63 }
64
65 /* If no suitable area was found, return an error. */
66 assert(largest);
67 if (!largest || largest->size < (2 << 20))
68 panic("No available memory found for relocation");
69
70 dest_addr = largest->start + largest->size;
71
72 return (ulong)dest_addr;
73}
74
75void dram_init_banksize(void)
76{
77 struct memory_info *info = &gd->arch.meminfo;
78 int num_banks;
79 int i;
80
81 for (i = 0, num_banks = 0; i < info->num_areas; i++) {
82 struct memory_area *area = &info->area[i];
83
84 if (area->start >= 1ULL << 32)
85 continue;
86 gd->bd->bi_dram[num_banks].start = area->start;
87 gd->bd->bi_dram[num_banks].size = area->size;
88 num_banks++;
89 }
Simon Glass428dfa42015-01-19 22:16:14 -070090}
91
Simon Glass428dfa42015-01-19 22:16:14 -070092static int read_seed_from_cmos(struct pei_data *pei_data)
93{
94 u16 c1, c2, checksum, seed_checksum;
Bin Meng770fd332015-07-15 16:23:39 +080095 struct udevice *dev;
96 int rcode = 0;
97
98 rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
99 if (rcode) {
100 debug("Cannot find RTC: err=%d\n", rcode);
101 return -ENODEV;
102 }
Simon Glass428dfa42015-01-19 22:16:14 -0700103
104 /*
105 * Read scrambler seeds from CMOS RAM. We don't want to store them in
106 * SPI flash since they change on every boot and that would wear down
107 * the flash too much. So we store these in CMOS and the large MRC
108 * data in SPI flash.
109 */
Bin Meng770fd332015-07-15 16:23:39 +0800110 rtc_read32(dev, CMOS_OFFSET_MRC_SEED, &pei_data->scrambler_seed);
Simon Glass428dfa42015-01-19 22:16:14 -0700111 debug("Read scrambler seed 0x%08x from CMOS 0x%02x\n",
112 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
113
Bin Meng770fd332015-07-15 16:23:39 +0800114 rtc_read32(dev, CMOS_OFFSET_MRC_SEED_S3, &pei_data->scrambler_seed_s3);
Simon Glass428dfa42015-01-19 22:16:14 -0700115 debug("Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
116 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
117
118 /* Compute seed checksum and compare */
119 c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed,
120 sizeof(u32));
121 c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3,
122 sizeof(u32));
123 checksum = add_ip_checksums(sizeof(u32), c1, c2);
124
Bin Meng770fd332015-07-15 16:23:39 +0800125 seed_checksum = rtc_read8(dev, CMOS_OFFSET_MRC_SEED_CHK);
126 seed_checksum |= rtc_read8(dev, CMOS_OFFSET_MRC_SEED_CHK + 1) << 8;
Simon Glass428dfa42015-01-19 22:16:14 -0700127
128 if (checksum != seed_checksum) {
129 debug("%s: invalid seed checksum\n", __func__);
130 pei_data->scrambler_seed = 0;
131 pei_data->scrambler_seed_s3 = 0;
132 return -EINVAL;
133 }
134
135 return 0;
136}
137
138static int prepare_mrc_cache(struct pei_data *pei_data)
139{
140 struct mrc_data_container *mrc_cache;
141 struct fmap_entry entry;
142 int ret;
143
144 ret = read_seed_from_cmos(pei_data);
145 if (ret)
146 return ret;
Bin Mengf34c0592015-10-11 21:37:40 -0700147 ret = mrccache_get_region(NULL, &entry);
Simon Glass428dfa42015-01-19 22:16:14 -0700148 if (ret)
149 return ret;
150 mrc_cache = mrccache_find_current(&entry);
151 if (!mrc_cache)
152 return -ENOENT;
153
154 /*
155 * TODO(sjg@chromium.org): Skip this for now as it causes boot
156 * problems
157 */
158 if (0) {
159 pei_data->mrc_input = mrc_cache->data;
160 pei_data->mrc_input_len = mrc_cache->data_size;
161 }
162 debug("%s: at %p, size %x checksum %04x\n", __func__,
163 pei_data->mrc_input, pei_data->mrc_input_len,
164 mrc_cache->checksum);
165
166 return 0;
167}
168
Simon Glass428dfa42015-01-19 22:16:14 -0700169static int write_seeds_to_cmos(struct pei_data *pei_data)
170{
171 u16 c1, c2, checksum;
Bin Meng770fd332015-07-15 16:23:39 +0800172 struct udevice *dev;
173 int rcode = 0;
174
175 rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
176 if (rcode) {
177 debug("Cannot find RTC: err=%d\n", rcode);
178 return -ENODEV;
179 }
Simon Glass428dfa42015-01-19 22:16:14 -0700180
181 /* Save the MRC seed values to CMOS */
Bin Meng770fd332015-07-15 16:23:39 +0800182 rtc_write32(dev, CMOS_OFFSET_MRC_SEED, pei_data->scrambler_seed);
Simon Glass428dfa42015-01-19 22:16:14 -0700183 debug("Save scrambler seed 0x%08x to CMOS 0x%02x\n",
184 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
185
Bin Meng770fd332015-07-15 16:23:39 +0800186 rtc_write32(dev, CMOS_OFFSET_MRC_SEED_S3, pei_data->scrambler_seed_s3);
Simon Glass428dfa42015-01-19 22:16:14 -0700187 debug("Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
188 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
189
190 /* Save a simple checksum of the seed values */
191 c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed,
192 sizeof(u32));
193 c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3,
194 sizeof(u32));
195 checksum = add_ip_checksums(sizeof(u32), c1, c2);
196
Bin Meng770fd332015-07-15 16:23:39 +0800197 rtc_write8(dev, CMOS_OFFSET_MRC_SEED_CHK, checksum & 0xff);
198 rtc_write8(dev, CMOS_OFFSET_MRC_SEED_CHK + 1, (checksum >> 8) & 0xff);
Simon Glass428dfa42015-01-19 22:16:14 -0700199
200 return 0;
201}
202
Simon Glass428dfa42015-01-19 22:16:14 -0700203/* Use this hook to save our SDRAM parameters */
204int misc_init_r(void)
205{
206 int ret;
207
Bin Mengf34c0592015-10-11 21:37:40 -0700208 ret = mrccache_save();
Simon Glass428dfa42015-01-19 22:16:14 -0700209 if (ret)
210 printf("Unable to save MRC data: %d\n", ret);
211
212 return 0;
213}
214
Simon Glass268eefd2014-11-12 22:42:28 -0700215static const char *const ecc_decoder[] = {
216 "inactive",
217 "active on IO",
218 "disabled on IO",
219 "active"
220};
221
222/*
223 * Dump in the log memory controller configuration as read from the memory
224 * controller registers.
225 */
226static void report_memory_config(void)
227{
228 u32 addr_decoder_common, addr_decode_ch[2];
229 int i;
230
231 addr_decoder_common = readl(MCHBAR_REG(0x5000));
232 addr_decode_ch[0] = readl(MCHBAR_REG(0x5004));
233 addr_decode_ch[1] = readl(MCHBAR_REG(0x5008));
234
235 debug("memcfg DDR3 clock %d MHz\n",
236 (readl(MCHBAR_REG(0x5e04)) * 13333 * 2 + 50) / 100);
237 debug("memcfg channel assignment: A: %d, B % d, C % d\n",
238 addr_decoder_common & 3,
239 (addr_decoder_common >> 2) & 3,
240 (addr_decoder_common >> 4) & 3);
241
242 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
243 u32 ch_conf = addr_decode_ch[i];
244 debug("memcfg channel[%d] config (%8.8x):\n", i, ch_conf);
245 debug(" ECC %s\n", ecc_decoder[(ch_conf >> 24) & 3]);
246 debug(" enhanced interleave mode %s\n",
247 ((ch_conf >> 22) & 1) ? "on" : "off");
248 debug(" rank interleave %s\n",
249 ((ch_conf >> 21) & 1) ? "on" : "off");
250 debug(" DIMMA %d MB width x%d %s rank%s\n",
251 ((ch_conf >> 0) & 0xff) * 256,
252 ((ch_conf >> 19) & 1) ? 16 : 8,
253 ((ch_conf >> 17) & 1) ? "dual" : "single",
254 ((ch_conf >> 16) & 1) ? "" : ", selected");
255 debug(" DIMMB %d MB width x%d %s rank%s\n",
256 ((ch_conf >> 8) & 0xff) * 256,
257 ((ch_conf >> 20) & 1) ? 16 : 8,
258 ((ch_conf >> 18) & 1) ? "dual" : "single",
259 ((ch_conf >> 16) & 1) ? ", selected" : "");
260 }
261}
262
263static void post_system_agent_init(struct pei_data *pei_data)
264{
265 /* If PCIe init is skipped, set the PEG clock gating */
266 if (!pei_data->pcie_init)
267 setbits_le32(MCHBAR_REG(0x7010), 1);
268}
269
270static asmlinkage void console_tx_byte(unsigned char byte)
271{
272#ifdef DEBUG
273 putc(byte);
274#endif
275}
276
Simon Glass428dfa42015-01-19 22:16:14 -0700277static int recovery_mode_enabled(void)
278{
279 return false;
280}
281
Simon Glass268eefd2014-11-12 22:42:28 -0700282/**
283 * Find the PEI executable in the ROM and execute it.
284 *
285 * @param pei_data: configuration data for UEFI PEI reference code
286 */
287int sdram_initialise(struct pei_data *pei_data)
288{
289 unsigned version;
290 const char *data;
291 uint16_t done;
292 int ret;
293
294 report_platform_info();
295
296 /* Wait for ME to be ready */
297 ret = intel_early_me_init();
298 if (ret)
299 return ret;
300 ret = intel_early_me_uma_size();
301 if (ret < 0)
302 return ret;
303
304 debug("Starting UEFI PEI System Agent\n");
305
Simon Glass428dfa42015-01-19 22:16:14 -0700306 /*
307 * Do not pass MRC data in for recovery mode boot,
308 * Always pass it in for S3 resume.
309 */
310 if (!recovery_mode_enabled() ||
311 pei_data->boot_mode == PEI_BOOT_RESUME) {
312 ret = prepare_mrc_cache(pei_data);
313 if (ret)
314 debug("prepare_mrc_cache failed: %d\n", ret);
315 }
316
Simon Glass268eefd2014-11-12 22:42:28 -0700317 /* If MRC data is not found we cannot continue S3 resume. */
318 if (pei_data->boot_mode == PEI_BOOT_RESUME && !pei_data->mrc_input) {
319 debug("Giving up in sdram_initialize: No MRC data\n");
Simon Glass1375e9a2015-04-28 20:11:30 -0600320 reset_cpu(0);
Simon Glass268eefd2014-11-12 22:42:28 -0700321 }
322
323 /* Pass console handler in pei_data */
324 pei_data->tx_byte = console_tx_byte;
325
326 debug("PEI data at %p, size %x:\n", pei_data, sizeof(*pei_data));
327
Bin Meng293f4972014-12-17 15:50:42 +0800328 data = (char *)CONFIG_X86_MRC_ADDR;
Simon Glass268eefd2014-11-12 22:42:28 -0700329 if (data) {
330 int rv;
331 int (*func)(struct pei_data *);
332
333 debug("Calling MRC at %p\n", data);
334 post_code(POST_PRE_MRC);
335 func = (int (*)(struct pei_data *))data;
336 rv = func(pei_data);
337 post_code(POST_MRC);
338 if (rv) {
339 switch (rv) {
340 case -1:
341 printf("PEI version mismatch.\n");
342 break;
343 case -2:
344 printf("Invalid memory frequency.\n");
345 break;
346 default:
347 printf("MRC returned %x.\n", rv);
348 }
349 printf("Nonzero MRC return value.\n");
350 return -EFAULT;
351 }
352 } else {
353 printf("UEFI PEI System Agent not found.\n");
354 return -ENOSYS;
355 }
356
357#if CONFIG_USBDEBUG
358 /* mrc.bin reconfigures USB, so reinit it to have debug */
359 early_usbdebug_init();
360#endif
361
362 version = readl(MCHBAR_REG(0x5034));
363 debug("System Agent Version %d.%d.%d Build %d\n",
364 version >> 24 , (version >> 16) & 0xff,
365 (version >> 8) & 0xff, version & 0xff);
Simon Glass428dfa42015-01-19 22:16:14 -0700366 debug("MCR output data length %#x at %p\n", pei_data->mrc_output_len,
367 pei_data->mrc_output);
Simon Glass268eefd2014-11-12 22:42:28 -0700368
369 /*
370 * Send ME init done for SandyBridge here. This is done inside the
371 * SystemAgent binary on IvyBridge
372 */
Simon Glass240d06d2015-03-05 12:25:15 -0700373 done = x86_pci_read_config32(PCH_DEV, PCI_DEVICE_ID);
Simon Glass268eefd2014-11-12 22:42:28 -0700374 done &= BASE_REV_MASK;
375 if (BASE_REV_SNB == done)
376 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
377 else
378 intel_early_me_status();
379
380 post_system_agent_init(pei_data);
381 report_memory_config();
382
Simon Glass428dfa42015-01-19 22:16:14 -0700383 /* S3 resume: don't save scrambler seed or MRC data */
384 if (pei_data->boot_mode != PEI_BOOT_RESUME) {
385 /*
386 * This will be copied to SDRAM in reserve_arch(), then written
Bin Mengf34c0592015-10-11 21:37:40 -0700387 * to SPI flash in mrccache_save()
Simon Glass428dfa42015-01-19 22:16:14 -0700388 */
389 gd->arch.mrc_output = (char *)pei_data->mrc_output;
390 gd->arch.mrc_output_len = pei_data->mrc_output_len;
391 ret = write_seeds_to_cmos(pei_data);
392 if (ret)
393 debug("Failed to write seeds to CMOS: %d\n", ret);
394 }
395
396 return 0;
397}
398
399int reserve_arch(void)
400{
Bin Mengf34c0592015-10-11 21:37:40 -0700401 return mrccache_reserve();
Simon Glass268eefd2014-11-12 22:42:28 -0700402}
403
404static int copy_spd(struct pei_data *peid)
405{
406 const int gpio_vector[] = {41, 42, 43, 10, -1};
407 int spd_index;
408 const void *blob = gd->fdt_blob;
409 int node, spd_node;
410 int ret, i;
411
412 for (i = 0; ; i++) {
413 if (gpio_vector[i] == -1)
414 break;
415 ret = gpio_requestf(gpio_vector[i], "spd_id%d", i);
416 if (ret) {
417 debug("%s: Could not request gpio %d\n", __func__,
418 gpio_vector[i]);
419 return ret;
420 }
421 }
422 spd_index = gpio_get_values_as_int(gpio_vector);
423 debug("spd index %d\n", spd_index);
424 node = fdtdec_next_compatible(blob, 0, COMPAT_MEMORY_SPD);
425 if (node < 0) {
426 printf("SPD data not found.\n");
427 return -ENOENT;
428 }
429
430 for (spd_node = fdt_first_subnode(blob, node);
431 spd_node > 0;
432 spd_node = fdt_next_subnode(blob, spd_node)) {
433 const char *data;
434 int len;
435
436 if (fdtdec_get_int(blob, spd_node, "reg", -1) != spd_index)
437 continue;
438 data = fdt_getprop(blob, spd_node, "data", &len);
439 if (len < sizeof(peid->spd_data[0])) {
440 printf("Missing SPD data\n");
441 return -EINVAL;
442 }
443
444 debug("Using SDRAM SPD data for '%s'\n",
445 fdt_get_name(blob, spd_node, NULL));
446 memcpy(peid->spd_data[0], data, sizeof(peid->spd_data[0]));
447 break;
448 }
449
450 if (spd_node < 0) {
451 printf("No SPD data found for index %d\n", spd_index);
452 return -ENOENT;
453 }
454
455 return 0;
456}
457
458/**
459 * add_memory_area() - Add a new usable memory area to our list
460 *
461 * Note: @start and @end must not span the first 4GB boundary
462 *
463 * @info: Place to store memory info
464 * @start: Start of this memory area
465 * @end: End of this memory area + 1
466 */
467static int add_memory_area(struct memory_info *info,
468 uint64_t start, uint64_t end)
469{
470 struct memory_area *ptr;
471
472 if (info->num_areas == CONFIG_NR_DRAM_BANKS)
473 return -ENOSPC;
474
475 ptr = &info->area[info->num_areas];
476 ptr->start = start;
477 ptr->size = end - start;
478 info->total_memory += ptr->size;
479 if (ptr->start < (1ULL << 32))
480 info->total_32bit_memory += ptr->size;
481 debug("%d: memory %llx size %llx, total now %llx / %llx\n",
482 info->num_areas, ptr->start, ptr->size,
483 info->total_32bit_memory, info->total_memory);
484 info->num_areas++;
485
486 return 0;
487}
488
489/**
490 * sdram_find() - Find available memory
491 *
492 * This is a bit complicated since on x86 there are system memory holes all
493 * over the place. We create a list of available memory blocks
494 */
495static int sdram_find(pci_dev_t dev)
496{
497 struct memory_info *info = &gd->arch.meminfo;
498 uint32_t tseg_base, uma_size, tolud;
499 uint64_t tom, me_base, touud;
500 uint64_t uma_memory_base = 0;
501 uint64_t uma_memory_size;
502 unsigned long long tomk;
503 uint16_t ggc;
504
505 /* Total Memory 2GB example:
506 *
507 * 00000000 0000MB-1992MB 1992MB RAM (writeback)
508 * 7c800000 1992MB-2000MB 8MB TSEG (SMRR)
509 * 7d000000 2000MB-2002MB 2MB GFX GTT (uncached)
510 * 7d200000 2002MB-2034MB 32MB GFX UMA (uncached)
511 * 7f200000 2034MB TOLUD
512 * 7f800000 2040MB MEBASE
513 * 7f800000 2040MB-2048MB 8MB ME UMA (uncached)
514 * 80000000 2048MB TOM
515 * 100000000 4096MB-4102MB 6MB RAM (writeback)
516 *
517 * Total Memory 4GB example:
518 *
519 * 00000000 0000MB-2768MB 2768MB RAM (writeback)
520 * ad000000 2768MB-2776MB 8MB TSEG (SMRR)
521 * ad800000 2776MB-2778MB 2MB GFX GTT (uncached)
522 * ada00000 2778MB-2810MB 32MB GFX UMA (uncached)
523 * afa00000 2810MB TOLUD
524 * ff800000 4088MB MEBASE
525 * ff800000 4088MB-4096MB 8MB ME UMA (uncached)
526 * 100000000 4096MB TOM
527 * 100000000 4096MB-5374MB 1278MB RAM (writeback)
528 * 14fe00000 5368MB TOUUD
529 */
530
531 /* Top of Upper Usable DRAM, including remap */
Simon Glass240d06d2015-03-05 12:25:15 -0700532 touud = x86_pci_read_config32(dev, TOUUD+4);
Simon Glass268eefd2014-11-12 22:42:28 -0700533 touud <<= 32;
Simon Glass240d06d2015-03-05 12:25:15 -0700534 touud |= x86_pci_read_config32(dev, TOUUD);
Simon Glass268eefd2014-11-12 22:42:28 -0700535
536 /* Top of Lower Usable DRAM */
Simon Glass240d06d2015-03-05 12:25:15 -0700537 tolud = x86_pci_read_config32(dev, TOLUD);
Simon Glass268eefd2014-11-12 22:42:28 -0700538
539 /* Top of Memory - does not account for any UMA */
Simon Glass240d06d2015-03-05 12:25:15 -0700540 tom = x86_pci_read_config32(dev, 0xa4);
Simon Glass268eefd2014-11-12 22:42:28 -0700541 tom <<= 32;
Simon Glass240d06d2015-03-05 12:25:15 -0700542 tom |= x86_pci_read_config32(dev, 0xa0);
Simon Glass268eefd2014-11-12 22:42:28 -0700543
544 debug("TOUUD %llx TOLUD %08x TOM %llx\n", touud, tolud, tom);
545
546 /* ME UMA needs excluding if total memory <4GB */
Simon Glass240d06d2015-03-05 12:25:15 -0700547 me_base = x86_pci_read_config32(dev, 0x74);
Simon Glass268eefd2014-11-12 22:42:28 -0700548 me_base <<= 32;
Simon Glass240d06d2015-03-05 12:25:15 -0700549 me_base |= x86_pci_read_config32(dev, 0x70);
Simon Glass268eefd2014-11-12 22:42:28 -0700550
551 debug("MEBASE %llx\n", me_base);
552
553 /* TODO: Get rid of all this shifting by 10 bits */
554 tomk = tolud >> 10;
555 if (me_base == tolud) {
556 /* ME is from MEBASE-TOM */
557 uma_size = (tom - me_base) >> 10;
558 /* Increment TOLUD to account for ME as RAM */
559 tolud += uma_size << 10;
560 /* UMA starts at old TOLUD */
561 uma_memory_base = tomk * 1024ULL;
562 uma_memory_size = uma_size * 1024ULL;
563 debug("ME UMA base %llx size %uM\n", me_base, uma_size >> 10);
564 }
565
566 /* Graphics memory comes next */
Simon Glass240d06d2015-03-05 12:25:15 -0700567 ggc = x86_pci_read_config16(dev, GGC);
Simon Glass268eefd2014-11-12 22:42:28 -0700568 if (!(ggc & 2)) {
569 debug("IGD decoded, subtracting ");
570
571 /* Graphics memory */
572 uma_size = ((ggc >> 3) & 0x1f) * 32 * 1024ULL;
573 debug("%uM UMA", uma_size >> 10);
574 tomk -= uma_size;
575 uma_memory_base = tomk * 1024ULL;
576 uma_memory_size += uma_size * 1024ULL;
577
578 /* GTT Graphics Stolen Memory Size (GGMS) */
579 uma_size = ((ggc >> 8) & 0x3) * 1024ULL;
580 tomk -= uma_size;
581 uma_memory_base = tomk * 1024ULL;
582 uma_memory_size += uma_size * 1024ULL;
583 debug(" and %uM GTT\n", uma_size >> 10);
584 }
585
586 /* Calculate TSEG size from its base which must be below GTT */
Simon Glass240d06d2015-03-05 12:25:15 -0700587 tseg_base = x86_pci_read_config32(dev, 0xb8);
Simon Glass268eefd2014-11-12 22:42:28 -0700588 uma_size = (uma_memory_base - tseg_base) >> 10;
589 tomk -= uma_size;
590 uma_memory_base = tomk * 1024ULL;
591 uma_memory_size += uma_size * 1024ULL;
592 debug("TSEG base 0x%08x size %uM\n", tseg_base, uma_size >> 10);
593
594 debug("Available memory below 4GB: %lluM\n", tomk >> 10);
595
596 /* Report the memory regions */
597 add_memory_area(info, 1 << 20, 2 << 28);
598 add_memory_area(info, (2 << 28) + (2 << 20), 4 << 28);
599 add_memory_area(info, (4 << 28) + (2 << 20), tseg_base);
600 add_memory_area(info, 1ULL << 32, touud);
Simon Glassa754b952015-01-01 16:18:10 -0700601
602 /* Add MTRRs for memory */
603 mtrr_add_request(MTRR_TYPE_WRBACK, 0, 2ULL << 30);
604 mtrr_add_request(MTRR_TYPE_WRBACK, 2ULL << 30, 512 << 20);
605 mtrr_add_request(MTRR_TYPE_WRBACK, 0xaULL << 28, 256 << 20);
606 mtrr_add_request(MTRR_TYPE_UNCACHEABLE, tseg_base, 16 << 20);
607 mtrr_add_request(MTRR_TYPE_UNCACHEABLE, tseg_base + (16 << 20),
608 32 << 20);
609
Simon Glass268eefd2014-11-12 22:42:28 -0700610 /*
611 * If >= 4GB installed then memory from TOLUD to 4GB
612 * is remapped above TOM, TOUUD will account for both
613 */
614 if (touud > (1ULL << 32ULL)) {
615 debug("Available memory above 4GB: %lluM\n",
616 (touud >> 20) - 4096);
617 }
618
619 return 0;
620}
621
622static void rcba_config(void)
623{
624 /*
625 * GFX INTA -> PIRQA (MSI)
626 * D28IP_P3IP WLAN INTA -> PIRQB
627 * D29IP_E1P EHCI1 INTA -> PIRQD
628 * D26IP_E2P EHCI2 INTA -> PIRQF
629 * D31IP_SIP SATA INTA -> PIRQF (MSI)
630 * D31IP_SMIP SMBUS INTB -> PIRQH
631 * D31IP_TTIP THRT INTC -> PIRQA
632 * D27IP_ZIP HDA INTA -> PIRQA (MSI)
633 *
634 * TRACKPAD -> PIRQE (Edge Triggered)
635 * TOUCHSCREEN -> PIRQG (Edge Triggered)
636 */
637
638 /* Device interrupt pin register (board specific) */
639 writel((INTC << D31IP_TTIP) | (NOINT << D31IP_SIP2) |
640 (INTB << D31IP_SMIP) | (INTA << D31IP_SIP), RCB_REG(D31IP));
641 writel(NOINT << D30IP_PIP, RCB_REG(D30IP));
642 writel(INTA << D29IP_E1P, RCB_REG(D29IP));
643 writel(INTA << D28IP_P3IP, RCB_REG(D28IP));
644 writel(INTA << D27IP_ZIP, RCB_REG(D27IP));
645 writel(INTA << D26IP_E2P, RCB_REG(D26IP));
646 writel(NOINT << D25IP_LIP, RCB_REG(D25IP));
647 writel(NOINT << D22IP_MEI1IP, RCB_REG(D22IP));
648
649 /* Device interrupt route registers */
650 writel(DIR_ROUTE(PIRQB, PIRQH, PIRQA, PIRQC), RCB_REG(D31IR));
651 writel(DIR_ROUTE(PIRQD, PIRQE, PIRQF, PIRQG), RCB_REG(D29IR));
652 writel(DIR_ROUTE(PIRQB, PIRQC, PIRQD, PIRQE), RCB_REG(D28IR));
653 writel(DIR_ROUTE(PIRQA, PIRQH, PIRQA, PIRQB), RCB_REG(D27IR));
654 writel(DIR_ROUTE(PIRQF, PIRQE, PIRQG, PIRQH), RCB_REG(D26IR));
655 writel(DIR_ROUTE(PIRQA, PIRQB, PIRQC, PIRQD), RCB_REG(D25IR));
656 writel(DIR_ROUTE(PIRQA, PIRQB, PIRQC, PIRQD), RCB_REG(D22IR));
657
658 /* Enable IOAPIC (generic) */
659 writew(0x0100, RCB_REG(OIC));
660 /* PCH BWG says to read back the IOAPIC enable register */
661 (void)readw(RCB_REG(OIC));
662
663 /* Disable unused devices (board specific) */
664 setbits_le32(RCB_REG(FD), PCH_DISABLE_ALWAYS);
665}
Simon Glass0b36ecd2014-11-12 22:42:07 -0700666
667int dram_init(void)
668{
Simon Glass268eefd2014-11-12 22:42:28 -0700669 struct pei_data pei_data __aligned(8) = {
670 .pei_version = PEI_VERSION,
671 .mchbar = DEFAULT_MCHBAR,
672 .dmibar = DEFAULT_DMIBAR,
673 .epbar = DEFAULT_EPBAR,
Simon Glass461cebf2015-01-27 22:13:33 -0700674 .pciexbar = CONFIG_PCIE_ECAM_BASE,
Simon Glass268eefd2014-11-12 22:42:28 -0700675 .smbusbar = SMBUS_IO_BASE,
676 .wdbbar = 0x4000000,
677 .wdbsize = 0x1000,
678 .hpet_address = CONFIG_HPET_ADDRESS,
679 .rcba = DEFAULT_RCBABASE,
680 .pmbase = DEFAULT_PMBASE,
681 .gpiobase = DEFAULT_GPIOBASE,
682 .thermalbase = 0xfed08000,
683 .system_type = 0, /* 0 Mobile, 1 Desktop/Server */
684 .tseg_size = CONFIG_SMM_TSEG_SIZE,
685 .ts_addresses = { 0x00, 0x00, 0x00, 0x00 },
686 .ec_present = 1,
687 .ddr3lv_support = 1,
688 /*
689 * 0 = leave channel enabled
690 * 1 = disable dimm 0 on channel
691 * 2 = disable dimm 1 on channel
692 * 3 = disable dimm 0+1 on channel
693 */
694 .dimm_channel0_disabled = 2,
695 .dimm_channel1_disabled = 2,
696 .max_ddr3_freq = 1600,
697 .usb_port_config = {
698 /*
699 * Empty and onboard Ports 0-7, set to un-used pin
700 * OC3
701 */
702 { 0, 3, 0x0000 }, /* P0= Empty */
703 { 1, 0, 0x0040 }, /* P1= Left USB 1 (OC0) */
704 { 1, 1, 0x0040 }, /* P2= Left USB 2 (OC1) */
705 { 1, 3, 0x0040 }, /* P3= SDCARD (no OC) */
706 { 0, 3, 0x0000 }, /* P4= Empty */
707 { 1, 3, 0x0040 }, /* P5= WWAN (no OC) */
708 { 0, 3, 0x0000 }, /* P6= Empty */
709 { 0, 3, 0x0000 }, /* P7= Empty */
710 /*
711 * Empty and onboard Ports 8-13, set to un-used pin
712 * OC4
713 */
714 { 1, 4, 0x0040 }, /* P8= Camera (no OC) */
715 { 1, 4, 0x0040 }, /* P9= Bluetooth (no OC) */
716 { 0, 4, 0x0000 }, /* P10= Empty */
717 { 0, 4, 0x0000 }, /* P11= Empty */
718 { 0, 4, 0x0000 }, /* P12= Empty */
719 { 0, 4, 0x0000 }, /* P13= Empty */
720 },
721 };
722 pci_dev_t dev = PCI_BDF(0, 0, 0);
723 int ret;
724
725 debug("Boot mode %d\n", gd->arch.pei_boot_mode);
726 debug("mcr_input %p\n", pei_data.mrc_input);
727 pei_data.boot_mode = gd->arch.pei_boot_mode;
728 ret = copy_spd(&pei_data);
729 if (!ret)
730 ret = sdram_initialise(&pei_data);
731 if (ret)
732 return ret;
733
734 rcba_config();
735 quick_ram_check();
736
737 writew(0xCAFE, MCHBAR_REG(SSKPD));
738
739 post_code(POST_DRAM);
740
741 ret = sdram_find(dev);
742 if (ret)
743 return ret;
744
745 gd->ram_size = gd->arch.meminfo.total_32bit_memory;
Simon Glass0b36ecd2014-11-12 22:42:07 -0700746
747 return 0;
748}