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#include "pci_root.h"
11
12#ifndef DEBUG_PCI
13#define DEBUG_PCI 0
14#endif
15
16#if DEBUG_PCI
17#define DBG(x...)printf(x)
18#else
19#define DBG(x...)
20#endif
21
22pci_dt_t*root_pci_dev;
23
24
25uint8_t pci_config_read8(uint32_t pci_addr, uint8_t reg)
26{
27pci_addr |= reg & ~3;
28outl(PCI_ADDR_REG, pci_addr);
29return inb(PCI_DATA_REG + (reg & 3));
30}
31
32uint16_t pci_config_read16(uint32_t pci_addr, uint8_t reg)
33{
34pci_addr |= reg & ~3;
35outl(PCI_ADDR_REG, pci_addr);
36return inw(PCI_DATA_REG + (reg & 2));
37}
38
39uint32_t pci_config_read32(uint32_t pci_addr, uint8_t reg)
40{
41pci_addr |= reg & ~3;
42outl(PCI_ADDR_REG, pci_addr);
43return inl(PCI_DATA_REG);
44}
45
46void pci_config_write8(uint32_t pci_addr, uint8_t reg, uint8_t data)
47{
48pci_addr |= reg & ~3;
49outl(PCI_ADDR_REG, pci_addr);
50outb(PCI_DATA_REG + (reg & 3), data);
51}
52
53void pci_config_write16(uint32_t pci_addr, uint8_t reg, uint16_t data)
54{
55pci_addr |= reg & ~3;
56outl(PCI_ADDR_REG, pci_addr);
57outw(PCI_DATA_REG + (reg & 2), data);
58}
59
60void pci_config_write32(uint32_t pci_addr, uint8_t reg, uint32_t data)
61{
62pci_addr |= reg & ~3;
63outl(PCI_ADDR_REG, pci_addr);
64outl(PCI_DATA_REG, data);
65}
66
67void scan_pci_bus(pci_dt_t *start, uint8_t bus)
68{
69pci_dt_t*new;
70pci_dt_t**current = &start->children;
71uint32_tid;
72uint32_tpci_addr;
73uint8_tdev = 0;
74uint8_tfunc = 0;
75uint8_tsecondary_bus;
76uint8_theader_type;
77
78for (dev = 0; dev < 32; dev++) {
79for (func = 0; func < 8; func++) {
80pci_addr = PCIADDR(bus, dev, func);
81id = pci_config_read32(pci_addr, PCI_VENDOR_ID);
82if (!id || id == 0xffffffff) {
83continue;
84}
85new = (pci_dt_t*)malloc(sizeof(pci_dt_t));
86bzero(new, sizeof(pci_dt_t));
87new->dev.addr= pci_addr;
88new->vendor_id= id & 0xffff;
89new->device_id= (id >> 16) & 0xffff;
90new->progif= pci_config_read8(pci_addr, PCI_CLASS_PROG);
91new->revision_id= pci_config_read8(pci_addr, PCI_CLASS_REVISION);
92new->subsys_id.subsys_id= pci_config_read32(pci_addr, PCI_SUBSYSTEM_VENDOR_ID);
93new->class_id= pci_config_read16(pci_addr, PCI_CLASS_DEVICE);
94new->parent= start;
95
96header_type = pci_config_read8(pci_addr, PCI_HEADER_TYPE);
97switch (header_type & 0x7f) {
98case PCI_HEADER_TYPE_BRIDGE:
99case PCI_HEADER_TYPE_CARDBUS:
100secondary_bus = pci_config_read8(pci_addr, PCI_SECONDARY_BUS);
101if (secondary_bus != 0) {
102scan_pci_bus(new, secondary_bus);
103}
104break;
105}
106*current = new;
107current = &new->next;
108
109if ((func == 0) && ((header_type & 0x80) == 0)) {
110break;
111}
112}
113}
114}
115
116void enable_pci_devs(void)
117{
118uint16_t id;
119uint32_t rcba, *fd;
120
121id = pci_config_read16(PCIADDR(0, 0x00, 0), 0x00);
122/* make sure we're on Intel chipset */
123if (id != 0x8086)
124return;
125rcba = pci_config_read32(PCIADDR(0, 0x1f, 0), 0xf0) & ~1;
126fd = (uint32_t *)(rcba + 0x3418);
127/* set SMBus Disable (SD) to 0 */
128*fd &= ~0x8;
129/* and all devices? */
130//*fd = 0x1;
131}
132
133
134void build_pci_dt(void)
135{
136root_pci_dev = malloc(sizeof(pci_dt_t));
137bzero(root_pci_dev, sizeof(pci_dt_t));
138enable_pci_devs();
139scan_pci_bus(root_pci_dev, 0);
140
141#if DEBUG_PCI
142dump_pci_dt(root_pci_dev->children);
143pause();
144#endif
145}
146
147static char dev_path[256];
148char *get_pci_dev_path(pci_dt_t *pci_dt)
149{
150pci_dt_t*current;
151pci_dt_t*end;
152chartmp[64];
153
154dev_path[0] = 0;
155end = root_pci_dev;
156
157int uid = getPciRootUID();
158while (end != pci_dt)
159{
160current = pci_dt;
161while (current->parent != end)
162current = current->parent;
163end = current;
164if (current->parent == root_pci_dev)
165{
166sprintf(tmp, "PciRoot(0x%x)/Pci(0x%x,0x%x)", uid,
167current->dev.bits.dev, current->dev.bits.func);
168} else {
169sprintf(tmp, "/Pci(0x%x,0x%x)",
170current->dev.bits.dev, current->dev.bits.func);
171}
172strcat(dev_path, tmp);
173}
174return dev_path;
175}
176
177void dump_pci_dt(pci_dt_t *pci_dt)
178{
179pci_dt_t*current;
180
181current = pci_dt;
182while (current) {
183printf("%02x:%02x.%x [%04x%02x] [%04x:%04x] (subsys [%04x:%04x]):: %s\n",
184current->dev.bits.bus, current->dev.bits.dev, current->dev.bits.func,
185current->class_id, current->vendor_id, current->device_id,
186current->subsys_id.subsys.vendor_id, current->subsys_id.subsys.device_id,
187get_pci_dev_path(current));
188dump_pci_dt(current->children);
189current = current->next;
190}
191}
192

Archive Download this file

Revision: 2262