sdram.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2016 Google, Inc
  3. *
  4. * From coreboot src/soc/intel/broadwell/romstage/raminit.c
  5. *
  6. * SPDX-License-Identifier: GPL-2.0
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <pci.h>
  11. #include <syscon.h>
  12. #include <asm/cpu.h>
  13. #include <asm/io.h>
  14. #include <asm/lpc_common.h>
  15. #include <asm/mrccache.h>
  16. #include <asm/mrc_common.h>
  17. #include <asm/mtrr.h>
  18. #include <asm/pci.h>
  19. #include <asm/arch/iomap.h>
  20. #include <asm/arch/me.h>
  21. #include <asm/arch/pch.h>
  22. #include <asm/arch/pei_data.h>
  23. #include <asm/arch/pm.h>
  24. ulong board_get_usable_ram_top(ulong total_size)
  25. {
  26. return mrc_common_board_get_usable_ram_top(total_size);
  27. }
  28. int dram_init_banksize(void)
  29. {
  30. mrc_common_dram_init_banksize();
  31. return 0;
  32. }
  33. void broadwell_fill_pei_data(struct pei_data *pei_data)
  34. {
  35. pei_data->pei_version = PEI_VERSION;
  36. pei_data->board_type = BOARD_TYPE_ULT;
  37. pei_data->pciexbar = MCFG_BASE_ADDRESS;
  38. pei_data->smbusbar = SMBUS_BASE_ADDRESS;
  39. pei_data->ehcibar = EARLY_EHCI_BAR;
  40. pei_data->xhcibar = EARLY_XHCI_BAR;
  41. pei_data->gttbar = EARLY_GTT_BAR;
  42. pei_data->pmbase = ACPI_BASE_ADDRESS;
  43. pei_data->gpiobase = GPIO_BASE_ADDRESS;
  44. pei_data->tseg_size = CONFIG_SMM_TSEG_SIZE;
  45. pei_data->temp_mmio_base = EARLY_TEMP_MMIO;
  46. pei_data->tx_byte = sdram_console_tx_byte;
  47. pei_data->ddr_refresh_2x = 1;
  48. }
  49. static inline void pei_data_usb2_port(struct pei_data *pei_data, int port,
  50. uint16_t length, uint8_t enable,
  51. uint8_t oc_pin, uint8_t location)
  52. {
  53. pei_data->usb2_ports[port].length = length;
  54. pei_data->usb2_ports[port].enable = enable;
  55. pei_data->usb2_ports[port].oc_pin = oc_pin;
  56. pei_data->usb2_ports[port].location = location;
  57. }
  58. static inline void pei_data_usb3_port(struct pei_data *pei_data, int port,
  59. uint8_t enable, uint8_t oc_pin,
  60. uint8_t fixed_eq)
  61. {
  62. pei_data->usb3_ports[port].enable = enable;
  63. pei_data->usb3_ports[port].oc_pin = oc_pin;
  64. pei_data->usb3_ports[port].fixed_eq = fixed_eq;
  65. }
  66. void mainboard_fill_pei_data(struct pei_data *pei_data)
  67. {
  68. /* DQ byte map for Samus board */
  69. const u8 dq_map[2][6][2] = {
  70. { { 0x0F, 0xF0 }, { 0x00, 0xF0 }, { 0x0F, 0xF0 },
  71. { 0x0F, 0x00 }, { 0xFF, 0x00 }, { 0xFF, 0x00 } },
  72. { { 0x0F, 0xF0 }, { 0x00, 0xF0 }, { 0x0F, 0xF0 },
  73. { 0x0F, 0x00 }, { 0xFF, 0x00 }, { 0xFF, 0x00 } } };
  74. /* DQS CPU<>DRAM map for Samus board */
  75. const u8 dqs_map[2][8] = {
  76. { 2, 0, 1, 3, 6, 4, 7, 5 },
  77. { 2, 1, 0, 3, 6, 5, 4, 7 } };
  78. pei_data->ec_present = 1;
  79. /* One installed DIMM per channel */
  80. pei_data->dimm_channel0_disabled = 2;
  81. pei_data->dimm_channel1_disabled = 2;
  82. memcpy(pei_data->dq_map, dq_map, sizeof(dq_map));
  83. memcpy(pei_data->dqs_map, dqs_map, sizeof(dqs_map));
  84. /* P0: HOST PORT */
  85. pei_data_usb2_port(pei_data, 0, 0x0080, 1, 0,
  86. USB_PORT_BACK_PANEL);
  87. /* P1: HOST PORT */
  88. pei_data_usb2_port(pei_data, 1, 0x0080, 1, 1,
  89. USB_PORT_BACK_PANEL);
  90. /* P2: RAIDEN */
  91. pei_data_usb2_port(pei_data, 2, 0x0080, 1, USB_OC_PIN_SKIP,
  92. USB_PORT_BACK_PANEL);
  93. /* P3: SD CARD */
  94. pei_data_usb2_port(pei_data, 3, 0x0040, 1, USB_OC_PIN_SKIP,
  95. USB_PORT_INTERNAL);
  96. /* P4: RAIDEN */
  97. pei_data_usb2_port(pei_data, 4, 0x0080, 1, USB_OC_PIN_SKIP,
  98. USB_PORT_BACK_PANEL);
  99. /* P5: WWAN (Disabled) */
  100. pei_data_usb2_port(pei_data, 5, 0x0000, 0, USB_OC_PIN_SKIP,
  101. USB_PORT_SKIP);
  102. /* P6: CAMERA */
  103. pei_data_usb2_port(pei_data, 6, 0x0040, 1, USB_OC_PIN_SKIP,
  104. USB_PORT_INTERNAL);
  105. /* P7: BT */
  106. pei_data_usb2_port(pei_data, 7, 0x0040, 1, USB_OC_PIN_SKIP,
  107. USB_PORT_INTERNAL);
  108. /* P1: HOST PORT */
  109. pei_data_usb3_port(pei_data, 0, 1, 0, 0);
  110. /* P2: HOST PORT */
  111. pei_data_usb3_port(pei_data, 1, 1, 1, 0);
  112. /* P3: RAIDEN */
  113. pei_data_usb3_port(pei_data, 2, 1, USB_OC_PIN_SKIP, 0);
  114. /* P4: RAIDEN */
  115. pei_data_usb3_port(pei_data, 3, 1, USB_OC_PIN_SKIP, 0);
  116. }
  117. static unsigned long get_top_of_ram(struct udevice *dev)
  118. {
  119. /*
  120. * Base of DPR is top of usable DRAM below 4GiB. The register has
  121. * 1 MiB alignment and reports the TOP of the range, the base
  122. * must be calculated from the size in MiB in bits 11:4.
  123. */
  124. u32 dpr, tom;
  125. dm_pci_read_config32(dev, DPR, &dpr);
  126. tom = dpr & ~((1 << 20) - 1);
  127. debug("dpt %08x tom %08x\n", dpr, tom);
  128. /* Subtract DMA Protected Range size if enabled */
  129. if (dpr & DPR_EPM)
  130. tom -= (dpr & DPR_SIZE_MASK) << 16;
  131. return (unsigned long)tom;
  132. }
  133. /**
  134. * sdram_find() - Find available memory
  135. *
  136. * This is a bit complicated since on x86 there are system memory holes all
  137. * over the place. We create a list of available memory blocks
  138. *
  139. * @dev: Northbridge device
  140. */
  141. static int sdram_find(struct udevice *dev)
  142. {
  143. struct memory_info *info = &gd->arch.meminfo;
  144. ulong top_of_ram;
  145. top_of_ram = get_top_of_ram(dev);
  146. mrc_add_memory_area(info, 0, top_of_ram);
  147. /* Add MTRRs for memory */
  148. mtrr_add_request(MTRR_TYPE_WRBACK, 0, 2ULL << 30);
  149. return 0;
  150. }
  151. static int prepare_mrc_cache(struct pei_data *pei_data)
  152. {
  153. struct mrc_data_container *mrc_cache;
  154. struct mrc_region entry;
  155. int ret;
  156. ret = mrccache_get_region(NULL, &entry);
  157. if (ret)
  158. return ret;
  159. mrc_cache = mrccache_find_current(&entry);
  160. if (!mrc_cache)
  161. return -ENOENT;
  162. pei_data->saved_data = mrc_cache->data;
  163. pei_data->saved_data_size = mrc_cache->data_size;
  164. debug("%s: at %p, size %x checksum %04x\n", __func__,
  165. pei_data->saved_data, pei_data->saved_data_size,
  166. mrc_cache->checksum);
  167. return 0;
  168. }
  169. int dram_init(void)
  170. {
  171. struct pei_data _pei_data __aligned(8);
  172. struct pei_data *pei_data = &_pei_data;
  173. struct udevice *dev, *me_dev, *pch_dev;
  174. struct chipset_power_state ps;
  175. const void *spd_data;
  176. int ret, size;
  177. memset(pei_data, '\0', sizeof(struct pei_data));
  178. /* Print ME state before MRC */
  179. ret = syscon_get_by_driver_data(X86_SYSCON_ME, &me_dev);
  180. if (ret)
  181. return ret;
  182. intel_me_status(me_dev);
  183. /* Save ME HSIO version */
  184. ret = uclass_first_device(UCLASS_PCH, &pch_dev);
  185. if (ret)
  186. return ret;
  187. if (!pch_dev)
  188. return -ENODEV;
  189. power_state_get(pch_dev, &ps);
  190. intel_me_hsio_version(me_dev, &ps.hsio_version, &ps.hsio_checksum);
  191. broadwell_fill_pei_data(pei_data);
  192. mainboard_fill_pei_data(pei_data);
  193. ret = uclass_first_device(UCLASS_NORTHBRIDGE, &dev);
  194. if (ret)
  195. return ret;
  196. if (!dev)
  197. return -ENODEV;
  198. size = 256;
  199. ret = mrc_locate_spd(dev, size, &spd_data);
  200. if (ret)
  201. return ret;
  202. memcpy(pei_data->spd_data[0][0], spd_data, size);
  203. memcpy(pei_data->spd_data[1][0], spd_data, size);
  204. ret = prepare_mrc_cache(pei_data);
  205. if (ret)
  206. debug("prepare_mrc_cache failed: %d\n", ret);
  207. debug("PEI version %#x\n", pei_data->pei_version);
  208. ret = mrc_common_init(dev, pei_data, true);
  209. if (ret)
  210. return ret;
  211. debug("Memory init done\n");
  212. ret = sdram_find(dev);
  213. if (ret)
  214. return ret;
  215. gd->ram_size = gd->arch.meminfo.total_32bit_memory;
  216. debug("RAM size %llx\n", (unsigned long long)gd->ram_size);
  217. debug("MRC output data length %#x at %p\n", pei_data->data_to_save_size,
  218. pei_data->data_to_save);
  219. /* S3 resume: don't save scrambler seed or MRC data */
  220. if (pei_data->boot_mode != SLEEP_STATE_S3) {
  221. /*
  222. * This will be copied to SDRAM in reserve_arch(), then written
  223. * to SPI flash in mrccache_save()
  224. */
  225. gd->arch.mrc_output = (char *)pei_data->data_to_save;
  226. gd->arch.mrc_output_len = pei_data->data_to_save_size;
  227. }
  228. gd->arch.pei_meminfo = pei_data->meminfo;
  229. return 0;
  230. }
  231. /* Use this hook to save our SDRAM parameters */
  232. int misc_init_r(void)
  233. {
  234. int ret;
  235. ret = mrccache_save();
  236. if (ret)
  237. printf("Unable to save MRC data: %d\n", ret);
  238. else
  239. debug("Saved MRC cache data\n");
  240. return 0;
  241. }
  242. void board_debug_uart_init(void)
  243. {
  244. struct udevice *bus = NULL;
  245. /* com1 / com2 decode range */
  246. pci_x86_write_config(bus, PCH_DEV_LPC, LPC_IO_DEC, 1 << 4, PCI_SIZE_16);
  247. pci_x86_write_config(bus, PCH_DEV_LPC, LPC_EN, COMA_LPC_EN,
  248. PCI_SIZE_16);
  249. }
  250. static const struct udevice_id broadwell_syscon_ids[] = {
  251. { .compatible = "intel,me", .data = X86_SYSCON_ME },
  252. { }
  253. };
  254. U_BOOT_DRIVER(syscon_intel_me) = {
  255. .name = "intel_me_syscon",
  256. .id = UCLASS_SYSCON,
  257. .of_match = broadwell_syscon_ids,
  258. };