1 // SPDX-License-Identifier: GPL-2.0
3 * Cadence USBSS and USBSSP DRD Driver.
5 * Copyright (C) 2018-2019 Cadence.
6 * Copyright (C) 2017-2018 NXP
7 * Copyright (C) 2019 Texas Instruments
9 * Author: Peter Chen <peter.chen@nxp.com>
10 * Pawel Laszczak <pawell@cadence.com>
11 * Roger Quadros <rogerq@ti.com>
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>
23 #include "host-export.h"
26 static int cdns_idle_init(struct cdns *cdns);
28 static int cdns_role_start(struct cdns *cdns, enum usb_role role)
32 if (WARN_ON(role > USB_ROLE_DEVICE))
35 mutex_lock(&cdns->mutex);
37 mutex_unlock(&cdns->mutex);
39 if (!cdns->roles[role])
42 if (cdns->roles[role]->state == CDNS_ROLE_STATE_ACTIVE)
45 mutex_lock(&cdns->mutex);
46 ret = cdns->roles[role]->start(cdns);
48 cdns->roles[role]->state = CDNS_ROLE_STATE_ACTIVE;
49 mutex_unlock(&cdns->mutex);
54 static void cdns_role_stop(struct cdns *cdns)
56 enum usb_role role = cdns->role;
58 if (WARN_ON(role > USB_ROLE_DEVICE))
61 if (cdns->roles[role]->state == CDNS_ROLE_STATE_INACTIVE)
64 mutex_lock(&cdns->mutex);
65 cdns->roles[role]->stop(cdns);
66 cdns->roles[role]->state = CDNS_ROLE_STATE_INACTIVE;
67 mutex_unlock(&cdns->mutex);
70 static void cdns_exit_roles(struct cdns *cdns)
77 * cdns_core_init_role - initialize role of operation
78 * @cdns: Pointer to cdns structure
80 * Returns 0 on success otherwise negative errno
82 static int cdns_core_init_role(struct cdns *cdns)
84 struct device *dev = cdns->dev;
85 enum usb_dr_mode best_dr_mode;
86 enum usb_dr_mode dr_mode;
89 dr_mode = usb_get_dr_mode(dev);
90 cdns->role = USB_ROLE_NONE;
93 * If driver can't read mode by means of usb_get_dr_mode function then
94 * chooses mode according with Kernel configuration. This setting
95 * can be restricted later depending on strap pin configuration.
97 if (dr_mode == USB_DR_MODE_UNKNOWN) {
98 if (cdns->version == CDNSP_CONTROLLER_V2) {
99 if (IS_ENABLED(CONFIG_USB_CDNSP_HOST) &&
100 IS_ENABLED(CONFIG_USB_CDNSP_GADGET))
101 dr_mode = USB_DR_MODE_OTG;
102 else if (IS_ENABLED(CONFIG_USB_CDNSP_HOST))
103 dr_mode = USB_DR_MODE_HOST;
104 else if (IS_ENABLED(CONFIG_USB_CDNSP_GADGET))
105 dr_mode = USB_DR_MODE_PERIPHERAL;
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;
118 * At this point cdns->dr_mode contains strap configuration.
119 * Driver try update this setting considering kernel configuration
121 best_dr_mode = cdns->dr_mode;
123 ret = cdns_idle_init(cdns);
127 if (dr_mode == USB_DR_MODE_OTG) {
128 best_dr_mode = cdns->dr_mode;
129 } else if (cdns->dr_mode == USB_DR_MODE_OTG) {
130 best_dr_mode = dr_mode;
131 } else if (cdns->dr_mode != dr_mode) {
132 dev_err(dev, "Incorrect DRD configuration\n");
136 dr_mode = best_dr_mode;
138 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
139 if ((cdns->version == CDNSP_CONTROLLER_V2 &&
140 IS_ENABLED(CONFIG_USB_CDNSP_HOST)) ||
141 (cdns->version < CDNSP_CONTROLLER_V2 &&
142 IS_ENABLED(CONFIG_USB_CDNS3_HOST)))
143 ret = cdns_host_init(cdns);
148 dev_err(dev, "Host initialization failed with %d\n",
154 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
155 if (cdns->gadget_init)
156 ret = cdns->gadget_init(cdns);
161 dev_err(dev, "Device initialization failed with %d\n",
167 cdns->dr_mode = dr_mode;
169 ret = cdns_drd_update_mode(cdns);
173 /* Initialize idle role to start with */
174 ret = cdns_role_start(cdns, USB_ROLE_NONE);
178 switch (cdns->dr_mode) {
179 case USB_DR_MODE_OTG:
180 ret = cdns_hw_role_switch(cdns);
184 case USB_DR_MODE_PERIPHERAL:
185 ret = cdns_role_start(cdns, USB_ROLE_DEVICE);
189 case USB_DR_MODE_HOST:
190 ret = cdns_role_start(cdns, USB_ROLE_HOST);
201 cdns_exit_roles(cdns);
206 * cdns_hw_role_state_machine - role switch state machine based on hw events.
207 * @cdns: Pointer to controller structure.
209 * Returns next role to be entered based on hw events.
211 static enum usb_role cdns_hw_role_state_machine(struct cdns *cdns)
213 enum usb_role role = USB_ROLE_NONE;
216 if (cdns->dr_mode != USB_DR_MODE_OTG) {
217 if (cdns_is_host(cdns))
218 role = USB_ROLE_HOST;
219 if (cdns_is_device(cdns))
220 role = USB_ROLE_DEVICE;
225 id = cdns_get_id(cdns);
226 vbus = cdns_get_vbus(cdns);
229 * Role change state machine
231 * Previous state: cdns->role
239 * Driver treats USB_ROLE_NONE synonymous to IDLE state from
240 * controller specification.
243 role = USB_ROLE_HOST;
245 role = USB_ROLE_DEVICE;
247 case USB_ROLE_HOST: /* from HOST, we can only change to NONE */
249 role = USB_ROLE_NONE;
251 case USB_ROLE_DEVICE: /* from GADGET, we can only change to NONE*/
253 role = USB_ROLE_NONE;
259 dev_dbg(cdns->dev, "role %d -> %d\n", cdns->role, role);
264 static int cdns_idle_role_start(struct cdns *cdns)
269 static void cdns_idle_role_stop(struct cdns *cdns)
271 /* Program Lane swap and bring PHY out of RESET */
272 phy_reset(cdns->usb3_phy);
275 static int cdns_idle_init(struct cdns *cdns)
277 struct cdns_role_driver *rdrv;
279 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
283 rdrv->start = cdns_idle_role_start;
284 rdrv->stop = cdns_idle_role_stop;
285 rdrv->state = CDNS_ROLE_STATE_INACTIVE;
286 rdrv->suspend = NULL;
290 cdns->roles[USB_ROLE_NONE] = rdrv;
296 * cdns_hw_role_switch - switch roles based on HW state
299 int cdns_hw_role_switch(struct cdns *cdns)
301 enum usb_role real_role, current_role;
304 /* Depends on role switch class */
308 pm_runtime_get_sync(cdns->dev);
310 current_role = cdns->role;
311 real_role = cdns_hw_role_state_machine(cdns);
313 /* Do nothing if nothing changed */
314 if (current_role == real_role)
317 cdns_role_stop(cdns);
319 dev_dbg(cdns->dev, "Switching role %d -> %d", current_role, real_role);
321 ret = cdns_role_start(cdns, real_role);
323 /* Back to current role */
324 dev_err(cdns->dev, "set %d has failed, back to %d\n",
325 real_role, current_role);
326 ret = cdns_role_start(cdns, current_role);
328 dev_err(cdns->dev, "back to %d failed too\n",
332 pm_runtime_put_sync(cdns->dev);
337 * cdns_role_get - get current role of controller.
339 * @sw: pointer to USB role switch structure
343 static enum usb_role cdns_role_get(struct usb_role_switch *sw)
345 struct cdns *cdns = usb_role_switch_get_drvdata(sw);
351 * cdns_role_set - set current role of controller.
353 * @sw: pointer to USB role switch structure
354 * @role: the previous role
355 * Handles below events:
356 * - Role switch for dual-role devices
357 * - USB_ROLE_GADGET <--> USB_ROLE_NONE for peripheral-only devices
359 static int cdns_role_set(struct usb_role_switch *sw, enum usb_role role)
361 struct cdns *cdns = usb_role_switch_get_drvdata(sw);
364 pm_runtime_get_sync(cdns->dev);
366 if (cdns->role == role)
369 if (cdns->dr_mode == USB_DR_MODE_HOST) {
379 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL) {
382 case USB_ROLE_DEVICE:
389 cdns_role_stop(cdns);
390 ret = cdns_role_start(cdns, role);
392 dev_err(cdns->dev, "set role %d has failed\n", role);
395 pm_runtime_put_sync(cdns->dev);
401 * cdns_wakeup_irq - interrupt handler for wakeup events
402 * @irq: irq number for cdns3/cdnsp core device
403 * @data: structure of cdns
405 * Returns IRQ_HANDLED or IRQ_NONE
407 static irqreturn_t cdns_wakeup_irq(int irq, void *data)
409 struct cdns *cdns = data;
412 disable_irq_nosync(irq);
413 cdns->wakeup_pending = true;
414 if ((cdns->role == USB_ROLE_HOST) && cdns->host_dev)
415 pm_request_resume(&cdns->host_dev->dev);
424 * cdns_init - probe for cdns3/cdnsp core device
425 * @cdns: Pointer to cdns structure.
427 * Returns 0 on success otherwise negative errno
429 int cdns_init(struct cdns *cdns)
431 struct device *dev = cdns->dev;
434 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
436 dev_err(dev, "error setting dma mask: %d\n", ret);
440 mutex_init(&cdns->mutex);
442 if (device_property_read_bool(dev, "usb-role-switch")) {
443 struct usb_role_switch_desc sw_desc = { };
445 sw_desc.set = cdns_role_set;
446 sw_desc.get = cdns_role_get;
447 sw_desc.allow_userspace_control = true;
448 sw_desc.driver_data = cdns;
449 sw_desc.fwnode = dev->fwnode;
451 cdns->role_sw = usb_role_switch_register(dev, &sw_desc);
452 if (IS_ERR(cdns->role_sw)) {
453 dev_warn(dev, "Unable to register Role Switch\n");
454 return PTR_ERR(cdns->role_sw);
458 if (cdns->wakeup_irq) {
459 ret = devm_request_irq(cdns->dev, cdns->wakeup_irq,
462 dev_name(cdns->dev), cdns);
465 dev_err(cdns->dev, "couldn't register wakeup irq handler\n");
466 goto role_switch_unregister;
470 ret = cdns_drd_init(cdns);
474 ret = cdns_core_init_role(cdns);
478 spin_lock_init(&cdns->lock);
480 dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
485 role_switch_unregister:
487 usb_role_switch_unregister(cdns->role_sw);
491 EXPORT_SYMBOL_GPL(cdns_init);
494 * cdns_remove - unbind drd driver and clean up
495 * @cdns: Pointer to cdns structure.
497 * Returns 0 on success otherwise negative errno
499 int cdns_remove(struct cdns *cdns)
501 cdns_exit_roles(cdns);
502 usb_role_switch_unregister(cdns->role_sw);
506 EXPORT_SYMBOL_GPL(cdns_remove);
508 #ifdef CONFIG_PM_SLEEP
509 int cdns_suspend(struct cdns *cdns)
511 struct device *dev = cdns->dev;
514 if (pm_runtime_status_suspended(dev))
515 pm_runtime_resume(dev);
517 if (cdns->roles[cdns->role]->suspend) {
518 spin_lock_irqsave(&cdns->lock, flags);
519 cdns->roles[cdns->role]->suspend(cdns, false);
520 spin_unlock_irqrestore(&cdns->lock, flags);
525 EXPORT_SYMBOL_GPL(cdns_suspend);
527 int cdns_resume(struct cdns *cdns, u8 set_active)
529 struct device *dev = cdns->dev;
530 enum usb_role real_role;
531 bool role_changed = false;
534 if (cdns_power_is_lost(cdns)) {
536 cdns->role = cdns_role_get(cdns->role_sw);
538 real_role = cdns_hw_role_state_machine(cdns);
539 if (real_role != cdns->role) {
540 ret = cdns_hw_role_switch(cdns);
548 if (cdns->role == USB_ROLE_HOST)
549 ret = cdns_drd_host_on(cdns);
550 else if (cdns->role == USB_ROLE_DEVICE)
551 ret = cdns_drd_gadget_on(cdns);
558 if (cdns->roles[cdns->role]->resume)
559 cdns->roles[cdns->role]->resume(cdns, cdns_power_is_lost(cdns));
562 pm_runtime_disable(dev);
563 pm_runtime_set_active(dev);
564 pm_runtime_enable(dev);
569 EXPORT_SYMBOL_GPL(cdns_resume);
570 #endif /* CONFIG_PM_SLEEP */
572 MODULE_AUTHOR("Peter Chen <peter.chen@nxp.com>");
573 MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
574 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
575 MODULE_DESCRIPTION("Cadence USBSS and USBSSP DRD Driver");
576 MODULE_LICENSE("GPL");