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 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * Derived in part from the SL811 HCD driver "u-boot/drivers/sl811_usb.c"
24 * (original copyright message follows):
27 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
29 * This code is based on linux driver for sl811hs chip, source at
30 * drivers/usb/host/sl811.c:
32 * SL811 Host Controller Interface driver for USB.
34 * Copyright (c) 2003/06, Courage Co., Ltd.
37 * 1.uhci.c by Linus Torvalds, Johannes Erdfelt, Randy Dunlap,
38 * Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber,
39 * Adam Richter, Gregory P. Smith;
40 * 2.Original SL811 driver (hc_sl811.o) by Pei Liu <pbl@cypress.com>
41 * 3.Rewrited as sl811.o by Yin Aihua <yinah:couragetech.com.cn>
43 * [[GNU/GPL disclaimer]]
45 * and in part from AU1x00 OHCI HCD driver "u-boot/cpu/mips/au1x00_usb_ohci.c"
46 * (original copyright message follows):
48 * URB OHCI HCD (Host Controller Driver) for USB on the AU1x00.
51 * Gary Jennejohn, DENX Software Engineering <gj@denx.de>
53 * [[GNU/GPL disclaimer]]
55 * Note: Part of this code has been derived from linux
60 #ifdef CONFIG_USB_ISP116X_HCD
64 #include <linux/list.h>
67 * ISP116x chips require certain delays between accesses to its
68 * registers. The following timing options exist.
70 * 1. Configure your memory controller (the best)
71 * 2. Use ndelay (easiest, poorest). For that, enable the following macro.
73 * Value is in microseconds.
75 #ifdef ISP116X_HCD_USE_UDELAY
80 * On some (slowly?) machines an extra delay after data packing into
81 * controller's FIFOs is required, * otherwise you may get the following
86 * USB: scanning bus for devices... isp116x: isp116x_submit_job: CTL:TIMEOUT
87 * isp116x: isp116x_submit_job: ****** FIFO not ready! ******
89 * USB device not responding, giving up (status=4)
90 * isp116x: isp116x_submit_job: ****** FIFO not empty! ******
91 * isp116x: isp116x_submit_job: ****** FIFO not empty! ******
92 * isp116x: isp116x_submit_job: ****** FIFO not empty! ******
93 * 3 USB Device(s) found
94 * scanning bus for storage devices... 0 Storage Device(s) found
96 * Value is in milliseconds.
98 #ifdef ISP116X_HCD_USE_EXTRA_DELAY
103 * Enable the following defines if you wish enable debugging messages.
105 #undef DEBUG /* enable debugging messages */
106 #undef TRACE /* enable tracing code */
107 #undef VERBOSE /* verbose debugging messages */
111 #define DRIVER_VERSION "08 Jan 2007"
112 static const char hcd_name[] = "isp116x-hcd";
114 struct isp116x isp116x_dev;
115 struct isp116x_platform_data isp116x_board;
116 int got_rhsc = 0; /* root hub status change */
117 struct usb_device *devgone; /* device which was disconnected */
118 int rh_devnum = 0; /* address of Root Hub endpoint */
120 /* ------------------------------------------------------------------------- */
122 #define ALIGN(x,a) (((x)+(a)-1UL)&~((a)-1UL))
123 #define min_t(type,x,y) \
124 ({ type __x = (x); type __y = (y); __x < __y ? __x : __y; })
126 /* ------------------------------------------------------------------------- */
128 static int isp116x_reset(struct isp116x *isp116x);
130 /* --- Debugging functions ------------------------------------------------- */
132 #define isp116x_show_reg(d, r) { \
134 DBG("%-12s[%02x]: %08x", #r, \
135 r, isp116x_read_reg32(d, r)); \
137 DBG("%-12s[%02x]: %04x", #r, \
138 r, isp116x_read_reg16(d, r)); \
142 #define isp116x_show_regs(d) { \
143 isp116x_show_reg(d, HCREVISION); \
144 isp116x_show_reg(d, HCCONTROL); \
145 isp116x_show_reg(d, HCCMDSTAT); \
146 isp116x_show_reg(d, HCINTSTAT); \
147 isp116x_show_reg(d, HCINTENB); \
148 isp116x_show_reg(d, HCFMINTVL); \
149 isp116x_show_reg(d, HCFMREM); \
150 isp116x_show_reg(d, HCFMNUM); \
151 isp116x_show_reg(d, HCLSTHRESH); \
152 isp116x_show_reg(d, HCRHDESCA); \
153 isp116x_show_reg(d, HCRHDESCB); \
154 isp116x_show_reg(d, HCRHSTATUS); \
155 isp116x_show_reg(d, HCRHPORT1); \
156 isp116x_show_reg(d, HCRHPORT2); \
157 isp116x_show_reg(d, HCHWCFG); \
158 isp116x_show_reg(d, HCDMACFG); \
159 isp116x_show_reg(d, HCXFERCTR); \
160 isp116x_show_reg(d, HCuPINT); \
161 isp116x_show_reg(d, HCuPINTENB); \
162 isp116x_show_reg(d, HCCHIPID); \
163 isp116x_show_reg(d, HCSCRATCH); \
164 isp116x_show_reg(d, HCITLBUFLEN); \
165 isp116x_show_reg(d, HCATLBUFLEN); \
166 isp116x_show_reg(d, HCBUFSTAT); \
167 isp116x_show_reg(d, HCRDITL0LEN); \
168 isp116x_show_reg(d, HCRDITL1LEN); \
173 static int isp116x_get_current_frame_number(struct usb_device *usb_dev)
175 struct isp116x *isp116x = &isp116x_dev;
177 return isp116x_read_reg32(isp116x, HCFMNUM);
180 static void dump_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
187 DBG("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d stat:%#lx",
189 isp116x_get_current_frame_number(dev),
190 usb_pipedevice(pipe),
191 usb_pipeendpoint(pipe),
192 usb_pipeout(pipe) ? 'O' : 'I',
193 usb_pipetype(pipe) < 2 ?
196 (usb_pipecontrol(pipe) ? "CTRL" : "BULK"), len, dev->status);
198 if (len > 0 && buffer) {
199 printf(__FILE__ ": data(%d):", len);
200 for (i = 0; i < 16 && i < len; i++)
201 printf(" %02x", ((__u8 *) buffer)[i]);
202 printf("%s\n", i < len ? "..." : "");
207 #define PTD_DIR_STR(ptd) ({char __c; \
208 switch(PTD_GET_DIR(ptd)){ \
209 case 0: __c = 's'; break; \
210 case 1: __c = 'o'; break; \
211 default: __c = 'i'; break; \
215 Dump PTD info. The code documents the format
218 static inline void dump_ptd(struct ptd *ptd)
224 DBG("PTD(ext) : cc:%x %d%c%d %d,%d,%d t:%x %x%x%x",
226 PTD_GET_FA(ptd), PTD_DIR_STR(ptd), PTD_GET_EP(ptd),
227 PTD_GET_COUNT(ptd), PTD_GET_LEN(ptd), PTD_GET_MPS(ptd),
229 PTD_GET_ACTIVE(ptd), PTD_GET_SPD(ptd), PTD_GET_LAST(ptd));
231 printf("isp116x: %s: PTD(byte): ", __FUNCTION__);
232 for (k = 0; k < sizeof(struct ptd); ++k)
233 printf("%02x ", ((u8 *) ptd)[k]);
238 static inline void dump_ptd_data(struct ptd *ptd, u8 * buf, int type)
243 if (type == 0 /* 0ut data */ ) {
244 printf("isp116x: %s: out data: ", __FUNCTION__);
245 for (k = 0; k < PTD_GET_LEN(ptd); ++k)
246 printf("%02x ", ((u8 *) buf)[k]);
249 if (type == 1 /* 1n data */ ) {
250 printf("isp116x: %s: in data: ", __FUNCTION__);
251 for (k = 0; k < PTD_GET_COUNT(ptd); ++k)
252 printf("%02x ", ((u8 *) buf)[k]);
256 if (PTD_GET_LAST(ptd))
257 DBG("--- last PTD ---");
263 #define dump_msg(dev, pipe, buffer, len, str) do { } while (0)
264 #define dump_pkt(dev, pipe, buffer, len, setup, str, small) do {} while (0)
266 #define dump_ptd(ptd) do {} while (0)
267 #define dump_ptd_data(ptd, buf, type) do {} while (0)
271 /* --- Virtual Root Hub ---------------------------------------------------- */
273 /* Device descriptor */
274 static __u8 root_hub_dev_des[] = {
275 0x12, /* __u8 bLength; */
276 0x01, /* __u8 bDescriptorType; Device */
277 0x10, /* __u16 bcdUSB; v1.1 */
279 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
280 0x00, /* __u8 bDeviceSubClass; */
281 0x00, /* __u8 bDeviceProtocol; */
282 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
283 0x00, /* __u16 idVendor; */
285 0x00, /* __u16 idProduct; */
287 0x00, /* __u16 bcdDevice; */
289 0x00, /* __u8 iManufacturer; */
290 0x01, /* __u8 iProduct; */
291 0x00, /* __u8 iSerialNumber; */
292 0x01 /* __u8 bNumConfigurations; */
295 /* Configuration descriptor */
296 static __u8 root_hub_config_des[] = {
297 0x09, /* __u8 bLength; */
298 0x02, /* __u8 bDescriptorType; Configuration */
299 0x19, /* __u16 wTotalLength; */
301 0x01, /* __u8 bNumInterfaces; */
302 0x01, /* __u8 bConfigurationValue; */
303 0x00, /* __u8 iConfiguration; */
304 0x40, /* __u8 bmAttributes;
305 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
306 0x00, /* __u8 MaxPower; */
309 0x09, /* __u8 if_bLength; */
310 0x04, /* __u8 if_bDescriptorType; Interface */
311 0x00, /* __u8 if_bInterfaceNumber; */
312 0x00, /* __u8 if_bAlternateSetting; */
313 0x01, /* __u8 if_bNumEndpoints; */
314 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
315 0x00, /* __u8 if_bInterfaceSubClass; */
316 0x00, /* __u8 if_bInterfaceProtocol; */
317 0x00, /* __u8 if_iInterface; */
320 0x07, /* __u8 ep_bLength; */
321 0x05, /* __u8 ep_bDescriptorType; Endpoint */
322 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
323 0x03, /* __u8 ep_bmAttributes; Interrupt */
324 0x00, /* __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
326 0xff /* __u8 ep_bInterval; 255 ms */
329 static unsigned char root_hub_str_index0[] = {
330 0x04, /* __u8 bLength; */
331 0x03, /* __u8 bDescriptorType; String-descriptor */
332 0x09, /* __u8 lang ID */
333 0x04, /* __u8 lang ID */
336 static unsigned char root_hub_str_index1[] = {
337 0x22, /* __u8 bLength; */
338 0x03, /* __u8 bDescriptorType; String-descriptor */
339 'I', /* __u8 Unicode */
340 0, /* __u8 Unicode */
341 'S', /* __u8 Unicode */
342 0, /* __u8 Unicode */
343 'P', /* __u8 Unicode */
344 0, /* __u8 Unicode */
345 '1', /* __u8 Unicode */
346 0, /* __u8 Unicode */
347 '1', /* __u8 Unicode */
348 0, /* __u8 Unicode */
349 '6', /* __u8 Unicode */
350 0, /* __u8 Unicode */
351 'x', /* __u8 Unicode */
352 0, /* __u8 Unicode */
353 ' ', /* __u8 Unicode */
354 0, /* __u8 Unicode */
355 'R', /* __u8 Unicode */
356 0, /* __u8 Unicode */
357 'o', /* __u8 Unicode */
358 0, /* __u8 Unicode */
359 'o', /* __u8 Unicode */
360 0, /* __u8 Unicode */
361 't', /* __u8 Unicode */
362 0, /* __u8 Unicode */
363 ' ', /* __u8 Unicode */
364 0, /* __u8 Unicode */
365 'H', /* __u8 Unicode */
366 0, /* __u8 Unicode */
367 'u', /* __u8 Unicode */
368 0, /* __u8 Unicode */
369 'b', /* __u8 Unicode */
370 0, /* __u8 Unicode */
374 * Hub class-specific descriptor is constructed dynamically
377 /* --- Virtual root hub management functions ------------------------------- */
379 static int rh_check_port_status(struct isp116x *isp116x)
385 temp = isp116x_read_reg32(isp116x, HCRHSTATUS);
386 ndp = (temp & RH_A_NDP);
387 for (i = 0; i < ndp; i++) {
388 temp = isp116x_read_reg32(isp116x, HCRHPORT1 + i);
389 /* check for a device disconnect */
390 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
391 (RH_PS_PESC | RH_PS_CSC)) && ((temp & RH_PS_CCS) == 0)) {
399 /* --- HC management functions --------------------------------------------- */
401 /* Write len bytes to fifo, pad till 32-bit boundary
403 static void write_ptddata_to_fifo(struct isp116x *isp116x, void *buf, int len)
406 u16 *dp2 = (u16 *) buf;
410 if ((unsigned long)dp2 & 1) {
412 for (; len > 1; len -= 2) {
415 isp116x_raw_write_data16(isp116x, w);
418 isp116x_write_data16(isp116x, (u16) * dp);
421 for (; len > 1; len -= 2)
422 isp116x_raw_write_data16(isp116x, *dp2++);
424 isp116x_write_data16(isp116x, 0xff & *((u8 *) dp2));
426 if (quot == 1 || quot == 2)
427 isp116x_raw_write_data16(isp116x, 0);
430 /* Read len bytes from fifo and then read till 32-bit boundary
432 static void read_ptddata_from_fifo(struct isp116x *isp116x, void *buf, int len)
435 u16 *dp2 = (u16 *) buf;
439 if ((unsigned long)dp2 & 1) {
441 for (; len > 1; len -= 2) {
442 w = isp116x_raw_read_data16(isp116x);
444 *dp++ = (w >> 8) & 0xff;
447 *dp = 0xff & isp116x_read_data16(isp116x);
450 for (; len > 1; len -= 2)
451 *dp2++ = isp116x_raw_read_data16(isp116x);
453 *(u8 *) dp2 = 0xff & isp116x_read_data16(isp116x);
455 if (quot == 1 || quot == 2)
456 isp116x_raw_read_data16(isp116x);
459 /* Write PTD's and data for scheduled transfers into the fifo ram.
460 * Fifo must be empty and ready */
461 static void pack_fifo(struct isp116x *isp116x, struct usb_device *dev,
462 unsigned long pipe, struct ptd *ptd, int n, void *data,
465 int buflen = n * sizeof(struct ptd) + len;
468 DBG("--- pack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
470 isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
471 isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
472 isp116x_write_addr(isp116x, HCATLPORT | ISP116x_WRITE_OFFSET);
475 for (i = 0; i < n; i++) {
476 DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
479 isp116x_write_data16(isp116x, ptd[i].count);
480 isp116x_write_data16(isp116x, ptd[i].mps);
481 isp116x_write_data16(isp116x, ptd[i].len);
482 isp116x_write_data16(isp116x, ptd[i].faddr);
484 dump_ptd_data(&ptd[i], (__u8 *) data + done, 0);
485 write_ptddata_to_fifo(isp116x,
486 (__u8 *) data + done,
487 PTD_GET_LEN(&ptd[i]));
489 done += PTD_GET_LEN(&ptd[i]);
493 /* Read the processed PTD's and data from fifo ram back to URBs' buffers.
494 * Fifo must be full and done */
495 static int unpack_fifo(struct isp116x *isp116x, struct usb_device *dev,
496 unsigned long pipe, struct ptd *ptd, int n, void *data,
499 int buflen = n * sizeof(struct ptd) + len;
500 int i, done, cc, ret;
502 isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
503 isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
504 isp116x_write_addr(isp116x, HCATLPORT);
508 for (i = 0; i < n; i++) {
509 DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
511 ptd[i].count = isp116x_read_data16(isp116x);
512 ptd[i].mps = isp116x_read_data16(isp116x);
513 ptd[i].len = isp116x_read_data16(isp116x);
514 ptd[i].faddr = isp116x_read_data16(isp116x);
517 read_ptddata_from_fifo(isp116x,
518 (__u8 *) data + done,
519 PTD_GET_LEN(&ptd[i]));
520 dump_ptd_data(&ptd[i], (__u8 *) data + done, 1);
522 done += PTD_GET_LEN(&ptd[i]);
524 cc = PTD_GET_CC(&ptd[i]);
525 if (cc == TD_DATAUNDERRUN) { /* underrun is no error... */
526 DBG("allowed data underrun");
529 if (cc != TD_CC_NOERROR && ret == TD_CC_NOERROR)
533 DBG("--- unpack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
538 /* Interrupt handling
540 static int isp116x_interrupt(struct isp116x *isp116x)
546 isp116x_write_reg16(isp116x, HCuPINTENB, 0);
547 irqstat = isp116x_read_reg16(isp116x, HCuPINT);
548 isp116x_write_reg16(isp116x, HCuPINT, irqstat);
549 DBG(">>>>>> irqstat %x <<<<<<", irqstat);
551 if (irqstat & HCuPINT_ATL) {
552 DBG(">>>>>> HCuPINT_ATL <<<<<<");
556 if (irqstat & HCuPINT_OPR) {
557 intstat = isp116x_read_reg32(isp116x, HCINTSTAT);
558 isp116x_write_reg32(isp116x, HCINTSTAT, intstat);
559 DBG(">>>>>> HCuPINT_OPR %x <<<<<<", intstat);
561 if (intstat & HCINT_UE) {
562 ERR("unrecoverable error, controller disabled");
564 /* FIXME: be optimistic, hope that bug won't repeat
565 * often. Make some non-interrupt context restart the
566 * controller. Count and limit the retries though;
567 * either hardware or software errors can go forever...
569 isp116x_reset(isp116x);
574 if (intstat & HCINT_RHSC) {
577 /* When root hub or any of its ports is going
578 to come out of suspend, it may take more
579 than 10ms for status bits to stabilize. */
583 if (intstat & HCINT_SO) {
584 ERR("schedule overrun");
588 irqstat &= ~HCuPINT_OPR;
594 #define PTD_NUM 64 /* it should be enougth... */
595 struct ptd ptd[PTD_NUM];
596 static inline int max_transfer_len(struct usb_device *dev, unsigned long pipe)
598 return min(PTD_NUM * usb_maxpacket(dev, pipe), PTD_NUM * 16);
601 /* Do an USB transfer
603 static int isp116x_submit_job(struct usb_device *dev, unsigned long pipe,
604 int dir, void *buffer, int len)
606 struct isp116x *isp116x = &isp116x_dev;
607 int type = usb_pipetype(pipe);
608 int epnum = usb_pipeendpoint(pipe);
609 int max = usb_maxpacket(dev, pipe);
610 int dir_out = usb_pipeout(pipe);
611 int speed_low = usb_pipeslow(pipe);
612 int i, done, stat, timeout, cc;
615 DBG("------------------------------------------------");
616 dump_msg(dev, pipe, buffer, len, "SUBMIT");
617 DBG("------------------------------------------------");
619 if (isp116x->disabled) {
621 dev->status = USB_ST_CRC_ERR;
625 /* device pulled? Shortcut the action. */
626 if (devgone == dev) {
628 dev->status = USB_ST_CRC_ERR;
629 return USB_ST_CRC_ERR;
633 ERR("pipesize for pipe %lx is zero", pipe);
634 dev->status = USB_ST_CRC_ERR;
638 if (type == PIPE_ISOCHRONOUS) {
639 ERR("isochronous transfers not supported");
640 dev->status = USB_ST_CRC_ERR;
644 /* FIFO not empty? */
645 if (isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_FULL) {
646 ERR("****** FIFO not empty! ******");
647 dev->status = USB_ST_BUF_ERR;
652 isp116x_write_reg32(isp116x, HCINTSTAT, 0xff);
654 /* Prepare the PTD data */
658 ptd[i].count = PTD_CC_MSK | PTD_ACTIVE_MSK |
659 PTD_TOGGLE(usb_gettoggle(dev, epnum, dir_out));
660 ptd[i].mps = PTD_MPS(max) | PTD_SPD(speed_low) | PTD_EP(epnum);
661 ptd[i].len = PTD_LEN(max > len - done ? len - done : max) |
663 ptd[i].faddr = PTD_FA(usb_pipedevice(pipe));
665 usb_dotoggle(dev, epnum, dir_out);
666 done += PTD_GET_LEN(&ptd[i]);
669 ERR("****** Cannot pack buffer! ******");
670 dev->status = USB_ST_BUF_ERR;
673 } while (done < len);
674 ptd[i - 1].mps |= PTD_LAST_MSK;
676 /* Pack data into FIFO ram */
677 pack_fifo(isp116x, dev, pipe, ptd, i, buffer, len);
679 wait_ms(EXTRA_DELAY);
682 /* Start the data transfer */
684 /* Allow more time for a BULK device to react - some are slow */
685 if (usb_pipetype(pipe) == PIPE_BULK)
690 /* Wait for it to complete */
692 /* Check whether the controller is done */
693 stat = isp116x_interrupt(isp116x);
696 dev->status = USB_ST_CRC_ERR;
702 /* Check the timeout */
707 stat = USB_ST_CRC_ERR;
712 /* We got an Root Hub Status Change interrupt */
714 isp116x_show_regs(isp116x);
719 timeout = rh_check_port_status(isp116x);
722 * FIXME! NOTE! AAAARGH!
723 * This is potentially dangerous because it assumes
724 * that only one device is ever plugged in!
730 /* Ok, now we can read transfer status */
732 /* FIFO not ready? */
733 if (!(isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_DONE)) {
734 ERR("****** FIFO not ready! ******");
735 dev->status = USB_ST_BUF_ERR;
739 /* Unpack data from FIFO ram */
740 cc = unpack_fifo(isp116x, dev, pipe, ptd, i, buffer, len);
742 /* Mmm... sometime we get 0x0f as cc which is a non sense!
743 * Just retry the transfer...
745 if (cc == 0x0f && retries-- > 0) {
746 usb_dotoggle(dev, epnum, dir_out);
750 if (cc != TD_CC_NOERROR) {
751 DBG("****** completition code error %x ******", cc);
753 case TD_CC_BITSTUFFING:
754 dev->status = USB_ST_BIT_ERR;
757 dev->status = USB_ST_STALLED;
759 case TD_BUFFEROVERRUN:
760 case TD_BUFFERUNDERRUN:
761 dev->status = USB_ST_BUF_ERR;
764 dev->status = USB_ST_CRC_ERR;
769 dump_msg(dev, pipe, buffer, len, "SUBMIT(ret)");
775 /* Adapted from au1x00_usb_ohci.c
777 static int isp116x_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
778 void *buffer, int transfer_len,
779 struct devrequest *cmd)
781 struct isp116x *isp116x = &isp116x_dev;
784 int leni = transfer_len;
788 u8 *data_buf = (u8 *) datab;
794 if ((pipe & PIPE_INTERRUPT) == PIPE_INTERRUPT) {
795 INFO("Root-Hub submit IRQ: NOT implemented");
799 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
800 wValue = swap_16(cmd->value);
801 wIndex = swap_16(cmd->index);
802 wLength = swap_16(cmd->length);
804 DBG("--- HUB ----------------------------------------");
805 DBG("submit rh urb, req=%x val=%#x index=%#x len=%d",
806 bmRType_bReq, wValue, wIndex, wLength);
807 dump_msg(dev, pipe, buffer, transfer_len, "RH");
808 DBG("------------------------------------------------");
810 switch (bmRType_bReq) {
812 DBG("RH_GET_STATUS");
814 *(__u16 *) data_buf = swap_16(1);
818 case RH_GET_STATUS | RH_INTERFACE:
819 DBG("RH_GET_STATUS | RH_INTERFACE");
821 *(__u16 *) data_buf = swap_16(0);
825 case RH_GET_STATUS | RH_ENDPOINT:
826 DBG("RH_GET_STATUS | RH_ENDPOINT");
828 *(__u16 *) data_buf = swap_16(0);
832 case RH_GET_STATUS | RH_CLASS:
833 DBG("RH_GET_STATUS | RH_CLASS");
835 tmp = isp116x_read_reg32(isp116x, HCRHSTATUS);
837 *(__u32 *) data_buf = swap_32(tmp & ~(RH_HS_CRWE | RH_HS_DRWE));
841 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
842 DBG("RH_GET_STATUS | RH_OTHER | RH_CLASS");
844 tmp = isp116x_read_reg32(isp116x, HCRHPORT1 + wIndex - 1);
845 *(__u32 *) data_buf = swap_32(tmp);
846 isp116x_show_regs(isp116x);
850 case RH_CLEAR_FEATURE | RH_ENDPOINT:
851 DBG("RH_CLEAR_FEATURE | RH_ENDPOINT");
854 case RH_ENDPOINT_STALL:
855 DBG("C_HUB_ENDPOINT_STALL");
861 case RH_CLEAR_FEATURE | RH_CLASS:
862 DBG("RH_CLEAR_FEATURE | RH_CLASS");
865 case RH_C_HUB_LOCAL_POWER:
866 DBG("C_HUB_LOCAL_POWER");
870 case RH_C_HUB_OVER_CURRENT:
871 DBG("C_HUB_OVER_CURRENT");
872 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_OCIC);
878 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
879 DBG("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS");
883 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
888 case RH_PORT_SUSPEND:
889 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
895 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
900 case RH_C_PORT_CONNECTION:
901 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
906 case RH_C_PORT_ENABLE:
907 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
912 case RH_C_PORT_SUSPEND:
913 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
918 case RH_C_PORT_OVER_CURRENT:
919 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
924 case RH_C_PORT_RESET:
925 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
931 ERR("invalid wValue");
932 stat = USB_ST_STALLED;
935 isp116x_show_regs(isp116x);
939 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
940 DBG("RH_SET_FEATURE | RH_OTHER | RH_CLASS");
943 case RH_PORT_SUSPEND:
944 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
950 /* Spin until any current reset finishes */
953 isp116x_read_reg32(isp116x,
954 HCRHPORT1 + wIndex - 1);
955 if (!(tmp & RH_PS_PRS))
959 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
967 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
973 isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
979 ERR("invalid wValue");
980 stat = USB_ST_STALLED;
983 isp116x_show_regs(isp116x);
988 DBG("RH_SET_ADDRESS");
994 case RH_GET_DESCRIPTOR:
995 DBG("RH_GET_DESCRIPTOR: %x, %d", wValue, wLength);
998 case (USB_DT_DEVICE << 8): /* device descriptor */
999 len = min_t(unsigned int,
1000 leni, min_t(unsigned int,
1001 sizeof(root_hub_dev_des),
1003 data_buf = root_hub_dev_des;
1006 case (USB_DT_CONFIG << 8): /* configuration descriptor */
1007 len = min_t(unsigned int,
1008 leni, min_t(unsigned int,
1009 sizeof(root_hub_config_des),
1011 data_buf = root_hub_config_des;
1014 case ((USB_DT_STRING << 8) | 0x00): /* string 0 descriptors */
1015 len = min_t(unsigned int,
1016 leni, min_t(unsigned int,
1017 sizeof(root_hub_str_index0),
1019 data_buf = root_hub_str_index0;
1022 case ((USB_DT_STRING << 8) | 0x01): /* string 1 descriptors */
1023 len = min_t(unsigned int,
1024 leni, min_t(unsigned int,
1025 sizeof(root_hub_str_index1),
1027 data_buf = root_hub_str_index1;
1031 ERR("invalid wValue");
1032 stat = USB_ST_STALLED;
1037 case RH_GET_DESCRIPTOR | RH_CLASS:
1038 DBG("RH_GET_DESCRIPTOR | RH_CLASS");
1040 tmp = isp116x_read_reg32(isp116x, HCRHDESCA);
1042 data_buf[0] = 0x09; /* min length; */
1044 data_buf[2] = tmp & RH_A_NDP;
1046 if (tmp & RH_A_PSM) /* per-port power switching? */
1047 data_buf[3] |= 0x01;
1048 if (tmp & RH_A_NOCP) /* no overcurrent reporting? */
1049 data_buf[3] |= 0x10;
1050 else if (tmp & RH_A_OCPM) /* per-port overcurrent rep? */
1051 data_buf[3] |= 0x08;
1053 /* Corresponds to data_buf[4-7] */
1055 data_buf[5] = (tmp & RH_A_POTPGT) >> 24;
1057 tmp = isp116x_read_reg32(isp116x, HCRHDESCB);
1059 data_buf[7] = tmp & RH_B_DR;
1060 if (data_buf[2] < 7)
1064 data_buf[8] = (tmp & RH_B_DR) >> 8;
1065 data_buf[10] = data_buf[9] = 0xff;
1068 len = min_t(unsigned int, leni,
1069 min_t(unsigned int, data_buf[0], wLength));
1072 case RH_GET_CONFIGURATION:
1073 DBG("RH_GET_CONFIGURATION");
1075 *(__u8 *) data_buf = 0x01;
1079 case RH_SET_CONFIGURATION:
1080 DBG("RH_SET_CONFIGURATION");
1082 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPSC);
1087 ERR("*** *** *** unsupported root hub command *** *** ***");
1088 stat = USB_ST_STALLED;
1091 len = min_t(int, len, leni);
1092 if (buffer != data_buf)
1093 memcpy(buffer, data_buf, len);
1097 DBG("dev act_len %d, status %d", dev->act_len, dev->status);
1099 dump_msg(dev, pipe, buffer, transfer_len, "RH(ret)");
1104 /* --- Transfer functions -------------------------------------------------- */
1106 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1107 int len, int interval)
1109 DBG("dev=%p pipe=%#lx buf=%p size=%d int=%d",
1110 dev, pipe, buffer, len, interval);
1115 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1116 int len, struct devrequest *setup)
1118 int devnum = usb_pipedevice(pipe);
1119 int epnum = usb_pipeendpoint(pipe);
1120 int max = max_transfer_len(dev, pipe);
1121 int dir_in = usb_pipein(pipe);
1124 /* Control message is for the HUB? */
1125 if (devnum == rh_devnum)
1126 return isp116x_submit_rh_msg(dev, pipe, buffer, len, setup);
1128 /* Ok, no HUB message so send the message to the device */
1131 DBG("--- SETUP PHASE --------------------------------");
1132 usb_settoggle(dev, epnum, 1, 0);
1133 ret = isp116x_submit_job(dev, pipe,
1135 setup, sizeof(struct devrequest));
1137 DBG("control setup phase error (ret = %d", ret);
1142 DBG("--- DATA PHASE ---------------------------------");
1144 usb_settoggle(dev, epnum, !dir_in, 1);
1145 while (done < len) {
1146 ret = isp116x_submit_job(dev, pipe,
1147 dir_in ? PTD_DIR_IN : PTD_DIR_OUT,
1148 (__u8 *) buffer + done,
1149 max > len - done ? len - done : max);
1151 DBG("control data phase error (ret = %d)", ret);
1156 if (dir_in && ret < max) /* short packet */
1161 DBG("--- STATUS PHASE -------------------------------");
1162 usb_settoggle(dev, epnum, !dir_in, 1);
1163 ret = isp116x_submit_job(dev, pipe,
1164 !dir_in ? PTD_DIR_IN : PTD_DIR_OUT, NULL, 0);
1166 DBG("control status phase error (ret = %d", ret);
1170 dev->act_len = done;
1172 dump_msg(dev, pipe, buffer, len, "DEV(ret)");
1177 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1180 int dir_out = usb_pipeout(pipe);
1181 int max = max_transfer_len(dev, pipe);
1184 DBG("--- BULK ---------------------------------------");
1185 DBG("dev=%ld pipe=%ld buf=%p size=%d dir_out=%d",
1186 usb_pipedevice(pipe), usb_pipeendpoint(pipe), buffer, len, dir_out);
1189 while (done < len) {
1190 ret = isp116x_submit_job(dev, pipe,
1191 !dir_out ? PTD_DIR_IN : PTD_DIR_OUT,
1192 (__u8 *) buffer + done,
1193 max > len - done ? len - done : max);
1195 DBG("error on bulk message (ret = %d)", ret);
1201 if (!dir_out && ret < max) /* short packet */
1205 dev->act_len = done;
1210 /* --- Basic functions ----------------------------------------------------- */
1212 static int isp116x_sw_reset(struct isp116x *isp116x)
1219 isp116x->disabled = 1;
1221 isp116x_write_reg16(isp116x, HCSWRES, HCSWRES_MAGIC);
1222 isp116x_write_reg32(isp116x, HCCMDSTAT, HCCMDSTAT_HCR);
1224 /* It usually resets within 1 ms */
1226 if (!(isp116x_read_reg32(isp116x, HCCMDSTAT) & HCCMDSTAT_HCR))
1230 ERR("software reset timeout");
1236 static int isp116x_reset(struct isp116x *isp116x)
1240 int ret, timeout = 15 /* ms */ ;
1244 ret = isp116x_sw_reset(isp116x);
1248 for (t = 0; t < timeout; t++) {
1249 clkrdy = isp116x_read_reg16(isp116x, HCuPINT) & HCuPINT_CLKRDY;
1255 ERR("clock not ready after %dms", timeout);
1256 /* After sw_reset the clock won't report to be ready, if
1257 H_WAKEUP pin is high. */
1258 ERR("please make sure that the H_WAKEUP pin is pulled low!");
1264 static void isp116x_stop(struct isp116x *isp116x)
1270 isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1272 /* Switch off ports' power, some devices don't come up
1273 after next 'start' without this */
1274 val = isp116x_read_reg32(isp116x, HCRHDESCA);
1275 val &= ~(RH_A_NPS | RH_A_PSM);
1276 isp116x_write_reg32(isp116x, HCRHDESCA, val);
1277 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPS);
1279 isp116x_sw_reset(isp116x);
1283 * Configure the chip. The chip must be successfully reset by now.
1285 static int isp116x_start(struct isp116x *isp116x)
1287 struct isp116x_platform_data *board = isp116x->board;
1292 /* Clear interrupt status and disable all interrupt sources */
1293 isp116x_write_reg16(isp116x, HCuPINT, 0xff);
1294 isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1296 isp116x_write_reg16(isp116x, HCITLBUFLEN, ISP116x_ITL_BUFSIZE);
1297 isp116x_write_reg16(isp116x, HCATLBUFLEN, ISP116x_ATL_BUFSIZE);
1299 /* Hardware configuration */
1300 val = HCHWCFG_DBWIDTH(1);
1301 if (board->sel15Kres)
1302 val |= HCHWCFG_15KRSEL;
1303 /* Remote wakeup won't work without working clock */
1304 if (board->remote_wakeup_enable)
1305 val |= HCHWCFG_CLKNOTSTOP;
1306 if (board->oc_enable)
1307 val |= HCHWCFG_ANALOG_OC;
1308 isp116x_write_reg16(isp116x, HCHWCFG, val);
1310 /* --- Root hub configuration */
1311 val = (25 << 24) & RH_A_POTPGT;
1312 /* AN10003_1.pdf recommends RH_A_NPS (no power switching) to
1313 be always set. Yet, instead, we request individual port
1316 /* Report overcurrent per port */
1318 isp116x_write_reg32(isp116x, HCRHDESCA, val);
1319 isp116x->rhdesca = isp116x_read_reg32(isp116x, HCRHDESCA);
1322 isp116x_write_reg32(isp116x, HCRHDESCB, val);
1323 isp116x->rhdescb = isp116x_read_reg32(isp116x, HCRHDESCB);
1326 if (board->remote_wakeup_enable)
1328 isp116x_write_reg32(isp116x, HCRHSTATUS, val);
1329 isp116x->rhstatus = isp116x_read_reg32(isp116x, HCRHSTATUS);
1331 isp116x_write_reg32(isp116x, HCFMINTVL, 0x27782edf);
1333 /* Go operational */
1334 val = HCCONTROL_USB_OPER;
1335 if (board->remote_wakeup_enable)
1336 val |= HCCONTROL_RWE;
1337 isp116x_write_reg32(isp116x, HCCONTROL, val);
1339 /* Disable ports to avoid race in device enumeration */
1340 isp116x_write_reg32(isp116x, HCRHPORT1, RH_PS_CCS);
1341 isp116x_write_reg32(isp116x, HCRHPORT2, RH_PS_CCS);
1343 isp116x_show_regs(isp116x);
1345 isp116x->disabled = 0;
1350 /* --- Init functions ------------------------------------------------------ */
1352 int isp116x_check_id(struct isp116x *isp116x)
1356 val = isp116x_read_reg16(isp116x, HCCHIPID);
1357 if ((val & HCCHIPID_MASK) != HCCHIPID_MAGIC) {
1358 ERR("invalid chip ID %04x", val);
1365 int usb_lowlevel_init(void)
1367 struct isp116x *isp116x = &isp116x_dev;
1371 /* Init device registers addr */
1372 isp116x->addr_reg = (u16 *) ISP116X_HCD_ADDR;
1373 isp116x->data_reg = (u16 *) ISP116X_HCD_DATA;
1375 /* Setup specific board settings */
1376 #ifdef ISP116X_HCD_SEL15kRES
1377 isp116x_board.sel15Kres = 1;
1379 #ifdef ISP116X_HCD_OC_ENABLE
1380 isp116x_board.oc_enable = 1;
1382 #ifdef ISP116X_HCD_REMOTE_WAKEUP_ENABLE
1383 isp116x_board.remote_wakeup_enable = 1;
1385 isp116x->board = &isp116x_board;
1387 /* Try to get ISP116x silicon chip ID */
1388 if (isp116x_check_id(isp116x) < 0)
1391 isp116x->disabled = 1;
1392 isp116x->sleeping = 0;
1394 isp116x_reset(isp116x);
1395 isp116x_start(isp116x);
1400 int usb_lowlevel_stop(void)
1402 struct isp116x *isp116x = &isp116x_dev;
1406 if (!isp116x->disabled)
1407 isp116x_stop(isp116x);
1412 #endif /* CONFIG_USB_ISP116X_HCD */