timer.c 3.0 KB

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