vt8500_timer.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * arch/arm/mach-vt8500/timer.c
  3. *
  4. * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
  5. * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /*
  22. * This file is copied and modified from the original timer.c provided by
  23. * Alexey Charkov. Minor changes have been made for Device Tree Support.
  24. */
  25. #include <linux/io.h>
  26. #include <linux/irq.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/clocksource.h>
  29. #include <linux/clockchips.h>
  30. #include <linux/delay.h>
  31. #include <asm/mach/time.h>
  32. #include <linux/of.h>
  33. #include <linux/of_address.h>
  34. #include <linux/of_irq.h>
  35. #define VT8500_TIMER_OFFSET 0x0100
  36. #define VT8500_TIMER_HZ 3000000
  37. #define TIMER_MATCH_VAL 0x0000
  38. #define TIMER_COUNT_VAL 0x0010
  39. #define TIMER_STATUS_VAL 0x0014
  40. #define TIMER_IER_VAL 0x001c /* interrupt enable */
  41. #define TIMER_CTRL_VAL 0x0020
  42. #define TIMER_AS_VAL 0x0024 /* access status */
  43. #define TIMER_COUNT_R_ACTIVE (1 << 5) /* not ready for read */
  44. #define TIMER_COUNT_W_ACTIVE (1 << 4) /* not ready for write */
  45. #define TIMER_MATCH_W_ACTIVE (1 << 0) /* not ready for write */
  46. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  47. static void __iomem *regbase;
  48. static cycle_t vt8500_timer_read(struct clocksource *cs)
  49. {
  50. int loops = msecs_to_loops(10);
  51. writel(3, regbase + TIMER_CTRL_VAL);
  52. while ((readl((regbase + TIMER_AS_VAL)) & TIMER_COUNT_R_ACTIVE)
  53. && --loops)
  54. cpu_relax();
  55. return readl(regbase + TIMER_COUNT_VAL);
  56. }
  57. static struct clocksource clocksource = {
  58. .name = "vt8500_timer",
  59. .rating = 200,
  60. .read = vt8500_timer_read,
  61. .mask = CLOCKSOURCE_MASK(32),
  62. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  63. };
  64. static int vt8500_timer_set_next_event(unsigned long cycles,
  65. struct clock_event_device *evt)
  66. {
  67. int loops = msecs_to_loops(10);
  68. cycle_t alarm = clocksource.read(&clocksource) + cycles;
  69. while ((readl(regbase + TIMER_AS_VAL) & TIMER_MATCH_W_ACTIVE)
  70. && --loops)
  71. cpu_relax();
  72. writel((unsigned long)alarm, regbase + TIMER_MATCH_VAL);
  73. if ((signed)(alarm - clocksource.read(&clocksource)) <= 16)
  74. return -ETIME;
  75. writel(1, regbase + TIMER_IER_VAL);
  76. return 0;
  77. }
  78. static void vt8500_timer_set_mode(enum clock_event_mode mode,
  79. struct clock_event_device *evt)
  80. {
  81. switch (mode) {
  82. case CLOCK_EVT_MODE_RESUME:
  83. case CLOCK_EVT_MODE_PERIODIC:
  84. break;
  85. case CLOCK_EVT_MODE_ONESHOT:
  86. case CLOCK_EVT_MODE_UNUSED:
  87. case CLOCK_EVT_MODE_SHUTDOWN:
  88. writel(readl(regbase + TIMER_CTRL_VAL) | 1,
  89. regbase + TIMER_CTRL_VAL);
  90. writel(0, regbase + TIMER_IER_VAL);
  91. break;
  92. }
  93. }
  94. static struct clock_event_device clockevent = {
  95. .name = "vt8500_timer",
  96. .features = CLOCK_EVT_FEAT_ONESHOT,
  97. .rating = 200,
  98. .set_next_event = vt8500_timer_set_next_event,
  99. .set_mode = vt8500_timer_set_mode,
  100. };
  101. static irqreturn_t vt8500_timer_interrupt(int irq, void *dev_id)
  102. {
  103. struct clock_event_device *evt = dev_id;
  104. writel(0xf, regbase + TIMER_STATUS_VAL);
  105. evt->event_handler(evt);
  106. return IRQ_HANDLED;
  107. }
  108. static struct irqaction irq = {
  109. .name = "vt8500_timer",
  110. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  111. .handler = vt8500_timer_interrupt,
  112. .dev_id = &clockevent,
  113. };
  114. static struct of_device_id vt8500_timer_ids[] = {
  115. { .compatible = "via,vt8500-timer" },
  116. { }
  117. };
  118. static void __init vt8500_timer_init(void)
  119. {
  120. struct device_node *np;
  121. int timer_irq;
  122. np = of_find_matching_node(NULL, vt8500_timer_ids);
  123. if (!np) {
  124. pr_err("%s: Timer description missing from Device Tree\n",
  125. __func__);
  126. return;
  127. }
  128. regbase = of_iomap(np, 0);
  129. if (!regbase) {
  130. pr_err("%s: Missing iobase description in Device Tree\n",
  131. __func__);
  132. of_node_put(np);
  133. return;
  134. }
  135. timer_irq = irq_of_parse_and_map(np, 0);
  136. if (!timer_irq) {
  137. pr_err("%s: Missing irq description in Device Tree\n",
  138. __func__);
  139. of_node_put(np);
  140. return;
  141. }
  142. writel(1, regbase + TIMER_CTRL_VAL);
  143. writel(0xf, regbase + TIMER_STATUS_VAL);
  144. writel(~0, regbase + TIMER_MATCH_VAL);
  145. if (clocksource_register_hz(&clocksource, VT8500_TIMER_HZ))
  146. pr_err("%s: vt8500_timer_init: clocksource_register failed for %s\n",
  147. __func__, clocksource.name);
  148. clockevent.cpumask = cpumask_of(0);
  149. if (setup_irq(timer_irq, &irq))
  150. pr_err("%s: setup_irq failed for %s\n", __func__,
  151. clockevent.name);
  152. clockevents_config_and_register(&clockevent, VT8500_TIMER_HZ,
  153. 4, 0xf0000000);
  154. }
  155. CLOCKSOURCE_OF_DECLARE(vt8500, "via,vt8500-timer", vt8500_timer_init)