ae3xx_timer.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Andestech ATCPIT100 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. #define REG32_TMR(x) (*(unsigned long *) ((plat->regs) + (x>>2)))
  16. /*
  17. * Definition of register offsets
  18. */
  19. /* ID and Revision Register */
  20. #define ID_REV 0x0
  21. /* Configuration Register */
  22. #define CFG 0x10
  23. /* Interrupt Enable Register */
  24. #define INT_EN 0x14
  25. #define CH_INT_EN(c , i) ((1<<i)<<(4*c))
  26. /* Interrupt Status Register */
  27. #define INT_STA 0x18
  28. #define CH_INT_STA(c , i) ((1<<i)<<(4*c))
  29. /* Channel Enable Register */
  30. #define CH_EN 0x1C
  31. #define CH_TMR_EN(c , t) ((1<<t)<<(4*c))
  32. /* Ch n Control REgister */
  33. #define CH_CTL(n) (0x20+0x10*n)
  34. /* Channel clock source , bit 3 , 0:External clock , 1:APB clock */
  35. #define APB_CLK (1<<3)
  36. /* Channel mode , bit 0~2 */
  37. #define TMR_32 1
  38. #define TMR_16 2
  39. #define TMR_8 3
  40. #define PWM 4
  41. #define CH_REL(n) (0x24+0x10*n)
  42. #define CH_CNT(n) (0x28+0x10*n)
  43. struct atctmr_timer_regs {
  44. u32 id_rev; /* 0x00 */
  45. u32 reservd[3]; /* 0x04 ~ 0x0c */
  46. u32 cfg; /* 0x10 */
  47. u32 int_en; /* 0x14 */
  48. u32 int_st; /* 0x18 */
  49. u32 ch_en; /* 0x1c */
  50. u32 ch0_ctrl; /* 0x20 */
  51. u32 ch0_reload; /* 0x24 */
  52. u32 ch0_cntr; /* 0x28 */
  53. u32 reservd1; /* 0x2c */
  54. u32 ch1_ctrl; /* 0x30 */
  55. u32 ch1_reload; /* 0x34 */
  56. u32 int_mask; /* 0x38 */
  57. };
  58. struct atftmr_timer_platdata {
  59. unsigned long *regs;
  60. };
  61. static int atftmr_timer_get_count(struct udevice *dev, u64 *count)
  62. {
  63. struct atftmr_timer_platdata *plat = dev->platdata;
  64. u32 val;
  65. val = ~(REG32_TMR(CH_CNT(1))+0xffffffff);
  66. *count = timer_conv_64(val);
  67. return 0;
  68. }
  69. static int atctmr_timer_probe(struct udevice *dev)
  70. {
  71. struct atftmr_timer_platdata *plat = dev->platdata;
  72. REG32_TMR(CH_REL(1)) = 0xffffffff;
  73. REG32_TMR(CH_CTL(1)) = APB_CLK|TMR_32;
  74. REG32_TMR(CH_EN) |= CH_TMR_EN(1 , 0);
  75. return 0;
  76. }
  77. static int atctme_timer_ofdata_to_platdata(struct udevice *dev)
  78. {
  79. struct atftmr_timer_platdata *plat = dev_get_platdata(dev);
  80. plat->regs = map_physmem(dev_get_addr(dev) , 0x100 , MAP_NOCACHE);
  81. return 0;
  82. }
  83. static const struct timer_ops ag101p_timer_ops = {
  84. .get_count = atftmr_timer_get_count,
  85. };
  86. static const struct udevice_id ag101p_timer_ids[] = {
  87. { .compatible = "andestech,atcpit100" },
  88. {}
  89. };
  90. U_BOOT_DRIVER(altera_timer) = {
  91. .name = "ae3xx_timer",
  92. .id = UCLASS_TIMER,
  93. .of_match = ag101p_timer_ids,
  94. .ofdata_to_platdata = atctme_timer_ofdata_to_platdata,
  95. .platdata_auto_alloc_size = sizeof(struct atftmr_timer_platdata),
  96. .probe = atctmr_timer_probe,
  97. .ops = &ag101p_timer_ops,
  98. .flags = DM_FLAG_PRE_RELOC,
  99. };