blob: d5c6db0045317f332880709326858e0fff6ce667 [file] [log] [blame]
Li Yang0807c502011-04-18 22:01:59 +02001/*
2 * OTG Finite State Machine from OTG spec
3 *
4 * Copyright (C) 2007,2008 Freescale Semiconductor, Inc.
5 *
6 * Author: Li Yang <LeoLi@freescale.com>
7 * Jerry Huang <Chang-Ming.Huang@freescale.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <linux/kernel.h>
25#include <linux/types.h>
26#include <linux/spinlock.h>
27#include <linux/delay.h>
28#include <linux/usb.h>
29#include <linux/usb/gadget.h>
30#include <linux/usb/otg.h>
Li Yang0807c502011-04-18 22:01:59 +020031
Anatolij Gustschin52d5b9a2013-08-21 17:43:31 +020032#include "phy-fsm-usb.h"
Li Yang0807c502011-04-18 22:01:59 +020033
34/* Change USB protocol when there is a protocol change */
35static int otg_set_protocol(struct otg_fsm *fsm, int protocol)
36{
37 int ret = 0;
38
39 if (fsm->protocol != protocol) {
40 VDBG("Changing role fsm->protocol= %d; new protocol= %d\n",
41 fsm->protocol, protocol);
42 /* stop old protocol */
43 if (fsm->protocol == PROTO_HOST)
Anton Tikhomirov425d7102013-10-03 12:42:04 +090044 ret = otg_start_host(fsm, 0);
Li Yang0807c502011-04-18 22:01:59 +020045 else if (fsm->protocol == PROTO_GADGET)
Anton Tikhomirov425d7102013-10-03 12:42:04 +090046 ret = otg_start_gadget(fsm, 0);
Li Yang0807c502011-04-18 22:01:59 +020047 if (ret)
48 return ret;
49
50 /* start new protocol */
51 if (protocol == PROTO_HOST)
Anton Tikhomirov425d7102013-10-03 12:42:04 +090052 ret = otg_start_host(fsm, 1);
Li Yang0807c502011-04-18 22:01:59 +020053 else if (protocol == PROTO_GADGET)
Anton Tikhomirov425d7102013-10-03 12:42:04 +090054 ret = otg_start_gadget(fsm, 1);
Li Yang0807c502011-04-18 22:01:59 +020055 if (ret)
56 return ret;
57
58 fsm->protocol = protocol;
59 return 0;
60 }
61
62 return 0;
63}
64
65static int state_changed;
66
67/* Called when leaving a state. Do state clean up jobs here */
68void otg_leave_state(struct otg_fsm *fsm, enum usb_otg_state old_state)
69{
70 switch (old_state) {
71 case OTG_STATE_B_IDLE:
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090072 otg_del_timer(fsm, B_SE0_SRP);
Li Yang0807c502011-04-18 22:01:59 +020073 fsm->b_se0_srp = 0;
74 break;
75 case OTG_STATE_B_SRP_INIT:
76 fsm->b_srp_done = 0;
77 break;
78 case OTG_STATE_B_PERIPHERAL:
79 break;
80 case OTG_STATE_B_WAIT_ACON:
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090081 otg_del_timer(fsm, B_ASE0_BRST);
Li Yang0807c502011-04-18 22:01:59 +020082 fsm->b_ase0_brst_tmout = 0;
83 break;
84 case OTG_STATE_B_HOST:
85 break;
86 case OTG_STATE_A_IDLE:
87 break;
88 case OTG_STATE_A_WAIT_VRISE:
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090089 otg_del_timer(fsm, A_WAIT_VRISE);
Li Yang0807c502011-04-18 22:01:59 +020090 fsm->a_wait_vrise_tmout = 0;
91 break;
92 case OTG_STATE_A_WAIT_BCON:
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090093 otg_del_timer(fsm, A_WAIT_BCON);
Li Yang0807c502011-04-18 22:01:59 +020094 fsm->a_wait_bcon_tmout = 0;
95 break;
96 case OTG_STATE_A_HOST:
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090097 otg_del_timer(fsm, A_WAIT_ENUM);
Li Yang0807c502011-04-18 22:01:59 +020098 break;
99 case OTG_STATE_A_SUSPEND:
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900100 otg_del_timer(fsm, A_AIDL_BDIS);
Li Yang0807c502011-04-18 22:01:59 +0200101 fsm->a_aidl_bdis_tmout = 0;
Anton Tikhomirovcff4dab2013-10-03 12:42:04 +0900102 fsm->a_suspend_req_inf = 0;
Li Yang0807c502011-04-18 22:01:59 +0200103 break;
104 case OTG_STATE_A_PERIPHERAL:
Anton Tikhomirov32949082013-10-03 12:42:04 +0900105 otg_del_timer(fsm, A_BIDL_ADIS);
106 fsm->a_bidl_adis_tmout = 0;
Li Yang0807c502011-04-18 22:01:59 +0200107 break;
108 case OTG_STATE_A_WAIT_VFALL:
Anton Tikhomirov32949082013-10-03 12:42:04 +0900109 otg_del_timer(fsm, A_WAIT_VFALL);
110 fsm->a_wait_vfall_tmout = 0;
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900111 otg_del_timer(fsm, A_WAIT_VRISE);
Li Yang0807c502011-04-18 22:01:59 +0200112 break;
113 case OTG_STATE_A_VBUS_ERR:
114 break;
115 default:
116 break;
117 }
118}
119
120/* Called when entering a state */
121int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
122{
123 state_changed = 1;
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200124 if (fsm->otg->phy->state == new_state)
Li Yang0807c502011-04-18 22:01:59 +0200125 return 0;
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200126 VDBG("Set state: %s\n", usb_otg_state_string(new_state));
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200127 otg_leave_state(fsm, fsm->otg->phy->state);
Li Yang0807c502011-04-18 22:01:59 +0200128 switch (new_state) {
129 case OTG_STATE_B_IDLE:
130 otg_drv_vbus(fsm, 0);
131 otg_chrg_vbus(fsm, 0);
132 otg_loc_conn(fsm, 0);
133 otg_loc_sof(fsm, 0);
134 otg_set_protocol(fsm, PROTO_UNDEF);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900135 otg_add_timer(fsm, B_SE0_SRP);
Li Yang0807c502011-04-18 22:01:59 +0200136 break;
137 case OTG_STATE_B_SRP_INIT:
138 otg_start_pulse(fsm);
139 otg_loc_sof(fsm, 0);
140 otg_set_protocol(fsm, PROTO_UNDEF);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900141 otg_add_timer(fsm, B_SRP_FAIL);
Li Yang0807c502011-04-18 22:01:59 +0200142 break;
143 case OTG_STATE_B_PERIPHERAL:
144 otg_chrg_vbus(fsm, 0);
145 otg_loc_conn(fsm, 1);
146 otg_loc_sof(fsm, 0);
147 otg_set_protocol(fsm, PROTO_GADGET);
148 break;
149 case OTG_STATE_B_WAIT_ACON:
150 otg_chrg_vbus(fsm, 0);
151 otg_loc_conn(fsm, 0);
152 otg_loc_sof(fsm, 0);
153 otg_set_protocol(fsm, PROTO_HOST);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900154 otg_add_timer(fsm, B_ASE0_BRST);
Li Yang0807c502011-04-18 22:01:59 +0200155 fsm->a_bus_suspend = 0;
156 break;
157 case OTG_STATE_B_HOST:
158 otg_chrg_vbus(fsm, 0);
159 otg_loc_conn(fsm, 0);
160 otg_loc_sof(fsm, 1);
161 otg_set_protocol(fsm, PROTO_HOST);
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200162 usb_bus_start_enum(fsm->otg->host,
163 fsm->otg->host->otg_port);
Li Yang0807c502011-04-18 22:01:59 +0200164 break;
165 case OTG_STATE_A_IDLE:
166 otg_drv_vbus(fsm, 0);
167 otg_chrg_vbus(fsm, 0);
168 otg_loc_conn(fsm, 0);
169 otg_loc_sof(fsm, 0);
170 otg_set_protocol(fsm, PROTO_HOST);
171 break;
172 case OTG_STATE_A_WAIT_VRISE:
173 otg_drv_vbus(fsm, 1);
174 otg_loc_conn(fsm, 0);
175 otg_loc_sof(fsm, 0);
176 otg_set_protocol(fsm, PROTO_HOST);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900177 otg_add_timer(fsm, A_WAIT_VRISE);
Li Yang0807c502011-04-18 22:01:59 +0200178 break;
179 case OTG_STATE_A_WAIT_BCON:
180 otg_drv_vbus(fsm, 1);
181 otg_loc_conn(fsm, 0);
182 otg_loc_sof(fsm, 0);
183 otg_set_protocol(fsm, PROTO_HOST);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900184 otg_add_timer(fsm, A_WAIT_BCON);
Li Yang0807c502011-04-18 22:01:59 +0200185 break;
186 case OTG_STATE_A_HOST:
187 otg_drv_vbus(fsm, 1);
188 otg_loc_conn(fsm, 0);
189 otg_loc_sof(fsm, 1);
190 otg_set_protocol(fsm, PROTO_HOST);
191 /*
192 * When HNP is triggered while a_bus_req = 0, a_host will
193 * suspend too fast to complete a_set_b_hnp_en
194 */
Anton Tikhomirovcff4dab2013-10-03 12:42:04 +0900195 if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900196 otg_add_timer(fsm, A_WAIT_ENUM);
Li Yang0807c502011-04-18 22:01:59 +0200197 break;
198 case OTG_STATE_A_SUSPEND:
199 otg_drv_vbus(fsm, 1);
200 otg_loc_conn(fsm, 0);
201 otg_loc_sof(fsm, 0);
202 otg_set_protocol(fsm, PROTO_HOST);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900203 otg_add_timer(fsm, A_AIDL_BDIS);
Li Yang0807c502011-04-18 22:01:59 +0200204
205 break;
206 case OTG_STATE_A_PERIPHERAL:
207 otg_loc_conn(fsm, 1);
208 otg_loc_sof(fsm, 0);
209 otg_set_protocol(fsm, PROTO_GADGET);
210 otg_drv_vbus(fsm, 1);
Anton Tikhomirov32949082013-10-03 12:42:04 +0900211 otg_add_timer(fsm, A_BIDL_ADIS);
Li Yang0807c502011-04-18 22:01:59 +0200212 break;
213 case OTG_STATE_A_WAIT_VFALL:
214 otg_drv_vbus(fsm, 0);
215 otg_loc_conn(fsm, 0);
216 otg_loc_sof(fsm, 0);
217 otg_set_protocol(fsm, PROTO_HOST);
Anton Tikhomirov32949082013-10-03 12:42:04 +0900218 otg_add_timer(fsm, A_WAIT_VFALL);
Li Yang0807c502011-04-18 22:01:59 +0200219 break;
220 case OTG_STATE_A_VBUS_ERR:
221 otg_drv_vbus(fsm, 0);
222 otg_loc_conn(fsm, 0);
223 otg_loc_sof(fsm, 0);
224 otg_set_protocol(fsm, PROTO_UNDEF);
225 break;
226 default:
227 break;
228 }
229
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200230 fsm->otg->phy->state = new_state;
Li Yang0807c502011-04-18 22:01:59 +0200231 return 0;
232}
233
234/* State change judgement */
235int otg_statemachine(struct otg_fsm *fsm)
236{
237 enum usb_otg_state state;
238 unsigned long flags;
239
240 spin_lock_irqsave(&fsm->lock, flags);
241
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200242 state = fsm->otg->phy->state;
Li Yang0807c502011-04-18 22:01:59 +0200243 state_changed = 0;
244 /* State machine state change judgement */
245
246 switch (state) {
247 case OTG_STATE_UNDEFINED:
248 VDBG("fsm->id = %d\n", fsm->id);
249 if (fsm->id)
250 otg_set_state(fsm, OTG_STATE_B_IDLE);
251 else
252 otg_set_state(fsm, OTG_STATE_A_IDLE);
253 break;
254 case OTG_STATE_B_IDLE:
255 if (!fsm->id)
256 otg_set_state(fsm, OTG_STATE_A_IDLE);
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200257 else if (fsm->b_sess_vld && fsm->otg->gadget)
Li Yang0807c502011-04-18 22:01:59 +0200258 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
Anton Tikhomirov68041782013-10-03 12:42:04 +0900259 else if (fsm->b_bus_req && fsm->b_ssend_srp && fsm->b_se0_srp)
Li Yang0807c502011-04-18 22:01:59 +0200260 otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
261 break;
262 case OTG_STATE_B_SRP_INIT:
263 if (!fsm->id || fsm->b_srp_done)
264 otg_set_state(fsm, OTG_STATE_B_IDLE);
265 break;
266 case OTG_STATE_B_PERIPHERAL:
267 if (!fsm->id || !fsm->b_sess_vld)
268 otg_set_state(fsm, OTG_STATE_B_IDLE);
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200269 else if (fsm->b_bus_req && fsm->otg->
Li Yang0807c502011-04-18 22:01:59 +0200270 gadget->b_hnp_enable && fsm->a_bus_suspend)
271 otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
272 break;
273 case OTG_STATE_B_WAIT_ACON:
274 if (fsm->a_conn)
275 otg_set_state(fsm, OTG_STATE_B_HOST);
276 else if (!fsm->id || !fsm->b_sess_vld)
277 otg_set_state(fsm, OTG_STATE_B_IDLE);
278 else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
279 fsm->b_ase0_brst_tmout = 0;
280 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
281 }
282 break;
283 case OTG_STATE_B_HOST:
284 if (!fsm->id || !fsm->b_sess_vld)
285 otg_set_state(fsm, OTG_STATE_B_IDLE);
286 else if (!fsm->b_bus_req || !fsm->a_conn)
287 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
288 break;
289 case OTG_STATE_A_IDLE:
290 if (fsm->id)
291 otg_set_state(fsm, OTG_STATE_B_IDLE);
292 else if (!fsm->a_bus_drop && (fsm->a_bus_req || fsm->a_srp_det))
293 otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
294 break;
295 case OTG_STATE_A_WAIT_VRISE:
296 if (fsm->id || fsm->a_bus_drop || fsm->a_vbus_vld ||
297 fsm->a_wait_vrise_tmout) {
298 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
299 }
300 break;
301 case OTG_STATE_A_WAIT_BCON:
302 if (!fsm->a_vbus_vld)
303 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
304 else if (fsm->b_conn)
305 otg_set_state(fsm, OTG_STATE_A_HOST);
306 else if (fsm->id | fsm->a_bus_drop | fsm->a_wait_bcon_tmout)
307 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
308 break;
309 case OTG_STATE_A_HOST:
Anton Tikhomirovcff4dab2013-10-03 12:42:04 +0900310 if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200311 fsm->otg->host->b_hnp_enable)
Li Yang0807c502011-04-18 22:01:59 +0200312 otg_set_state(fsm, OTG_STATE_A_SUSPEND);
313 else if (fsm->id || !fsm->b_conn || fsm->a_bus_drop)
314 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
315 else if (!fsm->a_vbus_vld)
316 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
317 break;
318 case OTG_STATE_A_SUSPEND:
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200319 if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
Li Yang0807c502011-04-18 22:01:59 +0200320 otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200321 else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
Li Yang0807c502011-04-18 22:01:59 +0200322 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
323 else if (fsm->a_bus_req || fsm->b_bus_resume)
324 otg_set_state(fsm, OTG_STATE_A_HOST);
325 else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
326 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
327 else if (!fsm->a_vbus_vld)
328 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
329 break;
330 case OTG_STATE_A_PERIPHERAL:
331 if (fsm->id || fsm->a_bus_drop)
332 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
Anton Tikhomirov32949082013-10-03 12:42:04 +0900333 else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
Li Yang0807c502011-04-18 22:01:59 +0200334 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
335 else if (!fsm->a_vbus_vld)
336 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
337 break;
338 case OTG_STATE_A_WAIT_VFALL:
Anton Tikhomirov32949082013-10-03 12:42:04 +0900339 if (fsm->a_wait_vfall_tmout || fsm->id || fsm->a_bus_req ||
340 (!fsm->a_sess_vld && !fsm->b_conn))
Li Yang0807c502011-04-18 22:01:59 +0200341 otg_set_state(fsm, OTG_STATE_A_IDLE);
342 break;
343 case OTG_STATE_A_VBUS_ERR:
344 if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
345 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
346 break;
347 default:
348 break;
349 }
350 spin_unlock_irqrestore(&fsm->lock, flags);
351
352 VDBG("quit statemachine, changed = %d\n", state_changed);
353 return state_changed;
354}