a20.c 3.2 KB

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