timer.c 5.0 KB

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