ast_timer.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2016 Google Inc.
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <errno.h>
  8. #include <timer.h>
  9. #include <asm/io.h>
  10. #include <asm/arch/timer.h>
  11. #define AST_TICK_TIMER 1
  12. #define AST_TMC_RELOAD_VAL 0xffffffff
  13. struct ast_timer_priv {
  14. struct ast_timer *regs;
  15. struct ast_timer_counter *tmc;
  16. };
  17. static struct ast_timer_counter *ast_get_timer_counter(struct ast_timer *timer,
  18. int n)
  19. {
  20. if (n > 3)
  21. return &timer->timers2[n - 4];
  22. else
  23. return &timer->timers1[n - 1];
  24. }
  25. static int ast_timer_probe(struct udevice *dev)
  26. {
  27. struct ast_timer_priv *priv = dev_get_priv(dev);
  28. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  29. writel(AST_TMC_RELOAD_VAL, &priv->tmc->reload_val);
  30. /*
  31. * Stop the timer. This will also load reload_val into
  32. * the status register.
  33. */
  34. clrbits_le32(&priv->regs->ctrl1,
  35. AST_TMC_EN << AST_TMC_CTRL1_SHIFT(AST_TICK_TIMER));
  36. /* Start the timer from the fixed 1MHz clock. */
  37. setbits_le32(&priv->regs->ctrl1,
  38. (AST_TMC_EN | AST_TMC_1MHZ) <<
  39. AST_TMC_CTRL1_SHIFT(AST_TICK_TIMER));
  40. uc_priv->clock_rate = AST_TMC_RATE;
  41. return 0;
  42. }
  43. static int ast_timer_get_count(struct udevice *dev, u64 *count)
  44. {
  45. struct ast_timer_priv *priv = dev_get_priv(dev);
  46. *count = AST_TMC_RELOAD_VAL - readl(&priv->tmc->status);
  47. return 0;
  48. }
  49. static int ast_timer_ofdata_to_platdata(struct udevice *dev)
  50. {
  51. struct ast_timer_priv *priv = dev_get_priv(dev);
  52. priv->regs = devfdt_get_addr_ptr(dev);
  53. if (IS_ERR(priv->regs))
  54. return PTR_ERR(priv->regs);
  55. priv->tmc = ast_get_timer_counter(priv->regs, AST_TICK_TIMER);
  56. return 0;
  57. }
  58. static const struct timer_ops ast_timer_ops = {
  59. .get_count = ast_timer_get_count,
  60. };
  61. static const struct udevice_id ast_timer_ids[] = {
  62. { .compatible = "aspeed,ast2500-timer" },
  63. { .compatible = "aspeed,ast2400-timer" },
  64. { }
  65. };
  66. U_BOOT_DRIVER(ast_timer) = {
  67. .name = "ast_timer",
  68. .id = UCLASS_TIMER,
  69. .of_match = ast_timer_ids,
  70. .probe = ast_timer_probe,
  71. .priv_auto_alloc_size = sizeof(struct ast_timer_priv),
  72. .ofdata_to_platdata = ast_timer_ofdata_to_platdata,
  73. .ops = &ast_timer_ops,
  74. };