msm_otg.txt 19 KB

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