localtimer.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * linux/arch/arm/mach-realview/localtimer.c
  3. *
  4. * Copyright (C) 2002 ARM Ltd.
  5. * All Rights Reserved
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/delay.h>
  14. #include <linux/device.h>
  15. #include <linux/smp.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/percpu.h>
  18. #include <linux/clockchips.h>
  19. #include <linux/irq.h>
  20. #include <linux/io.h>
  21. #include <asm/hardware/arm_twd.h>
  22. #include <asm/hardware/gic.h>
  23. #include <mach/hardware.h>
  24. #include <asm/irq.h>
  25. static DEFINE_PER_CPU(struct clock_event_device, local_clockevent);
  26. /*
  27. * Used on SMP for either the local timer or IPI_TIMER
  28. */
  29. void local_timer_interrupt(void)
  30. {
  31. struct clock_event_device *clk = &__get_cpu_var(local_clockevent);
  32. clk->event_handler(clk);
  33. }
  34. #ifdef CONFIG_LOCAL_TIMERS
  35. #define TWD_BASE(cpu) (twd_base_addr + (cpu) * twd_size)
  36. /* set up by the platform code */
  37. void __iomem *twd_base_addr;
  38. unsigned int twd_size;
  39. static unsigned long mpcore_timer_rate;
  40. static void local_timer_set_mode(enum clock_event_mode mode,
  41. struct clock_event_device *clk)
  42. {
  43. void __iomem *base = TWD_BASE(smp_processor_id());
  44. unsigned long ctrl;
  45. switch(mode) {
  46. case CLOCK_EVT_MODE_PERIODIC:
  47. /* timer load already set up */
  48. ctrl = TWD_TIMER_CONTROL_ENABLE | TWD_TIMER_CONTROL_IT_ENABLE
  49. | TWD_TIMER_CONTROL_PERIODIC;
  50. break;
  51. case CLOCK_EVT_MODE_ONESHOT:
  52. /* period set, and timer enabled in 'next_event' hook */
  53. ctrl = TWD_TIMER_CONTROL_IT_ENABLE | TWD_TIMER_CONTROL_ONESHOT;
  54. break;
  55. case CLOCK_EVT_MODE_UNUSED:
  56. case CLOCK_EVT_MODE_SHUTDOWN:
  57. default:
  58. ctrl = 0;
  59. }
  60. __raw_writel(ctrl, base + TWD_TIMER_CONTROL);
  61. }
  62. static int local_timer_set_next_event(unsigned long evt,
  63. struct clock_event_device *unused)
  64. {
  65. void __iomem *base = TWD_BASE(smp_processor_id());
  66. unsigned long ctrl = __raw_readl(base + TWD_TIMER_CONTROL);
  67. __raw_writel(evt, base + TWD_TIMER_COUNTER);
  68. __raw_writel(ctrl | TWD_TIMER_CONTROL_ENABLE, base + TWD_TIMER_CONTROL);
  69. return 0;
  70. }
  71. /*
  72. * local_timer_ack: checks for a local timer interrupt.
  73. *
  74. * If a local timer interrupt has occurred, acknowledge and return 1.
  75. * Otherwise, return 0.
  76. */
  77. int local_timer_ack(void)
  78. {
  79. void __iomem *base = TWD_BASE(smp_processor_id());
  80. if (__raw_readl(base + TWD_TIMER_INTSTAT)) {
  81. __raw_writel(1, base + TWD_TIMER_INTSTAT);
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. static void __cpuinit twd_calibrate_rate(unsigned int cpu)
  87. {
  88. void __iomem *base = TWD_BASE(cpu);
  89. unsigned long load, count;
  90. u64 waitjiffies;
  91. /*
  92. * If this is the first time round, we need to work out how fast
  93. * the timer ticks
  94. */
  95. if (mpcore_timer_rate == 0) {
  96. printk("Calibrating local timer... ");
  97. /* Wait for a tick to start */
  98. waitjiffies = get_jiffies_64() + 1;
  99. while (get_jiffies_64() < waitjiffies)
  100. udelay(10);
  101. /* OK, now the tick has started, let's get the timer going */
  102. waitjiffies += 5;
  103. /* enable, no interrupt or reload */
  104. __raw_writel(0x1, base + TWD_TIMER_CONTROL);
  105. /* maximum value */
  106. __raw_writel(0xFFFFFFFFU, base + TWD_TIMER_COUNTER);
  107. while (get_jiffies_64() < waitjiffies)
  108. udelay(10);
  109. count = __raw_readl(base + TWD_TIMER_COUNTER);
  110. mpcore_timer_rate = (0xFFFFFFFFU - count) * (HZ / 5);
  111. printk("%lu.%02luMHz.\n", mpcore_timer_rate / 1000000,
  112. (mpcore_timer_rate / 100000) % 100);
  113. }
  114. load = mpcore_timer_rate / HZ;
  115. __raw_writel(load, base + TWD_TIMER_LOAD);
  116. }
  117. /*
  118. * Setup the local clock events for a CPU.
  119. */
  120. void __cpuinit local_timer_setup(unsigned int cpu)
  121. {
  122. struct clock_event_device *clk = &per_cpu(local_clockevent, cpu);
  123. unsigned long flags;
  124. twd_calibrate_rate(cpu);
  125. clk->name = "local_timer";
  126. clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  127. clk->rating = 350;
  128. clk->set_mode = local_timer_set_mode;
  129. clk->set_next_event = local_timer_set_next_event;
  130. clk->irq = IRQ_LOCALTIMER;
  131. clk->cpumask = cpumask_of_cpu(cpu);
  132. clk->shift = 20;
  133. clk->mult = div_sc(mpcore_timer_rate, NSEC_PER_SEC, clk->shift);
  134. clk->max_delta_ns = clockevent_delta2ns(0xffffffff, clk);
  135. clk->min_delta_ns = clockevent_delta2ns(0xf, clk);
  136. /* Make sure our local interrupt controller has this enabled */
  137. local_irq_save(flags);
  138. get_irq_chip(IRQ_LOCALTIMER)->unmask(IRQ_LOCALTIMER);
  139. local_irq_restore(flags);
  140. clockevents_register_device(clk);
  141. }
  142. /*
  143. * take a local timer down
  144. */
  145. void __cpuexit local_timer_stop(unsigned int cpu)
  146. {
  147. __raw_writel(0, TWD_BASE(cpu) + TWD_TIMER_CONTROL);
  148. }
  149. #else /* CONFIG_LOCAL_TIMERS */
  150. static void dummy_timer_set_mode(enum clock_event_mode mode,
  151. struct clock_event_device *clk)
  152. {
  153. }
  154. void __cpuinit local_timer_setup(unsigned int cpu)
  155. {
  156. struct clock_event_device *clk = &per_cpu(local_clockevent, cpu);
  157. clk->name = "dummy_timer";
  158. clk->features = CLOCK_EVT_FEAT_DUMMY;
  159. clk->rating = 200;
  160. clk->set_mode = dummy_timer_set_mode;
  161. clk->broadcast = smp_timer_broadcast;
  162. clk->cpumask = cpumask_of_cpu(cpu);
  163. clockevents_register_device(clk);
  164. }
  165. #endif /* !CONFIG_LOCAL_TIMERS */