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