[ea991e84] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 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 | /**
|
---|
[17ceb72] | 29 | * @addtogroup drvusbuhcihc
|
---|
[ea991e84] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * PCI related functions needed by the UHCI driver.
|
---|
| 35 | */
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <assert.h>
|
---|
| 38 | #include <devman.h>
|
---|
| 39 | #include <device/hw_res.h>
|
---|
| 40 |
|
---|
[2d33eb5] | 41 | #include <usb/debug.h>
|
---|
[ab414a6] | 42 | #include <pci_dev_iface.h>
|
---|
[2d33eb5] | 43 |
|
---|
[c56dbe0] | 44 | #include "pci.h"
|
---|
| 45 |
|
---|
[ea993d18] | 46 | /** Get I/O address of registers and IRQ for given device.
|
---|
[ea991e84] | 47 | *
|
---|
| 48 | * @param[in] dev Device asking for the addresses.
|
---|
| 49 | * @param[out] io_reg_address Base address of the I/O range.
|
---|
| 50 | * @param[out] io_reg_size Size of the I/O range.
|
---|
| 51 | * @param[out] irq_no IRQ assigned to the device.
|
---|
| 52 | * @return Error code.
|
---|
| 53 | */
|
---|
[eb1a2f4] | 54 | int pci_get_my_registers(ddf_dev_t *dev,
|
---|
[ea993d18] | 55 | uintptr_t *io_reg_address, size_t *io_reg_size, int *irq_no)
|
---|
[ea991e84] | 56 | {
|
---|
| 57 | assert(dev != NULL);
|
---|
| 58 |
|
---|
[ea993d18] | 59 | int parent_phone =
|
---|
| 60 | devman_parent_device_connect(dev->handle, IPC_FLAG_BLOCKING);
|
---|
[ea991e84] | 61 | if (parent_phone < 0) {
|
---|
| 62 | return parent_phone;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | hw_resource_list_t hw_resources;
|
---|
[ea993d18] | 66 | int rc = hw_res_get_resource_list(parent_phone, &hw_resources);
|
---|
[ea991e84] | 67 | if (rc != EOK) {
|
---|
| 68 | goto leave;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | uintptr_t io_address = 0;
|
---|
| 72 | size_t io_size = 0;
|
---|
| 73 | bool io_found = false;
|
---|
| 74 |
|
---|
| 75 | int irq = 0;
|
---|
| 76 | bool irq_found = false;
|
---|
| 77 |
|
---|
| 78 | size_t i;
|
---|
| 79 | for (i = 0; i < hw_resources.count; i++) {
|
---|
| 80 | hw_resource_t *res = &hw_resources.resources[i];
|
---|
[4b4e163] | 81 | switch (res->type)
|
---|
| 82 | {
|
---|
| 83 | case INTERRUPT:
|
---|
| 84 | irq = res->res.interrupt.irq;
|
---|
| 85 | irq_found = true;
|
---|
| 86 | usb_log_debug2("Found interrupt: %d.\n", irq);
|
---|
| 87 | break;
|
---|
| 88 |
|
---|
| 89 | case IO_RANGE:
|
---|
| 90 | io_address = res->res.io_range.address;
|
---|
| 91 | io_size = res->res.io_range.size;
|
---|
[4125b7d] | 92 | usb_log_debug2("Found io: %" PRIx64" %zu.\n",
|
---|
[4b4e163] | 93 | res->res.io_range.address, res->res.io_range.size);
|
---|
| 94 | io_found = true;
|
---|
[ea993d18] | 95 | break;
|
---|
[4b4e163] | 96 |
|
---|
| 97 | default:
|
---|
| 98 | break;
|
---|
[ea991e84] | 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[4b4e163] | 102 | if (!io_found || !irq_found) {
|
---|
[ea991e84] | 103 | rc = ENOENT;
|
---|
| 104 | goto leave;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[2d33eb5] | 107 | *io_reg_address = io_address;
|
---|
| 108 | *io_reg_size = io_size;
|
---|
| 109 | *irq_no = irq;
|
---|
[ea991e84] | 110 |
|
---|
| 111 | rc = EOK;
|
---|
| 112 | leave:
|
---|
[17aca1c] | 113 | async_hangup(parent_phone);
|
---|
[ea991e84] | 114 | return rc;
|
---|
| 115 | }
|
---|
[fb78ae72] | 116 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 117 | /** Call the PCI driver with a request to enable interrupts
|
---|
[a7e2f0d] | 118 | *
|
---|
| 119 | * @param[in] device Device asking for interrupts
|
---|
| 120 | * @return Error code.
|
---|
| 121 | */
|
---|
[eb1a2f4] | 122 | int pci_enable_interrupts(ddf_dev_t *device)
|
---|
[fb78ae72] | 123 | {
|
---|
| 124 | int parent_phone = devman_parent_device_connect(device->handle,
|
---|
| 125 | IPC_FLAG_BLOCKING);
|
---|
| 126 | bool enabled = hw_res_enable_interrupt(parent_phone);
|
---|
[86c2ccd] | 127 | async_hangup(parent_phone);
|
---|
[fb78ae72] | 128 | return enabled ? EOK : EIO;
|
---|
| 129 | }
|
---|
[ab414a6] | 130 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 131 | /** Call the PCI driver with a request to clear legacy support register
|
---|
[a7e2f0d] | 132 | *
|
---|
| 133 | * @param[in] device Device asking to disable interrupts
|
---|
| 134 | * @return Error code.
|
---|
| 135 | */
|
---|
[ab414a6] | 136 | int pci_disable_legacy(ddf_dev_t *device)
|
---|
| 137 | {
|
---|
| 138 | assert(device);
|
---|
[4b4e163] | 139 | int parent_phone =
|
---|
| 140 | devman_parent_device_connect(device->handle, IPC_FLAG_BLOCKING);
|
---|
[ab414a6] | 141 | if (parent_phone < 0) {
|
---|
| 142 | return parent_phone;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[ea993d18] | 145 | /* See UHCI design guide for these values p.45,
|
---|
[b375bb8] | 146 | * write all WC bits in USB legacy register */
|
---|
| 147 | sysarg_t address = 0xc0;
|
---|
[af81980] | 148 | sysarg_t value = 0xaf00;
|
---|
[ab414a6] | 149 |
|
---|
[4b4e163] | 150 | int rc = async_req_3_0(parent_phone, DEV_IFACE_ID(PCI_DEV_IFACE),
|
---|
[ab414a6] | 151 | IPC_M_CONFIG_SPACE_WRITE_16, address, value);
|
---|
| 152 | async_hangup(parent_phone);
|
---|
| 153 |
|
---|
[4b4e163] | 154 | return rc;
|
---|
[ab414a6] | 155 | }
|
---|
| 156 | /*----------------------------------------------------------------------------*/
|
---|
[ea991e84] | 157 | /**
|
---|
| 158 | * @}
|
---|
| 159 | */
|
---|
| 160 |
|
---|
[c56dbe0] | 161 | /**
|
---|
| 162 | * @}
|
---|
| 163 | */
|
---|