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;
257 dev_dbg(cdns->dev, "role %d -> %d\n", cdns->role, role);
262 static int cdns_idle_role_start(struct cdns *cdns)
267 static void cdns_idle_role_stop(struct cdns *cdns)
269 /* Program Lane swap and bring PHY out of RESET */
270 phy_reset(cdns->usb3_phy);
273 static int cdns_idle_init(struct cdns *cdns)
275 struct cdns_role_driver *rdrv;
277 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
281 rdrv->start = cdns_idle_role_start;
282 rdrv->stop = cdns_idle_role_stop;
283 rdrv->state = CDNS_ROLE_STATE_INACTIVE;
284 rdrv->suspend = NULL;
288 cdns->roles[USB_ROLE_NONE] = rdrv;
294 * cdns_hw_role_switch - switch roles based on HW state
297 int cdns_hw_role_switch(struct cdns *cdns)
299 enum usb_role real_role, current_role;
302 /* Depends on role switch class */
306 pm_runtime_get_sync(cdns->dev);
308 current_role = cdns->role;
309 real_role = cdns_hw_role_state_machine(cdns);
311 /* Do nothing if nothing changed */
312 if (current_role == real_role)
315 cdns_role_stop(cdns);
317 dev_dbg(cdns->dev, "Switching role %d -> %d", current_role, real_role);
319 ret = cdns_role_start(cdns, real_role);
321 /* Back to current role */
322 dev_err(cdns->dev, "set %d has failed, back to %d\n",
323 real_role, current_role);
324 ret = cdns_role_start(cdns, current_role);
326 dev_err(cdns->dev, "back to %d failed too\n",
330 pm_runtime_put_sync(cdns->dev);
335 * cdns_role_get - get current role of controller.
337 * @sw: pointer to USB role switch structure
341 static enum usb_role cdns_role_get(struct usb_role_switch *sw)
343 struct cdns *cdns = usb_role_switch_get_drvdata(sw);
349 * cdns_role_set - set current role of controller.
351 * @sw: pointer to USB role switch structure
352 * @role: the previous role
353 * Handles below events:
354 * - Role switch for dual-role devices
355 * - USB_ROLE_GADGET <--> USB_ROLE_NONE for peripheral-only devices
357 static int cdns_role_set(struct usb_role_switch *sw, enum usb_role role)
359 struct cdns *cdns = usb_role_switch_get_drvdata(sw);
362 pm_runtime_get_sync(cdns->dev);
364 if (cdns->role == role)
367 if (cdns->dr_mode == USB_DR_MODE_HOST) {
377 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL) {
380 case USB_ROLE_DEVICE:
387 cdns_role_stop(cdns);
388 ret = cdns_role_start(cdns, role);
390 dev_err(cdns->dev, "set role %d has failed\n", role);
393 pm_runtime_put_sync(cdns->dev);
399 * cdns_wakeup_irq - interrupt handler for wakeup events
400 * @irq: irq number for cdns3/cdnsp core device
401 * @data: structure of cdns
403 * Returns IRQ_HANDLED or IRQ_NONE
405 static irqreturn_t cdns_wakeup_irq(int irq, void *data)
407 struct cdns *cdns = data;
410 disable_irq_nosync(irq);
411 cdns->wakeup_pending = true;
412 if ((cdns->role == USB_ROLE_HOST) && cdns->host_dev)
413 pm_request_resume(&cdns->host_dev->dev);
422 * cdns_init - probe for cdns3/cdnsp core device
423 * @cdns: Pointer to cdns structure.
425 * Returns 0 on success otherwise negative errno
427 int cdns_init(struct cdns *cdns)
429 struct device *dev = cdns->dev;
432 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
434 dev_err(dev, "error setting dma mask: %d\n", ret);
438 mutex_init(&cdns->mutex);
440 if (device_property_read_bool(dev, "usb-role-switch")) {
441 struct usb_role_switch_desc sw_desc = { };
443 sw_desc.set = cdns_role_set;
444 sw_desc.get = cdns_role_get;
445 sw_desc.allow_userspace_control = true;
446 sw_desc.driver_data = cdns;
447 sw_desc.fwnode = dev->fwnode;
449 cdns->role_sw = usb_role_switch_register(dev, &sw_desc);
450 if (IS_ERR(cdns->role_sw)) {
451 dev_warn(dev, "Unable to register Role Switch\n");
452 return PTR_ERR(cdns->role_sw);
456 if (cdns->wakeup_irq) {
457 ret = devm_request_irq(cdns->dev, cdns->wakeup_irq,
460 dev_name(cdns->dev), cdns);
463 dev_err(cdns->dev, "couldn't register wakeup irq handler\n");
464 goto role_switch_unregister;
468 ret = cdns_drd_init(cdns);
472 ret = cdns_core_init_role(cdns);
476 spin_lock_init(&cdns->lock);
478 dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
483 role_switch_unregister:
485 usb_role_switch_unregister(cdns->role_sw);
489 EXPORT_SYMBOL_GPL(cdns_init);
492 * cdns_remove - unbind drd driver and clean up
493 * @cdns: Pointer to cdns structure.
495 * Returns 0 on success otherwise negative errno
497 int cdns_remove(struct cdns *cdns)
499 cdns_exit_roles(cdns);
500 usb_role_switch_unregister(cdns->role_sw);
504 EXPORT_SYMBOL_GPL(cdns_remove);
506 #ifdef CONFIG_PM_SLEEP
507 int cdns_suspend(struct cdns *cdns)
509 struct device *dev = cdns->dev;
512 if (pm_runtime_status_suspended(dev))
513 pm_runtime_resume(dev);
515 if (cdns->roles[cdns->role]->suspend) {
516 spin_lock_irqsave(&cdns->lock, flags);
517 cdns->roles[cdns->role]->suspend(cdns, false);
518 spin_unlock_irqrestore(&cdns->lock, flags);
523 EXPORT_SYMBOL_GPL(cdns_suspend);
525 int cdns_resume(struct cdns *cdns, u8 set_active)
527 struct device *dev = cdns->dev;
528 enum usb_role real_role;
529 bool role_changed = false;
532 if (cdns_power_is_lost(cdns)) {
534 cdns->role = cdns_role_get(cdns->role_sw);
536 real_role = cdns_hw_role_state_machine(cdns);
537 if (real_role != cdns->role) {
538 ret = cdns_hw_role_switch(cdns);
546 if (cdns->role == USB_ROLE_HOST)
547 ret = cdns_drd_host_on(cdns);
548 else if (cdns->role == USB_ROLE_DEVICE)
549 ret = cdns_drd_gadget_on(cdns);
556 if (cdns->roles[cdns->role]->resume)
557 cdns->roles[cdns->role]->resume(cdns, cdns_power_is_lost(cdns));
560 pm_runtime_disable(dev);
561 pm_runtime_set_active(dev);
562 pm_runtime_enable(dev);
567 EXPORT_SYMBOL_GPL(cdns_resume);
568 #endif /* CONFIG_PM_SLEEP */
570 MODULE_AUTHOR("Peter Chen <peter.chen@nxp.com>");
571 MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
572 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
573 MODULE_DESCRIPTION("Cadence USBSS and USBSSP DRD Driver");
574 MODULE_LICENSE("GPL");