timer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2014 Broadcom Corporation.
  4. */
  5. #include <common.h>
  6. #include <div64.h>
  7. #include <init.h>
  8. #include <time.h>
  9. #include <asm/io.h>
  10. #include <asm/iproc-common/timer.h>
  11. #include <asm/iproc-common/sysmap.h>
  12. static inline uint64_t timer_global_read(void)
  13. {
  14. uint64_t cur_tick;
  15. uint32_t count_h;
  16. uint32_t count_l;
  17. do {
  18. count_h = readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
  19. TIMER_GLB_HI_OFFSET);
  20. count_l = readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
  21. TIMER_GLB_LOW_OFFSET);
  22. cur_tick = readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
  23. TIMER_GLB_HI_OFFSET);
  24. } while (cur_tick != count_h);
  25. return (cur_tick << 32) + count_l;
  26. }
  27. void timer_global_init(void)
  28. {
  29. writel(0, IPROC_PERIPH_GLB_TIM_REG_BASE + TIMER_GLB_CTRL_OFFSET);
  30. writel(0, IPROC_PERIPH_GLB_TIM_REG_BASE + TIMER_GLB_LOW_OFFSET);
  31. writel(0, IPROC_PERIPH_GLB_TIM_REG_BASE + TIMER_GLB_HI_OFFSET);
  32. writel(TIMER_GLB_TIM_CTRL_TIM_EN,
  33. IPROC_PERIPH_GLB_TIM_REG_BASE + TIMER_GLB_CTRL_OFFSET);
  34. }
  35. int timer_init(void)
  36. {
  37. timer_global_init();
  38. return 0;
  39. }
  40. unsigned long get_timer(unsigned long base)
  41. {
  42. uint64_t count;
  43. uint64_t ret;
  44. uint64_t tim_clk;
  45. uint64_t periph_clk;
  46. count = timer_global_read();
  47. /* default arm clk is 1GHz, periph_clk=arm_clk/2, tick per msec */
  48. periph_clk = 500000;
  49. tim_clk = lldiv(periph_clk,
  50. (((readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
  51. TIMER_GLB_CTRL_OFFSET) &
  52. TIMER_GLB_TIM_CTRL_PRESC_MASK) >> 8) + 1));
  53. ret = lldiv(count, (uint32_t)tim_clk);
  54. /* returns msec */
  55. return ret - base;
  56. }
  57. void __udelay(unsigned long usec)
  58. {
  59. uint64_t cur_tick, end_tick;
  60. uint64_t tim_clk;
  61. uint64_t periph_clk;
  62. /* default arm clk is 1GHz, periph_clk=arm_clk/2, tick per usec */
  63. periph_clk = 500;
  64. tim_clk = lldiv(periph_clk,
  65. (((readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
  66. TIMER_GLB_CTRL_OFFSET) &
  67. TIMER_GLB_TIM_CTRL_PRESC_MASK) >> 8) + 1));
  68. cur_tick = timer_global_read();
  69. end_tick = tim_clk;
  70. end_tick *= usec;
  71. end_tick += cur_tick;
  72. do {
  73. cur_tick = timer_global_read();
  74. } while (cur_tick < end_tick);
  75. }
  76. void timer_systick_init(uint32_t tick_ms)
  77. {
  78. /* Disable timer and clear interrupt status*/
  79. writel(0, IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_CTRL_OFFSET);
  80. writel(TIMER_PVT_TIM_INT_STATUS_SET,
  81. IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_STATUS_OFFSET);
  82. writel((PLL_AXI_CLK/1000) * tick_ms,
  83. IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_LOAD_OFFSET);
  84. writel(TIMER_PVT_TIM_CTRL_INT_EN |
  85. TIMER_PVT_TIM_CTRL_AUTO_RELD |
  86. TIMER_PVT_TIM_CTRL_TIM_EN,
  87. IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_CTRL_OFFSET);
  88. }
  89. void timer_systick_isr(void *data)
  90. {
  91. writel(TIMER_PVT_TIM_INT_STATUS_SET,
  92. IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_STATUS_OFFSET);
  93. }
  94. /*
  95. * This function is derived from PowerPC code (read timebase as long long).
  96. * On ARM it just returns the timer value in msec.
  97. */
  98. unsigned long long get_ticks(void)
  99. {
  100. return get_timer(0);
  101. }
  102. /*
  103. * This is used in conjuction with get_ticks, which returns msec as ticks.
  104. * Here we just return ticks/sec = msec/sec = 1000
  105. */
  106. ulong get_tbclk(void)
  107. {
  108. return 1000;
  109. }