timer-uclass.c 4.2 KB

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