1 /*****************************************************************************
2  * Copyright (c) 2013 IBM Corporation
3  * All rights reserved.
4  * This program and the accompanying materials
5  * are made available under the terms of the BSD License
6  * which accompanies this distribution, and is available at
7  * http://www.opensource.org/licenses/bsd-license.php
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12 
13 #include <string.h>
14 #include "usb.h"
15 #include "usb-core.h"
16 #include "usb-ehci.h"
17 #include "tools.h"
18 #include "paflof.h"
19 
20 #undef EHCI_DEBUG
21 //#define EHCI_DEBUG
22 #ifdef EHCI_DEBUG
23 #define dprintf(_x ...) do { printf(_x); } while(0)
24 #else
25 #define dprintf(_x ...)
26 #endif
27 
28 #ifdef EHCI_DEBUG
dump_ehci_regs(struct ehci_hcd * ehcd)29 static void dump_ehci_regs(struct ehci_hcd *ehcd)
30 {
31 	struct ehci_cap_regs *cap_regs;
32 	struct ehci_op_regs *op_regs;
33 
34 	cap_regs = ehcd->cap_regs;
35 	op_regs = ehcd->op_regs;
36 
37 	dprintf("\n - CAPLENGTH           %02X", read_reg8(&cap_regs->caplength));
38 	dprintf("\n - HCIVERSION          %04X", read_reg16(&cap_regs->hciversion));
39 	dprintf("\n - HCSPARAMS           %08X", read_reg32(&cap_regs->hcsparams));
40 	dprintf("\n - HCCPARAMS           %08X", read_reg32(&cap_regs->hccparams));
41 	dprintf("\n - HCSP_PORTROUTE      %016llX", read_reg64(&cap_regs->portroute));
42 	dprintf("\n");
43 
44 	dprintf("\n - USBCMD              %08X", read_reg32(&op_regs->usbcmd));
45 	dprintf("\n - USBSTS              %08X", read_reg32(&op_regs->usbsts));
46 	dprintf("\n - USBINTR             %08X", read_reg32(&op_regs->usbintr));
47 	dprintf("\n - FRINDEX             %08X", read_reg32(&op_regs->frindex));
48 	dprintf("\n - CTRLDSSEGMENT       %08X", read_reg32(&op_regs->ctrldssegment));
49 	dprintf("\n - PERIODICLISTBASE    %08X", read_reg32(&op_regs->periodiclistbase));
50 	dprintf("\n - ASYNCLISTADDR       %08X", read_reg32(&op_regs->asynclistaddr));
51 	dprintf("\n - CONFIGFLAG          %08X", read_reg32(&op_regs->configflag));
52 	dprintf("\n - PORTSC              %08X", read_reg32(&op_regs->portsc[0]));
53 	dprintf("\n");
54 }
55 #endif
56 
ehci_hub_check_ports(struct ehci_hcd * ehcd)57 static int ehci_hub_check_ports(struct ehci_hcd *ehcd)
58 {
59 	uint32_t num_ports, portsc, i;
60 	struct usb_dev *dev;
61 
62 	dprintf("%s: enter\n", __func__);
63 	num_ports = read_reg32(&ehcd->cap_regs->hcsparams) & HCS_NPORTS_MASK;
64 	for (i = 0; i < num_ports; i++) {
65 		dprintf("%s: device %d\n", __func__, i);
66 		portsc = read_reg32(&ehcd->op_regs->portsc[i]);
67 		if (portsc & PORT_CONNECT) { /* Device present */
68 			dprintf("usb-ehci: Device present on port %d\n", i);
69 			/* Reset the port */
70 			portsc = read_reg32(&ehcd->op_regs->portsc[i]);
71 			portsc = (portsc & ~PORT_PE) | PORT_RESET;
72 			write_reg32(&ehcd->op_regs->portsc[i], portsc);
73 			SLOF_msleep(20);
74 			portsc = read_reg32(&ehcd->op_regs->portsc[i]);
75 			portsc &= ~PORT_RESET;
76 			write_reg32(&ehcd->op_regs->portsc[i], portsc);
77 			SLOF_msleep(20);
78 			dev = usb_devpool_get();
79 			dprintf("usb-ehci: allocated device %p\n", dev);
80 			dev->hcidev = ehcd->hcidev;
81 			dev->speed = USB_HIGH_SPEED; /* TODO: Check for Low/Full speed device */
82 			if (usb_setup_new_device(dev, i))
83 				usb_slof_populate_new_device(dev);
84 			else
85 				printf("usb-ehci: unable to setup device on port %d\n", i);
86 		}
87 	}
88 	dprintf("%s: exit\n", __func__);
89 	return 0;
90 }
91 
ehci_hcd_init(struct ehci_hcd * ehcd)92 static int ehci_hcd_init(struct ehci_hcd *ehcd)
93 {
94 	uint32_t usbcmd;
95 	uint32_t time;
96 	struct ehci_framelist *fl;
97 	struct ehci_qh *qh_intr, *qh_async;
98 	int i;
99 	long fl_phys = 0, qh_intr_phys = 0, qh_async_phys;
100 
101 	/* Reset the host controller */
102 	time = SLOF_GetTimer() + 250;
103 	usbcmd = read_reg32(&ehcd->op_regs->usbcmd);
104 	write_reg32(&ehcd->op_regs->usbcmd, (usbcmd & ~(CMD_PSE | CMD_ASE)) | CMD_HCRESET);
105 	while (time > SLOF_GetTimer())
106 		cpu_relax();
107 	usbcmd = read_reg32(&ehcd->op_regs->usbcmd);
108 	if (usbcmd & CMD_HCRESET) {
109 		printf("usb-ehci: reset failed\n");
110 		return -1;
111 	}
112 
113 	/* Initialize periodic list */
114 	fl = SLOF_dma_alloc(sizeof(*fl));
115 	if (!fl) {
116 		printf("usb-ehci: Unable to allocate frame list\n");
117 		goto fail;
118 	}
119 	fl_phys = SLOF_dma_map_in(fl, sizeof(*fl), true);
120 	dprintf("fl %p, fl_phys %lx\n", fl, fl_phys);
121 
122 	/* TODO: allocate qh pool */
123 	qh_intr = SLOF_dma_alloc(sizeof(*qh_intr));
124 	if (!qh_intr) {
125 		printf("usb-ehci: Unable to allocate interrupt queue head\n");
126 		goto fail_qh_intr;
127 	}
128 	qh_intr_phys = SLOF_dma_map_in(qh_intr, sizeof(*qh_intr), true);
129 	dprintf("qh_intr %p, qh_intr_phys %lx\n", qh_intr, qh_intr_phys);
130 
131 	memset(qh_intr, 0, sizeof(*qh_intr));
132 	qh_intr->qh_ptr = QH_PTR_TERM;
133 	qh_intr->ep_cap2 = cpu_to_le32(0x01 << QH_SMASK_SHIFT);
134 	qh_intr->next_qtd = qh_intr->alt_next_qtd = QH_PTR_TERM;
135 	qh_intr->token = cpu_to_le32(QH_STS_HALTED);
136 	for (i = 0; i < FL_SIZE; i++)
137 		fl->fl_ptr[i] = cpu_to_le32(qh_intr_phys | EHCI_TYP_QH);
138 	write_reg32(&ehcd->op_regs->periodiclistbase, fl_phys);
139 
140 	/* Initialize async list */
141 	qh_async = SLOF_dma_alloc(sizeof(*qh_async));
142 	if (!qh_async) {
143 		printf("usb-ehci: Unable to allocate async queue head\n");
144 		goto fail_qh_async;
145 	}
146 	qh_async_phys = SLOF_dma_map_in(qh_async, sizeof(*qh_async), true);
147 	dprintf("qh_async %p, qh_async_phys %lx\n", qh_async, qh_async_phys);
148 
149 	memset(qh_async, 0, sizeof(*qh_async));
150 	qh_async->qh_ptr = cpu_to_le32(qh_async_phys | EHCI_TYP_QH);
151 	qh_async->ep_cap1 = cpu_to_le32(QH_CAP_H);
152 	qh_async->next_qtd = qh_async->alt_next_qtd = QH_PTR_TERM;
153 	qh_async->token = cpu_to_le32(QH_STS_HALTED);
154 	write_reg32(&ehcd->op_regs->asynclistaddr, qh_async_phys);
155 	ehcd->qh_async = qh_async;
156 	ehcd->qh_async_phys = qh_async_phys;
157 	ehcd->qh_intr = qh_intr;
158 	ehcd->qh_intr_phys = qh_intr_phys;
159 	ehcd->fl = fl;
160 	ehcd->fl_phys = fl_phys;
161 
162 	write_reg32(&ehcd->op_regs->usbcmd, usbcmd | CMD_ASE | CMD_RUN);
163 	write_reg32(&ehcd->op_regs->configflag, 1);
164 
165 	return 0;
166 
167 fail_qh_async:
168 	SLOF_dma_map_out(qh_intr_phys, qh_intr, sizeof(*qh_intr));
169 	SLOF_dma_free(qh_intr, sizeof(*qh_intr));
170 fail_qh_intr:
171 	SLOF_dma_map_out(fl_phys, fl, sizeof(*fl));
172 	SLOF_dma_free(fl, sizeof(*fl));
173 fail:
174 	return -1;
175 }
176 
ehci_hcd_exit(struct ehci_hcd * ehcd)177 static int ehci_hcd_exit(struct ehci_hcd *ehcd)
178 {
179 	uint32_t usbcmd;
180 
181 	if (!ehcd) {
182 		dprintf("NULL pointer\n");
183 		return false;
184 	}
185 
186 	usbcmd = read_reg32(&ehcd->op_regs->usbcmd);
187 	write_reg32(&ehcd->op_regs->usbcmd, usbcmd | ~CMD_RUN);
188 	write_reg32(&ehcd->op_regs->periodiclistbase, 0);
189 
190 	if (ehcd->pool) {
191 		SLOF_dma_map_out(ehcd->pool_phys, ehcd->pool, EHCI_PIPE_POOL_SIZE);
192 		SLOF_dma_free(ehcd->pool, EHCI_PIPE_POOL_SIZE);
193 	}
194 	if (ehcd->qh_intr) {
195 		SLOF_dma_map_out(ehcd->qh_intr_phys, ehcd->qh_intr, sizeof(struct ehci_qh));
196 		SLOF_dma_free(ehcd->qh_intr, sizeof(struct ehci_qh));
197 	}
198 	if (ehcd->qh_async) {
199 		SLOF_dma_map_out(ehcd->qh_async_phys, ehcd->qh_async, sizeof(struct ehci_qh));
200 		SLOF_dma_free(ehcd->qh_async, sizeof(struct ehci_qh));
201 	}
202 	if (ehcd->fl) {
203 		SLOF_dma_map_out(ehcd->fl_phys, ehcd->fl, sizeof(struct ehci_framelist));
204 		SLOF_dma_free(ehcd->fl, sizeof(struct ehci_framelist));
205 	}
206 	return true;
207 }
208 
ehci_alloc_pipe_pool(struct ehci_hcd * ehcd)209 static int ehci_alloc_pipe_pool(struct ehci_hcd *ehcd)
210 {
211 	struct ehci_pipe *epipe, *curr, *prev;
212 	unsigned int i, count;
213 	long epipe_phys = 0;
214 
215 	count = EHCI_PIPE_POOL_SIZE/sizeof(*epipe);
216 	ehcd->pool = epipe = SLOF_dma_alloc(EHCI_PIPE_POOL_SIZE);
217 	if (!epipe)
218 		return -1;
219 	ehcd->pool_phys = epipe_phys = SLOF_dma_map_in(epipe, EHCI_PIPE_POOL_SIZE, true);
220 	dprintf("%s: epipe %p, epipe_phys %lx\n", __func__, epipe, epipe_phys);
221 
222 	/* Although an array, link them */
223 	for (i = 0, curr = epipe, prev = NULL; i < count; i++, curr++) {
224 		if (prev)
225 			prev->pipe.next = &curr->pipe;
226 		curr->pipe.next = NULL;
227 		prev = curr;
228 		curr->qh_phys = epipe_phys + (curr - epipe) * sizeof(*curr) +
229 			offset_of(struct ehci_pipe, qh);
230 		dprintf("%s - %d: qh %p, qh_phys %lx\n", __func__,
231 			i, &curr->qh, curr->qh_phys);
232 	}
233 
234 	if (!ehcd->freelist)
235 		ehcd->freelist = &epipe->pipe;
236 	else
237 		ehcd->end->next = &epipe->pipe;
238 	ehcd->end = &prev->pipe;
239 
240 	return 0;
241 }
242 
ehci_init(struct usb_hcd_dev * hcidev)243 static void ehci_init(struct usb_hcd_dev *hcidev)
244 {
245 	struct ehci_hcd *ehcd;
246 
247 	printf("  EHCI: Initializing\n");
248 	dprintf("%s: device base address %p\n", __func__, hcidev->base);
249 
250 	ehcd = SLOF_alloc_mem(sizeof(*ehcd));
251 	if (!ehcd) {
252 		printf("usb-ehci: Unable to allocate memory\n");
253 		return;
254 	}
255 	memset(ehcd, 0, sizeof(*ehcd));
256 
257 	hcidev->nextaddr = 1;
258 	hcidev->priv = ehcd;
259 	ehcd->hcidev = hcidev;
260 	ehcd->cap_regs = (struct ehci_cap_regs *)(hcidev->base);
261 	ehcd->op_regs = (struct ehci_op_regs *)(hcidev->base +
262 						read_reg8(&ehcd->cap_regs->caplength));
263 #ifdef EHCI_DEBUG
264 	dump_ehci_regs(ehcd);
265 #endif
266 	ehci_hcd_init(ehcd);
267 	ehci_hub_check_ports(ehcd);
268 }
269 
ehci_exit(struct usb_hcd_dev * hcidev)270 static void ehci_exit(struct usb_hcd_dev *hcidev)
271 {
272 	struct ehci_hcd *ehcd;
273 	static int count = 0;
274 
275 	dprintf("%s: enter \n", __func__);
276 
277 	if (!hcidev && !hcidev->priv) {
278 		return;
279 	}
280 	count++;
281 	if (count > 1) {
282 		printf("%s: already called once \n", __func__);
283 		return;
284 	}
285 	ehcd = hcidev->priv;
286 	ehci_hcd_exit(ehcd);
287 	SLOF_free_mem(ehcd, sizeof(*ehcd));
288 	hcidev->priv = NULL;
289 }
290 
ehci_detect(void)291 static void ehci_detect(void)
292 {
293 
294 }
295 
ehci_disconnect(void)296 static void ehci_disconnect(void)
297 {
298 
299 }
300 
ehci_handshake(struct ehci_hcd * ehcd,uint32_t timeout)301 static int ehci_handshake(struct ehci_hcd *ehcd, uint32_t timeout)
302 {
303 	uint32_t usbsts = 0, time;
304 	uint32_t usbcmd;
305 	mb();
306 	usbcmd = read_reg32(&ehcd->op_regs->usbcmd);
307 	/* Ring a doorbell */
308 	write_reg32(&ehcd->op_regs->usbcmd, usbcmd | CMD_IAAD);
309 	mb();
310 	time = SLOF_GetTimer() + timeout;
311 	while ((time > SLOF_GetTimer())) {
312 		/* Wait for controller to confirm */
313 		usbsts = read_reg32(&ehcd->op_regs->usbsts);
314 		if (usbsts & STS_IAA) {
315 			/* Acknowledge it, for next doorbell to work */
316 			write_reg32(&ehcd->op_regs->usbsts, STS_IAA);
317 			return true;
318 		}
319 		cpu_relax();
320 	}
321 	return false;
322 }
323 
fill_qtd_buff(struct ehci_qtd * qtd,long data,uint32_t size)324 static int fill_qtd_buff(struct ehci_qtd *qtd, long data, uint32_t size)
325 {
326 	long i, rem;
327 	long pos = (data + 0x1000) & ~0xfff;
328 
329 	qtd->buffer[0] = cpu_to_le32(PTR_U32(data));
330 	for (i = 1; i < 5; i++) {
331 		if ((data + size - 1) >= pos) {
332 			//dprintf("data spans page boundary: %d, %p\n", i, pos);
333 			qtd->buffer[i] = cpu_to_le32(pos);
334 			pos += 0x1000;
335 		} else
336 			break;
337 	}
338 	if ((data + size) > pos)
339 		rem = data + size - pos;
340 	else
341 		rem = 0;
342 	return rem;
343 }
344 
ehci_send_ctrl(struct usb_pipe * pipe,struct usb_dev_req * req,void * data)345 static int ehci_send_ctrl(struct usb_pipe *pipe, struct usb_dev_req *req, void *data)
346 {
347 	struct ehci_hcd *ehcd;
348 	struct ehci_qtd *qtd, *qtds, *qtds_phys;
349 	struct ehci_pipe *epipe;
350 	uint32_t transfer_size = sizeof(*req);
351 	uint32_t datalen, pid;
352 	uint32_t time;
353 	long req_phys = 0, data_phys = 0;
354 	int ret = true;
355 
356 	if (pipe->type != USB_EP_TYPE_CONTROL) {
357 		printf("usb-ehci: Not a control pipe.\n");
358 		return false;
359 	}
360 
361 	ehcd = pipe->dev->hcidev->priv;
362 	qtds = qtd = SLOF_dma_alloc(sizeof(*qtds) * 3);
363 	if (!qtds) {
364 		printf("Error allocating qTDs.\n");
365 		return false;
366 	}
367 	qtds_phys = (struct ehci_qtd *)SLOF_dma_map_in(qtds, sizeof(*qtds) * 3, true);
368 	memset(qtds, 0, sizeof(*qtds) * 3);
369 	req_phys = SLOF_dma_map_in(req, sizeof(struct usb_dev_req), true);
370 	qtd->next_qtd = cpu_to_le32(PTR_U32(&qtds_phys[1]));
371 	qtd->alt_next_qtd = QH_PTR_TERM;
372 	qtd->token = cpu_to_le32((transfer_size << TOKEN_TBTT_SHIFT) |
373 			(3 << TOKEN_CERR_SHIFT) |
374 			(PID_SETUP << TOKEN_PID_SHIFT) |
375 			(QH_STS_ACTIVE << TOKEN_STATUS_SHIFT));
376 	fill_qtd_buff(qtd, req_phys, sizeof(*req));
377 
378 	qtd++;
379 	datalen = cpu_to_le16(req->wLength);
380 	pid = (req->bmRequestType & REQT_DIR_IN) ? PID_IN : PID_OUT;
381 	if (datalen) {
382 		data_phys = SLOF_dma_map_in(data, datalen, true);
383 		qtd->next_qtd = cpu_to_le32(PTR_U32(&qtds_phys[2]));
384 		qtd->alt_next_qtd = QH_PTR_TERM;
385 		qtd->token = cpu_to_le32((1 << TOKEN_DT_SHIFT) |
386 				(datalen << TOKEN_TBTT_SHIFT) |
387 				(3 << TOKEN_CERR_SHIFT) |
388 				(pid << TOKEN_PID_SHIFT) |
389 				(QH_STS_ACTIVE << TOKEN_STATUS_SHIFT));
390 		fill_qtd_buff(qtd, data_phys, datalen);
391 		qtd++;
392 	}
393 
394 	if (pid == PID_IN)
395 		pid = PID_OUT;
396 	else
397 		pid = PID_IN;
398 	qtd->next_qtd = QH_PTR_TERM;
399 	qtd->alt_next_qtd = QH_PTR_TERM;
400 	qtd->token = cpu_to_le32((1 << TOKEN_DT_SHIFT) |
401 			(3 << TOKEN_CERR_SHIFT) |
402 			(pid << TOKEN_PID_SHIFT) |
403 			(QH_STS_ACTIVE << TOKEN_STATUS_SHIFT));
404 
405 	/* link qtd to qh and attach to ehcd */
406 	mb();
407 	epipe = container_of(pipe, struct ehci_pipe, pipe);
408 	epipe->qh.next_qtd = cpu_to_le32(PTR_U32(qtds_phys));
409 	epipe->qh.qh_ptr = cpu_to_le32(ehcd->qh_async_phys | EHCI_TYP_QH);
410 	epipe->qh.ep_cap1 = cpu_to_le32((pipe->mps << QH_MPS_SHIFT) |
411 				(pipe->speed << QH_EPS_SHIFT) |
412 				(pipe->epno << QH_EP_SHIFT) |
413 				(pipe->dev->addr << QH_DEV_ADDR_SHIFT));
414 	mb();
415 
416 	ehcd->qh_async->qh_ptr = cpu_to_le32(epipe->qh_phys | EHCI_TYP_QH);
417 
418 	/* transfer data */
419 	mb();
420 	qtd = &qtds[0];
421 	time = SLOF_GetTimer() + USB_TIMEOUT;
422 	do {
423 		if (le32_to_cpu(qtd->token) & (QH_STS_ACTIVE << TOKEN_STATUS_SHIFT))
424 			mb();
425 		else
426 			qtd++;
427 
428 		if (time < SLOF_GetTimer()) { /* timed out */
429 			printf("usb-ehci: control transfer timed out_\n");
430 			ret = false;
431 			break;
432 		}
433 	} while (qtd->next_qtd != QH_PTR_TERM);
434 
435 	ehcd->qh_async->qh_ptr = cpu_to_le32(ehcd->qh_async_phys | EHCI_TYP_QH);
436 	mb();
437 	if (!ehci_handshake(ehcd, USB_TIMEOUT)) {
438 		printf("%s: handshake failed\n", __func__);
439 		ret = false;
440 	}
441 
442 	SLOF_dma_map_out(req_phys, req, sizeof(struct usb_dev_req));
443 	SLOF_dma_map_out(data_phys, data, datalen);
444 	SLOF_dma_map_out(PTR_U32(qtds_phys), qtds, sizeof(*qtds) * 3);
445 	SLOF_dma_free(qtds, sizeof(*qtds) * 3);
446 
447 	return ret;
448 }
449 
ehci_transfer_bulk(struct usb_pipe * pipe,void * td,void * td_phys,void * data_phys,int size)450 static int ehci_transfer_bulk(struct usb_pipe *pipe, void *td, void *td_phys,
451 			void *data_phys, int size)
452 {
453 	struct ehci_hcd *ehcd;
454 	struct ehci_qtd *qtd, *qtd_phys;
455 	struct ehci_pipe *epipe;
456 	uint32_t pid;
457 	int i, rem, ret = true;
458 	uint32_t time;
459 	long ptr;
460 
461 	dprintf("usb-ehci: bulk transfer: data %p, size %d, td %p, td_phys %p\n",
462 		data_phys, size, td, td_phys);
463 
464 	if (pipe->type != USB_EP_TYPE_BULK) {
465 		printf("usb-ehci: Not a bulk pipe.\n");
466 		return false;
467 	}
468 
469 	if (size > QTD_MAX_TRANSFER_LEN) {
470 		printf("usb-ehci: bulk transfer size too big\n");
471 		return false;
472 	}
473 
474 	ehcd = pipe->dev->hcidev->priv;
475 	pid = (pipe->dir == USB_PIPE_OUT) ? PID_OUT : PID_IN;
476 	qtd = (struct ehci_qtd *)td;
477 	qtd_phys = (struct ehci_qtd *)td_phys;
478 	ptr = (long)data_phys;
479 	for (i = 0; i < NUM_BULK_QTDS; i++) {
480 		memset(qtd, 0, sizeof(*qtd));
481 		rem = fill_qtd_buff(qtd, ptr, size);
482 		qtd->token = cpu_to_le32((1 << TOKEN_DT_SHIFT) |
483 				((size - rem) << TOKEN_TBTT_SHIFT) |
484 				(3 << TOKEN_CERR_SHIFT) |
485 				(pid << TOKEN_PID_SHIFT) |
486 				(QH_STS_ACTIVE << TOKEN_STATUS_SHIFT));
487 		if (rem) {
488 			qtd->next_qtd = cpu_to_le32(PTR_U32(&qtd_phys[i+1]));
489 			qtd->alt_next_qtd = QH_PTR_TERM;
490 			ptr += size - rem;
491 			size = rem;
492 			qtd++;
493 		} else {
494 			qtd->next_qtd = qtd->alt_next_qtd = QH_PTR_TERM;
495 			break; /* no more data */
496 		}
497 	}
498 
499 	/* link qtd to qh and attach to ehcd */
500 	mb();
501 	epipe = container_of(pipe, struct ehci_pipe, pipe);
502 	epipe->qh.next_qtd = cpu_to_le32(PTR_U32(qtd_phys));
503 	epipe->qh.qh_ptr = cpu_to_le32(ehcd->qh_async_phys | EHCI_TYP_QH);
504 	epipe->qh.ep_cap1 = cpu_to_le32((pipe->mps << QH_MPS_SHIFT) |
505 				(pipe->speed << QH_EPS_SHIFT) |
506 				(pipe->epno << QH_EP_SHIFT) |
507 				(pipe->dev->addr << QH_DEV_ADDR_SHIFT));
508 	mb();
509 
510 	ehcd->qh_async->qh_ptr = cpu_to_le32(epipe->qh_phys | EHCI_TYP_QH);
511 
512 	/* transfer data */
513 	mb();
514 	qtd = (struct ehci_qtd *)td;
515 	for (i = 0; i < NUM_BULK_QTDS; i++) {
516 		time = SLOF_GetTimer() + USB_TIMEOUT;
517 		while ((time > SLOF_GetTimer()) &&
518 			(le32_to_cpu(qtd->token) & (QH_STS_ACTIVE << TOKEN_STATUS_SHIFT)))
519 			cpu_relax();
520 		mb();
521 		if (qtd->next_qtd == QH_PTR_TERM)
522 			break;
523 
524 		if (le32_to_cpu(qtd->token) & (QH_STS_ACTIVE << TOKEN_STATUS_SHIFT)) {
525 			printf("usb-ehci: bulk transfer timed out_\n");
526 			ret = false;
527 			break;
528 		}
529 		qtd++;
530 	}
531 
532 	ehcd->qh_async->qh_ptr = cpu_to_le32(ehcd->qh_async_phys | EHCI_TYP_QH);
533 	mb();
534 	if (!ehci_handshake(ehcd, USB_TIMEOUT)) {
535 		printf("%s: handshake failed\n", __func__);
536 		ret = false;
537 	}
538 	return ret;
539 }
540 
ehci_get_pipe(struct usb_dev * dev,struct usb_ep_descr * ep,char * buf,size_t len)541 static struct usb_pipe *ehci_get_pipe(struct usb_dev *dev, struct usb_ep_descr *ep,
542 				char *buf, size_t len)
543 {
544 	struct ehci_hcd *ehcd;
545 	struct usb_pipe *new = NULL;
546 
547 	if (!dev)
548 		return NULL;
549 
550 	ehcd = (struct ehci_hcd *)dev->hcidev->priv;
551 	if (!ehcd->freelist) {
552 		dprintf("usb-ehci: %s allocating pool\n", __func__);
553 		if (ehci_alloc_pipe_pool(ehcd))
554 			return NULL;
555 	}
556 
557 	new = ehcd->freelist;
558 	ehcd->freelist = ehcd->freelist->next;
559 	if (!ehcd->freelist)
560 		ehcd->end = NULL;
561 
562 	memset(new, 0, sizeof(*new));
563 	new->dev = dev;
564 	new->next = NULL;
565 	new->type = ep->bmAttributes & USB_EP_TYPE_MASK;
566 	new->speed = dev->speed;
567 	new->mps = ep->wMaxPacketSize;
568 	new->dir = (ep->bEndpointAddress & 0x80) >> 7;
569 	new->epno = ep->bEndpointAddress & 0x0f;
570 
571 	return new;
572 }
573 
ehci_put_pipe(struct usb_pipe * pipe)574 static void ehci_put_pipe(struct usb_pipe *pipe)
575 {
576 	struct ehci_hcd *ehcd;
577 
578 	dprintf("usb-ehci: %s enter - %p\n", __func__, pipe);
579 	if (!pipe || !pipe->dev)
580 		return;
581 	ehcd = pipe->dev->hcidev->priv;
582 	if (ehcd->end)
583 		ehcd->end->next = pipe;
584 	else
585 		ehcd->freelist = pipe;
586 
587 	ehcd->end = pipe;
588 	pipe->next = NULL;
589 	pipe->dev = NULL;
590 	memset(pipe, 0, sizeof(*pipe));
591 	dprintf("usb-ehci: %s exit\n", __func__);
592 }
593 
594 struct usb_hcd_ops ehci_ops = {
595 	.name          = "ehci-hcd",
596 	.init          = ehci_init,
597 	.exit          = ehci_exit,
598 	.detect        = ehci_detect,
599 	.disconnect    = ehci_disconnect,
600 	.get_pipe      = ehci_get_pipe,
601 	.put_pipe      = ehci_put_pipe,
602 	.send_ctrl     = ehci_send_ctrl,
603 	.transfer_bulk = ehci_transfer_bulk,
604 	.usb_type      = USB_EHCI,
605 	.next          = NULL,
606 };
607 
usb_ehci_register(void)608 void usb_ehci_register(void)
609 {
610 	usb_hcd_register(&ehci_ops);
611 }
612