sti-timer.c 1.7 KB

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