timer.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. enum timer_location {
  45. LOCAL_TIMER = 0,
  46. GLOBAL_TIMER = 1,
  47. };
  48. #ifdef MSM_TMR0_BASE
  49. #define MSM_TMR_GLOBAL (MSM_TMR0_BASE - MSM_TMR_BASE)
  50. #else
  51. #define MSM_TMR_GLOBAL 0
  52. #endif
  53. #define MSM_GLOBAL_TIMER MSM_CLOCK_DGT
  54. #if defined(CONFIG_ARCH_QSD8X50)
  55. #define DGT_HZ (19200000 / 4) /* 19.2 MHz / 4 by default */
  56. #define MSM_DGT_SHIFT (0)
  57. #elif defined(CONFIG_ARCH_MSM7X30) || defined(CONFIG_ARCH_MSM8X60)
  58. #define DGT_HZ (24576000 / 4) /* 24.576 MHz (LPXO) / 4 by default */
  59. #define MSM_DGT_SHIFT (0)
  60. #else
  61. #define DGT_HZ 19200000 /* 19.2 MHz or 600 KHz after shift */
  62. #define MSM_DGT_SHIFT (5)
  63. #endif
  64. struct msm_clock {
  65. struct clock_event_device clockevent;
  66. struct clocksource clocksource;
  67. struct irqaction irq;
  68. void __iomem *regbase;
  69. uint32_t freq;
  70. uint32_t shift;
  71. void __iomem *global_counter;
  72. void __iomem *local_counter;
  73. };
  74. enum {
  75. MSM_CLOCK_GPT,
  76. MSM_CLOCK_DGT,
  77. NR_TIMERS,
  78. };
  79. static struct msm_clock msm_clocks[];
  80. static struct clock_event_device *local_clock_event;
  81. static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
  82. {
  83. struct clock_event_device *evt = dev_id;
  84. if (smp_processor_id() != 0)
  85. evt = local_clock_event;
  86. if (evt->event_handler == NULL)
  87. return IRQ_HANDLED;
  88. evt->event_handler(evt);
  89. return IRQ_HANDLED;
  90. }
  91. static cycle_t msm_read_timer_count(struct clocksource *cs)
  92. {
  93. struct msm_clock *clk = container_of(cs, struct msm_clock, clocksource);
  94. return readl(clk->global_counter);
  95. }
  96. static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt)
  97. {
  98. #ifdef CONFIG_SMP
  99. int i;
  100. for (i = 0; i < NR_TIMERS; i++)
  101. if (evt == &(msm_clocks[i].clockevent))
  102. return &msm_clocks[i];
  103. return &msm_clocks[MSM_GLOBAL_TIMER];
  104. #else
  105. return container_of(evt, struct msm_clock, clockevent);
  106. #endif
  107. }
  108. static int msm_timer_set_next_event(unsigned long cycles,
  109. struct clock_event_device *evt)
  110. {
  111. struct msm_clock *clock = clockevent_to_clock(evt);
  112. uint32_t now = readl(clock->local_counter);
  113. uint32_t alarm = now + (cycles << clock->shift);
  114. writel(alarm, clock->regbase + TIMER_MATCH_VAL);
  115. return 0;
  116. }
  117. static void msm_timer_set_mode(enum clock_event_mode mode,
  118. struct clock_event_device *evt)
  119. {
  120. struct msm_clock *clock = clockevent_to_clock(evt);
  121. switch (mode) {
  122. case CLOCK_EVT_MODE_RESUME:
  123. case CLOCK_EVT_MODE_PERIODIC:
  124. break;
  125. case CLOCK_EVT_MODE_ONESHOT:
  126. writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
  127. break;
  128. case CLOCK_EVT_MODE_UNUSED:
  129. case CLOCK_EVT_MODE_SHUTDOWN:
  130. writel(0, clock->regbase + TIMER_ENABLE);
  131. break;
  132. }
  133. }
  134. static struct msm_clock msm_clocks[] = {
  135. [MSM_CLOCK_GPT] = {
  136. .clockevent = {
  137. .name = "gp_timer",
  138. .features = CLOCK_EVT_FEAT_ONESHOT,
  139. .shift = 32,
  140. .rating = 200,
  141. .set_next_event = msm_timer_set_next_event,
  142. .set_mode = msm_timer_set_mode,
  143. },
  144. .clocksource = {
  145. .name = "gp_timer",
  146. .rating = 200,
  147. .read = msm_read_timer_count,
  148. .mask = CLOCKSOURCE_MASK(32),
  149. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  150. },
  151. .irq = {
  152. .name = "gp_timer",
  153. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_RISING,
  154. .handler = msm_timer_interrupt,
  155. .dev_id = &msm_clocks[0].clockevent,
  156. .irq = INT_GP_TIMER_EXP
  157. },
  158. .regbase = MSM_GPT_BASE,
  159. .freq = GPT_HZ,
  160. .local_counter = MSM_GPT_BASE + TIMER_COUNT_VAL,
  161. .global_counter = MSM_GPT_BASE + TIMER_COUNT_VAL +
  162. MSM_TMR_GLOBAL,
  163. },
  164. [MSM_CLOCK_DGT] = {
  165. .clockevent = {
  166. .name = "dg_timer",
  167. .features = CLOCK_EVT_FEAT_ONESHOT,
  168. .shift = 32 + MSM_DGT_SHIFT,
  169. .rating = 300,
  170. .set_next_event = msm_timer_set_next_event,
  171. .set_mode = msm_timer_set_mode,
  172. },
  173. .clocksource = {
  174. .name = "dg_timer",
  175. .rating = 300,
  176. .read = msm_read_timer_count,
  177. .mask = CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT)),
  178. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  179. },
  180. .irq = {
  181. .name = "dg_timer",
  182. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_RISING,
  183. .handler = msm_timer_interrupt,
  184. .dev_id = &msm_clocks[1].clockevent,
  185. .irq = INT_DEBUG_TIMER_EXP
  186. },
  187. .regbase = MSM_DGT_BASE,
  188. .freq = DGT_HZ >> MSM_DGT_SHIFT,
  189. .shift = MSM_DGT_SHIFT,
  190. .local_counter = MSM_DGT_BASE + TIMER_COUNT_VAL,
  191. .global_counter = MSM_DGT_BASE + TIMER_COUNT_VAL +
  192. MSM_TMR_GLOBAL,
  193. }
  194. };
  195. static void __init msm_timer_init(void)
  196. {
  197. int i;
  198. int res;
  199. #ifdef CONFIG_ARCH_MSM_SCORPIONMP
  200. writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
  201. #endif
  202. for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) {
  203. struct msm_clock *clock = &msm_clocks[i];
  204. struct clock_event_device *ce = &clock->clockevent;
  205. struct clocksource *cs = &clock->clocksource;
  206. writel(0, clock->regbase + TIMER_ENABLE);
  207. writel(0, clock->regbase + TIMER_CLEAR);
  208. writel(~0, clock->regbase + TIMER_MATCH_VAL);
  209. ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift);
  210. /* allow at least 10 seconds to notice that the timer wrapped */
  211. ce->max_delta_ns =
  212. clockevent_delta2ns(0xf0000000 >> clock->shift, ce);
  213. /* 4 gets rounded down to 3 */
  214. ce->min_delta_ns = clockevent_delta2ns(4, ce);
  215. ce->cpumask = cpumask_of(0);
  216. res = clocksource_register_hz(cs, clock->freq);
  217. if (res)
  218. printk(KERN_ERR "msm_timer_init: clocksource_register "
  219. "failed for %s\n", cs->name);
  220. res = setup_irq(clock->irq.irq, &clock->irq);
  221. if (res)
  222. printk(KERN_ERR "msm_timer_init: setup_irq "
  223. "failed for %s\n", cs->name);
  224. clockevents_register_device(ce);
  225. }
  226. }
  227. #ifdef CONFIG_SMP
  228. void __cpuinit local_timer_setup(struct clock_event_device *evt)
  229. {
  230. struct msm_clock *clock = &msm_clocks[MSM_GLOBAL_TIMER];
  231. /* Use existing clock_event for cpu 0 */
  232. if (!smp_processor_id())
  233. return;
  234. writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
  235. if (!local_clock_event) {
  236. writel(0, clock->regbase + TIMER_ENABLE);
  237. writel(0, clock->regbase + TIMER_CLEAR);
  238. writel(~0, clock->regbase + TIMER_MATCH_VAL);
  239. }
  240. evt->irq = clock->irq.irq;
  241. evt->name = "local_timer";
  242. evt->features = CLOCK_EVT_FEAT_ONESHOT;
  243. evt->rating = clock->clockevent.rating;
  244. evt->set_mode = msm_timer_set_mode;
  245. evt->set_next_event = msm_timer_set_next_event;
  246. evt->shift = clock->clockevent.shift;
  247. evt->mult = div_sc(clock->freq, NSEC_PER_SEC, evt->shift);
  248. evt->max_delta_ns =
  249. clockevent_delta2ns(0xf0000000 >> clock->shift, evt);
  250. evt->min_delta_ns = clockevent_delta2ns(4, evt);
  251. local_clock_event = evt;
  252. gic_enable_ppi(clock->irq.irq);
  253. clockevents_register_device(evt);
  254. }
  255. inline int local_timer_ack(void)
  256. {
  257. return 1;
  258. }
  259. #endif
  260. struct sys_timer msm_timer = {
  261. .init = msm_timer_init
  262. };