ag101p_timer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Andestech ATFTMR010 timer driver
  3. *
  4. * (C) Copyright 2016
  5. * Rick Chen, NDS32 Software Engineering, rick@andestech.com
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <timer.h>
  13. #include <linux/io.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. /*
  16. * Timer Control Register
  17. */
  18. #define T3_UPDOWN (1 << 11)
  19. #define T2_UPDOWN (1 << 10)
  20. #define T1_UPDOWN (1 << 9)
  21. #define T3_OFENABLE (1 << 8)
  22. #define T3_CLOCK (1 << 7)
  23. #define T3_ENABLE (1 << 6)
  24. #define T2_OFENABLE (1 << 5)
  25. #define T2_CLOCK (1 << 4)
  26. #define T2_ENABLE (1 << 3)
  27. #define T1_OFENABLE (1 << 2)
  28. #define T1_CLOCK (1 << 1)
  29. #define T1_ENABLE (1 << 0)
  30. /*
  31. * Timer Interrupt State & Mask Registers
  32. */
  33. #define T3_OVERFLOW (1 << 8)
  34. #define T3_MATCH2 (1 << 7)
  35. #define T3_MATCH1 (1 << 6)
  36. #define T2_OVERFLOW (1 << 5)
  37. #define T2_MATCH2 (1 << 4)
  38. #define T2_MATCH1 (1 << 3)
  39. #define T1_OVERFLOW (1 << 2)
  40. #define T1_MATCH2 (1 << 1)
  41. #define T1_MATCH1 (1 << 0)
  42. struct atftmr_timer_regs {
  43. u32 t1_counter; /* 0x00 */
  44. u32 t1_load; /* 0x04 */
  45. u32 t1_match1; /* 0x08 */
  46. u32 t1_match2; /* 0x0c */
  47. u32 t2_counter; /* 0x10 */
  48. u32 t2_load; /* 0x14 */
  49. u32 t2_match1; /* 0x18 */
  50. u32 t2_match2; /* 0x1c */
  51. u32 t3_counter; /* 0x20 */
  52. u32 t3_load; /* 0x24 */
  53. u32 t3_match1; /* 0x28 */
  54. u32 t3_match2; /* 0x2c */
  55. u32 cr; /* 0x30 */
  56. u32 int_state; /* 0x34 */
  57. u32 int_mask; /* 0x38 */
  58. };
  59. struct atftmr_timer_platdata {
  60. struct atftmr_timer_regs *regs;
  61. };
  62. static int atftmr_timer_get_count(struct udevice *dev, u64 *count)
  63. {
  64. struct atftmr_timer_platdata *plat = dev->platdata;
  65. struct atftmr_timer_regs *const regs = plat->regs;
  66. u32 val;
  67. val = readl(&regs->t3_counter);
  68. *count = timer_conv_64(val);
  69. return 0;
  70. }
  71. static int atftmr_timer_probe(struct udevice *dev)
  72. {
  73. struct atftmr_timer_platdata *plat = dev->platdata;
  74. struct atftmr_timer_regs *const regs = plat->regs;
  75. u32 cr;
  76. writel(0, &regs->t3_load);
  77. writel(0, &regs->t3_counter);
  78. writel(TIMER_LOAD_VAL, &regs->t3_match1);
  79. writel(TIMER_LOAD_VAL, &regs->t3_match2);
  80. /* disable interrupts */
  81. writel(T3_MATCH1|T3_MATCH2|T3_OVERFLOW , &regs->int_mask);
  82. cr = readl(&regs->cr);
  83. cr |= (T3_ENABLE|T3_UPDOWN);
  84. writel(cr, &regs->cr);
  85. return 0;
  86. }
  87. static int atftme_timer_ofdata_to_platdata(struct udevice *dev)
  88. {
  89. struct atftmr_timer_platdata *plat = dev_get_platdata(dev);
  90. plat->regs = map_physmem(devfdt_get_addr(dev),
  91. sizeof(struct atftmr_timer_regs),
  92. MAP_NOCACHE);
  93. return 0;
  94. }
  95. static const struct timer_ops ag101p_timer_ops = {
  96. .get_count = atftmr_timer_get_count,
  97. };
  98. static const struct udevice_id ag101p_timer_ids[] = {
  99. { .compatible = "andestech,attmr010" },
  100. {}
  101. };
  102. U_BOOT_DRIVER(altera_timer) = {
  103. .name = "ag101p_timer",
  104. .id = UCLASS_TIMER,
  105. .of_match = ag101p_timer_ids,
  106. .ofdata_to_platdata = atftme_timer_ofdata_to_platdata,
  107. .platdata_auto_alloc_size = sizeof(struct atftmr_timer_platdata),
  108. .probe = atftmr_timer_probe,
  109. .ops = &ag101p_timer_ops,
  110. .flags = DM_FLAG_PRE_RELOC,
  111. };