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