ds12887.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_RTC_DS12887) && (CONFIG_COMMANDS & CFG_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. static unsigned bcd2bin (uchar n)
  66. {
  67. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  68. }
  69. static unsigned char bin2bcd (unsigned int n)
  70. {
  71. return (((n / 10) << 4) | (n % 10));
  72. }
  73. /* ------------------------------------------------------------------------- */
  74. void rtc_get (struct rtc_time *tmp)
  75. {
  76. uchar sec, min, hour, mday, wday, mon, year;
  77. /* check if rtc is available for access */
  78. while( rtc_read(RTC_CONTROL_A) & RTC_CA_UIP)
  79. ;
  80. sec = rtc_read(RTC_SECONDS);
  81. min = rtc_read(RTC_MINUTES);
  82. hour = rtc_read(RTC_HOURS);
  83. mday = rtc_read(RTC_DATE_OF_MONTH);
  84. wday = rtc_read(RTC_DAY_OF_WEEK);
  85. mon = rtc_read(RTC_MONTH);
  86. year = rtc_read(RTC_YEAR);
  87. #ifdef RTC_DEBUG
  88. printf( "Get RTC year: %d; mon: %d; mday: %d; wday: %d; "
  89. "hr: %d; min: %d; sec: %d\n",
  90. year, mon, mday, wday, hour, min, sec );
  91. printf ( "Alarms: hour: %02x min: %02x sec: %02x\n",
  92. rtc_read (RTC_HOURS_ALARM),
  93. rtc_read (RTC_MINUTES_ALARM),
  94. rtc_read (RTC_SECONDS_ALARM) );
  95. #endif
  96. if( !(rtc_read(RTC_CONTROL_B) & RTC_CB_DM))
  97. { /* Information is in BCD format */
  98. printf(" Get: Convert BSD to BIN\n");
  99. tmp->tm_sec = bcd2bin (sec & 0x7F);
  100. tmp->tm_min = bcd2bin (min & 0x7F);
  101. tmp->tm_hour = bcd2bin (hour & 0x3F);
  102. tmp->tm_mday = bcd2bin (mday & 0x3F);
  103. tmp->tm_mon = bcd2bin (mon & 0x1F);
  104. tmp->tm_year = bcd2bin (year);
  105. tmp->tm_wday = bcd2bin (wday & 0x07);
  106. }
  107. else
  108. {
  109. tmp->tm_sec = sec & 0x7F;
  110. tmp->tm_min = min & 0x7F;
  111. tmp->tm_hour = hour & 0x3F;
  112. tmp->tm_mday = mday & 0x3F;
  113. tmp->tm_mon = mon & 0x1F;
  114. tmp->tm_year = year;
  115. tmp->tm_wday = wday & 0x07;
  116. }
  117. if(tmp->tm_year<70)
  118. tmp->tm_year+=2000;
  119. else
  120. tmp->tm_year+=1900;
  121. tmp->tm_yday = 0;
  122. tmp->tm_isdst= 0;
  123. #ifdef RTC_DEBUG
  124. printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  125. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  126. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  127. #endif
  128. }
  129. void rtc_set (struct rtc_time *tmp)
  130. {
  131. uchar save_ctrl_b;
  132. uchar sec, min, hour, mday, wday, mon, year;
  133. #ifdef RTC_DEBUG
  134. printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  135. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  136. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  137. #endif
  138. if( !(rtc_read(RTC_CONTROL_B) & RTC_CB_DM))
  139. { /* Information is in BCD format */
  140. year = bin2bcd(tmp->tm_year % 100);
  141. mon = bin2bcd(tmp->tm_mon);
  142. wday = bin2bcd(tmp->tm_wday);
  143. mday = bin2bcd(tmp->tm_mday);
  144. hour = bin2bcd(tmp->tm_hour);
  145. min = bin2bcd(tmp->tm_min);
  146. sec = bin2bcd(tmp->tm_sec);
  147. }
  148. else
  149. {
  150. year = tmp->tm_year % 100;
  151. mon = tmp->tm_mon;
  152. wday = tmp->tm_wday;
  153. mday = tmp->tm_mday;
  154. hour = tmp->tm_hour;
  155. min = tmp->tm_min;
  156. sec = tmp->tm_sec;
  157. }
  158. /* disables the RTC to update the regs */
  159. save_ctrl_b = rtc_read(RTC_CONTROL_B);
  160. save_ctrl_b |= RTC_CB_SET;
  161. rtc_write(RTC_CONTROL_B, save_ctrl_b);
  162. rtc_write (RTC_YEAR, year);
  163. rtc_write (RTC_MONTH, mon);
  164. rtc_write (RTC_DAY_OF_WEEK, wday);
  165. rtc_write (RTC_DATE_OF_MONTH, mday);
  166. rtc_write (RTC_HOURS, hour);
  167. rtc_write (RTC_MINUTES, min);
  168. rtc_write (RTC_SECONDS, sec);
  169. /* enables the RTC to update the regs */
  170. save_ctrl_b &= ~RTC_CB_SET;
  171. rtc_write(RTC_CONTROL_B, save_ctrl_b);
  172. }
  173. void rtc_reset (void)
  174. {
  175. struct rtc_time tmp;
  176. uchar ctrl_rg;
  177. ctrl_rg = RTC_CB_SET;
  178. rtc_write(RTC_CONTROL_B,ctrl_rg);
  179. tmp.tm_year = 1970 % 100;
  180. tmp.tm_mon = 1;
  181. tmp.tm_mday= 1;
  182. tmp.tm_hour = 0;
  183. tmp.tm_min = 0;
  184. tmp.tm_sec = 0;
  185. #ifdef RTC_DEBUG
  186. printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  187. tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
  188. tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
  189. #endif
  190. ctrl_rg = RTC_CB_SET | RTC_CB_24_12 | RTC_CB_DM;
  191. rtc_write(RTC_CONTROL_B,ctrl_rg);
  192. rtc_set(&tmp);
  193. rtc_write(RTC_HOURS_ALARM, 0),
  194. rtc_write(RTC_MINUTES_ALARM, 0),
  195. rtc_write(RTC_SECONDS_ALARM, 0);
  196. ctrl_rg = RTC_CB_24_12 | RTC_CB_DM;
  197. rtc_write(RTC_CONTROL_B,ctrl_rg);
  198. }
  199. #endif /* (CONFIG_RTC_DS12887) && (CONFIG_COMMANDS & CFG_CMD_DATE) */