]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/arm/mach-mvebu/pmsu.c
Merge branch 'mvebu/soc-cpufreq' into mvebu/soc
[karo-tx-linux.git] / arch / arm / mach-mvebu / pmsu.c
1 /*
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
19 #define pr_fmt(fmt) "mvebu-pmsu: " fmt
20
21 #include <linux/clk.h>
22 #include <linux/cpu_pm.h>
23 #include <linux/delay.h>
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/of_address.h>
27 #include <linux/of_device.h>
28 #include <linux/io.h>
29 #include <linux/platform_device.h>
30 #include <linux/pm_opp.h>
31 #include <linux/smp.h>
32 #include <linux/resource.h>
33 #include <linux/slab.h>
34 #include <asm/cacheflush.h>
35 #include <asm/cp15.h>
36 #include <asm/smp_plat.h>
37 #include <asm/suspend.h>
38 #include <asm/tlbflush.h>
39 #include "common.h"
40 #include "armada-370-xp.h"
41
42 static void __iomem *pmsu_mp_base;
43
44 #define PMSU_BASE_OFFSET    0x100
45 #define PMSU_REG_SIZE       0x1000
46
47 /* PMSU MP registers */
48 #define PMSU_CONTROL_AND_CONFIG(cpu)        ((cpu * 0x100) + 0x104)
49 #define PMSU_CONTROL_AND_CONFIG_DFS_REQ         BIT(18)
50 #define PMSU_CONTROL_AND_CONFIG_PWDDN_REQ       BIT(16)
51 #define PMSU_CONTROL_AND_CONFIG_L2_PWDDN        BIT(20)
52
53 #define PMSU_CPU_POWER_DOWN_CONTROL(cpu)    ((cpu * 0x100) + 0x108)
54
55 #define PMSU_CPU_POWER_DOWN_DIS_SNP_Q_SKIP      BIT(0)
56
57 #define PMSU_STATUS_AND_MASK(cpu)           ((cpu * 0x100) + 0x10c)
58 #define PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT      BIT(16)
59 #define PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT   BIT(17)
60 #define PMSU_STATUS_AND_MASK_IRQ_WAKEUP         BIT(20)
61 #define PMSU_STATUS_AND_MASK_FIQ_WAKEUP         BIT(21)
62 #define PMSU_STATUS_AND_MASK_DBG_WAKEUP         BIT(22)
63 #define PMSU_STATUS_AND_MASK_IRQ_MASK           BIT(24)
64 #define PMSU_STATUS_AND_MASK_FIQ_MASK           BIT(25)
65
66 #define PMSU_EVENT_STATUS_AND_MASK(cpu)     ((cpu * 0x100) + 0x120)
67 #define PMSU_EVENT_STATUS_AND_MASK_DFS_DONE        BIT(1)
68 #define PMSU_EVENT_STATUS_AND_MASK_DFS_DONE_MASK   BIT(17)
69
70 #define PMSU_BOOT_ADDR_REDIRECT_OFFSET(cpu) ((cpu * 0x100) + 0x124)
71
72 /* PMSU fabric registers */
73 #define L2C_NFABRIC_PM_CTL                  0x4
74 #define L2C_NFABRIC_PM_CTL_PWR_DOWN             BIT(20)
75
76 extern void ll_disable_coherency(void);
77 extern void ll_enable_coherency(void);
78
79 static struct platform_device armada_xp_cpuidle_device = {
80         .name = "cpuidle-armada-370-xp",
81 };
82
83 static struct of_device_id of_pmsu_table[] = {
84         { .compatible = "marvell,armada-370-pmsu", },
85         { .compatible = "marvell,armada-370-xp-pmsu", },
86         { .compatible = "marvell,armada-380-pmsu", },
87         { /* end of list */ },
88 };
89
90 void mvebu_pmsu_set_cpu_boot_addr(int hw_cpu, void *boot_addr)
91 {
92         writel(virt_to_phys(boot_addr), pmsu_mp_base +
93                 PMSU_BOOT_ADDR_REDIRECT_OFFSET(hw_cpu));
94 }
95
96 static int __init armada_370_xp_pmsu_init(void)
97 {
98         struct device_node *np;
99         struct resource res;
100         int ret = 0;
101
102         np = of_find_matching_node(NULL, of_pmsu_table);
103         if (!np)
104                 return 0;
105
106         pr_info("Initializing Power Management Service Unit\n");
107
108         if (of_address_to_resource(np, 0, &res)) {
109                 pr_err("unable to get resource\n");
110                 ret = -ENOENT;
111                 goto out;
112         }
113
114         if (of_device_is_compatible(np, "marvell,armada-370-xp-pmsu")) {
115                 pr_warn(FW_WARN "deprecated pmsu binding\n");
116                 res.start = res.start - PMSU_BASE_OFFSET;
117                 res.end = res.start + PMSU_REG_SIZE - 1;
118         }
119
120         if (!request_mem_region(res.start, resource_size(&res),
121                                 np->full_name)) {
122                 pr_err("unable to request region\n");
123                 ret = -EBUSY;
124                 goto out;
125         }
126
127         pmsu_mp_base = ioremap(res.start, resource_size(&res));
128         if (!pmsu_mp_base) {
129                 pr_err("unable to map registers\n");
130                 release_mem_region(res.start, resource_size(&res));
131                 ret = -ENOMEM;
132                 goto out;
133         }
134
135  out:
136         of_node_put(np);
137         return ret;
138 }
139
140 static void armada_370_xp_pmsu_enable_l2_powerdown_onidle(void)
141 {
142         u32 reg;
143
144         if (pmsu_mp_base == NULL)
145                 return;
146
147         /* Enable L2 & Fabric powerdown in Deep-Idle mode - Fabric */
148         reg = readl(pmsu_mp_base + L2C_NFABRIC_PM_CTL);
149         reg |= L2C_NFABRIC_PM_CTL_PWR_DOWN;
150         writel(reg, pmsu_mp_base + L2C_NFABRIC_PM_CTL);
151 }
152
153 static void armada_370_xp_cpu_resume(void)
154 {
155         asm volatile("bl    ll_add_cpu_to_smp_group\n\t"
156                      "bl    ll_enable_coherency\n\t"
157                      "b     cpu_resume\n\t");
158 }
159
160 /* No locking is needed because we only access per-CPU registers */
161 int armada_370_xp_pmsu_idle_enter(unsigned long deepidle)
162 {
163         unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
164         u32 reg;
165
166         if (pmsu_mp_base == NULL)
167                 return -EINVAL;
168
169         /*
170          * Adjust the PMSU configuration to wait for WFI signal, enable
171          * IRQ and FIQ as wakeup events, set wait for snoop queue empty
172          * indication and mask IRQ and FIQ from CPU
173          */
174         reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
175         reg |= PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT    |
176                PMSU_STATUS_AND_MASK_IRQ_WAKEUP       |
177                PMSU_STATUS_AND_MASK_FIQ_WAKEUP       |
178                PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT |
179                PMSU_STATUS_AND_MASK_IRQ_MASK         |
180                PMSU_STATUS_AND_MASK_FIQ_MASK;
181         writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
182
183         reg = readl(pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
184         /* ask HW to power down the L2 Cache if needed */
185         if (deepidle)
186                 reg |= PMSU_CONTROL_AND_CONFIG_L2_PWDDN;
187
188         /* request power down */
189         reg |= PMSU_CONTROL_AND_CONFIG_PWDDN_REQ;
190         writel(reg, pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
191
192         /* Disable snoop disable by HW - SW is taking care of it */
193         reg = readl(pmsu_mp_base + PMSU_CPU_POWER_DOWN_CONTROL(hw_cpu));
194         reg |= PMSU_CPU_POWER_DOWN_DIS_SNP_Q_SKIP;
195         writel(reg, pmsu_mp_base + PMSU_CPU_POWER_DOWN_CONTROL(hw_cpu));
196
197         v7_exit_coherency_flush(all);
198
199         ll_disable_coherency();
200
201         dsb();
202
203         wfi();
204
205         /* If we are here, wfi failed. As processors run out of
206          * coherency for some time, tlbs might be stale, so flush them
207          */
208         local_flush_tlb_all();
209
210         ll_enable_coherency();
211
212         /* Test the CR_C bit and set it if it was cleared */
213         asm volatile(
214         "mrc    p15, 0, %0, c1, c0, 0 \n\t"
215         "tst    %0, #(1 << 2) \n\t"
216         "orreq  %0, %0, #(1 << 2) \n\t"
217         "mcreq  p15, 0, %0, c1, c0, 0 \n\t"
218         "isb    "
219         : : "r" (0));
220
221         pr_warn("Failed to suspend the system\n");
222
223         return 0;
224 }
225
226 static int armada_370_xp_cpu_suspend(unsigned long deepidle)
227 {
228         return cpu_suspend(deepidle, armada_370_xp_pmsu_idle_enter);
229 }
230
231 /* No locking is needed because we only access per-CPU registers */
232 void armada_370_xp_pmsu_idle_exit(void)
233 {
234         unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
235         u32 reg;
236
237         if (pmsu_mp_base == NULL)
238                 return;
239
240         /* cancel ask HW to power down the L2 Cache if possible */
241         reg = readl(pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
242         reg &= ~PMSU_CONTROL_AND_CONFIG_L2_PWDDN;
243         writel(reg, pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(hw_cpu));
244
245         /* cancel Enable wakeup events and mask interrupts */
246         reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
247         reg &= ~(PMSU_STATUS_AND_MASK_IRQ_WAKEUP | PMSU_STATUS_AND_MASK_FIQ_WAKEUP);
248         reg &= ~PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT;
249         reg &= ~PMSU_STATUS_AND_MASK_SNP_Q_EMPTY_WAIT;
250         reg &= ~(PMSU_STATUS_AND_MASK_IRQ_MASK | PMSU_STATUS_AND_MASK_FIQ_MASK);
251         writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(hw_cpu));
252 }
253
254 static int armada_370_xp_cpu_pm_notify(struct notifier_block *self,
255                                     unsigned long action, void *hcpu)
256 {
257         if (action == CPU_PM_ENTER) {
258                 unsigned int hw_cpu = cpu_logical_map(smp_processor_id());
259                 mvebu_pmsu_set_cpu_boot_addr(hw_cpu, armada_370_xp_cpu_resume);
260         } else if (action == CPU_PM_EXIT) {
261                 armada_370_xp_pmsu_idle_exit();
262         }
263
264         return NOTIFY_OK;
265 }
266
267 static struct notifier_block armada_370_xp_cpu_pm_notifier = {
268         .notifier_call = armada_370_xp_cpu_pm_notify,
269 };
270
271 static int __init armada_370_xp_cpu_pm_init(void)
272 {
273         struct device_node *np;
274
275         /*
276          * Check that all the requirements are available to enable
277          * cpuidle. So far, it is only supported on Armada XP, cpuidle
278          * needs the coherency fabric and the PMSU enabled
279          */
280
281         if (!of_machine_is_compatible("marvell,armadaxp"))
282                 return 0;
283
284         np = of_find_compatible_node(NULL, NULL, "marvell,coherency-fabric");
285         if (!np)
286                 return 0;
287         of_node_put(np);
288
289         np = of_find_matching_node(NULL, of_pmsu_table);
290         if (!np)
291                 return 0;
292         of_node_put(np);
293
294         armada_370_xp_pmsu_enable_l2_powerdown_onidle();
295         armada_xp_cpuidle_device.dev.platform_data = armada_370_xp_cpu_suspend;
296         platform_device_register(&armada_xp_cpuidle_device);
297         cpu_pm_register_notifier(&armada_370_xp_cpu_pm_notifier);
298
299         return 0;
300 }
301
302 arch_initcall(armada_370_xp_cpu_pm_init);
303 early_initcall(armada_370_xp_pmsu_init);
304
305 static void mvebu_pmsu_dfs_request_local(void *data)
306 {
307         u32 reg;
308         u32 cpu = smp_processor_id();
309         unsigned long flags;
310
311         local_irq_save(flags);
312
313         /* Prepare to enter idle */
314         reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(cpu));
315         reg |= PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT |
316                PMSU_STATUS_AND_MASK_IRQ_MASK     |
317                PMSU_STATUS_AND_MASK_FIQ_MASK;
318         writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(cpu));
319
320         /* Request the DFS transition */
321         reg = readl(pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(cpu));
322         reg |= PMSU_CONTROL_AND_CONFIG_DFS_REQ;
323         writel(reg, pmsu_mp_base + PMSU_CONTROL_AND_CONFIG(cpu));
324
325         /* The fact of entering idle will trigger the DFS transition */
326         wfi();
327
328         /*
329          * We're back from idle, the DFS transition has completed,
330          * clear the idle wait indication.
331          */
332         reg = readl(pmsu_mp_base + PMSU_STATUS_AND_MASK(cpu));
333         reg &= ~PMSU_STATUS_AND_MASK_CPU_IDLE_WAIT;
334         writel(reg, pmsu_mp_base + PMSU_STATUS_AND_MASK(cpu));
335
336         local_irq_restore(flags);
337 }
338
339 int mvebu_pmsu_dfs_request(int cpu)
340 {
341         unsigned long timeout;
342         int hwcpu = cpu_logical_map(cpu);
343         u32 reg;
344
345         /* Clear any previous DFS DONE event */
346         reg = readl(pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
347         reg &= ~PMSU_EVENT_STATUS_AND_MASK_DFS_DONE;
348         writel(reg, pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
349
350         /* Mask the DFS done interrupt, since we are going to poll */
351         reg = readl(pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
352         reg |= PMSU_EVENT_STATUS_AND_MASK_DFS_DONE_MASK;
353         writel(reg, pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
354
355         /* Trigger the DFS on the appropriate CPU */
356         smp_call_function_single(cpu, mvebu_pmsu_dfs_request_local,
357                                  NULL, false);
358
359         /* Poll until the DFS done event is generated */
360         timeout = jiffies + HZ;
361         while (time_before(jiffies, timeout)) {
362                 reg = readl(pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
363                 if (reg & PMSU_EVENT_STATUS_AND_MASK_DFS_DONE)
364                         break;
365                 udelay(10);
366         }
367
368         if (time_after(jiffies, timeout))
369                 return -ETIME;
370
371         /* Restore the DFS mask to its original state */
372         reg = readl(pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
373         reg &= ~PMSU_EVENT_STATUS_AND_MASK_DFS_DONE_MASK;
374         writel(reg, pmsu_mp_base + PMSU_EVENT_STATUS_AND_MASK(hwcpu));
375
376         return 0;
377 }
378
379 static int __init armada_xp_pmsu_cpufreq_init(void)
380 {
381         struct device_node *np;
382         struct resource res;
383         int ret, cpu;
384
385         if (!of_machine_is_compatible("marvell,armadaxp"))
386                 return 0;
387
388         /*
389          * In order to have proper cpufreq handling, we need to ensure
390          * that the Device Tree description of the CPU clock includes
391          * the definition of the PMU DFS registers. If not, we do not
392          * register the clock notifier and the cpufreq driver. This
393          * piece of code is only for compatibility with old Device
394          * Trees.
395          */
396         np = of_find_compatible_node(NULL, NULL, "marvell,armada-xp-cpu-clock");
397         if (!np)
398                 return 0;
399
400         ret = of_address_to_resource(np, 1, &res);
401         if (ret) {
402                 pr_warn(FW_WARN "not enabling cpufreq, deprecated armada-xp-cpu-clock binding\n");
403                 of_node_put(np);
404                 return 0;
405         }
406
407         of_node_put(np);
408
409         /*
410          * For each CPU, this loop registers the operating points
411          * supported (which are the nominal CPU frequency and half of
412          * it), and registers the clock notifier that will take care
413          * of doing the PMSU part of a frequency transition.
414          */
415         for_each_possible_cpu(cpu) {
416                 struct device *cpu_dev;
417                 struct clk *clk;
418                 int ret;
419
420                 cpu_dev = get_cpu_device(cpu);
421                 if (!cpu_dev) {
422                         pr_err("Cannot get CPU %d\n", cpu);
423                         continue;
424                 }
425
426                 clk = clk_get(cpu_dev, 0);
427                 if (!clk) {
428                         pr_err("Cannot get clock for CPU %d\n", cpu);
429                         return -ENODEV;
430                 }
431
432                 /*
433                  * In case of a failure of dev_pm_opp_add(), we don't
434                  * bother with cleaning up the registered OPP (there's
435                  * no function to do so), and simply cancel the
436                  * registration of the cpufreq device.
437                  */
438                 ret = dev_pm_opp_add(cpu_dev, clk_get_rate(clk), 0);
439                 if (ret) {
440                         clk_put(clk);
441                         return ret;
442                 }
443
444                 ret = dev_pm_opp_add(cpu_dev, clk_get_rate(clk) / 2, 0);
445                 if (ret) {
446                         clk_put(clk);
447                         return ret;
448                 }
449         }
450
451         platform_device_register_simple("cpufreq-generic", -1, NULL, 0);
452         return 0;
453 }
454
455 device_initcall(armada_xp_pmsu_cpufreq_init);