ds3231.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2006
  4. * Markus Klotzbuecher, mk@denx.de
  5. *
  6. * (C) Copyright 2019 NXP
  7. * Chuanhua Han <chuanhua.han@nxp.com>
  8. */
  9. /*
  10. * Date & Time support (no alarms) for Dallas Semiconductor (now Maxim)
  11. * Extremly Accurate DS3231 Real Time Clock (RTC).
  12. *
  13. * copied from ds1337.c
  14. */
  15. #include <common.h>
  16. #include <command.h>
  17. #include <dm.h>
  18. #include <log.h>
  19. #include <rtc.h>
  20. #include <i2c.h>
  21. /*
  22. * RTC register addresses
  23. */
  24. #define RTC_SEC_REG_ADDR 0x0
  25. #define RTC_MIN_REG_ADDR 0x1
  26. #define RTC_HR_REG_ADDR 0x2
  27. #define RTC_DAY_REG_ADDR 0x3
  28. #define RTC_DATE_REG_ADDR 0x4
  29. #define RTC_MON_REG_ADDR 0x5
  30. #define RTC_YR_REG_ADDR 0x6
  31. #define RTC_CTL_REG_ADDR 0x0e
  32. #define RTC_STAT_REG_ADDR 0x0f
  33. /*
  34. * RTC control register bits
  35. */
  36. #define RTC_CTL_BIT_A1IE 0x1 /* Alarm 1 interrupt enable */
  37. #define RTC_CTL_BIT_A2IE 0x2 /* Alarm 2 interrupt enable */
  38. #define RTC_CTL_BIT_INTCN 0x4 /* Interrupt control */
  39. #define RTC_CTL_BIT_RS1 0x8 /* Rate select 1 */
  40. #define RTC_CTL_BIT_RS2 0x10 /* Rate select 2 */
  41. #define RTC_CTL_BIT_DOSC 0x80 /* Disable Oscillator */
  42. /*
  43. * RTC status register bits
  44. */
  45. #define RTC_STAT_BIT_A1F 0x1 /* Alarm 1 flag */
  46. #define RTC_STAT_BIT_A2F 0x2 /* Alarm 2 flag */
  47. #define RTC_STAT_BIT_OSF 0x80 /* Oscillator stop flag */
  48. #define RTC_STAT_BIT_BB32KHZ 0x40 /* Battery backed 32KHz Output */
  49. #define RTC_STAT_BIT_EN32KHZ 0x8 /* Enable 32KHz Output */
  50. #if !CONFIG_IS_ENABLED(DM_RTC)
  51. static uchar rtc_read (uchar reg);
  52. static void rtc_write (uchar reg, uchar val);
  53. /*
  54. * Get the current time from the RTC
  55. */
  56. int rtc_get (struct rtc_time *tmp)
  57. {
  58. int rel = 0;
  59. uchar sec, min, hour, mday, wday, mon_cent, year, control, status;
  60. control = rtc_read (RTC_CTL_REG_ADDR);
  61. status = rtc_read (RTC_STAT_REG_ADDR);
  62. sec = rtc_read (RTC_SEC_REG_ADDR);
  63. min = rtc_read (RTC_MIN_REG_ADDR);
  64. hour = rtc_read (RTC_HR_REG_ADDR);
  65. wday = rtc_read (RTC_DAY_REG_ADDR);
  66. mday = rtc_read (RTC_DATE_REG_ADDR);
  67. mon_cent = rtc_read (RTC_MON_REG_ADDR);
  68. year = rtc_read (RTC_YR_REG_ADDR);
  69. debug("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  70. "hr: %02x min: %02x sec: %02x control: %02x status: %02x\n",
  71. year, mon_cent, mday, wday, hour, min, sec, control, status);
  72. if (status & RTC_STAT_BIT_OSF) {
  73. printf ("### Warning: RTC oscillator has stopped\n");
  74. /* clear the OSF flag */
  75. rtc_write (RTC_STAT_REG_ADDR,
  76. rtc_read (RTC_STAT_REG_ADDR) & ~RTC_STAT_BIT_OSF);
  77. rel = -1;
  78. }
  79. tmp->tm_sec = bcd2bin (sec & 0x7F);
  80. tmp->tm_min = bcd2bin (min & 0x7F);
  81. tmp->tm_hour = bcd2bin (hour & 0x3F);
  82. tmp->tm_mday = bcd2bin (mday & 0x3F);
  83. tmp->tm_mon = bcd2bin (mon_cent & 0x1F);
  84. tmp->tm_year = bcd2bin (year) + ((mon_cent & 0x80) ? 2000 : 1900);
  85. tmp->tm_wday = bcd2bin ((wday - 1) & 0x07);
  86. tmp->tm_yday = 0;
  87. tmp->tm_isdst= 0;
  88. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  89. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  90. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  91. return rel;
  92. }
  93. /*
  94. * Set the RTC
  95. */
  96. int rtc_set (struct rtc_time *tmp)
  97. {
  98. uchar century;
  99. debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  100. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  101. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  102. rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
  103. century = (tmp->tm_year >= 2000) ? 0x80 : 0;
  104. rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon) | century);
  105. rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday + 1));
  106. rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
  107. rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
  108. rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
  109. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
  110. return 0;
  111. }
  112. /*
  113. * Reset the RTC. We also enable the oscillator output on the
  114. * SQW/INTB* pin and program it for 32,768 Hz output. Note that
  115. * according to the datasheet, turning on the square wave output
  116. * increases the current drain on the backup battery from about
  117. * 600 nA to 2uA.
  118. */
  119. void rtc_reset (void)
  120. {
  121. rtc_write (RTC_CTL_REG_ADDR, RTC_CTL_BIT_RS1 | RTC_CTL_BIT_RS2);
  122. }
  123. /*
  124. * Enable 32KHz output
  125. */
  126. #ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT
  127. void rtc_enable_32khz_output(void)
  128. {
  129. rtc_write(RTC_STAT_REG_ADDR,
  130. RTC_STAT_BIT_BB32KHZ | RTC_STAT_BIT_EN32KHZ);
  131. }
  132. #endif
  133. /*
  134. * Helper functions
  135. */
  136. static
  137. uchar rtc_read (uchar reg)
  138. {
  139. return (i2c_reg_read (CFG_SYS_I2C_RTC_ADDR, reg));
  140. }
  141. static void rtc_write (uchar reg, uchar val)
  142. {
  143. i2c_reg_write (CFG_SYS_I2C_RTC_ADDR, reg, val);
  144. }
  145. #else
  146. static int ds3231_rtc_get(struct udevice *dev, struct rtc_time *tmp)
  147. {
  148. uchar sec, min, hour, mday, wday, mon_cent, year, status;
  149. status = dm_i2c_reg_read(dev, RTC_STAT_REG_ADDR);
  150. sec = dm_i2c_reg_read(dev, RTC_SEC_REG_ADDR);
  151. min = dm_i2c_reg_read(dev, RTC_MIN_REG_ADDR);
  152. hour = dm_i2c_reg_read(dev, RTC_HR_REG_ADDR);
  153. wday = dm_i2c_reg_read(dev, RTC_DAY_REG_ADDR);
  154. mday = dm_i2c_reg_read(dev, RTC_DATE_REG_ADDR);
  155. mon_cent = dm_i2c_reg_read(dev, RTC_MON_REG_ADDR);
  156. year = dm_i2c_reg_read(dev, RTC_YR_REG_ADDR);
  157. if (status & RTC_STAT_BIT_OSF) {
  158. printf("### Warning: RTC oscillator has stopped\n");
  159. /* clear the OSF flag */
  160. dm_i2c_reg_write(dev, RTC_STAT_REG_ADDR,
  161. dm_i2c_reg_read(dev, RTC_STAT_REG_ADDR)
  162. & ~RTC_STAT_BIT_OSF);
  163. return -EINVAL;
  164. }
  165. tmp->tm_sec = bcd2bin(sec & 0x7F);
  166. tmp->tm_min = bcd2bin(min & 0x7F);
  167. tmp->tm_hour = bcd2bin(hour & 0x3F);
  168. tmp->tm_mday = bcd2bin(mday & 0x3F);
  169. tmp->tm_mon = bcd2bin(mon_cent & 0x1F);
  170. tmp->tm_year = bcd2bin(year) + ((mon_cent & 0x80) ? 2000 : 1900);
  171. tmp->tm_wday = bcd2bin((wday - 1) & 0x07);
  172. tmp->tm_yday = 0;
  173. tmp->tm_isdst = 0;
  174. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  175. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  176. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  177. return 0;
  178. }
  179. static int ds3231_rtc_set(struct udevice *dev, const struct rtc_time *tmp)
  180. {
  181. uchar century;
  182. debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  183. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  184. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  185. dm_i2c_reg_write(dev, RTC_YR_REG_ADDR, bin2bcd(tmp->tm_year % 100));
  186. century = (tmp->tm_year >= 2000) ? 0x80 : 0;
  187. dm_i2c_reg_write(dev, RTC_MON_REG_ADDR, bin2bcd(tmp->tm_mon) | century);
  188. dm_i2c_reg_write(dev, RTC_DAY_REG_ADDR, bin2bcd(tmp->tm_wday + 1));
  189. dm_i2c_reg_write(dev, RTC_DATE_REG_ADDR, bin2bcd(tmp->tm_mday));
  190. dm_i2c_reg_write(dev, RTC_HR_REG_ADDR, bin2bcd(tmp->tm_hour));
  191. dm_i2c_reg_write(dev, RTC_MIN_REG_ADDR, bin2bcd(tmp->tm_min));
  192. dm_i2c_reg_write(dev, RTC_SEC_REG_ADDR, bin2bcd(tmp->tm_sec));
  193. return 0;
  194. }
  195. static int ds3231_rtc_reset(struct udevice *dev)
  196. {
  197. int ret;
  198. ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR,
  199. RTC_CTL_BIT_RS1 | RTC_CTL_BIT_RS2);
  200. if (ret < 0)
  201. return ret;
  202. return 0;
  203. }
  204. static int ds3231_probe(struct udevice *dev)
  205. {
  206. i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
  207. DM_I2C_CHIP_WR_ADDRESS);
  208. return 0;
  209. }
  210. #ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT
  211. int rtc_enable_32khz_output(int busnum, int chip_addr)
  212. {
  213. int ret;
  214. struct udevice *dev;
  215. ret = i2c_get_chip_for_busnum(busnum, chip_addr, 1, &dev);
  216. if (!ret) {
  217. ret = dm_i2c_reg_write(dev, RTC_STAT_REG_ADDR,
  218. RTC_STAT_BIT_BB32KHZ |
  219. RTC_STAT_BIT_EN32KHZ);
  220. }
  221. return ret;
  222. }
  223. #endif
  224. static const struct rtc_ops ds3231_rtc_ops = {
  225. .get = ds3231_rtc_get,
  226. .set = ds3231_rtc_set,
  227. .reset = ds3231_rtc_reset,
  228. };
  229. static const struct udevice_id ds3231_rtc_ids[] = {
  230. { .compatible = "dallas,ds3231" },
  231. { .compatible = "dallas,ds3232" },
  232. { }
  233. };
  234. U_BOOT_DRIVER(rtc_ds3231) = {
  235. .name = "rtc-ds3231",
  236. .id = UCLASS_RTC,
  237. .probe = ds3231_probe,
  238. .of_match = ds3231_rtc_ids,
  239. .ops = &ds3231_rtc_ops,
  240. };
  241. #endif