ds1374.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * (C) Copyright 2001, 2002, 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. * Keith Outwater, keith_outwater@mvis.com`
  5. * Steven Scholz, steven.scholz@imc-berlin.de
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. /*
  26. * Date & Time support (no alarms) for Dallas Semiconductor (now Maxim)
  27. * DS1374 Real Time Clock (RTC).
  28. *
  29. * based on ds1337.c
  30. */
  31. #include <common.h>
  32. #include <command.h>
  33. #include <rtc.h>
  34. #include <i2c.h>
  35. #if (defined(CONFIG_RTC_DS1374)) && defined(CONFIG_CMD_DATE)
  36. /*---------------------------------------------------------------------*/
  37. #undef DEBUG_RTC
  38. #define DEBUG_RTC
  39. #ifdef DEBUG_RTC
  40. #define DEBUGR(fmt,args...) printf(fmt ,##args)
  41. #else
  42. #define DEBUGR(fmt,args...)
  43. #endif
  44. /*---------------------------------------------------------------------*/
  45. #ifndef CFG_I2C_RTC_ADDR
  46. # define CFG_I2C_RTC_ADDR 0x68
  47. #endif
  48. #if defined(CONFIG_RTC_DS1374) && (CFG_I2C_SPEED > 400000)
  49. # error The DS1374 is specified up to 400kHz in fast mode!
  50. #endif
  51. /*
  52. * RTC register addresses
  53. */
  54. #define RTC_TOD_CNT_BYTE0_ADDR 0x00 /* TimeOfDay */
  55. #define RTC_TOD_CNT_BYTE1_ADDR 0x01
  56. #define RTC_TOD_CNT_BYTE2_ADDR 0x02
  57. #define RTC_TOD_CNT_BYTE3_ADDR 0x03
  58. #define RTC_WD_ALM_CNT_BYTE0_ADDR 0x04
  59. #define RTC_WD_ALM_CNT_BYTE1_ADDR 0x05
  60. #define RTC_WD_ALM_CNT_BYTE2_ADDR 0x06
  61. #define RTC_CTL_ADDR 0x07 /* RTC-CoNTrol-register */
  62. #define RTC_SR_ADDR 0x08 /* RTC-StatusRegister */
  63. #define RTC_TCS_DS_ADDR 0x09 /* RTC-TrickleChargeSelect DiodeSelect-register */
  64. #define RTC_CTL_BIT_AIE (1<<0) /* Bit 0 - Alarm Interrupt enable */
  65. #define RTC_CTL_BIT_RS1 (1<<1) /* Bit 1/2 - Rate Select square wave output */
  66. #define RTC_CTL_BIT_RS2 (1<<2) /* Bit 2/2 - Rate Select square wave output */
  67. #define RTC_CTL_BIT_WDSTR (1<<3) /* Bit 3 - Watchdog Reset Steering */
  68. #define RTC_CTL_BIT_BBSQW (1<<4) /* Bit 4 - Battery-Backed Square-Wave */
  69. #define RTC_CTL_BIT_WD_ALM (1<<5) /* Bit 5 - Watchdoc/Alarm Counter Select */
  70. #define RTC_CTL_BIT_WACE (1<<6) /* Bit 6 - Watchdog/Alarm Counter Enable WACE*/
  71. #define RTC_CTL_BIT_EN_OSC (1<<7) /* Bit 7 - Enable Oscilator */
  72. #define RTC_SR_BIT_AF 0x01 /* Bit 0 = Alarm Flag */
  73. #define RTC_SR_BIT_OSF 0x80 /* Bit 7 - Osc Stop Flag */
  74. typedef unsigned char boolean_t;
  75. #ifndef TRUE
  76. #define TRUE ((boolean_t)(0==0))
  77. #endif
  78. #ifndef FALSE
  79. #define FALSE (!TRUE)
  80. #endif
  81. const char RtcTodAddr[] = {
  82. RTC_TOD_CNT_BYTE0_ADDR,
  83. RTC_TOD_CNT_BYTE1_ADDR,
  84. RTC_TOD_CNT_BYTE2_ADDR,
  85. RTC_TOD_CNT_BYTE3_ADDR
  86. };
  87. static uchar rtc_read (uchar reg);
  88. static void rtc_write (uchar reg, uchar val, boolean_t set);
  89. static void rtc_write_raw (uchar reg, uchar val);
  90. /*
  91. * Get the current time from the RTC
  92. */
  93. void rtc_get (struct rtc_time *tm){
  94. unsigned long time1, time2;
  95. unsigned int limit;
  96. unsigned char tmp;
  97. unsigned int i;
  98. /*
  99. * Since the reads are being performed one byte at a time,
  100. * there is a chance that a carry will occur during the read.
  101. * To detect this, 2 reads are performed and compared.
  102. */
  103. limit = 10;
  104. do {
  105. i = 4;
  106. time1 = 0;
  107. while (i--) {
  108. tmp = rtc_read(RtcTodAddr[i]);
  109. time1 = (time1 << 8) | (tmp & 0xff);
  110. }
  111. i = 4;
  112. time2 = 0;
  113. while (i--) {
  114. tmp = rtc_read(RtcTodAddr[i]);
  115. time2 = (time2 << 8) | (tmp & 0xff);
  116. }
  117. } while ((time1 != time2) && limit--);
  118. if (time1 != time2) {
  119. printf("can't get consistent time from rtc chip\n");
  120. }
  121. DEBUGR ("Get RTC s since 1.1.1970: %d\n", time1);
  122. to_tm(time1, tm); /* To Gregorian Date */
  123. if (rtc_read(RTC_SR_ADDR) & RTC_SR_BIT_OSF)
  124. printf ("### Warning: RTC oscillator has stopped\n");
  125. DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  126. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  127. tm->tm_hour, tm->tm_min, tm->tm_sec);
  128. }
  129. /*
  130. * Set the RTC
  131. */
  132. void rtc_set (struct rtc_time *tmp){
  133. unsigned long time;
  134. unsigned i;
  135. DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  136. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  137. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  138. if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
  139. printf("WARNING: year should be between 1970 and 2069!\n");
  140. time = mktime(tmp->tm_year, tmp->tm_mon,
  141. tmp->tm_mday, tmp->tm_hour,
  142. tmp->tm_min, tmp->tm_sec);
  143. DEBUGR ("Set RTC s since 1.1.1970: %d (0x%02x)\n", time, time);
  144. /* write to RTC_TOD_CNT_BYTEn_ADDR */
  145. for (i = 0; i <= 3; i++) {
  146. rtc_write_raw(RtcTodAddr[i], (unsigned char)(time & 0xff));
  147. time = time >> 8;
  148. }
  149. /* Start clock */
  150. rtc_write(RTC_CTL_ADDR, RTC_CTL_BIT_EN_OSC, FALSE);
  151. }
  152. /*
  153. * Reset the RTC. We setting the date back to 1970-01-01.
  154. * We also enable the oscillator output on the SQW/OUT pin and program
  155. * it for 32,768 Hz output. Note that according to the datasheet, turning
  156. * on the square wave output increases the current drain on the backup
  157. * battery to something between 480nA and 800nA.
  158. */
  159. void rtc_reset (void){
  160. struct rtc_time tmp;
  161. /* clear status flags */
  162. rtc_write (RTC_SR_ADDR, (RTC_SR_BIT_AF|RTC_SR_BIT_OSF), FALSE); /* clearing OSF and AF */
  163. /* Initialise DS1374 oriented to MPC8349E-ADS */
  164. rtc_write (RTC_CTL_ADDR, (RTC_CTL_BIT_EN_OSC
  165. |RTC_CTL_BIT_WACE
  166. |RTC_CTL_BIT_AIE), FALSE);/* start osc, disable WACE, clear AIE
  167. - set to 0 */
  168. rtc_write (RTC_CTL_ADDR, (RTC_CTL_BIT_WD_ALM
  169. |RTC_CTL_BIT_WDSTR
  170. |RTC_CTL_BIT_RS1
  171. |RTC_CTL_BIT_RS2
  172. |RTC_CTL_BIT_BBSQW), TRUE);/* disable WD/ALM, WDSTR set to INT-pin,
  173. set BBSQW and SQW to 32k
  174. - set to 1 */
  175. tmp.tm_year = 1970;
  176. tmp.tm_mon = 1;
  177. tmp.tm_mday= 1;
  178. tmp.tm_hour = 0;
  179. tmp.tm_min = 0;
  180. tmp.tm_sec = 0;
  181. rtc_set(&tmp);
  182. printf("RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  183. tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
  184. tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
  185. rtc_write(RTC_WD_ALM_CNT_BYTE2_ADDR,0xAC, TRUE);
  186. rtc_write(RTC_WD_ALM_CNT_BYTE1_ADDR,0xDE, TRUE);
  187. rtc_write(RTC_WD_ALM_CNT_BYTE2_ADDR,0xAD, TRUE);
  188. }
  189. /*
  190. * Helper functions
  191. */
  192. static uchar rtc_read (uchar reg)
  193. {
  194. return (i2c_reg_read (CFG_I2C_RTC_ADDR, reg));
  195. }
  196. static void rtc_write (uchar reg, uchar val, boolean_t set)
  197. {
  198. if (set == TRUE) {
  199. val |= i2c_reg_read (CFG_I2C_RTC_ADDR, reg);
  200. i2c_reg_write (CFG_I2C_RTC_ADDR, reg, val);
  201. } else {
  202. val = i2c_reg_read (CFG_I2C_RTC_ADDR, reg) & ~val;
  203. i2c_reg_write (CFG_I2C_RTC_ADDR, reg, val);
  204. }
  205. }
  206. static void rtc_write_raw (uchar reg, uchar val)
  207. {
  208. i2c_reg_write (CFG_I2C_RTC_ADDR, reg, val);
  209. }
  210. #endif