timer-uclass.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
  4. */
  5. #include <common.h>
  6. #include <cpu.h>
  7. #include <dm.h>
  8. #include <init.h>
  9. #include <dm/lists.h>
  10. #include <dm/device-internal.h>
  11. #include <dm/root.h>
  12. #include <clk.h>
  13. #include <errno.h>
  14. #include <timer.h>
  15. #include <linux/err.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. /*
  18. * Implement a timer uclass to work with lib/time.c. The timer is usually
  19. * a 32/64 bits free-running up counter. The get_rate() method is used to get
  20. * the input clock frequency of the timer. The get_count() method is used
  21. * to get the current 64 bits count value. If the hardware is counting down,
  22. * the value should be inversed inside the method. There may be no real
  23. * tick, and no timer interrupt.
  24. */
  25. int notrace timer_get_count(struct udevice *dev, u64 *count)
  26. {
  27. const struct timer_ops *ops = device_get_ops(dev);
  28. if (!ops->get_count)
  29. return -ENOSYS;
  30. return ops->get_count(dev, count);
  31. }
  32. unsigned long notrace timer_get_rate(struct udevice *dev)
  33. {
  34. struct timer_dev_priv *uc_priv = dev->uclass_priv;
  35. return uc_priv->clock_rate;
  36. }
  37. static int timer_pre_probe(struct udevice *dev)
  38. {
  39. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  40. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  41. struct clk timer_clk;
  42. int err;
  43. ulong ret;
  44. /* It is possible that a timer device has a null ofnode */
  45. if (!dev_of_valid(dev))
  46. return 0;
  47. err = clk_get_by_index(dev, 0, &timer_clk);
  48. if (!err) {
  49. ret = clk_get_rate(&timer_clk);
  50. if (IS_ERR_VALUE(ret))
  51. return ret;
  52. uc_priv->clock_rate = ret;
  53. } else {
  54. uc_priv->clock_rate =
  55. dev_read_u32_default(dev, "clock-frequency", 0);
  56. }
  57. #endif
  58. return 0;
  59. }
  60. static int timer_post_probe(struct udevice *dev)
  61. {
  62. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  63. if (!uc_priv->clock_rate)
  64. return -EINVAL;
  65. return 0;
  66. }
  67. /*
  68. * TODO: should be CONFIG_IS_ENABLED(CPU), but the SPL config has _SUPPORT on
  69. * the end...
  70. */
  71. #if defined(CONFIG_CPU) || defined(CONFIG_SPL_CPU_SUPPORT)
  72. int timer_timebase_fallback(struct udevice *dev)
  73. {
  74. struct udevice *cpu;
  75. struct cpu_platdata *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_platdata(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_alloc_size = sizeof(struct timer_dev_priv),
  148. };