blob: 1e06759d9eb7edcb7cb7ecdbfdd2162bf163c407 [file] [log] [blame]
wdenkabda5ca2003-05-31 18:35:21 +00001/*
2 * (C) Copyright 2002
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +02003 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
wdenkabda5ca2003-05-31 18:35:21 +00004 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
wdenk57b2d802003-06-27 21:31:46 +000024#include <common.h>
25#include <pci.h>
26#include <malloc.h>
27#include <asm/ptrace.h>
28#include <asm/realmode.h>
29#include <asm/io.h>
30#include <asm/pci.h>
Graeme Russ883c6032011-11-08 02:33:15 +000031#include "bios.h"
wdenkabda5ca2003-05-31 18:35:21 +000032
33#undef PCI_BIOS_DEBUG
34#undef VGA_BIOS_DEBUG
35
36#ifdef VGA_BIOS_DEBUG
Graeme Russ883c6032011-11-08 02:33:15 +000037#define PRINTF(fmt, args...) printf(fmt, ##args)
wdenkabda5ca2003-05-31 18:35:21 +000038#else
Graeme Russ883c6032011-11-08 02:33:15 +000039#define PRINTF(fmt, args...)
wdenkabda5ca2003-05-31 18:35:21 +000040#endif
41
Graeme Russ883c6032011-11-08 02:33:15 +000042#define PCI_CLASS_VIDEO 3
43#define PCI_CLASS_VIDEO_STD 0
44#define PCI_CLASS_VIDEO_PROG_IF_VGA 0
wdenkabda5ca2003-05-31 18:35:21 +000045
Graeme Russ883c6032011-11-08 02:33:15 +000046DEFINE_PCI_DEVICE_TABLE(supported) = {
Graeme Russd0811ab2009-08-23 12:59:50 +100047 {PCI_VIDEO_VENDOR_ID, PCI_VIDEO_DEVICE_ID},
48 {}
49};
wdenkabda5ca2003-05-31 18:35:21 +000050
51static u32 probe_pci_video(void)
52{
Graeme Russ883c6032011-11-08 02:33:15 +000053 struct pci_controller *hose;
54 pci_dev_t devbusfn = pci_find_devices(supported, 0);
wdenk57b2d802003-06-27 21:31:46 +000055
Graeme Russ883c6032011-11-08 02:33:15 +000056 if ((devbusfn != -1)) {
wdenkabda5ca2003-05-31 18:35:21 +000057 u32 old;
58 u32 addr;
wdenk57b2d802003-06-27 21:31:46 +000059
wdenkabda5ca2003-05-31 18:35:21 +000060 /* PCI video device detected */
wdenk57b2d802003-06-27 21:31:46 +000061 printf("Found PCI VGA device at %02x.%02x.%x\n",
Graeme Russ883c6032011-11-08 02:33:15 +000062 PCI_BUS(devbusfn),
63 PCI_DEV(devbusfn),
64 PCI_FUNC(devbusfn));
wdenk57b2d802003-06-27 21:31:46 +000065
wdenkabda5ca2003-05-31 18:35:21 +000066 /* Enable I/O decoding as well, PCI viudeo boards
67 * support I/O accesses, but they provide no
68 * bar register for this since the ports are fixed.
69 */
Graeme Russ883c6032011-11-08 02:33:15 +000070 pci_write_config_word(devbusfn,
71 PCI_COMMAND,
72 PCI_COMMAND_MEMORY |
73 PCI_COMMAND_IO |
74 PCI_COMMAND_MASTER);
wdenk57b2d802003-06-27 21:31:46 +000075
wdenkabda5ca2003-05-31 18:35:21 +000076 /* Test the ROM decoder, do the device support a rom? */
wdenk57b2d802003-06-27 21:31:46 +000077 pci_read_config_dword(devbusfn, PCI_ROM_ADDRESS, &old);
Graeme Russ883c6032011-11-08 02:33:15 +000078 pci_write_config_dword(devbusfn, PCI_ROM_ADDRESS,
79 (u32)PCI_ROM_ADDRESS_MASK);
wdenkabda5ca2003-05-31 18:35:21 +000080 pci_read_config_dword(devbusfn, PCI_ROM_ADDRESS, &addr);
81 pci_write_config_dword(devbusfn, PCI_ROM_ADDRESS, old);
wdenk57b2d802003-06-27 21:31:46 +000082
wdenkabda5ca2003-05-31 18:35:21 +000083 if (!addr) {
84 printf("PCI VGA have no ROM?\n");
85 return 0;
86 }
wdenk57b2d802003-06-27 21:31:46 +000087
wdenkabda5ca2003-05-31 18:35:21 +000088 /* device have a rom */
Graeme Russ883c6032011-11-08 02:33:15 +000089 if (pci_shadow_rom(devbusfn, (void *)0xc0000)) {
wdenkabda5ca2003-05-31 18:35:21 +000090 printf("Shadowing of PCI VGA BIOS failed\n");
91 return 0;
92 }
wdenk57b2d802003-06-27 21:31:46 +000093
wdenkabda5ca2003-05-31 18:35:21 +000094 /* Now enable lagacy VGA port access */
Graeme Russ883c6032011-11-08 02:33:15 +000095 hose = pci_bus_to_hose(PCI_BUS(devbusfn));
96 if (pci_enable_legacy_video_ports(hose)) {
wdenkabda5ca2003-05-31 18:35:21 +000097 printf("PCI VGA enable failed\n");
98 return 0;
99 }
wdenk57b2d802003-06-27 21:31:46 +0000100
101
wdenkabda5ca2003-05-31 18:35:21 +0000102 /* return the pci device info, that we'll need later */
wdenk57b2d802003-06-27 21:31:46 +0000103 return PCI_BUS(devbusfn) << 8 |
Graeme Russ883c6032011-11-08 02:33:15 +0000104 PCI_DEV(devbusfn) << 3 | (PCI_FUNC(devbusfn) & 7);
wdenkabda5ca2003-05-31 18:35:21 +0000105 }
wdenk57b2d802003-06-27 21:31:46 +0000106
wdenkabda5ca2003-05-31 18:35:21 +0000107 return 0;
108}
109
wdenkabda5ca2003-05-31 18:35:21 +0000110static int probe_isa_video(void)
111{
112 u32 ptr;
wdenk57b2d802003-06-27 21:31:46 +0000113 char *buf;
114
Graeme Russ883c6032011-11-08 02:33:15 +0000115 ptr = isa_map_rom(0xc0000, 0x8000);
116
117 if (!ptr)
wdenkabda5ca2003-05-31 18:35:21 +0000118 return -1;
Graeme Russ883c6032011-11-08 02:33:15 +0000119
120 buf = malloc(0x8000);
121 if (!buf) {
wdenkabda5ca2003-05-31 18:35:21 +0000122 isa_unmap_rom(ptr);
123 return -1;
124 }
Graeme Russ883c6032011-11-08 02:33:15 +0000125
wdenkabda5ca2003-05-31 18:35:21 +0000126 if (readw(ptr) != 0xaa55) {
127 free(buf);
128 isa_unmap_rom(ptr);
129 return -1;
130 }
wdenk57b2d802003-06-27 21:31:46 +0000131
wdenkabda5ca2003-05-31 18:35:21 +0000132 /* shadow the rom */
Graeme Russ883c6032011-11-08 02:33:15 +0000133 memcpy(buf, (void *)ptr, 0x8000);
wdenkabda5ca2003-05-31 18:35:21 +0000134 isa_unmap_rom(ptr);
Graeme Russ883c6032011-11-08 02:33:15 +0000135 memcpy((void *)0xc0000, buf, 0x8000);
wdenk57b2d802003-06-27 21:31:46 +0000136
wdenkabda5ca2003-05-31 18:35:21 +0000137 free(buf);
wdenk57b2d802003-06-27 21:31:46 +0000138
wdenkabda5ca2003-05-31 18:35:21 +0000139 return 0;
140}
141
142int video_bios_init(void)
143{
144 struct pt_regs regs;
Graeme Russ883c6032011-11-08 02:33:15 +0000145 int size;
146 int i;
147 u8 sum;
wdenkabda5ca2003-05-31 18:35:21 +0000148
149 /* clear the video bios area in case we warmbooted */
Graeme Russ883c6032011-11-08 02:33:15 +0000150 memset((void *)0xc0000, 0, 0x8000);
wdenkabda5ca2003-05-31 18:35:21 +0000151 memset(&regs, 0, sizeof(struct pt_regs));
wdenk57b2d802003-06-27 21:31:46 +0000152
Graeme Russ883c6032011-11-08 02:33:15 +0000153 if (probe_isa_video())
wdenkabda5ca2003-05-31 18:35:21 +0000154 /* No ISA board found, try the PCI bus */
155 regs.eax = probe_pci_video();
wdenk57b2d802003-06-27 21:31:46 +0000156
wdenkabda5ca2003-05-31 18:35:21 +0000157 /* Did we succeed in mapping any video bios */
158 if (readw(0xc0000) == 0xaa55) {
wdenkabda5ca2003-05-31 18:35:21 +0000159 PRINTF("Found video bios signature\n");
Graeme Russ883c6032011-11-08 02:33:15 +0000160 size = readb(0xc0002) * 512;
wdenkabda5ca2003-05-31 18:35:21 +0000161 PRINTF("size %d\n", size);
Graeme Russ883c6032011-11-08 02:33:15 +0000162 sum = 0;
163
164 for (i = 0; i < size; i++)
wdenkabda5ca2003-05-31 18:35:21 +0000165 sum += readb(0xc0000 + i);
Graeme Russ883c6032011-11-08 02:33:15 +0000166
167 PRINTF("Checksum is %sOK\n", sum ? "NOT " : "");
168
169 if (sum)
wdenkabda5ca2003-05-31 18:35:21 +0000170 return 1;
wdenk57b2d802003-06-27 21:31:46 +0000171
Graeme Russ883c6032011-11-08 02:33:15 +0000172 /*
173 * Some video bioses (ATI Mach64) seem to think that
wdenkabda5ca2003-05-31 18:35:21 +0000174 * the original int 10 handler is always at
175 * 0xf000:0xf065 , place an iret instruction there
176 */
177 writeb(0xcf, 0xff065);
wdenk57b2d802003-06-27 21:31:46 +0000178
wdenkabda5ca2003-05-31 18:35:21 +0000179 regs.esp = 0x8000;
180 regs.xss = 0x2000;
181 enter_realmode(0xc000, 3, &regs, &regs);
Graeme Russ883c6032011-11-08 02:33:15 +0000182
wdenkabda5ca2003-05-31 18:35:21 +0000183 PRINTF("INT 0x10 vector after: %04x:%04x\n",
wdenk57b2d802003-06-27 21:31:46 +0000184 readw(0x42), readw(0x40));
Graeme Russ883c6032011-11-08 02:33:15 +0000185 PRINTF("BIOS returned %scarry\n",
186 regs.eflags & 0x00000001 ? "" : "NOT ");
wdenk57b2d802003-06-27 21:31:46 +0000187#ifdef PCI_BIOS_DEBUG
wdenkabda5ca2003-05-31 18:35:21 +0000188 print_bios_bios_stat();
wdenk57b2d802003-06-27 21:31:46 +0000189#endif
Graeme Russ883c6032011-11-08 02:33:15 +0000190 return regs.eflags & 0x00000001;
wdenk57b2d802003-06-27 21:31:46 +0000191
wdenkabda5ca2003-05-31 18:35:21 +0000192 }
wdenkabda5ca2003-05-31 18:35:21 +0000193
wdenk57b2d802003-06-27 21:31:46 +0000194 return 1;
wdenkabda5ca2003-05-31 18:35:21 +0000195
wdenk57b2d802003-06-27 21:31:46 +0000196}