altera_timer.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
  6. * Scott McNutt <smcnutt@psyent.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <timer.h>
  14. #include <asm/io.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. /* control register */
  17. #define ALTERA_TIMER_CONT BIT(1) /* Continuous mode */
  18. #define ALTERA_TIMER_START BIT(2) /* Start timer */
  19. #define ALTERA_TIMER_STOP BIT(3) /* Stop timer */
  20. struct altera_timer_regs {
  21. u32 status; /* Timer status reg */
  22. u32 control; /* Timer control reg */
  23. u32 periodl; /* Timeout period low */
  24. u32 periodh; /* Timeout period high */
  25. u32 snapl; /* Snapshot low */
  26. u32 snaph; /* Snapshot high */
  27. };
  28. struct altera_timer_platdata {
  29. struct altera_timer_regs *regs;
  30. unsigned long clock_rate;
  31. };
  32. static int altera_timer_get_count(struct udevice *dev, unsigned long *count)
  33. {
  34. struct altera_timer_platdata *plat = dev->platdata;
  35. struct altera_timer_regs *const regs = plat->regs;
  36. u32 val;
  37. /* Trigger update */
  38. writel(0x0, &regs->snapl);
  39. /* Read timer value */
  40. val = readl(&regs->snapl) & 0xffff;
  41. val |= (readl(&regs->snaph) & 0xffff) << 16;
  42. *count = ~val;
  43. return 0;
  44. }
  45. static int altera_timer_probe(struct udevice *dev)
  46. {
  47. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  48. struct altera_timer_platdata *plat = dev->platdata;
  49. struct altera_timer_regs *const regs = plat->regs;
  50. uc_priv->clock_rate = plat->clock_rate;
  51. writel(0, &regs->status);
  52. writel(0, &regs->control);
  53. writel(ALTERA_TIMER_STOP, &regs->control);
  54. writel(0xffff, &regs->periodl);
  55. writel(0xffff, &regs->periodh);
  56. writel(ALTERA_TIMER_CONT | ALTERA_TIMER_START, &regs->control);
  57. return 0;
  58. }
  59. static int altera_timer_ofdata_to_platdata(struct udevice *dev)
  60. {
  61. struct altera_timer_platdata *plat = dev_get_platdata(dev);
  62. plat->regs = map_physmem(dev_get_addr(dev),
  63. sizeof(struct altera_timer_regs),
  64. MAP_NOCACHE);
  65. plat->clock_rate = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  66. "clock-frequency", 0);
  67. return 0;
  68. }
  69. static const struct timer_ops altera_timer_ops = {
  70. .get_count = altera_timer_get_count,
  71. };
  72. static const struct udevice_id altera_timer_ids[] = {
  73. { .compatible = "altr,timer-1.0" },
  74. {}
  75. };
  76. U_BOOT_DRIVER(altera_timer) = {
  77. .name = "altera_timer",
  78. .id = UCLASS_TIMER,
  79. .of_match = altera_timer_ids,
  80. .ofdata_to_platdata = altera_timer_ofdata_to_platdata,
  81. .platdata_auto_alloc_size = sizeof(struct altera_timer_platdata),
  82. .probe = altera_timer_probe,
  83. .ops = &altera_timer_ops,
  84. .flags = DM_FLAG_PRE_RELOC,
  85. };