1 // SPDX-License-Identifier: GPL-2.0
4 * Loongson PCH PIC support
7 #define pr_fmt(fmt) "pch-pic: " fmt
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/irqchip.h>
12 #include <linux/irqdomain.h>
13 #include <linux/kernel.h>
14 #include <linux/platform_device.h>
15 #include <linux/of_address.h>
16 #include <linux/of_irq.h>
17 #include <linux/of_platform.h>
20 #define PCH_PIC_MASK 0x20
21 #define PCH_PIC_HTMSI_EN 0x40
22 #define PCH_PIC_EDGE 0x60
23 #define PCH_PIC_CLR 0x80
24 #define PCH_PIC_AUTO0 0xc0
25 #define PCH_PIC_AUTO1 0xe0
26 #define PCH_INT_ROUTE(irq) (0x100 + irq)
27 #define PCH_INT_HTVEC(irq) (0x200 + irq)
28 #define PCH_PIC_POL 0x3e0
30 #define PIC_COUNT_PER_REG 32
31 #define PIC_REG_COUNT 2
32 #define PIC_COUNT (PIC_COUNT_PER_REG * PIC_REG_COUNT)
33 #define PIC_REG_IDX(irq_id) ((irq_id) / PIC_COUNT_PER_REG)
34 #define PIC_REG_BIT(irq_id) ((irq_id) % PIC_COUNT_PER_REG)
38 struct irq_domain *pic_domain;
40 raw_spinlock_t pic_lock;
43 static void pch_pic_bitset(struct pch_pic *priv, int offset, int bit)
46 void __iomem *addr = priv->base + offset + PIC_REG_IDX(bit) * 4;
48 raw_spin_lock(&priv->pic_lock);
50 reg |= BIT(PIC_REG_BIT(bit));
52 raw_spin_unlock(&priv->pic_lock);
55 static void pch_pic_bitclr(struct pch_pic *priv, int offset, int bit)
58 void __iomem *addr = priv->base + offset + PIC_REG_IDX(bit) * 4;
60 raw_spin_lock(&priv->pic_lock);
62 reg &= ~BIT(PIC_REG_BIT(bit));
64 raw_spin_unlock(&priv->pic_lock);
67 static void pch_pic_mask_irq(struct irq_data *d)
69 struct pch_pic *priv = irq_data_get_irq_chip_data(d);
71 pch_pic_bitset(priv, PCH_PIC_MASK, d->hwirq);
72 irq_chip_mask_parent(d);
75 static void pch_pic_unmask_irq(struct irq_data *d)
77 struct pch_pic *priv = irq_data_get_irq_chip_data(d);
79 writel(BIT(PIC_REG_BIT(d->hwirq)),
80 priv->base + PCH_PIC_CLR + PIC_REG_IDX(d->hwirq) * 4);
82 irq_chip_unmask_parent(d);
83 pch_pic_bitclr(priv, PCH_PIC_MASK, d->hwirq);
86 static int pch_pic_set_type(struct irq_data *d, unsigned int type)
88 struct pch_pic *priv = irq_data_get_irq_chip_data(d);
92 case IRQ_TYPE_EDGE_RISING:
93 pch_pic_bitset(priv, PCH_PIC_EDGE, d->hwirq);
94 pch_pic_bitclr(priv, PCH_PIC_POL, d->hwirq);
95 irq_set_handler_locked(d, handle_edge_irq);
97 case IRQ_TYPE_EDGE_FALLING:
98 pch_pic_bitset(priv, PCH_PIC_EDGE, d->hwirq);
99 pch_pic_bitset(priv, PCH_PIC_POL, d->hwirq);
100 irq_set_handler_locked(d, handle_edge_irq);
102 case IRQ_TYPE_LEVEL_HIGH:
103 pch_pic_bitclr(priv, PCH_PIC_EDGE, d->hwirq);
104 pch_pic_bitclr(priv, PCH_PIC_POL, d->hwirq);
105 irq_set_handler_locked(d, handle_level_irq);
107 case IRQ_TYPE_LEVEL_LOW:
108 pch_pic_bitclr(priv, PCH_PIC_EDGE, d->hwirq);
109 pch_pic_bitset(priv, PCH_PIC_POL, d->hwirq);
110 irq_set_handler_locked(d, handle_level_irq);
120 static void pch_pic_ack_irq(struct irq_data *d)
123 struct pch_pic *priv = irq_data_get_irq_chip_data(d);
125 reg = readl(priv->base + PCH_PIC_EDGE + PIC_REG_IDX(d->hwirq) * 4);
126 if (reg & BIT(PIC_REG_BIT(d->hwirq))) {
127 writel(BIT(PIC_REG_BIT(d->hwirq)),
128 priv->base + PCH_PIC_CLR + PIC_REG_IDX(d->hwirq) * 4);
130 irq_chip_ack_parent(d);
133 static struct irq_chip pch_pic_irq_chip = {
135 .irq_mask = pch_pic_mask_irq,
136 .irq_unmask = pch_pic_unmask_irq,
137 .irq_ack = pch_pic_ack_irq,
138 .irq_set_affinity = irq_chip_set_affinity_parent,
139 .irq_set_type = pch_pic_set_type,
142 static int pch_pic_alloc(struct irq_domain *domain, unsigned int virq,
143 unsigned int nr_irqs, void *arg)
148 struct irq_fwspec *fwspec = arg;
149 struct irq_fwspec parent_fwspec;
150 struct pch_pic *priv = domain->host_data;
152 err = irq_domain_translate_twocell(domain, fwspec, &hwirq, &type);
156 parent_fwspec.fwnode = domain->parent->fwnode;
157 parent_fwspec.param_count = 1;
158 parent_fwspec.param[0] = hwirq + priv->ht_vec_base;
160 err = irq_domain_alloc_irqs_parent(domain, virq, 1, &parent_fwspec);
164 irq_domain_set_info(domain, virq, hwirq,
165 &pch_pic_irq_chip, priv,
166 handle_level_irq, NULL, NULL);
172 static const struct irq_domain_ops pch_pic_domain_ops = {
173 .translate = irq_domain_translate_twocell,
174 .alloc = pch_pic_alloc,
175 .free = irq_domain_free_irqs_parent,
178 static void pch_pic_reset(struct pch_pic *priv)
182 for (i = 0; i < PIC_COUNT; i++) {
183 /* Write vectored ID */
184 writeb(priv->ht_vec_base + i, priv->base + PCH_INT_HTVEC(i));
185 /* Hardcode route to HT0 Lo */
186 writeb(1, priv->base + PCH_INT_ROUTE(i));
189 for (i = 0; i < PIC_REG_COUNT; i++) {
190 /* Clear IRQ cause registers, mask all interrupts */
191 writel_relaxed(0xFFFFFFFF, priv->base + PCH_PIC_MASK + 4 * i);
192 writel_relaxed(0xFFFFFFFF, priv->base + PCH_PIC_CLR + 4 * i);
193 /* Clear auto bounce, we don't need that */
194 writel_relaxed(0, priv->base + PCH_PIC_AUTO0 + 4 * i);
195 writel_relaxed(0, priv->base + PCH_PIC_AUTO1 + 4 * i);
196 /* Enable HTMSI transformer */
197 writel_relaxed(0xFFFFFFFF, priv->base + PCH_PIC_HTMSI_EN + 4 * i);
201 static int pch_pic_of_init(struct device_node *node,
202 struct device_node *parent)
204 struct pch_pic *priv;
205 struct irq_domain *parent_domain;
208 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
212 raw_spin_lock_init(&priv->pic_lock);
213 priv->base = of_iomap(node, 0);
219 parent_domain = irq_find_host(parent);
220 if (!parent_domain) {
221 pr_err("Failed to find the parent domain\n");
226 if (of_property_read_u32(node, "loongson,pic-base-vec",
227 &priv->ht_vec_base)) {
228 pr_err("Failed to determine pic-base-vec\n");
233 priv->pic_domain = irq_domain_create_hierarchy(parent_domain, 0,
235 of_node_to_fwnode(node),
238 if (!priv->pic_domain) {
239 pr_err("Failed to create IRQ domain\n");
256 IRQCHIP_DECLARE(pch_pic, "loongson,pch-pic-1.0", pch_pic_of_init);