blob: 3b2ed6e109b645359c0edb9e0fe0f1ddd0b7c507 [file] [log] [blame]
Jason Jinece92f82007-07-06 08:34:56 +08001/****************************************************************************
2*
Wolfgang Denk9c7e4b02007-08-06 02:17:36 +02003* Video BOOT Graphics Card POST Module
Jason Jinece92f82007-07-06 08:34:56 +08004*
5* ========================================================================
Kumar Gala4c2e3da2009-07-28 21:49:52 -05006* Copyright (C) 2007 Freescale Semiconductor, Inc.
Jason Jinece92f82007-07-06 08:34:56 +08007* 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*
Wolfgang Denk9c7e4b02007-08-06 02:17:36 +020033* Language: ANSI C
34* Environment: Linux Kernel
35* Developer: Kendall Bennett
Jason Jinece92f82007-07-06 08:34:56 +080036*
Wolfgang Denk9c7e4b02007-08-06 02:17:36 +020037* 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.
Jason Jinece92f82007-07-06 08:34:56 +080040*
Wolfgang Denk9c7e4b02007-08-06 02:17:36 +020041* Note that at present this module does *not* support
42* multiple controllers.
Jason Jinece92f82007-07-06 08:34:56 +080043*
Wolfgang Denk9c7e4b02007-08-06 02:17:36 +020044* 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.
Jason Jinece92f82007-07-06 08:34:56 +080047****************************************************************************/
48#include <common.h>
Jason Jinece92f82007-07-06 08:34:56 +080049#include "biosemui.h"
50#include <malloc.h>
51
52/* Length of the BIOS image */
Wolfgang Denk9c7e4b02007-08-06 02:17:36 +020053#define MAX_BIOSLEN (128 * 1024L)
Jason Jinece92f82007-07-06 08:34:56 +080054
Jason Jinece92f82007-07-06 08:34:56 +080055/* Place to save PCI BAR's that we change and later restore */
56static u32 saveROMBaseAddress;
57static u32 saveBaseAddress10;
58static u32 saveBaseAddress14;
59static u32 saveBaseAddress18;
60static u32 saveBaseAddress20;
61
62/****************************************************************************
63PARAMETERS:
Wolfgang Denk9c7e4b02007-08-06 02:17:36 +020064pcidev - PCI device info for the video card on the bus to boot
Jason Jinece92f82007-07-06 08:34:56 +080065VGAInfo - BIOS emulator VGA info structure
66
67REMARKS:
68This function executes the BIOS POST code on the controller. We assume that
69at this stage the controller has its I/O and memory space enabled and
70that all other controllers are in a disabled state.
71****************************************************************************/
72static void PCI_doBIOSPOST(pci_dev_t pcidev, BE_VGAInfo * VGAInfo)
73{
74 RMREGS regs;
75 RMSREGS sregs;
76
77 /* Determine the value to store in AX for BIOS POST. Per the PCI specs,
78 AH must contain the bus and AL must contain the devfn, encoded as
79 (dev << 3) | fn
80 */
81 memset(&regs, 0, sizeof(regs));
82 memset(&sregs, 0, sizeof(sregs));
83 regs.x.ax = ((int)PCI_BUS(pcidev) << 8) |
84 ((int)PCI_DEV(pcidev) << 3) | (int)PCI_FUNC(pcidev);
85
86 /*Setup the X86 emulator for the VGA BIOS*/
87 BE_setVGA(VGAInfo);
88
89 /*Execute the BIOS POST code*/
90 BE_callRealMode(0xC000, 0x0003, &regs, &sregs);
91
92 /*Cleanup and exit*/
93 BE_getVGA(VGAInfo);
94}
95
96/****************************************************************************
97PARAMETERS:
Wolfgang Denk9c7e4b02007-08-06 02:17:36 +020098pcidev - PCI device info for the video card on the bus
99bar - Place to return the base address register offset to use
Jason Jinece92f82007-07-06 08:34:56 +0800100
101RETURNS:
102The address to use to map the secondary BIOS (AGP devices)
103
104REMARKS:
105Searches all the PCI base address registers for the device looking for a
106memory mapping that is large enough to hold our ROM BIOS. We usually end up
107finding the framebuffer mapping (usually BAR 0x10), and we use this mapping
108to map the BIOS for the device into. We use a mapping that is already
109assigned to the device to ensure the memory range will be passed through
110by any PCI->PCI or AGP->PCI bridge that may be present.
111
112NOTE: Usually this function is only used for AGP devices, but it may be
113 used for PCI devices that have already been POST'ed and the BIOS
114 ROM base address has been zero'ed out.
115
116NOTE: This function leaves the original memory aperture disabled by leaving
117 it programmed to all 1's. It must be restored to the correct value
118 later.
119****************************************************************************/
120static u32 PCI_findBIOSAddr(pci_dev_t pcidev, int *bar)
121{
122 u32 base, size;
123
124 for (*bar = 0x10; *bar <= 0x14; (*bar) += 4) {
125 pci_read_config_dword(pcidev, *bar, &base);
126 if (!(base & 0x1)) {
127 pci_write_config_dword(pcidev, *bar, 0xFFFFFFFF);
128 pci_read_config_dword(pcidev, *bar, &size);
129 size = ~(size & ~0xFF) + 1;
130 if (size >= MAX_BIOSLEN)
131 return base & ~0xFF;
132 }
133 }
134 return 0;
135}
136
137/****************************************************************************
138REMARKS:
139Some non-x86 Linux kernels map PCI relocateable I/O to values that
140are above 64K, which will not work with the BIOS image that requires
141the offset for the I/O ports to be a maximum of 16-bits. Ideally
142someone should fix the kernel to map the I/O ports for VGA compatible
143devices to a different location (or just all I/O ports since it is
144unlikely you can have enough devices in the machine to use up all
14564K of the I/O space - a total of more than 256 cards would be
146necessary).
147
148Anyway to fix this we change all I/O mapped base registers and
149chop off the top bits.
150****************************************************************************/
151static void PCI_fixupIObase(pci_dev_t pcidev, int reg, u32 * base)
152{
153 if ((*base & 0x1) && (*base > 0xFFFE)) {
154 *base &= 0xFFFF;
155 pci_write_config_dword(pcidev, reg, *base);
156
157 }
158}
159
160/****************************************************************************
161PARAMETERS:
Wolfgang Denk9c7e4b02007-08-06 02:17:36 +0200162pcidev - PCI device info for the video card on the bus
Jason Jinece92f82007-07-06 08:34:56 +0800163
164RETURNS:
165Pointers to the mapped BIOS image
166
167REMARKS:
168Maps a pointer to the BIOS image on the graphics card on the PCI bus.
169****************************************************************************/
170void *PCI_mapBIOSImage(pci_dev_t pcidev)
171{
Ed Swarthoutf6a7a2e2010-03-31 15:52:40 -0500172 u32 BIOSImageBus;
Jason Jinece92f82007-07-06 08:34:56 +0800173 int BIOSImageBAR;
174 u8 *BIOSImage;
175
176 /*Save PCI BAR registers that might get changed*/
177 pci_read_config_dword(pcidev, PCI_ROM_ADDRESS, &saveROMBaseAddress);
178 pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &saveBaseAddress10);
179 pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_1, &saveBaseAddress14);
180 pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_2, &saveBaseAddress18);
181 pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_4, &saveBaseAddress20);
182
183 /*Fix up I/O base registers to less than 64K */
184 if(saveBaseAddress14 != 0)
185 PCI_fixupIObase(pcidev, PCI_BASE_ADDRESS_1, &saveBaseAddress14);
186 else
187 PCI_fixupIObase(pcidev, PCI_BASE_ADDRESS_4, &saveBaseAddress20);
188
189 /* Some cards have problems that stop us from being able to read the
190 BIOS image from the ROM BAR. To fix this we have to do some chipset
191 specific programming for different cards to solve this problem.
Wolfgang Denk9c7e4b02007-08-06 02:17:36 +0200192 */
Jason Jinece92f82007-07-06 08:34:56 +0800193
Ed Swarthoutf6a7a2e2010-03-31 15:52:40 -0500194 BIOSImageBus = PCI_findBIOSAddr(pcidev, &BIOSImageBAR);
195 if (BIOSImageBus == 0) {
Jason Jinece92f82007-07-06 08:34:56 +0800196 printf("Find bios addr error\n");
197 return NULL;
198 }
199
Ed Swarthoutf6a7a2e2010-03-31 15:52:40 -0500200 BIOSImage = pci_bus_to_virt(pcidev, BIOSImageBus,
201 PCI_REGION_MEM, 0, MAP_NOCACHE);
Jason Jinece92f82007-07-06 08:34:56 +0800202
203 /*Change the PCI BAR registers to map it onto the bus.*/
204 pci_write_config_dword(pcidev, BIOSImageBAR, 0);
Ed Swarthoutf6a7a2e2010-03-31 15:52:40 -0500205 pci_write_config_dword(pcidev, PCI_ROM_ADDRESS, BIOSImageBus | 0x1);
Jason Jinece92f82007-07-06 08:34:56 +0800206
207 udelay(1);
208
209 /*Check that the BIOS image is valid. If not fail, or return the
210 compiled in BIOS image if that option was enabled
211 */
212 if (BIOSImage[0] != 0x55 || BIOSImage[1] != 0xAA || BIOSImage[2] == 0) {
213 return NULL;
214 }
215
216 return BIOSImage;
217}
218
219/****************************************************************************
220PARAMETERS:
Wolfgang Denk9c7e4b02007-08-06 02:17:36 +0200221pcidev - PCI device info for the video card on the bus
Jason Jinece92f82007-07-06 08:34:56 +0800222
223REMARKS:
224Unmaps the BIOS image for the device and restores framebuffer mappings
225****************************************************************************/
226void PCI_unmapBIOSImage(pci_dev_t pcidev, void *BIOSImage)
227{
228 pci_write_config_dword(pcidev, PCI_ROM_ADDRESS, saveROMBaseAddress);
229 pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_0, saveBaseAddress10);
230 pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_1, saveBaseAddress14);
231 pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_2, saveBaseAddress18);
232 pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_4, saveBaseAddress20);
233}
234
235/****************************************************************************
236PARAMETERS:
Wolfgang Denk9c7e4b02007-08-06 02:17:36 +0200237pcidev - PCI device info for the video card on the bus to boot
Jason Jinece92f82007-07-06 08:34:56 +0800238VGAInfo - BIOS emulator VGA info structure
239
240RETURNS:
York Sun472d5462013-04-01 11:29:11 -0700241true if successfully initialised, false if not.
Jason Jinece92f82007-07-06 08:34:56 +0800242
243REMARKS:
244Loads and POST's the display controllers BIOS, directly from the BIOS
245image we can extract over the PCI bus.
246****************************************************************************/
247static int PCI_postController(pci_dev_t pcidev, BE_VGAInfo * VGAInfo)
248{
249 u32 BIOSImageLen;
250 uchar *mappedBIOS;
251 uchar *copyOfBIOS;
252
253 /*Allocate memory to store copy of BIOS from display controller*/
254 if ((mappedBIOS = PCI_mapBIOSImage(pcidev)) == NULL) {
255 printf("videoboot: Video ROM failed to map!\n");
256 return false;
257 }
258
259 BIOSImageLen = mappedBIOS[2] * 512;
260
261 if ((copyOfBIOS = malloc(BIOSImageLen)) == NULL) {
262 printf("videoboot: Out of memory!\n");
263 return false;
264 }
265 memcpy(copyOfBIOS, mappedBIOS, BIOSImageLen);
266
267 PCI_unmapBIOSImage(pcidev, mappedBIOS);
268
269 /*Save information in VGAInfo structure*/
270 VGAInfo->function = PCI_FUNC(pcidev);
271 VGAInfo->device = PCI_DEV(pcidev);
272 VGAInfo->bus = PCI_BUS(pcidev);
273 VGAInfo->pcidev = pcidev;
274 VGAInfo->BIOSImage = copyOfBIOS;
275 VGAInfo->BIOSImageLen = BIOSImageLen;
276
277 /*Now execute the BIOS POST for the device*/
278 if (copyOfBIOS[0] != 0x55 || copyOfBIOS[1] != 0xAA) {
279 printf("videoboot: Video ROM image is invalid!\n");
280 return false;
281 }
282
283 PCI_doBIOSPOST(pcidev, VGAInfo);
284
285 /*Reset the size of the BIOS image to the final size*/
286 VGAInfo->BIOSImageLen = copyOfBIOS[2] * 512;
287 return true;
288}
289
290/****************************************************************************
291PARAMETERS:
Wolfgang Denk9c7e4b02007-08-06 02:17:36 +0200292pcidev - PCI device info for the video card on the bus to boot
Jason Jinece92f82007-07-06 08:34:56 +0800293pVGAInfo - Place to return VGA info structure is requested
York Sun472d5462013-04-01 11:29:11 -0700294cleanUp - true to clean up on exit, false to leave emulator active
Jason Jinece92f82007-07-06 08:34:56 +0800295
296REMARKS:
297Boots the PCI/AGP video card on the bus using the Video ROM BIOS image
298and the X86 BIOS emulator module.
299****************************************************************************/
300int BootVideoCardBIOS(pci_dev_t pcidev, BE_VGAInfo ** pVGAInfo, int cleanUp)
301{
302 BE_VGAInfo *VGAInfo;
303
304 printf("videoboot: Booting PCI video card bus %d, function %d, device %d\n",
305 PCI_BUS(pcidev), PCI_FUNC(pcidev), PCI_DEV(pcidev));
306
307 /*Initialise the x86 BIOS emulator*/
308 if ((VGAInfo = malloc(sizeof(*VGAInfo))) == NULL) {
309 printf("videoboot: Out of memory!\n");
310 return false;
311 }
312 memset(VGAInfo, 0, sizeof(*VGAInfo));
313 BE_init(0, 65536, VGAInfo, 0);
314
315 /*Post all the display controller BIOS'es*/
Ed Swarthout9624f6d2010-03-31 09:54:28 -0500316 if (!PCI_postController(pcidev, VGAInfo))
317 return false;
Jason Jinece92f82007-07-06 08:34:56 +0800318
319 /*Cleanup and exit the emulator if requested. If the BIOS emulator
320 is needed after booting the card, we will not call BE_exit and
321 leave it enabled for further use (ie: VESA driver etc).
322 */
323 if (cleanUp) {
324 BE_exit();
325 if (VGAInfo->BIOSImage)
326 free(VGAInfo->BIOSImage);
327 free(VGAInfo);
328 VGAInfo = NULL;
329 }
330 /*Return VGA info pointer if the caller requested it*/
331 if (pVGAInfo)
332 *pVGAInfo = VGAInfo;
333 return true;
334}