rv8803.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Date & Time support for Micro Crystal RV-8803-C7.
  4. *
  5. * based on ds1307.c which is
  6. * (C) Copyright 2001, 2002, 2003
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. * Keith Outwater, keith_outwater@mvis.com`
  9. * Steven Scholz, steven.scholz@imc-berlin.de
  10. *
  11. */
  12. #include <common.h>
  13. #include <command.h>
  14. #include <dm.h>
  15. #include <log.h>
  16. #include <rtc.h>
  17. #include <i2c.h>
  18. #include <linux/bitops.h>
  19. /*
  20. * RTC register addresses
  21. */
  22. #define RTC_SEC_REG_ADDR 0x00
  23. #define RTC_MIN_REG_ADDR 0x01
  24. #define RTC_HR_REG_ADDR 0x02
  25. #define RTC_DAY_REG_ADDR 0x03
  26. #define RTC_DATE_REG_ADDR 0x04
  27. #define RTC_MON_REG_ADDR 0x05
  28. #define RTC_YR_REG_ADDR 0x06
  29. #define RTC_FLAG_REG_ADDR 0x0E
  30. #define RTC_FLAG_BIT_V1F BIT(0)
  31. #define RTC_FLAG_BIT_V2F BIT(1)
  32. #define RTC_CTL_REG_ADDR 0x0F
  33. #define RTC_CTL_BIT_RST BIT(0)
  34. static int rv8803_rtc_set(struct udevice *dev, const struct rtc_time *tm)
  35. {
  36. int ret;
  37. u8 buf[7];
  38. debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  39. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  40. tm->tm_hour, tm->tm_min, tm->tm_sec);
  41. if (tm->tm_year < 2000 || tm->tm_year > 2099)
  42. printf("WARNING: year should be between 2000 and 2099!\n");
  43. buf[RTC_YR_REG_ADDR] = bin2bcd(tm->tm_year % 100);
  44. buf[RTC_MON_REG_ADDR] = bin2bcd(tm->tm_mon);
  45. buf[RTC_DAY_REG_ADDR] = 1 << (tm->tm_wday & 0x7);
  46. buf[RTC_DATE_REG_ADDR] = bin2bcd(tm->tm_mday);
  47. buf[RTC_HR_REG_ADDR] = bin2bcd(tm->tm_hour);
  48. buf[RTC_MIN_REG_ADDR] = bin2bcd(tm->tm_min);
  49. buf[RTC_SEC_REG_ADDR] = bin2bcd(tm->tm_sec);
  50. ret = dm_i2c_write(dev, 0, buf, sizeof(buf));
  51. if (ret < 0)
  52. return ret;
  53. return 0;
  54. }
  55. static int rv8803_rtc_get(struct udevice *dev, struct rtc_time *tm)
  56. {
  57. int ret;
  58. u8 buf[7];
  59. int flags;
  60. flags = dm_i2c_reg_read(dev, RTC_FLAG_REG_ADDR);
  61. if (flags < 0)
  62. return flags;
  63. debug("%s: flags=%Xh\n", __func__, flags);
  64. if (flags & RTC_FLAG_BIT_V1F)
  65. printf("### Warning: temperature compensation has stopped\n");
  66. if (flags & RTC_FLAG_BIT_V2F) {
  67. printf("### Warning: Voltage low, data is invalid\n");
  68. return -1;
  69. }
  70. ret = dm_i2c_read(dev, 0, buf, sizeof(buf));
  71. if (ret < 0)
  72. return ret;
  73. tm->tm_sec = bcd2bin(buf[RTC_SEC_REG_ADDR] & 0x7F);
  74. tm->tm_min = bcd2bin(buf[RTC_MIN_REG_ADDR] & 0x7F);
  75. tm->tm_hour = bcd2bin(buf[RTC_HR_REG_ADDR] & 0x3F);
  76. tm->tm_mday = bcd2bin(buf[RTC_DATE_REG_ADDR] & 0x3F);
  77. tm->tm_mon = bcd2bin(buf[RTC_MON_REG_ADDR] & 0x1F);
  78. tm->tm_year = bcd2bin(buf[RTC_YR_REG_ADDR]) + 2000;
  79. tm->tm_wday = fls(buf[RTC_DAY_REG_ADDR] & 0x7F) - 1;
  80. tm->tm_yday = 0;
  81. tm->tm_isdst = 0;
  82. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  83. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  84. tm->tm_hour, tm->tm_min, tm->tm_sec);
  85. return 0;
  86. }
  87. static int rv8803_rtc_reset(struct udevice *dev)
  88. {
  89. int ret;
  90. struct rtc_time tmp = {
  91. .tm_year = 2000,
  92. .tm_mon = 1,
  93. .tm_mday = 1,
  94. .tm_hour = 0,
  95. .tm_min = 0,
  96. .tm_sec = 0,
  97. };
  98. /* assert reset */
  99. ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, RTC_CTL_BIT_RST);
  100. if (ret < 0)
  101. return ret;
  102. /* clear all flags */
  103. ret = dm_i2c_reg_write(dev, RTC_FLAG_REG_ADDR, 0);
  104. if (ret < 0)
  105. return ret;
  106. ret = rv8803_rtc_set(dev, &tmp);
  107. if (ret < 0)
  108. return ret;
  109. /* clear reset */
  110. ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, 0);
  111. if (ret < 0)
  112. return ret;
  113. debug("RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  114. tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
  115. tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
  116. return 0;
  117. }
  118. static int rv8803_probe(struct udevice *dev)
  119. {
  120. i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
  121. DM_I2C_CHIP_WR_ADDRESS);
  122. return 0;
  123. }
  124. static const struct rtc_ops rv8803_rtc_ops = {
  125. .get = rv8803_rtc_get,
  126. .set = rv8803_rtc_set,
  127. .reset = rv8803_rtc_reset,
  128. };
  129. static const struct udevice_id rv8803_rtc_ids[] = {
  130. { .compatible = "microcrystal,rv8803", },
  131. { }
  132. };
  133. U_BOOT_DRIVER(rtc_rv8803) = {
  134. .name = "rtc-rv8803",
  135. .id = UCLASS_RTC,
  136. .probe = rv8803_probe,
  137. .of_match = rv8803_rtc_ids,
  138. .ops = &rv8803_rtc_ops,
  139. };