xref: /openbmc/linux/drivers/usb/cdns3/core.c (revision 62975d27)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Cadence USBSS DRD Driver.
4  *
5  * Copyright (C) 2018-2019 Cadence.
6  * Copyright (C) 2017-2018 NXP
7  * Copyright (C) 2019 Texas Instruments
8  *
9  * Author: Peter Chen <peter.chen@nxp.com>
10  *         Pawel Laszczak <pawell@cadence.com>
11  *         Roger Quadros <rogerq@ti.com>
12  */
13 
14 #include <linux/dma-mapping.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/platform_device.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/pm_runtime.h>
21 
22 #include "gadget.h"
23 #include "core.h"
24 #include "host-export.h"
25 #include "gadget-export.h"
26 #include "drd.h"
27 
28 static int cdns3_idle_init(struct cdns3 *cdns);
29 
30 static inline
31 struct cdns3_role_driver *cdns3_get_current_role_driver(struct cdns3 *cdns)
32 {
33 	WARN_ON(!cdns->roles[cdns->role]);
34 	return cdns->roles[cdns->role];
35 }
36 
37 static int cdns3_role_start(struct cdns3 *cdns, enum usb_role role)
38 {
39 	int ret;
40 
41 	if (WARN_ON(role > USB_ROLE_DEVICE))
42 		return 0;
43 
44 	mutex_lock(&cdns->mutex);
45 	cdns->role = role;
46 	mutex_unlock(&cdns->mutex);
47 
48 	if (!cdns->roles[role])
49 		return -ENXIO;
50 
51 	if (cdns->roles[role]->state == CDNS3_ROLE_STATE_ACTIVE)
52 		return 0;
53 
54 	mutex_lock(&cdns->mutex);
55 	ret = cdns->roles[role]->start(cdns);
56 	if (!ret)
57 		cdns->roles[role]->state = CDNS3_ROLE_STATE_ACTIVE;
58 	mutex_unlock(&cdns->mutex);
59 
60 	return ret;
61 }
62 
63 static void cdns3_role_stop(struct cdns3 *cdns)
64 {
65 	enum usb_role role = cdns->role;
66 
67 	if (WARN_ON(role > USB_ROLE_DEVICE))
68 		return;
69 
70 	if (cdns->roles[role]->state == CDNS3_ROLE_STATE_INACTIVE)
71 		return;
72 
73 	mutex_lock(&cdns->mutex);
74 	cdns->roles[role]->stop(cdns);
75 	cdns->roles[role]->state = CDNS3_ROLE_STATE_INACTIVE;
76 	mutex_unlock(&cdns->mutex);
77 }
78 
79 static void cdns3_exit_roles(struct cdns3 *cdns)
80 {
81 	cdns3_role_stop(cdns);
82 	cdns3_drd_exit(cdns);
83 }
84 
85 /**
86  * cdns3_core_init_role - initialize role of operation
87  * @cdns: Pointer to cdns3 structure
88  *
89  * Returns 0 on success otherwise negative errno
90  */
91 static int cdns3_core_init_role(struct cdns3 *cdns)
92 {
93 	struct device *dev = cdns->dev;
94 	enum usb_dr_mode best_dr_mode;
95 	enum usb_dr_mode dr_mode;
96 	int ret = 0;
97 
98 	dr_mode = usb_get_dr_mode(dev);
99 	cdns->role = USB_ROLE_NONE;
100 
101 	/*
102 	 * If driver can't read mode by means of usb_get_dr_mode function then
103 	 * chooses mode according with Kernel configuration. This setting
104 	 * can be restricted later depending on strap pin configuration.
105 	 */
106 	if (dr_mode == USB_DR_MODE_UNKNOWN) {
107 		if (IS_ENABLED(CONFIG_USB_CDNS3_HOST) &&
108 		    IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
109 			dr_mode = USB_DR_MODE_OTG;
110 		else if (IS_ENABLED(CONFIG_USB_CDNS3_HOST))
111 			dr_mode = USB_DR_MODE_HOST;
112 		else if (IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
113 			dr_mode = USB_DR_MODE_PERIPHERAL;
114 	}
115 
116 	/*
117 	 * At this point cdns->dr_mode contains strap configuration.
118 	 * Driver try update this setting considering kernel configuration
119 	 */
120 	best_dr_mode = cdns->dr_mode;
121 
122 	ret = cdns3_idle_init(cdns);
123 	if (ret)
124 		return ret;
125 
126 	if (dr_mode == USB_DR_MODE_OTG) {
127 		best_dr_mode = cdns->dr_mode;
128 	} else if (cdns->dr_mode == USB_DR_MODE_OTG) {
129 		best_dr_mode = dr_mode;
130 	} else if (cdns->dr_mode != dr_mode) {
131 		dev_err(dev, "Incorrect DRD configuration\n");
132 		return -EINVAL;
133 	}
134 
135 	dr_mode = best_dr_mode;
136 
137 	if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
138 		ret = cdns3_host_init(cdns);
139 		if (ret) {
140 			dev_err(dev, "Host initialization failed with %d\n",
141 				ret);
142 			goto err;
143 		}
144 	}
145 
146 	if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
147 		ret = cdns3_gadget_init(cdns);
148 		if (ret) {
149 			dev_err(dev, "Device initialization failed with %d\n",
150 				ret);
151 			goto err;
152 		}
153 	}
154 
155 	cdns->dr_mode = dr_mode;
156 
157 	ret = cdns3_drd_update_mode(cdns);
158 	if (ret)
159 		goto err;
160 
161 	/* Initialize idle role to start with */
162 	ret = cdns3_role_start(cdns, USB_ROLE_NONE);
163 	if (ret)
164 		goto err;
165 
166 	switch (cdns->dr_mode) {
167 	case USB_DR_MODE_OTG:
168 		ret = cdns3_hw_role_switch(cdns);
169 		if (ret)
170 			goto err;
171 		break;
172 	case USB_DR_MODE_PERIPHERAL:
173 		ret = cdns3_role_start(cdns, USB_ROLE_DEVICE);
174 		if (ret)
175 			goto err;
176 		break;
177 	case USB_DR_MODE_HOST:
178 		ret = cdns3_role_start(cdns, USB_ROLE_HOST);
179 		if (ret)
180 			goto err;
181 		break;
182 	default:
183 		ret = -EINVAL;
184 		goto err;
185 	}
186 
187 	return ret;
188 err:
189 	cdns3_exit_roles(cdns);
190 	return ret;
191 }
192 
193 /**
194  * cdns3_hw_role_state_machine  - role switch state machine based on hw events.
195  * @cdns: Pointer to controller structure.
196  *
197  * Returns next role to be entered based on hw events.
198  */
199 static enum usb_role cdns3_hw_role_state_machine(struct cdns3 *cdns)
200 {
201 	enum usb_role role;
202 	int id, vbus;
203 
204 	if (cdns->dr_mode != USB_DR_MODE_OTG)
205 		goto not_otg;
206 
207 	id = cdns3_get_id(cdns);
208 	vbus = cdns3_get_vbus(cdns);
209 
210 	/*
211 	 * Role change state machine
212 	 * Inputs: ID, VBUS
213 	 * Previous state: cdns->role
214 	 * Next state: role
215 	 */
216 	role = cdns->role;
217 
218 	switch (role) {
219 	case USB_ROLE_NONE:
220 		/*
221 		 * Driver treats USB_ROLE_NONE synonymous to IDLE state from
222 		 * controller specification.
223 		 */
224 		if (!id)
225 			role = USB_ROLE_HOST;
226 		else if (vbus)
227 			role = USB_ROLE_DEVICE;
228 		break;
229 	case USB_ROLE_HOST: /* from HOST, we can only change to NONE */
230 		if (id)
231 			role = USB_ROLE_NONE;
232 		break;
233 	case USB_ROLE_DEVICE: /* from GADGET, we can only change to NONE*/
234 		if (!vbus)
235 			role = USB_ROLE_NONE;
236 		break;
237 	}
238 
239 	dev_dbg(cdns->dev, "role %d -> %d\n", cdns->role, role);
240 
241 	return role;
242 
243 not_otg:
244 	if (cdns3_is_host(cdns))
245 		role = USB_ROLE_HOST;
246 	if (cdns3_is_device(cdns))
247 		role = USB_ROLE_DEVICE;
248 
249 	return role;
250 }
251 
252 static int cdns3_idle_role_start(struct cdns3 *cdns)
253 {
254 	return 0;
255 }
256 
257 static void cdns3_idle_role_stop(struct cdns3 *cdns)
258 {
259 	/* Program Lane swap and bring PHY out of RESET */
260 	phy_reset(cdns->usb3_phy);
261 }
262 
263 static int cdns3_idle_init(struct cdns3 *cdns)
264 {
265 	struct cdns3_role_driver *rdrv;
266 
267 	rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
268 	if (!rdrv)
269 		return -ENOMEM;
270 
271 	rdrv->start = cdns3_idle_role_start;
272 	rdrv->stop = cdns3_idle_role_stop;
273 	rdrv->state = CDNS3_ROLE_STATE_INACTIVE;
274 	rdrv->suspend = NULL;
275 	rdrv->resume = NULL;
276 	rdrv->name = "idle";
277 
278 	cdns->roles[USB_ROLE_NONE] = rdrv;
279 
280 	return 0;
281 }
282 
283 /**
284  * cdns3_hw_role_switch - switch roles based on HW state
285  * @cdns3: controller
286  */
287 int cdns3_hw_role_switch(struct cdns3 *cdns)
288 {
289 	enum usb_role real_role, current_role;
290 	int ret = 0;
291 
292 	pm_runtime_get_sync(cdns->dev);
293 
294 	current_role = cdns->role;
295 	real_role = cdns3_hw_role_state_machine(cdns);
296 
297 	/* Do nothing if nothing changed */
298 	if (current_role == real_role)
299 		goto exit;
300 
301 	cdns3_role_stop(cdns);
302 
303 	dev_dbg(cdns->dev, "Switching role %d -> %d", current_role, real_role);
304 
305 	ret = cdns3_role_start(cdns, real_role);
306 	if (ret) {
307 		/* Back to current role */
308 		dev_err(cdns->dev, "set %d has failed, back to %d\n",
309 			real_role, current_role);
310 		ret = cdns3_role_start(cdns, current_role);
311 		if (ret)
312 			dev_err(cdns->dev, "back to %d failed too\n",
313 				current_role);
314 	}
315 exit:
316 	pm_runtime_put_sync(cdns->dev);
317 	return ret;
318 }
319 
320 /**
321  * cdsn3_role_get - get current role of controller.
322  *
323  * @dev: Pointer to device structure
324  *
325  * Returns role
326  */
327 static enum usb_role cdns3_role_get(struct usb_role_switch *sw)
328 {
329 	struct cdns3 *cdns = usb_role_switch_get_drvdata(sw);
330 
331 	return cdns->role;
332 }
333 
334 /**
335  * cdns3_role_set - set current role of controller.
336  *
337  * @dev: pointer to device object
338  * @role - the previous role
339  * Handles below events:
340  * - Role switch for dual-role devices
341  * - USB_ROLE_GADGET <--> USB_ROLE_NONE for peripheral-only devices
342  */
343 static int cdns3_role_set(struct usb_role_switch *sw, enum usb_role role)
344 {
345 	struct cdns3 *cdns = usb_role_switch_get_drvdata(sw);
346 	int ret = 0;
347 
348 	pm_runtime_get_sync(cdns->dev);
349 
350 	if (cdns->role == role)
351 		goto pm_put;
352 
353 	if (cdns->dr_mode == USB_DR_MODE_HOST) {
354 		switch (role) {
355 		case USB_ROLE_NONE:
356 		case USB_ROLE_HOST:
357 			break;
358 		default:
359 			ret = -EPERM;
360 			goto pm_put;
361 		}
362 	}
363 
364 	if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL) {
365 		switch (role) {
366 		case USB_ROLE_NONE:
367 		case USB_ROLE_DEVICE:
368 			break;
369 		default:
370 			ret = -EPERM;
371 			goto pm_put;
372 		}
373 	}
374 
375 	cdns3_role_stop(cdns);
376 	ret = cdns3_role_start(cdns, role);
377 	if (ret) {
378 		dev_err(cdns->dev, "set role %d has failed\n", role);
379 		ret = -EPERM;
380 	}
381 
382 pm_put:
383 	pm_runtime_put_sync(cdns->dev);
384 	return ret;
385 }
386 
387 /**
388  * cdns3_probe - probe for cdns3 core device
389  * @pdev: Pointer to cdns3 core platform device
390  *
391  * Returns 0 on success otherwise negative errno
392  */
393 static int cdns3_probe(struct platform_device *pdev)
394 {
395 	struct usb_role_switch_desc sw_desc = { };
396 	struct device *dev = &pdev->dev;
397 	struct resource	*res;
398 	struct cdns3 *cdns;
399 	void __iomem *regs;
400 	int ret;
401 
402 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
403 	if (ret) {
404 		dev_err(dev, "error setting dma mask: %d\n", ret);
405 		return -ENODEV;
406 	}
407 
408 	cdns = devm_kzalloc(dev, sizeof(*cdns), GFP_KERNEL);
409 	if (!cdns)
410 		return -ENOMEM;
411 
412 	cdns->dev = dev;
413 
414 	platform_set_drvdata(pdev, cdns);
415 
416 	res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "host");
417 	if (!res) {
418 		dev_err(dev, "missing host IRQ\n");
419 		return -ENODEV;
420 	}
421 
422 	cdns->xhci_res[0] = *res;
423 
424 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "xhci");
425 	if (!res) {
426 		dev_err(dev, "couldn't get xhci resource\n");
427 		return -ENXIO;
428 	}
429 
430 	cdns->xhci_res[1] = *res;
431 
432 	cdns->dev_irq = platform_get_irq_byname(pdev, "peripheral");
433 	if (cdns->dev_irq == -EPROBE_DEFER)
434 		return cdns->dev_irq;
435 
436 	if (cdns->dev_irq < 0)
437 		dev_err(dev, "couldn't get peripheral irq\n");
438 
439 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dev");
440 	regs = devm_ioremap_resource(dev, res);
441 	if (IS_ERR(regs))
442 		return PTR_ERR(regs);
443 	cdns->dev_regs	= regs;
444 
445 	cdns->otg_irq = platform_get_irq_byname(pdev, "otg");
446 	if (cdns->otg_irq == -EPROBE_DEFER)
447 		return cdns->otg_irq;
448 
449 	if (cdns->otg_irq < 0) {
450 		dev_err(dev, "couldn't get otg irq\n");
451 		return cdns->otg_irq;
452 	}
453 
454 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg");
455 	if (!res) {
456 		dev_err(dev, "couldn't get otg resource\n");
457 		return -ENXIO;
458 	}
459 
460 	cdns->otg_res = *res;
461 
462 	mutex_init(&cdns->mutex);
463 
464 	cdns->usb2_phy = devm_phy_optional_get(dev, "cdns3,usb2-phy");
465 	if (IS_ERR(cdns->usb2_phy))
466 		return PTR_ERR(cdns->usb2_phy);
467 
468 	ret = phy_init(cdns->usb2_phy);
469 	if (ret)
470 		return ret;
471 
472 	cdns->usb3_phy = devm_phy_optional_get(dev, "cdns3,usb3-phy");
473 	if (IS_ERR(cdns->usb3_phy))
474 		return PTR_ERR(cdns->usb3_phy);
475 
476 	ret = phy_init(cdns->usb3_phy);
477 	if (ret)
478 		goto err1;
479 
480 	ret = phy_power_on(cdns->usb2_phy);
481 	if (ret)
482 		goto err2;
483 
484 	ret = phy_power_on(cdns->usb3_phy);
485 	if (ret)
486 		goto err3;
487 
488 	sw_desc.set = cdns3_role_set;
489 	sw_desc.get = cdns3_role_get;
490 	sw_desc.allow_userspace_control = true;
491 	sw_desc.driver_data = cdns;
492 	if (device_property_read_bool(dev, "usb-role-switch"))
493 		sw_desc.fwnode = dev->fwnode;
494 
495 	cdns->role_sw = usb_role_switch_register(dev, &sw_desc);
496 	if (IS_ERR(cdns->role_sw)) {
497 		ret = PTR_ERR(cdns->role_sw);
498 		dev_warn(dev, "Unable to register Role Switch\n");
499 		goto err4;
500 	}
501 
502 	ret = cdns3_drd_init(cdns);
503 	if (ret)
504 		goto err5;
505 
506 	ret = cdns3_core_init_role(cdns);
507 	if (ret)
508 		goto err5;
509 
510 	device_set_wakeup_capable(dev, true);
511 	pm_runtime_set_active(dev);
512 	pm_runtime_enable(dev);
513 
514 	/*
515 	 * The controller needs less time between bus and controller suspend,
516 	 * and we also needs a small delay to avoid frequently entering low
517 	 * power mode.
518 	 */
519 	pm_runtime_set_autosuspend_delay(dev, 20);
520 	pm_runtime_mark_last_busy(dev);
521 	pm_runtime_use_autosuspend(dev);
522 	dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
523 
524 	return 0;
525 err5:
526 	cdns3_drd_exit(cdns);
527 	usb_role_switch_unregister(cdns->role_sw);
528 err4:
529 	phy_power_off(cdns->usb3_phy);
530 
531 err3:
532 	phy_power_off(cdns->usb2_phy);
533 err2:
534 	phy_exit(cdns->usb3_phy);
535 err1:
536 	phy_exit(cdns->usb2_phy);
537 
538 	return ret;
539 }
540 
541 /**
542  * cdns3_remove - unbind drd driver and clean up
543  * @pdev: Pointer to Linux platform device
544  *
545  * Returns 0 on success otherwise negative errno
546  */
547 static int cdns3_remove(struct platform_device *pdev)
548 {
549 	struct cdns3 *cdns = platform_get_drvdata(pdev);
550 
551 	pm_runtime_get_sync(&pdev->dev);
552 	pm_runtime_disable(&pdev->dev);
553 	pm_runtime_put_noidle(&pdev->dev);
554 	cdns3_exit_roles(cdns);
555 	usb_role_switch_unregister(cdns->role_sw);
556 	phy_power_off(cdns->usb2_phy);
557 	phy_power_off(cdns->usb3_phy);
558 	phy_exit(cdns->usb2_phy);
559 	phy_exit(cdns->usb3_phy);
560 	return 0;
561 }
562 
563 #ifdef CONFIG_PM_SLEEP
564 
565 static int cdns3_suspend(struct device *dev)
566 {
567 	struct cdns3 *cdns = dev_get_drvdata(dev);
568 	unsigned long flags;
569 
570 	if (cdns->role == USB_ROLE_HOST)
571 		return 0;
572 
573 	if (pm_runtime_status_suspended(dev))
574 		pm_runtime_resume(dev);
575 
576 	if (cdns->roles[cdns->role]->suspend) {
577 		spin_lock_irqsave(&cdns->gadget_dev->lock, flags);
578 		cdns->roles[cdns->role]->suspend(cdns, false);
579 		spin_unlock_irqrestore(&cdns->gadget_dev->lock, flags);
580 	}
581 
582 	return 0;
583 }
584 
585 static int cdns3_resume(struct device *dev)
586 {
587 	struct cdns3 *cdns = dev_get_drvdata(dev);
588 	unsigned long flags;
589 
590 	if (cdns->role == USB_ROLE_HOST)
591 		return 0;
592 
593 	if (cdns->roles[cdns->role]->resume) {
594 		spin_lock_irqsave(&cdns->gadget_dev->lock, flags);
595 		cdns->roles[cdns->role]->resume(cdns, false);
596 		spin_unlock_irqrestore(&cdns->gadget_dev->lock, flags);
597 	}
598 
599 	pm_runtime_disable(dev);
600 	pm_runtime_set_active(dev);
601 	pm_runtime_enable(dev);
602 
603 	return 0;
604 }
605 #endif
606 
607 static const struct dev_pm_ops cdns3_pm_ops = {
608 	SET_SYSTEM_SLEEP_PM_OPS(cdns3_suspend, cdns3_resume)
609 };
610 
611 #ifdef CONFIG_OF
612 static const struct of_device_id of_cdns3_match[] = {
613 	{ .compatible = "cdns,usb3" },
614 	{ },
615 };
616 MODULE_DEVICE_TABLE(of, of_cdns3_match);
617 #endif
618 
619 static struct platform_driver cdns3_driver = {
620 	.probe		= cdns3_probe,
621 	.remove		= cdns3_remove,
622 	.driver		= {
623 		.name	= "cdns-usb3",
624 		.of_match_table	= of_match_ptr(of_cdns3_match),
625 		.pm	= &cdns3_pm_ops,
626 	},
627 };
628 
629 module_platform_driver(cdns3_driver);
630 
631 MODULE_ALIAS("platform:cdns3");
632 MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
633 MODULE_LICENSE("GPL v2");
634 MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver");
635