sdram.c 22 KB

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