atibios.c 20 KB

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