1 // SPDX-License-Identifier: GPL-2.0
3 * Cadence USBSS DRD Driver.
5 * Copyright (C) 2018-2019 Cadence.
6 * Copyright (C) 2017-2018 NXP
7 * Copyright (C) 2019 Texas Instruments
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>
20 #include <linux/pm_runtime.h>
24 #include "host-export.h"
25 #include "gadget-export.h"
28 static int cdns3_idle_init(struct cdns3 *cdns);
30 static int cdns3_role_start(struct cdns3 *cdns, enum usb_role role)
34 if (WARN_ON(role > USB_ROLE_DEVICE))
37 mutex_lock(&cdns->mutex);
39 mutex_unlock(&cdns->mutex);
41 if (!cdns->roles[role])
44 if (cdns->roles[role]->state == CDNS3_ROLE_STATE_ACTIVE)
47 mutex_lock(&cdns->mutex);
48 ret = cdns->roles[role]->start(cdns);
50 cdns->roles[role]->state = CDNS3_ROLE_STATE_ACTIVE;
51 mutex_unlock(&cdns->mutex);
56 static void cdns3_role_stop(struct cdns3 *cdns)
58 enum usb_role role = cdns->role;
60 if (WARN_ON(role > USB_ROLE_DEVICE))
63 if (cdns->roles[role]->state == CDNS3_ROLE_STATE_INACTIVE)
66 mutex_lock(&cdns->mutex);
67 cdns->roles[role]->stop(cdns);
68 cdns->roles[role]->state = CDNS3_ROLE_STATE_INACTIVE;
69 mutex_unlock(&cdns->mutex);
72 static void cdns3_exit_roles(struct cdns3 *cdns)
74 cdns3_role_stop(cdns);
79 * cdns3_core_init_role - initialize role of operation
80 * @cdns: Pointer to cdns3 structure
82 * Returns 0 on success otherwise negative errno
84 static int cdns3_core_init_role(struct cdns3 *cdns)
86 struct device *dev = cdns->dev;
87 enum usb_dr_mode best_dr_mode;
88 enum usb_dr_mode dr_mode;
91 dr_mode = usb_get_dr_mode(dev);
92 cdns->role = USB_ROLE_NONE;
95 * If driver can't read mode by means of usb_get_dr_mode function then
96 * chooses mode according with Kernel configuration. This setting
97 * can be restricted later depending on strap pin configuration.
99 if (dr_mode == USB_DR_MODE_UNKNOWN) {
100 if (IS_ENABLED(CONFIG_USB_CDNS3_HOST) &&
101 IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
102 dr_mode = USB_DR_MODE_OTG;
103 else if (IS_ENABLED(CONFIG_USB_CDNS3_HOST))
104 dr_mode = USB_DR_MODE_HOST;
105 else if (IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
106 dr_mode = USB_DR_MODE_PERIPHERAL;
110 * At this point cdns->dr_mode contains strap configuration.
111 * Driver try update this setting considering kernel configuration
113 best_dr_mode = cdns->dr_mode;
115 ret = cdns3_idle_init(cdns);
119 if (dr_mode == USB_DR_MODE_OTG) {
120 best_dr_mode = cdns->dr_mode;
121 } else if (cdns->dr_mode == USB_DR_MODE_OTG) {
122 best_dr_mode = dr_mode;
123 } else if (cdns->dr_mode != dr_mode) {
124 dev_err(dev, "Incorrect DRD configuration\n");
128 dr_mode = best_dr_mode;
130 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
131 ret = cdns3_host_init(cdns);
133 dev_err(dev, "Host initialization failed with %d\n",
139 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
140 ret = cdns3_gadget_init(cdns);
142 dev_err(dev, "Device initialization failed with %d\n",
148 cdns->dr_mode = dr_mode;
150 ret = cdns3_drd_update_mode(cdns);
154 /* Initialize idle role to start with */
155 ret = cdns3_role_start(cdns, USB_ROLE_NONE);
159 switch (cdns->dr_mode) {
160 case USB_DR_MODE_OTG:
161 ret = cdns3_hw_role_switch(cdns);
165 case USB_DR_MODE_PERIPHERAL:
166 ret = cdns3_role_start(cdns, USB_ROLE_DEVICE);
170 case USB_DR_MODE_HOST:
171 ret = cdns3_role_start(cdns, USB_ROLE_HOST);
182 cdns3_exit_roles(cdns);
187 * cdns3_hw_role_state_machine - role switch state machine based on hw events.
188 * @cdns: Pointer to controller structure.
190 * Returns next role to be entered based on hw events.
192 static enum usb_role cdns3_hw_role_state_machine(struct cdns3 *cdns)
194 enum usb_role role = USB_ROLE_NONE;
197 if (cdns->dr_mode != USB_DR_MODE_OTG) {
198 if (cdns3_is_host(cdns))
199 role = USB_ROLE_HOST;
200 if (cdns3_is_device(cdns))
201 role = USB_ROLE_DEVICE;
206 id = cdns3_get_id(cdns);
207 vbus = cdns3_get_vbus(cdns);
210 * Role change state machine
212 * Previous state: cdns->role
220 * Driver treats USB_ROLE_NONE synonymous to IDLE state from
221 * controller specification.
224 role = USB_ROLE_HOST;
226 role = USB_ROLE_DEVICE;
228 case USB_ROLE_HOST: /* from HOST, we can only change to NONE */
230 role = USB_ROLE_NONE;
232 case USB_ROLE_DEVICE: /* from GADGET, we can only change to NONE*/
234 role = USB_ROLE_NONE;
238 dev_dbg(cdns->dev, "role %d -> %d\n", cdns->role, role);
243 static int cdns3_idle_role_start(struct cdns3 *cdns)
248 static void cdns3_idle_role_stop(struct cdns3 *cdns)
250 /* Program Lane swap and bring PHY out of RESET */
251 phy_reset(cdns->usb3_phy);
254 static int cdns3_idle_init(struct cdns3 *cdns)
256 struct cdns3_role_driver *rdrv;
258 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
262 rdrv->start = cdns3_idle_role_start;
263 rdrv->stop = cdns3_idle_role_stop;
264 rdrv->state = CDNS3_ROLE_STATE_INACTIVE;
265 rdrv->suspend = NULL;
269 cdns->roles[USB_ROLE_NONE] = rdrv;
275 * cdns3_hw_role_switch - switch roles based on HW state
278 int cdns3_hw_role_switch(struct cdns3 *cdns)
280 enum usb_role real_role, current_role;
283 pm_runtime_get_sync(cdns->dev);
285 current_role = cdns->role;
286 real_role = cdns3_hw_role_state_machine(cdns);
288 /* Do nothing if nothing changed */
289 if (current_role == real_role)
292 cdns3_role_stop(cdns);
294 dev_dbg(cdns->dev, "Switching role %d -> %d", current_role, real_role);
296 ret = cdns3_role_start(cdns, real_role);
298 /* Back to current role */
299 dev_err(cdns->dev, "set %d has failed, back to %d\n",
300 real_role, current_role);
301 ret = cdns3_role_start(cdns, current_role);
303 dev_err(cdns->dev, "back to %d failed too\n",
307 pm_runtime_put_sync(cdns->dev);
312 * cdsn3_role_get - get current role of controller.
314 * @sw: pointer to USB role switch structure
318 static enum usb_role cdns3_role_get(struct usb_role_switch *sw)
320 struct cdns3 *cdns = usb_role_switch_get_drvdata(sw);
326 * cdns3_role_set - set current role of controller.
328 * @sw: pointer to USB role switch structure
329 * @role: the previous role
330 * Handles below events:
331 * - Role switch for dual-role devices
332 * - USB_ROLE_GADGET <--> USB_ROLE_NONE for peripheral-only devices
334 static int cdns3_role_set(struct usb_role_switch *sw, enum usb_role role)
336 struct cdns3 *cdns = usb_role_switch_get_drvdata(sw);
339 pm_runtime_get_sync(cdns->dev);
341 if (cdns->role == role)
344 if (cdns->dr_mode == USB_DR_MODE_HOST) {
354 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL) {
357 case USB_ROLE_DEVICE:
364 cdns3_role_stop(cdns);
365 ret = cdns3_role_start(cdns, role);
367 dev_err(cdns->dev, "set role %d has failed\n", role);
370 pm_runtime_put_sync(cdns->dev);
375 * cdns3_probe - probe for cdns3 core device
376 * @pdev: Pointer to cdns3 core platform device
378 * Returns 0 on success otherwise negative errno
380 static int cdns3_probe(struct platform_device *pdev)
382 struct usb_role_switch_desc sw_desc = { };
383 struct device *dev = &pdev->dev;
384 struct resource *res;
389 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
391 dev_err(dev, "error setting dma mask: %d\n", ret);
395 cdns = devm_kzalloc(dev, sizeof(*cdns), GFP_KERNEL);
401 platform_set_drvdata(pdev, cdns);
403 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "host");
405 dev_err(dev, "missing host IRQ\n");
409 cdns->xhci_res[0] = *res;
411 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "xhci");
413 dev_err(dev, "couldn't get xhci resource\n");
417 cdns->xhci_res[1] = *res;
419 cdns->dev_irq = platform_get_irq_byname(pdev, "peripheral");
420 if (cdns->dev_irq == -EPROBE_DEFER)
421 return cdns->dev_irq;
423 if (cdns->dev_irq < 0)
424 dev_err(dev, "couldn't get peripheral irq\n");
426 regs = devm_platform_ioremap_resource_byname(pdev, "dev");
428 return PTR_ERR(regs);
429 cdns->dev_regs = regs;
431 cdns->otg_irq = platform_get_irq_byname(pdev, "otg");
432 if (cdns->otg_irq == -EPROBE_DEFER)
433 return cdns->otg_irq;
435 if (cdns->otg_irq < 0) {
436 dev_err(dev, "couldn't get otg irq\n");
437 return cdns->otg_irq;
440 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg");
442 dev_err(dev, "couldn't get otg resource\n");
446 cdns->otg_res = *res;
448 mutex_init(&cdns->mutex);
450 cdns->usb2_phy = devm_phy_optional_get(dev, "cdns3,usb2-phy");
451 if (IS_ERR(cdns->usb2_phy))
452 return PTR_ERR(cdns->usb2_phy);
454 ret = phy_init(cdns->usb2_phy);
458 cdns->usb3_phy = devm_phy_optional_get(dev, "cdns3,usb3-phy");
459 if (IS_ERR(cdns->usb3_phy))
460 return PTR_ERR(cdns->usb3_phy);
462 ret = phy_init(cdns->usb3_phy);
466 ret = phy_power_on(cdns->usb2_phy);
470 ret = phy_power_on(cdns->usb3_phy);
474 sw_desc.set = cdns3_role_set;
475 sw_desc.get = cdns3_role_get;
476 sw_desc.allow_userspace_control = true;
477 sw_desc.driver_data = cdns;
478 if (device_property_read_bool(dev, "usb-role-switch"))
479 sw_desc.fwnode = dev->fwnode;
481 cdns->role_sw = usb_role_switch_register(dev, &sw_desc);
482 if (IS_ERR(cdns->role_sw)) {
483 ret = PTR_ERR(cdns->role_sw);
484 dev_warn(dev, "Unable to register Role Switch\n");
488 ret = cdns3_drd_init(cdns);
492 ret = cdns3_core_init_role(cdns);
496 device_set_wakeup_capable(dev, true);
497 pm_runtime_set_active(dev);
498 pm_runtime_enable(dev);
501 * The controller needs less time between bus and controller suspend,
502 * and we also needs a small delay to avoid frequently entering low
505 pm_runtime_set_autosuspend_delay(dev, 20);
506 pm_runtime_mark_last_busy(dev);
507 pm_runtime_use_autosuspend(dev);
508 dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
512 cdns3_drd_exit(cdns);
513 usb_role_switch_unregister(cdns->role_sw);
515 phy_power_off(cdns->usb3_phy);
518 phy_power_off(cdns->usb2_phy);
520 phy_exit(cdns->usb3_phy);
522 phy_exit(cdns->usb2_phy);
528 * cdns3_remove - unbind drd driver and clean up
529 * @pdev: Pointer to Linux platform device
531 * Returns 0 on success otherwise negative errno
533 static int cdns3_remove(struct platform_device *pdev)
535 struct cdns3 *cdns = platform_get_drvdata(pdev);
537 pm_runtime_get_sync(&pdev->dev);
538 pm_runtime_disable(&pdev->dev);
539 pm_runtime_put_noidle(&pdev->dev);
540 cdns3_exit_roles(cdns);
541 usb_role_switch_unregister(cdns->role_sw);
542 phy_power_off(cdns->usb2_phy);
543 phy_power_off(cdns->usb3_phy);
544 phy_exit(cdns->usb2_phy);
545 phy_exit(cdns->usb3_phy);
549 #ifdef CONFIG_PM_SLEEP
551 static int cdns3_suspend(struct device *dev)
553 struct cdns3 *cdns = dev_get_drvdata(dev);
556 if (cdns->role == USB_ROLE_HOST)
559 if (pm_runtime_status_suspended(dev))
560 pm_runtime_resume(dev);
562 if (cdns->roles[cdns->role]->suspend) {
563 spin_lock_irqsave(&cdns->gadget_dev->lock, flags);
564 cdns->roles[cdns->role]->suspend(cdns, false);
565 spin_unlock_irqrestore(&cdns->gadget_dev->lock, flags);
571 static int cdns3_resume(struct device *dev)
573 struct cdns3 *cdns = dev_get_drvdata(dev);
576 if (cdns->role == USB_ROLE_HOST)
579 if (cdns->roles[cdns->role]->resume) {
580 spin_lock_irqsave(&cdns->gadget_dev->lock, flags);
581 cdns->roles[cdns->role]->resume(cdns, false);
582 spin_unlock_irqrestore(&cdns->gadget_dev->lock, flags);
585 pm_runtime_disable(dev);
586 pm_runtime_set_active(dev);
587 pm_runtime_enable(dev);
593 static const struct dev_pm_ops cdns3_pm_ops = {
594 SET_SYSTEM_SLEEP_PM_OPS(cdns3_suspend, cdns3_resume)
598 static const struct of_device_id of_cdns3_match[] = {
599 { .compatible = "cdns,usb3" },
602 MODULE_DEVICE_TABLE(of, of_cdns3_match);
605 static struct platform_driver cdns3_driver = {
606 .probe = cdns3_probe,
607 .remove = cdns3_remove,
610 .of_match_table = of_match_ptr(of_cdns3_match),
615 module_platform_driver(cdns3_driver);
617 MODULE_ALIAS("platform:cdns3");
619 MODULE_LICENSE("GPL v2");
620 MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver");