core.h 51 KB

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