wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
Albert ARIBAUD | 60fbc8d | 2011-08-04 18:45:45 +0200 | [diff] [blame] | 3 | * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se> |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 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 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 24 | #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 Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 31 | #include "bios.h" |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 32 | |
| 33 | #undef PCI_BIOS_DEBUG |
| 34 | #undef VGA_BIOS_DEBUG |
| 35 | |
| 36 | #ifdef VGA_BIOS_DEBUG |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 37 | #define PRINTF(fmt, args...) printf(fmt, ##args) |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 38 | #else |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 39 | #define PRINTF(fmt, args...) |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 42 | #define PCI_CLASS_VIDEO 3 |
| 43 | #define PCI_CLASS_VIDEO_STD 0 |
| 44 | #define PCI_CLASS_VIDEO_PROG_IF_VGA 0 |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 45 | |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 46 | DEFINE_PCI_DEVICE_TABLE(supported) = { |
Graeme Russ | d0811ab | 2009-08-23 12:59:50 +1000 | [diff] [blame] | 47 | {PCI_VIDEO_VENDOR_ID, PCI_VIDEO_DEVICE_ID}, |
| 48 | {} |
| 49 | }; |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 50 | |
| 51 | static u32 probe_pci_video(void) |
| 52 | { |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 53 | struct pci_controller *hose; |
| 54 | pci_dev_t devbusfn = pci_find_devices(supported, 0); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 55 | |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 56 | if ((devbusfn != -1)) { |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 57 | u32 old; |
| 58 | u32 addr; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 59 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 60 | /* PCI video device detected */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 61 | printf("Found PCI VGA device at %02x.%02x.%x\n", |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 62 | PCI_BUS(devbusfn), |
| 63 | PCI_DEV(devbusfn), |
| 64 | PCI_FUNC(devbusfn)); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 65 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 66 | /* 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 Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 70 | pci_write_config_word(devbusfn, |
| 71 | PCI_COMMAND, |
| 72 | PCI_COMMAND_MEMORY | |
| 73 | PCI_COMMAND_IO | |
| 74 | PCI_COMMAND_MASTER); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 75 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 76 | /* Test the ROM decoder, do the device support a rom? */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 77 | pci_read_config_dword(devbusfn, PCI_ROM_ADDRESS, &old); |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 78 | pci_write_config_dword(devbusfn, PCI_ROM_ADDRESS, |
| 79 | (u32)PCI_ROM_ADDRESS_MASK); |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 80 | pci_read_config_dword(devbusfn, PCI_ROM_ADDRESS, &addr); |
| 81 | pci_write_config_dword(devbusfn, PCI_ROM_ADDRESS, old); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 82 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 83 | if (!addr) { |
| 84 | printf("PCI VGA have no ROM?\n"); |
| 85 | return 0; |
| 86 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 87 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 88 | /* device have a rom */ |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 89 | if (pci_shadow_rom(devbusfn, (void *)0xc0000)) { |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 90 | printf("Shadowing of PCI VGA BIOS failed\n"); |
| 91 | return 0; |
| 92 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 93 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 94 | /* Now enable lagacy VGA port access */ |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 95 | hose = pci_bus_to_hose(PCI_BUS(devbusfn)); |
| 96 | if (pci_enable_legacy_video_ports(hose)) { |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 97 | printf("PCI VGA enable failed\n"); |
| 98 | return 0; |
| 99 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 100 | |
| 101 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 102 | /* return the pci device info, that we'll need later */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 103 | return PCI_BUS(devbusfn) << 8 | |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 104 | PCI_DEV(devbusfn) << 3 | (PCI_FUNC(devbusfn) & 7); |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 105 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 106 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 110 | static int probe_isa_video(void) |
| 111 | { |
| 112 | u32 ptr; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 113 | char *buf; |
| 114 | |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 115 | ptr = isa_map_rom(0xc0000, 0x8000); |
| 116 | |
| 117 | if (!ptr) |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 118 | return -1; |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 119 | |
| 120 | buf = malloc(0x8000); |
| 121 | if (!buf) { |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 122 | isa_unmap_rom(ptr); |
| 123 | return -1; |
| 124 | } |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 125 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 126 | if (readw(ptr) != 0xaa55) { |
| 127 | free(buf); |
| 128 | isa_unmap_rom(ptr); |
| 129 | return -1; |
| 130 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 131 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 132 | /* shadow the rom */ |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 133 | memcpy(buf, (void *)ptr, 0x8000); |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 134 | isa_unmap_rom(ptr); |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 135 | memcpy((void *)0xc0000, buf, 0x8000); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 136 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 137 | free(buf); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 138 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | int video_bios_init(void) |
| 143 | { |
| 144 | struct pt_regs regs; |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 145 | int size; |
| 146 | int i; |
| 147 | u8 sum; |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 148 | |
| 149 | /* clear the video bios area in case we warmbooted */ |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 150 | memset((void *)0xc0000, 0, 0x8000); |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 151 | memset(®s, 0, sizeof(struct pt_regs)); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 152 | |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 153 | if (probe_isa_video()) |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 154 | /* No ISA board found, try the PCI bus */ |
| 155 | regs.eax = probe_pci_video(); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 156 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 157 | /* Did we succeed in mapping any video bios */ |
| 158 | if (readw(0xc0000) == 0xaa55) { |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 159 | PRINTF("Found video bios signature\n"); |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 160 | size = readb(0xc0002) * 512; |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 161 | PRINTF("size %d\n", size); |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 162 | sum = 0; |
| 163 | |
| 164 | for (i = 0; i < size; i++) |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 165 | sum += readb(0xc0000 + i); |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 166 | |
| 167 | PRINTF("Checksum is %sOK\n", sum ? "NOT " : ""); |
| 168 | |
| 169 | if (sum) |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 170 | return 1; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 171 | |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 172 | /* |
| 173 | * Some video bioses (ATI Mach64) seem to think that |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 174 | * the original int 10 handler is always at |
| 175 | * 0xf000:0xf065 , place an iret instruction there |
| 176 | */ |
| 177 | writeb(0xcf, 0xff065); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 178 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 179 | regs.esp = 0x8000; |
| 180 | regs.xss = 0x2000; |
| 181 | enter_realmode(0xc000, 3, ®s, ®s); |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 182 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 183 | PRINTF("INT 0x10 vector after: %04x:%04x\n", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 184 | readw(0x42), readw(0x40)); |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 185 | PRINTF("BIOS returned %scarry\n", |
| 186 | regs.eflags & 0x00000001 ? "" : "NOT "); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 187 | #ifdef PCI_BIOS_DEBUG |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 188 | print_bios_bios_stat(); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 189 | #endif |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 190 | return regs.eflags & 0x00000001; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 191 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 192 | } |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 193 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 194 | return 1; |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 195 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 196 | } |