Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 7e61e45 | 2008-01-18 10:35:56 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2004 Linux Networx |
| 5 | * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx) |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 6 | * Copyright (C) 2009 coresystems GmbH |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 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 |
Paul Menzel | a46a712 | 2013-02-23 18:37:27 +0100 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 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 | |
Kyösti Mälkki | aad0747 | 2013-07-04 17:17:45 +0300 | [diff] [blame] | 28 | const struct pci_bus_operations *pci_bus_default_ops(device_t dev) |
| 29 | { |
| 30 | #if CONFIG_MMCONF_SUPPORT_DEFAULT |
| 31 | return &pci_ops_mmconf; |
| 32 | #else |
| 33 | return &pci_cf8_conf1; |
| 34 | #endif |
| 35 | } |
| 36 | |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 37 | static const struct pci_bus_operations *pci_bus_ops(struct bus *bus, struct device *dev) |
Kyösti Mälkki | aad0747 | 2013-07-04 17:17:45 +0300 | [diff] [blame] | 38 | { |
| 39 | const struct pci_bus_operations *bops; |
| 40 | bops = NULL; |
| 41 | if (bus && bus->dev && bus->dev->ops && bus->dev->ops->ops_pci_bus) { |
| 42 | bops = bus->dev->ops->ops_pci_bus(dev); |
| 43 | } |
| 44 | if (!bops) |
| 45 | bops = pci_bus_default_ops(dev); |
| 46 | return bops; |
| 47 | } |
| 48 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 49 | /* |
Kyösti Mälkki | aad0747 | 2013-07-04 17:17:45 +0300 | [diff] [blame] | 50 | * The only consumer of the return value of get_pbus() is pci_bus_ops(). |
| 51 | * pci_bus_ops() can handle being passed NULL and auto-picks working ops. |
Carl-Daniel Hailfinger | 00003ae | 2009-09-22 00:09:41 +0000 | [diff] [blame] | 52 | */ |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 53 | static struct bus *get_pbus(struct device *dev) |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 54 | { |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 55 | struct bus *pbus = NULL; |
Rudolf Marek | 3a8565a | 2009-03-26 21:45:26 +0000 | [diff] [blame] | 56 | |
| 57 | if (!dev) |
Carl-Daniel Hailfinger | 87e7050 | 2009-06-05 11:41:51 +0000 | [diff] [blame] | 58 | die("get_pbus: dev is NULL!\n"); |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 59 | else |
| 60 | pbus = dev->bus; |
Rudolf Marek | 3a8565a | 2009-03-26 21:45:26 +0000 | [diff] [blame] | 61 | |
Kyösti Mälkki | aad0747 | 2013-07-04 17:17:45 +0300 | [diff] [blame] | 62 | while (pbus && pbus->dev && !pci_bus_ops(pbus, dev)) { |
Carl-Daniel Hailfinger | bba113e | 2009-03-05 19:33:12 +0000 | [diff] [blame] | 63 | if (pbus == pbus->dev->bus) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 64 | printk(BIOS_ALERT, "%s in endless loop looking for a " |
Kyösti Mälkki | aad0747 | 2013-07-04 17:17:45 +0300 | [diff] [blame] | 65 | "parent bus with pci_bus_ops for %s, breaking " |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 66 | "out.\n", __func__, dev_path(dev)); |
Carl-Daniel Hailfinger | bba113e | 2009-03-05 19:33:12 +0000 | [diff] [blame] | 67 | break; |
| 68 | } |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 69 | pbus = pbus->dev->bus; |
| 70 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 71 | |
| 72 | if (!pbus || !pbus->dev || !pbus->dev->ops |
| 73 | || !pbus->dev->ops->ops_pci_bus) { |
| 74 | /* This can happen before the device tree is fully set up. */ |
| 75 | |
| 76 | // printk(BIOS_EMERG, "%s: Cannot find PCI bus operations.\n", |
| 77 | // dev_path(dev)); |
| 78 | |
Carl-Daniel Hailfinger | 00003ae | 2009-09-22 00:09:41 +0000 | [diff] [blame] | 79 | pbus = NULL; |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 80 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 81 | |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 82 | return pbus; |
| 83 | } |
| 84 | |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 85 | u8 pci_read_config8(struct device *dev, unsigned int where) |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 86 | { |
| 87 | struct bus *pbus = get_pbus(dev); |
Kyösti Mälkki | aad0747 | 2013-07-04 17:17:45 +0300 | [diff] [blame] | 88 | return pci_bus_ops(pbus, dev)->read8(pbus, dev->bus->secondary, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 89 | dev->path.pci.devfn, where); |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 92 | u16 pci_read_config16(struct device *dev, unsigned int where) |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 93 | { |
| 94 | struct bus *pbus = get_pbus(dev); |
Kyösti Mälkki | aad0747 | 2013-07-04 17:17:45 +0300 | [diff] [blame] | 95 | return pci_bus_ops(pbus, dev)->read16(pbus, dev->bus->secondary, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 96 | dev->path.pci.devfn, where); |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 99 | u32 pci_read_config32(struct device *dev, unsigned int where) |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 100 | { |
| 101 | struct bus *pbus = get_pbus(dev); |
Kyösti Mälkki | aad0747 | 2013-07-04 17:17:45 +0300 | [diff] [blame] | 102 | return pci_bus_ops(pbus, dev)->read32(pbus, dev->bus->secondary, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 103 | dev->path.pci.devfn, where); |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 106 | void pci_write_config8(struct device *dev, unsigned int where, u8 val) |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 107 | { |
| 108 | struct bus *pbus = get_pbus(dev); |
Kyösti Mälkki | aad0747 | 2013-07-04 17:17:45 +0300 | [diff] [blame] | 109 | pci_bus_ops(pbus, dev)->write8(pbus, dev->bus->secondary, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 110 | dev->path.pci.devfn, where, val); |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 113 | void pci_write_config16(struct device *dev, unsigned int where, u16 val) |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 114 | { |
| 115 | struct bus *pbus = get_pbus(dev); |
Kyösti Mälkki | aad0747 | 2013-07-04 17:17:45 +0300 | [diff] [blame] | 116 | pci_bus_ops(pbus, dev)->write16(pbus, dev->bus->secondary, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 117 | dev->path.pci.devfn, where, val); |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 120 | void pci_write_config32(struct device *dev, unsigned int where, u32 val) |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 121 | { |
| 122 | struct bus *pbus = get_pbus(dev); |
Kyösti Mälkki | aad0747 | 2013-07-04 17:17:45 +0300 | [diff] [blame] | 123 | pci_bus_ops(pbus, dev)->write32(pbus, dev->bus->secondary, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 124 | dev->path.pci.devfn, where, val); |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 125 | } |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 126 | |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 127 | #if CONFIG_MMCONF_SUPPORT |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 128 | u8 pci_mmio_read_config8(struct device *dev, unsigned int where) |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 129 | { |
| 130 | struct bus *pbus = get_pbus(dev); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 131 | return pci_ops_mmconf.read8(pbus, dev->bus->secondary, |
| 132 | dev->path.pci.devfn, where); |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 135 | u16 pci_mmio_read_config16(struct device *dev, unsigned int where) |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 136 | { |
| 137 | struct bus *pbus = get_pbus(dev); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 138 | return pci_ops_mmconf.read16(pbus, dev->bus->secondary, |
| 139 | dev->path.pci.devfn, where); |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 142 | u32 pci_mmio_read_config32(struct device *dev, unsigned int where) |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 143 | { |
| 144 | struct bus *pbus = get_pbus(dev); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 145 | return pci_ops_mmconf.read32(pbus, dev->bus->secondary, |
| 146 | dev->path.pci.devfn, where); |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 149 | void pci_mmio_write_config8(struct device *dev, unsigned int where, u8 val) |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 150 | { |
| 151 | struct bus *pbus = get_pbus(dev); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 152 | pci_ops_mmconf.write8(pbus, dev->bus->secondary, dev->path.pci.devfn, |
| 153 | where, val); |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 156 | void pci_mmio_write_config16(struct device *dev, unsigned int where, u16 val) |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 157 | { |
| 158 | struct bus *pbus = get_pbus(dev); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 159 | pci_ops_mmconf.write16(pbus, dev->bus->secondary, dev->path.pci.devfn, |
| 160 | where, val); |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Edward O'Callaghan | 016732f | 2014-10-29 03:04:40 +1100 | [diff] [blame] | 163 | void pci_mmio_write_config32(struct device *dev, unsigned int where, u32 val) |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 164 | { |
| 165 | struct bus *pbus = get_pbus(dev); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 166 | pci_ops_mmconf.write32(pbus, dev->bus->secondary, dev->path.pci.devfn, |
| 167 | where, val); |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 168 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 169 | |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 170 | #endif |