blob: af7daf1999b46927fd2f576fe96f89d717a1d175 [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>
33#include "ehci.h"
34#include "ehci_private.h"
35
Stefan Reinauer528b43d2011-04-14 19:52:04 +000036static void dump_td(u32 addr)
37{
Patrick Georgi7f43dc12010-09-25 17:01:13 +000038 qtd_t *td = phys_to_virt(addr);
Anton Kochkovc07fb152012-12-19 12:32:32 +040039 usb_debug("+---------------------------------------------------+\n");
40 if (((td->token & (3UL << 8)) >> 8) == 2)
41 usb_debug("|..[SETUP]..........................................|\n");
42 else if (((td->token & (3UL << 8)) >> 8) == 1)
43 usb_debug("|..[IN].............................................|\n");
44 else if (((td->token & (3UL << 8)) >> 8) == 0)
45 usb_debug("|..[OUT]............................................|\n");
46 else
47 usb_debug("|..[]...............................................|\n");
48 usb_debug("|:|============ EHCI TD at [0x%08lx] ==========|:|\n", addr);
49 usb_debug("|:| ERRORS = [%ld] | TOKEN = [0x%08lx] | |:|\n",
50 3 - ((td->token & QTD_CERR_MASK) >> QTD_CERR_SHIFT), td->token);
51 usb_debug("|:+-----------------------------------------------+:|\n");
52 usb_debug("|:| Next qTD [0x%08lx] |:|\n", td->next_qtd);
53 usb_debug("|:+-----------------------------------------------+:|\n");
54 usb_debug("|:| Alt. Next qTD [0x%08lx] |:|\n", td->alt_next_qtd);
55 usb_debug("|:+-----------------------------------------------+:|\n");
56 usb_debug("|:| | Bytes to Transfer | [%04ld] |:|\n", (td->token & (0x7FUL << 16)) >> 16);
57 usb_debug("|:| | PID CODE: | [%ld] |:|\n", (td->token & (3UL << 8)) >> 8);
58 usb_debug("|:| | Interrupt On Complete (IOC) | [%ld] |:|\n", (td->token & (1UL << 15)) >> 15);
59 usb_debug("|:| | Status Active | [%ld] |:|\n", (td->token & (1UL << 7)) >> 7);
60 usb_debug("|:| | Status Halted | [%ld] |:|\n", (td->token & (1UL << 6)) >> 6);
61 usb_debug("|:| TOKEN | Status Data Buffer Error | [%ld] |:|\n", (td->token & (1UL << 5)) >> 5);
62 usb_debug("|:| | Status Babble detected | [%ld] |:|\n", (td->token & (1UL << 4)) >> 4);
63 usb_debug("|:| | Status Transaction Error | [%ld] |:|\n", (td->token & (1UL << 3)) >> 3);
64 usb_debug("|:| | Status Missed Micro Frame | [%ld] |:|\n", (td->token & (1UL << 2)) >> 2);
65 usb_debug("|:| | Split Transaction State | [%ld] |:|\n", (td->token & (1UL << 1)) >> 1);
66 usb_debug("|:| | Ping State | [%ld] |:|\n", td->token & 1UL);
67 usb_debug("|:|-----------------------------------------------|:|\n");
68 usb_debug("|...................................................|\n");
69 usb_debug("+---------------------------------------------------+\n");
70}
71
Stefan Reinauer8992e532013-05-02 16:16:41 -070072#if 0 && defined(USB_DEBUG)
Anton Kochkovc07fb152012-12-19 12:32:32 +040073static void dump_qh(ehci_qh_t *cur)
74{
75 qtd_t *tmp_qtd = NULL;
76 usb_debug("+===================================================+\n");
77 usb_debug("| ############# EHCI QH at [0x%08lx] ########### |\n", virt_to_phys(cur));
78 usb_debug("+---------------------------------------------------+\n");
79 usb_debug("| Horizonal Link Pointer [0x%08lx] |\n", cur->horiz_link_ptr);
80 usb_debug("+------------------[ 0x%08lx ]-------------------+\n", cur->epchar);
81 usb_debug("| | Maximum Packet Length | [%04ld] |\n", ((cur->epchar & (0x7ffUL << 16)) >> 16));
82 usb_debug("| | Device Address | [%ld] |\n", cur->epchar & 0x7F);
83 usb_debug("| | Inactivate on Next Transaction | [%ld] |\n", ((cur->epchar & (1UL << 7)) >> 7));
84 usb_debug("| | Endpoint Number | [%ld] |\n", ((cur->epchar & (0xFUL << 8)) >> 8));
85 usb_debug("| EPCHAR | Endpoint Speed | [%ld] |\n", ((cur->epchar & (3UL << 12)) >> 12));
86 usb_debug("| | Data Toggle Control | [%ld] |\n", ((cur->epchar & (1UL << 14)) >> 14));
87 usb_debug("| | Head of Reclamation List Flag | [%ld] |\n", ((cur->epchar & (1UL << 15)) >> 15));
88 usb_debug("| | Control Endpoint Flag | [%ld] |\n", ((cur->epchar & (1UL << 27)) >> 27));
89 usb_debug("| | Nak Count Reload | [%ld] |\n", ((cur->epchar & (0xFUL << 28)) >> 28));
90 if (((cur->epchar & (1UL << QH_NON_HS_CTRL_EP_SHIFT)) >> QH_NON_HS_CTRL_EP_SHIFT) == 1) { /* Split transaction */
91 usb_debug("+--------+---------[ 0x%08lx ]----------+--------+\n", cur->epcaps);
92 usb_debug("| | Hub Port | [%ld] |\n", ((cur->epcaps & (0x7FUL << 23)) >> 23)); /* [29:23] */
93 usb_debug("| | Hub Address | [%ld] |\n", ((cur->epcaps & (0x7FUL << 16)) >> 16)); /* [22:16] */
94 }
95 usb_debug("+---------------------------------------------------+\n");
96 usb_debug("| Current QTD [0x%08lx] |\n", cur->current_td_ptr);
97
98 if (!((cur->horiz_link_ptr == 0) && (cur->epchar == 0))) {
99 /* Dump overlay QTD for this QH */
100 usb_debug("+---------------------------------------------------+\n");
101 usb_debug("|::::::::::::::::::: QTD OVERLAY :::::::::::::::::::|\n");
102 dump_td(virt_to_phys((void *)&(cur->td)));
103 /* Dump all TD tree for this QH */
104 tmp_qtd = (qtd_t *)phys_to_virt((cur->td.next_qtd & ~0x1FUL));
105 if (tmp_qtd != NULL)
106 usb_debug("|:::::::::::::::::: EHCI QTD CHAIN :::::::::::::::::|\n");
107 while (tmp_qtd != NULL)
108 {
109 dump_td(virt_to_phys(tmp_qtd));
110 tmp_qtd = (qtd_t *)phys_to_virt((tmp_qtd->next_qtd & ~0x1FUL));
111 }
112 usb_debug("|:::::::::::::::: EOF EHCI QTD CHAIN :::::::::::::::|\n");
113 usb_debug("+---------------------------------------------------+\n");
114 } else {
115 usb_debug("+---------------------------------------------------+\n");
116 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000117}
Stefan Reinauerd5c79f92013-03-25 15:56:08 -0700118#endif
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000119
120static void ehci_start (hci_t *controller)
121{
Patrick Georgi8fa27872011-11-24 13:19:57 +0100122 EHCI_INST(controller)->operation->usbcmd |= HC_OP_RS;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000123}
124
125static void ehci_stop (hci_t *controller)
126{
Patrick Georgi8fa27872011-11-24 13:19:57 +0100127 EHCI_INST(controller)->operation->usbcmd &= ~HC_OP_RS;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000128}
129
130static void ehci_reset (hci_t *controller)
131{
Anton Kochkov2fef58e2012-11-09 14:06:05 +0100132 short count = 0;
133 ehci_stop(controller);
134 /* wait 10 ms just to be shure */
135 mdelay(10);
136 if (EHCI_INST(controller)->operation->usbsts & HC_OP_HC_HALTED) {
137 EHCI_INST(controller)->operation->usbcmd = HC_OP_HC_RESET;
138 /* wait 100 ms */
139 for (count = 0; count < 10; count++) {
140 mdelay(10);
141 if (!(EHCI_INST(controller)->operation->usbcmd & HC_OP_HC_RESET)) {
142 return;
143 }
144 }
145 }
146 usb_debug("ehci_reset(): reset failed!\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000147}
148
Nico Huber6e711c62012-11-12 16:20:32 +0100149static void ehci_reinit (hci_t *controller)
150{
151}
152
Nico Huber62eb5b32012-05-25 10:09:13 +0200153static int ehci_set_periodic_schedule(ehci_t *ehcic, int enable)
154{
155 /* Set periodic schedule status. */
156 if (enable)
157 ehcic->operation->usbcmd |= HC_OP_PERIODIC_SCHED_EN;
158 else
159 ehcic->operation->usbcmd &= ~HC_OP_PERIODIC_SCHED_EN;
160 /* Wait for the controller to accept periodic schedule status.
161 * This shouldn't take too long, but we should timeout nevertheless.
162 */
163 enable = enable ? HC_OP_PERIODIC_SCHED_STAT : 0;
Gabe Blackef650a52012-11-21 02:16:13 -0800164 int timeout = 100000; /* time out after 100ms */
Nico Huber62eb5b32012-05-25 10:09:13 +0200165 while (((ehcic->operation->usbsts & HC_OP_PERIODIC_SCHED_STAT) != enable)
166 && timeout--)
Gabe Blackef650a52012-11-21 02:16:13 -0800167 udelay(1);
Nico Huber62eb5b32012-05-25 10:09:13 +0200168 if (timeout < 0) {
Gabe Black93ded592012-11-01 15:44:10 -0700169 usb_debug("ehci periodic schedule status change timed out.\n");
Nico Huber62eb5b32012-05-25 10:09:13 +0200170 return 1;
171 }
172 return 0;
173}
174
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000175static void ehci_shutdown (hci_t *controller)
176{
Mathias Krause7a9da712013-03-24 19:40:01 +0100177 detach_controller(controller);
Nico Huber62eb5b32012-05-25 10:09:13 +0200178 /* Make sure periodic schedule is disabled */
179 ehci_set_periodic_schedule(EHCI_INST(controller), 0);
180 /* Free periodic frame list */
181 free(phys_to_virt(EHCI_INST(controller)->operation->periodiclistbase));
182
Nico Huber3ca35ca2012-06-14 13:27:39 +0200183 /* Free dummy QH */
Nico Huber4f83d1b2012-11-23 11:58:57 +0100184 free((void *)EHCI_INST(controller)->dummy_qh);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200185
Patrick Georgi01178bb2011-11-04 11:57:46 +0100186 EHCI_INST(controller)->operation->configflag = 0;
Nico Huberd633dda2012-11-12 15:35:44 +0100187
188 free(EHCI_INST(controller));
189 free(controller);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000190}
191
192enum { EHCI_OUT=0, EHCI_IN=1, EHCI_SETUP=2 };
193
Nico Huber1ab60752012-05-23 09:21:54 +0200194/*
195 * returns the address of the closest USB2.0 hub, which is responsible for
196 * split transactions, along with the number of the used downstream port
197 */
198static int closest_usb2_hub(const usbdev_t *dev, int *const addr, int *const port)
199{
200 const usbdev_t *usb1dev;
201 do {
202 usb1dev = dev;
203 if ((dev->hub > 0) && (dev->hub < 128))
204 dev = dev->controller->devices[dev->hub];
205 else
206 dev = NULL;
207 } while (dev && (dev->speed < 2));
208 if (dev) {
209 *addr = usb1dev->hub;
210 *port = usb1dev->port;
211 return 0;
212 } else {
Gabe Black93ded592012-11-01 15:44:10 -0700213 usb_debug("ehci: Couldn't find closest USB2.0 hub.\n");
Nico Huber1ab60752012-05-23 09:21:54 +0200214 return 1;
215 }
216}
217
Patrick Georgi8fa27872011-11-24 13:19:57 +0100218/* returns handled bytes. assumes that the fields it writes are empty on entry */
219static int fill_td(qtd_t *td, void* data, int datalen)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000220{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000221 u32 total_len = 0;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100222 u32 page_no = 0;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000223
224 u32 start = virt_to_phys(data);
225 u32 page = start & ~4095;
226 u32 offset = start & 4095;
227 u32 page_len = 4096 - offset;
228
Patrick Georgi8fa27872011-11-24 13:19:57 +0100229 td->token |= 0 << QTD_CPAGE_SHIFT;
230 td->bufptrs[page_no++] = start;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000231
232 if (datalen <= page_len) {
233 total_len = datalen;
234 } else {
235 datalen -= page_len;
236 total_len += page_len;
237
Patrick Georgi8fa27872011-11-24 13:19:57 +0100238 while (page_no < 5) {
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000239 /* we have a continguous mapping between virtual and physical memory */
240 page += 4096;
241
Patrick Georgi8fa27872011-11-24 13:19:57 +0100242 td->bufptrs[page_no++] = page;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000243 if (datalen <= 4096) {
244 total_len += datalen;
245 break;
246 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000247 datalen -= 4096;
248 total_len += 4096;
Julius Werner69eea7c2013-01-11 16:25:52 -0800249
250 /* end TD at a packet boundary if transfer not complete */
251 if (page_no == 5)
252 total_len &= ~511;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100253 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000254 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100255 td->token |= total_len << QTD_TOTAL_LEN_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000256 return total_len;
257}
258
259/* free up data structures */
Patrick Georgi8fa27872011-11-24 13:19:57 +0100260static void free_qh_and_tds(ehci_qh_t *qh, qtd_t *cur)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000261{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000262 qtd_t *next;
263 while (cur) {
264 next = (qtd_t*)phys_to_virt(cur->next_qtd & ~31);
Nico Huber4f83d1b2012-11-23 11:58:57 +0100265 free((void *)cur);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000266 cur = next;
267 }
Nico Huber4f83d1b2012-11-23 11:58:57 +0100268 free((void *)qh);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000269}
270
Patrick Georgi8fa27872011-11-24 13:19:57 +0100271static int wait_for_tds(qtd_t *head)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000272{
Julius Wernere9738db2013-02-21 13:41:40 -0800273 /* returns the amount of bytes *not* transmitted, or -1 for error */
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000274 int result = 0;
275 qtd_t *cur = head;
276 while (1) {
277 if (0) dump_td(virt_to_phys(cur));
Nico Huber0421dc82012-05-21 14:53:43 +0200278
279 /* wait for results */
Nico Hubercef86922012-06-01 08:54:29 +0200280 /* how long to wait?
281 * tested with some USB2.0 flash sticks:
282 * TUR turn around took
Julius Werner52e3f442013-07-23 12:45:18 -0700283 * about 2.2s for the slowest (13fe:3800)
Nico Hubercef86922012-06-01 08:54:29 +0200284 * max. 250ms for the others
285 * slowest non-TUR turn around took about 1.3s
Julius Werner52e3f442013-07-23 12:45:18 -0700286 * set to 3s to be safe as a failed TUR can be fatal
Nico Huber0421dc82012-05-21 14:53:43 +0200287 */
Julius Werner52e3f442013-07-23 12:45:18 -0700288 int timeout = 60000; /* time out after 60000 * 50us == 3s */
Nico Huber0421dc82012-05-21 14:53:43 +0200289 while ((cur->token & QTD_ACTIVE) && !(cur->token & QTD_HALTED)
290 && timeout--)
291 udelay(50);
292 if (timeout < 0) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700293 usb_debug("Error: ehci: queue transfer "
Nico Huber0421dc82012-05-21 14:53:43 +0200294 "processing timed out.\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800295 return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200296 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100297 if (cur->token & QTD_HALTED) {
Nico Huber86c686a2012-11-21 16:22:26 +0100298 usb_debug("ERROR with packet\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000299 dump_td(virt_to_phys(cur));
Gabe Black93ded592012-11-01 15:44:10 -0700300 usb_debug("-----------------\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800301 return -1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000302 }
Julius Wernere9738db2013-02-21 13:41:40 -0800303 result += (cur->token & QTD_TOTAL_LEN_MASK)
304 >> QTD_TOTAL_LEN_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000305 if (cur->next_qtd & 1) {
Julius Wernere9738db2013-02-21 13:41:40 -0800306 break;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000307 }
308 if (0) dump_td(virt_to_phys(cur));
309 /* helps debugging the TD chain */
Gabe Black93ded592012-11-01 15:44:10 -0700310 if (0) usb_debug("\nmoving from %x to %x\n", cur, phys_to_virt(cur->next_qtd));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000311 cur = phys_to_virt(cur->next_qtd);
312 }
313 return result;
314}
315
Nico Huber0421dc82012-05-21 14:53:43 +0200316static int ehci_set_async_schedule(ehci_t *ehcic, int enable)
317{
318 /* Set async schedule status. */
319 if (enable)
320 ehcic->operation->usbcmd |= HC_OP_ASYNC_SCHED_EN;
321 else
322 ehcic->operation->usbcmd &= ~HC_OP_ASYNC_SCHED_EN;
323 /* Wait for the controller to accept async schedule status.
324 * This shouldn't take too long, but we should timeout nevertheless.
325 */
326 enable = enable ? HC_OP_ASYNC_SCHED_STAT : 0;
327 int timeout = 100; /* time out after 100ms */
328 while (((ehcic->operation->usbsts & HC_OP_ASYNC_SCHED_STAT) != enable)
329 && timeout--)
330 mdelay(1);
331 if (timeout < 0) {
Gabe Black93ded592012-11-01 15:44:10 -0700332 usb_debug("ehci async schedule status change timed out.\n");
Nico Huber0421dc82012-05-21 14:53:43 +0200333 return 1;
334 }
335 return 0;
336}
337
338static int ehci_process_async_schedule(
339 ehci_t *ehcic, ehci_qh_t *qhead, qtd_t *head)
340{
341 int result;
342
343 /* make sure async schedule is disabled */
Julius Wernere9738db2013-02-21 13:41:40 -0800344 if (ehci_set_async_schedule(ehcic, 0)) return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200345
346 /* hook up QH */
347 ehcic->operation->asynclistaddr = virt_to_phys(qhead);
348
349 /* start async schedule */
Julius Wernere9738db2013-02-21 13:41:40 -0800350 if (ehci_set_async_schedule(ehcic, 1)) return -1;
Nico Huber0421dc82012-05-21 14:53:43 +0200351
352 /* wait for result */
353 result = wait_for_tds(head);
354
355 /* disable async schedule */
356 ehci_set_async_schedule(ehcic, 0);
357
358 return result;
359}
360
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000361static int ehci_bulk (endpoint_t *ep, int size, u8 *data, int finalize)
362{
363 int result = 0;
Julius Wernere9738db2013-02-21 13:41:40 -0800364 int remaining = size;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000365 int endp = ep->endpoint & 0xf;
366 int pid = (ep->direction==IN)?EHCI_IN:EHCI_OUT;
367
Nico Huber1ab60752012-05-23 09:21:54 +0200368 int hubaddr = 0, hubport = 0;
369 if (ep->dev->speed < 2) {
370 /* we need a split transaction */
371 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
Julius Wernere9738db2013-02-21 13:41:40 -0800372 return -1;
Nico Huber1ab60752012-05-23 09:21:54 +0200373 }
374
Vincent Palatin716375d2012-10-25 17:38:43 -0700375 qtd_t *head = memalign(64, sizeof(qtd_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000376 qtd_t *cur = head;
377 while (1) {
Nico Huber4f83d1b2012-11-23 11:58:57 +0100378 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100379 cur->token = QTD_ACTIVE |
380 (pid << QTD_PID_SHIFT) |
381 (0 << QTD_CERR_SHIFT);
Julius Wernere9738db2013-02-21 13:41:40 -0800382 u32 chunk = fill_td(cur, data, remaining);
383 remaining -= chunk;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000384 data += chunk;
385
Patrick Georgi8fa27872011-11-24 13:19:57 +0100386 cur->alt_next_qtd = QTD_TERMINATE;
Julius Wernere9738db2013-02-21 13:41:40 -0800387 if (remaining == 0) {
Patrick Georgi8fa27872011-11-24 13:19:57 +0100388 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000389 break;
390 } else {
Vincent Palatin716375d2012-10-25 17:38:43 -0700391 qtd_t *next = memalign(64, sizeof(qtd_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000392 cur->next_qtd = virt_to_phys(next);
393 cur = next;
394 }
395 }
396
397 /* create QH */
Vincent Palatin716375d2012-10-25 17:38:43 -0700398 ehci_qh_t *qh = memalign(64, sizeof(ehci_qh_t));
Nico Huber4f83d1b2012-11-23 11:58:57 +0100399 memset((void *)qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100400 qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH;
401 qh->epchar = ep->dev->address |
402 (endp << QH_EP_SHIFT) |
403 (ep->dev->speed << QH_EPS_SHIFT) |
404 (0 << QH_DTC_SHIFT) |
405 (1 << QH_RECLAIM_HEAD_SHIFT) |
406 (ep->maxpacketsize << QH_MPS_SHIFT) |
407 (0 << QH_NAK_CNT_SHIFT);
Nico Huber1ab60752012-05-23 09:21:54 +0200408 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
409 (hubport << QH_PORT_NUMBER_SHIFT) |
410 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000411
412 qh->td.next_qtd = virt_to_phys(head);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100413 qh->td.token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
414 head->token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000415
Nico Huber0421dc82012-05-21 14:53:43 +0200416 result = ehci_process_async_schedule(
417 EHCI_INST(ep->dev->controller), qh, head);
Julius Wernere9738db2013-02-21 13:41:40 -0800418 if (result >= 0)
419 result = size - result;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000420
Patrick Georgi8fa27872011-11-24 13:19:57 +0100421 ep->toggle = (cur->token & QTD_TOGGLE_MASK) >> QTD_TOGGLE_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000422
423 free_qh_and_tds(qh, head);
Julius Wernere9738db2013-02-21 13:41:40 -0800424
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000425 return result;
426}
427
428
429/* FIXME: Handle control transfers as 3 QHs, so the 2nd stage can be >0x4000 bytes */
430static int ehci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq,
431 int dalen, u8 *data)
432{
433 int endp = 0; // this is control. always 0 (for now)
434 int toggle = 0;
435 int mlen = dev->endpoints[0].maxpacketsize;
436 int result = 0;
437
Nico Huber1ab60752012-05-23 09:21:54 +0200438 int hubaddr = 0, hubport = 0, non_hs_ctrl_ep = 0;
439 if (dev->speed < 2) {
440 /* we need a split transaction */
441 if (closest_usb2_hub(dev, &hubaddr, &hubport))
Julius Wernere9738db2013-02-21 13:41:40 -0800442 return -1;
Nico Huber1ab60752012-05-23 09:21:54 +0200443 non_hs_ctrl_ep = 1;
444 }
445
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000446 /* create qTDs */
Vincent Palatin716375d2012-10-25 17:38:43 -0700447 qtd_t *head = memalign(64, sizeof(qtd_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000448 qtd_t *cur = head;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100449 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100450 cur->token = QTD_ACTIVE |
451 (toggle?QTD_TOGGLE_DATA1:0) |
452 (EHCI_SETUP << QTD_PID_SHIFT) |
453 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000454 if (fill_td(cur, devreq, drlen) != drlen) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700455 usb_debug("ERROR: couldn't send the entire device request\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000456 }
Vincent Palatin716375d2012-10-25 17:38:43 -0700457 qtd_t *next = memalign(64, sizeof(qtd_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000458 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100459 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000460
461 /* FIXME: We're limited to 16-20K (depending on alignment) for payload for now.
462 * Figure out, how toggle can be set sensibly in this scenario */
463 if (dalen > 0) {
464 toggle ^= 1;
465 cur = next;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100466 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100467 cur->token = QTD_ACTIVE |
468 (toggle?QTD_TOGGLE_DATA1:0) |
469 (((dir == OUT)?EHCI_OUT:EHCI_IN) << QTD_PID_SHIFT) |
470 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000471 if (fill_td(cur, data, dalen) != dalen) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700472 usb_debug("ERROR: couldn't send the entire control payload\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000473 }
Vincent Palatin716375d2012-10-25 17:38:43 -0700474 next = memalign(64, sizeof(qtd_t));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000475 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100476 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000477 }
478
479 toggle = 1;
480 cur = next;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100481 memset((void *)cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100482 cur->token = QTD_ACTIVE |
483 (toggle?QTD_TOGGLE_DATA1:QTD_TOGGLE_DATA0) |
484 ((dir == OUT)?EHCI_IN:EHCI_OUT) << QTD_PID_SHIFT |
485 (0 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000486 fill_td(cur, NULL, 0);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100487 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
488 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000489
490 /* create QH */
Vincent Palatin716375d2012-10-25 17:38:43 -0700491 ehci_qh_t *qh = memalign(64, sizeof(ehci_qh_t));
Nico Huber4f83d1b2012-11-23 11:58:57 +0100492 memset((void *)qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100493 qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH;
494 qh->epchar = dev->address |
495 (endp << QH_EP_SHIFT) |
496 (dev->speed << QH_EPS_SHIFT) |
497 (1 << QH_DTC_SHIFT) | /* ctrl transfers are special: take toggle bit from TD */
498 (1 << QH_RECLAIM_HEAD_SHIFT) |
499 (mlen << QH_MPS_SHIFT) |
Nico Huber1ab60752012-05-23 09:21:54 +0200500 (non_hs_ctrl_ep << QH_NON_HS_CTRL_EP_SHIFT) |
Patrick Georgi8fa27872011-11-24 13:19:57 +0100501 (0 << QH_NAK_CNT_SHIFT);
Nico Huber1ab60752012-05-23 09:21:54 +0200502 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
503 (hubport << QH_PORT_NUMBER_SHIFT) |
504 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000505 qh->td.next_qtd = virt_to_phys(head);
506
Nico Huber0421dc82012-05-21 14:53:43 +0200507 result = ehci_process_async_schedule(
508 EHCI_INST(dev->controller), qh, head);
Julius Wernere9738db2013-02-21 13:41:40 -0800509 if (result >= 0)
510 result = dalen - result;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000511
512 free_qh_and_tds(qh, head);
513 return result;
514}
515
Nico Huber62eb5b32012-05-25 10:09:13 +0200516
517typedef struct _intr_qtd_t intr_qtd_t;
518
519struct _intr_qtd_t {
520 volatile qtd_t td;
521 u8 *data;
522 intr_qtd_t *next;
523};
524
525typedef struct {
526 volatile ehci_qh_t qh;
527 intr_qtd_t *head;
528 intr_qtd_t *tail;
Nico Huber0d120f82012-06-14 13:08:36 +0200529 intr_qtd_t *spare;
Nico Huber62eb5b32012-05-25 10:09:13 +0200530 u8 *data;
531 endpoint_t *endp;
532 int reqsize;
533} intr_queue_t;
534
535static void fill_intr_queue_td(
536 intr_queue_t *const intrq,
537 intr_qtd_t *const intr_qtd,
538 u8 *const data)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000539{
Nico Huber62eb5b32012-05-25 10:09:13 +0200540 const int pid = (intrq->endp->direction == IN) ? EHCI_IN
541 : (intrq->endp->direction == OUT) ? EHCI_OUT
542 : EHCI_SETUP;
543 const int cerr = (intrq->endp->dev->speed < 2) ? 1 : 0;
544
545 memset(intr_qtd, 0, sizeof(*intr_qtd));
546 intr_qtd->td.next_qtd = QTD_TERMINATE;
547 intr_qtd->td.alt_next_qtd = QTD_TERMINATE;
548 intr_qtd->td.token = QTD_ACTIVE |
549 (pid << QTD_PID_SHIFT) |
550 (cerr << QTD_CERR_SHIFT) |
551 ((intrq->endp->toggle & 1) << QTD_TOGGLE_SHIFT);
552 fill_td(&intr_qtd->td, data, intrq->reqsize);
553 intr_qtd->data = data;
554 intr_qtd->next = NULL;
555
556 intrq->endp->toggle ^= 1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000557}
558
Nico Huber62eb5b32012-05-25 10:09:13 +0200559static void ehci_destroy_intr_queue(endpoint_t *const, void *const);
560
561static void *ehci_create_intr_queue(
562 endpoint_t *const ep,
563 const int reqsize,
564 int reqcount,
565 const int reqtiming)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000566{
Nico Huber62eb5b32012-05-25 10:09:13 +0200567 int i;
568
569 if ((reqsize > (4 * 4096 + 1)) || /* the maximum for arbitrary aligned
570 data in five 4096 byte pages */
571 (reqtiming > 1024))
572 return NULL;
573 if (reqcount < 2) /* we need at least 2:
574 one for processing, one for the hc to advance to */
575 reqcount = 2;
576
577 int hubaddr = 0, hubport = 0;
578 if (ep->dev->speed < 2) {
579 /* we need a split transaction */
580 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
581 return NULL;
582 }
583
584 intr_queue_t *const intrq =
Vincent Palatin716375d2012-10-25 17:38:43 -0700585 (intr_queue_t *)memalign(64, sizeof(intr_queue_t));
Nico Huber0d120f82012-06-14 13:08:36 +0200586 /*
587 * reqcount data chunks
588 * plus one more spare, which we'll leave out of queue
589 */
590 u8 *data = (u8 *)malloc(reqsize * (reqcount + 1));
Nico Huber62eb5b32012-05-25 10:09:13 +0200591 if (!intrq || !data)
592 fatal("Not enough memory to create USB interrupt queue.\n");
593 intrq->data = data;
594 intrq->endp = ep;
595 intrq->reqsize = reqsize;
596
597 /* create #reqcount transfer descriptors (qTDs) */
Vincent Palatin716375d2012-10-25 17:38:43 -0700598 intrq->head = (intr_qtd_t *)memalign(64, sizeof(intr_qtd_t));
Nico Huber62eb5b32012-05-25 10:09:13 +0200599 intr_qtd_t *cur_td = intrq->head;
600 for (i = 0; i < reqcount; ++i) {
601 fill_intr_queue_td(intrq, cur_td, data);
602 data += reqsize;
603 if (i < reqcount - 1) {
604 /* create one more qTD */
605 intr_qtd_t *const next_td =
Vincent Palatin716375d2012-10-25 17:38:43 -0700606 (intr_qtd_t *)memalign(64, sizeof(intr_qtd_t));
Nico Huber62eb5b32012-05-25 10:09:13 +0200607 cur_td->td.next_qtd = virt_to_phys(&next_td->td);
608 cur_td->next = next_td;
609 cur_td = next_td;
610 }
611 }
612 intrq->tail = cur_td;
613
Nico Huber0d120f82012-06-14 13:08:36 +0200614 /* create spare qTD */
Vincent Palatin716375d2012-10-25 17:38:43 -0700615 intrq->spare = (intr_qtd_t *)memalign(64, sizeof(intr_qtd_t));
Nico Huber0d120f82012-06-14 13:08:36 +0200616 fill_intr_queue_td(intrq, intrq->spare, data);
617
Nico Huber62eb5b32012-05-25 10:09:13 +0200618 /* initialize QH */
619 const int endp = ep->endpoint & 0xf;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100620 memset((void *)&intrq->qh, 0, sizeof(intrq->qh));
Nico Huber62eb5b32012-05-25 10:09:13 +0200621 intrq->qh.horiz_link_ptr = PS_TERMINATE;
622 intrq->qh.epchar = ep->dev->address |
623 (endp << QH_EP_SHIFT) |
624 (ep->dev->speed << QH_EPS_SHIFT) |
625 (1 << QH_DTC_SHIFT) |
626 (0 << QH_RECLAIM_HEAD_SHIFT) |
627 (ep->maxpacketsize << QH_MPS_SHIFT) |
628 (0 << QH_NAK_CNT_SHIFT);
629 intrq->qh.epcaps = (1 << QH_PIPE_MULTIPLIER_SHIFT) |
630 (hubport << QH_PORT_NUMBER_SHIFT) |
631 (hubaddr << QH_HUB_ADDRESS_SHIFT) |
632 (0xfe << QH_UFRAME_CMASK_SHIFT) |
633 1 /* uFrame S-mask */;
634 intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td);
635
636 /* insert QH into periodic schedule */
637 int nothing_placed = 1;
638 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(ep->dev->controller)
639 ->operation->periodiclistbase);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200640 const u32 dummy_ptr = virt_to_phys(EHCI_INST(
641 ep->dev->controller)->dummy_qh) | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200642 for (i = 0; i < 1024; i += reqtiming) {
643 /* advance to the next free position */
Nico Huber3ca35ca2012-06-14 13:27:39 +0200644 while ((i < 1024) && (ps[i] != dummy_ptr)) ++i;
Nico Huber62eb5b32012-05-25 10:09:13 +0200645 if (i < 1024) {
646 ps[i] = virt_to_phys(&intrq->qh) | PS_TYPE_QH;
647 nothing_placed = 0;
648 }
649 }
650 if (nothing_placed) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700651 usb_debug("Error: Failed to place ehci interrupt queue head "
Nico Huber62eb5b32012-05-25 10:09:13 +0200652 "into periodic schedule: no space left\n");
653 ehci_destroy_intr_queue(ep, intrq);
654 return NULL;
655 }
656
657 return intrq;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000658}
659
Nico Huber62eb5b32012-05-25 10:09:13 +0200660static void ehci_destroy_intr_queue(endpoint_t *const ep, void *const queue)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000661{
Nico Huber62eb5b32012-05-25 10:09:13 +0200662 intr_queue_t *const intrq = (intr_queue_t *)queue;
663
664 /* remove QH from periodic schedule */
665 int i;
666 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(
667 ep->dev->controller)->operation->periodiclistbase);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200668 const u32 dummy_ptr = virt_to_phys(EHCI_INST(
669 ep->dev->controller)->dummy_qh) | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200670 for (i = 0; i < 1024; ++i) {
671 if ((ps[i] & PS_PTR_MASK) == virt_to_phys(&intrq->qh))
Nico Huber3ca35ca2012-06-14 13:27:39 +0200672 ps[i] = dummy_ptr;
Nico Huber62eb5b32012-05-25 10:09:13 +0200673 }
674
675 /* wait 1ms for frame to end */
676 mdelay(1);
677
678 while (intrq->head) {
679 /* disable qTD and destroy list */
680 intrq->head->td.next_qtd = QTD_TERMINATE;
681 intrq->head->td.token &= ~QTD_ACTIVE;
682
683 /* save and advance head ptr */
684 intr_qtd_t *const to_free = intrq->head;
685 intrq->head = intrq->head->next;
686
687 /* free current interrupt qTD */
688 free(to_free);
689 }
Nico Huber0d120f82012-06-14 13:08:36 +0200690 free(intrq->spare);
Nico Huber62eb5b32012-05-25 10:09:13 +0200691 free(intrq->data);
692 free(intrq);
693}
694
695static u8 *ehci_poll_intr_queue(void *const queue)
696{
697 intr_queue_t *const intrq = (intr_queue_t *)queue;
698
699 u8 *ret = NULL;
700
701 /* process if head qTD is inactive AND QH has been moved forward */
Nico Huber0d120f82012-06-14 13:08:36 +0200702 if (!(intrq->head->td.token & QTD_ACTIVE)) {
Nico Huber62eb5b32012-05-25 10:09:13 +0200703 if (!(intrq->head->td.token & QTD_STATUS_MASK))
704 ret = intrq->head->data;
705 else
Gabe Black93ded592012-11-01 15:44:10 -0700706 usb_debug("ehci_poll_intr_queue: transfer failed, "
Nico Huber62eb5b32012-05-25 10:09:13 +0200707 "status == 0x%02x\n",
708 intrq->head->td.token & QTD_STATUS_MASK);
709
Nico Huber0d120f82012-06-14 13:08:36 +0200710 /* insert spare qTD at the end and advance our tail ptr */
711 fill_intr_queue_td(intrq, intrq->spare, intrq->spare->data);
712 intrq->tail->td.next_qtd = virt_to_phys(&intrq->spare->td);
713 intrq->tail->next = intrq->spare;
Nico Huber62eb5b32012-05-25 10:09:13 +0200714 intrq->tail = intrq->tail->next;
Nico Huber0d120f82012-06-14 13:08:36 +0200715
716 /* reuse executed qTD as spare one and advance our head ptr */
717 intrq->spare = intrq->head;
718 intrq->head = intrq->head->next;
719 }
720 /* reset queue if we fully processed it after underrun */
Nico Huberaf169f42012-11-22 11:14:03 +0100721 else if ((intrq->qh.td.next_qtd & QTD_TERMINATE) &&
722 /* to prevent race conditions:
723 not our head and not active */
724 (intrq->qh.current_td_ptr !=
725 virt_to_phys(&intrq->head->td)) &&
726 !(intrq->qh.td.token & QTD_ACTIVE)) {
Gabe Black93ded592012-11-01 15:44:10 -0700727 usb_debug("resetting underrun ehci interrupt queue.\n");
Nico Huberaf169f42012-11-22 11:14:03 +0100728 intrq->qh.current_td_ptr = 0;
Nico Huber4f83d1b2012-11-23 11:58:57 +0100729 memset((void *)&intrq->qh.td, 0, sizeof(intrq->qh.td));
Nico Huber0d120f82012-06-14 13:08:36 +0200730 intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td);
Nico Huber62eb5b32012-05-25 10:09:13 +0200731 }
732 return ret;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000733}
734
735hci_t *
Nico Huber6e230662014-07-07 16:33:59 +0200736ehci_init (unsigned long physical_bar)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000737{
738 int i;
739 hci_t *controller = new_controller ();
740
741 if (!controller)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100742 fatal("Could not create USB controller instance.\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000743
744 controller->instance = malloc (sizeof (ehci_t));
745 if(!controller->instance)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100746 fatal("Not enough memory creating USB controller instance.\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000747
Anton Kochkov1c36ead2012-06-28 08:30:15 +0400748 controller->type = EHCI;
749
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000750 controller->start = ehci_start;
751 controller->stop = ehci_stop;
752 controller->reset = ehci_reset;
Nico Huber6e711c62012-11-12 16:20:32 +0100753 controller->init = ehci_reinit;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000754 controller->shutdown = ehci_shutdown;
755 controller->bulk = ehci_bulk;
756 controller->control = ehci_control;
Patrick Georgi482af6d2013-05-24 15:48:56 +0200757 controller->set_address = generic_set_address;
758 controller->finish_device_config = NULL;
759 controller->destroy_device = NULL;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000760 controller->create_intr_queue = ehci_create_intr_queue;
761 controller->destroy_intr_queue = ehci_destroy_intr_queue;
762 controller->poll_intr_queue = ehci_poll_intr_queue;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000763 for (i = 0; i < 128; i++) {
764 controller->devices[i] = 0;
765 }
766 init_device_entry (controller, 0);
767
Nico Huber5b9e6f12014-07-10 12:56:34 +0200768 EHCI_INST(controller)->capabilities = phys_to_virt(physical_bar);
769 EHCI_INST(controller)->operation = (hc_op_t *)(phys_to_virt(physical_bar) + EHCI_INST(controller)->capabilities->caplength);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000770
Vincent Palatin716375d2012-10-25 17:38:43 -0700771 /* Set the high address word (aka segment) if controller is 64-bit */
772 if (EHCI_INST(controller)->capabilities->hccparams & 1)
773 EHCI_INST(controller)->operation->ctrldssegment = 0;
774
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000775 /* Enable operation of controller */
776 controller->start(controller);
777
778 /* take over all ports. USB1 should be blind now */
779 EHCI_INST(controller)->operation->configflag = 1;
780
Nico Huber62eb5b32012-05-25 10:09:13 +0200781 /* Initialize periodic frame list */
782 /* 1024 32-bit pointers, 4kb aligned */
783 u32 *const periodic_list = (u32 *)memalign(4096, 1024 * sizeof(u32));
784 if (!periodic_list)
785 fatal("Not enough memory creating EHCI periodic frame list.\n");
Nico Huber3ca35ca2012-06-14 13:27:39 +0200786
787 /*
788 * Insert dummy QH in periodic frame list
789 * This helps with broken host controllers
790 * and doesn't violate the standard.
791 */
Vincent Palatin716375d2012-10-25 17:38:43 -0700792 EHCI_INST(controller)->dummy_qh = (ehci_qh_t *)memalign(64, sizeof(ehci_qh_t));
Nico Huber4f83d1b2012-11-23 11:58:57 +0100793 memset((void *)EHCI_INST(controller)->dummy_qh, 0,
Nico Huber3ca35ca2012-06-14 13:27:39 +0200794 sizeof(*EHCI_INST(controller)->dummy_qh));
795 EHCI_INST(controller)->dummy_qh->horiz_link_ptr = QH_TERMINATE;
Julius Werner4610f0e2013-08-22 16:24:09 -0700796 EHCI_INST(controller)->dummy_qh->td.next_qtd = QH_TERMINATE;
797 EHCI_INST(controller)->dummy_qh->td.alt_next_qtd = QH_TERMINATE;
Nico Huber62eb5b32012-05-25 10:09:13 +0200798 for (i = 0; i < 1024; ++i)
Nico Huber3ca35ca2012-06-14 13:27:39 +0200799 periodic_list[i] = virt_to_phys(EHCI_INST(controller)->dummy_qh)
800 | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200801
802 /* Make sure periodic schedule is disabled */
803 ehci_set_periodic_schedule(EHCI_INST(controller), 0);
804 /* Set periodic frame list pointer */
805 EHCI_INST(controller)->operation->periodiclistbase =
806 virt_to_phys(periodic_list);
807 /* Enable use of periodic schedule */
808 ehci_set_periodic_schedule(EHCI_INST(controller), 1);
809
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000810 /* TODO lots of stuff missing */
811
812 controller->devices[0]->controller = controller;
813 controller->devices[0]->init = ehci_rh_init;
814 controller->devices[0]->init (controller->devices[0]);
815
816 return controller;
817}
Stefan Reinauer8992e532013-05-02 16:16:41 -0700818
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700819#ifdef CONFIG_LP_USB_PCI
Stefan Reinauer8992e532013-05-02 16:16:41 -0700820hci_t *
821ehci_pci_init (pcidev_t addr)
822{
823 hci_t *controller;
824 u32 reg_base;
825
826 u32 pci_command = pci_read_config32(addr, PCI_COMMAND);
827 pci_command = (pci_command | PCI_COMMAND_MEMORY) & ~PCI_COMMAND_IO ;
828 pci_write_config32(addr, PCI_COMMAND, pci_command);
829
830 reg_base = pci_read_config32 (addr, USBBASE);
831
832 /* default value for frame length adjust */
833 pci_write_config8(addr, FLADJ, FLADJ_framelength(60000));
834
Nico Huber6e230662014-07-07 16:33:59 +0200835 controller = ehci_init((unsigned long)reg_base);
Stefan Reinauer8992e532013-05-02 16:16:41 -0700836
837 return controller;
838}
839#endif