patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / m68k / mac / via.c
1 /*
2  *      6522 Versatile Interface Adapter (VIA)
3  *
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.
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  *
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)
18  *
19  */
20
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/ide.h>
27
28 #include <asm/traps.h>
29 #include <asm/bootinfo.h>
30 #include <asm/macintosh.h>
31 #include <asm/macints.h>
32 #include <asm/machw.h>
33 #include <asm/mac_via.h>
34 #include <asm/mac_psc.h>
35
36 volatile __u8 *via1, *via2;
37 #if 0
38 /* See note in mac_via.h about how this is possibly not useful */
39 volatile long *via_memory_bogon=(long *)&via_memory_bogon;
40 #endif
41 int  rbv_present,via_alt_mapping;
42 __u8 rbv_clear;
43
44 /*
45  * Globals for accessing the VIA chip registers without having to
46  * check if we're hitting a real VIA or an RBV. Normally you could
47  * just hit the combined register (ie, vIER|rIER) but that seems to
48  * break on AV Macs...probably because they actually decode more than
49  * eight address bits. Why can't Apple engineers at least be
50  * _consistently_ lazy?                          - 1999-05-21 (jmt)
51  */
52
53 static int gIER,gIFR,gBufA,gBufB;
54
55 /*
56  * Timer defs.
57  */
58
59 #define TICK_SIZE               10000
60 #define MAC_CLOCK_TICK          (783300/HZ)             /* ticks per HZ */
61 #define MAC_CLOCK_LOW           (MAC_CLOCK_TICK&0xFF)
62 #define MAC_CLOCK_HIGH          (MAC_CLOCK_TICK>>8)
63
64 static int  nubus_active;
65
66 void via_debug_dump(void);
67 irqreturn_t via1_irq(int, void *, struct pt_regs *);
68 irqreturn_t via2_irq(int, void *, struct pt_regs *);
69 irqreturn_t via_nubus_irq(int, void *, struct pt_regs *);
70 void via_irq_enable(int irq);
71 void via_irq_disable(int irq);
72 void via_irq_clear(int irq);
73
74 extern irqreturn_t mac_bang(int, void *, struct pt_regs *);
75 extern irqreturn_t mac_scc_dispatch(int, void *, struct pt_regs *);
76 extern int oss_present;
77
78 /*
79  * Initialize the VIAs
80  *
81  * First we figure out where they actually _are_ as well as what type of
82  * VIA we have for VIA2 (it could be a real VIA or an RBV or even an OSS.)
83  * Then we pretty much clear them out and disable all IRQ sources.
84  *
85  * Note: the OSS is actually "detected" here and not in oss_init(). It just
86  *       seems more logical to do it here since via_init() needs to know
87  *       these things anyways.
88  */
89
90 void __init via_init(void)
91 {
92         switch(macintosh_config->via_type) {
93
94                 /* IIci, IIsi, IIvx, IIvi (P6xx), LC series */
95
96                 case MAC_VIA_IIci:
97                         via1 = (void *) VIA1_BASE;
98                         if (macintosh_config->ident == MAC_MODEL_IIFX) {
99                                 via2 = NULL;
100                                 rbv_present = 0;
101                                 oss_present = 1;
102                         } else {
103                                 via2 = (void *) RBV_BASE;
104                                 rbv_present = 1;
105                                 oss_present = 0;
106                         }
107                         if (macintosh_config->ident == MAC_MODEL_LCIII) {
108                                 rbv_clear = 0x00;
109                         } else {
110                                 /* on most RBVs (& unlike the VIAs), you   */
111                                 /* need to set bit 7 when you write to IFR */
112                                 /* in order for your clear to occur.       */
113                                 rbv_clear = 0x80;
114                         }
115                         gIER = rIER;
116                         gIFR = rIFR;
117                         gBufA = rSIFR;
118                         gBufB = rBufB;
119                         break;
120
121                 /* Quadra and early MacIIs agree on the VIA locations */
122
123                 case MAC_VIA_QUADRA:
124                 case MAC_VIA_II:
125                         via1 = (void *) VIA1_BASE;
126                         via2 = (void *) VIA2_BASE;
127                         rbv_present = 0;
128                         oss_present = 0;
129                         rbv_clear = 0x00;
130                         gIER = vIER;
131                         gIFR = vIFR;
132                         gBufA = vBufA;
133                         gBufB = vBufB;
134                         break;
135                 default:
136                         panic("UNKNOWN VIA TYPE");
137         }
138
139         printk(KERN_INFO "VIA1 at %p is a 6522 or clone\n", via1);
140
141         printk(KERN_INFO "VIA2 at %p is ", via2);
142         if (rbv_present) {
143                 printk(KERN_INFO "an RBV\n");
144         } else if (oss_present) {
145                 printk(KERN_INFO "an OSS\n");
146         } else {
147                 printk(KERN_INFO "a 6522 or clone\n");
148         }
149
150 #ifdef DEBUG_VIA
151         via_debug_dump();
152 #endif
153
154         /*
155          * Shut down all IRQ sources, reset the timers, and
156          * kill the timer latch on VIA1.
157          */
158
159         via1[vIER] = 0x7F;
160         via1[vIFR] = 0x7F;
161         via1[vT1LL] = 0;
162         via1[vT1LH] = 0;
163         via1[vT1CL] = 0;
164         via1[vT1CH] = 0;
165         via1[vT2CL] = 0;
166         via1[vT2CH] = 0;
167         via1[vACR] &= 0x3F;
168
169         /*
170          * SE/30: disable video IRQ
171          * XXX: testing for SE/30 VBL
172          */
173
174         if (macintosh_config->ident == MAC_MODEL_SE30) {
175                 via1[vDirB] |= 0x40;
176                 via1[vBufB] |= 0x40;
177         }
178
179         /*
180          * Set the RTC bits to a known state: all lines to outputs and
181          * RTC disabled (yes that's 0 to enable and 1 to disable).
182          */
183
184         via1[vDirB] |= (VIA1B_vRTCEnb | VIA1B_vRTCClk | VIA1B_vRTCData);
185         via1[vBufB] |= (VIA1B_vRTCEnb | VIA1B_vRTCClk);
186
187         /* Everything below this point is VIA2/RBV only... */
188
189         if (oss_present) return;
190
191 #if 1
192         /* Some machines support an alternate IRQ mapping that spreads  */
193         /* Ethernet and Sound out to their own autolevel IRQs and moves */
194         /* VIA1 to level 6. A/UX uses this mapping and we do too.  Note */
195         /* that the IIfx emulates this alternate mapping using the OSS. */
196
197         switch(macintosh_config->ident) {
198                 case MAC_MODEL_C610:
199                 case MAC_MODEL_Q610:
200                 case MAC_MODEL_C650:
201                 case MAC_MODEL_Q650:
202                 case MAC_MODEL_Q700:
203                 case MAC_MODEL_Q800:
204                 case MAC_MODEL_Q900:
205                 case MAC_MODEL_Q950:
206                         via_alt_mapping = 1;
207                         via1[vDirB] |= 0x40;
208                         via1[vBufB] &= ~0x40;
209                         break;
210                 default:
211                         via_alt_mapping = 0;
212                         break;
213         }
214 #else
215         /* The alernate IRQ mapping seems to just not work. Anyone with a   */
216         /* supported machine is welcome to take a stab at fixing it. It     */
217         /* _should_ work on the following Quadras: 610,650,700,800,900,950  */
218         /*                                               - 1999-06-12 (jmt) */
219
220         via_alt_mapping = 0;
221 #endif
222
223         /*
224          * Now initialize VIA2. For RBV we just kill all interrupts;
225          * for a regular VIA we also reset the timers and stuff.
226          */
227
228         via2[gIER] = 0x7F;
229         via2[gIFR] = 0x7F | rbv_clear;
230         if (!rbv_present) {
231                 via2[vT1LL] = 0;
232                 via2[vT1LH] = 0;
233                 via2[vT1CL] = 0;
234                 via2[vT1CH] = 0;
235                 via2[vT2CL] = 0;
236                 via2[vT2CH] = 0;
237                 via2[vACR] &= 0x3F;
238         }
239 }
240
241 /*
242  * Start the 100 Hz clock
243  */
244
245 void __init via_init_clock(irqreturn_t (*func)(int, void *, struct pt_regs *))
246 {
247         via1[vACR] |= 0x40;
248         via1[vT1LL] = MAC_CLOCK_LOW;
249         via1[vT1LH] = MAC_CLOCK_HIGH;
250         via1[vT1CL] = MAC_CLOCK_LOW;
251         via1[vT1CH] = MAC_CLOCK_HIGH;
252
253         request_irq(IRQ_MAC_TIMER_1, func, IRQ_FLG_LOCK, "timer", func);
254 }
255
256 /*
257  * Register the interrupt dispatchers for VIA or RBV machines only.
258  */
259
260 void __init via_register_interrupts(void)
261 {
262         if (via_alt_mapping) {
263                 cpu_request_irq(IRQ_AUTO_1, via1_irq,
264                                 IRQ_FLG_LOCK|IRQ_FLG_FAST, "software",
265                                 (void *) via1);
266                 cpu_request_irq(IRQ_AUTO_6, via1_irq,
267                                 IRQ_FLG_LOCK|IRQ_FLG_FAST, "via1",
268                                 (void *) via1);
269         } else {
270                 cpu_request_irq(IRQ_AUTO_1, via1_irq,
271                                 IRQ_FLG_LOCK|IRQ_FLG_FAST, "via1",
272                                 (void *) via1);
273 #if 0 /* interferes with serial on some machines */
274                 if (!psc_present) {
275                         cpu_request_irq(IRQ_AUTO_6, mac_bang, IRQ_FLG_LOCK,
276                                         "Off Switch", mac_bang);
277                 }
278 #endif
279         }
280         cpu_request_irq(IRQ_AUTO_2, via2_irq, IRQ_FLG_LOCK|IRQ_FLG_FAST,
281                         "via2", (void *) via2);
282         if (!psc_present) {
283                 cpu_request_irq(IRQ_AUTO_4, mac_scc_dispatch, IRQ_FLG_LOCK,
284                                 "scc", mac_scc_dispatch);
285         }
286         request_irq(IRQ_MAC_NUBUS, via_nubus_irq, IRQ_FLG_LOCK|IRQ_FLG_FAST,
287                         "nubus", (void *) via2);
288 }
289
290 /*
291  * Debugging dump, used in various places to see what's going on.
292  */
293
294 void via_debug_dump(void)
295 {
296         printk(KERN_DEBUG "VIA1: DDRA = 0x%02X DDRB = 0x%02X ACR = 0x%02X\n",
297                 (uint) via1[vDirA], (uint) via1[vDirB], (uint) via1[vACR]);
298         printk(KERN_DEBUG "         PCR = 0x%02X  IFR = 0x%02X IER = 0x%02X\n",
299                 (uint) via1[vPCR], (uint) via1[vIFR], (uint) via1[vIER]);
300         if (oss_present) {
301                 printk(KERN_DEBUG "VIA2: <OSS>\n");
302         } else if (rbv_present) {
303                 printk(KERN_DEBUG "VIA2:  IFR = 0x%02X  IER = 0x%02X\n",
304                         (uint) via2[rIFR], (uint) via2[rIER]);
305                 printk(KERN_DEBUG "      SIFR = 0x%02X SIER = 0x%02X\n",
306                         (uint) via2[rSIFR], (uint) via2[rSIER]);
307         } else {
308                 printk(KERN_DEBUG "VIA2: DDRA = 0x%02X DDRB = 0x%02X ACR = 0x%02X\n",
309                         (uint) via2[vDirA], (uint) via2[vDirB],
310                         (uint) via2[vACR]);
311                 printk(KERN_DEBUG "         PCR = 0x%02X  IFR = 0x%02X IER = 0x%02X\n",
312                         (uint) via2[vPCR],
313                         (uint) via2[vIFR], (uint) via2[vIER]);
314         }
315 }
316
317 /*
318  * This is always executed with interrupts disabled.
319  *
320  * TBI: get time offset between scheduling timer ticks
321  */
322
323 unsigned long mac_gettimeoffset (void)
324 {
325         unsigned long ticks, offset = 0;
326
327         /* read VIA1 timer 2 current value */
328         ticks = via1[vT1CL] | (via1[vT1CH] << 8);
329         /* The probability of underflow is less than 2% */
330         if (ticks > MAC_CLOCK_TICK - MAC_CLOCK_TICK / 50)
331                 /* Check for pending timer interrupt in VIA1 IFR */
332                 if (via1[vIFR] & 0x40) offset = TICK_SIZE;
333
334         ticks = MAC_CLOCK_TICK - ticks;
335         ticks = ticks * 10000L / MAC_CLOCK_TICK;
336
337         return ticks + offset;
338 }
339
340 /*
341  * Flush the L2 cache on Macs that have it by flipping
342  * the system into 24-bit mode for an instant.
343  */
344
345 void via_flush_cache(void)
346 {
347         via2[gBufB] &= ~VIA2B_vMode32;
348         via2[gBufB] |= VIA2B_vMode32;
349 }
350
351 /*
352  * Return the status of the L2 cache on a IIci
353  */
354
355 int via_get_cache_disable(void)
356 {
357         /* Safeguard against being called accidentally */
358         if (!via2) {
359                 printk(KERN_ERR "via_get_cache_disable called on a non-VIA machine!\n");
360                 return 1;
361         }
362
363         return (int) via2[gBufB] & VIA2B_vCDis;
364 }
365
366 /*
367  * Initialize VIA2 for Nubus access
368  */
369
370 void __init via_nubus_init(void)
371 {
372         /* don't set nubus_active = 0 here, it kills the Baboon */
373         /* interrupt that we've already registered.             */
374
375         /* unlock nubus transactions */
376
377         if (!rbv_present) {
378                 /* set the line to be an output on non-RBV machines */
379                 if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
380                    (macintosh_config->adb_type != MAC_ADB_PB2)) {
381                         via2[vDirB] |= 0x02;
382                 }
383         }
384
385         /* this seems to be an ADB bit on PMU machines */
386         /* according to MkLinux.  -- jmt               */
387
388         if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
389             (macintosh_config->adb_type != MAC_ADB_PB2)) {
390                 via2[gBufB] |= 0x02;
391         }
392
393         /* disable nubus slot interrupts. */
394         if (rbv_present) {
395                 via2[rSIER] = 0x7F;
396                 via2[rSIER] = nubus_active | 0x80;
397         } else {
398                 /* These are ADB bits on PMU */
399                 if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
400                    (macintosh_config->adb_type != MAC_ADB_PB2)) {
401                         switch(macintosh_config->ident)
402                         {
403                                 case MAC_MODEL_II:
404                                 case MAC_MODEL_IIX:
405                                 case MAC_MODEL_IICX:
406                                 case MAC_MODEL_SE30:
407                                         via2[vBufA] |= 0x3F;
408                                         via2[vDirA] = ~nubus_active | 0xc0;
409                                         break;
410                                 default:
411                                         via2[vBufA] = 0xFF;
412                                         via2[vDirA] = ~nubus_active;
413                         }
414                 }
415         }
416 }
417
418 /*
419  * The generic VIA interrupt routines (shamelessly stolen from Alan Cox's
420  * via6522.c :-), disable/pending masks added.
421  *
422  * The new interrupt architecture in macints.c takes care of a lot of the
423  * gruntwork for us, including tallying the interrupts and calling the
424  * handlers on the linked list. All we need to do here is basically generate
425  * the machspec interrupt number after clearing the interrupt.
426  */
427
428 irqreturn_t via1_irq(int irq, void *dev_id, struct pt_regs *regs)
429 {
430         int irq_bit, i;
431         unsigned char events, mask;
432
433         mask = via1[vIER] & 0x7F;
434         if (!(events = via1[vIFR] & mask))
435                 return IRQ_NONE;
436
437         for (i = 0, irq_bit = 1 ; i < 7 ; i++, irq_bit <<= 1)
438                 if (events & irq_bit) {
439                         via1[vIER] = irq_bit;
440                         mac_do_irq_list(VIA1_SOURCE_BASE + i, regs);
441                         via1[vIFR] = irq_bit;
442                         via1[vIER] = irq_bit | 0x80;
443                 }
444
445 #if 0 /* freakin' pmu is doing weird stuff */
446         if (!oss_present) {
447                 /* This (still) seems to be necessary to get IDE
448                    working.  However, if you enable VBL interrupts,
449                    you're screwed... */
450                 /* FIXME: should we check the SLOTIRQ bit before
451                    pulling this stunt? */
452                 /* No, it won't be set. that's why we're doing this. */
453                 via_irq_disable(IRQ_MAC_NUBUS);
454                 via_irq_clear(IRQ_MAC_NUBUS);
455                 mac_do_irq_list(IRQ_MAC_NUBUS, regs);
456                 via_irq_enable(IRQ_MAC_NUBUS);
457         }
458 #endif
459         return IRQ_HANDLED;
460 }
461
462 irqreturn_t via2_irq(int irq, void *dev_id, struct pt_regs *regs)
463 {
464         int irq_bit, i;
465         unsigned char events, mask;
466
467         mask = via2[gIER] & 0x7F;
468         if (!(events = via2[gIFR] & mask))
469                 return IRQ_NONE;
470
471         for (i = 0, irq_bit = 1 ; i < 7 ; i++, irq_bit <<= 1)
472                 if (events & irq_bit) {
473                         via2[gIER] = irq_bit;
474                         mac_do_irq_list(VIA2_SOURCE_BASE + i, regs);
475                         via2[gIFR] = irq_bit | rbv_clear;
476                         via2[gIER] = irq_bit | 0x80;
477                 }
478         return IRQ_HANDLED;
479 }
480
481 /*
482  * Dispatch Nubus interrupts. We are called as a secondary dispatch by the
483  * VIA2 dispatcher as a fast interrupt handler.
484  */
485
486 irqreturn_t via_nubus_irq(int irq, void *dev_id, struct pt_regs *regs)
487 {
488         int irq_bit, i;
489         unsigned char events;
490
491         if (!(events = ~via2[gBufA] & nubus_active))
492                 return IRQ_NONE;
493
494         for (i = 0, irq_bit = 1 ; i < 7 ; i++, irq_bit <<= 1) {
495                 if (events & irq_bit) {
496                         via_irq_disable(NUBUS_SOURCE_BASE + i);
497                         mac_do_irq_list(NUBUS_SOURCE_BASE + i, regs);
498                         via_irq_enable(NUBUS_SOURCE_BASE + i);
499                 }
500         }
501         return IRQ_HANDLED;
502 }
503
504 void via_irq_enable(int irq) {
505         int irq_src     = IRQ_SRC(irq);
506         int irq_idx     = IRQ_IDX(irq);
507         int irq_bit     = 1 << irq_idx;
508
509 #ifdef DEBUG_IRQUSE
510         printk(KERN_DEBUG "via_irq_enable(%d)\n", irq);
511 #endif
512
513         if (irq_src == 1) {
514                 via1[vIER] = irq_bit | 0x80;
515         } else if (irq_src == 2) {
516                 /*
517                  * Set vPCR for SCSI interrupts (but not on RBV)
518                  */
519                 if ((irq_idx == 0) && !rbv_present) {
520                         if (macintosh_config->scsi_type == MAC_SCSI_OLD) {
521                                 /* CB2 (IRQ) indep. input, positive edge */
522                                 /* CA2 (DRQ) indep. input, positive edge */
523                                 via2[vPCR] = 0x66;
524                         } else {
525                                 /* CB2 (IRQ) indep. input, negative edge */
526                                 /* CA2 (DRQ) indep. input, negative edge */
527                                 via2[vPCR] = 0x22;
528                         }
529                 }
530                 via2[gIER] = irq_bit | 0x80;
531         } else if (irq_src == 7) {
532                 if (rbv_present) {
533                         /* enable the slot interrupt. SIER works like IER. */
534                         via2[rSIER] = IER_SET_BIT(irq_idx);
535                 } else {
536                         /* Make sure the bit is an input, to enable the irq */
537                         /* But not on PowerBooks, that's ADB... */
538                         if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
539                            (macintosh_config->adb_type != MAC_ADB_PB2)) {
540                                 switch(macintosh_config->ident)
541                                 {
542                                         case MAC_MODEL_II:
543                                         case MAC_MODEL_IIX:
544                                         case MAC_MODEL_IICX:
545                                         case MAC_MODEL_SE30:
546                                                 via2[vDirA] &= (~irq_bit | 0xc0);
547                                                 break;
548                                         default:
549                                                 via2[vDirA] &= ~irq_bit;
550                                 }
551                         }
552                 }
553                 nubus_active |= irq_bit;
554         }
555 }
556
557 void via_irq_disable(int irq) {
558         int irq_src     = IRQ_SRC(irq);
559         int irq_idx     = IRQ_IDX(irq);
560         int irq_bit     = 1 << irq_idx;
561
562 #ifdef DEBUG_IRQUSE
563         printk(KERN_DEBUG "via_irq_disable(%d)\n", irq);
564 #endif
565
566         if (irq_src == 1) {
567                 via1[vIER] = irq_bit;
568         } else if (irq_src == 2) {
569                 via2[gIER] = irq_bit;
570         } else if (irq_src == 7) {
571                 if (rbv_present) {
572                         /* disable the slot interrupt.  SIER works like IER. */
573                         via2[rSIER] = IER_CLR_BIT(irq_idx);
574                 } else {
575                         /* disable the nubus irq by changing dir to output */
576                         /* except on PMU */
577                         if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
578                            (macintosh_config->adb_type != MAC_ADB_PB2)) {
579                                 via2[vDirA] |= irq_bit;
580                         }
581                 }
582                 nubus_active &= ~irq_bit;
583         }
584 }
585
586 void via_irq_clear(int irq) {
587         int irq_src     = IRQ_SRC(irq);
588         int irq_idx     = IRQ_IDX(irq);
589         int irq_bit     = 1 << irq_idx;
590
591         if (irq_src == 1) {
592                 via1[vIFR] = irq_bit;
593         } else if (irq_src == 2) {
594                 via2[gIFR] = irq_bit | rbv_clear;
595         } else if (irq_src == 7) {
596                 /* FIXME: hmm.. */
597         }
598 }
599
600 /*
601  * Returns nonzero if an interrupt is pending on the given
602  * VIA/IRQ combination.
603  */
604
605 int via_irq_pending(int irq)
606 {
607         int irq_src     = IRQ_SRC(irq);
608         int irq_idx     = IRQ_IDX(irq);
609         int irq_bit     = 1 << irq_idx;
610
611         if (irq_src == 1) {
612                 return via1[vIFR] & irq_bit;
613         } else if (irq_src == 2) {
614                 return via2[gIFR] & irq_bit;
615         } else if (irq_src == 7) {
616                 return ~via2[gBufA] & irq_bit;
617         }
618         return 0;
619 }