soc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <platform_def.h>
  8. #include <arch_helpers.h>
  9. #include <common/debug.h>
  10. #include <drivers/delay_timer.h>
  11. #include <lib/mmio.h>
  12. #include <dfs.h>
  13. #include <dram.h>
  14. #include <m0_ctl.h>
  15. #include <plat_private.h>
  16. #include <pmu.h>
  17. #include <rk3399_def.h>
  18. #include <secure.h>
  19. #include <soc.h>
  20. /* Table of regions to map using the MMU. */
  21. const mmap_region_t plat_rk_mmap[] = {
  22. MAP_REGION_FLAT(DEV_RNG0_BASE, DEV_RNG0_SIZE,
  23. MT_DEVICE | MT_RW | MT_SECURE),
  24. MAP_REGION_FLAT(PMUSRAM_BASE, PMUSRAM_SIZE,
  25. MT_MEMORY | MT_RW | MT_SECURE),
  26. { 0 }
  27. };
  28. /* The RockChip power domain tree descriptor */
  29. const unsigned char rockchip_power_domain_tree_desc[] = {
  30. /* No of root nodes */
  31. PLATFORM_SYSTEM_COUNT,
  32. /* No of children for the root node */
  33. PLATFORM_CLUSTER_COUNT,
  34. /* No of children for the first cluster node */
  35. PLATFORM_CLUSTER0_CORE_COUNT,
  36. /* No of children for the second cluster node */
  37. PLATFORM_CLUSTER1_CORE_COUNT
  38. };
  39. /* sleep data for pll suspend */
  40. static struct deepsleep_data_s slp_data;
  41. /* sleep data that needs to be accessed from pmusram */
  42. __pmusramdata struct pmu_sleep_data pmu_slp_data;
  43. static void set_pll_slow_mode(uint32_t pll_id)
  44. {
  45. if (pll_id == PPLL_ID)
  46. mmio_write_32(PMUCRU_BASE + PMUCRU_PPLL_CON(3), PLL_SLOW_MODE);
  47. else
  48. mmio_write_32((CRU_BASE +
  49. CRU_PLL_CON(pll_id, 3)), PLL_SLOW_MODE);
  50. }
  51. static void set_pll_normal_mode(uint32_t pll_id)
  52. {
  53. if (pll_id == PPLL_ID)
  54. mmio_write_32(PMUCRU_BASE + PMUCRU_PPLL_CON(3), PLL_NOMAL_MODE);
  55. else
  56. mmio_write_32(CRU_BASE +
  57. CRU_PLL_CON(pll_id, 3), PLL_NOMAL_MODE);
  58. }
  59. static void set_pll_bypass(uint32_t pll_id)
  60. {
  61. if (pll_id == PPLL_ID)
  62. mmio_write_32(PMUCRU_BASE +
  63. PMUCRU_PPLL_CON(3), PLL_BYPASS_MODE);
  64. else
  65. mmio_write_32(CRU_BASE +
  66. CRU_PLL_CON(pll_id, 3), PLL_BYPASS_MODE);
  67. }
  68. static void _pll_suspend(uint32_t pll_id)
  69. {
  70. set_pll_slow_mode(pll_id);
  71. set_pll_bypass(pll_id);
  72. }
  73. /**
  74. * disable_dvfs_plls - To suspend the specific PLLs
  75. *
  76. * When we close the center logic, the DPLL will be closed,
  77. * so we need to keep the ABPLL and switch to it to supply
  78. * clock for DDR during suspend, then we should not close
  79. * the ABPLL and exclude ABPLL_ID.
  80. */
  81. void disable_dvfs_plls(void)
  82. {
  83. _pll_suspend(CPLL_ID);
  84. _pll_suspend(NPLL_ID);
  85. _pll_suspend(VPLL_ID);
  86. _pll_suspend(GPLL_ID);
  87. _pll_suspend(ALPLL_ID);
  88. }
  89. /**
  90. * disable_nodvfs_plls - To suspend the PPLL
  91. */
  92. void disable_nodvfs_plls(void)
  93. {
  94. _pll_suspend(PPLL_ID);
  95. }
  96. /**
  97. * restore_pll - Copy PLL settings from memory to a PLL.
  98. *
  99. * This will copy PLL settings from an array in memory to the memory mapped
  100. * registers for a PLL.
  101. *
  102. * Note that: above the PLL exclude PPLL.
  103. *
  104. * pll_id: One of the values from enum plls_id
  105. * src: Pointer to the array of values to restore from
  106. */
  107. static void restore_pll(int pll_id, uint32_t *src)
  108. {
  109. /* Nice to have PLL off while configuring */
  110. mmio_write_32((CRU_BASE + CRU_PLL_CON(pll_id, 3)), PLL_SLOW_MODE);
  111. mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 0), src[0] | REG_SOC_WMSK);
  112. mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 1), src[1] | REG_SOC_WMSK);
  113. mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 2), src[2]);
  114. mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 4), src[4] | REG_SOC_WMSK);
  115. mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 5), src[5] | REG_SOC_WMSK);
  116. /* Do PLL_CON3 since that will enable things */
  117. mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 3), src[3] | REG_SOC_WMSK);
  118. /* Wait for PLL lock done */
  119. while ((mmio_read_32(CRU_BASE + CRU_PLL_CON(pll_id, 2)) &
  120. 0x80000000) == 0x0)
  121. ;
  122. }
  123. /**
  124. * save_pll - Copy PLL settings a PLL to memory
  125. *
  126. * This will copy PLL settings from the memory mapped registers for a PLL to
  127. * an array in memory.
  128. *
  129. * Note that: above the PLL exclude PPLL.
  130. *
  131. * pll_id: One of the values from enum plls_id
  132. * src: Pointer to the array of values to save to.
  133. */
  134. static void save_pll(uint32_t *dst, int pll_id)
  135. {
  136. int i;
  137. for (i = 0; i < PLL_CON_COUNT; i++)
  138. dst[i] = mmio_read_32(CRU_BASE + CRU_PLL_CON(pll_id, i));
  139. }
  140. /**
  141. * prepare_abpll_for_ddrctrl - Copy DPLL settings to ABPLL
  142. *
  143. * This will copy DPLL settings from the memory mapped registers for a PLL to
  144. * an array in memory.
  145. */
  146. void prepare_abpll_for_ddrctrl(void)
  147. {
  148. save_pll(slp_data.plls_con[ABPLL_ID], ABPLL_ID);
  149. save_pll(slp_data.plls_con[DPLL_ID], DPLL_ID);
  150. restore_pll(ABPLL_ID, slp_data.plls_con[DPLL_ID]);
  151. }
  152. void restore_abpll(void)
  153. {
  154. restore_pll(ABPLL_ID, slp_data.plls_con[ABPLL_ID]);
  155. }
  156. void clk_gate_con_save(void)
  157. {
  158. uint32_t i = 0;
  159. for (i = 0; i < PMUCRU_GATE_COUNT; i++)
  160. slp_data.pmucru_gate_con[i] =
  161. mmio_read_32(PMUCRU_BASE + PMUCRU_GATE_CON(i));
  162. for (i = 0; i < CRU_GATE_COUNT; i++)
  163. slp_data.cru_gate_con[i] =
  164. mmio_read_32(CRU_BASE + CRU_GATE_CON(i));
  165. }
  166. void clk_gate_con_disable(void)
  167. {
  168. uint32_t i;
  169. for (i = 0; i < PMUCRU_GATE_COUNT; i++)
  170. mmio_write_32(PMUCRU_BASE + PMUCRU_GATE_CON(i), REG_SOC_WMSK);
  171. for (i = 0; i < CRU_GATE_COUNT; i++)
  172. mmio_write_32(CRU_BASE + CRU_GATE_CON(i), REG_SOC_WMSK);
  173. }
  174. void clk_gate_con_restore(void)
  175. {
  176. uint32_t i;
  177. for (i = 0; i < PMUCRU_GATE_COUNT; i++)
  178. mmio_write_32(PMUCRU_BASE + PMUCRU_GATE_CON(i),
  179. REG_SOC_WMSK | slp_data.pmucru_gate_con[i]);
  180. for (i = 0; i < CRU_GATE_COUNT; i++)
  181. mmio_write_32(CRU_BASE + CRU_GATE_CON(i),
  182. REG_SOC_WMSK | slp_data.cru_gate_con[i]);
  183. }
  184. static void set_plls_nobypass(uint32_t pll_id)
  185. {
  186. if (pll_id == PPLL_ID)
  187. mmio_write_32(PMUCRU_BASE + PMUCRU_PPLL_CON(3),
  188. PLL_NO_BYPASS_MODE);
  189. else
  190. mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 3),
  191. PLL_NO_BYPASS_MODE);
  192. }
  193. static void _pll_resume(uint32_t pll_id)
  194. {
  195. set_plls_nobypass(pll_id);
  196. set_pll_normal_mode(pll_id);
  197. }
  198. void set_pmu_rsthold(void)
  199. {
  200. uint32_t rstnhold_cofig0;
  201. uint32_t rstnhold_cofig1;
  202. pmu_slp_data.pmucru_rstnhold_con0 = mmio_read_32(PMUCRU_BASE +
  203. PMUCRU_RSTNHOLD_CON0);
  204. pmu_slp_data.pmucru_rstnhold_con1 = mmio_read_32(PMUCRU_BASE +
  205. PMUCRU_RSTNHOLD_CON1);
  206. rstnhold_cofig0 = BIT_WITH_WMSK(PRESETN_NOC_PMU_HOLD) |
  207. BIT_WITH_WMSK(PRESETN_INTMEM_PMU_HOLD) |
  208. BIT_WITH_WMSK(HRESETN_CM0S_PMU_HOLD) |
  209. BIT_WITH_WMSK(HRESETN_CM0S_NOC_PMU_HOLD) |
  210. BIT_WITH_WMSK(DRESETN_CM0S_PMU_HOLD) |
  211. BIT_WITH_WMSK(POESETN_CM0S_PMU_HOLD) |
  212. BIT_WITH_WMSK(PRESETN_TIMER_PMU_0_1_HOLD) |
  213. BIT_WITH_WMSK(RESETN_TIMER_PMU_0_HOLD) |
  214. BIT_WITH_WMSK(RESETN_TIMER_PMU_1_HOLD) |
  215. BIT_WITH_WMSK(PRESETN_UART_M0_PMU_HOLD) |
  216. BIT_WITH_WMSK(RESETN_UART_M0_PMU_HOLD) |
  217. BIT_WITH_WMSK(PRESETN_WDT_PMU_HOLD);
  218. rstnhold_cofig1 = BIT_WITH_WMSK(PRESETN_RKPWM_PMU_HOLD) |
  219. BIT_WITH_WMSK(PRESETN_PMUGRF_HOLD) |
  220. BIT_WITH_WMSK(PRESETN_SGRF_HOLD) |
  221. BIT_WITH_WMSK(PRESETN_GPIO0_HOLD) |
  222. BIT_WITH_WMSK(PRESETN_GPIO1_HOLD) |
  223. BIT_WITH_WMSK(PRESETN_CRU_PMU_HOLD) |
  224. BIT_WITH_WMSK(PRESETN_PVTM_PMU_HOLD);
  225. mmio_write_32(PMUCRU_BASE + PMUCRU_RSTNHOLD_CON0, rstnhold_cofig0);
  226. mmio_write_32(PMUCRU_BASE + PMUCRU_RSTNHOLD_CON1, rstnhold_cofig1);
  227. }
  228. void pmu_sgrf_rst_hld(void)
  229. {
  230. mmio_write_32(PMUCRU_BASE + CRU_PMU_RSTHOLD_CON(1),
  231. CRU_PMU_SGRF_RST_HOLD);
  232. }
  233. /*
  234. * When system reset in running state, we want the cpus to be reboot
  235. * from maskrom (system reboot),
  236. * the pmusgrf reset-hold bits needs to be released.
  237. * When system wake up from system deep suspend, some soc will be reset
  238. * when waked up,
  239. * we want the bootcpu to be reboot from pmusram,
  240. * the pmusgrf reset-hold bits needs to be held.
  241. */
  242. __pmusramfunc void pmu_sgrf_rst_hld_release(void)
  243. {
  244. mmio_write_32(PMUCRU_BASE + CRU_PMU_RSTHOLD_CON(1),
  245. CRU_PMU_SGRF_RST_RLS);
  246. }
  247. __pmusramfunc void restore_pmu_rsthold(void)
  248. {
  249. mmio_write_32(PMUCRU_BASE + PMUCRU_RSTNHOLD_CON0,
  250. pmu_slp_data.pmucru_rstnhold_con0 | REG_SOC_WMSK);
  251. mmio_write_32(PMUCRU_BASE + PMUCRU_RSTNHOLD_CON1,
  252. pmu_slp_data.pmucru_rstnhold_con1 | REG_SOC_WMSK);
  253. }
  254. /**
  255. * enable_dvfs_plls - To resume the specific PLLs
  256. *
  257. * Please see the comment at the disable_dvfs_plls()
  258. * we don't suspend the ABPLL, so don't need resume
  259. * it too.
  260. */
  261. void enable_dvfs_plls(void)
  262. {
  263. _pll_resume(ALPLL_ID);
  264. _pll_resume(GPLL_ID);
  265. _pll_resume(VPLL_ID);
  266. _pll_resume(NPLL_ID);
  267. _pll_resume(CPLL_ID);
  268. }
  269. /**
  270. * enable_nodvfs_plls - To resume the PPLL
  271. */
  272. void enable_nodvfs_plls(void)
  273. {
  274. _pll_resume(PPLL_ID);
  275. }
  276. void soc_global_soft_reset_init(void)
  277. {
  278. mmio_write_32(PMUCRU_BASE + CRU_PMU_RSTHOLD_CON(1),
  279. CRU_PMU_SGRF_RST_RLS);
  280. mmio_clrbits_32(CRU_BASE + CRU_GLB_RST_CON,
  281. CRU_PMU_WDTRST_MSK | CRU_PMU_FIRST_SFTRST_MSK);
  282. }
  283. void __dead2 soc_global_soft_reset(void)
  284. {
  285. pmu_power_domains_on();
  286. set_pll_slow_mode(VPLL_ID);
  287. set_pll_slow_mode(NPLL_ID);
  288. set_pll_slow_mode(GPLL_ID);
  289. set_pll_slow_mode(CPLL_ID);
  290. set_pll_slow_mode(PPLL_ID);
  291. set_pll_slow_mode(ABPLL_ID);
  292. set_pll_slow_mode(ALPLL_ID);
  293. dsb();
  294. mmio_write_32(CRU_BASE + CRU_GLB_SRST_FST, GLB_SRST_FST_CFG_VAL);
  295. /*
  296. * Maybe the HW needs some times to reset the system,
  297. * so we do not hope the core to execute valid codes.
  298. */
  299. while (1)
  300. ;
  301. }
  302. void plat_rockchip_soc_init(void)
  303. {
  304. secure_timer_init();
  305. secure_sgrf_init();
  306. secure_sgrf_ddr_rgn_init();
  307. soc_global_soft_reset_init();
  308. plat_rockchip_gpio_init();
  309. m0_init();
  310. dram_init();
  311. dram_dfs_init();
  312. }