mc146818.c 5.1 KB

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