ds1374.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. * DS1374 Real Time Clock (RTC).
  11. *
  12. * based on ds1337.c
  13. */
  14. #include <common.h>
  15. #include <command.h>
  16. #include <rtc.h>
  17. #include <i2c.h>
  18. #if defined(CONFIG_CMD_DATE)
  19. /*---------------------------------------------------------------------*/
  20. #undef DEBUG_RTC
  21. #define DEBUG_RTC
  22. #ifdef DEBUG_RTC
  23. #define DEBUGR(fmt,args...) printf(fmt ,##args)
  24. #else
  25. #define DEBUGR(fmt,args...)
  26. #endif
  27. /*---------------------------------------------------------------------*/
  28. #ifndef CONFIG_SYS_I2C_RTC_ADDR
  29. # define CONFIG_SYS_I2C_RTC_ADDR 0x68
  30. #endif
  31. #if defined(CONFIG_RTC_DS1374) && (CONFIG_SYS_I2C_SPEED > 400000)
  32. # error The DS1374 is specified up to 400kHz in fast mode!
  33. #endif
  34. /*
  35. * RTC register addresses
  36. */
  37. #define RTC_TOD_CNT_BYTE0_ADDR 0x00 /* TimeOfDay */
  38. #define RTC_TOD_CNT_BYTE1_ADDR 0x01
  39. #define RTC_TOD_CNT_BYTE2_ADDR 0x02
  40. #define RTC_TOD_CNT_BYTE3_ADDR 0x03
  41. #define RTC_WD_ALM_CNT_BYTE0_ADDR 0x04
  42. #define RTC_WD_ALM_CNT_BYTE1_ADDR 0x05
  43. #define RTC_WD_ALM_CNT_BYTE2_ADDR 0x06
  44. #define RTC_CTL_ADDR 0x07 /* RTC-CoNTrol-register */
  45. #define RTC_SR_ADDR 0x08 /* RTC-StatusRegister */
  46. #define RTC_TCS_DS_ADDR 0x09 /* RTC-TrickleChargeSelect DiodeSelect-register */
  47. #define RTC_CTL_BIT_AIE (1<<0) /* Bit 0 - Alarm Interrupt enable */
  48. #define RTC_CTL_BIT_RS1 (1<<1) /* Bit 1/2 - Rate Select square wave output */
  49. #define RTC_CTL_BIT_RS2 (1<<2) /* Bit 2/2 - Rate Select square wave output */
  50. #define RTC_CTL_BIT_WDSTR (1<<3) /* Bit 3 - Watchdog Reset Steering */
  51. #define RTC_CTL_BIT_BBSQW (1<<4) /* Bit 4 - Battery-Backed Square-Wave */
  52. #define RTC_CTL_BIT_WD_ALM (1<<5) /* Bit 5 - Watchdoc/Alarm Counter Select */
  53. #define RTC_CTL_BIT_WACE (1<<6) /* Bit 6 - Watchdog/Alarm Counter Enable WACE*/
  54. #define RTC_CTL_BIT_EN_OSC (1<<7) /* Bit 7 - Enable Oscilator */
  55. #define RTC_SR_BIT_AF 0x01 /* Bit 0 = Alarm Flag */
  56. #define RTC_SR_BIT_OSF 0x80 /* Bit 7 - Osc Stop Flag */
  57. const char RtcTodAddr[] = {
  58. RTC_TOD_CNT_BYTE0_ADDR,
  59. RTC_TOD_CNT_BYTE1_ADDR,
  60. RTC_TOD_CNT_BYTE2_ADDR,
  61. RTC_TOD_CNT_BYTE3_ADDR
  62. };
  63. static uchar rtc_read (uchar reg);
  64. static void rtc_write(uchar reg, uchar val, bool set);
  65. static void rtc_write_raw (uchar reg, uchar val);
  66. /*
  67. * Get the current time from the RTC
  68. */
  69. int rtc_get (struct rtc_time *tm){
  70. int rel = 0;
  71. unsigned long time1, time2;
  72. unsigned int limit;
  73. unsigned char tmp;
  74. unsigned int i;
  75. /*
  76. * Since the reads are being performed one byte at a time,
  77. * there is a chance that a carry will occur during the read.
  78. * To detect this, 2 reads are performed and compared.
  79. */
  80. limit = 10;
  81. do {
  82. i = 4;
  83. time1 = 0;
  84. while (i--) {
  85. tmp = rtc_read(RtcTodAddr[i]);
  86. time1 = (time1 << 8) | (tmp & 0xff);
  87. }
  88. i = 4;
  89. time2 = 0;
  90. while (i--) {
  91. tmp = rtc_read(RtcTodAddr[i]);
  92. time2 = (time2 << 8) | (tmp & 0xff);
  93. }
  94. } while ((time1 != time2) && limit--);
  95. if (time1 != time2) {
  96. printf("can't get consistent time from rtc chip\n");
  97. rel = -1;
  98. }
  99. DEBUGR ("Get RTC s since 1.1.1970: %ld\n", time1);
  100. rtc_to_tm(time1, tm); /* To Gregorian Date */
  101. if (rtc_read(RTC_SR_ADDR) & RTC_SR_BIT_OSF) {
  102. printf ("### Warning: RTC oscillator has stopped\n");
  103. rel = -1;
  104. }
  105. DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  106. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  107. tm->tm_hour, tm->tm_min, tm->tm_sec);
  108. return rel;
  109. }
  110. /*
  111. * Set the RTC
  112. */
  113. int rtc_set (struct rtc_time *tmp){
  114. unsigned long time;
  115. unsigned i;
  116. DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  117. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  118. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  119. if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
  120. printf("WARNING: year should be between 1970 and 2069!\n");
  121. time = rtc_mktime(tmp);
  122. DEBUGR ("Set RTC s since 1.1.1970: %ld (0x%02lx)\n", time, time);
  123. /* write to RTC_TOD_CNT_BYTEn_ADDR */
  124. for (i = 0; i <= 3; i++) {
  125. rtc_write_raw(RtcTodAddr[i], (unsigned char)(time & 0xff));
  126. time = time >> 8;
  127. }
  128. /* Start clock */
  129. rtc_write(RTC_CTL_ADDR, RTC_CTL_BIT_EN_OSC, false);
  130. return 0;
  131. }
  132. /*
  133. * Reset the RTC. We setting the date back to 1970-01-01.
  134. * We also enable the oscillator output on the SQW/OUT pin and program
  135. * it for 32,768 Hz output. Note that according to the datasheet, turning
  136. * on the square wave output increases the current drain on the backup
  137. * battery to something between 480nA and 800nA.
  138. */
  139. void rtc_reset (void){
  140. /* clear status flags */
  141. rtc_write(RTC_SR_ADDR, (RTC_SR_BIT_AF|RTC_SR_BIT_OSF), false); /* clearing OSF and AF */
  142. /* Initialise DS1374 oriented to MPC8349E-ADS */
  143. rtc_write (RTC_CTL_ADDR, (RTC_CTL_BIT_EN_OSC
  144. |RTC_CTL_BIT_WACE
  145. |RTC_CTL_BIT_AIE), false);/* start osc, disable WACE, clear AIE
  146. - set to 0 */
  147. rtc_write (RTC_CTL_ADDR, (RTC_CTL_BIT_WD_ALM
  148. |RTC_CTL_BIT_WDSTR
  149. |RTC_CTL_BIT_RS1
  150. |RTC_CTL_BIT_RS2
  151. |RTC_CTL_BIT_BBSQW), true);/* disable WD/ALM, WDSTR set to INT-pin,
  152. set BBSQW and SQW to 32k
  153. - set to 1 */
  154. rtc_write(RTC_WD_ALM_CNT_BYTE2_ADDR, 0xAC, true);
  155. rtc_write(RTC_WD_ALM_CNT_BYTE1_ADDR, 0xDE, true);
  156. rtc_write(RTC_WD_ALM_CNT_BYTE2_ADDR, 0xAD, true);
  157. }
  158. /*
  159. * Helper functions
  160. */
  161. static uchar rtc_read (uchar reg)
  162. {
  163. return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg));
  164. }
  165. static void rtc_write(uchar reg, uchar val, bool set)
  166. {
  167. if (set == true) {
  168. val |= i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg);
  169. i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  170. } else {
  171. val = i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg) & ~val;
  172. i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  173. }
  174. }
  175. static void rtc_write_raw (uchar reg, uchar val)
  176. {
  177. i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  178. }
  179. #endif