mtspmc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (c) 2019, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <arch_helpers.h>
  8. #include <cortex_a53.h>
  9. #include <cortex_a73.h>
  10. #include <common/debug.h>
  11. #include <lib/mmio.h>
  12. #include <platform_def.h>
  13. #include <mcucfg.h>
  14. #include <spm.h>
  15. #include <drivers/delay_timer.h>
  16. #include <mtspmc.h>
  17. #include "mtspmc_private.h"
  18. static void set_retention(int cluster, int tick)
  19. {
  20. uint64_t cpuectlr;
  21. if (cluster)
  22. cpuectlr = read_a73_cpuectlr_el1();
  23. else
  24. cpuectlr = read_a53_cpuectlr_el1();
  25. cpuectlr &= ~0x7ULL;
  26. cpuectlr |= tick & 0x7;
  27. if (cluster)
  28. write_a73_cpuectlr_el1(cpuectlr);
  29. else
  30. write_a53_cpuectlr_el1(cpuectlr);
  31. }
  32. void spm_enable_cpu_auto_off(int cluster, int cpu)
  33. {
  34. uintptr_t reg = per_cpu(cluster, cpu, MCUCFG_SPARK);
  35. set_retention(cluster, 1);
  36. mmio_clrbits_32(reg, SW_NO_WAIT_Q);
  37. }
  38. void spm_disable_cpu_auto_off(int cluster, int cpu)
  39. {
  40. uintptr_t reg = per_cpu(cluster, cpu, MCUCFG_SPARK);
  41. mmio_setbits_32(reg, SW_NO_WAIT_Q);
  42. set_retention(cluster, 0);
  43. }
  44. void spm_set_cpu_power_off(int cluster, int cpu)
  45. {
  46. mmio_clrbits_32(per_cpu(cluster, cpu, SPM_CPU_PWR), PWRCTRL_PWR_ON);
  47. }
  48. void spm_enable_cluster_auto_off(int cluster)
  49. {
  50. assert(cluster);
  51. mmio_clrbits_32(MCUCFG_MP2_SPMC, SW_NO_WAIT_Q);
  52. mmio_clrbits_32(MCUCFG_MP2_COQ, BIT(0));
  53. mmio_clrbits_32(SPM_SPMC_DORMANT_ENABLE, MP1_SPMC_SRAM_DORMANT_EN);
  54. mmio_clrbits_32(per_cluster(cluster, SPM_CLUSTER_PWR), PWRCTRL_PWR_ON);
  55. }
  56. void mcucfg_set_bootaddr(int cluster, int cpu, uintptr_t bootaddr)
  57. {
  58. uintptr_t reg;
  59. const uintptr_t mp2_bootreg[] = {
  60. MCUCFG_MP2_RVADDR0, MCUCFG_MP2_RVADDR1,
  61. MCUCFG_MP2_RVADDR2, MCUCFG_MP2_RVADDR3 };
  62. if (cluster) {
  63. assert(cpu >= 0 && cpu < 4);
  64. reg = mp2_bootreg[cpu];
  65. } else {
  66. reg = per_cpu(cluster, cpu, MCUCFG_BOOTADDR);
  67. }
  68. mmio_write_32(reg, bootaddr);
  69. }
  70. uintptr_t mcucfg_get_bootaddr(int cluster, int cpu)
  71. {
  72. uintptr_t reg;
  73. const uintptr_t mp2_bootreg[] = {
  74. MCUCFG_MP2_RVADDR0, MCUCFG_MP2_RVADDR1,
  75. MCUCFG_MP2_RVADDR2, MCUCFG_MP2_RVADDR3 };
  76. if (cluster) {
  77. assert(cpu >= 0 && cpu < 4);
  78. reg = mp2_bootreg[cpu];
  79. } else {
  80. reg = per_cpu(cluster, cpu, MCUCFG_BOOTADDR);
  81. }
  82. return mmio_read_32(reg);
  83. }
  84. void mcucfg_init_archstate(int cluster, int cpu, int arm64)
  85. {
  86. uintptr_t reg;
  87. int i;
  88. reg = per_cluster(cluster, MCUCFG_INITARCH);
  89. i = cluster ? 16 : 12;
  90. mmio_setbits_32(reg, (arm64 & 1) << (i + cpu));
  91. }
  92. /**
  93. * Return power state of specified subsystem
  94. *
  95. * @mask: mask to SPM_PWR_STATUS to query the power state
  96. * of one subsystem.
  97. * RETURNS:
  98. * 0 (the subsys was powered off)
  99. * 1 (the subsys was powered on)
  100. */
  101. int spm_get_powerstate(uint32_t mask)
  102. {
  103. return mmio_read_32(SPM_PWR_STATUS) & mask;
  104. }
  105. int spm_get_cluster_powerstate(int cluster)
  106. {
  107. uint32_t mask;
  108. mask = cluster ? PWR_STATUS_MP1_CPUTOP : PWR_STATUS_MP0_CPUTOP;
  109. return spm_get_powerstate(mask);
  110. }
  111. int spm_get_cpu_powerstate(int cluster, int cpu)
  112. {
  113. uint32_t i;
  114. /*
  115. * a quick way to specify the mask of cpu[0-3]/cpu[4-7] in PWR_STATUS
  116. * register which are the BITS[9:12](MP0_CPU0~3) and
  117. * BITS[16:19](MP1_CPU0~3)
  118. */
  119. i = (cluster) ? 16 : 9;
  120. i = 1 << (i + cpu);
  121. return spm_get_powerstate(i);
  122. }
  123. int spmc_init(void)
  124. {
  125. /* enable SPM register control */
  126. mmio_write_32(SPM_POWERON_CONFIG_EN,
  127. PROJECT_CODE | MD_BCLK_CG_EN | BCLK_CG_EN);
  128. #if SPMC_MODE == 1
  129. INFO("SPM: enable SPMC mode\n");
  130. /* 0: SPMC mode 1: Legacy mode */
  131. mmio_write_32(SPM_BYPASS_SPMC, 0);
  132. mmio_clrbits_32(per_cluster(0, SPM_CLUSTER_PWR), PWRCTRL_PWR_ON_2ND);
  133. mmio_clrbits_32(per_cpu(0, 0, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
  134. mmio_clrbits_32(per_cpu(0, 1, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
  135. mmio_clrbits_32(per_cpu(0, 2, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
  136. mmio_clrbits_32(per_cpu(0, 3, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
  137. mmio_setbits_32(per_cpu(0, 1, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
  138. mmio_setbits_32(per_cpu(0, 2, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
  139. mmio_setbits_32(per_cpu(0, 3, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
  140. #endif
  141. mmio_clrbits_32(per_cluster(1, SPM_CLUSTER_PWR), PWRCTRL_PWR_ON_2ND);
  142. mmio_setbits_32(per_cluster(1, SPM_CLUSTER_PWR), PWRCTRL_PWR_RST_B);
  143. mmio_clrbits_32(per_cluster(1, SPM_CLUSTER_PWR), PWRCTRL_PWR_CLK_DIS);
  144. mmio_clrbits_32(per_cpu(1, 0, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
  145. mmio_clrbits_32(per_cpu(1, 1, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
  146. mmio_clrbits_32(per_cpu(1, 2, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
  147. mmio_clrbits_32(per_cpu(1, 3, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
  148. mmio_setbits_32(per_cpu(1, 0, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
  149. mmio_setbits_32(per_cpu(1, 1, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
  150. mmio_setbits_32(per_cpu(1, 2, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
  151. mmio_setbits_32(per_cpu(1, 3, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
  152. return 0;
  153. }
  154. /**
  155. * Power on a core with specified cluster and core index
  156. *
  157. * @cluster: the cluster ID of the CPU which to be powered on
  158. * @cpu: the CPU ID of the CPU which to be powered on
  159. */
  160. void spm_poweron_cpu(int cluster, int cpu)
  161. {
  162. INFO("spmc: power on core %d.%d\n", cluster, cpu);
  163. /* STA_POWER_ON */
  164. /* Start to turn on MP0_CPU0 */
  165. /* Set PWR_RST_B = 1 */
  166. mmio_setbits_32(per_cpu(cluster, cpu, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
  167. /* Set PWR_ON = 1 */
  168. mmio_setbits_32(per_cpu(cluster, cpu, SPM_CPU_PWR), PWRCTRL_PWR_ON);
  169. /* Wait until MP0_CPU0_PWR_STA_MASK = 1 */
  170. while (!spm_get_cpu_powerstate(cluster, cpu))
  171. ;
  172. /* Finish to turn on MP0_CPU0 */
  173. INFO("spmc: power on core %d.%d successfully\n", cluster, cpu);
  174. }
  175. /**
  176. * Power off a core with specified cluster and core index
  177. *
  178. * @cluster: the cluster ID of the CPU which to be powered off
  179. * @cpu: the CPU ID of the CPU which to be powered off
  180. */
  181. void spm_poweroff_cpu(int cluster, int cpu)
  182. {
  183. INFO("spmc: power off core %d.%d\n", cluster, cpu);
  184. /* Start to turn off MP0_CPU0 */
  185. /* Set PWR_ON_2ND = 0 */
  186. mmio_clrbits_32(per_cpu(cluster, cpu, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
  187. /* Set PWR_ON = 0 */
  188. mmio_clrbits_32(per_cpu(cluster, cpu, SPM_CPU_PWR), PWRCTRL_PWR_ON);
  189. /* Wait until MP0_CPU0_PWR_STA_MASK = 0 */
  190. while (spm_get_cpu_powerstate(cluster, cpu))
  191. ;
  192. /* Set PWR_RST_B = 0 */
  193. mmio_clrbits_32(per_cpu(cluster, cpu, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
  194. /* Finish to turn off MP0_CPU0 */
  195. INFO("spmc: power off core %d.%d successfully\n", cluster, cpu);
  196. }
  197. /**
  198. * Power off a cluster with specified index
  199. *
  200. * @cluster: the cluster index which to be powered off
  201. */
  202. void spm_poweroff_cluster(int cluster)
  203. {
  204. uint32_t mask;
  205. uint32_t pwr_rst_ctl;
  206. INFO("spmc: power off cluster %d\n", cluster);
  207. /* Start to turn off MP0_CPUTOP */
  208. /* Set bus protect - step1 : 0 */
  209. mask = (cluster) ? MP1_CPUTOP_PROT_STEP1_0_MASK :
  210. MP0_CPUTOP_PROT_STEP1_0_MASK;
  211. mmio_write_32(INFRA_TOPAXI_PROTECTEN_1_SET, mask);
  212. while ((mmio_read_32(INFRA_TOPAXI_PROTECTEN_STA1_1) & mask) != mask)
  213. ;
  214. /* Set PWR_ON_2ND = 0 */
  215. mmio_clrbits_32(per_cluster(cluster, SPM_CLUSTER_PWR),
  216. PWRCTRL_PWR_ON_2ND);
  217. /* SPMC_DORMANT_ENABLE[0]=0 */
  218. mask = (cluster) ? MP1_SPMC_SRAM_DORMANT_EN : MP0_SPMC_SRAM_DORMANT_EN;
  219. mmio_clrbits_32(SPM_SPMC_DORMANT_ENABLE, mask);
  220. /* Set PWR_ON = 0" */
  221. mmio_clrbits_32(per_cluster(cluster, SPM_CLUSTER_PWR), PWRCTRL_PWR_ON);
  222. /* Wait until MP0_CPUTOP_PWR_STA_MASK = 0 */
  223. while (spm_get_cluster_powerstate(cluster))
  224. ;
  225. /* NOTE
  226. * Following flow only for BIG core cluster. It was from
  227. * application note but not covered in mtcmos_ctrl.c
  228. */
  229. if (cluster) {
  230. pwr_rst_ctl = mmio_read_32(MCUCFG_MP2_PWR_RST_CTL);
  231. mmio_write_32(MCUCFG_MP2_PWR_RST_CTL,
  232. (pwr_rst_ctl & ~SW_RST_B) | TOPAON_APB_MASK);
  233. }
  234. /* CPU_EXT_BUCK_ISO[0]=1 */
  235. if (cluster)
  236. mmio_setbits_32(SPM_CPU_EXT_BUCK_ISO, MP1_EXT_BUCK_ISO);
  237. /* Finish to turn off MP0_CPUTOP */
  238. INFO("spmc: power off cluster %d successfully\n", cluster);
  239. }
  240. /**
  241. * Power on a cluster with specified index
  242. *
  243. * @cluster: the cluster index which to be powered on
  244. */
  245. void spm_poweron_cluster(int cluster)
  246. {
  247. uint32_t mask;
  248. uint32_t pwr_rst_ctl;
  249. INFO("spmc: power on cluster %d\n", cluster);
  250. /* Start to turn on MP1_CPUTOP */
  251. /* NOTE
  252. * Following flow only for BIG core cluster. It was from
  253. * application note but not covered in mtcmos_ctrl.c
  254. */
  255. if (cluster) {
  256. mmio_clrbits_32(MCUCFG_MP2_PWR_RST_CTL, SW_RST_B);
  257. /* CPU_EXT_BUCK_ISO[1]=0 */
  258. /* Set mp<n>_vproc_ext_off to 0 to release vproc isolation control */
  259. mmio_clrbits_32(SPM_CPU_EXT_BUCK_ISO, MP1_EXT_BUCK_ISO);
  260. /* NOTE
  261. * Following flow only for BIG core cluster. It was from
  262. * application note but not covered in mtcmos_ctrl.c
  263. */
  264. pwr_rst_ctl = mmio_read_32(MCUCFG_MP2_PWR_RST_CTL);
  265. mmio_write_32(MCUCFG_MP2_PWR_RST_CTL,
  266. (pwr_rst_ctl | SW_RST_B) & ~TOPAON_APB_MASK);
  267. }
  268. /* Set PWR_ON_2ND = 0 */
  269. mmio_clrbits_32(per_cluster(cluster, SPM_CLUSTER_PWR),
  270. PWRCTRL_PWR_ON_2ND);
  271. /* Set PWR_RST_B = 1 */
  272. mmio_setbits_32(per_cluster(cluster, SPM_CLUSTER_PWR),
  273. PWRCTRL_PWR_RST_B);
  274. /* Set PWR_CLK_DIS = 0 */
  275. mmio_clrbits_32(per_cluster(cluster, SPM_CLUSTER_PWR),
  276. PWRCTRL_PWR_CLK_DIS);
  277. /* Set PWR_ON = 1 */
  278. mmio_setbits_32(per_cluster(cluster, SPM_CLUSTER_PWR), PWRCTRL_PWR_ON);
  279. /* Wait until MP1_CPUTOP_PWR_STA_MASK = 1 */
  280. while (!spm_get_cluster_powerstate(cluster))
  281. ;
  282. /* Release bus protect - step1 : 0 */
  283. mask = (cluster) ? MP1_CPUTOP_PROT_STEP1_0_MASK :
  284. MP0_CPUTOP_PROT_STEP1_0_MASK;
  285. mmio_write_32(INFRA_TOPAXI_PROTECTEN_1_CLR, mask);
  286. /* Finish to turn on MP1_CPUTOP */
  287. INFO("spmc: power on cluster %d successfully\n", cluster);
  288. }