ds1307.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001, 2002, 2003
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. * Keith Outwater, keith_outwater@mvis.com`
  6. * Steven Scholz, steven.scholz@imc-berlin.de
  7. */
  8. /*
  9. * Date & Time support (no alarms) for Dallas Semiconductor (now Maxim)
  10. * DS1307 and DS1338/9 Real Time Clock (RTC).
  11. *
  12. * based on ds1337.c
  13. */
  14. #include <common.h>
  15. #include <command.h>
  16. #include <dm.h>
  17. #include <log.h>
  18. #include <rtc.h>
  19. #include <i2c.h>
  20. enum ds_type {
  21. ds_1307,
  22. ds_1337,
  23. ds_1339,
  24. ds_1340,
  25. m41t11,
  26. mcp794xx,
  27. };
  28. /*
  29. * RTC register addresses
  30. */
  31. #define RTC_SEC_REG_ADDR 0x00
  32. #define RTC_MIN_REG_ADDR 0x01
  33. #define RTC_HR_REG_ADDR 0x02
  34. #define RTC_DAY_REG_ADDR 0x03
  35. #define RTC_DATE_REG_ADDR 0x04
  36. #define RTC_MON_REG_ADDR 0x05
  37. #define RTC_YR_REG_ADDR 0x06
  38. #define RTC_CTL_REG_ADDR 0x07
  39. #define RTC_SEC_BIT_CH 0x80 /* Clock Halt (in Register 0) */
  40. #define RTC_CTL_BIT_RS0 0x01 /* Rate select 0 */
  41. #define RTC_CTL_BIT_RS1 0x02 /* Rate select 1 */
  42. #define RTC_CTL_BIT_SQWE 0x10 /* Square Wave Enable */
  43. #define RTC_CTL_BIT_OUT 0x80 /* Output Control */
  44. /* MCP7941X-specific bits */
  45. #define MCP7941X_BIT_ST 0x80
  46. #define MCP7941X_BIT_VBATEN 0x08
  47. #ifndef CONFIG_DM_RTC
  48. /*---------------------------------------------------------------------*/
  49. #undef DEBUG_RTC
  50. #ifdef DEBUG_RTC
  51. #define DEBUGR(fmt, args...) printf(fmt, ##args)
  52. #else
  53. #define DEBUGR(fmt, args...)
  54. #endif
  55. /*---------------------------------------------------------------------*/
  56. #ifndef CONFIG_SYS_I2C_RTC_ADDR
  57. # define CONFIG_SYS_I2C_RTC_ADDR 0x68
  58. #endif
  59. #if defined(CONFIG_RTC_DS1307) && (CONFIG_SYS_I2C_SPEED > 100000)
  60. # error The DS1307 is specified only up to 100kHz!
  61. #endif
  62. static uchar rtc_read (uchar reg);
  63. static void rtc_write (uchar reg, uchar val);
  64. /*
  65. * Get the current time from the RTC
  66. */
  67. int rtc_get (struct rtc_time *tmp)
  68. {
  69. int rel = 0;
  70. uchar sec, min, hour, mday, wday, mon, year;
  71. #ifdef CONFIG_RTC_MCP79411
  72. read_rtc:
  73. #endif
  74. sec = rtc_read (RTC_SEC_REG_ADDR);
  75. min = rtc_read (RTC_MIN_REG_ADDR);
  76. hour = rtc_read (RTC_HR_REG_ADDR);
  77. wday = rtc_read (RTC_DAY_REG_ADDR);
  78. mday = rtc_read (RTC_DATE_REG_ADDR);
  79. mon = rtc_read (RTC_MON_REG_ADDR);
  80. year = rtc_read (RTC_YR_REG_ADDR);
  81. DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  82. "hr: %02x min: %02x sec: %02x\n",
  83. year, mon, mday, wday, hour, min, sec);
  84. #ifdef CONFIG_RTC_DS1307
  85. if (sec & RTC_SEC_BIT_CH) {
  86. printf ("### Warning: RTC oscillator has stopped\n");
  87. /* clear the CH flag */
  88. rtc_write (RTC_SEC_REG_ADDR,
  89. rtc_read (RTC_SEC_REG_ADDR) & ~RTC_SEC_BIT_CH);
  90. rel = -1;
  91. }
  92. #endif
  93. #ifdef CONFIG_RTC_MCP79411
  94. /* make sure that the backup battery is enabled */
  95. if (!(wday & MCP7941X_BIT_VBATEN)) {
  96. rtc_write(RTC_DAY_REG_ADDR,
  97. wday | MCP7941X_BIT_VBATEN);
  98. }
  99. /* clock halted? turn it on, so clock can tick. */
  100. if (!(sec & MCP7941X_BIT_ST)) {
  101. rtc_write(RTC_SEC_REG_ADDR, MCP7941X_BIT_ST);
  102. printf("Started RTC\n");
  103. goto read_rtc;
  104. }
  105. #endif
  106. tmp->tm_sec = bcd2bin (sec & 0x7F);
  107. tmp->tm_min = bcd2bin (min & 0x7F);
  108. tmp->tm_hour = bcd2bin (hour & 0x3F);
  109. tmp->tm_mday = bcd2bin (mday & 0x3F);
  110. tmp->tm_mon = bcd2bin (mon & 0x1F);
  111. tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
  112. tmp->tm_wday = bcd2bin ((wday - 1) & 0x07);
  113. tmp->tm_yday = 0;
  114. tmp->tm_isdst= 0;
  115. DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  116. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  117. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  118. return rel;
  119. }
  120. /*
  121. * Set the RTC
  122. */
  123. int rtc_set (struct rtc_time *tmp)
  124. {
  125. DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  126. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  127. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  128. if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
  129. printf("WARNING: year should be between 1970 and 2069!\n");
  130. rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
  131. rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
  132. #ifdef CONFIG_RTC_MCP79411
  133. rtc_write (RTC_DAY_REG_ADDR,
  134. bin2bcd (tmp->tm_wday + 1) | MCP7941X_BIT_VBATEN);
  135. #else
  136. rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday + 1));
  137. #endif
  138. rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
  139. rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
  140. rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
  141. #ifdef CONFIG_RTC_MCP79411
  142. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec) | MCP7941X_BIT_ST);
  143. #else
  144. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
  145. #endif
  146. return 0;
  147. }
  148. /*
  149. * Reset the RTC. We setting the date back to 1970-01-01.
  150. * We also enable the oscillator output on the SQW/OUT pin and program
  151. * it for 32,768 Hz output. Note that according to the datasheet, turning
  152. * on the square wave output increases the current drain on the backup
  153. * battery to something between 480nA and 800nA.
  154. */
  155. void rtc_reset (void)
  156. {
  157. rtc_write (RTC_SEC_REG_ADDR, 0x00); /* clearing Clock Halt */
  158. rtc_write (RTC_CTL_REG_ADDR, RTC_CTL_BIT_SQWE | RTC_CTL_BIT_RS1 | RTC_CTL_BIT_RS0);
  159. }
  160. /*
  161. * Helper functions
  162. */
  163. static
  164. uchar rtc_read (uchar reg)
  165. {
  166. return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg));
  167. }
  168. static void rtc_write (uchar reg, uchar val)
  169. {
  170. i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  171. }
  172. #endif /* !CONFIG_DM_RTC */
  173. #ifdef CONFIG_DM_RTC
  174. static int ds1307_rtc_set(struct udevice *dev, const struct rtc_time *tm)
  175. {
  176. int ret;
  177. uchar buf[7];
  178. enum ds_type type = dev_get_driver_data(dev);
  179. debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  180. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  181. tm->tm_hour, tm->tm_min, tm->tm_sec);
  182. if (tm->tm_year < 1970 || tm->tm_year > 2069)
  183. printf("WARNING: year should be between 1970 and 2069!\n");
  184. buf[RTC_YR_REG_ADDR] = bin2bcd(tm->tm_year % 100);
  185. buf[RTC_MON_REG_ADDR] = bin2bcd(tm->tm_mon);
  186. buf[RTC_DAY_REG_ADDR] = bin2bcd(tm->tm_wday + 1);
  187. buf[RTC_DATE_REG_ADDR] = bin2bcd(tm->tm_mday);
  188. buf[RTC_HR_REG_ADDR] = bin2bcd(tm->tm_hour);
  189. buf[RTC_MIN_REG_ADDR] = bin2bcd(tm->tm_min);
  190. buf[RTC_SEC_REG_ADDR] = bin2bcd(tm->tm_sec);
  191. if (type == mcp794xx) {
  192. buf[RTC_DAY_REG_ADDR] |= MCP7941X_BIT_VBATEN;
  193. buf[RTC_SEC_REG_ADDR] |= MCP7941X_BIT_ST;
  194. }
  195. ret = dm_i2c_write(dev, 0, buf, sizeof(buf));
  196. if (ret < 0)
  197. return ret;
  198. return 0;
  199. }
  200. static int ds1307_rtc_get(struct udevice *dev, struct rtc_time *tm)
  201. {
  202. int ret;
  203. uchar buf[7];
  204. enum ds_type type = dev_get_driver_data(dev);
  205. read_rtc:
  206. ret = dm_i2c_read(dev, 0, buf, sizeof(buf));
  207. if (ret < 0)
  208. return ret;
  209. if (type == ds_1307) {
  210. if (buf[RTC_SEC_REG_ADDR] & RTC_SEC_BIT_CH) {
  211. printf("### Warning: RTC oscillator has stopped\n");
  212. /* clear the CH flag */
  213. buf[RTC_SEC_REG_ADDR] &= ~RTC_SEC_BIT_CH;
  214. dm_i2c_reg_write(dev, RTC_SEC_REG_ADDR,
  215. buf[RTC_SEC_REG_ADDR]);
  216. return -1;
  217. }
  218. }
  219. if (type == m41t11) {
  220. /* clock halted? turn it on, so clock can tick. */
  221. if (buf[RTC_SEC_REG_ADDR] & RTC_SEC_BIT_CH) {
  222. buf[RTC_SEC_REG_ADDR] &= ~RTC_SEC_BIT_CH;
  223. dm_i2c_reg_write(dev, RTC_SEC_REG_ADDR,
  224. MCP7941X_BIT_ST);
  225. dm_i2c_reg_write(dev, RTC_SEC_REG_ADDR,
  226. buf[RTC_SEC_REG_ADDR]);
  227. goto read_rtc;
  228. }
  229. }
  230. if (type == mcp794xx) {
  231. /* make sure that the backup battery is enabled */
  232. if (!(buf[RTC_DAY_REG_ADDR] & MCP7941X_BIT_VBATEN)) {
  233. dm_i2c_reg_write(dev, RTC_DAY_REG_ADDR,
  234. buf[RTC_DAY_REG_ADDR] |
  235. MCP7941X_BIT_VBATEN);
  236. }
  237. /* clock halted? turn it on, so clock can tick. */
  238. if (!(buf[RTC_SEC_REG_ADDR] & MCP7941X_BIT_ST)) {
  239. dm_i2c_reg_write(dev, RTC_SEC_REG_ADDR,
  240. MCP7941X_BIT_ST);
  241. printf("Started RTC\n");
  242. goto read_rtc;
  243. }
  244. }
  245. tm->tm_sec = bcd2bin(buf[RTC_SEC_REG_ADDR] & 0x7F);
  246. tm->tm_min = bcd2bin(buf[RTC_MIN_REG_ADDR] & 0x7F);
  247. tm->tm_hour = bcd2bin(buf[RTC_HR_REG_ADDR] & 0x3F);
  248. tm->tm_mday = bcd2bin(buf[RTC_DATE_REG_ADDR] & 0x3F);
  249. tm->tm_mon = bcd2bin(buf[RTC_MON_REG_ADDR] & 0x1F);
  250. tm->tm_year = bcd2bin(buf[RTC_YR_REG_ADDR]) +
  251. (bcd2bin(buf[RTC_YR_REG_ADDR]) >= 70 ?
  252. 1900 : 2000);
  253. tm->tm_wday = bcd2bin((buf[RTC_DAY_REG_ADDR] - 1) & 0x07);
  254. tm->tm_yday = 0;
  255. tm->tm_isdst = 0;
  256. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  257. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  258. tm->tm_hour, tm->tm_min, tm->tm_sec);
  259. return 0;
  260. }
  261. static int ds1307_rtc_reset(struct udevice *dev)
  262. {
  263. int ret;
  264. /* clear Clock Halt */
  265. ret = dm_i2c_reg_write(dev, RTC_SEC_REG_ADDR, 0x00);
  266. if (ret < 0)
  267. return ret;
  268. ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR,
  269. RTC_CTL_BIT_SQWE | RTC_CTL_BIT_RS1 |
  270. RTC_CTL_BIT_RS0);
  271. if (ret < 0)
  272. return ret;
  273. return 0;
  274. }
  275. static int ds1307_probe(struct udevice *dev)
  276. {
  277. i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
  278. DM_I2C_CHIP_WR_ADDRESS);
  279. return 0;
  280. }
  281. static const struct rtc_ops ds1307_rtc_ops = {
  282. .get = ds1307_rtc_get,
  283. .set = ds1307_rtc_set,
  284. .reset = ds1307_rtc_reset,
  285. };
  286. static const struct udevice_id ds1307_rtc_ids[] = {
  287. { .compatible = "dallas,ds1307", .data = ds_1307 },
  288. { .compatible = "dallas,ds1337", .data = ds_1337 },
  289. { .compatible = "dallas,ds1339", .data = ds_1339 },
  290. { .compatible = "dallas,ds1340", .data = ds_1340 },
  291. { .compatible = "microchip,mcp7941x", .data = mcp794xx },
  292. { .compatible = "st,m41t11", .data = m41t11 },
  293. { }
  294. };
  295. U_BOOT_DRIVER(rtc_ds1307) = {
  296. .name = "rtc-ds1307",
  297. .id = UCLASS_RTC,
  298. .probe = ds1307_probe,
  299. .of_match = ds1307_rtc_ids,
  300. .ops = &ds1307_rtc_ops,
  301. };
  302. #endif /* CONFIG_DM_RTC */