timer-cs5535.c 5.6 KB

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