[41b96b4] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
---|
| 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 | */
|
---|
[79ae36dd] | 28 |
|
---|
[41b96b4] | 29 | /**
|
---|
| 30 | * @addtogroup drvusbohci
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * PCI related functions needed by the OHCI driver.
|
---|
| 36 | */
|
---|
[79ae36dd] | 37 |
|
---|
[41b96b4] | 38 | #include <errno.h>
|
---|
| 39 | #include <assert.h>
|
---|
| 40 | #include <as.h>
|
---|
| 41 | #include <devman.h>
|
---|
| 42 | #include <ddi.h>
|
---|
| 43 | #include <libarch/ddi.h>
|
---|
| 44 | #include <device/hw_res.h>
|
---|
| 45 |
|
---|
| 46 | #include <usb/debug.h>
|
---|
| 47 | #include <pci_dev_iface.h>
|
---|
| 48 |
|
---|
| 49 | #include "pci.h"
|
---|
| 50 |
|
---|
| 51 | /** Get address of registers and IRQ for given device.
|
---|
| 52 | *
|
---|
| 53 | * @param[in] dev Device asking for the addresses.
|
---|
| 54 | * @param[out] mem_reg_address Base address of the memory range.
|
---|
| 55 | * @param[out] mem_reg_size Size of the memory range.
|
---|
| 56 | * @param[out] irq_no IRQ assigned to the device.
|
---|
| 57 | * @return Error code.
|
---|
| 58 | */
|
---|
| 59 | int pci_get_my_registers(ddf_dev_t *dev,
|
---|
| 60 | uintptr_t *mem_reg_address, size_t *mem_reg_size, int *irq_no)
|
---|
| 61 | {
|
---|
[dc5f2fb] | 62 | assert(dev);
|
---|
| 63 | assert(mem_reg_address);
|
---|
| 64 | assert(mem_reg_size);
|
---|
| 65 | assert(irq_no);
|
---|
[41b96b4] | 66 |
|
---|
[79ae36dd] | 67 | async_sess_t *parent_sess =
|
---|
| 68 | devman_parent_device_connect(EXCHANGE_SERIALIZE, dev->handle,
|
---|
[41b96b4] | 69 | IPC_FLAG_BLOCKING);
|
---|
[79ae36dd] | 70 | if (!parent_sess)
|
---|
| 71 | return ENOMEM;
|
---|
[45a9cf4] | 72 |
|
---|
[41b96b4] | 73 | hw_resource_list_t hw_resources;
|
---|
[79ae36dd] | 74 | int rc = hw_res_get_resource_list(parent_sess, &hw_resources);
|
---|
[45a9cf4] | 75 | async_hangup(parent_sess);
|
---|
[41b96b4] | 76 | if (rc != EOK) {
|
---|
| 77 | return rc;
|
---|
| 78 | }
|
---|
[45a9cf4] | 79 |
|
---|
[41b96b4] | 80 | uintptr_t mem_address = 0;
|
---|
| 81 | size_t mem_size = 0;
|
---|
| 82 | bool mem_found = false;
|
---|
[45a9cf4] | 83 |
|
---|
[41b96b4] | 84 | int irq = 0;
|
---|
| 85 | bool irq_found = false;
|
---|
[45a9cf4] | 86 |
|
---|
[9b8958b] | 87 | for (size_t i = 0; i < hw_resources.count; i++) {
|
---|
[41b96b4] | 88 | hw_resource_t *res = &hw_resources.resources[i];
|
---|
[79ae36dd] | 89 | switch (res->type) {
|
---|
[41b96b4] | 90 | case INTERRUPT:
|
---|
| 91 | irq = res->res.interrupt.irq;
|
---|
| 92 | irq_found = true;
|
---|
| 93 | usb_log_debug2("Found interrupt: %d.\n", irq);
|
---|
| 94 | break;
|
---|
| 95 | case MEM_RANGE:
|
---|
| 96 | if (res->res.mem_range.address != 0
|
---|
| 97 | && res->res.mem_range.size != 0 ) {
|
---|
| 98 | mem_address = res->res.mem_range.address;
|
---|
| 99 | mem_size = res->res.mem_range.size;
|
---|
[4125b7d] | 100 | usb_log_debug2("Found mem: %p %zu.\n",
|
---|
| 101 | (void *) mem_address, mem_size);
|
---|
[41b96b4] | 102 | mem_found = true;
|
---|
[79ae36dd] | 103 | }
|
---|
[41b96b4] | 104 | default:
|
---|
| 105 | break;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
[45a9cf4] | 108 | free(hw_resources.resources);
|
---|
| 109 |
|
---|
[41b96b4] | 110 | if (mem_found && irq_found) {
|
---|
| 111 | *mem_reg_address = mem_address;
|
---|
| 112 | *mem_reg_size = mem_size;
|
---|
| 113 | *irq_no = irq;
|
---|
[45a9cf4] | 114 | return EOK;
|
---|
| 115 | }
|
---|
| 116 | return ENOENT;
|
---|
[41b96b4] | 117 | }
|
---|
[79ae36dd] | 118 |
|
---|
| 119 | /** Call the PCI driver with a request to enable interrupts
|
---|
[41b96b4] | 120 | *
|
---|
| 121 | * @param[in] device Device asking for interrupts
|
---|
| 122 | * @return Error code.
|
---|
| 123 | */
|
---|
| 124 | int pci_enable_interrupts(ddf_dev_t *device)
|
---|
| 125 | {
|
---|
[79ae36dd] | 126 | async_sess_t *parent_sess =
|
---|
| 127 | devman_parent_device_connect(EXCHANGE_SERIALIZE, device->handle,
|
---|
| 128 | IPC_FLAG_BLOCKING);
|
---|
| 129 | if (!parent_sess)
|
---|
| 130 | return ENOMEM;
|
---|
| 131 |
|
---|
| 132 | bool enabled = hw_res_enable_interrupt(parent_sess);
|
---|
| 133 | async_hangup(parent_sess);
|
---|
| 134 |
|
---|
[41b96b4] | 135 | return enabled ? EOK : EIO;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * @}
|
---|
| 140 | */
|
---|