cdnsp-gadget.h 53 KB

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