timer.c 3.0 KB

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