Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 4 | * Copyright (C) 2008-2010 coresystems GmbH |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. 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 | * 3. 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 AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 30 | //#define USB_DEBUG |
| 31 | |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 32 | #include <arch/virtual.h> |
Jordan Crouse | 29061a5 | 2008-09-11 17:29:00 +0000 | [diff] [blame] | 33 | #include <usb/usb.h> |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 34 | #include "uhci.h" |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 35 | #include "uhci_private.h" |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 36 | |
| 37 | static void uhci_start (hci_t *controller); |
| 38 | static void uhci_stop (hci_t *controller); |
| 39 | static void uhci_reset (hci_t *controller); |
| 40 | static void uhci_shutdown (hci_t *controller); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 41 | static int uhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize); |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 42 | static int uhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq, |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 43 | int dalen, u8 *data); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 44 | static void* uhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming); |
| 45 | static void uhci_destroy_intr_queue (endpoint_t *ep, void *queue); |
| 46 | static u8* uhci_poll_intr_queue (void *queue); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 47 | |
| 48 | #if 0 |
| 49 | /* dump uhci */ |
| 50 | static void |
| 51 | uhci_dump (hci_t *controller) |
| 52 | { |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 53 | usb_debug ("dump:\nUSBCMD: %x\n", uhci_reg_read16 (controller, USBCMD)); |
| 54 | usb_debug ("USBSTS: %x\n", uhci_reg_read16 (controller, USBSTS)); |
| 55 | usb_debug ("USBINTR: %x\n", uhci_reg_read16 (controller, USBINTR)); |
| 56 | usb_debug ("FRNUM: %x\n", uhci_reg_read16 (controller, FRNUM)); |
| 57 | usb_debug ("FLBASEADD: %x\n", uhci_reg_read32 (controller, FLBASEADD)); |
| 58 | usb_debug ("SOFMOD: %x\n", uhci_reg_read8 (controller, SOFMOD)); |
| 59 | usb_debug ("PORTSC1: %x\n", uhci_reg_read16 (controller, PORTSC1)); |
| 60 | usb_debug ("PORTSC2: %x\n", uhci_reg_read16 (controller, PORTSC2)); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 61 | } |
| 62 | #endif |
| 63 | |
| 64 | static void |
| 65 | td_dump (td_t *td) |
| 66 | { |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 67 | char td_value[3]; |
Patrick Georgi | 7f96583 | 2011-04-21 18:57:16 +0200 | [diff] [blame] | 68 | const char *td_type; |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 69 | switch (td->token & TD_PID_MASK) { |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 70 | case UHCI_SETUP: |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 71 | td_type="SETUP"; |
| 72 | break; |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 73 | case UHCI_IN: |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 74 | td_type="IN"; |
| 75 | break; |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 76 | case UHCI_OUT: |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 77 | td_type="OUT"; |
| 78 | break; |
| 79 | default: |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 80 | sprintf(td_value, "%x", td->token & TD_PID_MASK); |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 81 | td_type=td_value; |
| 82 | } |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 83 | usb_debug ("%s packet (at %lx) to %x.%x failed\n", td_type, |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 84 | virt_to_phys (td), (td->token & TD_DEVADDR_MASK) >> TD_DEVADDR_SHIFT, |
| 85 | (td->token & TD_EP_MASK) >> TD_EP_SHIFT); |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 86 | usb_debug ("td (counter at %x) returns: ", td->ctrlsts >> TD_COUNTER_SHIFT); |
| 87 | usb_debug (" bitstuff err: %x, ", !!(td->ctrlsts & TD_STATUS_BITSTUFF_ERR)); |
| 88 | usb_debug (" CRC err: %x, ", !!(td->ctrlsts & TD_STATUS_CRC_ERR)); |
| 89 | usb_debug (" NAK rcvd: %x, ", !!(td->ctrlsts & TD_STATUS_NAK_RCVD)); |
| 90 | usb_debug (" Babble: %x, ", !!(td->ctrlsts & TD_STATUS_BABBLE)); |
| 91 | usb_debug (" Data Buffer err: %x, ", !!(td->ctrlsts & TD_STATUS_DATABUF_ERR)); |
| 92 | usb_debug (" Stalled: %x, ", !!(td->ctrlsts & TD_STATUS_STALLED)); |
| 93 | usb_debug (" Active: %x\n", !!(td->ctrlsts & TD_STATUS_ACTIVE)); |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 94 | if (td->ctrlsts & TD_STATUS_BABBLE) |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 95 | usb_debug (" Babble because of %s\n", |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 96 | (td->ctrlsts & TD_STATUS_BITSTUFF_ERR) ? "host" : "device"); |
| 97 | if (td->ctrlsts & TD_STATUS_ACTIVE) |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 98 | usb_debug (" still active - timeout?\n"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static void |
| 102 | uhci_reset (hci_t *controller) |
| 103 | { |
| 104 | /* reset */ |
Nico Huber | bb1c42b | 2012-05-21 14:23:03 +0200 | [diff] [blame] | 105 | uhci_reg_write16 (controller, USBCMD, 4); /* Global Reset */ |
| 106 | mdelay (50); /* uhci spec 2.1.1: at least 10ms */ |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 107 | uhci_reg_write16 (controller, USBCMD, 0); |
| 108 | mdelay (10); |
Nico Huber | bb1c42b | 2012-05-21 14:23:03 +0200 | [diff] [blame] | 109 | uhci_reg_write16 (controller, USBCMD, 2); /* Host Controller Reset */ |
| 110 | /* wait for controller to finish reset */ |
| 111 | /* TOTEST: how long to wait? 100ms for now */ |
| 112 | int timeout = 200; /* time out after 200 * 500us == 100ms */ |
| 113 | while (((uhci_reg_read16 (controller, USBCMD) & 2) != 0) && timeout--) |
| 114 | udelay (500); |
| 115 | if (timeout < 0) |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 116 | usb_debug ("Warning: uhci: host controller reset timed out.\n"); |
Nico Huber | 6e711c6 | 2012-11-12 16:20:32 +0100 | [diff] [blame] | 117 | } |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 118 | |
Nico Huber | 6e711c6 | 2012-11-12 16:20:32 +0100 | [diff] [blame] | 119 | static void |
| 120 | uhci_reinit (hci_t *controller) |
| 121 | { |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 122 | uhci_reg_write32 (controller, FLBASEADD, |
| 123 | (u32) virt_to_phys (UHCI_INST (controller)-> |
| 124 | framelistptr)); |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 125 | //usb_debug ("framelist at %p\n",UHCI_INST(controller)->framelistptr); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 126 | |
| 127 | /* disable irqs */ |
| 128 | uhci_reg_write16 (controller, USBINTR, 0); |
| 129 | |
| 130 | /* reset framelist index */ |
| 131 | uhci_reg_write16 (controller, FRNUM, 0); |
| 132 | |
Patrick Georgi | f42fdab | 2011-11-18 14:44:16 +0100 | [diff] [blame] | 133 | uhci_reg_write16(controller, USBCMD, |
| 134 | uhci_reg_read16(controller, USBCMD) | 0xc0); // max packets, configure flag |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 135 | |
| 136 | uhci_start (controller); |
| 137 | } |
| 138 | |
| 139 | hci_t * |
| 140 | uhci_init (pcidev_t addr) |
| 141 | { |
| 142 | int i; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 143 | u16 reg16; |
| 144 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 145 | hci_t *controller = new_controller (); |
| 146 | |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 147 | if (!controller) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 148 | fatal("Could not create USB controller instance.\n"); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 149 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 150 | controller->instance = malloc (sizeof (uhci_t)); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 151 | if(!controller->instance) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 152 | fatal("Not enough memory creating USB controller instance.\n"); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 153 | |
Anton Kochkov | 1c36ead | 2012-06-28 08:30:15 +0400 | [diff] [blame] | 154 | controller->type = UHCI; |
| 155 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 156 | controller->start = uhci_start; |
| 157 | controller->stop = uhci_stop; |
| 158 | controller->reset = uhci_reset; |
Nico Huber | 6e711c6 | 2012-11-12 16:20:32 +0100 | [diff] [blame] | 159 | controller->init = uhci_reinit; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 160 | controller->shutdown = uhci_shutdown; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 161 | controller->bulk = uhci_bulk; |
| 162 | controller->control = uhci_control; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 163 | controller->create_intr_queue = uhci_create_intr_queue; |
| 164 | controller->destroy_intr_queue = uhci_destroy_intr_queue; |
| 165 | controller->poll_intr_queue = uhci_poll_intr_queue; |
Stefan Reinauer | 219cece | 2009-07-18 15:17:40 +0000 | [diff] [blame] | 166 | for (i = 0; i < 128; i++) { |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 167 | controller->devices[i] = 0; |
| 168 | } |
| 169 | init_device_entry (controller, 0); |
| 170 | UHCI_INST (controller)->roothub = controller->devices[0]; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 171 | |
| 172 | controller->bus_address = addr; |
| 173 | controller->reg_base = pci_read_config32 (controller->bus_address, 0x20) & ~1; /* ~1 clears the register type indicator that is set to 1 for IO space */ |
| 174 | |
| 175 | /* kill legacy support handler */ |
| 176 | uhci_stop (controller); |
| 177 | mdelay (1); |
| 178 | uhci_reg_write16 (controller, USBSTS, 0x3f); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 179 | reg16 = pci_read_config16(controller->bus_address, 0xc0); |
| 180 | reg16 &= 0xdf80; |
| 181 | pci_write_config16 (controller->bus_address, 0xc0, reg16); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 182 | |
Anton Kochkov | f6c8080 | 2012-09-20 10:24:01 +0200 | [diff] [blame] | 183 | UHCI_INST (controller)->framelistptr = memalign (0x1000, 1024 * sizeof (flistp_t)); /* 4kb aligned to 4kb */ |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 184 | if (! UHCI_INST (controller)->framelistptr) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 185 | fatal("Not enough memory for USB frame list pointer.\n"); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 186 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 187 | memset (UHCI_INST (controller)->framelistptr, 0, |
| 188 | 1024 * sizeof (flistp_t)); |
| 189 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 190 | /* According to the *BSD UHCI code, this one is needed on some |
| 191 | PIIX chips, because otherwise they misbehave. It must be |
| 192 | added to the last chain. |
| 193 | |
| 194 | FIXME: this leaks, if the driver should ever be reinited |
| 195 | for some reason. Not a problem now. |
| 196 | */ |
| 197 | td_t *antiberserk = memalign(16, sizeof(td_t)); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 198 | if (!antiberserk) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 199 | fatal("Not enough memory for chipset workaround.\n"); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 200 | memset(antiberserk, 0, sizeof(td_t)); |
| 201 | |
| 202 | UHCI_INST (controller)->qh_prei = memalign (16, sizeof (qh_t)); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 203 | UHCI_INST (controller)->qh_intr = memalign (16, sizeof (qh_t)); |
| 204 | UHCI_INST (controller)->qh_data = memalign (16, sizeof (qh_t)); |
| 205 | UHCI_INST (controller)->qh_last = memalign (16, sizeof (qh_t)); |
| 206 | |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 207 | if (! UHCI_INST (controller)->qh_prei || |
| 208 | ! UHCI_INST (controller)->qh_intr || |
| 209 | ! UHCI_INST (controller)->qh_data || |
| 210 | ! UHCI_INST (controller)->qh_last) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 211 | fatal("Not enough memory for USB controller queues.\n"); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 212 | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 213 | UHCI_INST (controller)->qh_prei->headlinkptr = |
| 214 | virt_to_phys (UHCI_INST (controller)->qh_intr) | FLISTP_QH; |
| 215 | UHCI_INST (controller)->qh_prei->elementlinkptr = 0 | FLISTP_TERMINATE; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 216 | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 217 | UHCI_INST (controller)->qh_intr->headlinkptr = |
| 218 | virt_to_phys (UHCI_INST (controller)->qh_data) | FLISTP_QH; |
| 219 | UHCI_INST (controller)->qh_intr->elementlinkptr = 0 | FLISTP_TERMINATE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 220 | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 221 | UHCI_INST (controller)->qh_data->headlinkptr = |
| 222 | virt_to_phys (UHCI_INST (controller)->qh_last) | FLISTP_QH; |
| 223 | UHCI_INST (controller)->qh_data->elementlinkptr = 0 | FLISTP_TERMINATE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 224 | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 225 | UHCI_INST (controller)->qh_last->headlinkptr = virt_to_phys (UHCI_INST (controller)->qh_data) | FLISTP_TERMINATE; |
| 226 | UHCI_INST (controller)->qh_last->elementlinkptr = virt_to_phys (antiberserk) | FLISTP_TERMINATE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 227 | |
| 228 | for (i = 0; i < 1024; i++) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 229 | UHCI_INST (controller)->framelistptr[i] = |
| 230 | virt_to_phys (UHCI_INST (controller)->qh_prei) | FLISTP_QH; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 231 | } |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 232 | controller->devices[0]->controller = controller; |
| 233 | controller->devices[0]->init = uhci_rh_init; |
| 234 | controller->devices[0]->init (controller->devices[0]); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 235 | uhci_reset (controller); |
Nico Huber | 6e711c6 | 2012-11-12 16:20:32 +0100 | [diff] [blame] | 236 | uhci_reinit (controller); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 237 | return controller; |
| 238 | } |
| 239 | |
| 240 | static void |
| 241 | uhci_shutdown (hci_t *controller) |
| 242 | { |
| 243 | if (controller == 0) |
| 244 | return; |
| 245 | detach_controller (controller); |
| 246 | UHCI_INST (controller)->roothub->destroy (UHCI_INST (controller)-> |
| 247 | roothub); |
Patrick Georgi | f42fdab | 2011-11-18 14:44:16 +0100 | [diff] [blame] | 248 | uhci_reg_write16(controller, USBCMD, |
| 249 | uhci_reg_read16(controller, USBCMD) & 0); // stop work |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 250 | free (UHCI_INST (controller)->framelistptr); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 251 | free (UHCI_INST (controller)->qh_prei); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 252 | free (UHCI_INST (controller)->qh_intr); |
| 253 | free (UHCI_INST (controller)->qh_data); |
| 254 | free (UHCI_INST (controller)->qh_last); |
| 255 | free (UHCI_INST (controller)); |
| 256 | free (controller); |
| 257 | } |
| 258 | |
| 259 | static void |
| 260 | uhci_start (hci_t *controller) |
| 261 | { |
Patrick Georgi | f42fdab | 2011-11-18 14:44:16 +0100 | [diff] [blame] | 262 | uhci_reg_write16(controller, USBCMD, |
| 263 | uhci_reg_read16(controller, USBCMD) | 1); // start work on schedule |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static void |
| 267 | uhci_stop (hci_t *controller) |
| 268 | { |
Patrick Georgi | f42fdab | 2011-11-18 14:44:16 +0100 | [diff] [blame] | 269 | uhci_reg_write16(controller, USBCMD, |
| 270 | uhci_reg_read16(controller, USBCMD) & ~1); // stop work on schedule |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | #define GET_TD(x) ((void*)(((unsigned int)(x))&~0xf)) |
| 274 | |
| 275 | static td_t * |
| 276 | wait_for_completed_qh (hci_t *controller, qh_t *qh) |
| 277 | { |
Mathias Krause | 7b7b566 | 2012-05-29 16:19:19 +0200 | [diff] [blame] | 278 | int timeout = 1000; /* max 30 ms. */ |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 279 | void *current = GET_TD (qh->elementlinkptr); |
| 280 | while (((qh->elementlinkptr & FLISTP_TERMINATE) == 0) && (timeout-- > 0)) { |
| 281 | if (current != GET_TD (qh->elementlinkptr)) { |
| 282 | current = GET_TD (qh->elementlinkptr); |
Mathias Krause | 7b7b566 | 2012-05-29 16:19:19 +0200 | [diff] [blame] | 283 | timeout = 1000; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 284 | } |
Patrick Georgi | f42fdab | 2011-11-18 14:44:16 +0100 | [diff] [blame] | 285 | uhci_reg_write16(controller, USBSTS, |
| 286 | uhci_reg_read16(controller, USBSTS) | 0); // clear resettable registers |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 287 | udelay (30); |
| 288 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 289 | return (GET_TD (qh->elementlinkptr) == |
| 290 | 0) ? 0 : GET_TD (phys_to_virt (qh->elementlinkptr)); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 293 | static int |
| 294 | maxlen (int size) |
| 295 | { |
| 296 | return (size - 1) & 0x7ff; |
| 297 | } |
| 298 | |
| 299 | static int |
| 300 | min (int a, int b) |
| 301 | { |
| 302 | if (a < b) |
| 303 | return a; |
| 304 | else |
| 305 | return b; |
| 306 | } |
| 307 | |
| 308 | static int |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 309 | uhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq, int dalen, |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 310 | unsigned char *data) |
| 311 | { |
| 312 | int endp = 0; /* this is control: always 0 */ |
| 313 | int mlen = dev->endpoints[0].maxpacketsize; |
| 314 | int count = (2 + (dalen + mlen - 1) / mlen); |
| 315 | unsigned short req = ((unsigned short *) devreq)[0]; |
| 316 | int i; |
| 317 | td_t *tds = memalign (16, sizeof (td_t) * count); |
| 318 | memset (tds, 0, sizeof (td_t) * count); |
| 319 | count--; /* to compensate for 0-indexed array */ |
| 320 | for (i = 0; i < count; i++) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 321 | tds[i].ptr = virt_to_phys (&tds[i + 1]) | TD_DEPTH_FIRST; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 322 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 323 | tds[count].ptr = 0 | TD_DEPTH_FIRST | TD_TERMINATE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 324 | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 325 | tds[0].token = UHCI_SETUP | |
| 326 | dev->address << TD_DEVADDR_SHIFT | |
| 327 | endp << TD_EP_SHIFT | |
| 328 | TD_TOGGLE_DATA0 | |
| 329 | maxlen(drlen) << TD_MAXLEN_SHIFT; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 330 | tds[0].bufptr = virt_to_phys (devreq); |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 331 | tds[0].ctrlsts = (3 << TD_COUNTER_SHIFT) | |
| 332 | (dev->speed?TD_LOWSPEED:0) | |
| 333 | TD_STATUS_ACTIVE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 334 | |
| 335 | int toggle = 1; |
| 336 | for (i = 1; i < count; i++) { |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 337 | switch (dir) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 338 | case SETUP: tds[i].token = UHCI_SETUP; break; |
| 339 | case IN: tds[i].token = UHCI_IN; break; |
| 340 | case OUT: tds[i].token = UHCI_OUT; break; |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 341 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 342 | tds[i].token |= dev->address << TD_DEVADDR_SHIFT | |
| 343 | endp << TD_EP_SHIFT | |
| 344 | maxlen (min (mlen, dalen)) << TD_MAXLEN_SHIFT | |
| 345 | toggle << TD_TOGGLE_SHIFT; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 346 | tds[i].bufptr = virt_to_phys (data); |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 347 | tds[i].ctrlsts = (3 << TD_COUNTER_SHIFT) | |
| 348 | (dev->speed?TD_LOWSPEED:0) | |
| 349 | TD_STATUS_ACTIVE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 350 | toggle ^= 1; |
| 351 | dalen -= mlen; |
| 352 | data += mlen; |
| 353 | } |
| 354 | |
Nico Huber | cefec0e | 2012-05-16 15:04:27 +0200 | [diff] [blame] | 355 | tds[count].token = ((dir == OUT) ? UHCI_IN : UHCI_OUT) | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 356 | dev->address << TD_DEVADDR_SHIFT | |
| 357 | endp << TD_EP_SHIFT | |
| 358 | maxlen(0) << TD_MAXLEN_SHIFT | |
| 359 | TD_TOGGLE_DATA1; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 360 | tds[count].bufptr = 0; |
Nico Huber | cefec0e | 2012-05-16 15:04:27 +0200 | [diff] [blame] | 361 | tds[count].ctrlsts = (0 << TD_COUNTER_SHIFT) | /* as Linux 2.4.10 does */ |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 362 | (dev->speed?TD_LOWSPEED:0) | |
| 363 | TD_STATUS_ACTIVE; |
| 364 | UHCI_INST (dev->controller)->qh_data->elementlinkptr = |
| 365 | virt_to_phys (tds) & ~(FLISTP_QH | FLISTP_TERMINATE); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 366 | td_t *td = wait_for_completed_qh (dev->controller, |
| 367 | UHCI_INST (dev->controller)-> |
| 368 | qh_data); |
| 369 | int result; |
| 370 | if (td == 0) { |
| 371 | result = 0; |
| 372 | } else { |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 373 | usb_debug ("control packet, req %x\n", req); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 374 | td_dump (td); |
| 375 | result = 1; |
| 376 | } |
| 377 | free (tds); |
| 378 | return result; |
| 379 | } |
| 380 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 381 | static td_t * |
| 382 | create_schedule (int numpackets) |
| 383 | { |
| 384 | if (numpackets == 0) |
| 385 | return 0; |
| 386 | td_t *tds = memalign (16, sizeof (td_t) * numpackets); |
| 387 | memset (tds, 0, sizeof (td_t) * numpackets); |
| 388 | int i; |
| 389 | for (i = 0; i < numpackets; i++) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 390 | tds[i].ptr = virt_to_phys (&tds[i + 1]) | TD_DEPTH_FIRST; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 391 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 392 | tds[numpackets - 1].ptr = 0 | TD_TERMINATE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 393 | return tds; |
| 394 | } |
| 395 | |
| 396 | static void |
| 397 | fill_schedule (td_t *td, endpoint_t *ep, int length, unsigned char *data, |
| 398 | int *toggle) |
| 399 | { |
Patrick Georgi | cfaa081 | 2010-06-11 14:25:40 +0000 | [diff] [blame] | 400 | switch (ep->direction) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 401 | case IN: td->token = UHCI_IN; break; |
| 402 | case OUT: td->token = UHCI_OUT; break; |
| 403 | case SETUP: td->token = UHCI_SETUP; break; |
Patrick Georgi | cfaa081 | 2010-06-11 14:25:40 +0000 | [diff] [blame] | 404 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 405 | td->token |= ep->dev->address << TD_DEVADDR_SHIFT | |
| 406 | (ep->endpoint & 0xf) << TD_EP_SHIFT | |
| 407 | maxlen (length) << TD_MAXLEN_SHIFT | |
| 408 | (*toggle & 1) << TD_TOGGLE_SHIFT; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 409 | td->bufptr = virt_to_phys (data); |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 410 | td->ctrlsts = ((ep->direction == SETUP?3:0) << TD_COUNTER_SHIFT) | |
Nico Huber | cefec0e | 2012-05-16 15:04:27 +0200 | [diff] [blame] | 411 | (ep->dev->speed?TD_LOWSPEED:0) | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 412 | TD_STATUS_ACTIVE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 413 | *toggle ^= 1; |
| 414 | } |
| 415 | |
| 416 | static int |
| 417 | run_schedule (usbdev_t *dev, td_t *td) |
| 418 | { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 419 | UHCI_INST (dev->controller)->qh_data->elementlinkptr = |
Anton Kochkov | efcb8de | 2012-10-02 00:04:29 +0400 | [diff] [blame] | 420 | virt_to_phys (td) & ~(FLISTP_QH | FLISTP_TERMINATE); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 421 | td = wait_for_completed_qh (dev->controller, |
| 422 | UHCI_INST (dev->controller)->qh_data); |
| 423 | if (td == 0) { |
| 424 | return 0; |
| 425 | } else { |
| 426 | td_dump (td); |
| 427 | return 1; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /* finalize == 1: if data is of packet aligned size, add a zero length packet */ |
| 432 | static int |
| 433 | uhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize) |
| 434 | { |
| 435 | int maxpsize = ep->maxpacketsize; |
| 436 | if (maxpsize == 0) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 437 | fatal("MaxPacketSize == 0!!!"); |
Patrick Georgi | d2cb7ea | 2012-10-03 08:23:56 +0200 | [diff] [blame] | 438 | int numpackets = (size + maxpsize - 1) / maxpsize; |
| 439 | if (finalize && ((size % maxpsize) == 0)) { |
| 440 | numpackets++; |
| 441 | } |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 442 | if (numpackets == 0) |
| 443 | return 0; |
| 444 | td_t *tds = create_schedule (numpackets); |
| 445 | int i = 0, toggle = ep->toggle; |
| 446 | while ((size > 0) || ((size == 0) && (finalize != 0))) { |
| 447 | fill_schedule (&tds[i], ep, min (size, maxpsize), data, |
| 448 | &toggle); |
| 449 | i++; |
| 450 | data += maxpsize; |
| 451 | size -= maxpsize; |
| 452 | } |
| 453 | if (run_schedule (ep->dev, tds) == 1) { |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 454 | usb_debug("Stalled. Trying to clean up.\n"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 455 | clear_stall (ep); |
| 456 | free (tds); |
| 457 | return 1; |
| 458 | } |
| 459 | ep->toggle = toggle; |
| 460 | free (tds); |
| 461 | return 0; |
| 462 | } |
| 463 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 464 | typedef struct { |
| 465 | qh_t *qh; |
| 466 | td_t *tds; |
| 467 | td_t *last_td; |
| 468 | u8 *data; |
| 469 | int lastread; |
| 470 | int total; |
| 471 | int reqsize; |
| 472 | } intr_q; |
| 473 | |
| 474 | /* create and hook-up an intr queue into device schedule */ |
| 475 | static void* |
| 476 | uhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming) |
| 477 | { |
| 478 | u8 *data = malloc(reqsize*reqcount); |
| 479 | td_t *tds = memalign(16, sizeof(td_t) * reqcount); |
| 480 | qh_t *qh = memalign(16, sizeof(qh_t)); |
| 481 | |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 482 | if (!data || !tds || !qh) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 483 | fatal("Not enough memory to create USB intr queue prerequisites.\n"); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 484 | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 485 | qh->elementlinkptr = virt_to_phys(tds); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 486 | |
| 487 | intr_q *q = malloc(sizeof(intr_q)); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 488 | if (!q) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 489 | fatal("Not enough memory to create USB intr queue.\n"); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 490 | q->qh = qh; |
| 491 | q->tds = tds; |
| 492 | q->data = data; |
| 493 | q->lastread = 0; |
| 494 | q->total = reqcount; |
| 495 | q->reqsize = reqsize; |
| 496 | q->last_td = &tds[reqcount - 1]; |
| 497 | |
| 498 | memset (tds, 0, sizeof (td_t) * reqcount); |
| 499 | int i; |
| 500 | for (i = 0; i < reqcount; i++) { |
| 501 | tds[i].ptr = virt_to_phys (&tds[i + 1]); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 502 | |
Patrick Georgi | cfaa081 | 2010-06-11 14:25:40 +0000 | [diff] [blame] | 503 | switch (ep->direction) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 504 | case IN: tds[i].token = UHCI_IN; break; |
| 505 | case OUT: tds[i].token = UHCI_OUT; break; |
| 506 | case SETUP: tds[i].token = UHCI_SETUP; break; |
Patrick Georgi | cfaa081 | 2010-06-11 14:25:40 +0000 | [diff] [blame] | 507 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 508 | tds[i].token |= ep->dev->address << TD_DEVADDR_SHIFT | |
| 509 | (ep->endpoint & 0xf) << TD_EP_SHIFT | |
| 510 | maxlen (reqsize) << TD_MAXLEN_SHIFT | |
| 511 | (ep->toggle & 1) << TD_TOGGLE_SHIFT; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 512 | tds[i].bufptr = virt_to_phys (data); |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 513 | tds[i].ctrlsts = (0 << TD_COUNTER_SHIFT) | |
Nico Huber | cefec0e | 2012-05-16 15:04:27 +0200 | [diff] [blame] | 514 | (ep->dev->speed?TD_LOWSPEED:0) | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 515 | TD_STATUS_ACTIVE; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 516 | ep->toggle ^= 1; |
| 517 | data += reqsize; |
| 518 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 519 | tds[reqcount - 1].ptr = 0 | TD_TERMINATE; |
Nico Huber | ce407e4 | 2012-11-20 17:27:46 +0100 | [diff] [blame] | 520 | |
| 521 | /* insert QH into framelist */ |
| 522 | uhci_t *const uhcic = UHCI_INST(ep->dev->controller); |
| 523 | const u32 def_ptr = virt_to_phys(uhcic->qh_prei) | FLISTP_QH; |
| 524 | int nothing_placed = 1; |
| 525 | qh->headlinkptr = def_ptr; |
| 526 | for (i = 0; i < 1024; i += reqtiming) { |
| 527 | /* advance to the next free position */ |
| 528 | while ((i < 1024) && (uhcic->framelistptr[i] != def_ptr)) ++i; |
| 529 | if (i < 1024) { |
| 530 | uhcic->framelistptr[i] = virt_to_phys(qh) | FLISTP_QH; |
| 531 | nothing_placed = 0; |
| 532 | } |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 533 | } |
Nico Huber | ce407e4 | 2012-11-20 17:27:46 +0100 | [diff] [blame] | 534 | if (nothing_placed) { |
| 535 | printf("Error: Failed to place UHCI interrupt queue " |
| 536 | "head into framelist: no space left\n"); |
| 537 | uhci_destroy_intr_queue(ep, q); |
| 538 | return NULL; |
| 539 | } |
| 540 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 541 | return q; |
| 542 | } |
| 543 | |
| 544 | /* remove queue from device schedule, dropping all data that came in */ |
| 545 | static void |
| 546 | uhci_destroy_intr_queue (endpoint_t *ep, void *q_) |
| 547 | { |
Nico Huber | ce407e4 | 2012-11-20 17:27:46 +0100 | [diff] [blame] | 548 | intr_q *const q = (intr_q*)q_; |
| 549 | |
| 550 | /* remove QH from framelist */ |
| 551 | uhci_t *const uhcic = UHCI_INST(ep->dev->controller); |
| 552 | const u32 qh_ptr = virt_to_phys(q->qh) | FLISTP_QH; |
| 553 | const u32 def_ptr = virt_to_phys(uhcic->qh_prei) | FLISTP_QH; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 554 | int i; |
Nico Huber | ce407e4 | 2012-11-20 17:27:46 +0100 | [diff] [blame] | 555 | for (i = 0; i < 1024; ++i) { |
| 556 | if (uhcic->framelistptr[i] == qh_ptr) |
| 557 | uhcic->framelistptr[i] = def_ptr; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 558 | } |
Nico Huber | ce407e4 | 2012-11-20 17:27:46 +0100 | [diff] [blame] | 559 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 560 | free(q->data); |
| 561 | free(q->tds); |
| 562 | free(q->qh); |
| 563 | free(q); |
| 564 | } |
| 565 | |
| 566 | /* read one intr-packet from queue, if available. extend the queue for new input. |
| 567 | return NULL if nothing new available. |
| 568 | Recommended use: while (data=poll_intr_queue(q)) process(data); |
| 569 | */ |
| 570 | static u8* |
| 571 | uhci_poll_intr_queue (void *q_) |
| 572 | { |
| 573 | intr_q *q = (intr_q*)q_; |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 574 | if ((q->tds[q->lastread].ctrlsts & TD_STATUS_ACTIVE) == 0) { |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 575 | /* FIXME: handle errors */ |
| 576 | int current = q->lastread; |
| 577 | int previous; |
| 578 | if (q->lastread == 0) { |
| 579 | previous = q->total - 1; |
| 580 | } else { |
| 581 | previous = q->lastread - 1; |
| 582 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 583 | q->tds[previous].ctrlsts &= ~TD_STATUS_MASK; |
| 584 | q->tds[previous].ptr = 0 | TD_TERMINATE; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 585 | if (q->last_td != &q->tds[previous]) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 586 | q->last_td->ptr = virt_to_phys(&q->tds[previous]) & ~TD_TERMINATE; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 587 | q->last_td = &q->tds[previous]; |
| 588 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 589 | q->tds[previous].ctrlsts |= TD_STATUS_ACTIVE; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 590 | q->lastread = (q->lastread + 1) % q->total; |
| 591 | return &q->data[current*q->reqsize]; |
| 592 | } |
Nico Huber | 8c4d2f3 | 2012-11-20 17:49:00 +0100 | [diff] [blame^] | 593 | /* reset queue if we fully processed it after underrun */ |
| 594 | else if (q->qh->elementlinkptr & FLISTP_TERMINATE) { |
| 595 | usb_debug("resetting underrun uhci interrupt queue.\n"); |
| 596 | q->qh->elementlinkptr = virt_to_phys(q->tds + q->lastread); |
| 597 | } |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 598 | return NULL; |
| 599 | } |
| 600 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 601 | void |
| 602 | uhci_reg_write32 (hci_t *ctrl, usbreg reg, u32 value) |
| 603 | { |
| 604 | outl (value, ctrl->reg_base + reg); |
| 605 | } |
| 606 | |
| 607 | u32 |
| 608 | uhci_reg_read32 (hci_t *ctrl, usbreg reg) |
| 609 | { |
| 610 | return inl (ctrl->reg_base + reg); |
| 611 | } |
| 612 | |
| 613 | void |
| 614 | uhci_reg_write16 (hci_t *ctrl, usbreg reg, u16 value) |
| 615 | { |
| 616 | outw (value, ctrl->reg_base + reg); |
| 617 | } |
| 618 | |
| 619 | u16 |
| 620 | uhci_reg_read16 (hci_t *ctrl, usbreg reg) |
| 621 | { |
| 622 | return inw (ctrl->reg_base + reg); |
| 623 | } |
| 624 | |
| 625 | void |
| 626 | uhci_reg_write8 (hci_t *ctrl, usbreg reg, u8 value) |
| 627 | { |
| 628 | outb (value, ctrl->reg_base + reg); |
| 629 | } |
| 630 | |
| 631 | u8 |
| 632 | uhci_reg_read8 (hci_t *ctrl, usbreg reg) |
| 633 | { |
| 634 | return inb (ctrl->reg_base + reg); |
| 635 | } |