2 * RCPM(Run Control/Power Management) support
4 * Copyright 2012-2015 Freescale Semiconductor Inc.
6 * Author: Chenhui Zhao <chenhui.zhao@freescale.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #define pr_fmt(fmt) "%s: " fmt, __func__
16 #include <linux/types.h>
17 #include <linux/errno.h>
18 #include <linux/of_address.h>
19 #include <linux/export.h>
22 #include <linux/fsl/guts.h>
23 #include <asm/cputhreads.h>
24 #include <asm/fsl_pm.h>
27 static struct ccsr_rcpm_v1 __iomem
*rcpm_v1_regs
;
28 static struct ccsr_rcpm_v2 __iomem
*rcpm_v2_regs
;
29 static unsigned int fsl_supported_pm_modes
;
31 static void rcpm_v1_irq_mask(int cpu
)
33 int hw_cpu
= get_hard_smp_processor_id(cpu
);
34 unsigned int mask
= 1 << hw_cpu
;
36 setbits32(&rcpm_v1_regs
->cpmimr
, mask
);
37 setbits32(&rcpm_v1_regs
->cpmcimr
, mask
);
38 setbits32(&rcpm_v1_regs
->cpmmcmr
, mask
);
39 setbits32(&rcpm_v1_regs
->cpmnmimr
, mask
);
42 static void rcpm_v2_irq_mask(int cpu
)
44 int hw_cpu
= get_hard_smp_processor_id(cpu
);
45 unsigned int mask
= 1 << hw_cpu
;
47 setbits32(&rcpm_v2_regs
->tpmimr0
, mask
);
48 setbits32(&rcpm_v2_regs
->tpmcimr0
, mask
);
49 setbits32(&rcpm_v2_regs
->tpmmcmr0
, mask
);
50 setbits32(&rcpm_v2_regs
->tpmnmimr0
, mask
);
53 static void rcpm_v1_irq_unmask(int cpu
)
55 int hw_cpu
= get_hard_smp_processor_id(cpu
);
56 unsigned int mask
= 1 << hw_cpu
;
58 clrbits32(&rcpm_v1_regs
->cpmimr
, mask
);
59 clrbits32(&rcpm_v1_regs
->cpmcimr
, mask
);
60 clrbits32(&rcpm_v1_regs
->cpmmcmr
, mask
);
61 clrbits32(&rcpm_v1_regs
->cpmnmimr
, mask
);
64 static void rcpm_v2_irq_unmask(int cpu
)
66 int hw_cpu
= get_hard_smp_processor_id(cpu
);
67 unsigned int mask
= 1 << hw_cpu
;
69 clrbits32(&rcpm_v2_regs
->tpmimr0
, mask
);
70 clrbits32(&rcpm_v2_regs
->tpmcimr0
, mask
);
71 clrbits32(&rcpm_v2_regs
->tpmmcmr0
, mask
);
72 clrbits32(&rcpm_v2_regs
->tpmnmimr0
, mask
);
75 static void rcpm_v1_set_ip_power(bool enable
, u32 mask
)
78 setbits32(&rcpm_v1_regs
->ippdexpcr
, mask
);
80 clrbits32(&rcpm_v1_regs
->ippdexpcr
, mask
);
83 static void rcpm_v2_set_ip_power(bool enable
, u32 mask
)
86 setbits32(&rcpm_v2_regs
->ippdexpcr
[0], mask
);
88 clrbits32(&rcpm_v2_regs
->ippdexpcr
[0], mask
);
91 static void rcpm_v1_cpu_enter_state(int cpu
, int state
)
93 int hw_cpu
= get_hard_smp_processor_id(cpu
);
94 unsigned int mask
= 1 << hw_cpu
;
98 setbits32(&rcpm_v1_regs
->cdozcr
, mask
);
101 setbits32(&rcpm_v1_regs
->cnapcr
, mask
);
104 pr_warn("Unknown cpu PM state (%d)\n", state
);
109 static void rcpm_v2_cpu_enter_state(int cpu
, int state
)
111 int hw_cpu
= get_hard_smp_processor_id(cpu
);
112 u32 mask
= 1 << cpu_core_index_of_thread(cpu
);
116 /* one bit corresponds to one thread for PH10 of 6500 */
117 setbits32(&rcpm_v2_regs
->tph10setr0
, 1 << hw_cpu
);
120 setbits32(&rcpm_v2_regs
->pcph15setr
, mask
);
123 setbits32(&rcpm_v2_regs
->pcph20setr
, mask
);
126 setbits32(&rcpm_v2_regs
->pcph30setr
, mask
);
129 pr_warn("Unknown cpu PM state (%d)\n", state
);
133 static void rcpm_v1_cpu_die(int cpu
)
135 rcpm_v1_cpu_enter_state(cpu
, E500_PM_PH15
);
139 static void qoriq_disable_thread(int cpu
)
141 int thread
= cpu_thread_in_core(cpu
);
143 book3e_stop_thread(thread
);
147 static void rcpm_v2_cpu_die(int cpu
)
152 if (threads_per_core
== 2) {
153 primary
= cpu_first_thread_sibling(cpu
);
154 if (cpu_is_offline(primary
) && cpu_is_offline(primary
+ 1)) {
155 /* if both threads are offline, put the cpu in PH20 */
156 rcpm_v2_cpu_enter_state(cpu
, E500_PM_PH20
);
158 /* if only one thread is offline, disable the thread */
159 qoriq_disable_thread(cpu
);
164 if (threads_per_core
== 1)
165 rcpm_v2_cpu_enter_state(cpu
, E500_PM_PH20
);
168 static void rcpm_v1_cpu_exit_state(int cpu
, int state
)
170 int hw_cpu
= get_hard_smp_processor_id(cpu
);
171 unsigned int mask
= 1 << hw_cpu
;
175 clrbits32(&rcpm_v1_regs
->cdozcr
, mask
);
178 clrbits32(&rcpm_v1_regs
->cnapcr
, mask
);
181 pr_warn("Unknown cpu PM state (%d)\n", state
);
186 static void rcpm_v1_cpu_up_prepare(int cpu
)
188 rcpm_v1_cpu_exit_state(cpu
, E500_PM_PH15
);
189 rcpm_v1_irq_unmask(cpu
);
192 static void rcpm_v2_cpu_exit_state(int cpu
, int state
)
194 int hw_cpu
= get_hard_smp_processor_id(cpu
);
195 u32 mask
= 1 << cpu_core_index_of_thread(cpu
);
199 setbits32(&rcpm_v2_regs
->tph10clrr0
, 1 << hw_cpu
);
202 setbits32(&rcpm_v2_regs
->pcph15clrr
, mask
);
205 setbits32(&rcpm_v2_regs
->pcph20clrr
, mask
);
208 setbits32(&rcpm_v2_regs
->pcph30clrr
, mask
);
211 pr_warn("Unknown cpu PM state (%d)\n", state
);
215 static void rcpm_v2_cpu_up_prepare(int cpu
)
217 rcpm_v2_cpu_exit_state(cpu
, E500_PM_PH20
);
218 rcpm_v2_irq_unmask(cpu
);
221 static int rcpm_v1_plat_enter_state(int state
)
223 u32
*pmcsr_reg
= &rcpm_v1_regs
->powmgtcsr
;
229 setbits32(pmcsr_reg
, RCPM_POWMGTCSR_SLP
);
231 /* Upon resume, wait for RCPM_POWMGTCSR_SLP bit to be clear. */
232 result
= spin_event_timeout(
233 !(in_be32(pmcsr_reg
) & RCPM_POWMGTCSR_SLP
), 10000, 10);
235 pr_err("timeout waiting for SLP bit to be cleared\n");
240 pr_warn("Unknown platform PM state (%d)", state
);
247 static int rcpm_v2_plat_enter_state(int state
)
249 u32
*pmcsr_reg
= &rcpm_v2_regs
->powmgtcsr
;
255 /* clear previous LPM20 status */
256 setbits32(pmcsr_reg
, RCPM_POWMGTCSR_P_LPM20_ST
);
257 /* enter LPM20 status */
258 setbits32(pmcsr_reg
, RCPM_POWMGTCSR_LPM20_RQ
);
260 /* At this point, the device is in LPM20 status. */
263 result
= spin_event_timeout(
264 !(in_be32(pmcsr_reg
) & RCPM_POWMGTCSR_LPM20_ST
), 10000, 10);
266 pr_err("timeout waiting for LPM20 bit to be cleared\n");
271 pr_warn("Unknown platform PM state (%d)\n", state
);
278 static int rcpm_v1_plat_enter_sleep(void)
280 return rcpm_v1_plat_enter_state(PLAT_PM_SLEEP
);
283 static int rcpm_v2_plat_enter_sleep(void)
285 return rcpm_v2_plat_enter_state(PLAT_PM_LPM20
);
288 static void rcpm_common_freeze_time_base(u32
*tben_reg
, int freeze
)
293 mask
= in_be32(tben_reg
);
294 clrbits32(tben_reg
, mask
);
296 setbits32(tben_reg
, mask
);
299 /* read back to push the previous write */
303 static void rcpm_v1_freeze_time_base(bool freeze
)
305 rcpm_common_freeze_time_base(&rcpm_v1_regs
->ctbenr
, freeze
);
308 static void rcpm_v2_freeze_time_base(bool freeze
)
310 rcpm_common_freeze_time_base(&rcpm_v2_regs
->pctbenr
, freeze
);
313 static unsigned int rcpm_get_pm_modes(void)
315 return fsl_supported_pm_modes
;
318 static const struct fsl_pm_ops qoriq_rcpm_v1_ops
= {
319 .irq_mask
= rcpm_v1_irq_mask
,
320 .irq_unmask
= rcpm_v1_irq_unmask
,
321 .cpu_enter_state
= rcpm_v1_cpu_enter_state
,
322 .cpu_exit_state
= rcpm_v1_cpu_exit_state
,
323 .cpu_up_prepare
= rcpm_v1_cpu_up_prepare
,
324 .cpu_die
= rcpm_v1_cpu_die
,
325 .plat_enter_sleep
= rcpm_v1_plat_enter_sleep
,
326 .set_ip_power
= rcpm_v1_set_ip_power
,
327 .freeze_time_base
= rcpm_v1_freeze_time_base
,
328 .get_pm_modes
= rcpm_get_pm_modes
,
331 static const struct fsl_pm_ops qoriq_rcpm_v2_ops
= {
332 .irq_mask
= rcpm_v2_irq_mask
,
333 .irq_unmask
= rcpm_v2_irq_unmask
,
334 .cpu_enter_state
= rcpm_v2_cpu_enter_state
,
335 .cpu_exit_state
= rcpm_v2_cpu_exit_state
,
336 .cpu_up_prepare
= rcpm_v2_cpu_up_prepare
,
337 .cpu_die
= rcpm_v2_cpu_die
,
338 .plat_enter_sleep
= rcpm_v2_plat_enter_sleep
,
339 .set_ip_power
= rcpm_v2_set_ip_power
,
340 .freeze_time_base
= rcpm_v2_freeze_time_base
,
341 .get_pm_modes
= rcpm_get_pm_modes
,
344 static const struct of_device_id rcpm_matches
[] = {
346 .compatible
= "fsl,qoriq-rcpm-1.0",
347 .data
= &qoriq_rcpm_v1_ops
,
350 .compatible
= "fsl,qoriq-rcpm-2.0",
351 .data
= &qoriq_rcpm_v2_ops
,
354 .compatible
= "fsl,qoriq-rcpm-2.1",
355 .data
= &qoriq_rcpm_v2_ops
,
360 int __init
fsl_rcpm_init(void)
362 struct device_node
*np
;
363 const struct of_device_id
*match
;
366 np
= of_find_matching_node_and_match(NULL
, rcpm_matches
, &match
);
370 base
= of_iomap(np
, 0);
373 pr_err("of_iomap() error.\n");
380 /* support sleep by default */
381 fsl_supported_pm_modes
= FSL_PM_SLEEP
;
383 qoriq_pm_ops
= match
->data
;