1 // SPDX-License-Identifier: GPL-2.0
3 * otg_fsm.c - ChipIdea USB IP core OTG FSM driver
5 * Copyright (C) 2014 Freescale Semiconductor, Inc.
11 * This file mainly handles OTG fsm, it includes OTG fsm operations
19 #include <linux/usb/otg.h>
20 #include <linux/usb/gadget.h>
21 #include <linux/usb/hcd.h>
22 #include <linux/usb/chipidea.h>
23 #include <linux/regulator/consumer.h>
30 /* Add for otg: interact with user space app */
32 a_bus_req_show(struct device *dev, struct device_attribute *attr, char *buf)
36 struct ci_hdrc *ci = dev_get_drvdata(dev);
40 t = scnprintf(next, size, "%d\n", ci->fsm.a_bus_req);
44 return PAGE_SIZE - size;
48 a_bus_req_store(struct device *dev, struct device_attribute *attr,
49 const char *buf, size_t count)
51 struct ci_hdrc *ci = dev_get_drvdata(dev);
56 mutex_lock(&ci->fsm.lock);
58 ci->fsm.a_bus_req = 0;
59 } else if (buf[0] == '1') {
60 /* If a_bus_drop is TRUE, a_bus_req can't be set */
61 if (ci->fsm.a_bus_drop) {
62 mutex_unlock(&ci->fsm.lock);
65 ci->fsm.a_bus_req = 1;
66 if (ci->fsm.otg->state == OTG_STATE_A_PERIPHERAL) {
67 ci->gadget.host_request_flag = 1;
68 mutex_unlock(&ci->fsm.lock);
73 ci_otg_queue_work(ci);
74 mutex_unlock(&ci->fsm.lock);
78 static DEVICE_ATTR_RW(a_bus_req);
81 a_bus_drop_show(struct device *dev, struct device_attribute *attr, char *buf)
85 struct ci_hdrc *ci = dev_get_drvdata(dev);
89 t = scnprintf(next, size, "%d\n", ci->fsm.a_bus_drop);
93 return PAGE_SIZE - size;
97 a_bus_drop_store(struct device *dev, struct device_attribute *attr,
98 const char *buf, size_t count)
100 struct ci_hdrc *ci = dev_get_drvdata(dev);
105 mutex_lock(&ci->fsm.lock);
107 ci->fsm.a_bus_drop = 0;
108 } else if (buf[0] == '1') {
109 ci->fsm.a_bus_drop = 1;
110 ci->fsm.a_bus_req = 0;
113 ci_otg_queue_work(ci);
114 mutex_unlock(&ci->fsm.lock);
118 static DEVICE_ATTR_RW(a_bus_drop);
121 b_bus_req_show(struct device *dev, struct device_attribute *attr, char *buf)
125 struct ci_hdrc *ci = dev_get_drvdata(dev);
129 t = scnprintf(next, size, "%d\n", ci->fsm.b_bus_req);
133 return PAGE_SIZE - size;
137 b_bus_req_store(struct device *dev, struct device_attribute *attr,
138 const char *buf, size_t count)
140 struct ci_hdrc *ci = dev_get_drvdata(dev);
145 mutex_lock(&ci->fsm.lock);
147 ci->fsm.b_bus_req = 0;
148 else if (buf[0] == '1') {
149 ci->fsm.b_bus_req = 1;
150 if (ci->fsm.otg->state == OTG_STATE_B_PERIPHERAL) {
151 ci->gadget.host_request_flag = 1;
152 mutex_unlock(&ci->fsm.lock);
157 ci_otg_queue_work(ci);
158 mutex_unlock(&ci->fsm.lock);
162 static DEVICE_ATTR_RW(b_bus_req);
165 a_clr_err_store(struct device *dev, struct device_attribute *attr,
166 const char *buf, size_t count)
168 struct ci_hdrc *ci = dev_get_drvdata(dev);
173 mutex_lock(&ci->fsm.lock);
175 ci->fsm.a_clr_err = 1;
177 ci_otg_queue_work(ci);
178 mutex_unlock(&ci->fsm.lock);
182 static DEVICE_ATTR_WO(a_clr_err);
184 static struct attribute *inputs_attrs[] = {
185 &dev_attr_a_bus_req.attr,
186 &dev_attr_a_bus_drop.attr,
187 &dev_attr_b_bus_req.attr,
188 &dev_attr_a_clr_err.attr,
192 static const struct attribute_group inputs_attr_group = {
194 .attrs = inputs_attrs,
198 * Keep this list in the same order as timers indexed
199 * by enum otg_fsm_timer in include/linux/usb/otg-fsm.h
201 static unsigned otg_timer_ms[] = {
217 * Add timer to active timer list
219 static void ci_otg_add_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
221 unsigned long flags, timer_sec, timer_nsec;
223 if (t >= NUM_OTG_FSM_TIMERS)
226 spin_lock_irqsave(&ci->lock, flags);
227 timer_sec = otg_timer_ms[t] / MSEC_PER_SEC;
228 timer_nsec = (otg_timer_ms[t] % MSEC_PER_SEC) * NSEC_PER_MSEC;
229 ci->hr_timeouts[t] = ktime_add(ktime_get(),
230 ktime_set(timer_sec, timer_nsec));
231 ci->enabled_otg_timer_bits |= (1 << t);
232 if ((ci->next_otg_timer == NUM_OTG_FSM_TIMERS) ||
233 ktime_after(ci->hr_timeouts[ci->next_otg_timer],
234 ci->hr_timeouts[t])) {
235 ci->next_otg_timer = t;
236 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer,
237 ci->hr_timeouts[t], NSEC_PER_MSEC,
240 spin_unlock_irqrestore(&ci->lock, flags);
244 * Remove timer from active timer list
246 static void ci_otg_del_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
248 unsigned long flags, enabled_timer_bits;
249 enum otg_fsm_timer cur_timer, next_timer = NUM_OTG_FSM_TIMERS;
251 if ((t >= NUM_OTG_FSM_TIMERS) ||
252 !(ci->enabled_otg_timer_bits & (1 << t)))
255 spin_lock_irqsave(&ci->lock, flags);
256 ci->enabled_otg_timer_bits &= ~(1 << t);
257 if (ci->next_otg_timer == t) {
258 if (ci->enabled_otg_timer_bits == 0) {
259 /* No enabled timers after delete it */
260 hrtimer_cancel(&ci->otg_fsm_hrtimer);
261 ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
263 /* Find the next timer */
264 enabled_timer_bits = ci->enabled_otg_timer_bits;
265 for_each_set_bit(cur_timer, &enabled_timer_bits,
266 NUM_OTG_FSM_TIMERS) {
267 if ((next_timer == NUM_OTG_FSM_TIMERS) ||
268 ktime_before(ci->hr_timeouts[next_timer],
269 ci->hr_timeouts[cur_timer]))
270 next_timer = cur_timer;
274 if (next_timer != NUM_OTG_FSM_TIMERS) {
275 ci->next_otg_timer = next_timer;
276 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer,
277 ci->hr_timeouts[next_timer], NSEC_PER_MSEC,
280 spin_unlock_irqrestore(&ci->lock, flags);
283 /* OTG FSM timer handlers */
284 static int a_wait_vrise_tmout(struct ci_hdrc *ci)
286 ci->fsm.a_wait_vrise_tmout = 1;
290 static int a_wait_vfall_tmout(struct ci_hdrc *ci)
292 ci->fsm.a_wait_vfall_tmout = 1;
296 static int a_wait_bcon_tmout(struct ci_hdrc *ci)
298 ci->fsm.a_wait_bcon_tmout = 1;
302 static int a_aidl_bdis_tmout(struct ci_hdrc *ci)
304 ci->fsm.a_aidl_bdis_tmout = 1;
308 static int b_ase0_brst_tmout(struct ci_hdrc *ci)
310 ci->fsm.b_ase0_brst_tmout = 1;
314 static int a_bidl_adis_tmout(struct ci_hdrc *ci)
316 ci->fsm.a_bidl_adis_tmout = 1;
320 static int b_aidl_bdis_tmout(struct ci_hdrc *ci)
322 ci->fsm.a_bus_suspend = 1;
326 static int b_se0_srp_tmout(struct ci_hdrc *ci)
328 ci->fsm.b_se0_srp = 1;
332 static int b_srp_fail_tmout(struct ci_hdrc *ci)
334 ci->fsm.b_srp_done = 1;
338 static int b_data_pls_tmout(struct ci_hdrc *ci)
340 ci->fsm.b_srp_done = 1;
341 ci->fsm.b_bus_req = 0;
342 if (ci->fsm.power_up)
343 ci->fsm.power_up = 0;
344 hw_write_otgsc(ci, OTGSC_HABA, 0);
345 pm_runtime_put(ci->dev);
349 static int b_ssend_srp_tmout(struct ci_hdrc *ci)
351 ci->fsm.b_ssend_srp = 1;
352 /* only vbus fall below B_sess_vld in b_idle state */
353 if (ci->fsm.otg->state == OTG_STATE_B_IDLE)
360 * Keep this list in the same order as timers indexed
361 * by enum otg_fsm_timer in include/linux/usb/otg-fsm.h
363 static int (*otg_timer_handlers[])(struct ci_hdrc *) = {
364 a_wait_vrise_tmout, /* A_WAIT_VRISE */
365 a_wait_vfall_tmout, /* A_WAIT_VFALL */
366 a_wait_bcon_tmout, /* A_WAIT_BCON */
367 a_aidl_bdis_tmout, /* A_AIDL_BDIS */
368 b_ase0_brst_tmout, /* B_ASE0_BRST */
369 a_bidl_adis_tmout, /* A_BIDL_ADIS */
370 b_aidl_bdis_tmout, /* B_AIDL_BDIS */
371 b_se0_srp_tmout, /* B_SE0_SRP */
372 b_srp_fail_tmout, /* B_SRP_FAIL */
373 NULL, /* A_WAIT_ENUM */
374 b_data_pls_tmout, /* B_DATA_PLS */
375 b_ssend_srp_tmout, /* B_SSEND_SRP */
379 * Enable the next nearest enabled timer if have
381 static enum hrtimer_restart ci_otg_hrtimer_func(struct hrtimer *t)
383 struct ci_hdrc *ci = container_of(t, struct ci_hdrc, otg_fsm_hrtimer);
384 ktime_t now, *timeout;
385 unsigned long enabled_timer_bits;
387 enum otg_fsm_timer cur_timer, next_timer = NUM_OTG_FSM_TIMERS;
390 spin_lock_irqsave(&ci->lock, flags);
391 enabled_timer_bits = ci->enabled_otg_timer_bits;
392 ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
395 for_each_set_bit(cur_timer, &enabled_timer_bits, NUM_OTG_FSM_TIMERS) {
396 if (ktime_compare(now, ci->hr_timeouts[cur_timer]) >= 0) {
397 ci->enabled_otg_timer_bits &= ~(1 << cur_timer);
398 if (otg_timer_handlers[cur_timer])
399 ret = otg_timer_handlers[cur_timer](ci);
401 if ((next_timer == NUM_OTG_FSM_TIMERS) ||
402 ktime_before(ci->hr_timeouts[cur_timer],
403 ci->hr_timeouts[next_timer]))
404 next_timer = cur_timer;
407 /* Enable the next nearest timer */
408 if (next_timer < NUM_OTG_FSM_TIMERS) {
409 timeout = &ci->hr_timeouts[next_timer];
410 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer, *timeout,
411 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
412 ci->next_otg_timer = next_timer;
414 spin_unlock_irqrestore(&ci->lock, flags);
417 ci_otg_queue_work(ci);
419 return HRTIMER_NORESTART;
422 /* Initialize timers */
423 static int ci_otg_init_timers(struct ci_hdrc *ci)
425 hrtimer_init(&ci->otg_fsm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
426 ci->otg_fsm_hrtimer.function = ci_otg_hrtimer_func;
431 /* -------------------------------------------------------------*/
432 /* Operations that will be called from OTG Finite State Machine */
433 /* -------------------------------------------------------------*/
434 static void ci_otg_fsm_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
436 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
438 if (t < NUM_OTG_FSM_TIMERS)
439 ci_otg_add_timer(ci, t);
443 static void ci_otg_fsm_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
445 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
447 if (t < NUM_OTG_FSM_TIMERS)
448 ci_otg_del_timer(ci, t);
453 * A-device drive vbus: turn on vbus regulator and enable port power
454 * Data pulse irq should be disabled while vbus is on.
456 static void ci_otg_drv_vbus(struct otg_fsm *fsm, int on)
459 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
463 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
465 if (ci->platdata->reg_vbus) {
466 ret = regulator_enable(ci->platdata->reg_vbus);
469 "Failed to enable vbus regulator, ret=%d\n",
474 /* Disable data pulse irq */
475 hw_write_otgsc(ci, OTGSC_DPIE, 0);
480 if (ci->platdata->reg_vbus)
481 regulator_disable(ci->platdata->reg_vbus);
489 * Control data line by Run Stop bit.
491 static void ci_otg_loc_conn(struct otg_fsm *fsm, int on)
493 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
496 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
498 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
502 * Generate SOF by host.
503 * In host mode, controller will automatically send SOF.
504 * Suspend will block the data on the port.
506 * This is controlled through usbcore by usb autosuspend,
507 * so the usb device class driver need support autosuspend,
508 * otherwise the bus suspend will not happen.
510 static void ci_otg_loc_sof(struct otg_fsm *fsm, int on)
512 struct usb_device *udev;
517 udev = usb_hub_find_child(fsm->otg->host->root_hub, 1);
522 usb_disable_autosuspend(udev);
524 pm_runtime_set_autosuspend_delay(&udev->dev, 0);
525 usb_enable_autosuspend(udev);
530 * Start SRP pulsing by data-line pulsing,
531 * no v-bus pulsing followed
533 static void ci_otg_start_pulse(struct otg_fsm *fsm)
535 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
537 /* Hardware Assistant Data pulse */
538 hw_write_otgsc(ci, OTGSC_HADP, OTGSC_HADP);
540 pm_runtime_get(ci->dev);
541 ci_otg_add_timer(ci, B_DATA_PLS);
544 static int ci_otg_start_host(struct otg_fsm *fsm, int on)
546 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
550 ci_role_start(ci, CI_ROLE_HOST);
553 ci_role_start(ci, CI_ROLE_GADGET);
558 static int ci_otg_start_gadget(struct otg_fsm *fsm, int on)
560 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
563 usb_gadget_vbus_connect(&ci->gadget);
565 usb_gadget_vbus_disconnect(&ci->gadget);
570 static struct otg_fsm_ops ci_otg_ops = {
571 .drv_vbus = ci_otg_drv_vbus,
572 .loc_conn = ci_otg_loc_conn,
573 .loc_sof = ci_otg_loc_sof,
574 .start_pulse = ci_otg_start_pulse,
575 .add_timer = ci_otg_fsm_add_timer,
576 .del_timer = ci_otg_fsm_del_timer,
577 .start_host = ci_otg_start_host,
578 .start_gadget = ci_otg_start_gadget,
581 int ci_otg_fsm_work(struct ci_hdrc *ci)
584 * Don't do fsm transition for B device
585 * when there is no gadget class driver
587 if (ci->fsm.id && !(ci->driver) &&
588 ci->fsm.otg->state < OTG_STATE_A_IDLE)
591 pm_runtime_get_sync(ci->dev);
592 if (otg_statemachine(&ci->fsm)) {
593 if (ci->fsm.otg->state == OTG_STATE_A_IDLE) {
595 * Further state change for cases:
596 * a_idle to b_idle; or
597 * a_idle to a_wait_vrise due to ID change(1->0), so
598 * B-dev becomes A-dev can try to start new session
600 * a_idle to a_wait_vrise when power up
602 if ((ci->fsm.id) || (ci->id_event) ||
603 (ci->fsm.power_up)) {
604 ci_otg_queue_work(ci);
606 /* Enable data pulse irq */
607 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS |
609 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
610 hw_write_otgsc(ci, OTGSC_DPIE, OTGSC_DPIE);
613 ci->id_event = false;
614 } else if (ci->fsm.otg->state == OTG_STATE_B_IDLE) {
615 if (ci->fsm.b_sess_vld) {
616 ci->fsm.power_up = 0;
618 * Further transite to b_periphearl state
619 * when register gadget driver with vbus on
621 ci_otg_queue_work(ci);
623 } else if (ci->fsm.otg->state == OTG_STATE_A_HOST) {
624 pm_runtime_mark_last_busy(ci->dev);
625 pm_runtime_put_autosuspend(ci->dev);
629 pm_runtime_put_sync(ci->dev);
634 * Update fsm variables in each state if catching expected interrupts,
635 * called by otg fsm isr.
637 static void ci_otg_fsm_event(struct ci_hdrc *ci)
639 u32 intr_sts, otg_bsess_vld, port_conn;
640 struct otg_fsm *fsm = &ci->fsm;
642 intr_sts = hw_read_intr_status(ci);
643 otg_bsess_vld = hw_read_otgsc(ci, OTGSC_BSV);
644 port_conn = hw_read(ci, OP_PORTSC, PORTSC_CCS);
646 switch (ci->fsm.otg->state) {
647 case OTG_STATE_A_WAIT_BCON:
651 ci_otg_queue_work(ci);
654 case OTG_STATE_B_IDLE:
655 if (otg_bsess_vld && (intr_sts & USBi_PCI) && port_conn) {
657 ci_otg_queue_work(ci);
660 case OTG_STATE_B_PERIPHERAL:
661 if ((intr_sts & USBi_SLI) && port_conn && otg_bsess_vld) {
662 ci_otg_add_timer(ci, B_AIDL_BDIS);
663 } else if (intr_sts & USBi_PCI) {
664 ci_otg_del_timer(ci, B_AIDL_BDIS);
665 if (fsm->a_bus_suspend == 1)
666 fsm->a_bus_suspend = 0;
669 case OTG_STATE_B_HOST:
670 if ((intr_sts & USBi_PCI) && !port_conn) {
673 ci_otg_queue_work(ci);
676 case OTG_STATE_A_PERIPHERAL:
677 if (intr_sts & USBi_SLI) {
678 fsm->b_bus_suspend = 1;
680 * Init a timer to know how long this suspend
681 * will continue, if time out, indicates B no longer
682 * wants to be host role
684 ci_otg_add_timer(ci, A_BIDL_ADIS);
687 if (intr_sts & USBi_URI)
688 ci_otg_del_timer(ci, A_BIDL_ADIS);
690 if (intr_sts & USBi_PCI) {
691 if (fsm->b_bus_suspend == 1) {
692 ci_otg_del_timer(ci, A_BIDL_ADIS);
693 fsm->b_bus_suspend = 0;
697 case OTG_STATE_A_SUSPEND:
698 if ((intr_sts & USBi_PCI) && !port_conn) {
701 /* if gadget driver is binded */
703 /* A device to be peripheral mode */
704 ci->gadget.is_a_peripheral = 1;
706 ci_otg_queue_work(ci);
709 case OTG_STATE_A_HOST:
710 if ((intr_sts & USBi_PCI) && !port_conn) {
712 ci_otg_queue_work(ci);
715 case OTG_STATE_B_WAIT_ACON:
716 if ((intr_sts & USBi_PCI) && port_conn) {
718 ci_otg_queue_work(ci);
727 * ci_otg_irq - otg fsm related irq handling
728 * and also update otg fsm variable by monitoring usb host and udc
729 * state change interrupts.
732 irqreturn_t ci_otg_fsm_irq(struct ci_hdrc *ci)
734 irqreturn_t retval = IRQ_NONE;
735 u32 otgsc, otg_int_src = 0;
736 struct otg_fsm *fsm = &ci->fsm;
738 otgsc = hw_read_otgsc(ci, ~0);
739 otg_int_src = otgsc & OTGSC_INT_STATUS_BITS & (otgsc >> 8);
740 fsm->id = (otgsc & OTGSC_ID) ? 1 : 0;
743 if (otg_int_src & OTGSC_DPIS) {
744 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
747 } else if (otg_int_src & OTGSC_IDIS) {
748 hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
754 } else if (otg_int_src & OTGSC_BSVIS) {
755 hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
756 if (otgsc & OTGSC_BSV) {
758 ci_otg_del_timer(ci, B_SSEND_SRP);
759 ci_otg_del_timer(ci, B_SRP_FAIL);
760 fsm->b_ssend_srp = 0;
764 ci_otg_add_timer(ci, B_SSEND_SRP);
766 } else if (otg_int_src & OTGSC_AVVIS) {
767 hw_write_otgsc(ci, OTGSC_AVVIS, OTGSC_AVVIS);
768 if (otgsc & OTGSC_AVV) {
775 ci_otg_queue_work(ci);
779 ci_otg_fsm_event(ci);
784 void ci_hdrc_otg_fsm_start(struct ci_hdrc *ci)
786 ci_otg_queue_work(ci);
789 int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci)
794 ci->otg.phy = ci->phy;
796 ci->otg.usb_phy = ci->usb_phy;
798 ci->otg.gadget = &ci->gadget;
799 ci->fsm.otg = &ci->otg;
800 ci->fsm.power_up = 1;
801 ci->fsm.id = hw_read_otgsc(ci, OTGSC_ID) ? 1 : 0;
802 ci->fsm.otg->state = OTG_STATE_UNDEFINED;
803 ci->fsm.ops = &ci_otg_ops;
804 ci->gadget.hnp_polling_support = 1;
805 ci->fsm.host_req_flag = devm_kzalloc(ci->dev, 1, GFP_KERNEL);
806 if (!ci->fsm.host_req_flag)
809 mutex_init(&ci->fsm.lock);
811 retval = ci_otg_init_timers(ci);
813 dev_err(ci->dev, "Couldn't init OTG timers\n");
816 ci->enabled_otg_timer_bits = 0;
817 ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
819 retval = sysfs_create_group(&ci->dev->kobj, &inputs_attr_group);
822 "Can't register sysfs attr group: %d\n", retval);
826 /* Enable A vbus valid irq */
827 hw_write_otgsc(ci, OTGSC_AVVIE, OTGSC_AVVIE);
830 ci->fsm.b_ssend_srp =
831 hw_read_otgsc(ci, OTGSC_BSV) ? 0 : 1;
833 hw_read_otgsc(ci, OTGSC_BSV) ? 1 : 0;
835 hw_write_otgsc(ci, OTGSC_BSVIE, OTGSC_BSVIE);
841 void ci_hdrc_otg_fsm_remove(struct ci_hdrc *ci)
843 sysfs_remove_group(&ci->dev->kobj, &inputs_attr_group);