Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Cadence USBSS and USBSSP DRD Driver.
4 *
5 * Copyright (C) 2018-2020 Cadence.
6 * Copyright (C) 2019 Texas Instruments
7 *
8 * Author: Pawel Laszczak <pawell@cadence.com>
9 * Roger Quadros <rogerq@ti.com>
10 *
11 */
12#include <linux/kernel.h>
13#include <linux/interrupt.h>
14#include <linux/delay.h>
15#include <linux/iopoll.h>
16#include <linux/usb/otg.h>
17
18#include "drd.h"
19#include "core.h"
20
21/**
22 * cdns_set_mode - change mode of OTG Core
23 * @cdns: pointer to context structure
24 * @mode: selected mode from cdns_role
25 *
26 * Returns 0 on success otherwise negative errno
27 */
28static int cdns_set_mode(struct cdns *cdns, enum usb_dr_mode mode)
29{
30 void __iomem *override_reg;
31 u32 reg;
32
33 switch (mode) {
34 case USB_DR_MODE_PERIPHERAL:
35 break;
36 case USB_DR_MODE_HOST:
37 break;
38 case USB_DR_MODE_OTG:
39 dev_dbg(cdns->dev, "Set controller to OTG mode\n");
40
41 if (cdns->version == CDNSP_CONTROLLER_V2)
42 override_reg = &cdns->otg_cdnsp_regs->override;
43 else if (cdns->version == CDNS3_CONTROLLER_V1)
44 override_reg = &cdns->otg_v1_regs->override;
45 else
46 override_reg = &cdns->otg_v0_regs->ctrl1;
47
48 reg = readl(override_reg);
49
50 if (cdns->version != CDNS3_CONTROLLER_V0)
51 reg |= OVERRIDE_IDPULLUP;
52 else
53 reg |= OVERRIDE_IDPULLUP_V0;
54
55 writel(reg, override_reg);
56
57 if (cdns->version == CDNS3_CONTROLLER_V1) {
58 /*
59 * Enable work around feature built into the
60 * controller to address issue with RX Sensitivity
61 * est (EL_17) for USB2 PHY. The issue only occures
62 * for 0x0002450D controller version.
63 */
64 if (cdns->phyrst_a_enable) {
65 reg = readl(&cdns->otg_v1_regs->phyrst_cfg);
66 reg |= PHYRST_CFG_PHYRST_A_ENABLE;
67 writel(reg, &cdns->otg_v1_regs->phyrst_cfg);
68 }
69 }
70
71 /*
72 * Hardware specification says: "ID_VALUE must be valid within
73 * 50ms after idpullup is set to '1" so driver must wait
74 * 50ms before reading this pin.
75 */
76 usleep_range(50000, 60000);
77 break;
78 default:
79 dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode);
80 return -EINVAL;
81 }
82
83 return 0;
84}
85
86int cdns_get_id(struct cdns *cdns)
87{
88 int id;
89
90 id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE;
91 dev_dbg(cdns->dev, "OTG ID: %d", id);
92
93 return id;
94}
95
96int cdns_get_vbus(struct cdns *cdns)
97{
98 int vbus;
99
100 vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID);
101 dev_dbg(cdns->dev, "OTG VBUS: %d", vbus);
102
103 return vbus;
104}
105
106void cdns_clear_vbus(struct cdns *cdns)
107{
108 u32 reg;
109
110 if (cdns->version != CDNSP_CONTROLLER_V2)
111 return;
112
113 reg = readl(&cdns->otg_cdnsp_regs->override);
114 reg |= OVERRIDE_SESS_VLD_SEL;
115 writel(reg, &cdns->otg_cdnsp_regs->override);
116}
117EXPORT_SYMBOL_GPL(cdns_clear_vbus);
118
119void cdns_set_vbus(struct cdns *cdns)
120{
121 u32 reg;
122
123 if (cdns->version != CDNSP_CONTROLLER_V2)
124 return;
125
126 reg = readl(&cdns->otg_cdnsp_regs->override);
127 reg &= ~OVERRIDE_SESS_VLD_SEL;
128 writel(reg, &cdns->otg_cdnsp_regs->override);
129}
130EXPORT_SYMBOL_GPL(cdns_set_vbus);
131
132bool cdns_is_host(struct cdns *cdns)
133{
134 if (cdns->dr_mode == USB_DR_MODE_HOST)
135 return true;
136 else if (cdns_get_id(cdns) == CDNS3_ID_HOST)
137 return true;
138
139 return false;
140}
141
142bool cdns_is_device(struct cdns *cdns)
143{
144 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL)
145 return true;
146 else if (cdns->dr_mode == USB_DR_MODE_OTG)
147 if (cdns_get_id(cdns) == CDNS3_ID_PERIPHERAL)
148 return true;
149
150 return false;
151}
152
153/**
154 * cdns_otg_disable_irq - Disable all OTG interrupts
155 * @cdns: Pointer to controller context structure
156 */
157static void cdns_otg_disable_irq(struct cdns *cdns)
158{
159 if (cdns->version)
160 writel(0, &cdns->otg_irq_regs->ien);
161}
162
163/**
164 * cdns_otg_enable_irq - enable id and sess_valid interrupts
165 * @cdns: Pointer to controller context structure
166 */
167static void cdns_otg_enable_irq(struct cdns *cdns)
168{
169 writel(OTGIEN_ID_CHANGE_INT | OTGIEN_VBUSVALID_RISE_INT |
170 OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_irq_regs->ien);
171}
172
173/**
174 * cdns_drd_host_on - start host.
175 * @cdns: Pointer to controller context structure.
176 *
177 * Returns 0 on success otherwise negative errno.
178 */
179int cdns_drd_host_on(struct cdns *cdns)
180{
181 u32 val, ready_bit;
182 int ret;
183
184 /* Enable host mode. */
185 writel(OTGCMD_HOST_BUS_REQ | OTGCMD_OTG_DIS,
186 &cdns->otg_regs->cmd);
187
188 if (cdns->version == CDNSP_CONTROLLER_V2)
189 ready_bit = OTGSTS_CDNSP_XHCI_READY;
190 else
191 ready_bit = OTGSTS_CDNS3_XHCI_READY;
192
193 dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n");
194 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
195 val & ready_bit, 1, 100000);
196
197 if (ret)
198 dev_err(cdns->dev, "timeout waiting for xhci_ready\n");
199
200 phy_set_mode(cdns->usb2_phy, PHY_MODE_USB_HOST);
201 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_HOST);
202 return ret;
203}
204
205/**
206 * cdns_drd_host_off - stop host.
207 * @cdns: Pointer to controller context structure.
208 */
209void cdns_drd_host_off(struct cdns *cdns)
210{
211 u32 val;
212
213 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
214 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
215 &cdns->otg_regs->cmd);
216
217 /* Waiting till H_IDLE state.*/
218 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
219 !(val & OTGSTATE_HOST_STATE_MASK),
220 1, 2000000);
221 phy_set_mode(cdns->usb2_phy, PHY_MODE_INVALID);
222 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
223}
224
225/**
226 * cdns_drd_gadget_on - start gadget.
227 * @cdns: Pointer to controller context structure.
228 *
229 * Returns 0 on success otherwise negative errno
230 */
231int cdns_drd_gadget_on(struct cdns *cdns)
232{
233 u32 reg = OTGCMD_OTG_DIS;
234 u32 ready_bit;
235 int ret, val;
236
237 /* switch OTG core */
238 writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd);
239
240 dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n");
241
242 if (cdns->version == CDNSP_CONTROLLER_V2)
243 ready_bit = OTGSTS_CDNSP_DEV_READY;
244 else
245 ready_bit = OTGSTS_CDNS3_DEV_READY;
246
247 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
248 val & ready_bit, 1, 100000);
249 if (ret) {
250 dev_err(cdns->dev, "timeout waiting for dev_ready\n");
251 return ret;
252 }
253
254 phy_set_mode(cdns->usb2_phy, PHY_MODE_USB_DEVICE);
255 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_DEVICE);
256 return 0;
257}
258EXPORT_SYMBOL_GPL(cdns_drd_gadget_on);
259
260/**
261 * cdns_drd_gadget_off - stop gadget.
262 * @cdns: Pointer to controller context structure.
263 */
264void cdns_drd_gadget_off(struct cdns *cdns)
265{
266 u32 val;
267
268 /*
269 * Driver should wait at least 10us after disabling Device
270 * before turning-off Device (DEV_BUS_DROP).
271 */
272 usleep_range(20, 30);
273 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
274 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
275 &cdns->otg_regs->cmd);
276 /* Waiting till DEV_IDLE state.*/
277 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
278 !(val & OTGSTATE_DEV_STATE_MASK),
279 1, 2000000);
280 phy_set_mode(cdns->usb2_phy, PHY_MODE_INVALID);
281 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
282}
283EXPORT_SYMBOL_GPL(cdns_drd_gadget_off);
284
285/**
286 * cdns_init_otg_mode - initialize drd controller
287 * @cdns: Pointer to controller context structure
288 *
289 * Returns 0 on success otherwise negative errno
290 */
291static int cdns_init_otg_mode(struct cdns *cdns)
292{
293 int ret;
294
295 cdns_otg_disable_irq(cdns);
296 /* clear all interrupts */
297 writel(~0, &cdns->otg_irq_regs->ivect);
298
299 ret = cdns_set_mode(cdns, USB_DR_MODE_OTG);
300 if (ret)
301 return ret;
302
303 cdns_otg_enable_irq(cdns);
304
305 return 0;
306}
307
308/**
309 * cdns_drd_update_mode - initialize mode of operation
310 * @cdns: Pointer to controller context structure
311 *
312 * Returns 0 on success otherwise negative errno
313 */
314int cdns_drd_update_mode(struct cdns *cdns)
315{
316 int ret;
317
318 switch (cdns->dr_mode) {
319 case USB_DR_MODE_PERIPHERAL:
320 ret = cdns_set_mode(cdns, USB_DR_MODE_PERIPHERAL);
321 break;
322 case USB_DR_MODE_HOST:
323 ret = cdns_set_mode(cdns, USB_DR_MODE_HOST);
324 break;
325 case USB_DR_MODE_OTG:
326 ret = cdns_init_otg_mode(cdns);
327 break;
328 default:
329 dev_err(cdns->dev, "Unsupported mode of operation %d\n",
330 cdns->dr_mode);
331 return -EINVAL;
332 }
333
334 return ret;
335}
336
337static irqreturn_t cdns_drd_thread_irq(int irq, void *data)
338{
339 struct cdns *cdns = data;
340
341 cdns_hw_role_switch(cdns);
342
343 return IRQ_HANDLED;
344}
345
346/**
347 * cdns_drd_irq - interrupt handler for OTG events
348 *
349 * @irq: irq number for cdns core device
350 * @data: structure of cdns
351 *
352 * Returns IRQ_HANDLED or IRQ_NONE
353 */
354static irqreturn_t cdns_drd_irq(int irq, void *data)
355{
356 irqreturn_t ret = IRQ_NONE;
357 struct cdns *cdns = data;
358 u32 reg;
359
360 if (cdns->dr_mode != USB_DR_MODE_OTG)
361 return IRQ_NONE;
362
363 if (cdns->in_lpm)
364 return ret;
365
366 reg = readl(&cdns->otg_irq_regs->ivect);
367
368 if (!reg)
369 return IRQ_NONE;
370
371 if (reg & OTGIEN_ID_CHANGE_INT) {
372 dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n",
373 cdns_get_id(cdns));
374
375 ret = IRQ_WAKE_THREAD;
376 }
377
378 if (reg & (OTGIEN_VBUSVALID_RISE_INT | OTGIEN_VBUSVALID_FALL_INT)) {
379 dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n",
380 cdns_get_vbus(cdns));
381
382 ret = IRQ_WAKE_THREAD;
383 }
384
385 writel(~0, &cdns->otg_irq_regs->ivect);
386 return ret;
387}
388
389int cdns_drd_init(struct cdns *cdns)
390{
391 void __iomem *regs;
392 u32 state;
393 int ret;
394
395 regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res);
396 if (IS_ERR(regs))
397 return PTR_ERR(regs);
398
399 /* Detection of DRD version. Controller has been released
400 * in three versions. All are very similar and are software compatible,
401 * but they have same changes in register maps.
402 * The first register in oldest version is command register and it's
403 * read only. Driver should read 0 from it. On the other hand, in v1
404 * and v2 the first register contains device ID number which is not
405 * set to 0. Driver uses this fact to detect the proper version of
406 * controller.
407 */
408 cdns->otg_v0_regs = regs;
409 if (!readl(&cdns->otg_v0_regs->cmd)) {
410 cdns->version = CDNS3_CONTROLLER_V0;
411 cdns->otg_v1_regs = NULL;
412 cdns->otg_cdnsp_regs = NULL;
413 cdns->otg_regs = regs;
414 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
415 &cdns->otg_v0_regs->ien;
416 writel(1, &cdns->otg_v0_regs->simulate);
417 dev_dbg(cdns->dev, "DRD version v0 (%08x)\n",
418 readl(&cdns->otg_v0_regs->version));
419 } else {
420 cdns->otg_v0_regs = NULL;
421 cdns->otg_v1_regs = regs;
422 cdns->otg_cdnsp_regs = regs;
423
424 cdns->otg_regs = (void __iomem *)&cdns->otg_v1_regs->cmd;
425
426 state = readl(&cdns->otg_cdnsp_regs->did);
427
428 if (OTG_CDNSP_CHECK_DID(state)) {
429 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
430 &cdns->otg_cdnsp_regs->ien;
431 cdns->version = CDNSP_CONTROLLER_V2;
432 } else if (OTG_CDNS3_CHECK_DID(state)) {
433 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
434 &cdns->otg_v1_regs->ien;
435 writel(1, &cdns->otg_v1_regs->simulate);
436 cdns->version = CDNS3_CONTROLLER_V1;
437 } else {
438 dev_err(cdns->dev, "not supporte DID=0x%08x\n", state);
439 return -EINVAL;
440 }
441
442 dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n",
443 readl(&cdns->otg_v1_regs->did),
444 readl(&cdns->otg_v1_regs->rid));
445 }
446
447 state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts));
448
449 /* Update dr_mode according to STRAP configuration. */
450 cdns->dr_mode = USB_DR_MODE_OTG;
451
452 if ((cdns->version == CDNSP_CONTROLLER_V2 &&
453 state == OTGSTS_CDNSP_STRAP_HOST) ||
454 (cdns->version != CDNSP_CONTROLLER_V2 &&
455 state == OTGSTS_STRAP_HOST)) {
456 dev_dbg(cdns->dev, "Controller strapped to HOST\n");
457 cdns->dr_mode = USB_DR_MODE_HOST;
458 } else if ((cdns->version == CDNSP_CONTROLLER_V2 &&
459 state == OTGSTS_CDNSP_STRAP_GADGET) ||
460 (cdns->version != CDNSP_CONTROLLER_V2 &&
461 state == OTGSTS_STRAP_GADGET)) {
462 dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n");
463 cdns->dr_mode = USB_DR_MODE_PERIPHERAL;
464 }
465
466 ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq,
467 cdns_drd_irq,
468 cdns_drd_thread_irq,
469 IRQF_SHARED,
470 dev_name(cdns->dev), cdns);
471 if (ret) {
472 dev_err(cdns->dev, "couldn't get otg_irq\n");
473 return ret;
474 }
475
476 state = readl(&cdns->otg_regs->sts);
477 if (OTGSTS_OTG_NRDY(state)) {
478 dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n");
479 return -ENODEV;
480 }
481
482 return 0;
483}
484
485int cdns_drd_exit(struct cdns *cdns)
486{
487 cdns_otg_disable_irq(cdns);
488
489 return 0;
490}
491
492/* Indicate the cdns3 core was power lost before */
493bool cdns_power_is_lost(struct cdns *cdns)
494{
495 if (cdns->version == CDNS3_CONTROLLER_V0) {
496 if (!(readl(&cdns->otg_v0_regs->simulate) & BIT(0)))
497 return true;
498 } else {
499 if (!(readl(&cdns->otg_v1_regs->simulate) & BIT(0)))
500 return true;
501 }
502 return false;
503}
504EXPORT_SYMBOL_GPL(cdns_power_is_lost);
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Cadence USBSS DRD Driver.
4 *
5 * Copyright (C) 2018-2019 Cadence.
6 * Copyright (C) 2019 Texas Instruments
7 *
8 * Author: Pawel Laszczak <pawell@cadence.com>
9 * Roger Quadros <rogerq@ti.com>
10 *
11 *
12 */
13#include <linux/kernel.h>
14#include <linux/interrupt.h>
15#include <linux/delay.h>
16#include <linux/iopoll.h>
17#include <linux/usb/otg.h>
18
19#include "gadget.h"
20#include "drd.h"
21#include "core.h"
22
23/**
24 * cdns3_set_mode - change mode of OTG Core
25 * @cdns: pointer to context structure
26 * @mode: selected mode from cdns_role
27 *
28 * Returns 0 on success otherwise negative errno
29 */
30int cdns3_set_mode(struct cdns3 *cdns, enum usb_dr_mode mode)
31{
32 u32 reg;
33
34 switch (mode) {
35 case USB_DR_MODE_PERIPHERAL:
36 break;
37 case USB_DR_MODE_HOST:
38 break;
39 case USB_DR_MODE_OTG:
40 dev_dbg(cdns->dev, "Set controller to OTG mode\n");
41 if (cdns->version == CDNS3_CONTROLLER_V1) {
42 reg = readl(&cdns->otg_v1_regs->override);
43 reg |= OVERRIDE_IDPULLUP;
44 writel(reg, &cdns->otg_v1_regs->override);
45 } else {
46 reg = readl(&cdns->otg_v0_regs->ctrl1);
47 reg |= OVERRIDE_IDPULLUP_V0;
48 writel(reg, &cdns->otg_v0_regs->ctrl1);
49 }
50
51 /*
52 * Hardware specification says: "ID_VALUE must be valid within
53 * 50ms after idpullup is set to '1" so driver must wait
54 * 50ms before reading this pin.
55 */
56 usleep_range(50000, 60000);
57 break;
58 default:
59 dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode);
60 return -EINVAL;
61 }
62
63 return 0;
64}
65
66int cdns3_get_id(struct cdns3 *cdns)
67{
68 int id;
69
70 id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE;
71 dev_dbg(cdns->dev, "OTG ID: %d", id);
72
73 return id;
74}
75
76int cdns3_get_vbus(struct cdns3 *cdns)
77{
78 int vbus;
79
80 vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID);
81 dev_dbg(cdns->dev, "OTG VBUS: %d", vbus);
82
83 return vbus;
84}
85
86bool cdns3_is_host(struct cdns3 *cdns)
87{
88 if (cdns->dr_mode == USB_DR_MODE_HOST)
89 return true;
90 else if (cdns3_get_id(cdns) == CDNS3_ID_HOST)
91 return true;
92
93 return false;
94}
95
96bool cdns3_is_device(struct cdns3 *cdns)
97{
98 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL)
99 return true;
100 else if (cdns->dr_mode == USB_DR_MODE_OTG)
101 if (cdns3_get_id(cdns) == CDNS3_ID_PERIPHERAL)
102 return true;
103
104 return false;
105}
106
107/**
108 * cdns3_otg_disable_irq - Disable all OTG interrupts
109 * @cdns: Pointer to controller context structure
110 */
111static void cdns3_otg_disable_irq(struct cdns3 *cdns)
112{
113 writel(0, &cdns->otg_regs->ien);
114}
115
116/**
117 * cdns3_otg_enable_irq - enable id and sess_valid interrupts
118 * @cdns: Pointer to controller context structure
119 */
120static void cdns3_otg_enable_irq(struct cdns3 *cdns)
121{
122 writel(OTGIEN_ID_CHANGE_INT | OTGIEN_VBUSVALID_RISE_INT |
123 OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_regs->ien);
124}
125
126/**
127 * cdns3_drd_host_on - start host.
128 * @cdns: Pointer to controller context structure.
129 *
130 * Returns 0 on success otherwise negative errno.
131 */
132int cdns3_drd_host_on(struct cdns3 *cdns)
133{
134 u32 val;
135 int ret;
136
137 /* Enable host mode. */
138 writel(OTGCMD_HOST_BUS_REQ | OTGCMD_OTG_DIS,
139 &cdns->otg_regs->cmd);
140
141 dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n");
142 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
143 val & OTGSTS_XHCI_READY, 1, 100000);
144
145 if (ret)
146 dev_err(cdns->dev, "timeout waiting for xhci_ready\n");
147
148 return ret;
149}
150
151/**
152 * cdns3_drd_host_off - stop host.
153 * @cdns: Pointer to controller context structure.
154 */
155void cdns3_drd_host_off(struct cdns3 *cdns)
156{
157 u32 val;
158
159 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
160 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
161 &cdns->otg_regs->cmd);
162
163 /* Waiting till H_IDLE state.*/
164 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
165 !(val & OTGSTATE_HOST_STATE_MASK),
166 1, 2000000);
167}
168
169/**
170 * cdns3_drd_gadget_on - start gadget.
171 * @cdns: Pointer to controller context structure.
172 *
173 * Returns 0 on success otherwise negative errno
174 */
175int cdns3_drd_gadget_on(struct cdns3 *cdns)
176{
177 int ret, val;
178 u32 reg = OTGCMD_OTG_DIS;
179
180 /* switch OTG core */
181 writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd);
182
183 dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n");
184
185 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
186 val & OTGSTS_DEV_READY,
187 1, 100000);
188 if (ret) {
189 dev_err(cdns->dev, "timeout waiting for dev_ready\n");
190 return ret;
191 }
192
193 return 0;
194}
195
196/**
197 * cdns3_drd_gadget_off - stop gadget.
198 * @cdns: Pointer to controller context structure.
199 */
200void cdns3_drd_gadget_off(struct cdns3 *cdns)
201{
202 u32 val;
203
204 /*
205 * Driver should wait at least 10us after disabling Device
206 * before turning-off Device (DEV_BUS_DROP).
207 */
208 usleep_range(20, 30);
209 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
210 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
211 &cdns->otg_regs->cmd);
212 /* Waiting till DEV_IDLE state.*/
213 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
214 !(val & OTGSTATE_DEV_STATE_MASK),
215 1, 2000000);
216}
217
218/**
219 * cdns3_init_otg_mode - initialize drd controller
220 * @cdns: Pointer to controller context structure
221 *
222 * Returns 0 on success otherwise negative errno
223 */
224static int cdns3_init_otg_mode(struct cdns3 *cdns)
225{
226 int ret;
227
228 cdns3_otg_disable_irq(cdns);
229 /* clear all interrupts */
230 writel(~0, &cdns->otg_regs->ivect);
231
232 ret = cdns3_set_mode(cdns, USB_DR_MODE_OTG);
233 if (ret)
234 return ret;
235
236 cdns3_otg_enable_irq(cdns);
237
238 return 0;
239}
240
241/**
242 * cdns3_drd_update_mode - initialize mode of operation
243 * @cdns: Pointer to controller context structure
244 *
245 * Returns 0 on success otherwise negative errno
246 */
247int cdns3_drd_update_mode(struct cdns3 *cdns)
248{
249 int ret;
250
251 switch (cdns->dr_mode) {
252 case USB_DR_MODE_PERIPHERAL:
253 ret = cdns3_set_mode(cdns, USB_DR_MODE_PERIPHERAL);
254 break;
255 case USB_DR_MODE_HOST:
256 ret = cdns3_set_mode(cdns, USB_DR_MODE_HOST);
257 break;
258 case USB_DR_MODE_OTG:
259 ret = cdns3_init_otg_mode(cdns);
260 break;
261 default:
262 dev_err(cdns->dev, "Unsupported mode of operation %d\n",
263 cdns->dr_mode);
264 return -EINVAL;
265 }
266
267 return ret;
268}
269
270static irqreturn_t cdns3_drd_thread_irq(int irq, void *data)
271{
272 struct cdns3 *cdns = data;
273
274 cdns3_hw_role_switch(cdns);
275
276 return IRQ_HANDLED;
277}
278
279/**
280 * cdns3_drd_irq - interrupt handler for OTG events
281 *
282 * @irq: irq number for cdns3 core device
283 * @data: structure of cdns3
284 *
285 * Returns IRQ_HANDLED or IRQ_NONE
286 */
287static irqreturn_t cdns3_drd_irq(int irq, void *data)
288{
289 irqreturn_t ret = IRQ_NONE;
290 struct cdns3 *cdns = data;
291 u32 reg;
292
293 if (cdns->dr_mode != USB_DR_MODE_OTG)
294 return IRQ_NONE;
295
296 reg = readl(&cdns->otg_regs->ivect);
297
298 if (!reg)
299 return IRQ_NONE;
300
301 if (reg & OTGIEN_ID_CHANGE_INT) {
302 dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n",
303 cdns3_get_id(cdns));
304
305 ret = IRQ_WAKE_THREAD;
306 }
307
308 if (reg & (OTGIEN_VBUSVALID_RISE_INT | OTGIEN_VBUSVALID_FALL_INT)) {
309 dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n",
310 cdns3_get_vbus(cdns));
311
312 ret = IRQ_WAKE_THREAD;
313 }
314
315 writel(~0, &cdns->otg_regs->ivect);
316 return ret;
317}
318
319int cdns3_drd_init(struct cdns3 *cdns)
320{
321 void __iomem *regs;
322 u32 state;
323 int ret;
324
325 regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res);
326 if (IS_ERR(regs))
327 return PTR_ERR(regs);
328
329 /* Detection of DRD version. Controller has been released
330 * in two versions. Both are similar, but they have same changes
331 * in register maps.
332 * The first register in old version is command register and it's read
333 * only, so driver should read 0 from it. On the other hand, in v1
334 * the first register contains device ID number which is not set to 0.
335 * Driver uses this fact to detect the proper version of
336 * controller.
337 */
338 cdns->otg_v0_regs = regs;
339 if (!readl(&cdns->otg_v0_regs->cmd)) {
340 cdns->version = CDNS3_CONTROLLER_V0;
341 cdns->otg_v1_regs = NULL;
342 cdns->otg_regs = regs;
343 writel(1, &cdns->otg_v0_regs->simulate);
344 dev_dbg(cdns->dev, "DRD version v0 (%08x)\n",
345 readl(&cdns->otg_v0_regs->version));
346 } else {
347 cdns->otg_v0_regs = NULL;
348 cdns->otg_v1_regs = regs;
349 cdns->otg_regs = (void *)&cdns->otg_v1_regs->cmd;
350 cdns->version = CDNS3_CONTROLLER_V1;
351 writel(1, &cdns->otg_v1_regs->simulate);
352 dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n",
353 readl(&cdns->otg_v1_regs->did),
354 readl(&cdns->otg_v1_regs->rid));
355 }
356
357 state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts));
358
359 /* Update dr_mode according to STRAP configuration. */
360 cdns->dr_mode = USB_DR_MODE_OTG;
361 if (state == OTGSTS_STRAP_HOST) {
362 dev_dbg(cdns->dev, "Controller strapped to HOST\n");
363 cdns->dr_mode = USB_DR_MODE_HOST;
364 } else if (state == OTGSTS_STRAP_GADGET) {
365 dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n");
366 cdns->dr_mode = USB_DR_MODE_PERIPHERAL;
367 }
368
369 ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq,
370 cdns3_drd_irq,
371 cdns3_drd_thread_irq,
372 IRQF_SHARED,
373 dev_name(cdns->dev), cdns);
374 if (ret) {
375 dev_err(cdns->dev, "couldn't get otg_irq\n");
376 return ret;
377 }
378
379 state = readl(&cdns->otg_regs->sts);
380 if (OTGSTS_OTG_NRDY(state)) {
381 dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n");
382 return -ENODEV;
383 }
384
385 return 0;
386}
387
388int cdns3_drd_exit(struct cdns3 *cdns)
389{
390 cdns3_otg_disable_irq(cdns);
391 return 0;
392}