blob: f7d778580e9be692fe9b16ec638fb4e59ff35246 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +02002/*
3 * Copyright (c) 2012 Linaro : Daniel Lezcano <daniel.lezcano@linaro.org> (IBM)
4 *
5 * Based on the work of Rickard Andersson <rickard.andersson@stericsson.com>
6 * and Jonas Aaberg <jonas.aberg@stericsson.com>.
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +02007 */
8
Paul Gortmakerfdc7d512015-12-13 18:57:11 -05009#include <linux/init.h>
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +020010#include <linux/cpuidle.h>
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +020011#include <linux/spinlock.h>
12#include <linux/atomic.h>
13#include <linux/smp.h>
14#include <linux/mfd/dbx500-prcmu.h>
Linus Walleij1e22a8c2013-03-19 15:36:12 +010015#include <linux/platform_data/arm-ux500-pm.h>
Linus Walleij80253952013-07-10 15:35:26 +020016#include <linux/platform_device.h>
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +020017
18#include <asm/cpuidle.h>
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +020019
20static atomic_t master = ATOMIC_INIT(0);
21static DEFINE_SPINLOCK(master_lock);
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +020022
23static inline int ux500_enter_idle(struct cpuidle_device *dev,
24 struct cpuidle_driver *drv, int index)
25{
26 int this_cpu = smp_processor_id();
27 bool recouple = false;
28
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +020029 if (atomic_inc_return(&master) == num_online_cpus()) {
30
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))
34 goto wfi;
35
36 /* decouple the gic from the A9 cores */
Steve Zhan5cc23662013-01-23 11:24:47 +010037 if (prcmu_gic_decouple()) {
38 spin_unlock(&master_lock);
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +020039 goto out;
Steve Zhan5cc23662013-01-23 11:24:47 +010040 }
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +020041
42 /* If an error occur, we will have to recouple the gic
43 * manually */
44 recouple = true;
45
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))
50 goto out;
51
52 /* The prcmu will be in charge of watching the interrupts
53 * and wake up the cpus */
54 if (prcmu_copy_gic_settings())
55 goto out;
56
57 /* Check in the meantime an interrupt did
58 * not occur on the gic ... */
59 if (prcmu_gic_pending_irq())
60 goto out;
61
62 /* ... and the prcmu */
63 if (prcmu_pending_irq())
64 goto out;
65
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))
70 goto out;
71
72 /* When we switch to retention, the prcmu is in charge
73 * of recoupling the gic automatically */
74 recouple = false;
75
76 spin_unlock(&master_lock);
77 }
78wfi:
79 cpu_do_idle();
80out:
81 atomic_dec(&master);
82
83 if (recouple) {
84 prcmu_gic_recouple();
85 spin_unlock(&master_lock);
86 }
87
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +020088 return index;
89}
90
91static struct cpuidle_driver ux500_idle_driver = {
92 .name = "ux500_idle",
93 .owner = THIS_MODULE,
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +020094 .states = {
95 ARM_CPUIDLE_WFI_STATE,
96 {
97 .enter = ux500_enter_idle,
98 .exit_latency = 70,
99 .target_residency = 260,
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100100 .flags = CPUIDLE_FLAG_TIMER_STOP,
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +0200101 .name = "ApIdle",
102 .desc = "ARM Retention",
103 },
104 },
105 .safe_state_index = 0,
106 .state_count = 2,
107};
108
Daniel Lezcano2c2b24d2013-09-27 18:57:07 +0200109static int dbx500_cpuidle_probe(struct platform_device *pdev)
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +0200110{
Linus Walleij1e22a8c2013-03-19 15:36:12 +0100111 /* Configure wake up reasons */
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +0200112 prcmu_enable_wakeups(PRCMU_WAKEUP(ARM) | PRCMU_WAKEUP(RTC) |
113 PRCMU_WAKEUP(ABB));
114
Daniel Lezcanoe8928e22013-04-23 08:54:34 +0000115 return cpuidle_register(&ux500_idle_driver, NULL);
Daniel Lezcano3ebabaa2012-04-19 14:46:32 +0200116}
117
Linus Walleij80253952013-07-10 15:35:26 +0200118static struct platform_driver dbx500_cpuidle_plat_driver = {
119 .driver = {
Linus Walleijf28fd3b2021-08-06 11:14:04 +0200120 .name = "db8500-cpuidle",
Linus Walleij80253952013-07-10 15:35:26 +0200121 },
122 .probe = dbx500_cpuidle_probe,
123};
Paul Gortmakerfdc7d512015-12-13 18:57:11 -0500124builtin_platform_driver(dbx500_cpuidle_plat_driver);