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