a20.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007-2008 rPath, Inc. - All Rights Reserved
  5. * Copyright 2009 Intel Corporation; author H. Peter Anvin
  6. *
  7. * This file is part of the Linux kernel, and is made available under
  8. * the terms of the GNU General Public License version 2.
  9. *
  10. * ----------------------------------------------------------------------- */
  11. /*
  12. * Enable A20 gate (return -1 on failure)
  13. */
  14. #include "boot.h"
  15. #define MAX_8042_LOOPS 100000
  16. #define MAX_8042_FF 32
  17. static int empty_8042(void)
  18. {
  19. u8 status;
  20. int loops = MAX_8042_LOOPS;
  21. int ffs = MAX_8042_FF;
  22. while (loops--) {
  23. io_delay();
  24. status = inb(0x64);
  25. if (status == 0xff) {
  26. /* FF is a plausible, but very unlikely status */
  27. if (!--ffs)
  28. return -1; /* Assume no KBC present */
  29. }
  30. if (status & 1) {
  31. /* Read and discard input data */
  32. io_delay();
  33. (void)inb(0x60);
  34. } else if (!(status & 2)) {
  35. /* Buffers empty, finished! */
  36. return 0;
  37. }
  38. }
  39. return -1;
  40. }
  41. /* Returns nonzero if the A20 line is enabled. The memory address
  42. used as a test is the int $0x80 vector, which should be safe. */
  43. #define A20_TEST_ADDR (4*0x80)
  44. #define A20_TEST_SHORT 32
  45. #define A20_TEST_LONG 2097152 /* 2^21 */
  46. static int a20_test(int loops)
  47. {
  48. int ok = 0;
  49. int saved, ctr;
  50. set_fs(0x0000);
  51. set_gs(0xffff);
  52. saved = ctr = rdfs32(A20_TEST_ADDR);
  53. while (loops--) {
  54. wrfs32(++ctr, A20_TEST_ADDR);
  55. io_delay(); /* Serialize and make delay constant */
  56. ok = rdgs32(A20_TEST_ADDR+0x10) ^ ctr;
  57. if (ok)
  58. break;
  59. }
  60. wrfs32(saved, A20_TEST_ADDR);
  61. return ok;
  62. }
  63. /* Quick test to see if A20 is already enabled */
  64. static int a20_test_short(void)
  65. {
  66. return a20_test(A20_TEST_SHORT);
  67. }
  68. /* Longer test that actually waits for A20 to come on line; this
  69. is useful when dealing with the KBC or other slow external circuitry. */
  70. static int a20_test_long(void)
  71. {
  72. return a20_test(A20_TEST_LONG);
  73. }
  74. static void enable_a20_bios(void)
  75. {
  76. struct biosregs ireg;
  77. initregs(&ireg);
  78. ireg.ax = 0x2401;
  79. intcall(0x15, &ireg, NULL);
  80. }
  81. static void enable_a20_kbc(void)
  82. {
  83. empty_8042();
  84. outb(0xd1, 0x64); /* Command write */
  85. empty_8042();
  86. outb(0xdf, 0x60); /* A20 on */
  87. empty_8042();
  88. outb(0xff, 0x64); /* Null command, but UHCI wants it */
  89. empty_8042();
  90. }
  91. static void enable_a20_fast(void)
  92. {
  93. u8 port_a;
  94. port_a = inb(0x92); /* Configuration port A */
  95. port_a |= 0x02; /* Enable A20 */
  96. port_a &= ~0x01; /* Do not reset machine */
  97. outb(port_a, 0x92);
  98. }
  99. /*
  100. * Actual routine to enable A20; return 0 on ok, -1 on failure
  101. */
  102. #define A20_ENABLE_LOOPS 255 /* Number of times to try */
  103. int enable_a20(void)
  104. {
  105. int loops = A20_ENABLE_LOOPS;
  106. int kbc_err;
  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. kbc_err = empty_8042();
  118. if (a20_test_short())
  119. return 0; /* BIOS worked, but with delayed reaction */
  120. if (!kbc_err) {
  121. enable_a20_kbc();
  122. if (a20_test_long())
  123. return 0;
  124. }
  125. /* Finally, try enabling the "fast A20 gate" */
  126. enable_a20_fast();
  127. if (a20_test_long())
  128. return 0;
  129. }
  130. return -1;
  131. }