via.c 15 KB

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