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 #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)
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)) {
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)
292 u16 *dp2 = (u16 *) buf;
296 if ((unsigned long)dp2 & 1) {
298 for (; len > 1; len -= 2) {
301 isp116x_raw_write_data16(isp116x, w);
304 isp116x_write_data16(isp116x, (u16) * dp);
307 for (; len > 1; len -= 2)
308 isp116x_raw_write_data16(isp116x, *dp2++);
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)
321 u16 *dp2 = (u16 *) buf;
325 if ((unsigned long)dp2 & 1) {
327 for (; len > 1; len -= 2) {
328 w = isp116x_raw_read_data16(isp116x);
330 *dp++ = (w >> 8) & 0xff;
333 *dp = 0xff & isp116x_read_data16(isp116x);
336 for (; len > 1; len -= 2)
337 *dp2++ = isp116x_raw_read_data16(isp116x);
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,
351 int buflen = n * sizeof(struct ptd) + len;
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);
361 for (i = 0; i < n; i++) {
362 DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&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,
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);
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);
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)))
421 DBG("--- unpack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
426 /* Interrupt handling
428 static int isp116x_interrupt(struct isp116x *isp116x)
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 <<<<<<");
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);
463 if (intstat & HCINT_RHSC) {
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. */
472 if (intstat & HCINT_SO) {
473 ERR("schedule overrun");
477 irqstat &= ~HCuPINT_OPR;
483 /* With one PTD we can transfer almost 1K in one go;
484 * HC does the splitting into endpoint digestible transactions
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 */
514 DBG("------------------------------------------------");
515 dump_msg(dev, pipe, buffer, len, "SUBMIT");
516 DBG("------------------------------------------------");
520 dev->status = USB_ST_CRC_ERR;
524 if (isp116x->disabled) {
526 dev->status = USB_ST_CRC_ERR;
530 /* device pulled? Shortcut the action. */
531 if (devgone == dev) {
533 dev->status = USB_ST_CRC_ERR;
534 return USB_ST_CRC_ERR;
538 ERR("pipesize for pipe %lx is zero", pipe);
539 dev->status = USB_ST_CRC_ERR;
543 if (type == PIPE_ISOCHRONOUS) {
544 ERR("isochronous transfers not supported");
545 dev->status = USB_ST_CRC_ERR;
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;
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));
567 /* Pack data into FIFO ram */
568 pack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
573 /* Start the data transfer */
575 /* Allow more time for a BULK device to react - some are slow */
576 if (usb_pipebulk(pipe))
581 /* Wait for it to complete */
583 /* Check whether the controller is done */
584 stat = isp116x_interrupt(isp116x);
587 dev->status = USB_ST_CRC_ERR;
593 /* Check the timeout */
598 stat = USB_ST_CRC_ERR;
603 /* We got an Root Hub Status Change interrupt */
605 isp116x_show_regs(isp116x);
610 timeout = rh_check_port_status(isp116x);
613 * FIXME! NOTE! AAAARGH!
614 * This is potentially dangerous because it assumes
615 * that only one device is ever plugged in!
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;
630 /* Unpack data from FIFO ram */
631 cc = unpack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
633 i = PTD_GET_COUNT(ptd);
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) {
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));
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))) {
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));
666 if (cc != TD_CC_NOERROR && cc != TD_DATAUNDERRUN) {
667 DBG("****** completition code error %x ******", cc);
669 case TD_CC_BITSTUFFING:
670 dev->status = USB_ST_BIT_ERR;
673 dev->status = USB_ST_STALLED;
675 case TD_BUFFEROVERRUN:
676 case TD_BUFFERUNDERRUN:
677 dev->status = USB_ST_BUF_ERR;
680 dev->status = USB_ST_CRC_ERR;
684 else usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
686 dump_msg(dev, pipe, buffer, len, "SUBMIT(ret)");
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;
701 int leni = transfer_len;
705 u8 *data_buf = (u8 *) datab;
711 if (usb_pipeint(pipe)) {
712 INFO("Root-Hub submit IRQ: NOT implemented");
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) {
729 DBG("RH_GET_STATUS");
731 *(__u16 *) data_buf = swap_16(1);
735 case RH_GET_STATUS | RH_INTERFACE:
736 DBG("RH_GET_STATUS | RH_INTERFACE");
738 *(__u16 *) data_buf = swap_16(0);
742 case RH_GET_STATUS | RH_ENDPOINT:
743 DBG("RH_GET_STATUS | RH_ENDPOINT");
745 *(__u16 *) data_buf = swap_16(0);
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));
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);
767 case RH_CLEAR_FEATURE | RH_ENDPOINT:
768 DBG("RH_CLEAR_FEATURE | RH_ENDPOINT");
771 case RH_ENDPOINT_STALL:
772 DBG("C_HUB_ENDPOINT_STALL");
778 case RH_CLEAR_FEATURE | RH_CLASS:
779 DBG("RH_CLEAR_FEATURE | RH_CLASS");
782 case RH_C_HUB_LOCAL_POWER:
783 DBG("C_HUB_LOCAL_POWER");
787 case RH_C_HUB_OVER_CURRENT:
788 DBG("C_HUB_OVER_CURRENT");
789 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_OCIC);
795 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
796 DBG("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS");
800 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
805 case RH_PORT_SUSPEND:
806 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
812 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
817 case RH_C_PORT_CONNECTION:
818 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
823 case RH_C_PORT_ENABLE:
824 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
829 case RH_C_PORT_SUSPEND:
830 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
835 case RH_C_PORT_OVER_CURRENT:
836 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
841 case RH_C_PORT_RESET:
842 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
848 ERR("invalid wValue");
849 stat = USB_ST_STALLED;
852 isp116x_show_regs(isp116x);
856 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
857 DBG("RH_SET_FEATURE | RH_OTHER | RH_CLASS");
860 case RH_PORT_SUSPEND:
861 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
867 /* Spin until any current reset finishes */
870 isp116x_read_reg32(isp116x,
871 HCRHPORT1 + wIndex - 1);
872 if (!(tmp & RH_PS_PRS))
876 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
884 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
890 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
896 ERR("invalid wValue");
897 stat = USB_ST_STALLED;
900 isp116x_show_regs(isp116x);
905 DBG("RH_SET_ADDRESS");
911 case RH_GET_DESCRIPTOR:
912 DBG("RH_GET_DESCRIPTOR: %x, %d", wValue, wLength);
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),
920 data_buf = root_hub_dev_des;
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),
928 data_buf = root_hub_config_des;
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),
936 data_buf = root_hub_str_index0;
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),
944 data_buf = root_hub_str_index1;
948 ERR("invalid wValue");
949 stat = USB_ST_STALLED;
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; */
961 data_buf[2] = tmp & RH_A_NDP;
963 if (tmp & RH_A_PSM) /* per-port power switching? */
965 if (tmp & RH_A_NOCP) /* no overcurrent reporting? */
967 else if (tmp & RH_A_OCPM) /* per-port overcurrent rep? */
970 /* Corresponds to data_buf[4-7] */
972 data_buf[5] = (tmp & RH_A_POTPGT) >> 24;
974 tmp = isp116x_read_reg32(isp116x, HCRHDESCB);
976 data_buf[7] = tmp & RH_B_DR;
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));
989 case RH_GET_CONFIGURATION:
990 DBG("RH_GET_CONFIGURATION");
992 *(__u8 *) data_buf = 0x01;
996 case RH_SET_CONFIGURATION:
997 DBG("RH_SET_CONFIGURATION");
999 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPSC);
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);
1014 DBG("dev act_len %d, status %d", dev->act_len, dev->status);
1016 dump_msg(dev, pipe, buffer, transfer_len, "RH(ret)");
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);
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);
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 */
1048 DBG("--- SETUP PHASE --------------------------------");
1049 usb_settoggle(dev, epnum, 1, 0);
1050 ret = isp116x_submit_job(dev, pipe,
1052 setup, sizeof(struct devrequest));
1054 DBG("control setup phase error (ret = %d", ret);
1059 DBG("--- DATA PHASE ---------------------------------");
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);
1068 DBG("control data phase error (ret = %d)", ret);
1073 if (dir_in && ret < max) /* short packet */
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);
1083 DBG("control status phase error (ret = %d", ret);
1087 dev->act_len = done;
1089 dump_msg(dev, pipe, buffer, len, "DEV(ret)");
1094 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1097 int dir_out = usb_pipeout(pipe);
1098 int max = max_transfer_len(dev, pipe);
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);
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);
1112 DBG("error on bulk message (ret = %d)", ret);
1118 if (!dir_out && ret < max) /* short packet */
1122 dev->act_len = done;
1127 /* --- Basic functions ----------------------------------------------------- */
1129 static int isp116x_sw_reset(struct isp116x *isp116x)
1136 isp116x->disabled = 1;
1138 isp116x_write_reg16(isp116x, HCSWRES, HCSWRES_MAGIC);
1139 isp116x_write_reg32(isp116x, HCCMDSTAT, HCCMDSTAT_HCR);
1141 /* It usually resets within 1 ms */
1143 if (!(isp116x_read_reg32(isp116x, HCCMDSTAT) & HCCMDSTAT_HCR))
1147 ERR("software reset timeout");
1153 static int isp116x_reset(struct isp116x *isp116x)
1157 int ret, timeout = 15 /* ms */ ;
1161 ret = isp116x_sw_reset(isp116x);
1165 for (t = 0; t < timeout; t++) {
1166 clkrdy = isp116x_read_reg16(isp116x, HCuPINT) & HCuPINT_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!");
1181 static void isp116x_stop(struct isp116x *isp116x)
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;
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
1233 /* Report overcurrent per port */
1235 isp116x_write_reg32(isp116x, HCRHDESCA, val);
1236 isp116x->rhdesca = isp116x_read_reg32(isp116x, HCRHDESCA);
1239 isp116x_write_reg32(isp116x, HCRHDESCB, val);
1240 isp116x->rhdescb = isp116x_read_reg32(isp116x, HCRHDESCB);
1243 if (board->remote_wakeup_enable)
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;
1267 /* --- Init functions ------------------------------------------------------ */
1269 int isp116x_check_id(struct isp116x *isp116x)
1273 val = isp116x_read_reg16(isp116x, HCCHIPID);
1274 if ((val & HCCHIPID_MASK) != HCCHIPID_MAGIC) {
1275 ERR("invalid chip ID %04x", val);
1282 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller))
1284 struct isp116x *isp116x = &isp116x_dev;
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;
1298 #ifdef ISP116X_HCD_OC_ENABLE
1299 isp116x_board.oc_enable = 1;
1301 #ifdef ISP116X_HCD_REMOTE_WAKEUP_ENABLE
1302 isp116x_board.remote_wakeup_enable = 1;
1304 isp116x->board = &isp116x_board;
1306 /* Try to get ISP116x silicon chip ID */
1307 if (isp116x_check_id(isp116x) < 0)
1310 isp116x->disabled = 1;
1311 isp116x->sleeping = 0;
1313 isp116x_reset(isp116x);
1314 isp116x_start(isp116x);
1319 int usb_lowlevel_stop(int index)
1321 struct isp116x *isp116x = &isp116x_dev;
1325 if (!isp116x->disabled)
1326 isp116x_stop(isp116x);