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