ast_timer.c 2.1 KB

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