cdnsp-gadget.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  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. */
  10. #include <linux/moduleparam.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/module.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/delay.h>
  15. #include <linux/log2.h>
  16. #include <linux/slab.h>
  17. #include <linux/string_choices.h>
  18. #include <linux/pci.h>
  19. #include <linux/irq.h>
  20. #include <linux/dmi.h>
  21. #include "core.h"
  22. #include "gadget-export.h"
  23. #include "drd.h"
  24. #include "cdnsp-gadget.h"
  25. #include "cdnsp-trace.h"
  26. unsigned int cdnsp_port_speed(unsigned int port_status)
  27. {
  28. /*Detect gadget speed based on PORTSC register*/
  29. if (DEV_SUPERSPEEDPLUS(port_status) ||
  30. DEV_SSP_GEN1x2(port_status) || DEV_SSP_GEN2x2(port_status))
  31. return USB_SPEED_SUPER_PLUS;
  32. else if (DEV_SUPERSPEED(port_status))
  33. return USB_SPEED_SUPER;
  34. else if (DEV_HIGHSPEED(port_status))
  35. return USB_SPEED_HIGH;
  36. else if (DEV_FULLSPEED(port_status))
  37. return USB_SPEED_FULL;
  38. /* If device is detached then speed will be USB_SPEED_UNKNOWN.*/
  39. return USB_SPEED_UNKNOWN;
  40. }
  41. /*
  42. * Given a port state, this function returns a value that would result in the
  43. * port being in the same state, if the value was written to the port status
  44. * control register.
  45. * Save Read Only (RO) bits and save read/write bits where
  46. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  47. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  48. */
  49. u32 cdnsp_port_state_to_neutral(u32 state)
  50. {
  51. /* Save read-only status and port state. */
  52. return (state & CDNSP_PORT_RO) | (state & CDNSP_PORT_RWS);
  53. }
  54. /**
  55. * cdnsp_find_next_ext_cap - Find the offset of the extended capabilities
  56. * with capability ID id.
  57. * @base: PCI MMIO registers base address.
  58. * @start: Address at which to start looking, (0 or HCC_PARAMS to start at
  59. * beginning of list)
  60. * @id: Extended capability ID to search for.
  61. *
  62. * Returns the offset of the next matching extended capability structure.
  63. * Some capabilities can occur several times,
  64. * e.g., the EXT_CAPS_PROTOCOL, and this provides a way to find them all.
  65. */
  66. int cdnsp_find_next_ext_cap(void __iomem *base, u32 start, int id)
  67. {
  68. u32 offset = start;
  69. u32 next;
  70. u32 val;
  71. if (!start || start == HCC_PARAMS_OFFSET) {
  72. val = readl(base + HCC_PARAMS_OFFSET);
  73. if (val == ~0)
  74. return 0;
  75. offset = HCC_EXT_CAPS(val) << 2;
  76. if (!offset)
  77. return 0;
  78. }
  79. do {
  80. val = readl(base + offset);
  81. if (val == ~0)
  82. return 0;
  83. if (EXT_CAPS_ID(val) == id && offset != start)
  84. return offset;
  85. next = EXT_CAPS_NEXT(val);
  86. offset += next << 2;
  87. } while (next);
  88. return 0;
  89. }
  90. void cdnsp_set_link_state(struct cdnsp_device *pdev,
  91. __le32 __iomem *port_regs,
  92. u32 link_state)
  93. {
  94. int port_num = 0xFF;
  95. u32 temp;
  96. temp = readl(port_regs);
  97. temp = cdnsp_port_state_to_neutral(temp);
  98. temp |= PORT_WKCONN_E | PORT_WKDISC_E;
  99. writel(temp, port_regs);
  100. temp &= ~PORT_PLS_MASK;
  101. temp |= PORT_LINK_STROBE | link_state;
  102. if (pdev->active_port)
  103. port_num = pdev->active_port->port_num;
  104. trace_cdnsp_handle_port_status(port_num, readl(port_regs));
  105. writel(temp, port_regs);
  106. trace_cdnsp_link_state_changed(port_num, readl(port_regs));
  107. }
  108. static void cdnsp_disable_port(struct cdnsp_device *pdev,
  109. __le32 __iomem *port_regs)
  110. {
  111. u32 temp = cdnsp_port_state_to_neutral(readl(port_regs));
  112. writel(temp | PORT_PED, port_regs);
  113. }
  114. static void cdnsp_clear_port_change_bit(struct cdnsp_device *pdev,
  115. __le32 __iomem *port_regs)
  116. {
  117. u32 portsc = readl(port_regs);
  118. writel(cdnsp_port_state_to_neutral(portsc) |
  119. (portsc & PORT_CHANGE_BITS), port_regs);
  120. }
  121. static void cdnsp_set_apb_timeout_value(struct cdnsp_device *pdev)
  122. {
  123. struct cdns *cdns = dev_get_drvdata(pdev->dev);
  124. __le32 __iomem *reg;
  125. void __iomem *base;
  126. u32 offset = 0;
  127. u32 val;
  128. if (!cdns->override_apb_timeout)
  129. return;
  130. base = &pdev->cap_regs->hc_capbase;
  131. offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
  132. reg = base + offset + REG_CHICKEN_BITS_3_OFFSET;
  133. val = le32_to_cpu(readl(reg));
  134. val = CHICKEN_APB_TIMEOUT_SET(val, cdns->override_apb_timeout);
  135. writel(cpu_to_le32(val), reg);
  136. }
  137. static void cdnsp_set_chicken_bits_2(struct cdnsp_device *pdev, u32 bit)
  138. {
  139. __le32 __iomem *reg;
  140. void __iomem *base;
  141. u32 offset = 0;
  142. base = &pdev->cap_regs->hc_capbase;
  143. offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
  144. reg = base + offset + REG_CHICKEN_BITS_2_OFFSET;
  145. bit = readl(reg) | bit;
  146. writel(bit, reg);
  147. }
  148. static void cdnsp_clear_chicken_bits_2(struct cdnsp_device *pdev, u32 bit)
  149. {
  150. __le32 __iomem *reg;
  151. void __iomem *base;
  152. u32 offset = 0;
  153. base = &pdev->cap_regs->hc_capbase;
  154. offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
  155. reg = base + offset + REG_CHICKEN_BITS_2_OFFSET;
  156. bit = readl(reg) & ~bit;
  157. writel(bit, reg);
  158. }
  159. /*
  160. * Disable interrupts and begin the controller halting process.
  161. */
  162. static void cdnsp_quiesce(struct cdnsp_device *pdev)
  163. {
  164. u32 halted;
  165. u32 mask;
  166. u32 cmd;
  167. mask = ~(u32)(CDNSP_IRQS);
  168. halted = readl(&pdev->op_regs->status) & STS_HALT;
  169. if (!halted)
  170. mask &= ~(CMD_R_S | CMD_DEVEN);
  171. cmd = readl(&pdev->op_regs->command);
  172. cmd &= mask;
  173. writel(cmd, &pdev->op_regs->command);
  174. }
  175. /*
  176. * Force controller into halt state.
  177. *
  178. * Disable any IRQs and clear the run/stop bit.
  179. * Controller will complete any current and actively pipelined transactions, and
  180. * should halt within 16 ms of the run/stop bit being cleared.
  181. * Read controller Halted bit in the status register to see when the
  182. * controller is finished.
  183. */
  184. int cdnsp_halt(struct cdnsp_device *pdev)
  185. {
  186. int ret;
  187. u32 val;
  188. cdnsp_quiesce(pdev);
  189. ret = readl_poll_timeout_atomic(&pdev->op_regs->status, val,
  190. val & STS_HALT, 1,
  191. CDNSP_MAX_HALT_USEC);
  192. if (ret) {
  193. dev_err(pdev->dev, "ERROR: Device halt failed\n");
  194. return ret;
  195. }
  196. pdev->cdnsp_state |= CDNSP_STATE_HALTED;
  197. return 0;
  198. }
  199. /*
  200. * device controller died, register read returns 0xffffffff, or command never
  201. * ends.
  202. */
  203. void cdnsp_died(struct cdnsp_device *pdev)
  204. {
  205. dev_err(pdev->dev, "ERROR: CDNSP controller not responding\n");
  206. pdev->cdnsp_state |= CDNSP_STATE_DYING;
  207. cdnsp_halt(pdev);
  208. }
  209. /*
  210. * Set the run bit and wait for the device to be running.
  211. */
  212. static int cdnsp_start(struct cdnsp_device *pdev)
  213. {
  214. u32 temp;
  215. int ret;
  216. temp = readl(&pdev->op_regs->command);
  217. temp |= (CMD_R_S | CMD_DEVEN);
  218. writel(temp, &pdev->op_regs->command);
  219. pdev->cdnsp_state = 0;
  220. /*
  221. * Wait for the STS_HALT Status bit to be 0 to indicate the device is
  222. * running.
  223. */
  224. ret = readl_poll_timeout_atomic(&pdev->op_regs->status, temp,
  225. !(temp & STS_HALT), 1,
  226. CDNSP_MAX_HALT_USEC);
  227. if (ret) {
  228. pdev->cdnsp_state = CDNSP_STATE_DYING;
  229. dev_err(pdev->dev, "ERROR: Controller run failed\n");
  230. }
  231. return ret;
  232. }
  233. /*
  234. * Reset a halted controller.
  235. *
  236. * This resets pipelines, timers, counters, state machines, etc.
  237. * Transactions will be terminated immediately, and operational registers
  238. * will be set to their defaults.
  239. */
  240. int cdnsp_reset(struct cdnsp_device *pdev)
  241. {
  242. u32 command;
  243. u32 temp;
  244. int ret;
  245. temp = readl(&pdev->op_regs->status);
  246. if (temp == ~(u32)0) {
  247. dev_err(pdev->dev, "Device not accessible, reset failed.\n");
  248. return -ENODEV;
  249. }
  250. if ((temp & STS_HALT) == 0) {
  251. dev_err(pdev->dev, "Controller not halted, aborting reset.\n");
  252. return -EINVAL;
  253. }
  254. command = readl(&pdev->op_regs->command);
  255. command |= CMD_RESET;
  256. writel(command, &pdev->op_regs->command);
  257. ret = readl_poll_timeout_atomic(&pdev->op_regs->command, temp,
  258. !(temp & CMD_RESET), 1,
  259. 10 * 1000);
  260. if (ret) {
  261. dev_err(pdev->dev, "ERROR: Controller reset failed\n");
  262. return ret;
  263. }
  264. /*
  265. * CDNSP cannot write any doorbells or operational registers other
  266. * than status until the "Controller Not Ready" flag is cleared.
  267. */
  268. ret = readl_poll_timeout_atomic(&pdev->op_regs->status, temp,
  269. !(temp & STS_CNR), 1,
  270. 10 * 1000);
  271. if (ret) {
  272. dev_err(pdev->dev, "ERROR: Controller not ready to work\n");
  273. return ret;
  274. }
  275. dev_dbg(pdev->dev, "Controller ready to work");
  276. return ret;
  277. }
  278. /*
  279. * cdnsp_get_endpoint_index - Find the index for an endpoint given its
  280. * descriptor.Use the return value to right shift 1 for the bitmask.
  281. *
  282. * Index = (epnum * 2) + direction - 1,
  283. * where direction = 0 for OUT, 1 for IN.
  284. * For control endpoints, the IN index is used (OUT index is unused), so
  285. * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
  286. */
  287. static unsigned int
  288. cdnsp_get_endpoint_index(const struct usb_endpoint_descriptor *desc)
  289. {
  290. unsigned int index = (unsigned int)usb_endpoint_num(desc);
  291. if (usb_endpoint_xfer_control(desc))
  292. return index * 2;
  293. return (index * 2) + (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
  294. }
  295. /*
  296. * Find the flag for this endpoint (for use in the control context). Use the
  297. * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
  298. * bit 1, etc.
  299. */
  300. static unsigned int
  301. cdnsp_get_endpoint_flag(const struct usb_endpoint_descriptor *desc)
  302. {
  303. return 1 << (cdnsp_get_endpoint_index(desc) + 1);
  304. }
  305. int cdnsp_ep_enqueue(struct cdnsp_ep *pep, struct cdnsp_request *preq)
  306. {
  307. struct cdnsp_device *pdev = pep->pdev;
  308. struct usb_request *request;
  309. int ret;
  310. if (preq->epnum == 0 && !list_empty(&pep->pending_list)) {
  311. trace_cdnsp_request_enqueue_busy(preq);
  312. return -EBUSY;
  313. }
  314. request = &preq->request;
  315. request->actual = 0;
  316. request->status = -EINPROGRESS;
  317. preq->direction = pep->direction;
  318. preq->epnum = pep->number;
  319. preq->td.drbl = 0;
  320. ret = usb_gadget_map_request_by_dev(pdev->dev, request, pep->direction);
  321. if (ret) {
  322. trace_cdnsp_request_enqueue_error(preq);
  323. return ret;
  324. }
  325. list_add_tail(&preq->list, &pep->pending_list);
  326. trace_cdnsp_request_enqueue(preq);
  327. switch (usb_endpoint_type(pep->endpoint.desc)) {
  328. case USB_ENDPOINT_XFER_CONTROL:
  329. ret = cdnsp_queue_ctrl_tx(pdev, preq);
  330. break;
  331. case USB_ENDPOINT_XFER_BULK:
  332. case USB_ENDPOINT_XFER_INT:
  333. ret = cdnsp_queue_bulk_tx(pdev, preq);
  334. break;
  335. case USB_ENDPOINT_XFER_ISOC:
  336. ret = cdnsp_queue_isoc_tx(pdev, preq);
  337. }
  338. if (ret)
  339. goto unmap;
  340. return 0;
  341. unmap:
  342. usb_gadget_unmap_request_by_dev(pdev->dev, &preq->request,
  343. pep->direction);
  344. list_del(&preq->list);
  345. trace_cdnsp_request_enqueue_error(preq);
  346. return ret;
  347. }
  348. /*
  349. * Remove the request's TD from the endpoint ring. This may cause the
  350. * controller to stop USB transfers, potentially stopping in the middle of a
  351. * TRB buffer. The controller should pick up where it left off in the TD,
  352. * unless a Set Transfer Ring Dequeue Pointer is issued.
  353. *
  354. * The TRBs that make up the buffers for the canceled request will be "removed"
  355. * from the ring. Since the ring is a contiguous structure, they can't be
  356. * physically removed. Instead, there are two options:
  357. *
  358. * 1) If the controller is in the middle of processing the request to be
  359. * canceled, we simply move the ring's dequeue pointer past those TRBs
  360. * using the Set Transfer Ring Dequeue Pointer command. This will be
  361. * the common case, when drivers timeout on the last submitted request
  362. * and attempt to cancel.
  363. *
  364. * 2) If the controller is in the middle of a different TD, we turn the TRBs
  365. * into a series of 1-TRB transfer no-op TDs. No-ops shouldn't be chained.
  366. * The controller will need to invalidate the any TRBs it has cached after
  367. * the stop endpoint command.
  368. *
  369. * 3) The TD may have completed by the time the Stop Endpoint Command
  370. * completes, so software needs to handle that case too.
  371. *
  372. */
  373. int cdnsp_ep_dequeue(struct cdnsp_ep *pep, struct cdnsp_request *preq)
  374. {
  375. struct cdnsp_device *pdev = pep->pdev;
  376. int ret_stop = 0;
  377. int ret_rem;
  378. trace_cdnsp_request_dequeue(preq);
  379. if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_RUNNING)
  380. ret_stop = cdnsp_cmd_stop_ep(pdev, pep);
  381. ret_rem = cdnsp_remove_request(pdev, preq, pep);
  382. return ret_rem ? ret_rem : ret_stop;
  383. }
  384. static void cdnsp_zero_in_ctx(struct cdnsp_device *pdev)
  385. {
  386. struct cdnsp_input_control_ctx *ctrl_ctx;
  387. struct cdnsp_slot_ctx *slot_ctx;
  388. struct cdnsp_ep_ctx *ep_ctx;
  389. int i;
  390. ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
  391. /*
  392. * When a device's add flag and drop flag are zero, any subsequent
  393. * configure endpoint command will leave that endpoint's state
  394. * untouched. Make sure we don't leave any old state in the input
  395. * endpoint contexts.
  396. */
  397. ctrl_ctx->drop_flags = 0;
  398. ctrl_ctx->add_flags = 0;
  399. slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
  400. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  401. /* Endpoint 0 is always valid */
  402. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
  403. for (i = 1; i < CDNSP_ENDPOINTS_NUM; ++i) {
  404. ep_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, i);
  405. ep_ctx->ep_info = 0;
  406. ep_ctx->ep_info2 = 0;
  407. ep_ctx->deq = 0;
  408. ep_ctx->tx_info = 0;
  409. }
  410. }
  411. /* Issue a configure endpoint command and wait for it to finish. */
  412. static int cdnsp_configure_endpoint(struct cdnsp_device *pdev)
  413. {
  414. int ret;
  415. cdnsp_queue_configure_endpoint(pdev, pdev->cmd.in_ctx->dma);
  416. cdnsp_ring_cmd_db(pdev);
  417. ret = cdnsp_wait_for_cmd_compl(pdev);
  418. if (ret) {
  419. dev_err(pdev->dev,
  420. "ERR: unexpected command completion code 0x%x.\n", ret);
  421. return -EINVAL;
  422. }
  423. return ret;
  424. }
  425. static void cdnsp_invalidate_ep_events(struct cdnsp_device *pdev,
  426. struct cdnsp_ep *pep)
  427. {
  428. struct cdnsp_segment *segment;
  429. union cdnsp_trb *event;
  430. u32 cycle_state;
  431. u32 data;
  432. event = pdev->event_ring->dequeue;
  433. segment = pdev->event_ring->deq_seg;
  434. cycle_state = pdev->event_ring->cycle_state;
  435. while (1) {
  436. data = le32_to_cpu(event->trans_event.flags);
  437. /* Check the owner of the TRB. */
  438. if ((data & TRB_CYCLE) != cycle_state)
  439. break;
  440. if (TRB_FIELD_TO_TYPE(data) == TRB_TRANSFER &&
  441. TRB_TO_EP_ID(data) == (pep->idx + 1)) {
  442. data |= TRB_EVENT_INVALIDATE;
  443. event->trans_event.flags = cpu_to_le32(data);
  444. }
  445. if (cdnsp_last_trb_on_seg(segment, event)) {
  446. cycle_state ^= 1;
  447. segment = pdev->event_ring->deq_seg->next;
  448. event = segment->trbs;
  449. } else {
  450. event++;
  451. }
  452. }
  453. }
  454. int cdnsp_wait_for_cmd_compl(struct cdnsp_device *pdev)
  455. {
  456. struct cdnsp_segment *event_deq_seg;
  457. union cdnsp_trb *cmd_trb;
  458. dma_addr_t cmd_deq_dma;
  459. union cdnsp_trb *event;
  460. u32 cycle_state;
  461. u32 retry = 10;
  462. int ret, val;
  463. u64 cmd_dma;
  464. u32 flags;
  465. cmd_trb = pdev->cmd.command_trb;
  466. pdev->cmd.status = 0;
  467. trace_cdnsp_cmd_wait_for_compl(pdev->cmd_ring, &cmd_trb->generic);
  468. ret = readl_poll_timeout_atomic(&pdev->op_regs->cmd_ring, val,
  469. !CMD_RING_BUSY(val), 1,
  470. CDNSP_CMD_TIMEOUT);
  471. if (ret) {
  472. dev_err(pdev->dev, "ERR: Timeout while waiting for command\n");
  473. trace_cdnsp_cmd_timeout(pdev->cmd_ring, &cmd_trb->generic);
  474. pdev->cdnsp_state = CDNSP_STATE_DYING;
  475. return -ETIMEDOUT;
  476. }
  477. event = pdev->event_ring->dequeue;
  478. event_deq_seg = pdev->event_ring->deq_seg;
  479. cycle_state = pdev->event_ring->cycle_state;
  480. cmd_deq_dma = cdnsp_trb_virt_to_dma(pdev->cmd_ring->deq_seg, cmd_trb);
  481. if (!cmd_deq_dma)
  482. return -EINVAL;
  483. while (1) {
  484. flags = le32_to_cpu(event->event_cmd.flags);
  485. /* Check the owner of the TRB. */
  486. if ((flags & TRB_CYCLE) != cycle_state) {
  487. /*
  488. * Give some extra time to get chance controller
  489. * to finish command before returning error code.
  490. * Checking CMD_RING_BUSY is not sufficient because
  491. * this bit is cleared to '0' when the Command
  492. * Descriptor has been executed by controller
  493. * and not when command completion event has
  494. * be added to event ring.
  495. */
  496. if (retry--) {
  497. udelay(20);
  498. continue;
  499. }
  500. return -EINVAL;
  501. }
  502. cmd_dma = le64_to_cpu(event->event_cmd.cmd_trb);
  503. /*
  504. * Check whether the completion event is for last queued
  505. * command.
  506. */
  507. if (TRB_FIELD_TO_TYPE(flags) != TRB_COMPLETION ||
  508. cmd_dma != (u64)cmd_deq_dma) {
  509. if (!cdnsp_last_trb_on_seg(event_deq_seg, event)) {
  510. event++;
  511. continue;
  512. }
  513. if (cdnsp_last_trb_on_ring(pdev->event_ring,
  514. event_deq_seg, event))
  515. cycle_state ^= 1;
  516. event_deq_seg = event_deq_seg->next;
  517. event = event_deq_seg->trbs;
  518. continue;
  519. }
  520. trace_cdnsp_handle_command(pdev->cmd_ring, &cmd_trb->generic);
  521. pdev->cmd.status = GET_COMP_CODE(le32_to_cpu(event->event_cmd.status));
  522. if (pdev->cmd.status == COMP_SUCCESS)
  523. return 0;
  524. return -pdev->cmd.status;
  525. }
  526. }
  527. int cdnsp_halt_endpoint(struct cdnsp_device *pdev,
  528. struct cdnsp_ep *pep,
  529. int value)
  530. {
  531. int ret;
  532. trace_cdnsp_ep_halt(value ? "Set" : "Clear");
  533. ret = cdnsp_cmd_stop_ep(pdev, pep);
  534. if (ret)
  535. return ret;
  536. if (value) {
  537. if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_STOPPED) {
  538. cdnsp_queue_halt_endpoint(pdev, pep->idx);
  539. cdnsp_ring_cmd_db(pdev);
  540. ret = cdnsp_wait_for_cmd_compl(pdev);
  541. }
  542. pep->ep_state |= EP_HALTED;
  543. } else {
  544. cdnsp_queue_reset_ep(pdev, pep->idx);
  545. cdnsp_ring_cmd_db(pdev);
  546. ret = cdnsp_wait_for_cmd_compl(pdev);
  547. trace_cdnsp_handle_cmd_reset_ep(pep->out_ctx);
  548. if (ret)
  549. return ret;
  550. pep->ep_state &= ~EP_HALTED;
  551. if (pep->idx != 0 && !(pep->ep_state & EP_WEDGE))
  552. cdnsp_ring_doorbell_for_active_rings(pdev, pep);
  553. pep->ep_state &= ~EP_WEDGE;
  554. }
  555. return 0;
  556. }
  557. static int cdnsp_update_eps_configuration(struct cdnsp_device *pdev,
  558. struct cdnsp_ep *pep)
  559. {
  560. struct cdnsp_input_control_ctx *ctrl_ctx;
  561. struct cdnsp_slot_ctx *slot_ctx;
  562. int ret = 0;
  563. u32 ep_sts;
  564. int i;
  565. ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
  566. /* Don't issue the command if there's no endpoints to update. */
  567. if (ctrl_ctx->add_flags == 0 && ctrl_ctx->drop_flags == 0)
  568. return 0;
  569. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  570. ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
  571. ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
  572. /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
  573. slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
  574. for (i = CDNSP_ENDPOINTS_NUM; i >= 1; i--) {
  575. __le32 le32 = cpu_to_le32(BIT(i));
  576. if ((pdev->eps[i - 1].ring && !(ctrl_ctx->drop_flags & le32)) ||
  577. (ctrl_ctx->add_flags & le32) || i == 1) {
  578. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  579. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i));
  580. break;
  581. }
  582. }
  583. ep_sts = GET_EP_CTX_STATE(pep->out_ctx);
  584. if ((ctrl_ctx->add_flags != cpu_to_le32(SLOT_FLAG) &&
  585. ep_sts == EP_STATE_DISABLED) ||
  586. (ep_sts != EP_STATE_DISABLED && ctrl_ctx->drop_flags))
  587. ret = cdnsp_configure_endpoint(pdev);
  588. trace_cdnsp_configure_endpoint(cdnsp_get_slot_ctx(&pdev->out_ctx));
  589. trace_cdnsp_handle_cmd_config_ep(pep->out_ctx);
  590. cdnsp_zero_in_ctx(pdev);
  591. return ret;
  592. }
  593. /*
  594. * This submits a Reset Device Command, which will set the device state to 0,
  595. * set the device address to 0, and disable all the endpoints except the default
  596. * control endpoint. The USB core should come back and call
  597. * cdnsp_setup_device(), and then re-set up the configuration.
  598. */
  599. int cdnsp_reset_device(struct cdnsp_device *pdev)
  600. {
  601. struct cdnsp_slot_ctx *slot_ctx;
  602. int slot_state;
  603. int ret, i;
  604. slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
  605. slot_ctx->dev_info = 0;
  606. pdev->device_address = 0;
  607. /* If device is not setup, there is no point in resetting it. */
  608. slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
  609. slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
  610. trace_cdnsp_reset_device(slot_ctx);
  611. if (slot_state <= SLOT_STATE_DEFAULT &&
  612. pdev->eps[0].ep_state & EP_HALTED) {
  613. cdnsp_halt_endpoint(pdev, &pdev->eps[0], 0);
  614. }
  615. /*
  616. * During Reset Device command controller shall transition the
  617. * endpoint ep0 to the Running State.
  618. */
  619. pdev->eps[0].ep_state &= ~(EP_STOPPED | EP_HALTED);
  620. pdev->eps[0].ep_state |= EP_ENABLED;
  621. if (slot_state <= SLOT_STATE_DEFAULT)
  622. return 0;
  623. cdnsp_queue_reset_device(pdev);
  624. cdnsp_ring_cmd_db(pdev);
  625. ret = cdnsp_wait_for_cmd_compl(pdev);
  626. /*
  627. * After Reset Device command all not default endpoints
  628. * are in Disabled state.
  629. */
  630. for (i = 1; i < CDNSP_ENDPOINTS_NUM; ++i)
  631. pdev->eps[i].ep_state |= EP_STOPPED | EP_UNCONFIGURED;
  632. trace_cdnsp_handle_cmd_reset_dev(slot_ctx);
  633. if (ret)
  634. dev_err(pdev->dev, "Reset device failed with error code %d",
  635. ret);
  636. return ret;
  637. }
  638. /*
  639. * Sets the MaxPStreams field and the Linear Stream Array field.
  640. * Sets the dequeue pointer to the stream context array.
  641. */
  642. static void cdnsp_setup_streams_ep_input_ctx(struct cdnsp_device *pdev,
  643. struct cdnsp_ep_ctx *ep_ctx,
  644. struct cdnsp_stream_info *stream_info)
  645. {
  646. u32 max_primary_streams;
  647. /* MaxPStreams is the number of stream context array entries, not the
  648. * number we're actually using. Must be in 2^(MaxPstreams + 1) format.
  649. * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc.
  650. */
  651. max_primary_streams = fls(stream_info->num_stream_ctxs) - 2;
  652. ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK);
  653. ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams)
  654. | EP_HAS_LSA);
  655. ep_ctx->deq = cpu_to_le64(stream_info->ctx_array_dma);
  656. }
  657. /*
  658. * The drivers use this function to prepare a bulk endpoints to use streams.
  659. *
  660. * Don't allow the call to succeed if endpoint only supports one stream
  661. * (which means it doesn't support streams at all).
  662. */
  663. int cdnsp_alloc_streams(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
  664. {
  665. unsigned int num_streams = usb_ss_max_streams(pep->endpoint.comp_desc);
  666. unsigned int num_stream_ctxs;
  667. int ret;
  668. if (num_streams == 0)
  669. return 0;
  670. if (num_streams > STREAM_NUM_STREAMS)
  671. return -EINVAL;
  672. /*
  673. * Add two to the number of streams requested to account for
  674. * stream 0 that is reserved for controller usage and one additional
  675. * for TASK SET FULL response.
  676. */
  677. num_streams += 2;
  678. /* The stream context array size must be a power of two */
  679. num_stream_ctxs = roundup_pow_of_two(num_streams);
  680. trace_cdnsp_stream_number(pep, num_stream_ctxs, num_streams);
  681. ret = cdnsp_alloc_stream_info(pdev, pep, num_stream_ctxs, num_streams);
  682. if (ret)
  683. return ret;
  684. cdnsp_setup_streams_ep_input_ctx(pdev, pep->in_ctx, &pep->stream_info);
  685. pep->ep_state |= EP_HAS_STREAMS;
  686. pep->stream_info.td_count = 0;
  687. pep->stream_info.first_prime_det = 0;
  688. /* Subtract 1 for stream 0, which drivers can't use. */
  689. return num_streams - 1;
  690. }
  691. int cdnsp_disable_slot(struct cdnsp_device *pdev)
  692. {
  693. int ret;
  694. cdnsp_queue_slot_control(pdev, TRB_DISABLE_SLOT);
  695. cdnsp_ring_cmd_db(pdev);
  696. ret = cdnsp_wait_for_cmd_compl(pdev);
  697. pdev->slot_id = 0;
  698. pdev->active_port = NULL;
  699. trace_cdnsp_handle_cmd_disable_slot(cdnsp_get_slot_ctx(&pdev->out_ctx));
  700. memset(pdev->in_ctx.bytes, 0, CDNSP_CTX_SIZE);
  701. memset(pdev->out_ctx.bytes, 0, CDNSP_CTX_SIZE);
  702. return ret;
  703. }
  704. int cdnsp_enable_slot(struct cdnsp_device *pdev)
  705. {
  706. struct cdnsp_slot_ctx *slot_ctx;
  707. int slot_state;
  708. int ret;
  709. /* If device is not setup, there is no point in resetting it */
  710. slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
  711. slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
  712. if (slot_state != SLOT_STATE_DISABLED)
  713. return 0;
  714. cdnsp_queue_slot_control(pdev, TRB_ENABLE_SLOT);
  715. cdnsp_ring_cmd_db(pdev);
  716. ret = cdnsp_wait_for_cmd_compl(pdev);
  717. if (ret)
  718. goto show_trace;
  719. pdev->slot_id = 1;
  720. show_trace:
  721. trace_cdnsp_handle_cmd_enable_slot(cdnsp_get_slot_ctx(&pdev->out_ctx));
  722. return ret;
  723. }
  724. /*
  725. * Issue an Address Device command with BSR=0 if setup is SETUP_CONTEXT_ONLY
  726. * or with BSR = 1 if set_address is SETUP_CONTEXT_ADDRESS.
  727. */
  728. int cdnsp_setup_device(struct cdnsp_device *pdev, enum cdnsp_setup_dev setup)
  729. {
  730. struct cdnsp_input_control_ctx *ctrl_ctx;
  731. struct cdnsp_slot_ctx *slot_ctx;
  732. int dev_state = 0;
  733. int ret;
  734. if (!pdev->slot_id) {
  735. trace_cdnsp_slot_id("incorrect");
  736. return -EINVAL;
  737. }
  738. if (!pdev->active_port->port_num)
  739. return -EINVAL;
  740. slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
  741. dev_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
  742. if (setup == SETUP_CONTEXT_ONLY && dev_state == SLOT_STATE_DEFAULT) {
  743. trace_cdnsp_slot_already_in_default(slot_ctx);
  744. return 0;
  745. }
  746. slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
  747. ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
  748. if (!slot_ctx->dev_info || dev_state == SLOT_STATE_DEFAULT) {
  749. ret = cdnsp_setup_addressable_priv_dev(pdev);
  750. if (ret)
  751. return ret;
  752. }
  753. cdnsp_copy_ep0_dequeue_into_input_ctx(pdev);
  754. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
  755. ctrl_ctx->drop_flags = 0;
  756. trace_cdnsp_setup_device_slot(slot_ctx);
  757. cdnsp_queue_address_device(pdev, pdev->in_ctx.dma, setup);
  758. cdnsp_ring_cmd_db(pdev);
  759. ret = cdnsp_wait_for_cmd_compl(pdev);
  760. trace_cdnsp_handle_cmd_addr_dev(cdnsp_get_slot_ctx(&pdev->out_ctx));
  761. /* Zero the input context control for later use. */
  762. ctrl_ctx->add_flags = 0;
  763. ctrl_ctx->drop_flags = 0;
  764. return ret;
  765. }
  766. void cdnsp_set_usb2_hardware_lpm(struct cdnsp_device *pdev,
  767. struct usb_request *req,
  768. int enable)
  769. {
  770. if (pdev->active_port != &pdev->usb2_port || !pdev->gadget.lpm_capable)
  771. return;
  772. trace_cdnsp_lpm(enable);
  773. if (enable)
  774. writel(PORT_BESL(CDNSP_DEFAULT_BESL) | PORT_L1S_NYET | PORT_HLE,
  775. &pdev->active_port->regs->portpmsc);
  776. else
  777. writel(PORT_L1S_NYET, &pdev->active_port->regs->portpmsc);
  778. }
  779. static int cdnsp_get_frame(struct cdnsp_device *pdev)
  780. {
  781. return readl(&pdev->run_regs->microframe_index) >> 3;
  782. }
  783. static int cdnsp_gadget_ep_enable(struct usb_ep *ep,
  784. const struct usb_endpoint_descriptor *desc)
  785. {
  786. struct cdnsp_input_control_ctx *ctrl_ctx;
  787. struct cdnsp_device *pdev;
  788. struct cdnsp_ep *pep;
  789. unsigned long flags;
  790. u32 added_ctxs;
  791. int ret;
  792. if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT ||
  793. !desc->wMaxPacketSize)
  794. return -EINVAL;
  795. pep = to_cdnsp_ep(ep);
  796. pdev = pep->pdev;
  797. pep->ep_state &= ~EP_UNCONFIGURED;
  798. if (dev_WARN_ONCE(pdev->dev, pep->ep_state & EP_ENABLED,
  799. "%s is already enabled\n", pep->name))
  800. return 0;
  801. spin_lock_irqsave(&pdev->lock, flags);
  802. added_ctxs = cdnsp_get_endpoint_flag(desc);
  803. if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
  804. dev_err(pdev->dev, "ERROR: Bad endpoint number\n");
  805. ret = -EINVAL;
  806. goto unlock;
  807. }
  808. pep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
  809. if (pdev->gadget.speed == USB_SPEED_FULL) {
  810. if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT)
  811. pep->interval = desc->bInterval << 3;
  812. if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC)
  813. pep->interval = BIT(desc->bInterval - 1) << 3;
  814. }
  815. if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC) {
  816. if (pep->interval > BIT(12)) {
  817. dev_err(pdev->dev, "bInterval %d not supported\n",
  818. desc->bInterval);
  819. ret = -EINVAL;
  820. goto unlock;
  821. }
  822. cdnsp_set_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS);
  823. }
  824. ret = cdnsp_endpoint_init(pdev, pep, GFP_ATOMIC);
  825. if (ret)
  826. goto unlock;
  827. ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
  828. ctrl_ctx->add_flags = cpu_to_le32(added_ctxs);
  829. ctrl_ctx->drop_flags = 0;
  830. ret = cdnsp_update_eps_configuration(pdev, pep);
  831. if (ret) {
  832. cdnsp_free_endpoint_rings(pdev, pep);
  833. goto unlock;
  834. }
  835. pep->ep_state |= EP_ENABLED;
  836. pep->ep_state &= ~EP_STOPPED;
  837. unlock:
  838. trace_cdnsp_ep_enable_end(pep, 0);
  839. spin_unlock_irqrestore(&pdev->lock, flags);
  840. return ret;
  841. }
  842. static int cdnsp_gadget_ep_disable(struct usb_ep *ep)
  843. {
  844. struct cdnsp_input_control_ctx *ctrl_ctx;
  845. struct cdnsp_request *preq;
  846. struct cdnsp_device *pdev;
  847. struct cdnsp_ep *pep;
  848. unsigned long flags;
  849. u32 drop_flag;
  850. int ret = 0;
  851. if (!ep)
  852. return -EINVAL;
  853. pep = to_cdnsp_ep(ep);
  854. pdev = pep->pdev;
  855. spin_lock_irqsave(&pdev->lock, flags);
  856. if (!(pep->ep_state & EP_ENABLED)) {
  857. dev_err(pdev->dev, "%s is already disabled\n", pep->name);
  858. ret = -EINVAL;
  859. goto finish;
  860. }
  861. pep->ep_state |= EP_DIS_IN_RROGRESS;
  862. /* Endpoint was unconfigured by Reset Device command. */
  863. if (!(pep->ep_state & EP_UNCONFIGURED))
  864. cdnsp_cmd_stop_ep(pdev, pep);
  865. /* Remove all queued USB requests. */
  866. while (!list_empty(&pep->pending_list)) {
  867. preq = next_request(&pep->pending_list);
  868. cdnsp_ep_dequeue(pep, preq);
  869. }
  870. cdnsp_invalidate_ep_events(pdev, pep);
  871. pep->ep_state &= ~EP_DIS_IN_RROGRESS;
  872. drop_flag = cdnsp_get_endpoint_flag(pep->endpoint.desc);
  873. ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
  874. ctrl_ctx->drop_flags = cpu_to_le32(drop_flag);
  875. ctrl_ctx->add_flags = 0;
  876. cdnsp_endpoint_zero(pdev, pep);
  877. if (!(pep->ep_state & EP_UNCONFIGURED))
  878. ret = cdnsp_update_eps_configuration(pdev, pep);
  879. cdnsp_free_endpoint_rings(pdev, pep);
  880. pep->ep_state &= ~(EP_ENABLED | EP_UNCONFIGURED);
  881. pep->ep_state |= EP_STOPPED;
  882. finish:
  883. trace_cdnsp_ep_disable_end(pep, 0);
  884. spin_unlock_irqrestore(&pdev->lock, flags);
  885. return ret;
  886. }
  887. static struct usb_request *cdnsp_gadget_ep_alloc_request(struct usb_ep *ep,
  888. gfp_t gfp_flags)
  889. {
  890. struct cdnsp_ep *pep = to_cdnsp_ep(ep);
  891. struct cdnsp_request *preq;
  892. preq = kzalloc_obj(*preq, gfp_flags);
  893. if (!preq)
  894. return NULL;
  895. preq->epnum = pep->number;
  896. preq->pep = pep;
  897. trace_cdnsp_alloc_request(preq);
  898. return &preq->request;
  899. }
  900. static void cdnsp_gadget_ep_free_request(struct usb_ep *ep,
  901. struct usb_request *request)
  902. {
  903. struct cdnsp_request *preq = to_cdnsp_request(request);
  904. trace_cdnsp_free_request(preq);
  905. kfree(preq);
  906. }
  907. static int cdnsp_gadget_ep_queue(struct usb_ep *ep,
  908. struct usb_request *request,
  909. gfp_t gfp_flags)
  910. {
  911. struct cdnsp_request *preq;
  912. struct cdnsp_device *pdev;
  913. struct cdnsp_ep *pep;
  914. unsigned long flags;
  915. int ret;
  916. if (!request || !ep)
  917. return -EINVAL;
  918. pep = to_cdnsp_ep(ep);
  919. pdev = pep->pdev;
  920. if (!(pep->ep_state & EP_ENABLED)) {
  921. dev_err(pdev->dev, "%s: can't queue to disabled endpoint\n",
  922. pep->name);
  923. return -EINVAL;
  924. }
  925. preq = to_cdnsp_request(request);
  926. spin_lock_irqsave(&pdev->lock, flags);
  927. ret = cdnsp_ep_enqueue(pep, preq);
  928. spin_unlock_irqrestore(&pdev->lock, flags);
  929. return ret;
  930. }
  931. static int cdnsp_gadget_ep_dequeue(struct usb_ep *ep,
  932. struct usb_request *request)
  933. {
  934. struct cdnsp_ep *pep = to_cdnsp_ep(ep);
  935. struct cdnsp_device *pdev = pep->pdev;
  936. unsigned long flags;
  937. int ret;
  938. if (request->status != -EINPROGRESS)
  939. return 0;
  940. if (!pep->endpoint.desc) {
  941. dev_err(pdev->dev,
  942. "%s: can't dequeue to disabled endpoint\n",
  943. pep->name);
  944. return -ESHUTDOWN;
  945. }
  946. /* Requests has been dequeued during disabling endpoint. */
  947. if (!(pep->ep_state & EP_ENABLED))
  948. return 0;
  949. spin_lock_irqsave(&pdev->lock, flags);
  950. ret = cdnsp_ep_dequeue(pep, to_cdnsp_request(request));
  951. spin_unlock_irqrestore(&pdev->lock, flags);
  952. return ret;
  953. }
  954. static int cdnsp_gadget_ep_set_halt(struct usb_ep *ep, int value)
  955. {
  956. struct cdnsp_ep *pep = to_cdnsp_ep(ep);
  957. struct cdnsp_device *pdev = pep->pdev;
  958. struct cdnsp_request *preq;
  959. unsigned long flags;
  960. int ret;
  961. spin_lock_irqsave(&pdev->lock, flags);
  962. preq = next_request(&pep->pending_list);
  963. if (value) {
  964. if (preq) {
  965. trace_cdnsp_ep_busy_try_halt_again(pep, 0);
  966. ret = -EAGAIN;
  967. goto done;
  968. }
  969. }
  970. ret = cdnsp_halt_endpoint(pdev, pep, value);
  971. done:
  972. spin_unlock_irqrestore(&pdev->lock, flags);
  973. return ret;
  974. }
  975. static int cdnsp_gadget_ep_set_wedge(struct usb_ep *ep)
  976. {
  977. struct cdnsp_ep *pep = to_cdnsp_ep(ep);
  978. struct cdnsp_device *pdev = pep->pdev;
  979. unsigned long flags;
  980. int ret;
  981. spin_lock_irqsave(&pdev->lock, flags);
  982. pep->ep_state |= EP_WEDGE;
  983. ret = cdnsp_halt_endpoint(pdev, pep, 1);
  984. spin_unlock_irqrestore(&pdev->lock, flags);
  985. return ret;
  986. }
  987. static const struct usb_ep_ops cdnsp_gadget_ep0_ops = {
  988. .enable = cdnsp_gadget_ep_enable,
  989. .disable = cdnsp_gadget_ep_disable,
  990. .alloc_request = cdnsp_gadget_ep_alloc_request,
  991. .free_request = cdnsp_gadget_ep_free_request,
  992. .queue = cdnsp_gadget_ep_queue,
  993. .dequeue = cdnsp_gadget_ep_dequeue,
  994. .set_halt = cdnsp_gadget_ep_set_halt,
  995. .set_wedge = cdnsp_gadget_ep_set_wedge,
  996. };
  997. static const struct usb_ep_ops cdnsp_gadget_ep_ops = {
  998. .enable = cdnsp_gadget_ep_enable,
  999. .disable = cdnsp_gadget_ep_disable,
  1000. .alloc_request = cdnsp_gadget_ep_alloc_request,
  1001. .free_request = cdnsp_gadget_ep_free_request,
  1002. .queue = cdnsp_gadget_ep_queue,
  1003. .dequeue = cdnsp_gadget_ep_dequeue,
  1004. .set_halt = cdnsp_gadget_ep_set_halt,
  1005. .set_wedge = cdnsp_gadget_ep_set_wedge,
  1006. };
  1007. void cdnsp_gadget_giveback(struct cdnsp_ep *pep,
  1008. struct cdnsp_request *preq,
  1009. int status)
  1010. {
  1011. struct cdnsp_device *pdev = pep->pdev;
  1012. list_del(&preq->list);
  1013. if (preq->request.status == -EINPROGRESS)
  1014. preq->request.status = status;
  1015. usb_gadget_unmap_request_by_dev(pdev->dev, &preq->request,
  1016. preq->direction);
  1017. trace_cdnsp_request_giveback(preq);
  1018. if (preq != &pdev->ep0_preq) {
  1019. spin_unlock(&pdev->lock);
  1020. usb_gadget_giveback_request(&pep->endpoint, &preq->request);
  1021. spin_lock(&pdev->lock);
  1022. }
  1023. }
  1024. static struct usb_endpoint_descriptor cdnsp_gadget_ep0_desc = {
  1025. .bLength = USB_DT_ENDPOINT_SIZE,
  1026. .bDescriptorType = USB_DT_ENDPOINT,
  1027. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  1028. };
  1029. static int cdnsp_run(struct cdnsp_device *pdev,
  1030. enum usb_device_speed speed)
  1031. {
  1032. u32 fs_speed = 0;
  1033. u32 temp;
  1034. int ret;
  1035. temp = readl(&pdev->ir_set->irq_control);
  1036. temp &= ~IMOD_INTERVAL_MASK;
  1037. temp |= ((IMOD_DEFAULT_INTERVAL / 250) & IMOD_INTERVAL_MASK);
  1038. writel(temp, &pdev->ir_set->irq_control);
  1039. temp = readl(&pdev->port3x_regs->mode_addr);
  1040. switch (speed) {
  1041. case USB_SPEED_SUPER_PLUS:
  1042. temp |= CFG_3XPORT_SSP_SUPPORT;
  1043. break;
  1044. case USB_SPEED_SUPER:
  1045. temp &= ~CFG_3XPORT_SSP_SUPPORT;
  1046. break;
  1047. case USB_SPEED_HIGH:
  1048. break;
  1049. case USB_SPEED_FULL:
  1050. fs_speed = PORT_REG6_FORCE_FS;
  1051. break;
  1052. default:
  1053. dev_err(pdev->dev, "invalid maximum_speed parameter %d\n",
  1054. speed);
  1055. fallthrough;
  1056. case USB_SPEED_UNKNOWN:
  1057. /* Default to superspeed. */
  1058. speed = USB_SPEED_SUPER;
  1059. break;
  1060. }
  1061. if (speed >= USB_SPEED_SUPER) {
  1062. writel(temp, &pdev->port3x_regs->mode_addr);
  1063. cdnsp_set_link_state(pdev, &pdev->usb3_port.regs->portsc,
  1064. XDEV_RXDETECT);
  1065. } else {
  1066. cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc);
  1067. }
  1068. cdnsp_set_link_state(pdev, &pdev->usb2_port.regs->portsc,
  1069. XDEV_RXDETECT);
  1070. cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1071. writel(PORT_REG6_L1_L0_HW_EN | fs_speed, &pdev->port20_regs->port_reg6);
  1072. ret = cdnsp_start(pdev);
  1073. if (ret) {
  1074. ret = -ENODEV;
  1075. goto err;
  1076. }
  1077. temp = readl(&pdev->op_regs->command);
  1078. temp |= (CMD_INTE);
  1079. writel(temp, &pdev->op_regs->command);
  1080. temp = readl(&pdev->ir_set->irq_pending);
  1081. writel(IMAN_IE_SET(temp), &pdev->ir_set->irq_pending);
  1082. trace_cdnsp_init("Controller ready to work");
  1083. return 0;
  1084. err:
  1085. cdnsp_halt(pdev);
  1086. return ret;
  1087. }
  1088. static int cdnsp_gadget_udc_start(struct usb_gadget *g,
  1089. struct usb_gadget_driver *driver)
  1090. {
  1091. enum usb_device_speed max_speed = driver->max_speed;
  1092. struct cdnsp_device *pdev = gadget_to_cdnsp(g);
  1093. unsigned long flags;
  1094. int ret;
  1095. spin_lock_irqsave(&pdev->lock, flags);
  1096. pdev->gadget_driver = driver;
  1097. /* limit speed if necessary */
  1098. max_speed = min(driver->max_speed, g->max_speed);
  1099. ret = cdnsp_run(pdev, max_speed);
  1100. spin_unlock_irqrestore(&pdev->lock, flags);
  1101. return ret;
  1102. }
  1103. /*
  1104. * Update Event Ring Dequeue Pointer:
  1105. * - When all events have finished
  1106. * - To avoid "Event Ring Full Error" condition
  1107. */
  1108. void cdnsp_update_erst_dequeue(struct cdnsp_device *pdev,
  1109. union cdnsp_trb *event_ring_deq,
  1110. u8 clear_ehb)
  1111. {
  1112. u64 temp_64;
  1113. dma_addr_t deq;
  1114. temp_64 = cdnsp_read_64(&pdev->ir_set->erst_dequeue);
  1115. /* If necessary, update the HW's version of the event ring deq ptr. */
  1116. if (event_ring_deq != pdev->event_ring->dequeue) {
  1117. deq = cdnsp_trb_virt_to_dma(pdev->event_ring->deq_seg,
  1118. pdev->event_ring->dequeue);
  1119. temp_64 &= ERST_PTR_MASK;
  1120. temp_64 |= ((u64)deq & (u64)~ERST_PTR_MASK);
  1121. }
  1122. /* Clear the event handler busy flag (RW1C). */
  1123. if (clear_ehb)
  1124. temp_64 |= ERST_EHB;
  1125. else
  1126. temp_64 &= ~ERST_EHB;
  1127. cdnsp_write_64(temp_64, &pdev->ir_set->erst_dequeue);
  1128. }
  1129. static void cdnsp_clear_cmd_ring(struct cdnsp_device *pdev)
  1130. {
  1131. struct cdnsp_segment *seg;
  1132. u64 val_64;
  1133. int i;
  1134. cdnsp_initialize_ring_info(pdev->cmd_ring);
  1135. seg = pdev->cmd_ring->first_seg;
  1136. for (i = 0; i < pdev->cmd_ring->num_segs; i++) {
  1137. memset(seg->trbs, 0,
  1138. sizeof(union cdnsp_trb) * (TRBS_PER_SEGMENT - 1));
  1139. seg = seg->next;
  1140. }
  1141. /* Set the address in the Command Ring Control register. */
  1142. val_64 = cdnsp_read_64(&pdev->op_regs->cmd_ring);
  1143. val_64 = (val_64 & (u64)CMD_RING_RSVD_BITS) |
  1144. (pdev->cmd_ring->first_seg->dma & (u64)~CMD_RING_RSVD_BITS) |
  1145. pdev->cmd_ring->cycle_state;
  1146. cdnsp_write_64(val_64, &pdev->op_regs->cmd_ring);
  1147. }
  1148. static void cdnsp_consume_all_events(struct cdnsp_device *pdev)
  1149. {
  1150. struct cdnsp_segment *event_deq_seg;
  1151. union cdnsp_trb *event_ring_deq;
  1152. union cdnsp_trb *event;
  1153. u32 cycle_bit;
  1154. event_ring_deq = pdev->event_ring->dequeue;
  1155. event_deq_seg = pdev->event_ring->deq_seg;
  1156. event = pdev->event_ring->dequeue;
  1157. /* Update ring dequeue pointer. */
  1158. while (1) {
  1159. cycle_bit = (le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE);
  1160. /* Does the controller or driver own the TRB? */
  1161. if (cycle_bit != pdev->event_ring->cycle_state)
  1162. break;
  1163. cdnsp_inc_deq(pdev, pdev->event_ring);
  1164. if (!cdnsp_last_trb_on_seg(event_deq_seg, event)) {
  1165. event++;
  1166. continue;
  1167. }
  1168. if (cdnsp_last_trb_on_ring(pdev->event_ring, event_deq_seg,
  1169. event))
  1170. cycle_bit ^= 1;
  1171. event_deq_seg = event_deq_seg->next;
  1172. event = event_deq_seg->trbs;
  1173. }
  1174. cdnsp_update_erst_dequeue(pdev, event_ring_deq, 1);
  1175. }
  1176. static void cdnsp_stop(struct cdnsp_device *pdev)
  1177. {
  1178. u32 temp;
  1179. /* Remove internally queued request for ep0. */
  1180. if (!list_empty(&pdev->eps[0].pending_list)) {
  1181. struct cdnsp_request *req;
  1182. req = next_request(&pdev->eps[0].pending_list);
  1183. if (req == &pdev->ep0_preq)
  1184. cdnsp_ep_dequeue(&pdev->eps[0], req);
  1185. }
  1186. cdnsp_disable_port(pdev, &pdev->usb2_port.regs->portsc);
  1187. cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc);
  1188. cdnsp_disable_slot(pdev);
  1189. cdnsp_halt(pdev);
  1190. temp = readl(&pdev->op_regs->status);
  1191. writel((temp & ~0x1fff) | STS_EINT, &pdev->op_regs->status);
  1192. temp = readl(&pdev->ir_set->irq_pending);
  1193. writel(IMAN_IE_CLEAR(temp), &pdev->ir_set->irq_pending);
  1194. cdnsp_clear_port_change_bit(pdev, &pdev->usb2_port.regs->portsc);
  1195. cdnsp_clear_port_change_bit(pdev, &pdev->usb3_port.regs->portsc);
  1196. /* Clear interrupt line */
  1197. temp = readl(&pdev->ir_set->irq_pending);
  1198. temp |= IMAN_IP;
  1199. writel(temp, &pdev->ir_set->irq_pending);
  1200. cdnsp_consume_all_events(pdev);
  1201. cdnsp_clear_cmd_ring(pdev);
  1202. trace_cdnsp_exit("Controller stopped.");
  1203. }
  1204. /*
  1205. * Stop controller.
  1206. * This function is called by the gadget core when the driver is removed.
  1207. * Disable slot, disable IRQs, and quiesce the controller.
  1208. */
  1209. static int cdnsp_gadget_udc_stop(struct usb_gadget *g)
  1210. {
  1211. struct cdnsp_device *pdev = gadget_to_cdnsp(g);
  1212. unsigned long flags;
  1213. spin_lock_irqsave(&pdev->lock, flags);
  1214. cdnsp_stop(pdev);
  1215. pdev->gadget_driver = NULL;
  1216. spin_unlock_irqrestore(&pdev->lock, flags);
  1217. return 0;
  1218. }
  1219. static int cdnsp_gadget_get_frame(struct usb_gadget *g)
  1220. {
  1221. struct cdnsp_device *pdev = gadget_to_cdnsp(g);
  1222. return cdnsp_get_frame(pdev);
  1223. }
  1224. static void __cdnsp_gadget_wakeup(struct cdnsp_device *pdev)
  1225. {
  1226. struct cdnsp_port_regs __iomem *port_regs;
  1227. u32 portpm, portsc;
  1228. port_regs = pdev->active_port->regs;
  1229. portsc = readl(&port_regs->portsc) & PORT_PLS_MASK;
  1230. /* Remote wakeup feature is not enabled by host. */
  1231. if (pdev->gadget.speed < USB_SPEED_SUPER && portsc == XDEV_U2) {
  1232. portpm = readl(&port_regs->portpmsc);
  1233. if (!(portpm & PORT_RWE))
  1234. return;
  1235. }
  1236. if (portsc == XDEV_U3 && !pdev->may_wakeup)
  1237. return;
  1238. cdnsp_set_link_state(pdev, &port_regs->portsc, XDEV_U0);
  1239. pdev->cdnsp_state |= CDNSP_WAKEUP_PENDING;
  1240. }
  1241. static int cdnsp_gadget_wakeup(struct usb_gadget *g)
  1242. {
  1243. struct cdnsp_device *pdev = gadget_to_cdnsp(g);
  1244. unsigned long flags;
  1245. spin_lock_irqsave(&pdev->lock, flags);
  1246. __cdnsp_gadget_wakeup(pdev);
  1247. spin_unlock_irqrestore(&pdev->lock, flags);
  1248. return 0;
  1249. }
  1250. static int cdnsp_gadget_set_selfpowered(struct usb_gadget *g,
  1251. int is_selfpowered)
  1252. {
  1253. struct cdnsp_device *pdev = gadget_to_cdnsp(g);
  1254. unsigned long flags;
  1255. spin_lock_irqsave(&pdev->lock, flags);
  1256. g->is_selfpowered = !!is_selfpowered;
  1257. spin_unlock_irqrestore(&pdev->lock, flags);
  1258. return 0;
  1259. }
  1260. static int cdnsp_gadget_pullup(struct usb_gadget *gadget, int is_on)
  1261. {
  1262. struct cdnsp_device *pdev = gadget_to_cdnsp(gadget);
  1263. struct cdns *cdns = dev_get_drvdata(pdev->dev);
  1264. unsigned long flags;
  1265. trace_cdnsp_pullup(is_on);
  1266. /*
  1267. * Disable events handling while controller is being
  1268. * enabled/disabled.
  1269. */
  1270. disable_irq(cdns->dev_irq);
  1271. spin_lock_irqsave(&pdev->lock, flags);
  1272. if (!is_on) {
  1273. cdnsp_reset_device(pdev);
  1274. cdns_clear_vbus(cdns);
  1275. } else {
  1276. cdns_set_vbus(cdns);
  1277. }
  1278. spin_unlock_irqrestore(&pdev->lock, flags);
  1279. enable_irq(cdns->dev_irq);
  1280. return 0;
  1281. }
  1282. static const struct usb_gadget_ops cdnsp_gadget_ops = {
  1283. .get_frame = cdnsp_gadget_get_frame,
  1284. .wakeup = cdnsp_gadget_wakeup,
  1285. .set_selfpowered = cdnsp_gadget_set_selfpowered,
  1286. .pullup = cdnsp_gadget_pullup,
  1287. .udc_start = cdnsp_gadget_udc_start,
  1288. .udc_stop = cdnsp_gadget_udc_stop,
  1289. };
  1290. static void cdnsp_get_ep_buffering(struct cdnsp_device *pdev,
  1291. struct cdnsp_ep *pep)
  1292. {
  1293. void __iomem *reg = &pdev->cap_regs->hc_capbase;
  1294. int endpoints;
  1295. reg += cdnsp_find_next_ext_cap(reg, 0, XBUF_CAP_ID);
  1296. if (!pep->direction) {
  1297. pep->buffering = readl(reg + XBUF_RX_TAG_MASK_0_OFFSET);
  1298. pep->buffering_period = readl(reg + XBUF_RX_TAG_MASK_1_OFFSET);
  1299. pep->buffering = (pep->buffering + 1) / 2;
  1300. pep->buffering_period = (pep->buffering_period + 1) / 2;
  1301. return;
  1302. }
  1303. endpoints = HCS_ENDPOINTS(pdev->hcs_params1) / 2;
  1304. /* Set to XBUF_TX_TAG_MASK_0 register. */
  1305. reg += XBUF_TX_CMD_OFFSET + (endpoints * 2 + 2) * sizeof(u32);
  1306. /* Set reg to XBUF_TX_TAG_MASK_N related with this endpoint. */
  1307. reg += pep->number * sizeof(u32) * 2;
  1308. pep->buffering = (readl(reg) + 1) / 2;
  1309. pep->buffering_period = pep->buffering;
  1310. }
  1311. static int cdnsp_gadget_init_endpoints(struct cdnsp_device *pdev)
  1312. {
  1313. int max_streams = HCC_MAX_PSA(pdev->hcc_params);
  1314. struct cdnsp_ep *pep;
  1315. int i;
  1316. INIT_LIST_HEAD(&pdev->gadget.ep_list);
  1317. if (max_streams < STREAM_LOG_STREAMS) {
  1318. dev_err(pdev->dev, "Stream size %d not supported\n",
  1319. max_streams);
  1320. return -EINVAL;
  1321. }
  1322. max_streams = STREAM_LOG_STREAMS;
  1323. for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) {
  1324. bool direction = !(i & 1); /* Start from OUT endpoint. */
  1325. u8 epnum = ((i + 1) >> 1);
  1326. if (!CDNSP_IF_EP_EXIST(pdev, epnum, direction))
  1327. continue;
  1328. pep = &pdev->eps[i];
  1329. pep->pdev = pdev;
  1330. pep->number = epnum;
  1331. pep->direction = direction; /* 0 for OUT, 1 for IN. */
  1332. /*
  1333. * Ep0 is bidirectional, so ep0in and ep0out are represented by
  1334. * pdev->eps[0]
  1335. */
  1336. if (epnum == 0) {
  1337. snprintf(pep->name, sizeof(pep->name), "ep%d%s",
  1338. epnum, "BiDir");
  1339. pep->idx = 0;
  1340. usb_ep_set_maxpacket_limit(&pep->endpoint, 512);
  1341. pep->endpoint.maxburst = 1;
  1342. pep->endpoint.ops = &cdnsp_gadget_ep0_ops;
  1343. pep->endpoint.desc = &cdnsp_gadget_ep0_desc;
  1344. pep->endpoint.comp_desc = NULL;
  1345. pep->endpoint.caps.type_control = true;
  1346. pep->endpoint.caps.dir_in = true;
  1347. pep->endpoint.caps.dir_out = true;
  1348. pdev->ep0_preq.epnum = pep->number;
  1349. pdev->ep0_preq.pep = pep;
  1350. pdev->gadget.ep0 = &pep->endpoint;
  1351. } else {
  1352. snprintf(pep->name, sizeof(pep->name), "ep%d%s",
  1353. epnum, (pep->direction) ? "in" : "out");
  1354. pep->idx = (epnum * 2 + (direction ? 1 : 0)) - 1;
  1355. usb_ep_set_maxpacket_limit(&pep->endpoint, 1024);
  1356. pep->endpoint.max_streams = max_streams;
  1357. pep->endpoint.ops = &cdnsp_gadget_ep_ops;
  1358. list_add_tail(&pep->endpoint.ep_list,
  1359. &pdev->gadget.ep_list);
  1360. pep->endpoint.caps.type_iso = true;
  1361. pep->endpoint.caps.type_bulk = true;
  1362. pep->endpoint.caps.type_int = true;
  1363. pep->endpoint.caps.dir_in = direction;
  1364. pep->endpoint.caps.dir_out = !direction;
  1365. }
  1366. pep->endpoint.name = pep->name;
  1367. pep->in_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, pep->idx);
  1368. pep->out_ctx = cdnsp_get_ep_ctx(&pdev->out_ctx, pep->idx);
  1369. cdnsp_get_ep_buffering(pdev, pep);
  1370. dev_dbg(pdev->dev, "Init %s, MPS: %04x SupType: "
  1371. "CTRL: %s, INT: %s, BULK: %s, ISOC %s, "
  1372. "SupDir IN: %s, OUT: %s\n",
  1373. pep->name, 1024,
  1374. str_yes_no(pep->endpoint.caps.type_control),
  1375. str_yes_no(pep->endpoint.caps.type_int),
  1376. str_yes_no(pep->endpoint.caps.type_bulk),
  1377. str_yes_no(pep->endpoint.caps.type_iso),
  1378. str_yes_no(pep->endpoint.caps.dir_in),
  1379. str_yes_no(pep->endpoint.caps.dir_out));
  1380. INIT_LIST_HEAD(&pep->pending_list);
  1381. }
  1382. return 0;
  1383. }
  1384. static void cdnsp_gadget_free_endpoints(struct cdnsp_device *pdev)
  1385. {
  1386. struct cdnsp_ep *pep;
  1387. int i;
  1388. for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) {
  1389. pep = &pdev->eps[i];
  1390. if (pep->number != 0 && pep->out_ctx)
  1391. list_del(&pep->endpoint.ep_list);
  1392. }
  1393. }
  1394. void cdnsp_disconnect_gadget(struct cdnsp_device *pdev)
  1395. {
  1396. pdev->cdnsp_state |= CDNSP_STATE_DISCONNECT_PENDING;
  1397. if (pdev->gadget_driver && pdev->gadget_driver->disconnect) {
  1398. spin_unlock(&pdev->lock);
  1399. pdev->gadget_driver->disconnect(&pdev->gadget);
  1400. spin_lock(&pdev->lock);
  1401. }
  1402. pdev->gadget.speed = USB_SPEED_UNKNOWN;
  1403. usb_gadget_set_state(&pdev->gadget, USB_STATE_NOTATTACHED);
  1404. pdev->cdnsp_state &= ~CDNSP_STATE_DISCONNECT_PENDING;
  1405. }
  1406. void cdnsp_suspend_gadget(struct cdnsp_device *pdev)
  1407. {
  1408. if (pdev->gadget_driver && pdev->gadget_driver->suspend) {
  1409. spin_unlock(&pdev->lock);
  1410. pdev->gadget_driver->suspend(&pdev->gadget);
  1411. spin_lock(&pdev->lock);
  1412. }
  1413. }
  1414. void cdnsp_resume_gadget(struct cdnsp_device *pdev)
  1415. {
  1416. if (pdev->gadget_driver && pdev->gadget_driver->resume) {
  1417. spin_unlock(&pdev->lock);
  1418. pdev->gadget_driver->resume(&pdev->gadget);
  1419. spin_lock(&pdev->lock);
  1420. }
  1421. }
  1422. void cdnsp_irq_reset(struct cdnsp_device *pdev)
  1423. {
  1424. struct cdnsp_port_regs __iomem *port_regs;
  1425. cdnsp_reset_device(pdev);
  1426. port_regs = pdev->active_port->regs;
  1427. pdev->gadget.speed = cdnsp_port_speed(readl(port_regs));
  1428. spin_unlock(&pdev->lock);
  1429. usb_gadget_udc_reset(&pdev->gadget, pdev->gadget_driver);
  1430. spin_lock(&pdev->lock);
  1431. switch (pdev->gadget.speed) {
  1432. case USB_SPEED_SUPER_PLUS:
  1433. case USB_SPEED_SUPER:
  1434. cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1435. pdev->gadget.ep0->maxpacket = 512;
  1436. break;
  1437. case USB_SPEED_HIGH:
  1438. case USB_SPEED_FULL:
  1439. cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  1440. pdev->gadget.ep0->maxpacket = 64;
  1441. break;
  1442. default:
  1443. /* Low speed is not supported. */
  1444. dev_err(pdev->dev, "Unknown device speed\n");
  1445. break;
  1446. }
  1447. cdnsp_clear_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS);
  1448. cdnsp_setup_device(pdev, SETUP_CONTEXT_ONLY);
  1449. usb_gadget_set_state(&pdev->gadget, USB_STATE_DEFAULT);
  1450. }
  1451. static void cdnsp_get_rev_cap(struct cdnsp_device *pdev)
  1452. {
  1453. void __iomem *reg = &pdev->cap_regs->hc_capbase;
  1454. reg += cdnsp_find_next_ext_cap(reg, 0, RTL_REV_CAP);
  1455. pdev->rev_cap = reg;
  1456. pdev->rtl_revision = readl(&pdev->rev_cap->rtl_revision);
  1457. dev_info(pdev->dev, "Rev: %08x/%08x, eps: %08x, buff: %08x/%08x\n",
  1458. readl(&pdev->rev_cap->ctrl_revision),
  1459. readl(&pdev->rev_cap->rtl_revision),
  1460. readl(&pdev->rev_cap->ep_supported),
  1461. readl(&pdev->rev_cap->rx_buff_size),
  1462. readl(&pdev->rev_cap->tx_buff_size));
  1463. }
  1464. static int cdnsp_gen_setup(struct cdnsp_device *pdev)
  1465. {
  1466. int ret;
  1467. u32 reg;
  1468. pdev->cap_regs = pdev->regs;
  1469. pdev->op_regs = pdev->regs +
  1470. HC_LENGTH(readl(&pdev->cap_regs->hc_capbase));
  1471. pdev->run_regs = pdev->regs +
  1472. (readl(&pdev->cap_regs->run_regs_off) & RTSOFF_MASK);
  1473. /* Cache read-only capability registers */
  1474. pdev->hcs_params1 = readl(&pdev->cap_regs->hcs_params1);
  1475. pdev->hcc_params = readl(&pdev->cap_regs->hc_capbase);
  1476. pdev->hci_version = HC_VERSION(pdev->hcc_params);
  1477. pdev->hcc_params = readl(&pdev->cap_regs->hcc_params);
  1478. /*
  1479. * Override the APB timeout value to give the controller more time for
  1480. * enabling UTMI clock and synchronizing APB and UTMI clock domains.
  1481. * This fix is platform specific and is required to fixes issue with
  1482. * reading incorrect value from PORTSC register after resuming
  1483. * from L1 state.
  1484. */
  1485. cdnsp_set_apb_timeout_value(pdev);
  1486. cdnsp_get_rev_cap(pdev);
  1487. /* Make sure the Device Controller is halted. */
  1488. ret = cdnsp_halt(pdev);
  1489. if (ret)
  1490. return ret;
  1491. /* Reset the internal controller memory state and registers. */
  1492. ret = cdnsp_reset(pdev);
  1493. if (ret)
  1494. return ret;
  1495. /*
  1496. * Set dma_mask and coherent_dma_mask to 64-bits,
  1497. * if controller supports 64-bit addressing.
  1498. */
  1499. if (HCC_64BIT_ADDR(pdev->hcc_params) &&
  1500. !dma_set_mask(pdev->dev, DMA_BIT_MASK(64))) {
  1501. dev_dbg(pdev->dev, "Enabling 64-bit DMA addresses.\n");
  1502. dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(64));
  1503. } else {
  1504. /*
  1505. * This is to avoid error in cases where a 32-bit USB
  1506. * controller is used on a 64-bit capable system.
  1507. */
  1508. ret = dma_set_mask(pdev->dev, DMA_BIT_MASK(32));
  1509. if (ret)
  1510. return ret;
  1511. dev_dbg(pdev->dev, "Enabling 32-bit DMA addresses.\n");
  1512. dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(32));
  1513. }
  1514. spin_lock_init(&pdev->lock);
  1515. ret = cdnsp_mem_init(pdev);
  1516. if (ret)
  1517. return ret;
  1518. /*
  1519. * Software workaround for U1: after transition
  1520. * to U1 the controller starts gating clock, and in some cases,
  1521. * it causes that controller stack.
  1522. */
  1523. reg = readl(&pdev->port3x_regs->mode_2);
  1524. reg &= ~CFG_3XPORT_U1_PIPE_CLK_GATE_EN;
  1525. writel(reg, &pdev->port3x_regs->mode_2);
  1526. return 0;
  1527. }
  1528. static int __cdnsp_gadget_init(struct cdns *cdns)
  1529. {
  1530. struct cdnsp_device *pdev;
  1531. u32 max_speed;
  1532. int ret = -ENOMEM;
  1533. cdns_drd_gadget_on(cdns);
  1534. pdev = kzalloc_obj(*pdev);
  1535. if (!pdev)
  1536. return -ENOMEM;
  1537. pm_runtime_get_sync(cdns->dev);
  1538. cdns->gadget_dev = pdev;
  1539. pdev->dev = cdns->dev;
  1540. pdev->regs = cdns->dev_regs;
  1541. max_speed = usb_get_maximum_speed(cdns->dev);
  1542. switch (max_speed) {
  1543. case USB_SPEED_FULL:
  1544. case USB_SPEED_HIGH:
  1545. case USB_SPEED_SUPER:
  1546. case USB_SPEED_SUPER_PLUS:
  1547. break;
  1548. default:
  1549. dev_err(cdns->dev, "invalid speed parameter %d\n", max_speed);
  1550. fallthrough;
  1551. case USB_SPEED_UNKNOWN:
  1552. /* Default to SSP */
  1553. max_speed = USB_SPEED_SUPER_PLUS;
  1554. break;
  1555. }
  1556. pdev->gadget.ops = &cdnsp_gadget_ops;
  1557. pdev->gadget.name = "cdnsp-gadget";
  1558. pdev->gadget.speed = USB_SPEED_UNKNOWN;
  1559. pdev->gadget.sg_supported = 1;
  1560. pdev->gadget.max_speed = max_speed;
  1561. pdev->gadget.lpm_capable = 1;
  1562. pdev->setup_buf = kzalloc(CDNSP_EP0_SETUP_SIZE, GFP_KERNEL);
  1563. if (!pdev->setup_buf)
  1564. goto free_pdev;
  1565. /*
  1566. * Controller supports not aligned buffer but it should improve
  1567. * performance.
  1568. */
  1569. pdev->gadget.quirk_ep_out_aligned_size = true;
  1570. ret = cdnsp_gen_setup(pdev);
  1571. if (ret) {
  1572. dev_err(pdev->dev, "Generic initialization failed %d\n", ret);
  1573. goto free_setup;
  1574. }
  1575. ret = cdnsp_gadget_init_endpoints(pdev);
  1576. if (ret) {
  1577. dev_err(pdev->dev, "failed to initialize endpoints\n");
  1578. goto halt_pdev;
  1579. }
  1580. ret = usb_add_gadget_udc(pdev->dev, &pdev->gadget);
  1581. if (ret) {
  1582. dev_err(pdev->dev, "failed to register udc\n");
  1583. goto free_endpoints;
  1584. }
  1585. ret = devm_request_threaded_irq(pdev->dev, cdns->dev_irq,
  1586. cdnsp_irq_handler,
  1587. cdnsp_thread_irq_handler, IRQF_SHARED,
  1588. dev_name(pdev->dev), pdev);
  1589. if (ret)
  1590. goto del_gadget;
  1591. return 0;
  1592. del_gadget:
  1593. usb_del_gadget(&pdev->gadget);
  1594. cdnsp_gadget_free_endpoints(pdev);
  1595. usb_put_gadget(&pdev->gadget);
  1596. goto halt_pdev;
  1597. free_endpoints:
  1598. cdnsp_gadget_free_endpoints(pdev);
  1599. halt_pdev:
  1600. cdnsp_halt(pdev);
  1601. cdnsp_reset(pdev);
  1602. cdnsp_mem_cleanup(pdev);
  1603. free_setup:
  1604. kfree(pdev->setup_buf);
  1605. free_pdev:
  1606. kfree(pdev);
  1607. return ret;
  1608. }
  1609. static void cdnsp_gadget_exit(struct cdns *cdns)
  1610. {
  1611. struct cdnsp_device *pdev = cdns->gadget_dev;
  1612. devm_free_irq(pdev->dev, cdns->dev_irq, pdev);
  1613. pm_runtime_put_autosuspend(cdns->dev);
  1614. usb_del_gadget(&pdev->gadget);
  1615. cdnsp_gadget_free_endpoints(pdev);
  1616. usb_put_gadget(&pdev->gadget);
  1617. cdnsp_mem_cleanup(pdev);
  1618. kfree(pdev);
  1619. cdns->gadget_dev = NULL;
  1620. cdns_drd_gadget_off(cdns);
  1621. }
  1622. static int cdnsp_gadget_suspend(struct cdns *cdns, bool do_wakeup)
  1623. {
  1624. struct cdnsp_device *pdev = cdns->gadget_dev;
  1625. unsigned long flags;
  1626. if (pdev->link_state == XDEV_U3)
  1627. return 0;
  1628. spin_lock_irqsave(&pdev->lock, flags);
  1629. cdnsp_disconnect_gadget(pdev);
  1630. cdnsp_stop(pdev);
  1631. spin_unlock_irqrestore(&pdev->lock, flags);
  1632. return 0;
  1633. }
  1634. static int cdnsp_gadget_resume(struct cdns *cdns, bool lost_power)
  1635. {
  1636. struct cdnsp_device *pdev = cdns->gadget_dev;
  1637. enum usb_device_speed max_speed;
  1638. unsigned long flags;
  1639. int ret;
  1640. if (!pdev->gadget_driver)
  1641. return 0;
  1642. spin_lock_irqsave(&pdev->lock, flags);
  1643. max_speed = pdev->gadget_driver->max_speed;
  1644. /* Limit speed if necessary. */
  1645. max_speed = min(max_speed, pdev->gadget.max_speed);
  1646. ret = cdnsp_run(pdev, max_speed);
  1647. if (pdev->link_state == XDEV_U3)
  1648. __cdnsp_gadget_wakeup(pdev);
  1649. spin_unlock_irqrestore(&pdev->lock, flags);
  1650. return ret;
  1651. }
  1652. /**
  1653. * cdnsp_gadget_init - initialize device structure
  1654. * @cdns: cdnsp instance
  1655. *
  1656. * This function initializes the gadget.
  1657. */
  1658. int cdnsp_gadget_init(struct cdns *cdns)
  1659. {
  1660. struct cdns_role_driver *rdrv;
  1661. rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
  1662. if (!rdrv)
  1663. return -ENOMEM;
  1664. rdrv->start = __cdnsp_gadget_init;
  1665. rdrv->stop = cdnsp_gadget_exit;
  1666. rdrv->suspend = cdnsp_gadget_suspend;
  1667. rdrv->resume = cdnsp_gadget_resume;
  1668. rdrv->state = CDNS_ROLE_STATE_INACTIVE;
  1669. rdrv->name = "gadget";
  1670. cdns->roles[USB_ROLE_DEVICE] = rdrv;
  1671. return 0;
  1672. }