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