Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2045