c3237dbdbfde98fd9db61bd71a02b282ec1103db
[coreboot.git] / src / devices / pci_ops.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2004 Linux Networx
5  * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6  * Copyright (C) 2009 coresystems GmbH
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 #include <console/console.h>
23 #include <arch/pciconf.h>
24 #include <device/pci.h>
25 #include <device/pci_ids.h>
26 #include <device/pci_ops.h>
27
28 /*
29  * The only consumer of the return value of get_pbus() is ops_pci_bus().
30  * ops_pci_bus() can handle being passed NULL and auto-picks working ops.
31  */
32 static struct bus *get_pbus(device_t dev)
33 {
34         struct bus *pbus = NULL;
35
36         if (!dev)
37                 die("get_pbus: dev is NULL!\n");
38         else
39                 pbus = dev->bus;
40
41         while(pbus && pbus->dev && !ops_pci_bus(pbus)) {
42                 if (pbus == pbus->dev->bus) {
43                         printk(BIOS_ALERT, "%s in endless loop looking for a parent "
44                                 "bus with ops_pci_bus for %s, breaking out.\n",
45                                  __func__, dev_path(dev));
46                         break;
47                 }
48                 pbus = pbus->dev->bus;
49         }
50         if (!pbus || !pbus->dev || !pbus->dev->ops || !pbus->dev->ops->ops_pci_bus) {
51                 /* This can happen before the device tree is set up completely. */
52                 //printk(BIOS_EMERG, "%s: Cannot find pci bus operations.\n", dev_path(dev));
53                 pbus = NULL;
54         }
55         return pbus;
56 }
57
58 uint8_t pci_read_config8(device_t dev, unsigned where)
59 {
60         struct bus *pbus = get_pbus(dev);
61         return ops_pci_bus(pbus)->read8(pbus, dev->bus->secondary, dev->path.pci.devfn, where);
62 }
63
64 uint16_t pci_read_config16(device_t dev, unsigned where)
65 {
66         struct bus *pbus = get_pbus(dev);
67         return ops_pci_bus(pbus)->read16(pbus, dev->bus->secondary, dev->path.pci.devfn, where);
68 }
69
70 uint32_t pci_read_config32(device_t dev, unsigned where)
71 {
72         struct bus *pbus = get_pbus(dev);
73         return ops_pci_bus(pbus)->read32(pbus, dev->bus->secondary, dev->path.pci.devfn, where);
74 }
75
76 void pci_write_config8(device_t dev, unsigned where, uint8_t val)
77 {
78         struct bus *pbus = get_pbus(dev);
79         ops_pci_bus(pbus)->write8(pbus, dev->bus->secondary, dev->path.pci.devfn, where, val);
80 }
81
82 void pci_write_config16(device_t dev, unsigned where, uint16_t val)
83 {
84         struct bus *pbus = get_pbus(dev);
85         ops_pci_bus(pbus)->write16(pbus, dev->bus->secondary, dev->path.pci.devfn, where, val);
86 }
87
88 void pci_write_config32(device_t dev, unsigned where, uint32_t val)
89 {
90         struct bus *pbus = get_pbus(dev);
91         ops_pci_bus(pbus)->write32(pbus, dev->bus->secondary, dev->path.pci.devfn, where, val);
92 }
93
94 #if CONFIG_MMCONF_SUPPORT
95 uint8_t pci_mmio_read_config8(device_t dev, unsigned where)
96 {
97         struct bus *pbus = get_pbus(dev);
98         return pci_ops_mmconf.read8(pbus, dev->bus->secondary, dev->path.pci.devfn, where);
99 }
100
101 uint16_t pci_mmio_read_config16(device_t dev, unsigned where)
102 {
103         struct bus *pbus = get_pbus(dev);
104         return pci_ops_mmconf.read16(pbus, dev->bus->secondary, dev->path.pci.devfn, where);
105 }
106
107 uint32_t pci_mmio_read_config32(device_t dev, unsigned where)
108 {
109         struct bus *pbus = get_pbus(dev);
110         return pci_ops_mmconf.read32(pbus, dev->bus->secondary, dev->path.pci.devfn, where);
111 }
112
113 void pci_mmio_write_config8(device_t dev, unsigned where, uint8_t val)
114 {
115         struct bus *pbus = get_pbus(dev);
116         pci_ops_mmconf.write8(pbus, dev->bus->secondary, dev->path.pci.devfn, where, val);
117 }
118
119 void pci_mmio_write_config16(device_t dev, unsigned where, uint16_t val)
120 {
121         struct bus *pbus = get_pbus(dev);
122         pci_ops_mmconf.write16(pbus, dev->bus->secondary, dev->path.pci.devfn, where, val);
123 }
124
125 void pci_mmio_write_config32(device_t dev, unsigned where, uint32_t val)
126 {
127         struct bus *pbus = get_pbus(dev);
128         pci_ops_mmconf.write32(pbus, dev->bus->secondary, dev->path.pci.devfn, where, val);
129 }
130 #endif