blob: 82ebd618237393855471c1ee53403c3301ce0d76 [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
14#include <common.h>
15#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +053017#include <dm/device-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070018#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <dm/devres.h>
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +053020#include <dm/lists.h>
21#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
Kever Yang1b807052020-03-04 08:59:50 +0800112 dr_mode = usb_get_dr_mode(dev->node);
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
151#if defined(CONFIG_SPL_USB_HOST_SUPPORT) || !defined(CONFIG_SPL_BUILD)
152 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);
335 if (ret)
336 dev_warn(dev, "Unable to get USB2 phy (ret %d)\n", ret);
337
338 ret = generic_phy_init(&cdns->usb2_phy);
339 if (ret)
340 return ret;
341
342 ret = generic_phy_get_by_name(dev, "cdns3,usb3-phy", &cdns->usb3_phy);
343 if (ret)
344 dev_warn(dev, "Unable to get USB3 phy (ret %d)\n", ret);
345
346 ret = generic_phy_init(&cdns->usb3_phy);
347 if (ret)
348 return ret;
349
350 ret = generic_phy_power_on(&cdns->usb2_phy);
351 if (ret)
352 return ret;
353
354 ret = generic_phy_power_on(&cdns->usb3_phy);
355 if (ret)
356 return ret;
357
358 ret = cdns3_drd_init(cdns);
359 if (ret)
360 return ret;
361
362 ret = cdns3_core_init_role(cdns);
363 if (ret)
364 return ret;
365
366 dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
367
368 return 0;
369}
370
371static int cdns3_remove(struct cdns3 *cdns)
372{
373 cdns3_exit_roles(cdns);
374 generic_phy_power_off(&cdns->usb2_phy);
375 generic_phy_power_off(&cdns->usb3_phy);
376 generic_phy_exit(&cdns->usb2_phy);
377 generic_phy_exit(&cdns->usb3_phy);
378 return 0;
379}
380
381static const struct udevice_id cdns3_ids[] = {
382 { .compatible = "cdns,usb3" },
383 { },
384};
385
386int cdns3_bind(struct udevice *parent)
387{
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530388 enum usb_dr_mode dr_mode;
389 struct udevice *dev;
390 const char *driver;
391 const char *name;
Kever Yang1b807052020-03-04 08:59:50 +0800392 ofnode node;
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530393 int ret;
394
Kever Yang1b807052020-03-04 08:59:50 +0800395 node = ofnode_by_compatible(parent->node, "cdns,usb3");
396 if (!ofnode_valid(node)) {
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530397 ret = -ENODEV;
398 goto fail;
399 }
400
Kever Yang1b807052020-03-04 08:59:50 +0800401 name = ofnode_get_name(node);
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530402 dr_mode = usb_get_dr_mode(node);
403
404 switch (dr_mode) {
405#if defined(CONFIG_SPL_USB_HOST_SUPPORT) || \
406 (!defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST))
407 case USB_DR_MODE_HOST:
408 debug("%s: dr_mode: HOST\n", __func__);
409 driver = "cdns-usb3-host";
410 break;
411#endif
412#if CONFIG_IS_ENABLED(DM_USB_GADGET)
413 case USB_DR_MODE_PERIPHERAL:
414 debug("%s: dr_mode: PERIPHERAL\n", __func__);
415 driver = "cdns-usb3-peripheral";
416 break;
417#endif
418 default:
419 printf("%s: unsupported dr_mode\n", __func__);
420 ret = -ENODEV;
421 goto fail;
422 };
423
Kever Yang1b807052020-03-04 08:59:50 +0800424 ret = device_bind_driver_to_node(parent, driver, name, node, &dev);
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530425 if (ret) {
426 printf("%s: not able to bind usb device mode\n",
427 __func__);
428 goto fail;
429 }
430
431 return 0;
432
433fail:
434 /* do not return an error: failing to bind would hang the board */
435 return 0;
436}
437
438#if CONFIG_IS_ENABLED(DM_USB_GADGET)
439static int cdns3_gadget_probe(struct udevice *dev)
440{
441 struct cdns3_gadget_priv *priv = dev_get_priv(dev);
442 struct cdns3 *cdns = &priv->cdns;
443
444 cdns->dev = dev;
445
446 return cdns3_probe(cdns);
447}
448
449static int cdns3_gadget_remove(struct udevice *dev)
450{
451 struct cdns3_gadget_priv *priv = dev_get_priv(dev);
452 struct cdns3 *cdns = &priv->cdns;
453
454 return cdns3_remove(cdns);
455}
456
457U_BOOT_DRIVER(cdns_usb3_peripheral) = {
458 .name = "cdns-usb3-peripheral",
459 .id = UCLASS_USB_GADGET_GENERIC,
460 .of_match = cdns3_ids,
461 .probe = cdns3_gadget_probe,
462 .remove = cdns3_gadget_remove,
463 .priv_auto_alloc_size = sizeof(struct cdns3_gadget_priv),
464 .flags = DM_FLAG_ALLOC_PRIV_DMA,
465};
466#endif
467
468#if defined(CONFIG_SPL_USB_HOST_SUPPORT) || \
469 (!defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST))
470static int cdns3_host_probe(struct udevice *dev)
471{
472 struct cdns3_host_priv *priv = dev_get_priv(dev);
473 struct cdns3 *cdns = &priv->cdns;
474
475 cdns->dev = dev;
476
477 return cdns3_probe(cdns);
478}
479
480static int cdns3_host_remove(struct udevice *dev)
481{
482 struct cdns3_host_priv *priv = dev_get_priv(dev);
483 struct cdns3 *cdns = &priv->cdns;
484
485 return cdns3_remove(cdns);
486}
487
488U_BOOT_DRIVER(cdns_usb3_host) = {
489 .name = "cdns-usb3-host",
490 .id = UCLASS_USB,
491 .of_match = cdns3_ids,
492 .probe = cdns3_host_probe,
493 .remove = cdns3_host_remove,
494 .priv_auto_alloc_size = sizeof(struct cdns3_host_priv),
495 .ops = &xhci_usb_ops,
496 .flags = DM_FLAG_ALLOC_PRIV_DMA,
497};
498#endif