timer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2009 Samsung Electronics
  4. * Heungjun Kim <riverful.kim@samsung.com>
  5. * Inki Dae <inki.dae@samsung.com>
  6. * Minkyu Kang <mk7.kang@samsung.com>
  7. */
  8. #include <common.h>
  9. #include <div64.h>
  10. #include <init.h>
  11. #include <time.h>
  12. #include <asm/io.h>
  13. #include <asm/arch/pwm.h>
  14. #include <asm/arch/clk.h>
  15. /* Use the old PWM interface for now */
  16. #undef CONFIG_DM_PWM
  17. #include <pwm.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. unsigned long get_current_tick(void);
  20. static void reset_timer_masked(void);
  21. /* macro to read the 16 bit timer */
  22. static inline struct s5p_timer *s5p_get_base_timer(void)
  23. {
  24. return (struct s5p_timer *)samsung_get_base_timer();
  25. }
  26. /**
  27. * Read the countdown timer.
  28. *
  29. * This operates at 1MHz and counts downwards. It will wrap about every
  30. * hour (2^32 microseconds).
  31. *
  32. * @return current value of timer
  33. */
  34. static unsigned long timer_get_us_down(void)
  35. {
  36. struct s5p_timer *const timer = s5p_get_base_timer();
  37. return readl(&timer->tcnto4);
  38. }
  39. int timer_init(void)
  40. {
  41. /* PWM Timer 4 */
  42. pwm_init(4, MUX_DIV_4, 0);
  43. pwm_config(4, 100000, 100000);
  44. pwm_enable(4);
  45. /* Use this as the current monotonic time in us */
  46. gd->arch.timer_reset_value = 0;
  47. /* Use this as the last timer value we saw */
  48. gd->arch.lastinc = timer_get_us_down();
  49. reset_timer_masked();
  50. return 0;
  51. }
  52. /*
  53. * timer without interrupts
  54. */
  55. unsigned long get_timer(unsigned long base)
  56. {
  57. unsigned long long time_ms;
  58. ulong now = timer_get_us_down();
  59. /*
  60. * Increment the time by the amount elapsed since the last read.
  61. * The timer may have wrapped around, but it makes no difference to
  62. * our arithmetic here.
  63. */
  64. gd->arch.timer_reset_value += gd->arch.lastinc - now;
  65. gd->arch.lastinc = now;
  66. /* Divide by 1000 to convert from us to ms */
  67. time_ms = gd->arch.timer_reset_value;
  68. do_div(time_ms, 1000);
  69. return time_ms - base;
  70. }
  71. unsigned long __attribute__((no_instrument_function)) timer_get_us(void)
  72. {
  73. static unsigned long base_time_us;
  74. struct s5p_timer *const timer =
  75. (struct s5p_timer *)samsung_get_base_timer();
  76. unsigned long now_downward_us = readl(&timer->tcnto4);
  77. if (!base_time_us)
  78. base_time_us = now_downward_us;
  79. /* Note that this timer counts downward. */
  80. return base_time_us - now_downward_us;
  81. }
  82. /* delay x useconds */
  83. void __udelay(unsigned long usec)
  84. {
  85. unsigned long count_value;
  86. count_value = timer_get_us_down();
  87. while ((int)(count_value - timer_get_us_down()) < (int)usec)
  88. ;
  89. }
  90. static void reset_timer_masked(void)
  91. {
  92. struct s5p_timer *const timer = s5p_get_base_timer();
  93. /* reset time */
  94. gd->arch.lastinc = readl(&timer->tcnto4);
  95. gd->arch.tbl = 0;
  96. }
  97. /*
  98. * This function is derived from PowerPC code (read timebase as long long).
  99. * On ARM it just returns the timer value.
  100. */
  101. unsigned long long get_ticks(void)
  102. {
  103. return get_timer(0);
  104. }
  105. /*
  106. * This function is derived from PowerPC code (timebase clock frequency).
  107. * On ARM it returns the number of timer ticks per second.
  108. */
  109. unsigned long get_tbclk(void)
  110. {
  111. return CONFIG_SYS_HZ;
  112. }