mc146818.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifdef __I386__
  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. static uchar rtc_read (uchar reg);
  21. static void rtc_write (uchar reg, uchar val);
  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. /* ------------------------------------------------------------------------- */
  38. int rtc_get (struct rtc_time *tmp)
  39. {
  40. uchar sec, min, hour, mday, wday, mon, year;
  41. /* here check if rtc can be accessed */
  42. while((rtc_read(RTC_CONFIG_A)&0x80)==0x80);
  43. sec = rtc_read (RTC_SECONDS);
  44. min = rtc_read (RTC_MINUTES);
  45. hour = rtc_read (RTC_HOURS);
  46. mday = rtc_read (RTC_DATE_OF_MONTH);
  47. wday = rtc_read (RTC_DAY_OF_WEEK);
  48. mon = rtc_read (RTC_MONTH);
  49. year = rtc_read (RTC_YEAR);
  50. #ifdef RTC_DEBUG
  51. printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  52. "hr: %02x min: %02x sec: %02x\n",
  53. year, mon, mday, wday,
  54. hour, min, sec );
  55. printf ( "Alarms: month: %02x hour: %02x min: %02x sec: %02x\n",
  56. rtc_read (RTC_CONFIG_D) & 0x3F,
  57. rtc_read (RTC_HOURS_ALARM),
  58. rtc_read (RTC_MINUTES_ALARM),
  59. rtc_read (RTC_SECONDS_ALARM) );
  60. #endif
  61. tmp->tm_sec = bcd2bin (sec & 0x7F);
  62. tmp->tm_min = bcd2bin (min & 0x7F);
  63. tmp->tm_hour = bcd2bin (hour & 0x3F);
  64. tmp->tm_mday = bcd2bin (mday & 0x3F);
  65. tmp->tm_mon = bcd2bin (mon & 0x1F);
  66. tmp->tm_year = bcd2bin (year);
  67. tmp->tm_wday = bcd2bin (wday & 0x07);
  68. if(tmp->tm_year<70)
  69. tmp->tm_year+=2000;
  70. else
  71. tmp->tm_year+=1900;
  72. tmp->tm_yday = 0;
  73. tmp->tm_isdst= 0;
  74. #ifdef RTC_DEBUG
  75. printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  76. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  77. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  78. #endif
  79. return 0;
  80. }
  81. int rtc_set (struct rtc_time *tmp)
  82. {
  83. #ifdef RTC_DEBUG
  84. printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  85. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  86. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  87. #endif
  88. rtc_write(RTC_CONFIG_B,0x82); /* disables the RTC to update the regs */
  89. rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
  90. rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
  91. rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
  92. rtc_write (RTC_DATE_OF_MONTH, bin2bcd(tmp->tm_mday));
  93. rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
  94. rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
  95. rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
  96. rtc_write(RTC_CONFIG_B,0x02); /* enables the RTC to update the regs */
  97. return 0;
  98. }
  99. void rtc_reset (void)
  100. {
  101. rtc_write(RTC_CONFIG_B,0x82); /* disables the RTC to update the regs */
  102. rtc_write(RTC_CONFIG_A,0x20); /* Normal OP */
  103. rtc_write(RTC_CONFIG_B,0x00);
  104. rtc_write(RTC_CONFIG_B,0x00);
  105. rtc_write(RTC_CONFIG_B,0x02); /* enables the RTC to update the regs */
  106. }
  107. /* ------------------------------------------------------------------------- */
  108. #ifdef CONFIG_SYS_RTC_REG_BASE_ADDR
  109. /*
  110. * use direct memory access
  111. */
  112. static uchar rtc_read (uchar reg)
  113. {
  114. return(in8(CONFIG_SYS_RTC_REG_BASE_ADDR+reg));
  115. }
  116. static void rtc_write (uchar reg, uchar val)
  117. {
  118. out8(CONFIG_SYS_RTC_REG_BASE_ADDR+reg, val);
  119. }
  120. #else
  121. static uchar rtc_read (uchar reg)
  122. {
  123. out8(RTC_PORT_MC146818,reg);
  124. return(in8(RTC_PORT_MC146818+1));
  125. }
  126. static void rtc_write (uchar reg, uchar val)
  127. {
  128. out8(RTC_PORT_MC146818,reg);
  129. out8(RTC_PORT_MC146818+1,val);
  130. }
  131. #endif
  132. #endif