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;
156 /* TNotify wkaeup timer */
157 static void bdc_func_wake_timer(struct work_struct
*work
)
159 struct bdc
*bdc
= container_of(work
, struct bdc
, func_wake_notify
.work
);
162 dev_dbg(bdc
->dev
, "%s\n", __func__
);
163 spin_lock_irqsave(&bdc
->lock
, flags
);
165 * Check if host has started transferring on endpoints
166 * FUNC_WAKE_ISSUED is cleared when transfer has started after resume
168 if (bdc
->devstatus
& FUNC_WAKE_ISSUED
) {
169 dev_dbg(bdc
->dev
, "FUNC_WAKE_ISSUED FLAG IS STILL SET\n");
170 /* flag is still set, so again send func wake */
171 bdc_function_wake_fh(bdc
, 0);
172 schedule_delayed_work(&bdc
->func_wake_notify
,
173 msecs_to_jiffies(BDC_TNOTIFY
));
175 spin_unlock_irqrestore(&bdc
->lock
, flags
);
178 /* handler for Link state change condition */
179 static void handle_link_state_change(struct bdc
*bdc
, u32 uspc
)
183 dev_dbg(bdc
->dev
, "Link state change");
184 link_state
= BDC_PST(uspc
);
185 switch (link_state
) {
186 case BDC_LINK_STATE_U3
:
187 if ((bdc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) &&
188 bdc
->gadget_driver
->suspend
) {
189 dev_dbg(bdc
->dev
, "Entered Suspend mode\n");
190 spin_unlock(&bdc
->lock
);
191 bdc
->devstatus
|= DEVICE_SUSPENDED
;
192 bdc
->gadget_driver
->suspend(&bdc
->gadget
);
193 spin_lock(&bdc
->lock
);
196 case BDC_LINK_STATE_U0
:
197 if (bdc
->devstatus
& REMOTE_WAKEUP_ISSUED
) {
198 bdc
->devstatus
&= ~REMOTE_WAKEUP_ISSUED
;
199 if (bdc
->gadget
.speed
== USB_SPEED_SUPER
) {
200 bdc_function_wake_fh(bdc
, 0);
201 bdc
->devstatus
|= FUNC_WAKE_ISSUED
;
203 * Start a Notification timer and check if the
204 * Host transferred anything on any of the EPs,
205 * if not then send function wake again every
206 * TNotification secs until host initiates
207 * transfer to BDC, USB3 spec Table 8.13
209 schedule_delayed_work(
210 &bdc
->func_wake_notify
,
211 msecs_to_jiffies(BDC_TNOTIFY
));
212 dev_dbg(bdc
->dev
, "sched func_wake_notify\n");
217 case BDC_LINK_STATE_RESUME
:
218 dev_dbg(bdc
->dev
, "Resumed from Suspend\n");
219 if (bdc
->devstatus
& DEVICE_SUSPENDED
) {
220 bdc
->gadget_driver
->resume(&bdc
->gadget
);
221 bdc
->devstatus
&= ~DEVICE_SUSPENDED
;
225 dev_dbg(bdc
->dev
, "link state:%d\n", link_state
);
229 /* something changes on upstream port, handle it here */
230 void bdc_sr_uspc(struct bdc
*bdc
, struct bdc_sr
*sreport
)
234 bool connected
= false;
235 bool disconn
= false;
237 uspc
= bdc_readl(bdc
->regs
, BDC_USPC
);
238 dev_dbg(bdc
->dev
, "%s uspc=0x%08x\n", __func__
, uspc
);
240 /* Port connect changed */
241 if (uspc
& BDC_PCC
) {
242 /* Vbus not present, and not connected to Downstream port */
243 if ((uspc
& BDC_VBC
) && !(uspc
& BDC_VBS
) && !(uspc
& BDC_PCS
))
245 else if ((uspc
& BDC_PCS
) && !BDC_PST(uspc
))
247 clear_flags
|= BDC_PCC
;
250 /* Change in VBus and VBus is present */
251 if ((uspc
& BDC_VBC
) && (uspc
& BDC_VBS
)) {
253 dev_dbg(bdc
->dev
, "Do a softconnect\n");
254 /* Attached state, do a softconnect */
256 usb_gadget_set_state(&bdc
->gadget
, USB_STATE_POWERED
);
258 clear_flags
|= BDC_VBC
;
259 } else if ((uspc
& BDC_PRS
) || (uspc
& BDC_PRC
) || disconn
) {
260 /* Hot reset, warm reset, 2.0 bus reset or disconn */
261 dev_dbg(bdc
->dev
, "Port reset or disconn\n");
262 bdc_uspc_disconnected(bdc
, disconn
);
263 clear_flags
|= BDC_PRC
;
264 } else if ((uspc
& BDC_PSC
) && (uspc
& BDC_PCS
)) {
265 /* Change in Link state */
266 handle_link_state_change(bdc
, uspc
);
267 clear_flags
|= BDC_PSC
;
271 * In SS we might not have PRC bit set before connection, but in 2.0
272 * the PRC bit is set before connection, so moving this condition out
273 * of bus reset to handle both SS/2.0 speeds.
276 /* This is the connect event for U0/L0 */
277 dev_dbg(bdc
->dev
, "Connected\n");
278 bdc_uspc_connected(bdc
);
279 bdc
->devstatus
&= ~(DEVICE_SUSPENDED
);
281 uspc
= bdc_readl(bdc
->regs
, BDC_USPC
);
282 uspc
&= (~BDC_USPSC_RW
);
283 dev_dbg(bdc
->dev
, "uspc=%x\n", uspc
);
284 bdc_writel(bdc
->regs
, BDC_USPC
, clear_flags
);
287 /* Main interrupt handler for bdc */
288 static irqreturn_t
bdc_udc_interrupt(int irq
, void *_bdc
)
290 u32 eqp_index
, dqp_index
, sr_type
, srr_int
;
291 struct bdc_sr
*sreport
;
292 struct bdc
*bdc
= _bdc
;
296 spin_lock(&bdc
->lock
);
297 status
= bdc_readl(bdc
->regs
, BDC_BDCSC
);
298 if (!(status
& BDC_GIP
)) {
299 spin_unlock(&bdc
->lock
);
302 srr_int
= bdc_readl(bdc
->regs
, BDC_SRRINT(0));
303 /* Check if the SRR IP bit it set? */
304 if (!(srr_int
& BDC_SRR_IP
)) {
305 dev_warn(bdc
->dev
, "Global irq pending but SRR IP is 0\n");
306 spin_unlock(&bdc
->lock
);
309 eqp_index
= BDC_SRR_EPI(srr_int
);
310 dqp_index
= BDC_SRR_DPI(srr_int
);
312 "%s eqp_index=%d dqp_index=%d srr.dqp_index=%d\n\n",
313 __func__
, eqp_index
, dqp_index
, bdc
->srr
.dqp_index
);
315 /* check for ring empty condition */
316 if (eqp_index
== dqp_index
) {
317 dev_dbg(bdc
->dev
, "SRR empty?\n");
318 spin_unlock(&bdc
->lock
);
322 while (bdc
->srr
.dqp_index
!= eqp_index
) {
323 sreport
= &bdc
->srr
.sr_bds
[bdc
->srr
.dqp_index
];
324 /* sreport is read before using it */
326 sr_type
= le32_to_cpu(sreport
->offset
[3]) & BD_TYPE_BITMASK
;
327 dev_dbg_ratelimited(bdc
->dev
, "sr_type=%d\n", sr_type
);
330 bdc
->sr_handler
[0](bdc
, sreport
);
334 bdc
->sr_handler
[1](bdc
, sreport
);
337 dev_warn(bdc
->dev
, "SR:%d not handled\n", sr_type
);
339 /* Advance the srr dqp index */
340 srr_dqp_index_advc(bdc
, 0);
342 /* update the hw dequeue pointer */
343 srr_int
= bdc_readl(bdc
->regs
, BDC_SRRINT(0));
344 srr_int
&= ~BDC_SRR_DPI_MASK
;
345 srr_int
&= ~(BDC_SRR_RWS
|BDC_SRR_RST
|BDC_SRR_ISR
);
346 srr_int
|= ((bdc
->srr
.dqp_index
) << 16);
347 srr_int
|= BDC_SRR_IP
;
348 bdc_writel(bdc
->regs
, BDC_SRRINT(0), srr_int
);
349 srr_int
= bdc_readl(bdc
->regs
, BDC_SRRINT(0));
351 ret
= bdc_reinit(bdc
);
353 dev_err(bdc
->dev
, "err in bdc reinit\n");
356 spin_unlock(&bdc
->lock
);
362 static int bdc_udc_start(struct usb_gadget
*gadget
,
363 struct usb_gadget_driver
*driver
)
365 struct bdc
*bdc
= gadget_to_bdc(gadget
);
369 dev_dbg(bdc
->dev
, "%s()\n", __func__
);
370 spin_lock_irqsave(&bdc
->lock
, flags
);
371 if (bdc
->gadget_driver
) {
372 dev_err(bdc
->dev
, "%s is already bound to %s\n",
374 bdc
->gadget_driver
->driver
.name
);
379 * Run the controller from here and when BDC is connected to
380 * Host then driver will receive a USPC SR with VBUS present
381 * and then driver will do a softconnect.
385 dev_err(bdc
->dev
, "%s bdc run fail\n", __func__
);
388 bdc
->gadget_driver
= driver
;
389 bdc
->gadget
.dev
.driver
= &driver
->driver
;
391 spin_unlock_irqrestore(&bdc
->lock
, flags
);
396 static int bdc_udc_stop(struct usb_gadget
*gadget
)
398 struct bdc
*bdc
= gadget_to_bdc(gadget
);
401 dev_dbg(bdc
->dev
, "%s()\n", __func__
);
402 spin_lock_irqsave(&bdc
->lock
, flags
);
404 bdc
->gadget_driver
= NULL
;
405 bdc
->gadget
.dev
.driver
= NULL
;
406 spin_unlock_irqrestore(&bdc
->lock
, flags
);
411 static int bdc_udc_pullup(struct usb_gadget
*gadget
, int is_on
)
413 struct bdc
*bdc
= gadget_to_bdc(gadget
);
417 dev_dbg(bdc
->dev
, "%s() is_on:%d\n", __func__
, is_on
);
421 spin_lock_irqsave(&bdc
->lock
, flags
);
423 bdc_softdisconn(bdc
);
427 * For a self powered device, we need to wait till we receive
428 * a VBUS change and Vbus present event, then if pullup flag
429 * is set, then only we present the Termintation.
433 * Check if BDC is already connected to Host i.e Vbus=1,
434 * if yes, then present TERM now, this is typical for bus
437 uspc
= bdc_readl(bdc
->regs
, BDC_USPC
);
441 spin_unlock_irqrestore(&bdc
->lock
, flags
);
446 static int bdc_udc_set_selfpowered(struct usb_gadget
*gadget
,
449 struct bdc
*bdc
= gadget_to_bdc(gadget
);
452 dev_dbg(bdc
->dev
, "%s()\n", __func__
);
453 gadget
->is_selfpowered
= (is_self
!= 0);
454 spin_lock_irqsave(&bdc
->lock
, flags
);
456 bdc
->devstatus
|= 1 << USB_DEVICE_SELF_POWERED
;
458 bdc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
460 spin_unlock_irqrestore(&bdc
->lock
, flags
);
465 static int bdc_udc_wakeup(struct usb_gadget
*gadget
)
467 struct bdc
*bdc
= gadget_to_bdc(gadget
);
474 "%s() bdc->devstatus=%08x\n",
475 __func__
, bdc
->devstatus
);
477 if (!(bdc
->devstatus
& REMOTE_WAKE_ENABLE
))
480 spin_lock_irqsave(&bdc
->lock
, flags
);
481 uspc
= bdc_readl(bdc
->regs
, BDC_USPC
);
482 link_state
= BDC_PST(uspc
);
483 dev_dbg(bdc
->dev
, "link_state =%d portsc=%x", link_state
, uspc
);
484 if (link_state
!= BDC_LINK_STATE_U3
) {
486 "can't wakeup from link state %d\n",
491 if (bdc
->gadget
.speed
== USB_SPEED_SUPER
)
492 bdc
->devstatus
|= REMOTE_WAKEUP_ISSUED
;
494 uspc
&= ~BDC_PST_MASK
;
495 uspc
&= (~BDC_USPSC_RW
);
496 uspc
|= BDC_PST(BDC_LINK_STATE_U0
);
498 bdc_writel(bdc
->regs
, BDC_USPC
, uspc
);
499 uspc
= bdc_readl(bdc
->regs
, BDC_USPC
);
500 link_state
= BDC_PST(uspc
);
501 dev_dbg(bdc
->dev
, "link_state =%d portsc=%x", link_state
, uspc
);
503 spin_unlock_irqrestore(&bdc
->lock
, flags
);
508 static const struct usb_gadget_ops bdc_gadget_ops
= {
509 .wakeup
= bdc_udc_wakeup
,
510 .set_selfpowered
= bdc_udc_set_selfpowered
,
511 .pullup
= bdc_udc_pullup
,
512 .udc_start
= bdc_udc_start
,
513 .udc_stop
= bdc_udc_stop
,
516 /* Init the gadget interface and register the udc */
517 int bdc_udc_init(struct bdc
*bdc
)
522 dev_dbg(bdc
->dev
, "%s()\n", __func__
);
523 bdc
->gadget
.ops
= &bdc_gadget_ops
;
524 bdc
->gadget
.max_speed
= USB_SPEED_SUPER
;
525 bdc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
526 bdc
->gadget
.dev
.parent
= bdc
->dev
;
528 bdc
->gadget
.sg_supported
= false;
531 bdc
->gadget
.name
= BRCM_BDC_NAME
;
532 ret
= devm_request_irq(bdc
->dev
, bdc
->irq
, bdc_udc_interrupt
,
533 IRQF_SHARED
, BRCM_BDC_NAME
, bdc
);
536 "failed to request irq #%d %d\n",
541 ret
= bdc_init_ep(bdc
);
543 dev_err(bdc
->dev
, "bdc init ep fail: %d\n", ret
);
547 ret
= usb_add_gadget_udc(bdc
->dev
, &bdc
->gadget
);
549 dev_err(bdc
->dev
, "failed to register udc\n");
552 usb_gadget_set_state(&bdc
->gadget
, USB_STATE_NOTATTACHED
);
553 bdc
->bdc_ep_array
[1]->desc
= &bdc_gadget_ep0_desc
;
555 * Allocate bd list for ep0 only, ep0 will be enabled on connect
556 * status report when the speed is known
558 ret
= bdc_ep_enable(bdc
->bdc_ep_array
[1]);
560 dev_err(bdc
->dev
, "fail to enable %s\n",
561 bdc
->bdc_ep_array
[1]->name
);
564 INIT_DELAYED_WORK(&bdc
->func_wake_notify
, bdc_func_wake_timer
);
565 /* Enable Interrupts */
566 temp
= bdc_readl(bdc
->regs
, BDC_BDCSC
);
568 bdc_writel(bdc
->regs
, BDC_BDCSC
, temp
);
571 usb_del_gadget_udc(&bdc
->gadget
);
578 void bdc_udc_exit(struct bdc
*bdc
)
582 dev_dbg(bdc
->dev
, "%s()\n", __func__
);
583 spin_lock_irqsave(&bdc
->lock
, flags
);
584 bdc_ep_disable(bdc
->bdc_ep_array
[1]);
585 spin_unlock_irqrestore(&bdc
->lock
, flags
);
587 usb_del_gadget_udc(&bdc
->gadget
);