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