18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * linux/arch/arm/mach-omap1/fpga.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Interrupt handler for OMAP-1510 Innovator FPGA
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2001 RidgeRun, Inc.
88c2ecf20Sopenharmony_ci * Author: Greg Lonnon <glonnon@ridgerun.com>
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Copyright (C) 2002 MontaVista Software, Inc.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Separated FPGA interrupts from innovator1510.c and cleaned up for 2.6
138c2ecf20Sopenharmony_ci * Copyright (C) 2004 Nokia Corporation by Tony Lindrgen <tony@atomide.com>
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/types.h>
178c2ecf20Sopenharmony_ci#include <linux/gpio.h>
188c2ecf20Sopenharmony_ci#include <linux/init.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/device.h>
218c2ecf20Sopenharmony_ci#include <linux/errno.h>
228c2ecf20Sopenharmony_ci#include <linux/io.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <asm/irq.h>
258c2ecf20Sopenharmony_ci#include <asm/mach/irq.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include <mach/hardware.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include "iomap.h"
308c2ecf20Sopenharmony_ci#include "common.h"
318c2ecf20Sopenharmony_ci#include "fpga.h"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic void fpga_mask_irq(struct irq_data *d)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	unsigned int irq = d->irq - OMAP_FPGA_IRQ_BASE;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	if (irq < 8)
388c2ecf20Sopenharmony_ci		__raw_writeb((__raw_readb(OMAP1510_FPGA_IMR_LO)
398c2ecf20Sopenharmony_ci			      & ~(1 << irq)), OMAP1510_FPGA_IMR_LO);
408c2ecf20Sopenharmony_ci	else if (irq < 16)
418c2ecf20Sopenharmony_ci		__raw_writeb((__raw_readb(OMAP1510_FPGA_IMR_HI)
428c2ecf20Sopenharmony_ci			      & ~(1 << (irq - 8))), OMAP1510_FPGA_IMR_HI);
438c2ecf20Sopenharmony_ci	else
448c2ecf20Sopenharmony_ci		__raw_writeb((__raw_readb(INNOVATOR_FPGA_IMR2)
458c2ecf20Sopenharmony_ci			      & ~(1 << (irq - 16))), INNOVATOR_FPGA_IMR2);
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic inline u32 get_fpga_unmasked_irqs(void)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	return
528c2ecf20Sopenharmony_ci		((__raw_readb(OMAP1510_FPGA_ISR_LO) &
538c2ecf20Sopenharmony_ci		  __raw_readb(OMAP1510_FPGA_IMR_LO))) |
548c2ecf20Sopenharmony_ci		((__raw_readb(OMAP1510_FPGA_ISR_HI) &
558c2ecf20Sopenharmony_ci		  __raw_readb(OMAP1510_FPGA_IMR_HI)) << 8) |
568c2ecf20Sopenharmony_ci		((__raw_readb(INNOVATOR_FPGA_ISR2) &
578c2ecf20Sopenharmony_ci		  __raw_readb(INNOVATOR_FPGA_IMR2)) << 16);
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic void fpga_ack_irq(struct irq_data *d)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	/* Don't need to explicitly ACK FPGA interrupts */
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic void fpga_unmask_irq(struct irq_data *d)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	unsigned int irq = d->irq - OMAP_FPGA_IRQ_BASE;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	if (irq < 8)
718c2ecf20Sopenharmony_ci		__raw_writeb((__raw_readb(OMAP1510_FPGA_IMR_LO) | (1 << irq)),
728c2ecf20Sopenharmony_ci		     OMAP1510_FPGA_IMR_LO);
738c2ecf20Sopenharmony_ci	else if (irq < 16)
748c2ecf20Sopenharmony_ci		__raw_writeb((__raw_readb(OMAP1510_FPGA_IMR_HI)
758c2ecf20Sopenharmony_ci			      | (1 << (irq - 8))), OMAP1510_FPGA_IMR_HI);
768c2ecf20Sopenharmony_ci	else
778c2ecf20Sopenharmony_ci		__raw_writeb((__raw_readb(INNOVATOR_FPGA_IMR2)
788c2ecf20Sopenharmony_ci			      | (1 << (irq - 16))), INNOVATOR_FPGA_IMR2);
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic void fpga_mask_ack_irq(struct irq_data *d)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	fpga_mask_irq(d);
848c2ecf20Sopenharmony_ci	fpga_ack_irq(d);
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic void innovator_fpga_IRQ_demux(struct irq_desc *desc)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	u32 stat;
908c2ecf20Sopenharmony_ci	int fpga_irq;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	stat = get_fpga_unmasked_irqs();
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	if (!stat)
958c2ecf20Sopenharmony_ci		return;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	for (fpga_irq = OMAP_FPGA_IRQ_BASE;
988c2ecf20Sopenharmony_ci	     (fpga_irq < OMAP_FPGA_IRQ_END) && stat;
998c2ecf20Sopenharmony_ci	     fpga_irq++, stat >>= 1) {
1008c2ecf20Sopenharmony_ci		if (stat & 1) {
1018c2ecf20Sopenharmony_ci			generic_handle_irq(fpga_irq);
1028c2ecf20Sopenharmony_ci		}
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic struct irq_chip omap_fpga_irq_ack = {
1078c2ecf20Sopenharmony_ci	.name		= "FPGA-ack",
1088c2ecf20Sopenharmony_ci	.irq_ack	= fpga_mask_ack_irq,
1098c2ecf20Sopenharmony_ci	.irq_mask	= fpga_mask_irq,
1108c2ecf20Sopenharmony_ci	.irq_unmask	= fpga_unmask_irq,
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic struct irq_chip omap_fpga_irq = {
1158c2ecf20Sopenharmony_ci	.name		= "FPGA",
1168c2ecf20Sopenharmony_ci	.irq_ack	= fpga_ack_irq,
1178c2ecf20Sopenharmony_ci	.irq_mask	= fpga_mask_irq,
1188c2ecf20Sopenharmony_ci	.irq_unmask	= fpga_unmask_irq,
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/*
1228c2ecf20Sopenharmony_ci * All of the FPGA interrupt request inputs except for the touchscreen are
1238c2ecf20Sopenharmony_ci * edge-sensitive; the touchscreen is level-sensitive.  The edge-sensitive
1248c2ecf20Sopenharmony_ci * interrupts are acknowledged as a side-effect of reading the interrupt
1258c2ecf20Sopenharmony_ci * status register from the FPGA.  The edge-sensitive interrupt inputs
1268c2ecf20Sopenharmony_ci * cause a problem with level interrupt requests, such as Ethernet.  The
1278c2ecf20Sopenharmony_ci * problem occurs when a level interrupt request is asserted while its
1288c2ecf20Sopenharmony_ci * interrupt input is masked in the FPGA, which results in a missed
1298c2ecf20Sopenharmony_ci * interrupt.
1308c2ecf20Sopenharmony_ci *
1318c2ecf20Sopenharmony_ci * In an attempt to workaround the problem with missed interrupts, the
1328c2ecf20Sopenharmony_ci * mask_ack routine for all of the FPGA interrupts has been changed from
1338c2ecf20Sopenharmony_ci * fpga_mask_ack_irq() to fpga_ack_irq() so that the specific FPGA interrupt
1348c2ecf20Sopenharmony_ci * being serviced is left unmasked.  We can do this because the FPGA cascade
1358c2ecf20Sopenharmony_ci * interrupt is run with all interrupts masked.
1368c2ecf20Sopenharmony_ci *
1378c2ecf20Sopenharmony_ci * Limited testing indicates that this workaround appears to be effective
1388c2ecf20Sopenharmony_ci * for the smc9194 Ethernet driver used on the Innovator.  It should work
1398c2ecf20Sopenharmony_ci * on other FPGA interrupts as well, but any drivers that explicitly mask
1408c2ecf20Sopenharmony_ci * interrupts at the interrupt controller via disable_irq/enable_irq
1418c2ecf20Sopenharmony_ci * could pose a problem.
1428c2ecf20Sopenharmony_ci */
1438c2ecf20Sopenharmony_civoid omap1510_fpga_init_irq(void)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	int i, res;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	__raw_writeb(0, OMAP1510_FPGA_IMR_LO);
1488c2ecf20Sopenharmony_ci	__raw_writeb(0, OMAP1510_FPGA_IMR_HI);
1498c2ecf20Sopenharmony_ci	__raw_writeb(0, INNOVATOR_FPGA_IMR2);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	for (i = OMAP_FPGA_IRQ_BASE; i < OMAP_FPGA_IRQ_END; i++) {
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci		if (i == OMAP1510_INT_FPGA_TS) {
1548c2ecf20Sopenharmony_ci			/*
1558c2ecf20Sopenharmony_ci			 * The touchscreen interrupt is level-sensitive, so
1568c2ecf20Sopenharmony_ci			 * we'll use the regular mask_ack routine for it.
1578c2ecf20Sopenharmony_ci			 */
1588c2ecf20Sopenharmony_ci			irq_set_chip(i, &omap_fpga_irq_ack);
1598c2ecf20Sopenharmony_ci		}
1608c2ecf20Sopenharmony_ci		else {
1618c2ecf20Sopenharmony_ci			/*
1628c2ecf20Sopenharmony_ci			 * All FPGA interrupts except the touchscreen are
1638c2ecf20Sopenharmony_ci			 * edge-sensitive, so we won't mask them.
1648c2ecf20Sopenharmony_ci			 */
1658c2ecf20Sopenharmony_ci			irq_set_chip(i, &omap_fpga_irq);
1668c2ecf20Sopenharmony_ci		}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci		irq_set_handler(i, handle_edge_irq);
1698c2ecf20Sopenharmony_ci		irq_clear_status_flags(i, IRQ_NOREQUEST);
1708c2ecf20Sopenharmony_ci	}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	/*
1738c2ecf20Sopenharmony_ci	 * The FPGA interrupt line is connected to GPIO13. Claim this pin for
1748c2ecf20Sopenharmony_ci	 * the ARM.
1758c2ecf20Sopenharmony_ci	 *
1768c2ecf20Sopenharmony_ci	 * NOTE: For general GPIO/MPUIO access and interrupts, please see
1778c2ecf20Sopenharmony_ci	 * gpio.[ch]
1788c2ecf20Sopenharmony_ci	 */
1798c2ecf20Sopenharmony_ci	res = gpio_request(13, "FPGA irq");
1808c2ecf20Sopenharmony_ci	if (res) {
1818c2ecf20Sopenharmony_ci		pr_err("%s failed to get gpio\n", __func__);
1828c2ecf20Sopenharmony_ci		return;
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci	gpio_direction_input(13);
1858c2ecf20Sopenharmony_ci	irq_set_irq_type(gpio_to_irq(13), IRQ_TYPE_EDGE_RISING);
1868c2ecf20Sopenharmony_ci	irq_set_chained_handler(OMAP1510_INT_FPGA, innovator_fpga_IRQ_demux);
1878c2ecf20Sopenharmony_ci}
188