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