mk48t59.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_RTC_MK48T59)
  33. #if defined(CONFIG_BAB7xx)
  34. static uchar rtc_read (short reg)
  35. {
  36. out8(RTC_PORT_ADDR0, reg & 0xFF);
  37. out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
  38. return in8(RTC_PORT_DATA);
  39. }
  40. static void rtc_write (short reg, uchar val)
  41. {
  42. out8(RTC_PORT_ADDR0, reg & 0xFF);
  43. out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
  44. out8(RTC_PORT_DATA, val);
  45. }
  46. #elif defined(CONFIG_PCIPPC2)
  47. #include "../board/pcippc2/pcippc2.h"
  48. static uchar rtc_read (short reg)
  49. {
  50. return in8(RTC(reg));
  51. }
  52. static void rtc_write (short reg, uchar val)
  53. {
  54. out8(RTC(reg),val);
  55. }
  56. #else
  57. # error Board specific rtc access functions should be supplied
  58. #endif
  59. static unsigned bcd2bin (uchar n)
  60. {
  61. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  62. }
  63. static unsigned char bin2bcd (unsigned int n)
  64. {
  65. return (((n / 10) << 4) | (n % 10));
  66. }
  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 (CONFIG_COMMANDS & CFG_CMD_DATE)
  84. /* ------------------------------------------------------------------------- */
  85. void 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. }
  129. void rtc_set (struct rtc_time *tmp)
  130. {
  131. uchar save_ctrl_a;
  132. #ifdef RTC_DEBUG
  133. printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  134. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  135. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  136. #endif
  137. save_ctrl_a = rtc_read(RTC_CONTROLA);
  138. save_ctrl_a |= RTC_CA_WRITE;
  139. rtc_write(RTC_CONTROLA, save_ctrl_a); /* disables the RTC to update the regs */
  140. rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
  141. rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
  142. rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
  143. rtc_write (RTC_DAY_OF_MONTH, bin2bcd(tmp->tm_mday));
  144. rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
  145. rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
  146. rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
  147. save_ctrl_a &= ~RTC_CA_WRITE;
  148. rtc_write(RTC_CONTROLA, save_ctrl_a); /* enables the RTC to update the regs */
  149. }
  150. void rtc_reset (void)
  151. {
  152. uchar control_b;
  153. /*
  154. * Start oscillator here.
  155. */
  156. control_b = rtc_read(RTC_CONTROLB);
  157. control_b &= ~RTC_CB_STOP;
  158. rtc_write(RTC_CONTROLB, control_b);
  159. }
  160. void rtc_set_watchdog(short multi, short res)
  161. {
  162. uchar wd_value;
  163. wd_value = RTC_WDS | ((multi & 0x1F) << 2) | (res & 0x3);
  164. rtc_write(RTC_WATCHDOG, wd_value);
  165. }
  166. #endif /* (CONFIG_COMMANDS & CFG_CMD_DATE) */
  167. #endif /* CONFIG_RTC_MK48T59 */