2 * OMAP WakeupGen Source file
4 * OMAP WakeupGen is the interrupt controller extension used along
5 * with ARM GIC to wake the CPU out from low power states on
6 * external interrupts. It is responsible for generating wakeup
7 * event from the incoming interrupts and enable bits. It is
8 * implemented in MPU always ON power domain. During normal operation,
9 * WakeupGen delivers external interrupts directly to the GIC.
11 * Copyright (C) 2011 Texas Instruments, Inc.
12 * Santosh Shilimkar <santosh.shilimkar@ti.com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
22 #include <linux/irq.h>
23 #include <linux/irqchip.h>
24 #include <linux/irqdomain.h>
25 #include <linux/of_address.h>
26 #include <linux/platform_device.h>
27 #include <linux/cpu.h>
28 #include <linux/notifier.h>
29 #include <linux/cpu_pm.h>
31 #include "omap-wakeupgen.h"
32 #include "omap-secure.h"
35 #include "omap4-sar-layout.h"
39 #define AM43XX_NR_REG_BANKS 7
40 #define AM43XX_IRQS 224
41 #define MAX_NR_REG_BANKS AM43XX_NR_REG_BANKS
42 #define MAX_IRQS AM43XX_IRQS
43 #define DEFAULT_NR_REG_BANKS 5
44 #define DEFAULT_IRQS 160
45 #define WKG_MASK_ALL 0x00000000
46 #define WKG_UNMASK_ALL 0xffffffff
47 #define CPU_ENA_OFFSET 0x400
50 #define OMAP4_NR_BANKS 4
51 #define OMAP4_NR_IRQS 128
53 #define SYS_NIRQ1_EXT_SYS_IRQ_1 7
54 #define SYS_NIRQ2_EXT_SYS_IRQ_2 119
56 static void __iomem *wakeupgen_base;
57 static void __iomem *sar_base;
58 static DEFINE_RAW_SPINLOCK(wakeupgen_lock);
59 static unsigned int irq_target_cpu[MAX_IRQS];
60 static unsigned int irq_banks = DEFAULT_NR_REG_BANKS;
61 static unsigned int max_irqs = DEFAULT_IRQS;
62 static unsigned int omap_secure_apis;
65 static unsigned int wakeupgen_context[MAX_NR_REG_BANKS];
68 struct omap_wakeupgen_ops {
69 void (*save_context)(void);
70 void (*restore_context)(void);
73 static struct omap_wakeupgen_ops *wakeupgen_ops;
76 * Static helper functions.
78 static inline u32 wakeupgen_readl(u8 idx, u32 cpu)
80 return readl_relaxed(wakeupgen_base + OMAP_WKG_ENB_A_0 +
81 (cpu * CPU_ENA_OFFSET) + (idx * 4));
84 static inline void wakeupgen_writel(u32 val, u8 idx, u32 cpu)
86 writel_relaxed(val, wakeupgen_base + OMAP_WKG_ENB_A_0 +
87 (cpu * CPU_ENA_OFFSET) + (idx * 4));
90 static inline void sar_writel(u32 val, u32 offset, u8 idx)
92 writel_relaxed(val, sar_base + offset + (idx * 4));
95 static inline int _wakeupgen_get_irq_info(u32 irq, u32 *bit_posn, u8 *reg_index)
98 * Each WakeupGen register controls 32 interrupt.
99 * i.e. 1 bit per SPI IRQ
101 *reg_index = irq >> 5;
102 *bit_posn = irq %= 32;
107 static void _wakeupgen_clear(unsigned int irq, unsigned int cpu)
112 if (_wakeupgen_get_irq_info(irq, &bit_number, &i))
115 val = wakeupgen_readl(i, cpu);
116 val &= ~BIT(bit_number);
117 wakeupgen_writel(val, i, cpu);
120 static void _wakeupgen_set(unsigned int irq, unsigned int cpu)
125 if (_wakeupgen_get_irq_info(irq, &bit_number, &i))
128 val = wakeupgen_readl(i, cpu);
129 val |= BIT(bit_number);
130 wakeupgen_writel(val, i, cpu);
134 * Architecture specific Mask extension
136 static void wakeupgen_mask(struct irq_data *d)
140 raw_spin_lock_irqsave(&wakeupgen_lock, flags);
141 _wakeupgen_clear(d->hwirq, irq_target_cpu[d->hwirq]);
142 raw_spin_unlock_irqrestore(&wakeupgen_lock, flags);
143 irq_chip_mask_parent(d);
147 * Architecture specific Unmask extension
149 static void wakeupgen_unmask(struct irq_data *d)
153 raw_spin_lock_irqsave(&wakeupgen_lock, flags);
154 _wakeupgen_set(d->hwirq, irq_target_cpu[d->hwirq]);
155 raw_spin_unlock_irqrestore(&wakeupgen_lock, flags);
156 irq_chip_unmask_parent(d);
160 * The sys_nirq pins bypass peripheral modules and are wired directly
161 * to MPUSS wakeupgen. They get automatically inverted for GIC.
163 static int wakeupgen_irq_set_type(struct irq_data *d, unsigned int type)
165 bool inverted = false;
168 case IRQ_TYPE_LEVEL_LOW:
169 type &= ~IRQ_TYPE_LEVEL_MASK;
170 type |= IRQ_TYPE_LEVEL_HIGH;
173 case IRQ_TYPE_EDGE_FALLING:
174 type &= ~IRQ_TYPE_EDGE_BOTH;
175 type |= IRQ_TYPE_EDGE_RISING;
182 if (inverted && d->hwirq != SYS_NIRQ1_EXT_SYS_IRQ_1 &&
183 d->hwirq != SYS_NIRQ2_EXT_SYS_IRQ_2)
184 pr_warn("wakeupgen: irq%li polarity inverted in dts\n",
187 return irq_chip_set_type_parent(d, type);
190 #ifdef CONFIG_HOTPLUG_CPU
191 static DEFINE_PER_CPU(u32 [MAX_NR_REG_BANKS], irqmasks);
193 static void _wakeupgen_save_masks(unsigned int cpu)
197 for (i = 0; i < irq_banks; i++)
198 per_cpu(irqmasks, cpu)[i] = wakeupgen_readl(i, cpu);
201 static void _wakeupgen_restore_masks(unsigned int cpu)
205 for (i = 0; i < irq_banks; i++)
206 wakeupgen_writel(per_cpu(irqmasks, cpu)[i], i, cpu);
209 static void _wakeupgen_set_all(unsigned int cpu, unsigned int reg)
213 for (i = 0; i < irq_banks; i++)
214 wakeupgen_writel(reg, i, cpu);
218 * Mask or unmask all interrupts on given CPU.
219 * 0 = Mask all interrupts on the 'cpu'
220 * 1 = Unmask all interrupts on the 'cpu'
221 * Ensure that the initial mask is maintained. This is faster than
222 * iterating through GIC registers to arrive at the correct masks.
224 static void wakeupgen_irqmask_all(unsigned int cpu, unsigned int set)
228 raw_spin_lock_irqsave(&wakeupgen_lock, flags);
230 _wakeupgen_save_masks(cpu);
231 _wakeupgen_set_all(cpu, WKG_MASK_ALL);
233 _wakeupgen_set_all(cpu, WKG_UNMASK_ALL);
234 _wakeupgen_restore_masks(cpu);
236 raw_spin_unlock_irqrestore(&wakeupgen_lock, flags);
241 static inline void omap4_irq_save_context(void)
245 if (omap_rev() == OMAP4430_REV_ES1_0)
248 for (i = 0; i < irq_banks; i++) {
249 /* Save the CPUx interrupt mask for IRQ 0 to 127 */
250 val = wakeupgen_readl(i, 0);
251 sar_writel(val, WAKEUPGENENB_OFFSET_CPU0, i);
252 val = wakeupgen_readl(i, 1);
253 sar_writel(val, WAKEUPGENENB_OFFSET_CPU1, i);
256 * Disable the secure interrupts for CPUx. The restore
257 * code blindly restores secure and non-secure interrupt
258 * masks from SAR RAM. Secure interrupts are not suppose
259 * to be enabled from HLOS. So overwrite the SAR location
260 * so that the secure interrupt remains disabled.
262 sar_writel(0x0, WAKEUPGENENB_SECURE_OFFSET_CPU0, i);
263 sar_writel(0x0, WAKEUPGENENB_SECURE_OFFSET_CPU1, i);
266 /* Save AuxBoot* registers */
267 val = readl_relaxed(wakeupgen_base + OMAP_AUX_CORE_BOOT_0);
268 writel_relaxed(val, sar_base + AUXCOREBOOT0_OFFSET);
269 val = readl_relaxed(wakeupgen_base + OMAP_AUX_CORE_BOOT_1);
270 writel_relaxed(val, sar_base + AUXCOREBOOT1_OFFSET);
272 /* Save SyncReq generation logic */
273 val = readl_relaxed(wakeupgen_base + OMAP_PTMSYNCREQ_MASK);
274 writel_relaxed(val, sar_base + PTMSYNCREQ_MASK_OFFSET);
275 val = readl_relaxed(wakeupgen_base + OMAP_PTMSYNCREQ_EN);
276 writel_relaxed(val, sar_base + PTMSYNCREQ_EN_OFFSET);
278 /* Set the Backup Bit Mask status */
279 val = readl_relaxed(sar_base + SAR_BACKUP_STATUS_OFFSET);
280 val |= SAR_BACKUP_STATUS_WAKEUPGEN;
281 writel_relaxed(val, sar_base + SAR_BACKUP_STATUS_OFFSET);
285 static inline void omap5_irq_save_context(void)
289 for (i = 0; i < irq_banks; i++) {
290 /* Save the CPUx interrupt mask for IRQ 0 to 159 */
291 val = wakeupgen_readl(i, 0);
292 sar_writel(val, OMAP5_WAKEUPGENENB_OFFSET_CPU0, i);
293 val = wakeupgen_readl(i, 1);
294 sar_writel(val, OMAP5_WAKEUPGENENB_OFFSET_CPU1, i);
295 sar_writel(0x0, OMAP5_WAKEUPGENENB_SECURE_OFFSET_CPU0, i);
296 sar_writel(0x0, OMAP5_WAKEUPGENENB_SECURE_OFFSET_CPU1, i);
299 /* Save AuxBoot* registers */
300 val = readl_relaxed(wakeupgen_base + OMAP_AUX_CORE_BOOT_0);
301 writel_relaxed(val, sar_base + OMAP5_AUXCOREBOOT0_OFFSET);
302 val = readl_relaxed(wakeupgen_base + OMAP_AUX_CORE_BOOT_0);
303 writel_relaxed(val, sar_base + OMAP5_AUXCOREBOOT1_OFFSET);
305 /* Set the Backup Bit Mask status */
306 val = readl_relaxed(sar_base + OMAP5_SAR_BACKUP_STATUS_OFFSET);
307 val |= SAR_BACKUP_STATUS_WAKEUPGEN;
308 writel_relaxed(val, sar_base + OMAP5_SAR_BACKUP_STATUS_OFFSET);
312 static inline void am43xx_irq_save_context(void)
316 for (i = 0; i < irq_banks; i++) {
317 wakeupgen_context[i] = wakeupgen_readl(i, 0);
318 wakeupgen_writel(0, i, CPU0_ID);
323 * Save WakeupGen interrupt context in SAR BANK3. Restore is done by
324 * ROM code. WakeupGen IP is integrated along with GIC to manage the
325 * interrupt wakeups from CPU low power states. It manages
326 * masking/unmasking of Shared peripheral interrupts(SPI). So the
327 * interrupt enable/disable control should be in sync and consistent
328 * at WakeupGen and GIC so that interrupts are not lost.
330 static void irq_save_context(void)
332 /* DRA7 has no SAR to save */
336 if (wakeupgen_ops && wakeupgen_ops->save_context)
337 wakeupgen_ops->save_context();
341 * Clear WakeupGen SAR backup status.
343 static void irq_sar_clear(void)
346 u32 offset = SAR_BACKUP_STATUS_OFFSET;
347 /* DRA7 has no SAR to save */
351 if (soc_is_omap54xx())
352 offset = OMAP5_SAR_BACKUP_STATUS_OFFSET;
354 val = readl_relaxed(sar_base + offset);
355 val &= ~SAR_BACKUP_STATUS_WAKEUPGEN;
356 writel_relaxed(val, sar_base + offset);
359 static void am43xx_irq_restore_context(void)
363 for (i = 0; i < irq_banks; i++)
364 wakeupgen_writel(wakeupgen_context[i], i, CPU0_ID);
367 static void irq_restore_context(void)
369 if (wakeupgen_ops && wakeupgen_ops->restore_context)
370 wakeupgen_ops->restore_context();
374 * Save GIC and Wakeupgen interrupt context using secure API
375 * for HS/EMU devices.
377 static void irq_save_secure_context(void)
380 ret = omap_secure_dispatcher(OMAP4_HAL_SAVEGIC_INDEX,
383 if (ret != API_HAL_RET_VALUE_OK)
384 pr_err("GIC and Wakeupgen context save failed\n");
387 /* Define ops for context save and restore for each SoC */
388 static struct omap_wakeupgen_ops omap4_wakeupgen_ops = {
389 .save_context = omap4_irq_save_context,
390 .restore_context = irq_sar_clear,
393 static struct omap_wakeupgen_ops omap5_wakeupgen_ops = {
394 .save_context = omap5_irq_save_context,
395 .restore_context = irq_sar_clear,
398 static struct omap_wakeupgen_ops am43xx_wakeupgen_ops = {
399 .save_context = am43xx_irq_save_context,
400 .restore_context = am43xx_irq_restore_context,
403 static struct omap_wakeupgen_ops omap4_wakeupgen_ops = {};
404 static struct omap_wakeupgen_ops omap5_wakeupgen_ops = {};
405 static struct omap_wakeupgen_ops am43xx_wakeupgen_ops = {};
408 #ifdef CONFIG_HOTPLUG_CPU
409 static int omap_wakeupgen_cpu_online(unsigned int cpu)
411 wakeupgen_irqmask_all(cpu, 0);
415 static int omap_wakeupgen_cpu_dead(unsigned int cpu)
417 wakeupgen_irqmask_all(cpu, 1);
421 static void __init irq_hotplug_init(void)
423 cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "arm/omap-wake:online",
424 omap_wakeupgen_cpu_online, NULL);
425 cpuhp_setup_state_nocalls(CPUHP_ARM_OMAP_WAKE_DEAD,
426 "arm/omap-wake:dead", NULL,
427 omap_wakeupgen_cpu_dead);
430 static void __init irq_hotplug_init(void)
435 static int irq_notifier(struct notifier_block *self, unsigned long cmd, void *v)
438 case CPU_CLUSTER_PM_ENTER:
439 if (omap_type() == OMAP2_DEVICE_TYPE_GP)
442 irq_save_secure_context();
444 case CPU_CLUSTER_PM_EXIT:
445 if (omap_type() == OMAP2_DEVICE_TYPE_GP)
446 irq_restore_context();
452 static struct notifier_block irq_notifier_block = {
453 .notifier_call = irq_notifier,
456 static void __init irq_pm_init(void)
458 /* FIXME: Remove this when MPU OSWR support is added */
459 if (!IS_PM44XX_ERRATUM(PM_OMAP4_CPU_OSWR_DISABLE))
460 cpu_pm_register_notifier(&irq_notifier_block);
463 static void __init irq_pm_init(void)
467 void __iomem *omap_get_wakeupgen_base(void)
469 return wakeupgen_base;
472 int omap_secure_apis_support(void)
474 return omap_secure_apis;
477 static struct irq_chip wakeupgen_chip = {
479 .irq_eoi = irq_chip_eoi_parent,
480 .irq_mask = wakeupgen_mask,
481 .irq_unmask = wakeupgen_unmask,
482 .irq_retrigger = irq_chip_retrigger_hierarchy,
483 .irq_set_type = wakeupgen_irq_set_type,
484 .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND,
486 .irq_set_affinity = irq_chip_set_affinity_parent,
490 static int wakeupgen_domain_translate(struct irq_domain *d,
491 struct irq_fwspec *fwspec,
492 unsigned long *hwirq,
495 if (is_of_node(fwspec->fwnode)) {
496 if (fwspec->param_count != 3)
499 /* No PPI should point to this domain */
500 if (fwspec->param[0] != 0)
503 *hwirq = fwspec->param[1];
504 *type = fwspec->param[2];
511 static int wakeupgen_domain_alloc(struct irq_domain *domain,
513 unsigned int nr_irqs, void *data)
515 struct irq_fwspec *fwspec = data;
516 struct irq_fwspec parent_fwspec;
517 irq_hw_number_t hwirq;
520 if (fwspec->param_count != 3)
521 return -EINVAL; /* Not GIC compliant */
522 if (fwspec->param[0] != 0)
523 return -EINVAL; /* No PPI should point to this domain */
525 hwirq = fwspec->param[1];
526 if (hwirq >= MAX_IRQS)
527 return -EINVAL; /* Can't deal with this */
529 for (i = 0; i < nr_irqs; i++)
530 irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
531 &wakeupgen_chip, NULL);
533 parent_fwspec = *fwspec;
534 parent_fwspec.fwnode = domain->parent->fwnode;
535 return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
539 static const struct irq_domain_ops wakeupgen_domain_ops = {
540 .translate = wakeupgen_domain_translate,
541 .alloc = wakeupgen_domain_alloc,
542 .free = irq_domain_free_irqs_common,
546 * Initialise the wakeupgen module.
548 static int __init wakeupgen_init(struct device_node *node,
549 struct device_node *parent)
551 struct irq_domain *parent_domain, *domain;
553 unsigned int boot_cpu = smp_processor_id();
557 pr_err("%pOF: no parent, giving up\n", node);
561 parent_domain = irq_find_host(parent);
562 if (!parent_domain) {
563 pr_err("%pOF: unable to obtain parent domain\n", node);
566 /* Not supported on OMAP4 ES1.0 silicon */
567 if (omap_rev() == OMAP4430_REV_ES1_0) {
568 WARN(1, "WakeupGen: Not supported on OMAP4430 ES1.0\n");
572 /* Static mapping, never released */
573 wakeupgen_base = of_iomap(node, 0);
574 if (WARN_ON(!wakeupgen_base))
577 if (cpu_is_omap44xx()) {
578 irq_banks = OMAP4_NR_BANKS;
579 max_irqs = OMAP4_NR_IRQS;
580 omap_secure_apis = 1;
581 wakeupgen_ops = &omap4_wakeupgen_ops;
582 } else if (soc_is_omap54xx()) {
583 wakeupgen_ops = &omap5_wakeupgen_ops;
584 } else if (soc_is_am43xx()) {
585 irq_banks = AM43XX_NR_REG_BANKS;
586 max_irqs = AM43XX_IRQS;
587 wakeupgen_ops = &am43xx_wakeupgen_ops;
590 domain = irq_domain_add_hierarchy(parent_domain, 0, max_irqs,
591 node, &wakeupgen_domain_ops,
594 iounmap(wakeupgen_base);
598 /* Clear all IRQ bitmasks at wakeupGen level */
599 for (i = 0; i < irq_banks; i++) {
600 wakeupgen_writel(0, i, CPU0_ID);
601 if (!soc_is_am43xx())
602 wakeupgen_writel(0, i, CPU1_ID);
606 * FIXME: Add support to set_smp_affinity() once the core
607 * GIC code has necessary hooks in place.
610 /* Associate all the IRQs to boot CPU like GIC init does. */
611 for (i = 0; i < max_irqs; i++)
612 irq_target_cpu[i] = boot_cpu;
615 * Enables OMAP5 ES2 PM Mode using ES2_PM_MODE in AMBA_IF_MODE
616 * 0x0: ES1 behavior, CPU cores would enter and exit OFF mode together.
617 * 0x1: ES2 behavior, CPU cores are allowed to enter/exit OFF mode
619 * This needs to be set one time thanks to always ON domain.
621 * We do not support ES1 behavior anymore. OMAP5 is assumed to be
622 * ES2.0, and the same is applicable for DRA7.
624 if (soc_is_omap54xx() || soc_is_dra7xx()) {
625 val = __raw_readl(wakeupgen_base + OMAP_AMBA_IF_MODE);
627 omap_smc1(OMAP5_MON_AMBA_IF_INDEX, val);
633 sar_base = omap4_get_sar_ram_base();
637 IRQCHIP_DECLARE(ti_wakeupgen, "ti,omap4-wugen-mpu", wakeupgen_init);