atibios.c 20 KB

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