timer.c 5.2 KB

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