timer.c 6.3 KB

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