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 static int isp116x_reset(struct isp116x *isp116x);
108 /* --- Debugging functions ------------------------------------------------- */
110 #define isp116x_show_reg(d, r) { \
112 DBG("%-12s[%02x]: %08x", #r, \
113 r, isp116x_read_reg32(d, r)); \
115 DBG("%-12s[%02x]: %04x", #r, \
116 r, isp116x_read_reg16(d, r)); \
120 #define isp116x_show_regs(d) { \
121 isp116x_show_reg(d, HCREVISION); \
122 isp116x_show_reg(d, HCCONTROL); \
123 isp116x_show_reg(d, HCCMDSTAT); \
124 isp116x_show_reg(d, HCINTSTAT); \
125 isp116x_show_reg(d, HCINTENB); \
126 isp116x_show_reg(d, HCFMINTVL); \
127 isp116x_show_reg(d, HCFMREM); \
128 isp116x_show_reg(d, HCFMNUM); \
129 isp116x_show_reg(d, HCLSTHRESH); \
130 isp116x_show_reg(d, HCRHDESCA); \
131 isp116x_show_reg(d, HCRHDESCB); \
132 isp116x_show_reg(d, HCRHSTATUS); \
133 isp116x_show_reg(d, HCRHPORT1); \
134 isp116x_show_reg(d, HCRHPORT2); \
135 isp116x_show_reg(d, HCHWCFG); \
136 isp116x_show_reg(d, HCDMACFG); \
137 isp116x_show_reg(d, HCXFERCTR); \
138 isp116x_show_reg(d, HCuPINT); \
139 isp116x_show_reg(d, HCuPINTENB); \
140 isp116x_show_reg(d, HCCHIPID); \
141 isp116x_show_reg(d, HCSCRATCH); \
142 isp116x_show_reg(d, HCITLBUFLEN); \
143 isp116x_show_reg(d, HCATLBUFLEN); \
144 isp116x_show_reg(d, HCBUFSTAT); \
145 isp116x_show_reg(d, HCRDITL0LEN); \
146 isp116x_show_reg(d, HCRDITL1LEN); \
151 static int isp116x_get_current_frame_number(struct usb_device *usb_dev)
153 struct isp116x *isp116x = &isp116x_dev;
155 return isp116x_read_reg32(isp116x, HCFMNUM);
158 static void dump_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
165 DBG("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d stat:%#lx",
167 isp116x_get_current_frame_number(dev),
168 usb_pipedevice(pipe),
169 usb_pipeendpoint(pipe),
170 usb_pipeout(pipe) ? 'O' : 'I',
171 usb_pipetype(pipe) < 2 ?
174 (usb_pipecontrol(pipe) ? "CTRL" : "BULK"), len, dev->status);
176 if (len > 0 && buffer) {
177 printf(__FILE__ ": data(%d):", len);
178 for (i = 0; i < 16 && i < len; i++)
179 printf(" %02x", ((__u8 *) buffer)[i]);
180 printf("%s\n", i < len ? "..." : "");
185 #define PTD_DIR_STR(ptd) ({char __c; \
186 switch(PTD_GET_DIR(ptd)){ \
187 case 0: __c = 's'; break; \
188 case 1: __c = 'o'; break; \
189 default: __c = 'i'; break; \
193 Dump PTD info. The code documents the format
196 static inline void dump_ptd(struct ptd *ptd)
202 DBG("PTD(ext) : cc:%x %d%c%d %d,%d,%d t:%x %x%x%x",
204 PTD_GET_FA(ptd), PTD_DIR_STR(ptd), PTD_GET_EP(ptd),
205 PTD_GET_COUNT(ptd), PTD_GET_LEN(ptd), PTD_GET_MPS(ptd),
207 PTD_GET_ACTIVE(ptd), PTD_GET_SPD(ptd), PTD_GET_LAST(ptd));
209 printf("isp116x: %s: PTD(byte): ", __FUNCTION__);
210 for (k = 0; k < sizeof(struct ptd); ++k)
211 printf("%02x ", ((u8 *) ptd)[k]);
216 static inline void dump_ptd_data(struct ptd *ptd, u8 * buf, int type)
221 if (type == 0 /* 0ut data */ ) {
222 printf("isp116x: %s: out data: ", __FUNCTION__);
223 for (k = 0; k < PTD_GET_LEN(ptd); ++k)
224 printf("%02x ", ((u8 *) buf)[k]);
227 if (type == 1 /* 1n data */ ) {
228 printf("isp116x: %s: in data: ", __FUNCTION__);
229 for (k = 0; k < PTD_GET_COUNT(ptd); ++k)
230 printf("%02x ", ((u8 *) buf)[k]);
234 if (PTD_GET_LAST(ptd))
235 DBG("--- last PTD ---");
241 #define dump_msg(dev, pipe, buffer, len, str) do { } while (0)
242 #define dump_pkt(dev, pipe, buffer, len, setup, str, small) do {} while (0)
244 #define dump_ptd(ptd) do {} while (0)
245 #define dump_ptd_data(ptd, buf, type) do {} while (0)
249 /* --- Virtual Root Hub ---------------------------------------------------- */
251 #include <usbroothubdes.h>
254 * Hub class-specific descriptor is constructed dynamically
257 /* --- Virtual root hub management functions ------------------------------- */
259 static int rh_check_port_status(struct isp116x *isp116x)
265 temp = isp116x_read_reg32(isp116x, HCRHSTATUS);
266 ndp = (temp & RH_A_NDP);
267 for (i = 0; i < ndp; i++) {
268 temp = isp116x_read_reg32(isp116x, HCRHPORT1 + i);
269 /* check for a device disconnect */
270 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
271 (RH_PS_PESC | RH_PS_CSC)) && ((temp & RH_PS_CCS) == 0)) {
279 /* --- HC management functions --------------------------------------------- */
281 /* Write len bytes to fifo, pad till 32-bit boundary
283 static void write_ptddata_to_fifo(struct isp116x *isp116x, void *buf, int len)
286 u16 *dp2 = (u16 *) buf;
290 if ((unsigned long)dp2 & 1) {
292 for (; len > 1; len -= 2) {
295 isp116x_raw_write_data16(isp116x, w);
298 isp116x_write_data16(isp116x, (u16) * dp);
301 for (; len > 1; len -= 2)
302 isp116x_raw_write_data16(isp116x, *dp2++);
304 isp116x_write_data16(isp116x, 0xff & *((u8 *) dp2));
306 if (quot == 1 || quot == 2)
307 isp116x_raw_write_data16(isp116x, 0);
310 /* Read len bytes from fifo and then read till 32-bit boundary
312 static void read_ptddata_from_fifo(struct isp116x *isp116x, void *buf, int len)
315 u16 *dp2 = (u16 *) buf;
319 if ((unsigned long)dp2 & 1) {
321 for (; len > 1; len -= 2) {
322 w = isp116x_raw_read_data16(isp116x);
324 *dp++ = (w >> 8) & 0xff;
327 *dp = 0xff & isp116x_read_data16(isp116x);
330 for (; len > 1; len -= 2)
331 *dp2++ = isp116x_raw_read_data16(isp116x);
333 *(u8 *) dp2 = 0xff & isp116x_read_data16(isp116x);
335 if (quot == 1 || quot == 2)
336 isp116x_raw_read_data16(isp116x);
339 /* Write PTD's and data for scheduled transfers into the fifo ram.
340 * Fifo must be empty and ready */
341 static void pack_fifo(struct isp116x *isp116x, struct usb_device *dev,
342 unsigned long pipe, struct ptd *ptd, int n, void *data,
345 int buflen = n * sizeof(struct ptd) + len;
348 DBG("--- pack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
350 isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
351 isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
352 isp116x_write_addr(isp116x, HCATLPORT | ISP116x_WRITE_OFFSET);
355 for (i = 0; i < n; i++) {
356 DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
359 isp116x_write_data16(isp116x, ptd[i].count);
360 isp116x_write_data16(isp116x, ptd[i].mps);
361 isp116x_write_data16(isp116x, ptd[i].len);
362 isp116x_write_data16(isp116x, ptd[i].faddr);
364 dump_ptd_data(&ptd[i], (__u8 *) data + done, 0);
365 write_ptddata_to_fifo(isp116x,
366 (__u8 *) data + done,
367 PTD_GET_LEN(&ptd[i]));
369 done += PTD_GET_LEN(&ptd[i]);
373 /* Read the processed PTD's and data from fifo ram back to URBs' buffers.
374 * Fifo must be full and done */
375 static int unpack_fifo(struct isp116x *isp116x, struct usb_device *dev,
376 unsigned long pipe, struct ptd *ptd, int n, void *data,
379 int buflen = n * sizeof(struct ptd) + len;
380 int i, done, cc, ret;
382 isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
383 isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
384 isp116x_write_addr(isp116x, HCATLPORT);
388 for (i = 0; i < n; i++) {
389 DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
391 ptd[i].count = isp116x_read_data16(isp116x);
392 ptd[i].mps = isp116x_read_data16(isp116x);
393 ptd[i].len = isp116x_read_data16(isp116x);
394 ptd[i].faddr = isp116x_read_data16(isp116x);
397 read_ptddata_from_fifo(isp116x,
398 (__u8 *) data + done,
399 PTD_GET_LEN(&ptd[i]));
400 dump_ptd_data(&ptd[i], (__u8 *) data + done, 1);
402 done += PTD_GET_LEN(&ptd[i]);
404 cc = PTD_GET_CC(&ptd[i]);
406 /* Data underrun means basically that we had more buffer space than
407 * the function had data. It is perfectly normal but upper levels have
408 * to know how much we actually transferred.
410 if (cc == TD_NOTACCESSED ||
411 (cc != TD_CC_NOERROR && (ret == TD_CC_NOERROR || ret == TD_DATAUNDERRUN)))
415 DBG("--- unpack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
420 /* Interrupt handling
422 static int isp116x_interrupt(struct isp116x *isp116x)
428 isp116x_write_reg16(isp116x, HCuPINTENB, 0);
429 irqstat = isp116x_read_reg16(isp116x, HCuPINT);
430 isp116x_write_reg16(isp116x, HCuPINT, irqstat);
431 DBG(">>>>>> irqstat %x <<<<<<", irqstat);
433 if (irqstat & HCuPINT_ATL) {
434 DBG(">>>>>> HCuPINT_ATL <<<<<<");
439 if (irqstat & HCuPINT_OPR) {
440 intstat = isp116x_read_reg32(isp116x, HCINTSTAT);
441 isp116x_write_reg32(isp116x, HCINTSTAT, intstat);
442 DBG(">>>>>> HCuPINT_OPR %x <<<<<<", intstat);
444 if (intstat & HCINT_UE) {
445 ERR("unrecoverable error, controller disabled");
447 /* FIXME: be optimistic, hope that bug won't repeat
448 * often. Make some non-interrupt context restart the
449 * controller. Count and limit the retries though;
450 * either hardware or software errors can go forever...
452 isp116x_reset(isp116x);
457 if (intstat & HCINT_RHSC) {
460 /* When root hub or any of its ports is going
461 to come out of suspend, it may take more
462 than 10ms for status bits to stabilize. */
466 if (intstat & HCINT_SO) {
467 ERR("schedule overrun");
471 irqstat &= ~HCuPINT_OPR;
477 /* With one PTD we can transfer almost 1K in one go;
478 * HC does the splitting into endpoint digestible transactions
482 static inline int max_transfer_len(struct usb_device *dev, unsigned long pipe)
484 unsigned mpck = usb_maxpacket(dev, pipe);
486 /* One PTD can transfer 1023 bytes but try to always
487 * transfer multiples of endpoint buffer size
489 return 1023 / mpck * mpck;
492 /* Do an USB transfer
494 static int isp116x_submit_job(struct usb_device *dev, unsigned long pipe,
495 int dir, void *buffer, int len)
497 struct isp116x *isp116x = &isp116x_dev;
498 int type = usb_pipetype(pipe);
499 int epnum = usb_pipeendpoint(pipe);
500 int max = usb_maxpacket(dev, pipe);
501 int dir_out = usb_pipeout(pipe);
502 int speed_low = (dev->speed == USB_SPEED_LOW);
503 int i, done = 0, stat, timeout, cc;
505 /* 500 frames or 0.5s timeout when function is busy and NAKs transactions for a while */
508 DBG("------------------------------------------------");
509 dump_msg(dev, pipe, buffer, len, "SUBMIT");
510 DBG("------------------------------------------------");
514 dev->status = USB_ST_CRC_ERR;
518 if (isp116x->disabled) {
520 dev->status = USB_ST_CRC_ERR;
524 /* device pulled? Shortcut the action. */
525 if (devgone == dev) {
527 dev->status = USB_ST_CRC_ERR;
528 return USB_ST_CRC_ERR;
532 ERR("pipesize for pipe %lx is zero", pipe);
533 dev->status = USB_ST_CRC_ERR;
537 if (type == PIPE_ISOCHRONOUS) {
538 ERR("isochronous transfers not supported");
539 dev->status = USB_ST_CRC_ERR;
543 /* FIFO not empty? */
544 if (isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_FULL) {
545 ERR("****** FIFO not empty! ******");
546 dev->status = USB_ST_BUF_ERR;
551 isp116x_write_reg32(isp116x, HCINTSTAT, 0xff);
553 /* Prepare the PTD data */
554 ptd->count = PTD_CC_MSK | PTD_ACTIVE_MSK |
555 PTD_TOGGLE(usb_gettoggle(dev, epnum, dir_out));
556 ptd->mps = PTD_MPS(max) | PTD_SPD(speed_low) | PTD_EP(epnum) | PTD_LAST_MSK;
557 ptd->len = PTD_LEN(len) | PTD_DIR(dir);
558 ptd->faddr = PTD_FA(usb_pipedevice(pipe));
561 /* Pack data into FIFO ram */
562 pack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
567 /* Start the data transfer */
569 /* Allow more time for a BULK device to react - some are slow */
570 if (usb_pipebulk(pipe))
575 /* Wait for it to complete */
577 /* Check whether the controller is done */
578 stat = isp116x_interrupt(isp116x);
581 dev->status = USB_ST_CRC_ERR;
587 /* Check the timeout */
592 stat = USB_ST_CRC_ERR;
597 /* We got an Root Hub Status Change interrupt */
599 isp116x_show_regs(isp116x);
604 timeout = rh_check_port_status(isp116x);
607 * FIXME! NOTE! AAAARGH!
608 * This is potentially dangerous because it assumes
609 * that only one device is ever plugged in!
615 /* Ok, now we can read transfer status */
617 /* FIFO not ready? */
618 if (!(isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_DONE)) {
619 ERR("****** FIFO not ready! ******");
620 dev->status = USB_ST_BUF_ERR;
624 /* Unpack data from FIFO ram */
625 cc = unpack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
627 i = PTD_GET_COUNT(ptd);
632 /* There was some kind of real problem; Prepare the PTD again
633 * and retry from the failed transaction on
635 if (cc && cc != TD_NOTACCESSED && cc != TD_DATAUNDERRUN) {
636 if (retries >= 100) {
638 /* The chip will have toggled the toggle bit for the failed
639 * transaction too. We have to toggle it back.
641 usb_settoggle(dev, epnum, dir_out, !PTD_GET_TOGGLE(ptd));
645 /* "Normal" errors; TD_NOTACCESSED would mean in effect that the function have NAKed
646 * the transactions from the first on for the whole frame. It may be busy and we retry
647 * with the same PTD. PTD_ACTIVE (and not TD_NOTACCESSED) would mean that some of the
648 * PTD didn't make it because the function was busy or the frame ended before the PTD
649 * finished. We prepare the rest of the data and try again.
651 else if (cc == TD_NOTACCESSED || PTD_GET_ACTIVE(ptd) || (cc != TD_DATAUNDERRUN && PTD_GET_COUNT(ptd) < PTD_GET_LEN(ptd))) {
654 if (cc == TD_NOTACCESSED && PTD_GET_ACTIVE(ptd) && !PTD_GET_COUNT(ptd)) goto retry_same;
655 usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
660 if (cc != TD_CC_NOERROR && cc != TD_DATAUNDERRUN) {
661 DBG("****** completition code error %x ******", cc);
663 case TD_CC_BITSTUFFING:
664 dev->status = USB_ST_BIT_ERR;
667 dev->status = USB_ST_STALLED;
669 case TD_BUFFEROVERRUN:
670 case TD_BUFFERUNDERRUN:
671 dev->status = USB_ST_BUF_ERR;
674 dev->status = USB_ST_CRC_ERR;
678 else usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
680 dump_msg(dev, pipe, buffer, len, "SUBMIT(ret)");
686 /* Adapted from au1x00_usb_ohci.c
688 static int isp116x_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
689 void *buffer, int transfer_len,
690 struct devrequest *cmd)
692 struct isp116x *isp116x = &isp116x_dev;
695 int leni = transfer_len;
699 u8 *data_buf = (u8 *) datab;
705 if (usb_pipeint(pipe)) {
706 INFO("Root-Hub submit IRQ: NOT implemented");
710 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
711 wValue = swap_16(cmd->value);
712 wIndex = swap_16(cmd->index);
713 wLength = swap_16(cmd->length);
715 DBG("--- HUB ----------------------------------------");
716 DBG("submit rh urb, req=%x val=%#x index=%#x len=%d",
717 bmRType_bReq, wValue, wIndex, wLength);
718 dump_msg(dev, pipe, buffer, transfer_len, "RH");
719 DBG("------------------------------------------------");
721 switch (bmRType_bReq) {
723 DBG("RH_GET_STATUS");
725 *(__u16 *) data_buf = swap_16(1);
729 case RH_GET_STATUS | RH_INTERFACE:
730 DBG("RH_GET_STATUS | RH_INTERFACE");
732 *(__u16 *) data_buf = swap_16(0);
736 case RH_GET_STATUS | RH_ENDPOINT:
737 DBG("RH_GET_STATUS | RH_ENDPOINT");
739 *(__u16 *) data_buf = swap_16(0);
743 case RH_GET_STATUS | RH_CLASS:
744 DBG("RH_GET_STATUS | RH_CLASS");
746 tmp = isp116x_read_reg32(isp116x, HCRHSTATUS);
748 *(__u32 *) data_buf = swap_32(tmp & ~(RH_HS_CRWE | RH_HS_DRWE));
752 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
753 DBG("RH_GET_STATUS | RH_OTHER | RH_CLASS");
755 tmp = isp116x_read_reg32(isp116x, HCRHPORT1 + wIndex - 1);
756 *(__u32 *) data_buf = swap_32(tmp);
757 isp116x_show_regs(isp116x);
761 case RH_CLEAR_FEATURE | RH_ENDPOINT:
762 DBG("RH_CLEAR_FEATURE | RH_ENDPOINT");
765 case RH_ENDPOINT_STALL:
766 DBG("C_HUB_ENDPOINT_STALL");
772 case RH_CLEAR_FEATURE | RH_CLASS:
773 DBG("RH_CLEAR_FEATURE | RH_CLASS");
776 case RH_C_HUB_LOCAL_POWER:
777 DBG("C_HUB_LOCAL_POWER");
781 case RH_C_HUB_OVER_CURRENT:
782 DBG("C_HUB_OVER_CURRENT");
783 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_OCIC);
789 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
790 DBG("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS");
794 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
799 case RH_PORT_SUSPEND:
800 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
806 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
811 case RH_C_PORT_CONNECTION:
812 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
817 case RH_C_PORT_ENABLE:
818 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
823 case RH_C_PORT_SUSPEND:
824 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
829 case RH_C_PORT_OVER_CURRENT:
830 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
835 case RH_C_PORT_RESET:
836 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
842 ERR("invalid wValue");
843 stat = USB_ST_STALLED;
846 isp116x_show_regs(isp116x);
850 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
851 DBG("RH_SET_FEATURE | RH_OTHER | RH_CLASS");
854 case RH_PORT_SUSPEND:
855 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
861 /* Spin until any current reset finishes */
864 isp116x_read_reg32(isp116x,
865 HCRHPORT1 + wIndex - 1);
866 if (!(tmp & RH_PS_PRS))
870 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
878 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
884 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
890 ERR("invalid wValue");
891 stat = USB_ST_STALLED;
894 isp116x_show_regs(isp116x);
899 DBG("RH_SET_ADDRESS");
905 case RH_GET_DESCRIPTOR:
906 DBG("RH_GET_DESCRIPTOR: %x, %d", wValue, wLength);
909 case (USB_DT_DEVICE << 8): /* device descriptor */
910 len = min_t(unsigned int,
911 leni, min_t(unsigned int,
912 sizeof(root_hub_dev_des),
914 data_buf = root_hub_dev_des;
917 case (USB_DT_CONFIG << 8): /* configuration descriptor */
918 len = min_t(unsigned int,
919 leni, min_t(unsigned int,
920 sizeof(root_hub_config_des),
922 data_buf = root_hub_config_des;
925 case ((USB_DT_STRING << 8) | 0x00): /* string 0 descriptors */
926 len = min_t(unsigned int,
927 leni, min_t(unsigned int,
928 sizeof(root_hub_str_index0),
930 data_buf = root_hub_str_index0;
933 case ((USB_DT_STRING << 8) | 0x01): /* string 1 descriptors */
934 len = min_t(unsigned int,
935 leni, min_t(unsigned int,
936 sizeof(root_hub_str_index1),
938 data_buf = root_hub_str_index1;
942 ERR("invalid wValue");
943 stat = USB_ST_STALLED;
948 case RH_GET_DESCRIPTOR | RH_CLASS:
949 DBG("RH_GET_DESCRIPTOR | RH_CLASS");
951 tmp = isp116x_read_reg32(isp116x, HCRHDESCA);
953 data_buf[0] = 0x09; /* min length; */
955 data_buf[2] = tmp & RH_A_NDP;
957 if (tmp & RH_A_PSM) /* per-port power switching? */
959 if (tmp & RH_A_NOCP) /* no overcurrent reporting? */
961 else if (tmp & RH_A_OCPM) /* per-port overcurrent rep? */
964 /* Corresponds to data_buf[4-7] */
966 data_buf[5] = (tmp & RH_A_POTPGT) >> 24;
968 tmp = isp116x_read_reg32(isp116x, HCRHDESCB);
970 data_buf[7] = tmp & RH_B_DR;
975 data_buf[8] = (tmp & RH_B_DR) >> 8;
976 data_buf[10] = data_buf[9] = 0xff;
979 len = min_t(unsigned int, leni,
980 min_t(unsigned int, data_buf[0], wLength));
983 case RH_GET_CONFIGURATION:
984 DBG("RH_GET_CONFIGURATION");
986 *(__u8 *) data_buf = 0x01;
990 case RH_SET_CONFIGURATION:
991 DBG("RH_SET_CONFIGURATION");
993 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPSC);
998 ERR("*** *** *** unsupported root hub command *** *** ***");
999 stat = USB_ST_STALLED;
1002 len = min_t(int, len, leni);
1003 if (buffer != data_buf)
1004 memcpy(buffer, data_buf, len);
1008 DBG("dev act_len %d, status %d", dev->act_len, dev->status);
1010 dump_msg(dev, pipe, buffer, transfer_len, "RH(ret)");
1015 /* --- Transfer functions -------------------------------------------------- */
1017 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1018 int len, int interval)
1020 DBG("dev=%p pipe=%#lx buf=%p size=%d int=%d",
1021 dev, pipe, buffer, len, interval);
1026 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1027 int len, struct devrequest *setup)
1029 int devnum = usb_pipedevice(pipe);
1030 int epnum = usb_pipeendpoint(pipe);
1031 int max = max_transfer_len(dev, pipe);
1032 int dir_in = usb_pipein(pipe);
1035 /* Control message is for the HUB? */
1036 if (devnum == rh_devnum)
1037 return isp116x_submit_rh_msg(dev, pipe, buffer, len, setup);
1039 /* Ok, no HUB message so send the message to the device */
1042 DBG("--- SETUP PHASE --------------------------------");
1043 usb_settoggle(dev, epnum, 1, 0);
1044 ret = isp116x_submit_job(dev, pipe,
1046 setup, sizeof(struct devrequest));
1048 DBG("control setup phase error (ret = %d", ret);
1053 DBG("--- DATA PHASE ---------------------------------");
1055 usb_settoggle(dev, epnum, !dir_in, 1);
1056 while (done < len) {
1057 ret = isp116x_submit_job(dev, pipe,
1058 dir_in ? PTD_DIR_IN : PTD_DIR_OUT,
1059 (__u8 *) buffer + done,
1060 max > len - done ? len - done : max);
1062 DBG("control data phase error (ret = %d)", ret);
1067 if (dir_in && ret < max) /* short packet */
1072 DBG("--- STATUS PHASE -------------------------------");
1073 usb_settoggle(dev, epnum, !dir_in, 1);
1074 ret = isp116x_submit_job(dev, pipe,
1075 !dir_in ? PTD_DIR_IN : PTD_DIR_OUT, NULL, 0);
1077 DBG("control status phase error (ret = %d", ret);
1081 dev->act_len = done;
1083 dump_msg(dev, pipe, buffer, len, "DEV(ret)");
1088 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1091 int dir_out = usb_pipeout(pipe);
1092 int max = max_transfer_len(dev, pipe);
1095 DBG("--- BULK ---------------------------------------");
1096 DBG("dev=%ld pipe=%ld buf=%p size=%d dir_out=%d",
1097 usb_pipedevice(pipe), usb_pipeendpoint(pipe), buffer, len, dir_out);
1100 while (done < len) {
1101 ret = isp116x_submit_job(dev, pipe,
1102 !dir_out ? PTD_DIR_IN : PTD_DIR_OUT,
1103 (__u8 *) buffer + done,
1104 max > len - done ? len - done : max);
1106 DBG("error on bulk message (ret = %d)", ret);
1112 if (!dir_out && ret < max) /* short packet */
1116 dev->act_len = done;
1121 /* --- Basic functions ----------------------------------------------------- */
1123 static int isp116x_sw_reset(struct isp116x *isp116x)
1130 isp116x->disabled = 1;
1132 isp116x_write_reg16(isp116x, HCSWRES, HCSWRES_MAGIC);
1133 isp116x_write_reg32(isp116x, HCCMDSTAT, HCCMDSTAT_HCR);
1135 /* It usually resets within 1 ms */
1137 if (!(isp116x_read_reg32(isp116x, HCCMDSTAT) & HCCMDSTAT_HCR))
1141 ERR("software reset timeout");
1147 static int isp116x_reset(struct isp116x *isp116x)
1151 int ret, timeout = 15 /* ms */ ;
1155 ret = isp116x_sw_reset(isp116x);
1159 for (t = 0; t < timeout; t++) {
1160 clkrdy = isp116x_read_reg16(isp116x, HCuPINT) & HCuPINT_CLKRDY;
1166 ERR("clock not ready after %dms", timeout);
1167 /* After sw_reset the clock won't report to be ready, if
1168 H_WAKEUP pin is high. */
1169 ERR("please make sure that the H_WAKEUP pin is pulled low!");
1175 static void isp116x_stop(struct isp116x *isp116x)
1181 isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1183 /* Switch off ports' power, some devices don't come up
1184 after next 'start' without this */
1185 val = isp116x_read_reg32(isp116x, HCRHDESCA);
1186 val &= ~(RH_A_NPS | RH_A_PSM);
1187 isp116x_write_reg32(isp116x, HCRHDESCA, val);
1188 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPS);
1190 isp116x_sw_reset(isp116x);
1194 * Configure the chip. The chip must be successfully reset by now.
1196 static int isp116x_start(struct isp116x *isp116x)
1198 struct isp116x_platform_data *board = isp116x->board;
1203 /* Clear interrupt status and disable all interrupt sources */
1204 isp116x_write_reg16(isp116x, HCuPINT, 0xff);
1205 isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1207 isp116x_write_reg16(isp116x, HCITLBUFLEN, ISP116x_ITL_BUFSIZE);
1208 isp116x_write_reg16(isp116x, HCATLBUFLEN, ISP116x_ATL_BUFSIZE);
1210 /* Hardware configuration */
1211 val = HCHWCFG_DBWIDTH(1);
1212 if (board->sel15Kres)
1213 val |= HCHWCFG_15KRSEL;
1214 /* Remote wakeup won't work without working clock */
1215 if (board->remote_wakeup_enable)
1216 val |= HCHWCFG_CLKNOTSTOP;
1217 if (board->oc_enable)
1218 val |= HCHWCFG_ANALOG_OC;
1219 isp116x_write_reg16(isp116x, HCHWCFG, val);
1221 /* --- Root hub configuration */
1222 val = (25 << 24) & RH_A_POTPGT;
1223 /* AN10003_1.pdf recommends RH_A_NPS (no power switching) to
1224 be always set. Yet, instead, we request individual port
1227 /* Report overcurrent per port */
1229 isp116x_write_reg32(isp116x, HCRHDESCA, val);
1230 isp116x->rhdesca = isp116x_read_reg32(isp116x, HCRHDESCA);
1233 isp116x_write_reg32(isp116x, HCRHDESCB, val);
1234 isp116x->rhdescb = isp116x_read_reg32(isp116x, HCRHDESCB);
1237 if (board->remote_wakeup_enable)
1239 isp116x_write_reg32(isp116x, HCRHSTATUS, val);
1240 isp116x->rhstatus = isp116x_read_reg32(isp116x, HCRHSTATUS);
1242 isp116x_write_reg32(isp116x, HCFMINTVL, 0x27782edf);
1244 /* Go operational */
1245 val = HCCONTROL_USB_OPER;
1246 if (board->remote_wakeup_enable)
1247 val |= HCCONTROL_RWE;
1248 isp116x_write_reg32(isp116x, HCCONTROL, val);
1250 /* Disable ports to avoid race in device enumeration */
1251 isp116x_write_reg32(isp116x, HCRHPORT1, RH_PS_CCS);
1252 isp116x_write_reg32(isp116x, HCRHPORT2, RH_PS_CCS);
1254 isp116x_show_regs(isp116x);
1256 isp116x->disabled = 0;
1261 /* --- Init functions ------------------------------------------------------ */
1263 int isp116x_check_id(struct isp116x *isp116x)
1267 val = isp116x_read_reg16(isp116x, HCCHIPID);
1268 if ((val & HCCHIPID_MASK) != HCCHIPID_MAGIC) {
1269 ERR("invalid chip ID %04x", val);
1276 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller))
1278 struct isp116x *isp116x = &isp116x_dev;
1282 got_rhsc = rh_devnum = 0;
1284 /* Init device registers addr */
1285 isp116x->addr_reg = (u16 *) ISP116X_HCD_ADDR;
1286 isp116x->data_reg = (u16 *) ISP116X_HCD_DATA;
1288 /* Setup specific board settings */
1289 #ifdef ISP116X_HCD_SEL15kRES
1290 isp116x_board.sel15Kres = 1;
1292 #ifdef ISP116X_HCD_OC_ENABLE
1293 isp116x_board.oc_enable = 1;
1295 #ifdef ISP116X_HCD_REMOTE_WAKEUP_ENABLE
1296 isp116x_board.remote_wakeup_enable = 1;
1298 isp116x->board = &isp116x_board;
1300 /* Try to get ISP116x silicon chip ID */
1301 if (isp116x_check_id(isp116x) < 0)
1304 isp116x->disabled = 1;
1305 isp116x->sleeping = 0;
1307 isp116x_reset(isp116x);
1308 isp116x_start(isp116x);
1313 int usb_lowlevel_stop(int index)
1315 struct isp116x *isp116x = &isp116x_dev;
1319 if (!isp116x->disabled)
1320 isp116x_stop(isp116x);