Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2010 coresystems GmbH |
| 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 | d5c79f9 | 2013-03-25 15:56:08 -0700 | [diff] [blame] | 30 | //#define USB_DEBUG |
| 31 | |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 32 | #include <libpayload.h> |
| 33 | #include "ehci.h" |
| 34 | #include "ehci_private.h" |
| 35 | |
Stefan Reinauer | 528b43d | 2011-04-14 19:52:04 +0000 | [diff] [blame] | 36 | static void dump_td(u32 addr) |
| 37 | { |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 38 | qtd_t *td = phys_to_virt(addr); |
Anton Kochkov | c07fb15 | 2012-12-19 12:32:32 +0400 | [diff] [blame] | 39 | usb_debug("+---------------------------------------------------+\n"); |
| 40 | if (((td->token & (3UL << 8)) >> 8) == 2) |
| 41 | usb_debug("|..[SETUP]..........................................|\n"); |
| 42 | else if (((td->token & (3UL << 8)) >> 8) == 1) |
| 43 | usb_debug("|..[IN].............................................|\n"); |
| 44 | else if (((td->token & (3UL << 8)) >> 8) == 0) |
| 45 | usb_debug("|..[OUT]............................................|\n"); |
| 46 | else |
| 47 | usb_debug("|..[]...............................................|\n"); |
| 48 | usb_debug("|:|============ EHCI TD at [0x%08lx] ==========|:|\n", addr); |
| 49 | usb_debug("|:| ERRORS = [%ld] | TOKEN = [0x%08lx] | |:|\n", |
| 50 | 3 - ((td->token & QTD_CERR_MASK) >> QTD_CERR_SHIFT), td->token); |
| 51 | usb_debug("|:+-----------------------------------------------+:|\n"); |
| 52 | usb_debug("|:| Next qTD [0x%08lx] |:|\n", td->next_qtd); |
| 53 | usb_debug("|:+-----------------------------------------------+:|\n"); |
| 54 | usb_debug("|:| Alt. Next qTD [0x%08lx] |:|\n", td->alt_next_qtd); |
| 55 | usb_debug("|:+-----------------------------------------------+:|\n"); |
| 56 | usb_debug("|:| | Bytes to Transfer | [%04ld] |:|\n", (td->token & (0x7FUL << 16)) >> 16); |
| 57 | usb_debug("|:| | PID CODE: | [%ld] |:|\n", (td->token & (3UL << 8)) >> 8); |
| 58 | usb_debug("|:| | Interrupt On Complete (IOC) | [%ld] |:|\n", (td->token & (1UL << 15)) >> 15); |
| 59 | usb_debug("|:| | Status Active | [%ld] |:|\n", (td->token & (1UL << 7)) >> 7); |
| 60 | usb_debug("|:| | Status Halted | [%ld] |:|\n", (td->token & (1UL << 6)) >> 6); |
| 61 | usb_debug("|:| TOKEN | Status Data Buffer Error | [%ld] |:|\n", (td->token & (1UL << 5)) >> 5); |
| 62 | usb_debug("|:| | Status Babble detected | [%ld] |:|\n", (td->token & (1UL << 4)) >> 4); |
| 63 | usb_debug("|:| | Status Transaction Error | [%ld] |:|\n", (td->token & (1UL << 3)) >> 3); |
| 64 | usb_debug("|:| | Status Missed Micro Frame | [%ld] |:|\n", (td->token & (1UL << 2)) >> 2); |
| 65 | usb_debug("|:| | Split Transaction State | [%ld] |:|\n", (td->token & (1UL << 1)) >> 1); |
| 66 | usb_debug("|:| | Ping State | [%ld] |:|\n", td->token & 1UL); |
| 67 | usb_debug("|:|-----------------------------------------------|:|\n"); |
| 68 | usb_debug("|...................................................|\n"); |
| 69 | usb_debug("+---------------------------------------------------+\n"); |
| 70 | } |
| 71 | |
Stefan Reinauer | 8992e53 | 2013-05-02 16:16:41 -0700 | [diff] [blame] | 72 | #if 0 && defined(USB_DEBUG) |
Anton Kochkov | c07fb15 | 2012-12-19 12:32:32 +0400 | [diff] [blame] | 73 | static void dump_qh(ehci_qh_t *cur) |
| 74 | { |
| 75 | qtd_t *tmp_qtd = NULL; |
| 76 | usb_debug("+===================================================+\n"); |
| 77 | usb_debug("| ############# EHCI QH at [0x%08lx] ########### |\n", virt_to_phys(cur)); |
| 78 | usb_debug("+---------------------------------------------------+\n"); |
| 79 | usb_debug("| Horizonal Link Pointer [0x%08lx] |\n", cur->horiz_link_ptr); |
| 80 | usb_debug("+------------------[ 0x%08lx ]-------------------+\n", cur->epchar); |
| 81 | usb_debug("| | Maximum Packet Length | [%04ld] |\n", ((cur->epchar & (0x7ffUL << 16)) >> 16)); |
| 82 | usb_debug("| | Device Address | [%ld] |\n", cur->epchar & 0x7F); |
| 83 | usb_debug("| | Inactivate on Next Transaction | [%ld] |\n", ((cur->epchar & (1UL << 7)) >> 7)); |
| 84 | usb_debug("| | Endpoint Number | [%ld] |\n", ((cur->epchar & (0xFUL << 8)) >> 8)); |
| 85 | usb_debug("| EPCHAR | Endpoint Speed | [%ld] |\n", ((cur->epchar & (3UL << 12)) >> 12)); |
| 86 | usb_debug("| | Data Toggle Control | [%ld] |\n", ((cur->epchar & (1UL << 14)) >> 14)); |
| 87 | usb_debug("| | Head of Reclamation List Flag | [%ld] |\n", ((cur->epchar & (1UL << 15)) >> 15)); |
| 88 | usb_debug("| | Control Endpoint Flag | [%ld] |\n", ((cur->epchar & (1UL << 27)) >> 27)); |
| 89 | usb_debug("| | Nak Count Reload | [%ld] |\n", ((cur->epchar & (0xFUL << 28)) >> 28)); |
| 90 | if (((cur->epchar & (1UL << QH_NON_HS_CTRL_EP_SHIFT)) >> QH_NON_HS_CTRL_EP_SHIFT) == 1) { /* Split transaction */ |
| 91 | usb_debug("+--------+---------[ 0x%08lx ]----------+--------+\n", cur->epcaps); |
| 92 | usb_debug("| | Hub Port | [%ld] |\n", ((cur->epcaps & (0x7FUL << 23)) >> 23)); /* [29:23] */ |
| 93 | usb_debug("| | Hub Address | [%ld] |\n", ((cur->epcaps & (0x7FUL << 16)) >> 16)); /* [22:16] */ |
| 94 | } |
| 95 | usb_debug("+---------------------------------------------------+\n"); |
| 96 | usb_debug("| Current QTD [0x%08lx] |\n", cur->current_td_ptr); |
| 97 | |
| 98 | if (!((cur->horiz_link_ptr == 0) && (cur->epchar == 0))) { |
| 99 | /* Dump overlay QTD for this QH */ |
| 100 | usb_debug("+---------------------------------------------------+\n"); |
| 101 | usb_debug("|::::::::::::::::::: QTD OVERLAY :::::::::::::::::::|\n"); |
| 102 | dump_td(virt_to_phys((void *)&(cur->td))); |
| 103 | /* Dump all TD tree for this QH */ |
| 104 | tmp_qtd = (qtd_t *)phys_to_virt((cur->td.next_qtd & ~0x1FUL)); |
| 105 | if (tmp_qtd != NULL) |
| 106 | usb_debug("|:::::::::::::::::: EHCI QTD CHAIN :::::::::::::::::|\n"); |
| 107 | while (tmp_qtd != NULL) |
| 108 | { |
| 109 | dump_td(virt_to_phys(tmp_qtd)); |
| 110 | tmp_qtd = (qtd_t *)phys_to_virt((tmp_qtd->next_qtd & ~0x1FUL)); |
| 111 | } |
| 112 | usb_debug("|:::::::::::::::: EOF EHCI QTD CHAIN :::::::::::::::|\n"); |
| 113 | usb_debug("+---------------------------------------------------+\n"); |
| 114 | } else { |
| 115 | usb_debug("+---------------------------------------------------+\n"); |
| 116 | } |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 117 | } |
Stefan Reinauer | d5c79f9 | 2013-03-25 15:56:08 -0700 | [diff] [blame] | 118 | #endif |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 119 | |
| 120 | static void ehci_start (hci_t *controller) |
| 121 | { |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 122 | EHCI_INST(controller)->operation->usbcmd |= HC_OP_RS; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static void ehci_stop (hci_t *controller) |
| 126 | { |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 127 | EHCI_INST(controller)->operation->usbcmd &= ~HC_OP_RS; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void ehci_reset (hci_t *controller) |
| 131 | { |
Anton Kochkov | 2fef58e | 2012-11-09 14:06:05 +0100 | [diff] [blame] | 132 | short count = 0; |
| 133 | ehci_stop(controller); |
| 134 | /* wait 10 ms just to be shure */ |
| 135 | mdelay(10); |
| 136 | if (EHCI_INST(controller)->operation->usbsts & HC_OP_HC_HALTED) { |
| 137 | EHCI_INST(controller)->operation->usbcmd = HC_OP_HC_RESET; |
| 138 | /* wait 100 ms */ |
| 139 | for (count = 0; count < 10; count++) { |
| 140 | mdelay(10); |
| 141 | if (!(EHCI_INST(controller)->operation->usbcmd & HC_OP_HC_RESET)) { |
| 142 | return; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | usb_debug("ehci_reset(): reset failed!\n"); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Nico Huber | 6e711c6 | 2012-11-12 16:20:32 +0100 | [diff] [blame] | 149 | static void ehci_reinit (hci_t *controller) |
| 150 | { |
| 151 | } |
| 152 | |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 153 | static int ehci_set_periodic_schedule(ehci_t *ehcic, int enable) |
| 154 | { |
| 155 | /* Set periodic schedule status. */ |
| 156 | if (enable) |
| 157 | ehcic->operation->usbcmd |= HC_OP_PERIODIC_SCHED_EN; |
| 158 | else |
| 159 | ehcic->operation->usbcmd &= ~HC_OP_PERIODIC_SCHED_EN; |
| 160 | /* Wait for the controller to accept periodic schedule status. |
| 161 | * This shouldn't take too long, but we should timeout nevertheless. |
| 162 | */ |
| 163 | enable = enable ? HC_OP_PERIODIC_SCHED_STAT : 0; |
Gabe Black | ef650a5 | 2012-11-21 02:16:13 -0800 | [diff] [blame] | 164 | int timeout = 100000; /* time out after 100ms */ |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 165 | while (((ehcic->operation->usbsts & HC_OP_PERIODIC_SCHED_STAT) != enable) |
| 166 | && timeout--) |
Gabe Black | ef650a5 | 2012-11-21 02:16:13 -0800 | [diff] [blame] | 167 | udelay(1); |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 168 | if (timeout < 0) { |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 169 | usb_debug("ehci periodic schedule status change timed out.\n"); |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 170 | return 1; |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 175 | static void ehci_shutdown (hci_t *controller) |
| 176 | { |
Mathias Krause | 7a9da71 | 2013-03-24 19:40:01 +0100 | [diff] [blame] | 177 | detach_controller(controller); |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 178 | /* Make sure periodic schedule is disabled */ |
| 179 | ehci_set_periodic_schedule(EHCI_INST(controller), 0); |
| 180 | /* Free periodic frame list */ |
| 181 | free(phys_to_virt(EHCI_INST(controller)->operation->periodiclistbase)); |
| 182 | |
Nico Huber | 3ca35ca | 2012-06-14 13:27:39 +0200 | [diff] [blame] | 183 | /* Free dummy QH */ |
Nico Huber | 4f83d1b | 2012-11-23 11:58:57 +0100 | [diff] [blame] | 184 | free((void *)EHCI_INST(controller)->dummy_qh); |
Nico Huber | 3ca35ca | 2012-06-14 13:27:39 +0200 | [diff] [blame] | 185 | |
Patrick Georgi | 01178bb | 2011-11-04 11:57:46 +0100 | [diff] [blame] | 186 | EHCI_INST(controller)->operation->configflag = 0; |
Nico Huber | d633dda | 2012-11-12 15:35:44 +0100 | [diff] [blame] | 187 | |
| 188 | free(EHCI_INST(controller)); |
| 189 | free(controller); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | enum { EHCI_OUT=0, EHCI_IN=1, EHCI_SETUP=2 }; |
| 193 | |
Nico Huber | 1ab6075 | 2012-05-23 09:21:54 +0200 | [diff] [blame] | 194 | /* |
| 195 | * returns the address of the closest USB2.0 hub, which is responsible for |
| 196 | * split transactions, along with the number of the used downstream port |
| 197 | */ |
| 198 | static int closest_usb2_hub(const usbdev_t *dev, int *const addr, int *const port) |
| 199 | { |
| 200 | const usbdev_t *usb1dev; |
| 201 | do { |
| 202 | usb1dev = dev; |
| 203 | if ((dev->hub > 0) && (dev->hub < 128)) |
| 204 | dev = dev->controller->devices[dev->hub]; |
| 205 | else |
| 206 | dev = NULL; |
| 207 | } while (dev && (dev->speed < 2)); |
| 208 | if (dev) { |
| 209 | *addr = usb1dev->hub; |
| 210 | *port = usb1dev->port; |
| 211 | return 0; |
| 212 | } else { |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 213 | usb_debug("ehci: Couldn't find closest USB2.0 hub.\n"); |
Nico Huber | 1ab6075 | 2012-05-23 09:21:54 +0200 | [diff] [blame] | 214 | return 1; |
| 215 | } |
| 216 | } |
| 217 | |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 218 | /* returns handled bytes. assumes that the fields it writes are empty on entry */ |
| 219 | static int fill_td(qtd_t *td, void* data, int datalen) |
Stefan Reinauer | 528b43d | 2011-04-14 19:52:04 +0000 | [diff] [blame] | 220 | { |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 221 | u32 total_len = 0; |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 222 | u32 page_no = 0; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 223 | |
| 224 | u32 start = virt_to_phys(data); |
| 225 | u32 page = start & ~4095; |
| 226 | u32 offset = start & 4095; |
| 227 | u32 page_len = 4096 - offset; |
| 228 | |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 229 | td->token |= 0 << QTD_CPAGE_SHIFT; |
| 230 | td->bufptrs[page_no++] = start; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 231 | |
| 232 | if (datalen <= page_len) { |
| 233 | total_len = datalen; |
| 234 | } else { |
| 235 | datalen -= page_len; |
| 236 | total_len += page_len; |
| 237 | |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 238 | while (page_no < 5) { |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 239 | /* we have a continguous mapping between virtual and physical memory */ |
| 240 | page += 4096; |
| 241 | |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 242 | td->bufptrs[page_no++] = page; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 243 | if (datalen <= 4096) { |
| 244 | total_len += datalen; |
| 245 | break; |
| 246 | } |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 247 | datalen -= 4096; |
| 248 | total_len += 4096; |
Julius Werner | 69eea7c | 2013-01-11 16:25:52 -0800 | [diff] [blame] | 249 | |
| 250 | /* end TD at a packet boundary if transfer not complete */ |
| 251 | if (page_no == 5) |
| 252 | total_len &= ~511; |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 253 | } |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 254 | } |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 255 | td->token |= total_len << QTD_TOTAL_LEN_SHIFT; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 256 | return total_len; |
| 257 | } |
| 258 | |
| 259 | /* free up data structures */ |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 260 | static void free_qh_and_tds(ehci_qh_t *qh, qtd_t *cur) |
Stefan Reinauer | 528b43d | 2011-04-14 19:52:04 +0000 | [diff] [blame] | 261 | { |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 262 | qtd_t *next; |
| 263 | while (cur) { |
| 264 | next = (qtd_t*)phys_to_virt(cur->next_qtd & ~31); |
Nico Huber | 4f83d1b | 2012-11-23 11:58:57 +0100 | [diff] [blame] | 265 | free((void *)cur); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 266 | cur = next; |
| 267 | } |
Nico Huber | 4f83d1b | 2012-11-23 11:58:57 +0100 | [diff] [blame] | 268 | free((void *)qh); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 271 | static int wait_for_tds(qtd_t *head) |
Stefan Reinauer | 528b43d | 2011-04-14 19:52:04 +0000 | [diff] [blame] | 272 | { |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 273 | /* returns the amount of bytes *not* transmitted, or -1 for error */ |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 274 | int result = 0; |
| 275 | qtd_t *cur = head; |
| 276 | while (1) { |
| 277 | if (0) dump_td(virt_to_phys(cur)); |
Nico Huber | 0421dc8 | 2012-05-21 14:53:43 +0200 | [diff] [blame] | 278 | |
| 279 | /* wait for results */ |
Nico Huber | cef8692 | 2012-06-01 08:54:29 +0200 | [diff] [blame] | 280 | /* how long to wait? |
| 281 | * tested with some USB2.0 flash sticks: |
| 282 | * TUR turn around took |
Julius Werner | 52e3f44 | 2013-07-23 12:45:18 -0700 | [diff] [blame] | 283 | * about 2.2s for the slowest (13fe:3800) |
Nico Huber | cef8692 | 2012-06-01 08:54:29 +0200 | [diff] [blame] | 284 | * max. 250ms for the others |
| 285 | * slowest non-TUR turn around took about 1.3s |
Julius Werner | 52e3f44 | 2013-07-23 12:45:18 -0700 | [diff] [blame] | 286 | * set to 3s to be safe as a failed TUR can be fatal |
Nico Huber | 0421dc8 | 2012-05-21 14:53:43 +0200 | [diff] [blame] | 287 | */ |
Julius Werner | 52e3f44 | 2013-07-23 12:45:18 -0700 | [diff] [blame] | 288 | int timeout = 60000; /* time out after 60000 * 50us == 3s */ |
Nico Huber | 0421dc8 | 2012-05-21 14:53:43 +0200 | [diff] [blame] | 289 | while ((cur->token & QTD_ACTIVE) && !(cur->token & QTD_HALTED) |
| 290 | && timeout--) |
| 291 | udelay(50); |
| 292 | if (timeout < 0) { |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 293 | usb_debug("Error: ehci: queue transfer " |
Nico Huber | 0421dc8 | 2012-05-21 14:53:43 +0200 | [diff] [blame] | 294 | "processing timed out.\n"); |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 295 | return -1; |
Nico Huber | 0421dc8 | 2012-05-21 14:53:43 +0200 | [diff] [blame] | 296 | } |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 297 | if (cur->token & QTD_HALTED) { |
Nico Huber | 86c686a | 2012-11-21 16:22:26 +0100 | [diff] [blame] | 298 | usb_debug("ERROR with packet\n"); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 299 | dump_td(virt_to_phys(cur)); |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 300 | usb_debug("-----------------\n"); |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 301 | return -1; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 302 | } |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 303 | result += (cur->token & QTD_TOTAL_LEN_MASK) |
| 304 | >> QTD_TOTAL_LEN_SHIFT; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 305 | if (cur->next_qtd & 1) { |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 306 | break; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 307 | } |
| 308 | if (0) dump_td(virt_to_phys(cur)); |
| 309 | /* helps debugging the TD chain */ |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 310 | if (0) usb_debug("\nmoving from %x to %x\n", cur, phys_to_virt(cur->next_qtd)); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 311 | cur = phys_to_virt(cur->next_qtd); |
| 312 | } |
| 313 | return result; |
| 314 | } |
| 315 | |
Nico Huber | 0421dc8 | 2012-05-21 14:53:43 +0200 | [diff] [blame] | 316 | static int ehci_set_async_schedule(ehci_t *ehcic, int enable) |
| 317 | { |
| 318 | /* Set async schedule status. */ |
| 319 | if (enable) |
| 320 | ehcic->operation->usbcmd |= HC_OP_ASYNC_SCHED_EN; |
| 321 | else |
| 322 | ehcic->operation->usbcmd &= ~HC_OP_ASYNC_SCHED_EN; |
| 323 | /* Wait for the controller to accept async schedule status. |
| 324 | * This shouldn't take too long, but we should timeout nevertheless. |
| 325 | */ |
| 326 | enable = enable ? HC_OP_ASYNC_SCHED_STAT : 0; |
| 327 | int timeout = 100; /* time out after 100ms */ |
| 328 | while (((ehcic->operation->usbsts & HC_OP_ASYNC_SCHED_STAT) != enable) |
| 329 | && timeout--) |
| 330 | mdelay(1); |
| 331 | if (timeout < 0) { |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 332 | usb_debug("ehci async schedule status change timed out.\n"); |
Nico Huber | 0421dc8 | 2012-05-21 14:53:43 +0200 | [diff] [blame] | 333 | return 1; |
| 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int ehci_process_async_schedule( |
| 339 | ehci_t *ehcic, ehci_qh_t *qhead, qtd_t *head) |
| 340 | { |
| 341 | int result; |
| 342 | |
| 343 | /* make sure async schedule is disabled */ |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 344 | if (ehci_set_async_schedule(ehcic, 0)) return -1; |
Nico Huber | 0421dc8 | 2012-05-21 14:53:43 +0200 | [diff] [blame] | 345 | |
| 346 | /* hook up QH */ |
| 347 | ehcic->operation->asynclistaddr = virt_to_phys(qhead); |
| 348 | |
| 349 | /* start async schedule */ |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 350 | if (ehci_set_async_schedule(ehcic, 1)) return -1; |
Nico Huber | 0421dc8 | 2012-05-21 14:53:43 +0200 | [diff] [blame] | 351 | |
| 352 | /* wait for result */ |
| 353 | result = wait_for_tds(head); |
| 354 | |
| 355 | /* disable async schedule */ |
| 356 | ehci_set_async_schedule(ehcic, 0); |
| 357 | |
| 358 | return result; |
| 359 | } |
| 360 | |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 361 | static int ehci_bulk (endpoint_t *ep, int size, u8 *data, int finalize) |
| 362 | { |
| 363 | int result = 0; |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 364 | int remaining = size; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 365 | int endp = ep->endpoint & 0xf; |
| 366 | int pid = (ep->direction==IN)?EHCI_IN:EHCI_OUT; |
| 367 | |
Nico Huber | 1ab6075 | 2012-05-23 09:21:54 +0200 | [diff] [blame] | 368 | int hubaddr = 0, hubport = 0; |
| 369 | if (ep->dev->speed < 2) { |
| 370 | /* we need a split transaction */ |
| 371 | if (closest_usb2_hub(ep->dev, &hubaddr, &hubport)) |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 372 | return -1; |
Nico Huber | 1ab6075 | 2012-05-23 09:21:54 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Vincent Palatin | 716375d | 2012-10-25 17:38:43 -0700 | [diff] [blame] | 375 | qtd_t *head = memalign(64, sizeof(qtd_t)); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 376 | qtd_t *cur = head; |
| 377 | while (1) { |
Nico Huber | 4f83d1b | 2012-11-23 11:58:57 +0100 | [diff] [blame] | 378 | memset((void *)cur, 0, sizeof(qtd_t)); |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 379 | cur->token = QTD_ACTIVE | |
| 380 | (pid << QTD_PID_SHIFT) | |
| 381 | (0 << QTD_CERR_SHIFT); |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 382 | u32 chunk = fill_td(cur, data, remaining); |
| 383 | remaining -= chunk; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 384 | data += chunk; |
| 385 | |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 386 | cur->alt_next_qtd = QTD_TERMINATE; |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 387 | if (remaining == 0) { |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 388 | cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 389 | break; |
| 390 | } else { |
Vincent Palatin | 716375d | 2012-10-25 17:38:43 -0700 | [diff] [blame] | 391 | qtd_t *next = memalign(64, sizeof(qtd_t)); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 392 | cur->next_qtd = virt_to_phys(next); |
| 393 | cur = next; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /* create QH */ |
Vincent Palatin | 716375d | 2012-10-25 17:38:43 -0700 | [diff] [blame] | 398 | ehci_qh_t *qh = memalign(64, sizeof(ehci_qh_t)); |
Nico Huber | 4f83d1b | 2012-11-23 11:58:57 +0100 | [diff] [blame] | 399 | memset((void *)qh, 0, sizeof(ehci_qh_t)); |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 400 | qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH; |
| 401 | qh->epchar = ep->dev->address | |
| 402 | (endp << QH_EP_SHIFT) | |
| 403 | (ep->dev->speed << QH_EPS_SHIFT) | |
| 404 | (0 << QH_DTC_SHIFT) | |
| 405 | (1 << QH_RECLAIM_HEAD_SHIFT) | |
| 406 | (ep->maxpacketsize << QH_MPS_SHIFT) | |
| 407 | (0 << QH_NAK_CNT_SHIFT); |
Nico Huber | 1ab6075 | 2012-05-23 09:21:54 +0200 | [diff] [blame] | 408 | qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) | |
| 409 | (hubport << QH_PORT_NUMBER_SHIFT) | |
| 410 | (hubaddr << QH_HUB_ADDRESS_SHIFT); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 411 | |
| 412 | qh->td.next_qtd = virt_to_phys(head); |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 413 | qh->td.token |= (ep->toggle?QTD_TOGGLE_DATA1:0); |
| 414 | head->token |= (ep->toggle?QTD_TOGGLE_DATA1:0); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 415 | |
Nico Huber | 0421dc8 | 2012-05-21 14:53:43 +0200 | [diff] [blame] | 416 | result = ehci_process_async_schedule( |
| 417 | EHCI_INST(ep->dev->controller), qh, head); |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 418 | if (result >= 0) |
| 419 | result = size - result; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 420 | |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 421 | ep->toggle = (cur->token & QTD_TOGGLE_MASK) >> QTD_TOGGLE_SHIFT; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 422 | |
| 423 | free_qh_and_tds(qh, head); |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 424 | |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 425 | return result; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | /* FIXME: Handle control transfers as 3 QHs, so the 2nd stage can be >0x4000 bytes */ |
| 430 | static int ehci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq, |
| 431 | int dalen, u8 *data) |
| 432 | { |
| 433 | int endp = 0; // this is control. always 0 (for now) |
| 434 | int toggle = 0; |
| 435 | int mlen = dev->endpoints[0].maxpacketsize; |
| 436 | int result = 0; |
| 437 | |
Nico Huber | 1ab6075 | 2012-05-23 09:21:54 +0200 | [diff] [blame] | 438 | int hubaddr = 0, hubport = 0, non_hs_ctrl_ep = 0; |
| 439 | if (dev->speed < 2) { |
| 440 | /* we need a split transaction */ |
| 441 | if (closest_usb2_hub(dev, &hubaddr, &hubport)) |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 442 | return -1; |
Nico Huber | 1ab6075 | 2012-05-23 09:21:54 +0200 | [diff] [blame] | 443 | non_hs_ctrl_ep = 1; |
| 444 | } |
| 445 | |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 446 | /* create qTDs */ |
Vincent Palatin | 716375d | 2012-10-25 17:38:43 -0700 | [diff] [blame] | 447 | qtd_t *head = memalign(64, sizeof(qtd_t)); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 448 | qtd_t *cur = head; |
Nico Huber | 4f83d1b | 2012-11-23 11:58:57 +0100 | [diff] [blame] | 449 | memset((void *)cur, 0, sizeof(qtd_t)); |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 450 | cur->token = QTD_ACTIVE | |
| 451 | (toggle?QTD_TOGGLE_DATA1:0) | |
| 452 | (EHCI_SETUP << QTD_PID_SHIFT) | |
| 453 | (3 << QTD_CERR_SHIFT); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 454 | if (fill_td(cur, devreq, drlen) != drlen) { |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 455 | usb_debug("ERROR: couldn't send the entire device request\n"); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 456 | } |
Vincent Palatin | 716375d | 2012-10-25 17:38:43 -0700 | [diff] [blame] | 457 | qtd_t *next = memalign(64, sizeof(qtd_t)); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 458 | cur->next_qtd = virt_to_phys(next); |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 459 | cur->alt_next_qtd = QTD_TERMINATE; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 460 | |
| 461 | /* FIXME: We're limited to 16-20K (depending on alignment) for payload for now. |
| 462 | * Figure out, how toggle can be set sensibly in this scenario */ |
| 463 | if (dalen > 0) { |
| 464 | toggle ^= 1; |
| 465 | cur = next; |
Nico Huber | 4f83d1b | 2012-11-23 11:58:57 +0100 | [diff] [blame] | 466 | memset((void *)cur, 0, sizeof(qtd_t)); |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 467 | cur->token = QTD_ACTIVE | |
| 468 | (toggle?QTD_TOGGLE_DATA1:0) | |
| 469 | (((dir == OUT)?EHCI_OUT:EHCI_IN) << QTD_PID_SHIFT) | |
| 470 | (3 << QTD_CERR_SHIFT); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 471 | if (fill_td(cur, data, dalen) != dalen) { |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 472 | usb_debug("ERROR: couldn't send the entire control payload\n"); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 473 | } |
Vincent Palatin | 716375d | 2012-10-25 17:38:43 -0700 | [diff] [blame] | 474 | next = memalign(64, sizeof(qtd_t)); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 475 | cur->next_qtd = virt_to_phys(next); |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 476 | cur->alt_next_qtd = QTD_TERMINATE; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | toggle = 1; |
| 480 | cur = next; |
Nico Huber | 4f83d1b | 2012-11-23 11:58:57 +0100 | [diff] [blame] | 481 | memset((void *)cur, 0, sizeof(qtd_t)); |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 482 | cur->token = QTD_ACTIVE | |
| 483 | (toggle?QTD_TOGGLE_DATA1:QTD_TOGGLE_DATA0) | |
| 484 | ((dir == OUT)?EHCI_IN:EHCI_OUT) << QTD_PID_SHIFT | |
| 485 | (0 << QTD_CERR_SHIFT); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 486 | fill_td(cur, NULL, 0); |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 487 | cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE; |
| 488 | cur->alt_next_qtd = QTD_TERMINATE; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 489 | |
| 490 | /* create QH */ |
Vincent Palatin | 716375d | 2012-10-25 17:38:43 -0700 | [diff] [blame] | 491 | ehci_qh_t *qh = memalign(64, sizeof(ehci_qh_t)); |
Nico Huber | 4f83d1b | 2012-11-23 11:58:57 +0100 | [diff] [blame] | 492 | memset((void *)qh, 0, sizeof(ehci_qh_t)); |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 493 | qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH; |
| 494 | qh->epchar = dev->address | |
| 495 | (endp << QH_EP_SHIFT) | |
| 496 | (dev->speed << QH_EPS_SHIFT) | |
| 497 | (1 << QH_DTC_SHIFT) | /* ctrl transfers are special: take toggle bit from TD */ |
| 498 | (1 << QH_RECLAIM_HEAD_SHIFT) | |
| 499 | (mlen << QH_MPS_SHIFT) | |
Nico Huber | 1ab6075 | 2012-05-23 09:21:54 +0200 | [diff] [blame] | 500 | (non_hs_ctrl_ep << QH_NON_HS_CTRL_EP_SHIFT) | |
Patrick Georgi | 8fa2787 | 2011-11-24 13:19:57 +0100 | [diff] [blame] | 501 | (0 << QH_NAK_CNT_SHIFT); |
Nico Huber | 1ab6075 | 2012-05-23 09:21:54 +0200 | [diff] [blame] | 502 | qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) | |
| 503 | (hubport << QH_PORT_NUMBER_SHIFT) | |
| 504 | (hubaddr << QH_HUB_ADDRESS_SHIFT); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 505 | qh->td.next_qtd = virt_to_phys(head); |
| 506 | |
Nico Huber | 0421dc8 | 2012-05-21 14:53:43 +0200 | [diff] [blame] | 507 | result = ehci_process_async_schedule( |
| 508 | EHCI_INST(dev->controller), qh, head); |
Julius Werner | e9738db | 2013-02-21 13:41:40 -0800 | [diff] [blame] | 509 | if (result >= 0) |
| 510 | result = dalen - result; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 511 | |
| 512 | free_qh_and_tds(qh, head); |
| 513 | return result; |
| 514 | } |
| 515 | |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 516 | |
| 517 | typedef struct _intr_qtd_t intr_qtd_t; |
| 518 | |
| 519 | struct _intr_qtd_t { |
| 520 | volatile qtd_t td; |
| 521 | u8 *data; |
| 522 | intr_qtd_t *next; |
| 523 | }; |
| 524 | |
| 525 | typedef struct { |
| 526 | volatile ehci_qh_t qh; |
| 527 | intr_qtd_t *head; |
| 528 | intr_qtd_t *tail; |
Nico Huber | 0d120f8 | 2012-06-14 13:08:36 +0200 | [diff] [blame] | 529 | intr_qtd_t *spare; |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 530 | u8 *data; |
| 531 | endpoint_t *endp; |
| 532 | int reqsize; |
| 533 | } intr_queue_t; |
| 534 | |
| 535 | static void fill_intr_queue_td( |
| 536 | intr_queue_t *const intrq, |
| 537 | intr_qtd_t *const intr_qtd, |
| 538 | u8 *const data) |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 539 | { |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 540 | const int pid = (intrq->endp->direction == IN) ? EHCI_IN |
| 541 | : (intrq->endp->direction == OUT) ? EHCI_OUT |
| 542 | : EHCI_SETUP; |
| 543 | const int cerr = (intrq->endp->dev->speed < 2) ? 1 : 0; |
| 544 | |
| 545 | memset(intr_qtd, 0, sizeof(*intr_qtd)); |
| 546 | intr_qtd->td.next_qtd = QTD_TERMINATE; |
| 547 | intr_qtd->td.alt_next_qtd = QTD_TERMINATE; |
| 548 | intr_qtd->td.token = QTD_ACTIVE | |
| 549 | (pid << QTD_PID_SHIFT) | |
| 550 | (cerr << QTD_CERR_SHIFT) | |
| 551 | ((intrq->endp->toggle & 1) << QTD_TOGGLE_SHIFT); |
| 552 | fill_td(&intr_qtd->td, data, intrq->reqsize); |
| 553 | intr_qtd->data = data; |
| 554 | intr_qtd->next = NULL; |
| 555 | |
| 556 | intrq->endp->toggle ^= 1; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 559 | static void ehci_destroy_intr_queue(endpoint_t *const, void *const); |
| 560 | |
| 561 | static void *ehci_create_intr_queue( |
| 562 | endpoint_t *const ep, |
| 563 | const int reqsize, |
| 564 | int reqcount, |
| 565 | const int reqtiming) |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 566 | { |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 567 | int i; |
| 568 | |
| 569 | if ((reqsize > (4 * 4096 + 1)) || /* the maximum for arbitrary aligned |
| 570 | data in five 4096 byte pages */ |
| 571 | (reqtiming > 1024)) |
| 572 | return NULL; |
| 573 | if (reqcount < 2) /* we need at least 2: |
| 574 | one for processing, one for the hc to advance to */ |
| 575 | reqcount = 2; |
| 576 | |
| 577 | int hubaddr = 0, hubport = 0; |
| 578 | if (ep->dev->speed < 2) { |
| 579 | /* we need a split transaction */ |
| 580 | if (closest_usb2_hub(ep->dev, &hubaddr, &hubport)) |
| 581 | return NULL; |
| 582 | } |
| 583 | |
| 584 | intr_queue_t *const intrq = |
Vincent Palatin | 716375d | 2012-10-25 17:38:43 -0700 | [diff] [blame] | 585 | (intr_queue_t *)memalign(64, sizeof(intr_queue_t)); |
Nico Huber | 0d120f8 | 2012-06-14 13:08:36 +0200 | [diff] [blame] | 586 | /* |
| 587 | * reqcount data chunks |
| 588 | * plus one more spare, which we'll leave out of queue |
| 589 | */ |
| 590 | u8 *data = (u8 *)malloc(reqsize * (reqcount + 1)); |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 591 | if (!intrq || !data) |
| 592 | fatal("Not enough memory to create USB interrupt queue.\n"); |
| 593 | intrq->data = data; |
| 594 | intrq->endp = ep; |
| 595 | intrq->reqsize = reqsize; |
| 596 | |
| 597 | /* create #reqcount transfer descriptors (qTDs) */ |
Vincent Palatin | 716375d | 2012-10-25 17:38:43 -0700 | [diff] [blame] | 598 | intrq->head = (intr_qtd_t *)memalign(64, sizeof(intr_qtd_t)); |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 599 | intr_qtd_t *cur_td = intrq->head; |
| 600 | for (i = 0; i < reqcount; ++i) { |
| 601 | fill_intr_queue_td(intrq, cur_td, data); |
| 602 | data += reqsize; |
| 603 | if (i < reqcount - 1) { |
| 604 | /* create one more qTD */ |
| 605 | intr_qtd_t *const next_td = |
Vincent Palatin | 716375d | 2012-10-25 17:38:43 -0700 | [diff] [blame] | 606 | (intr_qtd_t *)memalign(64, sizeof(intr_qtd_t)); |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 607 | cur_td->td.next_qtd = virt_to_phys(&next_td->td); |
| 608 | cur_td->next = next_td; |
| 609 | cur_td = next_td; |
| 610 | } |
| 611 | } |
| 612 | intrq->tail = cur_td; |
| 613 | |
Nico Huber | 0d120f8 | 2012-06-14 13:08:36 +0200 | [diff] [blame] | 614 | /* create spare qTD */ |
Vincent Palatin | 716375d | 2012-10-25 17:38:43 -0700 | [diff] [blame] | 615 | intrq->spare = (intr_qtd_t *)memalign(64, sizeof(intr_qtd_t)); |
Nico Huber | 0d120f8 | 2012-06-14 13:08:36 +0200 | [diff] [blame] | 616 | fill_intr_queue_td(intrq, intrq->spare, data); |
| 617 | |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 618 | /* initialize QH */ |
| 619 | const int endp = ep->endpoint & 0xf; |
Nico Huber | 4f83d1b | 2012-11-23 11:58:57 +0100 | [diff] [blame] | 620 | memset((void *)&intrq->qh, 0, sizeof(intrq->qh)); |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 621 | intrq->qh.horiz_link_ptr = PS_TERMINATE; |
| 622 | intrq->qh.epchar = ep->dev->address | |
| 623 | (endp << QH_EP_SHIFT) | |
| 624 | (ep->dev->speed << QH_EPS_SHIFT) | |
| 625 | (1 << QH_DTC_SHIFT) | |
| 626 | (0 << QH_RECLAIM_HEAD_SHIFT) | |
| 627 | (ep->maxpacketsize << QH_MPS_SHIFT) | |
| 628 | (0 << QH_NAK_CNT_SHIFT); |
| 629 | intrq->qh.epcaps = (1 << QH_PIPE_MULTIPLIER_SHIFT) | |
| 630 | (hubport << QH_PORT_NUMBER_SHIFT) | |
| 631 | (hubaddr << QH_HUB_ADDRESS_SHIFT) | |
| 632 | (0xfe << QH_UFRAME_CMASK_SHIFT) | |
| 633 | 1 /* uFrame S-mask */; |
| 634 | intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td); |
| 635 | |
| 636 | /* insert QH into periodic schedule */ |
| 637 | int nothing_placed = 1; |
| 638 | u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(ep->dev->controller) |
| 639 | ->operation->periodiclistbase); |
Nico Huber | 3ca35ca | 2012-06-14 13:27:39 +0200 | [diff] [blame] | 640 | const u32 dummy_ptr = virt_to_phys(EHCI_INST( |
| 641 | ep->dev->controller)->dummy_qh) | PS_TYPE_QH; |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 642 | for (i = 0; i < 1024; i += reqtiming) { |
| 643 | /* advance to the next free position */ |
Nico Huber | 3ca35ca | 2012-06-14 13:27:39 +0200 | [diff] [blame] | 644 | while ((i < 1024) && (ps[i] != dummy_ptr)) ++i; |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 645 | if (i < 1024) { |
| 646 | ps[i] = virt_to_phys(&intrq->qh) | PS_TYPE_QH; |
| 647 | nothing_placed = 0; |
| 648 | } |
| 649 | } |
| 650 | if (nothing_placed) { |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 651 | usb_debug("Error: Failed to place ehci interrupt queue head " |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 652 | "into periodic schedule: no space left\n"); |
| 653 | ehci_destroy_intr_queue(ep, intrq); |
| 654 | return NULL; |
| 655 | } |
| 656 | |
| 657 | return intrq; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 660 | static void ehci_destroy_intr_queue(endpoint_t *const ep, void *const queue) |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 661 | { |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 662 | intr_queue_t *const intrq = (intr_queue_t *)queue; |
| 663 | |
| 664 | /* remove QH from periodic schedule */ |
| 665 | int i; |
| 666 | u32 *const ps = (u32 *)phys_to_virt(EHCI_INST( |
| 667 | ep->dev->controller)->operation->periodiclistbase); |
Nico Huber | 3ca35ca | 2012-06-14 13:27:39 +0200 | [diff] [blame] | 668 | const u32 dummy_ptr = virt_to_phys(EHCI_INST( |
| 669 | ep->dev->controller)->dummy_qh) | PS_TYPE_QH; |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 670 | for (i = 0; i < 1024; ++i) { |
| 671 | if ((ps[i] & PS_PTR_MASK) == virt_to_phys(&intrq->qh)) |
Nico Huber | 3ca35ca | 2012-06-14 13:27:39 +0200 | [diff] [blame] | 672 | ps[i] = dummy_ptr; |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* wait 1ms for frame to end */ |
| 676 | mdelay(1); |
| 677 | |
| 678 | while (intrq->head) { |
| 679 | /* disable qTD and destroy list */ |
| 680 | intrq->head->td.next_qtd = QTD_TERMINATE; |
| 681 | intrq->head->td.token &= ~QTD_ACTIVE; |
| 682 | |
| 683 | /* save and advance head ptr */ |
| 684 | intr_qtd_t *const to_free = intrq->head; |
| 685 | intrq->head = intrq->head->next; |
| 686 | |
| 687 | /* free current interrupt qTD */ |
| 688 | free(to_free); |
| 689 | } |
Nico Huber | 0d120f8 | 2012-06-14 13:08:36 +0200 | [diff] [blame] | 690 | free(intrq->spare); |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 691 | free(intrq->data); |
| 692 | free(intrq); |
| 693 | } |
| 694 | |
| 695 | static u8 *ehci_poll_intr_queue(void *const queue) |
| 696 | { |
| 697 | intr_queue_t *const intrq = (intr_queue_t *)queue; |
| 698 | |
| 699 | u8 *ret = NULL; |
| 700 | |
| 701 | /* process if head qTD is inactive AND QH has been moved forward */ |
Nico Huber | 0d120f8 | 2012-06-14 13:08:36 +0200 | [diff] [blame] | 702 | if (!(intrq->head->td.token & QTD_ACTIVE)) { |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 703 | if (!(intrq->head->td.token & QTD_STATUS_MASK)) |
| 704 | ret = intrq->head->data; |
| 705 | else |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 706 | usb_debug("ehci_poll_intr_queue: transfer failed, " |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 707 | "status == 0x%02x\n", |
| 708 | intrq->head->td.token & QTD_STATUS_MASK); |
| 709 | |
Nico Huber | 0d120f8 | 2012-06-14 13:08:36 +0200 | [diff] [blame] | 710 | /* insert spare qTD at the end and advance our tail ptr */ |
| 711 | fill_intr_queue_td(intrq, intrq->spare, intrq->spare->data); |
| 712 | intrq->tail->td.next_qtd = virt_to_phys(&intrq->spare->td); |
| 713 | intrq->tail->next = intrq->spare; |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 714 | intrq->tail = intrq->tail->next; |
Nico Huber | 0d120f8 | 2012-06-14 13:08:36 +0200 | [diff] [blame] | 715 | |
| 716 | /* reuse executed qTD as spare one and advance our head ptr */ |
| 717 | intrq->spare = intrq->head; |
| 718 | intrq->head = intrq->head->next; |
| 719 | } |
| 720 | /* reset queue if we fully processed it after underrun */ |
Nico Huber | af169f4 | 2012-11-22 11:14:03 +0100 | [diff] [blame] | 721 | else if ((intrq->qh.td.next_qtd & QTD_TERMINATE) && |
| 722 | /* to prevent race conditions: |
| 723 | not our head and not active */ |
| 724 | (intrq->qh.current_td_ptr != |
| 725 | virt_to_phys(&intrq->head->td)) && |
| 726 | !(intrq->qh.td.token & QTD_ACTIVE)) { |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 727 | usb_debug("resetting underrun ehci interrupt queue.\n"); |
Nico Huber | af169f4 | 2012-11-22 11:14:03 +0100 | [diff] [blame] | 728 | intrq->qh.current_td_ptr = 0; |
Nico Huber | 4f83d1b | 2012-11-23 11:58:57 +0100 | [diff] [blame] | 729 | memset((void *)&intrq->qh.td, 0, sizeof(intrq->qh.td)); |
Nico Huber | 0d120f8 | 2012-06-14 13:08:36 +0200 | [diff] [blame] | 730 | intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td); |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 731 | } |
| 732 | return ret; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | hci_t * |
Nico Huber | 6e23066 | 2014-07-07 16:33:59 +0200 | [diff] [blame] | 736 | ehci_init (unsigned long physical_bar) |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 737 | { |
| 738 | int i; |
| 739 | hci_t *controller = new_controller (); |
| 740 | |
| 741 | if (!controller) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 742 | fatal("Could not create USB controller instance.\n"); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 743 | |
| 744 | controller->instance = malloc (sizeof (ehci_t)); |
| 745 | if(!controller->instance) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 746 | fatal("Not enough memory creating USB controller instance.\n"); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 747 | |
Anton Kochkov | 1c36ead | 2012-06-28 08:30:15 +0400 | [diff] [blame] | 748 | controller->type = EHCI; |
| 749 | |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 750 | controller->start = ehci_start; |
| 751 | controller->stop = ehci_stop; |
| 752 | controller->reset = ehci_reset; |
Nico Huber | 6e711c6 | 2012-11-12 16:20:32 +0100 | [diff] [blame] | 753 | controller->init = ehci_reinit; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 754 | controller->shutdown = ehci_shutdown; |
| 755 | controller->bulk = ehci_bulk; |
| 756 | controller->control = ehci_control; |
Patrick Georgi | 482af6d | 2013-05-24 15:48:56 +0200 | [diff] [blame] | 757 | controller->set_address = generic_set_address; |
| 758 | controller->finish_device_config = NULL; |
| 759 | controller->destroy_device = NULL; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 760 | controller->create_intr_queue = ehci_create_intr_queue; |
| 761 | controller->destroy_intr_queue = ehci_destroy_intr_queue; |
| 762 | controller->poll_intr_queue = ehci_poll_intr_queue; |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 763 | for (i = 0; i < 128; i++) { |
| 764 | controller->devices[i] = 0; |
| 765 | } |
| 766 | init_device_entry (controller, 0); |
| 767 | |
Nico Huber | 5b9e6f1 | 2014-07-10 12:56:34 +0200 | [diff] [blame] | 768 | EHCI_INST(controller)->capabilities = phys_to_virt(physical_bar); |
| 769 | EHCI_INST(controller)->operation = (hc_op_t *)(phys_to_virt(physical_bar) + EHCI_INST(controller)->capabilities->caplength); |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 770 | |
Vincent Palatin | 716375d | 2012-10-25 17:38:43 -0700 | [diff] [blame] | 771 | /* Set the high address word (aka segment) if controller is 64-bit */ |
| 772 | if (EHCI_INST(controller)->capabilities->hccparams & 1) |
| 773 | EHCI_INST(controller)->operation->ctrldssegment = 0; |
| 774 | |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 775 | /* Enable operation of controller */ |
| 776 | controller->start(controller); |
| 777 | |
| 778 | /* take over all ports. USB1 should be blind now */ |
| 779 | EHCI_INST(controller)->operation->configflag = 1; |
| 780 | |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 781 | /* Initialize periodic frame list */ |
| 782 | /* 1024 32-bit pointers, 4kb aligned */ |
| 783 | u32 *const periodic_list = (u32 *)memalign(4096, 1024 * sizeof(u32)); |
| 784 | if (!periodic_list) |
| 785 | fatal("Not enough memory creating EHCI periodic frame list.\n"); |
Nico Huber | 3ca35ca | 2012-06-14 13:27:39 +0200 | [diff] [blame] | 786 | |
| 787 | /* |
| 788 | * Insert dummy QH in periodic frame list |
| 789 | * This helps with broken host controllers |
| 790 | * and doesn't violate the standard. |
| 791 | */ |
Vincent Palatin | 716375d | 2012-10-25 17:38:43 -0700 | [diff] [blame] | 792 | EHCI_INST(controller)->dummy_qh = (ehci_qh_t *)memalign(64, sizeof(ehci_qh_t)); |
Nico Huber | 4f83d1b | 2012-11-23 11:58:57 +0100 | [diff] [blame] | 793 | memset((void *)EHCI_INST(controller)->dummy_qh, 0, |
Nico Huber | 3ca35ca | 2012-06-14 13:27:39 +0200 | [diff] [blame] | 794 | sizeof(*EHCI_INST(controller)->dummy_qh)); |
| 795 | EHCI_INST(controller)->dummy_qh->horiz_link_ptr = QH_TERMINATE; |
Julius Werner | 4610f0e | 2013-08-22 16:24:09 -0700 | [diff] [blame^] | 796 | EHCI_INST(controller)->dummy_qh->td.next_qtd = QH_TERMINATE; |
| 797 | EHCI_INST(controller)->dummy_qh->td.alt_next_qtd = QH_TERMINATE; |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 798 | for (i = 0; i < 1024; ++i) |
Nico Huber | 3ca35ca | 2012-06-14 13:27:39 +0200 | [diff] [blame] | 799 | periodic_list[i] = virt_to_phys(EHCI_INST(controller)->dummy_qh) |
| 800 | | PS_TYPE_QH; |
Nico Huber | 62eb5b3 | 2012-05-25 10:09:13 +0200 | [diff] [blame] | 801 | |
| 802 | /* Make sure periodic schedule is disabled */ |
| 803 | ehci_set_periodic_schedule(EHCI_INST(controller), 0); |
| 804 | /* Set periodic frame list pointer */ |
| 805 | EHCI_INST(controller)->operation->periodiclistbase = |
| 806 | virt_to_phys(periodic_list); |
| 807 | /* Enable use of periodic schedule */ |
| 808 | ehci_set_periodic_schedule(EHCI_INST(controller), 1); |
| 809 | |
Patrick Georgi | 7f43dc1 | 2010-09-25 17:01:13 +0000 | [diff] [blame] | 810 | /* TODO lots of stuff missing */ |
| 811 | |
| 812 | controller->devices[0]->controller = controller; |
| 813 | controller->devices[0]->init = ehci_rh_init; |
| 814 | controller->devices[0]->init (controller->devices[0]); |
| 815 | |
| 816 | return controller; |
| 817 | } |
Stefan Reinauer | 8992e53 | 2013-05-02 16:16:41 -0700 | [diff] [blame] | 818 | |
Gabe Black | 1ee2c6d | 2013-08-09 04:27:35 -0700 | [diff] [blame] | 819 | #ifdef CONFIG_LP_USB_PCI |
Stefan Reinauer | 8992e53 | 2013-05-02 16:16:41 -0700 | [diff] [blame] | 820 | hci_t * |
| 821 | ehci_pci_init (pcidev_t addr) |
| 822 | { |
| 823 | hci_t *controller; |
| 824 | u32 reg_base; |
| 825 | |
| 826 | u32 pci_command = pci_read_config32(addr, PCI_COMMAND); |
| 827 | pci_command = (pci_command | PCI_COMMAND_MEMORY) & ~PCI_COMMAND_IO ; |
| 828 | pci_write_config32(addr, PCI_COMMAND, pci_command); |
| 829 | |
| 830 | reg_base = pci_read_config32 (addr, USBBASE); |
| 831 | |
| 832 | /* default value for frame length adjust */ |
| 833 | pci_write_config8(addr, FLADJ, FLADJ_framelength(60000)); |
| 834 | |
Nico Huber | 6e23066 | 2014-07-07 16:33:59 +0200 | [diff] [blame] | 835 | controller = ehci_init((unsigned long)reg_base); |
Stefan Reinauer | 8992e53 | 2013-05-02 16:16:41 -0700 | [diff] [blame] | 836 | |
| 837 | return controller; |
| 838 | } |
| 839 | #endif |