1 // SPDX-License-Identifier: GPL-2.0+
3 * Keystone: PSC configuration module
5 * (C) Copyright 2012-2014
6 * Texas Instruments Incorporated, <www.ti.com>
10 #include <linux/errno.h>
12 #include <asm/processor.h>
13 #include <asm/arch/psc_defs.h>
16 * psc_delay() - delay for psc
27 * psc_wait() - Wait for end of transitional state
28 * @domain_num: GPSC domain number
30 * Polls pstat for the selected domain and waits for transitions to be complete.
31 * Since this is boot loader code it is *ASSUMED* that interrupts are disabled
32 * and no other core is mucking around with the psc at the same time.
34 * Return: 0 when the domain is free. Returns -1 if a timeout occurred waiting
37 int psc_wait(u32 domain_num)
43 * Do nothing if the power domain is in transition. This should never
44 * happen since the boot code is the only software accesses psc.
45 * It's still remotely possible that the hardware state machines
46 * initiate transitions.
47 * Don't trap if the domain (or a module in this domain) is
48 * stuck in transition.
53 ptstat = __raw_readl(KS2_PSC_BASE + PSC_REG_PSTAT);
54 ptstat = ptstat & (1 << domain_num);
55 } while ((ptstat != 0) && ((retry += psc_delay()) <
56 PSC_PTSTAT_TIMEOUT_LIMIT));
58 if (retry >= PSC_PTSTAT_TIMEOUT_LIMIT)
65 * psc_get_domain_num() - Get the domain number
66 * @mod_num: LPSC module number
68 u32 psc_get_domain_num(u32 mod_num)
72 /* Get the power domain associated with the module number */
73 domain_num = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num));
74 domain_num = PSC_REG_MDCFG_GET_PD(domain_num);
80 * psc_set_state() - powers up/down a module
81 * @mod_num: LPSC module number
82 * @state: 1 to enable, 0 to disable.
84 * Powers up/down the requested module and the associated power domain if
85 * required. No action is taken it the module is already powered up/down.
86 * This only controls modules. The domain in which the module resides will
87 * be left in the power on state. Multiple modules can exist in a power
88 * domain, so powering down the domain based on a single module is not done.
90 * Return: 0 on success, -1 if the module can't be powered up, or if there is a
91 * timeout waiting for the transition.
93 int psc_set_state(u32 mod_num, u32 state)
103 * Get the power domain associated with the module number, and reset
104 * isolation functionality
106 v = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num));
107 domain_num = PSC_REG_MDCFG_GET_PD(v);
108 reset_iso = PSC_REG_MDCFG_GET_RESET_ISO(v);
110 /* Wait for the status of the domain/module to be non-transitional */
111 if (psc_wait(domain_num) != 0)
115 * Perform configuration even if the current status matches the
118 * Set the next state of the power domain to on. It's OK if the domain
119 * is always on. This code will not ever power down a domain, so no
120 * change is made if the new state is power down.
122 if (state == PSC_REG_VAL_MDCTL_NEXT_ON) {
123 pdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
124 pdctl = PSC_REG_PDCTL_SET_NEXT(pdctl,
125 PSC_REG_VAL_PDCTL_NEXT_ON);
126 __raw_writel(pdctl, KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
129 /* Set the next state for the module to enabled/disabled */
130 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
131 mdctl = PSC_REG_MDCTL_SET_NEXT(mdctl, state);
132 mdctl = PSC_REG_MDCTL_SET_RESET_ISO(mdctl, reset_iso);
133 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
135 /* Trigger the enable */
136 ptcmd = __raw_readl(KS2_PSC_BASE + PSC_REG_PTCMD);
137 ptcmd |= (u32)(1<<domain_num);
138 __raw_writel(ptcmd, KS2_PSC_BASE + PSC_REG_PTCMD);
140 /* Wait on the complete */
141 return psc_wait(domain_num);
145 * psc_enable_module() - power up a module
146 * @mod_num: LPSC module number
148 * Powers up the requested module and the associated power domain
149 * if required. No action is taken it the module is already powered up.
151 * Return: 0 on success, -1 if the module can't be powered up, or
152 * if there is a timeout waiting for the transition.
155 int psc_enable_module(u32 mod_num)
159 /* Set the bit to apply reset */
160 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
161 if ((mdctl & 0x3f) == PSC_REG_VAL_MDSTAT_STATE_ON)
164 return psc_set_state(mod_num, PSC_REG_VAL_MDCTL_NEXT_ON);
168 * psc_disable_module() - Power down a module
169 * @mod_num: LPSC module number
171 * Return: 0 on success, -1 on failure or timeout.
173 int psc_disable_module(u32 mod_num)
177 /* Set the bit to apply reset */
178 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
179 if ((mdctl & 0x3f) == 0)
181 mdctl = PSC_REG_MDCTL_SET_LRSTZ(mdctl, 0);
182 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
184 return psc_set_state(mod_num, PSC_REG_VAL_MDCTL_NEXT_SWRSTDISABLE);
188 * psc_set_reset_iso() - Set the reset isolation bit in mdctl
189 * @mod_num: LPSC module number
191 * The reset isolation enable bit is set. The state of the module is not
194 * Return: 0 if the module config showed that reset isolation is supported.
195 * Returns 1 otherwise. This is not an error, but setting the bit in mdctl
198 int psc_set_reset_iso(u32 mod_num)
203 /* Set the reset isolation bit */
204 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
205 mdctl = PSC_REG_MDCTL_SET_RESET_ISO(mdctl, 1);
206 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
208 v = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num));
209 if (PSC_REG_MDCFG_GET_RESET_ISO(v) == 1)
216 * psc_disable_domain() - Disable a power domain
217 * @domain_num: GPSC domain number
219 int psc_disable_domain(u32 domain_num)
224 pdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
225 pdctl = PSC_REG_PDCTL_SET_NEXT(pdctl, PSC_REG_VAL_PDCTL_NEXT_OFF);
226 pdctl = PSC_REG_PDCTL_SET_PDMODE(pdctl, PSC_REG_VAL_PDCTL_PDMODE_SLEEP);
227 __raw_writel(pdctl, KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
229 ptcmd = __raw_readl(KS2_PSC_BASE + PSC_REG_PTCMD);
230 ptcmd |= (u32)(1 << domain_num);
231 __raw_writel(ptcmd, KS2_PSC_BASE + PSC_REG_PTCMD);
233 return psc_wait(domain_num);
237 * psc_module_keep_in_reset_enabled() - Keep module in enabled,in-reset state
238 * @mod_num: LPSC module number
239 * @gate_clocks: Can the clocks be gated on this module?
241 * Enable the module, but do not release the module from local reset. This is
242 * necessary for many processor systems on keystone SoCs to allow for system
243 * initialization from a master processor prior to releasing the processor
246 int psc_module_keep_in_reset_enabled(u32 mod_num, bool gate_clocks)
248 u32 mdctl, ptcmd, mdstat;
250 int domain_num = psc_get_domain_num(mod_num);
251 int timeout = 100000;
253 /* Wait for any previous transitions to complete */
254 psc_wait(domain_num);
255 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
256 /* Should be set 0 to assert Local reset */
257 if ((mdctl & PSC_REG_MDCTL_SET_LRSTZ(mdctl, 1))) {
258 mdctl = PSC_REG_MDCTL_SET_LRSTZ(mdctl, 0);
259 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
260 /* Wait for transition to take place */
261 psc_wait(domain_num);
264 /* Clear Module reset */
265 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
266 next_state = gate_clocks ? PSC_REG_VAL_MDCTL_NEXT_OFF :
267 PSC_REG_VAL_MDCTL_NEXT_ON;
268 mdctl = PSC_REG_MDCTL_SET_NEXT(mdctl, next_state);
269 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
270 /* Trigger PD transition */
271 ptcmd = __raw_readl(KS2_PSC_BASE + PSC_REG_PTCMD);
272 ptcmd |= (u32)(1 << domain_num);
273 __raw_writel(ptcmd, KS2_PSC_BASE + PSC_REG_PTCMD);
274 psc_wait(domain_num);
276 mdstat = __raw_readl(KS2_PSC_BASE + PSC_REG_MDSTAT(mod_num));
278 mdstat = __raw_readl(KS2_PSC_BASE + PSC_REG_MDSTAT(mod_num));
280 if (!(PSC_REG_MDSTAT_GET_STATUS(mdstat) & 0x30) &&
281 PSC_REG_MDSTAT_GET_MRSTDONE(mdstat) &&
282 PSC_REG_MDSTAT_GET_LRSTDONE(mdstat))
288 printf("%s: Timedout waiting for mdstat(0x%08x) to change\n",
296 * psc_module_release_from_reset() - Release the module from reset
297 * @mod_num: LPSC module number
299 * This is the follow through for the command 'psc_module_keep_in_reset_enabled'
300 * Allowing the module to be released from reset once all required inits are
301 * complete for the module. Typically, this allows the processor module to start
304 int psc_module_release_from_reset(u32 mod_num)
307 int domain_num = psc_get_domain_num(mod_num);
308 int timeout = 100000;
310 /* Wait for any previous transitions to complete */
311 psc_wait(domain_num);
312 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
313 /* Should be set to 1 to de-assert Local reset */
314 if ((mdctl & PSC_REG_MDCTL_SET_LRSTZ(mdctl, 0))) {
315 mdctl = PSC_REG_MDCTL_SET_LRSTZ(mdctl, 1);
316 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
317 /* Wait for transition to take place */
318 psc_wait(domain_num);
320 mdstat = __raw_readl(KS2_PSC_BASE + PSC_REG_MDSTAT(mod_num));
322 mdstat = __raw_readl(KS2_PSC_BASE + PSC_REG_MDSTAT(mod_num));
324 if (!(PSC_REG_MDSTAT_GET_STATUS(mdstat) & 0x30) &&
325 PSC_REG_MDSTAT_GET_MRSTDONE(mdstat) &&
326 PSC_REG_MDSTAT_GET_LRSTDONE(mdstat))
332 printf("%s: Timedout waiting for mdstat(0x%08x) to change\n",