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;
17 * 3.Rewrited as sl811.o by Yin Aihua <yinah:couragetech.com.cn>
19 * SPDX-License-Identifier: GPL-2.0+
27 #include "../../../board/kup/common/kup.h"
30 # define EIEIO __asm__ volatile ("eieio")
32 # define EIEIO /* nothing */
35 #define SL811_ADR (0x50000000)
36 #define SL811_DAT (0x50000001)
42 static int root_hub_devnum = 0;
43 static struct usb_port_status rh_status = { 0 };/* root hub port status */
45 static int sl811_rh_submit_urb(struct usb_device *usb_dev, unsigned long pipe,
46 void *data, int buf_len, struct devrequest *cmd);
48 static void sl811_write (__u8 index, __u8 data)
50 *(volatile unsigned char *) (SL811_ADR) = index;
52 *(volatile unsigned char *) (SL811_DAT) = data;
56 static __u8 sl811_read (__u8 index)
60 *(volatile unsigned char *) (SL811_ADR) = index;
62 data = *(volatile unsigned char *) (SL811_DAT);
68 * Read consecutive bytes of data from the SL811H/SL11H buffer
70 static void inline sl811_read_buf(__u8 offset, __u8 *buf, __u8 size)
72 *(volatile unsigned char *) (SL811_ADR) = offset;
75 *buf++ = *(volatile unsigned char *) (SL811_DAT);
81 * Write consecutive bytes of data to the SL811H/SL11H buffer
83 static void inline sl811_write_buf(__u8 offset, __u8 *buf, __u8 size)
85 *(volatile unsigned char *) (SL811_ADR) = offset;
88 *(volatile unsigned char *) (SL811_DAT) = *buf++;
93 int usb_init_kup4x (void)
95 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
96 volatile memctl8xx_t *memctl = &immap->im_memctl;
100 memctl = &immap->im_memctl;
101 memctl->memc_or7 = 0xFFFF8726;
102 memctl->memc_br7 = 0x50000401; /* start at 0x50000000 */
103 /* BP 14 low = USB ON */
104 immap->im_cpm.cp_pbdat &= ~(BP_USB_VCC);
105 /* PB 14 nomal port */
106 immap->im_cpm.cp_pbpar &= ~(BP_USB_VCC);
108 immap->im_cpm.cp_pbdir |= (BP_USB_VCC);
112 for (i = 0x10; i < 0xff; i++) {
114 tmp = (sl811_read(i));
116 printf ("SL811 compare error index=0x%02x read=0x%02x\n", i, tmp);
120 printf ("SL811 ready\n");
125 * This function resets SL811HS controller and detects the speed of
126 * the connecting device
128 * Return: 0 = no device attached; 1 = USB device attached
130 static int sl811_hc_reset(void)
134 sl811_write(SL811_CTRL2, SL811_CTL2_HOST | SL811_12M_HI);
135 sl811_write(SL811_CTRL1, SL811_CTRL1_RESET);
139 /* Disable hardware SOF generation, clear all irq status. */
140 sl811_write(SL811_CTRL1, 0);
142 sl811_write(SL811_INTRSTS, 0xff);
143 status = sl811_read(SL811_INTRSTS);
145 if (status & SL811_INTR_NOTPRESENT) {
146 /* Device is not present */
147 PDEBUG(0, "Device not present\n");
148 rh_status.wPortStatus &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE);
149 rh_status.wPortChange |= USB_PORT_STAT_C_CONNECTION;
150 sl811_write(SL811_INTR, SL811_INTR_INSRMV);
154 /* Send SOF to address 0, endpoint 0. */
155 sl811_write(SL811_LEN_B, 0);
156 sl811_write(SL811_PIDEP_B, PIDEP(USB_PID_SOF, 0));
157 sl811_write(SL811_DEV_B, 0x00);
158 sl811_write(SL811_SOFLOW, SL811_12M_LOW);
160 if (status & SL811_INTR_SPEED_FULL) {
161 /* full speed device connect directly to root hub */
162 PDEBUG (0, "Full speed Device attached\n");
164 sl811_write(SL811_CTRL1, SL811_CTRL1_RESET);
166 sl811_write(SL811_CTRL2, SL811_CTL2_HOST | SL811_12M_HI);
167 sl811_write(SL811_CTRL1, SL811_CTRL1_SOF);
169 /* start the SOF or EOP */
170 sl811_write(SL811_CTRL_B, SL811_USB_CTRL_ARM);
171 rh_status.wPortStatus |= USB_PORT_STAT_CONNECTION;
172 rh_status.wPortStatus &= ~USB_PORT_STAT_LOW_SPEED;
174 sl811_write(SL811_INTRSTS, 0xff);
176 /* slow speed device connect directly to root-hub */
177 PDEBUG(0, "Low speed Device attached\n");
179 sl811_write(SL811_CTRL1, SL811_CTRL1_RESET);
181 sl811_write(SL811_CTRL2, SL811_CTL2_HOST | SL811_CTL2_DSWAP | SL811_12M_HI);
182 sl811_write(SL811_CTRL1, SL811_CTRL1_SPEED_LOW | SL811_CTRL1_SOF);
184 /* start the SOF or EOP */
185 sl811_write(SL811_CTRL_B, SL811_USB_CTRL_ARM);
186 rh_status.wPortStatus |= USB_PORT_STAT_CONNECTION | USB_PORT_STAT_LOW_SPEED;
188 sl811_write(SL811_INTRSTS, 0xff);
191 rh_status.wPortChange |= USB_PORT_STAT_C_CONNECTION;
192 sl811_write(SL811_INTR, /*SL811_INTR_INSRMV*/SL811_INTR_DONE_A);
197 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
204 int usb_lowlevel_stop(int index)
210 static int calc_needed_buswidth(int bytes, int need_preamble)
212 return !need_preamble ? bytes * 8 + 256 : 8 * 8 * bytes + 2048;
215 static int sl811_send_packet(struct usb_device *dev, unsigned long pipe, __u8 *buffer, int len)
217 __u8 ctrl = SL811_USB_CTRL_ARM | SL811_USB_CTRL_ENABLE;
219 int err = 0, time_start = get_timer(0);
220 int need_preamble = !(rh_status.wPortStatus & USB_PORT_STAT_LOW_SPEED) &&
221 (dev->speed == USB_SPEED_LOW);
226 if (usb_pipeout(pipe))
227 ctrl |= SL811_USB_CTRL_DIR_OUT;
228 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)))
229 ctrl |= SL811_USB_CTRL_TOGGLE_1;
231 ctrl |= SL811_USB_CTRL_PREAMBLE;
233 sl811_write(SL811_INTRSTS, 0xff);
236 sl811_write(SL811_ADDR_A, 0x10);
237 sl811_write(SL811_LEN_A, len);
238 if (usb_pipeout(pipe) && len)
239 sl811_write_buf(0x10, buffer, len);
241 if (!(rh_status.wPortStatus & USB_PORT_STAT_LOW_SPEED) &&
242 sl811_read(SL811_SOFCNTDIV)*64 < calc_needed_buswidth(len, need_preamble))
243 ctrl |= SL811_USB_CTRL_SOF;
245 ctrl &= ~SL811_USB_CTRL_SOF;
247 sl811_write(SL811_CTRL_A, ctrl);
248 while (!(sl811_read(SL811_INTRSTS) & SL811_INTR_DONE_A)) {
249 if (5*CONFIG_SYS_HZ < get_timer(time_start)) {
250 printf("USB transmit timed out\n");
251 return -USB_ST_CRC_ERR;
255 sl811_write(SL811_INTRSTS, 0xff);
256 status = sl811_read(SL811_STS_A);
258 if (status & SL811_USB_STS_ACK) {
259 int remainder = sl811_read(SL811_CNT_A);
261 PDEBUG(0, "usb transfer remainder = %d\n", remainder);
264 if (usb_pipein(pipe) && len)
265 sl811_read_buf(0x10, buffer, len);
269 if ((status & SL811_USB_STS_NAK) == SL811_USB_STS_NAK)
272 PDEBUG(0, "usb transfer error %#x\n", (int)status);
278 if (status & SL811_USB_STS_ERROR)
279 err |= USB_ST_BUF_ERR;
280 if (status & SL811_USB_STS_TIMEOUT)
281 err |= USB_ST_CRC_ERR;
282 if (status & SL811_USB_STS_STALL)
283 err |= USB_ST_STALLED;
288 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
291 int dir_out = usb_pipeout(pipe);
292 int ep = usb_pipeendpoint(pipe);
293 int max = usb_maxpacket(dev, pipe);
296 PDEBUG(7, "dev = %ld pipe = %ld buf = %p size = %d dir_out = %d\n",
297 usb_pipedevice(pipe), usb_pipeendpoint(pipe), buffer, len, dir_out);
301 sl811_write(SL811_DEV_A, usb_pipedevice(pipe));
302 sl811_write(SL811_PIDEP_A, PIDEP(!dir_out ? USB_PID_IN : USB_PID_OUT, ep));
304 int res = sl811_send_packet(dev, pipe, (__u8*)buffer+done,
305 max > len - done ? len - done : max);
311 if (!dir_out && res < max) /* short packet */
315 usb_dotoggle(dev, ep, dir_out);
323 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
324 int len,struct devrequest *setup)
327 int devnum = usb_pipedevice(pipe);
328 int ep = usb_pipeendpoint(pipe);
332 if (devnum == root_hub_devnum)
333 return sl811_rh_submit_urb(dev, pipe, buffer, len, setup);
335 PDEBUG(7, "dev = %d pipe = %ld buf = %p size = %d rt = %#x req = %#x bus = %i\n",
336 devnum, ep, buffer, len, (int)setup->requesttype,
337 (int)setup->request, sl811_read(SL811_SOFCNTDIV)*64);
339 sl811_write(SL811_DEV_A, devnum);
340 sl811_write(SL811_PIDEP_A, PIDEP(USB_PID_SETUP, ep));
342 usb_settoggle(dev, ep, 1, 0);
343 if (sl811_send_packet(dev, usb_sndctrlpipe(dev, ep),
344 (__u8*)setup, sizeof(*setup)) == sizeof(*setup)) {
345 int dir_in = usb_pipein(pipe);
346 int max = usb_maxpacket(dev, pipe);
349 sl811_write(SL811_PIDEP_A,
350 PIDEP(dir_in ? USB_PID_IN : USB_PID_OUT, ep));
351 usb_settoggle(dev, ep, usb_pipeout(pipe), 1);
353 int res = sl811_send_packet(dev, pipe, (__u8*)buffer+done,
354 max > len - done ? len - done : max);
356 PDEBUG(0, "status data failed!\n");
361 usb_dotoggle(dev, ep, usb_pipeout(pipe));
362 if (dir_in && res < max) /* short packet */
367 sl811_write(SL811_PIDEP_A,
368 PIDEP(!dir_in ? USB_PID_IN : USB_PID_OUT, ep));
369 usb_settoggle(dev, ep, !usb_pipeout(pipe), 1);
370 if (sl811_send_packet(dev,
371 !dir_in ? usb_rcvctrlpipe(dev, ep) :
372 usb_sndctrlpipe(dev, ep),
374 PDEBUG(0, "status phase failed!\n");
378 PDEBUG(0, "setup phase failed!\n");
387 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
388 int len, int interval)
390 PDEBUG(0, "dev = %p pipe = %#lx buf = %p size = %d int = %d\n", dev, pipe,
391 buffer, len, interval);
396 * SL811 Virtual Root Hub
399 /* Device descriptor */
400 static __u8 sl811_rh_dev_des[] =
402 0x12, /* __u8 bLength; */
403 0x01, /* __u8 bDescriptorType; Device */
404 0x10, /* __u16 bcdUSB; v1.1 */
406 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
407 0x00, /* __u8 bDeviceSubClass; */
408 0x00, /* __u8 bDeviceProtocol; */
409 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
410 0x00, /* __u16 idVendor; */
412 0x00, /* __u16 idProduct; */
414 0x00, /* __u16 bcdDevice; */
416 0x00, /* __u8 iManufacturer; */
417 0x02, /* __u8 iProduct; */
418 0x01, /* __u8 iSerialNumber; */
419 0x01 /* __u8 bNumConfigurations; */
422 /* Configuration descriptor */
423 static __u8 sl811_rh_config_des[] =
425 0x09, /* __u8 bLength; */
426 0x02, /* __u8 bDescriptorType; Configuration */
427 0x19, /* __u16 wTotalLength; */
429 0x01, /* __u8 bNumInterfaces; */
430 0x01, /* __u8 bConfigurationValue; */
431 0x00, /* __u8 iConfiguration; */
432 0x40, /* __u8 bmAttributes;
433 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup,
435 0x00, /* __u8 MaxPower; */
438 0x09, /* __u8 if_bLength; */
439 0x04, /* __u8 if_bDescriptorType; Interface */
440 0x00, /* __u8 if_bInterfaceNumber; */
441 0x00, /* __u8 if_bAlternateSetting; */
442 0x01, /* __u8 if_bNumEndpoints; */
443 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
444 0x00, /* __u8 if_bInterfaceSubClass; */
445 0x00, /* __u8 if_bInterfaceProtocol; */
446 0x00, /* __u8 if_iInterface; */
449 0x07, /* __u8 ep_bLength; */
450 0x05, /* __u8 ep_bDescriptorType; Endpoint */
451 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
452 0x03, /* __u8 ep_bmAttributes; Interrupt */
453 0x08, /* __u16 ep_wMaxPacketSize; */
455 0xff /* __u8 ep_bInterval; 255 ms */
458 /* root hub class descriptor*/
459 static __u8 sl811_rh_hub_des[] =
461 0x09, /* __u8 bLength; */
462 0x29, /* __u8 bDescriptorType; Hub-descriptor */
463 0x01, /* __u8 bNbrPorts; */
464 0x00, /* __u16 wHubCharacteristics; */
466 0x50, /* __u8 bPwrOn2pwrGood; 2ms */
467 0x00, /* __u8 bHubContrCurrent; 0 mA */
468 0xfc, /* __u8 DeviceRemovable; *** 7 Ports max *** */
469 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
473 * helper routine for returning string descriptors in UTF-16LE
474 * input can actually be ISO-8859-1; ASCII is its 7-bit subset
476 static int ascii2utf (char *s, u8 *utf, int utfmax)
480 for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
488 * root_hub_string is used by each host controller's root hub code,
489 * so that they're identified consistently throughout the system.
491 static int usb_root_hub_string (int id, int serial, char *type, __u8 *data, int len)
495 /* assert (len > (2 * (sizeof (buf) + 1)));
496 assert (strlen (type) <= 8);*/
500 *data++ = 4; *data++ = 3; /* 4 bytes data */
501 *data++ = 0; *data++ = 0; /* some language id */
505 } else if (id == 1) {
506 sprintf (buf, "%#x", serial);
508 /* product description */
509 } else if (id == 2) {
510 sprintf (buf, "USB %s Root Hub", type);
512 /* id 3 == vendor description */
514 /* unsupported IDs --> "stall" */
518 ascii2utf (buf, data + 2, len - 2);
519 data [0] = 2 + strlen(buf) * 2;
525 #define OK(x) len = (x); break
528 * This function handles all USB request to the the virtual root hub
530 static int sl811_rh_submit_urb(struct usb_device *usb_dev, unsigned long pipe,
531 void *data, int buf_len, struct devrequest *cmd)
534 __u8 *bufp = data_buf;
538 __u16 wValue = le16_to_cpu (cmd->value);
539 __u16 wLength = le16_to_cpu (cmd->length);
541 __u16 wIndex = le16_to_cpu (cmd->index);
544 if (usb_pipeint(pipe)) {
545 PDEBUG(0, "interrupt transfer unimplemented!\n");
549 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
551 PDEBUG(5, "submit rh urb, req = %d(%x) val = %#x index = %#x len=%d\n",
552 bmRType_bReq, bmRType_bReq, wValue, wIndex, wLength);
554 /* Request Destination:
555 without flags: Device,
556 USB_RECIP_INTERFACE: interface,
557 USB_RECIP_ENDPOINT: endpoint,
558 USB_TYPE_CLASS means HUB here,
559 USB_RECIP_OTHER | USB_TYPE_CLASS almost ever means HUB_PORT here
561 switch (bmRType_bReq) {
563 *(__u16 *)bufp = cpu_to_le16(1);
566 case RH_GET_STATUS | USB_RECIP_INTERFACE:
567 *(__u16 *)bufp = cpu_to_le16(0);
570 case RH_GET_STATUS | USB_RECIP_ENDPOINT:
571 *(__u16 *)bufp = cpu_to_le16(0);
574 case RH_GET_STATUS | USB_TYPE_CLASS:
575 *(__u32 *)bufp = cpu_to_le32(0);
578 case RH_GET_STATUS | USB_RECIP_OTHER | USB_TYPE_CLASS:
579 *(__u32 *)bufp = cpu_to_le32(rh_status.wPortChange<<16 | rh_status.wPortStatus);
582 case RH_CLEAR_FEATURE | USB_RECIP_ENDPOINT:
589 case RH_CLEAR_FEATURE | USB_TYPE_CLASS:
591 case C_HUB_LOCAL_POWER:
594 case C_HUB_OVER_CURRENT:
599 case RH_CLEAR_FEATURE | USB_RECIP_OTHER | USB_TYPE_CLASS:
601 case USB_PORT_FEAT_ENABLE:
602 rh_status.wPortStatus &= ~USB_PORT_STAT_ENABLE;
605 case USB_PORT_FEAT_SUSPEND:
606 rh_status.wPortStatus &= ~USB_PORT_STAT_SUSPEND;
609 case USB_PORT_FEAT_POWER:
610 rh_status.wPortStatus &= ~USB_PORT_STAT_POWER;
613 case USB_PORT_FEAT_C_CONNECTION:
614 rh_status.wPortChange &= ~USB_PORT_STAT_C_CONNECTION;
617 case USB_PORT_FEAT_C_ENABLE:
618 rh_status.wPortChange &= ~USB_PORT_STAT_C_ENABLE;
621 case USB_PORT_FEAT_C_SUSPEND:
622 rh_status.wPortChange &= ~USB_PORT_STAT_C_SUSPEND;
625 case USB_PORT_FEAT_C_OVER_CURRENT:
626 rh_status.wPortChange &= ~USB_PORT_STAT_C_OVERCURRENT;
629 case USB_PORT_FEAT_C_RESET:
630 rh_status.wPortChange &= ~USB_PORT_STAT_C_RESET;
635 case RH_SET_FEATURE | USB_RECIP_OTHER | USB_TYPE_CLASS:
637 case USB_PORT_FEAT_SUSPEND:
638 rh_status.wPortStatus |= USB_PORT_STAT_SUSPEND;
641 case USB_PORT_FEAT_RESET:
642 rh_status.wPortStatus |= USB_PORT_STAT_RESET;
643 rh_status.wPortChange = 0;
644 rh_status.wPortChange |= USB_PORT_STAT_C_RESET;
645 rh_status.wPortStatus &= ~USB_PORT_STAT_RESET;
646 rh_status.wPortStatus |= USB_PORT_STAT_ENABLE;
649 case USB_PORT_FEAT_POWER:
650 rh_status.wPortStatus |= USB_PORT_STAT_POWER;
653 case USB_PORT_FEAT_ENABLE:
654 rh_status.wPortStatus |= USB_PORT_STAT_ENABLE;
660 root_hub_devnum = wValue;
663 case RH_GET_DESCRIPTOR:
664 switch ((wValue & 0xff00) >> 8) {
666 len = sizeof(sl811_rh_dev_des);
667 bufp = sl811_rh_dev_des;
671 len = sizeof(sl811_rh_config_des);
672 bufp = sl811_rh_config_des;
676 len = usb_root_hub_string(wValue & 0xff, (int)(long)0, "SL811HS", data, wLength);
687 case RH_GET_DESCRIPTOR | USB_TYPE_CLASS:
688 len = sizeof(sl811_rh_hub_des);
689 bufp = sl811_rh_hub_des;
692 case RH_GET_CONFIGURATION:
696 case RH_SET_CONFIGURATION:
700 PDEBUG(1, "unsupported root hub command\n");
704 len = min(len, buf_len);
706 memcpy(data, bufp, len);
708 PDEBUG(5, "len = %d, status = %d\n", len, status);
710 usb_dev->status = status;
711 usb_dev->act_len = len;
713 return status == 0 ? len : status;