timer.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 int lpc32xx_clkevt_next_event(unsigned long delta,
  31. struct clock_event_device *dev)
  32. {
  33. __raw_writel(LPC32XX_TIMER_CNTR_TCR_RESET,
  34. LPC32XX_TIMER_TCR(LPC32XX_TIMER0_BASE));
  35. __raw_writel(delta, LPC32XX_TIMER_PR(LPC32XX_TIMER0_BASE));
  36. __raw_writel(LPC32XX_TIMER_CNTR_TCR_EN,
  37. LPC32XX_TIMER_TCR(LPC32XX_TIMER0_BASE));
  38. return 0;
  39. }
  40. static void lpc32xx_clkevt_mode(enum clock_event_mode mode,
  41. struct clock_event_device *dev)
  42. {
  43. switch (mode) {
  44. case CLOCK_EVT_MODE_PERIODIC:
  45. WARN_ON(1);
  46. break;
  47. case CLOCK_EVT_MODE_ONESHOT:
  48. case CLOCK_EVT_MODE_SHUTDOWN:
  49. /*
  50. * Disable the timer. When using oneshot, we must also
  51. * disable the timer to wait for the first call to
  52. * set_next_event().
  53. */
  54. __raw_writel(0, LPC32XX_TIMER_TCR(LPC32XX_TIMER0_BASE));
  55. break;
  56. case CLOCK_EVT_MODE_UNUSED:
  57. case CLOCK_EVT_MODE_RESUME:
  58. break;
  59. }
  60. }
  61. static struct clock_event_device lpc32xx_clkevt = {
  62. .name = "lpc32xx_clkevt",
  63. .features = CLOCK_EVT_FEAT_ONESHOT,
  64. .shift = 32,
  65. .rating = 300,
  66. .set_next_event = lpc32xx_clkevt_next_event,
  67. .set_mode = lpc32xx_clkevt_mode,
  68. };
  69. static irqreturn_t lpc32xx_timer_interrupt(int irq, void *dev_id)
  70. {
  71. struct clock_event_device *evt = &lpc32xx_clkevt;
  72. /* Clear match */
  73. __raw_writel(LPC32XX_TIMER_CNTR_MTCH_BIT(0),
  74. LPC32XX_TIMER_IR(LPC32XX_TIMER0_BASE));
  75. evt->event_handler(evt);
  76. return IRQ_HANDLED;
  77. }
  78. static struct irqaction lpc32xx_timer_irq = {
  79. .name = "LPC32XX Timer Tick",
  80. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  81. .handler = lpc32xx_timer_interrupt,
  82. };
  83. /*
  84. * The clock management driver isn't initialized at this point, so the
  85. * clocks need to be enabled here manually and then tagged as used in
  86. * the clock driver initialization
  87. */
  88. static void __init lpc32xx_timer_init(void)
  89. {
  90. u32 clkrate, pllreg;
  91. /* Enable timer clock */
  92. __raw_writel(LPC32XX_CLKPWR_TMRPWMCLK_TIMER0_EN |
  93. LPC32XX_CLKPWR_TMRPWMCLK_TIMER1_EN,
  94. LPC32XX_CLKPWR_TIMERS_PWMS_CLK_CTRL_1);
  95. /*
  96. * The clock driver isn't initialized at this point. So determine if
  97. * the SYSCLK is driven from the PLL397 or main oscillator and then use
  98. * it to compute the PLL frequency and the PCLK divider to get the base
  99. * timer rates. This rate is needed to compute the tick rate.
  100. */
  101. if (clk_is_sysclk_mainosc() != 0)
  102. clkrate = LPC32XX_MAIN_OSC_FREQ;
  103. else
  104. clkrate = 397 * LPC32XX_CLOCK_OSC_FREQ;
  105. /* Get ARM HCLKPLL register and convert it into a frequency */
  106. pllreg = __raw_readl(LPC32XX_CLKPWR_HCLKPLL_CTRL) & 0x1FFFF;
  107. clkrate = clk_get_pllrate_from_reg(clkrate, pllreg);
  108. /* Get PCLK divider and divide ARM PLL clock by it to get timer rate */
  109. clkrate = clkrate / clk_get_pclk_div();
  110. /* Initial timer setup */
  111. __raw_writel(0, LPC32XX_TIMER_TCR(LPC32XX_TIMER0_BASE));
  112. __raw_writel(LPC32XX_TIMER_CNTR_MTCH_BIT(0),
  113. LPC32XX_TIMER_IR(LPC32XX_TIMER0_BASE));
  114. __raw_writel(1, LPC32XX_TIMER_MR0(LPC32XX_TIMER0_BASE));
  115. __raw_writel(LPC32XX_TIMER_CNTR_MCR_MTCH(0) |
  116. LPC32XX_TIMER_CNTR_MCR_STOP(0) |
  117. LPC32XX_TIMER_CNTR_MCR_RESET(0),
  118. LPC32XX_TIMER_MCR(LPC32XX_TIMER0_BASE));
  119. /* Setup tick interrupt */
  120. setup_irq(IRQ_LPC32XX_TIMER0, &lpc32xx_timer_irq);
  121. /* Setup the clockevent structure. */
  122. lpc32xx_clkevt.mult = div_sc(clkrate, NSEC_PER_SEC,
  123. lpc32xx_clkevt.shift);
  124. lpc32xx_clkevt.max_delta_ns = clockevent_delta2ns(-1,
  125. &lpc32xx_clkevt);
  126. lpc32xx_clkevt.min_delta_ns = clockevent_delta2ns(1,
  127. &lpc32xx_clkevt) + 1;
  128. lpc32xx_clkevt.cpumask = cpumask_of(0);
  129. clockevents_register_device(&lpc32xx_clkevt);
  130. /* Use timer1 as clock source. */
  131. __raw_writel(LPC32XX_TIMER_CNTR_TCR_RESET,
  132. LPC32XX_TIMER_TCR(LPC32XX_TIMER1_BASE));
  133. __raw_writel(0, LPC32XX_TIMER_PR(LPC32XX_TIMER1_BASE));
  134. __raw_writel(0, LPC32XX_TIMER_MCR(LPC32XX_TIMER1_BASE));
  135. __raw_writel(LPC32XX_TIMER_CNTR_TCR_EN,
  136. LPC32XX_TIMER_TCR(LPC32XX_TIMER1_BASE));
  137. clocksource_mmio_init(LPC32XX_TIMER_TC(LPC32XX_TIMER1_BASE),
  138. "lpc32xx_clksrc", clkrate, 300, 32, clocksource_mmio_readl_up);
  139. }
  140. struct sys_timer lpc32xx_timer = {
  141. .init = &lpc32xx_timer_init,
  142. };