timer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 int lpc32xx_shutdown(struct clock_event_device *evt)
  41. {
  42. /*
  43. * Disable the timer. When using oneshot, we must also
  44. * disable the timer to wait for the first call to
  45. * set_next_event().
  46. */
  47. __raw_writel(0, LPC32XX_TIMER_TCR(LPC32XX_TIMER0_BASE));
  48. return 0;
  49. }
  50. static struct clock_event_device lpc32xx_clkevt = {
  51. .name = "lpc32xx_clkevt",
  52. .features = CLOCK_EVT_FEAT_ONESHOT,
  53. .rating = 300,
  54. .set_next_event = lpc32xx_clkevt_next_event,
  55. .set_state_shutdown = lpc32xx_shutdown,
  56. .set_state_oneshot = lpc32xx_shutdown,
  57. };
  58. static irqreturn_t lpc32xx_timer_interrupt(int irq, void *dev_id)
  59. {
  60. struct clock_event_device *evt = &lpc32xx_clkevt;
  61. /* Clear match */
  62. __raw_writel(LPC32XX_TIMER_CNTR_MTCH_BIT(0),
  63. LPC32XX_TIMER_IR(LPC32XX_TIMER0_BASE));
  64. evt->event_handler(evt);
  65. return IRQ_HANDLED;
  66. }
  67. static struct irqaction lpc32xx_timer_irq = {
  68. .name = "LPC32XX Timer Tick",
  69. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  70. .handler = lpc32xx_timer_interrupt,
  71. };
  72. /*
  73. * The clock management driver isn't initialized at this point, so the
  74. * clocks need to be enabled here manually and then tagged as used in
  75. * the clock driver initialization
  76. */
  77. void __init lpc32xx_timer_init(void)
  78. {
  79. u32 clkrate, pllreg;
  80. /* Enable timer clock */
  81. __raw_writel(LPC32XX_CLKPWR_TMRPWMCLK_TIMER0_EN |
  82. LPC32XX_CLKPWR_TMRPWMCLK_TIMER1_EN,
  83. LPC32XX_CLKPWR_TIMERS_PWMS_CLK_CTRL_1);
  84. /*
  85. * The clock driver isn't initialized at this point. So determine if
  86. * the SYSCLK is driven from the PLL397 or main oscillator and then use
  87. * it to compute the PLL frequency and the PCLK divider to get the base
  88. * timer rates. This rate is needed to compute the tick rate.
  89. */
  90. if (clk_is_sysclk_mainosc() != 0)
  91. clkrate = LPC32XX_MAIN_OSC_FREQ;
  92. else
  93. clkrate = 397 * LPC32XX_CLOCK_OSC_FREQ;
  94. /* Get ARM HCLKPLL register and convert it into a frequency */
  95. pllreg = __raw_readl(LPC32XX_CLKPWR_HCLKPLL_CTRL) & 0x1FFFF;
  96. clkrate = clk_get_pllrate_from_reg(clkrate, pllreg);
  97. /* Get PCLK divider and divide ARM PLL clock by it to get timer rate */
  98. clkrate = clkrate / clk_get_pclk_div();
  99. /* Initial timer setup */
  100. __raw_writel(0, LPC32XX_TIMER_TCR(LPC32XX_TIMER0_BASE));
  101. __raw_writel(LPC32XX_TIMER_CNTR_MTCH_BIT(0),
  102. LPC32XX_TIMER_IR(LPC32XX_TIMER0_BASE));
  103. __raw_writel(1, LPC32XX_TIMER_MR0(LPC32XX_TIMER0_BASE));
  104. __raw_writel(LPC32XX_TIMER_CNTR_MCR_MTCH(0) |
  105. LPC32XX_TIMER_CNTR_MCR_STOP(0) |
  106. LPC32XX_TIMER_CNTR_MCR_RESET(0),
  107. LPC32XX_TIMER_MCR(LPC32XX_TIMER0_BASE));
  108. /* Setup tick interrupt */
  109. setup_irq(IRQ_LPC32XX_TIMER0, &lpc32xx_timer_irq);
  110. /* Setup the clockevent structure. */
  111. lpc32xx_clkevt.cpumask = cpumask_of(0);
  112. clockevents_config_and_register(&lpc32xx_clkevt, clkrate, 1, -1);
  113. /* Use timer1 as clock source. */
  114. __raw_writel(LPC32XX_TIMER_CNTR_TCR_RESET,
  115. LPC32XX_TIMER_TCR(LPC32XX_TIMER1_BASE));
  116. __raw_writel(0, LPC32XX_TIMER_PR(LPC32XX_TIMER1_BASE));
  117. __raw_writel(0, LPC32XX_TIMER_MCR(LPC32XX_TIMER1_BASE));
  118. __raw_writel(LPC32XX_TIMER_CNTR_TCR_EN,
  119. LPC32XX_TIMER_TCR(LPC32XX_TIMER1_BASE));
  120. clocksource_mmio_init(LPC32XX_TIMER_TC(LPC32XX_TIMER1_BASE),
  121. "lpc32xx_clksrc", clkrate, 300, 32, clocksource_mmio_readl_up);
  122. }