time.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * linux/arch/arm/mach-mmp/time.c
  3. *
  4. * Support for clocksource and clockevents
  5. *
  6. * Copyright (C) 2008 Marvell International Ltd.
  7. * All rights reserved.
  8. *
  9. * 2008-04-11: Jason Chagas <Jason.chagas@marvell.com>
  10. * 2008-10-08: Bin Yang <bin.yang@marvell.com>
  11. *
  12. * The timers module actually includes three timers, each timer with upto
  13. * three match comparators. Timer #0 is used here in free-running mode as
  14. * the clock source, and match comparator #1 used as clock event device.
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2 as
  18. * published by the Free Software Foundation.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/clockchips.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/sched.h>
  27. #include <linux/cnt32_to_63.h>
  28. #include <mach/addr-map.h>
  29. #include <mach/regs-timers.h>
  30. #include <mach/irqs.h>
  31. #include "clock.h"
  32. #define TIMERS_VIRT_BASE TIMERS1_VIRT_BASE
  33. #define MAX_DELTA (0xfffffffe)
  34. #define MIN_DELTA (16)
  35. #define TCR2NS_SCALE_FACTOR 10
  36. static unsigned long tcr2ns_scale;
  37. static void __init set_tcr2ns_scale(unsigned long tcr_rate)
  38. {
  39. unsigned long long v = 1000000000ULL << TCR2NS_SCALE_FACTOR;
  40. do_div(v, tcr_rate);
  41. tcr2ns_scale = v;
  42. /*
  43. * We want an even value to automatically clear the top bit
  44. * returned by cnt32_to_63() without an additional run time
  45. * instruction. So if the LSB is 1 then round it up.
  46. */
  47. if (tcr2ns_scale & 1)
  48. tcr2ns_scale++;
  49. }
  50. /*
  51. * FIXME: the timer needs some delay to stablize the counter capture
  52. */
  53. static inline uint32_t timer_read(void)
  54. {
  55. int delay = 100;
  56. __raw_writel(1, TIMERS_VIRT_BASE + TMR_CVWR(0));
  57. while (delay--)
  58. cpu_relax();
  59. return __raw_readl(TIMERS_VIRT_BASE + TMR_CVWR(0));
  60. }
  61. unsigned long long sched_clock(void)
  62. {
  63. unsigned long long v = cnt32_to_63(timer_read());
  64. return (v * tcr2ns_scale) >> TCR2NS_SCALE_FACTOR;
  65. }
  66. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  67. {
  68. struct clock_event_device *c = dev_id;
  69. /* disable and clear pending interrupt status */
  70. __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_IER(0));
  71. __raw_writel(0x1, TIMERS_VIRT_BASE + TMR_ICR(0));
  72. c->event_handler(c);
  73. return IRQ_HANDLED;
  74. }
  75. static int timer_set_next_event(unsigned long delta,
  76. struct clock_event_device *dev)
  77. {
  78. unsigned long flags, next;
  79. local_irq_save(flags);
  80. /* clear pending interrupt status and enable */
  81. __raw_writel(0x01, TIMERS_VIRT_BASE + TMR_ICR(0));
  82. __raw_writel(0x01, TIMERS_VIRT_BASE + TMR_IER(0));
  83. next = timer_read() + delta;
  84. __raw_writel(next, TIMERS_VIRT_BASE + TMR_TN_MM(0, 0));
  85. local_irq_restore(flags);
  86. return 0;
  87. }
  88. static void timer_set_mode(enum clock_event_mode mode,
  89. struct clock_event_device *dev)
  90. {
  91. unsigned long flags;
  92. local_irq_save(flags);
  93. switch (mode) {
  94. case CLOCK_EVT_MODE_ONESHOT:
  95. case CLOCK_EVT_MODE_UNUSED:
  96. case CLOCK_EVT_MODE_SHUTDOWN:
  97. /* disable the matching interrupt */
  98. __raw_writel(0x00, TIMERS_VIRT_BASE + TMR_IER(0));
  99. break;
  100. case CLOCK_EVT_MODE_RESUME:
  101. case CLOCK_EVT_MODE_PERIODIC:
  102. break;
  103. }
  104. local_irq_restore(flags);
  105. }
  106. static struct clock_event_device ckevt = {
  107. .name = "clockevent",
  108. .features = CLOCK_EVT_FEAT_ONESHOT,
  109. .shift = 32,
  110. .rating = 200,
  111. .set_next_event = timer_set_next_event,
  112. .set_mode = timer_set_mode,
  113. };
  114. static cycle_t clksrc_read(struct clocksource *cs)
  115. {
  116. return timer_read();
  117. }
  118. static struct clocksource cksrc = {
  119. .name = "clocksource",
  120. .shift = 20,
  121. .rating = 200,
  122. .read = clksrc_read,
  123. .mask = CLOCKSOURCE_MASK(32),
  124. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  125. };
  126. static void __init timer_config(void)
  127. {
  128. uint32_t ccr = __raw_readl(TIMERS_VIRT_BASE + TMR_CCR);
  129. uint32_t cer = __raw_readl(TIMERS_VIRT_BASE + TMR_CER);
  130. uint32_t cmr = __raw_readl(TIMERS_VIRT_BASE + TMR_CMR);
  131. __raw_writel(cer & ~0x1, TIMERS_VIRT_BASE + TMR_CER); /* disable */
  132. ccr &= TMR_CCR_CS_0(0x3);
  133. __raw_writel(ccr, TIMERS_VIRT_BASE + TMR_CCR);
  134. /* free-running mode */
  135. __raw_writel(cmr | 0x01, TIMERS_VIRT_BASE + TMR_CMR);
  136. __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_PLCR(0)); /* free-running */
  137. __raw_writel(0x7, TIMERS_VIRT_BASE + TMR_ICR(0)); /* clear status */
  138. __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_IER(0));
  139. /* enable timer counter */
  140. __raw_writel(cer | 0x01, TIMERS_VIRT_BASE + TMR_CER);
  141. }
  142. static struct irqaction timer_irq = {
  143. .name = "timer",
  144. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  145. .handler = timer_interrupt,
  146. .dev_id = &ckevt,
  147. };
  148. void __init timer_init(int irq)
  149. {
  150. timer_config();
  151. set_tcr2ns_scale(CLOCK_TICK_RATE);
  152. ckevt.mult = div_sc(CLOCK_TICK_RATE, NSEC_PER_SEC, ckevt.shift);
  153. ckevt.max_delta_ns = clockevent_delta2ns(MAX_DELTA, &ckevt);
  154. ckevt.min_delta_ns = clockevent_delta2ns(MIN_DELTA, &ckevt);
  155. ckevt.cpumask = cpumask_of(0);
  156. cksrc.mult = clocksource_hz2mult(CLOCK_TICK_RATE, cksrc.shift);
  157. setup_irq(irq, &timer_irq);
  158. clocksource_register(&cksrc);
  159. clockevents_register_device(&ckevt);
  160. }