summaryrefslogtreecommitdiff
path: root/drivers/irqchip/irq-lan966x-oic.c
blob: 41ac880e3b87d0606348b7f9ee022ed41570c567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// SPDX-License-Identifier: GPL-2.0
/*
 * Driver for the Microchip LAN966x outbound interrupt controller
 *
 * Copyright (c) 2024 Technology Inc. and its subsidiaries.
 *
 * Authors:
 *	Horatiu Vultur <horatiu.vultur@microchip.com>
 *	Clément Léger <clement.leger@bootlin.com>
 *	Herve Codina <herve.codina@bootlin.com>
 */

#include <linux/interrupt.h>
#include <linux/irqchip/chained_irq.h>
#include <linux/irqchip.h>
#include <linux/irq.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>

struct lan966x_oic_chip_regs {
	int reg_off_ena_set;
	int reg_off_ena_clr;
	int reg_off_sticky;
	int reg_off_ident;
	int reg_off_map;
};

struct lan966x_oic_data {
	void __iomem	*regs;
	int		irq;
};

#define LAN966X_OIC_NR_IRQ 86

/* Interrupt sticky status */
#define LAN966X_OIC_INTR_STICKY		0x30
#define LAN966X_OIC_INTR_STICKY1	0x34
#define LAN966X_OIC_INTR_STICKY2	0x38

/* Interrupt enable */
#define LAN966X_OIC_INTR_ENA		0x48
#define LAN966X_OIC_INTR_ENA1		0x4c
#define LAN966X_OIC_INTR_ENA2		0x50

/* Atomic clear of interrupt enable */
#define LAN966X_OIC_INTR_ENA_CLR	0x54
#define LAN966X_OIC_INTR_ENA_CLR1	0x58
#define LAN966X_OIC_INTR_ENA_CLR2	0x5c

/* Atomic set of interrupt */
#define LAN966X_OIC_INTR_ENA_SET	0x60
#define LAN966X_OIC_INTR_ENA_SET1	0x64
#define LAN966X_OIC_INTR_ENA_SET2	0x68

/* Mapping of source to destination interrupts (_n = 0..8) */
#define LAN966X_OIC_DST_INTR_MAP(_n)	(0x78 + (_n) * 4)
#define LAN966X_OIC_DST_INTR_MAP1(_n)	(0x9c + (_n) * 4)
#define LAN966X_OIC_DST_INTR_MAP2(_n)	(0xc0 + (_n) * 4)

/* Currently active interrupt sources per destination (_n = 0..8) */
#define LAN966X_OIC_DST_INTR_IDENT(_n)	(0xe4 + (_n) * 4)
#define LAN966X_OIC_DST_INTR_IDENT1(_n)	(0x108 + (_n) * 4)
#define LAN966X_OIC_DST_INTR_IDENT2(_n)	(0x12c + (_n) * 4)

static unsigned int lan966x_oic_irq_startup(struct irq_data *data)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
	struct irq_chip_type *ct = irq_data_get_chip_type(data);
	struct lan966x_oic_chip_regs *chip_regs = gc->private;
	u32 map;

	irq_gc_lock(gc);

	/* Map the source interrupt to the destination */
	map = irq_reg_readl(gc, chip_regs->reg_off_map);
	map |= data->mask;
	irq_reg_writel(gc, map, chip_regs->reg_off_map);

	irq_gc_unlock(gc);

	ct->chip.irq_ack(data);
	ct->chip.irq_unmask(data);

	return 0;
}

static void lan966x_oic_irq_shutdown(struct irq_data *data)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
	struct irq_chip_type *ct = irq_data_get_chip_type(data);
	struct lan966x_oic_chip_regs *chip_regs = gc->private;
	u32 map;

	ct->chip.irq_mask(data);

	irq_gc_lock(gc);

	/* Unmap the interrupt */
	map = irq_reg_readl(gc, chip_regs->reg_off_map);
	map &= ~data->mask;
	irq_reg_writel(gc, map, chip_regs->reg_off_map);

	irq_gc_unlock(gc);
}

static int lan966x_oic_irq_set_type(struct irq_data *data,
				    unsigned int flow_type)
{
	if (flow_type != IRQ_TYPE_LEVEL_HIGH) {
		pr_err("lan966x oic doesn't support flow type %d\n", flow_type);
		return -EINVAL;
	}

	return 0;
}

static void lan966x_oic_irq_handler_domain(struct irq_domain *d, u32 first_irq)
{
	struct irq_chip_generic *gc = irq_get_domain_generic_chip(d, first_irq);
	struct lan966x_oic_chip_regs *chip_regs = gc->private;
	unsigned long ident;
	unsigned int hwirq;

	ident = irq_reg_readl(gc, chip_regs->reg_off_ident);
	if (!ident)
		return;

	for_each_set_bit(hwirq, &ident, 32)
		generic_handle_domain_irq(d, hwirq + first_irq);
}

static void lan966x_oic_irq_handler(struct irq_desc *desc)
{
	struct irq_domain *d = irq_desc_get_handler_data(desc);
	struct irq_chip *chip = irq_desc_get_chip(desc);

	chained_irq_enter(chip, desc);
	lan966x_oic_irq_handler_domain(d, 0);
	lan966x_oic_irq_handler_domain(d, 32);
	lan966x_oic_irq_handler_domain(d, 64);
	chained_irq_exit(chip, desc);
}

static struct lan966x_oic_chip_regs lan966x_oic_chip_regs[3] = {
	{
		.reg_off_ena_set = LAN966X_OIC_INTR_ENA_SET,
		.reg_off_ena_clr = LAN966X_OIC_INTR_ENA_CLR,
		.reg_off_sticky  = LAN966X_OIC_INTR_STICKY,
		.reg_off_ident   = LAN966X_OIC_DST_INTR_IDENT(0),
		.reg_off_map     = LAN966X_OIC_DST_INTR_MAP(0),
	}, {
		.reg_off_ena_set = LAN966X_OIC_INTR_ENA_SET1,
		.reg_off_ena_clr = LAN966X_OIC_INTR_ENA_CLR1,
		.reg_off_sticky  = LAN966X_OIC_INTR_STICKY1,
		.reg_off_ident   = LAN966X_OIC_DST_INTR_IDENT1(0),
		.reg_off_map     = LAN966X_OIC_DST_INTR_MAP1(0),
	}, {
		.reg_off_ena_set = LAN966X_OIC_INTR_ENA_SET2,
		.reg_off_ena_clr = LAN966X_OIC_INTR_ENA_CLR2,
		.reg_off_sticky  = LAN966X_OIC_INTR_STICKY2,
		.reg_off_ident   = LAN966X_OIC_DST_INTR_IDENT2(0),
		.reg_off_map     = LAN966X_OIC_DST_INTR_MAP2(0),
	}
};

static int lan966x_oic_chip_init(struct irq_chip_generic *gc)
{
	struct lan966x_oic_data *lan966x_oic = gc->domain->host_data;
	struct lan966x_oic_chip_regs *chip_regs;

	chip_regs = &lan966x_oic_chip_regs[gc->irq_base / 32];

	gc->reg_base = lan966x_oic->regs;
	gc->chip_types[0].regs.enable = chip_regs->reg_off_ena_set;
	gc->chip_types[0].regs.disable = chip_regs->reg_off_ena_clr;
	gc->chip_types[0].regs.ack = chip_regs->reg_off_sticky;
	gc->chip_types[0].chip.irq_startup = lan966x_oic_irq_startup;
	gc->chip_types[0].chip.irq_shutdown = lan966x_oic_irq_shutdown;
	gc->chip_types[0].chip.irq_set_type = lan966x_oic_irq_set_type;
	gc->chip_types[0].chip.irq_mask = irq_gc_mask_disable_reg;
	gc->chip_types[0].chip.irq_unmask = irq_gc_unmask_enable_reg;
	gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
	gc->private = chip_regs;

	/* Disable all interrupts handled by this chip */
	irq_reg_writel(gc, ~0U, chip_regs->reg_off_ena_clr);

	return 0;
}

static void lan966x_oic_chip_exit(struct irq_chip_generic *gc)
{
	/* Disable and ack all interrupts handled by this chip */
	irq_reg_writel(gc, ~0U, gc->chip_types[0].regs.disable);
	irq_reg_writel(gc, ~0U, gc->chip_types[0].regs.ack);
}

static int lan966x_oic_domain_init(struct irq_domain *d)
{
	struct lan966x_oic_data *lan966x_oic = d->host_data;

	irq_set_chained_handler_and_data(lan966x_oic->irq, lan966x_oic_irq_handler, d);

	return 0;
}

static void lan966x_oic_domain_exit(struct irq_domain *d)
{
	struct lan966x_oic_data *lan966x_oic = d->host_data;

	irq_set_chained_handler_and_data(lan966x_oic->irq, NULL, NULL);
}

static int lan966x_oic_probe(struct platform_device *pdev)
{
	struct irq_domain_chip_generic_info dgc_info = {
		.name		= "lan966x-oic",
		.handler	= handle_level_irq,
		.irqs_per_chip	= 32,
		.num_ct		= 1,
		.init		= lan966x_oic_chip_init,
		.exit		= lan966x_oic_chip_exit,
	};
	struct irq_domain_info d_info = {
		.fwnode		= of_node_to_fwnode(pdev->dev.of_node),
		.domain_flags	= IRQ_DOMAIN_FLAG_DESTROY_GC,
		.size		= LAN966X_OIC_NR_IRQ,
		.hwirq_max	= LAN966X_OIC_NR_IRQ,
		.ops		= &irq_generic_chip_ops,
		.dgc_info	= &dgc_info,
		.init		= lan966x_oic_domain_init,
		.exit		= lan966x_oic_domain_exit,
	};
	struct lan966x_oic_data *lan966x_oic;
	struct device *dev = &pdev->dev;
	struct irq_domain *domain;

	lan966x_oic = devm_kmalloc(dev, sizeof(*lan966x_oic), GFP_KERNEL);
	if (!lan966x_oic)
		return -ENOMEM;

	lan966x_oic->regs = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(lan966x_oic->regs))
		return dev_err_probe(dev, PTR_ERR(lan966x_oic->regs),
				     "failed to map resource\n");

	lan966x_oic->irq = platform_get_irq(pdev, 0);
	if (lan966x_oic->irq < 0)
		return dev_err_probe(dev, lan966x_oic->irq, "failed to get the IRQ\n");

	d_info.host_data = lan966x_oic;
	domain = devm_irq_domain_instantiate(dev, &d_info);
	if (IS_ERR(domain))
		return dev_err_probe(dev, PTR_ERR(domain),
				     "failed to instantiate the IRQ domain\n");
	return 0;
}

static const struct of_device_id lan966x_oic_of_match[] = {
	{ .compatible = "microchip,lan966x-oic" },
	{} /* sentinel */
};
MODULE_DEVICE_TABLE(of, lan966x_oic_of_match);

static struct platform_driver lan966x_oic_driver = {
	.probe = lan966x_oic_probe,
	.driver = {
		.name = "lan966x-oic",
		.of_match_table = lan966x_oic_of_match,
	},
};
module_platform_driver(lan966x_oic_driver);

MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>");
MODULE_DESCRIPTION("Microchip LAN966x OIC driver");
MODULE_LICENSE("GPL");