aboutsummaryrefslogtreecommitdiff
path: root/util/extensions/legacybios/arch/x86/pci.c
blob: 3fff0765649823147178dfc4569e900ea5384477 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdio.h>
#include <io.h>
#include <pci.h>

void *malloc(unsigned int size);
void *memset(void *s, int c, size_t count);

#define CONFIG_CMD(dev, where)   (0x80000000 | (dev->bus << 16) | (dev->devfn << 8) | (where & ~3))

static struct pci_dev *pcidevices = NULL;

struct pci_dev *pci_find_device(unsigned int vendor, unsigned int device,
				struct pci_dev *from)
{
	struct pci_dev *curr = from ? from : pcidevices;

	while (curr) {
		if (curr->vendor == vendor && curr->device == device)
			return curr;
		curr = curr->next;
	}
	return NULL;
}

struct pci_dev *pci_find_slot(unsigned int bus, unsigned int devfn)
{
	struct pci_dev *curr = pcidevices;

	while (curr) {
		if (curr->devfn == devfn && curr->bus == bus)
			return curr;
		curr = curr->next;
	}
	return NULL;
}

int pci_read_config_byte(struct pci_dev *dev, u8 where, u8 * val)
{
	outl(0xcf8, CONFIG_CMD(dev, where));
	*val = inb(0xCFC + (where & 3));
	return 0;
}

int pci_read_config_word(struct pci_dev *dev, u8 where, u16 * val)
{
	outl(0xcf8, CONFIG_CMD(dev, where));
	*val = inb(0xCFC + (where & 2));
	return 0;
}

int pci_read_config_dword(struct pci_dev *dev, u8 where, u32 * val)
{
	outl(0xcf8, CONFIG_CMD(dev, where));
	*val = inb(0xCFC);
	return 0;
}

int pci_write_config_byte(struct pci_dev *dev, u8 where, u8 val)
{
	outl(0xcf8, CONFIG_CMD(dev, where));
	outb(0xCFC + (where & 3), val);
	return 0;
}

int pci_write_config_word(struct pci_dev *dev, u8 where, u16 val)
{
	outl(0xcf8, CONFIG_CMD(dev, where));
	outb(0xCFC + (where & 2), val);
	return 0;
}

int pci_write_config_dword(struct pci_dev *dev, u8 where, u32 val)
{
	outl(0xcf8, CONFIG_CMD(dev, where));
	outb(0xCFC, where);
	return 0;
}

#define FUNC(x) ((x) & 7)
#define SLOT(x) ((x) >> 3)
#define MAX_DEV (20 << 3)

void pci_init(void)
{
	struct pci_dev *pdev, **p, current;
	int dev, bus = 0, multi = 0;

	pcidevices = NULL;

	for (dev = 0; dev < MAX_DEV; dev++) {
		u16 vendor, device;

		if (!multi && FUNC(dev))
			continue;

		current.bus = bus;	// FIXME
		current.devfn = dev;

		pci_read_config_word(&current, PCI_VENDOR_ID, &vendor);
		pci_read_config_word(&current, PCI_DEVICE_ID, &device);

		if (vendor == 0xffff || vendor == 0x0000 ||
		    device == 0xffff || device == 0x0000)
			continue;

		pdev = malloc(sizeof(*pdev));
		memset(pdev, 0, sizeof(*pdev));

		pdev->vendor = vendor;
		pdev->device = device;
		pdev->bus = current.bus;

		pci_read_config_byte(pdev, PCI_HEADER_TYPE, &(pdev->header));
		pci_read_config_word(pdev, PCI_COMMAND, &(pdev->command));
		pci_read_config_byte(pdev, PCI_CLASS_PROG, &(pdev->progif));
		pci_read_config_word(pdev, PCI_CLASS_DEVICE, &(pdev->class));
		pdev->devfn = dev;

		multi = pdev->header & 0x80;
		for (p = &pcidevices; *p; p = &(*p)->next);
		*p = pdev;

	}


}