Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 1 | /* Copyright (C) 2007,2008 Freescale Semiconductor, Inc. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify it |
| 4 | * under the terms of the GNU General Public License as published by the |
| 5 | * Free Software Foundation; either version 2 of the License, or (at your |
| 6 | * option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 675 Mass Ave, Cambridge, MA 02139, USA. |
| 16 | */ |
| 17 | |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 18 | #undef VERBOSE |
| 19 | |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 20 | #ifdef VERBOSE |
Greg Kroah-Hartman | 523e531 | 2013-06-28 11:32:55 -0700 | [diff] [blame] | 21 | #define VDBG(fmt, args...) pr_debug("[%s] " fmt , \ |
| 22 | __func__, ## args) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 23 | #else |
| 24 | #define VDBG(stuff...) do {} while (0) |
| 25 | #endif |
| 26 | |
| 27 | #ifdef VERBOSE |
| 28 | #define MPC_LOC printk("Current Location [%s]:[%d]\n", __FILE__, __LINE__) |
| 29 | #else |
| 30 | #define MPC_LOC do {} while (0) |
| 31 | #endif |
| 32 | |
| 33 | #define PROTO_UNDEF (0) |
| 34 | #define PROTO_HOST (1) |
| 35 | #define PROTO_GADGET (2) |
| 36 | |
| 37 | /* OTG state machine according to the OTG spec */ |
| 38 | struct otg_fsm { |
| 39 | /* Input */ |
| 40 | int a_bus_resume; |
| 41 | int a_bus_suspend; |
| 42 | int a_conn; |
| 43 | int a_sess_vld; |
| 44 | int a_srp_det; |
| 45 | int a_vbus_vld; |
| 46 | int b_bus_resume; |
| 47 | int b_bus_suspend; |
| 48 | int b_conn; |
| 49 | int b_se0_srp; |
| 50 | int b_sess_end; |
| 51 | int b_sess_vld; |
| 52 | int id; |
| 53 | |
| 54 | /* Internal variables */ |
| 55 | int a_set_b_hnp_en; |
| 56 | int b_srp_done; |
| 57 | int b_hnp_enable; |
| 58 | |
| 59 | /* Timeout indicator for timers */ |
| 60 | int a_wait_vrise_tmout; |
| 61 | int a_wait_bcon_tmout; |
| 62 | int a_aidl_bdis_tmout; |
| 63 | int b_ase0_brst_tmout; |
| 64 | |
| 65 | /* Informative variables */ |
| 66 | int a_bus_drop; |
| 67 | int a_bus_req; |
| 68 | int a_clr_err; |
| 69 | int a_suspend_req; |
| 70 | int b_bus_req; |
| 71 | |
| 72 | /* Output */ |
| 73 | int drv_vbus; |
| 74 | int loc_conn; |
| 75 | int loc_sof; |
| 76 | |
| 77 | struct otg_fsm_ops *ops; |
Heikki Krogerus | 7e062c0 | 2012-02-13 13:24:06 +0200 | [diff] [blame] | 78 | struct usb_otg *otg; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 79 | |
| 80 | /* Current usb protocol used: 0:undefine; 1:host; 2:client */ |
| 81 | int protocol; |
| 82 | spinlock_t lock; |
| 83 | }; |
| 84 | |
| 85 | struct otg_fsm_ops { |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 86 | void (*chrg_vbus)(struct otg_fsm *fsm, int on); |
| 87 | void (*drv_vbus)(struct otg_fsm *fsm, int on); |
| 88 | void (*loc_conn)(struct otg_fsm *fsm, int on); |
| 89 | void (*loc_sof)(struct otg_fsm *fsm, int on); |
| 90 | void (*start_pulse)(struct otg_fsm *fsm); |
| 91 | void (*add_timer)(struct otg_fsm *fsm, void *timer); |
| 92 | void (*del_timer)(struct otg_fsm *fsm, void *timer); |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 93 | int (*start_host)(struct otg_fsm *fsm, int on); |
| 94 | int (*start_gadget)(struct otg_fsm *fsm, int on); |
| 95 | }; |
| 96 | |
| 97 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 98 | static inline int otg_chrg_vbus(struct otg_fsm *fsm, int on) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 99 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 100 | if (!fsm->ops->chrg_vbus) |
| 101 | return -EOPNOTSUPP; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 102 | fsm->ops->chrg_vbus(fsm, on); |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 103 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 106 | static inline int otg_drv_vbus(struct otg_fsm *fsm, int on) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 107 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 108 | if (!fsm->ops->drv_vbus) |
| 109 | return -EOPNOTSUPP; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 110 | if (fsm->drv_vbus != on) { |
| 111 | fsm->drv_vbus = on; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 112 | fsm->ops->drv_vbus(fsm, on); |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 113 | } |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 114 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 117 | static inline int otg_loc_conn(struct otg_fsm *fsm, int on) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 118 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 119 | if (!fsm->ops->loc_conn) |
| 120 | return -EOPNOTSUPP; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 121 | if (fsm->loc_conn != on) { |
| 122 | fsm->loc_conn = on; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 123 | fsm->ops->loc_conn(fsm, on); |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 124 | } |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 125 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 128 | static inline int otg_loc_sof(struct otg_fsm *fsm, int on) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 129 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 130 | if (!fsm->ops->loc_sof) |
| 131 | return -EOPNOTSUPP; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 132 | if (fsm->loc_sof != on) { |
| 133 | fsm->loc_sof = on; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 134 | fsm->ops->loc_sof(fsm, on); |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 135 | } |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 136 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 139 | static inline int otg_start_pulse(struct otg_fsm *fsm) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 140 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 141 | if (!fsm->ops->start_pulse) |
| 142 | return -EOPNOTSUPP; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 143 | fsm->ops->start_pulse(fsm); |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 144 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 147 | static inline int otg_add_timer(struct otg_fsm *fsm, void *timer) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 148 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 149 | if (!fsm->ops->add_timer) |
| 150 | return -EOPNOTSUPP; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 151 | fsm->ops->add_timer(fsm, timer); |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 152 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 155 | static inline int otg_del_timer(struct otg_fsm *fsm, void *timer) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 156 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 157 | if (!fsm->ops->del_timer) |
| 158 | return -EOPNOTSUPP; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 159 | fsm->ops->del_timer(fsm, timer); |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 160 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | int otg_statemachine(struct otg_fsm *fsm); |
| 164 | |
| 165 | /* Defined by device specific driver, for different timer implementation */ |
| 166 | extern struct fsl_otg_timer *a_wait_vrise_tmr, *a_wait_bcon_tmr, |
| 167 | *a_aidl_bdis_tmr, *b_ase0_brst_tmr, *b_se0_srp_tmr, *b_srp_fail_tmr, |
| 168 | *a_wait_enum_tmr; |