1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d3f2950fSDaniel Lezcano /*
3d3f2950fSDaniel Lezcano * Copyright (c) 2012 Linaro : Daniel Lezcano <daniel.lezcano@linaro.org> (IBM)
4d3f2950fSDaniel Lezcano *
5d3f2950fSDaniel Lezcano * Based on the work of Rickard Andersson <rickard.andersson@stericsson.com>
6d3f2950fSDaniel Lezcano * and Jonas Aaberg <jonas.aberg@stericsson.com>.
7d3f2950fSDaniel Lezcano */
8d3f2950fSDaniel Lezcano
9fdc7d515SPaul Gortmaker #include <linux/init.h>
10d3f2950fSDaniel Lezcano #include <linux/cpuidle.h>
11d3f2950fSDaniel Lezcano #include <linux/spinlock.h>
12d3f2950fSDaniel Lezcano #include <linux/atomic.h>
13d3f2950fSDaniel Lezcano #include <linux/smp.h>
14d3f2950fSDaniel Lezcano #include <linux/mfd/dbx500-prcmu.h>
15d3f2950fSDaniel Lezcano #include <linux/platform_data/arm-ux500-pm.h>
16d3f2950fSDaniel Lezcano #include <linux/platform_device.h>
17d3f2950fSDaniel Lezcano
18d3f2950fSDaniel Lezcano #include <asm/cpuidle.h>
19d3f2950fSDaniel Lezcano
20d3f2950fSDaniel Lezcano static atomic_t master = ATOMIC_INIT(0);
21d3f2950fSDaniel Lezcano static DEFINE_SPINLOCK(master_lock);
22d3f2950fSDaniel Lezcano
ux500_enter_idle(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)23d3f2950fSDaniel Lezcano static inline int ux500_enter_idle(struct cpuidle_device *dev,
24d3f2950fSDaniel Lezcano struct cpuidle_driver *drv, int index)
25d3f2950fSDaniel Lezcano {
26d3f2950fSDaniel Lezcano int this_cpu = smp_processor_id();
27d3f2950fSDaniel Lezcano bool recouple = false;
28d3f2950fSDaniel Lezcano
29d3f2950fSDaniel Lezcano if (atomic_inc_return(&master) == num_online_cpus()) {
30d3f2950fSDaniel Lezcano
31d3f2950fSDaniel Lezcano /* With this lock, we prevent the other cpu to exit and enter
32d3f2950fSDaniel Lezcano * this function again and become the master */
33d3f2950fSDaniel Lezcano if (!spin_trylock(&master_lock))
34d3f2950fSDaniel Lezcano goto wfi;
35d3f2950fSDaniel Lezcano
36d3f2950fSDaniel Lezcano /* decouple the gic from the A9 cores */
37d3f2950fSDaniel Lezcano if (prcmu_gic_decouple()) {
38d3f2950fSDaniel Lezcano spin_unlock(&master_lock);
39d3f2950fSDaniel Lezcano goto out;
40d3f2950fSDaniel Lezcano }
41d3f2950fSDaniel Lezcano
42d3f2950fSDaniel Lezcano /* If an error occur, we will have to recouple the gic
43d3f2950fSDaniel Lezcano * manually */
44d3f2950fSDaniel Lezcano recouple = true;
45d3f2950fSDaniel Lezcano
46d3f2950fSDaniel Lezcano /* At this state, as the gic is decoupled, if the other
47d3f2950fSDaniel Lezcano * cpu is in WFI, we have the guarantee it won't be wake
48d3f2950fSDaniel Lezcano * up, so we can safely go to retention */
49d3f2950fSDaniel Lezcano if (!prcmu_is_cpu_in_wfi(this_cpu ? 0 : 1))
50d3f2950fSDaniel Lezcano goto out;
51d3f2950fSDaniel Lezcano
52d3f2950fSDaniel Lezcano /* The prcmu will be in charge of watching the interrupts
53d3f2950fSDaniel Lezcano * and wake up the cpus */
54d3f2950fSDaniel Lezcano if (prcmu_copy_gic_settings())
55d3f2950fSDaniel Lezcano goto out;
56d3f2950fSDaniel Lezcano
57d3f2950fSDaniel Lezcano /* Check in the meantime an interrupt did
58d3f2950fSDaniel Lezcano * not occur on the gic ... */
59d3f2950fSDaniel Lezcano if (prcmu_gic_pending_irq())
60d3f2950fSDaniel Lezcano goto out;
61d3f2950fSDaniel Lezcano
62d3f2950fSDaniel Lezcano /* ... and the prcmu */
63d3f2950fSDaniel Lezcano if (prcmu_pending_irq())
64d3f2950fSDaniel Lezcano goto out;
65d3f2950fSDaniel Lezcano
66d3f2950fSDaniel Lezcano /* Go to the retention state, the prcmu will wait for the
67d3f2950fSDaniel Lezcano * cpu to go WFI and this is what happens after exiting this
68d3f2950fSDaniel Lezcano * 'master' critical section */
69d3f2950fSDaniel Lezcano if (prcmu_set_power_state(PRCMU_AP_IDLE, true, true))
70d3f2950fSDaniel Lezcano goto out;
71d3f2950fSDaniel Lezcano
72d3f2950fSDaniel Lezcano /* When we switch to retention, the prcmu is in charge
73d3f2950fSDaniel Lezcano * of recoupling the gic automatically */
74d3f2950fSDaniel Lezcano recouple = false;
75d3f2950fSDaniel Lezcano
76d3f2950fSDaniel Lezcano spin_unlock(&master_lock);
77d3f2950fSDaniel Lezcano }
78d3f2950fSDaniel Lezcano wfi:
79d3f2950fSDaniel Lezcano cpu_do_idle();
80d3f2950fSDaniel Lezcano out:
81d3f2950fSDaniel Lezcano atomic_dec(&master);
82d3f2950fSDaniel Lezcano
83d3f2950fSDaniel Lezcano if (recouple) {
84d3f2950fSDaniel Lezcano prcmu_gic_recouple();
85d3f2950fSDaniel Lezcano spin_unlock(&master_lock);
86d3f2950fSDaniel Lezcano }
87d3f2950fSDaniel Lezcano
88d3f2950fSDaniel Lezcano return index;
89d3f2950fSDaniel Lezcano }
90d3f2950fSDaniel Lezcano
91d3f2950fSDaniel Lezcano static struct cpuidle_driver ux500_idle_driver = {
92d3f2950fSDaniel Lezcano .name = "ux500_idle",
93d3f2950fSDaniel Lezcano .owner = THIS_MODULE,
94d3f2950fSDaniel Lezcano .states = {
95d3f2950fSDaniel Lezcano ARM_CPUIDLE_WFI_STATE,
96d3f2950fSDaniel Lezcano {
97d3f2950fSDaniel Lezcano .enter = ux500_enter_idle,
98d3f2950fSDaniel Lezcano .exit_latency = 70,
99d3f2950fSDaniel Lezcano .target_residency = 260,
100b82b6ccaSDaniel Lezcano .flags = CPUIDLE_FLAG_TIMER_STOP,
101d3f2950fSDaniel Lezcano .name = "ApIdle",
102d3f2950fSDaniel Lezcano .desc = "ARM Retention",
103d3f2950fSDaniel Lezcano },
104d3f2950fSDaniel Lezcano },
105d3f2950fSDaniel Lezcano .safe_state_index = 0,
106d3f2950fSDaniel Lezcano .state_count = 2,
107d3f2950fSDaniel Lezcano };
108d3f2950fSDaniel Lezcano
dbx500_cpuidle_probe(struct platform_device * pdev)1092c2b24d9SDaniel Lezcano static int dbx500_cpuidle_probe(struct platform_device *pdev)
110d3f2950fSDaniel Lezcano {
111d3f2950fSDaniel Lezcano /* Configure wake up reasons */
112d3f2950fSDaniel Lezcano prcmu_enable_wakeups(PRCMU_WAKEUP(ARM) | PRCMU_WAKEUP(RTC) |
113d3f2950fSDaniel Lezcano PRCMU_WAKEUP(ABB));
114d3f2950fSDaniel Lezcano
115d3f2950fSDaniel Lezcano return cpuidle_register(&ux500_idle_driver, NULL);
116d3f2950fSDaniel Lezcano }
117d3f2950fSDaniel Lezcano
118d3f2950fSDaniel Lezcano static struct platform_driver dbx500_cpuidle_plat_driver = {
119d3f2950fSDaniel Lezcano .driver = {
120*f28fd3b6SLinus Walleij .name = "db8500-cpuidle",
121d3f2950fSDaniel Lezcano },
122d3f2950fSDaniel Lezcano .probe = dbx500_cpuidle_probe,
123d3f2950fSDaniel Lezcano };
124fdc7d515SPaul Gortmaker builtin_platform_driver(dbx500_cpuidle_plat_driver);
125