2 * 6522 Versatile Interface Adapter (VIA)
4 * There are two of these on the Mac II. Some IRQ's are vectored
5 * via them as are assorted bits and bobs - eg RTC, ADB.
7 * CSA: Motorola seems to have removed documentation on the 6522 from
9 * http://nerini.drf.com/vectrex/other/text/chips/6522/
10 * http://www.zymurgy.net/classic/vic20/vicdet1.htm
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
16 * 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
20 * 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)
25 #include <linux/types.h>
26 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/init.h>
30 #include <linux/ide.h>
32 #include <asm/bootinfo.h>
33 #include <asm/macintosh.h>
34 #include <asm/macints.h>
35 #include <asm/machw.h>
36 #include <asm/mac_via.h>
37 #include <asm/mac_psc.h>
39 volatile __u8 *via1, *via2;
41 /* See note in mac_via.h about how this is possibly not useful */
42 volatile long *via_memory_bogon=(long *)&via_memory_bogon;
44 int rbv_present, via_alt_mapping;
48 * Globals for accessing the VIA chip registers without having to
49 * check if we're hitting a real VIA or an RBV. Normally you could
50 * just hit the combined register (ie, vIER|rIER) but that seems to
51 * break on AV Macs...probably because they actually decode more than
52 * eight address bits. Why can't Apple engineers at least be
53 * _consistently_ lazy? - 1999-05-21 (jmt)
56 static int gIER,gIFR,gBufA,gBufB;
62 #define TICK_SIZE 10000
63 #define MAC_CLOCK_TICK (783300/HZ) /* ticks per HZ */
64 #define MAC_CLOCK_LOW (MAC_CLOCK_TICK&0xFF)
65 #define MAC_CLOCK_HIGH (MAC_CLOCK_TICK>>8)
67 static int nubus_active;
69 void via_debug_dump(void);
70 irqreturn_t via1_irq(int, void *);
71 irqreturn_t via2_irq(int, void *);
72 irqreturn_t via_nubus_irq(int, void *);
73 void via_irq_enable(int irq);
74 void via_irq_disable(int irq);
75 void via_irq_clear(int irq);
77 extern irqreturn_t mac_scc_dispatch(int, void *);
78 extern int oss_present;
83 * First we figure out where they actually _are_ as well as what type of
84 * VIA we have for VIA2 (it could be a real VIA or an RBV or even an OSS.)
85 * Then we pretty much clear them out and disable all IRQ sources.
87 * Note: the OSS is actually "detected" here and not in oss_init(). It just
88 * seems more logical to do it here since via_init() needs to know
89 * these things anyways.
92 void __init via_init(void)
94 switch(macintosh_config->via_type) {
96 /* IIci, IIsi, IIvx, IIvi (P6xx), LC series */
99 via1 = (void *) VIA1_BASE;
100 if (macintosh_config->ident == MAC_MODEL_IIFX) {
105 via2 = (void *) RBV_BASE;
109 if (macintosh_config->ident == MAC_MODEL_LCIII) {
112 /* on most RBVs (& unlike the VIAs), you */
113 /* need to set bit 7 when you write to IFR */
114 /* in order for your clear to occur. */
123 /* Quadra and early MacIIs agree on the VIA locations */
127 via1 = (void *) VIA1_BASE;
128 via2 = (void *) VIA2_BASE;
138 panic("UNKNOWN VIA TYPE");
141 printk(KERN_INFO "VIA1 at %p is a 6522 or clone\n", via1);
143 printk(KERN_INFO "VIA2 at %p is ", via2);
146 } else if (oss_present) {
149 printk("a 6522 or clone\n");
157 * Shut down all IRQ sources, reset the timers, and
158 * kill the timer latch on VIA1.
170 via1[vACR] &= ~0x03; /* disable port A & B latches */
173 * SE/30: disable video IRQ
174 * XXX: testing for SE/30 VBL
177 if (macintosh_config->ident == MAC_MODEL_SE30) {
183 * Set the RTC bits to a known state: all lines to outputs and
184 * RTC disabled (yes that's 0 to enable and 1 to disable).
187 via1[vDirB] |= (VIA1B_vRTCEnb | VIA1B_vRTCClk | VIA1B_vRTCData);
188 via1[vBufB] |= (VIA1B_vRTCEnb | VIA1B_vRTCClk);
190 /* Everything below this point is VIA2/RBV only... */
192 if (oss_present) return;
195 /* Some machines support an alternate IRQ mapping that spreads */
196 /* Ethernet and Sound out to their own autolevel IRQs and moves */
197 /* VIA1 to level 6. A/UX uses this mapping and we do too. Note */
198 /* that the IIfx emulates this alternate mapping using the OSS. */
200 switch(macintosh_config->ident) {
202 case MAC_MODEL_P475F:
205 case MAC_MODEL_Q605_ACC:
217 via1[vBufB] &= ~0x40;
228 * Now initialize VIA2. For RBV we just kill all interrupts;
229 * for a regular VIA we also reset the timers and stuff.
233 via2[gIFR] = 0x7F | rbv_clear;
242 via2[vACR] &= ~0x03; /* disable port A & B latches */
246 * Set vPCR for SCSI interrupts (but not on RBV)
249 if (macintosh_config->scsi_type == MAC_SCSI_OLD) {
250 /* CB2 (IRQ) indep. input, positive edge */
251 /* CA2 (DRQ) indep. input, positive edge */
254 /* CB2 (IRQ) indep. input, negative edge */
255 /* CA2 (DRQ) indep. input, negative edge */
262 * Start the 100 Hz clock
265 void __init via_init_clock(irq_handler_t func)
268 via1[vT1LL] = MAC_CLOCK_LOW;
269 via1[vT1LH] = MAC_CLOCK_HIGH;
270 via1[vT1CL] = MAC_CLOCK_LOW;
271 via1[vT1CH] = MAC_CLOCK_HIGH;
273 request_irq(IRQ_MAC_TIMER_1, func, IRQ_FLG_LOCK, "timer", func);
277 * Register the interrupt dispatchers for VIA or RBV machines only.
280 void __init via_register_interrupts(void)
282 if (via_alt_mapping) {
283 request_irq(IRQ_AUTO_1, via1_irq,
284 IRQ_FLG_LOCK|IRQ_FLG_FAST, "software",
286 request_irq(IRQ_AUTO_6, via1_irq,
287 IRQ_FLG_LOCK|IRQ_FLG_FAST, "via1",
290 request_irq(IRQ_AUTO_1, via1_irq,
291 IRQ_FLG_LOCK|IRQ_FLG_FAST, "via1",
294 request_irq(IRQ_AUTO_2, via2_irq, IRQ_FLG_LOCK|IRQ_FLG_FAST,
295 "via2", (void *) via2);
297 request_irq(IRQ_AUTO_4, mac_scc_dispatch, IRQ_FLG_LOCK,
298 "scc", mac_scc_dispatch);
300 request_irq(IRQ_MAC_NUBUS, via_nubus_irq, IRQ_FLG_LOCK|IRQ_FLG_FAST,
301 "nubus", (void *) via2);
305 * Debugging dump, used in various places to see what's going on.
308 void via_debug_dump(void)
310 printk(KERN_DEBUG "VIA1: DDRA = 0x%02X DDRB = 0x%02X ACR = 0x%02X\n",
311 (uint) via1[vDirA], (uint) via1[vDirB], (uint) via1[vACR]);
312 printk(KERN_DEBUG " PCR = 0x%02X IFR = 0x%02X IER = 0x%02X\n",
313 (uint) via1[vPCR], (uint) via1[vIFR], (uint) via1[vIER]);
315 printk(KERN_DEBUG "VIA2: <OSS>\n");
316 } else if (rbv_present) {
317 printk(KERN_DEBUG "VIA2: IFR = 0x%02X IER = 0x%02X\n",
318 (uint) via2[rIFR], (uint) via2[rIER]);
319 printk(KERN_DEBUG " SIFR = 0x%02X SIER = 0x%02X\n",
320 (uint) via2[rSIFR], (uint) via2[rSIER]);
322 printk(KERN_DEBUG "VIA2: DDRA = 0x%02X DDRB = 0x%02X ACR = 0x%02X\n",
323 (uint) via2[vDirA], (uint) via2[vDirB],
325 printk(KERN_DEBUG " PCR = 0x%02X IFR = 0x%02X IER = 0x%02X\n",
327 (uint) via2[vIFR], (uint) via2[vIER]);
332 * This is always executed with interrupts disabled.
334 * TBI: get time offset between scheduling timer ticks
337 unsigned long mac_gettimeoffset (void)
339 unsigned long ticks, offset = 0;
341 /* read VIA1 timer 2 current value */
342 ticks = via1[vT1CL] | (via1[vT1CH] << 8);
343 /* The probability of underflow is less than 2% */
344 if (ticks > MAC_CLOCK_TICK - MAC_CLOCK_TICK / 50)
345 /* Check for pending timer interrupt in VIA1 IFR */
346 if (via1[vIFR] & 0x40) offset = TICK_SIZE;
348 ticks = MAC_CLOCK_TICK - ticks;
349 ticks = ticks * 10000L / MAC_CLOCK_TICK;
351 return ticks + offset;
355 * Flush the L2 cache on Macs that have it by flipping
356 * the system into 24-bit mode for an instant.
359 void via_flush_cache(void)
361 via2[gBufB] &= ~VIA2B_vMode32;
362 via2[gBufB] |= VIA2B_vMode32;
366 * Return the status of the L2 cache on a IIci
369 int via_get_cache_disable(void)
371 /* Safeguard against being called accidentally */
373 printk(KERN_ERR "via_get_cache_disable called on a non-VIA machine!\n");
377 return (int) via2[gBufB] & VIA2B_vCDis;
381 * Initialize VIA2 for Nubus access
384 void __init via_nubus_init(void)
386 /* don't set nubus_active = 0 here, it kills the Baboon */
387 /* interrupt that we've already registered. */
389 /* unlock nubus transactions */
391 if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
392 (macintosh_config->adb_type != MAC_ADB_PB2)) {
393 /* set the line to be an output on non-RBV machines */
397 /* this seems to be an ADB bit on PMU machines */
398 /* according to MkLinux. -- jmt */
402 /* disable nubus slot interrupts. */
405 via2[rSIER] = nubus_active | 0x80;
407 /* These are ADB bits on PMU */
408 if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
409 (macintosh_config->adb_type != MAC_ADB_PB2)) {
410 switch(macintosh_config->ident)
417 via2[vDirA] = ~nubus_active | 0xc0;
421 via2[vDirA] = ~nubus_active;
428 * The generic VIA interrupt routines (shamelessly stolen from Alan Cox's
429 * via6522.c :-), disable/pending masks added.
431 * The new interrupt architecture in macints.c takes care of a lot of the
432 * gruntwork for us, including tallying the interrupts and calling the
433 * handlers on the linked list. All we need to do here is basically generate
434 * the machspec interrupt number after clearing the interrupt.
437 irqreturn_t via1_irq(int irq, void *dev_id)
440 unsigned char irq_bit, events;
442 events = via1[vIFR] & via1[vIER] & 0x7F;
446 irq_num = VIA1_SOURCE_BASE;
449 if (events & irq_bit) {
450 via1[vIER] = irq_bit;
451 via1[vIFR] = irq_bit;
452 m68k_handle_int(irq_num);
453 via1[vIER] = irq_bit | 0x80;
457 } while (events >= irq_bit);
459 #if 0 /* freakin' pmu is doing weird stuff */
461 /* This (still) seems to be necessary to get IDE
462 working. However, if you enable VBL interrupts,
464 /* FIXME: should we check the SLOTIRQ bit before
465 pulling this stunt? */
466 /* No, it won't be set. that's why we're doing this. */
467 via_irq_disable(IRQ_MAC_NUBUS);
468 via_irq_clear(IRQ_MAC_NUBUS);
469 m68k_handle_int(IRQ_MAC_NUBUS);
470 via_irq_enable(IRQ_MAC_NUBUS);
476 irqreturn_t via2_irq(int irq, void *dev_id)
479 unsigned char irq_bit, events;
481 events = via2[gIFR] & via2[gIER] & 0x7F;
485 irq_num = VIA2_SOURCE_BASE;
488 if (events & irq_bit) {
489 via2[gIER] = irq_bit;
490 via2[gIFR] = irq_bit | rbv_clear;
491 m68k_handle_int(irq_num);
492 via2[gIER] = irq_bit | 0x80;
496 } while (events >= irq_bit);
501 * Dispatch Nubus interrupts. We are called as a secondary dispatch by the
502 * VIA2 dispatcher as a fast interrupt handler.
505 irqreturn_t via_nubus_irq(int irq, void *dev_id)
508 unsigned char slot_bit, events;
510 events = ~via2[gBufA] & 0x7F;
512 events &= via2[rSIER];
514 events &= nubus_active;
519 slot_irq = IRQ_NUBUS_F;
522 if (events & slot_bit) {
524 m68k_handle_int(slot_irq);
530 /* clear the CA1 interrupt and make certain there's no more. */
531 via2[gIFR] = 0x02 | rbv_clear;
532 events = ~via2[gBufA] & 0x7F;
534 events &= via2[rSIER];
536 events &= nubus_active;
541 void via_irq_enable(int irq) {
542 int irq_src = IRQ_SRC(irq);
543 int irq_idx = IRQ_IDX(irq);
544 int irq_bit = 1 << irq_idx;
547 printk(KERN_DEBUG "via_irq_enable(%d)\n", irq);
551 via1[vIER] = irq_bit | 0x80;
552 } else if (irq_src == 2) {
553 via2[gIER] = irq_bit | 0x80;
554 } else if (irq_src == 7) {
555 nubus_active |= irq_bit;
557 /* enable the slot interrupt. SIER works like IER. */
558 via2[rSIER] = IER_SET_BIT(irq_idx);
560 /* Make sure the bit is an input, to enable the irq */
561 /* But not on PowerBooks, that's ADB... */
562 if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
563 (macintosh_config->adb_type != MAC_ADB_PB2)) {
564 switch(macintosh_config->ident)
570 via2[vDirA] &= (~irq_bit | 0xc0);
573 via2[vDirA] &= ~irq_bit;
580 void via_irq_disable(int irq) {
581 int irq_src = IRQ_SRC(irq);
582 int irq_idx = IRQ_IDX(irq);
583 int irq_bit = 1 << irq_idx;
586 printk(KERN_DEBUG "via_irq_disable(%d)\n", irq);
590 via1[vIER] = irq_bit & 0x7F;
591 } else if (irq_src == 2) {
592 via2[gIER] = irq_bit & 0x7F;
593 } else if (irq_src == 7) {
595 /* disable the slot interrupt. SIER works like IER. */
596 via2[rSIER] = IER_CLR_BIT(irq_idx);
598 /* disable the nubus irq by changing dir to output */
600 if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
601 (macintosh_config->adb_type != MAC_ADB_PB2)) {
602 via2[vDirA] |= irq_bit;
605 nubus_active &= ~irq_bit;
609 void via_irq_clear(int irq) {
610 int irq_src = IRQ_SRC(irq);
611 int irq_idx = IRQ_IDX(irq);
612 int irq_bit = 1 << irq_idx;
615 via1[vIFR] = irq_bit;
616 } else if (irq_src == 2) {
617 via2[gIFR] = irq_bit | rbv_clear;
618 } else if (irq_src == 7) {
619 /* FIXME: There is no way to clear an individual nubus slot
620 * IRQ flag, other than getting the device to do it.
626 * Returns nonzero if an interrupt is pending on the given
627 * VIA/IRQ combination.
630 int via_irq_pending(int irq)
632 int irq_src = IRQ_SRC(irq);
633 int irq_idx = IRQ_IDX(irq);
634 int irq_bit = 1 << irq_idx;
637 return via1[vIFR] & irq_bit;
638 } else if (irq_src == 2) {
639 return via2[gIFR] & irq_bit;
640 } else if (irq_src == 7) {
641 /* FIXME: this can't work while a slot irq is disabled! */
642 return ~via2[gBufA] & irq_bit;