blob: 4636e7cc0348277db608faa9fabecfd54eea6f97 [file] [log] [blame]
Patrick Georgi7f43dc12010-09-25 17:01:13 +00001/*
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 Reinauerd5c79f92013-03-25 15:56:08 -070030//#define USB_DEBUG
31
Patrick Georgi7f43dc12010-09-25 17:01:13 +000032#include <libpayload.h>
Furquan Shaikh5b1c3282014-08-24 23:07:43 -070033#include <arch/barrier.h>
Julius Werner509c37e2013-08-28 12:29:28 -070034#include <arch/cache.h>
Patrick Georgi7f43dc12010-09-25 17:01:13 +000035#include "ehci.h"
36#include "ehci_private.h"
37
Stefan Reinauer528b43d2011-04-14 19:52:04 +000038static void dump_td(u32 addr)
39{
Patrick Georgi7f43dc12010-09-25 17:01:13 +000040 qtd_t *td = phys_to_virt(addr);
Anton Kochkovc07fb152012-12-19 12:32:32 +040041 usb_debug("+---------------------------------------------------+\n");
42 if (((td->token & (3UL << 8)) >> 8) == 2)
43 usb_debug("|..[SETUP]..........................................|\n");
44 else if (((td->token & (3UL << 8)) >> 8) == 1)
45 usb_debug("|..[IN].............................................|\n");
46 else if (((td->token & (3UL << 8)) >> 8) == 0)
47 usb_debug("|..[OUT]............................................|\n");
48 else
49 usb_debug("|..[]...............................................|\n");
50 usb_debug("|:|============ EHCI TD at [0x%08lx] ==========|:|\n", addr);
51 usb_debug("|:| ERRORS = [%ld] | TOKEN = [0x%08lx] | |:|\n",
52 3 - ((td->token & QTD_CERR_MASK) >> QTD_CERR_SHIFT), td->token);
53 usb_debug("|:+-----------------------------------------------+:|\n");
54 usb_debug("|:| Next qTD [0x%08lx] |:|\n", td->next_qtd);
55 usb_debug("|:+-----------------------------------------------+:|\n");
56 usb_debug("|:| Alt. Next qTD [0x%08lx] |:|\n", td->alt_next_qtd);
57 usb_debug("|:+-----------------------------------------------+:|\n");
Julius Werner8e7a9e12013-10-29 11:31:40 -070058 usb_debug("|:| | Bytes to Transfer |[%05ld] |:|\n", (td->token & QTD_TOTAL_LEN_MASK) >> 16);
Anton Kochkovc07fb152012-12-19 12:32:32 +040059 usb_debug("|:| | PID CODE: | [%ld] |:|\n", (td->token & (3UL << 8)) >> 8);
60 usb_debug("|:| | Interrupt On Complete (IOC) | [%ld] |:|\n", (td->token & (1UL << 15)) >> 15);
61 usb_debug("|:| | Status Active | [%ld] |:|\n", (td->token & (1UL << 7)) >> 7);
62 usb_debug("|:| | Status Halted | [%ld] |:|\n", (td->token & (1UL << 6)) >> 6);
63 usb_debug("|:| TOKEN | Status Data Buffer Error | [%ld] |:|\n", (td->token & (1UL << 5)) >> 5);
64 usb_debug("|:| | Status Babble detected | [%ld] |:|\n", (td->token & (1UL << 4)) >> 4);
65 usb_debug("|:| | Status Transaction Error | [%ld] |:|\n", (td->token & (1UL << 3)) >> 3);
66 usb_debug("|:| | Status Missed Micro Frame | [%ld] |:|\n", (td->token & (1UL << 2)) >> 2);
67 usb_debug("|:| | Split Transaction State | [%ld] |:|\n", (td->token & (1UL << 1)) >> 1);
68 usb_debug("|:| | Ping State | [%ld] |:|\n", td->token & 1UL);
69 usb_debug("|:|-----------------------------------------------|:|\n");
70 usb_debug("|...................................................|\n");
71 usb_debug("+---------------------------------------------------+\n");
72}
73
Stefan Reinauer8992e532013-05-02 16:16:41 -070074#if 0 && defined(USB_DEBUG)
Anton Kochkovc07fb152012-12-19 12:32:32 +040075static void dump_qh(ehci_qh_t *cur)
76{
77 qtd_t *tmp_qtd = NULL;
78 usb_debug("+===================================================+\n");
79 usb_debug("| ############# EHCI QH at [0x%08lx] ########### |\n", virt_to_phys(cur));
80 usb_debug("+---------------------------------------------------+\n");
81 usb_debug("| Horizonal Link Pointer [0x%08lx] |\n", cur->horiz_link_ptr);
82 usb_debug("+------------------[ 0x%08lx ]-------------------+\n", cur->epchar);
83 usb_debug("| | Maximum Packet Length | [%04ld] |\n", ((cur->epchar & (0x7ffUL << 16)) >> 16));
84 usb_debug("| | Device Address | [%ld] |\n", cur->epchar & 0x7F);
85 usb_debug("| | Inactivate on Next Transaction | [%ld] |\n", ((cur->epchar & (1UL << 7)) >> 7));
86 usb_debug("| | Endpoint Number | [%ld] |\n", ((cur->epchar & (0xFUL << 8)) >> 8));
87 usb_debug("| EPCHAR | Endpoint Speed | [%ld] |\n", ((cur->epchar & (3UL << 12)) >> 12));
88 usb_debug("| | Data Toggle Control | [%ld] |\n", ((cur->epchar & (1UL << 14)) >> 14));
89 usb_debug("| | Head of Reclamation List Flag | [%ld] |\n", ((cur->epchar & (1UL << 15)) >> 15));
90 usb_debug("| | Control Endpoint Flag | [%ld] |\n", ((cur->epchar & (1UL << 27)) >> 27));
91 usb_debug("| | Nak Count Reload | [%ld] |\n", ((cur->epchar & (0xFUL << 28)) >> 28));
92 if (((cur->epchar & (1UL << QH_NON_HS_CTRL_EP_SHIFT)) >> QH_NON_HS_CTRL_EP_SHIFT) == 1) { /* Split transaction */
93 usb_debug("+--------+---------[ 0x%08lx ]----------+--------+\n", cur->epcaps);
94 usb_debug("| | Hub Port | [%ld] |\n", ((cur->epcaps & (0x7FUL << 23)) >> 23)); /* [29:23] */
95 usb_debug("| | Hub Address | [%ld] |\n", ((cur->epcaps & (0x7FUL << 16)) >> 16)); /* [22:16] */
96 }
97 usb_debug("+---------------------------------------------------+\n");
98 usb_debug("| Current QTD [0x%08lx] |\n", cur->current_td_ptr);
99
100 if (!((cur->horiz_link_ptr == 0) && (cur->epchar == 0))) {
101 /* Dump overlay QTD for this QH */
102 usb_debug("+---------------------------------------------------+\n");
103 usb_debug("|::::::::::::::::::: QTD OVERLAY :::::::::::::::::::|\n");
104 dump_td(virt_to_phys((void *)&(cur->td)));
105 /* Dump all TD tree for this QH */
106 tmp_qtd = (qtd_t *)phys_to_virt((cur->td.next_qtd & ~0x1FUL));
107 if (tmp_qtd != NULL)
108 usb_debug("|:::::::::::::::::: EHCI QTD CHAIN :::::::::::::::::|\n");
109 while (tmp_qtd != NULL)
110 {
111 dump_td(virt_to_phys(tmp_qtd));
112 tmp_qtd = (qtd_t *)phys_to_virt((tmp_qtd->next_qtd & ~0x1FUL));
113 }
114 usb_debug("|:::::::::::::::: EOF EHCI QTD CHAIN :::::::::::::::|\n");
115 usb_debug("+---------------------------------------------------+\n");
116 } else {
117 usb_debug("+---------------------------------------------------+\n");
118 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000119}
Stefan Reinauerd5c79f92013-03-25 15:56:08 -0700120#endif
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000121
122static void ehci_start (hci_t *controller)
123{
Patrick Georgi8fa27872011-11-24 13:19:57 +0100124 EHCI_INST(controller)->operation->usbcmd |= HC_OP_RS;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000125}
126
127static void ehci_stop (hci_t *controller)
128{
Patrick Georgi8fa27872011-11-24 13:19:57 +0100129 EHCI_INST(controller)->operation->usbcmd &= ~HC_OP_RS;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000130}
131
132static void ehci_reset (hci_t *controller)
133{
Anton Kochkov2fef58e2012-11-09 14:06:05 +0100134 short count = 0;
135 ehci_stop(controller);
136 /* wait 10 ms just to be shure */
137 mdelay(10);
138 if (EHCI_INST(controller)->operation->usbsts & HC_OP_HC_HALTED) {
139 EHCI_INST(controller)->operation->usbcmd = HC_OP_HC_RESET;
140 /* wait 100 ms */
141 for (count = 0; count < 10; count++) {
142 mdelay(10);
143 if (!(EHCI_INST(controller)->operation->usbcmd & HC_OP_HC_RESET)) {
144 return;
145 }
146 }
147 }
148 usb_debug("ehci_reset(): reset failed!\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000149}
150
Nico Huber6e711c62012-11-12 16:20:32 +0100151static void ehci_reinit (hci_t *controller)
152{
153}
154
Nico Huber62eb5b32012-05-25 10:09:13 +0200155static int ehci_set_periodic_schedule(ehci_t *ehcic, int enable)
156{
157 /* Set periodic schedule status. */
158 if (enable)
159 ehcic->operation->usbcmd |= HC_OP_PERIODIC_SCHED_EN;
160 else
161 ehcic->operation->usbcmd &= ~HC_OP_PERIODIC_SCHED_EN;
162 /* Wait for the controller to accept periodic schedule status.
163 * This shouldn't take too long, but we should timeout nevertheless.
164 */
165 enable = enable ? HC_OP_PERIODIC_SCHED_STAT : 0;
Gabe Blackef650a52012-11-21 02:16:13 -0800166 int timeout = 100000; /* time out after 100ms */
Nico Huber62eb5b32012-05-25 10:09:13 +0200167 while (((ehcic->operation->usbsts & HC_OP_PERIODIC_SCHED_STAT) != enable)
168 && timeout--)
Gabe Blackef650a52012-11-21 02:16:13 -0800169 udelay(1);
Nico Huber62eb5b32012-05-25 10:09:13 +0200170 if (timeout < 0) {
Gabe Black93ded592012-11-01 15:44:10 -0700171 usb_debug("ehci periodic schedule status change timed out.\n");
Nico Huber62eb5b32012-05-25 10:09:13 +0200172 return 1;
173 }
174 return 0;
175}
176
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000177static void ehci_shutdown (hci_t *controller)
178{
Mathias Krause7a9da712013-03-24 19:40:01 +0100179 detach_controller(controller);
Julius Werner7234d602014-04-08 12:54:25 -0700180
Nico Huber62eb5b32012-05-25 10:09:13 +0200181 /* Make sure periodic schedule is disabled */
182 ehci_set_periodic_schedule(EHCI_INST(controller), 0);
Nico Huber62eb5b32012-05-25 10:09:13 +0200183
Julius Werner7234d602014-04-08 12:54:25 -0700184 /* Give all ports back to companion controller */
Patrick Georgi01178bb2011-11-04 11:57:46 +0100185 EHCI_INST(controller)->operation->configflag = 0;
Nico Huberd633dda2012-11-12 15:35:44 +0100186
Julius Werner7234d602014-04-08 12:54:25 -0700187 /* Free all dynamic allocations */
188 free(EHCI_INST(controller)->dma_buffer);
189 free(phys_to_virt(EHCI_INST(controller)->operation->periodiclistbase));
190 free((void *)EHCI_INST(controller)->dummy_qh);
Nico Huberd633dda2012-11-12 15:35:44 +0100191 free(EHCI_INST(controller));
192 free(controller);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000193}
194
195enum { EHCI_OUT=0, EHCI_IN=1, EHCI_SETUP=2 };
196
Patrick Georgi8fa27872011-11-24 13:19:57 +0100197/* returns handled bytes. assumes that the fields it writes are empty on entry */
198static int fill_td(qtd_t *td, void* data, int datalen)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000199{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000200 u32 total_len = 0;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100201 u32 page_no = 0;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000202
203 u32 start = virt_to_phys(data);
204 u32 page = start & ~4095;
205 u32 offset = start & 4095;
206 u32 page_len = 4096 - offset;
207
Patrick Georgi8fa27872011-11-24 13:19:57 +0100208 td->token |= 0 << QTD_CPAGE_SHIFT;
209 td->bufptrs[page_no++] = start;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000210
211 if (datalen <= page_len) {
212 total_len = datalen;
213 } else {
214 datalen -= page_len;
215 total_len += page_len;
216
Patrick Georgi8fa27872011-11-24 13:19:57 +0100217 while (page_no < 5) {
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000218 /* we have a continguous mapping between virtual and physical memory */
219 page += 4096;
220
Patrick Georgi8fa27872011-11-24 13:19:57 +0100221 td->bufptrs[page_no++] = page;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000222 if (datalen <= 4096) {
223 total_len += datalen;
224 break;
225 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000226 datalen -= 4096;
227 total_len += 4096;
Julius Werner69eea7c2013-01-11 16:25:52 -0800228
229 /* end TD at a packet boundary if transfer not complete */
230 if (page_no == 5)
231 total_len &= ~511;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100232 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000233 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100234 td->token |= total_len << QTD_TOTAL_LEN_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000235 return total_len;
236}
237
238/* free up data structures */
Patrick Georgi8fa27872011-11-24 13:19:57 +0100239static void free_qh_and_tds(ehci_qh_t *qh, qtd_t *cur)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000240{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000241 qtd_t *next;
242 while (cur) {
243 next = (qtd_t*)phys_to_virt(cur->next_qtd & ~31);
Nico Huber4f83d1b2012-11-23 11:58:57 +0100244 free((void *)cur);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000245 cur = next;
246 }
Nico Huber4f83d1b2012-11-23 11:58:57 +0100247 free((void *)qh);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000248}
249
Patrick Georgi8fa27872011-11-24 13:19:57 +0100250static int wait_for_tds(qtd_t *head)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000251{
Julius Wernere9738db2013-02-21 13:41:40 -0800252 /* returns the amount of bytes *not* transmitted, or -1 for error */
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000253 int result = 0;
254 qtd_t *cur = head;
255 while (1) {
256 if (0) dump_td(virt_to_phys(cur));
Nico Huber0421dc82012-05-21 14:53:43 +0200257
258 /* wait for results */
Nico Hubercef86922012-06-01 08:54:29 +0200259 /* how long to wait?
260 * tested with some USB2.0 flash sticks:
261 * TUR turn around took
Julius Werner52e3f442013-07-23 12:45:18 -0700262 * about 2.2s for the slowest (13fe:3800)
Nico Hubercef86922012-06-01 08:54:29 +0200263 * max. 250ms for the others
264 * slowest non-TUR turn around took about 1.3s
Julius Werner52e3f442013-07-23 12:45:18 -0700265 * set to 3s to be safe as a failed TUR can be fatal
Nico Huber0421dc82012-05-21 14:53:43 +0200266 */
Julius Werner52e3f442013-07-23 12:45:18 -0700267 int timeout = 60000; /* time out after 60000 * 50us == 3s */
Nico Huber0421dc82012-05-21 14:53:43 +0200268 while ((cur->token & QTD_ACTIVE) && !(cur->token & QTD_HALTED)
269 && timeout--)
270 udelay(50);
271 if (timeout < 0) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700272 usb_debug("Error: ehci: queue transfer "
Nico Huber0421dc82012-05-21 14:53:43 +0200273 "processing timed out.\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800274 return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200275 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100276 if (cur->token & QTD_HALTED) {
Nico Huber86c686a2012-11-21 16:22:26 +0100277 usb_debug("ERROR with packet\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000278 dump_td(virt_to_phys(cur));
Gabe Black93ded592012-11-01 15:44:10 -0700279 usb_debug("-----------------\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800280 return -1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000281 }
Julius Wernere9738db2013-02-21 13:41:40 -0800282 result += (cur->token & QTD_TOTAL_LEN_MASK)
283 >> QTD_TOTAL_LEN_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000284 if (cur->next_qtd & 1) {
Julius Wernere9738db2013-02-21 13:41:40 -0800285 break;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000286 }
287 if (0) dump_td(virt_to_phys(cur));
288 /* helps debugging the TD chain */
Gabe Black93ded592012-11-01 15:44:10 -0700289 if (0) usb_debug("\nmoving from %x to %x\n", cur, phys_to_virt(cur->next_qtd));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000290 cur = phys_to_virt(cur->next_qtd);
291 }
292 return result;
293}
294
Nico Huber0421dc82012-05-21 14:53:43 +0200295static int ehci_set_async_schedule(ehci_t *ehcic, int enable)
296{
Furquan Shaikh5b1c3282014-08-24 23:07:43 -0700297
298 /* Memory barrier to ensure that all memory accesses before we set the
299 * async schedule are complete. It was observed especially in the case of
300 * arm64, that netboot and usb stuff resulted in lots of errors possibly
301 * due to CPU reordering. Hence, enforcing strict CPU ordering.
302 */
303 mb();
304
Nico Huber0421dc82012-05-21 14:53:43 +0200305 /* Set async schedule status. */
306 if (enable)
307 ehcic->operation->usbcmd |= HC_OP_ASYNC_SCHED_EN;
308 else
309 ehcic->operation->usbcmd &= ~HC_OP_ASYNC_SCHED_EN;
310 /* Wait for the controller to accept async schedule status.
311 * This shouldn't take too long, but we should timeout nevertheless.
312 */
313 enable = enable ? HC_OP_ASYNC_SCHED_STAT : 0;
314 int timeout = 100; /* time out after 100ms */
315 while (((ehcic->operation->usbsts & HC_OP_ASYNC_SCHED_STAT) != enable)
316 && timeout--)
317 mdelay(1);
318 if (timeout < 0) {
Gabe Black93ded592012-11-01 15:44:10 -0700319 usb_debug("ehci async schedule status change timed out.\n");
Nico Huber0421dc82012-05-21 14:53:43 +0200320 return 1;
321 }
322 return 0;
323}
324
325static int ehci_process_async_schedule(
326 ehci_t *ehcic, ehci_qh_t *qhead, qtd_t *head)
327{
328 int result;
329
330 /* make sure async schedule is disabled */
Julius Wernere9738db2013-02-21 13:41:40 -0800331 if (ehci_set_async_schedule(ehcic, 0)) return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200332
333 /* hook up QH */
334 ehcic->operation->asynclistaddr = virt_to_phys(qhead);
335
336 /* start async schedule */
Julius Wernere9738db2013-02-21 13:41:40 -0800337 if (ehci_set_async_schedule(ehcic, 1)) return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200338
339 /* wait for result */
340 result = wait_for_tds(head);
341
342 /* disable async schedule */
343 ehci_set_async_schedule(ehcic, 0);
344
345 return result;
346}
347
Julius Werner509c37e2013-08-28 12:29:28 -0700348static int ehci_bulk (endpoint_t *ep, int size, u8 *src, int finalize)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000349{
350 int result = 0;
Julius Werner509c37e2013-08-28 12:29:28 -0700351 u8 *end = src + size;
Julius Wernere9738db2013-02-21 13:41:40 -0800352 int remaining = size;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000353 int endp = ep->endpoint & 0xf;
354 int pid = (ep->direction==IN)?EHCI_IN:EHCI_OUT;
355
Nico Huber1ab60752012-05-23 09:21:54 +0200356 int hubaddr = 0, hubport = 0;
357 if (ep->dev->speed < 2) {
358 /* we need a split transaction */
359 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
Julius Wernere9738db2013-02-21 13:41:40 -0800360 return -1;
Nico Huber1ab60752012-05-23 09:21:54 +0200361 }
362
Julius Werner509c37e2013-08-28 12:29:28 -0700363 if (!dma_coherent(src)) {
364 end = EHCI_INST(ep->dev->controller)->dma_buffer + size;
365 if (size > DMA_SIZE) {
366 usb_debug("EHCI bulk transfer too large for DMA buffer: %d\n", size);
367 return -1;
368 }
369 if (pid == EHCI_OUT)
370 memcpy(end - size, src, size);
371 }
372
373 ehci_qh_t *qh = dma_memalign(64, sizeof(ehci_qh_t));
374 qtd_t *head = dma_memalign(64, sizeof(qtd_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000375 qtd_t *cur = head;
Julius Werner509c37e2013-08-28 12:29:28 -0700376 if (!qh || !head)
377 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000378 while (1) {
Nico Huber4f83d1b2012-11-23 11:58:57 +0100379 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100380 cur->token = QTD_ACTIVE |
381 (pid << QTD_PID_SHIFT) |
382 (0 << QTD_CERR_SHIFT);
Julius Werner509c37e2013-08-28 12:29:28 -0700383 remaining -= fill_td(cur, end - remaining, remaining);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000384
Patrick Georgi8fa27872011-11-24 13:19:57 +0100385 cur->alt_next_qtd = QTD_TERMINATE;
Julius Werner509c37e2013-08-28 12:29:28 -0700386 if (remaining <= 0) {
Patrick Georgi8fa27872011-11-24 13:19:57 +0100387 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000388 break;
389 } else {
Julius Werner509c37e2013-08-28 12:29:28 -0700390 qtd_t *next = dma_memalign(64, sizeof(qtd_t));
391 if (!next)
392 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000393 cur->next_qtd = virt_to_phys(next);
394 cur = next;
395 }
396 }
397
398 /* create QH */
Nico Huber4f83d1b2012-11-23 11:58:57 +0100399 memset((void *)qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100400 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 Huber1ab60752012-05-23 09:21:54 +0200408 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
409 (hubport << QH_PORT_NUMBER_SHIFT) |
410 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000411
412 qh->td.next_qtd = virt_to_phys(head);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100413 qh->td.token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
414 head->token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000415
Nico Huber0421dc82012-05-21 14:53:43 +0200416 result = ehci_process_async_schedule(
417 EHCI_INST(ep->dev->controller), qh, head);
Julius Werner509c37e2013-08-28 12:29:28 -0700418 if (result >= 0) {
Julius Wernere9738db2013-02-21 13:41:40 -0800419 result = size - result;
Julius Werner509c37e2013-08-28 12:29:28 -0700420 if (pid == EHCI_IN && end != src + size)
421 memcpy(src, end - size, result);
422 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000423
Patrick Georgi8fa27872011-11-24 13:19:57 +0100424 ep->toggle = (cur->token & QTD_TOGGLE_MASK) >> QTD_TOGGLE_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000425
426 free_qh_and_tds(qh, head);
Julius Wernere9738db2013-02-21 13:41:40 -0800427
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000428 return result;
Julius Werner509c37e2013-08-28 12:29:28 -0700429
430oom:
431 usb_debug("Not enough DMA memory for EHCI control structures!\n");
432 free_qh_and_tds(qh, head);
433 return -1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000434}
435
436
437/* FIXME: Handle control transfers as 3 QHs, so the 2nd stage can be >0x4000 bytes */
Julius Werner509c37e2013-08-28 12:29:28 -0700438static int ehci_control (usbdev_t *dev, direction_t dir, int drlen, void *setup,
439 int dalen, u8 *src)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000440{
Julius Werner509c37e2013-08-28 12:29:28 -0700441 u8 *data = src;
442 u8 *devreq = setup;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000443 int endp = 0; // this is control. always 0 (for now)
444 int toggle = 0;
445 int mlen = dev->endpoints[0].maxpacketsize;
446 int result = 0;
447
Nico Huber1ab60752012-05-23 09:21:54 +0200448 int hubaddr = 0, hubport = 0, non_hs_ctrl_ep = 0;
449 if (dev->speed < 2) {
450 /* we need a split transaction */
451 if (closest_usb2_hub(dev, &hubaddr, &hubport))
Julius Wernere9738db2013-02-21 13:41:40 -0800452 return -1;
Nico Huber1ab60752012-05-23 09:21:54 +0200453 non_hs_ctrl_ep = 1;
454 }
455
Julius Werner509c37e2013-08-28 12:29:28 -0700456 if (!dma_coherent(setup)) {
457 devreq = EHCI_INST(dev->controller)->dma_buffer;
458 memcpy(devreq, setup, drlen);
459 }
460 if (dalen > 0 && !dma_coherent(src)) {
461 data = EHCI_INST(dev->controller)->dma_buffer + drlen;
462 if (drlen + dalen > DMA_SIZE) {
463 usb_debug("EHCI control transfer too large for DMA buffer: %d\n", drlen + dalen);
464 return -1;
465 }
466 if (dir == OUT)
467 memcpy(data, src, dalen);
468 }
469
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000470 /* create qTDs */
Julius Werner509c37e2013-08-28 12:29:28 -0700471 qtd_t *head = dma_memalign(64, sizeof(qtd_t));
472 ehci_qh_t *qh = dma_memalign(64, sizeof(ehci_qh_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000473 qtd_t *cur = head;
Julius Werner509c37e2013-08-28 12:29:28 -0700474 if (!qh || !head)
475 goto oom;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100476 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100477 cur->token = QTD_ACTIVE |
478 (toggle?QTD_TOGGLE_DATA1:0) |
479 (EHCI_SETUP << QTD_PID_SHIFT) |
480 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000481 if (fill_td(cur, devreq, drlen) != drlen) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700482 usb_debug("ERROR: couldn't send the entire device request\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000483 }
Julius Werner509c37e2013-08-28 12:29:28 -0700484 qtd_t *next = dma_memalign(64, sizeof(qtd_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000485 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100486 cur->alt_next_qtd = QTD_TERMINATE;
Julius Werner509c37e2013-08-28 12:29:28 -0700487 if (!next)
488 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000489
490 /* FIXME: We're limited to 16-20K (depending on alignment) for payload for now.
491 * Figure out, how toggle can be set sensibly in this scenario */
492 if (dalen > 0) {
493 toggle ^= 1;
494 cur = next;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100495 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100496 cur->token = QTD_ACTIVE |
497 (toggle?QTD_TOGGLE_DATA1:0) |
498 (((dir == OUT)?EHCI_OUT:EHCI_IN) << QTD_PID_SHIFT) |
499 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000500 if (fill_td(cur, data, dalen) != dalen) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700501 usb_debug("ERROR: couldn't send the entire control payload\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000502 }
Julius Werner509c37e2013-08-28 12:29:28 -0700503 next = dma_memalign(64, sizeof(qtd_t));
504 if (!next)
505 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000506 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100507 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000508 }
509
510 toggle = 1;
511 cur = next;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100512 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100513 cur->token = QTD_ACTIVE |
514 (toggle?QTD_TOGGLE_DATA1:QTD_TOGGLE_DATA0) |
515 ((dir == OUT)?EHCI_IN:EHCI_OUT) << QTD_PID_SHIFT |
516 (0 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000517 fill_td(cur, NULL, 0);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100518 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
519 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000520
521 /* create QH */
Nico Huber4f83d1b2012-11-23 11:58:57 +0100522 memset((void *)qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100523 qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH;
524 qh->epchar = dev->address |
525 (endp << QH_EP_SHIFT) |
526 (dev->speed << QH_EPS_SHIFT) |
527 (1 << QH_DTC_SHIFT) | /* ctrl transfers are special: take toggle bit from TD */
528 (1 << QH_RECLAIM_HEAD_SHIFT) |
529 (mlen << QH_MPS_SHIFT) |
Nico Huber1ab60752012-05-23 09:21:54 +0200530 (non_hs_ctrl_ep << QH_NON_HS_CTRL_EP_SHIFT) |
Patrick Georgi8fa27872011-11-24 13:19:57 +0100531 (0 << QH_NAK_CNT_SHIFT);
Nico Huber1ab60752012-05-23 09:21:54 +0200532 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
533 (hubport << QH_PORT_NUMBER_SHIFT) |
534 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000535 qh->td.next_qtd = virt_to_phys(head);
536
Nico Huber0421dc82012-05-21 14:53:43 +0200537 result = ehci_process_async_schedule(
538 EHCI_INST(dev->controller), qh, head);
Julius Werner509c37e2013-08-28 12:29:28 -0700539 if (result >= 0) {
Julius Wernere9738db2013-02-21 13:41:40 -0800540 result = dalen - result;
Julius Werner509c37e2013-08-28 12:29:28 -0700541 if (dir == IN && data != src)
542 memcpy(src, data, result);
543 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000544
545 free_qh_and_tds(qh, head);
546 return result;
Julius Werner509c37e2013-08-28 12:29:28 -0700547
548oom:
549 usb_debug("Not enough DMA memory for EHCI control structures!\n");
550 free_qh_and_tds(qh, head);
551 return -1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000552}
553
Nico Huber62eb5b32012-05-25 10:09:13 +0200554
555typedef struct _intr_qtd_t intr_qtd_t;
556
557struct _intr_qtd_t {
558 volatile qtd_t td;
559 u8 *data;
560 intr_qtd_t *next;
561};
562
563typedef struct {
564 volatile ehci_qh_t qh;
565 intr_qtd_t *head;
566 intr_qtd_t *tail;
Nico Huber0d120f82012-06-14 13:08:36 +0200567 intr_qtd_t *spare;
Nico Huber62eb5b32012-05-25 10:09:13 +0200568 u8 *data;
569 endpoint_t *endp;
570 int reqsize;
571} intr_queue_t;
572
573static void fill_intr_queue_td(
574 intr_queue_t *const intrq,
575 intr_qtd_t *const intr_qtd,
576 u8 *const data)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000577{
Nico Huber62eb5b32012-05-25 10:09:13 +0200578 const int pid = (intrq->endp->direction == IN) ? EHCI_IN
579 : (intrq->endp->direction == OUT) ? EHCI_OUT
580 : EHCI_SETUP;
581 const int cerr = (intrq->endp->dev->speed < 2) ? 1 : 0;
582
583 memset(intr_qtd, 0, sizeof(*intr_qtd));
584 intr_qtd->td.next_qtd = QTD_TERMINATE;
585 intr_qtd->td.alt_next_qtd = QTD_TERMINATE;
586 intr_qtd->td.token = QTD_ACTIVE |
587 (pid << QTD_PID_SHIFT) |
588 (cerr << QTD_CERR_SHIFT) |
589 ((intrq->endp->toggle & 1) << QTD_TOGGLE_SHIFT);
590 fill_td(&intr_qtd->td, data, intrq->reqsize);
591 intr_qtd->data = data;
592 intr_qtd->next = NULL;
593
594 intrq->endp->toggle ^= 1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000595}
596
Nico Huber62eb5b32012-05-25 10:09:13 +0200597static void ehci_destroy_intr_queue(endpoint_t *const, void *const);
598
599static void *ehci_create_intr_queue(
600 endpoint_t *const ep,
601 const int reqsize,
602 int reqcount,
603 const int reqtiming)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000604{
Nico Huber62eb5b32012-05-25 10:09:13 +0200605 int i;
606
607 if ((reqsize > (4 * 4096 + 1)) || /* the maximum for arbitrary aligned
608 data in five 4096 byte pages */
609 (reqtiming > 1024))
610 return NULL;
611 if (reqcount < 2) /* we need at least 2:
612 one for processing, one for the hc to advance to */
613 reqcount = 2;
614
615 int hubaddr = 0, hubport = 0;
616 if (ep->dev->speed < 2) {
617 /* we need a split transaction */
618 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
619 return NULL;
620 }
621
Jim Lin627de922014-09-16 16:39:08 +0800622 intr_queue_t *const intrq = (intr_queue_t *)dma_memalign(64,
623 sizeof(intr_queue_t));
Nico Huber0d120f82012-06-14 13:08:36 +0200624 /*
625 * reqcount data chunks
626 * plus one more spare, which we'll leave out of queue
627 */
Julius Werner509c37e2013-08-28 12:29:28 -0700628 u8 *data = (u8 *)dma_malloc(reqsize * (reqcount + 1));
Nico Huber62eb5b32012-05-25 10:09:13 +0200629 if (!intrq || !data)
630 fatal("Not enough memory to create USB interrupt queue.\n");
631 intrq->data = data;
632 intrq->endp = ep;
633 intrq->reqsize = reqsize;
634
635 /* create #reqcount transfer descriptors (qTDs) */
Julius Werner509c37e2013-08-28 12:29:28 -0700636 intrq->head = (intr_qtd_t *)dma_memalign(64, sizeof(intr_qtd_t));
Nico Huber62eb5b32012-05-25 10:09:13 +0200637 intr_qtd_t *cur_td = intrq->head;
638 for (i = 0; i < reqcount; ++i) {
639 fill_intr_queue_td(intrq, cur_td, data);
640 data += reqsize;
641 if (i < reqcount - 1) {
642 /* create one more qTD */
643 intr_qtd_t *const next_td =
Julius Werner509c37e2013-08-28 12:29:28 -0700644 (intr_qtd_t *)dma_memalign(64, sizeof(intr_qtd_t));
Nico Huber62eb5b32012-05-25 10:09:13 +0200645 cur_td->td.next_qtd = virt_to_phys(&next_td->td);
646 cur_td->next = next_td;
647 cur_td = next_td;
648 }
649 }
650 intrq->tail = cur_td;
651
Nico Huber0d120f82012-06-14 13:08:36 +0200652 /* create spare qTD */
Julius Werner509c37e2013-08-28 12:29:28 -0700653 intrq->spare = (intr_qtd_t *)dma_memalign(64, sizeof(intr_qtd_t));
Jim Lin16713d72014-12-08 19:30:04 +0800654 intrq->spare->data = data;
Nico Huber0d120f82012-06-14 13:08:36 +0200655
Nico Huber62eb5b32012-05-25 10:09:13 +0200656 /* initialize QH */
657 const int endp = ep->endpoint & 0xf;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100658 memset((void *)&intrq->qh, 0, sizeof(intrq->qh));
Nico Huber62eb5b32012-05-25 10:09:13 +0200659 intrq->qh.horiz_link_ptr = PS_TERMINATE;
660 intrq->qh.epchar = ep->dev->address |
661 (endp << QH_EP_SHIFT) |
662 (ep->dev->speed << QH_EPS_SHIFT) |
663 (1 << QH_DTC_SHIFT) |
664 (0 << QH_RECLAIM_HEAD_SHIFT) |
665 (ep->maxpacketsize << QH_MPS_SHIFT) |
666 (0 << QH_NAK_CNT_SHIFT);
667 intrq->qh.epcaps = (1 << QH_PIPE_MULTIPLIER_SHIFT) |
668 (hubport << QH_PORT_NUMBER_SHIFT) |
669 (hubaddr << QH_HUB_ADDRESS_SHIFT) |
670 (0xfe << QH_UFRAME_CMASK_SHIFT) |
671 1 /* uFrame S-mask */;
672 intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td);
673
674 /* insert QH into periodic schedule */
675 int nothing_placed = 1;
676 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(ep->dev->controller)
677 ->operation->periodiclistbase);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200678 const u32 dummy_ptr = virt_to_phys(EHCI_INST(
679 ep->dev->controller)->dummy_qh) | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200680 for (i = 0; i < 1024; i += reqtiming) {
681 /* advance to the next free position */
Nico Huber3ca35ca2012-06-14 13:27:39 +0200682 while ((i < 1024) && (ps[i] != dummy_ptr)) ++i;
Nico Huber62eb5b32012-05-25 10:09:13 +0200683 if (i < 1024) {
684 ps[i] = virt_to_phys(&intrq->qh) | PS_TYPE_QH;
685 nothing_placed = 0;
686 }
687 }
688 if (nothing_placed) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700689 usb_debug("Error: Failed to place ehci interrupt queue head "
Nico Huber62eb5b32012-05-25 10:09:13 +0200690 "into periodic schedule: no space left\n");
691 ehci_destroy_intr_queue(ep, intrq);
692 return NULL;
693 }
694
695 return intrq;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000696}
697
Nico Huber62eb5b32012-05-25 10:09:13 +0200698static void ehci_destroy_intr_queue(endpoint_t *const ep, void *const queue)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000699{
Nico Huber62eb5b32012-05-25 10:09:13 +0200700 intr_queue_t *const intrq = (intr_queue_t *)queue;
701
702 /* remove QH from periodic schedule */
703 int i;
704 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(
705 ep->dev->controller)->operation->periodiclistbase);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200706 const u32 dummy_ptr = virt_to_phys(EHCI_INST(
707 ep->dev->controller)->dummy_qh) | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200708 for (i = 0; i < 1024; ++i) {
709 if ((ps[i] & PS_PTR_MASK) == virt_to_phys(&intrq->qh))
Nico Huber3ca35ca2012-06-14 13:27:39 +0200710 ps[i] = dummy_ptr;
Nico Huber62eb5b32012-05-25 10:09:13 +0200711 }
712
713 /* wait 1ms for frame to end */
714 mdelay(1);
715
716 while (intrq->head) {
717 /* disable qTD and destroy list */
718 intrq->head->td.next_qtd = QTD_TERMINATE;
719 intrq->head->td.token &= ~QTD_ACTIVE;
720
721 /* save and advance head ptr */
722 intr_qtd_t *const to_free = intrq->head;
723 intrq->head = intrq->head->next;
724
725 /* free current interrupt qTD */
726 free(to_free);
727 }
Nico Huber0d120f82012-06-14 13:08:36 +0200728 free(intrq->spare);
Nico Huber62eb5b32012-05-25 10:09:13 +0200729 free(intrq->data);
730 free(intrq);
731}
732
733static u8 *ehci_poll_intr_queue(void *const queue)
734{
735 intr_queue_t *const intrq = (intr_queue_t *)queue;
736
737 u8 *ret = NULL;
738
739 /* process if head qTD is inactive AND QH has been moved forward */
Nico Huber0d120f82012-06-14 13:08:36 +0200740 if (!(intrq->head->td.token & QTD_ACTIVE)) {
Nico Huber62eb5b32012-05-25 10:09:13 +0200741 if (!(intrq->head->td.token & QTD_STATUS_MASK))
742 ret = intrq->head->data;
743 else
Gabe Black93ded592012-11-01 15:44:10 -0700744 usb_debug("ehci_poll_intr_queue: transfer failed, "
Nico Huber62eb5b32012-05-25 10:09:13 +0200745 "status == 0x%02x\n",
746 intrq->head->td.token & QTD_STATUS_MASK);
747
Nico Huber0d120f82012-06-14 13:08:36 +0200748 /* insert spare qTD at the end and advance our tail ptr */
749 fill_intr_queue_td(intrq, intrq->spare, intrq->spare->data);
750 intrq->tail->td.next_qtd = virt_to_phys(&intrq->spare->td);
751 intrq->tail->next = intrq->spare;
Nico Huber62eb5b32012-05-25 10:09:13 +0200752 intrq->tail = intrq->tail->next;
Nico Huber0d120f82012-06-14 13:08:36 +0200753
754 /* reuse executed qTD as spare one and advance our head ptr */
755 intrq->spare = intrq->head;
756 intrq->head = intrq->head->next;
757 }
758 /* reset queue if we fully processed it after underrun */
Nico Huberaf169f42012-11-22 11:14:03 +0100759 else if ((intrq->qh.td.next_qtd & QTD_TERMINATE) &&
760 /* to prevent race conditions:
761 not our head and not active */
762 (intrq->qh.current_td_ptr !=
763 virt_to_phys(&intrq->head->td)) &&
764 !(intrq->qh.td.token & QTD_ACTIVE)) {
Gabe Black93ded592012-11-01 15:44:10 -0700765 usb_debug("resetting underrun ehci interrupt queue.\n");
Nico Huberaf169f42012-11-22 11:14:03 +0100766 intrq->qh.current_td_ptr = 0;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100767 memset((void *)&intrq->qh.td, 0, sizeof(intrq->qh.td));
Nico Huber0d120f82012-06-14 13:08:36 +0200768 intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td);
Nico Huber62eb5b32012-05-25 10:09:13 +0200769 }
770 return ret;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000771}
772
773hci_t *
Nico Huber6e230662014-07-07 16:33:59 +0200774ehci_init (unsigned long physical_bar)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000775{
776 int i;
777 hci_t *controller = new_controller ();
Julius Werner7234d602014-04-08 12:54:25 -0700778 controller->instance = xzalloc(sizeof (ehci_t));
Anton Kochkov1c36ead2012-06-28 08:30:15 +0400779 controller->type = EHCI;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000780 controller->start = ehci_start;
781 controller->stop = ehci_stop;
782 controller->reset = ehci_reset;
Nico Huber6e711c62012-11-12 16:20:32 +0100783 controller->init = ehci_reinit;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000784 controller->shutdown = ehci_shutdown;
785 controller->bulk = ehci_bulk;
786 controller->control = ehci_control;
Patrick Georgi482af6d2013-05-24 15:48:56 +0200787 controller->set_address = generic_set_address;
788 controller->finish_device_config = NULL;
789 controller->destroy_device = NULL;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000790 controller->create_intr_queue = ehci_create_intr_queue;
791 controller->destroy_intr_queue = ehci_destroy_intr_queue;
792 controller->poll_intr_queue = ehci_poll_intr_queue;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000793 init_device_entry (controller, 0);
794
Patrick Georgi77466062015-05-12 10:44:02 +0200795 EHCI_INST(controller)->capabilities = phys_to_virt(physical_bar);
796 EHCI_INST(controller)->operation = (hc_op_t *)(phys_to_virt(physical_bar) + EHCI_INST(controller)->capabilities->caplength);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000797
Vincent Palatin716375d2012-10-25 17:38:43 -0700798 /* Set the high address word (aka segment) if controller is 64-bit */
799 if (EHCI_INST(controller)->capabilities->hccparams & 1)
800 EHCI_INST(controller)->operation->ctrldssegment = 0;
801
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000802 /* Enable operation of controller */
803 controller->start(controller);
804
805 /* take over all ports. USB1 should be blind now */
806 EHCI_INST(controller)->operation->configflag = 1;
807
Nico Huber62eb5b32012-05-25 10:09:13 +0200808 /* Initialize periodic frame list */
809 /* 1024 32-bit pointers, 4kb aligned */
Julius Werner509c37e2013-08-28 12:29:28 -0700810 u32 *const periodic_list = (u32 *)dma_memalign(4096, 1024 * sizeof(u32));
Nico Huber62eb5b32012-05-25 10:09:13 +0200811 if (!periodic_list)
812 fatal("Not enough memory creating EHCI periodic frame list.\n");
Nico Huber3ca35ca2012-06-14 13:27:39 +0200813
Julius Werner509c37e2013-08-28 12:29:28 -0700814 if (dma_initialized()) {
815 EHCI_INST(controller)->dma_buffer = dma_memalign(4096, DMA_SIZE);
816 if (!EHCI_INST(controller)->dma_buffer)
817 fatal("Not enough DMA memory for EHCI bounce buffer.\n");
818 }
819
Nico Huber3ca35ca2012-06-14 13:27:39 +0200820 /*
821 * Insert dummy QH in periodic frame list
822 * This helps with broken host controllers
823 * and doesn't violate the standard.
824 */
Julius Werner509c37e2013-08-28 12:29:28 -0700825 EHCI_INST(controller)->dummy_qh = (ehci_qh_t *)dma_memalign(64, sizeof(ehci_qh_t));
Nico Huber4f83d1b2012-11-23 11:58:57 +0100826 memset((void *)EHCI_INST(controller)->dummy_qh, 0,
Nico Huber3ca35ca2012-06-14 13:27:39 +0200827 sizeof(*EHCI_INST(controller)->dummy_qh));
828 EHCI_INST(controller)->dummy_qh->horiz_link_ptr = QH_TERMINATE;
Julius Werner4610f0e2013-08-22 16:24:09 -0700829 EHCI_INST(controller)->dummy_qh->td.next_qtd = QH_TERMINATE;
830 EHCI_INST(controller)->dummy_qh->td.alt_next_qtd = QH_TERMINATE;
Nico Huber62eb5b32012-05-25 10:09:13 +0200831 for (i = 0; i < 1024; ++i)
Nico Huber3ca35ca2012-06-14 13:27:39 +0200832 periodic_list[i] = virt_to_phys(EHCI_INST(controller)->dummy_qh)
833 | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200834
835 /* Make sure periodic schedule is disabled */
836 ehci_set_periodic_schedule(EHCI_INST(controller), 0);
837 /* Set periodic frame list pointer */
838 EHCI_INST(controller)->operation->periodiclistbase =
839 virt_to_phys(periodic_list);
840 /* Enable use of periodic schedule */
841 ehci_set_periodic_schedule(EHCI_INST(controller), 1);
842
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000843 /* TODO lots of stuff missing */
844
845 controller->devices[0]->controller = controller;
846 controller->devices[0]->init = ehci_rh_init;
847 controller->devices[0]->init (controller->devices[0]);
848
849 return controller;
850}
Stefan Reinauer8992e532013-05-02 16:16:41 -0700851
Stefan Reinauer1b4d3942015-06-29 15:47:34 -0700852#if IS_ENABLED(CONFIG_LP_USB_PCI)
Stefan Reinauer8992e532013-05-02 16:16:41 -0700853hci_t *
854ehci_pci_init (pcidev_t addr)
855{
856 hci_t *controller;
857 u32 reg_base;
858
859 u32 pci_command = pci_read_config32(addr, PCI_COMMAND);
860 pci_command = (pci_command | PCI_COMMAND_MEMORY) & ~PCI_COMMAND_IO ;
861 pci_write_config32(addr, PCI_COMMAND, pci_command);
862
863 reg_base = pci_read_config32 (addr, USBBASE);
864
865 /* default value for frame length adjust */
866 pci_write_config8(addr, FLADJ, FLADJ_framelength(60000));
867
Nico Huber6e230662014-07-07 16:33:59 +0200868 controller = ehci_init((unsigned long)reg_base);
Stefan Reinauer8992e532013-05-02 16:16:41 -0700869
870 return controller;
871}
872#endif