timer.c 8.3 KB

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