Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 3 | * Video BOOT Graphics Card POST Module |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 4 | * |
| 5 | * ======================================================================== |
Kumar Gala | 6a6d948 | 2009-07-28 21:49:52 -0500 | [diff] [blame] | 6 | * Copyright (C) 2007 Freescale Semiconductor, Inc. |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 7 | * Jason Jin <Jason.jin@freescale.com> |
| 8 | * |
| 9 | * Copyright (C) 1991-2004 SciTech Software, Inc. All rights reserved. |
| 10 | * |
| 11 | * This file may be distributed and/or modified under the terms of the |
| 12 | * GNU General Public License version 2.0 as published by the Free |
| 13 | * Software Foundation and appearing in the file LICENSE.GPL included |
| 14 | * in the packaging of this file. |
| 15 | * |
| 16 | * Licensees holding a valid Commercial License for this product from |
| 17 | * SciTech Software, Inc. may use this file in accordance with the |
| 18 | * Commercial License Agreement provided with the Software. |
| 19 | * |
| 20 | * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING |
| 21 | * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE. |
| 23 | * |
| 24 | * See http://www.scitechsoft.com/license/ for information about |
| 25 | * the licensing options available and how to purchase a Commercial |
| 26 | * License Agreement. |
| 27 | * |
| 28 | * Contact license@scitechsoft.com if any conditions of this licensing |
| 29 | * are not clear to you, or you have questions about licensing options. |
| 30 | * |
| 31 | * ======================================================================== |
| 32 | * |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 33 | * Language: ANSI C |
| 34 | * Environment: Linux Kernel |
| 35 | * Developer: Kendall Bennett |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 36 | * |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 37 | * Description: Module to implement booting PCI/AGP controllers on the |
| 38 | * bus. We use the x86 real mode emulator to run the BIOS on |
| 39 | * graphics controllers to bring the cards up. |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 40 | * |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 41 | * Note that at present this module does *not* support |
| 42 | * multiple controllers. |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 43 | * |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 44 | * The orignal name of this file is warmboot.c. |
| 45 | * Jason ported this file to u-boot to run the ATI video card |
| 46 | * BIOS in u-boot. |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 47 | ****************************************************************************/ |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 48 | #include <compiler.h> |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 49 | #include <bios_emul.h> |
| 50 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 51 | #include <log.h> |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 52 | #include <malloc.h> |
Simon Glass | ec86bc6 | 2022-07-30 15:52:04 -0600 | [diff] [blame] | 53 | #include <vesa.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 54 | #include <linux/delay.h> |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 55 | #include "biosemui.h" |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 56 | |
| 57 | /* Length of the BIOS image */ |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 58 | #define MAX_BIOSLEN (128 * 1024L) |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 59 | |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 60 | /* Place to save PCI BAR's that we change and later restore */ |
| 61 | static u32 saveROMBaseAddress; |
| 62 | static u32 saveBaseAddress10; |
| 63 | static u32 saveBaseAddress14; |
| 64 | static u32 saveBaseAddress18; |
| 65 | static u32 saveBaseAddress20; |
| 66 | |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 67 | /* Addres im memory of VBE region */ |
| 68 | const int vbe_offset = 0x2000; |
| 69 | |
Bin Meng | c9dba41 | 2018-04-11 22:02:15 -0700 | [diff] [blame] | 70 | #ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 71 | static const void *bios_ptr(const void *buf, BE_VGAInfo *vga_info, |
| 72 | u32 x86_dword_ptr) |
| 73 | { |
| 74 | u32 seg_ofs, flat; |
| 75 | |
| 76 | seg_ofs = le32_to_cpu(x86_dword_ptr); |
| 77 | flat = ((seg_ofs & 0xffff0000) >> 12) | (seg_ofs & 0xffff); |
| 78 | if (flat >= 0xc0000) |
| 79 | return vga_info->BIOSImage + flat - 0xc0000; |
| 80 | else |
| 81 | return buf + (flat - vbe_offset); |
| 82 | } |
| 83 | |
| 84 | static int atibios_debug_mode(BE_VGAInfo *vga_info, RMREGS *regs, |
Simon Glass | 5b92520 | 2022-07-30 15:52:05 -0600 | [diff] [blame] | 85 | int vesa_mode, struct vesa_state *mode_info) |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 86 | { |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 87 | void *buffer = (void *)(M.mem_base + vbe_offset); |
| 88 | u16 buffer_seg = (((unsigned long)vbe_offset) >> 4) & 0xff00; |
| 89 | u16 buffer_adr = ((unsigned long)vbe_offset) & 0xffff; |
| 90 | struct vesa_mode_info *vm; |
Simon Glass | 5b92520 | 2022-07-30 15:52:05 -0600 | [diff] [blame] | 91 | struct vesa_bios_ext_info *info; |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 92 | const u16 *modes_bios, *ptr; |
| 93 | u16 *modes; |
| 94 | int size; |
| 95 | |
| 96 | debug("VBE: Getting information\n"); |
| 97 | regs->e.eax = VESA_GET_INFO; |
| 98 | regs->e.esi = buffer_seg; |
| 99 | regs->e.edi = buffer_adr; |
| 100 | info = buffer; |
| 101 | memset(info, '\0', sizeof(*info)); |
| 102 | strcpy(info->signature, "VBE2"); |
| 103 | BE_int86(0x10, regs, regs); |
| 104 | if (regs->e.eax != 0x4f) { |
| 105 | debug("VESA_GET_INFO: error %x\n", regs->e.eax); |
| 106 | return -ENOSYS; |
| 107 | } |
| 108 | debug("version %x\n", le16_to_cpu(info->version)); |
| 109 | debug("oem '%s'\n", (char *)bios_ptr(buffer, vga_info, |
| 110 | info->oem_string_ptr)); |
| 111 | debug("vendor '%s'\n", (char *)bios_ptr(buffer, vga_info, |
| 112 | info->vendor_name_ptr)); |
| 113 | debug("product '%s'\n", (char *)bios_ptr(buffer, vga_info, |
| 114 | info->product_name_ptr)); |
| 115 | debug("rev '%s'\n", (char *)bios_ptr(buffer, vga_info, |
| 116 | info->product_rev_ptr)); |
| 117 | modes_bios = bios_ptr(buffer, vga_info, info->modes_ptr); |
| 118 | debug("Modes: "); |
| 119 | for (ptr = modes_bios; *ptr != 0xffff; ptr++) |
| 120 | debug("%x ", le16_to_cpu(*ptr)); |
| 121 | debug("\nmemory %dMB\n", le16_to_cpu(info->total_memory) >> 4); |
| 122 | size = (ptr - modes_bios) * sizeof(u16) + 2; |
| 123 | modes = malloc(size); |
| 124 | if (!modes) |
| 125 | return -ENOMEM; |
| 126 | memcpy(modes, modes_bios, size); |
| 127 | |
| 128 | regs->e.eax = VESA_GET_CUR_MODE; |
| 129 | BE_int86(0x10, regs, regs); |
| 130 | if (regs->e.eax != 0x4f) { |
| 131 | debug("VESA_GET_CUR_MODE: error %x\n", regs->e.eax); |
| 132 | return -ENOSYS; |
| 133 | } |
| 134 | debug("Current mode %x\n", regs->e.ebx); |
| 135 | |
| 136 | for (ptr = modes; *ptr != 0xffff; ptr++) { |
| 137 | int mode = le16_to_cpu(*ptr); |
| 138 | bool linear_ok; |
| 139 | int attr; |
| 140 | |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 141 | debug("Mode %x: ", mode); |
Simon Glass | 5b92520 | 2022-07-30 15:52:05 -0600 | [diff] [blame] | 142 | memset(buffer, '\0', sizeof(struct vesa_state)); |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 143 | regs->e.eax = VESA_GET_MODE_INFO; |
| 144 | regs->e.ebx = 0; |
| 145 | regs->e.ecx = mode; |
| 146 | regs->e.edx = 0; |
| 147 | regs->e.esi = buffer_seg; |
| 148 | regs->e.edi = buffer_adr; |
| 149 | BE_int86(0x10, regs, regs); |
| 150 | if (regs->e.eax != 0x4f) { |
| 151 | debug("VESA_GET_MODE_INFO: error %x\n", regs->e.eax); |
| 152 | continue; |
| 153 | } |
| 154 | memcpy(mode_info->mode_info_block, buffer, |
| 155 | sizeof(struct vesa_mode_info)); |
| 156 | mode_info->valid = true; |
| 157 | vm = &mode_info->vesa; |
| 158 | attr = le16_to_cpu(vm->mode_attributes); |
| 159 | linear_ok = attr & 0x80; |
| 160 | debug("res %d x %d, %d bpp, mm %d, (Linear %s, attr %02x)\n", |
| 161 | le16_to_cpu(vm->x_resolution), |
| 162 | le16_to_cpu(vm->y_resolution), |
| 163 | vm->bits_per_pixel, vm->memory_model, |
| 164 | linear_ok ? "OK" : "not available", |
| 165 | attr); |
| 166 | debug("\tRGB pos=%d,%d,%d, size=%d,%d,%d\n", |
| 167 | vm->red_mask_pos, vm->green_mask_pos, vm->blue_mask_pos, |
| 168 | vm->red_mask_size, vm->green_mask_size, |
| 169 | vm->blue_mask_size); |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int atibios_set_vesa_mode(RMREGS *regs, int vesa_mode, |
Simon Glass | 5b92520 | 2022-07-30 15:52:05 -0600 | [diff] [blame] | 176 | struct vesa_state *mode_info) |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 177 | { |
| 178 | void *buffer = (void *)(M.mem_base + vbe_offset); |
| 179 | u16 buffer_seg = (((unsigned long)vbe_offset) >> 4) & 0xff00; |
| 180 | u16 buffer_adr = ((unsigned long)vbe_offset) & 0xffff; |
| 181 | struct vesa_mode_info *vm; |
| 182 | |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 183 | debug("VBE: Setting VESA mode %#04x\n", vesa_mode); |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 184 | regs->e.eax = VESA_SET_MODE; |
| 185 | regs->e.ebx = vesa_mode; |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 186 | /* request linear framebuffer mode and don't clear display */ |
| 187 | regs->e.ebx |= (1 << 14) | (1 << 15); |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 188 | BE_int86(0x10, regs, regs); |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 189 | if (regs->e.eax != 0x4f) { |
| 190 | debug("VESA_SET_MODE: error %x\n", regs->e.eax); |
| 191 | return -ENOSYS; |
| 192 | } |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 193 | |
Simon Glass | 5b92520 | 2022-07-30 15:52:05 -0600 | [diff] [blame] | 194 | memset(buffer, '\0', sizeof(struct vesa_state)); |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 195 | debug("VBE: Geting info for VESA mode %#04x\n", vesa_mode); |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 196 | regs->e.eax = VESA_GET_MODE_INFO; |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 197 | regs->e.ecx = vesa_mode; |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 198 | regs->e.esi = buffer_seg; |
| 199 | regs->e.edi = buffer_adr; |
| 200 | BE_int86(0x10, regs, regs); |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 201 | if (regs->e.eax != 0x4f) { |
| 202 | debug("VESA_GET_MODE_INFO: error %x\n", regs->e.eax); |
| 203 | return -ENOSYS; |
| 204 | } |
| 205 | |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 206 | memcpy(mode_info->mode_info_block, buffer, |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 207 | sizeof(struct vesa_mode_info)); |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 208 | mode_info->valid = true; |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 209 | mode_info->video_mode = vesa_mode; |
| 210 | vm = &mode_info->vesa; |
| 211 | vm->x_resolution = le16_to_cpu(vm->x_resolution); |
| 212 | vm->y_resolution = le16_to_cpu(vm->y_resolution); |
| 213 | vm->bytes_per_scanline = le16_to_cpu(vm->bytes_per_scanline); |
| 214 | vm->phys_base_ptr = le32_to_cpu(vm->phys_base_ptr); |
| 215 | vm->mode_attributes = le16_to_cpu(vm->mode_attributes); |
| 216 | debug("VBE: Init complete\n"); |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 217 | |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 218 | return 0; |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 219 | } |
Bin Meng | c9dba41 | 2018-04-11 22:02:15 -0700 | [diff] [blame] | 220 | #endif /* CONFIG_FRAMEBUFFER_SET_VESA_MODE */ |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 221 | |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 222 | /**************************************************************************** |
| 223 | PARAMETERS: |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 224 | pcidev - PCI device info for the video card on the bus to boot |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 225 | vga_info - BIOS emulator VGA info structure |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 226 | |
| 227 | REMARKS: |
| 228 | This function executes the BIOS POST code on the controller. We assume that |
| 229 | at this stage the controller has its I/O and memory space enabled and |
| 230 | that all other controllers are in a disabled state. |
| 231 | ****************************************************************************/ |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 232 | static void PCI_doBIOSPOST(struct udevice *pcidev, BE_VGAInfo *vga_info, |
Simon Glass | 5b92520 | 2022-07-30 15:52:05 -0600 | [diff] [blame] | 233 | int vesa_mode, struct vesa_state *mode_info) |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 234 | { |
| 235 | RMREGS regs; |
| 236 | RMSREGS sregs; |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 237 | pci_dev_t bdf; |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 238 | |
| 239 | /* Determine the value to store in AX for BIOS POST. Per the PCI specs, |
| 240 | AH must contain the bus and AL must contain the devfn, encoded as |
| 241 | (dev << 3) | fn |
| 242 | */ |
| 243 | memset(®s, 0, sizeof(regs)); |
| 244 | memset(&sregs, 0, sizeof(sregs)); |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 245 | bdf = dm_pci_get_bdf(pcidev); |
| 246 | regs.x.ax = (int)PCI_BUS(bdf) << 8 | |
| 247 | (int)PCI_DEV(bdf) << 3 | (int)PCI_FUNC(bdf); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 248 | /*Setup the X86 emulator for the VGA BIOS*/ |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 249 | BE_setVGA(vga_info); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 250 | |
| 251 | /*Execute the BIOS POST code*/ |
| 252 | BE_callRealMode(0xC000, 0x0003, ®s, &sregs); |
| 253 | |
| 254 | /*Cleanup and exit*/ |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 255 | BE_getVGA(vga_info); |
| 256 | |
Bin Meng | c9dba41 | 2018-04-11 22:02:15 -0700 | [diff] [blame] | 257 | #ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE |
Simon Glass | c55b1c6 | 2014-12-29 19:32:26 -0700 | [diff] [blame] | 258 | /* Useful for debugging */ |
| 259 | if (0) |
| 260 | atibios_debug_mode(vga_info, ®s, vesa_mode, mode_info); |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 261 | if (vesa_mode != -1) |
| 262 | atibios_set_vesa_mode(®s, vesa_mode, mode_info); |
Bin Meng | c9dba41 | 2018-04-11 22:02:15 -0700 | [diff] [blame] | 263 | #endif |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /**************************************************************************** |
| 267 | PARAMETERS: |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 268 | pcidev - PCI device info for the video card on the bus |
| 269 | bar - Place to return the base address register offset to use |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 270 | |
| 271 | RETURNS: |
| 272 | The address to use to map the secondary BIOS (AGP devices) |
| 273 | |
| 274 | REMARKS: |
| 275 | Searches all the PCI base address registers for the device looking for a |
| 276 | memory mapping that is large enough to hold our ROM BIOS. We usually end up |
| 277 | finding the framebuffer mapping (usually BAR 0x10), and we use this mapping |
| 278 | to map the BIOS for the device into. We use a mapping that is already |
| 279 | assigned to the device to ensure the memory range will be passed through |
| 280 | by any PCI->PCI or AGP->PCI bridge that may be present. |
| 281 | |
| 282 | NOTE: Usually this function is only used for AGP devices, but it may be |
| 283 | used for PCI devices that have already been POST'ed and the BIOS |
| 284 | ROM base address has been zero'ed out. |
| 285 | |
| 286 | NOTE: This function leaves the original memory aperture disabled by leaving |
| 287 | it programmed to all 1's. It must be restored to the correct value |
| 288 | later. |
| 289 | ****************************************************************************/ |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 290 | static u32 PCI_findBIOSAddr(struct udevice *pcidev, int *bar) |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 291 | { |
| 292 | u32 base, size; |
| 293 | |
| 294 | for (*bar = 0x10; *bar <= 0x14; (*bar) += 4) { |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 295 | dm_pci_read_config32(pcidev, *bar, &base); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 296 | if (!(base & 0x1)) { |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 297 | dm_pci_write_config32(pcidev, *bar, 0xFFFFFFFF); |
| 298 | dm_pci_read_config32(pcidev, *bar, &size); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 299 | size = ~(size & ~0xFF) + 1; |
| 300 | if (size >= MAX_BIOSLEN) |
| 301 | return base & ~0xFF; |
| 302 | } |
| 303 | } |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | /**************************************************************************** |
| 308 | REMARKS: |
| 309 | Some non-x86 Linux kernels map PCI relocateable I/O to values that |
| 310 | are above 64K, which will not work with the BIOS image that requires |
| 311 | the offset for the I/O ports to be a maximum of 16-bits. Ideally |
| 312 | someone should fix the kernel to map the I/O ports for VGA compatible |
| 313 | devices to a different location (or just all I/O ports since it is |
| 314 | unlikely you can have enough devices in the machine to use up all |
| 315 | 64K of the I/O space - a total of more than 256 cards would be |
| 316 | necessary). |
| 317 | |
| 318 | Anyway to fix this we change all I/O mapped base registers and |
| 319 | chop off the top bits. |
| 320 | ****************************************************************************/ |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 321 | static void PCI_fixupIObase(struct udevice *pcidev, int reg, u32 *base) |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 322 | { |
| 323 | if ((*base & 0x1) && (*base > 0xFFFE)) { |
| 324 | *base &= 0xFFFF; |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 325 | dm_pci_write_config32(pcidev, reg, *base); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 326 | |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /**************************************************************************** |
| 331 | PARAMETERS: |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 332 | pcidev - PCI device info for the video card on the bus |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 333 | |
| 334 | RETURNS: |
| 335 | Pointers to the mapped BIOS image |
| 336 | |
| 337 | REMARKS: |
| 338 | Maps a pointer to the BIOS image on the graphics card on the PCI bus. |
| 339 | ****************************************************************************/ |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 340 | void *PCI_mapBIOSImage(struct udevice *pcidev) |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 341 | { |
Ed Swarthout | 224dad2 | 2010-03-31 15:52:40 -0500 | [diff] [blame] | 342 | u32 BIOSImageBus; |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 343 | int BIOSImageBAR; |
| 344 | u8 *BIOSImage; |
| 345 | |
| 346 | /*Save PCI BAR registers that might get changed*/ |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 347 | dm_pci_read_config32(pcidev, PCI_ROM_ADDRESS, &saveROMBaseAddress); |
| 348 | dm_pci_read_config32(pcidev, PCI_BASE_ADDRESS_0, &saveBaseAddress10); |
| 349 | dm_pci_read_config32(pcidev, PCI_BASE_ADDRESS_1, &saveBaseAddress14); |
| 350 | dm_pci_read_config32(pcidev, PCI_BASE_ADDRESS_2, &saveBaseAddress18); |
| 351 | dm_pci_read_config32(pcidev, PCI_BASE_ADDRESS_4, &saveBaseAddress20); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 352 | |
| 353 | /*Fix up I/O base registers to less than 64K */ |
| 354 | if(saveBaseAddress14 != 0) |
| 355 | PCI_fixupIObase(pcidev, PCI_BASE_ADDRESS_1, &saveBaseAddress14); |
| 356 | else |
| 357 | PCI_fixupIObase(pcidev, PCI_BASE_ADDRESS_4, &saveBaseAddress20); |
| 358 | |
| 359 | /* Some cards have problems that stop us from being able to read the |
| 360 | BIOS image from the ROM BAR. To fix this we have to do some chipset |
| 361 | specific programming for different cards to solve this problem. |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 362 | */ |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 363 | |
Ed Swarthout | 224dad2 | 2010-03-31 15:52:40 -0500 | [diff] [blame] | 364 | BIOSImageBus = PCI_findBIOSAddr(pcidev, &BIOSImageBAR); |
| 365 | if (BIOSImageBus == 0) { |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 366 | printf("Find bios addr error\n"); |
| 367 | return NULL; |
| 368 | } |
| 369 | |
Andrew Scull | 3e0f2fb | 2022-04-21 16:11:12 +0000 | [diff] [blame] | 370 | BIOSImage = dm_pci_bus_to_virt(pcidev, BIOSImageBus, 0, PCI_REGION_TYPE, |
| 371 | PCI_REGION_MEM, MAP_NOCACHE); |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 372 | |
| 373 | /*Change the PCI BAR registers to map it onto the bus.*/ |
| 374 | dm_pci_write_config32(pcidev, BIOSImageBAR, 0); |
| 375 | dm_pci_write_config32(pcidev, PCI_ROM_ADDRESS, BIOSImageBus | 0x1); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 376 | udelay(1); |
| 377 | |
| 378 | /*Check that the BIOS image is valid. If not fail, or return the |
| 379 | compiled in BIOS image if that option was enabled |
| 380 | */ |
| 381 | if (BIOSImage[0] != 0x55 || BIOSImage[1] != 0xAA || BIOSImage[2] == 0) { |
| 382 | return NULL; |
| 383 | } |
| 384 | |
| 385 | return BIOSImage; |
| 386 | } |
| 387 | |
| 388 | /**************************************************************************** |
| 389 | PARAMETERS: |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 390 | pcidev - PCI device info for the video card on the bus |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 391 | |
| 392 | REMARKS: |
| 393 | Unmaps the BIOS image for the device and restores framebuffer mappings |
| 394 | ****************************************************************************/ |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 395 | void PCI_unmapBIOSImage(struct udevice *pcidev, void *BIOSImage) |
| 396 | { |
| 397 | dm_pci_write_config32(pcidev, PCI_ROM_ADDRESS, saveROMBaseAddress); |
| 398 | dm_pci_write_config32(pcidev, PCI_BASE_ADDRESS_0, saveBaseAddress10); |
| 399 | dm_pci_write_config32(pcidev, PCI_BASE_ADDRESS_1, saveBaseAddress14); |
| 400 | dm_pci_write_config32(pcidev, PCI_BASE_ADDRESS_2, saveBaseAddress18); |
| 401 | dm_pci_write_config32(pcidev, PCI_BASE_ADDRESS_4, saveBaseAddress20); |
| 402 | } |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 403 | |
| 404 | /**************************************************************************** |
| 405 | PARAMETERS: |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 406 | pcidev - PCI device info for the video card on the bus to boot |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 407 | VGAInfo - BIOS emulator VGA info structure |
| 408 | |
| 409 | RETURNS: |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 410 | true if successfully initialised, false if not. |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 411 | |
| 412 | REMARKS: |
| 413 | Loads and POST's the display controllers BIOS, directly from the BIOS |
| 414 | image we can extract over the PCI bus. |
| 415 | ****************************************************************************/ |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 416 | static int PCI_postController(struct udevice *pcidev, uchar *bios_rom, |
| 417 | int bios_len, BE_VGAInfo *vga_info, |
Simon Glass | 5b92520 | 2022-07-30 15:52:05 -0600 | [diff] [blame] | 418 | int vesa_mode, struct vesa_state *mode_info) |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 419 | { |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 420 | u32 bios_image_len; |
| 421 | uchar *mapped_bios; |
| 422 | uchar *copy_of_bios; |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 423 | pci_dev_t bdf; |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 424 | |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 425 | if (bios_rom) { |
| 426 | copy_of_bios = bios_rom; |
| 427 | bios_image_len = bios_len; |
| 428 | } else { |
| 429 | /* |
| 430 | * Allocate memory to store copy of BIOS from display |
| 431 | * controller |
| 432 | */ |
| 433 | mapped_bios = PCI_mapBIOSImage(pcidev); |
| 434 | if (mapped_bios == NULL) { |
| 435 | printf("videoboot: Video ROM failed to map!\n"); |
| 436 | return false; |
| 437 | } |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 438 | |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 439 | bios_image_len = mapped_bios[2] * 512; |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 440 | |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 441 | copy_of_bios = malloc(bios_image_len); |
| 442 | if (copy_of_bios == NULL) { |
| 443 | printf("videoboot: Out of memory!\n"); |
| 444 | return false; |
| 445 | } |
| 446 | memcpy(copy_of_bios, mapped_bios, bios_image_len); |
| 447 | PCI_unmapBIOSImage(pcidev, mapped_bios); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 448 | } |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 449 | |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 450 | /*Save information in vga_info structure*/ |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 451 | bdf = dm_pci_get_bdf(pcidev); |
| 452 | vga_info->function = PCI_FUNC(bdf); |
| 453 | vga_info->device = PCI_DEV(bdf); |
| 454 | vga_info->bus = PCI_BUS(bdf); |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 455 | vga_info->pcidev = pcidev; |
| 456 | vga_info->BIOSImage = copy_of_bios; |
| 457 | vga_info->BIOSImageLen = bios_image_len; |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 458 | |
| 459 | /*Now execute the BIOS POST for the device*/ |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 460 | if (copy_of_bios[0] != 0x55 || copy_of_bios[1] != 0xAA) { |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 461 | printf("videoboot: Video ROM image is invalid!\n"); |
| 462 | return false; |
| 463 | } |
| 464 | |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 465 | PCI_doBIOSPOST(pcidev, vga_info, vesa_mode, mode_info); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 466 | |
| 467 | /*Reset the size of the BIOS image to the final size*/ |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 468 | vga_info->BIOSImageLen = copy_of_bios[2] * 512; |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 469 | return true; |
| 470 | } |
| 471 | |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 472 | int biosemu_setup(struct udevice *pcidev, BE_VGAInfo **vga_infop) |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 473 | { |
| 474 | BE_VGAInfo *VGAInfo; |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 475 | pci_dev_t bdf = dm_pci_get_bdf(pcidev); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 476 | |
| 477 | printf("videoboot: Booting PCI video card bus %d, function %d, device %d\n", |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 478 | PCI_BUS(bdf), PCI_FUNC(bdf), PCI_DEV(bdf)); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 479 | /*Initialise the x86 BIOS emulator*/ |
| 480 | if ((VGAInfo = malloc(sizeof(*VGAInfo))) == NULL) { |
| 481 | printf("videoboot: Out of memory!\n"); |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 482 | return -ENOMEM; |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 483 | } |
| 484 | memset(VGAInfo, 0, sizeof(*VGAInfo)); |
| 485 | BE_init(0, 65536, VGAInfo, 0); |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 486 | *vga_infop = VGAInfo; |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 487 | |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | void biosemu_set_interrupt_handler(int intnum, int (*int_func)(void)) |
| 492 | { |
| 493 | X86EMU_setupIntrFunc(intnum, (X86EMU_intrFuncs)int_func); |
| 494 | } |
| 495 | |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 496 | int biosemu_run(struct udevice *pcidev, uchar *bios_rom, int bios_len, |
| 497 | BE_VGAInfo *vga_info, int clean_up, int vesa_mode, |
Simon Glass | 5b92520 | 2022-07-30 15:52:05 -0600 | [diff] [blame] | 498 | struct vesa_state *mode_info) |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 499 | { |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 500 | /*Post all the display controller BIOS'es*/ |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 501 | if (!PCI_postController(pcidev, bios_rom, bios_len, vga_info, |
| 502 | vesa_mode, mode_info)) |
| 503 | return -EINVAL; |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 504 | |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 505 | /* |
| 506 | * Cleanup and exit the emulator if requested. If the BIOS emulator |
| 507 | * is needed after booting the card, we will not call BE_exit and |
| 508 | * leave it enabled for further use (ie: VESA driver etc). |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 509 | */ |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 510 | if (clean_up) { |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 511 | BE_exit(); |
Bin Meng | a264c90 | 2015-04-24 15:48:05 +0800 | [diff] [blame] | 512 | if (vga_info->BIOSImage && |
Simon Glass | cc40906 | 2016-09-25 21:33:06 -0600 | [diff] [blame] | 513 | (ulong)(vga_info->BIOSImage) != 0xc0000) |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 514 | free(vga_info->BIOSImage); |
| 515 | free(vga_info); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 516 | } |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | /**************************************************************************** |
| 522 | PARAMETERS: |
| 523 | pcidev - PCI device info for the video card on the bus to boot |
| 524 | pVGAInfo - Place to return VGA info structure is requested |
| 525 | cleanUp - true to clean up on exit, false to leave emulator active |
| 526 | |
| 527 | REMARKS: |
| 528 | Boots the PCI/AGP video card on the bus using the Video ROM BIOS image |
| 529 | and the X86 BIOS emulator module. |
| 530 | ****************************************************************************/ |
Simon Glass | d3e0c8f | 2016-01-17 16:11:09 -0700 | [diff] [blame] | 531 | int BootVideoCardBIOS(struct udevice *pcidev, BE_VGAInfo **pVGAInfo, |
| 532 | int clean_up) |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 533 | { |
| 534 | BE_VGAInfo *VGAInfo; |
| 535 | int ret; |
| 536 | |
| 537 | ret = biosemu_setup(pcidev, &VGAInfo); |
| 538 | if (ret) |
| 539 | return false; |
| 540 | ret = biosemu_run(pcidev, NULL, 0, VGAInfo, clean_up, -1, NULL); |
| 541 | if (ret) |
| 542 | return false; |
| 543 | |
| 544 | /* Return VGA info pointer if the caller requested it*/ |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 545 | if (pVGAInfo) |
| 546 | *pVGAInfo = VGAInfo; |
Simon Glass | acda1cc | 2014-11-14 20:56:40 -0700 | [diff] [blame] | 547 | |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 548 | return true; |
| 549 | } |