musb_gadget.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  1. /*
  2. * MUSB OTG driver peripheral support
  3. *
  4. * Copyright 2005 Mentor Graphics Corporation
  5. * Copyright (C) 2005-2006 by Texas Instruments
  6. * Copyright (C) 2006-2007 Nokia Corporation
  7. * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
  8. *
  9. * SPDX-License-Identifier: GPL-2.0
  10. */
  11. #ifndef __UBOOT__
  12. #include <linux/kernel.h>
  13. #include <linux/list.h>
  14. #include <linux/timer.h>
  15. #include <linux/module.h>
  16. #include <linux/smp.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/delay.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/slab.h>
  21. #else
  22. #include <common.h>
  23. #include <linux/usb/ch9.h>
  24. #include "linux-compat.h"
  25. #endif
  26. #include "musb_core.h"
  27. /* MUSB PERIPHERAL status 3-mar-2006:
  28. *
  29. * - EP0 seems solid. It passes both USBCV and usbtest control cases.
  30. * Minor glitches:
  31. *
  32. * + remote wakeup to Linux hosts work, but saw USBCV failures;
  33. * in one test run (operator error?)
  34. * + endpoint halt tests -- in both usbtest and usbcv -- seem
  35. * to break when dma is enabled ... is something wrongly
  36. * clearing SENDSTALL?
  37. *
  38. * - Mass storage behaved ok when last tested. Network traffic patterns
  39. * (with lots of short transfers etc) need retesting; they turn up the
  40. * worst cases of the DMA, since short packets are typical but are not
  41. * required.
  42. *
  43. * - TX/IN
  44. * + both pio and dma behave in with network and g_zero tests
  45. * + no cppi throughput issues other than no-hw-queueing
  46. * + failed with FLAT_REG (DaVinci)
  47. * + seems to behave with double buffering, PIO -and- CPPI
  48. * + with gadgetfs + AIO, requests got lost?
  49. *
  50. * - RX/OUT
  51. * + both pio and dma behave in with network and g_zero tests
  52. * + dma is slow in typical case (short_not_ok is clear)
  53. * + double buffering ok with PIO
  54. * + double buffering *FAILS* with CPPI, wrong data bytes sometimes
  55. * + request lossage observed with gadgetfs
  56. *
  57. * - ISO not tested ... might work, but only weakly isochronous
  58. *
  59. * - Gadget driver disabling of softconnect during bind() is ignored; so
  60. * drivers can't hold off host requests until userspace is ready.
  61. * (Workaround: they can turn it off later.)
  62. *
  63. * - PORTABILITY (assumes PIO works):
  64. * + DaVinci, basically works with cppi dma
  65. * + OMAP 2430, ditto with mentor dma
  66. * + TUSB 6010, platform-specific dma in the works
  67. */
  68. /* ----------------------------------------------------------------------- */
  69. #define is_buffer_mapped(req) (is_dma_capable() && \
  70. (req->map_state != UN_MAPPED))
  71. #ifndef CONFIG_USB_MUSB_PIO_ONLY
  72. /* Maps the buffer to dma */
  73. static inline void map_dma_buffer(struct musb_request *request,
  74. struct musb *musb, struct musb_ep *musb_ep)
  75. {
  76. int compatible = true;
  77. struct dma_controller *dma = musb->dma_controller;
  78. request->map_state = UN_MAPPED;
  79. if (!is_dma_capable() || !musb_ep->dma)
  80. return;
  81. /* Check if DMA engine can handle this request.
  82. * DMA code must reject the USB request explicitly.
  83. * Default behaviour is to map the request.
  84. */
  85. if (dma->is_compatible)
  86. compatible = dma->is_compatible(musb_ep->dma,
  87. musb_ep->packet_sz, request->request.buf,
  88. request->request.length);
  89. if (!compatible)
  90. return;
  91. if (request->request.dma == DMA_ADDR_INVALID) {
  92. request->request.dma = dma_map_single(
  93. musb->controller,
  94. request->request.buf,
  95. request->request.length,
  96. request->tx
  97. ? DMA_TO_DEVICE
  98. : DMA_FROM_DEVICE);
  99. request->map_state = MUSB_MAPPED;
  100. } else {
  101. dma_sync_single_for_device(musb->controller,
  102. request->request.dma,
  103. request->request.length,
  104. request->tx
  105. ? DMA_TO_DEVICE
  106. : DMA_FROM_DEVICE);
  107. request->map_state = PRE_MAPPED;
  108. }
  109. }
  110. /* Unmap the buffer from dma and maps it back to cpu */
  111. static inline void unmap_dma_buffer(struct musb_request *request,
  112. struct musb *musb)
  113. {
  114. if (!is_buffer_mapped(request))
  115. return;
  116. if (request->request.dma == DMA_ADDR_INVALID) {
  117. dev_vdbg(musb->controller,
  118. "not unmapping a never mapped buffer\n");
  119. return;
  120. }
  121. if (request->map_state == MUSB_MAPPED) {
  122. dma_unmap_single(musb->controller,
  123. request->request.dma,
  124. request->request.length,
  125. request->tx
  126. ? DMA_TO_DEVICE
  127. : DMA_FROM_DEVICE);
  128. request->request.dma = DMA_ADDR_INVALID;
  129. } else { /* PRE_MAPPED */
  130. dma_sync_single_for_cpu(musb->controller,
  131. request->request.dma,
  132. request->request.length,
  133. request->tx
  134. ? DMA_TO_DEVICE
  135. : DMA_FROM_DEVICE);
  136. }
  137. request->map_state = UN_MAPPED;
  138. }
  139. #else
  140. static inline void map_dma_buffer(struct musb_request *request,
  141. struct musb *musb, struct musb_ep *musb_ep)
  142. {
  143. }
  144. static inline void unmap_dma_buffer(struct musb_request *request,
  145. struct musb *musb)
  146. {
  147. }
  148. #endif
  149. /*
  150. * Immediately complete a request.
  151. *
  152. * @param request the request to complete
  153. * @param status the status to complete the request with
  154. * Context: controller locked, IRQs blocked.
  155. */
  156. void musb_g_giveback(
  157. struct musb_ep *ep,
  158. struct usb_request *request,
  159. int status)
  160. __releases(ep->musb->lock)
  161. __acquires(ep->musb->lock)
  162. {
  163. struct musb_request *req;
  164. struct musb *musb;
  165. int busy = ep->busy;
  166. req = to_musb_request(request);
  167. list_del(&req->list);
  168. if (req->request.status == -EINPROGRESS)
  169. req->request.status = status;
  170. musb = req->musb;
  171. ep->busy = 1;
  172. spin_unlock(&musb->lock);
  173. unmap_dma_buffer(req, musb);
  174. if (request->status == 0)
  175. dev_dbg(musb->controller, "%s done request %p, %d/%d\n",
  176. ep->end_point.name, request,
  177. req->request.actual, req->request.length);
  178. else
  179. dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n",
  180. ep->end_point.name, request,
  181. req->request.actual, req->request.length,
  182. request->status);
  183. req->request.complete(&req->ep->end_point, &req->request);
  184. spin_lock(&musb->lock);
  185. ep->busy = busy;
  186. }
  187. /* ----------------------------------------------------------------------- */
  188. /*
  189. * Abort requests queued to an endpoint using the status. Synchronous.
  190. * caller locked controller and blocked irqs, and selected this ep.
  191. */
  192. static void nuke(struct musb_ep *ep, const int status)
  193. {
  194. struct musb *musb = ep->musb;
  195. struct musb_request *req = NULL;
  196. void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
  197. ep->busy = 1;
  198. if (is_dma_capable() && ep->dma) {
  199. struct dma_controller *c = ep->musb->dma_controller;
  200. int value;
  201. if (ep->is_in) {
  202. /*
  203. * The programming guide says that we must not clear
  204. * the DMAMODE bit before DMAENAB, so we only
  205. * clear it in the second write...
  206. */
  207. musb_writew(epio, MUSB_TXCSR,
  208. MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
  209. musb_writew(epio, MUSB_TXCSR,
  210. 0 | MUSB_TXCSR_FLUSHFIFO);
  211. } else {
  212. musb_writew(epio, MUSB_RXCSR,
  213. 0 | MUSB_RXCSR_FLUSHFIFO);
  214. musb_writew(epio, MUSB_RXCSR,
  215. 0 | MUSB_RXCSR_FLUSHFIFO);
  216. }
  217. value = c->channel_abort(ep->dma);
  218. dev_dbg(musb->controller, "%s: abort DMA --> %d\n",
  219. ep->name, value);
  220. c->channel_release(ep->dma);
  221. ep->dma = NULL;
  222. }
  223. while (!list_empty(&ep->req_list)) {
  224. req = list_first_entry(&ep->req_list, struct musb_request, list);
  225. musb_g_giveback(ep, &req->request, status);
  226. }
  227. }
  228. /* ----------------------------------------------------------------------- */
  229. /* Data transfers - pure PIO, pure DMA, or mixed mode */
  230. /*
  231. * This assumes the separate CPPI engine is responding to DMA requests
  232. * from the usb core ... sequenced a bit differently from mentor dma.
  233. */
  234. static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
  235. {
  236. if (can_bulk_split(musb, ep->type))
  237. return ep->hw_ep->max_packet_sz_tx;
  238. else
  239. return ep->packet_sz;
  240. }
  241. #ifdef CONFIG_USB_INVENTRA_DMA
  242. /* Peripheral tx (IN) using Mentor DMA works as follows:
  243. Only mode 0 is used for transfers <= wPktSize,
  244. mode 1 is used for larger transfers,
  245. One of the following happens:
  246. - Host sends IN token which causes an endpoint interrupt
  247. -> TxAvail
  248. -> if DMA is currently busy, exit.
  249. -> if queue is non-empty, txstate().
  250. - Request is queued by the gadget driver.
  251. -> if queue was previously empty, txstate()
  252. txstate()
  253. -> start
  254. /\ -> setup DMA
  255. | (data is transferred to the FIFO, then sent out when
  256. | IN token(s) are recd from Host.
  257. | -> DMA interrupt on completion
  258. | calls TxAvail.
  259. | -> stop DMA, ~DMAENAB,
  260. | -> set TxPktRdy for last short pkt or zlp
  261. | -> Complete Request
  262. | -> Continue next request (call txstate)
  263. |___________________________________|
  264. * Non-Mentor DMA engines can of course work differently, such as by
  265. * upleveling from irq-per-packet to irq-per-buffer.
  266. */
  267. #endif
  268. /*
  269. * An endpoint is transmitting data. This can be called either from
  270. * the IRQ routine or from ep.queue() to kickstart a request on an
  271. * endpoint.
  272. *
  273. * Context: controller locked, IRQs blocked, endpoint selected
  274. */
  275. static void txstate(struct musb *musb, struct musb_request *req)
  276. {
  277. u8 epnum = req->epnum;
  278. struct musb_ep *musb_ep;
  279. void __iomem *epio = musb->endpoints[epnum].regs;
  280. struct usb_request *request;
  281. u16 fifo_count = 0, csr;
  282. int use_dma = 0;
  283. musb_ep = req->ep;
  284. /* Check if EP is disabled */
  285. if (!musb_ep->desc) {
  286. dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
  287. musb_ep->end_point.name);
  288. return;
  289. }
  290. /* we shouldn't get here while DMA is active ... but we do ... */
  291. if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
  292. dev_dbg(musb->controller, "dma pending...\n");
  293. return;
  294. }
  295. /* read TXCSR before */
  296. csr = musb_readw(epio, MUSB_TXCSR);
  297. request = &req->request;
  298. fifo_count = min(max_ep_writesize(musb, musb_ep),
  299. (int)(request->length - request->actual));
  300. if (csr & MUSB_TXCSR_TXPKTRDY) {
  301. dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n",
  302. musb_ep->end_point.name, csr);
  303. return;
  304. }
  305. if (csr & MUSB_TXCSR_P_SENDSTALL) {
  306. dev_dbg(musb->controller, "%s stalling, txcsr %03x\n",
  307. musb_ep->end_point.name, csr);
  308. return;
  309. }
  310. dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
  311. epnum, musb_ep->packet_sz, fifo_count,
  312. csr);
  313. #ifndef CONFIG_USB_MUSB_PIO_ONLY
  314. if (is_buffer_mapped(req)) {
  315. struct dma_controller *c = musb->dma_controller;
  316. size_t request_size;
  317. /* setup DMA, then program endpoint CSR */
  318. request_size = min_t(size_t, request->length - request->actual,
  319. musb_ep->dma->max_len);
  320. use_dma = (request->dma != DMA_ADDR_INVALID);
  321. /* MUSB_TXCSR_P_ISO is still set correctly */
  322. #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
  323. {
  324. if (request_size < musb_ep->packet_sz)
  325. musb_ep->dma->desired_mode = 0;
  326. else
  327. musb_ep->dma->desired_mode = 1;
  328. use_dma = use_dma && c->channel_program(
  329. musb_ep->dma, musb_ep->packet_sz,
  330. musb_ep->dma->desired_mode,
  331. request->dma + request->actual, request_size);
  332. if (use_dma) {
  333. if (musb_ep->dma->desired_mode == 0) {
  334. /*
  335. * We must not clear the DMAMODE bit
  336. * before the DMAENAB bit -- and the
  337. * latter doesn't always get cleared
  338. * before we get here...
  339. */
  340. csr &= ~(MUSB_TXCSR_AUTOSET
  341. | MUSB_TXCSR_DMAENAB);
  342. musb_writew(epio, MUSB_TXCSR, csr
  343. | MUSB_TXCSR_P_WZC_BITS);
  344. csr &= ~MUSB_TXCSR_DMAMODE;
  345. csr |= (MUSB_TXCSR_DMAENAB |
  346. MUSB_TXCSR_MODE);
  347. /* against programming guide */
  348. } else {
  349. csr |= (MUSB_TXCSR_DMAENAB
  350. | MUSB_TXCSR_DMAMODE
  351. | MUSB_TXCSR_MODE);
  352. if (!musb_ep->hb_mult)
  353. csr |= MUSB_TXCSR_AUTOSET;
  354. }
  355. csr &= ~MUSB_TXCSR_P_UNDERRUN;
  356. musb_writew(epio, MUSB_TXCSR, csr);
  357. }
  358. }
  359. #elif defined(CONFIG_USB_TI_CPPI_DMA)
  360. /* program endpoint CSR first, then setup DMA */
  361. csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
  362. csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
  363. MUSB_TXCSR_MODE;
  364. musb_writew(epio, MUSB_TXCSR,
  365. (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
  366. | csr);
  367. /* ensure writebuffer is empty */
  368. csr = musb_readw(epio, MUSB_TXCSR);
  369. /* NOTE host side sets DMAENAB later than this; both are
  370. * OK since the transfer dma glue (between CPPI and Mentor
  371. * fifos) just tells CPPI it could start. Data only moves
  372. * to the USB TX fifo when both fifos are ready.
  373. */
  374. /* "mode" is irrelevant here; handle terminating ZLPs like
  375. * PIO does, since the hardware RNDIS mode seems unreliable
  376. * except for the last-packet-is-already-short case.
  377. */
  378. use_dma = use_dma && c->channel_program(
  379. musb_ep->dma, musb_ep->packet_sz,
  380. 0,
  381. request->dma + request->actual,
  382. request_size);
  383. if (!use_dma) {
  384. c->channel_release(musb_ep->dma);
  385. musb_ep->dma = NULL;
  386. csr &= ~MUSB_TXCSR_DMAENAB;
  387. musb_writew(epio, MUSB_TXCSR, csr);
  388. /* invariant: prequest->buf is non-null */
  389. }
  390. #elif defined(CONFIG_USB_TUSB_OMAP_DMA)
  391. use_dma = use_dma && c->channel_program(
  392. musb_ep->dma, musb_ep->packet_sz,
  393. request->zero,
  394. request->dma + request->actual,
  395. request_size);
  396. #endif
  397. }
  398. #endif
  399. if (!use_dma) {
  400. /*
  401. * Unmap the dma buffer back to cpu if dma channel
  402. * programming fails
  403. */
  404. unmap_dma_buffer(req, musb);
  405. musb_write_fifo(musb_ep->hw_ep, fifo_count,
  406. (u8 *) (request->buf + request->actual));
  407. request->actual += fifo_count;
  408. csr |= MUSB_TXCSR_TXPKTRDY;
  409. csr &= ~MUSB_TXCSR_P_UNDERRUN;
  410. musb_writew(epio, MUSB_TXCSR, csr);
  411. }
  412. /* host may already have the data when this message shows... */
  413. dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
  414. musb_ep->end_point.name, use_dma ? "dma" : "pio",
  415. request->actual, request->length,
  416. musb_readw(epio, MUSB_TXCSR),
  417. fifo_count,
  418. musb_readw(epio, MUSB_TXMAXP));
  419. }
  420. /*
  421. * FIFO state update (e.g. data ready).
  422. * Called from IRQ, with controller locked.
  423. */
  424. void musb_g_tx(struct musb *musb, u8 epnum)
  425. {
  426. u16 csr;
  427. struct musb_request *req;
  428. struct usb_request *request;
  429. u8 __iomem *mbase = musb->mregs;
  430. struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
  431. void __iomem *epio = musb->endpoints[epnum].regs;
  432. struct dma_channel *dma;
  433. musb_ep_select(mbase, epnum);
  434. req = next_request(musb_ep);
  435. request = &req->request;
  436. csr = musb_readw(epio, MUSB_TXCSR);
  437. dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
  438. dma = is_dma_capable() ? musb_ep->dma : NULL;
  439. /*
  440. * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
  441. * probably rates reporting as a host error.
  442. */
  443. if (csr & MUSB_TXCSR_P_SENTSTALL) {
  444. csr |= MUSB_TXCSR_P_WZC_BITS;
  445. csr &= ~MUSB_TXCSR_P_SENTSTALL;
  446. musb_writew(epio, MUSB_TXCSR, csr);
  447. return;
  448. }
  449. if (csr & MUSB_TXCSR_P_UNDERRUN) {
  450. /* We NAKed, no big deal... little reason to care. */
  451. csr |= MUSB_TXCSR_P_WZC_BITS;
  452. csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
  453. musb_writew(epio, MUSB_TXCSR, csr);
  454. dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
  455. epnum, request);
  456. }
  457. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  458. /*
  459. * SHOULD NOT HAPPEN... has with CPPI though, after
  460. * changing SENDSTALL (and other cases); harmless?
  461. */
  462. dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name);
  463. return;
  464. }
  465. if (request) {
  466. u8 is_dma = 0;
  467. if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
  468. is_dma = 1;
  469. csr |= MUSB_TXCSR_P_WZC_BITS;
  470. csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
  471. MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
  472. musb_writew(epio, MUSB_TXCSR, csr);
  473. /* Ensure writebuffer is empty. */
  474. csr = musb_readw(epio, MUSB_TXCSR);
  475. request->actual += musb_ep->dma->actual_len;
  476. dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
  477. epnum, csr, musb_ep->dma->actual_len, request);
  478. }
  479. /*
  480. * First, maybe a terminating short packet. Some DMA
  481. * engines might handle this by themselves.
  482. */
  483. if ((request->zero && request->length
  484. && (request->length % musb_ep->packet_sz == 0)
  485. && (request->actual == request->length))
  486. #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
  487. || (is_dma && (!dma->desired_mode ||
  488. (request->actual &
  489. (musb_ep->packet_sz - 1))))
  490. #endif
  491. ) {
  492. /*
  493. * On DMA completion, FIFO may not be
  494. * available yet...
  495. */
  496. if (csr & MUSB_TXCSR_TXPKTRDY)
  497. return;
  498. dev_dbg(musb->controller, "sending zero pkt\n");
  499. musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
  500. | MUSB_TXCSR_TXPKTRDY);
  501. request->zero = 0;
  502. }
  503. if (request->actual == request->length) {
  504. musb_g_giveback(musb_ep, request, 0);
  505. /*
  506. * In the giveback function the MUSB lock is
  507. * released and acquired after sometime. During
  508. * this time period the INDEX register could get
  509. * changed by the gadget_queue function especially
  510. * on SMP systems. Reselect the INDEX to be sure
  511. * we are reading/modifying the right registers
  512. */
  513. musb_ep_select(mbase, epnum);
  514. req = musb_ep->desc ? next_request(musb_ep) : NULL;
  515. if (!req) {
  516. dev_dbg(musb->controller, "%s idle now\n",
  517. musb_ep->end_point.name);
  518. return;
  519. }
  520. }
  521. txstate(musb, req);
  522. }
  523. }
  524. /* ------------------------------------------------------------ */
  525. #ifdef CONFIG_USB_INVENTRA_DMA
  526. /* Peripheral rx (OUT) using Mentor DMA works as follows:
  527. - Only mode 0 is used.
  528. - Request is queued by the gadget class driver.
  529. -> if queue was previously empty, rxstate()
  530. - Host sends OUT token which causes an endpoint interrupt
  531. /\ -> RxReady
  532. | -> if request queued, call rxstate
  533. | /\ -> setup DMA
  534. | | -> DMA interrupt on completion
  535. | | -> RxReady
  536. | | -> stop DMA
  537. | | -> ack the read
  538. | | -> if data recd = max expected
  539. | | by the request, or host
  540. | | sent a short packet,
  541. | | complete the request,
  542. | | and start the next one.
  543. | |_____________________________________|
  544. | else just wait for the host
  545. | to send the next OUT token.
  546. |__________________________________________________|
  547. * Non-Mentor DMA engines can of course work differently.
  548. */
  549. #endif
  550. /*
  551. * Context: controller locked, IRQs blocked, endpoint selected
  552. */
  553. static void rxstate(struct musb *musb, struct musb_request *req)
  554. {
  555. const u8 epnum = req->epnum;
  556. struct usb_request *request = &req->request;
  557. struct musb_ep *musb_ep;
  558. void __iomem *epio = musb->endpoints[epnum].regs;
  559. unsigned fifo_count = 0;
  560. u16 len;
  561. u16 csr = musb_readw(epio, MUSB_RXCSR);
  562. struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
  563. u8 use_mode_1;
  564. if (hw_ep->is_shared_fifo)
  565. musb_ep = &hw_ep->ep_in;
  566. else
  567. musb_ep = &hw_ep->ep_out;
  568. len = musb_ep->packet_sz;
  569. /* Check if EP is disabled */
  570. if (!musb_ep->desc) {
  571. dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
  572. musb_ep->end_point.name);
  573. return;
  574. }
  575. /* We shouldn't get here while DMA is active, but we do... */
  576. if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
  577. dev_dbg(musb->controller, "DMA pending...\n");
  578. return;
  579. }
  580. if (csr & MUSB_RXCSR_P_SENDSTALL) {
  581. dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n",
  582. musb_ep->end_point.name, csr);
  583. return;
  584. }
  585. if (is_cppi_enabled() && is_buffer_mapped(req)) {
  586. struct dma_controller *c = musb->dma_controller;
  587. struct dma_channel *channel = musb_ep->dma;
  588. /* NOTE: CPPI won't actually stop advancing the DMA
  589. * queue after short packet transfers, so this is almost
  590. * always going to run as IRQ-per-packet DMA so that
  591. * faults will be handled correctly.
  592. */
  593. if (c->channel_program(channel,
  594. musb_ep->packet_sz,
  595. !request->short_not_ok,
  596. request->dma + request->actual,
  597. request->length - request->actual)) {
  598. /* make sure that if an rxpkt arrived after the irq,
  599. * the cppi engine will be ready to take it as soon
  600. * as DMA is enabled
  601. */
  602. csr &= ~(MUSB_RXCSR_AUTOCLEAR
  603. | MUSB_RXCSR_DMAMODE);
  604. csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
  605. musb_writew(epio, MUSB_RXCSR, csr);
  606. return;
  607. }
  608. }
  609. if (csr & MUSB_RXCSR_RXPKTRDY) {
  610. len = musb_readw(epio, MUSB_RXCOUNT);
  611. /*
  612. * Enable Mode 1 on RX transfers only when short_not_ok flag
  613. * is set. Currently short_not_ok flag is set only from
  614. * file_storage and f_mass_storage drivers
  615. */
  616. if (request->short_not_ok && len == musb_ep->packet_sz)
  617. use_mode_1 = 1;
  618. else
  619. use_mode_1 = 0;
  620. if (request->actual < request->length) {
  621. #ifdef CONFIG_USB_INVENTRA_DMA
  622. if (is_buffer_mapped(req)) {
  623. struct dma_controller *c;
  624. struct dma_channel *channel;
  625. int use_dma = 0;
  626. c = musb->dma_controller;
  627. channel = musb_ep->dma;
  628. /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
  629. * mode 0 only. So we do not get endpoint interrupts due to DMA
  630. * completion. We only get interrupts from DMA controller.
  631. *
  632. * We could operate in DMA mode 1 if we knew the size of the tranfer
  633. * in advance. For mass storage class, request->length = what the host
  634. * sends, so that'd work. But for pretty much everything else,
  635. * request->length is routinely more than what the host sends. For
  636. * most these gadgets, end of is signified either by a short packet,
  637. * or filling the last byte of the buffer. (Sending extra data in
  638. * that last pckate should trigger an overflow fault.) But in mode 1,
  639. * we don't get DMA completion interrupt for short packets.
  640. *
  641. * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
  642. * to get endpoint interrupt on every DMA req, but that didn't seem
  643. * to work reliably.
  644. *
  645. * REVISIT an updated g_file_storage can set req->short_not_ok, which
  646. * then becomes usable as a runtime "use mode 1" hint...
  647. */
  648. /* Experimental: Mode1 works with mass storage use cases */
  649. if (use_mode_1) {
  650. csr |= MUSB_RXCSR_AUTOCLEAR;
  651. musb_writew(epio, MUSB_RXCSR, csr);
  652. csr |= MUSB_RXCSR_DMAENAB;
  653. musb_writew(epio, MUSB_RXCSR, csr);
  654. /*
  655. * this special sequence (enabling and then
  656. * disabling MUSB_RXCSR_DMAMODE) is required
  657. * to get DMAReq to activate
  658. */
  659. musb_writew(epio, MUSB_RXCSR,
  660. csr | MUSB_RXCSR_DMAMODE);
  661. musb_writew(epio, MUSB_RXCSR, csr);
  662. } else {
  663. if (!musb_ep->hb_mult &&
  664. musb_ep->hw_ep->rx_double_buffered)
  665. csr |= MUSB_RXCSR_AUTOCLEAR;
  666. csr |= MUSB_RXCSR_DMAENAB;
  667. musb_writew(epio, MUSB_RXCSR, csr);
  668. }
  669. if (request->actual < request->length) {
  670. int transfer_size = 0;
  671. if (use_mode_1) {
  672. transfer_size = min(request->length - request->actual,
  673. channel->max_len);
  674. musb_ep->dma->desired_mode = 1;
  675. } else {
  676. transfer_size = min(request->length - request->actual,
  677. (unsigned)len);
  678. musb_ep->dma->desired_mode = 0;
  679. }
  680. use_dma = c->channel_program(
  681. channel,
  682. musb_ep->packet_sz,
  683. channel->desired_mode,
  684. request->dma
  685. + request->actual,
  686. transfer_size);
  687. }
  688. if (use_dma)
  689. return;
  690. }
  691. #elif defined(CONFIG_USB_UX500_DMA)
  692. if ((is_buffer_mapped(req)) &&
  693. (request->actual < request->length)) {
  694. struct dma_controller *c;
  695. struct dma_channel *channel;
  696. int transfer_size = 0;
  697. c = musb->dma_controller;
  698. channel = musb_ep->dma;
  699. /* In case first packet is short */
  700. if (len < musb_ep->packet_sz)
  701. transfer_size = len;
  702. else if (request->short_not_ok)
  703. transfer_size = min(request->length -
  704. request->actual,
  705. channel->max_len);
  706. else
  707. transfer_size = min(request->length -
  708. request->actual,
  709. (unsigned)len);
  710. csr &= ~MUSB_RXCSR_DMAMODE;
  711. csr |= (MUSB_RXCSR_DMAENAB |
  712. MUSB_RXCSR_AUTOCLEAR);
  713. musb_writew(epio, MUSB_RXCSR, csr);
  714. if (transfer_size <= musb_ep->packet_sz) {
  715. musb_ep->dma->desired_mode = 0;
  716. } else {
  717. musb_ep->dma->desired_mode = 1;
  718. /* Mode must be set after DMAENAB */
  719. csr |= MUSB_RXCSR_DMAMODE;
  720. musb_writew(epio, MUSB_RXCSR, csr);
  721. }
  722. if (c->channel_program(channel,
  723. musb_ep->packet_sz,
  724. channel->desired_mode,
  725. request->dma
  726. + request->actual,
  727. transfer_size))
  728. return;
  729. }
  730. #endif /* Mentor's DMA */
  731. fifo_count = request->length - request->actual;
  732. dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
  733. musb_ep->end_point.name,
  734. len, fifo_count,
  735. musb_ep->packet_sz);
  736. fifo_count = min_t(unsigned, len, fifo_count);
  737. #ifdef CONFIG_USB_TUSB_OMAP_DMA
  738. if (tusb_dma_omap() && is_buffer_mapped(req)) {
  739. struct dma_controller *c = musb->dma_controller;
  740. struct dma_channel *channel = musb_ep->dma;
  741. u32 dma_addr = request->dma + request->actual;
  742. int ret;
  743. ret = c->channel_program(channel,
  744. musb_ep->packet_sz,
  745. channel->desired_mode,
  746. dma_addr,
  747. fifo_count);
  748. if (ret)
  749. return;
  750. }
  751. #endif
  752. /*
  753. * Unmap the dma buffer back to cpu if dma channel
  754. * programming fails. This buffer is mapped if the
  755. * channel allocation is successful
  756. */
  757. if (is_buffer_mapped(req)) {
  758. unmap_dma_buffer(req, musb);
  759. /*
  760. * Clear DMAENAB and AUTOCLEAR for the
  761. * PIO mode transfer
  762. */
  763. csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
  764. musb_writew(epio, MUSB_RXCSR, csr);
  765. }
  766. musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
  767. (request->buf + request->actual));
  768. request->actual += fifo_count;
  769. /* REVISIT if we left anything in the fifo, flush
  770. * it and report -EOVERFLOW
  771. */
  772. /* ack the read! */
  773. csr |= MUSB_RXCSR_P_WZC_BITS;
  774. csr &= ~MUSB_RXCSR_RXPKTRDY;
  775. musb_writew(epio, MUSB_RXCSR, csr);
  776. }
  777. }
  778. /* reach the end or short packet detected */
  779. if (request->actual == request->length || len < musb_ep->packet_sz)
  780. musb_g_giveback(musb_ep, request, 0);
  781. }
  782. /*
  783. * Data ready for a request; called from IRQ
  784. */
  785. void musb_g_rx(struct musb *musb, u8 epnum)
  786. {
  787. u16 csr;
  788. struct musb_request *req;
  789. struct usb_request *request;
  790. void __iomem *mbase = musb->mregs;
  791. struct musb_ep *musb_ep;
  792. void __iomem *epio = musb->endpoints[epnum].regs;
  793. struct dma_channel *dma;
  794. struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
  795. if (hw_ep->is_shared_fifo)
  796. musb_ep = &hw_ep->ep_in;
  797. else
  798. musb_ep = &hw_ep->ep_out;
  799. musb_ep_select(mbase, epnum);
  800. req = next_request(musb_ep);
  801. if (!req)
  802. return;
  803. request = &req->request;
  804. csr = musb_readw(epio, MUSB_RXCSR);
  805. dma = is_dma_capable() ? musb_ep->dma : NULL;
  806. dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
  807. csr, dma ? " (dma)" : "", request);
  808. if (csr & MUSB_RXCSR_P_SENTSTALL) {
  809. csr |= MUSB_RXCSR_P_WZC_BITS;
  810. csr &= ~MUSB_RXCSR_P_SENTSTALL;
  811. musb_writew(epio, MUSB_RXCSR, csr);
  812. return;
  813. }
  814. if (csr & MUSB_RXCSR_P_OVERRUN) {
  815. /* csr |= MUSB_RXCSR_P_WZC_BITS; */
  816. csr &= ~MUSB_RXCSR_P_OVERRUN;
  817. musb_writew(epio, MUSB_RXCSR, csr);
  818. dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request);
  819. if (request->status == -EINPROGRESS)
  820. request->status = -EOVERFLOW;
  821. }
  822. if (csr & MUSB_RXCSR_INCOMPRX) {
  823. /* REVISIT not necessarily an error */
  824. dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name);
  825. }
  826. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  827. /* "should not happen"; likely RXPKTRDY pending for DMA */
  828. dev_dbg(musb->controller, "%s busy, csr %04x\n",
  829. musb_ep->end_point.name, csr);
  830. return;
  831. }
  832. if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
  833. csr &= ~(MUSB_RXCSR_AUTOCLEAR
  834. | MUSB_RXCSR_DMAENAB
  835. | MUSB_RXCSR_DMAMODE);
  836. musb_writew(epio, MUSB_RXCSR,
  837. MUSB_RXCSR_P_WZC_BITS | csr);
  838. request->actual += musb_ep->dma->actual_len;
  839. dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
  840. epnum, csr,
  841. musb_readw(epio, MUSB_RXCSR),
  842. musb_ep->dma->actual_len, request);
  843. #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
  844. defined(CONFIG_USB_UX500_DMA)
  845. /* Autoclear doesn't clear RxPktRdy for short packets */
  846. if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
  847. || (dma->actual_len
  848. & (musb_ep->packet_sz - 1))) {
  849. /* ack the read! */
  850. csr &= ~MUSB_RXCSR_RXPKTRDY;
  851. musb_writew(epio, MUSB_RXCSR, csr);
  852. }
  853. /* incomplete, and not short? wait for next IN packet */
  854. if ((request->actual < request->length)
  855. && (musb_ep->dma->actual_len
  856. == musb_ep->packet_sz)) {
  857. /* In double buffer case, continue to unload fifo if
  858. * there is Rx packet in FIFO.
  859. **/
  860. csr = musb_readw(epio, MUSB_RXCSR);
  861. if ((csr & MUSB_RXCSR_RXPKTRDY) &&
  862. hw_ep->rx_double_buffered)
  863. goto exit;
  864. return;
  865. }
  866. #endif
  867. musb_g_giveback(musb_ep, request, 0);
  868. /*
  869. * In the giveback function the MUSB lock is
  870. * released and acquired after sometime. During
  871. * this time period the INDEX register could get
  872. * changed by the gadget_queue function especially
  873. * on SMP systems. Reselect the INDEX to be sure
  874. * we are reading/modifying the right registers
  875. */
  876. musb_ep_select(mbase, epnum);
  877. req = next_request(musb_ep);
  878. if (!req)
  879. return;
  880. }
  881. #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
  882. defined(CONFIG_USB_UX500_DMA)
  883. exit:
  884. #endif
  885. /* Analyze request */
  886. rxstate(musb, req);
  887. }
  888. /* ------------------------------------------------------------ */
  889. static int musb_gadget_enable(struct usb_ep *ep,
  890. const struct usb_endpoint_descriptor *desc)
  891. {
  892. unsigned long flags;
  893. struct musb_ep *musb_ep;
  894. struct musb_hw_ep *hw_ep;
  895. void __iomem *regs;
  896. struct musb *musb;
  897. void __iomem *mbase;
  898. u8 epnum;
  899. u16 csr;
  900. unsigned tmp;
  901. int status = -EINVAL;
  902. if (!ep || !desc)
  903. return -EINVAL;
  904. musb_ep = to_musb_ep(ep);
  905. hw_ep = musb_ep->hw_ep;
  906. regs = hw_ep->regs;
  907. musb = musb_ep->musb;
  908. mbase = musb->mregs;
  909. epnum = musb_ep->current_epnum;
  910. spin_lock_irqsave(&musb->lock, flags);
  911. if (musb_ep->desc) {
  912. status = -EBUSY;
  913. goto fail;
  914. }
  915. musb_ep->type = usb_endpoint_type(desc);
  916. /* check direction and (later) maxpacket size against endpoint */
  917. if (usb_endpoint_num(desc) != epnum)
  918. goto fail;
  919. /* REVISIT this rules out high bandwidth periodic transfers */
  920. tmp = usb_endpoint_maxp(desc);
  921. if (tmp & ~0x07ff) {
  922. int ok;
  923. if (usb_endpoint_dir_in(desc))
  924. ok = musb->hb_iso_tx;
  925. else
  926. ok = musb->hb_iso_rx;
  927. if (!ok) {
  928. dev_dbg(musb->controller, "no support for high bandwidth ISO\n");
  929. goto fail;
  930. }
  931. musb_ep->hb_mult = (tmp >> 11) & 3;
  932. } else {
  933. musb_ep->hb_mult = 0;
  934. }
  935. musb_ep->packet_sz = tmp & 0x7ff;
  936. tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
  937. /* enable the interrupts for the endpoint, set the endpoint
  938. * packet size (or fail), set the mode, clear the fifo
  939. */
  940. musb_ep_select(mbase, epnum);
  941. if (usb_endpoint_dir_in(desc)) {
  942. u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
  943. if (hw_ep->is_shared_fifo)
  944. musb_ep->is_in = 1;
  945. if (!musb_ep->is_in)
  946. goto fail;
  947. if (tmp > hw_ep->max_packet_sz_tx) {
  948. dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
  949. goto fail;
  950. }
  951. int_txe |= (1 << epnum);
  952. musb_writew(mbase, MUSB_INTRTXE, int_txe);
  953. /* REVISIT if can_bulk_split(), use by updating "tmp";
  954. * likewise high bandwidth periodic tx
  955. */
  956. /* Set TXMAXP with the FIFO size of the endpoint
  957. * to disable double buffering mode.
  958. */
  959. if (musb->double_buffer_not_ok)
  960. musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
  961. else
  962. musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
  963. | (musb_ep->hb_mult << 11));
  964. csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
  965. if (musb_readw(regs, MUSB_TXCSR)
  966. & MUSB_TXCSR_FIFONOTEMPTY)
  967. csr |= MUSB_TXCSR_FLUSHFIFO;
  968. if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
  969. csr |= MUSB_TXCSR_P_ISO;
  970. /* set twice in case of double buffering */
  971. musb_writew(regs, MUSB_TXCSR, csr);
  972. /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
  973. musb_writew(regs, MUSB_TXCSR, csr);
  974. } else {
  975. u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
  976. if (hw_ep->is_shared_fifo)
  977. musb_ep->is_in = 0;
  978. if (musb_ep->is_in)
  979. goto fail;
  980. if (tmp > hw_ep->max_packet_sz_rx) {
  981. dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
  982. goto fail;
  983. }
  984. int_rxe |= (1 << epnum);
  985. musb_writew(mbase, MUSB_INTRRXE, int_rxe);
  986. /* REVISIT if can_bulk_combine() use by updating "tmp"
  987. * likewise high bandwidth periodic rx
  988. */
  989. /* Set RXMAXP with the FIFO size of the endpoint
  990. * to disable double buffering mode.
  991. */
  992. if (musb->double_buffer_not_ok)
  993. musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
  994. else
  995. musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
  996. | (musb_ep->hb_mult << 11));
  997. /* force shared fifo to OUT-only mode */
  998. if (hw_ep->is_shared_fifo) {
  999. csr = musb_readw(regs, MUSB_TXCSR);
  1000. csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
  1001. musb_writew(regs, MUSB_TXCSR, csr);
  1002. }
  1003. csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
  1004. if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
  1005. csr |= MUSB_RXCSR_P_ISO;
  1006. else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
  1007. csr |= MUSB_RXCSR_DISNYET;
  1008. /* set twice in case of double buffering */
  1009. musb_writew(regs, MUSB_RXCSR, csr);
  1010. musb_writew(regs, MUSB_RXCSR, csr);
  1011. }
  1012. /* NOTE: all the I/O code _should_ work fine without DMA, in case
  1013. * for some reason you run out of channels here.
  1014. */
  1015. if (is_dma_capable() && musb->dma_controller) {
  1016. struct dma_controller *c = musb->dma_controller;
  1017. musb_ep->dma = c->channel_alloc(c, hw_ep,
  1018. (desc->bEndpointAddress & USB_DIR_IN));
  1019. } else
  1020. musb_ep->dma = NULL;
  1021. musb_ep->desc = desc;
  1022. musb_ep->busy = 0;
  1023. musb_ep->wedged = 0;
  1024. status = 0;
  1025. pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
  1026. musb_driver_name, musb_ep->end_point.name,
  1027. ({ char *s; switch (musb_ep->type) {
  1028. case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
  1029. case USB_ENDPOINT_XFER_INT: s = "int"; break;
  1030. default: s = "iso"; break;
  1031. }; s; }),
  1032. musb_ep->is_in ? "IN" : "OUT",
  1033. musb_ep->dma ? "dma, " : "",
  1034. musb_ep->packet_sz);
  1035. schedule_work(&musb->irq_work);
  1036. fail:
  1037. spin_unlock_irqrestore(&musb->lock, flags);
  1038. return status;
  1039. }
  1040. /*
  1041. * Disable an endpoint flushing all requests queued.
  1042. */
  1043. static int musb_gadget_disable(struct usb_ep *ep)
  1044. {
  1045. unsigned long flags;
  1046. struct musb *musb;
  1047. u8 epnum;
  1048. struct musb_ep *musb_ep;
  1049. void __iomem *epio;
  1050. int status = 0;
  1051. musb_ep = to_musb_ep(ep);
  1052. musb = musb_ep->musb;
  1053. epnum = musb_ep->current_epnum;
  1054. epio = musb->endpoints[epnum].regs;
  1055. spin_lock_irqsave(&musb->lock, flags);
  1056. musb_ep_select(musb->mregs, epnum);
  1057. /* zero the endpoint sizes */
  1058. if (musb_ep->is_in) {
  1059. u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
  1060. int_txe &= ~(1 << epnum);
  1061. musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
  1062. musb_writew(epio, MUSB_TXMAXP, 0);
  1063. } else {
  1064. u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
  1065. int_rxe &= ~(1 << epnum);
  1066. musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
  1067. musb_writew(epio, MUSB_RXMAXP, 0);
  1068. }
  1069. musb_ep->desc = NULL;
  1070. #ifndef __UBOOT__
  1071. musb_ep->end_point.desc = NULL;
  1072. #endif
  1073. /* abort all pending DMA and requests */
  1074. nuke(musb_ep, -ESHUTDOWN);
  1075. schedule_work(&musb->irq_work);
  1076. spin_unlock_irqrestore(&(musb->lock), flags);
  1077. dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
  1078. return status;
  1079. }
  1080. /*
  1081. * Allocate a request for an endpoint.
  1082. * Reused by ep0 code.
  1083. */
  1084. struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
  1085. {
  1086. struct musb_ep *musb_ep = to_musb_ep(ep);
  1087. struct musb *musb = musb_ep->musb;
  1088. struct musb_request *request = NULL;
  1089. request = kzalloc(sizeof *request, gfp_flags);
  1090. if (!request) {
  1091. dev_dbg(musb->controller, "not enough memory\n");
  1092. return NULL;
  1093. }
  1094. request->request.dma = DMA_ADDR_INVALID;
  1095. request->epnum = musb_ep->current_epnum;
  1096. request->ep = musb_ep;
  1097. return &request->request;
  1098. }
  1099. /*
  1100. * Free a request
  1101. * Reused by ep0 code.
  1102. */
  1103. void musb_free_request(struct usb_ep *ep, struct usb_request *req)
  1104. {
  1105. kfree(to_musb_request(req));
  1106. }
  1107. static LIST_HEAD(buffers);
  1108. struct free_record {
  1109. struct list_head list;
  1110. struct device *dev;
  1111. unsigned bytes;
  1112. dma_addr_t dma;
  1113. };
  1114. /*
  1115. * Context: controller locked, IRQs blocked.
  1116. */
  1117. void musb_ep_restart(struct musb *musb, struct musb_request *req)
  1118. {
  1119. dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
  1120. req->tx ? "TX/IN" : "RX/OUT",
  1121. &req->request, req->request.length, req->epnum);
  1122. musb_ep_select(musb->mregs, req->epnum);
  1123. if (req->tx)
  1124. txstate(musb, req);
  1125. else
  1126. rxstate(musb, req);
  1127. }
  1128. static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
  1129. gfp_t gfp_flags)
  1130. {
  1131. struct musb_ep *musb_ep;
  1132. struct musb_request *request;
  1133. struct musb *musb;
  1134. int status = 0;
  1135. unsigned long lockflags;
  1136. if (!ep || !req)
  1137. return -EINVAL;
  1138. if (!req->buf)
  1139. return -ENODATA;
  1140. musb_ep = to_musb_ep(ep);
  1141. musb = musb_ep->musb;
  1142. request = to_musb_request(req);
  1143. request->musb = musb;
  1144. if (request->ep != musb_ep)
  1145. return -EINVAL;
  1146. dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
  1147. /* request is mine now... */
  1148. request->request.actual = 0;
  1149. request->request.status = -EINPROGRESS;
  1150. request->epnum = musb_ep->current_epnum;
  1151. request->tx = musb_ep->is_in;
  1152. map_dma_buffer(request, musb, musb_ep);
  1153. spin_lock_irqsave(&musb->lock, lockflags);
  1154. /* don't queue if the ep is down */
  1155. if (!musb_ep->desc) {
  1156. dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
  1157. req, ep->name, "disabled");
  1158. status = -ESHUTDOWN;
  1159. goto cleanup;
  1160. }
  1161. /* add request to the list */
  1162. list_add_tail(&request->list, &musb_ep->req_list);
  1163. /* it this is the head of the queue, start i/o ... */
  1164. if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
  1165. musb_ep_restart(musb, request);
  1166. cleanup:
  1167. spin_unlock_irqrestore(&musb->lock, lockflags);
  1168. return status;
  1169. }
  1170. static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
  1171. {
  1172. struct musb_ep *musb_ep = to_musb_ep(ep);
  1173. struct musb_request *req = to_musb_request(request);
  1174. struct musb_request *r;
  1175. unsigned long flags;
  1176. int status = 0;
  1177. struct musb *musb = musb_ep->musb;
  1178. if (!ep || !request || to_musb_request(request)->ep != musb_ep)
  1179. return -EINVAL;
  1180. spin_lock_irqsave(&musb->lock, flags);
  1181. list_for_each_entry(r, &musb_ep->req_list, list) {
  1182. if (r == req)
  1183. break;
  1184. }
  1185. if (r != req) {
  1186. dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
  1187. status = -EINVAL;
  1188. goto done;
  1189. }
  1190. /* if the hardware doesn't have the request, easy ... */
  1191. if (musb_ep->req_list.next != &req->list || musb_ep->busy)
  1192. musb_g_giveback(musb_ep, request, -ECONNRESET);
  1193. /* ... else abort the dma transfer ... */
  1194. else if (is_dma_capable() && musb_ep->dma) {
  1195. struct dma_controller *c = musb->dma_controller;
  1196. musb_ep_select(musb->mregs, musb_ep->current_epnum);
  1197. if (c->channel_abort)
  1198. status = c->channel_abort(musb_ep->dma);
  1199. else
  1200. status = -EBUSY;
  1201. if (status == 0)
  1202. musb_g_giveback(musb_ep, request, -ECONNRESET);
  1203. } else {
  1204. /* NOTE: by sticking to easily tested hardware/driver states,
  1205. * we leave counting of in-flight packets imprecise.
  1206. */
  1207. musb_g_giveback(musb_ep, request, -ECONNRESET);
  1208. }
  1209. done:
  1210. spin_unlock_irqrestore(&musb->lock, flags);
  1211. return status;
  1212. }
  1213. /*
  1214. * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
  1215. * data but will queue requests.
  1216. *
  1217. * exported to ep0 code
  1218. */
  1219. static int musb_gadget_set_halt(struct usb_ep *ep, int value)
  1220. {
  1221. struct musb_ep *musb_ep = to_musb_ep(ep);
  1222. u8 epnum = musb_ep->current_epnum;
  1223. struct musb *musb = musb_ep->musb;
  1224. void __iomem *epio = musb->endpoints[epnum].regs;
  1225. void __iomem *mbase;
  1226. unsigned long flags;
  1227. u16 csr;
  1228. struct musb_request *request;
  1229. int status = 0;
  1230. if (!ep)
  1231. return -EINVAL;
  1232. mbase = musb->mregs;
  1233. spin_lock_irqsave(&musb->lock, flags);
  1234. if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
  1235. status = -EINVAL;
  1236. goto done;
  1237. }
  1238. musb_ep_select(mbase, epnum);
  1239. request = next_request(musb_ep);
  1240. if (value) {
  1241. if (request) {
  1242. dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
  1243. ep->name);
  1244. status = -EAGAIN;
  1245. goto done;
  1246. }
  1247. /* Cannot portably stall with non-empty FIFO */
  1248. if (musb_ep->is_in) {
  1249. csr = musb_readw(epio, MUSB_TXCSR);
  1250. if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  1251. dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
  1252. status = -EAGAIN;
  1253. goto done;
  1254. }
  1255. }
  1256. } else
  1257. musb_ep->wedged = 0;
  1258. /* set/clear the stall and toggle bits */
  1259. dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
  1260. if (musb_ep->is_in) {
  1261. csr = musb_readw(epio, MUSB_TXCSR);
  1262. csr |= MUSB_TXCSR_P_WZC_BITS
  1263. | MUSB_TXCSR_CLRDATATOG;
  1264. if (value)
  1265. csr |= MUSB_TXCSR_P_SENDSTALL;
  1266. else
  1267. csr &= ~(MUSB_TXCSR_P_SENDSTALL
  1268. | MUSB_TXCSR_P_SENTSTALL);
  1269. csr &= ~MUSB_TXCSR_TXPKTRDY;
  1270. musb_writew(epio, MUSB_TXCSR, csr);
  1271. } else {
  1272. csr = musb_readw(epio, MUSB_RXCSR);
  1273. csr |= MUSB_RXCSR_P_WZC_BITS
  1274. | MUSB_RXCSR_FLUSHFIFO
  1275. | MUSB_RXCSR_CLRDATATOG;
  1276. if (value)
  1277. csr |= MUSB_RXCSR_P_SENDSTALL;
  1278. else
  1279. csr &= ~(MUSB_RXCSR_P_SENDSTALL
  1280. | MUSB_RXCSR_P_SENTSTALL);
  1281. musb_writew(epio, MUSB_RXCSR, csr);
  1282. }
  1283. /* maybe start the first request in the queue */
  1284. if (!musb_ep->busy && !value && request) {
  1285. dev_dbg(musb->controller, "restarting the request\n");
  1286. musb_ep_restart(musb, request);
  1287. }
  1288. done:
  1289. spin_unlock_irqrestore(&musb->lock, flags);
  1290. return status;
  1291. }
  1292. #ifndef __UBOOT__
  1293. /*
  1294. * Sets the halt feature with the clear requests ignored
  1295. */
  1296. static int musb_gadget_set_wedge(struct usb_ep *ep)
  1297. {
  1298. struct musb_ep *musb_ep = to_musb_ep(ep);
  1299. if (!ep)
  1300. return -EINVAL;
  1301. musb_ep->wedged = 1;
  1302. return usb_ep_set_halt(ep);
  1303. }
  1304. #endif
  1305. static int musb_gadget_fifo_status(struct usb_ep *ep)
  1306. {
  1307. struct musb_ep *musb_ep = to_musb_ep(ep);
  1308. void __iomem *epio = musb_ep->hw_ep->regs;
  1309. int retval = -EINVAL;
  1310. if (musb_ep->desc && !musb_ep->is_in) {
  1311. struct musb *musb = musb_ep->musb;
  1312. int epnum = musb_ep->current_epnum;
  1313. void __iomem *mbase = musb->mregs;
  1314. unsigned long flags;
  1315. spin_lock_irqsave(&musb->lock, flags);
  1316. musb_ep_select(mbase, epnum);
  1317. /* FIXME return zero unless RXPKTRDY is set */
  1318. retval = musb_readw(epio, MUSB_RXCOUNT);
  1319. spin_unlock_irqrestore(&musb->lock, flags);
  1320. }
  1321. return retval;
  1322. }
  1323. static void musb_gadget_fifo_flush(struct usb_ep *ep)
  1324. {
  1325. struct musb_ep *musb_ep = to_musb_ep(ep);
  1326. struct musb *musb = musb_ep->musb;
  1327. u8 epnum = musb_ep->current_epnum;
  1328. void __iomem *epio = musb->endpoints[epnum].regs;
  1329. void __iomem *mbase;
  1330. unsigned long flags;
  1331. u16 csr, int_txe;
  1332. mbase = musb->mregs;
  1333. spin_lock_irqsave(&musb->lock, flags);
  1334. musb_ep_select(mbase, (u8) epnum);
  1335. /* disable interrupts */
  1336. int_txe = musb_readw(mbase, MUSB_INTRTXE);
  1337. musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
  1338. if (musb_ep->is_in) {
  1339. csr = musb_readw(epio, MUSB_TXCSR);
  1340. if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  1341. csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
  1342. /*
  1343. * Setting both TXPKTRDY and FLUSHFIFO makes controller
  1344. * to interrupt current FIFO loading, but not flushing
  1345. * the already loaded ones.
  1346. */
  1347. csr &= ~MUSB_TXCSR_TXPKTRDY;
  1348. musb_writew(epio, MUSB_TXCSR, csr);
  1349. /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
  1350. musb_writew(epio, MUSB_TXCSR, csr);
  1351. }
  1352. } else {
  1353. csr = musb_readw(epio, MUSB_RXCSR);
  1354. csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
  1355. musb_writew(epio, MUSB_RXCSR, csr);
  1356. musb_writew(epio, MUSB_RXCSR, csr);
  1357. }
  1358. /* re-enable interrupt */
  1359. musb_writew(mbase, MUSB_INTRTXE, int_txe);
  1360. spin_unlock_irqrestore(&musb->lock, flags);
  1361. }
  1362. static const struct usb_ep_ops musb_ep_ops = {
  1363. .enable = musb_gadget_enable,
  1364. .disable = musb_gadget_disable,
  1365. .alloc_request = musb_alloc_request,
  1366. .free_request = musb_free_request,
  1367. .queue = musb_gadget_queue,
  1368. .dequeue = musb_gadget_dequeue,
  1369. .set_halt = musb_gadget_set_halt,
  1370. #ifndef __UBOOT__
  1371. .set_wedge = musb_gadget_set_wedge,
  1372. #endif
  1373. .fifo_status = musb_gadget_fifo_status,
  1374. .fifo_flush = musb_gadget_fifo_flush
  1375. };
  1376. /* ----------------------------------------------------------------------- */
  1377. static int musb_gadget_get_frame(struct usb_gadget *gadget)
  1378. {
  1379. struct musb *musb = gadget_to_musb(gadget);
  1380. return (int)musb_readw(musb->mregs, MUSB_FRAME);
  1381. }
  1382. static int musb_gadget_wakeup(struct usb_gadget *gadget)
  1383. {
  1384. #ifndef __UBOOT__
  1385. struct musb *musb = gadget_to_musb(gadget);
  1386. void __iomem *mregs = musb->mregs;
  1387. unsigned long flags;
  1388. int status = -EINVAL;
  1389. u8 power, devctl;
  1390. int retries;
  1391. spin_lock_irqsave(&musb->lock, flags);
  1392. switch (musb->xceiv->state) {
  1393. case OTG_STATE_B_PERIPHERAL:
  1394. /* NOTE: OTG state machine doesn't include B_SUSPENDED;
  1395. * that's part of the standard usb 1.1 state machine, and
  1396. * doesn't affect OTG transitions.
  1397. */
  1398. if (musb->may_wakeup && musb->is_suspended)
  1399. break;
  1400. goto done;
  1401. case OTG_STATE_B_IDLE:
  1402. /* Start SRP ... OTG not required. */
  1403. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1404. dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
  1405. devctl |= MUSB_DEVCTL_SESSION;
  1406. musb_writeb(mregs, MUSB_DEVCTL, devctl);
  1407. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1408. retries = 100;
  1409. while (!(devctl & MUSB_DEVCTL_SESSION)) {
  1410. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1411. if (retries-- < 1)
  1412. break;
  1413. }
  1414. retries = 10000;
  1415. while (devctl & MUSB_DEVCTL_SESSION) {
  1416. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1417. if (retries-- < 1)
  1418. break;
  1419. }
  1420. spin_unlock_irqrestore(&musb->lock, flags);
  1421. otg_start_srp(musb->xceiv->otg);
  1422. spin_lock_irqsave(&musb->lock, flags);
  1423. /* Block idling for at least 1s */
  1424. musb_platform_try_idle(musb,
  1425. jiffies + msecs_to_jiffies(1 * HZ));
  1426. status = 0;
  1427. goto done;
  1428. default:
  1429. dev_dbg(musb->controller, "Unhandled wake: %s\n",
  1430. otg_state_string(musb->xceiv->state));
  1431. goto done;
  1432. }
  1433. status = 0;
  1434. power = musb_readb(mregs, MUSB_POWER);
  1435. power |= MUSB_POWER_RESUME;
  1436. musb_writeb(mregs, MUSB_POWER, power);
  1437. dev_dbg(musb->controller, "issue wakeup\n");
  1438. /* FIXME do this next chunk in a timer callback, no udelay */
  1439. mdelay(2);
  1440. power = musb_readb(mregs, MUSB_POWER);
  1441. power &= ~MUSB_POWER_RESUME;
  1442. musb_writeb(mregs, MUSB_POWER, power);
  1443. done:
  1444. spin_unlock_irqrestore(&musb->lock, flags);
  1445. return status;
  1446. #else
  1447. return 0;
  1448. #endif
  1449. }
  1450. static int
  1451. musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
  1452. {
  1453. struct musb *musb = gadget_to_musb(gadget);
  1454. musb->is_self_powered = !!is_selfpowered;
  1455. return 0;
  1456. }
  1457. static void musb_pullup(struct musb *musb, int is_on)
  1458. {
  1459. u8 power;
  1460. power = musb_readb(musb->mregs, MUSB_POWER);
  1461. if (is_on)
  1462. power |= MUSB_POWER_SOFTCONN;
  1463. else
  1464. power &= ~MUSB_POWER_SOFTCONN;
  1465. /* FIXME if on, HdrcStart; if off, HdrcStop */
  1466. dev_dbg(musb->controller, "gadget D+ pullup %s\n",
  1467. is_on ? "on" : "off");
  1468. musb_writeb(musb->mregs, MUSB_POWER, power);
  1469. }
  1470. #if 0
  1471. static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
  1472. {
  1473. dev_dbg(musb->controller, "<= %s =>\n", __func__);
  1474. /*
  1475. * FIXME iff driver's softconnect flag is set (as it is during probe,
  1476. * though that can clear it), just musb_pullup().
  1477. */
  1478. return -EINVAL;
  1479. }
  1480. #endif
  1481. static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  1482. {
  1483. #ifndef __UBOOT__
  1484. struct musb *musb = gadget_to_musb(gadget);
  1485. if (!musb->xceiv->set_power)
  1486. return -EOPNOTSUPP;
  1487. return usb_phy_set_power(musb->xceiv, mA);
  1488. #else
  1489. return 0;
  1490. #endif
  1491. }
  1492. static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
  1493. {
  1494. struct musb *musb = gadget_to_musb(gadget);
  1495. unsigned long flags;
  1496. is_on = !!is_on;
  1497. pm_runtime_get_sync(musb->controller);
  1498. /* NOTE: this assumes we are sensing vbus; we'd rather
  1499. * not pullup unless the B-session is active.
  1500. */
  1501. spin_lock_irqsave(&musb->lock, flags);
  1502. if (is_on != musb->softconnect) {
  1503. musb->softconnect = is_on;
  1504. musb_pullup(musb, is_on);
  1505. }
  1506. spin_unlock_irqrestore(&musb->lock, flags);
  1507. pm_runtime_put(musb->controller);
  1508. return 0;
  1509. }
  1510. #ifndef __UBOOT__
  1511. static int musb_gadget_start(struct usb_gadget *g,
  1512. struct usb_gadget_driver *driver);
  1513. static int musb_gadget_stop(struct usb_gadget *g,
  1514. struct usb_gadget_driver *driver);
  1515. #endif
  1516. static const struct usb_gadget_ops musb_gadget_operations = {
  1517. .get_frame = musb_gadget_get_frame,
  1518. .wakeup = musb_gadget_wakeup,
  1519. .set_selfpowered = musb_gadget_set_self_powered,
  1520. /* .vbus_session = musb_gadget_vbus_session, */
  1521. .vbus_draw = musb_gadget_vbus_draw,
  1522. .pullup = musb_gadget_pullup,
  1523. #ifndef __UBOOT__
  1524. .udc_start = musb_gadget_start,
  1525. .udc_stop = musb_gadget_stop,
  1526. #endif
  1527. };
  1528. /* ----------------------------------------------------------------------- */
  1529. /* Registration */
  1530. /* Only this registration code "knows" the rule (from USB standards)
  1531. * about there being only one external upstream port. It assumes
  1532. * all peripheral ports are external...
  1533. */
  1534. #ifndef __UBOOT__
  1535. static void musb_gadget_release(struct device *dev)
  1536. {
  1537. /* kref_put(WHAT) */
  1538. dev_dbg(dev, "%s\n", __func__);
  1539. }
  1540. #endif
  1541. static void __devinit
  1542. init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
  1543. {
  1544. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1545. memset(ep, 0, sizeof *ep);
  1546. ep->current_epnum = epnum;
  1547. ep->musb = musb;
  1548. ep->hw_ep = hw_ep;
  1549. ep->is_in = is_in;
  1550. INIT_LIST_HEAD(&ep->req_list);
  1551. sprintf(ep->name, "ep%d%s", epnum,
  1552. (!epnum || hw_ep->is_shared_fifo) ? "" : (
  1553. is_in ? "in" : "out"));
  1554. ep->end_point.name = ep->name;
  1555. INIT_LIST_HEAD(&ep->end_point.ep_list);
  1556. if (!epnum) {
  1557. ep->end_point.maxpacket = 64;
  1558. ep->end_point.ops = &musb_g_ep0_ops;
  1559. musb->g.ep0 = &ep->end_point;
  1560. } else {
  1561. if (is_in)
  1562. ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
  1563. else
  1564. ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
  1565. ep->end_point.ops = &musb_ep_ops;
  1566. list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
  1567. }
  1568. }
  1569. /*
  1570. * Initialize the endpoints exposed to peripheral drivers, with backlinks
  1571. * to the rest of the driver state.
  1572. */
  1573. static inline void __devinit musb_g_init_endpoints(struct musb *musb)
  1574. {
  1575. u8 epnum;
  1576. struct musb_hw_ep *hw_ep;
  1577. unsigned count = 0;
  1578. /* initialize endpoint list just once */
  1579. INIT_LIST_HEAD(&(musb->g.ep_list));
  1580. for (epnum = 0, hw_ep = musb->endpoints;
  1581. epnum < musb->nr_endpoints;
  1582. epnum++, hw_ep++) {
  1583. if (hw_ep->is_shared_fifo /* || !epnum */) {
  1584. init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
  1585. count++;
  1586. } else {
  1587. if (hw_ep->max_packet_sz_tx) {
  1588. init_peripheral_ep(musb, &hw_ep->ep_in,
  1589. epnum, 1);
  1590. count++;
  1591. }
  1592. if (hw_ep->max_packet_sz_rx) {
  1593. init_peripheral_ep(musb, &hw_ep->ep_out,
  1594. epnum, 0);
  1595. count++;
  1596. }
  1597. }
  1598. }
  1599. }
  1600. /* called once during driver setup to initialize and link into
  1601. * the driver model; memory is zeroed.
  1602. */
  1603. int __devinit musb_gadget_setup(struct musb *musb)
  1604. {
  1605. int status;
  1606. /* REVISIT minor race: if (erroneously) setting up two
  1607. * musb peripherals at the same time, only the bus lock
  1608. * is probably held.
  1609. */
  1610. musb->g.ops = &musb_gadget_operations;
  1611. #ifndef __UBOOT__
  1612. musb->g.max_speed = USB_SPEED_HIGH;
  1613. #endif
  1614. musb->g.speed = USB_SPEED_UNKNOWN;
  1615. #ifndef __UBOOT__
  1616. /* this "gadget" abstracts/virtualizes the controller */
  1617. dev_set_name(&musb->g.dev, "gadget");
  1618. musb->g.dev.parent = musb->controller;
  1619. musb->g.dev.dma_mask = musb->controller->dma_mask;
  1620. musb->g.dev.release = musb_gadget_release;
  1621. #endif
  1622. musb->g.name = musb_driver_name;
  1623. #ifndef __UBOOT__
  1624. if (is_otg_enabled(musb))
  1625. musb->g.is_otg = 1;
  1626. #endif
  1627. musb_g_init_endpoints(musb);
  1628. musb->is_active = 0;
  1629. musb_platform_try_idle(musb, 0);
  1630. #ifndef __UBOOT__
  1631. status = device_register(&musb->g.dev);
  1632. if (status != 0) {
  1633. put_device(&musb->g.dev);
  1634. return status;
  1635. }
  1636. status = usb_add_gadget_udc(musb->controller, &musb->g);
  1637. if (status)
  1638. goto err;
  1639. #endif
  1640. return 0;
  1641. #ifndef __UBOOT__
  1642. err:
  1643. musb->g.dev.parent = NULL;
  1644. device_unregister(&musb->g.dev);
  1645. return status;
  1646. #endif
  1647. }
  1648. void musb_gadget_cleanup(struct musb *musb)
  1649. {
  1650. #ifndef __UBOOT__
  1651. usb_del_gadget_udc(&musb->g);
  1652. if (musb->g.dev.parent)
  1653. device_unregister(&musb->g.dev);
  1654. #endif
  1655. }
  1656. /*
  1657. * Register the gadget driver. Used by gadget drivers when
  1658. * registering themselves with the controller.
  1659. *
  1660. * -EINVAL something went wrong (not driver)
  1661. * -EBUSY another gadget is already using the controller
  1662. * -ENOMEM no memory to perform the operation
  1663. *
  1664. * @param driver the gadget driver
  1665. * @return <0 if error, 0 if everything is fine
  1666. */
  1667. #ifndef __UBOOT__
  1668. static int musb_gadget_start(struct usb_gadget *g,
  1669. struct usb_gadget_driver *driver)
  1670. #else
  1671. int musb_gadget_start(struct usb_gadget *g,
  1672. struct usb_gadget_driver *driver)
  1673. #endif
  1674. {
  1675. struct musb *musb = gadget_to_musb(g);
  1676. #ifndef __UBOOT__
  1677. struct usb_otg *otg = musb->xceiv->otg;
  1678. #endif
  1679. unsigned long flags;
  1680. int retval = -EINVAL;
  1681. #ifndef __UBOOT__
  1682. if (driver->max_speed < USB_SPEED_HIGH)
  1683. goto err0;
  1684. #endif
  1685. pm_runtime_get_sync(musb->controller);
  1686. #ifndef __UBOOT__
  1687. dev_dbg(musb->controller, "registering driver %s\n", driver->function);
  1688. #endif
  1689. musb->softconnect = 0;
  1690. musb->gadget_driver = driver;
  1691. spin_lock_irqsave(&musb->lock, flags);
  1692. musb->is_active = 1;
  1693. #ifndef __UBOOT__
  1694. otg_set_peripheral(otg, &musb->g);
  1695. musb->xceiv->state = OTG_STATE_B_IDLE;
  1696. /*
  1697. * FIXME this ignores the softconnect flag. Drivers are
  1698. * allowed hold the peripheral inactive until for example
  1699. * userspace hooks up printer hardware or DSP codecs, so
  1700. * hosts only see fully functional devices.
  1701. */
  1702. if (!is_otg_enabled(musb))
  1703. #endif
  1704. musb_start(musb);
  1705. spin_unlock_irqrestore(&musb->lock, flags);
  1706. #ifndef __UBOOT__
  1707. if (is_otg_enabled(musb)) {
  1708. struct usb_hcd *hcd = musb_to_hcd(musb);
  1709. dev_dbg(musb->controller, "OTG startup...\n");
  1710. /* REVISIT: funcall to other code, which also
  1711. * handles power budgeting ... this way also
  1712. * ensures HdrcStart is indirectly called.
  1713. */
  1714. retval = usb_add_hcd(musb_to_hcd(musb), 0, 0);
  1715. if (retval < 0) {
  1716. dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
  1717. goto err2;
  1718. }
  1719. if ((musb->xceiv->last_event == USB_EVENT_ID)
  1720. && otg->set_vbus)
  1721. otg_set_vbus(otg, 1);
  1722. hcd->self.uses_pio_for_control = 1;
  1723. }
  1724. if (musb->xceiv->last_event == USB_EVENT_NONE)
  1725. pm_runtime_put(musb->controller);
  1726. #endif
  1727. return 0;
  1728. #ifndef __UBOOT__
  1729. err2:
  1730. if (!is_otg_enabled(musb))
  1731. musb_stop(musb);
  1732. err0:
  1733. return retval;
  1734. #endif
  1735. }
  1736. #ifndef __UBOOT__
  1737. static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
  1738. {
  1739. int i;
  1740. struct musb_hw_ep *hw_ep;
  1741. /* don't disconnect if it's not connected */
  1742. if (musb->g.speed == USB_SPEED_UNKNOWN)
  1743. driver = NULL;
  1744. else
  1745. musb->g.speed = USB_SPEED_UNKNOWN;
  1746. /* deactivate the hardware */
  1747. if (musb->softconnect) {
  1748. musb->softconnect = 0;
  1749. musb_pullup(musb, 0);
  1750. }
  1751. musb_stop(musb);
  1752. /* killing any outstanding requests will quiesce the driver;
  1753. * then report disconnect
  1754. */
  1755. if (driver) {
  1756. for (i = 0, hw_ep = musb->endpoints;
  1757. i < musb->nr_endpoints;
  1758. i++, hw_ep++) {
  1759. musb_ep_select(musb->mregs, i);
  1760. if (hw_ep->is_shared_fifo /* || !epnum */) {
  1761. nuke(&hw_ep->ep_in, -ESHUTDOWN);
  1762. } else {
  1763. if (hw_ep->max_packet_sz_tx)
  1764. nuke(&hw_ep->ep_in, -ESHUTDOWN);
  1765. if (hw_ep->max_packet_sz_rx)
  1766. nuke(&hw_ep->ep_out, -ESHUTDOWN);
  1767. }
  1768. }
  1769. }
  1770. }
  1771. /*
  1772. * Unregister the gadget driver. Used by gadget drivers when
  1773. * unregistering themselves from the controller.
  1774. *
  1775. * @param driver the gadget driver to unregister
  1776. */
  1777. static int musb_gadget_stop(struct usb_gadget *g,
  1778. struct usb_gadget_driver *driver)
  1779. {
  1780. struct musb *musb = gadget_to_musb(g);
  1781. unsigned long flags;
  1782. if (musb->xceiv->last_event == USB_EVENT_NONE)
  1783. pm_runtime_get_sync(musb->controller);
  1784. /*
  1785. * REVISIT always use otg_set_peripheral() here too;
  1786. * this needs to shut down the OTG engine.
  1787. */
  1788. spin_lock_irqsave(&musb->lock, flags);
  1789. musb_hnp_stop(musb);
  1790. (void) musb_gadget_vbus_draw(&musb->g, 0);
  1791. musb->xceiv->state = OTG_STATE_UNDEFINED;
  1792. stop_activity(musb, driver);
  1793. otg_set_peripheral(musb->xceiv->otg, NULL);
  1794. dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
  1795. musb->is_active = 0;
  1796. musb_platform_try_idle(musb, 0);
  1797. spin_unlock_irqrestore(&musb->lock, flags);
  1798. if (is_otg_enabled(musb)) {
  1799. usb_remove_hcd(musb_to_hcd(musb));
  1800. /* FIXME we need to be able to register another
  1801. * gadget driver here and have everything work;
  1802. * that currently misbehaves.
  1803. */
  1804. }
  1805. if (!is_otg_enabled(musb))
  1806. musb_stop(musb);
  1807. pm_runtime_put(musb->controller);
  1808. return 0;
  1809. }
  1810. #endif
  1811. /* ----------------------------------------------------------------------- */
  1812. /* lifecycle operations called through plat_uds.c */
  1813. void musb_g_resume(struct musb *musb)
  1814. {
  1815. #ifndef __UBOOT__
  1816. musb->is_suspended = 0;
  1817. switch (musb->xceiv->state) {
  1818. case OTG_STATE_B_IDLE:
  1819. break;
  1820. case OTG_STATE_B_WAIT_ACON:
  1821. case OTG_STATE_B_PERIPHERAL:
  1822. musb->is_active = 1;
  1823. if (musb->gadget_driver && musb->gadget_driver->resume) {
  1824. spin_unlock(&musb->lock);
  1825. musb->gadget_driver->resume(&musb->g);
  1826. spin_lock(&musb->lock);
  1827. }
  1828. break;
  1829. default:
  1830. WARNING("unhandled RESUME transition (%s)\n",
  1831. otg_state_string(musb->xceiv->state));
  1832. }
  1833. #endif
  1834. }
  1835. /* called when SOF packets stop for 3+ msec */
  1836. void musb_g_suspend(struct musb *musb)
  1837. {
  1838. #ifndef __UBOOT__
  1839. u8 devctl;
  1840. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1841. dev_dbg(musb->controller, "devctl %02x\n", devctl);
  1842. switch (musb->xceiv->state) {
  1843. case OTG_STATE_B_IDLE:
  1844. if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
  1845. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  1846. break;
  1847. case OTG_STATE_B_PERIPHERAL:
  1848. musb->is_suspended = 1;
  1849. if (musb->gadget_driver && musb->gadget_driver->suspend) {
  1850. spin_unlock(&musb->lock);
  1851. musb->gadget_driver->suspend(&musb->g);
  1852. spin_lock(&musb->lock);
  1853. }
  1854. break;
  1855. default:
  1856. /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
  1857. * A_PERIPHERAL may need care too
  1858. */
  1859. WARNING("unhandled SUSPEND transition (%s)\n",
  1860. otg_state_string(musb->xceiv->state));
  1861. }
  1862. #endif
  1863. }
  1864. /* Called during SRP */
  1865. void musb_g_wakeup(struct musb *musb)
  1866. {
  1867. musb_gadget_wakeup(&musb->g);
  1868. }
  1869. /* called when VBUS drops below session threshold, and in other cases */
  1870. void musb_g_disconnect(struct musb *musb)
  1871. {
  1872. void __iomem *mregs = musb->mregs;
  1873. u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
  1874. dev_dbg(musb->controller, "devctl %02x\n", devctl);
  1875. /* clear HR */
  1876. musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
  1877. /* don't draw vbus until new b-default session */
  1878. (void) musb_gadget_vbus_draw(&musb->g, 0);
  1879. musb->g.speed = USB_SPEED_UNKNOWN;
  1880. if (musb->gadget_driver && musb->gadget_driver->disconnect) {
  1881. spin_unlock(&musb->lock);
  1882. musb->gadget_driver->disconnect(&musb->g);
  1883. spin_lock(&musb->lock);
  1884. }
  1885. #ifndef __UBOOT__
  1886. switch (musb->xceiv->state) {
  1887. default:
  1888. dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
  1889. otg_state_string(musb->xceiv->state));
  1890. musb->xceiv->state = OTG_STATE_A_IDLE;
  1891. MUSB_HST_MODE(musb);
  1892. break;
  1893. case OTG_STATE_A_PERIPHERAL:
  1894. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  1895. MUSB_HST_MODE(musb);
  1896. break;
  1897. case OTG_STATE_B_WAIT_ACON:
  1898. case OTG_STATE_B_HOST:
  1899. case OTG_STATE_B_PERIPHERAL:
  1900. case OTG_STATE_B_IDLE:
  1901. musb->xceiv->state = OTG_STATE_B_IDLE;
  1902. break;
  1903. case OTG_STATE_B_SRP_INIT:
  1904. break;
  1905. }
  1906. #endif
  1907. musb->is_active = 0;
  1908. }
  1909. void musb_g_reset(struct musb *musb)
  1910. __releases(musb->lock)
  1911. __acquires(musb->lock)
  1912. {
  1913. void __iomem *mbase = musb->mregs;
  1914. u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
  1915. u8 power;
  1916. #ifndef __UBOOT__
  1917. dev_dbg(musb->controller, "<== %s addr=%x driver '%s'\n",
  1918. (devctl & MUSB_DEVCTL_BDEVICE)
  1919. ? "B-Device" : "A-Device",
  1920. musb_readb(mbase, MUSB_FADDR),
  1921. musb->gadget_driver
  1922. ? musb->gadget_driver->driver.name
  1923. : NULL
  1924. );
  1925. #endif
  1926. /* report disconnect, if we didn't already (flushing EP state) */
  1927. if (musb->g.speed != USB_SPEED_UNKNOWN)
  1928. musb_g_disconnect(musb);
  1929. /* clear HR */
  1930. else if (devctl & MUSB_DEVCTL_HR)
  1931. musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
  1932. /* what speed did we negotiate? */
  1933. power = musb_readb(mbase, MUSB_POWER);
  1934. musb->g.speed = (power & MUSB_POWER_HSMODE)
  1935. ? USB_SPEED_HIGH : USB_SPEED_FULL;
  1936. /* start in USB_STATE_DEFAULT */
  1937. musb->is_active = 1;
  1938. musb->is_suspended = 0;
  1939. MUSB_DEV_MODE(musb);
  1940. musb->address = 0;
  1941. musb->ep0_state = MUSB_EP0_STAGE_SETUP;
  1942. musb->may_wakeup = 0;
  1943. musb->g.b_hnp_enable = 0;
  1944. musb->g.a_alt_hnp_support = 0;
  1945. musb->g.a_hnp_support = 0;
  1946. #ifndef __UBOOT__
  1947. /* Normal reset, as B-Device;
  1948. * or else after HNP, as A-Device
  1949. */
  1950. if (devctl & MUSB_DEVCTL_BDEVICE) {
  1951. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  1952. musb->g.is_a_peripheral = 0;
  1953. } else if (is_otg_enabled(musb)) {
  1954. musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
  1955. musb->g.is_a_peripheral = 1;
  1956. } else
  1957. WARN_ON(1);
  1958. /* start with default limits on VBUS power draw */
  1959. (void) musb_gadget_vbus_draw(&musb->g,
  1960. is_otg_enabled(musb) ? 8 : 100);
  1961. #endif
  1962. }