18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *	linux/arch/alpha/kernel/sys_eiger.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *	Copyright (C) 1995 David A Rusling
68c2ecf20Sopenharmony_ci *	Copyright (C) 1996, 1999 Jay A Estabrook
78c2ecf20Sopenharmony_ci *	Copyright (C) 1998, 1999 Richard Henderson
88c2ecf20Sopenharmony_ci *	Copyright (C) 1999 Iain Grant
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Code supporting the EIGER (EV6+TSUNAMI).
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/types.h>
158c2ecf20Sopenharmony_ci#include <linux/mm.h>
168c2ecf20Sopenharmony_ci#include <linux/sched.h>
178c2ecf20Sopenharmony_ci#include <linux/pci.h>
188c2ecf20Sopenharmony_ci#include <linux/init.h>
198c2ecf20Sopenharmony_ci#include <linux/bitops.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <asm/ptrace.h>
228c2ecf20Sopenharmony_ci#include <asm/dma.h>
238c2ecf20Sopenharmony_ci#include <asm/irq.h>
248c2ecf20Sopenharmony_ci#include <asm/mmu_context.h>
258c2ecf20Sopenharmony_ci#include <asm/io.h>
268c2ecf20Sopenharmony_ci#include <asm/core_tsunami.h>
278c2ecf20Sopenharmony_ci#include <asm/hwrpb.h>
288c2ecf20Sopenharmony_ci#include <asm/tlbflush.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include "proto.h"
318c2ecf20Sopenharmony_ci#include "irq_impl.h"
328c2ecf20Sopenharmony_ci#include "pci_impl.h"
338c2ecf20Sopenharmony_ci#include "machvec_impl.h"
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* Note that this interrupt code is identical to TAKARA.  */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/* Note mask bit is true for DISABLED irqs.  */
398c2ecf20Sopenharmony_cistatic unsigned long cached_irq_mask[2] = { -1, -1 };
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic inline void
428c2ecf20Sopenharmony_cieiger_update_irq_hw(unsigned long irq, unsigned long mask)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	int regaddr;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	mask = (irq >= 64 ? mask << 16 : mask >> ((irq - 16) & 0x30));
478c2ecf20Sopenharmony_ci	regaddr = 0x510 + (((irq - 16) >> 2) & 0x0c);
488c2ecf20Sopenharmony_ci	outl(mask & 0xffff0000UL, regaddr);
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic inline void
528c2ecf20Sopenharmony_cieiger_enable_irq(struct irq_data *d)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	unsigned int irq = d->irq;
558c2ecf20Sopenharmony_ci	unsigned long mask;
568c2ecf20Sopenharmony_ci	mask = (cached_irq_mask[irq >= 64] &= ~(1UL << (irq & 63)));
578c2ecf20Sopenharmony_ci	eiger_update_irq_hw(irq, mask);
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic void
618c2ecf20Sopenharmony_cieiger_disable_irq(struct irq_data *d)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	unsigned int irq = d->irq;
648c2ecf20Sopenharmony_ci	unsigned long mask;
658c2ecf20Sopenharmony_ci	mask = (cached_irq_mask[irq >= 64] |= 1UL << (irq & 63));
668c2ecf20Sopenharmony_ci	eiger_update_irq_hw(irq, mask);
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic struct irq_chip eiger_irq_type = {
708c2ecf20Sopenharmony_ci	.name		= "EIGER",
718c2ecf20Sopenharmony_ci	.irq_unmask	= eiger_enable_irq,
728c2ecf20Sopenharmony_ci	.irq_mask	= eiger_disable_irq,
738c2ecf20Sopenharmony_ci	.irq_mask_ack	= eiger_disable_irq,
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic void
778c2ecf20Sopenharmony_cieiger_device_interrupt(unsigned long vector)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	unsigned intstatus;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/*
828c2ecf20Sopenharmony_ci	 * The PALcode will have passed us vectors 0x800 or 0x810,
838c2ecf20Sopenharmony_ci	 * which are fairly arbitrary values and serve only to tell
848c2ecf20Sopenharmony_ci	 * us whether an interrupt has come in on IRQ0 or IRQ1. If
858c2ecf20Sopenharmony_ci	 * it's IRQ1 it's a PCI interrupt; if it's IRQ0, it's
868c2ecf20Sopenharmony_ci	 * probably ISA, but PCI interrupts can come through IRQ0
878c2ecf20Sopenharmony_ci	 * as well if the interrupt controller isn't in accelerated
888c2ecf20Sopenharmony_ci	 * mode.
898c2ecf20Sopenharmony_ci	 *
908c2ecf20Sopenharmony_ci	 * OTOH, the accelerator thing doesn't seem to be working
918c2ecf20Sopenharmony_ci	 * overly well, so what we'll do instead is try directly
928c2ecf20Sopenharmony_ci	 * examining the Master Interrupt Register to see if it's a
938c2ecf20Sopenharmony_ci	 * PCI interrupt, and if _not_ then we'll pass it on to the
948c2ecf20Sopenharmony_ci	 * ISA handler.
958c2ecf20Sopenharmony_ci	 */
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	intstatus = inw(0x500) & 15;
988c2ecf20Sopenharmony_ci	if (intstatus) {
998c2ecf20Sopenharmony_ci		/*
1008c2ecf20Sopenharmony_ci		 * This is a PCI interrupt. Check each bit and
1018c2ecf20Sopenharmony_ci		 * despatch an interrupt if it's set.
1028c2ecf20Sopenharmony_ci		 */
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		if (intstatus & 8) handle_irq(16+3);
1058c2ecf20Sopenharmony_ci		if (intstatus & 4) handle_irq(16+2);
1068c2ecf20Sopenharmony_ci		if (intstatus & 2) handle_irq(16+1);
1078c2ecf20Sopenharmony_ci		if (intstatus & 1) handle_irq(16+0);
1088c2ecf20Sopenharmony_ci	} else {
1098c2ecf20Sopenharmony_ci		isa_device_interrupt(vector);
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic void
1148c2ecf20Sopenharmony_cieiger_srm_device_interrupt(unsigned long vector)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	int irq = (vector - 0x800) >> 4;
1178c2ecf20Sopenharmony_ci	handle_irq(irq);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic void __init
1218c2ecf20Sopenharmony_cieiger_init_irq(void)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	long i;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	outb(0, DMA1_RESET_REG);
1268c2ecf20Sopenharmony_ci	outb(0, DMA2_RESET_REG);
1278c2ecf20Sopenharmony_ci	outb(DMA_MODE_CASCADE, DMA2_MODE_REG);
1288c2ecf20Sopenharmony_ci	outb(0, DMA2_MASK_REG);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	if (alpha_using_srm)
1318c2ecf20Sopenharmony_ci		alpha_mv.device_interrupt = eiger_srm_device_interrupt;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	for (i = 16; i < 128; i += 16)
1348c2ecf20Sopenharmony_ci		eiger_update_irq_hw(i, -1);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	init_i8259a_irqs();
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	for (i = 16; i < 128; ++i) {
1398c2ecf20Sopenharmony_ci		irq_set_chip_and_handler(i, &eiger_irq_type, handle_level_irq);
1408c2ecf20Sopenharmony_ci		irq_set_status_flags(i, IRQ_LEVEL);
1418c2ecf20Sopenharmony_ci	}
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic int
1458c2ecf20Sopenharmony_cieiger_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	u8 irq_orig;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/* The SRM console has already calculated out the IRQ value's for
1508c2ecf20Sopenharmony_ci	   option cards. As this works lets just read in the value already
1518c2ecf20Sopenharmony_ci	   set and change it to a useable value by Linux.
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	   All the IRQ values generated by the console are greater than 90,
1548c2ecf20Sopenharmony_ci	   so we subtract 80 because it is (90 - allocated ISA IRQ's).  */
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq_orig);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	return irq_orig - 0x80;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic u8
1628c2ecf20Sopenharmony_cieiger_swizzle(struct pci_dev *dev, u8 *pinp)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	struct pci_controller *hose = dev->sysdata;
1658c2ecf20Sopenharmony_ci	int slot, pin = *pinp;
1668c2ecf20Sopenharmony_ci	int bridge_count = 0;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	/* Find the number of backplane bridges.  */
1698c2ecf20Sopenharmony_ci	int backplane = inw(0x502) & 0x0f;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	switch (backplane)
1728c2ecf20Sopenharmony_ci	{
1738c2ecf20Sopenharmony_ci	   case 0x00: bridge_count = 0; break; /* No bridges */
1748c2ecf20Sopenharmony_ci	   case 0x01: bridge_count = 1; break; /* 1 */
1758c2ecf20Sopenharmony_ci	   case 0x03: bridge_count = 2; break; /* 2 */
1768c2ecf20Sopenharmony_ci	   case 0x07: bridge_count = 3; break; /* 3 */
1778c2ecf20Sopenharmony_ci	   case 0x0f: bridge_count = 4; break; /* 4 */
1788c2ecf20Sopenharmony_ci	}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	slot = PCI_SLOT(dev->devfn);
1818c2ecf20Sopenharmony_ci	while (dev->bus->self) {
1828c2ecf20Sopenharmony_ci		/* Check for built-in bridges on hose 0. */
1838c2ecf20Sopenharmony_ci		if (hose->index == 0
1848c2ecf20Sopenharmony_ci		    && (PCI_SLOT(dev->bus->self->devfn)
1858c2ecf20Sopenharmony_ci			> 20 - bridge_count)) {
1868c2ecf20Sopenharmony_ci			slot = PCI_SLOT(dev->devfn);
1878c2ecf20Sopenharmony_ci			break;
1888c2ecf20Sopenharmony_ci		}
1898c2ecf20Sopenharmony_ci		/* Must be a card-based bridge.  */
1908c2ecf20Sopenharmony_ci		pin = pci_swizzle_interrupt_pin(dev, pin);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci		/* Move up the chain of bridges.  */
1938c2ecf20Sopenharmony_ci		dev = dev->bus->self;
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci	*pinp = pin;
1968c2ecf20Sopenharmony_ci	return slot;
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci/*
2008c2ecf20Sopenharmony_ci * The System Vectors
2018c2ecf20Sopenharmony_ci */
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistruct alpha_machine_vector eiger_mv __initmv = {
2048c2ecf20Sopenharmony_ci	.vector_name		= "Eiger",
2058c2ecf20Sopenharmony_ci	DO_EV6_MMU,
2068c2ecf20Sopenharmony_ci	DO_DEFAULT_RTC,
2078c2ecf20Sopenharmony_ci	DO_TSUNAMI_IO,
2088c2ecf20Sopenharmony_ci	.machine_check		= tsunami_machine_check,
2098c2ecf20Sopenharmony_ci	.max_isa_dma_address	= ALPHA_MAX_ISA_DMA_ADDRESS,
2108c2ecf20Sopenharmony_ci	.min_io_address		= DEFAULT_IO_BASE,
2118c2ecf20Sopenharmony_ci	.min_mem_address	= DEFAULT_MEM_BASE,
2128c2ecf20Sopenharmony_ci	.pci_dac_offset		= TSUNAMI_DAC_OFFSET,
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	.nr_irqs		= 128,
2158c2ecf20Sopenharmony_ci	.device_interrupt	= eiger_device_interrupt,
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	.init_arch		= tsunami_init_arch,
2188c2ecf20Sopenharmony_ci	.init_irq		= eiger_init_irq,
2198c2ecf20Sopenharmony_ci	.init_rtc		= common_init_rtc,
2208c2ecf20Sopenharmony_ci	.init_pci		= common_init_pci,
2218c2ecf20Sopenharmony_ci	.kill_arch		= tsunami_kill_arch,
2228c2ecf20Sopenharmony_ci	.pci_map_irq		= eiger_map_irq,
2238c2ecf20Sopenharmony_ci	.pci_swizzle		= eiger_swizzle,
2248c2ecf20Sopenharmony_ci};
2258c2ecf20Sopenharmony_ciALIAS_MV(eiger)
226