Tony Xie | f6118cc | 2016-01-15 17:17:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Tony Xie | f6118cc | 2016-01-15 17:17:32 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
Antonio Nino Diaz | 5eb8837 | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 7 | #ifndef PMU_COM_H |
| 8 | #define PMU_COM_H |
Tony Xie | f6118cc | 2016-01-15 17:17:32 +0800 | [diff] [blame] | 9 | |
tony.xie | 54973e7 | 2017-04-24 16:18:10 +0800 | [diff] [blame] | 10 | #ifndef CHECK_CPU_WFIE_BASE |
| 11 | #define CHECK_CPU_WFIE_BASE (PMU_BASE + PMU_CORE_PWR_ST) |
| 12 | #endif |
Caesar Wang | 59e41b5 | 2016-04-10 14:11:07 +0800 | [diff] [blame] | 13 | /* |
| 14 | * Use this macro to instantiate lock before it is used in below |
| 15 | * rockchip_pd_lock_xxx() macros |
| 16 | */ |
Tony Xie | 42e113e | 2016-07-16 11:16:51 +0800 | [diff] [blame] | 17 | DECLARE_BAKERY_LOCK(rockchip_pd_lock); |
Tony Xie | f6118cc | 2016-01-15 17:17:32 +0800 | [diff] [blame] | 18 | |
Caesar Wang | 59e41b5 | 2016-04-10 14:11:07 +0800 | [diff] [blame] | 19 | /* |
| 20 | * These are wrapper macros to the powe domain Bakery Lock API. |
| 21 | */ |
| 22 | #define rockchip_pd_lock_init() bakery_lock_init(&rockchip_pd_lock) |
Tony Xie | f6118cc | 2016-01-15 17:17:32 +0800 | [diff] [blame] | 23 | #define rockchip_pd_lock_get() bakery_lock_get(&rockchip_pd_lock) |
Tony Xie | f6118cc | 2016-01-15 17:17:32 +0800 | [diff] [blame] | 24 | #define rockchip_pd_lock_rls() bakery_lock_release(&rockchip_pd_lock) |
| 25 | |
Tony Xie | f6118cc | 2016-01-15 17:17:32 +0800 | [diff] [blame] | 26 | /***************************************************************************** |
| 27 | * power domain on or off |
| 28 | *****************************************************************************/ |
| 29 | enum pmu_pd_state { |
| 30 | pmu_pd_on = 0, |
| 31 | pmu_pd_off = 1 |
| 32 | }; |
| 33 | |
| 34 | #pragma weak plat_ic_get_pending_interrupt_id |
| 35 | #pragma weak pmu_power_domain_ctr |
| 36 | #pragma weak check_cpu_wfie |
| 37 | |
| 38 | static inline uint32_t pmu_power_domain_st(uint32_t pd) |
| 39 | { |
| 40 | uint32_t pwrdn_st = mmio_read_32(PMU_BASE + PMU_PWRDN_ST) & BIT(pd); |
| 41 | |
| 42 | if (pwrdn_st) |
| 43 | return pmu_pd_off; |
| 44 | else |
| 45 | return pmu_pd_on; |
| 46 | } |
| 47 | |
| 48 | static int pmu_power_domain_ctr(uint32_t pd, uint32_t pd_state) |
| 49 | { |
| 50 | uint32_t val; |
| 51 | uint32_t loop = 0; |
| 52 | int ret = 0; |
| 53 | |
| 54 | rockchip_pd_lock_get(); |
| 55 | |
| 56 | val = mmio_read_32(PMU_BASE + PMU_PWRDN_CON); |
| 57 | if (pd_state == pmu_pd_off) |
| 58 | val |= BIT(pd); |
| 59 | else |
| 60 | val &= ~BIT(pd); |
| 61 | |
| 62 | mmio_write_32(PMU_BASE + PMU_PWRDN_CON, val); |
| 63 | dsb(); |
| 64 | |
| 65 | while ((pmu_power_domain_st(pd) != pd_state) && (loop < PD_CTR_LOOP)) { |
| 66 | udelay(1); |
| 67 | loop++; |
| 68 | } |
| 69 | |
| 70 | if (pmu_power_domain_st(pd) != pd_state) { |
| 71 | WARN("%s: %d, %d, error!\n", __func__, pd, pd_state); |
| 72 | ret = -EINVAL; |
| 73 | } |
| 74 | |
| 75 | rockchip_pd_lock_rls(); |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | static int check_cpu_wfie(uint32_t cpu_id, uint32_t wfie_msk) |
| 81 | { |
| 82 | uint32_t cluster_id, loop = 0; |
| 83 | |
| 84 | if (cpu_id >= PLATFORM_CLUSTER0_CORE_COUNT) { |
| 85 | cluster_id = 1; |
| 86 | cpu_id -= PLATFORM_CLUSTER0_CORE_COUNT; |
| 87 | } else { |
| 88 | cluster_id = 0; |
| 89 | } |
| 90 | |
Heiko Stuebner | 3b2d563 | 2019-03-07 08:01:37 +0100 | [diff] [blame] | 91 | /* |
| 92 | * wfe/wfi tracking not possible, hopefully the host |
| 93 | * was sucessful in enabling wfe/wfi. |
| 94 | * We'll give a bit of additional time, like the kernel does. |
| 95 | */ |
| 96 | if ((cluster_id && clstb_cpu_wfe < 0) || |
| 97 | (!cluster_id && clstl_cpu_wfe < 0)) { |
| 98 | mdelay(1); |
| 99 | return 0; |
| 100 | } |
| 101 | |
Tony Xie | f6118cc | 2016-01-15 17:17:32 +0800 | [diff] [blame] | 102 | if (cluster_id) |
| 103 | wfie_msk <<= (clstb_cpu_wfe + cpu_id); |
| 104 | else |
| 105 | wfie_msk <<= (clstl_cpu_wfe + cpu_id); |
| 106 | |
tony.xie | 54973e7 | 2017-04-24 16:18:10 +0800 | [diff] [blame] | 107 | while (!(mmio_read_32(CHECK_CPU_WFIE_BASE) & wfie_msk) && |
Tony Xie | f6118cc | 2016-01-15 17:17:32 +0800 | [diff] [blame] | 108 | (loop < CHK_CPU_LOOP)) { |
| 109 | udelay(1); |
| 110 | loop++; |
| 111 | } |
| 112 | |
tony.xie | 54973e7 | 2017-04-24 16:18:10 +0800 | [diff] [blame] | 113 | if ((mmio_read_32(CHECK_CPU_WFIE_BASE) & wfie_msk) == 0) { |
Tony Xie | f6118cc | 2016-01-15 17:17:32 +0800 | [diff] [blame] | 114 | WARN("%s: %d, %d, %d, error!\n", __func__, |
| 115 | cluster_id, cpu_id, wfie_msk); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Antonio Nino Diaz | 5eb8837 | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 122 | #endif /* PMU_COM_H */ |