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):
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.
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.
37 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
39 * [[GNU/GPL disclaimer]]
41 * Note: Part of this code has been derived from linux
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
64 * On some (slowly?) machines an extra delay after data packing into
65 * controller's FIFOs is required, * otherwise you may get the following
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
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 */
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) { \
118 DBG("%-12s[%02x]: %08x", #r, \
119 r, isp116x_read_reg32(d, r)); \
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); \
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,
171 DBG("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d stat:%#lx",
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 ?
180 (usb_pipecontrol(pipe) ? "CTRL" : "BULK"), len, dev->status);
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 ? "..." : "");
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; \
199 Dump PTD info. The code documents the format
202 static inline void dump_ptd(struct ptd *ptd)
208 DBG("PTD(ext) : cc:%x %d%c%d %d,%d,%d t:%x %x%x%x",
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),
213 PTD_GET_ACTIVE(ptd), PTD_GET_SPD(ptd), PTD_GET_LAST(ptd));
215 printf("isp116x: %s: PTD(byte): ", __FUNCTION__);
216 for (k = 0; k < sizeof(struct ptd); ++k)
217 printf("%02x ", ((u8 *) ptd)[k]);
222 static inline void dump_ptd_data(struct ptd *ptd, u8 * buf, int type)
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]);
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]);
240 if (PTD_GET_LAST(ptd))
241 DBG("--- last PTD ---");
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)
255 /* --- Virtual Root Hub ---------------------------------------------------- */
257 /* Device descriptor */
258 static __u8 root_hub_dev_des[] = {
259 0x12, /* __u8 bLength; */
260 0x01, /* __u8 bDescriptorType; Device */
261 0x10, /* __u16 bcdUSB; v1.1 */
263 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
264 0x00, /* __u8 bDeviceSubClass; */
265 0x00, /* __u8 bDeviceProtocol; */
266 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
267 0x00, /* __u16 idVendor; */
269 0x00, /* __u16 idProduct; */
271 0x00, /* __u16 bcdDevice; */
273 0x00, /* __u8 iManufacturer; */
274 0x01, /* __u8 iProduct; */
275 0x00, /* __u8 iSerialNumber; */
276 0x01 /* __u8 bNumConfigurations; */
279 /* Configuration descriptor */
280 static __u8 root_hub_config_des[] = {
281 0x09, /* __u8 bLength; */
282 0x02, /* __u8 bDescriptorType; Configuration */
283 0x19, /* __u16 wTotalLength; */
285 0x01, /* __u8 bNumInterfaces; */
286 0x01, /* __u8 bConfigurationValue; */
287 0x00, /* __u8 iConfiguration; */
288 0x40, /* __u8 bmAttributes;
289 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
290 0x00, /* __u8 MaxPower; */
293 0x09, /* __u8 if_bLength; */
294 0x04, /* __u8 if_bDescriptorType; Interface */
295 0x00, /* __u8 if_bInterfaceNumber; */
296 0x00, /* __u8 if_bAlternateSetting; */
297 0x01, /* __u8 if_bNumEndpoints; */
298 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
299 0x00, /* __u8 if_bInterfaceSubClass; */
300 0x00, /* __u8 if_bInterfaceProtocol; */
301 0x00, /* __u8 if_iInterface; */
304 0x07, /* __u8 ep_bLength; */
305 0x05, /* __u8 ep_bDescriptorType; Endpoint */
306 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
307 0x03, /* __u8 ep_bmAttributes; Interrupt */
308 0x00, /* __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
310 0xff /* __u8 ep_bInterval; 255 ms */
313 static unsigned char root_hub_str_index0[] = {
314 0x04, /* __u8 bLength; */
315 0x03, /* __u8 bDescriptorType; String-descriptor */
316 0x09, /* __u8 lang ID */
317 0x04, /* __u8 lang ID */
320 static unsigned char root_hub_str_index1[] = {
321 0x22, /* __u8 bLength; */
322 0x03, /* __u8 bDescriptorType; String-descriptor */
323 'I', /* __u8 Unicode */
324 0, /* __u8 Unicode */
325 'S', /* __u8 Unicode */
326 0, /* __u8 Unicode */
327 'P', /* __u8 Unicode */
328 0, /* __u8 Unicode */
329 '1', /* __u8 Unicode */
330 0, /* __u8 Unicode */
331 '1', /* __u8 Unicode */
332 0, /* __u8 Unicode */
333 '6', /* __u8 Unicode */
334 0, /* __u8 Unicode */
335 'x', /* __u8 Unicode */
336 0, /* __u8 Unicode */
337 ' ', /* __u8 Unicode */
338 0, /* __u8 Unicode */
339 'R', /* __u8 Unicode */
340 0, /* __u8 Unicode */
341 'o', /* __u8 Unicode */
342 0, /* __u8 Unicode */
343 'o', /* __u8 Unicode */
344 0, /* __u8 Unicode */
345 't', /* __u8 Unicode */
346 0, /* __u8 Unicode */
347 ' ', /* __u8 Unicode */
348 0, /* __u8 Unicode */
349 'H', /* __u8 Unicode */
350 0, /* __u8 Unicode */
351 'u', /* __u8 Unicode */
352 0, /* __u8 Unicode */
353 'b', /* __u8 Unicode */
354 0, /* __u8 Unicode */
358 * Hub class-specific descriptor is constructed dynamically
361 /* --- Virtual root hub management functions ------------------------------- */
363 static int rh_check_port_status(struct isp116x *isp116x)
369 temp = isp116x_read_reg32(isp116x, HCRHSTATUS);
370 ndp = (temp & RH_A_NDP);
371 for (i = 0; i < ndp; i++) {
372 temp = isp116x_read_reg32(isp116x, HCRHPORT1 + i);
373 /* check for a device disconnect */
374 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
375 (RH_PS_PESC | RH_PS_CSC)) && ((temp & RH_PS_CCS) == 0)) {
383 /* --- HC management functions --------------------------------------------- */
385 /* Write len bytes to fifo, pad till 32-bit boundary
387 static void write_ptddata_to_fifo(struct isp116x *isp116x, void *buf, int len)
390 u16 *dp2 = (u16 *) buf;
394 if ((unsigned long)dp2 & 1) {
396 for (; len > 1; len -= 2) {
399 isp116x_raw_write_data16(isp116x, w);
402 isp116x_write_data16(isp116x, (u16) * dp);
405 for (; len > 1; len -= 2)
406 isp116x_raw_write_data16(isp116x, *dp2++);
408 isp116x_write_data16(isp116x, 0xff & *((u8 *) dp2));
410 if (quot == 1 || quot == 2)
411 isp116x_raw_write_data16(isp116x, 0);
414 /* Read len bytes from fifo and then read till 32-bit boundary
416 static void read_ptddata_from_fifo(struct isp116x *isp116x, void *buf, int len)
419 u16 *dp2 = (u16 *) buf;
423 if ((unsigned long)dp2 & 1) {
425 for (; len > 1; len -= 2) {
426 w = isp116x_raw_read_data16(isp116x);
428 *dp++ = (w >> 8) & 0xff;
431 *dp = 0xff & isp116x_read_data16(isp116x);
434 for (; len > 1; len -= 2)
435 *dp2++ = isp116x_raw_read_data16(isp116x);
437 *(u8 *) dp2 = 0xff & isp116x_read_data16(isp116x);
439 if (quot == 1 || quot == 2)
440 isp116x_raw_read_data16(isp116x);
443 /* Write PTD's and data for scheduled transfers into the fifo ram.
444 * Fifo must be empty and ready */
445 static void pack_fifo(struct isp116x *isp116x, struct usb_device *dev,
446 unsigned long pipe, struct ptd *ptd, int n, void *data,
449 int buflen = n * sizeof(struct ptd) + len;
452 DBG("--- pack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
454 isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
455 isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
456 isp116x_write_addr(isp116x, HCATLPORT | ISP116x_WRITE_OFFSET);
459 for (i = 0; i < n; i++) {
460 DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
463 isp116x_write_data16(isp116x, ptd[i].count);
464 isp116x_write_data16(isp116x, ptd[i].mps);
465 isp116x_write_data16(isp116x, ptd[i].len);
466 isp116x_write_data16(isp116x, ptd[i].faddr);
468 dump_ptd_data(&ptd[i], (__u8 *) data + done, 0);
469 write_ptddata_to_fifo(isp116x,
470 (__u8 *) data + done,
471 PTD_GET_LEN(&ptd[i]));
473 done += PTD_GET_LEN(&ptd[i]);
477 /* Read the processed PTD's and data from fifo ram back to URBs' buffers.
478 * Fifo must be full and done */
479 static int unpack_fifo(struct isp116x *isp116x, struct usb_device *dev,
480 unsigned long pipe, struct ptd *ptd, int n, void *data,
483 int buflen = n * sizeof(struct ptd) + len;
484 int i, done, cc, ret;
486 isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
487 isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
488 isp116x_write_addr(isp116x, HCATLPORT);
492 for (i = 0; i < n; i++) {
493 DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
495 ptd[i].count = isp116x_read_data16(isp116x);
496 ptd[i].mps = isp116x_read_data16(isp116x);
497 ptd[i].len = isp116x_read_data16(isp116x);
498 ptd[i].faddr = isp116x_read_data16(isp116x);
501 read_ptddata_from_fifo(isp116x,
502 (__u8 *) data + done,
503 PTD_GET_LEN(&ptd[i]));
504 dump_ptd_data(&ptd[i], (__u8 *) data + done, 1);
506 done += PTD_GET_LEN(&ptd[i]);
508 cc = PTD_GET_CC(&ptd[i]);
510 /* Data underrun means basically that we had more buffer space than
511 * the function had data. It is perfectly normal but upper levels have
512 * to know how much we actually transferred.
514 if (cc == TD_NOTACCESSED ||
515 (cc != TD_CC_NOERROR && (ret == TD_CC_NOERROR || ret == TD_DATAUNDERRUN)))
519 DBG("--- unpack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
524 /* Interrupt handling
526 static int isp116x_interrupt(struct isp116x *isp116x)
532 isp116x_write_reg16(isp116x, HCuPINTENB, 0);
533 irqstat = isp116x_read_reg16(isp116x, HCuPINT);
534 isp116x_write_reg16(isp116x, HCuPINT, irqstat);
535 DBG(">>>>>> irqstat %x <<<<<<", irqstat);
537 if (irqstat & HCuPINT_ATL) {
538 DBG(">>>>>> HCuPINT_ATL <<<<<<");
543 if (irqstat & HCuPINT_OPR) {
544 intstat = isp116x_read_reg32(isp116x, HCINTSTAT);
545 isp116x_write_reg32(isp116x, HCINTSTAT, intstat);
546 DBG(">>>>>> HCuPINT_OPR %x <<<<<<", intstat);
548 if (intstat & HCINT_UE) {
549 ERR("unrecoverable error, controller disabled");
551 /* FIXME: be optimistic, hope that bug won't repeat
552 * often. Make some non-interrupt context restart the
553 * controller. Count and limit the retries though;
554 * either hardware or software errors can go forever...
556 isp116x_reset(isp116x);
561 if (intstat & HCINT_RHSC) {
564 /* When root hub or any of its ports is going
565 to come out of suspend, it may take more
566 than 10ms for status bits to stabilize. */
570 if (intstat & HCINT_SO) {
571 ERR("schedule overrun");
575 irqstat &= ~HCuPINT_OPR;
581 /* With one PTD we can transfer almost 1K in one go;
582 * HC does the splitting into endpoint digestible transactions
586 static inline int max_transfer_len(struct usb_device *dev, unsigned long pipe)
588 unsigned mpck = usb_maxpacket(dev, pipe);
590 /* One PTD can transfer 1023 bytes but try to always
591 * transfer multiples of endpoint buffer size
593 return 1023 / mpck * mpck;
596 /* Do an USB transfer
598 static int isp116x_submit_job(struct usb_device *dev, unsigned long pipe,
599 int dir, void *buffer, int len)
601 struct isp116x *isp116x = &isp116x_dev;
602 int type = usb_pipetype(pipe);
603 int epnum = usb_pipeendpoint(pipe);
604 int max = usb_maxpacket(dev, pipe);
605 int dir_out = usb_pipeout(pipe);
606 int speed_low = (dev->speed == USB_SPEED_LOW);
607 int i, done = 0, stat, timeout, cc;
609 /* 500 frames or 0.5s timeout when function is busy and NAKs transactions for a while */
612 DBG("------------------------------------------------");
613 dump_msg(dev, pipe, buffer, len, "SUBMIT");
614 DBG("------------------------------------------------");
618 dev->status = USB_ST_CRC_ERR;
622 if (isp116x->disabled) {
624 dev->status = USB_ST_CRC_ERR;
628 /* device pulled? Shortcut the action. */
629 if (devgone == dev) {
631 dev->status = USB_ST_CRC_ERR;
632 return USB_ST_CRC_ERR;
636 ERR("pipesize for pipe %lx is zero", pipe);
637 dev->status = USB_ST_CRC_ERR;
641 if (type == PIPE_ISOCHRONOUS) {
642 ERR("isochronous transfers not supported");
643 dev->status = USB_ST_CRC_ERR;
647 /* FIFO not empty? */
648 if (isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_FULL) {
649 ERR("****** FIFO not empty! ******");
650 dev->status = USB_ST_BUF_ERR;
655 isp116x_write_reg32(isp116x, HCINTSTAT, 0xff);
657 /* Prepare the PTD data */
658 ptd->count = PTD_CC_MSK | PTD_ACTIVE_MSK |
659 PTD_TOGGLE(usb_gettoggle(dev, epnum, dir_out));
660 ptd->mps = PTD_MPS(max) | PTD_SPD(speed_low) | PTD_EP(epnum) | PTD_LAST_MSK;
661 ptd->len = PTD_LEN(len) | PTD_DIR(dir);
662 ptd->faddr = PTD_FA(usb_pipedevice(pipe));
665 /* Pack data into FIFO ram */
666 pack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
671 /* Start the data transfer */
673 /* Allow more time for a BULK device to react - some are slow */
674 if (usb_pipebulk(pipe))
679 /* Wait for it to complete */
681 /* Check whether the controller is done */
682 stat = isp116x_interrupt(isp116x);
685 dev->status = USB_ST_CRC_ERR;
691 /* Check the timeout */
696 stat = USB_ST_CRC_ERR;
701 /* We got an Root Hub Status Change interrupt */
703 isp116x_show_regs(isp116x);
708 timeout = rh_check_port_status(isp116x);
711 * FIXME! NOTE! AAAARGH!
712 * This is potentially dangerous because it assumes
713 * that only one device is ever plugged in!
719 /* Ok, now we can read transfer status */
721 /* FIFO not ready? */
722 if (!(isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_DONE)) {
723 ERR("****** FIFO not ready! ******");
724 dev->status = USB_ST_BUF_ERR;
728 /* Unpack data from FIFO ram */
729 cc = unpack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
731 i = PTD_GET_COUNT(ptd);
736 /* There was some kind of real problem; Prepare the PTD again
737 * and retry from the failed transaction on
739 if (cc && cc != TD_NOTACCESSED && cc != TD_DATAUNDERRUN) {
740 if (retries >= 100) {
742 /* The chip will have toggled the toggle bit for the failed
743 * transaction too. We have to toggle it back.
745 usb_settoggle(dev, epnum, dir_out, !PTD_GET_TOGGLE(ptd));
749 /* "Normal" errors; TD_NOTACCESSED would mean in effect that the function have NAKed
750 * the transactions from the first on for the whole frame. It may be busy and we retry
751 * with the same PTD. PTD_ACTIVE (and not TD_NOTACCESSED) would mean that some of the
752 * PTD didn't make it because the function was busy or the frame ended before the PTD
753 * finished. We prepare the rest of the data and try again.
755 else if (cc == TD_NOTACCESSED || PTD_GET_ACTIVE(ptd) || (cc != TD_DATAUNDERRUN && PTD_GET_COUNT(ptd) < PTD_GET_LEN(ptd))) {
758 if (cc == TD_NOTACCESSED && PTD_GET_ACTIVE(ptd) && !PTD_GET_COUNT(ptd)) goto retry_same;
759 usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
764 if (cc != TD_CC_NOERROR && cc != TD_DATAUNDERRUN) {
765 DBG("****** completition code error %x ******", cc);
767 case TD_CC_BITSTUFFING:
768 dev->status = USB_ST_BIT_ERR;
771 dev->status = USB_ST_STALLED;
773 case TD_BUFFEROVERRUN:
774 case TD_BUFFERUNDERRUN:
775 dev->status = USB_ST_BUF_ERR;
778 dev->status = USB_ST_CRC_ERR;
782 else usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
784 dump_msg(dev, pipe, buffer, len, "SUBMIT(ret)");
790 /* Adapted from au1x00_usb_ohci.c
792 static int isp116x_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
793 void *buffer, int transfer_len,
794 struct devrequest *cmd)
796 struct isp116x *isp116x = &isp116x_dev;
799 int leni = transfer_len;
803 u8 *data_buf = (u8 *) datab;
809 if (usb_pipeint(pipe)) {
810 INFO("Root-Hub submit IRQ: NOT implemented");
814 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
815 wValue = swap_16(cmd->value);
816 wIndex = swap_16(cmd->index);
817 wLength = swap_16(cmd->length);
819 DBG("--- HUB ----------------------------------------");
820 DBG("submit rh urb, req=%x val=%#x index=%#x len=%d",
821 bmRType_bReq, wValue, wIndex, wLength);
822 dump_msg(dev, pipe, buffer, transfer_len, "RH");
823 DBG("------------------------------------------------");
825 switch (bmRType_bReq) {
827 DBG("RH_GET_STATUS");
829 *(__u16 *) data_buf = swap_16(1);
833 case RH_GET_STATUS | RH_INTERFACE:
834 DBG("RH_GET_STATUS | RH_INTERFACE");
836 *(__u16 *) data_buf = swap_16(0);
840 case RH_GET_STATUS | RH_ENDPOINT:
841 DBG("RH_GET_STATUS | RH_ENDPOINT");
843 *(__u16 *) data_buf = swap_16(0);
847 case RH_GET_STATUS | RH_CLASS:
848 DBG("RH_GET_STATUS | RH_CLASS");
850 tmp = isp116x_read_reg32(isp116x, HCRHSTATUS);
852 *(__u32 *) data_buf = swap_32(tmp & ~(RH_HS_CRWE | RH_HS_DRWE));
856 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
857 DBG("RH_GET_STATUS | RH_OTHER | RH_CLASS");
859 tmp = isp116x_read_reg32(isp116x, HCRHPORT1 + wIndex - 1);
860 *(__u32 *) data_buf = swap_32(tmp);
861 isp116x_show_regs(isp116x);
865 case RH_CLEAR_FEATURE | RH_ENDPOINT:
866 DBG("RH_CLEAR_FEATURE | RH_ENDPOINT");
869 case RH_ENDPOINT_STALL:
870 DBG("C_HUB_ENDPOINT_STALL");
876 case RH_CLEAR_FEATURE | RH_CLASS:
877 DBG("RH_CLEAR_FEATURE | RH_CLASS");
880 case RH_C_HUB_LOCAL_POWER:
881 DBG("C_HUB_LOCAL_POWER");
885 case RH_C_HUB_OVER_CURRENT:
886 DBG("C_HUB_OVER_CURRENT");
887 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_OCIC);
893 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
894 DBG("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS");
898 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
903 case RH_PORT_SUSPEND:
904 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
910 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
915 case RH_C_PORT_CONNECTION:
916 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
921 case RH_C_PORT_ENABLE:
922 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
927 case RH_C_PORT_SUSPEND:
928 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
933 case RH_C_PORT_OVER_CURRENT:
934 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
939 case RH_C_PORT_RESET:
940 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
946 ERR("invalid wValue");
947 stat = USB_ST_STALLED;
950 isp116x_show_regs(isp116x);
954 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
955 DBG("RH_SET_FEATURE | RH_OTHER | RH_CLASS");
958 case RH_PORT_SUSPEND:
959 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
965 /* Spin until any current reset finishes */
968 isp116x_read_reg32(isp116x,
969 HCRHPORT1 + wIndex - 1);
970 if (!(tmp & RH_PS_PRS))
974 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
982 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
988 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
994 ERR("invalid wValue");
995 stat = USB_ST_STALLED;
998 isp116x_show_regs(isp116x);
1002 case RH_SET_ADDRESS:
1003 DBG("RH_SET_ADDRESS");
1009 case RH_GET_DESCRIPTOR:
1010 DBG("RH_GET_DESCRIPTOR: %x, %d", wValue, wLength);
1013 case (USB_DT_DEVICE << 8): /* device descriptor */
1014 len = min_t(unsigned int,
1015 leni, min_t(unsigned int,
1016 sizeof(root_hub_dev_des),
1018 data_buf = root_hub_dev_des;
1021 case (USB_DT_CONFIG << 8): /* configuration descriptor */
1022 len = min_t(unsigned int,
1023 leni, min_t(unsigned int,
1024 sizeof(root_hub_config_des),
1026 data_buf = root_hub_config_des;
1029 case ((USB_DT_STRING << 8) | 0x00): /* string 0 descriptors */
1030 len = min_t(unsigned int,
1031 leni, min_t(unsigned int,
1032 sizeof(root_hub_str_index0),
1034 data_buf = root_hub_str_index0;
1037 case ((USB_DT_STRING << 8) | 0x01): /* string 1 descriptors */
1038 len = min_t(unsigned int,
1039 leni, min_t(unsigned int,
1040 sizeof(root_hub_str_index1),
1042 data_buf = root_hub_str_index1;
1046 ERR("invalid wValue");
1047 stat = USB_ST_STALLED;
1052 case RH_GET_DESCRIPTOR | RH_CLASS:
1053 DBG("RH_GET_DESCRIPTOR | RH_CLASS");
1055 tmp = isp116x_read_reg32(isp116x, HCRHDESCA);
1057 data_buf[0] = 0x09; /* min length; */
1059 data_buf[2] = tmp & RH_A_NDP;
1061 if (tmp & RH_A_PSM) /* per-port power switching? */
1062 data_buf[3] |= 0x01;
1063 if (tmp & RH_A_NOCP) /* no overcurrent reporting? */
1064 data_buf[3] |= 0x10;
1065 else if (tmp & RH_A_OCPM) /* per-port overcurrent rep? */
1066 data_buf[3] |= 0x08;
1068 /* Corresponds to data_buf[4-7] */
1070 data_buf[5] = (tmp & RH_A_POTPGT) >> 24;
1072 tmp = isp116x_read_reg32(isp116x, HCRHDESCB);
1074 data_buf[7] = tmp & RH_B_DR;
1075 if (data_buf[2] < 7)
1079 data_buf[8] = (tmp & RH_B_DR) >> 8;
1080 data_buf[10] = data_buf[9] = 0xff;
1083 len = min_t(unsigned int, leni,
1084 min_t(unsigned int, data_buf[0], wLength));
1087 case RH_GET_CONFIGURATION:
1088 DBG("RH_GET_CONFIGURATION");
1090 *(__u8 *) data_buf = 0x01;
1094 case RH_SET_CONFIGURATION:
1095 DBG("RH_SET_CONFIGURATION");
1097 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPSC);
1102 ERR("*** *** *** unsupported root hub command *** *** ***");
1103 stat = USB_ST_STALLED;
1106 len = min_t(int, len, leni);
1107 if (buffer != data_buf)
1108 memcpy(buffer, data_buf, len);
1112 DBG("dev act_len %d, status %d", dev->act_len, dev->status);
1114 dump_msg(dev, pipe, buffer, transfer_len, "RH(ret)");
1119 /* --- Transfer functions -------------------------------------------------- */
1121 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1122 int len, int interval)
1124 DBG("dev=%p pipe=%#lx buf=%p size=%d int=%d",
1125 dev, pipe, buffer, len, interval);
1130 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1131 int len, struct devrequest *setup)
1133 int devnum = usb_pipedevice(pipe);
1134 int epnum = usb_pipeendpoint(pipe);
1135 int max = max_transfer_len(dev, pipe);
1136 int dir_in = usb_pipein(pipe);
1139 /* Control message is for the HUB? */
1140 if (devnum == rh_devnum)
1141 return isp116x_submit_rh_msg(dev, pipe, buffer, len, setup);
1143 /* Ok, no HUB message so send the message to the device */
1146 DBG("--- SETUP PHASE --------------------------------");
1147 usb_settoggle(dev, epnum, 1, 0);
1148 ret = isp116x_submit_job(dev, pipe,
1150 setup, sizeof(struct devrequest));
1152 DBG("control setup phase error (ret = %d", ret);
1157 DBG("--- DATA PHASE ---------------------------------");
1159 usb_settoggle(dev, epnum, !dir_in, 1);
1160 while (done < len) {
1161 ret = isp116x_submit_job(dev, pipe,
1162 dir_in ? PTD_DIR_IN : PTD_DIR_OUT,
1163 (__u8 *) buffer + done,
1164 max > len - done ? len - done : max);
1166 DBG("control data phase error (ret = %d)", ret);
1171 if (dir_in && ret < max) /* short packet */
1176 DBG("--- STATUS PHASE -------------------------------");
1177 usb_settoggle(dev, epnum, !dir_in, 1);
1178 ret = isp116x_submit_job(dev, pipe,
1179 !dir_in ? PTD_DIR_IN : PTD_DIR_OUT, NULL, 0);
1181 DBG("control status phase error (ret = %d", ret);
1185 dev->act_len = done;
1187 dump_msg(dev, pipe, buffer, len, "DEV(ret)");
1192 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1195 int dir_out = usb_pipeout(pipe);
1196 int max = max_transfer_len(dev, pipe);
1199 DBG("--- BULK ---------------------------------------");
1200 DBG("dev=%ld pipe=%ld buf=%p size=%d dir_out=%d",
1201 usb_pipedevice(pipe), usb_pipeendpoint(pipe), buffer, len, dir_out);
1204 while (done < len) {
1205 ret = isp116x_submit_job(dev, pipe,
1206 !dir_out ? PTD_DIR_IN : PTD_DIR_OUT,
1207 (__u8 *) buffer + done,
1208 max > len - done ? len - done : max);
1210 DBG("error on bulk message (ret = %d)", ret);
1216 if (!dir_out && ret < max) /* short packet */
1220 dev->act_len = done;
1225 /* --- Basic functions ----------------------------------------------------- */
1227 static int isp116x_sw_reset(struct isp116x *isp116x)
1234 isp116x->disabled = 1;
1236 isp116x_write_reg16(isp116x, HCSWRES, HCSWRES_MAGIC);
1237 isp116x_write_reg32(isp116x, HCCMDSTAT, HCCMDSTAT_HCR);
1239 /* It usually resets within 1 ms */
1241 if (!(isp116x_read_reg32(isp116x, HCCMDSTAT) & HCCMDSTAT_HCR))
1245 ERR("software reset timeout");
1251 static int isp116x_reset(struct isp116x *isp116x)
1255 int ret, timeout = 15 /* ms */ ;
1259 ret = isp116x_sw_reset(isp116x);
1263 for (t = 0; t < timeout; t++) {
1264 clkrdy = isp116x_read_reg16(isp116x, HCuPINT) & HCuPINT_CLKRDY;
1270 ERR("clock not ready after %dms", timeout);
1271 /* After sw_reset the clock won't report to be ready, if
1272 H_WAKEUP pin is high. */
1273 ERR("please make sure that the H_WAKEUP pin is pulled low!");
1279 static void isp116x_stop(struct isp116x *isp116x)
1285 isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1287 /* Switch off ports' power, some devices don't come up
1288 after next 'start' without this */
1289 val = isp116x_read_reg32(isp116x, HCRHDESCA);
1290 val &= ~(RH_A_NPS | RH_A_PSM);
1291 isp116x_write_reg32(isp116x, HCRHDESCA, val);
1292 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPS);
1294 isp116x_sw_reset(isp116x);
1298 * Configure the chip. The chip must be successfully reset by now.
1300 static int isp116x_start(struct isp116x *isp116x)
1302 struct isp116x_platform_data *board = isp116x->board;
1307 /* Clear interrupt status and disable all interrupt sources */
1308 isp116x_write_reg16(isp116x, HCuPINT, 0xff);
1309 isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1311 isp116x_write_reg16(isp116x, HCITLBUFLEN, ISP116x_ITL_BUFSIZE);
1312 isp116x_write_reg16(isp116x, HCATLBUFLEN, ISP116x_ATL_BUFSIZE);
1314 /* Hardware configuration */
1315 val = HCHWCFG_DBWIDTH(1);
1316 if (board->sel15Kres)
1317 val |= HCHWCFG_15KRSEL;
1318 /* Remote wakeup won't work without working clock */
1319 if (board->remote_wakeup_enable)
1320 val |= HCHWCFG_CLKNOTSTOP;
1321 if (board->oc_enable)
1322 val |= HCHWCFG_ANALOG_OC;
1323 isp116x_write_reg16(isp116x, HCHWCFG, val);
1325 /* --- Root hub configuration */
1326 val = (25 << 24) & RH_A_POTPGT;
1327 /* AN10003_1.pdf recommends RH_A_NPS (no power switching) to
1328 be always set. Yet, instead, we request individual port
1331 /* Report overcurrent per port */
1333 isp116x_write_reg32(isp116x, HCRHDESCA, val);
1334 isp116x->rhdesca = isp116x_read_reg32(isp116x, HCRHDESCA);
1337 isp116x_write_reg32(isp116x, HCRHDESCB, val);
1338 isp116x->rhdescb = isp116x_read_reg32(isp116x, HCRHDESCB);
1341 if (board->remote_wakeup_enable)
1343 isp116x_write_reg32(isp116x, HCRHSTATUS, val);
1344 isp116x->rhstatus = isp116x_read_reg32(isp116x, HCRHSTATUS);
1346 isp116x_write_reg32(isp116x, HCFMINTVL, 0x27782edf);
1348 /* Go operational */
1349 val = HCCONTROL_USB_OPER;
1350 if (board->remote_wakeup_enable)
1351 val |= HCCONTROL_RWE;
1352 isp116x_write_reg32(isp116x, HCCONTROL, val);
1354 /* Disable ports to avoid race in device enumeration */
1355 isp116x_write_reg32(isp116x, HCRHPORT1, RH_PS_CCS);
1356 isp116x_write_reg32(isp116x, HCRHPORT2, RH_PS_CCS);
1358 isp116x_show_regs(isp116x);
1360 isp116x->disabled = 0;
1365 /* --- Init functions ------------------------------------------------------ */
1367 int isp116x_check_id(struct isp116x *isp116x)
1371 val = isp116x_read_reg16(isp116x, HCCHIPID);
1372 if ((val & HCCHIPID_MASK) != HCCHIPID_MAGIC) {
1373 ERR("invalid chip ID %04x", val);
1380 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller))
1382 struct isp116x *isp116x = &isp116x_dev;
1386 got_rhsc = rh_devnum = 0;
1388 /* Init device registers addr */
1389 isp116x->addr_reg = (u16 *) ISP116X_HCD_ADDR;
1390 isp116x->data_reg = (u16 *) ISP116X_HCD_DATA;
1392 /* Setup specific board settings */
1393 #ifdef ISP116X_HCD_SEL15kRES
1394 isp116x_board.sel15Kres = 1;
1396 #ifdef ISP116X_HCD_OC_ENABLE
1397 isp116x_board.oc_enable = 1;
1399 #ifdef ISP116X_HCD_REMOTE_WAKEUP_ENABLE
1400 isp116x_board.remote_wakeup_enable = 1;
1402 isp116x->board = &isp116x_board;
1404 /* Try to get ISP116x silicon chip ID */
1405 if (isp116x_check_id(isp116x) < 0)
1408 isp116x->disabled = 1;
1409 isp116x->sleeping = 0;
1411 isp116x_reset(isp116x);
1412 isp116x_start(isp116x);
1417 int usb_lowlevel_stop(int index)
1419 struct isp116x *isp116x = &isp116x_dev;
1423 if (!isp116x->disabled)
1424 isp116x_stop(isp116x);