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