core.h 48 KB

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