2 * Mentor USB OTG Core host controller driver.
4 * Copyright (c) 2008 Texas Instruments
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * Author: Thomas Abraham t-abraham@ti.com, Texas Instruments
27 /* MSC control transfers */
28 #define USB_MSC_BBB_RESET 0xFF
29 #define USB_MSC_BBB_GET_MAX_LUN 0xFE
31 /* Endpoint configuration information */
32 static struct musb_epinfo epinfo[3] = {
33 {MUSB_BULK_EP, 1, 512}, /* EP1 - Bluk Out - 512 Bytes */
34 {MUSB_BULK_EP, 0, 512}, /* EP1 - Bluk In - 512 Bytes */
35 {MUSB_INTR_EP, 0, 64} /* EP2 - Interrupt IN - 64 Bytes */
38 /* --- Virtual Root Hub ---------------------------------------------------- */
39 #ifdef MUSB_NO_MULTIPOINT
41 static u32 port_status;
43 /* Device descriptor */
44 static u8 root_hub_dev_des[] = {
45 0x12, /* __u8 bLength; */
46 0x01, /* __u8 bDescriptorType; Device */
47 0x00, /* __u16 bcdUSB; v1.1 */
49 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
50 0x00, /* __u8 bDeviceSubClass; */
51 0x00, /* __u8 bDeviceProtocol; */
52 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
53 0x00, /* __u16 idVendor; */
55 0x00, /* __u16 idProduct; */
57 0x00, /* __u16 bcdDevice; */
59 0x00, /* __u8 iManufacturer; */
60 0x01, /* __u8 iProduct; */
61 0x00, /* __u8 iSerialNumber; */
62 0x01 /* __u8 bNumConfigurations; */
65 /* Configuration descriptor */
66 static u8 root_hub_config_des[] = {
67 0x09, /* __u8 bLength; */
68 0x02, /* __u8 bDescriptorType; Configuration */
69 0x19, /* __u16 wTotalLength; */
71 0x01, /* __u8 bNumInterfaces; */
72 0x01, /* __u8 bConfigurationValue; */
73 0x00, /* __u8 iConfiguration; */
74 0x40, /* __u8 bmAttributes;
75 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
76 0x00, /* __u8 MaxPower; */
79 0x09, /* __u8 if_bLength; */
80 0x04, /* __u8 if_bDescriptorType; Interface */
81 0x00, /* __u8 if_bInterfaceNumber; */
82 0x00, /* __u8 if_bAlternateSetting; */
83 0x01, /* __u8 if_bNumEndpoints; */
84 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
85 0x00, /* __u8 if_bInterfaceSubClass; */
86 0x00, /* __u8 if_bInterfaceProtocol; */
87 0x00, /* __u8 if_iInterface; */
90 0x07, /* __u8 ep_bLength; */
91 0x05, /* __u8 ep_bDescriptorType; Endpoint */
92 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
93 0x03, /* __u8 ep_bmAttributes; Interrupt */
94 0x00, /* __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
96 0xff /* __u8 ep_bInterval; 255 ms */
99 static unsigned char root_hub_str_index0[] = {
100 0x04, /* __u8 bLength; */
101 0x03, /* __u8 bDescriptorType; String-descriptor */
102 0x09, /* __u8 lang ID */
103 0x04, /* __u8 lang ID */
106 static unsigned char root_hub_str_index1[] = {
107 0x1c, /* __u8 bLength; */
108 0x03, /* __u8 bDescriptorType; String-descriptor */
109 'M', /* __u8 Unicode */
110 0, /* __u8 Unicode */
111 'U', /* __u8 Unicode */
112 0, /* __u8 Unicode */
113 'S', /* __u8 Unicode */
114 0, /* __u8 Unicode */
115 'B', /* __u8 Unicode */
116 0, /* __u8 Unicode */
117 ' ', /* __u8 Unicode */
118 0, /* __u8 Unicode */
119 'R', /* __u8 Unicode */
120 0, /* __u8 Unicode */
121 'o', /* __u8 Unicode */
122 0, /* __u8 Unicode */
123 'o', /* __u8 Unicode */
124 0, /* __u8 Unicode */
125 't', /* __u8 Unicode */
126 0, /* __u8 Unicode */
127 ' ', /* __u8 Unicode */
128 0, /* __u8 Unicode */
129 'H', /* __u8 Unicode */
130 0, /* __u8 Unicode */
131 'u', /* __u8 Unicode */
132 0, /* __u8 Unicode */
133 'b', /* __u8 Unicode */
134 0, /* __u8 Unicode */
139 * This function writes the data toggle value.
141 static void write_toggle(struct usb_device *dev, u8 ep, u8 dir_out)
143 u16 toggle = usb_gettoggle(dev, ep, dir_out);
147 csr = readw(&musbr->txcsr);
149 if (csr & MUSB_TXCSR_MODE)
150 csr = MUSB_TXCSR_CLRDATATOG;
153 writew(csr, &musbr->txcsr);
155 csr |= MUSB_TXCSR_H_WR_DATATOGGLE;
156 writew(csr, &musbr->txcsr);
157 csr |= (toggle << MUSB_TXCSR_H_DATATOGGLE_SHIFT);
158 writew(csr, &musbr->txcsr);
162 csr = readw(&musbr->txcsr);
163 if (csr & MUSB_TXCSR_MODE)
164 csr = MUSB_RXCSR_CLRDATATOG;
167 writew(csr, &musbr->rxcsr);
169 csr = readw(&musbr->rxcsr);
170 csr |= MUSB_RXCSR_H_WR_DATATOGGLE;
171 writew(csr, &musbr->rxcsr);
172 csr |= (toggle << MUSB_S_RXCSR_H_DATATOGGLE);
173 writew(csr, &musbr->rxcsr);
179 * This function checks if RxStall has occured on the endpoint. If a RxStall
180 * has occured, the RxStall is cleared and 1 is returned. If RxStall has
181 * not occured, 0 is returned.
183 static u8 check_stall(u8 ep, u8 dir_out)
189 csr = readw(&musbr->txcsr);
190 if (csr & MUSB_CSR0_H_RXSTALL) {
191 csr &= ~MUSB_CSR0_H_RXSTALL;
192 writew(csr, &musbr->txcsr);
195 } else { /* For non-ep0 */
196 if (dir_out) { /* is it tx ep */
197 csr = readw(&musbr->txcsr);
198 if (csr & MUSB_TXCSR_H_RXSTALL) {
199 csr &= ~MUSB_TXCSR_H_RXSTALL;
200 writew(csr, &musbr->txcsr);
203 } else { /* is it rx ep */
204 csr = readw(&musbr->rxcsr);
205 if (csr & MUSB_RXCSR_H_RXSTALL) {
206 csr &= ~MUSB_RXCSR_H_RXSTALL;
207 writew(csr, &musbr->rxcsr);
216 * waits until ep0 is ready. Returns 0 if ep is ready, -1 for timeout
217 * error and -2 for stall.
219 static int wait_until_ep0_ready(struct usb_device *dev, u32 bit_mask)
223 int timeout = CONFIG_MUSB_TIMEOUT;
226 csr = readw(&musbr->txcsr);
227 if (csr & MUSB_CSR0_H_ERROR) {
228 csr &= ~MUSB_CSR0_H_ERROR;
229 writew(csr, &musbr->txcsr);
230 dev->status = USB_ST_CRC_ERR;
236 case MUSB_CSR0_TXPKTRDY:
237 if (!(csr & MUSB_CSR0_TXPKTRDY)) {
238 if (check_stall(MUSB_CONTROL_EP, 0)) {
239 dev->status = USB_ST_STALLED;
246 case MUSB_CSR0_RXPKTRDY:
247 if (check_stall(MUSB_CONTROL_EP, 0)) {
248 dev->status = USB_ST_STALLED;
251 if (csr & MUSB_CSR0_RXPKTRDY)
255 case MUSB_CSR0_H_REQPKT:
256 if (!(csr & MUSB_CSR0_H_REQPKT)) {
257 if (check_stall(MUSB_CONTROL_EP, 0)) {
258 dev->status = USB_ST_STALLED;
266 /* Check the timeout */
270 dev->status = USB_ST_CRC_ERR;
280 * waits until tx ep is ready. Returns 1 when ep is ready and 0 on error.
282 static u8 wait_until_txep_ready(struct usb_device *dev, u8 ep)
285 int timeout = CONFIG_MUSB_TIMEOUT;
288 if (check_stall(ep, 1)) {
289 dev->status = USB_ST_STALLED;
293 csr = readw(&musbr->txcsr);
294 if (csr & MUSB_TXCSR_H_ERROR) {
295 dev->status = USB_ST_CRC_ERR;
299 /* Check the timeout */
303 dev->status = USB_ST_CRC_ERR;
307 } while (csr & MUSB_TXCSR_TXPKTRDY);
312 * waits until rx ep is ready. Returns 1 when ep is ready and 0 on error.
314 static u8 wait_until_rxep_ready(struct usb_device *dev, u8 ep)
317 int timeout = CONFIG_MUSB_TIMEOUT;
320 if (check_stall(ep, 0)) {
321 dev->status = USB_ST_STALLED;
325 csr = readw(&musbr->rxcsr);
326 if (csr & MUSB_RXCSR_H_ERROR) {
327 dev->status = USB_ST_CRC_ERR;
331 /* Check the timeout */
335 dev->status = USB_ST_CRC_ERR;
339 } while (!(csr & MUSB_RXCSR_RXPKTRDY));
344 * This function performs the setup phase of the control transfer
346 static int ctrlreq_setup_phase(struct usb_device *dev, struct devrequest *setup)
351 /* write the control request to ep0 fifo */
352 write_fifo(MUSB_CONTROL_EP, sizeof(struct devrequest), (void *)setup);
354 /* enable transfer of setup packet */
355 csr = readw(&musbr->txcsr);
356 csr |= (MUSB_CSR0_TXPKTRDY|MUSB_CSR0_H_SETUPPKT);
357 writew(csr, &musbr->txcsr);
359 /* wait until the setup packet is transmitted */
360 result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
366 * This function handles the control transfer in data phase
368 static int ctrlreq_in_data_phase(struct usb_device *dev, u32 len, void *buffer)
373 u8 maxpktsize = (1 << dev->maxpacketsize) * 8;
374 u8 *rxbuff = (u8 *)buffer;
378 while (rxlen < len) {
379 /* Determine the next read length */
380 nextlen = ((len-rxlen) > maxpktsize) ? maxpktsize : (len-rxlen);
382 /* Set the ReqPkt bit */
383 csr = readw(&musbr->txcsr);
384 writew(csr | MUSB_CSR0_H_REQPKT, &musbr->txcsr);
385 result = wait_until_ep0_ready(dev, MUSB_CSR0_RXPKTRDY);
389 /* Actual number of bytes received by usb */
390 rxedlength = readb(&musbr->rxcount);
392 /* Read the data from the RxFIFO */
393 read_fifo(MUSB_CONTROL_EP, rxedlength, &rxbuff[rxlen]);
395 /* Clear the RxPktRdy Bit */
396 csr = readw(&musbr->txcsr);
397 csr &= ~MUSB_CSR0_RXPKTRDY;
398 writew(csr, &musbr->txcsr);
401 if (rxedlength != nextlen) {
402 dev->act_len += rxedlength;
406 dev->act_len = rxlen;
412 * This function handles the control transfer out data phase
414 static int ctrlreq_out_data_phase(struct usb_device *dev, u32 len, void *buffer)
419 u8 maxpktsize = (1 << dev->maxpacketsize) * 8;
420 u8 *txbuff = (u8 *)buffer;
423 while (txlen < len) {
424 /* Determine the next write length */
425 nextlen = ((len-txlen) > maxpktsize) ? maxpktsize : (len-txlen);
427 /* Load the data to send in FIFO */
428 write_fifo(MUSB_CONTROL_EP, txlen, &txbuff[txlen]);
430 /* Set TXPKTRDY bit */
431 csr = readw(&musbr->txcsr);
432 writew(csr | MUSB_CSR0_H_DIS_PING | MUSB_CSR0_TXPKTRDY,
434 result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
439 dev->act_len = txlen;
445 * This function handles the control transfer out status phase
447 static int ctrlreq_out_status_phase(struct usb_device *dev)
452 /* Set the StatusPkt bit */
453 csr = readw(&musbr->txcsr);
454 csr |= (MUSB_CSR0_H_DIS_PING | MUSB_CSR0_TXPKTRDY |
455 MUSB_CSR0_H_STATUSPKT);
456 writew(csr, &musbr->txcsr);
458 /* Wait until TXPKTRDY bit is cleared */
459 result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
464 * This function handles the control transfer in status phase
466 static int ctrlreq_in_status_phase(struct usb_device *dev)
471 /* Set the StatusPkt bit and ReqPkt bit */
472 csr = MUSB_CSR0_H_DIS_PING | MUSB_CSR0_H_REQPKT | MUSB_CSR0_H_STATUSPKT;
473 writew(csr, &musbr->txcsr);
474 result = wait_until_ep0_ready(dev, MUSB_CSR0_H_REQPKT);
476 /* clear StatusPkt bit and RxPktRdy bit */
477 csr = readw(&musbr->txcsr);
478 csr &= ~(MUSB_CSR0_RXPKTRDY | MUSB_CSR0_H_STATUSPKT);
479 writew(csr, &musbr->txcsr);
484 * determines the speed of the device (High/Full/Slow)
486 static u8 get_dev_speed(struct usb_device *dev)
488 return (dev->speed & USB_SPEED_HIGH) ? MUSB_TYPE_SPEED_HIGH :
489 ((dev->speed & USB_SPEED_LOW) ? MUSB_TYPE_SPEED_LOW :
490 MUSB_TYPE_SPEED_FULL);
494 * configure the hub address and the port address.
496 static void config_hub_port(struct usb_device *dev, u8 ep)
501 /* Find out the nearest parent which is high speed */
502 while (dev->parent->parent != NULL)
503 if (get_dev_speed(dev->parent) != MUSB_TYPE_SPEED_HIGH)
508 /* determine the port address at that hub */
509 hub = dev->parent->devnum;
510 for (chid = 0; chid < USB_MAXCHILDREN; chid++)
511 if (dev->parent->children[chid] == dev)
514 #ifndef MUSB_NO_MULTIPOINT
515 /* configure the hub address and the port address */
516 writeb(hub, &musbr->tar[ep].txhubaddr);
517 writeb((chid + 1), &musbr->tar[ep].txhubport);
518 writeb(hub, &musbr->tar[ep].rxhubaddr);
519 writeb((chid + 1), &musbr->tar[ep].rxhubport);
523 #ifdef MUSB_NO_MULTIPOINT
525 static void musb_port_reset(int do_reset)
527 u8 power = readb(&musbr->power);
531 writeb(power | MUSB_POWER_RESET, &musbr->power);
532 port_status |= USB_PORT_STAT_RESET;
533 port_status &= ~USB_PORT_STAT_ENABLE;
536 writeb(power & ~MUSB_POWER_RESET, &musbr->power);
538 power = readb(&musbr->power);
539 if (power & MUSB_POWER_HSMODE)
540 port_status |= USB_PORT_STAT_HIGH_SPEED;
542 port_status &= ~(USB_PORT_STAT_RESET | (USB_PORT_STAT_C_CONNECTION << 16));
543 port_status |= USB_PORT_STAT_ENABLE
544 | (USB_PORT_STAT_C_RESET << 16)
545 | (USB_PORT_STAT_C_ENABLE << 16);
552 static int musb_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
553 void *buffer, int transfer_len,
554 struct devrequest *cmd)
556 int leni = transfer_len;
560 u8 *data_buf = (u8 *) datab;
567 if ((pipe & PIPE_INTERRUPT) == PIPE_INTERRUPT) {
568 debug("Root-Hub submit IRQ: NOT implemented\n");
572 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
573 wValue = swap_16(cmd->value);
574 wIndex = swap_16(cmd->index);
575 wLength = swap_16(cmd->length);
577 debug("--- HUB ----------------------------------------\n");
578 debug("submit rh urb, req=%x val=%#x index=%#x len=%d\n",
579 bmRType_bReq, wValue, wIndex, wLength);
580 debug("------------------------------------------------\n");
582 switch (bmRType_bReq) {
584 debug("RH_GET_STATUS\n");
586 *(__u16 *) data_buf = swap_16(1);
590 case RH_GET_STATUS | RH_INTERFACE:
591 debug("RH_GET_STATUS | RH_INTERFACE\n");
593 *(__u16 *) data_buf = swap_16(0);
597 case RH_GET_STATUS | RH_ENDPOINT:
598 debug("RH_GET_STATUS | RH_ENDPOINT\n");
600 *(__u16 *) data_buf = swap_16(0);
604 case RH_GET_STATUS | RH_CLASS:
605 debug("RH_GET_STATUS | RH_CLASS\n");
607 *(__u32 *) data_buf = swap_32(0);
611 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
612 debug("RH_GET_STATUS | RH_OTHER | RH_CLASS\n");
614 int_usb = readw(&musbr->intrusb);
615 if (int_usb & MUSB_INTR_CONNECT) {
616 port_status |= USB_PORT_STAT_CONNECTION
617 | (USB_PORT_STAT_C_CONNECTION << 16);
618 port_status |= USB_PORT_STAT_HIGH_SPEED
619 | USB_PORT_STAT_ENABLE;
622 if (port_status & USB_PORT_STAT_RESET)
625 *(__u32 *) data_buf = swap_32(port_status);
629 case RH_CLEAR_FEATURE | RH_ENDPOINT:
630 debug("RH_CLEAR_FEATURE | RH_ENDPOINT\n");
633 case RH_ENDPOINT_STALL:
634 debug("C_HUB_ENDPOINT_STALL\n");
638 port_status &= ~(1 << wValue);
641 case RH_CLEAR_FEATURE | RH_CLASS:
642 debug("RH_CLEAR_FEATURE | RH_CLASS\n");
645 case RH_C_HUB_LOCAL_POWER:
646 debug("C_HUB_LOCAL_POWER\n");
650 case RH_C_HUB_OVER_CURRENT:
651 debug("C_HUB_OVER_CURRENT\n");
655 port_status &= ~(1 << wValue);
658 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
659 debug("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS\n");
666 case RH_PORT_SUSPEND:
674 case RH_C_PORT_CONNECTION:
678 case RH_C_PORT_ENABLE:
682 case RH_C_PORT_SUSPEND:
686 case RH_C_PORT_OVER_CURRENT:
690 case RH_C_PORT_RESET:
695 debug("invalid wValue\n");
696 stat = USB_ST_STALLED;
699 port_status &= ~(1 << wValue);
702 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
703 debug("RH_SET_FEATURE | RH_OTHER | RH_CLASS\n");
706 case RH_PORT_SUSPEND:
724 debug("invalid wValue\n");
725 stat = USB_ST_STALLED;
728 port_status |= 1 << wValue;
732 debug("RH_SET_ADDRESS\n");
738 case RH_GET_DESCRIPTOR:
739 debug("RH_GET_DESCRIPTOR: %x, %d\n", wValue, wLength);
742 case (USB_DT_DEVICE << 8): /* device descriptor */
743 len = min_t(unsigned int,
744 leni, min_t(unsigned int,
745 sizeof(root_hub_dev_des),
747 data_buf = root_hub_dev_des;
750 case (USB_DT_CONFIG << 8): /* configuration descriptor */
751 len = min_t(unsigned int,
752 leni, min_t(unsigned int,
753 sizeof(root_hub_config_des),
755 data_buf = root_hub_config_des;
758 case ((USB_DT_STRING << 8) | 0x00): /* string 0 descriptors */
759 len = min_t(unsigned int,
760 leni, min_t(unsigned int,
761 sizeof(root_hub_str_index0),
763 data_buf = root_hub_str_index0;
766 case ((USB_DT_STRING << 8) | 0x01): /* string 1 descriptors */
767 len = min_t(unsigned int,
768 leni, min_t(unsigned int,
769 sizeof(root_hub_str_index1),
771 data_buf = root_hub_str_index1;
775 debug("invalid wValue\n");
776 stat = USB_ST_STALLED;
781 case RH_GET_DESCRIPTOR | RH_CLASS:
782 debug("RH_GET_DESCRIPTOR | RH_CLASS\n");
784 data_buf[0] = 0x09; /* min length; */
786 data_buf[2] = 0x1; /* 1 port */
787 data_buf[3] = 0x01; /* per-port power switching */
788 data_buf[3] |= 0x10; /* no overcurrent reporting */
790 /* Corresponds to data_buf[4-7] */
797 len = min_t(unsigned int, leni,
798 min_t(unsigned int, data_buf[0], wLength));
801 case RH_GET_CONFIGURATION:
802 debug("RH_GET_CONFIGURATION\n");
804 *(__u8 *) data_buf = 0x01;
808 case RH_SET_CONFIGURATION:
809 debug("RH_SET_CONFIGURATION\n");
815 debug("*** *** *** unsupported root hub command *** *** ***\n");
816 stat = USB_ST_STALLED;
819 len = min_t(int, len, leni);
820 if (buffer != data_buf)
821 memcpy(buffer, data_buf, len);
825 debug("dev act_len %d, status %d\n", dev->act_len, dev->status);
830 static void musb_rh_init(void)
838 static void musb_rh_init(void) {}
843 * do a control transfer
845 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
846 int len, struct devrequest *setup)
848 int devnum = usb_pipedevice(pipe);
852 #ifdef MUSB_NO_MULTIPOINT
853 /* Control message is for the HUB? */
854 if (devnum == rh_devnum)
855 return musb_submit_rh_msg(dev, pipe, buffer, len, setup);
858 /* select control endpoint */
859 writeb(MUSB_CONTROL_EP, &musbr->index);
860 csr = readw(&musbr->txcsr);
862 #ifndef MUSB_NO_MULTIPOINT
863 /* target addr and (for multipoint) hub addr/port */
864 writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].txfuncaddr);
865 writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].rxfuncaddr);
868 /* configure the hub address and the port number as required */
869 devspeed = get_dev_speed(dev);
870 if ((musb_ishighspeed()) && (dev->parent != NULL) &&
871 (devspeed != MUSB_TYPE_SPEED_HIGH)) {
872 config_hub_port(dev, MUSB_CONTROL_EP);
873 writeb(devspeed << 6, &musbr->txtype);
875 writeb(musb_cfg.musb_speed << 6, &musbr->txtype);
876 #ifndef MUSB_NO_MULTIPOINT
877 writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubaddr);
878 writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubport);
879 writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubaddr);
880 writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubport);
884 /* Control transfer setup phase */
885 if (ctrlreq_setup_phase(dev, setup) < 0)
888 switch (setup->request) {
889 case USB_REQ_GET_DESCRIPTOR:
890 case USB_REQ_GET_CONFIGURATION:
891 case USB_REQ_GET_INTERFACE:
892 case USB_REQ_GET_STATUS:
893 case USB_MSC_BBB_GET_MAX_LUN:
894 /* control transfer in-data-phase */
895 if (ctrlreq_in_data_phase(dev, len, buffer) < 0)
897 /* control transfer out-status-phase */
898 if (ctrlreq_out_status_phase(dev) < 0)
902 case USB_REQ_SET_ADDRESS:
903 case USB_REQ_SET_CONFIGURATION:
904 case USB_REQ_SET_FEATURE:
905 case USB_REQ_SET_INTERFACE:
906 case USB_REQ_CLEAR_FEATURE:
907 case USB_MSC_BBB_RESET:
908 /* control transfer in status phase */
909 if (ctrlreq_in_status_phase(dev) < 0)
913 case USB_REQ_SET_DESCRIPTOR:
914 /* control transfer out data phase */
915 if (ctrlreq_out_data_phase(dev, len, buffer) < 0)
917 /* control transfer in status phase */
918 if (ctrlreq_in_status_phase(dev) < 0)
923 /* unhandled control transfer */
930 #ifdef MUSB_NO_MULTIPOINT
931 /* Set device address to USB_FADDR register */
932 if (setup->request == USB_REQ_SET_ADDRESS)
933 writeb(dev->devnum, &musbr->faddr);
942 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
943 void *buffer, int len)
945 int dir_out = usb_pipeout(pipe);
946 int ep = usb_pipeendpoint(pipe);
947 #ifndef MUSB_NO_MULTIPOINT
948 int devnum = usb_pipedevice(pipe);
956 /* select bulk endpoint */
957 writeb(MUSB_BULK_EP, &musbr->index);
959 #ifndef MUSB_NO_MULTIPOINT
960 /* write the address of the device */
962 writeb(devnum, &musbr->tar[MUSB_BULK_EP].txfuncaddr);
964 writeb(devnum, &musbr->tar[MUSB_BULK_EP].rxfuncaddr);
967 /* configure the hub address and the port number as required */
968 devspeed = get_dev_speed(dev);
969 if ((musb_ishighspeed()) && (dev->parent != NULL) &&
970 (devspeed != MUSB_TYPE_SPEED_HIGH)) {
972 * MUSB is in high speed and the destination device is full
973 * speed device. So configure the hub address and port
976 config_hub_port(dev, MUSB_BULK_EP);
978 #ifndef MUSB_NO_MULTIPOINT
980 writeb(0, &musbr->tar[MUSB_BULK_EP].txhubaddr);
981 writeb(0, &musbr->tar[MUSB_BULK_EP].txhubport);
983 writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubaddr);
984 writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubport);
987 devspeed = musb_cfg.musb_speed;
990 /* Write the saved toggle bit value */
991 write_toggle(dev, ep, dir_out);
993 if (dir_out) { /* bulk-out transfer */
994 /* Program the TxType register */
995 type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
996 (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
997 (ep & MUSB_TYPE_REMOTE_END);
998 writeb(type, &musbr->txtype);
1000 /* Write maximum packet size to the TxMaxp register */
1001 writew(dev->epmaxpacketout[ep], &musbr->txmaxp);
1002 while (txlen < len) {
1003 nextlen = ((len-txlen) < dev->epmaxpacketout[ep]) ?
1004 (len-txlen) : dev->epmaxpacketout[ep];
1006 #ifdef CONFIG_USB_BLACKFIN
1007 /* Set the transfer data size */
1008 writew(nextlen, &musbr->txcount);
1011 /* Write the data to the FIFO */
1012 write_fifo(MUSB_BULK_EP, nextlen,
1013 (void *)(((u8 *)buffer) + txlen));
1015 /* Set the TxPktRdy bit */
1016 csr = readw(&musbr->txcsr);
1017 writew(csr | MUSB_TXCSR_TXPKTRDY, &musbr->txcsr);
1019 /* Wait until the TxPktRdy bit is cleared */
1020 if (!wait_until_txep_ready(dev, MUSB_BULK_EP)) {
1021 readw(&musbr->txcsr);
1022 usb_settoggle(dev, ep, dir_out,
1023 (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
1024 dev->act_len = txlen;
1030 /* Keep a copy of the data toggle bit */
1031 csr = readw(&musbr->txcsr);
1032 usb_settoggle(dev, ep, dir_out,
1033 (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
1034 } else { /* bulk-in transfer */
1035 /* Write the saved toggle bit value */
1036 write_toggle(dev, ep, dir_out);
1038 /* Program the RxType register */
1039 type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
1040 (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
1041 (ep & MUSB_TYPE_REMOTE_END);
1042 writeb(type, &musbr->rxtype);
1044 /* Write the maximum packet size to the RxMaxp register */
1045 writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
1046 while (txlen < len) {
1047 nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
1048 (len-txlen) : dev->epmaxpacketin[ep];
1050 /* Set the ReqPkt bit */
1051 csr = readw(&musbr->rxcsr);
1052 writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
1054 /* Wait until the RxPktRdy bit is set */
1055 if (!wait_until_rxep_ready(dev, MUSB_BULK_EP)) {
1056 csr = readw(&musbr->rxcsr);
1057 usb_settoggle(dev, ep, dir_out,
1058 (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
1059 csr &= ~MUSB_RXCSR_RXPKTRDY;
1060 writew(csr, &musbr->rxcsr);
1061 dev->act_len = txlen;
1065 /* Read the data from the FIFO */
1066 read_fifo(MUSB_BULK_EP, nextlen,
1067 (void *)(((u8 *)buffer) + txlen));
1069 /* Clear the RxPktRdy bit */
1070 csr = readw(&musbr->rxcsr);
1071 csr &= ~MUSB_RXCSR_RXPKTRDY;
1072 writew(csr, &musbr->rxcsr);
1076 /* Keep a copy of the data toggle bit */
1077 csr = readw(&musbr->rxcsr);
1078 usb_settoggle(dev, ep, dir_out,
1079 (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
1082 /* bulk transfer is complete */
1089 * This function initializes the usb controller module.
1091 int usb_lowlevel_init(void)
1098 if (musb_platform_init() == -1)
1101 /* Configure all the endpoint FIFO's and start usb controller */
1102 musbr = musb_cfg.regs;
1103 musb_configure_ep(&epinfo[0],
1104 sizeof(epinfo) / sizeof(struct musb_epinfo));
1108 * Wait until musb is enabled in host mode with a timeout. There
1109 * should be a usb device connected.
1111 timeout = musb_cfg.timeout;
1113 if (readb(&musbr->devctl) & MUSB_DEVCTL_HM)
1116 /* if musb core is not in host mode, then return */
1120 /* start usb bus reset */
1121 power = readb(&musbr->power);
1122 writeb(power | MUSB_POWER_RESET, &musbr->power);
1124 /* After initiating a usb reset, wait for about 20ms to 30ms */
1127 /* stop usb bus reset */
1128 power = readb(&musbr->power);
1129 power &= ~MUSB_POWER_RESET;
1130 writeb(power, &musbr->power);
1132 /* Determine if the connected device is a high/full/low speed device */
1133 musb_cfg.musb_speed = (readb(&musbr->power) & MUSB_POWER_HSMODE) ?
1134 MUSB_TYPE_SPEED_HIGH :
1135 ((readb(&musbr->devctl) & MUSB_DEVCTL_FSDEV) ?
1136 MUSB_TYPE_SPEED_FULL : MUSB_TYPE_SPEED_LOW);
1141 * This function stops the operation of the davinci usb module.
1143 int usb_lowlevel_stop(void)
1145 /* Reset the USB module */
1146 musb_platform_deinit();
1147 writeb(0, &musbr->devctl);
1152 * This function supports usb interrupt transfers. Currently, usb interrupt
1153 * transfers are not supported.
1155 int submit_int_msg(struct usb_device *dev, unsigned long pipe,
1156 void *buffer, int len, int interval)
1158 int dir_out = usb_pipeout(pipe);
1159 int ep = usb_pipeendpoint(pipe);
1160 #ifndef MUSB_NO_MULTIPOINT
1161 int devnum = usb_pipedevice(pipe);
1169 /* select interrupt endpoint */
1170 writeb(MUSB_INTR_EP, &musbr->index);
1172 #ifndef MUSB_NO_MULTIPOINT
1173 /* write the address of the device */
1175 writeb(devnum, &musbr->tar[MUSB_INTR_EP].txfuncaddr);
1177 writeb(devnum, &musbr->tar[MUSB_INTR_EP].rxfuncaddr);
1180 /* configure the hub address and the port number as required */
1181 devspeed = get_dev_speed(dev);
1182 if ((musb_ishighspeed()) && (dev->parent != NULL) &&
1183 (devspeed != MUSB_TYPE_SPEED_HIGH)) {
1185 * MUSB is in high speed and the destination device is full
1186 * speed device. So configure the hub address and port
1187 * address registers.
1189 config_hub_port(dev, MUSB_INTR_EP);
1191 #ifndef MUSB_NO_MULTIPOINT
1193 writeb(0, &musbr->tar[MUSB_INTR_EP].txhubaddr);
1194 writeb(0, &musbr->tar[MUSB_INTR_EP].txhubport);
1196 writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubaddr);
1197 writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubport);
1200 devspeed = musb_cfg.musb_speed;
1203 /* Write the saved toggle bit value */
1204 write_toggle(dev, ep, dir_out);
1206 if (!dir_out) { /* intrrupt-in transfer */
1207 /* Write the saved toggle bit value */
1208 write_toggle(dev, ep, dir_out);
1209 writeb(interval, &musbr->rxinterval);
1211 /* Program the RxType register */
1212 type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
1213 (MUSB_TYPE_PROTO_INTR << MUSB_TYPE_PROTO_SHIFT) |
1214 (ep & MUSB_TYPE_REMOTE_END);
1215 writeb(type, &musbr->rxtype);
1217 /* Write the maximum packet size to the RxMaxp register */
1218 writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
1220 while (txlen < len) {
1221 nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
1222 (len-txlen) : dev->epmaxpacketin[ep];
1224 /* Set the ReqPkt bit */
1225 csr = readw(&musbr->rxcsr);
1226 writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
1228 /* Wait until the RxPktRdy bit is set */
1229 if (!wait_until_rxep_ready(dev, MUSB_INTR_EP)) {
1230 csr = readw(&musbr->rxcsr);
1231 usb_settoggle(dev, ep, dir_out,
1232 (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
1233 csr &= ~MUSB_RXCSR_RXPKTRDY;
1234 writew(csr, &musbr->rxcsr);
1235 dev->act_len = txlen;
1239 /* Read the data from the FIFO */
1240 read_fifo(MUSB_INTR_EP, nextlen,
1241 (void *)(((u8 *)buffer) + txlen));
1243 /* Clear the RxPktRdy bit */
1244 csr = readw(&musbr->rxcsr);
1245 csr &= ~MUSB_RXCSR_RXPKTRDY;
1246 writew(csr, &musbr->rxcsr);
1250 /* Keep a copy of the data toggle bit */
1251 csr = readw(&musbr->rxcsr);
1252 usb_settoggle(dev, ep, dir_out,
1253 (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
1256 /* interrupt transfer is complete */
1257 dev->irq_status = 0;
1258 dev->irq_act_len = len;
1259 dev->irq_handle(dev);
1266 #ifdef CONFIG_SYS_USB_EVENT_POLL
1268 * This function polls for USB keyboard data.
1270 void usb_event_poll()
1272 struct stdio_dev *dev;
1273 struct usb_device *usb_kbd_dev;
1274 struct usb_interface *iface;
1275 struct usb_endpoint_descriptor *ep;
1279 /* Get the pointer to USB Keyboard device pointer */
1280 dev = stdio_get_by_name("usbkbd");
1281 usb_kbd_dev = (struct usb_device *)dev->priv;
1282 iface = &usb_kbd_dev->config.if_desc[0];
1283 ep = &iface->ep_desc[0];
1284 pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress);
1286 /* Submit a interrupt transfer request */
1287 maxp = usb_maxpacket(usb_kbd_dev, pipe);
1288 usb_submit_int_msg(usb_kbd_dev, pipe, &new[0],
1289 maxp > 8 ? 8 : maxp, ep->bInterval);
1291 #endif /* CONFIG_SYS_USB_EVENT_POLL */