mc146818.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter MPL AG Switzerland. d.peter@mpl.ch
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Date & Time support for the MC146818 (PIXX4) RTC
  9. */
  10. /*#define DEBUG*/
  11. #include <common.h>
  12. #include <command.h>
  13. #include <rtc.h>
  14. #include <version.h>
  15. #if defined(__I386__) || defined(CONFIG_MALTA)
  16. #include <asm/io.h>
  17. #define in8(p) inb(p)
  18. #define out8(p, v) outb(v, p)
  19. #endif
  20. #if defined(CONFIG_CMD_DATE)
  21. /* Set this to 1 to clear the CMOS RAM */
  22. #define CLEAR_CMOS 0
  23. static uchar rtc_read (uchar reg);
  24. static void rtc_write (uchar reg, uchar val);
  25. #define RTC_PORT_MC146818 CONFIG_SYS_ISA_IO_BASE_ADDRESS + 0x70
  26. #define RTC_SECONDS 0x00
  27. #define RTC_SECONDS_ALARM 0x01
  28. #define RTC_MINUTES 0x02
  29. #define RTC_MINUTES_ALARM 0x03
  30. #define RTC_HOURS 0x04
  31. #define RTC_HOURS_ALARM 0x05
  32. #define RTC_DAY_OF_WEEK 0x06
  33. #define RTC_DATE_OF_MONTH 0x07
  34. #define RTC_MONTH 0x08
  35. #define RTC_YEAR 0x09
  36. #define RTC_CONFIG_A 0x0A
  37. #define RTC_CONFIG_B 0x0B
  38. #define RTC_CONFIG_C 0x0C
  39. #define RTC_CONFIG_D 0x0D
  40. #define RTC_REG_SIZE 0x80
  41. #define RTC_CONFIG_A_REF_CLCK_32KHZ (1 << 5)
  42. #define RTC_CONFIG_A_RATE_1024HZ 6
  43. #define RTC_CONFIG_B_24H (1 << 1)
  44. #define RTC_CONFIG_D_VALID_RAM_AND_TIME 0x80
  45. /* ------------------------------------------------------------------------- */
  46. int rtc_get (struct rtc_time *tmp)
  47. {
  48. uchar sec, min, hour, mday, wday, mon, year;
  49. /* here check if rtc can be accessed */
  50. while((rtc_read(RTC_CONFIG_A)&0x80)==0x80);
  51. sec = rtc_read (RTC_SECONDS);
  52. min = rtc_read (RTC_MINUTES);
  53. hour = rtc_read (RTC_HOURS);
  54. mday = rtc_read (RTC_DATE_OF_MONTH);
  55. wday = rtc_read (RTC_DAY_OF_WEEK);
  56. mon = rtc_read (RTC_MONTH);
  57. year = rtc_read (RTC_YEAR);
  58. #ifdef RTC_DEBUG
  59. printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  60. "hr: %02x min: %02x sec: %02x\n",
  61. year, mon, mday, wday,
  62. hour, min, sec );
  63. printf ( "Alarms: month: %02x hour: %02x min: %02x sec: %02x\n",
  64. rtc_read (RTC_CONFIG_D) & 0x3F,
  65. rtc_read (RTC_HOURS_ALARM),
  66. rtc_read (RTC_MINUTES_ALARM),
  67. rtc_read (RTC_SECONDS_ALARM) );
  68. #endif
  69. tmp->tm_sec = bcd2bin (sec & 0x7F);
  70. tmp->tm_min = bcd2bin (min & 0x7F);
  71. tmp->tm_hour = bcd2bin (hour & 0x3F);
  72. tmp->tm_mday = bcd2bin (mday & 0x3F);
  73. tmp->tm_mon = bcd2bin (mon & 0x1F);
  74. tmp->tm_year = bcd2bin (year);
  75. tmp->tm_wday = bcd2bin (wday & 0x07);
  76. if(tmp->tm_year<70)
  77. tmp->tm_year+=2000;
  78. else
  79. tmp->tm_year+=1900;
  80. tmp->tm_yday = 0;
  81. tmp->tm_isdst= 0;
  82. #ifdef RTC_DEBUG
  83. printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  84. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  85. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  86. #endif
  87. return 0;
  88. }
  89. int rtc_set (struct rtc_time *tmp)
  90. {
  91. #ifdef RTC_DEBUG
  92. printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  93. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  94. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  95. #endif
  96. rtc_write(RTC_CONFIG_B,0x82); /* disables the RTC to update the regs */
  97. rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
  98. rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
  99. rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
  100. rtc_write (RTC_DATE_OF_MONTH, bin2bcd(tmp->tm_mday));
  101. rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
  102. rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
  103. rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
  104. rtc_write(RTC_CONFIG_B,0x02); /* enables the RTC to update the regs */
  105. return 0;
  106. }
  107. void rtc_reset (void)
  108. {
  109. rtc_write(RTC_CONFIG_B,0x82); /* disables the RTC to update the regs */
  110. rtc_write(RTC_CONFIG_A,0x20); /* Normal OP */
  111. rtc_write(RTC_CONFIG_B,0x00);
  112. rtc_write(RTC_CONFIG_B,0x00);
  113. rtc_write(RTC_CONFIG_B,0x02); /* enables the RTC to update the regs */
  114. }
  115. /* ------------------------------------------------------------------------- */
  116. #ifdef CONFIG_SYS_RTC_REG_BASE_ADDR
  117. /*
  118. * use direct memory access
  119. */
  120. static uchar rtc_read (uchar reg)
  121. {
  122. return in8(CONFIG_SYS_RTC_REG_BASE_ADDR + reg);
  123. }
  124. static void rtc_write (uchar reg, uchar val)
  125. {
  126. out8(CONFIG_SYS_RTC_REG_BASE_ADDR + reg, val);
  127. }
  128. #else
  129. static uchar rtc_read (uchar reg)
  130. {
  131. out8(RTC_PORT_MC146818,reg);
  132. return in8(RTC_PORT_MC146818 + 1);
  133. }
  134. static void rtc_write (uchar reg, uchar val)
  135. {
  136. out8(RTC_PORT_MC146818,reg);
  137. out8(RTC_PORT_MC146818+1, val);
  138. }
  139. #endif
  140. void rtc_init(void)
  141. {
  142. #if CLEAR_CMOS
  143. int i;
  144. rtc_write(RTC_SECONDS_ALARM, 0);
  145. rtc_write(RTC_MINUTES_ALARM, 0);
  146. rtc_write(RTC_HOURS_ALARM, 0);
  147. for (i = RTC_CONFIG_A; i < RTC_REG_SIZE; i++)
  148. rtc_write(i, 0);
  149. printf("RTC: zeroing CMOS RAM\n");
  150. #endif
  151. /* Setup the real time clock */
  152. rtc_write(RTC_CONFIG_B, RTC_CONFIG_B_24H);
  153. /* Setup the frequency it operates at */
  154. rtc_write(RTC_CONFIG_A, RTC_CONFIG_A_REF_CLCK_32KHZ |
  155. RTC_CONFIG_A_RATE_1024HZ);
  156. /* Ensure all reserved bits are 0 in register D */
  157. rtc_write(RTC_CONFIG_D, RTC_CONFIG_D_VALID_RAM_AND_TIME);
  158. /* Clear any pending interrupts */
  159. rtc_read(RTC_CONFIG_C);
  160. }
  161. #endif