atibios.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. static void PCI_doBIOSPOST(pci_dev_t pcidev, BE_VGAInfo *vga_info,
  212. int vesa_mode, struct vbe_mode_info *mode_info)
  213. {
  214. RMREGS regs;
  215. RMSREGS sregs;
  216. /* Determine the value to store in AX for BIOS POST. Per the PCI specs,
  217. AH must contain the bus and AL must contain the devfn, encoded as
  218. (dev << 3) | fn
  219. */
  220. memset(&regs, 0, sizeof(regs));
  221. memset(&sregs, 0, sizeof(sregs));
  222. regs.x.ax = ((int)PCI_BUS(pcidev) << 8) |
  223. ((int)PCI_DEV(pcidev) << 3) | (int)PCI_FUNC(pcidev);
  224. /*Setup the X86 emulator for the VGA BIOS*/
  225. BE_setVGA(vga_info);
  226. /*Execute the BIOS POST code*/
  227. BE_callRealMode(0xC000, 0x0003, &regs, &sregs);
  228. /*Cleanup and exit*/
  229. BE_getVGA(vga_info);
  230. /* Useful for debugging */
  231. if (0)
  232. atibios_debug_mode(vga_info, &regs, vesa_mode, mode_info);
  233. if (vesa_mode != -1)
  234. atibios_set_vesa_mode(&regs, vesa_mode, mode_info);
  235. }
  236. /****************************************************************************
  237. PARAMETERS:
  238. pcidev - PCI device info for the video card on the bus
  239. bar - Place to return the base address register offset to use
  240. RETURNS:
  241. The address to use to map the secondary BIOS (AGP devices)
  242. REMARKS:
  243. Searches all the PCI base address registers for the device looking for a
  244. memory mapping that is large enough to hold our ROM BIOS. We usually end up
  245. finding the framebuffer mapping (usually BAR 0x10), and we use this mapping
  246. to map the BIOS for the device into. We use a mapping that is already
  247. assigned to the device to ensure the memory range will be passed through
  248. by any PCI->PCI or AGP->PCI bridge that may be present.
  249. NOTE: Usually this function is only used for AGP devices, but it may be
  250. used for PCI devices that have already been POST'ed and the BIOS
  251. ROM base address has been zero'ed out.
  252. NOTE: This function leaves the original memory aperture disabled by leaving
  253. it programmed to all 1's. It must be restored to the correct value
  254. later.
  255. ****************************************************************************/
  256. static u32 PCI_findBIOSAddr(pci_dev_t pcidev, int *bar)
  257. {
  258. u32 base, size;
  259. for (*bar = 0x10; *bar <= 0x14; (*bar) += 4) {
  260. pci_read_config_dword(pcidev, *bar, &base);
  261. if (!(base & 0x1)) {
  262. pci_write_config_dword(pcidev, *bar, 0xFFFFFFFF);
  263. pci_read_config_dword(pcidev, *bar, &size);
  264. size = ~(size & ~0xFF) + 1;
  265. if (size >= MAX_BIOSLEN)
  266. return base & ~0xFF;
  267. }
  268. }
  269. return 0;
  270. }
  271. /****************************************************************************
  272. REMARKS:
  273. Some non-x86 Linux kernels map PCI relocateable I/O to values that
  274. are above 64K, which will not work with the BIOS image that requires
  275. the offset for the I/O ports to be a maximum of 16-bits. Ideally
  276. someone should fix the kernel to map the I/O ports for VGA compatible
  277. devices to a different location (or just all I/O ports since it is
  278. unlikely you can have enough devices in the machine to use up all
  279. 64K of the I/O space - a total of more than 256 cards would be
  280. necessary).
  281. Anyway to fix this we change all I/O mapped base registers and
  282. chop off the top bits.
  283. ****************************************************************************/
  284. static void PCI_fixupIObase(pci_dev_t pcidev, int reg, u32 * base)
  285. {
  286. if ((*base & 0x1) && (*base > 0xFFFE)) {
  287. *base &= 0xFFFF;
  288. pci_write_config_dword(pcidev, reg, *base);
  289. }
  290. }
  291. /****************************************************************************
  292. PARAMETERS:
  293. pcidev - PCI device info for the video card on the bus
  294. RETURNS:
  295. Pointers to the mapped BIOS image
  296. REMARKS:
  297. Maps a pointer to the BIOS image on the graphics card on the PCI bus.
  298. ****************************************************************************/
  299. void *PCI_mapBIOSImage(pci_dev_t pcidev)
  300. {
  301. u32 BIOSImageBus;
  302. int BIOSImageBAR;
  303. u8 *BIOSImage;
  304. /*Save PCI BAR registers that might get changed*/
  305. pci_read_config_dword(pcidev, PCI_ROM_ADDRESS, &saveROMBaseAddress);
  306. pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &saveBaseAddress10);
  307. pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_1, &saveBaseAddress14);
  308. pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_2, &saveBaseAddress18);
  309. pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_4, &saveBaseAddress20);
  310. /*Fix up I/O base registers to less than 64K */
  311. if(saveBaseAddress14 != 0)
  312. PCI_fixupIObase(pcidev, PCI_BASE_ADDRESS_1, &saveBaseAddress14);
  313. else
  314. PCI_fixupIObase(pcidev, PCI_BASE_ADDRESS_4, &saveBaseAddress20);
  315. /* Some cards have problems that stop us from being able to read the
  316. BIOS image from the ROM BAR. To fix this we have to do some chipset
  317. specific programming for different cards to solve this problem.
  318. */
  319. BIOSImageBus = PCI_findBIOSAddr(pcidev, &BIOSImageBAR);
  320. if (BIOSImageBus == 0) {
  321. printf("Find bios addr error\n");
  322. return NULL;
  323. }
  324. BIOSImage = pci_bus_to_virt(pcidev, BIOSImageBus,
  325. PCI_REGION_MEM, 0, MAP_NOCACHE);
  326. /*Change the PCI BAR registers to map it onto the bus.*/
  327. pci_write_config_dword(pcidev, BIOSImageBAR, 0);
  328. pci_write_config_dword(pcidev, PCI_ROM_ADDRESS, BIOSImageBus | 0x1);
  329. udelay(1);
  330. /*Check that the BIOS image is valid. If not fail, or return the
  331. compiled in BIOS image if that option was enabled
  332. */
  333. if (BIOSImage[0] != 0x55 || BIOSImage[1] != 0xAA || BIOSImage[2] == 0) {
  334. return NULL;
  335. }
  336. return BIOSImage;
  337. }
  338. /****************************************************************************
  339. PARAMETERS:
  340. pcidev - PCI device info for the video card on the bus
  341. REMARKS:
  342. Unmaps the BIOS image for the device and restores framebuffer mappings
  343. ****************************************************************************/
  344. void PCI_unmapBIOSImage(pci_dev_t pcidev, void *BIOSImage)
  345. {
  346. pci_write_config_dword(pcidev, PCI_ROM_ADDRESS, saveROMBaseAddress);
  347. pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_0, saveBaseAddress10);
  348. pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_1, saveBaseAddress14);
  349. pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_2, saveBaseAddress18);
  350. pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_4, saveBaseAddress20);
  351. }
  352. /****************************************************************************
  353. PARAMETERS:
  354. pcidev - PCI device info for the video card on the bus to boot
  355. VGAInfo - BIOS emulator VGA info structure
  356. RETURNS:
  357. true if successfully initialised, false if not.
  358. REMARKS:
  359. Loads and POST's the display controllers BIOS, directly from the BIOS
  360. image we can extract over the PCI bus.
  361. ****************************************************************************/
  362. static int PCI_postController(pci_dev_t pcidev, uchar *bios_rom, int bios_len,
  363. BE_VGAInfo *vga_info, int vesa_mode,
  364. struct vbe_mode_info *mode_info)
  365. {
  366. u32 bios_image_len;
  367. uchar *mapped_bios;
  368. uchar *copy_of_bios;
  369. if (bios_rom) {
  370. copy_of_bios = bios_rom;
  371. bios_image_len = bios_len;
  372. } else {
  373. /*
  374. * Allocate memory to store copy of BIOS from display
  375. * controller
  376. */
  377. mapped_bios = PCI_mapBIOSImage(pcidev);
  378. if (mapped_bios == NULL) {
  379. printf("videoboot: Video ROM failed to map!\n");
  380. return false;
  381. }
  382. bios_image_len = mapped_bios[2] * 512;
  383. copy_of_bios = malloc(bios_image_len);
  384. if (copy_of_bios == NULL) {
  385. printf("videoboot: Out of memory!\n");
  386. return false;
  387. }
  388. memcpy(copy_of_bios, mapped_bios, bios_image_len);
  389. PCI_unmapBIOSImage(pcidev, mapped_bios);
  390. }
  391. /*Save information in vga_info structure*/
  392. vga_info->function = PCI_FUNC(pcidev);
  393. vga_info->device = PCI_DEV(pcidev);
  394. vga_info->bus = PCI_BUS(pcidev);
  395. vga_info->pcidev = pcidev;
  396. vga_info->BIOSImage = copy_of_bios;
  397. vga_info->BIOSImageLen = bios_image_len;
  398. /*Now execute the BIOS POST for the device*/
  399. if (copy_of_bios[0] != 0x55 || copy_of_bios[1] != 0xAA) {
  400. printf("videoboot: Video ROM image is invalid!\n");
  401. return false;
  402. }
  403. PCI_doBIOSPOST(pcidev, vga_info, vesa_mode, mode_info);
  404. /*Reset the size of the BIOS image to the final size*/
  405. vga_info->BIOSImageLen = copy_of_bios[2] * 512;
  406. return true;
  407. }
  408. int biosemu_setup(pci_dev_t pcidev, BE_VGAInfo **vga_infop)
  409. {
  410. BE_VGAInfo *VGAInfo;
  411. printf("videoboot: Booting PCI video card bus %d, function %d, device %d\n",
  412. PCI_BUS(pcidev), PCI_FUNC(pcidev), PCI_DEV(pcidev));
  413. /*Initialise the x86 BIOS emulator*/
  414. if ((VGAInfo = malloc(sizeof(*VGAInfo))) == NULL) {
  415. printf("videoboot: Out of memory!\n");
  416. return -ENOMEM;
  417. }
  418. memset(VGAInfo, 0, sizeof(*VGAInfo));
  419. BE_init(0, 65536, VGAInfo, 0);
  420. *vga_infop = VGAInfo;
  421. return 0;
  422. }
  423. void biosemu_set_interrupt_handler(int intnum, int (*int_func)(void))
  424. {
  425. X86EMU_setupIntrFunc(intnum, (X86EMU_intrFuncs)int_func);
  426. }
  427. int biosemu_run(pci_dev_t pcidev, uchar *bios_rom, int bios_len,
  428. BE_VGAInfo *vga_info, int clean_up, int vesa_mode,
  429. struct vbe_mode_info *mode_info)
  430. {
  431. /*Post all the display controller BIOS'es*/
  432. if (!PCI_postController(pcidev, bios_rom, bios_len, vga_info,
  433. vesa_mode, mode_info))
  434. return -EINVAL;
  435. /*
  436. * Cleanup and exit the emulator if requested. If the BIOS emulator
  437. * is needed after booting the card, we will not call BE_exit and
  438. * leave it enabled for further use (ie: VESA driver etc).
  439. */
  440. if (clean_up) {
  441. BE_exit();
  442. if (vga_info->BIOSImage)
  443. free(vga_info->BIOSImage);
  444. free(vga_info);
  445. vga_info = NULL;
  446. }
  447. return 0;
  448. }
  449. /****************************************************************************
  450. PARAMETERS:
  451. pcidev - PCI device info for the video card on the bus to boot
  452. pVGAInfo - Place to return VGA info structure is requested
  453. cleanUp - true to clean up on exit, false to leave emulator active
  454. REMARKS:
  455. Boots the PCI/AGP video card on the bus using the Video ROM BIOS image
  456. and the X86 BIOS emulator module.
  457. ****************************************************************************/
  458. int BootVideoCardBIOS(pci_dev_t pcidev, BE_VGAInfo **pVGAInfo, int clean_up)
  459. {
  460. BE_VGAInfo *VGAInfo;
  461. int ret;
  462. ret = biosemu_setup(pcidev, &VGAInfo);
  463. if (ret)
  464. return false;
  465. ret = biosemu_run(pcidev, NULL, 0, VGAInfo, clean_up, -1, NULL);
  466. if (ret)
  467. return false;
  468. /* Return VGA info pointer if the caller requested it*/
  469. if (pVGAInfo)
  470. *pVGAInfo = VGAInfo;
  471. return true;
  472. }