timer.c 3.1 KB

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