timer.c 3.5 KB

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