timer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Freescale i.MX28 timer driver
  4. *
  5. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  6. * on behalf of DENX Software Engineering GmbH
  7. *
  8. * Based on code from LTIB:
  9. * (C) Copyright 2009-2010 Freescale Semiconductor, Inc.
  10. */
  11. #include <common.h>
  12. #include <asm/io.h>
  13. #include <asm/arch/imx-regs.h>
  14. #include <asm/arch/sys_proto.h>
  15. /* Maximum fixed count */
  16. #if defined(CONFIG_MX23)
  17. #define TIMER_LOAD_VAL 0xffff
  18. #elif defined(CONFIG_MX28)
  19. #define TIMER_LOAD_VAL 0xffffffff
  20. #endif
  21. DECLARE_GLOBAL_DATA_PTR;
  22. #define timestamp (gd->arch.tbl)
  23. #define lastdec (gd->arch.lastinc)
  24. /*
  25. * This driver uses 1kHz clock source.
  26. */
  27. #define MXS_INCREMENTER_HZ 1000
  28. static inline unsigned long tick_to_time(unsigned long tick)
  29. {
  30. return tick / (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
  31. }
  32. static inline unsigned long time_to_tick(unsigned long time)
  33. {
  34. return time * (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
  35. }
  36. /* Calculate how many ticks happen in "us" microseconds */
  37. static inline unsigned long us_to_tick(unsigned long us)
  38. {
  39. return (us * MXS_INCREMENTER_HZ) / 1000000;
  40. }
  41. int timer_init(void)
  42. {
  43. struct mxs_timrot_regs *timrot_regs =
  44. (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
  45. /* Reset Timers and Rotary Encoder module */
  46. mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
  47. /* Set fixed_count to 0 */
  48. #if defined(CONFIG_MX23)
  49. writel(0, &timrot_regs->hw_timrot_timcount0);
  50. #elif defined(CONFIG_MX28)
  51. writel(0, &timrot_regs->hw_timrot_fixed_count0);
  52. #endif
  53. /* Set UPDATE bit and 1Khz frequency */
  54. writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD |
  55. TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL,
  56. &timrot_regs->hw_timrot_timctrl0);
  57. /* Set fixed_count to maximal value */
  58. #if defined(CONFIG_MX23)
  59. writel(TIMER_LOAD_VAL - 1, &timrot_regs->hw_timrot_timcount0);
  60. #elif defined(CONFIG_MX28)
  61. writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
  62. #endif
  63. return 0;
  64. }
  65. unsigned long long get_ticks(void)
  66. {
  67. struct mxs_timrot_regs *timrot_regs =
  68. (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
  69. uint32_t now;
  70. /* Current tick value */
  71. #if defined(CONFIG_MX23)
  72. /* Upper bits are the valid ones. */
  73. now = readl(&timrot_regs->hw_timrot_timcount0) >>
  74. TIMROT_RUNNING_COUNTn_RUNNING_COUNT_OFFSET;
  75. #elif defined(CONFIG_MX28)
  76. now = readl(&timrot_regs->hw_timrot_running_count0);
  77. #else
  78. #error "Don't know how to read timrot_regs"
  79. #endif
  80. if (lastdec >= now) {
  81. /*
  82. * normal mode (non roll)
  83. * move stamp forward with absolut diff ticks
  84. */
  85. timestamp += (lastdec - now);
  86. } else {
  87. /* we have rollover of decrementer */
  88. timestamp += (TIMER_LOAD_VAL - now) + lastdec;
  89. }
  90. lastdec = now;
  91. return timestamp;
  92. }
  93. ulong get_timer_masked(void)
  94. {
  95. return tick_to_time(get_ticks());
  96. }
  97. ulong get_timer(ulong base)
  98. {
  99. return get_timer_masked() - base;
  100. }
  101. /* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
  102. #define MXS_HW_DIGCTL_MICROSECONDS 0x8001c0c0
  103. void __udelay(unsigned long usec)
  104. {
  105. uint32_t old, new, incr;
  106. uint32_t counter = 0;
  107. old = readl(MXS_HW_DIGCTL_MICROSECONDS);
  108. while (counter < usec) {
  109. new = readl(MXS_HW_DIGCTL_MICROSECONDS);
  110. /* Check if the timer wrapped. */
  111. if (new < old) {
  112. incr = 0xffffffff - old;
  113. incr += new;
  114. } else {
  115. incr = new - old;
  116. }
  117. /*
  118. * Check if we are close to the maximum time and the counter
  119. * would wrap if incremented. If that's the case, break out
  120. * from the loop as the requested delay time passed.
  121. */
  122. if (counter + incr < counter)
  123. break;
  124. counter += incr;
  125. old = new;
  126. }
  127. }
  128. ulong get_tbclk(void)
  129. {
  130. return MXS_INCREMENTER_HZ;
  131. }