time.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * arch/arm/mach-ks8695/time.c
  3. *
  4. * Copyright (C) 2006 Ben Dooks <ben@simtec.co.uk>
  5. * Copyright (C) 2006 Simtec Electronics
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/kernel.h>
  25. #include <linux/sched.h>
  26. #include <linux/io.h>
  27. #include <asm/mach/time.h>
  28. #include <asm/system_misc.h>
  29. #include <mach/regs-timer.h>
  30. #include <mach/regs-irq.h>
  31. #include "generic.h"
  32. /*
  33. * Returns number of ms since last clock interrupt. Note that interrupts
  34. * will have been disabled by do_gettimeoffset()
  35. */
  36. static unsigned long ks8695_gettimeoffset (void)
  37. {
  38. unsigned long elapsed, tick2, intpending;
  39. /*
  40. * Get the current number of ticks. Note that there is a race
  41. * condition between us reading the timer and checking for an
  42. * interrupt. We solve this by ensuring that the counter has not
  43. * reloaded between our two reads.
  44. */
  45. elapsed = __raw_readl(KS8695_TMR_VA + KS8695_T1TC) + __raw_readl(KS8695_TMR_VA + KS8695_T1PD);
  46. do {
  47. tick2 = elapsed;
  48. intpending = __raw_readl(KS8695_IRQ_VA + KS8695_INTST) & (1 << KS8695_IRQ_TIMER1);
  49. elapsed = __raw_readl(KS8695_TMR_VA + KS8695_T1TC) + __raw_readl(KS8695_TMR_VA + KS8695_T1PD);
  50. } while (elapsed > tick2);
  51. /* Convert to number of ticks expired (not remaining) */
  52. elapsed = (CLOCK_TICK_RATE / HZ) - elapsed;
  53. /* Is interrupt pending? If so, then timer has been reloaded already. */
  54. if (intpending)
  55. elapsed += (CLOCK_TICK_RATE / HZ);
  56. /* Convert ticks to usecs */
  57. return (unsigned long)(elapsed * (tick_nsec / 1000)) / LATCH;
  58. }
  59. /*
  60. * IRQ handler for the timer.
  61. */
  62. static irqreturn_t ks8695_timer_interrupt(int irq, void *dev_id)
  63. {
  64. timer_tick();
  65. return IRQ_HANDLED;
  66. }
  67. static struct irqaction ks8695_timer_irq = {
  68. .name = "ks8695_tick",
  69. .flags = IRQF_DISABLED | IRQF_TIMER,
  70. .handler = ks8695_timer_interrupt,
  71. };
  72. static void ks8695_timer_setup(void)
  73. {
  74. unsigned long tmout = CLOCK_TICK_RATE / HZ;
  75. unsigned long tmcon;
  76. /* disable timer1 */
  77. tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON);
  78. __raw_writel(tmcon & ~TMCON_T1EN, KS8695_TMR_VA + KS8695_TMCON);
  79. __raw_writel(tmout / 2, KS8695_TMR_VA + KS8695_T1TC);
  80. __raw_writel(tmout / 2, KS8695_TMR_VA + KS8695_T1PD);
  81. /* re-enable timer1 */
  82. __raw_writel(tmcon | TMCON_T1EN, KS8695_TMR_VA + KS8695_TMCON);
  83. }
  84. static void __init ks8695_timer_init (void)
  85. {
  86. ks8695_timer_setup();
  87. /* Enable timer interrupts */
  88. setup_irq(KS8695_IRQ_TIMER1, &ks8695_timer_irq);
  89. }
  90. struct sys_timer ks8695_timer = {
  91. .init = ks8695_timer_init,
  92. .offset = ks8695_gettimeoffset,
  93. .resume = ks8695_timer_setup,
  94. };
  95. void ks8695_restart(char mode, const char *cmd)
  96. {
  97. unsigned int reg;
  98. if (mode == 's')
  99. soft_restart(0);
  100. /* disable timer0 */
  101. reg = __raw_readl(KS8695_TMR_VA + KS8695_TMCON);
  102. __raw_writel(reg & ~TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
  103. /* enable watchdog mode */
  104. __raw_writel((10 << 8) | T0TC_WATCHDOG, KS8695_TMR_VA + KS8695_T0TC);
  105. /* re-enable timer0 */
  106. __raw_writel(reg | TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
  107. }