arm_global_timer.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  4. * Author(s): Patrice Chotard, <patrice.chotard@foss.st.com> for STMicroelectronics.
  5. *
  6. * ARM Cortext A9 global timer driver
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <clk.h>
  11. #include <timer.h>
  12. #include <linux/err.h>
  13. #include <asm/io.h>
  14. #include <asm/arch-armv7/globaltimer.h>
  15. struct arm_global_timer_priv {
  16. struct globaltimer *global_timer;
  17. };
  18. static u64 arm_global_timer_get_count(struct udevice *dev)
  19. {
  20. struct arm_global_timer_priv *priv = dev_get_priv(dev);
  21. struct globaltimer *global_timer = priv->global_timer;
  22. u32 low, high;
  23. u64 timer;
  24. u32 old = readl(&global_timer->cnt_h);
  25. while (1) {
  26. low = readl(&global_timer->cnt_l);
  27. high = readl(&global_timer->cnt_h);
  28. if (old == high)
  29. break;
  30. else
  31. old = high;
  32. }
  33. timer = high;
  34. return (u64)((timer << 32) | low);
  35. }
  36. static int arm_global_timer_probe(struct udevice *dev)
  37. {
  38. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  39. struct arm_global_timer_priv *priv = dev_get_priv(dev);
  40. struct clk clk;
  41. int err;
  42. ulong ret;
  43. /* get arm global timer base address */
  44. priv->global_timer = (struct globaltimer *)dev_read_addr_ptr(dev);
  45. if (!priv->global_timer)
  46. return -ENOENT;
  47. err = clk_get_by_index(dev, 0, &clk);
  48. if (!err) {
  49. ret = clk_get_rate(&clk);
  50. if (IS_ERR_VALUE(ret))
  51. return ret;
  52. uc_priv->clock_rate = ret;
  53. } else {
  54. uc_priv->clock_rate = CFG_SYS_HZ_CLOCK;
  55. }
  56. /* init timer */
  57. writel(0x01, &priv->global_timer->ctl);
  58. return 0;
  59. }
  60. static const struct timer_ops arm_global_timer_ops = {
  61. .get_count = arm_global_timer_get_count,
  62. };
  63. static const struct udevice_id arm_global_timer_ids[] = {
  64. { .compatible = "arm,cortex-a9-global-timer" },
  65. {}
  66. };
  67. U_BOOT_DRIVER(arm_global_timer) = {
  68. .name = "arm_global_timer",
  69. .id = UCLASS_TIMER,
  70. .of_match = arm_global_timer_ids,
  71. .priv_auto = sizeof(struct arm_global_timer_priv),
  72. .probe = arm_global_timer_probe,
  73. .ops = &arm_global_timer_ops,
  74. };