mchp-pit64b-timer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * 64-bit Periodic Interval Timer driver
  4. *
  5. * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries
  6. *
  7. * Author: Claudiu Beznea <claudiu.beznea@microchip.com>
  8. */
  9. #include <common.h>
  10. #include <clk.h>
  11. #include <dm.h>
  12. #include <timer.h>
  13. #include <asm/io.h>
  14. #define MCHP_PIT64B_CR 0x00 /* Control Register */
  15. #define MCHP_PIT64B_CR_START BIT(0)
  16. #define MCHP_PIT64B_CR_SWRST BIT(8)
  17. #define MCHP_PIT64B_MR 0x04 /* Mode Register */
  18. #define MCHP_PIT64B_MR_CONT BIT(0)
  19. #define MCHP_PIT64B_LSB_PR 0x08 /* LSB Period Register */
  20. #define MCHP_PIT64B_MSB_PR 0x0C /* MSB Period Register */
  21. #define MCHP_PIT64B_TLSBR 0x20 /* Timer LSB Register */
  22. #define MCHP_PIT64B_TMSBR 0x24 /* Timer MSB Register */
  23. struct mchp_pit64b_priv {
  24. void __iomem *base;
  25. };
  26. static u64 mchp_pit64b_get_count(struct udevice *dev)
  27. {
  28. struct mchp_pit64b_priv *priv = dev_get_priv(dev);
  29. u32 lsb = readl(priv->base + MCHP_PIT64B_TLSBR);
  30. u32 msb = readl(priv->base + MCHP_PIT64B_TMSBR);
  31. return ((u64)msb << 32) | lsb;
  32. }
  33. static int mchp_pit64b_probe(struct udevice *dev)
  34. {
  35. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  36. struct mchp_pit64b_priv *priv = dev_get_priv(dev);
  37. struct clk clk;
  38. ulong rate;
  39. int ret;
  40. priv->base = dev_read_addr_ptr(dev);
  41. if (IS_ERR(priv->base))
  42. return PTR_ERR(priv->base);
  43. ret = clk_get_by_index(dev, 0, &clk);
  44. if (ret)
  45. return ret;
  46. ret = clk_enable(&clk);
  47. if (ret)
  48. return ret;
  49. rate = clk_get_rate(&clk);
  50. if (!rate) {
  51. clk_disable(&clk);
  52. return -ENOTSUPP;
  53. }
  54. /* Reset the timer in case it was used by previous bootloaders. */
  55. writel(MCHP_PIT64B_CR_SWRST, priv->base + MCHP_PIT64B_CR);
  56. /*
  57. * Use highest prescaller (for a peripheral clock running at 200MHz
  58. * this will lead to the timer running at 12.5MHz) and continuous mode.
  59. */
  60. writel((15 << 8) | MCHP_PIT64B_MR_CONT, priv->base + MCHP_PIT64B_MR);
  61. uc_priv->clock_rate = rate / 16;
  62. /*
  63. * Simulate free running counter by setting max values to period
  64. * registers.
  65. */
  66. writel(~0UL, priv->base + MCHP_PIT64B_MSB_PR);
  67. writel(~0UL, priv->base + MCHP_PIT64B_LSB_PR);
  68. /* Start the timer. */
  69. writel(MCHP_PIT64B_CR_START, priv->base + MCHP_PIT64B_CR);
  70. return 0;
  71. }
  72. static const struct timer_ops mchp_pit64b_ops = {
  73. .get_count = mchp_pit64b_get_count,
  74. };
  75. static const struct udevice_id mchp_pit64b_ids[] = {
  76. { .compatible = "microchip,sam9x60-pit64b", },
  77. { .compatible = "microchip,sama7g5-pit64b", },
  78. { }
  79. };
  80. U_BOOT_DRIVER(mchp_pit64b) = {
  81. .name = "mchp-pit64b",
  82. .id = UCLASS_TIMER,
  83. .of_match = mchp_pit64b_ids,
  84. .priv_auto = sizeof(struct mchp_pit64b_priv),
  85. .probe = mchp_pit64b_probe,
  86. .ops = &mchp_pit64b_ops,
  87. .flags = DM_FLAG_PRE_RELOC,
  88. };