2 * Keystone: PSC configuration module
4 * (C) Copyright 2012-2014
5 * Texas Instruments Incorporated, <www.ti.com>
7 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm-generic/errno.h>
13 #include <asm/processor.h>
14 #include <asm/arch/psc_defs.h>
23 * FUNCTION PURPOSE: Wait for end of transitional state
25 * DESCRIPTION: Polls pstat for the selected domain and waits for transitions
28 * Since this is boot loader code it is *ASSUMED* that interrupts
29 * are disabled and no other core is mucking around with the psc
32 * Returns 0 when the domain is free. Returns -1 if a timeout
33 * occurred waiting for the completion.
35 int psc_wait(u32 domain_num)
41 * Do nothing if the power domain is in transition. This should never
42 * happen since the boot code is the only software accesses psc.
43 * It's still remotely possible that the hardware state machines
44 * initiate transitions.
45 * Don't trap if the domain (or a module in this domain) is
46 * stuck in transition.
51 ptstat = __raw_readl(KS2_PSC_BASE + PSC_REG_PSTAT);
52 ptstat = ptstat & (1 << domain_num);
53 } while ((ptstat != 0) && ((retry += psc_delay()) <
54 PSC_PTSTAT_TIMEOUT_LIMIT));
56 if (retry >= PSC_PTSTAT_TIMEOUT_LIMIT)
62 u32 psc_get_domain_num(u32 mod_num)
66 /* Get the power domain associated with the module number */
67 domain_num = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num));
68 domain_num = PSC_REG_MDCFG_GET_PD(domain_num);
74 * FUNCTION PURPOSE: Power up/down a module
76 * DESCRIPTION: Powers up/down the requested module and the associated power
77 * domain if required. No action is taken it the module is
78 * already powered up/down.
80 * This only controls modules. The domain in which the module
81 * resides will be left in the power on state. Multiple modules
82 * can exist in a power domain, so powering down the domain based
83 * on a single module is not done.
85 * Returns 0 on success, -1 if the module can't be powered up, or
86 * if there is a timeout waiting for the transition.
88 int psc_set_state(u32 mod_num, u32 state)
98 * Get the power domain associated with the module number, and reset
99 * isolation functionality
101 v = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num));
102 domain_num = PSC_REG_MDCFG_GET_PD(v);
103 reset_iso = PSC_REG_MDCFG_GET_RESET_ISO(v);
105 /* Wait for the status of the domain/module to be non-transitional */
106 if (psc_wait(domain_num) != 0)
110 * Perform configuration even if the current status matches the
113 * Set the next state of the power domain to on. It's OK if the domain
114 * is always on. This code will not ever power down a domain, so no
115 * change is made if the new state is power down.
117 if (state == PSC_REG_VAL_MDCTL_NEXT_ON) {
118 pdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
119 pdctl = PSC_REG_PDCTL_SET_NEXT(pdctl,
120 PSC_REG_VAL_PDCTL_NEXT_ON);
121 __raw_writel(pdctl, KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
124 /* Set the next state for the module to enabled/disabled */
125 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
126 mdctl = PSC_REG_MDCTL_SET_NEXT(mdctl, state);
127 mdctl = PSC_REG_MDCTL_SET_RESET_ISO(mdctl, reset_iso);
128 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
130 /* Trigger the enable */
131 ptcmd = __raw_readl(KS2_PSC_BASE + PSC_REG_PTCMD);
132 ptcmd |= (u32)(1<<domain_num);
133 __raw_writel(ptcmd, KS2_PSC_BASE + PSC_REG_PTCMD);
135 /* Wait on the complete */
136 return psc_wait(domain_num);
140 * FUNCTION PURPOSE: Power up a module
142 * DESCRIPTION: Powers up the requested module and the associated power domain
143 * if required. No action is taken it the module is already
146 * Returns 0 on success, -1 if the module can't be powered up, or
147 * if there is a timeout waiting for the transition.
149 int psc_enable_module(u32 mod_num)
153 /* Set the bit to apply reset */
154 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
155 if ((mdctl & 0x3f) == PSC_REG_VAL_MDSTAT_STATE_ON)
158 return psc_set_state(mod_num, PSC_REG_VAL_MDCTL_NEXT_ON);
162 * FUNCTION PURPOSE: Power down a module
164 * DESCRIPTION: Powers down the requested module.
166 * Returns 0 on success, -1 on failure or timeout.
168 int psc_disable_module(u32 mod_num)
172 /* Set the bit to apply reset */
173 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
174 if ((mdctl & 0x3f) == 0)
176 mdctl = PSC_REG_MDCTL_SET_LRSTZ(mdctl, 0);
177 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
179 return psc_set_state(mod_num, PSC_REG_VAL_MDCTL_NEXT_SWRSTDISABLE);
183 * FUNCTION PURPOSE: Set the reset isolation bit in mdctl
185 * DESCRIPTION: The reset isolation enable bit is set. The state of the module
186 * is not changed. Returns 0 if the module config showed that
187 * reset isolation is supported. Returns 1 otherwise. This is not
188 * an error, but setting the bit in mdctl has no effect.
190 int psc_set_reset_iso(u32 mod_num)
195 /* Set the reset isolation bit */
196 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
197 mdctl = PSC_REG_MDCTL_SET_RESET_ISO(mdctl, 1);
198 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
200 v = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num));
201 if (PSC_REG_MDCFG_GET_RESET_ISO(v) == 1)
208 * FUNCTION PURPOSE: Disable a power domain
210 * DESCRIPTION: The power domain is disabled
212 int psc_disable_domain(u32 domain_num)
217 pdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
218 pdctl = PSC_REG_PDCTL_SET_NEXT(pdctl, PSC_REG_VAL_PDCTL_NEXT_OFF);
219 pdctl = PSC_REG_PDCTL_SET_PDMODE(pdctl, PSC_REG_VAL_PDCTL_PDMODE_SLEEP);
220 __raw_writel(pdctl, KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
222 ptcmd = __raw_readl(KS2_PSC_BASE + PSC_REG_PTCMD);
223 ptcmd |= (u32)(1 << domain_num);
224 __raw_writel(ptcmd, KS2_PSC_BASE + PSC_REG_PTCMD);
226 return psc_wait(domain_num);