puma-rk3399.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <u-boot/crc.h>
  12. #include <usb.h>
  13. #include <dm/pinctrl.h>
  14. #include <dm/uclass-internal.h>
  15. #include <asm/io.h>
  16. #include <asm/setup.h>
  17. #include <asm/arch-rockchip/clock.h>
  18. #include <asm/arch-rockchip/hardware.h>
  19. #include <asm/arch-rockchip/grf_rk3399.h>
  20. #include <asm/arch-rockchip/periph.h>
  21. #include <asm/arch-rockchip/misc.h>
  22. #include <power/regulator.h>
  23. #include <u-boot/sha256.h>
  24. static void setup_iodomain(void)
  25. {
  26. const u32 GRF_IO_VSEL_GPIO4CD_SHIFT = 3;
  27. struct rk3399_grf_regs *grf =
  28. syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
  29. /*
  30. * Set bit 3 in GRF_IO_VSEL so PCIE_RST# works (pin GPIO4_C6).
  31. * Linux assumes that PCIE_RST# works out of the box as it probes
  32. * PCIe before loading the iodomain driver.
  33. */
  34. rk_setreg(&grf->io_vsel, 1 << GRF_IO_VSEL_GPIO4CD_SHIFT);
  35. }
  36. /*
  37. * Swap mmc0 and mmc1 in boot_targets if booted from SD-Card.
  38. *
  39. * If bootsource is uSD-card we can assume that we want to use the
  40. * SD-Card instead of the eMMC as first boot_target for distroboot.
  41. * We only want to swap the defaults and not any custom environment a
  42. * user has set. We exit early if a changed boot_targets environment
  43. * is detected.
  44. */
  45. static int setup_boottargets(void)
  46. {
  47. const char *boot_device =
  48. ofnode_get_chosen_prop("u-boot,spl-boot-device");
  49. char *env_default, *env;
  50. if (!boot_device) {
  51. debug("%s: /chosen/u-boot,spl-boot-device not set\n",
  52. __func__);
  53. return -1;
  54. }
  55. debug("%s: booted from %s\n", __func__, boot_device);
  56. env_default = env_get_default("boot_targets");
  57. env = env_get("boot_targets");
  58. if (!env) {
  59. debug("%s: boot_targets does not exist\n", __func__);
  60. return -1;
  61. }
  62. debug("%s: boot_targets current: %s - default: %s\n",
  63. __func__, env, env_default);
  64. if (strcmp(env_default, env) != 0) {
  65. debug("%s: boot_targets not default, don't change it\n",
  66. __func__);
  67. return 0;
  68. }
  69. /*
  70. * Only run, if booting from mmc1 (i.e. /dwmmc@fe320000) and
  71. * only consider cases where the default boot-order first
  72. * tries to boot from mmc0 (eMMC) and then from mmc1
  73. * (i.e. external SD).
  74. *
  75. * In other words: the SD card will be moved to earlier in the
  76. * order, if U-Boot was also loaded from the SD-card.
  77. */
  78. if (!strcmp(boot_device, "/dwmmc@fe320000")) {
  79. char *mmc0, *mmc1;
  80. debug("%s: booted from SD-Card\n", __func__);
  81. mmc0 = strstr(env, "mmc0");
  82. mmc1 = strstr(env, "mmc1");
  83. if (!mmc0 || !mmc1) {
  84. debug("%s: only one mmc boot_target found\n", __func__);
  85. return -1;
  86. }
  87. /*
  88. * If mmc0 comes first in the boot order, we need to change
  89. * the strings to make mmc1 first.
  90. */
  91. if (mmc0 < mmc1) {
  92. mmc0[3] = '1';
  93. mmc1[3] = '0';
  94. debug("%s: set boot_targets to: %s\n", __func__, env);
  95. env_set("boot_targets", env);
  96. }
  97. }
  98. return 0;
  99. }
  100. int misc_init_r(void)
  101. {
  102. const u32 cpuid_offset = 0x7;
  103. const u32 cpuid_length = 0x10;
  104. u8 cpuid[cpuid_length];
  105. int ret;
  106. ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid);
  107. if (ret)
  108. return ret;
  109. ret = rockchip_cpuid_set(cpuid, cpuid_length);
  110. if (ret)
  111. return ret;
  112. ret = rockchip_setup_macaddr();
  113. if (ret)
  114. return ret;
  115. setup_iodomain();
  116. setup_boottargets();
  117. return 0;
  118. }
  119. #ifdef CONFIG_SERIAL_TAG
  120. void get_board_serial(struct tag_serialnr *serialnr)
  121. {
  122. char *serial_string;
  123. u64 serial = 0;
  124. serial_string = env_get("serial#");
  125. if (serial_string)
  126. serial = simple_strtoull(serial_string, NULL, 16);
  127. serialnr->high = (u32)(serial >> 32);
  128. serialnr->low = (u32)(serial & 0xffffffff);
  129. }
  130. #endif
  131. /**
  132. * Switch power at an external regulator (for our root hub).
  133. *
  134. * @param ctrl pointer to the xHCI controller
  135. * @param port port number as in the control message (one-based)
  136. * @param enable boolean indicating whether to enable or disable power
  137. * @return returns 0 on success, an error-code on failure
  138. */
  139. static int board_usb_port_power_set(struct udevice *dev, int port,
  140. bool enable)
  141. {
  142. #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR)
  143. /* We start counting ports at 0, while USB counts from 1. */
  144. int index = port - 1;
  145. const char *regname = NULL;
  146. struct udevice *regulator;
  147. const char *prop = "tsd,usb-port-power";
  148. int ret;
  149. debug("%s: ctrl '%s' port %d enable %s\n", __func__,
  150. dev_read_name(dev), port, enable ? "true" : "false");
  151. ret = dev_read_string_index(dev, prop, index, &regname);
  152. if (ret < 0) {
  153. debug("%s: ctrl '%s' port %d: no entry in '%s'\n",
  154. __func__, dev_read_name(dev), port, prop);
  155. return ret;
  156. }
  157. ret = regulator_get_by_platname(regname, &regulator);
  158. if (ret) {
  159. debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n",
  160. __func__, dev_read_name(dev), port, regname);
  161. return ret;
  162. }
  163. regulator_set_enable(regulator, enable);
  164. return 0;
  165. #else
  166. return -ENOTSUPP;
  167. #endif
  168. }
  169. void usb_hub_reset_devices(struct usb_hub_device *hub, int port)
  170. {
  171. struct udevice *dev = hub->pusb_dev->dev;
  172. struct udevice *ctrl;
  173. /* We are only interested in our root-hubs */
  174. if (usb_hub_is_root_hub(dev) == false)
  175. return;
  176. ctrl = usb_get_bus(dev);
  177. if (!ctrl) {
  178. debug("%s: could not retrieve ctrl for hub\n", __func__);
  179. return;
  180. }
  181. /*
  182. * To work around an incompatibility between the single-threaded
  183. * USB stack in U-Boot and (a strange low-power mode of) the USB
  184. * hub we have on-module, we need to delay powering on the hub
  185. * until the first time the port is probed.
  186. */
  187. board_usb_port_power_set(ctrl, port, true);
  188. }