1 // SPDX-License-Identifier: GPL-2.0+
3 * bdc_udc.c - BRCM BDC USB3.0 device controller gagdet ops
5 * Copyright (C) 2014 Broadcom Corporation
7 * Author: Ashwini Pahuja
9 * Based on drivers under drivers/usb/gadget/udc/
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>
31 #include <linux/irq.h>
32 #include <asm/unaligned.h>
33 #include <linux/platform_device.h>
40 static const struct usb_gadget_ops bdc_gadget_ops;
42 static const char * const conn_speed_str[] = {
50 /* EP0 initial descripror */
51 static 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),
59 /* Advance the srr dqp maintained by SW */
60 static void srr_dqp_index_advc(struct bdc *bdc, u32 srr_num)
65 dev_dbg_ratelimited(bdc->dev, "srr->dqp_index:%d\n", srr->dqp_index);
67 /* rollback to 0 if we are past the last */
68 if (srr->dqp_index == NUM_SR_ENTRIES)
73 static void bdc_uspc_connected(struct bdc *bdc)
79 temp = bdc_readl(bdc->regs, BDC_USPC);
80 speed = BDC_PSP(temp);
81 dev_dbg(bdc->dev, "%s speed=%x\n", __func__, speed);
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);
97 bdc_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
98 bdc->gadget.ep0->maxpacket = 64;
99 bdc->gadget.speed = USB_SPEED_HIGH;
103 bdc_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
104 bdc->gadget.ep0->maxpacket = 64;
105 bdc->gadget.speed = USB_SPEED_FULL;
109 bdc_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
110 bdc->gadget.ep0->maxpacket = 8;
111 bdc->gadget.speed = USB_SPEED_LOW;
114 dev_err(bdc->dev, "UNDEFINED SPEED\n");
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]);
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);
128 /* device got disconnected */
129 static void bdc_uspc_disconnected(struct bdc *bdc, bool reinit)
133 dev_dbg(bdc->dev, "%s\n", __func__);
135 * Only stop ep0 from here, rest of the endpoints will be disabled
136 * from gadget_disconnect
138 ep = bdc->bdc_ep_array[1];
139 if (ep && (ep->flags & BDC_EP_ENABLED))
140 /* if enabled then stop and remove requests */
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);
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);
157 /* TNotify wkaeup timer */
158 static void bdc_func_wake_timer(struct work_struct *work)
160 struct bdc *bdc = container_of(work, struct bdc, func_wake_notify.work);
163 dev_dbg(bdc->dev, "%s\n", __func__);
164 spin_lock_irqsave(&bdc->lock, flags);
166 * Check if host has started transferring on endpoints
167 * FUNC_WAKE_ISSUED is cleared when transfer has started after resume
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));
176 spin_unlock_irqrestore(&bdc->lock, flags);
179 /* handler for Link state change condition */
180 static void handle_link_state_change(struct bdc *bdc, u32 uspc)
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);
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;
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
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");
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;
226 dev_dbg(bdc->dev, "link state:%d\n", link_state);
230 /* something changes on upstream port, handle it here */
231 void bdc_sr_uspc(struct bdc *bdc, struct bdc_sr *sreport)
235 bool connected = false;
236 bool disconn = false;
238 uspc = bdc_readl(bdc->regs, BDC_USPC);
239 dev_dbg(bdc->dev, "%s uspc=0x%08x\n", __func__, uspc);
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))
246 else if ((uspc & BDC_PCS) && !BDC_PST(uspc))
248 clear_flags |= BDC_PCC;
251 /* Change in VBus and VBus is present */
252 if ((uspc & BDC_VBC) && (uspc & BDC_VBS)) {
254 dev_dbg(bdc->dev, "Do a softconnect\n");
255 /* Attached state, do a softconnect */
257 usb_gadget_set_state(&bdc->gadget, USB_STATE_POWERED);
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;
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.
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);
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);
288 /* Main interrupt handler for bdc */
289 static irqreturn_t bdc_udc_interrupt(int irq, void *_bdc)
291 u32 eqp_index, dqp_index, sr_type, srr_int;
292 struct bdc_sr *sreport;
293 struct bdc *bdc = _bdc;
297 spin_lock(&bdc->lock);
298 status = bdc_readl(bdc->regs, BDC_BDCSC);
299 if (!(status & BDC_GIP)) {
300 spin_unlock(&bdc->lock);
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);
310 eqp_index = BDC_SRR_EPI(srr_int);
311 dqp_index = BDC_SRR_DPI(srr_int);
313 "%s eqp_index=%d dqp_index=%d srr.dqp_index=%d\n\n",
314 __func__, eqp_index, dqp_index, bdc->srr.dqp_index);
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);
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 */
327 sr_type = le32_to_cpu(sreport->offset[3]) & BD_TYPE_BITMASK;
328 dev_dbg_ratelimited(bdc->dev, "sr_type=%d\n", sr_type);
331 bdc->sr_handler[0](bdc, sreport);
335 bdc->sr_handler[1](bdc, sreport);
338 dev_warn(bdc->dev, "SR:%d not handled\n", sr_type);
340 /* Advance the srr dqp index */
341 srr_dqp_index_advc(bdc, 0);
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));
352 ret = bdc_reinit(bdc);
354 dev_err(bdc->dev, "err in bdc reinit\n");
357 spin_unlock(&bdc->lock);
363 static int bdc_udc_start(struct usb_gadget *gadget,
364 struct usb_gadget_driver *driver)
366 struct bdc *bdc = gadget_to_bdc(gadget);
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",
375 bdc->gadget_driver->driver.name);
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.
386 dev_err(bdc->dev, "%s bdc run fail\n", __func__);
389 bdc->gadget_driver = driver;
390 bdc->gadget.dev.driver = &driver->driver;
392 spin_unlock_irqrestore(&bdc->lock, flags);
397 static int bdc_udc_stop(struct usb_gadget *gadget)
399 struct bdc *bdc = gadget_to_bdc(gadget);
402 dev_dbg(bdc->dev, "%s()\n", __func__);
403 spin_lock_irqsave(&bdc->lock, flags);
405 bdc->gadget_driver = NULL;
406 bdc->gadget.dev.driver = NULL;
407 spin_unlock_irqrestore(&bdc->lock, flags);
412 static int bdc_udc_pullup(struct usb_gadget *gadget, int is_on)
414 struct bdc *bdc = gadget_to_bdc(gadget);
418 dev_dbg(bdc->dev, "%s() is_on:%d\n", __func__, is_on);
422 spin_lock_irqsave(&bdc->lock, flags);
424 bdc_softdisconn(bdc);
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.
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
438 uspc = bdc_readl(bdc->regs, BDC_USPC);
442 spin_unlock_irqrestore(&bdc->lock, flags);
447 static int bdc_udc_set_selfpowered(struct usb_gadget *gadget,
450 struct bdc *bdc = gadget_to_bdc(gadget);
453 dev_dbg(bdc->dev, "%s()\n", __func__);
454 gadget->is_selfpowered = (is_self != 0);
455 spin_lock_irqsave(&bdc->lock, flags);
457 bdc->devstatus |= 1 << USB_DEVICE_SELF_POWERED;
459 bdc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
461 spin_unlock_irqrestore(&bdc->lock, flags);
466 static int bdc_udc_wakeup(struct usb_gadget *gadget)
468 struct bdc *bdc = gadget_to_bdc(gadget);
475 "%s() bdc->devstatus=%08x\n",
476 __func__, bdc->devstatus);
478 if (!(bdc->devstatus & REMOTE_WAKE_ENABLE))
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) {
487 "can't wakeup from link state %d\n",
492 if (bdc->gadget.speed == USB_SPEED_SUPER)
493 bdc->devstatus |= REMOTE_WAKEUP_ISSUED;
495 uspc &= ~BDC_PST_MASK;
496 uspc &= (~BDC_USPSC_RW);
497 uspc |= BDC_PST(BDC_LINK_STATE_U0);
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);
504 spin_unlock_irqrestore(&bdc->lock, flags);
509 static 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,
517 /* Init the gadget interface and register the udc */
518 int bdc_udc_init(struct bdc *bdc)
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;
529 bdc->gadget.sg_supported = false;
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);
537 "failed to request irq #%d %d\n",
542 ret = bdc_init_ep(bdc);
544 dev_err(bdc->dev, "bdc init ep fail: %d\n", ret);
548 ret = usb_add_gadget_udc(bdc->dev, &bdc->gadget);
550 dev_err(bdc->dev, "failed to register udc\n");
553 usb_gadget_set_state(&bdc->gadget, USB_STATE_NOTATTACHED);
554 bdc->bdc_ep_array[1]->desc = &bdc_gadget_ep0_desc;
556 * Allocate bd list for ep0 only, ep0 will be enabled on connect
557 * status report when the speed is known
559 ret = bdc_ep_enable(bdc->bdc_ep_array[1]);
561 dev_err(bdc->dev, "fail to enable %s\n",
562 bdc->bdc_ep_array[1]->name);
565 INIT_DELAYED_WORK(&bdc->func_wake_notify, bdc_func_wake_timer);
566 /* Enable Interrupts */
567 temp = bdc_readl(bdc->regs, BDC_BDCSC);
569 bdc_writel(bdc->regs, BDC_BDCSC, temp);
572 usb_del_gadget_udc(&bdc->gadget);
579 void bdc_udc_exit(struct bdc *bdc)
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);
588 usb_del_gadget_udc(&bdc->gadget);