timer.c 2.5 KB

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