2 * Copyright (C) 2014 Google, Inc
4 * From coreboot, originally based on the Linux kernel (drivers/pci/pci.c).
7 * Copyright (C) 2003-2004 Linux Networx
8 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
9 * Copyright (C) 2003-2006 Ronald G. Minnich <rminnich@gmail.com>
10 * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov>
11 * Copyright (C) 2005-2006 Tyan
12 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
13 * Copyright (C) 2005-2009 coresystems GmbH
14 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
16 * PCI Bus Services, see include/linux/pci.h for further explanation.
18 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
19 * David Mosberger-Tang
21 * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
23 * SPDX-License-Identifier: GPL-2.0
27 #include <bios_emul.h>
35 #include <linux/screen_info.h>
37 __weak bool board_should_run_oprom(struct udevice *dev)
42 static bool should_load_oprom(struct udevice *dev)
44 if (board_should_run_oprom(dev))
50 __weak uint32_t board_map_oprom_vendev(uint32_t vendev)
55 static int pci_rom_probe(struct udevice *dev, struct pci_rom_header **hdrp)
57 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
58 struct pci_rom_header *rom_header;
59 struct pci_rom_data *rom_data;
60 u16 rom_vendor, rom_device;
66 vendev = pplat->vendor << 16 | pplat->device;
67 mapped_vendev = board_map_oprom_vendev(vendev);
68 if (vendev != mapped_vendev)
69 debug("Device ID mapped to %#08x\n", mapped_vendev);
71 #ifdef CONFIG_VGA_BIOS_ADDR
72 rom_address = CONFIG_VGA_BIOS_ADDR;
75 dm_pci_read_config32(dev, PCI_ROM_ADDRESS, &rom_address);
76 if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
77 debug("%s: rom_address=%x\n", __func__, rom_address);
81 /* Enable expansion ROM address decoding. */
82 dm_pci_write_config32(dev, PCI_ROM_ADDRESS,
83 rom_address | PCI_ROM_ADDRESS_ENABLE);
85 debug("Option ROM address %x\n", rom_address);
86 rom_header = (struct pci_rom_header *)(unsigned long)rom_address;
88 debug("PCI expansion ROM, signature %#04x, INIT size %#04x, data ptr %#04x\n",
89 le16_to_cpu(rom_header->signature),
90 rom_header->size * 512, le16_to_cpu(rom_header->data));
92 if (le16_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
93 printf("Incorrect expansion ROM header signature %04x\n",
94 le16_to_cpu(rom_header->signature));
95 #ifndef CONFIG_VGA_BIOS_ADDR
96 /* Disable expansion ROM address decoding */
97 dm_pci_write_config32(dev, PCI_ROM_ADDRESS, rom_address);
102 rom_data = (((void *)rom_header) + le16_to_cpu(rom_header->data));
103 rom_vendor = le16_to_cpu(rom_data->vendor);
104 rom_device = le16_to_cpu(rom_data->device);
106 debug("PCI ROM image, vendor ID %04x, device ID %04x,\n",
107 rom_vendor, rom_device);
109 /* If the device id is mapped, a mismatch is expected */
110 if ((pplat->vendor != rom_vendor || pplat->device != rom_device) &&
111 (vendev == mapped_vendev)) {
112 printf("ID mismatch: vendor ID %04x, device ID %04x\n",
113 rom_vendor, rom_device);
114 /* Continue anyway */
117 rom_class = (le16_to_cpu(rom_data->class_hi) << 8) | rom_data->class_lo;
118 debug("PCI ROM image, Class Code %06x, Code Type %02x\n",
119 rom_class, rom_data->type);
121 if (pplat->class != rom_class) {
122 debug("Class Code mismatch ROM %06x, dev %06x\n",
123 rom_class, pplat->class);
131 * pci_rom_load() - Load a ROM image and return a pointer to it
133 * @rom_header: Pointer to ROM image
134 * @ram_headerp: Returns a pointer to the image in RAM
135 * @allocedp: Returns true if @ram_headerp was allocated and needs
137 * @return 0 if OK, -ve on error. Note that @allocedp is set up regardless of
138 * the error state. Even if this function returns an error, it may have
141 static int pci_rom_load(struct pci_rom_header *rom_header,
142 struct pci_rom_header **ram_headerp, bool *allocedp)
144 struct pci_rom_data *rom_data;
145 unsigned int rom_size;
146 unsigned int image_size = 0;
151 /* Get next image, until we see an x86 version */
152 rom_header = (struct pci_rom_header *)((void *)rom_header +
155 rom_data = (struct pci_rom_data *)((void *)rom_header +
156 le16_to_cpu(rom_header->data));
158 image_size = le16_to_cpu(rom_data->ilen) * 512;
159 } while ((rom_data->type != 0) && (rom_data->indicator == 0));
161 if (rom_data->type != 0)
164 rom_size = rom_header->size * 512;
166 #ifdef PCI_VGA_RAM_IMAGE_START
167 target = (void *)PCI_VGA_RAM_IMAGE_START;
169 target = (void *)malloc(rom_size);
174 if (target != rom_header) {
175 ulong start = get_timer(0);
177 debug("Copying VGA ROM Image from %p to %p, 0x%x bytes\n",
178 rom_header, target, rom_size);
179 memcpy(target, rom_header, rom_size);
180 if (memcmp(target, rom_header, rom_size)) {
181 printf("VGA ROM copy failed\n");
184 debug("Copy took %lums\n", get_timer(start));
186 *ram_headerp = target;
191 struct vbe_mode_info mode_info;
193 int vbe_get_video_info(struct graphic_device *gdev)
195 #ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE
196 struct vesa_mode_info *vesa = &mode_info.vesa;
198 gdev->winSizeX = vesa->x_resolution;
199 gdev->winSizeY = vesa->y_resolution;
201 gdev->plnSizeX = vesa->x_resolution;
202 gdev->plnSizeY = vesa->y_resolution;
204 gdev->gdfBytesPP = vesa->bits_per_pixel / 8;
206 switch (vesa->bits_per_pixel) {
209 gdev->gdfIndex = GDF_32BIT_X888RGB;
212 gdev->gdfIndex = GDF_16BIT_565RGB;
215 gdev->gdfIndex = GDF__8BIT_INDEX;
219 gdev->isaBase = CONFIG_SYS_ISA_IO_BASE_ADDRESS;
220 gdev->pciBase = vesa->phys_base_ptr;
222 gdev->frameAdrs = vesa->phys_base_ptr;
223 gdev->memSize = vesa->bytes_per_scanline * vesa->y_resolution;
225 gdev->vprBase = vesa->phys_base_ptr;
226 gdev->cprBase = vesa->phys_base_ptr;
228 return gdev->winSizeX ? 0 : -ENOSYS;
234 void setup_video(struct screen_info *screen_info)
236 struct vesa_mode_info *vesa = &mode_info.vesa;
238 /* Sanity test on VESA parameters */
239 if (!vesa->x_resolution || !vesa->y_resolution)
242 screen_info->orig_video_isVGA = VIDEO_TYPE_VLFB;
244 screen_info->lfb_width = vesa->x_resolution;
245 screen_info->lfb_height = vesa->y_resolution;
246 screen_info->lfb_depth = vesa->bits_per_pixel;
247 screen_info->lfb_linelength = vesa->bytes_per_scanline;
248 screen_info->lfb_base = vesa->phys_base_ptr;
249 screen_info->lfb_size =
250 ALIGN(screen_info->lfb_linelength * screen_info->lfb_height,
252 screen_info->lfb_size >>= 16;
253 screen_info->red_size = vesa->red_mask_size;
254 screen_info->red_pos = vesa->red_mask_pos;
255 screen_info->green_size = vesa->green_mask_size;
256 screen_info->green_pos = vesa->green_mask_pos;
257 screen_info->blue_size = vesa->blue_mask_size;
258 screen_info->blue_pos = vesa->blue_mask_pos;
259 screen_info->rsvd_size = vesa->reserved_mask_size;
260 screen_info->rsvd_pos = vesa->reserved_mask_pos;
263 int dm_pci_run_vga_bios(struct udevice *dev, int (*int15_handler)(void),
266 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
267 struct pci_rom_header *rom = NULL, *ram = NULL;
269 bool emulate, alloced;
272 /* Only execute VGA ROMs */
273 if (((pplat->class >> 8) ^ PCI_CLASS_DISPLAY_VGA) & 0xff00) {
274 debug("%s: Class %#x, should be %#x\n", __func__, pplat->class,
275 PCI_CLASS_DISPLAY_VGA);
279 if (!should_load_oprom(dev))
282 ret = pci_rom_probe(dev, &rom);
286 ret = pci_rom_load(rom, &ram, &alloced);
290 if (!board_should_run_oprom(dev)) {
295 #if defined(CONFIG_FRAMEBUFFER_SET_VESA_MODE) && \
296 defined(CONFIG_FRAMEBUFFER_VESA_MODE)
297 vesa_mode = CONFIG_FRAMEBUFFER_VESA_MODE;
299 debug("Selected vesa mode %#x\n", vesa_mode);
301 if (exec_method & PCI_ROM_USE_NATIVE) {
305 if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
306 printf("BIOS native execution is only available on x86\n");
313 #ifdef CONFIG_BIOSEMU
316 if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
317 printf("BIOS emulation not available - see CONFIG_BIOSEMU\n");
326 #ifdef CONFIG_BIOSEMU
329 ret = biosemu_setup(dev, &info);
332 biosemu_set_interrupt_handler(0x15, int15_handler);
333 ret = biosemu_run(dev, (uchar *)ram, 1 << 16, info,
334 true, vesa_mode, &mode_info);
340 bios_set_interrupt_handler(0x15, int15_handler);
342 bios_run_on_x86(dev, (unsigned long)ram, vesa_mode,
346 debug("Final vesa mode %#x\n", mode_info.video_mode);