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 | 78d1432 | 2019-01-23 15:57:49 +0200 | [diff] [blame] | 42 | const struct pci_bus_operations *pci_bus_default_ops(void); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 43 | |
| 44 | static __always_inline const struct pci_bus_operations *pci_bus_ops(void) |
| 45 | { |
| 46 | return pci_bus_default_ops(); |
| 47 | } |
| 48 | |
| 49 | void __noreturn pcidev_die(void); |
| 50 | |
| 51 | static __always_inline void pcidev_assert(const struct device *dev) |
| 52 | { |
| 53 | if (!dev) |
| 54 | pcidev_die(); |
| 55 | } |
| 56 | |
| 57 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 58 | u8 pci_read_config8(const struct device *dev, u16 where) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 59 | { |
| 60 | pcidev_assert(dev); |
| 61 | return pci_bus_ops()->read8(dev, where); |
| 62 | } |
| 63 | |
| 64 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 65 | u16 pci_read_config16(const struct device *dev, u16 where) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 66 | { |
| 67 | pcidev_assert(dev); |
| 68 | return pci_bus_ops()->read16(dev, where); |
| 69 | } |
| 70 | |
| 71 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 72 | u32 pci_read_config32(const struct device *dev, u16 where) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 73 | { |
| 74 | pcidev_assert(dev); |
| 75 | return pci_bus_ops()->read32(dev, where); |
| 76 | } |
| 77 | |
| 78 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 79 | void pci_write_config8(const struct device *dev, u16 where, u8 val) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 80 | { |
| 81 | pcidev_assert(dev); |
| 82 | pci_bus_ops()->write8(dev, where, val); |
| 83 | } |
| 84 | |
| 85 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 86 | void pci_write_config16(const struct device *dev, u16 where, u16 val) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 87 | { |
| 88 | pcidev_assert(dev); |
| 89 | pci_bus_ops()->write16(dev, where, val); |
| 90 | } |
| 91 | |
| 92 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 93 | void pci_write_config32(const struct device *dev, u16 where, u32 val) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 94 | { |
| 95 | pcidev_assert(dev); |
| 96 | pci_bus_ops()->write32(dev, where, val); |
| 97 | } |
| 98 | |
Stefan Reinauer | 24d1d4b | 2013-03-21 11:51:41 -0700 | [diff] [blame] | 99 | #endif |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 100 | |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 101 | #ifdef __SIMPLE_DEVICE__ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 102 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 103 | void pci_or_config8(pci_devfn_t dev, u16 where, u8 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 104 | #else |
| 105 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 106 | void pci_or_config8(const struct device *dev, u16 where, u8 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 107 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 108 | { |
| 109 | u8 value = pci_read_config8(dev, where); |
| 110 | pci_write_config8(dev, where, value | ormask); |
| 111 | } |
| 112 | |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 113 | #ifdef __SIMPLE_DEVICE__ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 114 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 115 | void pci_or_config16(pci_devfn_t dev, u16 where, u16 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 116 | #else |
| 117 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 118 | void pci_or_config16(const struct device *dev, u16 where, u16 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 119 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 120 | { |
| 121 | u16 value = pci_read_config16(dev, where); |
| 122 | pci_write_config16(dev, where, value | ormask); |
| 123 | } |
| 124 | |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 125 | #ifdef __SIMPLE_DEVICE__ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 126 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 127 | void pci_or_config32(pci_devfn_t dev, u16 where, u32 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 128 | #else |
| 129 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 130 | void pci_or_config32(const struct device *dev, u16 where, u32 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 131 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 132 | { |
| 133 | u32 value = pci_read_config32(dev, where); |
| 134 | pci_write_config32(dev, where, value | ormask); |
| 135 | } |
| 136 | |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 137 | #ifdef __SIMPLE_DEVICE__ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 138 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 139 | 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] | 140 | #else |
| 141 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 142 | 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] | 143 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 144 | { |
| 145 | u8 reg8; |
| 146 | |
| 147 | reg8 = pci_read_config8(dev, reg); |
| 148 | reg8 &= mask; |
| 149 | reg8 |= or; |
| 150 | pci_write_config8(dev, reg, reg8); |
| 151 | } |
| 152 | |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 153 | #ifdef __SIMPLE_DEVICE__ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 154 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 155 | 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] | 156 | #else |
| 157 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 158 | 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] | 159 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 160 | { |
| 161 | u16 reg16; |
| 162 | |
| 163 | reg16 = pci_read_config16(dev, reg); |
| 164 | reg16 &= mask; |
| 165 | reg16 |= or; |
| 166 | pci_write_config16(dev, reg, reg16); |
| 167 | } |
| 168 | |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 169 | #ifdef __SIMPLE_DEVICE__ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 170 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 171 | 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] | 172 | #else |
| 173 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame^] | 174 | 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] | 175 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 176 | { |
| 177 | u32 reg32; |
| 178 | |
| 179 | reg32 = pci_read_config32(dev, reg); |
| 180 | reg32 &= mask; |
| 181 | reg32 |= or; |
| 182 | pci_write_config32(dev, reg, reg32); |
| 183 | } |
| 184 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 185 | #endif /* PCI_OPS_H */ |