time.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/mach-mmp/time.c
  4. *
  5. * Support for clocksource and clockevents
  6. *
  7. * Copyright (C) 2008 Marvell International Ltd.
  8. * All rights reserved.
  9. *
  10. * 2008-04-11: Jason Chagas <Jason.chagas@marvell.com>
  11. * 2008-10-08: Bin Yang <bin.yang@marvell.com>
  12. *
  13. * The timers module actually includes three timers, each timer with up to
  14. * three match comparators. Timer #0 is used here in free-running mode as
  15. * the clock source, and match comparator #1 used as clock event device.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/clockchips.h>
  21. #include <linux/clk.h>
  22. #include <linux/io.h>
  23. #include <linux/irq.h>
  24. #include <linux/of.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/sched_clock.h>
  28. #include <asm/mach/time.h>
  29. #include "regs-timers.h"
  30. #include <linux/soc/mmp/cputype.h>
  31. #define MAX_DELTA (0xfffffffe)
  32. #define MIN_DELTA (16)
  33. static void __iomem *mmp_timer_base;
  34. /*
  35. * Read the timer through the CVWR register. Delay is required after requesting
  36. * a read. The CR register cannot be directly read due to metastability issues
  37. * documented in the PXA168 software manual.
  38. */
  39. static inline uint32_t timer_read(void)
  40. {
  41. uint32_t val;
  42. int delay = 3;
  43. __raw_writel(1, mmp_timer_base + TMR_CVWR(1));
  44. while (delay--)
  45. val = __raw_readl(mmp_timer_base + TMR_CVWR(1));
  46. return val;
  47. }
  48. static u64 notrace mmp_read_sched_clock(void)
  49. {
  50. return timer_read();
  51. }
  52. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  53. {
  54. struct clock_event_device *c = dev_id;
  55. /*
  56. * Clear pending interrupt status.
  57. */
  58. __raw_writel(0x01, mmp_timer_base + TMR_ICR(0));
  59. /*
  60. * Disable timer 0.
  61. */
  62. __raw_writel(0x02, mmp_timer_base + TMR_CER);
  63. c->event_handler(c);
  64. return IRQ_HANDLED;
  65. }
  66. static int timer_set_next_event(unsigned long delta,
  67. struct clock_event_device *dev)
  68. {
  69. unsigned long flags;
  70. local_irq_save(flags);
  71. /*
  72. * Disable timer 0.
  73. */
  74. __raw_writel(0x02, mmp_timer_base + TMR_CER);
  75. /*
  76. * Clear and enable timer match 0 interrupt.
  77. */
  78. __raw_writel(0x01, mmp_timer_base + TMR_ICR(0));
  79. __raw_writel(0x01, mmp_timer_base + TMR_IER(0));
  80. /*
  81. * Setup new clockevent timer value.
  82. */
  83. __raw_writel(delta - 1, mmp_timer_base + TMR_TN_MM(0, 0));
  84. /*
  85. * Enable timer 0.
  86. */
  87. __raw_writel(0x03, mmp_timer_base + TMR_CER);
  88. local_irq_restore(flags);
  89. return 0;
  90. }
  91. static int timer_set_shutdown(struct clock_event_device *evt)
  92. {
  93. unsigned long flags;
  94. local_irq_save(flags);
  95. /* disable the matching interrupt */
  96. __raw_writel(0x00, mmp_timer_base + TMR_IER(0));
  97. local_irq_restore(flags);
  98. return 0;
  99. }
  100. static struct clock_event_device ckevt = {
  101. .name = "clockevent",
  102. .features = CLOCK_EVT_FEAT_ONESHOT,
  103. .rating = 200,
  104. .set_next_event = timer_set_next_event,
  105. .set_state_shutdown = timer_set_shutdown,
  106. .set_state_oneshot = timer_set_shutdown,
  107. };
  108. static u64 clksrc_read(struct clocksource *cs)
  109. {
  110. return timer_read();
  111. }
  112. static struct clocksource cksrc = {
  113. .name = "clocksource",
  114. .rating = 200,
  115. .read = clksrc_read,
  116. .mask = CLOCKSOURCE_MASK(32),
  117. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  118. };
  119. static void __init timer_config(void)
  120. {
  121. uint32_t ccr = __raw_readl(mmp_timer_base + TMR_CCR);
  122. __raw_writel(0x0, mmp_timer_base + TMR_CER); /* disable */
  123. ccr &= (cpu_is_mmp2() || cpu_is_mmp3()) ?
  124. (TMR_CCR_CS_0(0) | TMR_CCR_CS_1(0)) :
  125. (TMR_CCR_CS_0(3) | TMR_CCR_CS_1(3));
  126. __raw_writel(ccr, mmp_timer_base + TMR_CCR);
  127. /* set timer 0 to periodic mode, and timer 1 to free-running mode */
  128. __raw_writel(0x2, mmp_timer_base + TMR_CMR);
  129. __raw_writel(0x1, mmp_timer_base + TMR_PLCR(0)); /* periodic */
  130. __raw_writel(0x7, mmp_timer_base + TMR_ICR(0)); /* clear status */
  131. __raw_writel(0x0, mmp_timer_base + TMR_IER(0));
  132. __raw_writel(0x0, mmp_timer_base + TMR_PLCR(1)); /* free-running */
  133. __raw_writel(0x7, mmp_timer_base + TMR_ICR(1)); /* clear status */
  134. __raw_writel(0x0, mmp_timer_base + TMR_IER(1));
  135. /* enable timer 1 counter */
  136. __raw_writel(0x2, mmp_timer_base + TMR_CER);
  137. }
  138. static void __init mmp_timer_init(int irq, unsigned long rate)
  139. {
  140. timer_config();
  141. sched_clock_register(mmp_read_sched_clock, 32, rate);
  142. ckevt.cpumask = cpumask_of(0);
  143. if (request_irq(irq, timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
  144. "timer", &ckevt))
  145. pr_err("Failed to request irq %d (timer)\n", irq);
  146. clocksource_register_hz(&cksrc, rate);
  147. clockevents_config_and_register(&ckevt, rate, MIN_DELTA, MAX_DELTA);
  148. }
  149. static int __init mmp_dt_init_timer(struct device_node *np)
  150. {
  151. struct clk *clk;
  152. int irq, ret;
  153. unsigned long rate;
  154. clk = of_clk_get(np, 0);
  155. if (!IS_ERR(clk)) {
  156. ret = clk_prepare_enable(clk);
  157. if (ret)
  158. return ret;
  159. rate = clk_get_rate(clk);
  160. } else if (cpu_is_pj4()) {
  161. rate = 6500000;
  162. } else {
  163. rate = 3250000;
  164. }
  165. irq = irq_of_parse_and_map(np, 0);
  166. if (!irq)
  167. return -EINVAL;
  168. mmp_timer_base = of_iomap(np, 0);
  169. if (!mmp_timer_base)
  170. return -ENOMEM;
  171. mmp_timer_init(irq, rate);
  172. return 0;
  173. }
  174. TIMER_OF_DECLARE(mmp_timer, "mrvl,mmp-timer", mmp_dt_init_timer);