xref: /openbmc/u-boot/drivers/pci/pci_rom.c (revision 8beb0bda9c59146df70b960ce69ecfbd6f744028)
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>
283f4e1e8eSSimon Glass #include <dm.h>
296854f87cSSimon Glass #include <errno.h>
306854f87cSSimon Glass #include <malloc.h>
316854f87cSSimon Glass #include <pci.h>
326854f87cSSimon Glass #include <pci_rom.h>
336854f87cSSimon Glass #include <vbe.h>
346854f87cSSimon Glass #include <video_fb.h>
35a4520022SBin Meng #include <linux/screen_info.h>
366854f87cSSimon Glass 
373f4e1e8eSSimon Glass __weak bool board_should_run_oprom(struct udevice *dev)
386854f87cSSimon Glass {
396854f87cSSimon Glass 	return true;
406854f87cSSimon Glass }
416854f87cSSimon Glass 
423f4e1e8eSSimon Glass static bool should_load_oprom(struct udevice *dev)
436854f87cSSimon Glass {
446854f87cSSimon Glass 	if (IS_ENABLED(CONFIG_ALWAYS_LOAD_OPROM))
456854f87cSSimon Glass 		return 1;
466854f87cSSimon Glass 	if (board_should_run_oprom(dev))
476854f87cSSimon Glass 		return 1;
486854f87cSSimon Glass 
496854f87cSSimon Glass 	return 0;
506854f87cSSimon Glass }
516854f87cSSimon Glass 
526854f87cSSimon Glass __weak uint32_t board_map_oprom_vendev(uint32_t vendev)
536854f87cSSimon Glass {
546854f87cSSimon Glass 	return vendev;
556854f87cSSimon Glass }
566854f87cSSimon Glass 
573f4e1e8eSSimon Glass static int pci_rom_probe(struct udevice *dev, struct pci_rom_header **hdrp)
586854f87cSSimon Glass {
593f4e1e8eSSimon Glass 	struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
606854f87cSSimon Glass 	struct pci_rom_header *rom_header;
616854f87cSSimon Glass 	struct pci_rom_data *rom_data;
6240305240SSimon Glass 	u16 rom_vendor, rom_device;
63d57c2f24SBin Meng 	u32 rom_class;
646854f87cSSimon Glass 	u32 vendev;
656854f87cSSimon Glass 	u32 mapped_vendev;
666854f87cSSimon Glass 	u32 rom_address;
676854f87cSSimon Glass 
683f4e1e8eSSimon Glass 	vendev = pplat->vendor << 16 | pplat->device;
696854f87cSSimon Glass 	mapped_vendev = board_map_oprom_vendev(vendev);
706854f87cSSimon Glass 	if (vendev != mapped_vendev)
716854f87cSSimon Glass 		debug("Device ID mapped to %#08x\n", mapped_vendev);
726854f87cSSimon Glass 
73786a08e0SBin Meng #ifdef CONFIG_VGA_BIOS_ADDR
74786a08e0SBin Meng 	rom_address = CONFIG_VGA_BIOS_ADDR;
756854f87cSSimon Glass #else
764a2708a0SSimon Glass 
773f4e1e8eSSimon Glass 	dm_pci_read_config32(dev, PCI_ROM_ADDRESS, &rom_address);
786854f87cSSimon Glass 	if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
796854f87cSSimon Glass 		debug("%s: rom_address=%x\n", __func__, rom_address);
806854f87cSSimon Glass 		return -ENOENT;
816854f87cSSimon Glass 	}
826854f87cSSimon Glass 
836854f87cSSimon Glass 	/* Enable expansion ROM address decoding. */
843f4e1e8eSSimon Glass 	dm_pci_write_config32(dev, PCI_ROM_ADDRESS,
856854f87cSSimon Glass 			      rom_address | PCI_ROM_ADDRESS_ENABLE);
866854f87cSSimon Glass #endif
876854f87cSSimon Glass 	debug("Option ROM address %x\n", rom_address);
88ef2d17feSMinghuan Lian 	rom_header = (struct pci_rom_header *)(unsigned long)rom_address;
896854f87cSSimon Glass 
906854f87cSSimon Glass 	debug("PCI expansion ROM, signature %#04x, INIT size %#04x, data ptr %#04x\n",
9140305240SSimon Glass 	      le16_to_cpu(rom_header->signature),
9240305240SSimon Glass 	      rom_header->size * 512, le16_to_cpu(rom_header->data));
936854f87cSSimon Glass 
9440305240SSimon Glass 	if (le16_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
956854f87cSSimon Glass 		printf("Incorrect expansion ROM header signature %04x\n",
9640305240SSimon Glass 		       le16_to_cpu(rom_header->signature));
97f110da99SBin Meng #ifndef CONFIG_VGA_BIOS_ADDR
98f110da99SBin Meng 		/* Disable expansion ROM address decoding */
993f4e1e8eSSimon Glass 		dm_pci_write_config32(dev, PCI_ROM_ADDRESS, rom_address);
100f110da99SBin Meng #endif
1016854f87cSSimon Glass 		return -EINVAL;
1026854f87cSSimon Glass 	}
1036854f87cSSimon Glass 
10440305240SSimon Glass 	rom_data = (((void *)rom_header) + le16_to_cpu(rom_header->data));
10540305240SSimon Glass 	rom_vendor = le16_to_cpu(rom_data->vendor);
10640305240SSimon Glass 	rom_device = le16_to_cpu(rom_data->device);
1076854f87cSSimon Glass 
1086854f87cSSimon Glass 	debug("PCI ROM image, vendor ID %04x, device ID %04x,\n",
10940305240SSimon Glass 	      rom_vendor, rom_device);
1106854f87cSSimon Glass 
1116854f87cSSimon Glass 	/* If the device id is mapped, a mismatch is expected */
1123f4e1e8eSSimon Glass 	if ((pplat->vendor != rom_vendor || pplat->device != rom_device) &&
1136854f87cSSimon Glass 	    (vendev == mapped_vendev)) {
1146854f87cSSimon Glass 		printf("ID mismatch: vendor ID %04x, device ID %04x\n",
11540305240SSimon Glass 		       rom_vendor, rom_device);
116c5caba03SSimon Glass 		/* Continue anyway */
1176854f87cSSimon Glass 	}
1186854f87cSSimon Glass 
119d57c2f24SBin Meng 	rom_class = (le16_to_cpu(rom_data->class_hi) << 8) | rom_data->class_lo;
120d57c2f24SBin Meng 	debug("PCI ROM image, Class Code %06x, Code Type %02x\n",
121d57c2f24SBin Meng 	      rom_class, rom_data->type);
1226854f87cSSimon Glass 
1233f4e1e8eSSimon Glass 	if (pplat->class != rom_class) {
124d57c2f24SBin Meng 		debug("Class Code mismatch ROM %06x, dev %06x\n",
1253f4e1e8eSSimon Glass 		      rom_class, pplat->class);
1266854f87cSSimon Glass 	}
1276854f87cSSimon Glass 	*hdrp = rom_header;
1286854f87cSSimon Glass 
1296854f87cSSimon Glass 	return 0;
1306854f87cSSimon Glass }
1316854f87cSSimon Glass 
13281d0b354SBin Meng int pci_rom_load(struct pci_rom_header *rom_header,
1336854f87cSSimon Glass 		 struct pci_rom_header **ram_headerp)
1346854f87cSSimon Glass {
1356854f87cSSimon Glass 	struct pci_rom_data *rom_data;
1366854f87cSSimon Glass 	unsigned int rom_size;
1376854f87cSSimon Glass 	unsigned int image_size = 0;
1386854f87cSSimon Glass 	void *target;
1396854f87cSSimon Glass 
1406854f87cSSimon Glass 	do {
1416854f87cSSimon Glass 		/* Get next image, until we see an x86 version */
1426854f87cSSimon Glass 		rom_header = (struct pci_rom_header *)((void *)rom_header +
1436854f87cSSimon Glass 							    image_size);
1446854f87cSSimon Glass 
1456854f87cSSimon Glass 		rom_data = (struct pci_rom_data *)((void *)rom_header +
14640305240SSimon Glass 				le16_to_cpu(rom_header->data));
1476854f87cSSimon Glass 
14840305240SSimon Glass 		image_size = le16_to_cpu(rom_data->ilen) * 512;
14940305240SSimon Glass 	} while ((rom_data->type != 0) && (rom_data->indicator == 0));
1506854f87cSSimon Glass 
1516854f87cSSimon Glass 	if (rom_data->type != 0)
1526854f87cSSimon Glass 		return -EACCES;
1536854f87cSSimon Glass 
1546854f87cSSimon Glass 	rom_size = rom_header->size * 512;
1556854f87cSSimon Glass 
156bdc88d4eSSimon Glass #ifdef PCI_VGA_RAM_IMAGE_START
1576854f87cSSimon Glass 	target = (void *)PCI_VGA_RAM_IMAGE_START;
158bdc88d4eSSimon Glass #else
159bdc88d4eSSimon Glass 	target = (void *)malloc(rom_size);
160bdc88d4eSSimon Glass 	if (!target)
161bdc88d4eSSimon Glass 		return -ENOMEM;
162bdc88d4eSSimon Glass #endif
1636854f87cSSimon Glass 	if (target != rom_header) {
164fba7eac1SSimon Glass 		ulong start = get_timer(0);
165fba7eac1SSimon Glass 
1666854f87cSSimon Glass 		debug("Copying VGA ROM Image from %p to %p, 0x%x bytes\n",
1676854f87cSSimon Glass 		      rom_header, target, rom_size);
1686854f87cSSimon Glass 		memcpy(target, rom_header, rom_size);
1696854f87cSSimon Glass 		if (memcmp(target, rom_header, rom_size)) {
1706854f87cSSimon Glass 			printf("VGA ROM copy failed\n");
1716854f87cSSimon Glass 			return -EFAULT;
1726854f87cSSimon Glass 		}
173fba7eac1SSimon Glass 		debug("Copy took %lums\n", get_timer(start));
1746854f87cSSimon Glass 	}
1756854f87cSSimon Glass 	*ram_headerp = target;
1766854f87cSSimon Glass 
1776854f87cSSimon Glass 	return 0;
1786854f87cSSimon Glass }
1796854f87cSSimon Glass 
180153e1ddaSBin Meng struct vbe_mode_info mode_info;
1816854f87cSSimon Glass 
1826854f87cSSimon Glass int vbe_get_video_info(struct graphic_device *gdev)
1836854f87cSSimon Glass {
1846854f87cSSimon Glass #ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE
1856854f87cSSimon Glass 	struct vesa_mode_info *vesa = &mode_info.vesa;
1866854f87cSSimon Glass 
1876854f87cSSimon Glass 	gdev->winSizeX = vesa->x_resolution;
1886854f87cSSimon Glass 	gdev->winSizeY = vesa->y_resolution;
1896854f87cSSimon Glass 
1906854f87cSSimon Glass 	gdev->plnSizeX = vesa->x_resolution;
1916854f87cSSimon Glass 	gdev->plnSizeY = vesa->y_resolution;
1926854f87cSSimon Glass 
1936854f87cSSimon Glass 	gdev->gdfBytesPP = vesa->bits_per_pixel / 8;
1946854f87cSSimon Glass 
1956854f87cSSimon Glass 	switch (vesa->bits_per_pixel) {
1960e98a147SJian Luo 	case 32:
1976854f87cSSimon Glass 	case 24:
1986854f87cSSimon Glass 		gdev->gdfIndex = GDF_32BIT_X888RGB;
1996854f87cSSimon Glass 		break;
2006854f87cSSimon Glass 	case 16:
2016854f87cSSimon Glass 		gdev->gdfIndex = GDF_16BIT_565RGB;
2026854f87cSSimon Glass 		break;
2036854f87cSSimon Glass 	default:
2046854f87cSSimon Glass 		gdev->gdfIndex = GDF__8BIT_INDEX;
2056854f87cSSimon Glass 		break;
2066854f87cSSimon Glass 	}
2076854f87cSSimon Glass 
2086854f87cSSimon Glass 	gdev->isaBase = CONFIG_SYS_ISA_IO_BASE_ADDRESS;
2096854f87cSSimon Glass 	gdev->pciBase = vesa->phys_base_ptr;
2106854f87cSSimon Glass 
2116854f87cSSimon Glass 	gdev->frameAdrs = vesa->phys_base_ptr;
2126854f87cSSimon Glass 	gdev->memSize = vesa->bytes_per_scanline * vesa->y_resolution;
2136854f87cSSimon Glass 
2146854f87cSSimon Glass 	gdev->vprBase = vesa->phys_base_ptr;
2156854f87cSSimon Glass 	gdev->cprBase = vesa->phys_base_ptr;
2166854f87cSSimon Glass 
21723609c71SSimon Glass 	return gdev->winSizeX ? 0 : -ENOSYS;
2186854f87cSSimon Glass #else
2196854f87cSSimon Glass 	return -ENOSYS;
2206854f87cSSimon Glass #endif
2216854f87cSSimon Glass }
2226854f87cSSimon Glass 
223a4520022SBin Meng void setup_video(struct screen_info *screen_info)
224a4520022SBin Meng {
225a4520022SBin Meng 	struct vesa_mode_info *vesa = &mode_info.vesa;
226a4520022SBin Meng 
2271e7a0473SBin Meng 	/* Sanity test on VESA parameters */
2281e7a0473SBin Meng 	if (!vesa->x_resolution || !vesa->y_resolution)
2291e7a0473SBin Meng 		return;
2301e7a0473SBin Meng 
231a4520022SBin Meng 	screen_info->orig_video_isVGA = VIDEO_TYPE_VLFB;
232a4520022SBin Meng 
233a4520022SBin Meng 	screen_info->lfb_width = vesa->x_resolution;
234a4520022SBin Meng 	screen_info->lfb_height = vesa->y_resolution;
235a4520022SBin Meng 	screen_info->lfb_depth = vesa->bits_per_pixel;
236a4520022SBin Meng 	screen_info->lfb_linelength = vesa->bytes_per_scanline;
237a4520022SBin Meng 	screen_info->lfb_base = vesa->phys_base_ptr;
238a4520022SBin Meng 	screen_info->lfb_size =
239a4520022SBin Meng 		ALIGN(screen_info->lfb_linelength * screen_info->lfb_height,
240a4520022SBin Meng 		      65536);
241a4520022SBin Meng 	screen_info->lfb_size >>= 16;
242a4520022SBin Meng 	screen_info->red_size = vesa->red_mask_size;
243a4520022SBin Meng 	screen_info->red_pos = vesa->red_mask_pos;
244a4520022SBin Meng 	screen_info->green_size = vesa->green_mask_size;
245a4520022SBin Meng 	screen_info->green_pos = vesa->green_mask_pos;
246a4520022SBin Meng 	screen_info->blue_size = vesa->blue_mask_size;
247a4520022SBin Meng 	screen_info->blue_pos = vesa->blue_mask_pos;
248a4520022SBin Meng 	screen_info->rsvd_size = vesa->reserved_mask_size;
249a4520022SBin Meng 	screen_info->rsvd_pos = vesa->reserved_mask_pos;
250a4520022SBin Meng }
251a4520022SBin Meng 
2523f4e1e8eSSimon Glass int dm_pci_run_vga_bios(struct udevice *dev, int (*int15_handler)(void),
2533f4e1e8eSSimon Glass 			int exec_method)
2546854f87cSSimon Glass {
2553f4e1e8eSSimon Glass 	struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
2566854f87cSSimon Glass 	struct pci_rom_header *rom, *ram;
2576854f87cSSimon Glass 	int vesa_mode = -1;
258bc17d8f4SSimon Glass 	bool emulate;
2596854f87cSSimon Glass 	int ret;
2606854f87cSSimon Glass 
2616854f87cSSimon Glass 	/* Only execute VGA ROMs */
2623f4e1e8eSSimon Glass 	if (((pplat->class >> 8) ^ PCI_CLASS_DISPLAY_VGA) & 0xff00) {
2633f4e1e8eSSimon Glass 		debug("%s: Class %#x, should be %#x\n", __func__, pplat->class,
2646854f87cSSimon Glass 		      PCI_CLASS_DISPLAY_VGA);
2656854f87cSSimon Glass 		return -ENODEV;
2666854f87cSSimon Glass 	}
2676854f87cSSimon Glass 
2686854f87cSSimon Glass 	if (!should_load_oprom(dev))
2696854f87cSSimon Glass 		return -ENXIO;
2706854f87cSSimon Glass 
2713f4e1e8eSSimon Glass 	ret = pci_rom_probe(dev, &rom);
2726854f87cSSimon Glass 	if (ret)
2736854f87cSSimon Glass 		return ret;
2746854f87cSSimon Glass 
27581d0b354SBin Meng 	ret = pci_rom_load(rom, &ram);
2766854f87cSSimon Glass 	if (ret)
2776854f87cSSimon Glass 		return ret;
2786854f87cSSimon Glass 
2796854f87cSSimon Glass 	if (!board_should_run_oprom(dev))
2806854f87cSSimon Glass 		return -ENXIO;
2816854f87cSSimon Glass 
2826854f87cSSimon Glass #if defined(CONFIG_FRAMEBUFFER_SET_VESA_MODE) && \
2836854f87cSSimon Glass 		defined(CONFIG_FRAMEBUFFER_VESA_MODE)
2846854f87cSSimon Glass 	vesa_mode = CONFIG_FRAMEBUFFER_VESA_MODE;
2856854f87cSSimon Glass #endif
2869a99caf3SSimon Glass 	debug("Selected vesa mode %#x\n", vesa_mode);
287bc17d8f4SSimon Glass 
288bc17d8f4SSimon Glass 	if (exec_method & PCI_ROM_USE_NATIVE) {
289bc17d8f4SSimon Glass #ifdef CONFIG_X86
290bc17d8f4SSimon Glass 		emulate = false;
291bc17d8f4SSimon Glass #else
292bc17d8f4SSimon Glass 		if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
293bc17d8f4SSimon Glass 			printf("BIOS native execution is only available on x86\n");
294bc17d8f4SSimon Glass 			return -ENOSYS;
295bc17d8f4SSimon Glass 		}
296bc17d8f4SSimon Glass 		emulate = true;
297bc17d8f4SSimon Glass #endif
298bc17d8f4SSimon Glass 	} else {
299bc17d8f4SSimon Glass #ifdef CONFIG_BIOSEMU
300bc17d8f4SSimon Glass 		emulate = true;
301bc17d8f4SSimon Glass #else
302bc17d8f4SSimon Glass 		if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
303bc17d8f4SSimon Glass 			printf("BIOS emulation not available - see CONFIG_BIOSEMU\n");
304bc17d8f4SSimon Glass 			return -ENOSYS;
305bc17d8f4SSimon Glass 		}
306bc17d8f4SSimon Glass 		emulate = false;
307bc17d8f4SSimon Glass #endif
308bc17d8f4SSimon Glass 	}
309bc17d8f4SSimon Glass 
3106854f87cSSimon Glass 	if (emulate) {
3116854f87cSSimon Glass #ifdef CONFIG_BIOSEMU
3126854f87cSSimon Glass 		BE_VGAInfo *info;
3136854f87cSSimon Glass 
3143f4e1e8eSSimon Glass 		ret = biosemu_setup(dm_pci_get_bdf(dev), &info);
3156854f87cSSimon Glass 		if (ret)
3166854f87cSSimon Glass 			return ret;
3176854f87cSSimon Glass 		biosemu_set_interrupt_handler(0x15, int15_handler);
3183f4e1e8eSSimon Glass 		ret = biosemu_run(dm_pci_get_bdf(dev), (uchar *)ram, 1 << 16,
3193f4e1e8eSSimon Glass 				  info, true, vesa_mode, &mode_info);
3206854f87cSSimon Glass 		if (ret)
3216854f87cSSimon Glass 			return ret;
3226854f87cSSimon Glass #endif
3236854f87cSSimon Glass 	} else {
3246854f87cSSimon Glass #ifdef CONFIG_X86
3256854f87cSSimon Glass 		bios_set_interrupt_handler(0x15, int15_handler);
3266854f87cSSimon Glass 
327*8beb0bdaSSimon Glass 		bios_run_on_x86(dev, (unsigned long)ram, vesa_mode,
328*8beb0bdaSSimon Glass 				&mode_info);
3296854f87cSSimon Glass #endif
3306854f87cSSimon Glass 	}
3319a99caf3SSimon Glass 	debug("Final vesa mode %#x\n", mode_info.video_mode);
3326854f87cSSimon Glass 
3336854f87cSSimon Glass 	return 0;
3346854f87cSSimon Glass }
335