16854f87cSSimon Glass /* 26854f87cSSimon Glass * Copyright (C) 2014 Google, Inc 36854f87cSSimon Glass * 46854f87cSSimon Glass * From coreboot, originally based on the Linux kernel (drivers/pci/pci.c). 56854f87cSSimon Glass * 66854f87cSSimon Glass * Modifications are: 76854f87cSSimon Glass * Copyright (C) 2003-2004 Linux Networx 86854f87cSSimon Glass * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx) 96854f87cSSimon Glass * Copyright (C) 2003-2006 Ronald G. Minnich <rminnich@gmail.com> 106854f87cSSimon Glass * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov> 116854f87cSSimon Glass * Copyright (C) 2005-2006 Tyan 126854f87cSSimon Glass * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan) 136854f87cSSimon Glass * Copyright (C) 2005-2009 coresystems GmbH 146854f87cSSimon Glass * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH) 156854f87cSSimon Glass * 166854f87cSSimon Glass * PCI Bus Services, see include/linux/pci.h for further explanation. 176854f87cSSimon Glass * 186854f87cSSimon Glass * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter, 196854f87cSSimon Glass * David Mosberger-Tang 206854f87cSSimon Glass * 216854f87cSSimon Glass * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz> 226854f87cSSimon Glass 236854f87cSSimon Glass * SPDX-License-Identifier: GPL-2.0 246854f87cSSimon Glass */ 256854f87cSSimon Glass 266854f87cSSimon Glass #include <common.h> 276854f87cSSimon Glass #include <bios_emul.h> 286854f87cSSimon Glass #include <errno.h> 296854f87cSSimon Glass #include <malloc.h> 306854f87cSSimon Glass #include <pci.h> 316854f87cSSimon Glass #include <pci_rom.h> 326854f87cSSimon Glass #include <vbe.h> 336854f87cSSimon Glass #include <video_fb.h> 346854f87cSSimon Glass 356854f87cSSimon Glass #ifdef CONFIG_HAVE_ACPI_RESUME 366854f87cSSimon Glass #include <asm/acpi.h> 376854f87cSSimon Glass #endif 386854f87cSSimon Glass 396854f87cSSimon Glass __weak bool board_should_run_oprom(pci_dev_t dev) 406854f87cSSimon Glass { 416854f87cSSimon Glass return true; 426854f87cSSimon Glass } 436854f87cSSimon Glass 446854f87cSSimon Glass static bool should_load_oprom(pci_dev_t dev) 456854f87cSSimon Glass { 466854f87cSSimon Glass #ifdef CONFIG_HAVE_ACPI_RESUME 476854f87cSSimon Glass if (acpi_get_slp_type() == 3) 486854f87cSSimon Glass return false; 496854f87cSSimon Glass #endif 506854f87cSSimon Glass if (IS_ENABLED(CONFIG_ALWAYS_LOAD_OPROM)) 516854f87cSSimon Glass return 1; 526854f87cSSimon Glass if (board_should_run_oprom(dev)) 536854f87cSSimon Glass return 1; 546854f87cSSimon Glass 556854f87cSSimon Glass return 0; 566854f87cSSimon Glass } 576854f87cSSimon Glass 586854f87cSSimon Glass __weak uint32_t board_map_oprom_vendev(uint32_t vendev) 596854f87cSSimon Glass { 606854f87cSSimon Glass return vendev; 616854f87cSSimon Glass } 626854f87cSSimon Glass 636854f87cSSimon Glass static int pci_rom_probe(pci_dev_t dev, uint class, 646854f87cSSimon Glass struct pci_rom_header **hdrp) 656854f87cSSimon Glass { 666854f87cSSimon Glass struct pci_rom_header *rom_header; 676854f87cSSimon Glass struct pci_rom_data *rom_data; 686854f87cSSimon Glass u16 vendor, device; 6940305240SSimon Glass u16 rom_vendor, rom_device; 706854f87cSSimon Glass u32 vendev; 716854f87cSSimon Glass u32 mapped_vendev; 726854f87cSSimon Glass u32 rom_address; 736854f87cSSimon Glass 746854f87cSSimon Glass pci_read_config_word(dev, PCI_VENDOR_ID, &vendor); 756854f87cSSimon Glass pci_read_config_word(dev, PCI_DEVICE_ID, &device); 766854f87cSSimon Glass vendev = vendor << 16 | device; 776854f87cSSimon Glass mapped_vendev = board_map_oprom_vendev(vendev); 786854f87cSSimon Glass if (vendev != mapped_vendev) 796854f87cSSimon Glass debug("Device ID mapped to %#08x\n", mapped_vendev); 806854f87cSSimon Glass 816854f87cSSimon Glass #ifdef CONFIG_X86_OPTION_ROM_ADDR 826854f87cSSimon Glass rom_address = CONFIG_X86_OPTION_ROM_ADDR; 836854f87cSSimon Glass #else 84*4a2708a0SSimon Glass 85*4a2708a0SSimon Glass if (pciauto_setup_rom(pci_bus_to_hose(PCI_BUS(dev)), dev)) { 86*4a2708a0SSimon Glass debug("Cannot find option ROM\n"); 87*4a2708a0SSimon Glass return -ENOENT; 88*4a2708a0SSimon Glass } 89*4a2708a0SSimon Glass 906854f87cSSimon Glass pci_read_config_dword(dev, PCI_ROM_ADDRESS, &rom_address); 916854f87cSSimon Glass if (rom_address == 0x00000000 || rom_address == 0xffffffff) { 926854f87cSSimon Glass debug("%s: rom_address=%x\n", __func__, rom_address); 936854f87cSSimon Glass return -ENOENT; 946854f87cSSimon Glass } 956854f87cSSimon Glass 966854f87cSSimon Glass /* Enable expansion ROM address decoding. */ 976854f87cSSimon Glass pci_write_config_dword(dev, PCI_ROM_ADDRESS, 986854f87cSSimon Glass rom_address | PCI_ROM_ADDRESS_ENABLE); 996854f87cSSimon Glass #endif 1006854f87cSSimon Glass debug("Option ROM address %x\n", rom_address); 1016854f87cSSimon Glass rom_header = (struct pci_rom_header *)rom_address; 1026854f87cSSimon Glass 1036854f87cSSimon Glass debug("PCI expansion ROM, signature %#04x, INIT size %#04x, data ptr %#04x\n", 10440305240SSimon Glass le16_to_cpu(rom_header->signature), 10540305240SSimon Glass rom_header->size * 512, le16_to_cpu(rom_header->data)); 1066854f87cSSimon Glass 10740305240SSimon Glass if (le16_to_cpu(rom_header->signature) != PCI_ROM_HDR) { 1086854f87cSSimon Glass printf("Incorrect expansion ROM header signature %04x\n", 10940305240SSimon Glass le16_to_cpu(rom_header->signature)); 1106854f87cSSimon Glass return -EINVAL; 1116854f87cSSimon Glass } 1126854f87cSSimon Glass 11340305240SSimon Glass rom_data = (((void *)rom_header) + le16_to_cpu(rom_header->data)); 11440305240SSimon Glass rom_vendor = le16_to_cpu(rom_data->vendor); 11540305240SSimon Glass rom_device = le16_to_cpu(rom_data->device); 1166854f87cSSimon Glass 1176854f87cSSimon Glass debug("PCI ROM image, vendor ID %04x, device ID %04x,\n", 11840305240SSimon Glass rom_vendor, rom_device); 1196854f87cSSimon Glass 1206854f87cSSimon Glass /* If the device id is mapped, a mismatch is expected */ 12140305240SSimon Glass if ((vendor != rom_vendor || device != rom_device) && 1226854f87cSSimon Glass (vendev == mapped_vendev)) { 1236854f87cSSimon Glass printf("ID mismatch: vendor ID %04x, device ID %04x\n", 12440305240SSimon Glass rom_vendor, rom_device); 125c5caba03SSimon Glass /* Continue anyway */ 1266854f87cSSimon Glass } 1276854f87cSSimon Glass 1286854f87cSSimon Glass debug("PCI ROM image, Class Code %04x%02x, Code Type %02x\n", 1296854f87cSSimon Glass rom_data->class_hi, rom_data->class_lo, rom_data->type); 1306854f87cSSimon Glass 1316854f87cSSimon Glass if (class != ((rom_data->class_hi << 8) | rom_data->class_lo)) { 1326854f87cSSimon Glass debug("Class Code mismatch ROM %08x, dev %08x\n", 1336854f87cSSimon Glass (rom_data->class_hi << 8) | rom_data->class_lo, 1346854f87cSSimon Glass class); 1356854f87cSSimon Glass } 1366854f87cSSimon Glass *hdrp = rom_header; 1376854f87cSSimon Glass 1386854f87cSSimon Glass return 0; 1396854f87cSSimon Glass } 1406854f87cSSimon Glass 1416854f87cSSimon Glass int pci_rom_load(uint16_t class, struct pci_rom_header *rom_header, 1426854f87cSSimon Glass struct pci_rom_header **ram_headerp) 1436854f87cSSimon Glass { 1446854f87cSSimon Glass struct pci_rom_data *rom_data; 1456854f87cSSimon Glass unsigned int rom_size; 1466854f87cSSimon Glass unsigned int image_size = 0; 1476854f87cSSimon Glass void *target; 1486854f87cSSimon Glass 1496854f87cSSimon Glass do { 1506854f87cSSimon Glass /* Get next image, until we see an x86 version */ 1516854f87cSSimon Glass rom_header = (struct pci_rom_header *)((void *)rom_header + 1526854f87cSSimon Glass image_size); 1536854f87cSSimon Glass 1546854f87cSSimon Glass rom_data = (struct pci_rom_data *)((void *)rom_header + 15540305240SSimon Glass le16_to_cpu(rom_header->data)); 1566854f87cSSimon Glass 15740305240SSimon Glass image_size = le16_to_cpu(rom_data->ilen) * 512; 15840305240SSimon Glass } while ((rom_data->type != 0) && (rom_data->indicator == 0)); 1596854f87cSSimon Glass 1606854f87cSSimon Glass if (rom_data->type != 0) 1616854f87cSSimon Glass return -EACCES; 1626854f87cSSimon Glass 1636854f87cSSimon Glass rom_size = rom_header->size * 512; 1646854f87cSSimon Glass 165bdc88d4eSSimon Glass #ifdef PCI_VGA_RAM_IMAGE_START 1666854f87cSSimon Glass target = (void *)PCI_VGA_RAM_IMAGE_START; 167bdc88d4eSSimon Glass #else 168bdc88d4eSSimon Glass target = (void *)malloc(rom_size); 169bdc88d4eSSimon Glass if (!target) 170bdc88d4eSSimon Glass return -ENOMEM; 171bdc88d4eSSimon Glass #endif 1726854f87cSSimon Glass if (target != rom_header) { 173fba7eac1SSimon Glass ulong start = get_timer(0); 174fba7eac1SSimon Glass 1756854f87cSSimon Glass debug("Copying VGA ROM Image from %p to %p, 0x%x bytes\n", 1766854f87cSSimon Glass rom_header, target, rom_size); 1776854f87cSSimon Glass memcpy(target, rom_header, rom_size); 1786854f87cSSimon Glass if (memcmp(target, rom_header, rom_size)) { 1796854f87cSSimon Glass printf("VGA ROM copy failed\n"); 1806854f87cSSimon Glass return -EFAULT; 1816854f87cSSimon Glass } 182fba7eac1SSimon Glass debug("Copy took %lums\n", get_timer(start)); 1836854f87cSSimon Glass } 1846854f87cSSimon Glass *ram_headerp = target; 1856854f87cSSimon Glass 1866854f87cSSimon Glass return 0; 1876854f87cSSimon Glass } 1886854f87cSSimon Glass 1896854f87cSSimon Glass static struct vbe_mode_info mode_info; 1906854f87cSSimon Glass 1916854f87cSSimon Glass int vbe_get_video_info(struct graphic_device *gdev) 1926854f87cSSimon Glass { 1936854f87cSSimon Glass #ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE 1946854f87cSSimon Glass struct vesa_mode_info *vesa = &mode_info.vesa; 1956854f87cSSimon Glass 1966854f87cSSimon Glass gdev->winSizeX = vesa->x_resolution; 1976854f87cSSimon Glass gdev->winSizeY = vesa->y_resolution; 1986854f87cSSimon Glass 1996854f87cSSimon Glass gdev->plnSizeX = vesa->x_resolution; 2006854f87cSSimon Glass gdev->plnSizeY = vesa->y_resolution; 2016854f87cSSimon Glass 2026854f87cSSimon Glass gdev->gdfBytesPP = vesa->bits_per_pixel / 8; 2036854f87cSSimon Glass 2046854f87cSSimon Glass switch (vesa->bits_per_pixel) { 2056854f87cSSimon Glass case 24: 2066854f87cSSimon Glass gdev->gdfIndex = GDF_32BIT_X888RGB; 2076854f87cSSimon Glass break; 2086854f87cSSimon Glass case 16: 2096854f87cSSimon Glass gdev->gdfIndex = GDF_16BIT_565RGB; 2106854f87cSSimon Glass break; 2116854f87cSSimon Glass default: 2126854f87cSSimon Glass gdev->gdfIndex = GDF__8BIT_INDEX; 2136854f87cSSimon Glass break; 2146854f87cSSimon Glass } 2156854f87cSSimon Glass 2166854f87cSSimon Glass gdev->isaBase = CONFIG_SYS_ISA_IO_BASE_ADDRESS; 2176854f87cSSimon Glass gdev->pciBase = vesa->phys_base_ptr; 2186854f87cSSimon Glass 2196854f87cSSimon Glass gdev->frameAdrs = vesa->phys_base_ptr; 2206854f87cSSimon Glass gdev->memSize = vesa->bytes_per_scanline * vesa->y_resolution; 2216854f87cSSimon Glass 2226854f87cSSimon Glass gdev->vprBase = vesa->phys_base_ptr; 2236854f87cSSimon Glass gdev->cprBase = vesa->phys_base_ptr; 2246854f87cSSimon Glass 22523609c71SSimon Glass return gdev->winSizeX ? 0 : -ENOSYS; 2266854f87cSSimon Glass #else 2276854f87cSSimon Glass return -ENOSYS; 2286854f87cSSimon Glass #endif 2296854f87cSSimon Glass } 2306854f87cSSimon Glass 2316854f87cSSimon Glass int pci_run_vga_bios(pci_dev_t dev, int (*int15_handler)(void), bool emulate) 2326854f87cSSimon Glass { 2336854f87cSSimon Glass struct pci_rom_header *rom, *ram; 2346854f87cSSimon Glass int vesa_mode = -1; 2356854f87cSSimon Glass uint16_t class; 2366854f87cSSimon Glass int ret; 2376854f87cSSimon Glass 2386854f87cSSimon Glass /* Only execute VGA ROMs */ 2396854f87cSSimon Glass pci_read_config_word(dev, PCI_CLASS_DEVICE, &class); 2406854f87cSSimon Glass if ((class ^ PCI_CLASS_DISPLAY_VGA) & 0xff00) { 2416854f87cSSimon Glass debug("%s: Class %#x, should be %#x\n", __func__, class, 2426854f87cSSimon Glass PCI_CLASS_DISPLAY_VGA); 2436854f87cSSimon Glass return -ENODEV; 2446854f87cSSimon Glass } 2456854f87cSSimon Glass 2466854f87cSSimon Glass if (!should_load_oprom(dev)) 2476854f87cSSimon Glass return -ENXIO; 2486854f87cSSimon Glass 2496854f87cSSimon Glass ret = pci_rom_probe(dev, class, &rom); 2506854f87cSSimon Glass if (ret) 2516854f87cSSimon Glass return ret; 2526854f87cSSimon Glass 2536854f87cSSimon Glass ret = pci_rom_load(class, rom, &ram); 2546854f87cSSimon Glass if (ret) 2556854f87cSSimon Glass return ret; 2566854f87cSSimon Glass 2576854f87cSSimon Glass if (!board_should_run_oprom(dev)) 2586854f87cSSimon Glass return -ENXIO; 2596854f87cSSimon Glass 2606854f87cSSimon Glass #if defined(CONFIG_FRAMEBUFFER_SET_VESA_MODE) && \ 2616854f87cSSimon Glass defined(CONFIG_FRAMEBUFFER_VESA_MODE) 2626854f87cSSimon Glass vesa_mode = CONFIG_FRAMEBUFFER_VESA_MODE; 2636854f87cSSimon Glass #endif 2649a99caf3SSimon Glass debug("Selected vesa mode %#x\n", vesa_mode); 2656854f87cSSimon Glass if (emulate) { 2666854f87cSSimon Glass #ifdef CONFIG_BIOSEMU 2676854f87cSSimon Glass BE_VGAInfo *info; 2686854f87cSSimon Glass 2696854f87cSSimon Glass ret = biosemu_setup(dev, &info); 2706854f87cSSimon Glass if (ret) 2716854f87cSSimon Glass return ret; 2726854f87cSSimon Glass biosemu_set_interrupt_handler(0x15, int15_handler); 2736854f87cSSimon Glass ret = biosemu_run(dev, (uchar *)ram, 1 << 16, info, true, 2746854f87cSSimon Glass vesa_mode, &mode_info); 2756854f87cSSimon Glass if (ret) 2766854f87cSSimon Glass return ret; 2776854f87cSSimon Glass #else 2786854f87cSSimon Glass printf("BIOS emulation not available - see CONFIG_BIOSEMU\n"); 2796854f87cSSimon Glass return -ENOSYS; 2806854f87cSSimon Glass #endif 2816854f87cSSimon Glass } else { 2826854f87cSSimon Glass #ifdef CONFIG_X86 2836854f87cSSimon Glass bios_set_interrupt_handler(0x15, int15_handler); 2846854f87cSSimon Glass 2856854f87cSSimon Glass bios_run_on_x86(dev, (unsigned long)ram, vesa_mode, 2866854f87cSSimon Glass &mode_info); 2876854f87cSSimon Glass #else 2886854f87cSSimon Glass printf("BIOS native execution is only available on x86\n"); 2896854f87cSSimon Glass return -ENOSYS; 2906854f87cSSimon Glass #endif 2916854f87cSSimon Glass } 2929a99caf3SSimon Glass debug("Final vesa mode %#x\n", mode_info.video_mode); 2936854f87cSSimon Glass 2946854f87cSSimon Glass return 0; 2956854f87cSSimon Glass } 296