blob: 7002623e497bf9304ecf28dd2d2184de79955910 [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
Nico Huber1ab60752012-05-23 09:21:54 +0200197/*
198 * returns the address of the closest USB2.0 hub, which is responsible for
199 * split transactions, along with the number of the used downstream port
200 */
201static int closest_usb2_hub(const usbdev_t *dev, int *const addr, int *const port)
202{
203 const usbdev_t *usb1dev;
204 do {
205 usb1dev = dev;
206 if ((dev->hub > 0) && (dev->hub < 128))
207 dev = dev->controller->devices[dev->hub];
208 else
209 dev = NULL;
210 } while (dev && (dev->speed < 2));
211 if (dev) {
212 *addr = usb1dev->hub;
213 *port = usb1dev->port;
214 return 0;
215 } else {
Gabe Black93ded592012-11-01 15:44:10 -0700216 usb_debug("ehci: Couldn't find closest USB2.0 hub.\n");
Nico Huber1ab60752012-05-23 09:21:54 +0200217 return 1;
218 }
219}
220
Patrick Georgi8fa27872011-11-24 13:19:57 +0100221/* returns handled bytes. assumes that the fields it writes are empty on entry */
222static int fill_td(qtd_t *td, void* data, int datalen)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000223{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000224 u32 total_len = 0;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100225 u32 page_no = 0;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000226
227 u32 start = virt_to_phys(data);
228 u32 page = start & ~4095;
229 u32 offset = start & 4095;
230 u32 page_len = 4096 - offset;
231
Patrick Georgi8fa27872011-11-24 13:19:57 +0100232 td->token |= 0 << QTD_CPAGE_SHIFT;
233 td->bufptrs[page_no++] = start;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000234
235 if (datalen <= page_len) {
236 total_len = datalen;
237 } else {
238 datalen -= page_len;
239 total_len += page_len;
240
Patrick Georgi8fa27872011-11-24 13:19:57 +0100241 while (page_no < 5) {
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000242 /* we have a continguous mapping between virtual and physical memory */
243 page += 4096;
244
Patrick Georgi8fa27872011-11-24 13:19:57 +0100245 td->bufptrs[page_no++] = page;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000246 if (datalen <= 4096) {
247 total_len += datalen;
248 break;
249 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000250 datalen -= 4096;
251 total_len += 4096;
Julius Werner69eea7c2013-01-11 16:25:52 -0800252
253 /* end TD at a packet boundary if transfer not complete */
254 if (page_no == 5)
255 total_len &= ~511;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100256 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000257 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100258 td->token |= total_len << QTD_TOTAL_LEN_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000259 return total_len;
260}
261
262/* free up data structures */
Patrick Georgi8fa27872011-11-24 13:19:57 +0100263static void free_qh_and_tds(ehci_qh_t *qh, qtd_t *cur)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000264{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000265 qtd_t *next;
266 while (cur) {
267 next = (qtd_t*)phys_to_virt(cur->next_qtd & ~31);
Nico Huber4f83d1b2012-11-23 11:58:57 +0100268 free((void *)cur);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000269 cur = next;
270 }
Nico Huber4f83d1b2012-11-23 11:58:57 +0100271 free((void *)qh);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000272}
273
Patrick Georgi8fa27872011-11-24 13:19:57 +0100274static int wait_for_tds(qtd_t *head)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000275{
Julius Wernere9738db2013-02-21 13:41:40 -0800276 /* returns the amount of bytes *not* transmitted, or -1 for error */
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000277 int result = 0;
278 qtd_t *cur = head;
279 while (1) {
280 if (0) dump_td(virt_to_phys(cur));
Nico Huber0421dc82012-05-21 14:53:43 +0200281
282 /* wait for results */
Nico Hubercef86922012-06-01 08:54:29 +0200283 /* how long to wait?
284 * tested with some USB2.0 flash sticks:
285 * TUR turn around took
Julius Werner52e3f442013-07-23 12:45:18 -0700286 * about 2.2s for the slowest (13fe:3800)
Nico Hubercef86922012-06-01 08:54:29 +0200287 * max. 250ms for the others
288 * slowest non-TUR turn around took about 1.3s
Julius Werner52e3f442013-07-23 12:45:18 -0700289 * set to 3s to be safe as a failed TUR can be fatal
Nico Huber0421dc82012-05-21 14:53:43 +0200290 */
Julius Werner52e3f442013-07-23 12:45:18 -0700291 int timeout = 60000; /* time out after 60000 * 50us == 3s */
Nico Huber0421dc82012-05-21 14:53:43 +0200292 while ((cur->token & QTD_ACTIVE) && !(cur->token & QTD_HALTED)
293 && timeout--)
294 udelay(50);
295 if (timeout < 0) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700296 usb_debug("Error: ehci: queue transfer "
Nico Huber0421dc82012-05-21 14:53:43 +0200297 "processing timed out.\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800298 return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200299 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100300 if (cur->token & QTD_HALTED) {
Nico Huber86c686a2012-11-21 16:22:26 +0100301 usb_debug("ERROR with packet\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000302 dump_td(virt_to_phys(cur));
Gabe Black93ded592012-11-01 15:44:10 -0700303 usb_debug("-----------------\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800304 return -1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000305 }
Julius Wernere9738db2013-02-21 13:41:40 -0800306 result += (cur->token & QTD_TOTAL_LEN_MASK)
307 >> QTD_TOTAL_LEN_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000308 if (cur->next_qtd & 1) {
Julius Wernere9738db2013-02-21 13:41:40 -0800309 break;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000310 }
311 if (0) dump_td(virt_to_phys(cur));
312 /* helps debugging the TD chain */
Gabe Black93ded592012-11-01 15:44:10 -0700313 if (0) usb_debug("\nmoving from %x to %x\n", cur, phys_to_virt(cur->next_qtd));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000314 cur = phys_to_virt(cur->next_qtd);
315 }
316 return result;
317}
318
Nico Huber0421dc82012-05-21 14:53:43 +0200319static int ehci_set_async_schedule(ehci_t *ehcic, int enable)
320{
Furquan Shaikh5b1c3282014-08-24 23:07:43 -0700321
322 /* Memory barrier to ensure that all memory accesses before we set the
323 * async schedule are complete. It was observed especially in the case of
324 * arm64, that netboot and usb stuff resulted in lots of errors possibly
325 * due to CPU reordering. Hence, enforcing strict CPU ordering.
326 */
327 mb();
328
Nico Huber0421dc82012-05-21 14:53:43 +0200329 /* Set async schedule status. */
330 if (enable)
331 ehcic->operation->usbcmd |= HC_OP_ASYNC_SCHED_EN;
332 else
333 ehcic->operation->usbcmd &= ~HC_OP_ASYNC_SCHED_EN;
334 /* Wait for the controller to accept async schedule status.
335 * This shouldn't take too long, but we should timeout nevertheless.
336 */
337 enable = enable ? HC_OP_ASYNC_SCHED_STAT : 0;
338 int timeout = 100; /* time out after 100ms */
339 while (((ehcic->operation->usbsts & HC_OP_ASYNC_SCHED_STAT) != enable)
340 && timeout--)
341 mdelay(1);
342 if (timeout < 0) {
Gabe Black93ded592012-11-01 15:44:10 -0700343 usb_debug("ehci async schedule status change timed out.\n");
Nico Huber0421dc82012-05-21 14:53:43 +0200344 return 1;
345 }
346 return 0;
347}
348
349static int ehci_process_async_schedule(
350 ehci_t *ehcic, ehci_qh_t *qhead, qtd_t *head)
351{
352 int result;
353
354 /* make sure async schedule is disabled */
Julius Wernere9738db2013-02-21 13:41:40 -0800355 if (ehci_set_async_schedule(ehcic, 0)) return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200356
357 /* hook up QH */
358 ehcic->operation->asynclistaddr = virt_to_phys(qhead);
359
360 /* start async schedule */
Julius Wernere9738db2013-02-21 13:41:40 -0800361 if (ehci_set_async_schedule(ehcic, 1)) return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200362
363 /* wait for result */
364 result = wait_for_tds(head);
365
366 /* disable async schedule */
367 ehci_set_async_schedule(ehcic, 0);
368
369 return result;
370}
371
Julius Werner509c37e2013-08-28 12:29:28 -0700372static int ehci_bulk (endpoint_t *ep, int size, u8 *src, int finalize)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000373{
374 int result = 0;
Julius Werner509c37e2013-08-28 12:29:28 -0700375 u8 *end = src + size;
Julius Wernere9738db2013-02-21 13:41:40 -0800376 int remaining = size;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000377 int endp = ep->endpoint & 0xf;
378 int pid = (ep->direction==IN)?EHCI_IN:EHCI_OUT;
379
Nico Huber1ab60752012-05-23 09:21:54 +0200380 int hubaddr = 0, hubport = 0;
381 if (ep->dev->speed < 2) {
382 /* we need a split transaction */
383 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
Julius Wernere9738db2013-02-21 13:41:40 -0800384 return -1;
Nico Huber1ab60752012-05-23 09:21:54 +0200385 }
386
Julius Werner509c37e2013-08-28 12:29:28 -0700387 if (!dma_coherent(src)) {
388 end = EHCI_INST(ep->dev->controller)->dma_buffer + size;
389 if (size > DMA_SIZE) {
390 usb_debug("EHCI bulk transfer too large for DMA buffer: %d\n", size);
391 return -1;
392 }
393 if (pid == EHCI_OUT)
394 memcpy(end - size, src, size);
395 }
396
397 ehci_qh_t *qh = dma_memalign(64, sizeof(ehci_qh_t));
398 qtd_t *head = dma_memalign(64, sizeof(qtd_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000399 qtd_t *cur = head;
Julius Werner509c37e2013-08-28 12:29:28 -0700400 if (!qh || !head)
401 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000402 while (1) {
Nico Huber4f83d1b2012-11-23 11:58:57 +0100403 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100404 cur->token = QTD_ACTIVE |
405 (pid << QTD_PID_SHIFT) |
406 (0 << QTD_CERR_SHIFT);
Julius Werner509c37e2013-08-28 12:29:28 -0700407 remaining -= fill_td(cur, end - remaining, remaining);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000408
Patrick Georgi8fa27872011-11-24 13:19:57 +0100409 cur->alt_next_qtd = QTD_TERMINATE;
Julius Werner509c37e2013-08-28 12:29:28 -0700410 if (remaining <= 0) {
Patrick Georgi8fa27872011-11-24 13:19:57 +0100411 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000412 break;
413 } else {
Julius Werner509c37e2013-08-28 12:29:28 -0700414 qtd_t *next = dma_memalign(64, sizeof(qtd_t));
415 if (!next)
416 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000417 cur->next_qtd = virt_to_phys(next);
418 cur = next;
419 }
420 }
421
422 /* create QH */
Nico Huber4f83d1b2012-11-23 11:58:57 +0100423 memset((void *)qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100424 qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH;
425 qh->epchar = ep->dev->address |
426 (endp << QH_EP_SHIFT) |
427 (ep->dev->speed << QH_EPS_SHIFT) |
428 (0 << QH_DTC_SHIFT) |
429 (1 << QH_RECLAIM_HEAD_SHIFT) |
430 (ep->maxpacketsize << QH_MPS_SHIFT) |
431 (0 << QH_NAK_CNT_SHIFT);
Nico Huber1ab60752012-05-23 09:21:54 +0200432 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
433 (hubport << QH_PORT_NUMBER_SHIFT) |
434 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000435
436 qh->td.next_qtd = virt_to_phys(head);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100437 qh->td.token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
438 head->token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000439
Nico Huber0421dc82012-05-21 14:53:43 +0200440 result = ehci_process_async_schedule(
441 EHCI_INST(ep->dev->controller), qh, head);
Julius Werner509c37e2013-08-28 12:29:28 -0700442 if (result >= 0) {
Julius Wernere9738db2013-02-21 13:41:40 -0800443 result = size - result;
Julius Werner509c37e2013-08-28 12:29:28 -0700444 if (pid == EHCI_IN && end != src + size)
445 memcpy(src, end - size, result);
446 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000447
Patrick Georgi8fa27872011-11-24 13:19:57 +0100448 ep->toggle = (cur->token & QTD_TOGGLE_MASK) >> QTD_TOGGLE_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000449
450 free_qh_and_tds(qh, head);
Julius Wernere9738db2013-02-21 13:41:40 -0800451
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000452 return result;
Julius Werner509c37e2013-08-28 12:29:28 -0700453
454oom:
455 usb_debug("Not enough DMA memory for EHCI control structures!\n");
456 free_qh_and_tds(qh, head);
457 return -1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000458}
459
460
461/* FIXME: Handle control transfers as 3 QHs, so the 2nd stage can be >0x4000 bytes */
Julius Werner509c37e2013-08-28 12:29:28 -0700462static int ehci_control (usbdev_t *dev, direction_t dir, int drlen, void *setup,
463 int dalen, u8 *src)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000464{
Julius Werner509c37e2013-08-28 12:29:28 -0700465 u8 *data = src;
466 u8 *devreq = setup;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000467 int endp = 0; // this is control. always 0 (for now)
468 int toggle = 0;
469 int mlen = dev->endpoints[0].maxpacketsize;
470 int result = 0;
471
Nico Huber1ab60752012-05-23 09:21:54 +0200472 int hubaddr = 0, hubport = 0, non_hs_ctrl_ep = 0;
473 if (dev->speed < 2) {
474 /* we need a split transaction */
475 if (closest_usb2_hub(dev, &hubaddr, &hubport))
Julius Wernere9738db2013-02-21 13:41:40 -0800476 return -1;
Nico Huber1ab60752012-05-23 09:21:54 +0200477 non_hs_ctrl_ep = 1;
478 }
479
Julius Werner509c37e2013-08-28 12:29:28 -0700480 if (!dma_coherent(setup)) {
481 devreq = EHCI_INST(dev->controller)->dma_buffer;
482 memcpy(devreq, setup, drlen);
483 }
484 if (dalen > 0 && !dma_coherent(src)) {
485 data = EHCI_INST(dev->controller)->dma_buffer + drlen;
486 if (drlen + dalen > DMA_SIZE) {
487 usb_debug("EHCI control transfer too large for DMA buffer: %d\n", drlen + dalen);
488 return -1;
489 }
490 if (dir == OUT)
491 memcpy(data, src, dalen);
492 }
493
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000494 /* create qTDs */
Julius Werner509c37e2013-08-28 12:29:28 -0700495 qtd_t *head = dma_memalign(64, sizeof(qtd_t));
496 ehci_qh_t *qh = dma_memalign(64, sizeof(ehci_qh_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000497 qtd_t *cur = head;
Julius Werner509c37e2013-08-28 12:29:28 -0700498 if (!qh || !head)
499 goto oom;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100500 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100501 cur->token = QTD_ACTIVE |
502 (toggle?QTD_TOGGLE_DATA1:0) |
503 (EHCI_SETUP << QTD_PID_SHIFT) |
504 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000505 if (fill_td(cur, devreq, drlen) != drlen) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700506 usb_debug("ERROR: couldn't send the entire device request\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000507 }
Julius Werner509c37e2013-08-28 12:29:28 -0700508 qtd_t *next = dma_memalign(64, sizeof(qtd_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000509 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100510 cur->alt_next_qtd = QTD_TERMINATE;
Julius Werner509c37e2013-08-28 12:29:28 -0700511 if (!next)
512 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000513
514 /* FIXME: We're limited to 16-20K (depending on alignment) for payload for now.
515 * Figure out, how toggle can be set sensibly in this scenario */
516 if (dalen > 0) {
517 toggle ^= 1;
518 cur = next;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100519 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100520 cur->token = QTD_ACTIVE |
521 (toggle?QTD_TOGGLE_DATA1:0) |
522 (((dir == OUT)?EHCI_OUT:EHCI_IN) << QTD_PID_SHIFT) |
523 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000524 if (fill_td(cur, data, dalen) != dalen) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700525 usb_debug("ERROR: couldn't send the entire control payload\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000526 }
Julius Werner509c37e2013-08-28 12:29:28 -0700527 next = dma_memalign(64, sizeof(qtd_t));
528 if (!next)
529 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000530 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100531 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000532 }
533
534 toggle = 1;
535 cur = next;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100536 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100537 cur->token = QTD_ACTIVE |
538 (toggle?QTD_TOGGLE_DATA1:QTD_TOGGLE_DATA0) |
539 ((dir == OUT)?EHCI_IN:EHCI_OUT) << QTD_PID_SHIFT |
540 (0 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000541 fill_td(cur, NULL, 0);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100542 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
543 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000544
545 /* create QH */
Nico Huber4f83d1b2012-11-23 11:58:57 +0100546 memset((void *)qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100547 qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH;
548 qh->epchar = dev->address |
549 (endp << QH_EP_SHIFT) |
550 (dev->speed << QH_EPS_SHIFT) |
551 (1 << QH_DTC_SHIFT) | /* ctrl transfers are special: take toggle bit from TD */
552 (1 << QH_RECLAIM_HEAD_SHIFT) |
553 (mlen << QH_MPS_SHIFT) |
Nico Huber1ab60752012-05-23 09:21:54 +0200554 (non_hs_ctrl_ep << QH_NON_HS_CTRL_EP_SHIFT) |
Patrick Georgi8fa27872011-11-24 13:19:57 +0100555 (0 << QH_NAK_CNT_SHIFT);
Nico Huber1ab60752012-05-23 09:21:54 +0200556 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
557 (hubport << QH_PORT_NUMBER_SHIFT) |
558 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000559 qh->td.next_qtd = virt_to_phys(head);
560
Nico Huber0421dc82012-05-21 14:53:43 +0200561 result = ehci_process_async_schedule(
562 EHCI_INST(dev->controller), qh, head);
Julius Werner509c37e2013-08-28 12:29:28 -0700563 if (result >= 0) {
Julius Wernere9738db2013-02-21 13:41:40 -0800564 result = dalen - result;
Julius Werner509c37e2013-08-28 12:29:28 -0700565 if (dir == IN && data != src)
566 memcpy(src, data, result);
567 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000568
569 free_qh_and_tds(qh, head);
570 return result;
Julius Werner509c37e2013-08-28 12:29:28 -0700571
572oom:
573 usb_debug("Not enough DMA memory for EHCI control structures!\n");
574 free_qh_and_tds(qh, head);
575 return -1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000576}
577
Nico Huber62eb5b32012-05-25 10:09:13 +0200578
579typedef struct _intr_qtd_t intr_qtd_t;
580
581struct _intr_qtd_t {
582 volatile qtd_t td;
583 u8 *data;
584 intr_qtd_t *next;
585};
586
587typedef struct {
588 volatile ehci_qh_t qh;
589 intr_qtd_t *head;
590 intr_qtd_t *tail;
Nico Huber0d120f82012-06-14 13:08:36 +0200591 intr_qtd_t *spare;
Nico Huber62eb5b32012-05-25 10:09:13 +0200592 u8 *data;
593 endpoint_t *endp;
594 int reqsize;
595} intr_queue_t;
596
597static void fill_intr_queue_td(
598 intr_queue_t *const intrq,
599 intr_qtd_t *const intr_qtd,
600 u8 *const data)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000601{
Nico Huber62eb5b32012-05-25 10:09:13 +0200602 const int pid = (intrq->endp->direction == IN) ? EHCI_IN
603 : (intrq->endp->direction == OUT) ? EHCI_OUT
604 : EHCI_SETUP;
605 const int cerr = (intrq->endp->dev->speed < 2) ? 1 : 0;
606
607 memset(intr_qtd, 0, sizeof(*intr_qtd));
608 intr_qtd->td.next_qtd = QTD_TERMINATE;
609 intr_qtd->td.alt_next_qtd = QTD_TERMINATE;
610 intr_qtd->td.token = QTD_ACTIVE |
611 (pid << QTD_PID_SHIFT) |
612 (cerr << QTD_CERR_SHIFT) |
613 ((intrq->endp->toggle & 1) << QTD_TOGGLE_SHIFT);
614 fill_td(&intr_qtd->td, data, intrq->reqsize);
615 intr_qtd->data = data;
616 intr_qtd->next = NULL;
617
618 intrq->endp->toggle ^= 1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000619}
620
Nico Huber62eb5b32012-05-25 10:09:13 +0200621static void ehci_destroy_intr_queue(endpoint_t *const, void *const);
622
623static void *ehci_create_intr_queue(
624 endpoint_t *const ep,
625 const int reqsize,
626 int reqcount,
627 const int reqtiming)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000628{
Nico Huber62eb5b32012-05-25 10:09:13 +0200629 int i;
630
631 if ((reqsize > (4 * 4096 + 1)) || /* the maximum for arbitrary aligned
632 data in five 4096 byte pages */
633 (reqtiming > 1024))
634 return NULL;
635 if (reqcount < 2) /* we need at least 2:
636 one for processing, one for the hc to advance to */
637 reqcount = 2;
638
639 int hubaddr = 0, hubport = 0;
640 if (ep->dev->speed < 2) {
641 /* we need a split transaction */
642 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
643 return NULL;
644 }
645
Julius Werner509c37e2013-08-28 12:29:28 -0700646 intr_queue_t *const intrq = (intr_queue_t *)malloc(sizeof(intr_queue_t));
Nico Huber0d120f82012-06-14 13:08:36 +0200647 /*
648 * reqcount data chunks
649 * plus one more spare, which we'll leave out of queue
650 */
Julius Werner509c37e2013-08-28 12:29:28 -0700651 u8 *data = (u8 *)dma_malloc(reqsize * (reqcount + 1));
Nico Huber62eb5b32012-05-25 10:09:13 +0200652 if (!intrq || !data)
653 fatal("Not enough memory to create USB interrupt queue.\n");
654 intrq->data = data;
655 intrq->endp = ep;
656 intrq->reqsize = reqsize;
657
658 /* create #reqcount transfer descriptors (qTDs) */
Julius Werner509c37e2013-08-28 12:29:28 -0700659 intrq->head = (intr_qtd_t *)dma_memalign(64, sizeof(intr_qtd_t));
Nico Huber62eb5b32012-05-25 10:09:13 +0200660 intr_qtd_t *cur_td = intrq->head;
661 for (i = 0; i < reqcount; ++i) {
662 fill_intr_queue_td(intrq, cur_td, data);
663 data += reqsize;
664 if (i < reqcount - 1) {
665 /* create one more qTD */
666 intr_qtd_t *const next_td =
Julius Werner509c37e2013-08-28 12:29:28 -0700667 (intr_qtd_t *)dma_memalign(64, sizeof(intr_qtd_t));
Nico Huber62eb5b32012-05-25 10:09:13 +0200668 cur_td->td.next_qtd = virt_to_phys(&next_td->td);
669 cur_td->next = next_td;
670 cur_td = next_td;
671 }
672 }
673 intrq->tail = cur_td;
674
Nico Huber0d120f82012-06-14 13:08:36 +0200675 /* create spare qTD */
Julius Werner509c37e2013-08-28 12:29:28 -0700676 intrq->spare = (intr_qtd_t *)dma_memalign(64, sizeof(intr_qtd_t));
Nico Huber0d120f82012-06-14 13:08:36 +0200677 fill_intr_queue_td(intrq, intrq->spare, data);
678
Nico Huber62eb5b32012-05-25 10:09:13 +0200679 /* initialize QH */
680 const int endp = ep->endpoint & 0xf;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100681 memset((void *)&intrq->qh, 0, sizeof(intrq->qh));
Nico Huber62eb5b32012-05-25 10:09:13 +0200682 intrq->qh.horiz_link_ptr = PS_TERMINATE;
683 intrq->qh.epchar = ep->dev->address |
684 (endp << QH_EP_SHIFT) |
685 (ep->dev->speed << QH_EPS_SHIFT) |
686 (1 << QH_DTC_SHIFT) |
687 (0 << QH_RECLAIM_HEAD_SHIFT) |
688 (ep->maxpacketsize << QH_MPS_SHIFT) |
689 (0 << QH_NAK_CNT_SHIFT);
690 intrq->qh.epcaps = (1 << QH_PIPE_MULTIPLIER_SHIFT) |
691 (hubport << QH_PORT_NUMBER_SHIFT) |
692 (hubaddr << QH_HUB_ADDRESS_SHIFT) |
693 (0xfe << QH_UFRAME_CMASK_SHIFT) |
694 1 /* uFrame S-mask */;
695 intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td);
696
697 /* insert QH into periodic schedule */
698 int nothing_placed = 1;
699 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(ep->dev->controller)
700 ->operation->periodiclistbase);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200701 const u32 dummy_ptr = virt_to_phys(EHCI_INST(
702 ep->dev->controller)->dummy_qh) | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200703 for (i = 0; i < 1024; i += reqtiming) {
704 /* advance to the next free position */
Nico Huber3ca35ca2012-06-14 13:27:39 +0200705 while ((i < 1024) && (ps[i] != dummy_ptr)) ++i;
Nico Huber62eb5b32012-05-25 10:09:13 +0200706 if (i < 1024) {
707 ps[i] = virt_to_phys(&intrq->qh) | PS_TYPE_QH;
708 nothing_placed = 0;
709 }
710 }
711 if (nothing_placed) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700712 usb_debug("Error: Failed to place ehci interrupt queue head "
Nico Huber62eb5b32012-05-25 10:09:13 +0200713 "into periodic schedule: no space left\n");
714 ehci_destroy_intr_queue(ep, intrq);
715 return NULL;
716 }
717
718 return intrq;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000719}
720
Nico Huber62eb5b32012-05-25 10:09:13 +0200721static void ehci_destroy_intr_queue(endpoint_t *const ep, void *const queue)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000722{
Nico Huber62eb5b32012-05-25 10:09:13 +0200723 intr_queue_t *const intrq = (intr_queue_t *)queue;
724
725 /* remove QH from periodic schedule */
726 int i;
727 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(
728 ep->dev->controller)->operation->periodiclistbase);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200729 const u32 dummy_ptr = virt_to_phys(EHCI_INST(
730 ep->dev->controller)->dummy_qh) | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200731 for (i = 0; i < 1024; ++i) {
732 if ((ps[i] & PS_PTR_MASK) == virt_to_phys(&intrq->qh))
Nico Huber3ca35ca2012-06-14 13:27:39 +0200733 ps[i] = dummy_ptr;
Nico Huber62eb5b32012-05-25 10:09:13 +0200734 }
735
736 /* wait 1ms for frame to end */
737 mdelay(1);
738
739 while (intrq->head) {
740 /* disable qTD and destroy list */
741 intrq->head->td.next_qtd = QTD_TERMINATE;
742 intrq->head->td.token &= ~QTD_ACTIVE;
743
744 /* save and advance head ptr */
745 intr_qtd_t *const to_free = intrq->head;
746 intrq->head = intrq->head->next;
747
748 /* free current interrupt qTD */
749 free(to_free);
750 }
Nico Huber0d120f82012-06-14 13:08:36 +0200751 free(intrq->spare);
Nico Huber62eb5b32012-05-25 10:09:13 +0200752 free(intrq->data);
753 free(intrq);
754}
755
756static u8 *ehci_poll_intr_queue(void *const queue)
757{
758 intr_queue_t *const intrq = (intr_queue_t *)queue;
759
760 u8 *ret = NULL;
761
762 /* process if head qTD is inactive AND QH has been moved forward */
Nico Huber0d120f82012-06-14 13:08:36 +0200763 if (!(intrq->head->td.token & QTD_ACTIVE)) {
Nico Huber62eb5b32012-05-25 10:09:13 +0200764 if (!(intrq->head->td.token & QTD_STATUS_MASK))
765 ret = intrq->head->data;
766 else
Gabe Black93ded592012-11-01 15:44:10 -0700767 usb_debug("ehci_poll_intr_queue: transfer failed, "
Nico Huber62eb5b32012-05-25 10:09:13 +0200768 "status == 0x%02x\n",
769 intrq->head->td.token & QTD_STATUS_MASK);
770
Nico Huber0d120f82012-06-14 13:08:36 +0200771 /* insert spare qTD at the end and advance our tail ptr */
772 fill_intr_queue_td(intrq, intrq->spare, intrq->spare->data);
773 intrq->tail->td.next_qtd = virt_to_phys(&intrq->spare->td);
774 intrq->tail->next = intrq->spare;
Nico Huber62eb5b32012-05-25 10:09:13 +0200775 intrq->tail = intrq->tail->next;
Nico Huber0d120f82012-06-14 13:08:36 +0200776
777 /* reuse executed qTD as spare one and advance our head ptr */
778 intrq->spare = intrq->head;
779 intrq->head = intrq->head->next;
780 }
781 /* reset queue if we fully processed it after underrun */
Nico Huberaf169f42012-11-22 11:14:03 +0100782 else if ((intrq->qh.td.next_qtd & QTD_TERMINATE) &&
783 /* to prevent race conditions:
784 not our head and not active */
785 (intrq->qh.current_td_ptr !=
786 virt_to_phys(&intrq->head->td)) &&
787 !(intrq->qh.td.token & QTD_ACTIVE)) {
Gabe Black93ded592012-11-01 15:44:10 -0700788 usb_debug("resetting underrun ehci interrupt queue.\n");
Nico Huberaf169f42012-11-22 11:14:03 +0100789 intrq->qh.current_td_ptr = 0;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100790 memset((void *)&intrq->qh.td, 0, sizeof(intrq->qh.td));
Nico Huber0d120f82012-06-14 13:08:36 +0200791 intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td);
Nico Huber62eb5b32012-05-25 10:09:13 +0200792 }
793 return ret;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000794}
795
796hci_t *
Nico Huber6e230662014-07-07 16:33:59 +0200797ehci_init (unsigned long physical_bar)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000798{
799 int i;
800 hci_t *controller = new_controller ();
Julius Werner7234d602014-04-08 12:54:25 -0700801 controller->instance = xzalloc(sizeof (ehci_t));
Anton Kochkov1c36ead2012-06-28 08:30:15 +0400802 controller->type = EHCI;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000803 controller->start = ehci_start;
804 controller->stop = ehci_stop;
805 controller->reset = ehci_reset;
Nico Huber6e711c62012-11-12 16:20:32 +0100806 controller->init = ehci_reinit;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000807 controller->shutdown = ehci_shutdown;
808 controller->bulk = ehci_bulk;
809 controller->control = ehci_control;
Patrick Georgi482af6d2013-05-24 15:48:56 +0200810 controller->set_address = generic_set_address;
811 controller->finish_device_config = NULL;
812 controller->destroy_device = NULL;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000813 controller->create_intr_queue = ehci_create_intr_queue;
814 controller->destroy_intr_queue = ehci_destroy_intr_queue;
815 controller->poll_intr_queue = ehci_poll_intr_queue;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000816 init_device_entry (controller, 0);
817
Julius Werner7234d602014-04-08 12:54:25 -0700818 EHCI_INST(controller)->capabilities = phys_to_virt(controller->reg_base);
819 EHCI_INST(controller)->operation = (hc_op_t *)(phys_to_virt(controller->reg_base) + EHCI_INST(controller)->capabilities->caplength);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000820
Vincent Palatin716375d2012-10-25 17:38:43 -0700821 /* Set the high address word (aka segment) if controller is 64-bit */
822 if (EHCI_INST(controller)->capabilities->hccparams & 1)
823 EHCI_INST(controller)->operation->ctrldssegment = 0;
824
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000825 /* Enable operation of controller */
826 controller->start(controller);
827
828 /* take over all ports. USB1 should be blind now */
829 EHCI_INST(controller)->operation->configflag = 1;
830
Nico Huber62eb5b32012-05-25 10:09:13 +0200831 /* Initialize periodic frame list */
832 /* 1024 32-bit pointers, 4kb aligned */
Julius Werner509c37e2013-08-28 12:29:28 -0700833 u32 *const periodic_list = (u32 *)dma_memalign(4096, 1024 * sizeof(u32));
Nico Huber62eb5b32012-05-25 10:09:13 +0200834 if (!periodic_list)
835 fatal("Not enough memory creating EHCI periodic frame list.\n");
Nico Huber3ca35ca2012-06-14 13:27:39 +0200836
Julius Werner509c37e2013-08-28 12:29:28 -0700837 if (dma_initialized()) {
838 EHCI_INST(controller)->dma_buffer = dma_memalign(4096, DMA_SIZE);
839 if (!EHCI_INST(controller)->dma_buffer)
840 fatal("Not enough DMA memory for EHCI bounce buffer.\n");
841 }
842
Nico Huber3ca35ca2012-06-14 13:27:39 +0200843 /*
844 * Insert dummy QH in periodic frame list
845 * This helps with broken host controllers
846 * and doesn't violate the standard.
847 */
Julius Werner509c37e2013-08-28 12:29:28 -0700848 EHCI_INST(controller)->dummy_qh = (ehci_qh_t *)dma_memalign(64, sizeof(ehci_qh_t));
Nico Huber4f83d1b2012-11-23 11:58:57 +0100849 memset((void *)EHCI_INST(controller)->dummy_qh, 0,
Nico Huber3ca35ca2012-06-14 13:27:39 +0200850 sizeof(*EHCI_INST(controller)->dummy_qh));
851 EHCI_INST(controller)->dummy_qh->horiz_link_ptr = QH_TERMINATE;
Julius Werner4610f0e2013-08-22 16:24:09 -0700852 EHCI_INST(controller)->dummy_qh->td.next_qtd = QH_TERMINATE;
853 EHCI_INST(controller)->dummy_qh->td.alt_next_qtd = QH_TERMINATE;
Nico Huber62eb5b32012-05-25 10:09:13 +0200854 for (i = 0; i < 1024; ++i)
Nico Huber3ca35ca2012-06-14 13:27:39 +0200855 periodic_list[i] = virt_to_phys(EHCI_INST(controller)->dummy_qh)
856 | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200857
858 /* Make sure periodic schedule is disabled */
859 ehci_set_periodic_schedule(EHCI_INST(controller), 0);
860 /* Set periodic frame list pointer */
861 EHCI_INST(controller)->operation->periodiclistbase =
862 virt_to_phys(periodic_list);
863 /* Enable use of periodic schedule */
864 ehci_set_periodic_schedule(EHCI_INST(controller), 1);
865
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000866 /* TODO lots of stuff missing */
867
868 controller->devices[0]->controller = controller;
869 controller->devices[0]->init = ehci_rh_init;
870 controller->devices[0]->init (controller->devices[0]);
871
872 return controller;
873}
Stefan Reinauer8992e532013-05-02 16:16:41 -0700874
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700875#ifdef CONFIG_LP_USB_PCI
Stefan Reinauer8992e532013-05-02 16:16:41 -0700876hci_t *
877ehci_pci_init (pcidev_t addr)
878{
879 hci_t *controller;
880 u32 reg_base;
881
882 u32 pci_command = pci_read_config32(addr, PCI_COMMAND);
883 pci_command = (pci_command | PCI_COMMAND_MEMORY) & ~PCI_COMMAND_IO ;
884 pci_write_config32(addr, PCI_COMMAND, pci_command);
885
886 reg_base = pci_read_config32 (addr, USBBASE);
887
888 /* default value for frame length adjust */
889 pci_write_config8(addr, FLADJ, FLADJ_framelength(60000));
890
Nico Huber6e230662014-07-07 16:33:59 +0200891 controller = ehci_init((unsigned long)reg_base);
Stefan Reinauer8992e532013-05-02 16:16:41 -0700892
893 return controller;
894}
895#endif