orion-timer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <asm/io.h>
  3. #include <common.h>
  4. #include <div64.h>
  5. #include <dm/device.h>
  6. #include <dm/fdtaddr.h>
  7. #include <timer.h>
  8. #define TIMER_CTRL 0x00
  9. #define TIMER0_EN BIT(0)
  10. #define TIMER0_RELOAD_EN BIT(1)
  11. #define TIMER0_RELOAD 0x10
  12. #define TIMER0_VAL 0x14
  13. enum input_clock_type {
  14. INPUT_CLOCK_NON_FIXED,
  15. INPUT_CLOCK_25MHZ, /* input clock rate is fixed to 25MHz */
  16. };
  17. struct orion_timer_priv {
  18. void *base;
  19. };
  20. #define MVEBU_TIMER_FIXED_RATE_25MHZ 25000000
  21. static bool early_init_done(void *base)
  22. {
  23. if ((readl(base + TIMER_CTRL) & TIMER0_EN) &&
  24. (readl(base + TIMER0_RELOAD) == ~0))
  25. return true;
  26. return false;
  27. }
  28. /* Common functions for early (boot) and DM based timer */
  29. static void orion_timer_init(void *base, enum input_clock_type type)
  30. {
  31. /* Only init the timer once */
  32. if (early_init_done(base))
  33. return;
  34. writel(~0, base + TIMER0_VAL);
  35. writel(~0, base + TIMER0_RELOAD);
  36. if (type == INPUT_CLOCK_25MHZ) {
  37. /*
  38. * On Armada XP / 38x ..., the 25MHz clock source needs to
  39. * be enabled
  40. */
  41. setbits_le32(base + TIMER_CTRL, BIT(11));
  42. }
  43. /* enable timer */
  44. setbits_le32(base + TIMER_CTRL, TIMER0_EN | TIMER0_RELOAD_EN);
  45. }
  46. static uint64_t orion_timer_get_count(void *base)
  47. {
  48. return timer_conv_64(~readl(base + TIMER0_VAL));
  49. }
  50. /* Early (e.g. bootstage etc) timer functions */
  51. static void notrace timer_early_init(void)
  52. {
  53. if (IS_ENABLED(CONFIG_ARCH_MVEBU))
  54. orion_timer_init((void *)MVEBU_TIMER_BASE, INPUT_CLOCK_25MHZ);
  55. else
  56. orion_timer_init((void *)MVEBU_TIMER_BASE, INPUT_CLOCK_NON_FIXED);
  57. }
  58. /**
  59. * timer_early_get_rate() - Get the timer rate before driver model
  60. */
  61. unsigned long notrace timer_early_get_rate(void)
  62. {
  63. timer_early_init();
  64. if (IS_ENABLED(CONFIG_ARCH_MVEBU))
  65. return MVEBU_TIMER_FIXED_RATE_25MHZ;
  66. else
  67. return CFG_SYS_TCLK;
  68. }
  69. /**
  70. * timer_early_get_count() - Get the timer count before driver model
  71. *
  72. */
  73. u64 notrace timer_early_get_count(void)
  74. {
  75. timer_early_init();
  76. return orion_timer_get_count((void *)MVEBU_TIMER_BASE);
  77. }
  78. ulong timer_get_boot_us(void)
  79. {
  80. u64 ticks;
  81. ticks = timer_early_get_count();
  82. return lldiv(ticks * 1000, timer_early_get_rate());
  83. }
  84. /* DM timer functions */
  85. static uint64_t dm_orion_timer_get_count(struct udevice *dev)
  86. {
  87. struct orion_timer_priv *priv = dev_get_priv(dev);
  88. return orion_timer_get_count(priv->base);
  89. }
  90. static int orion_timer_probe(struct udevice *dev)
  91. {
  92. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  93. enum input_clock_type type = dev_get_driver_data(dev);
  94. struct orion_timer_priv *priv = dev_get_priv(dev);
  95. priv->base = devfdt_remap_addr_index(dev, 0);
  96. if (!priv->base) {
  97. debug("unable to map registers\n");
  98. return -ENOMEM;
  99. }
  100. if (type == INPUT_CLOCK_25MHZ)
  101. uc_priv->clock_rate = MVEBU_TIMER_FIXED_RATE_25MHZ;
  102. else
  103. uc_priv->clock_rate = CFG_SYS_TCLK;
  104. orion_timer_init(priv->base, type);
  105. return 0;
  106. }
  107. static const struct timer_ops orion_timer_ops = {
  108. .get_count = dm_orion_timer_get_count,
  109. };
  110. static const struct udevice_id orion_timer_ids[] = {
  111. { .compatible = "marvell,orion-timer", .data = INPUT_CLOCK_NON_FIXED },
  112. { .compatible = "marvell,armada-370-timer", .data = INPUT_CLOCK_25MHZ },
  113. { .compatible = "marvell,armada-xp-timer", .data = INPUT_CLOCK_25MHZ },
  114. {}
  115. };
  116. U_BOOT_DRIVER(orion_timer) = {
  117. .name = "orion_timer",
  118. .id = UCLASS_TIMER,
  119. .of_match = orion_timer_ids,
  120. .probe = orion_timer_probe,
  121. .ops = &orion_timer_ops,
  122. .priv_auto = sizeof(struct orion_timer_priv),
  123. };