| 1 |
|
| 2 |
|
| 3 |
#ifndef _PCI_MMIO_CFG_H |
| 4 |
#define _PCI_MMIO_CFG_H |
| 5 |
|
| 6 |
#include <stdint.h> |
| 7 |
#include <device/mmio.h> |
| 8 |
#include <device/pci_type.h> |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
union pci_bank { |
| 23 |
uint8_t reg8[4096]; |
| 24 |
uint16_t reg16[4096 / sizeof(uint16_t)]; |
| 25 |
uint32_t reg32[4096 / sizeof(uint32_t)]; |
| 26 |
}; |
| 27 |
|
| 28 |
#if CONFIG(ECAM_MMCONF_SUPPORT) |
| 29 |
|
| 30 |
#if CONFIG_ECAM_MMCONF_BASE_ADDRESS == 0 |
| 31 |
#error "CONFIG_ECAM_MMCONF_BASE_ADDRESS undefined!" |
| 32 |
#endif |
| 33 |
|
| 34 |
#if CONFIG_ECAM_MMCONF_BUS_NUMBER == 0 |
| 35 |
#error "CONFIG_ECAM_MMCONF_BUS_NUMBER is undefined!" |
| 36 |
#endif |
| 37 |
|
| 38 |
#if CONFIG_ECAM_MMCONF_BUS_NUMBER * MiB != CONFIG_ECAM_MMCONF_LENGTH |
| 39 |
#error "CONFIG_ECAM_MMCONF_LENGTH does not correspond with CONFIG_ECAM_MMCONF_BUS_NUMBER!" |
| 40 |
#endif |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
extern u8 *const pci_mmconf; |
| 45 |
|
| 46 |
static __always_inline |
| 47 |
volatile union pci_bank *pci_map_bus(pci_devfn_t dev) |
| 48 |
{ |
| 49 |
return (void *)&pci_mmconf[PCI_DEVFN_OFFSET(dev)]; |
| 50 |
} |
| 51 |
|
| 52 |
#else |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
volatile union pci_bank *pci_map_bus(pci_devfn_t dev); |
| 57 |
|
| 58 |
#endif |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static __always_inline |
| 67 |
uint8_t pci_s_read_config8(pci_devfn_t dev, uint16_t reg) |
| 68 |
{ |
| 69 |
return pci_map_bus(dev)->reg8[reg]; |
| 70 |
} |
| 71 |
|
| 72 |
static __always_inline |
| 73 |
uint16_t pci_s_read_config16(pci_devfn_t dev, uint16_t reg) |
| 74 |
{ |
| 75 |
return pci_map_bus(dev)->reg16[reg / sizeof(uint16_t)]; |
| 76 |
} |
| 77 |
|
| 78 |
static __always_inline |
| 79 |
uint32_t pci_s_read_config32(pci_devfn_t dev, uint16_t reg) |
| 80 |
{ |
| 81 |
return pci_map_bus(dev)->reg32[reg / sizeof(uint32_t)]; |
| 82 |
} |
| 83 |
|
| 84 |
static __always_inline |
| 85 |
void pci_s_write_config8(pci_devfn_t dev, uint16_t reg, uint8_t value) |
| 86 |
{ |
| 87 |
pci_map_bus(dev)->reg8[reg] = value; |
| 88 |
} |
| 89 |
|
| 90 |
static __always_inline |
| 91 |
void pci_s_write_config16(pci_devfn_t dev, uint16_t reg, uint16_t value) |
| 92 |
{ |
| 93 |
pci_map_bus(dev)->reg16[reg / sizeof(uint16_t)] = value; |
| 94 |
} |
| 95 |
|
| 96 |
static __always_inline |
| 97 |
void pci_s_write_config32(pci_devfn_t dev, uint16_t reg, uint32_t value) |
| 98 |
{ |
| 99 |
pci_map_bus(dev)->reg32[reg / sizeof(uint32_t)] = value; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
static __always_inline |
| 109 |
uint8_t *pci_mmio_config8_addr(pci_devfn_t dev, uint16_t reg) |
| 110 |
{ |
| 111 |
return (uint8_t *)&pci_map_bus(dev)->reg8[reg]; |
| 112 |
} |
| 113 |
|
| 114 |
static __always_inline |
| 115 |
uint16_t *pci_mmio_config16_addr(pci_devfn_t dev, uint16_t reg) |
| 116 |
{ |
| 117 |
return (uint16_t *)&pci_map_bus(dev)->reg16[reg / sizeof(uint16_t)]; |
| 118 |
} |
| 119 |
|
| 120 |
static __always_inline |
| 121 |
uint32_t *pci_mmio_config32_addr(pci_devfn_t dev, uint16_t reg) |
| 122 |
{ |
| 123 |
return (uint32_t *)&pci_map_bus(dev)->reg32[reg / sizeof(uint32_t)]; |
| 124 |
} |
| 125 |
|
| 126 |
#endif |