rk3288-board.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * (C) Copyright 2015 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <clk.h>
  8. #include <dm.h>
  9. #include <ram.h>
  10. #include <syscon.h>
  11. #include <asm/io.h>
  12. #include <asm/arch/clock.h>
  13. #include <asm/arch/cru_rk3288.h>
  14. #include <asm/arch/periph.h>
  15. #include <asm/arch/pmu_rk3288.h>
  16. #include <asm/arch/qos_rk3288.h>
  17. #include <asm/arch/boot_mode.h>
  18. #include <asm/gpio.h>
  19. #include <dm/pinctrl.h>
  20. #include <dt-bindings/clock/rk3288-cru.h>
  21. #include <power/regulator.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. #define PMU_BASE 0xff730000
  24. static void setup_boot_mode(void)
  25. {
  26. struct rk3288_pmu *const pmu = (void *)PMU_BASE;
  27. int boot_mode = readl(&pmu->sys_reg[0]);
  28. debug("boot mode %x.\n", boot_mode);
  29. /* Clear boot mode */
  30. writel(BOOT_NORMAL, &pmu->sys_reg[0]);
  31. switch (boot_mode) {
  32. case BOOT_FASTBOOT:
  33. printf("enter fastboot!\n");
  34. env_set("preboot", "setenv preboot; fastboot usb0");
  35. break;
  36. case BOOT_UMS:
  37. printf("enter UMS!\n");
  38. env_set("preboot", "setenv preboot; if mmc dev 0;"
  39. "then ums mmc 0; else ums mmc 1;fi");
  40. break;
  41. }
  42. }
  43. __weak int rk_board_late_init(void)
  44. {
  45. return 0;
  46. }
  47. int rk3288_qos_init(void)
  48. {
  49. int val = 2 << PRIORITY_HIGH_SHIFT | 2 << PRIORITY_LOW_SHIFT;
  50. /* set vop qos to higher priority */
  51. writel(val, CPU_AXI_QOS_PRIORITY + VIO0_VOP_QOS);
  52. writel(val, CPU_AXI_QOS_PRIORITY + VIO1_VOP_QOS);
  53. if (!fdt_node_check_compatible(gd->fdt_blob, 0,
  54. "rockchip,rk3288-tinker"))
  55. {
  56. /* set isp qos to higher priority */
  57. writel(val, CPU_AXI_QOS_PRIORITY + VIO1_ISP_R_QOS);
  58. writel(val, CPU_AXI_QOS_PRIORITY + VIO1_ISP_W0_QOS);
  59. writel(val, CPU_AXI_QOS_PRIORITY + VIO1_ISP_W1_QOS);
  60. }
  61. return 0;
  62. }
  63. static void rk3288_detect_reset_reason(void)
  64. {
  65. struct rk3288_cru *cru = rockchip_get_cru();
  66. const char *reason;
  67. if (IS_ERR(cru))
  68. return;
  69. switch (cru->cru_glb_rst_st) {
  70. case GLB_POR_RST:
  71. reason = "POR";
  72. break;
  73. case FST_GLB_RST_ST:
  74. case SND_GLB_RST_ST:
  75. reason = "RST";
  76. break;
  77. case FST_GLB_TSADC_RST_ST:
  78. case SND_GLB_TSADC_RST_ST:
  79. reason = "THERMAL";
  80. break;
  81. case FST_GLB_WDT_RST_ST:
  82. case SND_GLB_WDT_RST_ST:
  83. reason = "WDOG";
  84. break;
  85. default:
  86. reason = "unknown reset";
  87. }
  88. env_set("reset_reason", reason);
  89. /*
  90. * Clear cru_glb_rst_st, so we can determine the last reset cause
  91. * for following resets.
  92. */
  93. rk_clrreg(&cru->cru_glb_rst_st, GLB_RST_ST_MASK);
  94. }
  95. int board_late_init(void)
  96. {
  97. setup_boot_mode();
  98. rk3288_qos_init();
  99. rk3288_detect_reset_reason();
  100. return rk_board_late_init();
  101. }
  102. #if !CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM)
  103. static int veyron_init(void)
  104. {
  105. struct udevice *dev;
  106. struct clk clk;
  107. int ret;
  108. ret = regulator_get_by_platname("vdd_arm", &dev);
  109. if (ret) {
  110. debug("Cannot set regulator name\n");
  111. return ret;
  112. }
  113. /* Slowly raise to max CPU voltage to prevent overshoot */
  114. ret = regulator_set_value(dev, 1200000);
  115. if (ret)
  116. return ret;
  117. udelay(175); /* Must wait for voltage to stabilize, 2mV/us */
  118. ret = regulator_set_value(dev, 1400000);
  119. if (ret)
  120. return ret;
  121. udelay(100); /* Must wait for voltage to stabilize, 2mV/us */
  122. ret = rockchip_get_clk(&clk.dev);
  123. if (ret)
  124. return ret;
  125. clk.id = PLL_APLL;
  126. ret = clk_set_rate(&clk, 1800000000);
  127. if (IS_ERR_VALUE(ret))
  128. return ret;
  129. return 0;
  130. }
  131. #endif
  132. int board_init(void)
  133. {
  134. #if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM)
  135. struct udevice *pinctrl;
  136. int ret;
  137. /*
  138. * We need to implement sdcard iomux here for the further
  139. * initlization, otherwise, it'll hit sdcard command sending
  140. * timeout exception.
  141. */
  142. ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
  143. if (ret) {
  144. debug("%s: Cannot find pinctrl device\n", __func__);
  145. goto err;
  146. }
  147. ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD);
  148. if (ret) {
  149. debug("%s: Failed to set up SD card\n", __func__);
  150. goto err;
  151. }
  152. return 0;
  153. err:
  154. printf("board_init: Error %d\n", ret);
  155. /* No way to report error here */
  156. hang();
  157. return -1;
  158. #else
  159. int ret;
  160. /* We do some SoC one time setting here */
  161. if (!fdt_node_check_compatible(gd->fdt_blob, 0, "google,veyron")) {
  162. ret = veyron_init();
  163. if (ret)
  164. return ret;
  165. }
  166. return 0;
  167. #endif
  168. }
  169. #ifndef CONFIG_SYS_DCACHE_OFF
  170. void enable_caches(void)
  171. {
  172. /* Enable D-cache. I-cache is already enabled in start.S */
  173. dcache_enable();
  174. }
  175. #endif
  176. #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
  177. #include <usb.h>
  178. #include <usb/dwc2_udc.h>
  179. static struct dwc2_plat_otg_data rk3288_otg_data = {
  180. .rx_fifo_sz = 512,
  181. .np_tx_fifo_sz = 16,
  182. .tx_fifo_sz = 128,
  183. };
  184. int board_usb_init(int index, enum usb_init_type init)
  185. {
  186. int node, phy_node;
  187. const char *mode;
  188. bool matched = false;
  189. const void *blob = gd->fdt_blob;
  190. u32 grf_phy_offset;
  191. /* find the usb_otg node */
  192. node = fdt_node_offset_by_compatible(blob, -1,
  193. "rockchip,rk3288-usb");
  194. while (node > 0) {
  195. mode = fdt_getprop(blob, node, "dr_mode", NULL);
  196. if (mode && strcmp(mode, "otg") == 0) {
  197. matched = true;
  198. break;
  199. }
  200. node = fdt_node_offset_by_compatible(blob, node,
  201. "rockchip,rk3288-usb");
  202. }
  203. if (!matched) {
  204. debug("Not found usb_otg device\n");
  205. return -ENODEV;
  206. }
  207. rk3288_otg_data.regs_otg = fdtdec_get_addr(blob, node, "reg");
  208. node = fdtdec_lookup_phandle(blob, node, "phys");
  209. if (node <= 0) {
  210. debug("Not found usb phy device\n");
  211. return -ENODEV;
  212. }
  213. phy_node = fdt_parent_offset(blob, node);
  214. if (phy_node <= 0) {
  215. debug("Not found usb phy device\n");
  216. return -ENODEV;
  217. }
  218. rk3288_otg_data.phy_of_node = phy_node;
  219. grf_phy_offset = fdtdec_get_addr(blob, node, "reg");
  220. /* find the grf node */
  221. node = fdt_node_offset_by_compatible(blob, -1,
  222. "rockchip,rk3288-grf");
  223. if (node <= 0) {
  224. debug("Not found grf device\n");
  225. return -ENODEV;
  226. }
  227. rk3288_otg_data.regs_phy = grf_phy_offset +
  228. fdtdec_get_addr(blob, node, "reg");
  229. return dwc2_udc_probe(&rk3288_otg_data);
  230. }
  231. int board_usb_cleanup(int index, enum usb_init_type init)
  232. {
  233. return 0;
  234. }
  235. #endif
  236. static int do_clock(cmd_tbl_t *cmdtp, int flag, int argc,
  237. char * const argv[])
  238. {
  239. static const struct {
  240. char *name;
  241. int id;
  242. } clks[] = {
  243. { "osc", CLK_OSC },
  244. { "apll", CLK_ARM },
  245. { "dpll", CLK_DDR },
  246. { "cpll", CLK_CODEC },
  247. { "gpll", CLK_GENERAL },
  248. #ifdef CONFIG_ROCKCHIP_RK3036
  249. { "mpll", CLK_NEW },
  250. #else
  251. { "npll", CLK_NEW },
  252. #endif
  253. };
  254. int ret, i;
  255. struct udevice *dev;
  256. ret = rockchip_get_clk(&dev);
  257. if (ret) {
  258. printf("clk-uclass not found\n");
  259. return 0;
  260. }
  261. for (i = 0; i < ARRAY_SIZE(clks); i++) {
  262. struct clk clk;
  263. ulong rate;
  264. clk.id = clks[i].id;
  265. ret = clk_request(dev, &clk);
  266. if (ret < 0)
  267. continue;
  268. rate = clk_get_rate(&clk);
  269. printf("%s: %lu\n", clks[i].name, rate);
  270. clk_free(&clk);
  271. }
  272. return 0;
  273. }
  274. U_BOOT_CMD(
  275. clock, 2, 1, do_clock,
  276. "display information about clocks",
  277. ""
  278. );
  279. #define GRF_SOC_CON2 0xff77024c
  280. int board_early_init_f(void)
  281. {
  282. struct udevice *pinctrl;
  283. struct udevice *dev;
  284. int ret;
  285. /*
  286. * This init is done in SPL, but when chain-loading U-Boot SPL will
  287. * have been skipped. Allow the clock driver to check if it needs
  288. * setting up.
  289. */
  290. ret = rockchip_get_clk(&dev);
  291. if (ret) {
  292. debug("CLK init failed: %d\n", ret);
  293. return ret;
  294. }
  295. ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
  296. if (ret) {
  297. debug("%s: Cannot find pinctrl device\n", __func__);
  298. return ret;
  299. }
  300. /* Enable debug UART */
  301. ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG);
  302. if (ret) {
  303. debug("%s: Failed to set up console UART\n", __func__);
  304. return ret;
  305. }
  306. rk_setreg(GRF_SOC_CON2, 1 << 0);
  307. return 0;
  308. }