Re: [PATCH v2 2/2] irqchip: Add support for Realtek RTL838x/RTL839x IRQ controller

2021-02-02 Thread Marc Zyngier

John,

On 2021-02-02 15:33, John Crispin wrote:

On 04.01.21 14:17, Bert Vermeulen wrote:

This is a standard IRQ driver with only status and mask registers.

The mapping from SoC interrupts (18-31) to MIPS core interrupts is
done via an interrupt-map in device tree.

Signed-off-by: Bert Vermeulen 

Signed-off-by: John Crispin 


There is already a v4 on the list[1], so you may want to comment on that 
one.


It would also help if you'd give a reason for your SoB to be added.
Or did you mean to Ack the patch?

Thanks,

M.

[1] https://lore.kernel.ortg/r/20210122204224.509124-1-b...@biot.com
--
Jazz is not dead. It just smells funny...


Re: [PATCH v2 2/2] irqchip: Add support for Realtek RTL838x/RTL839x IRQ controller

2021-02-02 Thread John Crispin



On 04.01.21 14:17, Bert Vermeulen wrote:

This is a standard IRQ driver with only status and mask registers.

The mapping from SoC interrupts (18-31) to MIPS core interrupts is
done via an interrupt-map in device tree.

Signed-off-by: Bert Vermeulen 

Signed-off-by: John Crispin 

---
  drivers/irqchip/Makefile  |   1 +
  drivers/irqchip/irq-realtek-rtl.c | 180 ++
  2 files changed, 181 insertions(+)
  create mode 100644 drivers/irqchip/irq-realtek-rtl.c

diff --git a/drivers/irqchip/Makefile b/drivers/irqchip/Makefile
index 0ac93bfaec61..4fc1086bed7e 100644
--- a/drivers/irqchip/Makefile
+++ b/drivers/irqchip/Makefile
@@ -113,3 +113,4 @@ obj-$(CONFIG_LOONGSON_PCH_PIC)  += 
irq-loongson-pch-pic.o
  obj-$(CONFIG_LOONGSON_PCH_MSI)+= irq-loongson-pch-msi.o
  obj-$(CONFIG_MST_IRQ) += irq-mst-intc.o
  obj-$(CONFIG_SL28CPLD_INTC)   += irq-sl28cpld.o
+obj-$(CONFIG_MACH_REALTEK_RTL) += irq-realtek-rtl.o
diff --git a/drivers/irqchip/irq-realtek-rtl.c 
b/drivers/irqchip/irq-realtek-rtl.c
new file mode 100644
index ..bafe9ee4a85a
--- /dev/null
+++ b/drivers/irqchip/irq-realtek-rtl.c
@@ -0,0 +1,180 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2006-2012 Tony Wu 
+ * Copyright (C) 2020 Birger Koblitz 
+ * Copyright (C) 2020 Bert Vermeulen 
+ * Copyright (C) 2020 John Crispin 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Global Interrupt Mask Register */
+#define RTL_ICTL_GIMR  0x00
+/* Global Interrupt Status Register */
+#define RTL_ICTL_GISR  0x04
+/* Interrupt Routing Registers */
+#define RTL_ICTL_IRR0  0x08
+#define RTL_ICTL_IRR1  0x0c
+#define RTL_ICTL_IRR2  0x10
+#define RTL_ICTL_IRR3  0x14
+
+#define REG(x) (realtek_ictl_base + x)
+
+static DEFINE_RAW_SPINLOCK(irq_lock);
+static void __iomem *realtek_ictl_base;
+
+static void realtek_ictl_unmask_irq(struct irq_data *i)
+{
+   unsigned long flags;
+   u32 value;
+
+   raw_spin_lock_irqsave(&irq_lock, flags);
+
+   value = readl(REG(RTL_ICTL_GIMR));
+   value |= BIT(i->hwirq);
+   writel(value, REG(RTL_ICTL_GIMR));
+
+   raw_spin_unlock_irqrestore(&irq_lock, flags);
+}
+
+static void realtek_ictl_mask_irq(struct irq_data *i)
+{
+   unsigned long flags;
+   u32 value;
+
+   raw_spin_lock_irqsave(&irq_lock, flags);
+
+   value = readl(REG(RTL_ICTL_GIMR));
+   value &= ~BIT(i->hwirq);
+   writel(value, REG(RTL_ICTL_GIMR));
+
+   raw_spin_unlock_irqrestore(&irq_lock, flags);
+}
+
+static struct irq_chip realtek_ictl_irq = {
+   .name = "realtek-rtl-intc",
+   .irq_mask = realtek_ictl_mask_irq,
+   .irq_unmask = realtek_ictl_unmask_irq,
+};
+
+static int intc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
+{
+   irq_set_chip_and_handler(hw, &realtek_ictl_irq, handle_level_irq);
+
+   return 0;
+}
+
+static const struct irq_domain_ops irq_domain_ops = {
+   .map = intc_map,
+   .xlate = irq_domain_xlate_onecell,
+};
+
+static void realtek_irq_dispatch(struct irq_desc *desc)
+{
+   struct irq_chip *chip = irq_desc_get_chip(desc);
+   struct irq_domain *domain;
+   unsigned int pending;
+
+   chained_irq_enter(chip, desc);
+   pending = readl(REG(RTL_ICTL_GIMR)) & readl(REG(RTL_ICTL_GISR));
+   if (unlikely(!pending)) {
+   spurious_interrupt();
+   goto out;
+   }
+   domain = irq_desc_get_handler_data(desc);
+   generic_handle_irq(irq_find_mapping(domain, __ffs(pending)));
+
+out:
+   chained_irq_exit(chip, desc);
+}
+
+/*
+ * SoC interrupts are cascaded to MIPS CPU interrupts according to the
+ * interrupt-map in the device tree. Each SoC interrupt gets 4 bits for
+ * the CPU interrupt in an Interrupt Routing Register. Max 32 SoC interrupts
+ * thus go into 4 IRRs.
+ */
+static int __init map_interrupts(struct device_node *node)
+{
+   struct device_node *cpu_ictl;
+   const __be32 *imap;
+   u32 imaplen, soc_int, cpu_int, tmp, regs[4];
+   int ret, i, irr_regs[] = {
+   RTL_ICTL_IRR3,
+   RTL_ICTL_IRR2,
+   RTL_ICTL_IRR1,
+   RTL_ICTL_IRR0,
+   };
+
+   ret = of_property_read_u32(node, "#address-cells", &tmp);
+   if (ret || tmp)
+   return -EINVAL;
+
+   imap = of_get_property(node, "interrupt-map", &imaplen);
+   if (!imap || imaplen % 3)
+   return -EINVAL;
+
+   memset(regs, 0, sizeof(regs));
+   for (i = 0; i < imaplen; i += 3 * sizeof(u32)) {
+   soc_int = be32_to_cpup(imap);
+   if (soc_int > 31)
+   return -EINVAL;
+
+   cpu_ictl = of_find_node_by_phandle(be32_to_cpup(imap + 1));
+   if (!cpu_ictl)
+   return -EINVAL;
+   ret = of_property_read_u32(cpu_ictl, "#interrupt-cells", 

[PATCH v2 2/2] irqchip: Add support for Realtek RTL838x/RTL839x IRQ controller

2021-01-04 Thread Bert Vermeulen
This is a standard IRQ driver with only status and mask registers.

The mapping from SoC interrupts (18-31) to MIPS core interrupts is
done via an interrupt-map in device tree.

Signed-off-by: Bert Vermeulen 
---
 drivers/irqchip/Makefile  |   1 +
 drivers/irqchip/irq-realtek-rtl.c | 180 ++
 2 files changed, 181 insertions(+)
 create mode 100644 drivers/irqchip/irq-realtek-rtl.c

diff --git a/drivers/irqchip/Makefile b/drivers/irqchip/Makefile
index 0ac93bfaec61..4fc1086bed7e 100644
--- a/drivers/irqchip/Makefile
+++ b/drivers/irqchip/Makefile
@@ -113,3 +113,4 @@ obj-$(CONFIG_LOONGSON_PCH_PIC)  += 
irq-loongson-pch-pic.o
 obj-$(CONFIG_LOONGSON_PCH_MSI) += irq-loongson-pch-msi.o
 obj-$(CONFIG_MST_IRQ)  += irq-mst-intc.o
 obj-$(CONFIG_SL28CPLD_INTC)+= irq-sl28cpld.o
+obj-$(CONFIG_MACH_REALTEK_RTL) += irq-realtek-rtl.o
diff --git a/drivers/irqchip/irq-realtek-rtl.c 
b/drivers/irqchip/irq-realtek-rtl.c
new file mode 100644
index ..bafe9ee4a85a
--- /dev/null
+++ b/drivers/irqchip/irq-realtek-rtl.c
@@ -0,0 +1,180 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2006-2012 Tony Wu 
+ * Copyright (C) 2020 Birger Koblitz 
+ * Copyright (C) 2020 Bert Vermeulen 
+ * Copyright (C) 2020 John Crispin 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Global Interrupt Mask Register */
+#define RTL_ICTL_GIMR  0x00
+/* Global Interrupt Status Register */
+#define RTL_ICTL_GISR  0x04
+/* Interrupt Routing Registers */
+#define RTL_ICTL_IRR0  0x08
+#define RTL_ICTL_IRR1  0x0c
+#define RTL_ICTL_IRR2  0x10
+#define RTL_ICTL_IRR3  0x14
+
+#define REG(x) (realtek_ictl_base + x)
+
+static DEFINE_RAW_SPINLOCK(irq_lock);
+static void __iomem *realtek_ictl_base;
+
+static void realtek_ictl_unmask_irq(struct irq_data *i)
+{
+   unsigned long flags;
+   u32 value;
+
+   raw_spin_lock_irqsave(&irq_lock, flags);
+
+   value = readl(REG(RTL_ICTL_GIMR));
+   value |= BIT(i->hwirq);
+   writel(value, REG(RTL_ICTL_GIMR));
+
+   raw_spin_unlock_irqrestore(&irq_lock, flags);
+}
+
+static void realtek_ictl_mask_irq(struct irq_data *i)
+{
+   unsigned long flags;
+   u32 value;
+
+   raw_spin_lock_irqsave(&irq_lock, flags);
+
+   value = readl(REG(RTL_ICTL_GIMR));
+   value &= ~BIT(i->hwirq);
+   writel(value, REG(RTL_ICTL_GIMR));
+
+   raw_spin_unlock_irqrestore(&irq_lock, flags);
+}
+
+static struct irq_chip realtek_ictl_irq = {
+   .name = "realtek-rtl-intc",
+   .irq_mask = realtek_ictl_mask_irq,
+   .irq_unmask = realtek_ictl_unmask_irq,
+};
+
+static int intc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
+{
+   irq_set_chip_and_handler(hw, &realtek_ictl_irq, handle_level_irq);
+
+   return 0;
+}
+
+static const struct irq_domain_ops irq_domain_ops = {
+   .map = intc_map,
+   .xlate = irq_domain_xlate_onecell,
+};
+
+static void realtek_irq_dispatch(struct irq_desc *desc)
+{
+   struct irq_chip *chip = irq_desc_get_chip(desc);
+   struct irq_domain *domain;
+   unsigned int pending;
+
+   chained_irq_enter(chip, desc);
+   pending = readl(REG(RTL_ICTL_GIMR)) & readl(REG(RTL_ICTL_GISR));
+   if (unlikely(!pending)) {
+   spurious_interrupt();
+   goto out;
+   }
+   domain = irq_desc_get_handler_data(desc);
+   generic_handle_irq(irq_find_mapping(domain, __ffs(pending)));
+
+out:
+   chained_irq_exit(chip, desc);
+}
+
+/*
+ * SoC interrupts are cascaded to MIPS CPU interrupts according to the
+ * interrupt-map in the device tree. Each SoC interrupt gets 4 bits for
+ * the CPU interrupt in an Interrupt Routing Register. Max 32 SoC interrupts
+ * thus go into 4 IRRs.
+ */
+static int __init map_interrupts(struct device_node *node)
+{
+   struct device_node *cpu_ictl;
+   const __be32 *imap;
+   u32 imaplen, soc_int, cpu_int, tmp, regs[4];
+   int ret, i, irr_regs[] = {
+   RTL_ICTL_IRR3,
+   RTL_ICTL_IRR2,
+   RTL_ICTL_IRR1,
+   RTL_ICTL_IRR0,
+   };
+
+   ret = of_property_read_u32(node, "#address-cells", &tmp);
+   if (ret || tmp)
+   return -EINVAL;
+
+   imap = of_get_property(node, "interrupt-map", &imaplen);
+   if (!imap || imaplen % 3)
+   return -EINVAL;
+
+   memset(regs, 0, sizeof(regs));
+   for (i = 0; i < imaplen; i += 3 * sizeof(u32)) {
+   soc_int = be32_to_cpup(imap);
+   if (soc_int > 31)
+   return -EINVAL;
+
+   cpu_ictl = of_find_node_by_phandle(be32_to_cpup(imap + 1));
+   if (!cpu_ictl)
+   return -EINVAL;
+   ret = of_property_read_u32(cpu_ictl, "#interrupt-cells", &tmp);
+   if (ret || tmp != 1)
+   return -EINVAL;
+