timer.c 8.4 KB

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