cmos.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* include/modules/cmos.h
  2. *
  3. * Copyright (C) 2021, 2022 intirain.
  4. *
  5. * This file is part of evx.
  6. *
  7. * evx is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * evx is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with evx. If not, see <https://www.gnu.org/licenses/>
  19. */
  20. #ifndef __MOD_CMOS_H
  21. #define __MOD_CMOS_H
  22. #ifndef __ASSEMBLER__
  23. #include <arch/io.h>
  24. #include <kernel/time.h>
  25. #define CMOS_READ(addr) ({ \
  26. outb(addr, 0x70); \
  27. inb(0x71); })
  28. /* For time and they actually come from linux. */
  29. #define BCD2BIN(byte) ((byte) = ((byte) & 0xf) + ((byte) >> 4) * 10)
  30. static inline void read_hw_clock(struct ktime *tm) {
  31. do {
  32. tm->sec = CMOS_READ(0);
  33. tm->min = CMOS_READ(2);
  34. tm->hour = CMOS_READ(4);
  35. tm->day = CMOS_READ(7);
  36. tm->month = CMOS_READ(8) - 1;
  37. tm->year = CMOS_READ(9);
  38. } while (tm->sec != CMOS_READ(0));
  39. BCD2BIN(tm->sec); BCD2BIN(tm->min); BCD2BIN(tm->hour);
  40. BCD2BIN(tm->day); BCD2BIN(tm->month); BCD2BIN(tm->year);
  41. tm->year += tm->year >= 70 ? 1900 : 2000;
  42. tm->month++;
  43. }
  44. #endif
  45. #endif