puma-rk3399.c 6.7 KB

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