[8c06905] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * @defgroup pciintel pci bus driver for intel method 1.
|
---|
| 31 | * @brief HelenOS root pci bus driver for intel method 1.
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | /** @file
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <assert.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 | #include <errno.h>
|
---|
| 41 | #include <bool.h>
|
---|
| 42 | #include <fibril_synch.h>
|
---|
[c47e1a8] | 43 | #include <str.h>
|
---|
[8c06905] | 44 | #include <ctype.h>
|
---|
| 45 | #include <macros.h>
|
---|
| 46 |
|
---|
| 47 | #include <driver.h>
|
---|
| 48 | #include <devman.h>
|
---|
| 49 | #include <ipc/devman.h>
|
---|
| 50 | #include <ipc/dev_iface.h>
|
---|
| 51 | #include <resource.h>
|
---|
| 52 | #include <device/hw_res.h>
|
---|
| 53 | #include <ddi.h>
|
---|
[5e598e0] | 54 | #include <libarch/ddi.h>
|
---|
| 55 |
|
---|
| 56 | #include "pci.h"
|
---|
[8c06905] | 57 |
|
---|
| 58 | #define NAME "pciintel"
|
---|
| 59 |
|
---|
[5e598e0] | 60 | #define CONF_ADDR(bus, dev, fn, reg) ((1 << 31) | (bus << 16) | (dev << 11) | (fn << 8) | (reg & ~3))
|
---|
| 61 |
|
---|
| 62 |
|
---|
[3843ecb] | 63 | static hw_resource_list_t * pciintel_get_child_resources(device_t *dev)
|
---|
| 64 | {
|
---|
| 65 | pci_dev_data_t *dev_data = (pci_dev_data_t *)dev->driver_data;
|
---|
| 66 | if (NULL == dev_data) {
|
---|
| 67 | return NULL;
|
---|
| 68 | }
|
---|
| 69 | return &dev_data->hw_resources;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | static bool pciintel_enable_child_interrupt(device_t *dev)
|
---|
| 73 | {
|
---|
| 74 | // TODO
|
---|
| 75 |
|
---|
| 76 | return false;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | static resource_iface_t pciintel_child_res_iface = {
|
---|
| 80 | &pciintel_get_child_resources,
|
---|
| 81 | &pciintel_enable_child_interrupt
|
---|
| 82 | };
|
---|
| 83 |
|
---|
[5159ae9] | 84 | static device_ops_t pci_child_ops;
|
---|
[3843ecb] | 85 |
|
---|
| 86 |
|
---|
[df747b9c] | 87 | static int pci_add_device(device_t *dev);
|
---|
[8c06905] | 88 |
|
---|
| 89 | /** The pci bus driver's standard operations.
|
---|
| 90 | */
|
---|
| 91 | static driver_ops_t pci_ops = {
|
---|
| 92 | .add_device = &pci_add_device
|
---|
| 93 | };
|
---|
| 94 |
|
---|
| 95 | /** The pci bus driver structure.
|
---|
| 96 | */
|
---|
| 97 | static driver_t pci_driver = {
|
---|
| 98 | .name = NAME,
|
---|
| 99 | .driver_ops = &pci_ops
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 | typedef struct pciintel_bus_data {
|
---|
[d1fc8f0] | 103 | uint32_t conf_io_addr;
|
---|
[8c06905] | 104 | void *conf_data_port;
|
---|
| 105 | void *conf_addr_port;
|
---|
[5e598e0] | 106 | fibril_mutex_t conf_mutex;
|
---|
[8c06905] | 107 | } pci_bus_data_t;
|
---|
| 108 |
|
---|
[5e598e0] | 109 | static inline pci_bus_data_t *create_pci_bus_data()
|
---|
| 110 | {
|
---|
| 111 | pci_bus_data_t *bus_data = (pci_bus_data_t *)malloc(sizeof(pci_bus_data_t));
|
---|
| 112 | if(NULL != bus_data) {
|
---|
| 113 | memset(bus_data, 0, sizeof(pci_bus_data_t));
|
---|
| 114 | fibril_mutex_initialize(&bus_data->conf_mutex);
|
---|
| 115 | }
|
---|
| 116 | return bus_data;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | static inline void delete_pci_bus_data(pci_bus_data_t *bus_data)
|
---|
| 120 | {
|
---|
| 121 | free(bus_data);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | static void pci_conf_read(device_t *dev, int reg, uint8_t *buf, size_t len)
|
---|
| 125 | {
|
---|
| 126 | assert(NULL != dev->parent);
|
---|
| 127 |
|
---|
| 128 | pci_dev_data_t *dev_data = (pci_dev_data_t *)dev->driver_data;
|
---|
| 129 | pci_bus_data_t *bus_data = (pci_bus_data_t *)dev->parent->driver_data;
|
---|
| 130 |
|
---|
| 131 | fibril_mutex_lock(&bus_data->conf_mutex);
|
---|
| 132 |
|
---|
| 133 | uint32_t conf_addr = CONF_ADDR(dev_data->bus, dev_data->dev, dev_data->fn, reg);
|
---|
| 134 | void *addr = bus_data->conf_data_port + (reg & 3);
|
---|
| 135 |
|
---|
| 136 | pio_write_32(bus_data->conf_addr_port, conf_addr);
|
---|
| 137 |
|
---|
| 138 | switch (len) {
|
---|
| 139 | case 1:
|
---|
| 140 | buf[0] = pio_read_8(addr);
|
---|
| 141 | break;
|
---|
| 142 | case 2:
|
---|
| 143 | ((uint16_t *)buf)[0] = pio_read_16(addr);
|
---|
| 144 | break;
|
---|
| 145 | case 4:
|
---|
| 146 | ((uint32_t *)buf)[0] = pio_read_32(addr);
|
---|
| 147 | break;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | fibril_mutex_unlock(&bus_data->conf_mutex);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[d1fc8f0] | 153 | static void pci_conf_write(device_t *dev, int reg, uint8_t *buf, size_t len)
|
---|
| 154 | {
|
---|
| 155 | assert(NULL != dev->parent);
|
---|
| 156 |
|
---|
| 157 | pci_dev_data_t *dev_data = (pci_dev_data_t *)dev->driver_data;
|
---|
| 158 | pci_bus_data_t *bus_data = (pci_bus_data_t *)dev->parent->driver_data;
|
---|
| 159 |
|
---|
| 160 | fibril_mutex_lock(&bus_data->conf_mutex);
|
---|
| 161 |
|
---|
| 162 | uint32_t conf_addr = CONF_ADDR(dev_data->bus, dev_data->dev, dev_data->fn, reg);
|
---|
| 163 | void *addr = bus_data->conf_data_port + (reg & 3);
|
---|
| 164 |
|
---|
| 165 | pio_write_32(bus_data->conf_addr_port, conf_addr);
|
---|
| 166 |
|
---|
| 167 | switch (len) {
|
---|
| 168 | case 1:
|
---|
| 169 | pio_write_8(addr, buf[0]);
|
---|
| 170 | break;
|
---|
| 171 | case 2:
|
---|
| 172 | pio_write_16(addr, ((uint16_t *)buf)[0]);
|
---|
| 173 | break;
|
---|
| 174 | case 4:
|
---|
| 175 | pio_write_32(addr, ((uint32_t *)buf)[0]);
|
---|
| 176 | break;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | fibril_mutex_unlock(&bus_data->conf_mutex);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[5e598e0] | 182 | uint8_t pci_conf_read_8(device_t *dev, int reg)
|
---|
| 183 | {
|
---|
| 184 | uint8_t res;
|
---|
| 185 | pci_conf_read(dev, reg, &res, 1);
|
---|
| 186 | return res;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | uint16_t pci_conf_read_16(device_t *dev, int reg)
|
---|
| 190 | {
|
---|
| 191 | uint16_t res;
|
---|
| 192 | pci_conf_read(dev, reg, (uint8_t *)&res, 2);
|
---|
| 193 | return res;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | uint32_t pci_conf_read_32(device_t *dev, int reg)
|
---|
| 197 | {
|
---|
| 198 | uint32_t res;
|
---|
| 199 | pci_conf_read(dev, reg, (uint8_t *)&res, 4);
|
---|
| 200 | return res;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[d1fc8f0] | 203 | void pci_conf_write_8(device_t *dev, int reg, uint8_t val)
|
---|
| 204 | {
|
---|
[3a5909f] | 205 | pci_conf_write(dev, reg, (uint8_t *)&val, 1);
|
---|
[d1fc8f0] | 206 | }
|
---|
| 207 |
|
---|
| 208 | void pci_conf_write_16(device_t *dev, int reg, uint16_t val)
|
---|
| 209 | {
|
---|
[3a5909f] | 210 | pci_conf_write(dev, reg, (uint8_t *)&val, 2);
|
---|
[d1fc8f0] | 211 | }
|
---|
| 212 |
|
---|
| 213 | void pci_conf_write_32(device_t *dev, int reg, uint32_t val)
|
---|
| 214 | {
|
---|
| 215 | pci_conf_write(dev, reg, (uint8_t *)&val, 4);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 |
|
---|
[89ce401a] | 219 | void create_pci_match_ids(device_t *dev)
|
---|
| 220 | {
|
---|
| 221 | pci_dev_data_t *dev_data = (pci_dev_data_t *)dev->driver_data;
|
---|
| 222 | match_id_t *match_id = NULL;
|
---|
[c47e1a8] | 223 | char *match_id_str;
|
---|
[89ce401a] | 224 | match_id = create_match_id();
|
---|
| 225 | if (NULL != match_id) {
|
---|
[c47e1a8] | 226 | asprintf(&match_id_str, "pci/ven=%04x&dev=%04x", dev_data->vendor_id, dev_data->device_id);
|
---|
| 227 | match_id->id = match_id_str;
|
---|
[89ce401a] | 228 | match_id->score = 90;
|
---|
| 229 | add_match_id(&dev->match_ids, match_id);
|
---|
| 230 | }
|
---|
| 231 | // TODO add more ids (with subsys ids, using class id etc.)
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[3a5909f] | 234 | void pci_add_range(device_t *dev, uint64_t range_addr, size_t range_size, bool io)
|
---|
[d1fc8f0] | 235 | {
|
---|
[3a5909f] | 236 | pci_dev_data_t *dev_data = (pci_dev_data_t *)dev->driver_data;
|
---|
| 237 | hw_resource_list_t *hw_res_list = &dev_data->hw_resources;
|
---|
| 238 | hw_resource_t *hw_resources = hw_res_list->resources;
|
---|
| 239 | size_t count = hw_res_list->count;
|
---|
| 240 |
|
---|
| 241 | assert(NULL != hw_resources);
|
---|
| 242 | assert(count < PCI_MAX_HW_RES);
|
---|
| 243 |
|
---|
| 244 | if (io) {
|
---|
| 245 | hw_resources[count].type = IO_RANGE;
|
---|
| 246 | hw_resources[count].res.io_range.address = range_addr;
|
---|
| 247 | hw_resources[count].res.io_range.size = range_size;
|
---|
| 248 | hw_resources[count].res.io_range.endianness = LITTLE_ENDIAN;
|
---|
| 249 | } else {
|
---|
| 250 | hw_resources[count].type = MEM_RANGE;
|
---|
| 251 | hw_resources[count].res.mem_range.address = range_addr;
|
---|
| 252 | hw_resources[count].res.mem_range.size = range_size;
|
---|
| 253 | hw_resources[count].res.mem_range.endianness = LITTLE_ENDIAN;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | hw_res_list->count++;
|
---|
[d1fc8f0] | 257 | }
|
---|
| 258 |
|
---|
[3a5909f] | 259 |
|
---|
[d1fc8f0] | 260 | /** Read the base address register (BAR) of the device
|
---|
| 261 | * and if it contains valid address add it to the devices hw resource list.
|
---|
| 262 | *
|
---|
| 263 | * @param dev the pci device.
|
---|
| 264 | * @param addr the address of the BAR in the PCI configuration address space of the device.
|
---|
| 265 | *
|
---|
| 266 | * @return the addr the address of the BAR which should be read next.
|
---|
| 267 | */
|
---|
[3a5909f] | 268 | int pci_read_bar(device_t *dev, int addr)
|
---|
[d1fc8f0] | 269 | {
|
---|
| 270 | // value of the BAR
|
---|
| 271 | uint32_t val, mask;
|
---|
| 272 | // IO space address
|
---|
| 273 | bool io;
|
---|
| 274 | // 64-bit wide address
|
---|
| 275 | bool w64;
|
---|
| 276 |
|
---|
| 277 | // size of the io or memory range specified by the BAR
|
---|
| 278 | size_t range_size;
|
---|
| 279 | // beginning of the io or memory range specified by the BAR
|
---|
| 280 | uint64_t range_addr;
|
---|
| 281 |
|
---|
[3a5909f] | 282 | // get the value of the BAR
|
---|
[d1fc8f0] | 283 | val = pci_conf_read_32(dev, addr);
|
---|
| 284 |
|
---|
| 285 | io = (bool)(val & 1);
|
---|
| 286 | if (io) {
|
---|
| 287 | w64 = false;
|
---|
| 288 | } else {
|
---|
| 289 | switch ((val >> 1) & 3) {
|
---|
| 290 | case 0:
|
---|
| 291 | w64 = false;
|
---|
| 292 | break;
|
---|
| 293 | case 2:
|
---|
| 294 | w64 = true;
|
---|
| 295 | break;
|
---|
| 296 | default:
|
---|
| 297 | // reserved, go to the next BAR
|
---|
| 298 | return addr + 4;
|
---|
| 299 | }
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | // get the address mask
|
---|
| 303 | pci_conf_write_32(dev, addr, 0xffffffff);
|
---|
| 304 | mask = pci_conf_read_32(dev, addr);
|
---|
| 305 |
|
---|
| 306 | // restore the original value
|
---|
| 307 | pci_conf_write_32(dev, addr, val);
|
---|
[3a5909f] | 308 | val = pci_conf_read_32(dev, addr);
|
---|
[d1fc8f0] | 309 |
|
---|
[3a5909f] | 310 | range_size = pci_bar_mask_to_size(mask);
|
---|
[d1fc8f0] | 311 |
|
---|
| 312 | if (w64) {
|
---|
| 313 | range_addr = ((uint64_t)pci_conf_read_32(dev, addr + 4) << 32) | (val & 0xfffffff0);
|
---|
| 314 | } else {
|
---|
| 315 | range_addr = (val & 0xfffffff0);
|
---|
| 316 | }
|
---|
| 317 | if (0 != range_addr) {
|
---|
[3a5909f] | 318 | printf(NAME ": device %s : ", dev->name);
|
---|
| 319 | printf("address = %x", range_addr);
|
---|
| 320 | printf(", size = %x\n", range_size);
|
---|
[d1fc8f0] | 321 | }
|
---|
| 322 |
|
---|
[3a5909f] | 323 | pci_add_range(dev, range_addr, range_size, io);
|
---|
[d1fc8f0] | 324 |
|
---|
| 325 | if (w64) {
|
---|
| 326 | return addr + 8;
|
---|
| 327 | }
|
---|
| 328 | return addr + 4;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
[3a5909f] | 331 | void pci_add_interrupt(device_t *dev, int irq)
|
---|
[d1fc8f0] | 332 | {
|
---|
[3a5909f] | 333 | pci_dev_data_t *dev_data = (pci_dev_data_t *)dev->driver_data;
|
---|
| 334 | hw_resource_list_t *hw_res_list = &dev_data->hw_resources;
|
---|
| 335 | hw_resource_t *hw_resources = hw_res_list->resources;
|
---|
| 336 | size_t count = hw_res_list->count;
|
---|
[d1fc8f0] | 337 |
|
---|
[3a5909f] | 338 | assert(NULL != hw_resources);
|
---|
| 339 | assert(count < PCI_MAX_HW_RES);
|
---|
| 340 |
|
---|
| 341 | hw_resources[count].type = INTERRUPT;
|
---|
| 342 | hw_resources[count].res.interrupt.irq = irq;
|
---|
| 343 |
|
---|
| 344 | hw_res_list->count++;
|
---|
| 345 |
|
---|
| 346 |
|
---|
| 347 | printf(NAME ": device %s uses irq %x.\n", dev->name, irq);
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | void pci_read_interrupt(device_t *dev)
|
---|
| 351 | {
|
---|
| 352 | uint8_t irq = pci_conf_read_8(dev, PCI_BRIDGE_INT_LINE);
|
---|
| 353 | if (0xff != irq) {
|
---|
| 354 | pci_add_interrupt(dev, irq);
|
---|
[d1fc8f0] | 355 | }
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | /** Enumerate (recursively) and register the devices connected to a pci bus.
|
---|
| 359 | *
|
---|
| 360 | * @param parent the host-to-pci bridge device.
|
---|
| 361 | * @param bus_num the bus number.
|
---|
| 362 | */
|
---|
[5e598e0] | 363 | void pci_bus_scan(device_t *parent, int bus_num)
|
---|
| 364 | {
|
---|
| 365 | device_t *dev = create_device();
|
---|
| 366 | pci_dev_data_t *dev_data = create_pci_dev_data();
|
---|
| 367 | dev->driver_data = dev_data;
|
---|
| 368 | dev->parent = parent;
|
---|
| 369 |
|
---|
| 370 | int child_bus = 0;
|
---|
| 371 | int dnum, fnum;
|
---|
| 372 | bool multi;
|
---|
[d1fc8f0] | 373 | uint8_t header_type;
|
---|
[5e598e0] | 374 |
|
---|
| 375 | for (dnum = 0; dnum < 32; dnum++) {
|
---|
| 376 | multi = true;
|
---|
| 377 | for (fnum = 0; multi && fnum < 8; fnum++) {
|
---|
| 378 | init_pci_dev_data(dev_data, bus_num, dnum, fnum);
|
---|
| 379 | dev_data->vendor_id = pci_conf_read_16(dev, PCI_VENDOR_ID);
|
---|
| 380 | dev_data->device_id = pci_conf_read_16(dev, PCI_DEVICE_ID);
|
---|
[3a5909f] | 381 | if (dev_data->vendor_id == 0xffff) { // device is not present, go on scanning the bus
|
---|
[5e598e0] | 382 | if (fnum == 0) {
|
---|
| 383 | break;
|
---|
| 384 | } else {
|
---|
| 385 | continue;
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 | header_type = pci_conf_read_8(dev, PCI_HEADER_TYPE);
|
---|
| 389 | if (fnum == 0) {
|
---|
| 390 | multi = header_type >> 7; // is the device multifunction?
|
---|
| 391 | }
|
---|
| 392 | header_type = header_type & 0x7F; // clear the multifunction bit
|
---|
| 393 |
|
---|
[3a5909f] | 394 | create_pci_dev_name(dev);
|
---|
| 395 |
|
---|
| 396 | pci_alloc_resource_list(dev);
|
---|
[d1fc8f0] | 397 | pci_read_bars(dev);
|
---|
[3a5909f] | 398 | pci_read_interrupt(dev);
|
---|
| 399 |
|
---|
[5159ae9] | 400 | dev->ops = &pci_child_ops;
|
---|
[89ce401a] | 401 |
|
---|
[2480e19] | 402 | printf(NAME ": adding new child device %s.\n", dev->name);
|
---|
[89ce401a] | 403 |
|
---|
| 404 | create_pci_match_ids(dev);
|
---|
| 405 |
|
---|
[df747b9c] | 406 | if (EOK != child_device_register(dev, parent)) {
|
---|
[3a5909f] | 407 | pci_clean_resource_list(dev);
|
---|
[89ce401a] | 408 | clean_match_ids(&dev->match_ids);
|
---|
| 409 | free((char *)dev->name);
|
---|
| 410 | dev->name = NULL;
|
---|
| 411 | continue;
|
---|
| 412 | }
|
---|
[5e598e0] | 413 |
|
---|
[2480e19] | 414 | //printf(NAME ": new device %s was successfully registered by device manager.\n", dev->name);
|
---|
| 415 |
|
---|
[5e598e0] | 416 | if (header_type == PCI_HEADER_TYPE_BRIDGE || header_type == PCI_HEADER_TYPE_CARDBUS ) {
|
---|
| 417 | child_bus = pci_conf_read_8(dev, PCI_BRIDGE_SEC_BUS_NUM);
|
---|
| 418 | printf(NAME ": device is pci-to-pci bridge, secondary bus number = %d.\n", bus_num);
|
---|
| 419 | if(child_bus > bus_num) {
|
---|
| 420 | pci_bus_scan(parent, child_bus);
|
---|
| 421 | }
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | dev = create_device(); // alloc new aux. dev. structure
|
---|
| 425 | dev_data = create_pci_dev_data();
|
---|
| 426 | dev->driver_data = dev_data;
|
---|
| 427 | dev->parent = parent;
|
---|
| 428 | }
|
---|
| 429 | }
|
---|
| 430 |
|
---|
[3a5909f] | 431 | if (dev_data->vendor_id == 0xffff) {
|
---|
[5e598e0] | 432 | delete_device(dev);
|
---|
| 433 | delete_pci_dev_data(dev_data); // free the auxiliary device structure
|
---|
[3a5909f] | 434 | }
|
---|
[5e598e0] | 435 | }
|
---|
[8c06905] | 436 |
|
---|
[df747b9c] | 437 | static int pci_add_device(device_t *dev)
|
---|
[8c06905] | 438 | {
|
---|
| 439 | printf(NAME ": pci_add_device\n");
|
---|
| 440 |
|
---|
[5e598e0] | 441 | pci_bus_data_t *bus_data = create_pci_bus_data();
|
---|
[8c06905] | 442 | if (NULL == bus_data) {
|
---|
| 443 | printf(NAME ": pci_add_device allocation failed.\n");
|
---|
[df747b9c] | 444 | return ENOMEM;
|
---|
[8c06905] | 445 | }
|
---|
| 446 |
|
---|
| 447 | dev->parent_phone = devman_parent_device_connect(dev->handle, IPC_FLAG_BLOCKING);
|
---|
| 448 | if (dev->parent_phone <= 0) {
|
---|
| 449 | printf(NAME ": pci_add_device failed to connect to the parent's driver.\n");
|
---|
[89ce401a] | 450 | delete_pci_bus_data(bus_data);
|
---|
[472020fc] | 451 | return EPARTY; /* FIXME: use another EC */
|
---|
[8c06905] | 452 | }
|
---|
| 453 |
|
---|
| 454 | hw_resource_list_t hw_resources;
|
---|
| 455 |
|
---|
| 456 | if (!get_hw_resources(dev->parent_phone, &hw_resources)) {
|
---|
| 457 | printf(NAME ": pci_add_device failed to get hw resources for the device.\n");
|
---|
[89ce401a] | 458 | delete_pci_bus_data(bus_data);
|
---|
[8c06905] | 459 | ipc_hangup(dev->parent_phone);
|
---|
[472020fc] | 460 | return EPARTY; /* FIXME: use another EC */
|
---|
[3a5909f] | 461 | }
|
---|
[8c06905] | 462 |
|
---|
[3a5909f] | 463 | printf(NAME ": conf_addr = %x.\n", hw_resources.resources[0].res.io_range.address);
|
---|
[8c06905] | 464 |
|
---|
| 465 | assert(hw_resources.count > 0);
|
---|
[3a5909f] | 466 | assert(hw_resources.resources[0].type == IO_RANGE);
|
---|
| 467 | assert(hw_resources.resources[0].res.io_range.size == 8);
|
---|
[8c06905] | 468 |
|
---|
[3a5909f] | 469 | bus_data->conf_io_addr = (uint32_t)hw_resources.resources[0].res.io_range.address;
|
---|
[8c06905] | 470 |
|
---|
[d1fc8f0] | 471 | if (pio_enable((void *)bus_data->conf_io_addr, 8, &bus_data->conf_addr_port)) {
|
---|
[8c06905] | 472 | printf(NAME ": failed to enable configuration ports.\n");
|
---|
[89ce401a] | 473 | delete_pci_bus_data(bus_data);
|
---|
[8c06905] | 474 | ipc_hangup(dev->parent_phone);
|
---|
| 475 | clean_hw_resource_list(&hw_resources);
|
---|
[df747b9c] | 476 | return EADDRNOTAVAIL;
|
---|
[8c06905] | 477 | }
|
---|
| 478 | bus_data->conf_data_port = (char *)bus_data->conf_addr_port + 4;
|
---|
| 479 |
|
---|
| 480 | dev->driver_data = bus_data;
|
---|
| 481 |
|
---|
[89ce401a] | 482 | // enumerate child devices
|
---|
| 483 | printf(NAME ": scanning the bus\n");
|
---|
[5e598e0] | 484 | pci_bus_scan(dev, 0);
|
---|
[8c06905] | 485 |
|
---|
| 486 | clean_hw_resource_list(&hw_resources);
|
---|
| 487 |
|
---|
[df747b9c] | 488 | return EOK;
|
---|
[8c06905] | 489 | }
|
---|
| 490 |
|
---|
[3843ecb] | 491 | static void pciintel_init()
|
---|
| 492 | {
|
---|
[5159ae9] | 493 | pci_child_ops.interfaces[HW_RES_DEV_IFACE] = &pciintel_child_res_iface;
|
---|
[3843ecb] | 494 | }
|
---|
| 495 |
|
---|
[8c06905] | 496 | int main(int argc, char *argv[])
|
---|
| 497 | {
|
---|
[3843ecb] | 498 | printf(NAME ": HelenOS pci bus driver (intel method 1).\n");
|
---|
| 499 | pciintel_init();
|
---|
[8c06905] | 500 | return driver_main(&pci_driver);
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | /**
|
---|
| 504 | * @}
|
---|
[472020fc] | 505 | */
|
---|