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