blob: 4d0a209977dea50bd7be7b87ad99e5fb19c1a79b [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
Nico Huber3ca35ca2012-06-14 13:27:39 +020086 /* Free dummy QH */
87 free(EHCI_INST(controller)->dummy_qh);
88
Patrick Georgi01178bb2011-11-04 11:57:46 +010089 EHCI_INST(controller)->operation->configflag = 0;
Patrick Georgi7f43dc12010-09-25 17:01:13 +000090}
91
92enum { EHCI_OUT=0, EHCI_IN=1, EHCI_SETUP=2 };
93
Nico Huber1ab60752012-05-23 09:21:54 +020094/*
95 * returns the address of the closest USB2.0 hub, which is responsible for
96 * split transactions, along with the number of the used downstream port
97 */
98static int closest_usb2_hub(const usbdev_t *dev, int *const addr, int *const port)
99{
100 const usbdev_t *usb1dev;
101 do {
102 usb1dev = dev;
103 if ((dev->hub > 0) && (dev->hub < 128))
104 dev = dev->controller->devices[dev->hub];
105 else
106 dev = NULL;
107 } while (dev && (dev->speed < 2));
108 if (dev) {
109 *addr = usb1dev->hub;
110 *port = usb1dev->port;
111 return 0;
112 } else {
113 debug("ehci: Couldn't find closest USB2.0 hub.\n");
114 return 1;
115 }
116}
117
Patrick Georgi8fa27872011-11-24 13:19:57 +0100118/* returns handled bytes. assumes that the fields it writes are empty on entry */
119static int fill_td(qtd_t *td, void* data, int datalen)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000120{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000121 u32 total_len = 0;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100122 u32 page_no = 0;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000123
124 u32 start = virt_to_phys(data);
125 u32 page = start & ~4095;
126 u32 offset = start & 4095;
127 u32 page_len = 4096 - offset;
128
Patrick Georgi8fa27872011-11-24 13:19:57 +0100129 td->token |= 0 << QTD_CPAGE_SHIFT;
130 td->bufptrs[page_no++] = start;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000131
132 if (datalen <= page_len) {
133 total_len = datalen;
134 } else {
135 datalen -= page_len;
136 total_len += page_len;
137
Patrick Georgi8fa27872011-11-24 13:19:57 +0100138 while (page_no < 5) {
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000139 /* we have a continguous mapping between virtual and physical memory */
140 page += 4096;
141
Patrick Georgi8fa27872011-11-24 13:19:57 +0100142 td->bufptrs[page_no++] = page;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000143 if (datalen <= 4096) {
144 total_len += datalen;
145 break;
146 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000147 datalen -= 4096;
148 total_len += 4096;
Patrick Georgi8fa27872011-11-24 13:19:57 +0100149 }
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000150 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100151 td->token |= total_len << QTD_TOTAL_LEN_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000152 return total_len;
153}
154
155/* free up data structures */
Patrick Georgi8fa27872011-11-24 13:19:57 +0100156static void free_qh_and_tds(ehci_qh_t *qh, qtd_t *cur)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000157{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000158 qtd_t *next;
159 while (cur) {
160 next = (qtd_t*)phys_to_virt(cur->next_qtd & ~31);
161 free(cur);
162 cur = next;
163 }
164 free(qh);
165}
166
Patrick Georgi8fa27872011-11-24 13:19:57 +0100167static int wait_for_tds(qtd_t *head)
Stefan Reinauer528b43d2011-04-14 19:52:04 +0000168{
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000169 int result = 0;
170 qtd_t *cur = head;
171 while (1) {
172 if (0) dump_td(virt_to_phys(cur));
Nico Huber0421dc82012-05-21 14:53:43 +0200173
174 /* wait for results */
Nico Hubercef86922012-06-01 08:54:29 +0200175 /* how long to wait?
176 * tested with some USB2.0 flash sticks:
177 * TUR turn around took
178 * about 2s for the slowest (14cd:121c)
179 * max. 250ms for the others
180 * slowest non-TUR turn around took about 1.3s
181 * try 2s for now as a failed TUR is not fatal
Nico Huber0421dc82012-05-21 14:53:43 +0200182 */
Nico Hubercef86922012-06-01 08:54:29 +0200183 int timeout = 40000; /* time out after 40000 * 50us == 2s */
Nico Huber0421dc82012-05-21 14:53:43 +0200184 while ((cur->token & QTD_ACTIVE) && !(cur->token & QTD_HALTED)
185 && timeout--)
186 udelay(50);
187 if (timeout < 0) {
188 printf("Error: ehci: queue transfer "
189 "processing timed out.\n");
190 return 1;
191 }
Patrick Georgi8fa27872011-11-24 13:19:57 +0100192 if (cur->token & QTD_HALTED) {
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000193 printf("ERROR with packet\n");
194 dump_td(virt_to_phys(cur));
Mathias Krausec4716b42011-06-08 15:36:55 +0200195 debug("-----------------\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000196 return 1;
197 }
198 if (cur->next_qtd & 1) {
199 return 0;
200 }
201 if (0) dump_td(virt_to_phys(cur));
202 /* helps debugging the TD chain */
Mathias Krausec4716b42011-06-08 15:36:55 +0200203 if (0) debug("\nmoving from %x to %x\n", cur, phys_to_virt(cur->next_qtd));
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000204 cur = phys_to_virt(cur->next_qtd);
205 }
206 return result;
207}
208
Nico Huber0421dc82012-05-21 14:53:43 +0200209static int ehci_set_async_schedule(ehci_t *ehcic, int enable)
210{
211 /* Set async schedule status. */
212 if (enable)
213 ehcic->operation->usbcmd |= HC_OP_ASYNC_SCHED_EN;
214 else
215 ehcic->operation->usbcmd &= ~HC_OP_ASYNC_SCHED_EN;
216 /* Wait for the controller to accept async schedule status.
217 * This shouldn't take too long, but we should timeout nevertheless.
218 */
219 enable = enable ? HC_OP_ASYNC_SCHED_STAT : 0;
220 int timeout = 100; /* time out after 100ms */
221 while (((ehcic->operation->usbsts & HC_OP_ASYNC_SCHED_STAT) != enable)
222 && timeout--)
223 mdelay(1);
224 if (timeout < 0) {
225 debug("ehci async schedule status change timed out.\n");
226 return 1;
227 }
228 return 0;
229}
230
231static int ehci_process_async_schedule(
232 ehci_t *ehcic, ehci_qh_t *qhead, qtd_t *head)
233{
234 int result;
235
236 /* make sure async schedule is disabled */
237 if (ehci_set_async_schedule(ehcic, 0)) return 1;
238
239 /* hook up QH */
240 ehcic->operation->asynclistaddr = virt_to_phys(qhead);
241
242 /* start async schedule */
243 if (ehci_set_async_schedule(ehcic, 1)) return 1;
244
245 /* wait for result */
246 result = wait_for_tds(head);
247
248 /* disable async schedule */
249 ehci_set_async_schedule(ehcic, 0);
250
251 return result;
252}
253
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000254static int ehci_bulk (endpoint_t *ep, int size, u8 *data, int finalize)
255{
256 int result = 0;
257 int endp = ep->endpoint & 0xf;
258 int pid = (ep->direction==IN)?EHCI_IN:EHCI_OUT;
259
Nico Huber1ab60752012-05-23 09:21:54 +0200260 int hubaddr = 0, hubport = 0;
261 if (ep->dev->speed < 2) {
262 /* we need a split transaction */
263 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
264 return 1;
265 }
266
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000267 qtd_t *head = memalign(32, sizeof(qtd_t));
268 qtd_t *cur = head;
269 while (1) {
270 memset(cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100271 cur->token = QTD_ACTIVE |
272 (pid << QTD_PID_SHIFT) |
273 (0 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000274 u32 chunk = fill_td(cur, data, size);
275 size -= chunk;
276 data += chunk;
277
Patrick Georgi8fa27872011-11-24 13:19:57 +0100278 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000279 if (size == 0) {
Patrick Georgi8fa27872011-11-24 13:19:57 +0100280 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000281 break;
282 } else {
283 qtd_t *next = memalign(32, sizeof(qtd_t));
284 cur->next_qtd = virt_to_phys(next);
285 cur = next;
286 }
287 }
288
289 /* create QH */
290 ehci_qh_t *qh = memalign(32, sizeof(ehci_qh_t));
291 memset(qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100292 qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH;
293 qh->epchar = ep->dev->address |
294 (endp << QH_EP_SHIFT) |
295 (ep->dev->speed << QH_EPS_SHIFT) |
296 (0 << QH_DTC_SHIFT) |
297 (1 << QH_RECLAIM_HEAD_SHIFT) |
298 (ep->maxpacketsize << QH_MPS_SHIFT) |
299 (0 << QH_NAK_CNT_SHIFT);
Nico Huber1ab60752012-05-23 09:21:54 +0200300 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
301 (hubport << QH_PORT_NUMBER_SHIFT) |
302 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000303
304 qh->td.next_qtd = virt_to_phys(head);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100305 qh->td.token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
306 head->token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000307
Nico Huber0421dc82012-05-21 14:53:43 +0200308 result = ehci_process_async_schedule(
309 EHCI_INST(ep->dev->controller), qh, head);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000310
Patrick Georgi8fa27872011-11-24 13:19:57 +0100311 ep->toggle = (cur->token & QTD_TOGGLE_MASK) >> QTD_TOGGLE_SHIFT;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000312
313 free_qh_and_tds(qh, head);
314 return result;
315}
316
317
318/* FIXME: Handle control transfers as 3 QHs, so the 2nd stage can be >0x4000 bytes */
319static int ehci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq,
320 int dalen, u8 *data)
321{
322 int endp = 0; // this is control. always 0 (for now)
323 int toggle = 0;
324 int mlen = dev->endpoints[0].maxpacketsize;
325 int result = 0;
326
Nico Huber1ab60752012-05-23 09:21:54 +0200327 int hubaddr = 0, hubport = 0, non_hs_ctrl_ep = 0;
328 if (dev->speed < 2) {
329 /* we need a split transaction */
330 if (closest_usb2_hub(dev, &hubaddr, &hubport))
331 return 1;
332 non_hs_ctrl_ep = 1;
333 }
334
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000335 /* create qTDs */
336 qtd_t *head = memalign(32, sizeof(qtd_t));
337 qtd_t *cur = head;
338 memset(cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100339 cur->token = QTD_ACTIVE |
340 (toggle?QTD_TOGGLE_DATA1:0) |
341 (EHCI_SETUP << QTD_PID_SHIFT) |
342 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000343 if (fill_td(cur, devreq, drlen) != drlen) {
344 printf("ERROR: couldn't send the entire device request\n");
345 }
346 qtd_t *next = memalign(32, sizeof(qtd_t));
347 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100348 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000349
350 /* FIXME: We're limited to 16-20K (depending on alignment) for payload for now.
351 * Figure out, how toggle can be set sensibly in this scenario */
352 if (dalen > 0) {
353 toggle ^= 1;
354 cur = next;
355 memset(cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100356 cur->token = QTD_ACTIVE |
357 (toggle?QTD_TOGGLE_DATA1:0) |
358 (((dir == OUT)?EHCI_OUT:EHCI_IN) << QTD_PID_SHIFT) |
359 (3 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000360 if (fill_td(cur, data, dalen) != dalen) {
361 printf("ERROR: couldn't send the entire control payload\n");
362 }
363 next = memalign(32, sizeof(qtd_t));
364 cur->next_qtd = virt_to_phys(next);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100365 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000366 }
367
368 toggle = 1;
369 cur = next;
370 memset(cur, 0, sizeof(qtd_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100371 cur->token = QTD_ACTIVE |
372 (toggle?QTD_TOGGLE_DATA1:QTD_TOGGLE_DATA0) |
373 ((dir == OUT)?EHCI_IN:EHCI_OUT) << QTD_PID_SHIFT |
374 (0 << QTD_CERR_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000375 fill_td(cur, NULL, 0);
Patrick Georgi8fa27872011-11-24 13:19:57 +0100376 cur->next_qtd = virt_to_phys(0) | QTD_TERMINATE;
377 cur->alt_next_qtd = QTD_TERMINATE;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000378
379 /* create QH */
380 ehci_qh_t *qh = memalign(32, sizeof(ehci_qh_t));
381 memset(qh, 0, sizeof(ehci_qh_t));
Patrick Georgi8fa27872011-11-24 13:19:57 +0100382 qh->horiz_link_ptr = virt_to_phys(qh) | QH_QH;
383 qh->epchar = dev->address |
384 (endp << QH_EP_SHIFT) |
385 (dev->speed << QH_EPS_SHIFT) |
386 (1 << QH_DTC_SHIFT) | /* ctrl transfers are special: take toggle bit from TD */
387 (1 << QH_RECLAIM_HEAD_SHIFT) |
388 (mlen << QH_MPS_SHIFT) |
Nico Huber1ab60752012-05-23 09:21:54 +0200389 (non_hs_ctrl_ep << QH_NON_HS_CTRL_EP_SHIFT) |
Patrick Georgi8fa27872011-11-24 13:19:57 +0100390 (0 << QH_NAK_CNT_SHIFT);
Nico Huber1ab60752012-05-23 09:21:54 +0200391 qh->epcaps = (3 << QH_PIPE_MULTIPLIER_SHIFT) |
392 (hubport << QH_PORT_NUMBER_SHIFT) |
393 (hubaddr << QH_HUB_ADDRESS_SHIFT);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000394 qh->td.next_qtd = virt_to_phys(head);
395
Nico Huber0421dc82012-05-21 14:53:43 +0200396 result = ehci_process_async_schedule(
397 EHCI_INST(dev->controller), qh, head);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000398
399 free_qh_and_tds(qh, head);
400 return result;
401}
402
Nico Huber62eb5b32012-05-25 10:09:13 +0200403
404typedef struct _intr_qtd_t intr_qtd_t;
405
406struct _intr_qtd_t {
407 volatile qtd_t td;
408 u8 *data;
409 intr_qtd_t *next;
410};
411
412typedef struct {
413 volatile ehci_qh_t qh;
414 intr_qtd_t *head;
415 intr_qtd_t *tail;
Nico Huber0d120f82012-06-14 13:08:36 +0200416 intr_qtd_t *spare;
Nico Huber62eb5b32012-05-25 10:09:13 +0200417 u8 *data;
418 endpoint_t *endp;
419 int reqsize;
420} intr_queue_t;
421
422static void fill_intr_queue_td(
423 intr_queue_t *const intrq,
424 intr_qtd_t *const intr_qtd,
425 u8 *const data)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000426{
Nico Huber62eb5b32012-05-25 10:09:13 +0200427 const int pid = (intrq->endp->direction == IN) ? EHCI_IN
428 : (intrq->endp->direction == OUT) ? EHCI_OUT
429 : EHCI_SETUP;
430 const int cerr = (intrq->endp->dev->speed < 2) ? 1 : 0;
431
432 memset(intr_qtd, 0, sizeof(*intr_qtd));
433 intr_qtd->td.next_qtd = QTD_TERMINATE;
434 intr_qtd->td.alt_next_qtd = QTD_TERMINATE;
435 intr_qtd->td.token = QTD_ACTIVE |
436 (pid << QTD_PID_SHIFT) |
437 (cerr << QTD_CERR_SHIFT) |
438 ((intrq->endp->toggle & 1) << QTD_TOGGLE_SHIFT);
439 fill_td(&intr_qtd->td, data, intrq->reqsize);
440 intr_qtd->data = data;
441 intr_qtd->next = NULL;
442
443 intrq->endp->toggle ^= 1;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000444}
445
Nico Huber62eb5b32012-05-25 10:09:13 +0200446static void ehci_destroy_intr_queue(endpoint_t *const, void *const);
447
448static void *ehci_create_intr_queue(
449 endpoint_t *const ep,
450 const int reqsize,
451 int reqcount,
452 const int reqtiming)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000453{
Nico Huber62eb5b32012-05-25 10:09:13 +0200454 int i;
455
456 if ((reqsize > (4 * 4096 + 1)) || /* the maximum for arbitrary aligned
457 data in five 4096 byte pages */
458 (reqtiming > 1024))
459 return NULL;
460 if (reqcount < 2) /* we need at least 2:
461 one for processing, one for the hc to advance to */
462 reqcount = 2;
463
464 int hubaddr = 0, hubport = 0;
465 if (ep->dev->speed < 2) {
466 /* we need a split transaction */
467 if (closest_usb2_hub(ep->dev, &hubaddr, &hubport))
468 return NULL;
469 }
470
471 intr_queue_t *const intrq =
472 (intr_queue_t *)memalign(32, sizeof(intr_queue_t));
Nico Huber0d120f82012-06-14 13:08:36 +0200473 /*
474 * reqcount data chunks
475 * plus one more spare, which we'll leave out of queue
476 */
477 u8 *data = (u8 *)malloc(reqsize * (reqcount + 1));
Nico Huber62eb5b32012-05-25 10:09:13 +0200478 if (!intrq || !data)
479 fatal("Not enough memory to create USB interrupt queue.\n");
480 intrq->data = data;
481 intrq->endp = ep;
482 intrq->reqsize = reqsize;
483
484 /* create #reqcount transfer descriptors (qTDs) */
485 intrq->head = (intr_qtd_t *)memalign(32, sizeof(intr_qtd_t));
486 intr_qtd_t *cur_td = intrq->head;
487 for (i = 0; i < reqcount; ++i) {
488 fill_intr_queue_td(intrq, cur_td, data);
489 data += reqsize;
490 if (i < reqcount - 1) {
491 /* create one more qTD */
492 intr_qtd_t *const next_td =
493 (intr_qtd_t *)memalign(32, sizeof(intr_qtd_t));
494 cur_td->td.next_qtd = virt_to_phys(&next_td->td);
495 cur_td->next = next_td;
496 cur_td = next_td;
497 }
498 }
499 intrq->tail = cur_td;
500
Nico Huber0d120f82012-06-14 13:08:36 +0200501 /* create spare qTD */
502 intrq->spare = (intr_qtd_t *)memalign(32, sizeof(intr_qtd_t));
503 fill_intr_queue_td(intrq, intrq->spare, data);
504
Nico Huber62eb5b32012-05-25 10:09:13 +0200505 /* initialize QH */
506 const int endp = ep->endpoint & 0xf;
507 memset(&intrq->qh, 0, sizeof(intrq->qh));
508 intrq->qh.horiz_link_ptr = PS_TERMINATE;
509 intrq->qh.epchar = ep->dev->address |
510 (endp << QH_EP_SHIFT) |
511 (ep->dev->speed << QH_EPS_SHIFT) |
512 (1 << QH_DTC_SHIFT) |
513 (0 << QH_RECLAIM_HEAD_SHIFT) |
514 (ep->maxpacketsize << QH_MPS_SHIFT) |
515 (0 << QH_NAK_CNT_SHIFT);
516 intrq->qh.epcaps = (1 << QH_PIPE_MULTIPLIER_SHIFT) |
517 (hubport << QH_PORT_NUMBER_SHIFT) |
518 (hubaddr << QH_HUB_ADDRESS_SHIFT) |
519 (0xfe << QH_UFRAME_CMASK_SHIFT) |
520 1 /* uFrame S-mask */;
521 intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td);
522
523 /* insert QH into periodic schedule */
524 int nothing_placed = 1;
525 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(ep->dev->controller)
526 ->operation->periodiclistbase);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200527 const u32 dummy_ptr = virt_to_phys(EHCI_INST(
528 ep->dev->controller)->dummy_qh) | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200529 for (i = 0; i < 1024; i += reqtiming) {
530 /* advance to the next free position */
Nico Huber3ca35ca2012-06-14 13:27:39 +0200531 while ((i < 1024) && (ps[i] != dummy_ptr)) ++i;
Nico Huber62eb5b32012-05-25 10:09:13 +0200532 if (i < 1024) {
533 ps[i] = virt_to_phys(&intrq->qh) | PS_TYPE_QH;
534 nothing_placed = 0;
535 }
536 }
537 if (nothing_placed) {
538 printf("Error: Failed to place ehci interrupt queue head "
539 "into periodic schedule: no space left\n");
540 ehci_destroy_intr_queue(ep, intrq);
541 return NULL;
542 }
543
544 return intrq;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000545}
546
Nico Huber62eb5b32012-05-25 10:09:13 +0200547static void ehci_destroy_intr_queue(endpoint_t *const ep, void *const queue)
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000548{
Nico Huber62eb5b32012-05-25 10:09:13 +0200549 intr_queue_t *const intrq = (intr_queue_t *)queue;
550
551 /* remove QH from periodic schedule */
552 int i;
553 u32 *const ps = (u32 *)phys_to_virt(EHCI_INST(
554 ep->dev->controller)->operation->periodiclistbase);
Nico Huber3ca35ca2012-06-14 13:27:39 +0200555 const u32 dummy_ptr = virt_to_phys(EHCI_INST(
556 ep->dev->controller)->dummy_qh) | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200557 for (i = 0; i < 1024; ++i) {
558 if ((ps[i] & PS_PTR_MASK) == virt_to_phys(&intrq->qh))
Nico Huber3ca35ca2012-06-14 13:27:39 +0200559 ps[i] = dummy_ptr;
Nico Huber62eb5b32012-05-25 10:09:13 +0200560 }
561
562 /* wait 1ms for frame to end */
563 mdelay(1);
564
565 while (intrq->head) {
566 /* disable qTD and destroy list */
567 intrq->head->td.next_qtd = QTD_TERMINATE;
568 intrq->head->td.token &= ~QTD_ACTIVE;
569
570 /* save and advance head ptr */
571 intr_qtd_t *const to_free = intrq->head;
572 intrq->head = intrq->head->next;
573
574 /* free current interrupt qTD */
575 free(to_free);
576 }
Nico Huber0d120f82012-06-14 13:08:36 +0200577 free(intrq->spare);
Nico Huber62eb5b32012-05-25 10:09:13 +0200578 free(intrq->data);
579 free(intrq);
580}
581
582static u8 *ehci_poll_intr_queue(void *const queue)
583{
584 intr_queue_t *const intrq = (intr_queue_t *)queue;
585
586 u8 *ret = NULL;
587
588 /* process if head qTD is inactive AND QH has been moved forward */
Nico Huber0d120f82012-06-14 13:08:36 +0200589 if (!(intrq->head->td.token & QTD_ACTIVE)) {
Nico Huber62eb5b32012-05-25 10:09:13 +0200590 if (!(intrq->head->td.token & QTD_STATUS_MASK))
591 ret = intrq->head->data;
592 else
593 debug("ehci_poll_intr_queue: transfer failed, "
594 "status == 0x%02x\n",
595 intrq->head->td.token & QTD_STATUS_MASK);
596
Nico Huber0d120f82012-06-14 13:08:36 +0200597 /* insert spare qTD at the end and advance our tail ptr */
598 fill_intr_queue_td(intrq, intrq->spare, intrq->spare->data);
599 intrq->tail->td.next_qtd = virt_to_phys(&intrq->spare->td);
600 intrq->tail->next = intrq->spare;
Nico Huber62eb5b32012-05-25 10:09:13 +0200601 intrq->tail = intrq->tail->next;
Nico Huber0d120f82012-06-14 13:08:36 +0200602
603 /* reuse executed qTD as spare one and advance our head ptr */
604 intrq->spare = intrq->head;
605 intrq->head = intrq->head->next;
606 }
607 /* reset queue if we fully processed it after underrun */
608 else if (intrq->qh.td.next_qtd & QTD_TERMINATE) {
609 debug("resetting underrun ehci interrupt queue.\n");
610 memset(&intrq->qh.td, 0, sizeof(intrq->qh.td));
611 intrq->qh.td.next_qtd = virt_to_phys(&intrq->head->td);
Nico Huber62eb5b32012-05-25 10:09:13 +0200612 }
613 return ret;
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000614}
615
616hci_t *
617ehci_init (pcidev_t addr)
618{
619 int i;
620 hci_t *controller = new_controller ();
621
622 if (!controller)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100623 fatal("Could not create USB controller instance.\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000624
625 controller->instance = malloc (sizeof (ehci_t));
626 if(!controller->instance)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100627 fatal("Not enough memory creating USB controller instance.\n");
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000628
629#define PCI_COMMAND 4
630#define PCI_COMMAND_IO 1
631#define PCI_COMMAND_MEMORY 2
632#define PCI_COMMAND_MASTER 4
633
634 u32 pci_command = pci_read_config32(addr, PCI_COMMAND);
635 pci_command = (pci_command | PCI_COMMAND_MEMORY) & ~PCI_COMMAND_IO ;
636 pci_write_config32(addr, PCI_COMMAND, pci_command);
637
Anton Kochkov1c36ead2012-06-28 08:30:15 +0400638 controller->type = EHCI;
639
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000640 controller->start = ehci_start;
641 controller->stop = ehci_stop;
642 controller->reset = ehci_reset;
643 controller->shutdown = ehci_shutdown;
644 controller->bulk = ehci_bulk;
645 controller->control = ehci_control;
646 controller->create_intr_queue = ehci_create_intr_queue;
647 controller->destroy_intr_queue = ehci_destroy_intr_queue;
648 controller->poll_intr_queue = ehci_poll_intr_queue;
Steven A. Falco9229af92011-07-13 21:01:26 -0400649 controller->bus_address = addr;
Anton Kochkov2e33a652012-06-20 04:03:37 +0400650 controller->reg_base = pci_read_config32 (controller->bus_address, USBBASE);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000651 for (i = 0; i < 128; i++) {
652 controller->devices[i] = 0;
653 }
654 init_device_entry (controller, 0);
655
Anton Kochkov2e33a652012-06-20 04:03:37 +0400656 EHCI_INST(controller)->capabilities = phys_to_virt(controller->reg_base);
657 EHCI_INST(controller)->operation = (hc_op_t *)(phys_to_virt(controller->reg_base) + EHCI_INST(controller)->capabilities->caplength);
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000658
659 /* default value for frame length adjust */
660 pci_write_config8(addr, FLADJ, FLADJ_framelength(60000));
661
662 /* Enable operation of controller */
663 controller->start(controller);
664
665 /* take over all ports. USB1 should be blind now */
666 EHCI_INST(controller)->operation->configflag = 1;
667
Nico Huber62eb5b32012-05-25 10:09:13 +0200668 /* Initialize periodic frame list */
669 /* 1024 32-bit pointers, 4kb aligned */
670 u32 *const periodic_list = (u32 *)memalign(4096, 1024 * sizeof(u32));
671 if (!periodic_list)
672 fatal("Not enough memory creating EHCI periodic frame list.\n");
Nico Huber3ca35ca2012-06-14 13:27:39 +0200673
674 /*
675 * Insert dummy QH in periodic frame list
676 * This helps with broken host controllers
677 * and doesn't violate the standard.
678 */
679 EHCI_INST(controller)->dummy_qh = (ehci_qh_t *)memalign(32, sizeof(ehci_qh_t));
680 memset(EHCI_INST(controller)->dummy_qh, 0,
681 sizeof(*EHCI_INST(controller)->dummy_qh));
682 EHCI_INST(controller)->dummy_qh->horiz_link_ptr = QH_TERMINATE;
Nico Huber62eb5b32012-05-25 10:09:13 +0200683 for (i = 0; i < 1024; ++i)
Nico Huber3ca35ca2012-06-14 13:27:39 +0200684 periodic_list[i] = virt_to_phys(EHCI_INST(controller)->dummy_qh)
685 | PS_TYPE_QH;
Nico Huber62eb5b32012-05-25 10:09:13 +0200686
687 /* Make sure periodic schedule is disabled */
688 ehci_set_periodic_schedule(EHCI_INST(controller), 0);
689 /* Set periodic frame list pointer */
690 EHCI_INST(controller)->operation->periodiclistbase =
691 virt_to_phys(periodic_list);
692 /* Enable use of periodic schedule */
693 ehci_set_periodic_schedule(EHCI_INST(controller), 1);
694
Patrick Georgi7f43dc12010-09-25 17:01:13 +0000695 /* TODO lots of stuff missing */
696
697 controller->devices[0]->controller = controller;
698 controller->devices[0]->init = ehci_rh_init;
699 controller->devices[0]->init (controller->devices[0]);
700
701 return controller;
702}