timer.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #define MSM_DGT_BASE (MSM_GPT_BASE + 0x10)
  26. #define MSM_DGT_SHIFT (5)
  27. #define TIMER_MATCH_VAL 0x0000
  28. #define TIMER_COUNT_VAL 0x0004
  29. #define TIMER_ENABLE 0x0008
  30. #define TIMER_ENABLE_CLR_ON_MATCH_EN 2
  31. #define TIMER_ENABLE_EN 1
  32. #define TIMER_CLEAR 0x000C
  33. #define CSR_PROTECTION 0x0020
  34. #define CSR_PROTECTION_EN 1
  35. #define GPT_HZ 32768
  36. #define DGT_HZ 19200000 /* 19.2 MHz or 600 KHz after shift */
  37. struct msm_clock {
  38. struct clock_event_device clockevent;
  39. struct clocksource clocksource;
  40. struct irqaction irq;
  41. void __iomem *regbase;
  42. uint32_t freq;
  43. uint32_t shift;
  44. };
  45. static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
  46. {
  47. struct clock_event_device *evt = dev_id;
  48. evt->event_handler(evt);
  49. return IRQ_HANDLED;
  50. }
  51. static cycle_t msm_gpt_read(struct clocksource *cs)
  52. {
  53. return readl(MSM_GPT_BASE + TIMER_COUNT_VAL);
  54. }
  55. static cycle_t msm_dgt_read(struct clocksource *cs)
  56. {
  57. return readl(MSM_DGT_BASE + TIMER_COUNT_VAL) >> MSM_DGT_SHIFT;
  58. }
  59. static int msm_timer_set_next_event(unsigned long cycles,
  60. struct clock_event_device *evt)
  61. {
  62. struct msm_clock *clock = container_of(evt, struct msm_clock, clockevent);
  63. uint32_t now = readl(clock->regbase + TIMER_COUNT_VAL);
  64. uint32_t alarm = now + (cycles << clock->shift);
  65. int late;
  66. writel(alarm, clock->regbase + TIMER_MATCH_VAL);
  67. now = readl(clock->regbase + TIMER_COUNT_VAL);
  68. late = now - alarm;
  69. if (late >= (-2 << clock->shift) && late < DGT_HZ*5) {
  70. printk(KERN_NOTICE "msm_timer_set_next_event(%lu) clock %s, "
  71. "alarm already expired, now %x, alarm %x, late %d\n",
  72. cycles, clock->clockevent.name, now, alarm, late);
  73. return -ETIME;
  74. }
  75. return 0;
  76. }
  77. static void msm_timer_set_mode(enum clock_event_mode mode,
  78. struct clock_event_device *evt)
  79. {
  80. struct msm_clock *clock = container_of(evt, struct msm_clock, clockevent);
  81. switch (mode) {
  82. case CLOCK_EVT_MODE_RESUME:
  83. case CLOCK_EVT_MODE_PERIODIC:
  84. break;
  85. case CLOCK_EVT_MODE_ONESHOT:
  86. writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
  87. break;
  88. case CLOCK_EVT_MODE_UNUSED:
  89. case CLOCK_EVT_MODE_SHUTDOWN:
  90. writel(0, clock->regbase + TIMER_ENABLE);
  91. break;
  92. }
  93. }
  94. static struct msm_clock msm_clocks[] = {
  95. {
  96. .clockevent = {
  97. .name = "gp_timer",
  98. .features = CLOCK_EVT_FEAT_ONESHOT,
  99. .shift = 32,
  100. .rating = 200,
  101. .set_next_event = msm_timer_set_next_event,
  102. .set_mode = msm_timer_set_mode,
  103. },
  104. .clocksource = {
  105. .name = "gp_timer",
  106. .rating = 200,
  107. .read = msm_gpt_read,
  108. .mask = CLOCKSOURCE_MASK(32),
  109. .shift = 24,
  110. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  111. },
  112. .irq = {
  113. .name = "gp_timer",
  114. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_RISING,
  115. .handler = msm_timer_interrupt,
  116. .dev_id = &msm_clocks[0].clockevent,
  117. .irq = INT_GP_TIMER_EXP
  118. },
  119. .regbase = MSM_GPT_BASE,
  120. .freq = GPT_HZ
  121. },
  122. {
  123. .clockevent = {
  124. .name = "dg_timer",
  125. .features = CLOCK_EVT_FEAT_ONESHOT,
  126. .shift = 32 + MSM_DGT_SHIFT,
  127. .rating = 300,
  128. .set_next_event = msm_timer_set_next_event,
  129. .set_mode = msm_timer_set_mode,
  130. },
  131. .clocksource = {
  132. .name = "dg_timer",
  133. .rating = 300,
  134. .read = msm_dgt_read,
  135. .mask = CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT)),
  136. .shift = 24 - MSM_DGT_SHIFT,
  137. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  138. },
  139. .irq = {
  140. .name = "dg_timer",
  141. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_RISING,
  142. .handler = msm_timer_interrupt,
  143. .dev_id = &msm_clocks[1].clockevent,
  144. .irq = INT_DEBUG_TIMER_EXP
  145. },
  146. .regbase = MSM_DGT_BASE,
  147. .freq = DGT_HZ >> MSM_DGT_SHIFT,
  148. .shift = MSM_DGT_SHIFT
  149. }
  150. };
  151. static void __init msm_timer_init(void)
  152. {
  153. int i;
  154. int res;
  155. for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) {
  156. struct msm_clock *clock = &msm_clocks[i];
  157. struct clock_event_device *ce = &clock->clockevent;
  158. struct clocksource *cs = &clock->clocksource;
  159. writel(0, clock->regbase + TIMER_ENABLE);
  160. writel(0, clock->regbase + TIMER_CLEAR);
  161. writel(~0, clock->regbase + TIMER_MATCH_VAL);
  162. ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift);
  163. /* allow at least 10 seconds to notice that the timer wrapped */
  164. ce->max_delta_ns =
  165. clockevent_delta2ns(0xf0000000 >> clock->shift, ce);
  166. /* 4 gets rounded down to 3 */
  167. ce->min_delta_ns = clockevent_delta2ns(4, ce);
  168. ce->cpumask = cpumask_of(0);
  169. cs->mult = clocksource_hz2mult(clock->freq, cs->shift);
  170. res = clocksource_register(cs);
  171. if (res)
  172. printk(KERN_ERR "msm_timer_init: clocksource_register "
  173. "failed for %s\n", cs->name);
  174. res = setup_irq(clock->irq.irq, &clock->irq);
  175. if (res)
  176. printk(KERN_ERR "msm_timer_init: setup_irq "
  177. "failed for %s\n", cs->name);
  178. clockevents_register_device(ce);
  179. }
  180. }
  181. struct sys_timer msm_timer = {
  182. .init = msm_timer_init
  183. };