~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to roms/seabios/src/hw/pci.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// PCI config space access functions.
 
2
//
 
3
// Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
 
4
// Copyright (C) 2002  MandrakeSoft S.A.
 
5
//
 
6
// This file may be distributed under the terms of the GNU LGPLv3 license.
 
7
 
 
8
#include "output.h" // dprintf
 
9
#include "pci.h" // pci_config_writel
 
10
#include "pci_regs.h" // PCI_VENDOR_ID
 
11
#include "util.h" // udelay
 
12
#include "x86.h" // outl
 
13
 
 
14
#define PORT_PCI_CMD           0x0cf8
 
15
#define PORT_PCI_REBOOT        0x0cf9
 
16
#define PORT_PCI_DATA          0x0cfc
 
17
 
 
18
void pci_config_writel(u16 bdf, u32 addr, u32 val)
 
19
{
 
20
    outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
 
21
    outl(val, PORT_PCI_DATA);
 
22
}
 
23
 
 
24
void pci_config_writew(u16 bdf, u32 addr, u16 val)
 
25
{
 
26
    outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
 
27
    outw(val, PORT_PCI_DATA + (addr & 2));
 
28
}
 
29
 
 
30
void pci_config_writeb(u16 bdf, u32 addr, u8 val)
 
31
{
 
32
    outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
 
33
    outb(val, PORT_PCI_DATA + (addr & 3));
 
34
}
 
35
 
 
36
u32 pci_config_readl(u16 bdf, u32 addr)
 
37
{
 
38
    outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
 
39
    return inl(PORT_PCI_DATA);
 
40
}
 
41
 
 
42
u16 pci_config_readw(u16 bdf, u32 addr)
 
43
{
 
44
    outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
 
45
    return inw(PORT_PCI_DATA + (addr & 2));
 
46
}
 
47
 
 
48
u8 pci_config_readb(u16 bdf, u32 addr)
 
49
{
 
50
    outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
 
51
    return inb(PORT_PCI_DATA + (addr & 3));
 
52
}
 
53
 
 
54
void
 
55
pci_config_maskw(u16 bdf, u32 addr, u16 off, u16 on)
 
56
{
 
57
    u16 val = pci_config_readw(bdf, addr);
 
58
    val = (val & ~off) | on;
 
59
    pci_config_writew(bdf, addr, val);
 
60
}
 
61
 
 
62
// Helper function for foreachbdf() macro - return next device
 
63
int
 
64
pci_next(int bdf, int bus)
 
65
{
 
66
    if (pci_bdf_to_fn(bdf) == 0
 
67
        && (pci_config_readb(bdf, PCI_HEADER_TYPE) & 0x80) == 0)
 
68
        // Last found device wasn't a multi-function device - skip to
 
69
        // the next device.
 
70
        bdf += 8;
 
71
    else
 
72
        bdf += 1;
 
73
 
 
74
    for (;;) {
 
75
        if (pci_bdf_to_bus(bdf) != bus)
 
76
            return -1;
 
77
 
 
78
        u16 v = pci_config_readw(bdf, PCI_VENDOR_ID);
 
79
        if (v != 0x0000 && v != 0xffff)
 
80
            // Device is present.
 
81
            return bdf;
 
82
 
 
83
        if (pci_bdf_to_fn(bdf) == 0)
 
84
            bdf += 8;
 
85
        else
 
86
            bdf += 1;
 
87
    }
 
88
}
 
89
 
 
90
// Check if PCI is available at all
 
91
int
 
92
pci_probe_host(void)
 
93
{
 
94
    outl(0x80000000, PORT_PCI_CMD);
 
95
    if (inl(PORT_PCI_CMD) != 0x80000000) {
 
96
        dprintf(1, "Detected non-PCI system\n");
 
97
        return -1;
 
98
    }
 
99
    return 0;
 
100
}
 
101
 
 
102
void
 
103
pci_reboot(void)
 
104
{
 
105
    u8 v = inb(PORT_PCI_REBOOT) & ~6;
 
106
    outb(v|2, PORT_PCI_REBOOT); /* Request hard reset */
 
107
    udelay(50);
 
108
    outb(v|6, PORT_PCI_REBOOT); /* Actually do the reset */
 
109
    udelay(50);
 
110
}