Merge branch 'next'
[u-boot/qq2440-u-boot.git] / drivers / usb / host / isp116x-hcd.c
blob46e4cee1d04c9dd3af28bdaa09b121f11e056719
1 /*
2 * ISP116x HCD (Host Controller Driver) for u-boot.
4 * Copyright (C) 2006-2007 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (C) 2006-2007 Eurotech S.p.A. <info@eurotech.it>
7 * SPDX-License-Identifier: GPL-2.0+
9 * Derived in part from the SL811 HCD driver "u-boot/drivers/usb/sl811_usb.c"
10 * (original copyright message follows):
12 * (C) Copyright 2004
13 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 * This code is based on linux driver for sl811hs chip, source at
16 * drivers/usb/host/sl811.c:
18 * SL811 Host Controller Interface driver for USB.
20 * Copyright (c) 2003/06, Courage Co., Ltd.
22 * Based on:
23 * 1.uhci.c by Linus Torvalds, Johannes Erdfelt, Randy Dunlap,
24 * Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber,
25 * Adam Richter, Gregory P. Smith;
26 * 2.Original SL811 driver (hc_sl811.o) by Pei Liu <pbl@cypress.com>
27 * 3.Rewrited as sl811.o by Yin Aihua <yinah:couragetech.com.cn>
29 * [[GNU/GPL disclaimer]]
31 * and in part from AU1x00 OHCI HCD driver "u-boot/arch/mips/cpu/au1x00_usb_ohci.c"
32 * (original copyright message follows):
34 * URB OHCI HCD (Host Controller Driver) for USB on the AU1x00.
36 * (C) Copyright 2003
37 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
39 * [[GNU/GPL disclaimer]]
41 * Note: Part of this code has been derived from linux
44 #include <common.h>
45 #include <asm/io.h>
46 #include <usb.h>
47 #include <malloc.h>
48 #include <linux/list.h>
51 * ISP116x chips require certain delays between accesses to its
52 * registers. The following timing options exist.
54 * 1. Configure your memory controller (the best)
55 * 2. Use ndelay (easiest, poorest). For that, enable the following macro.
57 * Value is in microseconds.
59 #ifdef ISP116X_HCD_USE_UDELAY
60 #define UDELAY 1
61 #endif
64 * On some (slowly?) machines an extra delay after data packing into
65 * controller's FIFOs is required, * otherwise you may get the following
66 * error:
68 * uboot> usb start
69 * (Re)start USB...
70 * USB: scanning bus for devices... isp116x: isp116x_submit_job: CTL:TIMEOUT
71 * isp116x: isp116x_submit_job: ****** FIFO not ready! ******
73 * USB device not responding, giving up (status=4)
74 * isp116x: isp116x_submit_job: ****** FIFO not empty! ******
75 * isp116x: isp116x_submit_job: ****** FIFO not empty! ******
76 * isp116x: isp116x_submit_job: ****** FIFO not empty! ******
77 * 3 USB Device(s) found
78 * scanning bus for storage devices... 0 Storage Device(s) found
80 * Value is in milliseconds.
82 #ifdef ISP116X_HCD_USE_EXTRA_DELAY
83 #define EXTRA_DELAY 2
84 #endif
87 * Enable the following defines if you wish enable debugging messages.
89 #undef DEBUG /* enable debugging messages */
90 #undef TRACE /* enable tracing code */
91 #undef VERBOSE /* verbose debugging messages */
93 #include "isp116x.h"
95 #define DRIVER_VERSION "08 Jan 2007"
96 static const char hcd_name[] = "isp116x-hcd";
98 struct isp116x isp116x_dev;
99 struct isp116x_platform_data isp116x_board;
100 static int got_rhsc; /* root hub status change */
101 struct usb_device *devgone; /* device which was disconnected */
102 static int rh_devnum; /* address of Root Hub endpoint */
104 /* ------------------------------------------------------------------------- */
106 #define ALIGN(x,a) (((x)+(a)-1UL)&~((a)-1UL))
107 #define min_t(type,x,y) \
108 ({ type __x = (x); type __y = (y); __x < __y ? __x : __y; })
110 /* ------------------------------------------------------------------------- */
112 static int isp116x_reset(struct isp116x *isp116x);
114 /* --- Debugging functions ------------------------------------------------- */
116 #define isp116x_show_reg(d, r) { \
117 if ((r) < 0x20) { \
118 DBG("%-12s[%02x]: %08x", #r, \
119 r, isp116x_read_reg32(d, r)); \
120 } else { \
121 DBG("%-12s[%02x]: %04x", #r, \
122 r, isp116x_read_reg16(d, r)); \
126 #define isp116x_show_regs(d) { \
127 isp116x_show_reg(d, HCREVISION); \
128 isp116x_show_reg(d, HCCONTROL); \
129 isp116x_show_reg(d, HCCMDSTAT); \
130 isp116x_show_reg(d, HCINTSTAT); \
131 isp116x_show_reg(d, HCINTENB); \
132 isp116x_show_reg(d, HCFMINTVL); \
133 isp116x_show_reg(d, HCFMREM); \
134 isp116x_show_reg(d, HCFMNUM); \
135 isp116x_show_reg(d, HCLSTHRESH); \
136 isp116x_show_reg(d, HCRHDESCA); \
137 isp116x_show_reg(d, HCRHDESCB); \
138 isp116x_show_reg(d, HCRHSTATUS); \
139 isp116x_show_reg(d, HCRHPORT1); \
140 isp116x_show_reg(d, HCRHPORT2); \
141 isp116x_show_reg(d, HCHWCFG); \
142 isp116x_show_reg(d, HCDMACFG); \
143 isp116x_show_reg(d, HCXFERCTR); \
144 isp116x_show_reg(d, HCuPINT); \
145 isp116x_show_reg(d, HCuPINTENB); \
146 isp116x_show_reg(d, HCCHIPID); \
147 isp116x_show_reg(d, HCSCRATCH); \
148 isp116x_show_reg(d, HCITLBUFLEN); \
149 isp116x_show_reg(d, HCATLBUFLEN); \
150 isp116x_show_reg(d, HCBUFSTAT); \
151 isp116x_show_reg(d, HCRDITL0LEN); \
152 isp116x_show_reg(d, HCRDITL1LEN); \
155 #if defined(TRACE)
157 static int isp116x_get_current_frame_number(struct usb_device *usb_dev)
159 struct isp116x *isp116x = &isp116x_dev;
161 return isp116x_read_reg32(isp116x, HCFMNUM);
164 static void dump_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
165 int len, char *str)
167 #if defined(VERBOSE)
168 int i;
169 #endif
171 DBG("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d stat:%#lx",
172 str,
173 isp116x_get_current_frame_number(dev),
174 usb_pipedevice(pipe),
175 usb_pipeendpoint(pipe),
176 usb_pipeout(pipe) ? 'O' : 'I',
177 usb_pipetype(pipe) < 2 ?
178 (usb_pipeint(pipe) ?
179 "INTR" : "ISOC") :
180 (usb_pipecontrol(pipe) ? "CTRL" : "BULK"), len, dev->status);
181 #if defined(VERBOSE)
182 if (len > 0 && buffer) {
183 printf(__FILE__ ": data(%d):", len);
184 for (i = 0; i < 16 && i < len; i++)
185 printf(" %02x", ((__u8 *) buffer)[i]);
186 printf("%s\n", i < len ? "..." : "");
188 #endif
191 #define PTD_DIR_STR(ptd) ({char __c; \
192 switch(PTD_GET_DIR(ptd)){ \
193 case 0: __c = 's'; break; \
194 case 1: __c = 'o'; break; \
195 default: __c = 'i'; break; \
196 }; __c;})
199 Dump PTD info. The code documents the format
200 perfectly, right :)
202 static inline void dump_ptd(struct ptd *ptd)
204 #if defined(VERBOSE)
205 int k;
206 #endif
208 DBG("PTD(ext) : cc:%x %d%c%d %d,%d,%d t:%x %x%x%x",
209 PTD_GET_CC(ptd),
210 PTD_GET_FA(ptd), PTD_DIR_STR(ptd), PTD_GET_EP(ptd),
211 PTD_GET_COUNT(ptd), PTD_GET_LEN(ptd), PTD_GET_MPS(ptd),
212 PTD_GET_TOGGLE(ptd),
213 PTD_GET_ACTIVE(ptd), PTD_GET_SPD(ptd), PTD_GET_LAST(ptd));
214 #if defined(VERBOSE)
215 printf("isp116x: %s: PTD(byte): ", __FUNCTION__);
216 for (k = 0; k < sizeof(struct ptd); ++k)
217 printf("%02x ", ((u8 *) ptd)[k]);
218 printf("\n");
219 #endif
222 static inline void dump_ptd_data(struct ptd *ptd, u8 * buf, int type)
224 #if defined(VERBOSE)
225 int k;
227 if (type == 0 /* 0ut data */ ) {
228 printf("isp116x: %s: out data: ", __FUNCTION__);
229 for (k = 0; k < PTD_GET_LEN(ptd); ++k)
230 printf("%02x ", ((u8 *) buf)[k]);
231 printf("\n");
233 if (type == 1 /* 1n data */ ) {
234 printf("isp116x: %s: in data: ", __FUNCTION__);
235 for (k = 0; k < PTD_GET_COUNT(ptd); ++k)
236 printf("%02x ", ((u8 *) buf)[k]);
237 printf("\n");
240 if (PTD_GET_LAST(ptd))
241 DBG("--- last PTD ---");
242 #endif
245 #else
247 #define dump_msg(dev, pipe, buffer, len, str) do { } while (0)
248 #define dump_pkt(dev, pipe, buffer, len, setup, str, small) do {} while (0)
250 #define dump_ptd(ptd) do {} while (0)
251 #define dump_ptd_data(ptd, buf, type) do {} while (0)
253 #endif
255 /* --- Virtual Root Hub ---------------------------------------------------- */
257 #include <usbroothubdes.h>
260 * Hub class-specific descriptor is constructed dynamically
263 /* --- Virtual root hub management functions ------------------------------- */
265 static int rh_check_port_status(struct isp116x *isp116x)
267 u32 temp, ndp, i;
268 int res;
270 res = -1;
271 temp = isp116x_read_reg32(isp116x, HCRHSTATUS);
272 ndp = (temp & RH_A_NDP);
273 for (i = 0; i < ndp; i++) {
274 temp = isp116x_read_reg32(isp116x, HCRHPORT1 + i);
275 /* check for a device disconnect */
276 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
277 (RH_PS_PESC | RH_PS_CSC)) && ((temp & RH_PS_CCS) == 0)) {
278 res = i;
279 break;
282 return res;
285 /* --- HC management functions --------------------------------------------- */
287 /* Write len bytes to fifo, pad till 32-bit boundary
289 static void write_ptddata_to_fifo(struct isp116x *isp116x, void *buf, int len)
291 u8 *dp = (u8 *) buf;
292 u16 *dp2 = (u16 *) buf;
293 u16 w;
294 int quot = len % 4;
296 if ((unsigned long)dp2 & 1) {
297 /* not aligned */
298 for (; len > 1; len -= 2) {
299 w = *dp++;
300 w |= *dp++ << 8;
301 isp116x_raw_write_data16(isp116x, w);
303 if (len)
304 isp116x_write_data16(isp116x, (u16) * dp);
305 } else {
306 /* aligned */
307 for (; len > 1; len -= 2)
308 isp116x_raw_write_data16(isp116x, *dp2++);
309 if (len)
310 isp116x_write_data16(isp116x, 0xff & *((u8 *) dp2));
312 if (quot == 1 || quot == 2)
313 isp116x_raw_write_data16(isp116x, 0);
316 /* Read len bytes from fifo and then read till 32-bit boundary
318 static void read_ptddata_from_fifo(struct isp116x *isp116x, void *buf, int len)
320 u8 *dp = (u8 *) buf;
321 u16 *dp2 = (u16 *) buf;
322 u16 w;
323 int quot = len % 4;
325 if ((unsigned long)dp2 & 1) {
326 /* not aligned */
327 for (; len > 1; len -= 2) {
328 w = isp116x_raw_read_data16(isp116x);
329 *dp++ = w & 0xff;
330 *dp++ = (w >> 8) & 0xff;
332 if (len)
333 *dp = 0xff & isp116x_read_data16(isp116x);
334 } else {
335 /* aligned */
336 for (; len > 1; len -= 2)
337 *dp2++ = isp116x_raw_read_data16(isp116x);
338 if (len)
339 *(u8 *) dp2 = 0xff & isp116x_read_data16(isp116x);
341 if (quot == 1 || quot == 2)
342 isp116x_raw_read_data16(isp116x);
345 /* Write PTD's and data for scheduled transfers into the fifo ram.
346 * Fifo must be empty and ready */
347 static void pack_fifo(struct isp116x *isp116x, struct usb_device *dev,
348 unsigned long pipe, struct ptd *ptd, int n, void *data,
349 int len)
351 int buflen = n * sizeof(struct ptd) + len;
352 int i, done;
354 DBG("--- pack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
356 isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
357 isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
358 isp116x_write_addr(isp116x, HCATLPORT | ISP116x_WRITE_OFFSET);
360 done = 0;
361 for (i = 0; i < n; i++) {
362 DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
364 dump_ptd(&ptd[i]);
365 isp116x_write_data16(isp116x, ptd[i].count);
366 isp116x_write_data16(isp116x, ptd[i].mps);
367 isp116x_write_data16(isp116x, ptd[i].len);
368 isp116x_write_data16(isp116x, ptd[i].faddr);
370 dump_ptd_data(&ptd[i], (__u8 *) data + done, 0);
371 write_ptddata_to_fifo(isp116x,
372 (__u8 *) data + done,
373 PTD_GET_LEN(&ptd[i]));
375 done += PTD_GET_LEN(&ptd[i]);
379 /* Read the processed PTD's and data from fifo ram back to URBs' buffers.
380 * Fifo must be full and done */
381 static int unpack_fifo(struct isp116x *isp116x, struct usb_device *dev,
382 unsigned long pipe, struct ptd *ptd, int n, void *data,
383 int len)
385 int buflen = n * sizeof(struct ptd) + len;
386 int i, done, cc, ret;
388 isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
389 isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
390 isp116x_write_addr(isp116x, HCATLPORT);
392 ret = TD_CC_NOERROR;
393 done = 0;
394 for (i = 0; i < n; i++) {
395 DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
397 ptd[i].count = isp116x_read_data16(isp116x);
398 ptd[i].mps = isp116x_read_data16(isp116x);
399 ptd[i].len = isp116x_read_data16(isp116x);
400 ptd[i].faddr = isp116x_read_data16(isp116x);
401 dump_ptd(&ptd[i]);
403 read_ptddata_from_fifo(isp116x,
404 (__u8 *) data + done,
405 PTD_GET_LEN(&ptd[i]));
406 dump_ptd_data(&ptd[i], (__u8 *) data + done, 1);
408 done += PTD_GET_LEN(&ptd[i]);
410 cc = PTD_GET_CC(&ptd[i]);
412 /* Data underrun means basically that we had more buffer space than
413 * the function had data. It is perfectly normal but upper levels have
414 * to know how much we actually transferred.
416 if (cc == TD_NOTACCESSED ||
417 (cc != TD_CC_NOERROR && (ret == TD_CC_NOERROR || ret == TD_DATAUNDERRUN)))
418 ret = cc;
421 DBG("--- unpack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
423 return ret;
426 /* Interrupt handling
428 static int isp116x_interrupt(struct isp116x *isp116x)
430 u16 irqstat;
431 u32 intstat;
432 int ret = 0;
434 isp116x_write_reg16(isp116x, HCuPINTENB, 0);
435 irqstat = isp116x_read_reg16(isp116x, HCuPINT);
436 isp116x_write_reg16(isp116x, HCuPINT, irqstat);
437 DBG(">>>>>> irqstat %x <<<<<<", irqstat);
439 if (irqstat & HCuPINT_ATL) {
440 DBG(">>>>>> HCuPINT_ATL <<<<<<");
441 udelay(500);
442 ret = 1;
445 if (irqstat & HCuPINT_OPR) {
446 intstat = isp116x_read_reg32(isp116x, HCINTSTAT);
447 isp116x_write_reg32(isp116x, HCINTSTAT, intstat);
448 DBG(">>>>>> HCuPINT_OPR %x <<<<<<", intstat);
450 if (intstat & HCINT_UE) {
451 ERR("unrecoverable error, controller disabled");
453 /* FIXME: be optimistic, hope that bug won't repeat
454 * often. Make some non-interrupt context restart the
455 * controller. Count and limit the retries though;
456 * either hardware or software errors can go forever...
458 isp116x_reset(isp116x);
459 ret = -1;
460 return -1;
463 if (intstat & HCINT_RHSC) {
464 got_rhsc = 1;
465 ret = 1;
466 /* When root hub or any of its ports is going
467 to come out of suspend, it may take more
468 than 10ms for status bits to stabilize. */
469 mdelay(20);
472 if (intstat & HCINT_SO) {
473 ERR("schedule overrun");
474 ret = -1;
477 irqstat &= ~HCuPINT_OPR;
480 return ret;
483 /* With one PTD we can transfer almost 1K in one go;
484 * HC does the splitting into endpoint digestible transactions
486 struct ptd ptd[1];
488 static inline int max_transfer_len(struct usb_device *dev, unsigned long pipe)
490 unsigned mpck = usb_maxpacket(dev, pipe);
492 /* One PTD can transfer 1023 bytes but try to always
493 * transfer multiples of endpoint buffer size
495 return 1023 / mpck * mpck;
498 /* Do an USB transfer
500 static int isp116x_submit_job(struct usb_device *dev, unsigned long pipe,
501 int dir, void *buffer, int len)
503 struct isp116x *isp116x = &isp116x_dev;
504 int type = usb_pipetype(pipe);
505 int epnum = usb_pipeendpoint(pipe);
506 int max = usb_maxpacket(dev, pipe);
507 int dir_out = usb_pipeout(pipe);
508 int speed_low = (dev->speed == USB_SPEED_LOW);
509 int i, done = 0, stat, timeout, cc;
511 /* 500 frames or 0.5s timeout when function is busy and NAKs transactions for a while */
512 int retries = 500;
514 DBG("------------------------------------------------");
515 dump_msg(dev, pipe, buffer, len, "SUBMIT");
516 DBG("------------------------------------------------");
518 if (len >= 1024) {
519 ERR("Too big job");
520 dev->status = USB_ST_CRC_ERR;
521 return -1;
524 if (isp116x->disabled) {
525 ERR("EPIPE");
526 dev->status = USB_ST_CRC_ERR;
527 return -1;
530 /* device pulled? Shortcut the action. */
531 if (devgone == dev) {
532 ERR("ENODEV");
533 dev->status = USB_ST_CRC_ERR;
534 return USB_ST_CRC_ERR;
537 if (!max) {
538 ERR("pipesize for pipe %lx is zero", pipe);
539 dev->status = USB_ST_CRC_ERR;
540 return -1;
543 if (type == PIPE_ISOCHRONOUS) {
544 ERR("isochronous transfers not supported");
545 dev->status = USB_ST_CRC_ERR;
546 return -1;
549 /* FIFO not empty? */
550 if (isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_FULL) {
551 ERR("****** FIFO not empty! ******");
552 dev->status = USB_ST_BUF_ERR;
553 return -1;
556 retry:
557 isp116x_write_reg32(isp116x, HCINTSTAT, 0xff);
559 /* Prepare the PTD data */
560 ptd->count = PTD_CC_MSK | PTD_ACTIVE_MSK |
561 PTD_TOGGLE(usb_gettoggle(dev, epnum, dir_out));
562 ptd->mps = PTD_MPS(max) | PTD_SPD(speed_low) | PTD_EP(epnum) | PTD_LAST_MSK;
563 ptd->len = PTD_LEN(len) | PTD_DIR(dir);
564 ptd->faddr = PTD_FA(usb_pipedevice(pipe));
566 retry_same:
567 /* Pack data into FIFO ram */
568 pack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
569 #ifdef EXTRA_DELAY
570 mdelay(EXTRA_DELAY);
571 #endif
573 /* Start the data transfer */
575 /* Allow more time for a BULK device to react - some are slow */
576 if (usb_pipebulk(pipe))
577 timeout = 5000;
578 else
579 timeout = 100;
581 /* Wait for it to complete */
582 for (;;) {
583 /* Check whether the controller is done */
584 stat = isp116x_interrupt(isp116x);
586 if (stat < 0) {
587 dev->status = USB_ST_CRC_ERR;
588 break;
590 if (stat > 0)
591 break;
593 /* Check the timeout */
594 if (--timeout)
595 udelay(1);
596 else {
597 ERR("CTL:TIMEOUT ");
598 stat = USB_ST_CRC_ERR;
599 break;
603 /* We got an Root Hub Status Change interrupt */
604 if (got_rhsc) {
605 isp116x_show_regs(isp116x);
607 got_rhsc = 0;
609 /* Abuse timeout */
610 timeout = rh_check_port_status(isp116x);
611 if (timeout >= 0) {
613 * FIXME! NOTE! AAAARGH!
614 * This is potentially dangerous because it assumes
615 * that only one device is ever plugged in!
617 devgone = dev;
621 /* Ok, now we can read transfer status */
623 /* FIFO not ready? */
624 if (!(isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_DONE)) {
625 ERR("****** FIFO not ready! ******");
626 dev->status = USB_ST_BUF_ERR;
627 return -1;
630 /* Unpack data from FIFO ram */
631 cc = unpack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
633 i = PTD_GET_COUNT(ptd);
634 done += i;
635 buffer += i;
636 len -= i;
638 /* There was some kind of real problem; Prepare the PTD again
639 * and retry from the failed transaction on
641 if (cc && cc != TD_NOTACCESSED && cc != TD_DATAUNDERRUN) {
642 if (retries >= 100) {
643 retries -= 100;
644 /* The chip will have toggled the toggle bit for the failed
645 * transaction too. We have to toggle it back.
647 usb_settoggle(dev, epnum, dir_out, !PTD_GET_TOGGLE(ptd));
648 goto retry;
651 /* "Normal" errors; TD_NOTACCESSED would mean in effect that the function have NAKed
652 * the transactions from the first on for the whole frame. It may be busy and we retry
653 * with the same PTD. PTD_ACTIVE (and not TD_NOTACCESSED) would mean that some of the
654 * PTD didn't make it because the function was busy or the frame ended before the PTD
655 * finished. We prepare the rest of the data and try again.
657 else if (cc == TD_NOTACCESSED || PTD_GET_ACTIVE(ptd) || (cc != TD_DATAUNDERRUN && PTD_GET_COUNT(ptd) < PTD_GET_LEN(ptd))) {
658 if (retries) {
659 --retries;
660 if (cc == TD_NOTACCESSED && PTD_GET_ACTIVE(ptd) && !PTD_GET_COUNT(ptd)) goto retry_same;
661 usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
662 goto retry;
666 if (cc != TD_CC_NOERROR && cc != TD_DATAUNDERRUN) {
667 DBG("****** completition code error %x ******", cc);
668 switch (cc) {
669 case TD_CC_BITSTUFFING:
670 dev->status = USB_ST_BIT_ERR;
671 break;
672 case TD_CC_STALL:
673 dev->status = USB_ST_STALLED;
674 break;
675 case TD_BUFFEROVERRUN:
676 case TD_BUFFERUNDERRUN:
677 dev->status = USB_ST_BUF_ERR;
678 break;
679 default:
680 dev->status = USB_ST_CRC_ERR;
682 return -cc;
684 else usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
686 dump_msg(dev, pipe, buffer, len, "SUBMIT(ret)");
688 dev->status = 0;
689 return done;
692 /* Adapted from au1x00_usb_ohci.c
694 static int isp116x_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
695 void *buffer, int transfer_len,
696 struct devrequest *cmd)
698 struct isp116x *isp116x = &isp116x_dev;
699 u32 tmp = 0;
701 int leni = transfer_len;
702 int len = 0;
703 int stat = 0;
704 u32 datab[4];
705 u8 *data_buf = (u8 *) datab;
706 u16 bmRType_bReq;
707 u16 wValue;
708 u16 wIndex;
709 u16 wLength;
711 if (usb_pipeint(pipe)) {
712 INFO("Root-Hub submit IRQ: NOT implemented");
713 return 0;
716 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
717 wValue = swap_16(cmd->value);
718 wIndex = swap_16(cmd->index);
719 wLength = swap_16(cmd->length);
721 DBG("--- HUB ----------------------------------------");
722 DBG("submit rh urb, req=%x val=%#x index=%#x len=%d",
723 bmRType_bReq, wValue, wIndex, wLength);
724 dump_msg(dev, pipe, buffer, transfer_len, "RH");
725 DBG("------------------------------------------------");
727 switch (bmRType_bReq) {
728 case RH_GET_STATUS:
729 DBG("RH_GET_STATUS");
731 *(__u16 *) data_buf = swap_16(1);
732 len = 2;
733 break;
735 case RH_GET_STATUS | RH_INTERFACE:
736 DBG("RH_GET_STATUS | RH_INTERFACE");
738 *(__u16 *) data_buf = swap_16(0);
739 len = 2;
740 break;
742 case RH_GET_STATUS | RH_ENDPOINT:
743 DBG("RH_GET_STATUS | RH_ENDPOINT");
745 *(__u16 *) data_buf = swap_16(0);
746 len = 2;
747 break;
749 case RH_GET_STATUS | RH_CLASS:
750 DBG("RH_GET_STATUS | RH_CLASS");
752 tmp = isp116x_read_reg32(isp116x, HCRHSTATUS);
754 *(__u32 *) data_buf = swap_32(tmp & ~(RH_HS_CRWE | RH_HS_DRWE));
755 len = 4;
756 break;
758 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
759 DBG("RH_GET_STATUS | RH_OTHER | RH_CLASS");
761 tmp = isp116x_read_reg32(isp116x, HCRHPORT1 + wIndex - 1);
762 *(__u32 *) data_buf = swap_32(tmp);
763 isp116x_show_regs(isp116x);
764 len = 4;
765 break;
767 case RH_CLEAR_FEATURE | RH_ENDPOINT:
768 DBG("RH_CLEAR_FEATURE | RH_ENDPOINT");
770 switch (wValue) {
771 case RH_ENDPOINT_STALL:
772 DBG("C_HUB_ENDPOINT_STALL");
773 len = 0;
774 break;
776 break;
778 case RH_CLEAR_FEATURE | RH_CLASS:
779 DBG("RH_CLEAR_FEATURE | RH_CLASS");
781 switch (wValue) {
782 case RH_C_HUB_LOCAL_POWER:
783 DBG("C_HUB_LOCAL_POWER");
784 len = 0;
785 break;
787 case RH_C_HUB_OVER_CURRENT:
788 DBG("C_HUB_OVER_CURRENT");
789 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_OCIC);
790 len = 0;
791 break;
793 break;
795 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
796 DBG("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS");
798 switch (wValue) {
799 case RH_PORT_ENABLE:
800 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
801 RH_PS_CCS);
802 len = 0;
803 break;
805 case RH_PORT_SUSPEND:
806 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
807 RH_PS_POCI);
808 len = 0;
809 break;
811 case RH_PORT_POWER:
812 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
813 RH_PS_LSDA);
814 len = 0;
815 break;
817 case RH_C_PORT_CONNECTION:
818 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
819 RH_PS_CSC);
820 len = 0;
821 break;
823 case RH_C_PORT_ENABLE:
824 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
825 RH_PS_PESC);
826 len = 0;
827 break;
829 case RH_C_PORT_SUSPEND:
830 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
831 RH_PS_PSSC);
832 len = 0;
833 break;
835 case RH_C_PORT_OVER_CURRENT:
836 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
837 RH_PS_POCI);
838 len = 0;
839 break;
841 case RH_C_PORT_RESET:
842 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
843 RH_PS_PRSC);
844 len = 0;
845 break;
847 default:
848 ERR("invalid wValue");
849 stat = USB_ST_STALLED;
852 isp116x_show_regs(isp116x);
854 break;
856 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
857 DBG("RH_SET_FEATURE | RH_OTHER | RH_CLASS");
859 switch (wValue) {
860 case RH_PORT_SUSPEND:
861 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
862 RH_PS_PSS);
863 len = 0;
864 break;
866 case RH_PORT_RESET:
867 /* Spin until any current reset finishes */
868 while (1) {
869 tmp =
870 isp116x_read_reg32(isp116x,
871 HCRHPORT1 + wIndex - 1);
872 if (!(tmp & RH_PS_PRS))
873 break;
874 mdelay(1);
876 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
877 RH_PS_PRS);
878 mdelay(10);
880 len = 0;
881 break;
883 case RH_PORT_POWER:
884 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
885 RH_PS_PPS);
886 len = 0;
887 break;
889 case RH_PORT_ENABLE:
890 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
891 RH_PS_PES);
892 len = 0;
893 break;
895 default:
896 ERR("invalid wValue");
897 stat = USB_ST_STALLED;
900 isp116x_show_regs(isp116x);
902 break;
904 case RH_SET_ADDRESS:
905 DBG("RH_SET_ADDRESS");
907 rh_devnum = wValue;
908 len = 0;
909 break;
911 case RH_GET_DESCRIPTOR:
912 DBG("RH_GET_DESCRIPTOR: %x, %d", wValue, wLength);
914 switch (wValue) {
915 case (USB_DT_DEVICE << 8): /* device descriptor */
916 len = min_t(unsigned int,
917 leni, min_t(unsigned int,
918 sizeof(root_hub_dev_des),
919 wLength));
920 data_buf = root_hub_dev_des;
921 break;
923 case (USB_DT_CONFIG << 8): /* configuration descriptor */
924 len = min_t(unsigned int,
925 leni, min_t(unsigned int,
926 sizeof(root_hub_config_des),
927 wLength));
928 data_buf = root_hub_config_des;
929 break;
931 case ((USB_DT_STRING << 8) | 0x00): /* string 0 descriptors */
932 len = min_t(unsigned int,
933 leni, min_t(unsigned int,
934 sizeof(root_hub_str_index0),
935 wLength));
936 data_buf = root_hub_str_index0;
937 break;
939 case ((USB_DT_STRING << 8) | 0x01): /* string 1 descriptors */
940 len = min_t(unsigned int,
941 leni, min_t(unsigned int,
942 sizeof(root_hub_str_index1),
943 wLength));
944 data_buf = root_hub_str_index1;
945 break;
947 default:
948 ERR("invalid wValue");
949 stat = USB_ST_STALLED;
952 break;
954 case RH_GET_DESCRIPTOR | RH_CLASS:
955 DBG("RH_GET_DESCRIPTOR | RH_CLASS");
957 tmp = isp116x_read_reg32(isp116x, HCRHDESCA);
959 data_buf[0] = 0x09; /* min length; */
960 data_buf[1] = 0x29;
961 data_buf[2] = tmp & RH_A_NDP;
962 data_buf[3] = 0;
963 if (tmp & RH_A_PSM) /* per-port power switching? */
964 data_buf[3] |= 0x01;
965 if (tmp & RH_A_NOCP) /* no overcurrent reporting? */
966 data_buf[3] |= 0x10;
967 else if (tmp & RH_A_OCPM) /* per-port overcurrent rep? */
968 data_buf[3] |= 0x08;
970 /* Corresponds to data_buf[4-7] */
971 datab[1] = 0;
972 data_buf[5] = (tmp & RH_A_POTPGT) >> 24;
974 tmp = isp116x_read_reg32(isp116x, HCRHDESCB);
976 data_buf[7] = tmp & RH_B_DR;
977 if (data_buf[2] < 7)
978 data_buf[8] = 0xff;
979 else {
980 data_buf[0] += 2;
981 data_buf[8] = (tmp & RH_B_DR) >> 8;
982 data_buf[10] = data_buf[9] = 0xff;
985 len = min_t(unsigned int, leni,
986 min_t(unsigned int, data_buf[0], wLength));
987 break;
989 case RH_GET_CONFIGURATION:
990 DBG("RH_GET_CONFIGURATION");
992 *(__u8 *) data_buf = 0x01;
993 len = 1;
994 break;
996 case RH_SET_CONFIGURATION:
997 DBG("RH_SET_CONFIGURATION");
999 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPSC);
1000 len = 0;
1001 break;
1003 default:
1004 ERR("*** *** *** unsupported root hub command *** *** ***");
1005 stat = USB_ST_STALLED;
1008 len = min_t(int, len, leni);
1009 if (buffer != data_buf)
1010 memcpy(buffer, data_buf, len);
1012 dev->act_len = len;
1013 dev->status = stat;
1014 DBG("dev act_len %d, status %d", dev->act_len, dev->status);
1016 dump_msg(dev, pipe, buffer, transfer_len, "RH(ret)");
1018 return stat;
1021 /* --- Transfer functions -------------------------------------------------- */
1023 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1024 int len, int interval)
1026 DBG("dev=%p pipe=%#lx buf=%p size=%d int=%d",
1027 dev, pipe, buffer, len, interval);
1029 return -1;
1032 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1033 int len, struct devrequest *setup)
1035 int devnum = usb_pipedevice(pipe);
1036 int epnum = usb_pipeendpoint(pipe);
1037 int max = max_transfer_len(dev, pipe);
1038 int dir_in = usb_pipein(pipe);
1039 int done, ret;
1041 /* Control message is for the HUB? */
1042 if (devnum == rh_devnum)
1043 return isp116x_submit_rh_msg(dev, pipe, buffer, len, setup);
1045 /* Ok, no HUB message so send the message to the device */
1047 /* Setup phase */
1048 DBG("--- SETUP PHASE --------------------------------");
1049 usb_settoggle(dev, epnum, 1, 0);
1050 ret = isp116x_submit_job(dev, pipe,
1051 PTD_DIR_SETUP,
1052 setup, sizeof(struct devrequest));
1053 if (ret < 0) {
1054 DBG("control setup phase error (ret = %d", ret);
1055 return -1;
1058 /* Data phase */
1059 DBG("--- DATA PHASE ---------------------------------");
1060 done = 0;
1061 usb_settoggle(dev, epnum, !dir_in, 1);
1062 while (done < len) {
1063 ret = isp116x_submit_job(dev, pipe,
1064 dir_in ? PTD_DIR_IN : PTD_DIR_OUT,
1065 (__u8 *) buffer + done,
1066 max > len - done ? len - done : max);
1067 if (ret < 0) {
1068 DBG("control data phase error (ret = %d)", ret);
1069 return -1;
1071 done += ret;
1073 if (dir_in && ret < max) /* short packet */
1074 break;
1077 /* Status phase */
1078 DBG("--- STATUS PHASE -------------------------------");
1079 usb_settoggle(dev, epnum, !dir_in, 1);
1080 ret = isp116x_submit_job(dev, pipe,
1081 !dir_in ? PTD_DIR_IN : PTD_DIR_OUT, NULL, 0);
1082 if (ret < 0) {
1083 DBG("control status phase error (ret = %d", ret);
1084 return -1;
1087 dev->act_len = done;
1089 dump_msg(dev, pipe, buffer, len, "DEV(ret)");
1091 return done;
1094 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1095 int len)
1097 int dir_out = usb_pipeout(pipe);
1098 int max = max_transfer_len(dev, pipe);
1099 int done, ret;
1101 DBG("--- BULK ---------------------------------------");
1102 DBG("dev=%ld pipe=%ld buf=%p size=%d dir_out=%d",
1103 usb_pipedevice(pipe), usb_pipeendpoint(pipe), buffer, len, dir_out);
1105 done = 0;
1106 while (done < len) {
1107 ret = isp116x_submit_job(dev, pipe,
1108 !dir_out ? PTD_DIR_IN : PTD_DIR_OUT,
1109 (__u8 *) buffer + done,
1110 max > len - done ? len - done : max);
1111 if (ret < 0) {
1112 DBG("error on bulk message (ret = %d)", ret);
1113 return -1;
1116 done += ret;
1118 if (!dir_out && ret < max) /* short packet */
1119 break;
1122 dev->act_len = done;
1124 return 0;
1127 /* --- Basic functions ----------------------------------------------------- */
1129 static int isp116x_sw_reset(struct isp116x *isp116x)
1131 int retries = 15;
1132 int ret = 0;
1134 DBG("");
1136 isp116x->disabled = 1;
1138 isp116x_write_reg16(isp116x, HCSWRES, HCSWRES_MAGIC);
1139 isp116x_write_reg32(isp116x, HCCMDSTAT, HCCMDSTAT_HCR);
1140 while (--retries) {
1141 /* It usually resets within 1 ms */
1142 mdelay(1);
1143 if (!(isp116x_read_reg32(isp116x, HCCMDSTAT) & HCCMDSTAT_HCR))
1144 break;
1146 if (!retries) {
1147 ERR("software reset timeout");
1148 ret = -1;
1150 return ret;
1153 static int isp116x_reset(struct isp116x *isp116x)
1155 unsigned long t;
1156 u16 clkrdy = 0;
1157 int ret, timeout = 15 /* ms */ ;
1159 DBG("");
1161 ret = isp116x_sw_reset(isp116x);
1162 if (ret)
1163 return ret;
1165 for (t = 0; t < timeout; t++) {
1166 clkrdy = isp116x_read_reg16(isp116x, HCuPINT) & HCuPINT_CLKRDY;
1167 if (clkrdy)
1168 break;
1169 mdelay(1);
1171 if (!clkrdy) {
1172 ERR("clock not ready after %dms", timeout);
1173 /* After sw_reset the clock won't report to be ready, if
1174 H_WAKEUP pin is high. */
1175 ERR("please make sure that the H_WAKEUP pin is pulled low!");
1176 ret = -1;
1178 return ret;
1181 static void isp116x_stop(struct isp116x *isp116x)
1183 u32 val;
1185 DBG("");
1187 isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1189 /* Switch off ports' power, some devices don't come up
1190 after next 'start' without this */
1191 val = isp116x_read_reg32(isp116x, HCRHDESCA);
1192 val &= ~(RH_A_NPS | RH_A_PSM);
1193 isp116x_write_reg32(isp116x, HCRHDESCA, val);
1194 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPS);
1196 isp116x_sw_reset(isp116x);
1200 * Configure the chip. The chip must be successfully reset by now.
1202 static int isp116x_start(struct isp116x *isp116x)
1204 struct isp116x_platform_data *board = isp116x->board;
1205 u32 val;
1207 DBG("");
1209 /* Clear interrupt status and disable all interrupt sources */
1210 isp116x_write_reg16(isp116x, HCuPINT, 0xff);
1211 isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1213 isp116x_write_reg16(isp116x, HCITLBUFLEN, ISP116x_ITL_BUFSIZE);
1214 isp116x_write_reg16(isp116x, HCATLBUFLEN, ISP116x_ATL_BUFSIZE);
1216 /* Hardware configuration */
1217 val = HCHWCFG_DBWIDTH(1);
1218 if (board->sel15Kres)
1219 val |= HCHWCFG_15KRSEL;
1220 /* Remote wakeup won't work without working clock */
1221 if (board->remote_wakeup_enable)
1222 val |= HCHWCFG_CLKNOTSTOP;
1223 if (board->oc_enable)
1224 val |= HCHWCFG_ANALOG_OC;
1225 isp116x_write_reg16(isp116x, HCHWCFG, val);
1227 /* --- Root hub configuration */
1228 val = (25 << 24) & RH_A_POTPGT;
1229 /* AN10003_1.pdf recommends RH_A_NPS (no power switching) to
1230 be always set. Yet, instead, we request individual port
1231 power switching. */
1232 val |= RH_A_PSM;
1233 /* Report overcurrent per port */
1234 val |= RH_A_OCPM;
1235 isp116x_write_reg32(isp116x, HCRHDESCA, val);
1236 isp116x->rhdesca = isp116x_read_reg32(isp116x, HCRHDESCA);
1238 val = RH_B_PPCM;
1239 isp116x_write_reg32(isp116x, HCRHDESCB, val);
1240 isp116x->rhdescb = isp116x_read_reg32(isp116x, HCRHDESCB);
1242 val = 0;
1243 if (board->remote_wakeup_enable)
1244 val |= RH_HS_DRWE;
1245 isp116x_write_reg32(isp116x, HCRHSTATUS, val);
1246 isp116x->rhstatus = isp116x_read_reg32(isp116x, HCRHSTATUS);
1248 isp116x_write_reg32(isp116x, HCFMINTVL, 0x27782edf);
1250 /* Go operational */
1251 val = HCCONTROL_USB_OPER;
1252 if (board->remote_wakeup_enable)
1253 val |= HCCONTROL_RWE;
1254 isp116x_write_reg32(isp116x, HCCONTROL, val);
1256 /* Disable ports to avoid race in device enumeration */
1257 isp116x_write_reg32(isp116x, HCRHPORT1, RH_PS_CCS);
1258 isp116x_write_reg32(isp116x, HCRHPORT2, RH_PS_CCS);
1260 isp116x_show_regs(isp116x);
1262 isp116x->disabled = 0;
1264 return 0;
1267 /* --- Init functions ------------------------------------------------------ */
1269 int isp116x_check_id(struct isp116x *isp116x)
1271 int val;
1273 val = isp116x_read_reg16(isp116x, HCCHIPID);
1274 if ((val & HCCHIPID_MASK) != HCCHIPID_MAGIC) {
1275 ERR("invalid chip ID %04x", val);
1276 return -1;
1279 return 0;
1282 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller))
1284 struct isp116x *isp116x = &isp116x_dev;
1286 DBG("");
1288 got_rhsc = rh_devnum = 0;
1290 /* Init device registers addr */
1291 isp116x->addr_reg = (u16 *) ISP116X_HCD_ADDR;
1292 isp116x->data_reg = (u16 *) ISP116X_HCD_DATA;
1294 /* Setup specific board settings */
1295 #ifdef ISP116X_HCD_SEL15kRES
1296 isp116x_board.sel15Kres = 1;
1297 #endif
1298 #ifdef ISP116X_HCD_OC_ENABLE
1299 isp116x_board.oc_enable = 1;
1300 #endif
1301 #ifdef ISP116X_HCD_REMOTE_WAKEUP_ENABLE
1302 isp116x_board.remote_wakeup_enable = 1;
1303 #endif
1304 isp116x->board = &isp116x_board;
1306 /* Try to get ISP116x silicon chip ID */
1307 if (isp116x_check_id(isp116x) < 0)
1308 return -1;
1310 isp116x->disabled = 1;
1311 isp116x->sleeping = 0;
1313 isp116x_reset(isp116x);
1314 isp116x_start(isp116x);
1316 return 0;
1319 int usb_lowlevel_stop(int index)
1321 struct isp116x *isp116x = &isp116x_dev;
1323 DBG("");
1325 if (!isp116x->disabled)
1326 isp116x_stop(isp116x);
1328 return 0;