18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// Copyright (C) 2006, 2019 Texas Instruments.
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// Interrupt handler for DaVinci boards.
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/init.h>
98c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
108c2ecf20Sopenharmony_ci#include <linux/irq.h>
118c2ecf20Sopenharmony_ci#include <linux/irqchip/irq-davinci-aintc.h>
128c2ecf20Sopenharmony_ci#include <linux/io.h>
138c2ecf20Sopenharmony_ci#include <linux/irqdomain.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <asm/exception.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define DAVINCI_AINTC_FIQ_REG0		0x00
188c2ecf20Sopenharmony_ci#define DAVINCI_AINTC_FIQ_REG1		0x04
198c2ecf20Sopenharmony_ci#define DAVINCI_AINTC_IRQ_REG0		0x08
208c2ecf20Sopenharmony_ci#define DAVINCI_AINTC_IRQ_REG1		0x0c
218c2ecf20Sopenharmony_ci#define DAVINCI_AINTC_IRQ_IRQENTRY	0x14
228c2ecf20Sopenharmony_ci#define DAVINCI_AINTC_IRQ_ENT_REG0	0x18
238c2ecf20Sopenharmony_ci#define DAVINCI_AINTC_IRQ_ENT_REG1	0x1c
248c2ecf20Sopenharmony_ci#define DAVINCI_AINTC_IRQ_INCTL_REG	0x20
258c2ecf20Sopenharmony_ci#define DAVINCI_AINTC_IRQ_EABASE_REG	0x24
268c2ecf20Sopenharmony_ci#define DAVINCI_AINTC_IRQ_INTPRI0_REG	0x30
278c2ecf20Sopenharmony_ci#define DAVINCI_AINTC_IRQ_INTPRI7_REG	0x4c
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic void __iomem *davinci_aintc_base;
308c2ecf20Sopenharmony_cistatic struct irq_domain *davinci_aintc_irq_domain;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic inline void davinci_aintc_writel(unsigned long value, int offset)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	writel_relaxed(value, davinci_aintc_base + offset);
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic inline unsigned long davinci_aintc_readl(int offset)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	return readl_relaxed(davinci_aintc_base + offset);
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic __init void
438c2ecf20Sopenharmony_cidavinci_aintc_setup_gc(void __iomem *base,
448c2ecf20Sopenharmony_ci		       unsigned int irq_start, unsigned int num)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc;
478c2ecf20Sopenharmony_ci	struct irq_chip_type *ct;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	gc = irq_get_domain_generic_chip(davinci_aintc_irq_domain, irq_start);
508c2ecf20Sopenharmony_ci	gc->reg_base = base;
518c2ecf20Sopenharmony_ci	gc->irq_base = irq_start;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	ct = gc->chip_types;
548c2ecf20Sopenharmony_ci	ct->chip.irq_ack = irq_gc_ack_set_bit;
558c2ecf20Sopenharmony_ci	ct->chip.irq_mask = irq_gc_mask_clr_bit;
568c2ecf20Sopenharmony_ci	ct->chip.irq_unmask = irq_gc_mask_set_bit;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	ct->regs.ack = DAVINCI_AINTC_IRQ_REG0;
598c2ecf20Sopenharmony_ci	ct->regs.mask = DAVINCI_AINTC_IRQ_ENT_REG0;
608c2ecf20Sopenharmony_ci	irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
618c2ecf20Sopenharmony_ci			       IRQ_NOREQUEST | IRQ_NOPROBE, 0);
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic asmlinkage void __exception_irq_entry
658c2ecf20Sopenharmony_cidavinci_aintc_handle_irq(struct pt_regs *regs)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	int irqnr = davinci_aintc_readl(DAVINCI_AINTC_IRQ_IRQENTRY);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/*
708c2ecf20Sopenharmony_ci	 * Use the formula for entry vector index generation from section
718c2ecf20Sopenharmony_ci	 * 8.3.3 of the manual.
728c2ecf20Sopenharmony_ci	 */
738c2ecf20Sopenharmony_ci	irqnr >>= 2;
748c2ecf20Sopenharmony_ci	irqnr -= 1;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	handle_domain_irq(davinci_aintc_irq_domain, irqnr, regs);
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/* ARM Interrupt Controller Initialization */
808c2ecf20Sopenharmony_civoid __init davinci_aintc_init(const struct davinci_aintc_config *config)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	unsigned int irq_off, reg_off, prio, shift;
838c2ecf20Sopenharmony_ci	void __iomem *req;
848c2ecf20Sopenharmony_ci	int ret, irq_base;
858c2ecf20Sopenharmony_ci	const u8 *prios;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	req = request_mem_region(config->reg.start,
888c2ecf20Sopenharmony_ci				 resource_size(&config->reg),
898c2ecf20Sopenharmony_ci				 "davinci-cp-intc");
908c2ecf20Sopenharmony_ci	if (!req) {
918c2ecf20Sopenharmony_ci		pr_err("%s: register range busy\n", __func__);
928c2ecf20Sopenharmony_ci		return;
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	davinci_aintc_base = ioremap(config->reg.start,
968c2ecf20Sopenharmony_ci				     resource_size(&config->reg));
978c2ecf20Sopenharmony_ci	if (!davinci_aintc_base) {
988c2ecf20Sopenharmony_ci		pr_err("%s: unable to ioremap register range\n", __func__);
998c2ecf20Sopenharmony_ci		return;
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	/* Clear all interrupt requests */
1038c2ecf20Sopenharmony_ci	davinci_aintc_writel(~0x0, DAVINCI_AINTC_FIQ_REG0);
1048c2ecf20Sopenharmony_ci	davinci_aintc_writel(~0x0, DAVINCI_AINTC_FIQ_REG1);
1058c2ecf20Sopenharmony_ci	davinci_aintc_writel(~0x0, DAVINCI_AINTC_IRQ_REG0);
1068c2ecf20Sopenharmony_ci	davinci_aintc_writel(~0x0, DAVINCI_AINTC_IRQ_REG1);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/* Disable all interrupts */
1098c2ecf20Sopenharmony_ci	davinci_aintc_writel(0x0, DAVINCI_AINTC_IRQ_ENT_REG0);
1108c2ecf20Sopenharmony_ci	davinci_aintc_writel(0x0, DAVINCI_AINTC_IRQ_ENT_REG1);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	/* Interrupts disabled immediately, IRQ entry reflects all */
1138c2ecf20Sopenharmony_ci	davinci_aintc_writel(0x0, DAVINCI_AINTC_IRQ_INCTL_REG);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	/* we don't use the hardware vector table, just its entry addresses */
1168c2ecf20Sopenharmony_ci	davinci_aintc_writel(0, DAVINCI_AINTC_IRQ_EABASE_REG);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	/* Clear all interrupt requests */
1198c2ecf20Sopenharmony_ci	davinci_aintc_writel(~0x0, DAVINCI_AINTC_FIQ_REG0);
1208c2ecf20Sopenharmony_ci	davinci_aintc_writel(~0x0, DAVINCI_AINTC_FIQ_REG1);
1218c2ecf20Sopenharmony_ci	davinci_aintc_writel(~0x0, DAVINCI_AINTC_IRQ_REG0);
1228c2ecf20Sopenharmony_ci	davinci_aintc_writel(~0x0, DAVINCI_AINTC_IRQ_REG1);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	prios = config->prios;
1258c2ecf20Sopenharmony_ci	for (reg_off = DAVINCI_AINTC_IRQ_INTPRI0_REG;
1268c2ecf20Sopenharmony_ci	     reg_off <= DAVINCI_AINTC_IRQ_INTPRI7_REG; reg_off += 4) {
1278c2ecf20Sopenharmony_ci		for (shift = 0, prio = 0; shift < 32; shift += 4, prios++)
1288c2ecf20Sopenharmony_ci			prio |= (*prios & 0x07) << shift;
1298c2ecf20Sopenharmony_ci		davinci_aintc_writel(prio, reg_off);
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	irq_base = irq_alloc_descs(-1, 0, config->num_irqs, 0);
1338c2ecf20Sopenharmony_ci	if (irq_base < 0) {
1348c2ecf20Sopenharmony_ci		pr_err("%s: unable to allocate interrupt descriptors: %d\n",
1358c2ecf20Sopenharmony_ci		       __func__, irq_base);
1368c2ecf20Sopenharmony_ci		return;
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	davinci_aintc_irq_domain = irq_domain_add_legacy(NULL,
1408c2ecf20Sopenharmony_ci						config->num_irqs, irq_base, 0,
1418c2ecf20Sopenharmony_ci						&irq_domain_simple_ops, NULL);
1428c2ecf20Sopenharmony_ci	if (!davinci_aintc_irq_domain) {
1438c2ecf20Sopenharmony_ci		pr_err("%s: unable to create interrupt domain\n", __func__);
1448c2ecf20Sopenharmony_ci		return;
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	ret = irq_alloc_domain_generic_chips(davinci_aintc_irq_domain, 32, 1,
1488c2ecf20Sopenharmony_ci					     "AINTC", handle_edge_irq,
1498c2ecf20Sopenharmony_ci					     IRQ_NOREQUEST | IRQ_NOPROBE, 0, 0);
1508c2ecf20Sopenharmony_ci	if (ret) {
1518c2ecf20Sopenharmony_ci		pr_err("%s: unable to allocate generic irq chips for domain\n",
1528c2ecf20Sopenharmony_ci		       __func__);
1538c2ecf20Sopenharmony_ci		return;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	for (irq_off = 0, reg_off = 0;
1578c2ecf20Sopenharmony_ci	     irq_off < config->num_irqs;
1588c2ecf20Sopenharmony_ci	     irq_off += 32, reg_off += 0x04)
1598c2ecf20Sopenharmony_ci		davinci_aintc_setup_gc(davinci_aintc_base + reg_off,
1608c2ecf20Sopenharmony_ci				       irq_base + irq_off, 32);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	set_handle_irq(davinci_aintc_handle_irq);
1638c2ecf20Sopenharmony_ci}
164