cdnsp-gadget.h 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Cadence CDNSP DRD Driver.
  4. *
  5. * Copyright (C) 2020 Cadence.
  6. *
  7. * Author: Pawel Laszczak <pawell@cadence.com>
  8. *
  9. * Code based on Linux XHCI driver.
  10. * Origin: Copyright (C) 2008 Intel Corp.
  11. */
  12. #ifndef __LINUX_CDNSP_GADGET_H
  13. #define __LINUX_CDNSP_GADGET_H
  14. #include <linux/io-64-nonatomic-lo-hi.h>
  15. #include <linux/usb/gadget.h>
  16. #include <linux/irq.h>
  17. /* Max number slots - only 1 is allowed. */
  18. #define CDNSP_DEV_MAX_SLOTS 1
  19. #define CDNSP_EP0_SETUP_SIZE 512
  20. /* One control and 15 for in and 15 for out endpoints. */
  21. #define CDNSP_ENDPOINTS_NUM 31
  22. /* Best Effort Service Latency. */
  23. #define CDNSP_DEFAULT_BESL 0
  24. /* Device Controller command default timeout value in us */
  25. #define CDNSP_CMD_TIMEOUT (15 * 1000)
  26. /* Up to 16 ms to halt an device controller */
  27. #define CDNSP_MAX_HALT_USEC (16 * 1000)
  28. #define CDNSP_CTX_SIZE 2112
  29. /*
  30. * Controller register interface.
  31. */
  32. /**
  33. * struct cdnsp_cap_regs - CDNSP Registers.
  34. * @hc_capbase: Length of the capabilities register and controller
  35. * version number
  36. * @hcs_params1: HCSPARAMS1 - Structural Parameters 1
  37. * @hcs_params2: HCSPARAMS2 - Structural Parameters 2
  38. * @hcs_params3: HCSPARAMS3 - Structural Parameters 3
  39. * @hcc_params: HCCPARAMS - Capability Parameters
  40. * @db_off: DBOFF - Doorbell array offset
  41. * @run_regs_off: RTSOFF - Runtime register space offset
  42. * @hcc_params2: HCCPARAMS2 Capability Parameters 2,
  43. */
  44. struct cdnsp_cap_regs {
  45. __le32 hc_capbase;
  46. __le32 hcs_params1;
  47. __le32 hcs_params2;
  48. __le32 hcs_params3;
  49. __le32 hcc_params;
  50. __le32 db_off;
  51. __le32 run_regs_off;
  52. __le32 hcc_params2;
  53. /* Reserved up to (CAPLENGTH - 0x1C) */
  54. };
  55. /* hc_capbase bitmasks. */
  56. /* bits 7:0 - how long is the Capabilities register. */
  57. #define HC_LENGTH(p) (((p) >> 00) & GENMASK(7, 0))
  58. /* bits 31:16 */
  59. #define HC_VERSION(p) (((p) >> 16) & GENMASK(15, 1))
  60. /* HCSPARAMS1 - hcs_params1 - bitmasks */
  61. /* bits 0:7, Max Device Endpoints */
  62. #define HCS_ENDPOINTS_MASK GENMASK(7, 0)
  63. #define HCS_ENDPOINTS(p) (((p) & HCS_ENDPOINTS_MASK) >> 0)
  64. /* HCCPARAMS offset from PCI base address */
  65. #define HCC_PARAMS_OFFSET 0x10
  66. /* HCCPARAMS - hcc_params - bitmasks */
  67. /* 1: device controller can use 64-bit address pointers. */
  68. #define HCC_64BIT_ADDR(p) ((p) & BIT(0))
  69. /* 1: device controller uses 64-byte Device Context structures. */
  70. #define HCC_64BYTE_CONTEXT(p) ((p) & BIT(2))
  71. /* Max size for Primary Stream Arrays - 2^(n+1), where n is bits 12:15. */
  72. #define HCC_MAX_PSA(p) ((((p) >> 12) & 0xf) + 1)
  73. /* Extended Capabilities pointer from PCI base. */
  74. #define HCC_EXT_CAPS(p) (((p) & GENMASK(31, 16)) >> 16)
  75. #define CTX_SIZE(_hcc) (HCC_64BYTE_CONTEXT(_hcc) ? 64 : 32)
  76. /* db_off bitmask - bits 0:1 reserved. */
  77. #define DBOFF_MASK GENMASK(31, 2)
  78. /* run_regs_off bitmask - bits 0:4 reserved. */
  79. #define RTSOFF_MASK GENMASK(31, 5)
  80. /**
  81. * struct cdnsp_op_regs - Device Controller Operational Registers.
  82. * @command: USBCMD - Controller command register.
  83. * @status: USBSTS - Controller status register.
  84. * @page_size: This indicates the page size that the device controller supports.
  85. * If bit n is set, the controller supports a page size of 2^(n+12),
  86. * up to a 128MB page size. 4K is the minimum page size.
  87. * @dnctrl: DNCTRL - Device notification control register.
  88. * @cmd_ring: CRP - 64-bit Command Ring Pointer.
  89. * @dcbaa_ptr: DCBAAP - 64-bit Device Context Base Address Array Pointer.
  90. * @config_reg: CONFIG - Configure Register
  91. * @port_reg_base: PORTSCn - base address for Port Status and Control
  92. * Each port has a Port Status and Control register,
  93. * followed by a Port Power Management Status and Control
  94. * register, a Port Link Info register, and a reserved
  95. * register.
  96. */
  97. struct cdnsp_op_regs {
  98. __le32 command;
  99. __le32 status;
  100. __le32 page_size;
  101. __le32 reserved1;
  102. __le32 reserved2;
  103. __le32 dnctrl;
  104. __le64 cmd_ring;
  105. /* rsvd: offset 0x20-2F. */
  106. __le32 reserved3[4];
  107. __le64 dcbaa_ptr;
  108. __le32 config_reg;
  109. /* rsvd: offset 0x3C-3FF. */
  110. __le32 reserved4[241];
  111. /* port 1 registers, which serve as a base address for other ports. */
  112. __le32 port_reg_base;
  113. };
  114. /* Number of registers per port. */
  115. #define NUM_PORT_REGS 4
  116. /**
  117. * struct cdnsp_port_regs - Port Registers.
  118. * @portsc: PORTSC - Port Status and Control Register.
  119. * @portpmsc: PORTPMSC - Port Power Managements Status and Control Register.
  120. * @portli: PORTLI - Port Link Info register.
  121. */
  122. struct cdnsp_port_regs {
  123. __le32 portsc;
  124. __le32 portpmsc;
  125. __le32 portli;
  126. __le32 reserved;
  127. };
  128. /*
  129. * These bits are Read Only (RO) and should be saved and written to the
  130. * registers: 0 (connect status) and 10:13 (port speed).
  131. * These bits are also sticky - meaning they're in the AUX well and they aren't
  132. * changed by a hot and warm.
  133. */
  134. #define CDNSP_PORT_RO (PORT_CONNECT | DEV_SPEED_MASK)
  135. /*
  136. * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
  137. * bits 5:8 (link state), 25:26 ("wake on" enable state)
  138. */
  139. #define CDNSP_PORT_RWS (PORT_PLS_MASK | PORT_WKCONN_E | PORT_WKDISC_E)
  140. /*
  141. * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
  142. * bits 1 (port enable/disable), 17 ( connect changed),
  143. * 21 (port reset changed) , 22 (Port Link State Change),
  144. */
  145. #define CDNSP_PORT_RW1CS (PORT_PED | PORT_CSC | PORT_RC | PORT_PLC)
  146. /* USBCMD - USB command - bitmasks. */
  147. /* Run/Stop, controller execution - do not write unless controller is halted.*/
  148. #define CMD_R_S BIT(0)
  149. /*
  150. * Reset device controller - resets internal controller state machine and all
  151. * registers (except PCI config regs).
  152. */
  153. #define CMD_RESET BIT(1)
  154. /* Event Interrupt Enable - a '1' allows interrupts from the controller. */
  155. #define CMD_INTE BIT(2)
  156. /*
  157. * Device System Error Interrupt Enable - get out-of-band signal for
  158. * controller errors.
  159. */
  160. #define CMD_DSEIE BIT(3)
  161. /* device controller save/restore state. */
  162. #define CMD_CSS BIT(8)
  163. #define CMD_CRS BIT(9)
  164. /*
  165. * Enable Wrap Event - '1' means device controller generates an event
  166. * when MFINDEX wraps.
  167. */
  168. #define CMD_EWE BIT(10)
  169. /* 1: device enabled */
  170. #define CMD_DEVEN BIT(17)
  171. /* bits 18:31 are reserved (and should be preserved on writes). */
  172. /* Command register values to disable interrupts. */
  173. #define CDNSP_IRQS (CMD_INTE | CMD_DSEIE | CMD_EWE)
  174. /* USBSTS - USB status - bitmasks */
  175. /* controller not running - set to 1 when run/stop bit is cleared. */
  176. #define STS_HALT BIT(0)
  177. /*
  178. * serious error, e.g. PCI parity error. The controller will clear
  179. * the run/stop bit.
  180. */
  181. #define STS_FATAL BIT(2)
  182. /* event interrupt - clear this prior to clearing any IP flags in IR set.*/
  183. #define STS_EINT BIT(3)
  184. /* port change detect */
  185. #define STS_PCD BIT(4)
  186. /* save state status - '1' means device controller is saving state. */
  187. #define STS_SSS BIT(8)
  188. /* restore state status - '1' means controllers is restoring state. */
  189. #define STS_RSS BIT(9)
  190. /* 1: save or restore error */
  191. #define STS_SRE BIT(10)
  192. /* 1: device Not Ready to accept doorbell or op reg writes after reset. */
  193. #define STS_CNR BIT(11)
  194. /* 1: internal Device Controller Error.*/
  195. #define STS_HCE BIT(12)
  196. /* CRCR - Command Ring Control Register - cmd_ring bitmasks. */
  197. /* bit 0 is the command ring cycle state. */
  198. #define CMD_RING_CS BIT(0)
  199. /* stop ring immediately - abort the currently executing command. */
  200. #define CMD_RING_ABORT BIT(2)
  201. /*
  202. * Command Ring Busy.
  203. * Set when Doorbell register is written with DB for command and cleared when
  204. * the controller reached end of CR.
  205. */
  206. #define CMD_RING_BUSY(p) ((p) & BIT(4))
  207. /* 1: command ring is running */
  208. #define CMD_RING_RUNNING BIT(3)
  209. /* Command Ring pointer - bit mask for the lower 32 bits. */
  210. #define CMD_RING_RSVD_BITS GENMASK(5, 0)
  211. /* CONFIG - Configure Register - config_reg bitmasks. */
  212. /* bits 0:7 - maximum number of device slots enabled. */
  213. #define MAX_DEVS GENMASK(7, 0)
  214. /* bit 8: U3 Entry Enabled, assert PLC when controller enters U3. */
  215. #define CONFIG_U3E BIT(8)
  216. /* PORTSC - Port Status and Control Register - port_reg_base bitmasks */
  217. /* 1: device connected. */
  218. #define PORT_CONNECT BIT(0)
  219. /* 1: port enabled. */
  220. #define PORT_PED BIT(1)
  221. /* 1: port reset signaling asserted. */
  222. #define PORT_RESET BIT(4)
  223. /*
  224. * Port Link State - bits 5:8
  225. * A read gives the current link PM state of the port,
  226. * a write with Link State Write Strobe sets the link state.
  227. */
  228. #define PORT_PLS_MASK GENMASK(8, 5)
  229. #define XDEV_U0 (0x0 << 5)
  230. #define XDEV_U1 (0x1 << 5)
  231. #define XDEV_U2 (0x2 << 5)
  232. #define XDEV_U3 (0x3 << 5)
  233. #define XDEV_DISABLED (0x4 << 5)
  234. #define XDEV_RXDETECT (0x5 << 5)
  235. #define XDEV_INACTIVE (0x6 << 5)
  236. #define XDEV_POLLING (0x7 << 5)
  237. #define XDEV_RECOVERY (0x8 << 5)
  238. #define XDEV_HOT_RESET (0x9 << 5)
  239. #define XDEV_COMP_MODE (0xa << 5)
  240. #define XDEV_TEST_MODE (0xb << 5)
  241. #define XDEV_RESUME (0xf << 5)
  242. /* 1: port has power. */
  243. #define PORT_POWER BIT(9)
  244. /*
  245. * bits 10:13 indicate device speed:
  246. * 0 - undefined speed - port hasn't be initialized by a reset yet
  247. * 1 - full speed
  248. * 2 - Reserved (Low Speed not supported
  249. * 3 - high speed
  250. * 4 - super speed
  251. * 5 - super speed
  252. * 6-15 reserved
  253. */
  254. #define DEV_SPEED_MASK GENMASK(13, 10)
  255. #define XDEV_FS (0x1 << 10)
  256. #define XDEV_HS (0x3 << 10)
  257. #define XDEV_SS (0x4 << 10)
  258. #define XDEV_SSP (0x5 << 10)
  259. #define XDEV_SSP1x2 (0x6 << 10)
  260. #define XDEV_SSP2x2 (0x7 << 10)
  261. #define DEV_UNDEFSPEED(p) (((p) & DEV_SPEED_MASK) == (0x0 << 10))
  262. #define DEV_FULLSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_FS)
  263. #define DEV_HIGHSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_HS)
  264. #define DEV_SUPERSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_SS)
  265. #define DEV_SUPERSPEEDPLUS(p) (((p) & DEV_SPEED_MASK) == XDEV_SSP)
  266. #define DEV_SSP_GEN1x2(p) (((p) & DEV_SPEED_MASK) == XDEV_SSP1x2)
  267. #define DEV_SSP_GEN2x2(p) (((p) & DEV_SPEED_MASK) == XDEV_SSP2x2)
  268. #define DEV_SUPERSPEED_ANY(p) (((p) & DEV_SPEED_MASK) >= XDEV_SS)
  269. #define DEV_PORT_SPEED(p) (((p) >> 10) & 0x0f)
  270. /* Port Link State Write Strobe - set this when changing link state */
  271. #define PORT_LINK_STROBE BIT(16)
  272. /* 1: connect status change */
  273. #define PORT_CSC BIT(17)
  274. /* 1: warm reset for a USB 3.0 device is done. */
  275. #define PORT_WRC BIT(19)
  276. /* 1: reset change - 1 to 0 transition of PORT_RESET */
  277. #define PORT_RC BIT(21)
  278. /*
  279. * port link status change - set on some port link state transitions:
  280. * Transition Reason
  281. * ----------------------------------------------------------------------------
  282. * - U3 to Resume Wakeup signaling from a device
  283. * - Resume to Recovery to U0 USB 3.0 device resume
  284. * - Resume to U0 USB 2.0 device resume
  285. * - U3 to Recovery to U0 Software resume of USB 3.0 device complete
  286. * - U3 to U0 Software resume of USB 2.0 device complete
  287. * - U2 to U0 L1 resume of USB 2.1 device complete
  288. * - U0 to U0 L1 entry rejection by USB 2.1 device
  289. * - U0 to disabled L1 entry error with USB 2.1 device
  290. * - Any state to inactive Error on USB 3.0 port
  291. */
  292. #define PORT_PLC BIT(22)
  293. /* Port configure error change - port failed to configure its link partner. */
  294. #define PORT_CEC BIT(23)
  295. /* Wake on connect (enable). */
  296. #define PORT_WKCONN_E BIT(25)
  297. /* Wake on disconnect (enable). */
  298. #define PORT_WKDISC_E BIT(26)
  299. /* Indicates if Warm Reset is being received. */
  300. #define PORT_WR BIT(31)
  301. #define PORT_CHANGE_BITS (PORT_CSC | PORT_WRC | PORT_RC | PORT_PLC | PORT_CEC)
  302. /* PORTPMSCUSB3 - Port Power Management Status and Control - bitmasks. */
  303. /* Enables U1 entry. */
  304. #define PORT_U1_TIMEOUT_MASK GENMASK(7, 0)
  305. #define PORT_U1_TIMEOUT(p) ((p) & PORT_U1_TIMEOUT_MASK)
  306. /* Enables U2 entry .*/
  307. #define PORT_U2_TIMEOUT_MASK GENMASK(14, 8)
  308. #define PORT_U2_TIMEOUT(p) (((p) << 8) & PORT_U2_TIMEOUT_MASK)
  309. /* PORTPMSCUSB2 - Port Power Management Status and Control - bitmasks. */
  310. #define PORT_L1S_MASK GENMASK(2, 0)
  311. #define PORT_L1S(p) ((p) & PORT_L1S_MASK)
  312. #define PORT_L1S_ACK PORT_L1S(1)
  313. #define PORT_L1S_NYET PORT_L1S(2)
  314. #define PORT_L1S_STALL PORT_L1S(3)
  315. #define PORT_L1S_TIMEOUT PORT_L1S(4)
  316. /* Remote Wake Enable. */
  317. #define PORT_RWE BIT(3)
  318. /* Best Effort Service Latency (BESL). */
  319. #define PORT_BESL(p) (((p) << 4) & GENMASK(7, 4))
  320. /* Hardware LPM Enable (HLE). */
  321. #define PORT_HLE BIT(16)
  322. /* Received Best Effort Service Latency (BESL). */
  323. #define PORT_RRBESL(p) (((p) & GENMASK(20, 17)) >> 17)
  324. /* Port Test Control. */
  325. #define PORT_TEST_MODE_MASK GENMASK(31, 28)
  326. #define PORT_TEST_MODE(p) (((p) << 28) & PORT_TEST_MODE_MASK)
  327. /**
  328. * struct cdnsp_intr_reg - Interrupt Register Set.
  329. * @irq_pending: IMAN - Interrupt Management Register. Used to enable
  330. * interrupts and check for pending interrupts.
  331. * @irq_control: IMOD - Interrupt Moderation Register.
  332. * Used to throttle interrupts.
  333. * @erst_size: Number of segments in the Event Ring Segment Table (ERST).
  334. * @erst_base: ERST base address.
  335. * @erst_dequeue: Event ring dequeue pointer.
  336. *
  337. * Each interrupter (defined by a MSI-X vector) has an event ring and an Event
  338. * Ring Segment Table (ERST) associated with it. The event ring is comprised of
  339. * multiple segments of the same size. The controller places events on the ring
  340. * and "updates the Cycle bit in the TRBs to indicate to software the current
  341. * position of the Enqueue Pointer." The driver processes those events and
  342. * updates the dequeue pointer.
  343. */
  344. struct cdnsp_intr_reg {
  345. __le32 irq_pending;
  346. __le32 irq_control;
  347. __le32 erst_size;
  348. __le32 rsvd;
  349. __le64 erst_base;
  350. __le64 erst_dequeue;
  351. };
  352. /* IMAN - Interrupt Management Register - irq_pending bitmasks l. */
  353. #define IMAN_IE BIT(1)
  354. #define IMAN_IP BIT(0)
  355. /* bits 2:31 need to be preserved */
  356. #define IMAN_IE_SET(p) ((p) | IMAN_IE)
  357. #define IMAN_IE_CLEAR(p) ((p) & ~IMAN_IE)
  358. /* IMOD - Interrupter Moderation Register - irq_control bitmasks. */
  359. /*
  360. * Minimum interval between interrupts (in 250ns intervals). The interval
  361. * between interrupts will be longer if there are no events on the event ring.
  362. * Default is 4000 (1 ms).
  363. */
  364. #define IMOD_INTERVAL_MASK GENMASK(15, 0)
  365. /* Counter used to count down the time to the next interrupt - HW use only */
  366. #define IMOD_COUNTER_MASK GENMASK(31, 16)
  367. #define IMOD_DEFAULT_INTERVAL 0
  368. /* erst_size bitmasks. */
  369. /* Preserve bits 16:31 of erst_size. */
  370. #define ERST_SIZE_MASK GENMASK(31, 16)
  371. /* erst_dequeue bitmasks. */
  372. /*
  373. * Dequeue ERST Segment Index (DESI) - Segment number (or alias)
  374. * where the current dequeue pointer lies. This is an optional HW hint.
  375. */
  376. #define ERST_DESI_MASK GENMASK(2, 0)
  377. /* Event Handler Busy (EHB) - is the event ring scheduled to be serviced. */
  378. #define ERST_EHB BIT(3)
  379. #define ERST_PTR_MASK GENMASK(3, 0)
  380. /**
  381. * struct cdnsp_run_regs
  382. * @microframe_index: MFINDEX - current microframe number.
  383. * @ir_set: Array of Interrupter registers.
  384. *
  385. * Device Controller Runtime Registers:
  386. * "Software should read and write these registers using only Dword (32 bit)
  387. * or larger accesses"
  388. */
  389. struct cdnsp_run_regs {
  390. __le32 microframe_index;
  391. __le32 rsvd[7];
  392. struct cdnsp_intr_reg ir_set[128];
  393. };
  394. /**
  395. * USB2.0 Port Peripheral Configuration Registers.
  396. * @ext_cap: Header register for Extended Capability.
  397. * @port_reg1: Timer Configuration Register.
  398. * @port_reg2: Timer Configuration Register.
  399. * @port_reg3: Timer Configuration Register.
  400. * @port_reg4: Timer Configuration Register.
  401. * @port_reg5: Timer Configuration Register.
  402. * @port_reg6: Chicken bits for USB20PPP.
  403. */
  404. struct cdnsp_20port_cap {
  405. __le32 ext_cap;
  406. __le32 port_reg1;
  407. __le32 port_reg2;
  408. __le32 port_reg3;
  409. __le32 port_reg4;
  410. __le32 port_reg5;
  411. __le32 port_reg6;
  412. };
  413. /* Extended capability register fields */
  414. #define EXT_CAPS_ID(p) (((p) >> 0) & GENMASK(7, 0))
  415. #define EXT_CAPS_NEXT(p) (((p) >> 8) & GENMASK(7, 0))
  416. /* Extended capability IDs - ID 0 reserved */
  417. #define EXT_CAPS_PROTOCOL 2
  418. /* USB 2.0 Port Peripheral Configuration Extended Capability */
  419. #define EXT_CAP_CFG_DEV_20PORT_CAP_ID 0xC1
  420. /*
  421. * Setting this bit to '1' enables automatic wakeup from L1 state on transfer
  422. * TRB prepared when USBSSP operates in USB2.0 mode.
  423. */
  424. #define PORT_REG6_L1_L0_HW_EN BIT(1)
  425. /*
  426. * Setting this bit to '1' forces Full Speed when USBSSP operates in USB2.0
  427. * mode (disables High Speed).
  428. */
  429. #define PORT_REG6_FORCE_FS BIT(0)
  430. /**
  431. * USB3.x Port Peripheral Configuration Registers.
  432. * @ext_cap: Header register for Extended Capability.
  433. * @mode_addr: Miscellaneous 3xPORT operation mode configuration register.
  434. * @mode_2: 3x Port Control Register 2.
  435. */
  436. struct cdnsp_3xport_cap {
  437. __le32 ext_cap;
  438. __le32 mode_addr;
  439. __le32 reserved[52];
  440. __le32 mode_2;
  441. };
  442. /* Extended Capability Header for 3XPort Configuration Registers. */
  443. #define D_XEC_CFG_3XPORT_CAP 0xC0
  444. #define CFG_3XPORT_SSP_SUPPORT BIT(31)
  445. #define CFG_3XPORT_U1_PIPE_CLK_GATE_EN BIT(0)
  446. /* Revision Extended Capability ID */
  447. #define RTL_REV_CAP 0xC4
  448. #define RTL_REV_CAP_RX_BUFF_CMD_SIZE BITMASK(31, 24)
  449. #define RTL_REV_CAP_RX_BUFF_SIZE BITMASK(15, 0)
  450. #define RTL_REV_CAP_TX_BUFF_CMD_SIZE BITMASK(31, 24)
  451. #define RTL_REV_CAP_TX_BUFF_SIZE BITMASK(15, 0)
  452. #define CDNSP_VER_1 0x00000000
  453. #define CDNSP_VER_2 0x10000000
  454. #define CDNSP_IF_EP_EXIST(pdev, ep_num, dir) \
  455. (readl(&(pdev)->rev_cap->ep_supported) & \
  456. (BIT(ep_num) << ((dir) ? 0 : 16)))
  457. /**
  458. * struct cdnsp_rev_cap - controller capabilities.
  459. * @ext_cap: Header for RTL Revision Extended Capability.
  460. * @rtl_revision: RTL revision.
  461. * @rx_buff_size: Rx buffer sizes.
  462. * @tx_buff_size: Tx buffer sizes.
  463. * @ep_supported: Supported endpoints.
  464. * @ctrl_revision: Controller revision ID.
  465. */
  466. struct cdnsp_rev_cap {
  467. __le32 ext_cap;
  468. __le32 rtl_revision;
  469. __le32 rx_buff_size;
  470. __le32 tx_buff_size;
  471. __le32 ep_supported;
  472. __le32 ctrl_revision;
  473. };
  474. /* USB2.0 Port Peripheral Configuration Registers. */
  475. #define D_XEC_PRE_REGS_CAP 0xC8
  476. #define REG_CHICKEN_BITS_2_OFFSET 0x48
  477. #define CHICKEN_XDMA_2_TP_CACHE_DIS BIT(28)
  478. #define REG_CHICKEN_BITS_3_OFFSET 0x4C
  479. #define CHICKEN_APB_TIMEOUT_SET(p, val) (((p) & ~GENMASK(21, 0)) | (val))
  480. /* XBUF Extended Capability ID. */
  481. #define XBUF_CAP_ID 0xCB
  482. #define XBUF_RX_TAG_MASK_0_OFFSET 0x1C
  483. #define XBUF_RX_TAG_MASK_1_OFFSET 0x24
  484. #define XBUF_TX_CMD_OFFSET 0x2C
  485. /**
  486. * struct cdnsp_doorbell_array.
  487. * @cmd_db: Command ring doorbell register.
  488. * @ep_db: Endpoint ring doorbell register.
  489. * Bits 0 - 7: Endpoint target.
  490. * Bits 8 - 15: RsvdZ.
  491. * Bits 16 - 31: Stream ID.
  492. */
  493. struct cdnsp_doorbell_array {
  494. __le32 cmd_db;
  495. __le32 ep_db;
  496. };
  497. #define DB_VALUE(ep, stream) ((((ep) + 1) & 0xff) | ((stream) << 16))
  498. #define DB_VALUE_EP0_OUT(ep, stream) ((ep) & 0xff)
  499. #define DB_VALUE_CMD 0x00000000
  500. /**
  501. * struct cdnsp_container_ctx.
  502. * @type: Type of context. Used to calculated offsets to contained contexts.
  503. * @size: Size of the context data.
  504. * @ctx_size: context data structure size - 64 or 32 bits.
  505. * @dma: dma address of the bytes.
  506. * @bytes: The raw context data given to HW.
  507. *
  508. * Represents either a Device or Input context. Holds a pointer to the raw
  509. * memory used for the context (bytes) and dma address of it (dma).
  510. */
  511. struct cdnsp_container_ctx {
  512. unsigned int type;
  513. #define CDNSP_CTX_TYPE_DEVICE 0x1
  514. #define CDNSP_CTX_TYPE_INPUT 0x2
  515. int size;
  516. int ctx_size;
  517. dma_addr_t dma;
  518. u8 *bytes;
  519. };
  520. /**
  521. * struct cdnsp_slot_ctx
  522. * @dev_info: Device speed, and last valid endpoint.
  523. * @dev_port: Device port number that is needed to access the USB device.
  524. * @int_target: Interrupter target number.
  525. * @dev_state: Slot state and device address.
  526. *
  527. * Slot Context - This assumes the controller uses 32-byte context
  528. * structures. If the controller uses 64-byte contexts, there is an additional
  529. * 32 bytes reserved at the end of the slot context for controller internal use.
  530. */
  531. struct cdnsp_slot_ctx {
  532. __le32 dev_info;
  533. __le32 dev_port;
  534. __le32 int_target;
  535. __le32 dev_state;
  536. /* offset 0x10 to 0x1f reserved for controller internal use. */
  537. __le32 reserved[4];
  538. };
  539. /* Bits 20:23 in the Slot Context are the speed for the device. */
  540. #define SLOT_SPEED_FS (XDEV_FS << 10)
  541. #define SLOT_SPEED_HS (XDEV_HS << 10)
  542. #define SLOT_SPEED_SS (XDEV_SS << 10)
  543. #define SLOT_SPEED_SSP (XDEV_SSP << 10)
  544. /* dev_info bitmasks. */
  545. /* Device speed - values defined by PORTSC Device Speed field - 20:23. */
  546. #define DEV_SPEED GENMASK(23, 20)
  547. #define GET_DEV_SPEED(n) (((n) & DEV_SPEED) >> 20)
  548. /* Index of the last valid endpoint context in this device context - 27:31. */
  549. #define LAST_CTX_MASK ((unsigned int)GENMASK(31, 27))
  550. #define LAST_CTX(p) ((p) << 27)
  551. #define LAST_CTX_TO_EP_NUM(p) (((p) >> 27) - 1)
  552. #define SLOT_FLAG BIT(0)
  553. #define EP0_FLAG BIT(1)
  554. /* dev_port bitmasks */
  555. /* Device port number that is needed to access the USB device. */
  556. #define DEV_PORT(p) (((p) & 0xff) << 16)
  557. /* dev_state bitmasks */
  558. /* USB device address - assigned by the controller. */
  559. #define DEV_ADDR_MASK GENMASK(7, 0)
  560. /* Slot state */
  561. #define SLOT_STATE GENMASK(31, 27)
  562. #define GET_SLOT_STATE(p) (((p) & SLOT_STATE) >> 27)
  563. #define SLOT_STATE_DISABLED 0
  564. #define SLOT_STATE_ENABLED SLOT_STATE_DISABLED
  565. #define SLOT_STATE_DEFAULT 1
  566. #define SLOT_STATE_ADDRESSED 2
  567. #define SLOT_STATE_CONFIGURED 3
  568. /**
  569. * struct cdnsp_ep_ctx.
  570. * @ep_info: Endpoint state, streams, mult, and interval information.
  571. * @ep_info2: Information on endpoint type, max packet size, max burst size,
  572. * error count, and whether the controller will force an event for
  573. * all transactions.
  574. * @deq: 64-bit ring dequeue pointer address. If the endpoint only
  575. * defines one stream, this points to the endpoint transfer ring.
  576. * Otherwise, it points to a stream context array, which has a
  577. * ring pointer for each flow.
  578. * @tx_info: Average TRB lengths for the endpoint ring and
  579. * max payload within an Endpoint Service Interval Time (ESIT).
  580. *
  581. * Endpoint Context - This assumes the controller uses 32-byte context
  582. * structures. If the controller uses 64-byte contexts, there is an additional
  583. * 32 bytes reserved at the end of the endpoint context for controller internal
  584. * use.
  585. */
  586. struct cdnsp_ep_ctx {
  587. __le32 ep_info;
  588. __le32 ep_info2;
  589. __le64 deq;
  590. __le32 tx_info;
  591. /* offset 0x14 - 0x1f reserved for controller internal use. */
  592. __le32 reserved[3];
  593. };
  594. /* ep_info bitmasks. */
  595. /*
  596. * Endpoint State - bits 0:2:
  597. * 0 - disabled
  598. * 1 - running
  599. * 2 - halted due to halt condition
  600. * 3 - stopped
  601. * 4 - TRB error
  602. * 5-7 - reserved
  603. */
  604. #define EP_STATE_MASK GENMASK(3, 0)
  605. #define EP_STATE_DISABLED 0
  606. #define EP_STATE_RUNNING 1
  607. #define EP_STATE_HALTED 2
  608. #define EP_STATE_STOPPED 3
  609. #define EP_STATE_ERROR 4
  610. #define GET_EP_CTX_STATE(ctx) (le32_to_cpu((ctx)->ep_info) & EP_STATE_MASK)
  611. /* Mult - Max number of burst within an interval, in EP companion desc. */
  612. #define EP_MULT(p) (((p) << 8) & GENMASK(9, 8))
  613. #define CTX_TO_EP_MULT(p) (((p) & GENMASK(9, 8)) >> 8)
  614. /* bits 10:14 are Max Primary Streams. */
  615. /* bit 15 is Linear Stream Array. */
  616. /* Interval - period between requests to an endpoint - 125u increments. */
  617. #define EP_INTERVAL(p) (((p) << 16) & GENMASK(23, 16))
  618. #define EP_INTERVAL_TO_UFRAMES(p) (1 << (((p) & GENMASK(23, 16)) >> 16))
  619. #define CTX_TO_EP_INTERVAL(p) (((p) & GENMASK(23, 16)) >> 16)
  620. #define EP_MAXPSTREAMS_MASK GENMASK(14, 10)
  621. #define EP_MAXPSTREAMS(p) (((p) << 10) & EP_MAXPSTREAMS_MASK)
  622. #define CTX_TO_EP_MAXPSTREAMS(p) (((p) & EP_MAXPSTREAMS_MASK) >> 10)
  623. /* Endpoint is set up with a Linear Stream Array (vs. Secondary Stream Array) */
  624. #define EP_HAS_LSA BIT(15)
  625. /* ep_info2 bitmasks */
  626. #define ERROR_COUNT(p) (((p) & 0x3) << 1)
  627. #define CTX_TO_EP_TYPE(p) (((p) >> 3) & 0x7)
  628. #define EP_TYPE(p) ((p) << 3)
  629. #define ISOC_OUT_EP 1
  630. #define BULK_OUT_EP 2
  631. #define INT_OUT_EP 3
  632. #define CTRL_EP 4
  633. #define ISOC_IN_EP 5
  634. #define BULK_IN_EP 6
  635. #define INT_IN_EP 7
  636. /* bit 6 reserved. */
  637. /* bit 7 is Device Initiate Disable - for disabling stream selection. */
  638. #define MAX_BURST(p) (((p) << 8) & GENMASK(15, 8))
  639. #define CTX_TO_MAX_BURST(p) (((p) & GENMASK(15, 8)) >> 8)
  640. #define MAX_PACKET(p) (((p) << 16) & GENMASK(31, 16))
  641. #define MAX_PACKET_MASK GENMASK(31, 16)
  642. #define MAX_PACKET_DECODED(p) (((p) & GENMASK(31, 16)) >> 16)
  643. /* tx_info bitmasks. */
  644. #define EP_AVG_TRB_LENGTH(p) ((p) & GENMASK(15, 0))
  645. #define EP_MAX_ESIT_PAYLOAD_LO(p) (((p) << 16) & GENMASK(31, 16))
  646. #define EP_MAX_ESIT_PAYLOAD_HI(p) ((((p) & GENMASK(23, 16)) >> 16) << 24)
  647. #define CTX_TO_MAX_ESIT_PAYLOAD_LO(p) (((p) & GENMASK(31, 16)) >> 16)
  648. #define CTX_TO_MAX_ESIT_PAYLOAD_HI(p) (((p) & GENMASK(31, 24)) >> 24)
  649. /* deq bitmasks. */
  650. #define EP_CTX_CYCLE_MASK BIT(0)
  651. #define CTX_DEQ_MASK (~0xfL)
  652. /**
  653. * struct cdnsp_input_control_context
  654. * Input control context;
  655. *
  656. * @drop_context: Set the bit of the endpoint context you want to disable.
  657. * @add_context: Set the bit of the endpoint context you want to enable.
  658. */
  659. struct cdnsp_input_control_ctx {
  660. __le32 drop_flags;
  661. __le32 add_flags;
  662. __le32 rsvd2[6];
  663. };
  664. /**
  665. * Represents everything that is needed to issue a command on the command ring.
  666. *
  667. * @in_ctx: Pointer to input context structure.
  668. * @status: Command Completion Code for last command.
  669. * @command_trb: Pointer to command TRB.
  670. */
  671. struct cdnsp_command {
  672. /* Input context for changing device state. */
  673. struct cdnsp_container_ctx *in_ctx;
  674. u32 status;
  675. union cdnsp_trb *command_trb;
  676. };
  677. /**
  678. * Stream context structure.
  679. *
  680. * @stream_ring: 64-bit stream ring address, cycle state, and stream type.
  681. * @reserved: offset 0x14 - 0x1f reserved for controller internal use.
  682. */
  683. struct cdnsp_stream_ctx {
  684. __le64 stream_ring;
  685. __le32 reserved[2];
  686. };
  687. /* Stream Context Types - bits 3:1 of stream ctx deq ptr. */
  688. #define SCT_FOR_CTX(p) (((p) << 1) & GENMASK(3, 1))
  689. /* Secondary stream array type, dequeue pointer is to a transfer ring. */
  690. #define SCT_SEC_TR 0
  691. /* Primary stream array type, dequeue pointer is to a transfer ring. */
  692. #define SCT_PRI_TR 1
  693. /**
  694. * struct cdnsp_stream_info: Representing everything that is needed to
  695. * supports stream capable endpoints.
  696. * @stream_rings: Array of pointers containing Transfer rings for all
  697. * supported streams.
  698. * @num_streams: Number of streams, including stream 0.
  699. * @stream_ctx_array: The stream context array may be bigger than the number
  700. * of streams the driver asked for.
  701. * @num_stream_ctxs: Number of streams.
  702. * @ctx_array_dma: Dma address of Context Stream Array.
  703. * @trb_address_map: For mapping physical TRB addresses to segments in
  704. * stream rings.
  705. * @td_count: Number of TDs associated with endpoint.
  706. * @first_prime_det: First PRIME packet detected.
  707. * @drbls_count: Number of allowed doorbells.
  708. */
  709. struct cdnsp_stream_info {
  710. struct cdnsp_ring **stream_rings;
  711. unsigned int num_streams;
  712. struct cdnsp_stream_ctx *stream_ctx_array;
  713. unsigned int num_stream_ctxs;
  714. dma_addr_t ctx_array_dma;
  715. struct radix_tree_root trb_address_map;
  716. int td_count;
  717. u8 first_prime_det;
  718. #define STREAM_DRBL_FIFO_DEPTH 2
  719. u8 drbls_count;
  720. };
  721. #define STREAM_LOG_STREAMS 4
  722. #define STREAM_NUM_STREAMS BIT(STREAM_LOG_STREAMS)
  723. #if STREAM_LOG_STREAMS > 16 && STREAM_LOG_STREAMS < 1
  724. #error "Not suupported stream value"
  725. #endif
  726. /**
  727. * struct cdnsp_ep - extended device side representation of USB endpoint.
  728. * @endpoint: usb endpoint
  729. * @pending_req_list: List of requests queuing on transfer ring.
  730. * @pdev: Device associated with this endpoint.
  731. * @number: Endpoint number (1 - 15).
  732. * idx: The device context index (DCI).
  733. * interval: Interval between packets used for ISOC endpoint.
  734. * @name: A human readable name e.g. ep1out.
  735. * @direction: Endpoint direction.
  736. * @buffering: Number of on-chip buffers related to endpoint.
  737. * @buffering_period; Number of on-chip buffers related to periodic endpoint.
  738. * @in_ctx: Pointer to input endpoint context structure.
  739. * @out_ctx: Pointer to output endpoint context structure.
  740. * @ring: Pointer to transfer ring.
  741. * @stream_info: Hold stream information.
  742. * @ep_state: Current state of endpoint.
  743. * @skip: Sometimes the controller can not process isochronous endpoint ring
  744. * quickly enough, and it will miss some isoc tds on the ring and
  745. * generate Missed Service Error Event.
  746. * Set skip flag when receive a Missed Service Error Event and
  747. * process the missed tds on the endpoint ring.
  748. * @wa1_nop_trb: hold pointer to NOP trb.
  749. */
  750. struct cdnsp_ep {
  751. struct usb_ep endpoint;
  752. struct list_head pending_list;
  753. struct cdnsp_device *pdev;
  754. u8 number;
  755. u8 idx;
  756. u32 interval;
  757. char name[20];
  758. u8 direction;
  759. u8 buffering;
  760. u8 buffering_period;
  761. struct cdnsp_ep_ctx *in_ctx;
  762. struct cdnsp_ep_ctx *out_ctx;
  763. struct cdnsp_ring *ring;
  764. struct cdnsp_stream_info stream_info;
  765. unsigned int ep_state;
  766. #define EP_ENABLED BIT(0)
  767. #define EP_DIS_IN_RROGRESS BIT(1)
  768. #define EP_HALTED BIT(2)
  769. #define EP_STOPPED BIT(3)
  770. #define EP_WEDGE BIT(4)
  771. #define EP0_HALTED_STATUS BIT(5)
  772. #define EP_HAS_STREAMS BIT(6)
  773. #define EP_UNCONFIGURED BIT(7)
  774. bool skip;
  775. union cdnsp_trb *wa1_nop_trb;
  776. };
  777. /**
  778. * struct cdnsp_device_context_array
  779. * @dev_context_ptr: Array of 64-bit DMA addresses for device contexts.
  780. * @dma: DMA address for device contexts structure.
  781. */
  782. struct cdnsp_device_context_array {
  783. __le64 dev_context_ptrs[CDNSP_DEV_MAX_SLOTS + 1];
  784. dma_addr_t dma;
  785. };
  786. /**
  787. * struct cdnsp_transfer_event.
  788. * @buffer: 64-bit buffer address, or immediate data.
  789. * @transfer_len: Data length transferred.
  790. * @flags: Field is interpreted differently based on the type of TRB.
  791. */
  792. struct cdnsp_transfer_event {
  793. __le64 buffer;
  794. __le32 transfer_len;
  795. __le32 flags;
  796. };
  797. /* Invalidate event after disabling endpoint. */
  798. #define TRB_EVENT_INVALIDATE 8
  799. /* Transfer event TRB length bit mask. */
  800. /* bits 0:23 */
  801. #define EVENT_TRB_LEN(p) ((p) & GENMASK(23, 0))
  802. /* Completion Code - only applicable for some types of TRBs */
  803. #define COMP_CODE_MASK (0xff << 24)
  804. #define GET_COMP_CODE(p) (((p) & COMP_CODE_MASK) >> 24)
  805. #define COMP_INVALID 0
  806. #define COMP_SUCCESS 1
  807. #define COMP_DATA_BUFFER_ERROR 2
  808. #define COMP_BABBLE_DETECTED_ERROR 3
  809. #define COMP_TRB_ERROR 5
  810. #define COMP_RESOURCE_ERROR 7
  811. #define COMP_NO_SLOTS_AVAILABLE_ERROR 9
  812. #define COMP_INVALID_STREAM_TYPE_ERROR 10
  813. #define COMP_SLOT_NOT_ENABLED_ERROR 11
  814. #define COMP_ENDPOINT_NOT_ENABLED_ERROR 12
  815. #define COMP_SHORT_PACKET 13
  816. #define COMP_RING_UNDERRUN 14
  817. #define COMP_RING_OVERRUN 15
  818. #define COMP_VF_EVENT_RING_FULL_ERROR 16
  819. #define COMP_PARAMETER_ERROR 17
  820. #define COMP_CONTEXT_STATE_ERROR 19
  821. #define COMP_EVENT_RING_FULL_ERROR 21
  822. #define COMP_INCOMPATIBLE_DEVICE_ERROR 22
  823. #define COMP_MISSED_SERVICE_ERROR 23
  824. #define COMP_COMMAND_RING_STOPPED 24
  825. #define COMP_COMMAND_ABORTED 25
  826. #define COMP_STOPPED 26
  827. #define COMP_STOPPED_LENGTH_INVALID 27
  828. #define COMP_STOPPED_SHORT_PACKET 28
  829. #define COMP_MAX_EXIT_LATENCY_TOO_LARGE_ERROR 29
  830. #define COMP_ISOCH_BUFFER_OVERRUN 31
  831. #define COMP_EVENT_LOST_ERROR 32
  832. #define COMP_UNDEFINED_ERROR 33
  833. #define COMP_INVALID_STREAM_ID_ERROR 34
  834. /*Transfer Event NRDY bit fields */
  835. #define TRB_TO_DEV_STREAM(p) ((p) & GENMASK(16, 0))
  836. #define TRB_TO_HOST_STREAM(p) ((p) & GENMASK(16, 0))
  837. #define STREAM_PRIME_ACK 0xFFFE
  838. #define STREAM_REJECTED 0xFFFF
  839. /** Transfer Event bit fields **/
  840. #define TRB_TO_EP_ID(p) (((p) & GENMASK(20, 16)) >> 16)
  841. /**
  842. * struct cdnsp_link_trb
  843. * @segment_ptr: 64-bit segment pointer.
  844. * @intr_target: Interrupter target.
  845. * @control: Flags.
  846. */
  847. struct cdnsp_link_trb {
  848. __le64 segment_ptr;
  849. __le32 intr_target;
  850. __le32 control;
  851. };
  852. /* control bitfields */
  853. #define LINK_TOGGLE BIT(1)
  854. /**
  855. * struct cdnsp_event_cmd - Command completion event TRB.
  856. * cmd_trb: Pointer to command TRB, or the value passed by the event data trb
  857. * status: Command completion parameters and error code.
  858. * flags: Flags.
  859. */
  860. struct cdnsp_event_cmd {
  861. __le64 cmd_trb;
  862. __le32 status;
  863. __le32 flags;
  864. };
  865. /* flags bitmasks */
  866. /* Address device - disable SetAddress. */
  867. #define TRB_BSR BIT(9)
  868. /* Configure Endpoint - Deconfigure. */
  869. #define TRB_DC BIT(9)
  870. /* Force Header */
  871. #define TRB_FH_TO_PACKET_TYPE(p) ((p) & GENMASK(4, 0))
  872. #define TRB_FH_TR_PACKET 0x4
  873. #define TRB_FH_TO_DEVICE_ADDRESS(p) (((p) << 25) & GENMASK(31, 25))
  874. #define TRB_FH_TR_PACKET_DEV_NOT 0x6
  875. #define TRB_FH_TO_NOT_TYPE(p) (((p) << 4) & GENMASK(7, 4))
  876. #define TRB_FH_TR_PACKET_FUNCTION_WAKE 0x1
  877. #define TRB_FH_TO_INTERFACE(p) (((p) << 8) & GENMASK(15, 8))
  878. enum cdnsp_setup_dev {
  879. SETUP_CONTEXT_ONLY,
  880. SETUP_CONTEXT_ADDRESS,
  881. };
  882. /* bits 24:31 are the slot ID. */
  883. #define TRB_TO_SLOT_ID(p) (((p) & GENMASK(31, 24)) >> 24)
  884. #define SLOT_ID_FOR_TRB(p) (((p) << 24) & GENMASK(31, 24))
  885. /* Stop Endpoint TRB - ep_index to endpoint ID for this TRB. */
  886. #define TRB_TO_EP_INDEX(p) (((p) >> 16) & 0x1f)
  887. #define EP_ID_FOR_TRB(p) ((((p) + 1) << 16) & GENMASK(20, 16))
  888. #define SUSPEND_PORT_FOR_TRB(p) (((p) & 1) << 23)
  889. #define TRB_TO_SUSPEND_PORT(p) (((p) >> 23) & 0x1)
  890. #define LAST_EP_INDEX 30
  891. /* Set TR Dequeue Pointer command TRB fields. */
  892. #define TRB_TO_STREAM_ID(p) ((((p) & GENMASK(31, 16)) >> 16))
  893. #define STREAM_ID_FOR_TRB(p) ((((p)) << 16) & GENMASK(31, 16))
  894. #define SCT_FOR_TRB(p) (((p) << 1) & 0x7)
  895. /*
  896. * Halt Endpoint Command TRB field.
  897. * The ESP bit only exists in the SSP2 controller.
  898. */
  899. #define TRB_ESP BIT(9)
  900. /* Link TRB specific fields. */
  901. #define TRB_TC BIT(1)
  902. /* Port Status Change Event TRB fields. */
  903. /* Port ID - bits 31:24. */
  904. #define GET_PORT_ID(p) (((p) & GENMASK(31, 24)) >> 24)
  905. #define SET_PORT_ID(p) (((p) << 24) & GENMASK(31, 24))
  906. #define EVENT_DATA BIT(2)
  907. /* Normal TRB fields. */
  908. /* transfer_len bitmasks - bits 0:16. */
  909. #define TRB_LEN(p) ((p) & GENMASK(16, 0))
  910. /* TD Size, packets remaining in this TD, bits 21:17 (5 bits, so max 31). */
  911. #define TRB_TD_SIZE(p) (min((p), (u32)31) << 17)
  912. #define GET_TD_SIZE(p) (((p) & GENMASK(21, 17)) >> 17)
  913. /*
  914. * Controller uses the TD_SIZE field for TBC if Extended TBC
  915. * is enabled (ETE).
  916. */
  917. #define TRB_TD_SIZE_TBC(p) (min((p), (u32)31) << 17)
  918. /* Interrupter Target - which MSI-X vector to target the completion event at. */
  919. #define TRB_INTR_TARGET(p) (((p) << 22) & GENMASK(31, 22))
  920. #define GET_INTR_TARGET(p) (((p) & GENMASK(31, 22)) >> 22)
  921. /*
  922. * Total burst count field, Rsvdz on controller with Extended TBC
  923. * enabled (ETE).
  924. */
  925. #define TRB_TBC(p) (((p) & 0x3) << 7)
  926. #define TRB_TLBPC(p) (((p) & 0xf) << 16)
  927. /* Cycle bit - indicates TRB ownership by driver or driver.*/
  928. #define TRB_CYCLE BIT(0)
  929. /*
  930. * Force next event data TRB to be evaluated before task switch.
  931. * Used to pass OS data back after a TD completes.
  932. */
  933. #define TRB_ENT BIT(1)
  934. /* Interrupt on short packet. */
  935. #define TRB_ISP BIT(2)
  936. /* Set PCIe no snoop attribute. */
  937. #define TRB_NO_SNOOP BIT(3)
  938. /* Chain multiple TRBs into a TD. */
  939. #define TRB_CHAIN BIT(4)
  940. /* Interrupt on completion. */
  941. #define TRB_IOC BIT(5)
  942. /* The buffer pointer contains immediate data. */
  943. #define TRB_IDT BIT(6)
  944. /* 0 - NRDY during data stage, 1 - NRDY during status stage (only control). */
  945. #define TRB_STAT BIT(7)
  946. /* Block Event Interrupt. */
  947. #define TRB_BEI BIT(9)
  948. /* Control transfer TRB specific fields. */
  949. #define TRB_DIR_IN BIT(16)
  950. /* TRB bit mask in Data Stage TRB */
  951. #define TRB_SETUPID_BITMASK GENMASK(9, 8)
  952. #define TRB_SETUPID(p) ((p) << 8)
  953. #define TRB_SETUPID_TO_TYPE(p) (((p) & TRB_SETUPID_BITMASK) >> 8)
  954. #define TRB_SETUP_SPEEDID_USB3 0x1
  955. #define TRB_SETUP_SPEEDID_USB2 0x0
  956. #define TRB_SETUP_SPEEDID(p) ((p) & (1 << 7))
  957. #define TRB_SETUPSTAT_ACK 0x1
  958. #define TRB_SETUPSTAT_STALL 0x0
  959. #define TRB_SETUPSTAT(p) ((p) << 6)
  960. /* Isochronous TRB specific fields */
  961. #define TRB_SIA BIT(31)
  962. #define TRB_FRAME_ID(p) (((p) << 20) & GENMASK(30, 20))
  963. struct cdnsp_generic_trb {
  964. __le32 field[4];
  965. };
  966. union cdnsp_trb {
  967. struct cdnsp_link_trb link;
  968. struct cdnsp_transfer_event trans_event;
  969. struct cdnsp_event_cmd event_cmd;
  970. struct cdnsp_generic_trb generic;
  971. };
  972. /* TRB bit mask. */
  973. #define TRB_TYPE_BITMASK GENMASK(15, 10)
  974. #define TRB_TYPE(p) ((p) << 10)
  975. #define TRB_FIELD_TO_TYPE(p) (((p) & TRB_TYPE_BITMASK) >> 10)
  976. /* TRB type IDs. */
  977. /* bulk, interrupt, isoc scatter/gather, and control data stage. */
  978. #define TRB_NORMAL 1
  979. /* Setup Stage for control transfers. */
  980. #define TRB_SETUP 2
  981. /* Data Stage for control transfers. */
  982. #define TRB_DATA 3
  983. /* Status Stage for control transfers. */
  984. #define TRB_STATUS 4
  985. /* ISOC transfers. */
  986. #define TRB_ISOC 5
  987. /* TRB for linking ring segments. */
  988. #define TRB_LINK 6
  989. #define TRB_EVENT_DATA 7
  990. /* Transfer Ring No-op (not for the command ring). */
  991. #define TRB_TR_NOOP 8
  992. /* Command TRBs */
  993. /* Enable Slot Command. */
  994. #define TRB_ENABLE_SLOT 9
  995. /* Disable Slot Command. */
  996. #define TRB_DISABLE_SLOT 10
  997. /* Address Device Command. */
  998. #define TRB_ADDR_DEV 11
  999. /* Configure Endpoint Command. */
  1000. #define TRB_CONFIG_EP 12
  1001. /* Evaluate Context Command. */
  1002. #define TRB_EVAL_CONTEXT 13
  1003. /* Reset Endpoint Command. */
  1004. #define TRB_RESET_EP 14
  1005. /* Stop Transfer Ring Command. */
  1006. #define TRB_STOP_RING 15
  1007. /* Set Transfer Ring Dequeue Pointer Command. */
  1008. #define TRB_SET_DEQ 16
  1009. /* Reset Device Command. */
  1010. #define TRB_RESET_DEV 17
  1011. /* Force Event Command (opt). */
  1012. #define TRB_FORCE_EVENT 18
  1013. /* Force Header Command - generate a transaction or link management packet. */
  1014. #define TRB_FORCE_HEADER 22
  1015. /* No-op Command - not for transfer rings. */
  1016. #define TRB_CMD_NOOP 23
  1017. /* TRB IDs 24-31 reserved. */
  1018. /* Event TRBS. */
  1019. /* Transfer Event. */
  1020. #define TRB_TRANSFER 32
  1021. /* Command Completion Event. */
  1022. #define TRB_COMPLETION 33
  1023. /* Port Status Change Event. */
  1024. #define TRB_PORT_STATUS 34
  1025. /* Device Controller Event. */
  1026. #define TRB_HC_EVENT 37
  1027. /* MFINDEX Wrap Event - microframe counter wrapped. */
  1028. #define TRB_MFINDEX_WRAP 39
  1029. /* TRB IDs 40-47 reserved. */
  1030. /* Endpoint Not Ready Event. */
  1031. #define TRB_ENDPOINT_NRDY 48
  1032. /* TRB IDs 49-53 reserved. */
  1033. /* Halt Endpoint Command. */
  1034. #define TRB_HALT_ENDPOINT 54
  1035. /* Doorbell Overflow Event. */
  1036. #define TRB_DRB_OVERFLOW 57
  1037. #define TRB_TYPE_LINK(x) (((x) & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK))
  1038. #define TRB_TYPE_LINK_LE32(x) (((x) & cpu_to_le32(TRB_TYPE_BITMASK)) == \
  1039. cpu_to_le32(TRB_TYPE(TRB_LINK)))
  1040. #define TRB_TYPE_NOOP_LE32(x) (((x) & cpu_to_le32(TRB_TYPE_BITMASK)) == \
  1041. cpu_to_le32(TRB_TYPE(TRB_TR_NOOP)))
  1042. /*
  1043. * TRBS_PER_SEGMENT must be a multiple of 4.
  1044. * The command ring is 64-byte aligned, so it must also be greater than 16.
  1045. */
  1046. #define TRBS_PER_SEGMENT 256
  1047. #define TRBS_PER_EVENT_SEGMENT 256
  1048. #define TRBS_PER_EV_DEQ_UPDATE 100
  1049. #define TRB_SEGMENT_SIZE (TRBS_PER_SEGMENT * 16)
  1050. #define TRB_SEGMENT_SHIFT (ilog2(TRB_SEGMENT_SIZE))
  1051. /* TRB buffer pointers can't cross 64KB boundaries. */
  1052. #define TRB_MAX_BUFF_SHIFT 16
  1053. #define TRB_MAX_BUFF_SIZE BIT(TRB_MAX_BUFF_SHIFT)
  1054. /* How much data is left before the 64KB boundary? */
  1055. #define TRB_BUFF_LEN_UP_TO_BOUNDARY(addr) (TRB_MAX_BUFF_SIZE - \
  1056. ((addr) & (TRB_MAX_BUFF_SIZE - 1)))
  1057. /**
  1058. * struct cdnsp_segment - segment related data.
  1059. * @trbs: Array of Transfer Request Blocks.
  1060. * @next: Pointer to the next segment.
  1061. * @dma: DMA address of current segment.
  1062. * @bounce_dma: Bounce buffer DMA address .
  1063. * @bounce_buf: Bounce buffer virtual address.
  1064. * bounce_offs: Bounce buffer offset.
  1065. * bounce_len: Bounce buffer length.
  1066. */
  1067. struct cdnsp_segment {
  1068. union cdnsp_trb *trbs;
  1069. struct cdnsp_segment *next;
  1070. dma_addr_t dma;
  1071. /* Max packet sized bounce buffer for td-fragmant alignment */
  1072. dma_addr_t bounce_dma;
  1073. void *bounce_buf;
  1074. unsigned int bounce_offs;
  1075. unsigned int bounce_len;
  1076. };
  1077. /**
  1078. * struct cdnsp_td - Transfer Descriptor object.
  1079. * @td_list: Used for binding TD with ep_ring->td_list.
  1080. * @preq: Request associated with this TD
  1081. * @start_seg: Segment containing the first_trb in TD.
  1082. * @first_trb: First TRB for this TD.
  1083. * @last_trb: Last TRB related with TD.
  1084. * @bounce_seg: Bounce segment for this TD.
  1085. * @request_length_set: actual_length of the request has already been set.
  1086. * @drbl - TD has been added to HW scheduler - only for stream capable
  1087. * endpoints.
  1088. */
  1089. struct cdnsp_td {
  1090. struct list_head td_list;
  1091. struct cdnsp_request *preq;
  1092. struct cdnsp_segment *start_seg;
  1093. union cdnsp_trb *first_trb;
  1094. union cdnsp_trb *last_trb;
  1095. struct cdnsp_segment *bounce_seg;
  1096. bool request_length_set;
  1097. bool drbl;
  1098. };
  1099. /**
  1100. * struct cdnsp_dequeue_state - New dequeue pointer for Transfer Ring.
  1101. * @new_deq_seg: New dequeue segment.
  1102. * @new_deq_ptr: New dequeue pointer.
  1103. * @new_cycle_state: New cycle state.
  1104. * @stream_id: stream id for which new dequeue pointer has been selected.
  1105. */
  1106. struct cdnsp_dequeue_state {
  1107. struct cdnsp_segment *new_deq_seg;
  1108. union cdnsp_trb *new_deq_ptr;
  1109. int new_cycle_state;
  1110. unsigned int stream_id;
  1111. };
  1112. enum cdnsp_ring_type {
  1113. TYPE_CTRL = 0,
  1114. TYPE_ISOC,
  1115. TYPE_BULK,
  1116. TYPE_INTR,
  1117. TYPE_STREAM,
  1118. TYPE_COMMAND,
  1119. TYPE_EVENT,
  1120. };
  1121. /**
  1122. * struct cdnsp_ring - information describing transfer, command or event ring.
  1123. * @first_seg: First segment on transfer ring.
  1124. * @last_seg: Last segment on transfer ring.
  1125. * @enqueue: SW enqueue pointer address.
  1126. * @enq_seg: SW enqueue segment address.
  1127. * @dequeue: SW dequeue pointer address.
  1128. * @deq_seg: SW dequeue segment address.
  1129. * @td_list: transfer descriptor list associated with this ring.
  1130. * @cycle_state: Current cycle bit. Write the cycle state into the TRB cycle
  1131. * field to give ownership of the TRB to the device controller
  1132. * (if we are the producer) or to check if we own the TRB
  1133. * (if we are the consumer).
  1134. * @stream_id: Stream id
  1135. * @stream_active: Stream is active - PRIME packet has been detected.
  1136. * @stream_rejected: This ring has been rejected by host.
  1137. * @num_tds: Number of TDs associated with ring.
  1138. * @num_segs: Number of segments.
  1139. * @num_trbs_free: Number of free TRBs on the ring.
  1140. * @bounce_buf_len: Length of bounce buffer.
  1141. * @type: Ring type - event, transfer, or command ring.
  1142. * @last_td_was_short - TD is short TD.
  1143. * @trb_address_map: For mapping physical TRB addresses to segments in
  1144. * stream rings.
  1145. */
  1146. struct cdnsp_ring {
  1147. struct cdnsp_segment *first_seg;
  1148. struct cdnsp_segment *last_seg;
  1149. union cdnsp_trb *enqueue;
  1150. struct cdnsp_segment *enq_seg;
  1151. union cdnsp_trb *dequeue;
  1152. struct cdnsp_segment *deq_seg;
  1153. struct list_head td_list;
  1154. u32 cycle_state;
  1155. unsigned int stream_id;
  1156. unsigned int stream_active;
  1157. unsigned int stream_rejected;
  1158. int num_tds;
  1159. unsigned int num_segs;
  1160. unsigned int num_trbs_free;
  1161. unsigned int bounce_buf_len;
  1162. enum cdnsp_ring_type type;
  1163. bool last_td_was_short;
  1164. struct radix_tree_root *trb_address_map;
  1165. };
  1166. /**
  1167. * struct cdnsp_erst_entry - even ring segment table entry object.
  1168. * @seg_addr: 64-bit event ring segment address.
  1169. * seg_size: Number of TRBs in segment.;
  1170. */
  1171. struct cdnsp_erst_entry {
  1172. __le64 seg_addr;
  1173. __le32 seg_size;
  1174. /* Set to zero */
  1175. __le32 rsvd;
  1176. };
  1177. /**
  1178. * struct cdnsp_erst - even ring segment table for event ring.
  1179. * @entries: Array of event ring segments
  1180. * @num_entries: Number of segments in entries array.
  1181. * @erst_dma_addr: DMA address for entries array.
  1182. */
  1183. struct cdnsp_erst {
  1184. struct cdnsp_erst_entry *entries;
  1185. unsigned int num_entries;
  1186. dma_addr_t erst_dma_addr;
  1187. };
  1188. /**
  1189. * struct cdnsp_request - extended device side representation of usb_request
  1190. * object .
  1191. * @td: Transfer descriptor associated with this request.
  1192. * @request: Generic usb_request object describing single I/O request.
  1193. * @list: Used to adding request to endpoint pending_list.
  1194. * @pep: Extended representation of usb_ep object
  1195. * @epnum: Endpoint number associated with usb request.
  1196. * @direction: Endpoint direction for usb request.
  1197. */
  1198. struct cdnsp_request {
  1199. struct cdnsp_td td;
  1200. struct usb_request request;
  1201. struct list_head list;
  1202. struct cdnsp_ep *pep;
  1203. u8 epnum;
  1204. unsigned direction:1;
  1205. };
  1206. #define ERST_NUM_SEGS 1
  1207. /* Stages used during enumeration process.*/
  1208. enum cdnsp_ep0_stage {
  1209. CDNSP_SETUP_STAGE,
  1210. CDNSP_DATA_STAGE,
  1211. CDNSP_STATUS_STAGE,
  1212. };
  1213. /**
  1214. * struct cdnsp_port - holds information about detected ports.
  1215. * @port_num: Port number.
  1216. * @exist: Indicate if port exist.
  1217. * maj_rev: Major revision.
  1218. * min_rev: Minor revision.
  1219. */
  1220. struct cdnsp_port {
  1221. struct cdnsp_port_regs __iomem *regs;
  1222. u8 port_num;
  1223. u8 exist;
  1224. u8 maj_rev;
  1225. u8 min_rev;
  1226. };
  1227. #define CDNSP_EXT_PORT_MAJOR(x) (((x) >> 24) & 0xff)
  1228. #define CDNSP_EXT_PORT_MINOR(x) (((x) >> 16) & 0xff)
  1229. #define CDNSP_EXT_PORT_OFF(x) ((x) & 0xff)
  1230. #define CDNSP_EXT_PORT_COUNT(x) (((x) >> 8) & 0xff)
  1231. /**
  1232. * struct cdnsp_device - represent USB device.
  1233. * @dev: Pointer to device structure associated whit this controller.
  1234. * @gadget: Device side representation of the peripheral controller.
  1235. * @gadget_driver: Pointer to the gadget driver.
  1236. * @irq: IRQ line number used by device side.
  1237. * @regs:IO device memory.
  1238. * @cap_regs: Capability registers.
  1239. * @op_regs: Operational registers.
  1240. * @run_regs: Runtime registers.
  1241. * @dba: Device base address register.
  1242. * @ir_set: Current interrupter register set.
  1243. * @port20_regs: Port 2.0 Peripheral Configuration Registers.
  1244. * @port3x_regs: USB3.x Port Peripheral Configuration Registers.
  1245. * @rev_cap: Controller Capabilities Registers.
  1246. * @hcs_params1: Cached register copies of read-only HCSPARAMS1
  1247. * @hcc_params: Cached register copies of read-only HCCPARAMS1
  1248. * @rtl_revision: Cached controller rtl revision.
  1249. * @setup: Temporary buffer for setup packet.
  1250. * @ep0_preq: Internal allocated request used during enumeration.
  1251. * @ep0_stage: ep0 stage during enumeration process.
  1252. * @three_stage_setup: Three state or two state setup.
  1253. * @ep0_expect_in: Data IN expected for control transfer.
  1254. * @setup_id: Setup identifier.
  1255. * @setup_speed - Speed detected for current SETUP packet.
  1256. * @setup_buf: Buffer for SETUP packet.
  1257. * @device_address: Current device address.
  1258. * @may_wakeup: remote wakeup enabled/disabled.
  1259. * @lock: Lock used in interrupt thread context.
  1260. * @hci_version: device controller version.
  1261. * @dcbaa: Device context base address array.
  1262. * @cmd_ring: Command ring.
  1263. * @cmd: Represent all what is needed to issue command on Command Ring.
  1264. * @event_ring: Event ring.
  1265. * @erst: Event Ring Segment table
  1266. * @slot_id: Current Slot ID. Should be 0 or 1.
  1267. * @out_ctx: Output context.
  1268. * @in_ctx: Input context.
  1269. * @eps: array of endpoints object associated with device.
  1270. * @usb2_hw_lpm_capable: hardware lpm is enabled;
  1271. * @u1_allowed: Allow device transition to U1 state.
  1272. * @u2_allowed: Allow device transition to U2 state
  1273. * @device_pool: DMA pool for allocating input and output context.
  1274. * @segment_pool: DMA pool for allocating new segments.
  1275. * @cdnsp_state: Current state of controller.
  1276. * @link_state: Current link state.
  1277. * @usb2_port - Port USB 2.0.
  1278. * @usb3_port - Port USB 3.0.
  1279. * @active_port - Current selected Port.
  1280. * @test_mode: selected Test Mode.
  1281. */
  1282. struct cdnsp_device {
  1283. struct device *dev;
  1284. struct usb_gadget gadget;
  1285. struct usb_gadget_driver *gadget_driver;
  1286. unsigned int irq;
  1287. void __iomem *regs;
  1288. /* Registers map */
  1289. struct cdnsp_cap_regs __iomem *cap_regs;
  1290. struct cdnsp_op_regs __iomem *op_regs;
  1291. struct cdnsp_run_regs __iomem *run_regs;
  1292. struct cdnsp_doorbell_array __iomem *dba;
  1293. struct cdnsp_intr_reg __iomem *ir_set;
  1294. struct cdnsp_20port_cap __iomem *port20_regs;
  1295. struct cdnsp_3xport_cap __iomem *port3x_regs;
  1296. struct cdnsp_rev_cap __iomem *rev_cap;
  1297. /* Cached register copies of read-only CDNSP data */
  1298. __u32 hcs_params1;
  1299. __u32 hcs_params3;
  1300. __u32 hcc_params;
  1301. #define RTL_REVISION_NEW_LPM 0x2700
  1302. __u32 rtl_revision;
  1303. /* Lock used in interrupt thread context. */
  1304. spinlock_t lock;
  1305. struct usb_ctrlrequest setup;
  1306. struct cdnsp_request ep0_preq;
  1307. enum cdnsp_ep0_stage ep0_stage;
  1308. u8 three_stage_setup;
  1309. u8 ep0_expect_in;
  1310. u8 setup_id;
  1311. u8 setup_speed;
  1312. void *setup_buf;
  1313. u8 device_address;
  1314. int may_wakeup;
  1315. u16 hci_version;
  1316. /* data structures */
  1317. struct cdnsp_device_context_array *dcbaa;
  1318. struct cdnsp_ring *cmd_ring;
  1319. struct cdnsp_command cmd;
  1320. struct cdnsp_ring *event_ring;
  1321. struct cdnsp_erst erst;
  1322. int slot_id;
  1323. /*
  1324. * Commands to the hardware are passed an "input context" that
  1325. * tells the hardware what to change in its data structures.
  1326. * The hardware will return changes in an "output context" that
  1327. * software must allocate for the hardware. .
  1328. */
  1329. struct cdnsp_container_ctx out_ctx;
  1330. struct cdnsp_container_ctx in_ctx;
  1331. struct cdnsp_ep eps[CDNSP_ENDPOINTS_NUM];
  1332. u8 usb2_hw_lpm_capable:1;
  1333. u8 u1_allowed:1;
  1334. u8 u2_allowed:1;
  1335. /* DMA pools */
  1336. struct dma_pool *device_pool;
  1337. struct dma_pool *segment_pool;
  1338. #define CDNSP_STATE_HALTED BIT(1)
  1339. #define CDNSP_STATE_DYING BIT(2)
  1340. #define CDNSP_STATE_DISCONNECT_PENDING BIT(3)
  1341. #define CDNSP_WAKEUP_PENDING BIT(4)
  1342. unsigned int cdnsp_state;
  1343. unsigned int link_state;
  1344. struct cdnsp_port usb2_port;
  1345. struct cdnsp_port usb3_port;
  1346. struct cdnsp_port *active_port;
  1347. u16 test_mode;
  1348. };
  1349. /*
  1350. * Registers should always be accessed with double word or quad word accesses.
  1351. *
  1352. * Registers with 64-bit address pointers should be written to with
  1353. * dword accesses by writing the low dword first (ptr[0]), then the high dword
  1354. * (ptr[1]) second. controller implementations that do not support 64-bit
  1355. * address pointers will ignore the high dword, and write order is irrelevant.
  1356. */
  1357. static inline u64 cdnsp_read_64(__le64 __iomem *regs)
  1358. {
  1359. return lo_hi_readq(regs);
  1360. }
  1361. static inline void cdnsp_write_64(const u64 val, __le64 __iomem *regs)
  1362. {
  1363. lo_hi_writeq(val, regs);
  1364. }
  1365. /* CDNSP memory management functions. */
  1366. void cdnsp_mem_cleanup(struct cdnsp_device *pdev);
  1367. int cdnsp_mem_init(struct cdnsp_device *pdev);
  1368. int cdnsp_setup_addressable_priv_dev(struct cdnsp_device *pdev);
  1369. void cdnsp_copy_ep0_dequeue_into_input_ctx(struct cdnsp_device *pdev);
  1370. void cdnsp_endpoint_zero(struct cdnsp_device *pdev, struct cdnsp_ep *ep);
  1371. int cdnsp_endpoint_init(struct cdnsp_device *pdev,
  1372. struct cdnsp_ep *pep,
  1373. gfp_t mem_flags);
  1374. int cdnsp_ring_expansion(struct cdnsp_device *pdev,
  1375. struct cdnsp_ring *ring,
  1376. unsigned int num_trbs, gfp_t flags);
  1377. struct cdnsp_ring *cdnsp_dma_to_transfer_ring(struct cdnsp_ep *ep, u64 address);
  1378. int cdnsp_alloc_stream_info(struct cdnsp_device *pdev,
  1379. struct cdnsp_ep *pep,
  1380. unsigned int num_stream_ctxs,
  1381. unsigned int num_streams);
  1382. int cdnsp_alloc_streams(struct cdnsp_device *pdev, struct cdnsp_ep *pep);
  1383. void cdnsp_free_endpoint_rings(struct cdnsp_device *pdev, struct cdnsp_ep *pep);
  1384. /* Device controller glue. */
  1385. int cdnsp_find_next_ext_cap(void __iomem *base, u32 start, int id);
  1386. int cdnsp_halt(struct cdnsp_device *pdev);
  1387. void cdnsp_died(struct cdnsp_device *pdev);
  1388. int cdnsp_reset(struct cdnsp_device *pdev);
  1389. irqreturn_t cdnsp_irq_handler(int irq, void *priv);
  1390. int cdnsp_setup_device(struct cdnsp_device *pdev, enum cdnsp_setup_dev setup);
  1391. void cdnsp_set_usb2_hardware_lpm(struct cdnsp_device *usbsssp_data,
  1392. struct usb_request *req, int enable);
  1393. irqreturn_t cdnsp_thread_irq_handler(int irq, void *data);
  1394. /* Ring, segment, TRB, and TD functions. */
  1395. dma_addr_t cdnsp_trb_virt_to_dma(struct cdnsp_segment *seg,
  1396. union cdnsp_trb *trb);
  1397. bool cdnsp_last_trb_on_seg(struct cdnsp_segment *seg, union cdnsp_trb *trb);
  1398. bool cdnsp_last_trb_on_ring(struct cdnsp_ring *ring,
  1399. struct cdnsp_segment *seg,
  1400. union cdnsp_trb *trb);
  1401. int cdnsp_wait_for_cmd_compl(struct cdnsp_device *pdev);
  1402. void cdnsp_update_erst_dequeue(struct cdnsp_device *pdev,
  1403. union cdnsp_trb *event_ring_deq,
  1404. u8 clear_ehb);
  1405. void cdnsp_initialize_ring_info(struct cdnsp_ring *ring);
  1406. void cdnsp_ring_cmd_db(struct cdnsp_device *pdev);
  1407. void cdnsp_queue_slot_control(struct cdnsp_device *pdev, u32 trb_type);
  1408. void cdnsp_queue_address_device(struct cdnsp_device *pdev,
  1409. dma_addr_t in_ctx_ptr,
  1410. enum cdnsp_setup_dev setup);
  1411. void cdnsp_queue_stop_endpoint(struct cdnsp_device *pdev,
  1412. unsigned int ep_index);
  1413. int cdnsp_queue_ctrl_tx(struct cdnsp_device *pdev, struct cdnsp_request *preq);
  1414. int cdnsp_queue_bulk_tx(struct cdnsp_device *pdev, struct cdnsp_request *preq);
  1415. int cdnsp_queue_isoc_tx(struct cdnsp_device *pdev,
  1416. struct cdnsp_request *preq);
  1417. void cdnsp_queue_configure_endpoint(struct cdnsp_device *pdev,
  1418. dma_addr_t in_ctx_ptr);
  1419. void cdnsp_queue_reset_ep(struct cdnsp_device *pdev, unsigned int ep_index);
  1420. void cdnsp_queue_halt_endpoint(struct cdnsp_device *pdev,
  1421. unsigned int ep_index);
  1422. void cdnsp_force_header_wakeup(struct cdnsp_device *pdev, int intf_num);
  1423. void cdnsp_queue_reset_device(struct cdnsp_device *pdev);
  1424. void cdnsp_queue_new_dequeue_state(struct cdnsp_device *pdev,
  1425. struct cdnsp_ep *pep,
  1426. struct cdnsp_dequeue_state *deq_state);
  1427. void cdnsp_ring_doorbell_for_active_rings(struct cdnsp_device *pdev,
  1428. struct cdnsp_ep *pep);
  1429. void cdnsp_inc_deq(struct cdnsp_device *pdev, struct cdnsp_ring *ring);
  1430. void cdnsp_set_link_state(struct cdnsp_device *pdev,
  1431. __le32 __iomem *port_regs, u32 link_state);
  1432. u32 cdnsp_port_state_to_neutral(u32 state);
  1433. /* CDNSP device controller contexts. */
  1434. int cdnsp_enable_slot(struct cdnsp_device *pdev);
  1435. int cdnsp_disable_slot(struct cdnsp_device *pdev);
  1436. struct cdnsp_input_control_ctx
  1437. *cdnsp_get_input_control_ctx(struct cdnsp_container_ctx *ctx);
  1438. struct cdnsp_slot_ctx *cdnsp_get_slot_ctx(struct cdnsp_container_ctx *ctx);
  1439. struct cdnsp_ep_ctx *cdnsp_get_ep_ctx(struct cdnsp_container_ctx *ctx,
  1440. unsigned int ep_index);
  1441. /* CDNSP gadget interface. */
  1442. void cdnsp_suspend_gadget(struct cdnsp_device *pdev);
  1443. void cdnsp_resume_gadget(struct cdnsp_device *pdev);
  1444. void cdnsp_disconnect_gadget(struct cdnsp_device *pdev);
  1445. void cdnsp_gadget_giveback(struct cdnsp_ep *pep, struct cdnsp_request *preq,
  1446. int status);
  1447. int cdnsp_ep_enqueue(struct cdnsp_ep *pep, struct cdnsp_request *preq);
  1448. int cdnsp_ep_dequeue(struct cdnsp_ep *pep, struct cdnsp_request *preq);
  1449. unsigned int cdnsp_port_speed(unsigned int port_status);
  1450. void cdnsp_irq_reset(struct cdnsp_device *pdev);
  1451. int cdnsp_halt_endpoint(struct cdnsp_device *pdev,
  1452. struct cdnsp_ep *pep, int value);
  1453. int cdnsp_cmd_stop_ep(struct cdnsp_device *pdev, struct cdnsp_ep *pep);
  1454. void cdnsp_setup_analyze(struct cdnsp_device *pdev);
  1455. int cdnsp_status_stage(struct cdnsp_device *pdev);
  1456. int cdnsp_reset_device(struct cdnsp_device *pdev);
  1457. /**
  1458. * next_request - gets the next request on the given list
  1459. * @list: the request list to operate on
  1460. *
  1461. * Caller should take care of locking. This function return NULL or the first
  1462. * request available on list.
  1463. */
  1464. static inline struct cdnsp_request *next_request(struct list_head *list)
  1465. {
  1466. return list_first_entry_or_null(list, struct cdnsp_request, list);
  1467. }
  1468. #define to_cdnsp_ep(ep) (container_of(ep, struct cdnsp_ep, endpoint))
  1469. #define gadget_to_cdnsp(g) (container_of(g, struct cdnsp_device, gadget))
  1470. #define request_to_cdnsp_request(r) (container_of(r, struct cdnsp_request, \
  1471. request))
  1472. #define to_cdnsp_request(r) (container_of(r, struct cdnsp_request, request))
  1473. int cdnsp_remove_request(struct cdnsp_device *pdev, struct cdnsp_request *preq,
  1474. struct cdnsp_ep *pep);
  1475. #endif /* __LINUX_CDNSP_GADGET_H */