cs5536_mfgpt.c 5.4 KB

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