xref: /linux/drivers/usb/dwc2/gadget.c (revision c524dd5f432a0690710b62e729a3673c557d8b58)
147a1685fSDinh Nguyen /**
247a1685fSDinh Nguyen  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
347a1685fSDinh Nguyen  *		http://www.samsung.com
447a1685fSDinh Nguyen  *
547a1685fSDinh Nguyen  * Copyright 2008 Openmoko, Inc.
647a1685fSDinh Nguyen  * Copyright 2008 Simtec Electronics
747a1685fSDinh Nguyen  *      Ben Dooks <ben@simtec.co.uk>
847a1685fSDinh Nguyen  *      http://armlinux.simtec.co.uk/
947a1685fSDinh Nguyen  *
1047a1685fSDinh Nguyen  * S3C USB2.0 High-speed / OtG driver
1147a1685fSDinh Nguyen  *
1247a1685fSDinh Nguyen  * This program is free software; you can redistribute it and/or modify
1347a1685fSDinh Nguyen  * it under the terms of the GNU General Public License version 2 as
1447a1685fSDinh Nguyen  * published by the Free Software Foundation.
1547a1685fSDinh Nguyen  */
1647a1685fSDinh Nguyen 
1747a1685fSDinh Nguyen #include <linux/kernel.h>
1847a1685fSDinh Nguyen #include <linux/module.h>
1947a1685fSDinh Nguyen #include <linux/spinlock.h>
2047a1685fSDinh Nguyen #include <linux/interrupt.h>
2147a1685fSDinh Nguyen #include <linux/platform_device.h>
2247a1685fSDinh Nguyen #include <linux/dma-mapping.h>
237ad8096eSMarek Szyprowski #include <linux/mutex.h>
2447a1685fSDinh Nguyen #include <linux/seq_file.h>
2547a1685fSDinh Nguyen #include <linux/delay.h>
2647a1685fSDinh Nguyen #include <linux/io.h>
2747a1685fSDinh Nguyen #include <linux/slab.h>
2847a1685fSDinh Nguyen #include <linux/clk.h>
2947a1685fSDinh Nguyen #include <linux/regulator/consumer.h>
3047a1685fSDinh Nguyen #include <linux/of_platform.h>
3147a1685fSDinh Nguyen #include <linux/phy/phy.h>
3247a1685fSDinh Nguyen 
3347a1685fSDinh Nguyen #include <linux/usb/ch9.h>
3447a1685fSDinh Nguyen #include <linux/usb/gadget.h>
3547a1685fSDinh Nguyen #include <linux/usb/phy.h>
3647a1685fSDinh Nguyen #include <linux/platform_data/s3c-hsotg.h>
3747a1685fSDinh Nguyen 
38f7c0b143SDinh Nguyen #include "core.h"
39941fcce4SDinh Nguyen #include "hw.h"
4047a1685fSDinh Nguyen 
4147a1685fSDinh Nguyen /* conversion functions */
421f91b4ccSFelipe Balbi static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
4347a1685fSDinh Nguyen {
441f91b4ccSFelipe Balbi 	return container_of(req, struct dwc2_hsotg_req, req);
4547a1685fSDinh Nguyen }
4647a1685fSDinh Nguyen 
471f91b4ccSFelipe Balbi static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
4847a1685fSDinh Nguyen {
491f91b4ccSFelipe Balbi 	return container_of(ep, struct dwc2_hsotg_ep, ep);
5047a1685fSDinh Nguyen }
5147a1685fSDinh Nguyen 
52941fcce4SDinh Nguyen static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
5347a1685fSDinh Nguyen {
54941fcce4SDinh Nguyen 	return container_of(gadget, struct dwc2_hsotg, gadget);
5547a1685fSDinh Nguyen }
5647a1685fSDinh Nguyen 
5747a1685fSDinh Nguyen static inline void __orr32(void __iomem *ptr, u32 val)
5847a1685fSDinh Nguyen {
5995c8bc36SAntti Seppälä 	dwc2_writel(dwc2_readl(ptr) | val, ptr);
6047a1685fSDinh Nguyen }
6147a1685fSDinh Nguyen 
6247a1685fSDinh Nguyen static inline void __bic32(void __iomem *ptr, u32 val)
6347a1685fSDinh Nguyen {
6495c8bc36SAntti Seppälä 	dwc2_writel(dwc2_readl(ptr) & ~val, ptr);
6547a1685fSDinh Nguyen }
6647a1685fSDinh Nguyen 
671f91b4ccSFelipe Balbi static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
68c6f5c050SMian Yousaf Kaukab 						u32 ep_index, u32 dir_in)
69c6f5c050SMian Yousaf Kaukab {
70c6f5c050SMian Yousaf Kaukab 	if (dir_in)
71c6f5c050SMian Yousaf Kaukab 		return hsotg->eps_in[ep_index];
72c6f5c050SMian Yousaf Kaukab 	else
73c6f5c050SMian Yousaf Kaukab 		return hsotg->eps_out[ep_index];
74c6f5c050SMian Yousaf Kaukab }
75c6f5c050SMian Yousaf Kaukab 
76997f4f81SMickael Maison /* forward declaration of functions */
771f91b4ccSFelipe Balbi static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
7847a1685fSDinh Nguyen 
7947a1685fSDinh Nguyen /**
8047a1685fSDinh Nguyen  * using_dma - return the DMA status of the driver.
8147a1685fSDinh Nguyen  * @hsotg: The driver state.
8247a1685fSDinh Nguyen  *
8347a1685fSDinh Nguyen  * Return true if we're using DMA.
8447a1685fSDinh Nguyen  *
8547a1685fSDinh Nguyen  * Currently, we have the DMA support code worked into everywhere
8647a1685fSDinh Nguyen  * that needs it, but the AMBA DMA implementation in the hardware can
8747a1685fSDinh Nguyen  * only DMA from 32bit aligned addresses. This means that gadgets such
8847a1685fSDinh Nguyen  * as the CDC Ethernet cannot work as they often pass packets which are
8947a1685fSDinh Nguyen  * not 32bit aligned.
9047a1685fSDinh Nguyen  *
9147a1685fSDinh Nguyen  * Unfortunately the choice to use DMA or not is global to the controller
9247a1685fSDinh Nguyen  * and seems to be only settable when the controller is being put through
9347a1685fSDinh Nguyen  * a core reset. This means we either need to fix the gadgets to take
9447a1685fSDinh Nguyen  * account of DMA alignment, or add bounce buffers (yuerk).
9547a1685fSDinh Nguyen  *
96edd74be8SGregory Herrero  * g_using_dma is set depending on dts flag.
9747a1685fSDinh Nguyen  */
98941fcce4SDinh Nguyen static inline bool using_dma(struct dwc2_hsotg *hsotg)
9947a1685fSDinh Nguyen {
100edd74be8SGregory Herrero 	return hsotg->g_using_dma;
10147a1685fSDinh Nguyen }
10247a1685fSDinh Nguyen 
10347a1685fSDinh Nguyen /**
1041f91b4ccSFelipe Balbi  * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
10547a1685fSDinh Nguyen  * @hsotg: The device state
10647a1685fSDinh Nguyen  * @ints: A bitmask of the interrupts to enable
10747a1685fSDinh Nguyen  */
1081f91b4ccSFelipe Balbi static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
10947a1685fSDinh Nguyen {
11095c8bc36SAntti Seppälä 	u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
11147a1685fSDinh Nguyen 	u32 new_gsintmsk;
11247a1685fSDinh Nguyen 
11347a1685fSDinh Nguyen 	new_gsintmsk = gsintmsk | ints;
11447a1685fSDinh Nguyen 
11547a1685fSDinh Nguyen 	if (new_gsintmsk != gsintmsk) {
11647a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
11795c8bc36SAntti Seppälä 		dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
11847a1685fSDinh Nguyen 	}
11947a1685fSDinh Nguyen }
12047a1685fSDinh Nguyen 
12147a1685fSDinh Nguyen /**
1221f91b4ccSFelipe Balbi  * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
12347a1685fSDinh Nguyen  * @hsotg: The device state
12447a1685fSDinh Nguyen  * @ints: A bitmask of the interrupts to enable
12547a1685fSDinh Nguyen  */
1261f91b4ccSFelipe Balbi static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
12747a1685fSDinh Nguyen {
12895c8bc36SAntti Seppälä 	u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
12947a1685fSDinh Nguyen 	u32 new_gsintmsk;
13047a1685fSDinh Nguyen 
13147a1685fSDinh Nguyen 	new_gsintmsk = gsintmsk & ~ints;
13247a1685fSDinh Nguyen 
13347a1685fSDinh Nguyen 	if (new_gsintmsk != gsintmsk)
13495c8bc36SAntti Seppälä 		dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
13547a1685fSDinh Nguyen }
13647a1685fSDinh Nguyen 
13747a1685fSDinh Nguyen /**
1381f91b4ccSFelipe Balbi  * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
13947a1685fSDinh Nguyen  * @hsotg: The device state
14047a1685fSDinh Nguyen  * @ep: The endpoint index
14147a1685fSDinh Nguyen  * @dir_in: True if direction is in.
14247a1685fSDinh Nguyen  * @en: The enable value, true to enable
14347a1685fSDinh Nguyen  *
14447a1685fSDinh Nguyen  * Set or clear the mask for an individual endpoint's interrupt
14547a1685fSDinh Nguyen  * request.
14647a1685fSDinh Nguyen  */
1471f91b4ccSFelipe Balbi static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
14847a1685fSDinh Nguyen 				 unsigned int ep, unsigned int dir_in,
14947a1685fSDinh Nguyen 				 unsigned int en)
15047a1685fSDinh Nguyen {
15147a1685fSDinh Nguyen 	unsigned long flags;
15247a1685fSDinh Nguyen 	u32 bit = 1 << ep;
15347a1685fSDinh Nguyen 	u32 daint;
15447a1685fSDinh Nguyen 
15547a1685fSDinh Nguyen 	if (!dir_in)
15647a1685fSDinh Nguyen 		bit <<= 16;
15747a1685fSDinh Nguyen 
15847a1685fSDinh Nguyen 	local_irq_save(flags);
15995c8bc36SAntti Seppälä 	daint = dwc2_readl(hsotg->regs + DAINTMSK);
16047a1685fSDinh Nguyen 	if (en)
16147a1685fSDinh Nguyen 		daint |= bit;
16247a1685fSDinh Nguyen 	else
16347a1685fSDinh Nguyen 		daint &= ~bit;
16495c8bc36SAntti Seppälä 	dwc2_writel(daint, hsotg->regs + DAINTMSK);
16547a1685fSDinh Nguyen 	local_irq_restore(flags);
16647a1685fSDinh Nguyen }
16747a1685fSDinh Nguyen 
16847a1685fSDinh Nguyen /**
1691f91b4ccSFelipe Balbi  * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
17047a1685fSDinh Nguyen  * @hsotg: The device instance.
17147a1685fSDinh Nguyen  */
1721f91b4ccSFelipe Balbi static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
17347a1685fSDinh Nguyen {
17447a1685fSDinh Nguyen 	unsigned int ep;
17547a1685fSDinh Nguyen 	unsigned int addr;
17647a1685fSDinh Nguyen 	int timeout;
17747a1685fSDinh Nguyen 	u32 val;
17847a1685fSDinh Nguyen 
1797fcbc95cSGregory Herrero 	/* Reset fifo map if not correctly cleared during previous session */
1807fcbc95cSGregory Herrero 	WARN_ON(hsotg->fifo_map);
1817fcbc95cSGregory Herrero 	hsotg->fifo_map = 0;
1827fcbc95cSGregory Herrero 
1830a176279SGregory Herrero 	/* set RX/NPTX FIFO sizes */
18495c8bc36SAntti Seppälä 	dwc2_writel(hsotg->g_rx_fifo_sz, hsotg->regs + GRXFSIZ);
18595c8bc36SAntti Seppälä 	dwc2_writel((hsotg->g_rx_fifo_sz << FIFOSIZE_STARTADDR_SHIFT) |
1860a176279SGregory Herrero 		(hsotg->g_np_g_tx_fifo_sz << FIFOSIZE_DEPTH_SHIFT),
1870a176279SGregory Herrero 		hsotg->regs + GNPTXFSIZ);
18847a1685fSDinh Nguyen 
18947a1685fSDinh Nguyen 	/*
19047a1685fSDinh Nguyen 	 * arange all the rest of the TX FIFOs, as some versions of this
19147a1685fSDinh Nguyen 	 * block have overlapping default addresses. This also ensures
19247a1685fSDinh Nguyen 	 * that if the settings have been changed, then they are set to
19347a1685fSDinh Nguyen 	 * known values.
19447a1685fSDinh Nguyen 	 */
19547a1685fSDinh Nguyen 
19647a1685fSDinh Nguyen 	/* start at the end of the GNPTXFSIZ, rounded up */
1970a176279SGregory Herrero 	addr = hsotg->g_rx_fifo_sz + hsotg->g_np_g_tx_fifo_sz;
19847a1685fSDinh Nguyen 
19947a1685fSDinh Nguyen 	/*
2000a176279SGregory Herrero 	 * Configure fifos sizes from provided configuration and assign
201b203d0a2SRobert Baldyga 	 * them to endpoints dynamically according to maxpacket size value of
202b203d0a2SRobert Baldyga 	 * given endpoint.
20347a1685fSDinh Nguyen 	 */
2040a176279SGregory Herrero 	for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
2050a176279SGregory Herrero 		if (!hsotg->g_tx_fifo_sz[ep])
2060a176279SGregory Herrero 			continue;
207b203d0a2SRobert Baldyga 		val = addr;
2080a176279SGregory Herrero 		val |= hsotg->g_tx_fifo_sz[ep] << FIFOSIZE_DEPTH_SHIFT;
2090a176279SGregory Herrero 		WARN_ONCE(addr + hsotg->g_tx_fifo_sz[ep] > hsotg->fifo_mem,
210b203d0a2SRobert Baldyga 			  "insufficient fifo memory");
2110a176279SGregory Herrero 		addr += hsotg->g_tx_fifo_sz[ep];
21247a1685fSDinh Nguyen 
21395c8bc36SAntti Seppälä 		dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
21447a1685fSDinh Nguyen 	}
21547a1685fSDinh Nguyen 
21647a1685fSDinh Nguyen 	/*
21747a1685fSDinh Nguyen 	 * according to p428 of the design guide, we need to ensure that
21847a1685fSDinh Nguyen 	 * all fifos are flushed before continuing
21947a1685fSDinh Nguyen 	 */
22047a1685fSDinh Nguyen 
22195c8bc36SAntti Seppälä 	dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
22247a1685fSDinh Nguyen 	       GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
22347a1685fSDinh Nguyen 
22447a1685fSDinh Nguyen 	/* wait until the fifos are both flushed */
22547a1685fSDinh Nguyen 	timeout = 100;
22647a1685fSDinh Nguyen 	while (1) {
22795c8bc36SAntti Seppälä 		val = dwc2_readl(hsotg->regs + GRSTCTL);
22847a1685fSDinh Nguyen 
22947a1685fSDinh Nguyen 		if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
23047a1685fSDinh Nguyen 			break;
23147a1685fSDinh Nguyen 
23247a1685fSDinh Nguyen 		if (--timeout == 0) {
23347a1685fSDinh Nguyen 			dev_err(hsotg->dev,
23447a1685fSDinh Nguyen 				"%s: timeout flushing fifos (GRSTCTL=%08x)\n",
23547a1685fSDinh Nguyen 				__func__, val);
23648b20bcbSGregory Herrero 			break;
23747a1685fSDinh Nguyen 		}
23847a1685fSDinh Nguyen 
23947a1685fSDinh Nguyen 		udelay(1);
24047a1685fSDinh Nguyen 	}
24147a1685fSDinh Nguyen 
24247a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
24347a1685fSDinh Nguyen }
24447a1685fSDinh Nguyen 
24547a1685fSDinh Nguyen /**
24647a1685fSDinh Nguyen  * @ep: USB endpoint to allocate request for.
24747a1685fSDinh Nguyen  * @flags: Allocation flags
24847a1685fSDinh Nguyen  *
24947a1685fSDinh Nguyen  * Allocate a new USB request structure appropriate for the specified endpoint
25047a1685fSDinh Nguyen  */
2511f91b4ccSFelipe Balbi static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
25247a1685fSDinh Nguyen 						      gfp_t flags)
25347a1685fSDinh Nguyen {
2541f91b4ccSFelipe Balbi 	struct dwc2_hsotg_req *req;
25547a1685fSDinh Nguyen 
2561f91b4ccSFelipe Balbi 	req = kzalloc(sizeof(struct dwc2_hsotg_req), flags);
25747a1685fSDinh Nguyen 	if (!req)
25847a1685fSDinh Nguyen 		return NULL;
25947a1685fSDinh Nguyen 
26047a1685fSDinh Nguyen 	INIT_LIST_HEAD(&req->queue);
26147a1685fSDinh Nguyen 
26247a1685fSDinh Nguyen 	return &req->req;
26347a1685fSDinh Nguyen }
26447a1685fSDinh Nguyen 
26547a1685fSDinh Nguyen /**
26647a1685fSDinh Nguyen  * is_ep_periodic - return true if the endpoint is in periodic mode.
26747a1685fSDinh Nguyen  * @hs_ep: The endpoint to query.
26847a1685fSDinh Nguyen  *
26947a1685fSDinh Nguyen  * Returns true if the endpoint is in periodic mode, meaning it is being
27047a1685fSDinh Nguyen  * used for an Interrupt or ISO transfer.
27147a1685fSDinh Nguyen  */
2721f91b4ccSFelipe Balbi static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
27347a1685fSDinh Nguyen {
27447a1685fSDinh Nguyen 	return hs_ep->periodic;
27547a1685fSDinh Nguyen }
27647a1685fSDinh Nguyen 
27747a1685fSDinh Nguyen /**
2781f91b4ccSFelipe Balbi  * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
27947a1685fSDinh Nguyen  * @hsotg: The device state.
28047a1685fSDinh Nguyen  * @hs_ep: The endpoint for the request
28147a1685fSDinh Nguyen  * @hs_req: The request being processed.
28247a1685fSDinh Nguyen  *
2831f91b4ccSFelipe Balbi  * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
28447a1685fSDinh Nguyen  * of a request to ensure the buffer is ready for access by the caller.
28547a1685fSDinh Nguyen  */
2861f91b4ccSFelipe Balbi static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
2871f91b4ccSFelipe Balbi 				struct dwc2_hsotg_ep *hs_ep,
2881f91b4ccSFelipe Balbi 				struct dwc2_hsotg_req *hs_req)
28947a1685fSDinh Nguyen {
29047a1685fSDinh Nguyen 	struct usb_request *req = &hs_req->req;
29147a1685fSDinh Nguyen 
29247a1685fSDinh Nguyen 	/* ignore this if we're not moving any data */
29347a1685fSDinh Nguyen 	if (hs_req->req.length == 0)
29447a1685fSDinh Nguyen 		return;
29547a1685fSDinh Nguyen 
29647a1685fSDinh Nguyen 	usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
29747a1685fSDinh Nguyen }
29847a1685fSDinh Nguyen 
29947a1685fSDinh Nguyen /**
3001f91b4ccSFelipe Balbi  * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
30147a1685fSDinh Nguyen  * @hsotg: The controller state.
30247a1685fSDinh Nguyen  * @hs_ep: The endpoint we're going to write for.
30347a1685fSDinh Nguyen  * @hs_req: The request to write data for.
30447a1685fSDinh Nguyen  *
30547a1685fSDinh Nguyen  * This is called when the TxFIFO has some space in it to hold a new
30647a1685fSDinh Nguyen  * transmission and we have something to give it. The actual setup of
30747a1685fSDinh Nguyen  * the data size is done elsewhere, so all we have to do is to actually
30847a1685fSDinh Nguyen  * write the data.
30947a1685fSDinh Nguyen  *
31047a1685fSDinh Nguyen  * The return value is zero if there is more space (or nothing was done)
31147a1685fSDinh Nguyen  * otherwise -ENOSPC is returned if the FIFO space was used up.
31247a1685fSDinh Nguyen  *
31347a1685fSDinh Nguyen  * This routine is only needed for PIO
31447a1685fSDinh Nguyen  */
3151f91b4ccSFelipe Balbi static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
3161f91b4ccSFelipe Balbi 				struct dwc2_hsotg_ep *hs_ep,
3171f91b4ccSFelipe Balbi 				struct dwc2_hsotg_req *hs_req)
31847a1685fSDinh Nguyen {
31947a1685fSDinh Nguyen 	bool periodic = is_ep_periodic(hs_ep);
32095c8bc36SAntti Seppälä 	u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
32147a1685fSDinh Nguyen 	int buf_pos = hs_req->req.actual;
32247a1685fSDinh Nguyen 	int to_write = hs_ep->size_loaded;
32347a1685fSDinh Nguyen 	void *data;
32447a1685fSDinh Nguyen 	int can_write;
32547a1685fSDinh Nguyen 	int pkt_round;
32647a1685fSDinh Nguyen 	int max_transfer;
32747a1685fSDinh Nguyen 
32847a1685fSDinh Nguyen 	to_write -= (buf_pos - hs_ep->last_load);
32947a1685fSDinh Nguyen 
33047a1685fSDinh Nguyen 	/* if there's nothing to write, get out early */
33147a1685fSDinh Nguyen 	if (to_write == 0)
33247a1685fSDinh Nguyen 		return 0;
33347a1685fSDinh Nguyen 
33447a1685fSDinh Nguyen 	if (periodic && !hsotg->dedicated_fifos) {
33595c8bc36SAntti Seppälä 		u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
33647a1685fSDinh Nguyen 		int size_left;
33747a1685fSDinh Nguyen 		int size_done;
33847a1685fSDinh Nguyen 
33947a1685fSDinh Nguyen 		/*
34047a1685fSDinh Nguyen 		 * work out how much data was loaded so we can calculate
34147a1685fSDinh Nguyen 		 * how much data is left in the fifo.
34247a1685fSDinh Nguyen 		 */
34347a1685fSDinh Nguyen 
34447a1685fSDinh Nguyen 		size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
34547a1685fSDinh Nguyen 
34647a1685fSDinh Nguyen 		/*
34747a1685fSDinh Nguyen 		 * if shared fifo, we cannot write anything until the
34847a1685fSDinh Nguyen 		 * previous data has been completely sent.
34947a1685fSDinh Nguyen 		 */
35047a1685fSDinh Nguyen 		if (hs_ep->fifo_load != 0) {
3511f91b4ccSFelipe Balbi 			dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
35247a1685fSDinh Nguyen 			return -ENOSPC;
35347a1685fSDinh Nguyen 		}
35447a1685fSDinh Nguyen 
35547a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
35647a1685fSDinh Nguyen 			__func__, size_left,
35747a1685fSDinh Nguyen 			hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
35847a1685fSDinh Nguyen 
35947a1685fSDinh Nguyen 		/* how much of the data has moved */
36047a1685fSDinh Nguyen 		size_done = hs_ep->size_loaded - size_left;
36147a1685fSDinh Nguyen 
36247a1685fSDinh Nguyen 		/* how much data is left in the fifo */
36347a1685fSDinh Nguyen 		can_write = hs_ep->fifo_load - size_done;
36447a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
36547a1685fSDinh Nguyen 			__func__, can_write);
36647a1685fSDinh Nguyen 
36747a1685fSDinh Nguyen 		can_write = hs_ep->fifo_size - can_write;
36847a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
36947a1685fSDinh Nguyen 			__func__, can_write);
37047a1685fSDinh Nguyen 
37147a1685fSDinh Nguyen 		if (can_write <= 0) {
3721f91b4ccSFelipe Balbi 			dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
37347a1685fSDinh Nguyen 			return -ENOSPC;
37447a1685fSDinh Nguyen 		}
37547a1685fSDinh Nguyen 	} else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
37695c8bc36SAntti Seppälä 		can_write = dwc2_readl(hsotg->regs + DTXFSTS(hs_ep->index));
37747a1685fSDinh Nguyen 
37847a1685fSDinh Nguyen 		can_write &= 0xffff;
37947a1685fSDinh Nguyen 		can_write *= 4;
38047a1685fSDinh Nguyen 	} else {
38147a1685fSDinh Nguyen 		if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
38247a1685fSDinh Nguyen 			dev_dbg(hsotg->dev,
38347a1685fSDinh Nguyen 				"%s: no queue slots available (0x%08x)\n",
38447a1685fSDinh Nguyen 				__func__, gnptxsts);
38547a1685fSDinh Nguyen 
3861f91b4ccSFelipe Balbi 			dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
38747a1685fSDinh Nguyen 			return -ENOSPC;
38847a1685fSDinh Nguyen 		}
38947a1685fSDinh Nguyen 
39047a1685fSDinh Nguyen 		can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
39147a1685fSDinh Nguyen 		can_write *= 4;	/* fifo size is in 32bit quantities. */
39247a1685fSDinh Nguyen 	}
39347a1685fSDinh Nguyen 
39447a1685fSDinh Nguyen 	max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
39547a1685fSDinh Nguyen 
39647a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
39747a1685fSDinh Nguyen 		 __func__, gnptxsts, can_write, to_write, max_transfer);
39847a1685fSDinh Nguyen 
39947a1685fSDinh Nguyen 	/*
40047a1685fSDinh Nguyen 	 * limit to 512 bytes of data, it seems at least on the non-periodic
40147a1685fSDinh Nguyen 	 * FIFO, requests of >512 cause the endpoint to get stuck with a
40247a1685fSDinh Nguyen 	 * fragment of the end of the transfer in it.
40347a1685fSDinh Nguyen 	 */
40447a1685fSDinh Nguyen 	if (can_write > 512 && !periodic)
40547a1685fSDinh Nguyen 		can_write = 512;
40647a1685fSDinh Nguyen 
40747a1685fSDinh Nguyen 	/*
40847a1685fSDinh Nguyen 	 * limit the write to one max-packet size worth of data, but allow
40947a1685fSDinh Nguyen 	 * the transfer to return that it did not run out of fifo space
41047a1685fSDinh Nguyen 	 * doing it.
41147a1685fSDinh Nguyen 	 */
41247a1685fSDinh Nguyen 	if (to_write > max_transfer) {
41347a1685fSDinh Nguyen 		to_write = max_transfer;
41447a1685fSDinh Nguyen 
41547a1685fSDinh Nguyen 		/* it's needed only when we do not use dedicated fifos */
41647a1685fSDinh Nguyen 		if (!hsotg->dedicated_fifos)
4171f91b4ccSFelipe Balbi 			dwc2_hsotg_en_gsint(hsotg,
41847a1685fSDinh Nguyen 					   periodic ? GINTSTS_PTXFEMP :
41947a1685fSDinh Nguyen 					   GINTSTS_NPTXFEMP);
42047a1685fSDinh Nguyen 	}
42147a1685fSDinh Nguyen 
42247a1685fSDinh Nguyen 	/* see if we can write data */
42347a1685fSDinh Nguyen 
42447a1685fSDinh Nguyen 	if (to_write > can_write) {
42547a1685fSDinh Nguyen 		to_write = can_write;
42647a1685fSDinh Nguyen 		pkt_round = to_write % max_transfer;
42747a1685fSDinh Nguyen 
42847a1685fSDinh Nguyen 		/*
42947a1685fSDinh Nguyen 		 * Round the write down to an
43047a1685fSDinh Nguyen 		 * exact number of packets.
43147a1685fSDinh Nguyen 		 *
43247a1685fSDinh Nguyen 		 * Note, we do not currently check to see if we can ever
43347a1685fSDinh Nguyen 		 * write a full packet or not to the FIFO.
43447a1685fSDinh Nguyen 		 */
43547a1685fSDinh Nguyen 
43647a1685fSDinh Nguyen 		if (pkt_round)
43747a1685fSDinh Nguyen 			to_write -= pkt_round;
43847a1685fSDinh Nguyen 
43947a1685fSDinh Nguyen 		/*
44047a1685fSDinh Nguyen 		 * enable correct FIFO interrupt to alert us when there
44147a1685fSDinh Nguyen 		 * is more room left.
44247a1685fSDinh Nguyen 		 */
44347a1685fSDinh Nguyen 
44447a1685fSDinh Nguyen 		/* it's needed only when we do not use dedicated fifos */
44547a1685fSDinh Nguyen 		if (!hsotg->dedicated_fifos)
4461f91b4ccSFelipe Balbi 			dwc2_hsotg_en_gsint(hsotg,
44747a1685fSDinh Nguyen 					   periodic ? GINTSTS_PTXFEMP :
44847a1685fSDinh Nguyen 					   GINTSTS_NPTXFEMP);
44947a1685fSDinh Nguyen 	}
45047a1685fSDinh Nguyen 
45147a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
45247a1685fSDinh Nguyen 		 to_write, hs_req->req.length, can_write, buf_pos);
45347a1685fSDinh Nguyen 
45447a1685fSDinh Nguyen 	if (to_write <= 0)
45547a1685fSDinh Nguyen 		return -ENOSPC;
45647a1685fSDinh Nguyen 
45747a1685fSDinh Nguyen 	hs_req->req.actual = buf_pos + to_write;
45847a1685fSDinh Nguyen 	hs_ep->total_data += to_write;
45947a1685fSDinh Nguyen 
46047a1685fSDinh Nguyen 	if (periodic)
46147a1685fSDinh Nguyen 		hs_ep->fifo_load += to_write;
46247a1685fSDinh Nguyen 
46347a1685fSDinh Nguyen 	to_write = DIV_ROUND_UP(to_write, 4);
46447a1685fSDinh Nguyen 	data = hs_req->req.buf + buf_pos;
46547a1685fSDinh Nguyen 
46647a1685fSDinh Nguyen 	iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
46747a1685fSDinh Nguyen 
46847a1685fSDinh Nguyen 	return (to_write >= can_write) ? -ENOSPC : 0;
46947a1685fSDinh Nguyen }
47047a1685fSDinh Nguyen 
47147a1685fSDinh Nguyen /**
47247a1685fSDinh Nguyen  * get_ep_limit - get the maximum data legnth for this endpoint
47347a1685fSDinh Nguyen  * @hs_ep: The endpoint
47447a1685fSDinh Nguyen  *
47547a1685fSDinh Nguyen  * Return the maximum data that can be queued in one go on a given endpoint
47647a1685fSDinh Nguyen  * so that transfers that are too long can be split.
47747a1685fSDinh Nguyen  */
4781f91b4ccSFelipe Balbi static unsigned get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
47947a1685fSDinh Nguyen {
48047a1685fSDinh Nguyen 	int index = hs_ep->index;
48147a1685fSDinh Nguyen 	unsigned maxsize;
48247a1685fSDinh Nguyen 	unsigned maxpkt;
48347a1685fSDinh Nguyen 
48447a1685fSDinh Nguyen 	if (index != 0) {
48547a1685fSDinh Nguyen 		maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
48647a1685fSDinh Nguyen 		maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
48747a1685fSDinh Nguyen 	} else {
48847a1685fSDinh Nguyen 		maxsize = 64+64;
48947a1685fSDinh Nguyen 		if (hs_ep->dir_in)
49047a1685fSDinh Nguyen 			maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
49147a1685fSDinh Nguyen 		else
49247a1685fSDinh Nguyen 			maxpkt = 2;
49347a1685fSDinh Nguyen 	}
49447a1685fSDinh Nguyen 
49547a1685fSDinh Nguyen 	/* we made the constant loading easier above by using +1 */
49647a1685fSDinh Nguyen 	maxpkt--;
49747a1685fSDinh Nguyen 	maxsize--;
49847a1685fSDinh Nguyen 
49947a1685fSDinh Nguyen 	/*
50047a1685fSDinh Nguyen 	 * constrain by packet count if maxpkts*pktsize is greater
50147a1685fSDinh Nguyen 	 * than the length register size.
50247a1685fSDinh Nguyen 	 */
50347a1685fSDinh Nguyen 
50447a1685fSDinh Nguyen 	if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
50547a1685fSDinh Nguyen 		maxsize = maxpkt * hs_ep->ep.maxpacket;
50647a1685fSDinh Nguyen 
50747a1685fSDinh Nguyen 	return maxsize;
50847a1685fSDinh Nguyen }
50947a1685fSDinh Nguyen 
51047a1685fSDinh Nguyen /**
5111f91b4ccSFelipe Balbi  * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
51247a1685fSDinh Nguyen  * @hsotg: The controller state.
51347a1685fSDinh Nguyen  * @hs_ep: The endpoint to process a request for
51447a1685fSDinh Nguyen  * @hs_req: The request to start.
51547a1685fSDinh Nguyen  * @continuing: True if we are doing more for the current request.
51647a1685fSDinh Nguyen  *
51747a1685fSDinh Nguyen  * Start the given request running by setting the endpoint registers
51847a1685fSDinh Nguyen  * appropriately, and writing any data to the FIFOs.
51947a1685fSDinh Nguyen  */
5201f91b4ccSFelipe Balbi static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
5211f91b4ccSFelipe Balbi 				struct dwc2_hsotg_ep *hs_ep,
5221f91b4ccSFelipe Balbi 				struct dwc2_hsotg_req *hs_req,
52347a1685fSDinh Nguyen 				bool continuing)
52447a1685fSDinh Nguyen {
52547a1685fSDinh Nguyen 	struct usb_request *ureq = &hs_req->req;
52647a1685fSDinh Nguyen 	int index = hs_ep->index;
52747a1685fSDinh Nguyen 	int dir_in = hs_ep->dir_in;
52847a1685fSDinh Nguyen 	u32 epctrl_reg;
52947a1685fSDinh Nguyen 	u32 epsize_reg;
53047a1685fSDinh Nguyen 	u32 epsize;
53147a1685fSDinh Nguyen 	u32 ctrl;
53247a1685fSDinh Nguyen 	unsigned length;
53347a1685fSDinh Nguyen 	unsigned packets;
53447a1685fSDinh Nguyen 	unsigned maxreq;
53547a1685fSDinh Nguyen 
53647a1685fSDinh Nguyen 	if (index != 0) {
53747a1685fSDinh Nguyen 		if (hs_ep->req && !continuing) {
53847a1685fSDinh Nguyen 			dev_err(hsotg->dev, "%s: active request\n", __func__);
53947a1685fSDinh Nguyen 			WARN_ON(1);
54047a1685fSDinh Nguyen 			return;
54147a1685fSDinh Nguyen 		} else if (hs_ep->req != hs_req && continuing) {
54247a1685fSDinh Nguyen 			dev_err(hsotg->dev,
54347a1685fSDinh Nguyen 				"%s: continue different req\n", __func__);
54447a1685fSDinh Nguyen 			WARN_ON(1);
54547a1685fSDinh Nguyen 			return;
54647a1685fSDinh Nguyen 		}
54747a1685fSDinh Nguyen 	}
54847a1685fSDinh Nguyen 
54947a1685fSDinh Nguyen 	epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
55047a1685fSDinh Nguyen 	epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
55147a1685fSDinh Nguyen 
55247a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
55395c8bc36SAntti Seppälä 		__func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
55447a1685fSDinh Nguyen 		hs_ep->dir_in ? "in" : "out");
55547a1685fSDinh Nguyen 
55647a1685fSDinh Nguyen 	/* If endpoint is stalled, we will restart request later */
55795c8bc36SAntti Seppälä 	ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
55847a1685fSDinh Nguyen 
559b2d4c54eSMian Yousaf Kaukab 	if (index && ctrl & DXEPCTL_STALL) {
56047a1685fSDinh Nguyen 		dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
56147a1685fSDinh Nguyen 		return;
56247a1685fSDinh Nguyen 	}
56347a1685fSDinh Nguyen 
56447a1685fSDinh Nguyen 	length = ureq->length - ureq->actual;
56547a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
56647a1685fSDinh Nguyen 		ureq->length, ureq->actual);
56747a1685fSDinh Nguyen 
56847a1685fSDinh Nguyen 	maxreq = get_ep_limit(hs_ep);
56947a1685fSDinh Nguyen 	if (length > maxreq) {
57047a1685fSDinh Nguyen 		int round = maxreq % hs_ep->ep.maxpacket;
57147a1685fSDinh Nguyen 
57247a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
57347a1685fSDinh Nguyen 			__func__, length, maxreq, round);
57447a1685fSDinh Nguyen 
57547a1685fSDinh Nguyen 		/* round down to multiple of packets */
57647a1685fSDinh Nguyen 		if (round)
57747a1685fSDinh Nguyen 			maxreq -= round;
57847a1685fSDinh Nguyen 
57947a1685fSDinh Nguyen 		length = maxreq;
58047a1685fSDinh Nguyen 	}
58147a1685fSDinh Nguyen 
58247a1685fSDinh Nguyen 	if (length)
58347a1685fSDinh Nguyen 		packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
58447a1685fSDinh Nguyen 	else
58547a1685fSDinh Nguyen 		packets = 1;	/* send one packet if length is zero. */
58647a1685fSDinh Nguyen 
58747a1685fSDinh Nguyen 	if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
58847a1685fSDinh Nguyen 		dev_err(hsotg->dev, "req length > maxpacket*mc\n");
58947a1685fSDinh Nguyen 		return;
59047a1685fSDinh Nguyen 	}
59147a1685fSDinh Nguyen 
59247a1685fSDinh Nguyen 	if (dir_in && index != 0)
59347a1685fSDinh Nguyen 		if (hs_ep->isochronous)
59447a1685fSDinh Nguyen 			epsize = DXEPTSIZ_MC(packets);
59547a1685fSDinh Nguyen 		else
59647a1685fSDinh Nguyen 			epsize = DXEPTSIZ_MC(1);
59747a1685fSDinh Nguyen 	else
59847a1685fSDinh Nguyen 		epsize = 0;
59947a1685fSDinh Nguyen 
60047a1685fSDinh Nguyen 	/*
601f71b5e25SMian Yousaf Kaukab 	 * zero length packet should be programmed on its own and should not
602f71b5e25SMian Yousaf Kaukab 	 * be counted in DIEPTSIZ.PktCnt with other packets.
60347a1685fSDinh Nguyen 	 */
604f71b5e25SMian Yousaf Kaukab 	if (dir_in && ureq->zero && !continuing) {
605f71b5e25SMian Yousaf Kaukab 		/* Test if zlp is actually required. */
606f71b5e25SMian Yousaf Kaukab 		if ((ureq->length >= hs_ep->ep.maxpacket) &&
607f71b5e25SMian Yousaf Kaukab 					!(ureq->length % hs_ep->ep.maxpacket))
6088a20fa45SMian Yousaf Kaukab 			hs_ep->send_zlp = 1;
60947a1685fSDinh Nguyen 	}
61047a1685fSDinh Nguyen 
61147a1685fSDinh Nguyen 	epsize |= DXEPTSIZ_PKTCNT(packets);
61247a1685fSDinh Nguyen 	epsize |= DXEPTSIZ_XFERSIZE(length);
61347a1685fSDinh Nguyen 
61447a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
61547a1685fSDinh Nguyen 		__func__, packets, length, ureq->length, epsize, epsize_reg);
61647a1685fSDinh Nguyen 
61747a1685fSDinh Nguyen 	/* store the request as the current one we're doing */
61847a1685fSDinh Nguyen 	hs_ep->req = hs_req;
61947a1685fSDinh Nguyen 
62047a1685fSDinh Nguyen 	/* write size / packets */
62195c8bc36SAntti Seppälä 	dwc2_writel(epsize, hsotg->regs + epsize_reg);
62247a1685fSDinh Nguyen 
62347a1685fSDinh Nguyen 	if (using_dma(hsotg) && !continuing) {
62447a1685fSDinh Nguyen 		unsigned int dma_reg;
62547a1685fSDinh Nguyen 
62647a1685fSDinh Nguyen 		/*
62747a1685fSDinh Nguyen 		 * write DMA address to control register, buffer already
6281f91b4ccSFelipe Balbi 		 * synced by dwc2_hsotg_ep_queue().
62947a1685fSDinh Nguyen 		 */
63047a1685fSDinh Nguyen 
63147a1685fSDinh Nguyen 		dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
63295c8bc36SAntti Seppälä 		dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
63347a1685fSDinh Nguyen 
6340cc4cf6fSFabio Estevam 		dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
63547a1685fSDinh Nguyen 			__func__, &ureq->dma, dma_reg);
63647a1685fSDinh Nguyen 	}
63747a1685fSDinh Nguyen 
63847a1685fSDinh Nguyen 	ctrl |= DXEPCTL_EPENA;	/* ensure ep enabled */
63947a1685fSDinh Nguyen 	ctrl |= DXEPCTL_USBACTEP;
64047a1685fSDinh Nguyen 
641fe0b94abSMian Yousaf Kaukab 	dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
64247a1685fSDinh Nguyen 
64347a1685fSDinh Nguyen 	/* For Setup request do not clear NAK */
644fe0b94abSMian Yousaf Kaukab 	if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
64547a1685fSDinh Nguyen 		ctrl |= DXEPCTL_CNAK;	/* clear NAK set by core */
64647a1685fSDinh Nguyen 
64747a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
64895c8bc36SAntti Seppälä 	dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
64947a1685fSDinh Nguyen 
65047a1685fSDinh Nguyen 	/*
65147a1685fSDinh Nguyen 	 * set these, it seems that DMA support increments past the end
65247a1685fSDinh Nguyen 	 * of the packet buffer so we need to calculate the length from
65347a1685fSDinh Nguyen 	 * this information.
65447a1685fSDinh Nguyen 	 */
65547a1685fSDinh Nguyen 	hs_ep->size_loaded = length;
65647a1685fSDinh Nguyen 	hs_ep->last_load = ureq->actual;
65747a1685fSDinh Nguyen 
65847a1685fSDinh Nguyen 	if (dir_in && !using_dma(hsotg)) {
65947a1685fSDinh Nguyen 		/* set these anyway, we may need them for non-periodic in */
66047a1685fSDinh Nguyen 		hs_ep->fifo_load = 0;
66147a1685fSDinh Nguyen 
6621f91b4ccSFelipe Balbi 		dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
66347a1685fSDinh Nguyen 	}
66447a1685fSDinh Nguyen 
66547a1685fSDinh Nguyen 	/*
66647a1685fSDinh Nguyen 	 * clear the INTknTXFEmpMsk when we start request, more as a aide
66747a1685fSDinh Nguyen 	 * to debugging to see what is going on.
66847a1685fSDinh Nguyen 	 */
66947a1685fSDinh Nguyen 	if (dir_in)
67095c8bc36SAntti Seppälä 		dwc2_writel(DIEPMSK_INTKNTXFEMPMSK,
67147a1685fSDinh Nguyen 		       hsotg->regs + DIEPINT(index));
67247a1685fSDinh Nguyen 
67347a1685fSDinh Nguyen 	/*
67447a1685fSDinh Nguyen 	 * Note, trying to clear the NAK here causes problems with transmit
67547a1685fSDinh Nguyen 	 * on the S3C6400 ending up with the TXFIFO becoming full.
67647a1685fSDinh Nguyen 	 */
67747a1685fSDinh Nguyen 
67847a1685fSDinh Nguyen 	/* check ep is enabled */
67995c8bc36SAntti Seppälä 	if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
6801a0ed863SMian Yousaf Kaukab 		dev_dbg(hsotg->dev,
68147a1685fSDinh Nguyen 			 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
68295c8bc36SAntti Seppälä 			 index, dwc2_readl(hsotg->regs + epctrl_reg));
68347a1685fSDinh Nguyen 
68447a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
68595c8bc36SAntti Seppälä 		__func__, dwc2_readl(hsotg->regs + epctrl_reg));
68647a1685fSDinh Nguyen 
68747a1685fSDinh Nguyen 	/* enable ep interrupts */
6881f91b4ccSFelipe Balbi 	dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
68947a1685fSDinh Nguyen }
69047a1685fSDinh Nguyen 
69147a1685fSDinh Nguyen /**
6921f91b4ccSFelipe Balbi  * dwc2_hsotg_map_dma - map the DMA memory being used for the request
69347a1685fSDinh Nguyen  * @hsotg: The device state.
69447a1685fSDinh Nguyen  * @hs_ep: The endpoint the request is on.
69547a1685fSDinh Nguyen  * @req: The request being processed.
69647a1685fSDinh Nguyen  *
69747a1685fSDinh Nguyen  * We've been asked to queue a request, so ensure that the memory buffer
69847a1685fSDinh Nguyen  * is correctly setup for DMA. If we've been passed an extant DMA address
69947a1685fSDinh Nguyen  * then ensure the buffer has been synced to memory. If our buffer has no
70047a1685fSDinh Nguyen  * DMA memory, then we map the memory and mark our request to allow us to
70147a1685fSDinh Nguyen  * cleanup on completion.
70247a1685fSDinh Nguyen  */
7031f91b4ccSFelipe Balbi static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
7041f91b4ccSFelipe Balbi 			     struct dwc2_hsotg_ep *hs_ep,
70547a1685fSDinh Nguyen 			     struct usb_request *req)
70647a1685fSDinh Nguyen {
7071f91b4ccSFelipe Balbi 	struct dwc2_hsotg_req *hs_req = our_req(req);
70847a1685fSDinh Nguyen 	int ret;
70947a1685fSDinh Nguyen 
71047a1685fSDinh Nguyen 	/* if the length is zero, ignore the DMA data */
71147a1685fSDinh Nguyen 	if (hs_req->req.length == 0)
71247a1685fSDinh Nguyen 		return 0;
71347a1685fSDinh Nguyen 
71447a1685fSDinh Nguyen 	ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
71547a1685fSDinh Nguyen 	if (ret)
71647a1685fSDinh Nguyen 		goto dma_error;
71747a1685fSDinh Nguyen 
71847a1685fSDinh Nguyen 	return 0;
71947a1685fSDinh Nguyen 
72047a1685fSDinh Nguyen dma_error:
72147a1685fSDinh Nguyen 	dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
72247a1685fSDinh Nguyen 		__func__, req->buf, req->length);
72347a1685fSDinh Nguyen 
72447a1685fSDinh Nguyen 	return -EIO;
72547a1685fSDinh Nguyen }
72647a1685fSDinh Nguyen 
7271f91b4ccSFelipe Balbi static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
7281f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
7297d24c1b5SMian Yousaf Kaukab {
7307d24c1b5SMian Yousaf Kaukab 	void *req_buf = hs_req->req.buf;
7317d24c1b5SMian Yousaf Kaukab 
7327d24c1b5SMian Yousaf Kaukab 	/* If dma is not being used or buffer is aligned */
7337d24c1b5SMian Yousaf Kaukab 	if (!using_dma(hsotg) || !((long)req_buf & 3))
7347d24c1b5SMian Yousaf Kaukab 		return 0;
7357d24c1b5SMian Yousaf Kaukab 
7367d24c1b5SMian Yousaf Kaukab 	WARN_ON(hs_req->saved_req_buf);
7377d24c1b5SMian Yousaf Kaukab 
7387d24c1b5SMian Yousaf Kaukab 	dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
7397d24c1b5SMian Yousaf Kaukab 			hs_ep->ep.name, req_buf, hs_req->req.length);
7407d24c1b5SMian Yousaf Kaukab 
7417d24c1b5SMian Yousaf Kaukab 	hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
7427d24c1b5SMian Yousaf Kaukab 	if (!hs_req->req.buf) {
7437d24c1b5SMian Yousaf Kaukab 		hs_req->req.buf = req_buf;
7447d24c1b5SMian Yousaf Kaukab 		dev_err(hsotg->dev,
7457d24c1b5SMian Yousaf Kaukab 			"%s: unable to allocate memory for bounce buffer\n",
7467d24c1b5SMian Yousaf Kaukab 			__func__);
7477d24c1b5SMian Yousaf Kaukab 		return -ENOMEM;
7487d24c1b5SMian Yousaf Kaukab 	}
7497d24c1b5SMian Yousaf Kaukab 
7507d24c1b5SMian Yousaf Kaukab 	/* Save actual buffer */
7517d24c1b5SMian Yousaf Kaukab 	hs_req->saved_req_buf = req_buf;
7527d24c1b5SMian Yousaf Kaukab 
7537d24c1b5SMian Yousaf Kaukab 	if (hs_ep->dir_in)
7547d24c1b5SMian Yousaf Kaukab 		memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
7557d24c1b5SMian Yousaf Kaukab 	return 0;
7567d24c1b5SMian Yousaf Kaukab }
7577d24c1b5SMian Yousaf Kaukab 
7581f91b4ccSFelipe Balbi static void dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
7591f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
7607d24c1b5SMian Yousaf Kaukab {
7617d24c1b5SMian Yousaf Kaukab 	/* If dma is not being used or buffer was aligned */
7627d24c1b5SMian Yousaf Kaukab 	if (!using_dma(hsotg) || !hs_req->saved_req_buf)
7637d24c1b5SMian Yousaf Kaukab 		return;
7647d24c1b5SMian Yousaf Kaukab 
7657d24c1b5SMian Yousaf Kaukab 	dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
7667d24c1b5SMian Yousaf Kaukab 		hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
7677d24c1b5SMian Yousaf Kaukab 
7687d24c1b5SMian Yousaf Kaukab 	/* Copy data from bounce buffer on successful out transfer */
7697d24c1b5SMian Yousaf Kaukab 	if (!hs_ep->dir_in && !hs_req->req.status)
7707d24c1b5SMian Yousaf Kaukab 		memcpy(hs_req->saved_req_buf, hs_req->req.buf,
7717d24c1b5SMian Yousaf Kaukab 							hs_req->req.actual);
7727d24c1b5SMian Yousaf Kaukab 
7737d24c1b5SMian Yousaf Kaukab 	/* Free bounce buffer */
7747d24c1b5SMian Yousaf Kaukab 	kfree(hs_req->req.buf);
7757d24c1b5SMian Yousaf Kaukab 
7767d24c1b5SMian Yousaf Kaukab 	hs_req->req.buf = hs_req->saved_req_buf;
7777d24c1b5SMian Yousaf Kaukab 	hs_req->saved_req_buf = NULL;
7787d24c1b5SMian Yousaf Kaukab }
7797d24c1b5SMian Yousaf Kaukab 
7801f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
78147a1685fSDinh Nguyen 			      gfp_t gfp_flags)
78247a1685fSDinh Nguyen {
7831f91b4ccSFelipe Balbi 	struct dwc2_hsotg_req *hs_req = our_req(req);
7841f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
785941fcce4SDinh Nguyen 	struct dwc2_hsotg *hs = hs_ep->parent;
78647a1685fSDinh Nguyen 	bool first;
7877d24c1b5SMian Yousaf Kaukab 	int ret;
78847a1685fSDinh Nguyen 
78947a1685fSDinh Nguyen 	dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
79047a1685fSDinh Nguyen 		ep->name, req, req->length, req->buf, req->no_interrupt,
79147a1685fSDinh Nguyen 		req->zero, req->short_not_ok);
79247a1685fSDinh Nguyen 
7937ababa92SGregory Herrero 	/* Prevent new request submission when controller is suspended */
7947ababa92SGregory Herrero 	if (hs->lx_state == DWC2_L2) {
7957ababa92SGregory Herrero 		dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
7967ababa92SGregory Herrero 				__func__);
7977ababa92SGregory Herrero 		return -EAGAIN;
7987ababa92SGregory Herrero 	}
7997ababa92SGregory Herrero 
80047a1685fSDinh Nguyen 	/* initialise status of the request */
80147a1685fSDinh Nguyen 	INIT_LIST_HEAD(&hs_req->queue);
80247a1685fSDinh Nguyen 	req->actual = 0;
80347a1685fSDinh Nguyen 	req->status = -EINPROGRESS;
80447a1685fSDinh Nguyen 
8051f91b4ccSFelipe Balbi 	ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
8067d24c1b5SMian Yousaf Kaukab 	if (ret)
8077d24c1b5SMian Yousaf Kaukab 		return ret;
8087d24c1b5SMian Yousaf Kaukab 
80947a1685fSDinh Nguyen 	/* if we're using DMA, sync the buffers as necessary */
81047a1685fSDinh Nguyen 	if (using_dma(hs)) {
8111f91b4ccSFelipe Balbi 		ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
81247a1685fSDinh Nguyen 		if (ret)
81347a1685fSDinh Nguyen 			return ret;
81447a1685fSDinh Nguyen 	}
81547a1685fSDinh Nguyen 
81647a1685fSDinh Nguyen 	first = list_empty(&hs_ep->queue);
81747a1685fSDinh Nguyen 	list_add_tail(&hs_req->queue, &hs_ep->queue);
81847a1685fSDinh Nguyen 
81947a1685fSDinh Nguyen 	if (first)
8201f91b4ccSFelipe Balbi 		dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
82147a1685fSDinh Nguyen 
82247a1685fSDinh Nguyen 	return 0;
82347a1685fSDinh Nguyen }
82447a1685fSDinh Nguyen 
8251f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
82647a1685fSDinh Nguyen 			      gfp_t gfp_flags)
82747a1685fSDinh Nguyen {
8281f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
829941fcce4SDinh Nguyen 	struct dwc2_hsotg *hs = hs_ep->parent;
83047a1685fSDinh Nguyen 	unsigned long flags = 0;
83147a1685fSDinh Nguyen 	int ret = 0;
83247a1685fSDinh Nguyen 
83347a1685fSDinh Nguyen 	spin_lock_irqsave(&hs->lock, flags);
8341f91b4ccSFelipe Balbi 	ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
83547a1685fSDinh Nguyen 	spin_unlock_irqrestore(&hs->lock, flags);
83647a1685fSDinh Nguyen 
83747a1685fSDinh Nguyen 	return ret;
83847a1685fSDinh Nguyen }
83947a1685fSDinh Nguyen 
8401f91b4ccSFelipe Balbi static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
84147a1685fSDinh Nguyen 				      struct usb_request *req)
84247a1685fSDinh Nguyen {
8431f91b4ccSFelipe Balbi 	struct dwc2_hsotg_req *hs_req = our_req(req);
84447a1685fSDinh Nguyen 
84547a1685fSDinh Nguyen 	kfree(hs_req);
84647a1685fSDinh Nguyen }
84747a1685fSDinh Nguyen 
84847a1685fSDinh Nguyen /**
8491f91b4ccSFelipe Balbi  * dwc2_hsotg_complete_oursetup - setup completion callback
85047a1685fSDinh Nguyen  * @ep: The endpoint the request was on.
85147a1685fSDinh Nguyen  * @req: The request completed.
85247a1685fSDinh Nguyen  *
85347a1685fSDinh Nguyen  * Called on completion of any requests the driver itself
85447a1685fSDinh Nguyen  * submitted that need cleaning up.
85547a1685fSDinh Nguyen  */
8561f91b4ccSFelipe Balbi static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
85747a1685fSDinh Nguyen 					struct usb_request *req)
85847a1685fSDinh Nguyen {
8591f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
860941fcce4SDinh Nguyen 	struct dwc2_hsotg *hsotg = hs_ep->parent;
86147a1685fSDinh Nguyen 
86247a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
86347a1685fSDinh Nguyen 
8641f91b4ccSFelipe Balbi 	dwc2_hsotg_ep_free_request(ep, req);
86547a1685fSDinh Nguyen }
86647a1685fSDinh Nguyen 
86747a1685fSDinh Nguyen /**
86847a1685fSDinh Nguyen  * ep_from_windex - convert control wIndex value to endpoint
86947a1685fSDinh Nguyen  * @hsotg: The driver state.
87047a1685fSDinh Nguyen  * @windex: The control request wIndex field (in host order).
87147a1685fSDinh Nguyen  *
87247a1685fSDinh Nguyen  * Convert the given wIndex into a pointer to an driver endpoint
87347a1685fSDinh Nguyen  * structure, or return NULL if it is not a valid endpoint.
87447a1685fSDinh Nguyen  */
8751f91b4ccSFelipe Balbi static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
87647a1685fSDinh Nguyen 					   u32 windex)
87747a1685fSDinh Nguyen {
8781f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *ep;
87947a1685fSDinh Nguyen 	int dir = (windex & USB_DIR_IN) ? 1 : 0;
88047a1685fSDinh Nguyen 	int idx = windex & 0x7F;
88147a1685fSDinh Nguyen 
88247a1685fSDinh Nguyen 	if (windex >= 0x100)
88347a1685fSDinh Nguyen 		return NULL;
88447a1685fSDinh Nguyen 
88547a1685fSDinh Nguyen 	if (idx > hsotg->num_of_eps)
88647a1685fSDinh Nguyen 		return NULL;
88747a1685fSDinh Nguyen 
888c6f5c050SMian Yousaf Kaukab 	ep = index_to_ep(hsotg, idx, dir);
889c6f5c050SMian Yousaf Kaukab 
89047a1685fSDinh Nguyen 	if (idx && ep->dir_in != dir)
89147a1685fSDinh Nguyen 		return NULL;
89247a1685fSDinh Nguyen 
89347a1685fSDinh Nguyen 	return ep;
89447a1685fSDinh Nguyen }
89547a1685fSDinh Nguyen 
89647a1685fSDinh Nguyen /**
8971f91b4ccSFelipe Balbi  * dwc2_hsotg_set_test_mode - Enable usb Test Modes
8989e14d0a5SGregory Herrero  * @hsotg: The driver state.
8999e14d0a5SGregory Herrero  * @testmode: requested usb test mode
9009e14d0a5SGregory Herrero  * Enable usb Test Mode requested by the Host.
9019e14d0a5SGregory Herrero  */
9021f91b4ccSFelipe Balbi int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
9039e14d0a5SGregory Herrero {
90495c8bc36SAntti Seppälä 	int dctl = dwc2_readl(hsotg->regs + DCTL);
9059e14d0a5SGregory Herrero 
9069e14d0a5SGregory Herrero 	dctl &= ~DCTL_TSTCTL_MASK;
9079e14d0a5SGregory Herrero 	switch (testmode) {
9089e14d0a5SGregory Herrero 	case TEST_J:
9099e14d0a5SGregory Herrero 	case TEST_K:
9109e14d0a5SGregory Herrero 	case TEST_SE0_NAK:
9119e14d0a5SGregory Herrero 	case TEST_PACKET:
9129e14d0a5SGregory Herrero 	case TEST_FORCE_EN:
9139e14d0a5SGregory Herrero 		dctl |= testmode << DCTL_TSTCTL_SHIFT;
9149e14d0a5SGregory Herrero 		break;
9159e14d0a5SGregory Herrero 	default:
9169e14d0a5SGregory Herrero 		return -EINVAL;
9179e14d0a5SGregory Herrero 	}
91895c8bc36SAntti Seppälä 	dwc2_writel(dctl, hsotg->regs + DCTL);
9199e14d0a5SGregory Herrero 	return 0;
9209e14d0a5SGregory Herrero }
9219e14d0a5SGregory Herrero 
9229e14d0a5SGregory Herrero /**
9231f91b4ccSFelipe Balbi  * dwc2_hsotg_send_reply - send reply to control request
92447a1685fSDinh Nguyen  * @hsotg: The device state
92547a1685fSDinh Nguyen  * @ep: Endpoint 0
92647a1685fSDinh Nguyen  * @buff: Buffer for request
92747a1685fSDinh Nguyen  * @length: Length of reply.
92847a1685fSDinh Nguyen  *
92947a1685fSDinh Nguyen  * Create a request and queue it on the given endpoint. This is useful as
93047a1685fSDinh Nguyen  * an internal method of sending replies to certain control requests, etc.
93147a1685fSDinh Nguyen  */
9321f91b4ccSFelipe Balbi static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
9331f91b4ccSFelipe Balbi 				struct dwc2_hsotg_ep *ep,
93447a1685fSDinh Nguyen 				void *buff,
93547a1685fSDinh Nguyen 				int length)
93647a1685fSDinh Nguyen {
93747a1685fSDinh Nguyen 	struct usb_request *req;
93847a1685fSDinh Nguyen 	int ret;
93947a1685fSDinh Nguyen 
94047a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
94147a1685fSDinh Nguyen 
9421f91b4ccSFelipe Balbi 	req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
94347a1685fSDinh Nguyen 	hsotg->ep0_reply = req;
94447a1685fSDinh Nguyen 	if (!req) {
94547a1685fSDinh Nguyen 		dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
94647a1685fSDinh Nguyen 		return -ENOMEM;
94747a1685fSDinh Nguyen 	}
94847a1685fSDinh Nguyen 
94947a1685fSDinh Nguyen 	req->buf = hsotg->ep0_buff;
95047a1685fSDinh Nguyen 	req->length = length;
951f71b5e25SMian Yousaf Kaukab 	/*
952f71b5e25SMian Yousaf Kaukab 	 * zero flag is for sending zlp in DATA IN stage. It has no impact on
953f71b5e25SMian Yousaf Kaukab 	 * STATUS stage.
954f71b5e25SMian Yousaf Kaukab 	 */
955f71b5e25SMian Yousaf Kaukab 	req->zero = 0;
9561f91b4ccSFelipe Balbi 	req->complete = dwc2_hsotg_complete_oursetup;
95747a1685fSDinh Nguyen 
95847a1685fSDinh Nguyen 	if (length)
95947a1685fSDinh Nguyen 		memcpy(req->buf, buff, length);
96047a1685fSDinh Nguyen 
9611f91b4ccSFelipe Balbi 	ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
96247a1685fSDinh Nguyen 	if (ret) {
96347a1685fSDinh Nguyen 		dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
96447a1685fSDinh Nguyen 		return ret;
96547a1685fSDinh Nguyen 	}
96647a1685fSDinh Nguyen 
96747a1685fSDinh Nguyen 	return 0;
96847a1685fSDinh Nguyen }
96947a1685fSDinh Nguyen 
97047a1685fSDinh Nguyen /**
9711f91b4ccSFelipe Balbi  * dwc2_hsotg_process_req_status - process request GET_STATUS
97247a1685fSDinh Nguyen  * @hsotg: The device state
97347a1685fSDinh Nguyen  * @ctrl: USB control request
97447a1685fSDinh Nguyen  */
9751f91b4ccSFelipe Balbi static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
97647a1685fSDinh Nguyen 					struct usb_ctrlrequest *ctrl)
97747a1685fSDinh Nguyen {
9781f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
9791f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *ep;
98047a1685fSDinh Nguyen 	__le16 reply;
98147a1685fSDinh Nguyen 	int ret;
98247a1685fSDinh Nguyen 
98347a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
98447a1685fSDinh Nguyen 
98547a1685fSDinh Nguyen 	if (!ep0->dir_in) {
98647a1685fSDinh Nguyen 		dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
98747a1685fSDinh Nguyen 		return -EINVAL;
98847a1685fSDinh Nguyen 	}
98947a1685fSDinh Nguyen 
99047a1685fSDinh Nguyen 	switch (ctrl->bRequestType & USB_RECIP_MASK) {
99147a1685fSDinh Nguyen 	case USB_RECIP_DEVICE:
99247a1685fSDinh Nguyen 		reply = cpu_to_le16(0); /* bit 0 => self powered,
99347a1685fSDinh Nguyen 					 * bit 1 => remote wakeup */
99447a1685fSDinh Nguyen 		break;
99547a1685fSDinh Nguyen 
99647a1685fSDinh Nguyen 	case USB_RECIP_INTERFACE:
99747a1685fSDinh Nguyen 		/* currently, the data result should be zero */
99847a1685fSDinh Nguyen 		reply = cpu_to_le16(0);
99947a1685fSDinh Nguyen 		break;
100047a1685fSDinh Nguyen 
100147a1685fSDinh Nguyen 	case USB_RECIP_ENDPOINT:
100247a1685fSDinh Nguyen 		ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
100347a1685fSDinh Nguyen 		if (!ep)
100447a1685fSDinh Nguyen 			return -ENOENT;
100547a1685fSDinh Nguyen 
100647a1685fSDinh Nguyen 		reply = cpu_to_le16(ep->halted ? 1 : 0);
100747a1685fSDinh Nguyen 		break;
100847a1685fSDinh Nguyen 
100947a1685fSDinh Nguyen 	default:
101047a1685fSDinh Nguyen 		return 0;
101147a1685fSDinh Nguyen 	}
101247a1685fSDinh Nguyen 
101347a1685fSDinh Nguyen 	if (le16_to_cpu(ctrl->wLength) != 2)
101447a1685fSDinh Nguyen 		return -EINVAL;
101547a1685fSDinh Nguyen 
10161f91b4ccSFelipe Balbi 	ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
101747a1685fSDinh Nguyen 	if (ret) {
101847a1685fSDinh Nguyen 		dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
101947a1685fSDinh Nguyen 		return ret;
102047a1685fSDinh Nguyen 	}
102147a1685fSDinh Nguyen 
102247a1685fSDinh Nguyen 	return 1;
102347a1685fSDinh Nguyen }
102447a1685fSDinh Nguyen 
10251f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value);
102647a1685fSDinh Nguyen 
102747a1685fSDinh Nguyen /**
102847a1685fSDinh Nguyen  * get_ep_head - return the first request on the endpoint
102947a1685fSDinh Nguyen  * @hs_ep: The controller endpoint to get
103047a1685fSDinh Nguyen  *
103147a1685fSDinh Nguyen  * Get the first request on the endpoint.
103247a1685fSDinh Nguyen  */
10331f91b4ccSFelipe Balbi static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
103447a1685fSDinh Nguyen {
103547a1685fSDinh Nguyen 	if (list_empty(&hs_ep->queue))
103647a1685fSDinh Nguyen 		return NULL;
103747a1685fSDinh Nguyen 
10381f91b4ccSFelipe Balbi 	return list_first_entry(&hs_ep->queue, struct dwc2_hsotg_req, queue);
103947a1685fSDinh Nguyen }
104047a1685fSDinh Nguyen 
104147a1685fSDinh Nguyen /**
10421f91b4ccSFelipe Balbi  * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
104347a1685fSDinh Nguyen  * @hsotg: The device state
104447a1685fSDinh Nguyen  * @ctrl: USB control request
104547a1685fSDinh Nguyen  */
10461f91b4ccSFelipe Balbi static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
104747a1685fSDinh Nguyen 					 struct usb_ctrlrequest *ctrl)
104847a1685fSDinh Nguyen {
10491f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
10501f91b4ccSFelipe Balbi 	struct dwc2_hsotg_req *hs_req;
105147a1685fSDinh Nguyen 	bool restart;
105247a1685fSDinh Nguyen 	bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
10531f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *ep;
105447a1685fSDinh Nguyen 	int ret;
105547a1685fSDinh Nguyen 	bool halted;
10569e14d0a5SGregory Herrero 	u32 recip;
10579e14d0a5SGregory Herrero 	u32 wValue;
10589e14d0a5SGregory Herrero 	u32 wIndex;
105947a1685fSDinh Nguyen 
106047a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
106147a1685fSDinh Nguyen 		__func__, set ? "SET" : "CLEAR");
106247a1685fSDinh Nguyen 
10639e14d0a5SGregory Herrero 	wValue = le16_to_cpu(ctrl->wValue);
10649e14d0a5SGregory Herrero 	wIndex = le16_to_cpu(ctrl->wIndex);
10659e14d0a5SGregory Herrero 	recip = ctrl->bRequestType & USB_RECIP_MASK;
10669e14d0a5SGregory Herrero 
10679e14d0a5SGregory Herrero 	switch (recip) {
10689e14d0a5SGregory Herrero 	case USB_RECIP_DEVICE:
10699e14d0a5SGregory Herrero 		switch (wValue) {
10709e14d0a5SGregory Herrero 		case USB_DEVICE_TEST_MODE:
10719e14d0a5SGregory Herrero 			if ((wIndex & 0xff) != 0)
10729e14d0a5SGregory Herrero 				return -EINVAL;
10739e14d0a5SGregory Herrero 			if (!set)
10749e14d0a5SGregory Herrero 				return -EINVAL;
10759e14d0a5SGregory Herrero 
10769e14d0a5SGregory Herrero 			hsotg->test_mode = wIndex >> 8;
10771f91b4ccSFelipe Balbi 			ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
10789e14d0a5SGregory Herrero 			if (ret) {
10799e14d0a5SGregory Herrero 				dev_err(hsotg->dev,
10809e14d0a5SGregory Herrero 					"%s: failed to send reply\n", __func__);
10819e14d0a5SGregory Herrero 				return ret;
10829e14d0a5SGregory Herrero 			}
10839e14d0a5SGregory Herrero 			break;
10849e14d0a5SGregory Herrero 		default:
10859e14d0a5SGregory Herrero 			return -ENOENT;
10869e14d0a5SGregory Herrero 		}
10879e14d0a5SGregory Herrero 		break;
10889e14d0a5SGregory Herrero 
10899e14d0a5SGregory Herrero 	case USB_RECIP_ENDPOINT:
10909e14d0a5SGregory Herrero 		ep = ep_from_windex(hsotg, wIndex);
109147a1685fSDinh Nguyen 		if (!ep) {
109247a1685fSDinh Nguyen 			dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
10939e14d0a5SGregory Herrero 				__func__, wIndex);
109447a1685fSDinh Nguyen 			return -ENOENT;
109547a1685fSDinh Nguyen 		}
109647a1685fSDinh Nguyen 
10979e14d0a5SGregory Herrero 		switch (wValue) {
109847a1685fSDinh Nguyen 		case USB_ENDPOINT_HALT:
109947a1685fSDinh Nguyen 			halted = ep->halted;
110047a1685fSDinh Nguyen 
11011f91b4ccSFelipe Balbi 			dwc2_hsotg_ep_sethalt(&ep->ep, set);
110247a1685fSDinh Nguyen 
11031f91b4ccSFelipe Balbi 			ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
110447a1685fSDinh Nguyen 			if (ret) {
110547a1685fSDinh Nguyen 				dev_err(hsotg->dev,
110647a1685fSDinh Nguyen 					"%s: failed to send reply\n", __func__);
110747a1685fSDinh Nguyen 				return ret;
110847a1685fSDinh Nguyen 			}
110947a1685fSDinh Nguyen 
111047a1685fSDinh Nguyen 			/*
111147a1685fSDinh Nguyen 			 * we have to complete all requests for ep if it was
111247a1685fSDinh Nguyen 			 * halted, and the halt was cleared by CLEAR_FEATURE
111347a1685fSDinh Nguyen 			 */
111447a1685fSDinh Nguyen 
111547a1685fSDinh Nguyen 			if (!set && halted) {
111647a1685fSDinh Nguyen 				/*
111747a1685fSDinh Nguyen 				 * If we have request in progress,
111847a1685fSDinh Nguyen 				 * then complete it
111947a1685fSDinh Nguyen 				 */
112047a1685fSDinh Nguyen 				if (ep->req) {
112147a1685fSDinh Nguyen 					hs_req = ep->req;
112247a1685fSDinh Nguyen 					ep->req = NULL;
112347a1685fSDinh Nguyen 					list_del_init(&hs_req->queue);
1124c00dd4a6SGregory Herrero 					if (hs_req->req.complete) {
1125c00dd4a6SGregory Herrero 						spin_unlock(&hsotg->lock);
1126c00dd4a6SGregory Herrero 						usb_gadget_giveback_request(
1127c00dd4a6SGregory Herrero 							&ep->ep, &hs_req->req);
1128c00dd4a6SGregory Herrero 						spin_lock(&hsotg->lock);
1129c00dd4a6SGregory Herrero 					}
113047a1685fSDinh Nguyen 				}
113147a1685fSDinh Nguyen 
113247a1685fSDinh Nguyen 				/* If we have pending request, then start it */
1133c00dd4a6SGregory Herrero 				if (!ep->req) {
113447a1685fSDinh Nguyen 					restart = !list_empty(&ep->queue);
113547a1685fSDinh Nguyen 					if (restart) {
113647a1685fSDinh Nguyen 						hs_req = get_ep_head(ep);
11371f91b4ccSFelipe Balbi 						dwc2_hsotg_start_req(hsotg, ep,
113847a1685fSDinh Nguyen 								hs_req, false);
113947a1685fSDinh Nguyen 					}
114047a1685fSDinh Nguyen 				}
1141c00dd4a6SGregory Herrero 			}
114247a1685fSDinh Nguyen 
114347a1685fSDinh Nguyen 			break;
114447a1685fSDinh Nguyen 
114547a1685fSDinh Nguyen 		default:
114647a1685fSDinh Nguyen 			return -ENOENT;
114747a1685fSDinh Nguyen 		}
11489e14d0a5SGregory Herrero 		break;
11499e14d0a5SGregory Herrero 	default:
11509e14d0a5SGregory Herrero 		return -ENOENT;
11519e14d0a5SGregory Herrero 	}
115247a1685fSDinh Nguyen 	return 1;
115347a1685fSDinh Nguyen }
115447a1685fSDinh Nguyen 
11551f91b4ccSFelipe Balbi static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
115647a1685fSDinh Nguyen 
115747a1685fSDinh Nguyen /**
11581f91b4ccSFelipe Balbi  * dwc2_hsotg_stall_ep0 - stall ep0
115947a1685fSDinh Nguyen  * @hsotg: The device state
116047a1685fSDinh Nguyen  *
116147a1685fSDinh Nguyen  * Set stall for ep0 as response for setup request.
116247a1685fSDinh Nguyen  */
11631f91b4ccSFelipe Balbi static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
1164e9ebe7c3SJingoo Han {
11651f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
116647a1685fSDinh Nguyen 	u32 reg;
116747a1685fSDinh Nguyen 	u32 ctrl;
116847a1685fSDinh Nguyen 
116947a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
117047a1685fSDinh Nguyen 	reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
117147a1685fSDinh Nguyen 
117247a1685fSDinh Nguyen 	/*
117347a1685fSDinh Nguyen 	 * DxEPCTL_Stall will be cleared by EP once it has
117447a1685fSDinh Nguyen 	 * taken effect, so no need to clear later.
117547a1685fSDinh Nguyen 	 */
117647a1685fSDinh Nguyen 
117795c8bc36SAntti Seppälä 	ctrl = dwc2_readl(hsotg->regs + reg);
117847a1685fSDinh Nguyen 	ctrl |= DXEPCTL_STALL;
117947a1685fSDinh Nguyen 	ctrl |= DXEPCTL_CNAK;
118095c8bc36SAntti Seppälä 	dwc2_writel(ctrl, hsotg->regs + reg);
118147a1685fSDinh Nguyen 
118247a1685fSDinh Nguyen 	dev_dbg(hsotg->dev,
118347a1685fSDinh Nguyen 		"written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
118495c8bc36SAntti Seppälä 		ctrl, reg, dwc2_readl(hsotg->regs + reg));
118547a1685fSDinh Nguyen 
118647a1685fSDinh Nguyen 	 /*
118747a1685fSDinh Nguyen 	  * complete won't be called, so we enqueue
118847a1685fSDinh Nguyen 	  * setup request here
118947a1685fSDinh Nguyen 	  */
11901f91b4ccSFelipe Balbi 	 dwc2_hsotg_enqueue_setup(hsotg);
119147a1685fSDinh Nguyen }
119247a1685fSDinh Nguyen 
119347a1685fSDinh Nguyen /**
11941f91b4ccSFelipe Balbi  * dwc2_hsotg_process_control - process a control request
119547a1685fSDinh Nguyen  * @hsotg: The device state
119647a1685fSDinh Nguyen  * @ctrl: The control request received
119747a1685fSDinh Nguyen  *
119847a1685fSDinh Nguyen  * The controller has received the SETUP phase of a control request, and
119947a1685fSDinh Nguyen  * needs to work out what to do next (and whether to pass it on to the
120047a1685fSDinh Nguyen  * gadget driver).
120147a1685fSDinh Nguyen  */
12021f91b4ccSFelipe Balbi static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
120347a1685fSDinh Nguyen 				      struct usb_ctrlrequest *ctrl)
120447a1685fSDinh Nguyen {
12051f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
120647a1685fSDinh Nguyen 	int ret = 0;
120747a1685fSDinh Nguyen 	u32 dcfg;
120847a1685fSDinh Nguyen 
1209e525e743SMian Yousaf Kaukab 	dev_dbg(hsotg->dev,
1210e525e743SMian Yousaf Kaukab 		"ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1211e525e743SMian Yousaf Kaukab 		ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1212e525e743SMian Yousaf Kaukab 		ctrl->wIndex, ctrl->wLength);
121347a1685fSDinh Nguyen 
1214fe0b94abSMian Yousaf Kaukab 	if (ctrl->wLength == 0) {
121547a1685fSDinh Nguyen 		ep0->dir_in = 1;
1216fe0b94abSMian Yousaf Kaukab 		hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1217fe0b94abSMian Yousaf Kaukab 	} else if (ctrl->bRequestType & USB_DIR_IN) {
1218fe0b94abSMian Yousaf Kaukab 		ep0->dir_in = 1;
1219fe0b94abSMian Yousaf Kaukab 		hsotg->ep0_state = DWC2_EP0_DATA_IN;
1220fe0b94abSMian Yousaf Kaukab 	} else {
1221fe0b94abSMian Yousaf Kaukab 		ep0->dir_in = 0;
1222fe0b94abSMian Yousaf Kaukab 		hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1223fe0b94abSMian Yousaf Kaukab 	}
122447a1685fSDinh Nguyen 
122547a1685fSDinh Nguyen 	if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
122647a1685fSDinh Nguyen 		switch (ctrl->bRequest) {
122747a1685fSDinh Nguyen 		case USB_REQ_SET_ADDRESS:
12286d713c15SMian Yousaf Kaukab 			hsotg->connected = 1;
122995c8bc36SAntti Seppälä 			dcfg = dwc2_readl(hsotg->regs + DCFG);
123047a1685fSDinh Nguyen 			dcfg &= ~DCFG_DEVADDR_MASK;
1231d5dbd3f7SPaul Zimmerman 			dcfg |= (le16_to_cpu(ctrl->wValue) <<
1232d5dbd3f7SPaul Zimmerman 				 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
123395c8bc36SAntti Seppälä 			dwc2_writel(dcfg, hsotg->regs + DCFG);
123447a1685fSDinh Nguyen 
123547a1685fSDinh Nguyen 			dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
123647a1685fSDinh Nguyen 
12371f91b4ccSFelipe Balbi 			ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
123847a1685fSDinh Nguyen 			return;
123947a1685fSDinh Nguyen 
124047a1685fSDinh Nguyen 		case USB_REQ_GET_STATUS:
12411f91b4ccSFelipe Balbi 			ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
124247a1685fSDinh Nguyen 			break;
124347a1685fSDinh Nguyen 
124447a1685fSDinh Nguyen 		case USB_REQ_CLEAR_FEATURE:
124547a1685fSDinh Nguyen 		case USB_REQ_SET_FEATURE:
12461f91b4ccSFelipe Balbi 			ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
124747a1685fSDinh Nguyen 			break;
124847a1685fSDinh Nguyen 		}
124947a1685fSDinh Nguyen 	}
125047a1685fSDinh Nguyen 
125147a1685fSDinh Nguyen 	/* as a fallback, try delivering it to the driver to deal with */
125247a1685fSDinh Nguyen 
125347a1685fSDinh Nguyen 	if (ret == 0 && hsotg->driver) {
125447a1685fSDinh Nguyen 		spin_unlock(&hsotg->lock);
125547a1685fSDinh Nguyen 		ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
125647a1685fSDinh Nguyen 		spin_lock(&hsotg->lock);
125747a1685fSDinh Nguyen 		if (ret < 0)
125847a1685fSDinh Nguyen 			dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
125947a1685fSDinh Nguyen 	}
126047a1685fSDinh Nguyen 
126147a1685fSDinh Nguyen 	/*
126247a1685fSDinh Nguyen 	 * the request is either unhandlable, or is not formatted correctly
126347a1685fSDinh Nguyen 	 * so respond with a STALL for the status stage to indicate failure.
126447a1685fSDinh Nguyen 	 */
126547a1685fSDinh Nguyen 
126647a1685fSDinh Nguyen 	if (ret < 0)
12671f91b4ccSFelipe Balbi 		dwc2_hsotg_stall_ep0(hsotg);
126847a1685fSDinh Nguyen }
126947a1685fSDinh Nguyen 
127047a1685fSDinh Nguyen /**
12711f91b4ccSFelipe Balbi  * dwc2_hsotg_complete_setup - completion of a setup transfer
127247a1685fSDinh Nguyen  * @ep: The endpoint the request was on.
127347a1685fSDinh Nguyen  * @req: The request completed.
127447a1685fSDinh Nguyen  *
127547a1685fSDinh Nguyen  * Called on completion of any requests the driver itself submitted for
127647a1685fSDinh Nguyen  * EP0 setup packets
127747a1685fSDinh Nguyen  */
12781f91b4ccSFelipe Balbi static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
127947a1685fSDinh Nguyen 				     struct usb_request *req)
128047a1685fSDinh Nguyen {
12811f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
1282941fcce4SDinh Nguyen 	struct dwc2_hsotg *hsotg = hs_ep->parent;
128347a1685fSDinh Nguyen 
128447a1685fSDinh Nguyen 	if (req->status < 0) {
128547a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
128647a1685fSDinh Nguyen 		return;
128747a1685fSDinh Nguyen 	}
128847a1685fSDinh Nguyen 
128947a1685fSDinh Nguyen 	spin_lock(&hsotg->lock);
129047a1685fSDinh Nguyen 	if (req->actual == 0)
12911f91b4ccSFelipe Balbi 		dwc2_hsotg_enqueue_setup(hsotg);
129247a1685fSDinh Nguyen 	else
12931f91b4ccSFelipe Balbi 		dwc2_hsotg_process_control(hsotg, req->buf);
129447a1685fSDinh Nguyen 	spin_unlock(&hsotg->lock);
129547a1685fSDinh Nguyen }
129647a1685fSDinh Nguyen 
129747a1685fSDinh Nguyen /**
12981f91b4ccSFelipe Balbi  * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
129947a1685fSDinh Nguyen  * @hsotg: The device state.
130047a1685fSDinh Nguyen  *
130147a1685fSDinh Nguyen  * Enqueue a request on EP0 if necessary to received any SETUP packets
130247a1685fSDinh Nguyen  * received from the host.
130347a1685fSDinh Nguyen  */
13041f91b4ccSFelipe Balbi static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
130547a1685fSDinh Nguyen {
130647a1685fSDinh Nguyen 	struct usb_request *req = hsotg->ctrl_req;
13071f91b4ccSFelipe Balbi 	struct dwc2_hsotg_req *hs_req = our_req(req);
130847a1685fSDinh Nguyen 	int ret;
130947a1685fSDinh Nguyen 
131047a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
131147a1685fSDinh Nguyen 
131247a1685fSDinh Nguyen 	req->zero = 0;
131347a1685fSDinh Nguyen 	req->length = 8;
131447a1685fSDinh Nguyen 	req->buf = hsotg->ctrl_buff;
13151f91b4ccSFelipe Balbi 	req->complete = dwc2_hsotg_complete_setup;
131647a1685fSDinh Nguyen 
131747a1685fSDinh Nguyen 	if (!list_empty(&hs_req->queue)) {
131847a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
131947a1685fSDinh Nguyen 		return;
132047a1685fSDinh Nguyen 	}
132147a1685fSDinh Nguyen 
1322c6f5c050SMian Yousaf Kaukab 	hsotg->eps_out[0]->dir_in = 0;
13238a20fa45SMian Yousaf Kaukab 	hsotg->eps_out[0]->send_zlp = 0;
1324fe0b94abSMian Yousaf Kaukab 	hsotg->ep0_state = DWC2_EP0_SETUP;
132547a1685fSDinh Nguyen 
13261f91b4ccSFelipe Balbi 	ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
132747a1685fSDinh Nguyen 	if (ret < 0) {
132847a1685fSDinh Nguyen 		dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
132947a1685fSDinh Nguyen 		/*
133047a1685fSDinh Nguyen 		 * Don't think there's much we can do other than watch the
133147a1685fSDinh Nguyen 		 * driver fail.
133247a1685fSDinh Nguyen 		 */
133347a1685fSDinh Nguyen 	}
133447a1685fSDinh Nguyen }
133547a1685fSDinh Nguyen 
13361f91b4ccSFelipe Balbi static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
13371f91b4ccSFelipe Balbi 					struct dwc2_hsotg_ep *hs_ep)
1338fe0b94abSMian Yousaf Kaukab {
1339fe0b94abSMian Yousaf Kaukab 	u32 ctrl;
1340fe0b94abSMian Yousaf Kaukab 	u8 index = hs_ep->index;
1341fe0b94abSMian Yousaf Kaukab 	u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1342fe0b94abSMian Yousaf Kaukab 	u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1343fe0b94abSMian Yousaf Kaukab 
1344ccb34a91SMian Yousaf Kaukab 	if (hs_ep->dir_in)
1345ccb34a91SMian Yousaf Kaukab 		dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
1346ccb34a91SMian Yousaf Kaukab 									index);
1347ccb34a91SMian Yousaf Kaukab 	else
1348ccb34a91SMian Yousaf Kaukab 		dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
1349ccb34a91SMian Yousaf Kaukab 									index);
1350fe0b94abSMian Yousaf Kaukab 
135195c8bc36SAntti Seppälä 	dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1352fe0b94abSMian Yousaf Kaukab 		    DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1353fe0b94abSMian Yousaf Kaukab 		    epsiz_reg);
1354fe0b94abSMian Yousaf Kaukab 
135595c8bc36SAntti Seppälä 	ctrl = dwc2_readl(hsotg->regs + epctl_reg);
1356fe0b94abSMian Yousaf Kaukab 	ctrl |= DXEPCTL_CNAK;  /* clear NAK set by core */
1357fe0b94abSMian Yousaf Kaukab 	ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1358fe0b94abSMian Yousaf Kaukab 	ctrl |= DXEPCTL_USBACTEP;
135995c8bc36SAntti Seppälä 	dwc2_writel(ctrl, hsotg->regs + epctl_reg);
1360fe0b94abSMian Yousaf Kaukab }
1361fe0b94abSMian Yousaf Kaukab 
136247a1685fSDinh Nguyen /**
13631f91b4ccSFelipe Balbi  * dwc2_hsotg_complete_request - complete a request given to us
136447a1685fSDinh Nguyen  * @hsotg: The device state.
136547a1685fSDinh Nguyen  * @hs_ep: The endpoint the request was on.
136647a1685fSDinh Nguyen  * @hs_req: The request to complete.
136747a1685fSDinh Nguyen  * @result: The result code (0 => Ok, otherwise errno)
136847a1685fSDinh Nguyen  *
136947a1685fSDinh Nguyen  * The given request has finished, so call the necessary completion
137047a1685fSDinh Nguyen  * if it has one and then look to see if we can start a new request
137147a1685fSDinh Nguyen  * on the endpoint.
137247a1685fSDinh Nguyen  *
137347a1685fSDinh Nguyen  * Note, expects the ep to already be locked as appropriate.
137447a1685fSDinh Nguyen  */
13751f91b4ccSFelipe Balbi static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
13761f91b4ccSFelipe Balbi 				       struct dwc2_hsotg_ep *hs_ep,
13771f91b4ccSFelipe Balbi 				       struct dwc2_hsotg_req *hs_req,
137847a1685fSDinh Nguyen 				       int result)
137947a1685fSDinh Nguyen {
138047a1685fSDinh Nguyen 	bool restart;
138147a1685fSDinh Nguyen 
138247a1685fSDinh Nguyen 	if (!hs_req) {
138347a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
138447a1685fSDinh Nguyen 		return;
138547a1685fSDinh Nguyen 	}
138647a1685fSDinh Nguyen 
138747a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
138847a1685fSDinh Nguyen 		hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
138947a1685fSDinh Nguyen 
139047a1685fSDinh Nguyen 	/*
139147a1685fSDinh Nguyen 	 * only replace the status if we've not already set an error
139247a1685fSDinh Nguyen 	 * from a previous transaction
139347a1685fSDinh Nguyen 	 */
139447a1685fSDinh Nguyen 
139547a1685fSDinh Nguyen 	if (hs_req->req.status == -EINPROGRESS)
139647a1685fSDinh Nguyen 		hs_req->req.status = result;
139747a1685fSDinh Nguyen 
13981f91b4ccSFelipe Balbi 	dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
13997d24c1b5SMian Yousaf Kaukab 
140047a1685fSDinh Nguyen 	hs_ep->req = NULL;
140147a1685fSDinh Nguyen 	list_del_init(&hs_req->queue);
140247a1685fSDinh Nguyen 
140347a1685fSDinh Nguyen 	if (using_dma(hsotg))
14041f91b4ccSFelipe Balbi 		dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
140547a1685fSDinh Nguyen 
140647a1685fSDinh Nguyen 	/*
140747a1685fSDinh Nguyen 	 * call the complete request with the locks off, just in case the
140847a1685fSDinh Nguyen 	 * request tries to queue more work for this endpoint.
140947a1685fSDinh Nguyen 	 */
141047a1685fSDinh Nguyen 
141147a1685fSDinh Nguyen 	if (hs_req->req.complete) {
141247a1685fSDinh Nguyen 		spin_unlock(&hsotg->lock);
1413304f7e5eSMichal Sojka 		usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
141447a1685fSDinh Nguyen 		spin_lock(&hsotg->lock);
141547a1685fSDinh Nguyen 	}
141647a1685fSDinh Nguyen 
141747a1685fSDinh Nguyen 	/*
141847a1685fSDinh Nguyen 	 * Look to see if there is anything else to do. Note, the completion
141947a1685fSDinh Nguyen 	 * of the previous request may have caused a new request to be started
142047a1685fSDinh Nguyen 	 * so be careful when doing this.
142147a1685fSDinh Nguyen 	 */
142247a1685fSDinh Nguyen 
142347a1685fSDinh Nguyen 	if (!hs_ep->req && result >= 0) {
142447a1685fSDinh Nguyen 		restart = !list_empty(&hs_ep->queue);
142547a1685fSDinh Nguyen 		if (restart) {
142647a1685fSDinh Nguyen 			hs_req = get_ep_head(hs_ep);
14271f91b4ccSFelipe Balbi 			dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
142847a1685fSDinh Nguyen 		}
142947a1685fSDinh Nguyen 	}
143047a1685fSDinh Nguyen }
143147a1685fSDinh Nguyen 
143247a1685fSDinh Nguyen /**
14331f91b4ccSFelipe Balbi  * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
143447a1685fSDinh Nguyen  * @hsotg: The device state.
143547a1685fSDinh Nguyen  * @ep_idx: The endpoint index for the data
143647a1685fSDinh Nguyen  * @size: The size of data in the fifo, in bytes
143747a1685fSDinh Nguyen  *
143847a1685fSDinh Nguyen  * The FIFO status shows there is data to read from the FIFO for a given
143947a1685fSDinh Nguyen  * endpoint, so sort out whether we need to read the data into a request
144047a1685fSDinh Nguyen  * that has been made for that endpoint.
144147a1685fSDinh Nguyen  */
14421f91b4ccSFelipe Balbi static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
144347a1685fSDinh Nguyen {
14441f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
14451f91b4ccSFelipe Balbi 	struct dwc2_hsotg_req *hs_req = hs_ep->req;
144647a1685fSDinh Nguyen 	void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
144747a1685fSDinh Nguyen 	int to_read;
144847a1685fSDinh Nguyen 	int max_req;
144947a1685fSDinh Nguyen 	int read_ptr;
145047a1685fSDinh Nguyen 
145147a1685fSDinh Nguyen 
145247a1685fSDinh Nguyen 	if (!hs_req) {
145395c8bc36SAntti Seppälä 		u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
145447a1685fSDinh Nguyen 		int ptr;
145547a1685fSDinh Nguyen 
14566b448af4SRobert Baldyga 		dev_dbg(hsotg->dev,
145747a1685fSDinh Nguyen 			 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
145847a1685fSDinh Nguyen 			 __func__, size, ep_idx, epctl);
145947a1685fSDinh Nguyen 
146047a1685fSDinh Nguyen 		/* dump the data from the FIFO, we've nothing we can do */
146147a1685fSDinh Nguyen 		for (ptr = 0; ptr < size; ptr += 4)
146295c8bc36SAntti Seppälä 			(void)dwc2_readl(fifo);
146347a1685fSDinh Nguyen 
146447a1685fSDinh Nguyen 		return;
146547a1685fSDinh Nguyen 	}
146647a1685fSDinh Nguyen 
146747a1685fSDinh Nguyen 	to_read = size;
146847a1685fSDinh Nguyen 	read_ptr = hs_req->req.actual;
146947a1685fSDinh Nguyen 	max_req = hs_req->req.length - read_ptr;
147047a1685fSDinh Nguyen 
147147a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
147247a1685fSDinh Nguyen 		__func__, to_read, max_req, read_ptr, hs_req->req.length);
147347a1685fSDinh Nguyen 
147447a1685fSDinh Nguyen 	if (to_read > max_req) {
147547a1685fSDinh Nguyen 		/*
147647a1685fSDinh Nguyen 		 * more data appeared than we where willing
147747a1685fSDinh Nguyen 		 * to deal with in this request.
147847a1685fSDinh Nguyen 		 */
147947a1685fSDinh Nguyen 
148047a1685fSDinh Nguyen 		/* currently we don't deal this */
148147a1685fSDinh Nguyen 		WARN_ON_ONCE(1);
148247a1685fSDinh Nguyen 	}
148347a1685fSDinh Nguyen 
148447a1685fSDinh Nguyen 	hs_ep->total_data += to_read;
148547a1685fSDinh Nguyen 	hs_req->req.actual += to_read;
148647a1685fSDinh Nguyen 	to_read = DIV_ROUND_UP(to_read, 4);
148747a1685fSDinh Nguyen 
148847a1685fSDinh Nguyen 	/*
148947a1685fSDinh Nguyen 	 * note, we might over-write the buffer end by 3 bytes depending on
149047a1685fSDinh Nguyen 	 * alignment of the data.
149147a1685fSDinh Nguyen 	 */
149247a1685fSDinh Nguyen 	ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
149347a1685fSDinh Nguyen }
149447a1685fSDinh Nguyen 
149547a1685fSDinh Nguyen /**
14961f91b4ccSFelipe Balbi  * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
149747a1685fSDinh Nguyen  * @hsotg: The device instance
1498fe0b94abSMian Yousaf Kaukab  * @dir_in: If IN zlp
149947a1685fSDinh Nguyen  *
150047a1685fSDinh Nguyen  * Generate a zero-length IN packet request for terminating a SETUP
150147a1685fSDinh Nguyen  * transaction.
150247a1685fSDinh Nguyen  *
150347a1685fSDinh Nguyen  * Note, since we don't write any data to the TxFIFO, then it is
150447a1685fSDinh Nguyen  * currently believed that we do not need to wait for any space in
150547a1685fSDinh Nguyen  * the TxFIFO.
150647a1685fSDinh Nguyen  */
15071f91b4ccSFelipe Balbi static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
150847a1685fSDinh Nguyen {
1509c6f5c050SMian Yousaf Kaukab 	/* eps_out[0] is used in both directions */
1510fe0b94abSMian Yousaf Kaukab 	hsotg->eps_out[0]->dir_in = dir_in;
1511fe0b94abSMian Yousaf Kaukab 	hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
151247a1685fSDinh Nguyen 
15131f91b4ccSFelipe Balbi 	dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
151447a1685fSDinh Nguyen }
151547a1685fSDinh Nguyen 
151647a1685fSDinh Nguyen /**
15171f91b4ccSFelipe Balbi  * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
151847a1685fSDinh Nguyen  * @hsotg: The device instance
151947a1685fSDinh Nguyen  * @epnum: The endpoint received from
152047a1685fSDinh Nguyen  *
152147a1685fSDinh Nguyen  * The RXFIFO has delivered an OutDone event, which means that the data
152247a1685fSDinh Nguyen  * transfer for an OUT endpoint has been completed, either by a short
152347a1685fSDinh Nguyen  * packet or by the finish of a transfer.
152447a1685fSDinh Nguyen  */
15251f91b4ccSFelipe Balbi static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
152647a1685fSDinh Nguyen {
152795c8bc36SAntti Seppälä 	u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
15281f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
15291f91b4ccSFelipe Balbi 	struct dwc2_hsotg_req *hs_req = hs_ep->req;
153047a1685fSDinh Nguyen 	struct usb_request *req = &hs_req->req;
153147a1685fSDinh Nguyen 	unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
153247a1685fSDinh Nguyen 	int result = 0;
153347a1685fSDinh Nguyen 
153447a1685fSDinh Nguyen 	if (!hs_req) {
153547a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
153647a1685fSDinh Nguyen 		return;
153747a1685fSDinh Nguyen 	}
153847a1685fSDinh Nguyen 
1539fe0b94abSMian Yousaf Kaukab 	if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
1540fe0b94abSMian Yousaf Kaukab 		dev_dbg(hsotg->dev, "zlp packet received\n");
15411f91b4ccSFelipe Balbi 		dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
15421f91b4ccSFelipe Balbi 		dwc2_hsotg_enqueue_setup(hsotg);
1543fe0b94abSMian Yousaf Kaukab 		return;
1544fe0b94abSMian Yousaf Kaukab 	}
1545fe0b94abSMian Yousaf Kaukab 
154647a1685fSDinh Nguyen 	if (using_dma(hsotg)) {
154747a1685fSDinh Nguyen 		unsigned size_done;
154847a1685fSDinh Nguyen 
154947a1685fSDinh Nguyen 		/*
155047a1685fSDinh Nguyen 		 * Calculate the size of the transfer by checking how much
155147a1685fSDinh Nguyen 		 * is left in the endpoint size register and then working it
155247a1685fSDinh Nguyen 		 * out from the amount we loaded for the transfer.
155347a1685fSDinh Nguyen 		 *
155447a1685fSDinh Nguyen 		 * We need to do this as DMA pointers are always 32bit aligned
155547a1685fSDinh Nguyen 		 * so may overshoot/undershoot the transfer.
155647a1685fSDinh Nguyen 		 */
155747a1685fSDinh Nguyen 
155847a1685fSDinh Nguyen 		size_done = hs_ep->size_loaded - size_left;
155947a1685fSDinh Nguyen 		size_done += hs_ep->last_load;
156047a1685fSDinh Nguyen 
156147a1685fSDinh Nguyen 		req->actual = size_done;
156247a1685fSDinh Nguyen 	}
156347a1685fSDinh Nguyen 
156447a1685fSDinh Nguyen 	/* if there is more request to do, schedule new transfer */
156547a1685fSDinh Nguyen 	if (req->actual < req->length && size_left == 0) {
15661f91b4ccSFelipe Balbi 		dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
156747a1685fSDinh Nguyen 		return;
156847a1685fSDinh Nguyen 	}
156947a1685fSDinh Nguyen 
157047a1685fSDinh Nguyen 	if (req->actual < req->length && req->short_not_ok) {
157147a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
157247a1685fSDinh Nguyen 			__func__, req->actual, req->length);
157347a1685fSDinh Nguyen 
157447a1685fSDinh Nguyen 		/*
157547a1685fSDinh Nguyen 		 * todo - what should we return here? there's no one else
157647a1685fSDinh Nguyen 		 * even bothering to check the status.
157747a1685fSDinh Nguyen 		 */
157847a1685fSDinh Nguyen 	}
157947a1685fSDinh Nguyen 
1580fe0b94abSMian Yousaf Kaukab 	if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
1581fe0b94abSMian Yousaf Kaukab 		/* Move to STATUS IN */
15821f91b4ccSFelipe Balbi 		dwc2_hsotg_ep0_zlp(hsotg, true);
1583fe0b94abSMian Yousaf Kaukab 		return;
158447a1685fSDinh Nguyen 	}
158547a1685fSDinh Nguyen 
15861f91b4ccSFelipe Balbi 	dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
158747a1685fSDinh Nguyen }
158847a1685fSDinh Nguyen 
158947a1685fSDinh Nguyen /**
15901f91b4ccSFelipe Balbi  * dwc2_hsotg_read_frameno - read current frame number
159147a1685fSDinh Nguyen  * @hsotg: The device instance
159247a1685fSDinh Nguyen  *
159347a1685fSDinh Nguyen  * Return the current frame number
159447a1685fSDinh Nguyen  */
15951f91b4ccSFelipe Balbi static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
159647a1685fSDinh Nguyen {
159747a1685fSDinh Nguyen 	u32 dsts;
159847a1685fSDinh Nguyen 
159995c8bc36SAntti Seppälä 	dsts = dwc2_readl(hsotg->regs + DSTS);
160047a1685fSDinh Nguyen 	dsts &= DSTS_SOFFN_MASK;
160147a1685fSDinh Nguyen 	dsts >>= DSTS_SOFFN_SHIFT;
160247a1685fSDinh Nguyen 
160347a1685fSDinh Nguyen 	return dsts;
160447a1685fSDinh Nguyen }
160547a1685fSDinh Nguyen 
160647a1685fSDinh Nguyen /**
16071f91b4ccSFelipe Balbi  * dwc2_hsotg_handle_rx - RX FIFO has data
160847a1685fSDinh Nguyen  * @hsotg: The device instance
160947a1685fSDinh Nguyen  *
161047a1685fSDinh Nguyen  * The IRQ handler has detected that the RX FIFO has some data in it
161147a1685fSDinh Nguyen  * that requires processing, so find out what is in there and do the
161247a1685fSDinh Nguyen  * appropriate read.
161347a1685fSDinh Nguyen  *
161447a1685fSDinh Nguyen  * The RXFIFO is a true FIFO, the packets coming out are still in packet
161547a1685fSDinh Nguyen  * chunks, so if you have x packets received on an endpoint you'll get x
161647a1685fSDinh Nguyen  * FIFO events delivered, each with a packet's worth of data in it.
161747a1685fSDinh Nguyen  *
161847a1685fSDinh Nguyen  * When using DMA, we should not be processing events from the RXFIFO
161947a1685fSDinh Nguyen  * as the actual data should be sent to the memory directly and we turn
162047a1685fSDinh Nguyen  * on the completion interrupts to get notifications of transfer completion.
162147a1685fSDinh Nguyen  */
16221f91b4ccSFelipe Balbi static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
162347a1685fSDinh Nguyen {
162495c8bc36SAntti Seppälä 	u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
162547a1685fSDinh Nguyen 	u32 epnum, status, size;
162647a1685fSDinh Nguyen 
162747a1685fSDinh Nguyen 	WARN_ON(using_dma(hsotg));
162847a1685fSDinh Nguyen 
162947a1685fSDinh Nguyen 	epnum = grxstsr & GRXSTS_EPNUM_MASK;
163047a1685fSDinh Nguyen 	status = grxstsr & GRXSTS_PKTSTS_MASK;
163147a1685fSDinh Nguyen 
163247a1685fSDinh Nguyen 	size = grxstsr & GRXSTS_BYTECNT_MASK;
163347a1685fSDinh Nguyen 	size >>= GRXSTS_BYTECNT_SHIFT;
163447a1685fSDinh Nguyen 
163547a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
163647a1685fSDinh Nguyen 			__func__, grxstsr, size, epnum);
163747a1685fSDinh Nguyen 
163847a1685fSDinh Nguyen 	switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
163947a1685fSDinh Nguyen 	case GRXSTS_PKTSTS_GLOBALOUTNAK:
164047a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
164147a1685fSDinh Nguyen 		break;
164247a1685fSDinh Nguyen 
164347a1685fSDinh Nguyen 	case GRXSTS_PKTSTS_OUTDONE:
164447a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
16451f91b4ccSFelipe Balbi 			dwc2_hsotg_read_frameno(hsotg));
164647a1685fSDinh Nguyen 
164747a1685fSDinh Nguyen 		if (!using_dma(hsotg))
16481f91b4ccSFelipe Balbi 			dwc2_hsotg_handle_outdone(hsotg, epnum);
164947a1685fSDinh Nguyen 		break;
165047a1685fSDinh Nguyen 
165147a1685fSDinh Nguyen 	case GRXSTS_PKTSTS_SETUPDONE:
165247a1685fSDinh Nguyen 		dev_dbg(hsotg->dev,
165347a1685fSDinh Nguyen 			"SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
16541f91b4ccSFelipe Balbi 			dwc2_hsotg_read_frameno(hsotg),
165595c8bc36SAntti Seppälä 			dwc2_readl(hsotg->regs + DOEPCTL(0)));
1656fe0b94abSMian Yousaf Kaukab 		/*
16571f91b4ccSFelipe Balbi 		 * Call dwc2_hsotg_handle_outdone here if it was not called from
1658fe0b94abSMian Yousaf Kaukab 		 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
1659fe0b94abSMian Yousaf Kaukab 		 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
1660fe0b94abSMian Yousaf Kaukab 		 */
1661fe0b94abSMian Yousaf Kaukab 		if (hsotg->ep0_state == DWC2_EP0_SETUP)
16621f91b4ccSFelipe Balbi 			dwc2_hsotg_handle_outdone(hsotg, epnum);
166347a1685fSDinh Nguyen 		break;
166447a1685fSDinh Nguyen 
166547a1685fSDinh Nguyen 	case GRXSTS_PKTSTS_OUTRX:
16661f91b4ccSFelipe Balbi 		dwc2_hsotg_rx_data(hsotg, epnum, size);
166747a1685fSDinh Nguyen 		break;
166847a1685fSDinh Nguyen 
166947a1685fSDinh Nguyen 	case GRXSTS_PKTSTS_SETUPRX:
167047a1685fSDinh Nguyen 		dev_dbg(hsotg->dev,
167147a1685fSDinh Nguyen 			"SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
16721f91b4ccSFelipe Balbi 			dwc2_hsotg_read_frameno(hsotg),
167395c8bc36SAntti Seppälä 			dwc2_readl(hsotg->regs + DOEPCTL(0)));
167447a1685fSDinh Nguyen 
1675fe0b94abSMian Yousaf Kaukab 		WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
1676fe0b94abSMian Yousaf Kaukab 
16771f91b4ccSFelipe Balbi 		dwc2_hsotg_rx_data(hsotg, epnum, size);
167847a1685fSDinh Nguyen 		break;
167947a1685fSDinh Nguyen 
168047a1685fSDinh Nguyen 	default:
168147a1685fSDinh Nguyen 		dev_warn(hsotg->dev, "%s: unknown status %08x\n",
168247a1685fSDinh Nguyen 			 __func__, grxstsr);
168347a1685fSDinh Nguyen 
16841f91b4ccSFelipe Balbi 		dwc2_hsotg_dump(hsotg);
168547a1685fSDinh Nguyen 		break;
168647a1685fSDinh Nguyen 	}
168747a1685fSDinh Nguyen }
168847a1685fSDinh Nguyen 
168947a1685fSDinh Nguyen /**
16901f91b4ccSFelipe Balbi  * dwc2_hsotg_ep0_mps - turn max packet size into register setting
169147a1685fSDinh Nguyen  * @mps: The maximum packet size in bytes.
169247a1685fSDinh Nguyen  */
16931f91b4ccSFelipe Balbi static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
169447a1685fSDinh Nguyen {
169547a1685fSDinh Nguyen 	switch (mps) {
169647a1685fSDinh Nguyen 	case 64:
169747a1685fSDinh Nguyen 		return D0EPCTL_MPS_64;
169847a1685fSDinh Nguyen 	case 32:
169947a1685fSDinh Nguyen 		return D0EPCTL_MPS_32;
170047a1685fSDinh Nguyen 	case 16:
170147a1685fSDinh Nguyen 		return D0EPCTL_MPS_16;
170247a1685fSDinh Nguyen 	case 8:
170347a1685fSDinh Nguyen 		return D0EPCTL_MPS_8;
170447a1685fSDinh Nguyen 	}
170547a1685fSDinh Nguyen 
170647a1685fSDinh Nguyen 	/* bad max packet size, warn and return invalid result */
170747a1685fSDinh Nguyen 	WARN_ON(1);
170847a1685fSDinh Nguyen 	return (u32)-1;
170947a1685fSDinh Nguyen }
171047a1685fSDinh Nguyen 
171147a1685fSDinh Nguyen /**
17121f91b4ccSFelipe Balbi  * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
171347a1685fSDinh Nguyen  * @hsotg: The driver state.
171447a1685fSDinh Nguyen  * @ep: The index number of the endpoint
171547a1685fSDinh Nguyen  * @mps: The maximum packet size in bytes
171647a1685fSDinh Nguyen  *
171747a1685fSDinh Nguyen  * Configure the maximum packet size for the given endpoint, updating
171847a1685fSDinh Nguyen  * the hardware control registers to reflect this.
171947a1685fSDinh Nguyen  */
17201f91b4ccSFelipe Balbi static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
1721c6f5c050SMian Yousaf Kaukab 			unsigned int ep, unsigned int mps, unsigned int dir_in)
172247a1685fSDinh Nguyen {
17231f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep;
172447a1685fSDinh Nguyen 	void __iomem *regs = hsotg->regs;
172547a1685fSDinh Nguyen 	u32 mpsval;
172647a1685fSDinh Nguyen 	u32 mcval;
172747a1685fSDinh Nguyen 	u32 reg;
172847a1685fSDinh Nguyen 
1729c6f5c050SMian Yousaf Kaukab 	hs_ep = index_to_ep(hsotg, ep, dir_in);
1730c6f5c050SMian Yousaf Kaukab 	if (!hs_ep)
1731c6f5c050SMian Yousaf Kaukab 		return;
1732c6f5c050SMian Yousaf Kaukab 
173347a1685fSDinh Nguyen 	if (ep == 0) {
173447a1685fSDinh Nguyen 		/* EP0 is a special case */
17351f91b4ccSFelipe Balbi 		mpsval = dwc2_hsotg_ep0_mps(mps);
173647a1685fSDinh Nguyen 		if (mpsval > 3)
173747a1685fSDinh Nguyen 			goto bad_mps;
173847a1685fSDinh Nguyen 		hs_ep->ep.maxpacket = mps;
173947a1685fSDinh Nguyen 		hs_ep->mc = 1;
174047a1685fSDinh Nguyen 	} else {
174147a1685fSDinh Nguyen 		mpsval = mps & DXEPCTL_MPS_MASK;
174247a1685fSDinh Nguyen 		if (mpsval > 1024)
174347a1685fSDinh Nguyen 			goto bad_mps;
174447a1685fSDinh Nguyen 		mcval = ((mps >> 11) & 0x3) + 1;
174547a1685fSDinh Nguyen 		hs_ep->mc = mcval;
174647a1685fSDinh Nguyen 		if (mcval > 3)
174747a1685fSDinh Nguyen 			goto bad_mps;
174847a1685fSDinh Nguyen 		hs_ep->ep.maxpacket = mpsval;
174947a1685fSDinh Nguyen 	}
175047a1685fSDinh Nguyen 
1751c6f5c050SMian Yousaf Kaukab 	if (dir_in) {
175295c8bc36SAntti Seppälä 		reg = dwc2_readl(regs + DIEPCTL(ep));
175347a1685fSDinh Nguyen 		reg &= ~DXEPCTL_MPS_MASK;
175447a1685fSDinh Nguyen 		reg |= mpsval;
175595c8bc36SAntti Seppälä 		dwc2_writel(reg, regs + DIEPCTL(ep));
1756c6f5c050SMian Yousaf Kaukab 	} else {
175795c8bc36SAntti Seppälä 		reg = dwc2_readl(regs + DOEPCTL(ep));
175847a1685fSDinh Nguyen 		reg &= ~DXEPCTL_MPS_MASK;
175947a1685fSDinh Nguyen 		reg |= mpsval;
176095c8bc36SAntti Seppälä 		dwc2_writel(reg, regs + DOEPCTL(ep));
176147a1685fSDinh Nguyen 	}
176247a1685fSDinh Nguyen 
176347a1685fSDinh Nguyen 	return;
176447a1685fSDinh Nguyen 
176547a1685fSDinh Nguyen bad_mps:
176647a1685fSDinh Nguyen 	dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
176747a1685fSDinh Nguyen }
176847a1685fSDinh Nguyen 
176947a1685fSDinh Nguyen /**
17701f91b4ccSFelipe Balbi  * dwc2_hsotg_txfifo_flush - flush Tx FIFO
177147a1685fSDinh Nguyen  * @hsotg: The driver state
177247a1685fSDinh Nguyen  * @idx: The index for the endpoint (0..15)
177347a1685fSDinh Nguyen  */
17741f91b4ccSFelipe Balbi static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
177547a1685fSDinh Nguyen {
177647a1685fSDinh Nguyen 	int timeout;
177747a1685fSDinh Nguyen 	int val;
177847a1685fSDinh Nguyen 
177995c8bc36SAntti Seppälä 	dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
178047a1685fSDinh Nguyen 		    hsotg->regs + GRSTCTL);
178147a1685fSDinh Nguyen 
178247a1685fSDinh Nguyen 	/* wait until the fifo is flushed */
178347a1685fSDinh Nguyen 	timeout = 100;
178447a1685fSDinh Nguyen 
178547a1685fSDinh Nguyen 	while (1) {
178695c8bc36SAntti Seppälä 		val = dwc2_readl(hsotg->regs + GRSTCTL);
178747a1685fSDinh Nguyen 
178847a1685fSDinh Nguyen 		if ((val & (GRSTCTL_TXFFLSH)) == 0)
178947a1685fSDinh Nguyen 			break;
179047a1685fSDinh Nguyen 
179147a1685fSDinh Nguyen 		if (--timeout == 0) {
179247a1685fSDinh Nguyen 			dev_err(hsotg->dev,
179347a1685fSDinh Nguyen 				"%s: timeout flushing fifo (GRSTCTL=%08x)\n",
179447a1685fSDinh Nguyen 				__func__, val);
1795e0cbe595SMarek Szyprowski 			break;
179647a1685fSDinh Nguyen 		}
179747a1685fSDinh Nguyen 
179847a1685fSDinh Nguyen 		udelay(1);
179947a1685fSDinh Nguyen 	}
180047a1685fSDinh Nguyen }
180147a1685fSDinh Nguyen 
180247a1685fSDinh Nguyen /**
18031f91b4ccSFelipe Balbi  * dwc2_hsotg_trytx - check to see if anything needs transmitting
180447a1685fSDinh Nguyen  * @hsotg: The driver state
180547a1685fSDinh Nguyen  * @hs_ep: The driver endpoint to check.
180647a1685fSDinh Nguyen  *
180747a1685fSDinh Nguyen  * Check to see if there is a request that has data to send, and if so
180847a1685fSDinh Nguyen  * make an attempt to write data into the FIFO.
180947a1685fSDinh Nguyen  */
18101f91b4ccSFelipe Balbi static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
18111f91b4ccSFelipe Balbi 			   struct dwc2_hsotg_ep *hs_ep)
181247a1685fSDinh Nguyen {
18131f91b4ccSFelipe Balbi 	struct dwc2_hsotg_req *hs_req = hs_ep->req;
181447a1685fSDinh Nguyen 
181547a1685fSDinh Nguyen 	if (!hs_ep->dir_in || !hs_req) {
181647a1685fSDinh Nguyen 		/**
181747a1685fSDinh Nguyen 		 * if request is not enqueued, we disable interrupts
181847a1685fSDinh Nguyen 		 * for endpoints, excepting ep0
181947a1685fSDinh Nguyen 		 */
182047a1685fSDinh Nguyen 		if (hs_ep->index != 0)
18211f91b4ccSFelipe Balbi 			dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
182247a1685fSDinh Nguyen 					     hs_ep->dir_in, 0);
182347a1685fSDinh Nguyen 		return 0;
182447a1685fSDinh Nguyen 	}
182547a1685fSDinh Nguyen 
182647a1685fSDinh Nguyen 	if (hs_req->req.actual < hs_req->req.length) {
182747a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
182847a1685fSDinh Nguyen 			hs_ep->index);
18291f91b4ccSFelipe Balbi 		return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
183047a1685fSDinh Nguyen 	}
183147a1685fSDinh Nguyen 
183247a1685fSDinh Nguyen 	return 0;
183347a1685fSDinh Nguyen }
183447a1685fSDinh Nguyen 
183547a1685fSDinh Nguyen /**
18361f91b4ccSFelipe Balbi  * dwc2_hsotg_complete_in - complete IN transfer
183747a1685fSDinh Nguyen  * @hsotg: The device state.
183847a1685fSDinh Nguyen  * @hs_ep: The endpoint that has just completed.
183947a1685fSDinh Nguyen  *
184047a1685fSDinh Nguyen  * An IN transfer has been completed, update the transfer's state and then
184147a1685fSDinh Nguyen  * call the relevant completion routines.
184247a1685fSDinh Nguyen  */
18431f91b4ccSFelipe Balbi static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
18441f91b4ccSFelipe Balbi 				  struct dwc2_hsotg_ep *hs_ep)
184547a1685fSDinh Nguyen {
18461f91b4ccSFelipe Balbi 	struct dwc2_hsotg_req *hs_req = hs_ep->req;
184795c8bc36SAntti Seppälä 	u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
184847a1685fSDinh Nguyen 	int size_left, size_done;
184947a1685fSDinh Nguyen 
185047a1685fSDinh Nguyen 	if (!hs_req) {
185147a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "XferCompl but no req\n");
185247a1685fSDinh Nguyen 		return;
185347a1685fSDinh Nguyen 	}
185447a1685fSDinh Nguyen 
185547a1685fSDinh Nguyen 	/* Finish ZLP handling for IN EP0 transactions */
1856fe0b94abSMian Yousaf Kaukab 	if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
1857fe0b94abSMian Yousaf Kaukab 		dev_dbg(hsotg->dev, "zlp packet sent\n");
18581f91b4ccSFelipe Balbi 		dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
18599e14d0a5SGregory Herrero 		if (hsotg->test_mode) {
18609e14d0a5SGregory Herrero 			int ret;
18619e14d0a5SGregory Herrero 
18621f91b4ccSFelipe Balbi 			ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
18639e14d0a5SGregory Herrero 			if (ret < 0) {
18649e14d0a5SGregory Herrero 				dev_dbg(hsotg->dev, "Invalid Test #%d\n",
18659e14d0a5SGregory Herrero 						hsotg->test_mode);
18661f91b4ccSFelipe Balbi 				dwc2_hsotg_stall_ep0(hsotg);
18679e14d0a5SGregory Herrero 				return;
18689e14d0a5SGregory Herrero 			}
18699e14d0a5SGregory Herrero 		}
18701f91b4ccSFelipe Balbi 		dwc2_hsotg_enqueue_setup(hsotg);
187147a1685fSDinh Nguyen 		return;
187247a1685fSDinh Nguyen 	}
187347a1685fSDinh Nguyen 
187447a1685fSDinh Nguyen 	/*
187547a1685fSDinh Nguyen 	 * Calculate the size of the transfer by checking how much is left
187647a1685fSDinh Nguyen 	 * in the endpoint size register and then working it out from
187747a1685fSDinh Nguyen 	 * the amount we loaded for the transfer.
187847a1685fSDinh Nguyen 	 *
187947a1685fSDinh Nguyen 	 * We do this even for DMA, as the transfer may have incremented
188047a1685fSDinh Nguyen 	 * past the end of the buffer (DMA transfers are always 32bit
188147a1685fSDinh Nguyen 	 * aligned).
188247a1685fSDinh Nguyen 	 */
188347a1685fSDinh Nguyen 
188447a1685fSDinh Nguyen 	size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
188547a1685fSDinh Nguyen 
188647a1685fSDinh Nguyen 	size_done = hs_ep->size_loaded - size_left;
188747a1685fSDinh Nguyen 	size_done += hs_ep->last_load;
188847a1685fSDinh Nguyen 
188947a1685fSDinh Nguyen 	if (hs_req->req.actual != size_done)
189047a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
189147a1685fSDinh Nguyen 			__func__, hs_req->req.actual, size_done);
189247a1685fSDinh Nguyen 
189347a1685fSDinh Nguyen 	hs_req->req.actual = size_done;
189447a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
189547a1685fSDinh Nguyen 		hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
189647a1685fSDinh Nguyen 
189747a1685fSDinh Nguyen 	if (!size_left && hs_req->req.actual < hs_req->req.length) {
189847a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
18991f91b4ccSFelipe Balbi 		dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1900fe0b94abSMian Yousaf Kaukab 		return;
1901fe0b94abSMian Yousaf Kaukab 	}
1902fe0b94abSMian Yousaf Kaukab 
1903f71b5e25SMian Yousaf Kaukab 	/* Zlp for all endpoints, for ep0 only in DATA IN stage */
19048a20fa45SMian Yousaf Kaukab 	if (hs_ep->send_zlp) {
19051f91b4ccSFelipe Balbi 		dwc2_hsotg_program_zlp(hsotg, hs_ep);
19068a20fa45SMian Yousaf Kaukab 		hs_ep->send_zlp = 0;
1907f71b5e25SMian Yousaf Kaukab 		/* transfer will be completed on next complete interrupt */
1908f71b5e25SMian Yousaf Kaukab 		return;
1909f71b5e25SMian Yousaf Kaukab 	}
1910f71b5e25SMian Yousaf Kaukab 
1911fe0b94abSMian Yousaf Kaukab 	if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
1912fe0b94abSMian Yousaf Kaukab 		/* Move to STATUS OUT */
19131f91b4ccSFelipe Balbi 		dwc2_hsotg_ep0_zlp(hsotg, false);
1914fe0b94abSMian Yousaf Kaukab 		return;
1915fe0b94abSMian Yousaf Kaukab 	}
1916fe0b94abSMian Yousaf Kaukab 
19171f91b4ccSFelipe Balbi 	dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
191847a1685fSDinh Nguyen }
191947a1685fSDinh Nguyen 
192047a1685fSDinh Nguyen /**
19211f91b4ccSFelipe Balbi  * dwc2_hsotg_epint - handle an in/out endpoint interrupt
192247a1685fSDinh Nguyen  * @hsotg: The driver state
192347a1685fSDinh Nguyen  * @idx: The index for the endpoint (0..15)
192447a1685fSDinh Nguyen  * @dir_in: Set if this is an IN endpoint
192547a1685fSDinh Nguyen  *
192647a1685fSDinh Nguyen  * Process and clear any interrupt pending for an individual endpoint
192747a1685fSDinh Nguyen  */
19281f91b4ccSFelipe Balbi static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
192947a1685fSDinh Nguyen 			    int dir_in)
193047a1685fSDinh Nguyen {
19311f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
193247a1685fSDinh Nguyen 	u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
193347a1685fSDinh Nguyen 	u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
193447a1685fSDinh Nguyen 	u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
193547a1685fSDinh Nguyen 	u32 ints;
193647a1685fSDinh Nguyen 	u32 ctrl;
193747a1685fSDinh Nguyen 
193895c8bc36SAntti Seppälä 	ints = dwc2_readl(hsotg->regs + epint_reg);
193995c8bc36SAntti Seppälä 	ctrl = dwc2_readl(hsotg->regs + epctl_reg);
194047a1685fSDinh Nguyen 
194147a1685fSDinh Nguyen 	/* Clear endpoint interrupts */
194295c8bc36SAntti Seppälä 	dwc2_writel(ints, hsotg->regs + epint_reg);
194347a1685fSDinh Nguyen 
1944c6f5c050SMian Yousaf Kaukab 	if (!hs_ep) {
1945c6f5c050SMian Yousaf Kaukab 		dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
1946c6f5c050SMian Yousaf Kaukab 					__func__, idx, dir_in ? "in" : "out");
1947c6f5c050SMian Yousaf Kaukab 		return;
1948c6f5c050SMian Yousaf Kaukab 	}
1949c6f5c050SMian Yousaf Kaukab 
195047a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
195147a1685fSDinh Nguyen 		__func__, idx, dir_in ? "in" : "out", ints);
195247a1685fSDinh Nguyen 
1953b787d755SMian Yousaf Kaukab 	/* Don't process XferCompl interrupt if it is a setup packet */
1954b787d755SMian Yousaf Kaukab 	if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
1955b787d755SMian Yousaf Kaukab 		ints &= ~DXEPINT_XFERCOMPL;
1956b787d755SMian Yousaf Kaukab 
195747a1685fSDinh Nguyen 	if (ints & DXEPINT_XFERCOMPL) {
195847a1685fSDinh Nguyen 		if (hs_ep->isochronous && hs_ep->interval == 1) {
195947a1685fSDinh Nguyen 			if (ctrl & DXEPCTL_EOFRNUM)
196047a1685fSDinh Nguyen 				ctrl |= DXEPCTL_SETEVENFR;
196147a1685fSDinh Nguyen 			else
196247a1685fSDinh Nguyen 				ctrl |= DXEPCTL_SETODDFR;
196395c8bc36SAntti Seppälä 			dwc2_writel(ctrl, hsotg->regs + epctl_reg);
196447a1685fSDinh Nguyen 		}
196547a1685fSDinh Nguyen 
196647a1685fSDinh Nguyen 		dev_dbg(hsotg->dev,
196747a1685fSDinh Nguyen 			"%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
196895c8bc36SAntti Seppälä 			__func__, dwc2_readl(hsotg->regs + epctl_reg),
196995c8bc36SAntti Seppälä 			dwc2_readl(hsotg->regs + epsiz_reg));
197047a1685fSDinh Nguyen 
197147a1685fSDinh Nguyen 		/*
197247a1685fSDinh Nguyen 		 * we get OutDone from the FIFO, so we only need to look
197347a1685fSDinh Nguyen 		 * at completing IN requests here
197447a1685fSDinh Nguyen 		 */
197547a1685fSDinh Nguyen 		if (dir_in) {
19761f91b4ccSFelipe Balbi 			dwc2_hsotg_complete_in(hsotg, hs_ep);
197747a1685fSDinh Nguyen 
197847a1685fSDinh Nguyen 			if (idx == 0 && !hs_ep->req)
19791f91b4ccSFelipe Balbi 				dwc2_hsotg_enqueue_setup(hsotg);
198047a1685fSDinh Nguyen 		} else if (using_dma(hsotg)) {
198147a1685fSDinh Nguyen 			/*
198247a1685fSDinh Nguyen 			 * We're using DMA, we need to fire an OutDone here
198347a1685fSDinh Nguyen 			 * as we ignore the RXFIFO.
198447a1685fSDinh Nguyen 			 */
198547a1685fSDinh Nguyen 
19861f91b4ccSFelipe Balbi 			dwc2_hsotg_handle_outdone(hsotg, idx);
198747a1685fSDinh Nguyen 		}
198847a1685fSDinh Nguyen 	}
198947a1685fSDinh Nguyen 
199047a1685fSDinh Nguyen 	if (ints & DXEPINT_EPDISBLD) {
199147a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
199247a1685fSDinh Nguyen 
199347a1685fSDinh Nguyen 		if (dir_in) {
199495c8bc36SAntti Seppälä 			int epctl = dwc2_readl(hsotg->regs + epctl_reg);
199547a1685fSDinh Nguyen 
19961f91b4ccSFelipe Balbi 			dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
199747a1685fSDinh Nguyen 
199847a1685fSDinh Nguyen 			if ((epctl & DXEPCTL_STALL) &&
199947a1685fSDinh Nguyen 				(epctl & DXEPCTL_EPTYPE_BULK)) {
200095c8bc36SAntti Seppälä 				int dctl = dwc2_readl(hsotg->regs + DCTL);
200147a1685fSDinh Nguyen 
200247a1685fSDinh Nguyen 				dctl |= DCTL_CGNPINNAK;
200395c8bc36SAntti Seppälä 				dwc2_writel(dctl, hsotg->regs + DCTL);
200447a1685fSDinh Nguyen 			}
200547a1685fSDinh Nguyen 		}
200647a1685fSDinh Nguyen 	}
200747a1685fSDinh Nguyen 
200847a1685fSDinh Nguyen 	if (ints & DXEPINT_AHBERR)
200947a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
201047a1685fSDinh Nguyen 
201147a1685fSDinh Nguyen 	if (ints & DXEPINT_SETUP) {  /* Setup or Timeout */
201247a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: Setup/Timeout\n",  __func__);
201347a1685fSDinh Nguyen 
201447a1685fSDinh Nguyen 		if (using_dma(hsotg) && idx == 0) {
201547a1685fSDinh Nguyen 			/*
201647a1685fSDinh Nguyen 			 * this is the notification we've received a
201747a1685fSDinh Nguyen 			 * setup packet. In non-DMA mode we'd get this
201847a1685fSDinh Nguyen 			 * from the RXFIFO, instead we need to process
201947a1685fSDinh Nguyen 			 * the setup here.
202047a1685fSDinh Nguyen 			 */
202147a1685fSDinh Nguyen 
202247a1685fSDinh Nguyen 			if (dir_in)
202347a1685fSDinh Nguyen 				WARN_ON_ONCE(1);
202447a1685fSDinh Nguyen 			else
20251f91b4ccSFelipe Balbi 				dwc2_hsotg_handle_outdone(hsotg, 0);
202647a1685fSDinh Nguyen 		}
202747a1685fSDinh Nguyen 	}
202847a1685fSDinh Nguyen 
202947a1685fSDinh Nguyen 	if (ints & DXEPINT_BACK2BACKSETUP)
203047a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
203147a1685fSDinh Nguyen 
203247a1685fSDinh Nguyen 	if (dir_in && !hs_ep->isochronous) {
203347a1685fSDinh Nguyen 		/* not sure if this is important, but we'll clear it anyway */
203447a1685fSDinh Nguyen 		if (ints & DIEPMSK_INTKNTXFEMPMSK) {
203547a1685fSDinh Nguyen 			dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
203647a1685fSDinh Nguyen 				__func__, idx);
203747a1685fSDinh Nguyen 		}
203847a1685fSDinh Nguyen 
203947a1685fSDinh Nguyen 		/* this probably means something bad is happening */
204047a1685fSDinh Nguyen 		if (ints & DIEPMSK_INTKNEPMISMSK) {
204147a1685fSDinh Nguyen 			dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
204247a1685fSDinh Nguyen 				 __func__, idx);
204347a1685fSDinh Nguyen 		}
204447a1685fSDinh Nguyen 
204547a1685fSDinh Nguyen 		/* FIFO has space or is empty (see GAHBCFG) */
204647a1685fSDinh Nguyen 		if (hsotg->dedicated_fifos &&
204747a1685fSDinh Nguyen 		    ints & DIEPMSK_TXFIFOEMPTY) {
204847a1685fSDinh Nguyen 			dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
204947a1685fSDinh Nguyen 				__func__, idx);
205047a1685fSDinh Nguyen 			if (!using_dma(hsotg))
20511f91b4ccSFelipe Balbi 				dwc2_hsotg_trytx(hsotg, hs_ep);
205247a1685fSDinh Nguyen 		}
205347a1685fSDinh Nguyen 	}
205447a1685fSDinh Nguyen }
205547a1685fSDinh Nguyen 
205647a1685fSDinh Nguyen /**
20571f91b4ccSFelipe Balbi  * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
205847a1685fSDinh Nguyen  * @hsotg: The device state.
205947a1685fSDinh Nguyen  *
206047a1685fSDinh Nguyen  * Handle updating the device settings after the enumeration phase has
206147a1685fSDinh Nguyen  * been completed.
206247a1685fSDinh Nguyen  */
20631f91b4ccSFelipe Balbi static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
206447a1685fSDinh Nguyen {
206595c8bc36SAntti Seppälä 	u32 dsts = dwc2_readl(hsotg->regs + DSTS);
20669b2667f1SJingoo Han 	int ep0_mps = 0, ep_mps = 8;
206747a1685fSDinh Nguyen 
206847a1685fSDinh Nguyen 	/*
206947a1685fSDinh Nguyen 	 * This should signal the finish of the enumeration phase
207047a1685fSDinh Nguyen 	 * of the USB handshaking, so we should now know what rate
207147a1685fSDinh Nguyen 	 * we connected at.
207247a1685fSDinh Nguyen 	 */
207347a1685fSDinh Nguyen 
207447a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
207547a1685fSDinh Nguyen 
207647a1685fSDinh Nguyen 	/*
207747a1685fSDinh Nguyen 	 * note, since we're limited by the size of transfer on EP0, and
207847a1685fSDinh Nguyen 	 * it seems IN transfers must be a even number of packets we do
207947a1685fSDinh Nguyen 	 * not advertise a 64byte MPS on EP0.
208047a1685fSDinh Nguyen 	 */
208147a1685fSDinh Nguyen 
208247a1685fSDinh Nguyen 	/* catch both EnumSpd_FS and EnumSpd_FS48 */
208347a1685fSDinh Nguyen 	switch (dsts & DSTS_ENUMSPD_MASK) {
208447a1685fSDinh Nguyen 	case DSTS_ENUMSPD_FS:
208547a1685fSDinh Nguyen 	case DSTS_ENUMSPD_FS48:
208647a1685fSDinh Nguyen 		hsotg->gadget.speed = USB_SPEED_FULL;
208747a1685fSDinh Nguyen 		ep0_mps = EP0_MPS_LIMIT;
208847a1685fSDinh Nguyen 		ep_mps = 1023;
208947a1685fSDinh Nguyen 		break;
209047a1685fSDinh Nguyen 
209147a1685fSDinh Nguyen 	case DSTS_ENUMSPD_HS:
209247a1685fSDinh Nguyen 		hsotg->gadget.speed = USB_SPEED_HIGH;
209347a1685fSDinh Nguyen 		ep0_mps = EP0_MPS_LIMIT;
209447a1685fSDinh Nguyen 		ep_mps = 1024;
209547a1685fSDinh Nguyen 		break;
209647a1685fSDinh Nguyen 
209747a1685fSDinh Nguyen 	case DSTS_ENUMSPD_LS:
209847a1685fSDinh Nguyen 		hsotg->gadget.speed = USB_SPEED_LOW;
209947a1685fSDinh Nguyen 		/*
210047a1685fSDinh Nguyen 		 * note, we don't actually support LS in this driver at the
210147a1685fSDinh Nguyen 		 * moment, and the documentation seems to imply that it isn't
210247a1685fSDinh Nguyen 		 * supported by the PHYs on some of the devices.
210347a1685fSDinh Nguyen 		 */
210447a1685fSDinh Nguyen 		break;
210547a1685fSDinh Nguyen 	}
210647a1685fSDinh Nguyen 	dev_info(hsotg->dev, "new device is %s\n",
210747a1685fSDinh Nguyen 		 usb_speed_string(hsotg->gadget.speed));
210847a1685fSDinh Nguyen 
210947a1685fSDinh Nguyen 	/*
211047a1685fSDinh Nguyen 	 * we should now know the maximum packet size for an
211147a1685fSDinh Nguyen 	 * endpoint, so set the endpoints to a default value.
211247a1685fSDinh Nguyen 	 */
211347a1685fSDinh Nguyen 
211447a1685fSDinh Nguyen 	if (ep0_mps) {
211547a1685fSDinh Nguyen 		int i;
2116c6f5c050SMian Yousaf Kaukab 		/* Initialize ep0 for both in and out directions */
21171f91b4ccSFelipe Balbi 		dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 1);
21181f91b4ccSFelipe Balbi 		dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0);
2119c6f5c050SMian Yousaf Kaukab 		for (i = 1; i < hsotg->num_of_eps; i++) {
2120c6f5c050SMian Yousaf Kaukab 			if (hsotg->eps_in[i])
21211f91b4ccSFelipe Balbi 				dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 1);
2122c6f5c050SMian Yousaf Kaukab 			if (hsotg->eps_out[i])
21231f91b4ccSFelipe Balbi 				dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 0);
2124c6f5c050SMian Yousaf Kaukab 		}
212547a1685fSDinh Nguyen 	}
212647a1685fSDinh Nguyen 
212747a1685fSDinh Nguyen 	/* ensure after enumeration our EP0 is active */
212847a1685fSDinh Nguyen 
21291f91b4ccSFelipe Balbi 	dwc2_hsotg_enqueue_setup(hsotg);
213047a1685fSDinh Nguyen 
213147a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
213295c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + DIEPCTL0),
213395c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + DOEPCTL0));
213447a1685fSDinh Nguyen }
213547a1685fSDinh Nguyen 
213647a1685fSDinh Nguyen /**
213747a1685fSDinh Nguyen  * kill_all_requests - remove all requests from the endpoint's queue
213847a1685fSDinh Nguyen  * @hsotg: The device state.
213947a1685fSDinh Nguyen  * @ep: The endpoint the requests may be on.
214047a1685fSDinh Nguyen  * @result: The result code to use.
214147a1685fSDinh Nguyen  *
214247a1685fSDinh Nguyen  * Go through the requests on the given endpoint and mark them
214347a1685fSDinh Nguyen  * completed with the given result code.
214447a1685fSDinh Nguyen  */
2145941fcce4SDinh Nguyen static void kill_all_requests(struct dwc2_hsotg *hsotg,
21461f91b4ccSFelipe Balbi 			      struct dwc2_hsotg_ep *ep,
21476b448af4SRobert Baldyga 			      int result)
214847a1685fSDinh Nguyen {
21491f91b4ccSFelipe Balbi 	struct dwc2_hsotg_req *req, *treq;
2150b203d0a2SRobert Baldyga 	unsigned size;
215147a1685fSDinh Nguyen 
21526b448af4SRobert Baldyga 	ep->req = NULL;
215347a1685fSDinh Nguyen 
21546b448af4SRobert Baldyga 	list_for_each_entry_safe(req, treq, &ep->queue, queue)
21551f91b4ccSFelipe Balbi 		dwc2_hsotg_complete_request(hsotg, ep, req,
215647a1685fSDinh Nguyen 					   result);
21576b448af4SRobert Baldyga 
2158b203d0a2SRobert Baldyga 	if (!hsotg->dedicated_fifos)
2159b203d0a2SRobert Baldyga 		return;
216095c8bc36SAntti Seppälä 	size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4;
2161b203d0a2SRobert Baldyga 	if (size < ep->fifo_size)
21621f91b4ccSFelipe Balbi 		dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
216347a1685fSDinh Nguyen }
216447a1685fSDinh Nguyen 
216547a1685fSDinh Nguyen /**
21661f91b4ccSFelipe Balbi  * dwc2_hsotg_disconnect - disconnect service
216747a1685fSDinh Nguyen  * @hsotg: The device state.
216847a1685fSDinh Nguyen  *
216947a1685fSDinh Nguyen  * The device has been disconnected. Remove all current
217047a1685fSDinh Nguyen  * transactions and signal the gadget driver that this
217147a1685fSDinh Nguyen  * has happened.
217247a1685fSDinh Nguyen  */
21731f91b4ccSFelipe Balbi void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
217447a1685fSDinh Nguyen {
217547a1685fSDinh Nguyen 	unsigned ep;
217647a1685fSDinh Nguyen 
21774ace06e8SMarek Szyprowski 	if (!hsotg->connected)
21784ace06e8SMarek Szyprowski 		return;
21794ace06e8SMarek Szyprowski 
21804ace06e8SMarek Szyprowski 	hsotg->connected = 0;
21819e14d0a5SGregory Herrero 	hsotg->test_mode = 0;
2182c6f5c050SMian Yousaf Kaukab 
2183c6f5c050SMian Yousaf Kaukab 	for (ep = 0; ep < hsotg->num_of_eps; ep++) {
2184c6f5c050SMian Yousaf Kaukab 		if (hsotg->eps_in[ep])
2185c6f5c050SMian Yousaf Kaukab 			kill_all_requests(hsotg, hsotg->eps_in[ep],
2186c6f5c050SMian Yousaf Kaukab 								-ESHUTDOWN);
2187c6f5c050SMian Yousaf Kaukab 		if (hsotg->eps_out[ep])
2188c6f5c050SMian Yousaf Kaukab 			kill_all_requests(hsotg, hsotg->eps_out[ep],
2189c6f5c050SMian Yousaf Kaukab 								-ESHUTDOWN);
2190c6f5c050SMian Yousaf Kaukab 	}
219147a1685fSDinh Nguyen 
219247a1685fSDinh Nguyen 	call_gadget(hsotg, disconnect);
2193065d3931SGregory Herrero 	hsotg->lx_state = DWC2_L3;
219447a1685fSDinh Nguyen }
219547a1685fSDinh Nguyen 
219647a1685fSDinh Nguyen /**
21971f91b4ccSFelipe Balbi  * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
219847a1685fSDinh Nguyen  * @hsotg: The device state:
219947a1685fSDinh Nguyen  * @periodic: True if this is a periodic FIFO interrupt
220047a1685fSDinh Nguyen  */
22011f91b4ccSFelipe Balbi static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
220247a1685fSDinh Nguyen {
22031f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *ep;
220447a1685fSDinh Nguyen 	int epno, ret;
220547a1685fSDinh Nguyen 
220647a1685fSDinh Nguyen 	/* look through for any more data to transmit */
220747a1685fSDinh Nguyen 	for (epno = 0; epno < hsotg->num_of_eps; epno++) {
2208c6f5c050SMian Yousaf Kaukab 		ep = index_to_ep(hsotg, epno, 1);
2209c6f5c050SMian Yousaf Kaukab 
2210c6f5c050SMian Yousaf Kaukab 		if (!ep)
2211c6f5c050SMian Yousaf Kaukab 			continue;
221247a1685fSDinh Nguyen 
221347a1685fSDinh Nguyen 		if (!ep->dir_in)
221447a1685fSDinh Nguyen 			continue;
221547a1685fSDinh Nguyen 
221647a1685fSDinh Nguyen 		if ((periodic && !ep->periodic) ||
221747a1685fSDinh Nguyen 		    (!periodic && ep->periodic))
221847a1685fSDinh Nguyen 			continue;
221947a1685fSDinh Nguyen 
22201f91b4ccSFelipe Balbi 		ret = dwc2_hsotg_trytx(hsotg, ep);
222147a1685fSDinh Nguyen 		if (ret < 0)
222247a1685fSDinh Nguyen 			break;
222347a1685fSDinh Nguyen 	}
222447a1685fSDinh Nguyen }
222547a1685fSDinh Nguyen 
222647a1685fSDinh Nguyen /* IRQ flags which will trigger a retry around the IRQ loop */
222747a1685fSDinh Nguyen #define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
222847a1685fSDinh Nguyen 			GINTSTS_PTXFEMP |  \
222947a1685fSDinh Nguyen 			GINTSTS_RXFLVL)
223047a1685fSDinh Nguyen 
223147a1685fSDinh Nguyen /**
22321f91b4ccSFelipe Balbi  * dwc2_hsotg_corereset - issue softreset to the core
223347a1685fSDinh Nguyen  * @hsotg: The device state
223447a1685fSDinh Nguyen  *
223547a1685fSDinh Nguyen  * Issue a soft reset to the core, and await the core finishing it.
223647a1685fSDinh Nguyen  */
22371f91b4ccSFelipe Balbi static int dwc2_hsotg_corereset(struct dwc2_hsotg *hsotg)
223847a1685fSDinh Nguyen {
223947a1685fSDinh Nguyen 	int timeout;
224047a1685fSDinh Nguyen 	u32 grstctl;
224147a1685fSDinh Nguyen 
224247a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "resetting core\n");
224347a1685fSDinh Nguyen 
224447a1685fSDinh Nguyen 	/* issue soft reset */
224595c8bc36SAntti Seppälä 	dwc2_writel(GRSTCTL_CSFTRST, hsotg->regs + GRSTCTL);
224647a1685fSDinh Nguyen 
224747a1685fSDinh Nguyen 	timeout = 10000;
224847a1685fSDinh Nguyen 	do {
224995c8bc36SAntti Seppälä 		grstctl = dwc2_readl(hsotg->regs + GRSTCTL);
225047a1685fSDinh Nguyen 	} while ((grstctl & GRSTCTL_CSFTRST) && timeout-- > 0);
225147a1685fSDinh Nguyen 
225247a1685fSDinh Nguyen 	if (grstctl & GRSTCTL_CSFTRST) {
225347a1685fSDinh Nguyen 		dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
225447a1685fSDinh Nguyen 		return -EINVAL;
225547a1685fSDinh Nguyen 	}
225647a1685fSDinh Nguyen 
225747a1685fSDinh Nguyen 	timeout = 10000;
225847a1685fSDinh Nguyen 
225947a1685fSDinh Nguyen 	while (1) {
226095c8bc36SAntti Seppälä 		u32 grstctl = dwc2_readl(hsotg->regs + GRSTCTL);
226147a1685fSDinh Nguyen 
226247a1685fSDinh Nguyen 		if (timeout-- < 0) {
226347a1685fSDinh Nguyen 			dev_info(hsotg->dev,
226447a1685fSDinh Nguyen 				 "%s: reset failed, GRSTCTL=%08x\n",
226547a1685fSDinh Nguyen 				 __func__, grstctl);
226647a1685fSDinh Nguyen 			return -ETIMEDOUT;
226747a1685fSDinh Nguyen 		}
226847a1685fSDinh Nguyen 
226947a1685fSDinh Nguyen 		if (!(grstctl & GRSTCTL_AHBIDLE))
227047a1685fSDinh Nguyen 			continue;
227147a1685fSDinh Nguyen 
227247a1685fSDinh Nguyen 		break;		/* reset done */
227347a1685fSDinh Nguyen 	}
227447a1685fSDinh Nguyen 
227547a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "reset successful\n");
227647a1685fSDinh Nguyen 	return 0;
227747a1685fSDinh Nguyen }
227847a1685fSDinh Nguyen 
227947a1685fSDinh Nguyen /**
22801f91b4ccSFelipe Balbi  * dwc2_hsotg_core_init - issue softreset to the core
228147a1685fSDinh Nguyen  * @hsotg: The device state
228247a1685fSDinh Nguyen  *
228347a1685fSDinh Nguyen  * Issue a soft reset to the core, and await the core finishing it.
228447a1685fSDinh Nguyen  */
22851f91b4ccSFelipe Balbi void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
2286643cc4deSGregory Herrero 						bool is_usb_reset)
228747a1685fSDinh Nguyen {
2288643cc4deSGregory Herrero 	u32 val;
2289643cc4deSGregory Herrero 
2290643cc4deSGregory Herrero 	if (!is_usb_reset)
229186de4895SGregory Herrero 		if (dwc2_hsotg_corereset(hsotg))
229286de4895SGregory Herrero 			return;
229347a1685fSDinh Nguyen 
229447a1685fSDinh Nguyen 	/*
229547a1685fSDinh Nguyen 	 * we must now enable ep0 ready for host detection and then
229647a1685fSDinh Nguyen 	 * set configuration.
229747a1685fSDinh Nguyen 	 */
229847a1685fSDinh Nguyen 
229947a1685fSDinh Nguyen 	/* set the PLL on, remove the HNP/SRP and set the PHY */
2300fa4a8d72SMian Yousaf Kaukab 	val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
230195c8bc36SAntti Seppälä 	dwc2_writel(hsotg->phyif | GUSBCFG_TOUTCAL(7) |
2302f889f23dSMian Yousaf Kaukab 	       (val << GUSBCFG_USBTRDTIM_SHIFT), hsotg->regs + GUSBCFG);
230347a1685fSDinh Nguyen 
23041f91b4ccSFelipe Balbi 	dwc2_hsotg_init_fifo(hsotg);
230547a1685fSDinh Nguyen 
2306643cc4deSGregory Herrero 	if (!is_usb_reset)
230747a1685fSDinh Nguyen 		__orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
230847a1685fSDinh Nguyen 
230995c8bc36SAntti Seppälä 	dwc2_writel(DCFG_EPMISCNT(1) | DCFG_DEVSPD_HS,  hsotg->regs + DCFG);
231047a1685fSDinh Nguyen 
231147a1685fSDinh Nguyen 	/* Clear any pending OTG interrupts */
231295c8bc36SAntti Seppälä 	dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
231347a1685fSDinh Nguyen 
231447a1685fSDinh Nguyen 	/* Clear any pending interrupts */
231595c8bc36SAntti Seppälä 	dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
231647a1685fSDinh Nguyen 
231795c8bc36SAntti Seppälä 	dwc2_writel(GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
231847a1685fSDinh Nguyen 		GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
231947a1685fSDinh Nguyen 		GINTSTS_CONIDSTSCHNG | GINTSTS_USBRST |
23204876886fSGregory Herrero 		GINTSTS_RESETDET | GINTSTS_ENUMDONE |
23214876886fSGregory Herrero 		GINTSTS_OTGINT | GINTSTS_USBSUSP |
23224876886fSGregory Herrero 		GINTSTS_WKUPINT,
232347a1685fSDinh Nguyen 		hsotg->regs + GINTMSK);
232447a1685fSDinh Nguyen 
232547a1685fSDinh Nguyen 	if (using_dma(hsotg))
232695c8bc36SAntti Seppälä 		dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
23275f05048eSGregory Herrero 			    (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
232847a1685fSDinh Nguyen 			    hsotg->regs + GAHBCFG);
232947a1685fSDinh Nguyen 	else
233095c8bc36SAntti Seppälä 		dwc2_writel(((hsotg->dedicated_fifos) ?
233195c8bc36SAntti Seppälä 						(GAHBCFG_NP_TXF_EMP_LVL |
233247a1685fSDinh Nguyen 						 GAHBCFG_P_TXF_EMP_LVL) : 0) |
233395c8bc36SAntti Seppälä 			    GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
233447a1685fSDinh Nguyen 
233547a1685fSDinh Nguyen 	/*
233647a1685fSDinh Nguyen 	 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
233747a1685fSDinh Nguyen 	 * when we have no data to transfer. Otherwise we get being flooded by
233847a1685fSDinh Nguyen 	 * interrupts.
233947a1685fSDinh Nguyen 	 */
234047a1685fSDinh Nguyen 
234195c8bc36SAntti Seppälä 	dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
23426ff2e832SMian Yousaf Kaukab 		DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
234347a1685fSDinh Nguyen 		DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
234447a1685fSDinh Nguyen 		DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
234547a1685fSDinh Nguyen 		DIEPMSK_INTKNEPMISMSK,
234647a1685fSDinh Nguyen 		hsotg->regs + DIEPMSK);
234747a1685fSDinh Nguyen 
234847a1685fSDinh Nguyen 	/*
234947a1685fSDinh Nguyen 	 * don't need XferCompl, we get that from RXFIFO in slave mode. In
235047a1685fSDinh Nguyen 	 * DMA mode we may need this.
235147a1685fSDinh Nguyen 	 */
235295c8bc36SAntti Seppälä 	dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
235347a1685fSDinh Nguyen 				    DIEPMSK_TIMEOUTMSK) : 0) |
235447a1685fSDinh Nguyen 		DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
235547a1685fSDinh Nguyen 		DOEPMSK_SETUPMSK,
235647a1685fSDinh Nguyen 		hsotg->regs + DOEPMSK);
235747a1685fSDinh Nguyen 
235895c8bc36SAntti Seppälä 	dwc2_writel(0, hsotg->regs + DAINTMSK);
235947a1685fSDinh Nguyen 
236047a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
236195c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + DIEPCTL0),
236295c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + DOEPCTL0));
236347a1685fSDinh Nguyen 
236447a1685fSDinh Nguyen 	/* enable in and out endpoint interrupts */
23651f91b4ccSFelipe Balbi 	dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
236647a1685fSDinh Nguyen 
236747a1685fSDinh Nguyen 	/*
236847a1685fSDinh Nguyen 	 * Enable the RXFIFO when in slave mode, as this is how we collect
236947a1685fSDinh Nguyen 	 * the data. In DMA mode, we get events from the FIFO but also
237047a1685fSDinh Nguyen 	 * things we cannot process, so do not use it.
237147a1685fSDinh Nguyen 	 */
237247a1685fSDinh Nguyen 	if (!using_dma(hsotg))
23731f91b4ccSFelipe Balbi 		dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
237447a1685fSDinh Nguyen 
237547a1685fSDinh Nguyen 	/* Enable interrupts for EP0 in and out */
23761f91b4ccSFelipe Balbi 	dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
23771f91b4ccSFelipe Balbi 	dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
237847a1685fSDinh Nguyen 
2379643cc4deSGregory Herrero 	if (!is_usb_reset) {
238047a1685fSDinh Nguyen 		__orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
238147a1685fSDinh Nguyen 		udelay(10);  /* see openiboot */
238247a1685fSDinh Nguyen 		__bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2383643cc4deSGregory Herrero 	}
238447a1685fSDinh Nguyen 
238595c8bc36SAntti Seppälä 	dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
238647a1685fSDinh Nguyen 
238747a1685fSDinh Nguyen 	/*
238847a1685fSDinh Nguyen 	 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
238947a1685fSDinh Nguyen 	 * writing to the EPCTL register..
239047a1685fSDinh Nguyen 	 */
239147a1685fSDinh Nguyen 
239247a1685fSDinh Nguyen 	/* set to read 1 8byte packet */
239395c8bc36SAntti Seppälä 	dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
239447a1685fSDinh Nguyen 	       DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
239547a1685fSDinh Nguyen 
239695c8bc36SAntti Seppälä 	dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
239747a1685fSDinh Nguyen 	       DXEPCTL_CNAK | DXEPCTL_EPENA |
239847a1685fSDinh Nguyen 	       DXEPCTL_USBACTEP,
239947a1685fSDinh Nguyen 	       hsotg->regs + DOEPCTL0);
240047a1685fSDinh Nguyen 
240147a1685fSDinh Nguyen 	/* enable, but don't activate EP0in */
240295c8bc36SAntti Seppälä 	dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
240347a1685fSDinh Nguyen 	       DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
240447a1685fSDinh Nguyen 
24051f91b4ccSFelipe Balbi 	dwc2_hsotg_enqueue_setup(hsotg);
240647a1685fSDinh Nguyen 
240747a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
240895c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + DIEPCTL0),
240995c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + DOEPCTL0));
241047a1685fSDinh Nguyen 
241147a1685fSDinh Nguyen 	/* clear global NAKs */
2412643cc4deSGregory Herrero 	val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
2413643cc4deSGregory Herrero 	if (!is_usb_reset)
2414643cc4deSGregory Herrero 		val |= DCTL_SFTDISCON;
2415643cc4deSGregory Herrero 	__orr32(hsotg->regs + DCTL, val);
241647a1685fSDinh Nguyen 
241747a1685fSDinh Nguyen 	/* must be at-least 3ms to allow bus to see disconnect */
241847a1685fSDinh Nguyen 	mdelay(3);
241947a1685fSDinh Nguyen 
2420ac3c81f3SMarek Szyprowski 	hsotg->last_rst = jiffies;
2421065d3931SGregory Herrero 	hsotg->lx_state = DWC2_L0;
2422ad38dc5dSMarek Szyprowski }
2423ac3c81f3SMarek Szyprowski 
24241f91b4ccSFelipe Balbi static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
2425ad38dc5dSMarek Szyprowski {
2426ad38dc5dSMarek Szyprowski 	/* set the soft-disconnect bit */
2427ad38dc5dSMarek Szyprowski 	__orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
2428ad38dc5dSMarek Szyprowski }
2429ad38dc5dSMarek Szyprowski 
24301f91b4ccSFelipe Balbi void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
2431ad38dc5dSMarek Szyprowski {
243247a1685fSDinh Nguyen 	/* remove the soft-disconnect and let's go */
243347a1685fSDinh Nguyen 	__bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
243447a1685fSDinh Nguyen }
243547a1685fSDinh Nguyen 
243647a1685fSDinh Nguyen /**
24371f91b4ccSFelipe Balbi  * dwc2_hsotg_irq - handle device interrupt
243847a1685fSDinh Nguyen  * @irq: The IRQ number triggered
243947a1685fSDinh Nguyen  * @pw: The pw value when registered the handler.
244047a1685fSDinh Nguyen  */
24411f91b4ccSFelipe Balbi static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
244247a1685fSDinh Nguyen {
2443941fcce4SDinh Nguyen 	struct dwc2_hsotg *hsotg = pw;
244447a1685fSDinh Nguyen 	int retry_count = 8;
244547a1685fSDinh Nguyen 	u32 gintsts;
244647a1685fSDinh Nguyen 	u32 gintmsk;
244747a1685fSDinh Nguyen 
244847a1685fSDinh Nguyen 	spin_lock(&hsotg->lock);
244947a1685fSDinh Nguyen irq_retry:
245095c8bc36SAntti Seppälä 	gintsts = dwc2_readl(hsotg->regs + GINTSTS);
245195c8bc36SAntti Seppälä 	gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
245247a1685fSDinh Nguyen 
245347a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
245447a1685fSDinh Nguyen 		__func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
245547a1685fSDinh Nguyen 
245647a1685fSDinh Nguyen 	gintsts &= gintmsk;
245747a1685fSDinh Nguyen 
245847a1685fSDinh Nguyen 	if (gintsts & GINTSTS_ENUMDONE) {
245995c8bc36SAntti Seppälä 		dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
246047a1685fSDinh Nguyen 
24611f91b4ccSFelipe Balbi 		dwc2_hsotg_irq_enumdone(hsotg);
246247a1685fSDinh Nguyen 	}
246347a1685fSDinh Nguyen 
246447a1685fSDinh Nguyen 	if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
246595c8bc36SAntti Seppälä 		u32 daint = dwc2_readl(hsotg->regs + DAINT);
246695c8bc36SAntti Seppälä 		u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
246747a1685fSDinh Nguyen 		u32 daint_out, daint_in;
246847a1685fSDinh Nguyen 		int ep;
246947a1685fSDinh Nguyen 
247047a1685fSDinh Nguyen 		daint &= daintmsk;
247147a1685fSDinh Nguyen 		daint_out = daint >> DAINT_OUTEP_SHIFT;
247247a1685fSDinh Nguyen 		daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
247347a1685fSDinh Nguyen 
247447a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
247547a1685fSDinh Nguyen 
2476cec87f1dSMian Yousaf Kaukab 		for (ep = 0; ep < hsotg->num_of_eps && daint_out;
2477cec87f1dSMian Yousaf Kaukab 						ep++, daint_out >>= 1) {
247847a1685fSDinh Nguyen 			if (daint_out & 1)
24791f91b4ccSFelipe Balbi 				dwc2_hsotg_epint(hsotg, ep, 0);
248047a1685fSDinh Nguyen 		}
248147a1685fSDinh Nguyen 
2482cec87f1dSMian Yousaf Kaukab 		for (ep = 0; ep < hsotg->num_of_eps  && daint_in;
2483cec87f1dSMian Yousaf Kaukab 						ep++, daint_in >>= 1) {
248447a1685fSDinh Nguyen 			if (daint_in & 1)
24851f91b4ccSFelipe Balbi 				dwc2_hsotg_epint(hsotg, ep, 1);
248647a1685fSDinh Nguyen 		}
248747a1685fSDinh Nguyen 	}
248847a1685fSDinh Nguyen 
24894876886fSGregory Herrero 	if (gintsts & GINTSTS_RESETDET) {
24904876886fSGregory Herrero 		dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
24914876886fSGregory Herrero 
249295c8bc36SAntti Seppälä 		dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
24934876886fSGregory Herrero 
24944876886fSGregory Herrero 		/* This event must be used only if controller is suspended */
24954876886fSGregory Herrero 		if (hsotg->lx_state == DWC2_L2) {
24964876886fSGregory Herrero 			dwc2_exit_hibernation(hsotg, true);
24974876886fSGregory Herrero 			hsotg->lx_state = DWC2_L0;
24984876886fSGregory Herrero 		}
24994876886fSGregory Herrero 	}
25004876886fSGregory Herrero 
25014876886fSGregory Herrero 	if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
250247a1685fSDinh Nguyen 
250395c8bc36SAntti Seppälä 		u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
250447a1685fSDinh Nguyen 
25059599815dSMarek Szyprowski 		dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
250647a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
250795c8bc36SAntti Seppälä 			dwc2_readl(hsotg->regs + GNPTXSTS));
250847a1685fSDinh Nguyen 
250995c8bc36SAntti Seppälä 		dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
251047a1685fSDinh Nguyen 
25116d713c15SMian Yousaf Kaukab 		/* Report disconnection if it is not already done. */
25121f91b4ccSFelipe Balbi 		dwc2_hsotg_disconnect(hsotg);
25136d713c15SMian Yousaf Kaukab 
251447a1685fSDinh Nguyen 		if (usb_status & GOTGCTL_BSESVLD) {
251547a1685fSDinh Nguyen 			if (time_after(jiffies, hsotg->last_rst +
251647a1685fSDinh Nguyen 				       msecs_to_jiffies(200))) {
251747a1685fSDinh Nguyen 
2518c6f5c050SMian Yousaf Kaukab 				kill_all_requests(hsotg, hsotg->eps_out[0],
25196b448af4SRobert Baldyga 							  -ECONNRESET);
252047a1685fSDinh Nguyen 
25211f91b4ccSFelipe Balbi 				dwc2_hsotg_core_init_disconnected(hsotg, true);
252247a1685fSDinh Nguyen 			}
252347a1685fSDinh Nguyen 		}
252447a1685fSDinh Nguyen 	}
252547a1685fSDinh Nguyen 
252647a1685fSDinh Nguyen 	/* check both FIFOs */
252747a1685fSDinh Nguyen 
252847a1685fSDinh Nguyen 	if (gintsts & GINTSTS_NPTXFEMP) {
252947a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "NPTxFEmp\n");
253047a1685fSDinh Nguyen 
253147a1685fSDinh Nguyen 		/*
253247a1685fSDinh Nguyen 		 * Disable the interrupt to stop it happening again
253347a1685fSDinh Nguyen 		 * unless one of these endpoint routines decides that
253447a1685fSDinh Nguyen 		 * it needs re-enabling
253547a1685fSDinh Nguyen 		 */
253647a1685fSDinh Nguyen 
25371f91b4ccSFelipe Balbi 		dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
25381f91b4ccSFelipe Balbi 		dwc2_hsotg_irq_fifoempty(hsotg, false);
253947a1685fSDinh Nguyen 	}
254047a1685fSDinh Nguyen 
254147a1685fSDinh Nguyen 	if (gintsts & GINTSTS_PTXFEMP) {
254247a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "PTxFEmp\n");
254347a1685fSDinh Nguyen 
254447a1685fSDinh Nguyen 		/* See note in GINTSTS_NPTxFEmp */
254547a1685fSDinh Nguyen 
25461f91b4ccSFelipe Balbi 		dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
25471f91b4ccSFelipe Balbi 		dwc2_hsotg_irq_fifoempty(hsotg, true);
254847a1685fSDinh Nguyen 	}
254947a1685fSDinh Nguyen 
255047a1685fSDinh Nguyen 	if (gintsts & GINTSTS_RXFLVL) {
255147a1685fSDinh Nguyen 		/*
255247a1685fSDinh Nguyen 		 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
25531f91b4ccSFelipe Balbi 		 * we need to retry dwc2_hsotg_handle_rx if this is still
255447a1685fSDinh Nguyen 		 * set.
255547a1685fSDinh Nguyen 		 */
255647a1685fSDinh Nguyen 
25571f91b4ccSFelipe Balbi 		dwc2_hsotg_handle_rx(hsotg);
255847a1685fSDinh Nguyen 	}
255947a1685fSDinh Nguyen 
256047a1685fSDinh Nguyen 	if (gintsts & GINTSTS_ERLYSUSP) {
256147a1685fSDinh Nguyen 		dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
256295c8bc36SAntti Seppälä 		dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
256347a1685fSDinh Nguyen 	}
256447a1685fSDinh Nguyen 
256547a1685fSDinh Nguyen 	/*
256647a1685fSDinh Nguyen 	 * these next two seem to crop-up occasionally causing the core
256747a1685fSDinh Nguyen 	 * to shutdown the USB transfer, so try clearing them and logging
256847a1685fSDinh Nguyen 	 * the occurrence.
256947a1685fSDinh Nguyen 	 */
257047a1685fSDinh Nguyen 
257147a1685fSDinh Nguyen 	if (gintsts & GINTSTS_GOUTNAKEFF) {
257247a1685fSDinh Nguyen 		dev_info(hsotg->dev, "GOUTNakEff triggered\n");
257347a1685fSDinh Nguyen 
257495c8bc36SAntti Seppälä 		dwc2_writel(DCTL_CGOUTNAK, hsotg->regs + DCTL);
257547a1685fSDinh Nguyen 
25761f91b4ccSFelipe Balbi 		dwc2_hsotg_dump(hsotg);
257747a1685fSDinh Nguyen 	}
257847a1685fSDinh Nguyen 
257947a1685fSDinh Nguyen 	if (gintsts & GINTSTS_GINNAKEFF) {
258047a1685fSDinh Nguyen 		dev_info(hsotg->dev, "GINNakEff triggered\n");
258147a1685fSDinh Nguyen 
258295c8bc36SAntti Seppälä 		dwc2_writel(DCTL_CGNPINNAK, hsotg->regs + DCTL);
258347a1685fSDinh Nguyen 
25841f91b4ccSFelipe Balbi 		dwc2_hsotg_dump(hsotg);
258547a1685fSDinh Nguyen 	}
258647a1685fSDinh Nguyen 
258747a1685fSDinh Nguyen 	/*
258847a1685fSDinh Nguyen 	 * if we've had fifo events, we should try and go around the
258947a1685fSDinh Nguyen 	 * loop again to see if there's any point in returning yet.
259047a1685fSDinh Nguyen 	 */
259147a1685fSDinh Nguyen 
259247a1685fSDinh Nguyen 	if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
259347a1685fSDinh Nguyen 			goto irq_retry;
259447a1685fSDinh Nguyen 
259547a1685fSDinh Nguyen 	spin_unlock(&hsotg->lock);
259647a1685fSDinh Nguyen 
259747a1685fSDinh Nguyen 	return IRQ_HANDLED;
259847a1685fSDinh Nguyen }
259947a1685fSDinh Nguyen 
260047a1685fSDinh Nguyen /**
26011f91b4ccSFelipe Balbi  * dwc2_hsotg_ep_enable - enable the given endpoint
260247a1685fSDinh Nguyen  * @ep: The USB endpint to configure
260347a1685fSDinh Nguyen  * @desc: The USB endpoint descriptor to configure with.
260447a1685fSDinh Nguyen  *
260547a1685fSDinh Nguyen  * This is called from the USB gadget code's usb_ep_enable().
260647a1685fSDinh Nguyen  */
26071f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
260847a1685fSDinh Nguyen 			       const struct usb_endpoint_descriptor *desc)
260947a1685fSDinh Nguyen {
26101f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
2611941fcce4SDinh Nguyen 	struct dwc2_hsotg *hsotg = hs_ep->parent;
261247a1685fSDinh Nguyen 	unsigned long flags;
2613ca4c55adSMian Yousaf Kaukab 	unsigned int index = hs_ep->index;
261447a1685fSDinh Nguyen 	u32 epctrl_reg;
261547a1685fSDinh Nguyen 	u32 epctrl;
261647a1685fSDinh Nguyen 	u32 mps;
2617ca4c55adSMian Yousaf Kaukab 	unsigned int dir_in;
2618ca4c55adSMian Yousaf Kaukab 	unsigned int i, val, size;
261947a1685fSDinh Nguyen 	int ret = 0;
262047a1685fSDinh Nguyen 
262147a1685fSDinh Nguyen 	dev_dbg(hsotg->dev,
262247a1685fSDinh Nguyen 		"%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
262347a1685fSDinh Nguyen 		__func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
262447a1685fSDinh Nguyen 		desc->wMaxPacketSize, desc->bInterval);
262547a1685fSDinh Nguyen 
262647a1685fSDinh Nguyen 	/* not to be called for EP0 */
262747a1685fSDinh Nguyen 	WARN_ON(index == 0);
262847a1685fSDinh Nguyen 
262947a1685fSDinh Nguyen 	dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
263047a1685fSDinh Nguyen 	if (dir_in != hs_ep->dir_in) {
263147a1685fSDinh Nguyen 		dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
263247a1685fSDinh Nguyen 		return -EINVAL;
263347a1685fSDinh Nguyen 	}
263447a1685fSDinh Nguyen 
263547a1685fSDinh Nguyen 	mps = usb_endpoint_maxp(desc);
263647a1685fSDinh Nguyen 
26371f91b4ccSFelipe Balbi 	/* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
263847a1685fSDinh Nguyen 
263947a1685fSDinh Nguyen 	epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
264095c8bc36SAntti Seppälä 	epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
264147a1685fSDinh Nguyen 
264247a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
264347a1685fSDinh Nguyen 		__func__, epctrl, epctrl_reg);
264447a1685fSDinh Nguyen 
264547a1685fSDinh Nguyen 	spin_lock_irqsave(&hsotg->lock, flags);
264647a1685fSDinh Nguyen 
264747a1685fSDinh Nguyen 	epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
264847a1685fSDinh Nguyen 	epctrl |= DXEPCTL_MPS(mps);
264947a1685fSDinh Nguyen 
265047a1685fSDinh Nguyen 	/*
265147a1685fSDinh Nguyen 	 * mark the endpoint as active, otherwise the core may ignore
265247a1685fSDinh Nguyen 	 * transactions entirely for this endpoint
265347a1685fSDinh Nguyen 	 */
265447a1685fSDinh Nguyen 	epctrl |= DXEPCTL_USBACTEP;
265547a1685fSDinh Nguyen 
265647a1685fSDinh Nguyen 	/*
265747a1685fSDinh Nguyen 	 * set the NAK status on the endpoint, otherwise we might try and
265847a1685fSDinh Nguyen 	 * do something with data that we've yet got a request to process
265947a1685fSDinh Nguyen 	 * since the RXFIFO will take data for an endpoint even if the
266047a1685fSDinh Nguyen 	 * size register hasn't been set.
266147a1685fSDinh Nguyen 	 */
266247a1685fSDinh Nguyen 
266347a1685fSDinh Nguyen 	epctrl |= DXEPCTL_SNAK;
266447a1685fSDinh Nguyen 
266547a1685fSDinh Nguyen 	/* update the endpoint state */
26661f91b4ccSFelipe Balbi 	dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, dir_in);
266747a1685fSDinh Nguyen 
266847a1685fSDinh Nguyen 	/* default, set to non-periodic */
266947a1685fSDinh Nguyen 	hs_ep->isochronous = 0;
267047a1685fSDinh Nguyen 	hs_ep->periodic = 0;
267147a1685fSDinh Nguyen 	hs_ep->halted = 0;
267247a1685fSDinh Nguyen 	hs_ep->interval = desc->bInterval;
267347a1685fSDinh Nguyen 
267447a1685fSDinh Nguyen 	if (hs_ep->interval > 1 && hs_ep->mc > 1)
267547a1685fSDinh Nguyen 		dev_err(hsotg->dev, "MC > 1 when interval is not 1\n");
267647a1685fSDinh Nguyen 
267747a1685fSDinh Nguyen 	switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
267847a1685fSDinh Nguyen 	case USB_ENDPOINT_XFER_ISOC:
267947a1685fSDinh Nguyen 		epctrl |= DXEPCTL_EPTYPE_ISO;
268047a1685fSDinh Nguyen 		epctrl |= DXEPCTL_SETEVENFR;
268147a1685fSDinh Nguyen 		hs_ep->isochronous = 1;
268247a1685fSDinh Nguyen 		if (dir_in)
268347a1685fSDinh Nguyen 			hs_ep->periodic = 1;
268447a1685fSDinh Nguyen 		break;
268547a1685fSDinh Nguyen 
268647a1685fSDinh Nguyen 	case USB_ENDPOINT_XFER_BULK:
268747a1685fSDinh Nguyen 		epctrl |= DXEPCTL_EPTYPE_BULK;
268847a1685fSDinh Nguyen 		break;
268947a1685fSDinh Nguyen 
269047a1685fSDinh Nguyen 	case USB_ENDPOINT_XFER_INT:
2691b203d0a2SRobert Baldyga 		if (dir_in)
269247a1685fSDinh Nguyen 			hs_ep->periodic = 1;
269347a1685fSDinh Nguyen 
269447a1685fSDinh Nguyen 		epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
269547a1685fSDinh Nguyen 		break;
269647a1685fSDinh Nguyen 
269747a1685fSDinh Nguyen 	case USB_ENDPOINT_XFER_CONTROL:
269847a1685fSDinh Nguyen 		epctrl |= DXEPCTL_EPTYPE_CONTROL;
269947a1685fSDinh Nguyen 		break;
270047a1685fSDinh Nguyen 	}
270147a1685fSDinh Nguyen 
27024556e12cSMian Yousaf Kaukab 	/* If fifo is already allocated for this ep */
27034556e12cSMian Yousaf Kaukab 	if (hs_ep->fifo_index) {
27044556e12cSMian Yousaf Kaukab 		size =  hs_ep->ep.maxpacket * hs_ep->mc;
27054556e12cSMian Yousaf Kaukab 		/* If bigger fifo is required deallocate current one */
27064556e12cSMian Yousaf Kaukab 		if (size > hs_ep->fifo_size) {
27074556e12cSMian Yousaf Kaukab 			hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
27084556e12cSMian Yousaf Kaukab 			hs_ep->fifo_index = 0;
27094556e12cSMian Yousaf Kaukab 			hs_ep->fifo_size = 0;
27104556e12cSMian Yousaf Kaukab 		}
27114556e12cSMian Yousaf Kaukab 	}
27124556e12cSMian Yousaf Kaukab 
271347a1685fSDinh Nguyen 	/*
271447a1685fSDinh Nguyen 	 * if the hardware has dedicated fifos, we must give each IN EP
271547a1685fSDinh Nguyen 	 * a unique tx-fifo even if it is non-periodic.
271647a1685fSDinh Nguyen 	 */
27174556e12cSMian Yousaf Kaukab 	if (dir_in && hsotg->dedicated_fifos && !hs_ep->fifo_index) {
2718ca4c55adSMian Yousaf Kaukab 		u32 fifo_index = 0;
2719ca4c55adSMian Yousaf Kaukab 		u32 fifo_size = UINT_MAX;
2720b203d0a2SRobert Baldyga 		size = hs_ep->ep.maxpacket*hs_ep->mc;
27215f2196bdSMian Yousaf Kaukab 		for (i = 1; i < hsotg->num_of_eps; ++i) {
2722b203d0a2SRobert Baldyga 			if (hsotg->fifo_map & (1<<i))
2723b203d0a2SRobert Baldyga 				continue;
272495c8bc36SAntti Seppälä 			val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
2725b203d0a2SRobert Baldyga 			val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
2726b203d0a2SRobert Baldyga 			if (val < size)
2727b203d0a2SRobert Baldyga 				continue;
2728ca4c55adSMian Yousaf Kaukab 			/* Search for smallest acceptable fifo */
2729ca4c55adSMian Yousaf Kaukab 			if (val < fifo_size) {
2730ca4c55adSMian Yousaf Kaukab 				fifo_size = val;
2731ca4c55adSMian Yousaf Kaukab 				fifo_index = i;
2732b203d0a2SRobert Baldyga 			}
2733ca4c55adSMian Yousaf Kaukab 		}
2734ca4c55adSMian Yousaf Kaukab 		if (!fifo_index) {
27355f2196bdSMian Yousaf Kaukab 			dev_err(hsotg->dev,
27365f2196bdSMian Yousaf Kaukab 				"%s: No suitable fifo found\n", __func__);
2737b585a48bSSudip Mukherjee 			ret = -ENOMEM;
2738b585a48bSSudip Mukherjee 			goto error;
2739b585a48bSSudip Mukherjee 		}
2740ca4c55adSMian Yousaf Kaukab 		hsotg->fifo_map |= 1 << fifo_index;
2741ca4c55adSMian Yousaf Kaukab 		epctrl |= DXEPCTL_TXFNUM(fifo_index);
2742ca4c55adSMian Yousaf Kaukab 		hs_ep->fifo_index = fifo_index;
2743ca4c55adSMian Yousaf Kaukab 		hs_ep->fifo_size = fifo_size;
2744b203d0a2SRobert Baldyga 	}
274547a1685fSDinh Nguyen 
274647a1685fSDinh Nguyen 	/* for non control endpoints, set PID to D0 */
274747a1685fSDinh Nguyen 	if (index)
274847a1685fSDinh Nguyen 		epctrl |= DXEPCTL_SETD0PID;
274947a1685fSDinh Nguyen 
275047a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
275147a1685fSDinh Nguyen 		__func__, epctrl);
275247a1685fSDinh Nguyen 
275395c8bc36SAntti Seppälä 	dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
275447a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
275595c8bc36SAntti Seppälä 		__func__, dwc2_readl(hsotg->regs + epctrl_reg));
275647a1685fSDinh Nguyen 
275747a1685fSDinh Nguyen 	/* enable the endpoint interrupt */
27581f91b4ccSFelipe Balbi 	dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
275947a1685fSDinh Nguyen 
2760b585a48bSSudip Mukherjee error:
276147a1685fSDinh Nguyen 	spin_unlock_irqrestore(&hsotg->lock, flags);
276247a1685fSDinh Nguyen 	return ret;
276347a1685fSDinh Nguyen }
276447a1685fSDinh Nguyen 
276547a1685fSDinh Nguyen /**
27661f91b4ccSFelipe Balbi  * dwc2_hsotg_ep_disable - disable given endpoint
276747a1685fSDinh Nguyen  * @ep: The endpoint to disable.
276847a1685fSDinh Nguyen  */
27691f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
277047a1685fSDinh Nguyen {
27711f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
2772941fcce4SDinh Nguyen 	struct dwc2_hsotg *hsotg = hs_ep->parent;
277347a1685fSDinh Nguyen 	int dir_in = hs_ep->dir_in;
277447a1685fSDinh Nguyen 	int index = hs_ep->index;
277547a1685fSDinh Nguyen 	unsigned long flags;
277647a1685fSDinh Nguyen 	u32 epctrl_reg;
277747a1685fSDinh Nguyen 	u32 ctrl;
277847a1685fSDinh Nguyen 
27791e011293SMarek Szyprowski 	dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
278047a1685fSDinh Nguyen 
2781c6f5c050SMian Yousaf Kaukab 	if (ep == &hsotg->eps_out[0]->ep) {
278247a1685fSDinh Nguyen 		dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
278347a1685fSDinh Nguyen 		return -EINVAL;
278447a1685fSDinh Nguyen 	}
278547a1685fSDinh Nguyen 
278647a1685fSDinh Nguyen 	epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
278747a1685fSDinh Nguyen 
278847a1685fSDinh Nguyen 	spin_lock_irqsave(&hsotg->lock, flags);
278947a1685fSDinh Nguyen 
2790b203d0a2SRobert Baldyga 	hsotg->fifo_map &= ~(1<<hs_ep->fifo_index);
2791b203d0a2SRobert Baldyga 	hs_ep->fifo_index = 0;
2792b203d0a2SRobert Baldyga 	hs_ep->fifo_size = 0;
279347a1685fSDinh Nguyen 
279495c8bc36SAntti Seppälä 	ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
279547a1685fSDinh Nguyen 	ctrl &= ~DXEPCTL_EPENA;
279647a1685fSDinh Nguyen 	ctrl &= ~DXEPCTL_USBACTEP;
279747a1685fSDinh Nguyen 	ctrl |= DXEPCTL_SNAK;
279847a1685fSDinh Nguyen 
279947a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
280095c8bc36SAntti Seppälä 	dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
280147a1685fSDinh Nguyen 
280247a1685fSDinh Nguyen 	/* disable endpoint interrupts */
28031f91b4ccSFelipe Balbi 	dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
280447a1685fSDinh Nguyen 
28051141ea01SMian Yousaf Kaukab 	/* terminate all requests with shutdown */
28061141ea01SMian Yousaf Kaukab 	kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
28071141ea01SMian Yousaf Kaukab 
280847a1685fSDinh Nguyen 	spin_unlock_irqrestore(&hsotg->lock, flags);
280947a1685fSDinh Nguyen 	return 0;
281047a1685fSDinh Nguyen }
281147a1685fSDinh Nguyen 
281247a1685fSDinh Nguyen /**
281347a1685fSDinh Nguyen  * on_list - check request is on the given endpoint
281447a1685fSDinh Nguyen  * @ep: The endpoint to check.
281547a1685fSDinh Nguyen  * @test: The request to test if it is on the endpoint.
281647a1685fSDinh Nguyen  */
28171f91b4ccSFelipe Balbi static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
281847a1685fSDinh Nguyen {
28191f91b4ccSFelipe Balbi 	struct dwc2_hsotg_req *req, *treq;
282047a1685fSDinh Nguyen 
282147a1685fSDinh Nguyen 	list_for_each_entry_safe(req, treq, &ep->queue, queue) {
282247a1685fSDinh Nguyen 		if (req == test)
282347a1685fSDinh Nguyen 			return true;
282447a1685fSDinh Nguyen 	}
282547a1685fSDinh Nguyen 
282647a1685fSDinh Nguyen 	return false;
282747a1685fSDinh Nguyen }
282847a1685fSDinh Nguyen 
2829*c524dd5fSMian Yousaf Kaukab static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
2830*c524dd5fSMian Yousaf Kaukab 							u32 bit, u32 timeout)
2831*c524dd5fSMian Yousaf Kaukab {
2832*c524dd5fSMian Yousaf Kaukab 	u32 i;
2833*c524dd5fSMian Yousaf Kaukab 
2834*c524dd5fSMian Yousaf Kaukab 	for (i = 0; i < timeout; i++) {
2835*c524dd5fSMian Yousaf Kaukab 		if (dwc2_readl(hs_otg->regs + reg) & bit)
2836*c524dd5fSMian Yousaf Kaukab 			return 0;
2837*c524dd5fSMian Yousaf Kaukab 		udelay(1);
2838*c524dd5fSMian Yousaf Kaukab 	}
2839*c524dd5fSMian Yousaf Kaukab 
2840*c524dd5fSMian Yousaf Kaukab 	return -ETIMEDOUT;
2841*c524dd5fSMian Yousaf Kaukab }
2842*c524dd5fSMian Yousaf Kaukab 
2843*c524dd5fSMian Yousaf Kaukab static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
2844*c524dd5fSMian Yousaf Kaukab 						struct dwc2_hsotg_ep *hs_ep)
2845*c524dd5fSMian Yousaf Kaukab {
2846*c524dd5fSMian Yousaf Kaukab 	u32 epctrl_reg;
2847*c524dd5fSMian Yousaf Kaukab 	u32 epint_reg;
2848*c524dd5fSMian Yousaf Kaukab 
2849*c524dd5fSMian Yousaf Kaukab 	epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
2850*c524dd5fSMian Yousaf Kaukab 		DOEPCTL(hs_ep->index);
2851*c524dd5fSMian Yousaf Kaukab 	epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
2852*c524dd5fSMian Yousaf Kaukab 		DOEPINT(hs_ep->index);
2853*c524dd5fSMian Yousaf Kaukab 
2854*c524dd5fSMian Yousaf Kaukab 	dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
2855*c524dd5fSMian Yousaf Kaukab 			hs_ep->name);
2856*c524dd5fSMian Yousaf Kaukab 	if (hs_ep->dir_in) {
2857*c524dd5fSMian Yousaf Kaukab 		__orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
2858*c524dd5fSMian Yousaf Kaukab 		/* Wait for Nak effect */
2859*c524dd5fSMian Yousaf Kaukab 		if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
2860*c524dd5fSMian Yousaf Kaukab 						DXEPINT_INEPNAKEFF, 100))
2861*c524dd5fSMian Yousaf Kaukab 			dev_warn(hsotg->dev,
2862*c524dd5fSMian Yousaf Kaukab 				"%s: timeout DIEPINT.NAKEFF\n", __func__);
2863*c524dd5fSMian Yousaf Kaukab 	} else {
2864*c524dd5fSMian Yousaf Kaukab 		/* Clear any pending nak effect interrupt */
2865*c524dd5fSMian Yousaf Kaukab 		dwc2_writel(GINTSTS_GINNAKEFF, hsotg->regs + GINTSTS);
2866*c524dd5fSMian Yousaf Kaukab 
2867*c524dd5fSMian Yousaf Kaukab 		__orr32(hsotg->regs + DCTL, DCTL_SGNPINNAK);
2868*c524dd5fSMian Yousaf Kaukab 
2869*c524dd5fSMian Yousaf Kaukab 		/* Wait for global nak to take effect */
2870*c524dd5fSMian Yousaf Kaukab 		if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
2871*c524dd5fSMian Yousaf Kaukab 						GINTSTS_GINNAKEFF, 100))
2872*c524dd5fSMian Yousaf Kaukab 			dev_warn(hsotg->dev,
2873*c524dd5fSMian Yousaf Kaukab 				"%s: timeout GINTSTS.GINNAKEFF\n", __func__);
2874*c524dd5fSMian Yousaf Kaukab 	}
2875*c524dd5fSMian Yousaf Kaukab 
2876*c524dd5fSMian Yousaf Kaukab 	/* Disable ep */
2877*c524dd5fSMian Yousaf Kaukab 	__orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
2878*c524dd5fSMian Yousaf Kaukab 
2879*c524dd5fSMian Yousaf Kaukab 	/* Wait for ep to be disabled */
2880*c524dd5fSMian Yousaf Kaukab 	if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
2881*c524dd5fSMian Yousaf Kaukab 		dev_warn(hsotg->dev,
2882*c524dd5fSMian Yousaf Kaukab 			"%s: timeout DOEPCTL.EPDisable\n", __func__);
2883*c524dd5fSMian Yousaf Kaukab 
2884*c524dd5fSMian Yousaf Kaukab 	if (hs_ep->dir_in) {
2885*c524dd5fSMian Yousaf Kaukab 		if (hsotg->dedicated_fifos) {
2886*c524dd5fSMian Yousaf Kaukab 			dwc2_writel(GRSTCTL_TXFNUM(hs_ep->fifo_index) |
2887*c524dd5fSMian Yousaf Kaukab 				GRSTCTL_TXFFLSH, hsotg->regs + GRSTCTL);
2888*c524dd5fSMian Yousaf Kaukab 			/* Wait for fifo flush */
2889*c524dd5fSMian Yousaf Kaukab 			if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL,
2890*c524dd5fSMian Yousaf Kaukab 							GRSTCTL_TXFFLSH, 100))
2891*c524dd5fSMian Yousaf Kaukab 				dev_warn(hsotg->dev,
2892*c524dd5fSMian Yousaf Kaukab 					"%s: timeout flushing fifos\n",
2893*c524dd5fSMian Yousaf Kaukab 					__func__);
2894*c524dd5fSMian Yousaf Kaukab 		}
2895*c524dd5fSMian Yousaf Kaukab 		/* TODO: Flush shared tx fifo */
2896*c524dd5fSMian Yousaf Kaukab 	} else {
2897*c524dd5fSMian Yousaf Kaukab 		/* Remove global NAKs */
2898*c524dd5fSMian Yousaf Kaukab 		__bic32(hsotg->regs + DCTL, DCTL_SGNPINNAK);
2899*c524dd5fSMian Yousaf Kaukab 	}
2900*c524dd5fSMian Yousaf Kaukab }
2901*c524dd5fSMian Yousaf Kaukab 
290247a1685fSDinh Nguyen /**
29031f91b4ccSFelipe Balbi  * dwc2_hsotg_ep_dequeue - dequeue given endpoint
290447a1685fSDinh Nguyen  * @ep: The endpoint to dequeue.
290547a1685fSDinh Nguyen  * @req: The request to be removed from a queue.
290647a1685fSDinh Nguyen  */
29071f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
290847a1685fSDinh Nguyen {
29091f91b4ccSFelipe Balbi 	struct dwc2_hsotg_req *hs_req = our_req(req);
29101f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
2911941fcce4SDinh Nguyen 	struct dwc2_hsotg *hs = hs_ep->parent;
291247a1685fSDinh Nguyen 	unsigned long flags;
291347a1685fSDinh Nguyen 
29141e011293SMarek Szyprowski 	dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
291547a1685fSDinh Nguyen 
291647a1685fSDinh Nguyen 	spin_lock_irqsave(&hs->lock, flags);
291747a1685fSDinh Nguyen 
291847a1685fSDinh Nguyen 	if (!on_list(hs_ep, hs_req)) {
291947a1685fSDinh Nguyen 		spin_unlock_irqrestore(&hs->lock, flags);
292047a1685fSDinh Nguyen 		return -EINVAL;
292147a1685fSDinh Nguyen 	}
292247a1685fSDinh Nguyen 
2923*c524dd5fSMian Yousaf Kaukab 	/* Dequeue already started request */
2924*c524dd5fSMian Yousaf Kaukab 	if (req == &hs_ep->req->req)
2925*c524dd5fSMian Yousaf Kaukab 		dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
2926*c524dd5fSMian Yousaf Kaukab 
29271f91b4ccSFelipe Balbi 	dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
292847a1685fSDinh Nguyen 	spin_unlock_irqrestore(&hs->lock, flags);
292947a1685fSDinh Nguyen 
293047a1685fSDinh Nguyen 	return 0;
293147a1685fSDinh Nguyen }
293247a1685fSDinh Nguyen 
293347a1685fSDinh Nguyen /**
29341f91b4ccSFelipe Balbi  * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
293547a1685fSDinh Nguyen  * @ep: The endpoint to set halt.
293647a1685fSDinh Nguyen  * @value: Set or unset the halt.
293747a1685fSDinh Nguyen  */
29381f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value)
293947a1685fSDinh Nguyen {
29401f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
2941941fcce4SDinh Nguyen 	struct dwc2_hsotg *hs = hs_ep->parent;
294247a1685fSDinh Nguyen 	int index = hs_ep->index;
294347a1685fSDinh Nguyen 	u32 epreg;
294447a1685fSDinh Nguyen 	u32 epctl;
294547a1685fSDinh Nguyen 	u32 xfertype;
294647a1685fSDinh Nguyen 
294747a1685fSDinh Nguyen 	dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
294847a1685fSDinh Nguyen 
294947a1685fSDinh Nguyen 	if (index == 0) {
295047a1685fSDinh Nguyen 		if (value)
29511f91b4ccSFelipe Balbi 			dwc2_hsotg_stall_ep0(hs);
295247a1685fSDinh Nguyen 		else
295347a1685fSDinh Nguyen 			dev_warn(hs->dev,
295447a1685fSDinh Nguyen 				 "%s: can't clear halt on ep0\n", __func__);
295547a1685fSDinh Nguyen 		return 0;
295647a1685fSDinh Nguyen 	}
295747a1685fSDinh Nguyen 
2958c6f5c050SMian Yousaf Kaukab 	if (hs_ep->dir_in) {
295947a1685fSDinh Nguyen 		epreg = DIEPCTL(index);
296095c8bc36SAntti Seppälä 		epctl = dwc2_readl(hs->regs + epreg);
296147a1685fSDinh Nguyen 
296247a1685fSDinh Nguyen 		if (value) {
29635a350d53SFelipe Balbi 			epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
296447a1685fSDinh Nguyen 			if (epctl & DXEPCTL_EPENA)
296547a1685fSDinh Nguyen 				epctl |= DXEPCTL_EPDIS;
296647a1685fSDinh Nguyen 		} else {
296747a1685fSDinh Nguyen 			epctl &= ~DXEPCTL_STALL;
296847a1685fSDinh Nguyen 			xfertype = epctl & DXEPCTL_EPTYPE_MASK;
296947a1685fSDinh Nguyen 			if (xfertype == DXEPCTL_EPTYPE_BULK ||
297047a1685fSDinh Nguyen 				xfertype == DXEPCTL_EPTYPE_INTERRUPT)
297147a1685fSDinh Nguyen 					epctl |= DXEPCTL_SETD0PID;
297247a1685fSDinh Nguyen 		}
297395c8bc36SAntti Seppälä 		dwc2_writel(epctl, hs->regs + epreg);
2974c6f5c050SMian Yousaf Kaukab 	} else {
297547a1685fSDinh Nguyen 
297647a1685fSDinh Nguyen 		epreg = DOEPCTL(index);
297795c8bc36SAntti Seppälä 		epctl = dwc2_readl(hs->regs + epreg);
297847a1685fSDinh Nguyen 
297947a1685fSDinh Nguyen 		if (value)
298047a1685fSDinh Nguyen 			epctl |= DXEPCTL_STALL;
298147a1685fSDinh Nguyen 		else {
298247a1685fSDinh Nguyen 			epctl &= ~DXEPCTL_STALL;
298347a1685fSDinh Nguyen 			xfertype = epctl & DXEPCTL_EPTYPE_MASK;
298447a1685fSDinh Nguyen 			if (xfertype == DXEPCTL_EPTYPE_BULK ||
298547a1685fSDinh Nguyen 				xfertype == DXEPCTL_EPTYPE_INTERRUPT)
298647a1685fSDinh Nguyen 					epctl |= DXEPCTL_SETD0PID;
298747a1685fSDinh Nguyen 		}
298895c8bc36SAntti Seppälä 		dwc2_writel(epctl, hs->regs + epreg);
2989c6f5c050SMian Yousaf Kaukab 	}
299047a1685fSDinh Nguyen 
299147a1685fSDinh Nguyen 	hs_ep->halted = value;
299247a1685fSDinh Nguyen 
299347a1685fSDinh Nguyen 	return 0;
299447a1685fSDinh Nguyen }
299547a1685fSDinh Nguyen 
299647a1685fSDinh Nguyen /**
29971f91b4ccSFelipe Balbi  * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
299847a1685fSDinh Nguyen  * @ep: The endpoint to set halt.
299947a1685fSDinh Nguyen  * @value: Set or unset the halt.
300047a1685fSDinh Nguyen  */
30011f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
300247a1685fSDinh Nguyen {
30031f91b4ccSFelipe Balbi 	struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
3004941fcce4SDinh Nguyen 	struct dwc2_hsotg *hs = hs_ep->parent;
300547a1685fSDinh Nguyen 	unsigned long flags = 0;
300647a1685fSDinh Nguyen 	int ret = 0;
300747a1685fSDinh Nguyen 
300847a1685fSDinh Nguyen 	spin_lock_irqsave(&hs->lock, flags);
30091f91b4ccSFelipe Balbi 	ret = dwc2_hsotg_ep_sethalt(ep, value);
301047a1685fSDinh Nguyen 	spin_unlock_irqrestore(&hs->lock, flags);
301147a1685fSDinh Nguyen 
301247a1685fSDinh Nguyen 	return ret;
301347a1685fSDinh Nguyen }
301447a1685fSDinh Nguyen 
30151f91b4ccSFelipe Balbi static struct usb_ep_ops dwc2_hsotg_ep_ops = {
30161f91b4ccSFelipe Balbi 	.enable		= dwc2_hsotg_ep_enable,
30171f91b4ccSFelipe Balbi 	.disable	= dwc2_hsotg_ep_disable,
30181f91b4ccSFelipe Balbi 	.alloc_request	= dwc2_hsotg_ep_alloc_request,
30191f91b4ccSFelipe Balbi 	.free_request	= dwc2_hsotg_ep_free_request,
30201f91b4ccSFelipe Balbi 	.queue		= dwc2_hsotg_ep_queue_lock,
30211f91b4ccSFelipe Balbi 	.dequeue	= dwc2_hsotg_ep_dequeue,
30221f91b4ccSFelipe Balbi 	.set_halt	= dwc2_hsotg_ep_sethalt_lock,
302347a1685fSDinh Nguyen 	/* note, don't believe we have any call for the fifo routines */
302447a1685fSDinh Nguyen };
302547a1685fSDinh Nguyen 
302647a1685fSDinh Nguyen /**
30271f91b4ccSFelipe Balbi  * dwc2_hsotg_phy_enable - enable platform phy dev
302847a1685fSDinh Nguyen  * @hsotg: The driver state
302947a1685fSDinh Nguyen  *
303047a1685fSDinh Nguyen  * A wrapper for platform code responsible for controlling
303147a1685fSDinh Nguyen  * low-level USB code
303247a1685fSDinh Nguyen  */
30331f91b4ccSFelipe Balbi static void dwc2_hsotg_phy_enable(struct dwc2_hsotg *hsotg)
303447a1685fSDinh Nguyen {
303547a1685fSDinh Nguyen 	struct platform_device *pdev = to_platform_device(hsotg->dev);
303647a1685fSDinh Nguyen 
303747a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
303847a1685fSDinh Nguyen 
3039ca2c5ba8SKamil Debski 	if (hsotg->uphy)
3040ca2c5ba8SKamil Debski 		usb_phy_init(hsotg->uphy);
3041ca2c5ba8SKamil Debski 	else if (hsotg->plat && hsotg->plat->phy_init)
3042ca2c5ba8SKamil Debski 		hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
3043ca2c5ba8SKamil Debski 	else {
304447a1685fSDinh Nguyen 		phy_init(hsotg->phy);
304547a1685fSDinh Nguyen 		phy_power_on(hsotg->phy);
3046ca2c5ba8SKamil Debski 	}
304747a1685fSDinh Nguyen }
304847a1685fSDinh Nguyen 
304947a1685fSDinh Nguyen /**
30501f91b4ccSFelipe Balbi  * dwc2_hsotg_phy_disable - disable platform phy dev
305147a1685fSDinh Nguyen  * @hsotg: The driver state
305247a1685fSDinh Nguyen  *
305347a1685fSDinh Nguyen  * A wrapper for platform code responsible for controlling
305447a1685fSDinh Nguyen  * low-level USB code
305547a1685fSDinh Nguyen  */
30561f91b4ccSFelipe Balbi static void dwc2_hsotg_phy_disable(struct dwc2_hsotg *hsotg)
305747a1685fSDinh Nguyen {
305847a1685fSDinh Nguyen 	struct platform_device *pdev = to_platform_device(hsotg->dev);
305947a1685fSDinh Nguyen 
3060ca2c5ba8SKamil Debski 	if (hsotg->uphy)
3061ca2c5ba8SKamil Debski 		usb_phy_shutdown(hsotg->uphy);
3062ca2c5ba8SKamil Debski 	else if (hsotg->plat && hsotg->plat->phy_exit)
3063ca2c5ba8SKamil Debski 		hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
3064ca2c5ba8SKamil Debski 	else {
306547a1685fSDinh Nguyen 		phy_power_off(hsotg->phy);
306647a1685fSDinh Nguyen 		phy_exit(hsotg->phy);
3067ca2c5ba8SKamil Debski 	}
306847a1685fSDinh Nguyen }
306947a1685fSDinh Nguyen 
307047a1685fSDinh Nguyen /**
30711f91b4ccSFelipe Balbi  * dwc2_hsotg_init - initalize the usb core
307247a1685fSDinh Nguyen  * @hsotg: The driver state
307347a1685fSDinh Nguyen  */
30741f91b4ccSFelipe Balbi static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
307547a1685fSDinh Nguyen {
3076fa4a8d72SMian Yousaf Kaukab 	u32 trdtim;
307747a1685fSDinh Nguyen 	/* unmask subset of endpoint interrupts */
307847a1685fSDinh Nguyen 
307995c8bc36SAntti Seppälä 	dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
308047a1685fSDinh Nguyen 		    DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
308147a1685fSDinh Nguyen 		    hsotg->regs + DIEPMSK);
308247a1685fSDinh Nguyen 
308395c8bc36SAntti Seppälä 	dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
308447a1685fSDinh Nguyen 		    DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
308547a1685fSDinh Nguyen 		    hsotg->regs + DOEPMSK);
308647a1685fSDinh Nguyen 
308795c8bc36SAntti Seppälä 	dwc2_writel(0, hsotg->regs + DAINTMSK);
308847a1685fSDinh Nguyen 
308947a1685fSDinh Nguyen 	/* Be in disconnected state until gadget is registered */
309047a1685fSDinh Nguyen 	__orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
309147a1685fSDinh Nguyen 
309247a1685fSDinh Nguyen 	/* setup fifos */
309347a1685fSDinh Nguyen 
309447a1685fSDinh Nguyen 	dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
309595c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + GRXFSIZ),
309695c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + GNPTXFSIZ));
309747a1685fSDinh Nguyen 
30981f91b4ccSFelipe Balbi 	dwc2_hsotg_init_fifo(hsotg);
309947a1685fSDinh Nguyen 
310047a1685fSDinh Nguyen 	/* set the PLL on, remove the HNP/SRP and set the PHY */
3101fa4a8d72SMian Yousaf Kaukab 	trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
310295c8bc36SAntti Seppälä 	dwc2_writel(hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3103f889f23dSMian Yousaf Kaukab 		(trdtim << GUSBCFG_USBTRDTIM_SHIFT),
310447a1685fSDinh Nguyen 		hsotg->regs + GUSBCFG);
310547a1685fSDinh Nguyen 
3106f5090044SGregory Herrero 	if (using_dma(hsotg))
3107f5090044SGregory Herrero 		__orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
310847a1685fSDinh Nguyen }
310947a1685fSDinh Nguyen 
311047a1685fSDinh Nguyen /**
31111f91b4ccSFelipe Balbi  * dwc2_hsotg_udc_start - prepare the udc for work
311247a1685fSDinh Nguyen  * @gadget: The usb gadget state
311347a1685fSDinh Nguyen  * @driver: The usb gadget driver
311447a1685fSDinh Nguyen  *
311547a1685fSDinh Nguyen  * Perform initialization to prepare udc device and driver
311647a1685fSDinh Nguyen  * to work.
311747a1685fSDinh Nguyen  */
31181f91b4ccSFelipe Balbi static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
311947a1685fSDinh Nguyen 			   struct usb_gadget_driver *driver)
312047a1685fSDinh Nguyen {
3121941fcce4SDinh Nguyen 	struct dwc2_hsotg *hsotg = to_hsotg(gadget);
31225b9451f8SMarek Szyprowski 	unsigned long flags;
312347a1685fSDinh Nguyen 	int ret;
312447a1685fSDinh Nguyen 
312547a1685fSDinh Nguyen 	if (!hsotg) {
312647a1685fSDinh Nguyen 		pr_err("%s: called with no device\n", __func__);
312747a1685fSDinh Nguyen 		return -ENODEV;
312847a1685fSDinh Nguyen 	}
312947a1685fSDinh Nguyen 
313047a1685fSDinh Nguyen 	if (!driver) {
313147a1685fSDinh Nguyen 		dev_err(hsotg->dev, "%s: no driver\n", __func__);
313247a1685fSDinh Nguyen 		return -EINVAL;
313347a1685fSDinh Nguyen 	}
313447a1685fSDinh Nguyen 
313547a1685fSDinh Nguyen 	if (driver->max_speed < USB_SPEED_FULL)
313647a1685fSDinh Nguyen 		dev_err(hsotg->dev, "%s: bad speed\n", __func__);
313747a1685fSDinh Nguyen 
313847a1685fSDinh Nguyen 	if (!driver->setup) {
313947a1685fSDinh Nguyen 		dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
314047a1685fSDinh Nguyen 		return -EINVAL;
314147a1685fSDinh Nguyen 	}
314247a1685fSDinh Nguyen 
31437ad8096eSMarek Szyprowski 	mutex_lock(&hsotg->init_mutex);
314447a1685fSDinh Nguyen 	WARN_ON(hsotg->driver);
314547a1685fSDinh Nguyen 
314647a1685fSDinh Nguyen 	driver->driver.bus = NULL;
314747a1685fSDinh Nguyen 	hsotg->driver = driver;
314847a1685fSDinh Nguyen 	hsotg->gadget.dev.of_node = hsotg->dev->of_node;
314947a1685fSDinh Nguyen 	hsotg->gadget.speed = USB_SPEED_UNKNOWN;
315047a1685fSDinh Nguyen 
3151d00b4142SRobert Baldyga 	clk_enable(hsotg->clk);
3152d00b4142SRobert Baldyga 
315347a1685fSDinh Nguyen 	ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
315447a1685fSDinh Nguyen 				    hsotg->supplies);
315547a1685fSDinh Nguyen 	if (ret) {
315647a1685fSDinh Nguyen 		dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
315747a1685fSDinh Nguyen 		goto err;
315847a1685fSDinh Nguyen 	}
315947a1685fSDinh Nguyen 
31601f91b4ccSFelipe Balbi 	dwc2_hsotg_phy_enable(hsotg);
3161f6c01592SGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy))
3162f6c01592SGregory Herrero 		otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
3163c816c47fSMarek Szyprowski 
31645b9451f8SMarek Szyprowski 	spin_lock_irqsave(&hsotg->lock, flags);
31651f91b4ccSFelipe Balbi 	dwc2_hsotg_init(hsotg);
31661f91b4ccSFelipe Balbi 	dwc2_hsotg_core_init_disconnected(hsotg, false);
3167dc6e69e6SMarek Szyprowski 	hsotg->enabled = 0;
31685b9451f8SMarek Szyprowski 	spin_unlock_irqrestore(&hsotg->lock, flags);
31695b9451f8SMarek Szyprowski 
317047a1685fSDinh Nguyen 	dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
31715b9451f8SMarek Szyprowski 
31727ad8096eSMarek Szyprowski 	mutex_unlock(&hsotg->init_mutex);
31737ad8096eSMarek Szyprowski 
317447a1685fSDinh Nguyen 	return 0;
317547a1685fSDinh Nguyen 
317647a1685fSDinh Nguyen err:
31777ad8096eSMarek Szyprowski 	mutex_unlock(&hsotg->init_mutex);
317847a1685fSDinh Nguyen 	hsotg->driver = NULL;
317947a1685fSDinh Nguyen 	return ret;
318047a1685fSDinh Nguyen }
318147a1685fSDinh Nguyen 
318247a1685fSDinh Nguyen /**
31831f91b4ccSFelipe Balbi  * dwc2_hsotg_udc_stop - stop the udc
318447a1685fSDinh Nguyen  * @gadget: The usb gadget state
318547a1685fSDinh Nguyen  * @driver: The usb gadget driver
318647a1685fSDinh Nguyen  *
318747a1685fSDinh Nguyen  * Stop udc hw block and stay tunned for future transmissions
318847a1685fSDinh Nguyen  */
31891f91b4ccSFelipe Balbi static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
319047a1685fSDinh Nguyen {
3191941fcce4SDinh Nguyen 	struct dwc2_hsotg *hsotg = to_hsotg(gadget);
319247a1685fSDinh Nguyen 	unsigned long flags = 0;
319347a1685fSDinh Nguyen 	int ep;
319447a1685fSDinh Nguyen 
319547a1685fSDinh Nguyen 	if (!hsotg)
319647a1685fSDinh Nguyen 		return -ENODEV;
319747a1685fSDinh Nguyen 
31987ad8096eSMarek Szyprowski 	mutex_lock(&hsotg->init_mutex);
31997ad8096eSMarek Szyprowski 
320047a1685fSDinh Nguyen 	/* all endpoints should be shutdown */
3201c6f5c050SMian Yousaf Kaukab 	for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3202c6f5c050SMian Yousaf Kaukab 		if (hsotg->eps_in[ep])
32031f91b4ccSFelipe Balbi 			dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
3204c6f5c050SMian Yousaf Kaukab 		if (hsotg->eps_out[ep])
32051f91b4ccSFelipe Balbi 			dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
3206c6f5c050SMian Yousaf Kaukab 	}
320747a1685fSDinh Nguyen 
320847a1685fSDinh Nguyen 	spin_lock_irqsave(&hsotg->lock, flags);
320947a1685fSDinh Nguyen 
321047a1685fSDinh Nguyen 	hsotg->driver = NULL;
321147a1685fSDinh Nguyen 	hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3212dc6e69e6SMarek Szyprowski 	hsotg->enabled = 0;
321347a1685fSDinh Nguyen 
321447a1685fSDinh Nguyen 	spin_unlock_irqrestore(&hsotg->lock, flags);
321547a1685fSDinh Nguyen 
3216f6c01592SGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy))
3217f6c01592SGregory Herrero 		otg_set_peripheral(hsotg->uphy->otg, NULL);
32181f91b4ccSFelipe Balbi 	dwc2_hsotg_phy_disable(hsotg);
3219c816c47fSMarek Szyprowski 
322047a1685fSDinh Nguyen 	regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
322147a1685fSDinh Nguyen 
3222d00b4142SRobert Baldyga 	clk_disable(hsotg->clk);
3223d00b4142SRobert Baldyga 
32247ad8096eSMarek Szyprowski 	mutex_unlock(&hsotg->init_mutex);
32257ad8096eSMarek Szyprowski 
322647a1685fSDinh Nguyen 	return 0;
322747a1685fSDinh Nguyen }
322847a1685fSDinh Nguyen 
322947a1685fSDinh Nguyen /**
32301f91b4ccSFelipe Balbi  * dwc2_hsotg_gadget_getframe - read the frame number
323147a1685fSDinh Nguyen  * @gadget: The usb gadget state
323247a1685fSDinh Nguyen  *
323347a1685fSDinh Nguyen  * Read the {micro} frame number
323447a1685fSDinh Nguyen  */
32351f91b4ccSFelipe Balbi static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
323647a1685fSDinh Nguyen {
32371f91b4ccSFelipe Balbi 	return dwc2_hsotg_read_frameno(to_hsotg(gadget));
323847a1685fSDinh Nguyen }
323947a1685fSDinh Nguyen 
324047a1685fSDinh Nguyen /**
32411f91b4ccSFelipe Balbi  * dwc2_hsotg_pullup - connect/disconnect the USB PHY
324247a1685fSDinh Nguyen  * @gadget: The usb gadget state
324347a1685fSDinh Nguyen  * @is_on: Current state of the USB PHY
324447a1685fSDinh Nguyen  *
324547a1685fSDinh Nguyen  * Connect/Disconnect the USB PHY pullup
324647a1685fSDinh Nguyen  */
32471f91b4ccSFelipe Balbi static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
324847a1685fSDinh Nguyen {
3249941fcce4SDinh Nguyen 	struct dwc2_hsotg *hsotg = to_hsotg(gadget);
325047a1685fSDinh Nguyen 	unsigned long flags = 0;
325147a1685fSDinh Nguyen 
325277ba9119SGregory Herrero 	dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
325377ba9119SGregory Herrero 			hsotg->op_state);
325477ba9119SGregory Herrero 
325577ba9119SGregory Herrero 	/* Don't modify pullup state while in host mode */
325677ba9119SGregory Herrero 	if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
325777ba9119SGregory Herrero 		hsotg->enabled = is_on;
325877ba9119SGregory Herrero 		return 0;
325977ba9119SGregory Herrero 	}
326047a1685fSDinh Nguyen 
32617ad8096eSMarek Szyprowski 	mutex_lock(&hsotg->init_mutex);
326247a1685fSDinh Nguyen 	spin_lock_irqsave(&hsotg->lock, flags);
326347a1685fSDinh Nguyen 	if (is_on) {
3264d00b4142SRobert Baldyga 		clk_enable(hsotg->clk);
3265dc6e69e6SMarek Szyprowski 		hsotg->enabled = 1;
32661f91b4ccSFelipe Balbi 		dwc2_hsotg_core_init_disconnected(hsotg, false);
32671f91b4ccSFelipe Balbi 		dwc2_hsotg_core_connect(hsotg);
326847a1685fSDinh Nguyen 	} else {
32691f91b4ccSFelipe Balbi 		dwc2_hsotg_core_disconnect(hsotg);
32701f91b4ccSFelipe Balbi 		dwc2_hsotg_disconnect(hsotg);
3271dc6e69e6SMarek Szyprowski 		hsotg->enabled = 0;
3272d00b4142SRobert Baldyga 		clk_disable(hsotg->clk);
327347a1685fSDinh Nguyen 	}
327447a1685fSDinh Nguyen 
327547a1685fSDinh Nguyen 	hsotg->gadget.speed = USB_SPEED_UNKNOWN;
327647a1685fSDinh Nguyen 	spin_unlock_irqrestore(&hsotg->lock, flags);
32777ad8096eSMarek Szyprowski 	mutex_unlock(&hsotg->init_mutex);
327847a1685fSDinh Nguyen 
327947a1685fSDinh Nguyen 	return 0;
328047a1685fSDinh Nguyen }
328147a1685fSDinh Nguyen 
32821f91b4ccSFelipe Balbi static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
328383d98223SGregory Herrero {
328483d98223SGregory Herrero 	struct dwc2_hsotg *hsotg = to_hsotg(gadget);
328583d98223SGregory Herrero 	unsigned long flags;
328683d98223SGregory Herrero 
328783d98223SGregory Herrero 	dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
328883d98223SGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
328983d98223SGregory Herrero 
329083d98223SGregory Herrero 	if (is_active) {
3291cd0e641cSGregory Herrero 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
329218b2b37cSGregory Herrero 		/*
329318b2b37cSGregory Herrero 		 * If controller is hibernated, it must exit from hibernation
329418b2b37cSGregory Herrero 		 * before being initialized
329518b2b37cSGregory Herrero 		 */
3296065d3931SGregory Herrero 		if (hsotg->lx_state == DWC2_L2)
329718b2b37cSGregory Herrero 			dwc2_exit_hibernation(hsotg, false);
3298065d3931SGregory Herrero 
329983d98223SGregory Herrero 		/* Kill any ep0 requests as controller will be reinitialized */
330083d98223SGregory Herrero 		kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
33011f91b4ccSFelipe Balbi 		dwc2_hsotg_core_init_disconnected(hsotg, false);
330283d98223SGregory Herrero 		if (hsotg->enabled)
33031f91b4ccSFelipe Balbi 			dwc2_hsotg_core_connect(hsotg);
330483d98223SGregory Herrero 	} else {
33051f91b4ccSFelipe Balbi 		dwc2_hsotg_core_disconnect(hsotg);
33061f91b4ccSFelipe Balbi 		dwc2_hsotg_disconnect(hsotg);
330783d98223SGregory Herrero 	}
330883d98223SGregory Herrero 
330983d98223SGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
331083d98223SGregory Herrero 	return 0;
331183d98223SGregory Herrero }
331283d98223SGregory Herrero 
3313596d696aSGregory Herrero /**
33141f91b4ccSFelipe Balbi  * dwc2_hsotg_vbus_draw - report bMaxPower field
3315596d696aSGregory Herrero  * @gadget: The usb gadget state
3316596d696aSGregory Herrero  * @mA: Amount of current
3317596d696aSGregory Herrero  *
3318596d696aSGregory Herrero  * Report how much power the device may consume to the phy.
3319596d696aSGregory Herrero  */
33201f91b4ccSFelipe Balbi static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA)
3321596d696aSGregory Herrero {
3322596d696aSGregory Herrero 	struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3323596d696aSGregory Herrero 
3324596d696aSGregory Herrero 	if (IS_ERR_OR_NULL(hsotg->uphy))
3325596d696aSGregory Herrero 		return -ENOTSUPP;
3326596d696aSGregory Herrero 	return usb_phy_set_power(hsotg->uphy, mA);
3327596d696aSGregory Herrero }
3328596d696aSGregory Herrero 
33291f91b4ccSFelipe Balbi static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
33301f91b4ccSFelipe Balbi 	.get_frame	= dwc2_hsotg_gadget_getframe,
33311f91b4ccSFelipe Balbi 	.udc_start		= dwc2_hsotg_udc_start,
33321f91b4ccSFelipe Balbi 	.udc_stop		= dwc2_hsotg_udc_stop,
33331f91b4ccSFelipe Balbi 	.pullup                 = dwc2_hsotg_pullup,
33341f91b4ccSFelipe Balbi 	.vbus_session		= dwc2_hsotg_vbus_session,
33351f91b4ccSFelipe Balbi 	.vbus_draw		= dwc2_hsotg_vbus_draw,
333647a1685fSDinh Nguyen };
333747a1685fSDinh Nguyen 
333847a1685fSDinh Nguyen /**
33391f91b4ccSFelipe Balbi  * dwc2_hsotg_initep - initialise a single endpoint
334047a1685fSDinh Nguyen  * @hsotg: The device state.
334147a1685fSDinh Nguyen  * @hs_ep: The endpoint to be initialised.
334247a1685fSDinh Nguyen  * @epnum: The endpoint number
334347a1685fSDinh Nguyen  *
334447a1685fSDinh Nguyen  * Initialise the given endpoint (as part of the probe and device state
334547a1685fSDinh Nguyen  * creation) to give to the gadget driver. Setup the endpoint name, any
334647a1685fSDinh Nguyen  * direction information and other state that may be required.
334747a1685fSDinh Nguyen  */
33481f91b4ccSFelipe Balbi static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
33491f91b4ccSFelipe Balbi 				       struct dwc2_hsotg_ep *hs_ep,
3350c6f5c050SMian Yousaf Kaukab 				       int epnum,
3351c6f5c050SMian Yousaf Kaukab 				       bool dir_in)
335247a1685fSDinh Nguyen {
335347a1685fSDinh Nguyen 	char *dir;
335447a1685fSDinh Nguyen 
335547a1685fSDinh Nguyen 	if (epnum == 0)
335647a1685fSDinh Nguyen 		dir = "";
3357c6f5c050SMian Yousaf Kaukab 	else if (dir_in)
335847a1685fSDinh Nguyen 		dir = "in";
3359c6f5c050SMian Yousaf Kaukab 	else
3360c6f5c050SMian Yousaf Kaukab 		dir = "out";
336147a1685fSDinh Nguyen 
3362c6f5c050SMian Yousaf Kaukab 	hs_ep->dir_in = dir_in;
336347a1685fSDinh Nguyen 	hs_ep->index = epnum;
336447a1685fSDinh Nguyen 
336547a1685fSDinh Nguyen 	snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
336647a1685fSDinh Nguyen 
336747a1685fSDinh Nguyen 	INIT_LIST_HEAD(&hs_ep->queue);
336847a1685fSDinh Nguyen 	INIT_LIST_HEAD(&hs_ep->ep.ep_list);
336947a1685fSDinh Nguyen 
337047a1685fSDinh Nguyen 	/* add to the list of endpoints known by the gadget driver */
337147a1685fSDinh Nguyen 	if (epnum)
337247a1685fSDinh Nguyen 		list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
337347a1685fSDinh Nguyen 
337447a1685fSDinh Nguyen 	hs_ep->parent = hsotg;
337547a1685fSDinh Nguyen 	hs_ep->ep.name = hs_ep->name;
337647a1685fSDinh Nguyen 	usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
33771f91b4ccSFelipe Balbi 	hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
337847a1685fSDinh Nguyen 
33792954522fSRobert Baldyga 	if (epnum == 0) {
33802954522fSRobert Baldyga 		hs_ep->ep.caps.type_control = true;
33812954522fSRobert Baldyga 	} else {
33822954522fSRobert Baldyga 		hs_ep->ep.caps.type_iso = true;
33832954522fSRobert Baldyga 		hs_ep->ep.caps.type_bulk = true;
33842954522fSRobert Baldyga 		hs_ep->ep.caps.type_int = true;
33852954522fSRobert Baldyga 	}
33862954522fSRobert Baldyga 
33872954522fSRobert Baldyga 	if (dir_in)
33882954522fSRobert Baldyga 		hs_ep->ep.caps.dir_in = true;
33892954522fSRobert Baldyga 	else
33902954522fSRobert Baldyga 		hs_ep->ep.caps.dir_out = true;
33912954522fSRobert Baldyga 
339247a1685fSDinh Nguyen 	/*
339347a1685fSDinh Nguyen 	 * if we're using dma, we need to set the next-endpoint pointer
339447a1685fSDinh Nguyen 	 * to be something valid.
339547a1685fSDinh Nguyen 	 */
339647a1685fSDinh Nguyen 
339747a1685fSDinh Nguyen 	if (using_dma(hsotg)) {
339847a1685fSDinh Nguyen 		u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
3399c6f5c050SMian Yousaf Kaukab 		if (dir_in)
340095c8bc36SAntti Seppälä 			dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
3401c6f5c050SMian Yousaf Kaukab 		else
340295c8bc36SAntti Seppälä 			dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
340347a1685fSDinh Nguyen 	}
340447a1685fSDinh Nguyen }
340547a1685fSDinh Nguyen 
340647a1685fSDinh Nguyen /**
34071f91b4ccSFelipe Balbi  * dwc2_hsotg_hw_cfg - read HW configuration registers
340847a1685fSDinh Nguyen  * @param: The device state
340947a1685fSDinh Nguyen  *
341047a1685fSDinh Nguyen  * Read the USB core HW configuration registers
341147a1685fSDinh Nguyen  */
34121f91b4ccSFelipe Balbi static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
341347a1685fSDinh Nguyen {
3414c6f5c050SMian Yousaf Kaukab 	u32 cfg;
3415c6f5c050SMian Yousaf Kaukab 	u32 ep_type;
3416c6f5c050SMian Yousaf Kaukab 	u32 i;
3417c6f5c050SMian Yousaf Kaukab 
341847a1685fSDinh Nguyen 	/* check hardware configuration */
341947a1685fSDinh Nguyen 
342095c8bc36SAntti Seppälä 	cfg = dwc2_readl(hsotg->regs + GHWCFG2);
3421f889f23dSMian Yousaf Kaukab 	hsotg->num_of_eps = (cfg >> GHWCFG2_NUM_DEV_EP_SHIFT) & 0xF;
3422c6f5c050SMian Yousaf Kaukab 	/* Add ep0 */
3423c6f5c050SMian Yousaf Kaukab 	hsotg->num_of_eps++;
342447a1685fSDinh Nguyen 
34251f91b4ccSFelipe Balbi 	hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct dwc2_hsotg_ep),
3426c6f5c050SMian Yousaf Kaukab 								GFP_KERNEL);
3427c6f5c050SMian Yousaf Kaukab 	if (!hsotg->eps_in[0])
3428c6f5c050SMian Yousaf Kaukab 		return -ENOMEM;
34291f91b4ccSFelipe Balbi 	/* Same dwc2_hsotg_ep is used in both directions for ep0 */
3430c6f5c050SMian Yousaf Kaukab 	hsotg->eps_out[0] = hsotg->eps_in[0];
343147a1685fSDinh Nguyen 
343295c8bc36SAntti Seppälä 	cfg = dwc2_readl(hsotg->regs + GHWCFG1);
3433251a17f5SRoshan Pius 	for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
3434c6f5c050SMian Yousaf Kaukab 		ep_type = cfg & 3;
3435c6f5c050SMian Yousaf Kaukab 		/* Direction in or both */
3436c6f5c050SMian Yousaf Kaukab 		if (!(ep_type & 2)) {
3437c6f5c050SMian Yousaf Kaukab 			hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
34381f91b4ccSFelipe Balbi 				sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
3439c6f5c050SMian Yousaf Kaukab 			if (!hsotg->eps_in[i])
3440c6f5c050SMian Yousaf Kaukab 				return -ENOMEM;
3441c6f5c050SMian Yousaf Kaukab 		}
3442c6f5c050SMian Yousaf Kaukab 		/* Direction out or both */
3443c6f5c050SMian Yousaf Kaukab 		if (!(ep_type & 1)) {
3444c6f5c050SMian Yousaf Kaukab 			hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
34451f91b4ccSFelipe Balbi 				sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
3446c6f5c050SMian Yousaf Kaukab 			if (!hsotg->eps_out[i])
3447c6f5c050SMian Yousaf Kaukab 				return -ENOMEM;
3448c6f5c050SMian Yousaf Kaukab 		}
3449c6f5c050SMian Yousaf Kaukab 	}
3450c6f5c050SMian Yousaf Kaukab 
345195c8bc36SAntti Seppälä 	cfg = dwc2_readl(hsotg->regs + GHWCFG3);
3452f889f23dSMian Yousaf Kaukab 	hsotg->fifo_mem = (cfg >> GHWCFG3_DFIFO_DEPTH_SHIFT);
3453c6f5c050SMian Yousaf Kaukab 
345495c8bc36SAntti Seppälä 	cfg = dwc2_readl(hsotg->regs + GHWCFG4);
3455f889f23dSMian Yousaf Kaukab 	hsotg->dedicated_fifos = (cfg >> GHWCFG4_DED_FIFO_SHIFT) & 1;
345647a1685fSDinh Nguyen 
3457cff9eb75SMarek Szyprowski 	dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
3458cff9eb75SMarek Szyprowski 		 hsotg->num_of_eps,
3459cff9eb75SMarek Szyprowski 		 hsotg->dedicated_fifos ? "dedicated" : "shared",
3460cff9eb75SMarek Szyprowski 		 hsotg->fifo_mem);
3461c6f5c050SMian Yousaf Kaukab 	return 0;
346247a1685fSDinh Nguyen }
346347a1685fSDinh Nguyen 
346447a1685fSDinh Nguyen /**
34651f91b4ccSFelipe Balbi  * dwc2_hsotg_dump - dump state of the udc
346647a1685fSDinh Nguyen  * @param: The device state
346747a1685fSDinh Nguyen  */
34681f91b4ccSFelipe Balbi static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
346947a1685fSDinh Nguyen {
347047a1685fSDinh Nguyen #ifdef DEBUG
347147a1685fSDinh Nguyen 	struct device *dev = hsotg->dev;
347247a1685fSDinh Nguyen 	void __iomem *regs = hsotg->regs;
347347a1685fSDinh Nguyen 	u32 val;
347447a1685fSDinh Nguyen 	int idx;
347547a1685fSDinh Nguyen 
347647a1685fSDinh Nguyen 	dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
347795c8bc36SAntti Seppälä 		 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
347895c8bc36SAntti Seppälä 		 dwc2_readl(regs + DIEPMSK));
347947a1685fSDinh Nguyen 
3480f889f23dSMian Yousaf Kaukab 	dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
348195c8bc36SAntti Seppälä 		 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
348247a1685fSDinh Nguyen 
348347a1685fSDinh Nguyen 	dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
348495c8bc36SAntti Seppälä 		 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
348547a1685fSDinh Nguyen 
348647a1685fSDinh Nguyen 	/* show periodic fifo settings */
348747a1685fSDinh Nguyen 
3488364f8e93SMian Yousaf Kaukab 	for (idx = 1; idx < hsotg->num_of_eps; idx++) {
348995c8bc36SAntti Seppälä 		val = dwc2_readl(regs + DPTXFSIZN(idx));
349047a1685fSDinh Nguyen 		dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
349147a1685fSDinh Nguyen 			 val >> FIFOSIZE_DEPTH_SHIFT,
349247a1685fSDinh Nguyen 			 val & FIFOSIZE_STARTADDR_MASK);
349347a1685fSDinh Nguyen 	}
349447a1685fSDinh Nguyen 
3495364f8e93SMian Yousaf Kaukab 	for (idx = 0; idx < hsotg->num_of_eps; idx++) {
349647a1685fSDinh Nguyen 		dev_info(dev,
349747a1685fSDinh Nguyen 			 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
349895c8bc36SAntti Seppälä 			 dwc2_readl(regs + DIEPCTL(idx)),
349995c8bc36SAntti Seppälä 			 dwc2_readl(regs + DIEPTSIZ(idx)),
350095c8bc36SAntti Seppälä 			 dwc2_readl(regs + DIEPDMA(idx)));
350147a1685fSDinh Nguyen 
350295c8bc36SAntti Seppälä 		val = dwc2_readl(regs + DOEPCTL(idx));
350347a1685fSDinh Nguyen 		dev_info(dev,
350447a1685fSDinh Nguyen 			 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
350595c8bc36SAntti Seppälä 			 idx, dwc2_readl(regs + DOEPCTL(idx)),
350695c8bc36SAntti Seppälä 			 dwc2_readl(regs + DOEPTSIZ(idx)),
350795c8bc36SAntti Seppälä 			 dwc2_readl(regs + DOEPDMA(idx)));
350847a1685fSDinh Nguyen 
350947a1685fSDinh Nguyen 	}
351047a1685fSDinh Nguyen 
351147a1685fSDinh Nguyen 	dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
351295c8bc36SAntti Seppälä 		 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
351347a1685fSDinh Nguyen #endif
351447a1685fSDinh Nguyen }
351547a1685fSDinh Nguyen 
3516edd74be8SGregory Herrero #ifdef CONFIG_OF
35171f91b4ccSFelipe Balbi static void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg)
3518edd74be8SGregory Herrero {
3519edd74be8SGregory Herrero 	struct device_node *np = hsotg->dev->of_node;
35200a176279SGregory Herrero 	u32 len = 0;
35210a176279SGregory Herrero 	u32 i = 0;
3522edd74be8SGregory Herrero 
3523edd74be8SGregory Herrero 	/* Enable dma if requested in device tree */
3524edd74be8SGregory Herrero 	hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma");
35250a176279SGregory Herrero 
35260a176279SGregory Herrero 	/*
35270a176279SGregory Herrero 	* Register TX periodic fifo size per endpoint.
35280a176279SGregory Herrero 	* EP0 is excluded since it has no fifo configuration.
35290a176279SGregory Herrero 	*/
35300a176279SGregory Herrero 	if (!of_find_property(np, "g-tx-fifo-size", &len))
35310a176279SGregory Herrero 		goto rx_fifo;
35320a176279SGregory Herrero 
35330a176279SGregory Herrero 	len /= sizeof(u32);
35340a176279SGregory Herrero 
35350a176279SGregory Herrero 	/* Read tx fifo sizes other than ep0 */
35360a176279SGregory Herrero 	if (of_property_read_u32_array(np, "g-tx-fifo-size",
35370a176279SGregory Herrero 						&hsotg->g_tx_fifo_sz[1], len))
35380a176279SGregory Herrero 		goto rx_fifo;
35390a176279SGregory Herrero 
35400a176279SGregory Herrero 	/* Add ep0 */
35410a176279SGregory Herrero 	len++;
35420a176279SGregory Herrero 
35430a176279SGregory Herrero 	/* Make remaining TX fifos unavailable */
35440a176279SGregory Herrero 	if (len < MAX_EPS_CHANNELS) {
35450a176279SGregory Herrero 		for (i = len; i < MAX_EPS_CHANNELS; i++)
35460a176279SGregory Herrero 			hsotg->g_tx_fifo_sz[i] = 0;
35470a176279SGregory Herrero 	}
35480a176279SGregory Herrero 
35490a176279SGregory Herrero rx_fifo:
35500a176279SGregory Herrero 	/* Register RX fifo size */
35510a176279SGregory Herrero 	of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz);
35520a176279SGregory Herrero 
35530a176279SGregory Herrero 	/* Register NPTX fifo size */
35540a176279SGregory Herrero 	of_property_read_u32(np, "g-np-tx-fifo-size",
35550a176279SGregory Herrero 						&hsotg->g_np_g_tx_fifo_sz);
3556edd74be8SGregory Herrero }
3557edd74be8SGregory Herrero #else
35581f91b4ccSFelipe Balbi static inline void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg) { }
3559edd74be8SGregory Herrero #endif
3560edd74be8SGregory Herrero 
356147a1685fSDinh Nguyen /**
3562117777b2SDinh Nguyen  * dwc2_gadget_init - init function for gadget
3563117777b2SDinh Nguyen  * @dwc2: The data structure for the DWC2 driver.
3564117777b2SDinh Nguyen  * @irq: The IRQ number for the controller.
356547a1685fSDinh Nguyen  */
3566117777b2SDinh Nguyen int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
356747a1685fSDinh Nguyen {
3568117777b2SDinh Nguyen 	struct device *dev = hsotg->dev;
35691f91b4ccSFelipe Balbi 	struct dwc2_hsotg_plat *plat = dev->platform_data;
357047a1685fSDinh Nguyen 	int epnum;
357147a1685fSDinh Nguyen 	int ret;
357247a1685fSDinh Nguyen 	int i;
35730a176279SGregory Herrero 	u32 p_tx_fifo[] = DWC2_G_P_LEGACY_TX_FIFO_SIZE;
357447a1685fSDinh Nguyen 
35751b59fc7eSKamil Debski 	/* Set default UTMI width */
35761b59fc7eSKamil Debski 	hsotg->phyif = GUSBCFG_PHYIF16;
35771b59fc7eSKamil Debski 
35781f91b4ccSFelipe Balbi 	dwc2_hsotg_of_probe(hsotg);
3579edd74be8SGregory Herrero 
35800a176279SGregory Herrero 	/* Initialize to legacy fifo configuration values */
35810a176279SGregory Herrero 	hsotg->g_rx_fifo_sz = 2048;
35820a176279SGregory Herrero 	hsotg->g_np_g_tx_fifo_sz = 1024;
35830a176279SGregory Herrero 	memcpy(&hsotg->g_tx_fifo_sz[1], p_tx_fifo, sizeof(p_tx_fifo));
35840a176279SGregory Herrero 	/* Device tree specific probe */
35851f91b4ccSFelipe Balbi 	dwc2_hsotg_of_probe(hsotg);
35860a176279SGregory Herrero 	/* Dump fifo information */
35870a176279SGregory Herrero 	dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
35880a176279SGregory Herrero 						hsotg->g_np_g_tx_fifo_sz);
35890a176279SGregory Herrero 	dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz);
35900a176279SGregory Herrero 	for (i = 0; i < MAX_EPS_CHANNELS; i++)
35910a176279SGregory Herrero 		dev_dbg(dev, "Periodic TXFIFO%2d size: %d\n", i,
35920a176279SGregory Herrero 						hsotg->g_tx_fifo_sz[i]);
359347a1685fSDinh Nguyen 	/*
3594135b3c43SYunzhi Li 	 * If platform probe couldn't find a generic PHY or an old style
3595135b3c43SYunzhi Li 	 * USB PHY, fall back to pdata
359647a1685fSDinh Nguyen 	 */
3597135b3c43SYunzhi Li 	if (IS_ERR_OR_NULL(hsotg->phy) && IS_ERR_OR_NULL(hsotg->uphy)) {
3598117777b2SDinh Nguyen 		plat = dev_get_platdata(dev);
359947a1685fSDinh Nguyen 		if (!plat) {
3600117777b2SDinh Nguyen 			dev_err(dev,
360147a1685fSDinh Nguyen 			"no platform data or transceiver defined\n");
360247a1685fSDinh Nguyen 			return -EPROBE_DEFER;
360347a1685fSDinh Nguyen 		}
360447a1685fSDinh Nguyen 		hsotg->plat = plat;
3605135b3c43SYunzhi Li 	} else if (hsotg->phy) {
36061b59fc7eSKamil Debski 		/*
36071b59fc7eSKamil Debski 		 * If using the generic PHY framework, check if the PHY bus
36081b59fc7eSKamil Debski 		 * width is 8-bit and set the phyif appropriately.
36091b59fc7eSKamil Debski 		 */
3610135b3c43SYunzhi Li 		if (phy_get_bus_width(hsotg->phy) == 8)
36111b59fc7eSKamil Debski 			hsotg->phyif = GUSBCFG_PHYIF8;
36121b59fc7eSKamil Debski 	}
361347a1685fSDinh Nguyen 
3614117777b2SDinh Nguyen 	hsotg->clk = devm_clk_get(dev, "otg");
361547a1685fSDinh Nguyen 	if (IS_ERR(hsotg->clk)) {
36168d736d8aSDinh Nguyen 		hsotg->clk = NULL;
3617f415fbd1SDinh Nguyen 		dev_dbg(dev, "cannot get otg clock\n");
361847a1685fSDinh Nguyen 	}
361947a1685fSDinh Nguyen 
362047a1685fSDinh Nguyen 	hsotg->gadget.max_speed = USB_SPEED_HIGH;
36211f91b4ccSFelipe Balbi 	hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
362247a1685fSDinh Nguyen 	hsotg->gadget.name = dev_name(dev);
3623097ee662SGregory Herrero 	if (hsotg->dr_mode == USB_DR_MODE_OTG)
3624097ee662SGregory Herrero 		hsotg->gadget.is_otg = 1;
3625ec4cc657SMian Yousaf Kaukab 	else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
3626ec4cc657SMian Yousaf Kaukab 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
362747a1685fSDinh Nguyen 
362847a1685fSDinh Nguyen 	/* reset the system */
362947a1685fSDinh Nguyen 
3630f415fbd1SDinh Nguyen 	ret = clk_prepare_enable(hsotg->clk);
3631f415fbd1SDinh Nguyen 	if (ret) {
3632f415fbd1SDinh Nguyen 		dev_err(dev, "failed to enable otg clk\n");
3633f415fbd1SDinh Nguyen 		goto err_clk;
3634f415fbd1SDinh Nguyen 	}
3635f415fbd1SDinh Nguyen 
363647a1685fSDinh Nguyen 
363747a1685fSDinh Nguyen 	/* regulators */
363847a1685fSDinh Nguyen 
363947a1685fSDinh Nguyen 	for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
36401f91b4ccSFelipe Balbi 		hsotg->supplies[i].supply = dwc2_hsotg_supply_names[i];
364147a1685fSDinh Nguyen 
364247a1685fSDinh Nguyen 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
364347a1685fSDinh Nguyen 				 hsotg->supplies);
364447a1685fSDinh Nguyen 	if (ret) {
364547a1685fSDinh Nguyen 		dev_err(dev, "failed to request supplies: %d\n", ret);
364647a1685fSDinh Nguyen 		goto err_clk;
364747a1685fSDinh Nguyen 	}
364847a1685fSDinh Nguyen 
364947a1685fSDinh Nguyen 	ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
365047a1685fSDinh Nguyen 				    hsotg->supplies);
365147a1685fSDinh Nguyen 
365247a1685fSDinh Nguyen 	if (ret) {
3653941fcce4SDinh Nguyen 		dev_err(dev, "failed to enable supplies: %d\n", ret);
3654c139ec27SMian Yousaf Kaukab 		goto err_clk;
365547a1685fSDinh Nguyen 	}
365647a1685fSDinh Nguyen 
365747a1685fSDinh Nguyen 	/* usb phy enable */
36581f91b4ccSFelipe Balbi 	dwc2_hsotg_phy_enable(hsotg);
365947a1685fSDinh Nguyen 
36601b7a66b4SGregory Herrero 	/*
36611b7a66b4SGregory Herrero 	 * Force Device mode before initialization.
36621b7a66b4SGregory Herrero 	 * This allows correctly configuring fifo for device mode.
36631b7a66b4SGregory Herrero 	 */
36641b7a66b4SGregory Herrero 	__bic32(hsotg->regs + GUSBCFG, GUSBCFG_FORCEHOSTMODE);
36651b7a66b4SGregory Herrero 	__orr32(hsotg->regs + GUSBCFG, GUSBCFG_FORCEDEVMODE);
36661b7a66b4SGregory Herrero 
36671b7a66b4SGregory Herrero 	/*
36681b7a66b4SGregory Herrero 	 * According to Synopsys databook, this sleep is needed for the force
36691b7a66b4SGregory Herrero 	 * device mode to take effect.
36701b7a66b4SGregory Herrero 	 */
36711b7a66b4SGregory Herrero 	msleep(25);
36721b7a66b4SGregory Herrero 
36731f91b4ccSFelipe Balbi 	dwc2_hsotg_corereset(hsotg);
36741f91b4ccSFelipe Balbi 	ret = dwc2_hsotg_hw_cfg(hsotg);
3675c6f5c050SMian Yousaf Kaukab 	if (ret) {
3676c6f5c050SMian Yousaf Kaukab 		dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
3677c6f5c050SMian Yousaf Kaukab 		goto err_clk;
3678c6f5c050SMian Yousaf Kaukab 	}
3679c6f5c050SMian Yousaf Kaukab 
36801f91b4ccSFelipe Balbi 	dwc2_hsotg_init(hsotg);
368147a1685fSDinh Nguyen 
36821b7a66b4SGregory Herrero 	/* Switch back to default configuration */
36831b7a66b4SGregory Herrero 	__bic32(hsotg->regs + GUSBCFG, GUSBCFG_FORCEDEVMODE);
36841b7a66b4SGregory Herrero 
36853f95001dSMian Yousaf Kaukab 	hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
36863f95001dSMian Yousaf Kaukab 			DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
36873f95001dSMian Yousaf Kaukab 	if (!hsotg->ctrl_buff) {
36883f95001dSMian Yousaf Kaukab 		dev_err(dev, "failed to allocate ctrl request buff\n");
36893f95001dSMian Yousaf Kaukab 		ret = -ENOMEM;
36903f95001dSMian Yousaf Kaukab 		goto err_supplies;
36913f95001dSMian Yousaf Kaukab 	}
36923f95001dSMian Yousaf Kaukab 
36933f95001dSMian Yousaf Kaukab 	hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
36943f95001dSMian Yousaf Kaukab 			DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
36953f95001dSMian Yousaf Kaukab 	if (!hsotg->ep0_buff) {
36963f95001dSMian Yousaf Kaukab 		dev_err(dev, "failed to allocate ctrl reply buff\n");
36973f95001dSMian Yousaf Kaukab 		ret = -ENOMEM;
36983f95001dSMian Yousaf Kaukab 		goto err_supplies;
36993f95001dSMian Yousaf Kaukab 	}
37003f95001dSMian Yousaf Kaukab 
37011f91b4ccSFelipe Balbi 	ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
3702db8178c3SDinh Nguyen 				dev_name(hsotg->dev), hsotg);
3703eb3c56c5SMarek Szyprowski 	if (ret < 0) {
37041f91b4ccSFelipe Balbi 		dwc2_hsotg_phy_disable(hsotg);
3705eb3c56c5SMarek Szyprowski 		clk_disable_unprepare(hsotg->clk);
3706eb3c56c5SMarek Szyprowski 		regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3707eb3c56c5SMarek Szyprowski 				       hsotg->supplies);
3708db8178c3SDinh Nguyen 		dev_err(dev, "cannot claim IRQ for gadget\n");
3709c139ec27SMian Yousaf Kaukab 		goto err_supplies;
3710eb3c56c5SMarek Szyprowski 	}
3711eb3c56c5SMarek Szyprowski 
371247a1685fSDinh Nguyen 	/* hsotg->num_of_eps holds number of EPs other than ep0 */
371347a1685fSDinh Nguyen 
371447a1685fSDinh Nguyen 	if (hsotg->num_of_eps == 0) {
371547a1685fSDinh Nguyen 		dev_err(dev, "wrong number of EPs (zero)\n");
371647a1685fSDinh Nguyen 		ret = -EINVAL;
371747a1685fSDinh Nguyen 		goto err_supplies;
371847a1685fSDinh Nguyen 	}
371947a1685fSDinh Nguyen 
372047a1685fSDinh Nguyen 	/* setup endpoint information */
372147a1685fSDinh Nguyen 
372247a1685fSDinh Nguyen 	INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3723c6f5c050SMian Yousaf Kaukab 	hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
372447a1685fSDinh Nguyen 
372547a1685fSDinh Nguyen 	/* allocate EP0 request */
372647a1685fSDinh Nguyen 
37271f91b4ccSFelipe Balbi 	hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
372847a1685fSDinh Nguyen 						     GFP_KERNEL);
372947a1685fSDinh Nguyen 	if (!hsotg->ctrl_req) {
373047a1685fSDinh Nguyen 		dev_err(dev, "failed to allocate ctrl req\n");
373147a1685fSDinh Nguyen 		ret = -ENOMEM;
3732c6f5c050SMian Yousaf Kaukab 		goto err_supplies;
373347a1685fSDinh Nguyen 	}
373447a1685fSDinh Nguyen 
373547a1685fSDinh Nguyen 	/* initialise the endpoints now the core has been initialised */
3736c6f5c050SMian Yousaf Kaukab 	for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
3737c6f5c050SMian Yousaf Kaukab 		if (hsotg->eps_in[epnum])
37381f91b4ccSFelipe Balbi 			dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
3739c6f5c050SMian Yousaf Kaukab 								epnum, 1);
3740c6f5c050SMian Yousaf Kaukab 		if (hsotg->eps_out[epnum])
37411f91b4ccSFelipe Balbi 			dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
3742c6f5c050SMian Yousaf Kaukab 								epnum, 0);
3743c6f5c050SMian Yousaf Kaukab 	}
374447a1685fSDinh Nguyen 
374547a1685fSDinh Nguyen 	/* disable power and clock */
37461f91b4ccSFelipe Balbi 	dwc2_hsotg_phy_disable(hsotg);
374747a1685fSDinh Nguyen 
374847a1685fSDinh Nguyen 	ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
374947a1685fSDinh Nguyen 				    hsotg->supplies);
375047a1685fSDinh Nguyen 	if (ret) {
3751117777b2SDinh Nguyen 		dev_err(dev, "failed to disable supplies: %d\n", ret);
3752c6f5c050SMian Yousaf Kaukab 		goto err_supplies;
375347a1685fSDinh Nguyen 	}
375447a1685fSDinh Nguyen 
3755117777b2SDinh Nguyen 	ret = usb_add_gadget_udc(dev, &hsotg->gadget);
375647a1685fSDinh Nguyen 	if (ret)
3757c6f5c050SMian Yousaf Kaukab 		goto err_supplies;
375847a1685fSDinh Nguyen 
37591f91b4ccSFelipe Balbi 	dwc2_hsotg_dump(hsotg);
376047a1685fSDinh Nguyen 
376147a1685fSDinh Nguyen 	return 0;
376247a1685fSDinh Nguyen 
376347a1685fSDinh Nguyen err_supplies:
37641f91b4ccSFelipe Balbi 	dwc2_hsotg_phy_disable(hsotg);
376547a1685fSDinh Nguyen err_clk:
376647a1685fSDinh Nguyen 	clk_disable_unprepare(hsotg->clk);
376747a1685fSDinh Nguyen 
376847a1685fSDinh Nguyen 	return ret;
376947a1685fSDinh Nguyen }
377047a1685fSDinh Nguyen 
377147a1685fSDinh Nguyen /**
37721f91b4ccSFelipe Balbi  * dwc2_hsotg_remove - remove function for hsotg driver
377347a1685fSDinh Nguyen  * @pdev: The platform information for the driver
377447a1685fSDinh Nguyen  */
37751f91b4ccSFelipe Balbi int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
377647a1685fSDinh Nguyen {
377747a1685fSDinh Nguyen 	usb_del_gadget_udc(&hsotg->gadget);
377847a1685fSDinh Nguyen 	clk_disable_unprepare(hsotg->clk);
377947a1685fSDinh Nguyen 
378047a1685fSDinh Nguyen 	return 0;
378147a1685fSDinh Nguyen }
378247a1685fSDinh Nguyen 
37831f91b4ccSFelipe Balbi int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
378447a1685fSDinh Nguyen {
378547a1685fSDinh Nguyen 	unsigned long flags;
378647a1685fSDinh Nguyen 	int ret = 0;
378747a1685fSDinh Nguyen 
37889e779778SGregory Herrero 	if (hsotg->lx_state != DWC2_L0)
37899e779778SGregory Herrero 		return ret;
37909e779778SGregory Herrero 
37917ad8096eSMarek Szyprowski 	mutex_lock(&hsotg->init_mutex);
37927ad8096eSMarek Szyprowski 
3793dc6e69e6SMarek Szyprowski 	if (hsotg->driver) {
3794dc6e69e6SMarek Szyprowski 		int ep;
3795dc6e69e6SMarek Szyprowski 
379647a1685fSDinh Nguyen 		dev_info(hsotg->dev, "suspending usb gadget %s\n",
379747a1685fSDinh Nguyen 			 hsotg->driver->driver.name);
379847a1685fSDinh Nguyen 
379947a1685fSDinh Nguyen 		spin_lock_irqsave(&hsotg->lock, flags);
3800dc6e69e6SMarek Szyprowski 		if (hsotg->enabled)
38011f91b4ccSFelipe Balbi 			dwc2_hsotg_core_disconnect(hsotg);
38021f91b4ccSFelipe Balbi 		dwc2_hsotg_disconnect(hsotg);
380347a1685fSDinh Nguyen 		hsotg->gadget.speed = USB_SPEED_UNKNOWN;
380447a1685fSDinh Nguyen 		spin_unlock_irqrestore(&hsotg->lock, flags);
380547a1685fSDinh Nguyen 
38061f91b4ccSFelipe Balbi 		dwc2_hsotg_phy_disable(hsotg);
38077b093f77SMarek Szyprowski 
3808c6f5c050SMian Yousaf Kaukab 		for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3809c6f5c050SMian Yousaf Kaukab 			if (hsotg->eps_in[ep])
38101f91b4ccSFelipe Balbi 				dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
3811c6f5c050SMian Yousaf Kaukab 			if (hsotg->eps_out[ep])
38121f91b4ccSFelipe Balbi 				dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
3813c6f5c050SMian Yousaf Kaukab 		}
381447a1685fSDinh Nguyen 
381547a1685fSDinh Nguyen 		ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
381647a1685fSDinh Nguyen 					     hsotg->supplies);
3817d00b4142SRobert Baldyga 		clk_disable(hsotg->clk);
381847a1685fSDinh Nguyen 	}
381947a1685fSDinh Nguyen 
38207ad8096eSMarek Szyprowski 	mutex_unlock(&hsotg->init_mutex);
38217ad8096eSMarek Szyprowski 
382247a1685fSDinh Nguyen 	return ret;
382347a1685fSDinh Nguyen }
382447a1685fSDinh Nguyen 
38251f91b4ccSFelipe Balbi int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
382647a1685fSDinh Nguyen {
382747a1685fSDinh Nguyen 	unsigned long flags;
382847a1685fSDinh Nguyen 	int ret = 0;
382947a1685fSDinh Nguyen 
38309e779778SGregory Herrero 	if (hsotg->lx_state == DWC2_L2)
38319e779778SGregory Herrero 		return ret;
38329e779778SGregory Herrero 
38337ad8096eSMarek Szyprowski 	mutex_lock(&hsotg->init_mutex);
38347ad8096eSMarek Szyprowski 
383547a1685fSDinh Nguyen 	if (hsotg->driver) {
383647a1685fSDinh Nguyen 		dev_info(hsotg->dev, "resuming usb gadget %s\n",
383747a1685fSDinh Nguyen 			 hsotg->driver->driver.name);
3838d00b4142SRobert Baldyga 
3839d00b4142SRobert Baldyga 		clk_enable(hsotg->clk);
384047a1685fSDinh Nguyen 		ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
384147a1685fSDinh Nguyen 					    hsotg->supplies);
384247a1685fSDinh Nguyen 
38431f91b4ccSFelipe Balbi 		dwc2_hsotg_phy_enable(hsotg);
384447a1685fSDinh Nguyen 
384547a1685fSDinh Nguyen 		spin_lock_irqsave(&hsotg->lock, flags);
38461f91b4ccSFelipe Balbi 		dwc2_hsotg_core_init_disconnected(hsotg, false);
3847dc6e69e6SMarek Szyprowski 		if (hsotg->enabled)
38481f91b4ccSFelipe Balbi 			dwc2_hsotg_core_connect(hsotg);
384947a1685fSDinh Nguyen 		spin_unlock_irqrestore(&hsotg->lock, flags);
3850dc6e69e6SMarek Szyprowski 	}
38517ad8096eSMarek Szyprowski 	mutex_unlock(&hsotg->init_mutex);
385247a1685fSDinh Nguyen 
385347a1685fSDinh Nguyen 	return ret;
385447a1685fSDinh Nguyen }
3855