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