blob: 0cd2d09475aad2ca3289401b6419cb63f9750bee [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/init.h>
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030023#include <linux/io.h>
Gregory CLEMENT3e328422014-07-23 15:00:39 +020024#include <linux/kernel.h>
Gregory CLEMENT3076cc52014-07-23 15:00:40 +020025#include <linux/mbus.h>
Gregory CLEMENT3e328422014-07-23 15:00:39 +020026#include <linux/of_address.h>
Gregory CLEMENT8c16bab2014-04-14 17:10:14 +020027#include <linux/platform_device.h>
Thomas Petazzoni49754ff2014-04-14 15:50:29 +020028#include <linux/resource.h>
Gregory CLEMENT3e328422014-07-23 15:00:39 +020029#include <linux/smp.h>
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +020030#include <asm/cacheflush.h>
31#include <asm/cp15.h>
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030032#include <asm/smp_plat.h>
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +020033#include <asm/suspend.h>
34#include <asm/tlbflush.h>
Thomas Petazzoni49754ff2014-04-14 15:50:29 +020035#include "common.h"
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030036
37static void __iomem *pmsu_mp_base;
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030038
Gregory CLEMENT0c3acc72014-04-14 15:50:31 +020039#define PMSU_BASE_OFFSET 0x100
40#define PMSU_REG_SIZE 0x1000
41
Gregory CLEMENTf713c7e2014-04-14 17:10:10 +020042/* PMSU MP registers */
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +020043#define PMSU_CONTROL_AND_CONFIG(cpu) ((cpu * 0x100) + 0x104)
44#define PMSU_CONTROL_AND_CONFIG_DFS_REQ BIT(18)
45#define PMSU_CONTROL_AND_CONFIG_PWDDN_REQ BIT(16)
46#define PMSU_CONTROL_AND_CONFIG_L2_PWDDN BIT(20)
47
48#define PMSU_CPU_POWER_DOWN_CONTROL(cpu) ((cpu * 0x100) + 0x108)
49
50#define PMSU_CPU_POWER_DOWN_DIS_SNP_Q_SKIP BIT(0)
51
52#define PMSU_STATUS_AND_MASK(cpu) ((cpu * 0x100) + 0x10c)
53#define PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT BIT(16)
54#define PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT BIT(17)
55#define PMSU_STATUS_AND_MASK_IRQ_WAKEUP BIT(20)
56#define PMSU_STATUS_AND_MASK_FIQ_WAKEUP BIT(21)
57#define PMSU_STATUS_AND_MASK_DBG_WAKEUP BIT(22)
58#define PMSU_STATUS_AND_MASK_IRQ_MASK BIT(24)
59#define PMSU_STATUS_AND_MASK_FIQ_MASK BIT(25)
60
Gregory CLEMENTf713c7e2014-04-14 17:10:10 +020061#define PMSU_BOOT_ADDR_REDIRECT_OFFSET(cpu) ((cpu * 0x100) + 0x124)
62
63/* PMSU fabric registers */
64#define L2C_NFABRIC_PM_CTL 0x4
65#define L2C_NFABRIC_PM_CTL_PWR_DOWN BIT(20)
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030066
Gregory CLEMENT3076cc52014-07-23 15:00:40 +020067#define SRAM_PHYS_BASE 0xFFFF0000
68#define BOOTROM_BASE 0xFFF00000
69#define BOOTROM_SIZE 0x100000
70
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +020071extern void ll_disable_coherency(void);
72extern void ll_enable_coherency(void);
73
Thomas Petazzoni6509dc72014-06-30 14:09:25 +020074extern void armada_370_xp_cpu_resume(void);
75
Gregory CLEMENT752a9932014-07-23 15:00:44 +020076static void *mvebu_cpu_resume;
77
Gregory CLEMENT898ef3e2014-07-23 15:00:42 +020078static struct platform_device mvebu_v7_cpuidle_device = {
Gregory CLEMENTf50ee822014-07-23 15:00:48 +020079 .name = "cpuidle-armada-xp",
Gregory CLEMENT8c16bab2014-04-14 17:10:14 +020080};
81
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030082static struct of_device_id of_pmsu_table[] = {
Gregory CLEMENT0c3acc72014-04-14 15:50:31 +020083 { .compatible = "marvell,armada-370-pmsu", },
84 { .compatible = "marvell,armada-370-xp-pmsu", },
Thomas Petazzonib4bca242014-04-14 15:54:04 +020085 { .compatible = "marvell,armada-380-pmsu", },
Gregory CLEMENT7444dad2012-08-02 11:17:51 +030086 { /* end of list */ },
87};
88
Thomas Petazzoni05ad6902014-04-14 15:53:58 +020089void mvebu_pmsu_set_cpu_boot_addr(int hw_cpu, void *boot_addr)
Gregory CLEMENT02e7b062014-04-14 15:50:33 +020090{
91 writel(virt_to_phys(boot_addr), pmsu_mp_base +
92 PMSU_BOOT_ADDR_REDIRECT_OFFSET(hw_cpu));
93}
94
Gregory CLEMENT3076cc52014-07-23 15:00:40 +020095extern unsigned char mvebu_boot_wa_start;
96extern unsigned char mvebu_boot_wa_end;
97
98/*
99 * This function sets up the boot address workaround needed for SMP
100 * boot on Armada 375 Z1 and cpuidle on Armada 370. It unmaps the
101 * BootROM Mbus window, and instead remaps a crypto SRAM into which a
102 * custom piece of code is copied to replace the problematic BootROM.
103 */
104int mvebu_setup_boot_addr_wa(unsigned int crypto_eng_target,
105 unsigned int crypto_eng_attribute,
106 phys_addr_t resume_addr_reg)
107{
108 void __iomem *sram_virt_base;
109 u32 code_len = &mvebu_boot_wa_end - &mvebu_boot_wa_start;
110
111 mvebu_mbus_del_window(BOOTROM_BASE, BOOTROM_SIZE);
112 mvebu_mbus_add_window_by_id(crypto_eng_target, crypto_eng_attribute,
113 SRAM_PHYS_BASE, SZ_64K);
114
115 sram_virt_base = ioremap(SRAM_PHYS_BASE, SZ_64K);
116 if (!sram_virt_base) {
117 pr_err("Unable to map SRAM to setup the boot address WA\n");
118 return -ENOMEM;
119 }
120
121 memcpy(sram_virt_base, &mvebu_boot_wa_start, code_len);
122
123 /*
124 * The last word of the code copied in SRAM must contain the
125 * physical base address of the PMSU register. We
126 * intentionally store this address in the native endianness
127 * of the system.
128 */
129 __raw_writel((unsigned long)resume_addr_reg,
130 sram_virt_base + code_len - 4);
131
132 iounmap(sram_virt_base);
133
134 return 0;
135}
136
Gregory CLEMENT898ef3e2014-07-23 15:00:42 +0200137static int __init mvebu_v7_pmsu_init(void)
Gregory CLEMENT7444dad2012-08-02 11:17:51 +0300138{
139 struct device_node *np;
Thomas Petazzonibd045a12014-04-14 15:50:30 +0200140 struct resource res;
141 int ret = 0;
Gregory CLEMENT7444dad2012-08-02 11:17:51 +0300142
143 np = of_find_matching_node(NULL, of_pmsu_table);
Thomas Petazzonibd045a12014-04-14 15:50:30 +0200144 if (!np)
145 return 0;
146
147 pr_info("Initializing Power Management Service Unit\n");
148
149 if (of_address_to_resource(np, 0, &res)) {
150 pr_err("unable to get resource\n");
151 ret = -ENOENT;
152 goto out;
Gregory CLEMENT7444dad2012-08-02 11:17:51 +0300153 }
154
Gregory CLEMENT0c3acc72014-04-14 15:50:31 +0200155 if (of_device_is_compatible(np, "marvell,armada-370-xp-pmsu")) {
156 pr_warn(FW_WARN "deprecated pmsu binding\n");
157 res.start = res.start - PMSU_BASE_OFFSET;
158 res.end = res.start + PMSU_REG_SIZE - 1;
159 }
160
Thomas Petazzonibd045a12014-04-14 15:50:30 +0200161 if (!request_mem_region(res.start, resource_size(&res),
162 np->full_name)) {
163 pr_err("unable to request region\n");
164 ret = -EBUSY;
165 goto out;
166 }
167
168 pmsu_mp_base = ioremap(res.start, resource_size(&res));
169 if (!pmsu_mp_base) {
170 pr_err("unable to map registers\n");
171 release_mem_region(res.start, resource_size(&res));
172 ret = -ENOMEM;
173 goto out;
174 }
175
176 out:
177 of_node_put(np);
178 return ret;
Gregory CLEMENT7444dad2012-08-02 11:17:51 +0300179}
180
Gregory CLEMENT898ef3e2014-07-23 15:00:42 +0200181static void mvebu_v7_pmsu_enable_l2_powerdown_onidle(void)
Gregory CLEMENTf713c7e2014-04-14 17:10:10 +0200182{
183 u32 reg;
184
185 if (pmsu_mp_base == NULL)
186 return;
187
188 /* Enable L2 & Fabric powerdown in Deep-Idle mode - Fabric */
189 reg = readl(pmsu_mp_base + L2C_NFABRIC_PM_CTL);
190 reg |= L2C_NFABRIC_PM_CTL_PWR_DOWN;
191 writel(reg, pmsu_mp_base + L2C_NFABRIC_PM_CTL);
192}
193
Gregory CLEMENT5da964e2014-07-23 15:00:45 +0200194enum pmsu_idle_prepare_flags {
195 PMSU_PREPARE_NORMAL = 0,
196 PMSU_PREPARE_DEEP_IDLE = BIT(0),
197 PMSU_PREPARE_SNOOP_DISABLE = BIT(1),
198};
199
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200200/* No locking is needed because we only access per-CPU registers */
Gregory CLEMENT5da964e2014-07-23 15:00:45 +0200201static int mvebu_v7_pmsu_idle_prepare(unsigned long flags)
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200202{
203 unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
204 u32 reg;
205
206 if (pmsu_mp_base == NULL)
Thomas Petazzonibbb92282014-05-30 22:18:15 +0200207 return -EINVAL;
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200208
209 /*
210 * Adjust the PMSU configuration to wait for WFI signal, enable
211 * IRQ and FIQ as wakeup events, set wait for snoop queue empty
212 * indication and mask IRQ and FIQ from CPU
213 */
214 reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
215 reg |= PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT |
216 PMSU_STATUS_AND_MASK_IRQ_WAKEUP |
217 PMSU_STATUS_AND_MASK_FIQ_WAKEUP |
218 PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT |
219 PMSU_STATUS_AND_MASK_IRQ_MASK |
220 PMSU_STATUS_AND_MASK_FIQ_MASK;
221 writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
222
223 reg = readl(pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
224 /* ask HW to power down the L2 Cache if needed */
Gregory CLEMENT5da964e2014-07-23 15:00:45 +0200225 if (flags & PMSU_PREPARE_DEEP_IDLE)
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200226 reg |= PMSU_CONTROL_AND_CONFIG_L2_PWDDN;
227
228 /* request power down */
229 reg |= PMSU_CONTROL_AND_CONFIG_PWDDN_REQ;
230 writel(reg, pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
231
Gregory CLEMENT5da964e2014-07-23 15:00:45 +0200232 if (flags & PMSU_PREPARE_SNOOP_DISABLE) {
233 /* Disable snoop disable by HW - SW is taking care of it */
234 reg = readl(pmsu_mp_base + PMSU_CPU_POWER_DOWN_CONTROL(hw_cpu));
235 reg |= PMSU_CPU_POWER_DOWN_DIS_SNP_Q_SKIP;
236 writel(reg, pmsu_mp_base + PMSU_CPU_POWER_DOWN_CONTROL(hw_cpu));
237 }
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200238
Gregory CLEMENT9ce35882014-07-23 15:00:38 +0200239 return 0;
240}
241
242int armada_370_xp_pmsu_idle_enter(unsigned long deepidle)
243{
Gregory CLEMENT5da964e2014-07-23 15:00:45 +0200244 unsigned long flags = PMSU_PREPARE_SNOOP_DISABLE;
Gregory CLEMENT9ce35882014-07-23 15:00:38 +0200245 int ret;
246
Gregory CLEMENT5da964e2014-07-23 15:00:45 +0200247 if (deepidle)
248 flags |= PMSU_PREPARE_DEEP_IDLE;
249
250 ret = mvebu_v7_pmsu_idle_prepare(flags);
Gregory CLEMENT9ce35882014-07-23 15:00:38 +0200251 if (ret)
252 return ret;
253
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200254 v7_exit_coherency_flush(all);
255
256 ll_disable_coherency();
257
258 dsb();
259
260 wfi();
261
262 /* If we are here, wfi failed. As processors run out of
263 * coherency for some time, tlbs might be stale, so flush them
264 */
265 local_flush_tlb_all();
266
267 ll_enable_coherency();
268
269 /* Test the CR_C bit and set it if it was cleared */
270 asm volatile(
Gregory CLEMENT0d461e12014-07-04 16:22:16 +0200271 "mrc p15, 0, r0, c1, c0, 0 \n\t"
272 "tst r0, #(1 << 2) \n\t"
273 "orreq r0, r0, #(1 << 2) \n\t"
274 "mcreq p15, 0, r0, c1, c0, 0 \n\t"
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200275 "isb "
Gregory CLEMENT0d461e12014-07-04 16:22:16 +0200276 : : : "r0");
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200277
278 pr_warn("Failed to suspend the system\n");
279
280 return 0;
281}
282
283static int armada_370_xp_cpu_suspend(unsigned long deepidle)
284{
Thomas Petazzonibbb92282014-05-30 22:18:15 +0200285 return cpu_suspend(deepidle, armada_370_xp_pmsu_idle_enter);
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200286}
287
288/* No locking is needed because we only access per-CPU registers */
Gregory CLEMENT898ef3e2014-07-23 15:00:42 +0200289void mvebu_v7_pmsu_idle_exit(void)
Gregory CLEMENTc3e04ca2014-04-14 17:10:11 +0200290{
291 unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
292 u32 reg;
293
294 if (pmsu_mp_base == NULL)
295 return;
296
297 /* cancel ask HW to power down the L2 Cache if possible */
298 reg = readl(pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
299 reg &= ~PMSU_CONTROL_AND_CONFIG_L2_PWDDN;
300 writel(reg, pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
301
302 /* cancel Enable wakeup events and mask interrupts */
303 reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
304 reg &= ~(PMSU_STATUS_AND_MASK_IRQ_WAKEUP | PMSU_STATUS_AND_MASK_FIQ_WAKEUP);
305 reg &= ~PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT;
306 reg &= ~PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT;
307 reg &= ~(PMSU_STATUS_AND_MASK_IRQ_MASK | PMSU_STATUS_AND_MASK_FIQ_MASK);
308 writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
309}
310
Gregory CLEMENT898ef3e2014-07-23 15:00:42 +0200311static int mvebu_v7_cpu_pm_notify(struct notifier_block *self,
Gregory CLEMENTd163ee12014-04-14 17:10:12 +0200312 unsigned long action, void *hcpu)
313{
314 if (action == CPU_PM_ENTER) {
315 unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
Gregory CLEMENT752a9932014-07-23 15:00:44 +0200316 mvebu_pmsu_set_cpu_boot_addr(hw_cpu, mvebu_cpu_resume);
Gregory CLEMENTd163ee12014-04-14 17:10:12 +0200317 } else if (action == CPU_PM_EXIT) {
Gregory CLEMENT898ef3e2014-07-23 15:00:42 +0200318 mvebu_v7_pmsu_idle_exit();
Gregory CLEMENTd163ee12014-04-14 17:10:12 +0200319 }
320
321 return NOTIFY_OK;
322}
323
Gregory CLEMENT898ef3e2014-07-23 15:00:42 +0200324static struct notifier_block mvebu_v7_cpu_pm_notifier = {
325 .notifier_call = mvebu_v7_cpu_pm_notify,
Gregory CLEMENTd163ee12014-04-14 17:10:12 +0200326};
327
Gregory CLEMENT54a4d1b2014-07-23 15:00:43 +0200328static int __init armada_xp_cpuidle_init(void)
Gregory CLEMENT8c16bab2014-04-14 17:10:14 +0200329{
330 struct device_node *np;
331
Gregory CLEMENT8c16bab2014-04-14 17:10:14 +0200332 np = of_find_compatible_node(NULL, NULL, "marvell,coherency-fabric");
333 if (!np)
Gregory CLEMENT54a4d1b2014-07-23 15:00:43 +0200334 return -ENODEV;
Gregory CLEMENT8c16bab2014-04-14 17:10:14 +0200335 of_node_put(np);
336
Gregory CLEMENT752a9932014-07-23 15:00:44 +0200337 mvebu_cpu_resume = armada_370_xp_cpu_resume;
Gregory CLEMENT54a4d1b2014-07-23 15:00:43 +0200338 mvebu_v7_cpuidle_device.dev.platform_data = armada_370_xp_cpu_suspend;
339
340 return 0;
341}
342
343static int __init mvebu_v7_cpu_pm_init(void)
344{
345 struct device_node *np;
346 int ret;
347
Gregory CLEMENT8c16bab2014-04-14 17:10:14 +0200348 np = of_find_matching_node(NULL, of_pmsu_table);
349 if (!np)
350 return 0;
351 of_node_put(np);
352
Gregory CLEMENT54a4d1b2014-07-23 15:00:43 +0200353 if (of_machine_is_compatible("marvell,armadaxp"))
354 ret = armada_xp_cpuidle_init();
355 else
356 return 0;
357
358 if (ret)
359 return ret;
360
Gregory CLEMENT898ef3e2014-07-23 15:00:42 +0200361 mvebu_v7_pmsu_enable_l2_powerdown_onidle();
Gregory CLEMENT898ef3e2014-07-23 15:00:42 +0200362 platform_device_register(&mvebu_v7_cpuidle_device);
363 cpu_pm_register_notifier(&mvebu_v7_cpu_pm_notifier);
Gregory CLEMENT8c16bab2014-04-14 17:10:14 +0200364
365 return 0;
366}
367
Gregory CLEMENT898ef3e2014-07-23 15:00:42 +0200368arch_initcall(mvebu_v7_cpu_pm_init);
369early_initcall(mvebu_v7_pmsu_init);