rs5c372.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * rs5c372.c
  3. *
  4. * Device driver for Ricoh's Real Time Controller RS5C372A.
  5. *
  6. * Copyright (C) 2004 Gary Jennejohn garyj@denx.de
  7. *
  8. * Based in part in ds1307.c -
  9. * (C) Copyright 2001, 2002, 2003
  10. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  11. * Keith Outwater, keith_outwater@mvis.com`
  12. * Steven Scholz, steven.scholz@imc-berlin.de
  13. *
  14. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License version 2 as
  19. * published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  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. * Reads are always done starting with register 15, which requires some
  38. * jumping-through-hoops to access the data correctly.
  39. *
  40. * Writes are always done starting with register 0.
  41. */
  42. #define DEBUG 0
  43. #if DEBUG
  44. static unsigned int rtc_debug = DEBUG;
  45. #else
  46. #define rtc_debug 0 /* gcc will remove all the debug code for us */
  47. #endif
  48. #ifndef CONFIG_SYS_I2C_RTC_ADDR
  49. #define CONFIG_SYS_I2C_RTC_ADDR 0x32
  50. #endif
  51. #define RS5C372_RAM_SIZE 0x10
  52. #define RATE_32000HZ 0x80 /* Rate Select 32.000KHz */
  53. #define RATE_32768HZ 0x00 /* Rate Select 32.768KHz */
  54. #define STATUS_XPT 0x10 /* data invalid because voltage was 0 */
  55. #define USE_24HOUR_MODE 0x20
  56. #define TWELVE_HOUR_MODE(n) ((((n) >> 5) & 1) == 0)
  57. #define HOURS_AP(n) (((n) >> 5) & 1)
  58. #define HOURS_12(n) bcd2bin((n) & 0x1F)
  59. #define HOURS_24(n) bcd2bin((n) & 0x3F)
  60. static int setup_done = 0;
  61. static int
  62. rs5c372_readram(unsigned char *buf, int len)
  63. {
  64. int ret;
  65. ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, len);
  66. if (ret != 0) {
  67. printf("%s: failed to read\n", __FUNCTION__);
  68. return ret;
  69. }
  70. if (buf[0] & STATUS_XPT)
  71. printf("### Warning: RTC lost power\n");
  72. return ret;
  73. }
  74. static void
  75. rs5c372_enable(void)
  76. {
  77. unsigned char buf[RS5C372_RAM_SIZE + 1];
  78. int ret;
  79. /* note that this returns reg. 15 in buf[1] */
  80. ret = rs5c372_readram(&buf[1], RS5C372_RAM_SIZE);
  81. if (ret != 0) {
  82. printf("%s: failed\n", __FUNCTION__);
  83. return;
  84. }
  85. buf[0] = 0;
  86. /* we want to start writing at register 0 so we have to copy the */
  87. /* register contents up one slot */
  88. for (ret = 2; ret < 9; ret++)
  89. buf[ret - 1] = buf[ret];
  90. /* registers 0 to 6 (time values) are not touched */
  91. buf[8] = RATE_32768HZ; /* reg. 7 */
  92. buf[9] = 0; /* reg. 8 */
  93. buf[10] = 0; /* reg. 9 */
  94. buf[11] = 0; /* reg. 10 */
  95. buf[12] = 0; /* reg. 11 */
  96. buf[13] = 0; /* reg. 12 */
  97. buf[14] = 0; /* reg. 13 */
  98. buf[15] = 0; /* reg. 14 */
  99. buf[16] = USE_24HOUR_MODE; /* reg. 15 */
  100. ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, RS5C372_RAM_SIZE+1);
  101. if (ret != 0) {
  102. printf("%s: failed\n", __FUNCTION__);
  103. return;
  104. }
  105. setup_done = 1;
  106. return;
  107. }
  108. static void
  109. rs5c372_convert_to_time(struct rtc_time *dt, unsigned char *buf)
  110. {
  111. /* buf[0] is register 15 */
  112. dt->tm_sec = bcd2bin(buf[1]);
  113. dt->tm_min = bcd2bin(buf[2]);
  114. if (TWELVE_HOUR_MODE(buf[0])) {
  115. dt->tm_hour = HOURS_12(buf[3]);
  116. if (HOURS_AP(buf[3])) /* PM */
  117. dt->tm_hour += 12;
  118. } else /* 24-hour-mode */
  119. dt->tm_hour = HOURS_24(buf[3]);
  120. dt->tm_mday = bcd2bin(buf[5]);
  121. dt->tm_mon = bcd2bin(buf[6]);
  122. dt->tm_year = bcd2bin(buf[7]);
  123. if (dt->tm_year >= 70)
  124. dt->tm_year += 1900;
  125. else
  126. dt->tm_year += 2000;
  127. /* 0 is Sunday */
  128. dt->tm_wday = bcd2bin(buf[4] & 0x07);
  129. dt->tm_yday = 0;
  130. dt->tm_isdst= 0;
  131. if(rtc_debug > 2) {
  132. printf("rs5c372_convert_to_time: year = %d\n", dt->tm_year);
  133. printf("rs5c372_convert_to_time: mon = %d\n", dt->tm_mon);
  134. printf("rs5c372_convert_to_time: mday = %d\n", dt->tm_mday);
  135. printf("rs5c372_convert_to_time: hour = %d\n", dt->tm_hour);
  136. printf("rs5c372_convert_to_time: min = %d\n", dt->tm_min);
  137. printf("rs5c372_convert_to_time: sec = %d\n", dt->tm_sec);
  138. }
  139. }
  140. /*
  141. * Get the current time from the RTC
  142. */
  143. int
  144. rtc_get (struct rtc_time *tmp)
  145. {
  146. unsigned char buf[RS5C372_RAM_SIZE];
  147. int ret;
  148. if (!setup_done)
  149. rs5c372_enable();
  150. if (!setup_done)
  151. return -1;
  152. memset(buf, 0, sizeof(buf));
  153. /* note that this returns reg. 15 in buf[0] */
  154. ret = rs5c372_readram(buf, RS5C372_RAM_SIZE);
  155. if (ret != 0) {
  156. printf("%s: failed\n", __FUNCTION__);
  157. return -1;
  158. }
  159. rs5c372_convert_to_time(tmp, buf);
  160. return 0;
  161. }
  162. /*
  163. * Set the RTC
  164. */
  165. int rtc_set (struct rtc_time *tmp)
  166. {
  167. unsigned char buf[8], reg15;
  168. int ret;
  169. if (!setup_done)
  170. rs5c372_enable();
  171. if (!setup_done)
  172. return -1;
  173. if(rtc_debug > 2) {
  174. printf("rtc_set: tm_year = %d\n", tmp->tm_year);
  175. printf("rtc_set: tm_mon = %d\n", tmp->tm_mon);
  176. printf("rtc_set: tm_mday = %d\n", tmp->tm_mday);
  177. printf("rtc_set: tm_hour = %d\n", tmp->tm_hour);
  178. printf("rtc_set: tm_min = %d\n", tmp->tm_min);
  179. printf("rtc_set: tm_sec = %d\n", tmp->tm_sec);
  180. }
  181. memset(buf, 0, sizeof(buf));
  182. /* only read register 15 */
  183. ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 1);
  184. if (ret == 0) {
  185. /* need to save register 15 */
  186. reg15 = buf[0];
  187. buf[0] = 0; /* register address on RS5C372 */
  188. buf[1] = bin2bcd(tmp->tm_sec);
  189. buf[2] = bin2bcd(tmp->tm_min);
  190. /* need to handle 12 hour mode */
  191. if (TWELVE_HOUR_MODE(reg15)) {
  192. if (tmp->tm_hour >= 12) { /* PM */
  193. /* 12 PM is a special case */
  194. if (tmp->tm_hour == 12)
  195. buf[3] = bin2bcd(tmp->tm_hour);
  196. else
  197. buf[3] = bin2bcd(tmp->tm_hour - 12);
  198. buf[3] |= 0x20;
  199. }
  200. } else {
  201. buf[3] = bin2bcd(tmp->tm_hour);
  202. }
  203. buf[4] = bin2bcd(tmp->tm_wday);
  204. buf[5] = bin2bcd(tmp->tm_mday);
  205. buf[6] = bin2bcd(tmp->tm_mon);
  206. if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
  207. printf("WARNING: year should be between 1970 and 2069!\n");
  208. buf[7] = bin2bcd(tmp->tm_year % 100);
  209. ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 8);
  210. if (ret != 0) {
  211. printf("rs5c372_set_datetime(), i2c_master_send() returned %d\n",ret);
  212. return -1;
  213. }
  214. } else {
  215. return -1;
  216. }
  217. return 0;
  218. }
  219. /*
  220. * Reset the RTC. We set the date back to 1970-01-01.
  221. */
  222. void
  223. rtc_reset (void)
  224. {
  225. struct rtc_time tmp;
  226. if (!setup_done)
  227. rs5c372_enable();
  228. if (!setup_done)
  229. return;
  230. tmp.tm_year = 1970;
  231. tmp.tm_mon = 1;
  232. /* Jan. 1, 1970 was a Thursday */
  233. tmp.tm_wday= 4;
  234. tmp.tm_mday= 1;
  235. tmp.tm_hour = 0;
  236. tmp.tm_min = 0;
  237. tmp.tm_sec = 0;
  238. rtc_set(&tmp);
  239. printf ("RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  240. tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
  241. tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
  242. return;
  243. }
  244. #endif