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