1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012 Linaro : Daniel Lezcano <daniel.lezcano@linaro.org> (IBM)
5 * Based on the work of Rickard Andersson <rickard.andersson@stericsson.com>
6 * and Jonas Aaberg <jonas.aberg@stericsson.com>.
9 #include <linux/init.h>
10 #include <linux/cpuidle.h>
11 #include <linux/spinlock.h>
12 #include <linux/atomic.h>
13 #include <linux/smp.h>
14 #include <linux/mfd/dbx500-prcmu.h>
15 #include <linux/platform_data/arm-ux500-pm.h>
16 #include <linux/platform_device.h>
18 #include <asm/cpuidle.h>
20 static atomic_t master = ATOMIC_INIT(0);
21 static DEFINE_SPINLOCK(master_lock);
23 static inline int ux500_enter_idle(struct cpuidle_device *dev,
24 struct cpuidle_driver *drv, int index)
26 int this_cpu = smp_processor_id();
27 bool recouple = false;
29 if (atomic_inc_return(&master) == num_online_cpus()) {
31 /* With this lock, we prevent the other cpu to exit and enter
32 * this function again and become the master */
33 if (!spin_trylock(&master_lock))
36 /* decouple the gic from the A9 cores */
37 if (prcmu_gic_decouple()) {
38 spin_unlock(&master_lock);
42 /* If an error occur, we will have to recouple the gic
46 /* At this state, as the gic is decoupled, if the other
47 * cpu is in WFI, we have the guarantee it won't be wake
48 * up, so we can safely go to retention */
49 if (!prcmu_is_cpu_in_wfi(this_cpu ? 0 : 1))
52 /* The prcmu will be in charge of watching the interrupts
53 * and wake up the cpus */
54 if (prcmu_copy_gic_settings())
57 /* Check in the meantime an interrupt did
58 * not occur on the gic ... */
59 if (prcmu_gic_pending_irq())
62 /* ... and the prcmu */
63 if (prcmu_pending_irq())
66 /* Go to the retention state, the prcmu will wait for the
67 * cpu to go WFI and this is what happens after exiting this
68 * 'master' critical section */
69 if (prcmu_set_power_state(PRCMU_AP_IDLE, true, true))
72 /* When we switch to retention, the prcmu is in charge
73 * of recoupling the gic automatically */
76 spin_unlock(&master_lock);
85 spin_unlock(&master_lock);
91 static struct cpuidle_driver ux500_idle_driver = {
95 ARM_CPUIDLE_WFI_STATE,
97 .enter = ux500_enter_idle,
99 .target_residency = 260,
100 .flags = CPUIDLE_FLAG_TIMER_STOP,
102 .desc = "ARM Retention",
105 .safe_state_index = 0,
109 static int dbx500_cpuidle_probe(struct platform_device *pdev)
111 /* Configure wake up reasons */
112 prcmu_enable_wakeups(PRCMU_WAKEUP(ARM) | PRCMU_WAKEUP(RTC) |
115 return cpuidle_register(&ux500_idle_driver, NULL);
118 static struct platform_driver dbx500_cpuidle_plat_driver = {
120 .name = "db8500-cpuidle",
122 .probe = dbx500_cpuidle_probe,
124 builtin_platform_driver(dbx500_cpuidle_plat_driver);