apusys_power.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (c) 2023, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <inttypes.h>
  7. /* TF-A system header */
  8. #include <common/debug.h>
  9. #include <drivers/delay_timer.h>
  10. #include <lib/mmio.h>
  11. #include <lib/spinlock.h>
  12. #include <lib/utils_def.h>
  13. #include <lib/xlat_tables/xlat_tables_v2.h>
  14. /* Vendor header */
  15. #include "apusys.h"
  16. #include "apusys_power.h"
  17. #include "apusys_rv.h"
  18. #include <mtk_mmap_pool.h>
  19. static spinlock_t apu_lock;
  20. static bool apusys_top_on;
  21. static int apu_poll(uintptr_t reg, uint32_t mask, uint32_t value, uint32_t timeout_us)
  22. {
  23. uint32_t reg_val, count;
  24. count = timeout_us / APU_POLL_STEP_US;
  25. if (count == 0) {
  26. count = 1;
  27. }
  28. do {
  29. reg_val = mmio_read_32(reg);
  30. if ((reg_val & mask) == value) {
  31. return 0;
  32. }
  33. udelay(APU_POLL_STEP_US);
  34. } while (--count);
  35. ERROR(MODULE_TAG "Timeout polling APU register %#" PRIxPTR "\n", reg);
  36. ERROR(MODULE_TAG "Read value 0x%x, expected 0x%x\n", reg_val,
  37. (value == 0U) ? (reg_val & ~mask) : (reg_val | mask));
  38. return -1;
  39. }
  40. static void apu_backup_restore(enum APU_BACKUP_RESTORE_CTRL ctrl)
  41. {
  42. int i;
  43. static struct apu_restore_data apu_restore_data[] = {
  44. { UP_NORMAL_DOMAIN_NS, 0 },
  45. { UP_PRI_DOMAIN_NS, 0 },
  46. { UP_IOMMU_CTRL, 0 },
  47. { UP_CORE0_VABASE0, 0 },
  48. { UP_CORE0_MVABASE0, 0 },
  49. { UP_CORE0_VABASE1, 0 },
  50. { UP_CORE0_MVABASE1, 0 },
  51. { UP_CORE0_VABASE2, 0 },
  52. { UP_CORE0_MVABASE2, 0 },
  53. { UP_CORE0_VABASE3, 0 },
  54. { UP_CORE0_MVABASE3, 0 },
  55. { MD32_SYS_CTRL, 0 },
  56. { MD32_CLK_CTRL, 0 },
  57. { UP_WAKE_HOST_MASK0, 0 }
  58. };
  59. switch (ctrl) {
  60. case APU_CTRL_BACKUP:
  61. for (i = 0; i < ARRAY_SIZE(apu_restore_data); i++) {
  62. apu_restore_data[i].data = mmio_read_32(apu_restore_data[i].reg);
  63. }
  64. break;
  65. case APU_CTRL_RESTORE:
  66. for (i = 0; i < ARRAY_SIZE(apu_restore_data); i++) {
  67. mmio_write_32(apu_restore_data[i].reg, apu_restore_data[i].data);
  68. }
  69. break;
  70. default:
  71. ERROR(MODULE_TAG "%s invalid op: %d\n", __func__, ctrl);
  72. break;
  73. }
  74. }
  75. static void apu_xpu2apusys_d4_slv_en(enum APU_D4_SLV_CTRL en)
  76. {
  77. switch (en) {
  78. case D4_SLV_OFF:
  79. mmio_setbits_32(BCRM_FMEM_PDN_BASE + INFRA_FMEM_BUS_u_SI21_CTRL_0,
  80. INFRA_FMEM_BUS_u_SI21_CTRL_EN);
  81. mmio_setbits_32(BCRM_FMEM_PDN_BASE + INFRA_FMEM_BUS_u_SI22_CTRL_0,
  82. INFRA_FMEM_BUS_u_SI22_CTRL_EN);
  83. mmio_setbits_32(BCRM_FMEM_PDN_BASE + INFRA_FMEM_BUS_u_SI11_CTRL_0,
  84. INFRA_FMEM_BUS_u_SI11_CTRL_EN);
  85. mmio_setbits_32(BCRM_FMEM_PDN_BASE + INFRA_FMEM_M6M7_BUS_u_SI24_CTRL_0,
  86. INFRA_FMEM_M6M7_BUS_u_SI24_CTRL_EN);
  87. break;
  88. case D4_SLV_ON:
  89. mmio_clrbits_32(BCRM_FMEM_PDN_BASE + INFRA_FMEM_BUS_u_SI21_CTRL_0,
  90. INFRA_FMEM_BUS_u_SI21_CTRL_EN);
  91. mmio_clrbits_32(BCRM_FMEM_PDN_BASE + INFRA_FMEM_BUS_u_SI22_CTRL_0,
  92. INFRA_FMEM_BUS_u_SI22_CTRL_EN);
  93. mmio_clrbits_32(BCRM_FMEM_PDN_BASE + INFRA_FMEM_BUS_u_SI11_CTRL_0,
  94. INFRA_FMEM_BUS_u_SI11_CTRL_EN);
  95. mmio_clrbits_32(BCRM_FMEM_PDN_BASE + INFRA_FMEM_M6M7_BUS_u_SI24_CTRL_0,
  96. INFRA_FMEM_M6M7_BUS_u_SI24_CTRL_EN);
  97. break;
  98. default:
  99. ERROR(MODULE_TAG "%s invalid op: %d\n", __func__, en);
  100. break;
  101. }
  102. }
  103. static void apu_pwr_flow_remote_sync(uint32_t cfg)
  104. {
  105. mmio_write_32(APU_MBOX0_BASE + PWR_FLOW_SYNC_REG, (cfg & 0x1));
  106. }
  107. int apusys_kernel_apusys_pwr_top_on(void)
  108. {
  109. int ret;
  110. spin_lock(&apu_lock);
  111. if (apusys_top_on == true) {
  112. INFO(MODULE_TAG "%s: APUSYS already powered on!\n", __func__);
  113. spin_unlock(&apu_lock);
  114. return 0;
  115. }
  116. apu_pwr_flow_remote_sync(1);
  117. mmio_setbits_32(APU_RPC_BASE + APU_RPC_TOP_SEL_1, AFC_ENA);
  118. mmio_write_32(APU_RPC_BASE + APU_RPC_TOP_CON, REG_WAKEUP_SET);
  119. ret = apu_poll(APU_RPC_BASE + APU_RPC_INTF_PWR_RDY,
  120. PWR_RDY, PWR_RDY, APU_TOP_ON_POLLING_TIMEOUT_US);
  121. if (ret != 0) {
  122. ERROR(MODULE_TAG "%s polling RPC RDY timeout, ret %d\n", __func__, ret);
  123. spin_unlock(&apu_lock);
  124. return ret;
  125. }
  126. ret = apu_poll(APU_RPC_BASE + APU_RPC_STATUS,
  127. RPC_STATUS_RDY, RPC_STATUS_RDY, APU_TOP_ON_POLLING_TIMEOUT_US);
  128. if (ret != 0) {
  129. ERROR(MODULE_TAG "%s polling ARE FSM timeout, ret %d\n", __func__, ret);
  130. spin_unlock(&apu_lock);
  131. return ret;
  132. }
  133. mmio_write_32(APU_VCORE_BASE + APUSYS_VCORE_CG_CLR, CG_CLR);
  134. mmio_write_32(APU_RCX_BASE + APU_RCX_CG_CLR, CG_CLR);
  135. apu_xpu2apusys_d4_slv_en(D4_SLV_OFF);
  136. apu_backup_restore(APU_CTRL_RESTORE);
  137. apusys_top_on = true;
  138. spin_unlock(&apu_lock);
  139. return ret;
  140. }
  141. static void apu_sleep_rpc_rcx(void)
  142. {
  143. mmio_write_32(APU_RPC_BASE + APU_RPC_TOP_CON, REG_WAKEUP_CLR);
  144. dsb();
  145. udelay(10);
  146. mmio_setbits_32(APU_RPC_BASE + APU_RPC_TOP_SEL, (RPC_CTRL | RSV10));
  147. dsb();
  148. udelay(10);
  149. mmio_setbits_32(APU_RPC_BASE + APU_RPC_TOP_CON, CLR_IRQ);
  150. dsb();
  151. udelay(10);
  152. mmio_setbits_32(APU_RPC_BASE + APU_RPC_TOP_CON, SLEEP_REQ);
  153. dsb();
  154. udelay(100);
  155. }
  156. int apusys_kernel_apusys_pwr_top_off(void)
  157. {
  158. int ret;
  159. spin_lock(&apu_lock);
  160. if (apusys_top_on == false) {
  161. INFO(MODULE_TAG "%s: APUSYS already powered off!\n", __func__);
  162. spin_unlock(&apu_lock);
  163. return 0;
  164. }
  165. apu_backup_restore(APU_CTRL_BACKUP);
  166. apu_xpu2apusys_d4_slv_en(D4_SLV_ON);
  167. if (mmio_read_32(APU_MBOX0_BASE + PWR_FLOW_SYNC_REG) == 0) {
  168. apu_pwr_flow_remote_sync(1);
  169. } else {
  170. apu_sleep_rpc_rcx();
  171. }
  172. ret = apu_poll(APU_RPC_BASE + APU_RPC_INTF_PWR_RDY,
  173. PWR_RDY, PWR_OFF, APU_TOP_OFF_POLLING_TIMEOUT_US);
  174. if (ret != 0) {
  175. ERROR(MODULE_TAG "%s timeout to wait RPC sleep (val:%d), ret %d\n",
  176. __func__, APU_TOP_OFF_POLLING_TIMEOUT_US, ret);
  177. spin_unlock(&apu_lock);
  178. return ret;
  179. }
  180. apusys_top_on = false;
  181. spin_unlock(&apu_lock);
  182. return ret;
  183. }
  184. static void get_pll_pcw(const uint32_t clk_rate, uint32_t *r1, uint32_t *r2)
  185. {
  186. unsigned int fvco = clk_rate;
  187. unsigned int pcw_val;
  188. unsigned int postdiv_val = 1;
  189. unsigned int postdiv_reg = 0;
  190. while (fvco <= OUT_CLK_FREQ_MIN) {
  191. postdiv_val = postdiv_val << 1;
  192. postdiv_reg = postdiv_reg + 1;
  193. fvco = fvco << 1;
  194. }
  195. pcw_val = (fvco * (1 << DDS_SHIFT)) / BASIC_CLK_FREQ;
  196. if (postdiv_reg == 0) {
  197. pcw_val = pcw_val * 2;
  198. postdiv_val = postdiv_val << 1;
  199. postdiv_reg = postdiv_reg + 1;
  200. }
  201. *r1 = postdiv_reg;
  202. *r2 = pcw_val;
  203. }
  204. static void apu_pll_init(void)
  205. {
  206. const uint32_t pll_hfctl_cfg[PLL_NUM] = {
  207. PLL4HPLL_FHCTL0_CFG,
  208. PLL4HPLL_FHCTL1_CFG,
  209. PLL4HPLL_FHCTL2_CFG,
  210. PLL4HPLL_FHCTL3_CFG
  211. };
  212. const uint32_t pll_con1[PLL_NUM] = {
  213. PLL4H_PLL1_CON1,
  214. PLL4H_PLL2_CON1,
  215. PLL4H_PLL3_CON1,
  216. PLL4H_PLL4_CON1
  217. };
  218. const uint32_t pll_fhctl_dds[PLL_NUM] = {
  219. PLL4HPLL_FHCTL0_DDS,
  220. PLL4HPLL_FHCTL1_DDS,
  221. PLL4HPLL_FHCTL2_DDS,
  222. PLL4HPLL_FHCTL3_DDS
  223. };
  224. const uint32_t pll_freq_out[PLL_NUM] = {
  225. APUPLL0_DEFAULT_FREQ,
  226. APUPLL1_DEFAULT_FREQ,
  227. APUPLL2_DEFAULT_FREQ,
  228. APUPLL3_DEFAULT_FREQ
  229. };
  230. uint32_t pcw_val, posdiv_val;
  231. int pll_idx;
  232. mmio_setbits_32(APU_PLL_BASE + PLL4HPLL_FHCTL_RST_CON, PLL4H_PLL_HP_SWRSTB);
  233. mmio_setbits_32(APU_PLL_BASE + PLL4HPLL_FHCTL_HP_EN, PLL4H_PLL_HP_EN);
  234. mmio_setbits_32(APU_PLL_BASE + PLL4HPLL_FHCTL_CLK_CON, PLL4H_PLL_HP_CLKEN);
  235. for (pll_idx = 0; pll_idx < PLL_NUM; pll_idx++) {
  236. mmio_setbits_32(APU_PLL_BASE + pll_hfctl_cfg[pll_idx], (FHCTL0_EN | SFSTR0_EN));
  237. posdiv_val = 0;
  238. pcw_val = 0;
  239. get_pll_pcw(pll_freq_out[pll_idx], &posdiv_val, &pcw_val);
  240. mmio_clrsetbits_32(APU_PLL_BASE + pll_con1[pll_idx],
  241. (RG_PLL_POSDIV_MASK << RG_PLL_POSDIV_SFT),
  242. (posdiv_val << RG_PLL_POSDIV_SFT));
  243. mmio_write_32(APU_PLL_BASE + pll_fhctl_dds[pll_idx],
  244. (FHCTL_PLL_TGL_ORG | pcw_val));
  245. }
  246. }
  247. static void apu_acc_init(void)
  248. {
  249. mmio_write_32(APU_ACC_BASE + APU_ACC_CONFG_CLR0, CGEN_SOC);
  250. mmio_write_32(APU_ACC_BASE + APU_ACC_CONFG_SET0, HW_CTRL_EN);
  251. mmio_write_32(APU_ACC_BASE + APU_ACC_CONFG_CLR1, CGEN_SOC);
  252. mmio_write_32(APU_ACC_BASE + APU_ACC_CONFG_SET1, HW_CTRL_EN);
  253. mmio_write_32(APU_ACC_BASE + APU_ACC_CONFG_CLR2, CGEN_SOC);
  254. mmio_write_32(APU_ACC_BASE + APU_ACC_CONFG_SET2, HW_CTRL_EN);
  255. mmio_write_32(APU_ACC_BASE + APU_ACC_AUTO_CTRL_SET2, CLK_REQ_SW_EN);
  256. mmio_write_32(APU_ACC_BASE + APU_ACC_CONFG_CLR3, CGEN_SOC);
  257. mmio_write_32(APU_ACC_BASE + APU_ACC_CONFG_SET3, HW_CTRL_EN);
  258. mmio_write_32(APU_ACC_BASE + APU_ACC_AUTO_CTRL_SET3, CLK_REQ_SW_EN);
  259. mmio_write_32(APU_ACC_BASE + APU_ACC_CLK_INV_EN_SET, CLK_INV_EN);
  260. }
  261. static void apu_buck_off_cfg(void)
  262. {
  263. mmio_write_32(APU_RPC_BASE + APU_RPC_HW_CON, BUCK_PROT_REQ_SET);
  264. dsb();
  265. udelay(10);
  266. mmio_write_32(APU_RPC_BASE + APU_RPC_HW_CON, BUCK_ELS_EN_SET);
  267. dsb();
  268. udelay(10);
  269. mmio_write_32(APU_RPC_BASE + APU_RPC_HW_CON, BUCK_AO_RST_B_CLR);
  270. dsb();
  271. udelay(10);
  272. }
  273. static void apu_pcu_init(void)
  274. {
  275. uint32_t vapu_en_offset = BUCK_VAPU_PMIC_REG_EN_ADDR;
  276. uint32_t vapu_sram_en_offset = BUCK_VAPU_SRAM_PMIC_REG_EN_ADDR;
  277. mmio_write_32(APU_PCU_BASE + APU_PCU_CTRL_SET, AUTO_BUCK_EN);
  278. mmio_write_32(APU_PCU_BASE + APU_PCU_BUCK_STEP_SEL, BUCK_ON_OFF_CMD_EN);
  279. mmio_write_32(APU_PCU_BASE + APU_PCU_BUCK_ON_DAT0_L,
  280. ((vapu_sram_en_offset << BUCK_OFFSET_SFT) + BUCK_ON_CMD));
  281. mmio_write_32(APU_PCU_BASE + APU_PCU_BUCK_ON_DAT0_H, CMD_OP);
  282. mmio_write_32(APU_PCU_BASE + APU_PCU_BUCK_ON_DAT1_L,
  283. ((vapu_en_offset << BUCK_OFFSET_SFT) + BUCK_ON_CMD));
  284. mmio_write_32(APU_PCU_BASE + APU_PCU_BUCK_ON_DAT1_H, CMD_OP);
  285. mmio_write_32(APU_PCU_BASE + APU_PCU_BUCK_OFF_DAT0_L,
  286. ((vapu_en_offset << BUCK_OFFSET_SFT) + BUCK_OFF_CMD));
  287. mmio_write_32(APU_PCU_BASE + APU_PCU_BUCK_OFF_DAT0_H, CMD_OP);
  288. mmio_write_32(APU_PCU_BASE + APU_PCU_BUCK_OFF_DAT1_L,
  289. ((vapu_sram_en_offset << BUCK_OFFSET_SFT) + BUCK_OFF_CMD));
  290. mmio_write_32(APU_PCU_BASE + APU_PCU_BUCK_OFF_DAT1_H, CMD_OP);
  291. mmio_write_32(APU_PCU_BASE + APU_PCU_BUCK_ON_SLE0, APU_PCU_BUCK_ON_SETTLE_TIME);
  292. mmio_write_32(APU_PCU_BASE + APU_PCU_BUCK_ON_SLE1, APU_PCU_BUCK_ON_SETTLE_TIME);
  293. }
  294. static void apu_rpclite_init(void)
  295. {
  296. const uint32_t sleep_type_offset[] = {
  297. APU_RPC_SW_TYPE2,
  298. APU_RPC_SW_TYPE3,
  299. APU_RPC_SW_TYPE4,
  300. APU_RPC_SW_TYPE5,
  301. APU_RPC_SW_TYPE6,
  302. APU_RPC_SW_TYPE7,
  303. APU_RPC_SW_TYPE8,
  304. APU_RPC_SW_TYPE9
  305. };
  306. int ofs_arr_size = ARRAY_SIZE(sleep_type_offset);
  307. int ofs_idx;
  308. for (ofs_idx = 0 ; ofs_idx < ofs_arr_size ; ofs_idx++) {
  309. mmio_clrbits_32(APU_ACX0_RPC_LITE_BASE + sleep_type_offset[ofs_idx],
  310. SW_TYPE);
  311. }
  312. mmio_setbits_32(APU_ACX0_RPC_LITE_BASE + APU_RPC_TOP_SEL, RPC_CTRL);
  313. }
  314. static void apu_rpc_init(void)
  315. {
  316. mmio_clrbits_32(APU_RPC_BASE + APU_RPC_SW_TYPE0, SW_TYPE);
  317. mmio_setbits_32(APU_RPC_BASE + APU_RPC_TOP_SEL, RPC_TOP_CTRL);
  318. mmio_setbits_32(APU_RPC_BASE + APU_RPC_TOP_SEL_1, RPC_TOP_CTRL1);
  319. }
  320. static int apu_are_init(void)
  321. {
  322. int ret;
  323. int are_id = 0;
  324. const uint32_t are_base[APU_ARE_NUM] = { APU_ARE0_BASE, APU_ARE1_BASE, APU_ARE2_BASE };
  325. const uint32_t are_entry2_cfg_l[APU_ARE_NUM] = {
  326. ARE0_ENTRY2_CFG_L,
  327. ARE1_ENTRY2_CFG_L,
  328. ARE2_ENTRY2_CFG_L
  329. };
  330. mmio_setbits_32(APU_AO_CTL_BASE + CSR_DUMMY_0_ADDR, VCORE_ARE_REQ);
  331. ret = apu_poll(APU_ARE2_BASE + APU_ARE_GLO_FSM, ARE_GLO_FSM_IDLE, ARE_GLO_FSM_IDLE,
  332. APU_ARE_POLLING_TIMEOUT_US);
  333. if (ret != 0) {
  334. ERROR(MODULE_TAG "[%s][%d] ARE init timeout\n",
  335. __func__, __LINE__);
  336. return ret;
  337. }
  338. for (are_id = APU_ARE0; are_id < APU_ARE_NUM; are_id++) {
  339. mmio_write_32(are_base[are_id] + APU_ARE_ENTRY0_SRAM_H, ARE_ENTRY0_SRAM_H_INIT);
  340. mmio_write_32(are_base[are_id] + APU_ARE_ENTRY0_SRAM_L, ARE_ENTRY0_SRAM_L_INIT);
  341. mmio_write_32(are_base[are_id] + APU_ARE_ENTRY1_SRAM_H, ARE_ENTRY1_SRAM_H_INIT);
  342. mmio_write_32(are_base[are_id] + APU_ARE_ENTRY1_SRAM_L, ARE_ENTRY1_SRAM_L_INIT);
  343. mmio_write_32(are_base[are_id] + APU_ARE_ENTRY2_SRAM_H, ARE_ENTRY_CFG_H);
  344. mmio_write_32(are_base[are_id] + APU_ARE_ENTRY2_SRAM_L, are_entry2_cfg_l[are_id]);
  345. mmio_read_32(are_base[are_id] + APU_ARE_ENTRY2_SRAM_H);
  346. mmio_read_32(are_base[are_id] + APU_ARE_ENTRY2_SRAM_L);
  347. mmio_write_32(are_base[are_id] + APU_ARE_INI_CTRL, ARE_CONFG_INI);
  348. }
  349. return ret;
  350. }
  351. static void apu_aoc_init(void)
  352. {
  353. mmio_clrbits_32(SPM_BASE + APUSYS_BUCK_ISOLATION, IPU_EXT_BUCK_ISO);
  354. mmio_write_32(APU_RPC_BASE + APU_RPC_HW_CON, BUCK_ELS_EN_CLR);
  355. dsb();
  356. udelay(10);
  357. mmio_write_32(APU_RPC_BASE + APU_RPC_HW_CON, BUCK_AO_RST_B_SET);
  358. dsb();
  359. udelay(10);
  360. mmio_write_32(APU_RPC_BASE + APU_RPC_HW_CON, BUCK_PROT_REQ_CLR);
  361. dsb();
  362. udelay(10);
  363. mmio_write_32(APU_RPC_BASE + APU_RPC_HW_CON, SRAM_AOC_ISO_CLR);
  364. dsb();
  365. udelay(10);
  366. }
  367. static int init_hw_setting(void)
  368. {
  369. int ret;
  370. apu_aoc_init();
  371. apu_pcu_init();
  372. apu_rpc_init();
  373. apu_rpclite_init();
  374. ret = apu_are_init();
  375. if (ret != 0) {
  376. return ret;
  377. }
  378. apu_pll_init();
  379. apu_acc_init();
  380. apu_buck_off_cfg();
  381. return ret;
  382. }
  383. int apusys_power_init(void)
  384. {
  385. int ret;
  386. ret = init_hw_setting();
  387. if (ret != 0) {
  388. ERROR(MODULE_TAG "%s initial fail\n", __func__);
  389. } else {
  390. INFO(MODULE_TAG "%s initial done\n", __func__);
  391. }
  392. return ret;
  393. }