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