// SPDX-License-Identifier: GPL-2.0-only1/* -*- linux-c -*- ------------------------------------------------------- *2*3* Copyright (C) 1991, 1992 Linus Torvalds4* Copyright 2007-2008 rPath, Inc. - All Rights Reserved5* Copyright 2009 Intel Corporation; author H. Peter Anvin6*7* ----------------------------------------------------------------------- */89/*10* Enable A20 gate (return -1 on failure)11*/1213#include "boot.h"1415#define MAX_8042_LOOPS 10000016#define MAX_8042_FF 321718static int empty_8042(void)19{20u8 status;21int loops = MAX_8042_LOOPS;22int ffs = MAX_8042_FF;2324while (loops--) {25io_delay();2627status = inb(0x64);28if (status == 0xff) {29/* FF is a plausible, but very unlikely status */30if (!--ffs)31return -1; /* Assume no KBC present */32}33if (status & 1) {34/* Read and discard input data */35io_delay();36(void)inb(0x60);37} else if (!(status & 2)) {38/* Buffers empty, finished! */39return 0;40}41}4243return -1;44}4546/* Returns nonzero if the A20 line is enabled. The memory address47used as a test is the int $0x80 vector, which should be safe. */4849#define A20_TEST_ADDR (4*0x80)50#define A20_TEST_SHORT 3251#define A20_TEST_LONG 2097152 /* 2^21 */5253static int a20_test(int loops)54{55int ok = 0;56int saved, ctr;5758set_fs(0x0000);59set_gs(0xffff);6061saved = ctr = rdfs32(A20_TEST_ADDR);6263while (loops--) {64wrfs32(++ctr, A20_TEST_ADDR);65io_delay(); /* Serialize and make delay constant */66ok = rdgs32(A20_TEST_ADDR+0x10) ^ ctr;67if (ok)68break;69}7071wrfs32(saved, A20_TEST_ADDR);72return ok;73}7475/* Quick test to see if A20 is already enabled */76static int a20_test_short(void)77{78return a20_test(A20_TEST_SHORT);79}8081/* Longer test that actually waits for A20 to come on line; this82is useful when dealing with the KBC or other slow external circuitry. */83static int a20_test_long(void)84{85return a20_test(A20_TEST_LONG);86}8788static void enable_a20_bios(void)89{90struct biosregs ireg;9192initregs(&ireg);93ireg.ax = 0x2401;94intcall(0x15, &ireg, NULL);95}9697static void enable_a20_kbc(void)98{99empty_8042();100101outb(0xd1, 0x64); /* Command write */102empty_8042();103104outb(0xdf, 0x60); /* A20 on */105empty_8042();106107outb(0xff, 0x64); /* Null command, but UHCI wants it */108empty_8042();109}110111static void enable_a20_fast(void)112{113u8 port_a;114115port_a = inb(0x92); /* Configuration port A */116port_a |= 0x02; /* Enable A20 */117port_a &= ~0x01; /* Do not reset machine */118outb(port_a, 0x92);119}120121/*122* Actual routine to enable A20; return 0 on ok, -1 on failure123*/124125#define A20_ENABLE_LOOPS 255 /* Number of times to try */126127int enable_a20(void)128{129int loops = A20_ENABLE_LOOPS;130int kbc_err;131132while (loops--) {133/* First, check to see if A20 is already enabled134(legacy free, etc.) */135if (a20_test_short())136return 0;137138/* Next, try the BIOS (INT 0x15, AX=0x2401) */139enable_a20_bios();140if (a20_test_short())141return 0;142143/* Try enabling A20 through the keyboard controller */144kbc_err = empty_8042();145146if (a20_test_short())147return 0; /* BIOS worked, but with delayed reaction */148149if (!kbc_err) {150enable_a20_kbc();151if (a20_test_long())152return 0;153}154155/* Finally, try enabling the "fast A20 gate" */156enable_a20_fast();157if (a20_test_long())158return 0;159}160161return -1;162}163164165