atibios.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /****************************************************************************
  2. *
  3. * Video BOOT Graphics Card POST Module
  4. *
  5. * ========================================================================
  6. * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
  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. #ifdef CONFIG_BIOSEMU
  50. #include "biosemui.h"
  51. #include <malloc.h>
  52. /* Length of the BIOS image */
  53. #define MAX_BIOSLEN (128 * 1024L)
  54. /* Define some useful types and macros */
  55. #define true 1
  56. #define false 0
  57. /* Place to save PCI BAR's that we change and later restore */
  58. static u32 saveROMBaseAddress;
  59. static u32 saveBaseAddress10;
  60. static u32 saveBaseAddress14;
  61. static u32 saveBaseAddress18;
  62. static u32 saveBaseAddress20;
  63. /****************************************************************************
  64. PARAMETERS:
  65. pcidev - PCI device info for the video card on the bus to boot
  66. VGAInfo - BIOS emulator VGA info structure
  67. REMARKS:
  68. This function executes the BIOS POST code on the controller. We assume that
  69. at this stage the controller has its I/O and memory space enabled and
  70. that all other controllers are in a disabled state.
  71. ****************************************************************************/
  72. static void PCI_doBIOSPOST(pci_dev_t pcidev, BE_VGAInfo * VGAInfo)
  73. {
  74. RMREGS regs;
  75. RMSREGS sregs;
  76. /* Determine the value to store in AX for BIOS POST. Per the PCI specs,
  77. AH must contain the bus and AL must contain the devfn, encoded as
  78. (dev << 3) | fn
  79. */
  80. memset(&regs, 0, sizeof(regs));
  81. memset(&sregs, 0, sizeof(sregs));
  82. regs.x.ax = ((int)PCI_BUS(pcidev) << 8) |
  83. ((int)PCI_DEV(pcidev) << 3) | (int)PCI_FUNC(pcidev);
  84. /*Setup the X86 emulator for the VGA BIOS*/
  85. BE_setVGA(VGAInfo);
  86. /*Execute the BIOS POST code*/
  87. BE_callRealMode(0xC000, 0x0003, &regs, &sregs);
  88. /*Cleanup and exit*/
  89. BE_getVGA(VGAInfo);
  90. }
  91. /****************************************************************************
  92. PARAMETERS:
  93. pcidev - PCI device info for the video card on the bus
  94. bar - Place to return the base address register offset to use
  95. RETURNS:
  96. The address to use to map the secondary BIOS (AGP devices)
  97. REMARKS:
  98. Searches all the PCI base address registers for the device looking for a
  99. memory mapping that is large enough to hold our ROM BIOS. We usually end up
  100. finding the framebuffer mapping (usually BAR 0x10), and we use this mapping
  101. to map the BIOS for the device into. We use a mapping that is already
  102. assigned to the device to ensure the memory range will be passed through
  103. by any PCI->PCI or AGP->PCI bridge that may be present.
  104. NOTE: Usually this function is only used for AGP devices, but it may be
  105. used for PCI devices that have already been POST'ed and the BIOS
  106. ROM base address has been zero'ed out.
  107. NOTE: This function leaves the original memory aperture disabled by leaving
  108. it programmed to all 1's. It must be restored to the correct value
  109. later.
  110. ****************************************************************************/
  111. static u32 PCI_findBIOSAddr(pci_dev_t pcidev, int *bar)
  112. {
  113. u32 base, size;
  114. for (*bar = 0x10; *bar <= 0x14; (*bar) += 4) {
  115. pci_read_config_dword(pcidev, *bar, &base);
  116. if (!(base & 0x1)) {
  117. pci_write_config_dword(pcidev, *bar, 0xFFFFFFFF);
  118. pci_read_config_dword(pcidev, *bar, &size);
  119. size = ~(size & ~0xFF) + 1;
  120. if (size >= MAX_BIOSLEN)
  121. return base & ~0xFF;
  122. }
  123. }
  124. return 0;
  125. }
  126. /****************************************************************************
  127. REMARKS:
  128. Some non-x86 Linux kernels map PCI relocateable I/O to values that
  129. are above 64K, which will not work with the BIOS image that requires
  130. the offset for the I/O ports to be a maximum of 16-bits. Ideally
  131. someone should fix the kernel to map the I/O ports for VGA compatible
  132. devices to a different location (or just all I/O ports since it is
  133. unlikely you can have enough devices in the machine to use up all
  134. 64K of the I/O space - a total of more than 256 cards would be
  135. necessary).
  136. Anyway to fix this we change all I/O mapped base registers and
  137. chop off the top bits.
  138. ****************************************************************************/
  139. static void PCI_fixupIObase(pci_dev_t pcidev, int reg, u32 * base)
  140. {
  141. if ((*base & 0x1) && (*base > 0xFFFE)) {
  142. *base &= 0xFFFF;
  143. pci_write_config_dword(pcidev, reg, *base);
  144. }
  145. }
  146. /****************************************************************************
  147. PARAMETERS:
  148. pcidev - PCI device info for the video card on the bus
  149. RETURNS:
  150. Pointers to the mapped BIOS image
  151. REMARKS:
  152. Maps a pointer to the BIOS image on the graphics card on the PCI bus.
  153. ****************************************************************************/
  154. void *PCI_mapBIOSImage(pci_dev_t pcidev)
  155. {
  156. u32 BIOSImagePhys;
  157. int BIOSImageBAR;
  158. u8 *BIOSImage;
  159. /*Save PCI BAR registers that might get changed*/
  160. pci_read_config_dword(pcidev, PCI_ROM_ADDRESS, &saveROMBaseAddress);
  161. pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &saveBaseAddress10);
  162. pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_1, &saveBaseAddress14);
  163. pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_2, &saveBaseAddress18);
  164. pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_4, &saveBaseAddress20);
  165. /*Fix up I/O base registers to less than 64K */
  166. if(saveBaseAddress14 != 0)
  167. PCI_fixupIObase(pcidev, PCI_BASE_ADDRESS_1, &saveBaseAddress14);
  168. else
  169. PCI_fixupIObase(pcidev, PCI_BASE_ADDRESS_4, &saveBaseAddress20);
  170. /* Some cards have problems that stop us from being able to read the
  171. BIOS image from the ROM BAR. To fix this we have to do some chipset
  172. specific programming for different cards to solve this problem.
  173. */
  174. if ((BIOSImagePhys = PCI_findBIOSAddr(pcidev, &BIOSImageBAR)) == 0) {
  175. printf("Find bios addr error\n");
  176. return NULL;
  177. }
  178. BIOSImage = (u8 *) BIOSImagePhys;
  179. /*Change the PCI BAR registers to map it onto the bus.*/
  180. pci_write_config_dword(pcidev, BIOSImageBAR, 0);
  181. pci_write_config_dword(pcidev, PCI_ROM_ADDRESS, BIOSImagePhys | 0x1);
  182. udelay(1);
  183. /*Check that the BIOS image is valid. If not fail, or return the
  184. compiled in BIOS image if that option was enabled
  185. */
  186. if (BIOSImage[0] != 0x55 || BIOSImage[1] != 0xAA || BIOSImage[2] == 0) {
  187. return NULL;
  188. }
  189. return BIOSImage;
  190. }
  191. /****************************************************************************
  192. PARAMETERS:
  193. pcidev - PCI device info for the video card on the bus
  194. REMARKS:
  195. Unmaps the BIOS image for the device and restores framebuffer mappings
  196. ****************************************************************************/
  197. void PCI_unmapBIOSImage(pci_dev_t pcidev, void *BIOSImage)
  198. {
  199. pci_write_config_dword(pcidev, PCI_ROM_ADDRESS, saveROMBaseAddress);
  200. pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_0, saveBaseAddress10);
  201. pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_1, saveBaseAddress14);
  202. pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_2, saveBaseAddress18);
  203. pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_4, saveBaseAddress20);
  204. }
  205. /****************************************************************************
  206. PARAMETERS:
  207. pcidev - PCI device info for the video card on the bus to boot
  208. VGAInfo - BIOS emulator VGA info structure
  209. RETURNS:
  210. True if successfully initialised, false if not.
  211. REMARKS:
  212. Loads and POST's the display controllers BIOS, directly from the BIOS
  213. image we can extract over the PCI bus.
  214. ****************************************************************************/
  215. static int PCI_postController(pci_dev_t pcidev, BE_VGAInfo * VGAInfo)
  216. {
  217. u32 BIOSImageLen;
  218. uchar *mappedBIOS;
  219. uchar *copyOfBIOS;
  220. /*Allocate memory to store copy of BIOS from display controller*/
  221. if ((mappedBIOS = PCI_mapBIOSImage(pcidev)) == NULL) {
  222. printf("videoboot: Video ROM failed to map!\n");
  223. return false;
  224. }
  225. BIOSImageLen = mappedBIOS[2] * 512;
  226. if ((copyOfBIOS = malloc(BIOSImageLen)) == NULL) {
  227. printf("videoboot: Out of memory!\n");
  228. return false;
  229. }
  230. memcpy(copyOfBIOS, mappedBIOS, BIOSImageLen);
  231. PCI_unmapBIOSImage(pcidev, mappedBIOS);
  232. /*Save information in VGAInfo structure*/
  233. VGAInfo->function = PCI_FUNC(pcidev);
  234. VGAInfo->device = PCI_DEV(pcidev);
  235. VGAInfo->bus = PCI_BUS(pcidev);
  236. VGAInfo->pcidev = pcidev;
  237. VGAInfo->BIOSImage = copyOfBIOS;
  238. VGAInfo->BIOSImageLen = BIOSImageLen;
  239. /*Now execute the BIOS POST for the device*/
  240. if (copyOfBIOS[0] != 0x55 || copyOfBIOS[1] != 0xAA) {
  241. printf("videoboot: Video ROM image is invalid!\n");
  242. return false;
  243. }
  244. PCI_doBIOSPOST(pcidev, VGAInfo);
  245. /*Reset the size of the BIOS image to the final size*/
  246. VGAInfo->BIOSImageLen = copyOfBIOS[2] * 512;
  247. return true;
  248. }
  249. /****************************************************************************
  250. PARAMETERS:
  251. pcidev - PCI device info for the video card on the bus to boot
  252. pVGAInfo - Place to return VGA info structure is requested
  253. cleanUp - True to clean up on exit, false to leave emulator active
  254. REMARKS:
  255. Boots the PCI/AGP video card on the bus using the Video ROM BIOS image
  256. and the X86 BIOS emulator module.
  257. ****************************************************************************/
  258. int BootVideoCardBIOS(pci_dev_t pcidev, BE_VGAInfo ** pVGAInfo, int cleanUp)
  259. {
  260. BE_VGAInfo *VGAInfo;
  261. printf("videoboot: Booting PCI video card bus %d, function %d, device %d\n",
  262. PCI_BUS(pcidev), PCI_FUNC(pcidev), PCI_DEV(pcidev));
  263. /*Initialise the x86 BIOS emulator*/
  264. if ((VGAInfo = malloc(sizeof(*VGAInfo))) == NULL) {
  265. printf("videoboot: Out of memory!\n");
  266. return false;
  267. }
  268. memset(VGAInfo, 0, sizeof(*VGAInfo));
  269. BE_init(0, 65536, VGAInfo, 0);
  270. /*Post all the display controller BIOS'es*/
  271. PCI_postController(pcidev, VGAInfo);
  272. /*Cleanup and exit the emulator if requested. If the BIOS emulator
  273. is needed after booting the card, we will not call BE_exit and
  274. leave it enabled for further use (ie: VESA driver etc).
  275. */
  276. if (cleanUp) {
  277. BE_exit();
  278. if (VGAInfo->BIOSImage)
  279. free(VGAInfo->BIOSImage);
  280. free(VGAInfo);
  281. VGAInfo = NULL;
  282. }
  283. /*Return VGA info pointer if the caller requested it*/
  284. if (pVGAInfo)
  285. *pVGAInfo = VGAInfo;
  286. return true;
  287. }
  288. #endif