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