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 */
cdns_set_mode(struct cdns * cdns,enum usb_dr_mode mode)28 static 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
cdns_get_id(struct cdns * cdns)86 int 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
cdns_get_vbus(struct cdns * cdns)96 int 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
cdns_clear_vbus(struct cdns * cdns)106 void 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 }
117 EXPORT_SYMBOL_GPL(cdns_clear_vbus);
118
cdns_set_vbus(struct cdns * cdns)119 void 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 }
130 EXPORT_SYMBOL_GPL(cdns_set_vbus);
131
cdns_is_host(struct cdns * cdns)132 bool 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
cdns_is_device(struct cdns * cdns)142 bool 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 */
cdns_otg_disable_irq(struct cdns * cdns)157 static void cdns_otg_disable_irq(struct cdns *cdns)
158 {
159 writel(0, &cdns->otg_irq_regs->ien);
160 }
161
162 /**
163 * cdns_otg_enable_irq - enable id and sess_valid interrupts
164 * @cdns: Pointer to controller context structure
165 */
cdns_otg_enable_irq(struct cdns * cdns)166 static void cdns_otg_enable_irq(struct cdns *cdns)
167 {
168 writel(OTGIEN_ID_CHANGE_INT | OTGIEN_VBUSVALID_RISE_INT |
169 OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_irq_regs->ien);
170 }
171
172 /**
173 * cdns_drd_host_on - start host.
174 * @cdns: Pointer to controller context structure.
175 *
176 * Returns 0 on success otherwise negative errno.
177 */
cdns_drd_host_on(struct cdns * cdns)178 int cdns_drd_host_on(struct cdns *cdns)
179 {
180 u32 val, ready_bit;
181 int ret;
182
183 /* Enable host mode. */
184 writel(OTGCMD_HOST_BUS_REQ | OTGCMD_OTG_DIS,
185 &cdns->otg_regs->cmd);
186
187 if (cdns->version == CDNSP_CONTROLLER_V2)
188 ready_bit = OTGSTS_CDNSP_XHCI_READY;
189 else
190 ready_bit = OTGSTS_CDNS3_XHCI_READY;
191
192 dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n");
193 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
194 val & ready_bit, 1, 100000);
195
196 if (ret)
197 dev_err(cdns->dev, "timeout waiting for xhci_ready\n");
198
199 phy_set_mode(cdns->usb2_phy, PHY_MODE_USB_HOST);
200 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_HOST);
201 return ret;
202 }
203
204 /**
205 * cdns_drd_host_off - stop host.
206 * @cdns: Pointer to controller context structure.
207 */
cdns_drd_host_off(struct cdns * cdns)208 void cdns_drd_host_off(struct cdns *cdns)
209 {
210 u32 val;
211
212 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
213 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
214 &cdns->otg_regs->cmd);
215
216 /* Waiting till H_IDLE state.*/
217 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
218 !(val & OTGSTATE_HOST_STATE_MASK),
219 1, 2000000);
220 phy_set_mode(cdns->usb2_phy, PHY_MODE_INVALID);
221 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
222 }
223
224 /**
225 * cdns_drd_gadget_on - start gadget.
226 * @cdns: Pointer to controller context structure.
227 *
228 * Returns 0 on success otherwise negative errno
229 */
cdns_drd_gadget_on(struct cdns * cdns)230 int cdns_drd_gadget_on(struct cdns *cdns)
231 {
232 u32 reg = OTGCMD_OTG_DIS;
233 u32 ready_bit;
234 int ret, val;
235
236 /* switch OTG core */
237 writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd);
238
239 dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n");
240
241 if (cdns->version == CDNSP_CONTROLLER_V2)
242 ready_bit = OTGSTS_CDNSP_DEV_READY;
243 else
244 ready_bit = OTGSTS_CDNS3_DEV_READY;
245
246 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
247 val & ready_bit, 1, 100000);
248 if (ret) {
249 dev_err(cdns->dev, "timeout waiting for dev_ready\n");
250 return ret;
251 }
252
253 phy_set_mode(cdns->usb2_phy, PHY_MODE_USB_DEVICE);
254 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_DEVICE);
255 return 0;
256 }
257 EXPORT_SYMBOL_GPL(cdns_drd_gadget_on);
258
259 /**
260 * cdns_drd_gadget_off - stop gadget.
261 * @cdns: Pointer to controller context structure.
262 */
cdns_drd_gadget_off(struct cdns * cdns)263 void cdns_drd_gadget_off(struct cdns *cdns)
264 {
265 u32 val;
266
267 /*
268 * Driver should wait at least 10us after disabling Device
269 * before turning-off Device (DEV_BUS_DROP).
270 */
271 usleep_range(20, 30);
272 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
273 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
274 &cdns->otg_regs->cmd);
275 /* Waiting till DEV_IDLE state.*/
276 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
277 !(val & OTGSTATE_DEV_STATE_MASK),
278 1, 2000000);
279 phy_set_mode(cdns->usb2_phy, PHY_MODE_INVALID);
280 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
281 }
282 EXPORT_SYMBOL_GPL(cdns_drd_gadget_off);
283
284 /**
285 * cdns_init_otg_mode - initialize drd controller
286 * @cdns: Pointer to controller context structure
287 *
288 * Returns 0 on success otherwise negative errno
289 */
cdns_init_otg_mode(struct cdns * cdns)290 static int cdns_init_otg_mode(struct cdns *cdns)
291 {
292 int ret;
293
294 cdns_otg_disable_irq(cdns);
295 /* clear all interrupts */
296 writel(~0, &cdns->otg_irq_regs->ivect);
297
298 ret = cdns_set_mode(cdns, USB_DR_MODE_OTG);
299 if (ret)
300 return ret;
301
302 cdns_otg_enable_irq(cdns);
303
304 return 0;
305 }
306
307 /**
308 * cdns_drd_update_mode - initialize mode of operation
309 * @cdns: Pointer to controller context structure
310 *
311 * Returns 0 on success otherwise negative errno
312 */
cdns_drd_update_mode(struct cdns * cdns)313 int cdns_drd_update_mode(struct cdns *cdns)
314 {
315 int ret;
316
317 switch (cdns->dr_mode) {
318 case USB_DR_MODE_PERIPHERAL:
319 ret = cdns_set_mode(cdns, USB_DR_MODE_PERIPHERAL);
320 break;
321 case USB_DR_MODE_HOST:
322 ret = cdns_set_mode(cdns, USB_DR_MODE_HOST);
323 break;
324 case USB_DR_MODE_OTG:
325 ret = cdns_init_otg_mode(cdns);
326 break;
327 default:
328 dev_err(cdns->dev, "Unsupported mode of operation %d\n",
329 cdns->dr_mode);
330 return -EINVAL;
331 }
332
333 return ret;
334 }
335
cdns_drd_thread_irq(int irq,void * data)336 static irqreturn_t cdns_drd_thread_irq(int irq, void *data)
337 {
338 struct cdns *cdns = data;
339
340 cdns_hw_role_switch(cdns);
341
342 return IRQ_HANDLED;
343 }
344
345 /**
346 * cdns_drd_irq - interrupt handler for OTG events
347 *
348 * @irq: irq number for cdns core device
349 * @data: structure of cdns
350 *
351 * Returns IRQ_HANDLED or IRQ_NONE
352 */
cdns_drd_irq(int irq,void * data)353 static irqreturn_t cdns_drd_irq(int irq, void *data)
354 {
355 irqreturn_t ret = IRQ_NONE;
356 struct cdns *cdns = data;
357 u32 reg;
358
359 if (cdns->dr_mode != USB_DR_MODE_OTG)
360 return IRQ_NONE;
361
362 if (cdns->in_lpm)
363 return ret;
364
365 reg = readl(&cdns->otg_irq_regs->ivect);
366
367 if (!reg)
368 return IRQ_NONE;
369
370 if (reg & OTGIEN_ID_CHANGE_INT) {
371 dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n",
372 cdns_get_id(cdns));
373
374 ret = IRQ_WAKE_THREAD;
375 }
376
377 if (reg & (OTGIEN_VBUSVALID_RISE_INT | OTGIEN_VBUSVALID_FALL_INT)) {
378 dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n",
379 cdns_get_vbus(cdns));
380
381 ret = IRQ_WAKE_THREAD;
382 }
383
384 writel(~0, &cdns->otg_irq_regs->ivect);
385 return ret;
386 }
387
cdns_drd_init(struct cdns * cdns)388 int cdns_drd_init(struct cdns *cdns)
389 {
390 void __iomem *regs;
391 u32 state;
392 int ret;
393
394 regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res);
395 if (IS_ERR(regs))
396 return PTR_ERR(regs);
397
398 /* Detection of DRD version. Controller has been released
399 * in three versions. All are very similar and are software compatible,
400 * but they have same changes in register maps.
401 * The first register in oldest version is command register and it's
402 * read only. Driver should read 0 from it. On the other hand, in v1
403 * and v2 the first register contains device ID number which is not
404 * set to 0. Driver uses this fact to detect the proper version of
405 * controller.
406 */
407 cdns->otg_v0_regs = regs;
408 if (!readl(&cdns->otg_v0_regs->cmd)) {
409 cdns->version = CDNS3_CONTROLLER_V0;
410 cdns->otg_v1_regs = NULL;
411 cdns->otg_cdnsp_regs = NULL;
412 cdns->otg_regs = regs;
413 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
414 &cdns->otg_v0_regs->ien;
415 writel(1, &cdns->otg_v0_regs->simulate);
416 dev_dbg(cdns->dev, "DRD version v0 (%08x)\n",
417 readl(&cdns->otg_v0_regs->version));
418 } else {
419 cdns->otg_v0_regs = NULL;
420 cdns->otg_v1_regs = regs;
421 cdns->otg_cdnsp_regs = regs;
422
423 cdns->otg_regs = (void __iomem *)&cdns->otg_v1_regs->cmd;
424
425 if (readl(&cdns->otg_cdnsp_regs->did) == OTG_CDNSP_DID) {
426 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
427 &cdns->otg_cdnsp_regs->ien;
428 cdns->version = CDNSP_CONTROLLER_V2;
429 } else {
430 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
431 &cdns->otg_v1_regs->ien;
432 writel(1, &cdns->otg_v1_regs->simulate);
433 cdns->version = CDNS3_CONTROLLER_V1;
434 }
435
436 dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n",
437 readl(&cdns->otg_v1_regs->did),
438 readl(&cdns->otg_v1_regs->rid));
439 }
440
441 state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts));
442
443 /* Update dr_mode according to STRAP configuration. */
444 cdns->dr_mode = USB_DR_MODE_OTG;
445
446 if ((cdns->version == CDNSP_CONTROLLER_V2 &&
447 state == OTGSTS_CDNSP_STRAP_HOST) ||
448 (cdns->version != CDNSP_CONTROLLER_V2 &&
449 state == OTGSTS_STRAP_HOST)) {
450 dev_dbg(cdns->dev, "Controller strapped to HOST\n");
451 cdns->dr_mode = USB_DR_MODE_HOST;
452 } else if ((cdns->version == CDNSP_CONTROLLER_V2 &&
453 state == OTGSTS_CDNSP_STRAP_GADGET) ||
454 (cdns->version != CDNSP_CONTROLLER_V2 &&
455 state == OTGSTS_STRAP_GADGET)) {
456 dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n");
457 cdns->dr_mode = USB_DR_MODE_PERIPHERAL;
458 }
459
460 ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq,
461 cdns_drd_irq,
462 cdns_drd_thread_irq,
463 IRQF_SHARED,
464 dev_name(cdns->dev), cdns);
465 if (ret) {
466 dev_err(cdns->dev, "couldn't get otg_irq\n");
467 return ret;
468 }
469
470 state = readl(&cdns->otg_regs->sts);
471 if (OTGSTS_OTG_NRDY(state)) {
472 dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n");
473 return -ENODEV;
474 }
475
476 return 0;
477 }
478
cdns_drd_exit(struct cdns * cdns)479 int cdns_drd_exit(struct cdns *cdns)
480 {
481 cdns_otg_disable_irq(cdns);
482
483 return 0;
484 }
485
486
487 /* Indicate the cdns3 core was power lost before */
cdns_power_is_lost(struct cdns * cdns)488 bool cdns_power_is_lost(struct cdns *cdns)
489 {
490 if (cdns->version == CDNS3_CONTROLLER_V0) {
491 if (!(readl(&cdns->otg_v0_regs->simulate) & BIT(0)))
492 return true;
493 } else {
494 if (!(readl(&cdns->otg_v1_regs->simulate) & BIT(0)))
495 return true;
496 }
497 return false;
498 }
499 EXPORT_SYMBOL_GPL(cdns_power_is_lost);
500