m68k: Mac IRQ prep
[platform/kernel/linux-starfive.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  * 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  *
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)
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>
30 #include <linux/ide.h>
31
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>
38
39 volatile __u8 *via1, *via2;
40 #if 0
41 /* See note in mac_via.h about how this is possibly not useful */
42 volatile long *via_memory_bogon=(long *)&via_memory_bogon;
43 #endif
44 int rbv_present, via_alt_mapping;
45 __u8 rbv_clear;
46
47 /*
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)
54  */
55
56 static int gIER,gIFR,gBufA,gBufB;
57
58 /*
59  * Timer defs.
60  */
61
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)
66
67 static int  nubus_active;
68
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);
76
77 extern irqreturn_t mac_scc_dispatch(int, void *);
78 extern int oss_present;
79
80 /*
81  * Initialize the VIAs
82  *
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.
86  *
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.
90  */
91
92 void __init via_init(void)
93 {
94         switch(macintosh_config->via_type) {
95
96                 /* IIci, IIsi, IIvx, IIvi (P6xx), LC series */
97
98                 case MAC_VIA_IIci:
99                         via1 = (void *) VIA1_BASE;
100                         if (macintosh_config->ident == MAC_MODEL_IIFX) {
101                                 via2 = NULL;
102                                 rbv_present = 0;
103                                 oss_present = 1;
104                         } else {
105                                 via2 = (void *) RBV_BASE;
106                                 rbv_present = 1;
107                                 oss_present = 0;
108                         }
109                         if (macintosh_config->ident == MAC_MODEL_LCIII) {
110                                 rbv_clear = 0x00;
111                         } else {
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.       */
115                                 rbv_clear = 0x80;
116                         }
117                         gIER = rIER;
118                         gIFR = rIFR;
119                         gBufA = rSIFR;
120                         gBufB = rBufB;
121                         break;
122
123                 /* Quadra and early MacIIs agree on the VIA locations */
124
125                 case MAC_VIA_QUADRA:
126                 case MAC_VIA_II:
127                         via1 = (void *) VIA1_BASE;
128                         via2 = (void *) VIA2_BASE;
129                         rbv_present = 0;
130                         oss_present = 0;
131                         rbv_clear = 0x00;
132                         gIER = vIER;
133                         gIFR = vIFR;
134                         gBufA = vBufA;
135                         gBufB = vBufB;
136                         break;
137                 default:
138                         panic("UNKNOWN VIA TYPE");
139         }
140
141         printk(KERN_INFO "VIA1 at %p is a 6522 or clone\n", via1);
142
143         printk(KERN_INFO "VIA2 at %p is ", via2);
144         if (rbv_present) {
145                 printk("an RBV\n");
146         } else if (oss_present) {
147                 printk("an OSS\n");
148         } else {
149                 printk("a 6522 or clone\n");
150         }
151
152 #ifdef DEBUG_VIA
153         via_debug_dump();
154 #endif
155
156         /*
157          * Shut down all IRQ sources, reset the timers, and
158          * kill the timer latch on VIA1.
159          */
160
161         via1[vIER] = 0x7F;
162         via1[vIFR] = 0x7F;
163         via1[vT1LL] = 0;
164         via1[vT1LH] = 0;
165         via1[vT1CL] = 0;
166         via1[vT1CH] = 0;
167         via1[vT2CL] = 0;
168         via1[vT2CH] = 0;
169         via1[vACR] &= 0x3F;
170         via1[vACR] &= ~0x03; /* disable port A & B latches */
171
172         /*
173          * SE/30: disable video IRQ
174          * XXX: testing for SE/30 VBL
175          */
176
177         if (macintosh_config->ident == MAC_MODEL_SE30) {
178                 via1[vDirB] |= 0x40;
179                 via1[vBufB] |= 0x40;
180         }
181
182         /*
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).
185          */
186
187         via1[vDirB] |= (VIA1B_vRTCEnb | VIA1B_vRTCClk | VIA1B_vRTCData);
188         via1[vBufB] |= (VIA1B_vRTCEnb | VIA1B_vRTCClk);
189
190         /* Everything below this point is VIA2/RBV only... */
191
192         if (oss_present) return;
193
194 #if 1
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. */
199
200         switch(macintosh_config->ident) {
201                 case MAC_MODEL_P475:
202                 case MAC_MODEL_P475F:
203                 case MAC_MODEL_P575:
204                 case MAC_MODEL_Q605:
205                 case MAC_MODEL_Q605_ACC:
206                 case MAC_MODEL_C610:
207                 case MAC_MODEL_Q610:
208                 case MAC_MODEL_Q630:
209                 case MAC_MODEL_C650:
210                 case MAC_MODEL_Q650:
211                 case MAC_MODEL_Q700:
212                 case MAC_MODEL_Q800:
213                 case MAC_MODEL_Q900:
214                 case MAC_MODEL_Q950:
215                         via_alt_mapping = 1;
216                         via1[vDirB] |= 0x40;
217                         via1[vBufB] &= ~0x40;
218                         break;
219                 default:
220                         via_alt_mapping = 0;
221                         break;
222         }
223 #else
224         via_alt_mapping = 0;
225 #endif
226
227         /*
228          * Now initialize VIA2. For RBV we just kill all interrupts;
229          * for a regular VIA we also reset the timers and stuff.
230          */
231
232         via2[gIER] = 0x7F;
233         via2[gIFR] = 0x7F | rbv_clear;
234         if (!rbv_present) {
235                 via2[vT1LL] = 0;
236                 via2[vT1LH] = 0;
237                 via2[vT1CL] = 0;
238                 via2[vT1CH] = 0;
239                 via2[vT2CL] = 0;
240                 via2[vT2CH] = 0;
241                 via2[vACR] &= 0x3F;
242                 via2[vACR] &= ~0x03; /* disable port A & B latches */
243         }
244
245         /*
246          * Set vPCR for SCSI interrupts (but not on RBV)
247          */
248         if (!rbv_present) {
249                 if (macintosh_config->scsi_type == MAC_SCSI_OLD) {
250                         /* CB2 (IRQ) indep. input, positive edge */
251                         /* CA2 (DRQ) indep. input, positive edge */
252                         via2[vPCR] = 0x66;
253                 } else {
254                         /* CB2 (IRQ) indep. input, negative edge */
255                         /* CA2 (DRQ) indep. input, negative edge */
256                         via2[vPCR] = 0x22;
257                 }
258         }
259 }
260
261 /*
262  * Start the 100 Hz clock
263  */
264
265 void __init via_init_clock(irq_handler_t func)
266 {
267         via1[vACR] |= 0x40;
268         via1[vT1LL] = MAC_CLOCK_LOW;
269         via1[vT1LH] = MAC_CLOCK_HIGH;
270         via1[vT1CL] = MAC_CLOCK_LOW;
271         via1[vT1CH] = MAC_CLOCK_HIGH;
272
273         request_irq(IRQ_MAC_TIMER_1, func, IRQ_FLG_LOCK, "timer", func);
274 }
275
276 /*
277  * Register the interrupt dispatchers for VIA or RBV machines only.
278  */
279
280 void __init via_register_interrupts(void)
281 {
282         if (via_alt_mapping) {
283                 request_irq(IRQ_AUTO_1, via1_irq,
284                                 IRQ_FLG_LOCK|IRQ_FLG_FAST, "software",
285                                 (void *) via1);
286                 request_irq(IRQ_AUTO_6, via1_irq,
287                                 IRQ_FLG_LOCK|IRQ_FLG_FAST, "via1",
288                                 (void *) via1);
289         } else {
290                 request_irq(IRQ_AUTO_1, via1_irq,
291                                 IRQ_FLG_LOCK|IRQ_FLG_FAST, "via1",
292                                 (void *) via1);
293         }
294         request_irq(IRQ_AUTO_2, via2_irq, IRQ_FLG_LOCK|IRQ_FLG_FAST,
295                         "via2", (void *) via2);
296         if (!psc_present) {
297                 request_irq(IRQ_AUTO_4, mac_scc_dispatch, IRQ_FLG_LOCK,
298                                 "scc", mac_scc_dispatch);
299         }
300         request_irq(IRQ_MAC_NUBUS, via_nubus_irq, IRQ_FLG_LOCK|IRQ_FLG_FAST,
301                         "nubus", (void *) via2);
302 }
303
304 /*
305  * Debugging dump, used in various places to see what's going on.
306  */
307
308 void via_debug_dump(void)
309 {
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]);
314         if (oss_present) {
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]);
321         } else {
322                 printk(KERN_DEBUG "VIA2: DDRA = 0x%02X DDRB = 0x%02X ACR = 0x%02X\n",
323                         (uint) via2[vDirA], (uint) via2[vDirB],
324                         (uint) via2[vACR]);
325                 printk(KERN_DEBUG "         PCR = 0x%02X  IFR = 0x%02X IER = 0x%02X\n",
326                         (uint) via2[vPCR],
327                         (uint) via2[vIFR], (uint) via2[vIER]);
328         }
329 }
330
331 /*
332  * This is always executed with interrupts disabled.
333  *
334  * TBI: get time offset between scheduling timer ticks
335  */
336
337 unsigned long mac_gettimeoffset (void)
338 {
339         unsigned long ticks, offset = 0;
340
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;
347
348         ticks = MAC_CLOCK_TICK - ticks;
349         ticks = ticks * 10000L / MAC_CLOCK_TICK;
350
351         return ticks + offset;
352 }
353
354 /*
355  * Flush the L2 cache on Macs that have it by flipping
356  * the system into 24-bit mode for an instant.
357  */
358
359 void via_flush_cache(void)
360 {
361         via2[gBufB] &= ~VIA2B_vMode32;
362         via2[gBufB] |= VIA2B_vMode32;
363 }
364
365 /*
366  * Return the status of the L2 cache on a IIci
367  */
368
369 int via_get_cache_disable(void)
370 {
371         /* Safeguard against being called accidentally */
372         if (!via2) {
373                 printk(KERN_ERR "via_get_cache_disable called on a non-VIA machine!\n");
374                 return 1;
375         }
376
377         return (int) via2[gBufB] & VIA2B_vCDis;
378 }
379
380 /*
381  * Initialize VIA2 for Nubus access
382  */
383
384 void __init via_nubus_init(void)
385 {
386         /* don't set nubus_active = 0 here, it kills the Baboon */
387         /* interrupt that we've already registered.             */
388
389         /* unlock nubus transactions */
390
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 */
394                 if (!rbv_present)
395                         via2[vDirB] |= 0x02;
396
397                 /* this seems to be an ADB bit on PMU machines */
398                 /* according to MkLinux.  -- jmt               */
399                 via2[gBufB] |= 0x02;
400         }
401
402         /* disable nubus slot interrupts. */
403         if (rbv_present) {
404                 via2[rSIER] = 0x7F;
405                 via2[rSIER] = nubus_active | 0x80;
406         } else {
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)
411                         {
412                                 case MAC_MODEL_II:
413                                 case MAC_MODEL_IIX:
414                                 case MAC_MODEL_IICX:
415                                 case MAC_MODEL_SE30:
416                                         via2[vBufA] |= 0x3F;
417                                         via2[vDirA] = ~nubus_active | 0xc0;
418                                         break;
419                                 default:
420                                         via2[vBufA] = 0xFF;
421                                         via2[vDirA] = ~nubus_active;
422                         }
423                 }
424         }
425 }
426
427 /*
428  * The generic VIA interrupt routines (shamelessly stolen from Alan Cox's
429  * via6522.c :-), disable/pending masks added.
430  *
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.
435  */
436
437 irqreturn_t via1_irq(int irq, void *dev_id)
438 {
439         int irq_num;
440         unsigned char irq_bit, events;
441
442         events = via1[vIFR] & via1[vIER] & 0x7F;
443         if (!events)
444                 return IRQ_NONE;
445
446         irq_num = VIA1_SOURCE_BASE;
447         irq_bit = 1;
448         do {
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;
454                 }
455                 ++irq_num;
456                 irq_bit <<= 1;
457         } while (events >= irq_bit);
458
459 #if 0 /* freakin' pmu is doing weird stuff */
460         if (!oss_present) {
461                 /* This (still) seems to be necessary to get IDE
462                    working.  However, if you enable VBL interrupts,
463                    you're screwed... */
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);
471         }
472 #endif
473         return IRQ_HANDLED;
474 }
475
476 irqreturn_t via2_irq(int irq, void *dev_id)
477 {
478         int irq_num;
479         unsigned char irq_bit, events;
480
481         events = via2[gIFR] & via2[gIER] & 0x7F;
482         if (!events)
483                 return IRQ_NONE;
484
485         irq_num = VIA2_SOURCE_BASE;
486         irq_bit = 1;
487         do {
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;
493                 }
494                 ++irq_num;
495                 irq_bit <<= 1;
496         } while (events >= irq_bit);
497         return IRQ_HANDLED;
498 }
499
500 /*
501  * Dispatch Nubus interrupts. We are called as a secondary dispatch by the
502  * VIA2 dispatcher as a fast interrupt handler.
503  */
504
505 irqreturn_t via_nubus_irq(int irq, void *dev_id)
506 {
507         int slot_irq;
508         unsigned char slot_bit, events;
509
510         events = ~via2[gBufA] & 0x7F;
511         if (rbv_present)
512                 events &= via2[rSIER];
513         else
514                 events &= nubus_active;
515         if (!events)
516                 return IRQ_NONE;
517
518         do {
519                 slot_irq = IRQ_NUBUS_F;
520                 slot_bit = 0x40;
521                 do {
522                         if (events & slot_bit) {
523                                 events &= ~slot_bit;
524                                 m68k_handle_int(slot_irq);
525                         }
526                         --slot_irq;
527                         slot_bit >>= 1;
528                 } while (events);
529
530                 /* clear the CA1 interrupt and make certain there's no more. */
531                 via2[gIFR] = 0x02 | rbv_clear;
532                 events = ~via2[gBufA] & 0x7F;
533                 if (rbv_present)
534                         events &= via2[rSIER];
535                 else
536                         events &= nubus_active;
537         } while (events);
538         return IRQ_HANDLED;
539 }
540
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;
545
546 #ifdef DEBUG_IRQUSE
547         printk(KERN_DEBUG "via_irq_enable(%d)\n", irq);
548 #endif
549
550         if (irq_src == 1) {
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;
556                 if (rbv_present) {
557                         /* enable the slot interrupt. SIER works like IER. */
558                         via2[rSIER] = IER_SET_BIT(irq_idx);
559                 } else {
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)
565                                 {
566                                         case MAC_MODEL_II:
567                                         case MAC_MODEL_IIX:
568                                         case MAC_MODEL_IICX:
569                                         case MAC_MODEL_SE30:
570                                                 via2[vDirA] &= (~irq_bit | 0xc0);
571                                                 break;
572                                         default:
573                                                 via2[vDirA] &= ~irq_bit;
574                                 }
575                         }
576                 }
577         }
578 }
579
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;
584
585 #ifdef DEBUG_IRQUSE
586         printk(KERN_DEBUG "via_irq_disable(%d)\n", irq);
587 #endif
588
589         if (irq_src == 1) {
590                 via1[vIER] = irq_bit & 0x7F;
591         } else if (irq_src == 2) {
592                 via2[gIER] = irq_bit & 0x7F;
593         } else if (irq_src == 7) {
594                 if (rbv_present) {
595                         /* disable the slot interrupt.  SIER works like IER. */
596                         via2[rSIER] = IER_CLR_BIT(irq_idx);
597                 } else {
598                         /* disable the nubus irq by changing dir to output */
599                         /* except on PMU */
600                         if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
601                            (macintosh_config->adb_type != MAC_ADB_PB2)) {
602                                 via2[vDirA] |= irq_bit;
603                         }
604                 }
605                 nubus_active &= ~irq_bit;
606         }
607 }
608
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;
613
614         if (irq_src == 1) {
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.
621                  */
622         }
623 }
624
625 /*
626  * Returns nonzero if an interrupt is pending on the given
627  * VIA/IRQ combination.
628  */
629
630 int via_irq_pending(int irq)
631 {
632         int irq_src     = IRQ_SRC(irq);
633         int irq_idx     = IRQ_IDX(irq);
634         int irq_bit     = 1 << irq_idx;
635
636         if (irq_src == 1) {
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;
643         }
644         return 0;
645 }