pl031.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2008
  4. * Gururaja Hebbar gururajakr@sanyo.co.in
  5. *
  6. * reference linux-2.6.20.6/drivers/rtc/rtc-pl031.c
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <log.h>
  13. #include <rtc.h>
  14. #include <asm/io.h>
  15. #include <asm/types.h>
  16. /*
  17. * Register definitions
  18. */
  19. #define RTC_DR 0x00 /* Data read register */
  20. #define RTC_MR 0x04 /* Match register */
  21. #define RTC_LR 0x08 /* Data load register */
  22. #define RTC_CR 0x0c /* Control register */
  23. #define RTC_IMSC 0x10 /* Interrupt mask and set register */
  24. #define RTC_RIS 0x14 /* Raw interrupt status register */
  25. #define RTC_MIS 0x18 /* Masked interrupt status register */
  26. #define RTC_ICR 0x1c /* Interrupt clear register */
  27. #define RTC_CR_START (1 << 0)
  28. struct pl031_platdata {
  29. phys_addr_t base;
  30. };
  31. static inline u32 pl031_read_reg(struct udevice *dev, int reg)
  32. {
  33. struct pl031_platdata *pdata = dev_get_platdata(dev);
  34. return readl(pdata->base + reg);
  35. }
  36. static inline u32 pl031_write_reg(struct udevice *dev, int reg, u32 value)
  37. {
  38. struct pl031_platdata *pdata = dev_get_platdata(dev);
  39. return writel(value, pdata->base + reg);
  40. }
  41. /*
  42. * Probe RTC device
  43. */
  44. static int pl031_probe(struct udevice *dev)
  45. {
  46. /* Enable RTC Start in Control register*/
  47. pl031_write_reg(dev, RTC_CR, RTC_CR_START);
  48. return 0;
  49. }
  50. /*
  51. * Get the current time from the RTC
  52. */
  53. static int pl031_get(struct udevice *dev, struct rtc_time *tm)
  54. {
  55. unsigned long tim;
  56. if (!tm)
  57. return -EINVAL;
  58. tim = pl031_read_reg(dev, RTC_DR);
  59. rtc_to_tm(tim, tm);
  60. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  61. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  62. tm->tm_hour, tm->tm_min, tm->tm_sec);
  63. return 0;
  64. }
  65. /*
  66. * Set the RTC
  67. */
  68. static int pl031_set(struct udevice *dev, const struct rtc_time *tm)
  69. {
  70. unsigned long tim;
  71. if (!tm)
  72. return -EINVAL;
  73. debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  74. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  75. tm->tm_hour, tm->tm_min, tm->tm_sec);
  76. /* Calculate number of seconds this incoming time represents */
  77. tim = rtc_mktime(tm);
  78. pl031_write_reg(dev, RTC_LR, tim);
  79. return 0;
  80. }
  81. /*
  82. * Reset the RTC. We set the date back to 1970-01-01.
  83. */
  84. static int pl031_reset(struct udevice *dev)
  85. {
  86. pl031_write_reg(dev, RTC_LR, 0);
  87. return 0;
  88. }
  89. static const struct rtc_ops pl031_ops = {
  90. .get = pl031_get,
  91. .set = pl031_set,
  92. .reset = pl031_reset,
  93. };
  94. static const struct udevice_id pl031_ids[] = {
  95. { .compatible = "arm,pl031" },
  96. { }
  97. };
  98. static int pl031_ofdata_to_platdata(struct udevice *dev)
  99. {
  100. struct pl031_platdata *pdata = dev_get_platdata(dev);
  101. pdata->base = dev_read_addr(dev);
  102. return 0;
  103. }
  104. U_BOOT_DRIVER(rtc_pl031) = {
  105. .name = "rtc-pl031",
  106. .id = UCLASS_RTC,
  107. .of_match = pl031_ids,
  108. .probe = pl031_probe,
  109. .ofdata_to_platdata = pl031_ofdata_to_platdata,
  110. .platdata_auto_alloc_size = sizeof(struct pl031_platdata),
  111. .ops = &pl031_ops,
  112. };