mk48t59.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  3. * Andreas Heppel <aheppel@sysgo.de>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Date & Time support for the MK48T59 RTC
  25. */
  26. #undef RTC_DEBUG
  27. #include <common.h>
  28. #include <command.h>
  29. #include <config.h>
  30. #include <rtc.h>
  31. #include <mk48t59.h>
  32. #if defined(CONFIG_BAB7xx)
  33. static uchar rtc_read (short reg)
  34. {
  35. out8(RTC_PORT_ADDR0, reg & 0xFF);
  36. out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
  37. return in8(RTC_PORT_DATA);
  38. }
  39. static void rtc_write (short reg, uchar val)
  40. {
  41. out8(RTC_PORT_ADDR0, reg & 0xFF);
  42. out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
  43. out8(RTC_PORT_DATA, val);
  44. }
  45. #elif defined(CONFIG_PCIPPC2)
  46. #include "../board/pcippc2/pcippc2.h"
  47. static uchar rtc_read (short reg)
  48. {
  49. return in8(RTC(reg));
  50. }
  51. static void rtc_write (short reg, uchar val)
  52. {
  53. out8(RTC(reg),val);
  54. }
  55. #elif defined(CONFIG_EVAL5200)
  56. static uchar rtc_read (short reg)
  57. {
  58. return in8(RTC(reg));
  59. }
  60. static void rtc_write (short reg, uchar val)
  61. {
  62. out8(RTC(reg),val);
  63. }
  64. #else
  65. # error Board specific rtc access functions should be supplied
  66. #endif
  67. /* ------------------------------------------------------------------------- */
  68. void *nvram_read(void *dest, const short src, size_t count)
  69. {
  70. uchar *d = (uchar *) dest;
  71. short s = src;
  72. while (count--)
  73. *d++ = rtc_read(s++);
  74. return dest;
  75. }
  76. void nvram_write(short dest, const void *src, size_t count)
  77. {
  78. short d = dest;
  79. uchar *s = (uchar *) src;
  80. while (count--)
  81. rtc_write(d++, *s++);
  82. }
  83. #if defined(CONFIG_CMD_DATE)
  84. /* ------------------------------------------------------------------------- */
  85. int rtc_get (struct rtc_time *tmp)
  86. {
  87. uchar save_ctrl_a;
  88. uchar sec, min, hour, mday, wday, mon, year;
  89. /* Simple: freeze the clock, read it and allow updates again */
  90. save_ctrl_a = rtc_read(RTC_CONTROLA);
  91. /* Set the register to read the value. */
  92. save_ctrl_a |= RTC_CA_READ;
  93. rtc_write(RTC_CONTROLA, save_ctrl_a);
  94. sec = rtc_read (RTC_SECONDS);
  95. min = rtc_read (RTC_MINUTES);
  96. hour = rtc_read (RTC_HOURS);
  97. mday = rtc_read (RTC_DAY_OF_MONTH);
  98. wday = rtc_read (RTC_DAY_OF_WEEK);
  99. mon = rtc_read (RTC_MONTH);
  100. year = rtc_read (RTC_YEAR);
  101. /* re-enable update */
  102. save_ctrl_a &= ~RTC_CA_READ;
  103. rtc_write(RTC_CONTROLA, save_ctrl_a);
  104. #ifdef RTC_DEBUG
  105. printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  106. "hr: %02x min: %02x sec: %02x\n",
  107. year, mon, mday, wday,
  108. hour, min, sec );
  109. #endif
  110. tmp->tm_sec = bcd2bin (sec & 0x7F);
  111. tmp->tm_min = bcd2bin (min & 0x7F);
  112. tmp->tm_hour = bcd2bin (hour & 0x3F);
  113. tmp->tm_mday = bcd2bin (mday & 0x3F);
  114. tmp->tm_mon = bcd2bin (mon & 0x1F);
  115. tmp->tm_year = bcd2bin (year);
  116. tmp->tm_wday = bcd2bin (wday & 0x07);
  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. return 0;
  129. }
  130. int rtc_set (struct rtc_time *tmp)
  131. {
  132. uchar save_ctrl_a;
  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. save_ctrl_a = rtc_read(RTC_CONTROLA);
  139. save_ctrl_a |= RTC_CA_WRITE;
  140. rtc_write(RTC_CONTROLA, save_ctrl_a); /* disables the RTC to update the regs */
  141. rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
  142. rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
  143. rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
  144. rtc_write (RTC_DAY_OF_MONTH, bin2bcd(tmp->tm_mday));
  145. rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
  146. rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
  147. rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
  148. save_ctrl_a &= ~RTC_CA_WRITE;
  149. rtc_write(RTC_CONTROLA, save_ctrl_a); /* enables the RTC to update the regs */
  150. return 0;
  151. }
  152. void rtc_reset (void)
  153. {
  154. uchar control_b;
  155. /*
  156. * Start oscillator here.
  157. */
  158. control_b = rtc_read(RTC_CONTROLB);
  159. control_b &= ~RTC_CB_STOP;
  160. rtc_write(RTC_CONTROLB, control_b);
  161. }
  162. void rtc_set_watchdog(short multi, short res)
  163. {
  164. uchar wd_value;
  165. wd_value = RTC_WDS | ((multi & 0x1F) << 2) | (res & 0x3);
  166. rtc_write(RTC_WATCHDOG, wd_value);
  167. }
  168. #endif