timer.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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) || defined(CONFIG_ARCH_MSM8X60) || \
  53. defined(CONFIG_ARCH_MSM8960)
  54. #define DGT_HZ (24576000 / 4) /* 24.576 MHz (LPXO) / 4 by default */
  55. #define MSM_DGT_SHIFT (0)
  56. #else
  57. #define DGT_HZ 19200000 /* 19.2 MHz or 600 KHz after shift */
  58. #define MSM_DGT_SHIFT (5)
  59. #endif
  60. struct msm_clock {
  61. struct clock_event_device clockevent;
  62. struct clocksource clocksource;
  63. struct irqaction irq;
  64. void __iomem *regbase;
  65. uint32_t freq;
  66. uint32_t shift;
  67. void __iomem *global_counter;
  68. void __iomem *local_counter;
  69. };
  70. enum {
  71. MSM_CLOCK_GPT,
  72. MSM_CLOCK_DGT,
  73. NR_TIMERS,
  74. };
  75. static struct msm_clock msm_clocks[];
  76. static struct clock_event_device *local_clock_event;
  77. static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
  78. {
  79. struct clock_event_device *evt = dev_id;
  80. if (smp_processor_id() != 0)
  81. evt = local_clock_event;
  82. if (evt->event_handler == NULL)
  83. return IRQ_HANDLED;
  84. evt->event_handler(evt);
  85. return IRQ_HANDLED;
  86. }
  87. static cycle_t msm_read_timer_count(struct clocksource *cs)
  88. {
  89. struct msm_clock *clk = container_of(cs, struct msm_clock, clocksource);
  90. /*
  91. * Shift timer count down by a constant due to unreliable lower bits
  92. * on some targets.
  93. */
  94. return readl(clk->global_counter) >> clk->shift;
  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. .freq = GPT_HZ,
  159. },
  160. [MSM_CLOCK_DGT] = {
  161. .clockevent = {
  162. .name = "dg_timer",
  163. .features = CLOCK_EVT_FEAT_ONESHOT,
  164. .shift = 32 + MSM_DGT_SHIFT,
  165. .rating = 300,
  166. .set_next_event = msm_timer_set_next_event,
  167. .set_mode = msm_timer_set_mode,
  168. },
  169. .clocksource = {
  170. .name = "dg_timer",
  171. .rating = 300,
  172. .read = msm_read_timer_count,
  173. .mask = CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT)),
  174. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  175. },
  176. .irq = {
  177. .name = "dg_timer",
  178. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_RISING,
  179. .handler = msm_timer_interrupt,
  180. .dev_id = &msm_clocks[1].clockevent,
  181. .irq = INT_DEBUG_TIMER_EXP
  182. },
  183. .freq = DGT_HZ >> MSM_DGT_SHIFT,
  184. .shift = MSM_DGT_SHIFT,
  185. }
  186. };
  187. static void __init msm_timer_init(void)
  188. {
  189. int i;
  190. int res;
  191. int global_offset = 0;
  192. if (cpu_is_msm7x01()) {
  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_msm7x30()) {
  196. msm_clocks[MSM_CLOCK_GPT].regbase = MSM_CSR_BASE + 0x04;
  197. msm_clocks[MSM_CLOCK_DGT].regbase = MSM_CSR_BASE + 0x24;
  198. } else if (cpu_is_qsd8x50()) {
  199. msm_clocks[MSM_CLOCK_GPT].regbase = MSM_CSR_BASE;
  200. msm_clocks[MSM_CLOCK_DGT].regbase = MSM_CSR_BASE + 0x10;
  201. } else if (cpu_is_msm8x60() || cpu_is_msm8960()) {
  202. msm_clocks[MSM_CLOCK_GPT].regbase = MSM_TMR_BASE + 0x04;
  203. msm_clocks[MSM_CLOCK_DGT].regbase = MSM_TMR_BASE + 0x24;
  204. /* Use CPU0's timer as the global timer. */
  205. global_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
  206. } else
  207. BUG();
  208. #ifdef CONFIG_ARCH_MSM_SCORPIONMP
  209. writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
  210. #endif
  211. for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) {
  212. struct msm_clock *clock = &msm_clocks[i];
  213. struct clock_event_device *ce = &clock->clockevent;
  214. struct clocksource *cs = &clock->clocksource;
  215. clock->local_counter = clock->regbase + TIMER_COUNT_VAL;
  216. clock->global_counter = clock->local_counter + global_offset;
  217. writel(0, clock->regbase + TIMER_ENABLE);
  218. writel(0, clock->regbase + TIMER_CLEAR);
  219. writel(~0, clock->regbase + TIMER_MATCH_VAL);
  220. ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift);
  221. /* allow at least 10 seconds to notice that the timer wrapped */
  222. ce->max_delta_ns =
  223. clockevent_delta2ns(0xf0000000 >> clock->shift, ce);
  224. /* 4 gets rounded down to 3 */
  225. ce->min_delta_ns = clockevent_delta2ns(4, ce);
  226. ce->cpumask = cpumask_of(0);
  227. res = clocksource_register_hz(cs, clock->freq);
  228. if (res)
  229. printk(KERN_ERR "msm_timer_init: clocksource_register "
  230. "failed for %s\n", cs->name);
  231. res = setup_irq(clock->irq.irq, &clock->irq);
  232. if (res)
  233. printk(KERN_ERR "msm_timer_init: setup_irq "
  234. "failed for %s\n", cs->name);
  235. clockevents_register_device(ce);
  236. }
  237. }
  238. #ifdef CONFIG_SMP
  239. int __cpuinit local_timer_setup(struct clock_event_device *evt)
  240. {
  241. struct msm_clock *clock = &msm_clocks[MSM_GLOBAL_TIMER];
  242. /* Use existing clock_event for cpu 0 */
  243. if (!smp_processor_id())
  244. return 0;
  245. writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
  246. if (!local_clock_event) {
  247. writel(0, clock->regbase + TIMER_ENABLE);
  248. writel(0, clock->regbase + TIMER_CLEAR);
  249. writel(~0, clock->regbase + TIMER_MATCH_VAL);
  250. }
  251. evt->irq = clock->irq.irq;
  252. evt->name = "local_timer";
  253. evt->features = CLOCK_EVT_FEAT_ONESHOT;
  254. evt->rating = clock->clockevent.rating;
  255. evt->set_mode = msm_timer_set_mode;
  256. evt->set_next_event = msm_timer_set_next_event;
  257. evt->shift = clock->clockevent.shift;
  258. evt->mult = div_sc(clock->freq, NSEC_PER_SEC, evt->shift);
  259. evt->max_delta_ns =
  260. clockevent_delta2ns(0xf0000000 >> clock->shift, evt);
  261. evt->min_delta_ns = clockevent_delta2ns(4, evt);
  262. local_clock_event = evt;
  263. gic_enable_ppi(clock->irq.irq);
  264. clockevents_register_device(evt);
  265. return 0;
  266. }
  267. inline int local_timer_ack(void)
  268. {
  269. return 1;
  270. }
  271. #endif
  272. struct sys_timer msm_timer = {
  273. .init = msm_timer_init
  274. };