1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020, Jiaxun Yang <jiaxun.yang@flygoat.com>
4 * Loongson Local IO Interrupt Controller support
7 #include <linux/errno.h>
8 #include <linux/init.h>
9 #include <linux/types.h>
10 #include <linux/interrupt.h>
11 #include <linux/ioport.h>
12 #include <linux/irqchip.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
16 #include <linux/smp.h>
17 #include <linux/irqchip/chained_irq.h>
22 #include <asm/loongson.h>
25 #define LIOINTC_CHIP_IRQ 32
26 #define LIOINTC_NUM_PARENT 4
27 #define LIOINTC_NUM_CORES 4
29 #define LIOINTC_INTC_CHIP_START 0x20
31 #define LIOINTC_REG_INTC_STATUS (LIOINTC_INTC_CHIP_START + 0x20)
32 #define LIOINTC_REG_INTC_EN_STATUS (LIOINTC_INTC_CHIP_START + 0x04)
33 #define LIOINTC_REG_INTC_ENABLE (LIOINTC_INTC_CHIP_START + 0x08)
34 #define LIOINTC_REG_INTC_DISABLE (LIOINTC_INTC_CHIP_START + 0x0c)
36 * LIOINTC_REG_INTC_POL register is only valid for Loongson-2K series, and
37 * Loongson-3 series behave as noops.
39 #define LIOINTC_REG_INTC_POL (LIOINTC_INTC_CHIP_START + 0x10)
40 #define LIOINTC_REG_INTC_EDGE (LIOINTC_INTC_CHIP_START + 0x14)
42 #define LIOINTC_SHIFT_INTx 4
44 #define LIOINTC_ERRATA_IRQ 10
46 #if defined(CONFIG_MIPS)
47 #define liointc_core_id get_ebase_cpunum()
49 #define liointc_core_id get_csr_cpuid()
52 struct liointc_handler_data {
53 struct liointc_priv *priv;
58 struct irq_chip_generic *gc;
59 struct liointc_handler_data handler[LIOINTC_NUM_PARENT];
60 void __iomem *core_isr[LIOINTC_NUM_CORES];
61 u8 map_cache[LIOINTC_CHIP_IRQ];
64 bool has_lpc_irq_errata;
67 struct fwnode_handle *liointc_handle;
69 static void liointc_chained_handle_irq(struct irq_desc *desc)
71 struct liointc_handler_data *handler = irq_desc_get_handler_data(desc);
72 struct irq_chip *chip = irq_desc_get_chip(desc);
73 struct irq_chip_generic *gc = handler->priv->gc;
74 int core = liointc_core_id % LIOINTC_NUM_CORES;
77 chained_irq_enter(chip, desc);
79 pending = readl(handler->priv->core_isr[core]);
82 /* Always blame LPC IRQ if we have that bug */
83 if (handler->priv->has_lpc_irq_errata &&
84 (handler->parent_int_map & gc->mask_cache &
85 BIT(LIOINTC_ERRATA_IRQ)))
86 pending = BIT(LIOINTC_ERRATA_IRQ);
92 int bit = __ffs(pending);
94 generic_handle_domain_irq(gc->domain, bit);
98 chained_irq_exit(chip, desc);
101 static void liointc_set_bit(struct irq_chip_generic *gc,
106 writel(readl(gc->reg_base + offset) | mask,
107 gc->reg_base + offset);
109 writel(readl(gc->reg_base + offset) & ~mask,
110 gc->reg_base + offset);
113 static int liointc_set_type(struct irq_data *data, unsigned int type)
115 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
116 u32 mask = data->mask;
119 irq_gc_lock_irqsave(gc, flags);
121 case IRQ_TYPE_LEVEL_HIGH:
122 liointc_set_bit(gc, LIOINTC_REG_INTC_EDGE, mask, false);
123 liointc_set_bit(gc, LIOINTC_REG_INTC_POL, mask, false);
125 case IRQ_TYPE_LEVEL_LOW:
126 liointc_set_bit(gc, LIOINTC_REG_INTC_EDGE, mask, false);
127 liointc_set_bit(gc, LIOINTC_REG_INTC_POL, mask, true);
129 case IRQ_TYPE_EDGE_RISING:
130 liointc_set_bit(gc, LIOINTC_REG_INTC_EDGE, mask, true);
131 liointc_set_bit(gc, LIOINTC_REG_INTC_POL, mask, false);
133 case IRQ_TYPE_EDGE_FALLING:
134 liointc_set_bit(gc, LIOINTC_REG_INTC_EDGE, mask, true);
135 liointc_set_bit(gc, LIOINTC_REG_INTC_POL, mask, true);
138 irq_gc_unlock_irqrestore(gc, flags);
141 irq_gc_unlock_irqrestore(gc, flags);
143 irqd_set_trigger_type(data, type);
147 static void liointc_suspend(struct irq_chip_generic *gc)
149 struct liointc_priv *priv = gc->private;
151 priv->int_pol = readl(gc->reg_base + LIOINTC_REG_INTC_POL);
152 priv->int_edge = readl(gc->reg_base + LIOINTC_REG_INTC_EDGE);
155 static void liointc_resume(struct irq_chip_generic *gc)
157 struct liointc_priv *priv = gc->private;
161 irq_gc_lock_irqsave(gc, flags);
162 /* Disable all at first */
163 writel(0xffffffff, gc->reg_base + LIOINTC_REG_INTC_DISABLE);
164 /* Restore map cache */
165 for (i = 0; i < LIOINTC_CHIP_IRQ; i++)
166 writeb(priv->map_cache[i], gc->reg_base + i);
167 writel(priv->int_pol, gc->reg_base + LIOINTC_REG_INTC_POL);
168 writel(priv->int_edge, gc->reg_base + LIOINTC_REG_INTC_EDGE);
169 /* Restore mask cache */
170 writel(gc->mask_cache, gc->reg_base + LIOINTC_REG_INTC_ENABLE);
171 irq_gc_unlock_irqrestore(gc, flags);
174 static int parent_irq[LIOINTC_NUM_PARENT];
175 static u32 parent_int_map[LIOINTC_NUM_PARENT];
176 static const char *const parent_names[] = {"int0", "int1", "int2", "int3"};
177 static const char *const core_reg_names[] = {"isr0", "isr1", "isr2", "isr3"};
179 static int liointc_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
180 const u32 *intspec, unsigned int intsize,
181 unsigned long *out_hwirq, unsigned int *out_type)
183 if (WARN_ON(intsize < 1))
185 *out_hwirq = intspec[0] - GSI_MIN_CPU_IRQ;
188 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
190 *out_type = IRQ_TYPE_NONE;
195 static const struct irq_domain_ops acpi_irq_gc_ops = {
196 .map = irq_map_generic_chip,
197 .unmap = irq_unmap_generic_chip,
198 .xlate = liointc_domain_xlate,
201 static int liointc_init(phys_addr_t addr, unsigned long size, int revision,
202 struct fwnode_handle *domain_handle, struct device_node *node)
206 struct irq_chip_type *ct;
207 struct irq_chip_generic *gc;
208 struct irq_domain *domain;
209 struct liointc_priv *priv;
211 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
215 base = ioremap(addr, size);
219 for (i = 0; i < LIOINTC_NUM_CORES; i++)
220 priv->core_isr[i] = base + LIOINTC_REG_INTC_STATUS;
222 for (i = 0; i < LIOINTC_NUM_PARENT; i++)
223 priv->handler[i].parent_int_map = parent_int_map[i];
226 for (i = 0; i < LIOINTC_NUM_CORES; i++) {
227 int index = of_property_match_string(node,
228 "reg-names", core_reg_names[i]);
233 priv->core_isr[i] = of_iomap(node, index);
236 if (!priv->core_isr[0])
240 /* Setup IRQ domain */
242 domain = irq_domain_create_linear(domain_handle, LIOINTC_CHIP_IRQ,
243 &acpi_irq_gc_ops, priv);
245 domain = irq_domain_create_linear(domain_handle, LIOINTC_CHIP_IRQ,
246 &irq_generic_chip_ops, priv);
248 pr_err("loongson-liointc: cannot add IRQ domain\n");
252 err = irq_alloc_domain_generic_chips(domain, LIOINTC_CHIP_IRQ, 1,
253 (node ? node->full_name : "LIOINTC"),
254 handle_level_irq, 0, IRQ_NOPROBE, 0);
256 pr_err("loongson-liointc: unable to register IRQ domain\n");
257 goto out_free_domain;
261 /* Disable all IRQs */
262 writel(0xffffffff, base + LIOINTC_REG_INTC_DISABLE);
263 /* Set to level triggered */
264 writel(0x0, base + LIOINTC_REG_INTC_EDGE);
266 /* Generate parent INT part of map cache */
267 for (i = 0; i < LIOINTC_NUM_PARENT; i++) {
268 u32 pending = priv->handler[i].parent_int_map;
271 int bit = __ffs(pending);
273 priv->map_cache[bit] = BIT(i) << LIOINTC_SHIFT_INTx;
274 pending &= ~BIT(bit);
278 for (i = 0; i < LIOINTC_CHIP_IRQ; i++) {
279 /* Generate core part of map cache */
280 priv->map_cache[i] |= BIT(loongson_sysconf.boot_cpu_id);
281 writeb(priv->map_cache[i], base + i);
284 gc = irq_get_domain_generic_chip(domain, 0);
288 gc->suspend = liointc_suspend;
289 gc->resume = liointc_resume;
292 ct->regs.enable = LIOINTC_REG_INTC_ENABLE;
293 ct->regs.disable = LIOINTC_REG_INTC_DISABLE;
294 ct->chip.irq_unmask = irq_gc_unmask_enable_reg;
295 ct->chip.irq_mask = irq_gc_mask_disable_reg;
296 ct->chip.irq_mask_ack = irq_gc_mask_disable_reg;
297 ct->chip.irq_set_type = liointc_set_type;
298 ct->chip.flags = IRQCHIP_SKIP_SET_WAKE;
303 for (i = 0; i < LIOINTC_NUM_PARENT; i++) {
304 if (parent_irq[i] <= 0)
307 priv->handler[i].priv = priv;
308 irq_set_chained_handler_and_data(parent_irq[i],
309 liointc_chained_handle_irq, &priv->handler[i]);
312 liointc_handle = domain_handle;
316 irq_domain_remove(domain);
327 static int __init liointc_of_init(struct device_node *node,
328 struct device_node *parent)
330 bool have_parent = FALSE;
331 int sz, i, index, revision, err = 0;
334 if (!of_device_is_compatible(node, "loongson,liointc-2.0")) {
338 index = of_property_match_string(node, "reg-names", "main");
342 if (of_address_to_resource(node, index, &res))
345 for (i = 0; i < LIOINTC_NUM_PARENT; i++) {
346 parent_irq[i] = of_irq_get_byname(node, parent_names[i]);
347 if (parent_irq[i] > 0)
353 sz = of_property_read_variable_u32_array(node,
354 "loongson,parent_int_map",
359 pr_err("loongson-liointc: No parent_int_map\n");
363 err = liointc_init(res.start, resource_size(&res),
364 revision, of_node_to_fwnode(node), node);
371 IRQCHIP_DECLARE(loongson_liointc_1_0, "loongson,liointc-1.0", liointc_of_init);
372 IRQCHIP_DECLARE(loongson_liointc_1_0a, "loongson,liointc-1.0a", liointc_of_init);
373 IRQCHIP_DECLARE(loongson_liointc_2_0, "loongson,liointc-2.0", liointc_of_init);
378 static int __init htintc_parse_madt(union acpi_subtable_headers *header,
379 const unsigned long end)
381 struct acpi_madt_ht_pic *htintc_entry = (struct acpi_madt_ht_pic *)header;
382 struct irq_domain *parent = irq_find_matching_fwnode(liointc_handle, DOMAIN_BUS_ANY);
384 return htvec_acpi_init(parent, htintc_entry);
387 static int __init acpi_cascade_irqdomain_init(void)
391 r = acpi_table_parse_madt(ACPI_MADT_TYPE_HT_PIC, htintc_parse_madt, 0);
398 int __init liointc_acpi_init(struct irq_domain *parent, struct acpi_madt_lio_pic *acpi_liointc)
401 struct fwnode_handle *domain_handle;
403 parent_int_map[0] = acpi_liointc->cascade_map[0];
404 parent_int_map[1] = acpi_liointc->cascade_map[1];
406 parent_irq[0] = irq_create_mapping(parent, acpi_liointc->cascade[0]);
407 parent_irq[1] = irq_create_mapping(parent, acpi_liointc->cascade[1]);
409 domain_handle = irq_domain_alloc_fwnode(&acpi_liointc->address);
410 if (!domain_handle) {
411 pr_err("Unable to allocate domain handle\n");
415 ret = liointc_init(acpi_liointc->address, acpi_liointc->size,
416 1, domain_handle, NULL);
418 ret = acpi_cascade_irqdomain_init();
420 irq_domain_free_fwnode(domain_handle);