time.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 up to
  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 <asm/sched_clock.h>
  28. #include <mach/addr-map.h>
  29. #include <mach/regs-timers.h>
  30. #include <mach/regs-apbc.h>
  31. #include <mach/irqs.h>
  32. #include <mach/cputype.h>
  33. #include <asm/mach/time.h>
  34. #include "clock.h"
  35. #define TIMERS_VIRT_BASE TIMERS1_VIRT_BASE
  36. #define MAX_DELTA (0xfffffffe)
  37. #define MIN_DELTA (16)
  38. static DEFINE_CLOCK_DATA(cd);
  39. /*
  40. * FIXME: the timer needs some delay to stablize the counter capture
  41. */
  42. static inline uint32_t timer_read(void)
  43. {
  44. int delay = 100;
  45. __raw_writel(1, TIMERS_VIRT_BASE + TMR_CVWR(1));
  46. while (delay--)
  47. cpu_relax();
  48. return __raw_readl(TIMERS_VIRT_BASE + TMR_CVWR(1));
  49. }
  50. unsigned long long notrace sched_clock(void)
  51. {
  52. u32 cyc = timer_read();
  53. return cyc_to_sched_clock(&cd, cyc, (u32)~0);
  54. }
  55. static void notrace mmp_update_sched_clock(void)
  56. {
  57. u32 cyc = timer_read();
  58. update_sched_clock(&cd, cyc, (u32)~0);
  59. }
  60. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  61. {
  62. struct clock_event_device *c = dev_id;
  63. /*
  64. * Clear pending interrupt status.
  65. */
  66. __raw_writel(0x01, TIMERS_VIRT_BASE + TMR_ICR(0));
  67. /*
  68. * Disable timer 0.
  69. */
  70. __raw_writel(0x02, TIMERS_VIRT_BASE + TMR_CER);
  71. c->event_handler(c);
  72. return IRQ_HANDLED;
  73. }
  74. static int timer_set_next_event(unsigned long delta,
  75. struct clock_event_device *dev)
  76. {
  77. unsigned long flags;
  78. local_irq_save(flags);
  79. /*
  80. * Disable timer 0.
  81. */
  82. __raw_writel(0x02, TIMERS_VIRT_BASE + TMR_CER);
  83. /*
  84. * Clear and enable timer match 0 interrupt.
  85. */
  86. __raw_writel(0x01, TIMERS_VIRT_BASE + TMR_ICR(0));
  87. __raw_writel(0x01, TIMERS_VIRT_BASE + TMR_IER(0));
  88. /*
  89. * Setup new clockevent timer value.
  90. */
  91. __raw_writel(delta - 1, TIMERS_VIRT_BASE + TMR_TN_MM(0, 0));
  92. /*
  93. * Enable timer 0.
  94. */
  95. __raw_writel(0x03, TIMERS_VIRT_BASE + TMR_CER);
  96. local_irq_restore(flags);
  97. return 0;
  98. }
  99. static void timer_set_mode(enum clock_event_mode mode,
  100. struct clock_event_device *dev)
  101. {
  102. unsigned long flags;
  103. local_irq_save(flags);
  104. switch (mode) {
  105. case CLOCK_EVT_MODE_ONESHOT:
  106. case CLOCK_EVT_MODE_UNUSED:
  107. case CLOCK_EVT_MODE_SHUTDOWN:
  108. /* disable the matching interrupt */
  109. __raw_writel(0x00, TIMERS_VIRT_BASE + TMR_IER(0));
  110. break;
  111. case CLOCK_EVT_MODE_RESUME:
  112. case CLOCK_EVT_MODE_PERIODIC:
  113. break;
  114. }
  115. local_irq_restore(flags);
  116. }
  117. static struct clock_event_device ckevt = {
  118. .name = "clockevent",
  119. .features = CLOCK_EVT_FEAT_ONESHOT,
  120. .shift = 32,
  121. .rating = 200,
  122. .set_next_event = timer_set_next_event,
  123. .set_mode = timer_set_mode,
  124. };
  125. static cycle_t clksrc_read(struct clocksource *cs)
  126. {
  127. return timer_read();
  128. }
  129. static struct clocksource cksrc = {
  130. .name = "clocksource",
  131. .rating = 200,
  132. .read = clksrc_read,
  133. .mask = CLOCKSOURCE_MASK(32),
  134. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  135. };
  136. static void __init timer_config(void)
  137. {
  138. uint32_t ccr = __raw_readl(TIMERS_VIRT_BASE + TMR_CCR);
  139. __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_CER); /* disable */
  140. ccr &= (cpu_is_mmp2()) ? (TMR_CCR_CS_0(0) | TMR_CCR_CS_1(0)) :
  141. (TMR_CCR_CS_0(3) | TMR_CCR_CS_1(3));
  142. __raw_writel(ccr, TIMERS_VIRT_BASE + TMR_CCR);
  143. /* set timer 0 to periodic mode, and timer 1 to free-running mode */
  144. __raw_writel(0x2, TIMERS_VIRT_BASE + TMR_CMR);
  145. __raw_writel(0x1, TIMERS_VIRT_BASE + TMR_PLCR(0)); /* periodic */
  146. __raw_writel(0x7, TIMERS_VIRT_BASE + TMR_ICR(0)); /* clear status */
  147. __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_IER(0));
  148. __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_PLCR(1)); /* free-running */
  149. __raw_writel(0x7, TIMERS_VIRT_BASE + TMR_ICR(1)); /* clear status */
  150. __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_IER(1));
  151. /* enable timer 1 counter */
  152. __raw_writel(0x2, TIMERS_VIRT_BASE + TMR_CER);
  153. }
  154. static struct irqaction timer_irq = {
  155. .name = "timer",
  156. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  157. .handler = timer_interrupt,
  158. .dev_id = &ckevt,
  159. };
  160. void __init timer_init(int irq)
  161. {
  162. timer_config();
  163. init_sched_clock(&cd, mmp_update_sched_clock, 32, CLOCK_TICK_RATE);
  164. ckevt.mult = div_sc(CLOCK_TICK_RATE, NSEC_PER_SEC, ckevt.shift);
  165. ckevt.max_delta_ns = clockevent_delta2ns(MAX_DELTA, &ckevt);
  166. ckevt.min_delta_ns = clockevent_delta2ns(MIN_DELTA, &ckevt);
  167. ckevt.cpumask = cpumask_of(0);
  168. setup_irq(irq, &timer_irq);
  169. clocksource_register_hz(&cksrc, CLOCK_TICK_RATE);
  170. clockevents_register_device(&ckevt);
  171. }