blob: 596eb7be10f928512b696def1a6588735e5a2b7c [file] [log] [blame]
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001/**
2 * dwc3_otg.c - DesignWare USB3 DRD Controller OTG
3 *
4 * Copyright (c) 2012, Code Aurora Forum. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 and
8 * only version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/usb.h>
17#include <linux/usb/hcd.h>
18#include <linux/platform_device.h>
19
20#include "core.h"
21#include "dwc3_otg.h"
22#include "io.h"
23#include "xhci.h"
24
25
26/**
27 * dwc3_otg_set_host_regs - reset dwc3 otg registers to host operation.
28 *
29 * This function sets the OTG registers to work in A-Device host mode.
30 * This function should be called just before entering to A-Device mode.
31 *
32 * @w: Pointer to the dwc3 otg workqueue.
33 */
34static void dwc3_otg_set_host_regs(struct dwc3_otg *dotg)
35{
36 u32 octl;
37
38 /* Set OCTL[6](PeriMode) to 0 (host) */
39 octl = dwc3_readl(dotg->regs, DWC3_OCTL);
40 octl &= ~DWC3_OTG_OCTL_PERIMODE;
41 dwc3_writel(dotg->regs, DWC3_OCTL, octl);
42
43 /*
44 * TODO: add more OTG registers writes for HOST mode here,
45 * see figure 12-10 A-device flow in dwc3 Synopsis spec
46 */
47}
48
49/**
50 * dwc3_otg_set_peripheral_regs - reset dwc3 otg registers to peripheral operation.
51 *
52 * This function sets the OTG registers to work in B-Device peripheral mode.
53 * This function should be called just before entering to B-Device mode.
54 *
55 * @w: Pointer to the dwc3 otg workqueue.
56 */
57static void dwc3_otg_set_peripheral_regs(struct dwc3_otg *dotg)
58{
59 u32 octl;
60
61 /* Set OCTL[6](PeriMode) to 1 (peripheral) */
62 octl = dwc3_readl(dotg->regs, DWC3_OCTL);
63 octl |= DWC3_OTG_OCTL_PERIMODE;
64 dwc3_writel(dotg->regs, DWC3_OCTL, octl);
65
66 /*
67 * TODO: add more OTG registers writes for PERIPHERAL mode here,
68 * see figure 12-19 B-device flow in dwc3 Synopsis spec
69 */
70}
71
72/**
73 * dwc3_otg_start_host - helper function for starting/stoping the host controller driver.
74 *
75 * @otg: Pointer to the otg_transceiver structure.
76 * @on: start / stop the host controller driver.
77 *
78 * Returns 0 on success otherwise negative errno.
79 */
80static int dwc3_otg_start_host(struct usb_otg *otg, int on)
81{
82 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
83 struct usb_hcd *hcd;
84 struct xhci_hcd *xhci;
85 int ret = 0;
86
87 if (!otg->host)
88 return -EINVAL;
89
90 hcd = bus_to_hcd(otg->host);
91 xhci = hcd_to_xhci(hcd);
92 if (on) {
93 dev_dbg(otg->phy->dev, "%s: turn on host %s\n",
94 __func__, otg->host->bus_name);
95 dwc3_otg_set_host_regs(dotg);
96
97 /*
98 * This should be revisited for more testing post-silicon.
99 * In worst case we may need to disconnect the root hub
100 * before stopping the controller so that it does not
101 * interfere with runtime pm/system pm.
102 * We can also consider registering and unregistering xhci
103 * platform device. It is almost similar to add_hcd and
104 * remove_hcd, But we may not use standard set_host method
105 * anymore.
106 */
107 ret = hcd->driver->start(hcd);
108 if (ret) {
109 dev_err(otg->phy->dev,
110 "%s: failed to start primary hcd, ret=%d\n",
111 __func__, ret);
112 return ret;
113 }
114
115 ret = xhci->shared_hcd->driver->start(xhci->shared_hcd);
116 if (ret) {
117 dev_err(otg->phy->dev,
118 "%s: failed to start secondary hcd, ret=%d\n",
119 __func__, ret);
120 return ret;
121 }
122 } else {
123 dev_dbg(otg->phy->dev, "%s: turn off host %s\n",
124 __func__, otg->host->bus_name);
125 hcd->driver->stop(hcd);
126 }
127
128 return 0;
129}
130
131/**
132 * dwc3_otg_set_host - bind/unbind the host controller driver.
133 *
134 * @otg: Pointer to the otg_transceiver structure.
135 * @host: Pointer to the usb_bus structure.
136 *
137 * Returns 0 on success otherwise negative errno.
138 */
139static int dwc3_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
140{
141 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
142
143 if (host) {
144 dev_dbg(otg->phy->dev, "%s: set host %s\n",
145 __func__, host->bus_name);
146 otg->host = host;
147
148 /*
149 * Only after both peripheral and host are set then check
150 * OTG sm. This prevents unnecessary activation of the sm
151 * in case the ID is high.
152 */
153 if (otg->gadget)
154 schedule_work(&dotg->sm_work);
155 } else {
156 if (otg->phy->state == OTG_STATE_A_HOST) {
157 dwc3_otg_start_host(otg, 0);
158 otg->host = NULL;
159 otg->phy->state = OTG_STATE_UNDEFINED;
160 schedule_work(&dotg->sm_work);
161 } else {
162 otg->host = NULL;
163 }
164 }
165
166 return 0;
167}
168
169/**
170 * dwc3_otg_start_peripheral - bind/unbind the peripheral controller.
171 *
172 * @otg: Pointer to the otg_transceiver structure.
173 * @gadget: pointer to the usb_gadget structure.
174 *
175 * Returns 0 on success otherwise negative errno.
176 */
177static int dwc3_otg_start_peripheral(struct usb_otg *otg, int on)
178{
179 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
180
181 if (!otg->gadget)
182 return -EINVAL;
183
184 if (on) {
185 dev_dbg(otg->phy->dev, "%s: turn on gadget %s\n",
186 __func__, otg->gadget->name);
187 dwc3_otg_set_peripheral_regs(dotg);
188 usb_gadget_vbus_connect(otg->gadget);
189 } else {
190 dev_dbg(otg->phy->dev, "%s: turn off gadget %s\n",
191 __func__, otg->gadget->name);
192 usb_gadget_vbus_disconnect(otg->gadget);
193 }
194
195 return 0;
196}
197
198/**
199 * dwc3_otg_set_peripheral - bind/unbind the peripheral controller driver.
200 *
201 * @otg: Pointer to the otg_transceiver structure.
202 * @gadget: pointer to the usb_gadget structure.
203 *
204 * Returns 0 on success otherwise negative errno.
205 */
206static int dwc3_otg_set_peripheral(struct usb_otg *otg,
207 struct usb_gadget *gadget)
208{
209 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
210
211 if (gadget) {
212 dev_dbg(otg->phy->dev, "%s: set gadget %s\n",
213 __func__, gadget->name);
214 otg->gadget = gadget;
215
216 /*
217 * Only after both peripheral and host are set then check
218 * OTG sm. This prevents unnecessary activation of the sm
219 * in case the ID is grounded.
220 */
221 if (otg->host)
222 schedule_work(&dotg->sm_work);
223 } else {
224 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
225 dwc3_otg_start_peripheral(otg, 0);
226 otg->gadget = NULL;
227 otg->phy->state = OTG_STATE_UNDEFINED;
228 schedule_work(&dotg->sm_work);
229 } else {
230 otg->gadget = NULL;
231 }
232 }
233
234 return 0;
235}
236
237/**
Manu Gautam8c642812012-06-07 10:35:10 +0530238 * dwc3_ext_chg_det_done - callback to handle charger detection completion
239 * @otg: Pointer to the otg transceiver structure
240 * @charger: Pointer to the external charger structure
241 *
242 * Returns 0 on success
243 */
244static void dwc3_ext_chg_det_done(struct usb_otg *otg, struct dwc3_charger *chg)
245{
246 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
247
248 /*
249 * Ignore chg_detection notification if BSV has gone off by this time.
250 * STOP chg_det as part of !BSV handling would reset the chg_det flags
251 */
252 if (test_bit(B_SESS_VLD, &dotg->inputs))
253 schedule_work(&dotg->sm_work);
254}
255
256/**
257 * dwc3_set_charger - bind/unbind external charger driver
258 * @otg: Pointer to the otg transceiver structure
259 * @charger: Pointer to the external charger structure
260 *
261 * Returns 0 on success
262 */
263int dwc3_set_charger(struct usb_otg *otg, struct dwc3_charger *charger)
264{
265 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
266
267 dotg->charger = charger;
268 if (charger)
269 charger->notify_detection_complete = dwc3_ext_chg_det_done;
270
271 return 0;
272}
273
Manu Gautamb5067272012-07-02 09:53:41 +0530274/**
275 * dwc3_ext_event_notify - callback to handle events from external transceiver
276 * @otg: Pointer to the otg transceiver structure
277 * @event: Event reported by transceiver
278 *
279 * Returns 0 on success
280 */
281static void dwc3_ext_event_notify(struct usb_otg *otg,
282 enum dwc3_ext_events event)
283{
284 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
285 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
286 struct usb_phy *phy = dotg->otg.phy;
287
288 if (event == DWC3_EVENT_PHY_RESUME) {
289 if (!pm_runtime_status_suspended(phy->dev)) {
290 dev_warn(phy->dev, "PHY_RESUME event out of LPM!!!!\n");
291 } else {
292 dev_dbg(phy->dev, "ext PHY_RESUME event received\n");
293 /* ext_xceiver would have taken h/w out of LPM by now */
294 pm_runtime_get(phy->dev);
295 }
Manu Gautam377821c2012-09-28 16:53:24 +0530296 } else if (event == DWC3_EVENT_XCEIV_STATE) {
297 if (ext_xceiv->id == DWC3_ID_FLOAT)
298 set_bit(ID, &dotg->inputs);
299 else
300 clear_bit(ID, &dotg->inputs);
301
302 if (ext_xceiv->bsv)
303 set_bit(B_SESS_VLD, &dotg->inputs);
304 else
305 clear_bit(B_SESS_VLD, &dotg->inputs);
306
307 schedule_work(&dotg->sm_work);
Manu Gautamb5067272012-07-02 09:53:41 +0530308 }
Manu Gautamb5067272012-07-02 09:53:41 +0530309}
310
311/**
312 * dwc3_set_ext_xceiv - bind/unbind external transceiver driver
313 * @otg: Pointer to the otg transceiver structure
314 * @ext_xceiv: Pointer to the external transceiver struccture
315 *
316 * Returns 0 on success
317 */
318int dwc3_set_ext_xceiv(struct usb_otg *otg, struct dwc3_ext_xceiv *ext_xceiv)
319{
320 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
321
322 dotg->ext_xceiv = ext_xceiv;
323 if (ext_xceiv)
324 ext_xceiv->notify_ext_events = dwc3_ext_event_notify;
325
326 return 0;
327}
328
Manu Gautam8c642812012-06-07 10:35:10 +0530329/* IRQs which OTG driver is interested in handling */
330#define DWC3_OEVT_MASK (DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT | \
331 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)
332
333/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200334 * dwc3_otg_interrupt - interrupt handler for dwc3 otg events.
335 * @_dotg: Pointer to out controller context structure
336 *
337 * Returns IRQ_HANDLED on success otherwise IRQ_NONE.
338 */
339static irqreturn_t dwc3_otg_interrupt(int irq, void *_dotg)
340{
341 struct dwc3_otg *dotg = (struct dwc3_otg *)_dotg;
Manu Gautam8c642812012-06-07 10:35:10 +0530342 u32 osts, oevt_reg;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200343 int ret = IRQ_NONE;
344 int handled_irqs = 0;
345
346 oevt_reg = dwc3_readl(dotg->regs, DWC3_OEVT);
347
Manu Gautam8c642812012-06-07 10:35:10 +0530348 if (!(oevt_reg & DWC3_OEVT_MASK))
349 return IRQ_NONE;
350
351 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
352
353 if ((oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) ||
354 (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)) {
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200355 /*
Manu Gautam8c642812012-06-07 10:35:10 +0530356 * ID sts has changed, set inputs later, in the workqueue
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200357 * function, switch from A to B or from B to A.
358 */
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200359
Manu Gautam8c642812012-06-07 10:35:10 +0530360 if (osts & DWC3_OTG_OSTS_CONIDSTS)
361 set_bit(ID, &dotg->inputs);
362 else
363 clear_bit(ID, &dotg->inputs);
364
365 if (osts & DWC3_OTG_OSTS_BSESVALID)
366 set_bit(B_SESS_VLD, &dotg->inputs);
367 else
368 clear_bit(B_SESS_VLD, &dotg->inputs);
369
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200370 schedule_work(&dotg->sm_work);
371
Manu Gautam8c642812012-06-07 10:35:10 +0530372 handled_irqs |= (oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) ?
373 DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT : 0;
374 handled_irqs |= (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT) ?
375 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT : 0;
376
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200377 ret = IRQ_HANDLED;
Manu Gautam8c642812012-06-07 10:35:10 +0530378
379 /* Clear the interrupts we handled */
380 dwc3_writel(dotg->regs, DWC3_OEVT, handled_irqs);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200381 }
382
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200383 return ret;
384}
385
386/**
Manu Gautam8c642812012-06-07 10:35:10 +0530387 * dwc3_otg_init_sm - initialize OTG statemachine input
388 * @dotg: Pointer to the dwc3_otg structure
389 *
390 */
391void dwc3_otg_init_sm(struct dwc3_otg *dotg)
392{
393 u32 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
394 struct usb_phy *phy = dotg->otg.phy;
395
396 /*
397 * TODO: If using external notifications then wait here till initial
398 * state is reported
399 */
400
401 dev_dbg(phy->dev, "Initialize OTG inputs, osts: 0x%x\n", osts);
402
403 if (osts & DWC3_OTG_OSTS_CONIDSTS)
404 set_bit(ID, &dotg->inputs);
405 else
406 clear_bit(ID, &dotg->inputs);
407
408 if (osts & DWC3_OTG_OSTS_BSESVALID)
409 set_bit(B_SESS_VLD, &dotg->inputs);
410 else
411 clear_bit(B_SESS_VLD, &dotg->inputs);
412}
413
414/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200415 * dwc3_otg_sm_work - workqueue function.
416 *
417 * @w: Pointer to the dwc3 otg workqueue
418 *
419 * NOTE: After any change in phy->state,
420 * we must reschdule the state machine.
421 */
422static void dwc3_otg_sm_work(struct work_struct *w)
423{
424 struct dwc3_otg *dotg = container_of(w, struct dwc3_otg, sm_work);
425 struct usb_phy *phy = dotg->otg.phy;
Manu Gautam8c642812012-06-07 10:35:10 +0530426 struct dwc3_charger *charger = dotg->charger;
427 bool work = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200428
Manu Gautamb5067272012-07-02 09:53:41 +0530429 pm_runtime_resume(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200430 dev_dbg(phy->dev, "%s state\n", otg_state_string(phy->state));
431
432 /* Check OTG state */
433 switch (phy->state) {
434 case OTG_STATE_UNDEFINED:
Manu Gautam8c642812012-06-07 10:35:10 +0530435 dwc3_otg_init_sm(dotg);
436 /* Switch to A or B-Device according to ID / BSV */
437 if (!test_bit(ID, &dotg->inputs) && phy->otg->host) {
438 dev_dbg(phy->dev, "!id\n");
439 phy->state = OTG_STATE_A_IDLE;
440 work = 1;
441 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
442 dev_dbg(phy->dev, "b_sess_vld\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200443 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530444 work = 1;
445 } else {
446 phy->state = OTG_STATE_B_IDLE;
Manu Gautamb5067272012-07-02 09:53:41 +0530447 dev_dbg(phy->dev, "No device, trying to suspend\n");
448 pm_runtime_put_sync(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200449 }
450 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530451
452 case OTG_STATE_B_IDLE:
453 if (!test_bit(ID, &dotg->inputs) && phy->otg->host) {
454 dev_dbg(phy->dev, "!id\n");
455 phy->state = OTG_STATE_A_IDLE;
456 work = 1;
457 if (charger) {
458 if (charger->chg_type == DWC3_INVALID_CHARGER)
459 charger->start_detection(dotg->charger,
460 false);
461 else
462 charger->chg_type =
463 DWC3_INVALID_CHARGER;
464 }
465 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
466 dev_dbg(phy->dev, "b_sess_vld\n");
467 if (charger) {
468 /* Has charger been detected? If no detect it */
469 switch (charger->chg_type) {
470 case DWC3_DCP_CHARGER:
Manu Gautamb5067272012-07-02 09:53:41 +0530471 dev_dbg(phy->dev, "lpm, DCP charger\n");
472 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530473 break;
474 case DWC3_CDP_CHARGER:
475 dwc3_otg_start_peripheral(&dotg->otg,
476 1);
477 phy->state = OTG_STATE_B_PERIPHERAL;
478 work = 1;
479 break;
480 case DWC3_SDP_CHARGER:
481 dwc3_otg_start_peripheral(&dotg->otg,
482 1);
483 phy->state = OTG_STATE_B_PERIPHERAL;
484 work = 1;
485 break;
486 default:
487 dev_dbg(phy->dev, "chg_det started\n");
488 charger->start_detection(charger, true);
489 break;
490 }
491 } else {
492 /* no charger registered, start peripheral */
493 if (dwc3_otg_start_peripheral(&dotg->otg, 1)) {
494 /*
495 * Probably set_peripheral not called
496 * yet. We will re-try as soon as it
497 * will be called
498 */
Manu Gautamb5067272012-07-02 09:53:41 +0530499 dev_err(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530500 "unable to start B-device\n");
501 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530502 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530503 return;
504 }
505 }
506 } else {
507 if (charger) {
508 if (charger->chg_type == DWC3_INVALID_CHARGER)
509 charger->start_detection(dotg->charger,
510 false);
511 else
512 charger->chg_type =
513 DWC3_INVALID_CHARGER;
514 }
Manu Gautamb5067272012-07-02 09:53:41 +0530515 dev_dbg(phy->dev, "No device, trying to suspend\n");
516 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530517 }
518 break;
519
520 case OTG_STATE_B_PERIPHERAL:
521 if (!test_bit(B_SESS_VLD, &dotg->inputs) ||
522 !test_bit(ID, &dotg->inputs)) {
523 dev_dbg(phy->dev, "!id || !bsv\n");
524 dwc3_otg_start_peripheral(&dotg->otg, 0);
525 phy->state = OTG_STATE_B_IDLE;
526 if (charger)
527 charger->chg_type = DWC3_INVALID_CHARGER;
528 work = 1;
529 }
530 break;
531
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200532 case OTG_STATE_A_IDLE:
533 /* Switch to A-Device*/
Manu Gautam8c642812012-06-07 10:35:10 +0530534 if (test_bit(ID, &dotg->inputs)) {
535 dev_dbg(phy->dev, "id\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200536 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530537 work = 1;
538 } else {
539 if (dwc3_otg_start_host(&dotg->otg, 1)) {
540 /*
541 * Probably set_host was not called yet.
542 * We will re-try as soon as it will be called
543 */
Manu Gautamb5067272012-07-02 09:53:41 +0530544 dev_dbg(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530545 "unable to start A-device\n");
546 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530547 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530548 return;
549 }
550 phy->state = OTG_STATE_A_HOST;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200551 }
552 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530553
554 case OTG_STATE_A_HOST:
555 if (test_bit(ID, &dotg->inputs)) {
556 dev_dbg(phy->dev, "id\n");
557 dwc3_otg_start_host(&dotg->otg, 0);
558 phy->state = OTG_STATE_B_IDLE;
559 work = 1;
560 }
561 break;
562
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200563 default:
564 dev_err(phy->dev, "%s: invalid otg-state\n", __func__);
565
566 }
Manu Gautam8c642812012-06-07 10:35:10 +0530567
568 if (work)
569 schedule_work(&dotg->sm_work);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200570}
571
572
573/**
574 * dwc3_otg_reset - reset dwc3 otg registers.
575 *
576 * @w: Pointer to the dwc3 otg workqueue
577 */
578static void dwc3_otg_reset(struct dwc3_otg *dotg)
579{
580 /*
581 * OCFG[2] - OTG-Version = 1
582 * OCFG[1] - HNPCap = 0
583 * OCFG[0] - SRPCap = 0
584 */
585 dwc3_writel(dotg->regs, DWC3_OCFG, 0x4);
586
587 /*
588 * OCTL[6] - PeriMode = 1
589 * OCTL[5] - PrtPwrCtl = 0
590 * OCTL[4] - HNPReq = 0
591 * OCTL[3] - SesReq = 0
592 * OCTL[2] - TermSelDLPulse = 0
593 * OCTL[1] - DevSetHNPEn = 0
594 * OCTL[0] - HstSetHNPEn = 0
595 */
596 dwc3_writel(dotg->regs, DWC3_OCTL, 0x40);
597
598 /* Clear all otg events (interrupts) indications */
599 dwc3_writel(dotg->regs, DWC3_OEVT, 0xFFFF);
600
Manu Gautam8c642812012-06-07 10:35:10 +0530601 /* Enable ID/BSV StsChngEn event*/
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200602 dwc3_writel(dotg->regs, DWC3_OEVTEN,
Manu Gautam8c642812012-06-07 10:35:10 +0530603 DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT |
604 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200605}
606
607/**
608 * dwc3_otg_init - Initializes otg related registers
609 * @dwc: Pointer to out controller context structure
610 *
611 * Returns 0 on success otherwise negative errno.
612 */
613int dwc3_otg_init(struct dwc3 *dwc)
614{
615 u32 reg;
616 int ret = 0;
617 struct dwc3_otg *dotg;
618
619 dev_dbg(dwc->dev, "dwc3_otg_init\n");
620
621 /*
622 * GHWPARAMS6[10] bit is SRPSupport.
623 * This bit also reflects DWC_USB3_EN_OTG
624 */
625 reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
626 if (!(reg & DWC3_GHWPARAMS6_SRP_SUPPORT)) {
627 /*
628 * No OTG support in the HW core.
629 * We return 0 to indicate no error, since this is acceptable
630 * situation, just continue probe the dwc3 driver without otg.
631 */
632 dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n");
633 return 0;
634 }
635
636 /* Allocate and init otg instance */
637 dotg = kzalloc(sizeof(struct dwc3_otg), GFP_KERNEL);
638 if (!dotg) {
639 dev_err(dwc->dev, "unable to allocate dwc3_otg\n");
640 return -ENOMEM;
641 }
642
Manu Gautam17206c22012-06-21 10:17:53 +0530643 /* DWC3 has separate IRQ line for OTG events (ID/BSV etc.) */
644 dotg->irq = platform_get_irq_byname(to_platform_device(dwc->dev),
645 "otg_irq");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200646 if (dotg->irq < 0) {
Manu Gautam17206c22012-06-21 10:17:53 +0530647 dev_err(dwc->dev, "%s: missing OTG IRQ\n", __func__);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200648 ret = -ENODEV;
649 goto err1;
650 }
651
652 dotg->regs = dwc->regs;
653
654 dotg->otg.set_peripheral = dwc3_otg_set_peripheral;
655 dotg->otg.set_host = dwc3_otg_set_host;
656
657 /* This reference is used by dwc3 modules for checking otg existance */
658 dwc->dotg = dotg;
659
660 dotg->otg.phy = kzalloc(sizeof(struct usb_phy), GFP_KERNEL);
661 if (!dotg->otg.phy) {
662 dev_err(dwc->dev, "unable to allocate dwc3_otg.phy\n");
663 ret = -ENOMEM;
664 goto err1;
665 }
666
667 dotg->otg.phy->otg = &dotg->otg;
668 dotg->otg.phy->dev = dwc->dev;
669
670 ret = usb_set_transceiver(dotg->otg.phy);
671 if (ret) {
672 dev_err(dotg->otg.phy->dev,
673 "%s: failed to set transceiver, already exists\n",
674 __func__);
675 goto err2;
676 }
677
678 dwc3_otg_reset(dotg);
679
680 dotg->otg.phy->state = OTG_STATE_UNDEFINED;
681
682 INIT_WORK(&dotg->sm_work, dwc3_otg_sm_work);
683
684 ret = request_irq(dotg->irq, dwc3_otg_interrupt, IRQF_SHARED,
685 "dwc3_otg", dotg);
686 if (ret) {
687 dev_err(dotg->otg.phy->dev, "failed to request irq #%d --> %d\n",
688 dotg->irq, ret);
689 goto err3;
690 }
691
Manu Gautamb5067272012-07-02 09:53:41 +0530692 pm_runtime_get(dwc->dev);
693
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200694 return 0;
695
696err3:
697 cancel_work_sync(&dotg->sm_work);
698 usb_set_transceiver(NULL);
699err2:
700 kfree(dotg->otg.phy);
701err1:
702 dwc->dotg = NULL;
703 kfree(dotg);
704
705 return ret;
706}
707
708/**
709 * dwc3_otg_exit
710 * @dwc: Pointer to out controller context structure
711 *
712 * Returns 0 on success otherwise negative errno.
713 */
714void dwc3_otg_exit(struct dwc3 *dwc)
715{
716 struct dwc3_otg *dotg = dwc->dotg;
717
718 /* dotg is null when GHWPARAMS6[10]=SRPSupport=0, see dwc3_otg_init */
719 if (dotg) {
Manu Gautam8c642812012-06-07 10:35:10 +0530720 if (dotg->charger)
721 dotg->charger->start_detection(dotg->charger, false);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200722 cancel_work_sync(&dotg->sm_work);
723 usb_set_transceiver(NULL);
Manu Gautamb5067272012-07-02 09:53:41 +0530724 pm_runtime_put(dwc->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200725 free_irq(dotg->irq, dotg);
726 kfree(dotg->otg.phy);
727 kfree(dotg);
728 dwc->dotg = NULL;
729 }
730}