2 * Intel Langwell USB OTG transceiver driver
3 * Copyright (C) 2008 - 2010, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 /* This driver helps to switch Langwell OTG controller function between host
20 * and peripheral. It works with EHCI driver and Langwell client controller
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/pci.h>
26 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/kernel.h>
29 #include <linux/device.h>
30 #include <linux/moduleparam.h>
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/gadget.h>
33 #include <linux/usb.h>
34 #include <linux/usb/otg.h>
35 #include <linux/usb/hcd.h>
36 #include <linux/notifier.h>
37 #include <linux/delay.h>
38 #include <asm/intel_scu_ipc.h>
40 #include <linux/usb/langwell_otg.h>
42 #define DRIVER_DESC "Intel Langwell USB OTG transceiver driver"
43 #define DRIVER_VERSION "July 10, 2010"
45 MODULE_DESCRIPTION(DRIVER_DESC
);
46 MODULE_AUTHOR("Henry Yuan <hang.yuan@intel.com>, Hao Wu <hao.wu@intel.com>");
47 MODULE_VERSION(DRIVER_VERSION
);
48 MODULE_LICENSE("GPL");
50 static const char driver_name
[] = "langwell_otg";
52 static int langwell_otg_probe(struct pci_dev
*pdev
,
53 const struct pci_device_id
*id
);
54 static void langwell_otg_remove(struct pci_dev
*pdev
);
55 static int langwell_otg_suspend(struct pci_dev
*pdev
, pm_message_t message
);
56 static int langwell_otg_resume(struct pci_dev
*pdev
);
58 static int langwell_otg_set_host(struct otg_transceiver
*otg
,
59 struct usb_bus
*host
);
60 static int langwell_otg_set_peripheral(struct otg_transceiver
*otg
,
61 struct usb_gadget
*gadget
);
62 static int langwell_otg_start_srp(struct otg_transceiver
*otg
);
64 static const struct pci_device_id pci_ids
[] = {{
65 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
69 .subvendor
= PCI_ANY_ID
,
70 .subdevice
= PCI_ANY_ID
,
71 }, { /* end: all zeroes */ }
74 static struct pci_driver otg_pci_driver
= {
75 .name
= (char *) driver_name
,
78 .probe
= langwell_otg_probe
,
79 .remove
= langwell_otg_remove
,
81 .suspend
= langwell_otg_suspend
,
82 .resume
= langwell_otg_resume
,
85 static const char *state_string(enum usb_otg_state state
)
88 case OTG_STATE_A_IDLE
:
90 case OTG_STATE_A_WAIT_VRISE
:
91 return "a_wait_vrise";
92 case OTG_STATE_A_WAIT_BCON
:
94 case OTG_STATE_A_HOST
:
96 case OTG_STATE_A_SUSPEND
:
98 case OTG_STATE_A_PERIPHERAL
:
99 return "a_peripheral";
100 case OTG_STATE_A_WAIT_VFALL
:
101 return "a_wait_vfall";
102 case OTG_STATE_A_VBUS_ERR
:
104 case OTG_STATE_B_IDLE
:
106 case OTG_STATE_B_SRP_INIT
:
108 case OTG_STATE_B_PERIPHERAL
:
109 return "b_peripheral";
110 case OTG_STATE_B_WAIT_ACON
:
111 return "b_wait_acon";
112 case OTG_STATE_B_HOST
:
120 static inline struct langwell_otg_timer
*otg_timer_initializer
121 (void (*function
)(unsigned long), unsigned long expires
, unsigned long data
)
123 struct langwell_otg_timer
*timer
;
124 timer
= kmalloc(sizeof(struct langwell_otg_timer
), GFP_KERNEL
);
128 timer
->function
= function
;
129 timer
->expires
= expires
;
134 static struct langwell_otg_timer
*a_wait_vrise_tmr
, *a_aidl_bdis_tmr
,
135 *b_se0_srp_tmr
, *b_srp_init_tmr
;
137 static struct list_head active_timers
;
139 static struct langwell_otg
*the_transceiver
;
141 /* host/client notify transceiver when event affects HNP state */
142 void langwell_update_transceiver(void)
144 struct langwell_otg
*lnw
= the_transceiver
;
146 dev_dbg(lnw
->dev
, "transceiver is updated\n");
151 queue_work(lnw
->qwork
, &lnw
->work
);
153 EXPORT_SYMBOL(langwell_update_transceiver
);
155 static int langwell_otg_set_host(struct otg_transceiver
*otg
,
156 struct usb_bus
*host
)
163 static int langwell_otg_set_peripheral(struct otg_transceiver
*otg
,
164 struct usb_gadget
*gadget
)
166 otg
->gadget
= gadget
;
171 static int langwell_otg_set_power(struct otg_transceiver
*otg
,
177 /* A-device drives vbus, controlled through IPC commands */
178 static int langwell_otg_set_vbus(struct otg_transceiver
*otg
, bool enabled
)
180 struct langwell_otg
*lnw
= the_transceiver
;
183 dev_dbg(lnw
->dev
, "%s <--- %s\n", __func__
, enabled
? "on" : "off");
186 sub_id
= 0x8; /* Turn on the VBus */
188 sub_id
= 0x9; /* Turn off the VBus */
190 if (intel_scu_ipc_simple_command(0xef, sub_id
)) {
191 dev_dbg(lnw
->dev
, "Failed to set Vbus via IPC commands\n");
195 dev_dbg(lnw
->dev
, "%s --->\n", __func__
);
200 /* charge vbus or discharge vbus through a resistor to ground */
201 static void langwell_otg_chrg_vbus(int on
)
203 struct langwell_otg
*lnw
= the_transceiver
;
206 val
= readl(lnw
->iotg
.base
+ CI_OTGSC
);
209 writel((val
& ~OTGSC_INTSTS_MASK
) | OTGSC_VC
,
210 lnw
->iotg
.base
+ CI_OTGSC
);
212 writel((val
& ~OTGSC_INTSTS_MASK
) | OTGSC_VD
,
213 lnw
->iotg
.base
+ CI_OTGSC
);
217 static int langwell_otg_start_srp(struct otg_transceiver
*otg
)
219 struct langwell_otg
*lnw
= the_transceiver
;
220 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
223 dev_dbg(lnw
->dev
, "%s --->\n", __func__
);
225 val
= readl(iotg
->base
+ CI_OTGSC
);
227 writel((val
& ~OTGSC_INTSTS_MASK
) | OTGSC_HADP
,
228 iotg
->base
+ CI_OTGSC
);
230 /* Check if the data plus is finished or not */
232 val
= readl(iotg
->base
+ CI_OTGSC
);
233 if (val
& (OTGSC_HADP
| OTGSC_DP
))
234 dev_dbg(lnw
->dev
, "DataLine SRP Error\n");
236 /* Disable interrupt - b_sess_vld */
237 val
= readl(iotg
->base
+ CI_OTGSC
);
238 val
&= (~(OTGSC_BSVIE
| OTGSC_BSEIE
));
239 writel(val
, iotg
->base
+ CI_OTGSC
);
241 /* Start VBus SRP, drive vbus to generate VBus pulse */
242 iotg
->otg
.set_vbus(&iotg
->otg
, true);
244 iotg
->otg
.set_vbus(&iotg
->otg
, false);
246 /* Enable interrupt - b_sess_vld*/
247 val
= readl(iotg
->base
+ CI_OTGSC
);
248 dev_dbg(lnw
->dev
, "after VBUS pulse otgsc = %x\n", val
);
250 val
|= (OTGSC_BSVIE
| OTGSC_BSEIE
);
251 writel(val
, iotg
->base
+ CI_OTGSC
);
253 /* If Vbus is valid, then update the hsm */
254 if (val
& OTGSC_BSV
) {
255 dev_dbg(lnw
->dev
, "no b_sess_vld interrupt\n");
257 lnw
->iotg
.hsm
.b_sess_vld
= 1;
258 langwell_update_transceiver();
261 dev_dbg(lnw
->dev
, "%s <---\n", __func__
);
265 /* stop SOF via bus_suspend */
266 static void langwell_otg_loc_sof(int on
)
268 struct langwell_otg
*lnw
= the_transceiver
;
272 dev_dbg(lnw
->dev
, "%s ---> %s\n", __func__
, on
? "suspend" : "resume");
274 hcd
= bus_to_hcd(lnw
->iotg
.otg
.host
);
276 err
= hcd
->driver
->bus_resume(hcd
);
278 err
= hcd
->driver
->bus_suspend(hcd
);
281 dev_dbg(lnw
->dev
, "Fail to resume/suspend USB bus - %d\n", err
);
283 dev_dbg(lnw
->dev
, "%s <---\n", __func__
);
286 static int langwell_otg_check_otgsc(void)
288 struct langwell_otg
*lnw
= the_transceiver
;
291 dev_dbg(lnw
->dev
, "check sync OTGSC and USBCFG registers\n");
293 otgsc
= readl(lnw
->iotg
.base
+ CI_OTGSC
);
294 usbcfg
= readl(lnw
->usbcfg
);
296 dev_dbg(lnw
->dev
, "OTGSC = %08x, USBCFG = %08x\n",
298 dev_dbg(lnw
->dev
, "OTGSC_AVV = %d\n", !!(otgsc
& OTGSC_AVV
));
299 dev_dbg(lnw
->dev
, "USBCFG.VBUSVAL = %d\n",
300 !!(usbcfg
& USBCFG_VBUSVAL
));
301 dev_dbg(lnw
->dev
, "OTGSC_ASV = %d\n", !!(otgsc
& OTGSC_ASV
));
302 dev_dbg(lnw
->dev
, "USBCFG.AVALID = %d\n",
303 !!(usbcfg
& USBCFG_AVALID
));
304 dev_dbg(lnw
->dev
, "OTGSC_BSV = %d\n", !!(otgsc
& OTGSC_BSV
));
305 dev_dbg(lnw
->dev
, "USBCFG.BVALID = %d\n",
306 !!(usbcfg
& USBCFG_BVALID
));
307 dev_dbg(lnw
->dev
, "OTGSC_BSE = %d\n", !!(otgsc
& OTGSC_BSE
));
308 dev_dbg(lnw
->dev
, "USBCFG.SESEND = %d\n",
309 !!(usbcfg
& USBCFG_SESEND
));
311 /* Check USBCFG VBusValid/AValid/BValid/SessEnd */
312 if (!!(otgsc
& OTGSC_AVV
) ^ !!(usbcfg
& USBCFG_VBUSVAL
)) {
313 dev_dbg(lnw
->dev
, "OTGSC.AVV != USBCFG.VBUSVAL\n");
316 if (!!(otgsc
& OTGSC_ASV
) ^ !!(usbcfg
& USBCFG_AVALID
)) {
317 dev_dbg(lnw
->dev
, "OTGSC.ASV != USBCFG.AVALID\n");
320 if (!!(otgsc
& OTGSC_BSV
) ^ !!(usbcfg
& USBCFG_BVALID
)) {
321 dev_dbg(lnw
->dev
, "OTGSC.BSV != USBCFG.BVALID\n");
324 if (!!(otgsc
& OTGSC_BSE
) ^ !!(usbcfg
& USBCFG_SESEND
)) {
325 dev_dbg(lnw
->dev
, "OTGSC.BSE != USBCFG.SESSEN\n");
329 dev_dbg(lnw
->dev
, "OTGSC and USBCFG are synced\n");
334 dev_warn(lnw
->dev
, "OTGSC isn't equal to USBCFG\n");
339 static void langwell_otg_phy_low_power(int on
)
341 struct langwell_otg
*lnw
= the_transceiver
;
342 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
346 dev_dbg(lnw
->dev
, "%s ---> %s mode\n",
347 __func__
, on
? "Low power" : "Normal");
351 val
= readb(iotg
->base
+ CI_HOSTPC1
+ 2);
354 /* Due to hardware issue, after set PHCD, sync will failed
355 * between USBCFG and OTGSC, so before set PHCD, check if
356 * sync is in process now. If the answer is "yes", then do
357 * not touch PHCD bit */
358 retval
= langwell_otg_check_otgsc();
360 dev_dbg(lnw
->dev
, "Skip PHCD programming..\n");
364 writeb(val
| phcd
, iotg
->base
+ CI_HOSTPC1
+ 2);
366 writeb(val
& ~phcd
, iotg
->base
+ CI_HOSTPC1
+ 2);
368 dev_dbg(lnw
->dev
, "%s <--- done\n", __func__
);
371 /* After drv vbus, add 5 ms delay to set PHCD */
372 static void langwell_otg_phy_low_power_wait(int on
)
374 struct langwell_otg
*lnw
= the_transceiver
;
376 dev_dbg(lnw
->dev
, "add 5ms delay before programing PHCD\n");
379 langwell_otg_phy_low_power(on
);
382 /* Enable/Disable OTG interrupt */
383 static void langwell_otg_intr(int on
)
385 struct langwell_otg
*lnw
= the_transceiver
;
386 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
389 dev_dbg(lnw
->dev
, "%s ---> %s\n", __func__
, on
? "on" : "off");
391 val
= readl(iotg
->base
+ CI_OTGSC
);
393 /* OTGSC_INT_MASK doesn't contains 1msInt */
395 val
= val
| (OTGSC_INT_MASK
);
396 writel(val
, iotg
->base
+ CI_OTGSC
);
398 val
= val
& ~(OTGSC_INT_MASK
);
399 writel(val
, iotg
->base
+ CI_OTGSC
);
402 dev_dbg(lnw
->dev
, "%s <---\n", __func__
);
405 /* set HAAR: Hardware Assist Auto-Reset */
406 static void langwell_otg_HAAR(int on
)
408 struct langwell_otg
*lnw
= the_transceiver
;
409 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
412 dev_dbg(lnw
->dev
, "%s ---> %s\n", __func__
, on
? "on" : "off");
414 val
= readl(iotg
->base
+ CI_OTGSC
);
416 writel((val
& ~OTGSC_INTSTS_MASK
) | OTGSC_HAAR
,
417 iotg
->base
+ CI_OTGSC
);
419 writel((val
& ~OTGSC_INTSTS_MASK
) & ~OTGSC_HAAR
,
420 iotg
->base
+ CI_OTGSC
);
422 dev_dbg(lnw
->dev
, "%s <---\n", __func__
);
425 /* set HABA: Hardware Assist B-Disconnect to A-Connect */
426 static void langwell_otg_HABA(int on
)
428 struct langwell_otg
*lnw
= the_transceiver
;
429 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
432 dev_dbg(lnw
->dev
, "%s ---> %s\n", __func__
, on
? "on" : "off");
434 val
= readl(iotg
->base
+ CI_OTGSC
);
436 writel((val
& ~OTGSC_INTSTS_MASK
) | OTGSC_HABA
,
437 iotg
->base
+ CI_OTGSC
);
439 writel((val
& ~OTGSC_INTSTS_MASK
) & ~OTGSC_HABA
,
440 iotg
->base
+ CI_OTGSC
);
442 dev_dbg(lnw
->dev
, "%s <---\n", __func__
);
445 static int langwell_otg_check_se0_srp(int on
)
447 struct langwell_otg
*lnw
= the_transceiver
;
448 int delay_time
= TB_SE0_SRP
* 10;
451 dev_dbg(lnw
->dev
, "%s --->\n", __func__
);
457 val
= readl(lnw
->iotg
.base
+ CI_PORTSC1
);
461 dev_dbg(lnw
->dev
, "%s <---\n", __func__
);
465 /* The timeout callback function to set time out bit */
466 static void set_tmout(unsigned long indicator
)
468 *(int *)indicator
= 1;
471 void langwell_otg_nsf_msg(unsigned long indicator
)
473 struct langwell_otg
*lnw
= the_transceiver
;
481 "OTG:NSF-%lu - deivce not responding\n", indicator
);
485 "OTG:NSF-%lu - deivce not supported\n", indicator
);
488 dev_warn(lnw
->dev
, "Do not have this kind of NSF\n");
493 /* Initialize timers */
494 static int langwell_otg_init_timers(struct otg_hsm
*hsm
)
496 /* HSM used timers */
497 a_wait_vrise_tmr
= otg_timer_initializer(&set_tmout
, TA_WAIT_VRISE
,
498 (unsigned long)&hsm
->a_wait_vrise_tmout
);
499 if (a_wait_vrise_tmr
== NULL
)
501 a_aidl_bdis_tmr
= otg_timer_initializer(&set_tmout
, TA_AIDL_BDIS
,
502 (unsigned long)&hsm
->a_aidl_bdis_tmout
);
503 if (a_aidl_bdis_tmr
== NULL
)
505 b_se0_srp_tmr
= otg_timer_initializer(&set_tmout
, TB_SE0_SRP
,
506 (unsigned long)&hsm
->b_se0_srp
);
507 if (b_se0_srp_tmr
== NULL
)
509 b_srp_init_tmr
= otg_timer_initializer(&set_tmout
, TB_SRP_INIT
,
510 (unsigned long)&hsm
->b_srp_init_tmout
);
511 if (b_srp_init_tmr
== NULL
)
518 static void langwell_otg_free_timers(void)
520 kfree(a_wait_vrise_tmr
);
521 kfree(a_aidl_bdis_tmr
);
522 kfree(b_se0_srp_tmr
);
523 kfree(b_srp_init_tmr
);
526 /* The timeout callback function to set time out bit */
527 static void langwell_otg_timer_fn(unsigned long indicator
)
529 struct langwell_otg
*lnw
= the_transceiver
;
531 *(int *)indicator
= 1;
533 dev_dbg(lnw
->dev
, "kernel timer - timeout\n");
535 langwell_update_transceiver();
538 /* kernel timer used instead of HW based interrupt */
539 static void langwell_otg_add_ktimer(enum langwell_otg_timer_type timers
)
541 struct langwell_otg
*lnw
= the_transceiver
;
542 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
543 unsigned long j
= jiffies
;
544 unsigned long data
, time
;
547 case TA_WAIT_VRISE_TMR
:
548 iotg
->hsm
.a_wait_vrise_tmout
= 0;
549 data
= (unsigned long)&iotg
->hsm
.a_wait_vrise_tmout
;
550 time
= TA_WAIT_VRISE
;
552 case TA_WAIT_BCON_TMR
:
553 iotg
->hsm
.a_wait_bcon_tmout
= 0;
554 data
= (unsigned long)&iotg
->hsm
.a_wait_bcon_tmout
;
557 case TA_AIDL_BDIS_TMR
:
558 iotg
->hsm
.a_aidl_bdis_tmout
= 0;
559 data
= (unsigned long)&iotg
->hsm
.a_aidl_bdis_tmout
;
562 case TB_ASE0_BRST_TMR
:
563 iotg
->hsm
.b_ase0_brst_tmout
= 0;
564 data
= (unsigned long)&iotg
->hsm
.b_ase0_brst_tmout
;
567 case TB_SRP_INIT_TMR
:
568 iotg
->hsm
.b_srp_init_tmout
= 0;
569 data
= (unsigned long)&iotg
->hsm
.b_srp_init_tmout
;
572 case TB_SRP_FAIL_TMR
:
573 iotg
->hsm
.b_srp_fail_tmout
= 0;
574 data
= (unsigned long)&iotg
->hsm
.b_srp_fail_tmout
;
577 case TB_BUS_SUSPEND_TMR
:
578 iotg
->hsm
.b_bus_suspend_tmout
= 0;
579 data
= (unsigned long)&iotg
->hsm
.b_bus_suspend_tmout
;
580 time
= TB_BUS_SUSPEND
;
583 dev_dbg(lnw
->dev
, "unknown timer, cannot enable it\n");
587 lnw
->hsm_timer
.data
= data
;
588 lnw
->hsm_timer
.function
= langwell_otg_timer_fn
;
589 lnw
->hsm_timer
.expires
= j
+ time
* HZ
/ 1000; /* milliseconds */
591 add_timer(&lnw
->hsm_timer
);
593 dev_dbg(lnw
->dev
, "add timer successfully\n");
596 /* Add timer to timer list */
597 static void langwell_otg_add_timer(void *gtimer
)
599 struct langwell_otg_timer
*timer
= (struct langwell_otg_timer
*)gtimer
;
600 struct langwell_otg_timer
*tmp_timer
;
601 struct intel_mid_otg_xceiv
*iotg
= &the_transceiver
->iotg
;
604 /* Check if the timer is already in the active list,
605 * if so update timer count
607 list_for_each_entry(tmp_timer
, &active_timers
, list
)
608 if (tmp_timer
== timer
) {
609 timer
->count
= timer
->expires
;
612 timer
->count
= timer
->expires
;
614 if (list_empty(&active_timers
)) {
615 val32
= readl(iotg
->base
+ CI_OTGSC
);
616 writel(val32
| OTGSC_1MSE
, iotg
->base
+ CI_OTGSC
);
619 list_add_tail(&timer
->list
, &active_timers
);
622 /* Remove timer from the timer list; clear timeout status */
623 static void langwell_otg_del_timer(void *gtimer
)
625 struct langwell_otg
*lnw
= the_transceiver
;
626 struct langwell_otg_timer
*timer
= (struct langwell_otg_timer
*)gtimer
;
627 struct langwell_otg_timer
*tmp_timer
, *del_tmp
;
630 list_for_each_entry_safe(tmp_timer
, del_tmp
, &active_timers
, list
)
631 if (tmp_timer
== timer
)
632 list_del(&timer
->list
);
634 if (list_empty(&active_timers
)) {
635 val32
= readl(lnw
->iotg
.base
+ CI_OTGSC
);
636 writel(val32
& ~OTGSC_1MSE
, lnw
->iotg
.base
+ CI_OTGSC
);
640 /* Reduce timer count by 1, and find timeout conditions.*/
641 static int langwell_otg_tick_timer(u32
*int_sts
)
643 struct langwell_otg
*lnw
= the_transceiver
;
644 struct langwell_otg_timer
*tmp_timer
, *del_tmp
;
647 list_for_each_entry_safe(tmp_timer
, del_tmp
, &active_timers
, list
) {
649 /* check if timer expires */
650 if (!tmp_timer
->count
) {
651 list_del(&tmp_timer
->list
);
652 tmp_timer
->function(tmp_timer
->data
);
657 if (list_empty(&active_timers
)) {
658 dev_dbg(lnw
->dev
, "tick timer: disable 1ms int\n");
659 *int_sts
= *int_sts
& ~OTGSC_1MSE
;
664 static void reset_otg(void)
666 struct langwell_otg
*lnw
= the_transceiver
;
667 int delay_time
= 1000;
670 dev_dbg(lnw
->dev
, "reseting OTG controller ...\n");
671 val
= readl(lnw
->iotg
.base
+ CI_USBCMD
);
672 writel(val
| USBCMD_RST
, lnw
->iotg
.base
+ CI_USBCMD
);
676 dev_dbg(lnw
->dev
, "reset timeout\n");
677 val
= readl(lnw
->iotg
.base
+ CI_USBCMD
);
680 dev_dbg(lnw
->dev
, "reset done.\n");
683 static void set_host_mode(void)
685 struct langwell_otg
*lnw
= the_transceiver
;
689 val
= readl(lnw
->iotg
.base
+ CI_USBMODE
);
690 val
= (val
& (~USBMODE_CM
)) | USBMODE_HOST
;
691 writel(val
, lnw
->iotg
.base
+ CI_USBMODE
);
694 static void set_client_mode(void)
696 struct langwell_otg
*lnw
= the_transceiver
;
700 val
= readl(lnw
->iotg
.base
+ CI_USBMODE
);
701 val
= (val
& (~USBMODE_CM
)) | USBMODE_DEVICE
;
702 writel(val
, lnw
->iotg
.base
+ CI_USBMODE
);
705 static void init_hsm(void)
707 struct langwell_otg
*lnw
= the_transceiver
;
708 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
711 /* read OTGSC after reset */
712 val32
= readl(lnw
->iotg
.base
+ CI_OTGSC
);
713 dev_dbg(lnw
->dev
, "%s: OTGSC init value = 0x%x\n", __func__
, val32
);
716 if (val32
& OTGSC_ID
) {
718 iotg
->otg
.default_a
= 0;
720 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
723 iotg
->otg
.default_a
= 1;
725 iotg
->otg
.state
= OTG_STATE_A_IDLE
;
728 /* set session indicator */
729 if (val32
& OTGSC_BSE
)
730 iotg
->hsm
.b_sess_end
= 1;
731 if (val32
& OTGSC_BSV
)
732 iotg
->hsm
.b_sess_vld
= 1;
733 if (val32
& OTGSC_ASV
)
734 iotg
->hsm
.a_sess_vld
= 1;
735 if (val32
& OTGSC_AVV
)
736 iotg
->hsm
.a_vbus_vld
= 1;
738 /* defautly power the bus */
739 iotg
->hsm
.a_bus_req
= 1;
740 iotg
->hsm
.a_bus_drop
= 0;
741 /* defautly don't request bus as B device */
742 iotg
->hsm
.b_bus_req
= 0;
743 /* no system error */
744 iotg
->hsm
.a_clr_err
= 0;
746 langwell_otg_phy_low_power_wait(1);
749 static void update_hsm(void)
751 struct langwell_otg
*lnw
= the_transceiver
;
752 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
756 val32
= readl(lnw
->iotg
.base
+ CI_OTGSC
);
757 dev_dbg(lnw
->dev
, "%s: OTGSC value = 0x%x\n", __func__
, val32
);
759 iotg
->hsm
.id
= !!(val32
& OTGSC_ID
);
760 iotg
->hsm
.b_sess_end
= !!(val32
& OTGSC_BSE
);
761 iotg
->hsm
.b_sess_vld
= !!(val32
& OTGSC_BSV
);
762 iotg
->hsm
.a_sess_vld
= !!(val32
& OTGSC_ASV
);
763 iotg
->hsm
.a_vbus_vld
= !!(val32
& OTGSC_AVV
);
766 static irqreturn_t
otg_dummy_irq(int irq
, void *_dev
)
768 struct langwell_otg
*lnw
= the_transceiver
;
769 void __iomem
*reg_base
= _dev
;
773 val
= readl(reg_base
+ CI_USBMODE
);
774 if ((val
& USBMODE_CM
) != USBMODE_DEVICE
)
777 val
= readl(reg_base
+ CI_USBSTS
);
778 int_mask
= val
& INTR_DUMMY_MASK
;
783 /* clear hsm.b_conn here since host driver can't detect it
784 * otg_dummy_irq called means B-disconnect happened.
786 if (lnw
->iotg
.hsm
.b_conn
) {
787 lnw
->iotg
.hsm
.b_conn
= 0;
788 if (spin_trylock(&lnw
->wq_lock
)) {
789 langwell_update_transceiver();
790 spin_unlock(&lnw
->wq_lock
);
794 /* Clear interrupts */
795 writel(int_mask
, reg_base
+ CI_USBSTS
);
799 static irqreturn_t
otg_irq(int irq
, void *_dev
)
801 struct langwell_otg
*lnw
= _dev
;
802 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
807 int_sts
= readl(lnw
->iotg
.base
+ CI_OTGSC
);
808 int_en
= (int_sts
& OTGSC_INTEN_MASK
) >> 8;
809 int_mask
= int_sts
& int_en
;
813 if (int_mask
& OTGSC_IDIS
) {
814 dev_dbg(lnw
->dev
, "%s: id change int\n", __func__
);
815 iotg
->hsm
.id
= (int_sts
& OTGSC_ID
) ? 1 : 0;
816 dev_dbg(lnw
->dev
, "id = %d\n", iotg
->hsm
.id
);
819 if (int_mask
& OTGSC_DPIS
) {
820 dev_dbg(lnw
->dev
, "%s: data pulse int\n", __func__
);
821 iotg
->hsm
.a_srp_det
= (int_sts
& OTGSC_DPS
) ? 1 : 0;
822 dev_dbg(lnw
->dev
, "data pulse = %d\n", iotg
->hsm
.a_srp_det
);
825 if (int_mask
& OTGSC_BSEIS
) {
826 dev_dbg(lnw
->dev
, "%s: b session end int\n", __func__
);
827 iotg
->hsm
.b_sess_end
= (int_sts
& OTGSC_BSE
) ? 1 : 0;
828 dev_dbg(lnw
->dev
, "b_sess_end = %d\n", iotg
->hsm
.b_sess_end
);
831 if (int_mask
& OTGSC_BSVIS
) {
832 dev_dbg(lnw
->dev
, "%s: b session valid int\n", __func__
);
833 iotg
->hsm
.b_sess_vld
= (int_sts
& OTGSC_BSV
) ? 1 : 0;
834 dev_dbg(lnw
->dev
, "b_sess_vld = %d\n", iotg
->hsm
.b_sess_end
);
837 if (int_mask
& OTGSC_ASVIS
) {
838 dev_dbg(lnw
->dev
, "%s: a session valid int\n", __func__
);
839 iotg
->hsm
.a_sess_vld
= (int_sts
& OTGSC_ASV
) ? 1 : 0;
840 dev_dbg(lnw
->dev
, "a_sess_vld = %d\n", iotg
->hsm
.a_sess_vld
);
843 if (int_mask
& OTGSC_AVVIS
) {
844 dev_dbg(lnw
->dev
, "%s: a vbus valid int\n", __func__
);
845 iotg
->hsm
.a_vbus_vld
= (int_sts
& OTGSC_AVV
) ? 1 : 0;
846 dev_dbg(lnw
->dev
, "a_vbus_vld = %d\n", iotg
->hsm
.a_vbus_vld
);
850 if (int_mask
& OTGSC_1MSS
) {
851 /* need to schedule otg_work if any timer is expired */
852 if (langwell_otg_tick_timer(&int_sts
))
856 writel((int_sts
& ~OTGSC_INTSTS_MASK
) | int_mask
,
857 lnw
->iotg
.base
+ CI_OTGSC
);
859 langwell_update_transceiver();
864 static int langwell_otg_iotg_notify(struct notifier_block
*nb
,
865 unsigned long action
, void *data
)
867 struct langwell_otg
*lnw
= the_transceiver
;
868 struct intel_mid_otg_xceiv
*iotg
= data
;
878 case MID_OTG_NOTIFY_CONNECT
:
879 dev_dbg(lnw
->dev
, "Lnw OTG Notify Connect Event\n");
880 if (iotg
->otg
.default_a
== 1)
881 iotg
->hsm
.b_conn
= 1;
883 iotg
->hsm
.a_conn
= 1;
886 case MID_OTG_NOTIFY_DISCONN
:
887 dev_dbg(lnw
->dev
, "Lnw OTG Notify Disconnect Event\n");
888 if (iotg
->otg
.default_a
== 1)
889 iotg
->hsm
.b_conn
= 0;
891 iotg
->hsm
.a_conn
= 0;
894 case MID_OTG_NOTIFY_HSUSPEND
:
895 dev_dbg(lnw
->dev
, "Lnw OTG Notify Host Bus suspend Event\n");
896 if (iotg
->otg
.default_a
== 1)
897 iotg
->hsm
.a_suspend_req
= 1;
899 iotg
->hsm
.b_bus_req
= 0;
902 case MID_OTG_NOTIFY_HRESUME
:
903 dev_dbg(lnw
->dev
, "Lnw OTG Notify Host Bus resume Event\n");
904 if (iotg
->otg
.default_a
== 1)
905 iotg
->hsm
.b_bus_resume
= 1;
908 case MID_OTG_NOTIFY_CSUSPEND
:
909 dev_dbg(lnw
->dev
, "Lnw OTG Notify Client Bus suspend Event\n");
910 if (iotg
->otg
.default_a
== 1) {
911 if (iotg
->hsm
.b_bus_suspend_vld
== 2) {
912 iotg
->hsm
.b_bus_suspend
= 1;
913 iotg
->hsm
.b_bus_suspend_vld
= 0;
916 iotg
->hsm
.b_bus_suspend_vld
++;
920 if (iotg
->hsm
.a_bus_suspend
== 0) {
921 iotg
->hsm
.a_bus_suspend
= 1;
926 case MID_OTG_NOTIFY_CRESUME
:
927 dev_dbg(lnw
->dev
, "Lnw OTG Notify Client Bus resume Event\n");
928 if (iotg
->otg
.default_a
== 0)
929 iotg
->hsm
.a_bus_suspend
= 0;
932 case MID_OTG_NOTIFY_HOSTADD
:
933 dev_dbg(lnw
->dev
, "Lnw OTG Nofity Host Driver Add\n");
936 case MID_OTG_NOTIFY_HOSTREMOVE
:
937 dev_dbg(lnw
->dev
, "Lnw OTG Nofity Host Driver remove\n");
940 case MID_OTG_NOTIFY_CLIENTADD
:
941 dev_dbg(lnw
->dev
, "Lnw OTG Nofity Client Driver Add\n");
944 case MID_OTG_NOTIFY_CLIENTREMOVE
:
945 dev_dbg(lnw
->dev
, "Lnw OTG Nofity Client Driver remove\n");
949 dev_dbg(lnw
->dev
, "Lnw OTG Nofity unknown notify message\n");
954 langwell_update_transceiver();
959 static void langwell_otg_work(struct work_struct
*work
)
961 struct langwell_otg
*lnw
;
962 struct intel_mid_otg_xceiv
*iotg
;
964 struct pci_dev
*pdev
;
966 lnw
= container_of(work
, struct langwell_otg
, work
);
968 pdev
= to_pci_dev(lnw
->dev
);
970 dev_dbg(lnw
->dev
, "%s: old state = %s\n", __func__
,
971 state_string(iotg
->otg
.state
));
973 switch (iotg
->otg
.state
) {
974 case OTG_STATE_UNDEFINED
:
975 case OTG_STATE_B_IDLE
:
977 langwell_otg_del_timer(b_srp_init_tmr
);
978 del_timer_sync(&lnw
->hsm_timer
);
980 iotg
->otg
.default_a
= 1;
981 iotg
->hsm
.a_srp_det
= 0;
983 langwell_otg_chrg_vbus(0);
985 langwell_otg_phy_low_power(1);
987 iotg
->otg
.state
= OTG_STATE_A_IDLE
;
988 langwell_update_transceiver();
989 } else if (iotg
->hsm
.b_sess_vld
) {
990 langwell_otg_del_timer(b_srp_init_tmr
);
991 del_timer_sync(&lnw
->hsm_timer
);
992 iotg
->hsm
.b_sess_end
= 0;
993 iotg
->hsm
.a_bus_suspend
= 0;
994 langwell_otg_chrg_vbus(0);
996 if (lnw
->iotg
.start_peripheral
) {
997 lnw
->iotg
.start_peripheral(&lnw
->iotg
);
998 iotg
->otg
.state
= OTG_STATE_B_PERIPHERAL
;
1000 dev_dbg(lnw
->dev
, "client driver not loaded\n");
1002 } else if (iotg
->hsm
.b_srp_init_tmout
) {
1003 iotg
->hsm
.b_srp_init_tmout
= 0;
1004 dev_warn(lnw
->dev
, "SRP init timeout\n");
1005 } else if (iotg
->hsm
.b_srp_fail_tmout
) {
1006 iotg
->hsm
.b_srp_fail_tmout
= 0;
1007 iotg
->hsm
.b_bus_req
= 0;
1009 /* No silence failure */
1010 langwell_otg_nsf_msg(6);
1011 } else if (iotg
->hsm
.b_bus_req
&& iotg
->hsm
.b_sess_end
) {
1012 del_timer_sync(&lnw
->hsm_timer
);
1013 /* workaround for b_se0_srp detection */
1014 retval
= langwell_otg_check_se0_srp(0);
1016 iotg
->hsm
.b_bus_req
= 0;
1017 dev_dbg(lnw
->dev
, "LS isn't SE0, try later\n");
1019 /* clear the PHCD before start srp */
1020 langwell_otg_phy_low_power(0);
1023 langwell_otg_add_timer(b_srp_init_tmr
);
1024 iotg
->otg
.start_srp(&iotg
->otg
);
1025 langwell_otg_del_timer(b_srp_init_tmr
);
1026 langwell_otg_add_ktimer(TB_SRP_FAIL_TMR
);
1028 /* reset PHY low power mode here */
1029 langwell_otg_phy_low_power_wait(1);
1033 case OTG_STATE_B_SRP_INIT
:
1034 if (!iotg
->hsm
.id
) {
1035 iotg
->otg
.default_a
= 1;
1036 iotg
->hsm
.a_srp_det
= 0;
1039 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1040 langwell_otg_chrg_vbus(0);
1042 langwell_otg_phy_low_power(1);
1043 iotg
->otg
.state
= OTG_STATE_A_IDLE
;
1044 langwell_update_transceiver();
1045 } else if (iotg
->hsm
.b_sess_vld
) {
1046 langwell_otg_chrg_vbus(0);
1047 if (lnw
->iotg
.start_peripheral
) {
1048 lnw
->iotg
.start_peripheral(&lnw
->iotg
);
1049 iotg
->otg
.state
= OTG_STATE_B_PERIPHERAL
;
1051 dev_dbg(lnw
->dev
, "client driver not loaded\n");
1054 case OTG_STATE_B_PERIPHERAL
:
1055 if (!iotg
->hsm
.id
) {
1056 iotg
->otg
.default_a
= 1;
1057 iotg
->hsm
.a_srp_det
= 0;
1059 langwell_otg_chrg_vbus(0);
1061 if (lnw
->iotg
.stop_peripheral
)
1062 lnw
->iotg
.stop_peripheral(&lnw
->iotg
);
1065 "client driver has been removed.\n");
1068 langwell_otg_phy_low_power(1);
1069 iotg
->otg
.state
= OTG_STATE_A_IDLE
;
1070 langwell_update_transceiver();
1071 } else if (!iotg
->hsm
.b_sess_vld
) {
1072 iotg
->hsm
.b_hnp_enable
= 0;
1074 if (lnw
->iotg
.stop_peripheral
)
1075 lnw
->iotg
.stop_peripheral(&lnw
->iotg
);
1078 "client driver has been removed.\n");
1080 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
1081 } else if (iotg
->hsm
.b_bus_req
&& iotg
->otg
.gadget
&&
1082 iotg
->otg
.gadget
->b_hnp_enable
&&
1083 iotg
->hsm
.a_bus_suspend
) {
1085 if (lnw
->iotg
.stop_peripheral
)
1086 lnw
->iotg
.stop_peripheral(&lnw
->iotg
);
1089 "client driver has been removed.\n");
1091 langwell_otg_HAAR(1);
1092 iotg
->hsm
.a_conn
= 0;
1094 if (lnw
->iotg
.start_host
) {
1095 lnw
->iotg
.start_host(&lnw
->iotg
);
1096 iotg
->otg
.state
= OTG_STATE_B_WAIT_ACON
;
1099 "host driver not loaded.\n");
1101 iotg
->hsm
.a_bus_resume
= 0;
1102 langwell_otg_add_ktimer(TB_ASE0_BRST_TMR
);
1106 case OTG_STATE_B_WAIT_ACON
:
1107 if (!iotg
->hsm
.id
) {
1108 /* delete hsm timer for b_ase0_brst_tmr */
1109 del_timer_sync(&lnw
->hsm_timer
);
1111 iotg
->otg
.default_a
= 1;
1112 iotg
->hsm
.a_srp_det
= 0;
1114 langwell_otg_chrg_vbus(0);
1116 langwell_otg_HAAR(0);
1117 if (lnw
->iotg
.stop_host
)
1118 lnw
->iotg
.stop_host(&lnw
->iotg
);
1121 "host driver has been removed.\n");
1124 langwell_otg_phy_low_power(1);
1125 iotg
->otg
.state
= OTG_STATE_A_IDLE
;
1126 langwell_update_transceiver();
1127 } else if (!iotg
->hsm
.b_sess_vld
) {
1128 /* delete hsm timer for b_ase0_brst_tmr */
1129 del_timer_sync(&lnw
->hsm_timer
);
1131 iotg
->hsm
.b_hnp_enable
= 0;
1132 iotg
->hsm
.b_bus_req
= 0;
1134 langwell_otg_chrg_vbus(0);
1135 langwell_otg_HAAR(0);
1137 if (lnw
->iotg
.stop_host
)
1138 lnw
->iotg
.stop_host(&lnw
->iotg
);
1141 "host driver has been removed.\n");
1144 langwell_otg_phy_low_power(1);
1145 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
1146 } else if (iotg
->hsm
.a_conn
) {
1147 /* delete hsm timer for b_ase0_brst_tmr */
1148 del_timer_sync(&lnw
->hsm_timer
);
1150 langwell_otg_HAAR(0);
1151 iotg
->otg
.state
= OTG_STATE_B_HOST
;
1152 langwell_update_transceiver();
1153 } else if (iotg
->hsm
.a_bus_resume
||
1154 iotg
->hsm
.b_ase0_brst_tmout
) {
1155 /* delete hsm timer for b_ase0_brst_tmr */
1156 del_timer_sync(&lnw
->hsm_timer
);
1158 langwell_otg_HAAR(0);
1159 langwell_otg_nsf_msg(7);
1161 if (lnw
->iotg
.stop_host
)
1162 lnw
->iotg
.stop_host(&lnw
->iotg
);
1165 "host driver has been removed.\n");
1167 iotg
->hsm
.a_bus_suspend
= 0;
1168 iotg
->hsm
.b_bus_req
= 0;
1170 if (lnw
->iotg
.start_peripheral
)
1171 lnw
->iotg
.start_peripheral(&lnw
->iotg
);
1174 "client driver not loaded.\n");
1176 iotg
->otg
.state
= OTG_STATE_B_PERIPHERAL
;
1180 case OTG_STATE_B_HOST
:
1181 if (!iotg
->hsm
.id
) {
1182 iotg
->otg
.default_a
= 1;
1183 iotg
->hsm
.a_srp_det
= 0;
1185 langwell_otg_chrg_vbus(0);
1187 if (lnw
->iotg
.stop_host
)
1188 lnw
->iotg
.stop_host(&lnw
->iotg
);
1191 "host driver has been removed.\n");
1194 langwell_otg_phy_low_power(1);
1195 iotg
->otg
.state
= OTG_STATE_A_IDLE
;
1196 langwell_update_transceiver();
1197 } else if (!iotg
->hsm
.b_sess_vld
) {
1198 iotg
->hsm
.b_hnp_enable
= 0;
1199 iotg
->hsm
.b_bus_req
= 0;
1201 langwell_otg_chrg_vbus(0);
1202 if (lnw
->iotg
.stop_host
)
1203 lnw
->iotg
.stop_host(&lnw
->iotg
);
1206 "host driver has been removed.\n");
1209 langwell_otg_phy_low_power(1);
1210 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
1211 } else if ((!iotg
->hsm
.b_bus_req
) ||
1212 (!iotg
->hsm
.a_conn
)) {
1213 iotg
->hsm
.b_bus_req
= 0;
1214 langwell_otg_loc_sof(0);
1216 if (lnw
->iotg
.stop_host
)
1217 lnw
->iotg
.stop_host(&lnw
->iotg
);
1220 "host driver has been removed.\n");
1222 iotg
->hsm
.a_bus_suspend
= 0;
1224 if (lnw
->iotg
.start_peripheral
)
1225 lnw
->iotg
.start_peripheral(&lnw
->iotg
);
1228 "client driver not loaded.\n");
1230 iotg
->otg
.state
= OTG_STATE_B_PERIPHERAL
;
1234 case OTG_STATE_A_IDLE
:
1235 iotg
->otg
.default_a
= 1;
1237 iotg
->otg
.default_a
= 0;
1238 iotg
->hsm
.b_bus_req
= 0;
1239 iotg
->hsm
.vbus_srp_up
= 0;
1241 langwell_otg_chrg_vbus(0);
1243 langwell_otg_phy_low_power(1);
1244 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
1245 langwell_update_transceiver();
1246 } else if (!iotg
->hsm
.a_bus_drop
&&
1247 (iotg
->hsm
.a_srp_det
|| iotg
->hsm
.a_bus_req
)) {
1248 langwell_otg_phy_low_power(0);
1251 iotg
->otg
.set_vbus(&iotg
->otg
, true);
1253 iotg
->hsm
.vbus_srp_up
= 0;
1254 iotg
->hsm
.a_wait_vrise_tmout
= 0;
1255 langwell_otg_add_timer(a_wait_vrise_tmr
);
1256 iotg
->otg
.state
= OTG_STATE_A_WAIT_VRISE
;
1257 langwell_update_transceiver();
1258 } else if (!iotg
->hsm
.a_bus_drop
&& iotg
->hsm
.a_sess_vld
) {
1259 iotg
->hsm
.vbus_srp_up
= 1;
1260 } else if (!iotg
->hsm
.a_sess_vld
&& iotg
->hsm
.vbus_srp_up
) {
1262 langwell_otg_phy_low_power(0);
1265 iotg
->otg
.set_vbus(&iotg
->otg
, true);
1266 iotg
->hsm
.a_srp_det
= 1;
1267 iotg
->hsm
.vbus_srp_up
= 0;
1268 iotg
->hsm
.a_wait_vrise_tmout
= 0;
1269 langwell_otg_add_timer(a_wait_vrise_tmr
);
1270 iotg
->otg
.state
= OTG_STATE_A_WAIT_VRISE
;
1271 langwell_update_transceiver();
1272 } else if (!iotg
->hsm
.a_sess_vld
&&
1273 !iotg
->hsm
.vbus_srp_up
) {
1274 langwell_otg_phy_low_power(1);
1277 case OTG_STATE_A_WAIT_VRISE
:
1279 langwell_otg_del_timer(a_wait_vrise_tmr
);
1280 iotg
->hsm
.b_bus_req
= 0;
1281 iotg
->otg
.default_a
= 0;
1284 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1286 langwell_otg_phy_low_power_wait(1);
1287 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
1288 } else if (iotg
->hsm
.a_vbus_vld
) {
1289 langwell_otg_del_timer(a_wait_vrise_tmr
);
1290 iotg
->hsm
.b_conn
= 0;
1291 if (lnw
->iotg
.start_host
)
1292 lnw
->iotg
.start_host(&lnw
->iotg
);
1294 dev_dbg(lnw
->dev
, "host driver not loaded.\n");
1298 langwell_otg_add_ktimer(TA_WAIT_BCON_TMR
);
1299 iotg
->otg
.state
= OTG_STATE_A_WAIT_BCON
;
1300 } else if (iotg
->hsm
.a_wait_vrise_tmout
) {
1301 iotg
->hsm
.b_conn
= 0;
1302 if (iotg
->hsm
.a_vbus_vld
) {
1303 if (lnw
->iotg
.start_host
)
1304 lnw
->iotg
.start_host(&lnw
->iotg
);
1307 "host driver not loaded.\n");
1310 langwell_otg_add_ktimer(TA_WAIT_BCON_TMR
);
1311 iotg
->otg
.state
= OTG_STATE_A_WAIT_BCON
;
1315 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1316 langwell_otg_phy_low_power_wait(1);
1317 iotg
->otg
.state
= OTG_STATE_A_VBUS_ERR
;
1321 case OTG_STATE_A_WAIT_BCON
:
1323 /* delete hsm timer for a_wait_bcon_tmr */
1324 del_timer_sync(&lnw
->hsm_timer
);
1326 iotg
->otg
.default_a
= 0;
1327 iotg
->hsm
.b_bus_req
= 0;
1329 if (lnw
->iotg
.stop_host
)
1330 lnw
->iotg
.stop_host(&lnw
->iotg
);
1333 "host driver has been removed.\n");
1336 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1338 langwell_otg_phy_low_power_wait(1);
1339 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
1340 langwell_update_transceiver();
1341 } else if (!iotg
->hsm
.a_vbus_vld
) {
1342 /* delete hsm timer for a_wait_bcon_tmr */
1343 del_timer_sync(&lnw
->hsm_timer
);
1345 if (lnw
->iotg
.stop_host
)
1346 lnw
->iotg
.stop_host(&lnw
->iotg
);
1349 "host driver has been removed.\n");
1352 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1353 langwell_otg_phy_low_power_wait(1);
1354 iotg
->otg
.state
= OTG_STATE_A_VBUS_ERR
;
1355 } else if (iotg
->hsm
.a_bus_drop
||
1356 (iotg
->hsm
.a_wait_bcon_tmout
&&
1357 !iotg
->hsm
.a_bus_req
)) {
1358 /* delete hsm timer for a_wait_bcon_tmr */
1359 del_timer_sync(&lnw
->hsm_timer
);
1361 if (lnw
->iotg
.stop_host
)
1362 lnw
->iotg
.stop_host(&lnw
->iotg
);
1365 "host driver has been removed.\n");
1368 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1369 iotg
->otg
.state
= OTG_STATE_A_WAIT_VFALL
;
1370 } else if (iotg
->hsm
.b_conn
) {
1371 /* delete hsm timer for a_wait_bcon_tmr */
1372 del_timer_sync(&lnw
->hsm_timer
);
1374 iotg
->hsm
.a_suspend_req
= 0;
1375 iotg
->otg
.state
= OTG_STATE_A_HOST
;
1376 if (iotg
->hsm
.a_srp_det
&& iotg
->otg
.host
&&
1377 !iotg
->otg
.host
->b_hnp_enable
) {
1378 /* SRP capable peripheral-only device */
1379 iotg
->hsm
.a_bus_req
= 1;
1380 iotg
->hsm
.a_srp_det
= 0;
1381 } else if (!iotg
->hsm
.a_bus_req
&& iotg
->otg
.host
&&
1382 iotg
->otg
.host
->b_hnp_enable
) {
1383 /* It is not safe enough to do a fast
1384 * transition from A_WAIT_BCON to
1387 if (iotg
->hsm
.a_bus_req
)
1390 if (request_irq(pdev
->irq
,
1391 otg_dummy_irq
, IRQF_SHARED
,
1392 driver_name
, iotg
->base
) != 0) {
1394 "request interrupt %d fail\n",
1398 langwell_otg_HABA(1);
1399 iotg
->hsm
.b_bus_resume
= 0;
1400 iotg
->hsm
.a_aidl_bdis_tmout
= 0;
1402 langwell_otg_loc_sof(0);
1403 /* clear PHCD to enable HW timer */
1404 langwell_otg_phy_low_power(0);
1405 langwell_otg_add_timer(a_aidl_bdis_tmr
);
1406 iotg
->otg
.state
= OTG_STATE_A_SUSPEND
;
1407 } else if (!iotg
->hsm
.a_bus_req
&& iotg
->otg
.host
&&
1408 !iotg
->otg
.host
->b_hnp_enable
) {
1409 if (lnw
->iotg
.stop_host
)
1410 lnw
->iotg
.stop_host(&lnw
->iotg
);
1413 "host driver removed.\n");
1416 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1417 iotg
->otg
.state
= OTG_STATE_A_WAIT_VFALL
;
1421 case OTG_STATE_A_HOST
:
1423 iotg
->otg
.default_a
= 0;
1424 iotg
->hsm
.b_bus_req
= 0;
1426 if (lnw
->iotg
.stop_host
)
1427 lnw
->iotg
.stop_host(&lnw
->iotg
);
1430 "host driver has been removed.\n");
1433 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1435 langwell_otg_phy_low_power_wait(1);
1436 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
1437 langwell_update_transceiver();
1438 } else if (iotg
->hsm
.a_bus_drop
||
1440 !iotg
->otg
.host
->b_hnp_enable
&&
1441 !iotg
->hsm
.a_bus_req
)) {
1442 if (lnw
->iotg
.stop_host
)
1443 lnw
->iotg
.stop_host(&lnw
->iotg
);
1446 "host driver has been removed.\n");
1449 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1450 iotg
->otg
.state
= OTG_STATE_A_WAIT_VFALL
;
1451 } else if (!iotg
->hsm
.a_vbus_vld
) {
1452 if (lnw
->iotg
.stop_host
)
1453 lnw
->iotg
.stop_host(&lnw
->iotg
);
1456 "host driver has been removed.\n");
1459 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1460 langwell_otg_phy_low_power_wait(1);
1461 iotg
->otg
.state
= OTG_STATE_A_VBUS_ERR
;
1462 } else if (iotg
->otg
.host
&&
1463 iotg
->otg
.host
->b_hnp_enable
&&
1464 !iotg
->hsm
.a_bus_req
) {
1465 /* Set HABA to enable hardware assistance to signal
1466 * A-connect after receiver B-disconnect. Hardware
1467 * will then set client mode and enable URE, SLE and
1468 * PCE after the assistance. otg_dummy_irq is used to
1469 * clean these ints when client driver is not resumed.
1471 if (request_irq(pdev
->irq
, otg_dummy_irq
, IRQF_SHARED
,
1472 driver_name
, iotg
->base
) != 0) {
1474 "request interrupt %d failed\n",
1479 langwell_otg_HABA(1);
1480 iotg
->hsm
.b_bus_resume
= 0;
1481 iotg
->hsm
.a_aidl_bdis_tmout
= 0;
1482 langwell_otg_loc_sof(0);
1483 /* clear PHCD to enable HW timer */
1484 langwell_otg_phy_low_power(0);
1485 langwell_otg_add_timer(a_aidl_bdis_tmr
);
1486 iotg
->otg
.state
= OTG_STATE_A_SUSPEND
;
1487 } else if (!iotg
->hsm
.b_conn
|| !iotg
->hsm
.a_bus_req
) {
1488 langwell_otg_add_ktimer(TA_WAIT_BCON_TMR
);
1489 iotg
->otg
.state
= OTG_STATE_A_WAIT_BCON
;
1492 case OTG_STATE_A_SUSPEND
:
1494 langwell_otg_del_timer(a_aidl_bdis_tmr
);
1495 langwell_otg_HABA(0);
1496 free_irq(pdev
->irq
, iotg
->base
);
1497 iotg
->otg
.default_a
= 0;
1498 iotg
->hsm
.b_bus_req
= 0;
1500 if (lnw
->iotg
.stop_host
)
1501 lnw
->iotg
.stop_host(&lnw
->iotg
);
1504 "host driver has been removed.\n");
1507 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1509 langwell_otg_phy_low_power(1);
1510 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
1511 langwell_update_transceiver();
1512 } else if (iotg
->hsm
.a_bus_req
||
1513 iotg
->hsm
.b_bus_resume
) {
1514 langwell_otg_del_timer(a_aidl_bdis_tmr
);
1515 langwell_otg_HABA(0);
1516 free_irq(pdev
->irq
, iotg
->base
);
1517 iotg
->hsm
.a_suspend_req
= 0;
1518 langwell_otg_loc_sof(1);
1519 iotg
->otg
.state
= OTG_STATE_A_HOST
;
1520 } else if (iotg
->hsm
.a_aidl_bdis_tmout
||
1521 iotg
->hsm
.a_bus_drop
) {
1522 langwell_otg_del_timer(a_aidl_bdis_tmr
);
1523 langwell_otg_HABA(0);
1524 free_irq(pdev
->irq
, iotg
->base
);
1525 if (lnw
->iotg
.stop_host
)
1526 lnw
->iotg
.stop_host(&lnw
->iotg
);
1529 "host driver has been removed.\n");
1532 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1533 iotg
->otg
.state
= OTG_STATE_A_WAIT_VFALL
;
1534 } else if (!iotg
->hsm
.b_conn
&& iotg
->otg
.host
&&
1535 iotg
->otg
.host
->b_hnp_enable
) {
1536 langwell_otg_del_timer(a_aidl_bdis_tmr
);
1537 langwell_otg_HABA(0);
1538 free_irq(pdev
->irq
, iotg
->base
);
1540 if (lnw
->iotg
.stop_host
)
1541 lnw
->iotg
.stop_host(&lnw
->iotg
);
1544 "host driver has been removed.\n");
1546 iotg
->hsm
.b_bus_suspend
= 0;
1547 iotg
->hsm
.b_bus_suspend_vld
= 0;
1550 if (lnw
->iotg
.start_peripheral
)
1551 lnw
->iotg
.start_peripheral(&lnw
->iotg
);
1554 "client driver not loaded.\n");
1556 langwell_otg_add_ktimer(TB_BUS_SUSPEND_TMR
);
1557 iotg
->otg
.state
= OTG_STATE_A_PERIPHERAL
;
1559 } else if (!iotg
->hsm
.a_vbus_vld
) {
1560 langwell_otg_del_timer(a_aidl_bdis_tmr
);
1561 langwell_otg_HABA(0);
1562 free_irq(pdev
->irq
, iotg
->base
);
1563 if (lnw
->iotg
.stop_host
)
1564 lnw
->iotg
.stop_host(&lnw
->iotg
);
1567 "host driver has been removed.\n");
1570 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1571 langwell_otg_phy_low_power_wait(1);
1572 iotg
->otg
.state
= OTG_STATE_A_VBUS_ERR
;
1575 case OTG_STATE_A_PERIPHERAL
:
1577 /* delete hsm timer for b_bus_suspend_tmr */
1578 del_timer_sync(&lnw
->hsm_timer
);
1579 iotg
->otg
.default_a
= 0;
1580 iotg
->hsm
.b_bus_req
= 0;
1581 if (lnw
->iotg
.stop_peripheral
)
1582 lnw
->iotg
.stop_peripheral(&lnw
->iotg
);
1585 "client driver has been removed.\n");
1588 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1590 langwell_otg_phy_low_power_wait(1);
1591 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
1592 langwell_update_transceiver();
1593 } else if (!iotg
->hsm
.a_vbus_vld
) {
1594 /* delete hsm timer for b_bus_suspend_tmr */
1595 del_timer_sync(&lnw
->hsm_timer
);
1597 if (lnw
->iotg
.stop_peripheral
)
1598 lnw
->iotg
.stop_peripheral(&lnw
->iotg
);
1601 "client driver has been removed.\n");
1604 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1605 langwell_otg_phy_low_power_wait(1);
1606 iotg
->otg
.state
= OTG_STATE_A_VBUS_ERR
;
1607 } else if (iotg
->hsm
.a_bus_drop
) {
1608 /* delete hsm timer for b_bus_suspend_tmr */
1609 del_timer_sync(&lnw
->hsm_timer
);
1611 if (lnw
->iotg
.stop_peripheral
)
1612 lnw
->iotg
.stop_peripheral(&lnw
->iotg
);
1615 "client driver has been removed.\n");
1618 iotg
->otg
.set_vbus(&iotg
->otg
, false);
1619 iotg
->otg
.state
= OTG_STATE_A_WAIT_VFALL
;
1620 } else if (iotg
->hsm
.b_bus_suspend
) {
1621 /* delete hsm timer for b_bus_suspend_tmr */
1622 del_timer_sync(&lnw
->hsm_timer
);
1624 if (lnw
->iotg
.stop_peripheral
)
1625 lnw
->iotg
.stop_peripheral(&lnw
->iotg
);
1628 "client driver has been removed.\n");
1630 if (lnw
->iotg
.start_host
)
1631 lnw
->iotg
.start_host(&lnw
->iotg
);
1634 "host driver not loaded.\n");
1635 langwell_otg_add_ktimer(TA_WAIT_BCON_TMR
);
1636 iotg
->otg
.state
= OTG_STATE_A_WAIT_BCON
;
1637 } else if (iotg
->hsm
.b_bus_suspend_tmout
) {
1639 val
= readl(lnw
->iotg
.base
+ CI_PORTSC1
);
1640 if (!(val
& PORTSC_SUSP
))
1643 if (lnw
->iotg
.stop_peripheral
)
1644 lnw
->iotg
.stop_peripheral(&lnw
->iotg
);
1647 "client driver has been removed.\n");
1649 if (lnw
->iotg
.start_host
)
1650 lnw
->iotg
.start_host(&lnw
->iotg
);
1653 "host driver not loaded.\n");
1654 langwell_otg_add_ktimer(TA_WAIT_BCON_TMR
);
1655 iotg
->otg
.state
= OTG_STATE_A_WAIT_BCON
;
1658 case OTG_STATE_A_VBUS_ERR
:
1660 iotg
->otg
.default_a
= 0;
1661 iotg
->hsm
.a_clr_err
= 0;
1662 iotg
->hsm
.a_srp_det
= 0;
1664 langwell_otg_phy_low_power(1);
1665 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
1666 langwell_update_transceiver();
1667 } else if (iotg
->hsm
.a_clr_err
) {
1668 iotg
->hsm
.a_clr_err
= 0;
1669 iotg
->hsm
.a_srp_det
= 0;
1672 if (iotg
->otg
.state
== OTG_STATE_A_IDLE
)
1673 langwell_update_transceiver();
1675 /* FW will clear PHCD bit when any VBus
1676 * event detected. Reset PHCD to 1 again */
1677 langwell_otg_phy_low_power(1);
1680 case OTG_STATE_A_WAIT_VFALL
:
1682 iotg
->otg
.default_a
= 0;
1684 langwell_otg_phy_low_power(1);
1685 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
1686 langwell_update_transceiver();
1687 } else if (iotg
->hsm
.a_bus_req
) {
1690 iotg
->otg
.set_vbus(&iotg
->otg
, true);
1691 iotg
->hsm
.a_wait_vrise_tmout
= 0;
1692 langwell_otg_add_timer(a_wait_vrise_tmr
);
1693 iotg
->otg
.state
= OTG_STATE_A_WAIT_VRISE
;
1694 } else if (!iotg
->hsm
.a_sess_vld
) {
1695 iotg
->hsm
.a_srp_det
= 0;
1697 langwell_otg_phy_low_power(1);
1698 iotg
->otg
.state
= OTG_STATE_A_IDLE
;
1705 dev_dbg(lnw
->dev
, "%s: new state = %s\n", __func__
,
1706 state_string(iotg
->otg
.state
));
1710 show_registers(struct device
*_dev
, struct device_attribute
*attr
, char *buf
)
1712 struct langwell_otg
*lnw
= the_transceiver
;
1719 t
= scnprintf(next
, size
,
1723 "USBINTR = 0x%08x\n"
1724 "ASYNCLISTADDR = 0x%08x\n"
1725 "PORTSC1 = 0x%08x\n"
1726 "HOSTPC1 = 0x%08x\n"
1728 "USBMODE = 0x%08x\n",
1729 readl(lnw
->iotg
.base
+ 0x30),
1730 readl(lnw
->iotg
.base
+ 0x34),
1731 readl(lnw
->iotg
.base
+ 0x38),
1732 readl(lnw
->iotg
.base
+ 0x48),
1733 readl(lnw
->iotg
.base
+ 0x74),
1734 readl(lnw
->iotg
.base
+ 0xb4),
1735 readl(lnw
->iotg
.base
+ 0xf4),
1736 readl(lnw
->iotg
.base
+ 0xf8)
1741 return PAGE_SIZE
- size
;
1743 static DEVICE_ATTR(registers
, S_IRUGO
, show_registers
, NULL
);
1746 show_hsm(struct device
*_dev
, struct device_attribute
*attr
, char *buf
)
1748 struct langwell_otg
*lnw
= the_transceiver
;
1749 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
1757 iotg
->hsm
.a_set_b_hnp_en
= iotg
->otg
.host
->b_hnp_enable
;
1759 if (iotg
->otg
.gadget
)
1760 iotg
->hsm
.b_hnp_enable
= iotg
->otg
.gadget
->b_hnp_enable
;
1762 t
= scnprintf(next
, size
,
1764 "current state = %s\n"
1765 "a_bus_resume = \t%d\n"
1766 "a_bus_suspend = \t%d\n"
1768 "a_sess_vld = \t%d\n"
1769 "a_srp_det = \t%d\n"
1770 "a_vbus_vld = \t%d\n"
1771 "b_bus_resume = \t%d\n"
1772 "b_bus_suspend = \t%d\n"
1774 "b_se0_srp = \t%d\n"
1775 "b_sess_end = \t%d\n"
1776 "b_sess_vld = \t%d\n"
1778 "a_set_b_hnp_en = \t%d\n"
1779 "b_srp_done = \t%d\n"
1780 "b_hnp_enable = \t%d\n"
1781 "a_wait_vrise_tmout = \t%d\n"
1782 "a_wait_bcon_tmout = \t%d\n"
1783 "a_aidl_bdis_tmout = \t%d\n"
1784 "b_ase0_brst_tmout = \t%d\n"
1785 "a_bus_drop = \t%d\n"
1786 "a_bus_req = \t%d\n"
1787 "a_clr_err = \t%d\n"
1788 "a_suspend_req = \t%d\n"
1789 "b_bus_req = \t%d\n"
1790 "b_bus_suspend_tmout = \t%d\n"
1791 "b_bus_suspend_vld = \t%d\n",
1792 state_string(iotg
->otg
.state
),
1793 iotg
->hsm
.a_bus_resume
,
1794 iotg
->hsm
.a_bus_suspend
,
1796 iotg
->hsm
.a_sess_vld
,
1797 iotg
->hsm
.a_srp_det
,
1798 iotg
->hsm
.a_vbus_vld
,
1799 iotg
->hsm
.b_bus_resume
,
1800 iotg
->hsm
.b_bus_suspend
,
1802 iotg
->hsm
.b_se0_srp
,
1803 iotg
->hsm
.b_sess_end
,
1804 iotg
->hsm
.b_sess_vld
,
1806 iotg
->hsm
.a_set_b_hnp_en
,
1807 iotg
->hsm
.b_srp_done
,
1808 iotg
->hsm
.b_hnp_enable
,
1809 iotg
->hsm
.a_wait_vrise_tmout
,
1810 iotg
->hsm
.a_wait_bcon_tmout
,
1811 iotg
->hsm
.a_aidl_bdis_tmout
,
1812 iotg
->hsm
.b_ase0_brst_tmout
,
1813 iotg
->hsm
.a_bus_drop
,
1814 iotg
->hsm
.a_bus_req
,
1815 iotg
->hsm
.a_clr_err
,
1816 iotg
->hsm
.a_suspend_req
,
1817 iotg
->hsm
.b_bus_req
,
1818 iotg
->hsm
.b_bus_suspend_tmout
,
1819 iotg
->hsm
.b_bus_suspend_vld
1824 return PAGE_SIZE
- size
;
1826 static DEVICE_ATTR(hsm
, S_IRUGO
, show_hsm
, NULL
);
1829 get_a_bus_req(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1831 struct langwell_otg
*lnw
= the_transceiver
;
1838 t
= scnprintf(next
, size
, "%d", lnw
->iotg
.hsm
.a_bus_req
);
1842 return PAGE_SIZE
- size
;
1846 set_a_bus_req(struct device
*dev
, struct device_attribute
*attr
,
1847 const char *buf
, size_t count
)
1849 struct langwell_otg
*lnw
= the_transceiver
;
1850 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
1852 if (!iotg
->otg
.default_a
)
1857 if (buf
[0] == '0') {
1858 iotg
->hsm
.a_bus_req
= 0;
1859 dev_dbg(lnw
->dev
, "User request: a_bus_req = 0\n");
1860 } else if (buf
[0] == '1') {
1861 /* If a_bus_drop is TRUE, a_bus_req can't be set */
1862 if (iotg
->hsm
.a_bus_drop
)
1864 iotg
->hsm
.a_bus_req
= 1;
1865 dev_dbg(lnw
->dev
, "User request: a_bus_req = 1\n");
1867 if (spin_trylock(&lnw
->wq_lock
)) {
1868 langwell_update_transceiver();
1869 spin_unlock(&lnw
->wq_lock
);
1873 static DEVICE_ATTR(a_bus_req
, S_IRUGO
| S_IWUSR
, get_a_bus_req
, set_a_bus_req
);
1876 get_a_bus_drop(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1878 struct langwell_otg
*lnw
= the_transceiver
;
1885 t
= scnprintf(next
, size
, "%d", lnw
->iotg
.hsm
.a_bus_drop
);
1889 return PAGE_SIZE
- size
;
1893 set_a_bus_drop(struct device
*dev
, struct device_attribute
*attr
,
1894 const char *buf
, size_t count
)
1896 struct langwell_otg
*lnw
= the_transceiver
;
1897 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
1899 if (!iotg
->otg
.default_a
)
1904 if (buf
[0] == '0') {
1905 iotg
->hsm
.a_bus_drop
= 0;
1906 dev_dbg(lnw
->dev
, "User request: a_bus_drop = 0\n");
1907 } else if (buf
[0] == '1') {
1908 iotg
->hsm
.a_bus_drop
= 1;
1909 iotg
->hsm
.a_bus_req
= 0;
1910 dev_dbg(lnw
->dev
, "User request: a_bus_drop = 1\n");
1911 dev_dbg(lnw
->dev
, "User request: and a_bus_req = 0\n");
1913 if (spin_trylock(&lnw
->wq_lock
)) {
1914 langwell_update_transceiver();
1915 spin_unlock(&lnw
->wq_lock
);
1919 static DEVICE_ATTR(a_bus_drop
, S_IRUGO
| S_IWUSR
, get_a_bus_drop
, set_a_bus_drop
);
1922 get_b_bus_req(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1924 struct langwell_otg
*lnw
= the_transceiver
;
1931 t
= scnprintf(next
, size
, "%d", lnw
->iotg
.hsm
.b_bus_req
);
1935 return PAGE_SIZE
- size
;
1939 set_b_bus_req(struct device
*dev
, struct device_attribute
*attr
,
1940 const char *buf
, size_t count
)
1942 struct langwell_otg
*lnw
= the_transceiver
;
1943 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
1945 if (iotg
->otg
.default_a
)
1951 if (buf
[0] == '0') {
1952 iotg
->hsm
.b_bus_req
= 0;
1953 dev_dbg(lnw
->dev
, "User request: b_bus_req = 0\n");
1954 } else if (buf
[0] == '1') {
1955 iotg
->hsm
.b_bus_req
= 1;
1956 dev_dbg(lnw
->dev
, "User request: b_bus_req = 1\n");
1958 if (spin_trylock(&lnw
->wq_lock
)) {
1959 langwell_update_transceiver();
1960 spin_unlock(&lnw
->wq_lock
);
1964 static DEVICE_ATTR(b_bus_req
, S_IRUGO
| S_IWUSR
, get_b_bus_req
, set_b_bus_req
);
1967 set_a_clr_err(struct device
*dev
, struct device_attribute
*attr
,
1968 const char *buf
, size_t count
)
1970 struct langwell_otg
*lnw
= the_transceiver
;
1971 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
1973 if (!iotg
->otg
.default_a
)
1978 if (buf
[0] == '1') {
1979 iotg
->hsm
.a_clr_err
= 1;
1980 dev_dbg(lnw
->dev
, "User request: a_clr_err = 1\n");
1982 if (spin_trylock(&lnw
->wq_lock
)) {
1983 langwell_update_transceiver();
1984 spin_unlock(&lnw
->wq_lock
);
1988 static DEVICE_ATTR(a_clr_err
, S_IWUSR
, NULL
, set_a_clr_err
);
1990 static struct attribute
*inputs_attrs
[] = {
1991 &dev_attr_a_bus_req
.attr
,
1992 &dev_attr_a_bus_drop
.attr
,
1993 &dev_attr_b_bus_req
.attr
,
1994 &dev_attr_a_clr_err
.attr
,
1998 static struct attribute_group debug_dev_attr_group
= {
2000 .attrs
= inputs_attrs
,
2003 static int langwell_otg_probe(struct pci_dev
*pdev
,
2004 const struct pci_device_id
*id
)
2006 unsigned long resource
, len
;
2007 void __iomem
*base
= NULL
;
2010 struct langwell_otg
*lnw
;
2011 char qname
[] = "langwell_otg_queue";
2014 dev_dbg(&pdev
->dev
, "\notg controller is detected.\n");
2015 if (pci_enable_device(pdev
) < 0) {
2020 lnw
= kzalloc(sizeof *lnw
, GFP_KERNEL
);
2025 the_transceiver
= lnw
;
2027 /* control register: BAR 0 */
2028 resource
= pci_resource_start(pdev
, 0);
2029 len
= pci_resource_len(pdev
, 0);
2030 if (!request_mem_region(resource
, len
, driver_name
)) {
2036 base
= ioremap_nocache(resource
, len
);
2041 lnw
->iotg
.base
= base
;
2043 if (!request_mem_region(USBCFG_ADDR
, USBCFG_LEN
, driver_name
)) {
2047 lnw
->cfg_region
= 1;
2049 /* For the SCCB.USBCFG register */
2050 base
= ioremap_nocache(USBCFG_ADDR
, USBCFG_LEN
);
2058 dev_dbg(&pdev
->dev
, "No IRQ.\n");
2063 lnw
->qwork
= create_singlethread_workqueue(qname
);
2065 dev_dbg(&pdev
->dev
, "cannot create workqueue %s\n", qname
);
2069 INIT_WORK(&lnw
->work
, langwell_otg_work
);
2071 /* OTG common part */
2072 lnw
->dev
= &pdev
->dev
;
2073 lnw
->iotg
.otg
.dev
= lnw
->dev
;
2074 lnw
->iotg
.otg
.label
= driver_name
;
2075 lnw
->iotg
.otg
.set_host
= langwell_otg_set_host
;
2076 lnw
->iotg
.otg
.set_peripheral
= langwell_otg_set_peripheral
;
2077 lnw
->iotg
.otg
.set_power
= langwell_otg_set_power
;
2078 lnw
->iotg
.otg
.set_vbus
= langwell_otg_set_vbus
;
2079 lnw
->iotg
.otg
.start_srp
= langwell_otg_start_srp
;
2080 lnw
->iotg
.otg
.state
= OTG_STATE_UNDEFINED
;
2082 if (otg_set_transceiver(&lnw
->iotg
.otg
)) {
2083 dev_dbg(lnw
->dev
, "can't set transceiver\n");
2091 spin_lock_init(&lnw
->lock
);
2092 spin_lock_init(&lnw
->wq_lock
);
2093 INIT_LIST_HEAD(&active_timers
);
2094 retval
= langwell_otg_init_timers(&lnw
->iotg
.hsm
);
2096 dev_dbg(&pdev
->dev
, "Failed to init timers\n");
2100 init_timer(&lnw
->hsm_timer
);
2101 ATOMIC_INIT_NOTIFIER_HEAD(&lnw
->iotg
.iotg_notifier
);
2103 lnw
->iotg_notifier
.notifier_call
= langwell_otg_iotg_notify
;
2105 retval
= intel_mid_otg_register_notifier(&lnw
->iotg
,
2106 &lnw
->iotg_notifier
);
2108 dev_dbg(lnw
->dev
, "Failed to register notifier\n");
2112 if (request_irq(pdev
->irq
, otg_irq
, IRQF_SHARED
,
2113 driver_name
, lnw
) != 0) {
2114 dev_dbg(lnw
->dev
, "request interrupt %d failed\n", pdev
->irq
);
2119 /* enable OTGSC int */
2120 val32
= OTGSC_DPIE
| OTGSC_BSEIE
| OTGSC_BSVIE
|
2121 OTGSC_ASVIE
| OTGSC_AVVIE
| OTGSC_IDIE
| OTGSC_IDPU
;
2122 writel(val32
, lnw
->iotg
.base
+ CI_OTGSC
);
2124 retval
= device_create_file(&pdev
->dev
, &dev_attr_registers
);
2127 "Can't register sysfs attribute: %d\n", retval
);
2131 retval
= device_create_file(&pdev
->dev
, &dev_attr_hsm
);
2133 dev_dbg(lnw
->dev
, "Can't hsm sysfs attribute: %d\n", retval
);
2137 retval
= sysfs_create_group(&pdev
->dev
.kobj
, &debug_dev_attr_group
);
2140 "Can't register sysfs attr group: %d\n", retval
);
2144 if (lnw
->iotg
.otg
.state
== OTG_STATE_A_IDLE
)
2145 langwell_update_transceiver();
2150 if (the_transceiver
)
2151 langwell_otg_remove(pdev
);
2156 static void langwell_otg_remove(struct pci_dev
*pdev
)
2158 struct langwell_otg
*lnw
= the_transceiver
;
2161 flush_workqueue(lnw
->qwork
);
2162 destroy_workqueue(lnw
->qwork
);
2164 intel_mid_otg_unregister_notifier(&lnw
->iotg
, &lnw
->iotg_notifier
);
2165 langwell_otg_free_timers();
2167 /* disable OTGSC interrupt as OTGSC doesn't change in reset */
2168 writel(0, lnw
->iotg
.base
+ CI_OTGSC
);
2171 free_irq(pdev
->irq
, lnw
);
2173 iounmap(lnw
->usbcfg
);
2174 if (lnw
->cfg_region
)
2175 release_mem_region(USBCFG_ADDR
, USBCFG_LEN
);
2177 iounmap(lnw
->iotg
.base
);
2179 release_mem_region(pci_resource_start(pdev
, 0),
2180 pci_resource_len(pdev
, 0));
2182 otg_set_transceiver(NULL
);
2183 pci_disable_device(pdev
);
2184 sysfs_remove_group(&pdev
->dev
.kobj
, &debug_dev_attr_group
);
2185 device_remove_file(&pdev
->dev
, &dev_attr_hsm
);
2186 device_remove_file(&pdev
->dev
, &dev_attr_registers
);
2191 static void transceiver_suspend(struct pci_dev
*pdev
)
2193 pci_save_state(pdev
);
2194 pci_set_power_state(pdev
, PCI_D3hot
);
2195 langwell_otg_phy_low_power(1);
2198 static int langwell_otg_suspend(struct pci_dev
*pdev
, pm_message_t message
)
2200 struct langwell_otg
*lnw
= the_transceiver
;
2201 struct intel_mid_otg_xceiv
*iotg
= &lnw
->iotg
;
2204 /* Disbale OTG interrupts */
2205 langwell_otg_intr(0);
2208 free_irq(pdev
->irq
, lnw
);
2210 /* Prevent more otg_work */
2211 flush_workqueue(lnw
->qwork
);
2212 destroy_workqueue(lnw
->qwork
);
2216 switch (iotg
->otg
.state
) {
2217 case OTG_STATE_A_WAIT_VFALL
:
2218 iotg
->otg
.state
= OTG_STATE_A_IDLE
;
2219 case OTG_STATE_A_IDLE
:
2220 case OTG_STATE_B_IDLE
:
2221 case OTG_STATE_A_VBUS_ERR
:
2222 transceiver_suspend(pdev
);
2224 case OTG_STATE_A_WAIT_VRISE
:
2225 langwell_otg_del_timer(a_wait_vrise_tmr
);
2226 iotg
->hsm
.a_srp_det
= 0;
2229 iotg
->otg
.set_vbus(&iotg
->otg
, false);
2230 iotg
->otg
.state
= OTG_STATE_A_IDLE
;
2231 transceiver_suspend(pdev
);
2233 case OTG_STATE_A_WAIT_BCON
:
2234 del_timer_sync(&lnw
->hsm_timer
);
2235 if (lnw
->iotg
.stop_host
)
2236 lnw
->iotg
.stop_host(&lnw
->iotg
);
2238 dev_dbg(&pdev
->dev
, "host driver has been removed.\n");
2240 iotg
->hsm
.a_srp_det
= 0;
2243 iotg
->otg
.set_vbus(&iotg
->otg
, false);
2244 iotg
->otg
.state
= OTG_STATE_A_IDLE
;
2245 transceiver_suspend(pdev
);
2247 case OTG_STATE_A_HOST
:
2248 if (lnw
->iotg
.stop_host
)
2249 lnw
->iotg
.stop_host(&lnw
->iotg
);
2251 dev_dbg(&pdev
->dev
, "host driver has been removed.\n");
2253 iotg
->hsm
.a_srp_det
= 0;
2256 iotg
->otg
.set_vbus(&iotg
->otg
, false);
2258 iotg
->otg
.state
= OTG_STATE_A_IDLE
;
2259 transceiver_suspend(pdev
);
2261 case OTG_STATE_A_SUSPEND
:
2262 langwell_otg_del_timer(a_aidl_bdis_tmr
);
2263 langwell_otg_HABA(0);
2264 if (lnw
->iotg
.stop_host
)
2265 lnw
->iotg
.stop_host(&lnw
->iotg
);
2267 dev_dbg(lnw
->dev
, "host driver has been removed.\n");
2268 iotg
->hsm
.a_srp_det
= 0;
2271 iotg
->otg
.set_vbus(&iotg
->otg
, false);
2272 iotg
->otg
.state
= OTG_STATE_A_IDLE
;
2273 transceiver_suspend(pdev
);
2275 case OTG_STATE_A_PERIPHERAL
:
2276 del_timer_sync(&lnw
->hsm_timer
);
2278 if (lnw
->iotg
.stop_peripheral
)
2279 lnw
->iotg
.stop_peripheral(&lnw
->iotg
);
2282 "client driver has been removed.\n");
2283 iotg
->hsm
.a_srp_det
= 0;
2286 iotg
->otg
.set_vbus(&iotg
->otg
, false);
2287 iotg
->otg
.state
= OTG_STATE_A_IDLE
;
2288 transceiver_suspend(pdev
);
2290 case OTG_STATE_B_HOST
:
2291 if (lnw
->iotg
.stop_host
)
2292 lnw
->iotg
.stop_host(&lnw
->iotg
);
2294 dev_dbg(&pdev
->dev
, "host driver has been removed.\n");
2295 iotg
->hsm
.b_bus_req
= 0;
2296 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
2297 transceiver_suspend(pdev
);
2299 case OTG_STATE_B_PERIPHERAL
:
2300 if (lnw
->iotg
.stop_peripheral
)
2301 lnw
->iotg
.stop_peripheral(&lnw
->iotg
);
2304 "client driver has been removed.\n");
2305 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
2306 transceiver_suspend(pdev
);
2308 case OTG_STATE_B_WAIT_ACON
:
2309 /* delete hsm timer for b_ase0_brst_tmr */
2310 del_timer_sync(&lnw
->hsm_timer
);
2312 langwell_otg_HAAR(0);
2314 if (lnw
->iotg
.stop_host
)
2315 lnw
->iotg
.stop_host(&lnw
->iotg
);
2317 dev_dbg(&pdev
->dev
, "host driver has been removed.\n");
2318 iotg
->hsm
.b_bus_req
= 0;
2319 iotg
->otg
.state
= OTG_STATE_B_IDLE
;
2320 transceiver_suspend(pdev
);
2323 dev_dbg(lnw
->dev
, "error state before suspend\n");
2330 static void transceiver_resume(struct pci_dev
*pdev
)
2332 pci_restore_state(pdev
);
2333 pci_set_power_state(pdev
, PCI_D0
);
2336 static int langwell_otg_resume(struct pci_dev
*pdev
)
2338 struct langwell_otg
*lnw
= the_transceiver
;
2341 transceiver_resume(pdev
);
2343 lnw
->qwork
= create_singlethread_workqueue("langwell_otg_queue");
2345 dev_dbg(&pdev
->dev
, "cannot create langwell otg workqueuen");
2350 if (request_irq(pdev
->irq
, otg_irq
, IRQF_SHARED
,
2351 driver_name
, lnw
) != 0) {
2352 dev_dbg(&pdev
->dev
, "request interrupt %d failed\n", pdev
->irq
);
2357 /* enable OTG interrupts */
2358 langwell_otg_intr(1);
2362 langwell_update_transceiver();
2366 langwell_otg_intr(0);
2367 transceiver_suspend(pdev
);
2371 static int __init
langwell_otg_init(void)
2373 return pci_register_driver(&otg_pci_driver
);
2375 module_init(langwell_otg_init
);
2377 static void __exit
langwell_otg_cleanup(void)
2379 pci_unregister_driver(&otg_pci_driver
);
2381 module_exit(langwell_otg_cleanup
);