cdnsp-gadget.c 51 KB

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