timer.c 3.0 KB

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