puma-rk3399.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <env.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/setup.h>
  16. #include <asm/arch-rockchip/clock.h>
  17. #include <asm/arch-rockchip/hardware.h>
  18. #include <asm/arch-rockchip/grf_rk3399.h>
  19. #include <asm/arch-rockchip/periph.h>
  20. #include <power/regulator.h>
  21. #include <u-boot/sha256.h>
  22. static void setup_macaddr(void)
  23. {
  24. #if CONFIG_IS_ENABLED(CMD_NET)
  25. int ret;
  26. const char *cpuid = env_get("cpuid#");
  27. u8 hash[SHA256_SUM_LEN];
  28. int size = sizeof(hash);
  29. u8 mac_addr[6];
  30. /* Only generate a MAC address, if none is set in the environment */
  31. if (env_get("ethaddr"))
  32. return;
  33. if (!cpuid) {
  34. debug("%s: could not retrieve 'cpuid#'\n", __func__);
  35. return;
  36. }
  37. ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
  38. if (ret) {
  39. debug("%s: failed to calculate SHA256\n", __func__);
  40. return;
  41. }
  42. /* Copy 6 bytes of the hash to base the MAC address on */
  43. memcpy(mac_addr, hash, 6);
  44. /* Make this a valid MAC address and set it */
  45. mac_addr[0] &= 0xfe; /* clear multicast bit */
  46. mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
  47. eth_env_set_enetaddr("ethaddr", mac_addr);
  48. #endif
  49. }
  50. static void setup_serial(void)
  51. {
  52. #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
  53. const u32 cpuid_offset = 0x7;
  54. const u32 cpuid_length = 0x10;
  55. struct udevice *dev;
  56. int ret, i;
  57. u8 cpuid[cpuid_length];
  58. u8 low[cpuid_length/2], high[cpuid_length/2];
  59. char cpuid_str[cpuid_length * 2 + 1];
  60. u64 serialno;
  61. char serialno_str[17];
  62. /* retrieve the device */
  63. ret = uclass_get_device_by_driver(UCLASS_MISC,
  64. DM_GET_DRIVER(rockchip_efuse), &dev);
  65. if (ret) {
  66. debug("%s: could not find efuse device\n", __func__);
  67. return;
  68. }
  69. /* read the cpu_id range from the efuses */
  70. ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid));
  71. if (ret) {
  72. debug("%s: reading cpuid from the efuses failed\n",
  73. __func__);
  74. return;
  75. }
  76. memset(cpuid_str, 0, sizeof(cpuid_str));
  77. for (i = 0; i < 16; i++)
  78. sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
  79. debug("cpuid: %s\n", cpuid_str);
  80. /*
  81. * Mix the cpuid bytes using the same rules as in
  82. * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
  83. */
  84. for (i = 0; i < 8; i++) {
  85. low[i] = cpuid[1 + (i << 1)];
  86. high[i] = cpuid[i << 1];
  87. }
  88. serialno = crc32_no_comp(0, low, 8);
  89. serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
  90. snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno);
  91. env_set("cpuid#", cpuid_str);
  92. env_set("serial#", serialno_str);
  93. #endif
  94. }
  95. static void setup_iodomain(void)
  96. {
  97. const u32 GRF_IO_VSEL_GPIO4CD_SHIFT = 3;
  98. struct rk3399_grf_regs *grf =
  99. syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
  100. /*
  101. * Set bit 3 in GRF_IO_VSEL so PCIE_RST# works (pin GPIO4_C6).
  102. * Linux assumes that PCIE_RST# works out of the box as it probes
  103. * PCIe before loading the iodomain driver.
  104. */
  105. rk_setreg(&grf->io_vsel, 1 << GRF_IO_VSEL_GPIO4CD_SHIFT);
  106. }
  107. /*
  108. * Swap mmc0 and mmc1 in boot_targets if booted from SD-Card.
  109. *
  110. * If bootsource is uSD-card we can assume that we want to use the
  111. * SD-Card instead of the eMMC as first boot_target for distroboot.
  112. * We only want to swap the defaults and not any custom environment a
  113. * user has set. We exit early if a changed boot_targets environment
  114. * is detected.
  115. */
  116. static int setup_boottargets(void)
  117. {
  118. const char *boot_device =
  119. ofnode_get_chosen_prop("u-boot,spl-boot-device");
  120. char *env_default, *env;
  121. if (!boot_device) {
  122. debug("%s: /chosen/u-boot,spl-boot-device not set\n",
  123. __func__);
  124. return -1;
  125. }
  126. debug("%s: booted from %s\n", __func__, boot_device);
  127. env_default = env_get_default("boot_targets");
  128. env = env_get("boot_targets");
  129. if (!env) {
  130. debug("%s: boot_targets does not exist\n", __func__);
  131. return -1;
  132. }
  133. debug("%s: boot_targets current: %s - default: %s\n",
  134. __func__, env, env_default);
  135. if (strcmp(env_default, env) != 0) {
  136. debug("%s: boot_targets not default, don't change it\n",
  137. __func__);
  138. return 0;
  139. }
  140. /*
  141. * Only run, if booting from mmc1 (i.e. /dwmmc@fe320000) and
  142. * only consider cases where the default boot-order first
  143. * tries to boot from mmc0 (eMMC) and then from mmc1
  144. * (i.e. external SD).
  145. *
  146. * In other words: the SD card will be moved to earlier in the
  147. * order, if U-Boot was also loaded from the SD-card.
  148. */
  149. if (!strcmp(boot_device, "/dwmmc@fe320000")) {
  150. char *mmc0, *mmc1;
  151. debug("%s: booted from SD-Card\n", __func__);
  152. mmc0 = strstr(env, "mmc0");
  153. mmc1 = strstr(env, "mmc1");
  154. if (!mmc0 || !mmc1) {
  155. debug("%s: only one mmc boot_target found\n", __func__);
  156. return -1;
  157. }
  158. /*
  159. * If mmc0 comes first in the boot order, we need to change
  160. * the strings to make mmc1 first.
  161. */
  162. if (mmc0 < mmc1) {
  163. mmc0[3] = '1';
  164. mmc1[3] = '0';
  165. debug("%s: set boot_targets to: %s\n", __func__, env);
  166. env_set("boot_targets", env);
  167. }
  168. }
  169. return 0;
  170. }
  171. int misc_init_r(void)
  172. {
  173. setup_serial();
  174. setup_macaddr();
  175. setup_iodomain();
  176. setup_boottargets();
  177. return 0;
  178. }
  179. #ifdef CONFIG_SERIAL_TAG
  180. void get_board_serial(struct tag_serialnr *serialnr)
  181. {
  182. char *serial_string;
  183. u64 serial = 0;
  184. serial_string = env_get("serial#");
  185. if (serial_string)
  186. serial = simple_strtoull(serial_string, NULL, 16);
  187. serialnr->high = (u32)(serial >> 32);
  188. serialnr->low = (u32)(serial & 0xffffffff);
  189. }
  190. #endif
  191. /**
  192. * Switch power at an external regulator (for our root hub).
  193. *
  194. * @param ctrl pointer to the xHCI controller
  195. * @param port port number as in the control message (one-based)
  196. * @param enable boolean indicating whether to enable or disable power
  197. * @return returns 0 on success, an error-code on failure
  198. */
  199. static int board_usb_port_power_set(struct udevice *dev, int port,
  200. bool enable)
  201. {
  202. #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR)
  203. /* We start counting ports at 0, while USB counts from 1. */
  204. int index = port - 1;
  205. const char *regname = NULL;
  206. struct udevice *regulator;
  207. const char *prop = "tsd,usb-port-power";
  208. int ret;
  209. debug("%s: ctrl '%s' port %d enable %s\n", __func__,
  210. dev_read_name(dev), port, enable ? "true" : "false");
  211. ret = dev_read_string_index(dev, prop, index, &regname);
  212. if (ret < 0) {
  213. debug("%s: ctrl '%s' port %d: no entry in '%s'\n",
  214. __func__, dev_read_name(dev), port, prop);
  215. return ret;
  216. }
  217. ret = regulator_get_by_platname(regname, &regulator);
  218. if (ret) {
  219. debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n",
  220. __func__, dev_read_name(dev), port, regname);
  221. return ret;
  222. }
  223. regulator_set_enable(regulator, enable);
  224. return 0;
  225. #else
  226. return -ENOTSUPP;
  227. #endif
  228. }
  229. void usb_hub_reset_devices(struct usb_hub_device *hub, int port)
  230. {
  231. struct udevice *dev = hub->pusb_dev->dev;
  232. struct udevice *ctrl;
  233. /* We are only interested in our root-hubs */
  234. if (usb_hub_is_root_hub(dev) == false)
  235. return;
  236. ctrl = usb_get_bus(dev);
  237. if (!ctrl) {
  238. debug("%s: could not retrieve ctrl for hub\n", __func__);
  239. return;
  240. }
  241. /*
  242. * To work around an incompatibility between the single-threaded
  243. * USB stack in U-Boot and (a strange low-power mode of) the USB
  244. * hub we have on-module, we need to delay powering on the hub
  245. * until the first time the port is probed.
  246. */
  247. board_usb_port_power_set(ctrl, port, true);
  248. }