xref: /openbmc/linux/drivers/usb/gadget/udc/bdc/bdc_udc.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2efed421aSAshwini Pahuja /*
3efed421aSAshwini Pahuja  * bdc_udc.c - BRCM BDC USB3.0 device controller gagdet ops
4efed421aSAshwini Pahuja  *
5efed421aSAshwini Pahuja  * Copyright (C) 2014 Broadcom Corporation
6efed421aSAshwini Pahuja  *
7efed421aSAshwini Pahuja  * Author: Ashwini Pahuja
8efed421aSAshwini Pahuja  *
9efed421aSAshwini Pahuja  * Based on drivers under drivers/usb/gadget/udc/
10efed421aSAshwini Pahuja  */
11efed421aSAshwini Pahuja #include <linux/module.h>
12efed421aSAshwini Pahuja #include <linux/pci.h>
13efed421aSAshwini Pahuja #include <linux/dma-mapping.h>
14efed421aSAshwini Pahuja #include <linux/kernel.h>
15efed421aSAshwini Pahuja #include <linux/delay.h>
16efed421aSAshwini Pahuja #include <linux/ioport.h>
17efed421aSAshwini Pahuja #include <linux/sched.h>
18efed421aSAshwini Pahuja #include <linux/slab.h>
19efed421aSAshwini Pahuja #include <linux/errno.h>
20efed421aSAshwini Pahuja #include <linux/init.h>
21efed421aSAshwini Pahuja #include <linux/timer.h>
22efed421aSAshwini Pahuja #include <linux/list.h>
23efed421aSAshwini Pahuja #include <linux/interrupt.h>
24efed421aSAshwini Pahuja #include <linux/moduleparam.h>
25efed421aSAshwini Pahuja #include <linux/device.h>
26efed421aSAshwini Pahuja #include <linux/usb/ch9.h>
27efed421aSAshwini Pahuja #include <linux/usb/gadget.h>
28efed421aSAshwini Pahuja #include <linux/usb/otg.h>
29efed421aSAshwini Pahuja #include <linux/pm.h>
30efed421aSAshwini Pahuja #include <linux/io.h>
31efed421aSAshwini Pahuja #include <linux/irq.h>
32efed421aSAshwini Pahuja #include <asm/unaligned.h>
33efed421aSAshwini Pahuja #include <linux/platform_device.h>
34efed421aSAshwini Pahuja 
35efed421aSAshwini Pahuja #include "bdc.h"
36efed421aSAshwini Pahuja #include "bdc_ep.h"
37efed421aSAshwini Pahuja #include "bdc_cmd.h"
38efed421aSAshwini Pahuja #include "bdc_dbg.h"
39efed421aSAshwini Pahuja 
40efed421aSAshwini Pahuja static const struct usb_gadget_ops bdc_gadget_ops;
41efed421aSAshwini Pahuja 
42efed421aSAshwini Pahuja static const char * const conn_speed_str[] =  {
43efed421aSAshwini Pahuja 	"Not connected",
44efed421aSAshwini Pahuja 	"Full Speed",
45efed421aSAshwini Pahuja 	"Low Speed",
46efed421aSAshwini Pahuja 	"High Speed",
47efed421aSAshwini Pahuja 	"Super Speed",
48efed421aSAshwini Pahuja };
49efed421aSAshwini Pahuja 
50efed421aSAshwini Pahuja /* EP0 initial descripror */
51efed421aSAshwini Pahuja static struct usb_endpoint_descriptor bdc_gadget_ep0_desc = {
52efed421aSAshwini Pahuja 	.bLength = USB_DT_ENDPOINT_SIZE,
53efed421aSAshwini Pahuja 	.bDescriptorType = USB_DT_ENDPOINT,
54efed421aSAshwini Pahuja 	.bmAttributes = USB_ENDPOINT_XFER_CONTROL,
55efed421aSAshwini Pahuja 	.bEndpointAddress = 0,
56efed421aSAshwini Pahuja 	.wMaxPacketSize	= cpu_to_le16(EP0_MAX_PKT_SIZE),
57efed421aSAshwini Pahuja };
58efed421aSAshwini Pahuja 
59efed421aSAshwini Pahuja /* Advance the srr dqp maintained by SW */
srr_dqp_index_advc(struct bdc * bdc,u32 srr_num)60efed421aSAshwini Pahuja static void srr_dqp_index_advc(struct bdc *bdc, u32 srr_num)
61efed421aSAshwini Pahuja {
62efed421aSAshwini Pahuja 	struct srr *srr;
63efed421aSAshwini Pahuja 
64efed421aSAshwini Pahuja 	srr = &bdc->srr;
65efed421aSAshwini Pahuja 	dev_dbg_ratelimited(bdc->dev, "srr->dqp_index:%d\n", srr->dqp_index);
66efed421aSAshwini Pahuja 	srr->dqp_index++;
67efed421aSAshwini Pahuja 	/* rollback to 0 if we are past the last */
68efed421aSAshwini Pahuja 	if (srr->dqp_index == NUM_SR_ENTRIES)
69efed421aSAshwini Pahuja 		srr->dqp_index = 0;
70efed421aSAshwini Pahuja }
71efed421aSAshwini Pahuja 
72efed421aSAshwini Pahuja /* connect sr */
bdc_uspc_connected(struct bdc * bdc)73efed421aSAshwini Pahuja static void bdc_uspc_connected(struct bdc *bdc)
74efed421aSAshwini Pahuja {
75efed421aSAshwini Pahuja 	u32 speed, temp;
76efed421aSAshwini Pahuja 	u32 usppms;
77efed421aSAshwini Pahuja 	int ret;
78efed421aSAshwini Pahuja 
79efed421aSAshwini Pahuja 	temp = bdc_readl(bdc->regs, BDC_USPC);
80efed421aSAshwini Pahuja 	speed = BDC_PSP(temp);
81efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "%s speed=%x\n", __func__, speed);
82efed421aSAshwini Pahuja 	switch (speed) {
83efed421aSAshwini Pahuja 	case BDC_SPEED_SS:
84efed421aSAshwini Pahuja 		bdc_gadget_ep0_desc.wMaxPacketSize =
85efed421aSAshwini Pahuja 						cpu_to_le16(EP0_MAX_PKT_SIZE);
86efed421aSAshwini Pahuja 		bdc->gadget.ep0->maxpacket = EP0_MAX_PKT_SIZE;
87efed421aSAshwini Pahuja 		bdc->gadget.speed = USB_SPEED_SUPER;
88efed421aSAshwini Pahuja 		/* Enable U1T in SS mode */
89efed421aSAshwini Pahuja 		usppms =  bdc_readl(bdc->regs, BDC_USPPMS);
90efed421aSAshwini Pahuja 		usppms &= ~BDC_U1T(0xff);
91efed421aSAshwini Pahuja 		usppms |= BDC_U1T(U1_TIMEOUT);
92efed421aSAshwini Pahuja 		usppms |= BDC_PORT_W1S;
93efed421aSAshwini Pahuja 		bdc_writel(bdc->regs, BDC_USPPMS, usppms);
94efed421aSAshwini Pahuja 		break;
95efed421aSAshwini Pahuja 
96efed421aSAshwini Pahuja 	case BDC_SPEED_HS:
97efed421aSAshwini Pahuja 		bdc_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
98efed421aSAshwini Pahuja 		bdc->gadget.ep0->maxpacket = 64;
99efed421aSAshwini Pahuja 		bdc->gadget.speed = USB_SPEED_HIGH;
100efed421aSAshwini Pahuja 		break;
101efed421aSAshwini Pahuja 
102efed421aSAshwini Pahuja 	case BDC_SPEED_FS:
103efed421aSAshwini Pahuja 		bdc_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
104efed421aSAshwini Pahuja 		bdc->gadget.ep0->maxpacket = 64;
105efed421aSAshwini Pahuja 		bdc->gadget.speed = USB_SPEED_FULL;
106efed421aSAshwini Pahuja 		break;
107efed421aSAshwini Pahuja 
108efed421aSAshwini Pahuja 	case BDC_SPEED_LS:
109efed421aSAshwini Pahuja 		bdc_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
110efed421aSAshwini Pahuja 		bdc->gadget.ep0->maxpacket = 8;
111efed421aSAshwini Pahuja 		bdc->gadget.speed = USB_SPEED_LOW;
112efed421aSAshwini Pahuja 		break;
113efed421aSAshwini Pahuja 	default:
114efed421aSAshwini Pahuja 		dev_err(bdc->dev, "UNDEFINED SPEED\n");
115efed421aSAshwini Pahuja 		return;
116efed421aSAshwini Pahuja 	}
117efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "connected at %s\n", conn_speed_str[speed]);
118efed421aSAshwini Pahuja 	/* Now we know the speed, configure ep0 */
119efed421aSAshwini Pahuja 	bdc->bdc_ep_array[1]->desc = &bdc_gadget_ep0_desc;
120efed421aSAshwini Pahuja 	ret = bdc_config_ep(bdc, bdc->bdc_ep_array[1]);
121efed421aSAshwini Pahuja 	if (ret)
122efed421aSAshwini Pahuja 		dev_err(bdc->dev, "EP0 config failed\n");
123efed421aSAshwini Pahuja 	bdc->bdc_ep_array[1]->usb_ep.desc = &bdc_gadget_ep0_desc;
124efed421aSAshwini Pahuja 	bdc->bdc_ep_array[1]->flags |= BDC_EP_ENABLED;
125efed421aSAshwini Pahuja 	usb_gadget_set_state(&bdc->gadget, USB_STATE_DEFAULT);
126efed421aSAshwini Pahuja }
127efed421aSAshwini Pahuja 
128efed421aSAshwini Pahuja /* device got disconnected */
bdc_uspc_disconnected(struct bdc * bdc,bool reinit)129efed421aSAshwini Pahuja static void bdc_uspc_disconnected(struct bdc *bdc, bool reinit)
130efed421aSAshwini Pahuja {
131efed421aSAshwini Pahuja 	struct bdc_ep *ep;
132efed421aSAshwini Pahuja 
133efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "%s\n", __func__);
134efed421aSAshwini Pahuja 	/*
135efed421aSAshwini Pahuja 	 * Only stop ep0 from here, rest of the endpoints will be disabled
136efed421aSAshwini Pahuja 	 * from gadget_disconnect
137efed421aSAshwini Pahuja 	 */
138efed421aSAshwini Pahuja 	ep = bdc->bdc_ep_array[1];
139efed421aSAshwini Pahuja 	if (ep && (ep->flags & BDC_EP_ENABLED))
140efed421aSAshwini Pahuja 		/* if enabled then stop and remove requests */
141efed421aSAshwini Pahuja 		bdc_ep_disable(ep);
142efed421aSAshwini Pahuja 
143efed421aSAshwini Pahuja 	if (bdc->gadget_driver && bdc->gadget_driver->disconnect) {
144efed421aSAshwini Pahuja 		spin_unlock(&bdc->lock);
145efed421aSAshwini Pahuja 		bdc->gadget_driver->disconnect(&bdc->gadget);
146efed421aSAshwini Pahuja 		spin_lock(&bdc->lock);
147efed421aSAshwini Pahuja 	}
148efed421aSAshwini Pahuja 	/* Set Unknown speed */
149efed421aSAshwini Pahuja 	bdc->gadget.speed = USB_SPEED_UNKNOWN;
150efed421aSAshwini Pahuja 	bdc->devstatus &= DEVSTATUS_CLEAR;
151efed421aSAshwini Pahuja 	bdc->delayed_status = false;
152efed421aSAshwini Pahuja 	bdc->reinit = reinit;
153efed421aSAshwini Pahuja 	bdc->test_mode = false;
154*fb8f60ddSJustin Chen 	usb_gadget_set_state(&bdc->gadget, USB_STATE_NOTATTACHED);
155efed421aSAshwini Pahuja }
156efed421aSAshwini Pahuja 
157efed421aSAshwini Pahuja /* TNotify wkaeup timer */
bdc_func_wake_timer(struct work_struct * work)158efed421aSAshwini Pahuja static void bdc_func_wake_timer(struct work_struct *work)
159efed421aSAshwini Pahuja {
160efed421aSAshwini Pahuja 	struct bdc *bdc = container_of(work, struct bdc, func_wake_notify.work);
161efed421aSAshwini Pahuja 	unsigned long flags;
162efed421aSAshwini Pahuja 
163efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "%s\n", __func__);
164efed421aSAshwini Pahuja 	spin_lock_irqsave(&bdc->lock, flags);
165efed421aSAshwini Pahuja 	/*
166efed421aSAshwini Pahuja 	 * Check if host has started transferring on endpoints
167efed421aSAshwini Pahuja 	 * FUNC_WAKE_ISSUED is cleared when transfer has started after resume
168efed421aSAshwini Pahuja 	 */
169efed421aSAshwini Pahuja 	if (bdc->devstatus & FUNC_WAKE_ISSUED) {
170efed421aSAshwini Pahuja 		dev_dbg(bdc->dev, "FUNC_WAKE_ISSUED FLAG IS STILL SET\n");
171efed421aSAshwini Pahuja 		/* flag is still set, so again send func wake */
172efed421aSAshwini Pahuja 		bdc_function_wake_fh(bdc, 0);
173efed421aSAshwini Pahuja 		schedule_delayed_work(&bdc->func_wake_notify,
174efed421aSAshwini Pahuja 						msecs_to_jiffies(BDC_TNOTIFY));
175efed421aSAshwini Pahuja 	}
176efed421aSAshwini Pahuja 	spin_unlock_irqrestore(&bdc->lock, flags);
177efed421aSAshwini Pahuja }
178efed421aSAshwini Pahuja 
179efed421aSAshwini Pahuja /* handler for Link state change condition */
handle_link_state_change(struct bdc * bdc,u32 uspc)180efed421aSAshwini Pahuja static void handle_link_state_change(struct bdc *bdc, u32 uspc)
181efed421aSAshwini Pahuja {
182efed421aSAshwini Pahuja 	u32 link_state;
183efed421aSAshwini Pahuja 
184efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "Link state change");
185efed421aSAshwini Pahuja 	link_state = BDC_PST(uspc);
186efed421aSAshwini Pahuja 	switch (link_state) {
187efed421aSAshwini Pahuja 	case BDC_LINK_STATE_U3:
188efed421aSAshwini Pahuja 		if ((bdc->gadget.speed != USB_SPEED_UNKNOWN) &&
189efed421aSAshwini Pahuja 						bdc->gadget_driver->suspend) {
190efed421aSAshwini Pahuja 			dev_dbg(bdc->dev, "Entered Suspend mode\n");
191efed421aSAshwini Pahuja 			spin_unlock(&bdc->lock);
192efed421aSAshwini Pahuja 			bdc->devstatus |= DEVICE_SUSPENDED;
193efed421aSAshwini Pahuja 			bdc->gadget_driver->suspend(&bdc->gadget);
194efed421aSAshwini Pahuja 			spin_lock(&bdc->lock);
195efed421aSAshwini Pahuja 		}
196efed421aSAshwini Pahuja 		break;
197efed421aSAshwini Pahuja 	case BDC_LINK_STATE_U0:
198efed421aSAshwini Pahuja 		if (bdc->devstatus & REMOTE_WAKEUP_ISSUED) {
199efed421aSAshwini Pahuja 			bdc->devstatus &= ~REMOTE_WAKEUP_ISSUED;
200efed421aSAshwini Pahuja 			if (bdc->gadget.speed == USB_SPEED_SUPER) {
201efed421aSAshwini Pahuja 				bdc_function_wake_fh(bdc, 0);
202efed421aSAshwini Pahuja 				bdc->devstatus |= FUNC_WAKE_ISSUED;
203efed421aSAshwini Pahuja 				/*
204efed421aSAshwini Pahuja 				 * Start a Notification timer and check if the
205efed421aSAshwini Pahuja 				 * Host transferred anything on any of the EPs,
206efed421aSAshwini Pahuja 				 * if not then send function wake again every
207efed421aSAshwini Pahuja 				 * TNotification secs until host initiates
208efed421aSAshwini Pahuja 				 * transfer to BDC, USB3 spec Table 8.13
209efed421aSAshwini Pahuja 				 */
210efed421aSAshwini Pahuja 				schedule_delayed_work(
211efed421aSAshwini Pahuja 						&bdc->func_wake_notify,
212efed421aSAshwini Pahuja 						msecs_to_jiffies(BDC_TNOTIFY));
213efed421aSAshwini Pahuja 				dev_dbg(bdc->dev, "sched func_wake_notify\n");
214efed421aSAshwini Pahuja 			}
215efed421aSAshwini Pahuja 		}
216efed421aSAshwini Pahuja 		break;
217efed421aSAshwini Pahuja 
218efed421aSAshwini Pahuja 	case BDC_LINK_STATE_RESUME:
219efed421aSAshwini Pahuja 		dev_dbg(bdc->dev, "Resumed from Suspend\n");
220efed421aSAshwini Pahuja 		if (bdc->devstatus & DEVICE_SUSPENDED) {
221efed421aSAshwini Pahuja 			bdc->gadget_driver->resume(&bdc->gadget);
222efed421aSAshwini Pahuja 			bdc->devstatus &= ~DEVICE_SUSPENDED;
223efed421aSAshwini Pahuja 		}
224efed421aSAshwini Pahuja 		break;
225efed421aSAshwini Pahuja 	default:
226efed421aSAshwini Pahuja 		dev_dbg(bdc->dev, "link state:%d\n", link_state);
227efed421aSAshwini Pahuja 	}
228efed421aSAshwini Pahuja }
229efed421aSAshwini Pahuja 
230efed421aSAshwini Pahuja /* something changes on upstream port, handle it here */
bdc_sr_uspc(struct bdc * bdc,struct bdc_sr * sreport)231efed421aSAshwini Pahuja void bdc_sr_uspc(struct bdc *bdc, struct bdc_sr *sreport)
232efed421aSAshwini Pahuja {
233efed421aSAshwini Pahuja 	u32 clear_flags = 0;
234efed421aSAshwini Pahuja 	u32 uspc;
235efed421aSAshwini Pahuja 	bool connected = false;
236efed421aSAshwini Pahuja 	bool disconn = false;
237efed421aSAshwini Pahuja 
238efed421aSAshwini Pahuja 	uspc = bdc_readl(bdc->regs, BDC_USPC);
239efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "%s uspc=0x%08x\n", __func__, uspc);
240efed421aSAshwini Pahuja 
241efed421aSAshwini Pahuja 	/* Port connect changed */
242efed421aSAshwini Pahuja 	if (uspc & BDC_PCC) {
243efed421aSAshwini Pahuja 		/* Vbus not present, and not connected to Downstream port */
244efed421aSAshwini Pahuja 		if ((uspc & BDC_VBC) && !(uspc & BDC_VBS) && !(uspc & BDC_PCS))
245efed421aSAshwini Pahuja 			disconn = true;
246efed421aSAshwini Pahuja 		else if ((uspc & BDC_PCS) && !BDC_PST(uspc))
247efed421aSAshwini Pahuja 			connected = true;
248cff97f33SAl Cooper 		clear_flags |= BDC_PCC;
249efed421aSAshwini Pahuja 	}
250efed421aSAshwini Pahuja 
251efed421aSAshwini Pahuja 	/* Change in VBus and VBus is present */
252efed421aSAshwini Pahuja 	if ((uspc & BDC_VBC) && (uspc & BDC_VBS)) {
253efed421aSAshwini Pahuja 		if (bdc->pullup) {
254efed421aSAshwini Pahuja 			dev_dbg(bdc->dev, "Do a softconnect\n");
255efed421aSAshwini Pahuja 			/* Attached state, do a softconnect */
256efed421aSAshwini Pahuja 			bdc_softconn(bdc);
257efed421aSAshwini Pahuja 			usb_gadget_set_state(&bdc->gadget, USB_STATE_POWERED);
258efed421aSAshwini Pahuja 		}
259cff97f33SAl Cooper 		clear_flags |= BDC_VBC;
260efed421aSAshwini Pahuja 	} else if ((uspc & BDC_PRS) || (uspc & BDC_PRC) || disconn) {
261efed421aSAshwini Pahuja 		/* Hot reset, warm reset, 2.0 bus reset or disconn */
262efed421aSAshwini Pahuja 		dev_dbg(bdc->dev, "Port reset or disconn\n");
263efed421aSAshwini Pahuja 		bdc_uspc_disconnected(bdc, disconn);
264cff97f33SAl Cooper 		clear_flags |= BDC_PRC;
265efed421aSAshwini Pahuja 	} else if ((uspc & BDC_PSC) && (uspc & BDC_PCS)) {
266efed421aSAshwini Pahuja 		/* Change in Link state */
267efed421aSAshwini Pahuja 		handle_link_state_change(bdc, uspc);
268cff97f33SAl Cooper 		clear_flags |= BDC_PSC;
269efed421aSAshwini Pahuja 	}
270efed421aSAshwini Pahuja 
271efed421aSAshwini Pahuja 	/*
272efed421aSAshwini Pahuja 	 * In SS we might not have PRC bit set before connection, but in 2.0
273efed421aSAshwini Pahuja 	 * the PRC bit is set before connection, so moving this condition out
274efed421aSAshwini Pahuja 	 * of bus reset to handle both SS/2.0 speeds.
275efed421aSAshwini Pahuja 	 */
276efed421aSAshwini Pahuja 	if (connected) {
277efed421aSAshwini Pahuja 		/* This is the connect event for U0/L0 */
278efed421aSAshwini Pahuja 		dev_dbg(bdc->dev, "Connected\n");
279efed421aSAshwini Pahuja 		bdc_uspc_connected(bdc);
280efed421aSAshwini Pahuja 		bdc->devstatus &= ~(DEVICE_SUSPENDED);
281efed421aSAshwini Pahuja 	}
282efed421aSAshwini Pahuja 	uspc = bdc_readl(bdc->regs, BDC_USPC);
283efed421aSAshwini Pahuja 	uspc &= (~BDC_USPSC_RW);
284efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "uspc=%x\n", uspc);
285efed421aSAshwini Pahuja 	bdc_writel(bdc->regs, BDC_USPC, clear_flags);
286efed421aSAshwini Pahuja }
287efed421aSAshwini Pahuja 
288efed421aSAshwini Pahuja /* Main interrupt handler for bdc */
bdc_udc_interrupt(int irq,void * _bdc)289efed421aSAshwini Pahuja static irqreturn_t bdc_udc_interrupt(int irq, void *_bdc)
290efed421aSAshwini Pahuja {
291efed421aSAshwini Pahuja 	u32 eqp_index, dqp_index, sr_type, srr_int;
292efed421aSAshwini Pahuja 	struct bdc_sr *sreport;
293efed421aSAshwini Pahuja 	struct bdc *bdc = _bdc;
294efed421aSAshwini Pahuja 	u32 status;
295efed421aSAshwini Pahuja 	int ret;
296efed421aSAshwini Pahuja 
297efed421aSAshwini Pahuja 	spin_lock(&bdc->lock);
298efed421aSAshwini Pahuja 	status = bdc_readl(bdc->regs, BDC_BDCSC);
299efed421aSAshwini Pahuja 	if (!(status & BDC_GIP)) {
300efed421aSAshwini Pahuja 		spin_unlock(&bdc->lock);
301efed421aSAshwini Pahuja 		return IRQ_NONE;
302efed421aSAshwini Pahuja 	}
303efed421aSAshwini Pahuja 	srr_int = bdc_readl(bdc->regs, BDC_SRRINT(0));
304efed421aSAshwini Pahuja 	/* Check if the SRR IP bit it set? */
305efed421aSAshwini Pahuja 	if (!(srr_int & BDC_SRR_IP)) {
306efed421aSAshwini Pahuja 		dev_warn(bdc->dev, "Global irq pending but SRR IP is 0\n");
307efed421aSAshwini Pahuja 		spin_unlock(&bdc->lock);
308efed421aSAshwini Pahuja 		return IRQ_NONE;
309efed421aSAshwini Pahuja 	}
310efed421aSAshwini Pahuja 	eqp_index = BDC_SRR_EPI(srr_int);
311efed421aSAshwini Pahuja 	dqp_index = BDC_SRR_DPI(srr_int);
312efed421aSAshwini Pahuja 	dev_dbg(bdc->dev,
313efed421aSAshwini Pahuja 			"%s eqp_index=%d dqp_index=%d  srr.dqp_index=%d\n\n",
314efed421aSAshwini Pahuja 			 __func__, eqp_index, dqp_index, bdc->srr.dqp_index);
315efed421aSAshwini Pahuja 
316efed421aSAshwini Pahuja 	/* check for ring empty condition */
317efed421aSAshwini Pahuja 	if (eqp_index == dqp_index) {
318efed421aSAshwini Pahuja 		dev_dbg(bdc->dev, "SRR empty?\n");
319efed421aSAshwini Pahuja 		spin_unlock(&bdc->lock);
320efed421aSAshwini Pahuja 		return IRQ_HANDLED;
321efed421aSAshwini Pahuja 	}
322efed421aSAshwini Pahuja 
323efed421aSAshwini Pahuja 	while (bdc->srr.dqp_index != eqp_index) {
324efed421aSAshwini Pahuja 		sreport = &bdc->srr.sr_bds[bdc->srr.dqp_index];
325efed421aSAshwini Pahuja 		/* sreport is read before using it */
326efed421aSAshwini Pahuja 		rmb();
327efed421aSAshwini Pahuja 		sr_type = le32_to_cpu(sreport->offset[3]) & BD_TYPE_BITMASK;
328efed421aSAshwini Pahuja 		dev_dbg_ratelimited(bdc->dev, "sr_type=%d\n", sr_type);
329efed421aSAshwini Pahuja 		switch (sr_type) {
330efed421aSAshwini Pahuja 		case SR_XSF:
331efed421aSAshwini Pahuja 			bdc->sr_handler[0](bdc, sreport);
332efed421aSAshwini Pahuja 			break;
333efed421aSAshwini Pahuja 
334efed421aSAshwini Pahuja 		case SR_USPC:
335efed421aSAshwini Pahuja 			bdc->sr_handler[1](bdc, sreport);
336efed421aSAshwini Pahuja 			break;
337efed421aSAshwini Pahuja 		default:
338efed421aSAshwini Pahuja 			dev_warn(bdc->dev, "SR:%d not handled\n", sr_type);
339efed421aSAshwini Pahuja 		}
340efed421aSAshwini Pahuja 		/* Advance the srr dqp index */
341efed421aSAshwini Pahuja 		srr_dqp_index_advc(bdc, 0);
342efed421aSAshwini Pahuja 	}
343efed421aSAshwini Pahuja 	/* update the hw dequeue pointer */
344efed421aSAshwini Pahuja 	srr_int = bdc_readl(bdc->regs, BDC_SRRINT(0));
345efed421aSAshwini Pahuja 	srr_int &= ~BDC_SRR_DPI_MASK;
346efed421aSAshwini Pahuja 	srr_int &= ~(BDC_SRR_RWS|BDC_SRR_RST|BDC_SRR_ISR);
347efed421aSAshwini Pahuja 	srr_int |= ((bdc->srr.dqp_index) << 16);
348efed421aSAshwini Pahuja 	srr_int |= BDC_SRR_IP;
349efed421aSAshwini Pahuja 	bdc_writel(bdc->regs, BDC_SRRINT(0), srr_int);
350efed421aSAshwini Pahuja 	srr_int = bdc_readl(bdc->regs, BDC_SRRINT(0));
351efed421aSAshwini Pahuja 	if (bdc->reinit) {
352efed421aSAshwini Pahuja 		ret = bdc_reinit(bdc);
353efed421aSAshwini Pahuja 		if (ret)
354efed421aSAshwini Pahuja 			dev_err(bdc->dev, "err in bdc reinit\n");
355efed421aSAshwini Pahuja 	}
356efed421aSAshwini Pahuja 
357efed421aSAshwini Pahuja 	spin_unlock(&bdc->lock);
358efed421aSAshwini Pahuja 
359efed421aSAshwini Pahuja 	return IRQ_HANDLED;
360efed421aSAshwini Pahuja }
361efed421aSAshwini Pahuja 
362efed421aSAshwini Pahuja /* Gadget ops */
bdc_udc_start(struct usb_gadget * gadget,struct usb_gadget_driver * driver)363efed421aSAshwini Pahuja static int bdc_udc_start(struct usb_gadget *gadget,
364efed421aSAshwini Pahuja 				struct usb_gadget_driver *driver)
365efed421aSAshwini Pahuja {
366efed421aSAshwini Pahuja 	struct bdc *bdc = gadget_to_bdc(gadget);
367efed421aSAshwini Pahuja 	unsigned long flags;
368efed421aSAshwini Pahuja 	int ret = 0;
369efed421aSAshwini Pahuja 
370efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "%s()\n", __func__);
371efed421aSAshwini Pahuja 	spin_lock_irqsave(&bdc->lock, flags);
372efed421aSAshwini Pahuja 	if (bdc->gadget_driver) {
373efed421aSAshwini Pahuja 		dev_err(bdc->dev, "%s is already bound to %s\n",
374efed421aSAshwini Pahuja 			bdc->gadget.name,
375efed421aSAshwini Pahuja 			bdc->gadget_driver->driver.name);
376efed421aSAshwini Pahuja 		ret = -EBUSY;
377efed421aSAshwini Pahuja 		goto err;
378efed421aSAshwini Pahuja 	}
379efed421aSAshwini Pahuja 	/*
380efed421aSAshwini Pahuja 	 * Run the controller from here and when BDC is connected to
381efed421aSAshwini Pahuja 	 * Host then driver will receive a USPC SR with VBUS present
382efed421aSAshwini Pahuja 	 * and then driver will do a softconnect.
383efed421aSAshwini Pahuja 	 */
384efed421aSAshwini Pahuja 	ret = bdc_run(bdc);
385efed421aSAshwini Pahuja 	if (ret) {
386efed421aSAshwini Pahuja 		dev_err(bdc->dev, "%s bdc run fail\n", __func__);
387efed421aSAshwini Pahuja 		goto err;
388efed421aSAshwini Pahuja 	}
389efed421aSAshwini Pahuja 	bdc->gadget_driver = driver;
390efed421aSAshwini Pahuja 	bdc->gadget.dev.driver = &driver->driver;
391efed421aSAshwini Pahuja err:
392efed421aSAshwini Pahuja 	spin_unlock_irqrestore(&bdc->lock, flags);
393efed421aSAshwini Pahuja 
394efed421aSAshwini Pahuja 	return ret;
395efed421aSAshwini Pahuja }
396efed421aSAshwini Pahuja 
bdc_udc_stop(struct usb_gadget * gadget)397efed421aSAshwini Pahuja static int bdc_udc_stop(struct usb_gadget *gadget)
398efed421aSAshwini Pahuja {
399efed421aSAshwini Pahuja 	struct bdc *bdc = gadget_to_bdc(gadget);
400efed421aSAshwini Pahuja 	unsigned long flags;
401efed421aSAshwini Pahuja 
402efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "%s()\n", __func__);
403efed421aSAshwini Pahuja 	spin_lock_irqsave(&bdc->lock, flags);
404efed421aSAshwini Pahuja 	bdc_stop(bdc);
405efed421aSAshwini Pahuja 	bdc->gadget_driver = NULL;
406efed421aSAshwini Pahuja 	bdc->gadget.dev.driver = NULL;
407efed421aSAshwini Pahuja 	spin_unlock_irqrestore(&bdc->lock, flags);
408efed421aSAshwini Pahuja 
409efed421aSAshwini Pahuja 	return 0;
410efed421aSAshwini Pahuja }
411efed421aSAshwini Pahuja 
bdc_udc_pullup(struct usb_gadget * gadget,int is_on)412efed421aSAshwini Pahuja static int bdc_udc_pullup(struct usb_gadget *gadget, int is_on)
413efed421aSAshwini Pahuja {
414efed421aSAshwini Pahuja 	struct bdc *bdc = gadget_to_bdc(gadget);
415efed421aSAshwini Pahuja 	unsigned long flags;
416efed421aSAshwini Pahuja 	u32 uspc;
417efed421aSAshwini Pahuja 
418efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "%s() is_on:%d\n", __func__, is_on);
419efed421aSAshwini Pahuja 	if (!gadget)
420efed421aSAshwini Pahuja 		return -EINVAL;
421efed421aSAshwini Pahuja 
422efed421aSAshwini Pahuja 	spin_lock_irqsave(&bdc->lock, flags);
423efed421aSAshwini Pahuja 	if (!is_on) {
424efed421aSAshwini Pahuja 		bdc_softdisconn(bdc);
425efed421aSAshwini Pahuja 		bdc->pullup = false;
426efed421aSAshwini Pahuja 	} else {
427efed421aSAshwini Pahuja 		/*
428efed421aSAshwini Pahuja 		 * For a self powered device, we need to wait till we receive
429efed421aSAshwini Pahuja 		 * a VBUS change and Vbus present event, then if pullup flag
430efed421aSAshwini Pahuja 		 * is set, then only we present the Termintation.
431efed421aSAshwini Pahuja 		 */
432efed421aSAshwini Pahuja 		bdc->pullup = true;
433efed421aSAshwini Pahuja 		/*
434efed421aSAshwini Pahuja 		 * Check if BDC is already connected to Host i.e Vbus=1,
435efed421aSAshwini Pahuja 		 * if yes, then present TERM now, this is typical for bus
436efed421aSAshwini Pahuja 		 * powered devices.
437efed421aSAshwini Pahuja 		 */
438efed421aSAshwini Pahuja 		uspc = bdc_readl(bdc->regs, BDC_USPC);
439efed421aSAshwini Pahuja 		if (uspc & BDC_VBS)
440efed421aSAshwini Pahuja 			bdc_softconn(bdc);
441efed421aSAshwini Pahuja 	}
442efed421aSAshwini Pahuja 	spin_unlock_irqrestore(&bdc->lock, flags);
443efed421aSAshwini Pahuja 
444efed421aSAshwini Pahuja 	return 0;
445efed421aSAshwini Pahuja }
446efed421aSAshwini Pahuja 
bdc_udc_set_selfpowered(struct usb_gadget * gadget,int is_self)447efed421aSAshwini Pahuja static int bdc_udc_set_selfpowered(struct usb_gadget *gadget,
448efed421aSAshwini Pahuja 		int is_self)
449efed421aSAshwini Pahuja {
450efed421aSAshwini Pahuja 	struct bdc		*bdc = gadget_to_bdc(gadget);
451efed421aSAshwini Pahuja 	unsigned long           flags;
452efed421aSAshwini Pahuja 
453efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "%s()\n", __func__);
4544605437eSPeter Chen 	gadget->is_selfpowered = (is_self != 0);
455efed421aSAshwini Pahuja 	spin_lock_irqsave(&bdc->lock, flags);
456efed421aSAshwini Pahuja 	if (!is_self)
457efed421aSAshwini Pahuja 		bdc->devstatus |= 1 << USB_DEVICE_SELF_POWERED;
458efed421aSAshwini Pahuja 	else
459efed421aSAshwini Pahuja 		bdc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
460efed421aSAshwini Pahuja 
461efed421aSAshwini Pahuja 	spin_unlock_irqrestore(&bdc->lock, flags);
462efed421aSAshwini Pahuja 
463efed421aSAshwini Pahuja 	return 0;
464efed421aSAshwini Pahuja }
465efed421aSAshwini Pahuja 
bdc_udc_wakeup(struct usb_gadget * gadget)466efed421aSAshwini Pahuja static int bdc_udc_wakeup(struct usb_gadget *gadget)
467efed421aSAshwini Pahuja {
468efed421aSAshwini Pahuja 	struct bdc *bdc = gadget_to_bdc(gadget);
469efed421aSAshwini Pahuja 	unsigned long		flags;
470efed421aSAshwini Pahuja 	u8	link_state;
471efed421aSAshwini Pahuja 	u32	uspc;
472efed421aSAshwini Pahuja 	int ret = 0;
473efed421aSAshwini Pahuja 
474efed421aSAshwini Pahuja 	dev_dbg(bdc->dev,
475efed421aSAshwini Pahuja 		"%s() bdc->devstatus=%08x\n",
476efed421aSAshwini Pahuja 		__func__, bdc->devstatus);
477efed421aSAshwini Pahuja 
478efed421aSAshwini Pahuja 	if (!(bdc->devstatus & REMOTE_WAKE_ENABLE))
479efed421aSAshwini Pahuja 		return  -EOPNOTSUPP;
480efed421aSAshwini Pahuja 
481efed421aSAshwini Pahuja 	spin_lock_irqsave(&bdc->lock, flags);
482efed421aSAshwini Pahuja 	uspc = bdc_readl(bdc->regs, BDC_USPC);
483efed421aSAshwini Pahuja 	link_state = BDC_PST(uspc);
484efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "link_state =%d portsc=%x", link_state, uspc);
485efed421aSAshwini Pahuja 	if (link_state != BDC_LINK_STATE_U3) {
486efed421aSAshwini Pahuja 		dev_warn(bdc->dev,
487efed421aSAshwini Pahuja 			"can't wakeup from link state %d\n",
488efed421aSAshwini Pahuja 			link_state);
489efed421aSAshwini Pahuja 		ret = -EINVAL;
490efed421aSAshwini Pahuja 		goto out;
491efed421aSAshwini Pahuja 	}
492efed421aSAshwini Pahuja 	if (bdc->gadget.speed == USB_SPEED_SUPER)
493efed421aSAshwini Pahuja 		bdc->devstatus |= REMOTE_WAKEUP_ISSUED;
494efed421aSAshwini Pahuja 
495efed421aSAshwini Pahuja 	uspc &= ~BDC_PST_MASK;
496efed421aSAshwini Pahuja 	uspc &= (~BDC_USPSC_RW);
497efed421aSAshwini Pahuja 	uspc |=  BDC_PST(BDC_LINK_STATE_U0);
498efed421aSAshwini Pahuja 	uspc |=  BDC_SWS;
499efed421aSAshwini Pahuja 	bdc_writel(bdc->regs, BDC_USPC, uspc);
500efed421aSAshwini Pahuja 	uspc = bdc_readl(bdc->regs, BDC_USPC);
501efed421aSAshwini Pahuja 	link_state = BDC_PST(uspc);
502efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "link_state =%d portsc=%x", link_state, uspc);
503efed421aSAshwini Pahuja out:
504efed421aSAshwini Pahuja 	spin_unlock_irqrestore(&bdc->lock, flags);
505efed421aSAshwini Pahuja 
506efed421aSAshwini Pahuja 	return ret;
507efed421aSAshwini Pahuja }
508efed421aSAshwini Pahuja 
509efed421aSAshwini Pahuja static const struct usb_gadget_ops bdc_gadget_ops = {
510efed421aSAshwini Pahuja 	.wakeup = bdc_udc_wakeup,
511efed421aSAshwini Pahuja 	.set_selfpowered = bdc_udc_set_selfpowered,
512efed421aSAshwini Pahuja 	.pullup = bdc_udc_pullup,
513efed421aSAshwini Pahuja 	.udc_start = bdc_udc_start,
514efed421aSAshwini Pahuja 	.udc_stop = bdc_udc_stop,
515efed421aSAshwini Pahuja };
516efed421aSAshwini Pahuja 
517efed421aSAshwini Pahuja /* Init the gadget interface and register the udc */
bdc_udc_init(struct bdc * bdc)518efed421aSAshwini Pahuja int bdc_udc_init(struct bdc *bdc)
519efed421aSAshwini Pahuja {
520efed421aSAshwini Pahuja 	u32 temp;
521efed421aSAshwini Pahuja 	int ret;
522efed421aSAshwini Pahuja 
523efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "%s()\n", __func__);
524efed421aSAshwini Pahuja 	bdc->gadget.ops = &bdc_gadget_ops;
525efed421aSAshwini Pahuja 	bdc->gadget.max_speed = USB_SPEED_SUPER;
526efed421aSAshwini Pahuja 	bdc->gadget.speed = USB_SPEED_UNKNOWN;
527efed421aSAshwini Pahuja 	bdc->gadget.dev.parent = bdc->dev;
528efed421aSAshwini Pahuja 
529efed421aSAshwini Pahuja 	bdc->gadget.sg_supported = false;
530efed421aSAshwini Pahuja 
531efed421aSAshwini Pahuja 
532efed421aSAshwini Pahuja 	bdc->gadget.name = BRCM_BDC_NAME;
533efed421aSAshwini Pahuja 	ret = devm_request_irq(bdc->dev, bdc->irq, bdc_udc_interrupt,
534efed421aSAshwini Pahuja 				IRQF_SHARED, BRCM_BDC_NAME, bdc);
535efed421aSAshwini Pahuja 	if (ret) {
536efed421aSAshwini Pahuja 		dev_err(bdc->dev,
537efed421aSAshwini Pahuja 			"failed to request irq #%d %d\n",
538efed421aSAshwini Pahuja 			bdc->irq, ret);
539efed421aSAshwini Pahuja 		return ret;
540efed421aSAshwini Pahuja 	}
541efed421aSAshwini Pahuja 
542efed421aSAshwini Pahuja 	ret = bdc_init_ep(bdc);
543efed421aSAshwini Pahuja 	if (ret) {
544efed421aSAshwini Pahuja 		dev_err(bdc->dev, "bdc init ep fail: %d\n", ret);
545efed421aSAshwini Pahuja 		return ret;
546efed421aSAshwini Pahuja 	}
547efed421aSAshwini Pahuja 
548efed421aSAshwini Pahuja 	ret = usb_add_gadget_udc(bdc->dev, &bdc->gadget);
549efed421aSAshwini Pahuja 	if (ret) {
550efed421aSAshwini Pahuja 		dev_err(bdc->dev, "failed to register udc\n");
551efed421aSAshwini Pahuja 		goto err0;
552efed421aSAshwini Pahuja 	}
553efed421aSAshwini Pahuja 	usb_gadget_set_state(&bdc->gadget, USB_STATE_NOTATTACHED);
554efed421aSAshwini Pahuja 	bdc->bdc_ep_array[1]->desc = &bdc_gadget_ep0_desc;
555efed421aSAshwini Pahuja 	/*
556efed421aSAshwini Pahuja 	 * Allocate bd list for ep0 only, ep0 will be enabled on connect
557efed421aSAshwini Pahuja 	 * status report when the speed is known
558efed421aSAshwini Pahuja 	 */
559efed421aSAshwini Pahuja 	ret = bdc_ep_enable(bdc->bdc_ep_array[1]);
560efed421aSAshwini Pahuja 	if (ret) {
561efed421aSAshwini Pahuja 		dev_err(bdc->dev, "fail to enable %s\n",
562efed421aSAshwini Pahuja 						bdc->bdc_ep_array[1]->name);
563efed421aSAshwini Pahuja 		goto err1;
564efed421aSAshwini Pahuja 	}
565efed421aSAshwini Pahuja 	INIT_DELAYED_WORK(&bdc->func_wake_notify, bdc_func_wake_timer);
566efed421aSAshwini Pahuja 	/* Enable Interrupts */
567efed421aSAshwini Pahuja 	temp = bdc_readl(bdc->regs, BDC_BDCSC);
568efed421aSAshwini Pahuja 	temp |= BDC_GIE;
569efed421aSAshwini Pahuja 	bdc_writel(bdc->regs, BDC_BDCSC, temp);
570efed421aSAshwini Pahuja 	return 0;
571efed421aSAshwini Pahuja err1:
572efed421aSAshwini Pahuja 	usb_del_gadget_udc(&bdc->gadget);
573efed421aSAshwini Pahuja err0:
574efed421aSAshwini Pahuja 	bdc_free_ep(bdc);
575efed421aSAshwini Pahuja 
576efed421aSAshwini Pahuja 	return ret;
577efed421aSAshwini Pahuja }
578efed421aSAshwini Pahuja 
bdc_udc_exit(struct bdc * bdc)579efed421aSAshwini Pahuja void bdc_udc_exit(struct bdc *bdc)
580efed421aSAshwini Pahuja {
581cff5638eSAlexey Khoroshilov 	unsigned long flags;
582cff5638eSAlexey Khoroshilov 
583efed421aSAshwini Pahuja 	dev_dbg(bdc->dev, "%s()\n", __func__);
584cff5638eSAlexey Khoroshilov 	spin_lock_irqsave(&bdc->lock, flags);
585efed421aSAshwini Pahuja 	bdc_ep_disable(bdc->bdc_ep_array[1]);
586cff5638eSAlexey Khoroshilov 	spin_unlock_irqrestore(&bdc->lock, flags);
587cff5638eSAlexey Khoroshilov 
588efed421aSAshwini Pahuja 	usb_del_gadget_udc(&bdc->gadget);
589efed421aSAshwini Pahuja 	bdc_free_ep(bdc);
590efed421aSAshwini Pahuja }
591