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