162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * MUSB OTG driver host support
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright 2005 Mentor Graphics Corporation
662306a36Sopenharmony_ci * Copyright (C) 2005-2006 by Texas Instruments
762306a36Sopenharmony_ci * Copyright (C) 2006-2007 Nokia Corporation
862306a36Sopenharmony_ci * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/module.h>
1262306a36Sopenharmony_ci#include <linux/kernel.h>
1362306a36Sopenharmony_ci#include <linux/delay.h>
1462306a36Sopenharmony_ci#include <linux/sched.h>
1562306a36Sopenharmony_ci#include <linux/slab.h>
1662306a36Sopenharmony_ci#include <linux/errno.h>
1762306a36Sopenharmony_ci#include <linux/list.h>
1862306a36Sopenharmony_ci#include <linux/dma-mapping.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#include "musb_core.h"
2162306a36Sopenharmony_ci#include "musb_host.h"
2262306a36Sopenharmony_ci#include "musb_trace.h"
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/* MUSB HOST status 22-mar-2006
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci * - There's still lots of partial code duplication for fault paths, so
2762306a36Sopenharmony_ci *   they aren't handled as consistently as they need to be.
2862306a36Sopenharmony_ci *
2962306a36Sopenharmony_ci * - PIO mostly behaved when last tested.
3062306a36Sopenharmony_ci *     + including ep0, with all usbtest cases 9, 10
3162306a36Sopenharmony_ci *     + usbtest 14 (ep0out) doesn't seem to run at all
3262306a36Sopenharmony_ci *     + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
3362306a36Sopenharmony_ci *       configurations, but otherwise double buffering passes basic tests.
3462306a36Sopenharmony_ci *     + for 2.6.N, for N > ~10, needs API changes for hcd framework.
3562306a36Sopenharmony_ci *
3662306a36Sopenharmony_ci * - DMA (CPPI) ... partially behaves, not currently recommended
3762306a36Sopenharmony_ci *     + about 1/15 the speed of typical EHCI implementations (PCI)
3862306a36Sopenharmony_ci *     + RX, all too often reqpkt seems to misbehave after tx
3962306a36Sopenharmony_ci *     + TX, no known issues (other than evident silicon issue)
4062306a36Sopenharmony_ci *
4162306a36Sopenharmony_ci * - DMA (Mentor/OMAP) ...has at least toggle update problems
4262306a36Sopenharmony_ci *
4362306a36Sopenharmony_ci * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet
4462306a36Sopenharmony_ci *   starvation ... nothing yet for TX, interrupt, or bulk.
4562306a36Sopenharmony_ci *
4662306a36Sopenharmony_ci * - Not tested with HNP, but some SRP paths seem to behave.
4762306a36Sopenharmony_ci *
4862306a36Sopenharmony_ci * NOTE 24-August-2006:
4962306a36Sopenharmony_ci *
5062306a36Sopenharmony_ci * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
5162306a36Sopenharmony_ci *   extra endpoint for periodic use enabling hub + keybd + mouse.  That
5262306a36Sopenharmony_ci *   mostly works, except that with "usbnet" it's easy to trigger cases
5362306a36Sopenharmony_ci *   with "ping" where RX loses.  (a) ping to davinci, even "ping -f",
5462306a36Sopenharmony_ci *   fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
5562306a36Sopenharmony_ci *   although ARP RX wins.  (That test was done with a full speed link.)
5662306a36Sopenharmony_ci */
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci/*
6062306a36Sopenharmony_ci * NOTE on endpoint usage:
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci * CONTROL transfers all go through ep0.  BULK ones go through dedicated IN
6362306a36Sopenharmony_ci * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
6462306a36Sopenharmony_ci * (Yes, bulk _could_ use more of the endpoints than that, and would even
6562306a36Sopenharmony_ci * benefit from it.)
6662306a36Sopenharmony_ci *
6762306a36Sopenharmony_ci * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
6862306a36Sopenharmony_ci * So far that scheduling is both dumb and optimistic:  the endpoint will be
6962306a36Sopenharmony_ci * "claimed" until its software queue is no longer refilled.  No multiplexing
7062306a36Sopenharmony_ci * of transfers between endpoints, or anything clever.
7162306a36Sopenharmony_ci */
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cistruct musb *hcd_to_musb(struct usb_hcd *hcd)
7462306a36Sopenharmony_ci{
7562306a36Sopenharmony_ci	return *(struct musb **) hcd->hcd_priv;
7662306a36Sopenharmony_ci}
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_cistatic void musb_ep_program(struct musb *musb, u8 epnum,
8062306a36Sopenharmony_ci			struct urb *urb, int is_out,
8162306a36Sopenharmony_ci			u8 *buf, u32 offset, u32 len);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci/*
8462306a36Sopenharmony_ci * Clear TX fifo. Needed to avoid BABBLE errors.
8562306a36Sopenharmony_ci */
8662306a36Sopenharmony_cistatic void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
8762306a36Sopenharmony_ci{
8862306a36Sopenharmony_ci	struct musb	*musb = ep->musb;
8962306a36Sopenharmony_ci	void __iomem	*epio = ep->regs;
9062306a36Sopenharmony_ci	u16		csr;
9162306a36Sopenharmony_ci	int		retries = 1000;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	csr = musb_readw(epio, MUSB_TXCSR);
9462306a36Sopenharmony_ci	while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
9562306a36Sopenharmony_ci		csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_TXPKTRDY;
9662306a36Sopenharmony_ci		musb_writew(epio, MUSB_TXCSR, csr);
9762306a36Sopenharmony_ci		csr = musb_readw(epio, MUSB_TXCSR);
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci		/*
10062306a36Sopenharmony_ci		 * FIXME: sometimes the tx fifo flush failed, it has been
10162306a36Sopenharmony_ci		 * observed during device disconnect on AM335x.
10262306a36Sopenharmony_ci		 *
10362306a36Sopenharmony_ci		 * To reproduce the issue, ensure tx urb(s) are queued when
10462306a36Sopenharmony_ci		 * unplug the usb device which is connected to AM335x usb
10562306a36Sopenharmony_ci		 * host port.
10662306a36Sopenharmony_ci		 *
10762306a36Sopenharmony_ci		 * I found using a usb-ethernet device and running iperf
10862306a36Sopenharmony_ci		 * (client on AM335x) has very high chance to trigger it.
10962306a36Sopenharmony_ci		 *
11062306a36Sopenharmony_ci		 * Better to turn on musb_dbg() in musb_cleanup_urb() with
11162306a36Sopenharmony_ci		 * CPPI enabled to see the issue when aborting the tx channel.
11262306a36Sopenharmony_ci		 */
11362306a36Sopenharmony_ci		if (dev_WARN_ONCE(musb->controller, retries-- < 1,
11462306a36Sopenharmony_ci				"Could not flush host TX%d fifo: csr: %04x\n",
11562306a36Sopenharmony_ci				ep->epnum, csr))
11662306a36Sopenharmony_ci			return;
11762306a36Sopenharmony_ci		mdelay(1);
11862306a36Sopenharmony_ci	}
11962306a36Sopenharmony_ci}
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_cistatic void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
12262306a36Sopenharmony_ci{
12362306a36Sopenharmony_ci	void __iomem	*epio = ep->regs;
12462306a36Sopenharmony_ci	u16		csr;
12562306a36Sopenharmony_ci	int		retries = 5;
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	/* scrub any data left in the fifo */
12862306a36Sopenharmony_ci	do {
12962306a36Sopenharmony_ci		csr = musb_readw(epio, MUSB_TXCSR);
13062306a36Sopenharmony_ci		if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
13162306a36Sopenharmony_ci			break;
13262306a36Sopenharmony_ci		musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
13362306a36Sopenharmony_ci		csr = musb_readw(epio, MUSB_TXCSR);
13462306a36Sopenharmony_ci		udelay(10);
13562306a36Sopenharmony_ci	} while (--retries);
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
13862306a36Sopenharmony_ci			ep->epnum, csr);
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	/* and reset for the next transfer */
14162306a36Sopenharmony_ci	musb_writew(epio, MUSB_TXCSR, 0);
14262306a36Sopenharmony_ci}
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/*
14562306a36Sopenharmony_ci * Start transmit. Caller is responsible for locking shared resources.
14662306a36Sopenharmony_ci * musb must be locked.
14762306a36Sopenharmony_ci */
14862306a36Sopenharmony_cistatic inline void musb_h_tx_start(struct musb_hw_ep *ep)
14962306a36Sopenharmony_ci{
15062306a36Sopenharmony_ci	u16	txcsr;
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	/* NOTE: no locks here; caller should lock and select EP */
15362306a36Sopenharmony_ci	if (ep->epnum) {
15462306a36Sopenharmony_ci		txcsr = musb_readw(ep->regs, MUSB_TXCSR);
15562306a36Sopenharmony_ci		txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
15662306a36Sopenharmony_ci		musb_writew(ep->regs, MUSB_TXCSR, txcsr);
15762306a36Sopenharmony_ci	} else {
15862306a36Sopenharmony_ci		txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
15962306a36Sopenharmony_ci		musb_writew(ep->regs, MUSB_CSR0, txcsr);
16062306a36Sopenharmony_ci	}
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci}
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_cistatic inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
16562306a36Sopenharmony_ci{
16662306a36Sopenharmony_ci	u16	txcsr;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	/* NOTE: no locks here; caller should lock and select EP */
16962306a36Sopenharmony_ci	txcsr = musb_readw(ep->regs, MUSB_TXCSR);
17062306a36Sopenharmony_ci	txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
17162306a36Sopenharmony_ci	if (is_cppi_enabled(ep->musb))
17262306a36Sopenharmony_ci		txcsr |= MUSB_TXCSR_DMAMODE;
17362306a36Sopenharmony_ci	musb_writew(ep->regs, MUSB_TXCSR, txcsr);
17462306a36Sopenharmony_ci}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_cistatic void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
17762306a36Sopenharmony_ci{
17862306a36Sopenharmony_ci	if (is_in != 0 || ep->is_shared_fifo)
17962306a36Sopenharmony_ci		ep->in_qh  = qh;
18062306a36Sopenharmony_ci	if (is_in == 0 || ep->is_shared_fifo)
18162306a36Sopenharmony_ci		ep->out_qh = qh;
18262306a36Sopenharmony_ci}
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_cistatic struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
18562306a36Sopenharmony_ci{
18662306a36Sopenharmony_ci	return is_in ? ep->in_qh : ep->out_qh;
18762306a36Sopenharmony_ci}
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci/*
19062306a36Sopenharmony_ci * Start the URB at the front of an endpoint's queue
19162306a36Sopenharmony_ci * end must be claimed from the caller.
19262306a36Sopenharmony_ci *
19362306a36Sopenharmony_ci * Context: controller locked, irqs blocked
19462306a36Sopenharmony_ci */
19562306a36Sopenharmony_cistatic void
19662306a36Sopenharmony_cimusb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
19762306a36Sopenharmony_ci{
19862306a36Sopenharmony_ci	u32			len;
19962306a36Sopenharmony_ci	void __iomem		*mbase =  musb->mregs;
20062306a36Sopenharmony_ci	struct urb		*urb = next_urb(qh);
20162306a36Sopenharmony_ci	void			*buf = urb->transfer_buffer;
20262306a36Sopenharmony_ci	u32			offset = 0;
20362306a36Sopenharmony_ci	struct musb_hw_ep	*hw_ep = qh->hw_ep;
20462306a36Sopenharmony_ci	int			epnum = hw_ep->epnum;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	/* initialize software qh state */
20762306a36Sopenharmony_ci	qh->offset = 0;
20862306a36Sopenharmony_ci	qh->segsize = 0;
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	/* gather right source of data */
21162306a36Sopenharmony_ci	switch (qh->type) {
21262306a36Sopenharmony_ci	case USB_ENDPOINT_XFER_CONTROL:
21362306a36Sopenharmony_ci		/* control transfers always start with SETUP */
21462306a36Sopenharmony_ci		is_in = 0;
21562306a36Sopenharmony_ci		musb->ep0_stage = MUSB_EP0_START;
21662306a36Sopenharmony_ci		buf = urb->setup_packet;
21762306a36Sopenharmony_ci		len = 8;
21862306a36Sopenharmony_ci		break;
21962306a36Sopenharmony_ci	case USB_ENDPOINT_XFER_ISOC:
22062306a36Sopenharmony_ci		qh->iso_idx = 0;
22162306a36Sopenharmony_ci		qh->frame = 0;
22262306a36Sopenharmony_ci		offset = urb->iso_frame_desc[0].offset;
22362306a36Sopenharmony_ci		len = urb->iso_frame_desc[0].length;
22462306a36Sopenharmony_ci		break;
22562306a36Sopenharmony_ci	default:		/* bulk, interrupt */
22662306a36Sopenharmony_ci		/* actual_length may be nonzero on retry paths */
22762306a36Sopenharmony_ci		buf = urb->transfer_buffer + urb->actual_length;
22862306a36Sopenharmony_ci		len = urb->transfer_buffer_length - urb->actual_length;
22962306a36Sopenharmony_ci	}
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci	trace_musb_urb_start(musb, urb);
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci	/* Configure endpoint */
23462306a36Sopenharmony_ci	musb_ep_set_qh(hw_ep, is_in, qh);
23562306a36Sopenharmony_ci	musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	/* transmit may have more work: start it when it is time */
23862306a36Sopenharmony_ci	if (is_in)
23962306a36Sopenharmony_ci		return;
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci	/* determine if the time is right for a periodic transfer */
24262306a36Sopenharmony_ci	switch (qh->type) {
24362306a36Sopenharmony_ci	case USB_ENDPOINT_XFER_ISOC:
24462306a36Sopenharmony_ci	case USB_ENDPOINT_XFER_INT:
24562306a36Sopenharmony_ci		musb_dbg(musb, "check whether there's still time for periodic Tx");
24662306a36Sopenharmony_ci		/* FIXME this doesn't implement that scheduling policy ...
24762306a36Sopenharmony_ci		 * or handle framecounter wrapping
24862306a36Sopenharmony_ci		 */
24962306a36Sopenharmony_ci		if (1) {	/* Always assume URB_ISO_ASAP */
25062306a36Sopenharmony_ci			/* REVISIT the SOF irq handler shouldn't duplicate
25162306a36Sopenharmony_ci			 * this code; and we don't init urb->start_frame...
25262306a36Sopenharmony_ci			 */
25362306a36Sopenharmony_ci			qh->frame = 0;
25462306a36Sopenharmony_ci			goto start;
25562306a36Sopenharmony_ci		} else {
25662306a36Sopenharmony_ci			qh->frame = urb->start_frame;
25762306a36Sopenharmony_ci			/* enable SOF interrupt so we can count down */
25862306a36Sopenharmony_ci			musb_dbg(musb, "SOF for %d", epnum);
25962306a36Sopenharmony_ci#if 1 /* ifndef	CONFIG_ARCH_DAVINCI */
26062306a36Sopenharmony_ci			musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
26162306a36Sopenharmony_ci#endif
26262306a36Sopenharmony_ci		}
26362306a36Sopenharmony_ci		break;
26462306a36Sopenharmony_ci	default:
26562306a36Sopenharmony_cistart:
26662306a36Sopenharmony_ci		musb_dbg(musb, "Start TX%d %s", epnum,
26762306a36Sopenharmony_ci			hw_ep->tx_channel ? "dma" : "pio");
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci		if (!hw_ep->tx_channel)
27062306a36Sopenharmony_ci			musb_h_tx_start(hw_ep);
27162306a36Sopenharmony_ci		else if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
27262306a36Sopenharmony_ci			musb_h_tx_dma_start(hw_ep);
27362306a36Sopenharmony_ci	}
27462306a36Sopenharmony_ci}
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci/* Context: caller owns controller lock, IRQs are blocked */
27762306a36Sopenharmony_cistatic void musb_giveback(struct musb *musb, struct urb *urb, int status)
27862306a36Sopenharmony_ci__releases(musb->lock)
27962306a36Sopenharmony_ci__acquires(musb->lock)
28062306a36Sopenharmony_ci{
28162306a36Sopenharmony_ci	trace_musb_urb_gb(musb, urb);
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci	usb_hcd_unlink_urb_from_ep(musb->hcd, urb);
28462306a36Sopenharmony_ci	spin_unlock(&musb->lock);
28562306a36Sopenharmony_ci	usb_hcd_giveback_urb(musb->hcd, urb, status);
28662306a36Sopenharmony_ci	spin_lock(&musb->lock);
28762306a36Sopenharmony_ci}
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci/*
29062306a36Sopenharmony_ci * Advance this hardware endpoint's queue, completing the specified URB and
29162306a36Sopenharmony_ci * advancing to either the next URB queued to that qh, or else invalidating
29262306a36Sopenharmony_ci * that qh and advancing to the next qh scheduled after the current one.
29362306a36Sopenharmony_ci *
29462306a36Sopenharmony_ci * Context: caller owns controller lock, IRQs are blocked
29562306a36Sopenharmony_ci */
29662306a36Sopenharmony_cistatic void musb_advance_schedule(struct musb *musb, struct urb *urb,
29762306a36Sopenharmony_ci				  struct musb_hw_ep *hw_ep, int is_in)
29862306a36Sopenharmony_ci{
29962306a36Sopenharmony_ci	struct musb_qh		*qh = musb_ep_get_qh(hw_ep, is_in);
30062306a36Sopenharmony_ci	struct musb_hw_ep	*ep = qh->hw_ep;
30162306a36Sopenharmony_ci	int			ready = qh->is_ready;
30262306a36Sopenharmony_ci	int			status;
30362306a36Sopenharmony_ci	u16			toggle;
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci	status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	/* save toggle eagerly, for paranoia */
30862306a36Sopenharmony_ci	switch (qh->type) {
30962306a36Sopenharmony_ci	case USB_ENDPOINT_XFER_BULK:
31062306a36Sopenharmony_ci	case USB_ENDPOINT_XFER_INT:
31162306a36Sopenharmony_ci		toggle = musb->io.get_toggle(qh, !is_in);
31262306a36Sopenharmony_ci		usb_settoggle(urb->dev, qh->epnum, !is_in, toggle ? 1 : 0);
31362306a36Sopenharmony_ci		break;
31462306a36Sopenharmony_ci	case USB_ENDPOINT_XFER_ISOC:
31562306a36Sopenharmony_ci		if (status == 0 && urb->error_count)
31662306a36Sopenharmony_ci			status = -EXDEV;
31762306a36Sopenharmony_ci		break;
31862306a36Sopenharmony_ci	}
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci	qh->is_ready = 0;
32162306a36Sopenharmony_ci	musb_giveback(musb, urb, status);
32262306a36Sopenharmony_ci	qh->is_ready = ready;
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci	/*
32562306a36Sopenharmony_ci	 * musb->lock had been unlocked in musb_giveback, so qh may
32662306a36Sopenharmony_ci	 * be freed, need to get it again
32762306a36Sopenharmony_ci	 */
32862306a36Sopenharmony_ci	qh = musb_ep_get_qh(hw_ep, is_in);
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci	/* reclaim resources (and bandwidth) ASAP; deschedule it, and
33162306a36Sopenharmony_ci	 * invalidate qh as soon as list_empty(&hep->urb_list)
33262306a36Sopenharmony_ci	 */
33362306a36Sopenharmony_ci	if (qh && list_empty(&qh->hep->urb_list)) {
33462306a36Sopenharmony_ci		struct list_head	*head;
33562306a36Sopenharmony_ci		struct dma_controller	*dma = musb->dma_controller;
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci		if (is_in) {
33862306a36Sopenharmony_ci			ep->rx_reinit = 1;
33962306a36Sopenharmony_ci			if (ep->rx_channel) {
34062306a36Sopenharmony_ci				dma->channel_release(ep->rx_channel);
34162306a36Sopenharmony_ci				ep->rx_channel = NULL;
34262306a36Sopenharmony_ci			}
34362306a36Sopenharmony_ci		} else {
34462306a36Sopenharmony_ci			ep->tx_reinit = 1;
34562306a36Sopenharmony_ci			if (ep->tx_channel) {
34662306a36Sopenharmony_ci				dma->channel_release(ep->tx_channel);
34762306a36Sopenharmony_ci				ep->tx_channel = NULL;
34862306a36Sopenharmony_ci			}
34962306a36Sopenharmony_ci		}
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci		/* Clobber old pointers to this qh */
35262306a36Sopenharmony_ci		musb_ep_set_qh(ep, is_in, NULL);
35362306a36Sopenharmony_ci		qh->hep->hcpriv = NULL;
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci		switch (qh->type) {
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci		case USB_ENDPOINT_XFER_CONTROL:
35862306a36Sopenharmony_ci		case USB_ENDPOINT_XFER_BULK:
35962306a36Sopenharmony_ci			/* fifo policy for these lists, except that NAKing
36062306a36Sopenharmony_ci			 * should rotate a qh to the end (for fairness).
36162306a36Sopenharmony_ci			 */
36262306a36Sopenharmony_ci			if (qh->mux == 1) {
36362306a36Sopenharmony_ci				head = qh->ring.prev;
36462306a36Sopenharmony_ci				list_del(&qh->ring);
36562306a36Sopenharmony_ci				kfree(qh);
36662306a36Sopenharmony_ci				qh = first_qh(head);
36762306a36Sopenharmony_ci				break;
36862306a36Sopenharmony_ci			}
36962306a36Sopenharmony_ci			fallthrough;
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci		case USB_ENDPOINT_XFER_ISOC:
37262306a36Sopenharmony_ci		case USB_ENDPOINT_XFER_INT:
37362306a36Sopenharmony_ci			/* this is where periodic bandwidth should be
37462306a36Sopenharmony_ci			 * de-allocated if it's tracked and allocated;
37562306a36Sopenharmony_ci			 * and where we'd update the schedule tree...
37662306a36Sopenharmony_ci			 */
37762306a36Sopenharmony_ci			kfree(qh);
37862306a36Sopenharmony_ci			qh = NULL;
37962306a36Sopenharmony_ci			break;
38062306a36Sopenharmony_ci		}
38162306a36Sopenharmony_ci	}
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci	if (qh != NULL && qh->is_ready) {
38462306a36Sopenharmony_ci		musb_dbg(musb, "... next ep%d %cX urb %p",
38562306a36Sopenharmony_ci		    hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
38662306a36Sopenharmony_ci		musb_start_urb(musb, is_in, qh);
38762306a36Sopenharmony_ci	}
38862306a36Sopenharmony_ci}
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_cistatic u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
39162306a36Sopenharmony_ci{
39262306a36Sopenharmony_ci	/* we don't want fifo to fill itself again;
39362306a36Sopenharmony_ci	 * ignore dma (various models),
39462306a36Sopenharmony_ci	 * leave toggle alone (may not have been saved yet)
39562306a36Sopenharmony_ci	 */
39662306a36Sopenharmony_ci	csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
39762306a36Sopenharmony_ci	csr &= ~(MUSB_RXCSR_H_REQPKT
39862306a36Sopenharmony_ci		| MUSB_RXCSR_H_AUTOREQ
39962306a36Sopenharmony_ci		| MUSB_RXCSR_AUTOCLEAR);
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci	/* write 2x to allow double buffering */
40262306a36Sopenharmony_ci	musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
40362306a36Sopenharmony_ci	musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci	/* flush writebuffer */
40662306a36Sopenharmony_ci	return musb_readw(hw_ep->regs, MUSB_RXCSR);
40762306a36Sopenharmony_ci}
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci/*
41062306a36Sopenharmony_ci * PIO RX for a packet (or part of it).
41162306a36Sopenharmony_ci */
41262306a36Sopenharmony_cistatic bool
41362306a36Sopenharmony_cimusb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
41462306a36Sopenharmony_ci{
41562306a36Sopenharmony_ci	u16			rx_count;
41662306a36Sopenharmony_ci	u8			*buf;
41762306a36Sopenharmony_ci	u16			csr;
41862306a36Sopenharmony_ci	bool			done = false;
41962306a36Sopenharmony_ci	u32			length;
42062306a36Sopenharmony_ci	int			do_flush = 0;
42162306a36Sopenharmony_ci	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
42262306a36Sopenharmony_ci	void __iomem		*epio = hw_ep->regs;
42362306a36Sopenharmony_ci	struct musb_qh		*qh = hw_ep->in_qh;
42462306a36Sopenharmony_ci	int			pipe = urb->pipe;
42562306a36Sopenharmony_ci	void			*buffer = urb->transfer_buffer;
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci	/* musb_ep_select(mbase, epnum); */
42862306a36Sopenharmony_ci	rx_count = musb_readw(epio, MUSB_RXCOUNT);
42962306a36Sopenharmony_ci	musb_dbg(musb, "RX%d count %d, buffer %p len %d/%d", epnum, rx_count,
43062306a36Sopenharmony_ci			urb->transfer_buffer, qh->offset,
43162306a36Sopenharmony_ci			urb->transfer_buffer_length);
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci	/* unload FIFO */
43462306a36Sopenharmony_ci	if (usb_pipeisoc(pipe)) {
43562306a36Sopenharmony_ci		int					status = 0;
43662306a36Sopenharmony_ci		struct usb_iso_packet_descriptor	*d;
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci		if (iso_err) {
43962306a36Sopenharmony_ci			status = -EILSEQ;
44062306a36Sopenharmony_ci			urb->error_count++;
44162306a36Sopenharmony_ci		}
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci		d = urb->iso_frame_desc + qh->iso_idx;
44462306a36Sopenharmony_ci		buf = buffer + d->offset;
44562306a36Sopenharmony_ci		length = d->length;
44662306a36Sopenharmony_ci		if (rx_count > length) {
44762306a36Sopenharmony_ci			if (status == 0) {
44862306a36Sopenharmony_ci				status = -EOVERFLOW;
44962306a36Sopenharmony_ci				urb->error_count++;
45062306a36Sopenharmony_ci			}
45162306a36Sopenharmony_ci			musb_dbg(musb, "OVERFLOW %d into %d", rx_count, length);
45262306a36Sopenharmony_ci			do_flush = 1;
45362306a36Sopenharmony_ci		} else
45462306a36Sopenharmony_ci			length = rx_count;
45562306a36Sopenharmony_ci		urb->actual_length += length;
45662306a36Sopenharmony_ci		d->actual_length = length;
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ci		d->status = status;
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci		/* see if we are done */
46162306a36Sopenharmony_ci		done = (++qh->iso_idx >= urb->number_of_packets);
46262306a36Sopenharmony_ci	} else {
46362306a36Sopenharmony_ci		/* non-isoch */
46462306a36Sopenharmony_ci		buf = buffer + qh->offset;
46562306a36Sopenharmony_ci		length = urb->transfer_buffer_length - qh->offset;
46662306a36Sopenharmony_ci		if (rx_count > length) {
46762306a36Sopenharmony_ci			if (urb->status == -EINPROGRESS)
46862306a36Sopenharmony_ci				urb->status = -EOVERFLOW;
46962306a36Sopenharmony_ci			musb_dbg(musb, "OVERFLOW %d into %d", rx_count, length);
47062306a36Sopenharmony_ci			do_flush = 1;
47162306a36Sopenharmony_ci		} else
47262306a36Sopenharmony_ci			length = rx_count;
47362306a36Sopenharmony_ci		urb->actual_length += length;
47462306a36Sopenharmony_ci		qh->offset += length;
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_ci		/* see if we are done */
47762306a36Sopenharmony_ci		done = (urb->actual_length == urb->transfer_buffer_length)
47862306a36Sopenharmony_ci			|| (rx_count < qh->maxpacket)
47962306a36Sopenharmony_ci			|| (urb->status != -EINPROGRESS);
48062306a36Sopenharmony_ci		if (done
48162306a36Sopenharmony_ci				&& (urb->status == -EINPROGRESS)
48262306a36Sopenharmony_ci				&& (urb->transfer_flags & URB_SHORT_NOT_OK)
48362306a36Sopenharmony_ci				&& (urb->actual_length
48462306a36Sopenharmony_ci					< urb->transfer_buffer_length))
48562306a36Sopenharmony_ci			urb->status = -EREMOTEIO;
48662306a36Sopenharmony_ci	}
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	musb_read_fifo(hw_ep, length, buf);
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci	csr = musb_readw(epio, MUSB_RXCSR);
49162306a36Sopenharmony_ci	csr |= MUSB_RXCSR_H_WZC_BITS;
49262306a36Sopenharmony_ci	if (unlikely(do_flush))
49362306a36Sopenharmony_ci		musb_h_flush_rxfifo(hw_ep, csr);
49462306a36Sopenharmony_ci	else {
49562306a36Sopenharmony_ci		/* REVISIT this assumes AUTOCLEAR is never set */
49662306a36Sopenharmony_ci		csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
49762306a36Sopenharmony_ci		if (!done)
49862306a36Sopenharmony_ci			csr |= MUSB_RXCSR_H_REQPKT;
49962306a36Sopenharmony_ci		musb_writew(epio, MUSB_RXCSR, csr);
50062306a36Sopenharmony_ci	}
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_ci	return done;
50362306a36Sopenharmony_ci}
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ci/* we don't always need to reinit a given side of an endpoint...
50662306a36Sopenharmony_ci * when we do, use tx/rx reinit routine and then construct a new CSR
50762306a36Sopenharmony_ci * to address data toggle, NYET, and DMA or PIO.
50862306a36Sopenharmony_ci *
50962306a36Sopenharmony_ci * it's possible that driver bugs (especially for DMA) or aborting a
51062306a36Sopenharmony_ci * transfer might have left the endpoint busier than it should be.
51162306a36Sopenharmony_ci * the busy/not-empty tests are basically paranoia.
51262306a36Sopenharmony_ci */
51362306a36Sopenharmony_cistatic void
51462306a36Sopenharmony_cimusb_rx_reinit(struct musb *musb, struct musb_qh *qh, u8 epnum)
51562306a36Sopenharmony_ci{
51662306a36Sopenharmony_ci	struct musb_hw_ep *ep = musb->endpoints + epnum;
51762306a36Sopenharmony_ci	u16	csr;
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci	/* NOTE:  we know the "rx" fifo reinit never triggers for ep0.
52062306a36Sopenharmony_ci	 * That always uses tx_reinit since ep0 repurposes TX register
52162306a36Sopenharmony_ci	 * offsets; the initial SETUP packet is also a kind of OUT.
52262306a36Sopenharmony_ci	 */
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ci	/* if programmed for Tx, put it in RX mode */
52562306a36Sopenharmony_ci	if (ep->is_shared_fifo) {
52662306a36Sopenharmony_ci		csr = musb_readw(ep->regs, MUSB_TXCSR);
52762306a36Sopenharmony_ci		if (csr & MUSB_TXCSR_MODE) {
52862306a36Sopenharmony_ci			musb_h_tx_flush_fifo(ep);
52962306a36Sopenharmony_ci			csr = musb_readw(ep->regs, MUSB_TXCSR);
53062306a36Sopenharmony_ci			musb_writew(ep->regs, MUSB_TXCSR,
53162306a36Sopenharmony_ci				    csr | MUSB_TXCSR_FRCDATATOG);
53262306a36Sopenharmony_ci		}
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci		/*
53562306a36Sopenharmony_ci		 * Clear the MODE bit (and everything else) to enable Rx.
53662306a36Sopenharmony_ci		 * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
53762306a36Sopenharmony_ci		 */
53862306a36Sopenharmony_ci		if (csr & MUSB_TXCSR_DMAMODE)
53962306a36Sopenharmony_ci			musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
54062306a36Sopenharmony_ci		musb_writew(ep->regs, MUSB_TXCSR, 0);
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_ci	/* scrub all previous state, clearing toggle */
54362306a36Sopenharmony_ci	}
54462306a36Sopenharmony_ci	csr = musb_readw(ep->regs, MUSB_RXCSR);
54562306a36Sopenharmony_ci	if (csr & MUSB_RXCSR_RXPKTRDY)
54662306a36Sopenharmony_ci		WARNING("rx%d, packet/%d ready?\n", ep->epnum,
54762306a36Sopenharmony_ci			musb_readw(ep->regs, MUSB_RXCOUNT));
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci	musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
55062306a36Sopenharmony_ci
55162306a36Sopenharmony_ci	/* target addr and (for multipoint) hub addr/port */
55262306a36Sopenharmony_ci	if (musb->is_multipoint) {
55362306a36Sopenharmony_ci		musb_write_rxfunaddr(musb, epnum, qh->addr_reg);
55462306a36Sopenharmony_ci		musb_write_rxhubaddr(musb, epnum, qh->h_addr_reg);
55562306a36Sopenharmony_ci		musb_write_rxhubport(musb, epnum, qh->h_port_reg);
55662306a36Sopenharmony_ci	} else
55762306a36Sopenharmony_ci		musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ci	/* protocol/endpoint, interval/NAKlimit, i/o size */
56062306a36Sopenharmony_ci	musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
56162306a36Sopenharmony_ci	musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
56262306a36Sopenharmony_ci	/* NOTE: bulk combining rewrites high bits of maxpacket */
56362306a36Sopenharmony_ci	/* Set RXMAXP with the FIFO size of the endpoint
56462306a36Sopenharmony_ci	 * to disable double buffer mode.
56562306a36Sopenharmony_ci	 */
56662306a36Sopenharmony_ci	musb_writew(ep->regs, MUSB_RXMAXP,
56762306a36Sopenharmony_ci			qh->maxpacket | ((qh->hb_mult - 1) << 11));
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci	ep->rx_reinit = 0;
57062306a36Sopenharmony_ci}
57162306a36Sopenharmony_ci
57262306a36Sopenharmony_cistatic void musb_tx_dma_set_mode_mentor(struct musb_hw_ep *hw_ep,
57362306a36Sopenharmony_ci					struct musb_qh *qh,
57462306a36Sopenharmony_ci					u32 *length, u8 *mode)
57562306a36Sopenharmony_ci{
57662306a36Sopenharmony_ci	struct dma_channel	*channel = hw_ep->tx_channel;
57762306a36Sopenharmony_ci	void __iomem		*epio = hw_ep->regs;
57862306a36Sopenharmony_ci	u16			pkt_size = qh->maxpacket;
57962306a36Sopenharmony_ci	u16			csr;
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_ci	if (*length > channel->max_len)
58262306a36Sopenharmony_ci		*length = channel->max_len;
58362306a36Sopenharmony_ci
58462306a36Sopenharmony_ci	csr = musb_readw(epio, MUSB_TXCSR);
58562306a36Sopenharmony_ci	if (*length > pkt_size) {
58662306a36Sopenharmony_ci		*mode = 1;
58762306a36Sopenharmony_ci		csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
58862306a36Sopenharmony_ci		/* autoset shouldn't be set in high bandwidth */
58962306a36Sopenharmony_ci		/*
59062306a36Sopenharmony_ci		 * Enable Autoset according to table
59162306a36Sopenharmony_ci		 * below
59262306a36Sopenharmony_ci		 * bulk_split hb_mult	Autoset_Enable
59362306a36Sopenharmony_ci		 *	0	1	Yes(Normal)
59462306a36Sopenharmony_ci		 *	0	>1	No(High BW ISO)
59562306a36Sopenharmony_ci		 *	1	1	Yes(HS bulk)
59662306a36Sopenharmony_ci		 *	1	>1	Yes(FS bulk)
59762306a36Sopenharmony_ci		 */
59862306a36Sopenharmony_ci		if (qh->hb_mult == 1 || (qh->hb_mult > 1 &&
59962306a36Sopenharmony_ci					can_bulk_split(hw_ep->musb, qh->type)))
60062306a36Sopenharmony_ci			csr |= MUSB_TXCSR_AUTOSET;
60162306a36Sopenharmony_ci	} else {
60262306a36Sopenharmony_ci		*mode = 0;
60362306a36Sopenharmony_ci		csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
60462306a36Sopenharmony_ci		csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
60562306a36Sopenharmony_ci	}
60662306a36Sopenharmony_ci	channel->desired_mode = *mode;
60762306a36Sopenharmony_ci	musb_writew(epio, MUSB_TXCSR, csr);
60862306a36Sopenharmony_ci}
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_cistatic void musb_tx_dma_set_mode_cppi_tusb(struct musb_hw_ep *hw_ep,
61162306a36Sopenharmony_ci					   struct urb *urb,
61262306a36Sopenharmony_ci					   u8 *mode)
61362306a36Sopenharmony_ci{
61462306a36Sopenharmony_ci	struct dma_channel *channel = hw_ep->tx_channel;
61562306a36Sopenharmony_ci
61662306a36Sopenharmony_ci	channel->actual_len = 0;
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_ci	/*
61962306a36Sopenharmony_ci	 * TX uses "RNDIS" mode automatically but needs help
62062306a36Sopenharmony_ci	 * to identify the zero-length-final-packet case.
62162306a36Sopenharmony_ci	 */
62262306a36Sopenharmony_ci	*mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
62362306a36Sopenharmony_ci}
62462306a36Sopenharmony_ci
62562306a36Sopenharmony_cistatic bool musb_tx_dma_program(struct dma_controller *dma,
62662306a36Sopenharmony_ci		struct musb_hw_ep *hw_ep, struct musb_qh *qh,
62762306a36Sopenharmony_ci		struct urb *urb, u32 offset, u32 length)
62862306a36Sopenharmony_ci{
62962306a36Sopenharmony_ci	struct dma_channel	*channel = hw_ep->tx_channel;
63062306a36Sopenharmony_ci	u16			pkt_size = qh->maxpacket;
63162306a36Sopenharmony_ci	u8			mode;
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci	if (musb_dma_inventra(hw_ep->musb) || musb_dma_ux500(hw_ep->musb))
63462306a36Sopenharmony_ci		musb_tx_dma_set_mode_mentor(hw_ep, qh,
63562306a36Sopenharmony_ci					    &length, &mode);
63662306a36Sopenharmony_ci	else if (is_cppi_enabled(hw_ep->musb) || tusb_dma_omap(hw_ep->musb))
63762306a36Sopenharmony_ci		musb_tx_dma_set_mode_cppi_tusb(hw_ep, urb, &mode);
63862306a36Sopenharmony_ci	else
63962306a36Sopenharmony_ci		return false;
64062306a36Sopenharmony_ci
64162306a36Sopenharmony_ci	qh->segsize = length;
64262306a36Sopenharmony_ci
64362306a36Sopenharmony_ci	/*
64462306a36Sopenharmony_ci	 * Ensure the data reaches to main memory before starting
64562306a36Sopenharmony_ci	 * DMA transfer
64662306a36Sopenharmony_ci	 */
64762306a36Sopenharmony_ci	wmb();
64862306a36Sopenharmony_ci
64962306a36Sopenharmony_ci	if (!dma->channel_program(channel, pkt_size, mode,
65062306a36Sopenharmony_ci			urb->transfer_dma + offset, length)) {
65162306a36Sopenharmony_ci		void __iomem *epio = hw_ep->regs;
65262306a36Sopenharmony_ci		u16 csr;
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci		dma->channel_release(channel);
65562306a36Sopenharmony_ci		hw_ep->tx_channel = NULL;
65662306a36Sopenharmony_ci
65762306a36Sopenharmony_ci		csr = musb_readw(epio, MUSB_TXCSR);
65862306a36Sopenharmony_ci		csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
65962306a36Sopenharmony_ci		musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
66062306a36Sopenharmony_ci		return false;
66162306a36Sopenharmony_ci	}
66262306a36Sopenharmony_ci	return true;
66362306a36Sopenharmony_ci}
66462306a36Sopenharmony_ci
66562306a36Sopenharmony_ci/*
66662306a36Sopenharmony_ci * Program an HDRC endpoint as per the given URB
66762306a36Sopenharmony_ci * Context: irqs blocked, controller lock held
66862306a36Sopenharmony_ci */
66962306a36Sopenharmony_cistatic void musb_ep_program(struct musb *musb, u8 epnum,
67062306a36Sopenharmony_ci			struct urb *urb, int is_out,
67162306a36Sopenharmony_ci			u8 *buf, u32 offset, u32 len)
67262306a36Sopenharmony_ci{
67362306a36Sopenharmony_ci	struct dma_controller	*dma_controller;
67462306a36Sopenharmony_ci	struct dma_channel	*dma_channel;
67562306a36Sopenharmony_ci	u8			dma_ok;
67662306a36Sopenharmony_ci	void __iomem		*mbase = musb->mregs;
67762306a36Sopenharmony_ci	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
67862306a36Sopenharmony_ci	void __iomem		*epio = hw_ep->regs;
67962306a36Sopenharmony_ci	struct musb_qh		*qh = musb_ep_get_qh(hw_ep, !is_out);
68062306a36Sopenharmony_ci	u16			packet_sz = qh->maxpacket;
68162306a36Sopenharmony_ci	u8			use_dma = 1;
68262306a36Sopenharmony_ci	u16			csr;
68362306a36Sopenharmony_ci
68462306a36Sopenharmony_ci	musb_dbg(musb, "%s hw%d urb %p spd%d dev%d ep%d%s "
68562306a36Sopenharmony_ci				"h_addr%02x h_port%02x bytes %d",
68662306a36Sopenharmony_ci			is_out ? "-->" : "<--",
68762306a36Sopenharmony_ci			epnum, urb, urb->dev->speed,
68862306a36Sopenharmony_ci			qh->addr_reg, qh->epnum, is_out ? "out" : "in",
68962306a36Sopenharmony_ci			qh->h_addr_reg, qh->h_port_reg,
69062306a36Sopenharmony_ci			len);
69162306a36Sopenharmony_ci
69262306a36Sopenharmony_ci	musb_ep_select(mbase, epnum);
69362306a36Sopenharmony_ci
69462306a36Sopenharmony_ci	if (is_out && !len) {
69562306a36Sopenharmony_ci		use_dma = 0;
69662306a36Sopenharmony_ci		csr = musb_readw(epio, MUSB_TXCSR);
69762306a36Sopenharmony_ci		csr &= ~MUSB_TXCSR_DMAENAB;
69862306a36Sopenharmony_ci		musb_writew(epio, MUSB_TXCSR, csr);
69962306a36Sopenharmony_ci		hw_ep->tx_channel = NULL;
70062306a36Sopenharmony_ci	}
70162306a36Sopenharmony_ci
70262306a36Sopenharmony_ci	/* candidate for DMA? */
70362306a36Sopenharmony_ci	dma_controller = musb->dma_controller;
70462306a36Sopenharmony_ci	if (use_dma && is_dma_capable() && epnum && dma_controller) {
70562306a36Sopenharmony_ci		dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
70662306a36Sopenharmony_ci		if (!dma_channel) {
70762306a36Sopenharmony_ci			dma_channel = dma_controller->channel_alloc(
70862306a36Sopenharmony_ci					dma_controller, hw_ep, is_out);
70962306a36Sopenharmony_ci			if (is_out)
71062306a36Sopenharmony_ci				hw_ep->tx_channel = dma_channel;
71162306a36Sopenharmony_ci			else
71262306a36Sopenharmony_ci				hw_ep->rx_channel = dma_channel;
71362306a36Sopenharmony_ci		}
71462306a36Sopenharmony_ci	} else
71562306a36Sopenharmony_ci		dma_channel = NULL;
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_ci	/* make sure we clear DMAEnab, autoSet bits from previous run */
71862306a36Sopenharmony_ci
71962306a36Sopenharmony_ci	/* OUT/transmit/EP0 or IN/receive? */
72062306a36Sopenharmony_ci	if (is_out) {
72162306a36Sopenharmony_ci		u16	csr;
72262306a36Sopenharmony_ci		u16	int_txe;
72362306a36Sopenharmony_ci		u16	load_count;
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci		csr = musb_readw(epio, MUSB_TXCSR);
72662306a36Sopenharmony_ci
72762306a36Sopenharmony_ci		/* disable interrupt in case we flush */
72862306a36Sopenharmony_ci		int_txe = musb->intrtxe;
72962306a36Sopenharmony_ci		musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
73062306a36Sopenharmony_ci
73162306a36Sopenharmony_ci		/* general endpoint setup */
73262306a36Sopenharmony_ci		if (epnum) {
73362306a36Sopenharmony_ci			/* flush all old state, set default */
73462306a36Sopenharmony_ci			/*
73562306a36Sopenharmony_ci			 * We could be flushing valid
73662306a36Sopenharmony_ci			 * packets in double buffering
73762306a36Sopenharmony_ci			 * case
73862306a36Sopenharmony_ci			 */
73962306a36Sopenharmony_ci			if (!hw_ep->tx_double_buffered)
74062306a36Sopenharmony_ci				musb_h_tx_flush_fifo(hw_ep);
74162306a36Sopenharmony_ci
74262306a36Sopenharmony_ci			/*
74362306a36Sopenharmony_ci			 * We must not clear the DMAMODE bit before or in
74462306a36Sopenharmony_ci			 * the same cycle with the DMAENAB bit, so we clear
74562306a36Sopenharmony_ci			 * the latter first...
74662306a36Sopenharmony_ci			 */
74762306a36Sopenharmony_ci			csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
74862306a36Sopenharmony_ci					| MUSB_TXCSR_AUTOSET
74962306a36Sopenharmony_ci					| MUSB_TXCSR_DMAENAB
75062306a36Sopenharmony_ci					| MUSB_TXCSR_FRCDATATOG
75162306a36Sopenharmony_ci					| MUSB_TXCSR_H_RXSTALL
75262306a36Sopenharmony_ci					| MUSB_TXCSR_H_ERROR
75362306a36Sopenharmony_ci					| MUSB_TXCSR_TXPKTRDY
75462306a36Sopenharmony_ci					);
75562306a36Sopenharmony_ci			csr |= MUSB_TXCSR_MODE;
75662306a36Sopenharmony_ci
75762306a36Sopenharmony_ci			if (!hw_ep->tx_double_buffered)
75862306a36Sopenharmony_ci				csr |= musb->io.set_toggle(qh, is_out, urb);
75962306a36Sopenharmony_ci
76062306a36Sopenharmony_ci			musb_writew(epio, MUSB_TXCSR, csr);
76162306a36Sopenharmony_ci			/* REVISIT may need to clear FLUSHFIFO ... */
76262306a36Sopenharmony_ci			csr &= ~MUSB_TXCSR_DMAMODE;
76362306a36Sopenharmony_ci			musb_writew(epio, MUSB_TXCSR, csr);
76462306a36Sopenharmony_ci			csr = musb_readw(epio, MUSB_TXCSR);
76562306a36Sopenharmony_ci		} else {
76662306a36Sopenharmony_ci			/* endpoint 0: just flush */
76762306a36Sopenharmony_ci			musb_h_ep0_flush_fifo(hw_ep);
76862306a36Sopenharmony_ci		}
76962306a36Sopenharmony_ci
77062306a36Sopenharmony_ci		/* target addr and (for multipoint) hub addr/port */
77162306a36Sopenharmony_ci		if (musb->is_multipoint) {
77262306a36Sopenharmony_ci			musb_write_txfunaddr(musb, epnum, qh->addr_reg);
77362306a36Sopenharmony_ci			musb_write_txhubaddr(musb, epnum, qh->h_addr_reg);
77462306a36Sopenharmony_ci			musb_write_txhubport(musb, epnum, qh->h_port_reg);
77562306a36Sopenharmony_ci/* FIXME if !epnum, do the same for RX ... */
77662306a36Sopenharmony_ci		} else
77762306a36Sopenharmony_ci			musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
77862306a36Sopenharmony_ci
77962306a36Sopenharmony_ci		/* protocol/endpoint/interval/NAKlimit */
78062306a36Sopenharmony_ci		if (epnum) {
78162306a36Sopenharmony_ci			musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
78262306a36Sopenharmony_ci			if (can_bulk_split(musb, qh->type)) {
78362306a36Sopenharmony_ci				qh->hb_mult = hw_ep->max_packet_sz_tx
78462306a36Sopenharmony_ci						/ packet_sz;
78562306a36Sopenharmony_ci				musb_writew(epio, MUSB_TXMAXP, packet_sz
78662306a36Sopenharmony_ci					| ((qh->hb_mult) - 1) << 11);
78762306a36Sopenharmony_ci			} else {
78862306a36Sopenharmony_ci				musb_writew(epio, MUSB_TXMAXP,
78962306a36Sopenharmony_ci						qh->maxpacket |
79062306a36Sopenharmony_ci						((qh->hb_mult - 1) << 11));
79162306a36Sopenharmony_ci			}
79262306a36Sopenharmony_ci			musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
79362306a36Sopenharmony_ci		} else {
79462306a36Sopenharmony_ci			musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
79562306a36Sopenharmony_ci			if (musb->is_multipoint)
79662306a36Sopenharmony_ci				musb_writeb(epio, MUSB_TYPE0,
79762306a36Sopenharmony_ci						qh->type_reg);
79862306a36Sopenharmony_ci		}
79962306a36Sopenharmony_ci
80062306a36Sopenharmony_ci		if (can_bulk_split(musb, qh->type))
80162306a36Sopenharmony_ci			load_count = min((u32) hw_ep->max_packet_sz_tx,
80262306a36Sopenharmony_ci						len);
80362306a36Sopenharmony_ci		else
80462306a36Sopenharmony_ci			load_count = min((u32) packet_sz, len);
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_ci		if (dma_channel && musb_tx_dma_program(dma_controller,
80762306a36Sopenharmony_ci					hw_ep, qh, urb, offset, len))
80862306a36Sopenharmony_ci			load_count = 0;
80962306a36Sopenharmony_ci
81062306a36Sopenharmony_ci		if (load_count) {
81162306a36Sopenharmony_ci			/* PIO to load FIFO */
81262306a36Sopenharmony_ci			qh->segsize = load_count;
81362306a36Sopenharmony_ci			if (!buf) {
81462306a36Sopenharmony_ci				sg_miter_start(&qh->sg_miter, urb->sg, 1,
81562306a36Sopenharmony_ci						SG_MITER_ATOMIC
81662306a36Sopenharmony_ci						| SG_MITER_FROM_SG);
81762306a36Sopenharmony_ci				if (!sg_miter_next(&qh->sg_miter)) {
81862306a36Sopenharmony_ci					dev_err(musb->controller,
81962306a36Sopenharmony_ci							"error: sg"
82062306a36Sopenharmony_ci							"list empty\n");
82162306a36Sopenharmony_ci					sg_miter_stop(&qh->sg_miter);
82262306a36Sopenharmony_ci					goto finish;
82362306a36Sopenharmony_ci				}
82462306a36Sopenharmony_ci				buf = qh->sg_miter.addr + urb->sg->offset +
82562306a36Sopenharmony_ci					urb->actual_length;
82662306a36Sopenharmony_ci				load_count = min_t(u32, load_count,
82762306a36Sopenharmony_ci						qh->sg_miter.length);
82862306a36Sopenharmony_ci				musb_write_fifo(hw_ep, load_count, buf);
82962306a36Sopenharmony_ci				qh->sg_miter.consumed = load_count;
83062306a36Sopenharmony_ci				sg_miter_stop(&qh->sg_miter);
83162306a36Sopenharmony_ci			} else
83262306a36Sopenharmony_ci				musb_write_fifo(hw_ep, load_count, buf);
83362306a36Sopenharmony_ci		}
83462306a36Sopenharmony_cifinish:
83562306a36Sopenharmony_ci		/* re-enable interrupt */
83662306a36Sopenharmony_ci		musb_writew(mbase, MUSB_INTRTXE, int_txe);
83762306a36Sopenharmony_ci
83862306a36Sopenharmony_ci	/* IN/receive */
83962306a36Sopenharmony_ci	} else {
84062306a36Sopenharmony_ci		u16 csr = 0;
84162306a36Sopenharmony_ci
84262306a36Sopenharmony_ci		if (hw_ep->rx_reinit) {
84362306a36Sopenharmony_ci			musb_rx_reinit(musb, qh, epnum);
84462306a36Sopenharmony_ci			csr |= musb->io.set_toggle(qh, is_out, urb);
84562306a36Sopenharmony_ci
84662306a36Sopenharmony_ci			if (qh->type == USB_ENDPOINT_XFER_INT)
84762306a36Sopenharmony_ci				csr |= MUSB_RXCSR_DISNYET;
84862306a36Sopenharmony_ci
84962306a36Sopenharmony_ci		} else {
85062306a36Sopenharmony_ci			csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
85162306a36Sopenharmony_ci
85262306a36Sopenharmony_ci			if (csr & (MUSB_RXCSR_RXPKTRDY
85362306a36Sopenharmony_ci					| MUSB_RXCSR_DMAENAB
85462306a36Sopenharmony_ci					| MUSB_RXCSR_H_REQPKT))
85562306a36Sopenharmony_ci				ERR("broken !rx_reinit, ep%d csr %04x\n",
85662306a36Sopenharmony_ci						hw_ep->epnum, csr);
85762306a36Sopenharmony_ci
85862306a36Sopenharmony_ci			/* scrub any stale state, leaving toggle alone */
85962306a36Sopenharmony_ci			csr &= MUSB_RXCSR_DISNYET;
86062306a36Sopenharmony_ci		}
86162306a36Sopenharmony_ci
86262306a36Sopenharmony_ci		/* kick things off */
86362306a36Sopenharmony_ci
86462306a36Sopenharmony_ci		if ((is_cppi_enabled(musb) || tusb_dma_omap(musb)) && dma_channel) {
86562306a36Sopenharmony_ci			/* Candidate for DMA */
86662306a36Sopenharmony_ci			dma_channel->actual_len = 0L;
86762306a36Sopenharmony_ci			qh->segsize = len;
86862306a36Sopenharmony_ci
86962306a36Sopenharmony_ci			/* AUTOREQ is in a DMA register */
87062306a36Sopenharmony_ci			musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
87162306a36Sopenharmony_ci			csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
87262306a36Sopenharmony_ci
87362306a36Sopenharmony_ci			/*
87462306a36Sopenharmony_ci			 * Unless caller treats short RX transfers as
87562306a36Sopenharmony_ci			 * errors, we dare not queue multiple transfers.
87662306a36Sopenharmony_ci			 */
87762306a36Sopenharmony_ci			dma_ok = dma_controller->channel_program(dma_channel,
87862306a36Sopenharmony_ci					packet_sz, !(urb->transfer_flags &
87962306a36Sopenharmony_ci						     URB_SHORT_NOT_OK),
88062306a36Sopenharmony_ci					urb->transfer_dma + offset,
88162306a36Sopenharmony_ci					qh->segsize);
88262306a36Sopenharmony_ci			if (!dma_ok) {
88362306a36Sopenharmony_ci				dma_controller->channel_release(dma_channel);
88462306a36Sopenharmony_ci				hw_ep->rx_channel = dma_channel = NULL;
88562306a36Sopenharmony_ci			} else
88662306a36Sopenharmony_ci				csr |= MUSB_RXCSR_DMAENAB;
88762306a36Sopenharmony_ci		}
88862306a36Sopenharmony_ci
88962306a36Sopenharmony_ci		csr |= MUSB_RXCSR_H_REQPKT;
89062306a36Sopenharmony_ci		musb_dbg(musb, "RXCSR%d := %04x", epnum, csr);
89162306a36Sopenharmony_ci		musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
89262306a36Sopenharmony_ci		csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
89362306a36Sopenharmony_ci	}
89462306a36Sopenharmony_ci}
89562306a36Sopenharmony_ci
89662306a36Sopenharmony_ci/* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to
89762306a36Sopenharmony_ci * the end; avoids starvation for other endpoints.
89862306a36Sopenharmony_ci */
89962306a36Sopenharmony_cistatic void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
90062306a36Sopenharmony_ci	int is_in)
90162306a36Sopenharmony_ci{
90262306a36Sopenharmony_ci	struct dma_channel	*dma;
90362306a36Sopenharmony_ci	struct urb		*urb;
90462306a36Sopenharmony_ci	void __iomem		*mbase = musb->mregs;
90562306a36Sopenharmony_ci	void __iomem		*epio = ep->regs;
90662306a36Sopenharmony_ci	struct musb_qh		*cur_qh, *next_qh;
90762306a36Sopenharmony_ci	u16			rx_csr, tx_csr;
90862306a36Sopenharmony_ci	u16			toggle;
90962306a36Sopenharmony_ci
91062306a36Sopenharmony_ci	musb_ep_select(mbase, ep->epnum);
91162306a36Sopenharmony_ci	if (is_in) {
91262306a36Sopenharmony_ci		dma = is_dma_capable() ? ep->rx_channel : NULL;
91362306a36Sopenharmony_ci
91462306a36Sopenharmony_ci		/*
91562306a36Sopenharmony_ci		 * Need to stop the transaction by clearing REQPKT first
91662306a36Sopenharmony_ci		 * then the NAK Timeout bit ref MUSBMHDRC USB 2.0 HIGH-SPEED
91762306a36Sopenharmony_ci		 * DUAL-ROLE CONTROLLER Programmer's Guide, section 9.2.2
91862306a36Sopenharmony_ci		 */
91962306a36Sopenharmony_ci		rx_csr = musb_readw(epio, MUSB_RXCSR);
92062306a36Sopenharmony_ci		rx_csr |= MUSB_RXCSR_H_WZC_BITS;
92162306a36Sopenharmony_ci		rx_csr &= ~MUSB_RXCSR_H_REQPKT;
92262306a36Sopenharmony_ci		musb_writew(epio, MUSB_RXCSR, rx_csr);
92362306a36Sopenharmony_ci		rx_csr &= ~MUSB_RXCSR_DATAERROR;
92462306a36Sopenharmony_ci		musb_writew(epio, MUSB_RXCSR, rx_csr);
92562306a36Sopenharmony_ci
92662306a36Sopenharmony_ci		cur_qh = first_qh(&musb->in_bulk);
92762306a36Sopenharmony_ci	} else {
92862306a36Sopenharmony_ci		dma = is_dma_capable() ? ep->tx_channel : NULL;
92962306a36Sopenharmony_ci
93062306a36Sopenharmony_ci		/* clear nak timeout bit */
93162306a36Sopenharmony_ci		tx_csr = musb_readw(epio, MUSB_TXCSR);
93262306a36Sopenharmony_ci		tx_csr |= MUSB_TXCSR_H_WZC_BITS;
93362306a36Sopenharmony_ci		tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT;
93462306a36Sopenharmony_ci		musb_writew(epio, MUSB_TXCSR, tx_csr);
93562306a36Sopenharmony_ci
93662306a36Sopenharmony_ci		cur_qh = first_qh(&musb->out_bulk);
93762306a36Sopenharmony_ci	}
93862306a36Sopenharmony_ci	if (cur_qh) {
93962306a36Sopenharmony_ci		urb = next_urb(cur_qh);
94062306a36Sopenharmony_ci		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
94162306a36Sopenharmony_ci			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
94262306a36Sopenharmony_ci			musb->dma_controller->channel_abort(dma);
94362306a36Sopenharmony_ci			urb->actual_length += dma->actual_len;
94462306a36Sopenharmony_ci			dma->actual_len = 0L;
94562306a36Sopenharmony_ci		}
94662306a36Sopenharmony_ci		toggle = musb->io.get_toggle(cur_qh, !is_in);
94762306a36Sopenharmony_ci		usb_settoggle(urb->dev, cur_qh->epnum, !is_in, toggle ? 1 : 0);
94862306a36Sopenharmony_ci
94962306a36Sopenharmony_ci		if (is_in) {
95062306a36Sopenharmony_ci			/* move cur_qh to end of queue */
95162306a36Sopenharmony_ci			list_move_tail(&cur_qh->ring, &musb->in_bulk);
95262306a36Sopenharmony_ci
95362306a36Sopenharmony_ci			/* get the next qh from musb->in_bulk */
95462306a36Sopenharmony_ci			next_qh = first_qh(&musb->in_bulk);
95562306a36Sopenharmony_ci
95662306a36Sopenharmony_ci			/* set rx_reinit and schedule the next qh */
95762306a36Sopenharmony_ci			ep->rx_reinit = 1;
95862306a36Sopenharmony_ci		} else {
95962306a36Sopenharmony_ci			/* move cur_qh to end of queue */
96062306a36Sopenharmony_ci			list_move_tail(&cur_qh->ring, &musb->out_bulk);
96162306a36Sopenharmony_ci
96262306a36Sopenharmony_ci			/* get the next qh from musb->out_bulk */
96362306a36Sopenharmony_ci			next_qh = first_qh(&musb->out_bulk);
96462306a36Sopenharmony_ci
96562306a36Sopenharmony_ci			/* set tx_reinit and schedule the next qh */
96662306a36Sopenharmony_ci			ep->tx_reinit = 1;
96762306a36Sopenharmony_ci		}
96862306a36Sopenharmony_ci
96962306a36Sopenharmony_ci		if (next_qh)
97062306a36Sopenharmony_ci			musb_start_urb(musb, is_in, next_qh);
97162306a36Sopenharmony_ci	}
97262306a36Sopenharmony_ci}
97362306a36Sopenharmony_ci
97462306a36Sopenharmony_ci/*
97562306a36Sopenharmony_ci * Service the default endpoint (ep0) as host.
97662306a36Sopenharmony_ci * Return true until it's time to start the status stage.
97762306a36Sopenharmony_ci */
97862306a36Sopenharmony_cistatic bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
97962306a36Sopenharmony_ci{
98062306a36Sopenharmony_ci	bool			 more = false;
98162306a36Sopenharmony_ci	u8			*fifo_dest = NULL;
98262306a36Sopenharmony_ci	u16			fifo_count = 0;
98362306a36Sopenharmony_ci	struct musb_hw_ep	*hw_ep = musb->control_ep;
98462306a36Sopenharmony_ci	struct musb_qh		*qh = hw_ep->in_qh;
98562306a36Sopenharmony_ci	struct usb_ctrlrequest	*request;
98662306a36Sopenharmony_ci
98762306a36Sopenharmony_ci	switch (musb->ep0_stage) {
98862306a36Sopenharmony_ci	case MUSB_EP0_IN:
98962306a36Sopenharmony_ci		fifo_dest = urb->transfer_buffer + urb->actual_length;
99062306a36Sopenharmony_ci		fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
99162306a36Sopenharmony_ci				   urb->actual_length);
99262306a36Sopenharmony_ci		if (fifo_count < len)
99362306a36Sopenharmony_ci			urb->status = -EOVERFLOW;
99462306a36Sopenharmony_ci
99562306a36Sopenharmony_ci		musb_read_fifo(hw_ep, fifo_count, fifo_dest);
99662306a36Sopenharmony_ci
99762306a36Sopenharmony_ci		urb->actual_length += fifo_count;
99862306a36Sopenharmony_ci		if (len < qh->maxpacket) {
99962306a36Sopenharmony_ci			/* always terminate on short read; it's
100062306a36Sopenharmony_ci			 * rarely reported as an error.
100162306a36Sopenharmony_ci			 */
100262306a36Sopenharmony_ci		} else if (urb->actual_length <
100362306a36Sopenharmony_ci				urb->transfer_buffer_length)
100462306a36Sopenharmony_ci			more = true;
100562306a36Sopenharmony_ci		break;
100662306a36Sopenharmony_ci	case MUSB_EP0_START:
100762306a36Sopenharmony_ci		request = (struct usb_ctrlrequest *) urb->setup_packet;
100862306a36Sopenharmony_ci
100962306a36Sopenharmony_ci		if (!request->wLength) {
101062306a36Sopenharmony_ci			musb_dbg(musb, "start no-DATA");
101162306a36Sopenharmony_ci			break;
101262306a36Sopenharmony_ci		} else if (request->bRequestType & USB_DIR_IN) {
101362306a36Sopenharmony_ci			musb_dbg(musb, "start IN-DATA");
101462306a36Sopenharmony_ci			musb->ep0_stage = MUSB_EP0_IN;
101562306a36Sopenharmony_ci			more = true;
101662306a36Sopenharmony_ci			break;
101762306a36Sopenharmony_ci		} else {
101862306a36Sopenharmony_ci			musb_dbg(musb, "start OUT-DATA");
101962306a36Sopenharmony_ci			musb->ep0_stage = MUSB_EP0_OUT;
102062306a36Sopenharmony_ci			more = true;
102162306a36Sopenharmony_ci		}
102262306a36Sopenharmony_ci		fallthrough;
102362306a36Sopenharmony_ci	case MUSB_EP0_OUT:
102462306a36Sopenharmony_ci		fifo_count = min_t(size_t, qh->maxpacket,
102562306a36Sopenharmony_ci				   urb->transfer_buffer_length -
102662306a36Sopenharmony_ci				   urb->actual_length);
102762306a36Sopenharmony_ci		if (fifo_count) {
102862306a36Sopenharmony_ci			fifo_dest = (u8 *) (urb->transfer_buffer
102962306a36Sopenharmony_ci					+ urb->actual_length);
103062306a36Sopenharmony_ci			musb_dbg(musb, "Sending %d byte%s to ep0 fifo %p",
103162306a36Sopenharmony_ci					fifo_count,
103262306a36Sopenharmony_ci					(fifo_count == 1) ? "" : "s",
103362306a36Sopenharmony_ci					fifo_dest);
103462306a36Sopenharmony_ci			musb_write_fifo(hw_ep, fifo_count, fifo_dest);
103562306a36Sopenharmony_ci
103662306a36Sopenharmony_ci			urb->actual_length += fifo_count;
103762306a36Sopenharmony_ci			more = true;
103862306a36Sopenharmony_ci		}
103962306a36Sopenharmony_ci		break;
104062306a36Sopenharmony_ci	default:
104162306a36Sopenharmony_ci		ERR("bogus ep0 stage %d\n", musb->ep0_stage);
104262306a36Sopenharmony_ci		break;
104362306a36Sopenharmony_ci	}
104462306a36Sopenharmony_ci
104562306a36Sopenharmony_ci	return more;
104662306a36Sopenharmony_ci}
104762306a36Sopenharmony_ci
104862306a36Sopenharmony_ci/*
104962306a36Sopenharmony_ci * Handle default endpoint interrupt as host. Only called in IRQ time
105062306a36Sopenharmony_ci * from musb_interrupt().
105162306a36Sopenharmony_ci *
105262306a36Sopenharmony_ci * called with controller irqlocked
105362306a36Sopenharmony_ci */
105462306a36Sopenharmony_ciirqreturn_t musb_h_ep0_irq(struct musb *musb)
105562306a36Sopenharmony_ci{
105662306a36Sopenharmony_ci	struct urb		*urb;
105762306a36Sopenharmony_ci	u16			csr, len;
105862306a36Sopenharmony_ci	int			status = 0;
105962306a36Sopenharmony_ci	void __iomem		*mbase = musb->mregs;
106062306a36Sopenharmony_ci	struct musb_hw_ep	*hw_ep = musb->control_ep;
106162306a36Sopenharmony_ci	void __iomem		*epio = hw_ep->regs;
106262306a36Sopenharmony_ci	struct musb_qh		*qh = hw_ep->in_qh;
106362306a36Sopenharmony_ci	bool			complete = false;
106462306a36Sopenharmony_ci	irqreturn_t		retval = IRQ_NONE;
106562306a36Sopenharmony_ci
106662306a36Sopenharmony_ci	/* ep0 only has one queue, "in" */
106762306a36Sopenharmony_ci	urb = next_urb(qh);
106862306a36Sopenharmony_ci
106962306a36Sopenharmony_ci	musb_ep_select(mbase, 0);
107062306a36Sopenharmony_ci	csr = musb_readw(epio, MUSB_CSR0);
107162306a36Sopenharmony_ci	len = (csr & MUSB_CSR0_RXPKTRDY)
107262306a36Sopenharmony_ci			? musb_readb(epio, MUSB_COUNT0)
107362306a36Sopenharmony_ci			: 0;
107462306a36Sopenharmony_ci
107562306a36Sopenharmony_ci	musb_dbg(musb, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d",
107662306a36Sopenharmony_ci		csr, qh, len, urb, musb->ep0_stage);
107762306a36Sopenharmony_ci
107862306a36Sopenharmony_ci	/* if we just did status stage, we are done */
107962306a36Sopenharmony_ci	if (MUSB_EP0_STATUS == musb->ep0_stage) {
108062306a36Sopenharmony_ci		retval = IRQ_HANDLED;
108162306a36Sopenharmony_ci		complete = true;
108262306a36Sopenharmony_ci	}
108362306a36Sopenharmony_ci
108462306a36Sopenharmony_ci	/* prepare status */
108562306a36Sopenharmony_ci	if (csr & MUSB_CSR0_H_RXSTALL) {
108662306a36Sopenharmony_ci		musb_dbg(musb, "STALLING ENDPOINT");
108762306a36Sopenharmony_ci		status = -EPIPE;
108862306a36Sopenharmony_ci
108962306a36Sopenharmony_ci	} else if (csr & MUSB_CSR0_H_ERROR) {
109062306a36Sopenharmony_ci		musb_dbg(musb, "no response, csr0 %04x", csr);
109162306a36Sopenharmony_ci		status = -EPROTO;
109262306a36Sopenharmony_ci
109362306a36Sopenharmony_ci	} else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
109462306a36Sopenharmony_ci		musb_dbg(musb, "control NAK timeout");
109562306a36Sopenharmony_ci
109662306a36Sopenharmony_ci		/* NOTE:  this code path would be a good place to PAUSE a
109762306a36Sopenharmony_ci		 * control transfer, if another one is queued, so that
109862306a36Sopenharmony_ci		 * ep0 is more likely to stay busy.  That's already done
109962306a36Sopenharmony_ci		 * for bulk RX transfers.
110062306a36Sopenharmony_ci		 *
110162306a36Sopenharmony_ci		 * if (qh->ring.next != &musb->control), then
110262306a36Sopenharmony_ci		 * we have a candidate... NAKing is *NOT* an error
110362306a36Sopenharmony_ci		 */
110462306a36Sopenharmony_ci		musb_writew(epio, MUSB_CSR0, 0);
110562306a36Sopenharmony_ci		retval = IRQ_HANDLED;
110662306a36Sopenharmony_ci	}
110762306a36Sopenharmony_ci
110862306a36Sopenharmony_ci	if (status) {
110962306a36Sopenharmony_ci		musb_dbg(musb, "aborting");
111062306a36Sopenharmony_ci		retval = IRQ_HANDLED;
111162306a36Sopenharmony_ci		if (urb)
111262306a36Sopenharmony_ci			urb->status = status;
111362306a36Sopenharmony_ci		complete = true;
111462306a36Sopenharmony_ci
111562306a36Sopenharmony_ci		/* use the proper sequence to abort the transfer */
111662306a36Sopenharmony_ci		if (csr & MUSB_CSR0_H_REQPKT) {
111762306a36Sopenharmony_ci			csr &= ~MUSB_CSR0_H_REQPKT;
111862306a36Sopenharmony_ci			musb_writew(epio, MUSB_CSR0, csr);
111962306a36Sopenharmony_ci			csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
112062306a36Sopenharmony_ci			musb_writew(epio, MUSB_CSR0, csr);
112162306a36Sopenharmony_ci		} else {
112262306a36Sopenharmony_ci			musb_h_ep0_flush_fifo(hw_ep);
112362306a36Sopenharmony_ci		}
112462306a36Sopenharmony_ci
112562306a36Sopenharmony_ci		musb_writeb(epio, MUSB_NAKLIMIT0, 0);
112662306a36Sopenharmony_ci
112762306a36Sopenharmony_ci		/* clear it */
112862306a36Sopenharmony_ci		musb_writew(epio, MUSB_CSR0, 0);
112962306a36Sopenharmony_ci	}
113062306a36Sopenharmony_ci
113162306a36Sopenharmony_ci	if (unlikely(!urb)) {
113262306a36Sopenharmony_ci		/* stop endpoint since we have no place for its data, this
113362306a36Sopenharmony_ci		 * SHOULD NEVER HAPPEN! */
113462306a36Sopenharmony_ci		ERR("no URB for end 0\n");
113562306a36Sopenharmony_ci
113662306a36Sopenharmony_ci		musb_h_ep0_flush_fifo(hw_ep);
113762306a36Sopenharmony_ci		goto done;
113862306a36Sopenharmony_ci	}
113962306a36Sopenharmony_ci
114062306a36Sopenharmony_ci	if (!complete) {
114162306a36Sopenharmony_ci		/* call common logic and prepare response */
114262306a36Sopenharmony_ci		if (musb_h_ep0_continue(musb, len, urb)) {
114362306a36Sopenharmony_ci			/* more packets required */
114462306a36Sopenharmony_ci			csr = (MUSB_EP0_IN == musb->ep0_stage)
114562306a36Sopenharmony_ci				?  MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
114662306a36Sopenharmony_ci		} else {
114762306a36Sopenharmony_ci			/* data transfer complete; perform status phase */
114862306a36Sopenharmony_ci			if (usb_pipeout(urb->pipe)
114962306a36Sopenharmony_ci					|| !urb->transfer_buffer_length)
115062306a36Sopenharmony_ci				csr = MUSB_CSR0_H_STATUSPKT
115162306a36Sopenharmony_ci					| MUSB_CSR0_H_REQPKT;
115262306a36Sopenharmony_ci			else
115362306a36Sopenharmony_ci				csr = MUSB_CSR0_H_STATUSPKT
115462306a36Sopenharmony_ci					| MUSB_CSR0_TXPKTRDY;
115562306a36Sopenharmony_ci
115662306a36Sopenharmony_ci			/* disable ping token in status phase */
115762306a36Sopenharmony_ci			csr |= MUSB_CSR0_H_DIS_PING;
115862306a36Sopenharmony_ci
115962306a36Sopenharmony_ci			/* flag status stage */
116062306a36Sopenharmony_ci			musb->ep0_stage = MUSB_EP0_STATUS;
116162306a36Sopenharmony_ci
116262306a36Sopenharmony_ci			musb_dbg(musb, "ep0 STATUS, csr %04x", csr);
116362306a36Sopenharmony_ci
116462306a36Sopenharmony_ci		}
116562306a36Sopenharmony_ci		musb_writew(epio, MUSB_CSR0, csr);
116662306a36Sopenharmony_ci		retval = IRQ_HANDLED;
116762306a36Sopenharmony_ci	} else
116862306a36Sopenharmony_ci		musb->ep0_stage = MUSB_EP0_IDLE;
116962306a36Sopenharmony_ci
117062306a36Sopenharmony_ci	/* call completion handler if done */
117162306a36Sopenharmony_ci	if (complete)
117262306a36Sopenharmony_ci		musb_advance_schedule(musb, urb, hw_ep, 1);
117362306a36Sopenharmony_cidone:
117462306a36Sopenharmony_ci	return retval;
117562306a36Sopenharmony_ci}
117662306a36Sopenharmony_ci
117762306a36Sopenharmony_ci
117862306a36Sopenharmony_ci#ifdef CONFIG_USB_INVENTRA_DMA
117962306a36Sopenharmony_ci
118062306a36Sopenharmony_ci/* Host side TX (OUT) using Mentor DMA works as follows:
118162306a36Sopenharmony_ci	submit_urb ->
118262306a36Sopenharmony_ci		- if queue was empty, Program Endpoint
118362306a36Sopenharmony_ci		- ... which starts DMA to fifo in mode 1 or 0
118462306a36Sopenharmony_ci
118562306a36Sopenharmony_ci	DMA Isr (transfer complete) -> TxAvail()
118662306a36Sopenharmony_ci		- Stop DMA (~DmaEnab)	(<--- Alert ... currently happens
118762306a36Sopenharmony_ci					only in musb_cleanup_urb)
118862306a36Sopenharmony_ci		- TxPktRdy has to be set in mode 0 or for
118962306a36Sopenharmony_ci			short packets in mode 1.
119062306a36Sopenharmony_ci*/
119162306a36Sopenharmony_ci
119262306a36Sopenharmony_ci#endif
119362306a36Sopenharmony_ci
119462306a36Sopenharmony_ci/* Service a Tx-Available or dma completion irq for the endpoint */
119562306a36Sopenharmony_civoid musb_host_tx(struct musb *musb, u8 epnum)
119662306a36Sopenharmony_ci{
119762306a36Sopenharmony_ci	int			pipe;
119862306a36Sopenharmony_ci	bool			done = false;
119962306a36Sopenharmony_ci	u16			tx_csr;
120062306a36Sopenharmony_ci	size_t			length = 0;
120162306a36Sopenharmony_ci	size_t			offset = 0;
120262306a36Sopenharmony_ci	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
120362306a36Sopenharmony_ci	void __iomem		*epio = hw_ep->regs;
120462306a36Sopenharmony_ci	struct musb_qh		*qh = hw_ep->out_qh;
120562306a36Sopenharmony_ci	struct urb		*urb = next_urb(qh);
120662306a36Sopenharmony_ci	u32			status = 0;
120762306a36Sopenharmony_ci	void __iomem		*mbase = musb->mregs;
120862306a36Sopenharmony_ci	struct dma_channel	*dma;
120962306a36Sopenharmony_ci	bool			transfer_pending = false;
121062306a36Sopenharmony_ci
121162306a36Sopenharmony_ci	musb_ep_select(mbase, epnum);
121262306a36Sopenharmony_ci	tx_csr = musb_readw(epio, MUSB_TXCSR);
121362306a36Sopenharmony_ci
121462306a36Sopenharmony_ci	/* with CPPI, DMA sometimes triggers "extra" irqs */
121562306a36Sopenharmony_ci	if (!urb) {
121662306a36Sopenharmony_ci		musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
121762306a36Sopenharmony_ci		return;
121862306a36Sopenharmony_ci	}
121962306a36Sopenharmony_ci
122062306a36Sopenharmony_ci	pipe = urb->pipe;
122162306a36Sopenharmony_ci	dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
122262306a36Sopenharmony_ci	trace_musb_urb_tx(musb, urb);
122362306a36Sopenharmony_ci	musb_dbg(musb, "OUT/TX%d end, csr %04x%s", epnum, tx_csr,
122462306a36Sopenharmony_ci			dma ? ", dma" : "");
122562306a36Sopenharmony_ci
122662306a36Sopenharmony_ci	/* check for errors */
122762306a36Sopenharmony_ci	if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
122862306a36Sopenharmony_ci		/* dma was disabled, fifo flushed */
122962306a36Sopenharmony_ci		musb_dbg(musb, "TX end %d stall", epnum);
123062306a36Sopenharmony_ci
123162306a36Sopenharmony_ci		/* stall; record URB status */
123262306a36Sopenharmony_ci		status = -EPIPE;
123362306a36Sopenharmony_ci
123462306a36Sopenharmony_ci	} else if (tx_csr & MUSB_TXCSR_H_ERROR) {
123562306a36Sopenharmony_ci		/* (NON-ISO) dma was disabled, fifo flushed */
123662306a36Sopenharmony_ci		musb_dbg(musb, "TX 3strikes on ep=%d", epnum);
123762306a36Sopenharmony_ci
123862306a36Sopenharmony_ci		status = -ETIMEDOUT;
123962306a36Sopenharmony_ci
124062306a36Sopenharmony_ci	} else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
124162306a36Sopenharmony_ci		if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
124262306a36Sopenharmony_ci				&& !list_is_singular(&musb->out_bulk)) {
124362306a36Sopenharmony_ci			musb_dbg(musb, "NAK timeout on TX%d ep", epnum);
124462306a36Sopenharmony_ci			musb_bulk_nak_timeout(musb, hw_ep, 0);
124562306a36Sopenharmony_ci		} else {
124662306a36Sopenharmony_ci			musb_dbg(musb, "TX ep%d device not responding", epnum);
124762306a36Sopenharmony_ci			/* NOTE:  this code path would be a good place to PAUSE a
124862306a36Sopenharmony_ci			 * transfer, if there's some other (nonperiodic) tx urb
124962306a36Sopenharmony_ci			 * that could use this fifo.  (dma complicates it...)
125062306a36Sopenharmony_ci			 * That's already done for bulk RX transfers.
125162306a36Sopenharmony_ci			 *
125262306a36Sopenharmony_ci			 * if (bulk && qh->ring.next != &musb->out_bulk), then
125362306a36Sopenharmony_ci			 * we have a candidate... NAKing is *NOT* an error
125462306a36Sopenharmony_ci			 */
125562306a36Sopenharmony_ci			musb_ep_select(mbase, epnum);
125662306a36Sopenharmony_ci			musb_writew(epio, MUSB_TXCSR,
125762306a36Sopenharmony_ci					MUSB_TXCSR_H_WZC_BITS
125862306a36Sopenharmony_ci					| MUSB_TXCSR_TXPKTRDY);
125962306a36Sopenharmony_ci		}
126062306a36Sopenharmony_ci		return;
126162306a36Sopenharmony_ci	}
126262306a36Sopenharmony_ci
126362306a36Sopenharmony_cidone:
126462306a36Sopenharmony_ci	if (status) {
126562306a36Sopenharmony_ci		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
126662306a36Sopenharmony_ci			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
126762306a36Sopenharmony_ci			musb->dma_controller->channel_abort(dma);
126862306a36Sopenharmony_ci		}
126962306a36Sopenharmony_ci
127062306a36Sopenharmony_ci		/* do the proper sequence to abort the transfer in the
127162306a36Sopenharmony_ci		 * usb core; the dma engine should already be stopped.
127262306a36Sopenharmony_ci		 */
127362306a36Sopenharmony_ci		musb_h_tx_flush_fifo(hw_ep);
127462306a36Sopenharmony_ci		tx_csr &= ~(MUSB_TXCSR_AUTOSET
127562306a36Sopenharmony_ci				| MUSB_TXCSR_DMAENAB
127662306a36Sopenharmony_ci				| MUSB_TXCSR_H_ERROR
127762306a36Sopenharmony_ci				| MUSB_TXCSR_H_RXSTALL
127862306a36Sopenharmony_ci				| MUSB_TXCSR_H_NAKTIMEOUT
127962306a36Sopenharmony_ci				);
128062306a36Sopenharmony_ci
128162306a36Sopenharmony_ci		musb_ep_select(mbase, epnum);
128262306a36Sopenharmony_ci		musb_writew(epio, MUSB_TXCSR, tx_csr);
128362306a36Sopenharmony_ci		/* REVISIT may need to clear FLUSHFIFO ... */
128462306a36Sopenharmony_ci		musb_writew(epio, MUSB_TXCSR, tx_csr);
128562306a36Sopenharmony_ci		musb_writeb(epio, MUSB_TXINTERVAL, 0);
128662306a36Sopenharmony_ci
128762306a36Sopenharmony_ci		done = true;
128862306a36Sopenharmony_ci	}
128962306a36Sopenharmony_ci
129062306a36Sopenharmony_ci	/* second cppi case */
129162306a36Sopenharmony_ci	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
129262306a36Sopenharmony_ci		musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
129362306a36Sopenharmony_ci		return;
129462306a36Sopenharmony_ci	}
129562306a36Sopenharmony_ci
129662306a36Sopenharmony_ci	if (is_dma_capable() && dma && !status) {
129762306a36Sopenharmony_ci		/*
129862306a36Sopenharmony_ci		 * DMA has completed.  But if we're using DMA mode 1 (multi
129962306a36Sopenharmony_ci		 * packet DMA), we need a terminal TXPKTRDY interrupt before
130062306a36Sopenharmony_ci		 * we can consider this transfer completed, lest we trash
130162306a36Sopenharmony_ci		 * its last packet when writing the next URB's data.  So we
130262306a36Sopenharmony_ci		 * switch back to mode 0 to get that interrupt; we'll come
130362306a36Sopenharmony_ci		 * back here once it happens.
130462306a36Sopenharmony_ci		 */
130562306a36Sopenharmony_ci		if (tx_csr & MUSB_TXCSR_DMAMODE) {
130662306a36Sopenharmony_ci			/*
130762306a36Sopenharmony_ci			 * We shouldn't clear DMAMODE with DMAENAB set; so
130862306a36Sopenharmony_ci			 * clear them in a safe order.  That should be OK
130962306a36Sopenharmony_ci			 * once TXPKTRDY has been set (and I've never seen
131062306a36Sopenharmony_ci			 * it being 0 at this moment -- DMA interrupt latency
131162306a36Sopenharmony_ci			 * is significant) but if it hasn't been then we have
131262306a36Sopenharmony_ci			 * no choice but to stop being polite and ignore the
131362306a36Sopenharmony_ci			 * programmer's guide... :-)
131462306a36Sopenharmony_ci			 *
131562306a36Sopenharmony_ci			 * Note that we must write TXCSR with TXPKTRDY cleared
131662306a36Sopenharmony_ci			 * in order not to re-trigger the packet send (this bit
131762306a36Sopenharmony_ci			 * can't be cleared by CPU), and there's another caveat:
131862306a36Sopenharmony_ci			 * TXPKTRDY may be set shortly and then cleared in the
131962306a36Sopenharmony_ci			 * double-buffered FIFO mode, so we do an extra TXCSR
132062306a36Sopenharmony_ci			 * read for debouncing...
132162306a36Sopenharmony_ci			 */
132262306a36Sopenharmony_ci			tx_csr &= musb_readw(epio, MUSB_TXCSR);
132362306a36Sopenharmony_ci			if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
132462306a36Sopenharmony_ci				tx_csr &= ~(MUSB_TXCSR_DMAENAB |
132562306a36Sopenharmony_ci					    MUSB_TXCSR_TXPKTRDY);
132662306a36Sopenharmony_ci				musb_writew(epio, MUSB_TXCSR,
132762306a36Sopenharmony_ci					    tx_csr | MUSB_TXCSR_H_WZC_BITS);
132862306a36Sopenharmony_ci			}
132962306a36Sopenharmony_ci			tx_csr &= ~(MUSB_TXCSR_DMAMODE |
133062306a36Sopenharmony_ci				    MUSB_TXCSR_TXPKTRDY);
133162306a36Sopenharmony_ci			musb_writew(epio, MUSB_TXCSR,
133262306a36Sopenharmony_ci				    tx_csr | MUSB_TXCSR_H_WZC_BITS);
133362306a36Sopenharmony_ci
133462306a36Sopenharmony_ci			/*
133562306a36Sopenharmony_ci			 * There is no guarantee that we'll get an interrupt
133662306a36Sopenharmony_ci			 * after clearing DMAMODE as we might have done this
133762306a36Sopenharmony_ci			 * too late (after TXPKTRDY was cleared by controller).
133862306a36Sopenharmony_ci			 * Re-read TXCSR as we have spoiled its previous value.
133962306a36Sopenharmony_ci			 */
134062306a36Sopenharmony_ci			tx_csr = musb_readw(epio, MUSB_TXCSR);
134162306a36Sopenharmony_ci		}
134262306a36Sopenharmony_ci
134362306a36Sopenharmony_ci		/*
134462306a36Sopenharmony_ci		 * We may get here from a DMA completion or TXPKTRDY interrupt.
134562306a36Sopenharmony_ci		 * In any case, we must check the FIFO status here and bail out
134662306a36Sopenharmony_ci		 * only if the FIFO still has data -- that should prevent the
134762306a36Sopenharmony_ci		 * "missed" TXPKTRDY interrupts and deal with double-buffered
134862306a36Sopenharmony_ci		 * FIFO mode too...
134962306a36Sopenharmony_ci		 */
135062306a36Sopenharmony_ci		if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
135162306a36Sopenharmony_ci			musb_dbg(musb,
135262306a36Sopenharmony_ci				"DMA complete but FIFO not empty, CSR %04x",
135362306a36Sopenharmony_ci				tx_csr);
135462306a36Sopenharmony_ci			return;
135562306a36Sopenharmony_ci		}
135662306a36Sopenharmony_ci	}
135762306a36Sopenharmony_ci
135862306a36Sopenharmony_ci	if (!status || dma || usb_pipeisoc(pipe)) {
135962306a36Sopenharmony_ci		if (dma)
136062306a36Sopenharmony_ci			length = dma->actual_len;
136162306a36Sopenharmony_ci		else
136262306a36Sopenharmony_ci			length = qh->segsize;
136362306a36Sopenharmony_ci		qh->offset += length;
136462306a36Sopenharmony_ci
136562306a36Sopenharmony_ci		if (usb_pipeisoc(pipe)) {
136662306a36Sopenharmony_ci			struct usb_iso_packet_descriptor	*d;
136762306a36Sopenharmony_ci
136862306a36Sopenharmony_ci			d = urb->iso_frame_desc + qh->iso_idx;
136962306a36Sopenharmony_ci			d->actual_length = length;
137062306a36Sopenharmony_ci			d->status = status;
137162306a36Sopenharmony_ci			if (++qh->iso_idx >= urb->number_of_packets) {
137262306a36Sopenharmony_ci				done = true;
137362306a36Sopenharmony_ci			} else {
137462306a36Sopenharmony_ci				d++;
137562306a36Sopenharmony_ci				offset = d->offset;
137662306a36Sopenharmony_ci				length = d->length;
137762306a36Sopenharmony_ci			}
137862306a36Sopenharmony_ci		} else if (dma && urb->transfer_buffer_length == qh->offset) {
137962306a36Sopenharmony_ci			done = true;
138062306a36Sopenharmony_ci		} else {
138162306a36Sopenharmony_ci			/* see if we need to send more data, or ZLP */
138262306a36Sopenharmony_ci			if (qh->segsize < qh->maxpacket)
138362306a36Sopenharmony_ci				done = true;
138462306a36Sopenharmony_ci			else if (qh->offset == urb->transfer_buffer_length
138562306a36Sopenharmony_ci					&& !(urb->transfer_flags
138662306a36Sopenharmony_ci						& URB_ZERO_PACKET))
138762306a36Sopenharmony_ci				done = true;
138862306a36Sopenharmony_ci			if (!done) {
138962306a36Sopenharmony_ci				offset = qh->offset;
139062306a36Sopenharmony_ci				length = urb->transfer_buffer_length - offset;
139162306a36Sopenharmony_ci				transfer_pending = true;
139262306a36Sopenharmony_ci			}
139362306a36Sopenharmony_ci		}
139462306a36Sopenharmony_ci	}
139562306a36Sopenharmony_ci
139662306a36Sopenharmony_ci	/* urb->status != -EINPROGRESS means request has been faulted,
139762306a36Sopenharmony_ci	 * so we must abort this transfer after cleanup
139862306a36Sopenharmony_ci	 */
139962306a36Sopenharmony_ci	if (urb->status != -EINPROGRESS) {
140062306a36Sopenharmony_ci		done = true;
140162306a36Sopenharmony_ci		if (status == 0)
140262306a36Sopenharmony_ci			status = urb->status;
140362306a36Sopenharmony_ci	}
140462306a36Sopenharmony_ci
140562306a36Sopenharmony_ci	if (done) {
140662306a36Sopenharmony_ci		/* set status */
140762306a36Sopenharmony_ci		urb->status = status;
140862306a36Sopenharmony_ci		urb->actual_length = qh->offset;
140962306a36Sopenharmony_ci		musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
141062306a36Sopenharmony_ci		return;
141162306a36Sopenharmony_ci	} else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
141262306a36Sopenharmony_ci		if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
141362306a36Sopenharmony_ci				offset, length)) {
141462306a36Sopenharmony_ci			if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
141562306a36Sopenharmony_ci				musb_h_tx_dma_start(hw_ep);
141662306a36Sopenharmony_ci			return;
141762306a36Sopenharmony_ci		}
141862306a36Sopenharmony_ci	} else	if (tx_csr & MUSB_TXCSR_DMAENAB) {
141962306a36Sopenharmony_ci		musb_dbg(musb, "not complete, but DMA enabled?");
142062306a36Sopenharmony_ci		return;
142162306a36Sopenharmony_ci	}
142262306a36Sopenharmony_ci
142362306a36Sopenharmony_ci	/*
142462306a36Sopenharmony_ci	 * PIO: start next packet in this URB.
142562306a36Sopenharmony_ci	 *
142662306a36Sopenharmony_ci	 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
142762306a36Sopenharmony_ci	 * (and presumably, FIFO is not half-full) we should write *two*
142862306a36Sopenharmony_ci	 * packets before updating TXCSR; other docs disagree...
142962306a36Sopenharmony_ci	 */
143062306a36Sopenharmony_ci	if (length > qh->maxpacket)
143162306a36Sopenharmony_ci		length = qh->maxpacket;
143262306a36Sopenharmony_ci	/* Unmap the buffer so that CPU can use it */
143362306a36Sopenharmony_ci	usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
143462306a36Sopenharmony_ci
143562306a36Sopenharmony_ci	/*
143662306a36Sopenharmony_ci	 * We need to map sg if the transfer_buffer is
143762306a36Sopenharmony_ci	 * NULL.
143862306a36Sopenharmony_ci	 */
143962306a36Sopenharmony_ci	if (!urb->transfer_buffer) {
144062306a36Sopenharmony_ci		/* sg_miter_start is already done in musb_ep_program */
144162306a36Sopenharmony_ci		if (!sg_miter_next(&qh->sg_miter)) {
144262306a36Sopenharmony_ci			dev_err(musb->controller, "error: sg list empty\n");
144362306a36Sopenharmony_ci			sg_miter_stop(&qh->sg_miter);
144462306a36Sopenharmony_ci			status = -EINVAL;
144562306a36Sopenharmony_ci			goto done;
144662306a36Sopenharmony_ci		}
144762306a36Sopenharmony_ci		length = min_t(u32, length, qh->sg_miter.length);
144862306a36Sopenharmony_ci		musb_write_fifo(hw_ep, length, qh->sg_miter.addr);
144962306a36Sopenharmony_ci		qh->sg_miter.consumed = length;
145062306a36Sopenharmony_ci		sg_miter_stop(&qh->sg_miter);
145162306a36Sopenharmony_ci	} else {
145262306a36Sopenharmony_ci		musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
145362306a36Sopenharmony_ci	}
145462306a36Sopenharmony_ci
145562306a36Sopenharmony_ci	qh->segsize = length;
145662306a36Sopenharmony_ci
145762306a36Sopenharmony_ci	musb_ep_select(mbase, epnum);
145862306a36Sopenharmony_ci	musb_writew(epio, MUSB_TXCSR,
145962306a36Sopenharmony_ci			MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
146062306a36Sopenharmony_ci}
146162306a36Sopenharmony_ci
146262306a36Sopenharmony_ci#ifdef CONFIG_USB_TI_CPPI41_DMA
146362306a36Sopenharmony_ci/* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */
146462306a36Sopenharmony_cistatic int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
146562306a36Sopenharmony_ci				  struct musb_hw_ep *hw_ep,
146662306a36Sopenharmony_ci				  struct musb_qh *qh,
146762306a36Sopenharmony_ci				  struct urb *urb,
146862306a36Sopenharmony_ci				  size_t len)
146962306a36Sopenharmony_ci{
147062306a36Sopenharmony_ci	struct dma_channel *channel = hw_ep->rx_channel;
147162306a36Sopenharmony_ci	void __iomem *epio = hw_ep->regs;
147262306a36Sopenharmony_ci	dma_addr_t *buf;
147362306a36Sopenharmony_ci	u32 length;
147462306a36Sopenharmony_ci	u16 val;
147562306a36Sopenharmony_ci
147662306a36Sopenharmony_ci	buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset +
147762306a36Sopenharmony_ci		(u32)urb->transfer_dma;
147862306a36Sopenharmony_ci
147962306a36Sopenharmony_ci	length = urb->iso_frame_desc[qh->iso_idx].length;
148062306a36Sopenharmony_ci
148162306a36Sopenharmony_ci	val = musb_readw(epio, MUSB_RXCSR);
148262306a36Sopenharmony_ci	val |= MUSB_RXCSR_DMAENAB;
148362306a36Sopenharmony_ci	musb_writew(hw_ep->regs, MUSB_RXCSR, val);
148462306a36Sopenharmony_ci
148562306a36Sopenharmony_ci	return dma->channel_program(channel, qh->maxpacket, 0,
148662306a36Sopenharmony_ci				   (u32)buf, length);
148762306a36Sopenharmony_ci}
148862306a36Sopenharmony_ci#else
148962306a36Sopenharmony_cistatic inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
149062306a36Sopenharmony_ci					 struct musb_hw_ep *hw_ep,
149162306a36Sopenharmony_ci					 struct musb_qh *qh,
149262306a36Sopenharmony_ci					 struct urb *urb,
149362306a36Sopenharmony_ci					 size_t len)
149462306a36Sopenharmony_ci{
149562306a36Sopenharmony_ci	return false;
149662306a36Sopenharmony_ci}
149762306a36Sopenharmony_ci#endif
149862306a36Sopenharmony_ci
149962306a36Sopenharmony_ci#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
150062306a36Sopenharmony_ci	defined(CONFIG_USB_TI_CPPI41_DMA)
150162306a36Sopenharmony_ci/* Host side RX (IN) using Mentor DMA works as follows:
150262306a36Sopenharmony_ci	submit_urb ->
150362306a36Sopenharmony_ci		- if queue was empty, ProgramEndpoint
150462306a36Sopenharmony_ci		- first IN token is sent out (by setting ReqPkt)
150562306a36Sopenharmony_ci	LinuxIsr -> RxReady()
150662306a36Sopenharmony_ci	/\	=> first packet is received
150762306a36Sopenharmony_ci	|	- Set in mode 0 (DmaEnab, ~ReqPkt)
150862306a36Sopenharmony_ci	|		-> DMA Isr (transfer complete) -> RxReady()
150962306a36Sopenharmony_ci	|		    - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
151062306a36Sopenharmony_ci	|		    - if urb not complete, send next IN token (ReqPkt)
151162306a36Sopenharmony_ci	|			   |		else complete urb.
151262306a36Sopenharmony_ci	|			   |
151362306a36Sopenharmony_ci	---------------------------
151462306a36Sopenharmony_ci *
151562306a36Sopenharmony_ci * Nuances of mode 1:
151662306a36Sopenharmony_ci *	For short packets, no ack (+RxPktRdy) is sent automatically
151762306a36Sopenharmony_ci *	(even if AutoClear is ON)
151862306a36Sopenharmony_ci *	For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
151962306a36Sopenharmony_ci *	automatically => major problem, as collecting the next packet becomes
152062306a36Sopenharmony_ci *	difficult. Hence mode 1 is not used.
152162306a36Sopenharmony_ci *
152262306a36Sopenharmony_ci * REVISIT
152362306a36Sopenharmony_ci *	All we care about at this driver level is that
152462306a36Sopenharmony_ci *       (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
152562306a36Sopenharmony_ci *       (b) termination conditions are: short RX, or buffer full;
152662306a36Sopenharmony_ci *       (c) fault modes include
152762306a36Sopenharmony_ci *           - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
152862306a36Sopenharmony_ci *             (and that endpoint's dma queue stops immediately)
152962306a36Sopenharmony_ci *           - overflow (full, PLUS more bytes in the terminal packet)
153062306a36Sopenharmony_ci *
153162306a36Sopenharmony_ci *	So for example, usb-storage sets URB_SHORT_NOT_OK, and would
153262306a36Sopenharmony_ci *	thus be a great candidate for using mode 1 ... for all but the
153362306a36Sopenharmony_ci *	last packet of one URB's transfer.
153462306a36Sopenharmony_ci */
153562306a36Sopenharmony_cistatic int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
153662306a36Sopenharmony_ci				       struct musb_hw_ep *hw_ep,
153762306a36Sopenharmony_ci				       struct musb_qh *qh,
153862306a36Sopenharmony_ci				       struct urb *urb,
153962306a36Sopenharmony_ci				       size_t len)
154062306a36Sopenharmony_ci{
154162306a36Sopenharmony_ci	struct dma_channel *channel = hw_ep->rx_channel;
154262306a36Sopenharmony_ci	void __iomem *epio = hw_ep->regs;
154362306a36Sopenharmony_ci	u16 val;
154462306a36Sopenharmony_ci	int pipe;
154562306a36Sopenharmony_ci	bool done;
154662306a36Sopenharmony_ci
154762306a36Sopenharmony_ci	pipe = urb->pipe;
154862306a36Sopenharmony_ci
154962306a36Sopenharmony_ci	if (usb_pipeisoc(pipe)) {
155062306a36Sopenharmony_ci		struct usb_iso_packet_descriptor *d;
155162306a36Sopenharmony_ci
155262306a36Sopenharmony_ci		d = urb->iso_frame_desc + qh->iso_idx;
155362306a36Sopenharmony_ci		d->actual_length = len;
155462306a36Sopenharmony_ci
155562306a36Sopenharmony_ci		/* even if there was an error, we did the dma
155662306a36Sopenharmony_ci		 * for iso_frame_desc->length
155762306a36Sopenharmony_ci		 */
155862306a36Sopenharmony_ci		if (d->status != -EILSEQ && d->status != -EOVERFLOW)
155962306a36Sopenharmony_ci			d->status = 0;
156062306a36Sopenharmony_ci
156162306a36Sopenharmony_ci		if (++qh->iso_idx >= urb->number_of_packets) {
156262306a36Sopenharmony_ci			done = true;
156362306a36Sopenharmony_ci		} else {
156462306a36Sopenharmony_ci			/* REVISIT: Why ignore return value here? */
156562306a36Sopenharmony_ci			if (musb_dma_cppi41(hw_ep->musb))
156662306a36Sopenharmony_ci				done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh,
156762306a36Sopenharmony_ci							      urb, len);
156862306a36Sopenharmony_ci			done = false;
156962306a36Sopenharmony_ci		}
157062306a36Sopenharmony_ci
157162306a36Sopenharmony_ci	} else  {
157262306a36Sopenharmony_ci		/* done if urb buffer is full or short packet is recd */
157362306a36Sopenharmony_ci		done = (urb->actual_length + len >=
157462306a36Sopenharmony_ci			urb->transfer_buffer_length
157562306a36Sopenharmony_ci			|| channel->actual_len < qh->maxpacket
157662306a36Sopenharmony_ci			|| channel->rx_packet_done);
157762306a36Sopenharmony_ci	}
157862306a36Sopenharmony_ci
157962306a36Sopenharmony_ci	/* send IN token for next packet, without AUTOREQ */
158062306a36Sopenharmony_ci	if (!done) {
158162306a36Sopenharmony_ci		val = musb_readw(epio, MUSB_RXCSR);
158262306a36Sopenharmony_ci		val |= MUSB_RXCSR_H_REQPKT;
158362306a36Sopenharmony_ci		musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
158462306a36Sopenharmony_ci	}
158562306a36Sopenharmony_ci
158662306a36Sopenharmony_ci	return done;
158762306a36Sopenharmony_ci}
158862306a36Sopenharmony_ci
158962306a36Sopenharmony_ci/* Disadvantage of using mode 1:
159062306a36Sopenharmony_ci *	It's basically usable only for mass storage class; essentially all
159162306a36Sopenharmony_ci *	other protocols also terminate transfers on short packets.
159262306a36Sopenharmony_ci *
159362306a36Sopenharmony_ci * Details:
159462306a36Sopenharmony_ci *	An extra IN token is sent at the end of the transfer (due to AUTOREQ)
159562306a36Sopenharmony_ci *	If you try to use mode 1 for (transfer_buffer_length - 512), and try
159662306a36Sopenharmony_ci *	to use the extra IN token to grab the last packet using mode 0, then
159762306a36Sopenharmony_ci *	the problem is that you cannot be sure when the device will send the
159862306a36Sopenharmony_ci *	last packet and RxPktRdy set. Sometimes the packet is recd too soon
159962306a36Sopenharmony_ci *	such that it gets lost when RxCSR is re-set at the end of the mode 1
160062306a36Sopenharmony_ci *	transfer, while sometimes it is recd just a little late so that if you
160162306a36Sopenharmony_ci *	try to configure for mode 0 soon after the mode 1 transfer is
160262306a36Sopenharmony_ci *	completed, you will find rxcount 0. Okay, so you might think why not
160362306a36Sopenharmony_ci *	wait for an interrupt when the pkt is recd. Well, you won't get any!
160462306a36Sopenharmony_ci */
160562306a36Sopenharmony_cistatic int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
160662306a36Sopenharmony_ci					  struct musb_hw_ep *hw_ep,
160762306a36Sopenharmony_ci					  struct musb_qh *qh,
160862306a36Sopenharmony_ci					  struct urb *urb,
160962306a36Sopenharmony_ci					  size_t len,
161062306a36Sopenharmony_ci					  u8 iso_err)
161162306a36Sopenharmony_ci{
161262306a36Sopenharmony_ci	struct musb *musb = hw_ep->musb;
161362306a36Sopenharmony_ci	void __iomem *epio = hw_ep->regs;
161462306a36Sopenharmony_ci	struct dma_channel *channel = hw_ep->rx_channel;
161562306a36Sopenharmony_ci	u16 rx_count, val;
161662306a36Sopenharmony_ci	int length, pipe, done;
161762306a36Sopenharmony_ci	dma_addr_t buf;
161862306a36Sopenharmony_ci
161962306a36Sopenharmony_ci	rx_count = musb_readw(epio, MUSB_RXCOUNT);
162062306a36Sopenharmony_ci	pipe = urb->pipe;
162162306a36Sopenharmony_ci
162262306a36Sopenharmony_ci	if (usb_pipeisoc(pipe)) {
162362306a36Sopenharmony_ci		int d_status = 0;
162462306a36Sopenharmony_ci		struct usb_iso_packet_descriptor *d;
162562306a36Sopenharmony_ci
162662306a36Sopenharmony_ci		d = urb->iso_frame_desc + qh->iso_idx;
162762306a36Sopenharmony_ci
162862306a36Sopenharmony_ci		if (iso_err) {
162962306a36Sopenharmony_ci			d_status = -EILSEQ;
163062306a36Sopenharmony_ci			urb->error_count++;
163162306a36Sopenharmony_ci		}
163262306a36Sopenharmony_ci		if (rx_count > d->length) {
163362306a36Sopenharmony_ci			if (d_status == 0) {
163462306a36Sopenharmony_ci				d_status = -EOVERFLOW;
163562306a36Sopenharmony_ci				urb->error_count++;
163662306a36Sopenharmony_ci			}
163762306a36Sopenharmony_ci			musb_dbg(musb, "** OVERFLOW %d into %d",
163862306a36Sopenharmony_ci				rx_count, d->length);
163962306a36Sopenharmony_ci
164062306a36Sopenharmony_ci			length = d->length;
164162306a36Sopenharmony_ci		} else
164262306a36Sopenharmony_ci			length = rx_count;
164362306a36Sopenharmony_ci		d->status = d_status;
164462306a36Sopenharmony_ci		buf = urb->transfer_dma + d->offset;
164562306a36Sopenharmony_ci	} else {
164662306a36Sopenharmony_ci		length = rx_count;
164762306a36Sopenharmony_ci		buf = urb->transfer_dma + urb->actual_length;
164862306a36Sopenharmony_ci	}
164962306a36Sopenharmony_ci
165062306a36Sopenharmony_ci	channel->desired_mode = 0;
165162306a36Sopenharmony_ci#ifdef USE_MODE1
165262306a36Sopenharmony_ci	/* because of the issue below, mode 1 will
165362306a36Sopenharmony_ci	 * only rarely behave with correct semantics.
165462306a36Sopenharmony_ci	 */
165562306a36Sopenharmony_ci	if ((urb->transfer_flags & URB_SHORT_NOT_OK)
165662306a36Sopenharmony_ci	    && (urb->transfer_buffer_length - urb->actual_length)
165762306a36Sopenharmony_ci	    > qh->maxpacket)
165862306a36Sopenharmony_ci		channel->desired_mode = 1;
165962306a36Sopenharmony_ci	if (rx_count < hw_ep->max_packet_sz_rx) {
166062306a36Sopenharmony_ci		length = rx_count;
166162306a36Sopenharmony_ci		channel->desired_mode = 0;
166262306a36Sopenharmony_ci	} else {
166362306a36Sopenharmony_ci		length = urb->transfer_buffer_length;
166462306a36Sopenharmony_ci	}
166562306a36Sopenharmony_ci#endif
166662306a36Sopenharmony_ci
166762306a36Sopenharmony_ci	/* See comments above on disadvantages of using mode 1 */
166862306a36Sopenharmony_ci	val = musb_readw(epio, MUSB_RXCSR);
166962306a36Sopenharmony_ci	val &= ~MUSB_RXCSR_H_REQPKT;
167062306a36Sopenharmony_ci
167162306a36Sopenharmony_ci	if (channel->desired_mode == 0)
167262306a36Sopenharmony_ci		val &= ~MUSB_RXCSR_H_AUTOREQ;
167362306a36Sopenharmony_ci	else
167462306a36Sopenharmony_ci		val |= MUSB_RXCSR_H_AUTOREQ;
167562306a36Sopenharmony_ci	val |= MUSB_RXCSR_DMAENAB;
167662306a36Sopenharmony_ci
167762306a36Sopenharmony_ci	/* autoclear shouldn't be set in high bandwidth */
167862306a36Sopenharmony_ci	if (qh->hb_mult == 1)
167962306a36Sopenharmony_ci		val |= MUSB_RXCSR_AUTOCLEAR;
168062306a36Sopenharmony_ci
168162306a36Sopenharmony_ci	musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
168262306a36Sopenharmony_ci
168362306a36Sopenharmony_ci	/* REVISIT if when actual_length != 0,
168462306a36Sopenharmony_ci	 * transfer_buffer_length needs to be
168562306a36Sopenharmony_ci	 * adjusted first...
168662306a36Sopenharmony_ci	 */
168762306a36Sopenharmony_ci	done = dma->channel_program(channel, qh->maxpacket,
168862306a36Sopenharmony_ci				   channel->desired_mode,
168962306a36Sopenharmony_ci				   buf, length);
169062306a36Sopenharmony_ci
169162306a36Sopenharmony_ci	if (!done) {
169262306a36Sopenharmony_ci		dma->channel_release(channel);
169362306a36Sopenharmony_ci		hw_ep->rx_channel = NULL;
169462306a36Sopenharmony_ci		channel = NULL;
169562306a36Sopenharmony_ci		val = musb_readw(epio, MUSB_RXCSR);
169662306a36Sopenharmony_ci		val &= ~(MUSB_RXCSR_DMAENAB
169762306a36Sopenharmony_ci			 | MUSB_RXCSR_H_AUTOREQ
169862306a36Sopenharmony_ci			 | MUSB_RXCSR_AUTOCLEAR);
169962306a36Sopenharmony_ci		musb_writew(epio, MUSB_RXCSR, val);
170062306a36Sopenharmony_ci	}
170162306a36Sopenharmony_ci
170262306a36Sopenharmony_ci	return done;
170362306a36Sopenharmony_ci}
170462306a36Sopenharmony_ci#else
170562306a36Sopenharmony_cistatic inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
170662306a36Sopenharmony_ci					      struct musb_hw_ep *hw_ep,
170762306a36Sopenharmony_ci					      struct musb_qh *qh,
170862306a36Sopenharmony_ci					      struct urb *urb,
170962306a36Sopenharmony_ci					      size_t len)
171062306a36Sopenharmony_ci{
171162306a36Sopenharmony_ci	return false;
171262306a36Sopenharmony_ci}
171362306a36Sopenharmony_ci
171462306a36Sopenharmony_cistatic inline int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
171562306a36Sopenharmony_ci						 struct musb_hw_ep *hw_ep,
171662306a36Sopenharmony_ci						 struct musb_qh *qh,
171762306a36Sopenharmony_ci						 struct urb *urb,
171862306a36Sopenharmony_ci						 size_t len,
171962306a36Sopenharmony_ci						 u8 iso_err)
172062306a36Sopenharmony_ci{
172162306a36Sopenharmony_ci	return false;
172262306a36Sopenharmony_ci}
172362306a36Sopenharmony_ci#endif
172462306a36Sopenharmony_ci
172562306a36Sopenharmony_ci/*
172662306a36Sopenharmony_ci * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
172762306a36Sopenharmony_ci * and high-bandwidth IN transfer cases.
172862306a36Sopenharmony_ci */
172962306a36Sopenharmony_civoid musb_host_rx(struct musb *musb, u8 epnum)
173062306a36Sopenharmony_ci{
173162306a36Sopenharmony_ci	struct urb		*urb;
173262306a36Sopenharmony_ci	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
173362306a36Sopenharmony_ci	struct dma_controller	*c = musb->dma_controller;
173462306a36Sopenharmony_ci	void __iomem		*epio = hw_ep->regs;
173562306a36Sopenharmony_ci	struct musb_qh		*qh = hw_ep->in_qh;
173662306a36Sopenharmony_ci	size_t			xfer_len;
173762306a36Sopenharmony_ci	void __iomem		*mbase = musb->mregs;
173862306a36Sopenharmony_ci	u16			rx_csr, val;
173962306a36Sopenharmony_ci	bool			iso_err = false;
174062306a36Sopenharmony_ci	bool			done = false;
174162306a36Sopenharmony_ci	u32			status;
174262306a36Sopenharmony_ci	struct dma_channel	*dma;
174362306a36Sopenharmony_ci	unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
174462306a36Sopenharmony_ci
174562306a36Sopenharmony_ci	musb_ep_select(mbase, epnum);
174662306a36Sopenharmony_ci
174762306a36Sopenharmony_ci	urb = next_urb(qh);
174862306a36Sopenharmony_ci	dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
174962306a36Sopenharmony_ci	status = 0;
175062306a36Sopenharmony_ci	xfer_len = 0;
175162306a36Sopenharmony_ci
175262306a36Sopenharmony_ci	rx_csr = musb_readw(epio, MUSB_RXCSR);
175362306a36Sopenharmony_ci	val = rx_csr;
175462306a36Sopenharmony_ci
175562306a36Sopenharmony_ci	if (unlikely(!urb)) {
175662306a36Sopenharmony_ci		/* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
175762306a36Sopenharmony_ci		 * usbtest #11 (unlinks) triggers it regularly, sometimes
175862306a36Sopenharmony_ci		 * with fifo full.  (Only with DMA??)
175962306a36Sopenharmony_ci		 */
176062306a36Sopenharmony_ci		musb_dbg(musb, "BOGUS RX%d ready, csr %04x, count %d",
176162306a36Sopenharmony_ci			epnum, val, musb_readw(epio, MUSB_RXCOUNT));
176262306a36Sopenharmony_ci		musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
176362306a36Sopenharmony_ci		return;
176462306a36Sopenharmony_ci	}
176562306a36Sopenharmony_ci
176662306a36Sopenharmony_ci	trace_musb_urb_rx(musb, urb);
176762306a36Sopenharmony_ci
176862306a36Sopenharmony_ci	/* check for errors, concurrent stall & unlink is not really
176962306a36Sopenharmony_ci	 * handled yet! */
177062306a36Sopenharmony_ci	if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
177162306a36Sopenharmony_ci		musb_dbg(musb, "RX end %d STALL", epnum);
177262306a36Sopenharmony_ci
177362306a36Sopenharmony_ci		/* stall; record URB status */
177462306a36Sopenharmony_ci		status = -EPIPE;
177562306a36Sopenharmony_ci
177662306a36Sopenharmony_ci	} else if (rx_csr & MUSB_RXCSR_H_ERROR) {
177762306a36Sopenharmony_ci		dev_err(musb->controller, "ep%d RX three-strikes error", epnum);
177862306a36Sopenharmony_ci
177962306a36Sopenharmony_ci		/*
178062306a36Sopenharmony_ci		 * The three-strikes error could only happen when the USB
178162306a36Sopenharmony_ci		 * device is not accessible, for example detached or powered
178262306a36Sopenharmony_ci		 * off. So return the fatal error -ESHUTDOWN so hopefully the
178362306a36Sopenharmony_ci		 * USB device drivers won't immediately resubmit the same URB.
178462306a36Sopenharmony_ci		 */
178562306a36Sopenharmony_ci		status = -ESHUTDOWN;
178662306a36Sopenharmony_ci		musb_writeb(epio, MUSB_RXINTERVAL, 0);
178762306a36Sopenharmony_ci
178862306a36Sopenharmony_ci		rx_csr &= ~MUSB_RXCSR_H_ERROR;
178962306a36Sopenharmony_ci		musb_writew(epio, MUSB_RXCSR, rx_csr);
179062306a36Sopenharmony_ci
179162306a36Sopenharmony_ci	} else if (rx_csr & MUSB_RXCSR_DATAERROR) {
179262306a36Sopenharmony_ci
179362306a36Sopenharmony_ci		if (USB_ENDPOINT_XFER_ISOC != qh->type) {
179462306a36Sopenharmony_ci			musb_dbg(musb, "RX end %d NAK timeout", epnum);
179562306a36Sopenharmony_ci
179662306a36Sopenharmony_ci			/* NOTE: NAKing is *NOT* an error, so we want to
179762306a36Sopenharmony_ci			 * continue.  Except ... if there's a request for
179862306a36Sopenharmony_ci			 * another QH, use that instead of starving it.
179962306a36Sopenharmony_ci			 *
180062306a36Sopenharmony_ci			 * Devices like Ethernet and serial adapters keep
180162306a36Sopenharmony_ci			 * reads posted at all times, which will starve
180262306a36Sopenharmony_ci			 * other devices without this logic.
180362306a36Sopenharmony_ci			 */
180462306a36Sopenharmony_ci			if (usb_pipebulk(urb->pipe)
180562306a36Sopenharmony_ci					&& qh->mux == 1
180662306a36Sopenharmony_ci					&& !list_is_singular(&musb->in_bulk)) {
180762306a36Sopenharmony_ci				musb_bulk_nak_timeout(musb, hw_ep, 1);
180862306a36Sopenharmony_ci				return;
180962306a36Sopenharmony_ci			}
181062306a36Sopenharmony_ci			musb_ep_select(mbase, epnum);
181162306a36Sopenharmony_ci			rx_csr |= MUSB_RXCSR_H_WZC_BITS;
181262306a36Sopenharmony_ci			rx_csr &= ~MUSB_RXCSR_DATAERROR;
181362306a36Sopenharmony_ci			musb_writew(epio, MUSB_RXCSR, rx_csr);
181462306a36Sopenharmony_ci
181562306a36Sopenharmony_ci			goto finish;
181662306a36Sopenharmony_ci		} else {
181762306a36Sopenharmony_ci			musb_dbg(musb, "RX end %d ISO data error", epnum);
181862306a36Sopenharmony_ci			/* packet error reported later */
181962306a36Sopenharmony_ci			iso_err = true;
182062306a36Sopenharmony_ci		}
182162306a36Sopenharmony_ci	} else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
182262306a36Sopenharmony_ci		musb_dbg(musb, "end %d high bandwidth incomplete ISO packet RX",
182362306a36Sopenharmony_ci				epnum);
182462306a36Sopenharmony_ci		status = -EPROTO;
182562306a36Sopenharmony_ci	}
182662306a36Sopenharmony_ci
182762306a36Sopenharmony_ci	/* faults abort the transfer */
182862306a36Sopenharmony_ci	if (status) {
182962306a36Sopenharmony_ci		/* clean up dma and collect transfer count */
183062306a36Sopenharmony_ci		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
183162306a36Sopenharmony_ci			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
183262306a36Sopenharmony_ci			musb->dma_controller->channel_abort(dma);
183362306a36Sopenharmony_ci			xfer_len = dma->actual_len;
183462306a36Sopenharmony_ci		}
183562306a36Sopenharmony_ci		musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
183662306a36Sopenharmony_ci		musb_writeb(epio, MUSB_RXINTERVAL, 0);
183762306a36Sopenharmony_ci		done = true;
183862306a36Sopenharmony_ci		goto finish;
183962306a36Sopenharmony_ci	}
184062306a36Sopenharmony_ci
184162306a36Sopenharmony_ci	if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
184262306a36Sopenharmony_ci		/* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
184362306a36Sopenharmony_ci		ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
184462306a36Sopenharmony_ci		goto finish;
184562306a36Sopenharmony_ci	}
184662306a36Sopenharmony_ci
184762306a36Sopenharmony_ci	/* thorough shutdown for now ... given more precise fault handling
184862306a36Sopenharmony_ci	 * and better queueing support, we might keep a DMA pipeline going
184962306a36Sopenharmony_ci	 * while processing this irq for earlier completions.
185062306a36Sopenharmony_ci	 */
185162306a36Sopenharmony_ci
185262306a36Sopenharmony_ci	/* FIXME this is _way_ too much in-line logic for Mentor DMA */
185362306a36Sopenharmony_ci	if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) &&
185462306a36Sopenharmony_ci	    (rx_csr & MUSB_RXCSR_H_REQPKT)) {
185562306a36Sopenharmony_ci		/* REVISIT this happened for a while on some short reads...
185662306a36Sopenharmony_ci		 * the cleanup still needs investigation... looks bad...
185762306a36Sopenharmony_ci		 * and also duplicates dma cleanup code above ... plus,
185862306a36Sopenharmony_ci		 * shouldn't this be the "half full" double buffer case?
185962306a36Sopenharmony_ci		 */
186062306a36Sopenharmony_ci		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
186162306a36Sopenharmony_ci			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
186262306a36Sopenharmony_ci			musb->dma_controller->channel_abort(dma);
186362306a36Sopenharmony_ci			xfer_len = dma->actual_len;
186462306a36Sopenharmony_ci			done = true;
186562306a36Sopenharmony_ci		}
186662306a36Sopenharmony_ci
186762306a36Sopenharmony_ci		musb_dbg(musb, "RXCSR%d %04x, reqpkt, len %zu%s", epnum, rx_csr,
186862306a36Sopenharmony_ci				xfer_len, dma ? ", dma" : "");
186962306a36Sopenharmony_ci		rx_csr &= ~MUSB_RXCSR_H_REQPKT;
187062306a36Sopenharmony_ci
187162306a36Sopenharmony_ci		musb_ep_select(mbase, epnum);
187262306a36Sopenharmony_ci		musb_writew(epio, MUSB_RXCSR,
187362306a36Sopenharmony_ci				MUSB_RXCSR_H_WZC_BITS | rx_csr);
187462306a36Sopenharmony_ci	}
187562306a36Sopenharmony_ci
187662306a36Sopenharmony_ci	if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
187762306a36Sopenharmony_ci		xfer_len = dma->actual_len;
187862306a36Sopenharmony_ci
187962306a36Sopenharmony_ci		val &= ~(MUSB_RXCSR_DMAENAB
188062306a36Sopenharmony_ci			| MUSB_RXCSR_H_AUTOREQ
188162306a36Sopenharmony_ci			| MUSB_RXCSR_AUTOCLEAR
188262306a36Sopenharmony_ci			| MUSB_RXCSR_RXPKTRDY);
188362306a36Sopenharmony_ci		musb_writew(hw_ep->regs, MUSB_RXCSR, val);
188462306a36Sopenharmony_ci
188562306a36Sopenharmony_ci		if (musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
188662306a36Sopenharmony_ci		    musb_dma_cppi41(musb)) {
188762306a36Sopenharmony_ci			    done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len);
188862306a36Sopenharmony_ci			    musb_dbg(hw_ep->musb,
188962306a36Sopenharmony_ci				    "ep %d dma %s, rxcsr %04x, rxcount %d",
189062306a36Sopenharmony_ci				    epnum, done ? "off" : "reset",
189162306a36Sopenharmony_ci				    musb_readw(epio, MUSB_RXCSR),
189262306a36Sopenharmony_ci				    musb_readw(epio, MUSB_RXCOUNT));
189362306a36Sopenharmony_ci		} else {
189462306a36Sopenharmony_ci			done = true;
189562306a36Sopenharmony_ci		}
189662306a36Sopenharmony_ci
189762306a36Sopenharmony_ci	} else if (urb->status == -EINPROGRESS) {
189862306a36Sopenharmony_ci		/* if no errors, be sure a packet is ready for unloading */
189962306a36Sopenharmony_ci		if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
190062306a36Sopenharmony_ci			status = -EPROTO;
190162306a36Sopenharmony_ci			ERR("Rx interrupt with no errors or packet!\n");
190262306a36Sopenharmony_ci
190362306a36Sopenharmony_ci			/* FIXME this is another "SHOULD NEVER HAPPEN" */
190462306a36Sopenharmony_ci
190562306a36Sopenharmony_ci/* SCRUB (RX) */
190662306a36Sopenharmony_ci			/* do the proper sequence to abort the transfer */
190762306a36Sopenharmony_ci			musb_ep_select(mbase, epnum);
190862306a36Sopenharmony_ci			val &= ~MUSB_RXCSR_H_REQPKT;
190962306a36Sopenharmony_ci			musb_writew(epio, MUSB_RXCSR, val);
191062306a36Sopenharmony_ci			goto finish;
191162306a36Sopenharmony_ci		}
191262306a36Sopenharmony_ci
191362306a36Sopenharmony_ci		/* we are expecting IN packets */
191462306a36Sopenharmony_ci		if ((musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
191562306a36Sopenharmony_ci		    musb_dma_cppi41(musb)) && dma) {
191662306a36Sopenharmony_ci			musb_dbg(hw_ep->musb,
191762306a36Sopenharmony_ci				"RX%d count %d, buffer 0x%llx len %d/%d",
191862306a36Sopenharmony_ci				epnum, musb_readw(epio, MUSB_RXCOUNT),
191962306a36Sopenharmony_ci				(unsigned long long) urb->transfer_dma
192062306a36Sopenharmony_ci				+ urb->actual_length,
192162306a36Sopenharmony_ci				qh->offset,
192262306a36Sopenharmony_ci				urb->transfer_buffer_length);
192362306a36Sopenharmony_ci
192462306a36Sopenharmony_ci			if (musb_rx_dma_in_inventra_cppi41(c, hw_ep, qh, urb,
192562306a36Sopenharmony_ci							   xfer_len, iso_err))
192662306a36Sopenharmony_ci				goto finish;
192762306a36Sopenharmony_ci			else
192862306a36Sopenharmony_ci				dev_err(musb->controller, "error: rx_dma failed\n");
192962306a36Sopenharmony_ci		}
193062306a36Sopenharmony_ci
193162306a36Sopenharmony_ci		if (!dma) {
193262306a36Sopenharmony_ci			unsigned int received_len;
193362306a36Sopenharmony_ci
193462306a36Sopenharmony_ci			/* Unmap the buffer so that CPU can use it */
193562306a36Sopenharmony_ci			usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
193662306a36Sopenharmony_ci
193762306a36Sopenharmony_ci			/*
193862306a36Sopenharmony_ci			 * We need to map sg if the transfer_buffer is
193962306a36Sopenharmony_ci			 * NULL.
194062306a36Sopenharmony_ci			 */
194162306a36Sopenharmony_ci			if (!urb->transfer_buffer) {
194262306a36Sopenharmony_ci				qh->use_sg = true;
194362306a36Sopenharmony_ci				sg_miter_start(&qh->sg_miter, urb->sg, 1,
194462306a36Sopenharmony_ci						sg_flags);
194562306a36Sopenharmony_ci			}
194662306a36Sopenharmony_ci
194762306a36Sopenharmony_ci			if (qh->use_sg) {
194862306a36Sopenharmony_ci				if (!sg_miter_next(&qh->sg_miter)) {
194962306a36Sopenharmony_ci					dev_err(musb->controller, "error: sg list empty\n");
195062306a36Sopenharmony_ci					sg_miter_stop(&qh->sg_miter);
195162306a36Sopenharmony_ci					status = -EINVAL;
195262306a36Sopenharmony_ci					done = true;
195362306a36Sopenharmony_ci					goto finish;
195462306a36Sopenharmony_ci				}
195562306a36Sopenharmony_ci				urb->transfer_buffer = qh->sg_miter.addr;
195662306a36Sopenharmony_ci				received_len = urb->actual_length;
195762306a36Sopenharmony_ci				qh->offset = 0x0;
195862306a36Sopenharmony_ci				done = musb_host_packet_rx(musb, urb, epnum,
195962306a36Sopenharmony_ci						iso_err);
196062306a36Sopenharmony_ci				/* Calculate the number of bytes received */
196162306a36Sopenharmony_ci				received_len = urb->actual_length -
196262306a36Sopenharmony_ci					received_len;
196362306a36Sopenharmony_ci				qh->sg_miter.consumed = received_len;
196462306a36Sopenharmony_ci				sg_miter_stop(&qh->sg_miter);
196562306a36Sopenharmony_ci			} else {
196662306a36Sopenharmony_ci				done = musb_host_packet_rx(musb, urb,
196762306a36Sopenharmony_ci						epnum, iso_err);
196862306a36Sopenharmony_ci			}
196962306a36Sopenharmony_ci			musb_dbg(musb, "read %spacket", done ? "last " : "");
197062306a36Sopenharmony_ci		}
197162306a36Sopenharmony_ci	}
197262306a36Sopenharmony_ci
197362306a36Sopenharmony_cifinish:
197462306a36Sopenharmony_ci	urb->actual_length += xfer_len;
197562306a36Sopenharmony_ci	qh->offset += xfer_len;
197662306a36Sopenharmony_ci	if (done) {
197762306a36Sopenharmony_ci		if (qh->use_sg) {
197862306a36Sopenharmony_ci			qh->use_sg = false;
197962306a36Sopenharmony_ci			urb->transfer_buffer = NULL;
198062306a36Sopenharmony_ci		}
198162306a36Sopenharmony_ci
198262306a36Sopenharmony_ci		if (urb->status == -EINPROGRESS)
198362306a36Sopenharmony_ci			urb->status = status;
198462306a36Sopenharmony_ci		musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
198562306a36Sopenharmony_ci	}
198662306a36Sopenharmony_ci}
198762306a36Sopenharmony_ci
198862306a36Sopenharmony_ci/* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
198962306a36Sopenharmony_ci * the software schedule associates multiple such nodes with a given
199062306a36Sopenharmony_ci * host side hardware endpoint + direction; scheduling may activate
199162306a36Sopenharmony_ci * that hardware endpoint.
199262306a36Sopenharmony_ci */
199362306a36Sopenharmony_cistatic int musb_schedule(
199462306a36Sopenharmony_ci	struct musb		*musb,
199562306a36Sopenharmony_ci	struct musb_qh		*qh,
199662306a36Sopenharmony_ci	int			is_in)
199762306a36Sopenharmony_ci{
199862306a36Sopenharmony_ci	int			idle = 0;
199962306a36Sopenharmony_ci	int			best_diff;
200062306a36Sopenharmony_ci	int			best_end, epnum;
200162306a36Sopenharmony_ci	struct musb_hw_ep	*hw_ep = NULL;
200262306a36Sopenharmony_ci	struct list_head	*head = NULL;
200362306a36Sopenharmony_ci	u8			toggle;
200462306a36Sopenharmony_ci	u8			txtype;
200562306a36Sopenharmony_ci	struct urb		*urb = next_urb(qh);
200662306a36Sopenharmony_ci
200762306a36Sopenharmony_ci	/* use fixed hardware for control and bulk */
200862306a36Sopenharmony_ci	if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
200962306a36Sopenharmony_ci		head = &musb->control;
201062306a36Sopenharmony_ci		hw_ep = musb->control_ep;
201162306a36Sopenharmony_ci		goto success;
201262306a36Sopenharmony_ci	}
201362306a36Sopenharmony_ci
201462306a36Sopenharmony_ci	/* else, periodic transfers get muxed to other endpoints */
201562306a36Sopenharmony_ci
201662306a36Sopenharmony_ci	/*
201762306a36Sopenharmony_ci	 * We know this qh hasn't been scheduled, so all we need to do
201862306a36Sopenharmony_ci	 * is choose which hardware endpoint to put it on ...
201962306a36Sopenharmony_ci	 *
202062306a36Sopenharmony_ci	 * REVISIT what we really want here is a regular schedule tree
202162306a36Sopenharmony_ci	 * like e.g. OHCI uses.
202262306a36Sopenharmony_ci	 */
202362306a36Sopenharmony_ci	best_diff = 4096;
202462306a36Sopenharmony_ci	best_end = -1;
202562306a36Sopenharmony_ci
202662306a36Sopenharmony_ci	for (epnum = 1, hw_ep = musb->endpoints + 1;
202762306a36Sopenharmony_ci			epnum < musb->nr_endpoints;
202862306a36Sopenharmony_ci			epnum++, hw_ep++) {
202962306a36Sopenharmony_ci		int	diff;
203062306a36Sopenharmony_ci
203162306a36Sopenharmony_ci		if (musb_ep_get_qh(hw_ep, is_in) != NULL)
203262306a36Sopenharmony_ci			continue;
203362306a36Sopenharmony_ci
203462306a36Sopenharmony_ci		if (hw_ep == musb->bulk_ep)
203562306a36Sopenharmony_ci			continue;
203662306a36Sopenharmony_ci
203762306a36Sopenharmony_ci		if (is_in)
203862306a36Sopenharmony_ci			diff = hw_ep->max_packet_sz_rx;
203962306a36Sopenharmony_ci		else
204062306a36Sopenharmony_ci			diff = hw_ep->max_packet_sz_tx;
204162306a36Sopenharmony_ci		diff -= (qh->maxpacket * qh->hb_mult);
204262306a36Sopenharmony_ci
204362306a36Sopenharmony_ci		if (diff >= 0 && best_diff > diff) {
204462306a36Sopenharmony_ci
204562306a36Sopenharmony_ci			/*
204662306a36Sopenharmony_ci			 * Mentor controller has a bug in that if we schedule
204762306a36Sopenharmony_ci			 * a BULK Tx transfer on an endpoint that had earlier
204862306a36Sopenharmony_ci			 * handled ISOC then the BULK transfer has to start on
204962306a36Sopenharmony_ci			 * a zero toggle.  If the BULK transfer starts on a 1
205062306a36Sopenharmony_ci			 * toggle then this transfer will fail as the mentor
205162306a36Sopenharmony_ci			 * controller starts the Bulk transfer on a 0 toggle
205262306a36Sopenharmony_ci			 * irrespective of the programming of the toggle bits
205362306a36Sopenharmony_ci			 * in the TXCSR register.  Check for this condition
205462306a36Sopenharmony_ci			 * while allocating the EP for a Tx Bulk transfer.  If
205562306a36Sopenharmony_ci			 * so skip this EP.
205662306a36Sopenharmony_ci			 */
205762306a36Sopenharmony_ci			hw_ep = musb->endpoints + epnum;
205862306a36Sopenharmony_ci			toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
205962306a36Sopenharmony_ci			txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
206062306a36Sopenharmony_ci					>> 4) & 0x3;
206162306a36Sopenharmony_ci			if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
206262306a36Sopenharmony_ci				toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
206362306a36Sopenharmony_ci				continue;
206462306a36Sopenharmony_ci
206562306a36Sopenharmony_ci			best_diff = diff;
206662306a36Sopenharmony_ci			best_end = epnum;
206762306a36Sopenharmony_ci		}
206862306a36Sopenharmony_ci	}
206962306a36Sopenharmony_ci	/* use bulk reserved ep1 if no other ep is free */
207062306a36Sopenharmony_ci	if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
207162306a36Sopenharmony_ci		hw_ep = musb->bulk_ep;
207262306a36Sopenharmony_ci		if (is_in)
207362306a36Sopenharmony_ci			head = &musb->in_bulk;
207462306a36Sopenharmony_ci		else
207562306a36Sopenharmony_ci			head = &musb->out_bulk;
207662306a36Sopenharmony_ci
207762306a36Sopenharmony_ci		/* Enable bulk RX/TX NAK timeout scheme when bulk requests are
207862306a36Sopenharmony_ci		 * multiplexed. This scheme does not work in high speed to full
207962306a36Sopenharmony_ci		 * speed scenario as NAK interrupts are not coming from a
208062306a36Sopenharmony_ci		 * full speed device connected to a high speed device.
208162306a36Sopenharmony_ci		 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
208262306a36Sopenharmony_ci		 * 4 (8 frame or 8ms) for FS device.
208362306a36Sopenharmony_ci		 */
208462306a36Sopenharmony_ci		if (qh->dev)
208562306a36Sopenharmony_ci			qh->intv_reg =
208662306a36Sopenharmony_ci				(USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
208762306a36Sopenharmony_ci		goto success;
208862306a36Sopenharmony_ci	} else if (best_end < 0) {
208962306a36Sopenharmony_ci		dev_err(musb->controller,
209062306a36Sopenharmony_ci				"%s hwep alloc failed for %dx%d\n",
209162306a36Sopenharmony_ci				musb_ep_xfertype_string(qh->type),
209262306a36Sopenharmony_ci				qh->hb_mult, qh->maxpacket);
209362306a36Sopenharmony_ci		return -ENOSPC;
209462306a36Sopenharmony_ci	}
209562306a36Sopenharmony_ci
209662306a36Sopenharmony_ci	idle = 1;
209762306a36Sopenharmony_ci	qh->mux = 0;
209862306a36Sopenharmony_ci	hw_ep = musb->endpoints + best_end;
209962306a36Sopenharmony_ci	musb_dbg(musb, "qh %p periodic slot %d", qh, best_end);
210062306a36Sopenharmony_cisuccess:
210162306a36Sopenharmony_ci	if (head) {
210262306a36Sopenharmony_ci		idle = list_empty(head);
210362306a36Sopenharmony_ci		list_add_tail(&qh->ring, head);
210462306a36Sopenharmony_ci		qh->mux = 1;
210562306a36Sopenharmony_ci	}
210662306a36Sopenharmony_ci	qh->hw_ep = hw_ep;
210762306a36Sopenharmony_ci	qh->hep->hcpriv = qh;
210862306a36Sopenharmony_ci	if (idle)
210962306a36Sopenharmony_ci		musb_start_urb(musb, is_in, qh);
211062306a36Sopenharmony_ci	return 0;
211162306a36Sopenharmony_ci}
211262306a36Sopenharmony_ci
211362306a36Sopenharmony_cistatic int musb_urb_enqueue(
211462306a36Sopenharmony_ci	struct usb_hcd			*hcd,
211562306a36Sopenharmony_ci	struct urb			*urb,
211662306a36Sopenharmony_ci	gfp_t				mem_flags)
211762306a36Sopenharmony_ci{
211862306a36Sopenharmony_ci	unsigned long			flags;
211962306a36Sopenharmony_ci	struct musb			*musb = hcd_to_musb(hcd);
212062306a36Sopenharmony_ci	struct usb_host_endpoint	*hep = urb->ep;
212162306a36Sopenharmony_ci	struct musb_qh			*qh;
212262306a36Sopenharmony_ci	struct usb_endpoint_descriptor	*epd = &hep->desc;
212362306a36Sopenharmony_ci	int				ret;
212462306a36Sopenharmony_ci	unsigned			type_reg;
212562306a36Sopenharmony_ci	unsigned			interval;
212662306a36Sopenharmony_ci
212762306a36Sopenharmony_ci	/* host role must be active */
212862306a36Sopenharmony_ci	if (!is_host_active(musb) || !musb->is_active)
212962306a36Sopenharmony_ci		return -ENODEV;
213062306a36Sopenharmony_ci
213162306a36Sopenharmony_ci	trace_musb_urb_enq(musb, urb);
213262306a36Sopenharmony_ci
213362306a36Sopenharmony_ci	spin_lock_irqsave(&musb->lock, flags);
213462306a36Sopenharmony_ci	ret = usb_hcd_link_urb_to_ep(hcd, urb);
213562306a36Sopenharmony_ci	qh = ret ? NULL : hep->hcpriv;
213662306a36Sopenharmony_ci	if (qh)
213762306a36Sopenharmony_ci		urb->hcpriv = qh;
213862306a36Sopenharmony_ci	spin_unlock_irqrestore(&musb->lock, flags);
213962306a36Sopenharmony_ci
214062306a36Sopenharmony_ci	/* DMA mapping was already done, if needed, and this urb is on
214162306a36Sopenharmony_ci	 * hep->urb_list now ... so we're done, unless hep wasn't yet
214262306a36Sopenharmony_ci	 * scheduled onto a live qh.
214362306a36Sopenharmony_ci	 *
214462306a36Sopenharmony_ci	 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
214562306a36Sopenharmony_ci	 * disabled, testing for empty qh->ring and avoiding qh setup costs
214662306a36Sopenharmony_ci	 * except for the first urb queued after a config change.
214762306a36Sopenharmony_ci	 */
214862306a36Sopenharmony_ci	if (qh || ret)
214962306a36Sopenharmony_ci		return ret;
215062306a36Sopenharmony_ci
215162306a36Sopenharmony_ci	/* Allocate and initialize qh, minimizing the work done each time
215262306a36Sopenharmony_ci	 * hw_ep gets reprogrammed, or with irqs blocked.  Then schedule it.
215362306a36Sopenharmony_ci	 *
215462306a36Sopenharmony_ci	 * REVISIT consider a dedicated qh kmem_cache, so it's harder
215562306a36Sopenharmony_ci	 * for bugs in other kernel code to break this driver...
215662306a36Sopenharmony_ci	 */
215762306a36Sopenharmony_ci	qh = kzalloc(sizeof *qh, mem_flags);
215862306a36Sopenharmony_ci	if (!qh) {
215962306a36Sopenharmony_ci		spin_lock_irqsave(&musb->lock, flags);
216062306a36Sopenharmony_ci		usb_hcd_unlink_urb_from_ep(hcd, urb);
216162306a36Sopenharmony_ci		spin_unlock_irqrestore(&musb->lock, flags);
216262306a36Sopenharmony_ci		return -ENOMEM;
216362306a36Sopenharmony_ci	}
216462306a36Sopenharmony_ci
216562306a36Sopenharmony_ci	qh->hep = hep;
216662306a36Sopenharmony_ci	qh->dev = urb->dev;
216762306a36Sopenharmony_ci	INIT_LIST_HEAD(&qh->ring);
216862306a36Sopenharmony_ci	qh->is_ready = 1;
216962306a36Sopenharmony_ci
217062306a36Sopenharmony_ci	qh->maxpacket = usb_endpoint_maxp(epd);
217162306a36Sopenharmony_ci	qh->type = usb_endpoint_type(epd);
217262306a36Sopenharmony_ci
217362306a36Sopenharmony_ci	/* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
217462306a36Sopenharmony_ci	 * Some musb cores don't support high bandwidth ISO transfers; and
217562306a36Sopenharmony_ci	 * we don't (yet!) support high bandwidth interrupt transfers.
217662306a36Sopenharmony_ci	 */
217762306a36Sopenharmony_ci	qh->hb_mult = usb_endpoint_maxp_mult(epd);
217862306a36Sopenharmony_ci	if (qh->hb_mult > 1) {
217962306a36Sopenharmony_ci		int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
218062306a36Sopenharmony_ci
218162306a36Sopenharmony_ci		if (ok)
218262306a36Sopenharmony_ci			ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
218362306a36Sopenharmony_ci				|| (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
218462306a36Sopenharmony_ci		if (!ok) {
218562306a36Sopenharmony_ci			dev_err(musb->controller,
218662306a36Sopenharmony_ci				"high bandwidth %s (%dx%d) not supported\n",
218762306a36Sopenharmony_ci				musb_ep_xfertype_string(qh->type),
218862306a36Sopenharmony_ci				qh->hb_mult, qh->maxpacket & 0x7ff);
218962306a36Sopenharmony_ci			ret = -EMSGSIZE;
219062306a36Sopenharmony_ci			goto done;
219162306a36Sopenharmony_ci		}
219262306a36Sopenharmony_ci		qh->maxpacket &= 0x7ff;
219362306a36Sopenharmony_ci	}
219462306a36Sopenharmony_ci
219562306a36Sopenharmony_ci	qh->epnum = usb_endpoint_num(epd);
219662306a36Sopenharmony_ci
219762306a36Sopenharmony_ci	/* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
219862306a36Sopenharmony_ci	qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
219962306a36Sopenharmony_ci
220062306a36Sopenharmony_ci	/* precompute rxtype/txtype/type0 register */
220162306a36Sopenharmony_ci	type_reg = (qh->type << 4) | qh->epnum;
220262306a36Sopenharmony_ci	switch (urb->dev->speed) {
220362306a36Sopenharmony_ci	case USB_SPEED_LOW:
220462306a36Sopenharmony_ci		type_reg |= 0xc0;
220562306a36Sopenharmony_ci		break;
220662306a36Sopenharmony_ci	case USB_SPEED_FULL:
220762306a36Sopenharmony_ci		type_reg |= 0x80;
220862306a36Sopenharmony_ci		break;
220962306a36Sopenharmony_ci	default:
221062306a36Sopenharmony_ci		type_reg |= 0x40;
221162306a36Sopenharmony_ci	}
221262306a36Sopenharmony_ci	qh->type_reg = type_reg;
221362306a36Sopenharmony_ci
221462306a36Sopenharmony_ci	/* Precompute RXINTERVAL/TXINTERVAL register */
221562306a36Sopenharmony_ci	switch (qh->type) {
221662306a36Sopenharmony_ci	case USB_ENDPOINT_XFER_INT:
221762306a36Sopenharmony_ci		/*
221862306a36Sopenharmony_ci		 * Full/low speeds use the  linear encoding,
221962306a36Sopenharmony_ci		 * high speed uses the logarithmic encoding.
222062306a36Sopenharmony_ci		 */
222162306a36Sopenharmony_ci		if (urb->dev->speed <= USB_SPEED_FULL) {
222262306a36Sopenharmony_ci			interval = max_t(u8, epd->bInterval, 1);
222362306a36Sopenharmony_ci			break;
222462306a36Sopenharmony_ci		}
222562306a36Sopenharmony_ci		fallthrough;
222662306a36Sopenharmony_ci	case USB_ENDPOINT_XFER_ISOC:
222762306a36Sopenharmony_ci		/* ISO always uses logarithmic encoding */
222862306a36Sopenharmony_ci		interval = min_t(u8, epd->bInterval, 16);
222962306a36Sopenharmony_ci		break;
223062306a36Sopenharmony_ci	default:
223162306a36Sopenharmony_ci		/* REVISIT we actually want to use NAK limits, hinting to the
223262306a36Sopenharmony_ci		 * transfer scheduling logic to try some other qh, e.g. try
223362306a36Sopenharmony_ci		 * for 2 msec first:
223462306a36Sopenharmony_ci		 *
223562306a36Sopenharmony_ci		 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
223662306a36Sopenharmony_ci		 *
223762306a36Sopenharmony_ci		 * The downside of disabling this is that transfer scheduling
223862306a36Sopenharmony_ci		 * gets VERY unfair for nonperiodic transfers; a misbehaving
223962306a36Sopenharmony_ci		 * peripheral could make that hurt.  That's perfectly normal
224062306a36Sopenharmony_ci		 * for reads from network or serial adapters ... so we have
224162306a36Sopenharmony_ci		 * partial NAKlimit support for bulk RX.
224262306a36Sopenharmony_ci		 *
224362306a36Sopenharmony_ci		 * The upside of disabling it is simpler transfer scheduling.
224462306a36Sopenharmony_ci		 */
224562306a36Sopenharmony_ci		interval = 0;
224662306a36Sopenharmony_ci	}
224762306a36Sopenharmony_ci	qh->intv_reg = interval;
224862306a36Sopenharmony_ci
224962306a36Sopenharmony_ci	/* precompute addressing for external hub/tt ports */
225062306a36Sopenharmony_ci	if (musb->is_multipoint) {
225162306a36Sopenharmony_ci		struct usb_device	*parent = urb->dev->parent;
225262306a36Sopenharmony_ci
225362306a36Sopenharmony_ci		if (parent != hcd->self.root_hub) {
225462306a36Sopenharmony_ci			qh->h_addr_reg = (u8) parent->devnum;
225562306a36Sopenharmony_ci
225662306a36Sopenharmony_ci			/* set up tt info if needed */
225762306a36Sopenharmony_ci			if (urb->dev->tt) {
225862306a36Sopenharmony_ci				qh->h_port_reg = (u8) urb->dev->ttport;
225962306a36Sopenharmony_ci				if (urb->dev->tt->hub)
226062306a36Sopenharmony_ci					qh->h_addr_reg =
226162306a36Sopenharmony_ci						(u8) urb->dev->tt->hub->devnum;
226262306a36Sopenharmony_ci				if (urb->dev->tt->multi)
226362306a36Sopenharmony_ci					qh->h_addr_reg |= 0x80;
226462306a36Sopenharmony_ci			}
226562306a36Sopenharmony_ci		}
226662306a36Sopenharmony_ci	}
226762306a36Sopenharmony_ci
226862306a36Sopenharmony_ci	/* invariant: hep->hcpriv is null OR the qh that's already scheduled.
226962306a36Sopenharmony_ci	 * until we get real dma queues (with an entry for each urb/buffer),
227062306a36Sopenharmony_ci	 * we only have work to do in the former case.
227162306a36Sopenharmony_ci	 */
227262306a36Sopenharmony_ci	spin_lock_irqsave(&musb->lock, flags);
227362306a36Sopenharmony_ci	if (hep->hcpriv || !next_urb(qh)) {
227462306a36Sopenharmony_ci		/* some concurrent activity submitted another urb to hep...
227562306a36Sopenharmony_ci		 * odd, rare, error prone, but legal.
227662306a36Sopenharmony_ci		 */
227762306a36Sopenharmony_ci		kfree(qh);
227862306a36Sopenharmony_ci		qh = NULL;
227962306a36Sopenharmony_ci		ret = 0;
228062306a36Sopenharmony_ci	} else
228162306a36Sopenharmony_ci		ret = musb_schedule(musb, qh,
228262306a36Sopenharmony_ci				epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
228362306a36Sopenharmony_ci
228462306a36Sopenharmony_ci	if (ret == 0) {
228562306a36Sopenharmony_ci		urb->hcpriv = qh;
228662306a36Sopenharmony_ci		/* FIXME set urb->start_frame for iso/intr, it's tested in
228762306a36Sopenharmony_ci		 * musb_start_urb(), but otherwise only konicawc cares ...
228862306a36Sopenharmony_ci		 */
228962306a36Sopenharmony_ci	}
229062306a36Sopenharmony_ci	spin_unlock_irqrestore(&musb->lock, flags);
229162306a36Sopenharmony_ci
229262306a36Sopenharmony_cidone:
229362306a36Sopenharmony_ci	if (ret != 0) {
229462306a36Sopenharmony_ci		spin_lock_irqsave(&musb->lock, flags);
229562306a36Sopenharmony_ci		usb_hcd_unlink_urb_from_ep(hcd, urb);
229662306a36Sopenharmony_ci		spin_unlock_irqrestore(&musb->lock, flags);
229762306a36Sopenharmony_ci		kfree(qh);
229862306a36Sopenharmony_ci	}
229962306a36Sopenharmony_ci	return ret;
230062306a36Sopenharmony_ci}
230162306a36Sopenharmony_ci
230262306a36Sopenharmony_ci
230362306a36Sopenharmony_ci/*
230462306a36Sopenharmony_ci * abort a transfer that's at the head of a hardware queue.
230562306a36Sopenharmony_ci * called with controller locked, irqs blocked
230662306a36Sopenharmony_ci * that hardware queue advances to the next transfer, unless prevented
230762306a36Sopenharmony_ci */
230862306a36Sopenharmony_cistatic int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
230962306a36Sopenharmony_ci{
231062306a36Sopenharmony_ci	struct musb_hw_ep	*ep = qh->hw_ep;
231162306a36Sopenharmony_ci	struct musb		*musb = ep->musb;
231262306a36Sopenharmony_ci	void __iomem		*epio = ep->regs;
231362306a36Sopenharmony_ci	unsigned		hw_end = ep->epnum;
231462306a36Sopenharmony_ci	void __iomem		*regs = ep->musb->mregs;
231562306a36Sopenharmony_ci	int			is_in = usb_pipein(urb->pipe);
231662306a36Sopenharmony_ci	int			status = 0;
231762306a36Sopenharmony_ci	u16			csr;
231862306a36Sopenharmony_ci	struct dma_channel	*dma = NULL;
231962306a36Sopenharmony_ci
232062306a36Sopenharmony_ci	musb_ep_select(regs, hw_end);
232162306a36Sopenharmony_ci
232262306a36Sopenharmony_ci	if (is_dma_capable()) {
232362306a36Sopenharmony_ci		dma = is_in ? ep->rx_channel : ep->tx_channel;
232462306a36Sopenharmony_ci		if (dma) {
232562306a36Sopenharmony_ci			status = ep->musb->dma_controller->channel_abort(dma);
232662306a36Sopenharmony_ci			musb_dbg(musb, "abort %cX%d DMA for urb %p --> %d",
232762306a36Sopenharmony_ci				is_in ? 'R' : 'T', ep->epnum,
232862306a36Sopenharmony_ci				urb, status);
232962306a36Sopenharmony_ci			urb->actual_length += dma->actual_len;
233062306a36Sopenharmony_ci		}
233162306a36Sopenharmony_ci	}
233262306a36Sopenharmony_ci
233362306a36Sopenharmony_ci	/* turn off DMA requests, discard state, stop polling ... */
233462306a36Sopenharmony_ci	if (ep->epnum && is_in) {
233562306a36Sopenharmony_ci		/* giveback saves bulk toggle */
233662306a36Sopenharmony_ci		csr = musb_h_flush_rxfifo(ep, 0);
233762306a36Sopenharmony_ci
233862306a36Sopenharmony_ci		/* clear the endpoint's irq status here to avoid bogus irqs */
233962306a36Sopenharmony_ci		if (is_dma_capable() && dma)
234062306a36Sopenharmony_ci			musb_platform_clear_ep_rxintr(musb, ep->epnum);
234162306a36Sopenharmony_ci	} else if (ep->epnum) {
234262306a36Sopenharmony_ci		musb_h_tx_flush_fifo(ep);
234362306a36Sopenharmony_ci		csr = musb_readw(epio, MUSB_TXCSR);
234462306a36Sopenharmony_ci		csr &= ~(MUSB_TXCSR_AUTOSET
234562306a36Sopenharmony_ci			| MUSB_TXCSR_DMAENAB
234662306a36Sopenharmony_ci			| MUSB_TXCSR_H_RXSTALL
234762306a36Sopenharmony_ci			| MUSB_TXCSR_H_NAKTIMEOUT
234862306a36Sopenharmony_ci			| MUSB_TXCSR_H_ERROR
234962306a36Sopenharmony_ci			| MUSB_TXCSR_TXPKTRDY);
235062306a36Sopenharmony_ci		musb_writew(epio, MUSB_TXCSR, csr);
235162306a36Sopenharmony_ci		/* REVISIT may need to clear FLUSHFIFO ... */
235262306a36Sopenharmony_ci		musb_writew(epio, MUSB_TXCSR, csr);
235362306a36Sopenharmony_ci		/* flush cpu writebuffer */
235462306a36Sopenharmony_ci		csr = musb_readw(epio, MUSB_TXCSR);
235562306a36Sopenharmony_ci	} else  {
235662306a36Sopenharmony_ci		musb_h_ep0_flush_fifo(ep);
235762306a36Sopenharmony_ci	}
235862306a36Sopenharmony_ci	if (status == 0)
235962306a36Sopenharmony_ci		musb_advance_schedule(ep->musb, urb, ep, is_in);
236062306a36Sopenharmony_ci	return status;
236162306a36Sopenharmony_ci}
236262306a36Sopenharmony_ci
236362306a36Sopenharmony_cistatic int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
236462306a36Sopenharmony_ci{
236562306a36Sopenharmony_ci	struct musb		*musb = hcd_to_musb(hcd);
236662306a36Sopenharmony_ci	struct musb_qh		*qh;
236762306a36Sopenharmony_ci	unsigned long		flags;
236862306a36Sopenharmony_ci	int			is_in  = usb_pipein(urb->pipe);
236962306a36Sopenharmony_ci	int			ret;
237062306a36Sopenharmony_ci
237162306a36Sopenharmony_ci	trace_musb_urb_deq(musb, urb);
237262306a36Sopenharmony_ci
237362306a36Sopenharmony_ci	spin_lock_irqsave(&musb->lock, flags);
237462306a36Sopenharmony_ci	ret = usb_hcd_check_unlink_urb(hcd, urb, status);
237562306a36Sopenharmony_ci	if (ret)
237662306a36Sopenharmony_ci		goto done;
237762306a36Sopenharmony_ci
237862306a36Sopenharmony_ci	qh = urb->hcpriv;
237962306a36Sopenharmony_ci	if (!qh)
238062306a36Sopenharmony_ci		goto done;
238162306a36Sopenharmony_ci
238262306a36Sopenharmony_ci	/*
238362306a36Sopenharmony_ci	 * Any URB not actively programmed into endpoint hardware can be
238462306a36Sopenharmony_ci	 * immediately given back; that's any URB not at the head of an
238562306a36Sopenharmony_ci	 * endpoint queue, unless someday we get real DMA queues.  And even
238662306a36Sopenharmony_ci	 * if it's at the head, it might not be known to the hardware...
238762306a36Sopenharmony_ci	 *
238862306a36Sopenharmony_ci	 * Otherwise abort current transfer, pending DMA, etc.; urb->status
238962306a36Sopenharmony_ci	 * has already been updated.  This is a synchronous abort; it'd be
239062306a36Sopenharmony_ci	 * OK to hold off until after some IRQ, though.
239162306a36Sopenharmony_ci	 *
239262306a36Sopenharmony_ci	 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
239362306a36Sopenharmony_ci	 */
239462306a36Sopenharmony_ci	if (!qh->is_ready
239562306a36Sopenharmony_ci			|| urb->urb_list.prev != &qh->hep->urb_list
239662306a36Sopenharmony_ci			|| musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
239762306a36Sopenharmony_ci		int	ready = qh->is_ready;
239862306a36Sopenharmony_ci
239962306a36Sopenharmony_ci		qh->is_ready = 0;
240062306a36Sopenharmony_ci		musb_giveback(musb, urb, 0);
240162306a36Sopenharmony_ci		qh->is_ready = ready;
240262306a36Sopenharmony_ci
240362306a36Sopenharmony_ci		/* If nothing else (usually musb_giveback) is using it
240462306a36Sopenharmony_ci		 * and its URB list has emptied, recycle this qh.
240562306a36Sopenharmony_ci		 */
240662306a36Sopenharmony_ci		if (ready && list_empty(&qh->hep->urb_list)) {
240762306a36Sopenharmony_ci			musb_ep_set_qh(qh->hw_ep, is_in, NULL);
240862306a36Sopenharmony_ci			qh->hep->hcpriv = NULL;
240962306a36Sopenharmony_ci			list_del(&qh->ring);
241062306a36Sopenharmony_ci			kfree(qh);
241162306a36Sopenharmony_ci		}
241262306a36Sopenharmony_ci	} else
241362306a36Sopenharmony_ci		ret = musb_cleanup_urb(urb, qh);
241462306a36Sopenharmony_cidone:
241562306a36Sopenharmony_ci	spin_unlock_irqrestore(&musb->lock, flags);
241662306a36Sopenharmony_ci	return ret;
241762306a36Sopenharmony_ci}
241862306a36Sopenharmony_ci
241962306a36Sopenharmony_ci/* disable an endpoint */
242062306a36Sopenharmony_cistatic void
242162306a36Sopenharmony_cimusb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
242262306a36Sopenharmony_ci{
242362306a36Sopenharmony_ci	u8			is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
242462306a36Sopenharmony_ci	unsigned long		flags;
242562306a36Sopenharmony_ci	struct musb		*musb = hcd_to_musb(hcd);
242662306a36Sopenharmony_ci	struct musb_qh		*qh;
242762306a36Sopenharmony_ci	struct urb		*urb;
242862306a36Sopenharmony_ci
242962306a36Sopenharmony_ci	spin_lock_irqsave(&musb->lock, flags);
243062306a36Sopenharmony_ci
243162306a36Sopenharmony_ci	qh = hep->hcpriv;
243262306a36Sopenharmony_ci	if (qh == NULL)
243362306a36Sopenharmony_ci		goto exit;
243462306a36Sopenharmony_ci
243562306a36Sopenharmony_ci	/* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
243662306a36Sopenharmony_ci
243762306a36Sopenharmony_ci	/* Kick the first URB off the hardware, if needed */
243862306a36Sopenharmony_ci	qh->is_ready = 0;
243962306a36Sopenharmony_ci	if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
244062306a36Sopenharmony_ci		urb = next_urb(qh);
244162306a36Sopenharmony_ci
244262306a36Sopenharmony_ci		/* make software (then hardware) stop ASAP */
244362306a36Sopenharmony_ci		if (!urb->unlinked)
244462306a36Sopenharmony_ci			urb->status = -ESHUTDOWN;
244562306a36Sopenharmony_ci
244662306a36Sopenharmony_ci		/* cleanup */
244762306a36Sopenharmony_ci		musb_cleanup_urb(urb, qh);
244862306a36Sopenharmony_ci
244962306a36Sopenharmony_ci		/* Then nuke all the others ... and advance the
245062306a36Sopenharmony_ci		 * queue on hw_ep (e.g. bulk ring) when we're done.
245162306a36Sopenharmony_ci		 */
245262306a36Sopenharmony_ci		while (!list_empty(&hep->urb_list)) {
245362306a36Sopenharmony_ci			urb = next_urb(qh);
245462306a36Sopenharmony_ci			urb->status = -ESHUTDOWN;
245562306a36Sopenharmony_ci			musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
245662306a36Sopenharmony_ci		}
245762306a36Sopenharmony_ci	} else {
245862306a36Sopenharmony_ci		/* Just empty the queue; the hardware is busy with
245962306a36Sopenharmony_ci		 * other transfers, and since !qh->is_ready nothing
246062306a36Sopenharmony_ci		 * will activate any of these as it advances.
246162306a36Sopenharmony_ci		 */
246262306a36Sopenharmony_ci		while (!list_empty(&hep->urb_list))
246362306a36Sopenharmony_ci			musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
246462306a36Sopenharmony_ci
246562306a36Sopenharmony_ci		hep->hcpriv = NULL;
246662306a36Sopenharmony_ci		list_del(&qh->ring);
246762306a36Sopenharmony_ci		kfree(qh);
246862306a36Sopenharmony_ci	}
246962306a36Sopenharmony_ciexit:
247062306a36Sopenharmony_ci	spin_unlock_irqrestore(&musb->lock, flags);
247162306a36Sopenharmony_ci}
247262306a36Sopenharmony_ci
247362306a36Sopenharmony_cistatic int musb_h_get_frame_number(struct usb_hcd *hcd)
247462306a36Sopenharmony_ci{
247562306a36Sopenharmony_ci	struct musb	*musb = hcd_to_musb(hcd);
247662306a36Sopenharmony_ci
247762306a36Sopenharmony_ci	return musb_readw(musb->mregs, MUSB_FRAME);
247862306a36Sopenharmony_ci}
247962306a36Sopenharmony_ci
248062306a36Sopenharmony_cistatic int musb_h_start(struct usb_hcd *hcd)
248162306a36Sopenharmony_ci{
248262306a36Sopenharmony_ci	struct musb	*musb = hcd_to_musb(hcd);
248362306a36Sopenharmony_ci
248462306a36Sopenharmony_ci	/* NOTE: musb_start() is called when the hub driver turns
248562306a36Sopenharmony_ci	 * on port power, or when (OTG) peripheral starts.
248662306a36Sopenharmony_ci	 */
248762306a36Sopenharmony_ci	hcd->state = HC_STATE_RUNNING;
248862306a36Sopenharmony_ci	musb->port1_status = 0;
248962306a36Sopenharmony_ci	return 0;
249062306a36Sopenharmony_ci}
249162306a36Sopenharmony_ci
249262306a36Sopenharmony_cistatic void musb_h_stop(struct usb_hcd *hcd)
249362306a36Sopenharmony_ci{
249462306a36Sopenharmony_ci	musb_stop(hcd_to_musb(hcd));
249562306a36Sopenharmony_ci	hcd->state = HC_STATE_HALT;
249662306a36Sopenharmony_ci}
249762306a36Sopenharmony_ci
249862306a36Sopenharmony_cistatic int musb_bus_suspend(struct usb_hcd *hcd)
249962306a36Sopenharmony_ci{
250062306a36Sopenharmony_ci	struct musb	*musb = hcd_to_musb(hcd);
250162306a36Sopenharmony_ci	u8		devctl;
250262306a36Sopenharmony_ci	int		ret;
250362306a36Sopenharmony_ci
250462306a36Sopenharmony_ci	ret = musb_port_suspend(musb, true);
250562306a36Sopenharmony_ci	if (ret)
250662306a36Sopenharmony_ci		return ret;
250762306a36Sopenharmony_ci
250862306a36Sopenharmony_ci	if (!is_host_active(musb))
250962306a36Sopenharmony_ci		return 0;
251062306a36Sopenharmony_ci
251162306a36Sopenharmony_ci	switch (musb_get_state(musb)) {
251262306a36Sopenharmony_ci	case OTG_STATE_A_SUSPEND:
251362306a36Sopenharmony_ci		return 0;
251462306a36Sopenharmony_ci	case OTG_STATE_A_WAIT_VRISE:
251562306a36Sopenharmony_ci		/* ID could be grounded even if there's no device
251662306a36Sopenharmony_ci		 * on the other end of the cable.  NOTE that the
251762306a36Sopenharmony_ci		 * A_WAIT_VRISE timers are messy with MUSB...
251862306a36Sopenharmony_ci		 */
251962306a36Sopenharmony_ci		devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
252062306a36Sopenharmony_ci		if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
252162306a36Sopenharmony_ci			musb_set_state(musb, OTG_STATE_A_WAIT_BCON);
252262306a36Sopenharmony_ci		break;
252362306a36Sopenharmony_ci	default:
252462306a36Sopenharmony_ci		break;
252562306a36Sopenharmony_ci	}
252662306a36Sopenharmony_ci
252762306a36Sopenharmony_ci	if (musb->is_active) {
252862306a36Sopenharmony_ci		WARNING("trying to suspend as %s while active\n",
252962306a36Sopenharmony_ci			musb_otg_state_string(musb));
253062306a36Sopenharmony_ci		return -EBUSY;
253162306a36Sopenharmony_ci	} else
253262306a36Sopenharmony_ci		return 0;
253362306a36Sopenharmony_ci}
253462306a36Sopenharmony_ci
253562306a36Sopenharmony_cistatic int musb_bus_resume(struct usb_hcd *hcd)
253662306a36Sopenharmony_ci{
253762306a36Sopenharmony_ci	struct musb *musb = hcd_to_musb(hcd);
253862306a36Sopenharmony_ci
253962306a36Sopenharmony_ci	if (musb->config &&
254062306a36Sopenharmony_ci	    musb->config->host_port_deassert_reset_at_resume)
254162306a36Sopenharmony_ci		musb_port_reset(musb, false);
254262306a36Sopenharmony_ci
254362306a36Sopenharmony_ci	return 0;
254462306a36Sopenharmony_ci}
254562306a36Sopenharmony_ci
254662306a36Sopenharmony_ci#ifndef CONFIG_MUSB_PIO_ONLY
254762306a36Sopenharmony_ci
254862306a36Sopenharmony_ci#define MUSB_USB_DMA_ALIGN 4
254962306a36Sopenharmony_ci
255062306a36Sopenharmony_cistruct musb_temp_buffer {
255162306a36Sopenharmony_ci	void *kmalloc_ptr;
255262306a36Sopenharmony_ci	void *old_xfer_buffer;
255362306a36Sopenharmony_ci	u8 data[];
255462306a36Sopenharmony_ci};
255562306a36Sopenharmony_ci
255662306a36Sopenharmony_cistatic void musb_free_temp_buffer(struct urb *urb)
255762306a36Sopenharmony_ci{
255862306a36Sopenharmony_ci	enum dma_data_direction dir;
255962306a36Sopenharmony_ci	struct musb_temp_buffer *temp;
256062306a36Sopenharmony_ci	size_t length;
256162306a36Sopenharmony_ci
256262306a36Sopenharmony_ci	if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
256362306a36Sopenharmony_ci		return;
256462306a36Sopenharmony_ci
256562306a36Sopenharmony_ci	dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
256662306a36Sopenharmony_ci
256762306a36Sopenharmony_ci	temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
256862306a36Sopenharmony_ci			    data);
256962306a36Sopenharmony_ci
257062306a36Sopenharmony_ci	if (dir == DMA_FROM_DEVICE) {
257162306a36Sopenharmony_ci		if (usb_pipeisoc(urb->pipe))
257262306a36Sopenharmony_ci			length = urb->transfer_buffer_length;
257362306a36Sopenharmony_ci		else
257462306a36Sopenharmony_ci			length = urb->actual_length;
257562306a36Sopenharmony_ci
257662306a36Sopenharmony_ci		memcpy(temp->old_xfer_buffer, temp->data, length);
257762306a36Sopenharmony_ci	}
257862306a36Sopenharmony_ci	urb->transfer_buffer = temp->old_xfer_buffer;
257962306a36Sopenharmony_ci	kfree(temp->kmalloc_ptr);
258062306a36Sopenharmony_ci
258162306a36Sopenharmony_ci	urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
258262306a36Sopenharmony_ci}
258362306a36Sopenharmony_ci
258462306a36Sopenharmony_cistatic int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
258562306a36Sopenharmony_ci{
258662306a36Sopenharmony_ci	enum dma_data_direction dir;
258762306a36Sopenharmony_ci	struct musb_temp_buffer *temp;
258862306a36Sopenharmony_ci	void *kmalloc_ptr;
258962306a36Sopenharmony_ci	size_t kmalloc_size;
259062306a36Sopenharmony_ci
259162306a36Sopenharmony_ci	if (urb->num_sgs || urb->sg ||
259262306a36Sopenharmony_ci	    urb->transfer_buffer_length == 0 ||
259362306a36Sopenharmony_ci	    !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
259462306a36Sopenharmony_ci		return 0;
259562306a36Sopenharmony_ci
259662306a36Sopenharmony_ci	dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
259762306a36Sopenharmony_ci
259862306a36Sopenharmony_ci	/* Allocate a buffer with enough padding for alignment */
259962306a36Sopenharmony_ci	kmalloc_size = urb->transfer_buffer_length +
260062306a36Sopenharmony_ci		sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
260162306a36Sopenharmony_ci
260262306a36Sopenharmony_ci	kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
260362306a36Sopenharmony_ci	if (!kmalloc_ptr)
260462306a36Sopenharmony_ci		return -ENOMEM;
260562306a36Sopenharmony_ci
260662306a36Sopenharmony_ci	/* Position our struct temp_buffer such that data is aligned */
260762306a36Sopenharmony_ci	temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
260862306a36Sopenharmony_ci
260962306a36Sopenharmony_ci
261062306a36Sopenharmony_ci	temp->kmalloc_ptr = kmalloc_ptr;
261162306a36Sopenharmony_ci	temp->old_xfer_buffer = urb->transfer_buffer;
261262306a36Sopenharmony_ci	if (dir == DMA_TO_DEVICE)
261362306a36Sopenharmony_ci		memcpy(temp->data, urb->transfer_buffer,
261462306a36Sopenharmony_ci		       urb->transfer_buffer_length);
261562306a36Sopenharmony_ci	urb->transfer_buffer = temp->data;
261662306a36Sopenharmony_ci
261762306a36Sopenharmony_ci	urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
261862306a36Sopenharmony_ci
261962306a36Sopenharmony_ci	return 0;
262062306a36Sopenharmony_ci}
262162306a36Sopenharmony_ci
262262306a36Sopenharmony_cistatic int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
262362306a36Sopenharmony_ci				      gfp_t mem_flags)
262462306a36Sopenharmony_ci{
262562306a36Sopenharmony_ci	struct musb	*musb = hcd_to_musb(hcd);
262662306a36Sopenharmony_ci	int ret;
262762306a36Sopenharmony_ci
262862306a36Sopenharmony_ci	/*
262962306a36Sopenharmony_ci	 * The DMA engine in RTL1.8 and above cannot handle
263062306a36Sopenharmony_ci	 * DMA addresses that are not aligned to a 4 byte boundary.
263162306a36Sopenharmony_ci	 * For such engine implemented (un)map_urb_for_dma hooks.
263262306a36Sopenharmony_ci	 * Do not use these hooks for RTL<1.8
263362306a36Sopenharmony_ci	 */
263462306a36Sopenharmony_ci	if (musb->hwvers < MUSB_HWVERS_1800)
263562306a36Sopenharmony_ci		return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
263662306a36Sopenharmony_ci
263762306a36Sopenharmony_ci	ret = musb_alloc_temp_buffer(urb, mem_flags);
263862306a36Sopenharmony_ci	if (ret)
263962306a36Sopenharmony_ci		return ret;
264062306a36Sopenharmony_ci
264162306a36Sopenharmony_ci	ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
264262306a36Sopenharmony_ci	if (ret)
264362306a36Sopenharmony_ci		musb_free_temp_buffer(urb);
264462306a36Sopenharmony_ci
264562306a36Sopenharmony_ci	return ret;
264662306a36Sopenharmony_ci}
264762306a36Sopenharmony_ci
264862306a36Sopenharmony_cistatic void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
264962306a36Sopenharmony_ci{
265062306a36Sopenharmony_ci	struct musb	*musb = hcd_to_musb(hcd);
265162306a36Sopenharmony_ci
265262306a36Sopenharmony_ci	usb_hcd_unmap_urb_for_dma(hcd, urb);
265362306a36Sopenharmony_ci
265462306a36Sopenharmony_ci	/* Do not use this hook for RTL<1.8 (see description above) */
265562306a36Sopenharmony_ci	if (musb->hwvers < MUSB_HWVERS_1800)
265662306a36Sopenharmony_ci		return;
265762306a36Sopenharmony_ci
265862306a36Sopenharmony_ci	musb_free_temp_buffer(urb);
265962306a36Sopenharmony_ci}
266062306a36Sopenharmony_ci#endif /* !CONFIG_MUSB_PIO_ONLY */
266162306a36Sopenharmony_ci
266262306a36Sopenharmony_cistatic const struct hc_driver musb_hc_driver = {
266362306a36Sopenharmony_ci	.description		= "musb-hcd",
266462306a36Sopenharmony_ci	.product_desc		= "MUSB HDRC host driver",
266562306a36Sopenharmony_ci	.hcd_priv_size		= sizeof(struct musb *),
266662306a36Sopenharmony_ci	.flags			= HCD_USB2 | HCD_DMA | HCD_MEMORY,
266762306a36Sopenharmony_ci
266862306a36Sopenharmony_ci	/* not using irq handler or reset hooks from usbcore, since
266962306a36Sopenharmony_ci	 * those must be shared with peripheral code for OTG configs
267062306a36Sopenharmony_ci	 */
267162306a36Sopenharmony_ci
267262306a36Sopenharmony_ci	.start			= musb_h_start,
267362306a36Sopenharmony_ci	.stop			= musb_h_stop,
267462306a36Sopenharmony_ci
267562306a36Sopenharmony_ci	.get_frame_number	= musb_h_get_frame_number,
267662306a36Sopenharmony_ci
267762306a36Sopenharmony_ci	.urb_enqueue		= musb_urb_enqueue,
267862306a36Sopenharmony_ci	.urb_dequeue		= musb_urb_dequeue,
267962306a36Sopenharmony_ci	.endpoint_disable	= musb_h_disable,
268062306a36Sopenharmony_ci
268162306a36Sopenharmony_ci#ifndef CONFIG_MUSB_PIO_ONLY
268262306a36Sopenharmony_ci	.map_urb_for_dma	= musb_map_urb_for_dma,
268362306a36Sopenharmony_ci	.unmap_urb_for_dma	= musb_unmap_urb_for_dma,
268462306a36Sopenharmony_ci#endif
268562306a36Sopenharmony_ci
268662306a36Sopenharmony_ci	.hub_status_data	= musb_hub_status_data,
268762306a36Sopenharmony_ci	.hub_control		= musb_hub_control,
268862306a36Sopenharmony_ci	.bus_suspend		= musb_bus_suspend,
268962306a36Sopenharmony_ci	.bus_resume		= musb_bus_resume,
269062306a36Sopenharmony_ci	/* .start_port_reset	= NULL, */
269162306a36Sopenharmony_ci	/* .hub_irq_enable	= NULL, */
269262306a36Sopenharmony_ci};
269362306a36Sopenharmony_ci
269462306a36Sopenharmony_ciint musb_host_alloc(struct musb *musb)
269562306a36Sopenharmony_ci{
269662306a36Sopenharmony_ci	struct device	*dev = musb->controller;
269762306a36Sopenharmony_ci
269862306a36Sopenharmony_ci	/* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
269962306a36Sopenharmony_ci	musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
270062306a36Sopenharmony_ci	if (!musb->hcd)
270162306a36Sopenharmony_ci		return -EINVAL;
270262306a36Sopenharmony_ci
270362306a36Sopenharmony_ci	*musb->hcd->hcd_priv = (unsigned long) musb;
270462306a36Sopenharmony_ci	musb->hcd->self.uses_pio_for_control = 1;
270562306a36Sopenharmony_ci	musb->hcd->uses_new_polling = 1;
270662306a36Sopenharmony_ci	musb->hcd->has_tt = 1;
270762306a36Sopenharmony_ci
270862306a36Sopenharmony_ci	return 0;
270962306a36Sopenharmony_ci}
271062306a36Sopenharmony_ci
271162306a36Sopenharmony_civoid musb_host_cleanup(struct musb *musb)
271262306a36Sopenharmony_ci{
271362306a36Sopenharmony_ci	if (musb->port_mode == MUSB_PERIPHERAL)
271462306a36Sopenharmony_ci		return;
271562306a36Sopenharmony_ci	usb_remove_hcd(musb->hcd);
271662306a36Sopenharmony_ci}
271762306a36Sopenharmony_ci
271862306a36Sopenharmony_civoid musb_host_free(struct musb *musb)
271962306a36Sopenharmony_ci{
272062306a36Sopenharmony_ci	usb_put_hcd(musb->hcd);
272162306a36Sopenharmony_ci}
272262306a36Sopenharmony_ci
272362306a36Sopenharmony_ciint musb_host_setup(struct musb *musb, int power_budget)
272462306a36Sopenharmony_ci{
272562306a36Sopenharmony_ci	int ret;
272662306a36Sopenharmony_ci	struct usb_hcd *hcd = musb->hcd;
272762306a36Sopenharmony_ci
272862306a36Sopenharmony_ci	if (musb->port_mode == MUSB_HOST) {
272962306a36Sopenharmony_ci		MUSB_HST_MODE(musb);
273062306a36Sopenharmony_ci		musb_set_state(musb, OTG_STATE_A_IDLE);
273162306a36Sopenharmony_ci	}
273262306a36Sopenharmony_ci
273362306a36Sopenharmony_ci	if (musb->xceiv) {
273462306a36Sopenharmony_ci		otg_set_host(musb->xceiv->otg, &hcd->self);
273562306a36Sopenharmony_ci		musb->xceiv->otg->host = &hcd->self;
273662306a36Sopenharmony_ci	} else {
273762306a36Sopenharmony_ci		phy_set_mode(musb->phy, PHY_MODE_USB_HOST);
273862306a36Sopenharmony_ci	}
273962306a36Sopenharmony_ci
274062306a36Sopenharmony_ci	/* don't support otg protocols */
274162306a36Sopenharmony_ci	hcd->self.otg_port = 0;
274262306a36Sopenharmony_ci	hcd->power_budget = 2 * (power_budget ? : 250);
274362306a36Sopenharmony_ci	hcd->skip_phy_initialization = 1;
274462306a36Sopenharmony_ci
274562306a36Sopenharmony_ci	ret = usb_add_hcd(hcd, 0, 0);
274662306a36Sopenharmony_ci	if (ret < 0)
274762306a36Sopenharmony_ci		return ret;
274862306a36Sopenharmony_ci
274962306a36Sopenharmony_ci	device_wakeup_enable(hcd->self.controller);
275062306a36Sopenharmony_ci	return 0;
275162306a36Sopenharmony_ci}
275262306a36Sopenharmony_ci
275362306a36Sopenharmony_civoid musb_host_resume_root_hub(struct musb *musb)
275462306a36Sopenharmony_ci{
275562306a36Sopenharmony_ci	usb_hcd_resume_root_hub(musb->hcd);
275662306a36Sopenharmony_ci}
275762306a36Sopenharmony_ci
275862306a36Sopenharmony_civoid musb_host_poke_root_hub(struct musb *musb)
275962306a36Sopenharmony_ci{
276062306a36Sopenharmony_ci	MUSB_HST_MODE(musb);
276162306a36Sopenharmony_ci	if (musb->hcd->status_urb)
276262306a36Sopenharmony_ci		usb_hcd_poll_rh_status(musb->hcd);
276362306a36Sopenharmony_ci	else
276462306a36Sopenharmony_ci		usb_hcd_resume_root_hub(musb->hcd);
276562306a36Sopenharmony_ci}
2766