ice40-hcd.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. Introduction
  2. ============
  3. USB UICC connectivity is required for MSM8x12. This SoC has only 1 USB
  4. controller which is used for peripheral mode and charging. Hence an external
  5. USB host controller over SPI is used to connect a USB UICC card. ICE40 FPGA
  6. based SPI to IC-USB (Inter-Chip USB) bridge chip is used.
  7. The ICE40 Host controller driver (ice40-hcd) is registered as a SPI protocol
  8. driver and interacts with the SPI subsystem on one side and interacts with the
  9. USB core on the other side.
  10. Hardware description
  11. ====================
  12. The ICE40 devices are SRAM-based FPGAs. The SRAM memory cells are volatile,
  13. meaning that once power is removed from the device, its configuration is lost
  14. and must be reloaded on the next power-up. An on-chip non-volatile configuration
  15. memory or an external SPI flash are not used to store the configuration data due
  16. to increased power consumption. Instead, the software loads the configuration
  17. data through SPI interface after powering up the bridge chip. Once the
  18. configuration data is programmed successfully, the bridge chip will be ready for
  19. the USB host controller operations.
  20. The ICE40 device has an interrupt signal apart from the standard SPI signals
  21. CSn, SCLK, MOSI and MISO. It has support for 25 to 50 MHz frequencies. The
  22. maximum operating frequency during configuration loading is 25 MHz.
  23. The bridge chip requires two power supplies, SPI_VCC (1.8v - 3.3v) and VCC_CORE
  24. (1.2v). The SPI_VCC manages the SPI slave portion and VCC_CORE manages the USB
  25. serial engine (SIE) portion. It requires a 19.2 MHz reference clock and a
  26. 32 MHz clock is required for remote wakeup detection during suspend.
  27. The configuration loading sequence:
  28. - Assert the RSTn pin. This keeps bridge chip in reset state after downloading
  29. the configuration data.
  30. - The bridge chip samples the SPI interface chip select pin during power-up and
  31. enters SPI slave mode if it is low. Drive the chip select pin low before
  32. powering up the bridge chip.
  33. - Power-up the bridge chip by enabling SPI_VCC and VCC_CORE
  34. - De-assert the chip select pin after 50 usec.
  35. - Transfer the configuration data over SPI. Note that the bridge chip requires
  36. 49 dummy clock cycles after sending the data.
  37. - The bridge chip indicates the status of the configuration loading via config
  38. done pin. It may take 50 usec to assert this pin.
  39. The 19.2 MHz clock should be supplied before de-asserting the RSTn pin. A PLL
  40. is used to generate a 48MHz clock signal that then creates a 12MHz clock signal
  41. by a divider. When the PLLOK bit is set in USB Transfer Result register, it
  42. indicates that the PLL output is locked to the input reference clock. When it
  43. is 0, it indicates that the PLL is out of lock. It is recommended to assert the
  44. RSTn pin to re-synchronize the PLL to the reference clock when the PLL loses
  45. lock. The chip will be ready for the USB host controller operations after it is
  46. brought out of reset and PLL is synchronized to the reference clock.
  47. The software is responsible for initiating all the USB host transfers by writing
  48. the associated registers. The SIE in the bridge chip performs the USB host
  49. operations via the IC-USB bus based on the registers set by the software. The
  50. USB transfer results as well as the bus status like the peripheral connection,
  51. disconnection, resume, etc. are notified to software through the interrupt and
  52. the internal registers.
  53. The bridge chip provides the DP & DM pull-down resistor control to the software.
  54. The pull-down resistors are enabled automatically after the power up to force
  55. the SE0 condition on the bus. The software is required to disable these
  56. resistors before driving the reset on the bus. Control, Bulk and Interrupt
  57. transfers are supported. The data toggling states are not maintained in the
  58. hardware and should be serviced by the software. The bridge chip returns
  59. one of the following values for a USB transaction (SETUP/IN/OUT) via Transfer
  60. result register.
  61. xSUCCESS: Successful transfer.
  62. xBUSY: The SIE is busy with a USB transfer.
  63. xPKTERR: Packet Error (stuff, EOP).
  64. xPIDERR: PID check bits are incorrect.
  65. xNAK: Device returned NAK. This is not an error condition for IN/OUT. But it
  66. is an error condition for SETUP.
  67. xSTALL: Device returned STALL.
  68. xWRONGPID: Wrong PID is received. For example a IN transaction is attempted on
  69. OUT endpoint.
  70. xCRCERR: CRC error.
  71. xTOGERR: Toggle bit error. The SIE returns ACK when the toggle mismatch happens
  72. for IN transaction and returns this error code. Software should discard the
  73. data as it was received already in the previous transaction.
  74. xBADLEN: Too big packet size received.
  75. xTIMEOUT: Device failed to respond in time.
  76. Software description
  77. ====================
  78. This driver is compiled as a module and is loaded by the userspace after
  79. getting the UICC card insertion event from the modem processor. The module is
  80. unloaded upon the UICC card removal.
  81. This driver registers as a SPI protocol driver. The SPI controller driver
  82. manages the chip select pin. This pin needs to be driven low before powering
  83. up the bridge chip. Hence this pin settings are overridden temporarily during
  84. the bridge chip power-up sequence. The original settings are restored before
  85. sending the configuration data to the bridge chip which acts as a SPI slave.
  86. Both pinctl and gpiomux framework allow this type of use case.
  87. The configuration data file is stored on the eMMC card. Firmware class API
  88. request_firmware() is used to read the configuration data file. The
  89. configuration data is then sent to the bridge chip via SPI interface. The
  90. bridge chip asserts the config done pin once the configuration is completed.
  91. The driver registers as a Full Speed (USB 1.1) HCD. The following methods
  92. are implemented that are part of hc_drive struct:
  93. reset: It is called one time by the core during HCD registration. The
  94. default address 0 is programmed and the line state is sampled to check if any
  95. device is connected. If any device is connected, the port flags are updated
  96. accordingly. As the module is loaded after the UICC card is inserted, the
  97. device would be present at this time.
  98. start: This method is called one time by the core during HCD registration.
  99. The bridge chip is programmed to transmit the SOFs.
  100. stop: The method is called one time by the core during HCD deregistration.
  101. The bridge chip is programmed to stop transmitting the SOFs.
  102. hub_control: This method is called by the core to manage the Root HUB. The
  103. hardware does not maintain port state. The software maintain the port
  104. state and provide the information to the core when required. The following
  105. HUB class requests are supported.
  106. - GetHubDescriptor: The HUB descriptor is sent to the core. Only 1 port
  107. is present. Over current protection and port power control are not supported.
  108. - SetPortFeature: The device reset and suspend are supported. The The DP & DM
  109. pull-down resistors are disabled before driving the reset as per the IC-USB
  110. spec. The reset signaling is stopped when the core queries the port status.
  111. - GetPortStatus: The device connection status is sent to the core. If a reset
  112. is in progress, it is stopped before returning the port status.
  113. - ClearPortFeature: The device resume (clear suspend) is supported.
  114. urb_enqueue: This method is called by the core to initiate a USB Control/Bulk
  115. transfer. If the endpoint private context is not present, it will be created to
  116. hold the endpoint number, host endpoint structure, transaction error count, halt
  117. state and unlink state. The URB is attached to the endpoint URB list. If the
  118. endpoint is not active, it is attached to the asynchronous schedule list and the
  119. work is scheduled to traverse this list. The traversal algorithm is explained
  120. later in this document.
  121. urb_dequeue: This method is called by the core when an URB is unlinked. If the
  122. endpoint is not active, the URB is unlinked immediately. Otherwise the endpoint
  123. is marked for unlink and URB is unlinked from the asynchronous schedule work.
  124. bus_suspend: This method is called by the core during root hub suspend. The SOFs
  125. are already stopped during the port suspend which happens before root hub
  126. suspend. Assert the RSTn pin to put the bridge chip in reset state and stop XO
  127. (19.2 MHz) clock.
  128. bus_resume: This method is called by the core during root hub resume. Turn on
  129. the XO clock and de-assert the RSTn signal to bring the chip out of reset.
  130. endpoint_disable: This method is called by the core during the device
  131. disconnect. All the URB are unlinked by this time, so free the endpoint private
  132. structure.
  133. Asynchronous scheduling:
  134. All the active endpoints are queued to the asynchronous schedule list. A worker
  135. thread iterates over this circular list and process the URBs. Processing an URB
  136. involves initiating multiple SETUP/IN/OUT transactions and checking the result.
  137. After receiving the DATA/ACK, the toggle bit is inverted.
  138. A URB is finished when any of the following events occur:
  139. - The entire data is received for an OUT endpoint or a short packet is received
  140. for an IN endpoint.
  141. - The endpoint is stalled by the device. -EPIPE is returned.
  142. - Transaction error is occurred consecutively 3 times. -EPROTO is returned.
  143. - A NAK received for a SETUP transaction.
  144. - The URB is unlinked.
  145. The next transaction is issued on the next endpoint (if available) irrespective
  146. of the result of the current transaction. But the IN/OUT transaction of data
  147. or status phase is attempted immediately after the SETUP transaction for a
  148. control endpoint. If a NAK is received for this transaction, the control
  149. transfer is resumed next time when the control endpoint is encountered in the
  150. asynchronous schedule list. This is to give the control transfers priority
  151. over the bulk transfers.
  152. The endpoint is marked as halted when a URB is finished due to transaction
  153. errors or stall condition. The halted endpoint is removed from the asynchronous
  154. schedule list. It will be added again next time when a URB is enqueued on this
  155. endpoint.
  156. This driver provides debugfs interface and exports a file called "command" under
  157. <debugfs root>/ice40 directory. The following strings can be echoed to this
  158. file.
  159. "poll": If the device is connected after the module is loaded, it will not be
  160. detected automatically. The bus is sampled when this string is echoed. If a
  161. device is connected, port flags are updated and core is notified about the
  162. device connect event.
  163. "rwtest": Function Address register is written and read back to validate the
  164. contents. This should NOT be used while the usb device is connected. This is
  165. strictly for debugging purpose.
  166. "dump": Dumps all the register values to the kernel log buffer.
  167. Design Goals:
  168. =============
  169. - Handle errors gracefully. Implement retry mechanism for transaction errors,
  170. memory failures. Mark HCD as dead for serious errors like SPI transaction
  171. errors to avoid further interactions with the attached USB device.
  172. - Keep the asynchronous schedule algorithm simple and efficient. Take advantage
  173. of the static configuration of the USB device. UICC cards has only CCID and Mass
  174. storage interfaces. These interface protocol allows only 1 active transfer on
  175. either in or out endpoint.
  176. - Add trace points to capture USB transactions.
  177. Driver parameters
  178. =================
  179. The driver is compiled as a module and it accepts the configuration data file
  180. name as a module param called "firmware". The default configuration file name
  181. is "ice40.bin".
  182. Config options
  183. ==============
  184. Set CONFIG_USB_SL811_HCD to m to compile this driver as a module. The driver
  185. should not be compiled statically, because the configuration data is not
  186. available during kernel boot.
  187. To do
  188. =====
  189. - The bridge chip has 2 IN FIFO and 2 OUT FIFO. Implement double buffering.
  190. - The bridge chip has an interrupt to indicate the transaction (IN/OUT)
  191. completion. The current implementation uses polling for simplicity and to avoid
  192. interrupt latencies. Evaluate interrupt approach.
  193. - The bridge chip can be completely power collapsed during suspend to avoid
  194. leakage currents. As the bridge chip does not have any non-volatile memory,
  195. the configuration data needs to be loaded during resume. This method has higher
  196. power savings with higher resume latencies. Evaluate this approach.
  197. - Implement Interrupt transfers if required.
  198. - The request_firmware() API copies the configuration data file to the kernel
  199. virtual memory. This memory can't be used for DMA. The current implementation
  200. copies this data into contiguous physical memory which is allocated via
  201. kmalloc. If this memory allocation fails, try to allocate multiple pages
  202. and submit the SPI message with multiple transfers.