timer.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * arch/arm/mach-lpc32xx/timer.c
  3. *
  4. * Author: Kevin Wells <kevin.wells@nxp.com>
  5. *
  6. * Copyright (C) 2009 - 2010 NXP Semiconductors
  7. * Copyright (C) 2009 Fontys University of Applied Sciences, Eindhoven
  8. * Ed Schouten <e.schouten@fontys.nl>
  9. * Laurens Timmermans <l.timmermans@fontys.nl>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. */
  21. #include <linux/interrupt.h>
  22. #include <linux/irq.h>
  23. #include <linux/time.h>
  24. #include <linux/err.h>
  25. #include <linux/clockchips.h>
  26. #include <asm/mach/time.h>
  27. #include <mach/hardware.h>
  28. #include <mach/platform.h>
  29. #include "common.h"
  30. static cycle_t lpc32xx_clksrc_read(struct clocksource *cs)
  31. {
  32. return (cycle_t)__raw_readl(LCP32XX_TIMER_TC(LPC32XX_TIMER1_BASE));
  33. }
  34. static struct clocksource lpc32xx_clksrc = {
  35. .name = "lpc32xx_clksrc",
  36. .shift = 24,
  37. .rating = 300,
  38. .read = lpc32xx_clksrc_read,
  39. .mask = CLOCKSOURCE_MASK(32),
  40. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  41. };
  42. static int lpc32xx_clkevt_next_event(unsigned long delta,
  43. struct clock_event_device *dev)
  44. {
  45. __raw_writel(LCP32XX_TIMER_CNTR_TCR_RESET,
  46. LCP32XX_TIMER_TCR(LPC32XX_TIMER0_BASE));
  47. __raw_writel(delta, LCP32XX_TIMER_PR(LPC32XX_TIMER0_BASE));
  48. __raw_writel(LCP32XX_TIMER_CNTR_TCR_EN,
  49. LCP32XX_TIMER_TCR(LPC32XX_TIMER0_BASE));
  50. return 0;
  51. }
  52. static void lpc32xx_clkevt_mode(enum clock_event_mode mode,
  53. struct clock_event_device *dev)
  54. {
  55. switch (mode) {
  56. case CLOCK_EVT_MODE_PERIODIC:
  57. WARN_ON(1);
  58. break;
  59. case CLOCK_EVT_MODE_ONESHOT:
  60. case CLOCK_EVT_MODE_SHUTDOWN:
  61. /*
  62. * Disable the timer. When using oneshot, we must also
  63. * disable the timer to wait for the first call to
  64. * set_next_event().
  65. */
  66. __raw_writel(0, LCP32XX_TIMER_TCR(LPC32XX_TIMER0_BASE));
  67. break;
  68. case CLOCK_EVT_MODE_UNUSED:
  69. case CLOCK_EVT_MODE_RESUME:
  70. break;
  71. }
  72. }
  73. static struct clock_event_device lpc32xx_clkevt = {
  74. .name = "lpc32xx_clkevt",
  75. .features = CLOCK_EVT_FEAT_ONESHOT,
  76. .shift = 32,
  77. .rating = 300,
  78. .set_next_event = lpc32xx_clkevt_next_event,
  79. .set_mode = lpc32xx_clkevt_mode,
  80. };
  81. static irqreturn_t lpc32xx_timer_interrupt(int irq, void *dev_id)
  82. {
  83. struct clock_event_device *evt = &lpc32xx_clkevt;
  84. /* Clear match */
  85. __raw_writel(LCP32XX_TIMER_CNTR_MTCH_BIT(0),
  86. LCP32XX_TIMER_IR(LPC32XX_TIMER0_BASE));
  87. evt->event_handler(evt);
  88. return IRQ_HANDLED;
  89. }
  90. static struct irqaction lpc32xx_timer_irq = {
  91. .name = "LPC32XX Timer Tick",
  92. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  93. .handler = lpc32xx_timer_interrupt,
  94. };
  95. /*
  96. * The clock management driver isn't initialized at this point, so the
  97. * clocks need to be enabled here manually and then tagged as used in
  98. * the clock driver initialization
  99. */
  100. static void __init lpc32xx_timer_init(void)
  101. {
  102. u32 clkrate, pllreg;
  103. /* Enable timer clock */
  104. __raw_writel(LPC32XX_CLKPWR_TMRPWMCLK_TIMER0_EN |
  105. LPC32XX_CLKPWR_TMRPWMCLK_TIMER1_EN,
  106. LPC32XX_CLKPWR_TIMERS_PWMS_CLK_CTRL_1);
  107. /*
  108. * The clock driver isn't initialized at this point. So determine if
  109. * the SYSCLK is driven from the PLL397 or main oscillator and then use
  110. * it to compute the PLL frequency and the PCLK divider to get the base
  111. * timer rates. This rate is needed to compute the tick rate.
  112. */
  113. if (clk_is_sysclk_mainosc() != 0)
  114. clkrate = LPC32XX_MAIN_OSC_FREQ;
  115. else
  116. clkrate = 397 * LPC32XX_CLOCK_OSC_FREQ;
  117. /* Get ARM HCLKPLL register and convert it into a frequency */
  118. pllreg = __raw_readl(LPC32XX_CLKPWR_HCLKPLL_CTRL) & 0x1FFFF;
  119. clkrate = clk_get_pllrate_from_reg(clkrate, pllreg);
  120. /* Get PCLK divider and divide ARM PLL clock by it to get timer rate */
  121. clkrate = clkrate / clk_get_pclk_div();
  122. /* Initial timer setup */
  123. __raw_writel(0, LCP32XX_TIMER_TCR(LPC32XX_TIMER0_BASE));
  124. __raw_writel(LCP32XX_TIMER_CNTR_MTCH_BIT(0),
  125. LCP32XX_TIMER_IR(LPC32XX_TIMER0_BASE));
  126. __raw_writel(1, LCP32XX_TIMER_MR0(LPC32XX_TIMER0_BASE));
  127. __raw_writel(LCP32XX_TIMER_CNTR_MCR_MTCH(0) |
  128. LCP32XX_TIMER_CNTR_MCR_STOP(0) |
  129. LCP32XX_TIMER_CNTR_MCR_RESET(0),
  130. LCP32XX_TIMER_MCR(LPC32XX_TIMER0_BASE));
  131. /* Setup tick interrupt */
  132. setup_irq(IRQ_LPC32XX_TIMER0, &lpc32xx_timer_irq);
  133. /* Setup the clockevent structure. */
  134. lpc32xx_clkevt.mult = div_sc(clkrate, NSEC_PER_SEC,
  135. lpc32xx_clkevt.shift);
  136. lpc32xx_clkevt.max_delta_ns = clockevent_delta2ns(-1,
  137. &lpc32xx_clkevt);
  138. lpc32xx_clkevt.min_delta_ns = clockevent_delta2ns(1,
  139. &lpc32xx_clkevt) + 1;
  140. lpc32xx_clkevt.cpumask = cpumask_of(0);
  141. clockevents_register_device(&lpc32xx_clkevt);
  142. /* Use timer1 as clock source. */
  143. __raw_writel(LCP32XX_TIMER_CNTR_TCR_RESET,
  144. LCP32XX_TIMER_TCR(LPC32XX_TIMER1_BASE));
  145. __raw_writel(0, LCP32XX_TIMER_PR(LPC32XX_TIMER1_BASE));
  146. __raw_writel(0, LCP32XX_TIMER_MCR(LPC32XX_TIMER1_BASE));
  147. __raw_writel(LCP32XX_TIMER_CNTR_TCR_EN,
  148. LCP32XX_TIMER_TCR(LPC32XX_TIMER1_BASE));
  149. lpc32xx_clksrc.mult = clocksource_hz2mult(clkrate,
  150. lpc32xx_clksrc.shift);
  151. clocksource_register(&lpc32xx_clksrc);
  152. }
  153. struct sys_timer lpc32xx_timer = {
  154. .init = &lpc32xx_timer_init,
  155. };