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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * RTC-based time source
  27. *
  28. */
  29. #include <stdint.h>
  30. #include <time.h>
  31. #include <rtc.h>
  32. #include <ipxe/time.h>
  33. /**
  34. * Read RTC register
  35. *
  36. * @v address Register address
  37. * @ret data Data
  38. */
  39. static unsigned int rtc_readb ( int address ) {
  40. outb ( address, CMOS_ADDRESS );
  41. return inb ( CMOS_DATA );
  42. }
  43. /**
  44. * Check if RTC update is in progress
  45. *
  46. * @ret is_busy RTC update is in progress
  47. */
  48. static int rtc_is_busy ( void ) {
  49. return ( rtc_readb ( RTC_STATUS_A ) & RTC_STATUS_A_UPDATE_IN_PROGRESS );
  50. }
  51. /**
  52. * Read RTC BCD register
  53. *
  54. * @v address Register address
  55. * @ret value Value
  56. */
  57. static unsigned int rtc_readb_bcd ( int address ) {
  58. unsigned int bcd;
  59. bcd = rtc_readb ( address );
  60. return ( bcd - ( 6 * ( bcd >> 4 ) ) );
  61. }
  62. /**
  63. * Read RTC time
  64. *
  65. * @ret time Time, in seconds
  66. */
  67. static time_t rtc_read_time ( void ) {
  68. unsigned int status_b;
  69. int is_binary;
  70. int is_24hour;
  71. unsigned int ( * read_component ) ( int address );
  72. struct tm tm;
  73. int is_pm;
  74. unsigned int hour;
  75. time_t time;
  76. /* Wait for any in-progress update to complete */
  77. while ( rtc_is_busy() ) {}
  78. /* Determine RTC mode */
  79. status_b = rtc_readb ( RTC_STATUS_B );
  80. is_binary = ( status_b & RTC_STATUS_B_BINARY );
  81. is_24hour = ( status_b & RTC_STATUS_B_24_HOUR );
  82. read_component = ( is_binary ? rtc_readb : rtc_readb_bcd );
  83. /* Read time values */
  84. tm.tm_sec = read_component ( RTC_SEC );
  85. tm.tm_min = read_component ( RTC_MIN );
  86. hour = read_component ( RTC_HOUR );
  87. if ( ! is_24hour ) {
  88. is_pm = ( hour >= 80 );
  89. hour = ( ( ( ( hour & 0x7f ) % 80 ) % 12 ) +
  90. ( is_pm ? 12 : 0 ) );
  91. }
  92. tm.tm_hour = hour;
  93. tm.tm_mday = read_component ( RTC_MDAY );
  94. tm.tm_mon = ( read_component ( RTC_MON ) - 1 );
  95. tm.tm_year = ( read_component ( RTC_YEAR ) +
  96. 100 /* Assume we are in the 21st century, since
  97. * this code was written in 2012 */ );
  98. DBGC ( RTC_STATUS_A, "RTCTIME is %04d-%02d-%02d %02d:%02d:%02d "
  99. "(%s,%d-hour)\n", ( tm.tm_year + 1900 ), ( tm.tm_mon + 1 ),
  100. tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
  101. ( is_binary ? "binary" : "BCD" ), ( is_24hour ? 24 : 12 ) );
  102. /* Convert to seconds since the Epoch */
  103. time = mktime ( &tm );
  104. return time;
  105. }
  106. /**
  107. * Get current time in seconds
  108. *
  109. * @ret time Time, in seconds
  110. */
  111. static time_t rtc_now ( void ) {
  112. time_t time = 0;
  113. time_t last_time;
  114. /* Read time until we get two matching values in a row, in
  115. * case we end up reading a corrupted value in the middle of
  116. * an update.
  117. */
  118. do {
  119. last_time = time;
  120. time = rtc_read_time();
  121. } while ( time != last_time );
  122. return time;
  123. }
  124. PROVIDE_TIME ( rtc, time_now, rtc_now );