mv_udc_core.c 57 KB

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