mk48t59.c 5.3 KB

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