core.h 52 KB

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