PageRenderTime 132ms CodeModel.GetById 71ms RepoModel.GetById 4ms app.codeStats 0ms

/arch/arm/mach-mmp/time.c

https://bitbucket.org/sammyz/iscream_thunderc-2.6.35-rebase
C | 223 lines | 142 code | 45 blank | 36 comment | 3 complexity | 8527837c433016a1d6b012dfa05d5eac MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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/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. #define TCR2NS_SCALE_FACTOR 10
  39. static unsigned long tcr2ns_scale;
  40. static void __init set_tcr2ns_scale(unsigned long tcr_rate)
  41. {
  42. unsigned long long v = 1000000000ULL << TCR2NS_SCALE_FACTOR;
  43. do_div(v, tcr_rate);
  44. tcr2ns_scale = v;
  45. /*
  46. * We want an even value to automatically clear the top bit
  47. * returned by cnt32_to_63() without an additional run time
  48. * instruction. So if the LSB is 1 then round it up.
  49. */
  50. if (tcr2ns_scale & 1)
  51. tcr2ns_scale++;
  52. }
  53. /*
  54. * FIXME: the timer needs some delay to stablize the counter capture
  55. */
  56. static inline uint32_t timer_read(void)
  57. {
  58. int delay = 100;
  59. __raw_writel(1, TIMERS_VIRT_BASE + TMR_CVWR(0));
  60. while (delay--)
  61. cpu_relax();
  62. return __raw_readl(TIMERS_VIRT_BASE + TMR_CVWR(0));
  63. }
  64. unsigned long long sched_clock(void)
  65. {
  66. unsigned long long v = cnt32_to_63(timer_read());
  67. return (v * tcr2ns_scale) >> TCR2NS_SCALE_FACTOR;
  68. }
  69. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  70. {
  71. struct clock_event_device *c = dev_id;
  72. /* disable and clear pending interrupt status */
  73. __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_IER(0));
  74. __raw_writel(0x1, TIMERS_VIRT_BASE + TMR_ICR(0));
  75. c->event_handler(c);
  76. return IRQ_HANDLED;
  77. }
  78. static int timer_set_next_event(unsigned long delta,
  79. struct clock_event_device *dev)
  80. {
  81. unsigned long flags, next;
  82. local_irq_save(flags);
  83. /* clear pending interrupt status and enable */
  84. __raw_writel(0x01, TIMERS_VIRT_BASE + TMR_ICR(0));
  85. __raw_writel(0x01, TIMERS_VIRT_BASE + TMR_IER(0));
  86. next = timer_read() + delta;
  87. __raw_writel(next, TIMERS_VIRT_BASE + TMR_TN_MM(0, 0));
  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. .shift = 20,
  124. .rating = 200,
  125. .read = clksrc_read,
  126. .mask = CLOCKSOURCE_MASK(32),
  127. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  128. };
  129. static void __init timer_config(void)
  130. {
  131. uint32_t ccr = __raw_readl(TIMERS_VIRT_BASE + TMR_CCR);
  132. uint32_t cer = __raw_readl(TIMERS_VIRT_BASE + TMR_CER);
  133. uint32_t cmr = __raw_readl(TIMERS_VIRT_BASE + TMR_CMR);
  134. __raw_writel(cer & ~0x1, TIMERS_VIRT_BASE + TMR_CER); /* disable */
  135. ccr &= (cpu_is_mmp2()) ? TMR_CCR_CS_0(0) : TMR_CCR_CS_0(3);
  136. __raw_writel(ccr, TIMERS_VIRT_BASE + TMR_CCR);
  137. /* free-running mode */
  138. __raw_writel(cmr | 0x01, TIMERS_VIRT_BASE + TMR_CMR);
  139. __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_PLCR(0)); /* free-running */
  140. __raw_writel(0x7, TIMERS_VIRT_BASE + TMR_ICR(0)); /* clear status */
  141. __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_IER(0));
  142. /* enable timer counter */
  143. __raw_writel(cer | 0x01, TIMERS_VIRT_BASE + TMR_CER);
  144. }
  145. static struct irqaction timer_irq = {
  146. .name = "timer",
  147. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  148. .handler = timer_interrupt,
  149. .dev_id = &ckevt,
  150. };
  151. void __init timer_init(int irq)
  152. {
  153. timer_config();
  154. set_tcr2ns_scale(CLOCK_TICK_RATE);
  155. ckevt.mult = div_sc(CLOCK_TICK_RATE, NSEC_PER_SEC, ckevt.shift);
  156. ckevt.max_delta_ns = clockevent_delta2ns(MAX_DELTA, &ckevt);
  157. ckevt.min_delta_ns = clockevent_delta2ns(MIN_DELTA, &ckevt);
  158. ckevt.cpumask = cpumask_of(0);
  159. cksrc.mult = clocksource_hz2mult(CLOCK_TICK_RATE, cksrc.shift);
  160. setup_irq(irq, &timer_irq);
  161. clocksource_register(&cksrc);
  162. clockevents_register_device(&ckevt);
  163. }
  164. static void __init mmp2_timer_init(void)
  165. {
  166. unsigned long clk_rst;
  167. __raw_writel(APBC_APBCLK | APBC_RST, APBC_MMP2_TIMERS);
  168. /*
  169. * enable bus/functional clock, enable 6.5MHz (divider 4),
  170. * release reset
  171. */
  172. clk_rst = APBC_APBCLK | APBC_FNCLK | APBC_FNCLKSEL(1);
  173. __raw_writel(clk_rst, APBC_MMP2_TIMERS);
  174. timer_init(IRQ_MMP2_TIMER1);
  175. }
  176. struct sys_timer mmp2_timer = {
  177. .init = mmp2_timer_init,
  178. };