cmos.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2008, 2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef GRUB_CMOS_H
  19. #define GRUB_CMOS_H 1
  20. #include <grub/types.h>
  21. #if !defined (__powerpc__) && !defined (__sparc__)
  22. #include <grub/cpu/io.h>
  23. #include <grub/cpu/cmos.h>
  24. #endif
  25. #define GRUB_CMOS_INDEX_SECOND 0
  26. #define GRUB_CMOS_INDEX_SECOND_ALARM 1
  27. #define GRUB_CMOS_INDEX_MINUTE 2
  28. #define GRUB_CMOS_INDEX_MINUTE_ALARM 3
  29. #define GRUB_CMOS_INDEX_HOUR 4
  30. #define GRUB_CMOS_INDEX_HOUR_ALARM 5
  31. #define GRUB_CMOS_INDEX_DAY_OF_WEEK 6
  32. #define GRUB_CMOS_INDEX_DAY_OF_MONTH 7
  33. #define GRUB_CMOS_INDEX_MONTH 8
  34. #define GRUB_CMOS_INDEX_YEAR 9
  35. #define GRUB_CMOS_INDEX_STATUS_A 0xA
  36. #define GRUB_CMOS_INDEX_STATUS_B 0xB
  37. #define GRUB_CMOS_INDEX_STATUS_C 0xC
  38. #define GRUB_CMOS_INDEX_STATUS_D 0xD
  39. #define GRUB_CMOS_STATUS_B_DAYLIGHT 1
  40. #define GRUB_CMOS_STATUS_B_24HOUR 2
  41. #define GRUB_CMOS_STATUS_B_BINARY 4
  42. static inline grub_uint8_t
  43. grub_bcd_to_num (grub_uint8_t a)
  44. {
  45. return ((a >> 4) * 10 + (a & 0xF));
  46. }
  47. static inline grub_uint8_t
  48. grub_num_to_bcd (grub_uint8_t a)
  49. {
  50. return (((a / 10) << 4) + (a % 10));
  51. }
  52. #if !defined (__powerpc__) && !defined (__sparc__)
  53. static inline grub_err_t
  54. grub_cmos_read (grub_uint8_t index, grub_uint8_t *val)
  55. {
  56. if (index & 0x80)
  57. {
  58. grub_outb (index & 0x7f, GRUB_CMOS_ADDR_REG_HI);
  59. *val = grub_inb (GRUB_CMOS_DATA_REG_HI);
  60. }
  61. else
  62. {
  63. grub_outb (index & 0x7f, GRUB_CMOS_ADDR_REG);
  64. *val = grub_inb (GRUB_CMOS_DATA_REG);
  65. }
  66. return GRUB_ERR_NONE;
  67. }
  68. static inline grub_err_t
  69. grub_cmos_write (grub_uint8_t index, grub_uint8_t value)
  70. {
  71. if (index & 0x80)
  72. {
  73. grub_outb (index & 0x7f, GRUB_CMOS_ADDR_REG_HI);
  74. grub_outb (value, GRUB_CMOS_DATA_REG_HI);
  75. }
  76. else
  77. {
  78. grub_outb (index & 0x7f, GRUB_CMOS_ADDR_REG);
  79. grub_outb (value, GRUB_CMOS_DATA_REG);
  80. }
  81. return GRUB_ERR_NONE;
  82. }
  83. #else
  84. grub_err_t grub_cmos_find_port (void);
  85. extern volatile grub_uint8_t *grub_cmos_port;
  86. static inline grub_err_t
  87. grub_cmos_read (grub_uint8_t index, grub_uint8_t *val)
  88. {
  89. if (!grub_cmos_port)
  90. {
  91. grub_err_t err;
  92. err = grub_cmos_find_port ();
  93. if (err)
  94. return err;
  95. }
  96. grub_cmos_port[((index & 0x80) >> 6) | 0] = index & 0x7f;
  97. *val = grub_cmos_port[((index & 0x80) >> 6) | 1];
  98. return GRUB_ERR_NONE;
  99. }
  100. static inline grub_err_t
  101. grub_cmos_write (grub_uint8_t index, grub_uint8_t val)
  102. {
  103. if (!grub_cmos_port)
  104. {
  105. grub_err_t err;
  106. err = grub_cmos_find_port ();
  107. if (err)
  108. return err;
  109. }
  110. grub_cmos_port[((index & 0x80) >> 6) | 0] = index;
  111. grub_cmos_port[((index & 0x80) >> 6) | 1] = val;
  112. return GRUB_ERR_NONE;
  113. }
  114. #endif
  115. #endif /* GRUB_CMOS_H */