via.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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/clocksource.h>
  26. #include <linux/types.h>
  27. #include <linux/kernel.h>
  28. #include <linux/mm.h>
  29. #include <linux/delay.h>
  30. #include <linux/init.h>
  31. #include <linux/module.h>
  32. #include <linux/irq.h>
  33. #include <asm/macintosh.h>
  34. #include <asm/macints.h>
  35. #include <asm/mac_via.h>
  36. #include <asm/mac_psc.h>
  37. #include <asm/mac_oss.h>
  38. #include "mac.h"
  39. volatile __u8 *via1, *via2;
  40. int rbv_present;
  41. int via_alt_mapping;
  42. EXPORT_SYMBOL(via_alt_mapping);
  43. static __u8 rbv_clear;
  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. static int gIER,gIFR,gBufA,gBufB;
  53. /*
  54. * On Macs with a genuine VIA chip there is no way to mask an individual slot
  55. * interrupt. This limitation also seems to apply to VIA clone logic cores in
  56. * Quadra-like ASICs. (RBV and OSS machines don't have this limitation.)
  57. *
  58. * We used to fake it by configuring the relevant VIA pin as an output
  59. * (to mask the interrupt) or input (to unmask). That scheme did not work on
  60. * (at least) the Quadra 700. A NuBus card's /NMRQ signal is an open-collector
  61. * circuit (see Designing Cards and Drivers for Macintosh II and Macintosh SE,
  62. * p. 10-11 etc) but VIA outputs are not (see datasheet).
  63. *
  64. * Driving these outputs high must cause the VIA to source current and the
  65. * card to sink current when it asserts /NMRQ. Current will flow but the pin
  66. * voltage is uncertain and so the /NMRQ condition may still cause a transition
  67. * at the VIA2 CA1 input (which explains the lost interrupts). A side effect
  68. * is that a disabled slot IRQ can never be tested as pending or not.
  69. *
  70. * Driving these outputs low doesn't work either. All the slot /NMRQ lines are
  71. * (active low) OR'd together to generate the CA1 (aka "SLOTS") interrupt (see
  72. * The Guide To Macintosh Family Hardware, 2nd edition p. 167). If we drive a
  73. * disabled /NMRQ line low, the falling edge immediately triggers a CA1
  74. * interrupt and all slot interrupts after that will generate no transition
  75. * and therefore no interrupt, even after being re-enabled.
  76. *
  77. * So we make the VIA port A I/O lines inputs and use nubus_disabled to keep
  78. * track of their states. When any slot IRQ becomes disabled we mask the CA1
  79. * umbrella interrupt. Only when all slot IRQs become enabled do we unmask
  80. * the CA1 interrupt. It must remain enabled even when cards have no interrupt
  81. * handler registered. Drivers must therefore disable a slot interrupt at the
  82. * device before they call free_irq (like shared and autovector interrupts).
  83. *
  84. * There is also a related problem when MacOS is used to boot Linux. A network
  85. * card brought up by a MacOS driver may raise an interrupt while Linux boots.
  86. * This can be fatal since it can't be handled until the right driver loads
  87. * (if such a driver exists at all). Apparently related to this hardware
  88. * limitation, "Designing Cards and Drivers", p. 9-8, says that a slot
  89. * interrupt with no driver would crash MacOS (the book was written before
  90. * the appearance of Macs with RBV or OSS).
  91. */
  92. static u8 nubus_disabled;
  93. void via_debug_dump(void);
  94. static void via_nubus_init(void);
  95. /*
  96. * Initialize the VIAs
  97. *
  98. * First we figure out where they actually _are_ as well as what type of
  99. * VIA we have for VIA2 (it could be a real VIA or an RBV or even an OSS.)
  100. * Then we pretty much clear them out and disable all IRQ sources.
  101. */
  102. void __init via_init(void)
  103. {
  104. via1 = (void *)VIA1_BASE;
  105. pr_debug("VIA1 detected at %p\n", via1);
  106. if (oss_present) {
  107. via2 = NULL;
  108. rbv_present = 0;
  109. } else {
  110. switch (macintosh_config->via_type) {
  111. /* IIci, IIsi, IIvx, IIvi (P6xx), LC series */
  112. case MAC_VIA_IICI:
  113. via2 = (void *)RBV_BASE;
  114. pr_debug("VIA2 (RBV) detected at %p\n", via2);
  115. rbv_present = 1;
  116. if (macintosh_config->ident == MAC_MODEL_LCIII) {
  117. rbv_clear = 0x00;
  118. } else {
  119. /* on most RBVs (& unlike the VIAs), you */
  120. /* need to set bit 7 when you write to IFR */
  121. /* in order for your clear to occur. */
  122. rbv_clear = 0x80;
  123. }
  124. gIER = rIER;
  125. gIFR = rIFR;
  126. gBufA = rSIFR;
  127. gBufB = rBufB;
  128. break;
  129. /* Quadra and early MacIIs agree on the VIA locations */
  130. case MAC_VIA_QUADRA:
  131. case MAC_VIA_II:
  132. via2 = (void *) VIA2_BASE;
  133. pr_debug("VIA2 detected at %p\n", via2);
  134. rbv_present = 0;
  135. rbv_clear = 0x00;
  136. gIER = vIER;
  137. gIFR = vIFR;
  138. gBufA = vBufA;
  139. gBufB = vBufB;
  140. break;
  141. default:
  142. panic("UNKNOWN VIA TYPE");
  143. }
  144. }
  145. #ifdef DEBUG_VIA
  146. via_debug_dump();
  147. #endif
  148. /*
  149. * Shut down all IRQ sources, reset the timers, and
  150. * kill the timer latch on VIA1.
  151. */
  152. via1[vIER] = 0x7F;
  153. via1[vIFR] = 0x7F;
  154. via1[vT1CL] = 0;
  155. via1[vT1CH] = 0;
  156. via1[vT2CL] = 0;
  157. via1[vT2CH] = 0;
  158. via1[vACR] &= ~0xC0; /* setup T1 timer with no PB7 output */
  159. via1[vACR] &= ~0x03; /* disable port A & B latches */
  160. /*
  161. * SE/30: disable video IRQ
  162. */
  163. if (macintosh_config->ident == MAC_MODEL_SE30) {
  164. via1[vDirB] |= 0x40;
  165. via1[vBufB] |= 0x40;
  166. }
  167. switch (macintosh_config->adb_type) {
  168. case MAC_ADB_IOP:
  169. case MAC_ADB_II:
  170. case MAC_ADB_PB1:
  171. /*
  172. * Set the RTC bits to a known state: all lines to outputs and
  173. * RTC disabled (yes that's 0 to enable and 1 to disable).
  174. */
  175. via1[vDirB] |= VIA1B_vRTCEnb | VIA1B_vRTCClk | VIA1B_vRTCData;
  176. via1[vBufB] |= VIA1B_vRTCEnb | VIA1B_vRTCClk;
  177. break;
  178. }
  179. /* Everything below this point is VIA2/RBV only... */
  180. if (oss_present)
  181. return;
  182. if ((macintosh_config->via_type == MAC_VIA_QUADRA) &&
  183. (macintosh_config->adb_type != MAC_ADB_PB1) &&
  184. (macintosh_config->adb_type != MAC_ADB_PB2) &&
  185. (macintosh_config->ident != MAC_MODEL_C660) &&
  186. (macintosh_config->ident != MAC_MODEL_Q840)) {
  187. via_alt_mapping = 1;
  188. via1[vDirB] |= 0x40;
  189. via1[vBufB] &= ~0x40;
  190. } else {
  191. via_alt_mapping = 0;
  192. }
  193. /*
  194. * Now initialize VIA2. For RBV we just kill all interrupts;
  195. * for a regular VIA we also reset the timers and stuff.
  196. */
  197. via2[gIER] = 0x7F;
  198. via2[gIFR] = 0x7F | rbv_clear;
  199. if (!rbv_present) {
  200. via2[vT1CL] = 0;
  201. via2[vT1CH] = 0;
  202. via2[vT2CL] = 0;
  203. via2[vT2CH] = 0;
  204. via2[vACR] &= ~0xC0; /* setup T1 timer with no PB7 output */
  205. via2[vACR] &= ~0x03; /* disable port A & B latches */
  206. }
  207. via_nubus_init();
  208. /* Everything below this point is VIA2 only... */
  209. if (rbv_present)
  210. return;
  211. /*
  212. * Set vPCR for control line interrupts.
  213. *
  214. * CA1 (SLOTS IRQ), CB1 (ASC IRQ): negative edge trigger.
  215. *
  216. * Macs with ESP SCSI have a negative edge triggered SCSI interrupt.
  217. * Testing reveals that PowerBooks do too. However, the SE/30
  218. * schematic diagram shows an active high NCR5380 IRQ line.
  219. */
  220. pr_debug("VIA2 vPCR is 0x%02X\n", via2[vPCR]);
  221. if (macintosh_config->via_type == MAC_VIA_II) {
  222. /* CA2 (SCSI DRQ), CB2 (SCSI IRQ): indep. input, pos. edge */
  223. via2[vPCR] = 0x66;
  224. } else {
  225. /* CA2 (SCSI DRQ), CB2 (SCSI IRQ): indep. input, neg. edge */
  226. via2[vPCR] = 0x22;
  227. }
  228. }
  229. /*
  230. * Debugging dump, used in various places to see what's going on.
  231. */
  232. void via_debug_dump(void)
  233. {
  234. printk(KERN_DEBUG "VIA1: DDRA = 0x%02X DDRB = 0x%02X ACR = 0x%02X\n",
  235. (uint) via1[vDirA], (uint) via1[vDirB], (uint) via1[vACR]);
  236. printk(KERN_DEBUG " PCR = 0x%02X IFR = 0x%02X IER = 0x%02X\n",
  237. (uint) via1[vPCR], (uint) via1[vIFR], (uint) via1[vIER]);
  238. if (!via2)
  239. return;
  240. if (rbv_present) {
  241. printk(KERN_DEBUG "VIA2: IFR = 0x%02X IER = 0x%02X\n",
  242. (uint) via2[rIFR], (uint) via2[rIER]);
  243. printk(KERN_DEBUG " SIFR = 0x%02X SIER = 0x%02X\n",
  244. (uint) via2[rSIFR], (uint) via2[rSIER]);
  245. } else {
  246. printk(KERN_DEBUG "VIA2: DDRA = 0x%02X DDRB = 0x%02X ACR = 0x%02X\n",
  247. (uint) via2[vDirA], (uint) via2[vDirB],
  248. (uint) via2[vACR]);
  249. printk(KERN_DEBUG " PCR = 0x%02X IFR = 0x%02X IER = 0x%02X\n",
  250. (uint) via2[vPCR],
  251. (uint) via2[vIFR], (uint) via2[vIER]);
  252. }
  253. }
  254. /*
  255. * Flush the L2 cache on Macs that have it by flipping
  256. * the system into 24-bit mode for an instant.
  257. */
  258. void via_l2_flush(int writeback)
  259. {
  260. unsigned long flags;
  261. local_irq_save(flags);
  262. via2[gBufB] &= ~VIA2B_vMode32;
  263. via2[gBufB] |= VIA2B_vMode32;
  264. local_irq_restore(flags);
  265. }
  266. /*
  267. * Initialize VIA2 for Nubus access
  268. */
  269. static void __init via_nubus_init(void)
  270. {
  271. /* unlock nubus transactions */
  272. if ((macintosh_config->adb_type != MAC_ADB_PB1) &&
  273. (macintosh_config->adb_type != MAC_ADB_PB2)) {
  274. /* set the line to be an output on non-RBV machines */
  275. if (!rbv_present)
  276. via2[vDirB] |= 0x02;
  277. /* this seems to be an ADB bit on PMU machines */
  278. /* according to MkLinux. -- jmt */
  279. via2[gBufB] |= 0x02;
  280. }
  281. /*
  282. * Disable the slot interrupts. On some hardware that's not possible.
  283. * On some hardware it's unclear what all of these I/O lines do.
  284. */
  285. switch (macintosh_config->via_type) {
  286. case MAC_VIA_II:
  287. case MAC_VIA_QUADRA:
  288. pr_debug("VIA2 vDirA is 0x%02X\n", via2[vDirA]);
  289. break;
  290. case MAC_VIA_IICI:
  291. /* RBV. Disable all the slot interrupts. SIER works like IER. */
  292. via2[rSIER] = 0x7F;
  293. break;
  294. }
  295. }
  296. void via_nubus_irq_startup(int irq)
  297. {
  298. int irq_idx = IRQ_IDX(irq);
  299. switch (macintosh_config->via_type) {
  300. case MAC_VIA_II:
  301. case MAC_VIA_QUADRA:
  302. /* Make the port A line an input. Probably redundant. */
  303. if (macintosh_config->via_type == MAC_VIA_II) {
  304. /* The top two bits are RAM size outputs. */
  305. via2[vDirA] &= 0xC0 | ~(1 << irq_idx);
  306. } else {
  307. /* Allow NuBus slots 9 through F. */
  308. via2[vDirA] &= 0x80 | ~(1 << irq_idx);
  309. }
  310. fallthrough;
  311. case MAC_VIA_IICI:
  312. via_irq_enable(irq);
  313. break;
  314. }
  315. }
  316. void via_nubus_irq_shutdown(int irq)
  317. {
  318. switch (macintosh_config->via_type) {
  319. case MAC_VIA_II:
  320. case MAC_VIA_QUADRA:
  321. /* Ensure that the umbrella CA1 interrupt remains enabled. */
  322. via_irq_enable(irq);
  323. break;
  324. case MAC_VIA_IICI:
  325. via_irq_disable(irq);
  326. break;
  327. }
  328. }
  329. /*
  330. * The generic VIA interrupt routines (shamelessly stolen from Alan Cox's
  331. * via6522.c :-), disable/pending masks added.
  332. */
  333. #define VIA_TIMER_1_INT BIT(6)
  334. void via1_irq(struct irq_desc *desc)
  335. {
  336. int irq_num;
  337. unsigned char irq_bit, events;
  338. events = via1[vIFR] & via1[vIER] & 0x7F;
  339. if (!events)
  340. return;
  341. irq_num = IRQ_MAC_TIMER_1;
  342. irq_bit = VIA_TIMER_1_INT;
  343. if (events & irq_bit) {
  344. unsigned long flags;
  345. local_irq_save(flags);
  346. via1[vIFR] = irq_bit;
  347. generic_handle_irq(irq_num);
  348. local_irq_restore(flags);
  349. events &= ~irq_bit;
  350. if (!events)
  351. return;
  352. }
  353. irq_num = VIA1_SOURCE_BASE;
  354. irq_bit = 1;
  355. do {
  356. if (events & irq_bit) {
  357. via1[vIFR] = irq_bit;
  358. generic_handle_irq(irq_num);
  359. }
  360. ++irq_num;
  361. irq_bit <<= 1;
  362. } while (events >= irq_bit);
  363. }
  364. static void via2_irq(struct irq_desc *desc)
  365. {
  366. int irq_num;
  367. unsigned char irq_bit, events;
  368. events = via2[gIFR] & via2[gIER] & 0x7F;
  369. if (!events)
  370. return;
  371. irq_num = VIA2_SOURCE_BASE;
  372. irq_bit = 1;
  373. do {
  374. if (events & irq_bit) {
  375. via2[gIFR] = irq_bit | rbv_clear;
  376. generic_handle_irq(irq_num);
  377. }
  378. ++irq_num;
  379. irq_bit <<= 1;
  380. } while (events >= irq_bit);
  381. }
  382. /*
  383. * Dispatch Nubus interrupts. We are called as a secondary dispatch by the
  384. * VIA2 dispatcher as a fast interrupt handler.
  385. */
  386. static void via_nubus_irq(struct irq_desc *desc)
  387. {
  388. int slot_irq;
  389. unsigned char slot_bit, events;
  390. events = ~via2[gBufA] & 0x7F;
  391. if (rbv_present)
  392. events &= via2[rSIER];
  393. else
  394. events &= ~via2[vDirA];
  395. if (!events)
  396. return;
  397. do {
  398. slot_irq = IRQ_NUBUS_F;
  399. slot_bit = 0x40;
  400. do {
  401. if (events & slot_bit) {
  402. events &= ~slot_bit;
  403. generic_handle_irq(slot_irq);
  404. }
  405. --slot_irq;
  406. slot_bit >>= 1;
  407. } while (events);
  408. /* clear the CA1 interrupt and make certain there's no more. */
  409. via2[gIFR] = 0x02 | rbv_clear;
  410. events = ~via2[gBufA] & 0x7F;
  411. if (rbv_present)
  412. events &= via2[rSIER];
  413. else
  414. events &= ~via2[vDirA];
  415. } while (events);
  416. }
  417. /*
  418. * Register the interrupt dispatchers for VIA or RBV machines only.
  419. */
  420. void __init via_register_interrupts(void)
  421. {
  422. if (via_alt_mapping) {
  423. /* software interrupt */
  424. irq_set_chained_handler(IRQ_AUTO_1, via1_irq);
  425. /* via1 interrupt */
  426. irq_set_chained_handler(IRQ_AUTO_6, via1_irq);
  427. } else {
  428. irq_set_chained_handler(IRQ_AUTO_1, via1_irq);
  429. }
  430. irq_set_chained_handler(IRQ_AUTO_2, via2_irq);
  431. irq_set_chained_handler(IRQ_MAC_NUBUS, via_nubus_irq);
  432. }
  433. void via_irq_enable(int irq) {
  434. int irq_src = IRQ_SRC(irq);
  435. int irq_idx = IRQ_IDX(irq);
  436. if (irq_src == 1) {
  437. via1[vIER] = IER_SET_BIT(irq_idx);
  438. } else if (irq_src == 2) {
  439. if (irq != IRQ_MAC_NUBUS || nubus_disabled == 0)
  440. via2[gIER] = IER_SET_BIT(irq_idx);
  441. } else if (irq_src == 7) {
  442. switch (macintosh_config->via_type) {
  443. case MAC_VIA_II:
  444. case MAC_VIA_QUADRA:
  445. nubus_disabled &= ~(1 << irq_idx);
  446. /* Enable the CA1 interrupt when no slot is disabled. */
  447. if (!nubus_disabled)
  448. via2[gIER] = IER_SET_BIT(1);
  449. break;
  450. case MAC_VIA_IICI:
  451. /* On RBV, enable the slot interrupt.
  452. * SIER works like IER.
  453. */
  454. via2[rSIER] = IER_SET_BIT(irq_idx);
  455. break;
  456. }
  457. }
  458. }
  459. void via_irq_disable(int irq) {
  460. int irq_src = IRQ_SRC(irq);
  461. int irq_idx = IRQ_IDX(irq);
  462. if (irq_src == 1) {
  463. via1[vIER] = IER_CLR_BIT(irq_idx);
  464. } else if (irq_src == 2) {
  465. via2[gIER] = IER_CLR_BIT(irq_idx);
  466. } else if (irq_src == 7) {
  467. switch (macintosh_config->via_type) {
  468. case MAC_VIA_II:
  469. case MAC_VIA_QUADRA:
  470. nubus_disabled |= 1 << irq_idx;
  471. if (nubus_disabled)
  472. via2[gIER] = IER_CLR_BIT(1);
  473. break;
  474. case MAC_VIA_IICI:
  475. via2[rSIER] = IER_CLR_BIT(irq_idx);
  476. break;
  477. }
  478. }
  479. }
  480. void via1_set_head(int head)
  481. {
  482. if (head == 0)
  483. via1[vBufA] &= ~VIA1A_vHeadSel;
  484. else
  485. via1[vBufA] |= VIA1A_vHeadSel;
  486. }
  487. EXPORT_SYMBOL(via1_set_head);
  488. int via2_scsi_drq_pending(void)
  489. {
  490. return via2[gIFR] & (1 << IRQ_IDX(IRQ_MAC_SCSIDRQ));
  491. }
  492. EXPORT_SYMBOL(via2_scsi_drq_pending);
  493. /* timer and clock source */
  494. #define VIA_CLOCK_FREQ 783360 /* VIA "phase 2" clock in Hz */
  495. #define VIA_TIMER_CYCLES (VIA_CLOCK_FREQ / HZ) /* clock cycles per jiffy */
  496. #define VIA_TC (VIA_TIMER_CYCLES - 2) /* including 0 and -1 */
  497. #define VIA_TC_LOW (VIA_TC & 0xFF)
  498. #define VIA_TC_HIGH (VIA_TC >> 8)
  499. static u64 mac_read_clk(struct clocksource *cs);
  500. static struct clocksource mac_clk = {
  501. .name = "via1",
  502. .rating = 250,
  503. .read = mac_read_clk,
  504. .mask = CLOCKSOURCE_MASK(32),
  505. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  506. };
  507. static u32 clk_total, clk_offset;
  508. static irqreturn_t via_timer_handler(int irq, void *dev_id)
  509. {
  510. clk_total += VIA_TIMER_CYCLES;
  511. clk_offset = 0;
  512. legacy_timer_tick(1);
  513. return IRQ_HANDLED;
  514. }
  515. void __init via_init_clock(void)
  516. {
  517. if (request_irq(IRQ_MAC_TIMER_1, via_timer_handler, IRQF_TIMER, "timer",
  518. NULL)) {
  519. pr_err("Couldn't register %s interrupt\n", "timer");
  520. return;
  521. }
  522. via1[vT1CL] = VIA_TC_LOW;
  523. via1[vT1CH] = VIA_TC_HIGH;
  524. via1[vACR] |= 0x40;
  525. clocksource_register_hz(&mac_clk, VIA_CLOCK_FREQ);
  526. }
  527. static u64 mac_read_clk(struct clocksource *cs)
  528. {
  529. unsigned long flags;
  530. u8 count_high;
  531. u16 count;
  532. u32 ticks;
  533. /*
  534. * Timer counter wrap-around is detected with the timer interrupt flag
  535. * but reading the counter low byte (vT1CL) would reset the flag.
  536. * Also, accessing both counter registers is essentially a data race.
  537. * These problems are avoided by ignoring the low byte. Clock accuracy
  538. * is 256 times worse (error can reach 0.327 ms) but CPU overhead is
  539. * reduced by avoiding slow VIA register accesses.
  540. */
  541. local_irq_save(flags);
  542. count_high = via1[vT1CH];
  543. if (count_high == 0xFF)
  544. count_high = 0;
  545. if (count_high > 0 && (via1[vIFR] & VIA_TIMER_1_INT))
  546. clk_offset = VIA_TIMER_CYCLES;
  547. count = count_high << 8;
  548. ticks = VIA_TIMER_CYCLES - count;
  549. ticks += clk_offset + clk_total;
  550. local_irq_restore(flags);
  551. return ticks;
  552. }