core.h 48 KB

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