blob: b8156ac496a0a31c6c9d285ce1b5c4d8e9c4c2fe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * 6522 Versatile Interface Adapter (VIA)
3 *
Simon Arlott0c79cf62007-10-20 01:20:32 +02004 * There are two of these on the Mac II. Some IRQs are vectored
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * via them as are assorted bits and bobs - eg RTC, ADB.
6 *
7 * CSA: Motorola seems to have removed documentation on the 6522 from
8 * their web site; try
9 * http://nerini.drf.com/vectrex/other/text/chips/6522/
10 * http://www.zymurgy.net/classic/vic20/vicdet1.htm
11 * and
12 * http://193.23.168.87/mikro_laborversuche/via_iobaustein/via6522_1.html
13 * for info. A full-text web search on 6522 AND VIA will probably also
14 * net some usefulness. <cananian@alumni.princeton.edu> 20apr1999
15 *
Finn Thain67dfb152007-05-01 22:32:56 +020016 * Additional data is here (the SY6522 was used in the Mac II etc):
17 * http://www.6502.org/documents/datasheets/synertek/synertek_sy6522.pdf
18 * http://www.6502.org/documents/datasheets/synertek/synertek_sy6522_programming_reference.pdf
19 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * PRAM/RTC access algorithms are from the NetBSD RTC toolkit version 1.08b
21 * by Erik Vogan and adapted to Linux by Joshua M. Thompson (funaho@jurai.org)
22 *
23 */
24
25#include <linux/types.h>
26#include <linux/kernel.h>
27#include <linux/mm.h>
28#include <linux/delay.h>
29#include <linux/init.h>
Adrian Bunkdeea7772008-02-04 22:30:24 -080030#include <linux/module.h>
Geert Uytterhoevenddc7fd22011-07-13 21:48:30 +020031#ifdef CONFIG_GENERIC_HARDIRQS
32#include <linux/irq.h>
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/bootinfo.h>
36#include <asm/macintosh.h>
37#include <asm/macints.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/mac_via.h>
39#include <asm/mac_psc.h>
Geert Uytterhoevenc85627f2008-12-21 12:03:37 +010040#include <asm/mac_oss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42volatile __u8 *via1, *via2;
Adrian Bunkdeea7772008-02-04 22:30:24 -080043int rbv_present;
44int via_alt_mapping;
45EXPORT_SYMBOL(via_alt_mapping);
Adrian Bunk8dfbdf42008-07-17 21:16:25 +020046static __u8 rbv_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
49 * Globals for accessing the VIA chip registers without having to
50 * check if we're hitting a real VIA or an RBV. Normally you could
51 * just hit the combined register (ie, vIER|rIER) but that seems to
52 * break on AV Macs...probably because they actually decode more than
53 * eight address bits. Why can't Apple engineers at least be
54 * _consistently_ lazy? - 1999-05-21 (jmt)
55 */
56
57static int gIER,gIFR,gBufA,gBufB;
58
59/*
60 * Timer defs.
61 */
62
63#define TICK_SIZE 10000
64#define MAC_CLOCK_TICK (783300/HZ) /* ticks per HZ */
65#define MAC_CLOCK_LOW (MAC_CLOCK_TICK&0xFF)
66#define MAC_CLOCK_HIGH (MAC_CLOCK_TICK>>8)
67
Finn Thain4a973592008-11-18 20:45:20 +010068/* To disable a NuBus slot on Quadras we make that slot IRQ line an output set
Finn Thaincd713dd2007-05-01 22:32:57 +020069 * high. On RBV we just use the slot interrupt enable register. On Macs with
70 * genuine VIA chips we must use nubus_disabled to keep track of disabled slot
71 * interrupts. When any slot IRQ is disabled we mask the (edge triggered) CA1
72 * or "SLOTS" interrupt. When no slot is disabled, we unmask the CA1 interrupt.
73 * So, on genuine VIAs, having more than one NuBus IRQ can mean trouble,
74 * because closing one of those drivers can mask all of the NuBus interrupts.
75 * Also, since we can't mask the unregistered slot IRQs on genuine VIAs, it's
76 * possible to get interrupts from cards that MacOS or the ROM has configured
77 * but we have not. FWIW, "Designing Cards and Drivers for Macintosh II and
78 * Macintosh SE", page 9-8, says, a slot IRQ with no driver would crash MacOS.
79 */
80static u8 nubus_disabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82void via_debug_dump(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083void via_irq_enable(int irq);
84void via_irq_disable(int irq);
85void via_irq_clear(int irq);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
88 * Initialize the VIAs
89 *
90 * First we figure out where they actually _are_ as well as what type of
91 * VIA we have for VIA2 (it could be a real VIA or an RBV or even an OSS.)
92 * Then we pretty much clear them out and disable all IRQ sources.
93 *
94 * Note: the OSS is actually "detected" here and not in oss_init(). It just
95 * seems more logical to do it here since via_init() needs to know
96 * these things anyways.
97 */
98
99void __init via_init(void)
100{
101 switch(macintosh_config->via_type) {
102
103 /* IIci, IIsi, IIvx, IIvi (P6xx), LC series */
104
105 case MAC_VIA_IIci:
106 via1 = (void *) VIA1_BASE;
107 if (macintosh_config->ident == MAC_MODEL_IIFX) {
108 via2 = NULL;
109 rbv_present = 0;
110 oss_present = 1;
111 } else {
112 via2 = (void *) RBV_BASE;
113 rbv_present = 1;
114 oss_present = 0;
115 }
116 if (macintosh_config->ident == MAC_MODEL_LCIII) {
117 rbv_clear = 0x00;
118 } else {
119 /* on most RBVs (& unlike the VIAs), you */
120 /* need to set bit 7 when you write to IFR */
121 /* in order for your clear to occur. */
122 rbv_clear = 0x80;
123 }
124 gIER = rIER;
125 gIFR = rIFR;
126 gBufA = rSIFR;
127 gBufB = rBufB;
128 break;
129
130 /* Quadra and early MacIIs agree on the VIA locations */
131
132 case MAC_VIA_QUADRA:
133 case MAC_VIA_II:
134 via1 = (void *) VIA1_BASE;
135 via2 = (void *) VIA2_BASE;
136 rbv_present = 0;
137 oss_present = 0;
138 rbv_clear = 0x00;
139 gIER = vIER;
140 gIFR = vIFR;
141 gBufA = vBufA;
142 gBufB = vBufB;
143 break;
144 default:
145 panic("UNKNOWN VIA TYPE");
146 }
147
148 printk(KERN_INFO "VIA1 at %p is a 6522 or clone\n", via1);
149
150 printk(KERN_INFO "VIA2 at %p is ", via2);
151 if (rbv_present) {
Finn Thain67dfb152007-05-01 22:32:56 +0200152 printk("an RBV\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 } else if (oss_present) {
Finn Thain67dfb152007-05-01 22:32:56 +0200154 printk("an OSS\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 } else {
Finn Thain67dfb152007-05-01 22:32:56 +0200156 printk("a 6522 or clone\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158
159#ifdef DEBUG_VIA
160 via_debug_dump();
161#endif
162
163 /*
164 * Shut down all IRQ sources, reset the timers, and
165 * kill the timer latch on VIA1.
166 */
167
168 via1[vIER] = 0x7F;
169 via1[vIFR] = 0x7F;
170 via1[vT1LL] = 0;
171 via1[vT1LH] = 0;
172 via1[vT1CL] = 0;
173 via1[vT1CH] = 0;
174 via1[vT2CL] = 0;
175 via1[vT2CH] = 0;
Finn Thain4a973592008-11-18 20:45:20 +0100176 via1[vACR] &= ~0xC0; /* setup T1 timer with no PB7 output */
Finn Thain67dfb152007-05-01 22:32:56 +0200177 via1[vACR] &= ~0x03; /* disable port A & B latches */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 /*
180 * SE/30: disable video IRQ
181 * XXX: testing for SE/30 VBL
182 */
183
184 if (macintosh_config->ident == MAC_MODEL_SE30) {
185 via1[vDirB] |= 0x40;
186 via1[vBufB] |= 0x40;
187 }
188
189 /*
190 * Set the RTC bits to a known state: all lines to outputs and
191 * RTC disabled (yes that's 0 to enable and 1 to disable).
192 */
193
194 via1[vDirB] |= (VIA1B_vRTCEnb | VIA1B_vRTCClk | VIA1B_vRTCData);
195 via1[vBufB] |= (VIA1B_vRTCEnb | VIA1B_vRTCClk);
196
197 /* Everything below this point is VIA2/RBV only... */
198
Finn Thain4a973592008-11-18 20:45:20 +0100199 if (oss_present)
200 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* Some machines support an alternate IRQ mapping that spreads */
203 /* Ethernet and Sound out to their own autolevel IRQs and moves */
204 /* VIA1 to level 6. A/UX uses this mapping and we do too. Note */
205 /* that the IIfx emulates this alternate mapping using the OSS. */
206
Finn Thain4a973592008-11-18 20:45:20 +0100207 via_alt_mapping = 0;
208 if (macintosh_config->via_type == MAC_VIA_QUADRA)
209 switch (macintosh_config->ident) {
210 case MAC_MODEL_C660:
211 case MAC_MODEL_Q840:
212 /* not applicable */
213 break;
214 case MAC_MODEL_P588:
215 case MAC_MODEL_TV:
216 case MAC_MODEL_PB140:
217 case MAC_MODEL_PB145:
218 case MAC_MODEL_PB160:
219 case MAC_MODEL_PB165:
220 case MAC_MODEL_PB165C:
221 case MAC_MODEL_PB170:
222 case MAC_MODEL_PB180:
223 case MAC_MODEL_PB180C:
224 case MAC_MODEL_PB190:
225 case MAC_MODEL_PB520:
226 /* not yet tested */
227 break;
228 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 via_alt_mapping = 1;
230 via1[vDirB] |= 0x40;
231 via1[vBufB] &= ~0x40;
232 break;
Finn Thain4a973592008-11-18 20:45:20 +0100233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 /*
236 * Now initialize VIA2. For RBV we just kill all interrupts;
237 * for a regular VIA we also reset the timers and stuff.
238 */
239
240 via2[gIER] = 0x7F;
241 via2[gIFR] = 0x7F | rbv_clear;
242 if (!rbv_present) {
243 via2[vT1LL] = 0;
244 via2[vT1LH] = 0;
245 via2[vT1CL] = 0;
246 via2[vT1CH] = 0;
247 via2[vT2CL] = 0;
248 via2[vT2CH] = 0;
Finn Thain4a973592008-11-18 20:45:20 +0100249 via2[vACR] &= ~0xC0; /* setup T1 timer with no PB7 output */
Finn Thain67dfb152007-05-01 22:32:56 +0200250 via2[vACR] &= ~0x03; /* disable port A & B latches */
251 }
252
253 /*
Finn Thain4a973592008-11-18 20:45:20 +0100254 * Set vPCR for control line interrupts (but not on RBV)
Finn Thain67dfb152007-05-01 22:32:56 +0200255 */
256 if (!rbv_present) {
Finn Thain4a973592008-11-18 20:45:20 +0100257 /* For all VIA types, CA1 (SLOTS IRQ) and CB1 (ASC IRQ)
258 * are made negative edge triggered here.
259 */
Finn Thain67dfb152007-05-01 22:32:56 +0200260 if (macintosh_config->scsi_type == MAC_SCSI_OLD) {
261 /* CB2 (IRQ) indep. input, positive edge */
262 /* CA2 (DRQ) indep. input, positive edge */
263 via2[vPCR] = 0x66;
264 } else {
265 /* CB2 (IRQ) indep. input, negative edge */
266 /* CA2 (DRQ) indep. input, negative edge */
267 via2[vPCR] = 0x22;
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270}
271
272/*
273 * Start the 100 Hz clock
274 */
275
David Howells40220c12006-10-09 12:19:47 +0100276void __init via_init_clock(irq_handler_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 via1[vACR] |= 0x40;
279 via1[vT1LL] = MAC_CLOCK_LOW;
280 via1[vT1LH] = MAC_CLOCK_HIGH;
281 via1[vT1CL] = MAC_CLOCK_LOW;
282 via1[vT1CH] = MAC_CLOCK_HIGH;
283
Geert Uytterhoeven5a239452011-07-13 22:33:13 +0200284 if (request_irq(IRQ_MAC_TIMER_1, func, 0, "timer", func))
Geert Uytterhoeven92c3dd12008-12-30 14:02:27 +0100285 pr_err("Couldn't register %s interrupt\n", "timer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * Debugging dump, used in various places to see what's going on.
290 */
291
292void via_debug_dump(void)
293{
294 printk(KERN_DEBUG "VIA1: DDRA = 0x%02X DDRB = 0x%02X ACR = 0x%02X\n",
295 (uint) via1[vDirA], (uint) via1[vDirB], (uint) via1[vACR]);
296 printk(KERN_DEBUG " PCR = 0x%02X IFR = 0x%02X IER = 0x%02X\n",
297 (uint) via1[vPCR], (uint) via1[vIFR], (uint) via1[vIER]);
298 if (oss_present) {
299 printk(KERN_DEBUG "VIA2: <OSS>\n");
300 } else if (rbv_present) {
301 printk(KERN_DEBUG "VIA2: IFR = 0x%02X IER = 0x%02X\n",
302 (uint) via2[rIFR], (uint) via2[rIER]);
303 printk(KERN_DEBUG " SIFR = 0x%02X SIER = 0x%02X\n",
304 (uint) via2[rSIFR], (uint) via2[rSIER]);
305 } else {
306 printk(KERN_DEBUG "VIA2: DDRA = 0x%02X DDRB = 0x%02X ACR = 0x%02X\n",
307 (uint) via2[vDirA], (uint) via2[vDirB],
308 (uint) via2[vACR]);
309 printk(KERN_DEBUG " PCR = 0x%02X IFR = 0x%02X IER = 0x%02X\n",
310 (uint) via2[vPCR],
311 (uint) via2[vIFR], (uint) via2[vIER]);
312 }
313}
314
315/*
316 * This is always executed with interrupts disabled.
317 *
318 * TBI: get time offset between scheduling timer ticks
319 */
320
321unsigned long mac_gettimeoffset (void)
322{
323 unsigned long ticks, offset = 0;
324
325 /* read VIA1 timer 2 current value */
326 ticks = via1[vT1CL] | (via1[vT1CH] << 8);
327 /* The probability of underflow is less than 2% */
328 if (ticks > MAC_CLOCK_TICK - MAC_CLOCK_TICK / 50)
329 /* Check for pending timer interrupt in VIA1 IFR */
330 if (via1[vIFR] & 0x40) offset = TICK_SIZE;
331
332 ticks = MAC_CLOCK_TICK - ticks;
333 ticks = ticks * 10000L / MAC_CLOCK_TICK;
334
335 return ticks + offset;
336}
337
338/*
339 * Flush the L2 cache on Macs that have it by flipping
340 * the system into 24-bit mode for an instant.
341 */
342
343void via_flush_cache(void)
344{
345 via2[gBufB] &= ~VIA2B_vMode32;
346 via2[gBufB] |= VIA2B_vMode32;
347}
348
349/*
350 * Return the status of the L2 cache on a IIci
351 */
352
353int via_get_cache_disable(void)
354{
355 /* Safeguard against being called accidentally */
356 if (!via2) {
357 printk(KERN_ERR "via_get_cache_disable called on a non-VIA machine!\n");
358 return 1;
359 }
360
361 return (int) via2[gBufB] & VIA2B_vCDis;
362}
363
364/*
365 * Initialize VIA2 for Nubus access
366 */
367
368void __init via_nubus_init(void)
369{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* unlock nubus transactions */
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
373 (macintosh_config->adb_type != MAC_ADB_PB2)) {
Finn Thain67dfb152007-05-01 22:32:56 +0200374 /* set the line to be an output on non-RBV machines */
375 if (!rbv_present)
376 via2[vDirB] |= 0x02;
377
378 /* this seems to be an ADB bit on PMU machines */
379 /* according to MkLinux. -- jmt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 via2[gBufB] |= 0x02;
381 }
382
Finn Thaincd713dd2007-05-01 22:32:57 +0200383 /* Disable all the slot interrupts (where possible). */
384
385 switch (macintosh_config->via_type) {
386 case MAC_VIA_II:
387 /* Just make the port A lines inputs. */
388 switch(macintosh_config->ident) {
389 case MAC_MODEL_II:
390 case MAC_MODEL_IIX:
391 case MAC_MODEL_IICX:
392 case MAC_MODEL_SE30:
393 /* The top two bits are RAM size outputs. */
394 via2[vDirA] &= 0xC0;
395 break;
396 default:
397 via2[vDirA] &= 0x80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Finn Thaincd713dd2007-05-01 22:32:57 +0200399 break;
400 case MAC_VIA_IIci:
401 /* RBV. Disable all the slot interrupts. SIER works like IER. */
402 via2[rSIER] = 0x7F;
403 break;
404 case MAC_VIA_QUADRA:
405 /* Disable the inactive slot interrupts by making those lines outputs. */
406 if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
407 (macintosh_config->adb_type != MAC_ADB_PB2)) {
408 via2[vBufA] |= 0x7F;
409 via2[vDirA] |= 0x7F;
410 }
411 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413}
414
415/*
416 * The generic VIA interrupt routines (shamelessly stolen from Alan Cox's
417 * via6522.c :-), disable/pending masks added.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
419
Geert Uytterhoeven9145db52011-08-10 12:48:29 +0200420#ifdef CONFIG_GENERIC_HARDIRQS
421void via1_irq(unsigned int irq, struct irq_desc *desc)
422{
423 int irq_num;
424 unsigned char irq_bit, events;
425
426 events = via1[vIFR] & via1[vIER] & 0x7F;
427 if (!events)
428 return;
429
430 irq_num = VIA1_SOURCE_BASE;
431 irq_bit = 1;
432 do {
433 if (events & irq_bit) {
434 via1[vIFR] = irq_bit;
435 generic_handle_irq(irq_num);
436 }
437 ++irq_num;
438 irq_bit <<= 1;
439 } while (events >= irq_bit);
440}
441
442static void via2_irq(unsigned int irq, struct irq_desc *desc)
443{
444 int irq_num;
445 unsigned char irq_bit, events;
446
447 events = via2[gIFR] & via2[gIER] & 0x7F;
448 if (!events)
449 return;
450
451 irq_num = VIA2_SOURCE_BASE;
452 irq_bit = 1;
453 do {
454 if (events & irq_bit) {
455 via2[gIFR] = irq_bit | rbv_clear;
456 generic_handle_irq(irq_num);
457 }
458 ++irq_num;
459 irq_bit <<= 1;
460 } while (events >= irq_bit);
461}
462#else
Al Viro2850bc22006-10-07 14:16:45 +0100463irqreturn_t via1_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Finn Thain67dfb152007-05-01 22:32:56 +0200465 int irq_num;
466 unsigned char irq_bit, events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Finn Thain67dfb152007-05-01 22:32:56 +0200468 events = via1[vIFR] & via1[vIER] & 0x7F;
469 if (!events)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return IRQ_NONE;
471
Finn Thain67dfb152007-05-01 22:32:56 +0200472 irq_num = VIA1_SOURCE_BASE;
473 irq_bit = 1;
474 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (events & irq_bit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 via1[vIFR] = irq_bit;
Geert Uytterhoeven1425df82011-07-01 20:39:19 +0200477 generic_handle_irq(irq_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Finn Thain67dfb152007-05-01 22:32:56 +0200479 ++irq_num;
480 irq_bit <<= 1;
481 } while (events >= irq_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return IRQ_HANDLED;
483}
484
Al Viro2850bc22006-10-07 14:16:45 +0100485irqreturn_t via2_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Finn Thain67dfb152007-05-01 22:32:56 +0200487 int irq_num;
488 unsigned char irq_bit, events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Finn Thain67dfb152007-05-01 22:32:56 +0200490 events = via2[gIFR] & via2[gIER] & 0x7F;
491 if (!events)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return IRQ_NONE;
493
Finn Thain67dfb152007-05-01 22:32:56 +0200494 irq_num = VIA2_SOURCE_BASE;
495 irq_bit = 1;
496 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (events & irq_bit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 via2[gIFR] = irq_bit | rbv_clear;
Geert Uytterhoeven1425df82011-07-01 20:39:19 +0200499 generic_handle_irq(irq_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Finn Thain67dfb152007-05-01 22:32:56 +0200501 ++irq_num;
502 irq_bit <<= 1;
503 } while (events >= irq_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return IRQ_HANDLED;
505}
Geert Uytterhoeven9145db52011-08-10 12:48:29 +0200506#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508/*
509 * Dispatch Nubus interrupts. We are called as a secondary dispatch by the
510 * VIA2 dispatcher as a fast interrupt handler.
511 */
512
Geert Uytterhoeven9145db52011-08-10 12:48:29 +0200513#ifdef CONFIG_GENERIC_HARDIRQS
514void via_nubus_irq(unsigned int irq, struct irq_desc *desc)
515{
516 int slot_irq;
517 unsigned char slot_bit, events;
518
519 events = ~via2[gBufA] & 0x7F;
520 if (rbv_present)
521 events &= via2[rSIER];
522 else
523 events &= ~via2[vDirA];
524 if (!events)
525 return;
526
527 do {
528 slot_irq = IRQ_NUBUS_F;
529 slot_bit = 0x40;
530 do {
531 if (events & slot_bit) {
532 events &= ~slot_bit;
533 generic_handle_irq(slot_irq);
534 }
535 --slot_irq;
536 slot_bit >>= 1;
537 } while (events);
538
539 /* clear the CA1 interrupt and make certain there's no more. */
540 via2[gIFR] = 0x02 | rbv_clear;
541 events = ~via2[gBufA] & 0x7F;
542 if (rbv_present)
543 events &= via2[rSIER];
544 else
545 events &= ~via2[vDirA];
546 } while (events);
547}
548#else
Al Viro2850bc22006-10-07 14:16:45 +0100549irqreturn_t via_nubus_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Finn Thain67dfb152007-05-01 22:32:56 +0200551 int slot_irq;
552 unsigned char slot_bit, events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Finn Thain67dfb152007-05-01 22:32:56 +0200554 events = ~via2[gBufA] & 0x7F;
555 if (rbv_present)
556 events &= via2[rSIER];
557 else
Finn Thaincd713dd2007-05-01 22:32:57 +0200558 events &= ~via2[vDirA];
Finn Thain67dfb152007-05-01 22:32:56 +0200559 if (!events)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return IRQ_NONE;
561
Finn Thain67dfb152007-05-01 22:32:56 +0200562 do {
563 slot_irq = IRQ_NUBUS_F;
564 slot_bit = 0x40;
565 do {
566 if (events & slot_bit) {
567 events &= ~slot_bit;
Geert Uytterhoeven1425df82011-07-01 20:39:19 +0200568 generic_handle_irq(slot_irq);
Finn Thain67dfb152007-05-01 22:32:56 +0200569 }
570 --slot_irq;
571 slot_bit >>= 1;
572 } while (events);
573
574 /* clear the CA1 interrupt and make certain there's no more. */
575 via2[gIFR] = 0x02 | rbv_clear;
576 events = ~via2[gBufA] & 0x7F;
577 if (rbv_present)
578 events &= via2[rSIER];
579 else
Finn Thaincd713dd2007-05-01 22:32:57 +0200580 events &= ~via2[vDirA];
Finn Thain67dfb152007-05-01 22:32:56 +0200581 } while (events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return IRQ_HANDLED;
583}
Geert Uytterhoeven9145db52011-08-10 12:48:29 +0200584#endif
585
586/*
587 * Register the interrupt dispatchers for VIA or RBV machines only.
588 */
589
590void __init via_register_interrupts(void)
591{
592#ifdef CONFIG_GENERIC_HARDIRQS
593 if (via_alt_mapping) {
594 /* software interrupt */
595 irq_set_chained_handler(IRQ_AUTO_1, via1_irq);
596 /* via1 interrupt */
597 irq_set_chained_handler(IRQ_AUTO_6, via1_irq);
598 } else {
599 irq_set_chained_handler(IRQ_AUTO_1, via1_irq);
600 }
601 irq_set_chained_handler(IRQ_AUTO_2, via2_irq);
602 irq_set_chained_handler(IRQ_MAC_NUBUS, via_nubus_irq);
603#else
604 if (via_alt_mapping) {
605 if (request_irq(IRQ_AUTO_1, via1_irq, 0, "software",
606 (void *)via1))
607 pr_err("Couldn't register %s interrupt\n", "software");
608 if (request_irq(IRQ_AUTO_6, via1_irq, 0, "via1", (void *)via1))
609 pr_err("Couldn't register %s interrupt\n", "via1");
610 } else {
611 if (request_irq(IRQ_AUTO_1, via1_irq, 0, "via1", (void *)via1))
612 pr_err("Couldn't register %s interrupt\n", "via1");
613 }
614 if (request_irq(IRQ_AUTO_2, via2_irq, 0, "via2", (void *)via2))
615 pr_err("Couldn't register %s interrupt\n", "via2");
616 if (request_irq(IRQ_MAC_NUBUS, via_nubus_irq, 0, "nubus",
617 (void *)via2))
618 pr_err("Couldn't register %s interrupt\n", "nubus");
619#endif
620}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622void via_irq_enable(int irq) {
623 int irq_src = IRQ_SRC(irq);
624 int irq_idx = IRQ_IDX(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626#ifdef DEBUG_IRQUSE
627 printk(KERN_DEBUG "via_irq_enable(%d)\n", irq);
628#endif
629
630 if (irq_src == 1) {
Finn Thaincd713dd2007-05-01 22:32:57 +0200631 via1[vIER] = IER_SET_BIT(irq_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 } else if (irq_src == 2) {
Finn Thaincd713dd2007-05-01 22:32:57 +0200633 if (irq != IRQ_MAC_NUBUS || nubus_disabled == 0)
634 via2[gIER] = IER_SET_BIT(irq_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 } else if (irq_src == 7) {
Finn Thaincd713dd2007-05-01 22:32:57 +0200636 switch (macintosh_config->via_type) {
637 case MAC_VIA_II:
638 nubus_disabled &= ~(1 << irq_idx);
639 /* Enable the CA1 interrupt when no slot is disabled. */
640 if (!nubus_disabled)
641 via2[gIER] = IER_SET_BIT(1);
642 break;
643 case MAC_VIA_IIci:
644 /* On RBV, enable the slot interrupt.
645 * SIER works like IER.
646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 via2[rSIER] = IER_SET_BIT(irq_idx);
Finn Thaincd713dd2007-05-01 22:32:57 +0200648 break;
649 case MAC_VIA_QUADRA:
650 /* Make the port A line an input to enable the slot irq.
651 * But not on PowerBooks, that's ADB.
652 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
Finn Thaincd713dd2007-05-01 22:32:57 +0200654 (macintosh_config->adb_type != MAC_ADB_PB2))
655 via2[vDirA] &= ~(1 << irq_idx);
656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659}
660
661void via_irq_disable(int irq) {
662 int irq_src = IRQ_SRC(irq);
663 int irq_idx = IRQ_IDX(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665#ifdef DEBUG_IRQUSE
666 printk(KERN_DEBUG "via_irq_disable(%d)\n", irq);
667#endif
668
669 if (irq_src == 1) {
Finn Thaincd713dd2007-05-01 22:32:57 +0200670 via1[vIER] = IER_CLR_BIT(irq_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 } else if (irq_src == 2) {
Finn Thaincd713dd2007-05-01 22:32:57 +0200672 via2[gIER] = IER_CLR_BIT(irq_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 } else if (irq_src == 7) {
Finn Thaincd713dd2007-05-01 22:32:57 +0200674 switch (macintosh_config->via_type) {
675 case MAC_VIA_II:
676 nubus_disabled |= 1 << irq_idx;
677 if (nubus_disabled)
678 via2[gIER] = IER_CLR_BIT(1);
679 break;
680 case MAC_VIA_IIci:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 via2[rSIER] = IER_CLR_BIT(irq_idx);
Finn Thaincd713dd2007-05-01 22:32:57 +0200682 break;
683 case MAC_VIA_QUADRA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
Finn Thaincd713dd2007-05-01 22:32:57 +0200685 (macintosh_config->adb_type != MAC_ADB_PB2))
686 via2[vDirA] |= 1 << irq_idx;
687 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690}
691
692void via_irq_clear(int irq) {
693 int irq_src = IRQ_SRC(irq);
694 int irq_idx = IRQ_IDX(irq);
695 int irq_bit = 1 << irq_idx;
696
697 if (irq_src == 1) {
698 via1[vIFR] = irq_bit;
699 } else if (irq_src == 2) {
700 via2[gIFR] = irq_bit | rbv_clear;
701 } else if (irq_src == 7) {
Finn Thain67dfb152007-05-01 22:32:56 +0200702 /* FIXME: There is no way to clear an individual nubus slot
703 * IRQ flag, other than getting the device to do it.
704 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706}
707
708/*
709 * Returns nonzero if an interrupt is pending on the given
710 * VIA/IRQ combination.
711 */
712
713int via_irq_pending(int irq)
714{
715 int irq_src = IRQ_SRC(irq);
716 int irq_idx = IRQ_IDX(irq);
717 int irq_bit = 1 << irq_idx;
718
719 if (irq_src == 1) {
720 return via1[vIFR] & irq_bit;
721 } else if (irq_src == 2) {
722 return via2[gIFR] & irq_bit;
723 } else if (irq_src == 7) {
Finn Thaincd713dd2007-05-01 22:32:57 +0200724 /* Always 0 for MAC_VIA_QUADRA if the slot irq is disabled. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return ~via2[gBufA] & irq_bit;
726 }
727 return 0;
728}
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100729
730void via1_set_head(int head)
731{
732 if (head == 0)
733 via1[vBufA] &= ~VIA1A_vHeadSel;
734 else
735 via1[vBufA] |= VIA1A_vHeadSel;
736}
737EXPORT_SYMBOL(via1_set_head);