blob: 1094c78560ed9d594aaf555bbfee9e9aefd0446e [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
30#include <libpayload.h>
31#include "ehci.h"
32#include "ehci_private.h"
33
Stefan Reinauer528b43d2011-04-14 19:52:04 +000034static void dump_td(u32 addr)
35{
Patrick Georgi7f43dc12010-09-25 17:01:13 +000036 qtd_t *td = phys_to_virt(addr);
Patrick Georgi8fa27872011-11-24 13:19:57 +010037 debug("td at phys(%x): status: %x\n\n", addr, td->token & QTD_STATUS_MASK);
38 debug("- cerr: %x, total_len: %x\n\n", (td->token & QTD_CERR_MASK) >> QTD_CERR_SHIFT,
39 (td->token & QTD_TOTAL_LEN_MASK) >> QTD_TOTAL_LEN_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +000040}
41
42static void ehci_start (hci_t *controller)
43{
Patrick Georgi8fa27872011-11-24 13:19:57 +010044 EHCI_INST(controller)->operation->usbcmd |= HC_OP_RS;
Patrick Georgi7f43dc12010-09-25 17:01:13 +000045}
46
47static void ehci_stop (hci_t *controller)
48{
Patrick Georgi8fa27872011-11-24 13:19:57 +010049 EHCI_INST(controller)->operation->usbcmd &= ~HC_OP_RS;
Patrick Georgi7f43dc12010-09-25 17:01:13 +000050}
51
52static void ehci_reset (hci_t *controller)
53{
54
55}
56
Nico Huber62eb5b32012-05-25 10:09:13 +020057static int ehci_set_periodic_schedule(ehci_t *ehcic, int enable)
58{
59 /* Set periodic schedule status. */
60 if (enable)
61 ehcic->operation->usbcmd |= HC_OP_PERIODIC_SCHED_EN;
62 else
63 ehcic->operation->usbcmd &= ~HC_OP_PERIODIC_SCHED_EN;
64 /* Wait for the controller to accept periodic schedule status.
65 * This shouldn't take too long, but we should timeout nevertheless.
66 */
67 enable = enable ? HC_OP_PERIODIC_SCHED_STAT : 0;
68 int timeout = 100; /* time out after 100ms */
69 while (((ehcic->operation->usbsts & HC_OP_PERIODIC_SCHED_STAT) != enable)
70 && timeout--)
71 mdelay(1);
72 if (timeout < 0) {
73 debug("ehci periodic schedule status change timed out.\n");
74 return 1;
75 }
76 return 0;
77}
78
Patrick Georgi7f43dc12010-09-25 17:01:13 +000079static void ehci_shutdown (hci_t *controller)
80{
Nico Huber62eb5b32012-05-25 10:09:13 +020081 /* Make sure periodic schedule is disabled */
82 ehci_set_periodic_schedule(EHCI_INST(controller), 0);
83 /* Free periodic frame list */
84 free(phys_to_virt(EHCI_INST(controller)->operation->periodiclistbase));
85
Patrick Georgi01178bb2011-11-04 11:57:46 +010086 EHCI_INST(controller)->operation->configflag = 0;
Patrick Georgi7f43dc12010-09-25 17:01:13 +000087}
88
89enum { EHCI_OUT=0, EHCI_IN=1, EHCI_SETUP=2 };
90
Nico Huber1ab60752012-05-23 09:21:54 +020091/*
92 * returns the address of the closest USB2.0 hub, which is responsible for
93 * split transactions, along with the number of the used downstream port
94 */
95static int closest_usb2_hub(const usbdev_t *dev, int *const addr, int *const port)
96{
97 const usbdev_t *usb1dev;
98 do {
99 usb1dev = dev;
100 if ((dev->hub > 0) && (dev->hub < 128))
101 dev = dev->controller->devices[dev->hub];
102 else
103 dev = NULL;
104 } while (dev && (dev->speed < 2));
105 if (dev) {
106 *addr = usb1dev->hub;
107 *port = usb1dev->port;
108 return 0;
109 } else {
110 debug("ehci: Couldn't find closest USB2.0 hub.\n");
111 return 1;
112 }
113}
114
Patrick Georgi8fa27872011-11-24 13:19:57 +0100115/* returns handled bytes. assumes that the fields it writes are empty on entry */
116static int fill_td(qtd_t *td, void* data, int datalen)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000117{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000118 u32 total_len = 0;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100119 u32 page_no = 0;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000120
121 u32 start = virt_to_phys(data);
122 u32 page = start & ~4095;
123 u32 offset = start & 4095;
124 u32 page_len = 4096 - offset;
125
Patrick Georgi8fa27872011-11-24 13:19:57 +0100126 td->token |= 0 << QTD_CPAGE_SHIFT;
127 td->bufptrs[page_no++] = start;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000128
129 if (datalen <= page_len) {
130 total_len = datalen;
131 } else {
132 datalen -= page_len;
133 total_len += page_len;
134
Patrick Georgi8fa27872011-11-24 13:19:57 +0100135 while (page_no < 5) {
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000136 /* we have a continguous mapping between virtual and physical memory */
137 page += 4096;
138
Patrick Georgi8fa27872011-11-24 13:19:57 +0100139 td->bufptrs[page_no++] = page;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000140 if (datalen <= 4096) {
141 total_len += datalen;
142 break;
143 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000144 datalen -= 4096;
145 total_len += 4096;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100146 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000147 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100148 td->token |= total_len << QTD_TOTAL_LEN_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000149 return total_len;
150}
151
152/* free up data structures */
Patrick Georgi8fa27872011-11-24 13:19:57 +0100153static void free_qh_and_tds(ehci_qh_t *qh, qtd_t *cur)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000154{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000155 qtd_t *next;
156 while (cur) {
157 next = (qtd_t*)phys_to_virt(cur->next_qtd & ~31);
158 free(cur);
159 cur = next;
160 }
161 free(qh);
162}
163
Patrick Georgi8fa27872011-11-24 13:19:57 +0100164static int wait_for_tds(qtd_t *head)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000165{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000166 int result = 0;
167 qtd_t *cur = head;
168 while (1) {
169 if (0) dump_td(virt_to_phys(cur));
Nico Huber0421dc82012-05-21 14:53:43 +0200170
171 /* wait for results */
172 /* TOTEST: how long to wait?
173 * tested with some USB2.0 flash sticks:
174 * slowest took around 180ms
175 */
176 int timeout = 10000; /* time out after 10000 * 50us == 500ms */
177 while ((cur->token & QTD_ACTIVE) && !(cur->token & QTD_HALTED)
178 && timeout--)
179 udelay(50);
180 if (timeout < 0) {
181 printf("Error: ehci: queue transfer "
182 "processing timed out.\n");
183 return 1;
184 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100185 if (cur->token & QTD_HALTED) {
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000186 printf("ERROR with packet\n");
187 dump_td(virt_to_phys(cur));
Mathias Krausec4716b42011-06-08 15:36:55 +0200188 debug("-----------------\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000189 return 1;
190 }
191 if (cur->next_qtd & 1) {
192 return 0;
193 }
194 if (0) dump_td(virt_to_phys(cur));
195 /* helps debugging the TD chain */
Mathias Krausec4716b42011-06-08 15:36:55 +0200196 if (0) debug("\nmoving from %x to %x\n", cur, phys_to_virt(cur->next_qtd));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000197 cur = phys_to_virt(cur->next_qtd);
198 }
199 return result;
200}
201
Nico Huber0421dc82012-05-21 14:53:43 +0200202static int ehci_set_async_schedule(ehci_t *ehcic, int enable)
203{
204 /* Set async schedule status. */
205 if (enable)
206 ehcic->operation->usbcmd |= HC_OP_ASYNC_SCHED_EN;
207 else
208 ehcic->operation->usbcmd &= ~HC_OP_ASYNC_SCHED_EN;
209 /* Wait for the controller to accept async schedule status.
210 * This shouldn't take too long, but we should timeout nevertheless.
211 */
212 enable = enable ? HC_OP_ASYNC_SCHED_STAT : 0;
213 int timeout = 100; /* time out after 100ms */
214 while (((ehcic->operation->usbsts & HC_OP_ASYNC_SCHED_STAT) != enable)
215 && timeout--)
216 mdelay(1);
217 if (timeout < 0) {
218 debug("ehci async schedule status change timed out.\n");
219 return 1;
220 }
221 return 0;
222}
223
224static int ehci_process_async_schedule(
225 ehci_t *ehcic, ehci_qh_t *qhead, qtd_t *head)
226{
227 int result;
228
229 /* make sure async schedule is disabled */
230 if (ehci_set_async_schedule(ehcic, 0)) return 1;
231
232 /* hook up QH */
233 ehcic->operation->asynclistaddr = virt_to_phys(qhead);
234
235 /* start async schedule */
236 if (ehci_set_async_schedule(ehcic, 1)) return 1;
237
238 /* wait for result */
239 result = wait_for_tds(head);
240
241 /* disable async schedule */
242 ehci_set_async_schedule(ehcic, 0);
243
244 return result;
245}
246
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000247static int ehci_bulk (endpoint_t *ep, int size, u8 *data, int finalize)
248{
249 int result = 0;
250 int endp = ep->endpoint & 0xf;
251 int pid = (ep->direction==IN)?EHCI_IN:EHCI_OUT;
252
Nico Huber1ab60752012-05-23 09:21:54 +0200253 int hubaddr = 0, hubport = 0;
254 if (ep->dev->speed < 2) {
255 /* we need a split transaction */
256 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
257 return 1;
258 }
259
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000260 qtd_t *head = memalign(32, sizeof(qtd_t));
261 qtd_t *cur = head;
262 while (1) {
263 memset(cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100264 cur->token = QTD_ACTIVE |
265 (pid << QTD_PID_SHIFT) |
266 (0 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000267 u32 chunk = fill_td(cur, data, size);
268 size -= chunk;
269 data += chunk;
270
Patrick Georgi8fa27872011-11-24 13:19:57 +0100271 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000272 if (size == 0) {
Patrick Georgi8fa27872011-11-24 13:19:57 +0100273 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000274 break;
275 } else {
276 qtd_t *next = memalign(32, sizeof(qtd_t));
277 cur->next_qtd = virt_to_phys(next);
278 cur = next;
279 }
280 }
281
282 /* create QH */
283 ehci_qh_t *qh = memalign(32, sizeof(ehci_qh_t));
284 memset(qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100285 qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH;
286 qh->epchar = ep->dev->address |
287 (endp << QH_EP_SHIFT) |
288 (ep->dev->speed << QH_EPS_SHIFT) |
289 (0 << QH_DTC_SHIFT) |
290 (1 << QH_RECLAIM_HEAD_SHIFT) |
291 (ep->maxpacketsize << QH_MPS_SHIFT) |
292 (0 << QH_NAK_CNT_SHIFT);
Nico Huber1ab60752012-05-23 09:21:54 +0200293 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
294 (hubport << QH_PORT_NUMBER_SHIFT) |
295 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000296
297 qh->td.next_qtd = virt_to_phys(head);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100298 qh->td.token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
299 head->token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000300
Nico Huber0421dc82012-05-21 14:53:43 +0200301 result = ehci_process_async_schedule(
302 EHCI_INST(ep->dev->controller), qh, head);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000303
Patrick Georgi8fa27872011-11-24 13:19:57 +0100304 ep->toggle = (cur->token & QTD_TOGGLE_MASK) >> QTD_TOGGLE_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000305
306 free_qh_and_tds(qh, head);
307 return result;
308}
309
310
311/* FIXME: Handle control transfers as 3 QHs, so the 2nd stage can be >0x4000 bytes */
312static int ehci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq,
313 int dalen, u8 *data)
314{
315 int endp = 0; // this is control. always 0 (for now)
316 int toggle = 0;
317 int mlen = dev->endpoints[0].maxpacketsize;
318 int result = 0;
319
Nico Huber1ab60752012-05-23 09:21:54 +0200320 int hubaddr = 0, hubport = 0, non_hs_ctrl_ep = 0;
321 if (dev->speed < 2) {
322 /* we need a split transaction */
323 if (closest_usb2_hub(dev, &hubaddr, &hubport))
324 return 1;
325 non_hs_ctrl_ep = 1;
326 }
327
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000328 /* create qTDs */
329 qtd_t *head = memalign(32, sizeof(qtd_t));
330 qtd_t *cur = head;
331 memset(cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100332 cur->token = QTD_ACTIVE |
333 (toggle?QTD_TOGGLE_DATA1:0) |
334 (EHCI_SETUP << QTD_PID_SHIFT) |
335 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000336 if (fill_td(cur, devreq, drlen) != drlen) {
337 printf("ERROR: couldn't send the entire device request\n");
338 }
339 qtd_t *next = memalign(32, sizeof(qtd_t));
340 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100341 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000342
343 /* FIXME: We're limited to 16-20K (depending on alignment) for payload for now.
344 * Figure out, how toggle can be set sensibly in this scenario */
345 if (dalen > 0) {
346 toggle ^= 1;
347 cur = next;
348 memset(cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100349 cur->token = QTD_ACTIVE |
350 (toggle?QTD_TOGGLE_DATA1:0) |
351 (((dir == OUT)?EHCI_OUT:EHCI_IN) << QTD_PID_SHIFT) |
352 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000353 if (fill_td(cur, data, dalen) != dalen) {
354 printf("ERROR: couldn't send the entire control payload\n");
355 }
356 next = memalign(32, sizeof(qtd_t));
357 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100358 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000359 }
360
361 toggle = 1;
362 cur = next;
363 memset(cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100364 cur->token = QTD_ACTIVE |
365 (toggle?QTD_TOGGLE_DATA1:QTD_TOGGLE_DATA0) |
366 ((dir == OUT)?EHCI_IN:EHCI_OUT) << QTD_PID_SHIFT |
367 (0 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000368 fill_td(cur, NULL, 0);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100369 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
370 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000371
372 /* create QH */
373 ehci_qh_t *qh = memalign(32, sizeof(ehci_qh_t));
374 memset(qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100375 qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH;
376 qh->epchar = dev->address |
377 (endp << QH_EP_SHIFT) |
378 (dev->speed << QH_EPS_SHIFT) |
379 (1 << QH_DTC_SHIFT) | /* ctrl transfers are special: take toggle bit from TD */
380 (1 << QH_RECLAIM_HEAD_SHIFT) |
381 (mlen << QH_MPS_SHIFT) |
Nico Huber1ab60752012-05-23 09:21:54 +0200382 (non_hs_ctrl_ep << QH_NON_HS_CTRL_EP_SHIFT) |
Patrick Georgi8fa27872011-11-24 13:19:57 +0100383 (0 << QH_NAK_CNT_SHIFT);
Nico Huber1ab60752012-05-23 09:21:54 +0200384 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
385 (hubport << QH_PORT_NUMBER_SHIFT) |
386 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000387 qh->td.next_qtd = virt_to_phys(head);
388
Nico Huber0421dc82012-05-21 14:53:43 +0200389 result = ehci_process_async_schedule(
390 EHCI_INST(dev->controller), qh, head);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000391
392 free_qh_and_tds(qh, head);
393 return result;
394}
395
Nico Huber62eb5b32012-05-25 10:09:13 +0200396
397typedef struct _intr_qtd_t intr_qtd_t;
398
399struct _intr_qtd_t {
400 volatile qtd_t td;
401 u8 *data;
402 intr_qtd_t *next;
403};
404
405typedef struct {
406 volatile ehci_qh_t qh;
407 intr_qtd_t *head;
408 intr_qtd_t *tail;
409 u8 *data;
410 endpoint_t *endp;
411 int reqsize;
412} intr_queue_t;
413
414static void fill_intr_queue_td(
415 intr_queue_t *const intrq,
416 intr_qtd_t *const intr_qtd,
417 u8 *const data)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000418{
Nico Huber62eb5b32012-05-25 10:09:13 +0200419 const int pid = (intrq->endp->direction == IN) ? EHCI_IN
420 : (intrq->endp->direction == OUT) ? EHCI_OUT
421 : EHCI_SETUP;
422 const int cerr = (intrq->endp->dev->speed < 2) ? 1 : 0;
423
424 memset(intr_qtd, 0, sizeof(*intr_qtd));
425 intr_qtd->td.next_qtd = QTD_TERMINATE;
426 intr_qtd->td.alt_next_qtd = QTD_TERMINATE;
427 intr_qtd->td.token = QTD_ACTIVE |
428 (pid << QTD_PID_SHIFT) |
429 (cerr << QTD_CERR_SHIFT) |
430 ((intrq->endp->toggle & 1) << QTD_TOGGLE_SHIFT);
431 fill_td(&intr_qtd->td, data, intrq->reqsize);
432 intr_qtd->data = data;
433 intr_qtd->next = NULL;
434
435 intrq->endp->toggle ^= 1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000436}
437
Nico Huber62eb5b32012-05-25 10:09:13 +0200438static void ehci_destroy_intr_queue(endpoint_t *const, void *const);
439
440static void *ehci_create_intr_queue(
441 endpoint_t *const ep,
442 const int reqsize,
443 int reqcount,
444 const int reqtiming)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000445{
Nico Huber62eb5b32012-05-25 10:09:13 +0200446 int i;
447
448 if ((reqsize > (4 * 4096 + 1)) || /* the maximum for arbitrary aligned
449 data in five 4096 byte pages */
450 (reqtiming > 1024))
451 return NULL;
452 if (reqcount < 2) /* we need at least 2:
453 one for processing, one for the hc to advance to */
454 reqcount = 2;
455
456 int hubaddr = 0, hubport = 0;
457 if (ep->dev->speed < 2) {
458 /* we need a split transaction */
459 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
460 return NULL;
461 }
462
463 intr_queue_t *const intrq =
464 (intr_queue_t *)memalign(32, sizeof(intr_queue_t));
465 u8 *data = (u8 *)malloc(reqsize * reqcount);
466 if (!intrq || !data)
467 fatal("Not enough memory to create USB interrupt queue.\n");
468 intrq->data = data;
469 intrq->endp = ep;
470 intrq->reqsize = reqsize;
471
472 /* create #reqcount transfer descriptors (qTDs) */
473 intrq->head = (intr_qtd_t *)memalign(32, sizeof(intr_qtd_t));
474 intr_qtd_t *cur_td = intrq->head;
475 for (i = 0; i < reqcount; ++i) {
476 fill_intr_queue_td(intrq, cur_td, data);
477 data += reqsize;
478 if (i < reqcount - 1) {
479 /* create one more qTD */
480 intr_qtd_t *const next_td =
481 (intr_qtd_t *)memalign(32, sizeof(intr_qtd_t));
482 cur_td->td.next_qtd = virt_to_phys(&next_td->td);
483 cur_td->next = next_td;
484 cur_td = next_td;
485 }
486 }
487 intrq->tail = cur_td;
488
489 /* initialize QH */
490 const int endp = ep->endpoint & 0xf;
491 memset(&intrq->qh, 0, sizeof(intrq->qh));
492 intrq->qh.horiz_link_ptr = PS_TERMINATE;
493 intrq->qh.epchar = ep->dev->address |
494 (endp << QH_EP_SHIFT) |
495 (ep->dev->speed << QH_EPS_SHIFT) |
496 (1 << QH_DTC_SHIFT) |
497 (0 << QH_RECLAIM_HEAD_SHIFT) |
498 (ep->maxpacketsize << QH_MPS_SHIFT) |
499 (0 << QH_NAK_CNT_SHIFT);
500 intrq->qh.epcaps = (1 << QH_PIPE_MULTIPLIER_SHIFT) |
501 (hubport << QH_PORT_NUMBER_SHIFT) |
502 (hubaddr << QH_HUB_ADDRESS_SHIFT) |
503 (0xfe << QH_UFRAME_CMASK_SHIFT) |
504 1 /* uFrame S-mask */;
505 intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td);
506
507 /* insert QH into periodic schedule */
508 int nothing_placed = 1;
509 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(ep->dev->controller)
510 ->operation->periodiclistbase);
511 for (i = 0; i < 1024; i += reqtiming) {
512 /* advance to the next free position */
513 while ((i < 1024) && !(ps[i] & PS_TERMINATE)) ++i;
514 if (i < 1024) {
515 ps[i] = virt_to_phys(&intrq->qh) | PS_TYPE_QH;
516 nothing_placed = 0;
517 }
518 }
519 if (nothing_placed) {
520 printf("Error: Failed to place ehci interrupt queue head "
521 "into periodic schedule: no space left\n");
522 ehci_destroy_intr_queue(ep, intrq);
523 return NULL;
524 }
525
526 return intrq;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000527}
528
Nico Huber62eb5b32012-05-25 10:09:13 +0200529static void ehci_destroy_intr_queue(endpoint_t *const ep, void *const queue)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000530{
Nico Huber62eb5b32012-05-25 10:09:13 +0200531 intr_queue_t *const intrq = (intr_queue_t *)queue;
532
533 /* remove QH from periodic schedule */
534 int i;
535 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(
536 ep->dev->controller)->operation->periodiclistbase);
537 for (i = 0; i < 1024; ++i) {
538 if ((ps[i] & PS_PTR_MASK) == virt_to_phys(&intrq->qh))
539 ps[i] = PS_TERMINATE;
540 }
541
542 /* wait 1ms for frame to end */
543 mdelay(1);
544
545 while (intrq->head) {
546 /* disable qTD and destroy list */
547 intrq->head->td.next_qtd = QTD_TERMINATE;
548 intrq->head->td.token &= ~QTD_ACTIVE;
549
550 /* save and advance head ptr */
551 intr_qtd_t *const to_free = intrq->head;
552 intrq->head = intrq->head->next;
553
554 /* free current interrupt qTD */
555 free(to_free);
556 }
557 free(intrq->data);
558 free(intrq);
559}
560
561static u8 *ehci_poll_intr_queue(void *const queue)
562{
563 intr_queue_t *const intrq = (intr_queue_t *)queue;
564
565 u8 *ret = NULL;
566
567 /* process if head qTD is inactive AND QH has been moved forward */
568 if (!(intrq->head->td.token & QTD_ACTIVE) &&
569 (intrq->qh.current_td_ptr !=
570 virt_to_phys(&intrq->head->td))) {
571 if (!(intrq->head->td.token & QTD_STATUS_MASK))
572 ret = intrq->head->data;
573 else
574 debug("ehci_poll_intr_queue: transfer failed, "
575 "status == 0x%02x\n",
576 intrq->head->td.token & QTD_STATUS_MASK);
577
578 /* save and advance our head ptr */
579 intr_qtd_t *const new_td = intrq->head;
580 intrq->head = intrq->head->next;
581
582 /* reuse executed qTD */
583 fill_intr_queue_td(intrq, new_td, new_td->data);
584
585 /* at last insert reused qTD at the
586 * end and advance our tail ptr */
587 intrq->tail->td.next_qtd = virt_to_phys(&new_td->td);
588 intrq->tail->next = new_td;
589 intrq->tail = intrq->tail->next;
590 }
591 return ret;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000592}
593
594hci_t *
595ehci_init (pcidev_t addr)
596{
597 int i;
598 hci_t *controller = new_controller ();
599
600 if (!controller)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100601 fatal("Could not create USB controller instance.\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000602
603 controller->instance = malloc (sizeof (ehci_t));
604 if(!controller->instance)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100605 fatal("Not enough memory creating USB controller instance.\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000606
607#define PCI_COMMAND 4
608#define PCI_COMMAND_IO 1
609#define PCI_COMMAND_MEMORY 2
610#define PCI_COMMAND_MASTER 4
611
612 u32 pci_command = pci_read_config32(addr, PCI_COMMAND);
613 pci_command = (pci_command | PCI_COMMAND_MEMORY) & ~PCI_COMMAND_IO ;
614 pci_write_config32(addr, PCI_COMMAND, pci_command);
615
616 controller->start = ehci_start;
617 controller->stop = ehci_stop;
618 controller->reset = ehci_reset;
619 controller->shutdown = ehci_shutdown;
620 controller->bulk = ehci_bulk;
621 controller->control = ehci_control;
622 controller->create_intr_queue = ehci_create_intr_queue;
623 controller->destroy_intr_queue = ehci_destroy_intr_queue;
624 controller->poll_intr_queue = ehci_poll_intr_queue;
Steven A. Falco9229af92011-07-13 21:01:26 -0400625 controller->bus_address = addr;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000626 for (i = 0; i < 128; i++) {
627 controller->devices[i] = 0;
628 }
629 init_device_entry (controller, 0);
630
631 EHCI_INST(controller)->capabilities = phys_to_virt(pci_read_config32(addr, USBBASE));
632 EHCI_INST(controller)->operation = (hc_op_t *)(phys_to_virt(pci_read_config32(addr, USBBASE)) + EHCI_INST(controller)->capabilities->caplength);
633
634 /* default value for frame length adjust */
635 pci_write_config8(addr, FLADJ, FLADJ_framelength(60000));
636
637 /* Enable operation of controller */
638 controller->start(controller);
639
640 /* take over all ports. USB1 should be blind now */
641 EHCI_INST(controller)->operation->configflag = 1;
642
Nico Huber62eb5b32012-05-25 10:09:13 +0200643 /* Initialize periodic frame list */
644 /* 1024 32-bit pointers, 4kb aligned */
645 u32 *const periodic_list = (u32 *)memalign(4096, 1024 * sizeof(u32));
646 if (!periodic_list)
647 fatal("Not enough memory creating EHCI periodic frame list.\n");
648 for (i = 0; i < 1024; ++i)
649 periodic_list[i] = PS_TERMINATE;
650
651 /* Make sure periodic schedule is disabled */
652 ehci_set_periodic_schedule(EHCI_INST(controller), 0);
653 /* Set periodic frame list pointer */
654 EHCI_INST(controller)->operation->periodiclistbase =
655 virt_to_phys(periodic_list);
656 /* Enable use of periodic schedule */
657 ehci_set_periodic_schedule(EHCI_INST(controller), 1);
658
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000659 /* TODO lots of stuff missing */
660
661 controller->devices[0]->controller = controller;
662 controller->devices[0]->init = ehci_rh_init;
663 controller->devices[0]->init (controller->devices[0]);
664
665 return controller;
666}