mv_udc_core.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526
  1. /*
  2. * Copyright (C) 2011 Marvell International Ltd. All rights reserved.
  3. * Author: Chao Xie <chao.xie@marvell.com>
  4. * Neil Zhang <zhangwm@marvell.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/dmapool.h>
  15. #include <linux/kernel.h>
  16. #include <linux/delay.h>
  17. #include <linux/ioport.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/errno.h>
  21. #include <linux/init.h>
  22. #include <linux/timer.h>
  23. #include <linux/list.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/device.h>
  27. #include <linux/usb/ch9.h>
  28. #include <linux/usb/gadget.h>
  29. #include <linux/usb/otg.h>
  30. #include <linux/pm.h>
  31. #include <linux/io.h>
  32. #include <linux/irq.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/clk.h>
  35. #include <linux/platform_data/mv_usb.h>
  36. #include <asm/system.h>
  37. #include <asm/unaligned.h>
  38. #include "mv_udc.h"
  39. #define DRIVER_DESC "Marvell PXA USB Device Controller driver"
  40. #define DRIVER_VERSION "8 Nov 2010"
  41. #define ep_dir(ep) (((ep)->ep_num == 0) ? \
  42. ((ep)->udc->ep0_dir) : ((ep)->direction))
  43. /* timeout value -- usec */
  44. #define RESET_TIMEOUT 10000
  45. #define FLUSH_TIMEOUT 10000
  46. #define EPSTATUS_TIMEOUT 10000
  47. #define PRIME_TIMEOUT 10000
  48. #define READSAFE_TIMEOUT 1000
  49. #define DTD_TIMEOUT 1000
  50. #define LOOPS_USEC_SHIFT 4
  51. #define LOOPS_USEC (1 << LOOPS_USEC_SHIFT)
  52. #define LOOPS(timeout) ((timeout) >> LOOPS_USEC_SHIFT)
  53. static DECLARE_COMPLETION(release_done);
  54. static const char driver_name[] = "mv_udc";
  55. static const char driver_desc[] = DRIVER_DESC;
  56. /* controller device global variable */
  57. static struct mv_udc *the_controller;
  58. int mv_usb_otgsc;
  59. static void nuke(struct mv_ep *ep, int status);
  60. static void stop_activity(struct mv_udc *udc, struct usb_gadget_driver *driver);
  61. /* for endpoint 0 operations */
  62. static const struct usb_endpoint_descriptor mv_ep0_desc = {
  63. .bLength = USB_DT_ENDPOINT_SIZE,
  64. .bDescriptorType = USB_DT_ENDPOINT,
  65. .bEndpointAddress = 0,
  66. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  67. .wMaxPacketSize = EP0_MAX_PKT_SIZE,
  68. };
  69. static void ep0_reset(struct mv_udc *udc)
  70. {
  71. struct mv_ep *ep;
  72. u32 epctrlx;
  73. int i = 0;
  74. /* ep0 in and out */
  75. for (i = 0; i < 2; i++) {
  76. ep = &udc->eps[i];
  77. ep->udc = udc;
  78. /* ep0 dQH */
  79. ep->dqh = &udc->ep_dqh[i];
  80. /* configure ep0 endpoint capabilities in dQH */
  81. ep->dqh->max_packet_length =
  82. (EP0_MAX_PKT_SIZE << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
  83. | EP_QUEUE_HEAD_IOS;
  84. ep->dqh->next_dtd_ptr = EP_QUEUE_HEAD_NEXT_TERMINATE;
  85. epctrlx = readl(&udc->op_regs->epctrlx[0]);
  86. if (i) { /* TX */
  87. epctrlx |= EPCTRL_TX_ENABLE
  88. | (USB_ENDPOINT_XFER_CONTROL
  89. << EPCTRL_TX_EP_TYPE_SHIFT);
  90. } else { /* RX */
  91. epctrlx |= EPCTRL_RX_ENABLE
  92. | (USB_ENDPOINT_XFER_CONTROL
  93. << EPCTRL_RX_EP_TYPE_SHIFT);
  94. }
  95. writel(epctrlx, &udc->op_regs->epctrlx[0]);
  96. }
  97. }
  98. /* protocol ep0 stall, will automatically be cleared on new transaction */
  99. static void ep0_stall(struct mv_udc *udc)
  100. {
  101. u32 epctrlx;
  102. /* set TX and RX to stall */
  103. epctrlx = readl(&udc->op_regs->epctrlx[0]);
  104. epctrlx |= EPCTRL_RX_EP_STALL | EPCTRL_TX_EP_STALL;
  105. writel(epctrlx, &udc->op_regs->epctrlx[0]);
  106. /* update ep0 state */
  107. udc->ep0_state = WAIT_FOR_SETUP;
  108. udc->ep0_dir = EP_DIR_OUT;
  109. }
  110. static int process_ep_req(struct mv_udc *udc, int index,
  111. struct mv_req *curr_req)
  112. {
  113. struct mv_dtd *curr_dtd;
  114. struct mv_dqh *curr_dqh;
  115. int td_complete, actual, remaining_length;
  116. int i, direction;
  117. int retval = 0;
  118. u32 errors;
  119. u32 bit_pos;
  120. curr_dqh = &udc->ep_dqh[index];
  121. direction = index % 2;
  122. curr_dtd = curr_req->head;
  123. td_complete = 0;
  124. actual = curr_req->req.length;
  125. for (i = 0; i < curr_req->dtd_count; i++) {
  126. if (curr_dtd->size_ioc_sts & DTD_STATUS_ACTIVE) {
  127. dev_dbg(&udc->dev->dev, "%s, dTD not completed\n",
  128. udc->eps[index].name);
  129. return 1;
  130. }
  131. errors = curr_dtd->size_ioc_sts & DTD_ERROR_MASK;
  132. if (!errors) {
  133. remaining_length =
  134. (curr_dtd->size_ioc_sts & DTD_PACKET_SIZE)
  135. >> DTD_LENGTH_BIT_POS;
  136. actual -= remaining_length;
  137. if (remaining_length) {
  138. if (direction) {
  139. dev_dbg(&udc->dev->dev,
  140. "TX dTD remains data\n");
  141. retval = -EPROTO;
  142. break;
  143. } else
  144. break;
  145. }
  146. } else {
  147. dev_info(&udc->dev->dev,
  148. "complete_tr error: ep=%d %s: error = 0x%x\n",
  149. index >> 1, direction ? "SEND" : "RECV",
  150. errors);
  151. if (errors & DTD_STATUS_HALTED) {
  152. /* Clear the errors and Halt condition */
  153. curr_dqh->size_ioc_int_sts &= ~errors;
  154. retval = -EPIPE;
  155. } else if (errors & DTD_STATUS_DATA_BUFF_ERR) {
  156. retval = -EPROTO;
  157. } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
  158. retval = -EILSEQ;
  159. }
  160. }
  161. if (i != curr_req->dtd_count - 1)
  162. curr_dtd = (struct mv_dtd *)curr_dtd->next_dtd_virt;
  163. }
  164. if (retval)
  165. return retval;
  166. if (direction == EP_DIR_OUT)
  167. bit_pos = 1 << curr_req->ep->ep_num;
  168. else
  169. bit_pos = 1 << (16 + curr_req->ep->ep_num);
  170. while ((curr_dqh->curr_dtd_ptr == curr_dtd->td_dma)) {
  171. if (curr_dtd->dtd_next == EP_QUEUE_HEAD_NEXT_TERMINATE) {
  172. while (readl(&udc->op_regs->epstatus) & bit_pos)
  173. udelay(1);
  174. break;
  175. }
  176. udelay(1);
  177. }
  178. curr_req->req.actual = actual;
  179. return 0;
  180. }
  181. /*
  182. * done() - retire a request; caller blocked irqs
  183. * @status : request status to be set, only works when
  184. * request is still in progress.
  185. */
  186. static void done(struct mv_ep *ep, struct mv_req *req, int status)
  187. {
  188. struct mv_udc *udc = NULL;
  189. unsigned char stopped = ep->stopped;
  190. struct mv_dtd *curr_td, *next_td;
  191. int j;
  192. udc = (struct mv_udc *)ep->udc;
  193. /* Removed the req from fsl_ep->queue */
  194. list_del_init(&req->queue);
  195. /* req.status should be set as -EINPROGRESS in ep_queue() */
  196. if (req->req.status == -EINPROGRESS)
  197. req->req.status = status;
  198. else
  199. status = req->req.status;
  200. /* Free dtd for the request */
  201. next_td = req->head;
  202. for (j = 0; j < req->dtd_count; j++) {
  203. curr_td = next_td;
  204. if (j != req->dtd_count - 1)
  205. next_td = curr_td->next_dtd_virt;
  206. dma_pool_free(udc->dtd_pool, curr_td, curr_td->td_dma);
  207. }
  208. if (req->mapped) {
  209. dma_unmap_single(ep->udc->gadget.dev.parent,
  210. req->req.dma, req->req.length,
  211. ((ep_dir(ep) == EP_DIR_IN) ?
  212. DMA_TO_DEVICE : DMA_FROM_DEVICE));
  213. req->req.dma = DMA_ADDR_INVALID;
  214. req->mapped = 0;
  215. } else
  216. dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
  217. req->req.dma, req->req.length,
  218. ((ep_dir(ep) == EP_DIR_IN) ?
  219. DMA_TO_DEVICE : DMA_FROM_DEVICE));
  220. if (status && (status != -ESHUTDOWN))
  221. dev_info(&udc->dev->dev, "complete %s req %p stat %d len %u/%u",
  222. ep->ep.name, &req->req, status,
  223. req->req.actual, req->req.length);
  224. ep->stopped = 1;
  225. spin_unlock(&ep->udc->lock);
  226. /*
  227. * complete() is from gadget layer,
  228. * eg fsg->bulk_in_complete()
  229. */
  230. if (req->req.complete)
  231. req->req.complete(&ep->ep, &req->req);
  232. spin_lock(&ep->udc->lock);
  233. ep->stopped = stopped;
  234. }
  235. static int queue_dtd(struct mv_ep *ep, struct mv_req *req)
  236. {
  237. u32 tmp, epstatus, bit_pos, direction;
  238. struct mv_udc *udc;
  239. struct mv_dqh *dqh;
  240. unsigned int loops;
  241. int readsafe, retval = 0;
  242. udc = ep->udc;
  243. direction = ep_dir(ep);
  244. dqh = &(udc->ep_dqh[ep->ep_num * 2 + direction]);
  245. bit_pos = 1 << (((direction == EP_DIR_OUT) ? 0 : 16) + ep->ep_num);
  246. /* check if the pipe is empty */
  247. if (!(list_empty(&ep->queue))) {
  248. struct mv_req *lastreq;
  249. lastreq = list_entry(ep->queue.prev, struct mv_req, queue);
  250. lastreq->tail->dtd_next =
  251. req->head->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
  252. if (readl(&udc->op_regs->epprime) & bit_pos) {
  253. loops = LOOPS(PRIME_TIMEOUT);
  254. while (readl(&udc->op_regs->epprime) & bit_pos) {
  255. if (loops == 0) {
  256. retval = -ETIME;
  257. goto done;
  258. }
  259. udelay(LOOPS_USEC);
  260. loops--;
  261. }
  262. if (readl(&udc->op_regs->epstatus) & bit_pos)
  263. goto done;
  264. }
  265. readsafe = 0;
  266. loops = LOOPS(READSAFE_TIMEOUT);
  267. while (readsafe == 0) {
  268. if (loops == 0) {
  269. retval = -ETIME;
  270. goto done;
  271. }
  272. /* start with setting the semaphores */
  273. tmp = readl(&udc->op_regs->usbcmd);
  274. tmp |= USBCMD_ATDTW_TRIPWIRE_SET;
  275. writel(tmp, &udc->op_regs->usbcmd);
  276. /* read the endpoint status */
  277. epstatus = readl(&udc->op_regs->epstatus) & bit_pos;
  278. /*
  279. * Reread the ATDTW semaphore bit to check if it is
  280. * cleared. When hardware see a hazard, it will clear
  281. * the bit or else we remain set to 1 and we can
  282. * proceed with priming of endpoint if not already
  283. * primed.
  284. */
  285. if (readl(&udc->op_regs->usbcmd)
  286. & USBCMD_ATDTW_TRIPWIRE_SET) {
  287. readsafe = 1;
  288. }
  289. loops--;
  290. udelay(LOOPS_USEC);
  291. }
  292. /* Clear the semaphore */
  293. tmp = readl(&udc->op_regs->usbcmd);
  294. tmp &= USBCMD_ATDTW_TRIPWIRE_CLEAR;
  295. writel(tmp, &udc->op_regs->usbcmd);
  296. /* If endpoint is not active, we activate it now. */
  297. if (!epstatus) {
  298. if (direction == EP_DIR_IN) {
  299. struct mv_dtd *curr_dtd = dma_to_virt(
  300. &udc->dev->dev, dqh->curr_dtd_ptr);
  301. loops = LOOPS(DTD_TIMEOUT);
  302. while (curr_dtd->size_ioc_sts
  303. & DTD_STATUS_ACTIVE) {
  304. if (loops == 0) {
  305. retval = -ETIME;
  306. goto done;
  307. }
  308. loops--;
  309. udelay(LOOPS_USEC);
  310. }
  311. }
  312. /* No other transfers on the queue */
  313. /* Write dQH next pointer and terminate bit to 0 */
  314. dqh->next_dtd_ptr = req->head->td_dma
  315. & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
  316. dqh->size_ioc_int_sts = 0;
  317. /*
  318. * Ensure that updates to the QH will
  319. * occur before priming.
  320. */
  321. wmb();
  322. /* Prime the Endpoint */
  323. writel(bit_pos, &udc->op_regs->epprime);
  324. }
  325. } else {
  326. /* Write dQH next pointer and terminate bit to 0 */
  327. dqh->next_dtd_ptr = req->head->td_dma
  328. & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
  329. dqh->size_ioc_int_sts = 0;
  330. /* Ensure that updates to the QH will occur before priming. */
  331. wmb();
  332. /* Prime the Endpoint */
  333. writel(bit_pos, &udc->op_regs->epprime);
  334. if (direction == EP_DIR_IN) {
  335. /* FIXME add status check after prime the IN ep */
  336. int prime_again;
  337. u32 curr_dtd_ptr = dqh->curr_dtd_ptr;
  338. loops = LOOPS(DTD_TIMEOUT);
  339. prime_again = 0;
  340. while ((curr_dtd_ptr != req->head->td_dma)) {
  341. curr_dtd_ptr = dqh->curr_dtd_ptr;
  342. if (loops == 0) {
  343. dev_err(&udc->dev->dev,
  344. "failed to prime %s\n",
  345. ep->name);
  346. retval = -ETIME;
  347. goto done;
  348. }
  349. loops--;
  350. udelay(LOOPS_USEC);
  351. if (loops == (LOOPS(DTD_TIMEOUT) >> 2)) {
  352. if (prime_again)
  353. goto done;
  354. dev_info(&udc->dev->dev,
  355. "prime again\n");
  356. writel(bit_pos,
  357. &udc->op_regs->epprime);
  358. prime_again = 1;
  359. }
  360. }
  361. }
  362. }
  363. done:
  364. return retval;
  365. }
  366. static struct mv_dtd *build_dtd(struct mv_req *req, unsigned *length,
  367. dma_addr_t *dma, int *is_last)
  368. {
  369. u32 temp;
  370. struct mv_dtd *dtd;
  371. struct mv_udc *udc;
  372. /* how big will this transfer be? */
  373. *length = min(req->req.length - req->req.actual,
  374. (unsigned)EP_MAX_LENGTH_TRANSFER);
  375. udc = req->ep->udc;
  376. /*
  377. * Be careful that no _GFP_HIGHMEM is set,
  378. * or we can not use dma_to_virt
  379. */
  380. dtd = dma_pool_alloc(udc->dtd_pool, GFP_KERNEL, dma);
  381. if (dtd == NULL)
  382. return dtd;
  383. dtd->td_dma = *dma;
  384. /* initialize buffer page pointers */
  385. temp = (u32)(req->req.dma + req->req.actual);
  386. dtd->buff_ptr0 = cpu_to_le32(temp);
  387. temp &= ~0xFFF;
  388. dtd->buff_ptr1 = cpu_to_le32(temp + 0x1000);
  389. dtd->buff_ptr2 = cpu_to_le32(temp + 0x2000);
  390. dtd->buff_ptr3 = cpu_to_le32(temp + 0x3000);
  391. dtd->buff_ptr4 = cpu_to_le32(temp + 0x4000);
  392. req->req.actual += *length;
  393. /* zlp is needed if req->req.zero is set */
  394. if (req->req.zero) {
  395. if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
  396. *is_last = 1;
  397. else
  398. *is_last = 0;
  399. } else if (req->req.length == req->req.actual)
  400. *is_last = 1;
  401. else
  402. *is_last = 0;
  403. /* Fill in the transfer size; set active bit */
  404. temp = ((*length << DTD_LENGTH_BIT_POS) | DTD_STATUS_ACTIVE);
  405. /* Enable interrupt for the last dtd of a request */
  406. if (*is_last && !req->req.no_interrupt)
  407. temp |= DTD_IOC;
  408. dtd->size_ioc_sts = temp;
  409. mb();
  410. return dtd;
  411. }
  412. /* generate dTD linked list for a request */
  413. static int req_to_dtd(struct mv_req *req)
  414. {
  415. unsigned count;
  416. int is_last, is_first = 1;
  417. struct mv_dtd *dtd, *last_dtd = NULL;
  418. struct mv_udc *udc;
  419. dma_addr_t dma;
  420. udc = req->ep->udc;
  421. do {
  422. dtd = build_dtd(req, &count, &dma, &is_last);
  423. if (dtd == NULL)
  424. return -ENOMEM;
  425. if (is_first) {
  426. is_first = 0;
  427. req->head = dtd;
  428. } else {
  429. last_dtd->dtd_next = dma;
  430. last_dtd->next_dtd_virt = dtd;
  431. }
  432. last_dtd = dtd;
  433. req->dtd_count++;
  434. } while (!is_last);
  435. /* set terminate bit to 1 for the last dTD */
  436. dtd->dtd_next = DTD_NEXT_TERMINATE;
  437. req->tail = dtd;
  438. return 0;
  439. }
  440. static int mv_ep_enable(struct usb_ep *_ep,
  441. const struct usb_endpoint_descriptor *desc)
  442. {
  443. struct mv_udc *udc;
  444. struct mv_ep *ep;
  445. struct mv_dqh *dqh;
  446. u16 max = 0;
  447. u32 bit_pos, epctrlx, direction;
  448. unsigned char zlt = 0, ios = 0, mult = 0;
  449. unsigned long flags;
  450. ep = container_of(_ep, struct mv_ep, ep);
  451. udc = ep->udc;
  452. if (!_ep || !desc || ep->desc
  453. || desc->bDescriptorType != USB_DT_ENDPOINT)
  454. return -EINVAL;
  455. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
  456. return -ESHUTDOWN;
  457. direction = ep_dir(ep);
  458. max = usb_endpoint_maxp(desc);
  459. /*
  460. * disable HW zero length termination select
  461. * driver handles zero length packet through req->req.zero
  462. */
  463. zlt = 1;
  464. bit_pos = 1 << ((direction == EP_DIR_OUT ? 0 : 16) + ep->ep_num);
  465. /* Check if the Endpoint is Primed */
  466. if ((readl(&udc->op_regs->epprime) & bit_pos)
  467. || (readl(&udc->op_regs->epstatus) & bit_pos)) {
  468. dev_info(&udc->dev->dev,
  469. "ep=%d %s: Init ERROR: ENDPTPRIME=0x%x,"
  470. " ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
  471. (unsigned)ep->ep_num, direction ? "SEND" : "RECV",
  472. (unsigned)readl(&udc->op_regs->epprime),
  473. (unsigned)readl(&udc->op_regs->epstatus),
  474. (unsigned)bit_pos);
  475. goto en_done;
  476. }
  477. /* Set the max packet length, interrupt on Setup and Mult fields */
  478. switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  479. case USB_ENDPOINT_XFER_BULK:
  480. zlt = 1;
  481. mult = 0;
  482. break;
  483. case USB_ENDPOINT_XFER_CONTROL:
  484. ios = 1;
  485. case USB_ENDPOINT_XFER_INT:
  486. mult = 0;
  487. break;
  488. case USB_ENDPOINT_XFER_ISOC:
  489. /* Calculate transactions needed for high bandwidth iso */
  490. mult = (unsigned char)(1 + ((max >> 11) & 0x03));
  491. max = max & 0x7ff; /* bit 0~10 */
  492. /* 3 transactions at most */
  493. if (mult > 3)
  494. goto en_done;
  495. break;
  496. default:
  497. goto en_done;
  498. }
  499. spin_lock_irqsave(&udc->lock, flags);
  500. /* Get the endpoint queue head address */
  501. dqh = ep->dqh;
  502. dqh->max_packet_length = (max << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
  503. | (mult << EP_QUEUE_HEAD_MULT_POS)
  504. | (zlt ? EP_QUEUE_HEAD_ZLT_SEL : 0)
  505. | (ios ? EP_QUEUE_HEAD_IOS : 0);
  506. dqh->next_dtd_ptr = 1;
  507. dqh->size_ioc_int_sts = 0;
  508. ep->ep.maxpacket = max;
  509. ep->desc = desc;
  510. ep->stopped = 0;
  511. /* Enable the endpoint for Rx or Tx and set the endpoint type */
  512. epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
  513. if (direction == EP_DIR_IN) {
  514. epctrlx &= ~EPCTRL_TX_ALL_MASK;
  515. epctrlx |= EPCTRL_TX_ENABLE | EPCTRL_TX_DATA_TOGGLE_RST
  516. | ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  517. << EPCTRL_TX_EP_TYPE_SHIFT);
  518. } else {
  519. epctrlx &= ~EPCTRL_RX_ALL_MASK;
  520. epctrlx |= EPCTRL_RX_ENABLE | EPCTRL_RX_DATA_TOGGLE_RST
  521. | ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  522. << EPCTRL_RX_EP_TYPE_SHIFT);
  523. }
  524. writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
  525. /*
  526. * Implement Guideline (GL# USB-7) The unused endpoint type must
  527. * be programmed to bulk.
  528. */
  529. epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
  530. if ((epctrlx & EPCTRL_RX_ENABLE) == 0) {
  531. epctrlx |= (USB_ENDPOINT_XFER_BULK
  532. << EPCTRL_RX_EP_TYPE_SHIFT);
  533. writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
  534. }
  535. epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
  536. if ((epctrlx & EPCTRL_TX_ENABLE) == 0) {
  537. epctrlx |= (USB_ENDPOINT_XFER_BULK
  538. << EPCTRL_TX_EP_TYPE_SHIFT);
  539. writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
  540. }
  541. spin_unlock_irqrestore(&udc->lock, flags);
  542. return 0;
  543. en_done:
  544. return -EINVAL;
  545. }
  546. static int mv_ep_disable(struct usb_ep *_ep)
  547. {
  548. struct mv_udc *udc;
  549. struct mv_ep *ep;
  550. struct mv_dqh *dqh;
  551. u32 bit_pos, epctrlx, direction;
  552. unsigned long flags;
  553. ep = container_of(_ep, struct mv_ep, ep);
  554. if ((_ep == NULL) || !ep->desc)
  555. return -EINVAL;
  556. udc = ep->udc;
  557. /* Get the endpoint queue head address */
  558. dqh = ep->dqh;
  559. spin_lock_irqsave(&udc->lock, flags);
  560. direction = ep_dir(ep);
  561. bit_pos = 1 << ((direction == EP_DIR_OUT ? 0 : 16) + ep->ep_num);
  562. /* Reset the max packet length and the interrupt on Setup */
  563. dqh->max_packet_length = 0;
  564. /* Disable the endpoint for Rx or Tx and reset the endpoint type */
  565. epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
  566. epctrlx &= ~((direction == EP_DIR_IN)
  567. ? (EPCTRL_TX_ENABLE | EPCTRL_TX_TYPE)
  568. : (EPCTRL_RX_ENABLE | EPCTRL_RX_TYPE));
  569. writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
  570. /* nuke all pending requests (does flush) */
  571. nuke(ep, -ESHUTDOWN);
  572. ep->desc = NULL;
  573. ep->stopped = 1;
  574. spin_unlock_irqrestore(&udc->lock, flags);
  575. return 0;
  576. }
  577. static struct usb_request *
  578. mv_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  579. {
  580. struct mv_req *req = NULL;
  581. req = kzalloc(sizeof *req, gfp_flags);
  582. if (!req)
  583. return NULL;
  584. req->req.dma = DMA_ADDR_INVALID;
  585. INIT_LIST_HEAD(&req->queue);
  586. return &req->req;
  587. }
  588. static void mv_free_request(struct usb_ep *_ep, struct usb_request *_req)
  589. {
  590. struct mv_req *req = NULL;
  591. req = container_of(_req, struct mv_req, req);
  592. if (_req)
  593. kfree(req);
  594. }
  595. static void mv_ep_fifo_flush(struct usb_ep *_ep)
  596. {
  597. struct mv_udc *udc;
  598. u32 bit_pos, direction;
  599. struct mv_ep *ep;
  600. unsigned int loops;
  601. if (!_ep)
  602. return;
  603. ep = container_of(_ep, struct mv_ep, ep);
  604. if (!ep->desc)
  605. return;
  606. udc = ep->udc;
  607. direction = ep_dir(ep);
  608. if (ep->ep_num == 0)
  609. bit_pos = (1 << 16) | 1;
  610. else if (direction == EP_DIR_OUT)
  611. bit_pos = 1 << ep->ep_num;
  612. else
  613. bit_pos = 1 << (16 + ep->ep_num);
  614. loops = LOOPS(EPSTATUS_TIMEOUT);
  615. do {
  616. unsigned int inter_loops;
  617. if (loops == 0) {
  618. dev_err(&udc->dev->dev,
  619. "TIMEOUT for ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
  620. (unsigned)readl(&udc->op_regs->epstatus),
  621. (unsigned)bit_pos);
  622. return;
  623. }
  624. /* Write 1 to the Flush register */
  625. writel(bit_pos, &udc->op_regs->epflush);
  626. /* Wait until flushing completed */
  627. inter_loops = LOOPS(FLUSH_TIMEOUT);
  628. while (readl(&udc->op_regs->epflush)) {
  629. /*
  630. * ENDPTFLUSH bit should be cleared to indicate this
  631. * operation is complete
  632. */
  633. if (inter_loops == 0) {
  634. dev_err(&udc->dev->dev,
  635. "TIMEOUT for ENDPTFLUSH=0x%x,"
  636. "bit_pos=0x%x\n",
  637. (unsigned)readl(&udc->op_regs->epflush),
  638. (unsigned)bit_pos);
  639. return;
  640. }
  641. inter_loops--;
  642. udelay(LOOPS_USEC);
  643. }
  644. loops--;
  645. } while (readl(&udc->op_regs->epstatus) & bit_pos);
  646. }
  647. /* queues (submits) an I/O request to an endpoint */
  648. static int
  649. mv_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  650. {
  651. struct mv_ep *ep = container_of(_ep, struct mv_ep, ep);
  652. struct mv_req *req = container_of(_req, struct mv_req, req);
  653. struct mv_udc *udc = ep->udc;
  654. unsigned long flags;
  655. /* catch various bogus parameters */
  656. if (!_req || !req->req.complete || !req->req.buf
  657. || !list_empty(&req->queue)) {
  658. dev_err(&udc->dev->dev, "%s, bad params", __func__);
  659. return -EINVAL;
  660. }
  661. if (unlikely(!_ep || !ep->desc)) {
  662. dev_err(&udc->dev->dev, "%s, bad ep", __func__);
  663. return -EINVAL;
  664. }
  665. if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  666. if (req->req.length > ep->ep.maxpacket)
  667. return -EMSGSIZE;
  668. }
  669. udc = ep->udc;
  670. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
  671. return -ESHUTDOWN;
  672. req->ep = ep;
  673. /* map virtual address to hardware */
  674. if (req->req.dma == DMA_ADDR_INVALID) {
  675. req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
  676. req->req.buf,
  677. req->req.length, ep_dir(ep)
  678. ? DMA_TO_DEVICE
  679. : DMA_FROM_DEVICE);
  680. req->mapped = 1;
  681. } else {
  682. dma_sync_single_for_device(ep->udc->gadget.dev.parent,
  683. req->req.dma, req->req.length,
  684. ep_dir(ep)
  685. ? DMA_TO_DEVICE
  686. : DMA_FROM_DEVICE);
  687. req->mapped = 0;
  688. }
  689. req->req.status = -EINPROGRESS;
  690. req->req.actual = 0;
  691. req->dtd_count = 0;
  692. spin_lock_irqsave(&udc->lock, flags);
  693. /* build dtds and push them to device queue */
  694. if (!req_to_dtd(req)) {
  695. int retval;
  696. retval = queue_dtd(ep, req);
  697. if (retval) {
  698. spin_unlock_irqrestore(&udc->lock, flags);
  699. return retval;
  700. }
  701. } else {
  702. spin_unlock_irqrestore(&udc->lock, flags);
  703. return -ENOMEM;
  704. }
  705. /* Update ep0 state */
  706. if (ep->ep_num == 0)
  707. udc->ep0_state = DATA_STATE_XMIT;
  708. /* irq handler advances the queue */
  709. if (req != NULL)
  710. list_add_tail(&req->queue, &ep->queue);
  711. spin_unlock_irqrestore(&udc->lock, flags);
  712. return 0;
  713. }
  714. /* dequeues (cancels, unlinks) an I/O request from an endpoint */
  715. static int mv_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  716. {
  717. struct mv_ep *ep = container_of(_ep, struct mv_ep, ep);
  718. struct mv_req *req;
  719. struct mv_udc *udc = ep->udc;
  720. unsigned long flags;
  721. int stopped, ret = 0;
  722. u32 epctrlx;
  723. if (!_ep || !_req)
  724. return -EINVAL;
  725. spin_lock_irqsave(&ep->udc->lock, flags);
  726. stopped = ep->stopped;
  727. /* Stop the ep before we deal with the queue */
  728. ep->stopped = 1;
  729. epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
  730. if (ep_dir(ep) == EP_DIR_IN)
  731. epctrlx &= ~EPCTRL_TX_ENABLE;
  732. else
  733. epctrlx &= ~EPCTRL_RX_ENABLE;
  734. writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
  735. /* make sure it's actually queued on this endpoint */
  736. list_for_each_entry(req, &ep->queue, queue) {
  737. if (&req->req == _req)
  738. break;
  739. }
  740. if (&req->req != _req) {
  741. ret = -EINVAL;
  742. goto out;
  743. }
  744. /* The request is in progress, or completed but not dequeued */
  745. if (ep->queue.next == &req->queue) {
  746. _req->status = -ECONNRESET;
  747. mv_ep_fifo_flush(_ep); /* flush current transfer */
  748. /* The request isn't the last request in this ep queue */
  749. if (req->queue.next != &ep->queue) {
  750. struct mv_dqh *qh;
  751. struct mv_req *next_req;
  752. qh = ep->dqh;
  753. next_req = list_entry(req->queue.next, struct mv_req,
  754. queue);
  755. /* Point the QH to the first TD of next request */
  756. writel((u32) next_req->head, &qh->curr_dtd_ptr);
  757. } else {
  758. struct mv_dqh *qh;
  759. qh = ep->dqh;
  760. qh->next_dtd_ptr = 1;
  761. qh->size_ioc_int_sts = 0;
  762. }
  763. /* The request hasn't been processed, patch up the TD chain */
  764. } else {
  765. struct mv_req *prev_req;
  766. prev_req = list_entry(req->queue.prev, struct mv_req, queue);
  767. writel(readl(&req->tail->dtd_next),
  768. &prev_req->tail->dtd_next);
  769. }
  770. done(ep, req, -ECONNRESET);
  771. /* Enable EP */
  772. out:
  773. epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
  774. if (ep_dir(ep) == EP_DIR_IN)
  775. epctrlx |= EPCTRL_TX_ENABLE;
  776. else
  777. epctrlx |= EPCTRL_RX_ENABLE;
  778. writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
  779. ep->stopped = stopped;
  780. spin_unlock_irqrestore(&ep->udc->lock, flags);
  781. return ret;
  782. }
  783. static void ep_set_stall(struct mv_udc *udc, u8 ep_num, u8 direction, int stall)
  784. {
  785. u32 epctrlx;
  786. epctrlx = readl(&udc->op_regs->epctrlx[ep_num]);
  787. if (stall) {
  788. if (direction == EP_DIR_IN)
  789. epctrlx |= EPCTRL_TX_EP_STALL;
  790. else
  791. epctrlx |= EPCTRL_RX_EP_STALL;
  792. } else {
  793. if (direction == EP_DIR_IN) {
  794. epctrlx &= ~EPCTRL_TX_EP_STALL;
  795. epctrlx |= EPCTRL_TX_DATA_TOGGLE_RST;
  796. } else {
  797. epctrlx &= ~EPCTRL_RX_EP_STALL;
  798. epctrlx |= EPCTRL_RX_DATA_TOGGLE_RST;
  799. }
  800. }
  801. writel(epctrlx, &udc->op_regs->epctrlx[ep_num]);
  802. }
  803. static int ep_is_stall(struct mv_udc *udc, u8 ep_num, u8 direction)
  804. {
  805. u32 epctrlx;
  806. epctrlx = readl(&udc->op_regs->epctrlx[ep_num]);
  807. if (direction == EP_DIR_OUT)
  808. return (epctrlx & EPCTRL_RX_EP_STALL) ? 1 : 0;
  809. else
  810. return (epctrlx & EPCTRL_TX_EP_STALL) ? 1 : 0;
  811. }
  812. static int mv_ep_set_halt_wedge(struct usb_ep *_ep, int halt, int wedge)
  813. {
  814. struct mv_ep *ep;
  815. unsigned long flags = 0;
  816. int status = 0;
  817. struct mv_udc *udc;
  818. ep = container_of(_ep, struct mv_ep, ep);
  819. udc = ep->udc;
  820. if (!_ep || !ep->desc) {
  821. status = -EINVAL;
  822. goto out;
  823. }
  824. if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  825. status = -EOPNOTSUPP;
  826. goto out;
  827. }
  828. /*
  829. * Attempt to halt IN ep will fail if any transfer requests
  830. * are still queue
  831. */
  832. if (halt && (ep_dir(ep) == EP_DIR_IN) && !list_empty(&ep->queue)) {
  833. status = -EAGAIN;
  834. goto out;
  835. }
  836. spin_lock_irqsave(&ep->udc->lock, flags);
  837. ep_set_stall(udc, ep->ep_num, ep_dir(ep), halt);
  838. if (halt && wedge)
  839. ep->wedge = 1;
  840. else if (!halt)
  841. ep->wedge = 0;
  842. spin_unlock_irqrestore(&ep->udc->lock, flags);
  843. if (ep->ep_num == 0) {
  844. udc->ep0_state = WAIT_FOR_SETUP;
  845. udc->ep0_dir = EP_DIR_OUT;
  846. }
  847. out:
  848. return status;
  849. }
  850. static int mv_ep_set_halt(struct usb_ep *_ep, int halt)
  851. {
  852. return mv_ep_set_halt_wedge(_ep, halt, 0);
  853. }
  854. static int mv_ep_set_wedge(struct usb_ep *_ep)
  855. {
  856. return mv_ep_set_halt_wedge(_ep, 1, 1);
  857. }
  858. static struct usb_ep_ops mv_ep_ops = {
  859. .enable = mv_ep_enable,
  860. .disable = mv_ep_disable,
  861. .alloc_request = mv_alloc_request,
  862. .free_request = mv_free_request,
  863. .queue = mv_ep_queue,
  864. .dequeue = mv_ep_dequeue,
  865. .set_wedge = mv_ep_set_wedge,
  866. .set_halt = mv_ep_set_halt,
  867. .fifo_flush = mv_ep_fifo_flush, /* flush fifo */
  868. };
  869. static void udc_clock_enable(struct mv_udc *udc)
  870. {
  871. unsigned int i;
  872. for (i = 0; i < udc->clknum; i++)
  873. clk_enable(udc->clk[i]);
  874. }
  875. static void udc_clock_disable(struct mv_udc *udc)
  876. {
  877. unsigned int i;
  878. for (i = 0; i < udc->clknum; i++)
  879. clk_disable(udc->clk[i]);
  880. }
  881. static void udc_stop(struct mv_udc *udc)
  882. {
  883. u32 tmp;
  884. /* Disable interrupts */
  885. tmp = readl(&udc->op_regs->usbintr);
  886. tmp &= ~(USBINTR_INT_EN | USBINTR_ERR_INT_EN |
  887. USBINTR_PORT_CHANGE_DETECT_EN | USBINTR_RESET_EN);
  888. writel(tmp, &udc->op_regs->usbintr);
  889. udc->stopped = 1;
  890. /* Reset the Run the bit in the command register to stop VUSB */
  891. tmp = readl(&udc->op_regs->usbcmd);
  892. tmp &= ~USBCMD_RUN_STOP;
  893. writel(tmp, &udc->op_regs->usbcmd);
  894. }
  895. static void udc_start(struct mv_udc *udc)
  896. {
  897. u32 usbintr;
  898. usbintr = USBINTR_INT_EN | USBINTR_ERR_INT_EN
  899. | USBINTR_PORT_CHANGE_DETECT_EN
  900. | USBINTR_RESET_EN | USBINTR_DEVICE_SUSPEND;
  901. /* Enable interrupts */
  902. writel(usbintr, &udc->op_regs->usbintr);
  903. udc->stopped = 0;
  904. /* Set the Run bit in the command register */
  905. writel(USBCMD_RUN_STOP, &udc->op_regs->usbcmd);
  906. }
  907. static int udc_reset(struct mv_udc *udc)
  908. {
  909. unsigned int loops;
  910. u32 tmp, portsc;
  911. /* Stop the controller */
  912. tmp = readl(&udc->op_regs->usbcmd);
  913. tmp &= ~USBCMD_RUN_STOP;
  914. writel(tmp, &udc->op_regs->usbcmd);
  915. /* Reset the controller to get default values */
  916. writel(USBCMD_CTRL_RESET, &udc->op_regs->usbcmd);
  917. /* wait for reset to complete */
  918. loops = LOOPS(RESET_TIMEOUT);
  919. while (readl(&udc->op_regs->usbcmd) & USBCMD_CTRL_RESET) {
  920. if (loops == 0) {
  921. dev_err(&udc->dev->dev,
  922. "Wait for RESET completed TIMEOUT\n");
  923. return -ETIMEDOUT;
  924. }
  925. loops--;
  926. udelay(LOOPS_USEC);
  927. }
  928. /* set controller to device mode */
  929. tmp = readl(&udc->op_regs->usbmode);
  930. tmp |= USBMODE_CTRL_MODE_DEVICE;
  931. /* turn setup lockout off, require setup tripwire in usbcmd */
  932. tmp |= USBMODE_SETUP_LOCK_OFF | USBMODE_STREAM_DISABLE;
  933. writel(tmp, &udc->op_regs->usbmode);
  934. writel(0x0, &udc->op_regs->epsetupstat);
  935. /* Configure the Endpoint List Address */
  936. writel(udc->ep_dqh_dma & USB_EP_LIST_ADDRESS_MASK,
  937. &udc->op_regs->eplistaddr);
  938. portsc = readl(&udc->op_regs->portsc[0]);
  939. if (readl(&udc->cap_regs->hcsparams) & HCSPARAMS_PPC)
  940. portsc &= (~PORTSCX_W1C_BITS | ~PORTSCX_PORT_POWER);
  941. if (udc->force_fs)
  942. portsc |= PORTSCX_FORCE_FULL_SPEED_CONNECT;
  943. else
  944. portsc &= (~PORTSCX_FORCE_FULL_SPEED_CONNECT);
  945. writel(portsc, &udc->op_regs->portsc[0]);
  946. tmp = readl(&udc->op_regs->epctrlx[0]);
  947. tmp &= ~(EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL);
  948. writel(tmp, &udc->op_regs->epctrlx[0]);
  949. return 0;
  950. }
  951. static int mv_udc_enable_internal(struct mv_udc *udc)
  952. {
  953. int retval;
  954. if (udc->active)
  955. return 0;
  956. dev_dbg(&udc->dev->dev, "enable udc\n");
  957. udc_clock_enable(udc);
  958. if (udc->pdata->phy_init) {
  959. retval = udc->pdata->phy_init(udc->phy_regs);
  960. if (retval) {
  961. dev_err(&udc->dev->dev,
  962. "init phy error %d\n", retval);
  963. udc_clock_disable(udc);
  964. return retval;
  965. }
  966. }
  967. udc->active = 1;
  968. return 0;
  969. }
  970. static int mv_udc_enable(struct mv_udc *udc)
  971. {
  972. if (udc->clock_gating)
  973. return mv_udc_enable_internal(udc);
  974. return 0;
  975. }
  976. static void mv_udc_disable_internal(struct mv_udc *udc)
  977. {
  978. if (udc->active) {
  979. dev_dbg(&udc->dev->dev, "disable udc\n");
  980. if (udc->pdata->phy_deinit)
  981. udc->pdata->phy_deinit(udc->phy_regs);
  982. udc_clock_disable(udc);
  983. udc->active = 0;
  984. }
  985. }
  986. static void mv_udc_disable(struct mv_udc *udc)
  987. {
  988. if (udc->clock_gating)
  989. mv_udc_disable_internal(udc);
  990. }
  991. static int mv_udc_get_frame(struct usb_gadget *gadget)
  992. {
  993. struct mv_udc *udc;
  994. u16 retval;
  995. if (!gadget)
  996. return -ENODEV;
  997. udc = container_of(gadget, struct mv_udc, gadget);
  998. retval = readl(udc->op_regs->frindex) & USB_FRINDEX_MASKS;
  999. return retval;
  1000. }
  1001. /* Tries to wake up the host connected to this gadget */
  1002. static int mv_udc_wakeup(struct usb_gadget *gadget)
  1003. {
  1004. struct mv_udc *udc = container_of(gadget, struct mv_udc, gadget);
  1005. u32 portsc;
  1006. /* Remote wakeup feature not enabled by host */
  1007. if (!udc->remote_wakeup)
  1008. return -ENOTSUPP;
  1009. portsc = readl(&udc->op_regs->portsc);
  1010. /* not suspended? */
  1011. if (!(portsc & PORTSCX_PORT_SUSPEND))
  1012. return 0;
  1013. /* trigger force resume */
  1014. portsc |= PORTSCX_PORT_FORCE_RESUME;
  1015. writel(portsc, &udc->op_regs->portsc[0]);
  1016. return 0;
  1017. }
  1018. static int mv_udc_vbus_session(struct usb_gadget *gadget, int is_active)
  1019. {
  1020. struct mv_udc *udc;
  1021. unsigned long flags;
  1022. int retval = 0;
  1023. udc = container_of(gadget, struct mv_udc, gadget);
  1024. spin_lock_irqsave(&udc->lock, flags);
  1025. dev_dbg(&udc->dev->dev, "%s: softconnect %d, vbus_active %d\n",
  1026. __func__, udc->softconnect, udc->vbus_active);
  1027. udc->vbus_active = (is_active != 0);
  1028. if (udc->driver && udc->softconnect && udc->vbus_active) {
  1029. retval = mv_udc_enable(udc);
  1030. if (retval == 0) {
  1031. /* Clock is disabled, need re-init registers */
  1032. udc_reset(udc);
  1033. ep0_reset(udc);
  1034. udc_start(udc);
  1035. }
  1036. } else if (udc->driver && udc->softconnect) {
  1037. /* stop all the transfer in queue*/
  1038. stop_activity(udc, udc->driver);
  1039. udc_stop(udc);
  1040. mv_udc_disable(udc);
  1041. }
  1042. spin_unlock_irqrestore(&udc->lock, flags);
  1043. return retval;
  1044. }
  1045. static int mv_udc_pullup(struct usb_gadget *gadget, int is_on)
  1046. {
  1047. struct mv_udc *udc;
  1048. unsigned long flags;
  1049. int retval = 0;
  1050. udc = container_of(gadget, struct mv_udc, gadget);
  1051. spin_lock_irqsave(&udc->lock, flags);
  1052. dev_dbg(&udc->dev->dev, "%s: softconnect %d, vbus_active %d\n",
  1053. __func__, udc->softconnect, udc->vbus_active);
  1054. udc->softconnect = (is_on != 0);
  1055. if (udc->driver && udc->softconnect && udc->vbus_active) {
  1056. retval = mv_udc_enable(udc);
  1057. if (retval == 0) {
  1058. /* Clock is disabled, need re-init registers */
  1059. udc_reset(udc);
  1060. ep0_reset(udc);
  1061. udc_start(udc);
  1062. }
  1063. } else if (udc->driver && udc->vbus_active) {
  1064. /* stop all the transfer in queue*/
  1065. stop_activity(udc, udc->driver);
  1066. udc_stop(udc);
  1067. mv_udc_disable(udc);
  1068. }
  1069. spin_unlock_irqrestore(&udc->lock, flags);
  1070. return retval;
  1071. }
  1072. static int mv_udc_start(struct usb_gadget_driver *driver,
  1073. int (*bind)(struct usb_gadget *));
  1074. static int mv_udc_stop(struct usb_gadget_driver *driver);
  1075. /* device controller usb_gadget_ops structure */
  1076. static const struct usb_gadget_ops mv_ops = {
  1077. /* returns the current frame number */
  1078. .get_frame = mv_udc_get_frame,
  1079. /* tries to wake up the host connected to this gadget */
  1080. .wakeup = mv_udc_wakeup,
  1081. /* notify controller that VBUS is powered or not */
  1082. .vbus_session = mv_udc_vbus_session,
  1083. /* D+ pullup, software-controlled connect/disconnect to USB host */
  1084. .pullup = mv_udc_pullup,
  1085. .start = mv_udc_start,
  1086. .stop = mv_udc_stop,
  1087. };
  1088. static int eps_init(struct mv_udc *udc)
  1089. {
  1090. struct mv_ep *ep;
  1091. char name[14];
  1092. int i;
  1093. /* initialize ep0 */
  1094. ep = &udc->eps[0];
  1095. ep->udc = udc;
  1096. strncpy(ep->name, "ep0", sizeof(ep->name));
  1097. ep->ep.name = ep->name;
  1098. ep->ep.ops = &mv_ep_ops;
  1099. ep->wedge = 0;
  1100. ep->stopped = 0;
  1101. ep->ep.maxpacket = EP0_MAX_PKT_SIZE;
  1102. ep->ep_num = 0;
  1103. ep->desc = &mv_ep0_desc;
  1104. INIT_LIST_HEAD(&ep->queue);
  1105. ep->ep_type = USB_ENDPOINT_XFER_CONTROL;
  1106. /* initialize other endpoints */
  1107. for (i = 2; i < udc->max_eps * 2; i++) {
  1108. ep = &udc->eps[i];
  1109. if (i % 2) {
  1110. snprintf(name, sizeof(name), "ep%din", i / 2);
  1111. ep->direction = EP_DIR_IN;
  1112. } else {
  1113. snprintf(name, sizeof(name), "ep%dout", i / 2);
  1114. ep->direction = EP_DIR_OUT;
  1115. }
  1116. ep->udc = udc;
  1117. strncpy(ep->name, name, sizeof(ep->name));
  1118. ep->ep.name = ep->name;
  1119. ep->ep.ops = &mv_ep_ops;
  1120. ep->stopped = 0;
  1121. ep->ep.maxpacket = (unsigned short) ~0;
  1122. ep->ep_num = i / 2;
  1123. INIT_LIST_HEAD(&ep->queue);
  1124. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  1125. ep->dqh = &udc->ep_dqh[i];
  1126. }
  1127. return 0;
  1128. }
  1129. /* delete all endpoint requests, called with spinlock held */
  1130. static void nuke(struct mv_ep *ep, int status)
  1131. {
  1132. /* called with spinlock held */
  1133. ep->stopped = 1;
  1134. /* endpoint fifo flush */
  1135. mv_ep_fifo_flush(&ep->ep);
  1136. while (!list_empty(&ep->queue)) {
  1137. struct mv_req *req = NULL;
  1138. req = list_entry(ep->queue.next, struct mv_req, queue);
  1139. done(ep, req, status);
  1140. }
  1141. }
  1142. /* stop all USB activities */
  1143. static void stop_activity(struct mv_udc *udc, struct usb_gadget_driver *driver)
  1144. {
  1145. struct mv_ep *ep;
  1146. nuke(&udc->eps[0], -ESHUTDOWN);
  1147. list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
  1148. nuke(ep, -ESHUTDOWN);
  1149. }
  1150. /* report disconnect; the driver is already quiesced */
  1151. if (driver) {
  1152. spin_unlock(&udc->lock);
  1153. driver->disconnect(&udc->gadget);
  1154. spin_lock(&udc->lock);
  1155. }
  1156. }
  1157. static int mv_udc_start(struct usb_gadget_driver *driver,
  1158. int (*bind)(struct usb_gadget *))
  1159. {
  1160. struct mv_udc *udc = the_controller;
  1161. int retval = 0;
  1162. unsigned long flags;
  1163. if (!udc)
  1164. return -ENODEV;
  1165. if (udc->driver)
  1166. return -EBUSY;
  1167. spin_lock_irqsave(&udc->lock, flags);
  1168. /* hook up the driver ... */
  1169. driver->driver.bus = NULL;
  1170. udc->driver = driver;
  1171. udc->gadget.dev.driver = &driver->driver;
  1172. udc->usb_state = USB_STATE_ATTACHED;
  1173. udc->ep0_state = WAIT_FOR_SETUP;
  1174. udc->ep0_dir = EP_DIR_OUT;
  1175. spin_unlock_irqrestore(&udc->lock, flags);
  1176. retval = bind(&udc->gadget);
  1177. if (retval) {
  1178. dev_err(&udc->dev->dev, "bind to driver %s --> %d\n",
  1179. driver->driver.name, retval);
  1180. udc->driver = NULL;
  1181. udc->gadget.dev.driver = NULL;
  1182. return retval;
  1183. }
  1184. if (udc->transceiver) {
  1185. retval = otg_set_peripheral(udc->transceiver, &udc->gadget);
  1186. if (retval) {
  1187. dev_err(&udc->dev->dev,
  1188. "unable to register peripheral to otg\n");
  1189. if (driver->unbind) {
  1190. driver->unbind(&udc->gadget);
  1191. udc->gadget.dev.driver = NULL;
  1192. udc->driver = NULL;
  1193. }
  1194. return retval;
  1195. }
  1196. }
  1197. /* pullup is always on */
  1198. mv_udc_pullup(&udc->gadget, 1);
  1199. /* When boot with cable attached, there will be no vbus irq occurred */
  1200. if (udc->qwork)
  1201. queue_work(udc->qwork, &udc->vbus_work);
  1202. return 0;
  1203. }
  1204. static int mv_udc_stop(struct usb_gadget_driver *driver)
  1205. {
  1206. struct mv_udc *udc = the_controller;
  1207. unsigned long flags;
  1208. if (!udc)
  1209. return -ENODEV;
  1210. spin_lock_irqsave(&udc->lock, flags);
  1211. mv_udc_enable(udc);
  1212. udc_stop(udc);
  1213. /* stop all usb activities */
  1214. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1215. stop_activity(udc, driver);
  1216. mv_udc_disable(udc);
  1217. spin_unlock_irqrestore(&udc->lock, flags);
  1218. /* unbind gadget driver */
  1219. driver->unbind(&udc->gadget);
  1220. udc->gadget.dev.driver = NULL;
  1221. udc->driver = NULL;
  1222. return 0;
  1223. }
  1224. static void mv_set_ptc(struct mv_udc *udc, u32 mode)
  1225. {
  1226. u32 portsc;
  1227. portsc = readl(&udc->op_regs->portsc[0]);
  1228. portsc |= mode << 16;
  1229. writel(portsc, &udc->op_regs->portsc[0]);
  1230. }
  1231. static void prime_status_complete(struct usb_ep *ep, struct usb_request *_req)
  1232. {
  1233. struct mv_udc *udc = the_controller;
  1234. struct mv_req *req = container_of(_req, struct mv_req, req);
  1235. unsigned long flags;
  1236. dev_info(&udc->dev->dev, "switch to test mode %d\n", req->test_mode);
  1237. spin_lock_irqsave(&udc->lock, flags);
  1238. if (req->test_mode) {
  1239. mv_set_ptc(udc, req->test_mode);
  1240. req->test_mode = 0;
  1241. }
  1242. spin_unlock_irqrestore(&udc->lock, flags);
  1243. }
  1244. static int
  1245. udc_prime_status(struct mv_udc *udc, u8 direction, u16 status, bool empty)
  1246. {
  1247. int retval = 0;
  1248. struct mv_req *req;
  1249. struct mv_ep *ep;
  1250. ep = &udc->eps[0];
  1251. udc->ep0_dir = direction;
  1252. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1253. req = udc->status_req;
  1254. /* fill in the reqest structure */
  1255. if (empty == false) {
  1256. *((u16 *) req->req.buf) = cpu_to_le16(status);
  1257. req->req.length = 2;
  1258. } else
  1259. req->req.length = 0;
  1260. req->ep = ep;
  1261. req->req.status = -EINPROGRESS;
  1262. req->req.actual = 0;
  1263. if (udc->test_mode) {
  1264. req->req.complete = prime_status_complete;
  1265. req->test_mode = udc->test_mode;
  1266. udc->test_mode = 0;
  1267. } else
  1268. req->req.complete = NULL;
  1269. req->dtd_count = 0;
  1270. if (req->req.dma == DMA_ADDR_INVALID) {
  1271. req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
  1272. req->req.buf, req->req.length,
  1273. ep_dir(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1274. req->mapped = 1;
  1275. }
  1276. /* prime the data phase */
  1277. if (!req_to_dtd(req))
  1278. retval = queue_dtd(ep, req);
  1279. else{ /* no mem */
  1280. retval = -ENOMEM;
  1281. goto out;
  1282. }
  1283. if (retval) {
  1284. dev_err(&udc->dev->dev, "response error on GET_STATUS request\n");
  1285. goto out;
  1286. }
  1287. list_add_tail(&req->queue, &ep->queue);
  1288. return 0;
  1289. out:
  1290. return retval;
  1291. }
  1292. static void mv_udc_testmode(struct mv_udc *udc, u16 index)
  1293. {
  1294. if (index <= TEST_FORCE_EN) {
  1295. udc->test_mode = index;
  1296. if (udc_prime_status(udc, EP_DIR_IN, 0, true))
  1297. ep0_stall(udc);
  1298. } else
  1299. dev_err(&udc->dev->dev,
  1300. "This test mode(%d) is not supported\n", index);
  1301. }
  1302. static void ch9setaddress(struct mv_udc *udc, struct usb_ctrlrequest *setup)
  1303. {
  1304. udc->dev_addr = (u8)setup->wValue;
  1305. /* update usb state */
  1306. udc->usb_state = USB_STATE_ADDRESS;
  1307. if (udc_prime_status(udc, EP_DIR_IN, 0, true))
  1308. ep0_stall(udc);
  1309. }
  1310. static void ch9getstatus(struct mv_udc *udc, u8 ep_num,
  1311. struct usb_ctrlrequest *setup)
  1312. {
  1313. u16 status = 0;
  1314. int retval;
  1315. if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
  1316. != (USB_DIR_IN | USB_TYPE_STANDARD))
  1317. return;
  1318. if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
  1319. status = 1 << USB_DEVICE_SELF_POWERED;
  1320. status |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
  1321. } else if ((setup->bRequestType & USB_RECIP_MASK)
  1322. == USB_RECIP_INTERFACE) {
  1323. /* get interface status */
  1324. status = 0;
  1325. } else if ((setup->bRequestType & USB_RECIP_MASK)
  1326. == USB_RECIP_ENDPOINT) {
  1327. u8 ep_num, direction;
  1328. ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
  1329. direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
  1330. ? EP_DIR_IN : EP_DIR_OUT;
  1331. status = ep_is_stall(udc, ep_num, direction)
  1332. << USB_ENDPOINT_HALT;
  1333. }
  1334. retval = udc_prime_status(udc, EP_DIR_IN, status, false);
  1335. if (retval)
  1336. ep0_stall(udc);
  1337. else
  1338. udc->ep0_state = DATA_STATE_XMIT;
  1339. }
  1340. static void ch9clearfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
  1341. {
  1342. u8 ep_num;
  1343. u8 direction;
  1344. struct mv_ep *ep;
  1345. if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
  1346. == ((USB_TYPE_STANDARD | USB_RECIP_DEVICE))) {
  1347. switch (setup->wValue) {
  1348. case USB_DEVICE_REMOTE_WAKEUP:
  1349. udc->remote_wakeup = 0;
  1350. break;
  1351. default:
  1352. goto out;
  1353. }
  1354. } else if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
  1355. == ((USB_TYPE_STANDARD | USB_RECIP_ENDPOINT))) {
  1356. switch (setup->wValue) {
  1357. case USB_ENDPOINT_HALT:
  1358. ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
  1359. direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
  1360. ? EP_DIR_IN : EP_DIR_OUT;
  1361. if (setup->wValue != 0 || setup->wLength != 0
  1362. || ep_num > udc->max_eps)
  1363. goto out;
  1364. ep = &udc->eps[ep_num * 2 + direction];
  1365. if (ep->wedge == 1)
  1366. break;
  1367. spin_unlock(&udc->lock);
  1368. ep_set_stall(udc, ep_num, direction, 0);
  1369. spin_lock(&udc->lock);
  1370. break;
  1371. default:
  1372. goto out;
  1373. }
  1374. } else
  1375. goto out;
  1376. if (udc_prime_status(udc, EP_DIR_IN, 0, true))
  1377. ep0_stall(udc);
  1378. out:
  1379. return;
  1380. }
  1381. static void ch9setfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
  1382. {
  1383. u8 ep_num;
  1384. u8 direction;
  1385. if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
  1386. == ((USB_TYPE_STANDARD | USB_RECIP_DEVICE))) {
  1387. switch (setup->wValue) {
  1388. case USB_DEVICE_REMOTE_WAKEUP:
  1389. udc->remote_wakeup = 1;
  1390. break;
  1391. case USB_DEVICE_TEST_MODE:
  1392. if (setup->wIndex & 0xFF
  1393. || udc->gadget.speed != USB_SPEED_HIGH)
  1394. ep0_stall(udc);
  1395. if (udc->usb_state != USB_STATE_CONFIGURED
  1396. && udc->usb_state != USB_STATE_ADDRESS
  1397. && udc->usb_state != USB_STATE_DEFAULT)
  1398. ep0_stall(udc);
  1399. mv_udc_testmode(udc, (setup->wIndex >> 8));
  1400. goto out;
  1401. default:
  1402. goto out;
  1403. }
  1404. } else if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
  1405. == ((USB_TYPE_STANDARD | USB_RECIP_ENDPOINT))) {
  1406. switch (setup->wValue) {
  1407. case USB_ENDPOINT_HALT:
  1408. ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
  1409. direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
  1410. ? EP_DIR_IN : EP_DIR_OUT;
  1411. if (setup->wValue != 0 || setup->wLength != 0
  1412. || ep_num > udc->max_eps)
  1413. goto out;
  1414. spin_unlock(&udc->lock);
  1415. ep_set_stall(udc, ep_num, direction, 1);
  1416. spin_lock(&udc->lock);
  1417. break;
  1418. default:
  1419. goto out;
  1420. }
  1421. } else
  1422. goto out;
  1423. if (udc_prime_status(udc, EP_DIR_IN, 0, true))
  1424. ep0_stall(udc);
  1425. out:
  1426. return;
  1427. }
  1428. static void handle_setup_packet(struct mv_udc *udc, u8 ep_num,
  1429. struct usb_ctrlrequest *setup)
  1430. {
  1431. bool delegate = false;
  1432. nuke(&udc->eps[ep_num * 2 + EP_DIR_OUT], -ESHUTDOWN);
  1433. dev_dbg(&udc->dev->dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  1434. setup->bRequestType, setup->bRequest,
  1435. setup->wValue, setup->wIndex, setup->wLength);
  1436. /* We process some stardard setup requests here */
  1437. if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1438. switch (setup->bRequest) {
  1439. case USB_REQ_GET_STATUS:
  1440. ch9getstatus(udc, ep_num, setup);
  1441. break;
  1442. case USB_REQ_SET_ADDRESS:
  1443. ch9setaddress(udc, setup);
  1444. break;
  1445. case USB_REQ_CLEAR_FEATURE:
  1446. ch9clearfeature(udc, setup);
  1447. break;
  1448. case USB_REQ_SET_FEATURE:
  1449. ch9setfeature(udc, setup);
  1450. break;
  1451. default:
  1452. delegate = true;
  1453. }
  1454. } else
  1455. delegate = true;
  1456. /* delegate USB standard requests to the gadget driver */
  1457. if (delegate == true) {
  1458. /* USB requests handled by gadget */
  1459. if (setup->wLength) {
  1460. /* DATA phase from gadget, STATUS phase from udc */
  1461. udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
  1462. ? EP_DIR_IN : EP_DIR_OUT;
  1463. spin_unlock(&udc->lock);
  1464. if (udc->driver->setup(&udc->gadget,
  1465. &udc->local_setup_buff) < 0)
  1466. ep0_stall(udc);
  1467. spin_lock(&udc->lock);
  1468. udc->ep0_state = (setup->bRequestType & USB_DIR_IN)
  1469. ? DATA_STATE_XMIT : DATA_STATE_RECV;
  1470. } else {
  1471. /* no DATA phase, IN STATUS phase from gadget */
  1472. udc->ep0_dir = EP_DIR_IN;
  1473. spin_unlock(&udc->lock);
  1474. if (udc->driver->setup(&udc->gadget,
  1475. &udc->local_setup_buff) < 0)
  1476. ep0_stall(udc);
  1477. spin_lock(&udc->lock);
  1478. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1479. }
  1480. }
  1481. }
  1482. /* complete DATA or STATUS phase of ep0 prime status phase if needed */
  1483. static void ep0_req_complete(struct mv_udc *udc,
  1484. struct mv_ep *ep0, struct mv_req *req)
  1485. {
  1486. u32 new_addr;
  1487. if (udc->usb_state == USB_STATE_ADDRESS) {
  1488. /* set the new address */
  1489. new_addr = (u32)udc->dev_addr;
  1490. writel(new_addr << USB_DEVICE_ADDRESS_BIT_SHIFT,
  1491. &udc->op_regs->deviceaddr);
  1492. }
  1493. done(ep0, req, 0);
  1494. switch (udc->ep0_state) {
  1495. case DATA_STATE_XMIT:
  1496. /* receive status phase */
  1497. if (udc_prime_status(udc, EP_DIR_OUT, 0, true))
  1498. ep0_stall(udc);
  1499. break;
  1500. case DATA_STATE_RECV:
  1501. /* send status phase */
  1502. if (udc_prime_status(udc, EP_DIR_IN, 0 , true))
  1503. ep0_stall(udc);
  1504. break;
  1505. case WAIT_FOR_OUT_STATUS:
  1506. udc->ep0_state = WAIT_FOR_SETUP;
  1507. break;
  1508. case WAIT_FOR_SETUP:
  1509. dev_err(&udc->dev->dev, "unexpect ep0 packets\n");
  1510. break;
  1511. default:
  1512. ep0_stall(udc);
  1513. break;
  1514. }
  1515. }
  1516. static void get_setup_data(struct mv_udc *udc, u8 ep_num, u8 *buffer_ptr)
  1517. {
  1518. u32 temp;
  1519. struct mv_dqh *dqh;
  1520. dqh = &udc->ep_dqh[ep_num * 2 + EP_DIR_OUT];
  1521. /* Clear bit in ENDPTSETUPSTAT */
  1522. writel((1 << ep_num), &udc->op_regs->epsetupstat);
  1523. /* while a hazard exists when setup package arrives */
  1524. do {
  1525. /* Set Setup Tripwire */
  1526. temp = readl(&udc->op_regs->usbcmd);
  1527. writel(temp | USBCMD_SETUP_TRIPWIRE_SET, &udc->op_regs->usbcmd);
  1528. /* Copy the setup packet to local buffer */
  1529. memcpy(buffer_ptr, (u8 *) dqh->setup_buffer, 8);
  1530. } while (!(readl(&udc->op_regs->usbcmd) & USBCMD_SETUP_TRIPWIRE_SET));
  1531. /* Clear Setup Tripwire */
  1532. temp = readl(&udc->op_regs->usbcmd);
  1533. writel(temp & ~USBCMD_SETUP_TRIPWIRE_SET, &udc->op_regs->usbcmd);
  1534. }
  1535. static void irq_process_tr_complete(struct mv_udc *udc)
  1536. {
  1537. u32 tmp, bit_pos;
  1538. int i, ep_num = 0, direction = 0;
  1539. struct mv_ep *curr_ep;
  1540. struct mv_req *curr_req, *temp_req;
  1541. int status;
  1542. /*
  1543. * We use separate loops for ENDPTSETUPSTAT and ENDPTCOMPLETE
  1544. * because the setup packets are to be read ASAP
  1545. */
  1546. /* Process all Setup packet received interrupts */
  1547. tmp = readl(&udc->op_regs->epsetupstat);
  1548. if (tmp) {
  1549. for (i = 0; i < udc->max_eps; i++) {
  1550. if (tmp & (1 << i)) {
  1551. get_setup_data(udc, i,
  1552. (u8 *)(&udc->local_setup_buff));
  1553. handle_setup_packet(udc, i,
  1554. &udc->local_setup_buff);
  1555. }
  1556. }
  1557. }
  1558. /* Don't clear the endpoint setup status register here.
  1559. * It is cleared as a setup packet is read out of the buffer
  1560. */
  1561. /* Process non-setup transaction complete interrupts */
  1562. tmp = readl(&udc->op_regs->epcomplete);
  1563. if (!tmp)
  1564. return;
  1565. writel(tmp, &udc->op_regs->epcomplete);
  1566. for (i = 0; i < udc->max_eps * 2; i++) {
  1567. ep_num = i >> 1;
  1568. direction = i % 2;
  1569. bit_pos = 1 << (ep_num + 16 * direction);
  1570. if (!(bit_pos & tmp))
  1571. continue;
  1572. if (i == 1)
  1573. curr_ep = &udc->eps[0];
  1574. else
  1575. curr_ep = &udc->eps[i];
  1576. /* process the req queue until an uncomplete request */
  1577. list_for_each_entry_safe(curr_req, temp_req,
  1578. &curr_ep->queue, queue) {
  1579. status = process_ep_req(udc, i, curr_req);
  1580. if (status)
  1581. break;
  1582. /* write back status to req */
  1583. curr_req->req.status = status;
  1584. /* ep0 request completion */
  1585. if (ep_num == 0) {
  1586. ep0_req_complete(udc, curr_ep, curr_req);
  1587. break;
  1588. } else {
  1589. done(curr_ep, curr_req, status);
  1590. }
  1591. }
  1592. }
  1593. }
  1594. void irq_process_reset(struct mv_udc *udc)
  1595. {
  1596. u32 tmp;
  1597. unsigned int loops;
  1598. udc->ep0_dir = EP_DIR_OUT;
  1599. udc->ep0_state = WAIT_FOR_SETUP;
  1600. udc->remote_wakeup = 0; /* default to 0 on reset */
  1601. /* The address bits are past bit 25-31. Set the address */
  1602. tmp = readl(&udc->op_regs->deviceaddr);
  1603. tmp &= ~(USB_DEVICE_ADDRESS_MASK);
  1604. writel(tmp, &udc->op_regs->deviceaddr);
  1605. /* Clear all the setup token semaphores */
  1606. tmp = readl(&udc->op_regs->epsetupstat);
  1607. writel(tmp, &udc->op_regs->epsetupstat);
  1608. /* Clear all the endpoint complete status bits */
  1609. tmp = readl(&udc->op_regs->epcomplete);
  1610. writel(tmp, &udc->op_regs->epcomplete);
  1611. /* wait until all endptprime bits cleared */
  1612. loops = LOOPS(PRIME_TIMEOUT);
  1613. while (readl(&udc->op_regs->epprime) & 0xFFFFFFFF) {
  1614. if (loops == 0) {
  1615. dev_err(&udc->dev->dev,
  1616. "Timeout for ENDPTPRIME = 0x%x\n",
  1617. readl(&udc->op_regs->epprime));
  1618. break;
  1619. }
  1620. loops--;
  1621. udelay(LOOPS_USEC);
  1622. }
  1623. /* Write 1s to the Flush register */
  1624. writel((u32)~0, &udc->op_regs->epflush);
  1625. if (readl(&udc->op_regs->portsc[0]) & PORTSCX_PORT_RESET) {
  1626. dev_info(&udc->dev->dev, "usb bus reset\n");
  1627. udc->usb_state = USB_STATE_DEFAULT;
  1628. /* reset all the queues, stop all USB activities */
  1629. stop_activity(udc, udc->driver);
  1630. } else {
  1631. dev_info(&udc->dev->dev, "USB reset portsc 0x%x\n",
  1632. readl(&udc->op_regs->portsc));
  1633. /*
  1634. * re-initialize
  1635. * controller reset
  1636. */
  1637. udc_reset(udc);
  1638. /* reset all the queues, stop all USB activities */
  1639. stop_activity(udc, udc->driver);
  1640. /* reset ep0 dQH and endptctrl */
  1641. ep0_reset(udc);
  1642. /* enable interrupt and set controller to run state */
  1643. udc_start(udc);
  1644. udc->usb_state = USB_STATE_ATTACHED;
  1645. }
  1646. }
  1647. static void handle_bus_resume(struct mv_udc *udc)
  1648. {
  1649. udc->usb_state = udc->resume_state;
  1650. udc->resume_state = 0;
  1651. /* report resume to the driver */
  1652. if (udc->driver) {
  1653. if (udc->driver->resume) {
  1654. spin_unlock(&udc->lock);
  1655. udc->driver->resume(&udc->gadget);
  1656. spin_lock(&udc->lock);
  1657. }
  1658. }
  1659. }
  1660. static void irq_process_suspend(struct mv_udc *udc)
  1661. {
  1662. udc->resume_state = udc->usb_state;
  1663. udc->usb_state = USB_STATE_SUSPENDED;
  1664. if (udc->driver->suspend) {
  1665. spin_unlock(&udc->lock);
  1666. udc->driver->suspend(&udc->gadget);
  1667. spin_lock(&udc->lock);
  1668. }
  1669. }
  1670. static void irq_process_port_change(struct mv_udc *udc)
  1671. {
  1672. u32 portsc;
  1673. portsc = readl(&udc->op_regs->portsc[0]);
  1674. if (!(portsc & PORTSCX_PORT_RESET)) {
  1675. /* Get the speed */
  1676. u32 speed = portsc & PORTSCX_PORT_SPEED_MASK;
  1677. switch (speed) {
  1678. case PORTSCX_PORT_SPEED_HIGH:
  1679. udc->gadget.speed = USB_SPEED_HIGH;
  1680. break;
  1681. case PORTSCX_PORT_SPEED_FULL:
  1682. udc->gadget.speed = USB_SPEED_FULL;
  1683. break;
  1684. case PORTSCX_PORT_SPEED_LOW:
  1685. udc->gadget.speed = USB_SPEED_LOW;
  1686. break;
  1687. default:
  1688. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1689. break;
  1690. }
  1691. }
  1692. if (portsc & PORTSCX_PORT_SUSPEND) {
  1693. udc->resume_state = udc->usb_state;
  1694. udc->usb_state = USB_STATE_SUSPENDED;
  1695. if (udc->driver->suspend) {
  1696. spin_unlock(&udc->lock);
  1697. udc->driver->suspend(&udc->gadget);
  1698. spin_lock(&udc->lock);
  1699. }
  1700. }
  1701. if (!(portsc & PORTSCX_PORT_SUSPEND)
  1702. && udc->usb_state == USB_STATE_SUSPENDED) {
  1703. handle_bus_resume(udc);
  1704. }
  1705. if (!udc->resume_state)
  1706. udc->usb_state = USB_STATE_DEFAULT;
  1707. }
  1708. static void irq_process_error(struct mv_udc *udc)
  1709. {
  1710. /* Increment the error count */
  1711. udc->errors++;
  1712. }
  1713. static irqreturn_t mv_udc_irq(int irq, void *dev)
  1714. {
  1715. struct mv_udc *udc = (struct mv_udc *)dev;
  1716. u32 status, intr;
  1717. /* Disable ISR when stopped bit is set */
  1718. if (udc->stopped)
  1719. return IRQ_NONE;
  1720. spin_lock(&udc->lock);
  1721. status = readl(&udc->op_regs->usbsts);
  1722. intr = readl(&udc->op_regs->usbintr);
  1723. status &= intr;
  1724. if (status == 0) {
  1725. spin_unlock(&udc->lock);
  1726. return IRQ_NONE;
  1727. }
  1728. /* Clear all the interrupts occurred */
  1729. writel(status, &udc->op_regs->usbsts);
  1730. if (status & USBSTS_ERR)
  1731. irq_process_error(udc);
  1732. if (status & USBSTS_RESET)
  1733. irq_process_reset(udc);
  1734. if (status & USBSTS_PORT_CHANGE)
  1735. irq_process_port_change(udc);
  1736. if (status & USBSTS_INT)
  1737. irq_process_tr_complete(udc);
  1738. if (status & USBSTS_SUSPEND)
  1739. irq_process_suspend(udc);
  1740. spin_unlock(&udc->lock);
  1741. return IRQ_HANDLED;
  1742. }
  1743. static irqreturn_t mv_udc_vbus_irq(int irq, void *dev)
  1744. {
  1745. struct mv_udc *udc = (struct mv_udc *)dev;
  1746. /* polling VBUS and init phy may cause too much time*/
  1747. if (udc->qwork)
  1748. queue_work(udc->qwork, &udc->vbus_work);
  1749. return IRQ_HANDLED;
  1750. }
  1751. static void mv_udc_vbus_work(struct work_struct *work)
  1752. {
  1753. struct mv_udc *udc;
  1754. unsigned int vbus;
  1755. udc = container_of(work, struct mv_udc, vbus_work);
  1756. if (!udc->pdata->vbus)
  1757. return;
  1758. vbus = udc->pdata->vbus->poll();
  1759. dev_info(&udc->dev->dev, "vbus is %d\n", vbus);
  1760. if (vbus == VBUS_HIGH)
  1761. mv_udc_vbus_session(&udc->gadget, 1);
  1762. else if (vbus == VBUS_LOW)
  1763. mv_udc_vbus_session(&udc->gadget, 0);
  1764. }
  1765. /* release device structure */
  1766. static void gadget_release(struct device *_dev)
  1767. {
  1768. struct mv_udc *udc = the_controller;
  1769. complete(udc->done);
  1770. }
  1771. static int __devexit mv_udc_remove(struct platform_device *dev)
  1772. {
  1773. struct mv_udc *udc = the_controller;
  1774. int clk_i;
  1775. usb_del_gadget_udc(&udc->gadget);
  1776. if (udc->qwork) {
  1777. flush_workqueue(udc->qwork);
  1778. destroy_workqueue(udc->qwork);
  1779. }
  1780. /*
  1781. * If we have transceiver inited,
  1782. * then vbus irq will not be requested in udc driver.
  1783. */
  1784. if (udc->pdata && udc->pdata->vbus
  1785. && udc->clock_gating && udc->transceiver == NULL)
  1786. free_irq(udc->pdata->vbus->irq, &dev->dev);
  1787. /* free memory allocated in probe */
  1788. if (udc->dtd_pool)
  1789. dma_pool_destroy(udc->dtd_pool);
  1790. if (udc->ep_dqh)
  1791. dma_free_coherent(&dev->dev, udc->ep_dqh_size,
  1792. udc->ep_dqh, udc->ep_dqh_dma);
  1793. kfree(udc->eps);
  1794. if (udc->irq)
  1795. free_irq(udc->irq, &dev->dev);
  1796. mv_udc_disable(udc);
  1797. if (udc->cap_regs)
  1798. iounmap(udc->cap_regs);
  1799. udc->cap_regs = NULL;
  1800. if (udc->phy_regs)
  1801. iounmap((void *)udc->phy_regs);
  1802. udc->phy_regs = 0;
  1803. if (udc->status_req) {
  1804. kfree(udc->status_req->req.buf);
  1805. kfree(udc->status_req);
  1806. }
  1807. for (clk_i = 0; clk_i <= udc->clknum; clk_i++)
  1808. clk_put(udc->clk[clk_i]);
  1809. device_unregister(&udc->gadget.dev);
  1810. /* free dev, wait for the release() finished */
  1811. wait_for_completion(udc->done);
  1812. kfree(udc);
  1813. the_controller = NULL;
  1814. return 0;
  1815. }
  1816. static int __devinit mv_udc_probe(struct platform_device *dev)
  1817. {
  1818. struct mv_usb_platform_data *pdata = dev->dev.platform_data;
  1819. struct mv_udc *udc;
  1820. int retval = 0;
  1821. int clk_i = 0;
  1822. struct resource *r;
  1823. size_t size;
  1824. if (pdata == NULL) {
  1825. dev_err(&dev->dev, "missing platform_data\n");
  1826. return -ENODEV;
  1827. }
  1828. size = sizeof(*udc) + sizeof(struct clk *) * pdata->clknum;
  1829. udc = kzalloc(size, GFP_KERNEL);
  1830. if (udc == NULL) {
  1831. dev_err(&dev->dev, "failed to allocate memory for udc\n");
  1832. return -ENOMEM;
  1833. }
  1834. the_controller = udc;
  1835. udc->done = &release_done;
  1836. udc->pdata = dev->dev.platform_data;
  1837. spin_lock_init(&udc->lock);
  1838. udc->dev = dev;
  1839. #ifdef CONFIG_USB_OTG_UTILS
  1840. if (pdata->mode == MV_USB_MODE_OTG)
  1841. udc->transceiver = otg_get_transceiver();
  1842. #endif
  1843. udc->clknum = pdata->clknum;
  1844. for (clk_i = 0; clk_i < udc->clknum; clk_i++) {
  1845. udc->clk[clk_i] = clk_get(&dev->dev, pdata->clkname[clk_i]);
  1846. if (IS_ERR(udc->clk[clk_i])) {
  1847. retval = PTR_ERR(udc->clk[clk_i]);
  1848. goto err_put_clk;
  1849. }
  1850. }
  1851. r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "capregs");
  1852. if (r == NULL) {
  1853. dev_err(&dev->dev, "no I/O memory resource defined\n");
  1854. retval = -ENODEV;
  1855. goto err_put_clk;
  1856. }
  1857. udc->cap_regs = (struct mv_cap_regs __iomem *)
  1858. ioremap(r->start, resource_size(r));
  1859. if (udc->cap_regs == NULL) {
  1860. dev_err(&dev->dev, "failed to map I/O memory\n");
  1861. retval = -EBUSY;
  1862. goto err_put_clk;
  1863. }
  1864. r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "phyregs");
  1865. if (r == NULL) {
  1866. dev_err(&dev->dev, "no phy I/O memory resource defined\n");
  1867. retval = -ENODEV;
  1868. goto err_iounmap_capreg;
  1869. }
  1870. udc->phy_regs = (unsigned int)ioremap(r->start, resource_size(r));
  1871. if (udc->phy_regs == 0) {
  1872. dev_err(&dev->dev, "failed to map phy I/O memory\n");
  1873. retval = -EBUSY;
  1874. goto err_iounmap_capreg;
  1875. }
  1876. /* we will acces controller register, so enable the clk */
  1877. retval = mv_udc_enable_internal(udc);
  1878. if (retval)
  1879. goto err_iounmap_phyreg;
  1880. udc->op_regs = (struct mv_op_regs __iomem *)((u32)udc->cap_regs
  1881. + (readl(&udc->cap_regs->caplength_hciversion)
  1882. & CAPLENGTH_MASK));
  1883. udc->max_eps = readl(&udc->cap_regs->dccparams) & DCCPARAMS_DEN_MASK;
  1884. /*
  1885. * some platform will use usb to download image, it may not disconnect
  1886. * usb gadget before loading kernel. So first stop udc here.
  1887. */
  1888. udc_stop(udc);
  1889. writel(0xFFFFFFFF, &udc->op_regs->usbsts);
  1890. size = udc->max_eps * sizeof(struct mv_dqh) *2;
  1891. size = (size + DQH_ALIGNMENT - 1) & ~(DQH_ALIGNMENT - 1);
  1892. udc->ep_dqh = dma_alloc_coherent(&dev->dev, size,
  1893. &udc->ep_dqh_dma, GFP_KERNEL);
  1894. if (udc->ep_dqh == NULL) {
  1895. dev_err(&dev->dev, "allocate dQH memory failed\n");
  1896. retval = -ENOMEM;
  1897. goto err_disable_clock;
  1898. }
  1899. udc->ep_dqh_size = size;
  1900. /* create dTD dma_pool resource */
  1901. udc->dtd_pool = dma_pool_create("mv_dtd",
  1902. &dev->dev,
  1903. sizeof(struct mv_dtd),
  1904. DTD_ALIGNMENT,
  1905. DMA_BOUNDARY);
  1906. if (!udc->dtd_pool) {
  1907. retval = -ENOMEM;
  1908. goto err_free_dma;
  1909. }
  1910. size = udc->max_eps * sizeof(struct mv_ep) *2;
  1911. udc->eps = kzalloc(size, GFP_KERNEL);
  1912. if (udc->eps == NULL) {
  1913. dev_err(&dev->dev, "allocate ep memory failed\n");
  1914. retval = -ENOMEM;
  1915. goto err_destroy_dma;
  1916. }
  1917. /* initialize ep0 status request structure */
  1918. udc->status_req = kzalloc(sizeof(struct mv_req), GFP_KERNEL);
  1919. if (!udc->status_req) {
  1920. dev_err(&dev->dev, "allocate status_req memory failed\n");
  1921. retval = -ENOMEM;
  1922. goto err_free_eps;
  1923. }
  1924. INIT_LIST_HEAD(&udc->status_req->queue);
  1925. /* allocate a small amount of memory to get valid address */
  1926. udc->status_req->req.buf = kzalloc(8, GFP_KERNEL);
  1927. udc->status_req->req.dma = DMA_ADDR_INVALID;
  1928. udc->resume_state = USB_STATE_NOTATTACHED;
  1929. udc->usb_state = USB_STATE_POWERED;
  1930. udc->ep0_dir = EP_DIR_OUT;
  1931. udc->remote_wakeup = 0;
  1932. r = platform_get_resource(udc->dev, IORESOURCE_IRQ, 0);
  1933. if (r == NULL) {
  1934. dev_err(&dev->dev, "no IRQ resource defined\n");
  1935. retval = -ENODEV;
  1936. goto err_free_status_req;
  1937. }
  1938. udc->irq = r->start;
  1939. if (request_irq(udc->irq, mv_udc_irq,
  1940. IRQF_SHARED, driver_name, udc)) {
  1941. dev_err(&dev->dev, "Request irq %d for UDC failed\n",
  1942. udc->irq);
  1943. retval = -ENODEV;
  1944. goto err_free_status_req;
  1945. }
  1946. /* initialize gadget structure */
  1947. udc->gadget.ops = &mv_ops; /* usb_gadget_ops */
  1948. udc->gadget.ep0 = &udc->eps[0].ep; /* gadget ep0 */
  1949. INIT_LIST_HEAD(&udc->gadget.ep_list); /* ep_list */
  1950. udc->gadget.speed = USB_SPEED_UNKNOWN; /* speed */
  1951. udc->gadget.max_speed = USB_SPEED_HIGH; /* support dual speed */
  1952. /* the "gadget" abstracts/virtualizes the controller */
  1953. dev_set_name(&udc->gadget.dev, "gadget");
  1954. udc->gadget.dev.parent = &dev->dev;
  1955. udc->gadget.dev.dma_mask = dev->dev.dma_mask;
  1956. udc->gadget.dev.release = gadget_release;
  1957. udc->gadget.name = driver_name; /* gadget name */
  1958. retval = device_register(&udc->gadget.dev);
  1959. if (retval)
  1960. goto err_free_irq;
  1961. eps_init(udc);
  1962. /* VBUS detect: we can disable/enable clock on demand.*/
  1963. if (udc->transceiver)
  1964. udc->clock_gating = 1;
  1965. else if (pdata->vbus) {
  1966. udc->clock_gating = 1;
  1967. retval = request_threaded_irq(pdata->vbus->irq, NULL,
  1968. mv_udc_vbus_irq, IRQF_ONESHOT, "vbus", udc);
  1969. if (retval) {
  1970. dev_info(&dev->dev,
  1971. "Can not request irq for VBUS, "
  1972. "disable clock gating\n");
  1973. udc->clock_gating = 0;
  1974. }
  1975. udc->qwork = create_singlethread_workqueue("mv_udc_queue");
  1976. if (!udc->qwork) {
  1977. dev_err(&dev->dev, "cannot create workqueue\n");
  1978. retval = -ENOMEM;
  1979. goto err_unregister;
  1980. }
  1981. INIT_WORK(&udc->vbus_work, mv_udc_vbus_work);
  1982. }
  1983. /*
  1984. * When clock gating is supported, we can disable clk and phy.
  1985. * If not, it means that VBUS detection is not supported, we
  1986. * have to enable vbus active all the time to let controller work.
  1987. */
  1988. if (udc->clock_gating)
  1989. mv_udc_disable_internal(udc);
  1990. else
  1991. udc->vbus_active = 1;
  1992. retval = usb_add_gadget_udc(&dev->dev, &udc->gadget);
  1993. if (retval)
  1994. goto err_unregister;
  1995. dev_info(&dev->dev, "successful probe UDC device %s clock gating.\n",
  1996. udc->clock_gating ? "with" : "without");
  1997. return 0;
  1998. err_unregister:
  1999. if (udc->pdata && udc->pdata->vbus
  2000. && udc->clock_gating && udc->transceiver == NULL)
  2001. free_irq(pdata->vbus->irq, &dev->dev);
  2002. device_unregister(&udc->gadget.dev);
  2003. err_free_irq:
  2004. free_irq(udc->irq, &dev->dev);
  2005. err_free_status_req:
  2006. kfree(udc->status_req->req.buf);
  2007. kfree(udc->status_req);
  2008. err_free_eps:
  2009. kfree(udc->eps);
  2010. err_destroy_dma:
  2011. dma_pool_destroy(udc->dtd_pool);
  2012. err_free_dma:
  2013. dma_free_coherent(&dev->dev, udc->ep_dqh_size,
  2014. udc->ep_dqh, udc->ep_dqh_dma);
  2015. err_disable_clock:
  2016. mv_udc_disable_internal(udc);
  2017. err_iounmap_phyreg:
  2018. iounmap((void *)udc->phy_regs);
  2019. err_iounmap_capreg:
  2020. iounmap(udc->cap_regs);
  2021. err_put_clk:
  2022. for (clk_i--; clk_i >= 0; clk_i--)
  2023. clk_put(udc->clk[clk_i]);
  2024. the_controller = NULL;
  2025. kfree(udc);
  2026. return retval;
  2027. }
  2028. #ifdef CONFIG_PM
  2029. static int mv_udc_suspend(struct device *_dev)
  2030. {
  2031. struct mv_udc *udc = the_controller;
  2032. udc_stop(udc);
  2033. return 0;
  2034. }
  2035. static int mv_udc_resume(struct device *_dev)
  2036. {
  2037. struct mv_udc *udc = the_controller;
  2038. int retval;
  2039. if (udc->pdata->phy_init) {
  2040. retval = udc->pdata->phy_init(udc->phy_regs);
  2041. if (retval) {
  2042. dev_err(&udc->dev->dev,
  2043. "init phy error %d when resume back\n",
  2044. retval);
  2045. return retval;
  2046. }
  2047. }
  2048. udc_reset(udc);
  2049. ep0_reset(udc);
  2050. udc_start(udc);
  2051. return 0;
  2052. }
  2053. static const struct dev_pm_ops mv_udc_pm_ops = {
  2054. .suspend = mv_udc_suspend,
  2055. .resume = mv_udc_resume,
  2056. };
  2057. #endif
  2058. static void mv_udc_shutdown(struct platform_device *dev)
  2059. {
  2060. struct mv_udc *udc = the_controller;
  2061. u32 mode;
  2062. /* reset controller mode to IDLE */
  2063. mode = readl(&udc->op_regs->usbmode);
  2064. mode &= ~3;
  2065. writel(mode, &udc->op_regs->usbmode);
  2066. }
  2067. static struct platform_driver udc_driver = {
  2068. .probe = mv_udc_probe,
  2069. .remove = __exit_p(mv_udc_remove),
  2070. .shutdown = mv_udc_shutdown,
  2071. .driver = {
  2072. .owner = THIS_MODULE,
  2073. .name = "pxa-u2o",
  2074. #ifdef CONFIG_PM
  2075. .pm = &mv_udc_pm_ops,
  2076. #endif
  2077. },
  2078. };
  2079. MODULE_ALIAS("platform:pxa-u2o");
  2080. MODULE_DESCRIPTION(DRIVER_DESC);
  2081. MODULE_AUTHOR("Chao Xie <chao.xie@marvell.com>");
  2082. MODULE_VERSION(DRIVER_VERSION);
  2083. MODULE_LICENSE("GPL");
  2084. static int __init init(void)
  2085. {
  2086. return platform_driver_register(&udc_driver);
  2087. }
  2088. module_init(init);
  2089. static void __exit cleanup(void)
  2090. {
  2091. platform_driver_unregister(&udc_driver);
  2092. }
  2093. module_exit(cleanup);