ds12887.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * (C) Copyright 2003
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  17. * MA 02111-1307 USA
  18. */
  19. /*
  20. * Date & Time support for the DS12887 RTC
  21. */
  22. #undef RTC_DEBUG
  23. #include <common.h>
  24. #include <command.h>
  25. #include <config.h>
  26. #include <rtc.h>
  27. #if defined(CONFIG_CMD_DATE)
  28. #define RTC_SECONDS 0x00
  29. #define RTC_SECONDS_ALARM 0x01
  30. #define RTC_MINUTES 0x02
  31. #define RTC_MINUTES_ALARM 0x03
  32. #define RTC_HOURS 0x04
  33. #define RTC_HOURS_ALARM 0x05
  34. #define RTC_DAY_OF_WEEK 0x06
  35. #define RTC_DATE_OF_MONTH 0x07
  36. #define RTC_MONTH 0x08
  37. #define RTC_YEAR 0x09
  38. #define RTC_CONTROL_A 0x0A
  39. #define RTC_CONTROL_B 0x0B
  40. #define RTC_CONTROL_C 0x0C
  41. #define RTC_CONTROL_D 0x0D
  42. #define RTC_CA_UIP 0x80
  43. #define RTC_CB_DM 0x04
  44. #define RTC_CB_24_12 0x02
  45. #define RTC_CB_SET 0x80
  46. #if defined(CONFIG_ATC)
  47. static uchar rtc_read (uchar reg)
  48. {
  49. uchar val;
  50. *(volatile unsigned char*)(RTC_PORT_ADDR) = reg;
  51. __asm__ __volatile__ ("sync");
  52. val = *(volatile unsigned char*)(RTC_PORT_DATA);
  53. return (val);
  54. }
  55. static void rtc_write (uchar reg, uchar val)
  56. {
  57. *(volatile unsigned char*)(RTC_PORT_ADDR) = reg;
  58. __asm__ __volatile__ ("sync");
  59. *(volatile unsigned char*)(RTC_PORT_DATA) = val;
  60. __asm__ __volatile__ ("sync");
  61. }
  62. #else
  63. # error Board specific rtc access functions should be supplied
  64. #endif
  65. int rtc_get (struct rtc_time *tmp)
  66. {
  67. uchar sec, min, hour, mday, wday, mon, year;
  68. /* check if rtc is available for access */
  69. while( rtc_read(RTC_CONTROL_A) & RTC_CA_UIP)
  70. ;
  71. sec = rtc_read(RTC_SECONDS);
  72. min = rtc_read(RTC_MINUTES);
  73. hour = rtc_read(RTC_HOURS);
  74. mday = rtc_read(RTC_DATE_OF_MONTH);
  75. wday = rtc_read(RTC_DAY_OF_WEEK);
  76. mon = rtc_read(RTC_MONTH);
  77. year = rtc_read(RTC_YEAR);
  78. #ifdef RTC_DEBUG
  79. printf( "Get RTC year: %d; mon: %d; mday: %d; wday: %d; "
  80. "hr: %d; min: %d; sec: %d\n",
  81. year, mon, mday, wday, hour, min, sec );
  82. printf ( "Alarms: hour: %02x min: %02x sec: %02x\n",
  83. rtc_read (RTC_HOURS_ALARM),
  84. rtc_read (RTC_MINUTES_ALARM),
  85. rtc_read (RTC_SECONDS_ALARM) );
  86. #endif
  87. if( !(rtc_read(RTC_CONTROL_B) & RTC_CB_DM))
  88. { /* Information is in BCD format */
  89. printf(" Get: Convert BSD to BIN\n");
  90. tmp->tm_sec = bcd2bin (sec & 0x7F);
  91. tmp->tm_min = bcd2bin (min & 0x7F);
  92. tmp->tm_hour = bcd2bin (hour & 0x3F);
  93. tmp->tm_mday = bcd2bin (mday & 0x3F);
  94. tmp->tm_mon = bcd2bin (mon & 0x1F);
  95. tmp->tm_year = bcd2bin (year);
  96. tmp->tm_wday = bcd2bin (wday & 0x07);
  97. }
  98. else
  99. {
  100. tmp->tm_sec = sec & 0x7F;
  101. tmp->tm_min = min & 0x7F;
  102. tmp->tm_hour = hour & 0x3F;
  103. tmp->tm_mday = mday & 0x3F;
  104. tmp->tm_mon = mon & 0x1F;
  105. tmp->tm_year = year;
  106. tmp->tm_wday = wday & 0x07;
  107. }
  108. if(tmp->tm_year<70)
  109. tmp->tm_year+=2000;
  110. else
  111. tmp->tm_year+=1900;
  112. tmp->tm_yday = 0;
  113. tmp->tm_isdst= 0;
  114. #ifdef RTC_DEBUG
  115. printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  116. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  117. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  118. #endif
  119. return 0;
  120. }
  121. int rtc_set (struct rtc_time *tmp)
  122. {
  123. uchar save_ctrl_b;
  124. uchar sec, min, hour, mday, wday, mon, year;
  125. #ifdef RTC_DEBUG
  126. printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  127. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  128. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  129. #endif
  130. if( !(rtc_read(RTC_CONTROL_B) & RTC_CB_DM))
  131. { /* Information is in BCD format */
  132. year = bin2bcd(tmp->tm_year % 100);
  133. mon = bin2bcd(tmp->tm_mon);
  134. wday = bin2bcd(tmp->tm_wday);
  135. mday = bin2bcd(tmp->tm_mday);
  136. hour = bin2bcd(tmp->tm_hour);
  137. min = bin2bcd(tmp->tm_min);
  138. sec = bin2bcd(tmp->tm_sec);
  139. }
  140. else
  141. {
  142. year = tmp->tm_year % 100;
  143. mon = tmp->tm_mon;
  144. wday = tmp->tm_wday;
  145. mday = tmp->tm_mday;
  146. hour = tmp->tm_hour;
  147. min = tmp->tm_min;
  148. sec = tmp->tm_sec;
  149. }
  150. /* disables the RTC to update the regs */
  151. save_ctrl_b = rtc_read(RTC_CONTROL_B);
  152. save_ctrl_b |= RTC_CB_SET;
  153. rtc_write(RTC_CONTROL_B, save_ctrl_b);
  154. rtc_write (RTC_YEAR, year);
  155. rtc_write (RTC_MONTH, mon);
  156. rtc_write (RTC_DAY_OF_WEEK, wday);
  157. rtc_write (RTC_DATE_OF_MONTH, mday);
  158. rtc_write (RTC_HOURS, hour);
  159. rtc_write (RTC_MINUTES, min);
  160. rtc_write (RTC_SECONDS, sec);
  161. /* enables the RTC to update the regs */
  162. save_ctrl_b &= ~RTC_CB_SET;
  163. rtc_write(RTC_CONTROL_B, save_ctrl_b);
  164. return 0;
  165. }
  166. void rtc_reset (void)
  167. {
  168. struct rtc_time tmp;
  169. uchar ctrl_rg;
  170. ctrl_rg = RTC_CB_SET;
  171. rtc_write(RTC_CONTROL_B,ctrl_rg);
  172. tmp.tm_year = 1970 % 100;
  173. tmp.tm_mon = 1;
  174. tmp.tm_mday= 1;
  175. tmp.tm_hour = 0;
  176. tmp.tm_min = 0;
  177. tmp.tm_sec = 0;
  178. #ifdef RTC_DEBUG
  179. printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  180. tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
  181. tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
  182. #endif
  183. ctrl_rg = RTC_CB_SET | RTC_CB_24_12 | RTC_CB_DM;
  184. rtc_write(RTC_CONTROL_B,ctrl_rg);
  185. rtc_set(&tmp);
  186. rtc_write(RTC_HOURS_ALARM, 0),
  187. rtc_write(RTC_MINUTES_ALARM, 0),
  188. rtc_write(RTC_SECONDS_ALARM, 0);
  189. ctrl_rg = RTC_CB_24_12 | RTC_CB_DM;
  190. rtc_write(RTC_CONTROL_B,ctrl_rg);
  191. }
  192. #endif