timer.c 2.9 KB

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