Uwe Kleine-König | 292ec08 | 2013-06-26 09:18:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/irq/irq-nvic.c |
| 3 | * |
| 4 | * Copyright (C) 2008 ARM Limited, All Rights Reserved. |
| 5 | * Copyright (C) 2013 Pengutronix |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * Support for the Nested Vectored Interrupt Controller found on the |
| 12 | * ARMv7-M CPUs (Cortex-M3/M4) |
| 13 | */ |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/of_address.h> |
| 23 | #include <linux/irq.h> |
Joel Porquet | 41a83e0 | 2015-07-07 17:11:46 -0400 | [diff] [blame] | 24 | #include <linux/irqchip.h> |
Uwe Kleine-König | 292ec08 | 2013-06-26 09:18:48 +0200 | [diff] [blame] | 25 | #include <linux/irqdomain.h> |
| 26 | |
| 27 | #include <asm/v7m.h> |
| 28 | #include <asm/exception.h> |
| 29 | |
Uwe Kleine-König | 292ec08 | 2013-06-26 09:18:48 +0200 | [diff] [blame] | 30 | #define NVIC_ISER 0x000 |
| 31 | #define NVIC_ICER 0x080 |
Vladimir Murzin | 0063186 | 2021-12-01 11:02:58 +0000 | [diff] [blame] | 32 | #define NVIC_IPR 0x400 |
Uwe Kleine-König | 292ec08 | 2013-06-26 09:18:48 +0200 | [diff] [blame] | 33 | |
| 34 | #define NVIC_MAX_BANKS 16 |
| 35 | /* |
| 36 | * Each bank handles 32 irqs. Only the 16th (= last) bank handles only |
| 37 | * 16 irqs. |
| 38 | */ |
| 39 | #define NVIC_MAX_IRQ ((NVIC_MAX_BANKS - 1) * 32 + 16) |
| 40 | |
| 41 | static struct irq_domain *nvic_irq_domain; |
| 42 | |
| 43 | asmlinkage void __exception_irq_entry |
| 44 | nvic_handle_irq(irq_hw_number_t hwirq, struct pt_regs *regs) |
| 45 | { |
| 46 | unsigned int irq = irq_linear_revmap(nvic_irq_domain, hwirq); |
| 47 | |
| 48 | handle_IRQ(irq, regs); |
| 49 | } |
| 50 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 51 | static int nvic_irq_domain_translate(struct irq_domain *d, |
| 52 | struct irq_fwspec *fwspec, |
| 53 | unsigned long *hwirq, unsigned int *type) |
| 54 | { |
| 55 | if (WARN_ON(fwspec->param_count < 1)) |
| 56 | return -EINVAL; |
| 57 | *hwirq = fwspec->param[0]; |
| 58 | *type = IRQ_TYPE_NONE; |
| 59 | return 0; |
| 60 | } |
| 61 | |
Stefan Agner | 2d9f59f | 2015-05-16 11:44:16 +0200 | [diff] [blame] | 62 | static int nvic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 63 | unsigned int nr_irqs, void *arg) |
| 64 | { |
| 65 | int i, ret; |
| 66 | irq_hw_number_t hwirq; |
| 67 | unsigned int type = IRQ_TYPE_NONE; |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 68 | struct irq_fwspec *fwspec = arg; |
Stefan Agner | 2d9f59f | 2015-05-16 11:44:16 +0200 | [diff] [blame] | 69 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 70 | ret = nvic_irq_domain_translate(domain, fwspec, &hwirq, &type); |
Stefan Agner | 2d9f59f | 2015-05-16 11:44:16 +0200 | [diff] [blame] | 71 | if (ret) |
| 72 | return ret; |
| 73 | |
| 74 | for (i = 0; i < nr_irqs; i++) |
| 75 | irq_map_generic_chip(domain, virq + i, hwirq + i); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static const struct irq_domain_ops nvic_irq_domain_ops = { |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 81 | .translate = nvic_irq_domain_translate, |
Stefan Agner | 2d9f59f | 2015-05-16 11:44:16 +0200 | [diff] [blame] | 82 | .alloc = nvic_irq_domain_alloc, |
| 83 | .free = irq_domain_free_irqs_top, |
| 84 | }; |
| 85 | |
Uwe Kleine-König | 292ec08 | 2013-06-26 09:18:48 +0200 | [diff] [blame] | 86 | static int __init nvic_of_init(struct device_node *node, |
| 87 | struct device_node *parent) |
| 88 | { |
| 89 | unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN; |
| 90 | unsigned int irqs, i, ret, numbanks; |
| 91 | void __iomem *nvic_base; |
| 92 | |
| 93 | numbanks = (readl_relaxed(V7M_SCS_ICTR) & |
| 94 | V7M_SCS_ICTR_INTLINESNUM_MASK) + 1; |
| 95 | |
| 96 | nvic_base = of_iomap(node, 0); |
| 97 | if (!nvic_base) { |
| 98 | pr_warn("unable to map nvic registers\n"); |
| 99 | return -ENOMEM; |
| 100 | } |
| 101 | |
| 102 | irqs = numbanks * 32; |
| 103 | if (irqs > NVIC_MAX_IRQ) |
| 104 | irqs = NVIC_MAX_IRQ; |
| 105 | |
| 106 | nvic_irq_domain = |
Stefan Agner | 2d9f59f | 2015-05-16 11:44:16 +0200 | [diff] [blame] | 107 | irq_domain_add_linear(node, irqs, &nvic_irq_domain_ops, NULL); |
| 108 | |
Uwe Kleine-König | 292ec08 | 2013-06-26 09:18:48 +0200 | [diff] [blame] | 109 | if (!nvic_irq_domain) { |
| 110 | pr_warn("Failed to allocate irq domain\n"); |
Souptick Joarder (HPE) | 347e27a | 2022-02-18 22:03:03 +0530 | [diff] [blame] | 111 | iounmap(nvic_base); |
Uwe Kleine-König | 292ec08 | 2013-06-26 09:18:48 +0200 | [diff] [blame] | 112 | return -ENOMEM; |
| 113 | } |
| 114 | |
Axel Lin | 5b8aae4 | 2013-07-05 15:39:11 +0800 | [diff] [blame] | 115 | ret = irq_alloc_domain_generic_chips(nvic_irq_domain, 32, 1, |
Uwe Kleine-König | 292ec08 | 2013-06-26 09:18:48 +0200 | [diff] [blame] | 116 | "nvic_irq", handle_fasteoi_irq, |
| 117 | clr, 0, IRQ_GC_INIT_MASK_CACHE); |
| 118 | if (ret) { |
| 119 | pr_warn("Failed to allocate irq chips\n"); |
| 120 | irq_domain_remove(nvic_irq_domain); |
Souptick Joarder (HPE) | 347e27a | 2022-02-18 22:03:03 +0530 | [diff] [blame] | 121 | iounmap(nvic_base); |
Uwe Kleine-König | 292ec08 | 2013-06-26 09:18:48 +0200 | [diff] [blame] | 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | for (i = 0; i < numbanks; ++i) { |
| 126 | struct irq_chip_generic *gc; |
| 127 | |
| 128 | gc = irq_get_domain_generic_chip(nvic_irq_domain, 32 * i); |
| 129 | gc->reg_base = nvic_base + 4 * i; |
| 130 | gc->chip_types[0].regs.enable = NVIC_ISER; |
| 131 | gc->chip_types[0].regs.disable = NVIC_ICER; |
| 132 | gc->chip_types[0].chip.irq_mask = irq_gc_mask_disable_reg; |
| 133 | gc->chip_types[0].chip.irq_unmask = irq_gc_unmask_enable_reg; |
Daniel Thompson | 8b53ec2 | 2014-06-04 16:01:52 +0100 | [diff] [blame] | 134 | /* This is a no-op as end of interrupt is signaled by the |
| 135 | * exception return sequence. |
| 136 | */ |
| 137 | gc->chip_types[0].chip.irq_eoi = irq_gc_noop; |
Uwe Kleine-König | 292ec08 | 2013-06-26 09:18:48 +0200 | [diff] [blame] | 138 | |
| 139 | /* disable interrupts */ |
| 140 | writel_relaxed(~0, gc->reg_base + NVIC_ICER); |
| 141 | } |
| 142 | |
| 143 | /* Set priority on all interrupts */ |
| 144 | for (i = 0; i < irqs; i += 4) |
| 145 | writel_relaxed(0, nvic_base + NVIC_IPR + i); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | IRQCHIP_DECLARE(armv7m_nvic, "arm,armv7m-nvic", nvic_of_init); |