Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / SLOF / lib / libusb / usb-ohci.c
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 <byteorder.h>
15 #include "usb.h"
16 #include "usb-core.h"
17 #include "usb-ohci.h"
18
19 #undef OHCI_DEBUG
20 //#define OHCI_DEBUG
21 #ifdef OHCI_DEBUG
22 #define dprintf(_x ...) do { printf(_x); } while(0)
23 #else
24 #define dprintf(_x ...)
25 #endif
26
27 #undef OHCI_DEBUG_PACKET
28 //#define OHCI_DEBUG_PACKET
29 #ifdef OHCI_DEBUG_PACKET
30 #define dpprintf(_x ...) do { printf(_x); } while(0)
31 #else
32 #define dpprintf(_x ...)
33 #endif
34
35
36 /*
37  * Dump OHCI register
38  *
39  * @param   - ohci_hcd
40  * @return  -
41  */
42 static void ohci_dump_regs(struct ohci_regs *regs)
43 {
44         dprintf("\n - HcRevision         %08X", read_reg32(&regs->rev));
45         dprintf("   - HcControl          %08X", read_reg32(&regs->control));
46         dprintf("\n - HcCommandStatus    %08X", read_reg32(&regs->cmd_status));
47         dprintf("   - HcInterruptStatus  %08X", read_reg32(&regs->intr_status));
48         dprintf("\n - HcInterruptEnable  %08X", read_reg32(&regs->intr_enable));
49         dprintf("   - HcInterruptDisable %08X", read_reg32(&regs->intr_disable));
50         dprintf("\n - HcHCCA             %08X", read_reg32(&regs->hcca));
51         dprintf("   - HcPeriodCurrentED  %08X", read_reg32(&regs->period_curr_ed));
52         dprintf("\n - HcControlHeadED    %08X", read_reg32(&regs->cntl_head_ed));
53         dprintf("   - HcControlCurrentED %08X", read_reg32(&regs->cntl_curr_ed));
54         dprintf("\n - HcBulkHeadED       %08X", read_reg32(&regs->bulk_head_ed));
55         dprintf("   - HcBulkCurrentED    %08X", read_reg32(&regs->bulk_curr_ed));
56         dprintf("\n - HcDoneHead         %08X", read_reg32(&regs->done_head));
57         dprintf("   - HcFmInterval       %08X", read_reg32(&regs->fm_interval));
58         dprintf("\n - HcFmRemaining      %08X", read_reg32(&regs->fm_remaining));
59         dprintf("   - HcFmNumber         %08X", read_reg32(&regs->fm_num));
60         dprintf("\n - HcPeriodicStart    %08X", read_reg32(&regs->period_start));
61         dprintf("   - HcLSThreshold      %08X", read_reg32(&regs->ls_threshold));
62         dprintf("\n - HcRhDescriptorA    %08X", read_reg32(&regs->rh_desc_a));
63         dprintf("   - HcRhDescriptorB    %08X", read_reg32(&regs->rh_desc_b));
64         dprintf("\n - HcRhStatus         %08X", read_reg32(&regs->rh_status));
65         dprintf("\n");
66 }
67
68 /*
69  * OHCI Spec 5.1.1
70  * OHCI Spec 7.4 Root Hub Partition
71  */
72 static int ohci_hcd_reset(struct ohci_regs *regs)
73 {
74         uint32_t time;
75
76         /* USBRESET - 1sec */
77         write_reg32(&regs->control, 0);
78         SLOF_msleep(100);
79
80         write_reg32(&regs->intr_disable, ~0);
81         write_reg32(&regs->cmd_status, OHCI_CMD_STATUS_HCR);
82         mb();
83
84         time = 30; /* wait for not more than 30usec */
85         while ((read_reg32(&regs->cmd_status) & OHCI_CMD_STATUS_HCR) != 0) {
86                 time--;
87                 if (!time) {
88                         printf(" ** HCD Reset failed...");
89                         return -1;
90                 }
91                 SLOF_usleep(1);
92         }
93         return 0;
94 }
95
96 static int ohci_hcd_init(struct ohci_hcd *ohcd)
97 {
98         struct ohci_regs *regs;
99         struct ohci_ed *ed;
100         long ed_phys = 0;
101         unsigned int i;
102         uint32_t oldrwc;
103         struct usb_dev *rhdev = NULL;
104         struct usb_ep_descr ep;
105         uint32_t reg;
106
107         if (!ohcd)
108                 return -1;
109
110         regs = ohcd->regs;
111         rhdev = &ohcd->rhdev;
112         dprintf("%s: HCCA memory %p\n", __func__, ohcd->hcca);
113         dprintf("%s: OHCI Regs   %p\n", __func__, regs);
114
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);
119         if (!rhdev->intr) {
120                 printf("usb-ohci: oops could not allocate intr_pipe\n");
121                 return -1;
122         }
123
124         /*
125          * OHCI Spec 4.4: Host Controller Communications Area
126          */
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);
134
135         write_reg32(&regs->hcca, ohcd->hcca_phys);
136         write_reg32(&regs->cntl_head_ed, 0);
137         write_reg32(&regs->bulk_head_ed, 0);
138
139         /* OHCI Spec 7.1.2 HcControl Register */
140         oldrwc = read_reg32(&regs->control) & OHCI_CTRL_RWC;
141         write_reg32(&regs->control, (OHCI_CTRL_CBSR | OHCI_CTRL_CLE |
142                                         OHCI_CTRL_BLE | OHCI_CTRL_PLE |
143                                         OHCI_USB_OPER | oldrwc));
144         SLOF_msleep(100);
145         /*
146          * For JS20/21 need to rewrite it after setting it to
147          * operational state
148          */
149         write_reg32(&regs->fm_interval, FRAME_INTERVAL);
150         write_reg32(&regs->period_start, PERIODIC_START);
151         reg = read_reg32(&regs->rh_desc_a);
152         reg &= ~( RHDA_PSM_INDIVIDUAL | RHDA_OCPM_PERPORT );
153         reg |= RHDA_NPS_ENABLE;
154         write_reg32(&regs->rh_desc_a, reg);
155         write_reg32(&regs->rh_desc_b, 0);
156         mb();
157         SLOF_msleep(100);
158         ohci_dump_regs(regs);
159         return 0;
160 }
161
162 /*
163  * OHCI Spec 7.4 Root Hub Partition
164  */
165 static void ohci_hub_check_ports(struct ohci_hcd *ohcd)
166 {
167         struct ohci_regs *regs;
168         struct usb_dev *dev;
169         unsigned int ports, i, port_status, port_clear = 0;
170
171         regs = ohcd->regs;
172         ports = read_reg32(&regs->rh_desc_a) & RHDA_NDP;
173         write_reg32(&regs->rh_status, RH_STATUS_LPSC);
174         SLOF_msleep(100);
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(&regs->rh_ps[i]);
179                 if (port_status & RH_PS_CSC) {
180                         if (port_status & RH_PS_CCS) {
181                                 write_reg32(&regs->rh_ps[i], RH_PS_PRS);
182                                 mb();
183                                 port_clear |= RH_PS_CSC;
184                                 dprintf("Start enumerating device\n");
185                                 SLOF_msleep(100);
186                         } else
187                                 printf("Start removing device\n");
188                 }
189                 port_status = read_reg32(&regs->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);
197                 }
198                 if (port_status & RH_PS_PESC) {
199                         port_clear |= RH_PS_PESC;
200                         if (port_status & RH_PS_PES)
201                                 dprintf("enabled\n");
202                         else
203                                 dprintf("disabled\n");
204                 }
205                 if (port_status & RH_PS_PSSC) {
206                         port_clear |= RH_PS_PESC;
207                         dprintf("suspended\n");
208                 }
209                 port_clear &= 0xFFFF0000;
210                 if (port_clear)
211                         write_reg32(&regs->rh_ps[i], port_clear);
212         }
213 }
214
215 static inline struct ohci_ed *ohci_pipe_get_ed(struct usb_pipe *pipe)
216 {
217         struct ohci_pipe *opipe;
218         opipe = container_of(pipe, struct ohci_pipe, pipe);
219         dpprintf("%s: ed is %p\n", __func__, &opipe->ed);
220         return &opipe->ed;
221 }
222
223 static inline long ohci_pipe_get_ed_phys(struct usb_pipe *pipe)
224 {
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;
229 }
230
231 static inline struct ohci_pipe *ohci_pipe_get_opipe(struct usb_pipe *pipe)
232 {
233         struct ohci_pipe *opipe;
234         opipe = container_of(pipe, struct ohci_pipe, pipe);
235         dpprintf("%s: opipe is %p\n", __func__, opipe);
236         return opipe;
237 }
238
239 static int ohci_alloc_pipe_pool(struct ohci_hcd *ohcd)
240 {
241         struct ohci_pipe *opipe, *curr, *prev;
242         long opipe_phys = 0;
243         unsigned int i, count;
244 #ifdef OHCI_DEBUG_PACKET
245         struct usb_pipe *pipe;
246 #endif
247
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);
251         if (!opipe)
252                 return false;
253
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++) {
259                 if (prev)
260                         prev->pipe.next = &curr->pipe;
261                 curr->pipe.next = NULL;
262                 prev = curr;
263
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);
268         }
269
270         if (!ohcd->freelist)
271                 ohcd->freelist = &opipe->pipe;
272         else
273                 ohcd->end->next = &opipe->pipe;
274         ohcd->end = &prev->pipe;
275
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));
281 #endif
282
283         dprintf("usb-ohci: %s exit\n", __func__);
284         return true;
285 }
286
287 static void ohci_init(struct usb_hcd_dev *hcidev)
288 {
289         struct ohci_hcd *ohcd;
290
291         printf("  OHCI: initializing\n");
292         dprintf("%s: device base address %p\n", __func__, hcidev->base);
293
294         ohcd = SLOF_alloc_mem(sizeof(struct ohci_hcd));
295         if (!ohcd) {
296                 printf("usb-ohci: Unable to allocate memory\n");
297                 goto out;
298         }
299
300         hcidev->nextaddr = 1;
301         hcidev->priv = ohcd;
302         memset(ohcd, 0, sizeof(*ohcd));
303         ohcd->hcidev = hcidev;
304         ohcd->freelist = NULL;
305         ohcd->end = 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",
310                         ohcd->hcca);
311                 goto out_free_hcd;
312         }
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);
317
318         ohci_hcd_reset(ohcd->regs);
319         ohci_hcd_init(ohcd);
320         ohci_hub_check_ports(ohcd);
321         return;
322
323 out_free_hcd:
324         SLOF_dma_free(ohcd->hcca, sizeof(struct ohci_hcca));
325         SLOF_free_mem(ohcd, sizeof(struct ohci_hcd));
326 out:
327         return;
328 }
329
330 static void ohci_exit(struct usb_hcd_dev *hcidev)
331 {
332         struct ohci_hcd *ohcd = NULL;
333
334         dprintf("%s: enter \n", __func__);
335         if (!hcidev && !hcidev->priv)
336                 return;
337
338         ohcd = hcidev->priv;
339         write_reg32(&ohcd->regs->control, (OHCI_CTRL_CBSR | OHCI_USB_SUSPEND));
340         SLOF_msleep(20);
341         write_reg32(&ohcd->regs->hcca, cpu_to_le32(0));
342
343         if (ohcd->pool) {
344                 SLOF_dma_map_out(ohcd->pool_phys, ohcd->pool, OHCI_PIPE_POOL_SIZE);
345                 SLOF_dma_free(ohcd->pool, OHCI_PIPE_POOL_SIZE);
346         }
347         if (ohcd->hcca) {
348                 SLOF_dma_map_out(ohcd->hcca_phys, ohcd->hcca, sizeof(struct ohci_hcca));
349                 SLOF_dma_free(ohcd->hcca, sizeof(struct ohci_hcca));
350         }
351         SLOF_free_mem(ohcd, sizeof(struct ohci_hcd));
352         return;
353 }
354
355 static void ohci_detect(void)
356 {
357
358 }
359
360 static void ohci_disconnect(void)
361 {
362
363 }
364
365 #define OHCI_CTRL_TDS 3
366
367 static void ohci_fill_td(struct ohci_td *td, long next,
368                         long req, size_t size, unsigned int attr)
369 {
370         if (size && req) {
371                 td->cbp = cpu_to_le32(req);
372                 td->be = cpu_to_le32(req + size - 1);
373         } else {
374                 td->cbp = 0;
375                 td->be = 0;
376         }
377         td->attr = cpu_to_le32(attr);
378         td->next_td = cpu_to_le32(next);
379
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));
383 }
384
385 static void ohci_fill_ed(struct ohci_ed *ed, long headp, long tailp,
386                         unsigned int attr, long next_ed)
387 {
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));
395
396 }
397
398 static long ohci_get_td_phys(struct ohci_td *curr, struct ohci_td *start, long td_phys)
399 {
400         dpprintf("position %d\n", curr - start);
401         return td_phys + (curr - start) * sizeof(*start);
402 }
403
404 static long ohci_get_td_virt(struct ohci_td *curr, struct ohci_td *start, long td_virt, long total_count)
405 {
406         dpprintf("position %d\n", curr - start);
407         if ( (curr - start) >= total_count) {
408                 /* busted position, should ignore this */
409                 return 0;
410         }
411         return td_virt + (curr - start) * sizeof(*start);
412 }
413
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)
418 {
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;
423         int ret = true;
424         long count;
425
426         count = total_count;
427         td_start_phys = (struct ohci_td *) __td_start_phys;
428         hcca = ohcd->hcca;
429         time = SLOF_GetTimer() + USB_TIMEOUT;
430         dpprintf("Claiming %ld\n", count);
431
432 again:
433         mb();
434         /* Check if there is an interrupt */
435         reg = read_reg32(&ohcd->regs->intr_status);
436         while(!(reg & OHCI_INTR_STATUS_WD))
437         {
438                 if (time < SLOF_GetTimer()) {
439                         printf("Timed out waiting for interrupt %x\n", reg);
440                         return false;
441                 }
442                 mb();
443                 reg = read_reg32(&ohcd->regs->intr_status);
444         }
445
446         /* Interrupt is there, read from done_head pointer */
447         td_phys = (struct ohci_td *)(uint64_t) le32_to_cpu(hcca->done_head);
448         if (!td_phys) {
449                 dprintf("Again td_phys null %ld\n");
450                 goto again;
451         }
452         hcca->done_head = 0;
453         mb();
454
455         while (td_phys && (count > 0)) {
456                 td = (struct ohci_td *)(uint64_t) ohci_get_td_virt(td_phys,
457                                                                 td_start_phys,
458                                                                 PTR_U32(td_start),
459                                                                 total_count);
460
461                 if (!td) {
462                         printf("USB: Error TD null %p\n", td_phys);
463                         break;
464                 }
465                 count--;
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",
469                         __func__,
470                         le32_to_cpu(td->cbp), le32_to_cpu(td->attr),
471                         le32_to_cpu(td->next_td), le32_to_cpu(td->be));
472                 mb();
473                 reg = (le32_to_cpu(td->attr) & TDA_CC) >> 28;
474                 if (reg) {
475                         dprintf("%s: cbp %08X attr %08X next_td %08X be %08X\n",
476                                 __func__,
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 */
481                                 ret = reg * -1;
482                 }
483                 prev_td = td;
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;
487                 mb();
488         }
489         /* clear the WD interrupt status */
490         write_reg32(&ohcd->regs->intr_status, OHCI_INTR_STATUS_WD);
491         mb();
492         read_reg32(&ohcd->regs->intr_status);
493
494         if (count > 0) {
495                 dpprintf("Pending count %d\n", count);
496                 goto again;
497         }
498         dprintf("TD claims done\n");
499         return ret;
500 }
501
502 /*
503  * OHCI Spec:
504  *           4.2 Endpoint Descriptor
505  *           4.3.1 General Transfer Descriptor
506  *           5.2.8 Transfer Descriptor Queues
507  */
508 static int ohci_send_ctrl(struct usb_pipe *pipe, struct usb_dev_req *req, void *data)
509 {
510         struct ohci_ed *ed;
511         struct ohci_td *tds, *td, *td_phys;
512         struct ohci_regs *regs;
513         struct ohci_hcd *ohcd;
514         uint32_t datalen;
515         uint32_t dir, attr = 0;
516         uint32_t time;
517         int ret = true, i;
518         long req_phys = 0, data_phys = 0, td_next = 0, td_count = 0;
519         unsigned char *dbuf;
520
521         datalen = le16_to_cpu(req->wLength);
522         dir = (req->bmRequestType & REQT_DIR_IN) ? 1 : 0;
523
524         dprintf("usb-ohci: %s len %d DIR_IN %d\n", __func__, datalen, dir);
525
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);
529
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);
534         td++; td_count++;
535
536         if (datalen) {
537                 data_phys = SLOF_dma_map_in(data, datalen, true);
538                 attr = 0;
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);
542                 td++; td_count++;
543         }
544
545         attr = 0;
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);
549         td_count++;
550
551         ed = ohci_pipe_get_ed(pipe);
552         attr = 0;
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);
558         mb();
559         ed->attr &= cpu_to_le32(~EDA_SKIP);
560
561         ohcd = pipe->dev->hcidev->priv;
562         regs = ohcd->regs;
563         write_reg32(&regs->cntl_head_ed, ohci_pipe_get_ed_phys(pipe));
564         mb();
565         write_reg32(&regs->cmd_status, OHCI_CMD_STATUS_CLF);
566
567         time = SLOF_GetTimer() + USB_TIMEOUT;
568         while ((time > SLOF_GetTimer()) &&
569                 ((ed->headp & EDA_HEADP_MASK_LE) != ed->tailp))
570                 cpu_relax();
571
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]);
579                 dpprintf("\n");
580                 if (datalen) {
581                         dbuf = (unsigned char *)data;
582                         dpprintf("Reply:   ");
583                         for(i = 0; i < datalen; i++)
584                                 printf("%02X ", dbuf[i]);
585                         dpprintf("\n");
586                 }
587 #endif
588         }
589         else {
590                 printf("%s: timed out - failed\n", __func__);
591                 dpprintf("%s: headp %08X tailp %08X next_td %08X attr %08X\n",
592                         __func__,
593                         le32_to_cpu(ed->headp), le32_to_cpu(ed->tailp),
594                         le32_to_cpu(ed->next_ed), le32_to_cpu(ed->attr));
595                 printf("Request: ");
596                 dbuf = (unsigned char *)req;
597                 for(i = 0; i < 8; i++)
598                         printf("%02X ", dbuf[i]);
599                 printf("\n");
600         }
601         ret = ohci_process_done_head(ohcd, tds, (long)td_phys, td_count);
602         mb();
603         ed->attr |= cpu_to_le32(EDA_SKIP);
604         mb();
605         write_reg32(&regs->cntl_head_ed, 0);
606         write_reg32(&regs->cntl_curr_ed, 0);
607
608         SLOF_dma_map_out(req_phys, req, sizeof(struct usb_dev_req));
609         if (datalen)
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;
614 }
615
616 static int ohci_transfer_bulk(struct usb_pipe *pipe, void *td_ptr,
617                         void *td_phys_ptr, void *data_phys, int datalen)
618 {
619         struct ohci_ed *ed;
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;
626         uint32_t time;
627         int ret = true;
628
629         if (pipe->type != USB_EP_TYPE_BULK) {
630                 printf("usb-ohci: Not a bulk pipe.\n");
631                 ret = false;
632                 goto end;
633         }
634
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);
639                 ret = false;
640                 goto end;
641         }
642
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);
647
648         if (!tds) {
649                 printf("%s: tds NULL recieved\n", __func__);
650                 ret = false;
651                 goto end;
652         }
653         memset(td, 0, sizeof(*td) * OHCI_MAX_TDS);
654
655         len = datalen;
656         ptr = (long)data_phys;
657         attr = 0;
658         attr = (dir ? TDA_DP_IN : TDA_DP_OUT) | TDA_CC | TDA_ROUNDING;
659         while (len) {
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;
665                 td++; td_count++;
666         }
667
668         ed = ohci_pipe_get_ed(pipe);
669         attr = 0;
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);
676         mb();
677         ed->attr &= cpu_to_le32(~EDA_SKIP);
678
679         ohcd = pipe->dev->hcidev->priv;
680         regs = ohcd->regs;
681         ed_phys = ohci_pipe_get_ed_phys(pipe);
682         write_reg32(&regs->bulk_head_ed, ed_phys);
683         mb();
684         write_reg32(&regs->cmd_status, 0x4);
685
686         time = SLOF_GetTimer() + USB_TIMEOUT;
687         while ((time > SLOF_GetTimer()) &&
688                 ((ed->headp & EDA_HEADP_MASK_LE) != ed->tailp))
689                 cpu_relax();
690
691         if ((ed->headp & EDA_HEADP_MASK_LE) == ed->tailp)
692                 dprintf("%s: packet sent\n", __func__);
693         else {
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));
697         }
698         mb();
699         ret = ohci_process_done_head(ohcd, tds, td_phys, td_count);
700         mb();
701         ed->attr |= cpu_to_le32(EDA_SKIP);
702         mb();
703         write_reg32(&regs->bulk_head_ed, 0);
704         write_reg32(&regs->bulk_curr_ed, 0);
705
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);
712                 mb();
713                 if (ret == USB_STALL) /* Call reset recovery */
714                         usb_msc_resetrecovery(pipe->dev);
715         }
716
717 end:
718         return (ret > 0) ? true : false;
719 }
720
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)
724 {
725         struct ohci_hcca *hcca;
726         struct ohci_pipe *opipe;
727         struct ohci_ed *ed;
728         struct usb_dev *dev;
729         struct ohci_td *tds, *td;
730         int32_t count = 0, i;
731         uint8_t *ptr;
732         uint16_t mps;
733         long ed_phys, td_phys, td_next, buf_phys;
734
735         if (!pipe || !ohcd)
736                 return false;
737
738         hcca = ohcd->hcca;
739         dev = pipe->dev;
740         if (dev->class != DEV_HID_KEYB && dev->class != DEV_HUB)
741                 return false;
742
743         opipe = ohci_pipe_get_opipe(pipe);
744         ed = &(opipe->ed);
745         ed_phys = opipe->ed_phys;
746         mps = pipe->mps;
747         ed->attr = cpu_to_le32(EDA_DIR_IN |
748                                EDA_FADDR(dev->addr) |
749                                dev->speed |
750                                EDA_MPS(pipe->mps) |
751                                EDA_SKIP |
752                                EDA_EP(pipe->epno));
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);
757         if (!tds) {
758                 printf("%s: alloc failed\n", __func__);
759                 return false;
760         }
761         td_phys = SLOF_dma_map_in(td, sizeof(*td) * count, false);
762
763         memset(tds, 0, sizeof(*tds) * count);
764         memset(buf, 0, buflen);
765         buf_phys = SLOF_dma_map_in(buf, buflen, false);
766         opipe->td = td;
767         opipe->td_phys = td_phys;
768         opipe->count = count;
769         opipe->buf = buf;
770         opipe->buflen = buflen;
771         opipe->buf_phys = buf_phys;
772
773         ptr = (uint8_t *)buf_phys;
774         for (i = 0; i < count - 1; i++, ptr += mps) {
775                 td = &tds[i];
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));
784         }
785         td->next_td = 0;
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);
789
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),
793                 count, mps);
794         ed->next_ed = 0;
795
796
797         switch (dev->class) {
798         case DEV_HID_KEYB:
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);
805                 break;
806
807         case DEV_HUB:
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);
811                 break;
812
813         default:
814                 dprintf("%s: unhandled class %d\n", __func__, dev->class);
815         }
816         return true;
817 }
818
819 static int ohci_put_pipe_intr(struct usb_pipe *pipe, struct ohci_hcd *ohcd)
820 {
821         struct ohci_hcca *hcca;
822         struct ohci_pipe *opipe;
823         struct ohci_ed *ed;
824         struct usb_dev *dev;
825         struct ohci_td *td;
826         long ed_phys;
827
828         if (!pipe || !ohcd)
829                 return false;
830
831         hcca = ohcd->hcca;
832         dev = pipe->dev;
833
834         if (dev->class != DEV_HID_KEYB && dev->class != DEV_HUB)
835                 return false;
836
837         opipe = ohci_pipe_get_opipe(pipe);
838         ed = &(opipe->ed);
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);
842
843         ed->attr |= cpu_to_le32(EDA_SKIP);
844         mb();
845         ed->headp = 0;
846         ed->tailp = 0;
847         ed->next_ed = 0;
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);
851
852         switch (dev->class) {
853         case DEV_HID_KEYB:
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);
859                 break;
860
861         case DEV_HUB:
862                 dprintf("%s: HUB class %d\n", __func__, dev->class);
863                 hcca->intr_table[1] = cpu_to_le32(ed_phys);
864                 break;
865
866         default:
867                 dprintf("%s: unhandled class %d\n", __func__, dev->class);
868         }
869         return true;
870 }
871
872 static int ohci_init_bulk_ed(struct usb_dev *dev, struct usb_pipe *pipe)
873 {
874         struct ohci_pipe *opipe;
875         struct ohci_ed *ed;
876         uint32_t dir;
877
878         if (!pipe || !dev)
879                 return false;
880
881         opipe = ohci_pipe_get_opipe(pipe);
882         ed = &(opipe->ed);
883         dir = pipe->dir ? EDA_DIR_IN : EDA_DIR_OUT;
884
885         ed->attr = cpu_to_le32(dir |
886                                EDA_FADDR(dev->addr) |
887                                dev->speed |
888                                EDA_MPS(pipe->mps) |
889                                EDA_SKIP |
890                                EDA_EP(pipe->epno));
891
892         dprintf("%s: pipe %p attr %x\n", __func__, pipe,
893                 le32_to_cpu(ed->attr));
894         return true;
895 }
896
897 static struct usb_pipe *ohci_get_pipe(struct usb_dev *dev, struct usb_ep_descr *ep,
898                                 char *buf, size_t buflen)
899 {
900         struct ohci_hcd *ohcd;
901         struct usb_pipe *new = NULL;
902
903         dprintf("usb-ohci: %s enter %p\n", __func__, dev);
904         if (!dev)
905                 return NULL;
906
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))
911                         return NULL;
912         }
913
914         new = ohcd->freelist;
915         ohcd->freelist = ohcd->freelist->next;
916         if (!ohcd->freelist)
917                 ohcd->end = NULL;
918
919         memset(new, 0, sizeof(*new));
920         new->dev = dev;
921         new->next = NULL;
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",
930                                 __func__, new);
931         if (new->type == USB_EP_TYPE_BULK)
932                 ohci_init_bulk_ed(dev, new);
933
934         dprintf("usb-ohci: %s exit %p\n", __func__, new);
935         return new;
936 }
937
938 static void ohci_put_pipe(struct usb_pipe *pipe)
939 {
940         struct ohci_hcd *ohcd;
941
942         dprintf("usb-ohci: %s enter - %p\n", __func__, pipe);
943         if (!pipe || !pipe->dev)
944                 return;
945         ohcd = pipe->dev->hcidev->priv;
946         if (ohcd->end)
947                 ohcd->end->next = pipe;
948         else
949                 ohcd->freelist = pipe;
950
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",
954                                 __func__, pipe);
955
956         ohcd->end = pipe;
957         pipe->next = NULL;
958         pipe->dev = NULL;
959         memset(pipe, 0, sizeof(*pipe));
960         dprintf("usb-ohci: %s exit\n", __func__);
961 }
962
963 static uint16_t ohci_get_last_frame(struct usb_dev *dev)
964 {
965         struct ohci_hcd *ohcd;
966         struct ohci_regs *regs;
967
968         ohcd = dev->hcidev->priv;
969         regs = ohcd->regs;
970         return read_reg32(&regs->fm_num);
971 }
972
973 static int ohci_poll_intr(struct usb_pipe *pipe, uint8_t *data)
974 {
975         struct ohci_pipe *opipe;
976         struct ohci_ed *ed;
977         struct ohci_td *head, *tail, *curr, *next;
978         struct ohci_td *head_phys, *tail_phys, *curr_phys;
979         uint8_t *ptr = NULL;
980         unsigned int i, pos;
981         static uint16_t last_frame;
982         long ptr_phys = 0;
983         long td_next;
984
985         if (!pipe || last_frame == ohci_get_last_frame(pipe->dev))
986                 return 0;
987
988         dprintf("%s: enter\n", __func__);
989
990         last_frame = ohci_get_last_frame(pipe->dev);
991         opipe = ohci_pipe_get_opipe(pipe);
992         ed = &opipe->ed;
993
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),
999                 opipe->count);
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);
1003
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); */
1007
1008         if (curr != head) {
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);
1014                 }
1015
1016                 next = curr + 1;
1017                 if (next == (opipe->td + opipe->count - 1))
1018                         next = opipe->td;
1019
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);
1028                 mb();
1029                 ed->tailp = cpu_to_le32(td_next);
1030         } else
1031                 return 0;
1032
1033         dprintf("%s: exit\n", __func__);
1034         return 1;
1035 }
1036
1037 struct usb_hcd_ops ohci_ops = {
1038         .name        = "ohci-hcd",
1039         .init        = ohci_init,
1040         .exit        = ohci_exit,
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,
1049         .next        = NULL,
1050 };
1051
1052 void usb_ohci_register(void)
1053 {
1054         usb_hcd_register(&ohci_ops);
1055 }