pit.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2008 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. #include <grub/types.h>
  19. #include <grub/i386/io.h>
  20. #include <grub/i386/pit.h>
  21. GRUB_EXPORT(grub_pit_wait);
  22. #define TIMER2_REG_CONTROL 0x42
  23. #define TIMER_REG_COMMAND 0x43
  24. #define TIMER2_REG_LATCH 0x61
  25. #define TIMER2_SELECT 0x80
  26. #define TIMER_ENABLE_LSB 0x20
  27. #define TIMER_ENABLE_MSB 0x10
  28. #define TIMER2_LATCH 0x20
  29. #define TIMER2_SPEAKER 0x02
  30. #define TIMER2_GATE 0x01
  31. void
  32. grub_pit_wait (grub_uint16_t tics)
  33. {
  34. /* Disable timer2 gate and speaker. */
  35. grub_outb (grub_inb (TIMER2_REG_LATCH) & ~ (TIMER2_SPEAKER | TIMER2_GATE),
  36. TIMER2_REG_LATCH);
  37. /* Set tics. */
  38. grub_outb (TIMER2_SELECT | TIMER_ENABLE_LSB | TIMER_ENABLE_MSB, TIMER_REG_COMMAND);
  39. grub_outb (tics & 0xff, TIMER2_REG_CONTROL);
  40. grub_outb (tics >> 8, TIMER2_REG_CONTROL);
  41. /* Enable timer2 gate, keep speaker disabled. */
  42. grub_outb ((grub_inb (TIMER2_REG_LATCH) & ~ TIMER2_SPEAKER) | TIMER2_GATE,
  43. TIMER2_REG_LATCH);
  44. /* Wait. */
  45. while ((grub_inb (TIMER2_REG_LATCH) & TIMER2_LATCH) == 0x00);
  46. /* Disable timer2 gate and speaker. */
  47. grub_outb (grub_inb (TIMER2_REG_LATCH) & ~ (TIMER2_SPEAKER | TIMER2_GATE),
  48. TIMER2_REG_LATCH);
  49. }