time.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. * which however is currently broken
  29. */
  30. #include <linux/spinlock.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/module.h>
  33. #include <linux/sched.h>
  34. #include <linux/kernel.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. #include <asm/mcip.h>
  46. /* Timer related Aux registers */
  47. #define ARC_REG_TIMER0_LIMIT 0x23 /* timer 0 limit */
  48. #define ARC_REG_TIMER0_CTRL 0x22 /* timer 0 control */
  49. #define ARC_REG_TIMER0_CNT 0x21 /* timer 0 count */
  50. #define ARC_REG_TIMER1_LIMIT 0x102 /* timer 1 limit */
  51. #define ARC_REG_TIMER1_CTRL 0x101 /* timer 1 control */
  52. #define ARC_REG_TIMER1_CNT 0x100 /* timer 1 count */
  53. #define TIMER_CTRL_IE (1 << 0) /* Interupt when Count reachs limit */
  54. #define TIMER_CTRL_NH (1 << 1) /* Count only when CPU NOT halted */
  55. #define ARC_TIMER_MAX 0xFFFFFFFF
  56. /********** Clock Source Device *********/
  57. #ifdef CONFIG_ARC_HAS_GRTC
  58. static int arc_counter_setup(void)
  59. {
  60. return 1;
  61. }
  62. static cycle_t arc_counter_read(struct clocksource *cs)
  63. {
  64. unsigned long flags;
  65. union {
  66. #ifdef CONFIG_CPU_BIG_ENDIAN
  67. struct { u32 h, l; };
  68. #else
  69. struct { u32 l, h; };
  70. #endif
  71. cycle_t full;
  72. } stamp;
  73. local_irq_save(flags);
  74. __mcip_cmd(CMD_GRTC_READ_LO, 0);
  75. stamp.l = read_aux_reg(ARC_REG_MCIP_READBACK);
  76. __mcip_cmd(CMD_GRTC_READ_HI, 0);
  77. stamp.h = read_aux_reg(ARC_REG_MCIP_READBACK);
  78. local_irq_restore(flags);
  79. return stamp.full;
  80. }
  81. static struct clocksource arc_counter = {
  82. .name = "ARConnect GRTC",
  83. .rating = 400,
  84. .read = arc_counter_read,
  85. .mask = CLOCKSOURCE_MASK(64),
  86. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  87. };
  88. #else
  89. #ifdef CONFIG_ARC_HAS_RTC
  90. #define AUX_RTC_CTRL 0x103
  91. #define AUX_RTC_LOW 0x104
  92. #define AUX_RTC_HIGH 0x105
  93. int arc_counter_setup(void)
  94. {
  95. write_aux_reg(AUX_RTC_CTRL, 1);
  96. /* Not usable in SMP */
  97. return !IS_ENABLED(CONFIG_SMP);
  98. }
  99. static cycle_t arc_counter_read(struct clocksource *cs)
  100. {
  101. unsigned long status;
  102. union {
  103. #ifdef CONFIG_CPU_BIG_ENDIAN
  104. struct { u32 high, low; };
  105. #else
  106. struct { u32 low, high; };
  107. #endif
  108. cycle_t full;
  109. } stamp;
  110. /*
  111. * hardware has an internal state machine which tracks readout of
  112. * low/high and updates the CTRL.status if
  113. * - interrupt/exception taken between the two reads
  114. * - high increments after low has been read
  115. */
  116. do {
  117. stamp.low = read_aux_reg(AUX_RTC_LOW);
  118. stamp.high = read_aux_reg(AUX_RTC_HIGH);
  119. status = read_aux_reg(AUX_RTC_CTRL);
  120. } while (!(status & _BITUL(31)));
  121. return stamp.full;
  122. }
  123. static struct clocksource arc_counter = {
  124. .name = "ARCv2 RTC",
  125. .rating = 350,
  126. .read = arc_counter_read,
  127. .mask = CLOCKSOURCE_MASK(64),
  128. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  129. };
  130. #else /* !CONFIG_ARC_HAS_RTC */
  131. /*
  132. * set 32bit TIMER1 to keep counting monotonically and wraparound
  133. */
  134. int arc_counter_setup(void)
  135. {
  136. write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMER_MAX);
  137. write_aux_reg(ARC_REG_TIMER1_CNT, 0);
  138. write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
  139. /* Not usable in SMP */
  140. return !IS_ENABLED(CONFIG_SMP);
  141. }
  142. static cycle_t arc_counter_read(struct clocksource *cs)
  143. {
  144. return (cycle_t) read_aux_reg(ARC_REG_TIMER1_CNT);
  145. }
  146. static struct clocksource arc_counter = {
  147. .name = "ARC Timer1",
  148. .rating = 300,
  149. .read = arc_counter_read,
  150. .mask = CLOCKSOURCE_MASK(32),
  151. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  152. };
  153. #endif
  154. #endif
  155. /********** Clock Event Device *********/
  156. /*
  157. * Arm the timer to interrupt after @cycles
  158. * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
  159. */
  160. static void arc_timer_event_setup(unsigned int cycles)
  161. {
  162. write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles);
  163. write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */
  164. write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH);
  165. }
  166. static int arc_clkevent_set_next_event(unsigned long delta,
  167. struct clock_event_device *dev)
  168. {
  169. arc_timer_event_setup(delta);
  170. return 0;
  171. }
  172. static int arc_clkevent_set_periodic(struct clock_event_device *dev)
  173. {
  174. /*
  175. * At X Hz, 1 sec = 1000ms -> X cycles;
  176. * 10ms -> X / 100 cycles
  177. */
  178. arc_timer_event_setup(arc_get_core_freq() / HZ);
  179. return 0;
  180. }
  181. static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
  182. .name = "ARC Timer0",
  183. .features = CLOCK_EVT_FEAT_ONESHOT |
  184. CLOCK_EVT_FEAT_PERIODIC,
  185. .rating = 300,
  186. .irq = TIMER0_IRQ, /* hardwired, no need for resources */
  187. .set_next_event = arc_clkevent_set_next_event,
  188. .set_state_periodic = arc_clkevent_set_periodic,
  189. };
  190. static irqreturn_t timer_irq_handler(int irq, void *dev_id)
  191. {
  192. /*
  193. * Note that generic IRQ core could have passed @evt for @dev_id if
  194. * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
  195. */
  196. struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
  197. int irq_reenable = clockevent_state_periodic(evt);
  198. /*
  199. * Any write to CTRL reg ACks the interrupt, we rewrite the
  200. * Count when [N]ot [H]alted bit.
  201. * And re-arm it if perioid by [I]nterrupt [E]nable bit
  202. */
  203. write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH);
  204. evt->event_handler(evt);
  205. return IRQ_HANDLED;
  206. }
  207. /*
  208. * Setup the local event timer for @cpu
  209. */
  210. void arc_local_timer_setup()
  211. {
  212. struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
  213. int cpu = smp_processor_id();
  214. evt->cpumask = cpumask_of(cpu);
  215. clockevents_config_and_register(evt, arc_get_core_freq(),
  216. 0, ARC_TIMER_MAX);
  217. /* setup the per-cpu timer IRQ handler - for all cpus */
  218. arc_request_percpu_irq(TIMER0_IRQ, cpu, timer_irq_handler,
  219. "Timer0 (per-cpu-tick)", evt);
  220. }
  221. /*
  222. * Called from start_kernel() - boot CPU only
  223. *
  224. * -Sets up h/w timers as applicable on boot cpu
  225. * -Also sets up any global state needed for timer subsystem:
  226. * - for "counting" timer, registers a clocksource, usable across CPUs
  227. * (provided that underlying counter h/w is synchronized across cores)
  228. * - for "event" timer, sets up TIMER0 IRQ (as that is platform agnostic)
  229. */
  230. void __init time_init(void)
  231. {
  232. /*
  233. * sets up the timekeeping free-flowing counter which also returns
  234. * whether the counter is usable as clocksource
  235. */
  236. if (arc_counter_setup())
  237. /*
  238. * CLK upto 4.29 GHz can be safely represented in 32 bits
  239. * because Max 32 bit number is 4,294,967,295
  240. */
  241. clocksource_register_hz(&arc_counter, arc_get_core_freq());
  242. /* sets up the periodic event timer */
  243. arc_local_timer_setup();
  244. }