time.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * arch/arm/mach-pnx4008/time.c
  3. *
  4. * PNX4008 Timers
  5. *
  6. * Authors: Vitaly Wool, Dmitry Chigirev, Grigory Tolstolytkin <source@mvista.com>
  7. *
  8. * 2005 (c) MontaVista Software, Inc. This file is licensed under
  9. * the terms of the GNU General Public License version 2. This program
  10. * is licensed "as is" without any warranty of any kind, whether express
  11. * or implied.
  12. */
  13. #include <linux/config.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/sched.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/module.h>
  21. #include <linux/kallsyms.h>
  22. #include <asm/system.h>
  23. #include <asm/hardware.h>
  24. #include <asm/io.h>
  25. #include <asm/leds.h>
  26. #include <asm/irq.h>
  27. #include <asm/mach/irq.h>
  28. #include <asm/mach/time.h>
  29. #include <linux/time.h>
  30. #include <linux/timex.h>
  31. #include <asm/errno.h>
  32. /*! Note: all timers are UPCOUNTING */
  33. /*!
  34. * Returns number of us since last clock interrupt. Note that interrupts
  35. * will have been disabled by do_gettimeoffset()
  36. */
  37. static unsigned long pnx4008_gettimeoffset(void)
  38. {
  39. u32 ticks_to_match =
  40. __raw_readl(HSTIM_MATCH0) - __raw_readl(HSTIM_COUNTER);
  41. u32 elapsed = LATCH - ticks_to_match;
  42. return (elapsed * (tick_nsec / 1000)) / LATCH;
  43. }
  44. /*!
  45. * IRQ handler for the timer
  46. */
  47. static irqreturn_t pnx4008_timer_interrupt(int irq, void *dev_id,
  48. struct pt_regs *regs)
  49. {
  50. if (__raw_readl(HSTIM_INT) & MATCH0_INT) {
  51. write_seqlock(&xtime_lock);
  52. do {
  53. timer_tick(regs);
  54. /*
  55. * this algorithm takes care of possible delay
  56. * for this interrupt handling longer than a normal
  57. * timer period
  58. */
  59. __raw_writel(__raw_readl(HSTIM_MATCH0) + LATCH,
  60. HSTIM_MATCH0);
  61. __raw_writel(MATCH0_INT, HSTIM_INT); /* clear interrupt */
  62. /*
  63. * The goal is to keep incrementing HSTIM_MATCH0
  64. * register until HSTIM_MATCH0 indicates time after
  65. * what HSTIM_COUNTER indicates.
  66. */
  67. } while ((signed)
  68. (__raw_readl(HSTIM_MATCH0) -
  69. __raw_readl(HSTIM_COUNTER)) < 0);
  70. write_sequnlock(&xtime_lock);
  71. }
  72. return IRQ_HANDLED;
  73. }
  74. static struct irqaction pnx4008_timer_irq = {
  75. .name = "PNX4008 Tick Timer",
  76. .flags = IRQF_DISABLED | IRQF_TIMER,
  77. .handler = pnx4008_timer_interrupt
  78. };
  79. /*!
  80. * Set up timer and timer interrupt.
  81. */
  82. static __init void pnx4008_setup_timer(void)
  83. {
  84. __raw_writel(RESET_COUNT, MSTIM_CTRL);
  85. while (__raw_readl(MSTIM_COUNTER)) ; /* wait for reset to complete. 100% guarantee event */
  86. __raw_writel(0, MSTIM_CTRL); /* stop the timer */
  87. __raw_writel(0, MSTIM_MCTRL);
  88. __raw_writel(RESET_COUNT, HSTIM_CTRL);
  89. while (__raw_readl(HSTIM_COUNTER)) ; /* wait for reset to complete. 100% guarantee event */
  90. __raw_writel(0, HSTIM_CTRL);
  91. __raw_writel(0, HSTIM_MCTRL);
  92. __raw_writel(0, HSTIM_CCR);
  93. __raw_writel(12, HSTIM_PMATCH); /* scale down to 1 MHZ */
  94. __raw_writel(LATCH, HSTIM_MATCH0);
  95. __raw_writel(MR0_INT, HSTIM_MCTRL);
  96. setup_irq(HSTIMER_INT, &pnx4008_timer_irq);
  97. __raw_writel(COUNT_ENAB | DEBUG_EN, HSTIM_CTRL); /*start timer, stop when JTAG active */
  98. }
  99. /* Timer Clock Control in PM register */
  100. #define TIMCLK_CTRL_REG IO_ADDRESS((PNX4008_PWRMAN_BASE + 0xBC))
  101. #define WATCHDOG_CLK_EN 1
  102. #define TIMER_CLK_EN 2 /* HS and MS timers? */
  103. static u32 timclk_ctrl_reg_save;
  104. void pnx4008_timer_suspend(void)
  105. {
  106. timclk_ctrl_reg_save = __raw_readl(TIMCLK_CTRL_REG);
  107. __raw_writel(0, TIMCLK_CTRL_REG); /* disable timers */
  108. }
  109. void pnx4008_timer_resume(void)
  110. {
  111. __raw_writel(timclk_ctrl_reg_save, TIMCLK_CTRL_REG); /* enable timers */
  112. }
  113. struct sys_timer pnx4008_timer = {
  114. .init = pnx4008_setup_timer,
  115. .offset = pnx4008_gettimeoffset,
  116. .suspend = pnx4008_timer_suspend,
  117. .resume = pnx4008_timer_resume,
  118. };