pl031.c 2.9 KB

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