1 // SPDX-License-Identifier: GPL-2.0-only
2 /* -*- linux-c -*- ------------------------------------------------------- *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright 2007-2008 rPath, Inc. - All Rights Reserved
6 * Copyright 2009 Intel Corporation; author H. Peter Anvin
8 * ----------------------------------------------------------------------- */
11 * Enable A20 gate (return -1 on failure)
16 #define MAX_8042_LOOPS 100000
17 #define MAX_8042_FF 32
19 static int empty_8042(void)
22 int loops
= MAX_8042_LOOPS
;
23 int ffs
= MAX_8042_FF
;
30 /* FF is a plausible, but very unlikely status */
32 return -1; /* Assume no KBC present */
35 /* Read and discard input data */
38 } else if (!(status
& 2)) {
39 /* Buffers empty, finished! */
47 /* Returns nonzero if the A20 line is enabled. The memory address
48 used as a test is the int $0x80 vector, which should be safe. */
50 #define A20_TEST_ADDR (4*0x80)
51 #define A20_TEST_SHORT 32
52 #define A20_TEST_LONG 2097152 /* 2^21 */
54 static int a20_test(int loops
)
62 saved
= ctr
= rdfs32(A20_TEST_ADDR
);
65 wrfs32(++ctr
, A20_TEST_ADDR
);
66 io_delay(); /* Serialize and make delay constant */
67 ok
= rdgs32(A20_TEST_ADDR
+0x10) ^ ctr
;
72 wrfs32(saved
, A20_TEST_ADDR
);
76 /* Quick test to see if A20 is already enabled */
77 static int a20_test_short(void)
79 return a20_test(A20_TEST_SHORT
);
82 /* Longer test that actually waits for A20 to come on line; this
83 is useful when dealing with the KBC or other slow external circuitry. */
84 static int a20_test_long(void)
86 return a20_test(A20_TEST_LONG
);
89 static void enable_a20_bios(void)
95 intcall(0x15, &ireg
, NULL
);
98 static void enable_a20_kbc(void)
102 outb(0xd1, 0x64); /* Command write */
105 outb(0xdf, 0x60); /* A20 on */
108 outb(0xff, 0x64); /* Null command, but UHCI wants it */
112 static void enable_a20_fast(void)
116 port_a
= inb(0x92); /* Configuration port A */
117 port_a
|= 0x02; /* Enable A20 */
118 port_a
&= ~0x01; /* Do not reset machine */
123 * Actual routine to enable A20; return 0 on ok, -1 on failure
126 #define A20_ENABLE_LOOPS 255 /* Number of times to try */
130 int loops
= A20_ENABLE_LOOPS
;
134 /* First, check to see if A20 is already enabled
135 (legacy free, etc.) */
136 if (a20_test_short())
139 /* Next, try the BIOS (INT 0x15, AX=0x2401) */
141 if (a20_test_short())
144 /* Try enabling A20 through the keyboard controller */
145 kbc_err
= empty_8042();
147 if (a20_test_short())
148 return 0; /* BIOS worked, but with delayed reaction */
156 /* Finally, try enabling the "fast A20 gate" */