blob: c8060e60a7766e50b272d206afea7bbd7b418174 [file] [log] [blame]
wdenk7a8e9bed2003-05-31 18:35:21 +00001/*
2 * (C) Copyright 2002
3 * Daniel Engström, Omicron Ceti AB, daniel@omicron.se
4 *
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
wdenk8bde7f72003-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>
wdenk7a8e9bed2003-05-31 18:35:21 +000031
32#undef PCI_BIOS_DEBUG
33#undef VGA_BIOS_DEBUG
34
35#ifdef VGA_BIOS_DEBUG
36#define PRINTF(fmt,args...) printf (fmt ,##args)
37#else
38#define PRINTF(fmt,args...)
39#endif
40
41#ifdef CONFIG_PCI
42
43#ifdef PCI_BIOS_DEBUG
wdenk8bde7f72003-06-27 21:31:46 +000044#define RELOC_16(seg, off) *(u32*)(seg << 4 | (u32)&off)
wdenk7a8e9bed2003-05-31 18:35:21 +000045extern u32 num_pci_bios_present;
46extern u32 num_pci_bios_find_device;
47extern u32 num_pci_bios_find_class;
48extern u32 num_pci_bios_generate_special_cycle;
49extern u32 num_pci_bios_read_cfg_byte;
50extern u32 num_pci_bios_read_cfg_word;
51extern u32 num_pci_bios_read_cfg_dword;
52extern u32 num_pci_bios_write_cfg_byte;
53extern u32 num_pci_bios_write_cfg_word;
54extern u32 num_pci_bios_write_cfg_dword;
55extern u32 num_pci_bios_get_irq_routing;
56extern u32 num_pci_bios_set_irq;
57extern u32 num_pci_bios_unknown_function;
58
59void print_bios_bios_stat(void)
60{
61 printf("16 bit functions:\n");
62 printf("pci_bios_present: %d\n", RELOC_16(0xf000, num_pci_bios_present));
63 printf("pci_bios_find_device: %d\n", RELOC_16(0xf000, num_pci_bios_find_device));
64 printf("pci_bios_find_class: %d\n", RELOC_16(0xf000, num_pci_bios_find_class));
wdenk8bde7f72003-06-27 21:31:46 +000065 printf("pci_bios_generate_special_cycle: %d\n", RELOC_16(0xf000, num_pci_bios_generate_special_cycle));
wdenk7a8e9bed2003-05-31 18:35:21 +000066 printf("pci_bios_read_cfg_byte: %d\n", RELOC_16(0xf000, num_pci_bios_read_cfg_byte));
67 printf("pci_bios_read_cfg_word: %d\n", RELOC_16(0xf000, num_pci_bios_read_cfg_word));
68 printf("pci_bios_read_cfg_dword: %d\n", RELOC_16(0xf000, num_pci_bios_read_cfg_dword));
69 printf("pci_bios_write_cfg_byte: %d\n", RELOC_16(0xf000, num_pci_bios_write_cfg_byte));
70 printf("pci_bios_write_cfg_word: %d\n", RELOC_16(0xf000, num_pci_bios_write_cfg_word));
71 printf("pci_bios_write_cfg_dword: %d\n", RELOC_16(0xf000, num_pci_bios_write_cfg_dword));
72 printf("pci_bios_get_irq_routing: %d\n", RELOC_16(0xf000, num_pci_bios_get_irq_routing));
73 printf("pci_bios_set_irq: %d\n", RELOC_16(0xf000, num_pci_bios_set_irq));
74 printf("pci_bios_unknown_function: %d\n", RELOC_16(0xf000, num_pci_bios_unknown_function));
75
76}
77#endif
78
Graeme Russd7549022009-08-23 12:59:50 +100079#ifdef CONFIG_VIDEO
80
wdenk7a8e9bed2003-05-31 18:35:21 +000081#define PCI_CLASS_VIDEO 3
82#define PCI_CLASS_VIDEO_STD 0
83#define PCI_CLASS_VIDEO_PROG_IF_VGA 0
84
Graeme Russd7549022009-08-23 12:59:50 +100085static struct pci_device_id supported[] = {
86 {PCI_VIDEO_VENDOR_ID, PCI_VIDEO_DEVICE_ID},
87 {}
88};
wdenk7a8e9bed2003-05-31 18:35:21 +000089
90static u32 probe_pci_video(void)
91{
92 pci_dev_t devbusfn;
wdenk8bde7f72003-06-27 21:31:46 +000093
Graeme Russd7549022009-08-23 12:59:50 +100094 if ((devbusfn = pci_find_devices(supported, 0) != -1)) {
wdenk7a8e9bed2003-05-31 18:35:21 +000095 u32 old;
96 u32 addr;
wdenk8bde7f72003-06-27 21:31:46 +000097
wdenk7a8e9bed2003-05-31 18:35:21 +000098 /* PCI video device detected */
wdenk8bde7f72003-06-27 21:31:46 +000099 printf("Found PCI VGA device at %02x.%02x.%x\n",
wdenk7a8e9bed2003-05-31 18:35:21 +0000100 PCI_BUS(devbusfn), PCI_DEV(devbusfn), PCI_FUNC(devbusfn));
wdenk8bde7f72003-06-27 21:31:46 +0000101
wdenk7a8e9bed2003-05-31 18:35:21 +0000102 /* Enable I/O decoding as well, PCI viudeo boards
103 * support I/O accesses, but they provide no
104 * bar register for this since the ports are fixed.
105 */
106 pci_write_config_word(devbusfn, PCI_COMMAND, PCI_COMMAND_MEMORY | PCI_COMMAND_IO | PCI_COMMAND_MASTER);
wdenk8bde7f72003-06-27 21:31:46 +0000107
wdenk7a8e9bed2003-05-31 18:35:21 +0000108 /* Test the ROM decoder, do the device support a rom? */
wdenk8bde7f72003-06-27 21:31:46 +0000109 pci_read_config_dword(devbusfn, PCI_ROM_ADDRESS, &old);
Graeme Russd7549022009-08-23 12:59:50 +1000110 pci_write_config_dword(devbusfn, PCI_ROM_ADDRESS, (u32)PCI_ROM_ADDRESS_MASK);
wdenk7a8e9bed2003-05-31 18:35:21 +0000111 pci_read_config_dword(devbusfn, PCI_ROM_ADDRESS, &addr);
112 pci_write_config_dword(devbusfn, PCI_ROM_ADDRESS, old);
wdenk8bde7f72003-06-27 21:31:46 +0000113
wdenk7a8e9bed2003-05-31 18:35:21 +0000114 if (!addr) {
115 printf("PCI VGA have no ROM?\n");
116 return 0;
117 }
wdenk8bde7f72003-06-27 21:31:46 +0000118
wdenk7a8e9bed2003-05-31 18:35:21 +0000119 /* device have a rom */
wdenk8bde7f72003-06-27 21:31:46 +0000120 if (pci_shadow_rom(devbusfn, (void*)0xc0000)) {
wdenk7a8e9bed2003-05-31 18:35:21 +0000121 printf("Shadowing of PCI VGA BIOS failed\n");
122 return 0;
123 }
wdenk8bde7f72003-06-27 21:31:46 +0000124
wdenk7a8e9bed2003-05-31 18:35:21 +0000125 /* Now enable lagacy VGA port access */
126 if (pci_enable_legacy_video_ports(pci_bus_to_hose(PCI_BUS(devbusfn)))) {
127 printf("PCI VGA enable failed\n");
128 return 0;
129 }
wdenk8bde7f72003-06-27 21:31:46 +0000130
131
wdenk7a8e9bed2003-05-31 18:35:21 +0000132 /* return the pci device info, that we'll need later */
wdenk8bde7f72003-06-27 21:31:46 +0000133 return PCI_BUS(devbusfn) << 8 |
wdenk7a8e9bed2003-05-31 18:35:21 +0000134 PCI_DEV(devbusfn) << 3 | (PCI_FUNC(devbusfn)&7);
135 }
wdenk8bde7f72003-06-27 21:31:46 +0000136
wdenk7a8e9bed2003-05-31 18:35:21 +0000137 return 0;
138}
139
wdenk7a8e9bed2003-05-31 18:35:21 +0000140static int probe_isa_video(void)
141{
142 u32 ptr;
wdenk8bde7f72003-06-27 21:31:46 +0000143 char *buf;
144
wdenk7a8e9bed2003-05-31 18:35:21 +0000145 if (0 == (ptr = isa_map_rom(0xc0000, 0x8000))) {
146 return -1;
147 }
148 if (NULL == (buf=malloc(0x8000))) {
149 isa_unmap_rom(ptr);
150 return -1;
151 }
152 if (readw(ptr) != 0xaa55) {
153 free(buf);
154 isa_unmap_rom(ptr);
155 return -1;
156 }
wdenk8bde7f72003-06-27 21:31:46 +0000157
wdenk7a8e9bed2003-05-31 18:35:21 +0000158 /* shadow the rom */
159 memcpy(buf, (void*)ptr, 0x8000);
160 isa_unmap_rom(ptr);
161 memcpy((void*)0xc0000, buf, 0x8000);
wdenk8bde7f72003-06-27 21:31:46 +0000162
wdenk7a8e9bed2003-05-31 18:35:21 +0000163 free(buf);
wdenk8bde7f72003-06-27 21:31:46 +0000164
wdenk7a8e9bed2003-05-31 18:35:21 +0000165 return 0;
166}
167
168int video_bios_init(void)
169{
170 struct pt_regs regs;
171
172 /* clear the video bios area in case we warmbooted */
wdenk8bde7f72003-06-27 21:31:46 +0000173 memset((void*)0xc0000, 0, 0x8000);
wdenk7a8e9bed2003-05-31 18:35:21 +0000174 memset(&regs, 0, sizeof(struct pt_regs));
wdenk8bde7f72003-06-27 21:31:46 +0000175
wdenk7a8e9bed2003-05-31 18:35:21 +0000176 if (probe_isa_video()) {
177 /* No ISA board found, try the PCI bus */
178 regs.eax = probe_pci_video();
179 }
wdenk8bde7f72003-06-27 21:31:46 +0000180
wdenk7a8e9bed2003-05-31 18:35:21 +0000181 /* Did we succeed in mapping any video bios */
182 if (readw(0xc0000) == 0xaa55) {
183 int size;
184 int i;
185 u8 sum;
wdenk8bde7f72003-06-27 21:31:46 +0000186
wdenk7a8e9bed2003-05-31 18:35:21 +0000187 PRINTF("Found video bios signature\n");
188 size = 512*readb(0xc0002);
189 PRINTF("size %d\n", size);
190 sum=0;
191 for (i=0;i<size;i++) {
192 sum += readb(0xc0000 + i);
193 }
194 PRINTF("Checksum is %sOK\n",sum?"NOT ":"");
195 if (sum) {
196 return 1;
197 }
wdenk8bde7f72003-06-27 21:31:46 +0000198
wdenk7a8e9bed2003-05-31 18:35:21 +0000199 /* some video bioses (ATI Mach64) seem to think that
200 * the original int 10 handler is always at
201 * 0xf000:0xf065 , place an iret instruction there
202 */
203 writeb(0xcf, 0xff065);
wdenk8bde7f72003-06-27 21:31:46 +0000204
wdenk7a8e9bed2003-05-31 18:35:21 +0000205 regs.esp = 0x8000;
206 regs.xss = 0x2000;
207 enter_realmode(0xc000, 3, &regs, &regs);
208 PRINTF("INT 0x10 vector after: %04x:%04x\n",
wdenk8bde7f72003-06-27 21:31:46 +0000209 readw(0x42), readw(0x40));
wdenk7a8e9bed2003-05-31 18:35:21 +0000210 PRINTF("BIOS returned %scarry\n", regs.eflags & 1?"":"NOT ");
wdenk8bde7f72003-06-27 21:31:46 +0000211#ifdef PCI_BIOS_DEBUG
wdenk7a8e9bed2003-05-31 18:35:21 +0000212 print_bios_bios_stat();
wdenk8bde7f72003-06-27 21:31:46 +0000213#endif
wdenk7a8e9bed2003-05-31 18:35:21 +0000214 return (regs.eflags & 1);
wdenk8bde7f72003-06-27 21:31:46 +0000215
wdenk7a8e9bed2003-05-31 18:35:21 +0000216 }
wdenk8bde7f72003-06-27 21:31:46 +0000217
wdenk7a8e9bed2003-05-31 18:35:21 +0000218 return 1;
wdenk8bde7f72003-06-27 21:31:46 +0000219
wdenk7a8e9bed2003-05-31 18:35:21 +0000220}
Graeme Russ3ef96de2008-09-07 07:08:42 +1000221#endif
Graeme Russd7549022009-08-23 12:59:50 +1000222#endif