cs5536_mfgpt.c 5.4 KB

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