blob: 329c2d2f85959963c7828d3d9332315062480623 [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;
Anton Tikhomirovec049962013-10-03 12:42:04 +090074 fsm->adp_sns = 0;
75 fsm->adp_prb = 0;
Li Yang0807c502011-04-18 22:01:59 +020076 break;
77 case OTG_STATE_B_SRP_INIT:
Anton Tikhomirovec049962013-10-03 12:42:04 +090078 fsm->data_pulse = 0;
Li Yang0807c502011-04-18 22:01:59 +020079 fsm->b_srp_done = 0;
80 break;
81 case OTG_STATE_B_PERIPHERAL:
82 break;
83 case OTG_STATE_B_WAIT_ACON:
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090084 otg_del_timer(fsm, B_ASE0_BRST);
Li Yang0807c502011-04-18 22:01:59 +020085 fsm->b_ase0_brst_tmout = 0;
86 break;
87 case OTG_STATE_B_HOST:
88 break;
89 case OTG_STATE_A_IDLE:
Anton Tikhomirovec049962013-10-03 12:42:04 +090090 fsm->adp_prb = 0;
Li Yang0807c502011-04-18 22:01:59 +020091 break;
92 case OTG_STATE_A_WAIT_VRISE:
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090093 otg_del_timer(fsm, A_WAIT_VRISE);
Li Yang0807c502011-04-18 22:01:59 +020094 fsm->a_wait_vrise_tmout = 0;
95 break;
96 case OTG_STATE_A_WAIT_BCON:
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090097 otg_del_timer(fsm, A_WAIT_BCON);
Li Yang0807c502011-04-18 22:01:59 +020098 fsm->a_wait_bcon_tmout = 0;
99 break;
100 case OTG_STATE_A_HOST:
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900101 otg_del_timer(fsm, A_WAIT_ENUM);
Li Yang0807c502011-04-18 22:01:59 +0200102 break;
103 case OTG_STATE_A_SUSPEND:
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900104 otg_del_timer(fsm, A_AIDL_BDIS);
Li Yang0807c502011-04-18 22:01:59 +0200105 fsm->a_aidl_bdis_tmout = 0;
Anton Tikhomirovcff4dab2013-10-03 12:42:04 +0900106 fsm->a_suspend_req_inf = 0;
Li Yang0807c502011-04-18 22:01:59 +0200107 break;
108 case OTG_STATE_A_PERIPHERAL:
Anton Tikhomirov32949082013-10-03 12:42:04 +0900109 otg_del_timer(fsm, A_BIDL_ADIS);
110 fsm->a_bidl_adis_tmout = 0;
Li Yang0807c502011-04-18 22:01:59 +0200111 break;
112 case OTG_STATE_A_WAIT_VFALL:
Anton Tikhomirov32949082013-10-03 12:42:04 +0900113 otg_del_timer(fsm, A_WAIT_VFALL);
114 fsm->a_wait_vfall_tmout = 0;
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900115 otg_del_timer(fsm, A_WAIT_VRISE);
Li Yang0807c502011-04-18 22:01:59 +0200116 break;
117 case OTG_STATE_A_VBUS_ERR:
118 break;
119 default:
120 break;
121 }
122}
123
124/* Called when entering a state */
125int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
126{
127 state_changed = 1;
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200128 if (fsm->otg->phy->state == new_state)
Li Yang0807c502011-04-18 22:01:59 +0200129 return 0;
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200130 VDBG("Set state: %s\n", usb_otg_state_string(new_state));
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200131 otg_leave_state(fsm, fsm->otg->phy->state);
Li Yang0807c502011-04-18 22:01:59 +0200132 switch (new_state) {
133 case OTG_STATE_B_IDLE:
134 otg_drv_vbus(fsm, 0);
135 otg_chrg_vbus(fsm, 0);
136 otg_loc_conn(fsm, 0);
137 otg_loc_sof(fsm, 0);
Anton Tikhomirovec049962013-10-03 12:42:04 +0900138 /*
139 * Driver is responsible for starting ADP probing
140 * if ADP sensing times out.
141 */
142 otg_start_adp_sns(fsm);
Li Yang0807c502011-04-18 22:01:59 +0200143 otg_set_protocol(fsm, PROTO_UNDEF);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900144 otg_add_timer(fsm, B_SE0_SRP);
Li Yang0807c502011-04-18 22:01:59 +0200145 break;
146 case OTG_STATE_B_SRP_INIT:
147 otg_start_pulse(fsm);
148 otg_loc_sof(fsm, 0);
149 otg_set_protocol(fsm, PROTO_UNDEF);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900150 otg_add_timer(fsm, B_SRP_FAIL);
Li Yang0807c502011-04-18 22:01:59 +0200151 break;
152 case OTG_STATE_B_PERIPHERAL:
153 otg_chrg_vbus(fsm, 0);
154 otg_loc_conn(fsm, 1);
155 otg_loc_sof(fsm, 0);
156 otg_set_protocol(fsm, PROTO_GADGET);
157 break;
158 case OTG_STATE_B_WAIT_ACON:
159 otg_chrg_vbus(fsm, 0);
160 otg_loc_conn(fsm, 0);
161 otg_loc_sof(fsm, 0);
162 otg_set_protocol(fsm, PROTO_HOST);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900163 otg_add_timer(fsm, B_ASE0_BRST);
Li Yang0807c502011-04-18 22:01:59 +0200164 fsm->a_bus_suspend = 0;
165 break;
166 case OTG_STATE_B_HOST:
167 otg_chrg_vbus(fsm, 0);
168 otg_loc_conn(fsm, 0);
169 otg_loc_sof(fsm, 1);
170 otg_set_protocol(fsm, PROTO_HOST);
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200171 usb_bus_start_enum(fsm->otg->host,
172 fsm->otg->host->otg_port);
Li Yang0807c502011-04-18 22:01:59 +0200173 break;
174 case OTG_STATE_A_IDLE:
175 otg_drv_vbus(fsm, 0);
176 otg_chrg_vbus(fsm, 0);
177 otg_loc_conn(fsm, 0);
178 otg_loc_sof(fsm, 0);
Anton Tikhomirovec049962013-10-03 12:42:04 +0900179 otg_start_adp_prb(fsm);
Li Yang0807c502011-04-18 22:01:59 +0200180 otg_set_protocol(fsm, PROTO_HOST);
181 break;
182 case OTG_STATE_A_WAIT_VRISE:
183 otg_drv_vbus(fsm, 1);
184 otg_loc_conn(fsm, 0);
185 otg_loc_sof(fsm, 0);
186 otg_set_protocol(fsm, PROTO_HOST);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900187 otg_add_timer(fsm, A_WAIT_VRISE);
Li Yang0807c502011-04-18 22:01:59 +0200188 break;
189 case OTG_STATE_A_WAIT_BCON:
190 otg_drv_vbus(fsm, 1);
191 otg_loc_conn(fsm, 0);
192 otg_loc_sof(fsm, 0);
193 otg_set_protocol(fsm, PROTO_HOST);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900194 otg_add_timer(fsm, A_WAIT_BCON);
Li Yang0807c502011-04-18 22:01:59 +0200195 break;
196 case OTG_STATE_A_HOST:
197 otg_drv_vbus(fsm, 1);
198 otg_loc_conn(fsm, 0);
199 otg_loc_sof(fsm, 1);
200 otg_set_protocol(fsm, PROTO_HOST);
201 /*
202 * When HNP is triggered while a_bus_req = 0, a_host will
203 * suspend too fast to complete a_set_b_hnp_en
204 */
Anton Tikhomirovcff4dab2013-10-03 12:42:04 +0900205 if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900206 otg_add_timer(fsm, A_WAIT_ENUM);
Li Yang0807c502011-04-18 22:01:59 +0200207 break;
208 case OTG_STATE_A_SUSPEND:
209 otg_drv_vbus(fsm, 1);
210 otg_loc_conn(fsm, 0);
211 otg_loc_sof(fsm, 0);
212 otg_set_protocol(fsm, PROTO_HOST);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900213 otg_add_timer(fsm, A_AIDL_BDIS);
Li Yang0807c502011-04-18 22:01:59 +0200214
215 break;
216 case OTG_STATE_A_PERIPHERAL:
217 otg_loc_conn(fsm, 1);
218 otg_loc_sof(fsm, 0);
219 otg_set_protocol(fsm, PROTO_GADGET);
220 otg_drv_vbus(fsm, 1);
Anton Tikhomirov32949082013-10-03 12:42:04 +0900221 otg_add_timer(fsm, A_BIDL_ADIS);
Li Yang0807c502011-04-18 22:01:59 +0200222 break;
223 case OTG_STATE_A_WAIT_VFALL:
224 otg_drv_vbus(fsm, 0);
225 otg_loc_conn(fsm, 0);
226 otg_loc_sof(fsm, 0);
227 otg_set_protocol(fsm, PROTO_HOST);
Anton Tikhomirov32949082013-10-03 12:42:04 +0900228 otg_add_timer(fsm, A_WAIT_VFALL);
Li Yang0807c502011-04-18 22:01:59 +0200229 break;
230 case OTG_STATE_A_VBUS_ERR:
231 otg_drv_vbus(fsm, 0);
232 otg_loc_conn(fsm, 0);
233 otg_loc_sof(fsm, 0);
234 otg_set_protocol(fsm, PROTO_UNDEF);
235 break;
236 default:
237 break;
238 }
239
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200240 fsm->otg->phy->state = new_state;
Li Yang0807c502011-04-18 22:01:59 +0200241 return 0;
242}
243
244/* State change judgement */
245int otg_statemachine(struct otg_fsm *fsm)
246{
247 enum usb_otg_state state;
248 unsigned long flags;
249
250 spin_lock_irqsave(&fsm->lock, flags);
251
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200252 state = fsm->otg->phy->state;
Li Yang0807c502011-04-18 22:01:59 +0200253 state_changed = 0;
254 /* State machine state change judgement */
255
256 switch (state) {
257 case OTG_STATE_UNDEFINED:
258 VDBG("fsm->id = %d\n", fsm->id);
259 if (fsm->id)
260 otg_set_state(fsm, OTG_STATE_B_IDLE);
261 else
262 otg_set_state(fsm, OTG_STATE_A_IDLE);
263 break;
264 case OTG_STATE_B_IDLE:
265 if (!fsm->id)
266 otg_set_state(fsm, OTG_STATE_A_IDLE);
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200267 else if (fsm->b_sess_vld && fsm->otg->gadget)
Li Yang0807c502011-04-18 22:01:59 +0200268 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
Anton Tikhomirovec049962013-10-03 12:42:04 +0900269 else if ((fsm->b_bus_req || fsm->adp_change || fsm->power_up) &&
270 fsm->b_ssend_srp && fsm->b_se0_srp)
Li Yang0807c502011-04-18 22:01:59 +0200271 otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
272 break;
273 case OTG_STATE_B_SRP_INIT:
274 if (!fsm->id || fsm->b_srp_done)
275 otg_set_state(fsm, OTG_STATE_B_IDLE);
276 break;
277 case OTG_STATE_B_PERIPHERAL:
278 if (!fsm->id || !fsm->b_sess_vld)
279 otg_set_state(fsm, OTG_STATE_B_IDLE);
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200280 else if (fsm->b_bus_req && fsm->otg->
Li Yang0807c502011-04-18 22:01:59 +0200281 gadget->b_hnp_enable && fsm->a_bus_suspend)
282 otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
283 break;
284 case OTG_STATE_B_WAIT_ACON:
285 if (fsm->a_conn)
286 otg_set_state(fsm, OTG_STATE_B_HOST);
287 else if (!fsm->id || !fsm->b_sess_vld)
288 otg_set_state(fsm, OTG_STATE_B_IDLE);
289 else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
290 fsm->b_ase0_brst_tmout = 0;
291 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
292 }
293 break;
294 case OTG_STATE_B_HOST:
295 if (!fsm->id || !fsm->b_sess_vld)
296 otg_set_state(fsm, OTG_STATE_B_IDLE);
Anton Tikhomirovec049962013-10-03 12:42:04 +0900297 else if (!fsm->b_bus_req || !fsm->a_conn || fsm->test_device)
Li Yang0807c502011-04-18 22:01:59 +0200298 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
299 break;
300 case OTG_STATE_A_IDLE:
301 if (fsm->id)
302 otg_set_state(fsm, OTG_STATE_B_IDLE);
Anton Tikhomirovec049962013-10-03 12:42:04 +0900303 else if (!fsm->a_bus_drop && (fsm->a_bus_req ||
304 fsm->a_srp_det || fsm->adp_change || fsm->power_up))
Li Yang0807c502011-04-18 22:01:59 +0200305 otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
306 break;
307 case OTG_STATE_A_WAIT_VRISE:
308 if (fsm->id || fsm->a_bus_drop || fsm->a_vbus_vld ||
309 fsm->a_wait_vrise_tmout) {
310 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
311 }
312 break;
313 case OTG_STATE_A_WAIT_BCON:
314 if (!fsm->a_vbus_vld)
315 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
316 else if (fsm->b_conn)
317 otg_set_state(fsm, OTG_STATE_A_HOST);
318 else if (fsm->id | fsm->a_bus_drop | fsm->a_wait_bcon_tmout)
319 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
320 break;
321 case OTG_STATE_A_HOST:
Anton Tikhomirovcff4dab2013-10-03 12:42:04 +0900322 if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200323 fsm->otg->host->b_hnp_enable)
Li Yang0807c502011-04-18 22:01:59 +0200324 otg_set_state(fsm, OTG_STATE_A_SUSPEND);
325 else if (fsm->id || !fsm->b_conn || fsm->a_bus_drop)
326 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
327 else if (!fsm->a_vbus_vld)
328 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
329 break;
330 case OTG_STATE_A_SUSPEND:
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200331 if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
Li Yang0807c502011-04-18 22:01:59 +0200332 otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200333 else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
Li Yang0807c502011-04-18 22:01:59 +0200334 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
335 else if (fsm->a_bus_req || fsm->b_bus_resume)
336 otg_set_state(fsm, OTG_STATE_A_HOST);
337 else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
338 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
339 else if (!fsm->a_vbus_vld)
340 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
341 break;
342 case OTG_STATE_A_PERIPHERAL:
343 if (fsm->id || fsm->a_bus_drop)
344 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
Anton Tikhomirov32949082013-10-03 12:42:04 +0900345 else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
Li Yang0807c502011-04-18 22:01:59 +0200346 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
347 else if (!fsm->a_vbus_vld)
348 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
349 break;
350 case OTG_STATE_A_WAIT_VFALL:
Anton Tikhomirov32949082013-10-03 12:42:04 +0900351 if (fsm->a_wait_vfall_tmout || fsm->id || fsm->a_bus_req ||
352 (!fsm->a_sess_vld && !fsm->b_conn))
Li Yang0807c502011-04-18 22:01:59 +0200353 otg_set_state(fsm, OTG_STATE_A_IDLE);
354 break;
355 case OTG_STATE_A_VBUS_ERR:
356 if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
357 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
358 break;
359 default:
360 break;
361 }
362 spin_unlock_irqrestore(&fsm->lock, flags);
363
364 VDBG("quit statemachine, changed = %d\n", state_changed);
365 return state_changed;
366}