timer.c 9.0 KB

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