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