time.c 4.8 KB

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