You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rtc_time.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * RTC-based time source
  23. *
  24. */
  25. #include <stdint.h>
  26. #include <time.h>
  27. #include <rtc.h>
  28. #include <ipxe/time.h>
  29. /**
  30. * Read RTC register
  31. *
  32. * @v address Register address
  33. * @ret data Data
  34. */
  35. static unsigned int rtc_readb ( int address ) {
  36. outb ( address, CMOS_ADDRESS );
  37. return inb ( CMOS_DATA );
  38. }
  39. /**
  40. * Check if RTC update is in progress
  41. *
  42. * @ret is_busy RTC update is in progress
  43. */
  44. static int rtc_is_busy ( void ) {
  45. return ( rtc_readb ( RTC_STATUS_A ) & RTC_STATUS_A_UPDATE_IN_PROGRESS );
  46. }
  47. /**
  48. * Read RTC BCD register
  49. *
  50. * @v address Register address
  51. * @ret value Value
  52. */
  53. static unsigned int rtc_readb_bcd ( int address ) {
  54. unsigned int bcd;
  55. bcd = rtc_readb ( address );
  56. return ( bcd - ( 6 * ( bcd >> 4 ) ) );
  57. }
  58. /**
  59. * Read RTC time
  60. *
  61. * @ret time Time, in seconds
  62. */
  63. static time_t rtc_read_time ( void ) {
  64. unsigned int status_b;
  65. int is_binary;
  66. int is_24hour;
  67. unsigned int ( * read_component ) ( int address );
  68. struct tm tm;
  69. int is_pm;
  70. unsigned int hour;
  71. time_t time;
  72. /* Wait for any in-progress update to complete */
  73. while ( rtc_is_busy() ) {}
  74. /* Determine RTC mode */
  75. status_b = rtc_readb ( RTC_STATUS_B );
  76. is_binary = ( status_b & RTC_STATUS_B_BINARY );
  77. is_24hour = ( status_b & RTC_STATUS_B_24_HOUR );
  78. read_component = ( is_binary ? rtc_readb : rtc_readb_bcd );
  79. /* Read time values */
  80. tm.tm_sec = read_component ( RTC_SEC );
  81. tm.tm_min = read_component ( RTC_MIN );
  82. hour = read_component ( RTC_HOUR );
  83. if ( ! is_24hour ) {
  84. is_pm = ( hour >= 80 );
  85. hour = ( ( ( ( hour & 0x7f ) % 80 ) % 12 ) +
  86. ( is_pm ? 12 : 0 ) );
  87. }
  88. tm.tm_hour = hour;
  89. tm.tm_mday = read_component ( RTC_MDAY );
  90. tm.tm_mon = ( read_component ( RTC_MON ) - 1 );
  91. tm.tm_year = ( read_component ( RTC_YEAR ) +
  92. 100 /* Assume we are in the 21st century, since
  93. * this code was written in 2012 */ );
  94. DBGC ( RTC_STATUS_A, "RTCTIME is %04d-%02d-%02d %02d:%02d:%02d "
  95. "(%s,%d-hour)\n", ( tm.tm_year + 1900 ), ( tm.tm_mon + 1 ),
  96. tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
  97. ( is_binary ? "binary" : "BCD" ), ( is_24hour ? 24 : 12 ) );
  98. /* Convert to seconds since the Epoch */
  99. time = mktime ( &tm );
  100. return time;
  101. }
  102. /**
  103. * Get current time in seconds
  104. *
  105. * @ret time Time, in seconds
  106. */
  107. static time_t rtc_now ( void ) {
  108. time_t time = 0;
  109. time_t last_time;
  110. /* Read time until we get two matching values in a row, in
  111. * case we end up reading a corrupted value in the middle of
  112. * an update.
  113. */
  114. do {
  115. last_time = time;
  116. time = rtc_read_time();
  117. } while ( time != last_time );
  118. return time;
  119. }
  120. PROVIDE_TIME ( rtc, time_now, rtc_now );