]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - drivers/usb/dwc3/otg.c
usb: dwc3: otg: Add support for ending HNP session
[zynq/linux.git] / drivers / usb / dwc3 / otg.c
1 /**
2  * otg.c - DesignWare USB3 DRD Controller OTG file
3  *
4  * Copyright (C) 2016 Xilinx, Inc. All rights reserved.
5  *
6  * Author:  Manish Narani <mnarani@xilinx.com>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2  of
10  * the License as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/pci.h>
22 #include <linux/slab.h>
23 #include <linux/sched/signal.h>
24 #include <linux/sched.h>
25 #include <linux/freezer.h>
26 #include <linux/kthread.h>
27 #include <linux/version.h>
28 #include <linux/sysfs.h>
29
30 #include <linux/usb.h>
31 #include <linux/usb/hcd.h>
32 #include <linux/usb/gadget.h>
33 #include <linux/usb/otg.h>
34 #include <linux/usb/phy.h>
35
36 #include <../drivers/usb/host/xhci.h>
37 #include "platform_data.h"
38 #include "core.h"
39 #include "gadget.h"
40 #include "io.h"
41 #include "otg.h"
42
43 #include <linux/ulpi/regs.h>
44 #include <linux/ulpi/driver.h>
45 #include "debug.h"
46
47 /* Print the hardware registers' value for debugging purpose */
48 static void print_debug_regs(struct dwc3_otg *otg)
49 {
50         u32 gctl = otg_read(otg, DWC3_GCTL);
51         u32 gsts = otg_read(otg, DWC3_GSTS);
52         u32 gdbgltssm = otg_read(otg, DWC3_GDBGLTSSM);
53         u32 gusb2phycfg0 = otg_read(otg, DWC3_GUSB2PHYCFG(0));
54         u32 gusb3pipectl0 = otg_read(otg, DWC3_GUSB3PIPECTL(0));
55         u32 dcfg = otg_read(otg, DWC3_DCFG);
56         u32 dctl = otg_read(otg, DWC3_DCTL);
57         u32 dsts = otg_read(otg, DWC3_DSTS);
58         u32 ocfg = otg_read(otg, OCFG);
59         u32 octl = otg_read(otg, OCTL);
60         u32 oevt = otg_read(otg, OEVT);
61         u32 oevten = otg_read(otg, OEVTEN);
62         u32 osts = otg_read(otg, OSTS);
63
64         otg_info(otg, "gctl = %08x\n", gctl);
65         otg_info(otg, "gsts = %08x\n", gsts);
66         otg_info(otg, "gdbgltssm = %08x\n", gdbgltssm);
67         otg_info(otg, "gusb2phycfg0 = %08x\n", gusb2phycfg0);
68         otg_info(otg, "gusb3pipectl0 = %08x\n", gusb3pipectl0);
69         otg_info(otg, "dcfg = %08x\n", dcfg);
70         otg_info(otg, "dctl = %08x\n", dctl);
71         otg_info(otg, "dsts = %08x\n", dsts);
72         otg_info(otg, "ocfg = %08x\n", ocfg);
73         otg_info(otg, "octl = %08x\n", octl);
74         otg_info(otg, "oevt = %08x\n", oevt);
75         otg_info(otg, "oevten = %08x\n", oevten);
76         otg_info(otg, "osts = %08x\n", osts);
77 }
78
79 /* Check whether the hardware supports HNP or not */
80 static int hnp_capable(struct dwc3_otg *otg)
81 {
82         if (otg->hwparams6 & GHWPARAMS6_HNP_SUPPORT_ENABLED)
83                 return 1;
84         return 0;
85 }
86
87 /* Check whether the hardware supports SRP or not */
88 static int srp_capable(struct dwc3_otg *otg)
89 {
90         if (otg->hwparams6 & GHWPARAMS6_SRP_SUPPORT_ENABLED)
91                 return 1;
92         return 0;
93 }
94
95 /* Wakeup main thread to execute the OTG flow after an event */
96 static void wakeup_main_thread(struct dwc3_otg *otg)
97 {
98         if (!otg->main_thread)
99                 return;
100
101         otg_vdbg(otg, "\n");
102         /* Tell the main thread that something has happened */
103         otg->main_wakeup_needed = 1;
104         wake_up_interruptible(&otg->main_wq);
105 }
106
107 /* Sleep main thread for 'msecs' to wait for an event to occur */
108 static int sleep_main_thread_timeout(struct dwc3_otg *otg, int msecs)
109 {
110         signed long jiffies;
111         int rc = msecs;
112
113         if (signal_pending(current)) {
114                 otg_dbg(otg, "Main thread signal pending\n");
115                 rc = -EINTR;
116                 goto done;
117         }
118         if (otg->main_wakeup_needed) {
119                 otg_dbg(otg, "Main thread wakeup needed\n");
120                 rc = msecs;
121                 goto done;
122         }
123
124         jiffies = msecs_to_jiffies(msecs);
125         rc = wait_event_freezable_timeout(otg->main_wq,
126                                           otg->main_wakeup_needed,
127                                           jiffies);
128
129         if (rc > 0)
130                 rc = jiffies_to_msecs(rc);
131
132 done:
133         otg->main_wakeup_needed = 0;
134         return rc;
135 }
136
137 /* Sleep main thread to wait for an event to occur */
138 static int sleep_main_thread(struct dwc3_otg *otg)
139 {
140         int rc;
141
142         do {
143                 rc = sleep_main_thread_timeout(otg, 5000);
144         } while (rc == 0);
145
146         return rc;
147 }
148
149 static void get_events(struct dwc3_otg *otg, u32 *otg_events, u32 *user_events)
150 {
151         unsigned long flags;
152
153         spin_lock_irqsave(&otg->lock, flags);
154
155         if (otg_events)
156                 *otg_events = otg->otg_events;
157
158         if (user_events)
159                 *user_events = otg->user_events;
160
161         spin_unlock_irqrestore(&otg->lock, flags);
162 }
163
164 static void get_and_clear_events(struct dwc3_otg *otg, u32 *otg_events,
165                 u32 *user_events)
166 {
167         unsigned long flags;
168
169         spin_lock_irqsave(&otg->lock, flags);
170
171         if (otg_events)
172                 *otg_events = otg->otg_events;
173
174         if (user_events)
175                 *user_events = otg->user_events;
176
177         otg->otg_events = 0;
178         otg->user_events = 0;
179
180         spin_unlock_irqrestore(&otg->lock, flags);
181 }
182
183 static int check_event(struct dwc3_otg *otg, u32 otg_mask, u32 user_mask)
184 {
185         u32 otg_events;
186         u32 user_events;
187
188         get_events(otg, &otg_events, &user_events);
189         if ((otg_events & otg_mask) || (user_events & user_mask)) {
190                 otg_dbg(otg, "Event occurred: otg_events=%x, otg_mask=%x, \
191                                 user_events=%x, user_mask=%x\n", otg_events,
192                                 otg_mask, user_events, user_mask);
193                 return 1;
194         }
195
196         return 0;
197 }
198
199 static int sleep_until_event(struct dwc3_otg *otg, u32 otg_mask, u32 user_mask,
200                 u32 *otg_events, u32 *user_events, int timeout)
201 {
202         int rc;
203
204         /* Enable the events */
205         if (otg_mask)
206                 otg_write(otg, OEVTEN, otg_mask);
207
208         /* Wait until it occurs, or timeout, or interrupt. */
209         if (timeout) {
210                 otg_vdbg(otg, "Waiting for event (timeout=%d)...\n", timeout);
211                 rc = sleep_main_thread_until_condition_timeout(otg,
212                         check_event(otg, otg_mask, user_mask), timeout);
213         } else {
214                 otg_vdbg(otg, "Waiting for event (no timeout)...\n");
215                 rc = sleep_main_thread_until_condition(otg,
216                         check_event(otg, otg_mask, user_mask));
217         }
218
219         /* Disable the events */
220         otg_write(otg, OEVTEN, 0);
221
222         otg_vdbg(otg, "Woke up rc=%d\n", rc);
223         if (rc >= 0)
224                 get_and_clear_events(otg, otg_events, user_events);
225
226         return rc;
227 }
228
229 static void set_capabilities(struct dwc3_otg *otg)
230 {
231         u32 ocfg = 0;
232
233         otg_dbg(otg, "\n");
234         if (srp_capable(otg))
235                 ocfg |= OCFG_SRP_CAP;
236
237         if (hnp_capable(otg))
238                 ocfg |= OCFG_HNP_CAP;
239
240         otg_write(otg, OCFG, ocfg);
241
242         otg_dbg(otg, "Enabled SRP and HNP capabilities in OCFG\n");
243 }
244
245 static int otg3_handshake(struct dwc3_otg *otg, u32 reg, u32 mask, u32 done,
246                 u32 msec)
247 {
248         u32 result;
249         u32 usec = msec * 1000;
250
251         otg_vdbg(otg, "reg=%08x, mask=%08x, value=%08x\n", reg, mask, done);
252         do {
253                 result = otg_read(otg, reg);
254                 if ((result & mask) == done)
255                         return 1;
256                 udelay(1);
257                 usec -= 1;
258         } while (usec > 0);
259
260         return 0;
261 }
262
263 static int reset_port(struct dwc3_otg *otg)
264 {
265         otg_dbg(otg, "\n");
266         if (!otg->otg.host)
267                 return -ENODEV;
268         return usb_bus_start_enum(otg->otg.host, 1);
269 }
270
271 static int set_peri_mode(struct dwc3_otg *otg, int mode)
272 {
273         u32 octl;
274
275         /* Set peri_mode */
276         octl = otg_read(otg, OCTL);
277         if (mode)
278                 octl |= OCTL_PERI_MODE;
279         else
280                 octl &= ~OCTL_PERI_MODE;
281
282         otg_write(otg, OCTL, octl);
283         otg_dbg(otg, "set OCTL PERI_MODE = %d in OCTL\n", mode);
284
285         if (mode)
286                 return otg3_handshake(otg, OSTS, OSTS_PERIP_MODE,
287                                 OSTS_PERIP_MODE, 100);
288         else
289                 return otg3_handshake(otg, OSTS, OSTS_PERIP_MODE, 0, 100);
290
291         msleep(20);
292 }
293
294 static int start_host(struct dwc3_otg *otg)
295 {
296         int ret = -ENODEV;
297         int flg;
298         u32 octl;
299         u32 osts;
300         u32 ocfg;
301         u32 dctl;
302         struct usb_hcd *hcd;
303         struct xhci_hcd *xhci;
304
305         otg_dbg(otg, "\n");
306
307         if (!otg->otg.host)
308                 return -ENODEV;
309
310         /*
311          * Prevent the host USBCMD.HCRST from resetting OTG core by setting
312          * OCFG.OTGSftRstMsk
313          */
314         ocfg = otg_read(otg, OCFG);
315         ocfg |= DWC3_OCFG_SFTRSTMASK;
316         otg_write(otg, OCFG, ocfg);
317
318         dctl = otg_read(otg, DCTL);
319         if (dctl & DWC3_DCTL_RUN_STOP) {
320                 otg_dbg(otg, "Disabling the RUN/STOP bit\n");
321                 dctl &= ~DWC3_DCTL_RUN_STOP;
322                 otg_write(otg, DCTL, dctl);
323         }
324
325         if (!set_peri_mode(otg, PERI_MODE_HOST)) {
326                 otg_err(otg, "Failed to start host\n");
327                 return -EINVAL;
328         }
329
330         hcd = container_of(otg->otg.host, struct usb_hcd, self);
331         xhci = hcd_to_xhci(hcd);
332         otg_dbg(otg, "hcd=%p xhci=%p\n", hcd, xhci);
333
334         if (otg->host_started) {
335                 otg_info(otg, "Host already started\n");
336                 goto skip;
337         }
338
339         /* Start host driver */
340
341         *(struct xhci_hcd **)hcd->hcd_priv = xhci;
342         ret = usb_add_hcd(hcd, otg->hcd_irq, IRQF_SHARED);
343         if (ret) {
344                 otg_err(otg, "%s: failed to start primary hcd, ret=%d\n",
345                         __func__, ret);
346                 return ret;
347         }
348
349         *(struct xhci_hcd **)xhci->shared_hcd->hcd_priv = xhci;
350         if (xhci->shared_hcd) {
351                 ret = usb_add_hcd(xhci->shared_hcd, otg->hcd_irq, IRQF_SHARED);
352                 if (ret) {
353                         otg_err(otg,
354                                 "%s: failed to start secondary hcd, ret=%d\n",
355                                 __func__, ret);
356                         usb_remove_hcd(hcd);
357                         return ret;
358                 }
359         }
360
361         otg->host_started = 1;
362 skip:
363         hcd->self.otg_port = 1;
364         if (xhci->shared_hcd)
365                 xhci->shared_hcd->self.otg_port = 1;
366
367         set_capabilities(otg);
368
369         /* Power the port only for A-host */
370         if (otg->otg.state == OTG_STATE_A_WAIT_VRISE) {
371                 /* Spin on xhciPrtPwr bit until it becomes 1 */
372                 osts = otg_read(otg, OSTS);
373                 flg = otg3_handshake(otg, OSTS,
374                                 OSTS_XHCI_PRT_PWR,
375                                 OSTS_XHCI_PRT_PWR,
376                                 1000);
377                 if (flg) {
378                         otg_dbg(otg, "Port is powered by xhci-hcd\n");
379                         /* Set port power control bit */
380                         octl = otg_read(otg, OCTL);
381                         octl |= OCTL_PRT_PWR_CTL;
382                         otg_write(otg, OCTL, octl);
383                 } else {
384                         otg_dbg(otg, "Port is not powered by xhci-hcd\n");
385                 }
386         }
387
388         return ret;
389 }
390
391 static int stop_host(struct dwc3_otg *otg)
392 {
393         struct usb_hcd *hcd;
394         struct xhci_hcd *xhci;
395
396         otg_dbg(otg, "\n");
397
398         if (!otg->host_started) {
399                 otg_info(otg, "Host already stopped\n");
400                 return 1;
401         }
402
403         if (!otg->otg.host)
404                 return -ENODEV;
405
406         otg_dbg(otg, "%s: turn off host %s\n",
407                 __func__, otg->otg.host->bus_name);
408
409         if (work_pending(&otg->hp_work.work)) {
410                 while (!cancel_delayed_work(&otg->hp_work))
411                         msleep(20);
412         }
413
414         hcd = container_of(otg->otg.host, struct usb_hcd, self);
415         xhci = hcd_to_xhci(hcd);
416
417         if (xhci->shared_hcd)
418                 usb_remove_hcd(xhci->shared_hcd);
419         usb_remove_hcd(hcd);
420
421         otg->host_started = 0;
422         otg->dev_enum = 0;
423         return 0;
424 }
425
426 int dwc3_otg_host_release(struct usb_hcd *hcd)
427 {
428         struct usb_bus *bus;
429         struct usb_device *rh;
430         struct usb_device *udev;
431
432         if (!hcd)
433                 return -EINVAL;
434
435         bus = &hcd->self;
436         if (!bus->otg_port)
437                 return 0;
438
439         rh = bus->root_hub;
440         udev = usb_hub_find_child(rh, bus->otg_port);
441         if (!udev)
442                 return 0;
443
444         if (udev->config && udev->parent == udev->bus->root_hub) {
445                 struct usb_otg20_descriptor *desc;
446
447                 if (__usb_get_extra_descriptor(udev->rawdescriptors[0],
448                                 le16_to_cpu(udev->config[0].desc.wTotalLength),
449                                 USB_DT_OTG, (void **) &desc) == 0) {
450                         int err;
451
452                         dev_info(&udev->dev, "found OTG descriptor\n");
453                         if ((desc->bcdOTG >= 0x0200) &&
454                             (udev->speed == USB_SPEED_HIGH)) {
455                                 err = usb_control_msg(udev,
456                                                 usb_sndctrlpipe(udev, 0),
457                                                 USB_REQ_SET_FEATURE, 0,
458                                                 USB_DEVICE_TEST_MODE,
459                                                 7 << 8,
460                                                 NULL, 0, USB_CTRL_SET_TIMEOUT);
461                                 if (err < 0) {
462                                         dev_info(&udev->dev,
463                                                 "can't initiate HNP from host: %d\n",
464                                                 err);
465                                         return -1;
466                                 }
467                         }
468                 } else {
469                         dev_info(&udev->dev, "didn't find OTG descriptor\n");
470                 }
471         } else {
472                 dev_info(&udev->dev,
473                          "udev->config NULL or udev->parent != udev->bus->root_hub\n");
474         }
475
476         return 0;
477 }
478
479 /* Sends the host release set feature request */
480 static void host_release(struct dwc3_otg *otg)
481 {
482         struct usb_hcd *hcd;
483         struct xhci_hcd *xhci;
484
485         otg_dbg(otg, "\n");
486         if (!otg->otg.host)
487                 return;
488         hcd = container_of(otg->otg.host, struct usb_hcd, self);
489         xhci = hcd_to_xhci(hcd);
490         dwc3_otg_host_release(hcd);
491         if (xhci->shared_hcd)
492                 dwc3_otg_host_release(xhci->shared_hcd);
493 }
494
495 static void dwc3_otg_setup_event_buffers(struct dwc3_otg *otg)
496 {
497         if (dwc3_readl(otg->dwc->regs, DWC3_GEVNTADRLO(0)) == 0x0) {
498
499                 otg_dbg(otg, "setting up event buffers\n");
500                 dwc3_event_buffers_setup(otg->dwc);
501         }
502
503 }
504
505 static void start_peripheral(struct dwc3_otg *otg)
506 {
507         struct usb_gadget *gadget = otg->otg.gadget;
508         struct dwc3 *dwc = otg->dwc;
509         u32 ocfg;
510
511         otg_dbg(otg, "\n");
512         if (!gadget)
513                 return;
514
515         /*
516          * Prevent the gadget DCTL.CSFTRST from resetting OTG core by setting
517          * OCFG.OTGSftRstMsk
518          */
519         ocfg = otg_read(otg, OCFG);
520         ocfg |= DWC3_OCFG_SFTRSTMASK;
521         otg_write(otg, OCFG, ocfg);
522
523         if (!set_peri_mode(otg, PERI_MODE_PERIPHERAL))
524                 otg_err(otg, "Failed to set peripheral mode\n");
525
526         if (otg->peripheral_started) {
527                 otg_info(otg, "Peripheral already started\n");
528                 return;
529         }
530
531         set_capabilities(otg);
532
533         dwc3_otg_setup_event_buffers(otg);
534
535         if (dwc->gadget_driver) {
536                 struct dwc3_ep          *dep;
537                 int                     ret;
538
539                 spin_lock(&otg->lock);
540                 dep = dwc->eps[0];
541
542                 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
543                 if (ret)
544                         goto err0;
545
546                 dep = dwc->eps[1];
547
548                 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
549                 if (ret)
550                         goto err1;
551
552                 otg_dbg(otg, "enabled ep in gadget driver\n");
553                 /* begin to receive SETUP packets */
554                 dwc->ep0state = EP0_SETUP_PHASE;
555                 dwc3_ep0_out_start(dwc);
556
557                 otg_dbg(otg, "enabled irq\n");
558                 dwc3_gadget_enable_irq(dwc);
559
560                 otg_write(otg, DCTL, otg_read(otg, DCTL) | DCTL_RUN_STOP);
561                 otg_dbg(otg, "Setting DCTL_RUN_STOP to 1 in DCTL\n");
562                 spin_unlock(&otg->lock);
563         }
564
565         gadget->b_hnp_enable = 0;
566         gadget->host_request_flag = 0;
567
568         otg->peripheral_started = 1;
569
570         /*
571          * During HNP the bus shouldn't be idle for more than 155 ms, so
572          * give enough time for the host to load the stack before start
573          * triggerring events
574          */
575         msleep(500);
576
577         return;
578 err1:
579                 __dwc3_gadget_ep_disable(dwc->eps[0]);
580
581 err0:
582                 return;
583 }
584
585 static void stop_peripheral(struct dwc3_otg *otg)
586 {
587         struct usb_gadget *gadget = otg->otg.gadget;
588         struct dwc3 *dwc = otg->dwc;
589
590         otg_dbg(otg, "\n");
591
592         if (!otg->peripheral_started) {
593                 otg_info(otg, "Peripheral already stopped\n");
594                 return;
595         }
596
597         if (!gadget)
598                 return;
599
600         otg_dbg(otg, "disabled ep in gadget driver\n");
601         spin_lock(&otg->lock);
602
603         dwc3_gadget_disable_irq(dwc);
604         __dwc3_gadget_ep_disable(dwc->eps[0]);
605         __dwc3_gadget_ep_disable(dwc->eps[1]);
606
607         spin_unlock(&otg->lock);
608
609         otg->peripheral_started = 0;
610         msleep(20);
611 }
612
613 static void set_b_host(struct dwc3_otg *otg, int val)
614 {
615         otg->otg.host->is_b_host = val;
616 }
617
618 static enum usb_otg_state do_b_idle(struct dwc3_otg *otg);
619
620 static int init_b_device(struct dwc3_otg *otg)
621 {
622         otg_dbg(otg, "\n");
623         set_capabilities(otg);
624
625         if (!set_peri_mode(otg, PERI_MODE_PERIPHERAL))
626                 otg_err(otg, "Failed to start peripheral\n");
627
628         return do_b_idle(otg);
629 }
630
631 static int init_a_device(struct dwc3_otg *otg)
632 {
633         otg_write(otg, OCFG, 0);
634         otg_write(otg, OCTL, 0);
635
636         otg_dbg(otg, "Write 0 to OCFG and OCTL\n");
637         return OTG_STATE_A_IDLE;
638 }
639
640 static enum usb_otg_state do_connector_id_status(struct dwc3_otg *otg)
641 {
642         enum usb_otg_state state;
643         u32 osts;
644
645         otg_dbg(otg, "\n");
646
647         otg_write(otg, OCFG, 0);
648         otg_write(otg, OEVTEN, 0);
649         otg_write(otg, OEVT, 0xffffffff);
650         otg_write(otg, OEVTEN, OEVT_CONN_ID_STS_CHNG_EVNT);
651
652         msleep(60);
653
654         osts = otg_read(otg, OSTS);
655         if (!(osts & OSTS_CONN_ID_STS)) {
656                 otg_dbg(otg, "Connector ID is A\n");
657                 state = init_a_device(otg);
658         } else {
659                 otg_dbg(otg, "Connector ID is B\n");
660                 stop_host(otg);
661                 state = init_b_device(otg);
662         }
663
664         /* TODO: This is a workaround for latest hibernation-enabled bitfiles
665          * which have problems before initializing SRP.
666          */
667         msleep(50);
668
669         return state;
670 }
671
672 static void reset_hw(struct dwc3_otg *otg)
673 {
674         u32 temp;
675
676         otg_dbg(otg, "\n");
677
678         otg_write(otg, OEVTEN, 0);
679         temp = otg_read(otg, OCTL);
680         temp &= OCTL_PERI_MODE;
681         otg_write(otg, OCTL, temp);
682         temp = otg_read(otg, GCTL);
683         temp |= GCTL_PRT_CAP_DIR_OTG << GCTL_PRT_CAP_DIR_SHIFT;
684         otg_write(otg, GCTL, temp);
685 }
686
687 #define SRP_TIMEOUT                     6000
688
689 static void start_srp(struct dwc3_otg *otg)
690 {
691         u32 octl;
692
693         octl = otg_read(otg, OCTL);
694         octl |= OCTL_SES_REQ;
695         otg_write(otg, OCTL, octl);
696         otg_dbg(otg, "set OCTL_SES_REQ in OCTL\n");
697 }
698
699 static void start_b_hnp(struct dwc3_otg *otg)
700 {
701         u32 octl;
702
703         octl = otg_read(otg, OCTL);
704         octl |= OCTL_HNP_REQ | OCTL_DEV_SET_HNP_EN;
705         otg_write(otg, OCTL, octl);
706         otg_dbg(otg, "set (OCTL_HNP_REQ | OCTL_DEV_SET_HNP_EN) in OCTL\n");
707 }
708
709 static void stop_b_hnp(struct dwc3_otg *otg)
710 {
711         u32 octl;
712
713         octl = otg_read(otg, OCTL);
714         octl &= ~(OCTL_HNP_REQ | OCTL_DEV_SET_HNP_EN);
715         otg_write(otg, OCTL, octl);
716         otg_dbg(otg, "Clear ~(OCTL_HNP_REQ | OCTL_DEV_SET_HNP_EN) in OCTL\n");
717 }
718
719 static void start_a_hnp(struct dwc3_otg *otg)
720 {
721         u32 octl;
722
723         octl = otg_read(otg, OCTL);
724         octl |= OCTL_HST_SET_HNP_EN;
725         otg_write(otg, OCTL, octl);
726         otg_dbg(otg, "set OCTL_HST_SET_HNP_EN in OCTL\n");
727 }
728
729 static void stop_a_hnp(struct dwc3_otg *otg)
730 {
731         u32 octl;
732
733         octl = otg_read(otg, OCTL);
734         octl &= ~OCTL_HST_SET_HNP_EN;
735         otg_write(otg, OCTL, octl);
736         otg_dbg(otg, "clear OCTL_HST_SET_HNP_EN in OCTL\n");
737 }
738
739 static enum usb_otg_state do_a_hnp_init(struct dwc3_otg *otg)
740 {
741         int rc;
742         u32 otg_mask;
743         u32 otg_events = 0;
744
745         otg_dbg(otg, "");
746         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
747                 OEVT_A_DEV_HNP_CHNG_EVNT;
748
749         start_a_hnp(otg);
750         rc = 3000;
751
752 again:
753         rc = sleep_until_event(otg,
754                         otg_mask, 0,
755                         &otg_events, NULL, rc);
756         stop_a_hnp(otg);
757         if (rc < 0)
758                 return OTG_STATE_UNDEFINED;
759
760         /* Higher priority first */
761         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
762                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
763                 return OTG_STATE_UNDEFINED;
764
765         } else if (otg_events & OEVT_A_DEV_HNP_CHNG_EVNT) {
766                 otg_dbg(otg, "OEVT_A_DEV_HNP_CHNG_EVNT\n");
767                 if (otg_events & OEVT_HST_NEG_SCS) {
768                         otg_dbg(otg, "A-HNP Success\n");
769                         return OTG_STATE_A_PERIPHERAL;
770
771                 } else {
772                         otg_dbg(otg, "A-HNP Failed\n");
773                         return OTG_STATE_A_WAIT_VFALL;
774                 }
775
776         } else if (rc == 0) {
777                 otg_dbg(otg, "A-HNP Failed (Timed out)\n");
778                 return OTG_STATE_A_WAIT_VFALL;
779
780         } else {
781                 goto again;
782         }
783
784         /* Invalid state */
785         return OTG_STATE_UNDEFINED;
786 }
787
788 static enum usb_otg_state do_a_host(struct dwc3_otg *otg)
789 {
790         int rc;
791         u32 otg_mask;
792         u32 user_mask;
793         u32 otg_events = 0;
794         u32 user_events = 0;
795
796         otg_dbg(otg, "");
797
798         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
799                 OEVT_A_DEV_SESS_END_DET_EVNT;
800         user_mask = USER_SRP_EVENT |
801                 USER_HNP_EVENT;
802
803         rc = sleep_until_event(otg,
804                         otg_mask, user_mask,
805                         &otg_events, &user_events, 0);
806         if (rc < 0)
807                 return OTG_STATE_UNDEFINED;
808
809         /* Higher priority first */
810         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
811                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
812                 return OTG_STATE_UNDEFINED;
813
814         } else if (otg_events & OEVT_A_DEV_SESS_END_DET_EVNT) {
815                 otg_dbg(otg, "OEVT_A_DEV_SESS_END_DET_EVNT\n");
816                 return OTG_STATE_A_WAIT_VFALL;
817
818         } else if (user_events & USER_HNP_EVENT) {
819                 otg_dbg(otg, "USER_HNP_EVENT\n");
820                 return OTG_STATE_A_SUSPEND;
821         }
822
823         /* Invalid state */
824         return OTG_STATE_UNDEFINED;
825 }
826
827 #define A_WAIT_VFALL_TIMEOUT 1000
828
829 static enum usb_otg_state do_a_wait_vfall(struct dwc3_otg *otg)
830 {
831         int rc;
832         u32 otg_mask;
833         u32 otg_events = 0;
834
835         otg_dbg(otg, "");
836
837         otg_mask = OEVT_A_DEV_IDLE_EVNT;
838
839         rc = A_WAIT_VFALL_TIMEOUT;
840         rc = sleep_until_event(otg,
841                         otg_mask, 0,
842                         &otg_events, NULL, rc);
843         if (rc < 0)
844                 return OTG_STATE_UNDEFINED;
845
846         if (otg_events & OEVT_A_DEV_IDLE_EVNT) {
847                 otg_dbg(otg, "OEVT_A_DEV_IDLE_EVNT\n");
848                 return OTG_STATE_A_IDLE;
849
850         } else if (rc == 0) {
851                 otg_dbg(otg, "A_WAIT_VFALL_TIMEOUT\n");
852                 return OTG_STATE_A_IDLE;
853         }
854
855         /* Invalid state */
856         return OTG_STATE_UNDEFINED;
857
858 }
859
860 #define A_WAIT_BCON_TIMEOUT 1000
861
862 static enum usb_otg_state do_a_wait_bconn(struct dwc3_otg *otg)
863 {
864         int rc;
865         u32 otg_mask;
866         u32 otg_events = 0;
867
868         otg_dbg(otg, "");
869
870         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
871                 OEVT_A_DEV_SESS_END_DET_EVNT |
872                 OEVT_A_DEV_HOST_EVNT;
873
874         rc = A_WAIT_BCON_TIMEOUT;
875         rc = sleep_until_event(otg,
876                         otg_mask, 0,
877                         &otg_events, NULL, rc);
878         if (rc < 0)
879                 return OTG_STATE_UNDEFINED;
880
881         /* Higher priority first */
882         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
883                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
884                 return OTG_STATE_UNDEFINED;
885
886         } else if (otg_events & OEVT_A_DEV_SESS_END_DET_EVNT) {
887                 otg_dbg(otg, "OEVT_A_DEV_SESS_END_DET_EVNT\n");
888                 return OTG_STATE_A_WAIT_VFALL;
889
890         } else if (otg_events & OEVT_A_DEV_HOST_EVNT) {
891                 otg_dbg(otg, "OEVT_A_DEV_HOST_EVNT\n");
892                 return OTG_STATE_A_HOST;
893
894         } else if (rc == 0) {
895                 if (otg_read(otg, OCTL) & OCTL_PRT_PWR_CTL)
896                         return OTG_STATE_A_HOST;
897                 else
898                         return OTG_STATE_A_WAIT_VFALL;
899         }
900
901         /* Invalid state */
902         return OTG_STATE_UNDEFINED;
903 }
904
905 #define A_WAIT_VRISE_TIMEOUT 100
906
907 static enum usb_otg_state do_a_wait_vrise(struct dwc3_otg *otg)
908 {
909         int rc;
910         u32 otg_mask;
911         u32 otg_events = 0;
912         struct usb_hcd *hcd;
913         struct xhci_hcd *xhci;
914
915         otg_dbg(otg, "");
916         set_b_host(otg, 0);
917         start_host(otg);
918         hcd = container_of(otg->otg.host, struct usb_hcd, self);
919         xhci = hcd_to_xhci(hcd);
920         usb_kick_hub_wq(hcd->self.root_hub);
921         if (xhci->shared_hcd)
922                 usb_kick_hub_wq(xhci->shared_hcd->self.root_hub);
923
924         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
925                 OEVT_A_DEV_SESS_END_DET_EVNT;
926
927         rc = A_WAIT_VRISE_TIMEOUT;
928
929         rc = sleep_until_event(otg,
930                         otg_mask, 0,
931                         &otg_events, NULL, rc);
932         if (rc < 0)
933                 return OTG_STATE_UNDEFINED;
934
935         /* Higher priority first */
936         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
937                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
938                 return OTG_STATE_UNDEFINED;
939
940         } else if (otg_events & OEVT_A_DEV_SESS_END_DET_EVNT) {
941                 otg_dbg(otg, "OEVT_A_DEV_SESS_END_DET_EVNT\n");
942                 return OTG_STATE_A_WAIT_VFALL;
943
944         } else if (rc == 0) {
945                 if (otg_read(otg, OCTL) & OCTL_PRT_PWR_CTL)
946                         return OTG_STATE_A_WAIT_BCON;
947                 else
948                         return OTG_STATE_A_WAIT_VFALL;
949         }
950
951         /* Invalid state */
952         return OTG_STATE_UNDEFINED;
953 }
954
955 static enum usb_otg_state do_a_idle(struct dwc3_otg *otg)
956 {
957         int rc;
958         u32 otg_mask;
959         u32 user_mask;
960         u32 otg_events = 0;
961         u32 user_events = 0;
962
963         otg_dbg(otg, "");
964
965         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT | OEVT_A_DEV_SRP_DET_EVNT;
966         user_mask = USER_SRP_EVENT;
967
968         rc = sleep_until_event(otg,
969                         otg_mask, user_mask,
970                         &otg_events, &user_events,
971                         0);
972
973         if (rc < 0)
974                 return OTG_STATE_UNDEFINED;
975
976         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
977                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
978                 return OTG_STATE_UNDEFINED;
979         } else if (otg_events & OEVT_A_DEV_SRP_DET_EVNT) {
980                 otg_dbg(otg, "OEVT_A_DEV_SRP_DET_EVNT\n");
981                 return OTG_STATE_A_WAIT_VRISE;
982         } else if (user_events & USER_SRP_EVENT) {
983                 otg_dbg(otg, "User initiated VBUS\n");
984                 return OTG_STATE_A_WAIT_VRISE;
985         }
986
987         return OTG_STATE_UNDEFINED;
988 }
989
990 static enum usb_otg_state do_a_peripheral(struct dwc3_otg *otg)
991 {
992         int rc;
993         u32 otg_mask;
994         u32 user_mask;
995         u32 otg_events = 0;
996         u32 user_events = 0;
997
998         otg_dbg(otg, "");
999         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1000                 OEVT_A_DEV_SESS_END_DET_EVNT |
1001                 OEVT_A_DEV_B_DEV_HOST_END_EVNT;
1002         user_mask = USER_HNP_END_SESSION;
1003
1004         rc = sleep_until_event(otg,
1005                         otg_mask, user_mask,
1006                         &otg_events, &user_events, 0);
1007         if (rc < 0)
1008                 return OTG_STATE_UNDEFINED;
1009
1010         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1011                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1012                 return OTG_STATE_UNDEFINED;
1013
1014         } else if (otg_events & OEVT_A_DEV_SESS_END_DET_EVNT) {
1015                 otg_dbg(otg, "OEVT_A_DEV_SESS_END_DET_EVNT\n");
1016                 return OTG_STATE_A_WAIT_VFALL;
1017
1018         } else if (otg_events & OEVT_A_DEV_B_DEV_HOST_END_EVNT) {
1019                 otg_dbg(otg, "OEVT_A_DEV_B_DEV_HOST_END_EVNT\n");
1020                 return OTG_STATE_A_WAIT_VRISE;
1021         } else if (user_events & USER_HNP_END_SESSION) {
1022                 otg_dbg(otg, "USER_HNP_END_SESSION\n");
1023                 return OTG_STATE_A_WAIT_VRISE;
1024         }
1025
1026         return OTG_STATE_UNDEFINED;
1027 }
1028
1029 #define HNP_TIMEOUT     4000
1030
1031 static enum usb_otg_state do_b_hnp_init(struct dwc3_otg *otg)
1032 {
1033         int rc;
1034         u32 otg_mask;
1035         u32 events = 0;
1036
1037         otg_dbg(otg, "");
1038         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1039                 OEVT_B_DEV_HNP_CHNG_EVNT |
1040                 OEVT_B_DEV_VBUS_CHNG_EVNT;
1041
1042         start_b_hnp(otg);
1043         rc = HNP_TIMEOUT;
1044
1045 again:
1046         rc = sleep_until_event(otg,
1047                         otg_mask, 0,
1048                         &events, NULL, rc);
1049         stop_b_hnp(otg);
1050
1051         if (rc < 0)
1052                 return OTG_STATE_UNDEFINED;
1053
1054         if (events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1055                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1056                 return OTG_STATE_UNDEFINED;
1057         } else if (events & OEVT_B_DEV_VBUS_CHNG_EVNT) {
1058                 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1059                 return OTG_STATE_B_IDLE;
1060         } else if (events & OEVT_B_DEV_HNP_CHNG_EVNT) {
1061                 otg_dbg(otg, "OEVT_B_DEV_HNP_CHNG_EVNT\n");
1062                 if (events & OEVT_HST_NEG_SCS) {
1063                         otg_dbg(otg, "B-HNP Success\n");
1064                         return OTG_STATE_B_WAIT_ACON;
1065
1066                 } else {
1067                         otg_err(otg, "B-HNP Failed\n");
1068                         return OTG_STATE_B_PERIPHERAL;
1069                 }
1070         } else if (rc == 0) {
1071                 /* Timeout */
1072                 otg_err(otg, "HNP timed out!\n");
1073                 return OTG_STATE_B_PERIPHERAL;
1074
1075         } else {
1076                 goto again;
1077         }
1078
1079         return OTG_STATE_UNDEFINED;
1080 }
1081
1082 static enum usb_otg_state do_b_peripheral(struct dwc3_otg *otg)
1083 {
1084         int rc;
1085         u32 otg_mask;
1086         u32 user_mask;
1087         u32 otg_events = 0;
1088         u32 user_events = 0;
1089
1090         otg_dbg(otg, "");
1091         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT | OEVT_B_DEV_VBUS_CHNG_EVNT;
1092         user_mask = USER_HNP_EVENT | USER_END_SESSION |
1093                 USER_SRP_EVENT | INITIAL_SRP;
1094
1095 again:
1096         rc = sleep_until_event(otg,
1097                         otg_mask, user_mask,
1098                         &otg_events, &user_events, 0);
1099         if (rc < 0)
1100                 return OTG_STATE_UNDEFINED;
1101
1102         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1103                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1104                 return OTG_STATE_UNDEFINED;
1105         } else if (otg_events & OEVT_B_DEV_VBUS_CHNG_EVNT) {
1106                 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1107
1108                 if (otg_events & OEVT_B_SES_VLD_EVT) {
1109                         otg_dbg(otg, "Session valid\n");
1110                         goto again;
1111                 } else {
1112                         otg_dbg(otg, "Session not valid\n");
1113                         return OTG_STATE_B_IDLE;
1114                 }
1115
1116         } else if (user_events & USER_HNP_EVENT) {
1117                 otg_dbg(otg, "USER_HNP_EVENT\n");
1118                 return do_b_hnp_init(otg);
1119         } else if (user_events & USER_END_SESSION) {
1120                 otg_dbg(otg, "USER_END_SESSION\n");
1121                 return OTG_STATE_B_IDLE;
1122         }
1123
1124         return OTG_STATE_UNDEFINED;
1125 }
1126
1127 static enum usb_otg_state do_b_wait_acon(struct dwc3_otg *otg)
1128 {
1129         int rc;
1130         u32 otg_mask;
1131         u32 user_mask = 0;
1132         u32 otg_events = 0;
1133         u32 user_events = 0;
1134         struct usb_hcd *hcd;
1135         struct xhci_hcd *xhci;
1136
1137         otg_dbg(otg, "");
1138         set_b_host(otg, 1);
1139         start_host(otg);
1140         otg_mask = OEVT_B_DEV_B_HOST_END_EVNT;
1141         otg_write(otg, OEVTEN, otg_mask);
1142         reset_port(otg);
1143
1144         hcd = container_of(otg->otg.host, struct usb_hcd, self);
1145         xhci = hcd_to_xhci(hcd);
1146         usb_kick_hub_wq(hcd->self.root_hub);
1147         if (xhci->shared_hcd)
1148                 usb_kick_hub_wq(xhci->shared_hcd->self.root_hub);
1149
1150         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1151                 OEVT_B_DEV_B_HOST_END_EVNT |
1152                 OEVT_B_DEV_VBUS_CHNG_EVNT |
1153                 OEVT_HOST_ROLE_REQ_INIT_EVNT;
1154         user_mask = USER_A_CONN_EVENT | USER_HNP_END_SESSION;
1155
1156 again:
1157         rc = sleep_until_event(otg,
1158                         otg_mask, user_mask,
1159                         &otg_events, &user_events, 0);
1160         if (rc < 0)
1161                 return OTG_STATE_UNDEFINED;
1162
1163         /* Higher priority first */
1164         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1165                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1166                 return OTG_STATE_UNDEFINED;
1167         } else if (otg_events & OEVT_B_DEV_B_HOST_END_EVNT) {
1168                 otg_dbg(otg, "OEVT_B_DEV_B_HOST_END_EVNT\n");
1169                 return OTG_STATE_B_PERIPHERAL;
1170         } else if (otg_events & OEVT_B_DEV_VBUS_CHNG_EVNT) {
1171                 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1172                 if (otg_events & OEVT_B_SES_VLD_EVT) {
1173                         otg_dbg(otg, "Session valid\n");
1174                         goto again;
1175                 } else {
1176                         otg_dbg(otg, "Session not valid\n");
1177                         return OTG_STATE_B_IDLE;
1178                 }
1179         } else if (user_events & USER_A_CONN_EVENT) {
1180                 otg_dbg(otg, "A-device connected\n");
1181                 return OTG_STATE_B_HOST;
1182         } else if (user_events & USER_HNP_END_SESSION) {
1183                 otg_dbg(otg, "USER_HNP_END_SESSION\n");
1184                 return OTG_STATE_B_PERIPHERAL;
1185         }
1186
1187         /* Invalid state */
1188         return OTG_STATE_UNDEFINED;
1189 }
1190
1191 static enum usb_otg_state do_b_host(struct dwc3_otg *otg)
1192 {
1193         int rc;
1194         u32 otg_mask;
1195         u32 user_mask = 0;
1196         u32 otg_events = 0;
1197         u32 user_events = 0;
1198
1199         otg_dbg(otg, "");
1200
1201         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1202                 OEVT_B_DEV_B_HOST_END_EVNT |
1203                 OEVT_B_DEV_VBUS_CHNG_EVNT |
1204                 OEVT_HOST_ROLE_REQ_INIT_EVNT;
1205         user_mask = USER_HNP_END_SESSION;
1206
1207 again:
1208         rc = sleep_until_event(otg,
1209                         otg_mask, user_mask,
1210                         &otg_events, &user_events, 0);
1211         if (rc < 0)
1212                 return OTG_STATE_UNDEFINED;
1213
1214         /* Higher priority first */
1215         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1216                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1217                 return OTG_STATE_UNDEFINED;
1218         } else if (otg_events & OEVT_B_DEV_B_HOST_END_EVNT) {
1219                 otg_dbg(otg, "OEVT_B_DEV_B_HOST_END_EVNT\n");
1220                 return OTG_STATE_B_PERIPHERAL;
1221         } else if (otg_events & OEVT_B_DEV_VBUS_CHNG_EVNT) {
1222                 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1223                 if (otg_events & OEVT_B_SES_VLD_EVT) {
1224                         otg_dbg(otg, "Session valid\n");
1225                         goto again;
1226                 } else {
1227                         otg_dbg(otg, "Session not valid\n");
1228                         return OTG_STATE_B_IDLE;
1229                 }
1230         } else if (user_events & USER_HNP_END_SESSION) {
1231                 otg_dbg(otg, "USER_HNP_END_SESSION\n");
1232                 return OTG_STATE_B_PERIPHERAL;
1233         }
1234
1235         /* Invalid state */
1236         return OTG_STATE_UNDEFINED;
1237 }
1238
1239 static enum usb_otg_state do_b_idle(struct dwc3_otg *otg)
1240 {
1241         int rc;
1242         u32 otg_mask;
1243         u32 user_mask;
1244         u32 otg_events = 0;
1245         u32 user_events = 0;
1246
1247         otg_dbg(otg, "");
1248
1249         if (!set_peri_mode(otg, PERI_MODE_PERIPHERAL))
1250                 otg_err(otg, "Failed to set peripheral mode\n");
1251
1252         dwc3_otg_setup_event_buffers(otg);
1253
1254         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1255                 OEVT_B_DEV_SES_VLD_DET_EVNT |
1256                 OEVT_B_DEV_VBUS_CHNG_EVNT;
1257         user_mask = USER_SRP_EVENT;
1258
1259 again:
1260         rc = sleep_until_event(otg,
1261                         otg_mask, user_mask,
1262                         &otg_events, &user_events, 0);
1263
1264         if (rc < 0)
1265                 return OTG_STATE_UNDEFINED;
1266
1267         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1268                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1269                 return OTG_STATE_UNDEFINED;
1270         } else if ((otg_events & OEVT_B_DEV_VBUS_CHNG_EVNT) ||
1271                 (otg_events & OEVT_B_DEV_SES_VLD_DET_EVNT)) {
1272                 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1273                 if (otg_events & OEVT_B_SES_VLD_EVT) {
1274                         otg_dbg(otg, "Session valid\n");
1275                         return OTG_STATE_B_PERIPHERAL;
1276
1277                 } else {
1278                         otg_dbg(otg, "Session not valid\n");
1279                         goto again;
1280                 }
1281         } else if (user_events & USER_SRP_EVENT) {
1282                 otg_dbg(otg, "USER_SRP_EVENT\n");
1283                 return OTG_STATE_B_SRP_INIT;
1284         }
1285
1286         return OTG_STATE_UNDEFINED;
1287 }
1288
1289 static enum usb_otg_state do_b_srp_init(struct dwc3_otg *otg)
1290 {
1291         int rc;
1292         u32 otg_mask;
1293         u32 events = 0;
1294
1295         otg_dbg(otg, "");
1296         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1297                 OEVT_B_DEV_SES_VLD_DET_EVNT |
1298                 OEVT_B_DEV_VBUS_CHNG_EVNT;
1299
1300         otg_write(otg, OEVTEN, otg_mask);
1301         start_srp(otg);
1302
1303         rc = SRP_TIMEOUT;
1304
1305 again:
1306         rc = sleep_until_event(otg,
1307                         otg_mask, 0,
1308                         &events, NULL, rc);
1309         if (rc < 0)
1310                 return OTG_STATE_UNDEFINED;
1311
1312         if (events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1313                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1314                 return OTG_STATE_UNDEFINED;
1315         } else if (events & OEVT_B_DEV_SES_VLD_DET_EVNT) {
1316                 otg_dbg(otg, "OEVT_B_DEV_SES_VLD_DET_EVNT\n");
1317                 return OTG_STATE_B_PERIPHERAL;
1318         } else if (rc == 0) {
1319                 otg_dbg(otg, "SRP Timeout (rc=%d)\n", rc);
1320                 otg_info(otg, "DEVICE NO RESPONSE FOR SRP\n");
1321                 return OTG_STATE_B_IDLE;
1322
1323         } else {
1324                 goto again;
1325         }
1326
1327         return OTG_STATE_UNDEFINED;
1328 }
1329
1330 int otg_main_thread(void *data)
1331 {
1332         struct dwc3_otg *otg = (struct dwc3_otg *)data;
1333         enum usb_otg_state prev = OTG_STATE_UNDEFINED;
1334
1335 #ifdef VERBOSE_DEBUG
1336         u32 snpsid = otg_read(otg, 0xc120);
1337
1338         otg_vdbg(otg, "io_priv=%p\n", otg->regs);
1339         otg_vdbg(otg, "c120: %x\n", snpsid);
1340 #endif
1341
1342         /* Allow the thread to be killed by a signal, but set the signal mask
1343          * to block everything but INT, TERM, KILL, and USR1.
1344          */
1345         allow_signal(SIGINT);
1346         allow_signal(SIGTERM);
1347         allow_signal(SIGKILL);
1348         allow_signal(SIGUSR1);
1349
1350         /* Allow the thread to be frozen */
1351         set_freezable();
1352
1353         /* Allow host/peripheral driver load to finish */
1354         msleep(100);
1355
1356         reset_hw(otg);
1357
1358         stop_host(otg);
1359         stop_peripheral(otg);
1360
1361         otg_dbg(otg, "Thread running\n");
1362         while (1) {
1363                 enum usb_otg_state next = OTG_STATE_UNDEFINED;
1364
1365                 otg_vdbg(otg, "Main thread entering state\n");
1366
1367                 switch (otg->otg.state) {
1368                 case OTG_STATE_UNDEFINED:
1369                         otg_dbg(otg, "OTG_STATE_UNDEFINED\n");
1370                         next = do_connector_id_status(otg);
1371                         break;
1372
1373                 case OTG_STATE_A_IDLE:
1374                         otg_dbg(otg, "OTG_STATE_A_IDLE\n");
1375                         stop_peripheral(otg);
1376
1377                         if (prev == OTG_STATE_UNDEFINED)
1378                                 next = OTG_STATE_A_WAIT_VRISE;
1379                         else
1380                                 next = do_a_idle(otg);
1381                         break;
1382
1383                 case OTG_STATE_A_WAIT_VRISE:
1384                         otg_dbg(otg, "OTG_STATE_A_WAIT_VRISE\n");
1385                         next = do_a_wait_vrise(otg);
1386                         break;
1387
1388                 case OTG_STATE_A_WAIT_BCON:
1389                         otg_dbg(otg, "OTG_STATE_A_WAIT_BCON\n");
1390                         next = do_a_wait_bconn(otg);
1391                         break;
1392
1393                 case OTG_STATE_A_HOST:
1394                         otg_dbg(otg, "OTG_STATE_A_HOST\n");
1395                         stop_peripheral(otg);
1396                         next = do_a_host(otg);
1397                         /* Don't stop the host here if we are going into
1398                          * A_SUSPEND. We need to delay that until later. It
1399                          * will be stopped when coming out of A_SUSPEND
1400                          * state.
1401                          */
1402                         if (next != OTG_STATE_A_SUSPEND)
1403                                 stop_host(otg);
1404                         break;
1405
1406                 case OTG_STATE_A_SUSPEND:
1407                         otg_dbg(otg, "OTG_STATE_A_SUSPEND\n");
1408                         next = do_a_hnp_init(otg);
1409
1410                         /* Stop the host. */
1411                         stop_host(otg);
1412                         break;
1413
1414                 case OTG_STATE_A_WAIT_VFALL:
1415                         otg_dbg(otg, "OTG_STATE_A_WAIT_VFALL\n");
1416                         next = do_a_wait_vfall(otg);
1417                         stop_host(otg);
1418                         break;
1419
1420                 case OTG_STATE_A_PERIPHERAL:
1421                         otg_dbg(otg, "OTG_STATE_A_PERIPHERAL\n");
1422                         stop_host(otg);
1423                         start_peripheral(otg);
1424                         next = do_a_peripheral(otg);
1425                         stop_peripheral(otg);
1426                         break;
1427
1428                 case OTG_STATE_B_IDLE:
1429                         otg_dbg(otg, "OTG_STATE_B_IDLE\n");
1430                         next = do_b_idle(otg);
1431                         break;
1432
1433                 case OTG_STATE_B_PERIPHERAL:
1434                         otg_dbg(otg, "OTG_STATE_B_PERIPHERAL\n");
1435                         stop_host(otg);
1436                         start_peripheral(otg);
1437                         next = do_b_peripheral(otg);
1438                         stop_peripheral(otg);
1439                         break;
1440
1441                 case OTG_STATE_B_SRP_INIT:
1442                         otg_dbg(otg, "OTG_STATE_B_SRP_INIT\n");
1443                         otg_read(otg, OSTS);
1444                         next = do_b_srp_init(otg);
1445                         break;
1446
1447                 case OTG_STATE_B_WAIT_ACON:
1448                         otg_dbg(otg, "OTG_STATE_B_WAIT_ACON\n");
1449                         next = do_b_wait_acon(otg);
1450                         break;
1451
1452                 case OTG_STATE_B_HOST:
1453                         otg_dbg(otg, "OTG_STATE_B_HOST\n");
1454                         next = do_b_host(otg);
1455                         stop_host(otg);
1456                         break;
1457
1458                 default:
1459                         otg_err(otg, "Unknown state %d, sleeping...\n",
1460                                         otg->state);
1461                         sleep_main_thread(otg);
1462                         break;
1463                 }
1464
1465                 prev = otg->otg.state;
1466                 otg->otg.state = next;
1467                 if (kthread_should_stop())
1468                         break;
1469         }
1470
1471         otg->main_thread = NULL;
1472         otg_dbg(otg, "OTG main thread exiting....\n");
1473
1474         return 0;
1475 }
1476
1477 static void start_main_thread(struct dwc3_otg *otg)
1478 {
1479         if (!otg->main_thread && otg->otg.gadget && otg->otg.host) {
1480                 otg_dbg(otg, "Starting OTG main thread\n");
1481                 otg->main_thread = kthread_create(otg_main_thread, otg, "otg");
1482                 wake_up_process(otg->main_thread);
1483         }
1484 }
1485
1486 static inline struct dwc3_otg *otg_to_dwc3_otg(struct usb_otg *x)
1487 {
1488         return container_of(x, struct dwc3_otg, otg);
1489 }
1490
1491 static irqreturn_t dwc3_otg_irq(int irq, void *_otg)
1492 {
1493         struct dwc3_otg *otg;
1494         u32 oevt;
1495         u32 osts;
1496         u32 octl;
1497         u32 ocfg;
1498         u32 oevten;
1499         u32 otg_mask = OEVT_ALL;
1500
1501         if (!_otg)
1502                 return 0;
1503
1504         otg = (struct dwc3_otg *)_otg;
1505
1506         oevt = otg_read(otg, OEVT);
1507         osts = otg_read(otg, OSTS);
1508         octl = otg_read(otg, OCTL);
1509         ocfg = otg_read(otg, OCFG);
1510         oevten = otg_read(otg, OEVTEN);
1511
1512         /* Clear handled events */
1513         otg_write(otg, OEVT, oevt);
1514
1515         otg_vdbg(otg, "\n");
1516         otg_vdbg(otg, "    oevt = %08x\n", oevt);
1517         otg_vdbg(otg, "    osts = %08x\n", osts);
1518         otg_vdbg(otg, "    octl = %08x\n", octl);
1519         otg_vdbg(otg, "    ocfg = %08x\n", ocfg);
1520         otg_vdbg(otg, "  oevten = %08x\n", oevten);
1521
1522         otg_vdbg(otg, "oevt[DeviceMode] = %s\n",
1523                         oevt & OEVT_DEV_MOD_EVNT ? "Device" : "Host");
1524
1525         if (oevt & OEVT_CONN_ID_STS_CHNG_EVNT)
1526                 otg_dbg(otg, "Connector ID Status Change Event\n");
1527         if (oevt & OEVT_HOST_ROLE_REQ_INIT_EVNT)
1528                 otg_dbg(otg, "Host Role Request Init Notification Event\n");
1529         if (oevt & OEVT_HOST_ROLE_REQ_CONFIRM_EVNT)
1530                 otg_dbg(otg, "Host Role Request Confirm Notification Event\n");
1531         if (oevt & OEVT_A_DEV_B_DEV_HOST_END_EVNT)
1532                 otg_dbg(otg, "A-Device B-Host End Event\n");
1533         if (oevt & OEVT_A_DEV_HOST_EVNT)
1534                 otg_dbg(otg, "A-Device Host Event\n");
1535         if (oevt & OEVT_A_DEV_HNP_CHNG_EVNT)
1536                 otg_dbg(otg, "A-Device HNP Change Event\n");
1537         if (oevt & OEVT_A_DEV_SRP_DET_EVNT)
1538                 otg_dbg(otg, "A-Device SRP Detect Event\n");
1539         if (oevt & OEVT_A_DEV_SESS_END_DET_EVNT)
1540                 otg_dbg(otg, "A-Device Session End Detected Event\n");
1541         if (oevt & OEVT_B_DEV_B_HOST_END_EVNT)
1542                 otg_dbg(otg, "B-Device B-Host End Event\n");
1543         if (oevt & OEVT_B_DEV_HNP_CHNG_EVNT)
1544                 otg_dbg(otg, "B-Device HNP Change Event\n");
1545         if (oevt & OEVT_B_DEV_SES_VLD_DET_EVNT)
1546                 otg_dbg(otg, "B-Device Session Valid Detect Event\n");
1547         if (oevt & OEVT_B_DEV_VBUS_CHNG_EVNT)
1548                 otg_dbg(otg, "B-Device VBUS Change Event\n");
1549
1550         if (oevt & otg_mask) {
1551                 /* Pass event to main thread */
1552                 spin_lock(&otg->lock);
1553                 otg->otg_events |= oevt;
1554                 wakeup_main_thread(otg);
1555                 spin_unlock(&otg->lock);
1556                 return 1;
1557         }
1558
1559         return IRQ_HANDLED;
1560 }
1561
1562 static void hnp_polling_work(struct work_struct *w)
1563 {
1564         struct dwc3_otg *otg = container_of(w, struct dwc3_otg,
1565                         hp_work.work);
1566         struct usb_bus *bus;
1567         struct usb_device *udev;
1568         struct usb_hcd *hcd;
1569         u8 *otgstatus;
1570         int ret;
1571         int err;
1572
1573         hcd = container_of(otg->otg.host, struct usb_hcd, self);
1574         if (!hcd)
1575                 return;
1576
1577         bus = &hcd->self;
1578         if (!bus->otg_port)
1579                 return;
1580
1581         udev = usb_hub_find_child(bus->root_hub, bus->otg_port);
1582         if (!udev)
1583                 return;
1584
1585         otgstatus = kmalloc(sizeof(*otgstatus), GFP_NOIO);
1586         if (!otgstatus)
1587                 return;
1588
1589         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1590                         USB_REQ_GET_STATUS, USB_DIR_IN | USB_RECIP_DEVICE,
1591                         0, 0xf000, otgstatus, sizeof(*otgstatus),
1592                         USB_CTRL_GET_TIMEOUT);
1593
1594         if (ret == sizeof(*otgstatus) && (*otgstatus & 0x1)) {
1595                 /* enable HNP before suspend, it's simpler */
1596
1597                 udev->bus->b_hnp_enable = 1;
1598                 err = usb_control_msg(udev,
1599                                 usb_sndctrlpipe(udev, 0),
1600                                 USB_REQ_SET_FEATURE, 0,
1601                                 udev->bus->b_hnp_enable
1602                                 ? USB_DEVICE_B_HNP_ENABLE
1603                                 : USB_DEVICE_A_ALT_HNP_SUPPORT,
1604                                 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
1605
1606                 if (err < 0) {
1607                         /* OTG MESSAGE: report errors here,
1608                          * customize to match your product.
1609                          */
1610                         otg_info(otg, "ERROR : Device no response\n");
1611                         dev_info(&udev->dev, "can't set HNP mode: %d\n",
1612                                         err);
1613                         udev->bus->b_hnp_enable = 0;
1614                         if (le16_to_cpu(udev->descriptor.idVendor) == 0x1a0a) {
1615                                 if (usb_port_suspend(udev, PMSG_AUTO_SUSPEND)
1616                                                 < 0)
1617                                         dev_dbg(&udev->dev, "HNP fail, %d\n",
1618                                                         err);
1619                         }
1620                 } else {
1621                         /* Device wants role-switch, suspend the bus. */
1622                         static struct usb_phy *phy;
1623
1624                         phy = usb_get_phy(USB_PHY_TYPE_USB3);
1625                         otg_start_hnp(phy->otg);
1626                         usb_put_phy(phy);
1627
1628                         if (usb_port_suspend(udev, PMSG_AUTO_SUSPEND) < 0)
1629                                 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
1630                 }
1631         } else if (ret < 0) {
1632                 udev->bus->b_hnp_enable = 1;
1633                 err = usb_control_msg(udev,
1634                                 usb_sndctrlpipe(udev, 0),
1635                                 USB_REQ_SET_FEATURE, 0,
1636                                 USB_DEVICE_B_HNP_ENABLE,
1637                                 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
1638                 if (usb_port_suspend(udev, PMSG_AUTO_SUSPEND) < 0)
1639                         dev_dbg(&udev->dev, "HNP fail, %d\n", err);
1640         } else {
1641                 schedule_delayed_work(&otg->hp_work, 1 * HZ);
1642         }
1643
1644         kfree(otgstatus);
1645 }
1646
1647 static int dwc3_otg_notify_connect(struct usb_phy *phy,
1648                 enum usb_device_speed speed)
1649 {
1650         struct usb_bus *bus;
1651         struct usb_device *udev;
1652         struct usb_hcd *hcd;
1653         struct dwc3_otg *otg;
1654         int err = 0;
1655
1656         otg = otg_to_dwc3_otg(phy->otg);
1657
1658         hcd = container_of(phy->otg->host, struct usb_hcd, self);
1659         if (!hcd)
1660                 return -EINVAL;
1661
1662         bus = &hcd->self;
1663         if (!bus->otg_port)
1664                 return 0;
1665
1666         udev = usb_hub_find_child(bus->root_hub, bus->otg_port);
1667         if (!udev)
1668                 return 0;
1669
1670         /*
1671          * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1672          * to wake us after we've powered off VBUS; and HNP, switching roles
1673          * "host" to "peripheral".  The OTG descriptor helps figure this out.
1674          */
1675         if (udev->config && udev->parent == udev->bus->root_hub) {
1676                 struct usb_otg20_descriptor     *desc = NULL;
1677
1678                 /* descriptor may appear anywhere in config */
1679                 err = __usb_get_extra_descriptor(udev->rawdescriptors[0],
1680                                 le16_to_cpu(udev->config[0].desc.wTotalLength),
1681                                 USB_DT_OTG, (void **) &desc);
1682                 if (err || !(desc->bmAttributes & USB_OTG_HNP))
1683                         return 0;
1684
1685                 if (udev->portnum == udev->bus->otg_port) {
1686                         INIT_DELAYED_WORK(&otg->hp_work,
1687                                         hnp_polling_work);
1688                         schedule_delayed_work(&otg->hp_work, HZ);
1689                 }
1690
1691         }
1692
1693         return err;
1694 }
1695
1696 static int dwc3_otg_notify_disconnect(struct usb_phy *phy,
1697                 enum usb_device_speed speed)
1698 {
1699         struct dwc3_otg *otg;
1700
1701         otg = otg_to_dwc3_otg(phy->otg);
1702
1703         if (work_pending(&otg->hp_work.work)) {
1704                 while (!cancel_delayed_work(&otg->hp_work))
1705                         msleep(20);
1706         }
1707         return 0;
1708 }
1709
1710 static void dwc3_otg_set_peripheral(struct usb_otg *_otg, int yes)
1711 {
1712         struct dwc3_otg *otg;
1713
1714         if (!_otg)
1715                 return;
1716
1717         otg = otg_to_dwc3_otg(_otg);
1718         otg_dbg(otg, "\n");
1719
1720         if (yes) {
1721                 if (otg->hwparams6 == 0xdeadbeef)
1722                         otg->hwparams6 = otg_read(otg, GHWPARAMS6);
1723                 stop_host(otg);
1724         } else {
1725                 stop_peripheral(otg);
1726         }
1727
1728         set_peri_mode(otg, yes);
1729 }
1730 EXPORT_SYMBOL(dwc3_otg_set_peripheral);
1731
1732 static int dwc3_otg_set_periph(struct usb_otg *_otg, struct usb_gadget *gadget)
1733 {
1734         struct dwc3_otg *otg;
1735
1736         if (!_otg)
1737                 return -ENODEV;
1738
1739         otg = otg_to_dwc3_otg(_otg);
1740         otg_dbg(otg, "\n");
1741
1742         if ((long)gadget == 1) {
1743                 dwc3_otg_set_peripheral(_otg, 1);
1744                 return 0;
1745         }
1746
1747         if (!gadget) {
1748                 otg->otg.gadget = NULL;
1749                 return -ENODEV;
1750         }
1751
1752         otg->otg.gadget = gadget;
1753         otg->otg.gadget->hnp_polling_support = 1;
1754         otg->otg.state = OTG_STATE_B_IDLE;
1755
1756         start_main_thread(otg);
1757         return 0;
1758 }
1759
1760 static int dwc3_otg_set_host(struct usb_otg *_otg, struct usb_bus *host)
1761 {
1762         struct dwc3_otg *otg;
1763         struct usb_hcd *hcd;
1764         struct xhci_hcd *xhci;
1765
1766         if (!_otg)
1767                 return -ENODEV;
1768
1769         otg = otg_to_dwc3_otg(_otg);
1770         otg_dbg(otg, "\n");
1771
1772         if ((long)host == 1) {
1773                 dwc3_otg_set_peripheral(_otg, 0);
1774                 return 0;
1775         }
1776
1777         if (!host) {
1778                 otg->otg.host = NULL;
1779                 otg->hcd_irq = 0;
1780                 return -ENODEV;
1781         }
1782
1783         hcd = container_of(host, struct usb_hcd, self);
1784         xhci = hcd_to_xhci(hcd);
1785         otg_dbg(otg, "hcd=%p xhci=%p\n", hcd, xhci);
1786
1787         hcd->self.otg_port = 1;
1788         if (xhci->shared_hcd) {
1789                 xhci->shared_hcd->self.otg_port = 1;
1790                 otg_dbg(otg, "shared_hcd=%p\n", xhci->shared_hcd);
1791         }
1792
1793         otg->otg.host = host;
1794         otg->hcd_irq = hcd->irq;
1795         otg_dbg(otg, "host=%p irq=%d\n", otg->otg.host, otg->hcd_irq);
1796
1797
1798         otg->host_started = 1;
1799         otg->dev_enum = 0;
1800         start_main_thread(otg);
1801         return 0;
1802 }
1803
1804 static int dwc3_otg_start_srp(struct usb_otg *x)
1805 {
1806         unsigned long flags;
1807         struct dwc3_otg *otg;
1808
1809         if (!x)
1810                 return -ENODEV;
1811
1812         otg = otg_to_dwc3_otg(x);
1813         otg_dbg(otg, "\n");
1814
1815         if (!otg->otg.host || !otg->otg.gadget)
1816                 return -ENODEV;
1817
1818         spin_lock_irqsave(&otg->lock, flags);
1819         otg->user_events |= USER_SRP_EVENT;
1820         wakeup_main_thread(otg);
1821         spin_unlock_irqrestore(&otg->lock, flags);
1822         return 0;
1823 }
1824
1825 static int dwc3_otg_start_hnp(struct usb_otg *x)
1826 {
1827         unsigned long flags;
1828         struct dwc3_otg *otg;
1829
1830         if (!x)
1831                 return -ENODEV;
1832
1833         otg = otg_to_dwc3_otg(x);
1834         otg_dbg(otg, "\n");
1835
1836         if (!otg->otg.host || !otg->otg.gadget)
1837                 return -ENODEV;
1838
1839         spin_lock_irqsave(&otg->lock, flags);
1840         otg->user_events |= USER_HNP_EVENT;
1841         wakeup_main_thread(otg);
1842         spin_unlock_irqrestore(&otg->lock, flags);
1843         return 0;
1844 }
1845
1846 static int dwc3_otg_end_session(struct usb_otg *x)
1847 {
1848         unsigned long flags;
1849         struct dwc3_otg *otg;
1850
1851         if (!x)
1852                 return -ENODEV;
1853
1854         otg = otg_to_dwc3_otg(x);
1855         otg_dbg(otg, "\n");
1856
1857         if (!otg->otg.host || !otg->otg.gadget)
1858                 return -ENODEV;
1859
1860         spin_lock_irqsave(&otg->lock, flags);
1861         otg->user_events |= USER_END_SESSION;
1862         wakeup_main_thread(otg);
1863         spin_unlock_irqrestore(&otg->lock, flags);
1864         return 0;
1865 }
1866
1867 static int otg_end_session(struct usb_otg *otg)
1868 {
1869         return dwc3_otg_end_session(otg);
1870 }
1871 EXPORT_SYMBOL(otg_end_session);
1872
1873 static int dwc3_otg_received_host_release(struct usb_otg *x)
1874 {
1875         struct dwc3_otg *otg;
1876         unsigned long flags;
1877
1878         if (!x)
1879                 return -ENODEV;
1880
1881         otg = otg_to_dwc3_otg(x);
1882         otg_dbg(otg, "\n");
1883
1884         if (!otg->otg.host || !otg->otg.gadget)
1885                 return -ENODEV;
1886
1887         spin_lock_irqsave(&otg->lock, flags);
1888         otg->user_events |= PCD_RECEIVED_HOST_RELEASE_EVENT;
1889         wakeup_main_thread(otg);
1890         spin_unlock_irqrestore(&otg->lock, flags);
1891         return 0;
1892 }
1893
1894 int otg_host_release(struct usb_otg *otg)
1895 {
1896         return dwc3_otg_received_host_release(otg);
1897 }
1898 EXPORT_SYMBOL(otg_host_release);
1899
1900 static void dwc3_otg_enable_irq(struct dwc3_otg *otg)
1901 {
1902         u32                     reg;
1903
1904         /* Enable OTG IRQs */
1905         reg = OEVT_ALL;
1906
1907         otg_write(otg, OEVTEN, reg);
1908 }
1909
1910 static ssize_t store_srp(struct device *dev, struct device_attribute *attr,
1911                          const char *buf, size_t count)
1912 {
1913         struct usb_phy *phy;
1914         struct usb_otg *otg;
1915
1916         phy = usb_get_phy(USB_PHY_TYPE_USB3);
1917         if (IS_ERR(phy) || !phy) {
1918                 if (!IS_ERR(phy))
1919                         usb_put_phy(phy);
1920                 return count;
1921         }
1922
1923         otg = phy->otg;
1924         if (!otg) {
1925                 usb_put_phy(phy);
1926                 return count;
1927         }
1928
1929         otg_start_srp(otg);
1930         usb_put_phy(phy);
1931         return count;
1932 }
1933 static DEVICE_ATTR(srp, 0220, NULL, store_srp);
1934
1935 static ssize_t store_end(struct device *dev, struct device_attribute *attr,
1936                          const char *buf, size_t count)
1937 {
1938         struct usb_phy *phy;
1939         struct usb_otg *otg;
1940
1941         phy = usb_get_phy(USB_PHY_TYPE_USB3);
1942         if (IS_ERR(phy) || !phy) {
1943                 if (!IS_ERR(phy))
1944                         usb_put_phy(phy);
1945                 return count;
1946         }
1947
1948         otg = phy->otg;
1949         if (!otg) {
1950                 usb_put_phy(phy);
1951                 return count;
1952         }
1953
1954         otg_end_session(otg);
1955         usb_put_phy(phy);
1956         return count;
1957 }
1958 static DEVICE_ATTR(end, 0220, NULL, store_end);
1959
1960 static ssize_t store_hnp(struct device *dev, struct device_attribute *attr,
1961                          const char *buf, size_t count)
1962 {
1963         struct dwc3 *dwc = dev_get_drvdata(dev);
1964         struct usb_phy *phy = usb_get_phy(USB_PHY_TYPE_USB3);
1965         struct usb_otg *otg;
1966
1967         dev_dbg(dwc->dev, "%s()\n", __func__);
1968
1969         if (IS_ERR(phy) || !phy) {
1970                 dev_info(dwc->dev, "NO PHY!!\n");
1971                 if (!IS_ERR(phy))
1972                         usb_put_phy(phy);
1973                 return count;
1974         }
1975
1976         otg = phy->otg;
1977         if (!otg) {
1978                 dev_info(dwc->dev, "NO OTG!!\n");
1979                 usb_put_phy(phy);
1980                 return count;
1981         }
1982
1983         dev_info(dev, "b_hnp_enable is FALSE\n");
1984         dwc->gadget.host_request_flag = 1;
1985
1986         usb_put_phy(phy);
1987         return count;
1988 }
1989 static DEVICE_ATTR(hnp, 0220, NULL, store_hnp);
1990
1991 static ssize_t store_hnp_end(struct device *dev, struct device_attribute *attr,
1992                              const char *buf, size_t count)
1993 {
1994         struct usb_phy *phy;
1995         struct usb_otg *otg;
1996         unsigned long flags;
1997         struct dwc3_otg *dwc_otg;
1998
1999         phy = usb_get_phy(USB_PHY_TYPE_USB3);
2000         if (IS_ERR(phy) || !phy) {
2001                 if (!IS_ERR(phy))
2002                         usb_put_phy(phy);
2003                 return count;
2004         }
2005
2006         otg = phy->otg;
2007         if (!otg) {
2008                 usb_put_phy(phy);
2009                 return count;
2010         }
2011
2012         dwc_otg = otg_to_dwc3_otg(otg);
2013
2014         spin_lock_irqsave(&dwc_otg->lock, flags);
2015         dwc_otg->user_events |= USER_HNP_END_SESSION;
2016         wakeup_main_thread(dwc_otg);
2017         spin_unlock_irqrestore(&dwc_otg->lock, flags);
2018
2019         usb_put_phy(phy);
2020         return count;
2021 }
2022 static DEVICE_ATTR(hnp_end, 0220, NULL, store_hnp_end);
2023
2024 static ssize_t store_a_hnp_reqd(struct device *dev,
2025                 struct device_attribute *attr, const char *buf, size_t count)
2026 {
2027         struct dwc3 *dwc = dev_get_drvdata(dev);
2028         struct dwc3_otg *otg;
2029
2030         otg = dwc->otg;
2031         host_release(otg);
2032         return count;
2033 }
2034 static DEVICE_ATTR(a_hnp_reqd, 0220, NULL, store_a_hnp_reqd);
2035
2036 static ssize_t store_print_dbg(struct device *dev,
2037                 struct device_attribute *attr, const char *buf, size_t count)
2038 {
2039         struct dwc3 *dwc = dev_get_drvdata(dev);
2040         struct dwc3_otg *otg;
2041
2042         otg = dwc->otg;
2043         print_debug_regs(otg);
2044
2045         return count;
2046 }
2047 static DEVICE_ATTR(print_dbg, 0220, NULL, store_print_dbg);
2048
2049 void dwc_usb3_remove_dev_files(struct device *dev)
2050 {
2051         device_remove_file(dev, &dev_attr_print_dbg);
2052         device_remove_file(dev, &dev_attr_a_hnp_reqd);
2053         device_remove_file(dev, &dev_attr_end);
2054         device_remove_file(dev, &dev_attr_srp);
2055         device_remove_file(dev, &dev_attr_hnp);
2056         device_remove_file(dev, &dev_attr_hnp_end);
2057 }
2058
2059 int dwc3_otg_create_dev_files(struct device *dev)
2060 {
2061         int retval;
2062
2063         retval = device_create_file(dev, &dev_attr_hnp);
2064         if (retval)
2065                 goto fail;
2066
2067         retval = device_create_file(dev, &dev_attr_hnp_end);
2068         if (retval)
2069                 goto fail;
2070
2071         retval = device_create_file(dev, &dev_attr_srp);
2072         if (retval)
2073                 goto fail;
2074
2075         retval = device_create_file(dev, &dev_attr_end);
2076         if (retval)
2077                 goto fail;
2078
2079         retval = device_create_file(dev, &dev_attr_a_hnp_reqd);
2080         if (retval)
2081                 goto fail;
2082
2083         retval = device_create_file(dev, &dev_attr_print_dbg);
2084         if (retval)
2085                 goto fail;
2086
2087         return 0;
2088
2089 fail:
2090         dev_err(dev, "Failed to create one or more sysfs files!!\n");
2091         return retval;
2092 }
2093
2094 void dwc3_otg_init(struct dwc3 *dwc)
2095 {
2096         struct dwc3_otg *otg;
2097         int err;
2098         u32 reg;
2099
2100         dev_dbg(dwc->dev, "dwc3_otg_init\n");
2101
2102         /*
2103          * GHWPARAMS6[10] bit is SRPSupport.
2104          * This bit also reflects DWC_USB3_EN_OTG
2105          */
2106         reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
2107         if (!(reg & GHWPARAMS6_SRP_SUPPORT_ENABLED)) {
2108                 /*
2109                  * No OTG support in the HW core.
2110                  * We return 0 to indicate no error, since this is acceptable
2111                  * situation, just continue probe the dwc3 driver without otg.
2112                  */
2113                 dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n");
2114                 return;
2115         }
2116
2117         otg = kzalloc(sizeof(*otg), GFP_KERNEL);
2118         if (!otg) {
2119                 dev_err(otg->dev, "failed to allocate memroy\n");
2120                 return;
2121         }
2122
2123         dwc->otg = otg;
2124         otg->dev = dwc->dev;
2125         otg->dwc = dwc;
2126
2127         otg->regs = dwc->regs - DWC3_GLOBALS_REGS_START;
2128         otg->otg.usb_phy = kzalloc(sizeof(struct usb_phy), GFP_KERNEL);
2129         otg->otg.usb_phy->dev = otg->dev;
2130         otg->otg.usb_phy->label = "dwc3_otg";
2131         otg->otg.state = OTG_STATE_UNDEFINED;
2132         otg->otg.usb_phy->otg = &otg->otg;
2133         otg->otg.usb_phy->notify_connect = dwc3_otg_notify_connect;
2134         otg->otg.usb_phy->notify_disconnect = dwc3_otg_notify_disconnect;
2135
2136         otg->otg.start_srp = dwc3_otg_start_srp;
2137         otg->otg.start_hnp = dwc3_otg_start_hnp;
2138         otg->otg.set_host = dwc3_otg_set_host;
2139         otg->otg.set_peripheral = dwc3_otg_set_periph;
2140
2141         otg->hwparams6 = reg;
2142         otg->state = OTG_STATE_UNDEFINED;
2143
2144         spin_lock_init(&otg->lock);
2145         init_waitqueue_head(&otg->main_wq);
2146
2147         err = usb_add_phy(otg->otg.usb_phy, USB_PHY_TYPE_USB3);
2148         if (err) {
2149                 dev_err(otg->dev, "can't register transceiver, err: %d\n",
2150                         err);
2151                 goto exit;
2152         }
2153
2154         otg->irq = platform_get_irq(to_platform_device(otg->dev), 1);
2155
2156         dwc3_otg_create_dev_files(otg->dev);
2157
2158         /* Set irq handler */
2159         err = request_irq(otg->irq, dwc3_otg_irq, IRQF_SHARED, "dwc3_otg", otg);
2160         if (err) {
2161                 dev_err(otg->otg.usb_phy->dev, "failed to request irq #%d --> %d\n",
2162                                 otg->irq, err);
2163                 goto exit;
2164         }
2165
2166         dwc3_otg_enable_irq(otg);
2167
2168         err = dwc3_gadget_init(dwc);
2169         if (err) {
2170                 if (err != -EPROBE_DEFER)
2171                         dev_err(otg->otg.usb_phy->dev,
2172                                 "failed to initialize gadget\n");
2173                 goto exit;
2174         }
2175
2176         err = dwc3_host_init(dwc);
2177         if (err) {
2178                 if (err != -EPROBE_DEFER)
2179                         dev_err(otg->otg.usb_phy->dev,
2180                                 "failed to initialize host\n");
2181                 goto exit;
2182         }
2183
2184         return;
2185
2186 exit:
2187         kfree(otg->otg.usb_phy);
2188         kfree(otg);
2189 }
2190
2191 void dwc3_otg_exit(struct dwc3 *dwc)
2192 {
2193         struct dwc3_otg *otg = dwc->otg;
2194
2195         otg_dbg(otg, "\n");
2196         usb_remove_phy(otg->otg.usb_phy);
2197         kfree(otg->otg.usb_phy);
2198         kfree(otg);
2199 }