via.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * 6522 Versatile Interface Adapter (VIA)
  4. *
  5. * There are two of these on the Mac II. Some IRQs are vectored
  6. * via them as are assorted bits and bobs - eg RTC, ADB.
  7. *
  8. * CSA: Motorola seems to have removed documentation on the 6522 from
  9. * their web site; try
  10. * http://nerini.drf.com/vectrex/other/text/chips/6522/
  11. * http://www.zymurgy.net/classic/vic20/vicdet1.htm
  12. * and
  13. * http://193.23.168.87/mikro_laborversuche/via_iobaustein/via6522_1.html
  14. * for info. A full-text web search on 6522 AND VIA will probably also
  15. * net some usefulness. <cananian@alumni.princeton.edu> 20apr1999
  16. *
  17. * Additional data is here (the SY6522 was used in the Mac II etc):
  18. * http://www.6502.org/documents/datasheets/synertek/synertek_sy6522.pdf
  19. * http://www.6502.org/documents/datasheets/synertek/synertek_sy6522_programming_reference.pdf
  20. *
  21. * PRAM/RTC access algorithms are from the NetBSD RTC toolkit version 1.08b
  22. * by Erik Vogan and adapted to Linux by Joshua M. Thompson (funaho@jurai.org)
  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/module.h>
  31. #include <linux/irq.h>
  32. #include <asm/macintosh.h>
  33. #include <asm/macints.h>
  34. #include <asm/mac_via.h>
  35. #include <asm/mac_psc.h>
  36. #include <asm/mac_oss.h>
  37. volatile __u8 *via1, *via2;
  38. int rbv_present;
  39. int via_alt_mapping;
  40. EXPORT_SYMBOL(via_alt_mapping);
  41. static __u8 rbv_clear;
  42. /*
  43. * Globals for accessing the VIA chip registers without having to
  44. * check if we're hitting a real VIA or an RBV. Normally you could
  45. * just hit the combined register (ie, vIER|rIER) but that seems to
  46. * break on AV Macs...probably because they actually decode more than
  47. * eight address bits. Why can't Apple engineers at least be
  48. * _consistently_ lazy? - 1999-05-21 (jmt)
  49. */
  50. static int gIER,gIFR,gBufA,gBufB;
  51. /*
  52. * Timer defs.
  53. */
  54. #define TICK_SIZE 10000
  55. #define MAC_CLOCK_TICK (783300/HZ) /* ticks per HZ */
  56. #define MAC_CLOCK_LOW (MAC_CLOCK_TICK&0xFF)
  57. #define MAC_CLOCK_HIGH (MAC_CLOCK_TICK>>8)
  58. /*
  59. * On Macs with a genuine VIA chip there is no way to mask an individual slot
  60. * interrupt. This limitation also seems to apply to VIA clone logic cores in
  61. * Quadra-like ASICs. (RBV and OSS machines don't have this limitation.)
  62. *
  63. * We used to fake it by configuring the relevant VIA pin as an output
  64. * (to mask the interrupt) or input (to unmask). That scheme did not work on
  65. * (at least) the Quadra 700. A NuBus card's /NMRQ signal is an open-collector
  66. * circuit (see Designing Cards and Drivers for Macintosh II and Macintosh SE,
  67. * p. 10-11 etc) but VIA outputs are not (see datasheet).
  68. *
  69. * Driving these outputs high must cause the VIA to source current and the
  70. * card to sink current when it asserts /NMRQ. Current will flow but the pin
  71. * voltage is uncertain and so the /NMRQ condition may still cause a transition
  72. * at the VIA2 CA1 input (which explains the lost interrupts). A side effect
  73. * is that a disabled slot IRQ can never be tested as pending or not.
  74. *
  75. * Driving these outputs low doesn't work either. All the slot /NMRQ lines are
  76. * (active low) OR'd together to generate the CA1 (aka "SLOTS") interrupt (see
  77. * The Guide To Macintosh Family Hardware, 2nd edition p. 167). If we drive a
  78. * disabled /NMRQ line low, the falling edge immediately triggers a CA1
  79. * interrupt and all slot interrupts after that will generate no transition
  80. * and therefore no interrupt, even after being re-enabled.
  81. *
  82. * So we make the VIA port A I/O lines inputs and use nubus_disabled to keep
  83. * track of their states. When any slot IRQ becomes disabled we mask the CA1
  84. * umbrella interrupt. Only when all slot IRQs become enabled do we unmask
  85. * the CA1 interrupt. It must remain enabled even when cards have no interrupt
  86. * handler registered. Drivers must therefore disable a slot interrupt at the
  87. * device before they call free_irq (like shared and autovector interrupts).
  88. *
  89. * There is also a related problem when MacOS is used to boot Linux. A network
  90. * card brought up by a MacOS driver may raise an interrupt while Linux boots.
  91. * This can be fatal since it can't be handled until the right driver loads
  92. * (if such a driver exists at all). Apparently related to this hardware
  93. * limitation, "Designing Cards and Drivers", p. 9-8, says that a slot
  94. * interrupt with no driver would crash MacOS (the book was written before
  95. * the appearance of Macs with RBV or OSS).
  96. */
  97. static u8 nubus_disabled;
  98. void via_debug_dump(void);
  99. static void via_nubus_init(void);
  100. /*
  101. * Initialize the VIAs
  102. *
  103. * First we figure out where they actually _are_ as well as what type of
  104. * VIA we have for VIA2 (it could be a real VIA or an RBV or even an OSS.)
  105. * Then we pretty much clear them out and disable all IRQ sources.
  106. */
  107. void __init via_init(void)
  108. {
  109. via1 = (void *)VIA1_BASE;
  110. pr_debug("VIA1 detected at %p\n", via1);
  111. if (oss_present) {
  112. via2 = NULL;
  113. rbv_present = 0;
  114. } else {
  115. switch (macintosh_config->via_type) {
  116. /* IIci, IIsi, IIvx, IIvi (P6xx), LC series */
  117. case MAC_VIA_IICI:
  118. via2 = (void *)RBV_BASE;
  119. pr_debug("VIA2 (RBV) detected at %p\n", via2);
  120. rbv_present = 1;
  121. if (macintosh_config->ident == MAC_MODEL_LCIII) {
  122. rbv_clear = 0x00;
  123. } else {
  124. /* on most RBVs (& unlike the VIAs), you */
  125. /* need to set bit 7 when you write to IFR */
  126. /* in order for your clear to occur. */
  127. rbv_clear = 0x80;
  128. }
  129. gIER = rIER;
  130. gIFR = rIFR;
  131. gBufA = rSIFR;
  132. gBufB = rBufB;
  133. break;
  134. /* Quadra and early MacIIs agree on the VIA locations */
  135. case MAC_VIA_QUADRA:
  136. case MAC_VIA_II:
  137. via2 = (void *) VIA2_BASE;
  138. pr_debug("VIA2 detected at %p\n", via2);
  139. rbv_present = 0;
  140. rbv_clear = 0x00;
  141. gIER = vIER;
  142. gIFR = vIFR;
  143. gBufA = vBufA;
  144. gBufB = vBufB;
  145. break;
  146. default:
  147. panic("UNKNOWN VIA TYPE");
  148. }
  149. }
  150. #ifdef DEBUG_VIA
  151. via_debug_dump();
  152. #endif
  153. /*
  154. * Shut down all IRQ sources, reset the timers, and
  155. * kill the timer latch on VIA1.
  156. */
  157. via1[vIER] = 0x7F;
  158. via1[vIFR] = 0x7F;
  159. via1[vT1LL] = 0;
  160. via1[vT1LH] = 0;
  161. via1[vT1CL] = 0;
  162. via1[vT1CH] = 0;
  163. via1[vT2CL] = 0;
  164. via1[vT2CH] = 0;
  165. via1[vACR] &= ~0xC0; /* setup T1 timer with no PB7 output */
  166. via1[vACR] &= ~0x03; /* disable port A & B latches */
  167. /*
  168. * SE/30: disable video IRQ
  169. * XXX: testing for SE/30 VBL
  170. */
  171. if (macintosh_config->ident == MAC_MODEL_SE30) {
  172. via1[vDirB] |= 0x40;
  173. via1[vBufB] |= 0x40;
  174. }
  175. /*
  176. * Set the RTC bits to a known state: all lines to outputs and
  177. * RTC disabled (yes that's 0 to enable and 1 to disable).
  178. */
  179. via1[vDirB] |= (VIA1B_vRTCEnb | VIA1B_vRTCClk | VIA1B_vRTCData);
  180. via1[vBufB] |= (VIA1B_vRTCEnb | VIA1B_vRTCClk);
  181. /* Everything below this point is VIA2/RBV only... */
  182. if (oss_present)
  183. return;
  184. if ((macintosh_config->via_type == MAC_VIA_QUADRA) &&
  185. (macintosh_config->adb_type != MAC_ADB_PB1) &&
  186. (macintosh_config->adb_type != MAC_ADB_PB2) &&
  187. (macintosh_config->ident != MAC_MODEL_C660) &&
  188. (macintosh_config->ident != MAC_MODEL_Q840)) {
  189. via_alt_mapping = 1;
  190. via1[vDirB] |= 0x40;
  191. via1[vBufB] &= ~0x40;
  192. } else {
  193. via_alt_mapping = 0;
  194. }
  195. /*
  196. * Now initialize VIA2. For RBV we just kill all interrupts;
  197. * for a regular VIA we also reset the timers and stuff.
  198. */
  199. via2[gIER] = 0x7F;
  200. via2[gIFR] = 0x7F | rbv_clear;
  201. if (!rbv_present) {
  202. via2[vT1LL] = 0;
  203. via2[vT1LH] = 0;
  204. via2[vT1CL] = 0;
  205. via2[vT1CH] = 0;
  206. via2[vT2CL] = 0;
  207. via2[vT2CH] = 0;
  208. via2[vACR] &= ~0xC0; /* setup T1 timer with no PB7 output */
  209. via2[vACR] &= ~0x03; /* disable port A & B latches */
  210. }
  211. via_nubus_init();
  212. /* Everything below this point is VIA2 only... */
  213. if (rbv_present)
  214. return;
  215. /*
  216. * Set vPCR for control line interrupts.
  217. *
  218. * CA1 (SLOTS IRQ), CB1 (ASC IRQ): negative edge trigger.
  219. *
  220. * Macs with ESP SCSI have a negative edge triggered SCSI interrupt.
  221. * Testing reveals that PowerBooks do too. However, the SE/30
  222. * schematic diagram shows an active high NCR5380 IRQ line.
  223. */
  224. pr_debug("VIA2 vPCR is 0x%02X\n", via2[vPCR]);
  225. if (macintosh_config->via_type == MAC_VIA_II) {
  226. /* CA2 (SCSI DRQ), CB2 (SCSI IRQ): indep. input, pos. edge */
  227. via2[vPCR] = 0x66;
  228. } else {
  229. /* CA2 (SCSI DRQ), CB2 (SCSI IRQ): indep. input, neg. edge */
  230. via2[vPCR] = 0x22;
  231. }
  232. }
  233. /*
  234. * Start the 100 Hz clock
  235. */
  236. void __init via_init_clock(irq_handler_t func)
  237. {
  238. via1[vACR] |= 0x40;
  239. via1[vT1LL] = MAC_CLOCK_LOW;
  240. via1[vT1LH] = MAC_CLOCK_HIGH;
  241. via1[vT1CL] = MAC_CLOCK_LOW;
  242. via1[vT1CH] = MAC_CLOCK_HIGH;
  243. if (request_irq(IRQ_MAC_TIMER_1, func, 0, "timer", func))
  244. pr_err("Couldn't register %s interrupt\n", "timer");
  245. }
  246. /*
  247. * Debugging dump, used in various places to see what's going on.
  248. */
  249. void via_debug_dump(void)
  250. {
  251. printk(KERN_DEBUG "VIA1: DDRA = 0x%02X DDRB = 0x%02X ACR = 0x%02X\n",
  252. (uint) via1[vDirA], (uint) via1[vDirB], (uint) via1[vACR]);
  253. printk(KERN_DEBUG " PCR = 0x%02X IFR = 0x%02X IER = 0x%02X\n",
  254. (uint) via1[vPCR], (uint) via1[vIFR], (uint) via1[vIER]);
  255. if (!via2)
  256. return;
  257. if (rbv_present) {
  258. printk(KERN_DEBUG "VIA2: IFR = 0x%02X IER = 0x%02X\n",
  259. (uint) via2[rIFR], (uint) via2[rIER]);
  260. printk(KERN_DEBUG " SIFR = 0x%02X SIER = 0x%02X\n",
  261. (uint) via2[rSIFR], (uint) via2[rSIER]);
  262. } else {
  263. printk(KERN_DEBUG "VIA2: DDRA = 0x%02X DDRB = 0x%02X ACR = 0x%02X\n",
  264. (uint) via2[vDirA], (uint) via2[vDirB],
  265. (uint) via2[vACR]);
  266. printk(KERN_DEBUG " PCR = 0x%02X IFR = 0x%02X IER = 0x%02X\n",
  267. (uint) via2[vPCR],
  268. (uint) via2[vIFR], (uint) via2[vIER]);
  269. }
  270. }
  271. /*
  272. * This is always executed with interrupts disabled.
  273. *
  274. * TBI: get time offset between scheduling timer ticks
  275. */
  276. u32 mac_gettimeoffset(void)
  277. {
  278. unsigned long ticks, offset = 0;
  279. /* read VIA1 timer 2 current value */
  280. ticks = via1[vT1CL] | (via1[vT1CH] << 8);
  281. /* The probability of underflow is less than 2% */
  282. if (ticks > MAC_CLOCK_TICK - MAC_CLOCK_TICK / 50)
  283. /* Check for pending timer interrupt in VIA1 IFR */
  284. if (via1[vIFR] & 0x40) offset = TICK_SIZE;
  285. ticks = MAC_CLOCK_TICK - ticks;
  286. ticks = ticks * 10000L / MAC_CLOCK_TICK;
  287. return (ticks + offset) * 1000;
  288. }
  289. /*
  290. * Flush the L2 cache on Macs that have it by flipping
  291. * the system into 24-bit mode for an instant.
  292. */
  293. void via_flush_cache(void)
  294. {
  295. via2[gBufB] &= ~VIA2B_vMode32;
  296. via2[gBufB] |= VIA2B_vMode32;
  297. }
  298. /*
  299. * Return the status of the L2 cache on a IIci
  300. */
  301. int via_get_cache_disable(void)
  302. {
  303. /* Safeguard against being called accidentally */
  304. if (!via2) {
  305. printk(KERN_ERR "via_get_cache_disable called on a non-VIA machine!\n");
  306. return 1;
  307. }
  308. return (int) via2[gBufB] & VIA2B_vCDis;
  309. }
  310. /*
  311. * Initialize VIA2 for Nubus access
  312. */
  313. static void __init via_nubus_init(void)
  314. {
  315. /* unlock nubus transactions */
  316. if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
  317. (macintosh_config->adb_type != MAC_ADB_PB2)) {
  318. /* set the line to be an output on non-RBV machines */
  319. if (!rbv_present)
  320. via2[vDirB] |= 0x02;
  321. /* this seems to be an ADB bit on PMU machines */
  322. /* according to MkLinux. -- jmt */
  323. via2[gBufB] |= 0x02;
  324. }
  325. /*
  326. * Disable the slot interrupts. On some hardware that's not possible.
  327. * On some hardware it's unclear what all of these I/O lines do.
  328. */
  329. switch (macintosh_config->via_type) {
  330. case MAC_VIA_II:
  331. case MAC_VIA_QUADRA:
  332. pr_debug("VIA2 vDirA is 0x%02X\n", via2[vDirA]);
  333. break;
  334. case MAC_VIA_IICI:
  335. /* RBV. Disable all the slot interrupts. SIER works like IER. */
  336. via2[rSIER] = 0x7F;
  337. break;
  338. }
  339. }
  340. void via_nubus_irq_startup(int irq)
  341. {
  342. int irq_idx = IRQ_IDX(irq);
  343. switch (macintosh_config->via_type) {
  344. case MAC_VIA_II:
  345. case MAC_VIA_QUADRA:
  346. /* Make the port A line an input. Probably redundant. */
  347. if (macintosh_config->via_type == MAC_VIA_II) {
  348. /* The top two bits are RAM size outputs. */
  349. via2[vDirA] &= 0xC0 | ~(1 << irq_idx);
  350. } else {
  351. /* Allow NuBus slots 9 through F. */
  352. via2[vDirA] &= 0x80 | ~(1 << irq_idx);
  353. }
  354. /* fall through */
  355. case MAC_VIA_IICI:
  356. via_irq_enable(irq);
  357. break;
  358. }
  359. }
  360. void via_nubus_irq_shutdown(int irq)
  361. {
  362. switch (macintosh_config->via_type) {
  363. case MAC_VIA_II:
  364. case MAC_VIA_QUADRA:
  365. /* Ensure that the umbrella CA1 interrupt remains enabled. */
  366. via_irq_enable(irq);
  367. break;
  368. case MAC_VIA_IICI:
  369. via_irq_disable(irq);
  370. break;
  371. }
  372. }
  373. /*
  374. * The generic VIA interrupt routines (shamelessly stolen from Alan Cox's
  375. * via6522.c :-), disable/pending masks added.
  376. */
  377. void via1_irq(struct irq_desc *desc)
  378. {
  379. int irq_num;
  380. unsigned char irq_bit, events;
  381. events = via1[vIFR] & via1[vIER] & 0x7F;
  382. if (!events)
  383. return;
  384. irq_num = VIA1_SOURCE_BASE;
  385. irq_bit = 1;
  386. do {
  387. if (events & irq_bit) {
  388. via1[vIFR] = irq_bit;
  389. generic_handle_irq(irq_num);
  390. }
  391. ++irq_num;
  392. irq_bit <<= 1;
  393. } while (events >= irq_bit);
  394. }
  395. static void via2_irq(struct irq_desc *desc)
  396. {
  397. int irq_num;
  398. unsigned char irq_bit, events;
  399. events = via2[gIFR] & via2[gIER] & 0x7F;
  400. if (!events)
  401. return;
  402. irq_num = VIA2_SOURCE_BASE;
  403. irq_bit = 1;
  404. do {
  405. if (events & irq_bit) {
  406. via2[gIFR] = irq_bit | rbv_clear;
  407. generic_handle_irq(irq_num);
  408. }
  409. ++irq_num;
  410. irq_bit <<= 1;
  411. } while (events >= irq_bit);
  412. }
  413. /*
  414. * Dispatch Nubus interrupts. We are called as a secondary dispatch by the
  415. * VIA2 dispatcher as a fast interrupt handler.
  416. */
  417. static void via_nubus_irq(struct irq_desc *desc)
  418. {
  419. int slot_irq;
  420. unsigned char slot_bit, events;
  421. events = ~via2[gBufA] & 0x7F;
  422. if (rbv_present)
  423. events &= via2[rSIER];
  424. else
  425. events &= ~via2[vDirA];
  426. if (!events)
  427. return;
  428. do {
  429. slot_irq = IRQ_NUBUS_F;
  430. slot_bit = 0x40;
  431. do {
  432. if (events & slot_bit) {
  433. events &= ~slot_bit;
  434. generic_handle_irq(slot_irq);
  435. }
  436. --slot_irq;
  437. slot_bit >>= 1;
  438. } while (events);
  439. /* clear the CA1 interrupt and make certain there's no more. */
  440. via2[gIFR] = 0x02 | rbv_clear;
  441. events = ~via2[gBufA] & 0x7F;
  442. if (rbv_present)
  443. events &= via2[rSIER];
  444. else
  445. events &= ~via2[vDirA];
  446. } while (events);
  447. }
  448. /*
  449. * Register the interrupt dispatchers for VIA or RBV machines only.
  450. */
  451. void __init via_register_interrupts(void)
  452. {
  453. if (via_alt_mapping) {
  454. /* software interrupt */
  455. irq_set_chained_handler(IRQ_AUTO_1, via1_irq);
  456. /* via1 interrupt */
  457. irq_set_chained_handler(IRQ_AUTO_6, via1_irq);
  458. } else {
  459. irq_set_chained_handler(IRQ_AUTO_1, via1_irq);
  460. }
  461. irq_set_chained_handler(IRQ_AUTO_2, via2_irq);
  462. irq_set_chained_handler(IRQ_MAC_NUBUS, via_nubus_irq);
  463. }
  464. void via_irq_enable(int irq) {
  465. int irq_src = IRQ_SRC(irq);
  466. int irq_idx = IRQ_IDX(irq);
  467. if (irq_src == 1) {
  468. via1[vIER] = IER_SET_BIT(irq_idx);
  469. } else if (irq_src == 2) {
  470. if (irq != IRQ_MAC_NUBUS || nubus_disabled == 0)
  471. via2[gIER] = IER_SET_BIT(irq_idx);
  472. } else if (irq_src == 7) {
  473. switch (macintosh_config->via_type) {
  474. case MAC_VIA_II:
  475. case MAC_VIA_QUADRA:
  476. nubus_disabled &= ~(1 << irq_idx);
  477. /* Enable the CA1 interrupt when no slot is disabled. */
  478. if (!nubus_disabled)
  479. via2[gIER] = IER_SET_BIT(1);
  480. break;
  481. case MAC_VIA_IICI:
  482. /* On RBV, enable the slot interrupt.
  483. * SIER works like IER.
  484. */
  485. via2[rSIER] = IER_SET_BIT(irq_idx);
  486. break;
  487. }
  488. }
  489. }
  490. void via_irq_disable(int irq) {
  491. int irq_src = IRQ_SRC(irq);
  492. int irq_idx = IRQ_IDX(irq);
  493. if (irq_src == 1) {
  494. via1[vIER] = IER_CLR_BIT(irq_idx);
  495. } else if (irq_src == 2) {
  496. via2[gIER] = IER_CLR_BIT(irq_idx);
  497. } else if (irq_src == 7) {
  498. switch (macintosh_config->via_type) {
  499. case MAC_VIA_II:
  500. case MAC_VIA_QUADRA:
  501. nubus_disabled |= 1 << irq_idx;
  502. if (nubus_disabled)
  503. via2[gIER] = IER_CLR_BIT(1);
  504. break;
  505. case MAC_VIA_IICI:
  506. via2[rSIER] = IER_CLR_BIT(irq_idx);
  507. break;
  508. }
  509. }
  510. }
  511. void via1_set_head(int head)
  512. {
  513. if (head == 0)
  514. via1[vBufA] &= ~VIA1A_vHeadSel;
  515. else
  516. via1[vBufA] |= VIA1A_vHeadSel;
  517. }
  518. EXPORT_SYMBOL(via1_set_head);
  519. int via2_scsi_drq_pending(void)
  520. {
  521. return via2[gIFR] & (1 << IRQ_IDX(IRQ_MAC_SCSIDRQ));
  522. }
  523. EXPORT_SYMBOL(via2_scsi_drq_pending);