pmu.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  1. /*
  2. * Copyright (c) 2024, Rockchip, Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <arch_helpers.h>
  9. #include <bl31/bl31.h>
  10. #include <common/debug.h>
  11. #include <drivers/arm/gicv3.h>
  12. #include <drivers/console.h>
  13. #include <drivers/delay_timer.h>
  14. #include <drivers/ti/uart/uart_16550.h>
  15. #include <lib/mmio.h>
  16. #include <plat/common/platform.h>
  17. #include <platform_def.h>
  18. #include <pmu.h>
  19. #include <cpus_on_fixed_addr.h>
  20. #include <plat_pm_helpers.h>
  21. #include <plat_private.h>
  22. #include <pm_pd_regs.h>
  23. #include <rk3588_clk.h>
  24. #include <rockchip_sip_svc.h>
  25. #include <secure.h>
  26. #include <soc.h>
  27. #define PSRAM_SP_TOP ((PMUSRAM_BASE + PMUSRAM_RSIZE) & ~0xf)
  28. #define NONBOOT_CPUS_OFF_LOOP (500000)
  29. #define DSUGRF_REG_CNT (0x78 / 4 + 1)
  30. #define BCORE_GRF_REG_CNT (0x30 / 4 + 1)
  31. #define LCORE_GRF_REG_CNT (0x30 / 4 + 1)
  32. #define CENTER_GRF_REG_CNT (0x20 / 4 + 1)
  33. static struct psram_data_t *psram_sleep_cfg =
  34. (struct psram_data_t *)&sys_sleep_flag_sram;
  35. static int8_t pd_repair_map[] = {
  36. [PD_GPU] = PD_RPR_GPU,
  37. [PD_NPU] = -1,
  38. [PD_VCODEC] = -1,
  39. [PD_NPUTOP] = PD_RPR_NPUTOP,
  40. [PD_NPU1] = PD_RPR_NPU1,
  41. [PD_NPU2] = PD_RPR_NPU2,
  42. [PD_VENC0] = PD_RPR_VENC0,
  43. [PD_VENC1] = PD_RPR_VENC1,
  44. [PD_RKVDEC0] = PD_RPR_RKVDEC0,
  45. [PD_RKVDEC1] = PD_RPR_RKVDEC1,
  46. [PD_VDPU] = PD_RPR_VDPU,
  47. [PD_RGA30] = PD_RPR_RGA30,
  48. [PD_AV1] = PD_RPR_AV1,
  49. [PD_VI] = PD_RPR_VI,
  50. [PD_FEC] = PD_RPR_FEC,
  51. [PD_ISP1] = PD_RPR_ISP1,
  52. [PD_RGA31] = PD_RPR_RGA31,
  53. [PD_VOP] = PD_RPR_VOP,
  54. [PD_VO0] = PD_RPR_VO0,
  55. [PD_VO1] = PD_RPR_VO1,
  56. [PD_AUDIO] = PD_RPR_AUDIO,
  57. [PD_PHP] = PD_RPR_PHP,
  58. [PD_GMAC] = PD_RPR_GMAC,
  59. [PD_PCIE] = PD_RPR_PCIE,
  60. [PD_NVM] = -1,
  61. [PD_NVM0] = PD_RPR_NVM0,
  62. [PD_SDIO] = PD_RPR_SDIO,
  63. [PD_USB] = PD_RPR_USB,
  64. [PD_SECURE] = -1,
  65. [PD_SDMMC] = PD_RPR_SDMMC,
  66. [PD_CRYPTO] = PD_RPR_CRYPTO,
  67. [PD_CENTER] = PD_RPR_CENTER,
  68. [PD_DDR01] = PD_RPR_DDR01,
  69. [PD_DDR23] = PD_RPR_DDR23,
  70. };
  71. struct rk3588_sleep_ddr_data {
  72. uint32_t gpio0a_iomux_l, gpio0a_iomux_h, gpio0b_iomux_l;
  73. uint32_t pmu_pd_st0, bus_idle_st0, qch_pwr_st;
  74. uint32_t pmu2_vol_gate_con[3], pmu2_submem_gate_sft_con0;
  75. uint32_t pmu2_bisr_con0;
  76. uint32_t cpll_con0;
  77. uint32_t cru_mode_con, busscru_mode_con;
  78. uint32_t bussgrf_soc_con7;
  79. uint32_t pmu0grf_soc_con0, pmu0grf_soc_con1, pmu0grf_soc_con3;
  80. uint32_t pmu1grf_soc_con2, pmu1grf_soc_con7, pmu1grf_soc_con8, pmu1grf_soc_con9;
  81. uint32_t pmu0sgrf_soc_con1;
  82. uint32_t pmu1sgrf_soc_con14;
  83. uint32_t ddrgrf_chn_con0[4], ddrgrf_chn_con1[4],
  84. ddrgrf_chn_con2[4], pmu1_ddr_pwr_sft_con[4];
  85. uint32_t pmu1cru_clksel_con1;
  86. };
  87. static struct rk3588_sleep_ddr_data ddr_data;
  88. struct rk3588_sleep_pmusram_data {
  89. uint32_t dsusgrf_soc_con[DSUSGRF_SOC_CON_CNT],
  90. dsusgrf_ddr_hash_con[DSUSGRF_DDR_HASH_CON_CNT];
  91. uint32_t dsu_ddr_fw_rgn_reg[FIREWALL_DSU_RGN_CNT],
  92. dsu_ddr_fw_mst_reg[FIREWALL_DSU_MST_CNT],
  93. dsu_ddr_fw_con_reg[FIREWALL_DSU_CON_CNT];
  94. uint32_t busioc_gpio0b_iomux_h;
  95. };
  96. static __pmusramdata struct rk3588_sleep_pmusram_data pmusram_data;
  97. static __pmusramfunc void dsu_restore_early(void)
  98. {
  99. int i;
  100. /* dsusgrf */
  101. for (i = 0; i < DSUSGRF_SOC_CON_CNT; i++)
  102. mmio_write_32(DSUSGRF_BASE + DSUSGRF_SOC_CON(i),
  103. WITH_16BITS_WMSK(pmusram_data.dsusgrf_soc_con[i]));
  104. for (i = 0; i < DSUSGRF_DDR_HASH_CON_CNT; i++)
  105. mmio_write_32(DSUSGRF_BASE + DSUSGRF_DDR_HASH_CON(i),
  106. pmusram_data.dsusgrf_ddr_hash_con[i]);
  107. /* dsu ddr firewall */
  108. for (i = 0; i < FIREWALL_DSU_RGN_CNT; i++)
  109. mmio_write_32(FIREWALL_DSU_BASE + FIREWALL_DSU_RGN(i),
  110. pmusram_data.dsu_ddr_fw_rgn_reg[i]);
  111. for (i = 0; i < FIREWALL_DSU_MST_CNT; i++)
  112. mmio_write_32(FIREWALL_DSU_BASE + FIREWALL_DSU_MST(i),
  113. pmusram_data.dsu_ddr_fw_mst_reg[i]);
  114. for (i = 0; i < FIREWALL_DSU_CON_CNT; i++)
  115. mmio_write_32(FIREWALL_DSU_BASE + FIREWALL_DSU_CON(i),
  116. pmusram_data.dsu_ddr_fw_con_reg[i]);
  117. }
  118. static __pmusramfunc void ddr_resume(void)
  119. {
  120. /* check the crypto function had been enabled or not */
  121. if ((mmio_read_32(DSUSGRF_BASE + DSU_SGRF_SOC_CON(4)) & BIT(4)) != 0) {
  122. /* enable the crypto function */
  123. mmio_write_32(DSUSGRF_BASE + DSU_SGRF_SOC_CON(4), BITS_WITH_WMASK(0, 0x1, 4));
  124. dsb();
  125. isb();
  126. __asm__ volatile ("mov x0, #3\n"
  127. "dsb sy\n"
  128. "msr rmr_el3, x0\n"
  129. "1:\n"
  130. "isb\n"
  131. "wfi\n"
  132. "b 1b\n");
  133. }
  134. dsu_restore_early();
  135. }
  136. static void dsu_core_save(void)
  137. {
  138. int i;
  139. /* dsusgrf */
  140. for (i = 0; i < DSUSGRF_SOC_CON_CNT; i++)
  141. pmusram_data.dsusgrf_soc_con[i] =
  142. mmio_read_32(DSUSGRF_BASE + DSUSGRF_SOC_CON(i));
  143. for (i = 0; i < DSUSGRF_DDR_HASH_CON_CNT; i++)
  144. pmusram_data.dsusgrf_ddr_hash_con[i] =
  145. mmio_read_32(DSUSGRF_BASE + DSUSGRF_DDR_HASH_CON(i));
  146. /* dsu ddr firewall */
  147. for (i = 0; i < FIREWALL_DSU_RGN_CNT; i++)
  148. pmusram_data.dsu_ddr_fw_rgn_reg[i] =
  149. mmio_read_32(FIREWALL_DSU_BASE + FIREWALL_DSU_RGN(i));
  150. for (i = 0; i < FIREWALL_DSU_MST_CNT; i++)
  151. pmusram_data.dsu_ddr_fw_mst_reg[i] =
  152. mmio_read_32(FIREWALL_DSU_BASE + FIREWALL_DSU_MST(i));
  153. for (i = 0; i < FIREWALL_DSU_CON_CNT; i++)
  154. pmusram_data.dsu_ddr_fw_con_reg[i] =
  155. mmio_read_32(FIREWALL_DSU_BASE + FIREWALL_DSU_CON(i));
  156. pvtplls_suspend();
  157. pd_dsu_core_save();
  158. }
  159. static void dsu_core_restore(void)
  160. {
  161. pd_dsu_core_restore();
  162. pvtplls_resume();
  163. }
  164. static uint32_t clk_save[CRU_CLKGATE_CON_CNT + PHPCRU_CLKGATE_CON_CNT +
  165. SECURECRU_CLKGATE_CON_CNT + PMU1CRU_CLKGATE_CON_CNT];
  166. void clk_gate_con_save(void)
  167. {
  168. int i, j = 0;
  169. for (i = 0; i < CRU_CLKGATE_CON_CNT; i++, j++)
  170. clk_save[j] = mmio_read_32(CRU_BASE + CRU_CLKGATE_CON(i));
  171. clk_save[j] = mmio_read_32(PHP_CRU_BASE + PHPCRU_CLKGATE_CON);
  172. for (i = 0; i < SECURECRU_CLKGATE_CON_CNT; i++, j++)
  173. clk_save[j] = mmio_read_32(SCRU_BASE + SECURECRU_CLKGATE_CON(i));
  174. for (i = 0; i < PMU1CRU_CLKGATE_CON_CNT; i++, j++)
  175. clk_save[j] = mmio_read_32(PMU1CRU_BASE + CRU_CLKGATE_CON(i));
  176. }
  177. void clk_gate_con_disable(void)
  178. {
  179. int i;
  180. for (i = 0; i < CRU_CLKGATE_CON_CNT; i++)
  181. mmio_write_32(CRU_BASE + CRU_CLKGATE_CON(i), 0xffff0000);
  182. mmio_write_32(PHP_CRU_BASE + PHPCRU_CLKGATE_CON, 0xffff0000);
  183. for (i = 0; i < SECURECRU_CLKGATE_CON_CNT; i++)
  184. mmio_write_32(SCRU_BASE + SECURECRU_CLKGATE_CON(i), 0xffff0000);
  185. for (i = 0; i < PMU1CRU_CLKGATE_CON_CNT; i++)
  186. mmio_write_32(PMU1CRU_BASE + CRU_CLKGATE_CON(i), 0xffff0000);
  187. }
  188. void clk_gate_con_restore(void)
  189. {
  190. int i, j = 0;
  191. for (i = 0; i < CRU_CLKGATE_CON_CNT; i++, j++)
  192. mmio_write_32(CRU_BASE + CRU_CLKGATE_CON(i),
  193. WITH_16BITS_WMSK(clk_save[j]));
  194. mmio_write_32(PHP_CRU_BASE + PHPCRU_CLKGATE_CON,
  195. WITH_16BITS_WMSK(clk_save[j]));
  196. for (i = 0; i < SECURECRU_CLKGATE_CON_CNT; i++, j++)
  197. mmio_write_32(SCRU_BASE + SECURECRU_CLKGATE_CON(i),
  198. WITH_16BITS_WMSK(clk_save[j]));
  199. for (i = 0; i < PMU1CRU_CLKGATE_CON_CNT; i++, j++)
  200. mmio_write_32(PMU1CRU_BASE + CRU_CLKGATE_CON(i),
  201. WITH_16BITS_WMSK(clk_save[j]));
  202. }
  203. static void pmu_bus_idle_req(uint32_t bus, uint32_t state)
  204. {
  205. uint32_t wait_cnt = 0;
  206. mmio_write_32(PMU_BASE + PMU2_BUS_IDLE_SFTCON(bus / 16),
  207. BITS_WITH_WMASK(state, 0x1, bus % 16));
  208. while (pmu_bus_idle_st(bus) != state ||
  209. pmu_bus_idle_ack(bus) != state) {
  210. if (++wait_cnt > BUS_IDLE_LOOP)
  211. break;
  212. udelay(1);
  213. }
  214. if (wait_cnt > BUS_IDLE_LOOP)
  215. WARN("%s: can't wait state %d for bus %d (0x%x)\n",
  216. __func__, state, bus,
  217. mmio_read_32(PMU_BASE + PMU2_BUS_IDLE_ST(bus / 32)));
  218. }
  219. static void pmu_qch_pwr_ctlr(uint32_t msk, uint32_t state)
  220. {
  221. uint32_t wait_cnt = 0;
  222. if (state != 0)
  223. state = msk;
  224. mmio_write_32(PMU_BASE + PMU2_QCHANNEL_PWR_SFTCON,
  225. BITS_WITH_WMASK(state, msk, 0));
  226. while ((mmio_read_32(PMU_BASE + PMU2_QCHANNEL_STATUS) & msk) != state) {
  227. if (++wait_cnt > QCH_PWR_LOOP)
  228. break;
  229. udelay(1);
  230. }
  231. if (wait_cnt > BUS_IDLE_LOOP)
  232. WARN("%s: can't wait qch:0x%x to state:0x%x (0x%x)\n",
  233. __func__, msk, state,
  234. mmio_read_32(PMU_BASE + PMU2_QCHANNEL_STATUS));
  235. }
  236. static inline uint32_t pmu_power_domain_chain_st(uint32_t pd)
  237. {
  238. return mmio_read_32(PMU_BASE + PMU2_PWR_CHAIN1_ST(pd / 32)) & BIT(pd % 32) ?
  239. pmu_pd_on :
  240. pmu_pd_off;
  241. }
  242. static inline uint32_t pmu_power_domain_mem_st(uint32_t pd)
  243. {
  244. return mmio_read_32(PMU_BASE + PMU2_PWR_MEM_ST(pd / 32)) & BIT(pd % 32) ?
  245. pmu_pd_off :
  246. pmu_pd_on;
  247. }
  248. static inline uint32_t pmu_power_domain_st(uint32_t pd)
  249. {
  250. int8_t pd_repair = pd_repair_map[pd];
  251. if (pd_repair >= 0)
  252. return mmio_read_32(PMU_BASE + PMU2_BISR_STATUS(4)) & BIT(pd_repair) ?
  253. pmu_pd_on :
  254. pmu_pd_off;
  255. else
  256. return mmio_read_32(PMU_BASE + PMU2_PWR_GATE_ST(pd / 32)) & BIT(pd % 32) ?
  257. pmu_pd_off :
  258. pmu_pd_on;
  259. }
  260. static int pmu_power_domain_pd_to_mem_st(uint32_t pd, uint32_t *pd_mem_st)
  261. {
  262. uint32_t mem_st;
  263. switch (pd) {
  264. case PD_NPUTOP:
  265. mem_st = PD_NPU_TOP_MEM_ST;
  266. break;
  267. case PD_NPU1:
  268. mem_st = PD_NPU1_MEM_ST;
  269. break;
  270. case PD_NPU2:
  271. mem_st = PD_NPU2_MEM_ST;
  272. break;
  273. case PD_VENC0:
  274. mem_st = PD_VENC0_MEM_ST;
  275. break;
  276. case PD_VENC1:
  277. mem_st = PD_VENC1_MEM_ST;
  278. break;
  279. case PD_RKVDEC0:
  280. mem_st = PD_RKVDEC0_MEM_ST;
  281. break;
  282. case PD_RKVDEC1:
  283. mem_st = PD_RKVDEC1_MEM_ST;
  284. break;
  285. case PD_RGA30:
  286. mem_st = PD_RGA30_MEM_ST;
  287. break;
  288. case PD_AV1:
  289. mem_st = PD_AV1_MEM_ST;
  290. break;
  291. case PD_VI:
  292. mem_st = PD_VI_MEM_ST;
  293. break;
  294. case PD_FEC:
  295. mem_st = PD_FEC_MEM_ST;
  296. break;
  297. case PD_ISP1:
  298. mem_st = PD_ISP1_MEM_ST;
  299. break;
  300. case PD_RGA31:
  301. mem_st = PD_RGA31_MEM_ST;
  302. break;
  303. case PD_VOP:
  304. mem_st = PD_VOP_MEM_ST;
  305. break;
  306. case PD_VO0:
  307. mem_st = PD_VO0_MEM_ST;
  308. break;
  309. case PD_VO1:
  310. mem_st = PD_VO1_MEM_ST;
  311. break;
  312. case PD_AUDIO:
  313. mem_st = PD_AUDIO_MEM_ST;
  314. break;
  315. case PD_PHP:
  316. mem_st = PD_PHP_MEM_ST;
  317. break;
  318. case PD_GMAC:
  319. mem_st = PD_GMAC_MEM_ST;
  320. break;
  321. case PD_PCIE:
  322. mem_st = PD_PCIE_MEM_ST;
  323. break;
  324. case PD_NVM0:
  325. mem_st = PD_NVM0_MEM_ST;
  326. break;
  327. case PD_SDIO:
  328. mem_st = PD_SDIO_MEM_ST;
  329. break;
  330. case PD_USB:
  331. mem_st = PD_USB_MEM_ST;
  332. break;
  333. case PD_SDMMC:
  334. mem_st = PD_SDMMC_MEM_ST;
  335. break;
  336. default:
  337. return -EINVAL;
  338. }
  339. *pd_mem_st = mem_st;
  340. return 0;
  341. }
  342. static int pmu_power_domain_reset_mem(uint32_t pd, uint32_t pd_mem_st)
  343. {
  344. uint32_t loop = 0;
  345. int ret = 0;
  346. while (pmu_power_domain_chain_st(pd_mem_st) != pmu_pd_on) {
  347. udelay(1);
  348. loop++;
  349. if (loop >= PD_CTR_LOOP) {
  350. WARN("%s: %d chain up time out\n", __func__, pd);
  351. ret = -EINVAL;
  352. goto error;
  353. }
  354. }
  355. udelay(60);
  356. mmio_write_32(PMU_BASE + PMU2_MEMPWR_GATE_SFTCON(pd / 16),
  357. BITS_WITH_WMASK(pmu_pd_off, 0x1, pd % 16));
  358. dsb();
  359. loop = 0;
  360. while (pmu_power_domain_mem_st(pd_mem_st) != pmu_pd_off) {
  361. udelay(1);
  362. loop++;
  363. if (loop >= PD_CTR_LOOP) {
  364. WARN("%s: %d mem down time out\n", __func__, pd);
  365. ret = -EINVAL;
  366. goto error;
  367. }
  368. }
  369. mmio_write_32(PMU_BASE + PMU2_MEMPWR_GATE_SFTCON(pd / 16),
  370. BITS_WITH_WMASK(pmu_pd_on, 0x1, pd % 16));
  371. dsb();
  372. loop = 0;
  373. while (pmu_power_domain_mem_st(pd_mem_st) != pmu_pd_on) {
  374. udelay(1);
  375. loop++;
  376. if (loop >= PD_CTR_LOOP) {
  377. WARN("%s: %d mem up time out\n", __func__, pd);
  378. ret = -EINVAL;
  379. goto error;
  380. }
  381. }
  382. return 0;
  383. error:
  384. return ret;
  385. }
  386. static int pmu_power_domain_ctr(uint32_t pd, uint32_t pd_state)
  387. {
  388. uint32_t loop = 0;
  389. uint32_t is_mem_on = pmu_pd_off;
  390. uint32_t pd_mem_st;
  391. int ret = 0;
  392. if (pd_state == pmu_pd_on) {
  393. ret = pmu_power_domain_pd_to_mem_st(pd, &pd_mem_st);
  394. if (ret == 0) {
  395. is_mem_on = pmu_power_domain_mem_st(pd_mem_st);
  396. if (is_mem_on == pmu_pd_on)
  397. WARN("%s: %d mem is up\n", __func__, pd);
  398. }
  399. }
  400. mmio_write_32(PMU_BASE + PMU2_PWR_GATE_SFTCON(pd / 16),
  401. BITS_WITH_WMASK(pd_state, 0x1, pd % 16));
  402. dsb();
  403. if (is_mem_on == pmu_pd_on) {
  404. ret = pmu_power_domain_reset_mem(pd, pd_mem_st);
  405. if (ret != 0)
  406. goto out;
  407. WARN("%s: %d mem reset ok\n", __func__, pd);
  408. }
  409. while ((pmu_power_domain_st(pd) != pd_state) && (loop < PD_CTR_LOOP)) {
  410. udelay(1);
  411. loop++;
  412. }
  413. if (pmu_power_domain_st(pd) != pd_state) {
  414. WARN("%s: %d, %d, (0x%x, 0x%x) error!\n", __func__, pd, pd_state,
  415. mmio_read_32(PMU_BASE + PMU2_PWR_GATE_ST(0)),
  416. mmio_read_32(PMU_BASE + PMU2_BISR_STATUS(4)));
  417. ret = -EINVAL;
  418. }
  419. out:
  420. return ret;
  421. }
  422. static int pmu_set_power_domain(uint32_t pd_id, uint32_t pd_state)
  423. {
  424. uint32_t state;
  425. if (pmu_power_domain_st(pd_id) == pd_state)
  426. goto out;
  427. if (pd_state == pmu_pd_on)
  428. pmu_power_domain_ctr(pd_id, pd_state);
  429. state = (pd_state == pmu_pd_off) ? bus_idle : bus_active;
  430. switch (pd_id) {
  431. case PD_GPU:
  432. pmu_bus_idle_req(BUS_ID_GPU, state);
  433. break;
  434. case PD_NPUTOP:
  435. pmu_bus_idle_req(BUS_ID_NPUTOP, state);
  436. break;
  437. case PD_NPU1:
  438. pmu_bus_idle_req(BUS_ID_NPU1, state);
  439. break;
  440. case PD_NPU2:
  441. pmu_bus_idle_req(BUS_ID_NPU2, state);
  442. break;
  443. case PD_VENC0:
  444. pmu_bus_idle_req(BUS_ID_RKVENC0, state);
  445. break;
  446. case PD_VENC1:
  447. pmu_bus_idle_req(BUS_ID_RKVENC1, state);
  448. break;
  449. case PD_RKVDEC0:
  450. pmu_bus_idle_req(BUS_ID_RKVDEC0, state);
  451. break;
  452. case PD_RKVDEC1:
  453. pmu_bus_idle_req(BUS_ID_RKVDEC1, state);
  454. break;
  455. case PD_VDPU:
  456. pmu_bus_idle_req(BUS_ID_VDPU, state);
  457. break;
  458. case PD_AV1:
  459. pmu_bus_idle_req(BUS_ID_AV1, state);
  460. break;
  461. case PD_VI:
  462. pmu_bus_idle_req(BUS_ID_VI, state);
  463. break;
  464. case PD_ISP1:
  465. pmu_bus_idle_req(BUS_ID_ISP, state);
  466. break;
  467. case PD_RGA31:
  468. pmu_bus_idle_req(BUS_ID_RGA31, state);
  469. break;
  470. case PD_VOP:
  471. pmu_bus_idle_req(BUS_ID_VOP_CHANNEL, state);
  472. pmu_bus_idle_req(BUS_ID_VOP, state);
  473. break;
  474. case PD_VO0:
  475. pmu_bus_idle_req(BUS_ID_VO0, state);
  476. break;
  477. case PD_VO1:
  478. pmu_bus_idle_req(BUS_ID_VO1, state);
  479. break;
  480. case PD_AUDIO:
  481. pmu_bus_idle_req(BUS_ID_AUDIO, state);
  482. break;
  483. case PD_PHP:
  484. pmu_bus_idle_req(BUS_ID_PHP, state);
  485. break;
  486. case PD_NVM:
  487. pmu_bus_idle_req(BUS_ID_NVM, state);
  488. break;
  489. case PD_SDIO:
  490. pmu_bus_idle_req(BUS_ID_SDIO, state);
  491. break;
  492. case PD_USB:
  493. pmu_bus_idle_req(BUS_ID_USB, state);
  494. break;
  495. case PD_SECURE:
  496. pmu_bus_idle_req(BUS_ID_SECURE, state);
  497. break;
  498. default:
  499. break;
  500. }
  501. if (pd_state == pmu_pd_off)
  502. pmu_power_domain_ctr(pd_id, pd_state);
  503. out:
  504. return 0;
  505. }
  506. static void pmu_power_domains_suspend(void)
  507. {
  508. ddr_data.qch_pwr_st =
  509. mmio_read_32(PMU_BASE + PMU2_QCHANNEL_STATUS) & PMU2_QCH_PWR_MSK;
  510. ddr_data.pmu_pd_st0 = mmio_read_32(PMU_BASE + PMU2_PWR_GATE_ST(0));
  511. ddr_data.bus_idle_st0 = mmio_read_32(PMU_BASE + PMU2_BUS_IDLE_ST(0));
  512. qos_save();
  513. if ((ddr_data.pmu_pd_st0 & BIT(PD_PHP)) == 0)
  514. pd_php_save();
  515. if ((ddr_data.pmu_pd_st0 & BIT(PD_CRYPTO)) == 0)
  516. pd_crypto_save();
  517. pmu_qch_pwr_ctlr(0x20, 1);
  518. pmu_qch_pwr_ctlr(0x40, 1);
  519. pmu_qch_pwr_ctlr(0x1, 1);
  520. pmu_qch_pwr_ctlr(0x2, 1);
  521. pmu_qch_pwr_ctlr(0x4, 1);
  522. pmu_qch_pwr_ctlr(0x8, 1);
  523. pmu_qch_pwr_ctlr(0x10, 1);
  524. pmu_bus_idle_req(BUS_ID_VO1USBTOP, bus_idle);
  525. pmu_bus_idle_req(BUS_ID_SECURE_VO1USB_CHANNEL, bus_idle);
  526. pmu_bus_idle_req(BUS_ID_USB, bus_idle);
  527. pmu_set_power_domain(PD_GPU, pmu_pd_off);
  528. pmu_set_power_domain(PD_NPU1, pmu_pd_off);
  529. pmu_set_power_domain(PD_NPU2, pmu_pd_off);
  530. pmu_set_power_domain(PD_NPUTOP, pmu_pd_off);
  531. pmu_set_power_domain(PD_NPU, pmu_pd_off);
  532. pmu_set_power_domain(PD_RKVDEC1, pmu_pd_off);
  533. pmu_set_power_domain(PD_RKVDEC0, pmu_pd_off);
  534. pmu_set_power_domain(PD_VENC1, pmu_pd_off);
  535. pmu_set_power_domain(PD_VENC0, pmu_pd_off);
  536. pmu_set_power_domain(PD_VCODEC, pmu_pd_off);
  537. pmu_set_power_domain(PD_RGA30, pmu_pd_off);
  538. pmu_set_power_domain(PD_AV1, pmu_pd_off);
  539. pmu_set_power_domain(PD_VDPU, pmu_pd_off);
  540. pmu_set_power_domain(PD_VO0, pmu_pd_off);
  541. pmu_set_power_domain(PD_VO1, pmu_pd_off);
  542. pmu_set_power_domain(PD_VOP, pmu_pd_off);
  543. pmu_set_power_domain(PD_FEC, pmu_pd_off);
  544. pmu_set_power_domain(PD_ISP1, pmu_pd_off);
  545. pmu_set_power_domain(PD_VI, pmu_pd_off);
  546. pmu_set_power_domain(PD_RGA31, pmu_pd_off);
  547. pmu_set_power_domain(PD_AUDIO, pmu_pd_off);
  548. pmu_set_power_domain(PD_GMAC, pmu_pd_off);
  549. pmu_set_power_domain(PD_PCIE, pmu_pd_off);
  550. pmu_set_power_domain(PD_PHP, pmu_pd_off);
  551. pmu_set_power_domain(PD_SDIO, pmu_pd_off);
  552. pmu_set_power_domain(PD_NVM0, pmu_pd_off);
  553. pmu_set_power_domain(PD_NVM, pmu_pd_off);
  554. pmu_set_power_domain(PD_SDMMC, pmu_pd_off);
  555. pmu_set_power_domain(PD_CRYPTO, pmu_pd_off);
  556. }
  557. static void pmu_power_domains_resume(void)
  558. {
  559. int i;
  560. pmu_set_power_domain(PD_CRYPTO, !!(ddr_data.pmu_pd_st0 & BIT(PD_CRYPTO)));
  561. pmu_set_power_domain(PD_SDMMC, !!(ddr_data.pmu_pd_st0 & BIT(PD_SDMMC)));
  562. pmu_set_power_domain(PD_NVM, !!(ddr_data.pmu_pd_st0 & BIT(PD_NVM)));
  563. pmu_set_power_domain(PD_NVM0, !!(ddr_data.pmu_pd_st0 & BIT(PD_NVM0)));
  564. pmu_set_power_domain(PD_SDIO, !!(ddr_data.pmu_pd_st0 & BIT(PD_SDIO)));
  565. pmu_set_power_domain(PD_PHP, !!(ddr_data.pmu_pd_st0 & BIT(PD_PHP)));
  566. pmu_set_power_domain(PD_PCIE, !!(ddr_data.pmu_pd_st0 & BIT(PD_PCIE)));
  567. pmu_set_power_domain(PD_GMAC, !!(ddr_data.pmu_pd_st0 & BIT(PD_GMAC)));
  568. pmu_set_power_domain(PD_AUDIO, !!(ddr_data.pmu_pd_st0 & BIT(PD_AUDIO)));
  569. pmu_set_power_domain(PD_USB, !!(ddr_data.pmu_pd_st0 & BIT(PD_USB)));
  570. pmu_set_power_domain(PD_RGA31, !!(ddr_data.pmu_pd_st0 & BIT(PD_RGA31)));
  571. pmu_set_power_domain(PD_VI, !!(ddr_data.pmu_pd_st0 & BIT(PD_VI)));
  572. pmu_set_power_domain(PD_ISP1, !!(ddr_data.pmu_pd_st0 & BIT(PD_ISP1)));
  573. pmu_set_power_domain(PD_FEC, !!(ddr_data.pmu_pd_st0 & BIT(PD_FEC)));
  574. pmu_set_power_domain(PD_VOP, !!(ddr_data.pmu_pd_st0 & BIT(PD_VOP)));
  575. pmu_set_power_domain(PD_VO1, !!(ddr_data.pmu_pd_st0 & BIT(PD_VO1)));
  576. pmu_set_power_domain(PD_VO0, !!(ddr_data.pmu_pd_st0 & BIT(PD_VO0)));
  577. pmu_set_power_domain(PD_VDPU, !!(ddr_data.pmu_pd_st0 & BIT(PD_VDPU)));
  578. pmu_set_power_domain(PD_AV1, !!(ddr_data.pmu_pd_st0 & BIT(PD_AV1)));
  579. pmu_set_power_domain(PD_RGA30, !!(ddr_data.pmu_pd_st0 & BIT(PD_RGA30)));
  580. pmu_set_power_domain(PD_VCODEC, !!(ddr_data.pmu_pd_st0 & BIT(PD_VCODEC)));
  581. pmu_set_power_domain(PD_VENC0, !!(ddr_data.pmu_pd_st0 & BIT(PD_VENC0)));
  582. pmu_set_power_domain(PD_VENC1, !!(ddr_data.pmu_pd_st0 & BIT(PD_VENC1)));
  583. pmu_set_power_domain(PD_RKVDEC0, !!(ddr_data.pmu_pd_st0 & BIT(PD_RKVDEC0)));
  584. pmu_set_power_domain(PD_RKVDEC1, !!(ddr_data.pmu_pd_st0 & BIT(PD_RKVDEC1)));
  585. pmu_set_power_domain(PD_NPU, !!(ddr_data.pmu_pd_st0 & BIT(PD_NPU)));
  586. pmu_set_power_domain(PD_NPUTOP, !!(ddr_data.pmu_pd_st0 & BIT(PD_NPUTOP)));
  587. pmu_set_power_domain(PD_NPU2, !!(ddr_data.pmu_pd_st0 & BIT(PD_NPU2)));
  588. pmu_set_power_domain(PD_NPU1, !!(ddr_data.pmu_pd_st0 & BIT(PD_NPU1)));
  589. pmu_set_power_domain(PD_GPU, !!(ddr_data.pmu_pd_st0 & BIT(PD_GPU)));
  590. for (i = 0; i < 32; i++)
  591. pmu_bus_idle_req(i, !!(ddr_data.bus_idle_st0 & BIT(i)));
  592. pmu_qch_pwr_ctlr(0x10, !!(ddr_data.qch_pwr_st & 0x10));
  593. pmu_qch_pwr_ctlr(0x8, !!(ddr_data.qch_pwr_st & 0x8));
  594. pmu_qch_pwr_ctlr(0x4, !!(ddr_data.qch_pwr_st & 0x4));
  595. pmu_qch_pwr_ctlr(0x2, !!(ddr_data.qch_pwr_st & 0x2));
  596. pmu_qch_pwr_ctlr(0x1, !!(ddr_data.qch_pwr_st & 0x1));
  597. pmu_qch_pwr_ctlr(0x40, !!(ddr_data.qch_pwr_st & 0x40));
  598. pmu_qch_pwr_ctlr(0x20, !!(ddr_data.qch_pwr_st & 0x20));
  599. if ((ddr_data.pmu_pd_st0 & BIT(PD_CRYPTO)) == 0)
  600. pd_crypto_restore();
  601. if ((ddr_data.pmu_pd_st0 & BIT(PD_PHP)) == 0)
  602. pd_php_restore();
  603. qos_restore();
  604. }
  605. static int cpus_power_domain_on(uint32_t cpu_id)
  606. {
  607. mmio_write_32(PMU_BASE + PMU2_CPU_AUTO_PWR_CON(cpu_id),
  608. BITS_WITH_WMASK(0, 0x1, core_pm_en));
  609. mmio_write_32(PMU_BASE + PMU2_CPU_AUTO_PWR_CON(cpu_id),
  610. BITS_WITH_WMASK(1, 0x1, core_pm_sft_wakeup_en));
  611. dsb();
  612. return 0;
  613. }
  614. static int cpus_power_domain_off(uint32_t cpu_id, uint32_t pd_cfg)
  615. {
  616. uint32_t apm_value = BIT(core_pm_en);
  617. if (pd_cfg == core_pwr_wfi_int)
  618. apm_value |= BIT(core_pm_int_wakeup_en);
  619. mmio_write_32(PMU_BASE + PMU2_CPU_AUTO_PWR_CON(cpu_id),
  620. BITS_WITH_WMASK(apm_value, 0x3, 0));
  621. dsb();
  622. return 0;
  623. }
  624. static inline void cpus_pd_req_enter_wfi(void)
  625. {
  626. /* CORTEX_A55_CPUACTLR_EL1 */
  627. __asm__ volatile ("msr DBGPRCR_EL1, xzr\n"
  628. "mrs x0, S3_0_C15_C2_7\n"
  629. "orr x0, x0, #0x1\n"
  630. "msr S3_0_C15_C2_7, x0\n"
  631. "1:\n"
  632. "isb\n"
  633. "wfi\n"
  634. "b 1b\n");
  635. }
  636. static void nonboot_cpus_off(void)
  637. {
  638. uint32_t boot_cpu, cpu, tmp;
  639. uint32_t exp_st;
  640. uint32_t bcore0_rst_msk = 0, bcore1_rst_msk = 0;
  641. int wait_cnt;
  642. bcore0_rst_msk = CRU_BIGCPU02_RST_MSK | CRU_BIGCPU13_RST_MSK;
  643. bcore1_rst_msk = CRU_BIGCPU02_RST_MSK | CRU_BIGCPU13_RST_MSK;
  644. mmio_write_32(BIGCORE0CRU_BASE + 0xa00, BITS_WITH_WMASK(0, bcore0_rst_msk, 0));
  645. mmio_write_32(BIGCORE1CRU_BASE + 0xa00, BITS_WITH_WMASK(0, bcore1_rst_msk, 0));
  646. wait_cnt = NONBOOT_CPUS_OFF_LOOP;
  647. exp_st = SYS_GRF_BIG_CPUS_WFE;
  648. do {
  649. wait_cnt--;
  650. tmp = mmio_read_32(SYSGRF_BASE + SYS_GRF_SOC_STATUS(3));
  651. tmp &= SYS_GRF_BIG_CPUS_WFE;
  652. } while (tmp != exp_st && wait_cnt);
  653. boot_cpu = plat_my_core_pos();
  654. /* turn off noboot cpus */
  655. for (cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu++) {
  656. if (cpu == boot_cpu)
  657. continue;
  658. cpus_power_domain_off(cpu, core_pwr_wfi);
  659. }
  660. mmio_write_32(SRAM_BASE + 0x08, (uintptr_t)&cpus_pd_req_enter_wfi);
  661. mmio_write_32(SRAM_BASE + 0x04, 0xdeadbeaf);
  662. dsb();
  663. isb();
  664. sev();
  665. wait_cnt = NONBOOT_CPUS_OFF_LOOP;
  666. do {
  667. wait_cnt--;
  668. tmp = mmio_read_32(PMU_BASE + PMU2_CLUSTER_ST);
  669. tmp &= CLUSTER_STS_NONBOOT_CPUS_DWN;
  670. } while (tmp != CLUSTER_STS_NONBOOT_CPUS_DWN && wait_cnt);
  671. if (tmp != CLUSTER_STS_NONBOOT_CPUS_DWN)
  672. ERROR("nonboot cpus status(%x) error!\n", tmp);
  673. }
  674. int rockchip_soc_cores_pwr_dm_on(unsigned long mpidr,
  675. uint64_t entrypoint)
  676. {
  677. uint32_t cpu_id = plat_core_pos_by_mpidr(mpidr);
  678. assert(cpu_id < PLATFORM_CORE_COUNT);
  679. assert(cpuson_flags[cpu_id] == 0);
  680. cpuson_flags[cpu_id] = PMU_CPU_HOTPLUG;
  681. cpuson_entry_point[cpu_id] = entrypoint;
  682. dsb();
  683. flush_dcache_range((uintptr_t)cpuson_flags, sizeof(cpuson_flags));
  684. flush_dcache_range((uintptr_t)cpuson_entry_point,
  685. sizeof(cpuson_entry_point));
  686. dsb();
  687. isb();
  688. cpus_power_domain_on(cpu_id);
  689. return PSCI_E_SUCCESS;
  690. }
  691. int rockchip_soc_cores_pwr_dm_on_finish(void)
  692. {
  693. uint32_t cpu_id = plat_my_core_pos();
  694. mmio_write_32(PMU_BASE + PMU2_CPU_AUTO_PWR_CON(cpu_id),
  695. BITS_WITH_WMASK(0, 0xf, 0));
  696. return PSCI_E_SUCCESS;
  697. }
  698. int rockchip_soc_cores_pwr_dm_off(void)
  699. {
  700. uint32_t cpu_id = plat_my_core_pos();
  701. cpus_power_domain_off(cpu_id, core_pwr_wfi);
  702. return PSCI_E_SUCCESS;
  703. }
  704. int rockchip_soc_cores_pwr_dm_suspend(void)
  705. {
  706. uint32_t cpu_id = plat_my_core_pos();
  707. assert(cpu_id < PLATFORM_CORE_COUNT);
  708. cpuson_flags[cpu_id] = PMU_CPU_AUTO_PWRDN;
  709. cpuson_entry_point[cpu_id] = plat_get_sec_entrypoint();
  710. dsb();
  711. flush_dcache_range((uintptr_t)cpuson_flags, sizeof(cpuson_flags));
  712. flush_dcache_range((uintptr_t)cpuson_entry_point,
  713. sizeof(cpuson_entry_point));
  714. dsb();
  715. isb();
  716. cpus_power_domain_off(cpu_id, core_pwr_wfi_int);
  717. __asm__ volatile ("msr DBGPRCR_EL1, xzr\n"
  718. "mrs x0, S3_0_C15_C2_7\n"
  719. "orr x0, x0, #0x1\n"
  720. "msr S3_0_C15_C2_7, x0\n");
  721. return PSCI_E_SUCCESS;
  722. }
  723. int rockchip_soc_cores_pwr_dm_resume(void)
  724. {
  725. uint32_t cpu_id = plat_my_core_pos();
  726. mmio_write_32(PMU_BASE + PMU2_CPU_AUTO_PWR_CON(cpu_id),
  727. BITS_WITH_WMASK(0, 0x3, 0));
  728. dsb();
  729. return PSCI_E_SUCCESS;
  730. }
  731. static void ddr_sleep_config(void)
  732. {
  733. int i;
  734. if (pmu_power_domain_st(PD_DDR01) == 0) {
  735. ddr_data.ddrgrf_chn_con0[0] =
  736. mmio_read_32(DDR01GRF_BASE + DDRGRF_CHA_CON(0));
  737. ddr_data.ddrgrf_chn_con0[1] =
  738. mmio_read_32(DDR01GRF_BASE + DDRGRF_CHB_CON(0));
  739. ddr_data.ddrgrf_chn_con1[0] =
  740. mmio_read_32(DDR01GRF_BASE + DDRGRF_CHA_CON(1));
  741. ddr_data.ddrgrf_chn_con1[1] =
  742. mmio_read_32(DDR01GRF_BASE + DDRGRF_CHB_CON(1));
  743. ddr_data.ddrgrf_chn_con2[0] =
  744. mmio_read_32(DDR01GRF_BASE + DDRGRF_CHA_CON(2));
  745. ddr_data.ddrgrf_chn_con2[1] =
  746. mmio_read_32(DDR01GRF_BASE + DDRGRF_CHB_CON(2));
  747. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHA_CON(2), 0x20002000);
  748. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHB_CON(2), 0x20002000);
  749. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHA_CON(2), 0x08000000);
  750. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHB_CON(2), 0x08000000);
  751. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHA_CON(0), 0x00200020);
  752. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHB_CON(0), 0x00200020);
  753. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHA_CON(1), 0x00400040);
  754. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHB_CON(1), 0x00400040);
  755. }
  756. if (pmu_power_domain_st(PD_DDR23) == 0) {
  757. ddr_data.ddrgrf_chn_con0[2] =
  758. mmio_read_32(DDR23GRF_BASE + DDRGRF_CHA_CON(0));
  759. ddr_data.ddrgrf_chn_con0[3] =
  760. mmio_read_32(DDR23GRF_BASE + DDRGRF_CHB_CON(0));
  761. ddr_data.ddrgrf_chn_con1[2] =
  762. mmio_read_32(DDR23GRF_BASE + DDRGRF_CHA_CON(1));
  763. ddr_data.ddrgrf_chn_con1[3] =
  764. mmio_read_32(DDR23GRF_BASE + DDRGRF_CHB_CON(1));
  765. ddr_data.ddrgrf_chn_con2[2] =
  766. mmio_read_32(DDR23GRF_BASE + DDRGRF_CHA_CON(2));
  767. ddr_data.ddrgrf_chn_con2[3] =
  768. mmio_read_32(DDR23GRF_BASE + DDRGRF_CHB_CON(2));
  769. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHA_CON(2), 0x20002000);
  770. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHB_CON(2), 0x20002000);
  771. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHA_CON(2), 0x08000000);
  772. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHB_CON(2), 0x08000000);
  773. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHA_CON(0), 0x00200020);
  774. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHB_CON(0), 0x00200020);
  775. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHA_CON(1), 0x00400040);
  776. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHB_CON(1), 0x00400040);
  777. }
  778. for (i = 0; i < DDR_CHN_CNT; i++) {
  779. ddr_data.pmu1_ddr_pwr_sft_con[i] =
  780. mmio_read_32(PMU_BASE + PMU1_DDR_PWR_SFTCON(i));
  781. mmio_write_32(PMU_BASE + PMU1_DDR_PWR_SFTCON(i), 0x0fff0900);
  782. }
  783. }
  784. static void ddr_sleep_config_restore(void)
  785. {
  786. int i;
  787. for (i = 0; i < DDR_CHN_CNT; i++) {
  788. mmio_write_32(PMU_BASE + PMU1_DDR_PWR_SFTCON(i),
  789. 0x0fff0000 | ddr_data.pmu1_ddr_pwr_sft_con[i]);
  790. }
  791. if (pmu_power_domain_st(PD_DDR01) == 0) {
  792. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHA_CON(1),
  793. 0x00400000 | ddr_data.ddrgrf_chn_con1[0]);
  794. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHB_CON(1),
  795. 0x00400000 | ddr_data.ddrgrf_chn_con1[1]);
  796. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHA_CON(0),
  797. 0x00200000 | ddr_data.ddrgrf_chn_con0[0]);
  798. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHB_CON(0),
  799. 0x00200000 | ddr_data.ddrgrf_chn_con0[1]);
  800. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHA_CON(2),
  801. 0x28000000 | ddr_data.ddrgrf_chn_con2[0]);
  802. mmio_write_32(DDR01GRF_BASE + DDRGRF_CHB_CON(2),
  803. 0x28000000 | ddr_data.ddrgrf_chn_con2[1]);
  804. }
  805. if (pmu_power_domain_st(PD_DDR23) == 0) {
  806. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHA_CON(1),
  807. 0x00400000 | ddr_data.ddrgrf_chn_con1[2]);
  808. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHB_CON(1),
  809. 0x00400000 | ddr_data.ddrgrf_chn_con1[3]);
  810. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHA_CON(0),
  811. 0x00200000 | ddr_data.ddrgrf_chn_con0[2]);
  812. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHB_CON(0),
  813. 0x00200000 | ddr_data.ddrgrf_chn_con0[3]);
  814. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHA_CON(2),
  815. 0x28000000 | ddr_data.ddrgrf_chn_con2[2]);
  816. mmio_write_32(DDR23GRF_BASE + DDRGRF_CHB_CON(2),
  817. 0x28000000 | ddr_data.ddrgrf_chn_con2[3]);
  818. }
  819. }
  820. static void pmu_sleep_config(void)
  821. {
  822. uint32_t pmu1_pwr_con, pmu1_wkup_int_con, pmu1_cru_pwr_con;
  823. uint32_t pmu1_ddr_pwr_con, pmu1_pll_pd_con[2] = {0};
  824. uint32_t pmu2_dsu_pwr_con, pmu2_core_pwr_con, pmu2_clst_idle_con;
  825. uint32_t pmu2_bus_idle_con[3] = {0}, pmu2_pwr_gate_con[3] = {0};
  826. uint32_t pmu2_vol_gate_con[3] = {0}, pmu2_qch_pwr_con = 0;
  827. int i;
  828. ddr_data.pmu1grf_soc_con7 = mmio_read_32(PMU1GRF_BASE + PMU1_GRF_SOC_CON(7));
  829. ddr_data.pmu1grf_soc_con8 = mmio_read_32(PMU1GRF_BASE + PMU1_GRF_SOC_CON(8));
  830. ddr_data.pmu1grf_soc_con9 = mmio_read_32(PMU1GRF_BASE + PMU1_GRF_SOC_CON(9));
  831. ddr_data.pmu1sgrf_soc_con14 = mmio_read_32(PMU1SGRF_BASE + PMU1_SGRF_SOC_CON(14));
  832. ddr_data.pmu0sgrf_soc_con1 = mmio_read_32(PMU0SGRF_BASE + PMU0_SGRF_SOC_CON(1));
  833. ddr_data.pmu0grf_soc_con1 = mmio_read_32(PMU0GRF_BASE + PMU0_GRF_SOC_CON(1));
  834. ddr_data.pmu2_vol_gate_con[0] = mmio_read_32(PMU_BASE + PMU2_VOL_GATE_CON(0));
  835. ddr_data.pmu2_vol_gate_con[1] = mmio_read_32(PMU_BASE + PMU2_VOL_GATE_CON(1));
  836. ddr_data.pmu2_vol_gate_con[2] = mmio_read_32(PMU_BASE + PMU2_VOL_GATE_CON(2));
  837. ddr_data.pmu2_submem_gate_sft_con0 =
  838. mmio_read_32(PMU_BASE + PMU2_MEMPWR_MD_GATE_SFTCON(0));
  839. /* save pmic_sleep iomux gpio0_a4 */
  840. ddr_data.gpio0a_iomux_l = mmio_read_32(PMU0IOC_BASE + 0);
  841. ddr_data.gpio0a_iomux_h = mmio_read_32(PMU0IOC_BASE + 4);
  842. ddr_data.pmu0grf_soc_con3 = mmio_read_32(PMU0GRF_BASE + PMU0_GRF_SOC_CON(3));
  843. /* PMU1 repair disable */
  844. mmio_write_32(PMU0GRF_BASE + PMU0_GRF_SOC_CON(0), 0x00010000);
  845. /* set pmic_sleep iomux */
  846. mmio_write_32(PMU0IOC_BASE + 0,
  847. BITS_WITH_WMASK(1, 0xf, 8) |
  848. BITS_WITH_WMASK(1, 0xfu, 12));
  849. /* set tsadc_shut_m0 pin iomux to gpio */
  850. mmio_write_32(PMU0IOC_BASE + 0,
  851. BITS_WITH_WMASK(0, 0xf, 4));
  852. /* set spi2_cs0/1 pin iomux to gpio */
  853. mmio_write_32(PMU0IOC_BASE + 8,
  854. BITS_WITH_WMASK(0, 0xff, 0));
  855. /* sleep 1~2 src select */
  856. mmio_write_32(PMU0GRF_BASE + PMU0_GRF_SOC_CON(3),
  857. BITS_WITH_WMASK(0x8, 0xf, 0) |
  858. BITS_WITH_WMASK(0x8, 0xf, 4) |
  859. BITS_WITH_WMASK(0x0, 0x3, 8));
  860. pmu1_wkup_int_con = BIT(WAKEUP_GPIO0_INT_EN) |
  861. BIT(WAKEUP_CPU0_INT_EN);
  862. pmu1_pwr_con = BIT(powermode_en);
  863. pmu1_cru_pwr_con =
  864. BIT(alive_osc_mode_en) |
  865. BIT(power_off_en) |
  866. BIT(pd_clk_src_gate_en);
  867. pmu1_ddr_pwr_con = 0;
  868. pmu2_dsu_pwr_con =
  869. BIT(DSU_PWRDN_EN) |
  870. BIT(DSU_PWROFF_EN);
  871. pmu2_core_pwr_con = BIT(CORE_PWRDN_EN);
  872. pmu2_clst_idle_con =
  873. BIT(IDLE_REQ_BIGCORE0_EN) |
  874. BIT(IDLE_REQ_BIGCORE1_EN) |
  875. BIT(IDLE_REQ_DSU_EN) |
  876. BIT(IDLE_REQ_LITDSU_EN) |
  877. BIT(IDLE_REQ_ADB400_CORE_QCH_EN);
  878. pmu1_pll_pd_con[0] =
  879. BIT(B0PLL_PD_EN) |
  880. BIT(B1PLL_PD_EN) |
  881. BIT(LPLL_PD_EN) |
  882. BIT(V0PLL_PD_EN) |
  883. BIT(AUPLL_PD_EN) |
  884. BIT(GPLL_PD_EN) |
  885. BIT(CPLL_PD_EN) |
  886. BIT(NPLL_PD_EN);
  887. pmu1_pll_pd_con[1] =
  888. BIT(PPLL_PD_EN) |
  889. BIT(SPLL_PD_EN);
  890. pmu2_bus_idle_con[0] = 0;
  891. pmu2_bus_idle_con[1] =
  892. BIT(BUS_ID_SECURE - 16) |
  893. BIT(BUS_ID_SECURE_CENTER_CHANNEL - 16) |
  894. BIT(BUS_ID_CENTER_CHANNEL - 16);
  895. pmu2_bus_idle_con[2] =
  896. BIT(BUS_ID_MSCH - 32) |
  897. BIT(BUS_ID_BUS - 32) |
  898. BIT(BUS_ID_TOP - 32);
  899. pmu2_pwr_gate_con[0] = 0;
  900. pmu2_pwr_gate_con[1] = BIT(PD_SECURE - 16);
  901. pmu2_pwr_gate_con[2] = 0;
  902. pmu2_qch_pwr_con = 0;
  903. pmu2_vol_gate_con[0] = 0x7;
  904. pmu2_vol_gate_con[2] = 0;
  905. mmio_write_32(PMU_BASE + PMU2_CORE_AUTO_PWR_CON(0), 0x00030000);
  906. mmio_write_32(PMU_BASE + PMU2_CORE_AUTO_PWR_CON(1), 0x00030000);
  907. mmio_write_32(PMU_BASE + PMU2_CORE_PWR_CON(0),
  908. WITH_16BITS_WMSK(pmu2_core_pwr_con));
  909. mmio_write_32(PMU_BASE + PMU2_CORE_PWR_CON(1),
  910. WITH_16BITS_WMSK(pmu2_core_pwr_con));
  911. mmio_write_32(PMU_BASE + PMU2_CLUSTER_IDLE_CON,
  912. WITH_16BITS_WMSK(pmu2_clst_idle_con));
  913. mmio_write_32(PMU_BASE + PMU2_DSU_AUTO_PWR_CON, 0x00030000);
  914. mmio_write_32(PMU_BASE + PMU2_DSU_PWR_CON,
  915. WITH_16BITS_WMSK(pmu2_dsu_pwr_con));
  916. mmio_write_32(PMU_BASE + PMU1_OSC_STABLE_CNT_THRESH, 24000);
  917. mmio_write_32(PMU_BASE + PMU1_STABLE_CNT_THRESH, 24000);
  918. mmio_write_32(PMU_BASE + PMU1_WAKEUP_RST_CLR_CNT_THRESH, 24000);
  919. mmio_write_32(PMU_BASE + PMU1_PLL_LOCK_CNT_THRESH, 24000);
  920. mmio_write_32(PMU_BASE + PMU1_PWM_SWITCH_CNT_THRESH, 24000);
  921. mmio_write_32(PMU_BASE + PMU2_CORE0_STABLE_CNT_THRESH, 24000);
  922. mmio_write_32(PMU_BASE + PMU2_CORE0_PWRUP_CNT_THRESH, 24000);
  923. mmio_write_32(PMU_BASE + PMU2_CORE0_PWRDN_CNT_THRESH, 24000);
  924. mmio_write_32(PMU_BASE + PMU2_CORE1_STABLE_CNT_THRESH, 24000);
  925. mmio_write_32(PMU_BASE + PMU2_CORE1_PWRUP_CNT_THRESH, 24000);
  926. mmio_write_32(PMU_BASE + PMU2_CORE1_PWRDN_CNT_THRESH, 24000);
  927. mmio_write_32(PMU_BASE + PMU2_DSU_STABLE_CNT_THRESH, 24000);
  928. mmio_write_32(PMU_BASE + PMU2_DSU_PWRUP_CNT_THRESH, 24000);
  929. mmio_write_32(PMU_BASE + PMU2_DSU_PWRDN_CNT_THRESH, 24000);
  930. /* Config pmu power mode and pmu wakeup source */
  931. mmio_write_32(PMU_BASE + PMU1_INT_MASK_CON,
  932. BITS_WITH_WMASK(1, 0x1, 0));
  933. /* pmu1_pwr_con */
  934. mmio_write_32(PMU_BASE + PMU1_PWR_CON,
  935. WITH_16BITS_WMSK(pmu1_pwr_con));
  936. /* cru_pwr_con */
  937. mmio_write_32(PMU_BASE + PMU1_CRU_PWR_CON,
  938. WITH_16BITS_WMSK(pmu1_cru_pwr_con));
  939. /* wakeup source */
  940. mmio_write_32(PMU_BASE + PMU1_WAKEUP_INT_CON, pmu1_wkup_int_con);
  941. /* ddr pwr con */
  942. for (i = 0; i < DDR_CHN_CNT; i++) {
  943. mmio_write_32(PMU_BASE + PMU1_DDR_PWR_CON(i),
  944. WITH_16BITS_WMSK(pmu1_ddr_pwr_con));
  945. pmu2_bus_idle_con[1] |=
  946. BIT(BUS_ID_MSCH0 - 16 + i);
  947. }
  948. /* pll_pd */
  949. mmio_write_32(PMU_BASE + PMU1_PLLPD_CON(0),
  950. WITH_16BITS_WMSK(pmu1_pll_pd_con[0]));
  951. mmio_write_32(PMU_BASE + PMU1_PLLPD_CON(1),
  952. WITH_16BITS_WMSK(pmu1_pll_pd_con[1]));
  953. /* bypass cpu1~7*/
  954. mmio_write_32(PMU_BASE + PMU2_PWR_CON1, 0x00ff00fe);
  955. /* bus idle */
  956. mmio_write_32(PMU_BASE + PMU2_BUS_IDLE_CON(0),
  957. WITH_16BITS_WMSK(pmu2_bus_idle_con[0]));
  958. mmio_write_32(PMU_BASE + PMU2_BUS_IDLE_CON(1),
  959. WITH_16BITS_WMSK(pmu2_bus_idle_con[1]));
  960. mmio_write_32(PMU_BASE + PMU2_BUS_IDLE_CON(2),
  961. WITH_16BITS_WMSK(pmu2_bus_idle_con[2]));
  962. mmio_write_32(PMU_BASE + PMU2_BUS_IDLE_CON(2),
  963. 0xf000f000);
  964. /* power gate */
  965. mmio_write_32(PMU_BASE + PMU2_PWR_GATE_CON(0),
  966. WITH_16BITS_WMSK(pmu2_pwr_gate_con[0]));
  967. mmio_write_32(PMU_BASE + PMU2_PWR_GATE_CON(1),
  968. WITH_16BITS_WMSK(pmu2_pwr_gate_con[1]));
  969. mmio_write_32(PMU_BASE + PMU2_PWR_GATE_CON(2),
  970. WITH_16BITS_WMSK(pmu2_pwr_gate_con[2]));
  971. /* vol gate */
  972. mmio_write_32(PMU_BASE + PMU2_VOL_GATE_CON(0),
  973. BITS_WITH_WMASK(pmu2_vol_gate_con[0], 0x7, 0));
  974. mmio_write_32(PMU_BASE + PMU2_VOL_GATE_CON(1), 0);
  975. mmio_write_32(PMU_BASE + PMU2_VOL_GATE_CON(2),
  976. BITS_WITH_WMASK(pmu2_vol_gate_con[2], 0x3, 0));
  977. /* qch */
  978. mmio_write_32(PMU_BASE + PMU2_QCHANNEL_PWR_CON,
  979. BITS_WITH_WMASK(pmu2_qch_pwr_con, 0x7f, 0));
  980. mmio_write_32(PMU_BASE + PMU2_MEMPWR_MD_GATE_SFTCON(0),
  981. 0x000f000f);
  982. }
  983. static void pmu_sleep_restore(void)
  984. {
  985. mmio_write_32(PMU1GRF_BASE + PMU1_GRF_SOC_CON(7),
  986. WITH_16BITS_WMSK(ddr_data.pmu1grf_soc_con7));
  987. mmio_write_32(PMU1GRF_BASE + PMU1_GRF_SOC_CON(8),
  988. WITH_16BITS_WMSK(ddr_data.pmu1grf_soc_con8));
  989. mmio_write_32(PMU1GRF_BASE + PMU1_GRF_SOC_CON(9),
  990. WITH_16BITS_WMSK(ddr_data.pmu1grf_soc_con9));
  991. mmio_write_32(PMU1SGRF_BASE + PMU1_SGRF_SOC_CON(14),
  992. WITH_16BITS_WMSK(ddr_data.pmu1sgrf_soc_con14));
  993. mmio_write_32(PMU0SGRF_BASE + PMU0_SGRF_SOC_CON(1),
  994. WITH_16BITS_WMSK(ddr_data.pmu0sgrf_soc_con1));
  995. mmio_write_32(PMU0GRF_BASE + PMU0_GRF_SOC_CON(1),
  996. WITH_16BITS_WMSK(ddr_data.pmu0grf_soc_con1));
  997. mmio_write_32(PMU_BASE + PMU2_CORE_PWR_CON(0), 0xffff0000);
  998. mmio_write_32(PMU_BASE + PMU2_CORE_PWR_CON(1), 0xffff0000);
  999. mmio_write_32(PMU_BASE + PMU2_CLUSTER_IDLE_CON, 0xffff0000);
  1000. mmio_write_32(PMU_BASE + PMU2_DSU_PWR_CON, 0xffff0000);
  1001. mmio_write_32(PMU_BASE + PMU2_PWR_CON1, 0xffff0000);
  1002. /* Must clear PMU1_WAKEUP_INT_CON because the wakeup source
  1003. * in PMU1_WAKEUP_INT_CON will wakeup cpus in cpu_auto_pd state.
  1004. */
  1005. mmio_write_32(PMU_BASE + PMU1_WAKEUP_INT_CON, 0);
  1006. mmio_write_32(PMU_BASE + PMU1_PWR_CON, 0xffff0000);
  1007. mmio_write_32(PMU_BASE + PMU1_INT_MASK_CON, 0x00010000);
  1008. mmio_write_32(PMU_BASE + PMU0_WAKEUP_INT_CON, 0x00010000);
  1009. mmio_write_32(PMU_BASE + PMU0_PWR_CON, 0xffff0000);
  1010. mmio_write_32(PMU_BASE + PMU2_VOL_GATE_CON(0),
  1011. WITH_16BITS_WMSK(ddr_data.pmu2_vol_gate_con[0]));
  1012. mmio_write_32(PMU_BASE + PMU2_VOL_GATE_CON(1),
  1013. WITH_16BITS_WMSK(ddr_data.pmu2_vol_gate_con[1]));
  1014. mmio_write_32(PMU_BASE + PMU2_VOL_GATE_CON(2),
  1015. WITH_16BITS_WMSK(ddr_data.pmu2_vol_gate_con[2]));
  1016. mmio_write_32(PMU_BASE + PMU2_MEMPWR_MD_GATE_SFTCON(0),
  1017. WITH_16BITS_WMSK(ddr_data.pmu2_submem_gate_sft_con0));
  1018. mmio_write_32(PMU0GRF_BASE + PMU0_GRF_SOC_CON(3),
  1019. WITH_16BITS_WMSK(ddr_data.pmu0grf_soc_con3));
  1020. mmio_write_32(PMU1GRF_BASE + PMU1_GRF_SOC_CON(2),
  1021. WITH_16BITS_WMSK(ddr_data.pmu1grf_soc_con2));
  1022. mmio_write_32(PMU0IOC_BASE + 0x4,
  1023. WITH_16BITS_WMSK(ddr_data.gpio0a_iomux_h));
  1024. mmio_write_32(PMU0IOC_BASE + 0,
  1025. WITH_16BITS_WMSK(ddr_data.gpio0a_iomux_l));
  1026. }
  1027. static void soc_sleep_config(void)
  1028. {
  1029. ddr_data.gpio0b_iomux_l = mmio_read_32(PMU0IOC_BASE + 0x8);
  1030. pmu_sleep_config();
  1031. ddr_sleep_config();
  1032. }
  1033. static void soc_sleep_restore(void)
  1034. {
  1035. ddr_sleep_config_restore();
  1036. pmu_sleep_restore();
  1037. mmio_write_32(PMU0IOC_BASE + 0x8, WITH_16BITS_WMSK(ddr_data.gpio0b_iomux_l));
  1038. }
  1039. static void pm_pll_suspend(void)
  1040. {
  1041. ddr_data.cru_mode_con = mmio_read_32(CRU_BASE + 0x280);
  1042. ddr_data.busscru_mode_con = mmio_read_32(BUSSCRU_BASE + 0x280);
  1043. ddr_data.pmu2_bisr_con0 = mmio_read_32(PMU_BASE + PMU2_BISR_CON(0));
  1044. ddr_data.cpll_con0 = mmio_read_32(CRU_BASE + CRU_PLLS_CON(2, 0));
  1045. ddr_data.pmu1cru_clksel_con1 = mmio_read_32(PMU1CRU_BASE + CRU_CLKSEL_CON(1));
  1046. /* disable bisr_init */
  1047. mmio_write_32(PMU_BASE + PMU2_BISR_CON(0), BITS_WITH_WMASK(0, 0x1, 0));
  1048. /* cpll bypass */
  1049. mmio_write_32(CRU_BASE + CRU_PLLS_CON(2, 0), BITS_WITH_WMASK(1u, 1u, 15));
  1050. }
  1051. static void pm_pll_restore(void)
  1052. {
  1053. pm_pll_wait_lock(CRU_BASE + CRU_PLLS_CON(2, 0));
  1054. mmio_write_32(CRU_BASE + 0x280, WITH_16BITS_WMSK(ddr_data.cru_mode_con));
  1055. mmio_write_32(BUSSCRU_BASE + 0x280, WITH_16BITS_WMSK(ddr_data.busscru_mode_con));
  1056. mmio_write_32(CRU_BASE + CRU_PLLS_CON(2, 0), WITH_16BITS_WMSK(ddr_data.cpll_con0));
  1057. dsb();
  1058. isb();
  1059. mmio_write_32(PMU_BASE + PMU2_BISR_CON(0), WITH_16BITS_WMSK(ddr_data.pmu2_bisr_con0));
  1060. }
  1061. int rockchip_soc_sys_pwr_dm_suspend(void)
  1062. {
  1063. clk_gate_con_save();
  1064. clk_gate_con_disable();
  1065. psram_sleep_cfg->pm_flag &= ~PM_WARM_BOOT_BIT;
  1066. pmu_power_domains_suspend();
  1067. soc_sleep_config();
  1068. dsu_core_save();
  1069. pm_pll_suspend();
  1070. return 0;
  1071. }
  1072. int rockchip_soc_sys_pwr_dm_resume(void)
  1073. {
  1074. pm_pll_restore();
  1075. dsu_core_restore();
  1076. soc_sleep_restore();
  1077. pmu_power_domains_resume();
  1078. plat_rockchip_gic_cpuif_enable();
  1079. psram_sleep_cfg->pm_flag |= PM_WARM_BOOT_BIT;
  1080. clk_gate_con_restore();
  1081. return 0;
  1082. }
  1083. void __dead2 rockchip_soc_cores_pd_pwr_dn_wfi(const
  1084. psci_power_state_t *target_state)
  1085. {
  1086. psci_power_down_wfi();
  1087. }
  1088. void __dead2 rockchip_soc_sys_pd_pwr_dn_wfi(void)
  1089. {
  1090. cpus_pd_req_enter_wfi();
  1091. psci_power_down_wfi();
  1092. }
  1093. void __dead2 rockchip_soc_soft_reset(void)
  1094. {
  1095. /* pll slow mode */
  1096. mmio_write_32(CRU_BASE + 0x280, 0x03ff0000);
  1097. mmio_write_32(BIGCORE0CRU_BASE + 0x280, 0x00030000);
  1098. mmio_write_32(BIGCORE0CRU_BASE + 0x300, 0x60000000);
  1099. mmio_write_32(BIGCORE0CRU_BASE + 0x304, 0x00600000);
  1100. mmio_write_32(BIGCORE1CRU_BASE + 0x280, 0x00030000);
  1101. mmio_write_32(BIGCORE1CRU_BASE + 0x300, 0x60000000);
  1102. mmio_write_32(BIGCORE1CRU_BASE + 0x304, 0x00600000);
  1103. mmio_write_32(DSUCRU_BASE + 0x280, 0x00030000);
  1104. mmio_write_32(DSUCRU_BASE + 0x318, 0x30600000);
  1105. mmio_write_32(DSUCRU_BASE + 0x31c, 0x30600000);
  1106. mmio_write_32(DSUCRU_BASE + 0x304, 0x00010000);
  1107. mmio_write_32(BUSSCRU_BASE + 0x280, 0x0003000);
  1108. dsb();
  1109. isb();
  1110. mmio_write_32(CRU_BASE + CRU_GLB_SRST_FST, GLB_SRST_FST_CFG_VAL);
  1111. /*
  1112. * Maybe the HW needs some times to reset the system,
  1113. * so we do not hope the core to execute valid codes.
  1114. */
  1115. psci_power_down_wfi();
  1116. }
  1117. void __dead2 rockchip_soc_system_off(void)
  1118. {
  1119. /* set pmic_sleep pin(gpio0_a2) to gpio mode */
  1120. mmio_write_32(PMU0IOC_BASE + 0, BITS_WITH_WMASK(0, 0xf, 8));
  1121. /* config output */
  1122. mmio_write_32(GPIO0_BASE + GPIO_SWPORT_DDR_L,
  1123. BITS_WITH_WMASK(1, 0x1, 2));
  1124. /* config output high level */
  1125. mmio_write_32(GPIO0_BASE + GPIO_SWPORT_DR_L,
  1126. BITS_WITH_WMASK(1, 0x1, 2));
  1127. dsb();
  1128. /*
  1129. * Maybe the HW needs some times to reset the system,
  1130. * so we do not hope the core to execute valid codes.
  1131. */
  1132. psci_power_down_wfi();
  1133. }
  1134. static void rockchip_pmu_pd_init(void)
  1135. {
  1136. mmio_write_32(PMU_BASE + PMU2_BISR_CON(1), 0xffffffff);
  1137. mmio_write_32(PMU_BASE + PMU2_BISR_CON(2), 0xffffffff);
  1138. mmio_write_32(PMU_BASE + PMU2_BISR_CON(3), 0xffffffff);
  1139. pmu_set_power_domain(PD_PHP, pmu_pd_on);
  1140. pmu_set_power_domain(PD_PCIE, pmu_pd_on);
  1141. pmu_set_power_domain(PD_GMAC, pmu_pd_on);
  1142. pmu_set_power_domain(PD_SECURE, pmu_pd_on);
  1143. pmu_set_power_domain(PD_VOP, pmu_pd_on);
  1144. pmu_set_power_domain(PD_VO0, pmu_pd_on);
  1145. pmu_set_power_domain(PD_VO1, pmu_pd_on);
  1146. }
  1147. #define PLL_LOCKED_TIMEOUT 600000U
  1148. void pm_pll_wait_lock(uint32_t pll_base)
  1149. {
  1150. int delay = PLL_LOCKED_TIMEOUT;
  1151. if ((mmio_read_32(pll_base + CRU_PLL_CON(1)) & CRU_PLLCON1_PWRDOWN) != 0)
  1152. return;
  1153. while (delay-- >= 0) {
  1154. if (mmio_read_32(pll_base + CRU_PLL_CON(6)) &
  1155. CRU_PLLCON6_LOCK_STATUS)
  1156. break;
  1157. udelay(1);
  1158. }
  1159. if (delay <= 0)
  1160. ERROR("Can't wait pll(0x%x) lock\n", pll_base);
  1161. }
  1162. void rockchip_plat_mmu_el3(void)
  1163. {
  1164. /* Nothing todo */
  1165. }
  1166. void plat_rockchip_pmu_init(void)
  1167. {
  1168. int cpu;
  1169. for (cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu++)
  1170. cpuson_flags[cpu] = 0;
  1171. psram_sleep_cfg->sp = PSRAM_SP_TOP;
  1172. psram_sleep_cfg->ddr_func = (uint64_t)ddr_resume;
  1173. psram_sleep_cfg->ddr_data = 0;
  1174. psram_sleep_cfg->ddr_flag = 0;
  1175. psram_sleep_cfg->boot_mpidr = read_mpidr_el1() & 0xffff;
  1176. psram_sleep_cfg->pm_flag = PM_WARM_BOOT_BIT;
  1177. nonboot_cpus_off();
  1178. /*
  1179. * When perform idle operation, corresponding clock can be
  1180. * opened or gated automatically.
  1181. */
  1182. mmio_write_32(PMU_BASE + PMU2_BIU_AUTO_CON(0), 0xffffffff);
  1183. mmio_write_32(PMU_BASE + PMU2_BIU_AUTO_CON(1), 0xffffffff);
  1184. mmio_write_32(PMU_BASE + PMU2_BIU_AUTO_CON(2), 0x00070007);
  1185. rockchip_pmu_pd_init();
  1186. /* grf_con_pmic_sleep_sel
  1187. * pmic sleep function selection
  1188. * 1'b0: From reset pulse generator, can reset external PMIC
  1189. * 1'b1: From pmu block, only support sleep function for external PMIC
  1190. */
  1191. mmio_write_32(PMU0GRF_BASE + PMU0_GRF_SOC_CON(3), 0x03ff0000);
  1192. /* pmusram remap to 0xffff0000 */
  1193. mmio_write_32(PMU0SGRF_BASE + PMU0_SGRF_SOC_CON(2), 0x00030001);
  1194. pm_reg_rgns_init();
  1195. }
  1196. static uint64_t boot_cpu_save[4];
  1197. /* define in .data section */
  1198. static uint32_t need_en_crypto = 1;
  1199. void rockchip_cpu_reset_early(u_register_t arg0, u_register_t arg1,
  1200. u_register_t arg2, u_register_t arg3)
  1201. {
  1202. if (need_en_crypto == 0)
  1203. return;
  1204. /* check the crypto function had been enabled or not */
  1205. if ((mmio_read_32(DSUSGRF_BASE + DSU_SGRF_SOC_CON(4)) & BIT(4)) != 0) {
  1206. /* save x0~x3 */
  1207. boot_cpu_save[0] = arg0;
  1208. boot_cpu_save[1] = arg1;
  1209. boot_cpu_save[2] = arg2;
  1210. boot_cpu_save[3] = arg3;
  1211. /* enable the crypto function */
  1212. mmio_write_32(DSUSGRF_BASE + DSU_SGRF_SOC_CON(4),
  1213. BITS_WITH_WMASK(0, 0x1, 4));
  1214. /* remap pmusram to 0xffff0000 */
  1215. mmio_write_32(PMU0SGRF_BASE + PMU0_SGRF_SOC_CON(2), 0x00030001);
  1216. psram_sleep_cfg->pm_flag = PM_WARM_BOOT_BIT;
  1217. cpuson_flags[0] = PMU_CPU_HOTPLUG;
  1218. cpuson_entry_point[0] = (uintptr_t)BL31_BASE;
  1219. dsb();
  1220. /* Must reset core0 to enable the crypto function.
  1221. * Core0 will boot from pmu_sram and jump to BL31_BASE.
  1222. */
  1223. __asm__ volatile ("mov x0, #3\n"
  1224. "dsb sy\n"
  1225. "msr rmr_el3, x0\n"
  1226. "1:\n"
  1227. "isb\n"
  1228. "wfi\n"
  1229. "b 1b\n");
  1230. } else {
  1231. need_en_crypto = 0;
  1232. /* remap bootrom to 0xffff0000 */
  1233. mmio_write_32(PMU0SGRF_BASE + PMU0_SGRF_SOC_CON(2), 0x00030000);
  1234. /*
  1235. * the crypto function has been enabled,
  1236. * restore the x0~x3.
  1237. */
  1238. __asm__ volatile ("ldr x20, [%0]\n"
  1239. "ldr x21, [%0, 0x8]\n"
  1240. "ldr x22, [%0, 0x10]\n"
  1241. "ldr x23, [%0, 0x18]\n"
  1242. : : "r" (&boot_cpu_save[0]));
  1243. }
  1244. }