rtc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* RTC (CMOS) date/time read. */
  2. #include "rtc.h"
  3. #define CMOS_ADDR 0x70
  4. #define CMOS_DATA 0x71
  5. static inline unsigned char cmos_read(unsigned char reg)
  6. {
  7. unsigned char v;
  8. /* Bit 7 on 0x70 disables NMI during access. */
  9. __asm__ volatile ("outb %b1, %w0" : : "Nd"(CMOS_ADDR), "a"((unsigned char)(reg | 0x80)));
  10. __asm__ volatile ("inb %w1, %0" : "=a"(v) : "Nd"(CMOS_DATA));
  11. return v;
  12. }
  13. static unsigned int bcd_to_bin(unsigned char bcd)
  14. {
  15. return (unsigned int)((bcd >> 4) * 10 + (bcd & 0x0F));
  16. }
  17. static int bcd_digits(unsigned char bcd)
  18. {
  19. return (bcd & 0x0F) <= 9 && ((bcd >> 4) & 0x0F) <= 9;
  20. }
  21. /* Read hour/minute/second out of the CMOS. A read that straddles the RTC's
  22. * update cycle can tear (invalid BCD, or a value out of range), which used to
  23. * feed nonsense into callers - retry a few times and fall back to the last
  24. * values that validated. Handles the 12-hour PM flag (hour bit 7). */
  25. static void rtc_read_time(unsigned int *hour, unsigned int *minute,
  26. unsigned int *second)
  27. {
  28. static unsigned int last_h, last_m, last_s;
  29. unsigned int tries;
  30. for (tries = 0; tries < 4; tries++) {
  31. unsigned int n = 0;
  32. unsigned char s, m, h;
  33. unsigned int sv, mv, hv;
  34. while ((cmos_read(0x0A) & 0x80) && ++n < 10000)
  35. ;
  36. s = cmos_read(0x00);
  37. m = cmos_read(0x02);
  38. h = (unsigned char)(cmos_read(0x04) & 0x7F); /* drop the PM flag */
  39. if (!bcd_digits(s) || !bcd_digits(m) || !bcd_digits(h))
  40. continue;
  41. sv = bcd_to_bin(s);
  42. mv = bcd_to_bin(m);
  43. hv = bcd_to_bin(h);
  44. if (sv > 59 || mv > 59 || hv > 23)
  45. continue;
  46. last_h = hv;
  47. last_m = mv;
  48. last_s = sv;
  49. break;
  50. }
  51. *hour = last_h;
  52. *minute = last_m;
  53. *second = last_s;
  54. }
  55. void rtc_read_datetime(char *buf)
  56. {
  57. static unsigned int last_d = 1, last_mo = 1, last_y = 2000;
  58. unsigned int s, m, h, d, mo, y;
  59. unsigned char dd, mm, yy;
  60. rtc_read_time(&h, &m, &s);
  61. /* Date registers (a torn read keeps the last good values). */
  62. {
  63. unsigned int n = 0;
  64. while ((cmos_read(0x0A) & 0x80) && ++n < 10000)
  65. ;
  66. }
  67. dd = cmos_read(0x07);
  68. mm = cmos_read(0x08);
  69. yy = cmos_read(0x09);
  70. if (bcd_digits(dd) && bcd_to_bin(dd) >= 1 && bcd_to_bin(dd) <= 31)
  71. last_d = bcd_to_bin(dd);
  72. if (bcd_digits(mm) && bcd_to_bin(mm) >= 1 && bcd_to_bin(mm) <= 12)
  73. last_mo = bcd_to_bin(mm);
  74. if (bcd_digits(yy))
  75. last_y = 2000 + bcd_to_bin(yy);
  76. d = last_d;
  77. mo = last_mo;
  78. y = last_y;
  79. if (y < 2000)
  80. y += 100;
  81. buf[0] = (char)('0' + (y / 1000));
  82. buf[1] = (char)('0' + ((y / 100) % 10));
  83. buf[2] = (char)('0' + ((y / 10) % 10));
  84. buf[3] = (char)('0' + (y % 10));
  85. buf[4] = '-';
  86. buf[5] = (char)('0' + (mo / 10));
  87. buf[6] = (char)('0' + (mo % 10));
  88. buf[7] = '-';
  89. buf[8] = (char)('0' + (d / 10));
  90. buf[9] = (char)('0' + (d % 10));
  91. buf[10] = ' ';
  92. buf[11] = (char)('0' + (h / 10));
  93. buf[12] = (char)('0' + (h % 10));
  94. buf[13] = ':';
  95. buf[14] = (char)('0' + (m / 10));
  96. buf[15] = (char)('0' + (m % 10));
  97. buf[16] = ':';
  98. buf[17] = (char)('0' + (s / 10));
  99. buf[18] = (char)('0' + (s % 10));
  100. buf[19] = '\0';
  101. }
  102. void rtc_read_hms(unsigned int *hour, unsigned int *minute, unsigned int *second)
  103. {
  104. rtc_read_time(hour, minute, second);
  105. }