timer.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* linux/arch/arm/mach-msm/timer.c
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/init.h>
  16. #include <linux/time.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/clk.h>
  20. #include <linux/clockchips.h>
  21. #include <linux/delay.h>
  22. #include <linux/io.h>
  23. #include <asm/mach/time.h>
  24. #include <mach/msm_iomap.h>
  25. #ifndef MSM_DGT_BASE
  26. #define MSM_DGT_BASE (MSM_GPT_BASE + 0x10)
  27. #endif
  28. #define MSM_DGT_SHIFT (5)
  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 2
  33. #define TIMER_ENABLE_EN 1
  34. #define TIMER_CLEAR 0x000C
  35. #define CSR_PROTECTION 0x0020
  36. #define CSR_PROTECTION_EN 1
  37. #define GPT_HZ 32768
  38. #define DGT_HZ 19200000 /* 19.2 MHz or 600 KHz after shift */
  39. struct msm_clock {
  40. struct clock_event_device clockevent;
  41. struct clocksource clocksource;
  42. struct irqaction irq;
  43. void __iomem *regbase;
  44. uint32_t freq;
  45. uint32_t shift;
  46. };
  47. static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
  48. {
  49. struct clock_event_device *evt = dev_id;
  50. evt->event_handler(evt);
  51. return IRQ_HANDLED;
  52. }
  53. static cycle_t msm_gpt_read(struct clocksource *cs)
  54. {
  55. return readl(MSM_GPT_BASE + TIMER_COUNT_VAL);
  56. }
  57. static cycle_t msm_dgt_read(struct clocksource *cs)
  58. {
  59. return readl(MSM_DGT_BASE + TIMER_COUNT_VAL) >> MSM_DGT_SHIFT;
  60. }
  61. static int msm_timer_set_next_event(unsigned long cycles,
  62. struct clock_event_device *evt)
  63. {
  64. struct msm_clock *clock = container_of(evt, struct msm_clock, clockevent);
  65. uint32_t now = readl(clock->regbase + TIMER_COUNT_VAL);
  66. uint32_t alarm = now + (cycles << clock->shift);
  67. int late;
  68. writel(alarm, clock->regbase + TIMER_MATCH_VAL);
  69. now = readl(clock->regbase + TIMER_COUNT_VAL);
  70. late = now - alarm;
  71. if (late >= (-2 << clock->shift) && late < DGT_HZ*5) {
  72. printk(KERN_NOTICE "msm_timer_set_next_event(%lu) clock %s, "
  73. "alarm already expired, now %x, alarm %x, late %d\n",
  74. cycles, clock->clockevent.name, now, alarm, late);
  75. return -ETIME;
  76. }
  77. return 0;
  78. }
  79. static void msm_timer_set_mode(enum clock_event_mode mode,
  80. struct clock_event_device *evt)
  81. {
  82. struct msm_clock *clock = container_of(evt, struct msm_clock, clockevent);
  83. switch (mode) {
  84. case CLOCK_EVT_MODE_RESUME:
  85. case CLOCK_EVT_MODE_PERIODIC:
  86. break;
  87. case CLOCK_EVT_MODE_ONESHOT:
  88. writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
  89. break;
  90. case CLOCK_EVT_MODE_UNUSED:
  91. case CLOCK_EVT_MODE_SHUTDOWN:
  92. writel(0, clock->regbase + TIMER_ENABLE);
  93. break;
  94. }
  95. }
  96. static struct msm_clock msm_clocks[] = {
  97. {
  98. .clockevent = {
  99. .name = "gp_timer",
  100. .features = CLOCK_EVT_FEAT_ONESHOT,
  101. .shift = 32,
  102. .rating = 200,
  103. .set_next_event = msm_timer_set_next_event,
  104. .set_mode = msm_timer_set_mode,
  105. },
  106. .clocksource = {
  107. .name = "gp_timer",
  108. .rating = 200,
  109. .read = msm_gpt_read,
  110. .mask = CLOCKSOURCE_MASK(32),
  111. .shift = 24,
  112. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  113. },
  114. .irq = {
  115. .name = "gp_timer",
  116. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_RISING,
  117. .handler = msm_timer_interrupt,
  118. .dev_id = &msm_clocks[0].clockevent,
  119. .irq = INT_GP_TIMER_EXP
  120. },
  121. .regbase = MSM_GPT_BASE,
  122. .freq = GPT_HZ
  123. },
  124. {
  125. .clockevent = {
  126. .name = "dg_timer",
  127. .features = CLOCK_EVT_FEAT_ONESHOT,
  128. .shift = 32 + MSM_DGT_SHIFT,
  129. .rating = 300,
  130. .set_next_event = msm_timer_set_next_event,
  131. .set_mode = msm_timer_set_mode,
  132. },
  133. .clocksource = {
  134. .name = "dg_timer",
  135. .rating = 300,
  136. .read = msm_dgt_read,
  137. .mask = CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT)),
  138. .shift = 24 - MSM_DGT_SHIFT,
  139. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  140. },
  141. .irq = {
  142. .name = "dg_timer",
  143. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_RISING,
  144. .handler = msm_timer_interrupt,
  145. .dev_id = &msm_clocks[1].clockevent,
  146. .irq = INT_DEBUG_TIMER_EXP
  147. },
  148. .regbase = MSM_DGT_BASE,
  149. .freq = DGT_HZ >> MSM_DGT_SHIFT,
  150. .shift = MSM_DGT_SHIFT
  151. }
  152. };
  153. static void __init msm_timer_init(void)
  154. {
  155. int i;
  156. int res;
  157. for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) {
  158. struct msm_clock *clock = &msm_clocks[i];
  159. struct clock_event_device *ce = &clock->clockevent;
  160. struct clocksource *cs = &clock->clocksource;
  161. writel(0, clock->regbase + TIMER_ENABLE);
  162. writel(0, clock->regbase + TIMER_CLEAR);
  163. writel(~0, clock->regbase + TIMER_MATCH_VAL);
  164. ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift);
  165. /* allow at least 10 seconds to notice that the timer wrapped */
  166. ce->max_delta_ns =
  167. clockevent_delta2ns(0xf0000000 >> clock->shift, ce);
  168. /* 4 gets rounded down to 3 */
  169. ce->min_delta_ns = clockevent_delta2ns(4, ce);
  170. ce->cpumask = cpumask_of(0);
  171. cs->mult = clocksource_hz2mult(clock->freq, cs->shift);
  172. res = clocksource_register(cs);
  173. if (res)
  174. printk(KERN_ERR "msm_timer_init: clocksource_register "
  175. "failed for %s\n", cs->name);
  176. res = setup_irq(clock->irq.irq, &clock->irq);
  177. if (res)
  178. printk(KERN_ERR "msm_timer_init: setup_irq "
  179. "failed for %s\n", cs->name);
  180. clockevents_register_device(ce);
  181. }
  182. }
  183. struct sys_timer msm_timer = {
  184. .init = msm_timer_init
  185. };