core.h 57 KB

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