time.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/time.h>
  35. #include <linux/init.h>
  36. #include <linux/timex.h>
  37. #include <linux/profile.h>
  38. #include <linux/clocksource.h>
  39. #include <linux/clockchips.h>
  40. #include <asm/irq.h>
  41. #include <asm/arcregs.h>
  42. #include <asm/clk.h>
  43. #include <asm/mach_desc.h>
  44. /* Timer related Aux registers */
  45. #define ARC_REG_TIMER0_LIMIT 0x23 /* timer 0 limit */
  46. #define ARC_REG_TIMER0_CTRL 0x22 /* timer 0 control */
  47. #define ARC_REG_TIMER0_CNT 0x21 /* timer 0 count */
  48. #define ARC_REG_TIMER1_LIMIT 0x102 /* timer 1 limit */
  49. #define ARC_REG_TIMER1_CTRL 0x101 /* timer 1 control */
  50. #define ARC_REG_TIMER1_CNT 0x100 /* timer 1 count */
  51. #define TIMER_CTRL_IE (1 << 0) /* Interupt when Count reachs limit */
  52. #define TIMER_CTRL_NH (1 << 1) /* Count only when CPU NOT halted */
  53. #define ARC_TIMER_MAX 0xFFFFFFFF
  54. /********** Clock Source Device *********/
  55. #ifdef CONFIG_ARC_HAS_RTSC
  56. int arc_counter_setup(void)
  57. {
  58. /*
  59. * For SMP this needs to be 0. However Kconfig glue doesn't
  60. * enable this option for SMP configs
  61. */
  62. return 1;
  63. }
  64. static cycle_t arc_counter_read(struct clocksource *cs)
  65. {
  66. unsigned long flags;
  67. union {
  68. #ifdef CONFIG_CPU_BIG_ENDIAN
  69. struct { u32 high, low; };
  70. #else
  71. struct { u32 low, high; };
  72. #endif
  73. cycle_t full;
  74. } stamp;
  75. flags = arch_local_irq_save();
  76. __asm__ __volatile(
  77. " .extCoreRegister tsch, 58, r, cannot_shortcut \n"
  78. " rtsc %0, 0 \n"
  79. " mov %1, 0 \n"
  80. : "=r" (stamp.low), "=r" (stamp.high));
  81. arch_local_irq_restore(flags);
  82. return stamp.full;
  83. }
  84. static struct clocksource arc_counter = {
  85. .name = "ARC RTSC",
  86. .rating = 300,
  87. .read = arc_counter_read,
  88. .mask = CLOCKSOURCE_MASK(32),
  89. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  90. };
  91. #else /* !CONFIG_ARC_HAS_RTSC */
  92. static bool is_usable_as_clocksource(void)
  93. {
  94. #ifdef CONFIG_SMP
  95. return 0;
  96. #else
  97. return 1;
  98. #endif
  99. }
  100. /*
  101. * set 32bit TIMER1 to keep counting monotonically and wraparound
  102. */
  103. int arc_counter_setup(void)
  104. {
  105. write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMER_MAX);
  106. write_aux_reg(ARC_REG_TIMER1_CNT, 0);
  107. write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
  108. return is_usable_as_clocksource();
  109. }
  110. static cycle_t arc_counter_read(struct clocksource *cs)
  111. {
  112. return (cycle_t) read_aux_reg(ARC_REG_TIMER1_CNT);
  113. }
  114. static struct clocksource arc_counter = {
  115. .name = "ARC Timer1",
  116. .rating = 300,
  117. .read = arc_counter_read,
  118. .mask = CLOCKSOURCE_MASK(32),
  119. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  120. };
  121. #endif
  122. /********** Clock Event Device *********/
  123. /*
  124. * Arm the timer to interrupt after @limit cycles
  125. * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
  126. */
  127. static void arc_timer_event_setup(unsigned int limit)
  128. {
  129. write_aux_reg(ARC_REG_TIMER0_LIMIT, limit);
  130. write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */
  131. write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH);
  132. }
  133. static int arc_clkevent_set_next_event(unsigned long delta,
  134. struct clock_event_device *dev)
  135. {
  136. arc_timer_event_setup(delta);
  137. return 0;
  138. }
  139. static void arc_clkevent_set_mode(enum clock_event_mode mode,
  140. struct clock_event_device *dev)
  141. {
  142. switch (mode) {
  143. case CLOCK_EVT_MODE_PERIODIC:
  144. arc_timer_event_setup(arc_get_core_freq() / HZ);
  145. break;
  146. case CLOCK_EVT_MODE_ONESHOT:
  147. break;
  148. default:
  149. break;
  150. }
  151. return;
  152. }
  153. static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
  154. .name = "ARC Timer0",
  155. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  156. .mode = CLOCK_EVT_MODE_UNUSED,
  157. .rating = 300,
  158. .irq = TIMER0_IRQ, /* hardwired, no need for resources */
  159. .set_next_event = arc_clkevent_set_next_event,
  160. .set_mode = arc_clkevent_set_mode,
  161. };
  162. static irqreturn_t timer_irq_handler(int irq, void *dev_id)
  163. {
  164. /*
  165. * Note that generic IRQ core could have passed @evt for @dev_id if
  166. * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
  167. */
  168. struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
  169. int irq_reenable = evt->mode == CLOCK_EVT_MODE_PERIODIC;
  170. /*
  171. * Any write to CTRL reg ACks the interrupt, we rewrite the
  172. * Count when [N]ot [H]alted bit.
  173. * And re-arm it if perioid by [I]nterrupt [E]nable bit
  174. */
  175. write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH);
  176. evt->event_handler(evt);
  177. return IRQ_HANDLED;
  178. }
  179. static struct irqaction arc_timer_irq = {
  180. .name = "Timer0 (clock-evt-dev)",
  181. .flags = IRQF_TIMER | IRQF_PERCPU,
  182. .handler = timer_irq_handler,
  183. };
  184. /*
  185. * Setup the local event timer for @cpu
  186. */
  187. void arc_local_timer_setup()
  188. {
  189. struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
  190. int cpu = smp_processor_id();
  191. evt->cpumask = cpumask_of(cpu);
  192. clockevents_config_and_register(evt, arc_get_core_freq(),
  193. 0, ARC_TIMER_MAX);
  194. /*
  195. * setup the per-cpu timer IRQ handler - for all cpus
  196. * For non boot CPU explicitly unmask at intc
  197. * setup_irq() -> .. -> irq_startup() already does this on boot-cpu
  198. */
  199. if (!cpu)
  200. setup_irq(TIMER0_IRQ, &arc_timer_irq);
  201. else
  202. arch_unmask_irq(TIMER0_IRQ);
  203. }
  204. /*
  205. * Called from start_kernel() - boot CPU only
  206. *
  207. * -Sets up h/w timers as applicable on boot cpu
  208. * -Also sets up any global state needed for timer subsystem:
  209. * - for "counting" timer, registers a clocksource, usable across CPUs
  210. * (provided that underlying counter h/w is synchronized across cores)
  211. * - for "event" timer, sets up TIMER0 IRQ (as that is platform agnostic)
  212. */
  213. void __init time_init(void)
  214. {
  215. /*
  216. * sets up the timekeeping free-flowing counter which also returns
  217. * whether the counter is usable as clocksource
  218. */
  219. if (arc_counter_setup())
  220. /*
  221. * CLK upto 4.29 GHz can be safely represented in 32 bits
  222. * because Max 32 bit number is 4,294,967,295
  223. */
  224. clocksource_register_hz(&arc_counter, arc_get_core_freq());
  225. /* sets up the periodic event timer */
  226. arc_local_timer_setup();
  227. if (machine_desc->init_time)
  228. machine_desc->init_time();
  229. }