Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/pciintel/pci.c

    r91579d5 rebcb05a  
    4848
    4949#include <ddf/driver.h>
     50#include <ddf/log.h>
    5051#include <devman.h>
    5152#include <ipc/devman.h>
    5253#include <ipc/dev_iface.h>
    53 #include <ipc/irc.h>
    54 #include <ipc/ns.h>
    55 #include <ipc/services.h>
    56 #include <sysinfo.h>
    5754#include <ops/hw_res.h>
    5855#include <device/hw_res.h>
    5956#include <ddi.h>
    6057#include <libarch/ddi.h>
    61 #include <pci_dev_iface.h>
    6258
    6359#include "pci.h"
     
    8884static bool pciintel_enable_interrupt(ddf_fun_t *fnode)
    8985{
    90         /* This is an old ugly way, copied from ne2000 driver */
    91         assert(fnode);
    92         pci_fun_t *dev_data = (pci_fun_t *) fnode->driver_data;
    93 
    94         sysarg_t apic;
    95         sysarg_t i8259;
    96 
    97         int irc_phone = -1;
    98         int irc_service = -1;
    99 
    100         if ((sysinfo_get_value("apic", &apic) == EOK) && (apic)) {
    101                 irc_service = SERVICE_APIC;
    102         } else if ((sysinfo_get_value("i8259", &i8259) == EOK) && (i8259)) {
    103                 irc_service = SERVICE_I8259;
    104         }
    105 
    106         if (irc_service == -1) {
    107                 return false;
    108         }
    109 
    110         irc_phone = service_connect_blocking(irc_service, 0, 0);
    111         if (irc_phone < 0) {
    112                 return false;
    113         }
    114 
    115         size_t i;
    116         for (i = 0; i < dev_data->hw_resources.count; i++) {
    117                 if (dev_data->hw_resources.resources[i].type == INTERRUPT) {
    118                         int irq = dev_data->hw_resources.resources[i].res.interrupt.irq;
    119                         int rc = async_req_1_0(irc_phone, IRC_ENABLE_INTERRUPT, irq);
    120                         if (rc != EOK) {
    121                                 async_hangup(irc_phone);
    122                                 return false;
    123                         }
    124                 }
    125         }
    126 
    127         async_hangup(irc_phone);
    128         return true;
    129 }
    130 
    131 static int pci_config_space_write_32(
    132     ddf_fun_t *fun, uint32_t address, uint32_t data)
    133 {
    134         if (address > 252)
    135                 return EINVAL;
    136         pci_conf_write_32(PCI_FUN(fun), address, data);
    137         return EOK;
    138 }
    139 
    140 static int pci_config_space_write_16(
    141     ddf_fun_t *fun, uint32_t address, uint16_t data)
    142 {
    143         if (address > 254)
    144                 return EINVAL;
    145         pci_conf_write_16(PCI_FUN(fun), address, data);
    146         return EOK;
    147 }
    148 
    149 static int pci_config_space_write_8(
    150     ddf_fun_t *fun, uint32_t address, uint8_t data)
    151 {
    152         if (address > 255)
    153                 return EINVAL;
    154         pci_conf_write_8(PCI_FUN(fun), address, data);
    155         return EOK;
    156 }
    157 
    158 static int pci_config_space_read_32(
    159     ddf_fun_t *fun, uint32_t address, uint32_t *data)
    160 {
    161         if (address > 252)
    162                 return EINVAL;
    163         *data = pci_conf_read_32(PCI_FUN(fun), address);
    164         return EOK;
    165 }
    166 
    167 static int pci_config_space_read_16(
    168     ddf_fun_t *fun, uint32_t address, uint16_t *data)
    169 {
    170         if (address > 254)
    171                 return EINVAL;
    172         *data = pci_conf_read_16(PCI_FUN(fun), address);
    173         return EOK;
    174 }
    175 
    176 static int pci_config_space_read_8(
    177     ddf_fun_t *fun, uint32_t address, uint8_t *data)
    178 {
    179         if (address > 255)
    180                 return EINVAL;
    181         *data = pci_conf_read_8(PCI_FUN(fun), address);
    182         return EOK;
     86        /* TODO */
     87       
     88        return false;
    18389}
    18490
     
    18894};
    18995
    190 static pci_dev_iface_t pci_dev_ops = {
    191         .config_space_read_8 = &pci_config_space_read_8,
    192         .config_space_read_16 = &pci_config_space_read_16,
    193         .config_space_read_32 = &pci_config_space_read_32,
    194         .config_space_write_8 = &pci_config_space_write_8,
    195         .config_space_write_16 = &pci_config_space_write_16,
    196         .config_space_write_32 = &pci_config_space_write_32
    197 };
    198 
    199 static ddf_dev_ops_t pci_fun_ops = {
    200         .interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops,
    201         .interfaces[PCI_DEV_IFACE] = &pci_dev_ops
    202 };
     96static ddf_dev_ops_t pci_fun_ops;
    20397
    20498static int pci_add_device(ddf_dev_t *);
     
    332226
    333227        if (match_id_str == NULL) {
    334                 printf(NAME ": out of memory creating match ID.\n");
     228                ddf_msg(LVL_ERROR, "Out of memory creating match ID.");
    335229                return;
    336230        }
     
    338232        rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 90);
    339233        if (rc != EOK) {
    340                 printf(NAME ": error adding match ID: %s\n",
     234                ddf_msg(LVL_ERROR, "Failed adding match ID: %s",
    341235                    str_error(rc));
    342236        }
     
    394288        /* Get the value of the BAR. */
    395289        val = pci_conf_read_32(fun, addr);
    396 
    397 #define IO_MASK  (~0x3)
    398 #define MEM_MASK (~0xf)
    399290       
    400291        io = (bool) (val & 1);
    401292        if (io) {
    402293                addrw64 = false;
    403                 mask = IO_MASK;
    404294        } else {
    405                 mask = MEM_MASK;
    406295                switch ((val >> 1) & 3) {
    407296                case 0:
     
    419308        /* Get the address mask. */
    420309        pci_conf_write_32(fun, addr, 0xffffffff);
    421         mask &= pci_conf_read_32(fun, addr);
     310        mask = pci_conf_read_32(fun, addr);
    422311       
    423312        /* Restore the original value. */
     
    435324       
    436325        if (range_addr != 0) {
    437                 printf(NAME ": function %s : ", fun->fnode->name);
    438                 printf("address = %" PRIx64, range_addr);
    439                 printf(", size = %x\n", (unsigned int) range_size);
     326                ddf_msg(LVL_DEBUG, "Function %s : address = %" PRIx64
     327                    ", size = %x", fun->fnode->name, range_addr,
     328                    (unsigned int) range_size);
    440329        }
    441330       
     
    462351        hw_res_list->count++;
    463352       
    464         printf(NAME ": function %s uses irq %x.\n", fun->fnode->name, irq);
     353        ddf_msg(LVL_NOTE, "Function %s uses irq %x.", fun->fnode->name, irq);
    465354}
    466355
     
    518407                        char *fun_name = pci_fun_create_name(fun);
    519408                        if (fun_name == NULL) {
    520                                 printf(NAME ": out of memory.\n");
     409                                ddf_msg(LVL_ERROR, "Out of memory.");
    521410                                return;
    522411                        }
     
    524413                        fnode = ddf_fun_create(bus->dnode, fun_inner, fun_name);
    525414                        if (fnode == NULL) {
    526                                 printf(NAME ": error creating function.\n");
     415                                ddf_msg(LVL_ERROR, "Failed creating function.");
    527416                                return;
    528417                        }
     
    538427                        fnode->driver_data = fun;
    539428                       
    540                         printf(NAME ": adding new function %s.\n",
     429                        ddf_msg(LVL_DEBUG, "Adding new function %s.",
    541430                            fnode->name);
    542431                       
     
    555444                                child_bus = pci_conf_read_8(fun,
    556445                                    PCI_BRIDGE_SEC_BUS_NUM);
    557                                 printf(NAME ": device is pci-to-pci bridge, "
    558                                     "secondary bus number = %d.\n", bus_num);
     446                                ddf_msg(LVL_DEBUG, "Device is pci-to-pci "
     447                                    "bridge, secondary bus number = %d.",
     448                                    bus_num);
    559449                                if (child_bus > bus_num)
    560450                                        pci_bus_scan(bus, child_bus);
     
    578468        int rc;
    579469       
    580         printf(NAME ": pci_add_device\n");
     470        ddf_msg(LVL_DEBUG, "pci_add_device");
    581471        dnode->parent_phone = -1;
    582472       
    583473        bus = pci_bus_new();
    584474        if (bus == NULL) {
    585                 printf(NAME ": pci_add_device allocation failed.\n");
     475                ddf_msg(LVL_ERROR, "pci_add_device allocation failed.");
    586476                rc = ENOMEM;
    587477                goto fail;
     
    593483            IPC_FLAG_BLOCKING);
    594484        if (dnode->parent_phone < 0) {
    595                 printf(NAME ": pci_add_device failed to connect to the "
    596                     "parent's driver.\n");
     485                ddf_msg(LVL_ERROR, "pci_add_device failed to connect to the "
     486                    "parent's driver.");
    597487                rc = dnode->parent_phone;
    598488                goto fail;
     
    603493        rc = hw_res_get_resource_list(dnode->parent_phone, &hw_resources);
    604494        if (rc != EOK) {
    605                 printf(NAME ": pci_add_device failed to get hw resources for "
    606                     "the device.\n");
     495                ddf_msg(LVL_ERROR, "pci_add_device failed to get hw resources "
     496                    "for the device.");
    607497                goto fail;
    608498        }
    609499        got_res = true;
    610500       
    611         printf(NAME ": conf_addr = %" PRIx64 ".\n",
     501        ddf_msg(LVL_DEBUG, "conf_addr = %" PRIx64 ".",
    612502            hw_resources.resources[0].res.io_range.address);
    613503       
     
    621511        if (pio_enable((void *)(uintptr_t)bus->conf_io_addr, 8,
    622512            &bus->conf_addr_port)) {
    623                 printf(NAME ": failed to enable configuration ports.\n");
     513                ddf_msg(LVL_ERROR, "Failed to enable configuration ports.");
    624514                rc = EADDRNOTAVAIL;
    625515                goto fail;
     
    628518       
    629519        /* Make the bus device more visible. It has no use yet. */
    630         printf(NAME ": adding a 'ctl' function\n");
     520        ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
    631521       
    632522        ctl = ddf_fun_create(bus->dnode, fun_exposed, "ctl");
    633523        if (ctl == NULL) {
    634                 printf(NAME ": error creating control function.\n");
     524                ddf_msg(LVL_ERROR, "Failed creating control function.");
    635525                rc = ENOMEM;
    636526                goto fail;
     
    639529        rc = ddf_fun_bind(ctl);
    640530        if (rc != EOK) {
    641                 printf(NAME ": error binding control function.\n");
     531                ddf_msg(LVL_ERROR, "Failed binding control function.");
    642532                goto fail;
    643533        }
    644534       
    645535        /* Enumerate functions. */
    646         printf(NAME ": scanning the bus\n");
     536        ddf_msg(LVL_DEBUG, "Scanning the bus");
    647537        pci_bus_scan(bus, 0);
    648538       
     
    666556static void pciintel_init(void)
    667557{
     558        ddf_log_init(NAME, LVL_ERROR);
    668559        pci_fun_ops.interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops;
    669         pci_fun_ops.interfaces[PCI_DEV_IFACE] = &pci_dev_ops;
    670560}
    671561
     
    739629size_t pci_bar_mask_to_size(uint32_t mask)
    740630{
    741         size_t size = mask & ~(mask - 1);
    742         return size;
     631        return ((mask & 0xfffffff0) ^ 0xffffffff) + 1;
    743632}
    744633
    745634int main(int argc, char *argv[])
    746635{
    747         printf(NAME ": HelenOS pci bus driver (intel method 1).\n");
     636        printf(NAME ": HelenOS PCI bus driver (Intel method 1).\n");
    748637        pciintel_init();
    749638        return ddf_driver_main(&pci_driver);
Note: See TracChangeset for help on using the changeset viewer.