xref: /openbmc/linux/drivers/usb/cdns3/core.c (revision a325f174)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Cadence USBSS and USBSSP 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 <linux/dma-mapping.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/of.h>
18 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/pm_runtime.h>
22 
23 #include "core.h"
24 #include "host-export.h"
25 #include "drd.h"
26 
27 static int cdns_idle_init(struct cdns *cdns);
28 
29 static int cdns_role_start(struct cdns *cdns, enum usb_role role)
30 {
31 	int ret;
32 
33 	if (WARN_ON(role > USB_ROLE_DEVICE))
34 		return 0;
35 
36 	mutex_lock(&cdns->mutex);
37 	cdns->role = role;
38 	mutex_unlock(&cdns->mutex);
39 
40 	if (!cdns->roles[role])
41 		return -ENXIO;
42 
43 	if (cdns->roles[role]->state == CDNS_ROLE_STATE_ACTIVE)
44 		return 0;
45 
46 	mutex_lock(&cdns->mutex);
47 	ret = cdns->roles[role]->start(cdns);
48 	if (!ret)
49 		cdns->roles[role]->state = CDNS_ROLE_STATE_ACTIVE;
50 	mutex_unlock(&cdns->mutex);
51 
52 	return ret;
53 }
54 
55 static void cdns_role_stop(struct cdns *cdns)
56 {
57 	enum usb_role role = cdns->role;
58 
59 	if (WARN_ON(role > USB_ROLE_DEVICE))
60 		return;
61 
62 	if (cdns->roles[role]->state == CDNS_ROLE_STATE_INACTIVE)
63 		return;
64 
65 	mutex_lock(&cdns->mutex);
66 	cdns->roles[role]->stop(cdns);
67 	cdns->roles[role]->state = CDNS_ROLE_STATE_INACTIVE;
68 	mutex_unlock(&cdns->mutex);
69 }
70 
71 static void cdns_exit_roles(struct cdns *cdns)
72 {
73 	cdns_role_stop(cdns);
74 	cdns_drd_exit(cdns);
75 }
76 
77 /**
78  * cdns_core_init_role - initialize role of operation
79  * @cdns: Pointer to cdns structure
80  *
81  * Returns 0 on success otherwise negative errno
82  */
83 static int cdns_core_init_role(struct cdns *cdns)
84 {
85 	struct device *dev = cdns->dev;
86 	enum usb_dr_mode best_dr_mode;
87 	enum usb_dr_mode dr_mode;
88 	int ret;
89 
90 	dr_mode = usb_get_dr_mode(dev);
91 	cdns->role = USB_ROLE_NONE;
92 
93 	/*
94 	 * If driver can't read mode by means of usb_get_dr_mode function then
95 	 * chooses mode according with Kernel configuration. This setting
96 	 * can be restricted later depending on strap pin configuration.
97 	 */
98 	if (dr_mode == USB_DR_MODE_UNKNOWN) {
99 		if (cdns->version == CDNSP_CONTROLLER_V2) {
100 			if (IS_ENABLED(CONFIG_USB_CDNSP_HOST) &&
101 			    IS_ENABLED(CONFIG_USB_CDNSP_GADGET))
102 				dr_mode = USB_DR_MODE_OTG;
103 			else if (IS_ENABLED(CONFIG_USB_CDNSP_HOST))
104 				dr_mode = USB_DR_MODE_HOST;
105 			else if (IS_ENABLED(CONFIG_USB_CDNSP_GADGET))
106 				dr_mode = USB_DR_MODE_PERIPHERAL;
107 		} else {
108 			if (IS_ENABLED(CONFIG_USB_CDNS3_HOST) &&
109 			    IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
110 				dr_mode = USB_DR_MODE_OTG;
111 			else if (IS_ENABLED(CONFIG_USB_CDNS3_HOST))
112 				dr_mode = USB_DR_MODE_HOST;
113 			else if (IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
114 				dr_mode = USB_DR_MODE_PERIPHERAL;
115 		}
116 	}
117 
118 	/*
119 	 * At this point cdns->dr_mode contains strap configuration.
120 	 * Driver try update this setting considering kernel configuration
121 	 */
122 	best_dr_mode = cdns->dr_mode;
123 
124 	ret = cdns_idle_init(cdns);
125 	if (ret)
126 		return ret;
127 
128 	if (dr_mode == USB_DR_MODE_OTG) {
129 		best_dr_mode = cdns->dr_mode;
130 	} else if (cdns->dr_mode == USB_DR_MODE_OTG) {
131 		best_dr_mode = dr_mode;
132 	} else if (cdns->dr_mode != dr_mode) {
133 		dev_err(dev, "Incorrect DRD configuration\n");
134 		return -EINVAL;
135 	}
136 
137 	dr_mode = best_dr_mode;
138 
139 	if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
140 		if ((cdns->version == CDNSP_CONTROLLER_V2 &&
141 		     IS_ENABLED(CONFIG_USB_CDNSP_HOST)) ||
142 		    (cdns->version < CDNSP_CONTROLLER_V2 &&
143 		     IS_ENABLED(CONFIG_USB_CDNS3_HOST)))
144 			ret = cdns_host_init(cdns);
145 		else
146 			ret = -ENXIO;
147 
148 		if (ret) {
149 			dev_err(dev, "Host initialization failed with %d\n",
150 				ret);
151 			goto err;
152 		}
153 	}
154 
155 	if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
156 		if (cdns->gadget_init)
157 			ret = cdns->gadget_init(cdns);
158 		else
159 			ret = -ENXIO;
160 
161 		if (ret) {
162 			dev_err(dev, "Device initialization failed with %d\n",
163 				ret);
164 			goto err;
165 		}
166 	}
167 
168 	cdns->dr_mode = dr_mode;
169 
170 	ret = cdns_drd_update_mode(cdns);
171 	if (ret)
172 		goto err;
173 
174 	/* Initialize idle role to start with */
175 	ret = cdns_role_start(cdns, USB_ROLE_NONE);
176 	if (ret)
177 		goto err;
178 
179 	switch (cdns->dr_mode) {
180 	case USB_DR_MODE_OTG:
181 		ret = cdns_hw_role_switch(cdns);
182 		if (ret)
183 			goto err;
184 		break;
185 	case USB_DR_MODE_PERIPHERAL:
186 		ret = cdns_role_start(cdns, USB_ROLE_DEVICE);
187 		if (ret)
188 			goto err;
189 		break;
190 	case USB_DR_MODE_HOST:
191 		ret = cdns_role_start(cdns, USB_ROLE_HOST);
192 		if (ret)
193 			goto err;
194 		break;
195 	default:
196 		ret = -EINVAL;
197 		goto err;
198 	}
199 
200 	return 0;
201 err:
202 	cdns_exit_roles(cdns);
203 	return ret;
204 }
205 
206 /**
207  * cdns_hw_role_state_machine  - role switch state machine based on hw events.
208  * @cdns: Pointer to controller structure.
209  *
210  * Returns next role to be entered based on hw events.
211  */
212 static enum usb_role cdns_hw_role_state_machine(struct cdns *cdns)
213 {
214 	enum usb_role role = USB_ROLE_NONE;
215 	int id, vbus;
216 
217 	if (cdns->dr_mode != USB_DR_MODE_OTG) {
218 		if (cdns_is_host(cdns))
219 			role = USB_ROLE_HOST;
220 		if (cdns_is_device(cdns))
221 			role = USB_ROLE_DEVICE;
222 
223 		return role;
224 	}
225 
226 	id = cdns_get_id(cdns);
227 	vbus = cdns_get_vbus(cdns);
228 
229 	/*
230 	 * Role change state machine
231 	 * Inputs: ID, VBUS
232 	 * Previous state: cdns->role
233 	 * Next state: role
234 	 */
235 	role = cdns->role;
236 
237 	switch (role) {
238 	case USB_ROLE_NONE:
239 		/*
240 		 * Driver treats USB_ROLE_NONE synonymous to IDLE state from
241 		 * controller specification.
242 		 */
243 		if (!id)
244 			role = USB_ROLE_HOST;
245 		else if (vbus)
246 			role = USB_ROLE_DEVICE;
247 		break;
248 	case USB_ROLE_HOST: /* from HOST, we can only change to NONE */
249 		if (id)
250 			role = USB_ROLE_NONE;
251 		break;
252 	case USB_ROLE_DEVICE: /* from GADGET, we can only change to NONE*/
253 		if (!vbus)
254 			role = USB_ROLE_NONE;
255 		break;
256 	}
257 
258 	dev_dbg(cdns->dev, "role %d -> %d\n", cdns->role, role);
259 
260 	return role;
261 }
262 
263 static int cdns_idle_role_start(struct cdns *cdns)
264 {
265 	return 0;
266 }
267 
268 static void cdns_idle_role_stop(struct cdns *cdns)
269 {
270 	/* Program Lane swap and bring PHY out of RESET */
271 	phy_reset(cdns->usb3_phy);
272 }
273 
274 static int cdns_idle_init(struct cdns *cdns)
275 {
276 	struct cdns_role_driver *rdrv;
277 
278 	rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
279 	if (!rdrv)
280 		return -ENOMEM;
281 
282 	rdrv->start = cdns_idle_role_start;
283 	rdrv->stop = cdns_idle_role_stop;
284 	rdrv->state = CDNS_ROLE_STATE_INACTIVE;
285 	rdrv->suspend = NULL;
286 	rdrv->resume = NULL;
287 	rdrv->name = "idle";
288 
289 	cdns->roles[USB_ROLE_NONE] = rdrv;
290 
291 	return 0;
292 }
293 
294 /**
295  * cdns_hw_role_switch - switch roles based on HW state
296  * @cdns: controller
297  */
298 int cdns_hw_role_switch(struct cdns *cdns)
299 {
300 	enum usb_role real_role, current_role;
301 	int ret = 0;
302 
303 	/* Depends on role switch class */
304 	if (cdns->role_sw)
305 		return 0;
306 
307 	pm_runtime_get_sync(cdns->dev);
308 
309 	current_role = cdns->role;
310 	real_role = cdns_hw_role_state_machine(cdns);
311 
312 	/* Do nothing if nothing changed */
313 	if (current_role == real_role)
314 		goto exit;
315 
316 	cdns_role_stop(cdns);
317 
318 	dev_dbg(cdns->dev, "Switching role %d -> %d", current_role, real_role);
319 
320 	ret = cdns_role_start(cdns, real_role);
321 	if (ret) {
322 		/* Back to current role */
323 		dev_err(cdns->dev, "set %d has failed, back to %d\n",
324 			real_role, current_role);
325 		ret = cdns_role_start(cdns, current_role);
326 		if (ret)
327 			dev_err(cdns->dev, "back to %d failed too\n",
328 				current_role);
329 	}
330 exit:
331 	pm_runtime_put_sync(cdns->dev);
332 	return ret;
333 }
334 
335 /**
336  * cdns_role_get - get current role of controller.
337  *
338  * @sw: pointer to USB role switch structure
339  *
340  * Returns role
341  */
342 static enum usb_role cdns_role_get(struct usb_role_switch *sw)
343 {
344 	struct cdns *cdns = usb_role_switch_get_drvdata(sw);
345 
346 	return cdns->role;
347 }
348 
349 /**
350  * cdns_role_set - set current role of controller.
351  *
352  * @sw: pointer to USB role switch structure
353  * @role: the previous role
354  * Handles below events:
355  * - Role switch for dual-role devices
356  * - USB_ROLE_GADGET <--> USB_ROLE_NONE for peripheral-only devices
357  */
358 static int cdns_role_set(struct usb_role_switch *sw, enum usb_role role)
359 {
360 	struct cdns *cdns = usb_role_switch_get_drvdata(sw);
361 	int ret = 0;
362 
363 	pm_runtime_get_sync(cdns->dev);
364 
365 	if (cdns->role == role)
366 		goto pm_put;
367 
368 	if (cdns->dr_mode == USB_DR_MODE_HOST) {
369 		switch (role) {
370 		case USB_ROLE_NONE:
371 		case USB_ROLE_HOST:
372 			break;
373 		default:
374 			goto pm_put;
375 		}
376 	}
377 
378 	if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL) {
379 		switch (role) {
380 		case USB_ROLE_NONE:
381 		case USB_ROLE_DEVICE:
382 			break;
383 		default:
384 			goto pm_put;
385 		}
386 	}
387 
388 	cdns_role_stop(cdns);
389 	ret = cdns_role_start(cdns, role);
390 	if (ret)
391 		dev_err(cdns->dev, "set role %d has failed\n", role);
392 
393 pm_put:
394 	pm_runtime_put_sync(cdns->dev);
395 	return ret;
396 }
397 
398 
399 /**
400  * cdns_wakeup_irq - interrupt handler for wakeup events
401  * @irq: irq number for cdns3/cdnsp core device
402  * @data: structure of cdns
403  *
404  * Returns IRQ_HANDLED or IRQ_NONE
405  */
406 static irqreturn_t cdns_wakeup_irq(int irq, void *data)
407 {
408 	struct cdns *cdns = data;
409 
410 	if (cdns->in_lpm) {
411 		disable_irq_nosync(irq);
412 		cdns->wakeup_pending = true;
413 		if ((cdns->role == USB_ROLE_HOST) && cdns->host_dev)
414 			pm_request_resume(&cdns->host_dev->dev);
415 
416 		return IRQ_HANDLED;
417 	}
418 
419 	return IRQ_NONE;
420 }
421 
422 /**
423  * cdns_init - probe for cdns3/cdnsp core device
424  * @cdns: Pointer to cdns structure.
425  *
426  * Returns 0 on success otherwise negative errno
427  */
428 int cdns_init(struct cdns *cdns)
429 {
430 	struct device *dev = cdns->dev;
431 	int ret;
432 
433 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
434 	if (ret) {
435 		dev_err(dev, "error setting dma mask: %d\n", ret);
436 		return ret;
437 	}
438 
439 	mutex_init(&cdns->mutex);
440 
441 	if (device_property_read_bool(dev, "usb-role-switch")) {
442 		struct usb_role_switch_desc sw_desc = { };
443 
444 		sw_desc.set = cdns_role_set;
445 		sw_desc.get = cdns_role_get;
446 		sw_desc.allow_userspace_control = true;
447 		sw_desc.driver_data = cdns;
448 		sw_desc.fwnode = dev->fwnode;
449 
450 		cdns->role_sw = usb_role_switch_register(dev, &sw_desc);
451 		if (IS_ERR(cdns->role_sw)) {
452 			dev_warn(dev, "Unable to register Role Switch\n");
453 			return PTR_ERR(cdns->role_sw);
454 		}
455 	}
456 
457 	if (cdns->wakeup_irq) {
458 		ret = devm_request_irq(cdns->dev, cdns->wakeup_irq,
459 						cdns_wakeup_irq,
460 						IRQF_SHARED,
461 						dev_name(cdns->dev), cdns);
462 
463 		if (ret) {
464 			dev_err(cdns->dev, "couldn't register wakeup irq handler\n");
465 			goto role_switch_unregister;
466 		}
467 	}
468 
469 	ret = cdns_drd_init(cdns);
470 	if (ret)
471 		goto init_failed;
472 
473 	ret = cdns_core_init_role(cdns);
474 	if (ret)
475 		goto init_failed;
476 
477 	spin_lock_init(&cdns->lock);
478 
479 	dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
480 
481 	return 0;
482 init_failed:
483 	cdns_drd_exit(cdns);
484 role_switch_unregister:
485 	if (cdns->role_sw)
486 		usb_role_switch_unregister(cdns->role_sw);
487 
488 	return ret;
489 }
490 EXPORT_SYMBOL_GPL(cdns_init);
491 
492 /**
493  * cdns_remove - unbind drd driver and clean up
494  * @cdns: Pointer to cdns structure.
495  *
496  * Returns 0 on success otherwise negative errno
497  */
498 int cdns_remove(struct cdns *cdns)
499 {
500 	cdns_exit_roles(cdns);
501 	usb_role_switch_unregister(cdns->role_sw);
502 
503 	return 0;
504 }
505 EXPORT_SYMBOL_GPL(cdns_remove);
506 
507 #ifdef CONFIG_PM_SLEEP
508 int cdns_suspend(struct cdns *cdns)
509 {
510 	struct device *dev = cdns->dev;
511 	unsigned long flags;
512 
513 	if (pm_runtime_status_suspended(dev))
514 		pm_runtime_resume(dev);
515 
516 	if (cdns->roles[cdns->role]->suspend) {
517 		spin_lock_irqsave(&cdns->lock, flags);
518 		cdns->roles[cdns->role]->suspend(cdns, false);
519 		spin_unlock_irqrestore(&cdns->lock, flags);
520 	}
521 
522 	return 0;
523 }
524 EXPORT_SYMBOL_GPL(cdns_suspend);
525 
526 int cdns_resume(struct cdns *cdns)
527 {
528 	enum usb_role real_role;
529 	bool role_changed = false;
530 	int ret = 0;
531 
532 	if (cdns_power_is_lost(cdns)) {
533 		if (cdns->role_sw) {
534 			cdns->role = cdns_role_get(cdns->role_sw);
535 		} else {
536 			real_role = cdns_hw_role_state_machine(cdns);
537 			if (real_role != cdns->role) {
538 				ret = cdns_hw_role_switch(cdns);
539 				if (ret)
540 					return ret;
541 				role_changed = true;
542 			}
543 		}
544 
545 		if (!role_changed) {
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);
550 
551 			if (ret)
552 				return ret;
553 		}
554 	}
555 
556 	if (cdns->roles[cdns->role]->resume)
557 		cdns->roles[cdns->role]->resume(cdns, cdns_power_is_lost(cdns));
558 
559 	return 0;
560 }
561 EXPORT_SYMBOL_GPL(cdns_resume);
562 
563 void cdns_set_active(struct cdns *cdns, u8 set_active)
564 {
565 	struct device *dev = cdns->dev;
566 
567 	if (set_active) {
568 		pm_runtime_disable(dev);
569 		pm_runtime_set_active(dev);
570 		pm_runtime_enable(dev);
571 	}
572 
573 	return;
574 }
575 EXPORT_SYMBOL_GPL(cdns_set_active);
576 #endif /* CONFIG_PM_SLEEP */
577 
578 MODULE_AUTHOR("Peter Chen <peter.chen@nxp.com>");
579 MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
580 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
581 MODULE_DESCRIPTION("Cadence USBSS and USBSSP DRD Driver");
582 MODULE_LICENSE("GPL");
583