altera_timer.c 2.3 KB

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