18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2012 Freescale Semiconductor, Inc.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author: Varun Sethi <varun.sethi@freescale.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/irq.h>
98c2ecf20Sopenharmony_ci#include <linux/smp.h>
108c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <asm/io.h>
138c2ecf20Sopenharmony_ci#include <asm/irq.h>
148c2ecf20Sopenharmony_ci#include <asm/mpic.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "mpic.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define MPIC_ERR_INT_BASE	0x3900
198c2ecf20Sopenharmony_ci#define MPIC_ERR_INT_EISR	0x0000
208c2ecf20Sopenharmony_ci#define MPIC_ERR_INT_EIMR	0x0010
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic inline u32 mpic_fsl_err_read(u32 __iomem *base, unsigned int err_reg)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	return in_be32(base + (err_reg >> 2));
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic inline void mpic_fsl_err_write(u32 __iomem *base, u32 value)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	out_be32(base + (MPIC_ERR_INT_EIMR >> 2), value);
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic void fsl_mpic_mask_err(struct irq_data *d)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	u32 eimr;
358c2ecf20Sopenharmony_ci	struct mpic *mpic = irq_data_get_irq_chip_data(d);
368c2ecf20Sopenharmony_ci	unsigned int src = virq_to_hw(d->irq) - mpic->err_int_vecs[0];
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	eimr = mpic_fsl_err_read(mpic->err_regs, MPIC_ERR_INT_EIMR);
398c2ecf20Sopenharmony_ci	eimr |= (1 << (31 - src));
408c2ecf20Sopenharmony_ci	mpic_fsl_err_write(mpic->err_regs, eimr);
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic void fsl_mpic_unmask_err(struct irq_data *d)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	u32 eimr;
468c2ecf20Sopenharmony_ci	struct mpic *mpic = irq_data_get_irq_chip_data(d);
478c2ecf20Sopenharmony_ci	unsigned int src = virq_to_hw(d->irq) - mpic->err_int_vecs[0];
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	eimr = mpic_fsl_err_read(mpic->err_regs, MPIC_ERR_INT_EIMR);
508c2ecf20Sopenharmony_ci	eimr &= ~(1 << (31 - src));
518c2ecf20Sopenharmony_ci	mpic_fsl_err_write(mpic->err_regs, eimr);
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic struct irq_chip fsl_mpic_err_chip = {
558c2ecf20Sopenharmony_ci	.irq_disable	= fsl_mpic_mask_err,
568c2ecf20Sopenharmony_ci	.irq_mask	= fsl_mpic_mask_err,
578c2ecf20Sopenharmony_ci	.irq_unmask	= fsl_mpic_unmask_err,
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciint mpic_setup_error_int(struct mpic *mpic, int intvec)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	int i;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	mpic->err_regs = ioremap(mpic->paddr + MPIC_ERR_INT_BASE, 0x1000);
658c2ecf20Sopenharmony_ci	if (!mpic->err_regs) {
668c2ecf20Sopenharmony_ci		pr_err("could not map mpic error registers\n");
678c2ecf20Sopenharmony_ci		return -ENOMEM;
688c2ecf20Sopenharmony_ci	}
698c2ecf20Sopenharmony_ci	mpic->hc_err = fsl_mpic_err_chip;
708c2ecf20Sopenharmony_ci	mpic->hc_err.name = mpic->name;
718c2ecf20Sopenharmony_ci	mpic->flags |= MPIC_FSL_HAS_EIMR;
728c2ecf20Sopenharmony_ci	/* allocate interrupt vectors for error interrupts */
738c2ecf20Sopenharmony_ci	for (i = MPIC_MAX_ERR - 1; i >= 0; i--)
748c2ecf20Sopenharmony_ci		mpic->err_int_vecs[i] = intvec--;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	return 0;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ciint mpic_map_error_int(struct mpic *mpic, unsigned int virq, irq_hw_number_t  hw)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	if ((mpic->flags & MPIC_FSL_HAS_EIMR) &&
828c2ecf20Sopenharmony_ci	    (hw >= mpic->err_int_vecs[0] &&
838c2ecf20Sopenharmony_ci	     hw <= mpic->err_int_vecs[MPIC_MAX_ERR - 1])) {
848c2ecf20Sopenharmony_ci		WARN_ON(mpic->flags & MPIC_SECONDARY);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci		pr_debug("mpic: mapping as Error Interrupt\n");
878c2ecf20Sopenharmony_ci		irq_set_chip_data(virq, mpic);
888c2ecf20Sopenharmony_ci		irq_set_chip_and_handler(virq, &mpic->hc_err,
898c2ecf20Sopenharmony_ci					 handle_level_irq);
908c2ecf20Sopenharmony_ci		return 1;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	return 0;
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic irqreturn_t fsl_error_int_handler(int irq, void *data)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	struct mpic *mpic = (struct mpic *) data;
998c2ecf20Sopenharmony_ci	u32 eisr, eimr;
1008c2ecf20Sopenharmony_ci	int errint;
1018c2ecf20Sopenharmony_ci	unsigned int cascade_irq;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	eisr = mpic_fsl_err_read(mpic->err_regs, MPIC_ERR_INT_EISR);
1048c2ecf20Sopenharmony_ci	eimr = mpic_fsl_err_read(mpic->err_regs, MPIC_ERR_INT_EIMR);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	if (!(eisr & ~eimr))
1078c2ecf20Sopenharmony_ci		return IRQ_NONE;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	while (eisr) {
1108c2ecf20Sopenharmony_ci		errint = __builtin_clz(eisr);
1118c2ecf20Sopenharmony_ci		cascade_irq = irq_linear_revmap(mpic->irqhost,
1128c2ecf20Sopenharmony_ci				 mpic->err_int_vecs[errint]);
1138c2ecf20Sopenharmony_ci		WARN_ON(!cascade_irq);
1148c2ecf20Sopenharmony_ci		if (cascade_irq) {
1158c2ecf20Sopenharmony_ci			generic_handle_irq(cascade_irq);
1168c2ecf20Sopenharmony_ci		} else {
1178c2ecf20Sopenharmony_ci			eimr |=  1 << (31 - errint);
1188c2ecf20Sopenharmony_ci			mpic_fsl_err_write(mpic->err_regs, eimr);
1198c2ecf20Sopenharmony_ci		}
1208c2ecf20Sopenharmony_ci		eisr &= ~(1 << (31 - errint));
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_civoid mpic_err_int_init(struct mpic *mpic, irq_hw_number_t irqnum)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	unsigned int virq;
1298c2ecf20Sopenharmony_ci	int ret;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	virq = irq_create_mapping(mpic->irqhost, irqnum);
1328c2ecf20Sopenharmony_ci	if (!virq) {
1338c2ecf20Sopenharmony_ci		pr_err("Error interrupt setup failed\n");
1348c2ecf20Sopenharmony_ci		return;
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	/* Mask all error interrupts */
1388c2ecf20Sopenharmony_ci	mpic_fsl_err_write(mpic->err_regs, ~0);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	ret = request_irq(virq, fsl_error_int_handler, IRQF_NO_THREAD,
1418c2ecf20Sopenharmony_ci		    "mpic-error-int", mpic);
1428c2ecf20Sopenharmony_ci	if (ret)
1438c2ecf20Sopenharmony_ci		pr_err("Failed to register error interrupt handler\n");
1448c2ecf20Sopenharmony_ci}
145