timer.c 2.9 KB

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