Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | Introduction |
| 2 | ============ |
| 3 | This driver implements Session Request Protocol (SRP) and Host negotiation |
| 4 | Protocol (HNP) described in On-The-Go (OTG) Supplement to the USB 2.0 |
| 5 | Specification. It also provides support for Accessory Charger Adapter (ACA) |
| 6 | defined in the Battery Charging Specification 1.1. |
| 7 | |
| 8 | These protocols provide a means for USB host devices to intelligently manage |
| 9 | power on VBUS and USB peripheral devices to become the host when paired with |
| 10 | another OTG device. |
| 11 | |
| 12 | Hardware description |
| 13 | ==================== |
| 14 | USB hardware found in Qualcomm chipsets like MSM7x27, MSM7x30, QSD8x50 and |
| 15 | MSM8660 is compliant to USB 2.0 high speed On-The-Go protocol. |
| 16 | The transceiver, aka PHY is integrated on the chip and ULPI interface is used for |
| 17 | communication. |
| 18 | |
| 19 | USB hardware interfaces to the system memory via AHB BUS. DMA engine is included |
| 20 | to move all of the data to be transferred over the USB between USB core and |
| 21 | system memory. Device controller can support 16 endpoints of all types |
| 22 | (control/bulk/interrupt /isochronous) defined in USB 2.0 specification. The |
| 23 | host controller is compliant to EHCI specification. Directly connected USB 1.1 |
| 24 | Full/Low speed devices are supported without a companion controller by having |
| 25 | inbuilt Transaction Translator (TT). |
| 26 | |
| 27 | USB_HS_CLK, USB_HS_PCLK and USB_HS_CCLK are required for USB operation. |
| 28 | Phy feeds 60MHZ HS_CLK to link when ULPI interface is used. This clock needs to |
| 29 | be turned on only while resetting the link. HS_PCLK (Pbus clock) is required to |
| 30 | move data to/from hardware FIFO. This clock may not be required on targets like |
| 31 | MSM8660 where USB is part of smart peripheral subsystem. AXI bus frequency needs |
| 32 | to be kept at maximum value while USB data transfers are happening. HS_CCLK |
| 33 | (core clock) is introduced in MSM7x30 to get rid of dependency on AXI bus |
| 34 | frequency. |
| 35 | |
| 36 | The same irq line is shared across OTG, Device controller and Host controller |
| 37 | drivers. Phy is integrated on the chip and no gpios are required to connect link |
| 38 | and PHY. |
| 39 | |
| 40 | Phy can monitor VBUS and ID lines while operating in low power mode (LPM). But |
| 41 | leaving comparators ON in LPM increases power consumption. Hence VBUS line is |
| 42 | routed to PMIC hardware which can generate interrupt (when accessed by Apps |
| 43 | processor) or send RPC callback. This is also useful when an External LDO to |
| 44 | power up 3.3V of PHY is not installed. An internal LDO is turned upon |
| 45 | receiving notification from PMIC. Id line is not routed to PMIC. Hence OTG mode |
| 46 | can not be supported with this configuration and External LDO must be present. |
| 47 | |
| 48 | Hardware can generate interrupt when voltage on VBUS line is reached |
| 49 | above/below A-VBUS Valid and B-Session Valid threshold values defined in OTG |
| 50 | specification. Interrupt is generated when Id line is grounded i.e Micro-A |
| 51 | cable is connected. |
| 52 | |
| 53 | The following hardware features help in meeting the SRP and HNP protocol |
| 54 | timings. |
| 55 | |
| 56 | Hardware Assist Data-pulse (HADP): |
| 57 | --------------------------------- |
| 58 | When software programs HADP, Hardware start a data pulse of approximately 7ms |
| 59 | in duration and then automatically ceases the data pulsing. This automation |
| 60 | relieves software from controlling the data-pulse duration. This assist will |
| 61 | ensure data pulsing meets the OTG requirement of > 5ms and < 10ms. |
| 62 | |
| 63 | Hardware Assist Auto-Reset (HAAR): |
| 64 | --------------------------------- |
| 65 | When software programs HAAR, Hardware will automatically start a reset after |
| 66 | a connect event. This shortcuts the normal process where software is notified |
| 67 | of the connect event and starts the reset. Software will still receive |
| 68 | notification of the connect event but should not write the reset bit when the |
| 69 | HAAR is set. Software will be notified again after the reset is complete via |
| 70 | the enable change bit in the PORTSC register which cause a port change |
| 71 | interrupt. |
| 72 | |
| 73 | Hardware Assist B-Disconnect to A-Connect (HABA): |
| 74 | ------------------------------------------------ |
| 75 | During Host negotiation Protocol(HNP), A-Device must enable pull-up on D+ as |
| 76 | soon as possible after detecting disconnect from B-device. |
| 77 | |
| 78 | When Software programs HABA, the Host Controller port is in suspend mode, and |
| 79 | the B-device disconnects, then this hardware assist begins. |
| 80 | 1. Reset the OTG core |
| 81 | 2. Write the OTG core into device mode. |
| 82 | 3. Write the device run bit to a '1' and enable necessary interrupts including: |
| 83 | * USB Reset Enable (URE) : enables interrupt on usb bus reset to device |
| 84 | * Sleep Enable (SLE) : enables interrupt on device suspend |
| 85 | * Port Change Detect Enable (PCE) : enables interrupt on device connect |
| 86 | |
| 87 | When software has enabled this hardware assist, it must not interfere during the |
| 88 | transition and should not write any register in the core until it gets an |
| 89 | interrupt from the device controller signifying that a reset interrupt has |
| 90 | occurred or at least first verify that the core has entered device mode. |
| 91 | |
| 92 | The following hardware feature helps in supporting Accessory Charger Adapter: |
| 93 | |
| 94 | PHY Support for ID_A/B/C: |
| 95 | ------------------------ |
| 96 | Accessory Charger Adapter has three ports to attach an OTG, charger and A or |
| 97 | B-device. So, based on what all device are attached to the ACA, it outputs a |
| 98 | state on the ID pin (i.e GROUND, ID_A, ID_B, ID_C, FLOAT). |
| 99 | USB PHY has support for these ID states. Once software enables this support, |
| 100 | PHY sets corresponding bit in its INTS register based on any changes in the |
| 101 | ID state. |
| 102 | |
| 103 | Software description |
| 104 | ==================== |
| 105 | |
| 106 | This driver provides OTG functionality when Device controller driver (DCD) and |
| 107 | Host controller driver (HCD) are enabled. It is enabled even when one of the DCD |
| 108 | or HCD is enabled to use PHY initialization, clock management, register memory |
| 109 | mapping, low power mode (LPM) functionalities. |
| 110 | |
| 111 | Session Request Protocol (SRP): A-device may turn off power on VBUS upon user |
| 112 | request or A_WAIT_BCON timeout. SRP detection interrupt is enabled and |
| 113 | hardware is put into LPM. If Data pulse is detected, A-device starts a new |
| 114 | session by applying power on VBUS. Hardware Auto Assist Data pulse feature is |
| 115 | used to program Data pulse |
| 116 | When acting as a B-device, if SRP initial conditions (SE0 condition for |
| 117 | TB_SE0_SRP min and previous session was ended before TB_SSEND_SRP) are met, SRP |
| 118 | is initiated upon user request. Hardware Auto Assist Data pulse feature is |
| 119 | used to program Data pulse. If remote device does not turn on VBUS before |
| 120 | TB_SRP_FAIL, an error is reported to user space. |
| 121 | |
| 122 | Host Negotiation Protocol (HNP): A-device periodically polls B-device to check |
| 123 | host request status. When B-device returns true, A-device shall enable HNP and |
| 124 | suspend the bus with in THOST_REQ_SUSP. HNP polling is implemented in USB core |
| 125 | software. HCD registers a start_hnp callback method with EHCI framework. This |
| 126 | method is called after suspending the port if HNP is enabled. HCD notifies OTG |
| 127 | that B-device is suspended. A_AIDL_BDIS timer is kicked and waits for B-device |
| 128 | disconnection. If B-device does not signal disconnect within TA_AIDL_BDIS |
| 129 | timeout, session is closed by powering down VBUS. Otherwise A-device stops HCD |
| 130 | and starts DCD to enable pull-up. A-device again resumes host role if it had |
| 131 | observed bus idle for TA_BIDL_ADIS time. |
| 132 | B-device signals host_request true upon user request. DCD notifies OTG that |
| 133 | HNP is enabled and bus is idle. OTG driver disable pull-up by stopping DCD and |
| 134 | kick B_ASE0_BRST timer. If A-device does not signal connect with in |
| 135 | TB_ASE0_BRST, B-device resumes in peripheral role. Otherwise B-device assert |
| 136 | the bus reset and enumerate the A-device. |
| 137 | |
| 138 | MSM chipsets which have 45nm integrated PHY supports Attach Detection Protocol. |
| 139 | (A protocol which enables an OTG device to detect when a remote device has been |
| 140 | attached or detached without supplying VBUS). ADP support needs to be |
| 141 | implemented to efficiently supply/request power on VBUS. Leakage currents (i.e |
| 142 | VBUS applied but no peripheral is connected) are very less on MSM hardware. So |
| 143 | VBUS can be applied when Id becomes false. ADP may be never implemented in |
| 144 | this driver due to this reason. |
| 145 | |
| 146 | The state machine is implemented as described in the OTG specification. |
| 147 | A few exceptions are mentioned below: |
| 148 | |
| 149 | 1. Host session request i.e a_bus_request input variable is automatically |
| 150 | asserted when Id becomes false and SRP is detected. |
| 151 | It is de-asserted When HCD suspends the bus and asserted again in case of |
| 152 | remote device remote wakeup. |
| 153 | 2. Output variables like drv_vbus, loc_conn, loc_sof, adp_prb are not |
| 154 | maintained in the state machine as they serve no purpose. |
| 155 | 3. Bus power down request i.e a_bus_drop is cleared when Micro-A cable is |
| 156 | connected so that non OTG device can be detected when Micro-A cable is |
| 157 | connected next time. |
| 158 | 4. Input variables that determine SRP initial condition status b_se0_srp and |
| 159 | b_ssend_srp are not maintained in state machine processing. When a session is |
| 160 | ended i.e VBUS falls below B-Session Valid threshold, time stamp is taken and |
| 161 | is compared against the current time at the time of SRP initiation. |
| 162 | |
| 163 | |
| 164 | Controller gives interrupt for every 1 msec if 1MSIE (1 Msec interrupt enable) |
| 165 | is set. Timers used in OTG state machine can be implementing using 1MSEC |
| 166 | timer as a source. But hrtimer on MSM hardware can give at least 1/32KHZ |
| 167 | precision. So hrtimer framework is used to implement OTG timers. No two OTG |
| 168 | timers run in parallel. Hence one hrtimer is used for all OTG timers. |
| 169 | |
| 170 | OTG state machine processing can not be done in atomic context. Hence a worker |
| 171 | thread is created for processing the state machine events. A separate worker |
| 172 | thread is created instead of using default worker thread to meet OTG |
| 173 | specification timings. |
| 174 | |
| 175 | OTG supplement Revision 2.0 has made HNP timings less stringent compared to |
| 176 | Revision 1.3. TA_BDIS_ACON (The time with in A-Device should enable pull-up |
| 177 | upon B-device signals disconnect) has been changed to 150 msec from 3 msec. |
| 178 | DCD can be easily activated within 150 msec. Hence HABA is not used. |
| 179 | TB_ACON_BSE0 (The time with in B-device should reset the A-device) has been |
| 180 | changed to 150 msec from 1 sec. Host software would easily meet this timing |
| 181 | given that de-bounce delays and root hub port power stabilization delays are |
| 182 | not required during HNP. |
| 183 | |
| 184 | Accessory Charger Adapter (ACA): To support ACA there must be support in the |
| 185 | USB hardware (Controller and PHY) for the ID_A/B/C states. It should be able |
| 186 | to interrupt software for any ID state changes. On receiving this interrupt, |
| 187 | interrupt handler checks the current ID state and invokes OTG state machine |
| 188 | for further handling. Even if the USB Controller doesn't support these ID_A/B/C |
| 189 | states, driver can still detect the ID state transitions by depending on USB |
| 190 | PHY if the PHY supports these ID states. For this scenario, driver relies |
| 191 | on polling of PHY register to determine the ID state changes as long as an |
| 192 | ACA is attached to the system. This polling is implemented by using a timer |
| 193 | running at a frequency of 1 sec. This timer checks for the current ID state |
| 194 | and on detecting any change it invokes OTG state machine for further handling. |
| 195 | |
| 196 | Following are the actions performed by the driver as per the ID state: |
| 197 | * ID_FLOAT: Configure device to act as peripheral and allow charging if VBUS |
| 198 | is present, else move it to LPM (low power mode). |
| 199 | * ID_GROUND: Configure device to act as host and supply VBUS. |
| 200 | * ID_A: Configure device to act as host and don't supply VBUS. In this state |
| 201 | the device can charge as well. |
| 202 | * ID_B: Keep the device in IDLE state and allow charging. |
| 203 | * ID_C: Configure device to act as peripheral and allow charging. |
| 204 | |
| 205 | Design |
| 206 | ====== |
| 207 | |
| 208 | The following goals are kept in mind while designing OTG state machine. |
| 209 | |
| 210 | 1. Avoid User intervention when operating as a standard Host or standard |
| 211 | peripheral |
| 212 | 2. Support host only and peripheral only modes |
| 213 | 3. Put Hardware in LPM when ever possible |
| 214 | 4. Pass OTG compliance tests |
| 215 | 5. Report notification/error messages to user space |
| 216 | 6. With ACA, allow charging in Host mode as well |
| 217 | 7. Disable LPM as long as ID state polling is happening |
| 218 | |
| 219 | Power Management |
| 220 | ================ |
| 221 | |
| 222 | System suspend is negated by acquiring wakelock while processing OTG state |
| 223 | machine, or while polling for the PHY ID state in case of ACA. |
| 224 | Wakelock is released: |
| 225 | 1. After activating the DCD/HCD. It is the responsibility of DCD/HCD to |
| 226 | acquire wakelock if required. |
| 227 | 2. After putting hardware in LPM. |
| 228 | 3. No state machine events and timers are pending. This would cover scenarios |
| 229 | mentioned in (1) and (2). |
| 230 | 4. After driver stops polling for ID state in case of ACA. |
| 231 | |
| 232 | Wake lock is re-acquired when state machine work is scheduled, which can |
| 233 | happen from interrupt (exiting LPM), sysfs entries (initiate SRP, clear |
| 234 | error, bus drop, etc), or from ID state polling routine. |
| 235 | |
| 236 | OTG driver provides set_suspend method for DCD/HCD to put hardware in LPM. DCD |
| 237 | can use this method upon bus suspend. HCD can use this method upon suspending |
| 238 | the root hub. |
| 239 | |
| 240 | LPM entering procedure: |
| 241 | 1. Clear PHY interrupt latch registers. |
| 242 | 2. Enable PHY comparators to detect Id, B-Session Valid interrupts while hardware |
| 243 | is in LPM. |
| 244 | 3. Turn off PLL block on the PHY to achieve maximum power savings. |
| 245 | 4. Put PHY in suspend mode by setting PHCD bit in PORTSC register. |
| 246 | 5. Enable asynchronous interrupt so that PHY can generate interrupt when |
| 247 | clocks are disabled. |
| 248 | 6. Disable all USB clocks. |
| 249 | |
| 250 | LPM exit procedure: |
| 251 | 1. Enable USB clocks. |
| 252 | 2. Disable asynchronous interrupt. |
| 253 | 3. Put PHY out of suspend mode. This is not required when LPM is exited due to |
| 254 | hardware activity i.e asynchronous interrupt. |
| 255 | |
| 256 | SMP/multi-core |
| 257 | ============== |
| 258 | |
| 259 | OTG state machine inputs like bus request, bus drop, srp_detect etc accessed |
| 260 | from interrupt context, and multiple process contexts. Hence atomic bit ops are |
| 261 | used. ulpi_read and ulpi_write functions can now be accessed from multiple |
| 262 | context, hence, these are protected using a spin_lock. |
| 263 | |
| 264 | Interface |
| 265 | ========= |
| 266 | This driver provides the following methods to DCD and HCD. |
| 267 | |
| 268 | set_peripheral: DCD use this methods to register/unregister USB gadget. |
| 269 | |
| 270 | set_host: HCD use this method to register/unregister USB bus. Unlike gadget |
| 271 | framework, there are no standard methods to start/stop HCD. Hence start_host |
| 272 | method is introduced and must be initialized by HCD prior to registration. |
| 273 | |
| 274 | set_clk: HCD and DCD use this method to turn on/off USB_HS_CLK clk which is |
| 275 | required only while resetting the controller. |
| 276 | |
| 277 | start_srp: DCD use this method to initiate Session Request Protocol (SRP). |
| 278 | SRP may be initiated when function drivers use remote wakeup facility, when |
| 279 | B-Device wishes to become host. OTG driver programs Data-Pulsing if initial |
| 280 | condition of SRP are met. Otherwise proper error code is returned. |
| 281 | |
| 282 | set_suspend: DCD call this method when controller generates suspend |
| 283 | interrupt or generates reset/port change interrupt before HNP and during HNP. |
| 284 | If device is in B_PERIPHERAL state, HNP is initiated if host had enabled it. |
| 285 | If device is in A_PERIPHERAL state, A_BIDL_ADIS timer is kicked in case of |
| 286 | suspend interrupt. If this timer expires, A-device take back it's host role |
| 287 | and continue previous session. This timer is deleted in case of |
| 288 | reset/port change interrupts. |
| 289 | HCD call this method after suspending the root hub. Hardware is put into LPM. |
| 290 | |
| 291 | start_hnp: A-device needs to enable pull-up on D+ within TA_BIDL_ADIS after |
| 292 | suspending the bus i.e putting port in suspend state. EHCI stack can use this |
| 293 | method to notify OTG right after suspending the OTG port. OTG driver schedule |
| 294 | a work to stop host and enable pull-up on D+. |
| 295 | |
| 296 | send_event: USB core, DCD and HCD can use otg_send_event() API to send OTG |
| 297 | notification/error messages to user space. send_event method defined in |
| 298 | otg_transceiver is invoked by otg_send_event() API. An uevent is sent |
| 299 | with SUBSYSTEM=platform, MODULE=msm_otg and EVENT=<event>, where event could |
| 300 | be one of the following events. |
| 301 | |
| 302 | OTG_EVENT_DEV_CONN_TMOUT: Device connection timeout or device not responding. |
| 303 | OTG_EVENT_NO_RESP_FOR_HNP_ENABLE: Device is not responding to B_HNP_ENABLE |
| 304 | feature request. |
| 305 | OTG_EVENT_HUB_NOT_SUPPORTED: Host does not support HUB class peripherals. |
| 306 | OTG_EVENT_DEV_NOT_SUPPORTED: Host does not support the attached peripheral. |
| 307 | OTG_EVENT_HNP_FAILED: HNP failed due to not meeting protocol timings. |
| 308 | OTG_EVENT_NO_RESP_FOR_SRP: No Response for B-device SRP request. |
| 309 | |
| 310 | set_power: DCD can use otg_set_power() API to specify about the current that |
| 311 | can be withdrawn from the VBUS for charging. Based on the current OTG state |
| 312 | and whether ACA is attached or not, OTG driver makes a decision about the |
| 313 | charging current and calls the charging APIs. |
| 314 | |
| 315 | The following sysfs nodes are provided at /sys/devices/platform/msm_otg |
| 316 | |
| 317 | pwr_down: This node can be used to control A-device session. a_bus_drop and |
| 318 | a_bus_req state machine input variables are altered to start/stop session. |
| 319 | Write to this node is invalid when device operating as a B-device. |
| 320 | |
| 321 | start_srp: This node can be used for requesting session. If all initial |
| 322 | conditions of SRP are met, SRP is initiated. Write to this node is invalid |
| 323 | when device operating as an A-device. |
| 324 | |
| 325 | clr_err: This node can be used to clear over-current condition. Write to this |
| 326 | node is invalid when operating as an B-device. Error condition is |
| 327 | automatically cleared when Id becomes false. |
| 328 | |
| 329 | The following sysfs nodes are provided at /sys/devices/platform/msm_hsusb/otg |
| 330 | |
| 331 | host_request: This node can be used for requesting host role. A-device shall |
| 332 | select a_hnp_support feature prior to configuration and poll B-device for host |
| 333 | request. When '1' is written to this node, host request is asserted. This node |
| 334 | can also be used for taking host role when A-device operating as a peripheral. |
| 335 | |
| 336 | hnp_avail: User space can check this node before requesting the host role. |
| 337 | Gadget controller driver asserts its internal variable hnp_avail when HNP |
| 338 | polling request is send by the Host. |
| 339 | |
| 340 | Dependencies |
| 341 | ============ |
| 342 | |
| 343 | If USB clocks are controlled by modem processor, proc_comm interface is used |
| 344 | to turn on/off clocks. |
| 345 | |
| 346 | If VBUS power is controlled by modem processor, RPC interface is used to turn |
| 347 | on/off VBUS power. |
| 348 | |
| 349 | Config options |
| 350 | ============== |
| 351 | |
| 352 | CONFIG_USB_MSM_ACA: Enable support for Accessory Charger Adapter (ACA) |
| 353 | CONFIG_ENABLE_MSM_OTG_A_WAIT_BCON_TIMEOUT: Enable A_WAIT_BCON timeout. VBUS |
| 354 | will be turned off and SRP detection is enabled upon this timeout. If this |
| 355 | config is not selected, VBUS will not be turned off when Mini/Micro-A cable |
| 356 | is connected. But hardware is put into LPM. |
| 357 | |
| 358 | Other |
| 359 | ===== |
| 360 | On-The-Go and Embedded Host Supplement to the USB Revision 2.0 Specification |
| 361 | (Revision 2.0) found at http://www.usb.org/developers/onthego |
| 362 | |
| 363 | Known issues |
| 364 | ============ |
| 365 | Phy instability issues are observed when vbus_valid interrupt is enabled. |
| 366 | Hence a_vbus_vld state machine variable is explicitly asserted after |
| 367 | a_wait_vrise timer expiration. |
| 368 | |
| 369 | Spurious interrupt is seen when trying to put PHY in Low Power Mode with |
| 370 | ID_A/B/C interrupts enabled in the PHY. As a result of which PHY doesn't stay |
| 371 | in LPM. Hence, ID_A/B/C interrupts are disabled before entering LPM, and |
| 372 | enabled while returning. |
| 373 | |
| 374 | To do |
| 375 | ===== |
| 376 | |
| 377 | Verify SRP detection on all targets. |
| 378 | |
| 379 | Phy instability issues are observed when A-Vbus Valid interrupt is enabled. |
| 380 | But without this interrupt over current condition can not be determined. Root |
| 381 | cause analysis for PHY instability issue and alternative methods like PMIC |
| 382 | interrupt are being pursued. |