timer.c 3.5 KB

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