cadence-ttc.c 2.7 KB

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