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 get_a_bus_req(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 set_a_bus_req(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(a_bus_req, S_IRUGO | S_IWUSR, get_a_bus_req, set_a_bus_req);
81 get_a_bus_drop(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 set_a_bus_drop(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(a_bus_drop, S_IRUGO | S_IWUSR, get_a_bus_drop,
122 get_b_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
126 struct ci_hdrc *ci = dev_get_drvdata(dev);
130 t = scnprintf(next, size, "%d\n", ci->fsm.b_bus_req);
134 return PAGE_SIZE - size;
138 set_b_bus_req(struct device *dev, struct device_attribute *attr,
139 const char *buf, size_t count)
141 struct ci_hdrc *ci = dev_get_drvdata(dev);
146 mutex_lock(&ci->fsm.lock);
148 ci->fsm.b_bus_req = 0;
149 else if (buf[0] == '1') {
150 ci->fsm.b_bus_req = 1;
151 if (ci->fsm.otg->state == OTG_STATE_B_PERIPHERAL) {
152 ci->gadget.host_request_flag = 1;
153 mutex_unlock(&ci->fsm.lock);
158 ci_otg_queue_work(ci);
159 mutex_unlock(&ci->fsm.lock);
163 static DEVICE_ATTR(b_bus_req, S_IRUGO | S_IWUSR, get_b_bus_req, set_b_bus_req);
166 set_a_clr_err(struct device *dev, struct device_attribute *attr,
167 const char *buf, size_t count)
169 struct ci_hdrc *ci = dev_get_drvdata(dev);
174 mutex_lock(&ci->fsm.lock);
176 ci->fsm.a_clr_err = 1;
178 ci_otg_queue_work(ci);
179 mutex_unlock(&ci->fsm.lock);
183 static DEVICE_ATTR(a_clr_err, S_IWUSR, NULL, set_a_clr_err);
185 static struct attribute *inputs_attrs[] = {
186 &dev_attr_a_bus_req.attr,
187 &dev_attr_a_bus_drop.attr,
188 &dev_attr_b_bus_req.attr,
189 &dev_attr_a_clr_err.attr,
193 static const struct attribute_group inputs_attr_group = {
195 .attrs = inputs_attrs,
199 * Keep this list in the same order as timers indexed
200 * by enum otg_fsm_timer in include/linux/usb/otg-fsm.h
202 static unsigned otg_timer_ms[] = {
218 * Add timer to active timer list
220 static void ci_otg_add_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
222 unsigned long flags, timer_sec, timer_nsec;
224 if (t >= NUM_OTG_FSM_TIMERS)
227 spin_lock_irqsave(&ci->lock, flags);
228 timer_sec = otg_timer_ms[t] / MSEC_PER_SEC;
229 timer_nsec = (otg_timer_ms[t] % MSEC_PER_SEC) * NSEC_PER_MSEC;
230 ci->hr_timeouts[t] = ktime_add(ktime_get(),
231 ktime_set(timer_sec, timer_nsec));
232 ci->enabled_otg_timer_bits |= (1 << t);
233 if ((ci->next_otg_timer == NUM_OTG_FSM_TIMERS) ||
234 ktime_after(ci->hr_timeouts[ci->next_otg_timer],
235 ci->hr_timeouts[t])) {
236 ci->next_otg_timer = t;
237 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer,
238 ci->hr_timeouts[t], NSEC_PER_MSEC,
241 spin_unlock_irqrestore(&ci->lock, flags);
245 * Remove timer from active timer list
247 static void ci_otg_del_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
249 unsigned long flags, enabled_timer_bits;
250 enum otg_fsm_timer cur_timer, next_timer = NUM_OTG_FSM_TIMERS;
252 if ((t >= NUM_OTG_FSM_TIMERS) ||
253 !(ci->enabled_otg_timer_bits & (1 << t)))
256 spin_lock_irqsave(&ci->lock, flags);
257 ci->enabled_otg_timer_bits &= ~(1 << t);
258 if (ci->next_otg_timer == t) {
259 if (ci->enabled_otg_timer_bits == 0) {
260 /* No enabled timers after delete it */
261 hrtimer_cancel(&ci->otg_fsm_hrtimer);
262 ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
264 /* Find the next timer */
265 enabled_timer_bits = ci->enabled_otg_timer_bits;
266 for_each_set_bit(cur_timer, &enabled_timer_bits,
267 NUM_OTG_FSM_TIMERS) {
268 if ((next_timer == NUM_OTG_FSM_TIMERS) ||
269 ktime_before(ci->hr_timeouts[next_timer],
270 ci->hr_timeouts[cur_timer]))
271 next_timer = cur_timer;
275 if (next_timer != NUM_OTG_FSM_TIMERS) {
276 ci->next_otg_timer = next_timer;
277 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer,
278 ci->hr_timeouts[next_timer], NSEC_PER_MSEC,
281 spin_unlock_irqrestore(&ci->lock, flags);
284 /* OTG FSM timer handlers */
285 static int a_wait_vrise_tmout(struct ci_hdrc *ci)
287 ci->fsm.a_wait_vrise_tmout = 1;
291 static int a_wait_vfall_tmout(struct ci_hdrc *ci)
293 ci->fsm.a_wait_vfall_tmout = 1;
297 static int a_wait_bcon_tmout(struct ci_hdrc *ci)
299 ci->fsm.a_wait_bcon_tmout = 1;
303 static int a_aidl_bdis_tmout(struct ci_hdrc *ci)
305 ci->fsm.a_aidl_bdis_tmout = 1;
309 static int b_ase0_brst_tmout(struct ci_hdrc *ci)
311 ci->fsm.b_ase0_brst_tmout = 1;
315 static int a_bidl_adis_tmout(struct ci_hdrc *ci)
317 ci->fsm.a_bidl_adis_tmout = 1;
321 static int b_aidl_bdis_tmout(struct ci_hdrc *ci)
323 ci->fsm.a_bus_suspend = 1;
327 static int b_se0_srp_tmout(struct ci_hdrc *ci)
329 ci->fsm.b_se0_srp = 1;
333 static int b_srp_fail_tmout(struct ci_hdrc *ci)
335 ci->fsm.b_srp_done = 1;
339 static int b_data_pls_tmout(struct ci_hdrc *ci)
341 ci->fsm.b_srp_done = 1;
342 ci->fsm.b_bus_req = 0;
343 if (ci->fsm.power_up)
344 ci->fsm.power_up = 0;
345 hw_write_otgsc(ci, OTGSC_HABA, 0);
346 pm_runtime_put(ci->dev);
350 static int b_ssend_srp_tmout(struct ci_hdrc *ci)
352 ci->fsm.b_ssend_srp = 1;
353 /* only vbus fall below B_sess_vld in b_idle state */
354 if (ci->fsm.otg->state == OTG_STATE_B_IDLE)
361 * Keep this list in the same order as timers indexed
362 * by enum otg_fsm_timer in include/linux/usb/otg-fsm.h
364 static int (*otg_timer_handlers[])(struct ci_hdrc *) = {
365 a_wait_vrise_tmout, /* A_WAIT_VRISE */
366 a_wait_vfall_tmout, /* A_WAIT_VFALL */
367 a_wait_bcon_tmout, /* A_WAIT_BCON */
368 a_aidl_bdis_tmout, /* A_AIDL_BDIS */
369 b_ase0_brst_tmout, /* B_ASE0_BRST */
370 a_bidl_adis_tmout, /* A_BIDL_ADIS */
371 b_aidl_bdis_tmout, /* B_AIDL_BDIS */
372 b_se0_srp_tmout, /* B_SE0_SRP */
373 b_srp_fail_tmout, /* B_SRP_FAIL */
374 NULL, /* A_WAIT_ENUM */
375 b_data_pls_tmout, /* B_DATA_PLS */
376 b_ssend_srp_tmout, /* B_SSEND_SRP */
380 * Enable the next nearest enabled timer if have
382 static enum hrtimer_restart ci_otg_hrtimer_func(struct hrtimer *t)
384 struct ci_hdrc *ci = container_of(t, struct ci_hdrc, otg_fsm_hrtimer);
385 ktime_t now, *timeout;
386 unsigned long enabled_timer_bits;
388 enum otg_fsm_timer cur_timer, next_timer = NUM_OTG_FSM_TIMERS;
391 spin_lock_irqsave(&ci->lock, flags);
392 enabled_timer_bits = ci->enabled_otg_timer_bits;
393 ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
396 for_each_set_bit(cur_timer, &enabled_timer_bits, NUM_OTG_FSM_TIMERS) {
397 if (ktime_compare(now, ci->hr_timeouts[cur_timer]) >= 0) {
398 ci->enabled_otg_timer_bits &= ~(1 << cur_timer);
399 if (otg_timer_handlers[cur_timer])
400 ret = otg_timer_handlers[cur_timer](ci);
402 if ((next_timer == NUM_OTG_FSM_TIMERS) ||
403 ktime_before(ci->hr_timeouts[cur_timer],
404 ci->hr_timeouts[next_timer]))
405 next_timer = cur_timer;
408 /* Enable the next nearest timer */
409 if (next_timer < NUM_OTG_FSM_TIMERS) {
410 timeout = &ci->hr_timeouts[next_timer];
411 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer, *timeout,
412 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
413 ci->next_otg_timer = next_timer;
415 spin_unlock_irqrestore(&ci->lock, flags);
418 ci_otg_queue_work(ci);
420 return HRTIMER_NORESTART;
423 /* Initialize timers */
424 static int ci_otg_init_timers(struct ci_hdrc *ci)
426 hrtimer_init(&ci->otg_fsm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
427 ci->otg_fsm_hrtimer.function = ci_otg_hrtimer_func;
432 /* -------------------------------------------------------------*/
433 /* Operations that will be called from OTG Finite State Machine */
434 /* -------------------------------------------------------------*/
435 static void ci_otg_fsm_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
437 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
439 if (t < NUM_OTG_FSM_TIMERS)
440 ci_otg_add_timer(ci, t);
444 static void ci_otg_fsm_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
446 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
448 if (t < NUM_OTG_FSM_TIMERS)
449 ci_otg_del_timer(ci, t);
454 * A-device drive vbus: turn on vbus regulator and enable port power
455 * Data pulse irq should be disabled while vbus is on.
457 static void ci_otg_drv_vbus(struct otg_fsm *fsm, int on)
460 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
463 /* Enable power power */
464 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
466 if (ci->platdata->reg_vbus) {
467 ret = regulator_enable(ci->platdata->reg_vbus);
470 "Failed to enable vbus regulator, ret=%d\n",
475 /* Disable data pulse irq */
476 hw_write_otgsc(ci, OTGSC_DPIE, 0);
481 if (ci->platdata->reg_vbus)
482 regulator_disable(ci->platdata->reg_vbus);
490 * Control data line by Run Stop bit.
492 static void ci_otg_loc_conn(struct otg_fsm *fsm, int on)
494 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
497 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
499 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
503 * Generate SOF by host.
504 * In host mode, controller will automatically send SOF.
505 * Suspend will block the data on the port.
507 * This is controlled through usbcore by usb autosuspend,
508 * so the usb device class driver need support autosuspend,
509 * otherwise the bus suspend will not happen.
511 static void ci_otg_loc_sof(struct otg_fsm *fsm, int on)
513 struct usb_device *udev;
518 udev = usb_hub_find_child(fsm->otg->host->root_hub, 1);
523 usb_disable_autosuspend(udev);
525 pm_runtime_set_autosuspend_delay(&udev->dev, 0);
526 usb_enable_autosuspend(udev);
531 * Start SRP pulsing by data-line pulsing,
532 * no v-bus pulsing followed
534 static void ci_otg_start_pulse(struct otg_fsm *fsm)
536 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
538 /* Hardware Assistant Data pulse */
539 hw_write_otgsc(ci, OTGSC_HADP, OTGSC_HADP);
541 pm_runtime_get(ci->dev);
542 ci_otg_add_timer(ci, B_DATA_PLS);
545 static int ci_otg_start_host(struct otg_fsm *fsm, int on)
547 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
551 ci_role_start(ci, CI_ROLE_HOST);
554 ci_role_start(ci, CI_ROLE_GADGET);
559 static int ci_otg_start_gadget(struct otg_fsm *fsm, int on)
561 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
564 usb_gadget_vbus_connect(&ci->gadget);
566 usb_gadget_vbus_disconnect(&ci->gadget);
571 static struct otg_fsm_ops ci_otg_ops = {
572 .drv_vbus = ci_otg_drv_vbus,
573 .loc_conn = ci_otg_loc_conn,
574 .loc_sof = ci_otg_loc_sof,
575 .start_pulse = ci_otg_start_pulse,
576 .add_timer = ci_otg_fsm_add_timer,
577 .del_timer = ci_otg_fsm_del_timer,
578 .start_host = ci_otg_start_host,
579 .start_gadget = ci_otg_start_gadget,
582 int ci_otg_fsm_work(struct ci_hdrc *ci)
585 * Don't do fsm transition for B device
586 * when there is no gadget class driver
588 if (ci->fsm.id && !(ci->driver) &&
589 ci->fsm.otg->state < OTG_STATE_A_IDLE)
592 pm_runtime_get_sync(ci->dev);
593 if (otg_statemachine(&ci->fsm)) {
594 if (ci->fsm.otg->state == OTG_STATE_A_IDLE) {
596 * Further state change for cases:
597 * a_idle to b_idle; or
598 * a_idle to a_wait_vrise due to ID change(1->0), so
599 * B-dev becomes A-dev can try to start new session
601 * a_idle to a_wait_vrise when power up
603 if ((ci->fsm.id) || (ci->id_event) ||
604 (ci->fsm.power_up)) {
605 ci_otg_queue_work(ci);
607 /* Enable data pulse irq */
608 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS |
610 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
611 hw_write_otgsc(ci, OTGSC_DPIE, OTGSC_DPIE);
614 ci->id_event = false;
615 } else if (ci->fsm.otg->state == OTG_STATE_B_IDLE) {
616 if (ci->fsm.b_sess_vld) {
617 ci->fsm.power_up = 0;
619 * Further transite to b_periphearl state
620 * when register gadget driver with vbus on
622 ci_otg_queue_work(ci);
624 } else if (ci->fsm.otg->state == OTG_STATE_A_HOST) {
625 pm_runtime_mark_last_busy(ci->dev);
626 pm_runtime_put_autosuspend(ci->dev);
630 pm_runtime_put_sync(ci->dev);
635 * Update fsm variables in each state if catching expected interrupts,
636 * called by otg fsm isr.
638 static void ci_otg_fsm_event(struct ci_hdrc *ci)
640 u32 intr_sts, otg_bsess_vld, port_conn;
641 struct otg_fsm *fsm = &ci->fsm;
643 intr_sts = hw_read_intr_status(ci);
644 otg_bsess_vld = hw_read_otgsc(ci, OTGSC_BSV);
645 port_conn = hw_read(ci, OP_PORTSC, PORTSC_CCS);
647 switch (ci->fsm.otg->state) {
648 case OTG_STATE_A_WAIT_BCON:
652 ci_otg_queue_work(ci);
655 case OTG_STATE_B_IDLE:
656 if (otg_bsess_vld && (intr_sts & USBi_PCI) && port_conn) {
658 ci_otg_queue_work(ci);
661 case OTG_STATE_B_PERIPHERAL:
662 if ((intr_sts & USBi_SLI) && port_conn && otg_bsess_vld) {
663 ci_otg_add_timer(ci, B_AIDL_BDIS);
664 } else if (intr_sts & USBi_PCI) {
665 ci_otg_del_timer(ci, B_AIDL_BDIS);
666 if (fsm->a_bus_suspend == 1)
667 fsm->a_bus_suspend = 0;
670 case OTG_STATE_B_HOST:
671 if ((intr_sts & USBi_PCI) && !port_conn) {
674 ci_otg_queue_work(ci);
677 case OTG_STATE_A_PERIPHERAL:
678 if (intr_sts & USBi_SLI) {
679 fsm->b_bus_suspend = 1;
681 * Init a timer to know how long this suspend
682 * will continue, if time out, indicates B no longer
683 * wants to be host role
685 ci_otg_add_timer(ci, A_BIDL_ADIS);
688 if (intr_sts & USBi_URI)
689 ci_otg_del_timer(ci, A_BIDL_ADIS);
691 if (intr_sts & USBi_PCI) {
692 if (fsm->b_bus_suspend == 1) {
693 ci_otg_del_timer(ci, A_BIDL_ADIS);
694 fsm->b_bus_suspend = 0;
698 case OTG_STATE_A_SUSPEND:
699 if ((intr_sts & USBi_PCI) && !port_conn) {
702 /* if gadget driver is binded */
704 /* A device to be peripheral mode */
705 ci->gadget.is_a_peripheral = 1;
707 ci_otg_queue_work(ci);
710 case OTG_STATE_A_HOST:
711 if ((intr_sts & USBi_PCI) && !port_conn) {
713 ci_otg_queue_work(ci);
716 case OTG_STATE_B_WAIT_ACON:
717 if ((intr_sts & USBi_PCI) && port_conn) {
719 ci_otg_queue_work(ci);
728 * ci_otg_irq - otg fsm related irq handling
729 * and also update otg fsm variable by monitoring usb host and udc
730 * state change interrupts.
733 irqreturn_t ci_otg_fsm_irq(struct ci_hdrc *ci)
735 irqreturn_t retval = IRQ_NONE;
736 u32 otgsc, otg_int_src = 0;
737 struct otg_fsm *fsm = &ci->fsm;
739 otgsc = hw_read_otgsc(ci, ~0);
740 otg_int_src = otgsc & OTGSC_INT_STATUS_BITS & (otgsc >> 8);
741 fsm->id = (otgsc & OTGSC_ID) ? 1 : 0;
744 if (otg_int_src & OTGSC_DPIS) {
745 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
748 } else if (otg_int_src & OTGSC_IDIS) {
749 hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
755 } else if (otg_int_src & OTGSC_BSVIS) {
756 hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
757 if (otgsc & OTGSC_BSV) {
759 ci_otg_del_timer(ci, B_SSEND_SRP);
760 ci_otg_del_timer(ci, B_SRP_FAIL);
761 fsm->b_ssend_srp = 0;
765 ci_otg_add_timer(ci, B_SSEND_SRP);
767 } else if (otg_int_src & OTGSC_AVVIS) {
768 hw_write_otgsc(ci, OTGSC_AVVIS, OTGSC_AVVIS);
769 if (otgsc & OTGSC_AVV) {
776 ci_otg_queue_work(ci);
780 ci_otg_fsm_event(ci);
785 void ci_hdrc_otg_fsm_start(struct ci_hdrc *ci)
787 ci_otg_queue_work(ci);
790 int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci)
795 ci->otg.phy = ci->phy;
797 ci->otg.usb_phy = ci->usb_phy;
799 ci->otg.gadget = &ci->gadget;
800 ci->fsm.otg = &ci->otg;
801 ci->fsm.power_up = 1;
802 ci->fsm.id = hw_read_otgsc(ci, OTGSC_ID) ? 1 : 0;
803 ci->fsm.otg->state = OTG_STATE_UNDEFINED;
804 ci->fsm.ops = &ci_otg_ops;
805 ci->gadget.hnp_polling_support = 1;
806 ci->fsm.host_req_flag = devm_kzalloc(ci->dev, 1, GFP_KERNEL);
807 if (!ci->fsm.host_req_flag)
810 mutex_init(&ci->fsm.lock);
812 retval = ci_otg_init_timers(ci);
814 dev_err(ci->dev, "Couldn't init OTG timers\n");
817 ci->enabled_otg_timer_bits = 0;
818 ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
820 retval = sysfs_create_group(&ci->dev->kobj, &inputs_attr_group);
823 "Can't register sysfs attr group: %d\n", retval);
827 /* Enable A vbus valid irq */
828 hw_write_otgsc(ci, OTGSC_AVVIE, OTGSC_AVVIE);
831 ci->fsm.b_ssend_srp =
832 hw_read_otgsc(ci, OTGSC_BSV) ? 0 : 1;
834 hw_read_otgsc(ci, OTGSC_BSV) ? 1 : 0;
836 hw_write_otgsc(ci, OTGSC_BSVIE, OTGSC_BSVIE);
842 void ci_hdrc_otg_fsm_remove(struct ci_hdrc *ci)
844 sysfs_remove_group(&ci->dev->kobj, &inputs_attr_group);