plat_pm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * Copyright (C) 2018 Marvell International Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. * https://spdx.org/licenses
  6. */
  7. #include <assert.h>
  8. #include <common/debug.h>
  9. #include <drivers/arm/gicv2.h>
  10. #include <drivers/console.h>
  11. #include <drivers/delay_timer.h>
  12. #include <drivers/marvell/cache_llc.h>
  13. #include <lib/bakery_lock.h>
  14. #include <lib/mmio.h>
  15. #include <plat/common/platform.h>
  16. #include <armada_common.h>
  17. #include <marvell_pm.h>
  18. #if MSS_SUPPORT
  19. #include <mss_pm_ipc.h>
  20. #endif
  21. #include <plat_marvell.h>
  22. #include <plat_pm_trace.h>
  23. #define MVEBU_PRIVATE_UID_REG 0x30
  24. #define MVEBU_RFU_GLOBL_SW_RST 0x84
  25. #define MVEBU_CCU_RVBAR(cpu) (MVEBU_REGS_BASE + 0x640 + (cpu * 4))
  26. #define MVEBU_CCU_CPU_UN_RESET(cpu) (MVEBU_REGS_BASE + 0x650 + (cpu * 4))
  27. #define MPIDR_CPU_GET(mpidr) ((mpidr) & MPIDR_CPU_MASK)
  28. #define MPIDR_CLUSTER_GET(mpidr) MPIDR_AFFLVL1_VAL((mpidr))
  29. #define MVEBU_GPIO_MASK(index) (1 << (index % 32))
  30. #define MVEBU_MPP_MASK(index) (0xF << (4 * (index % 8)))
  31. #define MVEBU_GPIO_VALUE(index, value) (value << (index % 32))
  32. #define MVEBU_USER_CMD_0_REG (MVEBU_DRAM_MAC_BASE + 0x20)
  33. #define MVEBU_USER_CMD_CH0_OFFSET 28
  34. #define MVEBU_USER_CMD_CH0_MASK (1 << MVEBU_USER_CMD_CH0_OFFSET)
  35. #define MVEBU_USER_CMD_CH0_EN (1 << MVEBU_USER_CMD_CH0_OFFSET)
  36. #define MVEBU_USER_CMD_CS_OFFSET 24
  37. #define MVEBU_USER_CMD_CS_MASK (0xF << MVEBU_USER_CMD_CS_OFFSET)
  38. #define MVEBU_USER_CMD_CS_ALL (0xF << MVEBU_USER_CMD_CS_OFFSET)
  39. #define MVEBU_USER_CMD_SR_OFFSET 6
  40. #define MVEBU_USER_CMD_SR_MASK (0x3 << MVEBU_USER_CMD_SR_OFFSET)
  41. #define MVEBU_USER_CMD_SR_ENTER (0x1 << MVEBU_USER_CMD_SR_OFFSET)
  42. #define MVEBU_MC_PWR_CTRL_REG (MVEBU_DRAM_MAC_BASE + 0x54)
  43. #define MVEBU_MC_AC_ON_DLY_OFFSET 8
  44. #define MVEBU_MC_AC_ON_DLY_MASK (0xF << MVEBU_MC_AC_ON_DLY_OFFSET)
  45. #define MVEBU_MC_AC_ON_DLY_DEF_VAR (8 << MVEBU_MC_AC_ON_DLY_OFFSET)
  46. #define MVEBU_MC_AC_OFF_DLY_OFFSET 4
  47. #define MVEBU_MC_AC_OFF_DLY_MASK (0xF << MVEBU_MC_AC_OFF_DLY_OFFSET)
  48. #define MVEBU_MC_AC_OFF_DLY_DEF_VAR (0xC << MVEBU_MC_AC_OFF_DLY_OFFSET)
  49. #define MVEBU_MC_PHY_AUTO_OFF_OFFSET 0
  50. #define MVEBU_MC_PHY_AUTO_OFF_MASK (1 << MVEBU_MC_PHY_AUTO_OFF_OFFSET)
  51. #define MVEBU_MC_PHY_AUTO_OFF_EN (1 << MVEBU_MC_PHY_AUTO_OFF_OFFSET)
  52. /* this lock synchronize AP multiple cores execution with MSS */
  53. DEFINE_BAKERY_LOCK(pm_sys_lock);
  54. /* Weak definitions may be overridden in specific board */
  55. #pragma weak plat_marvell_get_pm_cfg
  56. /* AP806 CPU power down /power up definitions */
  57. enum CPU_ID {
  58. CPU0,
  59. CPU1,
  60. CPU2,
  61. CPU3
  62. };
  63. #define REG_WR_VALIDATE_TIMEOUT (2000)
  64. #define FEATURE_DISABLE_STATUS_REG \
  65. (MVEBU_REGS_BASE + 0x6F8230)
  66. #define FEATURE_DISABLE_STATUS_CPU_CLUSTER_OFFSET 4
  67. #define FEATURE_DISABLE_STATUS_CPU_CLUSTER_MASK \
  68. (0x1 << FEATURE_DISABLE_STATUS_CPU_CLUSTER_OFFSET)
  69. #ifdef MVEBU_SOC_AP807
  70. #define PWRC_CPUN_CR_PWR_DN_RQ_OFFSET 1
  71. #define PWRC_CPUN_CR_LDO_BYPASS_RDY_OFFSET 0
  72. #else
  73. #define PWRC_CPUN_CR_PWR_DN_RQ_OFFSET 0
  74. #define PWRC_CPUN_CR_LDO_BYPASS_RDY_OFFSET 31
  75. #endif
  76. #define PWRC_CPUN_CR_REG(cpu_id) \
  77. (MVEBU_REGS_BASE + 0x680000 + (cpu_id * 0x10))
  78. #define PWRC_CPUN_CR_PWR_DN_RQ_MASK \
  79. (0x1 << PWRC_CPUN_CR_PWR_DN_RQ_OFFSET)
  80. #define PWRC_CPUN_CR_ISO_ENABLE_OFFSET 16
  81. #define PWRC_CPUN_CR_ISO_ENABLE_MASK \
  82. (0x1 << PWRC_CPUN_CR_ISO_ENABLE_OFFSET)
  83. #define PWRC_CPUN_CR_LDO_BYPASS_RDY_MASK \
  84. (0x1U << PWRC_CPUN_CR_LDO_BYPASS_RDY_OFFSET)
  85. #define CCU_B_PRCRN_REG(cpu_id) \
  86. (MVEBU_REGS_BASE + 0x1A50 + \
  87. ((cpu_id / 2) * (0x400)) + ((cpu_id % 2) * 4))
  88. #define CCU_B_PRCRN_CPUPORESET_STATIC_OFFSET 0
  89. #define CCU_B_PRCRN_CPUPORESET_STATIC_MASK \
  90. (0x1 << CCU_B_PRCRN_CPUPORESET_STATIC_OFFSET)
  91. /* power switch fingers */
  92. #define AP807_PWRC_LDO_CR0_REG \
  93. (MVEBU_REGS_BASE + 0x680000 + 0x100)
  94. #define AP807_PWRC_LDO_CR0_OFFSET 16
  95. #define AP807_PWRC_LDO_CR0_MASK \
  96. (0xff << AP807_PWRC_LDO_CR0_OFFSET)
  97. #define AP807_PWRC_LDO_CR0_VAL 0xfc
  98. /*
  99. * Power down CPU:
  100. * Used to reduce power consumption, and avoid SoC unnecessary temperature rise.
  101. */
  102. static int plat_marvell_cpu_powerdown(int cpu_id)
  103. {
  104. uint32_t reg_val;
  105. int exit_loop = REG_WR_VALIDATE_TIMEOUT;
  106. INFO("Powering down CPU%d\n", cpu_id);
  107. /* 1. Isolation enable */
  108. reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
  109. reg_val |= 0x1 << PWRC_CPUN_CR_ISO_ENABLE_OFFSET;
  110. mmio_write_32(PWRC_CPUN_CR_REG(cpu_id), reg_val);
  111. /* 2. Read and check Isolation enabled - verify bit set to 1 */
  112. do {
  113. reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
  114. exit_loop--;
  115. } while (!(reg_val & (0x1 << PWRC_CPUN_CR_ISO_ENABLE_OFFSET)) &&
  116. exit_loop > 0);
  117. /* 3. Switch off CPU power */
  118. reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
  119. reg_val &= ~PWRC_CPUN_CR_PWR_DN_RQ_MASK;
  120. mmio_write_32(PWRC_CPUN_CR_REG(cpu_id), reg_val);
  121. /* 4. Read and check Switch Off - verify bit set to 0 */
  122. exit_loop = REG_WR_VALIDATE_TIMEOUT;
  123. do {
  124. reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
  125. exit_loop--;
  126. } while (reg_val & PWRC_CPUN_CR_PWR_DN_RQ_MASK && exit_loop > 0);
  127. if (exit_loop <= 0)
  128. goto cpu_poweroff_error;
  129. /* 5. De-Assert power ready */
  130. reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
  131. reg_val &= ~PWRC_CPUN_CR_LDO_BYPASS_RDY_MASK;
  132. mmio_write_32(PWRC_CPUN_CR_REG(cpu_id), reg_val);
  133. /* 6. Assert CPU POR reset */
  134. reg_val = mmio_read_32(CCU_B_PRCRN_REG(cpu_id));
  135. reg_val &= ~CCU_B_PRCRN_CPUPORESET_STATIC_MASK;
  136. mmio_write_32(CCU_B_PRCRN_REG(cpu_id), reg_val);
  137. /* 7. Read and poll on Validate the CPU is out of reset */
  138. exit_loop = REG_WR_VALIDATE_TIMEOUT;
  139. do {
  140. reg_val = mmio_read_32(CCU_B_PRCRN_REG(cpu_id));
  141. exit_loop--;
  142. } while (reg_val & CCU_B_PRCRN_CPUPORESET_STATIC_MASK && exit_loop > 0);
  143. if (exit_loop <= 0)
  144. goto cpu_poweroff_error;
  145. INFO("Successfully powered down CPU%d\n", cpu_id);
  146. return 0;
  147. cpu_poweroff_error:
  148. ERROR("ERROR: Can't power down CPU%d\n", cpu_id);
  149. return -1;
  150. }
  151. /*
  152. * Power down CPUs 1-3 at early boot stage,
  153. * to reduce power consumption and SoC temperature.
  154. * This is triggered by BLE prior to DDR initialization.
  155. *
  156. * Note:
  157. * All CPUs will be powered up by plat_marvell_cpu_powerup on Linux boot stage,
  158. * which is triggered by PSCI ops (pwr_domain_on).
  159. */
  160. int plat_marvell_early_cpu_powerdown(void)
  161. {
  162. uint32_t cpu_cluster_status =
  163. mmio_read_32(FEATURE_DISABLE_STATUS_REG) &
  164. FEATURE_DISABLE_STATUS_CPU_CLUSTER_MASK;
  165. /* if cpu_cluster_status bit is set,
  166. * that means we have only single cluster
  167. */
  168. int cluster_count = cpu_cluster_status ? 1 : 2;
  169. INFO("Powering off unused CPUs\n");
  170. /* CPU1 is in AP806 cluster-0, which always exists, so power it down */
  171. if (plat_marvell_cpu_powerdown(CPU1) == -1)
  172. return -1;
  173. /*
  174. * CPU2-3 are in AP806 2nd cluster (cluster-1),
  175. * which doesn't exists in dual-core systems.
  176. * so need to check if we have dual-core (single cluster)
  177. * or quad-code (2 clusters)
  178. */
  179. if (cluster_count == 2) {
  180. /* CPU2-3 are part of 2nd cluster */
  181. if (plat_marvell_cpu_powerdown(CPU2) == -1)
  182. return -1;
  183. if (plat_marvell_cpu_powerdown(CPU3) == -1)
  184. return -1;
  185. }
  186. return 0;
  187. }
  188. /*
  189. * Power up CPU - part of Linux boot stage
  190. */
  191. static int plat_marvell_cpu_powerup(u_register_t mpidr)
  192. {
  193. uint32_t reg_val;
  194. int cpu_id = MPIDR_CPU_GET(mpidr),
  195. cluster = MPIDR_CLUSTER_GET(mpidr);
  196. int exit_loop = REG_WR_VALIDATE_TIMEOUT;
  197. /* calculate absolute CPU ID */
  198. cpu_id = cluster * PLAT_MARVELL_CLUSTER_CORE_COUNT + cpu_id;
  199. INFO("Powering on CPU%d\n", cpu_id);
  200. #ifdef MVEBU_SOC_AP807
  201. /* Activate 2 power switch fingers */
  202. reg_val = mmio_read_32(AP807_PWRC_LDO_CR0_REG);
  203. reg_val &= ~(AP807_PWRC_LDO_CR0_MASK);
  204. reg_val |= (AP807_PWRC_LDO_CR0_VAL << AP807_PWRC_LDO_CR0_OFFSET);
  205. mmio_write_32(AP807_PWRC_LDO_CR0_REG, reg_val);
  206. udelay(100);
  207. #endif
  208. /* 1. Switch CPU power ON */
  209. reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
  210. reg_val |= 0x1 << PWRC_CPUN_CR_PWR_DN_RQ_OFFSET;
  211. mmio_write_32(PWRC_CPUN_CR_REG(cpu_id), reg_val);
  212. /* 2. Wait for CPU on, up to 100 uSec: */
  213. udelay(100);
  214. /* 3. Assert power ready */
  215. reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
  216. reg_val |= 0x1U << PWRC_CPUN_CR_LDO_BYPASS_RDY_OFFSET;
  217. mmio_write_32(PWRC_CPUN_CR_REG(cpu_id), reg_val);
  218. /* 4. Read & Validate power ready
  219. * used in order to generate 16 Host CPU cycles
  220. */
  221. do {
  222. reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
  223. exit_loop--;
  224. } while (!(reg_val & (0x1U << PWRC_CPUN_CR_LDO_BYPASS_RDY_OFFSET)) &&
  225. exit_loop > 0);
  226. if (exit_loop <= 0)
  227. goto cpu_poweron_error;
  228. /* 5. Isolation disable */
  229. reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
  230. reg_val &= ~PWRC_CPUN_CR_ISO_ENABLE_MASK;
  231. mmio_write_32(PWRC_CPUN_CR_REG(cpu_id), reg_val);
  232. /* 6. Read and check Isolation enabled - verify bit set to 1 */
  233. exit_loop = REG_WR_VALIDATE_TIMEOUT;
  234. do {
  235. reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
  236. exit_loop--;
  237. } while ((reg_val & (0x1 << PWRC_CPUN_CR_ISO_ENABLE_OFFSET)) &&
  238. exit_loop > 0);
  239. /* 7. De Assert CPU POR reset & Core reset */
  240. reg_val = mmio_read_32(CCU_B_PRCRN_REG(cpu_id));
  241. reg_val |= 0x1 << CCU_B_PRCRN_CPUPORESET_STATIC_OFFSET;
  242. mmio_write_32(CCU_B_PRCRN_REG(cpu_id), reg_val);
  243. /* 8. Read & Validate CPU POR reset */
  244. exit_loop = REG_WR_VALIDATE_TIMEOUT;
  245. do {
  246. reg_val = mmio_read_32(CCU_B_PRCRN_REG(cpu_id));
  247. exit_loop--;
  248. } while (!(reg_val & (0x1 << CCU_B_PRCRN_CPUPORESET_STATIC_OFFSET)) &&
  249. exit_loop > 0);
  250. if (exit_loop <= 0)
  251. goto cpu_poweron_error;
  252. INFO("Successfully powered on CPU%d\n", cpu_id);
  253. return 0;
  254. cpu_poweron_error:
  255. ERROR("ERROR: Can't power up CPU%d\n", cpu_id);
  256. return -1;
  257. }
  258. static int plat_marvell_cpu_on(u_register_t mpidr)
  259. {
  260. int cpu_id;
  261. int cluster;
  262. /* Set barierr */
  263. dsbsy();
  264. /* Get cpu number - use CPU ID */
  265. cpu_id = MPIDR_CPU_GET(mpidr);
  266. /* Get cluster number - use affinity level 1 */
  267. cluster = MPIDR_CLUSTER_GET(mpidr);
  268. /* Set CPU private UID */
  269. mmio_write_32(MVEBU_REGS_BASE + MVEBU_PRIVATE_UID_REG, cluster + 0x4);
  270. /* Set the cpu start address to BL1 entry point (align to 0x10000) */
  271. mmio_write_32(MVEBU_CCU_RVBAR(cpu_id),
  272. PLAT_MARVELL_CPU_ENTRY_ADDR >> 16);
  273. /* Get the cpu out of reset */
  274. mmio_write_32(MVEBU_CCU_CPU_UN_RESET(cpu_id), 0x10001);
  275. return 0;
  276. }
  277. /*****************************************************************************
  278. * A8K handler called to check the validity of the power state
  279. * parameter.
  280. *****************************************************************************
  281. */
  282. static int a8k_validate_power_state(unsigned int power_state,
  283. psci_power_state_t *req_state)
  284. {
  285. int pstate = psci_get_pstate_type(power_state);
  286. int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
  287. int i;
  288. if (pwr_lvl > PLAT_MAX_PWR_LVL)
  289. return PSCI_E_INVALID_PARAMS;
  290. /* Sanity check the requested state */
  291. if (pstate == PSTATE_TYPE_STANDBY) {
  292. /*
  293. * It's possible to enter standby only on power level 0
  294. * Ignore any other power level.
  295. */
  296. if (pwr_lvl != MARVELL_PWR_LVL0)
  297. return PSCI_E_INVALID_PARAMS;
  298. req_state->pwr_domain_state[MARVELL_PWR_LVL0] =
  299. MARVELL_LOCAL_STATE_RET;
  300. } else {
  301. for (i = MARVELL_PWR_LVL0; i <= pwr_lvl; i++)
  302. req_state->pwr_domain_state[i] =
  303. MARVELL_LOCAL_STATE_OFF;
  304. }
  305. /*
  306. * We expect the 'state id' to be zero.
  307. */
  308. if (psci_get_pstate_id(power_state))
  309. return PSCI_E_INVALID_PARAMS;
  310. return PSCI_E_SUCCESS;
  311. }
  312. /*****************************************************************************
  313. * A8K handler called when a CPU is about to enter standby.
  314. *****************************************************************************
  315. */
  316. static void a8k_cpu_standby(plat_local_state_t cpu_state)
  317. {
  318. if (!is_pm_fw_running()) {
  319. ERROR("%s: needs to be implemented\n", __func__);
  320. panic();
  321. }
  322. }
  323. /*****************************************************************************
  324. * A8K handler called when a power domain is about to be turned on. The
  325. * mpidr determines the CPU to be turned on.
  326. *****************************************************************************
  327. */
  328. static int a8k_pwr_domain_on(u_register_t mpidr)
  329. {
  330. /* Power up CPU (CPUs 1-3 are powered off at start of BLE) */
  331. plat_marvell_cpu_powerup(mpidr);
  332. #if MSS_SUPPORT
  333. if (is_pm_fw_running()) {
  334. unsigned int target =
  335. ((mpidr & 0xFF) + (((mpidr >> 8) & 0xFF) * 2));
  336. /*
  337. * pm system synchronization - used to synchronize
  338. * multiple core access to MSS
  339. */
  340. bakery_lock_get(&pm_sys_lock);
  341. /* send CPU ON IPC Message to MSS */
  342. mss_pm_ipc_msg_send(target, PM_IPC_MSG_CPU_ON, 0);
  343. /* trigger IPC message to MSS */
  344. mss_pm_ipc_msg_trigger();
  345. /* pm system synchronization */
  346. bakery_lock_release(&pm_sys_lock);
  347. /* trace message */
  348. PM_TRACE(TRACE_PWR_DOMAIN_ON | target);
  349. } else
  350. #endif
  351. {
  352. /* proprietary CPU ON execution flow */
  353. plat_marvell_cpu_on(mpidr);
  354. }
  355. return 0;
  356. }
  357. /*****************************************************************************
  358. * A8K handler called to validate the entry point.
  359. *****************************************************************************
  360. */
  361. static int a8k_validate_ns_entrypoint(uintptr_t entrypoint)
  362. {
  363. return PSCI_E_SUCCESS;
  364. }
  365. /*****************************************************************************
  366. * A8K handler called when a power domain is about to be turned off. The
  367. * target_state encodes the power state that each level should transition to.
  368. *****************************************************************************
  369. */
  370. static void a8k_pwr_domain_off(const psci_power_state_t *target_state)
  371. {
  372. #if MSS_SUPPORT
  373. if (is_pm_fw_running()) {
  374. unsigned int idx = plat_my_core_pos();
  375. /* Prevent interrupts from spuriously waking up this cpu */
  376. gicv2_cpuif_disable();
  377. /* pm system synchronization - used to synchronize multiple
  378. * core access to MSS
  379. */
  380. bakery_lock_get(&pm_sys_lock);
  381. /* send CPU OFF IPC Message to MSS */
  382. mss_pm_ipc_msg_send(idx, PM_IPC_MSG_CPU_OFF, target_state);
  383. /* trigger IPC message to MSS */
  384. mss_pm_ipc_msg_trigger();
  385. /* pm system synchronization */
  386. bakery_lock_release(&pm_sys_lock);
  387. /* trace message */
  388. PM_TRACE(TRACE_PWR_DOMAIN_OFF);
  389. } else {
  390. INFO("%s: is not supported without SCP\n", __func__);
  391. }
  392. #endif
  393. }
  394. /* Get PM config to power off the SoC */
  395. void *plat_marvell_get_pm_cfg(void)
  396. {
  397. return NULL;
  398. }
  399. /*
  400. * This function should be called on restore from
  401. * "suspend to RAM" state when the execution flow
  402. * has to bypass BootROM image to RAM copy and speed up
  403. * the system recovery
  404. *
  405. */
  406. static void plat_marvell_exit_bootrom(void)
  407. {
  408. marvell_exit_bootrom(PLAT_MARVELL_TRUSTED_ROM_BASE);
  409. }
  410. /*
  411. * Prepare for the power off of the system via GPIO
  412. */
  413. static void plat_marvell_power_off_gpio(struct power_off_method *pm_cfg,
  414. register_t *gpio_addr,
  415. register_t *gpio_data)
  416. {
  417. unsigned int gpio;
  418. unsigned int idx;
  419. unsigned int shift;
  420. unsigned int reg;
  421. unsigned int addr;
  422. gpio_info_t *info;
  423. unsigned int tog_bits;
  424. assert((pm_cfg->cfg.gpio.pin_count < PMIC_GPIO_MAX_NUMBER) &&
  425. (pm_cfg->cfg.gpio.step_count < PMIC_GPIO_MAX_TOGGLE_STEP));
  426. /* Prepare GPIOs for PMIC */
  427. for (gpio = 0; gpio < pm_cfg->cfg.gpio.pin_count; gpio++) {
  428. info = &pm_cfg->cfg.gpio.info[gpio];
  429. /* Set PMIC GPIO to output mode */
  430. reg = mmio_read_32(MVEBU_CP_GPIO_DATA_OUT_EN(
  431. info->cp_index, info->gpio_index));
  432. mmio_write_32(MVEBU_CP_GPIO_DATA_OUT_EN(
  433. info->cp_index, info->gpio_index),
  434. reg & ~MVEBU_GPIO_MASK(info->gpio_index));
  435. /* Set the appropriate MPP to GPIO mode */
  436. reg = mmio_read_32(MVEBU_PM_MPP_REGS(info->cp_index,
  437. info->gpio_index));
  438. mmio_write_32(MVEBU_PM_MPP_REGS(info->cp_index,
  439. info->gpio_index),
  440. reg & ~MVEBU_MPP_MASK(info->gpio_index));
  441. }
  442. /* Wait for MPP & GPIO pre-configurations done */
  443. mdelay(pm_cfg->cfg.gpio.delay_ms);
  444. /* Toggle the GPIO values, and leave final step to be triggered
  445. * after DDR self-refresh is enabled
  446. */
  447. for (idx = 0; idx < pm_cfg->cfg.gpio.step_count; idx++) {
  448. tog_bits = pm_cfg->cfg.gpio.seq[idx];
  449. /* The GPIOs must be within same GPIO register,
  450. * thus could get the original value by first GPIO
  451. */
  452. info = &pm_cfg->cfg.gpio.info[0];
  453. reg = mmio_read_32(MVEBU_CP_GPIO_DATA_OUT(
  454. info->cp_index, info->gpio_index));
  455. addr = MVEBU_CP_GPIO_DATA_OUT(info->cp_index, info->gpio_index);
  456. for (gpio = 0; gpio < pm_cfg->cfg.gpio.pin_count; gpio++) {
  457. shift = pm_cfg->cfg.gpio.info[gpio].gpio_index % 32;
  458. if (GPIO_LOW == (tog_bits & (1 << gpio)))
  459. reg &= ~(1 << shift);
  460. else
  461. reg |= (1 << shift);
  462. }
  463. /* Set the GPIO register, for last step just store
  464. * register address and values to system registers
  465. */
  466. if (idx < pm_cfg->cfg.gpio.step_count - 1) {
  467. mmio_write_32(MVEBU_CP_GPIO_DATA_OUT(
  468. info->cp_index, info->gpio_index), reg);
  469. mdelay(pm_cfg->cfg.gpio.delay_ms);
  470. } else {
  471. /* Save GPIO register and address values for
  472. * finishing the power down operation later
  473. */
  474. *gpio_addr = addr;
  475. *gpio_data = reg;
  476. }
  477. }
  478. }
  479. /*
  480. * Prepare for the power off of the system
  481. */
  482. static void plat_marvell_power_off_prepare(struct power_off_method *pm_cfg,
  483. register_t *addr, register_t *data)
  484. {
  485. switch (pm_cfg->type) {
  486. case PMIC_GPIO:
  487. plat_marvell_power_off_gpio(pm_cfg, addr, data);
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. /*****************************************************************************
  494. * A8K handler called when a power domain is about to be suspended. The
  495. * target_state encodes the power state that each level should transition to.
  496. *****************************************************************************
  497. */
  498. static void a8k_pwr_domain_suspend(const psci_power_state_t *target_state)
  499. {
  500. #if MSS_SUPPORT
  501. if (is_pm_fw_running()) {
  502. unsigned int idx;
  503. /* Prevent interrupts from spuriously waking up this cpu */
  504. gicv2_cpuif_disable();
  505. idx = plat_my_core_pos();
  506. /* pm system synchronization - used to synchronize multiple
  507. * core access to MSS
  508. */
  509. bakery_lock_get(&pm_sys_lock);
  510. /* send CPU Suspend IPC Message to MSS */
  511. mss_pm_ipc_msg_send(idx, PM_IPC_MSG_CPU_SUSPEND, target_state);
  512. /* trigger IPC message to MSS */
  513. mss_pm_ipc_msg_trigger();
  514. /* pm system synchronization */
  515. bakery_lock_release(&pm_sys_lock);
  516. /* trace message */
  517. PM_TRACE(TRACE_PWR_DOMAIN_SUSPEND);
  518. } else
  519. #endif
  520. {
  521. uintptr_t *mailbox = (void *)PLAT_MARVELL_MAILBOX_BASE;
  522. INFO("Suspending to RAM\n");
  523. marvell_console_runtime_end();
  524. /* Prevent interrupts from spuriously waking up this cpu */
  525. gicv2_cpuif_disable();
  526. mailbox[MBOX_IDX_SUSPEND_MAGIC] = MVEBU_MAILBOX_SUSPEND_STATE;
  527. mailbox[MBOX_IDX_ROM_EXIT_ADDR] = (uintptr_t)&plat_marvell_exit_bootrom;
  528. #if PLAT_MARVELL_SHARED_RAM_CACHED
  529. flush_dcache_range(PLAT_MARVELL_MAILBOX_BASE +
  530. MBOX_IDX_SUSPEND_MAGIC * sizeof(uintptr_t),
  531. 2 * sizeof(uintptr_t));
  532. #endif
  533. /* Flush and disable LLC before going off-power */
  534. llc_disable(0);
  535. isb();
  536. /*
  537. * Do not halt here!
  538. * The function must return for allowing the caller function
  539. * psci_power_up_finish() to do the proper context saving and
  540. * to release the CPU lock.
  541. */
  542. }
  543. }
  544. /*****************************************************************************
  545. * A8K handler called when a power domain has just been powered on after
  546. * being turned off earlier. The target_state encodes the low power state that
  547. * each level has woken up from.
  548. *****************************************************************************
  549. */
  550. static void a8k_pwr_domain_on_finish(const psci_power_state_t *target_state)
  551. {
  552. /* arch specific configuration */
  553. marvell_psci_arch_init(0);
  554. /* Interrupt initialization */
  555. gicv2_pcpu_distif_init();
  556. gicv2_cpuif_enable();
  557. if (is_pm_fw_running()) {
  558. /* trace message */
  559. PM_TRACE(TRACE_PWR_DOMAIN_ON_FINISH);
  560. }
  561. }
  562. /*****************************************************************************
  563. * A8K handler called when a power domain has just been powered on after
  564. * having been suspended earlier. The target_state encodes the low power state
  565. * that each level has woken up from.
  566. * TODO: At the moment we reuse the on finisher and reinitialize the secure
  567. * context. Need to implement a separate suspend finisher.
  568. *****************************************************************************
  569. */
  570. static void a8k_pwr_domain_suspend_finish(
  571. const psci_power_state_t *target_state)
  572. {
  573. if (is_pm_fw_running()) {
  574. /* arch specific configuration */
  575. marvell_psci_arch_init(0);
  576. /* Interrupt initialization */
  577. gicv2_cpuif_enable();
  578. /* trace message */
  579. PM_TRACE(TRACE_PWR_DOMAIN_SUSPEND_FINISH);
  580. } else {
  581. uintptr_t *mailbox = (void *)PLAT_MARVELL_MAILBOX_BASE;
  582. /* Only primary CPU requres platform init */
  583. if (!plat_my_core_pos()) {
  584. /* Initialize the console to provide
  585. * early debug support
  586. */
  587. marvell_console_runtime_init();
  588. bl31_plat_arch_setup();
  589. marvell_bl31_platform_setup();
  590. /*
  591. * Remove suspend to RAM marker from the mailbox
  592. * for treating a regular reset as a cold boot
  593. */
  594. mailbox[MBOX_IDX_SUSPEND_MAGIC] = 0;
  595. mailbox[MBOX_IDX_ROM_EXIT_ADDR] = 0;
  596. #if PLAT_MARVELL_SHARED_RAM_CACHED
  597. flush_dcache_range(PLAT_MARVELL_MAILBOX_BASE +
  598. MBOX_IDX_SUSPEND_MAGIC * sizeof(uintptr_t),
  599. 2 * sizeof(uintptr_t));
  600. #endif
  601. }
  602. }
  603. }
  604. /*****************************************************************************
  605. * This handler is called by the PSCI implementation during the `SYSTEM_SUSPEND`
  606. * call to get the `power_state` parameter. This allows the platform to encode
  607. * the appropriate State-ID field within the `power_state` parameter which can
  608. * be utilized in `pwr_domain_suspend()` to suspend to system affinity level.
  609. *****************************************************************************
  610. */
  611. static void a8k_get_sys_suspend_power_state(psci_power_state_t *req_state)
  612. {
  613. /* lower affinities use PLAT_MAX_OFF_STATE */
  614. for (int i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++)
  615. req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE;
  616. }
  617. static void
  618. __dead2 a8k_pwr_domain_pwr_down_wfi(const psci_power_state_t *target_state)
  619. {
  620. struct power_off_method *pm_cfg;
  621. unsigned int srcmd;
  622. unsigned int sdram_reg;
  623. register_t gpio_data = 0, gpio_addr = 0;
  624. if (is_pm_fw_running()) {
  625. psci_power_down_wfi();
  626. panic();
  627. }
  628. pm_cfg = (struct power_off_method *)plat_marvell_get_pm_cfg();
  629. /* Prepare for power off */
  630. plat_marvell_power_off_prepare(pm_cfg, &gpio_addr, &gpio_data);
  631. /* First step to enable DDR self-refresh
  632. * to keep the data during suspend
  633. */
  634. mmio_write_32(MVEBU_MC_PWR_CTRL_REG, 0x8C1);
  635. /* Save DDR self-refresh second step register
  636. * and value to be issued later
  637. */
  638. sdram_reg = MVEBU_USER_CMD_0_REG;
  639. srcmd = mmio_read_32(sdram_reg);
  640. srcmd &= ~(MVEBU_USER_CMD_CH0_MASK | MVEBU_USER_CMD_CS_MASK |
  641. MVEBU_USER_CMD_SR_MASK);
  642. srcmd |= (MVEBU_USER_CMD_CH0_EN | MVEBU_USER_CMD_CS_ALL |
  643. MVEBU_USER_CMD_SR_ENTER);
  644. /*
  645. * Wait for DRAM is done using registers access only.
  646. * At this stage any access to DRAM (procedure call) will
  647. * release it from the self-refresh mode
  648. */
  649. __asm__ volatile (
  650. /* Align to a cache line */
  651. " .balign 64\n\t"
  652. /* Enter self refresh */
  653. " str %[srcmd], [%[sdram_reg]]\n\t"
  654. /*
  655. * Wait 100 cycles for DDR to enter self refresh, by
  656. * doing 50 times two instructions.
  657. */
  658. " mov x1, #50\n\t"
  659. "1: subs x1, x1, #1\n\t"
  660. " bne 1b\n\t"
  661. /* Issue the command to trigger the SoC power off */
  662. " str %[gpio_data], [%[gpio_addr]]\n\t"
  663. /* Trap the processor */
  664. " b .\n\t"
  665. : : [srcmd] "r" (srcmd), [sdram_reg] "r" (sdram_reg),
  666. [gpio_addr] "r" (gpio_addr), [gpio_data] "r" (gpio_data)
  667. : "x1");
  668. panic();
  669. }
  670. /*****************************************************************************
  671. * A8K handlers to shutdown/reboot the system
  672. *****************************************************************************
  673. */
  674. /* Set a weak stub for platforms that don't configure system power off */
  675. #pragma weak system_power_off
  676. int system_power_off(void)
  677. {
  678. return 0;
  679. }
  680. static void __dead2 a8k_system_off(void)
  681. {
  682. /* Call the platform specific system power off function */
  683. system_power_off();
  684. /* board doesn't have a system off implementation */
  685. ERROR("%s: needs to be implemented\n", __func__);
  686. panic();
  687. }
  688. void plat_marvell_system_reset(void)
  689. {
  690. mmio_write_32(MVEBU_RFU_BASE + MVEBU_RFU_GLOBL_SW_RST, 0x0);
  691. }
  692. static void __dead2 a8k_system_reset(void)
  693. {
  694. plat_marvell_system_reset();
  695. /* we shouldn't get to this point */
  696. panic();
  697. }
  698. /*****************************************************************************
  699. * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
  700. * platform layer will take care of registering the handlers with PSCI.
  701. *****************************************************************************
  702. */
  703. const plat_psci_ops_t plat_arm_psci_pm_ops = {
  704. .cpu_standby = a8k_cpu_standby,
  705. .pwr_domain_on = a8k_pwr_domain_on,
  706. .pwr_domain_off = a8k_pwr_domain_off,
  707. .pwr_domain_suspend = a8k_pwr_domain_suspend,
  708. .pwr_domain_on_finish = a8k_pwr_domain_on_finish,
  709. .get_sys_suspend_power_state = a8k_get_sys_suspend_power_state,
  710. .pwr_domain_suspend_finish = a8k_pwr_domain_suspend_finish,
  711. .pwr_domain_pwr_down_wfi = a8k_pwr_domain_pwr_down_wfi,
  712. .system_off = a8k_system_off,
  713. .system_reset = a8k_system_reset,
  714. .validate_power_state = a8k_validate_power_state,
  715. .validate_ns_entrypoint = a8k_validate_ns_entrypoint
  716. };