timer.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #ifdef CONFIG_LOCAL_TIMERS
  111. static int __cpuinit msm_local_timer_setup(struct clock_event_device *evt)
  112. {
  113. /* Use existing clock_event for cpu 0 */
  114. if (!smp_processor_id())
  115. return 0;
  116. writel_relaxed(0, event_base + TIMER_ENABLE);
  117. writel_relaxed(0, event_base + TIMER_CLEAR);
  118. writel_relaxed(~0, event_base + TIMER_MATCH_VAL);
  119. evt->irq = msm_clockevent.irq;
  120. evt->name = "local_timer";
  121. evt->features = msm_clockevent.features;
  122. evt->rating = msm_clockevent.rating;
  123. evt->set_mode = msm_timer_set_mode;
  124. evt->set_next_event = msm_timer_set_next_event;
  125. evt->shift = msm_clockevent.shift;
  126. evt->mult = div_sc(GPT_HZ, NSEC_PER_SEC, evt->shift);
  127. evt->max_delta_ns = clockevent_delta2ns(0xf0000000, evt);
  128. evt->min_delta_ns = clockevent_delta2ns(4, evt);
  129. *__this_cpu_ptr(msm_evt.percpu_evt) = evt;
  130. clockevents_register_device(evt);
  131. enable_percpu_irq(evt->irq, 0);
  132. return 0;
  133. }
  134. static void msm_local_timer_stop(struct clock_event_device *evt)
  135. {
  136. evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
  137. disable_percpu_irq(evt->irq);
  138. }
  139. static struct local_timer_ops msm_local_timer_ops __cpuinitdata = {
  140. .setup = msm_local_timer_setup,
  141. .stop = msm_local_timer_stop,
  142. };
  143. #endif /* CONFIG_LOCAL_TIMERS */
  144. static void __init msm_timer_init(void)
  145. {
  146. struct clock_event_device *ce = &msm_clockevent;
  147. struct clocksource *cs = &msm_clocksource;
  148. int res;
  149. u32 dgt_hz;
  150. if (cpu_is_msm7x01()) {
  151. event_base = MSM_CSR_BASE;
  152. source_base = MSM_CSR_BASE + 0x10;
  153. dgt_hz = 19200000 >> MSM_DGT_SHIFT; /* 600 KHz */
  154. cs->read = msm_read_timer_count_shift;
  155. cs->mask = CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT));
  156. } else if (cpu_is_msm7x30()) {
  157. event_base = MSM_CSR_BASE + 0x04;
  158. source_base = MSM_CSR_BASE + 0x24;
  159. dgt_hz = 24576000 / 4;
  160. } else if (cpu_is_qsd8x50()) {
  161. event_base = MSM_CSR_BASE;
  162. source_base = MSM_CSR_BASE + 0x10;
  163. dgt_hz = 19200000 / 4;
  164. } else if (cpu_is_msm8x60() || cpu_is_msm8960()) {
  165. event_base = MSM_TMR_BASE + 0x04;
  166. /* Use CPU0's timer as the global clock source. */
  167. source_base = MSM_TMR0_BASE + 0x24;
  168. dgt_hz = 27000000 / 4;
  169. writel_relaxed(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
  170. } else
  171. BUG();
  172. writel_relaxed(0, event_base + TIMER_ENABLE);
  173. writel_relaxed(0, event_base + TIMER_CLEAR);
  174. writel_relaxed(~0, event_base + TIMER_MATCH_VAL);
  175. ce->cpumask = cpumask_of(0);
  176. ce->irq = INT_GP_TIMER_EXP;
  177. clockevents_config_and_register(ce, GPT_HZ, 4, 0xffffffff);
  178. if (cpu_is_msm8x60() || cpu_is_msm8960()) {
  179. msm_evt.percpu_evt = alloc_percpu(struct clock_event_device *);
  180. if (!msm_evt.percpu_evt) {
  181. pr_err("memory allocation failed for %s\n", ce->name);
  182. goto err;
  183. }
  184. *__this_cpu_ptr(msm_evt.percpu_evt) = ce;
  185. res = request_percpu_irq(ce->irq, msm_timer_interrupt,
  186. ce->name, msm_evt.percpu_evt);
  187. if (!res) {
  188. enable_percpu_irq(ce->irq, 0);
  189. #ifdef CONFIG_LOCAL_TIMERS
  190. local_timer_register(&msm_local_timer_ops);
  191. #endif
  192. }
  193. } else {
  194. msm_evt.evt = ce;
  195. res = request_irq(ce->irq, msm_timer_interrupt,
  196. IRQF_TIMER | IRQF_NOBALANCING |
  197. IRQF_TRIGGER_RISING, ce->name, &msm_evt.evt);
  198. }
  199. if (res)
  200. pr_err("request_irq failed for %s\n", ce->name);
  201. err:
  202. writel_relaxed(TIMER_ENABLE_EN, source_base + TIMER_ENABLE);
  203. res = clocksource_register_hz(cs, dgt_hz);
  204. if (res)
  205. pr_err("clocksource_register failed\n");
  206. }
  207. struct sys_timer msm_timer = {
  208. .init = msm_timer_init
  209. };