Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 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 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 18 | #ifndef PCI_OPS_H |
| 19 | #define PCI_OPS_H |
| 20 | |
| 21 | #include <stdint.h> |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 22 | #include <device/device.h> |
Kyösti Mälkki | 3e6913b | 2019-03-02 16:26:10 +0200 | [diff] [blame] | 23 | #include <device/pci_type.h> |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 24 | #include <arch/pci_ops.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 25 | |
Kyösti Mälkki | 92b5296 | 2019-03-01 08:08:28 +0200 | [diff] [blame] | 26 | #ifdef __SIMPLE_DEVICE__ |
| 27 | |
| 28 | /* Avoid name collisions as different stages have different signature |
| 29 | * for these functions. The _s_ stands for simple, fundamental IO or |
| 30 | * MMIO variant. |
| 31 | */ |
| 32 | #define pci_read_config8 pci_s_read_config8 |
| 33 | #define pci_read_config16 pci_s_read_config16 |
| 34 | #define pci_read_config32 pci_s_read_config32 |
| 35 | #define pci_write_config8 pci_s_write_config8 |
| 36 | #define pci_write_config16 pci_s_write_config16 |
| 37 | #define pci_write_config32 pci_s_write_config32 |
| 38 | #else |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 39 | |
| 40 | #include <device/pci.h> |
| 41 | |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 42 | void __noreturn pcidev_die(void); |
| 43 | |
Kyösti Mälkki | 34cf561 | 2019-03-11 20:34:26 +0200 | [diff] [blame] | 44 | static __always_inline pci_devfn_t pcidev_bdf(const struct device *dev) |
| 45 | { |
| 46 | return (dev->path.pci.devfn << 12) | (dev->bus->secondary << 20); |
| 47 | } |
| 48 | |
| 49 | static __always_inline pci_devfn_t pcidev_assert(const struct device *dev) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 50 | { |
| 51 | if (!dev) |
| 52 | pcidev_die(); |
Kyösti Mälkki | 34cf561 | 2019-03-11 20:34:26 +0200 | [diff] [blame] | 53 | return pcidev_bdf(dev); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 57 | u8 pci_read_config8(const struct device *dev, u16 reg) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 58 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 59 | return pci_s_read_config8(PCI_BDF(dev), reg); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 63 | u16 pci_read_config16(const struct device *dev, u16 reg) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 64 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 65 | return pci_s_read_config16(PCI_BDF(dev), reg); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 69 | u32 pci_read_config32(const struct device *dev, u16 reg) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 70 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 71 | return pci_s_read_config32(PCI_BDF(dev), reg); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 75 | void pci_write_config8(const struct device *dev, u16 reg, u8 val) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 76 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 77 | pci_s_write_config8(PCI_BDF(dev), reg, val); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 81 | void pci_write_config16(const struct device *dev, u16 reg, u16 val) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 82 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 83 | pci_s_write_config16(PCI_BDF(dev), reg, val); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 87 | void pci_write_config32(const struct device *dev, u16 reg, u32 val) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 88 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 89 | pci_s_write_config32(PCI_BDF(dev), reg, val); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Stefan Reinauer | 24d1d4b | 2013-03-21 11:51:41 -0700 | [diff] [blame] | 92 | #endif |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 93 | |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 94 | #ifdef __SIMPLE_DEVICE__ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 95 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 96 | void pci_or_config8(pci_devfn_t dev, u16 reg, u8 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 97 | #else |
| 98 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 99 | void pci_or_config8(const struct device *dev, u16 reg, u8 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 100 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 101 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 102 | u8 value = pci_read_config8(dev, reg); |
| 103 | pci_write_config8(dev, reg, value | ormask); |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 106 | #ifdef __SIMPLE_DEVICE__ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 107 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 108 | void pci_or_config16(pci_devfn_t dev, u16 reg, u16 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 109 | #else |
| 110 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 111 | void pci_or_config16(const struct device *dev, u16 reg, u16 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 112 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 113 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 114 | u16 value = pci_read_config16(dev, reg); |
| 115 | pci_write_config16(dev, reg, value | ormask); |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 118 | #ifdef __SIMPLE_DEVICE__ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 119 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 120 | void pci_or_config32(pci_devfn_t dev, u16 reg, u32 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 121 | #else |
| 122 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 123 | void pci_or_config32(const struct device *dev, u16 reg, u32 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 124 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 125 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 126 | u32 value = pci_read_config32(dev, reg); |
| 127 | pci_write_config32(dev, reg, value | ormask); |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 130 | #ifdef __SIMPLE_DEVICE__ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 131 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 132 | void pci_update_config8(pci_devfn_t dev, u16 reg, u8 mask, u8 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 133 | #else |
| 134 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 135 | void pci_update_config8(const struct device *dev, u16 reg, u8 mask, u8 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 136 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 137 | { |
| 138 | u8 reg8; |
| 139 | |
| 140 | reg8 = pci_read_config8(dev, reg); |
| 141 | reg8 &= mask; |
| 142 | reg8 |= or; |
| 143 | pci_write_config8(dev, reg, reg8); |
| 144 | } |
| 145 | |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 146 | #ifdef __SIMPLE_DEVICE__ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 147 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 148 | void pci_update_config16(pci_devfn_t dev, u16 reg, u16 mask, u16 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 149 | #else |
| 150 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 151 | void pci_update_config16(const struct device *dev, u16 reg, u16 mask, u16 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 152 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 153 | { |
| 154 | u16 reg16; |
| 155 | |
| 156 | reg16 = pci_read_config16(dev, reg); |
| 157 | reg16 &= mask; |
| 158 | reg16 |= or; |
| 159 | pci_write_config16(dev, reg, reg16); |
| 160 | } |
| 161 | |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 162 | #ifdef __SIMPLE_DEVICE__ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 163 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 164 | void pci_update_config32(pci_devfn_t dev, u16 reg, u32 mask, u32 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 165 | #else |
| 166 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 167 | void pci_update_config32(const struct device *dev, u16 reg, u32 mask, u32 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 168 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 169 | { |
| 170 | u32 reg32; |
| 171 | |
| 172 | reg32 = pci_read_config32(dev, reg); |
| 173 | reg32 &= mask; |
| 174 | reg32 |= or; |
| 175 | pci_write_config32(dev, reg, reg32); |
| 176 | } |
| 177 | |
Kyösti Mälkki | 9c0e14e | 2019-01-23 16:46:35 +0200 | [diff] [blame] | 178 | u16 pci_s_find_next_capability(pci_devfn_t dev, u16 cap, u16 last); |
| 179 | u16 pci_s_find_capability(pci_devfn_t dev, u16 cap); |
| 180 | |
| 181 | #ifndef __SIMPLE_DEVICE__ |
| 182 | static __always_inline |
| 183 | u16 pci_find_next_capability(const struct device *dev, u16 cap, u16 last) |
| 184 | { |
| 185 | return pci_s_find_next_capability(PCI_BDF(dev), cap, last); |
| 186 | } |
| 187 | |
| 188 | static __always_inline |
| 189 | u16 pci_find_capability(const struct device *dev, u16 cap) |
| 190 | { |
| 191 | return pci_s_find_capability(PCI_BDF(dev), cap); |
| 192 | } |
| 193 | #endif |
| 194 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 195 | #endif /* PCI_OPS_H */ |