timer.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * linux/arch/arm/mach-nomadik/timer.c
  3. *
  4. * Copyright (C) 2008 STMicroelectronics
  5. * Copyright (C) 2010 Alessandro Rubini
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2, as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/io.h>
  15. #include <linux/clockchips.h>
  16. #include <linux/clk.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/err.h>
  19. #include <asm/mach/time.h>
  20. #include <plat/mtu.h>
  21. void __iomem *mtu_base; /* ssigned by machine code */
  22. /*
  23. * Kernel assumes that sched_clock can be called early
  24. * but the MTU may not yet be initialized.
  25. */
  26. static cycle_t nmdk_read_timer_dummy(struct clocksource *cs)
  27. {
  28. return 0;
  29. }
  30. /* clocksource: MTU decrements, so we negate the value being read. */
  31. static cycle_t nmdk_read_timer(struct clocksource *cs)
  32. {
  33. return -readl(mtu_base + MTU_VAL(0));
  34. }
  35. static struct clocksource nmdk_clksrc = {
  36. .name = "mtu_0",
  37. .rating = 200,
  38. .read = nmdk_read_timer_dummy,
  39. .mask = CLOCKSOURCE_MASK(32),
  40. .shift = 20,
  41. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  42. };
  43. /*
  44. * Override the global weak sched_clock symbol with this
  45. * local implementation which uses the clocksource to get some
  46. * better resolution when scheduling the kernel. We accept that
  47. * this wraps around for now, since it is just a relative time
  48. * stamp. (Inspired by OMAP implementation.)
  49. */
  50. unsigned long long notrace sched_clock(void)
  51. {
  52. return clocksource_cyc2ns(nmdk_clksrc.read(
  53. &nmdk_clksrc),
  54. nmdk_clksrc.mult,
  55. nmdk_clksrc.shift);
  56. }
  57. /* Clockevent device: use one-shot mode */
  58. static void nmdk_clkevt_mode(enum clock_event_mode mode,
  59. struct clock_event_device *dev)
  60. {
  61. u32 cr;
  62. switch (mode) {
  63. case CLOCK_EVT_MODE_PERIODIC:
  64. pr_err("%s: periodic mode not supported\n", __func__);
  65. break;
  66. case CLOCK_EVT_MODE_ONESHOT:
  67. /* Load highest value, enable device, enable interrupts */
  68. cr = readl(mtu_base + MTU_CR(1));
  69. writel(0, mtu_base + MTU_LR(1));
  70. writel(cr | MTU_CRn_ENA, mtu_base + MTU_CR(1));
  71. writel(0x2, mtu_base + MTU_IMSC);
  72. break;
  73. case CLOCK_EVT_MODE_SHUTDOWN:
  74. case CLOCK_EVT_MODE_UNUSED:
  75. /* disable irq */
  76. writel(0, mtu_base + MTU_IMSC);
  77. break;
  78. case CLOCK_EVT_MODE_RESUME:
  79. break;
  80. }
  81. }
  82. static int nmdk_clkevt_next(unsigned long evt, struct clock_event_device *ev)
  83. {
  84. /* writing the value has immediate effect */
  85. writel(evt, mtu_base + MTU_LR(1));
  86. return 0;
  87. }
  88. static struct clock_event_device nmdk_clkevt = {
  89. .name = "mtu_1",
  90. .features = CLOCK_EVT_FEAT_ONESHOT,
  91. .shift = 32,
  92. .rating = 200,
  93. .set_mode = nmdk_clkevt_mode,
  94. .set_next_event = nmdk_clkevt_next,
  95. };
  96. /*
  97. * IRQ Handler for timer 1 of the MTU block.
  98. */
  99. static irqreturn_t nmdk_timer_interrupt(int irq, void *dev_id)
  100. {
  101. struct clock_event_device *evdev = dev_id;
  102. writel(1 << 1, mtu_base + MTU_ICR); /* Interrupt clear reg */
  103. evdev->event_handler(evdev);
  104. return IRQ_HANDLED;
  105. }
  106. static struct irqaction nmdk_timer_irq = {
  107. .name = "Nomadik Timer Tick",
  108. .flags = IRQF_DISABLED | IRQF_TIMER,
  109. .handler = nmdk_timer_interrupt,
  110. .dev_id = &nmdk_clkevt,
  111. };
  112. void __init nmdk_timer_init(void)
  113. {
  114. unsigned long rate;
  115. struct clk *clk0;
  116. struct clk *clk1;
  117. u32 cr;
  118. clk0 = clk_get_sys("mtu0", NULL);
  119. BUG_ON(IS_ERR(clk0));
  120. clk1 = clk_get_sys("mtu1", NULL);
  121. BUG_ON(IS_ERR(clk1));
  122. clk_enable(clk0);
  123. clk_enable(clk1);
  124. /*
  125. * Tick rate is 2.4MHz for Nomadik and 110MHz for ux500:
  126. * use a divide-by-16 counter if it's more than 16MHz
  127. */
  128. cr = MTU_CRn_32BITS;;
  129. rate = clk_get_rate(clk0);
  130. if (rate > 16 << 20) {
  131. rate /= 16;
  132. cr |= MTU_CRn_PRESCALE_16;
  133. } else {
  134. cr |= MTU_CRn_PRESCALE_1;
  135. }
  136. /* Timer 0 is the free running clocksource */
  137. writel(cr, mtu_base + MTU_CR(0));
  138. writel(0, mtu_base + MTU_LR(0));
  139. writel(0, mtu_base + MTU_BGLR(0));
  140. writel(cr | MTU_CRn_ENA, mtu_base + MTU_CR(0));
  141. nmdk_clksrc.mult = clocksource_hz2mult(rate, nmdk_clksrc.shift);
  142. /* Now the scheduling clock is ready */
  143. nmdk_clksrc.read = nmdk_read_timer;
  144. if (clocksource_register(&nmdk_clksrc))
  145. pr_err("timer: failed to initialize clock source %s\n",
  146. nmdk_clksrc.name);
  147. /* Timer 1 is used for events, fix according to rate */
  148. cr = MTU_CRn_32BITS;
  149. rate = clk_get_rate(clk1);
  150. if (rate > 16 << 20) {
  151. rate /= 16;
  152. cr |= MTU_CRn_PRESCALE_16;
  153. } else {
  154. cr |= MTU_CRn_PRESCALE_1;
  155. }
  156. writel(cr | MTU_CRn_ONESHOT, mtu_base + MTU_CR(1)); /* off, currently */
  157. nmdk_clkevt.mult = div_sc(rate, NSEC_PER_SEC, nmdk_clkevt.shift);
  158. nmdk_clkevt.max_delta_ns =
  159. clockevent_delta2ns(0xffffffff, &nmdk_clkevt);
  160. nmdk_clkevt.min_delta_ns =
  161. clockevent_delta2ns(0x00000002, &nmdk_clkevt);
  162. nmdk_clkevt.cpumask = cpumask_of(0);
  163. /* Register irq and clockevents */
  164. setup_irq(IRQ_MTU0, &nmdk_timer_irq);
  165. clockevents_register_device(&nmdk_clkevt);
  166. }