c2fd4f14a732efc4dabf3bc54e3f67786a1a0b64
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PRU-ICSS INTC IRQChip driver for various TI SoCs
4 *
5 * Copyright (C) 2016-2019 Texas Instruments Incorporated - http://www.ti.com/
6 * Andrew F. Davis <afd@ti.com>
7 * Suman Anna <s-anna@ti.com>
8 */
10 #include <linux/irq.h>
11 #include <linux/irqchip/chained_irq.h>
12 #include <linux/irqdomain.h>
13 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/pruss_driver.h>
18 /*
19 * Number of host interrupts reaching the main MPU sub-system. Note that this
20 * is not the same as the total number of host interrupts supported by the PRUSS
21 * INTC instance
22 */
23 #define MAX_HOST_NUM_IRQS 8
25 /* PRU_ICSS_INTC registers */
26 #define PRU_INTC_REVID 0x0000
27 #define PRU_INTC_CR 0x0004
28 #define PRU_INTC_GER 0x0010
29 #define PRU_INTC_GNLR 0x001C
30 #define PRU_INTC_SISR 0x0020
31 #define PRU_INTC_SICR 0x0024
32 #define PRU_INTC_EISR 0x0028
33 #define PRU_INTC_EICR 0x002C
34 #define PRU_INTC_HIEISR 0x0034
35 #define PRU_INTC_HIDISR 0x0038
36 #define PRU_INTC_GPIR 0x0080
37 #define PRU_INTC_SRSR0 0x0200
38 #define PRU_INTC_SRSR1 0x0204
39 #define PRU_INTC_SECR0 0x0280
40 #define PRU_INTC_SECR1 0x0284
41 #define PRU_INTC_ESR0 0x0300
42 #define PRU_INTC_ESR1 0x0304
43 #define PRU_INTC_ECR0 0x0380
44 #define PRU_INTC_ECR1 0x0384
45 #define PRU_INTC_CMR(x) (0x0400 + (x) * 4)
46 #define PRU_INTC_HMR(x) (0x0800 + (x) * 4)
47 #define PRU_INTC_HIPIR(x) (0x0900 + (x) * 4)
48 #define PRU_INTC_SIPR0 0x0D00
49 #define PRU_INTC_SIPR1 0x0D04
50 #define PRU_INTC_SITR0 0x0D80
51 #define PRU_INTC_SITR1 0x0D84
52 #define PRU_INTC_HINLR(x) (0x1100 + (x) * 4)
53 #define PRU_INTC_HIER 0x1500
55 /* HIPIR register bit-fields */
56 #define INTC_HIPIR_NONE_HINT 0x80000000
58 static const char * const irq_names[] = {
59 "host2", "host3", "host4", "host5", "host6", "host7", "host8", "host9",
60 };
62 /**
63 * struct pruss_intc - PRUSS interrupt controller structure
64 * @pruss: back-reference to parent PRUSS structure
65 * @irqs: kernel irq numbers corresponding to PRUSS host interrupts
66 * @base: base virtual address of INTC register space
67 * @irqchip: irq chip for this interrupt controller
68 * @domain: irq domain for this interrupt controller
69 * @config_map: stored INTC configuration mapping data
70 * @lock: mutex to serialize access to INTC
71 * @host_mask: indicate which HOST IRQs are enabled
72 */
73 struct pruss_intc {
74 struct pruss *pruss;
75 unsigned int irqs[MAX_HOST_NUM_IRQS];
76 void __iomem *base;
77 struct irq_chip *irqchip;
78 struct irq_domain *domain;
79 struct pruss_intc_config config_map;
80 struct mutex lock; /* PRUSS INTC lock */
81 u32 host_mask;
82 };
84 static inline u32 pruss_intc_read_reg(struct pruss_intc *intc, unsigned int reg)
85 {
86 return readl_relaxed(intc->base + reg);
87 }
89 static inline void pruss_intc_write_reg(struct pruss_intc *intc,
90 unsigned int reg, u32 val)
91 {
92 writel_relaxed(val, intc->base + reg);
93 }
95 static int pruss_intc_check_write(struct pruss_intc *intc, unsigned int reg,
96 unsigned int sysevent)
97 {
98 if (!intc)
99 return -EINVAL;
101 if (sysevent >= MAX_PRU_SYS_EVENTS)
102 return -EINVAL;
104 pruss_intc_write_reg(intc, reg, sysevent);
106 return 0;
107 }
109 static struct pruss_intc *to_pruss_intc(struct pruss *pruss)
110 {
111 struct device_node *parent = pruss->dev->of_node;
112 struct device_node *np;
113 struct platform_device *pdev;
114 struct pruss_intc *intc = NULL;
116 np = of_get_child_by_name(parent, "interrupt-controller");
117 if (!np) {
118 dev_err(pruss->dev, "pruss does not have an interrupt-controller node\n");
119 return NULL;
120 }
122 pdev = of_find_device_by_node(np);
123 if (!pdev) {
124 dev_err(pruss->dev, "no associated platform device\n");
125 goto out;
126 }
128 intc = platform_get_drvdata(pdev);
129 out:
130 of_node_put(np);
131 return intc;
132 }
134 /**
135 * pruss_intc_configure() - configure the PRUSS INTC
136 * @pruss: the pruss instance
137 * @intc_config: PRU core-specific INTC configuration
138 *
139 * Configures the PRUSS INTC with the provided configuration from
140 * a PRU core. Any existing event to channel mappings or channel to
141 * host interrupt mappings are checked to make sure there are no
142 * conflicting configuration between both the PRU cores. The function
143 * is intended to be used only by the PRU remoteproc driver.
144 *
145 * Returns 0 on success, or a suitable error code otherwise
146 */
147 int pruss_intc_configure(struct pruss *pruss,
148 struct pruss_intc_config *intc_config)
149 {
150 struct device *dev = pruss->dev;
151 struct pruss_intc *intc = to_pruss_intc(pruss);
152 int i, idx, ret;
153 s8 ch, host;
154 u64 sysevt_mask = 0;
155 u32 ch_mask = 0;
156 u32 host_mask = 0;
157 u32 val;
159 if (!intc)
160 return -EINVAL;
162 mutex_lock(&intc->lock);
164 /*
165 * configure channel map registers - each register holds map info
166 * for 4 events, with each event occupying the lower nibble in
167 * a register byte address in little-endian fashion
168 */
169 for (i = 0; i < ARRAY_SIZE(intc_config->sysev_to_ch); i++) {
170 ch = intc_config->sysev_to_ch[i];
171 if (ch < 0)
172 continue;
174 /* check if sysevent already assigned */
175 if (intc->config_map.sysev_to_ch[i] != -1) {
176 dev_err(dev, "event %d (req. channel %d) already assigned to channel %d\n",
177 i, ch, intc->config_map.sysev_to_ch[i]);
178 ret = -EEXIST;
179 goto unlock;
180 }
182 intc->config_map.sysev_to_ch[i] = ch;
184 idx = i / 4;
185 val = pruss_intc_read_reg(intc, PRU_INTC_CMR(idx));
186 val |= ch << ((i & 3) * 8);
187 pruss_intc_write_reg(intc, PRU_INTC_CMR(idx), val);
188 sysevt_mask |= BIT_ULL(i);
189 ch_mask |= BIT(ch);
191 dev_dbg(dev, "SYSEV%d -> CH%d (CMR%d 0x%08x)\n", i, ch, idx,
192 pruss_intc_read_reg(intc, PRU_INTC_CMR(idx)));
193 }
195 /*
196 * set host map registers - each register holds map info for
197 * 4 channels, with each channel occupying the lower nibble in
198 * a register byte address in little-endian fashion
199 */
200 for (i = 0; i < ARRAY_SIZE(intc_config->ch_to_host); i++) {
201 host = intc_config->ch_to_host[i];
202 if (host < 0)
203 continue;
205 /* check if channel already assigned */
206 if (intc->config_map.ch_to_host[i] != -1) {
207 dev_err(dev, "channel %d (req. intr_no %d) already assigned to intr_no %d\n",
208 i, host, intc->config_map.ch_to_host[i]);
209 ret = -EEXIST;
210 goto unlock;
211 }
213 /* check if host intr is already in use by other PRU */
214 if (intc->host_mask & (1U << host)) {
215 dev_err(dev, "%s: host intr %d already in use\n",
216 __func__, host);
217 ret = -EEXIST;
218 goto unlock;
219 }
221 intc->config_map.ch_to_host[i] = host;
223 idx = i / 4;
225 val = pruss_intc_read_reg(intc, PRU_INTC_HMR(idx));
226 val |= host << ((i & 3) * 8);
227 pruss_intc_write_reg(intc, PRU_INTC_HMR(idx), val);
229 ch_mask |= BIT(i);
230 host_mask |= BIT(host);
232 dev_dbg(dev, "CH%d -> HOST%d (HMR%d 0x%08x)\n", i, host, idx,
233 pruss_intc_read_reg(intc, PRU_INTC_HMR(idx)));
234 }
236 dev_info(dev, "configured system_events = 0x%016llx intr_channels = 0x%08x host_intr = 0x%08x\n",
237 sysevt_mask, ch_mask, host_mask);
239 /* enable system events, writing 0 has no-effect */
240 pruss_intc_write_reg(intc, PRU_INTC_ESR0, lower_32_bits(sysevt_mask));
241 pruss_intc_write_reg(intc, PRU_INTC_SECR0, lower_32_bits(sysevt_mask));
242 pruss_intc_write_reg(intc, PRU_INTC_ESR1, upper_32_bits(sysevt_mask));
243 pruss_intc_write_reg(intc, PRU_INTC_SECR1, upper_32_bits(sysevt_mask));
245 /* enable host interrupts */
246 for (i = 0; i < MAX_PRU_HOST_INT; i++) {
247 if (host_mask & BIT(i))
248 pruss_intc_write_reg(intc, PRU_INTC_HIEISR, i);
249 }
251 /* global interrupt enable */
252 pruss_intc_write_reg(intc, PRU_INTC_GER, 1);
254 intc->host_mask |= host_mask;
256 mutex_unlock(&intc->lock);
257 return 0;
259 unlock:
260 mutex_unlock(&intc->lock);
261 return ret;
262 }
263 EXPORT_SYMBOL_GPL(pruss_intc_configure);
265 /**
266 * pruss_intc_unconfigure() - unconfigure the PRUSS INTC
267 * @pruss: the pruss instance
268 * @intc_config: PRU core specific INTC configuration
269 *
270 * Undo whatever was done in pruss_intc_configure() for a PRU core.
271 * It should be sufficient to just mark the resources free in the
272 * global map and disable the host interrupts and sysevents.
273 */
274 int pruss_intc_unconfigure(struct pruss *pruss,
275 struct pruss_intc_config *intc_config)
276 {
277 struct device *dev = pruss->dev;
278 struct pruss_intc *intc = to_pruss_intc(pruss);
279 int i;
280 s8 ch, host;
281 u64 sysevt_mask = 0;
282 u32 host_mask = 0;
284 if (!intc)
285 return -EINVAL;
287 mutex_lock(&intc->lock);
289 for (i = 0; i < ARRAY_SIZE(intc_config->sysev_to_ch); i++) {
290 ch = intc_config->sysev_to_ch[i];
291 if (ch < 0)
292 continue;
294 /* mark sysevent free in global map */
295 intc->config_map.sysev_to_ch[i] = -1;
296 sysevt_mask |= BIT_ULL(i);
297 }
299 for (i = 0; i < ARRAY_SIZE(intc_config->ch_to_host); i++) {
300 host = intc_config->ch_to_host[i];
301 if (host < 0)
302 continue;
304 /* mark channel free in global map */
305 intc->config_map.ch_to_host[i] = -1;
306 host_mask |= BIT(host);
307 }
309 dev_info(dev, "unconfigured system_events = 0x%016llx host_intr = 0x%08x\n",
310 sysevt_mask, host_mask);
312 /* disable system events, writing 0 has no-effect */
313 pruss_intc_write_reg(intc, PRU_INTC_ECR0, lower_32_bits(sysevt_mask));
314 pruss_intc_write_reg(intc, PRU_INTC_ECR1, upper_32_bits(sysevt_mask));
315 /* clear any pending status */
316 pruss_intc_write_reg(intc, PRU_INTC_SECR0, lower_32_bits(sysevt_mask));
317 pruss_intc_write_reg(intc, PRU_INTC_SECR1, upper_32_bits(sysevt_mask));
319 /* disable host interrupts */
320 for (i = 0; i < MAX_PRU_HOST_INT; i++) {
321 if (host_mask & BIT(i))
322 pruss_intc_write_reg(intc, PRU_INTC_HIDISR, i);
323 }
325 intc->host_mask &= ~host_mask;
326 mutex_unlock(&intc->lock);
328 return 0;
329 }
330 EXPORT_SYMBOL_GPL(pruss_intc_unconfigure);
332 static void pruss_intc_init(struct pruss_intc *intc)
333 {
334 int i;
336 /* configure polarity to active high for all system interrupts */
337 pruss_intc_write_reg(intc, PRU_INTC_SIPR0, 0xffffffff);
338 pruss_intc_write_reg(intc, PRU_INTC_SIPR1, 0xffffffff);
340 /* configure type to pulse interrupt for all system interrupts */
341 pruss_intc_write_reg(intc, PRU_INTC_SITR0, 0);
342 pruss_intc_write_reg(intc, PRU_INTC_SITR1, 0);
344 /* clear all 16 interrupt channel map registers */
345 for (i = 0; i < 16; i++)
346 pruss_intc_write_reg(intc, PRU_INTC_CMR(i), 0);
348 /* clear all 3 host interrupt map registers */
349 for (i = 0; i < 3; i++)
350 pruss_intc_write_reg(intc, PRU_INTC_HMR(i), 0);
351 }
353 static void pruss_intc_irq_ack(struct irq_data *data)
354 {
355 struct pruss_intc *intc = irq_data_get_irq_chip_data(data);
356 unsigned int hwirq = data->hwirq;
358 pruss_intc_check_write(intc, PRU_INTC_SICR, hwirq);
359 }
361 static void pruss_intc_irq_mask(struct irq_data *data)
362 {
363 struct pruss_intc *intc = irq_data_get_irq_chip_data(data);
364 unsigned int hwirq = data->hwirq;
366 pruss_intc_check_write(intc, PRU_INTC_EICR, hwirq);
367 }
369 static void pruss_intc_irq_unmask(struct irq_data *data)
370 {
371 struct pruss_intc *intc = irq_data_get_irq_chip_data(data);
372 unsigned int hwirq = data->hwirq;
374 pruss_intc_check_write(intc, PRU_INTC_EISR, hwirq);
375 }
377 static int pruss_intc_irq_retrigger(struct irq_data *data)
378 {
379 struct pruss_intc *intc = irq_data_get_irq_chip_data(data);
380 unsigned int hwirq = data->hwirq;
382 return pruss_intc_check_write(intc, PRU_INTC_SISR, hwirq);
383 }
385 static int pruss_intc_irq_reqres(struct irq_data *data)
386 {
387 if (!try_module_get(THIS_MODULE))
388 return -ENODEV;
390 return 0;
391 }
393 static void pruss_intc_irq_relres(struct irq_data *data)
394 {
395 module_put(THIS_MODULE);
396 }
398 /**
399 * pruss_intc_trigger() - trigger a PRU system event
400 * @irq: linux IRQ number associated with a PRU system event
401 *
402 * Trigger an interrupt by signalling a specific PRU system event.
403 * This can be used by PRUSS client users to raise/send an event to
404 * a PRU or any other core that is listening on the host interrupt
405 * mapped to that specific PRU system event. The @irq variable is the
406 * Linux IRQ number associated with a specific PRU system event that
407 * a client user/application uses. The interrupt mappings for this is
408 * provided by the PRUSS INTC irqchip instance.
409 *
410 * Returns 0 on success, or an error value upon failure.
411 */
412 int pruss_intc_trigger(unsigned int irq)
413 {
414 struct irq_desc *desc;
416 if (irq <= 0)
417 return -EINVAL;
419 desc = irq_to_desc(irq);
420 if (!desc)
421 return -EINVAL;
423 pruss_intc_irq_retrigger(&desc->irq_data);
425 return 0;
426 }
427 EXPORT_SYMBOL_GPL(pruss_intc_trigger);
429 static int pruss_intc_irq_domain_map(struct irq_domain *d, unsigned int virq,
430 irq_hw_number_t hw)
431 {
432 struct pruss_intc *intc = d->host_data;
434 irq_set_chip_data(virq, intc);
435 irq_set_chip_and_handler(virq, intc->irqchip, handle_level_irq);
437 return 0;
438 }
440 static void pruss_intc_irq_domain_unmap(struct irq_domain *d, unsigned int virq)
441 {
442 irq_set_chip_and_handler(virq, NULL, NULL);
443 irq_set_chip_data(virq, NULL);
444 }
446 static const struct irq_domain_ops pruss_intc_irq_domain_ops = {
447 .xlate = irq_domain_xlate_onecell,
448 .map = pruss_intc_irq_domain_map,
449 .unmap = pruss_intc_irq_domain_unmap,
450 };
452 static void pruss_intc_irq_handler(struct irq_desc *desc)
453 {
454 unsigned int irq = irq_desc_get_irq(desc);
455 struct irq_chip *chip = irq_desc_get_chip(desc);
456 struct pruss_intc *intc = irq_get_handler_data(irq);
457 u32 hipir;
458 unsigned int virq;
459 int i, hwirq;
461 chained_irq_enter(chip, desc);
463 /* find our host irq number */
464 for (i = 0; i < MAX_HOST_NUM_IRQS; i++)
465 if (intc->irqs[i] == irq)
466 break;
467 if (i == MAX_HOST_NUM_IRQS)
468 goto err;
470 i += MIN_PRU_HOST_INT;
472 /* get highest priority pending PRUSS system event */
473 hipir = pruss_intc_read_reg(intc, PRU_INTC_HIPIR(i));
474 while (!(hipir & BIT(31))) {
475 hwirq = hipir & GENMASK(9, 0);
476 virq = irq_linear_revmap(intc->domain, hwirq);
478 /*
479 * XXX: manually ACK any system events that do not have a
480 * handler mapped yet
481 */
482 if (unlikely(!virq))
483 pruss_intc_check_write(intc, PRU_INTC_SICR, hwirq);
484 else
485 generic_handle_irq(virq);
487 /* get next system event */
488 hipir = pruss_intc_read_reg(intc, PRU_INTC_HIPIR(i));
489 }
490 err:
491 chained_irq_exit(chip, desc);
492 }
494 static int pruss_intc_probe(struct platform_device *pdev)
495 {
496 struct device *dev = &pdev->dev;
497 struct platform_device *ppdev = to_platform_device(dev->parent);
498 struct pruss_intc *intc;
499 struct resource *res;
500 struct irq_chip *irqchip;
501 int i, irq;
503 intc = devm_kzalloc(dev, sizeof(*intc), GFP_KERNEL);
504 if (!intc)
505 return -ENOMEM;
506 platform_set_drvdata(pdev, intc);
508 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
509 intc->base = devm_ioremap_resource(dev, res);
510 if (IS_ERR(intc->base)) {
511 dev_err(dev, "failed to parse and map intc memory resource\n");
512 return PTR_ERR(intc->base);
513 }
515 dev_dbg(dev, "intc memory: pa %pa size 0x%zx va %pK\n", &res->start,
516 (size_t)resource_size(res), intc->base);
518 mutex_init(&intc->lock);
520 for (i = 0; i < ARRAY_SIZE(intc->config_map.sysev_to_ch); i++)
521 intc->config_map.sysev_to_ch[i] = -1;
523 for (i = 0; i < ARRAY_SIZE(intc->config_map.ch_to_host); i++)
524 intc->config_map.ch_to_host[i] = -1;
526 intc->pruss = platform_get_drvdata(ppdev);
527 pruss_intc_init(intc);
529 irqchip = devm_kzalloc(dev, sizeof(*irqchip), GFP_KERNEL);
530 if (!irqchip)
531 return -ENOMEM;
533 irqchip->irq_ack = pruss_intc_irq_ack;
534 irqchip->irq_mask = pruss_intc_irq_mask;
535 irqchip->irq_unmask = pruss_intc_irq_unmask;
536 irqchip->irq_retrigger = pruss_intc_irq_retrigger;
537 irqchip->irq_request_resources = pruss_intc_irq_reqres;
538 irqchip->irq_release_resources = pruss_intc_irq_relres;
539 irqchip->name = dev_name(dev);
540 intc->irqchip = irqchip;
542 /* always 64 events */
543 intc->domain = irq_domain_add_linear(dev->of_node, MAX_PRU_SYS_EVENTS,
544 &pruss_intc_irq_domain_ops, intc);
545 if (!intc->domain)
546 return -ENOMEM;
548 for (i = 0; i < MAX_HOST_NUM_IRQS; i++) {
549 irq = platform_get_irq_byname(ppdev, irq_names[i]);
550 if (irq < 0) {
551 dev_err(dev->parent, "platform_get_irq_byname failed for %s : %d\n",
552 irq_names[i], irq);
553 goto fail_irq;
554 }
556 intc->irqs[i] = irq;
557 irq_set_handler_data(irq, intc);
558 irq_set_chained_handler(irq, pruss_intc_irq_handler);
559 }
561 return 0;
563 fail_irq:
564 irq_domain_remove(intc->domain);
565 return irq;
566 }
568 static int pruss_intc_remove(struct platform_device *pdev)
569 {
570 struct pruss_intc *intc = platform_get_drvdata(pdev);
571 unsigned int hwirq;
573 if (intc->domain) {
574 for (hwirq = 0; hwirq < MAX_PRU_SYS_EVENTS; hwirq++)
575 irq_dispose_mapping(irq_find_mapping(intc->domain,
576 hwirq));
577 irq_domain_remove(intc->domain);
578 }
580 return 0;
581 }
583 static const struct of_device_id pruss_intc_of_match[] = {
584 { .compatible = "ti,am3356-pruss-intc", },
585 { /* sentinel */ },
586 };
587 MODULE_DEVICE_TABLE(of, pruss_intc_of_match);
589 static struct platform_driver pruss_intc_driver = {
590 .driver = {
591 .name = "pruss-intc",
592 .of_match_table = pruss_intc_of_match,
593 },
594 .probe = pruss_intc_probe,
595 .remove = pruss_intc_remove,
596 };
597 module_platform_driver(pruss_intc_driver);
599 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
600 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
601 MODULE_DESCRIPTION("PRU-ICSS INTC Driver");
602 MODULE_LICENSE("GPL v2");