2 * This file is part of the coreboot project.
4 * Copyright (C) 2004 Linux Networx
5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6 * Copyright (C) 2009 coresystems GmbH
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.
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.
18 #include <console/console.h>
19 #include <device/pci.h>
20 #include <device/pci_ops.h>
22 static const struct pci_bus_operations
*pci_bus_ops(struct bus
*bus
, struct device
*dev
)
24 const struct pci_bus_operations
*bops
;
26 if (bus
&& bus
->dev
&& bus
->dev
->ops
&& bus
->dev
->ops
->ops_pci_bus
) {
27 bops
= bus
->dev
->ops
->ops_pci_bus(dev
);
30 bops
= pci_bus_default_ops(dev
);
35 * The only consumer of the return value of get_pbus() is pci_bus_ops().
36 * pci_bus_ops() can handle being passed NULL and auto-picks working ops.
38 static struct bus
*get_pbus(struct device
*dev
)
40 struct bus
*pbus
= NULL
;
43 die("get_pbus: dev is NULL!\n");
47 while (pbus
&& pbus
->dev
&& !pci_bus_ops(pbus
, dev
)) {
48 if (pbus
== pbus
->dev
->bus
) {
49 printk(BIOS_ALERT
, "%s in endless loop looking for a "
50 "parent bus with pci_bus_ops for %s, breaking "
51 "out.\n", __func__
, dev_path(dev
));
54 pbus
= pbus
->dev
->bus
;
57 if (!pbus
|| !pbus
->dev
|| !pbus
->dev
->ops
58 || !pbus
->dev
->ops
->ops_pci_bus
) {
59 /* This can happen before the device tree is fully set up. */
61 // printk(BIOS_EMERG, "%s: Cannot find PCI bus operations.\n",
70 u8
pci_read_config8(struct device
*dev
, unsigned int where
)
72 struct bus
*pbus
= get_pbus(dev
);
73 return pci_bus_ops(pbus
, dev
)->read8(pbus
, dev
->bus
->secondary
,
74 dev
->path
.pci
.devfn
, where
);
77 u16
pci_read_config16(struct device
*dev
, unsigned int where
)
79 struct bus
*pbus
= get_pbus(dev
);
80 return pci_bus_ops(pbus
, dev
)->read16(pbus
, dev
->bus
->secondary
,
81 dev
->path
.pci
.devfn
, where
);
84 u32
pci_read_config32(struct device
*dev
, unsigned int where
)
86 struct bus
*pbus
= get_pbus(dev
);
87 return pci_bus_ops(pbus
, dev
)->read32(pbus
, dev
->bus
->secondary
,
88 dev
->path
.pci
.devfn
, where
);
91 void pci_write_config8(struct device
*dev
, unsigned int where
, u8 val
)
93 struct bus
*pbus
= get_pbus(dev
);
94 pci_bus_ops(pbus
, dev
)->write8(pbus
, dev
->bus
->secondary
,
95 dev
->path
.pci
.devfn
, where
, val
);
98 void pci_write_config16(struct device
*dev
, unsigned int where
, u16 val
)
100 struct bus
*pbus
= get_pbus(dev
);
101 pci_bus_ops(pbus
, dev
)->write16(pbus
, dev
->bus
->secondary
,
102 dev
->path
.pci
.devfn
, where
, val
);
105 void pci_write_config32(struct device
*dev
, unsigned int where
, u32 val
)
107 struct bus
*pbus
= get_pbus(dev
);
108 pci_bus_ops(pbus
, dev
)->write32(pbus
, dev
->bus
->secondary
,
109 dev
->path
.pci
.devfn
, where
, val
);