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) { |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 455 | usb_debug("Stalled. Trying to clean up.\n"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 456 | clear_stall (ep); |
| 457 | free (tds); |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 458 | return -1; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 459 | } |
| 460 | ep->toggle = toggle; |
| 461 | free (tds); |
| 462 | return 0; |
| 463 | } |
| 464 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 465 | typedef struct { |
| 466 | qh_t *qh; |
| 467 | td_t *tds; |
| 468 | td_t *last_td; |
| 469 | u8 *data; |
| 470 | int lastread; |
| 471 | int total; |
| 472 | int reqsize; |
| 473 | } intr_q; |
| 474 | |
| 475 | /* create and hook-up an intr queue into device schedule */ |
| 476 | static void* |
| 477 | uhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming) |
| 478 | { |
| 479 | u8 *data = malloc(reqsize*reqcount); |
| 480 | td_t *tds = memalign(16, sizeof(td_t) * reqcount); |
| 481 | qh_t *qh = memalign(16, sizeof(qh_t)); |
| 482 | |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 483 | if (!data || !tds || !qh) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 484 | fatal("Not enough memory to create USB intr queue prerequisites.\n"); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 485 | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 486 | qh->elementlinkptr = virt_to_phys(tds); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 487 | |
| 488 | intr_q *q = malloc(sizeof(intr_q)); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 489 | if (!q) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 490 | fatal("Not enough memory to create USB intr queue.\n"); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 491 | q->qh = qh; |
| 492 | q->tds = tds; |
| 493 | q->data = data; |
| 494 | q->lastread = 0; |
| 495 | q->total = reqcount; |
| 496 | q->reqsize = reqsize; |
| 497 | q->last_td = &tds[reqcount - 1]; |
| 498 | |
| 499 | memset (tds, 0, sizeof (td_t) * reqcount); |
| 500 | int i; |
| 501 | for (i = 0; i < reqcount; i++) { |
| 502 | tds[i].ptr = virt_to_phys (&tds[i + 1]); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 503 | |
Patrick Georgi | cfaa081 | 2010-06-11 14:25:40 +0000 | [diff] [blame] | 504 | switch (ep->direction) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 505 | case IN: tds[i].token = UHCI_IN; break; |
| 506 | case OUT: tds[i].token = UHCI_OUT; break; |
| 507 | case SETUP: tds[i].token = UHCI_SETUP; break; |
Patrick Georgi | cfaa081 | 2010-06-11 14:25:40 +0000 | [diff] [blame] | 508 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 509 | tds[i].token |= ep->dev->address << TD_DEVADDR_SHIFT | |
| 510 | (ep->endpoint & 0xf) << TD_EP_SHIFT | |
| 511 | maxlen (reqsize) << TD_MAXLEN_SHIFT | |
| 512 | (ep->toggle & 1) << TD_TOGGLE_SHIFT; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 513 | tds[i].bufptr = virt_to_phys (data); |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 514 | tds[i].ctrlsts = (0 << TD_COUNTER_SHIFT) | |
Nico Huber | cefec0e | 2012-05-16 15:04:27 +0200 | [diff] [blame] | 515 | (ep->dev->speed?TD_LOWSPEED:0) | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 516 | TD_STATUS_ACTIVE; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 517 | ep->toggle ^= 1; |
| 518 | data += reqsize; |
| 519 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 520 | tds[reqcount - 1].ptr = 0 | TD_TERMINATE; |
Nico Huber | ce407e4 | 2012-11-20 17:27:46 +0100 | [diff] [blame] | 521 | |
| 522 | /* insert QH into framelist */ |
| 523 | uhci_t *const uhcic = UHCI_INST(ep->dev->controller); |
| 524 | const u32 def_ptr = virt_to_phys(uhcic->qh_prei) | FLISTP_QH; |
| 525 | int nothing_placed = 1; |
| 526 | qh->headlinkptr = def_ptr; |
| 527 | for (i = 0; i < 1024; i += reqtiming) { |
| 528 | /* advance to the next free position */ |
| 529 | while ((i < 1024) && (uhcic->framelistptr[i] != def_ptr)) ++i; |
| 530 | if (i < 1024) { |
| 531 | uhcic->framelistptr[i] = virt_to_phys(qh) | FLISTP_QH; |
| 532 | nothing_placed = 0; |
| 533 | } |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 534 | } |
Nico Huber | ce407e4 | 2012-11-20 17:27:46 +0100 | [diff] [blame] | 535 | if (nothing_placed) { |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 536 | usb_debug("Error: Failed to place UHCI interrupt queue " |
Nico Huber | ce407e4 | 2012-11-20 17:27:46 +0100 | [diff] [blame] | 537 | "head into framelist: no space left\n"); |
| 538 | uhci_destroy_intr_queue(ep, q); |
| 539 | return NULL; |
| 540 | } |
| 541 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 542 | return q; |
| 543 | } |
| 544 | |
| 545 | /* remove queue from device schedule, dropping all data that came in */ |
| 546 | static void |
| 547 | uhci_destroy_intr_queue (endpoint_t *ep, void *q_) |
| 548 | { |
Nico Huber | ce407e4 | 2012-11-20 17:27:46 +0100 | [diff] [blame] | 549 | intr_q *const q = (intr_q*)q_; |
| 550 | |
| 551 | /* remove QH from framelist */ |
| 552 | uhci_t *const uhcic = UHCI_INST(ep->dev->controller); |
| 553 | const u32 qh_ptr = virt_to_phys(q->qh) | FLISTP_QH; |
| 554 | const u32 def_ptr = virt_to_phys(uhcic->qh_prei) | FLISTP_QH; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 555 | int i; |
Nico Huber | ce407e4 | 2012-11-20 17:27:46 +0100 | [diff] [blame] | 556 | for (i = 0; i < 1024; ++i) { |
| 557 | if (uhcic->framelistptr[i] == qh_ptr) |
| 558 | uhcic->framelistptr[i] = def_ptr; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 559 | } |
Nico Huber | ce407e4 | 2012-11-20 17:27:46 +0100 | [diff] [blame] | 560 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 561 | free(q->data); |
| 562 | free(q->tds); |
| 563 | free(q->qh); |
| 564 | free(q); |
| 565 | } |
| 566 | |
| 567 | /* read one intr-packet from queue, if available. extend the queue for new input. |
| 568 | return NULL if nothing new available. |
| 569 | Recommended use: while (data=poll_intr_queue(q)) process(data); |
| 570 | */ |
| 571 | static u8* |
| 572 | uhci_poll_intr_queue (void *q_) |
| 573 | { |
| 574 | intr_q *q = (intr_q*)q_; |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 575 | if ((q->tds[q->lastread].ctrlsts & TD_STATUS_ACTIVE) == 0) { |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 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; |
Nico Huber | be58fee | 2012-11-22 11:12:13 +0100 | [diff] [blame] | 591 | if (!(q->tds[current].ctrlsts & TD_STATUS_MASK)) |
| 592 | return &q->data[current*q->reqsize]; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 593 | } |
Nico Huber | 8c4d2f3 | 2012-11-20 17:49:00 +0100 | [diff] [blame] | 594 | /* reset queue if we fully processed it after underrun */ |
| 595 | else if (q->qh->elementlinkptr & FLISTP_TERMINATE) { |
| 596 | usb_debug("resetting underrun uhci interrupt queue.\n"); |
| 597 | q->qh->elementlinkptr = virt_to_phys(q->tds + q->lastread); |
| 598 | } |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 599 | return NULL; |
| 600 | } |
| 601 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 602 | void |
| 603 | uhci_reg_write32 (hci_t *ctrl, usbreg reg, u32 value) |
| 604 | { |
| 605 | outl (value, ctrl->reg_base + reg); |
| 606 | } |
| 607 | |
| 608 | u32 |
| 609 | uhci_reg_read32 (hci_t *ctrl, usbreg reg) |
| 610 | { |
| 611 | return inl (ctrl->reg_base + reg); |
| 612 | } |
| 613 | |
| 614 | void |
| 615 | uhci_reg_write16 (hci_t *ctrl, usbreg reg, u16 value) |
| 616 | { |
| 617 | outw (value, ctrl->reg_base + reg); |
| 618 | } |
| 619 | |
| 620 | u16 |
| 621 | uhci_reg_read16 (hci_t *ctrl, usbreg reg) |
| 622 | { |
| 623 | return inw (ctrl->reg_base + reg); |
| 624 | } |
| 625 | |
| 626 | void |
| 627 | uhci_reg_write8 (hci_t *ctrl, usbreg reg, u8 value) |
| 628 | { |
| 629 | outb (value, ctrl->reg_base + reg); |
| 630 | } |
| 631 | |
| 632 | u8 |
| 633 | uhci_reg_read8 (hci_t *ctrl, usbreg reg) |
| 634 | { |
| 635 | return inb (ctrl->reg_base + reg); |
| 636 | } |