1 /* Copyright (C) 2007,2008 Freescale Semiconductor, Inc.
2  *
3  * This program is free software; you can redistribute  it and/or modify it
4  * under  the terms of  the GNU General  Public License as published by the
5  * Free Software Foundation;  either version 2 of the  License, or (at your
6  * option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the  GNU General Public License along
14  * with this program; if not, write  to the Free Software Foundation, Inc.,
15  * 675 Mass Ave, Cambridge, MA 02139, USA.
16  */
17 
18 #undef DEBUG
19 #undef VERBOSE
20 
21 #ifdef DEBUG
22 #define DBG(fmt, args...) printk(KERN_DEBUG "[%s]  " fmt , \
23 				 __func__, ## args)
24 #else
25 #define DBG(fmt, args...)	do {} while (0)
26 #endif
27 
28 #ifdef VERBOSE
29 #define VDBG		DBG
30 #else
31 #define VDBG(stuff...)	do {} while (0)
32 #endif
33 
34 #ifdef VERBOSE
35 #define MPC_LOC printk("Current Location [%s]:[%d]\n", __FILE__, __LINE__)
36 #else
37 #define MPC_LOC do {} while (0)
38 #endif
39 
40 #define PROTO_UNDEF	(0)
41 #define PROTO_HOST	(1)
42 #define PROTO_GADGET	(2)
43 
44 /* OTG state machine according to the OTG spec */
45 struct otg_fsm {
46 	/* Input */
47 	int a_bus_resume;
48 	int a_bus_suspend;
49 	int a_conn;
50 	int a_sess_vld;
51 	int a_srp_det;
52 	int a_vbus_vld;
53 	int b_bus_resume;
54 	int b_bus_suspend;
55 	int b_conn;
56 	int b_se0_srp;
57 	int b_sess_end;
58 	int b_sess_vld;
59 	int id;
60 
61 	/* Internal variables */
62 	int a_set_b_hnp_en;
63 	int b_srp_done;
64 	int b_hnp_enable;
65 
66 	/* Timeout indicator for timers */
67 	int a_wait_vrise_tmout;
68 	int a_wait_bcon_tmout;
69 	int a_aidl_bdis_tmout;
70 	int b_ase0_brst_tmout;
71 
72 	/* Informative variables */
73 	int a_bus_drop;
74 	int a_bus_req;
75 	int a_clr_err;
76 	int a_suspend_req;
77 	int b_bus_req;
78 
79 	/* Output */
80 	int drv_vbus;
81 	int loc_conn;
82 	int loc_sof;
83 
84 	struct otg_fsm_ops *ops;
85 	struct usb_otg *otg;
86 
87 	/* Current usb protocol used: 0:undefine; 1:host; 2:client */
88 	int protocol;
89 	spinlock_t lock;
90 };
91 
92 struct otg_fsm_ops {
93 	void	(*chrg_vbus)(int on);
94 	void	(*drv_vbus)(int on);
95 	void	(*loc_conn)(int on);
96 	void	(*loc_sof)(int on);
97 	void	(*start_pulse)(void);
98 	void	(*add_timer)(void *timer);
99 	void	(*del_timer)(void *timer);
100 	int	(*start_host)(struct otg_fsm *fsm, int on);
101 	int	(*start_gadget)(struct otg_fsm *fsm, int on);
102 };
103 
104 
otg_chrg_vbus(struct otg_fsm * fsm,int on)105 static inline void otg_chrg_vbus(struct otg_fsm *fsm, int on)
106 {
107 	fsm->ops->chrg_vbus(on);
108 }
109 
otg_drv_vbus(struct otg_fsm * fsm,int on)110 static inline void otg_drv_vbus(struct otg_fsm *fsm, int on)
111 {
112 	if (fsm->drv_vbus != on) {
113 		fsm->drv_vbus = on;
114 		fsm->ops->drv_vbus(on);
115 	}
116 }
117 
otg_loc_conn(struct otg_fsm * fsm,int on)118 static inline void otg_loc_conn(struct otg_fsm *fsm, int on)
119 {
120 	if (fsm->loc_conn != on) {
121 		fsm->loc_conn = on;
122 		fsm->ops->loc_conn(on);
123 	}
124 }
125 
otg_loc_sof(struct otg_fsm * fsm,int on)126 static inline void otg_loc_sof(struct otg_fsm *fsm, int on)
127 {
128 	if (fsm->loc_sof != on) {
129 		fsm->loc_sof = on;
130 		fsm->ops->loc_sof(on);
131 	}
132 }
133 
otg_start_pulse(struct otg_fsm * fsm)134 static inline void otg_start_pulse(struct otg_fsm *fsm)
135 {
136 	fsm->ops->start_pulse();
137 }
138 
otg_add_timer(struct otg_fsm * fsm,void * timer)139 static inline void otg_add_timer(struct otg_fsm *fsm, void *timer)
140 {
141 	fsm->ops->add_timer(timer);
142 }
143 
otg_del_timer(struct otg_fsm * fsm,void * timer)144 static inline void otg_del_timer(struct otg_fsm *fsm, void *timer)
145 {
146 	fsm->ops->del_timer(timer);
147 }
148 
149 int otg_statemachine(struct otg_fsm *fsm);
150 
151 /* Defined by device specific driver, for different timer implementation */
152 extern struct fsl_otg_timer *a_wait_vrise_tmr, *a_wait_bcon_tmr,
153 	*a_aidl_bdis_tmr, *b_ase0_brst_tmr, *b_se0_srp_tmr, *b_srp_fail_tmr,
154 	*a_wait_enum_tmr;
155