1 // SPDX-License-Identifier: GPL-2.0+
3 * Actions Semi Owl SoCs SIRQ interrupt controller driver
5 * Copyright (C) 2014 Actions Semi Inc.
6 * David Liu <liuwei@actions-semi.com>
8 * Author: Parthiban Nallathambi <pn@denx.de>
9 * Author: Saravanan Sekar <sravanhome@gmail.com>
10 * Author: Cristian Ciocaltea <cristian.ciocaltea@gmail.com>
13 #include <linux/bitfield.h>
14 #include <linux/interrupt.h>
15 #include <linux/irqchip.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
19 #include <dt-bindings/interrupt-controller/arm-gic.h>
23 #define INTC_EXTCTL_PENDING BIT(0)
24 #define INTC_EXTCTL_CLK_SEL BIT(4)
25 #define INTC_EXTCTL_EN BIT(5)
26 #define INTC_EXTCTL_TYPE_MASK GENMASK(7, 6)
27 #define INTC_EXTCTL_TYPE_HIGH 0
28 #define INTC_EXTCTL_TYPE_LOW BIT(6)
29 #define INTC_EXTCTL_TYPE_RISING BIT(7)
30 #define INTC_EXTCTL_TYPE_FALLING (BIT(6) | BIT(7))
32 /* S500 & S700 SIRQ control register masks */
33 #define INTC_EXTCTL_SIRQ0_MASK GENMASK(23, 16)
34 #define INTC_EXTCTL_SIRQ1_MASK GENMASK(15, 8)
35 #define INTC_EXTCTL_SIRQ2_MASK GENMASK(7, 0)
37 /* S900 SIRQ control register offsets, relative to controller base address */
38 #define INTC_EXTCTL0 0x0000
39 #define INTC_EXTCTL1 0x0328
40 #define INTC_EXTCTL2 0x032c
42 struct owl_sirq_params
{
43 /* INTC_EXTCTL reg shared for all three SIRQ lines */
45 /* INTC_EXTCTL reg offsets relative to controller base address */
46 u16 reg_offset
[NUM_SIRQ
];
49 struct owl_sirq_chip_data
{
50 const struct owl_sirq_params
*params
;
53 u32 ext_irqs
[NUM_SIRQ
];
56 /* S500 & S700 SoCs */
57 static const struct owl_sirq_params owl_sirq_s500_params
= {
59 .reg_offset
= { 0, 0, 0 },
63 static const struct owl_sirq_params owl_sirq_s900_params
= {
65 .reg_offset
= { INTC_EXTCTL0
, INTC_EXTCTL1
, INTC_EXTCTL2
},
68 static u32
owl_field_get(u32 val
, u32 index
)
72 return FIELD_GET(INTC_EXTCTL_SIRQ0_MASK
, val
);
74 return FIELD_GET(INTC_EXTCTL_SIRQ1_MASK
, val
);
77 return FIELD_GET(INTC_EXTCTL_SIRQ2_MASK
, val
);
81 static u32
owl_field_prep(u32 val
, u32 index
)
85 return FIELD_PREP(INTC_EXTCTL_SIRQ0_MASK
, val
);
87 return FIELD_PREP(INTC_EXTCTL_SIRQ1_MASK
, val
);
90 return FIELD_PREP(INTC_EXTCTL_SIRQ2_MASK
, val
);
94 static u32
owl_sirq_read_extctl(struct owl_sirq_chip_data
*data
, u32 index
)
98 val
= readl_relaxed(data
->base
+ data
->params
->reg_offset
[index
]);
99 if (data
->params
->reg_shared
)
100 val
= owl_field_get(val
, index
);
105 static void owl_sirq_write_extctl(struct owl_sirq_chip_data
*data
,
106 u32 extctl
, u32 index
)
110 if (data
->params
->reg_shared
) {
111 val
= readl_relaxed(data
->base
+ data
->params
->reg_offset
[index
]);
112 val
&= ~owl_field_prep(0xff, index
);
113 extctl
= owl_field_prep(extctl
, index
) | val
;
116 writel_relaxed(extctl
, data
->base
+ data
->params
->reg_offset
[index
]);
119 static void owl_sirq_clear_set_extctl(struct owl_sirq_chip_data
*d
,
120 u32 clear
, u32 set
, u32 index
)
125 raw_spin_lock_irqsave(&d
->lock
, flags
);
126 val
= owl_sirq_read_extctl(d
, index
);
129 owl_sirq_write_extctl(d
, val
, index
);
130 raw_spin_unlock_irqrestore(&d
->lock
, flags
);
133 static void owl_sirq_eoi(struct irq_data
*data
)
135 struct owl_sirq_chip_data
*chip_data
= irq_data_get_irq_chip_data(data
);
138 * Software must clear external interrupt pending, when interrupt type
139 * is edge triggered, so we need per SIRQ based clearing.
141 if (!irqd_is_level_type(data
))
142 owl_sirq_clear_set_extctl(chip_data
, 0, INTC_EXTCTL_PENDING
,
145 irq_chip_eoi_parent(data
);
148 static void owl_sirq_mask(struct irq_data
*data
)
150 struct owl_sirq_chip_data
*chip_data
= irq_data_get_irq_chip_data(data
);
152 owl_sirq_clear_set_extctl(chip_data
, INTC_EXTCTL_EN
, 0, data
->hwirq
);
153 irq_chip_mask_parent(data
);
156 static void owl_sirq_unmask(struct irq_data
*data
)
158 struct owl_sirq_chip_data
*chip_data
= irq_data_get_irq_chip_data(data
);
160 owl_sirq_clear_set_extctl(chip_data
, 0, INTC_EXTCTL_EN
, data
->hwirq
);
161 irq_chip_unmask_parent(data
);
165 * GIC does not handle falling edge or active low, hence SIRQ shall be
166 * programmed to convert falling edge to rising edge signal and active
167 * low to active high signal.
169 static int owl_sirq_set_type(struct irq_data
*data
, unsigned int type
)
171 struct owl_sirq_chip_data
*chip_data
= irq_data_get_irq_chip_data(data
);
175 case IRQ_TYPE_LEVEL_LOW
:
176 sirq_type
= INTC_EXTCTL_TYPE_LOW
;
177 type
= IRQ_TYPE_LEVEL_HIGH
;
179 case IRQ_TYPE_LEVEL_HIGH
:
180 sirq_type
= INTC_EXTCTL_TYPE_HIGH
;
182 case IRQ_TYPE_EDGE_FALLING
:
183 sirq_type
= INTC_EXTCTL_TYPE_FALLING
;
184 type
= IRQ_TYPE_EDGE_RISING
;
186 case IRQ_TYPE_EDGE_RISING
:
187 sirq_type
= INTC_EXTCTL_TYPE_RISING
;
193 owl_sirq_clear_set_extctl(chip_data
, INTC_EXTCTL_TYPE_MASK
, sirq_type
,
196 return irq_chip_set_type_parent(data
, type
);
199 static struct irq_chip owl_sirq_chip
= {
201 .irq_mask
= owl_sirq_mask
,
202 .irq_unmask
= owl_sirq_unmask
,
203 .irq_eoi
= owl_sirq_eoi
,
204 .irq_set_type
= owl_sirq_set_type
,
205 .irq_retrigger
= irq_chip_retrigger_hierarchy
,
207 .irq_set_affinity
= irq_chip_set_affinity_parent
,
211 static int owl_sirq_domain_translate(struct irq_domain
*d
,
212 struct irq_fwspec
*fwspec
,
213 unsigned long *hwirq
,
216 if (!is_of_node(fwspec
->fwnode
))
219 if (fwspec
->param_count
!= 2 || fwspec
->param
[0] >= NUM_SIRQ
)
222 *hwirq
= fwspec
->param
[0];
223 *type
= fwspec
->param
[1];
228 static int owl_sirq_domain_alloc(struct irq_domain
*domain
, unsigned int virq
,
229 unsigned int nr_irqs
, void *data
)
231 struct owl_sirq_chip_data
*chip_data
= domain
->host_data
;
232 struct irq_fwspec
*fwspec
= data
;
233 struct irq_fwspec parent_fwspec
;
234 irq_hw_number_t hwirq
;
238 if (WARN_ON(nr_irqs
!= 1))
241 ret
= owl_sirq_domain_translate(domain
, fwspec
, &hwirq
, &type
);
246 case IRQ_TYPE_EDGE_RISING
:
247 case IRQ_TYPE_LEVEL_HIGH
:
249 case IRQ_TYPE_EDGE_FALLING
:
250 type
= IRQ_TYPE_EDGE_RISING
;
252 case IRQ_TYPE_LEVEL_LOW
:
253 type
= IRQ_TYPE_LEVEL_HIGH
;
259 irq_domain_set_hwirq_and_chip(domain
, virq
, hwirq
, &owl_sirq_chip
,
262 parent_fwspec
.fwnode
= domain
->parent
->fwnode
;
263 parent_fwspec
.param_count
= 3;
264 parent_fwspec
.param
[0] = GIC_SPI
;
265 parent_fwspec
.param
[1] = chip_data
->ext_irqs
[hwirq
];
266 parent_fwspec
.param
[2] = type
;
268 return irq_domain_alloc_irqs_parent(domain
, virq
, 1, &parent_fwspec
);
271 static const struct irq_domain_ops owl_sirq_domain_ops
= {
272 .translate
= owl_sirq_domain_translate
,
273 .alloc
= owl_sirq_domain_alloc
,
274 .free
= irq_domain_free_irqs_common
,
277 static int __init
owl_sirq_init(const struct owl_sirq_params
*params
,
278 struct device_node
*node
,
279 struct device_node
*parent
)
281 struct irq_domain
*domain
, *parent_domain
;
282 struct owl_sirq_chip_data
*chip_data
;
285 parent_domain
= irq_find_host(parent
);
286 if (!parent_domain
) {
287 pr_err("%pOF: failed to find sirq parent domain\n", node
);
291 chip_data
= kzalloc(sizeof(*chip_data
), GFP_KERNEL
);
295 raw_spin_lock_init(&chip_data
->lock
);
297 chip_data
->params
= params
;
299 chip_data
->base
= of_iomap(node
, 0);
300 if (!chip_data
->base
) {
301 pr_err("%pOF: failed to map sirq registers\n", node
);
306 for (i
= 0; i
< NUM_SIRQ
; i
++) {
307 struct of_phandle_args irq
;
309 ret
= of_irq_parse_one(node
, i
, &irq
);
311 pr_err("%pOF: failed to parse interrupt %d\n", node
, i
);
315 if (WARN_ON(irq
.args_count
!= 3)) {
320 chip_data
->ext_irqs
[i
] = irq
.args
[1];
322 /* Set 24MHz external interrupt clock freq */
323 owl_sirq_clear_set_extctl(chip_data
, 0, INTC_EXTCTL_CLK_SEL
, i
);
326 domain
= irq_domain_add_hierarchy(parent_domain
, 0, NUM_SIRQ
, node
,
327 &owl_sirq_domain_ops
, chip_data
);
329 pr_err("%pOF: failed to add domain\n", node
);
337 iounmap(chip_data
->base
);
344 static int __init
owl_sirq_s500_of_init(struct device_node
*node
,
345 struct device_node
*parent
)
347 return owl_sirq_init(&owl_sirq_s500_params
, node
, parent
);
350 IRQCHIP_DECLARE(owl_sirq_s500
, "actions,s500-sirq", owl_sirq_s500_of_init
);
351 IRQCHIP_DECLARE(owl_sirq_s700
, "actions,s700-sirq", owl_sirq_s500_of_init
);
353 static int __init
owl_sirq_s900_of_init(struct device_node
*node
,
354 struct device_node
*parent
)
356 return owl_sirq_init(&owl_sirq_s900_params
, node
, parent
);
359 IRQCHIP_DECLARE(owl_sirq_s900
, "actions,s900-sirq", owl_sirq_s900_of_init
);