timer-uclass.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <dm/lists.h>
  9. #include <dm/device-internal.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 = fdtdec_get_int(gd->fdt_blob,
  49. dev_of_offset(dev), "clock-frequency", 0);
  50. #endif
  51. return 0;
  52. }
  53. static int timer_post_probe(struct udevice *dev)
  54. {
  55. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  56. if (!uc_priv->clock_rate)
  57. return -EINVAL;
  58. return 0;
  59. }
  60. u64 timer_conv_64(u32 count)
  61. {
  62. /* increment tbh if tbl has rolled over */
  63. if (count < gd->timebase_l)
  64. gd->timebase_h++;
  65. gd->timebase_l = count;
  66. return ((u64)gd->timebase_h << 32) | gd->timebase_l;
  67. }
  68. int notrace dm_timer_init(void)
  69. {
  70. __maybe_unused const void *blob = gd->fdt_blob;
  71. struct udevice *dev = NULL;
  72. int node = -ENOENT;
  73. int ret;
  74. if (gd->timer)
  75. return 0;
  76. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  77. /* Check for a chosen timer to be used for tick */
  78. node = fdtdec_get_chosen_node(blob, "tick-timer");
  79. #endif
  80. if (node < 0) {
  81. /* No chosen timer, trying first available timer */
  82. ret = uclass_first_device_err(UCLASS_TIMER, &dev);
  83. if (ret)
  84. return ret;
  85. } else {
  86. if (uclass_get_device_by_of_offset(UCLASS_TIMER, node, &dev)) {
  87. /*
  88. * If the timer is not marked to be bound before
  89. * relocation, bind it anyway.
  90. */
  91. if (node > 0 &&
  92. !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
  93. &dev)) {
  94. ret = device_probe(dev);
  95. if (ret)
  96. return ret;
  97. }
  98. }
  99. }
  100. if (dev) {
  101. gd->timer = dev;
  102. return 0;
  103. }
  104. return -ENODEV;
  105. }
  106. UCLASS_DRIVER(timer) = {
  107. .id = UCLASS_TIMER,
  108. .name = "timer",
  109. .pre_probe = timer_pre_probe,
  110. .flags = DM_UC_FLAG_SEQ_ALIAS,
  111. .post_probe = timer_post_probe,
  112. .per_device_auto_alloc_size = sizeof(struct timer_dev_priv),
  113. };