atibios.c 20 KB

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