10fdebc5eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27444dad2SGregory CLEMENT /*
37444dad2SGregory CLEMENT * Power Management Service Unit(PMSU) support for Armada 370/XP platforms.
47444dad2SGregory CLEMENT *
57444dad2SGregory CLEMENT * Copyright (C) 2012 Marvell
67444dad2SGregory CLEMENT *
77444dad2SGregory CLEMENT * Yehuda Yitschak <yehuday@marvell.com>
87444dad2SGregory CLEMENT * Gregory Clement <gregory.clement@free-electrons.com>
97444dad2SGregory CLEMENT * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
107444dad2SGregory CLEMENT *
117444dad2SGregory CLEMENT * The Armada 370 and Armada XP SOCs have a power management service
127444dad2SGregory CLEMENT * unit which is responsible for powering down and waking up CPUs and
137444dad2SGregory CLEMENT * other SOC units
147444dad2SGregory CLEMENT */
157444dad2SGregory CLEMENT
16bd045a1eSThomas Petazzoni #define pr_fmt(fmt) "mvebu-pmsu: " fmt
17bd045a1eSThomas Petazzoni
18a509ea84SThomas Petazzoni #include <linux/clk.h>
19d163ee16SGregory CLEMENT #include <linux/cpu_pm.h>
20a509ea84SThomas Petazzoni #include <linux/delay.h>
217444dad2SGregory CLEMENT #include <linux/init.h>
227444dad2SGregory CLEMENT #include <linux/io.h>
233e328428SGregory CLEMENT #include <linux/kernel.h>
243076cc58SGregory CLEMENT #include <linux/mbus.h>
2553e2fd83SBen Dooks #include <linux/mvebu-pmsu.h>
26*1402f75dSRob Herring #include <linux/of.h>
277444dad2SGregory CLEMENT #include <linux/of_address.h>
288c16babcSGregory CLEMENT #include <linux/platform_device.h>
2949754ffeSThomas Petazzoni #include <linux/resource.h>
30a509ea84SThomas Petazzoni #include <linux/slab.h>
313e328428SGregory CLEMENT #include <linux/smp.h>
32c3e04cabSGregory CLEMENT #include <asm/cacheflush.h>
33c3e04cabSGregory CLEMENT #include <asm/cp15.h>
34e53b1fd4SGregory CLEMENT #include <asm/smp_scu.h>
357444dad2SGregory CLEMENT #include <asm/smp_plat.h>
36c3e04cabSGregory CLEMENT #include <asm/suspend.h>
37c3e04cabSGregory CLEMENT #include <asm/tlbflush.h>
3849754ffeSThomas Petazzoni #include "common.h"
3953e2fd83SBen Dooks #include "pmsu.h"
407444dad2SGregory CLEMENT
410c3acc74SGregory CLEMENT #define PMSU_BASE_OFFSET 0x100
420c3acc74SGregory CLEMENT #define PMSU_REG_SIZE 0x1000
430c3acc74SGregory CLEMENT
44f713c7e7SGregory CLEMENT /* PMSU MP registers */
45c3e04cabSGregory CLEMENT #define PMSU_CONTROL_AND_CONFIG(cpu) ((cpu * 0x100) + 0x104)
46c3e04cabSGregory CLEMENT #define PMSU_CONTROL_AND_CONFIG_DFS_REQ BIT(18)
47c3e04cabSGregory CLEMENT #define PMSU_CONTROL_AND_CONFIG_PWDDN_REQ BIT(16)
48c3e04cabSGregory CLEMENT #define PMSU_CONTROL_AND_CONFIG_L2_PWDDN BIT(20)
49c3e04cabSGregory CLEMENT
50c3e04cabSGregory CLEMENT #define PMSU_CPU_POWER_DOWN_CONTROL(cpu) ((cpu * 0x100) + 0x108)
51c3e04cabSGregory CLEMENT
52c3e04cabSGregory CLEMENT #define PMSU_CPU_POWER_DOWN_DIS_SNP_Q_SKIP BIT(0)
53c3e04cabSGregory CLEMENT
54c3e04cabSGregory CLEMENT #define PMSU_STATUS_AND_MASK(cpu) ((cpu * 0x100) + 0x10c)
55c3e04cabSGregory CLEMENT #define PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT BIT(16)
56c3e04cabSGregory CLEMENT #define PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT BIT(17)
57c3e04cabSGregory CLEMENT #define PMSU_STATUS_AND_MASK_IRQ_WAKEUP BIT(20)
58c3e04cabSGregory CLEMENT #define PMSU_STATUS_AND_MASK_FIQ_WAKEUP BIT(21)
59c3e04cabSGregory CLEMENT #define PMSU_STATUS_AND_MASK_DBG_WAKEUP BIT(22)
60c3e04cabSGregory CLEMENT #define PMSU_STATUS_AND_MASK_IRQ_MASK BIT(24)
61c3e04cabSGregory CLEMENT #define PMSU_STATUS_AND_MASK_FIQ_MASK BIT(25)
62c3e04cabSGregory CLEMENT
63a509ea84SThomas Petazzoni #define PMSU_EVENT_STATUS_AND_MASK(cpu) ((cpu * 0x100) + 0x120)
64a509ea84SThomas Petazzoni #define PMSU_EVENT_STATUS_AND_MASK_DFS_DONE BIT(1)
65a509ea84SThomas Petazzoni #define PMSU_EVENT_STATUS_AND_MASK_DFS_DONE_MASK BIT(17)
66a509ea84SThomas Petazzoni
670c3acc74SGregory CLEMENT #define PMSU_BOOT_ADDR_REDIRECT_OFFSET(cpu) ((cpu * 0x100) + 0x124)
687444dad2SGregory CLEMENT
69f713c7e7SGregory CLEMENT /* PMSU fabric registers */
70f713c7e7SGregory CLEMENT #define L2C_NFABRIC_PM_CTL 0x4
71f713c7e7SGregory CLEMENT #define L2C_NFABRIC_PM_CTL_PWR_DOWN BIT(20)
72f713c7e7SGregory CLEMENT
73e53b1fd4SGregory CLEMENT /* PMSU delay registers */
74e53b1fd4SGregory CLEMENT #define PMSU_POWERDOWN_DELAY 0xF04
75e53b1fd4SGregory CLEMENT #define PMSU_POWERDOWN_DELAY_PMU BIT(1)
76e53b1fd4SGregory CLEMENT #define PMSU_POWERDOWN_DELAY_MASK 0xFFFE
77e53b1fd4SGregory CLEMENT #define PMSU_DFLT_ARMADA38X_DELAY 0x64
78e53b1fd4SGregory CLEMENT
79e53b1fd4SGregory CLEMENT /* CA9 MPcore SoC Control registers */
80e53b1fd4SGregory CLEMENT
81e53b1fd4SGregory CLEMENT #define MPCORE_RESET_CTL 0x64
82e53b1fd4SGregory CLEMENT #define MPCORE_RESET_CTL_L2 BIT(0)
83e53b1fd4SGregory CLEMENT #define MPCORE_RESET_CTL_DEBUG BIT(16)
84e53b1fd4SGregory CLEMENT
853076cc58SGregory CLEMENT #define SRAM_PHYS_BASE 0xFFFF0000
863076cc58SGregory CLEMENT #define BOOTROM_BASE 0xFFF00000
873076cc58SGregory CLEMENT #define BOOTROM_SIZE 0x100000
883076cc58SGregory CLEMENT
893b9e4b14SGregory CLEMENT #define ARMADA_370_CRYPT0_ENG_TARGET 0x9
903b9e4b14SGregory CLEMENT #define ARMADA_370_CRYPT0_ENG_ATTR 0x1
913b9e4b14SGregory CLEMENT
92c3e04cabSGregory CLEMENT extern void ll_disable_coherency(void);
93c3e04cabSGregory CLEMENT extern void ll_enable_coherency(void);
94c3e04cabSGregory CLEMENT
956509dc74SThomas Petazzoni extern void armada_370_xp_cpu_resume(void);
96e53b1fd4SGregory CLEMENT extern void armada_38x_cpu_resume(void);
97e53b1fd4SGregory CLEMENT
983b9e4b14SGregory CLEMENT static phys_addr_t pmsu_mp_phys_base;
993b9e4b14SGregory CLEMENT static void __iomem *pmsu_mp_base;
1006509dc74SThomas Petazzoni
101752a9937SGregory CLEMENT static void *mvebu_cpu_resume;
1028c16babcSGregory CLEMENT
103444d2d33SUwe Kleine-König static const struct of_device_id of_pmsu_table[] = {
1040c3acc74SGregory CLEMENT { .compatible = "marvell,armada-370-pmsu", },
1050c3acc74SGregory CLEMENT { .compatible = "marvell,armada-370-xp-pmsu", },
106b4bca249SThomas Petazzoni { .compatible = "marvell,armada-380-pmsu", },
1077444dad2SGregory CLEMENT { /* end of list */ },
1087444dad2SGregory CLEMENT };
1097444dad2SGregory CLEMENT
mvebu_pmsu_set_cpu_boot_addr(int hw_cpu,void * boot_addr)11005ad6906SThomas Petazzoni void mvebu_pmsu_set_cpu_boot_addr(int hw_cpu, void *boot_addr)
11102e7b067SGregory CLEMENT {
11264fc2a94SFlorian Fainelli writel(__pa_symbol(boot_addr), pmsu_mp_base +
11302e7b067SGregory CLEMENT PMSU_BOOT_ADDR_REDIRECT_OFFSET(hw_cpu));
11402e7b067SGregory CLEMENT }
11502e7b067SGregory CLEMENT
116d0d378ffSEthan Tuttle extern unsigned char mvebu_boot_wa_start[];
117d0d378ffSEthan Tuttle extern unsigned char mvebu_boot_wa_end[];
1183076cc58SGregory CLEMENT
1193076cc58SGregory CLEMENT /*
1203076cc58SGregory CLEMENT * This function sets up the boot address workaround needed for SMP
1213076cc58SGregory CLEMENT * boot on Armada 375 Z1 and cpuidle on Armada 370. It unmaps the
1223076cc58SGregory CLEMENT * BootROM Mbus window, and instead remaps a crypto SRAM into which a
1233076cc58SGregory CLEMENT * custom piece of code is copied to replace the problematic BootROM.
1243076cc58SGregory CLEMENT */
mvebu_setup_boot_addr_wa(unsigned int crypto_eng_target,unsigned int crypto_eng_attribute,phys_addr_t resume_addr_reg)1253076cc58SGregory CLEMENT int mvebu_setup_boot_addr_wa(unsigned int crypto_eng_target,
1263076cc58SGregory CLEMENT unsigned int crypto_eng_attribute,
1273076cc58SGregory CLEMENT phys_addr_t resume_addr_reg)
1283076cc58SGregory CLEMENT {
1293076cc58SGregory CLEMENT void __iomem *sram_virt_base;
130d0d378ffSEthan Tuttle u32 code_len = mvebu_boot_wa_end - mvebu_boot_wa_start;
1313076cc58SGregory CLEMENT
1323076cc58SGregory CLEMENT mvebu_mbus_del_window(BOOTROM_BASE, BOOTROM_SIZE);
1333076cc58SGregory CLEMENT mvebu_mbus_add_window_by_id(crypto_eng_target, crypto_eng_attribute,
1343076cc58SGregory CLEMENT SRAM_PHYS_BASE, SZ_64K);
1353076cc58SGregory CLEMENT
1363076cc58SGregory CLEMENT sram_virt_base = ioremap(SRAM_PHYS_BASE, SZ_64K);
1373076cc58SGregory CLEMENT if (!sram_virt_base) {
1383076cc58SGregory CLEMENT pr_err("Unable to map SRAM to setup the boot address WA\n");
1393076cc58SGregory CLEMENT return -ENOMEM;
1403076cc58SGregory CLEMENT }
1413076cc58SGregory CLEMENT
1423076cc58SGregory CLEMENT memcpy(sram_virt_base, &mvebu_boot_wa_start, code_len);
1433076cc58SGregory CLEMENT
1443076cc58SGregory CLEMENT /*
1453076cc58SGregory CLEMENT * The last word of the code copied in SRAM must contain the
1463076cc58SGregory CLEMENT * physical base address of the PMSU register. We
1473076cc58SGregory CLEMENT * intentionally store this address in the native endianness
1483076cc58SGregory CLEMENT * of the system.
1493076cc58SGregory CLEMENT */
1503076cc58SGregory CLEMENT __raw_writel((unsigned long)resume_addr_reg,
1513076cc58SGregory CLEMENT sram_virt_base + code_len - 4);
1523076cc58SGregory CLEMENT
1533076cc58SGregory CLEMENT iounmap(sram_virt_base);
1543076cc58SGregory CLEMENT
1553076cc58SGregory CLEMENT return 0;
1563076cc58SGregory CLEMENT }
1573076cc58SGregory CLEMENT
mvebu_v7_pmsu_init(void)158898ef3e9SGregory CLEMENT static int __init mvebu_v7_pmsu_init(void)
1597444dad2SGregory CLEMENT {
1607444dad2SGregory CLEMENT struct device_node *np;
161bd045a1eSThomas Petazzoni struct resource res;
162bd045a1eSThomas Petazzoni int ret = 0;
1637444dad2SGregory CLEMENT
1647444dad2SGregory CLEMENT np = of_find_matching_node(NULL, of_pmsu_table);
165bd045a1eSThomas Petazzoni if (!np)
166bd045a1eSThomas Petazzoni return 0;
167bd045a1eSThomas Petazzoni
1687444dad2SGregory CLEMENT pr_info("Initializing Power Management Service Unit\n");
169bd045a1eSThomas Petazzoni
170bd045a1eSThomas Petazzoni if (of_address_to_resource(np, 0, &res)) {
171bd045a1eSThomas Petazzoni pr_err("unable to get resource\n");
172bd045a1eSThomas Petazzoni ret = -ENOENT;
173bd045a1eSThomas Petazzoni goto out;
1747444dad2SGregory CLEMENT }
1757444dad2SGregory CLEMENT
1760c3acc74SGregory CLEMENT if (of_device_is_compatible(np, "marvell,armada-370-xp-pmsu")) {
1770c3acc74SGregory CLEMENT pr_warn(FW_WARN "deprecated pmsu binding\n");
1780c3acc74SGregory CLEMENT res.start = res.start - PMSU_BASE_OFFSET;
1790c3acc74SGregory CLEMENT res.end = res.start + PMSU_REG_SIZE - 1;
1800c3acc74SGregory CLEMENT }
1810c3acc74SGregory CLEMENT
182bd045a1eSThomas Petazzoni if (!request_mem_region(res.start, resource_size(&res),
183bd045a1eSThomas Petazzoni np->full_name)) {
184bd045a1eSThomas Petazzoni pr_err("unable to request region\n");
185bd045a1eSThomas Petazzoni ret = -EBUSY;
186bd045a1eSThomas Petazzoni goto out;
187bd045a1eSThomas Petazzoni }
188bd045a1eSThomas Petazzoni
1893b9e4b14SGregory CLEMENT pmsu_mp_phys_base = res.start;
1903b9e4b14SGregory CLEMENT
191bd045a1eSThomas Petazzoni pmsu_mp_base = ioremap(res.start, resource_size(&res));
192bd045a1eSThomas Petazzoni if (!pmsu_mp_base) {
193bd045a1eSThomas Petazzoni pr_err("unable to map registers\n");
194bd045a1eSThomas Petazzoni release_mem_region(res.start, resource_size(&res));
195bd045a1eSThomas Petazzoni ret = -ENOMEM;
196bd045a1eSThomas Petazzoni goto out;
197bd045a1eSThomas Petazzoni }
198bd045a1eSThomas Petazzoni
199bd045a1eSThomas Petazzoni out:
200bd045a1eSThomas Petazzoni of_node_put(np);
201bd045a1eSThomas Petazzoni return ret;
2027444dad2SGregory CLEMENT }
2037444dad2SGregory CLEMENT
mvebu_v7_pmsu_enable_l2_powerdown_onidle(void)204898ef3e9SGregory CLEMENT static void mvebu_v7_pmsu_enable_l2_powerdown_onidle(void)
205f713c7e7SGregory CLEMENT {
206f713c7e7SGregory CLEMENT u32 reg;
207f713c7e7SGregory CLEMENT
208f713c7e7SGregory CLEMENT if (pmsu_mp_base == NULL)
209f713c7e7SGregory CLEMENT return;
210f713c7e7SGregory CLEMENT
211f713c7e7SGregory CLEMENT /* Enable L2 & Fabric powerdown in Deep-Idle mode - Fabric */
212f713c7e7SGregory CLEMENT reg = readl(pmsu_mp_base + L2C_NFABRIC_PM_CTL);
213f713c7e7SGregory CLEMENT reg |= L2C_NFABRIC_PM_CTL_PWR_DOWN;
214f713c7e7SGregory CLEMENT writel(reg, pmsu_mp_base + L2C_NFABRIC_PM_CTL);
215f713c7e7SGregory CLEMENT }
216f713c7e7SGregory CLEMENT
2175da964e0SGregory CLEMENT enum pmsu_idle_prepare_flags {
2185da964e0SGregory CLEMENT PMSU_PREPARE_NORMAL = 0,
2195da964e0SGregory CLEMENT PMSU_PREPARE_DEEP_IDLE = BIT(0),
2205da964e0SGregory CLEMENT PMSU_PREPARE_SNOOP_DISABLE = BIT(1),
2215da964e0SGregory CLEMENT };
222c3e04cabSGregory CLEMENT
223c3e04cabSGregory CLEMENT /* No locking is needed because we only access per-CPU registers */
mvebu_v7_pmsu_idle_prepare(unsigned long flags)2245da964e0SGregory CLEMENT static int mvebu_v7_pmsu_idle_prepare(unsigned long flags)
225c3e04cabSGregory CLEMENT {
226c3e04cabSGregory CLEMENT unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
227c3e04cabSGregory CLEMENT u32 reg;
228c3e04cabSGregory CLEMENT
229c3e04cabSGregory CLEMENT if (pmsu_mp_base == NULL)
230bbb92284SThomas Petazzoni return -EINVAL;
231c3e04cabSGregory CLEMENT
232c3e04cabSGregory CLEMENT /*
233c3e04cabSGregory CLEMENT * Adjust the PMSU configuration to wait for WFI signal, enable
234c3e04cabSGregory CLEMENT * IRQ and FIQ as wakeup events, set wait for snoop queue empty
235c3e04cabSGregory CLEMENT * indication and mask IRQ and FIQ from CPU
236c3e04cabSGregory CLEMENT */
237c3e04cabSGregory CLEMENT reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
238c3e04cabSGregory CLEMENT reg |= PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT |
239c3e04cabSGregory CLEMENT PMSU_STATUS_AND_MASK_IRQ_WAKEUP |
240c3e04cabSGregory CLEMENT PMSU_STATUS_AND_MASK_FIQ_WAKEUP |
241c3e04cabSGregory CLEMENT PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT |
242c3e04cabSGregory CLEMENT PMSU_STATUS_AND_MASK_IRQ_MASK |
243c3e04cabSGregory CLEMENT PMSU_STATUS_AND_MASK_FIQ_MASK;
244c3e04cabSGregory CLEMENT writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
245c3e04cabSGregory CLEMENT
246c3e04cabSGregory CLEMENT reg = readl(pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
247c3e04cabSGregory CLEMENT /* ask HW to power down the L2 Cache if needed */
2485da964e0SGregory CLEMENT if (flags & PMSU_PREPARE_DEEP_IDLE)
249c3e04cabSGregory CLEMENT reg |= PMSU_CONTROL_AND_CONFIG_L2_PWDDN;
250c3e04cabSGregory CLEMENT
251c3e04cabSGregory CLEMENT /* request power down */
252c3e04cabSGregory CLEMENT reg |= PMSU_CONTROL_AND_CONFIG_PWDDN_REQ;
253c3e04cabSGregory CLEMENT writel(reg, pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
254c3e04cabSGregory CLEMENT
2555da964e0SGregory CLEMENT if (flags & PMSU_PREPARE_SNOOP_DISABLE) {
256c3e04cabSGregory CLEMENT /* Disable snoop disable by HW - SW is taking care of it */
257c3e04cabSGregory CLEMENT reg = readl(pmsu_mp_base + PMSU_CPU_POWER_DOWN_CONTROL(hw_cpu));
258c3e04cabSGregory CLEMENT reg |= PMSU_CPU_POWER_DOWN_DIS_SNP_Q_SKIP;
259c3e04cabSGregory CLEMENT writel(reg, pmsu_mp_base + PMSU_CPU_POWER_DOWN_CONTROL(hw_cpu));
2605da964e0SGregory CLEMENT }
261c3e04cabSGregory CLEMENT
2629ce35884SGregory CLEMENT return 0;
2639ce35884SGregory CLEMENT }
2649ce35884SGregory CLEMENT
armada_370_xp_pmsu_idle_enter(unsigned long deepidle)2659ce35884SGregory CLEMENT int armada_370_xp_pmsu_idle_enter(unsigned long deepidle)
2669ce35884SGregory CLEMENT {
2675da964e0SGregory CLEMENT unsigned long flags = PMSU_PREPARE_SNOOP_DISABLE;
2689ce35884SGregory CLEMENT int ret;
2699ce35884SGregory CLEMENT
2705da964e0SGregory CLEMENT if (deepidle)
2715da964e0SGregory CLEMENT flags |= PMSU_PREPARE_DEEP_IDLE;
2725da964e0SGregory CLEMENT
2735da964e0SGregory CLEMENT ret = mvebu_v7_pmsu_idle_prepare(flags);
2749ce35884SGregory CLEMENT if (ret)
2759ce35884SGregory CLEMENT return ret;
276c3e04cabSGregory CLEMENT
277c3e04cabSGregory CLEMENT v7_exit_coherency_flush(all);
278c3e04cabSGregory CLEMENT
279c3e04cabSGregory CLEMENT ll_disable_coherency();
280c3e04cabSGregory CLEMENT
281c3e04cabSGregory CLEMENT dsb();
282c3e04cabSGregory CLEMENT
283c3e04cabSGregory CLEMENT wfi();
284c3e04cabSGregory CLEMENT
285c3e04cabSGregory CLEMENT /* If we are here, wfi failed. As processors run out of
286c3e04cabSGregory CLEMENT * coherency for some time, tlbs might be stale, so flush them
287c3e04cabSGregory CLEMENT */
288c3e04cabSGregory CLEMENT local_flush_tlb_all();
289c3e04cabSGregory CLEMENT
290c3e04cabSGregory CLEMENT ll_enable_coherency();
291c3e04cabSGregory CLEMENT
292c3e04cabSGregory CLEMENT /* Test the CR_C bit and set it if it was cleared */
293c3e04cabSGregory CLEMENT asm volatile(
294a2faac39SNick Desaulniers ".arch armv7-a\n\t"
2950d461e1bSGregory CLEMENT "mrc p15, 0, r0, c1, c0, 0 \n\t"
2967ee20ff0SGregory CLEMENT "tst r0, %0 \n\t"
2970d461e1bSGregory CLEMENT "orreq r0, r0, #(1 << 2) \n\t"
2980d461e1bSGregory CLEMENT "mcreq p15, 0, r0, c1, c0, 0 \n\t"
299c3e04cabSGregory CLEMENT "isb "
3007ee20ff0SGregory CLEMENT : : "Ir" (CR_C) : "r0");
301c3e04cabSGregory CLEMENT
3023b9e4b14SGregory CLEMENT pr_debug("Failed to suspend the system\n");
303c3e04cabSGregory CLEMENT
304c3e04cabSGregory CLEMENT return 0;
305c3e04cabSGregory CLEMENT }
306c3e04cabSGregory CLEMENT
armada_370_xp_cpu_suspend(unsigned long deepidle)307c3e04cabSGregory CLEMENT static int armada_370_xp_cpu_suspend(unsigned long deepidle)
308c3e04cabSGregory CLEMENT {
309bbb92284SThomas Petazzoni return cpu_suspend(deepidle, armada_370_xp_pmsu_idle_enter);
310c3e04cabSGregory CLEMENT }
311c3e04cabSGregory CLEMENT
armada_38x_do_cpu_suspend(unsigned long deepidle)312626d6864SGregory CLEMENT int armada_38x_do_cpu_suspend(unsigned long deepidle)
313e53b1fd4SGregory CLEMENT {
314e53b1fd4SGregory CLEMENT unsigned long flags = 0;
315e53b1fd4SGregory CLEMENT
316e53b1fd4SGregory CLEMENT if (deepidle)
317e53b1fd4SGregory CLEMENT flags |= PMSU_PREPARE_DEEP_IDLE;
318e53b1fd4SGregory CLEMENT
319e53b1fd4SGregory CLEMENT mvebu_v7_pmsu_idle_prepare(flags);
320e53b1fd4SGregory CLEMENT /*
321e53b1fd4SGregory CLEMENT * Already flushed cache, but do it again as the outer cache
322e53b1fd4SGregory CLEMENT * functions dirty the cache with spinlocks
323e53b1fd4SGregory CLEMENT */
324e53b1fd4SGregory CLEMENT v7_exit_coherency_flush(louis);
325e53b1fd4SGregory CLEMENT
326e53b1fd4SGregory CLEMENT scu_power_mode(mvebu_get_scu_base(), SCU_PM_POWEROFF);
327e53b1fd4SGregory CLEMENT
328e53b1fd4SGregory CLEMENT cpu_do_idle();
329e53b1fd4SGregory CLEMENT
330e53b1fd4SGregory CLEMENT return 1;
331e53b1fd4SGregory CLEMENT }
332e53b1fd4SGregory CLEMENT
armada_38x_cpu_suspend(unsigned long deepidle)333e53b1fd4SGregory CLEMENT static int armada_38x_cpu_suspend(unsigned long deepidle)
334e53b1fd4SGregory CLEMENT {
335e53b1fd4SGregory CLEMENT return cpu_suspend(false, armada_38x_do_cpu_suspend);
336e53b1fd4SGregory CLEMENT }
337e53b1fd4SGregory CLEMENT
338c3e04cabSGregory CLEMENT /* No locking is needed because we only access per-CPU registers */
mvebu_v7_pmsu_idle_exit(void)339898ef3e9SGregory CLEMENT void mvebu_v7_pmsu_idle_exit(void)
340c3e04cabSGregory CLEMENT {
341c3e04cabSGregory CLEMENT unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
342c3e04cabSGregory CLEMENT u32 reg;
343c3e04cabSGregory CLEMENT
344c3e04cabSGregory CLEMENT if (pmsu_mp_base == NULL)
345c3e04cabSGregory CLEMENT return;
346c3e04cabSGregory CLEMENT /* cancel ask HW to power down the L2 Cache if possible */
347c3e04cabSGregory CLEMENT reg = readl(pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
348c3e04cabSGregory CLEMENT reg &= ~PMSU_CONTROL_AND_CONFIG_L2_PWDDN;
349c3e04cabSGregory CLEMENT writel(reg, pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
350c3e04cabSGregory CLEMENT
351c3e04cabSGregory CLEMENT /* cancel Enable wakeup events and mask interrupts */
352c3e04cabSGregory CLEMENT reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
353c3e04cabSGregory CLEMENT reg &= ~(PMSU_STATUS_AND_MASK_IRQ_WAKEUP | PMSU_STATUS_AND_MASK_FIQ_WAKEUP);
354c3e04cabSGregory CLEMENT reg &= ~PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT;
355c3e04cabSGregory CLEMENT reg &= ~PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT;
356c3e04cabSGregory CLEMENT reg &= ~(PMSU_STATUS_AND_MASK_IRQ_MASK | PMSU_STATUS_AND_MASK_FIQ_MASK);
357c3e04cabSGregory CLEMENT writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
358c3e04cabSGregory CLEMENT }
359c3e04cabSGregory CLEMENT
mvebu_v7_cpu_pm_notify(struct notifier_block * self,unsigned long action,void * hcpu)360898ef3e9SGregory CLEMENT static int mvebu_v7_cpu_pm_notify(struct notifier_block *self,
361d163ee16SGregory CLEMENT unsigned long action, void *hcpu)
362d163ee16SGregory CLEMENT {
363d163ee16SGregory CLEMENT if (action == CPU_PM_ENTER) {
364d163ee16SGregory CLEMENT unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
365752a9937SGregory CLEMENT mvebu_pmsu_set_cpu_boot_addr(hw_cpu, mvebu_cpu_resume);
366d163ee16SGregory CLEMENT } else if (action == CPU_PM_EXIT) {
367898ef3e9SGregory CLEMENT mvebu_v7_pmsu_idle_exit();
368d163ee16SGregory CLEMENT }
369d163ee16SGregory CLEMENT
370d163ee16SGregory CLEMENT return NOTIFY_OK;
371d163ee16SGregory CLEMENT }
372d163ee16SGregory CLEMENT
373898ef3e9SGregory CLEMENT static struct notifier_block mvebu_v7_cpu_pm_notifier = {
374898ef3e9SGregory CLEMENT .notifier_call = mvebu_v7_cpu_pm_notify,
375d163ee16SGregory CLEMENT };
376d163ee16SGregory CLEMENT
3773b9e4b14SGregory CLEMENT static struct platform_device mvebu_v7_cpuidle_device;
3783b9e4b14SGregory CLEMENT
broken_idle(struct device_node * np)3799d2ea95aSVincent Donnefort static int broken_idle(struct device_node *np)
3809d2ea95aSVincent Donnefort {
3819d2ea95aSVincent Donnefort if (of_property_read_bool(np, "broken-idle")) {
3829d2ea95aSVincent Donnefort pr_warn("CPU idle is currently broken: disabling\n");
3839d2ea95aSVincent Donnefort return 1;
3849d2ea95aSVincent Donnefort }
3859d2ea95aSVincent Donnefort
3869d2ea95aSVincent Donnefort return 0;
3879d2ea95aSVincent Donnefort }
3889d2ea95aSVincent Donnefort
armada_370_cpuidle_init(void)3893b9e4b14SGregory CLEMENT static __init int armada_370_cpuidle_init(void)
3908c16babcSGregory CLEMENT {
3918c16babcSGregory CLEMENT struct device_node *np;
3923b9e4b14SGregory CLEMENT phys_addr_t redirect_reg;
3938c16babcSGregory CLEMENT
3948c16babcSGregory CLEMENT np = of_find_compatible_node(NULL, NULL, "marvell,coherency-fabric");
3958c16babcSGregory CLEMENT if (!np)
3963b9e4b14SGregory CLEMENT return -ENODEV;
3979d2ea95aSVincent Donnefort
3989d2ea95aSVincent Donnefort if (broken_idle(np))
3999d2ea95aSVincent Donnefort goto end;
4008c16babcSGregory CLEMENT
4013b9e4b14SGregory CLEMENT /*
4023b9e4b14SGregory CLEMENT * On Armada 370, there is "a slow exit process from the deep
4033b9e4b14SGregory CLEMENT * idle state due to heavy L1/L2 cache cleanup operations
4043b9e4b14SGregory CLEMENT * performed by the BootROM software". To avoid this, we
4053b9e4b14SGregory CLEMENT * replace the restart code of the bootrom by a a simple jump
4063b9e4b14SGregory CLEMENT * to the boot address. Then the code located at this boot
4073b9e4b14SGregory CLEMENT * address will take care of the initialization.
4083b9e4b14SGregory CLEMENT */
4093b9e4b14SGregory CLEMENT redirect_reg = pmsu_mp_phys_base + PMSU_BOOT_ADDR_REDIRECT_OFFSET(0);
4103b9e4b14SGregory CLEMENT mvebu_setup_boot_addr_wa(ARMADA_370_CRYPT0_ENG_TARGET,
4113b9e4b14SGregory CLEMENT ARMADA_370_CRYPT0_ENG_ATTR,
4123b9e4b14SGregory CLEMENT redirect_reg);
4133b9e4b14SGregory CLEMENT
4143b9e4b14SGregory CLEMENT mvebu_cpu_resume = armada_370_xp_cpu_resume;
4153b9e4b14SGregory CLEMENT mvebu_v7_cpuidle_device.dev.platform_data = armada_370_xp_cpu_suspend;
4163b9e4b14SGregory CLEMENT mvebu_v7_cpuidle_device.name = "cpuidle-armada-370";
4173b9e4b14SGregory CLEMENT
4189d2ea95aSVincent Donnefort end:
4199d2ea95aSVincent Donnefort of_node_put(np);
4203b9e4b14SGregory CLEMENT return 0;
4213b9e4b14SGregory CLEMENT }
4223b9e4b14SGregory CLEMENT
armada_38x_cpuidle_init(void)423e53b1fd4SGregory CLEMENT static __init int armada_38x_cpuidle_init(void)
424e53b1fd4SGregory CLEMENT {
425e53b1fd4SGregory CLEMENT struct device_node *np;
426e53b1fd4SGregory CLEMENT void __iomem *mpsoc_base;
427e53b1fd4SGregory CLEMENT u32 reg;
428e53b1fd4SGregory CLEMENT
429a17683baSThomas Petazzoni pr_warn("CPU idle is currently broken on Armada 38x: disabling\n");
430548ae94cSGregory CLEMENT return 0;
431548ae94cSGregory CLEMENT
432e53b1fd4SGregory CLEMENT np = of_find_compatible_node(NULL, NULL,
433e53b1fd4SGregory CLEMENT "marvell,armada-380-coherency-fabric");
434e53b1fd4SGregory CLEMENT if (!np)
435e53b1fd4SGregory CLEMENT return -ENODEV;
4369d2ea95aSVincent Donnefort
4379d2ea95aSVincent Donnefort if (broken_idle(np))
4389d2ea95aSVincent Donnefort goto end;
4399d2ea95aSVincent Donnefort
440e53b1fd4SGregory CLEMENT of_node_put(np);
441e53b1fd4SGregory CLEMENT
442e53b1fd4SGregory CLEMENT np = of_find_compatible_node(NULL, NULL,
443e53b1fd4SGregory CLEMENT "marvell,armada-380-mpcore-soc-ctrl");
444e53b1fd4SGregory CLEMENT if (!np)
445e53b1fd4SGregory CLEMENT return -ENODEV;
446e53b1fd4SGregory CLEMENT mpsoc_base = of_iomap(np, 0);
447e53b1fd4SGregory CLEMENT BUG_ON(!mpsoc_base);
448e53b1fd4SGregory CLEMENT
449e53b1fd4SGregory CLEMENT /* Set up reset mask when powering down the cpus */
450e53b1fd4SGregory CLEMENT reg = readl(mpsoc_base + MPCORE_RESET_CTL);
451e53b1fd4SGregory CLEMENT reg |= MPCORE_RESET_CTL_L2;
452e53b1fd4SGregory CLEMENT reg |= MPCORE_RESET_CTL_DEBUG;
453e53b1fd4SGregory CLEMENT writel(reg, mpsoc_base + MPCORE_RESET_CTL);
454e53b1fd4SGregory CLEMENT iounmap(mpsoc_base);
455e53b1fd4SGregory CLEMENT
456e53b1fd4SGregory CLEMENT /* Set up delay */
457e53b1fd4SGregory CLEMENT reg = readl(pmsu_mp_base + PMSU_POWERDOWN_DELAY);
458e53b1fd4SGregory CLEMENT reg &= ~PMSU_POWERDOWN_DELAY_MASK;
459e53b1fd4SGregory CLEMENT reg |= PMSU_DFLT_ARMADA38X_DELAY;
460e53b1fd4SGregory CLEMENT reg |= PMSU_POWERDOWN_DELAY_PMU;
461e53b1fd4SGregory CLEMENT writel(reg, pmsu_mp_base + PMSU_POWERDOWN_DELAY);
462e53b1fd4SGregory CLEMENT
463e53b1fd4SGregory CLEMENT mvebu_cpu_resume = armada_38x_cpu_resume;
464e53b1fd4SGregory CLEMENT mvebu_v7_cpuidle_device.dev.platform_data = armada_38x_cpu_suspend;
465e53b1fd4SGregory CLEMENT mvebu_v7_cpuidle_device.name = "cpuidle-armada-38x";
466e53b1fd4SGregory CLEMENT
4679d2ea95aSVincent Donnefort end:
4689d2ea95aSVincent Donnefort of_node_put(np);
469e53b1fd4SGregory CLEMENT return 0;
470e53b1fd4SGregory CLEMENT }
471e53b1fd4SGregory CLEMENT
armada_xp_cpuidle_init(void)4723b9e4b14SGregory CLEMENT static __init int armada_xp_cpuidle_init(void)
4738c16babcSGregory CLEMENT {
4748c16babcSGregory CLEMENT struct device_node *np;
4758c16babcSGregory CLEMENT
4768c16babcSGregory CLEMENT np = of_find_compatible_node(NULL, NULL, "marvell,coherency-fabric");
4778c16babcSGregory CLEMENT if (!np)
47854a4d1b8SGregory CLEMENT return -ENODEV;
4799d2ea95aSVincent Donnefort
4809d2ea95aSVincent Donnefort if (broken_idle(np))
4819d2ea95aSVincent Donnefort goto end;
4828c16babcSGregory CLEMENT
483752a9937SGregory CLEMENT mvebu_cpu_resume = armada_370_xp_cpu_resume;
48454a4d1b8SGregory CLEMENT mvebu_v7_cpuidle_device.dev.platform_data = armada_370_xp_cpu_suspend;
4853b9e4b14SGregory CLEMENT mvebu_v7_cpuidle_device.name = "cpuidle-armada-xp";
48654a4d1b8SGregory CLEMENT
4879d2ea95aSVincent Donnefort end:
4889d2ea95aSVincent Donnefort of_node_put(np);
48954a4d1b8SGregory CLEMENT return 0;
49054a4d1b8SGregory CLEMENT }
49154a4d1b8SGregory CLEMENT
mvebu_v7_cpu_pm_init(void)49254a4d1b8SGregory CLEMENT static int __init mvebu_v7_cpu_pm_init(void)
49354a4d1b8SGregory CLEMENT {
49454a4d1b8SGregory CLEMENT struct device_node *np;
49554a4d1b8SGregory CLEMENT int ret;
49654a4d1b8SGregory CLEMENT
4978c16babcSGregory CLEMENT np = of_find_matching_node(NULL, of_pmsu_table);
4988c16babcSGregory CLEMENT if (!np)
4998c16babcSGregory CLEMENT return 0;
5008c16babcSGregory CLEMENT of_node_put(np);
5018c16babcSGregory CLEMENT
502548ae94cSGregory CLEMENT /*
503548ae94cSGregory CLEMENT * Currently the CPU idle support for Armada 38x is broken, as
504548ae94cSGregory CLEMENT * the CPU hotplug uses some of the CPU idle functions it is
505548ae94cSGregory CLEMENT * broken too, so let's disable it
506548ae94cSGregory CLEMENT */
507548ae94cSGregory CLEMENT if (of_machine_is_compatible("marvell,armada380")) {
508548ae94cSGregory CLEMENT cpu_hotplug_disable();
509a17683baSThomas Petazzoni pr_warn("CPU hotplug support is currently broken on Armada 38x: disabling\n");
510548ae94cSGregory CLEMENT }
511548ae94cSGregory CLEMENT
51254a4d1b8SGregory CLEMENT if (of_machine_is_compatible("marvell,armadaxp"))
51354a4d1b8SGregory CLEMENT ret = armada_xp_cpuidle_init();
5143b9e4b14SGregory CLEMENT else if (of_machine_is_compatible("marvell,armada370"))
5153b9e4b14SGregory CLEMENT ret = armada_370_cpuidle_init();
516e53b1fd4SGregory CLEMENT else if (of_machine_is_compatible("marvell,armada380"))
517e53b1fd4SGregory CLEMENT ret = armada_38x_cpuidle_init();
51854a4d1b8SGregory CLEMENT else
51954a4d1b8SGregory CLEMENT return 0;
52054a4d1b8SGregory CLEMENT
52154a4d1b8SGregory CLEMENT if (ret)
52254a4d1b8SGregory CLEMENT return ret;
52354a4d1b8SGregory CLEMENT
524898ef3e9SGregory CLEMENT mvebu_v7_pmsu_enable_l2_powerdown_onidle();
525548ae94cSGregory CLEMENT if (mvebu_v7_cpuidle_device.name)
526898ef3e9SGregory CLEMENT platform_device_register(&mvebu_v7_cpuidle_device);
527898ef3e9SGregory CLEMENT cpu_pm_register_notifier(&mvebu_v7_cpu_pm_notifier);
5288c16babcSGregory CLEMENT
5298c16babcSGregory CLEMENT return 0;
5308c16babcSGregory CLEMENT }
5318c16babcSGregory CLEMENT
532898ef3e9SGregory CLEMENT arch_initcall(mvebu_v7_cpu_pm_init);
533898ef3e9SGregory CLEMENT early_initcall(mvebu_v7_pmsu_init);
534a509ea84SThomas Petazzoni
mvebu_pmsu_dfs_request_local(void * data)535a509ea84SThomas Petazzoni static void mvebu_pmsu_dfs_request_local(void *data)
536a509ea84SThomas Petazzoni {
537a509ea84SThomas Petazzoni u32 reg;
538a509ea84SThomas Petazzoni u32 cpu = smp_processor_id();
539a509ea84SThomas Petazzoni unsigned long flags;
540a509ea84SThomas Petazzoni
541a509ea84SThomas Petazzoni local_irq_save(flags);
542a509ea84SThomas Petazzoni
543a509ea84SThomas Petazzoni /* Prepare to enter idle */
544a509ea84SThomas Petazzoni reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(cpu));
545a509ea84SThomas Petazzoni reg |= PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT |
546a509ea84SThomas Petazzoni PMSU_STATUS_AND_MASK_IRQ_MASK |
547a509ea84SThomas Petazzoni PMSU_STATUS_AND_MASK_FIQ_MASK;
548a509ea84SThomas Petazzoni writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(cpu));
549a509ea84SThomas Petazzoni
550a509ea84SThomas Petazzoni /* Request the DFS transition */
551a509ea84SThomas Petazzoni reg = readl(pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(cpu));
552a509ea84SThomas Petazzoni reg |= PMSU_CONTROL_AND_CONFIG_DFS_REQ;
553a509ea84SThomas Petazzoni writel(reg, pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(cpu));
554a509ea84SThomas Petazzoni
555a509ea84SThomas Petazzoni /* The fact of entering idle will trigger the DFS transition */
556a509ea84SThomas Petazzoni wfi();
557a509ea84SThomas Petazzoni
558a509ea84SThomas Petazzoni /*
559a509ea84SThomas Petazzoni * We're back from idle, the DFS transition has completed,
560a509ea84SThomas Petazzoni * clear the idle wait indication.
561a509ea84SThomas Petazzoni */
562a509ea84SThomas Petazzoni reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(cpu));
563a509ea84SThomas Petazzoni reg &= ~PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT;
564a509ea84SThomas Petazzoni writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(cpu));
565a509ea84SThomas Petazzoni
566a509ea84SThomas Petazzoni local_irq_restore(flags);
567a509ea84SThomas Petazzoni }
568a509ea84SThomas Petazzoni
mvebu_pmsu_dfs_request(int cpu)569a509ea84SThomas Petazzoni int mvebu_pmsu_dfs_request(int cpu)
570a509ea84SThomas Petazzoni {
571a509ea84SThomas Petazzoni unsigned long timeout;
572a509ea84SThomas Petazzoni int hwcpu = cpu_logical_map(cpu);
573a509ea84SThomas Petazzoni u32 reg;
574a509ea84SThomas Petazzoni
575a509ea84SThomas Petazzoni /* Clear any previous DFS DONE event */
576a509ea84SThomas Petazzoni reg = readl(pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
577a509ea84SThomas Petazzoni reg &= ~PMSU_EVENT_STATUS_AND_MASK_DFS_DONE;
578a509ea84SThomas Petazzoni writel(reg, pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
579a509ea84SThomas Petazzoni
580a509ea84SThomas Petazzoni /* Mask the DFS done interrupt, since we are going to poll */
581a509ea84SThomas Petazzoni reg = readl(pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
582a509ea84SThomas Petazzoni reg |= PMSU_EVENT_STATUS_AND_MASK_DFS_DONE_MASK;
583a509ea84SThomas Petazzoni writel(reg, pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
584a509ea84SThomas Petazzoni
585a509ea84SThomas Petazzoni /* Trigger the DFS on the appropriate CPU */
586a509ea84SThomas Petazzoni smp_call_function_single(cpu, mvebu_pmsu_dfs_request_local,
587a509ea84SThomas Petazzoni NULL, false);
588a509ea84SThomas Petazzoni
589a509ea84SThomas Petazzoni /* Poll until the DFS done event is generated */
590a509ea84SThomas Petazzoni timeout = jiffies + HZ;
591a509ea84SThomas Petazzoni while (time_before(jiffies, timeout)) {
592a509ea84SThomas Petazzoni reg = readl(pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
593a509ea84SThomas Petazzoni if (reg & PMSU_EVENT_STATUS_AND_MASK_DFS_DONE)
594a509ea84SThomas Petazzoni break;
595a509ea84SThomas Petazzoni udelay(10);
596a509ea84SThomas Petazzoni }
597a509ea84SThomas Petazzoni
598a509ea84SThomas Petazzoni if (time_after(jiffies, timeout))
599a509ea84SThomas Petazzoni return -ETIME;
600a509ea84SThomas Petazzoni
601a509ea84SThomas Petazzoni /* Restore the DFS mask to its original state */
602a509ea84SThomas Petazzoni reg = readl(pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
603a509ea84SThomas Petazzoni reg &= ~PMSU_EVENT_STATUS_AND_MASK_DFS_DONE_MASK;
604a509ea84SThomas Petazzoni writel(reg, pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
605a509ea84SThomas Petazzoni
606a509ea84SThomas Petazzoni return 0;
607a509ea84SThomas Petazzoni }
608