timer-uclass.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <dm.h>
  7. #include <dm/lists.h>
  8. #include <dm/device-internal.h>
  9. #include <dm/root.h>
  10. #include <clk.h>
  11. #include <errno.h>
  12. #include <timer.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /*
  15. * Implement a timer uclass to work with lib/time.c. The timer is usually
  16. * a 32/64 bits free-running up counter. The get_rate() method is used to get
  17. * the input clock frequency of the timer. The get_count() method is used
  18. * to get the current 64 bits count value. If the hardware is counting down,
  19. * the value should be inversed inside the method. There may be no real
  20. * tick, and no timer interrupt.
  21. */
  22. int notrace timer_get_count(struct udevice *dev, u64 *count)
  23. {
  24. const struct timer_ops *ops = device_get_ops(dev);
  25. if (!ops->get_count)
  26. return -ENOSYS;
  27. return ops->get_count(dev, count);
  28. }
  29. unsigned long notrace timer_get_rate(struct udevice *dev)
  30. {
  31. struct timer_dev_priv *uc_priv = dev->uclass_priv;
  32. return uc_priv->clock_rate;
  33. }
  34. static int timer_pre_probe(struct udevice *dev)
  35. {
  36. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  37. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  38. struct clk timer_clk;
  39. int err;
  40. ulong ret;
  41. err = clk_get_by_index(dev, 0, &timer_clk);
  42. if (!err) {
  43. ret = clk_get_rate(&timer_clk);
  44. if (IS_ERR_VALUE(ret))
  45. return ret;
  46. uc_priv->clock_rate = ret;
  47. } else {
  48. uc_priv->clock_rate =
  49. dev_read_u32_default(dev, "clock-frequency", 0);
  50. }
  51. #endif
  52. return 0;
  53. }
  54. static int timer_post_probe(struct udevice *dev)
  55. {
  56. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  57. if (!uc_priv->clock_rate)
  58. return -EINVAL;
  59. return 0;
  60. }
  61. u64 timer_conv_64(u32 count)
  62. {
  63. /* increment tbh if tbl has rolled over */
  64. if (count < gd->timebase_l)
  65. gd->timebase_h++;
  66. gd->timebase_l = count;
  67. return ((u64)gd->timebase_h << 32) | gd->timebase_l;
  68. }
  69. int notrace dm_timer_init(void)
  70. {
  71. struct udevice *dev = NULL;
  72. __maybe_unused ofnode node;
  73. int ret;
  74. if (gd->timer)
  75. return 0;
  76. /*
  77. * Directly access gd->dm_root to suppress error messages, if the
  78. * virtual root driver does not yet exist.
  79. */
  80. if (gd->dm_root == NULL)
  81. return -EAGAIN;
  82. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  83. /* Check for a chosen timer to be used for tick */
  84. node = ofnode_get_chosen_node("tick-timer");
  85. if (ofnode_valid(node) &&
  86. uclass_get_device_by_ofnode(UCLASS_TIMER, node, &dev)) {
  87. /*
  88. * If the timer is not marked to be bound before
  89. * relocation, bind it anyway.
  90. */
  91. if (!lists_bind_fdt(dm_root(), node, &dev)) {
  92. ret = device_probe(dev);
  93. if (ret)
  94. return ret;
  95. }
  96. }
  97. #endif
  98. if (!dev) {
  99. /* Fall back to the first available timer */
  100. ret = uclass_first_device_err(UCLASS_TIMER, &dev);
  101. if (ret)
  102. return ret;
  103. }
  104. if (dev) {
  105. gd->timer = dev;
  106. return 0;
  107. }
  108. return -ENODEV;
  109. }
  110. UCLASS_DRIVER(timer) = {
  111. .id = UCLASS_TIMER,
  112. .name = "timer",
  113. .pre_probe = timer_pre_probe,
  114. .flags = DM_UC_FLAG_SEQ_ALIAS,
  115. .post_probe = timer_post_probe,
  116. .per_device_auto_alloc_size = sizeof(struct timer_dev_priv),
  117. };