1 // SPDX-License-Identifier: GPL-2.0+
3 * Mentor USB OTG Core host controller driver.
5 * Copyright (c) 2008 Texas Instruments
7 * Author: Thomas Abraham t-abraham@ti.com, Texas Instruments
15 /* MSC control transfers */
16 #define USB_MSC_BBB_RESET 0xFF
17 #define USB_MSC_BBB_GET_MAX_LUN 0xFE
19 /* Endpoint configuration information */
20 static const struct musb_epinfo epinfo[3] = {
21 {MUSB_BULK_EP, 1, 512}, /* EP1 - Bluk Out - 512 Bytes */
22 {MUSB_BULK_EP, 0, 512}, /* EP1 - Bluk In - 512 Bytes */
23 {MUSB_INTR_EP, 0, 64} /* EP2 - Interrupt IN - 64 Bytes */
26 /* --- Virtual Root Hub ---------------------------------------------------- */
27 #ifdef MUSB_NO_MULTIPOINT
29 static u32 port_status;
31 #include <usbroothubdes.h>
36 * This function writes the data toggle value.
38 static void write_toggle(struct usb_device *dev, u8 ep, u8 dir_out)
40 u16 toggle = usb_gettoggle(dev, ep, dir_out);
44 csr = readw(&musbr->txcsr);
46 if (csr & MUSB_TXCSR_MODE)
47 csr = MUSB_TXCSR_CLRDATATOG;
50 writew(csr, &musbr->txcsr);
52 csr |= MUSB_TXCSR_H_WR_DATATOGGLE;
53 writew(csr, &musbr->txcsr);
54 csr |= (toggle << MUSB_TXCSR_H_DATATOGGLE_SHIFT);
55 writew(csr, &musbr->txcsr);
59 csr = readw(&musbr->txcsr);
60 if (csr & MUSB_TXCSR_MODE)
61 csr = MUSB_RXCSR_CLRDATATOG;
64 writew(csr, &musbr->rxcsr);
66 csr = readw(&musbr->rxcsr);
67 csr |= MUSB_RXCSR_H_WR_DATATOGGLE;
68 writew(csr, &musbr->rxcsr);
69 csr |= (toggle << MUSB_S_RXCSR_H_DATATOGGLE);
70 writew(csr, &musbr->rxcsr);
76 * This function checks if RxStall has occurred on the endpoint. If a RxStall
77 * has occurred, the RxStall is cleared and 1 is returned. If RxStall has
78 * not occurred, 0 is returned.
80 static u8 check_stall(u8 ep, u8 dir_out)
86 csr = readw(&musbr->txcsr);
87 if (csr & MUSB_CSR0_H_RXSTALL) {
88 csr &= ~MUSB_CSR0_H_RXSTALL;
89 writew(csr, &musbr->txcsr);
92 } else { /* For non-ep0 */
93 if (dir_out) { /* is it tx ep */
94 csr = readw(&musbr->txcsr);
95 if (csr & MUSB_TXCSR_H_RXSTALL) {
96 csr &= ~MUSB_TXCSR_H_RXSTALL;
97 writew(csr, &musbr->txcsr);
100 } else { /* is it rx ep */
101 csr = readw(&musbr->rxcsr);
102 if (csr & MUSB_RXCSR_H_RXSTALL) {
103 csr &= ~MUSB_RXCSR_H_RXSTALL;
104 writew(csr, &musbr->rxcsr);
113 * waits until ep0 is ready. Returns 0 if ep is ready, -1 for timeout
114 * error and -2 for stall.
116 static int wait_until_ep0_ready(struct usb_device *dev, u32 bit_mask)
120 int timeout = CONFIG_USB_MUSB_TIMEOUT;
123 csr = readw(&musbr->txcsr);
124 if (csr & MUSB_CSR0_H_ERROR) {
125 csr &= ~MUSB_CSR0_H_ERROR;
126 writew(csr, &musbr->txcsr);
127 dev->status = USB_ST_CRC_ERR;
133 case MUSB_CSR0_TXPKTRDY:
134 if (!(csr & MUSB_CSR0_TXPKTRDY)) {
135 if (check_stall(MUSB_CONTROL_EP, 0)) {
136 dev->status = USB_ST_STALLED;
143 case MUSB_CSR0_RXPKTRDY:
144 if (check_stall(MUSB_CONTROL_EP, 0)) {
145 dev->status = USB_ST_STALLED;
148 if (csr & MUSB_CSR0_RXPKTRDY)
152 case MUSB_CSR0_H_REQPKT:
153 if (!(csr & MUSB_CSR0_H_REQPKT)) {
154 if (check_stall(MUSB_CONTROL_EP, 0)) {
155 dev->status = USB_ST_STALLED;
163 /* Check the timeout */
167 dev->status = USB_ST_CRC_ERR;
177 * waits until tx ep is ready. Returns 1 when ep is ready and 0 on error.
179 static int wait_until_txep_ready(struct usb_device *dev, u8 ep)
182 int timeout = CONFIG_USB_MUSB_TIMEOUT;
185 if (check_stall(ep, 1)) {
186 dev->status = USB_ST_STALLED;
190 csr = readw(&musbr->txcsr);
191 if (csr & MUSB_TXCSR_H_ERROR) {
192 dev->status = USB_ST_CRC_ERR;
196 /* Check the timeout */
200 dev->status = USB_ST_CRC_ERR;
204 } while (csr & MUSB_TXCSR_TXPKTRDY);
209 * waits until rx ep is ready. Returns 1 when ep is ready and 0 on error.
211 static int wait_until_rxep_ready(struct usb_device *dev, u8 ep)
214 int timeout = CONFIG_USB_MUSB_TIMEOUT;
217 if (check_stall(ep, 0)) {
218 dev->status = USB_ST_STALLED;
222 csr = readw(&musbr->rxcsr);
223 if (csr & MUSB_RXCSR_H_ERROR) {
224 dev->status = USB_ST_CRC_ERR;
228 /* Check the timeout */
232 dev->status = USB_ST_CRC_ERR;
236 } while (!(csr & MUSB_RXCSR_RXPKTRDY));
241 * This function performs the setup phase of the control transfer
243 static int ctrlreq_setup_phase(struct usb_device *dev, struct devrequest *setup)
248 /* write the control request to ep0 fifo */
249 write_fifo(MUSB_CONTROL_EP, sizeof(struct devrequest), (void *)setup);
251 /* enable transfer of setup packet */
252 csr = readw(&musbr->txcsr);
253 csr |= (MUSB_CSR0_TXPKTRDY|MUSB_CSR0_H_SETUPPKT);
254 writew(csr, &musbr->txcsr);
256 /* wait until the setup packet is transmitted */
257 result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
263 * This function handles the control transfer in data phase
265 static int ctrlreq_in_data_phase(struct usb_device *dev, u32 len, void *buffer)
270 u8 maxpktsize = (1 << dev->maxpacketsize) * 8;
271 u8 *rxbuff = (u8 *)buffer;
275 while (rxlen < len) {
276 /* Determine the next read length */
277 nextlen = ((len-rxlen) > maxpktsize) ? maxpktsize : (len-rxlen);
279 /* Set the ReqPkt bit */
280 csr = readw(&musbr->txcsr);
281 writew(csr | MUSB_CSR0_H_REQPKT, &musbr->txcsr);
282 result = wait_until_ep0_ready(dev, MUSB_CSR0_RXPKTRDY);
286 /* Actual number of bytes received by usb */
287 rxedlength = readb(&musbr->rxcount);
289 /* Read the data from the RxFIFO */
290 read_fifo(MUSB_CONTROL_EP, rxedlength, &rxbuff[rxlen]);
292 /* Clear the RxPktRdy Bit */
293 csr = readw(&musbr->txcsr);
294 csr &= ~MUSB_CSR0_RXPKTRDY;
295 writew(csr, &musbr->txcsr);
298 if (rxedlength != nextlen) {
299 dev->act_len += rxedlength;
303 dev->act_len = rxlen;
309 * This function handles the control transfer out data phase
311 static int ctrlreq_out_data_phase(struct usb_device *dev, u32 len, void *buffer)
316 u8 maxpktsize = (1 << dev->maxpacketsize) * 8;
317 u8 *txbuff = (u8 *)buffer;
320 while (txlen < len) {
321 /* Determine the next write length */
322 nextlen = ((len-txlen) > maxpktsize) ? maxpktsize : (len-txlen);
324 /* Load the data to send in FIFO */
325 write_fifo(MUSB_CONTROL_EP, txlen, &txbuff[txlen]);
327 /* Set TXPKTRDY bit */
328 csr = readw(&musbr->txcsr);
330 csr |= MUSB_CSR0_TXPKTRDY;
331 csr |= MUSB_CSR0_H_DIS_PING;
332 writew(csr, &musbr->txcsr);
333 result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
338 dev->act_len = txlen;
344 * This function handles the control transfer out status phase
346 static int ctrlreq_out_status_phase(struct usb_device *dev)
351 /* Set the StatusPkt bit */
352 csr = readw(&musbr->txcsr);
353 csr |= (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_H_STATUSPKT);
354 csr |= MUSB_CSR0_H_DIS_PING;
355 writew(csr, &musbr->txcsr);
357 /* Wait until TXPKTRDY bit is cleared */
358 result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
363 * This function handles the control transfer in status phase
365 static int ctrlreq_in_status_phase(struct usb_device *dev)
370 /* Set the StatusPkt bit and ReqPkt bit */
371 csr = MUSB_CSR0_H_REQPKT | MUSB_CSR0_H_STATUSPKT;
372 csr |= MUSB_CSR0_H_DIS_PING;
373 writew(csr, &musbr->txcsr);
374 result = wait_until_ep0_ready(dev, MUSB_CSR0_H_REQPKT);
376 /* clear StatusPkt bit and RxPktRdy bit */
377 csr = readw(&musbr->txcsr);
378 csr &= ~(MUSB_CSR0_RXPKTRDY | MUSB_CSR0_H_STATUSPKT);
379 writew(csr, &musbr->txcsr);
384 * determines the speed of the device (High/Full/Slow)
386 static u8 get_dev_speed(struct usb_device *dev)
388 return (dev->speed == USB_SPEED_HIGH) ? MUSB_TYPE_SPEED_HIGH :
389 ((dev->speed == USB_SPEED_LOW) ? MUSB_TYPE_SPEED_LOW :
390 MUSB_TYPE_SPEED_FULL);
394 * configure the hub address and the port address.
396 static void config_hub_port(struct usb_device *dev, u8 ep)
401 /* Find out the nearest parent which is high speed */
402 while (dev->parent->parent != NULL)
403 if (get_dev_speed(dev->parent) != MUSB_TYPE_SPEED_HIGH)
408 /* determine the port address at that hub */
409 hub = dev->parent->devnum;
410 for (chid = 0; chid < USB_MAXCHILDREN; chid++)
411 if (dev->parent->children[chid] == dev)
414 #ifndef MUSB_NO_MULTIPOINT
415 /* configure the hub address and the port address */
416 writeb(hub, &musbr->tar[ep].txhubaddr);
417 writeb((chid + 1), &musbr->tar[ep].txhubport);
418 writeb(hub, &musbr->tar[ep].rxhubaddr);
419 writeb((chid + 1), &musbr->tar[ep].rxhubport);
423 #ifdef MUSB_NO_MULTIPOINT
425 static void musb_port_reset(int do_reset)
427 u8 power = readb(&musbr->power);
431 writeb(power | MUSB_POWER_RESET, &musbr->power);
432 port_status |= USB_PORT_STAT_RESET;
433 port_status &= ~USB_PORT_STAT_ENABLE;
436 writeb(power & ~MUSB_POWER_RESET, &musbr->power);
438 power = readb(&musbr->power);
439 if (power & MUSB_POWER_HSMODE)
440 port_status |= USB_PORT_STAT_HIGH_SPEED;
442 port_status &= ~(USB_PORT_STAT_RESET | (USB_PORT_STAT_C_CONNECTION << 16));
443 port_status |= USB_PORT_STAT_ENABLE
444 | (USB_PORT_STAT_C_RESET << 16)
445 | (USB_PORT_STAT_C_ENABLE << 16);
452 static int musb_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
453 void *buffer, int transfer_len,
454 struct devrequest *cmd)
456 int leni = transfer_len;
460 const u8 *data_buf = (u8 *) datab;
467 if ((pipe & PIPE_INTERRUPT) == PIPE_INTERRUPT) {
468 debug("Root-Hub submit IRQ: NOT implemented\n");
472 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
473 wValue = swap_16(cmd->value);
474 wIndex = swap_16(cmd->index);
475 wLength = swap_16(cmd->length);
477 debug("--- HUB ----------------------------------------\n");
478 debug("submit rh urb, req=%x val=%#x index=%#x len=%d\n",
479 bmRType_bReq, wValue, wIndex, wLength);
480 debug("------------------------------------------------\n");
482 switch (bmRType_bReq) {
484 debug("RH_GET_STATUS\n");
486 *(__u16 *) data_buf = swap_16(1);
490 case RH_GET_STATUS | RH_INTERFACE:
491 debug("RH_GET_STATUS | RH_INTERFACE\n");
493 *(__u16 *) data_buf = swap_16(0);
497 case RH_GET_STATUS | RH_ENDPOINT:
498 debug("RH_GET_STATUS | RH_ENDPOINT\n");
500 *(__u16 *) data_buf = swap_16(0);
504 case RH_GET_STATUS | RH_CLASS:
505 debug("RH_GET_STATUS | RH_CLASS\n");
507 *(__u32 *) data_buf = swap_32(0);
511 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
512 debug("RH_GET_STATUS | RH_OTHER | RH_CLASS\n");
514 int_usb = readw(&musbr->intrusb);
515 if (int_usb & MUSB_INTR_CONNECT) {
516 port_status |= USB_PORT_STAT_CONNECTION
517 | (USB_PORT_STAT_C_CONNECTION << 16);
518 port_status |= USB_PORT_STAT_HIGH_SPEED
519 | USB_PORT_STAT_ENABLE;
522 if (port_status & USB_PORT_STAT_RESET)
525 *(__u32 *) data_buf = swap_32(port_status);
529 case RH_CLEAR_FEATURE | RH_ENDPOINT:
530 debug("RH_CLEAR_FEATURE | RH_ENDPOINT\n");
533 case RH_ENDPOINT_STALL:
534 debug("C_HUB_ENDPOINT_STALL\n");
538 port_status &= ~(1 << wValue);
541 case RH_CLEAR_FEATURE | RH_CLASS:
542 debug("RH_CLEAR_FEATURE | RH_CLASS\n");
545 case RH_C_HUB_LOCAL_POWER:
546 debug("C_HUB_LOCAL_POWER\n");
550 case RH_C_HUB_OVER_CURRENT:
551 debug("C_HUB_OVER_CURRENT\n");
555 port_status &= ~(1 << wValue);
558 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
559 debug("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS\n");
566 case RH_PORT_SUSPEND:
574 case RH_C_PORT_CONNECTION:
578 case RH_C_PORT_ENABLE:
582 case RH_C_PORT_SUSPEND:
586 case RH_C_PORT_OVER_CURRENT:
590 case RH_C_PORT_RESET:
595 debug("invalid wValue\n");
596 stat = USB_ST_STALLED;
599 port_status &= ~(1 << wValue);
602 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
603 debug("RH_SET_FEATURE | RH_OTHER | RH_CLASS\n");
606 case RH_PORT_SUSPEND:
624 debug("invalid wValue\n");
625 stat = USB_ST_STALLED;
628 port_status |= 1 << wValue;
632 debug("RH_SET_ADDRESS\n");
638 case RH_GET_DESCRIPTOR:
639 debug("RH_GET_DESCRIPTOR: %x, %d\n", wValue, wLength);
642 case (USB_DT_DEVICE << 8): /* device descriptor */
643 len = min_t(unsigned int,
644 leni, min_t(unsigned int,
645 sizeof(root_hub_dev_des),
647 data_buf = root_hub_dev_des;
650 case (USB_DT_CONFIG << 8): /* configuration descriptor */
651 len = min_t(unsigned int,
652 leni, min_t(unsigned int,
653 sizeof(root_hub_config_des),
655 data_buf = root_hub_config_des;
658 case ((USB_DT_STRING << 8) | 0x00): /* string 0 descriptors */
659 len = min_t(unsigned int,
660 leni, min_t(unsigned int,
661 sizeof(root_hub_str_index0),
663 data_buf = root_hub_str_index0;
666 case ((USB_DT_STRING << 8) | 0x01): /* string 1 descriptors */
667 len = min_t(unsigned int,
668 leni, min_t(unsigned int,
669 sizeof(root_hub_str_index1),
671 data_buf = root_hub_str_index1;
675 debug("invalid wValue\n");
676 stat = USB_ST_STALLED;
681 case RH_GET_DESCRIPTOR | RH_CLASS: {
682 u8 *_data_buf = (u8 *) datab;
683 debug("RH_GET_DESCRIPTOR | RH_CLASS\n");
685 _data_buf[0] = 0x09; /* min length; */
687 _data_buf[2] = 0x1; /* 1 port */
688 _data_buf[3] = 0x01; /* per-port power switching */
689 _data_buf[3] |= 0x10; /* no overcurrent reporting */
691 /* Corresponds to data_buf[4-7] */
698 len = min_t(unsigned int, leni,
699 min_t(unsigned int, data_buf[0], wLength));
703 case RH_GET_CONFIGURATION:
704 debug("RH_GET_CONFIGURATION\n");
706 *(__u8 *) data_buf = 0x01;
710 case RH_SET_CONFIGURATION:
711 debug("RH_SET_CONFIGURATION\n");
717 debug("*** *** *** unsupported root hub command *** *** ***\n");
718 stat = USB_ST_STALLED;
721 len = min_t(int, len, leni);
722 if (buffer != data_buf)
723 memcpy(buffer, data_buf, len);
727 debug("dev act_len %d, status %lu\n", dev->act_len, dev->status);
732 static void musb_rh_init(void)
740 static void musb_rh_init(void) {}
745 * do a control transfer
747 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
748 int len, struct devrequest *setup)
750 int devnum = usb_pipedevice(pipe);
753 #ifdef MUSB_NO_MULTIPOINT
754 /* Control message is for the HUB? */
755 if (devnum == rh_devnum) {
756 int stat = musb_submit_rh_msg(dev, pipe, buffer, len, setup);
762 /* select control endpoint */
763 writeb(MUSB_CONTROL_EP, &musbr->index);
764 readw(&musbr->txcsr);
766 #ifndef MUSB_NO_MULTIPOINT
767 /* target addr and (for multipoint) hub addr/port */
768 writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].txfuncaddr);
769 writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].rxfuncaddr);
772 /* configure the hub address and the port number as required */
773 devspeed = get_dev_speed(dev);
774 if ((musb_ishighspeed()) && (dev->parent != NULL) &&
775 (devspeed != MUSB_TYPE_SPEED_HIGH)) {
776 config_hub_port(dev, MUSB_CONTROL_EP);
777 writeb(devspeed << 6, &musbr->txtype);
779 writeb(musb_cfg.musb_speed << 6, &musbr->txtype);
780 #ifndef MUSB_NO_MULTIPOINT
781 writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubaddr);
782 writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubport);
783 writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubaddr);
784 writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubport);
788 /* Control transfer setup phase */
789 if (ctrlreq_setup_phase(dev, setup) < 0)
792 switch (setup->request) {
793 case USB_REQ_GET_DESCRIPTOR:
794 case USB_REQ_GET_CONFIGURATION:
795 case USB_REQ_GET_INTERFACE:
796 case USB_REQ_GET_STATUS:
797 case USB_MSC_BBB_GET_MAX_LUN:
798 /* control transfer in-data-phase */
799 if (ctrlreq_in_data_phase(dev, len, buffer) < 0)
801 /* control transfer out-status-phase */
802 if (ctrlreq_out_status_phase(dev) < 0)
806 case USB_REQ_SET_ADDRESS:
807 case USB_REQ_SET_CONFIGURATION:
808 case USB_REQ_SET_FEATURE:
809 case USB_REQ_SET_INTERFACE:
810 case USB_REQ_CLEAR_FEATURE:
811 case USB_MSC_BBB_RESET:
812 /* control transfer in status phase */
813 if (ctrlreq_in_status_phase(dev) < 0)
817 case USB_REQ_SET_DESCRIPTOR:
818 /* control transfer out data phase */
819 if (ctrlreq_out_data_phase(dev, len, buffer) < 0)
821 /* control transfer in status phase */
822 if (ctrlreq_in_status_phase(dev) < 0)
827 /* unhandled control transfer */
834 #ifdef MUSB_NO_MULTIPOINT
835 /* Set device address to USB_FADDR register */
836 if (setup->request == USB_REQ_SET_ADDRESS)
837 writeb(dev->devnum, &musbr->faddr);
846 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
847 void *buffer, int len)
849 int dir_out = usb_pipeout(pipe);
850 int ep = usb_pipeendpoint(pipe);
851 #ifndef MUSB_NO_MULTIPOINT
852 int devnum = usb_pipedevice(pipe);
860 /* select bulk endpoint */
861 writeb(MUSB_BULK_EP, &musbr->index);
863 #ifndef MUSB_NO_MULTIPOINT
864 /* write the address of the device */
866 writeb(devnum, &musbr->tar[MUSB_BULK_EP].txfuncaddr);
868 writeb(devnum, &musbr->tar[MUSB_BULK_EP].rxfuncaddr);
871 /* configure the hub address and the port number as required */
872 devspeed = get_dev_speed(dev);
873 if ((musb_ishighspeed()) && (dev->parent != NULL) &&
874 (devspeed != MUSB_TYPE_SPEED_HIGH)) {
876 * MUSB is in high speed and the destination device is full
877 * speed device. So configure the hub address and port
880 config_hub_port(dev, MUSB_BULK_EP);
882 #ifndef MUSB_NO_MULTIPOINT
884 writeb(0, &musbr->tar[MUSB_BULK_EP].txhubaddr);
885 writeb(0, &musbr->tar[MUSB_BULK_EP].txhubport);
887 writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubaddr);
888 writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubport);
891 devspeed = musb_cfg.musb_speed;
894 /* Write the saved toggle bit value */
895 write_toggle(dev, ep, dir_out);
897 if (dir_out) { /* bulk-out transfer */
898 /* Program the TxType register */
899 type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
900 (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
901 (ep & MUSB_TYPE_REMOTE_END);
902 writeb(type, &musbr->txtype);
904 /* Write maximum packet size to the TxMaxp register */
905 writew(dev->epmaxpacketout[ep], &musbr->txmaxp);
906 while (txlen < len) {
907 nextlen = ((len-txlen) < dev->epmaxpacketout[ep]) ?
908 (len-txlen) : dev->epmaxpacketout[ep];
910 /* Write the data to the FIFO */
911 write_fifo(MUSB_BULK_EP, nextlen,
912 (void *)(((u8 *)buffer) + txlen));
914 /* Set the TxPktRdy bit */
915 csr = readw(&musbr->txcsr);
916 writew(csr | MUSB_TXCSR_TXPKTRDY, &musbr->txcsr);
918 /* Wait until the TxPktRdy bit is cleared */
919 if (wait_until_txep_ready(dev, MUSB_BULK_EP) != 1) {
920 readw(&musbr->txcsr);
921 usb_settoggle(dev, ep, dir_out,
922 (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
923 dev->act_len = txlen;
929 /* Keep a copy of the data toggle bit */
930 csr = readw(&musbr->txcsr);
931 usb_settoggle(dev, ep, dir_out,
932 (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
933 } else { /* bulk-in transfer */
934 /* Write the saved toggle bit value */
935 write_toggle(dev, ep, dir_out);
937 /* Program the RxType register */
938 type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
939 (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
940 (ep & MUSB_TYPE_REMOTE_END);
941 writeb(type, &musbr->rxtype);
943 /* Write the maximum packet size to the RxMaxp register */
944 writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
945 while (txlen < len) {
946 nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
947 (len-txlen) : dev->epmaxpacketin[ep];
949 /* Set the ReqPkt bit */
950 csr = readw(&musbr->rxcsr);
951 writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
953 /* Wait until the RxPktRdy bit is set */
954 if (wait_until_rxep_ready(dev, MUSB_BULK_EP) != 1) {
955 csr = readw(&musbr->rxcsr);
956 usb_settoggle(dev, ep, dir_out,
957 (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
958 csr &= ~MUSB_RXCSR_RXPKTRDY;
959 writew(csr, &musbr->rxcsr);
960 dev->act_len = txlen;
964 /* Read the data from the FIFO */
965 read_fifo(MUSB_BULK_EP, nextlen,
966 (void *)(((u8 *)buffer) + txlen));
968 /* Clear the RxPktRdy bit */
969 csr = readw(&musbr->rxcsr);
970 csr &= ~MUSB_RXCSR_RXPKTRDY;
971 writew(csr, &musbr->rxcsr);
975 /* Keep a copy of the data toggle bit */
976 csr = readw(&musbr->rxcsr);
977 usb_settoggle(dev, ep, dir_out,
978 (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
981 /* bulk transfer is complete */
988 * This function initializes the usb controller module.
990 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
997 if (musb_platform_init() == -1)
1000 /* Configure all the endpoint FIFO's and start usb controller */
1001 musbr = musb_cfg.regs;
1002 musb_configure_ep(&epinfo[0], ARRAY_SIZE(epinfo));
1006 * Wait until musb is enabled in host mode with a timeout. There
1007 * should be a usb device connected.
1009 timeout = musb_cfg.timeout;
1011 if (readb(&musbr->devctl) & MUSB_DEVCTL_HM)
1014 /* if musb core is not in host mode, then return */
1018 /* start usb bus reset */
1019 power = readb(&musbr->power);
1020 writeb(power | MUSB_POWER_RESET, &musbr->power);
1022 /* After initiating a usb reset, wait for about 20ms to 30ms */
1025 /* stop usb bus reset */
1026 power = readb(&musbr->power);
1027 power &= ~MUSB_POWER_RESET;
1028 writeb(power, &musbr->power);
1030 /* Determine if the connected device is a high/full/low speed device */
1031 musb_cfg.musb_speed = (readb(&musbr->power) & MUSB_POWER_HSMODE) ?
1032 MUSB_TYPE_SPEED_HIGH :
1033 ((readb(&musbr->devctl) & MUSB_DEVCTL_FSDEV) ?
1034 MUSB_TYPE_SPEED_FULL : MUSB_TYPE_SPEED_LOW);
1039 * This function stops the operation of the davinci usb module.
1041 int usb_lowlevel_stop(int index)
1043 /* Reset the USB module */
1044 musb_platform_deinit();
1045 writeb(0, &musbr->devctl);
1050 * This function supports usb interrupt transfers. Currently, usb interrupt
1051 * transfers are not supported.
1053 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1054 int len, int interval, bool nonblock)
1056 int dir_out = usb_pipeout(pipe);
1057 int ep = usb_pipeendpoint(pipe);
1058 #ifndef MUSB_NO_MULTIPOINT
1059 int devnum = usb_pipedevice(pipe);
1067 /* select interrupt endpoint */
1068 writeb(MUSB_INTR_EP, &musbr->index);
1070 #ifndef MUSB_NO_MULTIPOINT
1071 /* write the address of the device */
1073 writeb(devnum, &musbr->tar[MUSB_INTR_EP].txfuncaddr);
1075 writeb(devnum, &musbr->tar[MUSB_INTR_EP].rxfuncaddr);
1078 /* configure the hub address and the port number as required */
1079 devspeed = get_dev_speed(dev);
1080 if ((musb_ishighspeed()) && (dev->parent != NULL) &&
1081 (devspeed != MUSB_TYPE_SPEED_HIGH)) {
1083 * MUSB is in high speed and the destination device is full
1084 * speed device. So configure the hub address and port
1085 * address registers.
1087 config_hub_port(dev, MUSB_INTR_EP);
1089 #ifndef MUSB_NO_MULTIPOINT
1091 writeb(0, &musbr->tar[MUSB_INTR_EP].txhubaddr);
1092 writeb(0, &musbr->tar[MUSB_INTR_EP].txhubport);
1094 writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubaddr);
1095 writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubport);
1098 devspeed = musb_cfg.musb_speed;
1101 /* Write the saved toggle bit value */
1102 write_toggle(dev, ep, dir_out);
1104 if (!dir_out) { /* intrrupt-in transfer */
1105 /* Write the saved toggle bit value */
1106 write_toggle(dev, ep, dir_out);
1107 writeb(interval, &musbr->rxinterval);
1109 /* Program the RxType register */
1110 type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
1111 (MUSB_TYPE_PROTO_INTR << MUSB_TYPE_PROTO_SHIFT) |
1112 (ep & MUSB_TYPE_REMOTE_END);
1113 writeb(type, &musbr->rxtype);
1115 /* Write the maximum packet size to the RxMaxp register */
1116 writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
1118 while (txlen < len) {
1119 nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
1120 (len-txlen) : dev->epmaxpacketin[ep];
1122 /* Set the ReqPkt bit */
1123 csr = readw(&musbr->rxcsr);
1124 writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
1126 /* Wait until the RxPktRdy bit is set */
1127 if (wait_until_rxep_ready(dev, MUSB_INTR_EP) != 1) {
1128 csr = readw(&musbr->rxcsr);
1129 usb_settoggle(dev, ep, dir_out,
1130 (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
1131 csr &= ~MUSB_RXCSR_RXPKTRDY;
1132 writew(csr, &musbr->rxcsr);
1133 dev->act_len = txlen;
1137 /* Read the data from the FIFO */
1138 read_fifo(MUSB_INTR_EP, nextlen,
1139 (void *)(((u8 *)buffer) + txlen));
1141 /* Clear the RxPktRdy bit */
1142 csr = readw(&musbr->rxcsr);
1143 csr &= ~MUSB_RXCSR_RXPKTRDY;
1144 writew(csr, &musbr->rxcsr);
1148 /* Keep a copy of the data toggle bit */
1149 csr = readw(&musbr->rxcsr);
1150 usb_settoggle(dev, ep, dir_out,
1151 (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
1154 /* interrupt transfer is complete */
1155 dev->irq_status = 0;
1156 dev->irq_act_len = len;
1157 dev->irq_handle(dev);