timer.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_DGT_SHIFT 5
  38. static void __iomem *event_base;
  39. static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
  40. {
  41. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  42. /* Stop the timer tick */
  43. if (evt->mode == CLOCK_EVT_MODE_ONESHOT) {
  44. u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  45. ctrl &= ~TIMER_ENABLE_EN;
  46. writel_relaxed(ctrl, event_base + TIMER_ENABLE);
  47. }
  48. evt->event_handler(evt);
  49. return IRQ_HANDLED;
  50. }
  51. static int msm_timer_set_next_event(unsigned long cycles,
  52. struct clock_event_device *evt)
  53. {
  54. u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  55. writel_relaxed(0, event_base + TIMER_CLEAR);
  56. writel_relaxed(cycles, event_base + TIMER_MATCH_VAL);
  57. writel_relaxed(ctrl | TIMER_ENABLE_EN, event_base + TIMER_ENABLE);
  58. return 0;
  59. }
  60. static void msm_timer_set_mode(enum clock_event_mode mode,
  61. struct clock_event_device *evt)
  62. {
  63. u32 ctrl;
  64. ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  65. ctrl &= ~(TIMER_ENABLE_EN | TIMER_ENABLE_CLR_ON_MATCH_EN);
  66. switch (mode) {
  67. case CLOCK_EVT_MODE_RESUME:
  68. case CLOCK_EVT_MODE_PERIODIC:
  69. break;
  70. case CLOCK_EVT_MODE_ONESHOT:
  71. /* Timer is enabled in set_next_event */
  72. break;
  73. case CLOCK_EVT_MODE_UNUSED:
  74. case CLOCK_EVT_MODE_SHUTDOWN:
  75. break;
  76. }
  77. writel_relaxed(ctrl, event_base + TIMER_ENABLE);
  78. }
  79. static struct clock_event_device msm_clockevent = {
  80. .name = "gp_timer",
  81. .features = CLOCK_EVT_FEAT_ONESHOT,
  82. .rating = 200,
  83. .set_next_event = msm_timer_set_next_event,
  84. .set_mode = msm_timer_set_mode,
  85. };
  86. static union {
  87. struct clock_event_device *evt;
  88. struct clock_event_device __percpu **percpu_evt;
  89. } msm_evt;
  90. static void __iomem *source_base;
  91. static cycle_t msm_read_timer_count(struct clocksource *cs)
  92. {
  93. return readl_relaxed(source_base + TIMER_COUNT_VAL);
  94. }
  95. static cycle_t msm_read_timer_count_shift(struct clocksource *cs)
  96. {
  97. /*
  98. * Shift timer count down by a constant due to unreliable lower bits
  99. * on some targets.
  100. */
  101. return msm_read_timer_count(cs) >> MSM_DGT_SHIFT;
  102. }
  103. static struct clocksource msm_clocksource = {
  104. .name = "dg_timer",
  105. .rating = 300,
  106. .read = msm_read_timer_count,
  107. .mask = CLOCKSOURCE_MASK(32),
  108. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  109. };
  110. static void __init msm_timer_init(void)
  111. {
  112. struct clock_event_device *ce = &msm_clockevent;
  113. struct clocksource *cs = &msm_clocksource;
  114. int res;
  115. u32 dgt_hz;
  116. if (cpu_is_msm7x01()) {
  117. event_base = MSM_CSR_BASE;
  118. source_base = MSM_CSR_BASE + 0x10;
  119. dgt_hz = 19200000 >> MSM_DGT_SHIFT; /* 600 KHz */
  120. cs->read = msm_read_timer_count_shift;
  121. cs->mask = CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT));
  122. } else if (cpu_is_msm7x30()) {
  123. event_base = MSM_CSR_BASE + 0x04;
  124. source_base = MSM_CSR_BASE + 0x24;
  125. dgt_hz = 24576000 / 4;
  126. } else if (cpu_is_qsd8x50()) {
  127. event_base = MSM_CSR_BASE;
  128. source_base = MSM_CSR_BASE + 0x10;
  129. dgt_hz = 19200000 / 4;
  130. } else if (cpu_is_msm8x60() || cpu_is_msm8960()) {
  131. event_base = MSM_TMR_BASE + 0x04;
  132. /* Use CPU0's timer as the global clock source. */
  133. source_base = MSM_TMR0_BASE + 0x24;
  134. dgt_hz = 27000000 / 4;
  135. writel_relaxed(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
  136. } else
  137. BUG();
  138. writel_relaxed(0, event_base + TIMER_ENABLE);
  139. writel_relaxed(0, event_base + TIMER_CLEAR);
  140. writel_relaxed(~0, event_base + TIMER_MATCH_VAL);
  141. ce->cpumask = cpumask_of(0);
  142. ce->irq = INT_GP_TIMER_EXP;
  143. clockevents_config_and_register(ce, GPT_HZ, 4, 0xffffffff);
  144. if (cpu_is_msm8x60() || cpu_is_msm8960()) {
  145. msm_evt.percpu_evt = alloc_percpu(struct clock_event_device *);
  146. if (!msm_evt.percpu_evt) {
  147. pr_err("memory allocation failed for %s\n", ce->name);
  148. goto err;
  149. }
  150. *__this_cpu_ptr(msm_evt.percpu_evt) = ce;
  151. res = request_percpu_irq(ce->irq, msm_timer_interrupt,
  152. ce->name, msm_evt.percpu_evt);
  153. if (!res)
  154. enable_percpu_irq(ce->irq, 0);
  155. } else {
  156. msm_evt.evt = ce;
  157. res = request_irq(ce->irq, msm_timer_interrupt,
  158. IRQF_TIMER | IRQF_NOBALANCING |
  159. IRQF_TRIGGER_RISING, ce->name, &msm_evt.evt);
  160. }
  161. if (res)
  162. pr_err("request_irq failed for %s\n", ce->name);
  163. err:
  164. writel_relaxed(TIMER_ENABLE_EN, source_base + TIMER_ENABLE);
  165. res = clocksource_register_hz(cs, dgt_hz);
  166. if (res)
  167. pr_err("clocksource_register failed\n");
  168. }
  169. #ifdef CONFIG_LOCAL_TIMERS
  170. int __cpuinit local_timer_setup(struct clock_event_device *evt)
  171. {
  172. /* Use existing clock_event for cpu 0 */
  173. if (!smp_processor_id())
  174. return 0;
  175. writel_relaxed(0, event_base + TIMER_ENABLE);
  176. writel_relaxed(0, event_base + TIMER_CLEAR);
  177. writel_relaxed(~0, event_base + TIMER_MATCH_VAL);
  178. evt->irq = msm_clockevent.irq;
  179. evt->name = "local_timer";
  180. evt->features = msm_clockevent.features;
  181. evt->rating = msm_clockevent.rating;
  182. evt->set_mode = msm_timer_set_mode;
  183. evt->set_next_event = msm_timer_set_next_event;
  184. evt->shift = msm_clockevent.shift;
  185. evt->mult = div_sc(GPT_HZ, NSEC_PER_SEC, evt->shift);
  186. evt->max_delta_ns = clockevent_delta2ns(0xf0000000, evt);
  187. evt->min_delta_ns = clockevent_delta2ns(4, evt);
  188. *__this_cpu_ptr(msm_evt.percpu_evt) = evt;
  189. clockevents_register_device(evt);
  190. enable_percpu_irq(evt->irq, 0);
  191. return 0;
  192. }
  193. void local_timer_stop(struct clock_event_device *evt)
  194. {
  195. evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
  196. disable_percpu_irq(evt->irq);
  197. }
  198. #endif /* CONFIG_LOCAL_TIMERS */
  199. struct sys_timer msm_timer = {
  200. .init = msm_timer_init
  201. };