timer.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. static ulong get_timer_raw(void)
  54. {
  55. ulong now = __raw_readl(&regs->tim34);
  56. if (now >= lastinc)
  57. timestamp += now - lastinc;
  58. else
  59. timestamp += now + TIMER_LOAD_VAL - lastinc;
  60. lastinc = now;
  61. return timestamp;
  62. }
  63. ulong get_timer(ulong base)
  64. {
  65. return (get_timer_raw() / (TIMER_LOAD_VAL / TIM_CLK_DIV)) - base;
  66. }
  67. unsigned long long get_ticks(void)
  68. {
  69. return get_timer(0);
  70. }
  71. void __udelay(unsigned long usec)
  72. {
  73. ulong tmo;
  74. ulong endtime;
  75. signed long diff;
  76. tmo = CONFIG_SYS_HZ_CLOCK / 1000;
  77. tmo *= usec;
  78. tmo /= (1000 * TIM_CLK_DIV);
  79. endtime = get_timer_raw() + tmo;
  80. do {
  81. ulong now = get_timer_raw();
  82. diff = endtime - now;
  83. } while (diff >= 0);
  84. }
  85. ulong get_tbclk(void)
  86. {
  87. return CONFIG_SYS_HZ;
  88. }