rs5c372.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #include <common.h>
  22. #include <command.h>
  23. #include <rtc.h>
  24. #include <i2c.h>
  25. #if defined(CONFIG_CMD_DATE)
  26. /*
  27. * Reads are always done starting with register 15, which requires some
  28. * jumping-through-hoops to access the data correctly.
  29. *
  30. * Writes are always done starting with register 0.
  31. */
  32. #define DEBUG 0
  33. #if DEBUG
  34. static unsigned int rtc_debug = DEBUG;
  35. #else
  36. #define rtc_debug 0 /* gcc will remove all the debug code for us */
  37. #endif
  38. #ifndef CONFIG_SYS_I2C_RTC_ADDR
  39. #define CONFIG_SYS_I2C_RTC_ADDR 0x32
  40. #endif
  41. #define RS5C372_RAM_SIZE 0x10
  42. #define RATE_32000HZ 0x80 /* Rate Select 32.000KHz */
  43. #define RATE_32768HZ 0x00 /* Rate Select 32.768KHz */
  44. #define STATUS_XPT 0x10 /* data invalid because voltage was 0 */
  45. #define USE_24HOUR_MODE 0x20
  46. #define TWELVE_HOUR_MODE(n) ((((n) >> 5) & 1) == 0)
  47. #define HOURS_AP(n) (((n) >> 5) & 1)
  48. #define HOURS_12(n) bcd2bin((n) & 0x1F)
  49. #define HOURS_24(n) bcd2bin((n) & 0x3F)
  50. static int setup_done = 0;
  51. static int
  52. rs5c372_readram(unsigned char *buf, int len)
  53. {
  54. int ret;
  55. ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, len);
  56. if (ret != 0) {
  57. printf("%s: failed to read\n", __FUNCTION__);
  58. return ret;
  59. }
  60. if (buf[0] & STATUS_XPT)
  61. printf("### Warning: RTC lost power\n");
  62. return ret;
  63. }
  64. static void
  65. rs5c372_enable(void)
  66. {
  67. unsigned char buf[RS5C372_RAM_SIZE + 1];
  68. int ret;
  69. /* note that this returns reg. 15 in buf[1] */
  70. ret = rs5c372_readram(&buf[1], RS5C372_RAM_SIZE);
  71. if (ret != 0) {
  72. printf("%s: failed\n", __FUNCTION__);
  73. return;
  74. }
  75. buf[0] = 0;
  76. /* we want to start writing at register 0 so we have to copy the */
  77. /* register contents up one slot */
  78. for (ret = 2; ret < 9; ret++)
  79. buf[ret - 1] = buf[ret];
  80. /* registers 0 to 6 (time values) are not touched */
  81. buf[8] = RATE_32768HZ; /* reg. 7 */
  82. buf[9] = 0; /* reg. 8 */
  83. buf[10] = 0; /* reg. 9 */
  84. buf[11] = 0; /* reg. 10 */
  85. buf[12] = 0; /* reg. 11 */
  86. buf[13] = 0; /* reg. 12 */
  87. buf[14] = 0; /* reg. 13 */
  88. buf[15] = 0; /* reg. 14 */
  89. buf[16] = USE_24HOUR_MODE; /* reg. 15 */
  90. ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, RS5C372_RAM_SIZE+1);
  91. if (ret != 0) {
  92. printf("%s: failed\n", __FUNCTION__);
  93. return;
  94. }
  95. setup_done = 1;
  96. return;
  97. }
  98. static void
  99. rs5c372_convert_to_time(struct rtc_time *dt, unsigned char *buf)
  100. {
  101. /* buf[0] is register 15 */
  102. dt->tm_sec = bcd2bin(buf[1]);
  103. dt->tm_min = bcd2bin(buf[2]);
  104. if (TWELVE_HOUR_MODE(buf[0])) {
  105. dt->tm_hour = HOURS_12(buf[3]);
  106. if (HOURS_AP(buf[3])) /* PM */
  107. dt->tm_hour += 12;
  108. } else /* 24-hour-mode */
  109. dt->tm_hour = HOURS_24(buf[3]);
  110. dt->tm_mday = bcd2bin(buf[5]);
  111. dt->tm_mon = bcd2bin(buf[6]);
  112. dt->tm_year = bcd2bin(buf[7]);
  113. if (dt->tm_year >= 70)
  114. dt->tm_year += 1900;
  115. else
  116. dt->tm_year += 2000;
  117. /* 0 is Sunday */
  118. dt->tm_wday = bcd2bin(buf[4] & 0x07);
  119. dt->tm_yday = 0;
  120. dt->tm_isdst= 0;
  121. if(rtc_debug > 2) {
  122. printf("rs5c372_convert_to_time: year = %d\n", dt->tm_year);
  123. printf("rs5c372_convert_to_time: mon = %d\n", dt->tm_mon);
  124. printf("rs5c372_convert_to_time: mday = %d\n", dt->tm_mday);
  125. printf("rs5c372_convert_to_time: hour = %d\n", dt->tm_hour);
  126. printf("rs5c372_convert_to_time: min = %d\n", dt->tm_min);
  127. printf("rs5c372_convert_to_time: sec = %d\n", dt->tm_sec);
  128. }
  129. }
  130. /*
  131. * Get the current time from the RTC
  132. */
  133. int
  134. rtc_get (struct rtc_time *tmp)
  135. {
  136. unsigned char buf[RS5C372_RAM_SIZE];
  137. int ret;
  138. if (!setup_done)
  139. rs5c372_enable();
  140. if (!setup_done)
  141. return -1;
  142. memset(buf, 0, sizeof(buf));
  143. /* note that this returns reg. 15 in buf[0] */
  144. ret = rs5c372_readram(buf, RS5C372_RAM_SIZE);
  145. if (ret != 0) {
  146. printf("%s: failed\n", __FUNCTION__);
  147. return -1;
  148. }
  149. rs5c372_convert_to_time(tmp, buf);
  150. return 0;
  151. }
  152. /*
  153. * Set the RTC
  154. */
  155. int rtc_set (struct rtc_time *tmp)
  156. {
  157. unsigned char buf[8], reg15;
  158. int ret;
  159. if (!setup_done)
  160. rs5c372_enable();
  161. if (!setup_done)
  162. return -1;
  163. if(rtc_debug > 2) {
  164. printf("rtc_set: tm_year = %d\n", tmp->tm_year);
  165. printf("rtc_set: tm_mon = %d\n", tmp->tm_mon);
  166. printf("rtc_set: tm_mday = %d\n", tmp->tm_mday);
  167. printf("rtc_set: tm_hour = %d\n", tmp->tm_hour);
  168. printf("rtc_set: tm_min = %d\n", tmp->tm_min);
  169. printf("rtc_set: tm_sec = %d\n", tmp->tm_sec);
  170. }
  171. memset(buf, 0, sizeof(buf));
  172. /* only read register 15 */
  173. ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 1);
  174. if (ret == 0) {
  175. /* need to save register 15 */
  176. reg15 = buf[0];
  177. buf[0] = 0; /* register address on RS5C372 */
  178. buf[1] = bin2bcd(tmp->tm_sec);
  179. buf[2] = bin2bcd(tmp->tm_min);
  180. /* need to handle 12 hour mode */
  181. if (TWELVE_HOUR_MODE(reg15)) {
  182. if (tmp->tm_hour >= 12) { /* PM */
  183. /* 12 PM is a special case */
  184. if (tmp->tm_hour == 12)
  185. buf[3] = bin2bcd(tmp->tm_hour);
  186. else
  187. buf[3] = bin2bcd(tmp->tm_hour - 12);
  188. buf[3] |= 0x20;
  189. }
  190. } else {
  191. buf[3] = bin2bcd(tmp->tm_hour);
  192. }
  193. buf[4] = bin2bcd(tmp->tm_wday);
  194. buf[5] = bin2bcd(tmp->tm_mday);
  195. buf[6] = bin2bcd(tmp->tm_mon);
  196. if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
  197. printf("WARNING: year should be between 1970 and 2069!\n");
  198. buf[7] = bin2bcd(tmp->tm_year % 100);
  199. ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 8);
  200. if (ret != 0) {
  201. printf("rs5c372_set_datetime(), i2c_master_send() returned %d\n",ret);
  202. return -1;
  203. }
  204. } else {
  205. return -1;
  206. }
  207. return 0;
  208. }
  209. /*
  210. * Reset the RTC. We set the date back to 1970-01-01.
  211. */
  212. void
  213. rtc_reset (void)
  214. {
  215. struct rtc_time tmp;
  216. if (!setup_done)
  217. rs5c372_enable();
  218. if (!setup_done)
  219. return;
  220. tmp.tm_year = 1970;
  221. tmp.tm_mon = 1;
  222. /* Jan. 1, 1970 was a Thursday */
  223. tmp.tm_wday= 4;
  224. tmp.tm_mday= 1;
  225. tmp.tm_hour = 0;
  226. tmp.tm_min = 0;
  227. tmp.tm_sec = 0;
  228. rtc_set(&tmp);
  229. printf ("RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  230. tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
  231. tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
  232. return;
  233. }
  234. #endif