ds1374.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_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 CONFIG_SYS_I2C_RTC_ADDR
  46. # define CONFIG_SYS_I2C_RTC_ADDR 0x68
  47. #endif
  48. #if defined(CONFIG_RTC_DS1374) && (CONFIG_SYS_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. int rtc_get (struct rtc_time *tm){
  94. int rel = 0;
  95. unsigned long time1, time2;
  96. unsigned int limit;
  97. unsigned char tmp;
  98. unsigned int i;
  99. /*
  100. * Since the reads are being performed one byte at a time,
  101. * there is a chance that a carry will occur during the read.
  102. * To detect this, 2 reads are performed and compared.
  103. */
  104. limit = 10;
  105. do {
  106. i = 4;
  107. time1 = 0;
  108. while (i--) {
  109. tmp = rtc_read(RtcTodAddr[i]);
  110. time1 = (time1 << 8) | (tmp & 0xff);
  111. }
  112. i = 4;
  113. time2 = 0;
  114. while (i--) {
  115. tmp = rtc_read(RtcTodAddr[i]);
  116. time2 = (time2 << 8) | (tmp & 0xff);
  117. }
  118. } while ((time1 != time2) && limit--);
  119. if (time1 != time2) {
  120. printf("can't get consistent time from rtc chip\n");
  121. rel = -1;
  122. }
  123. DEBUGR ("Get RTC s since 1.1.1970: %ld\n", time1);
  124. to_tm(time1, tm); /* To Gregorian Date */
  125. if (rtc_read(RTC_SR_ADDR) & RTC_SR_BIT_OSF) {
  126. printf ("### Warning: RTC oscillator has stopped\n");
  127. rel = -1;
  128. }
  129. DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  130. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  131. tm->tm_hour, tm->tm_min, tm->tm_sec);
  132. return rel;
  133. }
  134. /*
  135. * Set the RTC
  136. */
  137. int rtc_set (struct rtc_time *tmp){
  138. unsigned long time;
  139. unsigned i;
  140. DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  141. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  142. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  143. if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
  144. printf("WARNING: year should be between 1970 and 2069!\n");
  145. time = mktime(tmp->tm_year, tmp->tm_mon,
  146. tmp->tm_mday, tmp->tm_hour,
  147. tmp->tm_min, tmp->tm_sec);
  148. DEBUGR ("Set RTC s since 1.1.1970: %ld (0x%02lx)\n", time, time);
  149. /* write to RTC_TOD_CNT_BYTEn_ADDR */
  150. for (i = 0; i <= 3; i++) {
  151. rtc_write_raw(RtcTodAddr[i], (unsigned char)(time & 0xff));
  152. time = time >> 8;
  153. }
  154. /* Start clock */
  155. rtc_write(RTC_CTL_ADDR, RTC_CTL_BIT_EN_OSC, FALSE);
  156. return 0;
  157. }
  158. /*
  159. * Reset the RTC. We setting the date back to 1970-01-01.
  160. * We also enable the oscillator output on the SQW/OUT pin and program
  161. * it for 32,768 Hz output. Note that according to the datasheet, turning
  162. * on the square wave output increases the current drain on the backup
  163. * battery to something between 480nA and 800nA.
  164. */
  165. void rtc_reset (void){
  166. struct rtc_time tmp;
  167. /* clear status flags */
  168. rtc_write (RTC_SR_ADDR, (RTC_SR_BIT_AF|RTC_SR_BIT_OSF), FALSE); /* clearing OSF and AF */
  169. /* Initialise DS1374 oriented to MPC8349E-ADS */
  170. rtc_write (RTC_CTL_ADDR, (RTC_CTL_BIT_EN_OSC
  171. |RTC_CTL_BIT_WACE
  172. |RTC_CTL_BIT_AIE), FALSE);/* start osc, disable WACE, clear AIE
  173. - set to 0 */
  174. rtc_write (RTC_CTL_ADDR, (RTC_CTL_BIT_WD_ALM
  175. |RTC_CTL_BIT_WDSTR
  176. |RTC_CTL_BIT_RS1
  177. |RTC_CTL_BIT_RS2
  178. |RTC_CTL_BIT_BBSQW), TRUE);/* disable WD/ALM, WDSTR set to INT-pin,
  179. set BBSQW and SQW to 32k
  180. - set to 1 */
  181. tmp.tm_year = 1970;
  182. tmp.tm_mon = 1;
  183. tmp.tm_mday= 1;
  184. tmp.tm_hour = 0;
  185. tmp.tm_min = 0;
  186. tmp.tm_sec = 0;
  187. rtc_set(&tmp);
  188. printf("RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  189. tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
  190. tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
  191. rtc_write(RTC_WD_ALM_CNT_BYTE2_ADDR,0xAC, TRUE);
  192. rtc_write(RTC_WD_ALM_CNT_BYTE1_ADDR,0xDE, TRUE);
  193. rtc_write(RTC_WD_ALM_CNT_BYTE2_ADDR,0xAD, TRUE);
  194. }
  195. /*
  196. * Helper functions
  197. */
  198. static uchar rtc_read (uchar reg)
  199. {
  200. return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg));
  201. }
  202. static void rtc_write (uchar reg, uchar val, boolean_t set)
  203. {
  204. if (set == TRUE) {
  205. val |= i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg);
  206. i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  207. } else {
  208. val = i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg) & ~val;
  209. i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  210. }
  211. }
  212. static void rtc_write_raw (uchar reg, uchar val)
  213. {
  214. i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  215. }
  216. #endif