These changes are the raw update to qemu-2.6.
[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 (usb_setup_new_device(dev, i))
196                                 usb_slof_populate_new_device(dev);
197                         else
198                                 printf("usb-ohci: unable to setup device on port %d\n", i);
199                 }
200                 if (port_status & RH_PS_PESC) {
201                         port_clear |= RH_PS_PESC;
202                         if (port_status & RH_PS_PES)
203                                 dprintf("enabled\n");
204                         else
205                                 dprintf("disabled\n");
206                 }
207                 if (port_status & RH_PS_PSSC) {
208                         port_clear |= RH_PS_PESC;
209                         dprintf("suspended\n");
210                 }
211                 port_clear &= 0xFFFF0000;
212                 if (port_clear)
213                         write_reg32(&regs->rh_ps[i], port_clear);
214         }
215 }
216
217 static inline struct ohci_ed *ohci_pipe_get_ed(struct usb_pipe *pipe)
218 {
219         struct ohci_pipe *opipe;
220         opipe = container_of(pipe, struct ohci_pipe, pipe);
221         dpprintf("%s: ed is %p\n", __func__, &opipe->ed);
222         return &opipe->ed;
223 }
224
225 static inline long ohci_pipe_get_ed_phys(struct usb_pipe *pipe)
226 {
227         struct ohci_pipe *opipe;
228         opipe = container_of(pipe, struct ohci_pipe, pipe);
229         dpprintf("%s: ed_phys is %x\n", __func__, opipe->ed_phys);
230         return opipe->ed_phys;
231 }
232
233 static inline struct ohci_pipe *ohci_pipe_get_opipe(struct usb_pipe *pipe)
234 {
235         struct ohci_pipe *opipe;
236         opipe = container_of(pipe, struct ohci_pipe, pipe);
237         dpprintf("%s: opipe is %p\n", __func__, opipe);
238         return opipe;
239 }
240
241 static int ohci_alloc_pipe_pool(struct ohci_hcd *ohcd)
242 {
243         struct ohci_pipe *opipe, *curr, *prev;
244         long opipe_phys = 0;
245         unsigned int i, count;
246 #ifdef OHCI_DEBUG_PACKET
247         struct usb_pipe *pipe;
248 #endif
249
250         dprintf("usb-ohci: %s enter\n", __func__);
251         count = OHCI_PIPE_POOL_SIZE/sizeof(*opipe);
252         ohcd->pool = opipe = SLOF_dma_alloc(OHCI_PIPE_POOL_SIZE);
253         if (!opipe)
254                 return false;
255
256         ohcd->pool_phys = opipe_phys = SLOF_dma_map_in(opipe, OHCI_PIPE_POOL_SIZE, true);
257         dprintf("usb-ohci: %s opipe %p, opipe_phys %lx size %ld count %d\n",
258                 __func__, opipe, opipe_phys, sizeof(*opipe), count);
259         /* Although an array, link them*/
260         for (i = 0, curr = opipe, prev = NULL; i < count; i++, curr++) {
261                 if (prev)
262                         prev->pipe.next = &curr->pipe;
263                 curr->pipe.next = NULL;
264                 prev = curr;
265
266                 if (((uint64_t)&curr->ed) % 16)
267                         printf("usb-ohci: Warning ED not aligned to 16byte boundary");
268                 curr->ed_phys = opipe_phys + (curr - opipe) * sizeof(*curr) +
269                         offset_of(struct ohci_pipe, ed);
270         }
271
272         if (!ohcd->freelist)
273                 ohcd->freelist = &opipe->pipe;
274         else
275                 ohcd->end->next = &opipe->pipe;
276         ohcd->end = &prev->pipe;
277
278 #ifdef OHCI_DEBUG_PACKET
279         for (i = 0, pipe = ohcd->freelist; pipe; pipe = pipe->next)
280                 dprintf("usb-ohci: %d: pipe cur %p ed %p ed_phys %x\n",
281                         i++, pipe, ohci_pipe_get_ed(pipe),
282                         ohci_pipe_get_ed_phys(pipe));
283 #endif
284
285         dprintf("usb-ohci: %s exit\n", __func__);
286         return true;
287 }
288
289 static void ohci_init(struct usb_hcd_dev *hcidev)
290 {
291         struct ohci_hcd *ohcd;
292
293         printf("  OHCI: initializing\n");
294         dprintf("%s: device base address %p\n", __func__, hcidev->base);
295
296         ohcd = SLOF_alloc_mem(sizeof(struct ohci_hcd));
297         if (!ohcd) {
298                 printf("usb-ohci: Unable to allocate memory\n");
299                 goto out;
300         }
301
302         hcidev->nextaddr = 1;
303         hcidev->priv = ohcd;
304         memset(ohcd, 0, sizeof(*ohcd));
305         ohcd->hcidev = hcidev;
306         ohcd->freelist = NULL;
307         ohcd->end = NULL;
308         ohcd->regs = (struct ohci_regs *)(hcidev->base);
309         ohcd->hcca = SLOF_dma_alloc(sizeof(struct ohci_hcca));
310         if (!ohcd->hcca || PTR_U32(ohcd->hcca) & HCCA_ALIGN) {
311                 printf("usb-ohci: Unable to allocate/unaligned HCCA memory %p\n",
312                         ohcd->hcca);
313                 goto out_free_hcd;
314         }
315         ohcd->hcca_phys = SLOF_dma_map_in(ohcd->hcca,
316                                         sizeof(struct ohci_hcca), true);
317         dprintf("usb-ohci: HCCA memory %p HCCA-dev memory %08lx\n",
318                 ohcd->hcca, ohcd->hcca_phys);
319
320         ohci_hcd_reset(ohcd->regs);
321         ohci_hcd_init(ohcd);
322         ohci_hub_check_ports(ohcd);
323         return;
324
325 out_free_hcd:
326         SLOF_dma_free(ohcd->hcca, sizeof(struct ohci_hcca));
327         SLOF_free_mem(ohcd, sizeof(struct ohci_hcd));
328 out:
329         return;
330 }
331
332 static void ohci_exit(struct usb_hcd_dev *hcidev)
333 {
334         struct ohci_hcd *ohcd = NULL;
335
336         dprintf("%s: enter \n", __func__);
337         if (!hcidev && !hcidev->priv)
338                 return;
339
340         ohcd = hcidev->priv;
341         write_reg32(&ohcd->regs->control, (OHCI_CTRL_CBSR | OHCI_USB_SUSPEND));
342         SLOF_msleep(20);
343         write_reg32(&ohcd->regs->hcca, cpu_to_le32(0));
344
345         if (ohcd->pool) {
346                 SLOF_dma_map_out(ohcd->pool_phys, ohcd->pool, OHCI_PIPE_POOL_SIZE);
347                 SLOF_dma_free(ohcd->pool, OHCI_PIPE_POOL_SIZE);
348         }
349         if (ohcd->hcca) {
350                 SLOF_dma_map_out(ohcd->hcca_phys, ohcd->hcca, sizeof(struct ohci_hcca));
351                 SLOF_dma_free(ohcd->hcca, sizeof(struct ohci_hcca));
352         }
353         SLOF_free_mem(ohcd, sizeof(struct ohci_hcd));
354         return;
355 }
356
357 static void ohci_detect(void)
358 {
359
360 }
361
362 static void ohci_disconnect(void)
363 {
364
365 }
366
367 #define OHCI_CTRL_TDS 3
368
369 static void ohci_fill_td(struct ohci_td *td, long next,
370                         long req, size_t size, unsigned int attr)
371 {
372         if (size && req) {
373                 td->cbp = cpu_to_le32(req);
374                 td->be = cpu_to_le32(req + size - 1);
375         } else {
376                 td->cbp = 0;
377                 td->be = 0;
378         }
379         td->attr = cpu_to_le32(attr);
380         td->next_td = cpu_to_le32(next);
381
382         dpprintf("%s: cbp %08X attr %08X next_td %08X be %08X\n", __func__,
383                 le32_to_cpu(td->cbp), le32_to_cpu(td->attr),
384                 le32_to_cpu(td->next_td), le32_to_cpu(td->be));
385 }
386
387 static void ohci_fill_ed(struct ohci_ed *ed, long headp, long tailp,
388                         unsigned int attr, long next_ed)
389 {
390         ed->attr = cpu_to_le32(attr);
391         ed->headp = cpu_to_le32(headp) | (ed->headp & ~EDA_HEADP_MASK_LE);
392         ed->tailp = cpu_to_le32(tailp);
393         ed->next_ed = cpu_to_le32(next_ed);
394         dpprintf("%s: headp %08X tailp %08X next_td %08X attr %08X\n", __func__,
395                 le32_to_cpu(ed->headp), le32_to_cpu(ed->tailp),
396                 le32_to_cpu(ed->next_ed), le32_to_cpu(ed->attr));
397
398 }
399
400 static long ohci_get_td_phys(struct ohci_td *curr, struct ohci_td *start, long td_phys)
401 {
402         dpprintf("position %d\n", curr - start);
403         return td_phys + (curr - start) * sizeof(*start);
404 }
405
406 static long ohci_get_td_virt(struct ohci_td *curr, struct ohci_td *start, long td_virt, long total_count)
407 {
408         dpprintf("position %d\n", curr - start);
409         if ( (curr - start) >= total_count) {
410                 /* busted position, should ignore this */
411                 return 0;
412         }
413         return td_virt + (curr - start) * sizeof(*start);
414 }
415
416 /* OHCI Spec: 4.4.2.3 HccaDoneHead*/
417 static int ohci_process_done_head(struct ohci_hcd *ohcd,
418                                 struct ohci_td *td_start,
419                                 long __td_start_phys, long total_count)
420 {
421         struct ohci_hcca *hcca;
422         struct ohci_td *td_phys = NULL, *td_start_phys;
423         struct ohci_td *td, *prev_td = NULL;
424         uint32_t reg = 0, time = 0;
425         int ret = true;
426         long count;
427
428         count = total_count;
429         td_start_phys = (struct ohci_td *) __td_start_phys;
430         hcca = ohcd->hcca;
431         time = SLOF_GetTimer() + USB_TIMEOUT;
432         dpprintf("Claiming %ld\n", count);
433
434 again:
435         mb();
436         /* Check if there is an interrupt */
437         reg = read_reg32(&ohcd->regs->intr_status);
438         while(!(reg & OHCI_INTR_STATUS_WD))
439         {
440                 if (time < SLOF_GetTimer()) {
441                         printf("Timed out waiting for interrupt %x\n", reg);
442                         return false;
443                 }
444                 mb();
445                 reg = read_reg32(&ohcd->regs->intr_status);
446         }
447
448         /* Interrupt is there, read from done_head pointer */
449         td_phys = (struct ohci_td *)(uint64_t) le32_to_cpu(hcca->done_head);
450         if (!td_phys) {
451                 dprintf("Again td_phys null\n");
452                 goto again;
453         }
454         hcca->done_head = 0;
455         mb();
456
457         while (td_phys && (count > 0)) {
458                 td = (struct ohci_td *)(uint64_t) ohci_get_td_virt(td_phys,
459                                                                 td_start_phys,
460                                                                 PTR_U32(td_start),
461                                                                 total_count);
462
463                 if (!td) {
464                         printf("USB: Error TD null %p\n", td_phys);
465                         break;
466                 }
467                 count--;
468                 dprintf("Claimed %p(%p) td_start %p count %ld\n",
469                         td, td_phys, td_start_phys, count);
470                 dpprintf("%s: cbp %08X attr %08X next_td %08X be %08X\n",
471                         __func__,
472                         le32_to_cpu(td->cbp), le32_to_cpu(td->attr),
473                         le32_to_cpu(td->next_td), le32_to_cpu(td->be));
474                 mb();
475                 reg = (le32_to_cpu(td->attr) & TDA_CC) >> 28;
476                 if (reg) {
477                         dprintf("%s: cbp %08X attr %08X next_td %08X be %08X\n",
478                                 __func__,
479                                 le32_to_cpu(td->cbp), le32_to_cpu(td->attr),
480                                 le32_to_cpu(td->next_td), le32_to_cpu(td->be));
481                         printf("USB: Error %s %p\n", tda_cc_error[reg], td);
482                         if (reg > 3) /* Return negative error code */
483                                 ret = reg * -1;
484                 }
485                 prev_td = td;
486                 td_phys = (struct ohci_td *)(uint64_t) le32_to_cpu(td->next_td);
487                 prev_td->attr |= cpu_to_le32(TDA_DONE);
488                 prev_td->next_td = 0;
489                 mb();
490         }
491         /* clear the WD interrupt status */
492         write_reg32(&ohcd->regs->intr_status, OHCI_INTR_STATUS_WD);
493         mb();
494         read_reg32(&ohcd->regs->intr_status);
495
496         if (count > 0) {
497                 dpprintf("Pending count %d\n", count);
498                 goto again;
499         }
500         dprintf("TD claims done\n");
501         return ret;
502 }
503
504 /*
505  * OHCI Spec:
506  *           4.2 Endpoint Descriptor
507  *           4.3.1 General Transfer Descriptor
508  *           5.2.8 Transfer Descriptor Queues
509  */
510 static int ohci_send_ctrl(struct usb_pipe *pipe, struct usb_dev_req *req, void *data)
511 {
512         struct ohci_ed *ed;
513         struct ohci_td *tds, *td, *td_phys;
514         struct ohci_regs *regs;
515         struct ohci_hcd *ohcd;
516         uint32_t datalen;
517         uint32_t dir, attr = 0;
518         uint32_t time;
519         int ret = true, i;
520         long req_phys = 0, data_phys = 0, td_next = 0, td_count = 0;
521         unsigned char *dbuf;
522
523         datalen = le16_to_cpu(req->wLength);
524         dir = (req->bmRequestType & REQT_DIR_IN) ? 1 : 0;
525
526         dprintf("usb-ohci: %s len %d DIR_IN %d\n", __func__, datalen, dir);
527
528         tds = td = (struct ohci_td *) SLOF_dma_alloc(sizeof(*td) * OHCI_CTRL_TDS);
529         td_phys = (struct ohci_td *) SLOF_dma_map_in(td, sizeof(*td) * OHCI_CTRL_TDS, true);
530         memset(td, 0, sizeof(*td) * OHCI_CTRL_TDS);
531
532         req_phys = SLOF_dma_map_in(req, sizeof(struct usb_dev_req), true);
533         attr = TDA_DP_SETUP | TDA_CC | TDA_TOGGLE_DATA0;
534         td_next = ohci_get_td_phys(td + 1, tds, PTR_U32(td_phys));
535         ohci_fill_td(td, td_next, req_phys, sizeof(*req), attr);
536         td++; td_count++;
537
538         if (datalen) {
539                 data_phys = SLOF_dma_map_in(data, datalen, true);
540                 attr = 0;
541                 attr = (dir ? TDA_DP_IN : TDA_DP_OUT) | TDA_TOGGLE_DATA1 | TDA_CC;
542                 td_next = ohci_get_td_phys(td + 1, tds, PTR_U32(td_phys));
543                 ohci_fill_td(td, td_next, data_phys, datalen, attr);
544                 td++; td_count++;
545         }
546
547         attr = 0;
548         attr = (dir ? TDA_DP_OUT : TDA_DP_IN) | TDA_CC | TDA_TOGGLE_DATA1;
549         td_next = ohci_get_td_phys(td + 1, tds, PTR_U32(td_phys));
550         ohci_fill_td(td, 0, 0, 0, attr);
551         td_count++;
552
553         ed = ohci_pipe_get_ed(pipe);
554         attr = 0;
555         attr = EDA_FADDR(pipe->dev->addr) | EDA_MPS(pipe->mps) | EDA_SKIP;
556         ohci_fill_ed(ed, PTR_U32(td_phys), td_next, attr, 0);
557         ed->tailp = 0; /* HACK */
558         dprintf("usb-ohci: %s - td_start %p td_end %lx req %lx\n", __func__,
559                 td_phys, td_next, req_phys);
560         mb();
561         ed->attr &= cpu_to_le32(~EDA_SKIP);
562
563         ohcd = pipe->dev->hcidev->priv;
564         regs = ohcd->regs;
565         write_reg32(&regs->cntl_head_ed, ohci_pipe_get_ed_phys(pipe));
566         mb();
567         write_reg32(&regs->cmd_status, OHCI_CMD_STATUS_CLF);
568
569         time = SLOF_GetTimer() + USB_TIMEOUT;
570         while ((time > SLOF_GetTimer()) &&
571                 ((ed->headp & EDA_HEADP_MASK_LE) != ed->tailp))
572                 cpu_relax();
573
574         if ((ed->headp & EDA_HEADP_MASK_LE) == ed->tailp) {
575                 dprintf("%s: packet sent\n", __func__);
576 #ifdef OHCI_DEBUG_PACKET
577                 dpprintf("Request: ");
578                 dbuf = (unsigned char *)req;
579                 for(i = 0; i < 8; i++)
580                         printf("%02X ", dbuf[i]);
581                 dpprintf("\n");
582                 if (datalen) {
583                         dbuf = (unsigned char *)data;
584                         dpprintf("Reply:   ");
585                         for(i = 0; i < datalen; i++)
586                                 printf("%02X ", dbuf[i]);
587                         dpprintf("\n");
588                 }
589 #endif
590         }
591         else {
592                 printf("%s: timed out - failed\n", __func__);
593                 dpprintf("%s: headp %08X tailp %08X next_td %08X attr %08X\n",
594                         __func__,
595                         le32_to_cpu(ed->headp), le32_to_cpu(ed->tailp),
596                         le32_to_cpu(ed->next_ed), le32_to_cpu(ed->attr));
597                 printf("Request: ");
598                 dbuf = (unsigned char *)req;
599                 for(i = 0; i < 8; i++)
600                         printf("%02X ", dbuf[i]);
601                 printf("\n");
602         }
603         ret = ohci_process_done_head(ohcd, tds, (long)td_phys, td_count);
604         mb();
605         ed->attr |= cpu_to_le32(EDA_SKIP);
606         mb();
607         write_reg32(&regs->cntl_head_ed, 0);
608         write_reg32(&regs->cntl_curr_ed, 0);
609
610         SLOF_dma_map_out(req_phys, req, sizeof(struct usb_dev_req));
611         if (datalen)
612                 SLOF_dma_map_out(data_phys, data, datalen);
613         SLOF_dma_map_out(PTR_U32(td_phys), tds, sizeof(*td) * OHCI_CTRL_TDS);
614         SLOF_dma_free(tds, sizeof(*td) * OHCI_CTRL_TDS);
615         return (ret > 0) ? true : false;
616 }
617
618 static int ohci_transfer_bulk(struct usb_pipe *pipe, void *td_ptr,
619                         void *td_phys_ptr, void *data_phys, int datalen)
620 {
621         struct ohci_ed *ed;
622         struct ohci_td *td, *tds;
623         struct ohci_regs *regs;
624         struct ohci_hcd *ohcd;
625         long td_phys = 0, td_next, ed_phys, ptr, td_count = 0;
626         uint32_t dir, attr = 0, count;
627         size_t len, packet_len;
628         uint32_t time;
629         int ret = true;
630
631         if (pipe->type != USB_EP_TYPE_BULK) {
632                 printf("usb-ohci: Not a bulk pipe.\n");
633                 ret = false;
634                 goto end;
635         }
636
637         dir = (pipe->dir == USB_PIPE_OUT) ? 0 : 1;
638         count = datalen / OHCI_MAX_BULK_SIZE;
639         if (count > OHCI_MAX_TDS) {
640                 printf("usb-ohci: buffer size not supported - %d\n", datalen);
641                 ret = false;
642                 goto end;
643         }
644
645         td = tds = (struct ohci_td *) td_ptr;
646         td_phys = (long)td_phys_ptr;
647         dprintf("usb-ohci: %s pipe %p data_phys %p len %d DIR_IN %d td %p td_phys %lx\n",
648                 __func__, pipe, data_phys, datalen, dir, td, td_phys);
649
650         if (!tds) {
651                 printf("%s: tds NULL recieved\n", __func__);
652                 ret = false;
653                 goto end;
654         }
655         memset(td, 0, sizeof(*td) * OHCI_MAX_TDS);
656
657         len = datalen;
658         ptr = (long)data_phys;
659         attr = 0;
660         attr = (dir ? TDA_DP_IN : TDA_DP_OUT) | TDA_CC | TDA_ROUNDING;
661         while (len) {
662                 packet_len = (OHCI_MAX_BULK_SIZE < len)? OHCI_MAX_BULK_SIZE : len;
663                 td_next = ohci_get_td_phys((td + 1), tds, td_phys);
664                 ohci_fill_td(td, td_next, ptr, packet_len, attr);
665                 ptr = ptr + packet_len;
666                 len = len - packet_len;
667                 td++; td_count++;
668         }
669
670         ed = ohci_pipe_get_ed(pipe);
671         attr = 0;
672         dir = pipe->dir ? EDA_DIR_IN : EDA_DIR_OUT;
673         attr = dir | EDA_FADDR(pipe->dev->addr) | EDA_MPS(pipe->mps)
674                 | EDA_SKIP | pipe->dev->speed | EDA_EP(pipe->epno);
675         td_next = ohci_get_td_phys(td, tds, td_phys);
676         ohci_fill_ed(ed, td_phys, td_next, attr, 0);
677         dprintf("usb-ohci: %s - tds %lx td %lx\n", __func__, td_phys, td_next);
678         mb();
679         ed->attr &= cpu_to_le32(~EDA_SKIP);
680
681         ohcd = pipe->dev->hcidev->priv;
682         regs = ohcd->regs;
683         ed_phys = ohci_pipe_get_ed_phys(pipe);
684         write_reg32(&regs->bulk_head_ed, ed_phys);
685         mb();
686         write_reg32(&regs->cmd_status, 0x4);
687
688         time = SLOF_GetTimer() + USB_TIMEOUT;
689         while ((time > SLOF_GetTimer()) &&
690                 ((ed->headp & EDA_HEADP_MASK_LE) != ed->tailp))
691                 cpu_relax();
692
693         if ((ed->headp & EDA_HEADP_MASK_LE) == ed->tailp)
694                 dprintf("%s: packet sent\n", __func__);
695         else {
696                 dpprintf("%s: headp %08X tailp %08X next_td %08X attr %08X\n", __func__,
697                          le32_to_cpu(ed->headp), le32_to_cpu(ed->tailp),
698                          le32_to_cpu(ed->next_ed), le32_to_cpu(ed->attr));
699         }
700         mb();
701         ret = ohci_process_done_head(ohcd, tds, td_phys, td_count);
702         mb();
703         ed->attr |= cpu_to_le32(EDA_SKIP);
704         mb();
705         write_reg32(&regs->bulk_head_ed, 0);
706         write_reg32(&regs->bulk_curr_ed, 0);
707
708         if (le32_to_cpu(ed->headp) & EDA_HEADP_HALTED) {
709                 printf("ED Halted\n");
710                 printf("%s: headp %08X tailp %08X next_td %08X attr %08X\n", __func__,
711                         le32_to_cpu(ed->headp), le32_to_cpu(ed->tailp),
712                         le32_to_cpu(ed->next_ed), le32_to_cpu(ed->attr));
713                 ed->headp &= ~cpu_to_le32(EDA_HEADP_HALTED);
714                 mb();
715                 if (ret == USB_STALL) /* Call reset recovery */
716                         usb_msc_resetrecovery(pipe->dev);
717         }
718
719 end:
720         return (ret > 0) ? true : false;
721 }
722
723 /* Populate the hcca intr region with periodic intr */
724 static int ohci_get_pipe_intr(struct usb_pipe *pipe, struct ohci_hcd *ohcd,
725                         char *buf, size_t buflen)
726 {
727         struct ohci_hcca *hcca;
728         struct ohci_pipe *opipe;
729         struct ohci_ed *ed;
730         struct usb_dev *dev;
731         struct ohci_td *tds, *td;
732         int32_t count = 0, i;
733         uint8_t *ptr;
734         uint16_t mps;
735         long ed_phys, td_phys, td_next, buf_phys;
736
737         if (!pipe || !ohcd)
738                 return false;
739
740         hcca = ohcd->hcca;
741         dev = pipe->dev;
742         if (dev->class != DEV_HID_KEYB && dev->class != DEV_HUB)
743                 return false;
744
745         opipe = ohci_pipe_get_opipe(pipe);
746         ed = &(opipe->ed);
747         ed_phys = opipe->ed_phys;
748         mps = pipe->mps;
749         ed->attr = cpu_to_le32(EDA_DIR_IN |
750                                EDA_FADDR(dev->addr) |
751                                dev->speed |
752                                EDA_MPS(pipe->mps) |
753                                EDA_SKIP |
754                                EDA_EP(pipe->epno));
755         dprintf("%s: pipe %p ed %p dev %p opipe %p\n", __func__,
756                 pipe, ed, dev, opipe);
757         count = (buflen/mps) + 1;
758         tds = td = SLOF_dma_alloc(sizeof(*td) * count);
759         if (!tds) {
760                 printf("%s: alloc failed\n", __func__);
761                 return false;
762         }
763         td_phys = SLOF_dma_map_in(td, sizeof(*td) * count, false);
764
765         memset(tds, 0, sizeof(*tds) * count);
766         memset(buf, 0, buflen);
767         buf_phys = SLOF_dma_map_in(buf, buflen, false);
768         opipe->td = td;
769         opipe->td_phys = td_phys;
770         opipe->count = count;
771         opipe->buf = buf;
772         opipe->buflen = buflen;
773         opipe->buf_phys = buf_phys;
774
775         ptr = (uint8_t *)buf_phys;
776         for (i = 0; i < count - 1; i++, ptr += mps) {
777                 td = &tds[i];
778                 td_next = ohci_get_td_phys(td + 1, &tds[0], td_phys);
779                 td->cbp = cpu_to_le32(PTR_U32(ptr));
780                 td->attr = cpu_to_le32(TDA_DP_IN | TDA_ROUNDING | TDA_CC);
781                 td->next_td = cpu_to_le32(td_next);
782                 td->be = cpu_to_le32(PTR_U32(ptr) + mps - 1);
783                 dprintf("td %p td++ %x ptr %p be %x\n",
784                         td, le32_to_cpu(td->next_td),
785                         ptr, (PTR_U32(ptr) + mps - 1));
786         }
787         td->next_td = 0;
788         td_next = ohci_get_td_phys(td, &tds[0], td_phys);
789         ed->headp = cpu_to_le32(td_phys);
790         ed->tailp = cpu_to_le32(td_next);
791
792         dprintf("%s: head %08X tail %08X, count %d, mps %d\n", __func__,
793                 le32_to_cpu(ed->headp),
794                 le32_to_cpu(ed->tailp),
795                 count, mps);
796         ed->next_ed = 0;
797
798
799         switch (dev->class) {
800         case DEV_HID_KEYB:
801                 dprintf("%s: Keyboard class %d\n", __func__, dev->class);
802                 hcca->intr_table[0] = cpu_to_le32(ed_phys);
803                 hcca->intr_table[8] = cpu_to_le32(ed_phys);
804                 hcca->intr_table[16] = cpu_to_le32(ed_phys);
805                 hcca->intr_table[24] = cpu_to_le32(ed_phys);
806                 ed->attr &= cpu_to_le32(~EDA_SKIP);
807                 break;
808
809         case DEV_HUB:
810                 dprintf("%s: HUB class %x\n", __func__, dev->class);
811                 hcca->intr_table[1] = cpu_to_le32(ed_phys);
812                 ed->attr &= cpu_to_le32(~EDA_SKIP);
813                 break;
814
815         default:
816                 dprintf("%s: unhandled class %d\n", __func__, dev->class);
817         }
818         return true;
819 }
820
821 static int ohci_put_pipe_intr(struct usb_pipe *pipe, struct ohci_hcd *ohcd)
822 {
823         struct ohci_hcca *hcca;
824         struct ohci_pipe *opipe;
825         struct ohci_ed *ed;
826         struct usb_dev *dev;
827         struct ohci_td *td;
828         long ed_phys;
829
830         if (!pipe || !ohcd)
831                 return false;
832
833         hcca = ohcd->hcca;
834         dev = pipe->dev;
835
836         if (dev->class != DEV_HID_KEYB && dev->class != DEV_HUB)
837                 return false;
838
839         opipe = ohci_pipe_get_opipe(pipe);
840         ed = &(opipe->ed);
841         ed_phys = opipe->ed_phys;
842         dprintf("%s: td %p td_phys %08lx buf %p buf_phys %08lx\n", __func__,
843                 opipe->td, opipe->td_phys, opipe->buf, opipe->buf_phys);
844
845         ed->attr |= cpu_to_le32(EDA_SKIP);
846         mb();
847         ed->headp = 0;
848         ed->tailp = 0;
849         ed->next_ed = 0;
850         SLOF_dma_map_out(opipe->buf_phys, opipe->buf, opipe->buflen);
851         SLOF_dma_map_out(opipe->td_phys, opipe->td, sizeof(*td) * opipe->count);
852         SLOF_dma_free(opipe->td, sizeof(*td) * opipe->count);
853
854         switch (dev->class) {
855         case DEV_HID_KEYB:
856                 dprintf("%s: Keyboard class %d\n", __func__, dev->class);
857                 hcca->intr_table[0] = cpu_to_le32(ed_phys);
858                 hcca->intr_table[8] = cpu_to_le32(ed_phys);
859                 hcca->intr_table[16] = cpu_to_le32(ed_phys);
860                 hcca->intr_table[24] = cpu_to_le32(ed_phys);
861                 break;
862
863         case DEV_HUB:
864                 dprintf("%s: HUB class %d\n", __func__, dev->class);
865                 hcca->intr_table[1] = cpu_to_le32(ed_phys);
866                 break;
867
868         default:
869                 dprintf("%s: unhandled class %d\n", __func__, dev->class);
870         }
871         return true;
872 }
873
874 static int ohci_init_bulk_ed(struct usb_dev *dev, struct usb_pipe *pipe)
875 {
876         struct ohci_pipe *opipe;
877         struct ohci_ed *ed;
878         uint32_t dir;
879
880         if (!pipe || !dev)
881                 return false;
882
883         opipe = ohci_pipe_get_opipe(pipe);
884         ed = &(opipe->ed);
885         dir = pipe->dir ? EDA_DIR_IN : EDA_DIR_OUT;
886
887         ed->attr = cpu_to_le32(dir |
888                                EDA_FADDR(dev->addr) |
889                                dev->speed |
890                                EDA_MPS(pipe->mps) |
891                                EDA_SKIP |
892                                EDA_EP(pipe->epno));
893
894         dprintf("%s: pipe %p attr %x\n", __func__, pipe,
895                 le32_to_cpu(ed->attr));
896         return true;
897 }
898
899 static struct usb_pipe *ohci_get_pipe(struct usb_dev *dev, struct usb_ep_descr *ep,
900                                 char *buf, size_t buflen)
901 {
902         struct ohci_hcd *ohcd;
903         struct usb_pipe *new = NULL;
904
905         dprintf("usb-ohci: %s enter %p\n", __func__, dev);
906         if (!dev)
907                 return NULL;
908
909         ohcd = (struct ohci_hcd *)dev->hcidev->priv;
910         if (!ohcd->freelist) {
911                 dprintf("usb-ohci: %s allocating pool\n", __func__);
912                 if (!ohci_alloc_pipe_pool(ohcd))
913                         return NULL;
914         }
915
916         new = ohcd->freelist;
917         ohcd->freelist = ohcd->freelist->next;
918         if (!ohcd->freelist)
919                 ohcd->end = NULL;
920
921         memset(new, 0, sizeof(*new));
922         new->dev = dev;
923         new->next = NULL;
924         new->type = ep->bmAttributes & USB_EP_TYPE_MASK;
925         new->speed = dev->speed;
926         new->mps = le16_to_cpu(ep->wMaxPacketSize);
927         new->epno = ep->bEndpointAddress & 0xF;
928         new->dir = ep->bEndpointAddress & 0x80;
929         if (new->type == USB_EP_TYPE_INTR)
930                 if (!ohci_get_pipe_intr(new, ohcd, buf, buflen))
931                         dprintf("usb-ohci: %s alloc_intr failed  %p\n",
932                                 __func__, new);
933         if (new->type == USB_EP_TYPE_BULK)
934                 ohci_init_bulk_ed(dev, new);
935
936         dprintf("usb-ohci: %s exit %p\n", __func__, new);
937         return new;
938 }
939
940 static void ohci_put_pipe(struct usb_pipe *pipe)
941 {
942         struct ohci_hcd *ohcd;
943
944         dprintf("usb-ohci: %s enter - %p\n", __func__, pipe);
945         if (!pipe || !pipe->dev)
946                 return;
947         ohcd = pipe->dev->hcidev->priv;
948         if (ohcd->end)
949                 ohcd->end->next = pipe;
950         else
951                 ohcd->freelist = pipe;
952
953         if (pipe->type == USB_EP_TYPE_INTR)
954                 if (!ohci_put_pipe_intr(pipe, ohcd))
955                         dprintf("usb-ohci: %s alloc_intr failed  %p\n",
956                                 __func__, pipe);
957
958         ohcd->end = pipe;
959         pipe->next = NULL;
960         pipe->dev = NULL;
961         memset(pipe, 0, sizeof(*pipe));
962         dprintf("usb-ohci: %s exit\n", __func__);
963 }
964
965 static uint16_t ohci_get_last_frame(struct usb_dev *dev)
966 {
967         struct ohci_hcd *ohcd;
968         struct ohci_regs *regs;
969
970         ohcd = dev->hcidev->priv;
971         regs = ohcd->regs;
972         return read_reg32(&regs->fm_num);
973 }
974
975 static int ohci_poll_intr(struct usb_pipe *pipe, uint8_t *data)
976 {
977         struct ohci_pipe *opipe;
978         struct ohci_ed *ed;
979         struct ohci_td *head, *tail, *curr, *next;
980         struct ohci_td *head_phys, *tail_phys, *curr_phys;
981         uint8_t *ptr = NULL;
982         unsigned int i, pos;
983         static uint16_t last_frame;
984         long ptr_phys = 0;
985         long td_next;
986
987         if (!pipe || last_frame == ohci_get_last_frame(pipe->dev))
988                 return 0;
989
990         dprintf("%s: enter\n", __func__);
991
992         last_frame = ohci_get_last_frame(pipe->dev);
993         opipe = ohci_pipe_get_opipe(pipe);
994         ed = &opipe->ed;
995
996         head_phys = (struct ohci_td *)(long)(le32_to_cpu(ed->headp) & EDA_HEADP_MASK);
997         tail_phys = (struct ohci_td *)(long)le32_to_cpu(ed->tailp);
998         curr_phys = (struct ohci_td *) opipe->td_phys;
999         pos = (tail_phys - curr_phys + 1) % (opipe->count - 1);
1000         dprintf("pos %d %ld -- %d\n", pos, (tail_phys - curr_phys + 1),
1001                 opipe->count);
1002         curr = opipe->td + pos;
1003         head = opipe->td + (head_phys - (struct ohci_td *) opipe->td_phys);
1004         tail = opipe->td + (tail_phys - (struct ohci_td *) opipe->td_phys);
1005
1006         /* dprintf("%08X %08X %08X %08X\n",
1007            opipe->td_phys, head_phys, tail_phys, curr_phys);
1008            dprintf("%08X %08X %08X %08X\n", opipe->td, head, tail, curr); */
1009
1010         if (curr != head) {
1011                 ptr = (uint8_t *) ((long)opipe->buf + pipe->mps * pos);
1012                 ptr_phys = opipe->buf_phys + pipe->mps * pos;
1013                 if (le32_to_cpu(*(uint32_t *)ptr) != 0) {
1014                         for (i = 0; i < 8; i++)
1015                                 data[i] = *(ptr + i);
1016                 }
1017
1018                 next = curr + 1;
1019                 if (next == (opipe->td + opipe->count - 1))
1020                         next = opipe->td;
1021
1022                 curr->attr = cpu_to_le32(TDA_DP_IN | TDA_ROUNDING | TDA_CC);
1023                 curr->next_td = cpu_to_le32(0);
1024                 curr->cbp = cpu_to_le32(PTR_U32(ptr_phys));
1025                 curr->be = cpu_to_le32(PTR_U32(ptr_phys + pipe->mps - 1));
1026                 td_next = ohci_get_td_phys(curr, opipe->td, opipe->td_phys);
1027                 dprintf("Connecting %p to %p(phys %08lx) ptr %p, "
1028                         "ptr_phys %08lx\n", tail, curr, td_next, ptr, ptr_phys);
1029                 tail->next_td = cpu_to_le32(td_next);
1030                 mb();
1031                 ed->tailp = cpu_to_le32(td_next);
1032         } else
1033                 return 0;
1034
1035         dprintf("%s: exit\n", __func__);
1036         return 1;
1037 }
1038
1039 struct usb_hcd_ops ohci_ops = {
1040         .name        = "ohci-hcd",
1041         .init        = ohci_init,
1042         .exit        = ohci_exit,
1043         .detect      = ohci_detect,
1044         .disconnect  = ohci_disconnect,
1045         .get_pipe    = ohci_get_pipe,
1046         .put_pipe    = ohci_put_pipe,
1047         .send_ctrl   = ohci_send_ctrl,
1048         .transfer_bulk = ohci_transfer_bulk,
1049         .poll_intr   = ohci_poll_intr,
1050         .usb_type    = USB_OHCI,
1051         .next        = NULL,
1052 };
1053
1054 void usb_ohci_register(void)
1055 {
1056         usb_hcd_register(&ohci_ops);
1057 }