rs5c372.c 5.8 KB

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