sti-timer.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * (C) Copyright 2017 Patrice Chotard <patrice.chotard@st.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <fdtdec.h>
  9. #include <timer.h>
  10. #include <asm/io.h>
  11. #include <asm/arch-armv7/globaltimer.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. struct sti_timer_priv {
  14. struct globaltimer *global_timer;
  15. };
  16. static int sti_timer_get_count(struct udevice *dev, u64 *count)
  17. {
  18. struct sti_timer_priv *priv = dev_get_priv(dev);
  19. struct globaltimer *global_timer = priv->global_timer;
  20. u32 low, high;
  21. u64 timer;
  22. u32 old = readl(&global_timer->cnt_h);
  23. while (1) {
  24. low = readl(&global_timer->cnt_l);
  25. high = readl(&global_timer->cnt_h);
  26. if (old == high)
  27. break;
  28. else
  29. old = high;
  30. }
  31. timer = high;
  32. *count = (u64)((timer << 32) | low);
  33. return 0;
  34. }
  35. static int sti_timer_probe(struct udevice *dev)
  36. {
  37. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  38. struct sti_timer_priv *priv = dev_get_priv(dev);
  39. fdt_addr_t addr;
  40. uc_priv->clock_rate = CONFIG_SYS_HZ_CLOCK;
  41. /* get arm global timer base address */
  42. addr = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev), "reg");
  43. priv->global_timer = (struct globaltimer *)addr;
  44. /* init timer */
  45. writel(0x01, &priv->global_timer->ctl);
  46. return 0;
  47. }
  48. static const struct timer_ops sti_timer_ops = {
  49. .get_count = sti_timer_get_count,
  50. };
  51. static const struct udevice_id sti_timer_ids[] = {
  52. { .compatible = "arm,cortex-a9-global-timer" },
  53. {}
  54. };
  55. U_BOOT_DRIVER(sti_timer) = {
  56. .name = "sti_timer",
  57. .id = UCLASS_TIMER,
  58. .of_match = sti_timer_ids,
  59. .priv_auto_alloc_size = sizeof(struct sti_timer_priv),
  60. .probe = sti_timer_probe,
  61. .ops = &sti_timer_ops,
  62. .flags = DM_FLAG_PRE_RELOC,
  63. };