a20.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. *
  6. * This file is part of the Linux kernel, and is made available under
  7. * the terms of the GNU General Public License version 2.
  8. *
  9. * ----------------------------------------------------------------------- */
  10. /*
  11. * arch/i386/boot/a20.c
  12. *
  13. * Enable A20 gate (return -1 on failure)
  14. */
  15. #include "boot.h"
  16. #define MAX_8042_LOOPS 100000
  17. static int empty_8042(void)
  18. {
  19. u8 status;
  20. int loops = MAX_8042_LOOPS;
  21. while (loops--) {
  22. io_delay();
  23. status = inb(0x64);
  24. if (status & 1) {
  25. /* Read and discard input data */
  26. io_delay();
  27. (void)inb(0x60);
  28. } else if (!(status & 2)) {
  29. /* Buffers empty, finished! */
  30. return 0;
  31. }
  32. }
  33. return -1;
  34. }
  35. /* Returns nonzero if the A20 line is enabled. The memory address
  36. used as a test is the int $0x80 vector, which should be safe. */
  37. #define A20_TEST_ADDR (4*0x80)
  38. #define A20_TEST_SHORT 32
  39. #define A20_TEST_LONG 2097152 /* 2^21 */
  40. static int a20_test(int loops)
  41. {
  42. int ok = 0;
  43. int saved, ctr;
  44. set_fs(0x0000);
  45. set_gs(0xffff);
  46. saved = ctr = rdfs32(A20_TEST_ADDR);
  47. while (loops--) {
  48. wrfs32(++ctr, A20_TEST_ADDR);
  49. io_delay(); /* Serialize and make delay constant */
  50. ok = rdgs32(A20_TEST_ADDR+0x10) ^ ctr;
  51. if (ok)
  52. break;
  53. }
  54. wrfs32(saved, A20_TEST_ADDR);
  55. return ok;
  56. }
  57. /* Quick test to see if A20 is already enabled */
  58. static int a20_test_short(void)
  59. {
  60. return a20_test(A20_TEST_SHORT);
  61. }
  62. /* Longer test that actually waits for A20 to come on line; this
  63. is useful when dealing with the KBC or other slow external circuitry. */
  64. static int a20_test_long(void)
  65. {
  66. return a20_test(A20_TEST_LONG);
  67. }
  68. static void enable_a20_bios(void)
  69. {
  70. asm volatile("pushfl; int $0x15; popfl"
  71. : : "a" ((u16)0x2401));
  72. }
  73. static void enable_a20_kbc(void)
  74. {
  75. empty_8042();
  76. outb(0xd1, 0x64); /* Command write */
  77. empty_8042();
  78. outb(0xdf, 0x60); /* A20 on */
  79. empty_8042();
  80. }
  81. static void enable_a20_fast(void)
  82. {
  83. u8 port_a;
  84. port_a = inb(0x92); /* Configuration port A */
  85. port_a |= 0x02; /* Enable A20 */
  86. port_a &= ~0x01; /* Do not reset machine */
  87. outb(port_a, 0x92);
  88. }
  89. /*
  90. * Actual routine to enable A20; return 0 on ok, -1 on failure
  91. */
  92. #define A20_ENABLE_LOOPS 255 /* Number of times to try */
  93. int enable_a20(void)
  94. {
  95. int loops = A20_ENABLE_LOOPS;
  96. #if defined(CONFIG_X86_ELAN)
  97. /* Elan croaks if we try to touch the KBC */
  98. enable_a20_fast();
  99. while (!a20_test_long())
  100. ;
  101. return 0;
  102. #elif defined(CONFIG_X86_VOYAGER)
  103. /* On Voyager, a20_test() is unsafe? */
  104. enable_a20_kbc();
  105. return 0;
  106. #else
  107. while (loops--) {
  108. /* First, check to see if A20 is already enabled
  109. (legacy free, etc.) */
  110. if (a20_test_short())
  111. return 0;
  112. /* Next, try the BIOS (INT 0x15, AX=0x2401) */
  113. enable_a20_bios();
  114. if (a20_test_short())
  115. return 0;
  116. /* Try enabling A20 through the keyboard controller */
  117. empty_8042();
  118. if (a20_test_short())
  119. return 0; /* BIOS worked, but with delayed reaction */
  120. enable_a20_kbc();
  121. if (a20_test_long())
  122. return 0;
  123. /* Finally, try enabling the "fast A20 gate" */
  124. enable_a20_fast();
  125. if (a20_test_long())
  126. return 0;
  127. }
  128. return -1;
  129. #endif
  130. }