timer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * TNETV107X: Timer implementation
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <common.h>
  22. #include <asm/io.h>
  23. #include <asm/arch/clock.h>
  24. struct timer_regs {
  25. u_int32_t pid12;
  26. u_int32_t pad[3];
  27. u_int32_t tim12;
  28. u_int32_t tim34;
  29. u_int32_t prd12;
  30. u_int32_t prd34;
  31. u_int32_t tcr;
  32. u_int32_t tgcr;
  33. u_int32_t wdtcr;
  34. };
  35. #define regs ((struct timer_regs *)CONFIG_SYS_TIMERBASE)
  36. #define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ)
  37. #define TIM_CLK_DIV 16
  38. static ulong timestamp;
  39. static ulong lastinc;
  40. int timer_init(void)
  41. {
  42. clk_enable(TNETV107X_LPSC_TIMER0);
  43. lastinc = timestamp = 0;
  44. /* We are using timer34 in unchained 32-bit mode, full speed */
  45. __raw_writel(0x0, &regs->tcr);
  46. __raw_writel(0x0, &regs->tgcr);
  47. __raw_writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &regs->tgcr);
  48. __raw_writel(0x0, &regs->tim34);
  49. __raw_writel(TIMER_LOAD_VAL, &regs->prd34);
  50. __raw_writel(2 << 22, &regs->tcr);
  51. return 0;
  52. }
  53. void reset_timer(void)
  54. {
  55. lastinc = timestamp = 0;
  56. __raw_writel(0, &regs->tcr);
  57. __raw_writel(0, &regs->tim34);
  58. __raw_writel(2 << 22, &regs->tcr);
  59. }
  60. static ulong get_timer_raw(void)
  61. {
  62. ulong now = __raw_readl(&regs->tim34);
  63. if (now >= lastinc)
  64. timestamp += now - lastinc;
  65. else
  66. timestamp += now + TIMER_LOAD_VAL - lastinc;
  67. lastinc = now;
  68. return timestamp;
  69. }
  70. ulong get_timer(ulong base)
  71. {
  72. return (get_timer_raw() / (TIMER_LOAD_VAL / TIM_CLK_DIV)) - base;
  73. }
  74. void set_timer(ulong t)
  75. {
  76. timestamp = t;
  77. }
  78. unsigned long long get_ticks(void)
  79. {
  80. return get_timer(0);
  81. }
  82. void __udelay(unsigned long usec)
  83. {
  84. ulong tmo;
  85. ulong endtime;
  86. signed long diff;
  87. tmo = CONFIG_SYS_HZ_CLOCK / 1000;
  88. tmo *= usec;
  89. tmo /= (1000 * TIM_CLK_DIV);
  90. endtime = get_timer_raw() + tmo;
  91. do {
  92. ulong now = get_timer_raw();
  93. diff = endtime - now;
  94. } while (diff >= 0);
  95. }
  96. ulong get_tbclk(void)
  97. {
  98. return CONFIG_SYS_HZ;
  99. }