Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/libsaio/pci.c

1/*
2 *
3 * Copyright 2008 by Islam M. Ahmed Zaid. All rights reserved.
4 *
5 */
6
7#include "libsaio.h"
8#include "bootstruct.h"
9#include "pci.h"
10
11uint32_t pci_config_read(uint32_t pci_addr, uint8_t reg, uint8_t bytes)
12{
13uint32_t data = -1;
14
15pci_addr |= reg & ~3;
16outl(PCI_ADDR_REG, pci_addr);
17
18switch (bytes)
19{
20case 1:
21data = inb(PCI_DATA_REG + (reg & 3));
22break;
23case 2:
24data = inw(PCI_DATA_REG + (reg & 2));
25break;
26case 4:
27data = inl(PCI_DATA_REG);
28break;
29}
30
31return data;
32}
33
34void pci_config_write(uint32_t pci_addr, uint8_t reg, uint32_t data, uint8_t bytes)
35{
36pci_addr |= reg & ~3;
37outl(PCI_ADDR_REG, pci_addr);
38
39switch (bytes)
40{
41case 1:
42outb(PCI_DATA_REG + (reg & 3), data);
43break;
44case 2:
45outw(PCI_DATA_REG + (reg & 2), data);
46break;
47case 4:
48outl(PCI_DATA_REG, data);
49break;
50}
51}
52
53pci_dt_t *root_pci_dev;
54
55void scan_pci_bus(pci_dt_t *start, uint8_t bus)
56{
57uint8_t dev, func, secondary_bus, header_type;
58uint32_t id, pci_addr;
59pci_dt_t *new;
60pci_dt_t **current = &start->children;
61
62for (dev = 0; dev < 32; dev++)
63for (func = 0; func < 8; func++)
64{
65pci_addr = PCIADDR(bus, dev, func);
66id = pci_config_read32(pci_addr, PCI_VENDOR_ID);
67if (!id || id == 0xffffffff)
68continue;
69
70new = (pci_dt_t*)malloc(sizeof(pci_dt_t));
71if (!new)
72return;
73memset(new, 0, sizeof(pci_dt_t));
74
75new->dev.addr= pci_addr;
76new->vendor_id= id & 0xffff;
77new->device_id= (id >> 16) & 0xffff;
78new->class_id= pci_config_read16(pci_addr, PCI_CLASS_DEVICE);
79new->parent= start;
80
81header_type = pci_config_read8(pci_addr, PCI_HEADER_TYPE);
82switch (header_type & 0x7f)
83{
84case PCI_HEADER_TYPE_BRIDGE:
85case PCI_HEADER_TYPE_CARDBUS:
86secondary_bus = pci_config_read8(pci_addr, PCI_SECONDARY_BUS);
87if (secondary_bus != 0)
88scan_pci_bus(new, secondary_bus);
89break;
90}
91
92*current = new;
93current = &new->next;
94
95if ((func == 0) && ((header_type & 0x80) == 0))
96break;
97}
98}
99
100void enable_pci_devs(void)
101{
102uint16_t id;
103uint32_t rcba, *fd;
104
105id = pci_config_read16(PCIADDR(0, 0x00, 0), 0x00);
106/* make sure we're on Intel chipset */
107if (id != 0x8086)
108return;
109rcba = pci_config_read32(PCIADDR(0, 0x1f, 0), 0xf0) & ~1;
110fd = (uint32_t *)(rcba + 0x3418);
111/* set SMBus Disable (SD) to 0 */
112*fd &= ~0x8;
113/* and all devices? */
114//*fd = 0x1;
115}
116
117void build_pci_dt(void)
118{
119root_pci_dev = malloc(sizeof(pci_dt_t));
120
121if (!root_pci_dev)
122return;
123
124bzero(root_pci_dev, sizeof(pci_dt_t));
125enable_pci_devs();
126scan_pci_bus(root_pci_dev, 0);
127}
128
129char dev_path[80];
130
131char *get_pci_dev_path(pci_dt_t *pci_dt)
132{
133pci_dt_t *current, *end;
134char tmp[30];
135
136dev_path[0] = 0;
137end = root_pci_dev;
138
139while (end != pci_dt)
140{
141current = pci_dt;
142while (current->parent != end)
143current = current->parent;
144end = current;
145
146sprintf(tmp, "%s/Pci(0x%x,0x%x)",
147(current->parent == root_pci_dev) ? "PciRoot(0x0)" : "",
148current->dev.bits.dev, current->dev.bits.func);
149strcat(dev_path, tmp);
150}
151
152return dev_path;
153}
154
155void dump_pci_dt(pci_dt_t *pci_dt)
156{
157pci_dt_t *current = pci_dt;
158
159while (current)
160{
161printf("%02x:%02x.%x [%04x] [%04x:%04x] :: %s\n",
162current->dev.bits.bus, current->dev.bits.dev, current->dev.bits.func,
163current->class_id, current->vendor_id, current->device_id,
164get_pci_dev_path(current));
165dump_pci_dt(current->children);
166current = current->next;
167}
168}
169
170
171void lspci(const char *booterParam)
172{
173if(bootArgs->Video.v_display == VGA_TEXT_MODE)
174{
175setActiveDisplayPage(1);
176clearScreenRows(0, 24);
177setCursorPosition(0, 0, 1);
178}
179
180dump_pci_dt(root_pci_dev->children);
181
182printf("(Press a key to continue...)");
183getc();
184
185if(bootArgs->Video.v_display == VGA_TEXT_MODE)
186setActiveDisplayPage(0);
187}
188
189int check_vga_nvidia(pci_dt_t *pci_dt)
190{
191pci_dt_t *current = pci_dt;
192while (current)
193{
194if(current->vendor_id == PCI_CLASS_DISPLAY_VGA)
195if(current->class_id == PCI_VENDOR_ID_NVIDIA)
196return 1;
197
198check_vga_nvidia(current->children);
199current = current->next;
200}
201return 0;
202}
203
204

Archive Download this file

Revision: 1