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 * PRAM/RTC access algorithms are from the NetBSD RTC toolkit version 1.08b
17 * by Erik Vogan and adapted to Linux by Joshua M. Thompson (funaho@jurai.org)
21 #include <linux/types.h>
22 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/ide.h>
28 #include <asm/bootinfo.h>
29 #include <asm/macintosh.h>
30 #include <asm/macints.h>
31 #include <asm/machw.h>
32 #include <asm/mac_via.h>
33 #include <asm/mac_psc.h>
35 volatile __u8
*via1
, *via2
;
37 /* See note in mac_via.h about how this is possibly not useful */
38 volatile long *via_memory_bogon
=(long *)&via_memory_bogon
;
40 int rbv_present
,via_alt_mapping
;
44 * Globals for accessing the VIA chip registers without having to
45 * check if we're hitting a real VIA or an RBV. Normally you could
46 * just hit the combined register (ie, vIER|rIER) but that seems to
47 * break on AV Macs...probably because they actually decode more than
48 * eight address bits. Why can't Apple engineers at least be
49 * _consistently_ lazy? - 1999-05-21 (jmt)
52 static int gIER
,gIFR
,gBufA
,gBufB
;
58 #define TICK_SIZE 10000
59 #define MAC_CLOCK_TICK (783300/HZ) /* ticks per HZ */
60 #define MAC_CLOCK_LOW (MAC_CLOCK_TICK&0xFF)
61 #define MAC_CLOCK_HIGH (MAC_CLOCK_TICK>>8)
63 static int nubus_active
;
65 void via_debug_dump(void);
66 irqreturn_t
via1_irq(int, void *);
67 irqreturn_t
via2_irq(int, void *);
68 irqreturn_t
via_nubus_irq(int, void *);
69 void via_irq_enable(int irq
);
70 void via_irq_disable(int irq
);
71 void via_irq_clear(int irq
);
73 extern irqreturn_t
mac_scc_dispatch(int, void *);
74 extern int oss_present
;
79 * First we figure out where they actually _are_ as well as what type of
80 * VIA we have for VIA2 (it could be a real VIA or an RBV or even an OSS.)
81 * Then we pretty much clear them out and disable all IRQ sources.
83 * Note: the OSS is actually "detected" here and not in oss_init(). It just
84 * seems more logical to do it here since via_init() needs to know
85 * these things anyways.
88 void __init
via_init(void)
90 switch(macintosh_config
->via_type
) {
92 /* IIci, IIsi, IIvx, IIvi (P6xx), LC series */
95 via1
= (void *) VIA1_BASE
;
96 if (macintosh_config
->ident
== MAC_MODEL_IIFX
) {
101 via2
= (void *) RBV_BASE
;
105 if (macintosh_config
->ident
== MAC_MODEL_LCIII
) {
108 /* on most RBVs (& unlike the VIAs), you */
109 /* need to set bit 7 when you write to IFR */
110 /* in order for your clear to occur. */
119 /* Quadra and early MacIIs agree on the VIA locations */
123 via1
= (void *) VIA1_BASE
;
124 via2
= (void *) VIA2_BASE
;
134 panic("UNKNOWN VIA TYPE");
137 printk(KERN_INFO
"VIA1 at %p is a 6522 or clone\n", via1
);
139 printk(KERN_INFO
"VIA2 at %p is ", via2
);
141 printk(KERN_INFO
"an RBV\n");
142 } else if (oss_present
) {
143 printk(KERN_INFO
"an OSS\n");
145 printk(KERN_INFO
"a 6522 or clone\n");
153 * Shut down all IRQ sources, reset the timers, and
154 * kill the timer latch on VIA1.
168 * SE/30: disable video IRQ
169 * XXX: testing for SE/30 VBL
172 if (macintosh_config
->ident
== MAC_MODEL_SE30
) {
178 * Set the RTC bits to a known state: all lines to outputs and
179 * RTC disabled (yes that's 0 to enable and 1 to disable).
182 via1
[vDirB
] |= (VIA1B_vRTCEnb
| VIA1B_vRTCClk
| VIA1B_vRTCData
);
183 via1
[vBufB
] |= (VIA1B_vRTCEnb
| VIA1B_vRTCClk
);
185 /* Everything below this point is VIA2/RBV only... */
187 if (oss_present
) return;
190 /* Some machines support an alternate IRQ mapping that spreads */
191 /* Ethernet and Sound out to their own autolevel IRQs and moves */
192 /* VIA1 to level 6. A/UX uses this mapping and we do too. Note */
193 /* that the IIfx emulates this alternate mapping using the OSS. */
195 switch(macintosh_config
->ident
) {
197 case MAC_MODEL_P475F
:
200 case MAC_MODEL_Q605_ACC
:
212 via1
[vBufB
] &= ~0x40;
223 * Now initialize VIA2. For RBV we just kill all interrupts;
224 * for a regular VIA we also reset the timers and stuff.
228 via2
[gIFR
] = 0x7F | rbv_clear
;
241 * Start the 100 Hz clock
244 void __init
via_init_clock(irq_handler_t func
)
247 via1
[vT1LL
] = MAC_CLOCK_LOW
;
248 via1
[vT1LH
] = MAC_CLOCK_HIGH
;
249 via1
[vT1CL
] = MAC_CLOCK_LOW
;
250 via1
[vT1CH
] = MAC_CLOCK_HIGH
;
252 request_irq(IRQ_MAC_TIMER_1
, func
, IRQ_FLG_LOCK
, "timer", func
);
256 * Register the interrupt dispatchers for VIA or RBV machines only.
259 void __init
via_register_interrupts(void)
261 if (via_alt_mapping
) {
262 request_irq(IRQ_AUTO_1
, via1_irq
,
263 IRQ_FLG_LOCK
|IRQ_FLG_FAST
, "software",
265 request_irq(IRQ_AUTO_6
, via1_irq
,
266 IRQ_FLG_LOCK
|IRQ_FLG_FAST
, "via1",
269 request_irq(IRQ_AUTO_1
, via1_irq
,
270 IRQ_FLG_LOCK
|IRQ_FLG_FAST
, "via1",
273 request_irq(IRQ_AUTO_2
, via2_irq
, IRQ_FLG_LOCK
|IRQ_FLG_FAST
,
274 "via2", (void *) via2
);
276 request_irq(IRQ_AUTO_4
, mac_scc_dispatch
, IRQ_FLG_LOCK
,
277 "scc", mac_scc_dispatch
);
279 request_irq(IRQ_MAC_NUBUS
, via_nubus_irq
, IRQ_FLG_LOCK
|IRQ_FLG_FAST
,
280 "nubus", (void *) via2
);
284 * Debugging dump, used in various places to see what's going on.
287 void via_debug_dump(void)
289 printk(KERN_DEBUG
"VIA1: DDRA = 0x%02X DDRB = 0x%02X ACR = 0x%02X\n",
290 (uint
) via1
[vDirA
], (uint
) via1
[vDirB
], (uint
) via1
[vACR
]);
291 printk(KERN_DEBUG
" PCR = 0x%02X IFR = 0x%02X IER = 0x%02X\n",
292 (uint
) via1
[vPCR
], (uint
) via1
[vIFR
], (uint
) via1
[vIER
]);
294 printk(KERN_DEBUG
"VIA2: <OSS>\n");
295 } else if (rbv_present
) {
296 printk(KERN_DEBUG
"VIA2: IFR = 0x%02X IER = 0x%02X\n",
297 (uint
) via2
[rIFR
], (uint
) via2
[rIER
]);
298 printk(KERN_DEBUG
" SIFR = 0x%02X SIER = 0x%02X\n",
299 (uint
) via2
[rSIFR
], (uint
) via2
[rSIER
]);
301 printk(KERN_DEBUG
"VIA2: DDRA = 0x%02X DDRB = 0x%02X ACR = 0x%02X\n",
302 (uint
) via2
[vDirA
], (uint
) via2
[vDirB
],
304 printk(KERN_DEBUG
" PCR = 0x%02X IFR = 0x%02X IER = 0x%02X\n",
306 (uint
) via2
[vIFR
], (uint
) via2
[vIER
]);
311 * This is always executed with interrupts disabled.
313 * TBI: get time offset between scheduling timer ticks
316 unsigned long mac_gettimeoffset (void)
318 unsigned long ticks
, offset
= 0;
320 /* read VIA1 timer 2 current value */
321 ticks
= via1
[vT1CL
] | (via1
[vT1CH
] << 8);
322 /* The probability of underflow is less than 2% */
323 if (ticks
> MAC_CLOCK_TICK
- MAC_CLOCK_TICK
/ 50)
324 /* Check for pending timer interrupt in VIA1 IFR */
325 if (via1
[vIFR
] & 0x40) offset
= TICK_SIZE
;
327 ticks
= MAC_CLOCK_TICK
- ticks
;
328 ticks
= ticks
* 10000L / MAC_CLOCK_TICK
;
330 return ticks
+ offset
;
334 * Flush the L2 cache on Macs that have it by flipping
335 * the system into 24-bit mode for an instant.
338 void via_flush_cache(void)
340 via2
[gBufB
] &= ~VIA2B_vMode32
;
341 via2
[gBufB
] |= VIA2B_vMode32
;
345 * Return the status of the L2 cache on a IIci
348 int via_get_cache_disable(void)
350 /* Safeguard against being called accidentally */
352 printk(KERN_ERR
"via_get_cache_disable called on a non-VIA machine!\n");
356 return (int) via2
[gBufB
] & VIA2B_vCDis
;
360 * Initialize VIA2 for Nubus access
363 void __init
via_nubus_init(void)
365 /* don't set nubus_active = 0 here, it kills the Baboon */
366 /* interrupt that we've already registered. */
368 /* unlock nubus transactions */
371 /* set the line to be an output on non-RBV machines */
372 if ((macintosh_config
->adb_type
!= MAC_ADB_PB1
) &&
373 (macintosh_config
->adb_type
!= MAC_ADB_PB2
)) {
378 /* this seems to be an ADB bit on PMU machines */
379 /* according to MkLinux. -- jmt */
381 if ((macintosh_config
->adb_type
!= MAC_ADB_PB1
) &&
382 (macintosh_config
->adb_type
!= MAC_ADB_PB2
)) {
386 /* disable nubus slot interrupts. */
389 via2
[rSIER
] = nubus_active
| 0x80;
391 /* These are ADB bits on PMU */
392 if ((macintosh_config
->adb_type
!= MAC_ADB_PB1
) &&
393 (macintosh_config
->adb_type
!= MAC_ADB_PB2
)) {
394 switch(macintosh_config
->ident
)
401 via2
[vDirA
] = ~nubus_active
| 0xc0;
405 via2
[vDirA
] = ~nubus_active
;
412 * The generic VIA interrupt routines (shamelessly stolen from Alan Cox's
413 * via6522.c :-), disable/pending masks added.
415 * The new interrupt architecture in macints.c takes care of a lot of the
416 * gruntwork for us, including tallying the interrupts and calling the
417 * handlers on the linked list. All we need to do here is basically generate
418 * the machspec interrupt number after clearing the interrupt.
421 irqreturn_t
via1_irq(int irq
, void *dev_id
)
424 unsigned char events
, mask
;
426 mask
= via1
[vIER
] & 0x7F;
427 if (!(events
= via1
[vIFR
] & mask
))
430 for (i
= 0, irq_bit
= 1 ; i
< 7 ; i
++, irq_bit
<<= 1)
431 if (events
& irq_bit
) {
432 via1
[vIER
] = irq_bit
;
433 m68k_handle_int(VIA1_SOURCE_BASE
+ i
);
434 via1
[vIFR
] = irq_bit
;
435 via1
[vIER
] = irq_bit
| 0x80;
438 #if 0 /* freakin' pmu is doing weird stuff */
440 /* This (still) seems to be necessary to get IDE
441 working. However, if you enable VBL interrupts,
443 /* FIXME: should we check the SLOTIRQ bit before
444 pulling this stunt? */
445 /* No, it won't be set. that's why we're doing this. */
446 via_irq_disable(IRQ_MAC_NUBUS
);
447 via_irq_clear(IRQ_MAC_NUBUS
);
448 m68k_handle_int(IRQ_MAC_NUBUS
);
449 via_irq_enable(IRQ_MAC_NUBUS
);
455 irqreturn_t
via2_irq(int irq
, void *dev_id
)
458 unsigned char events
, mask
;
460 mask
= via2
[gIER
] & 0x7F;
461 if (!(events
= via2
[gIFR
] & mask
))
464 for (i
= 0, irq_bit
= 1 ; i
< 7 ; i
++, irq_bit
<<= 1)
465 if (events
& irq_bit
) {
466 via2
[gIER
] = irq_bit
;
467 via2
[gIFR
] = irq_bit
| rbv_clear
;
468 m68k_handle_int(VIA2_SOURCE_BASE
+ i
);
469 via2
[gIER
] = irq_bit
| 0x80;
475 * Dispatch Nubus interrupts. We are called as a secondary dispatch by the
476 * VIA2 dispatcher as a fast interrupt handler.
479 irqreturn_t
via_nubus_irq(int irq
, void *dev_id
)
482 unsigned char events
;
484 if (!(events
= ~via2
[gBufA
] & nubus_active
))
487 for (i
= 0, irq_bit
= 1 ; i
< 7 ; i
++, irq_bit
<<= 1) {
488 if (events
& irq_bit
) {
489 via_irq_disable(NUBUS_SOURCE_BASE
+ i
);
490 m68k_handle_int(NUBUS_SOURCE_BASE
+ i
);
491 via_irq_enable(NUBUS_SOURCE_BASE
+ i
);
497 void via_irq_enable(int irq
) {
498 int irq_src
= IRQ_SRC(irq
);
499 int irq_idx
= IRQ_IDX(irq
);
500 int irq_bit
= 1 << irq_idx
;
503 printk(KERN_DEBUG
"via_irq_enable(%d)\n", irq
);
507 via1
[vIER
] = irq_bit
| 0x80;
508 } else if (irq_src
== 2) {
510 * Set vPCR for SCSI interrupts (but not on RBV)
512 if ((irq_idx
== 0) && !rbv_present
) {
513 if (macintosh_config
->scsi_type
== MAC_SCSI_OLD
) {
514 /* CB2 (IRQ) indep. input, positive edge */
515 /* CA2 (DRQ) indep. input, positive edge */
518 /* CB2 (IRQ) indep. input, negative edge */
519 /* CA2 (DRQ) indep. input, negative edge */
523 via2
[gIER
] = irq_bit
| 0x80;
524 } else if (irq_src
== 7) {
525 nubus_active
|= irq_bit
;
527 /* enable the slot interrupt. SIER works like IER. */
528 via2
[rSIER
] = IER_SET_BIT(irq_idx
);
530 /* Make sure the bit is an input, to enable the irq */
531 /* But not on PowerBooks, that's ADB... */
532 if ((macintosh_config
->adb_type
!= MAC_ADB_PB1
) &&
533 (macintosh_config
->adb_type
!= MAC_ADB_PB2
)) {
534 switch(macintosh_config
->ident
)
540 via2
[vDirA
] &= (~irq_bit
| 0xc0);
543 via2
[vDirA
] &= ~irq_bit
;
550 void via_irq_disable(int irq
) {
551 int irq_src
= IRQ_SRC(irq
);
552 int irq_idx
= IRQ_IDX(irq
);
553 int irq_bit
= 1 << irq_idx
;
556 printk(KERN_DEBUG
"via_irq_disable(%d)\n", irq
);
560 via1
[vIER
] = irq_bit
;
561 } else if (irq_src
== 2) {
562 via2
[gIER
] = irq_bit
;
563 } else if (irq_src
== 7) {
565 /* disable the slot interrupt. SIER works like IER. */
566 via2
[rSIER
] = IER_CLR_BIT(irq_idx
);
568 /* disable the nubus irq by changing dir to output */
570 if ((macintosh_config
->adb_type
!= MAC_ADB_PB1
) &&
571 (macintosh_config
->adb_type
!= MAC_ADB_PB2
)) {
572 via2
[vDirA
] |= irq_bit
;
575 nubus_active
&= ~irq_bit
;
579 void via_irq_clear(int irq
) {
580 int irq_src
= IRQ_SRC(irq
);
581 int irq_idx
= IRQ_IDX(irq
);
582 int irq_bit
= 1 << irq_idx
;
585 via1
[vIFR
] = irq_bit
;
586 } else if (irq_src
== 2) {
587 via2
[gIFR
] = irq_bit
| rbv_clear
;
588 } else if (irq_src
== 7) {
594 * Returns nonzero if an interrupt is pending on the given
595 * VIA/IRQ combination.
598 int via_irq_pending(int irq
)
600 int irq_src
= IRQ_SRC(irq
);
601 int irq_idx
= IRQ_IDX(irq
);
602 int irq_bit
= 1 << irq_idx
;
605 return via1
[vIFR
] & irq_bit
;
606 } else if (irq_src
== 2) {
607 return via2
[gIFR
] & irq_bit
;
608 } else if (irq_src
== 7) {
609 return ~via2
[gBufA
] & irq_bit
;