1/*
2 *	linux/arch/alpha/kernel/sys_ruffian.c
3 *
4 *	Copyright (C) 1995 David A Rusling
5 *	Copyright (C) 1996 Jay A Estabrook
6 *	Copyright (C) 1998, 1999, 2000 Richard Henderson
7 *
8 * Code supporting the RUFFIAN.
9 */
10
11#include <linux/kernel.h>
12#include <linux/types.h>
13#include <linux/mm.h>
14#include <linux/sched.h>
15#include <linux/pci.h>
16#include <linux/ioport.h>
17#include <linux/init.h>
18
19#include <asm/ptrace.h>
20#include <asm/system.h>
21#include <asm/dma.h>
22#include <asm/irq.h>
23#include <asm/mmu_context.h>
24#include <asm/io.h>
25#include <asm/pgtable.h>
26#include <asm/core_cia.h>
27#include <asm/tlbflush.h>
28#include <asm/8253pit.h>
29
30#include "proto.h"
31#include "irq_impl.h"
32#include "pci_impl.h"
33#include "machvec_impl.h"
34
35
36static void __init
37ruffian_init_irq(void)
38{
39	/* Invert 6&7 for i82371 */
40	*(vulp)PYXIS_INT_HILO  = 0x000000c0UL; mb();
41	*(vulp)PYXIS_INT_CNFG  = 0x00002064UL; mb();	 /* all clear */
42
43	outb(0x11,0xA0);
44	outb(0x08,0xA1);
45	outb(0x02,0xA1);
46	outb(0x01,0xA1);
47	outb(0xFF,0xA1);
48
49	outb(0x11,0x20);
50	outb(0x00,0x21);
51	outb(0x04,0x21);
52	outb(0x01,0x21);
53	outb(0xFF,0x21);
54
55	/* Finish writing the 82C59A PIC Operation Control Words */
56	outb(0x20,0xA0);
57	outb(0x20,0x20);
58
59	init_i8259a_irqs();
60
61	/* Not interested in the bogus interrupts (0,3,6),
62	   NMI (1), HALT (2), flash (5), or 21142 (8).  */
63	init_pyxis_irqs(0x16f0000);
64
65	common_init_isa_dma();
66}
67
68#define RUFFIAN_LATCH	((PIT_TICK_RATE + HZ / 2) / HZ)
69
70static void __init
71ruffian_init_rtc(void)
72{
73	/* Ruffian does not have the RTC connected to the CPU timer
74	   interrupt.  Instead, it uses the PIT connected to IRQ 0.  */
75
76	/* Setup interval timer.  */
77	outb(0x34, 0x43);		/* binary, mode 2, LSB/MSB, ch 0 */
78	outb(RUFFIAN_LATCH & 0xff, 0x40);	/* LSB */
79	outb(RUFFIAN_LATCH >> 8, 0x40);		/* MSB */
80
81	outb(0xb6, 0x43);		/* pit counter 2: speaker */
82	outb(0x31, 0x42);
83	outb(0x13, 0x42);
84
85	setup_irq(0, &timer_irqaction);
86}
87
88static void
89ruffian_kill_arch (int mode)
90{
91	cia_kill_arch(mode);
92}
93
94/*
95 *  Interrupt routing:
96 *
97 *		Primary bus
98 *	  IdSel		INTA	INTB	INTC	INTD
99 * 21052   13		  -	  -	  -	  -
100 * SIO	   14		 23	  -	  -	  -
101 * 21143   15		 44	  -	  -	  -
102 * Slot 0  17		 43	 42	 41	 40
103 *
104 *		Secondary bus
105 *	  IdSel		INTA	INTB	INTC	INTD
106 * Slot 0   8 (18)	 19	 18	 17	 16
107 * Slot 1   9 (19)	 31	 30	 29	 28
108 * Slot 2  10 (20)	 27	 26	 25	 24
109 * Slot 3  11 (21)	 39	 38	 37	 36
110 * Slot 4  12 (22)	 35	 34	 33	 32
111 * 53c875  13 (23)	 20	  -	  -	  -
112 *
113 */
114
115static int __init
116ruffian_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
117{
118        static char irq_tab[11][5] __initdata = {
119	      /*INT  INTA INTB INTC INTD */
120		{-1,  -1,  -1,  -1,  -1},  /* IdSel 13,  21052	     */
121		{-1,  -1,  -1,  -1,  -1},  /* IdSel 14,  SIO	     */
122		{44,  44,  44,  44,  44},  /* IdSel 15,  21143	     */
123		{-1,  -1,  -1,  -1,  -1},  /* IdSel 16,  none	     */
124		{43,  43,  42,  41,  40},  /* IdSel 17,  64-bit slot */
125		/* the next 6 are actually on PCI bus 1, across the bridge */
126		{19,  19,  18,  17,  16},  /* IdSel  8,  slot 0	     */
127		{31,  31,  30,  29,  28},  /* IdSel  9,  slot 1	     */
128		{27,  27,  26,  25,  24},  /* IdSel 10,  slot 2	     */
129		{39,  39,  38,  37,  36},  /* IdSel 11,  slot 3	     */
130		{35,  35,  34,  33,  32},  /* IdSel 12,  slot 4	     */
131		{20,  20,  20,  20,  20},  /* IdSel 13,  53c875	     */
132        };
133	const long min_idsel = 13, max_idsel = 23, irqs_per_slot = 5;
134	return COMMON_TABLE_LOOKUP;
135}
136
137static u8 __init
138ruffian_swizzle(struct pci_dev *dev, u8 *pinp)
139{
140	int slot, pin = *pinp;
141
142	if (dev->bus->number == 0) {
143		slot = PCI_SLOT(dev->devfn);
144	}
145	/* Check for the built-in bridge.  */
146	else if (PCI_SLOT(dev->bus->self->devfn) == 13) {
147		slot = PCI_SLOT(dev->devfn) + 10;
148	}
149	else
150	{
151		/* Must be a card-based bridge.  */
152		do {
153			if (PCI_SLOT(dev->bus->self->devfn) == 13) {
154				slot = PCI_SLOT(dev->devfn) + 10;
155				break;
156			}
157			pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
158
159			/* Move up the chain of bridges.  */
160			dev = dev->bus->self;
161			/* Slot of the next bridge.  */
162			slot = PCI_SLOT(dev->devfn);
163		} while (dev->bus->self);
164	}
165	*pinp = pin;
166	return slot;
167}
168
169#ifdef BUILDING_FOR_MILO
170/*
171 * The DeskStation Ruffian motherboard firmware does not place
172 * the memory size in the PALimpure area.  Therefore, we use
173 * the Bank Configuration Registers in PYXIS to obtain the size.
174 */
175static unsigned long __init
176ruffian_get_bank_size(unsigned long offset)
177{
178	unsigned long bank_addr, bank, ret = 0;
179
180	/* Valid offsets are: 0x800, 0x840 and 0x880
181	   since Ruffian only uses three banks.  */
182	bank_addr = (unsigned long)PYXIS_MCR + offset;
183	bank = *(vulp)bank_addr;
184
185	/* Check BANK_ENABLE */
186	if (bank & 0x01) {
187		static unsigned long size[] __initdata = {
188			0x40000000UL, /* 0x00,   1G */
189			0x20000000UL, /* 0x02, 512M */
190			0x10000000UL, /* 0x04, 256M */
191			0x08000000UL, /* 0x06, 128M */
192			0x04000000UL, /* 0x08,  64M */
193			0x02000000UL, /* 0x0a,  32M */
194			0x01000000UL, /* 0x0c,  16M */
195			0x00800000UL, /* 0x0e,   8M */
196			0x80000000UL, /* 0x10,   2G */
197		};
198
199		bank = (bank & 0x1e) >> 1;
200		if (bank < ARRAY_SIZE(size))
201			ret = size[bank];
202	}
203
204	return ret;
205}
206#endif /* BUILDING_FOR_MILO */
207
208/*
209 * The System Vector
210 */
211
212struct alpha_machine_vector ruffian_mv __initmv = {
213	.vector_name		= "Ruffian",
214	DO_EV5_MMU,
215	DO_DEFAULT_RTC,
216	DO_PYXIS_IO,
217	.machine_check		= cia_machine_check,
218	.max_isa_dma_address	= ALPHA_RUFFIAN_MAX_ISA_DMA_ADDRESS,
219	.min_io_address		= DEFAULT_IO_BASE,
220	.min_mem_address	= DEFAULT_MEM_BASE,
221	.pci_dac_offset		= PYXIS_DAC_OFFSET,
222
223	.nr_irqs		= 48,
224	.device_interrupt	= pyxis_device_interrupt,
225
226	.init_arch		= pyxis_init_arch,
227	.init_irq		= ruffian_init_irq,
228	.init_rtc		= ruffian_init_rtc,
229	.init_pci		= cia_init_pci,
230	.kill_arch		= ruffian_kill_arch,
231	.pci_map_irq		= ruffian_map_irq,
232	.pci_swizzle		= ruffian_swizzle,
233};
234ALIAS_MV(ruffian)
235