timer-uclass.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
  4. */
  5. #define LOG_CATEGORY UCLASS_TIMER
  6. #include <common.h>
  7. #include <clk.h>
  8. #include <cpu.h>
  9. #include <dm.h>
  10. #include <asm/global_data.h>
  11. #include <dm/lists.h>
  12. #include <dm/device_compat.h>
  13. #include <dm/device-internal.h>
  14. #include <dm/root.h>
  15. #include <errno.h>
  16. #include <init.h>
  17. #include <timer.h>
  18. #include <linux/err.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /*
  21. * Implement a timer uclass to work with lib/time.c. The timer is usually
  22. * a 32/64 bits free-running up counter. The get_rate() method is used to get
  23. * the input clock frequency of the timer. The get_count() method is used
  24. * to get the current 64 bits count value. If the hardware is counting down,
  25. * the value should be inversed inside the method. There may be no real
  26. * tick, and no timer interrupt.
  27. */
  28. int notrace timer_get_count(struct udevice *dev, u64 *count)
  29. {
  30. const struct timer_ops *ops = device_get_ops(dev);
  31. if (!ops->get_count)
  32. return -ENOSYS;
  33. *count = ops->get_count(dev);
  34. return 0;
  35. }
  36. unsigned long notrace timer_get_rate(struct udevice *dev)
  37. {
  38. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  39. return uc_priv->clock_rate;
  40. }
  41. static int timer_pre_probe(struct udevice *dev)
  42. {
  43. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  44. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  45. struct clk timer_clk;
  46. int err;
  47. ulong ret;
  48. /* It is possible that a timer device has a null ofnode */
  49. if (!dev_has_ofnode(dev))
  50. return 0;
  51. err = clk_get_by_index(dev, 0, &timer_clk);
  52. if (!err) {
  53. ret = clk_get_rate(&timer_clk);
  54. if (IS_ERR_VALUE(ret))
  55. return ret;
  56. uc_priv->clock_rate = ret;
  57. } else {
  58. uc_priv->clock_rate =
  59. dev_read_u32_default(dev, "clock-frequency", 0);
  60. }
  61. #endif
  62. return 0;
  63. }
  64. static int timer_post_probe(struct udevice *dev)
  65. {
  66. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  67. if (!uc_priv->clock_rate)
  68. return -EINVAL;
  69. return 0;
  70. }
  71. #if CONFIG_IS_ENABLED(CPU)
  72. int timer_timebase_fallback(struct udevice *dev)
  73. {
  74. struct udevice *cpu;
  75. struct cpu_plat *cpu_plat;
  76. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  77. /* Did we get our clock rate from the device tree? */
  78. if (uc_priv->clock_rate)
  79. return 0;
  80. /* Fall back to timebase-frequency */
  81. dev_dbg(dev, "missing clocks or clock-frequency property; falling back on timebase-frequency\n");
  82. cpu = cpu_get_current_dev();
  83. if (!cpu)
  84. return -ENODEV;
  85. cpu_plat = dev_get_parent_plat(cpu);
  86. if (!cpu_plat)
  87. return -ENODEV;
  88. uc_priv->clock_rate = cpu_plat->timebase_freq;
  89. return 0;
  90. }
  91. #endif
  92. u64 timer_conv_64(u32 count)
  93. {
  94. /* increment tbh if tbl has rolled over */
  95. if (count < gd->timebase_l)
  96. gd->timebase_h++;
  97. gd->timebase_l = count;
  98. return ((u64)gd->timebase_h << 32) | gd->timebase_l;
  99. }
  100. int notrace dm_timer_init(void)
  101. {
  102. struct udevice *dev = NULL;
  103. __maybe_unused ofnode node;
  104. int ret;
  105. if (gd->timer)
  106. return 0;
  107. /*
  108. * Directly access gd->dm_root to suppress error messages, if the
  109. * virtual root driver does not yet exist.
  110. */
  111. if (gd->dm_root == NULL)
  112. return -EAGAIN;
  113. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  114. /* Check for a chosen timer to be used for tick */
  115. node = ofnode_get_chosen_node("tick-timer");
  116. if (ofnode_valid(node) &&
  117. uclass_get_device_by_ofnode(UCLASS_TIMER, node, &dev)) {
  118. /*
  119. * If the timer is not marked to be bound before
  120. * relocation, bind it anyway.
  121. */
  122. if (!lists_bind_fdt(dm_root(), node, &dev, false)) {
  123. ret = device_probe(dev);
  124. if (ret)
  125. return ret;
  126. }
  127. }
  128. #endif
  129. if (!dev) {
  130. /* Fall back to the first available timer */
  131. ret = uclass_first_device_err(UCLASS_TIMER, &dev);
  132. if (ret)
  133. return ret;
  134. }
  135. if (dev) {
  136. gd->timer = dev;
  137. return 0;
  138. }
  139. return -ENODEV;
  140. }
  141. UCLASS_DRIVER(timer) = {
  142. .id = UCLASS_TIMER,
  143. .name = "timer",
  144. .pre_probe = timer_pre_probe,
  145. .flags = DM_UC_FLAG_SEQ_ALIAS,
  146. .post_probe = timer_post_probe,
  147. .per_device_auto = sizeof(struct timer_dev_priv),
  148. };