via.c 16 KB

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