mk48t59.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_AMIGAONEG3SE)
  56. #include "../board/MAI/AmigaOneG3SE/via686.h"
  57. #include "../board/MAI/AmigaOneG3SE/memio.h"
  58. static uchar rtc_read (short reg)
  59. {
  60. out_byte(CMOS_ADDR, (uint8)reg);
  61. return in_byte(CMOS_DATA);
  62. }
  63. static void rtc_write (short reg, uchar val)
  64. {
  65. out_byte(CMOS_ADDR, (uint8)reg);
  66. out_byte(CMOS_DATA, (uint8)val);
  67. }
  68. #elif defined(CONFIG_EVAL5200)
  69. static uchar rtc_read (short reg)
  70. {
  71. return in8(RTC(reg));
  72. }
  73. static void rtc_write (short reg, uchar val)
  74. {
  75. out8(RTC(reg),val);
  76. }
  77. #else
  78. # error Board specific rtc access functions should be supplied
  79. #endif
  80. static unsigned bcd2bin (uchar n)
  81. {
  82. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  83. }
  84. static unsigned char bin2bcd (unsigned int n)
  85. {
  86. return (((n / 10) << 4) | (n % 10));
  87. }
  88. /* ------------------------------------------------------------------------- */
  89. void *nvram_read(void *dest, const short src, size_t count)
  90. {
  91. uchar *d = (uchar *) dest;
  92. short s = src;
  93. while (count--)
  94. *d++ = rtc_read(s++);
  95. return dest;
  96. }
  97. void nvram_write(short dest, const void *src, size_t count)
  98. {
  99. short d = dest;
  100. uchar *s = (uchar *) src;
  101. while (count--)
  102. rtc_write(d++, *s++);
  103. }
  104. #if defined(CONFIG_CMD_DATE)
  105. /* ------------------------------------------------------------------------- */
  106. int rtc_get (struct rtc_time *tmp)
  107. {
  108. uchar save_ctrl_a;
  109. uchar sec, min, hour, mday, wday, mon, year;
  110. /* Simple: freeze the clock, read it and allow updates again */
  111. save_ctrl_a = rtc_read(RTC_CONTROLA);
  112. /* Set the register to read the value. */
  113. save_ctrl_a |= RTC_CA_READ;
  114. rtc_write(RTC_CONTROLA, save_ctrl_a);
  115. sec = rtc_read (RTC_SECONDS);
  116. min = rtc_read (RTC_MINUTES);
  117. hour = rtc_read (RTC_HOURS);
  118. mday = rtc_read (RTC_DAY_OF_MONTH);
  119. wday = rtc_read (RTC_DAY_OF_WEEK);
  120. mon = rtc_read (RTC_MONTH);
  121. year = rtc_read (RTC_YEAR);
  122. /* re-enable update */
  123. save_ctrl_a &= ~RTC_CA_READ;
  124. rtc_write(RTC_CONTROLA, save_ctrl_a);
  125. #ifdef RTC_DEBUG
  126. printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  127. "hr: %02x min: %02x sec: %02x\n",
  128. year, mon, mday, wday,
  129. hour, min, sec );
  130. #endif
  131. tmp->tm_sec = bcd2bin (sec & 0x7F);
  132. tmp->tm_min = bcd2bin (min & 0x7F);
  133. tmp->tm_hour = bcd2bin (hour & 0x3F);
  134. tmp->tm_mday = bcd2bin (mday & 0x3F);
  135. tmp->tm_mon = bcd2bin (mon & 0x1F);
  136. tmp->tm_year = bcd2bin (year);
  137. tmp->tm_wday = bcd2bin (wday & 0x07);
  138. if(tmp->tm_year<70)
  139. tmp->tm_year+=2000;
  140. else
  141. tmp->tm_year+=1900;
  142. tmp->tm_yday = 0;
  143. tmp->tm_isdst= 0;
  144. #ifdef RTC_DEBUG
  145. printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  146. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  147. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  148. #endif
  149. return 0;
  150. }
  151. int rtc_set (struct rtc_time *tmp)
  152. {
  153. uchar save_ctrl_a;
  154. #ifdef RTC_DEBUG
  155. printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  156. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  157. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  158. #endif
  159. save_ctrl_a = rtc_read(RTC_CONTROLA);
  160. save_ctrl_a |= RTC_CA_WRITE;
  161. rtc_write(RTC_CONTROLA, save_ctrl_a); /* disables the RTC to update the regs */
  162. rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
  163. rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
  164. rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
  165. rtc_write (RTC_DAY_OF_MONTH, bin2bcd(tmp->tm_mday));
  166. rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
  167. rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
  168. rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
  169. save_ctrl_a &= ~RTC_CA_WRITE;
  170. rtc_write(RTC_CONTROLA, save_ctrl_a); /* enables the RTC to update the regs */
  171. return 0;
  172. }
  173. void rtc_reset (void)
  174. {
  175. uchar control_b;
  176. /*
  177. * Start oscillator here.
  178. */
  179. control_b = rtc_read(RTC_CONTROLB);
  180. control_b &= ~RTC_CB_STOP;
  181. rtc_write(RTC_CONTROLB, control_b);
  182. }
  183. void rtc_set_watchdog(short multi, short res)
  184. {
  185. uchar wd_value;
  186. wd_value = RTC_WDS | ((multi & 0x1F) << 2) | (res & 0x3);
  187. rtc_write(RTC_WATCHDOG, wd_value);
  188. }
  189. #endif