puma-rk3399.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <environment.h>
  8. #include <misc.h>
  9. #include <spl.h>
  10. #include <syscon.h>
  11. #include <usb.h>
  12. #include <dm/pinctrl.h>
  13. #include <dm/uclass-internal.h>
  14. #include <asm/io.h>
  15. #include <asm/gpio.h>
  16. #include <asm/setup.h>
  17. #include <asm/arch/clock.h>
  18. #include <asm/arch/cru_rk3399.h>
  19. #include <asm/arch/hardware.h>
  20. #include <asm/arch/grf_rk3399.h>
  21. #include <asm/arch/periph.h>
  22. #include <power/regulator.h>
  23. #include <u-boot/sha256.h>
  24. int board_init(void)
  25. {
  26. int ret;
  27. /*
  28. * We need to call into regulators_enable_boot_on() again, as the call
  29. * during SPL may have not included all regulators.
  30. */
  31. ret = regulators_enable_boot_on(false);
  32. if (ret)
  33. debug("%s: Cannot enable boot on regulator\n", __func__);
  34. return 0;
  35. }
  36. static void rk3399_force_power_on_reset(void)
  37. {
  38. ofnode node;
  39. struct gpio_desc sysreset_gpio;
  40. debug("%s: trying to force a power-on reset\n", __func__);
  41. node = ofnode_path("/config");
  42. if (!ofnode_valid(node)) {
  43. debug("%s: no /config node?\n", __func__);
  44. return;
  45. }
  46. if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0,
  47. &sysreset_gpio, GPIOD_IS_OUT)) {
  48. debug("%s: could not find a /config/sysreset-gpio\n", __func__);
  49. return;
  50. }
  51. dm_gpio_set_value(&sysreset_gpio, 1);
  52. }
  53. void spl_board_init(void)
  54. {
  55. int ret;
  56. struct rk3399_cru *cru = rockchip_get_cru();
  57. /*
  58. * The RK3399 resets only 'almost all logic' (see also in the TRM
  59. * "3.9.4 Global software reset"), when issuing a software reset.
  60. * This may cause issues during boot-up for some configurations of
  61. * the application software stack.
  62. *
  63. * To work around this, we test whether the last reset reason was
  64. * a power-on reset and (if not) issue an overtemp-reset to reset
  65. * the entire module.
  66. *
  67. * While this was previously fixed by modifying the various places
  68. * that could generate a software reset (e.g. U-Boot's sysreset
  69. * driver, the ATF or Linux), we now have it here to ensure that
  70. * we no longer have to track this through the various components.
  71. */
  72. if (cru->glb_rst_st != 0)
  73. rk3399_force_power_on_reset();
  74. /*
  75. * Turning the eMMC and SPI back on (if disabled via the Qseven
  76. * BIOS_ENABLE) signal is done through a always-on regulator).
  77. */
  78. ret = regulators_enable_boot_on(false);
  79. if (ret)
  80. debug("%s: Cannot enable boot on regulator\n", __func__);
  81. preloader_console_init();
  82. }
  83. static void setup_macaddr(void)
  84. {
  85. #if CONFIG_IS_ENABLED(CMD_NET)
  86. int ret;
  87. const char *cpuid = env_get("cpuid#");
  88. u8 hash[SHA256_SUM_LEN];
  89. int size = sizeof(hash);
  90. u8 mac_addr[6];
  91. /* Only generate a MAC address, if none is set in the environment */
  92. if (env_get("ethaddr"))
  93. return;
  94. if (!cpuid) {
  95. debug("%s: could not retrieve 'cpuid#'\n", __func__);
  96. return;
  97. }
  98. ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
  99. if (ret) {
  100. debug("%s: failed to calculate SHA256\n", __func__);
  101. return;
  102. }
  103. /* Copy 6 bytes of the hash to base the MAC address on */
  104. memcpy(mac_addr, hash, 6);
  105. /* Make this a valid MAC address and set it */
  106. mac_addr[0] &= 0xfe; /* clear multicast bit */
  107. mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
  108. eth_env_set_enetaddr("ethaddr", mac_addr);
  109. #endif
  110. }
  111. static void setup_serial(void)
  112. {
  113. #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
  114. const u32 cpuid_offset = 0x7;
  115. const u32 cpuid_length = 0x10;
  116. struct udevice *dev;
  117. int ret, i;
  118. u8 cpuid[cpuid_length];
  119. u8 low[cpuid_length/2], high[cpuid_length/2];
  120. char cpuid_str[cpuid_length * 2 + 1];
  121. u64 serialno;
  122. char serialno_str[17];
  123. /* retrieve the device */
  124. ret = uclass_get_device_by_driver(UCLASS_MISC,
  125. DM_GET_DRIVER(rockchip_efuse), &dev);
  126. if (ret) {
  127. debug("%s: could not find efuse device\n", __func__);
  128. return;
  129. }
  130. /* read the cpu_id range from the efuses */
  131. ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid));
  132. if (ret) {
  133. debug("%s: reading cpuid from the efuses failed\n",
  134. __func__);
  135. return;
  136. }
  137. memset(cpuid_str, 0, sizeof(cpuid_str));
  138. for (i = 0; i < 16; i++)
  139. sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
  140. debug("cpuid: %s\n", cpuid_str);
  141. /*
  142. * Mix the cpuid bytes using the same rules as in
  143. * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
  144. */
  145. for (i = 0; i < 8; i++) {
  146. low[i] = cpuid[1 + (i << 1)];
  147. high[i] = cpuid[i << 1];
  148. }
  149. serialno = crc32_no_comp(0, low, 8);
  150. serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
  151. snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno);
  152. env_set("cpuid#", cpuid_str);
  153. env_set("serial#", serialno_str);
  154. #endif
  155. }
  156. static void setup_iodomain(void)
  157. {
  158. const u32 GRF_IO_VSEL_GPIO4CD_SHIFT = 3;
  159. struct rk3399_grf_regs *grf =
  160. syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
  161. /*
  162. * Set bit 3 in GRF_IO_VSEL so PCIE_RST# works (pin GPIO4_C6).
  163. * Linux assumes that PCIE_RST# works out of the box as it probes
  164. * PCIe before loading the iodomain driver.
  165. */
  166. rk_setreg(&grf->io_vsel, 1 << GRF_IO_VSEL_GPIO4CD_SHIFT);
  167. }
  168. int misc_init_r(void)
  169. {
  170. setup_serial();
  171. setup_macaddr();
  172. setup_iodomain();
  173. return 0;
  174. }
  175. #ifdef CONFIG_SERIAL_TAG
  176. void get_board_serial(struct tag_serialnr *serialnr)
  177. {
  178. char *serial_string;
  179. u64 serial = 0;
  180. serial_string = env_get("serial#");
  181. if (serial_string)
  182. serial = simple_strtoull(serial_string, NULL, 16);
  183. serialnr->high = (u32)(serial >> 32);
  184. serialnr->low = (u32)(serial & 0xffffffff);
  185. }
  186. #endif
  187. /**
  188. * Switch power at an external regulator (for our root hub).
  189. *
  190. * @param ctrl pointer to the xHCI controller
  191. * @param port port number as in the control message (one-based)
  192. * @param enable boolean indicating whether to enable or disable power
  193. * @return returns 0 on success, an error-code on failure
  194. */
  195. static int board_usb_port_power_set(struct udevice *dev, int port,
  196. bool enable)
  197. {
  198. #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR)
  199. /* We start counting ports at 0, while USB counts from 1. */
  200. int index = port - 1;
  201. const char *regname = NULL;
  202. struct udevice *regulator;
  203. const char *prop = "tsd,usb-port-power";
  204. int ret;
  205. debug("%s: ctrl '%s' port %d enable %s\n", __func__,
  206. dev_read_name(dev), port, enable ? "true" : "false");
  207. ret = dev_read_string_index(dev, prop, index, &regname);
  208. if (ret < 0) {
  209. debug("%s: ctrl '%s' port %d: no entry in '%s'\n",
  210. __func__, dev_read_name(dev), port, prop);
  211. return ret;
  212. }
  213. ret = regulator_get_by_platname(regname, &regulator);
  214. if (ret) {
  215. debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n",
  216. __func__, dev_read_name(dev), port, regname);
  217. return ret;
  218. }
  219. regulator_set_enable(regulator, enable);
  220. return 0;
  221. #else
  222. return -ENOTSUPP;
  223. #endif
  224. }
  225. void usb_hub_reset_devices(struct usb_hub_device *hub, int port)
  226. {
  227. struct udevice *dev = hub->pusb_dev->dev;
  228. struct udevice *ctrl;
  229. /* We are only interested in our root-hubs */
  230. if (usb_hub_is_root_hub(dev) == false)
  231. return;
  232. ctrl = usb_get_bus(dev);
  233. if (!ctrl) {
  234. debug("%s: could not retrieve ctrl for hub\n", __func__);
  235. return;
  236. }
  237. /*
  238. * To work around an incompatibility between the single-threaded
  239. * USB stack in U-Boot and (a strange low-power mode of) the USB
  240. * hub we have on-module, we need to delay powering on the hub
  241. * until the first time the port is probed.
  242. */
  243. board_usb_port_power_set(ctrl, port, true);
  244. }