Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1119