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