core.h 52 KB

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