blob: 73e662971185b58f8f8221220f027c441bd01811 [file] [log] [blame]
Gregory CLEMENT7444dad2012-08-02 11:17:51 +03001/*
2 * Power Management Service Unit(PMSU) support for Armada 370/XP platforms.
3 *
4 * Copyright (C) 2012 Marvell
5 *
6 * Yehuda Yitschak <yehuday@marvell.com>
7 * Gregory Clement <gregory.clement@free-electrons.com>
8 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
9 *
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
13 *
14 * The Armada 370 and Armada XP SOCs have a power management service
15 * unit which is responsible for powering down and waking up CPUs and
16 * other SOC units
17 */
18
Thomas Petazzonibd045a12014-04-14 15:50:30 +020019#define pr_fmt(fmt) "mvebu-pmsu: " fmt
20
Gregory CLEMENTd163ee12014-04-14 17:10:12 +020021#include <linux/cpu_pm.h>
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030022#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/of_address.h>
25#include <linux/io.h>
Gregory CLEMENT8c16bab2014-04-14 17:10:14 +020026#include <linux/platform_device.h>
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030027#include <linux/smp.h>
Thomas Petazzoni49754ff2014-04-14 15:50:29 +020028#include <linux/resource.h>
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +020029#include <asm/cacheflush.h>
30#include <asm/cp15.h>
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030031#include <asm/smp_plat.h>
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +020032#include <asm/suspend.h>
33#include <asm/tlbflush.h>
Thomas Petazzoni49754ff2014-04-14 15:50:29 +020034#include "common.h"
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030035
36static void __iomem *pmsu_mp_base;
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030037
Gregory CLEMENT0c3acc72014-04-14 15:50:31 +020038#define PMSU_BASE_OFFSET 0x100
39#define PMSU_REG_SIZE 0x1000
40
Gregory CLEMENTf713c7e2014-04-14 17:10:10 +020041/* PMSU MP registers */
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +020042#define PMSU_CONTROL_AND_CONFIG(cpu) ((cpu * 0x100) + 0x104)
43#define PMSU_CONTROL_AND_CONFIG_DFS_REQ BIT(18)
44#define PMSU_CONTROL_AND_CONFIG_PWDDN_REQ BIT(16)
45#define PMSU_CONTROL_AND_CONFIG_L2_PWDDN BIT(20)
46
47#define PMSU_CPU_POWER_DOWN_CONTROL(cpu) ((cpu * 0x100) + 0x108)
48
49#define PMSU_CPU_POWER_DOWN_DIS_SNP_Q_SKIP BIT(0)
50
51#define PMSU_STATUS_AND_MASK(cpu) ((cpu * 0x100) + 0x10c)
52#define PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT BIT(16)
53#define PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT BIT(17)
54#define PMSU_STATUS_AND_MASK_IRQ_WAKEUP BIT(20)
55#define PMSU_STATUS_AND_MASK_FIQ_WAKEUP BIT(21)
56#define PMSU_STATUS_AND_MASK_DBG_WAKEUP BIT(22)
57#define PMSU_STATUS_AND_MASK_IRQ_MASK BIT(24)
58#define PMSU_STATUS_AND_MASK_FIQ_MASK BIT(25)
59
Gregory CLEMENTf713c7e2014-04-14 17:10:10 +020060#define PMSU_BOOT_ADDR_REDIRECT_OFFSET(cpu) ((cpu * 0x100) + 0x124)
61
62/* PMSU fabric registers */
63#define L2C_NFABRIC_PM_CTL 0x4
64#define L2C_NFABRIC_PM_CTL_PWR_DOWN BIT(20)
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030065
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +020066extern void ll_disable_coherency(void);
67extern void ll_enable_coherency(void);
68
Gregory CLEMENT8c16bab2014-04-14 17:10:14 +020069static struct platform_device armada_xp_cpuidle_device = {
70 .name = "cpuidle-armada-370-xp",
71};
72
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030073static struct of_device_id of_pmsu_table[] = {
Gregory CLEMENT0c3acc72014-04-14 15:50:31 +020074 { .compatible = "marvell,armada-370-pmsu", },
75 { .compatible = "marvell,armada-370-xp-pmsu", },
Thomas Petazzonib4bca242014-04-14 15:54:04 +020076 { .compatible = "marvell,armada-380-pmsu", },
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030077 { /* end of list */ },
78};
79
Thomas Petazzoni05ad6902014-04-14 15:53:58 +020080void mvebu_pmsu_set_cpu_boot_addr(int hw_cpu, void *boot_addr)
Gregory CLEMENT02e7b062014-04-14 15:50:33 +020081{
82 writel(virt_to_phys(boot_addr), pmsu_mp_base +
83 PMSU_BOOT_ADDR_REDIRECT_OFFSET(hw_cpu));
84}
85
Jisheng Zhangb12634e2013-11-07 17:02:38 +080086static int __init armada_370_xp_pmsu_init(void)
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030087{
88 struct device_node *np;
Thomas Petazzonibd045a12014-04-14 15:50:30 +020089 struct resource res;
90 int ret = 0;
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030091
92 np = of_find_matching_node(NULL, of_pmsu_table);
Thomas Petazzonibd045a12014-04-14 15:50:30 +020093 if (!np)
94 return 0;
95
96 pr_info("Initializing Power Management Service Unit\n");
97
98 if (of_address_to_resource(np, 0, &res)) {
99 pr_err("unable to get resource\n");
100 ret = -ENOENT;
101 goto out;
Gregory CLEMENT7444dad2012-08-02 11:17:51 +0300102 }
103
Gregory CLEMENT0c3acc72014-04-14 15:50:31 +0200104 if (of_device_is_compatible(np, "marvell,armada-370-xp-pmsu")) {
105 pr_warn(FW_WARN "deprecated pmsu binding\n");
106 res.start = res.start - PMSU_BASE_OFFSET;
107 res.end = res.start + PMSU_REG_SIZE - 1;
108 }
109
Thomas Petazzonibd045a12014-04-14 15:50:30 +0200110 if (!request_mem_region(res.start, resource_size(&res),
111 np->full_name)) {
112 pr_err("unable to request region\n");
113 ret = -EBUSY;
114 goto out;
115 }
116
117 pmsu_mp_base = ioremap(res.start, resource_size(&res));
118 if (!pmsu_mp_base) {
119 pr_err("unable to map registers\n");
120 release_mem_region(res.start, resource_size(&res));
121 ret = -ENOMEM;
122 goto out;
123 }
124
125 out:
126 of_node_put(np);
127 return ret;
Gregory CLEMENT7444dad2012-08-02 11:17:51 +0300128}
129
Gregory CLEMENTf713c7e2014-04-14 17:10:10 +0200130static void armada_370_xp_pmsu_enable_l2_powerdown_onidle(void)
131{
132 u32 reg;
133
134 if (pmsu_mp_base == NULL)
135 return;
136
137 /* Enable L2 & Fabric powerdown in Deep-Idle mode - Fabric */
138 reg = readl(pmsu_mp_base + L2C_NFABRIC_PM_CTL);
139 reg |= L2C_NFABRIC_PM_CTL_PWR_DOWN;
140 writel(reg, pmsu_mp_base + L2C_NFABRIC_PM_CTL);
141}
142
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200143static void armada_370_xp_cpu_resume(void)
144{
145 asm volatile("bl ll_add_cpu_to_smp_group\n\t"
146 "bl ll_enable_coherency\n\t"
147 "b cpu_resume\n\t");
148}
149
150/* No locking is needed because we only access per-CPU registers */
Thomas Petazzonibbb92282014-05-30 22:18:15 +0200151static int armada_370_xp_pmsu_idle_enter(unsigned long deepidle)
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200152{
153 unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
154 u32 reg;
155
156 if (pmsu_mp_base == NULL)
Thomas Petazzonibbb92282014-05-30 22:18:15 +0200157 return -EINVAL;
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200158
159 /*
160 * Adjust the PMSU configuration to wait for WFI signal, enable
161 * IRQ and FIQ as wakeup events, set wait for snoop queue empty
162 * indication and mask IRQ and FIQ from CPU
163 */
164 reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
165 reg |= PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT |
166 PMSU_STATUS_AND_MASK_IRQ_WAKEUP |
167 PMSU_STATUS_AND_MASK_FIQ_WAKEUP |
168 PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT |
169 PMSU_STATUS_AND_MASK_IRQ_MASK |
170 PMSU_STATUS_AND_MASK_FIQ_MASK;
171 writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
172
173 reg = readl(pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
174 /* ask HW to power down the L2 Cache if needed */
175 if (deepidle)
176 reg |= PMSU_CONTROL_AND_CONFIG_L2_PWDDN;
177
178 /* request power down */
179 reg |= PMSU_CONTROL_AND_CONFIG_PWDDN_REQ;
180 writel(reg, pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
181
182 /* Disable snoop disable by HW - SW is taking care of it */
183 reg = readl(pmsu_mp_base + PMSU_CPU_POWER_DOWN_CONTROL(hw_cpu));
184 reg |= PMSU_CPU_POWER_DOWN_DIS_SNP_Q_SKIP;
185 writel(reg, pmsu_mp_base + PMSU_CPU_POWER_DOWN_CONTROL(hw_cpu));
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200186
187 v7_exit_coherency_flush(all);
188
189 ll_disable_coherency();
190
191 dsb();
192
193 wfi();
194
195 /* If we are here, wfi failed. As processors run out of
196 * coherency for some time, tlbs might be stale, so flush them
197 */
198 local_flush_tlb_all();
199
200 ll_enable_coherency();
201
202 /* Test the CR_C bit and set it if it was cleared */
203 asm volatile(
204 "mrc p15, 0, %0, c1, c0, 0 \n\t"
205 "tst %0, #(1 << 2) \n\t"
206 "orreq %0, %0, #(1 << 2) \n\t"
207 "mcreq p15, 0, %0, c1, c0, 0 \n\t"
208 "isb "
209 : : "r" (0));
210
211 pr_warn("Failed to suspend the system\n");
212
213 return 0;
214}
215
216static int armada_370_xp_cpu_suspend(unsigned long deepidle)
217{
Thomas Petazzonibbb92282014-05-30 22:18:15 +0200218 return cpu_suspend(deepidle, armada_370_xp_pmsu_idle_enter);
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200219}
220
221/* No locking is needed because we only access per-CPU registers */
Thomas Petazzonibbb92282014-05-30 22:18:15 +0200222static void armada_370_xp_pmsu_idle_exit(void)
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200223{
224 unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
225 u32 reg;
226
227 if (pmsu_mp_base == NULL)
228 return;
229
230 /* cancel ask HW to power down the L2 Cache if possible */
231 reg = readl(pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
232 reg &= ~PMSU_CONTROL_AND_CONFIG_L2_PWDDN;
233 writel(reg, pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
234
235 /* cancel Enable wakeup events and mask interrupts */
236 reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
237 reg &= ~(PMSU_STATUS_AND_MASK_IRQ_WAKEUP | PMSU_STATUS_AND_MASK_FIQ_WAKEUP);
238 reg &= ~PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT;
239 reg &= ~PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT;
240 reg &= ~(PMSU_STATUS_AND_MASK_IRQ_MASK | PMSU_STATUS_AND_MASK_FIQ_MASK);
241 writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
242}
243
Gregory CLEMENTd163ee12014-04-14 17:10:12 +0200244static int armada_370_xp_cpu_pm_notify(struct notifier_block *self,
245 unsigned long action, void *hcpu)
246{
247 if (action == CPU_PM_ENTER) {
248 unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
249 mvebu_pmsu_set_cpu_boot_addr(hw_cpu, armada_370_xp_cpu_resume);
250 } else if (action == CPU_PM_EXIT) {
Thomas Petazzonibbb92282014-05-30 22:18:15 +0200251 armada_370_xp_pmsu_idle_exit();
Gregory CLEMENTd163ee12014-04-14 17:10:12 +0200252 }
253
254 return NOTIFY_OK;
255}
256
257static struct notifier_block armada_370_xp_cpu_pm_notifier = {
258 .notifier_call = armada_370_xp_cpu_pm_notify,
259};
260
Gregory CLEMENT8c16bab2014-04-14 17:10:14 +0200261int __init armada_370_xp_cpu_pm_init(void)
262{
263 struct device_node *np;
264
265 /*
266 * Check that all the requirements are available to enable
267 * cpuidle. So far, it is only supported on Armada XP, cpuidle
268 * needs the coherency fabric and the PMSU enabled
269 */
270
271 if (!of_machine_is_compatible("marvell,armadaxp"))
272 return 0;
273
274 np = of_find_compatible_node(NULL, NULL, "marvell,coherency-fabric");
275 if (!np)
276 return 0;
277 of_node_put(np);
278
279 np = of_find_matching_node(NULL, of_pmsu_table);
280 if (!np)
281 return 0;
282 of_node_put(np);
283
284 armada_370_xp_pmsu_enable_l2_powerdown_onidle();
285 armada_xp_cpuidle_device.dev.platform_data = armada_370_xp_cpu_suspend;
286 platform_device_register(&armada_xp_cpuidle_device);
287 cpu_pm_register_notifier(&armada_370_xp_cpu_pm_notifier);
288
289 return 0;
290}
291
292arch_initcall(armada_370_xp_cpu_pm_init);
Gregory CLEMENT7444dad2012-08-02 11:17:51 +0300293early_initcall(armada_370_xp_pmsu_init);