sdram.c 21 KB

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