blob: 129b9b825e7a70a73d387836eb4adf141afabddf [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");
57 usb_debug("|:| | Bytes to Transfer | [%04ld] |:|\n", (td->token & (0x7FUL << 16)) >> 16);
58 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);
Nico Huber62eb5b32012-05-25 10:09:13 +0200179 /* Make sure periodic schedule is disabled */
180 ehci_set_periodic_schedule(EHCI_INST(controller), 0);
181 /* Free periodic frame list */
182 free(phys_to_virt(EHCI_INST(controller)->operation->periodiclistbase));
183
Nico Huber3ca35ca2012-06-14 13:27:39 +0200184 /* Free dummy QH */
Nico Huber4f83d1b2012-11-23 11:58:57 +0100185 free((void *)EHCI_INST(controller)->dummy_qh);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200186
Patrick Georgi01178bb2011-11-04 11:57:46 +0100187 EHCI_INST(controller)->operation->configflag = 0;
Nico Huberd633dda2012-11-12 15:35:44 +0100188
189 free(EHCI_INST(controller));
190 free(controller);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000191}
192
193enum { EHCI_OUT=0, EHCI_IN=1, EHCI_SETUP=2 };
194
Nico Huber1ab60752012-05-23 09:21:54 +0200195/*
196 * returns the address of the closest USB2.0 hub, which is responsible for
197 * split transactions, along with the number of the used downstream port
198 */
199static int closest_usb2_hub(const usbdev_t *dev, int *const addr, int *const port)
200{
201 const usbdev_t *usb1dev;
202 do {
203 usb1dev = dev;
204 if ((dev->hub > 0) && (dev->hub < 128))
205 dev = dev->controller->devices[dev->hub];
206 else
207 dev = NULL;
208 } while (dev && (dev->speed < 2));
209 if (dev) {
210 *addr = usb1dev->hub;
211 *port = usb1dev->port;
212 return 0;
213 } else {
Gabe Black93ded592012-11-01 15:44:10 -0700214 usb_debug("ehci: Couldn't find closest USB2.0 hub.\n");
Nico Huber1ab60752012-05-23 09:21:54 +0200215 return 1;
216 }
217}
218
Patrick Georgi8fa27872011-11-24 13:19:57 +0100219/* returns handled bytes. assumes that the fields it writes are empty on entry */
220static int fill_td(qtd_t *td, void* data, int datalen)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000221{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000222 u32 total_len = 0;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100223 u32 page_no = 0;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000224
225 u32 start = virt_to_phys(data);
226 u32 page = start & ~4095;
227 u32 offset = start & 4095;
228 u32 page_len = 4096 - offset;
229
Patrick Georgi8fa27872011-11-24 13:19:57 +0100230 td->token |= 0 << QTD_CPAGE_SHIFT;
231 td->bufptrs[page_no++] = start;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000232
233 if (datalen <= page_len) {
234 total_len = datalen;
235 } else {
236 datalen -= page_len;
237 total_len += page_len;
238
Patrick Georgi8fa27872011-11-24 13:19:57 +0100239 while (page_no < 5) {
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000240 /* we have a continguous mapping between virtual and physical memory */
241 page += 4096;
242
Patrick Georgi8fa27872011-11-24 13:19:57 +0100243 td->bufptrs[page_no++] = page;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000244 if (datalen <= 4096) {
245 total_len += datalen;
246 break;
247 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000248 datalen -= 4096;
249 total_len += 4096;
Julius Werner69eea7c2013-01-11 16:25:52 -0800250
251 /* end TD at a packet boundary if transfer not complete */
252 if (page_no == 5)
253 total_len &= ~511;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100254 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000255 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100256 td->token |= total_len << QTD_TOTAL_LEN_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000257 return total_len;
258}
259
260/* free up data structures */
Patrick Georgi8fa27872011-11-24 13:19:57 +0100261static void free_qh_and_tds(ehci_qh_t *qh, qtd_t *cur)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000262{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000263 qtd_t *next;
264 while (cur) {
265 next = (qtd_t*)phys_to_virt(cur->next_qtd & ~31);
Nico Huber4f83d1b2012-11-23 11:58:57 +0100266 free((void *)cur);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000267 cur = next;
268 }
Nico Huber4f83d1b2012-11-23 11:58:57 +0100269 free((void *)qh);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000270}
271
Patrick Georgi8fa27872011-11-24 13:19:57 +0100272static int wait_for_tds(qtd_t *head)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000273{
Julius Wernere9738db2013-02-21 13:41:40 -0800274 /* returns the amount of bytes *not* transmitted, or -1 for error */
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000275 int result = 0;
276 qtd_t *cur = head;
277 while (1) {
278 if (0) dump_td(virt_to_phys(cur));
Nico Huber0421dc82012-05-21 14:53:43 +0200279
280 /* wait for results */
Nico Hubercef86922012-06-01 08:54:29 +0200281 /* how long to wait?
282 * tested with some USB2.0 flash sticks:
283 * TUR turn around took
Julius Werner52e3f442013-07-23 12:45:18 -0700284 * about 2.2s for the slowest (13fe:3800)
Nico Hubercef86922012-06-01 08:54:29 +0200285 * max. 250ms for the others
286 * slowest non-TUR turn around took about 1.3s
Julius Werner52e3f442013-07-23 12:45:18 -0700287 * set to 3s to be safe as a failed TUR can be fatal
Nico Huber0421dc82012-05-21 14:53:43 +0200288 */
Julius Werner52e3f442013-07-23 12:45:18 -0700289 int timeout = 60000; /* time out after 60000 * 50us == 3s */
Nico Huber0421dc82012-05-21 14:53:43 +0200290 while ((cur->token & QTD_ACTIVE) && !(cur->token & QTD_HALTED)
291 && timeout--)
292 udelay(50);
293 if (timeout < 0) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700294 usb_debug("Error: ehci: queue transfer "
Nico Huber0421dc82012-05-21 14:53:43 +0200295 "processing timed out.\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800296 return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200297 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100298 if (cur->token & QTD_HALTED) {
Nico Huber86c686a2012-11-21 16:22:26 +0100299 usb_debug("ERROR with packet\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000300 dump_td(virt_to_phys(cur));
Gabe Black93ded592012-11-01 15:44:10 -0700301 usb_debug("-----------------\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800302 return -1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000303 }
Julius Wernere9738db2013-02-21 13:41:40 -0800304 result += (cur->token & QTD_TOTAL_LEN_MASK)
305 >> QTD_TOTAL_LEN_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000306 if (cur->next_qtd & 1) {
Julius Wernere9738db2013-02-21 13:41:40 -0800307 break;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000308 }
309 if (0) dump_td(virt_to_phys(cur));
310 /* helps debugging the TD chain */
Gabe Black93ded592012-11-01 15:44:10 -0700311 if (0) usb_debug("\nmoving from %x to %x\n", cur, phys_to_virt(cur->next_qtd));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000312 cur = phys_to_virt(cur->next_qtd);
313 }
314 return result;
315}
316
Nico Huber0421dc82012-05-21 14:53:43 +0200317static int ehci_set_async_schedule(ehci_t *ehcic, int enable)
318{
319 /* Set async schedule status. */
320 if (enable)
321 ehcic->operation->usbcmd |= HC_OP_ASYNC_SCHED_EN;
322 else
323 ehcic->operation->usbcmd &= ~HC_OP_ASYNC_SCHED_EN;
324 /* Wait for the controller to accept async schedule status.
325 * This shouldn't take too long, but we should timeout nevertheless.
326 */
327 enable = enable ? HC_OP_ASYNC_SCHED_STAT : 0;
328 int timeout = 100; /* time out after 100ms */
329 while (((ehcic->operation->usbsts & HC_OP_ASYNC_SCHED_STAT) != enable)
330 && timeout--)
331 mdelay(1);
332 if (timeout < 0) {
Gabe Black93ded592012-11-01 15:44:10 -0700333 usb_debug("ehci async schedule status change timed out.\n");
Nico Huber0421dc82012-05-21 14:53:43 +0200334 return 1;
335 }
336 return 0;
337}
338
339static int ehci_process_async_schedule(
340 ehci_t *ehcic, ehci_qh_t *qhead, qtd_t *head)
341{
342 int result;
343
344 /* make sure async schedule is disabled */
Julius Wernere9738db2013-02-21 13:41:40 -0800345 if (ehci_set_async_schedule(ehcic, 0)) return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200346
347 /* hook up QH */
348 ehcic->operation->asynclistaddr = virt_to_phys(qhead);
349
350 /* start async schedule */
Julius Wernere9738db2013-02-21 13:41:40 -0800351 if (ehci_set_async_schedule(ehcic, 1)) return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200352
353 /* wait for result */
354 result = wait_for_tds(head);
355
356 /* disable async schedule */
357 ehci_set_async_schedule(ehcic, 0);
358
359 return result;
360}
361
Julius Werner509c37e2013-08-28 12:29:28 -0700362static int ehci_bulk (endpoint_t *ep, int size, u8 *src, int finalize)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000363{
364 int result = 0;
Julius Werner509c37e2013-08-28 12:29:28 -0700365 u8 *end = src + size;
Julius Wernere9738db2013-02-21 13:41:40 -0800366 int remaining = size;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000367 int endp = ep->endpoint & 0xf;
368 int pid = (ep->direction==IN)?EHCI_IN:EHCI_OUT;
369
Nico Huber1ab60752012-05-23 09:21:54 +0200370 int hubaddr = 0, hubport = 0;
371 if (ep->dev->speed < 2) {
372 /* we need a split transaction */
373 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
Julius Wernere9738db2013-02-21 13:41:40 -0800374 return -1;
Nico Huber1ab60752012-05-23 09:21:54 +0200375 }
376
Julius Werner509c37e2013-08-28 12:29:28 -0700377 if (!dma_coherent(src)) {
378 end = EHCI_INST(ep->dev->controller)->dma_buffer + size;
379 if (size > DMA_SIZE) {
380 usb_debug("EHCI bulk transfer too large for DMA buffer: %d\n", size);
381 return -1;
382 }
383 if (pid == EHCI_OUT)
384 memcpy(end - size, src, size);
385 }
386
387 ehci_qh_t *qh = dma_memalign(64, sizeof(ehci_qh_t));
388 qtd_t *head = dma_memalign(64, sizeof(qtd_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000389 qtd_t *cur = head;
Julius Werner509c37e2013-08-28 12:29:28 -0700390 if (!qh || !head)
391 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000392 while (1) {
Nico Huber4f83d1b2012-11-23 11:58:57 +0100393 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100394 cur->token = QTD_ACTIVE |
395 (pid << QTD_PID_SHIFT) |
396 (0 << QTD_CERR_SHIFT);
Julius Werner509c37e2013-08-28 12:29:28 -0700397 remaining -= fill_td(cur, end - remaining, remaining);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000398
Patrick Georgi8fa27872011-11-24 13:19:57 +0100399 cur->alt_next_qtd = QTD_TERMINATE;
Julius Werner509c37e2013-08-28 12:29:28 -0700400 if (remaining <= 0) {
Patrick Georgi8fa27872011-11-24 13:19:57 +0100401 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000402 break;
403 } else {
Julius Werner509c37e2013-08-28 12:29:28 -0700404 qtd_t *next = dma_memalign(64, sizeof(qtd_t));
405 if (!next)
406 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000407 cur->next_qtd = virt_to_phys(next);
408 cur = next;
409 }
410 }
411
412 /* create QH */
Nico Huber4f83d1b2012-11-23 11:58:57 +0100413 memset((void *)qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100414 qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH;
415 qh->epchar = ep->dev->address |
416 (endp << QH_EP_SHIFT) |
417 (ep->dev->speed << QH_EPS_SHIFT) |
418 (0 << QH_DTC_SHIFT) |
419 (1 << QH_RECLAIM_HEAD_SHIFT) |
420 (ep->maxpacketsize << QH_MPS_SHIFT) |
421 (0 << QH_NAK_CNT_SHIFT);
Nico Huber1ab60752012-05-23 09:21:54 +0200422 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
423 (hubport << QH_PORT_NUMBER_SHIFT) |
424 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000425
426 qh->td.next_qtd = virt_to_phys(head);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100427 qh->td.token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
428 head->token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000429
Nico Huber0421dc82012-05-21 14:53:43 +0200430 result = ehci_process_async_schedule(
431 EHCI_INST(ep->dev->controller), qh, head);
Julius Werner509c37e2013-08-28 12:29:28 -0700432 if (result >= 0) {
Julius Wernere9738db2013-02-21 13:41:40 -0800433 result = size - result;
Julius Werner509c37e2013-08-28 12:29:28 -0700434 if (pid == EHCI_IN && end != src + size)
435 memcpy(src, end - size, result);
436 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000437
Patrick Georgi8fa27872011-11-24 13:19:57 +0100438 ep->toggle = (cur->token & QTD_TOGGLE_MASK) >> QTD_TOGGLE_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000439
440 free_qh_and_tds(qh, head);
Julius Wernere9738db2013-02-21 13:41:40 -0800441
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000442 return result;
Julius Werner509c37e2013-08-28 12:29:28 -0700443
444oom:
445 usb_debug("Not enough DMA memory for EHCI control structures!\n");
446 free_qh_and_tds(qh, head);
447 return -1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000448}
449
450
451/* FIXME: Handle control transfers as 3 QHs, so the 2nd stage can be >0x4000 bytes */
Julius Werner509c37e2013-08-28 12:29:28 -0700452static int ehci_control (usbdev_t *dev, direction_t dir, int drlen, void *setup,
453 int dalen, u8 *src)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000454{
Julius Werner509c37e2013-08-28 12:29:28 -0700455 u8 *data = src;
456 u8 *devreq = setup;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000457 int endp = 0; // this is control. always 0 (for now)
458 int toggle = 0;
459 int mlen = dev->endpoints[0].maxpacketsize;
460 int result = 0;
461
Nico Huber1ab60752012-05-23 09:21:54 +0200462 int hubaddr = 0, hubport = 0, non_hs_ctrl_ep = 0;
463 if (dev->speed < 2) {
464 /* we need a split transaction */
465 if (closest_usb2_hub(dev, &hubaddr, &hubport))
Julius Wernere9738db2013-02-21 13:41:40 -0800466 return -1;
Nico Huber1ab60752012-05-23 09:21:54 +0200467 non_hs_ctrl_ep = 1;
468 }
469
Julius Werner509c37e2013-08-28 12:29:28 -0700470 if (!dma_coherent(setup)) {
471 devreq = EHCI_INST(dev->controller)->dma_buffer;
472 memcpy(devreq, setup, drlen);
473 }
474 if (dalen > 0 && !dma_coherent(src)) {
475 data = EHCI_INST(dev->controller)->dma_buffer + drlen;
476 if (drlen + dalen > DMA_SIZE) {
477 usb_debug("EHCI control transfer too large for DMA buffer: %d\n", drlen + dalen);
478 return -1;
479 }
480 if (dir == OUT)
481 memcpy(data, src, dalen);
482 }
483
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000484 /* create qTDs */
Julius Werner509c37e2013-08-28 12:29:28 -0700485 qtd_t *head = dma_memalign(64, sizeof(qtd_t));
486 ehci_qh_t *qh = dma_memalign(64, sizeof(ehci_qh_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000487 qtd_t *cur = head;
Julius Werner509c37e2013-08-28 12:29:28 -0700488 if (!qh || !head)
489 goto oom;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100490 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100491 cur->token = QTD_ACTIVE |
492 (toggle?QTD_TOGGLE_DATA1:0) |
493 (EHCI_SETUP << QTD_PID_SHIFT) |
494 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000495 if (fill_td(cur, devreq, drlen) != drlen) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700496 usb_debug("ERROR: couldn't send the entire device request\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000497 }
Julius Werner509c37e2013-08-28 12:29:28 -0700498 qtd_t *next = dma_memalign(64, sizeof(qtd_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000499 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100500 cur->alt_next_qtd = QTD_TERMINATE;
Julius Werner509c37e2013-08-28 12:29:28 -0700501 if (!next)
502 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000503
504 /* FIXME: We're limited to 16-20K (depending on alignment) for payload for now.
505 * Figure out, how toggle can be set sensibly in this scenario */
506 if (dalen > 0) {
507 toggle ^= 1;
508 cur = next;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100509 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100510 cur->token = QTD_ACTIVE |
511 (toggle?QTD_TOGGLE_DATA1:0) |
512 (((dir == OUT)?EHCI_OUT:EHCI_IN) << QTD_PID_SHIFT) |
513 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000514 if (fill_td(cur, data, dalen) != dalen) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700515 usb_debug("ERROR: couldn't send the entire control payload\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000516 }
Julius Werner509c37e2013-08-28 12:29:28 -0700517 next = dma_memalign(64, sizeof(qtd_t));
518 if (!next)
519 goto oom;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000520 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100521 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000522 }
523
524 toggle = 1;
525 cur = next;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100526 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100527 cur->token = QTD_ACTIVE |
528 (toggle?QTD_TOGGLE_DATA1:QTD_TOGGLE_DATA0) |
529 ((dir == OUT)?EHCI_IN:EHCI_OUT) << QTD_PID_SHIFT |
530 (0 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000531 fill_td(cur, NULL, 0);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100532 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
533 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000534
535 /* create QH */
Nico Huber4f83d1b2012-11-23 11:58:57 +0100536 memset((void *)qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100537 qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH;
538 qh->epchar = dev->address |
539 (endp << QH_EP_SHIFT) |
540 (dev->speed << QH_EPS_SHIFT) |
541 (1 << QH_DTC_SHIFT) | /* ctrl transfers are special: take toggle bit from TD */
542 (1 << QH_RECLAIM_HEAD_SHIFT) |
543 (mlen << QH_MPS_SHIFT) |
Nico Huber1ab60752012-05-23 09:21:54 +0200544 (non_hs_ctrl_ep << QH_NON_HS_CTRL_EP_SHIFT) |
Patrick Georgi8fa27872011-11-24 13:19:57 +0100545 (0 << QH_NAK_CNT_SHIFT);
Nico Huber1ab60752012-05-23 09:21:54 +0200546 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
547 (hubport << QH_PORT_NUMBER_SHIFT) |
548 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000549 qh->td.next_qtd = virt_to_phys(head);
550
Nico Huber0421dc82012-05-21 14:53:43 +0200551 result = ehci_process_async_schedule(
552 EHCI_INST(dev->controller), qh, head);
Julius Werner509c37e2013-08-28 12:29:28 -0700553 if (result >= 0) {
Julius Wernere9738db2013-02-21 13:41:40 -0800554 result = dalen - result;
Julius Werner509c37e2013-08-28 12:29:28 -0700555 if (dir == IN && data != src)
556 memcpy(src, data, result);
557 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000558
559 free_qh_and_tds(qh, head);
560 return result;
Julius Werner509c37e2013-08-28 12:29:28 -0700561
562oom:
563 usb_debug("Not enough DMA memory for EHCI control structures!\n");
564 free_qh_and_tds(qh, head);
565 return -1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000566}
567
Nico Huber62eb5b32012-05-25 10:09:13 +0200568
569typedef struct _intr_qtd_t intr_qtd_t;
570
571struct _intr_qtd_t {
572 volatile qtd_t td;
573 u8 *data;
574 intr_qtd_t *next;
575};
576
577typedef struct {
578 volatile ehci_qh_t qh;
579 intr_qtd_t *head;
580 intr_qtd_t *tail;
Nico Huber0d120f82012-06-14 13:08:36 +0200581 intr_qtd_t *spare;
Nico Huber62eb5b32012-05-25 10:09:13 +0200582 u8 *data;
583 endpoint_t *endp;
584 int reqsize;
585} intr_queue_t;
586
587static void fill_intr_queue_td(
588 intr_queue_t *const intrq,
589 intr_qtd_t *const intr_qtd,
590 u8 *const data)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000591{
Nico Huber62eb5b32012-05-25 10:09:13 +0200592 const int pid = (intrq->endp->direction == IN) ? EHCI_IN
593 : (intrq->endp->direction == OUT) ? EHCI_OUT
594 : EHCI_SETUP;
595 const int cerr = (intrq->endp->dev->speed < 2) ? 1 : 0;
596
597 memset(intr_qtd, 0, sizeof(*intr_qtd));
598 intr_qtd->td.next_qtd = QTD_TERMINATE;
599 intr_qtd->td.alt_next_qtd = QTD_TERMINATE;
600 intr_qtd->td.token = QTD_ACTIVE |
601 (pid << QTD_PID_SHIFT) |
602 (cerr << QTD_CERR_SHIFT) |
603 ((intrq->endp->toggle & 1) << QTD_TOGGLE_SHIFT);
604 fill_td(&intr_qtd->td, data, intrq->reqsize);
605 intr_qtd->data = data;
606 intr_qtd->next = NULL;
607
608 intrq->endp->toggle ^= 1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000609}
610
Nico Huber62eb5b32012-05-25 10:09:13 +0200611static void ehci_destroy_intr_queue(endpoint_t *const, void *const);
612
613static void *ehci_create_intr_queue(
614 endpoint_t *const ep,
615 const int reqsize,
616 int reqcount,
617 const int reqtiming)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000618{
Nico Huber62eb5b32012-05-25 10:09:13 +0200619 int i;
620
621 if ((reqsize > (4 * 4096 + 1)) || /* the maximum for arbitrary aligned
622 data in five 4096 byte pages */
623 (reqtiming > 1024))
624 return NULL;
625 if (reqcount < 2) /* we need at least 2:
626 one for processing, one for the hc to advance to */
627 reqcount = 2;
628
629 int hubaddr = 0, hubport = 0;
630 if (ep->dev->speed < 2) {
631 /* we need a split transaction */
632 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
633 return NULL;
634 }
635
Julius Werner509c37e2013-08-28 12:29:28 -0700636 intr_queue_t *const intrq = (intr_queue_t *)malloc(sizeof(intr_queue_t));
Nico Huber0d120f82012-06-14 13:08:36 +0200637 /*
638 * reqcount data chunks
639 * plus one more spare, which we'll leave out of queue
640 */
Julius Werner509c37e2013-08-28 12:29:28 -0700641 u8 *data = (u8 *)dma_malloc(reqsize * (reqcount + 1));
Nico Huber62eb5b32012-05-25 10:09:13 +0200642 if (!intrq || !data)
643 fatal("Not enough memory to create USB interrupt queue.\n");
644 intrq->data = data;
645 intrq->endp = ep;
646 intrq->reqsize = reqsize;
647
648 /* create #reqcount transfer descriptors (qTDs) */
Julius Werner509c37e2013-08-28 12:29:28 -0700649 intrq->head = (intr_qtd_t *)dma_memalign(64, sizeof(intr_qtd_t));
Nico Huber62eb5b32012-05-25 10:09:13 +0200650 intr_qtd_t *cur_td = intrq->head;
651 for (i = 0; i < reqcount; ++i) {
652 fill_intr_queue_td(intrq, cur_td, data);
653 data += reqsize;
654 if (i < reqcount - 1) {
655 /* create one more qTD */
656 intr_qtd_t *const next_td =
Julius Werner509c37e2013-08-28 12:29:28 -0700657 (intr_qtd_t *)dma_memalign(64, sizeof(intr_qtd_t));
Nico Huber62eb5b32012-05-25 10:09:13 +0200658 cur_td->td.next_qtd = virt_to_phys(&next_td->td);
659 cur_td->next = next_td;
660 cur_td = next_td;
661 }
662 }
663 intrq->tail = cur_td;
664
Nico Huber0d120f82012-06-14 13:08:36 +0200665 /* create spare qTD */
Julius Werner509c37e2013-08-28 12:29:28 -0700666 intrq->spare = (intr_qtd_t *)dma_memalign(64, sizeof(intr_qtd_t));
Nico Huber0d120f82012-06-14 13:08:36 +0200667 fill_intr_queue_td(intrq, intrq->spare, data);
668
Nico Huber62eb5b32012-05-25 10:09:13 +0200669 /* initialize QH */
670 const int endp = ep->endpoint & 0xf;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100671 memset((void *)&intrq->qh, 0, sizeof(intrq->qh));
Nico Huber62eb5b32012-05-25 10:09:13 +0200672 intrq->qh.horiz_link_ptr = PS_TERMINATE;
673 intrq->qh.epchar = ep->dev->address |
674 (endp << QH_EP_SHIFT) |
675 (ep->dev->speed << QH_EPS_SHIFT) |
676 (1 << QH_DTC_SHIFT) |
677 (0 << QH_RECLAIM_HEAD_SHIFT) |
678 (ep->maxpacketsize << QH_MPS_SHIFT) |
679 (0 << QH_NAK_CNT_SHIFT);
680 intrq->qh.epcaps = (1 << QH_PIPE_MULTIPLIER_SHIFT) |
681 (hubport << QH_PORT_NUMBER_SHIFT) |
682 (hubaddr << QH_HUB_ADDRESS_SHIFT) |
683 (0xfe << QH_UFRAME_CMASK_SHIFT) |
684 1 /* uFrame S-mask */;
685 intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td);
686
687 /* insert QH into periodic schedule */
688 int nothing_placed = 1;
689 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(ep->dev->controller)
690 ->operation->periodiclistbase);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200691 const u32 dummy_ptr = virt_to_phys(EHCI_INST(
692 ep->dev->controller)->dummy_qh) | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200693 for (i = 0; i < 1024; i += reqtiming) {
694 /* advance to the next free position */
Nico Huber3ca35ca2012-06-14 13:27:39 +0200695 while ((i < 1024) && (ps[i] != dummy_ptr)) ++i;
Nico Huber62eb5b32012-05-25 10:09:13 +0200696 if (i < 1024) {
697 ps[i] = virt_to_phys(&intrq->qh) | PS_TYPE_QH;
698 nothing_placed = 0;
699 }
700 }
701 if (nothing_placed) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700702 usb_debug("Error: Failed to place ehci interrupt queue head "
Nico Huber62eb5b32012-05-25 10:09:13 +0200703 "into periodic schedule: no space left\n");
704 ehci_destroy_intr_queue(ep, intrq);
705 return NULL;
706 }
707
708 return intrq;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000709}
710
Nico Huber62eb5b32012-05-25 10:09:13 +0200711static void ehci_destroy_intr_queue(endpoint_t *const ep, void *const queue)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000712{
Nico Huber62eb5b32012-05-25 10:09:13 +0200713 intr_queue_t *const intrq = (intr_queue_t *)queue;
714
715 /* remove QH from periodic schedule */
716 int i;
717 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(
718 ep->dev->controller)->operation->periodiclistbase);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200719 const u32 dummy_ptr = virt_to_phys(EHCI_INST(
720 ep->dev->controller)->dummy_qh) | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200721 for (i = 0; i < 1024; ++i) {
722 if ((ps[i] & PS_PTR_MASK) == virt_to_phys(&intrq->qh))
Nico Huber3ca35ca2012-06-14 13:27:39 +0200723 ps[i] = dummy_ptr;
Nico Huber62eb5b32012-05-25 10:09:13 +0200724 }
725
726 /* wait 1ms for frame to end */
727 mdelay(1);
728
729 while (intrq->head) {
730 /* disable qTD and destroy list */
731 intrq->head->td.next_qtd = QTD_TERMINATE;
732 intrq->head->td.token &= ~QTD_ACTIVE;
733
734 /* save and advance head ptr */
735 intr_qtd_t *const to_free = intrq->head;
736 intrq->head = intrq->head->next;
737
738 /* free current interrupt qTD */
739 free(to_free);
740 }
Nico Huber0d120f82012-06-14 13:08:36 +0200741 free(intrq->spare);
Nico Huber62eb5b32012-05-25 10:09:13 +0200742 free(intrq->data);
743 free(intrq);
744}
745
746static u8 *ehci_poll_intr_queue(void *const queue)
747{
748 intr_queue_t *const intrq = (intr_queue_t *)queue;
749
750 u8 *ret = NULL;
751
752 /* process if head qTD is inactive AND QH has been moved forward */
Nico Huber0d120f82012-06-14 13:08:36 +0200753 if (!(intrq->head->td.token & QTD_ACTIVE)) {
Nico Huber62eb5b32012-05-25 10:09:13 +0200754 if (!(intrq->head->td.token & QTD_STATUS_MASK))
755 ret = intrq->head->data;
756 else
Gabe Black93ded592012-11-01 15:44:10 -0700757 usb_debug("ehci_poll_intr_queue: transfer failed, "
Nico Huber62eb5b32012-05-25 10:09:13 +0200758 "status == 0x%02x\n",
759 intrq->head->td.token & QTD_STATUS_MASK);
760
Nico Huber0d120f82012-06-14 13:08:36 +0200761 /* insert spare qTD at the end and advance our tail ptr */
762 fill_intr_queue_td(intrq, intrq->spare, intrq->spare->data);
763 intrq->tail->td.next_qtd = virt_to_phys(&intrq->spare->td);
764 intrq->tail->next = intrq->spare;
Nico Huber62eb5b32012-05-25 10:09:13 +0200765 intrq->tail = intrq->tail->next;
Nico Huber0d120f82012-06-14 13:08:36 +0200766
767 /* reuse executed qTD as spare one and advance our head ptr */
768 intrq->spare = intrq->head;
769 intrq->head = intrq->head->next;
770 }
771 /* reset queue if we fully processed it after underrun */
Nico Huberaf169f42012-11-22 11:14:03 +0100772 else if ((intrq->qh.td.next_qtd & QTD_TERMINATE) &&
773 /* to prevent race conditions:
774 not our head and not active */
775 (intrq->qh.current_td_ptr !=
776 virt_to_phys(&intrq->head->td)) &&
777 !(intrq->qh.td.token & QTD_ACTIVE)) {
Gabe Black93ded592012-11-01 15:44:10 -0700778 usb_debug("resetting underrun ehci interrupt queue.\n");
Nico Huberaf169f42012-11-22 11:14:03 +0100779 intrq->qh.current_td_ptr = 0;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100780 memset((void *)&intrq->qh.td, 0, sizeof(intrq->qh.td));
Nico Huber0d120f82012-06-14 13:08:36 +0200781 intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td);
Nico Huber62eb5b32012-05-25 10:09:13 +0200782 }
783 return ret;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000784}
785
786hci_t *
Nico Huber6e230662014-07-07 16:33:59 +0200787ehci_init (unsigned long physical_bar)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000788{
789 int i;
790 hci_t *controller = new_controller ();
791
792 if (!controller)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100793 fatal("Could not create USB controller instance.\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000794
795 controller->instance = malloc (sizeof (ehci_t));
796 if(!controller->instance)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100797 fatal("Not enough memory creating USB controller instance.\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000798
Anton Kochkov1c36ead2012-06-28 08:30:15 +0400799 controller->type = EHCI;
800
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000801 controller->start = ehci_start;
802 controller->stop = ehci_stop;
803 controller->reset = ehci_reset;
Nico Huber6e711c62012-11-12 16:20:32 +0100804 controller->init = ehci_reinit;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000805 controller->shutdown = ehci_shutdown;
806 controller->bulk = ehci_bulk;
807 controller->control = ehci_control;
Patrick Georgi482af6d2013-05-24 15:48:56 +0200808 controller->set_address = generic_set_address;
809 controller->finish_device_config = NULL;
810 controller->destroy_device = NULL;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000811 controller->create_intr_queue = ehci_create_intr_queue;
812 controller->destroy_intr_queue = ehci_destroy_intr_queue;
813 controller->poll_intr_queue = ehci_poll_intr_queue;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000814 for (i = 0; i < 128; i++) {
815 controller->devices[i] = 0;
816 }
817 init_device_entry (controller, 0);
818
Nico Huber5b9e6f12014-07-10 12:56:34 +0200819 EHCI_INST(controller)->capabilities = phys_to_virt(physical_bar);
820 EHCI_INST(controller)->operation = (hc_op_t *)(phys_to_virt(physical_bar) + EHCI_INST(controller)->capabilities->caplength);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000821
Vincent Palatin716375d2012-10-25 17:38:43 -0700822 /* Set the high address word (aka segment) if controller is 64-bit */
823 if (EHCI_INST(controller)->capabilities->hccparams & 1)
824 EHCI_INST(controller)->operation->ctrldssegment = 0;
825
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000826 /* Enable operation of controller */
827 controller->start(controller);
828
829 /* take over all ports. USB1 should be blind now */
830 EHCI_INST(controller)->operation->configflag = 1;
831
Nico Huber62eb5b32012-05-25 10:09:13 +0200832 /* Initialize periodic frame list */
833 /* 1024 32-bit pointers, 4kb aligned */
Julius Werner509c37e2013-08-28 12:29:28 -0700834 u32 *const periodic_list = (u32 *)dma_memalign(4096, 1024 * sizeof(u32));
Nico Huber62eb5b32012-05-25 10:09:13 +0200835 if (!periodic_list)
836 fatal("Not enough memory creating EHCI periodic frame list.\n");
Nico Huber3ca35ca2012-06-14 13:27:39 +0200837
Julius Werner509c37e2013-08-28 12:29:28 -0700838 if (dma_initialized()) {
839 EHCI_INST(controller)->dma_buffer = dma_memalign(4096, DMA_SIZE);
840 if (!EHCI_INST(controller)->dma_buffer)
841 fatal("Not enough DMA memory for EHCI bounce buffer.\n");
842 }
843
Nico Huber3ca35ca2012-06-14 13:27:39 +0200844 /*
845 * Insert dummy QH in periodic frame list
846 * This helps with broken host controllers
847 * and doesn't violate the standard.
848 */
Julius Werner509c37e2013-08-28 12:29:28 -0700849 EHCI_INST(controller)->dummy_qh = (ehci_qh_t *)dma_memalign(64, sizeof(ehci_qh_t));
Nico Huber4f83d1b2012-11-23 11:58:57 +0100850 memset((void *)EHCI_INST(controller)->dummy_qh, 0,
Nico Huber3ca35ca2012-06-14 13:27:39 +0200851 sizeof(*EHCI_INST(controller)->dummy_qh));
852 EHCI_INST(controller)->dummy_qh->horiz_link_ptr = QH_TERMINATE;
Julius Werner4610f0e2013-08-22 16:24:09 -0700853 EHCI_INST(controller)->dummy_qh->td.next_qtd = QH_TERMINATE;
854 EHCI_INST(controller)->dummy_qh->td.alt_next_qtd = QH_TERMINATE;
Nico Huber62eb5b32012-05-25 10:09:13 +0200855 for (i = 0; i < 1024; ++i)
Nico Huber3ca35ca2012-06-14 13:27:39 +0200856 periodic_list[i] = virt_to_phys(EHCI_INST(controller)->dummy_qh)
857 | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200858
859 /* Make sure periodic schedule is disabled */
860 ehci_set_periodic_schedule(EHCI_INST(controller), 0);
861 /* Set periodic frame list pointer */
862 EHCI_INST(controller)->operation->periodiclistbase =
863 virt_to_phys(periodic_list);
864 /* Enable use of periodic schedule */
865 ehci_set_periodic_schedule(EHCI_INST(controller), 1);
866
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000867 /* TODO lots of stuff missing */
868
869 controller->devices[0]->controller = controller;
870 controller->devices[0]->init = ehci_rh_init;
871 controller->devices[0]->init (controller->devices[0]);
872
873 return controller;
874}
Stefan Reinauer8992e532013-05-02 16:16:41 -0700875
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700876#ifdef CONFIG_LP_USB_PCI
Stefan Reinauer8992e532013-05-02 16:16:41 -0700877hci_t *
878ehci_pci_init (pcidev_t addr)
879{
880 hci_t *controller;
881 u32 reg_base;
882
883 u32 pci_command = pci_read_config32(addr, PCI_COMMAND);
884 pci_command = (pci_command | PCI_COMMAND_MEMORY) & ~PCI_COMMAND_IO ;
885 pci_write_config32(addr, PCI_COMMAND, pci_command);
886
887 reg_base = pci_read_config32 (addr, USBBASE);
888
889 /* default value for frame length adjust */
890 pci_write_config8(addr, FLADJ, FLADJ_framelength(60000));
891
Nico Huber6e230662014-07-07 16:33:59 +0200892 controller = ehci_init((unsigned long)reg_base);
Stefan Reinauer8992e532013-05-02 16:16:41 -0700893
894 return controller;
895}
896#endif