core.h 51 KB

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