blob: b83712c4636426d6336559a9a2c2fd0ea0de75b8 [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>
Julius Werner509c37e2013-08-28 12:29:28 -070033#include <arch/cache.h>
Patrick Georgi7f43dc12010-09-25 17:01:13 +000034#include "ehci.h"
35#include "ehci_private.h"
36
Stefan Reinauer528b43d2011-04-14 19:52:04 +000037static void dump_td(u32 addr)
38{
Patrick Georgi7f43dc12010-09-25 17:01:13 +000039 qtd_t *td = phys_to_virt(addr);
Anton Kochkovc07fb152012-12-19 12:32:32 +040040 usb_debug("+---------------------------------------------------+\n");
41 if (((td->token & (3UL << 8)) >> 8) == 2)
42 usb_debug("|..[SETUP]..........................................|\n");
43 else if (((td->token & (3UL << 8)) >> 8) == 1)
44 usb_debug("|..[IN].............................................|\n");
45 else if (((td->token & (3UL << 8)) >> 8) == 0)
46 usb_debug("|..[OUT]............................................|\n");
47 else
48 usb_debug("|..[]...............................................|\n");
49 usb_debug("|:|============ EHCI TD at [0x%08lx] ==========|:|\n", addr);
50 usb_debug("|:| ERRORS = [%ld] | TOKEN = [0x%08lx] | |:|\n",
51 3 - ((td->token & QTD_CERR_MASK) >> QTD_CERR_SHIFT), td->token);
52 usb_debug("|:+-----------------------------------------------+:|\n");
53 usb_debug("|:| Next qTD [0x%08lx] |:|\n", td->next_qtd);
54 usb_debug("|:+-----------------------------------------------+:|\n");
55 usb_debug("|:| Alt. Next qTD [0x%08lx] |:|\n", td->alt_next_qtd);
56 usb_debug("|:+-----------------------------------------------+:|\n");
Julius Werner8e7a9e12013-10-29 11:31:40 -070057 usb_debug("|:| | Bytes to Transfer |[%05ld] |:|\n", (td->token & QTD_TOTAL_LEN_MASK) >> 16);
Anton Kochkovc07fb152012-12-19 12:32:32 +040058 usb_debug("|:| | PID CODE: | [%ld] |:|\n", (td->token & (3UL << 8)) >> 8);
59 usb_debug("|:| | Interrupt On Complete (IOC) | [%ld] |:|\n", (td->token & (1UL << 15)) >> 15);
60 usb_debug("|:| | Status Active | [%ld] |:|\n", (td->token & (1UL << 7)) >> 7);
61 usb_debug("|:| | Status Halted | [%ld] |:|\n", (td->token & (1UL << 6)) >> 6);
62 usb_debug("|:| TOKEN | Status Data Buffer Error | [%ld] |:|\n", (td->token & (1UL << 5)) >> 5);
63 usb_debug("|:| | Status Babble detected | [%ld] |:|\n", (td->token & (1UL << 4)) >> 4);
64 usb_debug("|:| | Status Transaction Error | [%ld] |:|\n", (td->token & (1UL << 3)) >> 3);
65 usb_debug("|:| | Status Missed Micro Frame | [%ld] |:|\n", (td->token & (1UL << 2)) >> 2);
66 usb_debug("|:| | Split Transaction State | [%ld] |:|\n", (td->token & (1UL << 1)) >> 1);
67 usb_debug("|:| | Ping State | [%ld] |:|\n", td->token & 1UL);
68 usb_debug("|:|-----------------------------------------------|:|\n");
69 usb_debug("|...................................................|\n");
70 usb_debug("+---------------------------------------------------+\n");
71}
72
Stefan Reinauer8992e532013-05-02 16:16:41 -070073#if 0 && defined(USB_DEBUG)
Anton Kochkovc07fb152012-12-19 12:32:32 +040074static void dump_qh(ehci_qh_t *cur)
75{
76 qtd_t *tmp_qtd = NULL;
77 usb_debug("+===================================================+\n");
78 usb_debug("| ############# EHCI QH at [0x%08lx] ########### |\n", virt_to_phys(cur));
79 usb_debug("+---------------------------------------------------+\n");
80 usb_debug("| Horizonal Link Pointer [0x%08lx] |\n", cur->horiz_link_ptr);
81 usb_debug("+------------------[ 0x%08lx ]-------------------+\n", cur->epchar);
82 usb_debug("| | Maximum Packet Length | [%04ld] |\n", ((cur->epchar & (0x7ffUL << 16)) >> 16));
83 usb_debug("| | Device Address | [%ld] |\n", cur->epchar & 0x7F);
84 usb_debug("| | Inactivate on Next Transaction | [%ld] |\n", ((cur->epchar & (1UL << 7)) >> 7));
85 usb_debug("| | Endpoint Number | [%ld] |\n", ((cur->epchar & (0xFUL << 8)) >> 8));
86 usb_debug("| EPCHAR | Endpoint Speed | [%ld] |\n", ((cur->epchar & (3UL << 12)) >> 12));
87 usb_debug("| | Data Toggle Control | [%ld] |\n", ((cur->epchar & (1UL << 14)) >> 14));
88 usb_debug("| | Head of Reclamation List Flag | [%ld] |\n", ((cur->epchar & (1UL << 15)) >> 15));
89 usb_debug("| | Control Endpoint Flag | [%ld] |\n", ((cur->epchar & (1UL << 27)) >> 27));
90 usb_debug("| | Nak Count Reload | [%ld] |\n", ((cur->epchar & (0xFUL << 28)) >> 28));
91 if (((cur->epchar & (1UL << QH_NON_HS_CTRL_EP_SHIFT)) >> QH_NON_HS_CTRL_EP_SHIFT) == 1) { /* Split transaction */
92 usb_debug("+--------+---------[ 0x%08lx ]----------+--------+\n", cur->epcaps);
93 usb_debug("| | Hub Port | [%ld] |\n", ((cur->epcaps & (0x7FUL << 23)) >> 23)); /* [29:23] */
94 usb_debug("| | Hub Address | [%ld] |\n", ((cur->epcaps & (0x7FUL << 16)) >> 16)); /* [22:16] */
95 }
96 usb_debug("+---------------------------------------------------+\n");
97 usb_debug("| Current QTD [0x%08lx] |\n", cur->current_td_ptr);
98
99 if (!((cur->horiz_link_ptr == 0) && (cur->epchar == 0))) {
100 /* Dump overlay QTD for this QH */
101 usb_debug("+---------------------------------------------------+\n");
102 usb_debug("|::::::::::::::::::: QTD OVERLAY :::::::::::::::::::|\n");
103 dump_td(virt_to_phys((void *)&(cur->td)));
104 /* Dump all TD tree for this QH */
105 tmp_qtd = (qtd_t *)phys_to_virt((cur->td.next_qtd & ~0x1FUL));
106 if (tmp_qtd != NULL)
107 usb_debug("|:::::::::::::::::: EHCI QTD CHAIN :::::::::::::::::|\n");
108 while (tmp_qtd != NULL)
109 {
110 dump_td(virt_to_phys(tmp_qtd));
111 tmp_qtd = (qtd_t *)phys_to_virt((tmp_qtd->next_qtd & ~0x1FUL));
112 }
113 usb_debug("|:::::::::::::::: EOF EHCI QTD CHAIN :::::::::::::::|\n");
114 usb_debug("+---------------------------------------------------+\n");
115 } else {
116 usb_debug("+---------------------------------------------------+\n");
117 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000118}
Stefan Reinauerd5c79f92013-03-25 15:56:08 -0700119#endif
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000120
121static void ehci_start (hci_t *controller)
122{
Patrick Georgi8fa27872011-11-24 13:19:57 +0100123 EHCI_INST(controller)->operation->usbcmd |= HC_OP_RS;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000124}
125
126static void ehci_stop (hci_t *controller)
127{
Patrick Georgi8fa27872011-11-24 13:19:57 +0100128 EHCI_INST(controller)->operation->usbcmd &= ~HC_OP_RS;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000129}
130
131static void ehci_reset (hci_t *controller)
132{
Anton Kochkov2fef58e2012-11-09 14:06:05 +0100133 short count = 0;
134 ehci_stop(controller);
135 /* wait 10 ms just to be shure */
136 mdelay(10);
137 if (EHCI_INST(controller)->operation->usbsts & HC_OP_HC_HALTED) {
138 EHCI_INST(controller)->operation->usbcmd = HC_OP_HC_RESET;
139 /* wait 100 ms */
140 for (count = 0; count < 10; count++) {
141 mdelay(10);
142 if (!(EHCI_INST(controller)->operation->usbcmd & HC_OP_HC_RESET)) {
143 return;
144 }
145 }
146 }
147 usb_debug("ehci_reset(): reset failed!\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000148}
149
Nico Huber6e711c62012-11-12 16:20:32 +0100150static void ehci_reinit (hci_t *controller)
151{
152}
153
Nico Huber62eb5b32012-05-25 10:09:13 +0200154static int ehci_set_periodic_schedule(ehci_t *ehcic, int enable)
155{
156 /* Set periodic schedule status. */
157 if (enable)
158 ehcic->operation->usbcmd |= HC_OP_PERIODIC_SCHED_EN;
159 else
160 ehcic->operation->usbcmd &= ~HC_OP_PERIODIC_SCHED_EN;
161 /* Wait for the controller to accept periodic schedule status.
162 * This shouldn't take too long, but we should timeout nevertheless.
163 */
164 enable = enable ? HC_OP_PERIODIC_SCHED_STAT : 0;
Gabe Blackef650a52012-11-21 02:16:13 -0800165 int timeout = 100000; /* time out after 100ms */
Nico Huber62eb5b32012-05-25 10:09:13 +0200166 while (((ehcic->operation->usbsts & HC_OP_PERIODIC_SCHED_STAT) != enable)
167 && timeout--)
Gabe Blackef650a52012-11-21 02:16:13 -0800168 udelay(1);
Nico Huber62eb5b32012-05-25 10:09:13 +0200169 if (timeout < 0) {
Gabe Black93ded592012-11-01 15:44:10 -0700170 usb_debug("ehci periodic schedule status change timed out.\n");
Nico Huber62eb5b32012-05-25 10:09:13 +0200171 return 1;
172 }
173 return 0;
174}
175
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000176static void ehci_shutdown (hci_t *controller)
177{
Mathias Krause7a9da712013-03-24 19:40:01 +0100178 detach_controller(controller);
Julius Werner7234d602014-04-08 12:54:25 -0700179
Nico Huber62eb5b32012-05-25 10:09:13 +0200180 /* Make sure periodic schedule is disabled */
181 ehci_set_periodic_schedule(EHCI_INST(controller), 0);
Nico Huber62eb5b32012-05-25 10:09:13 +0200182
Julius Werner7234d602014-04-08 12:54:25 -0700183 /* Give all ports back to companion controller */
Patrick Georgi01178bb2011-11-04 11:57:46 +0100184 EHCI_INST(controller)->operation->configflag = 0;
Nico Huberd633dda2012-11-12 15:35:44 +0100185
Julius Werner7234d602014-04-08 12:54:25 -0700186 /* Free all dynamic allocations */
187 free(EHCI_INST(controller)->dma_buffer);
188 free(phys_to_virt(EHCI_INST(controller)->operation->periodiclistbase));
189 free((void *)EHCI_INST(controller)->dummy_qh);
Nico Huberd633dda2012-11-12 15:35:44 +0100190 free(EHCI_INST(controller));
191 free(controller);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000192}
193
194enum { EHCI_OUT=0, EHCI_IN=1, EHCI_SETUP=2 };
195
Nico Huber1ab60752012-05-23 09:21:54 +0200196/*
197 * returns the address of the closest USB2.0 hub, which is responsible for
198 * split transactions, along with the number of the used downstream port
199 */
200static int closest_usb2_hub(const usbdev_t *dev, int *const addr, int *const port)
201{
202 const usbdev_t *usb1dev;
203 do {
204 usb1dev = dev;
205 if ((dev->hub > 0) && (dev->hub < 128))
206 dev = dev->controller->devices[dev->hub];
207 else
208 dev = NULL;
209 } while (dev && (dev->speed < 2));
210 if (dev) {
211 *addr = usb1dev->hub;
212 *port = usb1dev->port;
213 return 0;
214 } else {
Gabe Black93ded592012-11-01 15:44:10 -0700215 usb_debug("ehci: Couldn't find closest USB2.0 hub.\n");
Nico Huber1ab60752012-05-23 09:21:54 +0200216 return 1;
217 }
218}
219
Patrick Georgi8fa27872011-11-24 13:19:57 +0100220/* returns handled bytes. assumes that the fields it writes are empty on entry */
221static int fill_td(qtd_t *td, void* data, int datalen)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000222{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000223 u32 total_len = 0;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100224 u32 page_no = 0;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000225
226 u32 start = virt_to_phys(data);
227 u32 page = start & ~4095;
228 u32 offset = start & 4095;
229 u32 page_len = 4096 - offset;
230
Patrick Georgi8fa27872011-11-24 13:19:57 +0100231 td->token |= 0 << QTD_CPAGE_SHIFT;
232 td->bufptrs[page_no++] = start;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000233
234 if (datalen <= page_len) {
235 total_len = datalen;
236 } else {
237 datalen -= page_len;
238 total_len += page_len;
239
Patrick Georgi8fa27872011-11-24 13:19:57 +0100240 while (page_no < 5) {
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000241 /* we have a continguous mapping between virtual and physical memory */
242 page += 4096;
243
Patrick Georgi8fa27872011-11-24 13:19:57 +0100244 td->bufptrs[page_no++] = page;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000245 if (datalen <= 4096) {
246 total_len += datalen;
247 break;
248 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000249 datalen -= 4096;
250 total_len += 4096;
Julius Werner69eea7c2013-01-11 16:25:52 -0800251
252 /* end TD at a packet boundary if transfer not complete */
253 if (page_no == 5)
254 total_len &= ~511;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100255 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000256 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100257 td->token |= total_len << QTD_TOTAL_LEN_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000258 return total_len;
259}
260
261/* free up data structures */
Patrick Georgi8fa27872011-11-24 13:19:57 +0100262static void free_qh_and_tds(ehci_qh_t *qh, qtd_t *cur)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000263{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000264 qtd_t *next;
265 while (cur) {
266 next = (qtd_t*)phys_to_virt(cur->next_qtd & ~31);
Nico Huber4f83d1b2012-11-23 11:58:57 +0100267 free((void *)cur);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000268 cur = next;
269 }
Nico Huber4f83d1b2012-11-23 11:58:57 +0100270 free((void *)qh);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000271}
272
Patrick Georgi8fa27872011-11-24 13:19:57 +0100273static int wait_for_tds(qtd_t *head)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000274{
Julius Wernere9738db2013-02-21 13:41:40 -0800275 /* returns the amount of bytes *not* transmitted, or -1 for error */
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000276 int result = 0;
277 qtd_t *cur = head;
278 while (1) {
279 if (0) dump_td(virt_to_phys(cur));
Nico Huber0421dc82012-05-21 14:53:43 +0200280
281 /* wait for results */
Nico Hubercef86922012-06-01 08:54:29 +0200282 /* how long to wait?
283 * tested with some USB2.0 flash sticks:
284 * TUR turn around took
Julius Werner52e3f442013-07-23 12:45:18 -0700285 * about 2.2s for the slowest (13fe:3800)
Nico Hubercef86922012-06-01 08:54:29 +0200286 * max. 250ms for the others
287 * slowest non-TUR turn around took about 1.3s
Julius Werner52e3f442013-07-23 12:45:18 -0700288 * set to 3s to be safe as a failed TUR can be fatal
Nico Huber0421dc82012-05-21 14:53:43 +0200289 */
Julius Werner52e3f442013-07-23 12:45:18 -0700290 int timeout = 60000; /* time out after 60000 * 50us == 3s */
Nico Huber0421dc82012-05-21 14:53:43 +0200291 while ((cur->token & QTD_ACTIVE) && !(cur->token & QTD_HALTED)
292 && timeout--)
293 udelay(50);
294 if (timeout < 0) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700295 usb_debug("Error: ehci: queue transfer "
Nico Huber0421dc82012-05-21 14:53:43 +0200296 "processing timed out.\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800297 return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200298 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100299 if (cur->token & QTD_HALTED) {
Nico Huber86c686a2012-11-21 16:22:26 +0100300 usb_debug("ERROR with packet\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000301 dump_td(virt_to_phys(cur));
Gabe Black93ded592012-11-01 15:44:10 -0700302 usb_debug("-----------------\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800303 return -1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000304 }
Julius Wernere9738db2013-02-21 13:41:40 -0800305 result += (cur->token & QTD_TOTAL_LEN_MASK)
306 >> QTD_TOTAL_LEN_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000307 if (cur->next_qtd & 1) {
Julius Wernere9738db2013-02-21 13:41:40 -0800308 break;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000309 }
310 if (0) dump_td(virt_to_phys(cur));
311 /* helps debugging the TD chain */
Gabe Black93ded592012-11-01 15:44:10 -0700312 if (0) usb_debug("\nmoving from %x to %x\n", cur, phys_to_virt(cur->next_qtd));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000313 cur = phys_to_virt(cur->next_qtd);
314 }
315 return result;
316}
317
Nico Huber0421dc82012-05-21 14:53:43 +0200318static int ehci_set_async_schedule(ehci_t *ehcic, int enable)
319{
320 /* Set async schedule status. */
321 if (enable)
322 ehcic->operation->usbcmd |= HC_OP_ASYNC_SCHED_EN;
323 else
324 ehcic->operation->usbcmd &= ~HC_OP_ASYNC_SCHED_EN;
325 /* Wait for the controller to accept async schedule status.
326 * This shouldn't take too long, but we should timeout nevertheless.
327 */
328 enable = enable ? HC_OP_ASYNC_SCHED_STAT : 0;
329 int timeout = 100; /* time out after 100ms */
330 while (((ehcic->operation->usbsts & HC_OP_ASYNC_SCHED_STAT) != enable)
331 && timeout--)
332 mdelay(1);
333 if (timeout < 0) {
Gabe Black93ded592012-11-01 15:44:10 -0700334 usb_debug("ehci async schedule status change timed out.\n");
Nico Huber0421dc82012-05-21 14:53:43 +0200335 return 1;
336 }
337 return 0;
338}
339
340static int ehci_process_async_schedule(
341 ehci_t *ehcic, ehci_qh_t *qhead, qtd_t *head)
342{
343 int result;
344
345 /* make sure async schedule is disabled */
Julius Wernere9738db2013-02-21 13:41:40 -0800346 if (ehci_set_async_schedule(ehcic, 0)) return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200347
348 /* hook up QH */
349 ehcic->operation->asynclistaddr = virt_to_phys(qhead);
350
351 /* start async schedule */
Julius Wernere9738db2013-02-21 13:41:40 -0800352 if (ehci_set_async_schedule(ehcic, 1)) return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200353
354 /* wait for result */
355 result = wait_for_tds(head);
356
357 /* disable async schedule */
358 ehci_set_async_schedule(ehcic, 0);
359
360 return result;
361}
362
Julius Werner509c37e2013-08-28 12:29:28 -0700363static int ehci_bulk (endpoint_t *ep, int size, u8 *src, int finalize)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000364{
365 int result = 0;
Julius Werner509c37e2013-08-28 12:29:28 -0700366 u8 *end = src + size;
Julius Wernere9738db2013-02-21 13:41:40 -0800367 int remaining = size;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000368 int endp = ep->endpoint & 0xf;
369 int pid = (ep->direction==IN)?EHCI_IN:EHCI_OUT;
370
Nico Huber1ab60752012-05-23 09:21:54 +0200371 int hubaddr = 0, hubport = 0;
372 if (ep->dev->speed < 2) {
373 /* we need a split transaction */
374 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
Julius Wernere9738db2013-02-21 13:41:40 -0800375 return -1;
Nico Huber1ab60752012-05-23 09:21:54 +0200376 }
377
Julius Werner509c37e2013-08-28 12:29:28 -0700378 if (!dma_coherent(src)) {
379 end = EHCI_INST(ep->dev->controller)->dma_buffer + size;
380 if (size > DMA_SIZE) {
381 usb_debug("EHCI bulk transfer too large for DMA buffer: %d\n", size);
382 return -1;
383 }
384 if (pid == EHCI_OUT)
385 memcpy(end - size, src, size);
386 }
387
388 ehci_qh_t *qh = dma_memalign(64, sizeof(ehci_qh_t));
389 qtd_t *head = dma_memalign(64, sizeof(qtd_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000390 qtd_t *cur = head;
Julius Werner509c37e2013-08-28 12:29:28 -0700391 if (!qh || !head)
392 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000393 while (1) {
Nico Huber4f83d1b2012-11-23 11:58:57 +0100394 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100395 cur->token = QTD_ACTIVE |
396 (pid << QTD_PID_SHIFT) |
397 (0 << QTD_CERR_SHIFT);
Julius Werner509c37e2013-08-28 12:29:28 -0700398 remaining -= fill_td(cur, end - remaining, remaining);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000399
Patrick Georgi8fa27872011-11-24 13:19:57 +0100400 cur->alt_next_qtd = QTD_TERMINATE;
Julius Werner509c37e2013-08-28 12:29:28 -0700401 if (remaining <= 0) {
Patrick Georgi8fa27872011-11-24 13:19:57 +0100402 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000403 break;
404 } else {
Julius Werner509c37e2013-08-28 12:29:28 -0700405 qtd_t *next = dma_memalign(64, sizeof(qtd_t));
406 if (!next)
407 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000408 cur->next_qtd = virt_to_phys(next);
409 cur = next;
410 }
411 }
412
413 /* create QH */
Nico Huber4f83d1b2012-11-23 11:58:57 +0100414 memset((void *)qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100415 qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH;
416 qh->epchar = ep->dev->address |
417 (endp << QH_EP_SHIFT) |
418 (ep->dev->speed << QH_EPS_SHIFT) |
419 (0 << QH_DTC_SHIFT) |
420 (1 << QH_RECLAIM_HEAD_SHIFT) |
421 (ep->maxpacketsize << QH_MPS_SHIFT) |
422 (0 << QH_NAK_CNT_SHIFT);
Nico Huber1ab60752012-05-23 09:21:54 +0200423 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
424 (hubport << QH_PORT_NUMBER_SHIFT) |
425 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000426
427 qh->td.next_qtd = virt_to_phys(head);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100428 qh->td.token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
429 head->token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000430
Nico Huber0421dc82012-05-21 14:53:43 +0200431 result = ehci_process_async_schedule(
432 EHCI_INST(ep->dev->controller), qh, head);
Julius Werner509c37e2013-08-28 12:29:28 -0700433 if (result >= 0) {
Julius Wernere9738db2013-02-21 13:41:40 -0800434 result = size - result;
Julius Werner509c37e2013-08-28 12:29:28 -0700435 if (pid == EHCI_IN && end != src + size)
436 memcpy(src, end - size, result);
437 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000438
Patrick Georgi8fa27872011-11-24 13:19:57 +0100439 ep->toggle = (cur->token & QTD_TOGGLE_MASK) >> QTD_TOGGLE_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000440
441 free_qh_and_tds(qh, head);
Julius Wernere9738db2013-02-21 13:41:40 -0800442
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000443 return result;
Julius Werner509c37e2013-08-28 12:29:28 -0700444
445oom:
446 usb_debug("Not enough DMA memory for EHCI control structures!\n");
447 free_qh_and_tds(qh, head);
448 return -1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000449}
450
451
452/* FIXME: Handle control transfers as 3 QHs, so the 2nd stage can be >0x4000 bytes */
Julius Werner509c37e2013-08-28 12:29:28 -0700453static int ehci_control (usbdev_t *dev, direction_t dir, int drlen, void *setup,
454 int dalen, u8 *src)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000455{
Julius Werner509c37e2013-08-28 12:29:28 -0700456 u8 *data = src;
457 u8 *devreq = setup;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000458 int endp = 0; // this is control. always 0 (for now)
459 int toggle = 0;
460 int mlen = dev->endpoints[0].maxpacketsize;
461 int result = 0;
462
Nico Huber1ab60752012-05-23 09:21:54 +0200463 int hubaddr = 0, hubport = 0, non_hs_ctrl_ep = 0;
464 if (dev->speed < 2) {
465 /* we need a split transaction */
466 if (closest_usb2_hub(dev, &hubaddr, &hubport))
Julius Wernere9738db2013-02-21 13:41:40 -0800467 return -1;
Nico Huber1ab60752012-05-23 09:21:54 +0200468 non_hs_ctrl_ep = 1;
469 }
470
Julius Werner509c37e2013-08-28 12:29:28 -0700471 if (!dma_coherent(setup)) {
472 devreq = EHCI_INST(dev->controller)->dma_buffer;
473 memcpy(devreq, setup, drlen);
474 }
475 if (dalen > 0 && !dma_coherent(src)) {
476 data = EHCI_INST(dev->controller)->dma_buffer + drlen;
477 if (drlen + dalen > DMA_SIZE) {
478 usb_debug("EHCI control transfer too large for DMA buffer: %d\n", drlen + dalen);
479 return -1;
480 }
481 if (dir == OUT)
482 memcpy(data, src, dalen);
483 }
484
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000485 /* create qTDs */
Julius Werner509c37e2013-08-28 12:29:28 -0700486 qtd_t *head = dma_memalign(64, sizeof(qtd_t));
487 ehci_qh_t *qh = dma_memalign(64, sizeof(ehci_qh_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000488 qtd_t *cur = head;
Julius Werner509c37e2013-08-28 12:29:28 -0700489 if (!qh || !head)
490 goto oom;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100491 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100492 cur->token = QTD_ACTIVE |
493 (toggle?QTD_TOGGLE_DATA1:0) |
494 (EHCI_SETUP << QTD_PID_SHIFT) |
495 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000496 if (fill_td(cur, devreq, drlen) != drlen) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700497 usb_debug("ERROR: couldn't send the entire device request\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000498 }
Julius Werner509c37e2013-08-28 12:29:28 -0700499 qtd_t *next = dma_memalign(64, sizeof(qtd_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000500 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100501 cur->alt_next_qtd = QTD_TERMINATE;
Julius Werner509c37e2013-08-28 12:29:28 -0700502 if (!next)
503 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000504
505 /* FIXME: We're limited to 16-20K (depending on alignment) for payload for now.
506 * Figure out, how toggle can be set sensibly in this scenario */
507 if (dalen > 0) {
508 toggle ^= 1;
509 cur = next;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100510 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100511 cur->token = QTD_ACTIVE |
512 (toggle?QTD_TOGGLE_DATA1:0) |
513 (((dir == OUT)?EHCI_OUT:EHCI_IN) << QTD_PID_SHIFT) |
514 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000515 if (fill_td(cur, data, dalen) != dalen) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700516 usb_debug("ERROR: couldn't send the entire control payload\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000517 }
Julius Werner509c37e2013-08-28 12:29:28 -0700518 next = dma_memalign(64, sizeof(qtd_t));
519 if (!next)
520 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000521 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100522 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000523 }
524
525 toggle = 1;
526 cur = next;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100527 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100528 cur->token = QTD_ACTIVE |
529 (toggle?QTD_TOGGLE_DATA1:QTD_TOGGLE_DATA0) |
530 ((dir == OUT)?EHCI_IN:EHCI_OUT) << QTD_PID_SHIFT |
531 (0 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000532 fill_td(cur, NULL, 0);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100533 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
534 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000535
536 /* create QH */
Nico Huber4f83d1b2012-11-23 11:58:57 +0100537 memset((void *)qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100538 qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH;
539 qh->epchar = dev->address |
540 (endp << QH_EP_SHIFT) |
541 (dev->speed << QH_EPS_SHIFT) |
542 (1 << QH_DTC_SHIFT) | /* ctrl transfers are special: take toggle bit from TD */
543 (1 << QH_RECLAIM_HEAD_SHIFT) |
544 (mlen << QH_MPS_SHIFT) |
Nico Huber1ab60752012-05-23 09:21:54 +0200545 (non_hs_ctrl_ep << QH_NON_HS_CTRL_EP_SHIFT) |
Patrick Georgi8fa27872011-11-24 13:19:57 +0100546 (0 << QH_NAK_CNT_SHIFT);
Nico Huber1ab60752012-05-23 09:21:54 +0200547 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
548 (hubport << QH_PORT_NUMBER_SHIFT) |
549 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000550 qh->td.next_qtd = virt_to_phys(head);
551
Nico Huber0421dc82012-05-21 14:53:43 +0200552 result = ehci_process_async_schedule(
553 EHCI_INST(dev->controller), qh, head);
Julius Werner509c37e2013-08-28 12:29:28 -0700554 if (result >= 0) {
Julius Wernere9738db2013-02-21 13:41:40 -0800555 result = dalen - result;
Julius Werner509c37e2013-08-28 12:29:28 -0700556 if (dir == IN && data != src)
557 memcpy(src, data, result);
558 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000559
560 free_qh_and_tds(qh, head);
561 return result;
Julius Werner509c37e2013-08-28 12:29:28 -0700562
563oom:
564 usb_debug("Not enough DMA memory for EHCI control structures!\n");
565 free_qh_and_tds(qh, head);
566 return -1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000567}
568
Nico Huber62eb5b32012-05-25 10:09:13 +0200569
570typedef struct _intr_qtd_t intr_qtd_t;
571
572struct _intr_qtd_t {
573 volatile qtd_t td;
574 u8 *data;
575 intr_qtd_t *next;
576};
577
578typedef struct {
579 volatile ehci_qh_t qh;
580 intr_qtd_t *head;
581 intr_qtd_t *tail;
Nico Huber0d120f82012-06-14 13:08:36 +0200582 intr_qtd_t *spare;
Nico Huber62eb5b32012-05-25 10:09:13 +0200583 u8 *data;
584 endpoint_t *endp;
585 int reqsize;
586} intr_queue_t;
587
588static void fill_intr_queue_td(
589 intr_queue_t *const intrq,
590 intr_qtd_t *const intr_qtd,
591 u8 *const data)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000592{
Nico Huber62eb5b32012-05-25 10:09:13 +0200593 const int pid = (intrq->endp->direction == IN) ? EHCI_IN
594 : (intrq->endp->direction == OUT) ? EHCI_OUT
595 : EHCI_SETUP;
596 const int cerr = (intrq->endp->dev->speed < 2) ? 1 : 0;
597
598 memset(intr_qtd, 0, sizeof(*intr_qtd));
599 intr_qtd->td.next_qtd = QTD_TERMINATE;
600 intr_qtd->td.alt_next_qtd = QTD_TERMINATE;
601 intr_qtd->td.token = QTD_ACTIVE |
602 (pid << QTD_PID_SHIFT) |
603 (cerr << QTD_CERR_SHIFT) |
604 ((intrq->endp->toggle & 1) << QTD_TOGGLE_SHIFT);
605 fill_td(&intr_qtd->td, data, intrq->reqsize);
606 intr_qtd->data = data;
607 intr_qtd->next = NULL;
608
609 intrq->endp->toggle ^= 1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000610}
611
Nico Huber62eb5b32012-05-25 10:09:13 +0200612static void ehci_destroy_intr_queue(endpoint_t *const, void *const);
613
614static void *ehci_create_intr_queue(
615 endpoint_t *const ep,
616 const int reqsize,
617 int reqcount,
618 const int reqtiming)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000619{
Nico Huber62eb5b32012-05-25 10:09:13 +0200620 int i;
621
622 if ((reqsize > (4 * 4096 + 1)) || /* the maximum for arbitrary aligned
623 data in five 4096 byte pages */
624 (reqtiming > 1024))
625 return NULL;
626 if (reqcount < 2) /* we need at least 2:
627 one for processing, one for the hc to advance to */
628 reqcount = 2;
629
630 int hubaddr = 0, hubport = 0;
631 if (ep->dev->speed < 2) {
632 /* we need a split transaction */
633 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
634 return NULL;
635 }
636
Julius Werner509c37e2013-08-28 12:29:28 -0700637 intr_queue_t *const intrq = (intr_queue_t *)malloc(sizeof(intr_queue_t));
Nico Huber0d120f82012-06-14 13:08:36 +0200638 /*
639 * reqcount data chunks
640 * plus one more spare, which we'll leave out of queue
641 */
Julius Werner509c37e2013-08-28 12:29:28 -0700642 u8 *data = (u8 *)dma_malloc(reqsize * (reqcount + 1));
Nico Huber62eb5b32012-05-25 10:09:13 +0200643 if (!intrq || !data)
644 fatal("Not enough memory to create USB interrupt queue.\n");
645 intrq->data = data;
646 intrq->endp = ep;
647 intrq->reqsize = reqsize;
648
649 /* create #reqcount transfer descriptors (qTDs) */
Julius Werner509c37e2013-08-28 12:29:28 -0700650 intrq->head = (intr_qtd_t *)dma_memalign(64, sizeof(intr_qtd_t));
Nico Huber62eb5b32012-05-25 10:09:13 +0200651 intr_qtd_t *cur_td = intrq->head;
652 for (i = 0; i < reqcount; ++i) {
653 fill_intr_queue_td(intrq, cur_td, data);
654 data += reqsize;
655 if (i < reqcount - 1) {
656 /* create one more qTD */
657 intr_qtd_t *const next_td =
Julius Werner509c37e2013-08-28 12:29:28 -0700658 (intr_qtd_t *)dma_memalign(64, sizeof(intr_qtd_t));
Nico Huber62eb5b32012-05-25 10:09:13 +0200659 cur_td->td.next_qtd = virt_to_phys(&next_td->td);
660 cur_td->next = next_td;
661 cur_td = next_td;
662 }
663 }
664 intrq->tail = cur_td;
665
Nico Huber0d120f82012-06-14 13:08:36 +0200666 /* create spare qTD */
Julius Werner509c37e2013-08-28 12:29:28 -0700667 intrq->spare = (intr_qtd_t *)dma_memalign(64, sizeof(intr_qtd_t));
Nico Huber0d120f82012-06-14 13:08:36 +0200668 fill_intr_queue_td(intrq, intrq->spare, data);
669
Nico Huber62eb5b32012-05-25 10:09:13 +0200670 /* initialize QH */
671 const int endp = ep->endpoint & 0xf;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100672 memset((void *)&intrq->qh, 0, sizeof(intrq->qh));
Nico Huber62eb5b32012-05-25 10:09:13 +0200673 intrq->qh.horiz_link_ptr = PS_TERMINATE;
674 intrq->qh.epchar = ep->dev->address |
675 (endp << QH_EP_SHIFT) |
676 (ep->dev->speed << QH_EPS_SHIFT) |
677 (1 << QH_DTC_SHIFT) |
678 (0 << QH_RECLAIM_HEAD_SHIFT) |
679 (ep->maxpacketsize << QH_MPS_SHIFT) |
680 (0 << QH_NAK_CNT_SHIFT);
681 intrq->qh.epcaps = (1 << QH_PIPE_MULTIPLIER_SHIFT) |
682 (hubport << QH_PORT_NUMBER_SHIFT) |
683 (hubaddr << QH_HUB_ADDRESS_SHIFT) |
684 (0xfe << QH_UFRAME_CMASK_SHIFT) |
685 1 /* uFrame S-mask */;
686 intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td);
687
688 /* insert QH into periodic schedule */
689 int nothing_placed = 1;
690 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(ep->dev->controller)
691 ->operation->periodiclistbase);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200692 const u32 dummy_ptr = virt_to_phys(EHCI_INST(
693 ep->dev->controller)->dummy_qh) | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200694 for (i = 0; i < 1024; i += reqtiming) {
695 /* advance to the next free position */
Nico Huber3ca35ca2012-06-14 13:27:39 +0200696 while ((i < 1024) && (ps[i] != dummy_ptr)) ++i;
Nico Huber62eb5b32012-05-25 10:09:13 +0200697 if (i < 1024) {
698 ps[i] = virt_to_phys(&intrq->qh) | PS_TYPE_QH;
699 nothing_placed = 0;
700 }
701 }
702 if (nothing_placed) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700703 usb_debug("Error: Failed to place ehci interrupt queue head "
Nico Huber62eb5b32012-05-25 10:09:13 +0200704 "into periodic schedule: no space left\n");
705 ehci_destroy_intr_queue(ep, intrq);
706 return NULL;
707 }
708
709 return intrq;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000710}
711
Nico Huber62eb5b32012-05-25 10:09:13 +0200712static void ehci_destroy_intr_queue(endpoint_t *const ep, void *const queue)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000713{
Nico Huber62eb5b32012-05-25 10:09:13 +0200714 intr_queue_t *const intrq = (intr_queue_t *)queue;
715
716 /* remove QH from periodic schedule */
717 int i;
718 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(
719 ep->dev->controller)->operation->periodiclistbase);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200720 const u32 dummy_ptr = virt_to_phys(EHCI_INST(
721 ep->dev->controller)->dummy_qh) | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200722 for (i = 0; i < 1024; ++i) {
723 if ((ps[i] & PS_PTR_MASK) == virt_to_phys(&intrq->qh))
Nico Huber3ca35ca2012-06-14 13:27:39 +0200724 ps[i] = dummy_ptr;
Nico Huber62eb5b32012-05-25 10:09:13 +0200725 }
726
727 /* wait 1ms for frame to end */
728 mdelay(1);
729
730 while (intrq->head) {
731 /* disable qTD and destroy list */
732 intrq->head->td.next_qtd = QTD_TERMINATE;
733 intrq->head->td.token &= ~QTD_ACTIVE;
734
735 /* save and advance head ptr */
736 intr_qtd_t *const to_free = intrq->head;
737 intrq->head = intrq->head->next;
738
739 /* free current interrupt qTD */
740 free(to_free);
741 }
Nico Huber0d120f82012-06-14 13:08:36 +0200742 free(intrq->spare);
Nico Huber62eb5b32012-05-25 10:09:13 +0200743 free(intrq->data);
744 free(intrq);
745}
746
747static u8 *ehci_poll_intr_queue(void *const queue)
748{
749 intr_queue_t *const intrq = (intr_queue_t *)queue;
750
751 u8 *ret = NULL;
752
753 /* process if head qTD is inactive AND QH has been moved forward */
Nico Huber0d120f82012-06-14 13:08:36 +0200754 if (!(intrq->head->td.token & QTD_ACTIVE)) {
Nico Huber62eb5b32012-05-25 10:09:13 +0200755 if (!(intrq->head->td.token & QTD_STATUS_MASK))
756 ret = intrq->head->data;
757 else
Gabe Black93ded592012-11-01 15:44:10 -0700758 usb_debug("ehci_poll_intr_queue: transfer failed, "
Nico Huber62eb5b32012-05-25 10:09:13 +0200759 "status == 0x%02x\n",
760 intrq->head->td.token & QTD_STATUS_MASK);
761
Nico Huber0d120f82012-06-14 13:08:36 +0200762 /* insert spare qTD at the end and advance our tail ptr */
763 fill_intr_queue_td(intrq, intrq->spare, intrq->spare->data);
764 intrq->tail->td.next_qtd = virt_to_phys(&intrq->spare->td);
765 intrq->tail->next = intrq->spare;
Nico Huber62eb5b32012-05-25 10:09:13 +0200766 intrq->tail = intrq->tail->next;
Nico Huber0d120f82012-06-14 13:08:36 +0200767
768 /* reuse executed qTD as spare one and advance our head ptr */
769 intrq->spare = intrq->head;
770 intrq->head = intrq->head->next;
771 }
772 /* reset queue if we fully processed it after underrun */
Nico Huberaf169f42012-11-22 11:14:03 +0100773 else if ((intrq->qh.td.next_qtd & QTD_TERMINATE) &&
774 /* to prevent race conditions:
775 not our head and not active */
776 (intrq->qh.current_td_ptr !=
777 virt_to_phys(&intrq->head->td)) &&
778 !(intrq->qh.td.token & QTD_ACTIVE)) {
Gabe Black93ded592012-11-01 15:44:10 -0700779 usb_debug("resetting underrun ehci interrupt queue.\n");
Nico Huberaf169f42012-11-22 11:14:03 +0100780 intrq->qh.current_td_ptr = 0;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100781 memset((void *)&intrq->qh.td, 0, sizeof(intrq->qh.td));
Nico Huber0d120f82012-06-14 13:08:36 +0200782 intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td);
Nico Huber62eb5b32012-05-25 10:09:13 +0200783 }
784 return ret;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000785}
786
787hci_t *
Nico Huber6e230662014-07-07 16:33:59 +0200788ehci_init (unsigned long physical_bar)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000789{
790 int i;
791 hci_t *controller = new_controller ();
Julius Werner7234d602014-04-08 12:54:25 -0700792 controller->instance = xzalloc(sizeof (ehci_t));
Anton Kochkov1c36ead2012-06-28 08:30:15 +0400793 controller->type = EHCI;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000794 controller->start = ehci_start;
795 controller->stop = ehci_stop;
796 controller->reset = ehci_reset;
Nico Huber6e711c62012-11-12 16:20:32 +0100797 controller->init = ehci_reinit;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000798 controller->shutdown = ehci_shutdown;
799 controller->bulk = ehci_bulk;
800 controller->control = ehci_control;
Patrick Georgi482af6d2013-05-24 15:48:56 +0200801 controller->set_address = generic_set_address;
802 controller->finish_device_config = NULL;
803 controller->destroy_device = NULL;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000804 controller->create_intr_queue = ehci_create_intr_queue;
805 controller->destroy_intr_queue = ehci_destroy_intr_queue;
806 controller->poll_intr_queue = ehci_poll_intr_queue;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000807 init_device_entry (controller, 0);
808
Julius Werner7234d602014-04-08 12:54:25 -0700809 EHCI_INST(controller)->capabilities = phys_to_virt(controller->reg_base);
810 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 +0000811
Vincent Palatin716375d2012-10-25 17:38:43 -0700812 /* Set the high address word (aka segment) if controller is 64-bit */
813 if (EHCI_INST(controller)->capabilities->hccparams & 1)
814 EHCI_INST(controller)->operation->ctrldssegment = 0;
815
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000816 /* Enable operation of controller */
817 controller->start(controller);
818
819 /* take over all ports. USB1 should be blind now */
820 EHCI_INST(controller)->operation->configflag = 1;
821
Nico Huber62eb5b32012-05-25 10:09:13 +0200822 /* Initialize periodic frame list */
823 /* 1024 32-bit pointers, 4kb aligned */
Julius Werner509c37e2013-08-28 12:29:28 -0700824 u32 *const periodic_list = (u32 *)dma_memalign(4096, 1024 * sizeof(u32));
Nico Huber62eb5b32012-05-25 10:09:13 +0200825 if (!periodic_list)
826 fatal("Not enough memory creating EHCI periodic frame list.\n");
Nico Huber3ca35ca2012-06-14 13:27:39 +0200827
Julius Werner509c37e2013-08-28 12:29:28 -0700828 if (dma_initialized()) {
829 EHCI_INST(controller)->dma_buffer = dma_memalign(4096, DMA_SIZE);
830 if (!EHCI_INST(controller)->dma_buffer)
831 fatal("Not enough DMA memory for EHCI bounce buffer.\n");
832 }
833
Nico Huber3ca35ca2012-06-14 13:27:39 +0200834 /*
835 * Insert dummy QH in periodic frame list
836 * This helps with broken host controllers
837 * and doesn't violate the standard.
838 */
Julius Werner509c37e2013-08-28 12:29:28 -0700839 EHCI_INST(controller)->dummy_qh = (ehci_qh_t *)dma_memalign(64, sizeof(ehci_qh_t));
Nico Huber4f83d1b2012-11-23 11:58:57 +0100840 memset((void *)EHCI_INST(controller)->dummy_qh, 0,
Nico Huber3ca35ca2012-06-14 13:27:39 +0200841 sizeof(*EHCI_INST(controller)->dummy_qh));
842 EHCI_INST(controller)->dummy_qh->horiz_link_ptr = QH_TERMINATE;
Julius Werner4610f0e2013-08-22 16:24:09 -0700843 EHCI_INST(controller)->dummy_qh->td.next_qtd = QH_TERMINATE;
844 EHCI_INST(controller)->dummy_qh->td.alt_next_qtd = QH_TERMINATE;
Nico Huber62eb5b32012-05-25 10:09:13 +0200845 for (i = 0; i < 1024; ++i)
Nico Huber3ca35ca2012-06-14 13:27:39 +0200846 periodic_list[i] = virt_to_phys(EHCI_INST(controller)->dummy_qh)
847 | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200848
849 /* Make sure periodic schedule is disabled */
850 ehci_set_periodic_schedule(EHCI_INST(controller), 0);
851 /* Set periodic frame list pointer */
852 EHCI_INST(controller)->operation->periodiclistbase =
853 virt_to_phys(periodic_list);
854 /* Enable use of periodic schedule */
855 ehci_set_periodic_schedule(EHCI_INST(controller), 1);
856
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000857 /* TODO lots of stuff missing */
858
859 controller->devices[0]->controller = controller;
860 controller->devices[0]->init = ehci_rh_init;
861 controller->devices[0]->init (controller->devices[0]);
862
863 return controller;
864}
Stefan Reinauer8992e532013-05-02 16:16:41 -0700865
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700866#ifdef CONFIG_LP_USB_PCI
Stefan Reinauer8992e532013-05-02 16:16:41 -0700867hci_t *
868ehci_pci_init (pcidev_t addr)
869{
870 hci_t *controller;
871 u32 reg_base;
872
873 u32 pci_command = pci_read_config32(addr, PCI_COMMAND);
874 pci_command = (pci_command | PCI_COMMAND_MEMORY) & ~PCI_COMMAND_IO ;
875 pci_write_config32(addr, PCI_COMMAND, pci_command);
876
877 reg_base = pci_read_config32 (addr, USBBASE);
878
879 /* default value for frame length adjust */
880 pci_write_config8(addr, FLADJ, FLADJ_framelength(60000));
881
Nico Huber6e230662014-07-07 16:33:59 +0200882 controller = ehci_init((unsigned long)reg_base);
Stefan Reinauer8992e532013-05-02 16:16:41 -0700883
884 return controller;
885}
886#endif