core.h 49 KB

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