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
17 #include <dm/device-internal.h>
18 #include <dm/device_compat.h>
19 #include <dm/devres.h>
21 #include <linux/bug.h>
22 #include <linux/kernel.h>
28 #include "host-export.h"
29 #include "gadget-export.h"
32 static int cdns3_idle_init(struct cdns3 *cdns);
34 struct cdns3_host_priv {
35 struct xhci_ctrl xhci_ctrl;
39 struct cdns3_gadget_priv {
44 struct cdns3_role_driver *cdns3_get_current_role_driver(struct cdns3 *cdns)
46 WARN_ON(!cdns->roles[cdns->role]);
47 return cdns->roles[cdns->role];
50 static int cdns3_role_start(struct cdns3 *cdns, enum usb_role role)
54 if (WARN_ON(role > USB_ROLE_DEVICE))
57 mutex_lock(&cdns->mutex);
59 mutex_unlock(&cdns->mutex);
61 if (!cdns->roles[role])
64 if (cdns->roles[role]->state == CDNS3_ROLE_STATE_ACTIVE)
67 mutex_lock(&cdns->mutex);
68 ret = cdns->roles[role]->start(cdns);
70 cdns->roles[role]->state = CDNS3_ROLE_STATE_ACTIVE;
71 mutex_unlock(&cdns->mutex);
76 static void cdns3_role_stop(struct cdns3 *cdns)
78 enum usb_role role = cdns->role;
80 if (WARN_ON(role > USB_ROLE_DEVICE))
83 if (cdns->roles[role]->state == CDNS3_ROLE_STATE_INACTIVE)
86 mutex_lock(&cdns->mutex);
87 cdns->roles[role]->stop(cdns);
88 cdns->roles[role]->state = CDNS3_ROLE_STATE_INACTIVE;
89 mutex_unlock(&cdns->mutex);
92 static void cdns3_exit_roles(struct cdns3 *cdns)
94 cdns3_role_stop(cdns);
98 static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns);
101 * cdns3_core_init_role - initialize role of operation
102 * @cdns: Pointer to cdns3 structure
104 * Returns 0 on success otherwise negative errno
106 static int cdns3_core_init_role(struct cdns3 *cdns)
108 struct udevice *dev = cdns->dev;
109 enum usb_dr_mode best_dr_mode;
110 enum usb_dr_mode dr_mode;
113 dr_mode = usb_get_dr_mode(dev_ofnode(dev));
114 cdns->role = USB_ROLE_NONE;
117 * If driver can't read mode by means of usb_get_dr_mode function then
118 * chooses mode according with Kernel configuration. This setting
119 * can be restricted later depending on strap pin configuration.
121 if (dr_mode == USB_DR_MODE_UNKNOWN) {
122 if (IS_ENABLED(CONFIG_USB_CDNS3_HOST) &&
123 IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
124 dr_mode = USB_DR_MODE_OTG;
125 else if (IS_ENABLED(CONFIG_USB_CDNS3_HOST))
126 dr_mode = USB_DR_MODE_HOST;
127 else if (IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
128 dr_mode = USB_DR_MODE_PERIPHERAL;
132 * At this point cdns->dr_mode contains strap configuration.
133 * Driver try update this setting considering kernel configuration
135 best_dr_mode = cdns->dr_mode;
137 ret = cdns3_idle_init(cdns);
141 if (dr_mode == USB_DR_MODE_OTG) {
142 best_dr_mode = cdns->dr_mode;
143 } else if (cdns->dr_mode == USB_DR_MODE_OTG) {
144 best_dr_mode = dr_mode;
145 } else if (cdns->dr_mode != dr_mode) {
146 dev_err(dev, "Incorrect DRD configuration\n");
150 dr_mode = best_dr_mode;
152 #if defined(CONFIG_SPL_USB_HOST_SUPPORT) || !defined(CONFIG_SPL_BUILD)
153 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
154 ret = cdns3_host_init(cdns);
156 dev_err(dev, "Host initialization failed with %d\n",
163 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
164 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
165 ret = cdns3_gadget_init(cdns);
167 dev_err(dev, "Device initialization failed with %d\n",
174 cdns->dr_mode = dr_mode;
176 ret = cdns3_drd_update_mode(cdns);
180 if (cdns->dr_mode != USB_DR_MODE_OTG) {
181 ret = cdns3_hw_role_switch(cdns);
188 cdns3_exit_roles(cdns);
193 * cdsn3_hw_role_state_machine - role switch state machine based on hw events
194 * @cdns: Pointer to controller structure.
196 * Returns next role to be entered based on hw events.
198 static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns)
203 if (cdns->dr_mode != USB_DR_MODE_OTG)
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 if (cdns3_is_host(cdns))
244 role = USB_ROLE_HOST;
245 if (cdns3_is_device(cdns))
246 role = USB_ROLE_DEVICE;
251 static int cdns3_idle_role_start(struct cdns3 *cdns)
256 static void cdns3_idle_role_stop(struct cdns3 *cdns)
258 /* Program Lane swap and bring PHY out of RESET */
259 generic_phy_reset(&cdns->usb3_phy);
262 static int cdns3_idle_init(struct cdns3 *cdns)
264 struct cdns3_role_driver *rdrv;
266 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
270 rdrv->start = cdns3_idle_role_start;
271 rdrv->stop = cdns3_idle_role_stop;
272 rdrv->state = CDNS3_ROLE_STATE_INACTIVE;
273 rdrv->suspend = NULL;
277 cdns->roles[USB_ROLE_NONE] = rdrv;
283 * cdns3_hw_role_switch - switch roles based on HW state
286 int cdns3_hw_role_switch(struct cdns3 *cdns)
288 enum usb_role real_role, current_role;
291 /* Do nothing if role based on syfs. */
292 if (cdns->role_override)
295 current_role = cdns->role;
296 real_role = cdsn3_hw_role_state_machine(cdns);
298 /* Do nothing if nothing changed */
299 if (current_role == real_role)
302 cdns3_role_stop(cdns);
304 dev_dbg(cdns->dev, "Switching role %d -> %d", current_role, real_role);
306 ret = cdns3_role_start(cdns, real_role);
308 /* Back to current role */
309 dev_err(cdns->dev, "set %d has failed, back to %d\n",
310 real_role, current_role);
311 ret = cdns3_role_start(cdns, current_role);
313 dev_err(cdns->dev, "back to %d failed too\n",
320 static int cdns3_probe(struct cdns3 *cdns)
322 struct udevice *dev = cdns->dev;
325 cdns->xhci_regs = dev_remap_addr_name(dev, "xhci");
326 if (!cdns->xhci_regs)
329 cdns->dev_regs = dev_remap_addr_name(dev, "dev");
333 mutex_init(&cdns->mutex);
335 ret = generic_phy_get_by_name(dev, "cdns3,usb2-phy", &cdns->usb2_phy);
337 dev_warn(dev, "Unable to get USB2 phy (ret %d)\n", ret);
339 ret = generic_phy_init(&cdns->usb2_phy);
343 ret = generic_phy_get_by_name(dev, "cdns3,usb3-phy", &cdns->usb3_phy);
345 dev_warn(dev, "Unable to get USB3 phy (ret %d)\n", ret);
347 ret = generic_phy_init(&cdns->usb3_phy);
351 ret = generic_phy_power_on(&cdns->usb2_phy);
355 ret = generic_phy_power_on(&cdns->usb3_phy);
359 ret = cdns3_drd_init(cdns);
363 ret = cdns3_core_init_role(cdns);
367 dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
372 static int cdns3_remove(struct cdns3 *cdns)
374 cdns3_exit_roles(cdns);
375 generic_phy_power_off(&cdns->usb2_phy);
376 generic_phy_power_off(&cdns->usb3_phy);
377 generic_phy_exit(&cdns->usb2_phy);
378 generic_phy_exit(&cdns->usb3_phy);
382 static const struct udevice_id cdns3_ids[] = {
383 { .compatible = "cdns,usb3" },
387 int cdns3_bind(struct udevice *parent)
389 enum usb_dr_mode dr_mode;
396 node = ofnode_by_compatible(dev_ofnode(parent), "cdns,usb3");
397 if (!ofnode_valid(node)) {
402 name = ofnode_get_name(node);
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, node, &dev);
427 printf("%s: not able to bind usb device mode\n",
435 /* do not return an error: failing to bind would hang the board */
439 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
440 static int cdns3_gadget_probe(struct udevice *dev)
442 struct cdns3_gadget_priv *priv = dev_get_priv(dev);
443 struct cdns3 *cdns = &priv->cdns;
447 return cdns3_probe(cdns);
450 static int cdns3_gadget_remove(struct udevice *dev)
452 struct cdns3_gadget_priv *priv = dev_get_priv(dev);
453 struct cdns3 *cdns = &priv->cdns;
455 return cdns3_remove(cdns);
458 U_BOOT_DRIVER(cdns_usb3_peripheral) = {
459 .name = "cdns-usb3-peripheral",
460 .id = UCLASS_USB_GADGET_GENERIC,
461 .of_match = cdns3_ids,
462 .probe = cdns3_gadget_probe,
463 .remove = cdns3_gadget_remove,
464 .priv_auto = sizeof(struct cdns3_gadget_priv),
465 .flags = DM_FLAG_ALLOC_PRIV_DMA,
469 #if defined(CONFIG_SPL_USB_HOST_SUPPORT) || \
470 (!defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST))
471 static int cdns3_host_probe(struct udevice *dev)
473 struct cdns3_host_priv *priv = dev_get_priv(dev);
474 struct cdns3 *cdns = &priv->cdns;
478 return cdns3_probe(cdns);
481 static int cdns3_host_remove(struct udevice *dev)
483 struct cdns3_host_priv *priv = dev_get_priv(dev);
484 struct cdns3 *cdns = &priv->cdns;
486 return cdns3_remove(cdns);
489 U_BOOT_DRIVER(cdns_usb3_host) = {
490 .name = "cdns-usb3-host",
492 .of_match = cdns3_ids,
493 .probe = cdns3_host_probe,
494 .remove = cdns3_host_remove,
495 .priv_auto = sizeof(struct cdns3_host_priv),
496 .ops = &xhci_usb_ops,
497 .flags = DM_FLAG_ALLOC_PRIV_DMA,