timer.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * (C) Copyright 2006
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * (C) Copyright 2002
  6. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Marius Groeger <mgroeger@sysgo.de>
  8. *
  9. * (C) Copyright 2002
  10. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  11. * Alex Zuepke <azu@sysgo.de>
  12. *
  13. * See file CREDITS for list of people who contributed to this
  14. * project.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. */
  31. #include <common.h>
  32. #include <asm/arch/ixp425.h>
  33. #ifdef CONFIG_TIMER_IRQ
  34. #define FREQ 66666666
  35. #define CLOCK_TICK_RATE (((FREQ / CONFIG_SYS_HZ & ~IXP425_OST_RELOAD_MASK) + 1) * CONFIG_SYS_HZ)
  36. #define LATCH ((CLOCK_TICK_RATE + CONFIG_SYS_HZ/2) / CONFIG_SYS_HZ) /* For divider */
  37. /*
  38. * When interrupts are enabled, use timer 2 for time/delay generation...
  39. */
  40. static volatile ulong timestamp;
  41. static void timer_isr(void *data)
  42. {
  43. unsigned int *pTime = (unsigned int *)data;
  44. (*pTime)++;
  45. /*
  46. * Reset IRQ source
  47. */
  48. *IXP425_OSST = IXP425_OSST_TIMER_2_PEND;
  49. }
  50. ulong get_timer (ulong base)
  51. {
  52. return timestamp - base;
  53. }
  54. void reset_timer (void)
  55. {
  56. timestamp = 0;
  57. }
  58. int timer_init (void)
  59. {
  60. /* install interrupt handler for timer */
  61. irq_install_handler(IXP425_TIMER_2_IRQ, timer_isr, (void *)&timestamp);
  62. /* setup the Timer counter value */
  63. *IXP425_OSRT2 = (LATCH & ~IXP425_OST_RELOAD_MASK) | IXP425_OST_ENABLE;
  64. /* enable timer irq */
  65. *IXP425_ICMR = (1 << IXP425_TIMER_2_IRQ);
  66. return 0;
  67. }
  68. #else
  69. ulong get_timer (ulong base)
  70. {
  71. return get_timer_masked () - base;
  72. }
  73. void ixp425_udelay(unsigned long usec)
  74. {
  75. /*
  76. * This function has a max usec, but since it is called from udelay
  77. * we should not have to worry... be happy
  78. */
  79. unsigned long usecs = CONFIG_SYS_HZ/1000000L & ~IXP425_OST_RELOAD_MASK;
  80. *IXP425_OSST = IXP425_OSST_TIMER_1_PEND;
  81. usecs |= IXP425_OST_ONE_SHOT | IXP425_OST_ENABLE;
  82. *IXP425_OSRT1 = usecs;
  83. while (!(*IXP425_OSST & IXP425_OSST_TIMER_1_PEND));
  84. }
  85. void udelay (unsigned long usec)
  86. {
  87. while (usec--) ixp425_udelay(1);
  88. }
  89. static ulong reload_constant = 0xfffffff0;
  90. void reset_timer_masked (void)
  91. {
  92. ulong reload = reload_constant | IXP425_OST_ONE_SHOT | IXP425_OST_ENABLE;
  93. *IXP425_OSST = IXP425_OSST_TIMER_1_PEND;
  94. *IXP425_OSRT1 = reload;
  95. }
  96. ulong get_timer_masked (void)
  97. {
  98. /*
  99. * Note that it is possible for this to wrap!
  100. * In this case we return max.
  101. */
  102. ulong current = *IXP425_OST1;
  103. if (*IXP425_OSST & IXP425_OSST_TIMER_1_PEND)
  104. {
  105. return reload_constant;
  106. }
  107. return (reload_constant - current);
  108. }
  109. int timer_init(void)
  110. {
  111. return 0;
  112. }
  113. #endif