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