Linux-libre 4.4.137-gnu
[librecmc/linux-libre.git] / drivers / usb / chipidea / core.c
1 /*
2  * core.c - ChipIdea USB IP core family device controller
3  *
4  * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
5  *
6  * Author: David Lopo
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 /*
14  * Description: ChipIdea USB IP core family device controller
15  *
16  * This driver is composed of several blocks:
17  * - HW:     hardware interface
18  * - DBG:    debug facilities (optional)
19  * - UTIL:   utilities
20  * - ISR:    interrupts handling
21  * - ENDPT:  endpoint operations (Gadget API)
22  * - GADGET: gadget operations (Gadget API)
23  * - BUS:    bus glue code, bus abstraction layer
24  *
25  * Compile Options
26  * - CONFIG_USB_CHIPIDEA_DEBUG: enable debug facilities
27  * - STALL_IN:  non-empty bulk-in pipes cannot be halted
28  *              if defined mass storage compliance succeeds but with warnings
29  *              => case 4: Hi >  Dn
30  *              => case 5: Hi >  Di
31  *              => case 8: Hi <> Do
32  *              if undefined usbtest 13 fails
33  * - TRACE:     enable function tracing (depends on DEBUG)
34  *
35  * Main Features
36  * - Chapter 9 & Mass Storage Compliance with Gadget File Storage
37  * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined)
38  * - Normal & LPM support
39  *
40  * USBTEST Report
41  * - OK: 0-12, 13 (STALL_IN defined) & 14
42  * - Not Supported: 15 & 16 (ISO)
43  *
44  * TODO List
45  * - Suspend & Remote Wakeup
46  */
47 #include <linux/delay.h>
48 #include <linux/device.h>
49 #include <linux/dma-mapping.h>
50 #include <linux/extcon.h>
51 #include <linux/phy/phy.h>
52 #include <linux/platform_device.h>
53 #include <linux/module.h>
54 #include <linux/idr.h>
55 #include <linux/interrupt.h>
56 #include <linux/io.h>
57 #include <linux/kernel.h>
58 #include <linux/slab.h>
59 #include <linux/pm_runtime.h>
60 #include <linux/usb/ch9.h>
61 #include <linux/usb/gadget.h>
62 #include <linux/usb/otg.h>
63 #include <linux/usb/chipidea.h>
64 #include <linux/usb/of.h>
65 #include <linux/of.h>
66 #include <linux/phy.h>
67 #include <linux/regulator/consumer.h>
68 #include <linux/usb/ehci_def.h>
69
70 #include "ci.h"
71 #include "udc.h"
72 #include "bits.h"
73 #include "host.h"
74 #include "debug.h"
75 #include "otg.h"
76 #include "otg_fsm.h"
77
78 /* Controller register map */
79 static const u8 ci_regs_nolpm[] = {
80         [CAP_CAPLENGTH]         = 0x00U,
81         [CAP_HCCPARAMS]         = 0x08U,
82         [CAP_DCCPARAMS]         = 0x24U,
83         [CAP_TESTMODE]          = 0x38U,
84         [OP_USBCMD]             = 0x00U,
85         [OP_USBSTS]             = 0x04U,
86         [OP_USBINTR]            = 0x08U,
87         [OP_DEVICEADDR]         = 0x14U,
88         [OP_ENDPTLISTADDR]      = 0x18U,
89         [OP_TTCTRL]             = 0x1CU,
90         [OP_BURSTSIZE]          = 0x20U,
91         [OP_PORTSC]             = 0x44U,
92         [OP_DEVLC]              = 0x84U,
93         [OP_OTGSC]              = 0x64U,
94         [OP_USBMODE]            = 0x68U,
95         [OP_ENDPTSETUPSTAT]     = 0x6CU,
96         [OP_ENDPTPRIME]         = 0x70U,
97         [OP_ENDPTFLUSH]         = 0x74U,
98         [OP_ENDPTSTAT]          = 0x78U,
99         [OP_ENDPTCOMPLETE]      = 0x7CU,
100         [OP_ENDPTCTRL]          = 0x80U,
101 };
102
103 static const u8 ci_regs_lpm[] = {
104         [CAP_CAPLENGTH]         = 0x00U,
105         [CAP_HCCPARAMS]         = 0x08U,
106         [CAP_DCCPARAMS]         = 0x24U,
107         [CAP_TESTMODE]          = 0xFCU,
108         [OP_USBCMD]             = 0x00U,
109         [OP_USBSTS]             = 0x04U,
110         [OP_USBINTR]            = 0x08U,
111         [OP_DEVICEADDR]         = 0x14U,
112         [OP_ENDPTLISTADDR]      = 0x18U,
113         [OP_TTCTRL]             = 0x1CU,
114         [OP_BURSTSIZE]          = 0x20U,
115         [OP_PORTSC]             = 0x44U,
116         [OP_DEVLC]              = 0x84U,
117         [OP_OTGSC]              = 0xC4U,
118         [OP_USBMODE]            = 0xC8U,
119         [OP_ENDPTSETUPSTAT]     = 0xD8U,
120         [OP_ENDPTPRIME]         = 0xDCU,
121         [OP_ENDPTFLUSH]         = 0xE0U,
122         [OP_ENDPTSTAT]          = 0xE4U,
123         [OP_ENDPTCOMPLETE]      = 0xE8U,
124         [OP_ENDPTCTRL]          = 0xECU,
125 };
126
127 static void hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm)
128 {
129         int i;
130
131         for (i = 0; i < OP_ENDPTCTRL; i++)
132                 ci->hw_bank.regmap[i] =
133                         (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) +
134                         (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]);
135
136         for (; i <= OP_LAST; i++)
137                 ci->hw_bank.regmap[i] = ci->hw_bank.op +
138                         4 * (i - OP_ENDPTCTRL) +
139                         (is_lpm
140                          ? ci_regs_lpm[OP_ENDPTCTRL]
141                          : ci_regs_nolpm[OP_ENDPTCTRL]);
142
143 }
144
145 static enum ci_revision ci_get_revision(struct ci_hdrc *ci)
146 {
147         int ver = hw_read_id_reg(ci, ID_ID, VERSION) >> __ffs(VERSION);
148         enum ci_revision rev = CI_REVISION_UNKNOWN;
149
150         if (ver == 0x2) {
151                 rev = hw_read_id_reg(ci, ID_ID, REVISION)
152                         >> __ffs(REVISION);
153                 rev += CI_REVISION_20;
154         } else if (ver == 0x0) {
155                 rev = CI_REVISION_1X;
156         }
157
158         return rev;
159 }
160
161 /**
162  * hw_read_intr_enable: returns interrupt enable register
163  *
164  * @ci: the controller
165  *
166  * This function returns register data
167  */
168 u32 hw_read_intr_enable(struct ci_hdrc *ci)
169 {
170         return hw_read(ci, OP_USBINTR, ~0);
171 }
172
173 /**
174  * hw_read_intr_status: returns interrupt status register
175  *
176  * @ci: the controller
177  *
178  * This function returns register data
179  */
180 u32 hw_read_intr_status(struct ci_hdrc *ci)
181 {
182         return hw_read(ci, OP_USBSTS, ~0);
183 }
184
185 /**
186  * hw_port_test_set: writes port test mode (execute without interruption)
187  * @mode: new value
188  *
189  * This function returns an error code
190  */
191 int hw_port_test_set(struct ci_hdrc *ci, u8 mode)
192 {
193         const u8 TEST_MODE_MAX = 7;
194
195         if (mode > TEST_MODE_MAX)
196                 return -EINVAL;
197
198         hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << __ffs(PORTSC_PTC));
199         return 0;
200 }
201
202 /**
203  * hw_port_test_get: reads port test mode value
204  *
205  * @ci: the controller
206  *
207  * This function returns port test mode value
208  */
209 u8 hw_port_test_get(struct ci_hdrc *ci)
210 {
211         return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> __ffs(PORTSC_PTC);
212 }
213
214 static void hw_wait_phy_stable(void)
215 {
216         /*
217          * The phy needs some delay to output the stable status from low
218          * power mode. And for OTGSC, the status inputs are debounced
219          * using a 1 ms time constant, so, delay 2ms for controller to get
220          * the stable status, like vbus and id when the phy leaves low power.
221          */
222         usleep_range(2000, 2500);
223 }
224
225 /* The PHY enters/leaves low power mode */
226 static void ci_hdrc_enter_lpm(struct ci_hdrc *ci, bool enable)
227 {
228         enum ci_hw_regs reg = ci->hw_bank.lpm ? OP_DEVLC : OP_PORTSC;
229         bool lpm = !!(hw_read(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm)));
230
231         if (enable && !lpm)
232                 hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
233                                 PORTSC_PHCD(ci->hw_bank.lpm));
234         else if (!enable && lpm)
235                 hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
236                                 0);
237 }
238
239 static int hw_device_init(struct ci_hdrc *ci, void __iomem *base)
240 {
241         u32 reg;
242
243         /* bank is a module variable */
244         ci->hw_bank.abs = base;
245
246         ci->hw_bank.cap = ci->hw_bank.abs;
247         ci->hw_bank.cap += ci->platdata->capoffset;
248         ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff);
249
250         hw_alloc_regmap(ci, false);
251         reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
252                 __ffs(HCCPARAMS_LEN);
253         ci->hw_bank.lpm  = reg;
254         if (reg)
255                 hw_alloc_regmap(ci, !!reg);
256         ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs;
257         ci->hw_bank.size += OP_LAST;
258         ci->hw_bank.size /= sizeof(u32);
259
260         reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
261                 __ffs(DCCPARAMS_DEN);
262         ci->hw_ep_max = reg * 2;   /* cache hw ENDPT_MAX */
263
264         if (ci->hw_ep_max > ENDPT_MAX)
265                 return -ENODEV;
266
267         ci_hdrc_enter_lpm(ci, false);
268
269         /* Disable all interrupts bits */
270         hw_write(ci, OP_USBINTR, 0xffffffff, 0);
271
272         /* Clear all interrupts status bits*/
273         hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff);
274
275         ci->rev = ci_get_revision(ci);
276
277         dev_dbg(ci->dev,
278                 "ChipIdea HDRC found, revision: %d, lpm: %d; cap: %p op: %p\n",
279                 ci->rev, ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
280
281         /* setup lock mode ? */
282
283         /* ENDPTSETUPSTAT is '0' by default */
284
285         /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
286
287         return 0;
288 }
289
290 static void hw_phymode_configure(struct ci_hdrc *ci)
291 {
292         u32 portsc, lpm, sts = 0;
293
294         switch (ci->platdata->phy_mode) {
295         case USBPHY_INTERFACE_MODE_UTMI:
296                 portsc = PORTSC_PTS(PTS_UTMI);
297                 lpm = DEVLC_PTS(PTS_UTMI);
298                 break;
299         case USBPHY_INTERFACE_MODE_UTMIW:
300                 portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW;
301                 lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW;
302                 break;
303         case USBPHY_INTERFACE_MODE_ULPI:
304                 portsc = PORTSC_PTS(PTS_ULPI);
305                 lpm = DEVLC_PTS(PTS_ULPI);
306                 break;
307         case USBPHY_INTERFACE_MODE_SERIAL:
308                 portsc = PORTSC_PTS(PTS_SERIAL);
309                 lpm = DEVLC_PTS(PTS_SERIAL);
310                 sts = 1;
311                 break;
312         case USBPHY_INTERFACE_MODE_HSIC:
313                 portsc = PORTSC_PTS(PTS_HSIC);
314                 lpm = DEVLC_PTS(PTS_HSIC);
315                 break;
316         default:
317                 return;
318         }
319
320         if (ci->hw_bank.lpm) {
321                 hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm);
322                 if (sts)
323                         hw_write(ci, OP_DEVLC, DEVLC_STS, DEVLC_STS);
324         } else {
325                 hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc);
326                 if (sts)
327                         hw_write(ci, OP_PORTSC, PORTSC_STS, PORTSC_STS);
328         }
329 }
330
331 /**
332  * _ci_usb_phy_init: initialize phy taking in account both phy and usb_phy
333  * interfaces
334  * @ci: the controller
335  *
336  * This function returns an error code if the phy failed to init
337  */
338 static int _ci_usb_phy_init(struct ci_hdrc *ci)
339 {
340         int ret;
341
342         if (ci->phy) {
343                 ret = phy_init(ci->phy);
344                 if (ret)
345                         return ret;
346
347                 ret = phy_power_on(ci->phy);
348                 if (ret) {
349                         phy_exit(ci->phy);
350                         return ret;
351                 }
352         } else {
353                 ret = usb_phy_init(ci->usb_phy);
354         }
355
356         return ret;
357 }
358
359 /**
360  * _ci_usb_phy_exit: deinitialize phy taking in account both phy and usb_phy
361  * interfaces
362  * @ci: the controller
363  */
364 static void ci_usb_phy_exit(struct ci_hdrc *ci)
365 {
366         if (ci->phy) {
367                 phy_power_off(ci->phy);
368                 phy_exit(ci->phy);
369         } else {
370                 usb_phy_shutdown(ci->usb_phy);
371         }
372 }
373
374 /**
375  * ci_usb_phy_init: initialize phy according to different phy type
376  * @ci: the controller
377  *
378  * This function returns an error code if usb_phy_init has failed
379  */
380 static int ci_usb_phy_init(struct ci_hdrc *ci)
381 {
382         int ret;
383
384         switch (ci->platdata->phy_mode) {
385         case USBPHY_INTERFACE_MODE_UTMI:
386         case USBPHY_INTERFACE_MODE_UTMIW:
387         case USBPHY_INTERFACE_MODE_HSIC:
388                 ret = _ci_usb_phy_init(ci);
389                 if (!ret)
390                         hw_wait_phy_stable();
391                 else
392                         return ret;
393                 hw_phymode_configure(ci);
394                 break;
395         case USBPHY_INTERFACE_MODE_ULPI:
396         case USBPHY_INTERFACE_MODE_SERIAL:
397                 hw_phymode_configure(ci);
398                 ret = _ci_usb_phy_init(ci);
399                 if (ret)
400                         return ret;
401                 break;
402         default:
403                 ret = _ci_usb_phy_init(ci);
404                 if (!ret)
405                         hw_wait_phy_stable();
406         }
407
408         return ret;
409 }
410
411
412 /**
413  * ci_platform_configure: do controller configure
414  * @ci: the controller
415  *
416  */
417 void ci_platform_configure(struct ci_hdrc *ci)
418 {
419         bool is_device_mode, is_host_mode;
420
421         is_device_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_DC;
422         is_host_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_HC;
423
424         if (is_device_mode &&
425                 (ci->platdata->flags & CI_HDRC_DISABLE_DEVICE_STREAMING))
426                 hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
427
428         if (is_host_mode &&
429                 (ci->platdata->flags & CI_HDRC_DISABLE_HOST_STREAMING))
430                 hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
431
432         if (ci->platdata->flags & CI_HDRC_FORCE_FULLSPEED) {
433                 if (ci->hw_bank.lpm)
434                         hw_write(ci, OP_DEVLC, DEVLC_PFSC, DEVLC_PFSC);
435                 else
436                         hw_write(ci, OP_PORTSC, PORTSC_PFSC, PORTSC_PFSC);
437         }
438
439         if (ci->platdata->flags & CI_HDRC_SET_NON_ZERO_TTHA)
440                 hw_write(ci, OP_TTCTRL, TTCTRL_TTHA_MASK, TTCTRL_TTHA);
441
442         hw_write(ci, OP_USBCMD, 0xff0000, ci->platdata->itc_setting << 16);
443
444         if (ci->platdata->flags & CI_HDRC_OVERRIDE_AHB_BURST)
445                 hw_write_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK,
446                         ci->platdata->ahb_burst_config);
447
448         /* override burst size, take effect only when ahb_burst_config is 0 */
449         if (!hw_read_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK)) {
450                 if (ci->platdata->flags & CI_HDRC_OVERRIDE_TX_BURST)
451                         hw_write(ci, OP_BURSTSIZE, TX_BURST_MASK,
452                         ci->platdata->tx_burst_size << __ffs(TX_BURST_MASK));
453
454                 if (ci->platdata->flags & CI_HDRC_OVERRIDE_RX_BURST)
455                         hw_write(ci, OP_BURSTSIZE, RX_BURST_MASK,
456                                 ci->platdata->rx_burst_size);
457         }
458 }
459
460 /**
461  * hw_controller_reset: do controller reset
462  * @ci: the controller
463   *
464  * This function returns an error code
465  */
466 static int hw_controller_reset(struct ci_hdrc *ci)
467 {
468         int count = 0;
469
470         hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST);
471         while (hw_read(ci, OP_USBCMD, USBCMD_RST)) {
472                 udelay(10);
473                 if (count++ > 1000)
474                         return -ETIMEDOUT;
475         }
476
477         return 0;
478 }
479
480 /**
481  * hw_device_reset: resets chip (execute without interruption)
482  * @ci: the controller
483  *
484  * This function returns an error code
485  */
486 int hw_device_reset(struct ci_hdrc *ci)
487 {
488         int ret;
489
490         /* should flush & stop before reset */
491         hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
492         hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
493
494         ret = hw_controller_reset(ci);
495         if (ret) {
496                 dev_err(ci->dev, "error resetting controller, ret=%d\n", ret);
497                 return ret;
498         }
499
500         if (ci->platdata->notify_event)
501                 ci->platdata->notify_event(ci,
502                         CI_HDRC_CONTROLLER_RESET_EVENT);
503
504         /* USBMODE should be configured step by step */
505         hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
506         hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_DC);
507         /* HW >= 2.3 */
508         hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
509
510         if (hw_read(ci, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DC) {
511                 pr_err("cannot enter in %s device mode", ci_role(ci)->name);
512                 pr_err("lpm = %i", ci->hw_bank.lpm);
513                 return -ENODEV;
514         }
515
516         ci_platform_configure(ci);
517
518         return 0;
519 }
520
521 static irqreturn_t ci_irq(int irq, void *data)
522 {
523         struct ci_hdrc *ci = data;
524         irqreturn_t ret = IRQ_NONE;
525         u32 otgsc = 0;
526
527         if (ci->in_lpm) {
528                 disable_irq_nosync(irq);
529                 ci->wakeup_int = true;
530                 pm_runtime_get(ci->dev);
531                 return IRQ_HANDLED;
532         }
533
534         if (ci->is_otg) {
535                 otgsc = hw_read_otgsc(ci, ~0);
536                 if (ci_otg_is_fsm_mode(ci)) {
537                         ret = ci_otg_fsm_irq(ci);
538                         if (ret == IRQ_HANDLED)
539                                 return ret;
540                 }
541         }
542
543         /*
544          * Handle id change interrupt, it indicates device/host function
545          * switch.
546          */
547         if (ci->is_otg && (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS)) {
548                 ci->id_event = true;
549                 /* Clear ID change irq status */
550                 hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
551                 ci_otg_queue_work(ci);
552                 return IRQ_HANDLED;
553         }
554
555         /*
556          * Handle vbus change interrupt, it indicates device connection
557          * and disconnection events.
558          */
559         if (ci->is_otg && (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
560                 ci->b_sess_valid_event = true;
561                 /* Clear BSV irq */
562                 hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
563                 ci_otg_queue_work(ci);
564                 return IRQ_HANDLED;
565         }
566
567         /* Handle device/host interrupt */
568         if (ci->role != CI_ROLE_END)
569                 ret = ci_role(ci)->irq(ci);
570
571         return ret;
572 }
573
574 static int ci_vbus_notifier(struct notifier_block *nb, unsigned long event,
575                             void *ptr)
576 {
577         struct ci_hdrc_cable *vbus = container_of(nb, struct ci_hdrc_cable, nb);
578         struct ci_hdrc *ci = vbus->ci;
579
580         if (event)
581                 vbus->state = true;
582         else
583                 vbus->state = false;
584
585         vbus->changed = true;
586
587         ci_irq(ci->irq, ci);
588         return NOTIFY_DONE;
589 }
590
591 static int ci_id_notifier(struct notifier_block *nb, unsigned long event,
592                           void *ptr)
593 {
594         struct ci_hdrc_cable *id = container_of(nb, struct ci_hdrc_cable, nb);
595         struct ci_hdrc *ci = id->ci;
596
597         if (event)
598                 id->state = false;
599         else
600                 id->state = true;
601
602         id->changed = true;
603
604         ci_irq(ci->irq, ci);
605         return NOTIFY_DONE;
606 }
607
608 static int ci_get_platdata(struct device *dev,
609                 struct ci_hdrc_platform_data *platdata)
610 {
611         struct extcon_dev *ext_vbus, *ext_id;
612         struct ci_hdrc_cable *cable;
613         int ret;
614
615         if (!platdata->phy_mode)
616                 platdata->phy_mode = of_usb_get_phy_mode(dev->of_node);
617
618         if (!platdata->dr_mode)
619                 platdata->dr_mode = usb_get_dr_mode(dev);
620
621         if (platdata->dr_mode == USB_DR_MODE_UNKNOWN)
622                 platdata->dr_mode = USB_DR_MODE_OTG;
623
624         if (platdata->dr_mode != USB_DR_MODE_PERIPHERAL) {
625                 /* Get the vbus regulator */
626                 platdata->reg_vbus = devm_regulator_get(dev, "vbus");
627                 if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
628                         return -EPROBE_DEFER;
629                 } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
630                         /* no vbus regulator is needed */
631                         platdata->reg_vbus = NULL;
632                 } else if (IS_ERR(platdata->reg_vbus)) {
633                         dev_err(dev, "Getting regulator error: %ld\n",
634                                 PTR_ERR(platdata->reg_vbus));
635                         return PTR_ERR(platdata->reg_vbus);
636                 }
637                 /* Get TPL support */
638                 if (!platdata->tpl_support)
639                         platdata->tpl_support =
640                                 of_usb_host_tpl_support(dev->of_node);
641         }
642
643         if (platdata->dr_mode == USB_DR_MODE_OTG) {
644                 /* We can support HNP and SRP of OTG 2.0 */
645                 platdata->ci_otg_caps.otg_rev = 0x0200;
646                 platdata->ci_otg_caps.hnp_support = true;
647                 platdata->ci_otg_caps.srp_support = true;
648
649                 /* Update otg capabilities by DT properties */
650                 ret = of_usb_update_otg_caps(dev->of_node,
651                                         &platdata->ci_otg_caps);
652                 if (ret)
653                         return ret;
654         }
655
656         if (usb_get_maximum_speed(dev) == USB_SPEED_FULL)
657                 platdata->flags |= CI_HDRC_FORCE_FULLSPEED;
658
659         if (of_find_property(dev->of_node, "phy-clkgate-delay-us", NULL))
660                 of_property_read_u32(dev->of_node, "phy-clkgate-delay-us",
661                                      &platdata->phy_clkgate_delay_us);
662
663         platdata->itc_setting = 1;
664         if (of_find_property(dev->of_node, "itc-setting", NULL)) {
665                 ret = of_property_read_u32(dev->of_node, "itc-setting",
666                         &platdata->itc_setting);
667                 if (ret) {
668                         dev_err(dev,
669                                 "failed to get itc-setting\n");
670                         return ret;
671                 }
672         }
673
674         if (of_find_property(dev->of_node, "ahb-burst-config", NULL)) {
675                 ret = of_property_read_u32(dev->of_node, "ahb-burst-config",
676                         &platdata->ahb_burst_config);
677                 if (ret) {
678                         dev_err(dev,
679                                 "failed to get ahb-burst-config\n");
680                         return ret;
681                 }
682                 platdata->flags |= CI_HDRC_OVERRIDE_AHB_BURST;
683         }
684
685         if (of_find_property(dev->of_node, "tx-burst-size-dword", NULL)) {
686                 ret = of_property_read_u32(dev->of_node, "tx-burst-size-dword",
687                         &platdata->tx_burst_size);
688                 if (ret) {
689                         dev_err(dev,
690                                 "failed to get tx-burst-size-dword\n");
691                         return ret;
692                 }
693                 platdata->flags |= CI_HDRC_OVERRIDE_TX_BURST;
694         }
695
696         if (of_find_property(dev->of_node, "rx-burst-size-dword", NULL)) {
697                 ret = of_property_read_u32(dev->of_node, "rx-burst-size-dword",
698                         &platdata->rx_burst_size);
699                 if (ret) {
700                         dev_err(dev,
701                                 "failed to get rx-burst-size-dword\n");
702                         return ret;
703                 }
704                 platdata->flags |= CI_HDRC_OVERRIDE_RX_BURST;
705         }
706
707         ext_id = ERR_PTR(-ENODEV);
708         ext_vbus = ERR_PTR(-ENODEV);
709         if (of_property_read_bool(dev->of_node, "extcon")) {
710                 /* Each one of them is not mandatory */
711                 ext_vbus = extcon_get_edev_by_phandle(dev, 0);
712                 if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV)
713                         return PTR_ERR(ext_vbus);
714
715                 ext_id = extcon_get_edev_by_phandle(dev, 1);
716                 if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV)
717                         return PTR_ERR(ext_id);
718         }
719
720         cable = &platdata->vbus_extcon;
721         cable->nb.notifier_call = ci_vbus_notifier;
722         cable->edev = ext_vbus;
723
724         if (!IS_ERR(ext_vbus)) {
725                 ret = extcon_get_cable_state_(cable->edev, EXTCON_USB);
726                 if (ret)
727                         cable->state = true;
728                 else
729                         cable->state = false;
730         }
731
732         cable = &platdata->id_extcon;
733         cable->nb.notifier_call = ci_id_notifier;
734         cable->edev = ext_id;
735
736         if (!IS_ERR(ext_id)) {
737                 ret = extcon_get_cable_state_(cable->edev, EXTCON_USB_HOST);
738                 if (ret)
739                         cable->state = false;
740                 else
741                         cable->state = true;
742         }
743         return 0;
744 }
745
746 static int ci_extcon_register(struct ci_hdrc *ci)
747 {
748         struct ci_hdrc_cable *id, *vbus;
749         int ret;
750
751         id = &ci->platdata->id_extcon;
752         id->ci = ci;
753         if (!IS_ERR(id->edev)) {
754                 ret = extcon_register_notifier(id->edev, EXTCON_USB_HOST,
755                                                &id->nb);
756                 if (ret < 0) {
757                         dev_err(ci->dev, "register ID failed\n");
758                         return ret;
759                 }
760         }
761
762         vbus = &ci->platdata->vbus_extcon;
763         vbus->ci = ci;
764         if (!IS_ERR(vbus->edev)) {
765                 ret = extcon_register_notifier(vbus->edev, EXTCON_USB,
766                                                &vbus->nb);
767                 if (ret < 0) {
768                         extcon_unregister_notifier(id->edev, EXTCON_USB_HOST,
769                                                    &id->nb);
770                         dev_err(ci->dev, "register VBUS failed\n");
771                         return ret;
772                 }
773         }
774
775         return 0;
776 }
777
778 static void ci_extcon_unregister(struct ci_hdrc *ci)
779 {
780         struct ci_hdrc_cable *cable;
781
782         cable = &ci->platdata->id_extcon;
783         if (!IS_ERR(cable->edev))
784                 extcon_unregister_notifier(cable->edev, EXTCON_USB_HOST,
785                                            &cable->nb);
786
787         cable = &ci->platdata->vbus_extcon;
788         if (!IS_ERR(cable->edev))
789                 extcon_unregister_notifier(cable->edev, EXTCON_USB, &cable->nb);
790 }
791
792 static DEFINE_IDA(ci_ida);
793
794 struct platform_device *ci_hdrc_add_device(struct device *dev,
795                         struct resource *res, int nres,
796                         struct ci_hdrc_platform_data *platdata)
797 {
798         struct platform_device *pdev;
799         int id, ret;
800
801         ret = ci_get_platdata(dev, platdata);
802         if (ret)
803                 return ERR_PTR(ret);
804
805         id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL);
806         if (id < 0)
807                 return ERR_PTR(id);
808
809         pdev = platform_device_alloc("ci_hdrc", id);
810         if (!pdev) {
811                 ret = -ENOMEM;
812                 goto put_id;
813         }
814
815         pdev->dev.parent = dev;
816         pdev->dev.dma_mask = dev->dma_mask;
817         pdev->dev.dma_parms = dev->dma_parms;
818         dma_set_coherent_mask(&pdev->dev, dev->coherent_dma_mask);
819
820         ret = platform_device_add_resources(pdev, res, nres);
821         if (ret)
822                 goto err;
823
824         ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
825         if (ret)
826                 goto err;
827
828         ret = platform_device_add(pdev);
829         if (ret)
830                 goto err;
831
832         return pdev;
833
834 err:
835         platform_device_put(pdev);
836 put_id:
837         ida_simple_remove(&ci_ida, id);
838         return ERR_PTR(ret);
839 }
840 EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
841
842 void ci_hdrc_remove_device(struct platform_device *pdev)
843 {
844         int id = pdev->id;
845         platform_device_unregister(pdev);
846         ida_simple_remove(&ci_ida, id);
847 }
848 EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
849
850 static inline void ci_role_destroy(struct ci_hdrc *ci)
851 {
852         ci_hdrc_gadget_destroy(ci);
853         ci_hdrc_host_destroy(ci);
854         if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
855                 ci_hdrc_otg_destroy(ci);
856 }
857
858 static void ci_get_otg_capable(struct ci_hdrc *ci)
859 {
860         if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG)
861                 ci->is_otg = false;
862         else
863                 ci->is_otg = (hw_read(ci, CAP_DCCPARAMS,
864                                 DCCPARAMS_DC | DCCPARAMS_HC)
865                                         == (DCCPARAMS_DC | DCCPARAMS_HC));
866         if (ci->is_otg) {
867                 dev_dbg(ci->dev, "It is OTG capable controller\n");
868                 /* Disable and clear all OTG irq */
869                 hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
870                                                         OTGSC_INT_STATUS_BITS);
871         }
872 }
873
874 static int ci_hdrc_probe(struct platform_device *pdev)
875 {
876         struct device   *dev = &pdev->dev;
877         struct ci_hdrc  *ci;
878         struct resource *res;
879         void __iomem    *base;
880         int             ret;
881         enum usb_dr_mode dr_mode;
882
883         if (!dev_get_platdata(dev)) {
884                 dev_err(dev, "platform data missing\n");
885                 return -ENODEV;
886         }
887
888         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
889         base = devm_ioremap_resource(dev, res);
890         if (IS_ERR(base))
891                 return PTR_ERR(base);
892
893         ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
894         if (!ci)
895                 return -ENOMEM;
896
897         spin_lock_init(&ci->lock);
898         ci->dev = dev;
899         ci->platdata = dev_get_platdata(dev);
900         ci->imx28_write_fix = !!(ci->platdata->flags &
901                 CI_HDRC_IMX28_WRITE_FIX);
902         ci->supports_runtime_pm = !!(ci->platdata->flags &
903                 CI_HDRC_SUPPORTS_RUNTIME_PM);
904
905         ret = hw_device_init(ci, base);
906         if (ret < 0) {
907                 dev_err(dev, "can't initialize hardware\n");
908                 return -ENODEV;
909         }
910
911         if (ci->platdata->phy) {
912                 ci->phy = ci->platdata->phy;
913         } else if (ci->platdata->usb_phy) {
914                 ci->usb_phy = ci->platdata->usb_phy;
915         } else {
916                 ci->phy = devm_phy_get(dev->parent, "usb-phy");
917                 ci->usb_phy = devm_usb_get_phy(dev->parent, USB_PHY_TYPE_USB2);
918
919                 /* if both generic PHY and USB PHY layers aren't enabled */
920                 if (PTR_ERR(ci->phy) == -ENOSYS &&
921                                 PTR_ERR(ci->usb_phy) == -ENXIO)
922                         return -ENXIO;
923
924                 if (IS_ERR(ci->phy) && IS_ERR(ci->usb_phy))
925                         return -EPROBE_DEFER;
926
927                 if (IS_ERR(ci->phy))
928                         ci->phy = NULL;
929                 else if (IS_ERR(ci->usb_phy))
930                         ci->usb_phy = NULL;
931         }
932
933         ret = ci_usb_phy_init(ci);
934         if (ret) {
935                 dev_err(dev, "unable to init phy: %d\n", ret);
936                 return ret;
937         }
938
939         ci->hw_bank.phys = res->start;
940
941         ci->irq = platform_get_irq(pdev, 0);
942         if (ci->irq < 0) {
943                 dev_err(dev, "missing IRQ\n");
944                 ret = ci->irq;
945                 goto deinit_phy;
946         }
947
948         ci_get_otg_capable(ci);
949
950         dr_mode = ci->platdata->dr_mode;
951         /* initialize role(s) before the interrupt is requested */
952         if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
953                 ret = ci_hdrc_host_init(ci);
954                 if (ret) {
955                         if (ret == -ENXIO)
956                                 dev_info(dev, "doesn't support host\n");
957                         else
958                                 goto deinit_phy;
959                 }
960         }
961
962         if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
963                 ret = ci_hdrc_gadget_init(ci);
964                 if (ret) {
965                         if (ret == -ENXIO)
966                                 dev_info(dev, "doesn't support gadget\n");
967                         else
968                                 goto deinit_host;
969                 }
970         }
971
972         if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
973                 dev_err(dev, "no supported roles\n");
974                 ret = -ENODEV;
975                 goto deinit_gadget;
976         }
977
978         if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) {
979                 ret = ci_hdrc_otg_init(ci);
980                 if (ret) {
981                         dev_err(dev, "init otg fails, ret = %d\n", ret);
982                         goto deinit_gadget;
983                 }
984         }
985
986         if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
987                 if (ci->is_otg) {
988                         ci->role = ci_otg_role(ci);
989                         /* Enable ID change irq */
990                         hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
991                 } else {
992                         /*
993                          * If the controller is not OTG capable, but support
994                          * role switch, the defalt role is gadget, and the
995                          * user can switch it through debugfs.
996                          */
997                         ci->role = CI_ROLE_GADGET;
998                 }
999         } else {
1000                 ci->role = ci->roles[CI_ROLE_HOST]
1001                         ? CI_ROLE_HOST
1002                         : CI_ROLE_GADGET;
1003         }
1004
1005         if (!ci_otg_is_fsm_mode(ci)) {
1006                 /* only update vbus status for peripheral */
1007                 if (ci->role == CI_ROLE_GADGET)
1008                         ci_handle_vbus_change(ci);
1009
1010                 ret = ci_role_start(ci, ci->role);
1011                 if (ret) {
1012                         dev_err(dev, "can't start %s role\n",
1013                                                 ci_role(ci)->name);
1014                         goto stop;
1015                 }
1016         }
1017
1018         platform_set_drvdata(pdev, ci);
1019         ret = devm_request_irq(dev, ci->irq, ci_irq, IRQF_SHARED,
1020                         ci->platdata->name, ci);
1021         if (ret)
1022                 goto stop;
1023
1024         ret = ci_extcon_register(ci);
1025         if (ret)
1026                 goto stop;
1027
1028         if (ci->supports_runtime_pm) {
1029                 pm_runtime_set_active(&pdev->dev);
1030                 pm_runtime_enable(&pdev->dev);
1031                 pm_runtime_set_autosuspend_delay(&pdev->dev, 2000);
1032                 pm_runtime_mark_last_busy(ci->dev);
1033                 pm_runtime_use_autosuspend(&pdev->dev);
1034         }
1035
1036         if (ci_otg_is_fsm_mode(ci))
1037                 ci_hdrc_otg_fsm_start(ci);
1038
1039         device_set_wakeup_capable(&pdev->dev, true);
1040
1041         ret = dbg_create_files(ci);
1042         if (!ret)
1043                 return 0;
1044
1045         ci_extcon_unregister(ci);
1046 stop:
1047         if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
1048                 ci_hdrc_otg_destroy(ci);
1049 deinit_gadget:
1050         ci_hdrc_gadget_destroy(ci);
1051 deinit_host:
1052         ci_hdrc_host_destroy(ci);
1053 deinit_phy:
1054         ci_usb_phy_exit(ci);
1055
1056         return ret;
1057 }
1058
1059 static int ci_hdrc_remove(struct platform_device *pdev)
1060 {
1061         struct ci_hdrc *ci = platform_get_drvdata(pdev);
1062
1063         if (ci->supports_runtime_pm) {
1064                 pm_runtime_get_sync(&pdev->dev);
1065                 pm_runtime_disable(&pdev->dev);
1066                 pm_runtime_put_noidle(&pdev->dev);
1067         }
1068
1069         dbg_remove_files(ci);
1070         ci_extcon_unregister(ci);
1071         ci_role_destroy(ci);
1072         ci_hdrc_enter_lpm(ci, true);
1073         ci_usb_phy_exit(ci);
1074
1075         return 0;
1076 }
1077
1078 #ifdef CONFIG_PM
1079 /* Prepare wakeup by SRP before suspend */
1080 static void ci_otg_fsm_suspend_for_srp(struct ci_hdrc *ci)
1081 {
1082         if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
1083                                 !hw_read_otgsc(ci, OTGSC_ID)) {
1084                 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
1085                                                                 PORTSC_PP);
1086                 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_WKCN,
1087                                                                 PORTSC_WKCN);
1088         }
1089 }
1090
1091 /* Handle SRP when wakeup by data pulse */
1092 static void ci_otg_fsm_wakeup_by_srp(struct ci_hdrc *ci)
1093 {
1094         if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
1095                 (ci->fsm.a_bus_drop == 1) && (ci->fsm.a_bus_req == 0)) {
1096                 if (!hw_read_otgsc(ci, OTGSC_ID)) {
1097                         ci->fsm.a_srp_det = 1;
1098                         ci->fsm.a_bus_drop = 0;
1099                 } else {
1100                         ci->fsm.id = 1;
1101                 }
1102                 ci_otg_queue_work(ci);
1103         }
1104 }
1105
1106 static void ci_controller_suspend(struct ci_hdrc *ci)
1107 {
1108         disable_irq(ci->irq);
1109         ci_hdrc_enter_lpm(ci, true);
1110         if (ci->platdata->phy_clkgate_delay_us)
1111                 usleep_range(ci->platdata->phy_clkgate_delay_us,
1112                              ci->platdata->phy_clkgate_delay_us + 50);
1113         usb_phy_set_suspend(ci->usb_phy, 1);
1114         ci->in_lpm = true;
1115         enable_irq(ci->irq);
1116 }
1117
1118 static int ci_controller_resume(struct device *dev)
1119 {
1120         struct ci_hdrc *ci = dev_get_drvdata(dev);
1121
1122         dev_dbg(dev, "at %s\n", __func__);
1123
1124         if (!ci->in_lpm) {
1125                 WARN_ON(1);
1126                 return 0;
1127         }
1128
1129         ci_hdrc_enter_lpm(ci, false);
1130         if (ci->usb_phy) {
1131                 usb_phy_set_suspend(ci->usb_phy, 0);
1132                 usb_phy_set_wakeup(ci->usb_phy, false);
1133                 hw_wait_phy_stable();
1134         }
1135
1136         ci->in_lpm = false;
1137         if (ci->wakeup_int) {
1138                 ci->wakeup_int = false;
1139                 pm_runtime_mark_last_busy(ci->dev);
1140                 pm_runtime_put_autosuspend(ci->dev);
1141                 enable_irq(ci->irq);
1142                 if (ci_otg_is_fsm_mode(ci))
1143                         ci_otg_fsm_wakeup_by_srp(ci);
1144         }
1145
1146         return 0;
1147 }
1148
1149 #ifdef CONFIG_PM_SLEEP
1150 static int ci_suspend(struct device *dev)
1151 {
1152         struct ci_hdrc *ci = dev_get_drvdata(dev);
1153
1154         if (ci->wq)
1155                 flush_workqueue(ci->wq);
1156         /*
1157          * Controller needs to be active during suspend, otherwise the core
1158          * may run resume when the parent is at suspend if other driver's
1159          * suspend fails, it occurs before parent's suspend has not started,
1160          * but the core suspend has finished.
1161          */
1162         if (ci->in_lpm)
1163                 pm_runtime_resume(dev);
1164
1165         if (ci->in_lpm) {
1166                 WARN_ON(1);
1167                 return 0;
1168         }
1169
1170         if (device_may_wakeup(dev)) {
1171                 if (ci_otg_is_fsm_mode(ci))
1172                         ci_otg_fsm_suspend_for_srp(ci);
1173
1174                 usb_phy_set_wakeup(ci->usb_phy, true);
1175                 enable_irq_wake(ci->irq);
1176         }
1177
1178         ci_controller_suspend(ci);
1179
1180         return 0;
1181 }
1182
1183 static int ci_resume(struct device *dev)
1184 {
1185         struct ci_hdrc *ci = dev_get_drvdata(dev);
1186         int ret;
1187
1188         if (device_may_wakeup(dev))
1189                 disable_irq_wake(ci->irq);
1190
1191         ret = ci_controller_resume(dev);
1192         if (ret)
1193                 return ret;
1194
1195         if (ci->supports_runtime_pm) {
1196                 pm_runtime_disable(dev);
1197                 pm_runtime_set_active(dev);
1198                 pm_runtime_enable(dev);
1199         }
1200
1201         return ret;
1202 }
1203 #endif /* CONFIG_PM_SLEEP */
1204
1205 static int ci_runtime_suspend(struct device *dev)
1206 {
1207         struct ci_hdrc *ci = dev_get_drvdata(dev);
1208
1209         dev_dbg(dev, "at %s\n", __func__);
1210
1211         if (ci->in_lpm) {
1212                 WARN_ON(1);
1213                 return 0;
1214         }
1215
1216         if (ci_otg_is_fsm_mode(ci))
1217                 ci_otg_fsm_suspend_for_srp(ci);
1218
1219         usb_phy_set_wakeup(ci->usb_phy, true);
1220         ci_controller_suspend(ci);
1221
1222         return 0;
1223 }
1224
1225 static int ci_runtime_resume(struct device *dev)
1226 {
1227         return ci_controller_resume(dev);
1228 }
1229
1230 #endif /* CONFIG_PM */
1231 static const struct dev_pm_ops ci_pm_ops = {
1232         SET_SYSTEM_SLEEP_PM_OPS(ci_suspend, ci_resume)
1233         SET_RUNTIME_PM_OPS(ci_runtime_suspend, ci_runtime_resume, NULL)
1234 };
1235
1236 static struct platform_driver ci_hdrc_driver = {
1237         .probe  = ci_hdrc_probe,
1238         .remove = ci_hdrc_remove,
1239         .driver = {
1240                 .name   = "ci_hdrc",
1241                 .pm     = &ci_pm_ops,
1242         },
1243 };
1244
1245 static int __init ci_hdrc_platform_register(void)
1246 {
1247         ci_hdrc_host_driver_init();
1248         return platform_driver_register(&ci_hdrc_driver);
1249 }
1250 module_init(ci_hdrc_platform_register);
1251
1252 static void __exit ci_hdrc_platform_unregister(void)
1253 {
1254         platform_driver_unregister(&ci_hdrc_driver);
1255 }
1256 module_exit(ci_hdrc_platform_unregister);
1257
1258 MODULE_ALIAS("platform:ci_hdrc");
1259 MODULE_LICENSE("GPL v2");
1260 MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
1261 MODULE_DESCRIPTION("ChipIdea HDRC Driver");