timer-uclass.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  37. struct clk timer_clk;
  38. int err;
  39. ulong ret;
  40. err = clk_get_by_index(dev, 0, &timer_clk);
  41. if (!err) {
  42. ret = clk_get_rate(&timer_clk);
  43. if (IS_ERR_VALUE(ret))
  44. return ret;
  45. uc_priv->clock_rate = ret;
  46. } else
  47. uc_priv->clock_rate = fdtdec_get_int(gd->fdt_blob,
  48. dev->of_offset, "clock-frequency", 0);
  49. return 0;
  50. }
  51. static int timer_post_probe(struct udevice *dev)
  52. {
  53. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  54. if (!uc_priv->clock_rate)
  55. return -EINVAL;
  56. return 0;
  57. }
  58. u64 timer_conv_64(u32 count)
  59. {
  60. /* increment tbh if tbl has rolled over */
  61. if (count < gd->timebase_l)
  62. gd->timebase_h++;
  63. gd->timebase_l = count;
  64. return ((u64)gd->timebase_h << 32) | gd->timebase_l;
  65. }
  66. int notrace dm_timer_init(void)
  67. {
  68. const void *blob = gd->fdt_blob;
  69. struct udevice *dev = NULL;
  70. int node;
  71. int ret;
  72. if (gd->timer)
  73. return 0;
  74. /* Check for a chosen timer to be used for tick */
  75. node = fdtdec_get_chosen_node(blob, "tick-timer");
  76. if (node < 0) {
  77. /* No chosen timer, trying first available timer */
  78. ret = uclass_first_device_err(UCLASS_TIMER, &dev);
  79. if (ret)
  80. return ret;
  81. } else {
  82. if (uclass_get_device_by_of_offset(UCLASS_TIMER, node, &dev)) {
  83. /*
  84. * If the timer is not marked to be bound before
  85. * relocation, bind it anyway.
  86. */
  87. if (node > 0 &&
  88. !lists_bind_fdt(gd->dm_root, blob, node, &dev)) {
  89. ret = device_probe(dev);
  90. if (ret)
  91. return ret;
  92. }
  93. }
  94. }
  95. if (dev) {
  96. gd->timer = dev;
  97. return 0;
  98. }
  99. return -ENODEV;
  100. }
  101. UCLASS_DRIVER(timer) = {
  102. .id = UCLASS_TIMER,
  103. .name = "timer",
  104. .pre_probe = timer_pre_probe,
  105. .flags = DM_UC_FLAG_SEQ_ALIAS,
  106. .post_probe = timer_post_probe,
  107. .per_device_auto_alloc_size = sizeof(struct timer_dev_priv),
  108. };