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
9 * Author: Peter Chen <peter.chen@nxp.com>
10 * Pawel Laszczak <pawell@cadence.com>
11 * Roger Quadros <rogerq@ti.com>
16 #include <dm/device-internal.h>
17 #include <dm/device_compat.h>
18 #include <dm/devres.h>
20 #include <linux/kernel.h>
26 #include "host-export.h"
27 #include "gadget-export.h"
30 static int cdns3_idle_init(struct cdns3 *cdns);
32 struct cdns3_host_priv {
33 struct xhci_ctrl xhci_ctrl;
37 struct cdns3_gadget_priv {
42 struct cdns3_role_driver *cdns3_get_current_role_driver(struct cdns3 *cdns)
44 WARN_ON(!cdns->roles[cdns->role]);
45 return cdns->roles[cdns->role];
48 static int cdns3_role_start(struct cdns3 *cdns, enum usb_role role)
52 if (WARN_ON(role > USB_ROLE_DEVICE))
55 mutex_lock(&cdns->mutex);
57 mutex_unlock(&cdns->mutex);
59 if (!cdns->roles[role])
62 if (cdns->roles[role]->state == CDNS3_ROLE_STATE_ACTIVE)
65 mutex_lock(&cdns->mutex);
66 ret = cdns->roles[role]->start(cdns);
68 cdns->roles[role]->state = CDNS3_ROLE_STATE_ACTIVE;
69 mutex_unlock(&cdns->mutex);
74 static void cdns3_role_stop(struct cdns3 *cdns)
76 enum usb_role role = cdns->role;
78 if (WARN_ON(role > USB_ROLE_DEVICE))
81 if (cdns->roles[role]->state == CDNS3_ROLE_STATE_INACTIVE)
84 mutex_lock(&cdns->mutex);
85 cdns->roles[role]->stop(cdns);
86 cdns->roles[role]->state = CDNS3_ROLE_STATE_INACTIVE;
87 mutex_unlock(&cdns->mutex);
90 static void cdns3_exit_roles(struct cdns3 *cdns)
92 cdns3_role_stop(cdns);
96 static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns);
99 * cdns3_core_init_role - initialize role of operation
100 * @cdns: Pointer to cdns3 structure
102 * Returns 0 on success otherwise negative errno
104 static int cdns3_core_init_role(struct cdns3 *cdns)
106 struct udevice *dev = cdns->dev;
107 enum usb_dr_mode best_dr_mode;
108 enum usb_dr_mode dr_mode;
111 dr_mode = usb_get_dr_mode(dev_of_offset(dev));
112 cdns->role = USB_ROLE_NONE;
115 * If driver can't read mode by means of usb_get_dr_mode function then
116 * chooses mode according with Kernel configuration. This setting
117 * can be restricted later depending on strap pin configuration.
119 if (dr_mode == USB_DR_MODE_UNKNOWN) {
120 if (IS_ENABLED(CONFIG_USB_CDNS3_HOST) &&
121 IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
122 dr_mode = USB_DR_MODE_OTG;
123 else if (IS_ENABLED(CONFIG_USB_CDNS3_HOST))
124 dr_mode = USB_DR_MODE_HOST;
125 else if (IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
126 dr_mode = USB_DR_MODE_PERIPHERAL;
130 * At this point cdns->dr_mode contains strap configuration.
131 * Driver try update this setting considering kernel configuration
133 best_dr_mode = cdns->dr_mode;
135 ret = cdns3_idle_init(cdns);
139 if (dr_mode == USB_DR_MODE_OTG) {
140 best_dr_mode = cdns->dr_mode;
141 } else if (cdns->dr_mode == USB_DR_MODE_OTG) {
142 best_dr_mode = dr_mode;
143 } else if (cdns->dr_mode != dr_mode) {
144 dev_err(dev, "Incorrect DRD configuration\n");
148 dr_mode = best_dr_mode;
150 #if defined(CONFIG_SPL_USB_HOST_SUPPORT) || !defined(CONFIG_SPL_BUILD)
151 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
152 ret = cdns3_host_init(cdns);
154 dev_err(dev, "Host initialization failed with %d\n",
161 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
162 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
163 ret = cdns3_gadget_init(cdns);
165 dev_err(dev, "Device initialization failed with %d\n",
172 cdns->dr_mode = dr_mode;
174 ret = cdns3_drd_update_mode(cdns);
178 if (cdns->dr_mode != USB_DR_MODE_OTG) {
179 ret = cdns3_hw_role_switch(cdns);
186 cdns3_exit_roles(cdns);
191 * cdsn3_hw_role_state_machine - role switch state machine based on hw events
192 * @cdns: Pointer to controller structure.
194 * Returns next role to be entered based on hw events.
196 static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns)
201 if (cdns->dr_mode != USB_DR_MODE_OTG)
204 id = cdns3_get_id(cdns);
205 vbus = cdns3_get_vbus(cdns);
208 * Role change state machine
210 * Previous state: cdns->role
218 * Driver treats USB_ROLE_NONE synonymous to IDLE state from
219 * controller specification.
222 role = USB_ROLE_HOST;
224 role = USB_ROLE_DEVICE;
226 case USB_ROLE_HOST: /* from HOST, we can only change to NONE */
228 role = USB_ROLE_NONE;
230 case USB_ROLE_DEVICE: /* from GADGET, we can only change to NONE*/
232 role = USB_ROLE_NONE;
236 dev_dbg(cdns->dev, "role %d -> %d\n", cdns->role, role);
241 if (cdns3_is_host(cdns))
242 role = USB_ROLE_HOST;
243 if (cdns3_is_device(cdns))
244 role = USB_ROLE_DEVICE;
249 static int cdns3_idle_role_start(struct cdns3 *cdns)
254 static void cdns3_idle_role_stop(struct cdns3 *cdns)
256 /* Program Lane swap and bring PHY out of RESET */
257 generic_phy_reset(&cdns->usb3_phy);
260 static int cdns3_idle_init(struct cdns3 *cdns)
262 struct cdns3_role_driver *rdrv;
264 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
268 rdrv->start = cdns3_idle_role_start;
269 rdrv->stop = cdns3_idle_role_stop;
270 rdrv->state = CDNS3_ROLE_STATE_INACTIVE;
271 rdrv->suspend = NULL;
275 cdns->roles[USB_ROLE_NONE] = rdrv;
281 * cdns3_hw_role_switch - switch roles based on HW state
284 int cdns3_hw_role_switch(struct cdns3 *cdns)
286 enum usb_role real_role, current_role;
289 /* Do nothing if role based on syfs. */
290 if (cdns->role_override)
293 current_role = cdns->role;
294 real_role = cdsn3_hw_role_state_machine(cdns);
296 /* Do nothing if nothing changed */
297 if (current_role == real_role)
300 cdns3_role_stop(cdns);
302 dev_dbg(cdns->dev, "Switching role %d -> %d", current_role, real_role);
304 ret = cdns3_role_start(cdns, real_role);
306 /* Back to current role */
307 dev_err(cdns->dev, "set %d has failed, back to %d\n",
308 real_role, current_role);
309 ret = cdns3_role_start(cdns, current_role);
311 dev_err(cdns->dev, "back to %d failed too\n",
318 static int cdns3_probe(struct cdns3 *cdns)
320 struct udevice *dev = cdns->dev;
323 cdns->xhci_regs = dev_remap_addr_name(dev, "xhci");
324 if (!cdns->xhci_regs)
327 cdns->dev_regs = dev_remap_addr_name(dev, "dev");
331 mutex_init(&cdns->mutex);
333 ret = generic_phy_get_by_name(dev, "cdns3,usb2-phy", &cdns->usb2_phy);
335 dev_warn(dev, "Unable to get USB2 phy (ret %d)\n", ret);
337 ret = generic_phy_init(&cdns->usb2_phy);
341 ret = generic_phy_get_by_name(dev, "cdns3,usb3-phy", &cdns->usb3_phy);
343 dev_warn(dev, "Unable to get USB3 phy (ret %d)\n", ret);
345 ret = generic_phy_init(&cdns->usb3_phy);
349 ret = generic_phy_power_on(&cdns->usb2_phy);
353 ret = generic_phy_power_on(&cdns->usb3_phy);
357 ret = cdns3_drd_init(cdns);
361 ret = cdns3_core_init_role(cdns);
365 dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
370 static int cdns3_remove(struct cdns3 *cdns)
372 cdns3_exit_roles(cdns);
373 generic_phy_power_off(&cdns->usb2_phy);
374 generic_phy_power_off(&cdns->usb3_phy);
375 generic_phy_exit(&cdns->usb2_phy);
376 generic_phy_exit(&cdns->usb3_phy);
380 static const struct udevice_id cdns3_ids[] = {
381 { .compatible = "cdns,usb3" },
385 int cdns3_bind(struct udevice *parent)
387 int from = dev_of_offset(parent);
388 const void *fdt = gd->fdt_blob;
389 enum usb_dr_mode dr_mode;
396 node = fdt_node_offset_by_compatible(fdt, from, "cdns,usb3");
402 name = fdt_get_name(fdt, node, NULL);
403 dr_mode = usb_get_dr_mode(node);
406 #if defined(CONFIG_SPL_USB_HOST_SUPPORT) || \
407 (!defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST))
408 case USB_DR_MODE_HOST:
409 debug("%s: dr_mode: HOST\n", __func__);
410 driver = "cdns-usb3-host";
413 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
414 case USB_DR_MODE_PERIPHERAL:
415 debug("%s: dr_mode: PERIPHERAL\n", __func__);
416 driver = "cdns-usb3-peripheral";
420 printf("%s: unsupported dr_mode\n", __func__);
425 ret = device_bind_driver_to_node(parent, driver, name,
426 offset_to_ofnode(node), &dev);
428 printf("%s: not able to bind usb device mode\n",
436 /* do not return an error: failing to bind would hang the board */
440 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
441 static int cdns3_gadget_probe(struct udevice *dev)
443 struct cdns3_gadget_priv *priv = dev_get_priv(dev);
444 struct cdns3 *cdns = &priv->cdns;
448 return cdns3_probe(cdns);
451 static int cdns3_gadget_remove(struct udevice *dev)
453 struct cdns3_gadget_priv *priv = dev_get_priv(dev);
454 struct cdns3 *cdns = &priv->cdns;
456 return cdns3_remove(cdns);
459 U_BOOT_DRIVER(cdns_usb3_peripheral) = {
460 .name = "cdns-usb3-peripheral",
461 .id = UCLASS_USB_GADGET_GENERIC,
462 .of_match = cdns3_ids,
463 .probe = cdns3_gadget_probe,
464 .remove = cdns3_gadget_remove,
465 .priv_auto_alloc_size = sizeof(struct cdns3_gadget_priv),
466 .flags = DM_FLAG_ALLOC_PRIV_DMA,
470 #if defined(CONFIG_SPL_USB_HOST_SUPPORT) || \
471 (!defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST))
472 static int cdns3_host_probe(struct udevice *dev)
474 struct cdns3_host_priv *priv = dev_get_priv(dev);
475 struct cdns3 *cdns = &priv->cdns;
479 return cdns3_probe(cdns);
482 static int cdns3_host_remove(struct udevice *dev)
484 struct cdns3_host_priv *priv = dev_get_priv(dev);
485 struct cdns3 *cdns = &priv->cdns;
487 return cdns3_remove(cdns);
490 U_BOOT_DRIVER(cdns_usb3_host) = {
491 .name = "cdns-usb3-host",
493 .of_match = cdns3_ids,
494 .probe = cdns3_host_probe,
495 .remove = cdns3_host_remove,
496 .priv_auto_alloc_size = sizeof(struct cdns3_host_priv),
497 .ops = &xhci_usb_ops,
498 .flags = DM_FLAG_ALLOC_PRIV_DMA,