blob: 93b815ccb497d7c81eb0491a441fa082e3c703f6 [file] [log] [blame]
Jason Jina63ce952007-07-06 08:34:56 +08001/****************************************************************************
2*
Wolfgang Denk96bb2e02007-08-06 02:17:36 +02003* Video BOOT Graphics Card POST Module
Jason Jina63ce952007-07-06 08:34:56 +08004*
5* ========================================================================
Kumar Gala6a6d9482009-07-28 21:49:52 -05006* Copyright (C) 2007 Freescale Semiconductor, Inc.
Jason Jina63ce952007-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 Denk96bb2e02007-08-06 02:17:36 +020033* Language: ANSI C
34* Environment: Linux Kernel
35* Developer: Kendall Bennett
Jason Jina63ce952007-07-06 08:34:56 +080036*
Wolfgang Denk96bb2e02007-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 Jina63ce952007-07-06 08:34:56 +080040*
Wolfgang Denk96bb2e02007-08-06 02:17:36 +020041* Note that at present this module does *not* support
42* multiple controllers.
Jason Jina63ce952007-07-06 08:34:56 +080043*
Wolfgang Denk96bb2e02007-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 Jina63ce952007-07-06 08:34:56 +080047****************************************************************************/
48#include <common.h>
Simon Glassacda1cc2014-11-14 20:56:40 -070049#include <bios_emul.h>
50#include <errno.h>
Jason Jina63ce952007-07-06 08:34:56 +080051#include <malloc.h>
Simon Glassacda1cc2014-11-14 20:56:40 -070052#include <vbe.h>
53#include "biosemui.h"
Jason Jina63ce952007-07-06 08:34:56 +080054
55/* Length of the BIOS image */
Wolfgang Denk96bb2e02007-08-06 02:17:36 +020056#define MAX_BIOSLEN (128 * 1024L)
Jason Jina63ce952007-07-06 08:34:56 +080057
Jason Jina63ce952007-07-06 08:34:56 +080058/* Place to save PCI BAR's that we change and later restore */
59static u32 saveROMBaseAddress;
60static u32 saveBaseAddress10;
61static u32 saveBaseAddress14;
62static u32 saveBaseAddress18;
63static u32 saveBaseAddress20;
64
Simon Glassacda1cc2014-11-14 20:56:40 -070065static void atibios_set_vesa_mode(RMREGS *regs, int vesa_mode,
66 struct vbe_mode_info *mode_info)
67{
68 debug("VBE: Setting VESA mode %#04x\n", vesa_mode);
69 /* request linear framebuffer mode */
70 vesa_mode |= (1 << 14);
71 /* request clearing of framebuffer */
72 vesa_mode &= ~(1 << 15);
73 regs->e.eax = VESA_SET_MODE;
74 regs->e.ebx = vesa_mode;
75 BE_int86(0x10, regs, regs);
76
77 int offset = 0x2000;
78 void *buffer = (void *)(M.mem_base + offset);
79
80 u16 buffer_seg = (((unsigned long)offset) >> 4) & 0xff00;
81 u16 buffer_adr = ((unsigned long)offset) & 0xffff;
82 regs->e.eax = VESA_GET_MODE_INFO;
83 regs->e.ebx = 0;
84 regs->e.ecx = vesa_mode;
85 regs->e.edx = 0;
86 regs->e.esi = buffer_seg;
87 regs->e.edi = buffer_adr;
88 BE_int86(0x10, regs, regs);
89 memcpy(mode_info->mode_info_block, buffer,
90 sizeof(struct vbe_mode_info));
91 mode_info->valid = true;
92
93 vesa_mode |= (1 << 14);
94 /* request clearing of framebuffer */
95 vesa_mode &= ~(1 << 15);
96 regs->e.eax = VESA_SET_MODE;
97 regs->e.ebx = vesa_mode;
98 BE_int86(0x10, regs, regs);
99}
100
Jason Jina63ce952007-07-06 08:34:56 +0800101/****************************************************************************
102PARAMETERS:
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200103pcidev - PCI device info for the video card on the bus to boot
Simon Glassacda1cc2014-11-14 20:56:40 -0700104vga_info - BIOS emulator VGA info structure
Jason Jina63ce952007-07-06 08:34:56 +0800105
106REMARKS:
107This function executes the BIOS POST code on the controller. We assume that
108at this stage the controller has its I/O and memory space enabled and
109that all other controllers are in a disabled state.
110****************************************************************************/
Simon Glassacda1cc2014-11-14 20:56:40 -0700111static void PCI_doBIOSPOST(pci_dev_t pcidev, BE_VGAInfo *vga_info,
112 int vesa_mode, struct vbe_mode_info *mode_info)
Jason Jina63ce952007-07-06 08:34:56 +0800113{
114 RMREGS regs;
115 RMSREGS sregs;
116
117 /* Determine the value to store in AX for BIOS POST. Per the PCI specs,
118 AH must contain the bus and AL must contain the devfn, encoded as
119 (dev << 3) | fn
120 */
121 memset(&regs, 0, sizeof(regs));
122 memset(&sregs, 0, sizeof(sregs));
123 regs.x.ax = ((int)PCI_BUS(pcidev) << 8) |
124 ((int)PCI_DEV(pcidev) << 3) | (int)PCI_FUNC(pcidev);
125
126 /*Setup the X86 emulator for the VGA BIOS*/
Simon Glassacda1cc2014-11-14 20:56:40 -0700127 BE_setVGA(vga_info);
Jason Jina63ce952007-07-06 08:34:56 +0800128
129 /*Execute the BIOS POST code*/
130 BE_callRealMode(0xC000, 0x0003, &regs, &sregs);
131
132 /*Cleanup and exit*/
Simon Glassacda1cc2014-11-14 20:56:40 -0700133 BE_getVGA(vga_info);
134
135 if (vesa_mode != -1)
136 atibios_set_vesa_mode(&regs, vesa_mode, mode_info);
Jason Jina63ce952007-07-06 08:34:56 +0800137}
138
139/****************************************************************************
140PARAMETERS:
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200141pcidev - PCI device info for the video card on the bus
142bar - Place to return the base address register offset to use
Jason Jina63ce952007-07-06 08:34:56 +0800143
144RETURNS:
145The address to use to map the secondary BIOS (AGP devices)
146
147REMARKS:
148Searches all the PCI base address registers for the device looking for a
149memory mapping that is large enough to hold our ROM BIOS. We usually end up
150finding the framebuffer mapping (usually BAR 0x10), and we use this mapping
151to map the BIOS for the device into. We use a mapping that is already
152assigned to the device to ensure the memory range will be passed through
153by any PCI->PCI or AGP->PCI bridge that may be present.
154
155NOTE: Usually this function is only used for AGP devices, but it may be
156 used for PCI devices that have already been POST'ed and the BIOS
157 ROM base address has been zero'ed out.
158
159NOTE: This function leaves the original memory aperture disabled by leaving
160 it programmed to all 1's. It must be restored to the correct value
161 later.
162****************************************************************************/
163static u32 PCI_findBIOSAddr(pci_dev_t pcidev, int *bar)
164{
165 u32 base, size;
166
167 for (*bar = 0x10; *bar <= 0x14; (*bar) += 4) {
168 pci_read_config_dword(pcidev, *bar, &base);
169 if (!(base & 0x1)) {
170 pci_write_config_dword(pcidev, *bar, 0xFFFFFFFF);
171 pci_read_config_dword(pcidev, *bar, &size);
172 size = ~(size & ~0xFF) + 1;
173 if (size >= MAX_BIOSLEN)
174 return base & ~0xFF;
175 }
176 }
177 return 0;
178}
179
180/****************************************************************************
181REMARKS:
182Some non-x86 Linux kernels map PCI relocateable I/O to values that
183are above 64K, which will not work with the BIOS image that requires
184the offset for the I/O ports to be a maximum of 16-bits. Ideally
185someone should fix the kernel to map the I/O ports for VGA compatible
186devices to a different location (or just all I/O ports since it is
187unlikely you can have enough devices in the machine to use up all
18864K of the I/O space - a total of more than 256 cards would be
189necessary).
190
191Anyway to fix this we change all I/O mapped base registers and
192chop off the top bits.
193****************************************************************************/
194static void PCI_fixupIObase(pci_dev_t pcidev, int reg, u32 * base)
195{
196 if ((*base & 0x1) && (*base > 0xFFFE)) {
197 *base &= 0xFFFF;
198 pci_write_config_dword(pcidev, reg, *base);
199
200 }
201}
202
203/****************************************************************************
204PARAMETERS:
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200205pcidev - PCI device info for the video card on the bus
Jason Jina63ce952007-07-06 08:34:56 +0800206
207RETURNS:
208Pointers to the mapped BIOS image
209
210REMARKS:
211Maps a pointer to the BIOS image on the graphics card on the PCI bus.
212****************************************************************************/
213void *PCI_mapBIOSImage(pci_dev_t pcidev)
214{
Ed Swarthout224dad22010-03-31 15:52:40 -0500215 u32 BIOSImageBus;
Jason Jina63ce952007-07-06 08:34:56 +0800216 int BIOSImageBAR;
217 u8 *BIOSImage;
218
219 /*Save PCI BAR registers that might get changed*/
220 pci_read_config_dword(pcidev, PCI_ROM_ADDRESS, &saveROMBaseAddress);
221 pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &saveBaseAddress10);
222 pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_1, &saveBaseAddress14);
223 pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_2, &saveBaseAddress18);
224 pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_4, &saveBaseAddress20);
225
226 /*Fix up I/O base registers to less than 64K */
227 if(saveBaseAddress14 != 0)
228 PCI_fixupIObase(pcidev, PCI_BASE_ADDRESS_1, &saveBaseAddress14);
229 else
230 PCI_fixupIObase(pcidev, PCI_BASE_ADDRESS_4, &saveBaseAddress20);
231
232 /* Some cards have problems that stop us from being able to read the
233 BIOS image from the ROM BAR. To fix this we have to do some chipset
234 specific programming for different cards to solve this problem.
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200235 */
Jason Jina63ce952007-07-06 08:34:56 +0800236
Ed Swarthout224dad22010-03-31 15:52:40 -0500237 BIOSImageBus = PCI_findBIOSAddr(pcidev, &BIOSImageBAR);
238 if (BIOSImageBus == 0) {
Jason Jina63ce952007-07-06 08:34:56 +0800239 printf("Find bios addr error\n");
240 return NULL;
241 }
242
Ed Swarthout224dad22010-03-31 15:52:40 -0500243 BIOSImage = pci_bus_to_virt(pcidev, BIOSImageBus,
244 PCI_REGION_MEM, 0, MAP_NOCACHE);
Jason Jina63ce952007-07-06 08:34:56 +0800245
246 /*Change the PCI BAR registers to map it onto the bus.*/
247 pci_write_config_dword(pcidev, BIOSImageBAR, 0);
Ed Swarthout224dad22010-03-31 15:52:40 -0500248 pci_write_config_dword(pcidev, PCI_ROM_ADDRESS, BIOSImageBus | 0x1);
Jason Jina63ce952007-07-06 08:34:56 +0800249
250 udelay(1);
251
252 /*Check that the BIOS image is valid. If not fail, or return the
253 compiled in BIOS image if that option was enabled
254 */
255 if (BIOSImage[0] != 0x55 || BIOSImage[1] != 0xAA || BIOSImage[2] == 0) {
256 return NULL;
257 }
258
259 return BIOSImage;
260}
261
262/****************************************************************************
263PARAMETERS:
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200264pcidev - PCI device info for the video card on the bus
Jason Jina63ce952007-07-06 08:34:56 +0800265
266REMARKS:
267Unmaps the BIOS image for the device and restores framebuffer mappings
268****************************************************************************/
269void PCI_unmapBIOSImage(pci_dev_t pcidev, void *BIOSImage)
270{
271 pci_write_config_dword(pcidev, PCI_ROM_ADDRESS, saveROMBaseAddress);
272 pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_0, saveBaseAddress10);
273 pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_1, saveBaseAddress14);
274 pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_2, saveBaseAddress18);
275 pci_write_config_dword(pcidev, PCI_BASE_ADDRESS_4, saveBaseAddress20);
276}
277
278/****************************************************************************
279PARAMETERS:
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200280pcidev - PCI device info for the video card on the bus to boot
Jason Jina63ce952007-07-06 08:34:56 +0800281VGAInfo - BIOS emulator VGA info structure
282
283RETURNS:
York Sun4a598092013-04-01 11:29:11 -0700284true if successfully initialised, false if not.
Jason Jina63ce952007-07-06 08:34:56 +0800285
286REMARKS:
287Loads and POST's the display controllers BIOS, directly from the BIOS
288image we can extract over the PCI bus.
289****************************************************************************/
Simon Glassacda1cc2014-11-14 20:56:40 -0700290static int PCI_postController(pci_dev_t pcidev, uchar *bios_rom, int bios_len,
291 BE_VGAInfo *vga_info, int vesa_mode,
292 struct vbe_mode_info *mode_info)
Jason Jina63ce952007-07-06 08:34:56 +0800293{
Simon Glassacda1cc2014-11-14 20:56:40 -0700294 u32 bios_image_len;
295 uchar *mapped_bios;
296 uchar *copy_of_bios;
Jason Jina63ce952007-07-06 08:34:56 +0800297
Simon Glassacda1cc2014-11-14 20:56:40 -0700298 if (bios_rom) {
299 copy_of_bios = bios_rom;
300 bios_image_len = bios_len;
301 } else {
302 /*
303 * Allocate memory to store copy of BIOS from display
304 * controller
305 */
306 mapped_bios = PCI_mapBIOSImage(pcidev);
307 if (mapped_bios == NULL) {
308 printf("videoboot: Video ROM failed to map!\n");
309 return false;
310 }
Jason Jina63ce952007-07-06 08:34:56 +0800311
Simon Glassacda1cc2014-11-14 20:56:40 -0700312 bios_image_len = mapped_bios[2] * 512;
Jason Jina63ce952007-07-06 08:34:56 +0800313
Simon Glassacda1cc2014-11-14 20:56:40 -0700314 copy_of_bios = malloc(bios_image_len);
315 if (copy_of_bios == NULL) {
316 printf("videoboot: Out of memory!\n");
317 return false;
318 }
319 memcpy(copy_of_bios, mapped_bios, bios_image_len);
320 PCI_unmapBIOSImage(pcidev, mapped_bios);
Jason Jina63ce952007-07-06 08:34:56 +0800321 }
Jason Jina63ce952007-07-06 08:34:56 +0800322
Simon Glassacda1cc2014-11-14 20:56:40 -0700323 /*Save information in vga_info structure*/
324 vga_info->function = PCI_FUNC(pcidev);
325 vga_info->device = PCI_DEV(pcidev);
326 vga_info->bus = PCI_BUS(pcidev);
327 vga_info->pcidev = pcidev;
328 vga_info->BIOSImage = copy_of_bios;
329 vga_info->BIOSImageLen = bios_image_len;
Jason Jina63ce952007-07-06 08:34:56 +0800330
331 /*Now execute the BIOS POST for the device*/
Simon Glassacda1cc2014-11-14 20:56:40 -0700332 if (copy_of_bios[0] != 0x55 || copy_of_bios[1] != 0xAA) {
Jason Jina63ce952007-07-06 08:34:56 +0800333 printf("videoboot: Video ROM image is invalid!\n");
334 return false;
335 }
336
Simon Glassacda1cc2014-11-14 20:56:40 -0700337 PCI_doBIOSPOST(pcidev, vga_info, vesa_mode, mode_info);
Jason Jina63ce952007-07-06 08:34:56 +0800338
339 /*Reset the size of the BIOS image to the final size*/
Simon Glassacda1cc2014-11-14 20:56:40 -0700340 vga_info->BIOSImageLen = copy_of_bios[2] * 512;
Jason Jina63ce952007-07-06 08:34:56 +0800341 return true;
342}
343
Simon Glassacda1cc2014-11-14 20:56:40 -0700344int biosemu_setup(pci_dev_t pcidev, BE_VGAInfo **vga_infop)
Jason Jina63ce952007-07-06 08:34:56 +0800345{
346 BE_VGAInfo *VGAInfo;
347
348 printf("videoboot: Booting PCI video card bus %d, function %d, device %d\n",
349 PCI_BUS(pcidev), PCI_FUNC(pcidev), PCI_DEV(pcidev));
350
351 /*Initialise the x86 BIOS emulator*/
352 if ((VGAInfo = malloc(sizeof(*VGAInfo))) == NULL) {
353 printf("videoboot: Out of memory!\n");
Simon Glassacda1cc2014-11-14 20:56:40 -0700354 return -ENOMEM;
Jason Jina63ce952007-07-06 08:34:56 +0800355 }
356 memset(VGAInfo, 0, sizeof(*VGAInfo));
357 BE_init(0, 65536, VGAInfo, 0);
Simon Glassacda1cc2014-11-14 20:56:40 -0700358 *vga_infop = VGAInfo;
Jason Jina63ce952007-07-06 08:34:56 +0800359
Simon Glassacda1cc2014-11-14 20:56:40 -0700360 return 0;
361}
362
363void biosemu_set_interrupt_handler(int intnum, int (*int_func)(void))
364{
365 X86EMU_setupIntrFunc(intnum, (X86EMU_intrFuncs)int_func);
366}
367
368int biosemu_run(pci_dev_t pcidev, uchar *bios_rom, int bios_len,
369 BE_VGAInfo *vga_info, int clean_up, int vesa_mode,
370 struct vbe_mode_info *mode_info)
371{
Jason Jina63ce952007-07-06 08:34:56 +0800372 /*Post all the display controller BIOS'es*/
Simon Glassacda1cc2014-11-14 20:56:40 -0700373 if (!PCI_postController(pcidev, bios_rom, bios_len, vga_info,
374 vesa_mode, mode_info))
375 return -EINVAL;
Jason Jina63ce952007-07-06 08:34:56 +0800376
Simon Glassacda1cc2014-11-14 20:56:40 -0700377 /*
378 * Cleanup and exit the emulator if requested. If the BIOS emulator
379 * is needed after booting the card, we will not call BE_exit and
380 * leave it enabled for further use (ie: VESA driver etc).
Jason Jina63ce952007-07-06 08:34:56 +0800381 */
Simon Glassacda1cc2014-11-14 20:56:40 -0700382 if (clean_up) {
Jason Jina63ce952007-07-06 08:34:56 +0800383 BE_exit();
Simon Glassacda1cc2014-11-14 20:56:40 -0700384 if (vga_info->BIOSImage)
385 free(vga_info->BIOSImage);
386 free(vga_info);
387 vga_info = NULL;
Jason Jina63ce952007-07-06 08:34:56 +0800388 }
Simon Glassacda1cc2014-11-14 20:56:40 -0700389
390 return 0;
391}
392
393/****************************************************************************
394PARAMETERS:
395pcidev - PCI device info for the video card on the bus to boot
396pVGAInfo - Place to return VGA info structure is requested
397cleanUp - true to clean up on exit, false to leave emulator active
398
399REMARKS:
400Boots the PCI/AGP video card on the bus using the Video ROM BIOS image
401and the X86 BIOS emulator module.
402****************************************************************************/
403int BootVideoCardBIOS(pci_dev_t pcidev, BE_VGAInfo **pVGAInfo, int clean_up)
404{
405 BE_VGAInfo *VGAInfo;
406 int ret;
407
408 ret = biosemu_setup(pcidev, &VGAInfo);
409 if (ret)
410 return false;
411 ret = biosemu_run(pcidev, NULL, 0, VGAInfo, clean_up, -1, NULL);
412 if (ret)
413 return false;
414
415 /* Return VGA info pointer if the caller requested it*/
Jason Jina63ce952007-07-06 08:34:56 +0800416 if (pVGAInfo)
417 *pVGAInfo = VGAInfo;
Simon Glassacda1cc2014-11-14 20:56:40 -0700418
Jason Jina63ce952007-07-06 08:34:56 +0800419 return true;
420}