1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2013, NVIDIA Corporation. All rights reserved.
4 */
5
6 #include <asm/firmware.h>
7 #include <linux/tick.h>
8 #include <linux/cpuidle.h>
9 #include <linux/cpu_pm.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12
13 #include <linux/firmware/trusted_foundations.h>
14
15 #include <asm/cpuidle.h>
16 #include <asm/smp_plat.h>
17 #include <asm/suspend.h>
18 #include <asm/psci.h>
19
20 #include "cpuidle.h"
21 #include "pm.h"
22 #include "sleep.h"
23
24 #ifdef CONFIG_PM_SLEEP
25 #define TEGRA114_MAX_STATES 2
26 #else
27 #define TEGRA114_MAX_STATES 1
28 #endif
29
30 #ifdef CONFIG_PM_SLEEP
tegra114_idle_power_down(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)31 static int tegra114_idle_power_down(struct cpuidle_device *dev,
32 struct cpuidle_driver *drv,
33 int index)
34 {
35 local_fiq_disable();
36
37 tegra_set_cpu_in_lp2();
38 cpu_pm_enter();
39
40 call_firmware_op(prepare_idle, TF_PM_MODE_LP2_NOFLUSH_L2);
41
42 /* Do suspend by ourselves if the firmware does not implement it */
43 if (call_firmware_op(do_idle, 0) == -ENOSYS)
44 cpu_suspend(0, tegra30_sleep_cpu_secondary_finish);
45
46 cpu_pm_exit();
47 tegra_clear_cpu_in_lp2();
48
49 local_fiq_enable();
50
51 return index;
52 }
53
tegra114_idle_enter_s2idle(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)54 static int tegra114_idle_enter_s2idle(struct cpuidle_device *dev,
55 struct cpuidle_driver *drv,
56 int index)
57 {
58 tegra114_idle_power_down(dev, drv, index);
59
60 return 0;
61 }
62 #endif
63
64 static struct cpuidle_driver tegra_idle_driver = {
65 .name = "tegra_idle",
66 .owner = THIS_MODULE,
67 .state_count = TEGRA114_MAX_STATES,
68 .states = {
69 [0] = ARM_CPUIDLE_WFI_STATE_PWR(600),
70 #ifdef CONFIG_PM_SLEEP
71 [1] = {
72 .enter = tegra114_idle_power_down,
73 .enter_s2idle = tegra114_idle_enter_s2idle,
74 .exit_latency = 500,
75 .target_residency = 1000,
76 .flags = CPUIDLE_FLAG_TIMER_STOP,
77 .power_usage = 0,
78 .name = "powered-down",
79 .desc = "CPU power gated",
80 },
81 #endif
82 },
83 };
84
tegra114_cpuidle_init(void)85 int __init tegra114_cpuidle_init(void)
86 {
87 if (!psci_smp_available())
88 return cpuidle_register(&tegra_idle_driver, NULL);
89
90 return 0;
91 }
92