ast_timer.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include <linux/err.h>
  12. #define AST_TICK_TIMER 1
  13. #define AST_TMC_RELOAD_VAL 0xffffffff
  14. struct ast_timer_priv {
  15. struct ast_timer *regs;
  16. struct ast_timer_counter *tmc;
  17. };
  18. static struct ast_timer_counter *ast_get_timer_counter(struct ast_timer *timer,
  19. int n)
  20. {
  21. if (n > 3)
  22. return &timer->timers2[n - 4];
  23. else
  24. return &timer->timers1[n - 1];
  25. }
  26. static int ast_timer_probe(struct udevice *dev)
  27. {
  28. struct ast_timer_priv *priv = dev_get_priv(dev);
  29. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  30. writel(AST_TMC_RELOAD_VAL, &priv->tmc->reload_val);
  31. /*
  32. * Stop the timer. This will also load reload_val into
  33. * the status register.
  34. */
  35. clrbits_le32(&priv->regs->ctrl1,
  36. AST_TMC_EN << AST_TMC_CTRL1_SHIFT(AST_TICK_TIMER));
  37. /* Start the timer from the fixed 1MHz clock. */
  38. setbits_le32(&priv->regs->ctrl1,
  39. (AST_TMC_EN | AST_TMC_1MHZ) <<
  40. AST_TMC_CTRL1_SHIFT(AST_TICK_TIMER));
  41. uc_priv->clock_rate = AST_TMC_RATE;
  42. return 0;
  43. }
  44. static u64 ast_timer_get_count(struct udevice *dev)
  45. {
  46. struct ast_timer_priv *priv = dev_get_priv(dev);
  47. return AST_TMC_RELOAD_VAL - readl(&priv->tmc->status);
  48. }
  49. static int ast_timer_of_to_plat(struct udevice *dev)
  50. {
  51. struct ast_timer_priv *priv = dev_get_priv(dev);
  52. priv->regs = dev_read_addr_ptr(dev);
  53. if (!priv->regs)
  54. return -EINVAL;
  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 = sizeof(struct ast_timer_priv),
  72. .of_to_plat = ast_timer_of_to_plat,
  73. .ops = &ast_timer_ops,
  74. };