1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 HiSilicon Limited, All Rights Reserved.
4 * Author: Jun Ma <majun258@huawei.com>
5 * Author: Yun Wu <wuyun.wu@huawei.com>
8 #include <linux/acpi.h>
9 #include <linux/interrupt.h>
10 #include <linux/irqchip.h>
11 #include <linux/module.h>
12 #include <linux/msi.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
19 /* Interrupt numbers per mbigen node supported */
20 #define IRQS_PER_MBIGEN_NODE 128
22 /* 64 irqs (Pin0-pin63) are reserved for each mbigen chip */
23 #define RESERVED_IRQ_PER_MBIGEN_CHIP 64
25 /* The maximum IRQ pin number of mbigen chip(start from 0) */
26 #define MAXIMUM_IRQ_PIN_NUM 1407
29 * In mbigen vector register
30 * bit[21:12]: event id value
31 * bit[11:0]: device id
33 #define IRQ_EVENT_ID_SHIFT 12
34 #define IRQ_EVENT_ID_MASK 0x3ff
36 /* register range of each mbigen node */
37 #define MBIGEN_NODE_OFFSET 0x1000
39 /* offset of vector register in mbigen node */
40 #define REG_MBIGEN_VEC_OFFSET 0x200
43 * offset of clear register in mbigen node
44 * This register is used to clear the status
47 #define REG_MBIGEN_CLEAR_OFFSET 0xa000
50 * offset of interrupt type register
51 * This register is used to configure interrupt
54 #define REG_MBIGEN_TYPE_OFFSET 0x0
57 * struct mbigen_device - holds the information of mbigen device.
59 * @pdev: pointer to the platform device structure of mbigen chip.
60 * @base: mapped address of this mbigen chip.
62 struct mbigen_device
{
63 struct platform_device
*pdev
;
67 static inline unsigned int get_mbigen_node_offset(unsigned int nid
)
69 unsigned int offset
= nid
* MBIGEN_NODE_OFFSET
;
72 * To avoid touched clear register in unexpected way, we need to directly
73 * skip clear register when access to more than 10 mbigen nodes.
75 if (nid
>= (REG_MBIGEN_CLEAR_OFFSET
/ MBIGEN_NODE_OFFSET
))
76 offset
+= MBIGEN_NODE_OFFSET
;
81 static inline unsigned int get_mbigen_vec_reg(irq_hw_number_t hwirq
)
83 unsigned int nid
, pin
;
85 hwirq
-= RESERVED_IRQ_PER_MBIGEN_CHIP
;
86 nid
= hwirq
/ IRQS_PER_MBIGEN_NODE
+ 1;
87 pin
= hwirq
% IRQS_PER_MBIGEN_NODE
;
89 return pin
* 4 + get_mbigen_node_offset(nid
) + REG_MBIGEN_VEC_OFFSET
;
92 static inline void get_mbigen_type_reg(irq_hw_number_t hwirq
,
95 unsigned int nid
, irq_ofst
, ofst
;
97 hwirq
-= RESERVED_IRQ_PER_MBIGEN_CHIP
;
98 nid
= hwirq
/ IRQS_PER_MBIGEN_NODE
+ 1;
99 irq_ofst
= hwirq
% IRQS_PER_MBIGEN_NODE
;
101 *mask
= 1 << (irq_ofst
% 32);
102 ofst
= irq_ofst
/ 32 * 4;
104 *addr
= ofst
+ get_mbigen_node_offset(nid
) + REG_MBIGEN_TYPE_OFFSET
;
107 static inline void get_mbigen_clear_reg(irq_hw_number_t hwirq
,
108 u32
*mask
, u32
*addr
)
110 unsigned int ofst
= (hwirq
/ 32) * 4;
112 *mask
= 1 << (hwirq
% 32);
113 *addr
= ofst
+ REG_MBIGEN_CLEAR_OFFSET
;
116 static void mbigen_eoi_irq(struct irq_data
*data
)
118 void __iomem
*base
= data
->chip_data
;
121 get_mbigen_clear_reg(data
->hwirq
, &mask
, &addr
);
123 writel_relaxed(mask
, base
+ addr
);
125 irq_chip_eoi_parent(data
);
128 static int mbigen_set_type(struct irq_data
*data
, unsigned int type
)
130 void __iomem
*base
= data
->chip_data
;
133 if (type
!= IRQ_TYPE_LEVEL_HIGH
&& type
!= IRQ_TYPE_EDGE_RISING
)
136 get_mbigen_type_reg(data
->hwirq
, &mask
, &addr
);
138 val
= readl_relaxed(base
+ addr
);
140 if (type
== IRQ_TYPE_LEVEL_HIGH
)
145 writel_relaxed(val
, base
+ addr
);
150 static void mbigen_write_msi_msg(struct irq_data
*d
, struct msi_msg
*msg
)
152 void __iomem
*base
= d
->chip_data
;
155 if (!msg
->address_lo
&& !msg
->address_hi
)
158 base
+= get_mbigen_vec_reg(d
->hwirq
);
159 val
= readl_relaxed(base
);
161 val
&= ~(IRQ_EVENT_ID_MASK
<< IRQ_EVENT_ID_SHIFT
);
162 val
|= (msg
->data
<< IRQ_EVENT_ID_SHIFT
);
164 /* The address of doorbell is encoded in mbigen register by default
165 * So,we don't need to program the doorbell address at here
167 writel_relaxed(val
, base
);
170 static int mbigen_domain_translate(struct irq_domain
*d
, struct irq_fwspec
*fwspec
,
171 unsigned long *hwirq
, unsigned int *type
)
173 if (is_of_node(fwspec
->fwnode
) || is_acpi_device_node(fwspec
->fwnode
)) {
174 if (fwspec
->param_count
!= 2)
177 if ((fwspec
->param
[0] > MAXIMUM_IRQ_PIN_NUM
) ||
178 (fwspec
->param
[0] < RESERVED_IRQ_PER_MBIGEN_CHIP
))
181 *hwirq
= fwspec
->param
[0];
183 /* If there is no valid irq type, just use the default type */
184 if ((fwspec
->param
[1] == IRQ_TYPE_EDGE_RISING
) ||
185 (fwspec
->param
[1] == IRQ_TYPE_LEVEL_HIGH
))
186 *type
= fwspec
->param
[1];
195 static void mbigen_domain_set_desc(msi_alloc_info_t
*arg
, struct msi_desc
*desc
)
198 arg
->hwirq
= (u32
)desc
->data
.icookie
.value
;
201 static const struct msi_domain_template mbigen_msi_template
= {
204 .irq_mask
= irq_chip_mask_parent
,
205 .irq_unmask
= irq_chip_unmask_parent
,
206 .irq_eoi
= mbigen_eoi_irq
,
207 .irq_set_type
= mbigen_set_type
,
208 .irq_write_msi_msg
= mbigen_write_msi_msg
,
212 .set_desc
= mbigen_domain_set_desc
,
213 .msi_translate
= mbigen_domain_translate
,
217 .bus_token
= DOMAIN_BUS_WIRED_TO_MSI
,
218 .flags
= MSI_FLAG_USE_DEV_FWNODE
,
222 static bool mbigen_create_device_domain(struct device
*dev
, unsigned int size
,
223 struct mbigen_device
*mgn_chip
)
225 if (WARN_ON_ONCE(!dev
->msi
.domain
))
228 return msi_create_device_irq_domain(dev
, MSI_DEFAULT_DOMAIN
,
229 &mbigen_msi_template
, size
,
230 NULL
, mgn_chip
->base
);
233 static int mbigen_of_create_domain(struct platform_device
*pdev
,
234 struct mbigen_device
*mgn_chip
)
236 struct platform_device
*child
;
239 for_each_child_of_node_scoped(pdev
->dev
.of_node
, np
) {
240 if (!of_property_read_bool(np
, "interrupt-controller"))
243 child
= of_platform_device_create(np
, NULL
, NULL
);
247 if (of_property_read_u32(child
->dev
.of_node
, "num-pins",
249 dev_err(&pdev
->dev
, "No num-pins property\n");
253 if (!mbigen_create_device_domain(&child
->dev
, num_pins
, mgn_chip
))
261 static const struct acpi_device_id mbigen_acpi_match
[] = {
265 MODULE_DEVICE_TABLE(acpi
, mbigen_acpi_match
);
267 static int mbigen_acpi_create_domain(struct platform_device
*pdev
,
268 struct mbigen_device
*mgn_chip
)
274 * "num-pins" is the total number of interrupt pins implemented in
275 * this mbigen instance, and mbigen is an interrupt controller
276 * connected to ITS converting wired interrupts into MSI, so we
277 * use "num-pins" to alloc MSI vectors which are needed by client
278 * devices connected to it.
280 * Here is the DSDT device node used for mbigen in firmware:
282 * Name(_HID, "HISI0152")
284 * Name(_CRS, ResourceTemplate() {
285 * Memory32Fixed(ReadWrite, 0xa0080000, 0x10000)
288 * Name(_DSD, Package () {
289 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
291 * Package () {"num-pins", 378}
296 ret
= device_property_read_u32(&pdev
->dev
, "num-pins", &num_pins
);
297 if (ret
|| num_pins
== 0)
300 if (!mbigen_create_device_domain(&pdev
->dev
, num_pins
, mgn_chip
))
306 static inline int mbigen_acpi_create_domain(struct platform_device
*pdev
,
307 struct mbigen_device
*mgn_chip
)
313 static int mbigen_device_probe(struct platform_device
*pdev
)
315 struct mbigen_device
*mgn_chip
;
316 struct resource
*res
;
319 mgn_chip
= devm_kzalloc(&pdev
->dev
, sizeof(*mgn_chip
), GFP_KERNEL
);
323 mgn_chip
->pdev
= pdev
;
325 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
329 mgn_chip
->base
= devm_ioremap(&pdev
->dev
, res
->start
,
331 if (!mgn_chip
->base
) {
332 dev_err(&pdev
->dev
, "failed to ioremap %pR\n", res
);
336 if (IS_ENABLED(CONFIG_OF
) && pdev
->dev
.of_node
)
337 err
= mbigen_of_create_domain(pdev
, mgn_chip
);
338 else if (ACPI_COMPANION(&pdev
->dev
))
339 err
= mbigen_acpi_create_domain(pdev
, mgn_chip
);
344 dev_err(&pdev
->dev
, "Failed to create mbi-gen irqdomain\n");
348 platform_set_drvdata(pdev
, mgn_chip
);
352 static const struct of_device_id mbigen_of_match
[] = {
353 { .compatible
= "hisilicon,mbigen-v2" },
356 MODULE_DEVICE_TABLE(of
, mbigen_of_match
);
358 static struct platform_driver mbigen_platform_driver
= {
360 .name
= "Hisilicon MBIGEN-V2",
361 .of_match_table
= mbigen_of_match
,
362 .acpi_match_table
= ACPI_PTR(mbigen_acpi_match
),
363 .suppress_bind_attrs
= true,
365 .probe
= mbigen_device_probe
,
368 module_platform_driver(mbigen_platform_driver
);
370 MODULE_AUTHOR("Jun Ma <majun258@huawei.com>");
371 MODULE_AUTHOR("Yun Wu <wuyun.wu@huawei.com>");
372 MODULE_DESCRIPTION("HiSilicon MBI Generator driver");