musb_gadget.c 59 KB

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