Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | /* * Copyright (c) 2009-2010, 2013-2014 Wind River Systems, Inc. * * SPDX-License-Identifier: Apache-2.0 */ /** * @file * @brief PCI bus support * * * This module implements the PCI config space access functions * */ #include <kernel.h> #include <arch/cpu.h> #include <pci/pci_mgr.h> #include <string.h> /** * * @brief Write a 32bit data to pci reg in offset * * @param bus_no Bus number. * @param device_no Device number * @param func_no Function number * @param offset Offset into the configuration space. * @param data Data written to the offset. * * @return N/A */ void pci_config_out_long(uint32_t bus_no, uint32_t device_no, uint32_t func_no, uint32_t offset, uint32_t data) { union pci_addr_reg pci_addr; /* create the PCI address we're going to access */ pci_addr.field.bus = bus_no; pci_addr.field.device = device_no; pci_addr.field.func = func_no; pci_addr.field.reg = offset / 4; pci_addr.field.offset = 0; /* write to the PCI controller */ pci_write(DEFAULT_PCI_CONTROLLER, pci_addr, sizeof(uint32_t), data); } /** * * @brief Write a 16bit data to pci reg in offset * * @param bus_no Bus number. * @param device_no Device number. * @param func_no Function number. * @param offset Offset into the configuration space. * @param data Data written to the offset. * * @return N/A */ void pci_config_out_word(uint32_t bus_no, uint32_t device_no, uint32_t func_no, uint32_t offset, uint16_t data) { union pci_addr_reg pci_addr; /* create the PCI address we're going to access */ pci_addr.field.bus = bus_no; pci_addr.field.device = device_no; pci_addr.field.func = func_no; pci_addr.field.reg = offset / 4; pci_addr.field.offset = offset & 2; /* write to the PCI controller */ pci_write(DEFAULT_PCI_CONTROLLER, pci_addr, sizeof(uint16_t), data); } /** * * @brief Write a 8bit data to pci reg in offset * * @param bus_no Bus number. * @param device_no Device number. * @param func_no Function number. * @param offset Offset into the configuration space. * @param data Data written to the offset. * * @return N/A */ void pci_config_out_byte(uint32_t bus_no, uint32_t device_no, uint32_t func_no, uint32_t offset, uint8_t data) { union pci_addr_reg pci_addr; /* create the PCI address we're going to access */ pci_addr.field.bus = bus_no; pci_addr.field.device = device_no; pci_addr.field.func = func_no; pci_addr.field.reg = offset / 4; pci_addr.field.offset = offset % 4; /* write to the PCI controller */ pci_write(DEFAULT_PCI_CONTROLLER, pci_addr, sizeof(uint8_t), data); } /** * * @brief Read a 32bit data from pci reg in offset * * @param bus_no Bus number. * @param device_no Device number. * @param func_no Function number. * @param offset Offset into the configuration space. * @param data Data read from the offset. * * @return N/A * */ void pci_config_in_long(uint32_t bus_no, uint32_t device_no, uint32_t func_no, uint32_t offset, uint32_t *data) { union pci_addr_reg pci_addr; /* create the PCI address we're going to access */ pci_addr.field.bus = bus_no; pci_addr.field.device = device_no; pci_addr.field.func = func_no; pci_addr.field.reg = offset / 4; pci_addr.field.offset = 0; /* read from the PCI controller */ pci_read(DEFAULT_PCI_CONTROLLER, pci_addr, sizeof(uint32_t), data); } /** * * @brief Read in a 16bit data from a pci reg in offset * * @param bus_no Bus number. * @param device_no Device number. * @param func_no Function number. * @param offset Offset into the configuration space. * @param data Data read from the offset. * * @return N/A * */ void pci_config_in_word(uint32_t bus_no, uint32_t device_no, uint32_t func_no, uint32_t offset, uint16_t *data) { union pci_addr_reg pci_addr; uint32_t pci_data; /* create the PCI address we're going to access */ pci_addr.field.bus = bus_no; pci_addr.field.device = device_no; pci_addr.field.func = func_no; pci_addr.field.reg = offset / 4; pci_addr.field.offset = offset & 2; /* read from the PCI controller */ pci_read(DEFAULT_PCI_CONTROLLER, pci_addr, sizeof(uint16_t), &pci_data); /* return the data */ *data = (uint16_t)pci_data; } /** * * @brief Read in a 8bit data from a pci reg in offset * * @param bus_no Bus number. * @param device_no Device number. * @param func_no Function number. * @param offset Offset into the configuration space. * @param data Data read from the offset. * * @return N/A * */ void pci_config_in_byte(uint32_t bus_no, uint32_t device_no, uint32_t func_no, uint32_t offset, uint8_t *data) { union pci_addr_reg pci_addr; uint32_t pci_data; /* create the PCI address we're going to access */ pci_addr.field.bus = bus_no; pci_addr.field.device = device_no; pci_addr.field.func = func_no; pci_addr.field.reg = offset / 4; pci_addr.field.offset = offset % 4; /* read from the PCI controller */ pci_read(DEFAULT_PCI_CONTROLLER, pci_addr, sizeof(uint8_t), &pci_data); /* return the data */ *data = (uint8_t)pci_data; } /** * * @brief Find extended capability in ECP linked list * * This routine searches for an extended capability in the linked list of * capabilities in config space. If found, the offset of the first byte * of the capability of interest in config space is returned via pOffset. * * @param ext_cap_find_id Extended capabilities ID to search for. * @param bus PCI bus number. * @param device PCI device number. * @param function PCI function number. * @param p_offset Returned config space offset. * * @return 0 if Extended Capability found, -1 otherwise * */ int pci_config_ext_cap_ptr_find(uint8_t ext_cap_find_id, uint32_t bus, uint32_t device, uint32_t function, uint8_t *p_offset) { uint16_t tmp_stat; uint8_t tmp_offset; uint8_t cap_offset = 0x00; uint8_t cap_id = 0x00; /* Check to see if the device has any extended capabilities */ pci_config_in_word(bus, device, function, PCI_CFG_STATUS, &tmp_stat); if ((tmp_stat & PCI_STATUS_NEW_CAP) == 0) { return -1; } /* Get the initial ECP offset and make longword aligned */ pci_config_in_byte(bus, device, function, PCI_CFG_CAP_PTR, &cap_offset); cap_offset &= ~0x02; /* Bounds check the ECP offset */ if (cap_offset < 0x40) { return -1; } /* Look for the specified Extended Cap item in the linked list */ while (cap_offset != 0x00) { /* Get the Capability ID and check */ pci_config_in_byte(bus, device, function, (int)cap_offset, &cap_id); if (cap_id == ext_cap_find_id) { *p_offset = cap_offset; return 0; } /* Get the offset to the next New Capabilities item */ tmp_offset = cap_offset + (uint8_t)0x01; pci_config_in_byte(bus, device, function, (int)tmp_offset, &cap_offset); } return -1; } |