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
22 #include <linux/usb/otg.h>
23 #include <linux/usb/gadget.h>
24 #include <linux/usb/hcd.h>
25 #include <linux/usb/chipidea.h>
26 #include <linux/regulator/consumer.h>
33 static struct ci_otg_fsm_timer *otg_timer_initializer
34 (struct ci_hdrc *ci, void (*function)(void *, unsigned long),
35 unsigned long expires, unsigned long data)
37 struct ci_otg_fsm_timer *timer;
39 timer = devm_kzalloc(ci->dev, sizeof(struct ci_otg_fsm_timer),
43 timer->function = function;
44 timer->expires = expires;
49 /* Add for otg: interact with user space app */
51 get_a_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
55 struct ci_hdrc *ci = dev_get_drvdata(dev);
59 t = scnprintf(next, size, "%d\n", ci->fsm.a_bus_req);
63 return PAGE_SIZE - size;
67 set_a_bus_req(struct device *dev, struct device_attribute *attr,
68 const char *buf, size_t count)
70 struct ci_hdrc *ci = dev_get_drvdata(dev);
75 mutex_lock(&ci->fsm.lock);
77 ci->fsm.a_bus_req = 0;
78 } else if (buf[0] == '1') {
79 /* If a_bus_drop is TRUE, a_bus_req can't be set */
80 if (ci->fsm.a_bus_drop) {
81 mutex_unlock(&ci->fsm.lock);
84 ci->fsm.a_bus_req = 1;
87 ci_otg_queue_work(ci);
88 mutex_unlock(&ci->fsm.lock);
92 static DEVICE_ATTR(a_bus_req, S_IRUGO | S_IWUSR, get_a_bus_req, set_a_bus_req);
95 get_a_bus_drop(struct device *dev, struct device_attribute *attr, char *buf)
99 struct ci_hdrc *ci = dev_get_drvdata(dev);
103 t = scnprintf(next, size, "%d\n", ci->fsm.a_bus_drop);
107 return PAGE_SIZE - size;
111 set_a_bus_drop(struct device *dev, struct device_attribute *attr,
112 const char *buf, size_t count)
114 struct ci_hdrc *ci = dev_get_drvdata(dev);
119 mutex_lock(&ci->fsm.lock);
121 ci->fsm.a_bus_drop = 0;
122 } else if (buf[0] == '1') {
123 ci->fsm.a_bus_drop = 1;
124 ci->fsm.a_bus_req = 0;
127 ci_otg_queue_work(ci);
128 mutex_unlock(&ci->fsm.lock);
132 static DEVICE_ATTR(a_bus_drop, S_IRUGO | S_IWUSR, get_a_bus_drop,
136 get_b_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
140 struct ci_hdrc *ci = dev_get_drvdata(dev);
144 t = scnprintf(next, size, "%d\n", ci->fsm.b_bus_req);
148 return PAGE_SIZE - size;
152 set_b_bus_req(struct device *dev, struct device_attribute *attr,
153 const char *buf, size_t count)
155 struct ci_hdrc *ci = dev_get_drvdata(dev);
160 mutex_lock(&ci->fsm.lock);
162 ci->fsm.b_bus_req = 0;
163 else if (buf[0] == '1')
164 ci->fsm.b_bus_req = 1;
166 ci_otg_queue_work(ci);
167 mutex_unlock(&ci->fsm.lock);
171 static DEVICE_ATTR(b_bus_req, S_IRUGO | S_IWUSR, get_b_bus_req, set_b_bus_req);
174 set_a_clr_err(struct device *dev, struct device_attribute *attr,
175 const char *buf, size_t count)
177 struct ci_hdrc *ci = dev_get_drvdata(dev);
182 mutex_lock(&ci->fsm.lock);
184 ci->fsm.a_clr_err = 1;
186 ci_otg_queue_work(ci);
187 mutex_unlock(&ci->fsm.lock);
191 static DEVICE_ATTR(a_clr_err, S_IWUSR, NULL, set_a_clr_err);
193 static struct attribute *inputs_attrs[] = {
194 &dev_attr_a_bus_req.attr,
195 &dev_attr_a_bus_drop.attr,
196 &dev_attr_b_bus_req.attr,
197 &dev_attr_a_clr_err.attr,
201 static struct attribute_group inputs_attr_group = {
203 .attrs = inputs_attrs,
207 * Add timer to active timer list
209 static void ci_otg_add_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
211 struct ci_otg_fsm_timer *tmp_timer;
212 struct ci_otg_fsm_timer *timer = ci->fsm_timer->timer_list[t];
213 struct list_head *active_timers = &ci->fsm_timer->active_timers;
215 if (t >= NUM_OTG_FSM_TIMERS)
219 * Check if the timer is already in the active list,
220 * if so update timer count
222 list_for_each_entry(tmp_timer, active_timers, list)
223 if (tmp_timer == timer) {
224 timer->count = timer->expires;
228 if (list_empty(active_timers))
229 pm_runtime_get(ci->dev);
231 timer->count = timer->expires;
232 list_add_tail(&timer->list, active_timers);
235 if (!(hw_read_otgsc(ci, OTGSC_1MSIE)))
236 hw_write_otgsc(ci, OTGSC_1MSIE, OTGSC_1MSIE);
240 * Remove timer from active timer list
242 static void ci_otg_del_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
244 struct ci_otg_fsm_timer *tmp_timer, *del_tmp;
245 struct ci_otg_fsm_timer *timer = ci->fsm_timer->timer_list[t];
246 struct list_head *active_timers = &ci->fsm_timer->active_timers;
249 if (t >= NUM_OTG_FSM_TIMERS)
252 list_for_each_entry_safe(tmp_timer, del_tmp, active_timers, list)
253 if (tmp_timer == timer) {
254 list_del(&timer->list);
258 /* Disable 1ms irq if there is no any active timer */
259 if (list_empty(active_timers) && (flag == 1)) {
260 hw_write_otgsc(ci, OTGSC_1MSIE, 0);
261 pm_runtime_put(ci->dev);
266 * Reduce timer count by 1, and find timeout conditions.
267 * Called by otg 1ms timer interrupt
269 static inline int ci_otg_tick_timer(struct ci_hdrc *ci)
271 struct ci_otg_fsm_timer *tmp_timer, *del_tmp;
272 struct list_head *active_timers = &ci->fsm_timer->active_timers;
275 list_for_each_entry_safe(tmp_timer, del_tmp, active_timers, list) {
277 /* check if timer expires */
278 if (!tmp_timer->count) {
279 list_del(&tmp_timer->list);
280 tmp_timer->function(ci, tmp_timer->data);
285 /* disable 1ms irq if there is no any timer active */
286 if ((expired == 1) && list_empty(active_timers)) {
287 hw_write_otgsc(ci, OTGSC_1MSIE, 0);
288 pm_runtime_put(ci->dev);
294 /* The timeout callback function to set time out bit */
295 static void set_tmout(void *ptr, unsigned long indicator)
297 *(int *)indicator = 1;
300 static void set_tmout_and_fsm(void *ptr, unsigned long indicator)
302 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
304 set_tmout(ci, indicator);
306 ci_otg_queue_work(ci);
309 static void a_wait_vfall_tmout_func(void *ptr, unsigned long indicator)
311 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
313 set_tmout(ci, indicator);
314 /* Disable port power */
315 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP, 0);
316 /* Clear existing DP irq */
317 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
318 /* Enable data pulse irq */
319 hw_write_otgsc(ci, OTGSC_DPIE, OTGSC_DPIE);
320 ci_otg_queue_work(ci);
323 static void b_ssend_srp_tmout_func(void *ptr, unsigned long indicator)
325 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
327 set_tmout(ci, indicator);
329 /* only vbus fall below B_sess_vld in b_idle state */
330 if (ci->fsm.otg->state == OTG_STATE_B_IDLE)
331 ci_otg_queue_work(ci);
334 static void b_data_pulse_end(void *ptr, unsigned long indicator)
336 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
338 ci->fsm.b_srp_done = 1;
339 ci->fsm.b_bus_req = 0;
340 if (ci->fsm.power_up)
341 ci->fsm.power_up = 0;
343 hw_write_otgsc(ci, OTGSC_HABA, 0);
345 ci_otg_queue_work(ci);
348 /* Initialize timers */
349 static int ci_otg_init_timers(struct ci_hdrc *ci)
351 struct otg_fsm *fsm = &ci->fsm;
353 /* FSM used timers */
354 ci->fsm_timer->timer_list[A_WAIT_VRISE] =
355 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_WAIT_VRISE,
356 (unsigned long)&fsm->a_wait_vrise_tmout);
357 if (ci->fsm_timer->timer_list[A_WAIT_VRISE] == NULL)
360 ci->fsm_timer->timer_list[A_WAIT_VFALL] =
361 otg_timer_initializer(ci, &a_wait_vfall_tmout_func,
362 TA_WAIT_VFALL, (unsigned long)&fsm->a_wait_vfall_tmout);
363 if (ci->fsm_timer->timer_list[A_WAIT_VFALL] == NULL)
366 ci->fsm_timer->timer_list[A_WAIT_BCON] =
367 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_WAIT_BCON,
368 (unsigned long)&fsm->a_wait_bcon_tmout);
369 if (ci->fsm_timer->timer_list[A_WAIT_BCON] == NULL)
372 ci->fsm_timer->timer_list[A_AIDL_BDIS] =
373 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_AIDL_BDIS,
374 (unsigned long)&fsm->a_aidl_bdis_tmout);
375 if (ci->fsm_timer->timer_list[A_AIDL_BDIS] == NULL)
378 ci->fsm_timer->timer_list[A_BIDL_ADIS] =
379 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_BIDL_ADIS,
380 (unsigned long)&fsm->a_bidl_adis_tmout);
381 if (ci->fsm_timer->timer_list[A_BIDL_ADIS] == NULL)
384 ci->fsm_timer->timer_list[B_ASE0_BRST] =
385 otg_timer_initializer(ci, &set_tmout_and_fsm, TB_ASE0_BRST,
386 (unsigned long)&fsm->b_ase0_brst_tmout);
387 if (ci->fsm_timer->timer_list[B_ASE0_BRST] == NULL)
390 ci->fsm_timer->timer_list[B_SE0_SRP] =
391 otg_timer_initializer(ci, &set_tmout_and_fsm, TB_SE0_SRP,
392 (unsigned long)&fsm->b_se0_srp);
393 if (ci->fsm_timer->timer_list[B_SE0_SRP] == NULL)
396 ci->fsm_timer->timer_list[B_SSEND_SRP] =
397 otg_timer_initializer(ci, &b_ssend_srp_tmout_func, TB_SSEND_SRP,
398 (unsigned long)&fsm->b_ssend_srp);
399 if (ci->fsm_timer->timer_list[B_SSEND_SRP] == NULL)
402 ci->fsm_timer->timer_list[B_SRP_FAIL] =
403 otg_timer_initializer(ci, &set_tmout, TB_SRP_FAIL,
404 (unsigned long)&fsm->b_srp_done);
405 if (ci->fsm_timer->timer_list[B_SRP_FAIL] == NULL)
408 ci->fsm_timer->timer_list[B_DATA_PLS] =
409 otg_timer_initializer(ci, &b_data_pulse_end, TB_DATA_PLS, 0);
410 if (ci->fsm_timer->timer_list[B_DATA_PLS] == NULL)
416 /* -------------------------------------------------------------*/
417 /* Operations that will be called from OTG Finite State Machine */
418 /* -------------------------------------------------------------*/
419 static void ci_otg_fsm_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
421 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
423 if (t < NUM_OTG_FSM_TIMERS)
424 ci_otg_add_timer(ci, t);
428 static void ci_otg_fsm_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
430 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
432 if (t < NUM_OTG_FSM_TIMERS)
433 ci_otg_del_timer(ci, t);
438 * A-device drive vbus: turn on vbus regulator and enable port power
439 * Data pulse irq should be disabled while vbus is on.
441 static void ci_otg_drv_vbus(struct otg_fsm *fsm, int on)
444 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
447 /* Enable power power */
448 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
450 if (ci->platdata->reg_vbus) {
451 ret = regulator_enable(ci->platdata->reg_vbus);
454 "Failed to enable vbus regulator, ret=%d\n",
459 /* Disable data pulse irq */
460 hw_write_otgsc(ci, OTGSC_DPIE, 0);
465 if (ci->platdata->reg_vbus)
466 regulator_disable(ci->platdata->reg_vbus);
474 * Control data line by Run Stop bit.
476 static void ci_otg_loc_conn(struct otg_fsm *fsm, int on)
478 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
481 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
483 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
487 * Generate SOF by host.
488 * This is controlled through suspend/resume the port.
489 * In host mode, controller will automatically send SOF.
490 * Suspend will block the data on the port.
492 static void ci_otg_loc_sof(struct otg_fsm *fsm, int on)
494 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
497 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_FPR,
500 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_SUSP,
505 * Start SRP pulsing by data-line pulsing,
506 * no v-bus pulsing followed
508 static void ci_otg_start_pulse(struct otg_fsm *fsm)
510 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
512 /* Hardware Assistant Data pulse */
513 hw_write_otgsc(ci, OTGSC_HADP, OTGSC_HADP);
515 ci_otg_add_timer(ci, B_DATA_PLS);
518 static int ci_otg_start_host(struct otg_fsm *fsm, int on)
520 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
522 mutex_unlock(&fsm->lock);
525 ci_role_start(ci, CI_ROLE_HOST);
529 ci_role_start(ci, CI_ROLE_GADGET);
531 mutex_lock(&fsm->lock);
535 static int ci_otg_start_gadget(struct otg_fsm *fsm, int on)
537 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
539 mutex_unlock(&fsm->lock);
541 usb_gadget_vbus_connect(&ci->gadget);
543 usb_gadget_vbus_disconnect(&ci->gadget);
544 mutex_lock(&fsm->lock);
549 static struct otg_fsm_ops ci_otg_ops = {
550 .drv_vbus = ci_otg_drv_vbus,
551 .loc_conn = ci_otg_loc_conn,
552 .loc_sof = ci_otg_loc_sof,
553 .start_pulse = ci_otg_start_pulse,
554 .add_timer = ci_otg_fsm_add_timer,
555 .del_timer = ci_otg_fsm_del_timer,
556 .start_host = ci_otg_start_host,
557 .start_gadget = ci_otg_start_gadget,
560 int ci_otg_fsm_work(struct ci_hdrc *ci)
563 * Don't do fsm transition for B device
564 * when there is no gadget class driver
566 if (ci->fsm.id && !(ci->driver) &&
567 ci->fsm.otg->state < OTG_STATE_A_IDLE)
570 pm_runtime_get_sync(ci->dev);
571 if (otg_statemachine(&ci->fsm)) {
572 if (ci->fsm.otg->state == OTG_STATE_A_IDLE) {
574 * Further state change for cases:
575 * a_idle to b_idle; or
576 * a_idle to a_wait_vrise due to ID change(1->0), so
577 * B-dev becomes A-dev can try to start new session
579 * a_idle to a_wait_vrise when power up
581 if ((ci->fsm.id) || (ci->id_event) ||
583 ci_otg_queue_work(ci);
585 ci->id_event = false;
586 } else if (ci->fsm.otg->state == OTG_STATE_B_IDLE) {
587 if (ci->fsm.b_sess_vld) {
588 ci->fsm.power_up = 0;
590 * Further transite to b_periphearl state
591 * when register gadget driver with vbus on
593 ci_otg_queue_work(ci);
595 } else if (ci->fsm.otg->state == OTG_STATE_A_HOST) {
596 pm_runtime_mark_last_busy(ci->dev);
597 pm_runtime_put_autosuspend(ci->dev);
601 pm_runtime_put_sync(ci->dev);
606 * Update fsm variables in each state if catching expected interrupts,
607 * called by otg fsm isr.
609 static void ci_otg_fsm_event(struct ci_hdrc *ci)
611 u32 intr_sts, otg_bsess_vld, port_conn;
612 struct otg_fsm *fsm = &ci->fsm;
614 intr_sts = hw_read_intr_status(ci);
615 otg_bsess_vld = hw_read_otgsc(ci, OTGSC_BSV);
616 port_conn = hw_read(ci, OP_PORTSC, PORTSC_CCS);
618 switch (ci->fsm.otg->state) {
619 case OTG_STATE_A_WAIT_BCON:
623 ci_otg_queue_work(ci);
626 case OTG_STATE_B_IDLE:
627 if (otg_bsess_vld && (intr_sts & USBi_PCI) && port_conn) {
629 ci_otg_queue_work(ci);
632 case OTG_STATE_B_PERIPHERAL:
633 if ((intr_sts & USBi_SLI) && port_conn && otg_bsess_vld) {
634 fsm->a_bus_suspend = 1;
635 ci_otg_queue_work(ci);
636 } else if (intr_sts & USBi_PCI) {
637 if (fsm->a_bus_suspend == 1)
638 fsm->a_bus_suspend = 0;
641 case OTG_STATE_B_HOST:
642 if ((intr_sts & USBi_PCI) && !port_conn) {
645 ci_otg_queue_work(ci);
648 case OTG_STATE_A_PERIPHERAL:
649 if (intr_sts & USBi_SLI) {
650 fsm->b_bus_suspend = 1;
652 * Init a timer to know how long this suspend
653 * will continue, if time out, indicates B no longer
654 * wants to be host role
656 ci_otg_add_timer(ci, A_BIDL_ADIS);
659 if (intr_sts & USBi_URI)
660 ci_otg_del_timer(ci, A_BIDL_ADIS);
662 if (intr_sts & USBi_PCI) {
663 if (fsm->b_bus_suspend == 1) {
664 ci_otg_del_timer(ci, A_BIDL_ADIS);
665 fsm->b_bus_suspend = 0;
669 case OTG_STATE_A_SUSPEND:
670 if ((intr_sts & USBi_PCI) && !port_conn) {
673 /* if gadget driver is binded */
675 /* A device to be peripheral mode */
676 ci->gadget.is_a_peripheral = 1;
678 ci_otg_queue_work(ci);
681 case OTG_STATE_A_HOST:
682 if ((intr_sts & USBi_PCI) && !port_conn) {
684 ci_otg_queue_work(ci);
687 case OTG_STATE_B_WAIT_ACON:
688 if ((intr_sts & USBi_PCI) && port_conn) {
690 ci_otg_queue_work(ci);
699 * ci_otg_irq - otg fsm related irq handling
700 * and also update otg fsm variable by monitoring usb host and udc
701 * state change interrupts.
704 irqreturn_t ci_otg_fsm_irq(struct ci_hdrc *ci)
706 irqreturn_t retval = IRQ_NONE;
707 u32 otgsc, otg_int_src = 0;
708 struct otg_fsm *fsm = &ci->fsm;
710 otgsc = hw_read_otgsc(ci, ~0);
711 otg_int_src = otgsc & OTGSC_INT_STATUS_BITS & (otgsc >> 8);
712 fsm->id = (otgsc & OTGSC_ID) ? 1 : 0;
715 if (otg_int_src & OTGSC_1MSIS) {
716 hw_write_otgsc(ci, OTGSC_1MSIS, OTGSC_1MSIS);
717 retval = ci_otg_tick_timer(ci);
719 } else if (otg_int_src & OTGSC_DPIS) {
720 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
723 } else if (otg_int_src & OTGSC_IDIS) {
724 hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
730 } else if (otg_int_src & OTGSC_BSVIS) {
731 hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
732 if (otgsc & OTGSC_BSV) {
734 ci_otg_del_timer(ci, B_SSEND_SRP);
735 ci_otg_del_timer(ci, B_SRP_FAIL);
736 fsm->b_ssend_srp = 0;
740 ci_otg_add_timer(ci, B_SSEND_SRP);
742 } else if (otg_int_src & OTGSC_AVVIS) {
743 hw_write_otgsc(ci, OTGSC_AVVIS, OTGSC_AVVIS);
744 if (otgsc & OTGSC_AVV) {
751 ci_otg_queue_work(ci);
755 ci_otg_fsm_event(ci);
760 void ci_hdrc_otg_fsm_start(struct ci_hdrc *ci)
762 ci_otg_queue_work(ci);
765 int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci)
770 ci->otg.phy = ci->phy;
772 ci->otg.usb_phy = ci->usb_phy;
774 ci->otg.gadget = &ci->gadget;
775 ci->fsm.otg = &ci->otg;
776 ci->fsm.power_up = 1;
777 ci->fsm.id = hw_read_otgsc(ci, OTGSC_ID) ? 1 : 0;
778 ci->fsm.otg->state = OTG_STATE_UNDEFINED;
779 ci->fsm.ops = &ci_otg_ops;
781 mutex_init(&ci->fsm.lock);
783 ci->fsm_timer = devm_kzalloc(ci->dev,
784 sizeof(struct ci_otg_fsm_timer_list), GFP_KERNEL);
788 INIT_LIST_HEAD(&ci->fsm_timer->active_timers);
789 retval = ci_otg_init_timers(ci);
791 dev_err(ci->dev, "Couldn't init OTG timers\n");
795 retval = sysfs_create_group(&ci->dev->kobj, &inputs_attr_group);
798 "Can't register sysfs attr group: %d\n", retval);
802 /* Enable A vbus valid irq */
803 hw_write_otgsc(ci, OTGSC_AVVIE, OTGSC_AVVIE);
806 ci->fsm.b_ssend_srp =
807 hw_read_otgsc(ci, OTGSC_BSV) ? 0 : 1;
809 hw_read_otgsc(ci, OTGSC_BSV) ? 1 : 0;
811 hw_write_otgsc(ci, OTGSC_BSVIE, OTGSC_BSVIE);
817 void ci_hdrc_otg_fsm_remove(struct ci_hdrc *ci)
819 sysfs_remove_group(&ci->dev->kobj, &inputs_attr_group);