Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Keystone: PSC configuration module |
| 3 | * |
| 4 | * (C) Copyright 2012-2014 |
| 5 | * Texas Instruments Incorporated, <www.ti.com> |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <asm-generic/errno.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <asm/processor.h> |
| 14 | #include <asm/arch/psc_defs.h> |
| 15 | |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 16 | /** |
| 17 | * psc_delay() - delay for psc |
| 18 | * |
| 19 | * Return: 10 |
| 20 | */ |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 21 | int psc_delay(void) |
| 22 | { |
| 23 | udelay(10); |
| 24 | return 10; |
| 25 | } |
| 26 | |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 27 | /** |
| 28 | * psc_wait() - Wait for end of transitional state |
| 29 | * @domain_num: GPSC domain number |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 30 | * |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 31 | * Polls pstat for the selected domain and waits for transitions to be complete. |
| 32 | * Since this is boot loader code it is *ASSUMED* that interrupts are disabled |
| 33 | * and no other core is mucking around with the psc at the same time. |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 34 | * |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 35 | * Return: 0 when the domain is free. Returns -1 if a timeout occurred waiting |
| 36 | * for the completion. |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 37 | */ |
| 38 | int psc_wait(u32 domain_num) |
| 39 | { |
| 40 | u32 retry; |
| 41 | u32 ptstat; |
| 42 | |
| 43 | /* |
| 44 | * Do nothing if the power domain is in transition. This should never |
| 45 | * happen since the boot code is the only software accesses psc. |
| 46 | * It's still remotely possible that the hardware state machines |
| 47 | * initiate transitions. |
| 48 | * Don't trap if the domain (or a module in this domain) is |
| 49 | * stuck in transition. |
| 50 | */ |
| 51 | retry = 0; |
| 52 | |
| 53 | do { |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 54 | ptstat = __raw_readl(KS2_PSC_BASE + PSC_REG_PSTAT); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 55 | ptstat = ptstat & (1 << domain_num); |
| 56 | } while ((ptstat != 0) && ((retry += psc_delay()) < |
| 57 | PSC_PTSTAT_TIMEOUT_LIMIT)); |
| 58 | |
| 59 | if (retry >= PSC_PTSTAT_TIMEOUT_LIMIT) |
| 60 | return -1; |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 65 | /** |
| 66 | * psc_get_domain_num() - Get the domain number |
| 67 | * @mod_num: LPSC module number |
| 68 | */ |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 69 | u32 psc_get_domain_num(u32 mod_num) |
| 70 | { |
| 71 | u32 domain_num; |
| 72 | |
| 73 | /* Get the power domain associated with the module number */ |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 74 | domain_num = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 75 | domain_num = PSC_REG_MDCFG_GET_PD(domain_num); |
| 76 | |
| 77 | return domain_num; |
| 78 | } |
| 79 | |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 80 | /** |
| 81 | * psc_set_state() - powers up/down a module |
| 82 | * @mod_num: LPSC module number |
| 83 | * @state: 1 to enable, 0 to disable. |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 84 | * |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 85 | * Powers up/down the requested module and the associated power domain if |
| 86 | * required. No action is taken it the module is already powered up/down. |
| 87 | * This only controls modules. The domain in which the module resides will |
| 88 | * be left in the power on state. Multiple modules can exist in a power |
| 89 | * domain, so powering down the domain based on a single module is not done. |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 90 | * |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 91 | * Return: 0 on success, -1 if the module can't be powered up, or if there is a |
| 92 | * timeout waiting for the transition. |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 93 | */ |
| 94 | int psc_set_state(u32 mod_num, u32 state) |
| 95 | { |
| 96 | u32 domain_num; |
| 97 | u32 pdctl; |
| 98 | u32 mdctl; |
| 99 | u32 ptcmd; |
| 100 | u32 reset_iso; |
| 101 | u32 v; |
| 102 | |
| 103 | /* |
| 104 | * Get the power domain associated with the module number, and reset |
| 105 | * isolation functionality |
| 106 | */ |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 107 | v = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 108 | domain_num = PSC_REG_MDCFG_GET_PD(v); |
| 109 | reset_iso = PSC_REG_MDCFG_GET_RESET_ISO(v); |
| 110 | |
| 111 | /* Wait for the status of the domain/module to be non-transitional */ |
| 112 | if (psc_wait(domain_num) != 0) |
| 113 | return -1; |
| 114 | |
| 115 | /* |
| 116 | * Perform configuration even if the current status matches the |
| 117 | * existing state |
| 118 | * |
| 119 | * Set the next state of the power domain to on. It's OK if the domain |
| 120 | * is always on. This code will not ever power down a domain, so no |
| 121 | * change is made if the new state is power down. |
| 122 | */ |
| 123 | if (state == PSC_REG_VAL_MDCTL_NEXT_ON) { |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 124 | pdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_PDCTL(domain_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 125 | pdctl = PSC_REG_PDCTL_SET_NEXT(pdctl, |
| 126 | PSC_REG_VAL_PDCTL_NEXT_ON); |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 127 | __raw_writel(pdctl, KS2_PSC_BASE + PSC_REG_PDCTL(domain_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* Set the next state for the module to enabled/disabled */ |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 131 | mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 132 | mdctl = PSC_REG_MDCTL_SET_NEXT(mdctl, state); |
| 133 | mdctl = PSC_REG_MDCTL_SET_RESET_ISO(mdctl, reset_iso); |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 134 | __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 135 | |
| 136 | /* Trigger the enable */ |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 137 | ptcmd = __raw_readl(KS2_PSC_BASE + PSC_REG_PTCMD); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 138 | ptcmd |= (u32)(1<<domain_num); |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 139 | __raw_writel(ptcmd, KS2_PSC_BASE + PSC_REG_PTCMD); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 140 | |
| 141 | /* Wait on the complete */ |
| 142 | return psc_wait(domain_num); |
| 143 | } |
| 144 | |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 145 | /** |
| 146 | * psc_enable_module() - power up a module |
| 147 | * @mod_num: LPSC module number |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 148 | * |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 149 | * Powers up the requested module and the associated power domain |
| 150 | * if required. No action is taken it the module is already powered up. |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 151 | * |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 152 | * Return: 0 on success, -1 if the module can't be powered up, or |
| 153 | * if there is a timeout waiting for the transition. |
| 154 | * |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 155 | */ |
| 156 | int psc_enable_module(u32 mod_num) |
| 157 | { |
| 158 | u32 mdctl; |
| 159 | |
| 160 | /* Set the bit to apply reset */ |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 161 | mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 162 | if ((mdctl & 0x3f) == PSC_REG_VAL_MDSTAT_STATE_ON) |
| 163 | return 0; |
| 164 | |
| 165 | return psc_set_state(mod_num, PSC_REG_VAL_MDCTL_NEXT_ON); |
| 166 | } |
| 167 | |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 168 | /** |
| 169 | * psc_disable_module() - Power down a module |
| 170 | * @mod_num: LPSC module number |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 171 | * |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 172 | * Return: 0 on success, -1 on failure or timeout. |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 173 | */ |
| 174 | int psc_disable_module(u32 mod_num) |
| 175 | { |
| 176 | u32 mdctl; |
| 177 | |
| 178 | /* Set the bit to apply reset */ |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 179 | mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 180 | if ((mdctl & 0x3f) == 0) |
| 181 | return 0; |
| 182 | mdctl = PSC_REG_MDCTL_SET_LRSTZ(mdctl, 0); |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 183 | __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 184 | |
| 185 | return psc_set_state(mod_num, PSC_REG_VAL_MDCTL_NEXT_SWRSTDISABLE); |
| 186 | } |
| 187 | |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 188 | /** |
| 189 | * psc_set_reset_iso() - Set the reset isolation bit in mdctl |
| 190 | * @mod_num: LPSC module number |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 191 | * |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 192 | * The reset isolation enable bit is set. The state of the module is not |
| 193 | * changed. |
| 194 | * |
| 195 | * Return: 0 if the module config showed that reset isolation is supported. |
| 196 | * Returns 1 otherwise. This is not an error, but setting the bit in mdctl |
| 197 | * has no effect. |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 198 | */ |
| 199 | int psc_set_reset_iso(u32 mod_num) |
| 200 | { |
| 201 | u32 v; |
| 202 | u32 mdctl; |
| 203 | |
| 204 | /* Set the reset isolation bit */ |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 205 | mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 206 | mdctl = PSC_REG_MDCTL_SET_RESET_ISO(mdctl, 1); |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 207 | __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 208 | |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 209 | v = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 210 | if (PSC_REG_MDCFG_GET_RESET_ISO(v) == 1) |
| 211 | return 0; |
| 212 | |
| 213 | return 1; |
| 214 | } |
| 215 | |
Nishanth Menon | b3ec5de | 2016-02-25 12:53:42 -0600 | [diff] [blame^] | 216 | /** |
| 217 | * psc_disable_domain() - Disable a power domain |
| 218 | * @domain_num: GPSC domain number |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 219 | */ |
| 220 | int psc_disable_domain(u32 domain_num) |
| 221 | { |
| 222 | u32 pdctl; |
| 223 | u32 ptcmd; |
| 224 | |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 225 | pdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_PDCTL(domain_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 226 | pdctl = PSC_REG_PDCTL_SET_NEXT(pdctl, PSC_REG_VAL_PDCTL_NEXT_OFF); |
| 227 | pdctl = PSC_REG_PDCTL_SET_PDMODE(pdctl, PSC_REG_VAL_PDCTL_PDMODE_SLEEP); |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 228 | __raw_writel(pdctl, KS2_PSC_BASE + PSC_REG_PDCTL(domain_num)); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 229 | |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 230 | ptcmd = __raw_readl(KS2_PSC_BASE + PSC_REG_PTCMD); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 231 | ptcmd |= (u32)(1 << domain_num); |
Khoronzhuk, Ivan | 49d39a2 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 232 | __raw_writel(ptcmd, KS2_PSC_BASE + PSC_REG_PTCMD); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 233 | |
| 234 | return psc_wait(domain_num); |
| 235 | } |