3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * This code is based on linux driver for sl811hs chip, source at
6 * drivers/usb/host/sl811.c:
8 * SL811 Host Controller Interface driver for USB.
10 * Copyright (c) 2003/06, Courage Co., Ltd.
13 * 1.uhci.c by Linus Torvalds, Johannes Erdfelt, Randy Dunlap,
14 * Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber,
15 * Adam Richter, Gregory P. Smith;
16 * 2.Original SL811 driver (hc_sl811.o) by Pei Liu <pbl@cypress.com>
17 * 3.Rewrited as sl811.o by Yin Aihua <yinah:couragetech.com.cn>
19 * See file CREDITS for list of people who contributed to this
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License as
24 * published by the Free Software Foundation; either version 2 of
25 * the License, or (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
39 #ifdef CONFIG_USB_SL811HS
44 #include "../board/kup/common/kup.h"
47 # define EIEIO __asm__ volatile ("eieio")
49 # define EIEIO /* nothing */
52 #define SL811_ADR (0x50000000)
53 #define SL811_DAT (0x50000001)
55 #define mdelay(n) ({unsigned long msec=(n); while (msec--) udelay(1000);})
61 static int root_hub_devnum = 0;
62 static struct usb_port_status rh_status = { 0 };/* root hub port status */
64 static int sl811_rh_submit_urb(struct usb_device *usb_dev, unsigned long pipe,
65 void *data, int buf_len, struct devrequest *cmd);
67 static void sl811_write (__u8 index, __u8 data)
69 *(volatile unsigned char *) (SL811_ADR) = index;
71 *(volatile unsigned char *) (SL811_DAT) = data;
75 static __u8 sl811_read (__u8 index)
79 *(volatile unsigned char *) (SL811_ADR) = index;
81 data = *(volatile unsigned char *) (SL811_DAT);
87 * Read consecutive bytes of data from the SL811H/SL11H buffer
89 static void inline sl811_read_buf(__u8 offset, __u8 *buf, __u8 size)
91 *(volatile unsigned char *) (SL811_ADR) = offset;
94 *buf++ = *(volatile unsigned char *) (SL811_DAT);
100 * Write consecutive bytes of data to the SL811H/SL11H buffer
102 static void inline sl811_write_buf(__u8 offset, __u8 *buf, __u8 size)
104 *(volatile unsigned char *) (SL811_ADR) = offset;
107 *(volatile unsigned char *) (SL811_DAT) = *buf++;
112 int usb_init_kup4x (void)
114 volatile immap_t *immap = (immap_t *) CFG_IMMR;
115 volatile memctl8xx_t *memctl = &immap->im_memctl;
119 memctl = &immap->im_memctl;
120 memctl->memc_or7 = 0xFFFF8726;
121 memctl->memc_br7 = 0x50000401; /* start at 0x50000000 */
122 /* BP 14 low = USB ON */
123 immap->im_cpm.cp_pbdat &= ~(BP_USB_VCC);
124 /* PB 14 nomal port */
125 immap->im_cpm.cp_pbpar &= ~(BP_USB_VCC);
127 immap->im_cpm.cp_pbdir |= (BP_USB_VCC);
131 for (i = 0x10; i < 0xff; i++) {
133 tmp = (sl811_read(i));
135 printf ("SL811 compare error index=0x%02x read=0x%02x\n", i, tmp);
139 printf ("SL811 ready\n");
144 * This function resets SL811HS controller and detects the speed of
145 * the connecting device
147 * Return: 0 = no device attached; 1 = USB device attached
149 static int sl811_hc_reset(void)
153 sl811_write(SL811_CTRL2, SL811_CTL2_HOST | SL811_12M_HI);
154 sl811_write(SL811_CTRL1, SL811_CTRL1_RESET);
158 /* Disable hardware SOF generation, clear all irq status. */
159 sl811_write(SL811_CTRL1, 0);
161 sl811_write(SL811_INTRSTS, 0xff);
162 status = sl811_read(SL811_INTRSTS);
164 if (status & SL811_INTR_NOTPRESENT) {
165 /* Device is not present */
166 PDEBUG(0, "Device not present\n");
167 rh_status.wPortStatus &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE);
168 rh_status.wPortChange |= USB_PORT_STAT_C_CONNECTION;
169 sl811_write(SL811_INTR, SL811_INTR_INSRMV);
173 /* Send SOF to address 0, endpoint 0. */
174 sl811_write(SL811_LEN_B, 0);
175 sl811_write(SL811_PIDEP_B, PIDEP(USB_PID_SOF, 0));
176 sl811_write(SL811_DEV_B, 0x00);
177 sl811_write(SL811_SOFLOW, SL811_12M_LOW);
179 if (status & SL811_INTR_SPEED_FULL) {
180 /* full speed device connect directly to root hub */
181 PDEBUG (0, "Full speed Device attached\n");
183 sl811_write(SL811_CTRL1, SL811_CTRL1_RESET);
185 sl811_write(SL811_CTRL2, SL811_CTL2_HOST | SL811_12M_HI);
186 sl811_write(SL811_CTRL1, SL811_CTRL1_SOF);
188 /* start the SOF or EOP */
189 sl811_write(SL811_CTRL_B, SL811_USB_CTRL_ARM);
190 rh_status.wPortStatus |= USB_PORT_STAT_CONNECTION;
191 rh_status.wPortStatus &= ~USB_PORT_STAT_LOW_SPEED;
193 sl811_write(SL811_INTRSTS, 0xff);
195 /* slow speed device connect directly to root-hub */
196 PDEBUG(0, "Low speed Device attached\n");
198 sl811_write(SL811_CTRL1, SL811_CTRL1_RESET);
200 sl811_write(SL811_CTRL2, SL811_CTL2_HOST | SL811_CTL2_DSWAP | SL811_12M_HI);
201 sl811_write(SL811_CTRL1, SL811_CTRL1_SPEED_LOW | SL811_CTRL1_SOF);
203 /* start the SOF or EOP */
204 sl811_write(SL811_CTRL_B, SL811_USB_CTRL_ARM);
205 rh_status.wPortStatus |= USB_PORT_STAT_CONNECTION | USB_PORT_STAT_LOW_SPEED;
207 sl811_write(SL811_INTRSTS, 0xff);
210 rh_status.wPortChange |= USB_PORT_STAT_C_CONNECTION;
211 sl811_write(SL811_INTR, /*SL811_INTR_INSRMV*/SL811_INTR_DONE_A);
216 int usb_lowlevel_init(void)
223 int usb_lowlevel_stop(void)
229 static int calc_needed_buswidth(int bytes, int low_speed)
231 return bytes * 8 + 512;
234 static int sl811_send_packet(int dir_to_host, int data1, __u8 *buffer, int len)
236 __u8 ctrl = SL811_USB_CTRL_ARM | SL811_USB_CTRL_ENABLE;
238 int err = 0, timeout = get_timer(0) + 5*CFG_HZ;
244 ctrl |= SL811_USB_CTRL_DIR_OUT;
246 ctrl |= SL811_USB_CTRL_TOGGLE_1;
248 sl811_write(SL811_INTRSTS, 0xff);
251 sl811_write(SL811_ADDR_A, 0x10);
252 sl811_write(SL811_LEN_A, len);
253 if (!dir_to_host && len)
254 sl811_write_buf(0x10, buffer, len);
256 if (sl811_read(SL811_SOFCNTDIV)*64 < calc_needed_buswidth(len, 0))
257 ctrl |= SL811_USB_CTRL_SOF;
259 ctrl &= ~SL811_USB_CTRL_SOF;
261 sl811_write(SL811_CTRL_A, ctrl);
262 while (!(sl811_read(SL811_INTRSTS) & SL811_INTR_DONE_A)) {
263 if (timeout < get_timer(0)) {
264 printf("USB transmit timed out\n");
265 return -USB_ST_CRC_ERR;
269 sl811_write(SL811_INTRSTS, 0xff);
270 status = sl811_read(SL811_STS_A);
272 if (status & SL811_USB_STS_ACK) {
273 int remainder = sl811_read(SL811_CNT_A);
275 PDEBUG(0, "usb transfer remainder = %d\n", remainder);
278 if (dir_to_host && len)
279 sl811_read_buf(0x10, buffer, len);
283 if ((status & SL811_USB_STS_NAK) == SL811_USB_STS_NAK)
286 PDEBUG(0, "usb transfer error %#x\n", (int)status);
292 if (status & SL811_USB_STS_ERROR)
293 err |= USB_ST_BUF_ERR;
294 if (status & SL811_USB_STS_TIMEOUT)
295 err |= USB_ST_CRC_ERR;
296 if (status & SL811_USB_STS_STALL)
297 err |= USB_ST_STALLED;
302 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
305 int dir_out = usb_pipeout(pipe);
306 int ep = usb_pipeendpoint(pipe);
307 __u8* buf = (__u8*)buffer;
308 int max = usb_maxpacket(dev, pipe);
311 PDEBUG(7, "dev = %ld pipe = %ld buf = %p size = %d dir_out = %d\n",
312 usb_pipedevice(pipe), usb_pipeendpoint(pipe), buffer, len, dir_out);
316 sl811_write(SL811_DEV_A, usb_pipedevice(pipe));
317 sl811_write(SL811_PIDEP_A, PIDEP(!dir_out ? USB_PID_IN : USB_PID_OUT, ep));
319 int res = sl811_send_packet(!dir_out, usb_gettoggle(dev, ep, dir_out),
321 max > len - done ? len - done : max);
327 if (!dir_out && res < max) /* short packet */
331 usb_dotoggle(dev, ep, dir_out);
339 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
340 int len,struct devrequest *setup)
343 int devnum = usb_pipedevice(pipe);
347 if (devnum == root_hub_devnum)
348 return sl811_rh_submit_urb(dev, pipe, buffer, len, setup);
350 PDEBUG(7, "dev = %d pipe = %ld buf = %p size = %d rt = %#x req = %#x\n",
351 devnum, usb_pipeendpoint(pipe), buffer, len, (int)setup->requesttype,
352 (int)setup->request);
354 sl811_write(SL811_DEV_A, devnum);
355 sl811_write(SL811_PIDEP_A, PIDEP(USB_PID_SETUP, 0));
357 if (sl811_send_packet(0, 0, (__u8*)setup, sizeof(*setup)) == sizeof(*setup)) {
358 int dir_in = setup->requesttype & USB_DIR_IN;
359 __u8* buf = (__u8*)buffer;
361 int max = usb_maxpacket(dev, pipe);
364 sl811_write(SL811_PIDEP_A,
365 PIDEP(dir_in ? USB_PID_IN : USB_PID_OUT, 0));
367 int res = sl811_send_packet(dir_in, data1, buf+done,
368 max > len - done ? len - done : max);
375 if (dir_in && res < max) /* short packet */
382 sl811_write(SL811_PIDEP_A,
383 PIDEP(!dir_in ? USB_PID_IN : USB_PID_OUT, 0));
384 if (sl811_send_packet(!dir_in, 1, 0, 0) < 0) {
385 PDEBUG(0, "status phase failed!\n");
389 PDEBUG(0, "setup phase failed!\n");
398 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
399 int len, int interval)
401 PDEBUG(0, "dev = %p pipe = %#lx buf = %p size = %d int = %d\n", dev, pipe,
402 buffer, len, interval);
407 * SL811 Virtual Root Hub
410 /* Device descriptor */
411 static __u8 sl811_rh_dev_des[] =
413 0x12, /* __u8 bLength; */
414 0x01, /* __u8 bDescriptorType; Device */
415 0x10, /* __u16 bcdUSB; v1.1 */
417 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
418 0x00, /* __u8 bDeviceSubClass; */
419 0x00, /* __u8 bDeviceProtocol; */
420 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
421 0x00, /* __u16 idVendor; */
423 0x00, /* __u16 idProduct; */
425 0x00, /* __u16 bcdDevice; */
427 0x00, /* __u8 iManufacturer; */
428 0x02, /* __u8 iProduct; */
429 0x01, /* __u8 iSerialNumber; */
430 0x01 /* __u8 bNumConfigurations; */
433 /* Configuration descriptor */
434 static __u8 sl811_rh_config_des[] =
436 0x09, /* __u8 bLength; */
437 0x02, /* __u8 bDescriptorType; Configuration */
438 0x19, /* __u16 wTotalLength; */
440 0x01, /* __u8 bNumInterfaces; */
441 0x01, /* __u8 bConfigurationValue; */
442 0x00, /* __u8 iConfiguration; */
443 0x40, /* __u8 bmAttributes;
444 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup,
446 0x00, /* __u8 MaxPower; */
449 0x09, /* __u8 if_bLength; */
450 0x04, /* __u8 if_bDescriptorType; Interface */
451 0x00, /* __u8 if_bInterfaceNumber; */
452 0x00, /* __u8 if_bAlternateSetting; */
453 0x01, /* __u8 if_bNumEndpoints; */
454 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
455 0x00, /* __u8 if_bInterfaceSubClass; */
456 0x00, /* __u8 if_bInterfaceProtocol; */
457 0x00, /* __u8 if_iInterface; */
460 0x07, /* __u8 ep_bLength; */
461 0x05, /* __u8 ep_bDescriptorType; Endpoint */
462 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
463 0x03, /* __u8 ep_bmAttributes; Interrupt */
464 0x08, /* __u16 ep_wMaxPacketSize; */
466 0xff /* __u8 ep_bInterval; 255 ms */
469 /* root hub class descriptor*/
470 static __u8 sl811_rh_hub_des[] =
472 0x09, /* __u8 bLength; */
473 0x29, /* __u8 bDescriptorType; Hub-descriptor */
474 0x01, /* __u8 bNbrPorts; */
475 0x00, /* __u16 wHubCharacteristics; */
477 0x50, /* __u8 bPwrOn2pwrGood; 2ms */
478 0x00, /* __u8 bHubContrCurrent; 0 mA */
479 0xfc, /* __u8 DeviceRemovable; *** 7 Ports max *** */
480 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
484 * helper routine for returning string descriptors in UTF-16LE
485 * input can actually be ISO-8859-1; ASCII is its 7-bit subset
487 static int ascii2utf (char *s, u8 *utf, int utfmax)
491 for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
499 * root_hub_string is used by each host controller's root hub code,
500 * so that they're identified consistently throughout the system.
502 static int usb_root_hub_string (int id, int serial, char *type, __u8 *data, int len)
506 /* assert (len > (2 * (sizeof (buf) + 1)));
507 assert (strlen (type) <= 8);*/
511 *data++ = 3; *data++ = 4; /* 4 bytes data */
512 *data++ = 0; *data++ = 0; /* some language id */
516 } else if (id == 1) {
517 sprintf (buf, "%x", serial);
519 /* product description */
520 } else if (id == 2) {
521 sprintf (buf, "USB %s Root Hub", type);
523 /* id 3 == vendor description */
525 /* unsupported IDs --> "stall" */
529 data [0] = 2 + ascii2utf (buf, data + 2, len - 2);
535 #define OK(x) len = (x); break
538 * This function handles all USB request to the the virtual root hub
540 static int sl811_rh_submit_urb(struct usb_device *usb_dev, unsigned long pipe,
541 void *data, int buf_len, struct devrequest *cmd)
544 __u8 *bufp = data_buf;
553 if (usb_pipeint(pipe)) {
554 PDEBUG(0, "interrupt transfer unimplemented!\n");
558 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
559 wValue = le16_to_cpu (cmd->value);
560 wIndex = le16_to_cpu (cmd->index);
561 wLength = le16_to_cpu (cmd->length);
563 PDEBUG(5, "submit rh urb, req = %d(%x) val = %#x index = %#x len=%d\n",
564 bmRType_bReq, bmRType_bReq, wValue, wIndex, wLength);
566 /* Request Destination:
567 without flags: Device,
568 USB_RECIP_INTERFACE: interface,
569 USB_RECIP_ENDPOINT: endpoint,
570 USB_TYPE_CLASS means HUB here,
571 USB_RECIP_OTHER | USB_TYPE_CLASS almost ever means HUB_PORT here
573 switch (bmRType_bReq) {
575 *(__u16 *)bufp = cpu_to_le16(1);
578 case RH_GET_STATUS | USB_RECIP_INTERFACE:
579 *(__u16 *)bufp = cpu_to_le16(0);
582 case RH_GET_STATUS | USB_RECIP_ENDPOINT:
583 *(__u16 *)bufp = cpu_to_le16(0);
586 case RH_GET_STATUS | USB_TYPE_CLASS:
587 *(__u32 *)bufp = cpu_to_le32(0);
590 case RH_GET_STATUS | USB_RECIP_OTHER | USB_TYPE_CLASS:
591 *(__u32 *)bufp = cpu_to_le32(rh_status.wPortChange<<16 | rh_status.wPortStatus);
594 case RH_CLEAR_FEATURE | USB_RECIP_ENDPOINT:
601 case RH_CLEAR_FEATURE | USB_TYPE_CLASS:
603 case C_HUB_LOCAL_POWER:
606 case C_HUB_OVER_CURRENT:
611 case RH_CLEAR_FEATURE | USB_RECIP_OTHER | USB_TYPE_CLASS:
613 case USB_PORT_FEAT_ENABLE:
614 rh_status.wPortStatus &= ~USB_PORT_STAT_ENABLE;
617 case USB_PORT_FEAT_SUSPEND:
618 rh_status.wPortStatus &= ~USB_PORT_STAT_SUSPEND;
621 case USB_PORT_FEAT_POWER:
622 rh_status.wPortStatus &= ~USB_PORT_STAT_POWER;
625 case USB_PORT_FEAT_C_CONNECTION:
626 rh_status.wPortChange &= ~USB_PORT_STAT_C_CONNECTION;
629 case USB_PORT_FEAT_C_ENABLE:
630 rh_status.wPortChange &= ~USB_PORT_STAT_C_ENABLE;
633 case USB_PORT_FEAT_C_SUSPEND:
634 rh_status.wPortChange &= ~USB_PORT_STAT_C_SUSPEND;
637 case USB_PORT_FEAT_C_OVER_CURRENT:
638 rh_status.wPortChange &= ~USB_PORT_STAT_C_OVERCURRENT;
641 case USB_PORT_FEAT_C_RESET:
642 rh_status.wPortChange &= ~USB_PORT_STAT_C_RESET;
647 case RH_SET_FEATURE | USB_RECIP_OTHER | USB_TYPE_CLASS:
649 case USB_PORT_FEAT_SUSPEND:
650 rh_status.wPortStatus |= USB_PORT_STAT_SUSPEND;
653 case USB_PORT_FEAT_RESET:
654 rh_status.wPortStatus |= USB_PORT_STAT_RESET;
655 rh_status.wPortChange = 0;
656 rh_status.wPortChange |= USB_PORT_STAT_C_RESET;
657 rh_status.wPortStatus &= ~USB_PORT_STAT_RESET;
658 rh_status.wPortStatus |= USB_PORT_STAT_ENABLE;
661 case USB_PORT_FEAT_POWER:
662 rh_status.wPortStatus |= USB_PORT_STAT_POWER;
665 case USB_PORT_FEAT_ENABLE:
666 rh_status.wPortStatus |= USB_PORT_STAT_ENABLE;
672 root_hub_devnum = wValue;
675 case RH_GET_DESCRIPTOR:
676 switch ((wValue & 0xff00) >> 8) {
678 len = sizeof(sl811_rh_dev_des);
679 bufp = sl811_rh_dev_des;
683 len = sizeof(sl811_rh_config_des);
684 bufp = sl811_rh_config_des;
688 len = usb_root_hub_string(wValue & 0xff, (int)(long)0, "SL811HS", data, wLength);
699 case RH_GET_DESCRIPTOR | USB_TYPE_CLASS:
700 len = sizeof(sl811_rh_hub_des);
701 bufp = sl811_rh_hub_des;
704 case RH_GET_CONFIGURATION:
708 case RH_SET_CONFIGURATION:
712 PDEBUG(1, "unsupported root hub command\n");
716 len = min(len, buf_len);
718 memcpy(data, bufp, len);
720 PDEBUG(5, "len = %d, status = %d\n", len, status);
722 usb_dev->status = status;
723 usb_dev->act_len = len;
725 return status == 0 ? len : status;
728 #endif /* CONFIG_USB_SL811HS */