blob: 7441b46a27f1b86ac609b2e6e5c71b8e187cfd1d [file] [log] [blame]
Li Yang0807c502011-04-18 22:01:59 +02001/* 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
Li Yang0807c502011-04-18 22:01:59 +020018#undef VERBOSE
19
Li Yang0807c502011-04-18 22:01:59 +020020#ifdef VERBOSE
Greg Kroah-Hartman523e5312013-06-28 11:32:55 -070021#define VDBG(fmt, args...) pr_debug("[%s] " fmt , \
22 __func__, ## args)
Li Yang0807c502011-04-18 22:01:59 +020023#else
24#define VDBG(stuff...) do {} while (0)
25#endif
26
27#ifdef VERBOSE
28#define MPC_LOC printk("Current Location [%s]:[%d]\n", __FILE__, __LINE__)
29#else
30#define MPC_LOC do {} while (0)
31#endif
32
33#define PROTO_UNDEF (0)
34#define PROTO_HOST (1)
35#define PROTO_GADGET (2)
36
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090037enum otg_fsm_timer {
Anton Tikhomirov32949082013-10-03 12:42:04 +090038 /* Standard OTG timers */
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090039 A_WAIT_VRISE,
Anton Tikhomirov32949082013-10-03 12:42:04 +090040 A_WAIT_VFALL,
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090041 A_WAIT_BCON,
42 A_AIDL_BDIS,
43 B_ASE0_BRST,
Anton Tikhomirov32949082013-10-03 12:42:04 +090044 A_BIDL_ADIS,
45
46 /* Auxiliary timers */
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090047 B_SE0_SRP,
48 B_SRP_FAIL,
49 A_WAIT_ENUM,
Anton Tikhomirov32949082013-10-03 12:42:04 +090050
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090051 NUM_OTG_FSM_TIMERS,
52};
53
Li Yang0807c502011-04-18 22:01:59 +020054/* OTG state machine according to the OTG spec */
55struct otg_fsm {
56 /* Input */
Anton Tikhomirov4662e5e2013-10-03 12:42:04 +090057 int id;
Anton Tikhomirovec049962013-10-03 12:42:04 +090058 int adp_change;
59 int power_up;
60 int test_device;
Anton Tikhomirovcff4dab2013-10-03 12:42:04 +090061 int a_bus_drop;
62 int a_bus_req;
Anton Tikhomirov4662e5e2013-10-03 12:42:04 +090063 int a_srp_det;
64 int a_vbus_vld;
65 int b_conn;
Li Yang0807c502011-04-18 22:01:59 +020066 int a_bus_resume;
67 int a_bus_suspend;
68 int a_conn;
Anton Tikhomirovcff4dab2013-10-03 12:42:04 +090069 int b_bus_req;
Li Yang0807c502011-04-18 22:01:59 +020070 int b_se0_srp;
Anton Tikhomirov68041782013-10-03 12:42:04 +090071 int b_ssend_srp;
Li Yang0807c502011-04-18 22:01:59 +020072 int b_sess_vld;
Anton Tikhomirov4662e5e2013-10-03 12:42:04 +090073 /* Auxilary inputs */
74 int a_sess_vld;
75 int b_bus_resume;
76 int b_bus_suspend;
Li Yang0807c502011-04-18 22:01:59 +020077
78 /* Output */
Anton Tikhomirovec049962013-10-03 12:42:04 +090079 int data_pulse;
Li Yang0807c502011-04-18 22:01:59 +020080 int drv_vbus;
81 int loc_conn;
82 int loc_sof;
Anton Tikhomirovec049962013-10-03 12:42:04 +090083 int adp_prb;
84 int adp_sns;
Li Yang0807c502011-04-18 22:01:59 +020085
Anton Tikhomirov4662e5e2013-10-03 12:42:04 +090086 /* Internal variables */
87 int a_set_b_hnp_en;
88 int b_srp_done;
89 int b_hnp_enable;
90 int a_clr_err;
91
92 /* Informative variables */
93 int a_bus_drop_inf;
94 int a_bus_req_inf;
95 int a_clr_err_inf;
96 int b_bus_req_inf;
97 /* Auxilary informative variables */
98 int a_suspend_req_inf;
99
100 /* Timeout indicator for timers */
101 int a_wait_vrise_tmout;
102 int a_wait_vfall_tmout;
103 int a_wait_bcon_tmout;
104 int a_aidl_bdis_tmout;
105 int b_ase0_brst_tmout;
106 int a_bidl_adis_tmout;
107
Li Yang0807c502011-04-18 22:01:59 +0200108 struct otg_fsm_ops *ops;
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200109 struct usb_otg *otg;
Li Yang0807c502011-04-18 22:01:59 +0200110
111 /* Current usb protocol used: 0:undefine; 1:host; 2:client */
112 int protocol;
113 spinlock_t lock;
114};
115
116struct otg_fsm_ops {
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900117 void (*chrg_vbus)(struct otg_fsm *fsm, int on);
118 void (*drv_vbus)(struct otg_fsm *fsm, int on);
119 void (*loc_conn)(struct otg_fsm *fsm, int on);
120 void (*loc_sof)(struct otg_fsm *fsm, int on);
121 void (*start_pulse)(struct otg_fsm *fsm);
Anton Tikhomirovec049962013-10-03 12:42:04 +0900122 void (*start_adp_prb)(struct otg_fsm *fsm);
123 void (*start_adp_sns)(struct otg_fsm *fsm);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900124 void (*add_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
125 void (*del_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
Li Yang0807c502011-04-18 22:01:59 +0200126 int (*start_host)(struct otg_fsm *fsm, int on);
127 int (*start_gadget)(struct otg_fsm *fsm, int on);
128};
129
130
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900131static inline int otg_chrg_vbus(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200132{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900133 if (!fsm->ops->chrg_vbus)
134 return -EOPNOTSUPP;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900135 fsm->ops->chrg_vbus(fsm, on);
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900136 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200137}
138
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900139static inline int otg_drv_vbus(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200140{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900141 if (!fsm->ops->drv_vbus)
142 return -EOPNOTSUPP;
Li Yang0807c502011-04-18 22:01:59 +0200143 if (fsm->drv_vbus != on) {
144 fsm->drv_vbus = on;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900145 fsm->ops->drv_vbus(fsm, on);
Li Yang0807c502011-04-18 22:01:59 +0200146 }
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900147 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200148}
149
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900150static inline int otg_loc_conn(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200151{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900152 if (!fsm->ops->loc_conn)
153 return -EOPNOTSUPP;
Li Yang0807c502011-04-18 22:01:59 +0200154 if (fsm->loc_conn != on) {
155 fsm->loc_conn = on;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900156 fsm->ops->loc_conn(fsm, on);
Li Yang0807c502011-04-18 22:01:59 +0200157 }
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900158 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200159}
160
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900161static inline int otg_loc_sof(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200162{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900163 if (!fsm->ops->loc_sof)
164 return -EOPNOTSUPP;
Li Yang0807c502011-04-18 22:01:59 +0200165 if (fsm->loc_sof != on) {
166 fsm->loc_sof = on;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900167 fsm->ops->loc_sof(fsm, on);
Li Yang0807c502011-04-18 22:01:59 +0200168 }
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900169 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200170}
171
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900172static inline int otg_start_pulse(struct otg_fsm *fsm)
Li Yang0807c502011-04-18 22:01:59 +0200173{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900174 if (!fsm->ops->start_pulse)
175 return -EOPNOTSUPP;
Anton Tikhomirovec049962013-10-03 12:42:04 +0900176 if (!fsm->data_pulse) {
177 fsm->data_pulse = 1;
178 fsm->ops->start_pulse(fsm);
179 }
180 return 0;
181}
182
183static inline int otg_start_adp_prb(struct otg_fsm *fsm)
184{
185 if (!fsm->ops->start_adp_prb)
186 return -EOPNOTSUPP;
187 if (!fsm->adp_prb) {
188 fsm->adp_sns = 0;
189 fsm->adp_prb = 1;
190 fsm->ops->start_adp_prb(fsm);
191 }
192 return 0;
193}
194
195static inline int otg_start_adp_sns(struct otg_fsm *fsm)
196{
197 if (!fsm->ops->start_adp_sns)
198 return -EOPNOTSUPP;
199 if (!fsm->adp_sns) {
200 fsm->adp_sns = 1;
201 fsm->ops->start_adp_sns(fsm);
202 }
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900203 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200204}
205
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900206static inline int otg_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
Li Yang0807c502011-04-18 22:01:59 +0200207{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900208 if (!fsm->ops->add_timer)
209 return -EOPNOTSUPP;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900210 fsm->ops->add_timer(fsm, timer);
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900211 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200212}
213
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900214static inline int otg_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
Li Yang0807c502011-04-18 22:01:59 +0200215{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900216 if (!fsm->ops->del_timer)
217 return -EOPNOTSUPP;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900218 fsm->ops->del_timer(fsm, timer);
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900219 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200220}
221
Anton Tikhomirov425d7102013-10-03 12:42:04 +0900222static inline int otg_start_host(struct otg_fsm *fsm, int on)
223{
224 if (!fsm->ops->start_host)
225 return -EOPNOTSUPP;
226 return fsm->ops->start_host(fsm, on);
227}
228
229static inline int otg_start_gadget(struct otg_fsm *fsm, int on)
230{
231 if (!fsm->ops->start_gadget)
232 return -EOPNOTSUPP;
233 return fsm->ops->start_gadget(fsm, on);
234}
235
Li Yang0807c502011-04-18 22:01:59 +0200236int otg_statemachine(struct otg_fsm *fsm);