altera_timer.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. };
  31. static int altera_timer_get_count(struct udevice *dev, u64 *count)
  32. {
  33. struct altera_timer_platdata *plat = dev->platdata;
  34. struct altera_timer_regs *const regs = plat->regs;
  35. u32 val;
  36. /* Trigger update */
  37. writel(0x0, &regs->snapl);
  38. /* Read timer value */
  39. val = readl(&regs->snapl) & 0xffff;
  40. val |= (readl(&regs->snaph) & 0xffff) << 16;
  41. *count = timer_conv_64(~val);
  42. return 0;
  43. }
  44. static int altera_timer_probe(struct udevice *dev)
  45. {
  46. struct altera_timer_platdata *plat = dev->platdata;
  47. struct altera_timer_regs *const regs = plat->regs;
  48. writel(0, &regs->status);
  49. writel(0, &regs->control);
  50. writel(ALTERA_TIMER_STOP, &regs->control);
  51. writel(0xffff, &regs->periodl);
  52. writel(0xffff, &regs->periodh);
  53. writel(ALTERA_TIMER_CONT | ALTERA_TIMER_START, &regs->control);
  54. return 0;
  55. }
  56. static int altera_timer_ofdata_to_platdata(struct udevice *dev)
  57. {
  58. struct altera_timer_platdata *plat = dev_get_platdata(dev);
  59. plat->regs = map_physmem(dev_get_addr(dev),
  60. sizeof(struct altera_timer_regs),
  61. MAP_NOCACHE);
  62. return 0;
  63. }
  64. static const struct timer_ops altera_timer_ops = {
  65. .get_count = altera_timer_get_count,
  66. };
  67. static const struct udevice_id altera_timer_ids[] = {
  68. { .compatible = "altr,timer-1.0" },
  69. {}
  70. };
  71. U_BOOT_DRIVER(altera_timer) = {
  72. .name = "altera_timer",
  73. .id = UCLASS_TIMER,
  74. .of_match = altera_timer_ids,
  75. .ofdata_to_platdata = altera_timer_ofdata_to_platdata,
  76. .platdata_auto_alloc_size = sizeof(struct altera_timer_platdata),
  77. .probe = altera_timer_probe,
  78. .ops = &altera_timer_ops,
  79. .flags = DM_FLAG_PRE_RELOC,
  80. };