1 // SPDX-License-Identifier: GPL-2.0+
3 * Based on drivers/usb/gadget/omap1510_udc.c
4 * TI OMAP1510 USB bus interface driver
7 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
13 #include <linux/delay.h>
16 #include <usbdevice.h>
18 #include <usb/designware_udc.h>
20 #include <asm/arch/hardware.h>
22 #define UDC_INIT_MDELAY 80 /* Device settle delay */
24 /* Some kind of debugging output... */
25 #ifndef DEBUG_DWUSBTTY
27 #define UDCDBGA(fmt, args...)
29 #define UDCDBG(str) serial_printf(str "\n")
30 #define UDCDBGA(fmt, args...) serial_printf(fmt "\n", ##args)
33 static struct urb *ep0_urb;
34 static struct usb_device_instance *udc_device;
36 static struct plug_regs *const plug_regs_p =
37 (struct plug_regs * const)CONFIG_SYS_PLUG_BASE;
38 static struct udc_regs *const udc_regs_p =
39 (struct udc_regs * const)CONFIG_SYS_USBD_BASE;
40 static struct udc_endp_regs *const outep_regs_p =
41 &((struct udc_regs * const)CONFIG_SYS_USBD_BASE)->out_regs[0];
42 static struct udc_endp_regs *const inep_regs_p =
43 &((struct udc_regs * const)CONFIG_SYS_USBD_BASE)->in_regs[0];
46 * udc_state_transition - Write the next packet to TxFIFO.
47 * @initial: Initial state.
48 * @final: Final state.
50 * Helper function to implement device state changes. The device states and
51 * the events that transition between them are:
56 * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET
62 * DEVICE_RESET DEVICE_POWER_INTERRUPTION
68 * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET
74 * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED
79 * udc_state_transition transitions up (in the direction from STATE_ATTACHED
80 * to STATE_CONFIGURED) from the specified initial state to the specified final
81 * state, passing through each intermediate state on the way. If the initial
82 * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
83 * no state transitions will take place.
85 * udc_state_transition also transitions down (in the direction from
86 * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
87 * specified final state, passing through each intermediate state on the way.
88 * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
89 * state, then no state transitions will take place.
91 * This function must only be called with interrupts disabled.
93 static void udc_state_transition(usb_device_state_t initial,
94 usb_device_state_t final)
96 if (initial < final) {
99 usbd_device_event_irq(udc_device,
100 DEVICE_HUB_CONFIGURED, 0);
101 if (final == STATE_POWERED)
104 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
105 if (final == STATE_DEFAULT)
108 usbd_device_event_irq(udc_device,
109 DEVICE_ADDRESS_ASSIGNED, 0);
110 if (final == STATE_ADDRESSED)
112 case STATE_ADDRESSED:
113 usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
114 case STATE_CONFIGURED:
119 } else if (initial > final) {
121 case STATE_CONFIGURED:
122 usbd_device_event_irq(udc_device,
123 DEVICE_DE_CONFIGURED, 0);
124 if (final == STATE_ADDRESSED)
126 case STATE_ADDRESSED:
127 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
128 if (final == STATE_DEFAULT)
131 usbd_device_event_irq(udc_device,
132 DEVICE_POWER_INTERRUPTION, 0);
133 if (final == STATE_POWERED)
136 usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
146 static void udc_stall_ep(u32 ep_num)
148 writel(readl(&inep_regs_p[ep_num].endp_cntl) | ENDP_CNTL_STALL,
149 &inep_regs_p[ep_num].endp_cntl);
151 writel(readl(&outep_regs_p[ep_num].endp_cntl) | ENDP_CNTL_STALL,
152 &outep_regs_p[ep_num].endp_cntl);
155 static void *get_fifo(int ep_num, int in)
157 u32 *fifo_ptr = (u32 *)CONFIG_SYS_FIFO_BASE;
161 fifo_ptr += readl(&inep_regs_p[1].endp_bsorfn);
162 /* break intentionally left out */
165 fifo_ptr += readl(&inep_regs_p[0].endp_bsorfn);
166 /* break intentionally left out */
172 readl(&outep_regs_p[2].endp_maxpacksize) >> 16;
173 /* break intentionally left out */
179 fifo_ptr += readl(&outep_regs_p[0].endp_maxpacksize) >> 16;
180 /* break intentionally left out */
183 return (void *)fifo_ptr;
186 static int usbgetpckfromfifo(int epNum, u8 *bufp, u32 len)
188 u8 *fifo_ptr = (u8 *)get_fifo(epNum, 0);
194 if (readl(&udc_regs_p->dev_stat) & DEV_STAT_RXFIFO_EMPTY)
197 nw = len / sizeof(u32);
198 nb = len % sizeof(u32);
200 /* use tmp buf if bufp is not word aligned */
202 wrdp = (u32 *)&tmp[0];
206 for (i = 0; i < nw; i++) {
207 writel(readl(fifo_ptr), wrdp);
212 for (i = 0; i < nb; i++) {
213 writeb(readb(fifo_ptr), bytp);
217 readl(&outep_regs_p[epNum].write_done);
219 /* copy back tmp buffer to bufp if bufp is not word aligned */
221 memcpy(bufp, tmp, len);
226 static void usbputpcktofifo(int epNum, u8 *bufp, u32 len)
231 u8 *fifo_ptr = get_fifo(epNum, 1);
233 nw = len / sizeof(int);
234 nb = len % sizeof(int);
236 for (i = 0; i < nw; i++) {
237 writel(*wrdp, fifo_ptr);
242 for (i = 0; i < nb; i++) {
243 writeb(*bytp, fifo_ptr);
250 * dw_write_noniso_tx_fifo - Write the next packet to TxFIFO.
251 * @endpoint: Endpoint pointer.
253 * If the endpoint has an active tx_urb, then the next packet of data from the
254 * URB is written to the tx FIFO. The total amount of data in the urb is given
255 * by urb->actual_length. The maximum amount of data that can be sent in any
256 * one packet is given by endpoint->tx_packetSize. The number of data bytes
257 * from this URB that have already been transmitted is given by endpoint->sent.
258 * endpoint->last is updated by this routine with the number of data bytes
259 * transmitted in this packet.
262 static void dw_write_noniso_tx_fifo(struct usb_endpoint_instance
265 struct urb *urb = endpoint->tx_urb;
271 UDCDBGA("urb->buffer %p, buffer_length %d, actual_length %d",
272 urb->buffer, urb->buffer_length, urb->actual_length);
274 last = min_t(u32, urb->actual_length - endpoint->sent,
275 endpoint->tx_packetSize);
278 u8 *cp = urb->buffer + endpoint->sent;
281 * This ensures that USBD packet fifo is accessed
282 * - through word aligned pointer or
283 * - through non word aligned pointer but only
284 * with a max length to make the next packet
288 align = ((ulong)cp % sizeof(int));
290 last = min(last, sizeof(int) - align);
292 UDCDBGA("endpoint->sent %d, tx_packetSize %d, last %d",
293 endpoint->sent, endpoint->tx_packetSize, last);
295 usbputpcktofifo(endpoint->endpoint_address &
296 USB_ENDPOINT_NUMBER_MASK, cp, last);
298 endpoint->last = last;
303 * Handle SETUP USB interrupt.
304 * This function implements TRM Figure 14-14.
306 static void dw_udc_setup(struct usb_endpoint_instance *endpoint)
308 u8 *datap = (u8 *)&ep0_urb->device_request;
309 int ep_addr = endpoint->endpoint_address;
311 UDCDBG("-> Entering device setup");
312 usbgetpckfromfifo(ep_addr, datap, 8);
314 /* Try to process setup packet */
315 if (ep0_recv_setup(ep0_urb)) {
316 /* Not a setup packet, stall next EP0 transaction */
318 UDCDBG("can't parse setup packet, still waiting for setup");
322 /* Check direction */
323 if ((ep0_urb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK)
324 == USB_REQ_HOST2DEVICE) {
325 UDCDBG("control write on EP0");
326 if (le16_to_cpu(ep0_urb->device_request.wLength)) {
327 /* Stall this request */
328 UDCDBG("Stalling unsupported EP0 control write data "
334 UDCDBG("control read on EP0");
336 * The ep0_recv_setup function has already placed our response
337 * packet data in ep0_urb->buffer and the packet length in
338 * ep0_urb->actual_length.
340 endpoint->tx_urb = ep0_urb;
343 * Write packet data to the FIFO. dw_write_noniso_tx_fifo
344 * will update endpoint->last with the number of bytes written
347 dw_write_noniso_tx_fifo(endpoint);
349 writel(0x0, &inep_regs_p[ep_addr].write_done);
352 udc_unset_nak(endpoint->endpoint_address);
354 UDCDBG("<- Leaving device setup");
358 * Handle endpoint 0 RX interrupt
360 static void dw_udc_ep0_rx(struct usb_endpoint_instance *endpoint)
366 /* Check direction */
367 if ((ep0_urb->device_request.bmRequestType
368 & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) {
370 * This rx interrupt must be for a control write data
373 * We don't support control write data stages.
374 * We should never end up here.
377 UDCDBG("Stalling unexpected EP0 control write "
378 "data stage packet");
382 * This rx interrupt must be for a control read status
385 UDCDBG("ACK on EP0 control read status stage packet");
386 u32 len = (readl(&outep_regs_p[0].endp_status) >> 11) & 0xfff;
387 usbgetpckfromfifo(0, dummy, len);
392 * Handle endpoint 0 TX interrupt
394 static void dw_udc_ep0_tx(struct usb_endpoint_instance *endpoint)
396 struct usb_device_request *request = &ep0_urb->device_request;
401 /* Check direction */
402 if ((request->bmRequestType & USB_REQ_DIRECTION_MASK) ==
403 USB_REQ_HOST2DEVICE) {
405 * This tx interrupt must be for a control write status
408 UDCDBG("ACK on EP0 control write status stage packet");
411 * This tx interrupt must be for a control read data
414 int wLength = le16_to_cpu(request->wLength);
417 * Update our count of bytes sent so far in this
420 endpoint->sent += endpoint->last;
423 * We are finished with this transfer if we have sent
424 * all of the bytes in our tx urb (urb->actual_length)
425 * unless we need a zero-length terminating packet. We
426 * need a zero-length terminating packet if we returned
427 * fewer bytes than were requested (wLength) by the host,
428 * and the number of bytes we returned is an exact
429 * multiple of the packet size endpoint->tx_packetSize.
431 if ((endpoint->sent == ep0_urb->actual_length) &&
432 ((ep0_urb->actual_length == wLength) ||
433 (endpoint->last != endpoint->tx_packetSize))) {
434 /* Done with control read data stage. */
435 UDCDBG("control read data stage complete");
438 * We still have another packet of data to send
439 * in this control read data stage or else we
440 * need a zero-length terminating packet.
442 UDCDBG("ACK control read data stage packet");
443 dw_write_noniso_tx_fifo(endpoint);
445 ep_addr = endpoint->endpoint_address;
446 writel(0x0, &inep_regs_p[ep_addr].write_done);
451 static struct usb_endpoint_instance *dw_find_ep(int ep)
455 for (i = 0; i < udc_device->bus->max_endpoints; i++) {
456 if ((udc_device->bus->endpoint_array[i].endpoint_address &
457 USB_ENDPOINT_NUMBER_MASK) == ep)
458 return &udc_device->bus->endpoint_array[i];
464 * Handle RX transaction on non-ISO endpoint.
465 * The ep argument is a physical endpoint number for a non-ISO IN endpoint
466 * in the range 1 to 15.
468 static void dw_udc_epn_rx(int ep)
472 struct usb_endpoint_instance *endpoint = dw_find_ep(ep);
475 urb = endpoint->rcv_urb;
478 u8 *cp = urb->buffer + urb->actual_length;
480 nbytes = (readl(&outep_regs_p[ep].endp_status) >> 11) &
482 usbgetpckfromfifo(ep, cp, nbytes);
483 usbd_rcv_complete(endpoint, nbytes, 0);
489 * Handle TX transaction on non-ISO endpoint.
490 * The ep argument is a physical endpoint number for a non-ISO IN endpoint
491 * in the range 16 to 30.
493 static void dw_udc_epn_tx(int ep)
495 struct usb_endpoint_instance *endpoint = dw_find_ep(ep);
501 * We need to transmit a terminating zero-length packet now if
502 * we have sent all of the data in this URB and the transfer
503 * size was an exact multiple of the packet size.
505 if (endpoint->tx_urb &&
506 (endpoint->last == endpoint->tx_packetSize) &&
507 (endpoint->tx_urb->actual_length - endpoint->sent -
508 endpoint->last == 0)) {
509 /* handle zero length packet here */
510 writel(0x0, &inep_regs_p[ep].write_done);
514 if (endpoint->tx_urb && endpoint->tx_urb->actual_length) {
515 /* retire the data that was just sent */
516 usbd_tx_complete(endpoint);
518 * Check to see if we have more data ready to transmit
521 if (endpoint->tx_urb && endpoint->tx_urb->actual_length) {
522 /* write data to FIFO */
523 dw_write_noniso_tx_fifo(endpoint);
524 writel(0x0, &inep_regs_p[ep].write_done);
526 } else if (endpoint->tx_urb
527 && (endpoint->tx_urb->actual_length == 0)) {
528 /* udc_set_nak(ep); */
534 * Start of public functions.
537 /* Called to start packet transmission. */
538 int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
540 udc_unset_nak(endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK);
544 /* Start to initialize h/w stuff */
554 readl(&plug_regs_p->plug_pending);
556 for (i = 0; i < UDC_INIT_MDELAY; i++)
559 plug_st = readl(&plug_regs_p->plug_state);
560 writel(plug_st | PLUG_STATUS_EN, &plug_regs_p->plug_state);
562 writel(~0x0, &udc_regs_p->endp_int);
563 writel(~0x0, &udc_regs_p->dev_int_mask);
564 writel(~0x0, &udc_regs_p->endp_int_mask);
566 #ifndef CONFIG_USBD_HS
567 writel(DEV_CONF_FS_SPEED | DEV_CONF_REMWAKEUP | DEV_CONF_SELFPOW |
568 DEV_CONF_PHYINT_16, &udc_regs_p->dev_conf);
570 writel(DEV_CONF_HS_SPEED | DEV_CONF_REMWAKEUP | DEV_CONF_SELFPOW |
571 DEV_CONF_PHYINT_16, &udc_regs_p->dev_conf);
574 writel(DEV_CNTL_SOFTDISCONNECT, &udc_regs_p->dev_cntl);
576 /* Clear all interrupts pending */
577 writel(DEV_INT_MSK, &udc_regs_p->dev_int);
582 int is_usbd_high_speed(void)
584 return (readl(&udc_regs_p->dev_stat) & DEV_STAT_ENUM) ? 0 : 1;
588 * udc_setup_ep - setup endpoint
589 * Associate a physical endpoint with endpoint_instance
591 void udc_setup_ep(struct usb_device_instance *device,
592 u32 ep, struct usb_endpoint_instance *endpoint)
594 UDCDBGA("setting up endpoint addr %x", endpoint->endpoint_address);
603 if ((ep != 0) && (udc_device->device_state < STATE_ADDRESSED))
606 tt = env_get("usbtty");
610 ep_addr = endpoint->endpoint_address;
611 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
613 if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
615 packet_size = endpoint->tx_packetSize;
616 buffer_size = packet_size * 2;
617 attributes = endpoint->tx_attributes;
620 packet_size = endpoint->rcv_packetSize;
621 buffer_size = packet_size * 2;
622 attributes = endpoint->rcv_attributes;
625 switch (attributes & USB_ENDPOINT_XFERTYPE_MASK) {
626 case USB_ENDPOINT_XFER_CONTROL:
627 ep_type = ENDP_EPTYPE_CNTL;
629 case USB_ENDPOINT_XFER_BULK:
631 ep_type = ENDP_EPTYPE_BULK;
633 case USB_ENDPOINT_XFER_INT:
634 ep_type = ENDP_EPTYPE_INT;
636 case USB_ENDPOINT_XFER_ISOC:
637 ep_type = ENDP_EPTYPE_ISO;
641 struct udc_endp_regs *out_p = &outep_regs_p[ep_num];
642 struct udc_endp_regs *in_p = &inep_regs_p[ep_num];
645 /* Setup endpoint 0 */
646 buffer_size = packet_size;
648 writel(readl(&in_p->endp_cntl) | ENDP_CNTL_CNAK,
651 writel(readl(&out_p->endp_cntl) | ENDP_CNTL_CNAK,
654 writel(ENDP_CNTL_CONTROL | ENDP_CNTL_FLUSH, &in_p->endp_cntl);
656 writel(buffer_size / sizeof(int), &in_p->endp_bsorfn);
658 writel(packet_size, &in_p->endp_maxpacksize);
660 writel(ENDP_CNTL_CONTROL | ENDP_CNTL_RRDY, &out_p->endp_cntl);
662 writel(packet_size | ((buffer_size / sizeof(int)) << 16),
663 &out_p->endp_maxpacksize);
665 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
666 /* Setup the IN endpoint */
667 writel(0x0, &in_p->endp_status);
668 writel((ep_type << 4) | ENDP_CNTL_RRDY, &in_p->endp_cntl);
669 writel(buffer_size / sizeof(int), &in_p->endp_bsorfn);
670 writel(packet_size, &in_p->endp_maxpacksize);
672 if (!strcmp(tt, "cdc_acm")) {
673 if (ep_type == ENDP_EPTYPE_INT) {
674 /* Conf no. 1 Interface no. 0 */
675 writel((packet_size << 19) |
676 ENDP_EPDIR_IN | (1 << 7) |
677 (0 << 11) | (ep_type << 5) | ep_num,
678 &udc_regs_p->udc_endp_reg[ep_num]);
680 /* Conf no. 1 Interface no. 1 */
681 writel((packet_size << 19) |
682 ENDP_EPDIR_IN | (1 << 7) |
683 (1 << 11) | (ep_type << 5) | ep_num,
684 &udc_regs_p->udc_endp_reg[ep_num]);
687 /* Conf no. 1 Interface no. 0 */
688 writel((packet_size << 19) |
689 ENDP_EPDIR_IN | (1 << 7) |
690 (0 << 11) | (ep_type << 5) | ep_num,
691 &udc_regs_p->udc_endp_reg[ep_num]);
695 /* Setup the OUT endpoint */
696 writel(0x0, &out_p->endp_status);
697 writel((ep_type << 4) | ENDP_CNTL_RRDY, &out_p->endp_cntl);
698 writel(packet_size | ((buffer_size / sizeof(int)) << 16),
699 &out_p->endp_maxpacksize);
701 if (!strcmp(tt, "cdc_acm")) {
702 writel((packet_size << 19) |
703 ENDP_EPDIR_OUT | (1 << 7) |
704 (1 << 11) | (ep_type << 5) | ep_num,
705 &udc_regs_p->udc_endp_reg[ep_num]);
707 writel((packet_size << 19) |
708 ENDP_EPDIR_OUT | (1 << 7) |
709 (0 << 11) | (ep_type << 5) | ep_num,
710 &udc_regs_p->udc_endp_reg[ep_num]);
715 endp_intmask = readl(&udc_regs_p->endp_int_mask);
716 endp_intmask &= ~((1 << ep_num) | 0x10000 << ep_num);
717 writel(endp_intmask, &udc_regs_p->endp_int_mask);
720 /* Turn on the USB connection by enabling the pullup resistor */
721 void udc_connect(void)
723 u32 plug_st, dev_cntl;
725 dev_cntl = readl(&udc_regs_p->dev_cntl);
726 dev_cntl |= DEV_CNTL_SOFTDISCONNECT;
727 writel(dev_cntl, &udc_regs_p->dev_cntl);
731 dev_cntl = readl(&udc_regs_p->dev_cntl);
732 dev_cntl &= ~DEV_CNTL_SOFTDISCONNECT;
733 writel(dev_cntl, &udc_regs_p->dev_cntl);
735 plug_st = readl(&plug_regs_p->plug_state);
736 plug_st &= ~(PLUG_STATUS_PHY_RESET | PLUG_STATUS_PHY_MODE);
737 writel(plug_st, &plug_regs_p->plug_state);
740 /* Turn off the USB connection by disabling the pullup resistor */
741 void udc_disconnect(void)
745 writel(DEV_CNTL_SOFTDISCONNECT, &udc_regs_p->dev_cntl);
747 plug_st = readl(&plug_regs_p->plug_state);
748 plug_st |= (PLUG_STATUS_PHY_RESET | PLUG_STATUS_PHY_MODE);
749 writel(plug_st, &plug_regs_p->plug_state);
752 /* Switch on the UDC */
753 void udc_enable(struct usb_device_instance *device)
755 UDCDBGA("enable device %p, status %d", device, device->status);
757 /* Save the device structure pointer */
763 usbd_alloc_urb(udc_device, udc_device->bus->endpoint_array);
765 serial_printf("udc_enable: ep0_urb already allocated %p\n",
769 writel(DEV_INT_SOF, &udc_regs_p->dev_int_mask);
773 * udc_startup - allow udc code to do any additional startup
775 void udc_startup_events(struct usb_device_instance *device)
777 /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
778 usbd_device_event_irq(device, DEVICE_INIT, 0);
781 * The DEVICE_CREATE event puts the USB device in the state
784 usbd_device_event_irq(device, DEVICE_CREATE, 0);
787 * Some USB controller driver implementations signal
788 * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
789 * DEVICE_HUB_CONFIGURED causes a transition to the state STATE_POWERED,
790 * and DEVICE_RESET causes a transition to the state STATE_DEFAULT.
791 * The DW USB client controller has the capability to detect when the
792 * USB cable is connected to a powered USB bus, so we will defer the
793 * DEVICE_HUB_CONFIGURED and DEVICE_RESET events until later.
800 * Plug detection interrupt handling
802 static void dw_udc_plug_irq(void)
804 if (readl(&plug_regs_p->plug_state) & PLUG_STATUS_ATTACHED) {
807 * Turn off PHY reset bit (PLUG detect).
808 * Switch PHY opmode to normal operation (PLUG detect).
811 writel(DEV_INT_SOF, &udc_regs_p->dev_int_mask);
813 UDCDBG("device attached and powered");
814 udc_state_transition(udc_device->device_state, STATE_POWERED);
816 writel(~0x0, &udc_regs_p->dev_int_mask);
818 UDCDBG("device detached or unpowered");
819 udc_state_transition(udc_device->device_state, STATE_ATTACHED);
824 * Device interrupt handling
826 static void dw_udc_dev_irq(void)
828 if (readl(&udc_regs_p->dev_int) & DEV_INT_USBRESET) {
829 writel(~0x0, &udc_regs_p->endp_int_mask);
831 writel(readl(&inep_regs_p[0].endp_cntl) | ENDP_CNTL_FLUSH,
832 &inep_regs_p[0].endp_cntl);
834 writel(DEV_INT_USBRESET, &udc_regs_p->dev_int);
837 * This endpoint0 specific register can be programmed only
838 * after the phy clock is initialized
840 writel((EP0_MAX_PACKET_SIZE << 19) | ENDP_EPTYPE_CNTL,
841 &udc_regs_p->udc_endp_reg[0]);
843 UDCDBG("device reset in progess");
844 udc_state_transition(udc_device->device_state, STATE_DEFAULT);
847 /* Device Enumeration completed */
848 if (readl(&udc_regs_p->dev_int) & DEV_INT_ENUM) {
849 writel(DEV_INT_ENUM, &udc_regs_p->dev_int);
851 /* Endpoint interrupt enabled for Ctrl IN & Ctrl OUT */
852 writel(readl(&udc_regs_p->endp_int_mask) & ~0x10001,
853 &udc_regs_p->endp_int_mask);
855 UDCDBG("default -> addressed");
856 udc_state_transition(udc_device->device_state, STATE_ADDRESSED);
859 /* The USB will be in SUSPEND in 3 ms */
860 if (readl(&udc_regs_p->dev_int) & DEV_INT_INACTIVE) {
861 writel(DEV_INT_INACTIVE, &udc_regs_p->dev_int);
863 UDCDBG("entering inactive state");
864 /* usbd_device_event_irq(udc_device, DEVICE_BUS_INACTIVE, 0); */
867 /* SetConfiguration command received */
868 if (readl(&udc_regs_p->dev_int) & DEV_INT_SETCFG) {
869 writel(DEV_INT_SETCFG, &udc_regs_p->dev_int);
871 UDCDBG("entering configured state");
872 udc_state_transition(udc_device->device_state,
876 /* SetInterface command received */
877 if (readl(&udc_regs_p->dev_int) & DEV_INT_SETINTF)
878 writel(DEV_INT_SETINTF, &udc_regs_p->dev_int);
880 /* USB Suspend detected on cable */
881 if (readl(&udc_regs_p->dev_int) & DEV_INT_SUSPUSB) {
882 writel(DEV_INT_SUSPUSB, &udc_regs_p->dev_int);
884 UDCDBG("entering suspended state");
885 usbd_device_event_irq(udc_device, DEVICE_BUS_INACTIVE, 0);
888 /* USB Start-Of-Frame detected on cable */
889 if (readl(&udc_regs_p->dev_int) & DEV_INT_SOF)
890 writel(DEV_INT_SOF, &udc_regs_p->dev_int);
894 * Endpoint interrupt handling
896 static void dw_udc_endpoint_irq(void)
898 while (readl(&udc_regs_p->endp_int) & ENDP0_INT_CTRLOUT) {
900 writel(ENDP0_INT_CTRLOUT, &udc_regs_p->endp_int);
902 if ((readl(&outep_regs_p[0].endp_status) & ENDP_STATUS_OUTMSK)
903 == ENDP_STATUS_OUT_SETUP) {
904 dw_udc_setup(udc_device->bus->endpoint_array + 0);
905 writel(ENDP_STATUS_OUT_SETUP,
906 &outep_regs_p[0].endp_status);
908 } else if ((readl(&outep_regs_p[0].endp_status) &
909 ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_DATA) {
910 dw_udc_ep0_rx(udc_device->bus->endpoint_array + 0);
911 writel(ENDP_STATUS_OUT_DATA,
912 &outep_regs_p[0].endp_status);
914 } else if ((readl(&outep_regs_p[0].endp_status) &
915 ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_NONE) {
919 writel(0x0, &outep_regs_p[0].endp_status);
922 if (readl(&udc_regs_p->endp_int) & ENDP0_INT_CTRLIN) {
923 dw_udc_ep0_tx(udc_device->bus->endpoint_array + 0);
925 writel(ENDP_STATUS_IN, &inep_regs_p[0].endp_status);
926 writel(ENDP0_INT_CTRLIN, &udc_regs_p->endp_int);
929 if (readl(&udc_regs_p->endp_int) & ENDP_INT_NONISOOUT_MSK) {
931 u32 ep_int = readl(&udc_regs_p->endp_int) &
932 ENDP_INT_NONISOOUT_MSK;
935 while (0x0 == (ep_int & 0x1)) {
940 writel((1 << 16) << epnum, &udc_regs_p->endp_int);
942 if ((readl(&outep_regs_p[epnum].endp_status) &
943 ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_DATA) {
945 dw_udc_epn_rx(epnum);
946 writel(ENDP_STATUS_OUT_DATA,
947 &outep_regs_p[epnum].endp_status);
948 } else if ((readl(&outep_regs_p[epnum].endp_status) &
949 ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_NONE) {
950 writel(0x0, &outep_regs_p[epnum].endp_status);
954 if (readl(&udc_regs_p->endp_int) & ENDP_INT_NONISOIN_MSK) {
956 u32 ep_int = readl(&udc_regs_p->endp_int) &
957 ENDP_INT_NONISOIN_MSK;
959 while (0x0 == (ep_int & 0x1)) {
964 if (readl(&inep_regs_p[epnum].endp_status) & ENDP_STATUS_IN) {
965 writel(ENDP_STATUS_IN,
966 &outep_regs_p[epnum].endp_status);
967 dw_udc_epn_tx(epnum);
969 writel(ENDP_STATUS_IN,
970 &outep_regs_p[epnum].endp_status);
973 writel((1 << epnum), &udc_regs_p->endp_int);
983 * Loop while we have interrupts.
984 * If we don't do this, the input chain
985 * polling delay is likely to miss
988 while (readl(&plug_regs_p->plug_pending))
991 while (readl(&udc_regs_p->dev_int))
994 if (readl(&udc_regs_p->endp_int))
995 dw_udc_endpoint_irq();
999 void udc_set_nak(int epid)
1001 writel(readl(&inep_regs_p[epid].endp_cntl) | ENDP_CNTL_SNAK,
1002 &inep_regs_p[epid].endp_cntl);
1004 writel(readl(&outep_regs_p[epid].endp_cntl) | ENDP_CNTL_SNAK,
1005 &outep_regs_p[epid].endp_cntl);
1008 void udc_unset_nak(int epid)
1012 val = readl(&inep_regs_p[epid].endp_cntl);
1013 val &= ~ENDP_CNTL_SNAK;
1014 val |= ENDP_CNTL_CNAK;
1015 writel(val, &inep_regs_p[epid].endp_cntl);
1017 val = readl(&outep_regs_p[epid].endp_cntl);
1018 val &= ~ENDP_CNTL_SNAK;
1019 val |= ENDP_CNTL_CNAK;
1020 writel(val, &outep_regs_p[epid].endp_cntl);