cs5536_mfgpt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * CS5536 General timer functions
  3. *
  4. * Copyright (C) 2007 Lemote Inc. & Insititute of Computing Technology
  5. * Author: Yanhua, yanh@lemote.com
  6. *
  7. * Copyright (C) 2009 Lemote Inc.
  8. * Author: Wu zhangjin, wuzhangjin@gmail.com
  9. *
  10. * Reference: AMD Geode(TM) CS5536 Companion Device Data Book
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/io.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/clockchips.h>
  24. #include <asm/time.h>
  25. #include <cs5536/cs5536_mfgpt.h>
  26. static DEFINE_RAW_SPINLOCK(mfgpt_lock);
  27. static u32 mfgpt_base;
  28. /*
  29. * Initialize the MFGPT timer.
  30. *
  31. * This is also called after resume to bring the MFGPT into operation again.
  32. */
  33. /* disable counter */
  34. void disable_mfgpt0_counter(void)
  35. {
  36. outw(inw(MFGPT0_SETUP) & 0x7fff, MFGPT0_SETUP);
  37. }
  38. EXPORT_SYMBOL(disable_mfgpt0_counter);
  39. /* enable counter, comparator2 to event mode, 14.318MHz clock */
  40. void enable_mfgpt0_counter(void)
  41. {
  42. outw(0xe310, MFGPT0_SETUP);
  43. }
  44. EXPORT_SYMBOL(enable_mfgpt0_counter);
  45. static void init_mfgpt_timer(enum clock_event_mode mode,
  46. struct clock_event_device *evt)
  47. {
  48. raw_spin_lock(&mfgpt_lock);
  49. switch (mode) {
  50. case CLOCK_EVT_MODE_PERIODIC:
  51. outw(COMPARE, MFGPT0_CMP2); /* set comparator2 */
  52. outw(0, MFGPT0_CNT); /* set counter to 0 */
  53. enable_mfgpt0_counter();
  54. break;
  55. case CLOCK_EVT_MODE_SHUTDOWN:
  56. case CLOCK_EVT_MODE_UNUSED:
  57. if (evt->mode == CLOCK_EVT_MODE_PERIODIC ||
  58. evt->mode == CLOCK_EVT_MODE_ONESHOT)
  59. disable_mfgpt0_counter();
  60. break;
  61. case CLOCK_EVT_MODE_ONESHOT:
  62. /* The oneshot mode have very high deviation, Not use it! */
  63. break;
  64. case CLOCK_EVT_MODE_RESUME:
  65. /* Nothing to do here */
  66. break;
  67. }
  68. raw_spin_unlock(&mfgpt_lock);
  69. }
  70. static struct clock_event_device mfgpt_clockevent = {
  71. .name = "mfgpt",
  72. .features = CLOCK_EVT_FEAT_PERIODIC,
  73. .set_mode = init_mfgpt_timer,
  74. .irq = CS5536_MFGPT_INTR,
  75. };
  76. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  77. {
  78. u32 basehi;
  79. /*
  80. * get MFGPT base address
  81. *
  82. * NOTE: do not remove me, it's need for the value of mfgpt_base is
  83. * variable
  84. */
  85. _rdmsr(DIVIL_MSR_REG(DIVIL_LBAR_MFGPT), &basehi, &mfgpt_base);
  86. /* ack */
  87. outw(inw(MFGPT0_SETUP) | 0x4000, MFGPT0_SETUP);
  88. mfgpt_clockevent.event_handler(&mfgpt_clockevent);
  89. return IRQ_HANDLED;
  90. }
  91. static struct irqaction irq5 = {
  92. .handler = timer_interrupt,
  93. .flags = IRQF_NOBALANCING | IRQF_TIMER,
  94. .name = "timer"
  95. };
  96. /*
  97. * Initialize the conversion factor and the min/max deltas of the clock event
  98. * structure and register the clock event source with the framework.
  99. */
  100. void __init setup_mfgpt0_timer(void)
  101. {
  102. u32 basehi;
  103. struct clock_event_device *cd = &mfgpt_clockevent;
  104. unsigned int cpu = smp_processor_id();
  105. cd->cpumask = cpumask_of(cpu);
  106. clockevent_set_clock(cd, MFGPT_TICK_RATE);
  107. cd->max_delta_ns = clockevent_delta2ns(0xffff, cd);
  108. cd->min_delta_ns = clockevent_delta2ns(0xf, cd);
  109. /* Enable MFGPT0 Comparator 2 Output to the Interrupt Mapper */
  110. _wrmsr(DIVIL_MSR_REG(MFGPT_IRQ), 0, 0x100);
  111. /* Enable Interrupt Gate 5 */
  112. _wrmsr(DIVIL_MSR_REG(PIC_ZSEL_LOW), 0, 0x50000);
  113. /* get MFGPT base address */
  114. _rdmsr(DIVIL_MSR_REG(DIVIL_LBAR_MFGPT), &basehi, &mfgpt_base);
  115. clockevents_register_device(cd);
  116. setup_irq(CS5536_MFGPT_INTR, &irq5);
  117. }
  118. /*
  119. * Since the MFGPT overflows every tick, its not very useful
  120. * to just read by itself. So use jiffies to emulate a free
  121. * running counter:
  122. */
  123. static cycle_t mfgpt_read(struct clocksource *cs)
  124. {
  125. unsigned long flags;
  126. int count;
  127. u32 jifs;
  128. static int old_count;
  129. static u32 old_jifs;
  130. raw_spin_lock_irqsave(&mfgpt_lock, flags);
  131. /*
  132. * Although our caller may have the read side of xtime_lock,
  133. * this is now a seqlock, and we are cheating in this routine
  134. * by having side effects on state that we cannot undo if
  135. * there is a collision on the seqlock and our caller has to
  136. * retry. (Namely, old_jifs and old_count.) So we must treat
  137. * jiffies as volatile despite the lock. We read jiffies
  138. * before latching the timer count to guarantee that although
  139. * the jiffies value might be older than the count (that is,
  140. * the counter may underflow between the last point where
  141. * jiffies was incremented and the point where we latch the
  142. * count), it cannot be newer.
  143. */
  144. jifs = jiffies;
  145. /* read the count */
  146. count = inw(MFGPT0_CNT);
  147. /*
  148. * It's possible for count to appear to go the wrong way for this
  149. * reason:
  150. *
  151. * The timer counter underflows, but we haven't handled the resulting
  152. * interrupt and incremented jiffies yet.
  153. *
  154. * Previous attempts to handle these cases intelligently were buggy, so
  155. * we just do the simple thing now.
  156. */
  157. if (count < old_count && jifs == old_jifs)
  158. count = old_count;
  159. old_count = count;
  160. old_jifs = jifs;
  161. raw_spin_unlock_irqrestore(&mfgpt_lock, flags);
  162. return (cycle_t) (jifs * COMPARE) + count;
  163. }
  164. static struct clocksource clocksource_mfgpt = {
  165. .name = "mfgpt",
  166. .rating = 120, /* Functional for real use, but not desired */
  167. .read = mfgpt_read,
  168. .mask = CLOCKSOURCE_MASK(32),
  169. };
  170. int __init init_mfgpt_clocksource(void)
  171. {
  172. if (num_possible_cpus() > 1) /* MFGPT does not scale! */
  173. return 0;
  174. return clocksource_register_hz(&clocksource_mfgpt, MFGPT_TICK_RATE);
  175. }
  176. arch_initcall(init_mfgpt_clocksource);