time.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <linux/sched_clock.h>
  30. #include <asm/mach/time.h>
  31. #include "addr-map.h"
  32. #include "regs-timers.h"
  33. #include "regs-apbc.h"
  34. #include "irqs.h"
  35. #include "cputype.h"
  36. #include "clock.h"
  37. #ifdef CONFIG_CPU_MMP2
  38. #define MMP_CLOCK_FREQ 6500000
  39. #else
  40. #define MMP_CLOCK_FREQ 3250000
  41. #endif
  42. #define TIMERS_VIRT_BASE TIMERS1_VIRT_BASE
  43. #define MAX_DELTA (0xfffffffe)
  44. #define MIN_DELTA (16)
  45. static void __iomem *mmp_timer_base = TIMERS_VIRT_BASE;
  46. /*
  47. * FIXME: the timer needs some delay to stablize the counter capture
  48. */
  49. static inline uint32_t timer_read(void)
  50. {
  51. int delay = 100;
  52. __raw_writel(1, mmp_timer_base + TMR_CVWR(1));
  53. while (delay--)
  54. cpu_relax();
  55. return __raw_readl(mmp_timer_base + TMR_CVWR(1));
  56. }
  57. static u64 notrace mmp_read_sched_clock(void)
  58. {
  59. return timer_read();
  60. }
  61. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  62. {
  63. struct clock_event_device *c = dev_id;
  64. /*
  65. * Clear pending interrupt status.
  66. */
  67. __raw_writel(0x01, mmp_timer_base + TMR_ICR(0));
  68. /*
  69. * Disable timer 0.
  70. */
  71. __raw_writel(0x02, mmp_timer_base + TMR_CER);
  72. c->event_handler(c);
  73. return IRQ_HANDLED;
  74. }
  75. static int timer_set_next_event(unsigned long delta,
  76. struct clock_event_device *dev)
  77. {
  78. unsigned long flags;
  79. local_irq_save(flags);
  80. /*
  81. * Disable timer 0.
  82. */
  83. __raw_writel(0x02, mmp_timer_base + TMR_CER);
  84. /*
  85. * Clear and enable timer match 0 interrupt.
  86. */
  87. __raw_writel(0x01, mmp_timer_base + TMR_ICR(0));
  88. __raw_writel(0x01, mmp_timer_base + TMR_IER(0));
  89. /*
  90. * Setup new clockevent timer value.
  91. */
  92. __raw_writel(delta - 1, mmp_timer_base + TMR_TN_MM(0, 0));
  93. /*
  94. * Enable timer 0.
  95. */
  96. __raw_writel(0x03, mmp_timer_base + TMR_CER);
  97. local_irq_restore(flags);
  98. return 0;
  99. }
  100. static int timer_set_shutdown(struct clock_event_device *evt)
  101. {
  102. unsigned long flags;
  103. local_irq_save(flags);
  104. /* disable the matching interrupt */
  105. __raw_writel(0x00, mmp_timer_base + TMR_IER(0));
  106. local_irq_restore(flags);
  107. return 0;
  108. }
  109. static struct clock_event_device ckevt = {
  110. .name = "clockevent",
  111. .features = CLOCK_EVT_FEAT_ONESHOT,
  112. .rating = 200,
  113. .set_next_event = timer_set_next_event,
  114. .set_state_shutdown = timer_set_shutdown,
  115. .set_state_oneshot = timer_set_shutdown,
  116. };
  117. static cycle_t clksrc_read(struct clocksource *cs)
  118. {
  119. return timer_read();
  120. }
  121. static struct clocksource cksrc = {
  122. .name = "clocksource",
  123. .rating = 200,
  124. .read = clksrc_read,
  125. .mask = CLOCKSOURCE_MASK(32),
  126. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  127. };
  128. static void __init timer_config(void)
  129. {
  130. uint32_t ccr = __raw_readl(mmp_timer_base + TMR_CCR);
  131. __raw_writel(0x0, mmp_timer_base + TMR_CER); /* disable */
  132. ccr &= (cpu_is_mmp2()) ? (TMR_CCR_CS_0(0) | TMR_CCR_CS_1(0)) :
  133. (TMR_CCR_CS_0(3) | TMR_CCR_CS_1(3));
  134. __raw_writel(ccr, mmp_timer_base + TMR_CCR);
  135. /* set timer 0 to periodic mode, and timer 1 to free-running mode */
  136. __raw_writel(0x2, mmp_timer_base + TMR_CMR);
  137. __raw_writel(0x1, mmp_timer_base + TMR_PLCR(0)); /* periodic */
  138. __raw_writel(0x7, mmp_timer_base + TMR_ICR(0)); /* clear status */
  139. __raw_writel(0x0, mmp_timer_base + TMR_IER(0));
  140. __raw_writel(0x0, mmp_timer_base + TMR_PLCR(1)); /* free-running */
  141. __raw_writel(0x7, mmp_timer_base + TMR_ICR(1)); /* clear status */
  142. __raw_writel(0x0, mmp_timer_base + TMR_IER(1));
  143. /* enable timer 1 counter */
  144. __raw_writel(0x2, mmp_timer_base + TMR_CER);
  145. }
  146. static struct irqaction timer_irq = {
  147. .name = "timer",
  148. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  149. .handler = timer_interrupt,
  150. .dev_id = &ckevt,
  151. };
  152. void __init timer_init(int irq)
  153. {
  154. timer_config();
  155. sched_clock_register(mmp_read_sched_clock, 32, MMP_CLOCK_FREQ);
  156. ckevt.cpumask = cpumask_of(0);
  157. setup_irq(irq, &timer_irq);
  158. clocksource_register_hz(&cksrc, MMP_CLOCK_FREQ);
  159. clockevents_config_and_register(&ckevt, MMP_CLOCK_FREQ,
  160. MIN_DELTA, MAX_DELTA);
  161. }
  162. #ifdef CONFIG_OF
  163. static const struct of_device_id mmp_timer_dt_ids[] = {
  164. { .compatible = "mrvl,mmp-timer", },
  165. {}
  166. };
  167. void __init mmp_dt_init_timer(void)
  168. {
  169. struct device_node *np;
  170. int irq, ret;
  171. np = of_find_matching_node(NULL, mmp_timer_dt_ids);
  172. if (!np) {
  173. ret = -ENODEV;
  174. goto out;
  175. }
  176. irq = irq_of_parse_and_map(np, 0);
  177. if (!irq) {
  178. ret = -EINVAL;
  179. goto out;
  180. }
  181. mmp_timer_base = of_iomap(np, 0);
  182. if (!mmp_timer_base) {
  183. ret = -ENOMEM;
  184. goto out;
  185. }
  186. timer_init(irq);
  187. return;
  188. out:
  189. pr_err("Failed to get timer from device tree with error:%d\n", ret);
  190. }
  191. #endif