time.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * vineetg: Jan 1011
  9. * -sched_clock( ) no longer jiffies based. Uses the same clocksource
  10. * as gtod
  11. *
  12. * Rajeshwarr/Vineetg: Mar 2008
  13. * -Implemented CONFIG_GENERIC_TIME (rather deleted arch specific code)
  14. * for arch independent gettimeofday()
  15. * -Implemented CONFIG_GENERIC_CLOCKEVENTS as base for hrtimers
  16. *
  17. * Vineetg: Mar 2008: Forked off from time.c which now is time-jiff.c
  18. */
  19. /* ARC700 has two 32bit independent prog Timers: TIMER0 and TIMER1
  20. * Each can programmed to go from @count to @limit and optionally
  21. * interrupt when that happens.
  22. * A write to Control Register clears the Interrupt
  23. *
  24. * We've designated TIMER0 for events (clockevents)
  25. * while TIMER1 for free running (clocksource)
  26. *
  27. * Newer ARC700 cores have 64bit clk fetching RTSC insn, preferred over TIMER1
  28. */
  29. #include <linux/spinlock.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/module.h>
  32. #include <linux/sched.h>
  33. #include <linux/kernel.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/time.h>
  36. #include <linux/init.h>
  37. #include <linux/timex.h>
  38. #include <linux/profile.h>
  39. #include <linux/clocksource.h>
  40. #include <linux/clockchips.h>
  41. #include <asm/irq.h>
  42. #include <asm/arcregs.h>
  43. #include <asm/clk.h>
  44. #include <asm/mach_desc.h>
  45. #define ARC_TIMER_MAX 0xFFFFFFFF
  46. /********** Clock Source Device *********/
  47. #ifdef CONFIG_ARC_HAS_RTSC
  48. int __cpuinit arc_counter_setup(void)
  49. {
  50. /* RTSC insn taps into cpu clk, needs no setup */
  51. /* For SMP, only allowed if cross-core-sync, hence usable as cs */
  52. return 1;
  53. }
  54. static cycle_t arc_counter_read(struct clocksource *cs)
  55. {
  56. unsigned long flags;
  57. union {
  58. #ifdef CONFIG_CPU_BIG_ENDIAN
  59. struct { u32 high, low; };
  60. #else
  61. struct { u32 low, high; };
  62. #endif
  63. cycle_t full;
  64. } stamp;
  65. flags = arch_local_irq_save();
  66. __asm__ __volatile(
  67. " .extCoreRegister tsch, 58, r, cannot_shortcut \n"
  68. " rtsc %0, 0 \n"
  69. " mov %1, 0 \n"
  70. : "=r" (stamp.low), "=r" (stamp.high));
  71. arch_local_irq_restore(flags);
  72. return stamp.full;
  73. }
  74. static struct clocksource arc_counter = {
  75. .name = "ARC RTSC",
  76. .rating = 300,
  77. .read = arc_counter_read,
  78. .mask = CLOCKSOURCE_MASK(32),
  79. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  80. };
  81. #else /* !CONFIG_ARC_HAS_RTSC */
  82. static bool is_usable_as_clocksource(void)
  83. {
  84. #ifdef CONFIG_SMP
  85. return 0;
  86. #else
  87. return 1;
  88. #endif
  89. }
  90. /*
  91. * set 32bit TIMER1 to keep counting monotonically and wraparound
  92. */
  93. int __cpuinit arc_counter_setup(void)
  94. {
  95. write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMER_MAX);
  96. write_aux_reg(ARC_REG_TIMER1_CNT, 0);
  97. write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
  98. return is_usable_as_clocksource();
  99. }
  100. static cycle_t arc_counter_read(struct clocksource *cs)
  101. {
  102. return (cycle_t) read_aux_reg(ARC_REG_TIMER1_CNT);
  103. }
  104. static struct clocksource arc_counter = {
  105. .name = "ARC Timer1",
  106. .rating = 300,
  107. .read = arc_counter_read,
  108. .mask = CLOCKSOURCE_MASK(32),
  109. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  110. };
  111. #endif
  112. /********** Clock Event Device *********/
  113. /*
  114. * Arm the timer to interrupt after @limit cycles
  115. * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
  116. */
  117. static void arc_timer_event_setup(unsigned int limit)
  118. {
  119. write_aux_reg(ARC_REG_TIMER0_LIMIT, limit);
  120. write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */
  121. write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH);
  122. }
  123. /*
  124. * Acknowledge the interrupt (oneshot) and optionally re-arm it (periodic)
  125. * -Any write to CTRL Reg will ack the intr (NH bit: Count when not halted)
  126. * -Rearming is done by setting the IE bit
  127. *
  128. * Small optimisation: Normal code would have been
  129. * if (irq_reenable)
  130. * CTRL_REG = (IE | NH);
  131. * else
  132. * CTRL_REG = NH;
  133. * However since IE is BIT0 we can fold the branch
  134. */
  135. static void arc_timer_event_ack(unsigned int irq_reenable)
  136. {
  137. write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH);
  138. }
  139. static int arc_clkevent_set_next_event(unsigned long delta,
  140. struct clock_event_device *dev)
  141. {
  142. arc_timer_event_setup(delta);
  143. return 0;
  144. }
  145. static void arc_clkevent_set_mode(enum clock_event_mode mode,
  146. struct clock_event_device *dev)
  147. {
  148. switch (mode) {
  149. case CLOCK_EVT_MODE_PERIODIC:
  150. arc_timer_event_setup(arc_get_core_freq() / HZ);
  151. break;
  152. case CLOCK_EVT_MODE_ONESHOT:
  153. break;
  154. default:
  155. break;
  156. }
  157. return;
  158. }
  159. static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
  160. .name = "ARC Timer0",
  161. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  162. .mode = CLOCK_EVT_MODE_UNUSED,
  163. .rating = 300,
  164. .irq = TIMER0_IRQ, /* hardwired, no need for resources */
  165. .set_next_event = arc_clkevent_set_next_event,
  166. .set_mode = arc_clkevent_set_mode,
  167. };
  168. static irqreturn_t timer_irq_handler(int irq, void *dev_id)
  169. {
  170. struct clock_event_device *clk = &__get_cpu_var(arc_clockevent_device);
  171. arc_timer_event_ack(clk->mode == CLOCK_EVT_MODE_PERIODIC);
  172. clk->event_handler(clk);
  173. return IRQ_HANDLED;
  174. }
  175. static struct irqaction arc_timer_irq = {
  176. .name = "Timer0 (clock-evt-dev)",
  177. .flags = IRQF_TIMER | IRQF_PERCPU,
  178. .handler = timer_irq_handler,
  179. };
  180. /*
  181. * Setup the local event timer for @cpu
  182. * N.B. weak so that some exotic ARC SoCs can completely override it
  183. */
  184. void __attribute__((weak)) __cpuinit arc_local_timer_setup(unsigned int cpu)
  185. {
  186. struct clock_event_device *clk = &per_cpu(arc_clockevent_device, cpu);
  187. clockevents_calc_mult_shift(clk, arc_get_core_freq(), 5);
  188. clk->max_delta_ns = clockevent_delta2ns(ARC_TIMER_MAX, clk);
  189. clk->cpumask = cpumask_of(cpu);
  190. clockevents_register_device(clk);
  191. /*
  192. * setup the per-cpu timer IRQ handler - for all cpus
  193. * For non boot CPU explicitly unmask at intc
  194. * setup_irq() -> .. -> irq_startup() already does this on boot-cpu
  195. */
  196. if (!cpu)
  197. setup_irq(TIMER0_IRQ, &arc_timer_irq);
  198. else
  199. arch_unmask_irq(TIMER0_IRQ);
  200. }
  201. /*
  202. * Called from start_kernel() - boot CPU only
  203. *
  204. * -Sets up h/w timers as applicable on boot cpu
  205. * -Also sets up any global state needed for timer subsystem:
  206. * - for "counting" timer, registers a clocksource, usable across CPUs
  207. * (provided that underlying counter h/w is synchronized across cores)
  208. * - for "event" timer, sets up TIMER0 IRQ (as that is platform agnostic)
  209. */
  210. void __init time_init(void)
  211. {
  212. /*
  213. * sets up the timekeeping free-flowing counter which also returns
  214. * whether the counter is usable as clocksource
  215. */
  216. if (arc_counter_setup())
  217. /*
  218. * CLK upto 4.29 GHz can be safely represented in 32 bits
  219. * because Max 32 bit number is 4,294,967,295
  220. */
  221. clocksource_register_hz(&arc_counter, arc_get_core_freq());
  222. /* sets up the periodic event timer */
  223. arc_local_timer_setup(smp_processor_id());
  224. if (machine_desc->init_time)
  225. machine_desc->init_time();
  226. }