time.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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(0));
  46. while (delay--)
  47. cpu_relax();
  48. return __raw_readl(TIMERS_VIRT_BASE + TMR_CVWR(0));
  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. /* disable and clear pending interrupt status */
  64. __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_IER(0));
  65. __raw_writel(0x1, TIMERS_VIRT_BASE + TMR_ICR(0));
  66. c->event_handler(c);
  67. return IRQ_HANDLED;
  68. }
  69. static int timer_set_next_event(unsigned long delta,
  70. struct clock_event_device *dev)
  71. {
  72. unsigned long flags, next;
  73. local_irq_save(flags);
  74. /* clear pending interrupt status and enable */
  75. __raw_writel(0x01, TIMERS_VIRT_BASE + TMR_ICR(0));
  76. __raw_writel(0x01, TIMERS_VIRT_BASE + TMR_IER(0));
  77. next = timer_read() + delta;
  78. __raw_writel(next, TIMERS_VIRT_BASE + TMR_TN_MM(0, 0));
  79. local_irq_restore(flags);
  80. return 0;
  81. }
  82. static void timer_set_mode(enum clock_event_mode mode,
  83. struct clock_event_device *dev)
  84. {
  85. unsigned long flags;
  86. local_irq_save(flags);
  87. switch (mode) {
  88. case CLOCK_EVT_MODE_ONESHOT:
  89. case CLOCK_EVT_MODE_UNUSED:
  90. case CLOCK_EVT_MODE_SHUTDOWN:
  91. /* disable the matching interrupt */
  92. __raw_writel(0x00, TIMERS_VIRT_BASE + TMR_IER(0));
  93. break;
  94. case CLOCK_EVT_MODE_RESUME:
  95. case CLOCK_EVT_MODE_PERIODIC:
  96. break;
  97. }
  98. local_irq_restore(flags);
  99. }
  100. static struct clock_event_device ckevt = {
  101. .name = "clockevent",
  102. .features = CLOCK_EVT_FEAT_ONESHOT,
  103. .shift = 32,
  104. .rating = 200,
  105. .set_next_event = timer_set_next_event,
  106. .set_mode = timer_set_mode,
  107. };
  108. static cycle_t clksrc_read(struct clocksource *cs)
  109. {
  110. return timer_read();
  111. }
  112. static struct clocksource cksrc = {
  113. .name = "clocksource",
  114. .rating = 200,
  115. .read = clksrc_read,
  116. .mask = CLOCKSOURCE_MASK(32),
  117. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  118. };
  119. static void __init timer_config(void)
  120. {
  121. uint32_t ccr = __raw_readl(TIMERS_VIRT_BASE + TMR_CCR);
  122. uint32_t cer = __raw_readl(TIMERS_VIRT_BASE + TMR_CER);
  123. uint32_t cmr = __raw_readl(TIMERS_VIRT_BASE + TMR_CMR);
  124. __raw_writel(cer & ~0x1, TIMERS_VIRT_BASE + TMR_CER); /* disable */
  125. ccr &= (cpu_is_mmp2()) ? TMR_CCR_CS_0(0) : TMR_CCR_CS_0(3);
  126. __raw_writel(ccr, TIMERS_VIRT_BASE + TMR_CCR);
  127. /* free-running mode */
  128. __raw_writel(cmr | 0x01, TIMERS_VIRT_BASE + TMR_CMR);
  129. __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_PLCR(0)); /* free-running */
  130. __raw_writel(0x7, TIMERS_VIRT_BASE + TMR_ICR(0)); /* clear status */
  131. __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_IER(0));
  132. /* enable timer counter */
  133. __raw_writel(cer | 0x01, TIMERS_VIRT_BASE + TMR_CER);
  134. }
  135. static struct irqaction timer_irq = {
  136. .name = "timer",
  137. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  138. .handler = timer_interrupt,
  139. .dev_id = &ckevt,
  140. };
  141. void __init timer_init(int irq)
  142. {
  143. timer_config();
  144. init_sched_clock(&cd, mmp_update_sched_clock, 32, CLOCK_TICK_RATE);
  145. ckevt.mult = div_sc(CLOCK_TICK_RATE, NSEC_PER_SEC, ckevt.shift);
  146. ckevt.max_delta_ns = clockevent_delta2ns(MAX_DELTA, &ckevt);
  147. ckevt.min_delta_ns = clockevent_delta2ns(MIN_DELTA, &ckevt);
  148. ckevt.cpumask = cpumask_of(0);
  149. setup_irq(irq, &timer_irq);
  150. clocksource_register_hz(&cksrc, CLOCK_TICK_RATE);
  151. clockevents_register_device(&ckevt);
  152. }