core.h 52 KB

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