timer.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2014, STMicroelectronics - All Rights Reserved
  4. * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
  5. */
  6. #include <common.h>
  7. #include <init.h>
  8. #include <time.h>
  9. #include <asm/io.h>
  10. #include <asm/arch-stv0991/hardware.h>
  11. #include <asm/arch-stv0991/stv0991_cgu.h>
  12. #include <asm/arch-stv0991/stv0991_gpt.h>
  13. static struct stv0991_cgu_regs *const stv0991_cgu_regs = \
  14. (struct stv0991_cgu_regs *) (CGU_BASE_ADDR);
  15. #define READ_TIMER() (readl(&gpt1_regs_ptr->cnt) & GPT_FREE_RUNNING)
  16. #define GPT_RESOLUTION (CONFIG_STV0991_HZ_CLOCK / CONFIG_STV0991_HZ)
  17. DECLARE_GLOBAL_DATA_PTR;
  18. #define timestamp gd->arch.tbl
  19. #define lastdec gd->arch.lastinc
  20. static ulong get_timer_masked(void);
  21. int timer_init(void)
  22. {
  23. /* Timer1 clock configuration */
  24. writel(TIMER1_CLK_CFG, &stv0991_cgu_regs->tim_freq);
  25. writel(readl(&stv0991_cgu_regs->cgu_enable_2) |
  26. TIMER1_CLK_EN, &stv0991_cgu_regs->cgu_enable_2);
  27. /* Stop the timer */
  28. writel(readl(&gpt1_regs_ptr->cr1) & ~GPT_CR1_CEN, &gpt1_regs_ptr->cr1);
  29. writel(GPT_PRESCALER_128, &gpt1_regs_ptr->psc);
  30. /* Configure timer for auto-reload */
  31. writel(readl(&gpt1_regs_ptr->cr1) | GPT_MODE_AUTO_RELOAD,
  32. &gpt1_regs_ptr->cr1);
  33. /* load value for free running */
  34. writel(GPT_FREE_RUNNING, &gpt1_regs_ptr->arr);
  35. /* start timer */
  36. writel(readl(&gpt1_regs_ptr->cr1) | GPT_CR1_CEN,
  37. &gpt1_regs_ptr->cr1);
  38. /* Reset the timer */
  39. lastdec = READ_TIMER();
  40. timestamp = 0;
  41. return 0;
  42. }
  43. /*
  44. * timer without interrupts
  45. */
  46. ulong get_timer(ulong base)
  47. {
  48. return (get_timer_masked() / GPT_RESOLUTION) - base;
  49. }
  50. void __udelay(unsigned long usec)
  51. {
  52. ulong tmo;
  53. ulong start = get_timer_masked();
  54. ulong tenudelcnt = CONFIG_STV0991_HZ_CLOCK / (1000 * 100);
  55. ulong rndoff;
  56. rndoff = (usec % 10) ? 1 : 0;
  57. /* tenudelcnt timer tick gives 10 microsecconds delay */
  58. tmo = ((usec / 10) + rndoff) * tenudelcnt;
  59. while ((ulong) (get_timer_masked() - start) < tmo)
  60. ;
  61. }
  62. static ulong get_timer_masked(void)
  63. {
  64. ulong now = READ_TIMER();
  65. if (now >= lastdec) {
  66. /* normal mode */
  67. timestamp += now - lastdec;
  68. } else {
  69. /* we have an overflow ... */
  70. timestamp += now + GPT_FREE_RUNNING - lastdec;
  71. }
  72. lastdec = now;
  73. return timestamp;
  74. }
  75. /*
  76. * This function is derived from PowerPC code (read timebase as long long).
  77. * On ARM it just returns the timer value.
  78. */
  79. unsigned long long get_ticks(void)
  80. {
  81. return get_timer(0);
  82. }
  83. /*
  84. * This function is derived from PowerPC code (timebase clock frequency).
  85. * On ARM it returns the number of timer ticks per second.
  86. */
  87. ulong get_tbclk(void)
  88. {
  89. return CONFIG_STV0991_HZ;
  90. }