timer.c 2.8 KB

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