Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 2 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 3 | #ifndef PCI_OPS_H |
| 4 | #define PCI_OPS_H |
| 5 | |
| 6 | #include <stdint.h> |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 7 | #include <device/device.h> |
Kyösti Mälkki | 3e6913b | 2019-03-02 16:26:10 +0200 | [diff] [blame] | 8 | #include <device/pci_type.h> |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 9 | #include <arch/pci_ops.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 10 | |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 11 | void __noreturn pcidev_die(void); |
| 12 | |
Kyösti Mälkki | 34cf561 | 2019-03-11 20:34:26 +0200 | [diff] [blame] | 13 | static __always_inline pci_devfn_t pcidev_bdf(const struct device *dev) |
| 14 | { |
| 15 | return (dev->path.pci.devfn << 12) | (dev->bus->secondary << 20); |
| 16 | } |
| 17 | |
| 18 | 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] | 19 | { |
| 20 | if (!dev) |
| 21 | pcidev_die(); |
Kyösti Mälkki | 34cf561 | 2019-03-11 20:34:26 +0200 | [diff] [blame] | 22 | return pcidev_bdf(dev); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 23 | } |
Kyösti Mälkki | 3f98d41 | 2019-07-29 16:38:14 +0300 | [diff] [blame] | 24 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 25 | #if defined(__SIMPLE_DEVICE__) |
| 26 | #define ENV_PCI_SIMPLE_DEVICE 1 |
| 27 | #else |
| 28 | #define ENV_PCI_SIMPLE_DEVICE 0 |
| 29 | #endif |
| 30 | |
| 31 | #if ENV_PCI_SIMPLE_DEVICE |
Kyösti Mälkki | 3f98d41 | 2019-07-29 16:38:14 +0300 | [diff] [blame] | 32 | |
| 33 | /* Avoid name collisions as different stages have different signature |
| 34 | * for these functions. The _s_ stands for simple, fundamental IO or |
| 35 | * MMIO variant. |
| 36 | */ |
| 37 | #define pci_read_config8 pci_s_read_config8 |
| 38 | #define pci_read_config16 pci_s_read_config16 |
| 39 | #define pci_read_config32 pci_s_read_config32 |
| 40 | #define pci_write_config8 pci_s_write_config8 |
| 41 | #define pci_write_config16 pci_s_write_config16 |
| 42 | #define pci_write_config32 pci_s_write_config32 |
| 43 | #else |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 44 | |
| 45 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 46 | u8 pci_read_config8(const struct device *dev, u16 reg) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 47 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 48 | return pci_s_read_config8(PCI_BDF(dev), reg); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 52 | u16 pci_read_config16(const struct device *dev, u16 reg) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 53 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 54 | return pci_s_read_config16(PCI_BDF(dev), reg); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 58 | u32 pci_read_config32(const struct device *dev, u16 reg) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 59 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 60 | return pci_s_read_config32(PCI_BDF(dev), reg); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 64 | 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] | 65 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 66 | pci_s_write_config8(PCI_BDF(dev), reg, val); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 70 | 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] | 71 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 72 | pci_s_write_config16(PCI_BDF(dev), reg, val); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 76 | 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] | 77 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 78 | pci_s_write_config32(PCI_BDF(dev), reg, val); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Stefan Reinauer | 24d1d4b | 2013-03-21 11:51:41 -0700 | [diff] [blame] | 81 | #endif |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 82 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 83 | #if ENV_PCI_SIMPLE_DEVICE |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 84 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 85 | 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] | 86 | #else |
| 87 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 88 | 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] | 89 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 90 | { |
| 91 | u8 reg8; |
| 92 | |
| 93 | reg8 = pci_read_config8(dev, reg); |
| 94 | reg8 &= mask; |
| 95 | reg8 |= or; |
| 96 | pci_write_config8(dev, reg, reg8); |
| 97 | } |
| 98 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 99 | #if ENV_PCI_SIMPLE_DEVICE |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 100 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 101 | 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] | 102 | #else |
| 103 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 104 | 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] | 105 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 106 | { |
| 107 | u16 reg16; |
| 108 | |
| 109 | reg16 = pci_read_config16(dev, reg); |
| 110 | reg16 &= mask; |
| 111 | reg16 |= or; |
| 112 | pci_write_config16(dev, reg, reg16); |
| 113 | } |
| 114 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 115 | #if ENV_PCI_SIMPLE_DEVICE |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 116 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 117 | 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] | 118 | #else |
| 119 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 120 | 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] | 121 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 122 | { |
| 123 | u32 reg32; |
| 124 | |
| 125 | reg32 = pci_read_config32(dev, reg); |
| 126 | reg32 &= mask; |
| 127 | reg32 |= or; |
| 128 | pci_write_config32(dev, reg, reg32); |
| 129 | } |
| 130 | |
Angel Pons | 2f3456a | 2020-06-07 18:31:33 +0200 | [diff] [blame] | 131 | #if ENV_PCI_SIMPLE_DEVICE |
| 132 | static __always_inline |
| 133 | void pci_and_config8(pci_devfn_t dev, u16 reg, u8 andmask) |
| 134 | #else |
| 135 | static __always_inline |
| 136 | void pci_and_config8(const struct device *dev, u16 reg, u8 andmask) |
| 137 | #endif |
| 138 | { |
| 139 | pci_update_config8(dev, reg, andmask, 0); |
| 140 | } |
| 141 | |
| 142 | #if ENV_PCI_SIMPLE_DEVICE |
| 143 | static __always_inline |
| 144 | void pci_and_config16(pci_devfn_t dev, u16 reg, u16 andmask) |
| 145 | #else |
| 146 | static __always_inline |
| 147 | void pci_and_config16(const struct device *dev, u16 reg, u16 andmask) |
| 148 | #endif |
| 149 | { |
| 150 | pci_update_config16(dev, reg, andmask, 0); |
| 151 | } |
| 152 | |
| 153 | #if ENV_PCI_SIMPLE_DEVICE |
| 154 | static __always_inline |
| 155 | void pci_and_config32(pci_devfn_t dev, u16 reg, u32 andmask) |
| 156 | #else |
| 157 | static __always_inline |
| 158 | void pci_and_config32(const struct device *dev, u16 reg, u32 andmask) |
| 159 | #endif |
| 160 | { |
| 161 | pci_update_config32(dev, reg, andmask, 0); |
| 162 | } |
| 163 | |
| 164 | #if ENV_PCI_SIMPLE_DEVICE |
| 165 | static __always_inline |
| 166 | void pci_or_config8(pci_devfn_t dev, u16 reg, u8 ormask) |
| 167 | #else |
| 168 | static __always_inline |
| 169 | void pci_or_config8(const struct device *dev, u16 reg, u8 ormask) |
| 170 | #endif |
| 171 | { |
| 172 | pci_update_config8(dev, reg, 0xff, ormask); |
| 173 | } |
| 174 | |
| 175 | #if ENV_PCI_SIMPLE_DEVICE |
| 176 | static __always_inline |
| 177 | void pci_or_config16(pci_devfn_t dev, u16 reg, u16 ormask) |
| 178 | #else |
| 179 | static __always_inline |
| 180 | void pci_or_config16(const struct device *dev, u16 reg, u16 ormask) |
| 181 | #endif |
| 182 | { |
| 183 | pci_update_config16(dev, reg, 0xffff, ormask); |
| 184 | } |
| 185 | |
| 186 | #if ENV_PCI_SIMPLE_DEVICE |
| 187 | static __always_inline |
| 188 | void pci_or_config32(pci_devfn_t dev, u16 reg, u32 ormask) |
| 189 | #else |
| 190 | static __always_inline |
| 191 | void pci_or_config32(const struct device *dev, u16 reg, u32 ormask) |
| 192 | #endif |
| 193 | { |
| 194 | pci_update_config32(dev, reg, 0xffffffff, ormask); |
| 195 | } |
| 196 | |
Kyösti Mälkki | 9c0e14e | 2019-01-23 16:46:35 +0200 | [diff] [blame] | 197 | u16 pci_s_find_next_capability(pci_devfn_t dev, u16 cap, u16 last); |
| 198 | u16 pci_s_find_capability(pci_devfn_t dev, u16 cap); |
| 199 | |
Kyösti Mälkki | 9c0e14e | 2019-01-23 16:46:35 +0200 | [diff] [blame] | 200 | static __always_inline |
| 201 | u16 pci_find_next_capability(const struct device *dev, u16 cap, u16 last) |
| 202 | { |
| 203 | return pci_s_find_next_capability(PCI_BDF(dev), cap, last); |
| 204 | } |
| 205 | |
| 206 | static __always_inline |
| 207 | u16 pci_find_capability(const struct device *dev, u16 cap) |
| 208 | { |
| 209 | return pci_s_find_capability(PCI_BDF(dev), cap); |
| 210 | } |
Kyösti Mälkki | 9c0e14e | 2019-01-23 16:46:35 +0200 | [diff] [blame] | 211 | |
Tim Wawrzynczak | 8a1ad13 | 2020-11-05 14:38:51 -0700 | [diff] [blame] | 212 | /* |
| 213 | * Determine if the given PCI device is the source of wake from sleep by checking PME_STATUS and |
| 214 | * PME_ENABLE bits in PM control and status register. |
| 215 | * |
| 216 | * Returns true if PCI device is wake source, false otherwise. |
| 217 | */ |
Tim Wawrzynczak | 93982c3 | 2021-04-29 09:45:59 -0600 | [diff] [blame] | 218 | bool pci_dev_is_wake_source(pci_devfn_t dev); |
Tim Wawrzynczak | 8a1ad13 | 2020-11-05 14:38:51 -0700 | [diff] [blame] | 219 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 220 | #endif /* PCI_OPS_H */ |