timer.c 6.9 KB

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