cadence-ttc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Xilinx, Inc. (Michal Simek)
  4. */
  5. #include <common.h>
  6. #include <bootstage.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <init.h>
  10. #include <timer.h>
  11. #include <asm/global_data.h>
  12. #include <asm/io.h>
  13. #include <linux/bitops.h>
  14. #include <linux/err.h>
  15. #define CNT_CNTRL_RESET BIT(4)
  16. struct cadence_ttc_regs {
  17. u32 clk_cntrl1; /* 0x0 - Clock Control 1 */
  18. u32 clk_cntrl2; /* 0x4 - Clock Control 2 */
  19. u32 clk_cntrl3; /* 0x8 - Clock Control 3 */
  20. u32 counter_cntrl1; /* 0xC - Counter Control 1 */
  21. u32 counter_cntrl2; /* 0x10 - Counter Control 2 */
  22. u32 counter_cntrl3; /* 0x14 - Counter Control 3 */
  23. u32 counter_val1; /* 0x18 - Counter Control 1 */
  24. u32 counter_val2; /* 0x1C - Counter Control 2 */
  25. u32 counter_val3; /* 0x20 - Counter Control 3 */
  26. u32 reserved[15];
  27. u32 interrupt_enable1; /* 0x60 - Interrupt Enable 1 */
  28. u32 interrupt_enable2; /* 0x64 - Interrupt Enable 2 */
  29. u32 interrupt_enable3; /* 0x68 - Interrupt Enable 3 */
  30. };
  31. struct cadence_ttc_priv {
  32. struct cadence_ttc_regs *regs;
  33. };
  34. #if CONFIG_IS_ENABLED(BOOTSTAGE)
  35. ulong timer_get_boot_us(void)
  36. {
  37. u64 ticks = 0;
  38. u32 rate = 1;
  39. u64 us;
  40. int ret;
  41. ret = dm_timer_init();
  42. if (!ret) {
  43. /* The timer is available */
  44. rate = timer_get_rate(gd->timer);
  45. timer_get_count(gd->timer, &ticks);
  46. } else {
  47. return 0;
  48. }
  49. us = (ticks * 1000) / rate;
  50. return us;
  51. }
  52. #endif
  53. static u64 cadence_ttc_get_count(struct udevice *dev)
  54. {
  55. struct cadence_ttc_priv *priv = dev_get_priv(dev);
  56. return readl(&priv->regs->counter_val1);
  57. }
  58. static int cadence_ttc_probe(struct udevice *dev)
  59. {
  60. struct cadence_ttc_priv *priv = dev_get_priv(dev);
  61. /* Disable interrupts for sure */
  62. writel(0, &priv->regs->interrupt_enable1);
  63. writel(0, &priv->regs->interrupt_enable2);
  64. writel(0, &priv->regs->interrupt_enable3);
  65. /* Make sure that clocks are configured properly without prescaller */
  66. writel(0, &priv->regs->clk_cntrl1);
  67. writel(0, &priv->regs->clk_cntrl2);
  68. writel(0, &priv->regs->clk_cntrl3);
  69. /* Reset and enable this counter */
  70. writel(CNT_CNTRL_RESET, &priv->regs->counter_cntrl1);
  71. return 0;
  72. }
  73. static int cadence_ttc_of_to_plat(struct udevice *dev)
  74. {
  75. struct cadence_ttc_priv *priv = dev_get_priv(dev);
  76. priv->regs = map_physmem(dev_read_addr(dev),
  77. sizeof(struct cadence_ttc_regs), MAP_NOCACHE);
  78. if (IS_ERR(priv->regs))
  79. return PTR_ERR(priv->regs);
  80. return 0;
  81. }
  82. static int cadence_ttc_bind(struct udevice *dev)
  83. {
  84. const char *cells;
  85. cells = dev_read_prop(dev, "#pwm-cells", NULL);
  86. if (cells)
  87. return -ENODEV;
  88. return 0;
  89. }
  90. static const struct timer_ops cadence_ttc_ops = {
  91. .get_count = cadence_ttc_get_count,
  92. };
  93. static const struct udevice_id cadence_ttc_ids[] = {
  94. { .compatible = "cdns,ttc" },
  95. {}
  96. };
  97. U_BOOT_DRIVER(cadence_ttc) = {
  98. .name = "cadence_ttc",
  99. .id = UCLASS_TIMER,
  100. .of_match = cadence_ttc_ids,
  101. .of_to_plat = cadence_ttc_of_to_plat,
  102. .priv_auto = sizeof(struct cadence_ttc_priv),
  103. .probe = cadence_ttc_probe,
  104. .ops = &cadence_ttc_ops,
  105. .bind = cadence_ttc_bind,
  106. };