timer.c 3.5 KB

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