core.h 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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. * @dir_in: Set to true if this endpoint is of the IN direction, which
  150. * means that it is sending data to the Host.
  151. * @index: The index for the endpoint registers.
  152. * @mc: Multi Count - number of transactions per microframe
  153. * @interval: Interval for periodic endpoints, in frames or microframes.
  154. * @name: The name array passed to the USB core.
  155. * @halted: Set if the endpoint has been halted.
  156. * @periodic: Set if this is a periodic ep, such as Interrupt
  157. * @isochronous: Set if this is a isochronous ep
  158. * @send_zlp: Set if we need to send a zero-length packet.
  159. * @desc_list_dma: The DMA address of descriptor chain currently in use.
  160. * @desc_list: Pointer to descriptor DMA chain head currently in use.
  161. * @desc_count: Count of entries within the DMA descriptor chain of EP.
  162. * @next_desc: index of next free descriptor in the ISOC chain under SW control.
  163. * @compl_desc: index of next descriptor to be completed by xFerComplete
  164. * @total_data: The total number of data bytes done.
  165. * @fifo_size: The size of the FIFO (for periodic IN endpoints)
  166. * @fifo_index: For Dedicated FIFO operation, only FIFO0 can be used for EP0.
  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 int next_desc;
  212. unsigned int compl_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. * @ipg_isoc_en: Indicates the IPG supports is enabled or disabled.
  354. * 0 - Disable (default)
  355. * 1 - Enable
  356. * @acg_enable: For enabling Active Clock Gating in the controller
  357. * 0 - No
  358. * 1 - Yes
  359. * @ulpi_fs_ls: Make ULPI phy operate in FS/LS mode only
  360. * 0 - No (default)
  361. * 1 - Yes
  362. * @host_support_fs_ls_low_power: Specifies whether low power mode is supported
  363. * when attached to a Full Speed or Low Speed device in
  364. * host mode.
  365. * 0 - Don't support low power mode (default)
  366. * 1 - Support low power mode
  367. * @host_ls_low_power_phy_clk: Specifies the PHY clock rate in low power mode
  368. * when connected to a Low Speed device in host
  369. * mode. This parameter is applicable only if
  370. * host_support_fs_ls_low_power is enabled.
  371. * 0 - 48 MHz
  372. * (default when phy_type is UTMI+ or ULPI)
  373. * 1 - 6 MHz
  374. * (default when phy_type is Full Speed)
  375. * @oc_disable: Flag to disable overcurrent condition.
  376. * 0 - Allow overcurrent condition to get detected
  377. * 1 - Disable overcurrent condtion to get detected
  378. * @ts_dline: Enable Term Select Dline pulsing
  379. * 0 - No (default)
  380. * 1 - Yes
  381. * @reload_ctl: Allow dynamic reloading of HFIR register during runtime
  382. * 0 - No (default for core < 2.92a)
  383. * 1 - Yes (default for core >= 2.92a)
  384. * @ahbcfg: This field allows the default value of the GAHBCFG
  385. * register to be overridden
  386. * -1 - GAHBCFG value will be set to 0x06
  387. * (INCR, default)
  388. * all others - GAHBCFG value will be overridden with
  389. * this value
  390. * Not all bits can be controlled like this, the
  391. * bits defined by GAHBCFG_CTRL_MASK are controlled
  392. * by the driver and are ignored in this
  393. * configuration value.
  394. * @uframe_sched: True to enable the microframe scheduler
  395. * @external_id_pin_ctl: Specifies whether ID pin is handled externally.
  396. * Disable CONIDSTSCHNG controller interrupt in such
  397. * case.
  398. * 0 - No (default)
  399. * 1 - Yes
  400. * @power_down: Specifies whether the controller support power_down.
  401. * If power_down is enabled, the controller will enter
  402. * power_down in both peripheral and host mode when
  403. * needed.
  404. * 0 - No (default)
  405. * 1 - Partial power down
  406. * 2 - Hibernation
  407. * @lpm: Enable LPM support.
  408. * 0 - No
  409. * 1 - Yes
  410. * @lpm_clock_gating: Enable core PHY clock gating.
  411. * 0 - No
  412. * 1 - Yes
  413. * @besl: Enable LPM Errata support.
  414. * 0 - No
  415. * 1 - Yes
  416. * @hird_threshold_en: HIRD or HIRD Threshold enable.
  417. * 0 - No
  418. * 1 - Yes
  419. * @hird_threshold: Value of BESL or HIRD Threshold.
  420. * @activate_stm_fs_transceiver: Activate internal transceiver using GGPIO
  421. * register.
  422. * 0 - Deactivate the transceiver (default)
  423. * 1 - Activate the transceiver
  424. * @g_dma: Enables gadget dma usage (default: autodetect).
  425. * @g_dma_desc: Enables gadget descriptor DMA (default: autodetect).
  426. * @g_rx_fifo_size: The periodic rx fifo size for the device, in
  427. * DWORDS from 16-32768 (default: 2048 if
  428. * possible, otherwise autodetect).
  429. * @g_np_tx_fifo_size: The non-periodic tx fifo size for the device in
  430. * DWORDS from 16-32768 (default: 1024 if
  431. * possible, otherwise autodetect).
  432. * @g_tx_fifo_size: An array of TX fifo sizes in dedicated fifo
  433. * mode. Each value corresponds to one EP
  434. * starting from EP1 (max 15 values). Sizes are
  435. * in DWORDS with possible values from from
  436. * 16-32768 (default: 256, 256, 256, 256, 768,
  437. * 768, 768, 768, 0, 0, 0, 0, 0, 0, 0).
  438. * @change_speed_quirk: Change speed configuration to DWC2_SPEED_PARAM_FULL
  439. * while full&low speed device connect. And change speed
  440. * back to DWC2_SPEED_PARAM_HIGH while device is gone.
  441. * 0 - No (default)
  442. * 1 - Yes
  443. *
  444. * The following parameters may be specified when starting the module. These
  445. * parameters define how the DWC_otg controller should be configured. A
  446. * value of -1 (or any other out of range value) for any parameter means
  447. * to read the value from hardware (if possible) or use the builtin
  448. * default described above.
  449. */
  450. struct dwc2_core_params {
  451. u8 otg_cap;
  452. #define DWC2_CAP_PARAM_HNP_SRP_CAPABLE 0
  453. #define DWC2_CAP_PARAM_SRP_ONLY_CAPABLE 1
  454. #define DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
  455. u8 phy_type;
  456. #define DWC2_PHY_TYPE_PARAM_FS 0
  457. #define DWC2_PHY_TYPE_PARAM_UTMI 1
  458. #define DWC2_PHY_TYPE_PARAM_ULPI 2
  459. u8 speed;
  460. #define DWC2_SPEED_PARAM_HIGH 0
  461. #define DWC2_SPEED_PARAM_FULL 1
  462. #define DWC2_SPEED_PARAM_LOW 2
  463. u8 phy_utmi_width;
  464. bool phy_ulpi_ddr;
  465. bool phy_ulpi_ext_vbus;
  466. bool enable_dynamic_fifo;
  467. bool en_multiple_tx_fifo;
  468. bool i2c_enable;
  469. bool acg_enable;
  470. bool ulpi_fs_ls;
  471. bool ts_dline;
  472. bool reload_ctl;
  473. bool uframe_sched;
  474. bool external_id_pin_ctl;
  475. int power_down;
  476. #define DWC2_POWER_DOWN_PARAM_NONE 0
  477. #define DWC2_POWER_DOWN_PARAM_PARTIAL 1
  478. #define DWC2_POWER_DOWN_PARAM_HIBERNATION 2
  479. bool lpm;
  480. bool lpm_clock_gating;
  481. bool besl;
  482. bool hird_threshold_en;
  483. u8 hird_threshold;
  484. bool activate_stm_fs_transceiver;
  485. bool ipg_isoc_en;
  486. u16 max_packet_count;
  487. u32 max_transfer_size;
  488. u32 ahbcfg;
  489. /* Host parameters */
  490. bool host_dma;
  491. bool dma_desc_enable;
  492. bool dma_desc_fs_enable;
  493. bool host_support_fs_ls_low_power;
  494. bool host_ls_low_power_phy_clk;
  495. bool oc_disable;
  496. u8 host_channels;
  497. u16 host_rx_fifo_size;
  498. u16 host_nperio_tx_fifo_size;
  499. u16 host_perio_tx_fifo_size;
  500. /* Gadget parameters */
  501. bool g_dma;
  502. bool g_dma_desc;
  503. u32 g_rx_fifo_size;
  504. u32 g_np_tx_fifo_size;
  505. u32 g_tx_fifo_size[MAX_EPS_CHANNELS];
  506. bool change_speed_quirk;
  507. };
  508. /**
  509. * struct dwc2_hw_params - Autodetected parameters.
  510. *
  511. * These parameters are the various parameters read from hardware
  512. * registers during initialization. They typically contain the best
  513. * supported or maximum value that can be configured in the
  514. * corresponding dwc2_core_params value.
  515. *
  516. * The values that are not in dwc2_core_params are documented below.
  517. *
  518. * @op_mode: Mode of Operation
  519. * 0 - HNP- and SRP-Capable OTG (Host & Device)
  520. * 1 - SRP-Capable OTG (Host & Device)
  521. * 2 - Non-HNP and Non-SRP Capable OTG (Host & Device)
  522. * 3 - SRP-Capable Device
  523. * 4 - Non-OTG Device
  524. * 5 - SRP-Capable Host
  525. * 6 - Non-OTG Host
  526. * @arch: Architecture
  527. * 0 - Slave only
  528. * 1 - External DMA
  529. * 2 - Internal DMA
  530. * @ipg_isoc_en: This feature indicates that the controller supports
  531. * the worst-case scenario of Rx followed by Rx
  532. * Interpacket Gap (IPG) (32 bitTimes) as per the utmi
  533. * specification for any token following ISOC OUT token.
  534. * 0 - Don't support
  535. * 1 - Support
  536. * @power_optimized: Are power optimizations enabled?
  537. * @num_dev_ep: Number of device endpoints available
  538. * @num_dev_in_eps: Number of device IN endpoints available
  539. * @num_dev_perio_in_ep: Number of device periodic IN endpoints
  540. * available
  541. * @dev_token_q_depth: Device Mode IN Token Sequence Learning Queue
  542. * Depth
  543. * 0 to 30
  544. * @host_perio_tx_q_depth:
  545. * Host Mode Periodic Request Queue Depth
  546. * 2, 4 or 8
  547. * @nperio_tx_q_depth:
  548. * Non-Periodic Request Queue Depth
  549. * 2, 4 or 8
  550. * @hs_phy_type: High-speed PHY interface type
  551. * 0 - High-speed interface not supported
  552. * 1 - UTMI+
  553. * 2 - ULPI
  554. * 3 - UTMI+ and ULPI
  555. * @fs_phy_type: Full-speed PHY interface type
  556. * 0 - Full speed interface not supported
  557. * 1 - Dedicated full speed interface
  558. * 2 - FS pins shared with UTMI+ pins
  559. * 3 - FS pins shared with ULPI pins
  560. * @total_fifo_size: Total internal RAM for FIFOs (bytes)
  561. * @hibernation: Is hibernation enabled?
  562. * @utmi_phy_data_width: UTMI+ PHY data width
  563. * 0 - 8 bits
  564. * 1 - 16 bits
  565. * 2 - 8 or 16 bits
  566. * @snpsid: Value from SNPSID register
  567. * @dev_ep_dirs: Direction of device endpoints (GHWCFG1)
  568. * @g_tx_fifo_size: Power-on values of TxFIFO sizes
  569. * @dma_desc_enable: When DMA mode is enabled, specifies whether to use
  570. * address DMA mode or descriptor DMA mode for accessing
  571. * the data FIFOs. The driver will automatically detect the
  572. * value for this if none is specified.
  573. * 0 - Address DMA
  574. * 1 - Descriptor DMA (default, if available)
  575. * @enable_dynamic_fifo: 0 - Use coreConsultant-specified FIFO size parameters
  576. * 1 - Allow dynamic FIFO sizing (default, if available)
  577. * @en_multiple_tx_fifo: Specifies whether dedicated per-endpoint transmit FIFOs
  578. * are enabled for non-periodic IN endpoints in device
  579. * mode.
  580. * @host_nperio_tx_fifo_size: Number of 4-byte words in the non-periodic Tx FIFO
  581. * in host mode when dynamic FIFO sizing is enabled
  582. * 16 to 32768
  583. * Actual maximum value is autodetected and also
  584. * the default.
  585. * @host_perio_tx_fifo_size: Number of 4-byte words in the periodic Tx FIFO in
  586. * host mode when dynamic FIFO sizing is enabled
  587. * 16 to 32768
  588. * Actual maximum value is autodetected and also
  589. * the default.
  590. * @max_transfer_size: The maximum transfer size supported, in bytes
  591. * 2047 to 65,535
  592. * Actual maximum value is autodetected and also
  593. * the default.
  594. * @max_packet_count: The maximum number of packets in a transfer
  595. * 15 to 511
  596. * Actual maximum value is autodetected and also
  597. * the default.
  598. * @host_channels: The number of host channel registers to use
  599. * 1 to 16
  600. * Actual maximum value is autodetected and also
  601. * the default.
  602. * @dev_nperio_tx_fifo_size: Number of 4-byte words in the non-periodic Tx FIFO
  603. * in device mode when dynamic FIFO sizing is enabled
  604. * 16 to 32768
  605. * Actual maximum value is autodetected and also
  606. * the default.
  607. * @i2c_enable: Specifies whether to use the I2Cinterface for a full
  608. * speed PHY. This parameter is only applicable if phy_type
  609. * is FS.
  610. * 0 - No (default)
  611. * 1 - Yes
  612. * @acg_enable: For enabling Active Clock Gating in the controller
  613. * 0 - Disable
  614. * 1 - Enable
  615. * @lpm_mode: For enabling Link Power Management in the controller
  616. * 0 - Disable
  617. * 1 - Enable
  618. * @rx_fifo_size: Number of 4-byte words in the Rx FIFO when dynamic
  619. * FIFO sizing is enabled 16 to 32768
  620. * Actual maximum value is autodetected and also
  621. * the default.
  622. */
  623. struct dwc2_hw_params {
  624. unsigned op_mode:3;
  625. unsigned arch:2;
  626. unsigned dma_desc_enable:1;
  627. unsigned enable_dynamic_fifo:1;
  628. unsigned en_multiple_tx_fifo:1;
  629. unsigned rx_fifo_size:16;
  630. unsigned host_nperio_tx_fifo_size:16;
  631. unsigned dev_nperio_tx_fifo_size:16;
  632. unsigned host_perio_tx_fifo_size:16;
  633. unsigned nperio_tx_q_depth:3;
  634. unsigned host_perio_tx_q_depth:3;
  635. unsigned dev_token_q_depth:5;
  636. unsigned max_transfer_size:26;
  637. unsigned max_packet_count:11;
  638. unsigned host_channels:5;
  639. unsigned hs_phy_type:2;
  640. unsigned fs_phy_type:2;
  641. unsigned i2c_enable:1;
  642. unsigned acg_enable:1;
  643. unsigned num_dev_ep:4;
  644. unsigned num_dev_in_eps : 4;
  645. unsigned num_dev_perio_in_ep:4;
  646. unsigned total_fifo_size:16;
  647. unsigned power_optimized:1;
  648. unsigned hibernation:1;
  649. unsigned utmi_phy_data_width:2;
  650. unsigned lpm_mode:1;
  651. unsigned ipg_isoc_en:1;
  652. u32 snpsid;
  653. u32 dev_ep_dirs;
  654. u32 g_tx_fifo_size[MAX_EPS_CHANNELS];
  655. };
  656. /* Size of control and EP0 buffers */
  657. #define DWC2_CTRL_BUFF_SIZE 8
  658. /**
  659. * struct dwc2_gregs_backup - Holds global registers state before
  660. * entering partial power down
  661. * @gotgctl: Backup of GOTGCTL register
  662. * @gintmsk: Backup of GINTMSK register
  663. * @gahbcfg: Backup of GAHBCFG register
  664. * @gusbcfg: Backup of GUSBCFG register
  665. * @grxfsiz: Backup of GRXFSIZ register
  666. * @gnptxfsiz: Backup of GNPTXFSIZ register
  667. * @gi2cctl: Backup of GI2CCTL register
  668. * @glpmcfg: Backup of GLPMCFG register
  669. * @gdfifocfg: Backup of GDFIFOCFG register
  670. * @pcgcctl: Backup of PCGCCTL register
  671. * @pcgcctl1: Backup of PCGCCTL1 register
  672. * @dtxfsiz: Backup of DTXFSIZ registers for each endpoint
  673. * @gpwrdn: Backup of GPWRDN register
  674. * @valid: True if registers values backuped.
  675. */
  676. struct dwc2_gregs_backup {
  677. u32 gotgctl;
  678. u32 gintmsk;
  679. u32 gahbcfg;
  680. u32 gusbcfg;
  681. u32 grxfsiz;
  682. u32 gnptxfsiz;
  683. u32 gi2cctl;
  684. u32 glpmcfg;
  685. u32 pcgcctl;
  686. u32 pcgcctl1;
  687. u32 gdfifocfg;
  688. u32 gpwrdn;
  689. bool valid;
  690. };
  691. /**
  692. * struct dwc2_dregs_backup - Holds device registers state before
  693. * entering partial power down
  694. * @dcfg: Backup of DCFG register
  695. * @dctl: Backup of DCTL register
  696. * @daintmsk: Backup of DAINTMSK register
  697. * @diepmsk: Backup of DIEPMSK register
  698. * @doepmsk: Backup of DOEPMSK register
  699. * @diepctl: Backup of DIEPCTL register
  700. * @dieptsiz: Backup of DIEPTSIZ register
  701. * @diepdma: Backup of DIEPDMA register
  702. * @doepctl: Backup of DOEPCTL register
  703. * @doeptsiz: Backup of DOEPTSIZ register
  704. * @doepdma: Backup of DOEPDMA register
  705. * @dtxfsiz: Backup of DTXFSIZ registers for each endpoint
  706. * @valid: True if registers values backuped.
  707. */
  708. struct dwc2_dregs_backup {
  709. u32 dcfg;
  710. u32 dctl;
  711. u32 daintmsk;
  712. u32 diepmsk;
  713. u32 doepmsk;
  714. u32 diepctl[MAX_EPS_CHANNELS];
  715. u32 dieptsiz[MAX_EPS_CHANNELS];
  716. u32 diepdma[MAX_EPS_CHANNELS];
  717. u32 doepctl[MAX_EPS_CHANNELS];
  718. u32 doeptsiz[MAX_EPS_CHANNELS];
  719. u32 doepdma[MAX_EPS_CHANNELS];
  720. u32 dtxfsiz[MAX_EPS_CHANNELS];
  721. bool valid;
  722. };
  723. /**
  724. * struct dwc2_hregs_backup - Holds host registers state before
  725. * entering partial power down
  726. * @hcfg: Backup of HCFG register
  727. * @haintmsk: Backup of HAINTMSK register
  728. * @hcintmsk: Backup of HCINTMSK register
  729. * @hprt0: Backup of HPTR0 register
  730. * @hfir: Backup of HFIR register
  731. * @hptxfsiz: Backup of HPTXFSIZ register
  732. * @valid: True if registers values backuped.
  733. */
  734. struct dwc2_hregs_backup {
  735. u32 hcfg;
  736. u32 haintmsk;
  737. u32 hcintmsk[MAX_EPS_CHANNELS];
  738. u32 hprt0;
  739. u32 hfir;
  740. u32 hptxfsiz;
  741. bool valid;
  742. };
  743. /*
  744. * Constants related to high speed periodic scheduling
  745. *
  746. * We have a periodic schedule that is DWC2_HS_SCHEDULE_UFRAMES long. From a
  747. * reservation point of view it's assumed that the schedule goes right back to
  748. * the beginning after the end of the schedule.
  749. *
  750. * What does that mean for scheduling things with a long interval? It means
  751. * we'll reserve time for them in every possible microframe that they could
  752. * ever be scheduled in. ...but we'll still only actually schedule them as
  753. * often as they were requested.
  754. *
  755. * We keep our schedule in a "bitmap" structure. This simplifies having
  756. * to keep track of and merge intervals: we just let the bitmap code do most
  757. * of the heavy lifting. In a way scheduling is much like memory allocation.
  758. *
  759. * We schedule 100us per uframe or 80% of 125us (the maximum amount you're
  760. * supposed to schedule for periodic transfers). That's according to spec.
  761. *
  762. * Note that though we only schedule 80% of each microframe, the bitmap that we
  763. * keep the schedule in is tightly packed (AKA it doesn't have 100us worth of
  764. * space for each uFrame).
  765. *
  766. * Requirements:
  767. * - DWC2_HS_SCHEDULE_UFRAMES must even divide 0x4000 (HFNUM_MAX_FRNUM + 1)
  768. * - DWC2_HS_SCHEDULE_UFRAMES must be 8 times DWC2_LS_SCHEDULE_FRAMES (probably
  769. * could be any multiple of 8 times DWC2_LS_SCHEDULE_FRAMES, but there might
  770. * be bugs). The 8 comes from the USB spec: number of microframes per frame.
  771. */
  772. #define DWC2_US_PER_UFRAME 125
  773. #define DWC2_HS_PERIODIC_US_PER_UFRAME 100
  774. #define DWC2_HS_SCHEDULE_UFRAMES 8
  775. #define DWC2_HS_SCHEDULE_US (DWC2_HS_SCHEDULE_UFRAMES * \
  776. DWC2_HS_PERIODIC_US_PER_UFRAME)
  777. /*
  778. * Constants related to low speed scheduling
  779. *
  780. * For high speed we schedule every 1us. For low speed that's a bit overkill,
  781. * so we make up a unit called a "slice" that's worth 25us. There are 40
  782. * slices in a full frame and we can schedule 36 of those (90%) for periodic
  783. * transfers.
  784. *
  785. * Our low speed schedule can be as short as 1 frame or could be longer. When
  786. * we only schedule 1 frame it means that we'll need to reserve a time every
  787. * frame even for things that only transfer very rarely, so something that runs
  788. * every 2048 frames will get time reserved in every frame. Our low speed
  789. * schedule can be longer and we'll be able to handle more overlap, but that
  790. * will come at increased memory cost and increased time to schedule.
  791. *
  792. * Note: one other advantage of a short low speed schedule is that if we mess
  793. * up and miss scheduling we can jump in and use any of the slots that we
  794. * happened to reserve.
  795. *
  796. * With 25 us per slice and 1 frame in the schedule, we only need 4 bytes for
  797. * the schedule. There will be one schedule per TT.
  798. *
  799. * Requirements:
  800. * - DWC2_US_PER_SLICE must evenly divide DWC2_LS_PERIODIC_US_PER_FRAME.
  801. */
  802. #define DWC2_US_PER_SLICE 25
  803. #define DWC2_SLICES_PER_UFRAME (DWC2_US_PER_UFRAME / DWC2_US_PER_SLICE)
  804. #define DWC2_ROUND_US_TO_SLICE(us) \
  805. (DIV_ROUND_UP((us), DWC2_US_PER_SLICE) * \
  806. DWC2_US_PER_SLICE)
  807. #define DWC2_LS_PERIODIC_US_PER_FRAME \
  808. 900
  809. #define DWC2_LS_PERIODIC_SLICES_PER_FRAME \
  810. (DWC2_LS_PERIODIC_US_PER_FRAME / \
  811. DWC2_US_PER_SLICE)
  812. #define DWC2_LS_SCHEDULE_FRAMES 1
  813. #define DWC2_LS_SCHEDULE_SLICES (DWC2_LS_SCHEDULE_FRAMES * \
  814. DWC2_LS_PERIODIC_SLICES_PER_FRAME)
  815. /**
  816. * struct dwc2_hsotg - Holds the state of the driver, including the non-periodic
  817. * and periodic schedules
  818. *
  819. * These are common for both host and peripheral modes:
  820. *
  821. * @dev: The struct device pointer
  822. * @regs: Pointer to controller regs
  823. * @hw_params: Parameters that were autodetected from the
  824. * hardware registers
  825. * @params: Parameters that define how the core should be configured
  826. * @op_state: The operational State, during transitions (a_host=>
  827. * a_peripheral and b_device=>b_host) this may not match
  828. * the core, but allows the software to determine
  829. * transitions
  830. * @dr_mode: Requested mode of operation, one of following:
  831. * - USB_DR_MODE_PERIPHERAL
  832. * - USB_DR_MODE_HOST
  833. * - USB_DR_MODE_OTG
  834. * @hcd_enabled: Host mode sub-driver initialization indicator.
  835. * @gadget_enabled: Peripheral mode sub-driver initialization indicator.
  836. * @ll_hw_enabled: Status of low-level hardware resources.
  837. * @hibernated: True if core is hibernated
  838. * @frame_number: Frame number read from the core. For both device
  839. * and host modes. The value ranges are from 0
  840. * to HFNUM_MAX_FRNUM.
  841. * @phy: The otg phy transceiver structure for phy control.
  842. * @uphy: The otg phy transceiver structure for old USB phy
  843. * control.
  844. * @plat: The platform specific configuration data. This can be
  845. * removed once all SoCs support usb transceiver.
  846. * @supplies: Definition of USB power supplies
  847. * @vbus_supply: Regulator supplying vbus.
  848. * @phyif: PHY interface width
  849. * @lock: Spinlock that protects all the driver data structures
  850. * @priv: Stores a pointer to the struct usb_hcd
  851. * @queuing_high_bandwidth: True if multiple packets of a high-bandwidth
  852. * transfer are in process of being queued
  853. * @srp_success: Stores status of SRP request in the case of a FS PHY
  854. * with an I2C interface
  855. * @wq_otg: Workqueue object used for handling of some interrupts
  856. * @wf_otg: Work object for handling Connector ID Status Change
  857. * interrupt
  858. * @wkp_timer: Timer object for handling Wakeup Detected interrupt
  859. * @lx_state: Lx state of connected device
  860. * @gr_backup: Backup of global registers during suspend
  861. * @dr_backup: Backup of device registers during suspend
  862. * @hr_backup: Backup of host registers during suspend
  863. *
  864. * These are for host mode:
  865. *
  866. * @flags: Flags for handling root port state changes
  867. * @flags.d32: Contain all root port flags
  868. * @flags.b: Separate root port flags from each other
  869. * @flags.b.port_connect_status_change: True if root port connect status
  870. * changed
  871. * @flags.b.port_connect_status: True if device connected to root port
  872. * @flags.b.port_reset_change: True if root port reset status changed
  873. * @flags.b.port_enable_change: True if root port enable status changed
  874. * @flags.b.port_suspend_change: True if root port suspend status changed
  875. * @flags.b.port_over_current_change: True if root port over current state
  876. * changed.
  877. * @flags.b.port_l1_change: True if root port l1 status changed
  878. * @flags.b.reserved: Reserved bits of root port register
  879. * @non_periodic_sched_inactive: Inactive QHs in the non-periodic schedule.
  880. * Transfers associated with these QHs are not currently
  881. * assigned to a host channel.
  882. * @non_periodic_sched_active: Active QHs in the non-periodic schedule.
  883. * Transfers associated with these QHs are currently
  884. * assigned to a host channel.
  885. * @non_periodic_qh_ptr: Pointer to next QH to process in the active
  886. * non-periodic schedule
  887. * @non_periodic_sched_waiting: Waiting QHs in the non-periodic schedule.
  888. * Transfers associated with these QHs are not currently
  889. * assigned to a host channel.
  890. * @periodic_sched_inactive: Inactive QHs in the periodic schedule. This is a
  891. * list of QHs for periodic transfers that are _not_
  892. * scheduled for the next frame. Each QH in the list has an
  893. * interval counter that determines when it needs to be
  894. * scheduled for execution. This scheduling mechanism
  895. * allows only a simple calculation for periodic bandwidth
  896. * used (i.e. must assume that all periodic transfers may
  897. * need to execute in the same frame). However, it greatly
  898. * simplifies scheduling and should be sufficient for the
  899. * vast majority of OTG hosts, which need to connect to a
  900. * small number of peripherals at one time. Items move from
  901. * this list to periodic_sched_ready when the QH interval
  902. * counter is 0 at SOF.
  903. * @periodic_sched_ready: List of periodic QHs that are ready for execution in
  904. * the next frame, but have not yet been assigned to host
  905. * channels. Items move from this list to
  906. * periodic_sched_assigned as host channels become
  907. * available during the current frame.
  908. * @periodic_sched_assigned: List of periodic QHs to be executed in the next
  909. * frame that are assigned to host channels. Items move
  910. * from this list to periodic_sched_queued as the
  911. * transactions for the QH are queued to the DWC_otg
  912. * controller.
  913. * @periodic_sched_queued: List of periodic QHs that have been queued for
  914. * execution. Items move from this list to either
  915. * periodic_sched_inactive or periodic_sched_ready when the
  916. * channel associated with the transfer is released. If the
  917. * interval for the QH is 1, the item moves to
  918. * periodic_sched_ready because it must be rescheduled for
  919. * the next frame. Otherwise, the item moves to
  920. * periodic_sched_inactive.
  921. * @split_order: List keeping track of channels doing splits, in order.
  922. * @periodic_usecs: Total bandwidth claimed so far for periodic transfers.
  923. * This value is in microseconds per (micro)frame. The
  924. * assumption is that all periodic transfers may occur in
  925. * the same (micro)frame.
  926. * @hs_periodic_bitmap: Bitmap used by the microframe scheduler any time the
  927. * host is in high speed mode; low speed schedules are
  928. * stored elsewhere since we need one per TT.
  929. * @periodic_qh_count: Count of periodic QHs, if using several eps. Used for
  930. * SOF enable/disable.
  931. * @free_hc_list: Free host channels in the controller. This is a list of
  932. * struct dwc2_host_chan items.
  933. * @periodic_channels: Number of host channels assigned to periodic transfers.
  934. * Currently assuming that there is a dedicated host
  935. * channel for each periodic transaction and at least one
  936. * host channel is available for non-periodic transactions.
  937. * @non_periodic_channels: Number of host channels assigned to non-periodic
  938. * transfers
  939. * @available_host_channels: Number of host channels available for the
  940. * microframe scheduler to use
  941. * @hc_ptr_array: Array of pointers to the host channel descriptors.
  942. * Allows accessing a host channel descriptor given the
  943. * host channel number. This is useful in interrupt
  944. * handlers.
  945. * @status_buf: Buffer used for data received during the status phase of
  946. * a control transfer.
  947. * @status_buf_dma: DMA address for status_buf
  948. * @start_work: Delayed work for handling host A-cable connection
  949. * @reset_work: Delayed work for handling a port reset
  950. * @otg_port: OTG port number
  951. * @frame_list: Frame list
  952. * @frame_list_dma: Frame list DMA address
  953. * @frame_list_sz: Frame list size
  954. * @desc_gen_cache: Kmem cache for generic descriptors
  955. * @desc_hsisoc_cache: Kmem cache for hs isochronous descriptors
  956. *
  957. * These are for peripheral mode:
  958. *
  959. * @driver: USB gadget driver
  960. * @dedicated_fifos: Set if the hardware has dedicated IN-EP fifos.
  961. * @num_of_eps: Number of available EPs (excluding EP0)
  962. * @debug_root: Root directrory for debugfs.
  963. * @ep0_reply: Request used for ep0 reply.
  964. * @ep0_buff: Buffer for EP0 reply data, if needed.
  965. * @ctrl_buff: Buffer for EP0 control requests.
  966. * @ctrl_req: Request for EP0 control packets.
  967. * @ep0_state: EP0 control transfers state
  968. * @test_mode: USB test mode requested by the host
  969. * @remote_wakeup_allowed: True if device is allowed to wake-up host by
  970. * remote-wakeup signalling
  971. * @setup_desc_dma: EP0 setup stage desc chain DMA address
  972. * @setup_desc: EP0 setup stage desc chain pointer
  973. * @ctrl_in_desc_dma: EP0 IN data phase desc chain DMA address
  974. * @ctrl_in_desc: EP0 IN data phase desc chain pointer
  975. * @ctrl_out_desc_dma: EP0 OUT data phase desc chain DMA address
  976. * @ctrl_out_desc: EP0 OUT data phase desc chain pointer
  977. * @irq: Interrupt request line number
  978. * @clk: Pointer to otg clock
  979. * @reset: Pointer to dwc2 reset controller
  980. * @reset_ecc: Pointer to dwc2 optional reset controller in Stratix10.
  981. * @regset: A pointer to a struct debugfs_regset32, which contains
  982. * a pointer to an array of register definitions, the
  983. * array size and the base address where the register bank
  984. * is to be found.
  985. * @bus_suspended: True if bus is suspended
  986. * @last_frame_num: Number of last frame. Range from 0 to 32768
  987. * @frame_num_array: Used only if CONFIG_USB_DWC2_TRACK_MISSED_SOFS is
  988. * defined, for missed SOFs tracking. Array holds that
  989. * frame numbers, which not equal to last_frame_num +1
  990. * @last_frame_num_array: Used only if CONFIG_USB_DWC2_TRACK_MISSED_SOFS is
  991. * defined, for missed SOFs tracking.
  992. * If current_frame_number != last_frame_num+1
  993. * then last_frame_num added to this array
  994. * @frame_num_idx: Actual size of frame_num_array and last_frame_num_array
  995. * @dumped_frame_num_array: 1 - if missed SOFs frame numbers dumbed
  996. * 0 - if missed SOFs frame numbers not dumbed
  997. * @fifo_mem: Total internal RAM for FIFOs (bytes)
  998. * @fifo_map: Each bit intend for concrete fifo. If that bit is set,
  999. * then that fifo is used
  1000. * @gadget: Represents a usb slave device
  1001. * @connected: Used in slave mode. True if device connected with host
  1002. * @eps_in: The IN endpoints being supplied to the gadget framework
  1003. * @eps_out: The OUT endpoints being supplied to the gadget framework
  1004. * @new_connection: Used in host mode. True if there are new connected
  1005. * device
  1006. * @enabled: Indicates the enabling state of controller
  1007. *
  1008. */
  1009. struct dwc2_hsotg {
  1010. struct device *dev;
  1011. void __iomem *regs;
  1012. /** Params detected from hardware */
  1013. struct dwc2_hw_params hw_params;
  1014. /** Params to actually use */
  1015. struct dwc2_core_params params;
  1016. enum usb_otg_state op_state;
  1017. enum usb_dr_mode dr_mode;
  1018. unsigned int hcd_enabled:1;
  1019. unsigned int gadget_enabled:1;
  1020. unsigned int ll_hw_enabled:1;
  1021. unsigned int hibernated:1;
  1022. u16 frame_number;
  1023. struct phy *phy;
  1024. struct usb_phy *uphy;
  1025. struct dwc2_hsotg_plat *plat;
  1026. struct regulator_bulk_data supplies[DWC2_NUM_SUPPLIES];
  1027. struct regulator *vbus_supply;
  1028. u32 phyif;
  1029. spinlock_t lock;
  1030. void *priv;
  1031. int irq;
  1032. struct clk *clk;
  1033. struct reset_control *reset;
  1034. struct reset_control *reset_ecc;
  1035. unsigned int queuing_high_bandwidth:1;
  1036. unsigned int srp_success:1;
  1037. struct workqueue_struct *wq_otg;
  1038. struct work_struct wf_otg;
  1039. struct timer_list wkp_timer;
  1040. enum dwc2_lx_state lx_state;
  1041. struct dwc2_gregs_backup gr_backup;
  1042. struct dwc2_dregs_backup dr_backup;
  1043. struct dwc2_hregs_backup hr_backup;
  1044. struct dentry *debug_root;
  1045. struct debugfs_regset32 *regset;
  1046. /* DWC OTG HW Release versions */
  1047. #define DWC2_CORE_REV_2_71a 0x4f54271a
  1048. #define DWC2_CORE_REV_2_72a 0x4f54272a
  1049. #define DWC2_CORE_REV_2_80a 0x4f54280a
  1050. #define DWC2_CORE_REV_2_90a 0x4f54290a
  1051. #define DWC2_CORE_REV_2_91a 0x4f54291a
  1052. #define DWC2_CORE_REV_2_92a 0x4f54292a
  1053. #define DWC2_CORE_REV_2_94a 0x4f54294a
  1054. #define DWC2_CORE_REV_3_00a 0x4f54300a
  1055. #define DWC2_CORE_REV_3_10a 0x4f54310a
  1056. #define DWC2_CORE_REV_4_00a 0x4f54400a
  1057. #define DWC2_FS_IOT_REV_1_00a 0x5531100a
  1058. #define DWC2_HS_IOT_REV_1_00a 0x5532100a
  1059. /* DWC OTG HW Core ID */
  1060. #define DWC2_OTG_ID 0x4f540000
  1061. #define DWC2_FS_IOT_ID 0x55310000
  1062. #define DWC2_HS_IOT_ID 0x55320000
  1063. #if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
  1064. union dwc2_hcd_internal_flags {
  1065. u32 d32;
  1066. struct {
  1067. unsigned port_connect_status_change:1;
  1068. unsigned port_connect_status:1;
  1069. unsigned port_reset_change:1;
  1070. unsigned port_enable_change:1;
  1071. unsigned port_suspend_change:1;
  1072. unsigned port_over_current_change:1;
  1073. unsigned port_l1_change:1;
  1074. unsigned reserved:25;
  1075. } b;
  1076. } flags;
  1077. struct list_head non_periodic_sched_inactive;
  1078. struct list_head non_periodic_sched_waiting;
  1079. struct list_head non_periodic_sched_active;
  1080. struct list_head *non_periodic_qh_ptr;
  1081. struct list_head periodic_sched_inactive;
  1082. struct list_head periodic_sched_ready;
  1083. struct list_head periodic_sched_assigned;
  1084. struct list_head periodic_sched_queued;
  1085. struct list_head split_order;
  1086. u16 periodic_usecs;
  1087. unsigned long hs_periodic_bitmap[
  1088. DIV_ROUND_UP(DWC2_HS_SCHEDULE_US, BITS_PER_LONG)];
  1089. u16 periodic_qh_count;
  1090. bool bus_suspended;
  1091. bool new_connection;
  1092. u16 last_frame_num;
  1093. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  1094. #define FRAME_NUM_ARRAY_SIZE 1000
  1095. u16 *frame_num_array;
  1096. u16 *last_frame_num_array;
  1097. int frame_num_idx;
  1098. int dumped_frame_num_array;
  1099. #endif
  1100. struct list_head free_hc_list;
  1101. int periodic_channels;
  1102. int non_periodic_channels;
  1103. int available_host_channels;
  1104. struct dwc2_host_chan *hc_ptr_array[MAX_EPS_CHANNELS];
  1105. u8 *status_buf;
  1106. dma_addr_t status_buf_dma;
  1107. #define DWC2_HCD_STATUS_BUF_SIZE 64
  1108. struct delayed_work start_work;
  1109. struct delayed_work reset_work;
  1110. u8 otg_port;
  1111. u32 *frame_list;
  1112. dma_addr_t frame_list_dma;
  1113. u32 frame_list_sz;
  1114. struct kmem_cache *desc_gen_cache;
  1115. struct kmem_cache *desc_hsisoc_cache;
  1116. #endif /* CONFIG_USB_DWC2_HOST || CONFIG_USB_DWC2_DUAL_ROLE */
  1117. #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
  1118. IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
  1119. /* Gadget structures */
  1120. struct usb_gadget_driver *driver;
  1121. int fifo_mem;
  1122. unsigned int dedicated_fifos:1;
  1123. unsigned char num_of_eps;
  1124. u32 fifo_map;
  1125. struct usb_request *ep0_reply;
  1126. struct usb_request *ctrl_req;
  1127. void *ep0_buff;
  1128. void *ctrl_buff;
  1129. enum dwc2_ep0_state ep0_state;
  1130. u8 test_mode;
  1131. dma_addr_t setup_desc_dma[2];
  1132. struct dwc2_dma_desc *setup_desc[2];
  1133. dma_addr_t ctrl_in_desc_dma;
  1134. struct dwc2_dma_desc *ctrl_in_desc;
  1135. dma_addr_t ctrl_out_desc_dma;
  1136. struct dwc2_dma_desc *ctrl_out_desc;
  1137. struct usb_gadget gadget;
  1138. unsigned int enabled:1;
  1139. unsigned int connected:1;
  1140. unsigned int remote_wakeup_allowed:1;
  1141. struct dwc2_hsotg_ep *eps_in[MAX_EPS_CHANNELS];
  1142. struct dwc2_hsotg_ep *eps_out[MAX_EPS_CHANNELS];
  1143. #endif /* CONFIG_USB_DWC2_PERIPHERAL || CONFIG_USB_DWC2_DUAL_ROLE */
  1144. };
  1145. /* Reasons for halting a host channel */
  1146. enum dwc2_halt_status {
  1147. DWC2_HC_XFER_NO_HALT_STATUS,
  1148. DWC2_HC_XFER_COMPLETE,
  1149. DWC2_HC_XFER_URB_COMPLETE,
  1150. DWC2_HC_XFER_ACK,
  1151. DWC2_HC_XFER_NAK,
  1152. DWC2_HC_XFER_NYET,
  1153. DWC2_HC_XFER_STALL,
  1154. DWC2_HC_XFER_XACT_ERR,
  1155. DWC2_HC_XFER_FRAME_OVERRUN,
  1156. DWC2_HC_XFER_BABBLE_ERR,
  1157. DWC2_HC_XFER_DATA_TOGGLE_ERR,
  1158. DWC2_HC_XFER_AHB_ERR,
  1159. DWC2_HC_XFER_PERIODIC_INCOMPLETE,
  1160. DWC2_HC_XFER_URB_DEQUEUE,
  1161. };
  1162. /* Core version information */
  1163. static inline bool dwc2_is_iot(struct dwc2_hsotg *hsotg)
  1164. {
  1165. return (hsotg->hw_params.snpsid & 0xfff00000) == 0x55300000;
  1166. }
  1167. static inline bool dwc2_is_fs_iot(struct dwc2_hsotg *hsotg)
  1168. {
  1169. return (hsotg->hw_params.snpsid & 0xffff0000) == 0x55310000;
  1170. }
  1171. static inline bool dwc2_is_hs_iot(struct dwc2_hsotg *hsotg)
  1172. {
  1173. return (hsotg->hw_params.snpsid & 0xffff0000) == 0x55320000;
  1174. }
  1175. /*
  1176. * The following functions support initialization of the core driver component
  1177. * and the DWC_otg controller
  1178. */
  1179. int dwc2_core_reset(struct dwc2_hsotg *hsotg, bool skip_wait);
  1180. int dwc2_enter_partial_power_down(struct dwc2_hsotg *hsotg);
  1181. int dwc2_exit_partial_power_down(struct dwc2_hsotg *hsotg, bool restore);
  1182. int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg, int is_host);
  1183. int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
  1184. int reset, int is_host);
  1185. void dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host);
  1186. void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg);
  1187. bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg);
  1188. /*
  1189. * Common core Functions.
  1190. * The following functions support managing the DWC_otg controller in either
  1191. * device or host mode.
  1192. */
  1193. void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes);
  1194. void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num);
  1195. void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg);
  1196. void dwc2_enable_global_interrupts(struct dwc2_hsotg *hcd);
  1197. void dwc2_disable_global_interrupts(struct dwc2_hsotg *hcd);
  1198. void dwc2_hib_restore_common(struct dwc2_hsotg *hsotg, int rem_wakeup,
  1199. int is_host);
  1200. int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg);
  1201. int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg);
  1202. void dwc2_enable_acg(struct dwc2_hsotg *hsotg);
  1203. /* This function should be called on every hardware interrupt. */
  1204. irqreturn_t dwc2_handle_common_intr(int irq, void *dev);
  1205. /* The device ID match table */
  1206. extern const struct of_device_id dwc2_of_match_table[];
  1207. int dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg);
  1208. int dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg);
  1209. /* Common polling functions */
  1210. int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg, u32 bit,
  1211. u32 timeout);
  1212. int dwc2_hsotg_wait_bit_clear(struct dwc2_hsotg *hs_otg, u32 reg, u32 bit,
  1213. u32 timeout);
  1214. /* Parameters */
  1215. int dwc2_get_hwparams(struct dwc2_hsotg *hsotg);
  1216. int dwc2_init_params(struct dwc2_hsotg *hsotg);
  1217. /*
  1218. * The following functions check the controller's OTG operation mode
  1219. * capability (GHWCFG2.OTG_MODE).
  1220. *
  1221. * These functions can be used before the internal hsotg->hw_params
  1222. * are read in and cached so they always read directly from the
  1223. * GHWCFG2 register.
  1224. */
  1225. unsigned int dwc2_op_mode(struct dwc2_hsotg *hsotg);
  1226. bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg);
  1227. bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg);
  1228. bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg);
  1229. /*
  1230. * Returns the mode of operation, host or device
  1231. */
  1232. static inline int dwc2_is_host_mode(struct dwc2_hsotg *hsotg)
  1233. {
  1234. return (dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_CURMODE_HOST) != 0;
  1235. }
  1236. static inline int dwc2_is_device_mode(struct dwc2_hsotg *hsotg)
  1237. {
  1238. return (dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_CURMODE_HOST) == 0;
  1239. }
  1240. /*
  1241. * Dump core registers and SPRAM
  1242. */
  1243. void dwc2_dump_dev_registers(struct dwc2_hsotg *hsotg);
  1244. void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg);
  1245. void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg);
  1246. /* Gadget defines */
  1247. #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
  1248. IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
  1249. int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg);
  1250. int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2);
  1251. int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2);
  1252. int dwc2_gadget_init(struct dwc2_hsotg *hsotg);
  1253. void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2,
  1254. bool reset);
  1255. void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg);
  1256. void dwc2_hsotg_disconnect(struct dwc2_hsotg *dwc2);
  1257. int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode);
  1258. #define dwc2_is_device_connected(hsotg) (hsotg->connected)
  1259. int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg);
  1260. int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup);
  1261. int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg);
  1262. int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
  1263. int rem_wakeup, int reset);
  1264. int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg);
  1265. int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg);
  1266. int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg);
  1267. void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg);
  1268. #else
  1269. static inline int dwc2_hsotg_remove(struct dwc2_hsotg *dwc2)
  1270. { return 0; }
  1271. static inline int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2)
  1272. { return 0; }
  1273. static inline int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2)
  1274. { return 0; }
  1275. static inline int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
  1276. { return 0; }
  1277. static inline void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2,
  1278. bool reset) {}
  1279. static inline void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg) {}
  1280. static inline void dwc2_hsotg_disconnect(struct dwc2_hsotg *dwc2) {}
  1281. static inline int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg,
  1282. int testmode)
  1283. { return 0; }
  1284. #define dwc2_is_device_connected(hsotg) (0)
  1285. static inline int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
  1286. { return 0; }
  1287. static inline int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg,
  1288. int remote_wakeup)
  1289. { return 0; }
  1290. static inline int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
  1291. { return 0; }
  1292. static inline int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
  1293. int rem_wakeup, int reset)
  1294. { return 0; }
  1295. static inline int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
  1296. { return 0; }
  1297. static inline int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
  1298. { return 0; }
  1299. static inline int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
  1300. { return 0; }
  1301. static inline void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg) {}
  1302. #endif
  1303. #if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
  1304. int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg);
  1305. int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us);
  1306. void dwc2_hcd_connect(struct dwc2_hsotg *hsotg);
  1307. void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force);
  1308. void dwc2_hcd_start(struct dwc2_hsotg *hsotg);
  1309. int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup);
  1310. int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg);
  1311. int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg);
  1312. int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg);
  1313. int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg,
  1314. int rem_wakeup, int reset);
  1315. #else
  1316. static inline int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
  1317. { return 0; }
  1318. static inline int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg,
  1319. int us)
  1320. { return 0; }
  1321. static inline void dwc2_hcd_connect(struct dwc2_hsotg *hsotg) {}
  1322. static inline void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force) {}
  1323. static inline void dwc2_hcd_start(struct dwc2_hsotg *hsotg) {}
  1324. static inline void dwc2_hcd_remove(struct dwc2_hsotg *hsotg) {}
  1325. static inline int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
  1326. { return 0; }
  1327. static inline int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
  1328. { return 0; }
  1329. static inline int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
  1330. { return 0; }
  1331. static inline int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
  1332. { return 0; }
  1333. static inline int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
  1334. { return 0; }
  1335. static inline int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg,
  1336. int rem_wakeup, int reset)
  1337. { return 0; }
  1338. #endif
  1339. #endif /* __DWC2_CORE_H__ */