2 * otg_fsm.c - ChipIdea USB IP core OTG FSM driver
4 * Copyright (C) 2014 Freescale Semiconductor, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 * This file mainly handles OTG fsm, it includes OTG fsm operations
18 #include <linux/usb/otg.h>
19 #include <linux/usb/gadget.h>
20 #include <linux/usb/hcd.h>
21 #include <linux/usb/chipidea.h>
22 #include <linux/regulator/consumer.h>
29 static struct ci_otg_fsm_timer *otg_timer_initializer
30 (struct ci_hdrc *ci, void (*function)(void *, unsigned long),
31 unsigned long expires, unsigned long data)
33 struct ci_otg_fsm_timer *timer;
35 timer = devm_kzalloc(ci->dev, sizeof(struct ci_otg_fsm_timer),
39 timer->function = function;
40 timer->expires = expires;
46 * Add timer to active timer list
48 static void ci_otg_add_timer(struct ci_hdrc *ci, enum ci_otg_fsm_timer_index t)
50 struct ci_otg_fsm_timer *tmp_timer;
51 struct ci_otg_fsm_timer *timer = ci->fsm_timer->timer_list[t];
52 struct list_head *active_timers = &ci->fsm_timer->active_timers;
54 if (t >= NUM_CI_OTG_FSM_TIMERS)
58 * Check if the timer is already in the active list,
59 * if so update timer count
61 list_for_each_entry(tmp_timer, active_timers, list)
62 if (tmp_timer == timer) {
63 timer->count = timer->expires;
67 timer->count = timer->expires;
68 list_add_tail(&timer->list, active_timers);
71 if (!(hw_read_otgsc(ci, OTGSC_1MSIE)))
72 hw_write_otgsc(ci, OTGSC_1MSIE, OTGSC_1MSIE);
76 * Remove timer from active timer list
78 static void ci_otg_del_timer(struct ci_hdrc *ci, enum ci_otg_fsm_timer_index t)
80 struct ci_otg_fsm_timer *tmp_timer, *del_tmp;
81 struct ci_otg_fsm_timer *timer = ci->fsm_timer->timer_list[t];
82 struct list_head *active_timers = &ci->fsm_timer->active_timers;
84 if (t >= NUM_CI_OTG_FSM_TIMERS)
87 list_for_each_entry_safe(tmp_timer, del_tmp, active_timers, list)
88 if (tmp_timer == timer)
89 list_del(&timer->list);
91 /* Disable 1ms irq if there is no any active timer */
92 if (list_empty(active_timers))
93 hw_write_otgsc(ci, OTGSC_1MSIE, 0);
96 /* The timeout callback function to set time out bit */
97 static void set_tmout(void *ptr, unsigned long indicator)
99 *(int *)indicator = 1;
102 static void set_tmout_and_fsm(void *ptr, unsigned long indicator)
104 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
106 set_tmout(ci, indicator);
108 disable_irq_nosync(ci->irq);
109 queue_work(ci->wq, &ci->work);
112 static void a_wait_vfall_tmout_func(void *ptr, unsigned long indicator)
114 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
116 set_tmout(ci, indicator);
117 /* Disable port power */
118 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP, 0);
119 /* Clear exsiting DP irq */
120 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
121 /* Enable data pulse irq */
122 hw_write_otgsc(ci, OTGSC_DPIE, OTGSC_DPIE);
123 disable_irq_nosync(ci->irq);
124 queue_work(ci->wq, &ci->work);
127 static void b_ase0_brst_tmout_func(void *ptr, unsigned long indicator)
129 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
131 set_tmout(ci, indicator);
132 if (!hw_read_otgsc(ci, OTGSC_BSV))
133 ci->fsm.b_sess_vld = 0;
135 disable_irq_nosync(ci->irq);
136 queue_work(ci->wq, &ci->work);
139 static void b_ssend_srp_tmout_func(void *ptr, unsigned long indicator)
141 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
143 set_tmout(ci, indicator);
145 /* only vbus fall below B_sess_vld in b_idle state */
146 if (ci->transceiver->state == OTG_STATE_B_IDLE) {
147 disable_irq_nosync(ci->irq);
148 queue_work(ci->wq, &ci->work);
152 static void b_sess_vld_tmout_func(void *ptr, unsigned long indicator)
154 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
156 /* Check if A detached */
157 if (!(hw_read_otgsc(ci, OTGSC_BSV))) {
158 ci->fsm.b_sess_vld = 0;
159 ci_otg_add_timer(ci, B_SSEND_SRP);
160 disable_irq_nosync(ci->irq);
161 queue_work(ci->wq, &ci->work);
165 static void b_data_pulse_end(void *ptr, unsigned long indicator)
167 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
169 ci->fsm.b_srp_done = 1;
170 ci->fsm.b_bus_req = 0;
171 if (ci->fsm.power_up)
172 ci->fsm.power_up = 0;
174 hw_write_otgsc(ci, OTGSC_HABA, 0);
176 disable_irq_nosync(ci->irq);
177 queue_work(ci->wq, &ci->work);
180 /* Initialize timers */
181 static int ci_otg_init_timers(struct ci_hdrc *ci)
183 struct otg_fsm *fsm = &ci->fsm;
185 /* FSM used timers */
186 ci->fsm_timer->timer_list[A_WAIT_VRISE] =
187 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_WAIT_VRISE,
188 (unsigned long)&fsm->a_wait_vrise_tmout);
189 if (ci->fsm_timer->timer_list[A_WAIT_VRISE] == NULL)
192 ci->fsm_timer->timer_list[A_WAIT_VFALL] =
193 otg_timer_initializer(ci, &a_wait_vfall_tmout_func,
194 TA_WAIT_VFALL, (unsigned long)&fsm->a_wait_vfall_tmout);
195 if (ci->fsm_timer->timer_list[A_WAIT_VFALL] == NULL)
198 ci->fsm_timer->timer_list[A_WAIT_BCON] =
199 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_WAIT_BCON,
200 (unsigned long)&fsm->a_wait_bcon_tmout);
201 if (ci->fsm_timer->timer_list[A_WAIT_BCON] == NULL)
204 ci->fsm_timer->timer_list[A_AIDL_BDIS] =
205 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_AIDL_BDIS,
206 (unsigned long)&fsm->a_aidl_bdis_tmout);
207 if (ci->fsm_timer->timer_list[A_AIDL_BDIS] == NULL)
210 ci->fsm_timer->timer_list[A_BIDL_ADIS] =
211 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_BIDL_ADIS,
212 (unsigned long)&fsm->a_bidl_adis_tmout);
213 if (ci->fsm_timer->timer_list[A_BIDL_ADIS] == NULL)
216 ci->fsm_timer->timer_list[B_ASE0_BRST] =
217 otg_timer_initializer(ci, &b_ase0_brst_tmout_func, TB_ASE0_BRST,
218 (unsigned long)&fsm->b_ase0_brst_tmout);
219 if (ci->fsm_timer->timer_list[B_ASE0_BRST] == NULL)
222 ci->fsm_timer->timer_list[B_SE0_SRP] =
223 otg_timer_initializer(ci, &set_tmout_and_fsm, TB_SE0_SRP,
224 (unsigned long)&fsm->b_se0_srp);
225 if (ci->fsm_timer->timer_list[B_SE0_SRP] == NULL)
228 ci->fsm_timer->timer_list[B_SSEND_SRP] =
229 otg_timer_initializer(ci, &b_ssend_srp_tmout_func, TB_SSEND_SRP,
230 (unsigned long)&fsm->b_ssend_srp);
231 if (ci->fsm_timer->timer_list[B_SSEND_SRP] == NULL)
234 ci->fsm_timer->timer_list[B_SRP_FAIL] =
235 otg_timer_initializer(ci, &set_tmout, TB_SRP_FAIL,
236 (unsigned long)&fsm->b_srp_done);
237 if (ci->fsm_timer->timer_list[B_SRP_FAIL] == NULL)
240 ci->fsm_timer->timer_list[B_DATA_PLS] =
241 otg_timer_initializer(ci, &b_data_pulse_end, TB_DATA_PLS, 0);
242 if (ci->fsm_timer->timer_list[B_DATA_PLS] == NULL)
245 ci->fsm_timer->timer_list[B_SESS_VLD] = otg_timer_initializer(ci,
246 &b_sess_vld_tmout_func, TB_SESS_VLD, 0);
247 if (ci->fsm_timer->timer_list[B_SESS_VLD] == NULL)
253 /* -------------------------------------------------------------*/
254 /* Operations that will be called from OTG Finite State Machine */
255 /* -------------------------------------------------------------*/
256 static void ci_otg_fsm_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
258 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
260 if (t < NUM_OTG_FSM_TIMERS)
261 ci_otg_add_timer(ci, t);
265 static void ci_otg_fsm_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
267 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
269 if (t < NUM_OTG_FSM_TIMERS)
270 ci_otg_del_timer(ci, t);
275 * A-device drive vbus: turn on vbus regulator and enable port power
276 * Data pulse irq should be disabled while vbus is on.
278 static void ci_otg_drv_vbus(struct otg_fsm *fsm, int on)
281 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
284 /* Enable power power */
285 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
287 if (ci->platdata->reg_vbus) {
288 ret = regulator_enable(ci->platdata->reg_vbus);
291 "Failed to enable vbus regulator, ret=%d\n",
296 /* Disable data pulse irq */
297 hw_write_otgsc(ci, OTGSC_DPIE, 0);
302 if (ci->platdata->reg_vbus)
303 regulator_disable(ci->platdata->reg_vbus);
311 * Control data line by Run Stop bit.
313 static void ci_otg_loc_conn(struct otg_fsm *fsm, int on)
315 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
318 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
320 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
324 * Generate SOF by host.
325 * This is controlled through suspend/resume the port.
326 * In host mode, controller will automatically send SOF.
327 * Suspend will block the data on the port.
329 static void ci_otg_loc_sof(struct otg_fsm *fsm, int on)
331 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
334 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_FPR,
337 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_SUSP,
342 * Start SRP pulsing by data-line pulsing,
343 * no v-bus pulsing followed
345 static void ci_otg_start_pulse(struct otg_fsm *fsm)
347 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
349 /* Hardware Assistant Data pulse */
350 hw_write_otgsc(ci, OTGSC_HADP, OTGSC_HADP);
352 ci_otg_add_timer(ci, B_DATA_PLS);
355 static int ci_otg_start_host(struct otg_fsm *fsm, int on)
357 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
359 mutex_unlock(&fsm->lock);
362 ci_role_start(ci, CI_ROLE_HOST);
365 hw_device_reset(ci, USBMODE_CM_DC);
366 ci_role_start(ci, CI_ROLE_GADGET);
368 mutex_lock(&fsm->lock);
372 static int ci_otg_start_gadget(struct otg_fsm *fsm, int on)
374 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
376 mutex_unlock(&fsm->lock);
378 usb_gadget_vbus_connect(&ci->gadget);
380 usb_gadget_vbus_disconnect(&ci->gadget);
381 mutex_lock(&fsm->lock);
386 static struct otg_fsm_ops ci_otg_ops = {
387 .drv_vbus = ci_otg_drv_vbus,
388 .loc_conn = ci_otg_loc_conn,
389 .loc_sof = ci_otg_loc_sof,
390 .start_pulse = ci_otg_start_pulse,
391 .add_timer = ci_otg_fsm_add_timer,
392 .del_timer = ci_otg_fsm_del_timer,
393 .start_host = ci_otg_start_host,
394 .start_gadget = ci_otg_start_gadget,
397 int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci)
402 otg = devm_kzalloc(ci->dev,
403 sizeof(struct usb_otg), GFP_KERNEL);
406 "Failed to allocate usb_otg structure for ci hdrc otg!\n");
410 otg->phy = ci->transceiver;
411 otg->gadget = &ci->gadget;
413 ci->transceiver->otg = ci->fsm.otg;
414 ci->fsm.power_up = 1;
415 ci->fsm.id = hw_read_otgsc(ci, OTGSC_ID) ? 1 : 0;
416 ci->transceiver->state = OTG_STATE_UNDEFINED;
417 ci->fsm.ops = &ci_otg_ops;
419 mutex_init(&ci->fsm.lock);
421 ci->fsm_timer = devm_kzalloc(ci->dev,
422 sizeof(struct ci_otg_fsm_timer_list), GFP_KERNEL);
423 if (!ci->fsm_timer) {
425 "Failed to allocate timer structure for ci hdrc otg!\n");
429 INIT_LIST_HEAD(&ci->fsm_timer->active_timers);
430 retval = ci_otg_init_timers(ci);
432 dev_err(ci->dev, "Couldn't init OTG timers\n");
436 /* Enable A vbus valid irq */
437 hw_write_otgsc(ci, OTGSC_AVVIE, OTGSC_AVVIE);
440 ci->fsm.b_ssend_srp =
441 hw_read_otgsc(ci, OTGSC_BSV) ? 0 : 1;
443 hw_read_otgsc(ci, OTGSC_BSV) ? 1 : 0;
445 hw_write_otgsc(ci, OTGSC_BSVIE, OTGSC_BSVIE);