mc146818.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter MPL AG Switzerland. d.peter@mpl.ch
  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 MC146818 (PIXX4) RTC
  25. */
  26. /*#define DEBUG*/
  27. #include <common.h>
  28. #include <command.h>
  29. #include <rtc.h>
  30. #if defined(CONFIG_RTC_MC146818) && (CONFIG_COMMANDS & CFG_CMD_DATE)
  31. static uchar rtc_read (uchar reg);
  32. static void rtc_write (uchar reg, uchar val);
  33. static uchar bin2bcd (unsigned int n);
  34. static unsigned bcd2bin(uchar c);
  35. #define RTC_PORT_MC146818 CFG_ISA_IO_BASE_ADDRESS + 0x70
  36. #define RTC_SECONDS 0x00
  37. #define RTC_SECONDS_ALARM 0x01
  38. #define RTC_MINUTES 0x02
  39. #define RTC_MINUTES_ALARM 0x03
  40. #define RTC_HOURS 0x04
  41. #define RTC_HOURS_ALARM 0x05
  42. #define RTC_DAY_OF_WEEK 0x06
  43. #define RTC_DATE_OF_MONTH 0x07
  44. #define RTC_MONTH 0x08
  45. #define RTC_YEAR 0x09
  46. #define RTC_CONFIG_A 0x0A
  47. #define RTC_CONFIG_B 0x0B
  48. #define RTC_CONFIG_C 0x0C
  49. #define RTC_CONFIG_D 0x0D
  50. /* ------------------------------------------------------------------------- */
  51. void rtc_get (struct rtc_time *tmp)
  52. {
  53. uchar sec, min, hour, mday, wday, mon, year;
  54. /* here check if rtc can be accessed */
  55. while((rtc_read(RTC_CONFIG_A)&0x80)==0x80);
  56. sec = rtc_read (RTC_SECONDS);
  57. min = rtc_read (RTC_MINUTES);
  58. hour = rtc_read (RTC_HOURS);
  59. mday = rtc_read (RTC_DATE_OF_MONTH);
  60. wday = rtc_read (RTC_DAY_OF_WEEK);
  61. mon = rtc_read (RTC_MONTH);
  62. year = rtc_read (RTC_YEAR);
  63. #ifdef CONFIG_AMIGAONEG3SE
  64. wday -= 1; /* VIA 686 stores Sunday = 1, Monday = 2, ... */
  65. #endif
  66. #ifdef RTC_DEBUG
  67. printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  68. "hr: %02x min: %02x sec: %02x\n",
  69. year, mon, mday, wday,
  70. hour, min, sec );
  71. printf ( "Alarms: month: %02x hour: %02x min: %02x sec: %02x\n",
  72. rtc_read (RTC_CONFIG_D) & 0x3F,
  73. rtc_read (RTC_HOURS_ALARM),
  74. rtc_read (RTC_MINUTES_ALARM),
  75. rtc_read (RTC_SECONDS_ALARM) );
  76. #endif
  77. tmp->tm_sec = bcd2bin (sec & 0x7F);
  78. tmp->tm_min = bcd2bin (min & 0x7F);
  79. tmp->tm_hour = bcd2bin (hour & 0x3F);
  80. tmp->tm_mday = bcd2bin (mday & 0x3F);
  81. tmp->tm_mon = bcd2bin (mon & 0x1F);
  82. tmp->tm_year = bcd2bin (year);
  83. tmp->tm_wday = bcd2bin (wday & 0x07);
  84. if(tmp->tm_year<70)
  85. tmp->tm_year+=2000;
  86. else
  87. tmp->tm_year+=1900;
  88. tmp->tm_yday = 0;
  89. tmp->tm_isdst= 0;
  90. #ifdef RTC_DEBUG
  91. printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  92. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  93. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  94. #endif
  95. }
  96. void rtc_set (struct rtc_time *tmp)
  97. {
  98. #ifdef RTC_DEBUG
  99. printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  100. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  101. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  102. #endif
  103. rtc_write(RTC_CONFIG_B,0x82); /* disables the RTC to update the regs */
  104. rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
  105. rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
  106. #ifdef CONFIG_AMIGAONEG3SE
  107. rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday)+1);
  108. #else
  109. rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
  110. #endif
  111. rtc_write (RTC_DATE_OF_MONTH, bin2bcd(tmp->tm_mday));
  112. rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
  113. rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
  114. rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
  115. rtc_write(RTC_CONFIG_B,0x02); /* enables the RTC to update the regs */
  116. }
  117. void rtc_reset (void)
  118. {
  119. rtc_write(RTC_CONFIG_B,0x82); /* disables the RTC to update the regs */
  120. rtc_write(RTC_CONFIG_A,0x20); /* Normal OP */
  121. rtc_write(RTC_CONFIG_B,0x00);
  122. rtc_write(RTC_CONFIG_B,0x00);
  123. rtc_write(RTC_CONFIG_B,0x02); /* enables the RTC to update the regs */
  124. }
  125. /* ------------------------------------------------------------------------- */
  126. #ifdef CFG_RTC_REG_BASE_ADDR
  127. /*
  128. * use direct memory access
  129. */
  130. static uchar rtc_read (uchar reg)
  131. {
  132. return(in8(CFG_RTC_REG_BASE_ADDR+reg));
  133. }
  134. static void rtc_write (uchar reg, uchar val)
  135. {
  136. out8(CFG_RTC_REG_BASE_ADDR+reg, val);
  137. }
  138. #else
  139. static uchar rtc_read (uchar reg)
  140. {
  141. out8(RTC_PORT_MC146818,reg);
  142. return(in8(RTC_PORT_MC146818+1));
  143. }
  144. static void rtc_write (uchar reg, uchar val)
  145. {
  146. out8(RTC_PORT_MC146818,reg);
  147. out8(RTC_PORT_MC146818+1,val);
  148. }
  149. #endif
  150. static unsigned bcd2bin (uchar n)
  151. {
  152. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  153. }
  154. static unsigned char bin2bcd (unsigned int n)
  155. {
  156. return (((n / 10) << 4) | (n % 10));
  157. }
  158. #endif /* CONFIG_RTC_MC146818 && CFG_CMD_DATE */