fttmr010_timer.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2009 Faraday Technology
  4. * Po-Yu Chuang <ratbert@faraday-tech.com>
  5. *
  6. * 23/08/2022 Port to DM
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <log.h>
  11. #include <timer.h>
  12. #include <asm/io.h>
  13. #include <dm/ofnode.h>
  14. #include <faraday/fttmr010.h>
  15. #include <asm/global_data.h>
  16. #define TIMER_LOAD_VAL 0xffffffff
  17. struct fttmr010_timer_priv {
  18. struct fttmr010 __iomem *regs;
  19. };
  20. static u64 fttmr010_timer_get_count(struct udevice *dev)
  21. {
  22. struct fttmr010_timer_priv *priv = dev_get_priv(dev);
  23. struct fttmr010 *tmr = priv->regs;
  24. u32 now = TIMER_LOAD_VAL - readl(&tmr->timer3_counter);
  25. /* increment tbu if tbl has rolled over */
  26. if (now < gd->arch.tbl)
  27. gd->arch.tbu++;
  28. gd->arch.tbl = now;
  29. return ((u64)gd->arch.tbu << 32) | gd->arch.tbl;
  30. }
  31. static int fttmr010_timer_probe(struct udevice *dev)
  32. {
  33. struct fttmr010_timer_priv *priv = dev_get_priv(dev);
  34. struct fttmr010 *tmr;
  35. unsigned int cr;
  36. priv->regs = dev_read_addr_ptr(dev);
  37. if (!priv->regs)
  38. return -EINVAL;
  39. tmr = priv->regs;
  40. debug("Faraday FTTMR010 timer revision 0x%08X\n", readl(&tmr->revision));
  41. /* disable timers */
  42. writel(0, &tmr->cr);
  43. /* setup timer */
  44. writel(TIMER_LOAD_VAL, &tmr->timer3_load);
  45. writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
  46. writel(0, &tmr->timer3_match1);
  47. writel(0, &tmr->timer3_match2);
  48. /* we don't want timer to issue interrupts */
  49. writel(FTTMR010_TM3_MATCH1 |
  50. FTTMR010_TM3_MATCH2 |
  51. FTTMR010_TM3_OVERFLOW,
  52. &tmr->interrupt_mask);
  53. cr = readl(&tmr->cr);
  54. cr |= FTTMR010_TM3_CLOCK; /* use external clock */
  55. cr |= FTTMR010_TM3_ENABLE;
  56. writel(cr, &tmr->cr);
  57. gd->arch.tbl = 0;
  58. gd->arch.tbu = 0;
  59. return 0;
  60. }
  61. static const struct timer_ops fttmr010_timer_ops = {
  62. .get_count = fttmr010_timer_get_count,
  63. };
  64. static const struct udevice_id fttmr010_timer_ids[] = {
  65. { .compatible = "faraday,fttmr010-timer" },
  66. {}
  67. };
  68. U_BOOT_DRIVER(fttmr010_timer) = {
  69. .name = "fttmr010_timer",
  70. .id = UCLASS_TIMER,
  71. .of_match = fttmr010_timer_ids,
  72. .priv_auto = sizeof(struct fttmr010_timer_priv),
  73. .probe = fttmr010_timer_probe,
  74. .ops = &fttmr010_timer_ops,
  75. };