core.h 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * core.h - DesignWare HS OTG Controller common declarations
  4. *
  5. * Copyright (C) 2004-2013 Synopsys, Inc.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The names of the above-listed copyright holders may not be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * ALTERNATIVELY, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") as published by the Free Software
  22. * Foundation; either version 2 of the License, or (at your option) any
  23. * later version.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #ifndef __DWC2_CORE_H__
  38. #define __DWC2_CORE_H__
  39. #include <linux/phy/phy.h>
  40. #include <linux/regulator/consumer.h>
  41. #include <linux/usb/gadget.h>
  42. #include <linux/usb/otg.h>
  43. #include <linux/usb/phy.h>
  44. #include "hw.h"
  45. /*
  46. * Suggested defines for tracers:
  47. * - no_printk: Disable tracing
  48. * - pr_info: Print this info to the console
  49. * - trace_printk: Print this info to trace buffer (good for verbose logging)
  50. */
  51. #define DWC2_TRACE_SCHEDULER no_printk
  52. #define DWC2_TRACE_SCHEDULER_VB no_printk
  53. /* Detailed scheduler tracing, but won't overwhelm console */
  54. #define dwc2_sch_dbg(hsotg, fmt, ...) \
  55. DWC2_TRACE_SCHEDULER(pr_fmt("%s: SCH: " fmt), \
  56. dev_name(hsotg->dev), ##__VA_ARGS__)
  57. /* Verbose scheduler tracing */
  58. #define dwc2_sch_vdbg(hsotg, fmt, ...) \
  59. DWC2_TRACE_SCHEDULER_VB(pr_fmt("%s: SCH: " fmt), \
  60. dev_name(hsotg->dev), ##__VA_ARGS__)
  61. /* Maximum number of Endpoints/HostChannels */
  62. #define MAX_EPS_CHANNELS 16
  63. /* dwc2-hsotg declarations */
  64. static const char * const dwc2_hsotg_supply_names[] = {
  65. "vusb_d", /* digital USB supply, 1.2V */
  66. "vusb_a", /* analog USB supply, 1.1V */
  67. };
  68. #define DWC2_NUM_SUPPLIES ARRAY_SIZE(dwc2_hsotg_supply_names)
  69. /*
  70. * EP0_MPS_LIMIT
  71. *
  72. * Unfortunately there seems to be a limit of the amount of data that can
  73. * be transferred by IN transactions on EP0. This is either 127 bytes or 3
  74. * packets (which practically means 1 packet and 63 bytes of data) when the
  75. * MPS is set to 64.
  76. *
  77. * This means if we are wanting to move >127 bytes of data, we need to
  78. * split the transactions up, but just doing one packet at a time does
  79. * not work (this may be an implicit DATA0 PID on first packet of the
  80. * transaction) and doing 2 packets is outside the controller's limits.
  81. *
  82. * If we try to lower the MPS size for EP0, then no transfers work properly
  83. * for EP0, and the system will fail basic enumeration. As no cause for this
  84. * has currently been found, we cannot support any large IN transfers for
  85. * EP0.
  86. */
  87. #define EP0_MPS_LIMIT 64
  88. struct dwc2_hsotg;
  89. struct dwc2_hsotg_req;
  90. /**
  91. * struct dwc2_hsotg_ep - driver endpoint definition.
  92. * @ep: The gadget layer representation of the endpoint.
  93. * @name: The driver generated name for the endpoint.
  94. * @queue: Queue of requests for this endpoint.
  95. * @parent: Reference back to the parent device structure.
  96. * @req: The current request that the endpoint is processing. This is
  97. * used to indicate an request has been loaded onto the endpoint
  98. * and has yet to be completed (maybe due to data move, or simply
  99. * awaiting an ack from the core all the data has been completed).
  100. * @debugfs: File entry for debugfs file for this endpoint.
  101. * @dir_in: Set to true if this endpoint is of the IN direction, which
  102. * means that it is sending data to the Host.
  103. * @map_dir: Set to the value of dir_in when the DMA buffer is mapped.
  104. * @index: The index for the endpoint registers.
  105. * @mc: Multi Count - number of transactions per microframe
  106. * @interval: Interval for periodic endpoints, in frames or microframes.
  107. * @name: The name array passed to the USB core.
  108. * @halted: Set if the endpoint has been halted.
  109. * @periodic: Set if this is a periodic ep, such as Interrupt
  110. * @isochronous: Set if this is a isochronous ep
  111. * @send_zlp: Set if we need to send a zero-length packet.
  112. * @desc_list_dma: The DMA address of descriptor chain currently in use.
  113. * @desc_list: Pointer to descriptor DMA chain head currently in use.
  114. * @desc_count: Count of entries within the DMA descriptor chain of EP.
  115. * @next_desc: index of next free descriptor in the ISOC chain under SW control.
  116. * @compl_desc: index of next descriptor to be completed by xFerComplete
  117. * @total_data: The total number of data bytes done.
  118. * @fifo_size: The size of the FIFO (for periodic IN endpoints)
  119. * @fifo_index: For Dedicated FIFO operation, only FIFO0 can be used for EP0.
  120. * @fifo_load: The amount of data loaded into the FIFO (periodic IN)
  121. * @last_load: The offset of data for the last start of request.
  122. * @size_loaded: The last loaded size for DxEPTSIZE for periodic IN
  123. * @target_frame: Targeted frame num to setup next ISOC transfer
  124. * @frame_overrun: Indicates SOF number overrun in DSTS
  125. *
  126. * This is the driver's state for each registered enpoint, allowing it
  127. * to keep track of transactions that need doing. Each endpoint has a
  128. * lock to protect the state, to try and avoid using an overall lock
  129. * for the host controller as much as possible.
  130. *
  131. * For periodic IN endpoints, we have fifo_size and fifo_load to try
  132. * and keep track of the amount of data in the periodic FIFO for each
  133. * of these as we don't have a status register that tells us how much
  134. * is in each of them. (note, this may actually be useless information
  135. * as in shared-fifo mode periodic in acts like a single-frame packet
  136. * buffer than a fifo)
  137. */
  138. struct dwc2_hsotg_ep {
  139. struct usb_ep ep;
  140. struct list_head queue;
  141. struct dwc2_hsotg *parent;
  142. struct dwc2_hsotg_req *req;
  143. struct dentry *debugfs;
  144. unsigned long total_data;
  145. unsigned int size_loaded;
  146. unsigned int last_load;
  147. unsigned int fifo_load;
  148. unsigned short fifo_size;
  149. unsigned short fifo_index;
  150. unsigned char dir_in;
  151. unsigned char map_dir;
  152. unsigned char index;
  153. unsigned char mc;
  154. u16 interval;
  155. unsigned int halted:1;
  156. unsigned int periodic:1;
  157. unsigned int isochronous:1;
  158. unsigned int send_zlp:1;
  159. unsigned int target_frame;
  160. #define TARGET_FRAME_INITIAL 0xFFFFFFFF
  161. bool frame_overrun;
  162. dma_addr_t desc_list_dma;
  163. struct dwc2_dma_desc *desc_list;
  164. u8 desc_count;
  165. unsigned int next_desc;
  166. unsigned int compl_desc;
  167. char name[10];
  168. };
  169. /**
  170. * struct dwc2_hsotg_req - data transfer request
  171. * @req: The USB gadget request
  172. * @queue: The list of requests for the endpoint this is queued for.
  173. * @saved_req_buf: variable to save req.buf when bounce buffers are used.
  174. */
  175. struct dwc2_hsotg_req {
  176. struct usb_request req;
  177. struct list_head queue;
  178. void *saved_req_buf;
  179. };
  180. #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
  181. IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
  182. #define call_gadget(_hs, _entry) \
  183. do { \
  184. if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
  185. (_hs)->driver && (_hs)->driver->_entry) { \
  186. spin_unlock(&_hs->lock); \
  187. (_hs)->driver->_entry(&(_hs)->gadget); \
  188. spin_lock(&_hs->lock); \
  189. } \
  190. } while (0)
  191. #else
  192. #define call_gadget(_hs, _entry) do {} while (0)
  193. #endif
  194. struct dwc2_hsotg;
  195. struct dwc2_host_chan;
  196. /* Device States */
  197. enum dwc2_lx_state {
  198. DWC2_L0, /* On state */
  199. DWC2_L1, /* LPM sleep state */
  200. DWC2_L2, /* USB suspend state */
  201. DWC2_L3, /* Off state */
  202. };
  203. /* Gadget ep0 states */
  204. enum dwc2_ep0_state {
  205. DWC2_EP0_SETUP,
  206. DWC2_EP0_DATA_IN,
  207. DWC2_EP0_DATA_OUT,
  208. DWC2_EP0_STATUS_IN,
  209. DWC2_EP0_STATUS_OUT,
  210. };
  211. /**
  212. * struct dwc2_core_params - Parameters for configuring the core
  213. *
  214. * @otg_cap: Specifies the OTG capabilities.
  215. * 0 - HNP and SRP capable
  216. * 1 - SRP Only capable
  217. * 2 - No HNP/SRP capable (always available)
  218. * Defaults to best available option (0, 1, then 2)
  219. * @host_dma: Specifies whether to use slave or DMA mode for accessing
  220. * the data FIFOs. The driver will automatically detect the
  221. * value for this parameter if none is specified.
  222. * 0 - Slave (always available)
  223. * 1 - DMA (default, if available)
  224. * @dma_desc_enable: When DMA mode is enabled, specifies whether to use
  225. * address DMA mode or descriptor DMA mode for accessing
  226. * the data FIFOs. The driver will automatically detect the
  227. * value for this if none is specified.
  228. * 0 - Address DMA
  229. * 1 - Descriptor DMA (default, if available)
  230. * @dma_desc_fs_enable: When DMA mode is enabled, specifies whether to use
  231. * address DMA mode or descriptor DMA mode for accessing
  232. * the data FIFOs in Full Speed mode only. The driver
  233. * will automatically detect the value for this if none is
  234. * specified.
  235. * 0 - Address DMA
  236. * 1 - Descriptor DMA in FS (default, if available)
  237. * @speed: Specifies the maximum speed of operation in host and
  238. * device mode. The actual speed depends on the speed of
  239. * the attached device and the value of phy_type.
  240. * 0 - High Speed
  241. * (default when phy_type is UTMI+ or ULPI)
  242. * 1 - Full Speed
  243. * (default when phy_type is Full Speed)
  244. * @enable_dynamic_fifo: 0 - Use coreConsultant-specified FIFO size parameters
  245. * 1 - Allow dynamic FIFO sizing (default, if available)
  246. * @en_multiple_tx_fifo: Specifies whether dedicated per-endpoint transmit FIFOs
  247. * are enabled for non-periodic IN endpoints in device
  248. * mode.
  249. * @host_rx_fifo_size: Number of 4-byte words in the Rx FIFO in host mode when
  250. * dynamic FIFO sizing is enabled
  251. * 16 to 32768
  252. * Actual maximum value is autodetected and also
  253. * the default.
  254. * @host_nperio_tx_fifo_size: Number of 4-byte words in the non-periodic Tx FIFO
  255. * in host mode when dynamic FIFO sizing is enabled
  256. * 16 to 32768
  257. * Actual maximum value is autodetected and also
  258. * the default.
  259. * @host_perio_tx_fifo_size: Number of 4-byte words in the periodic Tx FIFO in
  260. * host mode when dynamic FIFO sizing is enabled
  261. * 16 to 32768
  262. * Actual maximum value is autodetected and also
  263. * the default.
  264. * @max_transfer_size: The maximum transfer size supported, in bytes
  265. * 2047 to 65,535
  266. * Actual maximum value is autodetected and also
  267. * the default.
  268. * @max_packet_count: The maximum number of packets in a transfer
  269. * 15 to 511
  270. * Actual maximum value is autodetected and also
  271. * the default.
  272. * @host_channels: The number of host channel registers to use
  273. * 1 to 16
  274. * Actual maximum value is autodetected and also
  275. * the default.
  276. * @phy_type: Specifies the type of PHY interface to use. By default,
  277. * the driver will automatically detect the phy_type.
  278. * 0 - Full Speed Phy
  279. * 1 - UTMI+ Phy
  280. * 2 - ULPI Phy
  281. * Defaults to best available option (2, 1, then 0)
  282. * @phy_utmi_width: Specifies the UTMI+ Data Width (in bits). This parameter
  283. * is applicable for a phy_type of UTMI+ or ULPI. (For a
  284. * ULPI phy_type, this parameter indicates the data width
  285. * between the MAC and the ULPI Wrapper.) Also, this
  286. * parameter is applicable only if the OTG_HSPHY_WIDTH cC
  287. * parameter was set to "8 and 16 bits", meaning that the
  288. * core has been configured to work at either data path
  289. * width.
  290. * 8 or 16 (default 16 if available)
  291. * @phy_ulpi_ddr: Specifies whether the ULPI operates at double or single
  292. * data rate. This parameter is only applicable if phy_type
  293. * is ULPI.
  294. * 0 - single data rate ULPI interface with 8 bit wide
  295. * data bus (default)
  296. * 1 - double data rate ULPI interface with 4 bit wide
  297. * data bus
  298. * @phy_ulpi_ext_vbus: For a ULPI phy, specifies whether to use the internal or
  299. * external supply to drive the VBus
  300. * 0 - Internal supply (default)
  301. * 1 - External supply
  302. * @i2c_enable: Specifies whether to use the I2Cinterface for a full
  303. * speed PHY. This parameter is only applicable if phy_type
  304. * is FS.
  305. * 0 - No (default)
  306. * 1 - Yes
  307. * @ipg_isoc_en: Indicates the IPG supports is enabled or disabled.
  308. * 0 - Disable (default)
  309. * 1 - Enable
  310. * @acg_enable: For enabling Active Clock Gating in the controller
  311. * 0 - No
  312. * 1 - Yes
  313. * @ulpi_fs_ls: Make ULPI phy operate in FS/LS mode only
  314. * 0 - No (default)
  315. * 1 - Yes
  316. * @host_support_fs_ls_low_power: Specifies whether low power mode is supported
  317. * when attached to a Full Speed or Low Speed device in
  318. * host mode.
  319. * 0 - Don't support low power mode (default)
  320. * 1 - Support low power mode
  321. * @host_ls_low_power_phy_clk: Specifies the PHY clock rate in low power mode
  322. * when connected to a Low Speed device in host
  323. * mode. This parameter is applicable only if
  324. * host_support_fs_ls_low_power is enabled.
  325. * 0 - 48 MHz
  326. * (default when phy_type is UTMI+ or ULPI)
  327. * 1 - 6 MHz
  328. * (default when phy_type is Full Speed)
  329. * @oc_disable: Flag to disable overcurrent condition.
  330. * 0 - Allow overcurrent condition to get detected
  331. * 1 - Disable overcurrent condtion to get detected
  332. * @ts_dline: Enable Term Select Dline pulsing
  333. * 0 - No (default)
  334. * 1 - Yes
  335. * @reload_ctl: Allow dynamic reloading of HFIR register during runtime
  336. * 0 - No (default for core < 2.92a)
  337. * 1 - Yes (default for core >= 2.92a)
  338. * @ahbcfg: This field allows the default value of the GAHBCFG
  339. * register to be overridden
  340. * -1 - GAHBCFG value will be set to 0x06
  341. * (INCR, default)
  342. * all others - GAHBCFG value will be overridden with
  343. * this value
  344. * Not all bits can be controlled like this, the
  345. * bits defined by GAHBCFG_CTRL_MASK are controlled
  346. * by the driver and are ignored in this
  347. * configuration value.
  348. * @uframe_sched: True to enable the microframe scheduler
  349. * @external_id_pin_ctl: Specifies whether ID pin is handled externally.
  350. * Disable CONIDSTSCHNG controller interrupt in such
  351. * case.
  352. * 0 - No (default)
  353. * 1 - Yes
  354. * @power_down: Specifies whether the controller support power_down.
  355. * If power_down is enabled, the controller will enter
  356. * power_down in both peripheral and host mode when
  357. * needed.
  358. * 0 - No (default)
  359. * 1 - Partial power down
  360. * 2 - Hibernation
  361. * @lpm: Enable LPM support.
  362. * 0 - No
  363. * 1 - Yes
  364. * @lpm_clock_gating: Enable core PHY clock gating.
  365. * 0 - No
  366. * 1 - Yes
  367. * @besl: Enable LPM Errata support.
  368. * 0 - No
  369. * 1 - Yes
  370. * @hird_threshold_en: HIRD or HIRD Threshold enable.
  371. * 0 - No
  372. * 1 - Yes
  373. * @hird_threshold: Value of BESL or HIRD Threshold.
  374. * @activate_stm_fs_transceiver: Activate internal transceiver using GGPIO
  375. * register.
  376. * 0 - Deactivate the transceiver (default)
  377. * 1 - Activate the transceiver
  378. * @g_dma: Enables gadget dma usage (default: autodetect).
  379. * @g_dma_desc: Enables gadget descriptor DMA (default: autodetect).
  380. * @g_rx_fifo_size: The periodic rx fifo size for the device, in
  381. * DWORDS from 16-32768 (default: 2048 if
  382. * possible, otherwise autodetect).
  383. * @g_np_tx_fifo_size: The non-periodic tx fifo size for the device in
  384. * DWORDS from 16-32768 (default: 1024 if
  385. * possible, otherwise autodetect).
  386. * @g_tx_fifo_size: An array of TX fifo sizes in dedicated fifo
  387. * mode. Each value corresponds to one EP
  388. * starting from EP1 (max 15 values). Sizes are
  389. * in DWORDS with possible values from from
  390. * 16-32768 (default: 256, 256, 256, 256, 768,
  391. * 768, 768, 768, 0, 0, 0, 0, 0, 0, 0).
  392. * @change_speed_quirk: Change speed configuration to DWC2_SPEED_PARAM_FULL
  393. * while full&low speed device connect. And change speed
  394. * back to DWC2_SPEED_PARAM_HIGH while device is gone.
  395. * 0 - No (default)
  396. * 1 - Yes
  397. *
  398. * The following parameters may be specified when starting the module. These
  399. * parameters define how the DWC_otg controller should be configured. A
  400. * value of -1 (or any other out of range value) for any parameter means
  401. * to read the value from hardware (if possible) or use the builtin
  402. * default described above.
  403. */
  404. struct dwc2_core_params {
  405. u8 otg_cap;
  406. #define DWC2_CAP_PARAM_HNP_SRP_CAPABLE 0
  407. #define DWC2_CAP_PARAM_SRP_ONLY_CAPABLE 1
  408. #define DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
  409. u8 phy_type;
  410. #define DWC2_PHY_TYPE_PARAM_FS 0
  411. #define DWC2_PHY_TYPE_PARAM_UTMI 1
  412. #define DWC2_PHY_TYPE_PARAM_ULPI 2
  413. u8 speed;
  414. #define DWC2_SPEED_PARAM_HIGH 0
  415. #define DWC2_SPEED_PARAM_FULL 1
  416. #define DWC2_SPEED_PARAM_LOW 2
  417. u8 phy_utmi_width;
  418. bool phy_ulpi_ddr;
  419. bool phy_ulpi_ext_vbus;
  420. bool enable_dynamic_fifo;
  421. bool en_multiple_tx_fifo;
  422. bool i2c_enable;
  423. bool acg_enable;
  424. bool ulpi_fs_ls;
  425. bool ts_dline;
  426. bool reload_ctl;
  427. bool uframe_sched;
  428. bool external_id_pin_ctl;
  429. int power_down;
  430. #define DWC2_POWER_DOWN_PARAM_NONE 0
  431. #define DWC2_POWER_DOWN_PARAM_PARTIAL 1
  432. #define DWC2_POWER_DOWN_PARAM_HIBERNATION 2
  433. bool lpm;
  434. bool lpm_clock_gating;
  435. bool besl;
  436. bool hird_threshold_en;
  437. u8 hird_threshold;
  438. bool activate_stm_fs_transceiver;
  439. bool ipg_isoc_en;
  440. u16 max_packet_count;
  441. u32 max_transfer_size;
  442. u32 ahbcfg;
  443. /* Host parameters */
  444. bool host_dma;
  445. bool dma_desc_enable;
  446. bool dma_desc_fs_enable;
  447. bool host_support_fs_ls_low_power;
  448. bool host_ls_low_power_phy_clk;
  449. bool oc_disable;
  450. u8 host_channels;
  451. u16 host_rx_fifo_size;
  452. u16 host_nperio_tx_fifo_size;
  453. u16 host_perio_tx_fifo_size;
  454. /* Gadget parameters */
  455. bool g_dma;
  456. bool g_dma_desc;
  457. u32 g_rx_fifo_size;
  458. u32 g_np_tx_fifo_size;
  459. u32 g_tx_fifo_size[MAX_EPS_CHANNELS];
  460. bool change_speed_quirk;
  461. };
  462. /**
  463. * struct dwc2_hw_params - Autodetected parameters.
  464. *
  465. * These parameters are the various parameters read from hardware
  466. * registers during initialization. They typically contain the best
  467. * supported or maximum value that can be configured in the
  468. * corresponding dwc2_core_params value.
  469. *
  470. * The values that are not in dwc2_core_params are documented below.
  471. *
  472. * @op_mode: Mode of Operation
  473. * 0 - HNP- and SRP-Capable OTG (Host & Device)
  474. * 1 - SRP-Capable OTG (Host & Device)
  475. * 2 - Non-HNP and Non-SRP Capable OTG (Host & Device)
  476. * 3 - SRP-Capable Device
  477. * 4 - Non-OTG Device
  478. * 5 - SRP-Capable Host
  479. * 6 - Non-OTG Host
  480. * @arch: Architecture
  481. * 0 - Slave only
  482. * 1 - External DMA
  483. * 2 - Internal DMA
  484. * @ipg_isoc_en: This feature indicates that the controller supports
  485. * the worst-case scenario of Rx followed by Rx
  486. * Interpacket Gap (IPG) (32 bitTimes) as per the utmi
  487. * specification for any token following ISOC OUT token.
  488. * 0 - Don't support
  489. * 1 - Support
  490. * @power_optimized: Are power optimizations enabled?
  491. * @num_dev_ep: Number of device endpoints available
  492. * @num_dev_in_eps: Number of device IN endpoints available
  493. * @num_dev_perio_in_ep: Number of device periodic IN endpoints
  494. * available
  495. * @dev_token_q_depth: Device Mode IN Token Sequence Learning Queue
  496. * Depth
  497. * 0 to 30
  498. * @host_perio_tx_q_depth:
  499. * Host Mode Periodic Request Queue Depth
  500. * 2, 4 or 8
  501. * @nperio_tx_q_depth:
  502. * Non-Periodic Request Queue Depth
  503. * 2, 4 or 8
  504. * @hs_phy_type: High-speed PHY interface type
  505. * 0 - High-speed interface not supported
  506. * 1 - UTMI+
  507. * 2 - ULPI
  508. * 3 - UTMI+ and ULPI
  509. * @fs_phy_type: Full-speed PHY interface type
  510. * 0 - Full speed interface not supported
  511. * 1 - Dedicated full speed interface
  512. * 2 - FS pins shared with UTMI+ pins
  513. * 3 - FS pins shared with ULPI pins
  514. * @total_fifo_size: Total internal RAM for FIFOs (bytes)
  515. * @hibernation: Is hibernation enabled?
  516. * @utmi_phy_data_width: UTMI+ PHY data width
  517. * 0 - 8 bits
  518. * 1 - 16 bits
  519. * 2 - 8 or 16 bits
  520. * @snpsid: Value from SNPSID register
  521. * @dev_ep_dirs: Direction of device endpoints (GHWCFG1)
  522. * @g_tx_fifo_size: Power-on values of TxFIFO sizes
  523. * @dma_desc_enable: When DMA mode is enabled, specifies whether to use
  524. * address DMA mode or descriptor DMA mode for accessing
  525. * the data FIFOs. The driver will automatically detect the
  526. * value for this if none is specified.
  527. * 0 - Address DMA
  528. * 1 - Descriptor DMA (default, if available)
  529. * @enable_dynamic_fifo: 0 - Use coreConsultant-specified FIFO size parameters
  530. * 1 - Allow dynamic FIFO sizing (default, if available)
  531. * @en_multiple_tx_fifo: Specifies whether dedicated per-endpoint transmit FIFOs
  532. * are enabled for non-periodic IN endpoints in device
  533. * mode.
  534. * @host_nperio_tx_fifo_size: Number of 4-byte words in the non-periodic Tx FIFO
  535. * in host mode when dynamic FIFO sizing is enabled
  536. * 16 to 32768
  537. * Actual maximum value is autodetected and also
  538. * the default.
  539. * @host_perio_tx_fifo_size: Number of 4-byte words in the periodic Tx FIFO in
  540. * host mode when dynamic FIFO sizing is enabled
  541. * 16 to 32768
  542. * Actual maximum value is autodetected and also
  543. * the default.
  544. * @max_transfer_size: The maximum transfer size supported, in bytes
  545. * 2047 to 65,535
  546. * Actual maximum value is autodetected and also
  547. * the default.
  548. * @max_packet_count: The maximum number of packets in a transfer
  549. * 15 to 511
  550. * Actual maximum value is autodetected and also
  551. * the default.
  552. * @host_channels: The number of host channel registers to use
  553. * 1 to 16
  554. * Actual maximum value is autodetected and also
  555. * the default.
  556. * @dev_nperio_tx_fifo_size: Number of 4-byte words in the non-periodic Tx FIFO
  557. * in device mode when dynamic FIFO sizing is enabled
  558. * 16 to 32768
  559. * Actual maximum value is autodetected and also
  560. * the default.
  561. * @i2c_enable: Specifies whether to use the I2Cinterface for a full
  562. * speed PHY. This parameter is only applicable if phy_type
  563. * is FS.
  564. * 0 - No (default)
  565. * 1 - Yes
  566. * @acg_enable: For enabling Active Clock Gating in the controller
  567. * 0 - Disable
  568. * 1 - Enable
  569. * @lpm_mode: For enabling Link Power Management in the controller
  570. * 0 - Disable
  571. * 1 - Enable
  572. * @rx_fifo_size: Number of 4-byte words in the Rx FIFO when dynamic
  573. * FIFO sizing is enabled 16 to 32768
  574. * Actual maximum value is autodetected and also
  575. * the default.
  576. */
  577. struct dwc2_hw_params {
  578. unsigned op_mode:3;
  579. unsigned arch:2;
  580. unsigned dma_desc_enable:1;
  581. unsigned enable_dynamic_fifo:1;
  582. unsigned en_multiple_tx_fifo:1;
  583. unsigned rx_fifo_size:16;
  584. unsigned host_nperio_tx_fifo_size:16;
  585. unsigned dev_nperio_tx_fifo_size:16;
  586. unsigned host_perio_tx_fifo_size:16;
  587. unsigned nperio_tx_q_depth:3;
  588. unsigned host_perio_tx_q_depth:3;
  589. unsigned dev_token_q_depth:5;
  590. unsigned max_transfer_size:26;
  591. unsigned max_packet_count:11;
  592. unsigned host_channels:5;
  593. unsigned hs_phy_type:2;
  594. unsigned fs_phy_type:2;
  595. unsigned i2c_enable:1;
  596. unsigned acg_enable:1;
  597. unsigned num_dev_ep:4;
  598. unsigned num_dev_in_eps : 4;
  599. unsigned num_dev_perio_in_ep:4;
  600. unsigned total_fifo_size:16;
  601. unsigned power_optimized:1;
  602. unsigned hibernation:1;
  603. unsigned utmi_phy_data_width:2;
  604. unsigned lpm_mode:1;
  605. unsigned ipg_isoc_en:1;
  606. u32 snpsid;
  607. u32 dev_ep_dirs;
  608. u32 g_tx_fifo_size[MAX_EPS_CHANNELS];
  609. };
  610. /* Size of control and EP0 buffers */
  611. #define DWC2_CTRL_BUFF_SIZE 8
  612. /**
  613. * struct dwc2_gregs_backup - Holds global registers state before
  614. * entering partial power down
  615. * @gotgctl: Backup of GOTGCTL register
  616. * @gintmsk: Backup of GINTMSK register
  617. * @gahbcfg: Backup of GAHBCFG register
  618. * @gusbcfg: Backup of GUSBCFG register
  619. * @grxfsiz: Backup of GRXFSIZ register
  620. * @gnptxfsiz: Backup of GNPTXFSIZ register
  621. * @gi2cctl: Backup of GI2CCTL register
  622. * @glpmcfg: Backup of GLPMCFG register
  623. * @gdfifocfg: Backup of GDFIFOCFG register
  624. * @pcgcctl: Backup of PCGCCTL register
  625. * @pcgcctl1: Backup of PCGCCTL1 register
  626. * @dtxfsiz: Backup of DTXFSIZ registers for each endpoint
  627. * @gpwrdn: Backup of GPWRDN register
  628. * @valid: True if registers values backuped.
  629. */
  630. struct dwc2_gregs_backup {
  631. u32 gotgctl;
  632. u32 gintmsk;
  633. u32 gahbcfg;
  634. u32 gusbcfg;
  635. u32 grxfsiz;
  636. u32 gnptxfsiz;
  637. u32 gi2cctl;
  638. u32 glpmcfg;
  639. u32 pcgcctl;
  640. u32 pcgcctl1;
  641. u32 gdfifocfg;
  642. u32 gpwrdn;
  643. bool valid;
  644. };
  645. /**
  646. * struct dwc2_dregs_backup - Holds device registers state before
  647. * entering partial power down
  648. * @dcfg: Backup of DCFG register
  649. * @dctl: Backup of DCTL register
  650. * @daintmsk: Backup of DAINTMSK register
  651. * @diepmsk: Backup of DIEPMSK register
  652. * @doepmsk: Backup of DOEPMSK register
  653. * @diepctl: Backup of DIEPCTL register
  654. * @dieptsiz: Backup of DIEPTSIZ register
  655. * @diepdma: Backup of DIEPDMA register
  656. * @doepctl: Backup of DOEPCTL register
  657. * @doeptsiz: Backup of DOEPTSIZ register
  658. * @doepdma: Backup of DOEPDMA register
  659. * @dtxfsiz: Backup of DTXFSIZ registers for each endpoint
  660. * @valid: True if registers values backuped.
  661. */
  662. struct dwc2_dregs_backup {
  663. u32 dcfg;
  664. u32 dctl;
  665. u32 daintmsk;
  666. u32 diepmsk;
  667. u32 doepmsk;
  668. u32 diepctl[MAX_EPS_CHANNELS];
  669. u32 dieptsiz[MAX_EPS_CHANNELS];
  670. u32 diepdma[MAX_EPS_CHANNELS];
  671. u32 doepctl[MAX_EPS_CHANNELS];
  672. u32 doeptsiz[MAX_EPS_CHANNELS];
  673. u32 doepdma[MAX_EPS_CHANNELS];
  674. u32 dtxfsiz[MAX_EPS_CHANNELS];
  675. bool valid;
  676. };
  677. /**
  678. * struct dwc2_hregs_backup - Holds host registers state before
  679. * entering partial power down
  680. * @hcfg: Backup of HCFG register
  681. * @haintmsk: Backup of HAINTMSK register
  682. * @hcintmsk: Backup of HCINTMSK register
  683. * @hprt0: Backup of HPTR0 register
  684. * @hfir: Backup of HFIR register
  685. * @hptxfsiz: Backup of HPTXFSIZ register
  686. * @valid: True if registers values backuped.
  687. */
  688. struct dwc2_hregs_backup {
  689. u32 hcfg;
  690. u32 haintmsk;
  691. u32 hcintmsk[MAX_EPS_CHANNELS];
  692. u32 hprt0;
  693. u32 hfir;
  694. u32 hptxfsiz;
  695. bool valid;
  696. };
  697. /*
  698. * Constants related to high speed periodic scheduling
  699. *
  700. * We have a periodic schedule that is DWC2_HS_SCHEDULE_UFRAMES long. From a
  701. * reservation point of view it's assumed that the schedule goes right back to
  702. * the beginning after the end of the schedule.
  703. *
  704. * What does that mean for scheduling things with a long interval? It means
  705. * we'll reserve time for them in every possible microframe that they could
  706. * ever be scheduled in. ...but we'll still only actually schedule them as
  707. * often as they were requested.
  708. *
  709. * We keep our schedule in a "bitmap" structure. This simplifies having
  710. * to keep track of and merge intervals: we just let the bitmap code do most
  711. * of the heavy lifting. In a way scheduling is much like memory allocation.
  712. *
  713. * We schedule 100us per uframe or 80% of 125us (the maximum amount you're
  714. * supposed to schedule for periodic transfers). That's according to spec.
  715. *
  716. * Note that though we only schedule 80% of each microframe, the bitmap that we
  717. * keep the schedule in is tightly packed (AKA it doesn't have 100us worth of
  718. * space for each uFrame).
  719. *
  720. * Requirements:
  721. * - DWC2_HS_SCHEDULE_UFRAMES must even divide 0x4000 (HFNUM_MAX_FRNUM + 1)
  722. * - DWC2_HS_SCHEDULE_UFRAMES must be 8 times DWC2_LS_SCHEDULE_FRAMES (probably
  723. * could be any multiple of 8 times DWC2_LS_SCHEDULE_FRAMES, but there might
  724. * be bugs). The 8 comes from the USB spec: number of microframes per frame.
  725. */
  726. #define DWC2_US_PER_UFRAME 125
  727. #define DWC2_HS_PERIODIC_US_PER_UFRAME 100
  728. #define DWC2_HS_SCHEDULE_UFRAMES 8
  729. #define DWC2_HS_SCHEDULE_US (DWC2_HS_SCHEDULE_UFRAMES * \
  730. DWC2_HS_PERIODIC_US_PER_UFRAME)
  731. /*
  732. * Constants related to low speed scheduling
  733. *
  734. * For high speed we schedule every 1us. For low speed that's a bit overkill,
  735. * so we make up a unit called a "slice" that's worth 25us. There are 40
  736. * slices in a full frame and we can schedule 36 of those (90%) for periodic
  737. * transfers.
  738. *
  739. * Our low speed schedule can be as short as 1 frame or could be longer. When
  740. * we only schedule 1 frame it means that we'll need to reserve a time every
  741. * frame even for things that only transfer very rarely, so something that runs
  742. * every 2048 frames will get time reserved in every frame. Our low speed
  743. * schedule can be longer and we'll be able to handle more overlap, but that
  744. * will come at increased memory cost and increased time to schedule.
  745. *
  746. * Note: one other advantage of a short low speed schedule is that if we mess
  747. * up and miss scheduling we can jump in and use any of the slots that we
  748. * happened to reserve.
  749. *
  750. * With 25 us per slice and 1 frame in the schedule, we only need 4 bytes for
  751. * the schedule. There will be one schedule per TT.
  752. *
  753. * Requirements:
  754. * - DWC2_US_PER_SLICE must evenly divide DWC2_LS_PERIODIC_US_PER_FRAME.
  755. */
  756. #define DWC2_US_PER_SLICE 25
  757. #define DWC2_SLICES_PER_UFRAME (DWC2_US_PER_UFRAME / DWC2_US_PER_SLICE)
  758. #define DWC2_ROUND_US_TO_SLICE(us) \
  759. (DIV_ROUND_UP((us), DWC2_US_PER_SLICE) * \
  760. DWC2_US_PER_SLICE)
  761. #define DWC2_LS_PERIODIC_US_PER_FRAME \
  762. 900
  763. #define DWC2_LS_PERIODIC_SLICES_PER_FRAME \
  764. (DWC2_LS_PERIODIC_US_PER_FRAME / \
  765. DWC2_US_PER_SLICE)
  766. #define DWC2_LS_SCHEDULE_FRAMES 1
  767. #define DWC2_LS_SCHEDULE_SLICES (DWC2_LS_SCHEDULE_FRAMES * \
  768. DWC2_LS_PERIODIC_SLICES_PER_FRAME)
  769. /**
  770. * struct dwc2_hsotg - Holds the state of the driver, including the non-periodic
  771. * and periodic schedules
  772. *
  773. * These are common for both host and peripheral modes:
  774. *
  775. * @dev: The struct device pointer
  776. * @regs: Pointer to controller regs
  777. * @hw_params: Parameters that were autodetected from the
  778. * hardware registers
  779. * @params: Parameters that define how the core should be configured
  780. * @op_state: The operational State, during transitions (a_host=>
  781. * a_peripheral and b_device=>b_host) this may not match
  782. * the core, but allows the software to determine
  783. * transitions
  784. * @dr_mode: Requested mode of operation, one of following:
  785. * - USB_DR_MODE_PERIPHERAL
  786. * - USB_DR_MODE_HOST
  787. * - USB_DR_MODE_OTG
  788. * @hcd_enabled: Host mode sub-driver initialization indicator.
  789. * @gadget_enabled: Peripheral mode sub-driver initialization indicator.
  790. * @ll_hw_enabled: Status of low-level hardware resources.
  791. * @hibernated: True if core is hibernated
  792. * @frame_number: Frame number read from the core. For both device
  793. * and host modes. The value ranges are from 0
  794. * to HFNUM_MAX_FRNUM.
  795. * @phy: The otg phy transceiver structure for phy control.
  796. * @uphy: The otg phy transceiver structure for old USB phy
  797. * control.
  798. * @plat: The platform specific configuration data. This can be
  799. * removed once all SoCs support usb transceiver.
  800. * @supplies: Definition of USB power supplies
  801. * @vbus_supply: Regulator supplying vbus.
  802. * @phyif: PHY interface width
  803. * @lock: Spinlock that protects all the driver data structures
  804. * @priv: Stores a pointer to the struct usb_hcd
  805. * @queuing_high_bandwidth: True if multiple packets of a high-bandwidth
  806. * transfer are in process of being queued
  807. * @srp_success: Stores status of SRP request in the case of a FS PHY
  808. * with an I2C interface
  809. * @wq_otg: Workqueue object used for handling of some interrupts
  810. * @wf_otg: Work object for handling Connector ID Status Change
  811. * interrupt
  812. * @wkp_timer: Timer object for handling Wakeup Detected interrupt
  813. * @lx_state: Lx state of connected device
  814. * @gr_backup: Backup of global registers during suspend
  815. * @dr_backup: Backup of device registers during suspend
  816. * @hr_backup: Backup of host registers during suspend
  817. * @needs_byte_swap: Specifies whether the opposite endianness.
  818. *
  819. * These are for host mode:
  820. *
  821. * @flags: Flags for handling root port state changes
  822. * @flags.d32: Contain all root port flags
  823. * @flags.b: Separate root port flags from each other
  824. * @flags.b.port_connect_status_change: True if root port connect status
  825. * changed
  826. * @flags.b.port_connect_status: True if device connected to root port
  827. * @flags.b.port_reset_change: True if root port reset status changed
  828. * @flags.b.port_enable_change: True if root port enable status changed
  829. * @flags.b.port_suspend_change: True if root port suspend status changed
  830. * @flags.b.port_over_current_change: True if root port over current state
  831. * changed.
  832. * @flags.b.port_l1_change: True if root port l1 status changed
  833. * @flags.b.reserved: Reserved bits of root port register
  834. * @non_periodic_sched_inactive: Inactive QHs in the non-periodic schedule.
  835. * Transfers associated with these QHs are not currently
  836. * assigned to a host channel.
  837. * @non_periodic_sched_active: Active QHs in the non-periodic schedule.
  838. * Transfers associated with these QHs are currently
  839. * assigned to a host channel.
  840. * @non_periodic_qh_ptr: Pointer to next QH to process in the active
  841. * non-periodic schedule
  842. * @non_periodic_sched_waiting: Waiting QHs in the non-periodic schedule.
  843. * Transfers associated with these QHs are not currently
  844. * assigned to a host channel.
  845. * @periodic_sched_inactive: Inactive QHs in the periodic schedule. This is a
  846. * list of QHs for periodic transfers that are _not_
  847. * scheduled for the next frame. Each QH in the list has an
  848. * interval counter that determines when it needs to be
  849. * scheduled for execution. This scheduling mechanism
  850. * allows only a simple calculation for periodic bandwidth
  851. * used (i.e. must assume that all periodic transfers may
  852. * need to execute in the same frame). However, it greatly
  853. * simplifies scheduling and should be sufficient for the
  854. * vast majority of OTG hosts, which need to connect to a
  855. * small number of peripherals at one time. Items move from
  856. * this list to periodic_sched_ready when the QH interval
  857. * counter is 0 at SOF.
  858. * @periodic_sched_ready: List of periodic QHs that are ready for execution in
  859. * the next frame, but have not yet been assigned to host
  860. * channels. Items move from this list to
  861. * periodic_sched_assigned as host channels become
  862. * available during the current frame.
  863. * @periodic_sched_assigned: List of periodic QHs to be executed in the next
  864. * frame that are assigned to host channels. Items move
  865. * from this list to periodic_sched_queued as the
  866. * transactions for the QH are queued to the DWC_otg
  867. * controller.
  868. * @periodic_sched_queued: List of periodic QHs that have been queued for
  869. * execution. Items move from this list to either
  870. * periodic_sched_inactive or periodic_sched_ready when the
  871. * channel associated with the transfer is released. If the
  872. * interval for the QH is 1, the item moves to
  873. * periodic_sched_ready because it must be rescheduled for
  874. * the next frame. Otherwise, the item moves to
  875. * periodic_sched_inactive.
  876. * @split_order: List keeping track of channels doing splits, in order.
  877. * @periodic_usecs: Total bandwidth claimed so far for periodic transfers.
  878. * This value is in microseconds per (micro)frame. The
  879. * assumption is that all periodic transfers may occur in
  880. * the same (micro)frame.
  881. * @hs_periodic_bitmap: Bitmap used by the microframe scheduler any time the
  882. * host is in high speed mode; low speed schedules are
  883. * stored elsewhere since we need one per TT.
  884. * @periodic_qh_count: Count of periodic QHs, if using several eps. Used for
  885. * SOF enable/disable.
  886. * @free_hc_list: Free host channels in the controller. This is a list of
  887. * struct dwc2_host_chan items.
  888. * @periodic_channels: Number of host channels assigned to periodic transfers.
  889. * Currently assuming that there is a dedicated host
  890. * channel for each periodic transaction and at least one
  891. * host channel is available for non-periodic transactions.
  892. * @non_periodic_channels: Number of host channels assigned to non-periodic
  893. * transfers
  894. * @available_host_channels: Number of host channels available for the
  895. * microframe scheduler to use
  896. * @hc_ptr_array: Array of pointers to the host channel descriptors.
  897. * Allows accessing a host channel descriptor given the
  898. * host channel number. This is useful in interrupt
  899. * handlers.
  900. * @status_buf: Buffer used for data received during the status phase of
  901. * a control transfer.
  902. * @status_buf_dma: DMA address for status_buf
  903. * @start_work: Delayed work for handling host A-cable connection
  904. * @reset_work: Delayed work for handling a port reset
  905. * @otg_port: OTG port number
  906. * @frame_list: Frame list
  907. * @frame_list_dma: Frame list DMA address
  908. * @frame_list_sz: Frame list size
  909. * @desc_gen_cache: Kmem cache for generic descriptors
  910. * @desc_hsisoc_cache: Kmem cache for hs isochronous descriptors
  911. * @unaligned_cache: Kmem cache for DMA mode to handle non-aligned buf
  912. *
  913. * These are for peripheral mode:
  914. *
  915. * @driver: USB gadget driver
  916. * @dedicated_fifos: Set if the hardware has dedicated IN-EP fifos.
  917. * @num_of_eps: Number of available EPs (excluding EP0)
  918. * @debug_root: Root directrory for debugfs.
  919. * @ep0_reply: Request used for ep0 reply.
  920. * @ep0_buff: Buffer for EP0 reply data, if needed.
  921. * @ctrl_buff: Buffer for EP0 control requests.
  922. * @ctrl_req: Request for EP0 control packets.
  923. * @ep0_state: EP0 control transfers state
  924. * @test_mode: USB test mode requested by the host
  925. * @remote_wakeup_allowed: True if device is allowed to wake-up host by
  926. * remote-wakeup signalling
  927. * @setup_desc_dma: EP0 setup stage desc chain DMA address
  928. * @setup_desc: EP0 setup stage desc chain pointer
  929. * @ctrl_in_desc_dma: EP0 IN data phase desc chain DMA address
  930. * @ctrl_in_desc: EP0 IN data phase desc chain pointer
  931. * @ctrl_out_desc_dma: EP0 OUT data phase desc chain DMA address
  932. * @ctrl_out_desc: EP0 OUT data phase desc chain pointer
  933. * @irq: Interrupt request line number
  934. * @clk: Pointer to otg clock
  935. * @reset: Pointer to dwc2 reset controller
  936. * @reset_ecc: Pointer to dwc2 optional reset controller in Stratix10.
  937. * @regset: A pointer to a struct debugfs_regset32, which contains
  938. * a pointer to an array of register definitions, the
  939. * array size and the base address where the register bank
  940. * is to be found.
  941. * @bus_suspended: True if bus is suspended
  942. * @last_frame_num: Number of last frame. Range from 0 to 32768
  943. * @frame_num_array: Used only if CONFIG_USB_DWC2_TRACK_MISSED_SOFS is
  944. * defined, for missed SOFs tracking. Array holds that
  945. * frame numbers, which not equal to last_frame_num +1
  946. * @last_frame_num_array: Used only if CONFIG_USB_DWC2_TRACK_MISSED_SOFS is
  947. * defined, for missed SOFs tracking.
  948. * If current_frame_number != last_frame_num+1
  949. * then last_frame_num added to this array
  950. * @frame_num_idx: Actual size of frame_num_array and last_frame_num_array
  951. * @dumped_frame_num_array: 1 - if missed SOFs frame numbers dumbed
  952. * 0 - if missed SOFs frame numbers not dumbed
  953. * @fifo_mem: Total internal RAM for FIFOs (bytes)
  954. * @fifo_map: Each bit intend for concrete fifo. If that bit is set,
  955. * then that fifo is used
  956. * @gadget: Represents a usb slave device
  957. * @connected: Used in slave mode. True if device connected with host
  958. * @eps_in: The IN endpoints being supplied to the gadget framework
  959. * @eps_out: The OUT endpoints being supplied to the gadget framework
  960. * @new_connection: Used in host mode. True if there are new connected
  961. * device
  962. * @enabled: Indicates the enabling state of controller
  963. *
  964. */
  965. struct dwc2_hsotg {
  966. struct device *dev;
  967. void __iomem *regs;
  968. /** Params detected from hardware */
  969. struct dwc2_hw_params hw_params;
  970. /** Params to actually use */
  971. struct dwc2_core_params params;
  972. enum usb_otg_state op_state;
  973. enum usb_dr_mode dr_mode;
  974. unsigned int hcd_enabled:1;
  975. unsigned int gadget_enabled:1;
  976. unsigned int ll_hw_enabled:1;
  977. unsigned int hibernated:1;
  978. u16 frame_number;
  979. struct phy *phy;
  980. struct usb_phy *uphy;
  981. struct dwc2_hsotg_plat *plat;
  982. struct regulator_bulk_data supplies[DWC2_NUM_SUPPLIES];
  983. struct regulator *vbus_supply;
  984. u32 phyif;
  985. spinlock_t lock;
  986. void *priv;
  987. int irq;
  988. struct clk *clk;
  989. struct reset_control *reset;
  990. struct reset_control *reset_ecc;
  991. unsigned int queuing_high_bandwidth:1;
  992. unsigned int srp_success:1;
  993. struct workqueue_struct *wq_otg;
  994. struct work_struct wf_otg;
  995. struct timer_list wkp_timer;
  996. enum dwc2_lx_state lx_state;
  997. struct dwc2_gregs_backup gr_backup;
  998. struct dwc2_dregs_backup dr_backup;
  999. struct dwc2_hregs_backup hr_backup;
  1000. struct dentry *debug_root;
  1001. struct debugfs_regset32 *regset;
  1002. bool needs_byte_swap;
  1003. /* DWC OTG HW Release versions */
  1004. #define DWC2_CORE_REV_2_71a 0x4f54271a
  1005. #define DWC2_CORE_REV_2_72a 0x4f54272a
  1006. #define DWC2_CORE_REV_2_80a 0x4f54280a
  1007. #define DWC2_CORE_REV_2_90a 0x4f54290a
  1008. #define DWC2_CORE_REV_2_91a 0x4f54291a
  1009. #define DWC2_CORE_REV_2_92a 0x4f54292a
  1010. #define DWC2_CORE_REV_2_94a 0x4f54294a
  1011. #define DWC2_CORE_REV_3_00a 0x4f54300a
  1012. #define DWC2_CORE_REV_3_10a 0x4f54310a
  1013. #define DWC2_CORE_REV_4_00a 0x4f54400a
  1014. #define DWC2_FS_IOT_REV_1_00a 0x5531100a
  1015. #define DWC2_HS_IOT_REV_1_00a 0x5532100a
  1016. /* DWC OTG HW Core ID */
  1017. #define DWC2_OTG_ID 0x4f540000
  1018. #define DWC2_FS_IOT_ID 0x55310000
  1019. #define DWC2_HS_IOT_ID 0x55320000
  1020. #if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
  1021. union dwc2_hcd_internal_flags {
  1022. u32 d32;
  1023. struct {
  1024. unsigned port_connect_status_change:1;
  1025. unsigned port_connect_status:1;
  1026. unsigned port_reset_change:1;
  1027. unsigned port_enable_change:1;
  1028. unsigned port_suspend_change:1;
  1029. unsigned port_over_current_change:1;
  1030. unsigned port_l1_change:1;
  1031. unsigned reserved:25;
  1032. } b;
  1033. } flags;
  1034. struct list_head non_periodic_sched_inactive;
  1035. struct list_head non_periodic_sched_waiting;
  1036. struct list_head non_periodic_sched_active;
  1037. struct list_head *non_periodic_qh_ptr;
  1038. struct list_head periodic_sched_inactive;
  1039. struct list_head periodic_sched_ready;
  1040. struct list_head periodic_sched_assigned;
  1041. struct list_head periodic_sched_queued;
  1042. struct list_head split_order;
  1043. u16 periodic_usecs;
  1044. unsigned long hs_periodic_bitmap[
  1045. DIV_ROUND_UP(DWC2_HS_SCHEDULE_US, BITS_PER_LONG)];
  1046. u16 periodic_qh_count;
  1047. bool bus_suspended;
  1048. bool new_connection;
  1049. u16 last_frame_num;
  1050. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  1051. #define FRAME_NUM_ARRAY_SIZE 1000
  1052. u16 *frame_num_array;
  1053. u16 *last_frame_num_array;
  1054. int frame_num_idx;
  1055. int dumped_frame_num_array;
  1056. #endif
  1057. struct list_head free_hc_list;
  1058. int periodic_channels;
  1059. int non_periodic_channels;
  1060. int available_host_channels;
  1061. struct dwc2_host_chan *hc_ptr_array[MAX_EPS_CHANNELS];
  1062. u8 *status_buf;
  1063. dma_addr_t status_buf_dma;
  1064. #define DWC2_HCD_STATUS_BUF_SIZE 64
  1065. struct delayed_work start_work;
  1066. struct delayed_work reset_work;
  1067. u8 otg_port;
  1068. u32 *frame_list;
  1069. dma_addr_t frame_list_dma;
  1070. u32 frame_list_sz;
  1071. struct kmem_cache *desc_gen_cache;
  1072. struct kmem_cache *desc_hsisoc_cache;
  1073. struct kmem_cache *unaligned_cache;
  1074. #define DWC2_KMEM_UNALIGNED_BUF_SIZE 1024
  1075. #endif /* CONFIG_USB_DWC2_HOST || CONFIG_USB_DWC2_DUAL_ROLE */
  1076. #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
  1077. IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
  1078. /* Gadget structures */
  1079. struct usb_gadget_driver *driver;
  1080. int fifo_mem;
  1081. unsigned int dedicated_fifos:1;
  1082. unsigned char num_of_eps;
  1083. u32 fifo_map;
  1084. struct usb_request *ep0_reply;
  1085. struct usb_request *ctrl_req;
  1086. void *ep0_buff;
  1087. void *ctrl_buff;
  1088. enum dwc2_ep0_state ep0_state;
  1089. u8 test_mode;
  1090. dma_addr_t setup_desc_dma[2];
  1091. struct dwc2_dma_desc *setup_desc[2];
  1092. dma_addr_t ctrl_in_desc_dma;
  1093. struct dwc2_dma_desc *ctrl_in_desc;
  1094. dma_addr_t ctrl_out_desc_dma;
  1095. struct dwc2_dma_desc *ctrl_out_desc;
  1096. struct usb_gadget gadget;
  1097. unsigned int enabled:1;
  1098. unsigned int connected:1;
  1099. unsigned int remote_wakeup_allowed:1;
  1100. struct dwc2_hsotg_ep *eps_in[MAX_EPS_CHANNELS];
  1101. struct dwc2_hsotg_ep *eps_out[MAX_EPS_CHANNELS];
  1102. #endif /* CONFIG_USB_DWC2_PERIPHERAL || CONFIG_USB_DWC2_DUAL_ROLE */
  1103. };
  1104. /* Normal architectures just use readl/write */
  1105. static inline u32 dwc2_readl(struct dwc2_hsotg *hsotg, u32 offset)
  1106. {
  1107. u32 val;
  1108. val = readl(hsotg->regs + offset);
  1109. if (hsotg->needs_byte_swap)
  1110. return swab32(val);
  1111. else
  1112. return val;
  1113. }
  1114. static inline void dwc2_writel(struct dwc2_hsotg *hsotg, u32 value, u32 offset)
  1115. {
  1116. if (hsotg->needs_byte_swap)
  1117. writel(swab32(value), hsotg->regs + offset);
  1118. else
  1119. writel(value, hsotg->regs + offset);
  1120. #ifdef DWC2_LOG_WRITES
  1121. pr_info("info:: wrote %08x to %p\n", value, hsotg->regs + offset);
  1122. #endif
  1123. }
  1124. static inline void dwc2_readl_rep(struct dwc2_hsotg *hsotg, u32 offset,
  1125. void *buffer, unsigned int count)
  1126. {
  1127. if (count) {
  1128. u32 *buf = buffer;
  1129. do {
  1130. u32 x = dwc2_readl(hsotg, offset);
  1131. *buf++ = x;
  1132. } while (--count);
  1133. }
  1134. }
  1135. static inline void dwc2_writel_rep(struct dwc2_hsotg *hsotg, u32 offset,
  1136. const void *buffer, unsigned int count)
  1137. {
  1138. if (count) {
  1139. const u32 *buf = buffer;
  1140. do {
  1141. dwc2_writel(hsotg, *buf++, offset);
  1142. } while (--count);
  1143. }
  1144. }
  1145. /* Reasons for halting a host channel */
  1146. enum dwc2_halt_status {
  1147. DWC2_HC_XFER_NO_HALT_STATUS,
  1148. DWC2_HC_XFER_COMPLETE,
  1149. DWC2_HC_XFER_URB_COMPLETE,
  1150. DWC2_HC_XFER_ACK,
  1151. DWC2_HC_XFER_NAK,
  1152. DWC2_HC_XFER_NYET,
  1153. DWC2_HC_XFER_STALL,
  1154. DWC2_HC_XFER_XACT_ERR,
  1155. DWC2_HC_XFER_FRAME_OVERRUN,
  1156. DWC2_HC_XFER_BABBLE_ERR,
  1157. DWC2_HC_XFER_DATA_TOGGLE_ERR,
  1158. DWC2_HC_XFER_AHB_ERR,
  1159. DWC2_HC_XFER_PERIODIC_INCOMPLETE,
  1160. DWC2_HC_XFER_URB_DEQUEUE,
  1161. };
  1162. /* Core version information */
  1163. static inline bool dwc2_is_iot(struct dwc2_hsotg *hsotg)
  1164. {
  1165. return (hsotg->hw_params.snpsid & 0xfff00000) == 0x55300000;
  1166. }
  1167. static inline bool dwc2_is_fs_iot(struct dwc2_hsotg *hsotg)
  1168. {
  1169. return (hsotg->hw_params.snpsid & 0xffff0000) == 0x55310000;
  1170. }
  1171. static inline bool dwc2_is_hs_iot(struct dwc2_hsotg *hsotg)
  1172. {
  1173. return (hsotg->hw_params.snpsid & 0xffff0000) == 0x55320000;
  1174. }
  1175. /*
  1176. * The following functions support initialization of the core driver component
  1177. * and the DWC_otg controller
  1178. */
  1179. int dwc2_core_reset(struct dwc2_hsotg *hsotg, bool skip_wait);
  1180. int dwc2_enter_partial_power_down(struct dwc2_hsotg *hsotg);
  1181. int dwc2_exit_partial_power_down(struct dwc2_hsotg *hsotg, bool restore);
  1182. int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg, int is_host);
  1183. int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
  1184. int reset, int is_host);
  1185. void dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host);
  1186. void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg);
  1187. bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg);
  1188. /*
  1189. * Common core Functions.
  1190. * The following functions support managing the DWC_otg controller in either
  1191. * device or host mode.
  1192. */
  1193. void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes);
  1194. void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num);
  1195. void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg);
  1196. void dwc2_enable_global_interrupts(struct dwc2_hsotg *hcd);
  1197. void dwc2_disable_global_interrupts(struct dwc2_hsotg *hcd);
  1198. void dwc2_hib_restore_common(struct dwc2_hsotg *hsotg, int rem_wakeup,
  1199. int is_host);
  1200. int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg);
  1201. int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg);
  1202. void dwc2_enable_acg(struct dwc2_hsotg *hsotg);
  1203. /* This function should be called on every hardware interrupt. */
  1204. irqreturn_t dwc2_handle_common_intr(int irq, void *dev);
  1205. /* The device ID match table */
  1206. extern const struct of_device_id dwc2_of_match_table[];
  1207. int dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg);
  1208. int dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg);
  1209. /* Common polling functions */
  1210. int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg, u32 bit,
  1211. u32 timeout);
  1212. int dwc2_hsotg_wait_bit_clear(struct dwc2_hsotg *hs_otg, u32 reg, u32 bit,
  1213. u32 timeout);
  1214. /* Parameters */
  1215. int dwc2_get_hwparams(struct dwc2_hsotg *hsotg);
  1216. int dwc2_init_params(struct dwc2_hsotg *hsotg);
  1217. /*
  1218. * The following functions check the controller's OTG operation mode
  1219. * capability (GHWCFG2.OTG_MODE).
  1220. *
  1221. * These functions can be used before the internal hsotg->hw_params
  1222. * are read in and cached so they always read directly from the
  1223. * GHWCFG2 register.
  1224. */
  1225. unsigned int dwc2_op_mode(struct dwc2_hsotg *hsotg);
  1226. bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg);
  1227. bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg);
  1228. bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg);
  1229. /*
  1230. * Returns the mode of operation, host or device
  1231. */
  1232. static inline int dwc2_is_host_mode(struct dwc2_hsotg *hsotg)
  1233. {
  1234. return (dwc2_readl(hsotg, GINTSTS) & GINTSTS_CURMODE_HOST) != 0;
  1235. }
  1236. static inline int dwc2_is_device_mode(struct dwc2_hsotg *hsotg)
  1237. {
  1238. return (dwc2_readl(hsotg, GINTSTS) & GINTSTS_CURMODE_HOST) == 0;
  1239. }
  1240. /*
  1241. * Dump core registers and SPRAM
  1242. */
  1243. void dwc2_dump_dev_registers(struct dwc2_hsotg *hsotg);
  1244. void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg);
  1245. void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg);
  1246. /* Gadget defines */
  1247. #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
  1248. IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
  1249. int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg);
  1250. int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2);
  1251. int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2);
  1252. int dwc2_gadget_init(struct dwc2_hsotg *hsotg);
  1253. void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2,
  1254. bool reset);
  1255. void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg);
  1256. void dwc2_hsotg_disconnect(struct dwc2_hsotg *dwc2);
  1257. int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode);
  1258. #define dwc2_is_device_connected(hsotg) (hsotg->connected)
  1259. int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg);
  1260. int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup);
  1261. int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg);
  1262. int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
  1263. int rem_wakeup, int reset);
  1264. int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg);
  1265. int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg);
  1266. int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg);
  1267. void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg);
  1268. #else
  1269. static inline int dwc2_hsotg_remove(struct dwc2_hsotg *dwc2)
  1270. { return 0; }
  1271. static inline int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2)
  1272. { return 0; }
  1273. static inline int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2)
  1274. { return 0; }
  1275. static inline int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
  1276. { return 0; }
  1277. static inline void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2,
  1278. bool reset) {}
  1279. static inline void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg) {}
  1280. static inline void dwc2_hsotg_disconnect(struct dwc2_hsotg *dwc2) {}
  1281. static inline int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg,
  1282. int testmode)
  1283. { return 0; }
  1284. #define dwc2_is_device_connected(hsotg) (0)
  1285. static inline int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
  1286. { return 0; }
  1287. static inline int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg,
  1288. int remote_wakeup)
  1289. { return 0; }
  1290. static inline int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
  1291. { return 0; }
  1292. static inline int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
  1293. int rem_wakeup, int reset)
  1294. { return 0; }
  1295. static inline int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
  1296. { return 0; }
  1297. static inline int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
  1298. { return 0; }
  1299. static inline int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
  1300. { return 0; }
  1301. static inline void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg) {}
  1302. #endif
  1303. #if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
  1304. int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg);
  1305. int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us);
  1306. void dwc2_hcd_connect(struct dwc2_hsotg *hsotg);
  1307. void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force);
  1308. void dwc2_hcd_start(struct dwc2_hsotg *hsotg);
  1309. int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup);
  1310. int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg);
  1311. int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg);
  1312. int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg);
  1313. int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg,
  1314. int rem_wakeup, int reset);
  1315. #else
  1316. static inline int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
  1317. { return 0; }
  1318. static inline int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg,
  1319. int us)
  1320. { return 0; }
  1321. static inline void dwc2_hcd_connect(struct dwc2_hsotg *hsotg) {}
  1322. static inline void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force) {}
  1323. static inline void dwc2_hcd_start(struct dwc2_hsotg *hsotg) {}
  1324. static inline void dwc2_hcd_remove(struct dwc2_hsotg *hsotg) {}
  1325. static inline int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
  1326. { return 0; }
  1327. static inline int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
  1328. { return 0; }
  1329. static inline int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
  1330. { return 0; }
  1331. static inline int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
  1332. { return 0; }
  1333. static inline int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
  1334. { return 0; }
  1335. static inline int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg,
  1336. int rem_wakeup, int reset)
  1337. { return 0; }
  1338. #endif
  1339. #endif /* __DWC2_CORE_H__ */