1 /*****************************************************************************
2 * Copyright (c) 2013 IBM Corporation
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
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
14 #include <byteorder.h>
22 #define dprintf(_x ...) do { printf(_x); } while(0)
24 #define dprintf(_x ...)
27 #undef OHCI_DEBUG_PACKET
28 //#define OHCI_DEBUG_PACKET
29 #ifdef OHCI_DEBUG_PACKET
30 #define dpprintf(_x ...) do { printf(_x); } while(0)
32 #define dpprintf(_x ...)
42 static void ohci_dump_regs(struct ohci_regs *regs)
44 dprintf("\n - HcRevision %08X", read_reg32(®s->rev));
45 dprintf(" - HcControl %08X", read_reg32(®s->control));
46 dprintf("\n - HcCommandStatus %08X", read_reg32(®s->cmd_status));
47 dprintf(" - HcInterruptStatus %08X", read_reg32(®s->intr_status));
48 dprintf("\n - HcInterruptEnable %08X", read_reg32(®s->intr_enable));
49 dprintf(" - HcInterruptDisable %08X", read_reg32(®s->intr_disable));
50 dprintf("\n - HcHCCA %08X", read_reg32(®s->hcca));
51 dprintf(" - HcPeriodCurrentED %08X", read_reg32(®s->period_curr_ed));
52 dprintf("\n - HcControlHeadED %08X", read_reg32(®s->cntl_head_ed));
53 dprintf(" - HcControlCurrentED %08X", read_reg32(®s->cntl_curr_ed));
54 dprintf("\n - HcBulkHeadED %08X", read_reg32(®s->bulk_head_ed));
55 dprintf(" - HcBulkCurrentED %08X", read_reg32(®s->bulk_curr_ed));
56 dprintf("\n - HcDoneHead %08X", read_reg32(®s->done_head));
57 dprintf(" - HcFmInterval %08X", read_reg32(®s->fm_interval));
58 dprintf("\n - HcFmRemaining %08X", read_reg32(®s->fm_remaining));
59 dprintf(" - HcFmNumber %08X", read_reg32(®s->fm_num));
60 dprintf("\n - HcPeriodicStart %08X", read_reg32(®s->period_start));
61 dprintf(" - HcLSThreshold %08X", read_reg32(®s->ls_threshold));
62 dprintf("\n - HcRhDescriptorA %08X", read_reg32(®s->rh_desc_a));
63 dprintf(" - HcRhDescriptorB %08X", read_reg32(®s->rh_desc_b));
64 dprintf("\n - HcRhStatus %08X", read_reg32(®s->rh_status));
70 * OHCI Spec 7.4 Root Hub Partition
72 static int ohci_hcd_reset(struct ohci_regs *regs)
77 write_reg32(®s->control, 0);
80 write_reg32(®s->intr_disable, ~0);
81 write_reg32(®s->cmd_status, OHCI_CMD_STATUS_HCR);
84 time = 30; /* wait for not more than 30usec */
85 while ((read_reg32(®s->cmd_status) & OHCI_CMD_STATUS_HCR) != 0) {
88 printf(" ** HCD Reset failed...");
96 static int ohci_hcd_init(struct ohci_hcd *ohcd)
98 struct ohci_regs *regs;
103 struct usb_dev *rhdev = NULL;
104 struct usb_ep_descr ep;
111 rhdev = &ohcd->rhdev;
112 dprintf("%s: HCCA memory %p\n", __func__, ohcd->hcca);
113 dprintf("%s: OHCI Regs %p\n", __func__, regs);
115 rhdev->hcidev = ohcd->hcidev;
116 ep.bmAttributes = USB_EP_TYPE_INTR;
117 ep.wMaxPacketSize = 8;
118 rhdev->intr = usb_get_pipe(rhdev, &ep, NULL, 0);
120 printf("usb-ohci: oops could not allocate intr_pipe\n");
125 * OHCI Spec 4.4: Host Controller Communications Area
127 ed = ohci_pipe_get_ed(rhdev->intr);
128 ed_phys = ohci_pipe_get_ed_phys(rhdev->intr);
129 memset(ohcd->hcca, 0, HCCA_SIZE);
130 memset(ed, 0, sizeof(struct ohci_ed));
131 ed->attr = cpu_to_le32(EDA_SKIP);
132 for (i = 0; i < HCCA_INTR_NUM; i++)
133 ohcd->hcca->intr_table[i] = cpu_to_le32(ed_phys);
135 write_reg32(®s->hcca, ohcd->hcca_phys);
136 write_reg32(®s->cntl_head_ed, 0);
137 write_reg32(®s->bulk_head_ed, 0);
139 /* OHCI Spec 7.1.2 HcControl Register */
140 oldrwc = read_reg32(®s->control) & OHCI_CTRL_RWC;
141 write_reg32(®s->control, (OHCI_CTRL_CBSR | OHCI_CTRL_CLE |
142 OHCI_CTRL_BLE | OHCI_CTRL_PLE |
143 OHCI_USB_OPER | oldrwc));
146 * For JS20/21 need to rewrite it after setting it to
149 write_reg32(®s->fm_interval, FRAME_INTERVAL);
150 write_reg32(®s->period_start, PERIODIC_START);
151 reg = read_reg32(®s->rh_desc_a);
152 reg &= ~( RHDA_PSM_INDIVIDUAL | RHDA_OCPM_PERPORT );
153 reg |= RHDA_NPS_ENABLE;
154 write_reg32(®s->rh_desc_a, reg);
155 write_reg32(®s->rh_desc_b, 0);
158 ohci_dump_regs(regs);
163 * OHCI Spec 7.4 Root Hub Partition
165 static void ohci_hub_check_ports(struct ohci_hcd *ohcd)
167 struct ohci_regs *regs;
169 unsigned int ports, i, port_status, port_clear = 0;
172 ports = read_reg32(®s->rh_desc_a) & RHDA_NDP;
173 write_reg32(®s->rh_status, RH_STATUS_LPSC);
175 dprintf("usb-ohci: ports connected %d\n", ports);
176 for (i = 0; i < ports; i++) {
177 dprintf("usb-ohci: ports scanning %d\n", i);
178 port_status = read_reg32(®s->rh_ps[i]);
179 if (port_status & RH_PS_CSC) {
180 if (port_status & RH_PS_CCS) {
181 write_reg32(®s->rh_ps[i], RH_PS_PRS);
183 port_clear |= RH_PS_CSC;
184 dprintf("Start enumerating device\n");
187 printf("Start removing device\n");
189 port_status = read_reg32(®s->rh_ps[i]);
190 if (port_status & RH_PS_PRSC) {
191 port_clear |= RH_PS_PRSC;
192 dev = usb_devpool_get();
193 dprintf("usb-ohci: Device reset, setting up %p\n", dev);
194 dev->hcidev = ohcd->hcidev;
195 if (!setup_new_device(dev, i))
196 printf("usb-ohci: unable to setup device on port %d\n", i);
198 if (port_status & RH_PS_PESC) {
199 port_clear |= RH_PS_PESC;
200 if (port_status & RH_PS_PES)
201 dprintf("enabled\n");
203 dprintf("disabled\n");
205 if (port_status & RH_PS_PSSC) {
206 port_clear |= RH_PS_PESC;
207 dprintf("suspended\n");
209 port_clear &= 0xFFFF0000;
211 write_reg32(®s->rh_ps[i], port_clear);
215 static inline struct ohci_ed *ohci_pipe_get_ed(struct usb_pipe *pipe)
217 struct ohci_pipe *opipe;
218 opipe = container_of(pipe, struct ohci_pipe, pipe);
219 dpprintf("%s: ed is %p\n", __func__, &opipe->ed);
223 static inline long ohci_pipe_get_ed_phys(struct usb_pipe *pipe)
225 struct ohci_pipe *opipe;
226 opipe = container_of(pipe, struct ohci_pipe, pipe);
227 dpprintf("%s: ed_phys is %x\n", __func__, opipe->ed_phys);
228 return opipe->ed_phys;
231 static inline struct ohci_pipe *ohci_pipe_get_opipe(struct usb_pipe *pipe)
233 struct ohci_pipe *opipe;
234 opipe = container_of(pipe, struct ohci_pipe, pipe);
235 dpprintf("%s: opipe is %p\n", __func__, opipe);
239 static int ohci_alloc_pipe_pool(struct ohci_hcd *ohcd)
241 struct ohci_pipe *opipe, *curr, *prev;
243 unsigned int i, count;
244 #ifdef OHCI_DEBUG_PACKET
245 struct usb_pipe *pipe;
248 dprintf("usb-ohci: %s enter\n", __func__);
249 count = OHCI_PIPE_POOL_SIZE/sizeof(*opipe);
250 ohcd->pool = opipe = SLOF_dma_alloc(OHCI_PIPE_POOL_SIZE);
254 ohcd->pool_phys = opipe_phys = SLOF_dma_map_in(opipe, OHCI_PIPE_POOL_SIZE, true);
255 dprintf("usb-ohci: %s opipe %x, opipe_phys %x size %d count %d\n",
256 __func__, opipe, opipe_phys, sizeof(*opipe), count);
257 /* Although an array, link them*/
258 for (i = 0, curr = opipe, prev = NULL; i < count; i++, curr++) {
260 prev->pipe.next = &curr->pipe;
261 curr->pipe.next = NULL;
264 if (((uint64_t)&curr->ed) % 16)
265 printf("usb-ohci: Warning ED not aligned to 16byte boundary");
266 curr->ed_phys = opipe_phys + (curr - opipe) * sizeof(*curr) +
267 offset_of(struct ohci_pipe, ed);
271 ohcd->freelist = &opipe->pipe;
273 ohcd->end->next = &opipe->pipe;
274 ohcd->end = &prev->pipe;
276 #ifdef OHCI_DEBUG_PACKET
277 for (i = 0, pipe = ohcd->freelist; pipe; pipe = pipe->next)
278 dprintf("usb-ohci: %d: pipe cur %p ed %p ed_phys %x\n",
279 i++, pipe, ohci_pipe_get_ed(pipe),
280 ohci_pipe_get_ed_phys(pipe));
283 dprintf("usb-ohci: %s exit\n", __func__);
287 static void ohci_init(struct usb_hcd_dev *hcidev)
289 struct ohci_hcd *ohcd;
291 printf(" OHCI: initializing\n");
292 dprintf("%s: device base address %p\n", __func__, hcidev->base);
294 ohcd = SLOF_alloc_mem(sizeof(struct ohci_hcd));
296 printf("usb-ohci: Unable to allocate memory\n");
300 hcidev->nextaddr = 1;
302 memset(ohcd, 0, sizeof(*ohcd));
303 ohcd->hcidev = hcidev;
304 ohcd->freelist = NULL;
306 ohcd->regs = (struct ohci_regs *)(hcidev->base);
307 ohcd->hcca = SLOF_dma_alloc(sizeof(struct ohci_hcca));
308 if (!ohcd->hcca || PTR_U32(ohcd->hcca) & HCCA_ALIGN) {
309 printf("usb-ohci: Unable to allocate/unaligned HCCA memory %p\n",
313 ohcd->hcca_phys = SLOF_dma_map_in(ohcd->hcca,
314 sizeof(struct ohci_hcca), true);
315 dprintf("usb-ohci: HCCA memory %p HCCA-dev memory %08lx\n",
316 ohcd->hcca, ohcd->hcca_phys);
318 ohci_hcd_reset(ohcd->regs);
320 ohci_hub_check_ports(ohcd);
324 SLOF_dma_free(ohcd->hcca, sizeof(struct ohci_hcca));
325 SLOF_free_mem(ohcd, sizeof(struct ohci_hcd));
330 static void ohci_exit(struct usb_hcd_dev *hcidev)
332 struct ohci_hcd *ohcd = NULL;
334 dprintf("%s: enter \n", __func__);
335 if (!hcidev && !hcidev->priv)
339 write_reg32(&ohcd->regs->control, (OHCI_CTRL_CBSR | OHCI_USB_SUSPEND));
341 write_reg32(&ohcd->regs->hcca, cpu_to_le32(0));
344 SLOF_dma_map_out(ohcd->pool_phys, ohcd->pool, OHCI_PIPE_POOL_SIZE);
345 SLOF_dma_free(ohcd->pool, OHCI_PIPE_POOL_SIZE);
348 SLOF_dma_map_out(ohcd->hcca_phys, ohcd->hcca, sizeof(struct ohci_hcca));
349 SLOF_dma_free(ohcd->hcca, sizeof(struct ohci_hcca));
351 SLOF_free_mem(ohcd, sizeof(struct ohci_hcd));
355 static void ohci_detect(void)
360 static void ohci_disconnect(void)
365 #define OHCI_CTRL_TDS 3
367 static void ohci_fill_td(struct ohci_td *td, long next,
368 long req, size_t size, unsigned int attr)
371 td->cbp = cpu_to_le32(req);
372 td->be = cpu_to_le32(req + size - 1);
377 td->attr = cpu_to_le32(attr);
378 td->next_td = cpu_to_le32(next);
380 dpprintf("%s: cbp %08X attr %08X next_td %08X be %08X\n", __func__,
381 le32_to_cpu(td->cbp), le32_to_cpu(td->attr),
382 le32_to_cpu(td->next_td), le32_to_cpu(td->be));
385 static void ohci_fill_ed(struct ohci_ed *ed, long headp, long tailp,
386 unsigned int attr, long next_ed)
388 ed->attr = cpu_to_le32(attr);
389 ed->headp = cpu_to_le32(headp) | (ed->headp & ~EDA_HEADP_MASK_LE);
390 ed->tailp = cpu_to_le32(tailp);
391 ed->next_ed = cpu_to_le32(next_ed);
392 dpprintf("%s: headp %08X tailp %08X next_td %08X attr %08X\n", __func__,
393 le32_to_cpu(ed->headp), le32_to_cpu(ed->tailp),
394 le32_to_cpu(ed->next_ed), le32_to_cpu(ed->attr));
398 static long ohci_get_td_phys(struct ohci_td *curr, struct ohci_td *start, long td_phys)
400 dpprintf("position %d\n", curr - start);
401 return td_phys + (curr - start) * sizeof(*start);
404 static long ohci_get_td_virt(struct ohci_td *curr, struct ohci_td *start, long td_virt, long total_count)
406 dpprintf("position %d\n", curr - start);
407 if ( (curr - start) >= total_count) {
408 /* busted position, should ignore this */
411 return td_virt + (curr - start) * sizeof(*start);
414 /* OHCI Spec: 4.4.2.3 HccaDoneHead*/
415 static int ohci_process_done_head(struct ohci_hcd *ohcd,
416 struct ohci_td *td_start,
417 long __td_start_phys, long total_count)
419 struct ohci_hcca *hcca;
420 struct ohci_td *td_phys = NULL, *td_start_phys;
421 struct ohci_td *td, *prev_td = NULL;
422 uint32_t reg = 0, time = 0;
427 td_start_phys = (struct ohci_td *) __td_start_phys;
429 time = SLOF_GetTimer() + USB_TIMEOUT;
430 dpprintf("Claiming %ld\n", count);
434 /* Check if there is an interrupt */
435 reg = read_reg32(&ohcd->regs->intr_status);
436 while(!(reg & OHCI_INTR_STATUS_WD))
438 if (time < SLOF_GetTimer()) {
439 printf("Timed out waiting for interrupt %x\n", reg);
443 reg = read_reg32(&ohcd->regs->intr_status);
446 /* Interrupt is there, read from done_head pointer */
447 td_phys = (struct ohci_td *)(uint64_t) le32_to_cpu(hcca->done_head);
449 dprintf("Again td_phys null %ld\n");
455 while (td_phys && (count > 0)) {
456 td = (struct ohci_td *)(uint64_t) ohci_get_td_virt(td_phys,
462 printf("USB: Error TD null %p\n", td_phys);
466 dprintf("Claimed %p(%p) td_start %p count %ld\n",
467 td, td_phys, td_start_phys, count);
468 dpprintf("%s: cbp %08X attr %08X next_td %08X be %08X\n",
470 le32_to_cpu(td->cbp), le32_to_cpu(td->attr),
471 le32_to_cpu(td->next_td), le32_to_cpu(td->be));
473 reg = (le32_to_cpu(td->attr) & TDA_CC) >> 28;
475 dprintf("%s: cbp %08X attr %08X next_td %08X be %08X\n",
477 le32_to_cpu(td->cbp), le32_to_cpu(td->attr),
478 le32_to_cpu(td->next_td), le32_to_cpu(td->be));
479 printf("USB: Error %s %p\n", tda_cc_error[reg], td);
480 if (reg > 3) /* Return negative error code */
484 td_phys = (struct ohci_td *)(uint64_t) le32_to_cpu(td->next_td);
485 prev_td->attr |= cpu_to_le32(TDA_DONE);
486 prev_td->next_td = 0;
489 /* clear the WD interrupt status */
490 write_reg32(&ohcd->regs->intr_status, OHCI_INTR_STATUS_WD);
492 read_reg32(&ohcd->regs->intr_status);
495 dpprintf("Pending count %d\n", count);
498 dprintf("TD claims done\n");
504 * 4.2 Endpoint Descriptor
505 * 4.3.1 General Transfer Descriptor
506 * 5.2.8 Transfer Descriptor Queues
508 static int ohci_send_ctrl(struct usb_pipe *pipe, struct usb_dev_req *req, void *data)
511 struct ohci_td *tds, *td, *td_phys;
512 struct ohci_regs *regs;
513 struct ohci_hcd *ohcd;
515 uint32_t dir, attr = 0;
518 long req_phys = 0, data_phys = 0, td_next = 0, td_count = 0;
521 datalen = le16_to_cpu(req->wLength);
522 dir = (req->bmRequestType & REQT_DIR_IN) ? 1 : 0;
524 dprintf("usb-ohci: %s len %d DIR_IN %d\n", __func__, datalen, dir);
526 tds = td = (struct ohci_td *) SLOF_dma_alloc(sizeof(*td) * OHCI_CTRL_TDS);
527 td_phys = (struct ohci_td *) SLOF_dma_map_in(td, sizeof(*td) * OHCI_CTRL_TDS, true);
528 memset(td, 0, sizeof(*td) * OHCI_CTRL_TDS);
530 req_phys = SLOF_dma_map_in(req, sizeof(struct usb_dev_req), true);
531 attr = TDA_DP_SETUP | TDA_CC | TDA_TOGGLE_DATA0;
532 td_next = ohci_get_td_phys(td + 1, tds, PTR_U32(td_phys));
533 ohci_fill_td(td, td_next, req_phys, sizeof(*req), attr);
537 data_phys = SLOF_dma_map_in(data, datalen, true);
539 attr = (dir ? TDA_DP_IN : TDA_DP_OUT) | TDA_TOGGLE_DATA1 | TDA_CC;
540 td_next = ohci_get_td_phys(td + 1, tds, PTR_U32(td_phys));
541 ohci_fill_td(td, td_next, data_phys, datalen, attr);
546 attr = (dir ? TDA_DP_OUT : TDA_DP_IN) | TDA_CC | TDA_TOGGLE_DATA1;
547 td_next = ohci_get_td_phys(td + 1, tds, PTR_U32(td_phys));
548 ohci_fill_td(td, 0, 0, 0, attr);
551 ed = ohci_pipe_get_ed(pipe);
553 attr = EDA_FADDR(pipe->dev->addr) | EDA_MPS(pipe->mps) | EDA_SKIP;
554 ohci_fill_ed(ed, PTR_U32(td_phys), td_next, attr, 0);
555 ed->tailp = 0; /* HACK */
556 dprintf("usb-ohci: %s - td_start %x td_end %x req %x\n", __func__,
557 td_phys, td_next, req_phys);
559 ed->attr &= cpu_to_le32(~EDA_SKIP);
561 ohcd = pipe->dev->hcidev->priv;
563 write_reg32(®s->cntl_head_ed, ohci_pipe_get_ed_phys(pipe));
565 write_reg32(®s->cmd_status, OHCI_CMD_STATUS_CLF);
567 time = SLOF_GetTimer() + USB_TIMEOUT;
568 while ((time > SLOF_GetTimer()) &&
569 ((ed->headp & EDA_HEADP_MASK_LE) != ed->tailp))
572 if ((ed->headp & EDA_HEADP_MASK_LE) == ed->tailp) {
573 dprintf("%s: packet sent\n", __func__);
574 #ifdef OHCI_DEBUG_PACKET
575 dpprintf("Request: ");
576 dbuf = (unsigned char *)req;
577 for(i = 0; i < 8; i++)
578 printf("%02X ", dbuf[i]);
581 dbuf = (unsigned char *)data;
583 for(i = 0; i < datalen; i++)
584 printf("%02X ", dbuf[i]);
590 printf("%s: timed out - failed\n", __func__);
591 dpprintf("%s: headp %08X tailp %08X next_td %08X attr %08X\n",
593 le32_to_cpu(ed->headp), le32_to_cpu(ed->tailp),
594 le32_to_cpu(ed->next_ed), le32_to_cpu(ed->attr));
596 dbuf = (unsigned char *)req;
597 for(i = 0; i < 8; i++)
598 printf("%02X ", dbuf[i]);
601 ret = ohci_process_done_head(ohcd, tds, (long)td_phys, td_count);
603 ed->attr |= cpu_to_le32(EDA_SKIP);
605 write_reg32(®s->cntl_head_ed, 0);
606 write_reg32(®s->cntl_curr_ed, 0);
608 SLOF_dma_map_out(req_phys, req, sizeof(struct usb_dev_req));
610 SLOF_dma_map_out(data_phys, data, datalen);
611 SLOF_dma_map_out(PTR_U32(td_phys), tds, sizeof(*td) * OHCI_CTRL_TDS);
612 SLOF_dma_free(tds, sizeof(*td) * OHCI_CTRL_TDS);
613 return (ret > 0) ? true : false;
616 static int ohci_transfer_bulk(struct usb_pipe *pipe, void *td_ptr,
617 void *td_phys_ptr, void *data_phys, int datalen)
620 struct ohci_td *td, *tds;
621 struct ohci_regs *regs;
622 struct ohci_hcd *ohcd;
623 long td_phys = 0, td_next, ed_phys, ptr, td_count = 0;
624 uint32_t dir, attr = 0, count;
625 size_t len, packet_len;
629 if (pipe->type != USB_EP_TYPE_BULK) {
630 printf("usb-ohci: Not a bulk pipe.\n");
635 dir = (pipe->dir == USB_PIPE_OUT) ? 0 : 1;
636 count = datalen / OHCI_MAX_BULK_SIZE;
637 if (count > OHCI_MAX_TDS) {
638 printf("usb-ohci: buffer size not supported - %d\n", datalen);
643 td = tds = (struct ohci_td *) td_ptr;
644 td_phys = (long)td_phys_ptr;
645 dprintf("usb-ohci: %s pipe %p data_phys %p len %d DIR_IN %d td %p td_phys %p\n",
646 __func__, pipe, data_phys, datalen, dir, td, td_phys);
649 printf("%s: tds NULL recieved\n", __func__);
653 memset(td, 0, sizeof(*td) * OHCI_MAX_TDS);
656 ptr = (long)data_phys;
658 attr = (dir ? TDA_DP_IN : TDA_DP_OUT) | TDA_CC | TDA_ROUNDING;
660 packet_len = (OHCI_MAX_BULK_SIZE < len)? OHCI_MAX_BULK_SIZE : len;
661 td_next = ohci_get_td_phys((td + 1), tds, td_phys);
662 ohci_fill_td(td, td_next, ptr, packet_len, attr);
663 ptr = ptr + packet_len;
664 len = len - packet_len;
668 ed = ohci_pipe_get_ed(pipe);
670 dir = pipe->dir ? EDA_DIR_IN : EDA_DIR_OUT;
671 attr = dir | EDA_FADDR(pipe->dev->addr) | EDA_MPS(pipe->mps)
672 | EDA_SKIP | pipe->dev->speed | EDA_EP(pipe->epno);
673 td_next = ohci_get_td_phys(td, tds, td_phys);
674 ohci_fill_ed(ed, td_phys, td_next, attr, 0);
675 dprintf("usb-ohci: %s - tds %p td %p\n", __func__, td_phys, td_next);
677 ed->attr &= cpu_to_le32(~EDA_SKIP);
679 ohcd = pipe->dev->hcidev->priv;
681 ed_phys = ohci_pipe_get_ed_phys(pipe);
682 write_reg32(®s->bulk_head_ed, ed_phys);
684 write_reg32(®s->cmd_status, 0x4);
686 time = SLOF_GetTimer() + USB_TIMEOUT;
687 while ((time > SLOF_GetTimer()) &&
688 ((ed->headp & EDA_HEADP_MASK_LE) != ed->tailp))
691 if ((ed->headp & EDA_HEADP_MASK_LE) == ed->tailp)
692 dprintf("%s: packet sent\n", __func__);
694 dpprintf("%s: headp %08X tailp %08X next_td %08X attr %08X\n", __func__,
695 le32_to_cpu(ed->headp), le32_to_cpu(ed->tailp),
696 le32_to_cpu(ed->next_ed), le32_to_cpu(ed->attr));
699 ret = ohci_process_done_head(ohcd, tds, td_phys, td_count);
701 ed->attr |= cpu_to_le32(EDA_SKIP);
703 write_reg32(®s->bulk_head_ed, 0);
704 write_reg32(®s->bulk_curr_ed, 0);
706 if (le32_to_cpu(ed->headp) & EDA_HEADP_HALTED) {
707 printf("ED Halted\n");
708 printf("%s: headp %08X tailp %08X next_td %08X attr %08X\n", __func__,
709 le32_to_cpu(ed->headp), le32_to_cpu(ed->tailp),
710 le32_to_cpu(ed->next_ed), le32_to_cpu(ed->attr));
711 ed->headp &= ~cpu_to_le32(EDA_HEADP_HALTED);
713 if (ret == USB_STALL) /* Call reset recovery */
714 usb_msc_resetrecovery(pipe->dev);
718 return (ret > 0) ? true : false;
721 /* Populate the hcca intr region with periodic intr */
722 static int ohci_get_pipe_intr(struct usb_pipe *pipe, struct ohci_hcd *ohcd,
723 char *buf, size_t buflen)
725 struct ohci_hcca *hcca;
726 struct ohci_pipe *opipe;
729 struct ohci_td *tds, *td;
730 int32_t count = 0, i;
733 long ed_phys, td_phys, td_next, buf_phys;
740 if (dev->class != DEV_HID_KEYB && dev->class != DEV_HUB)
743 opipe = ohci_pipe_get_opipe(pipe);
745 ed_phys = opipe->ed_phys;
747 ed->attr = cpu_to_le32(EDA_DIR_IN |
748 EDA_FADDR(dev->addr) |
753 dprintf("%s: pipe %p ed %p dev %p opipe %p\n", __func__,
754 pipe, ed, dev, opipe);
755 count = (buflen/mps) + 1;
756 tds = td = SLOF_dma_alloc(sizeof(*td) * count);
758 printf("%s: alloc failed\n", __func__);
761 td_phys = SLOF_dma_map_in(td, sizeof(*td) * count, false);
763 memset(tds, 0, sizeof(*tds) * count);
764 memset(buf, 0, buflen);
765 buf_phys = SLOF_dma_map_in(buf, buflen, false);
767 opipe->td_phys = td_phys;
768 opipe->count = count;
770 opipe->buflen = buflen;
771 opipe->buf_phys = buf_phys;
773 ptr = (uint8_t *)buf_phys;
774 for (i = 0; i < count - 1; i++, ptr += mps) {
776 td_next = ohci_get_td_phys(td + 1, &tds[0], td_phys);
777 td->cbp = cpu_to_le32(PTR_U32(ptr));
778 td->attr = cpu_to_le32(TDA_DP_IN | TDA_ROUNDING | TDA_CC);
779 td->next_td = cpu_to_le32(td_next);
780 td->be = cpu_to_le32(PTR_U32(ptr) + mps - 1);
781 dprintf("td %x td++ %x ptr %x be %x\n",
782 td, le32_to_cpu(td->next_td),
783 ptr, (PTR_U32(ptr) + mps - 1));
786 td_next = ohci_get_td_phys(td, &tds[0], td_phys);
787 ed->headp = cpu_to_le32(td_phys);
788 ed->tailp = cpu_to_le32(td_next);
790 dprintf("%s: head %08X tail %08X, count %d, mps %d\n", __func__,
791 le32_to_cpu(ed->headp),
792 le32_to_cpu(ed->tailp),
797 switch (dev->class) {
799 dprintf("%s: Keyboard class %d\n", __func__, dev->class);
800 hcca->intr_table[0] = cpu_to_le32(ed_phys);
801 hcca->intr_table[8] = cpu_to_le32(ed_phys);
802 hcca->intr_table[16] = cpu_to_le32(ed_phys);
803 hcca->intr_table[24] = cpu_to_le32(ed_phys);
804 ed->attr &= cpu_to_le32(~EDA_SKIP);
808 dprintf("%s: HUB class %x\n", __func__, dev->class);
809 hcca->intr_table[1] = cpu_to_le32(ed_phys);
810 ed->attr &= cpu_to_le32(~EDA_SKIP);
814 dprintf("%s: unhandled class %d\n", __func__, dev->class);
819 static int ohci_put_pipe_intr(struct usb_pipe *pipe, struct ohci_hcd *ohcd)
821 struct ohci_hcca *hcca;
822 struct ohci_pipe *opipe;
834 if (dev->class != DEV_HID_KEYB && dev->class != DEV_HUB)
837 opipe = ohci_pipe_get_opipe(pipe);
839 ed_phys = opipe->ed_phys;
840 dprintf("%s: td %p td_phys %08lx buf %p buf_phys %08lx\n", __func__,
841 opipe->td, opipe->td_phys, opipe->buf, opipe->buf_phys);
843 ed->attr |= cpu_to_le32(EDA_SKIP);
848 SLOF_dma_map_out(opipe->buf_phys, opipe->buf, opipe->buflen);
849 SLOF_dma_map_out(opipe->td_phys, opipe->td, sizeof(*td) * opipe->count);
850 SLOF_dma_free(opipe->td, sizeof(*td) * opipe->count);
852 switch (dev->class) {
854 dprintf("%s: Keyboard class %d\n", __func__, dev->class);
855 hcca->intr_table[0] = cpu_to_le32(ed_phys);
856 hcca->intr_table[8] = cpu_to_le32(ed_phys);
857 hcca->intr_table[16] = cpu_to_le32(ed_phys);
858 hcca->intr_table[24] = cpu_to_le32(ed_phys);
862 dprintf("%s: HUB class %d\n", __func__, dev->class);
863 hcca->intr_table[1] = cpu_to_le32(ed_phys);
867 dprintf("%s: unhandled class %d\n", __func__, dev->class);
872 static int ohci_init_bulk_ed(struct usb_dev *dev, struct usb_pipe *pipe)
874 struct ohci_pipe *opipe;
881 opipe = ohci_pipe_get_opipe(pipe);
883 dir = pipe->dir ? EDA_DIR_IN : EDA_DIR_OUT;
885 ed->attr = cpu_to_le32(dir |
886 EDA_FADDR(dev->addr) |
892 dprintf("%s: pipe %p attr %x\n", __func__, pipe,
893 le32_to_cpu(ed->attr));
897 static struct usb_pipe *ohci_get_pipe(struct usb_dev *dev, struct usb_ep_descr *ep,
898 char *buf, size_t buflen)
900 struct ohci_hcd *ohcd;
901 struct usb_pipe *new = NULL;
903 dprintf("usb-ohci: %s enter %p\n", __func__, dev);
907 ohcd = (struct ohci_hcd *)dev->hcidev->priv;
908 if (!ohcd->freelist) {
909 dprintf("usb-ohci: %s allocating pool\n", __func__);
910 if (!ohci_alloc_pipe_pool(ohcd))
914 new = ohcd->freelist;
915 ohcd->freelist = ohcd->freelist->next;
919 memset(new, 0, sizeof(*new));
922 new->type = ep->bmAttributes & USB_EP_TYPE_MASK;
923 new->speed = dev->speed;
924 new->mps = le16_to_cpu(ep->wMaxPacketSize);
925 new->epno = ep->bEndpointAddress & 0xF;
926 new->dir = ep->bEndpointAddress & 0x80;
927 if (new->type == USB_EP_TYPE_INTR)
928 if (!ohci_get_pipe_intr(new, ohcd, buf, buflen))
929 dprintf("usb-ohci: %s alloc_intr failed %p\n",
931 if (new->type == USB_EP_TYPE_BULK)
932 ohci_init_bulk_ed(dev, new);
934 dprintf("usb-ohci: %s exit %p\n", __func__, new);
938 static void ohci_put_pipe(struct usb_pipe *pipe)
940 struct ohci_hcd *ohcd;
942 dprintf("usb-ohci: %s enter - %p\n", __func__, pipe);
943 if (!pipe || !pipe->dev)
945 ohcd = pipe->dev->hcidev->priv;
947 ohcd->end->next = pipe;
949 ohcd->freelist = pipe;
951 if (pipe->type == USB_EP_TYPE_INTR)
952 if (!ohci_put_pipe_intr(pipe, ohcd))
953 dprintf("usb-ohci: %s alloc_intr failed %p\n",
959 memset(pipe, 0, sizeof(*pipe));
960 dprintf("usb-ohci: %s exit\n", __func__);
963 static uint16_t ohci_get_last_frame(struct usb_dev *dev)
965 struct ohci_hcd *ohcd;
966 struct ohci_regs *regs;
968 ohcd = dev->hcidev->priv;
970 return read_reg32(®s->fm_num);
973 static int ohci_poll_intr(struct usb_pipe *pipe, uint8_t *data)
975 struct ohci_pipe *opipe;
977 struct ohci_td *head, *tail, *curr, *next;
978 struct ohci_td *head_phys, *tail_phys, *curr_phys;
981 static uint16_t last_frame;
985 if (!pipe || last_frame == ohci_get_last_frame(pipe->dev))
988 dprintf("%s: enter\n", __func__);
990 last_frame = ohci_get_last_frame(pipe->dev);
991 opipe = ohci_pipe_get_opipe(pipe);
994 head_phys = (struct ohci_td *)(long)(le32_to_cpu(ed->headp) & EDA_HEADP_MASK);
995 tail_phys = (struct ohci_td *)(long)le32_to_cpu(ed->tailp);
996 curr_phys = (struct ohci_td *) opipe->td_phys;
997 pos = (tail_phys - curr_phys + 1) % (opipe->count - 1);
998 dprintf("pos %d %ld -- %d\n", pos, (tail_phys - curr_phys + 1),
1000 curr = opipe->td + pos;
1001 head = opipe->td + (head_phys - (struct ohci_td *) opipe->td_phys);
1002 tail = opipe->td + (tail_phys - (struct ohci_td *) opipe->td_phys);
1004 /* dprintf("%08X %08X %08X %08X\n",
1005 opipe->td_phys, head_phys, tail_phys, curr_phys);
1006 dprintf("%08X %08X %08X %08X\n", opipe->td, head, tail, curr); */
1009 ptr = (uint8_t *) ((long)opipe->buf + pipe->mps * pos);
1010 ptr_phys = opipe->buf_phys + pipe->mps * pos;
1011 if (le32_to_cpu(*(uint32_t *)ptr) != 0) {
1012 for (i = 0; i < 8; i++)
1013 data[i] = *(ptr + i);
1017 if (next == (opipe->td + opipe->count - 1))
1020 curr->attr = cpu_to_le32(TDA_DP_IN | TDA_ROUNDING | TDA_CC);
1021 curr->next_td = cpu_to_le32(0);
1022 curr->cbp = cpu_to_le32(PTR_U32(ptr_phys));
1023 curr->be = cpu_to_le32(PTR_U32(ptr_phys + pipe->mps - 1));
1024 td_next = ohci_get_td_phys(curr, opipe->td, opipe->td_phys);
1025 dprintf("Connecting %p to %p(phys %08lx) ptr %p, "
1026 "ptr_phys %08lx\n", tail, curr, td_next, ptr, ptr_phys);
1027 tail->next_td = cpu_to_le32(td_next);
1029 ed->tailp = cpu_to_le32(td_next);
1033 dprintf("%s: exit\n", __func__);
1037 struct usb_hcd_ops ohci_ops = {
1041 .detect = ohci_detect,
1042 .disconnect = ohci_disconnect,
1043 .get_pipe = ohci_get_pipe,
1044 .put_pipe = ohci_put_pipe,
1045 .send_ctrl = ohci_send_ctrl,
1046 .transfer_bulk = ohci_transfer_bulk,
1047 .poll_intr = ohci_poll_intr,
1048 .usb_type = USB_OHCI,
1052 void usb_ohci_register(void)
1054 usb_hcd_register(&ohci_ops);