mc146818.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #define RTC_PORT_MC146818 CONFIG_SYS_ISA_IO_BASE_ADDRESS + 0x70
  24. #define RTC_SECONDS 0x00
  25. #define RTC_SECONDS_ALARM 0x01
  26. #define RTC_MINUTES 0x02
  27. #define RTC_MINUTES_ALARM 0x03
  28. #define RTC_HOURS 0x04
  29. #define RTC_HOURS_ALARM 0x05
  30. #define RTC_DAY_OF_WEEK 0x06
  31. #define RTC_DATE_OF_MONTH 0x07
  32. #define RTC_MONTH 0x08
  33. #define RTC_YEAR 0x09
  34. #define RTC_CONFIG_A 0x0A
  35. #define RTC_CONFIG_B 0x0B
  36. #define RTC_CONFIG_C 0x0C
  37. #define RTC_CONFIG_D 0x0D
  38. #define RTC_REG_SIZE 0x80
  39. #define RTC_CONFIG_A_REF_CLCK_32KHZ (1 << 5)
  40. #define RTC_CONFIG_A_RATE_1024HZ 6
  41. #define RTC_CONFIG_B_24H (1 << 1)
  42. #define RTC_CONFIG_D_VALID_RAM_AND_TIME 0x80
  43. /* ------------------------------------------------------------------------- */
  44. int rtc_get (struct rtc_time *tmp)
  45. {
  46. uchar sec, min, hour, mday, wday, mon, year;
  47. /* here check if rtc can be accessed */
  48. while ((rtc_read8(RTC_CONFIG_A) & 0x80) == 0x80);
  49. sec = rtc_read8(RTC_SECONDS);
  50. min = rtc_read8(RTC_MINUTES);
  51. hour = rtc_read8(RTC_HOURS);
  52. mday = rtc_read8(RTC_DATE_OF_MONTH);
  53. wday = rtc_read8(RTC_DAY_OF_WEEK);
  54. mon = rtc_read8(RTC_MONTH);
  55. year = rtc_read8(RTC_YEAR);
  56. #ifdef RTC_DEBUG
  57. printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  58. "hr: %02x min: %02x sec: %02x\n",
  59. year, mon, mday, wday,
  60. hour, min, sec );
  61. printf ( "Alarms: month: %02x hour: %02x min: %02x sec: %02x\n",
  62. rtc_read8(RTC_CONFIG_D) & 0x3F,
  63. rtc_read8(RTC_HOURS_ALARM),
  64. rtc_read8(RTC_MINUTES_ALARM),
  65. rtc_read8(RTC_SECONDS_ALARM));
  66. #endif
  67. tmp->tm_sec = bcd2bin (sec & 0x7F);
  68. tmp->tm_min = bcd2bin (min & 0x7F);
  69. tmp->tm_hour = bcd2bin (hour & 0x3F);
  70. tmp->tm_mday = bcd2bin (mday & 0x3F);
  71. tmp->tm_mon = bcd2bin (mon & 0x1F);
  72. tmp->tm_year = bcd2bin (year);
  73. tmp->tm_wday = bcd2bin (wday & 0x07);
  74. if(tmp->tm_year<70)
  75. tmp->tm_year+=2000;
  76. else
  77. tmp->tm_year+=1900;
  78. tmp->tm_yday = 0;
  79. tmp->tm_isdst= 0;
  80. #ifdef RTC_DEBUG
  81. printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  82. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  83. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  84. #endif
  85. return 0;
  86. }
  87. int rtc_set (struct rtc_time *tmp)
  88. {
  89. #ifdef RTC_DEBUG
  90. printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  91. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  92. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  93. #endif
  94. rtc_write8(RTC_CONFIG_B, 0x82); /* disable the RTC to update the regs */
  95. rtc_write8(RTC_YEAR, bin2bcd(tmp->tm_year % 100));
  96. rtc_write8(RTC_MONTH, bin2bcd(tmp->tm_mon));
  97. rtc_write8(RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
  98. rtc_write8(RTC_DATE_OF_MONTH, bin2bcd(tmp->tm_mday));
  99. rtc_write8(RTC_HOURS, bin2bcd(tmp->tm_hour));
  100. rtc_write8(RTC_MINUTES, bin2bcd(tmp->tm_min));
  101. rtc_write8(RTC_SECONDS, bin2bcd(tmp->tm_sec));
  102. rtc_write8(RTC_CONFIG_B, 0x02); /* enable the RTC to update the regs */
  103. return 0;
  104. }
  105. void rtc_reset (void)
  106. {
  107. rtc_write8(RTC_CONFIG_B, 0x82); /* disable the RTC to update the regs */
  108. rtc_write8(RTC_CONFIG_A, 0x20); /* Normal OP */
  109. rtc_write8(RTC_CONFIG_B, 0x00);
  110. rtc_write8(RTC_CONFIG_B, 0x00);
  111. rtc_write8(RTC_CONFIG_B, 0x02); /* enable the RTC to update the regs */
  112. }
  113. /* ------------------------------------------------------------------------- */
  114. /*
  115. * use direct memory access
  116. */
  117. int rtc_read8(int reg)
  118. {
  119. #ifdef CONFIG_SYS_RTC_REG_BASE_ADDR
  120. return in8(CONFIG_SYS_RTC_REG_BASE_ADDR + reg);
  121. #else
  122. int ofs = 0;
  123. if (reg >= 128) {
  124. ofs = 2;
  125. reg -= 128;
  126. }
  127. out8(RTC_PORT_MC146818 + ofs, reg);
  128. return in8(RTC_PORT_MC146818 + ofs + 1);
  129. #endif
  130. }
  131. void rtc_write8(int reg, uchar val)
  132. {
  133. #ifdef CONFIG_SYS_RTC_REG_BASE_ADDR
  134. out8(CONFIG_SYS_RTC_REG_BASE_ADDR + reg, val);
  135. #else
  136. int ofs = 0;
  137. if (reg >= 128) {
  138. ofs = 2;
  139. reg -= 128;
  140. }
  141. out8(RTC_PORT_MC146818 + ofs, reg);
  142. out8(RTC_PORT_MC146818 + ofs + 1, val);
  143. #endif
  144. }
  145. u32 rtc_read32(int reg)
  146. {
  147. u32 value = 0;
  148. int i;
  149. for (i = 0; i < sizeof(value); i++)
  150. value |= rtc_read8(reg + i) << (i << 3);
  151. return value;
  152. }
  153. void rtc_write32(int reg, u32 value)
  154. {
  155. int i;
  156. for (i = 0; i < sizeof(value); i++)
  157. rtc_write8(reg + i, (value >> (i << 3)) & 0xff);
  158. }
  159. void rtc_init(void)
  160. {
  161. #if CLEAR_CMOS
  162. int i;
  163. rtc_write8(RTC_SECONDS_ALARM, 0);
  164. rtc_write8(RTC_MINUTES_ALARM, 0);
  165. rtc_write8(RTC_HOURS_ALARM, 0);
  166. for (i = RTC_CONFIG_A; i < RTC_REG_SIZE; i++)
  167. rtc_write8(i, 0);
  168. printf("RTC: zeroing CMOS RAM\n");
  169. #endif
  170. /* Setup the real time clock */
  171. rtc_write8(RTC_CONFIG_B, RTC_CONFIG_B_24H);
  172. /* Setup the frequency it operates at */
  173. rtc_write8(RTC_CONFIG_A, RTC_CONFIG_A_REF_CLCK_32KHZ |
  174. RTC_CONFIG_A_RATE_1024HZ);
  175. /* Ensure all reserved bits are 0 in register D */
  176. rtc_write8(RTC_CONFIG_D, RTC_CONFIG_D_VALID_RAM_AND_TIME);
  177. /* Clear any pending interrupts */
  178. rtc_read8(RTC_CONFIG_C);
  179. }
  180. #endif