timer.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * TNETV107X: Timer implementation
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <asm/io.h>
  8. #include <asm/arch/clock.h>
  9. struct timer_regs {
  10. u_int32_t pid12;
  11. u_int32_t pad[3];
  12. u_int32_t tim12;
  13. u_int32_t tim34;
  14. u_int32_t prd12;
  15. u_int32_t prd34;
  16. u_int32_t tcr;
  17. u_int32_t tgcr;
  18. u_int32_t wdtcr;
  19. };
  20. #define regs ((struct timer_regs *)CONFIG_SYS_TIMERBASE)
  21. #define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ)
  22. #define TIM_CLK_DIV 16
  23. static ulong timestamp;
  24. static ulong lastinc;
  25. int timer_init(void)
  26. {
  27. clk_enable(TNETV107X_LPSC_TIMER0);
  28. lastinc = timestamp = 0;
  29. /* We are using timer34 in unchained 32-bit mode, full speed */
  30. __raw_writel(0x0, &regs->tcr);
  31. __raw_writel(0x0, &regs->tgcr);
  32. __raw_writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &regs->tgcr);
  33. __raw_writel(0x0, &regs->tim34);
  34. __raw_writel(TIMER_LOAD_VAL, &regs->prd34);
  35. __raw_writel(2 << 22, &regs->tcr);
  36. return 0;
  37. }
  38. static ulong get_timer_raw(void)
  39. {
  40. ulong now = __raw_readl(&regs->tim34);
  41. if (now >= lastinc)
  42. timestamp += now - lastinc;
  43. else
  44. timestamp += now + TIMER_LOAD_VAL - lastinc;
  45. lastinc = now;
  46. return timestamp;
  47. }
  48. ulong get_timer(ulong base)
  49. {
  50. return (get_timer_raw() / (TIMER_LOAD_VAL / TIM_CLK_DIV)) - base;
  51. }
  52. unsigned long long get_ticks(void)
  53. {
  54. return get_timer(0);
  55. }
  56. void __udelay(unsigned long usec)
  57. {
  58. ulong tmo;
  59. ulong endtime;
  60. signed long diff;
  61. tmo = CONFIG_SYS_HZ_CLOCK / 1000;
  62. tmo *= usec;
  63. tmo /= (1000 * TIM_CLK_DIV);
  64. endtime = get_timer_raw() + tmo;
  65. do {
  66. ulong now = get_timer_raw();
  67. diff = endtime - now;
  68. } while (diff >= 0);
  69. }
  70. ulong get_tbclk(void)
  71. {
  72. return CONFIG_SYS_HZ;
  73. }