time.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * linux/arch/arm/mach-mmp/time.c
  3. *
  4. * Support for clocksource and clockevents
  5. *
  6. * Copyright (C) 2008 Marvell International Ltd.
  7. * All rights reserved.
  8. *
  9. * 2008-04-11: Jason Chagas <Jason.chagas@marvell.com>
  10. * 2008-10-08: Bin Yang <bin.yang@marvell.com>
  11. *
  12. * The timers module actually includes three timers, each timer with up to
  13. * three match comparators. Timer #0 is used here in free-running mode as
  14. * the clock source, and match comparator #1 used as clock event device.
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2 as
  18. * published by the Free Software Foundation.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/clockchips.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/of.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_irq.h>
  29. #include <asm/sched_clock.h>
  30. #include <mach/addr-map.h>
  31. #include <mach/regs-timers.h>
  32. #include <mach/regs-apbc.h>
  33. #include <mach/irqs.h>
  34. #include <mach/cputype.h>
  35. #include <asm/mach/time.h>
  36. #include "clock.h"
  37. #define TIMERS_VIRT_BASE TIMERS1_VIRT_BASE
  38. #define MAX_DELTA (0xfffffffe)
  39. #define MIN_DELTA (16)
  40. static void __iomem *mmp_timer_base = TIMERS_VIRT_BASE;
  41. /*
  42. * FIXME: the timer needs some delay to stablize the counter capture
  43. */
  44. static inline uint32_t timer_read(void)
  45. {
  46. int delay = 100;
  47. __raw_writel(1, mmp_timer_base + TMR_CVWR(1));
  48. while (delay--)
  49. cpu_relax();
  50. return __raw_readl(mmp_timer_base + TMR_CVWR(1));
  51. }
  52. static u32 notrace mmp_read_sched_clock(void)
  53. {
  54. return timer_read();
  55. }
  56. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  57. {
  58. struct clock_event_device *c = dev_id;
  59. /*
  60. * Clear pending interrupt status.
  61. */
  62. __raw_writel(0x01, mmp_timer_base + TMR_ICR(0));
  63. /*
  64. * Disable timer 0.
  65. */
  66. __raw_writel(0x02, mmp_timer_base + TMR_CER);
  67. c->event_handler(c);
  68. return IRQ_HANDLED;
  69. }
  70. static int timer_set_next_event(unsigned long delta,
  71. struct clock_event_device *dev)
  72. {
  73. unsigned long flags;
  74. local_irq_save(flags);
  75. /*
  76. * Disable timer 0.
  77. */
  78. __raw_writel(0x02, mmp_timer_base + TMR_CER);
  79. /*
  80. * Clear and enable timer match 0 interrupt.
  81. */
  82. __raw_writel(0x01, mmp_timer_base + TMR_ICR(0));
  83. __raw_writel(0x01, mmp_timer_base + TMR_IER(0));
  84. /*
  85. * Setup new clockevent timer value.
  86. */
  87. __raw_writel(delta - 1, mmp_timer_base + TMR_TN_MM(0, 0));
  88. /*
  89. * Enable timer 0.
  90. */
  91. __raw_writel(0x03, mmp_timer_base + TMR_CER);
  92. local_irq_restore(flags);
  93. return 0;
  94. }
  95. static void timer_set_mode(enum clock_event_mode mode,
  96. struct clock_event_device *dev)
  97. {
  98. unsigned long flags;
  99. local_irq_save(flags);
  100. switch (mode) {
  101. case CLOCK_EVT_MODE_ONESHOT:
  102. case CLOCK_EVT_MODE_UNUSED:
  103. case CLOCK_EVT_MODE_SHUTDOWN:
  104. /* disable the matching interrupt */
  105. __raw_writel(0x00, mmp_timer_base + TMR_IER(0));
  106. break;
  107. case CLOCK_EVT_MODE_RESUME:
  108. case CLOCK_EVT_MODE_PERIODIC:
  109. break;
  110. }
  111. local_irq_restore(flags);
  112. }
  113. static struct clock_event_device ckevt = {
  114. .name = "clockevent",
  115. .features = CLOCK_EVT_FEAT_ONESHOT,
  116. .rating = 200,
  117. .set_next_event = timer_set_next_event,
  118. .set_mode = timer_set_mode,
  119. };
  120. static cycle_t clksrc_read(struct clocksource *cs)
  121. {
  122. return timer_read();
  123. }
  124. static struct clocksource cksrc = {
  125. .name = "clocksource",
  126. .rating = 200,
  127. .read = clksrc_read,
  128. .mask = CLOCKSOURCE_MASK(32),
  129. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  130. };
  131. static void __init timer_config(void)
  132. {
  133. uint32_t ccr = __raw_readl(mmp_timer_base + TMR_CCR);
  134. __raw_writel(0x0, mmp_timer_base + TMR_CER); /* disable */
  135. ccr &= (cpu_is_mmp2()) ? (TMR_CCR_CS_0(0) | TMR_CCR_CS_1(0)) :
  136. (TMR_CCR_CS_0(3) | TMR_CCR_CS_1(3));
  137. __raw_writel(ccr, mmp_timer_base + TMR_CCR);
  138. /* set timer 0 to periodic mode, and timer 1 to free-running mode */
  139. __raw_writel(0x2, mmp_timer_base + TMR_CMR);
  140. __raw_writel(0x1, mmp_timer_base + TMR_PLCR(0)); /* periodic */
  141. __raw_writel(0x7, mmp_timer_base + TMR_ICR(0)); /* clear status */
  142. __raw_writel(0x0, mmp_timer_base + TMR_IER(0));
  143. __raw_writel(0x0, mmp_timer_base + TMR_PLCR(1)); /* free-running */
  144. __raw_writel(0x7, mmp_timer_base + TMR_ICR(1)); /* clear status */
  145. __raw_writel(0x0, mmp_timer_base + TMR_IER(1));
  146. /* enable timer 1 counter */
  147. __raw_writel(0x2, mmp_timer_base + TMR_CER);
  148. }
  149. static struct irqaction timer_irq = {
  150. .name = "timer",
  151. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  152. .handler = timer_interrupt,
  153. .dev_id = &ckevt,
  154. };
  155. void __init timer_init(int irq)
  156. {
  157. timer_config();
  158. setup_sched_clock(mmp_read_sched_clock, 32, CLOCK_TICK_RATE);
  159. ckevt.cpumask = cpumask_of(0);
  160. setup_irq(irq, &timer_irq);
  161. clocksource_register_hz(&cksrc, CLOCK_TICK_RATE);
  162. clockevents_config_and_register(&ckevt, CLOCK_TICK_RATE,
  163. MIN_DELTA, MAX_DELTA);
  164. }
  165. #ifdef CONFIG_OF
  166. static struct of_device_id mmp_timer_dt_ids[] = {
  167. { .compatible = "mrvl,mmp-timer", },
  168. {}
  169. };
  170. void __init mmp_dt_init_timer(void)
  171. {
  172. struct device_node *np;
  173. int irq, ret;
  174. np = of_find_matching_node(NULL, mmp_timer_dt_ids);
  175. if (!np) {
  176. ret = -ENODEV;
  177. goto out;
  178. }
  179. irq = irq_of_parse_and_map(np, 0);
  180. if (!irq) {
  181. ret = -EINVAL;
  182. goto out;
  183. }
  184. mmp_timer_base = of_iomap(np, 0);
  185. if (!mmp_timer_base) {
  186. ret = -ENOMEM;
  187. goto out;
  188. }
  189. timer_init(irq);
  190. return;
  191. out:
  192. pr_err("Failed to get timer from device tree with error:%d\n", ret);
  193. }
  194. #endif