ltimer.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Driver for LAMEbus clock/timer card
  31. */
  32. #include <types.h>
  33. #include <lib.h>
  34. #include <spl.h>
  35. #include <clock.h>
  36. #include <platform/bus.h>
  37. #include <lamebus/ltimer.h>
  38. #include "autoconf.h"
  39. /* Registers (offsets within slot) */
  40. #define LT_REG_SEC 0 /* time of day: seconds */
  41. #define LT_REG_NSEC 4 /* time of day: nanoseconds */
  42. #define LT_REG_ROE 8 /* Restart On countdown-timer Expiry flag */
  43. #define LT_REG_IRQ 12 /* Interrupt status register */
  44. #define LT_REG_COUNT 16 /* Time for countdown timer (usec) */
  45. #define LT_REG_SPKR 20 /* Beep control */
  46. static bool havetimerclock;
  47. /*
  48. * Setup routine called by autoconf stuff when an ltimer is found.
  49. */
  50. int
  51. config_ltimer(struct ltimer_softc *lt, int ltimerno)
  52. {
  53. /*
  54. * Running on System/161 2.x, we always use the processor
  55. * on-chip timer for hardclock and we don't need ltimer as
  56. * hardclock.
  57. *
  58. * Ideally there should be code here that will use an ltimer
  59. * for hardclock if nothing else is available; e.g. if we
  60. * wanted to make OS/161 2.x run on System/161 1.x. However,
  61. * that requires a good bit more infrastructure for handling
  62. * timers than we have and it doesn't seem worthwhile.
  63. *
  64. * It would also require some hacking, because all CPUs need
  65. * to receive timer interrupts. (Exercise: how would you make
  66. * sure all CPUs receive exactly one timer interrupt? Remember
  67. * that LAMEbus uses level-triggered interrupts, so the
  68. * hardware interrupt line will cause repeated interrupts if
  69. * it's not reset on the device; but if it's reset on the
  70. * device before all CPUs manage to see it, those CPUs won't
  71. * be interrupted at all.)
  72. *
  73. * Note that the beep and rtclock devices *do* attach to
  74. * ltimer.
  75. */
  76. (void)ltimerno;
  77. lt->lt_hardclock = 0;
  78. /*
  79. * We do, however, use ltimer for the timer clock, since the
  80. * on-chip timer can't do that.
  81. */
  82. if (!havetimerclock) {
  83. havetimerclock = true;
  84. lt->lt_timerclock = 1;
  85. /* Wire it to go off once every 10 ms */
  86. /* KMS: reduced this from 1s to 10ms */
  87. bus_write_register(lt->lt_bus, lt->lt_buspos, LT_REG_ROE, 1);
  88. bus_write_register(lt->lt_bus, lt->lt_buspos, LT_REG_COUNT,
  89. LT_GRANULARITY);
  90. }
  91. return 0;
  92. }
  93. /*
  94. * Interrupt handler.
  95. */
  96. void
  97. ltimer_irq(void *vlt)
  98. {
  99. struct ltimer_softc *lt = vlt;
  100. uint32_t val;
  101. val = bus_read_register(lt->lt_bus, lt->lt_buspos, LT_REG_IRQ);
  102. if (val) {
  103. /*
  104. * Only call hardclock if we're responsible for hardclock.
  105. * (Any additional timer devices are unused.)
  106. */
  107. if (lt->lt_hardclock) {
  108. hardclock();
  109. }
  110. /*
  111. * Likewise for timerclock.
  112. */
  113. if (lt->lt_timerclock) {
  114. timerclock();
  115. }
  116. }
  117. }
  118. /*
  119. * The timer device will beep if you write to the beep register. It
  120. * doesn't matter what value you write. This function is called if
  121. * the beep device is attached to this timer.
  122. */
  123. void
  124. ltimer_beep(void *vlt)
  125. {
  126. struct ltimer_softc *lt = vlt;
  127. bus_write_register(lt->lt_bus, lt->lt_buspos, LT_REG_SPKR, 440);
  128. }
  129. /*
  130. * The timer device also has a realtime clock on it.
  131. * This function gets called if the rtclock device is attached
  132. * to this timer.
  133. */
  134. void
  135. ltimer_gettime(void *vlt, time_t *secs, uint32_t *nsecs)
  136. {
  137. struct ltimer_softc *lt = vlt;
  138. uint32_t secs1, secs2;
  139. int spl;
  140. /*
  141. * Read the seconds twice, on either side of the nanoseconds.
  142. * If nsecs is small, use the *later* value of seconds, in case
  143. * the nanoseconds turned over between the time we got the earlier
  144. * value and the time we got nsecs.
  145. *
  146. * Note that the clock in the ltimer device is accurate down
  147. * to a single processor cycle, so this might actually matter
  148. * now and then.
  149. *
  150. * Do it with interrupts off on the current processor to avoid
  151. * getting garbage if we get an interrupt among the register
  152. * reads.
  153. */
  154. spl = splhigh();
  155. secs1 = bus_read_register(lt->lt_bus, lt->lt_buspos,
  156. LT_REG_SEC);
  157. *nsecs = bus_read_register(lt->lt_bus, lt->lt_buspos,
  158. LT_REG_NSEC);
  159. secs2 = bus_read_register(lt->lt_bus, lt->lt_buspos,
  160. LT_REG_SEC);
  161. splx(spl);
  162. if (*nsecs < 5000000) {
  163. *secs = secs2;
  164. }
  165. else {
  166. *secs = secs1;
  167. }
  168. }