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