timer-uclass.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <errno.h>
  11. #include <timer.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. /*
  14. * Implement a timer uclass to work with lib/time.c. The timer is usually
  15. * a 32/64 bits free-running up counter. The get_rate() method is used to get
  16. * the input clock frequency of the timer. The get_count() method is used
  17. * to get the current 64 bits count value. If the hardware is counting down,
  18. * the value should be inversed inside the method. There may be no real
  19. * tick, and no timer interrupt.
  20. */
  21. int notrace timer_get_count(struct udevice *dev, u64 *count)
  22. {
  23. const struct timer_ops *ops = device_get_ops(dev);
  24. if (!ops->get_count)
  25. return -ENOSYS;
  26. return ops->get_count(dev, count);
  27. }
  28. unsigned long notrace timer_get_rate(struct udevice *dev)
  29. {
  30. struct timer_dev_priv *uc_priv = dev->uclass_priv;
  31. return uc_priv->clock_rate;
  32. }
  33. static int timer_pre_probe(struct udevice *dev)
  34. {
  35. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  36. uc_priv->clock_rate = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  37. "clock-frequency", 0);
  38. return 0;
  39. }
  40. static int timer_post_probe(struct udevice *dev)
  41. {
  42. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  43. if (!uc_priv->clock_rate)
  44. return -EINVAL;
  45. return 0;
  46. }
  47. u64 timer_conv_64(u32 count)
  48. {
  49. /* increment tbh if tbl has rolled over */
  50. if (count < gd->timebase_l)
  51. gd->timebase_h++;
  52. gd->timebase_l = count;
  53. return ((u64)gd->timebase_h << 32) | gd->timebase_l;
  54. }
  55. int notrace dm_timer_init(void)
  56. {
  57. const void *blob = gd->fdt_blob;
  58. struct udevice *dev = NULL;
  59. int node;
  60. int ret;
  61. if (gd->timer)
  62. return 0;
  63. /* Check for a chosen timer to be used for tick */
  64. node = fdtdec_get_chosen_node(blob, "tick-timer");
  65. if (node < 0) {
  66. /* No chosen timer, trying first available timer */
  67. ret = uclass_first_device_err(UCLASS_TIMER, &dev);
  68. if (ret)
  69. return ret;
  70. } else {
  71. if (uclass_get_device_by_of_offset(UCLASS_TIMER, node, &dev)) {
  72. /*
  73. * If the timer is not marked to be bound before
  74. * relocation, bind it anyway.
  75. */
  76. if (node > 0 &&
  77. !lists_bind_fdt(gd->dm_root, blob, node, &dev)) {
  78. ret = device_probe(dev);
  79. if (ret)
  80. return ret;
  81. }
  82. }
  83. }
  84. if (dev) {
  85. gd->timer = dev;
  86. return 0;
  87. }
  88. return -ENODEV;
  89. }
  90. UCLASS_DRIVER(timer) = {
  91. .id = UCLASS_TIMER,
  92. .name = "timer",
  93. .pre_probe = timer_pre_probe,
  94. .flags = DM_UC_FLAG_SEQ_ALIAS,
  95. .post_probe = timer_post_probe,
  96. .per_device_auto_alloc_size = sizeof(struct timer_dev_priv),
  97. };