timer.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. * Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/clocksource.h>
  17. #include <linux/clockchips.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/io.h>
  22. #include <asm/mach/time.h>
  23. #include <asm/hardware/gic.h>
  24. #include <asm/localtimer.h>
  25. #include <mach/msm_iomap.h>
  26. #include <mach/cpu.h>
  27. #include <mach/board.h>
  28. #define TIMER_MATCH_VAL 0x0000
  29. #define TIMER_COUNT_VAL 0x0004
  30. #define TIMER_ENABLE 0x0008
  31. #define TIMER_ENABLE_CLR_ON_MATCH_EN BIT(1)
  32. #define TIMER_ENABLE_EN BIT(0)
  33. #define TIMER_CLEAR 0x000C
  34. #define DGT_CLK_CTL 0x0034
  35. #define DGT_CLK_CTL_DIV_4 0x3
  36. #define GPT_HZ 32768
  37. #define MSM_GLOBAL_TIMER MSM_CLOCK_GPT
  38. /* TODO: Remove these ifdefs */
  39. #if defined(CONFIG_ARCH_QSD8X50)
  40. #define DGT_HZ (19200000 / 4) /* 19.2 MHz / 4 by default */
  41. #define MSM_DGT_SHIFT (0)
  42. #elif defined(CONFIG_ARCH_MSM7X30)
  43. #define DGT_HZ (24576000 / 4) /* 24.576 MHz (LPXO) / 4 by default */
  44. #define MSM_DGT_SHIFT (0)
  45. #elif defined(CONFIG_ARCH_MSM8X60) || defined(CONFIG_ARCH_MSM8960)
  46. #define DGT_HZ (27000000 / 4) /* 27 MHz (PXO) / 4 by default */
  47. #define MSM_DGT_SHIFT (0)
  48. #else
  49. #define DGT_HZ 19200000 /* 19.2 MHz or 600 KHz after shift */
  50. #define MSM_DGT_SHIFT (5)
  51. #endif
  52. struct msm_clock {
  53. struct clock_event_device clockevent;
  54. struct clocksource clocksource;
  55. unsigned int irq;
  56. void __iomem *regbase;
  57. uint32_t freq;
  58. uint32_t shift;
  59. void __iomem *global_counter;
  60. void __iomem *local_counter;
  61. union {
  62. struct clock_event_device *evt;
  63. struct clock_event_device __percpu **percpu_evt;
  64. };
  65. };
  66. enum {
  67. MSM_CLOCK_GPT,
  68. MSM_CLOCK_DGT,
  69. NR_TIMERS,
  70. };
  71. static struct msm_clock msm_clocks[];
  72. static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt);
  73. static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
  74. {
  75. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  76. if (evt->event_handler == NULL)
  77. return IRQ_HANDLED;
  78. /* Stop the timer tick */
  79. if (evt->mode == CLOCK_EVT_MODE_ONESHOT) {
  80. struct msm_clock *clock = clockevent_to_clock(evt);
  81. u32 ctrl = readl_relaxed(clock->regbase + TIMER_ENABLE);
  82. ctrl &= ~TIMER_ENABLE_EN;
  83. writel_relaxed(ctrl, clock->regbase + TIMER_ENABLE);
  84. }
  85. evt->event_handler(evt);
  86. return IRQ_HANDLED;
  87. }
  88. static cycle_t msm_read_timer_count(struct clocksource *cs)
  89. {
  90. struct msm_clock *clk = container_of(cs, struct msm_clock, clocksource);
  91. /*
  92. * Shift timer count down by a constant due to unreliable lower bits
  93. * on some targets.
  94. */
  95. return readl(clk->global_counter) >> clk->shift;
  96. }
  97. static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt)
  98. {
  99. #ifdef CONFIG_SMP
  100. int i;
  101. for (i = 0; i < NR_TIMERS; i++)
  102. if (evt == &(msm_clocks[i].clockevent))
  103. return &msm_clocks[i];
  104. return &msm_clocks[MSM_GLOBAL_TIMER];
  105. #else
  106. return container_of(evt, struct msm_clock, clockevent);
  107. #endif
  108. }
  109. static int msm_timer_set_next_event(unsigned long cycles,
  110. struct clock_event_device *evt)
  111. {
  112. struct msm_clock *clock = clockevent_to_clock(evt);
  113. u32 match = cycles << clock->shift;
  114. u32 ctrl = readl_relaxed(clock->regbase + TIMER_ENABLE);
  115. writel_relaxed(0, clock->regbase + TIMER_CLEAR);
  116. writel_relaxed(match, clock->regbase + TIMER_MATCH_VAL);
  117. writel_relaxed(ctrl | TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
  118. return 0;
  119. }
  120. static void msm_timer_set_mode(enum clock_event_mode mode,
  121. struct clock_event_device *evt)
  122. {
  123. struct msm_clock *clock = clockevent_to_clock(evt);
  124. u32 ctrl;
  125. ctrl = readl_relaxed(clock->regbase + TIMER_ENABLE);
  126. ctrl &= ~(TIMER_ENABLE_EN | TIMER_ENABLE_CLR_ON_MATCH_EN);
  127. switch (mode) {
  128. case CLOCK_EVT_MODE_RESUME:
  129. case CLOCK_EVT_MODE_PERIODIC:
  130. break;
  131. case CLOCK_EVT_MODE_ONESHOT:
  132. /* Timer is enabled in set_next_event */
  133. break;
  134. case CLOCK_EVT_MODE_UNUSED:
  135. case CLOCK_EVT_MODE_SHUTDOWN:
  136. break;
  137. }
  138. writel_relaxed(ctrl, clock->regbase + TIMER_ENABLE);
  139. }
  140. static struct msm_clock msm_clocks[] = {
  141. [MSM_CLOCK_GPT] = {
  142. .clockevent = {
  143. .name = "gp_timer",
  144. .features = CLOCK_EVT_FEAT_ONESHOT,
  145. .shift = 32,
  146. .rating = 200,
  147. .set_next_event = msm_timer_set_next_event,
  148. .set_mode = msm_timer_set_mode,
  149. },
  150. .irq = INT_GP_TIMER_EXP,
  151. .freq = GPT_HZ,
  152. },
  153. [MSM_CLOCK_DGT] = {
  154. .clocksource = {
  155. .name = "dg_timer",
  156. .rating = 300,
  157. .read = msm_read_timer_count,
  158. .mask = CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT)),
  159. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  160. },
  161. .freq = DGT_HZ >> MSM_DGT_SHIFT,
  162. .shift = MSM_DGT_SHIFT,
  163. }
  164. };
  165. static void __init msm_timer_init(void)
  166. {
  167. struct msm_clock *clock;
  168. struct clock_event_device *ce = &msm_clocks[MSM_CLOCK_GPT].clockevent;
  169. struct clocksource *cs = &msm_clocks[MSM_CLOCK_DGT].clocksource;
  170. int res;
  171. int global_offset = 0;
  172. if (cpu_is_msm7x01()) {
  173. msm_clocks[MSM_CLOCK_GPT].regbase = MSM_CSR_BASE;
  174. msm_clocks[MSM_CLOCK_DGT].regbase = MSM_CSR_BASE + 0x10;
  175. } else if (cpu_is_msm7x30()) {
  176. msm_clocks[MSM_CLOCK_GPT].regbase = MSM_CSR_BASE + 0x04;
  177. msm_clocks[MSM_CLOCK_DGT].regbase = MSM_CSR_BASE + 0x24;
  178. } else if (cpu_is_qsd8x50()) {
  179. msm_clocks[MSM_CLOCK_GPT].regbase = MSM_CSR_BASE;
  180. msm_clocks[MSM_CLOCK_DGT].regbase = MSM_CSR_BASE + 0x10;
  181. } else if (cpu_is_msm8x60() || cpu_is_msm8960()) {
  182. msm_clocks[MSM_CLOCK_GPT].regbase = MSM_TMR_BASE + 0x04;
  183. msm_clocks[MSM_CLOCK_DGT].regbase = MSM_TMR_BASE + 0x24;
  184. /* Use CPU0's timer as the global timer. */
  185. global_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
  186. } else
  187. BUG();
  188. #ifdef CONFIG_ARCH_MSM_SCORPIONMP
  189. writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
  190. #endif
  191. clock = &msm_clocks[MSM_CLOCK_GPT];
  192. clock->local_counter = clock->regbase + TIMER_COUNT_VAL;
  193. writel_relaxed(0, clock->regbase + TIMER_ENABLE);
  194. writel_relaxed(0, clock->regbase + TIMER_CLEAR);
  195. writel_relaxed(~0, clock->regbase + TIMER_MATCH_VAL);
  196. ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift);
  197. /*
  198. * allow at least 10 seconds to notice that the timer
  199. * wrapped
  200. */
  201. ce->max_delta_ns =
  202. clockevent_delta2ns(0xf0000000 >> clock->shift, ce);
  203. /* 4 gets rounded down to 3 */
  204. ce->min_delta_ns = clockevent_delta2ns(4, ce);
  205. ce->cpumask = cpumask_of(0);
  206. ce->irq = clock->irq;
  207. if (cpu_is_msm8x60() || cpu_is_msm8960()) {
  208. clock->percpu_evt = alloc_percpu(struct clock_event_device *);
  209. if (!clock->percpu_evt) {
  210. pr_err("memory allocation failed for %s\n", ce->name);
  211. goto err;
  212. }
  213. *__this_cpu_ptr(clock->percpu_evt) = ce;
  214. res = request_percpu_irq(ce->irq, msm_timer_interrupt,
  215. ce->name, clock->percpu_evt);
  216. if (!res)
  217. enable_percpu_irq(ce->irq, 0);
  218. } else {
  219. clock->evt = ce;
  220. res = request_irq(ce->irq, msm_timer_interrupt,
  221. IRQF_TIMER | IRQF_NOBALANCING |
  222. IRQF_TRIGGER_RISING, ce->name, &clock->evt);
  223. }
  224. if (res)
  225. pr_err("request_irq failed for %s\n", ce->name);
  226. clockevents_register_device(ce);
  227. err:
  228. clock = &msm_clocks[MSM_CLOCK_DGT];
  229. clock->local_counter = clock->regbase + TIMER_COUNT_VAL;
  230. clock->global_counter = clock->local_counter + global_offset;
  231. writel_relaxed(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
  232. res = clocksource_register_hz(cs, clock->freq);
  233. if (res)
  234. pr_err("clocksource_register failed for %s\n", cs->name);
  235. }
  236. #ifdef CONFIG_LOCAL_TIMERS
  237. int __cpuinit local_timer_setup(struct clock_event_device *evt)
  238. {
  239. static bool local_timer_inited;
  240. struct msm_clock *clock = &msm_clocks[MSM_GLOBAL_TIMER];
  241. /* Use existing clock_event for cpu 0 */
  242. if (!smp_processor_id())
  243. return 0;
  244. if (!local_timer_inited) {
  245. writel(0, clock->regbase + TIMER_ENABLE);
  246. writel(0, clock->regbase + TIMER_CLEAR);
  247. writel(~0, clock->regbase + TIMER_MATCH_VAL);
  248. local_timer_inited = true;
  249. }
  250. evt->irq = clock->irq;
  251. evt->name = "local_timer";
  252. evt->features = CLOCK_EVT_FEAT_ONESHOT;
  253. evt->rating = clock->clockevent.rating;
  254. evt->set_mode = msm_timer_set_mode;
  255. evt->set_next_event = msm_timer_set_next_event;
  256. evt->shift = clock->clockevent.shift;
  257. evt->mult = div_sc(clock->freq, NSEC_PER_SEC, evt->shift);
  258. evt->max_delta_ns =
  259. clockevent_delta2ns(0xf0000000 >> clock->shift, evt);
  260. evt->min_delta_ns = clockevent_delta2ns(4, evt);
  261. *__this_cpu_ptr(clock->percpu_evt) = evt;
  262. enable_percpu_irq(evt->irq, 0);
  263. clockevents_register_device(evt);
  264. return 0;
  265. }
  266. void local_timer_stop(struct clock_event_device *evt)
  267. {
  268. evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
  269. disable_percpu_irq(evt->irq);
  270. }
  271. #endif /* CONFIG_LOCAL_TIMERS */
  272. struct sys_timer msm_timer = {
  273. .init = msm_timer_init
  274. };