xref: /openbmc/linux/drivers/usb/cdns3/core.c (revision cc698db4)
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  * cdns_wakeup_irq - interrupt handler for wakeup events
400  * @irq: irq number for cdns3/cdnsp core device
401  * @data: structure of cdns
402  *
403  * Returns IRQ_HANDLED or IRQ_NONE
404  */
405 static irqreturn_t cdns_wakeup_irq(int irq, void *data)
406 {
407 	struct cdns *cdns = data;
408 
409 	if (cdns->in_lpm) {
410 		disable_irq_nosync(irq);
411 		cdns->wakeup_pending = true;
412 		if ((cdns->role == USB_ROLE_HOST) && cdns->host_dev)
413 			pm_request_resume(&cdns->host_dev->dev);
414 
415 		return IRQ_HANDLED;
416 	}
417 
418 	return IRQ_NONE;
419 }
420 
421 /**
422  * cdns_init - probe for cdns3/cdnsp core device
423  * @cdns: Pointer to cdns structure.
424  *
425  * Returns 0 on success otherwise negative errno
426  */
427 int cdns_init(struct cdns *cdns)
428 {
429 	struct device *dev = cdns->dev;
430 	int ret;
431 
432 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
433 	if (ret) {
434 		dev_err(dev, "error setting dma mask: %d\n", ret);
435 		return ret;
436 	}
437 
438 	mutex_init(&cdns->mutex);
439 
440 	if (device_property_read_bool(dev, "usb-role-switch")) {
441 		struct usb_role_switch_desc sw_desc = { };
442 
443 		sw_desc.set = cdns_role_set;
444 		sw_desc.get = cdns_role_get;
445 		sw_desc.allow_userspace_control = true;
446 		sw_desc.driver_data = cdns;
447 		sw_desc.fwnode = dev->fwnode;
448 
449 		cdns->role_sw = usb_role_switch_register(dev, &sw_desc);
450 		if (IS_ERR(cdns->role_sw)) {
451 			dev_warn(dev, "Unable to register Role Switch\n");
452 			return PTR_ERR(cdns->role_sw);
453 		}
454 	}
455 
456 	if (cdns->wakeup_irq) {
457 		ret = devm_request_irq(cdns->dev, cdns->wakeup_irq,
458 						cdns_wakeup_irq,
459 						IRQF_SHARED,
460 						dev_name(cdns->dev), cdns);
461 
462 		if (ret) {
463 			dev_err(cdns->dev, "couldn't register wakeup irq handler\n");
464 			goto role_switch_unregister;
465 		}
466 	}
467 
468 	ret = cdns_drd_init(cdns);
469 	if (ret)
470 		goto init_failed;
471 
472 	ret = cdns_core_init_role(cdns);
473 	if (ret)
474 		goto init_failed;
475 
476 	spin_lock_init(&cdns->lock);
477 
478 	dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
479 
480 	return 0;
481 init_failed:
482 	cdns_drd_exit(cdns);
483 role_switch_unregister:
484 	if (cdns->role_sw)
485 		usb_role_switch_unregister(cdns->role_sw);
486 
487 	return ret;
488 }
489 EXPORT_SYMBOL_GPL(cdns_init);
490 
491 /**
492  * cdns_remove - unbind drd driver and clean up
493  * @cdns: Pointer to cdns structure.
494  *
495  * Returns 0 on success otherwise negative errno
496  */
497 int cdns_remove(struct cdns *cdns)
498 {
499 	cdns_exit_roles(cdns);
500 	usb_role_switch_unregister(cdns->role_sw);
501 
502 	return 0;
503 }
504 EXPORT_SYMBOL_GPL(cdns_remove);
505 
506 #ifdef CONFIG_PM_SLEEP
507 int cdns_suspend(struct cdns *cdns)
508 {
509 	struct device *dev = cdns->dev;
510 	unsigned long flags;
511 
512 	if (pm_runtime_status_suspended(dev))
513 		pm_runtime_resume(dev);
514 
515 	if (cdns->roles[cdns->role]->suspend) {
516 		spin_lock_irqsave(&cdns->lock, flags);
517 		cdns->roles[cdns->role]->suspend(cdns, false);
518 		spin_unlock_irqrestore(&cdns->lock, flags);
519 	}
520 
521 	return 0;
522 }
523 EXPORT_SYMBOL_GPL(cdns_suspend);
524 
525 int cdns_resume(struct cdns *cdns)
526 {
527 	enum usb_role real_role;
528 	bool role_changed = false;
529 	int ret = 0;
530 
531 	if (cdns_power_is_lost(cdns)) {
532 		if (cdns->role_sw) {
533 			cdns->role = cdns_role_get(cdns->role_sw);
534 		} else {
535 			real_role = cdns_hw_role_state_machine(cdns);
536 			if (real_role != cdns->role) {
537 				ret = cdns_hw_role_switch(cdns);
538 				if (ret)
539 					return ret;
540 				role_changed = true;
541 			}
542 		}
543 
544 		if (!role_changed) {
545 			if (cdns->role == USB_ROLE_HOST)
546 				ret = cdns_drd_host_on(cdns);
547 			else if (cdns->role == USB_ROLE_DEVICE)
548 				ret = cdns_drd_gadget_on(cdns);
549 
550 			if (ret)
551 				return ret;
552 		}
553 	}
554 
555 	if (cdns->roles[cdns->role]->resume)
556 		cdns->roles[cdns->role]->resume(cdns, cdns_power_is_lost(cdns));
557 
558 	return 0;
559 }
560 EXPORT_SYMBOL_GPL(cdns_resume);
561 
562 void cdns_set_active(struct cdns *cdns, u8 set_active)
563 {
564 	struct device *dev = cdns->dev;
565 
566 	if (set_active) {
567 		pm_runtime_disable(dev);
568 		pm_runtime_set_active(dev);
569 		pm_runtime_enable(dev);
570 	}
571 
572 	return;
573 }
574 EXPORT_SYMBOL_GPL(cdns_set_active);
575 #endif /* CONFIG_PM_SLEEP */
576 
577 MODULE_AUTHOR("Peter Chen <peter.chen@nxp.com>");
578 MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
579 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
580 MODULE_DESCRIPTION("Cadence USBSS and USBSSP DRD Driver");
581 MODULE_LICENSE("GPL");
582