core.h 48 KB

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