musb_gadget.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  20. * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  23. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/list.h>
  31. #include <linux/timer.h>
  32. #include <linux/module.h>
  33. #include <linux/smp.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/delay.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/slab.h>
  38. #include "musb_core.h"
  39. /* #include "mu3d_hal_osal.h" */
  40. #include "mu3d_hal_qmu_drv.h"
  41. #include "mu3d_hal_usb_drv.h"
  42. #include "mu3d_hal_hw.h"
  43. #include "ssusb_io.h"
  44. /* ----------------------------------------------------------------------- */
  45. #define is_buffer_mapped(req) (req->map_state != UN_MAPPED)
  46. /* Maps the buffer to dma */
  47. static inline void map_dma_buffer(struct musb_request *request,
  48. struct musb *musb, struct musb_ep *musb_ep)
  49. {
  50. request->map_state = UN_MAPPED;
  51. #ifndef USE_SSUSB_QMU
  52. /* for pio need modify it yun */
  53. return;
  54. #endif
  55. if (request->request.dma == DMA_ADDR_INVALID) {
  56. request->request.dma = dma_map_single(musb->controller,
  57. request->request.buf,
  58. request->request.length,
  59. request->tx ? DMA_TO_DEVICE :
  60. DMA_FROM_DEVICE);
  61. request->map_state = MUSB_MAPPED;
  62. mu3d_dbg(K_DEBUG, "%s - dma_map_single\n", __func__);
  63. } else {
  64. dma_sync_single_for_device(musb->controller,
  65. request->request.dma,
  66. request->request.length,
  67. request->tx ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  68. request->map_state = PRE_MAPPED;
  69. mu3d_dbg(K_DEBUG, "%s - dma_sync_single_for_device\n", __func__);
  70. }
  71. }
  72. /* Unmap the buffer from dma and maps it back to cpu */
  73. static inline void unmap_dma_buffer(struct musb_request *request, struct musb *musb)
  74. {
  75. if (!is_buffer_mapped(request))
  76. return;
  77. if (request->request.dma == DMA_ADDR_INVALID) {
  78. dev_vdbg(musb->controller, "not unmapping a never mapped buffer\n");
  79. return;
  80. }
  81. if (request->map_state == MUSB_MAPPED) {
  82. /* avoid adb push error ,modify later */
  83. dma_unmap_single(musb->controller, request->request.dma,
  84. request->request.length,
  85. request->tx ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  86. request->request.dma = DMA_ADDR_INVALID;
  87. mu3d_dbg(K_DEBUG, "%s - dma_unmap_single\n", __func__);
  88. } else { /* PRE_MAPPED */
  89. dma_sync_single_for_cpu(musb->controller,
  90. request->request.dma,
  91. request->request.length,
  92. request->tx ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  93. mu3d_dbg(K_DEBUG, "%s - dma_sync_single_for_cpu\n", __func__);
  94. }
  95. request->map_state = UN_MAPPED;
  96. }
  97. /*
  98. * Immediately complete a request.
  99. *
  100. * @param request the request to complete
  101. * @param status the status to complete the request with
  102. * Context: controller locked, IRQs blocked.
  103. */
  104. void musb_g_giveback(struct musb_ep *ep,
  105. struct usb_request *request,
  106. int status) __releases(ep->musb->lock) __acquires(ep->musb->lock)
  107. {
  108. struct musb_request *req;
  109. struct musb *musb;
  110. int busy = ep->busy;
  111. req = to_musb_request(request);
  112. list_del(&req->list);
  113. if (req->request.status == -EINPROGRESS)
  114. req->request.status = status;
  115. musb = req->musb;
  116. /* mu3d_dbg(K_DEBUG, "%s - 0\n", __func__); */
  117. /* if ((request->status == 0) && (512 < request->actual) && (request->actual < 1024)) */
  118. /* print_buf((char *)request->buf, request->actual); */
  119. ep->busy = 1;
  120. spin_unlock(&musb->lock);
  121. unmap_dma_buffer(req, musb);
  122. /* mu3d_dbg(K_DEBUG, "%s - 1\n", __func__); */
  123. /* if ((request->status == 0) && (512 < request->actual) && (request->actual < 1024)) */
  124. /* print_buf((char *)request->buf, request->actual); */
  125. if (request->status == 0)
  126. /* dev_dbg(musb->controller, "%s done request %p, %d/%d\n", */
  127. mu3d_dbg(K_DEBUG, "%s done request %p, %d/%d\n",
  128. ep->end_point.name, request, req->request.actual, req->request.length);
  129. else
  130. /* dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n", */
  131. mu3d_dbg(K_DEBUG, "%s request %p, %d/%d fault %d\n",
  132. ep->end_point.name, request,
  133. req->request.actual, req->request.length, request->status);
  134. mu3d_dbg(K_DEBUG, "musb_g_giveback : %p, #%d\n", request, req->request.actual);
  135. req->request.complete(&req->ep->end_point, &req->request);
  136. spin_lock(&musb->lock);
  137. ep->busy = busy;
  138. }
  139. /* ----------------------------------------------------------------------- */
  140. /*
  141. * Abort requests queued to an endpoint using the status. Synchronous.
  142. * caller locked controller and blocked irqs, and selected this ep.
  143. */
  144. static void nuke(struct musb_ep *ep, const int status)
  145. {
  146. struct musb_request *req = NULL;
  147. mu3d_dbg(K_INFO, "%s status=%d %s-%s\n", __func__, status, ep->end_point.name,
  148. (ep->is_in ? "IN" : "OUT"));
  149. ep->busy = 1;
  150. #ifdef USE_SSUSB_QMU
  151. mu3d_hal_flush_qmu(ep->musb, ep->hw_ep->epnum, (ep->is_in ? USB_TX : USB_RX));
  152. /* mu3d_hal_start_qmu(ep->musb, ep->musb->mregs, ep->hw_ep->epnum, (ep->is_in? USB_TX: USB_RX)); */
  153. #endif
  154. while (!list_empty(&ep->req_list)) {
  155. req = list_first_entry(&ep->req_list, struct musb_request, list);
  156. musb_g_giveback(ep, &req->request, status);
  157. mu3d_dbg(K_INFO, "%s call musb_g_giveback() EP is %s\n", __func__,
  158. ep->end_point.name);
  159. }
  160. }
  161. /* ----------------------------------------------------------------------- */
  162. /* Data transfers - pure PIO, pure DMA, or mixed mode */
  163. /*
  164. * This assumes the separate CPPI engine is responding to DMA requests
  165. * from the usb core ... sequenced a bit differently from mentor dma.
  166. */
  167. static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
  168. {
  169. /* if (can_bulk_split(musb, ep->type)) */
  170. /* return ep->hw_ep->max_packet_sz_tx; */
  171. /* else */
  172. return ep->packet_sz;
  173. }
  174. /* #ifdef CONFIG_USB_INVENTRA_DMA */
  175. /* Peripheral tx (IN) using Mentor DMA works as follows:
  176. Only mode 0 is used for transfers <= wPktSize,
  177. mode 1 is used for larger transfers,
  178. One of the following happens:
  179. - Host sends IN token which causes an endpoint interrupt
  180. -> TxAvail
  181. -> if DMA is currently busy, exit.
  182. -> if queue is non-empty, txstate().
  183. - Request is queued by the gadget driver.
  184. -> if queue was previously empty, txstate()
  185. txstate()
  186. -> start
  187. /\ -> setup DMA
  188. | (data is transferred to the FIFO, then sent out when
  189. | IN token(s) are recd from Host.
  190. | -> DMA interrupt on completion
  191. | calls TxAvail.
  192. | -> stop DMA, ~DMAENAB,
  193. | -> set TxPktRdy for last short pkt or zlp
  194. | -> Complete Request
  195. | -> Continue next request (call txstate)
  196. |___________________________________|
  197. * Non-Mentor DMA engines can of course work differently, such as by
  198. * upleveling from irq-per-packet to irq-per-buffer.
  199. */
  200. /* #endif */
  201. /* #ifndef USE_SSUSB_QMU */
  202. /* maybe noneed it such macro modify later yun */
  203. /*
  204. * An endpoint is transmitting data. This can be called either from
  205. * the IRQ routine or from ep.queue() to kickstart a request on an
  206. * endpoint.
  207. *
  208. * Context: controller locked, IRQs blocked, endpoint selected
  209. */
  210. static void txstate(struct musb *musb, struct musb_request *req)
  211. {
  212. /* u8 epnum = req->epnum; */
  213. struct musb_ep *musb_ep = req->ep;
  214. struct usb_request *request;
  215. struct musb_hw_ep *hw_ep = musb_ep->hw_ep;
  216. u16 count = 0;
  217. u32 txcsr0 = 0;
  218. u32 maxp;
  219. /* int use_dma = 0; */
  220. mu3d_dbg(K_DEBUG, "%s\n", __func__);
  221. /* we shouldn't get here while DMA is active ... but we do ... */
  222. /* if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { */
  223. /* dev_dbg(musb->controller, "dma pending...\n"); */
  224. /* return; */
  225. /* } */
  226. /* read TXCSR before */
  227. txcsr0 = mu3d_readl(hw_ep->addr_txcsr0, 0);
  228. request = &req->request;
  229. count = min(max_ep_writesize(musb, musb_ep), (int)(request->length - request->actual));
  230. if (txcsr0 & TX_TXPKTRDY)
  231. return;
  232. if (txcsr0 & TX_SENDSTALL)
  233. return;
  234. /* if (!use_dma) { */
  235. /*
  236. * Unmap the dma buffer back to cpu if dma channel
  237. * programming fails
  238. */
  239. /* unmap_dma_buffer(req, musb); //modify it and fifo write bellow later yun */
  240. maxp = musb_ep->end_point.maxpacket;
  241. /* mu3d_hal_write_fifo(musb_ep->hw_ep, epnum, fifo_count, (u8 *) (request->buf + request->actual), maxp); */
  242. musb_write_fifo(musb_ep->hw_ep, count, (u8 *) (request->buf + request->actual));
  243. request->actual += count;
  244. /* if (count == 0) { */
  245. /* mu3d_setmsk(hw_ep->addr_txcsr0, 0, TX_TXPKTRDY); */
  246. /* } else { */
  247. #ifdef AUTOSET
  248. if (count < maxp) {
  249. mu3d_setmsk(hw_ep->addr_txcsr0, 0, TX_TXPKTRDY);
  250. mu3d_dbg(K_DEBUG, "short packet\n");
  251. }
  252. #else
  253. mu3d_setmsk(hw_ep->addr_txcsr0, 0, TX_TXPKTRDY);
  254. #endif
  255. /* } */
  256. mu3d_dbg(K_DEBUG, "%s actual=%d, count=%d\n", __func__, request->actual, count);
  257. #if 0
  258. musb_write_fifo(musb_ep->hw_ep, fifo_count, (u8 *) (request->buf + request->actual));
  259. request->actual += fifo_count;
  260. txcsr0 &= TX_W1C_BITS;
  261. txcsr0 |= TX_TXPKTRDY;
  262. os_writel(musb->endpoints[epnum].addr_txcsr0, txcsr0);
  263. #endif
  264. /* } */
  265. }
  266. /* #endif */
  267. /*
  268. * FIFO state update (e.g. data ready).
  269. * Called from IRQ, with controller locked.
  270. */
  271. void musb_g_tx(struct musb *musb, u8 epnum)
  272. {
  273. u32 txcsr0;
  274. struct musb_request *req;
  275. struct usb_request *request;
  276. struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
  277. /* struct dma_channel *dma; */
  278. req = next_request(musb_ep);
  279. if (NULL == req)
  280. return;
  281. request = &req->request;
  282. txcsr0 = mu3d_readl(musb_ep->hw_ep->addr_txcsr0, 0);
  283. /* dma = is_dma_capable() ? musb_ep->dma : NULL; */
  284. /* if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { */
  285. /*
  286. * SHOULD NOT HAPPEN... has with CPPI though, after
  287. * changing SENDSTALL (and other cases); harmless?
  288. */
  289. /* dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name); */
  290. /* return; */
  291. /* } */
  292. if (request) {
  293. /*
  294. * First, maybe a terminating short packet. Some DMA
  295. * engines might handle this by themselves.
  296. */
  297. if ((request->zero && request->length && (request->length % musb_ep->packet_sz == 0)
  298. && (request->actual == request->length))
  299. ) {
  300. /*
  301. * On DMA completion, FIFO may not be
  302. * available yet...
  303. */
  304. if (txcsr0 & TX_TXPKTRDY)
  305. return;
  306. dev_dbg(musb->controller, "sending zero pkt\n");
  307. mu3d_writel(musb_ep->hw_ep->addr_txcsr0, 0,
  308. (txcsr0 & TX_W1C_BITS) | TX_TXPKTRDY);
  309. request->zero = 0;
  310. }
  311. if (request->actual == request->length) {
  312. musb_g_giveback(musb_ep, request, 0);
  313. req = musb_ep->desc ? next_request(musb_ep) : NULL;
  314. if (!req) {
  315. dev_dbg(musb->controller, "%s idle now\n", musb_ep->end_point.name);
  316. return;
  317. }
  318. }
  319. /* #ifdef USE_SSUSB_QMU */
  320. /* txstate_qmu(musb, req); */
  321. /* #else */
  322. txstate(musb, req);
  323. /* #endif */
  324. }
  325. }
  326. /* ------------------------------------------------------------ */
  327. /* #ifdef CONFIG_USB_INVENTRA_DMA */
  328. /* Peripheral rx (OUT) using Mentor DMA works as follows:
  329. - Only mode 0 is used.
  330. - Request is queued by the gadget class driver.
  331. -> if queue was previously empty, rxstate()
  332. - Host sends OUT token which causes an endpoint interrupt
  333. /\ -> RxReady
  334. | -> if request queued, call rxstate
  335. | /\ -> setup DMA
  336. | | -> DMA interrupt on completion
  337. | | -> RxReady
  338. | | -> stop DMA
  339. | | -> ack the read
  340. | | -> if data recd = max expected
  341. | | by the request, or host
  342. | | sent a short packet,
  343. | | complete the request,
  344. | | and start the next one.
  345. | |_____________________________________|
  346. | else just wait for the host
  347. | to send the next OUT token.
  348. |__________________________________________________|
  349. * Non-Mentor DMA engines can of course work differently.
  350. */
  351. /* #endif */
  352. /*
  353. * Context: controller locked, IRQs blocked, endpoint selected
  354. */
  355. static void rxstate(struct musb *musb, struct musb_request *req)
  356. {
  357. const u8 epnum = req->epnum;
  358. struct usb_request *request = &req->request;
  359. struct musb_ep *musb_ep;
  360. u16 fifo_count;
  361. unsigned len = 0;
  362. struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
  363. u32 rxcsr0 = mu3d_readl(hw_ep->addr_rxcsr0, 0);
  364. musb_ep = &hw_ep->ep_out;
  365. fifo_count = musb_ep->packet_sz;
  366. /* We shouldn't get here while DMA is active, but we do... */
  367. /* if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { */
  368. /* dev_dbg(musb->controller, "DMA pending...\n"); */
  369. /* return; */
  370. /* } */
  371. mu3d_dbg(K_DEBUG, "req-list(%p) next=%p, prev=%p\n", &musb_ep->req_list,
  372. musb_ep->req_list.next, musb_ep->req_list.prev);
  373. mu3d_dbg(K_DEBUG, "epnum=%d, rxcsr addr=0x%p, rxcsr0=%#x\n", epnum,
  374. musb->endpoints[epnum].addr_rxcsr0, rxcsr0);
  375. if (rxcsr0 & RX_SENDSTALL)
  376. return;
  377. if (rxcsr0 & RX_RXPKTRDY) {
  378. fifo_count =
  379. (mu3d_xcsr_readl(musb->mac_base, U3D_RX1CSR3, epnum) >> EP_RX_COUNT_OFST);
  380. if (request->actual < request->length) {
  381. len = request->length - request->actual;
  382. fifo_count = min_t(unsigned, len, fifo_count);
  383. /* fifo_count = mu3d_hal_read_fifo( epnum, (request->buf + request->actual)); */
  384. musb_read_fifo(&musb->endpoints[epnum], fifo_count,
  385. (request->buf + request->actual));
  386. request->actual += fifo_count;
  387. /* ack the read! */
  388. #ifdef AUTOCLEAR
  389. if (!fifo_count) {
  390. mu3d_xcsr_writel(musb->mac_base, U3D_RX1CSR0, epnum,
  391. mu3d_xcsr_readl(musb->mac_base, U3D_RX1CSR0,
  392. epnum) | RX_RXPKTRDY);
  393. }
  394. #else
  395. mu3d_xcsr_writel(musb->mac_base, U3D_RX1CSR0, epnum,
  396. mu3d_xcsr_readl(musb->mac_base, U3D_RX1CSR0,
  397. epnum) | RX_RXPKTRDY);
  398. #endif
  399. }
  400. }
  401. mu3d_dbg(K_DEBUG, "%s length=%d, actual=%d, fifo_count=%d, packet_sz=%d\n",
  402. __func__, request->length, request->actual, fifo_count, musb_ep->packet_sz);
  403. /* reach the end or short packet detected */
  404. if (request->actual == request->length || fifo_count < musb_ep->packet_sz)
  405. musb_g_giveback(musb_ep, request, 0);
  406. }
  407. /*
  408. * Data ready for a request; called from IRQ
  409. */
  410. void musb_g_rx(struct musb *musb, u8 epnum)
  411. {
  412. u32 rxcsr0;
  413. struct musb_request *req;
  414. struct usb_request *request;
  415. struct musb_ep *musb_ep;
  416. /* struct dma_channel *dma; */
  417. struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
  418. musb_ep = &hw_ep->ep_out;
  419. mu3d_dbg(K_DEBUG, "%s-%d ep%dRx\n", __func__, __LINE__, epnum);
  420. mu3d_dbg(K_DEBUG, "req-list(%p) next=%p, prev=%p\n", &musb_ep->req_list,
  421. musb_ep->req_list.next, musb_ep->req_list.prev);
  422. req = next_request(musb_ep);
  423. if (!req) {
  424. mu3d_dbg(K_DEBUG, "%s-%d ep%dRx, RXCSR0-0x%x(request is null, return)\n",
  425. __func__, __LINE__, epnum, mu3d_readl(hw_ep->addr_rxcsr0, 0));
  426. return;
  427. }
  428. request = &req->request;
  429. rxcsr0 = mu3d_readl(hw_ep->addr_rxcsr0, 0);
  430. if (rxcsr0 & RX_SENTSTALL) {
  431. mu3d_dbg(K_DEBUG, "%s-%d ep%dRx (rx_sentstall)\n", __func__, __LINE__, epnum);
  432. /* EPN needs to continuous sending STALL until host set clear_feature to clear the status. */
  433. /* musb_writew(epio, MUSB_RXCSR, csr); */
  434. mu3d_writel(hw_ep->addr_rxcsr0, 0, (rxcsr0 & RX_W1C_BITS) | RX_SENTSTALL);
  435. return;
  436. }
  437. /* dma = is_dma_capable() ? musb_ep->dma : NULL; */
  438. /* Analyze request */
  439. rxstate(musb, req);
  440. }
  441. /* ------------------------------------------------------------ */
  442. static int musb_gadget_enable(struct usb_ep *ep, const struct usb_endpoint_descriptor *desc)
  443. {
  444. unsigned long flags;
  445. struct musb_ep *musb_ep;
  446. struct musb_hw_ep *hw_ep;
  447. struct musb *musb;
  448. void __iomem *mbase;
  449. u8 epnum = 0;
  450. unsigned maxp = 0;
  451. int status = -EINVAL;
  452. TRANSFER_TYPE type = USB_CTRL;
  453. USB_DIR dir = USB_TX;
  454. if (!ep || !desc)
  455. return -EINVAL;
  456. mu3d_dbg(K_INFO, "musb_gadget_enable %s\n", ep->name);
  457. musb_ep = to_musb_ep(ep);
  458. hw_ep = musb_ep->hw_ep;
  459. musb = musb_ep->musb;
  460. mbase = musb->mac_base;
  461. epnum = hw_ep->epnum;
  462. spin_lock_irqsave(&musb->lock, flags);
  463. if (musb_ep->desc) {
  464. status = -EBUSY;
  465. goto fail;
  466. }
  467. musb_ep->type = usb_endpoint_type(desc);
  468. /* check direction and (later) maxpacket size against endpoint */
  469. if (usb_endpoint_num(desc) != epnum)
  470. goto fail;
  471. /* REVISIT this rules out high bandwidth periodic transfers */
  472. maxp = le16_to_cpu(desc->wMaxPacketSize);
  473. if (maxp & ~0x07ff)
  474. goto fail;
  475. musb_ep->packet_sz = maxp;
  476. mu3d_dbg(K_DEBUG, "U3D_EPIER=0x%X\n", mu3d_readl(mbase, U3D_EPIER));
  477. /* enable the interrupts for the endpoint, set the endpoint
  478. * packet size (or fail), set the mode, clear the fifo
  479. */
  480. if (usb_endpoint_dir_in(desc)) { /* TX */
  481. #ifndef USE_SSUSB_QMU
  482. u32 int_txe = mu3d_readl(mbase, U3D_EPIER);
  483. #endif
  484. if (hw_ep->is_shared_fifo)
  485. musb_ep->is_in = 1;
  486. if (!musb_ep->is_in)
  487. goto fail;
  488. if (maxp > hw_ep->max_packet_sz_tx)
  489. goto fail;
  490. #ifndef USE_SSUSB_QMU
  491. mu3d_dbg(K_DEBUG, "epnum=%d, int_txe=0x%x, EPIER=0x%x+\n", epnum, int_txe,
  492. mu3d_readl(mbase, U3D_EPIER));
  493. int_txe |= (1 << epnum);
  494. mu3d_writel(mbase, U3D_EPIESR, int_txe);
  495. mu3d_dbg(K_DEBUG, "epnum=%d, int_txe=0x%x, EPIER=0x%x-\n", epnum, int_txe,
  496. mu3d_readl(mbase, U3D_EPIER));
  497. #endif
  498. dir = USB_TX;
  499. switch (musb_ep->type) {
  500. case USB_ENDPOINT_XFER_BULK:
  501. type = USB_BULK;
  502. break;
  503. case USB_ENDPOINT_XFER_ISOC:
  504. type = USB_ISO;
  505. break;
  506. case USB_ENDPOINT_XFER_INT:
  507. type = USB_INTR;
  508. break;
  509. }
  510. } else {
  511. #ifndef USE_SSUSB_QMU
  512. u32 int_rxe = mu3d_readl(mbase, U3D_EPIER);
  513. #endif
  514. if (hw_ep->is_shared_fifo)
  515. musb_ep->is_in = 0;
  516. if (musb_ep->is_in)
  517. goto fail;
  518. if (maxp > hw_ep->max_packet_sz_rx) {
  519. dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
  520. goto fail;
  521. }
  522. #ifndef USE_SSUSB_QMU
  523. mu3d_dbg(K_DEBUG, "int_rxe=0x%x, EPIER=0x%x+\n", int_rxe,
  524. mu3d_readl(mbase, U3D_EPIER));
  525. int_rxe |= (BIT16 << epnum);
  526. mu3d_writel(mbase, U3D_EPIESR, int_rxe);
  527. mu3d_dbg(K_DEBUG, "int_rxe=0x%x, EPIER=0x%x-\n", int_rxe,
  528. mu3d_readl(mbase, U3D_EPIER));
  529. #endif
  530. dir = USB_RX;
  531. switch (musb_ep->type) {
  532. case USB_ENDPOINT_XFER_BULK:
  533. type = USB_BULK;
  534. break;
  535. case USB_ENDPOINT_XFER_ISOC:
  536. type = USB_ISO;
  537. break;
  538. case USB_ENDPOINT_XFER_INT:
  539. type = USB_INTR;
  540. break;
  541. }
  542. }
  543. /*
  544. * At PIO with 2 slot(double buffer), the host transfers 512 + N bytes.
  545. * 512 would fill the 1st slot. And the rest of N bytes will put into
  546. * the 2nd slot. The interrupt is coming. The driver reads the data
  547. * stored at the 1st slot. Then driver expects the next interrupt to
  548. * read the data at the 2nd slot. But the interrupt does not show up!
  549. * Designer says maybe the interrupt the driver handles is the interrupt
  550. * come from the 2nd slot. the system does not fast enough. So the later
  551. * one and the previous one merge to one interrupt.
  552. * So at FPGA stage and PIO, just use _ONE_ slot.
  553. */
  554. #ifdef USE_SSUSB_QMU
  555. mu3d_hal_ep_enable(musb, epnum, dir, type, maxp, 0, MAX_SLOT, 0, 0);
  556. #else
  557. /*TODO: Check support mulitslots on real ship */
  558. mu3d_hal_ep_enable(musb, epnum, dir, type, maxp, 0, 0, 0, 0);
  559. #endif
  560. #ifdef USE_SSUSB_QMU
  561. mu3d_hal_start_qmu(musb, epnum, dir);
  562. #endif
  563. /* NOTE: all the I/O code _should_ work fine without DMA, in case
  564. * for some reason you run out of channels here.
  565. */
  566. /* if (is_dma_capable() && musb->dma_controller) { */
  567. /* struct dma_controller *c = musb->dma_controller; */
  568. /* musb_ep->dma = c->channel_alloc(c, hw_ep, (desc->bEndpointAddress & USB_DIR_IN)); */
  569. /* } else */
  570. /* musb_ep->dma = NULL; */
  571. musb_ep->desc = desc;
  572. musb_ep->busy = 0;
  573. musb_ep->wedged = 0;
  574. status = 0;
  575. musb->active_ep++;
  576. mu3d_dbg(K_DEBUG, "[U3D]%s active_ep=%d\n", __func__, musb->active_ep);
  577. queue_work(musb->wq, &musb->otg_event_work);
  578. fail:
  579. spin_unlock_irqrestore(&musb->lock, flags);
  580. return status;
  581. }
  582. /*
  583. * Disable an endpoint flushing all requests queued.
  584. */
  585. static int musb_gadget_disable(struct usb_ep *ep)
  586. {
  587. struct musb_ep *musb_ep = to_musb_ep(ep);
  588. struct musb *musb = musb_ep->musb;
  589. struct musb_hw_ep *hw_ep = musb_ep->hw_ep;
  590. u8 epnum = musb_ep->current_epnum;
  591. unsigned long flags;
  592. u32 rxcsr0_val;
  593. u32 txcsr0_val;
  594. int status = 0;
  595. mu3d_dbg(K_INFO, "%s\n", __func__);
  596. spin_lock_irqsave(&musb->lock, flags);
  597. #ifndef USE_SSUSB_QMU
  598. /* disable interrupt */
  599. if (musb_ep->is_in) { /* TX */
  600. mu3d_setmsk(musb->mac_base, U3D_EPIECR, (1 << epnum));
  601. } else {
  602. mu3d_setmsk(musb->mac_base, U3D_EPIECR, (BIT16 << epnum));
  603. }
  604. #endif
  605. /* zero the endpoint sizes */
  606. if (musb_ep->is_in) {
  607. txcsr0_val = mu3d_readl(hw_ep->addr_txcsr0, 0);
  608. txcsr0_val &= ~(TX_TXMAXPKTSZ);
  609. txcsr0_val &= TX_W1C_BITS;
  610. mu3d_writel(hw_ep->addr_txcsr0, 0, txcsr0_val);
  611. /* os_writelmskumsk(musb->endpoints[epnum].addr_rxcsr0, 0, TX_TXMAXPKTSZ, TX_W1C_BITS); */
  612. } else {
  613. rxcsr0_val = mu3d_readl(hw_ep->addr_rxcsr0, 0);
  614. rxcsr0_val &= ~(RX_RXMAXPKTSZ);
  615. rxcsr0_val &= RX_W1C_BITS;
  616. mu3d_writel(hw_ep->addr_rxcsr0, 0, rxcsr0_val);
  617. /* os_writelmskumsk(musb->endpoints[epnum].addr_rxcsr0, 0, RX_RXMAXPKTSZ, RX_W1C_BITS); */
  618. }
  619. musb_ep->desc = NULL;
  620. /* abort all pending DMA and requests */
  621. nuke(musb_ep, -ESHUTDOWN);
  622. mu3d_hal_unfigured_ep_num(musb, epnum, (musb_ep->is_in ? USB_TX : USB_RX));
  623. queue_work(musb->wq, &musb->otg_event_work);
  624. musb->active_ep--;
  625. mu3d_dbg(K_INFO, "[U3D]%s active_ep=%d, mu3d is_active=%d\n", __func__,
  626. musb->active_ep, musb->is_active);
  627. if (musb->active_ep == 0 && musb->is_active == 0 && !is_maual_otg(musb->ssusb))
  628. queue_work(musb->wq, &musb->suspend_work);
  629. spin_unlock_irqrestore(&(musb->lock), flags);
  630. dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
  631. return status;
  632. }
  633. /*
  634. * Allocate a request for an endpoint.
  635. * Reused by ep0 code.
  636. */
  637. struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
  638. {
  639. struct musb_ep *musb_ep = to_musb_ep(ep);
  640. struct musb_request *request = NULL;
  641. request = kzalloc(sizeof(*request), gfp_flags);
  642. if (!request)
  643. return NULL;
  644. request->request.dma = DMA_ADDR_INVALID;
  645. request->epnum = musb_ep->current_epnum;
  646. request->ep = musb_ep;
  647. return &request->request;
  648. }
  649. /*
  650. * Free a request
  651. * Reused by ep0 code.
  652. */
  653. void musb_free_request(struct usb_ep *ep, struct usb_request *req)
  654. {
  655. kfree(to_musb_request(req));
  656. }
  657. /* static LIST_HEAD(buffers); */
  658. /* struct free_record { */
  659. /* struct list_head list; */
  660. /* struct device *dev; */
  661. /* unsigned bytes; */
  662. /* dma_addr_t dma; */
  663. /* }; */
  664. /*
  665. * Context: controller locked, IRQs blocked.
  666. */
  667. void musb_ep_restart(struct musb *musb, struct musb_request *req)
  668. {
  669. /*
  670. * We don't do anything if QMU because QMU is already
  671. * waiting there when musb_gadget_queue().
  672. */
  673. #ifndef USE_SSUSB_QMU
  674. /* dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n", */
  675. mu3d_dbg(K_DEBUG, " %s %s request %p len %u on hw_ep%d\n",
  676. __func__, req->tx ? "TX/IN" : "RX/OUT",
  677. &req->request, req->request.length, req->epnum);
  678. if (req->tx)
  679. txstate(musb, req);
  680. else
  681. rxstate(musb, req);
  682. #endif
  683. }
  684. static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req, gfp_t gfp_flags)
  685. {
  686. struct musb_ep *musb_ep;
  687. struct musb_request *request;
  688. struct musb *musb;
  689. int status = 0;
  690. unsigned long lockflags;
  691. if (!ep || !req)
  692. return -EINVAL;
  693. if (!req->buf)
  694. return -ENODATA;
  695. musb_ep = to_musb_ep(ep);
  696. musb = musb_ep->musb;
  697. request = to_musb_request(req);
  698. request->musb = musb;
  699. if (request->ep != musb_ep)
  700. return -EINVAL;
  701. mu3d_dbg(K_DEBUG, "%s %s, req=%p, len#%d\n", __func__, ep->name, req,
  702. request->request.length);
  703. /* dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req); */
  704. /* request is mine now... */
  705. request->request.actual = 0;
  706. request->request.status = -EINPROGRESS;
  707. request->epnum = musb_ep->current_epnum;
  708. request->tx = musb_ep->is_in;
  709. map_dma_buffer(request, musb, musb_ep);
  710. spin_lock_irqsave(&musb->lock, lockflags);
  711. /* don't queue if the ep is down */
  712. if (!musb_ep->desc) {
  713. dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
  714. req, ep->name, "disabled");
  715. status = -ESHUTDOWN;
  716. goto cleanup;
  717. }
  718. /* add request to the list */
  719. list_add_tail(&request->list, &musb_ep->req_list);
  720. #ifdef USE_SSUSB_QMU
  721. if (request->request.dma != DMA_ADDR_INVALID) {
  722. qmu_dbg(K_DEBUG, "%s %s EP%d, len=%d, maxp=%d request=%p\n",
  723. request->tx ? "[TX]" : "[RX]", __func__, request->epnum,
  724. request->request.length, ep->maxpacket, request);
  725. if (request->tx) {
  726. struct musb_hw_ep *hw_ep = musb_ep->hw_ep;
  727. request->request.actual = request->request.length;
  728. if (request->request.length > 0) {
  729. mu3d_hal_insert_transfer_gpd(request->epnum, USB_TX,
  730. request->request.dma,
  731. request->request.length, true, true,
  732. false,
  733. ((request->request.zero == 1) ? 1 : 0),
  734. ep->maxpacket);
  735. /*Enable Tx_DMAREQEN */
  736. mu3d_setmsk(hw_ep->addr_txcsr0, 0, TX_DMAREQEN);
  737. mu3d_hal_resume_qmu(musb, request->epnum, USB_TX);
  738. } else if (request->request.length == 0) {
  739. qmu_dbg(K_DEBUG, "[TX]" "==Have send ZLP==\n");
  740. /* If there is only ZLP in the request list. Just send ZLP directly */
  741. if (next_request(musb_ep) == request) {
  742. /* Send ZLP by setting TXPKTRDY, TODO add timeout... */
  743. while (!(mu3d_readl(hw_ep->addr_txcsr0, 0) & TX_FIFOFULL)) {
  744. mu3d_clrmsk(hw_ep->addr_txcsr0, 0, TX_DMAREQEN);
  745. /* mb(); */
  746. mu3d_setmsk(hw_ep->addr_txcsr0, 0, TX_TXPKTRDY);
  747. break;
  748. }
  749. qmu_dbg(K_DEBUG,
  750. "[TX]Give back ZLP of EP%d. actual:%d, length:%d %p\n",
  751. request->epnum,
  752. request->request.actual,
  753. request->request.length, request);
  754. musb_g_giveback(musb_ep, &(request->request), 0);
  755. }
  756. }
  757. } else {
  758. mu3d_hal_insert_transfer_gpd(request->epnum, USB_RX, request->request.dma,
  759. request->request.length, true, true, false,
  760. 0, ep->maxpacket);
  761. mu3d_hal_resume_qmu(musb, request->epnum, USB_RX);
  762. }
  763. }
  764. #else
  765. /* it this is the head of the queue, start i/o ... */
  766. if (!musb_ep->busy && is_first_entry(&request->list, &musb_ep->req_list))
  767. musb_ep_restart(musb, request);
  768. #endif
  769. cleanup:
  770. spin_unlock_irqrestore(&musb->lock, lockflags);
  771. return status;
  772. }
  773. static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
  774. {
  775. struct musb_ep *musb_ep = to_musb_ep(ep);
  776. struct musb_request *req = to_musb_request(request);
  777. struct musb_request *r;
  778. unsigned long flags;
  779. int status = 0;
  780. struct musb *musb = musb_ep->musb;
  781. if (!ep || !request || to_musb_request(request)->ep != musb_ep)
  782. return -EINVAL;
  783. mu3d_dbg(K_INFO, "*************** musb_gadget_dequeue : request 0x%p\n\n\n", request);
  784. spin_lock_irqsave(&musb->lock, flags);
  785. list_for_each_entry(r, &musb_ep->req_list, list) {
  786. if (r == req)
  787. break;
  788. }
  789. if (r != req) {
  790. dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
  791. status = -EINVAL;
  792. goto done;
  793. }
  794. /* if the hardware doesn't have the request, easy ... */
  795. if (musb_ep->req_list.next != &req->list || musb_ep->busy)
  796. musb_g_giveback(musb_ep, request, -ECONNRESET);
  797. /* ... else abort the dma transfer ... */
  798. #ifdef USE_SSUSB_QMU
  799. else {
  800. mu3d_hal_flush_qmu(musb, musb_ep->hw_ep->epnum, (musb_ep->is_in ? USB_TX : USB_RX));
  801. /* is_in--> TX */
  802. musb_g_giveback(musb_ep, request, -ECONNRESET);
  803. /* only start qmu, don't need to reset EP */
  804. mu3d_hal_start_qmu(musb, musb_ep->hw_ep->epnum, (musb_ep->is_in ? USB_TX : USB_RX));
  805. status = 0;
  806. }
  807. #else
  808. /* else if (is_dma_capable() && musb_ep->dma) { */
  809. /* struct dma_controller *c = musb->dma_controller; */
  810. /* if (c->channel_abort) */
  811. /* status = c->channel_abort(musb_ep->dma); */
  812. /* else */
  813. /* status = -EBUSY; */
  814. /* if (status == 0) */
  815. /* musb_g_giveback(musb_ep, request, -ECONNRESET); */
  816. /* } */
  817. else {
  818. /* NOTE: by sticking to easily tested hardware/driver states,
  819. * we leave counting of in-flight packets imprecise.
  820. */
  821. musb_g_giveback(musb_ep, request, -ECONNRESET);
  822. }
  823. #endif
  824. done:
  825. spin_unlock_irqrestore(&musb->lock, flags);
  826. return status;
  827. }
  828. /*
  829. * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
  830. * data but will queue requests.
  831. *
  832. * exported to ep0 code
  833. */
  834. static int musb_gadget_set_halt(struct usb_ep *ep, int value)
  835. {
  836. struct musb_ep *musb_ep = to_musb_ep(ep);
  837. u8 epnum = musb_ep->current_epnum;
  838. struct musb *musb = musb_ep->musb;
  839. struct musb_hw_ep *hw_ep = musb_ep->hw_ep;
  840. void __iomem *mbase = musb->mac_base;
  841. unsigned long flags;
  842. u32 txcsr0 = 0, rxcsr0 = 0;
  843. struct musb_request *request;
  844. int status = 0;
  845. if (!ep)
  846. return -EINVAL;
  847. mu3d_dbg(K_DEBUG, "musb_gadget_set_halt : %s...", ep->name);
  848. spin_lock_irqsave(&musb->lock, flags);
  849. if (USB_ENDPOINT_XFER_ISOC == musb_ep->type) {
  850. status = -EINVAL;
  851. goto done;
  852. }
  853. request = next_request(musb_ep);
  854. if (value) {
  855. if (request) {
  856. dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
  857. ep->name);
  858. status = -EAGAIN;
  859. goto done;
  860. }
  861. /* Cannot portably stall with non-empty FIFO */
  862. if (musb_ep->is_in) {
  863. txcsr0 = mu3d_readl(hw_ep->addr_txcsr0, 0);
  864. if (!(txcsr0 & TX_FIFOEMPTY)) {
  865. dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
  866. status = -EAGAIN;
  867. goto done;
  868. }
  869. }
  870. } else
  871. musb_ep->wedged = 0;
  872. /* set/clear the stall and toggle bits */
  873. dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
  874. if (musb_ep->is_in) { /* TX */
  875. txcsr0 = mu3d_readl(hw_ep->addr_txcsr0, 0) & TX_W1C_BITS;
  876. if (value) { /* set */
  877. txcsr0 |= TX_SENDSTALL;
  878. mu3d_writel(hw_ep->addr_txcsr0, 0, txcsr0);
  879. } else { /* clear */
  880. txcsr0 = (txcsr0 & (~TX_SENDSTALL)) | TX_SENTSTALL;
  881. /* we need to also clear SENTSTALL to let the EP work normaly. */
  882. mu3d_writel(hw_ep->addr_txcsr0, 0, txcsr0);
  883. mu3d_setmsk(mbase, U3D_EP_RST, (BIT16 << epnum)); /* reset TX EP */
  884. mu3d_clrmsk(mbase, U3D_EP_RST, (BIT16 << epnum)); /* reset reset TX EP */
  885. /* We cannot flush QMU now, because the MSC gadget will*/
  886. /*not re-submit the CBW request after clear halt. */
  887. /* mu3d_hal_flush_qmu(musb, epnum, USB_TX); */
  888. /* mu3d_hal_restart_qmu(musb, epnum, USB_TX); */
  889. }
  890. } else {
  891. rxcsr0 = mu3d_readl(hw_ep->addr_rxcsr0, 0) & RX_W1C_BITS;
  892. if (value) { /* set stall */
  893. rxcsr0 &= RX_W1C_BITS;
  894. rxcsr0 |= RX_SENDSTALL;
  895. mu3d_writel(hw_ep->addr_rxcsr0, 0, rxcsr0);
  896. /* writel(rxcsr0, musb->endpoints[epnum].addr_rxcsr0); */
  897. } else { /* clear stall */
  898. rxcsr0 = (rxcsr0 & (~RX_SENDSTALL)) | RX_SENTSTALL;
  899. /* we need to also clear SENTSTALL to let the EP work normaly. */
  900. mu3d_writel(hw_ep->addr_rxcsr0, 0, rxcsr0);
  901. /* writel(rxcsr0, musb->endpoints[epnum].addr_rxcsr0); */
  902. mu3d_setmsk(mbase, U3D_EP_RST, (1 << epnum)); /* reset RX EP */
  903. mu3d_clrmsk(mbase, U3D_EP_RST, (1 << epnum)); /* reset reset RX EP */
  904. /* We cannot flush QMU now, because the MSC gadget will*/
  905. /*not re-submit the CBW request after clear halt. */
  906. /* mu3d_hal_flush_qmu(musb, epnum, USB_RX); */
  907. /* mu3d_hal_restart_qmu(musb, epnum, USB_RX); */
  908. }
  909. }
  910. /* maybe start the first request in the queue */
  911. if (!musb_ep->busy && !value && request) {
  912. dev_dbg(musb->controller, "restarting the request\n");
  913. musb_ep_restart(musb, request);
  914. }
  915. done:
  916. spin_unlock_irqrestore(&musb->lock, flags);
  917. return status;
  918. }
  919. /*
  920. * Sets the halt feature with the clear requests ignored
  921. */
  922. static int musb_gadget_set_wedge(struct usb_ep *ep)
  923. {
  924. struct musb_ep *musb_ep = to_musb_ep(ep);
  925. if (!ep)
  926. return -EINVAL;
  927. musb_ep->wedged = 1;
  928. return usb_ep_set_halt(ep);
  929. }
  930. static int musb_gadget_fifo_status(struct usb_ep *ep)
  931. {
  932. struct musb_ep *musb_ep = to_musb_ep(ep);
  933. int retval = -EINVAL;
  934. if (musb_ep->desc && !musb_ep->is_in) {
  935. struct musb *musb = musb_ep->musb;
  936. /* int epnum = musb_ep->current_epnum; */
  937. unsigned long flags;
  938. spin_lock_irqsave(&musb->lock, flags);
  939. /* FIXME return zero unless RXPKTRDY is set */
  940. retval = mu3d_readl(musb_ep->hw_ep->addr_rxcsr3, 0) >> 16;
  941. spin_unlock_irqrestore(&musb->lock, flags);
  942. }
  943. return retval;
  944. }
  945. static void musb_gadget_fifo_flush(struct usb_ep *ep)
  946. {
  947. struct musb_ep *musb_ep = to_musb_ep(ep);
  948. struct musb *musb = musb_ep->musb;
  949. u8 epnum = musb_ep->current_epnum;
  950. void __iomem *mbase = musb->mac_base;
  951. unsigned long flags;
  952. #ifndef USE_SSUSB_QMU /* we don't enable EP interrupts in QMU mode. */
  953. u32 int_txe;
  954. #endif
  955. u32 txcsr0 = 0;
  956. spin_lock_irqsave(&musb->lock, flags);
  957. /* disable interrupts */
  958. #ifndef USE_SSUSB_QMU /* we don't enable EP interrupts in QMU mode. */
  959. int_txe = mu3d_readl(mbase, U3D_EPIER);
  960. mu3d_writel(mbase, U3D_EPIECR, (1 << epnum)); /* set clear register */
  961. #endif
  962. if (musb_ep->is_in) { /* TX */
  963. #ifdef USE_SSUSB_QMU
  964. mu3d_hal_flush_qmu(musb, epnum, USB_TX);
  965. mu3d_hal_restart_qmu(musb, epnum, USB_TX);
  966. #endif
  967. txcsr0 = mu3d_readl(musb->endpoints[epnum].addr_txcsr0, 0);
  968. if (!(txcsr0 & TX_FIFOEMPTY)) {
  969. mu3d_dbg(K_DEBUG, "%s RESET\n", ep->name);
  970. mu3d_setmsk(mbase, U3D_EP_RST, (BIT16 << epnum)); /* reset TX EP */
  971. mu3d_clrmsk(mbase, U3D_EP_RST, (BIT16 << epnum)); /* reset reset TX EP */
  972. /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
  973. }
  974. } else {
  975. #ifdef USE_SSUSB_QMU
  976. mu3d_hal_flush_qmu(musb, epnum, USB_RX);
  977. mu3d_hal_restart_qmu(musb, epnum, USB_RX);
  978. #endif
  979. mu3d_dbg(K_DEBUG, "%s RESET\n", ep->name);
  980. /* os_writew(musb->endpoints[epnum].addr_rxcsr0, rxcsr0 | USB_RXCSR_FLUSHFIFO); */
  981. /* os_writew(musb->endpoints[epnum].addr_rxcsr0, rxcsr0 & (~USB_RXCSR_FLUSHFIFO)); */
  982. mu3d_setmsk(mbase, U3D_EP_RST, (1 << epnum)); /* reset RX EP */
  983. mu3d_clrmsk(mbase, U3D_EP_RST, (1 << epnum)); /* reset reset RX EP */
  984. }
  985. /* re-enable interrupt */
  986. #ifndef USE_SSUSB_QMU
  987. mu3d_writel(mbase, U3D_EPIESR, int_txe);
  988. #endif
  989. spin_unlock_irqrestore(&musb->lock, flags);
  990. }
  991. static const struct usb_ep_ops musb_ep_ops = {
  992. .enable = musb_gadget_enable,
  993. .disable = musb_gadget_disable,
  994. .alloc_request = musb_alloc_request,
  995. .free_request = musb_free_request,
  996. .queue = musb_gadget_queue,
  997. .dequeue = musb_gadget_dequeue,
  998. .set_halt = musb_gadget_set_halt,
  999. .set_wedge = musb_gadget_set_wedge,
  1000. .fifo_status = musb_gadget_fifo_status,
  1001. .fifo_flush = musb_gadget_fifo_flush
  1002. };
  1003. /* ----------------------------------------------------------------------- */
  1004. static int musb_gadget_get_frame(struct usb_gadget *gadget)
  1005. {
  1006. struct musb *musb = gadget_to_musb(gadget);
  1007. return (int)mu3d_readl(musb->mac_base, U3D_USB20_FRAME_NUM);
  1008. }
  1009. static int musb_gadget_wakeup(struct usb_gadget *gadget)
  1010. {
  1011. struct musb *musb = gadget_to_musb(gadget);
  1012. unsigned long flags;
  1013. int status = -EINVAL;
  1014. /* u8 devctl; */
  1015. /* int retries; */
  1016. spin_lock_irqsave(&musb->lock, flags);
  1017. mu3d_dbg(K_DEBUG, "musb_gadget_wakeup\n");
  1018. switch (musb->xceiv->state) {
  1019. case OTG_STATE_B_PERIPHERAL:
  1020. /* NOTE: OTG state machine doesn't include B_SUSPENDED;
  1021. * that's part of the standard usb 1.1 state machine, and
  1022. * doesn't affect OTG transitions.
  1023. */
  1024. if (musb->may_wakeup && musb->is_suspended)
  1025. break;
  1026. goto done;
  1027. case OTG_STATE_B_IDLE:
  1028. /* Start SRP ... OTG not required. */
  1029. status = 0;
  1030. goto done;
  1031. default:
  1032. dev_dbg(musb->controller, "Unhandled wake: %s\n",
  1033. usb_otg_state_string(musb->xceiv->state));
  1034. goto done;
  1035. }
  1036. status = 0;
  1037. mu3d_dbg(K_DEBUG, "****************** musb_gadget_wakeup......\n");
  1038. /* mu3d_hal_resume(); */
  1039. done:
  1040. spin_unlock_irqrestore(&musb->lock, flags);
  1041. return status;
  1042. }
  1043. static int musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
  1044. {
  1045. struct musb *musb = gadget_to_musb(gadget);
  1046. musb->is_self_powered = !!is_selfpowered;
  1047. return 0;
  1048. }
  1049. #if 0
  1050. static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
  1051. {
  1052. dev_dbg(musb->controller, "<= %s =>\n", __func__);
  1053. /*
  1054. * FIXME iff driver's softconnect flag is set (as it is during probe,
  1055. * though that can clear it), just musb_pullup().
  1056. */
  1057. return -EINVAL;
  1058. }
  1059. #endif
  1060. static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  1061. {
  1062. struct musb *musb = gadget_to_musb(gadget);
  1063. if (!musb->xceiv->set_power)
  1064. return -EOPNOTSUPP;
  1065. return usb_phy_set_power(musb->xceiv, mA);
  1066. }
  1067. static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
  1068. {
  1069. struct musb *musb = gadget_to_musb(gadget);
  1070. unsigned long flags;
  1071. mu3d_dbg(K_INFO, "%s (on-%d)\n", __func__, is_on);
  1072. is_on = !!is_on;
  1073. if (musb->is_active == 0) {
  1074. /* save it for musb_start() to process the request */
  1075. musb->softconnect = is_on;
  1076. mu3d_dbg(K_INFO, "mu3d gadget is inactive(pullup %s)!!\n", is_on ? "on" : "off");
  1077. return 0;
  1078. }
  1079. pm_runtime_get_sync(musb->controller);
  1080. /* NOTE: this assumes we are sensing vbus; we'd rather
  1081. * not pullup unless the B-session is active.
  1082. */
  1083. spin_lock_irqsave(&musb->lock, flags);
  1084. if (is_on != musb->softconnect) {
  1085. musb->softconnect = is_on;
  1086. musb_dev_on_off(musb, is_on);
  1087. }
  1088. spin_unlock_irqrestore(&musb->lock, flags);
  1089. pm_runtime_put(musb->controller);
  1090. return 0;
  1091. }
  1092. static int musb_gadget_start(struct usb_gadget *g, struct usb_gadget_driver *driver);
  1093. static int musb_gadget_stop(struct usb_gadget *g, struct usb_gadget_driver *driver);
  1094. static const struct usb_gadget_ops musb_gadget_operations = {
  1095. .get_frame = musb_gadget_get_frame,
  1096. .wakeup = musb_gadget_wakeup,
  1097. .set_selfpowered = musb_gadget_set_self_powered,
  1098. /* .vbus_session = musb_gadget_vbus_session, */
  1099. .vbus_draw = musb_gadget_vbus_draw,
  1100. .pullup = musb_gadget_pullup,
  1101. .udc_start = musb_gadget_start,
  1102. .udc_stop = musb_gadget_stop,
  1103. /*REVISIT-J: Do we need implement "get_config_params" to config U1/U2 */
  1104. };
  1105. /* ----------------------------------------------------------------------- */
  1106. /* Registration */
  1107. /* Only this registration code "knows" the rule (from USB standards)
  1108. * about there being only one external upstream port. It assumes
  1109. * all peripheral ports are external...
  1110. */
  1111. #ifdef NEVER
  1112. static void musb_gadget_release(struct device *dev)
  1113. {
  1114. /* kref_put(WHAT) */
  1115. dev_dbg(dev, "%s\n", __func__);
  1116. }
  1117. #endif /* NEVER */
  1118. static void init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
  1119. {
  1120. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1121. memset(ep, 0, sizeof(*ep));
  1122. ep->current_epnum = epnum;
  1123. ep->musb = musb;
  1124. ep->hw_ep = hw_ep;
  1125. ep->is_in = is_in;
  1126. INIT_LIST_HEAD(&ep->req_list);
  1127. sprintf(ep->name, "ep%d%s", epnum,
  1128. (!epnum || hw_ep->is_shared_fifo) ? "" : (is_in ? "in" : "out"));
  1129. mu3d_dbg(K_DEBUG, "%s, name=%s\n", __func__, ep->name);
  1130. ep->end_point.name = ep->name;
  1131. INIT_LIST_HEAD(&ep->end_point.ep_list);
  1132. if (!epnum) {
  1133. ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
  1134. ep->end_point.ops = &musb_g_ep0_ops;
  1135. musb->g.ep0 = &ep->end_point;
  1136. } else {
  1137. if (is_in)
  1138. ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
  1139. else
  1140. ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
  1141. ep->end_point.ops = &musb_ep_ops;
  1142. list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
  1143. }
  1144. mu3d_dbg(K_DEBUG, "%s, name=%s, maxp=%d\n", __func__, ep->end_point.name,
  1145. ep->end_point.maxpacket);
  1146. }
  1147. /*
  1148. * Initialize the endpoints exposed to peripheral drivers, with backlinks
  1149. * to the rest of the driver state.
  1150. */
  1151. static inline void musb_g_init_endpoints(struct musb *musb)
  1152. {
  1153. u8 epnum;
  1154. struct musb_hw_ep *hw_ep;
  1155. unsigned count = 0;
  1156. /* initialize endpoint list just once */
  1157. INIT_LIST_HEAD(&(musb->g.ep_list));
  1158. mu3d_dbg(K_INFO, "%s nr_endpoints=%d\n", __func__, musb->nr_endpoints);
  1159. for (epnum = 0, hw_ep = musb->endpoints; epnum < musb->nr_endpoints; epnum++, hw_ep++) {
  1160. mu3d_dbg(K_INFO, "%s epnum=%d shared_fifo=%d rx_maxp=%d tx_maxp=%d\n",
  1161. __func__, epnum, hw_ep->is_shared_fifo, hw_ep->max_packet_sz_rx ? : 0,
  1162. hw_ep->max_packet_sz_tx ? : 0);
  1163. if (hw_ep->is_shared_fifo /* || !epnum */) {
  1164. init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
  1165. count++;
  1166. } else {
  1167. if (hw_ep->max_packet_sz_tx) {
  1168. init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 1);
  1169. count++;
  1170. }
  1171. if (hw_ep->max_packet_sz_rx) {
  1172. init_peripheral_ep(musb, &hw_ep->ep_out, epnum, 0);
  1173. count++;
  1174. }
  1175. }
  1176. }
  1177. }
  1178. /* called once during driver setup to initialize and link into
  1179. * the driver model; memory is zeroed.
  1180. */
  1181. int musb_gadget_setup(struct musb *musb)
  1182. {
  1183. int status;
  1184. /* REVISIT minor race: if (erroneously) setting up two
  1185. * musb peripherals at the same time, only the bus lock
  1186. * is probably held.
  1187. */
  1188. musb->g.ops = &musb_gadget_operations;
  1189. musb->g.max_speed = USB_SPEED_SUPER;
  1190. musb->g.speed = USB_SPEED_UNKNOWN;
  1191. /* this "gadget" abstracts/virtualizes the controller */
  1192. /* dev_set_name(&musb->g.dev, "gadget"); */
  1193. /* musb->g.dev.parent = musb->controller; */
  1194. /* musb->g.dev.dma_mask = musb->controller->dma_mask; */
  1195. /* musb->g.dev.release = musb_gadget_release; */
  1196. musb->g.name = musb_driver_name;
  1197. if (is_otg_enabled(musb))
  1198. musb->g.is_otg = 1;
  1199. musb_g_init_endpoints(musb);
  1200. musb->is_active = 0;
  1201. musb_platform_try_idle(musb, 0);
  1202. /* status = device_register(&musb->g.dev); */
  1203. /* if (status != 0) { */
  1204. /* put_device(&musb->g.dev); */
  1205. /* return status; */
  1206. /* } */
  1207. status = usb_add_gadget_udc(musb->controller, &musb->g);
  1208. if (status)
  1209. goto err;
  1210. return 0;
  1211. err:
  1212. musb->g.dev.parent = NULL;
  1213. device_unregister(&musb->g.dev);
  1214. return status;
  1215. }
  1216. void musb_gadget_cleanup(struct musb *musb)
  1217. {
  1218. usb_del_gadget_udc(&musb->g);
  1219. /* if (musb->g.dev.parent) */
  1220. /* device_unregister(&musb->g.dev); */
  1221. }
  1222. /*
  1223. * Register the gadget driver. Used by gadget drivers when
  1224. * registering themselves with the controller.
  1225. *
  1226. * -EINVAL something went wrong (not driver)
  1227. * -EBUSY another gadget is already using the controller
  1228. * -ENOMEM no memory to perform the operation
  1229. *
  1230. * @param driver the gadget driver
  1231. * @return <0 if error, 0 if everything is fine
  1232. */
  1233. static int musb_gadget_start(struct usb_gadget *g, struct usb_gadget_driver *driver)
  1234. {
  1235. struct musb *musb = gadget_to_musb(g);
  1236. struct usb_otg *otg = musb->xceiv->otg;
  1237. unsigned long flags;
  1238. int retval = 0;
  1239. mu3d_dbg(K_INFO, "%s %d\n", __func__, __LINE__);
  1240. if (driver->max_speed < USB_SPEED_HIGH) {
  1241. retval = -EINVAL;
  1242. goto err;
  1243. }
  1244. pm_runtime_get_sync(musb->controller);
  1245. mu3d_dbg(K_INFO, "%s %d\n", __func__, __LINE__);
  1246. dev_dbg(musb->controller, "registering driver %s\n", driver->function);
  1247. musb->softconnect = 0;
  1248. musb->gadget_driver = driver;
  1249. spin_lock_irqsave(&musb->lock, flags);
  1250. musb->is_active = 1;
  1251. otg_set_peripheral(otg, &musb->g);
  1252. musb->xceiv->state = OTG_STATE_B_IDLE;
  1253. /**
  1254. * if there is no iddig to detect usb plug in/out on box, start it directly if init as device;
  1255. * if it's on power saving mode and not plug in cable when bootup system,
  1256. * LDO & clock will be disabled.
  1257. */
  1258. if (musb->ssusb->is_power_saving_mode)
  1259. queue_delayed_work(musb->wq, &musb->connection_work, 0);
  1260. else if (musb->start_mu3d)
  1261. musb_start(musb);
  1262. else if (is_init_host_for_manual_otg(musb->ssusb))
  1263. mu3d_hal_ssusb_dis(musb);
  1264. spin_unlock_irqrestore(&musb->lock, flags);
  1265. if (musb->xceiv->last_event == USB_EVENT_NONE)
  1266. pm_runtime_put(musb->controller);
  1267. return 0;
  1268. err:
  1269. return retval;
  1270. }
  1271. static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
  1272. {
  1273. int i;
  1274. struct musb_hw_ep *hw_ep;
  1275. /* don't disconnect if it's not connected */
  1276. if (musb->g.speed == USB_SPEED_UNKNOWN)
  1277. driver = NULL;
  1278. else
  1279. musb->g.speed = USB_SPEED_UNKNOWN;
  1280. /* deactivate the hardware */
  1281. if (musb->softconnect) {
  1282. musb->softconnect = 0;
  1283. musb_dev_on_off(musb, 0);
  1284. }
  1285. musb_stop(musb);
  1286. /* killing any outstanding requests will quiesce the driver;
  1287. * then report disconnect
  1288. */
  1289. if (driver) {
  1290. for (i = 0, hw_ep = musb->endpoints; i < musb->nr_endpoints; i++, hw_ep++) {
  1291. if (hw_ep->is_shared_fifo /* || !epnum */) {
  1292. nuke(&hw_ep->ep_in, -ESHUTDOWN);
  1293. } else {
  1294. if (hw_ep->max_packet_sz_tx)
  1295. nuke(&hw_ep->ep_in, -ESHUTDOWN);
  1296. if (hw_ep->max_packet_sz_rx)
  1297. nuke(&hw_ep->ep_out, -ESHUTDOWN);
  1298. }
  1299. }
  1300. }
  1301. }
  1302. /*
  1303. * Unregister the gadget driver. Used by gadget drivers when
  1304. * unregistering themselves from the controller.
  1305. *
  1306. * @param driver the gadget driver to unregister
  1307. */
  1308. static int musb_gadget_stop(struct usb_gadget *g, struct usb_gadget_driver *driver)
  1309. {
  1310. struct musb *musb = gadget_to_musb(g);
  1311. unsigned long flags;
  1312. if (musb->xceiv->last_event == USB_EVENT_NONE)
  1313. pm_runtime_get_sync(musb->controller);
  1314. mu3d_dbg(K_INFO, "%s\n", __func__);
  1315. /*
  1316. * REVISIT always use otg_set_peripheral() here too;
  1317. * this needs to shut down the OTG engine.
  1318. */
  1319. spin_lock_irqsave(&musb->lock, flags);
  1320. /* musb_hnp_stop(musb); */
  1321. musb_gadget_vbus_draw(&musb->g, 0);
  1322. musb->xceiv->state = OTG_STATE_UNDEFINED;
  1323. stop_activity(musb, driver);
  1324. otg_set_peripheral(musb->xceiv->otg, NULL);
  1325. dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
  1326. musb->is_active = 0;
  1327. musb_platform_try_idle(musb, 0);
  1328. spin_unlock_irqrestore(&musb->lock, flags);
  1329. if (musb->start_mu3d)
  1330. musb_stop(musb);
  1331. pm_runtime_put(musb->controller);
  1332. return 0;
  1333. }
  1334. /* ----------------------------------------------------------------------- */
  1335. /* lifecycle operations called through plat_uds.c */
  1336. void musb_g_resume(struct musb *musb)
  1337. {
  1338. musb->is_suspended = 0;
  1339. switch (musb->xceiv->state) {
  1340. case OTG_STATE_B_IDLE:
  1341. break;
  1342. /* case OTG_STATE_B_WAIT_ACON: */
  1343. case OTG_STATE_B_PERIPHERAL:
  1344. musb->is_active = 1;
  1345. if (musb->gadget_driver && musb->gadget_driver->resume) {
  1346. spin_unlock(&musb->lock);
  1347. musb->gadget_driver->resume(&musb->g);
  1348. spin_lock(&musb->lock);
  1349. }
  1350. break;
  1351. default:
  1352. mu3d_dbg(K_ERR, "unhandled RESUME transition (%s)\n",
  1353. usb_otg_state_string(musb->xceiv->state));
  1354. }
  1355. }
  1356. /* called when SOF packets stop for 3+ msec */
  1357. void musb_g_suspend(struct musb *musb)
  1358. {
  1359. u32 devctl;
  1360. devctl = mu3d_readl(musb->mac_base, U3D_DEVICE_CONTROL);
  1361. dev_notice(musb->controller, "devctl %02x\n", devctl);
  1362. switch (musb->xceiv->state) {
  1363. case OTG_STATE_B_IDLE:
  1364. if ((devctl & VBUS) == USB_DEVCTL_VBUSVALID)
  1365. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  1366. break;
  1367. case OTG_STATE_B_PERIPHERAL:
  1368. musb->is_suspended = 1;
  1369. if (musb->gadget_driver && musb->gadget_driver->suspend) {
  1370. spin_unlock(&musb->lock);
  1371. musb->gadget_driver->suspend(&musb->g);
  1372. spin_lock(&musb->lock);
  1373. }
  1374. musb_sync_with_bat(musb, USB_SUSPEND); /* annonce to battery */
  1375. break;
  1376. default:
  1377. /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
  1378. * A_PERIPHERAL may need care too
  1379. */
  1380. mu3d_dbg(K_ERR, "unhandled SUSPEND transition (%s)\n",
  1381. usb_otg_state_string(musb->xceiv->state));
  1382. }
  1383. }
  1384. /* Called during SRP */
  1385. void musb_g_wakeup(struct musb *musb)
  1386. {
  1387. musb_gadget_wakeup(&musb->g);
  1388. }
  1389. /* called when VBUS drops below session threshold, and in other cases */
  1390. void musb_g_disconnect(struct musb *musb)
  1391. {
  1392. u32 devctl = mu3d_readl(musb->mac_base, U3D_DEVICE_CONTROL);
  1393. dev_notice(musb->controller, "devctl %02x\n", devctl);
  1394. /* clear HR */
  1395. /* marked off for 3.0 reset device test */
  1396. /* os_writel(U3D_DEVICE_CONTROL, devctl & SESSION); */
  1397. /* don't draw vbus until new b-default session */
  1398. (void)musb_gadget_vbus_draw(&musb->g, 0);
  1399. musb->g.speed = USB_SPEED_UNKNOWN;
  1400. if (musb->gadget_driver && musb->gadget_driver->disconnect) {
  1401. spin_unlock(&musb->lock);
  1402. musb->gadget_driver->disconnect(&musb->g);
  1403. spin_lock(&musb->lock);
  1404. }
  1405. switch (musb->xceiv->state) {
  1406. default:
  1407. dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
  1408. usb_otg_state_string(musb->xceiv->state));
  1409. break;
  1410. case OTG_STATE_B_PERIPHERAL:
  1411. case OTG_STATE_B_IDLE:
  1412. musb->xceiv->state = OTG_STATE_B_IDLE;
  1413. break;
  1414. case OTG_STATE_B_SRP_INIT:
  1415. break;
  1416. }
  1417. musb->is_active = 0;
  1418. }
  1419. void musb_conifg_ep0(struct musb *musb)
  1420. {
  1421. void __iomem *mbase = musb->mac_base;
  1422. mu3d_dbg(K_DEBUG, "U3D_DEVICE_CONF: %x\n", mu3d_readl(mbase, U3D_DEVICE_CONF));
  1423. if (mu3d_readl(mbase, U3D_DEVICE_CONF) & HW_USB2_3_SEL) { /* SS */
  1424. musb->g.speed = USB_SPEED_SUPER;
  1425. musb->g.ep0->maxpacket = 512;
  1426. mu3d_dbg(K_INFO, "musb_g_reset musb->g.speed: super\n");
  1427. ep0_setup(musb, musb->endpoints, &ep0_cfg_u3);
  1428. } else { /* HS, FS */
  1429. musb->g.speed = (u8) (mu3d_readl(mbase, U3D_POWER_MANAGEMENT) & HS_MODE)
  1430. ? USB_SPEED_HIGH : USB_SPEED_FULL;
  1431. musb->g.ep0->maxpacket = 64;
  1432. mu3d_dbg(K_INFO, "musb_g_reset musb->g.speed: %s\n",
  1433. (musb->g.speed == USB_SPEED_HIGH) ? "high" : "full");
  1434. ep0_setup(musb, musb->endpoints, &ep0_cfg_u2);
  1435. }
  1436. mu3d_dbg(K_DEBUG, "U3D_EP0CSR: %x\n", mu3d_readl(mbase, U3D_EP0CSR));
  1437. mu3d_dbg(K_DEBUG, "U3D_RXCOUNT0: %x\n", mu3d_readl(mbase, U3D_RXCOUNT0));
  1438. }
  1439. void musb_g_reset(struct musb *musb) __releases(musb->lock) __acquires(musb->lock)
  1440. {
  1441. /* u32 devctl = os_readl(U3D_DEVICE_CONTROL); */
  1442. /* it may greater than 2.5mA , but it should meet the spec's requirement !! */
  1443. if (musb->test_mode == 0)
  1444. musb_sync_with_bat(musb, USB_UNCONFIGURED);
  1445. mu3d_hal_unfigured_ep(musb);
  1446. /* report disconnect, if we didn't already (flushing EP state) */
  1447. if (musb->g.speed != USB_SPEED_UNKNOWN)
  1448. musb_g_disconnect(musb);
  1449. /* clear HR */
  1450. /* else if (devctl & HOSTREQ) { */
  1451. /* marked off for 3.0 reset device test */
  1452. /* os_writel(U3D_DEVICE_CONTROL, SESSION); */
  1453. /* } */
  1454. musb_conifg_ep0(musb);
  1455. /* start in USB_STATE_DEFAULT */
  1456. musb->is_active = 1;
  1457. musb->is_suspended = 0;
  1458. /* MUSB_DEV_MODE(musb); */
  1459. musb->address = 0;
  1460. musb->ep0_state = MUSB_EP0_STAGE_SETUP;
  1461. musb->may_wakeup = 0;
  1462. musb->g.b_hnp_enable = 0;
  1463. musb->g.a_alt_hnp_support = 0;
  1464. musb->g.a_hnp_support = 0;
  1465. /* Normal reset, as B-Device;
  1466. * or else after HNP, as A-Device
  1467. */
  1468. #if 0
  1469. if (devctl & B_DEV) {
  1470. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  1471. musb->g.is_a_peripheral = 0;
  1472. } else if (is_otg_enabled(musb)) {
  1473. musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
  1474. musb->g.is_a_peripheral = 1;
  1475. } else
  1476. WARN_ON(1);
  1477. #endif
  1478. /* only as b-peripheral */
  1479. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  1480. musb->g.is_a_peripheral = 0;
  1481. /* start with default limits on VBUS power draw */
  1482. (void)musb_gadget_vbus_draw(&musb->g, is_otg_enabled(musb) ? 8 : 100);
  1483. }