timer.c 2.6 KB

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