Guo Ren | d8a5f5f | 2018-09-16 15:57:14 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd. |
| 3 | |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/init.h> |
| 6 | #include <linux/of.h> |
| 7 | #include <linux/of_address.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/irqdomain.h> |
| 10 | #include <linux/irqchip.h> |
| 11 | #include <linux/irq.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/smp.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <asm/irq.h> |
| 16 | #include <asm/traps.h> |
| 17 | #include <asm/reg_ops.h> |
| 18 | |
| 19 | static struct irq_domain *root_domain; |
| 20 | static void __iomem *INTCG_base; |
| 21 | static void __iomem *INTCL_base; |
| 22 | |
| 23 | #define IPI_IRQ 15 |
| 24 | #define INTC_IRQS 256 |
| 25 | #define COMM_IRQ_BASE 32 |
| 26 | |
| 27 | #define INTCG_SIZE 0x8000 |
| 28 | #define INTCL_SIZE 0x1000 |
| 29 | |
| 30 | #define INTCG_ICTLR 0x0 |
| 31 | #define INTCG_CICFGR 0x100 |
| 32 | #define INTCG_CIDSTR 0x1000 |
| 33 | |
| 34 | #define INTCL_PICTLR 0x0 |
Guo Ren | 648f835 | 2019-06-06 15:37:31 +0800 | [diff] [blame^] | 35 | #define INTCL_CFGR 0x14 |
Guo Ren | d8a5f5f | 2018-09-16 15:57:14 +0800 | [diff] [blame] | 36 | #define INTCL_SIGR 0x60 |
| 37 | #define INTCL_HPPIR 0x68 |
| 38 | #define INTCL_RDYIR 0x6c |
| 39 | #define INTCL_SENR 0xa0 |
| 40 | #define INTCL_CENR 0xa4 |
| 41 | #define INTCL_CACR 0xb4 |
| 42 | |
| 43 | static DEFINE_PER_CPU(void __iomem *, intcl_reg); |
| 44 | |
Guo Ren | 648f835 | 2019-06-06 15:37:31 +0800 | [diff] [blame^] | 45 | static unsigned long *__trigger; |
| 46 | |
| 47 | #define IRQ_OFFSET(irq) ((irq < COMM_IRQ_BASE) ? irq : (irq - COMM_IRQ_BASE)) |
| 48 | |
| 49 | #define TRIG_BYTE_OFFSET(i) ((((i) * 2) / 32) * 4) |
| 50 | #define TRIG_BIT_OFFSET(i) (((i) * 2) % 32) |
| 51 | |
| 52 | #define TRIG_VAL(trigger, irq) (trigger << TRIG_BIT_OFFSET(IRQ_OFFSET(irq))) |
| 53 | #define TRIG_VAL_MSK(irq) (~(3 << TRIG_BIT_OFFSET(IRQ_OFFSET(irq)))) |
| 54 | |
| 55 | #define TRIG_BASE(irq) \ |
| 56 | (TRIG_BYTE_OFFSET(IRQ_OFFSET(irq)) + ((irq < COMM_IRQ_BASE) ? \ |
| 57 | (this_cpu_read(intcl_reg) + INTCL_CFGR) : (INTCG_base + INTCG_CICFGR))) |
| 58 | |
| 59 | static DEFINE_SPINLOCK(setup_lock); |
| 60 | static void setup_trigger(unsigned long irq, unsigned long trigger) |
| 61 | { |
| 62 | unsigned int tmp; |
| 63 | |
| 64 | spin_lock(&setup_lock); |
| 65 | |
| 66 | /* setup trigger */ |
| 67 | tmp = readl_relaxed(TRIG_BASE(irq)) & TRIG_VAL_MSK(irq); |
| 68 | |
| 69 | writel_relaxed(tmp | TRIG_VAL(trigger, irq), TRIG_BASE(irq)); |
| 70 | |
| 71 | spin_unlock(&setup_lock); |
| 72 | } |
| 73 | |
Guo Ren | d8a5f5f | 2018-09-16 15:57:14 +0800 | [diff] [blame] | 74 | static void csky_mpintc_handler(struct pt_regs *regs) |
| 75 | { |
| 76 | void __iomem *reg_base = this_cpu_read(intcl_reg); |
| 77 | |
| 78 | do { |
| 79 | handle_domain_irq(root_domain, |
| 80 | readl_relaxed(reg_base + INTCL_RDYIR), |
| 81 | regs); |
| 82 | } while (readl_relaxed(reg_base + INTCL_HPPIR) & BIT(31)); |
| 83 | } |
| 84 | |
| 85 | static void csky_mpintc_enable(struct irq_data *d) |
| 86 | { |
| 87 | void __iomem *reg_base = this_cpu_read(intcl_reg); |
| 88 | |
Guo Ren | 648f835 | 2019-06-06 15:37:31 +0800 | [diff] [blame^] | 89 | setup_trigger(d->hwirq, __trigger[d->hwirq]); |
| 90 | |
Guo Ren | d8a5f5f | 2018-09-16 15:57:14 +0800 | [diff] [blame] | 91 | writel_relaxed(d->hwirq, reg_base + INTCL_SENR); |
| 92 | } |
| 93 | |
| 94 | static void csky_mpintc_disable(struct irq_data *d) |
| 95 | { |
| 96 | void __iomem *reg_base = this_cpu_read(intcl_reg); |
| 97 | |
| 98 | writel_relaxed(d->hwirq, reg_base + INTCL_CENR); |
| 99 | } |
| 100 | |
| 101 | static void csky_mpintc_eoi(struct irq_data *d) |
| 102 | { |
| 103 | void __iomem *reg_base = this_cpu_read(intcl_reg); |
| 104 | |
| 105 | writel_relaxed(d->hwirq, reg_base + INTCL_CACR); |
| 106 | } |
| 107 | |
Guo Ren | 648f835 | 2019-06-06 15:37:31 +0800 | [diff] [blame^] | 108 | static int csky_mpintc_set_type(struct irq_data *d, unsigned int type) |
| 109 | { |
| 110 | switch (type & IRQ_TYPE_SENSE_MASK) { |
| 111 | case IRQ_TYPE_LEVEL_HIGH: |
| 112 | __trigger[d->hwirq] = 0; |
| 113 | break; |
| 114 | case IRQ_TYPE_LEVEL_LOW: |
| 115 | __trigger[d->hwirq] = 1; |
| 116 | break; |
| 117 | case IRQ_TYPE_EDGE_RISING: |
| 118 | __trigger[d->hwirq] = 2; |
| 119 | break; |
| 120 | case IRQ_TYPE_EDGE_FALLING: |
| 121 | __trigger[d->hwirq] = 3; |
| 122 | break; |
| 123 | default: |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Guo Ren | d8a5f5f | 2018-09-16 15:57:14 +0800 | [diff] [blame] | 130 | #ifdef CONFIG_SMP |
| 131 | static int csky_irq_set_affinity(struct irq_data *d, |
| 132 | const struct cpumask *mask_val, |
| 133 | bool force) |
| 134 | { |
| 135 | unsigned int cpu; |
| 136 | unsigned int offset = 4 * (d->hwirq - COMM_IRQ_BASE); |
| 137 | |
| 138 | if (!force) |
| 139 | cpu = cpumask_any_and(mask_val, cpu_online_mask); |
| 140 | else |
| 141 | cpu = cpumask_first(mask_val); |
| 142 | |
| 143 | if (cpu >= nr_cpu_ids) |
| 144 | return -EINVAL; |
| 145 | |
| 146 | /* Enable interrupt destination */ |
| 147 | cpu |= BIT(31); |
| 148 | |
| 149 | writel_relaxed(cpu, INTCG_base + INTCG_CIDSTR + offset); |
| 150 | |
| 151 | irq_data_update_effective_affinity(d, cpumask_of(cpu)); |
| 152 | |
| 153 | return IRQ_SET_MASK_OK_DONE; |
| 154 | } |
| 155 | #endif |
| 156 | |
| 157 | static struct irq_chip csky_irq_chip = { |
| 158 | .name = "C-SKY SMP Intc", |
| 159 | .irq_eoi = csky_mpintc_eoi, |
| 160 | .irq_enable = csky_mpintc_enable, |
| 161 | .irq_disable = csky_mpintc_disable, |
Guo Ren | 648f835 | 2019-06-06 15:37:31 +0800 | [diff] [blame^] | 162 | .irq_set_type = csky_mpintc_set_type, |
Guo Ren | d8a5f5f | 2018-09-16 15:57:14 +0800 | [diff] [blame] | 163 | #ifdef CONFIG_SMP |
| 164 | .irq_set_affinity = csky_irq_set_affinity, |
| 165 | #endif |
| 166 | }; |
| 167 | |
| 168 | static int csky_irqdomain_map(struct irq_domain *d, unsigned int irq, |
| 169 | irq_hw_number_t hwirq) |
| 170 | { |
| 171 | if (hwirq < COMM_IRQ_BASE) { |
| 172 | irq_set_percpu_devid(irq); |
| 173 | irq_set_chip_and_handler(irq, &csky_irq_chip, |
| 174 | handle_percpu_irq); |
| 175 | } else { |
| 176 | irq_set_chip_and_handler(irq, &csky_irq_chip, |
| 177 | handle_fasteoi_irq); |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Guo Ren | 648f835 | 2019-06-06 15:37:31 +0800 | [diff] [blame^] | 183 | static int csky_irq_domain_xlate_cells(struct irq_domain *d, |
| 184 | struct device_node *ctrlr, const u32 *intspec, |
| 185 | unsigned int intsize, unsigned long *out_hwirq, |
| 186 | unsigned int *out_type) |
| 187 | { |
| 188 | if (WARN_ON(intsize < 1)) |
| 189 | return -EINVAL; |
| 190 | |
| 191 | *out_hwirq = intspec[0]; |
| 192 | if (intsize > 1) |
| 193 | *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK; |
| 194 | else |
| 195 | *out_type = IRQ_TYPE_LEVEL_HIGH; |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
Guo Ren | d8a5f5f | 2018-09-16 15:57:14 +0800 | [diff] [blame] | 200 | static const struct irq_domain_ops csky_irqdomain_ops = { |
| 201 | .map = csky_irqdomain_map, |
Guo Ren | 648f835 | 2019-06-06 15:37:31 +0800 | [diff] [blame^] | 202 | .xlate = csky_irq_domain_xlate_cells, |
Guo Ren | d8a5f5f | 2018-09-16 15:57:14 +0800 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | #ifdef CONFIG_SMP |
| 206 | static void csky_mpintc_send_ipi(const struct cpumask *mask) |
| 207 | { |
| 208 | void __iomem *reg_base = this_cpu_read(intcl_reg); |
| 209 | |
| 210 | /* |
| 211 | * INTCL_SIGR[3:0] INTID |
| 212 | * INTCL_SIGR[8:15] CPUMASK |
| 213 | */ |
| 214 | writel_relaxed((*cpumask_bits(mask)) << 8 | IPI_IRQ, |
| 215 | reg_base + INTCL_SIGR); |
| 216 | } |
| 217 | #endif |
| 218 | |
| 219 | /* C-SKY multi processor interrupt controller */ |
| 220 | static int __init |
| 221 | csky_mpintc_init(struct device_node *node, struct device_node *parent) |
| 222 | { |
| 223 | int ret; |
| 224 | unsigned int cpu, nr_irq; |
| 225 | #ifdef CONFIG_SMP |
| 226 | unsigned int ipi_irq; |
| 227 | #endif |
| 228 | |
| 229 | if (parent) |
| 230 | return 0; |
| 231 | |
| 232 | ret = of_property_read_u32(node, "csky,num-irqs", &nr_irq); |
| 233 | if (ret < 0) |
| 234 | nr_irq = INTC_IRQS; |
| 235 | |
Guo Ren | 648f835 | 2019-06-06 15:37:31 +0800 | [diff] [blame^] | 236 | __trigger = kcalloc(nr_irq, sizeof(unsigned long), GFP_KERNEL); |
| 237 | if (__trigger == NULL) |
| 238 | return -ENXIO; |
| 239 | |
Guo Ren | d8a5f5f | 2018-09-16 15:57:14 +0800 | [diff] [blame] | 240 | if (INTCG_base == NULL) { |
| 241 | INTCG_base = ioremap(mfcr("cr<31, 14>"), |
| 242 | INTCL_SIZE*nr_cpu_ids + INTCG_SIZE); |
| 243 | if (INTCG_base == NULL) |
| 244 | return -EIO; |
| 245 | |
| 246 | INTCL_base = INTCG_base + INTCG_SIZE; |
| 247 | |
| 248 | writel_relaxed(BIT(0), INTCG_base + INTCG_ICTLR); |
| 249 | } |
| 250 | |
| 251 | root_domain = irq_domain_add_linear(node, nr_irq, &csky_irqdomain_ops, |
| 252 | NULL); |
| 253 | if (!root_domain) |
| 254 | return -ENXIO; |
| 255 | |
| 256 | /* for every cpu */ |
| 257 | for_each_present_cpu(cpu) { |
| 258 | per_cpu(intcl_reg, cpu) = INTCL_base + (INTCL_SIZE * cpu); |
| 259 | writel_relaxed(BIT(0), per_cpu(intcl_reg, cpu) + INTCL_PICTLR); |
| 260 | } |
| 261 | |
| 262 | set_handle_irq(&csky_mpintc_handler); |
| 263 | |
| 264 | #ifdef CONFIG_SMP |
| 265 | ipi_irq = irq_create_mapping(root_domain, IPI_IRQ); |
| 266 | if (!ipi_irq) |
| 267 | return -EIO; |
| 268 | |
| 269 | set_send_ipi(&csky_mpintc_send_ipi, ipi_irq); |
| 270 | #endif |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | IRQCHIP_DECLARE(csky_mpintc, "csky,mpintc", csky_mpintc_init); |