Commit | Line | Data |
---|---|---|
be6a98d3 RH |
1 | /* |
2 | * Copyright 2012 Calxeda, Inc. | |
3 | * | |
4 | * Based on arch/arm/plat-mxc/cpuidle.c: | |
5 | * Copyright 2012 Freescale Semiconductor, Inc. | |
6 | * Copyright 2012 Linaro Ltd. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms and conditions of the GNU General Public License, | |
10 | * version 2, as published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope it will be useful, but WITHOUT | |
13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 | * more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along with | |
18 | * this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <linux/cpuidle.h> | |
22 | #include <linux/init.h> | |
23 | #include <linux/io.h> | |
24 | #include <linux/of.h> | |
25 | #include <linux/time.h> | |
26 | #include <linux/delay.h> | |
27 | #include <linux/suspend.h> | |
28 | #include <asm/cpuidle.h> | |
29 | #include <asm/proc-fns.h> | |
30 | #include <asm/smp_scu.h> | |
31 | #include <asm/suspend.h> | |
32 | #include <asm/cacheflush.h> | |
33 | #include <asm/cp15.h> | |
34 | ||
35 | extern void highbank_set_cpu_jump(int cpu, void *jump_addr); | |
36 | extern void *scu_base_addr; | |
37 | ||
38 | static struct cpuidle_device __percpu *calxeda_idle_cpuidle_devices; | |
39 | ||
40 | static inline unsigned int get_auxcr(void) | |
41 | { | |
42 | unsigned int val; | |
43 | asm("mrc p15, 0, %0, c1, c0, 1 @ get AUXCR" : "=r" (val) : : "cc"); | |
44 | return val; | |
45 | } | |
46 | ||
47 | static inline void set_auxcr(unsigned int val) | |
48 | { | |
49 | asm volatile("mcr p15, 0, %0, c1, c0, 1 @ set AUXCR" | |
50 | : : "r" (val) : "cc"); | |
51 | isb(); | |
52 | } | |
53 | ||
54 | static noinline void calxeda_idle_restore(void) | |
55 | { | |
56 | set_cr(get_cr() | CR_C); | |
57 | set_auxcr(get_auxcr() | 0x40); | |
58 | scu_power_mode(scu_base_addr, SCU_PM_NORMAL); | |
59 | } | |
60 | ||
61 | static int calxeda_idle_finish(unsigned long val) | |
62 | { | |
63 | /* Already flushed cache, but do it again as the outer cache functions | |
64 | * dirty the cache with spinlocks */ | |
65 | flush_cache_all(); | |
66 | ||
67 | set_auxcr(get_auxcr() & ~0x40); | |
68 | set_cr(get_cr() & ~CR_C); | |
69 | ||
70 | scu_power_mode(scu_base_addr, SCU_PM_DORMANT); | |
71 | ||
72 | cpu_do_idle(); | |
73 | ||
74 | /* Restore things if we didn't enter power-gating */ | |
75 | calxeda_idle_restore(); | |
76 | return 1; | |
77 | } | |
78 | ||
79 | static int calxeda_pwrdown_idle(struct cpuidle_device *dev, | |
80 | struct cpuidle_driver *drv, | |
81 | int index) | |
82 | { | |
83 | highbank_set_cpu_jump(smp_processor_id(), cpu_resume); | |
84 | cpu_suspend(0, calxeda_idle_finish); | |
85 | return index; | |
86 | } | |
87 | ||
88 | static void calxeda_idle_cpuidle_devices_uninit(void) | |
89 | { | |
90 | int i; | |
91 | struct cpuidle_device *dev; | |
92 | ||
93 | for_each_possible_cpu(i) { | |
94 | dev = per_cpu_ptr(calxeda_idle_cpuidle_devices, i); | |
95 | cpuidle_unregister_device(dev); | |
96 | } | |
97 | ||
98 | free_percpu(calxeda_idle_cpuidle_devices); | |
99 | } | |
100 | ||
101 | static struct cpuidle_driver calxeda_idle_driver = { | |
102 | .name = "calxeda_idle", | |
be6a98d3 RH |
103 | .states = { |
104 | ARM_CPUIDLE_WFI_STATE, | |
105 | { | |
106 | .name = "PG", | |
107 | .desc = "Power Gate", | |
108 | .flags = CPUIDLE_FLAG_TIME_VALID, | |
109 | .exit_latency = 30, | |
110 | .power_usage = 50, | |
111 | .target_residency = 200, | |
112 | .enter = calxeda_pwrdown_idle, | |
113 | }, | |
114 | }, | |
115 | .state_count = 2, | |
116 | }; | |
117 | ||
118 | static int __init calxeda_cpuidle_init(void) | |
119 | { | |
120 | int cpu_id; | |
121 | int ret; | |
122 | struct cpuidle_device *dev; | |
123 | struct cpuidle_driver *drv = &calxeda_idle_driver; | |
124 | ||
125 | if (!of_machine_is_compatible("calxeda,highbank")) | |
126 | return -ENODEV; | |
127 | ||
128 | ret = cpuidle_register_driver(drv); | |
129 | if (ret) | |
130 | return ret; | |
131 | ||
132 | calxeda_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device); | |
133 | if (calxeda_idle_cpuidle_devices == NULL) { | |
134 | ret = -ENOMEM; | |
135 | goto unregister_drv; | |
136 | } | |
137 | ||
138 | /* initialize state data for each cpuidle_device */ | |
139 | for_each_possible_cpu(cpu_id) { | |
140 | dev = per_cpu_ptr(calxeda_idle_cpuidle_devices, cpu_id); | |
141 | dev->cpu = cpu_id; | |
142 | dev->state_count = drv->state_count; | |
143 | ||
144 | ret = cpuidle_register_device(dev); | |
145 | if (ret) { | |
146 | pr_err("Failed to register cpu %u, error: %d\n", | |
147 | cpu_id, ret); | |
148 | goto uninit; | |
149 | } | |
150 | } | |
151 | ||
152 | return 0; | |
153 | ||
154 | uninit: | |
155 | calxeda_idle_cpuidle_devices_uninit(); | |
156 | unregister_drv: | |
157 | cpuidle_unregister_driver(drv); | |
158 | return ret; | |
159 | } | |
160 | module_init(calxeda_cpuidle_init); |