Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2008 Rudolf Marek <r.marek@assembler.cz> |
| 5 | * Copyright (C) 2009 One Laptop per Child, Association, Inc. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Stefan Reinauer | 23836e2 | 2010-04-15 12:39:29 +0000 | [diff] [blame] | 18 | /* FIXME This code should be dropped and instead the generic resume code |
| 19 | * should be used. |
| 20 | */ |
| 21 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 22 | /* Parts of this code is taken from reboot.c from Linux. */ |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * This file mostly copied from Rudolf's S3 patch, some changes in |
| 26 | * acpi_jump_wake(). |
| 27 | */ |
| 28 | |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 29 | #include <stdint.h> |
| 30 | #include <string.h> |
| 31 | #include <arch/io.h> |
| 32 | #include <console/console.h> |
| 33 | #include <delay.h> |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 34 | #include "wakeup.h" |
| 35 | |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 36 | int enable_a20(void); |
| 37 | |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 38 | /* |
| 39 | * The following code and data reboots the machine by switching to real |
| 40 | * mode and jumping to the BIOS reset entry point, as if the CPU has |
| 41 | * really been reset. The previous version asked the keyboard |
| 42 | * controller to pulse the CPU reset line, which is more thorough, but |
| 43 | * doesn't work with at least one type of 486 motherboard. It is easy |
| 44 | * to stop this code working; hence the copious comments. |
| 45 | */ |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 46 | |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 47 | static unsigned long long real_mode_gdt_entries[3] = { |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 48 | 0x0000000000000000ULL, /* Null descriptor */ |
| 49 | 0x00009a000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */ |
| 50 | 0x000092000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */ |
| 51 | }; |
| 52 | |
| 53 | struct Xgt_desc_struct { |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 54 | unsigned short size; |
| 55 | unsigned long address __attribute__ ((packed)); |
| 56 | unsigned short pad; |
| 57 | } __attribute__ ((packed)); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 58 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 59 | static struct Xgt_desc_struct real_mode_gdt = { |
| 60 | sizeof(real_mode_gdt_entries) - 1, |
| 61 | (long)real_mode_gdt_entries |
| 62 | }, |
| 63 | real_mode_idt = {0x3ff, 0}, |
| 64 | no_idt = { 0, 0 }; |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 65 | |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 66 | /* |
| 67 | * This is 16-bit protected mode code to disable paging and the cache, |
| 68 | * switch to real mode and jump to the BIOS reset code. |
| 69 | * |
| 70 | * The instruction that switches to real mode by writing to CR0 must be |
| 71 | * followed immediately by a far jump instruction, which set CS to a |
| 72 | * valid value for real mode, and flushes the prefetch queue to avoid |
| 73 | * running instructions that have already been decoded in protected |
| 74 | * mode. |
| 75 | * |
| 76 | * Clears all the flags except ET, especially PG (paging), PE |
| 77 | * (protected-mode enable) and TS (task switch for coprocessor state |
| 78 | * save). Flushes the TLB after paging has been disabled. Sets CD and |
| 79 | * NW, to disable the cache on a 486, and invalidates the cache. This |
| 80 | * is more like the state of a 486 after reset. I don't know if |
| 81 | * something else should be done for other chips. |
| 82 | * |
| 83 | * More could be done here to set up the registers as if a CPU reset had |
| 84 | * occurred; hopefully real BIOSs don't assume much. |
| 85 | */ |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 86 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 87 | // 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000, %eax */ |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 88 | |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 89 | static unsigned char real_mode_switch[] = { |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 90 | 0x66, 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */ |
| 91 | 0x24, 0xfe, /* andb $0xfe,al */ |
| 92 | 0x66, 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */ |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 93 | }; |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 94 | |
| 95 | static unsigned char jump_to_wakeup[] = { |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 96 | 0xea, 0x00, 0x00, 0x00, 0xe0 /* ljmp $0xffff, $0x0000 */ |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 99 | void acpi_jump_wake(u32 vector) |
| 100 | { |
Stefan Reinauer | 56a684a | 2010-04-07 15:40:26 +0000 | [diff] [blame] | 101 | u32 dwEip; |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 102 | struct Xgt_desc_struct *wake_thunk16_Xgt_desc; |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 103 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 104 | printk(BIOS_DEBUG, "IN ACPI JUMP WAKE TO %x\n", vector); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 105 | if (enable_a20()) |
| 106 | die("failed to enable A20\n"); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 107 | printk(BIOS_DEBUG, "IN ACPI JUMP WAKE TO 3 %x\n", vector); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 108 | |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 109 | *((u16 *) (jump_to_wakeup + 3)) = (u16) (vector >> 4); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 110 | printk(BIOS_DEBUG, "%x %x %x %x %x\n", jump_to_wakeup[0], jump_to_wakeup[1], |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 111 | jump_to_wakeup[2], jump_to_wakeup[3], jump_to_wakeup[4]); |
| 112 | |
| 113 | memcpy((void *)(WAKE_THUNK16_ADDR - sizeof(real_mode_switch) - 100), |
| 114 | real_mode_switch, sizeof(real_mode_switch)); |
| 115 | memcpy((void *)(WAKE_THUNK16_ADDR - 100), jump_to_wakeup, |
| 116 | sizeof(jump_to_wakeup)); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 117 | |
Myles Watson | bd4f2f8 | 2009-07-02 21:19:33 +0000 | [diff] [blame] | 118 | //jason_tsc_count(); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 119 | printk(BIOS_EMERG, "file '%s', line %d\n\n", __FILE__, __LINE__); |
Myles Watson | bd4f2f8 | 2009-07-02 21:19:33 +0000 | [diff] [blame] | 120 | //jason_tsc_count_end(); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 121 | |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 122 | unsigned long long *real_mode_gdt_entries_at_eseg; |
Myles Watson | f4cc089 | 2010-04-14 16:50:16 +0000 | [diff] [blame] | 123 | real_mode_gdt_entries_at_eseg = (void *)WAKE_THUNK16_GDT; /* Copy from real_mode_gdt_entries and change limition to 1M and data base to 0; */ |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 124 | real_mode_gdt_entries_at_eseg[0] = 0x0000000000000000ULL; /* Null descriptor */ |
| 125 | real_mode_gdt_entries_at_eseg[1] = 0x000f9a000000ffffULL; /* 16-bit real-mode 1M code at 0x00000000 */ |
| 126 | real_mode_gdt_entries_at_eseg[2] = 0x000f93000000ffffULL; /* 16-bit real-mode 1M data at 0x00000000 */ |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 127 | |
Myles Watson | f4cc089 | 2010-04-14 16:50:16 +0000 | [diff] [blame] | 128 | wake_thunk16_Xgt_desc = (void *)WAKE_THUNK16_XDTR; |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 129 | wake_thunk16_Xgt_desc[0].size = sizeof(real_mode_gdt_entries) - 1; |
| 130 | wake_thunk16_Xgt_desc[0].address = (long)real_mode_gdt_entries_at_eseg; |
| 131 | wake_thunk16_Xgt_desc[1].size = 0x3ff; |
| 132 | wake_thunk16_Xgt_desc[1].address = 0; |
| 133 | wake_thunk16_Xgt_desc[2].size = 0; |
| 134 | wake_thunk16_Xgt_desc[2].address = 0; |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 135 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 136 | /* Added this code to get current value of EIP. */ |
| 137 | __asm__ volatile ( |
| 138 | "calll geip\n\t" |
| 139 | "geip: \n\t" |
| 140 | "popl %0\n\t" |
| 141 | : "=a" (dwEip) |
| 142 | ); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 143 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 144 | unsigned char *dest, *src; |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 145 | src = (unsigned char *)dwEip; |
Myles Watson | f4cc089 | 2010-04-14 16:50:16 +0000 | [diff] [blame] | 146 | dest = (void *)WAKE_RECOVER1M_CODE; |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 147 | u32 i; |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 148 | for (i = 0; i < 0x200; i++) |
| 149 | dest[i] = src[i]; |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 150 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 151 | __asm__ __volatile__("ljmp $0x0010,%0" /* 08 error */ |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 152 | ::"i"((void *)(WAKE_RECOVER1M_CODE + 0x20))); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 153 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 154 | /* Added 0x20 "nop" to make sure the ljmp will not jump then halt. */ |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 155 | asm volatile ("nop"); |
| 156 | asm volatile ("nop"); |
| 157 | asm volatile ("nop"); |
| 158 | asm volatile ("nop"); |
| 159 | asm volatile ("nop"); |
| 160 | asm volatile ("nop"); |
| 161 | asm volatile ("nop"); |
| 162 | asm volatile ("nop"); |
| 163 | asm volatile ("nop"); |
| 164 | asm volatile ("nop"); |
| 165 | |
| 166 | asm volatile ("nop"); |
| 167 | asm volatile ("nop"); |
| 168 | asm volatile ("nop"); |
| 169 | asm volatile ("nop"); |
| 170 | asm volatile ("nop"); |
| 171 | asm volatile ("nop"); |
| 172 | asm volatile ("nop"); |
| 173 | asm volatile ("nop"); |
| 174 | asm volatile ("nop"); |
| 175 | asm volatile ("nop"); |
| 176 | |
| 177 | asm volatile ("nop"); |
| 178 | asm volatile ("nop"); |
| 179 | asm volatile ("nop"); |
| 180 | asm volatile ("nop"); |
| 181 | asm volatile ("nop"); |
| 182 | asm volatile ("nop"); |
| 183 | asm volatile ("nop"); |
| 184 | asm volatile ("nop"); |
| 185 | asm volatile ("nop"); |
| 186 | asm volatile ("nop"); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 187 | |
| 188 | __asm__ volatile ( |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 189 | /* |
| 190 | * Set new esp, maybe ebp should not equal to esp?, due to the |
| 191 | * variable in acpi_jump_wake?, anyway, this may be not a big |
| 192 | * problem. and I didn't clear the area (ef000+-0x200) to zero. |
| 193 | */ |
| 194 | "movl %0, %%ebp\n\t" |
| 195 | "movl %0, %%esp\n\t"::"a" (WAKE_THUNK16_STACK) |
| 196 | ); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 197 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 198 | /* |
| 199 | * Only "src" and "dest" use the new stack, and the esp maybe also |
| 200 | * used in resumevector. |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 201 | */ |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 202 | #if PAYLOAD_IS_SEABIOS == 1 |
| 203 | /* WAKE_MEM_INFO inited in get_set_top_available_mem in tables.c. */ |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 204 | src = |
| 205 | (unsigned char *)((*(u32 *) WAKE_MEM_INFO) - 64 * 1024 - 0x100000); |
| 206 | dest = 0; |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * If recovered 0-e0000, then when resume, before WinXP turn on the |
| 210 | * desktop screen, there is gray background which last 1sec. |
| 211 | */ |
| 212 | for (i = 0; i < 0xa0000; i++) |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 213 | dest[i] = src[i]; |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 214 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 215 | #if 0 |
| 216 | __asm__ volatile ( |
| 217 | "movl %0, %%esi\n\t" |
| 218 | "movl $0, %%edi\n\t" |
| 219 | "movl $0xa0000, %%ecx\n\t" |
| 220 | "shrl $2, %%ecx\n\t" |
| 221 | "rep movsd\n\t" |
| 222 | ::"a"(src) |
| 223 | ); |
| 224 | #endif |
| 225 | src = (unsigned char *)((*(u32 *) WAKE_MEM_INFO) - 64 * 1024 |
| 226 | - 0x100000 + 0xc0000); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 227 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 228 | #if 0 |
| 229 | dest = 0xc0000; |
| 230 | for (i = 0; i < 0x20000; i++) |
| 231 | dest[i] = src[i]; |
| 232 | |
| 233 | __asm__ volatile ( |
| 234 | "movl %0, %%esi\n\t" |
| 235 | "movl $0xc0000, %%edi\n\t" |
| 236 | "movl $0x20000, %%ecx\n\t" |
| 237 | "shrl $2, %%ecx\n\t" |
| 238 | "rep movsd\n\t" |
| 239 | ::"a"(src) |
| 240 | ); |
| 241 | #endif |
| 242 | |
| 243 | src = (unsigned char *)((*(u32 *) WAKE_MEM_INFO) - 64 * 1024 |
| 244 | - 0x100000 + 0xe0000 + WAKE_SPECIAL_SIZE); |
| 245 | |
| 246 | /* dest = 0xf0000; */ |
| 247 | /* for (i = 0; i < 0x10000; i++) */ |
| 248 | /* dest[i] = src[i]; */ |
| 249 | __asm__ volatile ( |
| 250 | "movl %0, %%esi\n\t" |
| 251 | "movl %1, %%edi\n\t" |
| 252 | "movl %2, %%ecx\n\t" |
| 253 | "shrl $2, %%ecx\n\t" |
| 254 | "rep movsd\n\t"::"r" (src), |
| 255 | "r"(0xe0000 + WAKE_SPECIAL_SIZE), |
| 256 | "r"(0x10000 - WAKE_SPECIAL_SIZE) |
| 257 | ); |
| 258 | |
| 259 | src = (unsigned char *)((*(u32 *) WAKE_MEM_INFO) - 64 * 1024 |
| 260 | - 0x100000 + 0xf0000); |
| 261 | /* dest = 0xf0000; */ |
| 262 | /* for (i = 0; i < 0x10000; i++) */ |
| 263 | /* dest[i] = src[i]; */ |
| 264 | __asm__ volatile ( |
| 265 | "movl %0, %%esi\n\t" |
| 266 | "movl $0xf0000, %%edi\n\t" |
| 267 | "movl $0x10000, %%ecx\n\t" |
| 268 | "shrl $2, %%ecx\n\t" "rep movsd\n\t"::"a" (src) |
| 269 | ); |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 270 | |
| 271 | asm volatile ("wbinvd"); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 272 | #endif |
| 273 | /* Set up the IDT for real mode. */ |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 274 | asm volatile ("lidt %0"::"m" (wake_thunk16_Xgt_desc[1])); |
| 275 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 276 | /* |
| 277 | * Set up a GDT from which we can load segment descriptors for real |
| 278 | * mode. The GDT is not used in real mode; it is just needed here to |
| 279 | * prepare the descriptors. |
| 280 | */ |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 281 | asm volatile ("lgdt %0"::"m" (wake_thunk16_Xgt_desc[0])); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 282 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 283 | /* |
| 284 | * Load the data segment registers, and thus the descriptors ready for |
| 285 | * real mode. The base address of each segment is 0x100, 16 times the |
| 286 | * selector value being loaded here. This is so that the segment |
| 287 | * registers don't have to be reloaded after switching to real mode: |
| 288 | * the values are consistent for real mode operation already. |
| 289 | */ |
| 290 | __asm__ __volatile__( |
| 291 | "movl $0x0010,%%eax\n" |
| 292 | "\tmovl %%eax,%%ds\n" |
| 293 | "\tmovl %%eax,%%es\n" |
| 294 | "\tmovl %%eax,%%fs\n" |
| 295 | "\tmovl %%eax,%%gs\n" |
| 296 | "\tmovl %%eax,%%ss":::"eax" |
| 297 | ); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 298 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 299 | /* |
| 300 | * Jump to the 16-bit code that we copied earlier. It disables paging |
| 301 | * and the cache, switches to real mode, and jumps to the BIOS reset |
| 302 | * entry point. |
| 303 | */ |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 304 | |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 305 | __asm__ __volatile__( |
| 306 | "ljmp $0x0008,%0"::"i" |
| 307 | ((void *)(WAKE_THUNK16_ADDR - sizeof(real_mode_switch) - 100)) |
| 308 | ); |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 309 | } |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 310 | |
| 311 | /* -*- linux-c -*- ------------------------------------------------------- * |
| 312 | * |
| 313 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 314 | * Copyright 2007 rPath, Inc. - All Rights Reserved |
| 315 | * |
| 316 | * This file is part of the Linux kernel, and is made available under |
| 317 | * the terms of the GNU General Public License version 2. |
| 318 | * |
| 319 | * ----------------------------------------------------------------------- */ |
| 320 | |
| 321 | /* |
Stefan Reinauer | 8677a23 | 2010-12-11 20:33:41 +0000 | [diff] [blame] | 322 | * arch/x86/boot/a20.c |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 323 | * |
| 324 | * Enable A20 gate (return -1 on failure) |
| 325 | */ |
| 326 | |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 327 | #define MAX_8042_LOOPS 100000 |
| 328 | |
| 329 | static int empty_8042(void) |
| 330 | { |
| 331 | u8 status; |
| 332 | int loops = MAX_8042_LOOPS; |
| 333 | |
| 334 | while (loops--) { |
| 335 | udelay(1); |
| 336 | |
| 337 | status = inb(0x64); |
| 338 | if (status & 1) { |
| 339 | /* Read and discard input data */ |
| 340 | udelay(1); |
| 341 | (void)inb(0x60); |
| 342 | } else if (!(status & 2)) { |
| 343 | /* Buffers empty, finished! */ |
| 344 | return 0; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | /* Returns nonzero if the A20 line is enabled. The memory address |
| 352 | used as a test is the int $0x80 vector, which should be safe. */ |
| 353 | |
| 354 | #define A20_TEST_ADDR (4*0x80) |
| 355 | #define A20_TEST_SHORT 32 |
| 356 | #define A20_TEST_LONG 2097152 /* 2^21 */ |
| 357 | |
| 358 | static int a20_test(int loops) |
| 359 | { |
| 360 | int ok = 0; |
| 361 | int saved, ctr; |
| 362 | |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 363 | saved = ctr = *((u32 *) A20_TEST_ADDR); |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 364 | |
| 365 | while (loops--) { |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 366 | |
| 367 | *((u32 *) A20_TEST_ADDR) = ++ctr; |
| 368 | |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 369 | udelay(1); /* Serialize and make delay constant */ |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 370 | |
| 371 | ok = *((u32 *) A20_TEST_ADDR + 0xffff0 + 0x10) ^ ctr; |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 372 | if (ok) |
| 373 | break; |
| 374 | } |
| 375 | |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 376 | *((u32 *) A20_TEST_ADDR) = saved; |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 377 | return ok; |
| 378 | } |
| 379 | |
| 380 | /* Quick test to see if A20 is already enabled */ |
| 381 | static int a20_test_short(void) |
| 382 | { |
| 383 | return a20_test(A20_TEST_SHORT); |
| 384 | } |
| 385 | |
| 386 | /* Longer test that actually waits for A20 to come on line; this |
| 387 | is useful when dealing with the KBC or other slow external circuitry. */ |
| 388 | static int a20_test_long(void) |
| 389 | { |
| 390 | return a20_test(A20_TEST_LONG); |
| 391 | } |
| 392 | |
| 393 | static void enable_a20_kbc(void) |
| 394 | { |
| 395 | empty_8042(); |
| 396 | |
| 397 | outb(0xd1, 0x64); /* Command write */ |
| 398 | empty_8042(); |
| 399 | |
| 400 | outb(0xdf, 0x60); /* A20 on */ |
| 401 | empty_8042(); |
| 402 | } |
| 403 | |
| 404 | static void enable_a20_fast(void) |
| 405 | { |
| 406 | u8 port_a; |
| 407 | |
| 408 | port_a = inb(0x92); /* Configuration port A */ |
Uwe Hermann | 0ffff34 | 2009-06-07 13:46:50 +0000 | [diff] [blame] | 409 | port_a |= 0x02; /* Enable A20 */ |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 410 | port_a &= ~0x01; /* Do not reset machine */ |
| 411 | outb(port_a, 0x92); |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Actual routine to enable A20; return 0 on ok, -1 on failure |
| 416 | */ |
| 417 | |
| 418 | #define A20_ENABLE_LOOPS 255 /* Number of times to try */ |
| 419 | |
| 420 | int enable_a20(void) |
| 421 | { |
| 422 | int loops = A20_ENABLE_LOOPS; |
| 423 | |
| 424 | while (loops--) { |
| 425 | /* First, check to see if A20 is already enabled |
| 426 | (legacy free, etc.) */ |
| 427 | if (a20_test_short()) |
| 428 | return 0; |
| 429 | |
| 430 | /* Try enabling A20 through the keyboard controller */ |
| 431 | empty_8042(); |
Uwe Hermann | d64f403 | 2009-06-07 14:38:32 +0000 | [diff] [blame] | 432 | |
| 433 | // if (a20_test_short()) |
| 434 | // return 0; /* BIOS worked, but with delayed reaction */ |
Uwe Hermann | 20a98c9 | 2009-06-05 23:02:43 +0000 | [diff] [blame] | 435 | |
| 436 | enable_a20_kbc(); |
| 437 | if (a20_test_long()) |
| 438 | return 0; |
| 439 | |
| 440 | /* Finally, try enabling the "fast A20 gate" */ |
| 441 | enable_a20_fast(); |
| 442 | if (a20_test_long()) |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | return -1; |
| 447 | } |