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>
17 * psc_delay() - delay for psc
28 * psc_wait() - Wait for end of transitional state
29 * @domain_num: GPSC domain number
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.
35 * Return: 0 when the domain is free. Returns -1 if a timeout occurred waiting
38 int psc_wait(u32 domain_num)
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.
54 ptstat = __raw_readl(KS2_PSC_BASE + PSC_REG_PSTAT);
55 ptstat = ptstat & (1 << domain_num);
56 } while ((ptstat != 0) && ((retry += psc_delay()) <
57 PSC_PTSTAT_TIMEOUT_LIMIT));
59 if (retry >= PSC_PTSTAT_TIMEOUT_LIMIT)
66 * psc_get_domain_num() - Get the domain number
67 * @mod_num: LPSC module number
69 u32 psc_get_domain_num(u32 mod_num)
73 /* Get the power domain associated with the module number */
74 domain_num = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num));
75 domain_num = PSC_REG_MDCFG_GET_PD(domain_num);
81 * psc_set_state() - powers up/down a module
82 * @mod_num: LPSC module number
83 * @state: 1 to enable, 0 to disable.
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.
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.
94 int psc_set_state(u32 mod_num, u32 state)
104 * Get the power domain associated with the module number, and reset
105 * isolation functionality
107 v = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num));
108 domain_num = PSC_REG_MDCFG_GET_PD(v);
109 reset_iso = PSC_REG_MDCFG_GET_RESET_ISO(v);
111 /* Wait for the status of the domain/module to be non-transitional */
112 if (psc_wait(domain_num) != 0)
116 * Perform configuration even if the current status matches the
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.
123 if (state == PSC_REG_VAL_MDCTL_NEXT_ON) {
124 pdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
125 pdctl = PSC_REG_PDCTL_SET_NEXT(pdctl,
126 PSC_REG_VAL_PDCTL_NEXT_ON);
127 __raw_writel(pdctl, KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
130 /* Set the next state for the module to enabled/disabled */
131 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
132 mdctl = PSC_REG_MDCTL_SET_NEXT(mdctl, state);
133 mdctl = PSC_REG_MDCTL_SET_RESET_ISO(mdctl, reset_iso);
134 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
136 /* Trigger the enable */
137 ptcmd = __raw_readl(KS2_PSC_BASE + PSC_REG_PTCMD);
138 ptcmd |= (u32)(1<<domain_num);
139 __raw_writel(ptcmd, KS2_PSC_BASE + PSC_REG_PTCMD);
141 /* Wait on the complete */
142 return psc_wait(domain_num);
146 * psc_enable_module() - power up a module
147 * @mod_num: LPSC module number
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.
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.
156 int psc_enable_module(u32 mod_num)
160 /* Set the bit to apply reset */
161 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
162 if ((mdctl & 0x3f) == PSC_REG_VAL_MDSTAT_STATE_ON)
165 return psc_set_state(mod_num, PSC_REG_VAL_MDCTL_NEXT_ON);
169 * psc_disable_module() - Power down a module
170 * @mod_num: LPSC module number
172 * Return: 0 on success, -1 on failure or timeout.
174 int psc_disable_module(u32 mod_num)
178 /* Set the bit to apply reset */
179 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
180 if ((mdctl & 0x3f) == 0)
182 mdctl = PSC_REG_MDCTL_SET_LRSTZ(mdctl, 0);
183 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
185 return psc_set_state(mod_num, PSC_REG_VAL_MDCTL_NEXT_SWRSTDISABLE);
189 * psc_set_reset_iso() - Set the reset isolation bit in mdctl
190 * @mod_num: LPSC module number
192 * The reset isolation enable bit is set. The state of the module is not
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
199 int psc_set_reset_iso(u32 mod_num)
204 /* Set the reset isolation bit */
205 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
206 mdctl = PSC_REG_MDCTL_SET_RESET_ISO(mdctl, 1);
207 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
209 v = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num));
210 if (PSC_REG_MDCFG_GET_RESET_ISO(v) == 1)
217 * psc_disable_domain() - Disable a power domain
218 * @domain_num: GPSC domain number
220 int psc_disable_domain(u32 domain_num)
225 pdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
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);
228 __raw_writel(pdctl, KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
230 ptcmd = __raw_readl(KS2_PSC_BASE + PSC_REG_PTCMD);
231 ptcmd |= (u32)(1 << domain_num);
232 __raw_writel(ptcmd, KS2_PSC_BASE + PSC_REG_PTCMD);
234 return psc_wait(domain_num);
238 * psc_module_keep_in_reset_enabled() - Keep module in enabled,in-reset state
239 * @mod_num: LPSC module number
240 * @gate_clocks: Can the clocks be gated on this module?
242 * Enable the module, but do not release the module from local reset. This is
243 * necessary for many processor systems on keystone SoCs to allow for system
244 * initialization from a master processor prior to releasing the processor
247 int psc_module_keep_in_reset_enabled(u32 mod_num, bool gate_clocks)
249 u32 mdctl, ptcmd, mdstat;
251 int domain_num = psc_get_domain_num(mod_num);
252 int timeout = 100000;
254 /* Wait for any previous transitions to complete */
255 psc_wait(domain_num);
256 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
257 /* Should be set 0 to assert Local reset */
258 if ((mdctl & PSC_REG_MDCTL_SET_LRSTZ(mdctl, 1))) {
259 mdctl = PSC_REG_MDCTL_SET_LRSTZ(mdctl, 0);
260 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
261 /* Wait for transition to take place */
262 psc_wait(domain_num);
265 /* Clear Module reset */
266 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
267 next_state = gate_clocks ? PSC_REG_VAL_MDCTL_NEXT_OFF :
268 PSC_REG_VAL_MDCTL_NEXT_ON;
269 mdctl = PSC_REG_MDCTL_SET_NEXT(mdctl, next_state);
270 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
271 /* Trigger PD transition */
272 ptcmd = __raw_readl(KS2_PSC_BASE + PSC_REG_PTCMD);
273 ptcmd |= (u32)(1 << domain_num);
274 __raw_writel(ptcmd, KS2_PSC_BASE + PSC_REG_PTCMD);
275 psc_wait(domain_num);
277 mdstat = __raw_readl(KS2_PSC_BASE + PSC_REG_MDSTAT(mod_num));
279 mdstat = __raw_readl(KS2_PSC_BASE + PSC_REG_MDSTAT(mod_num));
281 if (!(PSC_REG_MDSTAT_GET_STATUS(mdstat) & 0x30) &&
282 PSC_REG_MDSTAT_GET_MRSTDONE(mdstat) &&
283 PSC_REG_MDSTAT_GET_LRSTDONE(mdstat))
289 printf("%s: Timedout waiting for mdstat(0x%08x) to change\n",
297 * psc_module_release_from_reset() - Release the module from reset
298 * @mod_num: LPSC module number
300 * This is the follow through for the command 'psc_module_keep_in_reset_enabled'
301 * Allowing the module to be released from reset once all required inits are
302 * complete for the module. Typically, this allows the processor module to start
305 int psc_module_release_from_reset(u32 mod_num)
308 int domain_num = psc_get_domain_num(mod_num);
309 int timeout = 100000;
311 /* Wait for any previous transitions to complete */
312 psc_wait(domain_num);
313 mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
314 /* Should be set to 1 to de-assert Local reset */
315 if ((mdctl & PSC_REG_MDCTL_SET_LRSTZ(mdctl, 0))) {
316 mdctl = PSC_REG_MDCTL_SET_LRSTZ(mdctl, 1);
317 __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
318 /* Wait for transition to take place */
319 psc_wait(domain_num);
321 mdstat = __raw_readl(KS2_PSC_BASE + PSC_REG_MDSTAT(mod_num));
323 mdstat = __raw_readl(KS2_PSC_BASE + PSC_REG_MDSTAT(mod_num));
325 if (!(PSC_REG_MDSTAT_GET_STATUS(mdstat) & 0x30) &&
326 PSC_REG_MDSTAT_GET_MRSTDONE(mdstat) &&
327 PSC_REG_MDSTAT_GET_LRSTDONE(mdstat))
333 printf("%s: Timedout waiting for mdstat(0x%08x) to change\n",