blob: b4e931646b886c344c47f571a51356afa41a7efc [file] [log] [blame]
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +05301// SPDX-License-Identifier: GPL-2.0
2/*
3 * Cadence USBSS DRD Driver.
4 *
5 * Copyright (C) 2018-2019 Cadence.
6 * Copyright (C) 2017-2018 NXP
7 * Copyright (C) 2019 Texas Instruments
8 *
9 * Author: Peter Chen <peter.chen@nxp.com>
10 * Pawel Laszczak <pawell@cadence.com>
11 * Roger Quadros <rogerq@ti.com>
12 */
13
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +053014#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +053016#include <dm/device-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <dm/devres.h>
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +053019#include <dm/lists.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060020#include <linux/bug.h>
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +053021#include <linux/kernel.h>
22#include <linux/io.h>
23#include <usb.h>
Vignesh Raghavendra833e4d12019-11-18 19:16:33 +053024#include <usb/xhci.h>
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +053025
26#include "core.h"
27#include "host-export.h"
28#include "gadget-export.h"
29#include "drd.h"
30
31static int cdns3_idle_init(struct cdns3 *cdns);
32
33struct cdns3_host_priv {
34 struct xhci_ctrl xhci_ctrl;
35 struct cdns3 cdns;
36};
37
38struct cdns3_gadget_priv {
39 struct cdns3 cdns;
40};
41
42static inline
43struct cdns3_role_driver *cdns3_get_current_role_driver(struct cdns3 *cdns)
44{
45 WARN_ON(!cdns->roles[cdns->role]);
46 return cdns->roles[cdns->role];
47}
48
49static int cdns3_role_start(struct cdns3 *cdns, enum usb_role role)
50{
51 int ret;
52
53 if (WARN_ON(role > USB_ROLE_DEVICE))
54 return 0;
55
56 mutex_lock(&cdns->mutex);
57 cdns->role = role;
58 mutex_unlock(&cdns->mutex);
59
60 if (!cdns->roles[role])
61 return -ENXIO;
62
63 if (cdns->roles[role]->state == CDNS3_ROLE_STATE_ACTIVE)
64 return 0;
65
66 mutex_lock(&cdns->mutex);
67 ret = cdns->roles[role]->start(cdns);
68 if (!ret)
69 cdns->roles[role]->state = CDNS3_ROLE_STATE_ACTIVE;
70 mutex_unlock(&cdns->mutex);
71
72 return ret;
73}
74
75static void cdns3_role_stop(struct cdns3 *cdns)
76{
77 enum usb_role role = cdns->role;
78
79 if (WARN_ON(role > USB_ROLE_DEVICE))
80 return;
81
82 if (cdns->roles[role]->state == CDNS3_ROLE_STATE_INACTIVE)
83 return;
84
85 mutex_lock(&cdns->mutex);
86 cdns->roles[role]->stop(cdns);
87 cdns->roles[role]->state = CDNS3_ROLE_STATE_INACTIVE;
88 mutex_unlock(&cdns->mutex);
89}
90
91static void cdns3_exit_roles(struct cdns3 *cdns)
92{
93 cdns3_role_stop(cdns);
94 cdns3_drd_exit(cdns);
95}
96
97static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns);
98
99/**
100 * cdns3_core_init_role - initialize role of operation
101 * @cdns: Pointer to cdns3 structure
102 *
103 * Returns 0 on success otherwise negative errno
104 */
105static int cdns3_core_init_role(struct cdns3 *cdns)
106{
107 struct udevice *dev = cdns->dev;
108 enum usb_dr_mode best_dr_mode;
109 enum usb_dr_mode dr_mode;
110 int ret = 0;
111
Simon Glassa7ece582020-12-19 10:40:14 -0700112 dr_mode = usb_get_dr_mode(dev_ofnode(dev));
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530113 cdns->role = USB_ROLE_NONE;
114
115 /*
116 * If driver can't read mode by means of usb_get_dr_mode function then
117 * chooses mode according with Kernel configuration. This setting
118 * can be restricted later depending on strap pin configuration.
119 */
120 if (dr_mode == USB_DR_MODE_UNKNOWN) {
121 if (IS_ENABLED(CONFIG_USB_CDNS3_HOST) &&
122 IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
123 dr_mode = USB_DR_MODE_OTG;
124 else if (IS_ENABLED(CONFIG_USB_CDNS3_HOST))
125 dr_mode = USB_DR_MODE_HOST;
126 else if (IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
127 dr_mode = USB_DR_MODE_PERIPHERAL;
128 }
129
130 /*
131 * At this point cdns->dr_mode contains strap configuration.
132 * Driver try update this setting considering kernel configuration
133 */
134 best_dr_mode = cdns->dr_mode;
135
136 ret = cdns3_idle_init(cdns);
137 if (ret)
138 return ret;
139
140 if (dr_mode == USB_DR_MODE_OTG) {
141 best_dr_mode = cdns->dr_mode;
142 } else if (cdns->dr_mode == USB_DR_MODE_OTG) {
143 best_dr_mode = dr_mode;
144 } else if (cdns->dr_mode != dr_mode) {
145 dev_err(dev, "Incorrect DRD configuration\n");
146 return -EINVAL;
147 }
148
149 dr_mode = best_dr_mode;
150
Simon Glass1f2440c2021-07-10 21:14:29 -0600151#if defined(CONFIG_SPL_USB_HOST) || !defined(CONFIG_SPL_BUILD)
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530152 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
153 ret = cdns3_host_init(cdns);
154 if (ret) {
155 dev_err(dev, "Host initialization failed with %d\n",
156 ret);
157 goto err;
158 }
159 }
160#endif
161
162#if CONFIG_IS_ENABLED(DM_USB_GADGET)
163 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
164 ret = cdns3_gadget_init(cdns);
165 if (ret) {
166 dev_err(dev, "Device initialization failed with %d\n",
167 ret);
168 goto err;
169 }
170 }
171#endif
172
173 cdns->dr_mode = dr_mode;
174
175 ret = cdns3_drd_update_mode(cdns);
176 if (ret)
177 goto err;
178
179 if (cdns->dr_mode != USB_DR_MODE_OTG) {
180 ret = cdns3_hw_role_switch(cdns);
181 if (ret)
182 goto err;
183 }
184
185 return ret;
186err:
187 cdns3_exit_roles(cdns);
188 return ret;
189}
190
191/**
192 * cdsn3_hw_role_state_machine - role switch state machine based on hw events
193 * @cdns: Pointer to controller structure.
194 *
195 * Returns next role to be entered based on hw events.
196 */
197static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns)
198{
199 enum usb_role role;
200 int id, vbus;
201
202 if (cdns->dr_mode != USB_DR_MODE_OTG)
203 goto not_otg;
204
205 id = cdns3_get_id(cdns);
206 vbus = cdns3_get_vbus(cdns);
207
208 /*
209 * Role change state machine
210 * Inputs: ID, VBUS
211 * Previous state: cdns->role
212 * Next state: role
213 */
214 role = cdns->role;
215
216 switch (role) {
217 case USB_ROLE_NONE:
218 /*
219 * Driver treats USB_ROLE_NONE synonymous to IDLE state from
220 * controller specification.
221 */
222 if (!id)
223 role = USB_ROLE_HOST;
224 else if (vbus)
225 role = USB_ROLE_DEVICE;
226 break;
227 case USB_ROLE_HOST: /* from HOST, we can only change to NONE */
228 if (id)
229 role = USB_ROLE_NONE;
230 break;
231 case USB_ROLE_DEVICE: /* from GADGET, we can only change to NONE*/
232 if (!vbus)
233 role = USB_ROLE_NONE;
234 break;
235 }
236
237 dev_dbg(cdns->dev, "role %d -> %d\n", cdns->role, role);
238
239 return role;
240
241not_otg:
242 if (cdns3_is_host(cdns))
243 role = USB_ROLE_HOST;
244 if (cdns3_is_device(cdns))
245 role = USB_ROLE_DEVICE;
246
247 return role;
248}
249
250static int cdns3_idle_role_start(struct cdns3 *cdns)
251{
252 return 0;
253}
254
255static void cdns3_idle_role_stop(struct cdns3 *cdns)
256{
257 /* Program Lane swap and bring PHY out of RESET */
258 generic_phy_reset(&cdns->usb3_phy);
259}
260
261static int cdns3_idle_init(struct cdns3 *cdns)
262{
263 struct cdns3_role_driver *rdrv;
264
265 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
266 if (!rdrv)
267 return -ENOMEM;
268
269 rdrv->start = cdns3_idle_role_start;
270 rdrv->stop = cdns3_idle_role_stop;
271 rdrv->state = CDNS3_ROLE_STATE_INACTIVE;
272 rdrv->suspend = NULL;
273 rdrv->resume = NULL;
274 rdrv->name = "idle";
275
276 cdns->roles[USB_ROLE_NONE] = rdrv;
277
278 return 0;
279}
280
281/**
282 * cdns3_hw_role_switch - switch roles based on HW state
283 * @cdns3: controller
284 */
285int cdns3_hw_role_switch(struct cdns3 *cdns)
286{
287 enum usb_role real_role, current_role;
288 int ret = 0;
289
290 /* Do nothing if role based on syfs. */
291 if (cdns->role_override)
292 return 0;
293
294 current_role = cdns->role;
295 real_role = cdsn3_hw_role_state_machine(cdns);
296
297 /* Do nothing if nothing changed */
298 if (current_role == real_role)
299 goto exit;
300
301 cdns3_role_stop(cdns);
302
303 dev_dbg(cdns->dev, "Switching role %d -> %d", current_role, real_role);
304
305 ret = cdns3_role_start(cdns, real_role);
306 if (ret) {
307 /* Back to current role */
308 dev_err(cdns->dev, "set %d has failed, back to %d\n",
309 real_role, current_role);
310 ret = cdns3_role_start(cdns, current_role);
311 if (ret)
312 dev_err(cdns->dev, "back to %d failed too\n",
313 current_role);
314 }
315exit:
316 return ret;
317}
318
319static int cdns3_probe(struct cdns3 *cdns)
320{
321 struct udevice *dev = cdns->dev;
322 int ret;
323
324 cdns->xhci_regs = dev_remap_addr_name(dev, "xhci");
325 if (!cdns->xhci_regs)
326 return -EINVAL;
327
328 cdns->dev_regs = dev_remap_addr_name(dev, "dev");
329 if (!cdns->dev_regs)
330 return -EINVAL;
331
332 mutex_init(&cdns->mutex);
333
334 ret = generic_phy_get_by_name(dev, "cdns3,usb2-phy", &cdns->usb2_phy);
Roger Quadrosff4e6072024-01-12 14:49:48 +0200335 if (!ret) {
336 ret = generic_phy_init(&cdns->usb2_phy);
337 if (ret) {
338 dev_err(dev, "USB2 PHY init failed: %d\n", ret);
339 return ret;
340 }
341 } else if (ret != -ENOENT && ret != -ENODATA) {
342 dev_err(dev, "Couldn't get USB2 PHY: %d\n", ret);
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530343 return ret;
Roger Quadrosff4e6072024-01-12 14:49:48 +0200344 }
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530345
346 ret = generic_phy_get_by_name(dev, "cdns3,usb3-phy", &cdns->usb3_phy);
Roger Quadrosff4e6072024-01-12 14:49:48 +0200347 if (!ret) {
348 ret = generic_phy_init(&cdns->usb3_phy);
349 if (ret) {
350 dev_err(dev, "USB3 PHY init failed: %d\n", ret);
351 return ret;
352 }
353 } else if (ret != -ENOENT && ret != -ENODATA) {
354 dev_err(dev, "Couldn't get USB3 PHY: %d\n", ret);
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530355 return ret;
Roger Quadrosff4e6072024-01-12 14:49:48 +0200356 }
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530357
358 ret = generic_phy_power_on(&cdns->usb2_phy);
359 if (ret)
360 return ret;
361
362 ret = generic_phy_power_on(&cdns->usb3_phy);
363 if (ret)
364 return ret;
365
366 ret = cdns3_drd_init(cdns);
367 if (ret)
368 return ret;
369
370 ret = cdns3_core_init_role(cdns);
371 if (ret)
372 return ret;
373
374 dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
375
376 return 0;
377}
378
379static int cdns3_remove(struct cdns3 *cdns)
380{
381 cdns3_exit_roles(cdns);
382 generic_phy_power_off(&cdns->usb2_phy);
383 generic_phy_power_off(&cdns->usb3_phy);
384 generic_phy_exit(&cdns->usb2_phy);
385 generic_phy_exit(&cdns->usb3_phy);
386 return 0;
387}
388
389static const struct udevice_id cdns3_ids[] = {
390 { .compatible = "cdns,usb3" },
391 { },
392};
393
394int cdns3_bind(struct udevice *parent)
395{
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530396 enum usb_dr_mode dr_mode;
397 struct udevice *dev;
398 const char *driver;
399 const char *name;
Kever Yang1b807052020-03-04 08:59:50 +0800400 ofnode node;
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530401 int ret;
402
Simon Glassa7ece582020-12-19 10:40:14 -0700403 node = ofnode_by_compatible(dev_ofnode(parent), "cdns,usb3");
Kever Yang1b807052020-03-04 08:59:50 +0800404 if (!ofnode_valid(node)) {
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530405 ret = -ENODEV;
406 goto fail;
407 }
408
Kever Yang1b807052020-03-04 08:59:50 +0800409 name = ofnode_get_name(node);
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530410 dr_mode = usb_get_dr_mode(node);
411
412 switch (dr_mode) {
Simon Glass1f2440c2021-07-10 21:14:29 -0600413#if defined(CONFIG_SPL_USB_HOST) || \
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530414 (!defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST))
415 case USB_DR_MODE_HOST:
416 debug("%s: dr_mode: HOST\n", __func__);
417 driver = "cdns-usb3-host";
418 break;
419#endif
420#if CONFIG_IS_ENABLED(DM_USB_GADGET)
421 case USB_DR_MODE_PERIPHERAL:
422 debug("%s: dr_mode: PERIPHERAL\n", __func__);
423 driver = "cdns-usb3-peripheral";
424 break;
425#endif
426 default:
427 printf("%s: unsupported dr_mode\n", __func__);
428 ret = -ENODEV;
429 goto fail;
430 };
431
Kever Yang1b807052020-03-04 08:59:50 +0800432 ret = device_bind_driver_to_node(parent, driver, name, node, &dev);
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530433 if (ret) {
434 printf("%s: not able to bind usb device mode\n",
435 __func__);
436 goto fail;
437 }
438
439 return 0;
440
441fail:
442 /* do not return an error: failing to bind would hang the board */
443 return 0;
444}
445
446#if CONFIG_IS_ENABLED(DM_USB_GADGET)
447static int cdns3_gadget_probe(struct udevice *dev)
448{
449 struct cdns3_gadget_priv *priv = dev_get_priv(dev);
450 struct cdns3 *cdns = &priv->cdns;
451
452 cdns->dev = dev;
453
454 return cdns3_probe(cdns);
455}
456
457static int cdns3_gadget_remove(struct udevice *dev)
458{
459 struct cdns3_gadget_priv *priv = dev_get_priv(dev);
460 struct cdns3 *cdns = &priv->cdns;
461
462 return cdns3_remove(cdns);
463}
464
465U_BOOT_DRIVER(cdns_usb3_peripheral) = {
466 .name = "cdns-usb3-peripheral",
467 .id = UCLASS_USB_GADGET_GENERIC,
468 .of_match = cdns3_ids,
469 .probe = cdns3_gadget_probe,
470 .remove = cdns3_gadget_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700471 .priv_auto = sizeof(struct cdns3_gadget_priv),
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530472 .flags = DM_FLAG_ALLOC_PRIV_DMA,
473};
474#endif
475
Simon Glass1f2440c2021-07-10 21:14:29 -0600476#if defined(CONFIG_SPL_USB_HOST) || \
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530477 (!defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST))
478static int cdns3_host_probe(struct udevice *dev)
479{
480 struct cdns3_host_priv *priv = dev_get_priv(dev);
481 struct cdns3 *cdns = &priv->cdns;
482
483 cdns->dev = dev;
484
485 return cdns3_probe(cdns);
486}
487
488static int cdns3_host_remove(struct udevice *dev)
489{
490 struct cdns3_host_priv *priv = dev_get_priv(dev);
491 struct cdns3 *cdns = &priv->cdns;
492
493 return cdns3_remove(cdns);
494}
495
496U_BOOT_DRIVER(cdns_usb3_host) = {
497 .name = "cdns-usb3-host",
498 .id = UCLASS_USB,
499 .of_match = cdns3_ids,
500 .probe = cdns3_host_probe,
501 .remove = cdns3_host_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700502 .priv_auto = sizeof(struct cdns3_host_priv),
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530503 .ops = &xhci_usb_ops,
504 .flags = DM_FLAG_ALLOC_PRIV_DMA,
505};
506#endif