cs5535-clockevt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Clock event driver for the CS5535/CS5536
  3. *
  4. * Copyright (C) 2006, Advanced Micro Devices, Inc.
  5. * Copyright (C) 2007 Andres Salomon <dilinger@debian.org>
  6. * Copyright (C) 2009 Andres Salomon <dilinger@collabora.co.uk>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of version 2 of the GNU General Public License
  10. * as published by the Free Software Foundation.
  11. *
  12. * The MFGPTs are documented in AMD Geode CS5536 Companion Device Data Book.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/irq.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/cs5535.h>
  19. #include <linux/clockchips.h>
  20. #define DRV_NAME "cs5535-clockevt"
  21. static int timer_irq;
  22. module_param_named(irq, timer_irq, int, 0644);
  23. MODULE_PARM_DESC(irq, "Which IRQ to use for the clock source MFGPT ticks.");
  24. /*
  25. * We are using the 32.768kHz input clock - it's the only one that has the
  26. * ranges we find desirable. The following table lists the suitable
  27. * divisors and the associated Hz, minimum interval and the maximum interval:
  28. *
  29. * Divisor Hz Min Delta (s) Max Delta (s)
  30. * 1 32768 .00048828125 2.000
  31. * 2 16384 .0009765625 4.000
  32. * 4 8192 .001953125 8.000
  33. * 8 4096 .00390625 16.000
  34. * 16 2048 .0078125 32.000
  35. * 32 1024 .015625 64.000
  36. * 64 512 .03125 128.000
  37. * 128 256 .0625 256.000
  38. * 256 128 .125 512.000
  39. */
  40. static unsigned int cs5535_tick_mode = CLOCK_EVT_MODE_SHUTDOWN;
  41. static struct cs5535_mfgpt_timer *cs5535_event_clock;
  42. /* Selected from the table above */
  43. #define MFGPT_DIVISOR 16
  44. #define MFGPT_SCALE 4 /* divisor = 2^(scale) */
  45. #define MFGPT_HZ (32768 / MFGPT_DIVISOR)
  46. #define MFGPT_PERIODIC (MFGPT_HZ / HZ)
  47. /*
  48. * The MFPGT timers on the CS5536 provide us with suitable timers to use
  49. * as clock event sources - not as good as a HPET or APIC, but certainly
  50. * better than the PIT. This isn't a general purpose MFGPT driver, but
  51. * a simplified one designed specifically to act as a clock event source.
  52. * For full details about the MFGPT, please consult the CS5536 data sheet.
  53. */
  54. static void disable_timer(struct cs5535_mfgpt_timer *timer)
  55. {
  56. /* avoid races by clearing CMP1 and CMP2 unconditionally */
  57. cs5535_mfgpt_write(timer, MFGPT_REG_SETUP,
  58. (uint16_t) ~MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP1 |
  59. MFGPT_SETUP_CMP2);
  60. }
  61. static void start_timer(struct cs5535_mfgpt_timer *timer, uint16_t delta)
  62. {
  63. cs5535_mfgpt_write(timer, MFGPT_REG_CMP2, delta);
  64. cs5535_mfgpt_write(timer, MFGPT_REG_COUNTER, 0);
  65. cs5535_mfgpt_write(timer, MFGPT_REG_SETUP,
  66. MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
  67. }
  68. static void mfgpt_set_mode(enum clock_event_mode mode,
  69. struct clock_event_device *evt)
  70. {
  71. disable_timer(cs5535_event_clock);
  72. if (mode == CLOCK_EVT_MODE_PERIODIC)
  73. start_timer(cs5535_event_clock, MFGPT_PERIODIC);
  74. cs5535_tick_mode = mode;
  75. }
  76. static int mfgpt_next_event(unsigned long delta, struct clock_event_device *evt)
  77. {
  78. start_timer(cs5535_event_clock, delta);
  79. return 0;
  80. }
  81. static struct clock_event_device cs5535_clockevent = {
  82. .name = DRV_NAME,
  83. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  84. .set_mode = mfgpt_set_mode,
  85. .set_next_event = mfgpt_next_event,
  86. .rating = 250,
  87. .shift = 32
  88. };
  89. static irqreturn_t mfgpt_tick(int irq, void *dev_id)
  90. {
  91. uint16_t val = cs5535_mfgpt_read(cs5535_event_clock, MFGPT_REG_SETUP);
  92. /* See if the interrupt was for us */
  93. if (!(val & (MFGPT_SETUP_SETUP | MFGPT_SETUP_CMP2 | MFGPT_SETUP_CMP1)))
  94. return IRQ_NONE;
  95. /* Turn off the clock (and clear the event) */
  96. disable_timer(cs5535_event_clock);
  97. if (cs5535_tick_mode == CLOCK_EVT_MODE_SHUTDOWN)
  98. return IRQ_HANDLED;
  99. /* Clear the counter */
  100. cs5535_mfgpt_write(cs5535_event_clock, MFGPT_REG_COUNTER, 0);
  101. /* Restart the clock in periodic mode */
  102. if (cs5535_tick_mode == CLOCK_EVT_MODE_PERIODIC)
  103. cs5535_mfgpt_write(cs5535_event_clock, MFGPT_REG_SETUP,
  104. MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
  105. cs5535_clockevent.event_handler(&cs5535_clockevent);
  106. return IRQ_HANDLED;
  107. }
  108. static struct irqaction mfgptirq = {
  109. .handler = mfgpt_tick,
  110. .flags = IRQF_DISABLED | IRQF_NOBALANCING | IRQF_TIMER | IRQF_SHARED,
  111. .name = DRV_NAME,
  112. };
  113. static int __init cs5535_mfgpt_init(void)
  114. {
  115. struct cs5535_mfgpt_timer *timer;
  116. int ret;
  117. uint16_t val;
  118. timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
  119. if (!timer) {
  120. printk(KERN_ERR DRV_NAME ": Could not allocate MFPGT timer\n");
  121. return -ENODEV;
  122. }
  123. cs5535_event_clock = timer;
  124. /* Set up the IRQ on the MFGPT side */
  125. if (cs5535_mfgpt_setup_irq(timer, MFGPT_CMP2, &timer_irq)) {
  126. printk(KERN_ERR DRV_NAME ": Could not set up IRQ %d\n",
  127. timer_irq);
  128. goto err_timer;
  129. }
  130. /* And register it with the kernel */
  131. ret = setup_irq(timer_irq, &mfgptirq);
  132. if (ret) {
  133. printk(KERN_ERR DRV_NAME ": Unable to set up the interrupt.\n");
  134. goto err_irq;
  135. }
  136. /* Set the clock scale and enable the event mode for CMP2 */
  137. val = MFGPT_SCALE | (3 << 8);
  138. cs5535_mfgpt_write(cs5535_event_clock, MFGPT_REG_SETUP, val);
  139. /* Set up the clock event */
  140. cs5535_clockevent.mult = div_sc(MFGPT_HZ, NSEC_PER_SEC,
  141. cs5535_clockevent.shift);
  142. cs5535_clockevent.min_delta_ns = clockevent_delta2ns(0xF,
  143. &cs5535_clockevent);
  144. cs5535_clockevent.max_delta_ns = clockevent_delta2ns(0xFFFE,
  145. &cs5535_clockevent);
  146. printk(KERN_INFO DRV_NAME
  147. ": Registering MFGPT timer as a clock event, using IRQ %d\n",
  148. timer_irq);
  149. clockevents_register_device(&cs5535_clockevent);
  150. return 0;
  151. err_irq:
  152. cs5535_mfgpt_release_irq(cs5535_event_clock, MFGPT_CMP2, &timer_irq);
  153. err_timer:
  154. cs5535_mfgpt_free_timer(cs5535_event_clock);
  155. printk(KERN_ERR DRV_NAME ": Unable to set up the MFGPT clock source\n");
  156. return -EIO;
  157. }
  158. module_init(cs5535_mfgpt_init);
  159. MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
  160. MODULE_DESCRIPTION("CS5535/CS5536 MFGPT clock event driver");
  161. MODULE_LICENSE("GPL");