npcm-timer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2022 Nuvoton Technology Corp.
  4. */
  5. #include <common.h>
  6. #include <clk.h>
  7. #include <dm.h>
  8. #include <timer.h>
  9. #include <asm/io.h>
  10. #define NPCM_TIMER_CLOCK_RATE 1000000UL /* 1MHz timer */
  11. #define NPCM_TIMER_INPUT_RATE 25000000UL /* Rate of input clock */
  12. #define NPCM_TIMER_TDR_MASK GENMASK(23, 0)
  13. #define NPCM_TIMER_MAX_VAL NPCM_TIMER_TDR_MASK /* max counter value */
  14. /* Register offsets */
  15. #define TCR0 0x0 /* Timer Control and Status Register */
  16. #define TICR0 0x8 /* Timer Initial Count Register */
  17. #define TDR0 0x10 /* Timer Data Register */
  18. /* TCR fields */
  19. #define TCR_MODE_PERIODIC BIT(27)
  20. #define TCR_EN BIT(30)
  21. #define TCR_PRESCALE (NPCM_TIMER_INPUT_RATE / NPCM_TIMER_CLOCK_RATE - 1)
  22. enum input_clock_type {
  23. INPUT_CLOCK_FIXED, /* input clock rate is fixed */
  24. INPUT_CLOCK_NON_FIXED
  25. };
  26. /**
  27. * struct npcm_timer_priv - private data for npcm timer driver
  28. * npcm timer is a 24-bits down-counting timer.
  29. *
  30. * @last_count: last hw counter value
  31. * @counter: the value to be returned for get_count ops
  32. */
  33. struct npcm_timer_priv {
  34. void __iomem *base;
  35. u32 last_count;
  36. u64 counter;
  37. };
  38. static u64 npcm_timer_get_count(struct udevice *dev)
  39. {
  40. struct npcm_timer_priv *priv = dev_get_priv(dev);
  41. u32 val;
  42. /* The timer is counting down */
  43. val = readl(priv->base + TDR0) & NPCM_TIMER_TDR_MASK;
  44. if (val <= priv->last_count)
  45. priv->counter += priv->last_count - val;
  46. else
  47. priv->counter += priv->last_count + (NPCM_TIMER_MAX_VAL + 1 - val);
  48. priv->last_count = val;
  49. return priv->counter;
  50. }
  51. static int npcm_timer_probe(struct udevice *dev)
  52. {
  53. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  54. struct npcm_timer_priv *priv = dev_get_priv(dev);
  55. enum input_clock_type type = dev_get_driver_data(dev);
  56. struct clk clk;
  57. int ret;
  58. priv->base = dev_read_addr_ptr(dev);
  59. if (!priv->base)
  60. return -EINVAL;
  61. uc_priv->clock_rate = NPCM_TIMER_CLOCK_RATE;
  62. if (type == INPUT_CLOCK_NON_FIXED) {
  63. ret = clk_get_by_index(dev, 0, &clk);
  64. if (ret < 0)
  65. return ret;
  66. ret = clk_set_rate(&clk, NPCM_TIMER_INPUT_RATE);
  67. if (ret < 0)
  68. return ret;
  69. }
  70. /*
  71. * Configure timer and start
  72. * periodic mode
  73. * timer clock rate = input clock / prescale
  74. */
  75. writel(0, priv->base + TCR0);
  76. writel(NPCM_TIMER_MAX_VAL, priv->base + TICR0);
  77. writel(TCR_EN | TCR_MODE_PERIODIC | TCR_PRESCALE,
  78. priv->base + TCR0);
  79. return 0;
  80. }
  81. static const struct timer_ops npcm_timer_ops = {
  82. .get_count = npcm_timer_get_count,
  83. };
  84. static const struct udevice_id npcm_timer_ids[] = {
  85. { .compatible = "nuvoton,npcm845-timer", .data = INPUT_CLOCK_FIXED},
  86. { .compatible = "nuvoton,npcm750-timer", .data = INPUT_CLOCK_NON_FIXED},
  87. {}
  88. };
  89. U_BOOT_DRIVER(npcm_timer) = {
  90. .name = "npcm_timer",
  91. .id = UCLASS_TIMER,
  92. .of_match = npcm_timer_ids,
  93. .priv_auto = sizeof(struct npcm_timer_priv),
  94. .probe = npcm_timer_probe,
  95. .ops = &npcm_timer_ops,
  96. .flags = DM_FLAG_PRE_RELOC,
  97. };