mv_udc_core.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430
  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. {
  184. struct mv_udc *udc = NULL;
  185. unsigned char stopped = ep->stopped;
  186. struct mv_dtd *curr_td, *next_td;
  187. int j;
  188. udc = (struct mv_udc *)ep->udc;
  189. /* Removed the req from fsl_ep->queue */
  190. list_del_init(&req->queue);
  191. /* req.status should be set as -EINPROGRESS in ep_queue() */
  192. if (req->req.status == -EINPROGRESS)
  193. req->req.status = status;
  194. else
  195. status = req->req.status;
  196. /* Free dtd for the request */
  197. next_td = req->head;
  198. for (j = 0; j < req->dtd_count; j++) {
  199. curr_td = next_td;
  200. if (j != req->dtd_count - 1)
  201. next_td = curr_td->next_dtd_virt;
  202. dma_pool_free(udc->dtd_pool, curr_td, curr_td->td_dma);
  203. }
  204. usb_gadget_unmap_request(&udc->gadget, &req->req, ep_dir(ep));
  205. if (status && (status != -ESHUTDOWN))
  206. dev_info(&udc->dev->dev, "complete %s req %p stat %d len %u/%u",
  207. ep->ep.name, &req->req, status,
  208. req->req.actual, req->req.length);
  209. ep->stopped = 1;
  210. spin_unlock(&ep->udc->lock);
  211. /*
  212. * complete() is from gadget layer,
  213. * eg fsg->bulk_in_complete()
  214. */
  215. if (req->req.complete)
  216. req->req.complete(&ep->ep, &req->req);
  217. spin_lock(&ep->udc->lock);
  218. ep->stopped = stopped;
  219. }
  220. static int queue_dtd(struct mv_ep *ep, struct mv_req *req)
  221. {
  222. struct mv_udc *udc;
  223. struct mv_dqh *dqh;
  224. u32 bit_pos, direction;
  225. u32 usbcmd, epstatus;
  226. unsigned int loops;
  227. int retval = 0;
  228. udc = ep->udc;
  229. direction = ep_dir(ep);
  230. dqh = &(udc->ep_dqh[ep->ep_num * 2 + direction]);
  231. bit_pos = 1 << (((direction == EP_DIR_OUT) ? 0 : 16) + ep->ep_num);
  232. /* check if the pipe is empty */
  233. if (!(list_empty(&ep->queue))) {
  234. struct mv_req *lastreq;
  235. lastreq = list_entry(ep->queue.prev, struct mv_req, queue);
  236. lastreq->tail->dtd_next =
  237. req->head->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
  238. wmb();
  239. if (readl(&udc->op_regs->epprime) & bit_pos)
  240. goto done;
  241. loops = LOOPS(READSAFE_TIMEOUT);
  242. while (1) {
  243. /* start with setting the semaphores */
  244. usbcmd = readl(&udc->op_regs->usbcmd);
  245. usbcmd |= USBCMD_ATDTW_TRIPWIRE_SET;
  246. writel(usbcmd, &udc->op_regs->usbcmd);
  247. /* read the endpoint status */
  248. epstatus = readl(&udc->op_regs->epstatus) & bit_pos;
  249. /*
  250. * Reread the ATDTW semaphore bit to check if it is
  251. * cleared. When hardware see a hazard, it will clear
  252. * the bit or else we remain set to 1 and we can
  253. * proceed with priming of endpoint if not already
  254. * primed.
  255. */
  256. if (readl(&udc->op_regs->usbcmd)
  257. & USBCMD_ATDTW_TRIPWIRE_SET)
  258. break;
  259. loops--;
  260. if (loops == 0) {
  261. dev_err(&udc->dev->dev,
  262. "Timeout for ATDTW_TRIPWIRE...\n");
  263. retval = -ETIME;
  264. goto done;
  265. }
  266. udelay(LOOPS_USEC);
  267. }
  268. /* Clear the semaphore */
  269. usbcmd = readl(&udc->op_regs->usbcmd);
  270. usbcmd &= USBCMD_ATDTW_TRIPWIRE_CLEAR;
  271. writel(usbcmd, &udc->op_regs->usbcmd);
  272. if (epstatus)
  273. goto done;
  274. }
  275. /* Write dQH next pointer and terminate bit to 0 */
  276. dqh->next_dtd_ptr = req->head->td_dma
  277. & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
  278. /* clear active and halt bit, in case set from a previous error */
  279. dqh->size_ioc_int_sts &= ~(DTD_STATUS_ACTIVE | DTD_STATUS_HALTED);
  280. /* Ensure that updates to the QH will occure before priming. */
  281. wmb();
  282. /* Prime the Endpoint */
  283. writel(bit_pos, &udc->op_regs->epprime);
  284. done:
  285. return retval;
  286. }
  287. static struct mv_dtd *build_dtd(struct mv_req *req, unsigned *length,
  288. dma_addr_t *dma, int *is_last)
  289. {
  290. struct mv_dtd *dtd;
  291. struct mv_udc *udc;
  292. struct mv_dqh *dqh;
  293. u32 temp, mult = 0;
  294. /* how big will this transfer be? */
  295. if (usb_endpoint_xfer_isoc(req->ep->ep.desc)) {
  296. dqh = req->ep->dqh;
  297. mult = (dqh->max_packet_length >> EP_QUEUE_HEAD_MULT_POS)
  298. & 0x3;
  299. *length = min(req->req.length - req->req.actual,
  300. (unsigned)(mult * req->ep->ep.maxpacket));
  301. } else
  302. *length = min(req->req.length - req->req.actual,
  303. (unsigned)EP_MAX_LENGTH_TRANSFER);
  304. udc = req->ep->udc;
  305. /*
  306. * Be careful that no _GFP_HIGHMEM is set,
  307. * or we can not use dma_to_virt
  308. */
  309. dtd = dma_pool_alloc(udc->dtd_pool, GFP_ATOMIC, dma);
  310. if (dtd == NULL)
  311. return dtd;
  312. dtd->td_dma = *dma;
  313. /* initialize buffer page pointers */
  314. temp = (u32)(req->req.dma + req->req.actual);
  315. dtd->buff_ptr0 = cpu_to_le32(temp);
  316. temp &= ~0xFFF;
  317. dtd->buff_ptr1 = cpu_to_le32(temp + 0x1000);
  318. dtd->buff_ptr2 = cpu_to_le32(temp + 0x2000);
  319. dtd->buff_ptr3 = cpu_to_le32(temp + 0x3000);
  320. dtd->buff_ptr4 = cpu_to_le32(temp + 0x4000);
  321. req->req.actual += *length;
  322. /* zlp is needed if req->req.zero is set */
  323. if (req->req.zero) {
  324. if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
  325. *is_last = 1;
  326. else
  327. *is_last = 0;
  328. } else if (req->req.length == req->req.actual)
  329. *is_last = 1;
  330. else
  331. *is_last = 0;
  332. /* Fill in the transfer size; set active bit */
  333. temp = ((*length << DTD_LENGTH_BIT_POS) | DTD_STATUS_ACTIVE);
  334. /* Enable interrupt for the last dtd of a request */
  335. if (*is_last && !req->req.no_interrupt)
  336. temp |= DTD_IOC;
  337. temp |= mult << 10;
  338. dtd->size_ioc_sts = temp;
  339. mb();
  340. return dtd;
  341. }
  342. /* generate dTD linked list for a request */
  343. static int req_to_dtd(struct mv_req *req)
  344. {
  345. unsigned count;
  346. int is_last, is_first = 1;
  347. struct mv_dtd *dtd, *last_dtd = NULL;
  348. struct mv_udc *udc;
  349. dma_addr_t dma;
  350. udc = req->ep->udc;
  351. do {
  352. dtd = build_dtd(req, &count, &dma, &is_last);
  353. if (dtd == NULL)
  354. return -ENOMEM;
  355. if (is_first) {
  356. is_first = 0;
  357. req->head = dtd;
  358. } else {
  359. last_dtd->dtd_next = dma;
  360. last_dtd->next_dtd_virt = dtd;
  361. }
  362. last_dtd = dtd;
  363. req->dtd_count++;
  364. } while (!is_last);
  365. /* set terminate bit to 1 for the last dTD */
  366. dtd->dtd_next = DTD_NEXT_TERMINATE;
  367. req->tail = dtd;
  368. return 0;
  369. }
  370. static int mv_ep_enable(struct usb_ep *_ep,
  371. const struct usb_endpoint_descriptor *desc)
  372. {
  373. struct mv_udc *udc;
  374. struct mv_ep *ep;
  375. struct mv_dqh *dqh;
  376. u16 max = 0;
  377. u32 bit_pos, epctrlx, direction;
  378. unsigned char zlt = 0, ios = 0, mult = 0;
  379. unsigned long flags;
  380. ep = container_of(_ep, struct mv_ep, ep);
  381. udc = ep->udc;
  382. if (!_ep || !desc
  383. || desc->bDescriptorType != USB_DT_ENDPOINT)
  384. return -EINVAL;
  385. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
  386. return -ESHUTDOWN;
  387. direction = ep_dir(ep);
  388. max = usb_endpoint_maxp(desc);
  389. /*
  390. * disable HW zero length termination select
  391. * driver handles zero length packet through req->req.zero
  392. */
  393. zlt = 1;
  394. bit_pos = 1 << ((direction == EP_DIR_OUT ? 0 : 16) + ep->ep_num);
  395. /* Check if the Endpoint is Primed */
  396. if ((readl(&udc->op_regs->epprime) & bit_pos)
  397. || (readl(&udc->op_regs->epstatus) & bit_pos)) {
  398. dev_info(&udc->dev->dev,
  399. "ep=%d %s: Init ERROR: ENDPTPRIME=0x%x,"
  400. " ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
  401. (unsigned)ep->ep_num, direction ? "SEND" : "RECV",
  402. (unsigned)readl(&udc->op_regs->epprime),
  403. (unsigned)readl(&udc->op_regs->epstatus),
  404. (unsigned)bit_pos);
  405. goto en_done;
  406. }
  407. /* Set the max packet length, interrupt on Setup and Mult fields */
  408. switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  409. case USB_ENDPOINT_XFER_BULK:
  410. zlt = 1;
  411. mult = 0;
  412. break;
  413. case USB_ENDPOINT_XFER_CONTROL:
  414. ios = 1;
  415. case USB_ENDPOINT_XFER_INT:
  416. mult = 0;
  417. break;
  418. case USB_ENDPOINT_XFER_ISOC:
  419. /* Calculate transactions needed for high bandwidth iso */
  420. mult = (unsigned char)(1 + ((max >> 11) & 0x03));
  421. max = max & 0x7ff; /* bit 0~10 */
  422. /* 3 transactions at most */
  423. if (mult > 3)
  424. goto en_done;
  425. break;
  426. default:
  427. goto en_done;
  428. }
  429. spin_lock_irqsave(&udc->lock, flags);
  430. /* Get the endpoint queue head address */
  431. dqh = ep->dqh;
  432. dqh->max_packet_length = (max << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
  433. | (mult << EP_QUEUE_HEAD_MULT_POS)
  434. | (zlt ? EP_QUEUE_HEAD_ZLT_SEL : 0)
  435. | (ios ? EP_QUEUE_HEAD_IOS : 0);
  436. dqh->next_dtd_ptr = 1;
  437. dqh->size_ioc_int_sts = 0;
  438. ep->ep.maxpacket = max;
  439. ep->ep.desc = desc;
  440. ep->stopped = 0;
  441. /* Enable the endpoint for Rx or Tx and set the endpoint type */
  442. epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
  443. if (direction == EP_DIR_IN) {
  444. epctrlx &= ~EPCTRL_TX_ALL_MASK;
  445. epctrlx |= EPCTRL_TX_ENABLE | EPCTRL_TX_DATA_TOGGLE_RST
  446. | ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  447. << EPCTRL_TX_EP_TYPE_SHIFT);
  448. } else {
  449. epctrlx &= ~EPCTRL_RX_ALL_MASK;
  450. epctrlx |= EPCTRL_RX_ENABLE | EPCTRL_RX_DATA_TOGGLE_RST
  451. | ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  452. << EPCTRL_RX_EP_TYPE_SHIFT);
  453. }
  454. writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
  455. /*
  456. * Implement Guideline (GL# USB-7) The unused endpoint type must
  457. * be programmed to bulk.
  458. */
  459. epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
  460. if ((epctrlx & EPCTRL_RX_ENABLE) == 0) {
  461. epctrlx |= (USB_ENDPOINT_XFER_BULK
  462. << EPCTRL_RX_EP_TYPE_SHIFT);
  463. writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
  464. }
  465. epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
  466. if ((epctrlx & EPCTRL_TX_ENABLE) == 0) {
  467. epctrlx |= (USB_ENDPOINT_XFER_BULK
  468. << EPCTRL_TX_EP_TYPE_SHIFT);
  469. writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
  470. }
  471. spin_unlock_irqrestore(&udc->lock, flags);
  472. return 0;
  473. en_done:
  474. return -EINVAL;
  475. }
  476. static int mv_ep_disable(struct usb_ep *_ep)
  477. {
  478. struct mv_udc *udc;
  479. struct mv_ep *ep;
  480. struct mv_dqh *dqh;
  481. u32 bit_pos, epctrlx, direction;
  482. unsigned long flags;
  483. ep = container_of(_ep, struct mv_ep, ep);
  484. if ((_ep == NULL) || !ep->ep.desc)
  485. return -EINVAL;
  486. udc = ep->udc;
  487. /* Get the endpoint queue head address */
  488. dqh = ep->dqh;
  489. spin_lock_irqsave(&udc->lock, flags);
  490. direction = ep_dir(ep);
  491. bit_pos = 1 << ((direction == EP_DIR_OUT ? 0 : 16) + ep->ep_num);
  492. /* Reset the max packet length and the interrupt on Setup */
  493. dqh->max_packet_length = 0;
  494. /* Disable the endpoint for Rx or Tx and reset the endpoint type */
  495. epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
  496. epctrlx &= ~((direction == EP_DIR_IN)
  497. ? (EPCTRL_TX_ENABLE | EPCTRL_TX_TYPE)
  498. : (EPCTRL_RX_ENABLE | EPCTRL_RX_TYPE));
  499. writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
  500. /* nuke all pending requests (does flush) */
  501. nuke(ep, -ESHUTDOWN);
  502. ep->ep.desc = NULL;
  503. ep->stopped = 1;
  504. spin_unlock_irqrestore(&udc->lock, flags);
  505. return 0;
  506. }
  507. static struct usb_request *
  508. mv_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  509. {
  510. struct mv_req *req = NULL;
  511. req = kzalloc(sizeof *req, gfp_flags);
  512. if (!req)
  513. return NULL;
  514. req->req.dma = DMA_ADDR_INVALID;
  515. INIT_LIST_HEAD(&req->queue);
  516. return &req->req;
  517. }
  518. static void mv_free_request(struct usb_ep *_ep, struct usb_request *_req)
  519. {
  520. struct mv_req *req = NULL;
  521. req = container_of(_req, struct mv_req, req);
  522. if (_req)
  523. kfree(req);
  524. }
  525. static void mv_ep_fifo_flush(struct usb_ep *_ep)
  526. {
  527. struct mv_udc *udc;
  528. u32 bit_pos, direction;
  529. struct mv_ep *ep;
  530. unsigned int loops;
  531. if (!_ep)
  532. return;
  533. ep = container_of(_ep, struct mv_ep, ep);
  534. if (!ep->ep.desc)
  535. return;
  536. udc = ep->udc;
  537. direction = ep_dir(ep);
  538. if (ep->ep_num == 0)
  539. bit_pos = (1 << 16) | 1;
  540. else if (direction == EP_DIR_OUT)
  541. bit_pos = 1 << ep->ep_num;
  542. else
  543. bit_pos = 1 << (16 + ep->ep_num);
  544. loops = LOOPS(EPSTATUS_TIMEOUT);
  545. do {
  546. unsigned int inter_loops;
  547. if (loops == 0) {
  548. dev_err(&udc->dev->dev,
  549. "TIMEOUT for ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
  550. (unsigned)readl(&udc->op_regs->epstatus),
  551. (unsigned)bit_pos);
  552. return;
  553. }
  554. /* Write 1 to the Flush register */
  555. writel(bit_pos, &udc->op_regs->epflush);
  556. /* Wait until flushing completed */
  557. inter_loops = LOOPS(FLUSH_TIMEOUT);
  558. while (readl(&udc->op_regs->epflush)) {
  559. /*
  560. * ENDPTFLUSH bit should be cleared to indicate this
  561. * operation is complete
  562. */
  563. if (inter_loops == 0) {
  564. dev_err(&udc->dev->dev,
  565. "TIMEOUT for ENDPTFLUSH=0x%x,"
  566. "bit_pos=0x%x\n",
  567. (unsigned)readl(&udc->op_regs->epflush),
  568. (unsigned)bit_pos);
  569. return;
  570. }
  571. inter_loops--;
  572. udelay(LOOPS_USEC);
  573. }
  574. loops--;
  575. } while (readl(&udc->op_regs->epstatus) & bit_pos);
  576. }
  577. /* queues (submits) an I/O request to an endpoint */
  578. static int
  579. mv_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  580. {
  581. struct mv_ep *ep = container_of(_ep, struct mv_ep, ep);
  582. struct mv_req *req = container_of(_req, struct mv_req, req);
  583. struct mv_udc *udc = ep->udc;
  584. unsigned long flags;
  585. int retval;
  586. /* catch various bogus parameters */
  587. if (!_req || !req->req.complete || !req->req.buf
  588. || !list_empty(&req->queue)) {
  589. dev_err(&udc->dev->dev, "%s, bad params", __func__);
  590. return -EINVAL;
  591. }
  592. if (unlikely(!_ep || !ep->ep.desc)) {
  593. dev_err(&udc->dev->dev, "%s, bad ep", __func__);
  594. return -EINVAL;
  595. }
  596. udc = ep->udc;
  597. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
  598. return -ESHUTDOWN;
  599. req->ep = ep;
  600. /* map virtual address to hardware */
  601. retval = usb_gadget_map_request(&udc->gadget, _req, ep_dir(ep));
  602. if (retval)
  603. return retval;
  604. req->req.status = -EINPROGRESS;
  605. req->req.actual = 0;
  606. req->dtd_count = 0;
  607. spin_lock_irqsave(&udc->lock, flags);
  608. /* build dtds and push them to device queue */
  609. if (!req_to_dtd(req)) {
  610. retval = queue_dtd(ep, req);
  611. if (retval) {
  612. spin_unlock_irqrestore(&udc->lock, flags);
  613. dev_err(&udc->dev->dev, "Failed to queue dtd\n");
  614. goto err_unmap_dma;
  615. }
  616. } else {
  617. spin_unlock_irqrestore(&udc->lock, flags);
  618. dev_err(&udc->dev->dev, "Failed to dma_pool_alloc\n");
  619. retval = -ENOMEM;
  620. goto err_unmap_dma;
  621. }
  622. /* Update ep0 state */
  623. if (ep->ep_num == 0)
  624. udc->ep0_state = DATA_STATE_XMIT;
  625. /* irq handler advances the queue */
  626. list_add_tail(&req->queue, &ep->queue);
  627. spin_unlock_irqrestore(&udc->lock, flags);
  628. return 0;
  629. err_unmap_dma:
  630. usb_gadget_unmap_request(&udc->gadget, _req, ep_dir(ep));
  631. return retval;
  632. }
  633. static void mv_prime_ep(struct mv_ep *ep, struct mv_req *req)
  634. {
  635. struct mv_dqh *dqh = ep->dqh;
  636. u32 bit_pos;
  637. /* Write dQH next pointer and terminate bit to 0 */
  638. dqh->next_dtd_ptr = req->head->td_dma
  639. & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
  640. /* clear active and halt bit, in case set from a previous error */
  641. dqh->size_ioc_int_sts &= ~(DTD_STATUS_ACTIVE | DTD_STATUS_HALTED);
  642. /* Ensure that updates to the QH will occure before priming. */
  643. wmb();
  644. bit_pos = 1 << (((ep_dir(ep) == EP_DIR_OUT) ? 0 : 16) + ep->ep_num);
  645. /* Prime the Endpoint */
  646. writel(bit_pos, &ep->udc->op_regs->epprime);
  647. }
  648. /* dequeues (cancels, unlinks) an I/O request from an endpoint */
  649. static int mv_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  650. {
  651. struct mv_ep *ep = container_of(_ep, struct mv_ep, ep);
  652. struct mv_req *req;
  653. struct mv_udc *udc = ep->udc;
  654. unsigned long flags;
  655. int stopped, ret = 0;
  656. u32 epctrlx;
  657. if (!_ep || !_req)
  658. return -EINVAL;
  659. spin_lock_irqsave(&ep->udc->lock, flags);
  660. stopped = ep->stopped;
  661. /* Stop the ep before we deal with the queue */
  662. ep->stopped = 1;
  663. epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
  664. if (ep_dir(ep) == EP_DIR_IN)
  665. epctrlx &= ~EPCTRL_TX_ENABLE;
  666. else
  667. epctrlx &= ~EPCTRL_RX_ENABLE;
  668. writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
  669. /* make sure it's actually queued on this endpoint */
  670. list_for_each_entry(req, &ep->queue, queue) {
  671. if (&req->req == _req)
  672. break;
  673. }
  674. if (&req->req != _req) {
  675. ret = -EINVAL;
  676. goto out;
  677. }
  678. /* The request is in progress, or completed but not dequeued */
  679. if (ep->queue.next == &req->queue) {
  680. _req->status = -ECONNRESET;
  681. mv_ep_fifo_flush(_ep); /* flush current transfer */
  682. /* The request isn't the last request in this ep queue */
  683. if (req->queue.next != &ep->queue) {
  684. struct mv_req *next_req;
  685. next_req = list_entry(req->queue.next,
  686. struct mv_req, queue);
  687. /* Point the QH to the first TD of next request */
  688. mv_prime_ep(ep, next_req);
  689. } else {
  690. struct mv_dqh *qh;
  691. qh = ep->dqh;
  692. qh->next_dtd_ptr = 1;
  693. qh->size_ioc_int_sts = 0;
  694. }
  695. /* The request hasn't been processed, patch up the TD chain */
  696. } else {
  697. struct mv_req *prev_req;
  698. prev_req = list_entry(req->queue.prev, struct mv_req, queue);
  699. writel(readl(&req->tail->dtd_next),
  700. &prev_req->tail->dtd_next);
  701. }
  702. done(ep, req, -ECONNRESET);
  703. /* Enable EP */
  704. out:
  705. epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
  706. if (ep_dir(ep) == EP_DIR_IN)
  707. epctrlx |= EPCTRL_TX_ENABLE;
  708. else
  709. epctrlx |= EPCTRL_RX_ENABLE;
  710. writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
  711. ep->stopped = stopped;
  712. spin_unlock_irqrestore(&ep->udc->lock, flags);
  713. return ret;
  714. }
  715. static void ep_set_stall(struct mv_udc *udc, u8 ep_num, u8 direction, int stall)
  716. {
  717. u32 epctrlx;
  718. epctrlx = readl(&udc->op_regs->epctrlx[ep_num]);
  719. if (stall) {
  720. if (direction == EP_DIR_IN)
  721. epctrlx |= EPCTRL_TX_EP_STALL;
  722. else
  723. epctrlx |= EPCTRL_RX_EP_STALL;
  724. } else {
  725. if (direction == EP_DIR_IN) {
  726. epctrlx &= ~EPCTRL_TX_EP_STALL;
  727. epctrlx |= EPCTRL_TX_DATA_TOGGLE_RST;
  728. } else {
  729. epctrlx &= ~EPCTRL_RX_EP_STALL;
  730. epctrlx |= EPCTRL_RX_DATA_TOGGLE_RST;
  731. }
  732. }
  733. writel(epctrlx, &udc->op_regs->epctrlx[ep_num]);
  734. }
  735. static int ep_is_stall(struct mv_udc *udc, u8 ep_num, u8 direction)
  736. {
  737. u32 epctrlx;
  738. epctrlx = readl(&udc->op_regs->epctrlx[ep_num]);
  739. if (direction == EP_DIR_OUT)
  740. return (epctrlx & EPCTRL_RX_EP_STALL) ? 1 : 0;
  741. else
  742. return (epctrlx & EPCTRL_TX_EP_STALL) ? 1 : 0;
  743. }
  744. static int mv_ep_set_halt_wedge(struct usb_ep *_ep, int halt, int wedge)
  745. {
  746. struct mv_ep *ep;
  747. unsigned long flags = 0;
  748. int status = 0;
  749. struct mv_udc *udc;
  750. ep = container_of(_ep, struct mv_ep, ep);
  751. udc = ep->udc;
  752. if (!_ep || !ep->ep.desc) {
  753. status = -EINVAL;
  754. goto out;
  755. }
  756. if (ep->ep.desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  757. status = -EOPNOTSUPP;
  758. goto out;
  759. }
  760. /*
  761. * Attempt to halt IN ep will fail if any transfer requests
  762. * are still queue
  763. */
  764. if (halt && (ep_dir(ep) == EP_DIR_IN) && !list_empty(&ep->queue)) {
  765. status = -EAGAIN;
  766. goto out;
  767. }
  768. spin_lock_irqsave(&ep->udc->lock, flags);
  769. ep_set_stall(udc, ep->ep_num, ep_dir(ep), halt);
  770. if (halt && wedge)
  771. ep->wedge = 1;
  772. else if (!halt)
  773. ep->wedge = 0;
  774. spin_unlock_irqrestore(&ep->udc->lock, flags);
  775. if (ep->ep_num == 0) {
  776. udc->ep0_state = WAIT_FOR_SETUP;
  777. udc->ep0_dir = EP_DIR_OUT;
  778. }
  779. out:
  780. return status;
  781. }
  782. static int mv_ep_set_halt(struct usb_ep *_ep, int halt)
  783. {
  784. return mv_ep_set_halt_wedge(_ep, halt, 0);
  785. }
  786. static int mv_ep_set_wedge(struct usb_ep *_ep)
  787. {
  788. return mv_ep_set_halt_wedge(_ep, 1, 1);
  789. }
  790. static struct usb_ep_ops mv_ep_ops = {
  791. .enable = mv_ep_enable,
  792. .disable = mv_ep_disable,
  793. .alloc_request = mv_alloc_request,
  794. .free_request = mv_free_request,
  795. .queue = mv_ep_queue,
  796. .dequeue = mv_ep_dequeue,
  797. .set_wedge = mv_ep_set_wedge,
  798. .set_halt = mv_ep_set_halt,
  799. .fifo_flush = mv_ep_fifo_flush, /* flush fifo */
  800. };
  801. static void udc_clock_enable(struct mv_udc *udc)
  802. {
  803. unsigned int i;
  804. for (i = 0; i < udc->clknum; i++)
  805. clk_prepare_enable(udc->clk[i]);
  806. }
  807. static void udc_clock_disable(struct mv_udc *udc)
  808. {
  809. unsigned int i;
  810. for (i = 0; i < udc->clknum; i++)
  811. clk_disable_unprepare(udc->clk[i]);
  812. }
  813. static void udc_stop(struct mv_udc *udc)
  814. {
  815. u32 tmp;
  816. /* Disable interrupts */
  817. tmp = readl(&udc->op_regs->usbintr);
  818. tmp &= ~(USBINTR_INT_EN | USBINTR_ERR_INT_EN |
  819. USBINTR_PORT_CHANGE_DETECT_EN | USBINTR_RESET_EN);
  820. writel(tmp, &udc->op_regs->usbintr);
  821. udc->stopped = 1;
  822. /* Reset the Run the bit in the command register to stop VUSB */
  823. tmp = readl(&udc->op_regs->usbcmd);
  824. tmp &= ~USBCMD_RUN_STOP;
  825. writel(tmp, &udc->op_regs->usbcmd);
  826. }
  827. static void udc_start(struct mv_udc *udc)
  828. {
  829. u32 usbintr;
  830. usbintr = USBINTR_INT_EN | USBINTR_ERR_INT_EN
  831. | USBINTR_PORT_CHANGE_DETECT_EN
  832. | USBINTR_RESET_EN | USBINTR_DEVICE_SUSPEND;
  833. /* Enable interrupts */
  834. writel(usbintr, &udc->op_regs->usbintr);
  835. udc->stopped = 0;
  836. /* Set the Run bit in the command register */
  837. writel(USBCMD_RUN_STOP, &udc->op_regs->usbcmd);
  838. }
  839. static int udc_reset(struct mv_udc *udc)
  840. {
  841. unsigned int loops;
  842. u32 tmp, portsc;
  843. /* Stop the controller */
  844. tmp = readl(&udc->op_regs->usbcmd);
  845. tmp &= ~USBCMD_RUN_STOP;
  846. writel(tmp, &udc->op_regs->usbcmd);
  847. /* Reset the controller to get default values */
  848. writel(USBCMD_CTRL_RESET, &udc->op_regs->usbcmd);
  849. /* wait for reset to complete */
  850. loops = LOOPS(RESET_TIMEOUT);
  851. while (readl(&udc->op_regs->usbcmd) & USBCMD_CTRL_RESET) {
  852. if (loops == 0) {
  853. dev_err(&udc->dev->dev,
  854. "Wait for RESET completed TIMEOUT\n");
  855. return -ETIMEDOUT;
  856. }
  857. loops--;
  858. udelay(LOOPS_USEC);
  859. }
  860. /* set controller to device mode */
  861. tmp = readl(&udc->op_regs->usbmode);
  862. tmp |= USBMODE_CTRL_MODE_DEVICE;
  863. /* turn setup lockout off, require setup tripwire in usbcmd */
  864. tmp |= USBMODE_SETUP_LOCK_OFF;
  865. writel(tmp, &udc->op_regs->usbmode);
  866. writel(0x0, &udc->op_regs->epsetupstat);
  867. /* Configure the Endpoint List Address */
  868. writel(udc->ep_dqh_dma & USB_EP_LIST_ADDRESS_MASK,
  869. &udc->op_regs->eplistaddr);
  870. portsc = readl(&udc->op_regs->portsc[0]);
  871. if (readl(&udc->cap_regs->hcsparams) & HCSPARAMS_PPC)
  872. portsc &= (~PORTSCX_W1C_BITS | ~PORTSCX_PORT_POWER);
  873. if (udc->force_fs)
  874. portsc |= PORTSCX_FORCE_FULL_SPEED_CONNECT;
  875. else
  876. portsc &= (~PORTSCX_FORCE_FULL_SPEED_CONNECT);
  877. writel(portsc, &udc->op_regs->portsc[0]);
  878. tmp = readl(&udc->op_regs->epctrlx[0]);
  879. tmp &= ~(EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL);
  880. writel(tmp, &udc->op_regs->epctrlx[0]);
  881. return 0;
  882. }
  883. static int mv_udc_enable_internal(struct mv_udc *udc)
  884. {
  885. int retval;
  886. if (udc->active)
  887. return 0;
  888. dev_dbg(&udc->dev->dev, "enable udc\n");
  889. udc_clock_enable(udc);
  890. if (udc->pdata->phy_init) {
  891. retval = udc->pdata->phy_init(udc->phy_regs);
  892. if (retval) {
  893. dev_err(&udc->dev->dev,
  894. "init phy error %d\n", retval);
  895. udc_clock_disable(udc);
  896. return retval;
  897. }
  898. }
  899. udc->active = 1;
  900. return 0;
  901. }
  902. static int mv_udc_enable(struct mv_udc *udc)
  903. {
  904. if (udc->clock_gating)
  905. return mv_udc_enable_internal(udc);
  906. return 0;
  907. }
  908. static void mv_udc_disable_internal(struct mv_udc *udc)
  909. {
  910. if (udc->active) {
  911. dev_dbg(&udc->dev->dev, "disable udc\n");
  912. if (udc->pdata->phy_deinit)
  913. udc->pdata->phy_deinit(udc->phy_regs);
  914. udc_clock_disable(udc);
  915. udc->active = 0;
  916. }
  917. }
  918. static void mv_udc_disable(struct mv_udc *udc)
  919. {
  920. if (udc->clock_gating)
  921. mv_udc_disable_internal(udc);
  922. }
  923. static int mv_udc_get_frame(struct usb_gadget *gadget)
  924. {
  925. struct mv_udc *udc;
  926. u16 retval;
  927. if (!gadget)
  928. return -ENODEV;
  929. udc = container_of(gadget, struct mv_udc, gadget);
  930. retval = readl(&udc->op_regs->frindex) & USB_FRINDEX_MASKS;
  931. return retval;
  932. }
  933. /* Tries to wake up the host connected to this gadget */
  934. static int mv_udc_wakeup(struct usb_gadget *gadget)
  935. {
  936. struct mv_udc *udc = container_of(gadget, struct mv_udc, gadget);
  937. u32 portsc;
  938. /* Remote wakeup feature not enabled by host */
  939. if (!udc->remote_wakeup)
  940. return -ENOTSUPP;
  941. portsc = readl(&udc->op_regs->portsc);
  942. /* not suspended? */
  943. if (!(portsc & PORTSCX_PORT_SUSPEND))
  944. return 0;
  945. /* trigger force resume */
  946. portsc |= PORTSCX_PORT_FORCE_RESUME;
  947. writel(portsc, &udc->op_regs->portsc[0]);
  948. return 0;
  949. }
  950. static int mv_udc_vbus_session(struct usb_gadget *gadget, int is_active)
  951. {
  952. struct mv_udc *udc;
  953. unsigned long flags;
  954. int retval = 0;
  955. udc = container_of(gadget, struct mv_udc, gadget);
  956. spin_lock_irqsave(&udc->lock, flags);
  957. udc->vbus_active = (is_active != 0);
  958. dev_dbg(&udc->dev->dev, "%s: softconnect %d, vbus_active %d\n",
  959. __func__, udc->softconnect, udc->vbus_active);
  960. if (udc->driver && udc->softconnect && udc->vbus_active) {
  961. retval = mv_udc_enable(udc);
  962. if (retval == 0) {
  963. /* Clock is disabled, need re-init registers */
  964. udc_reset(udc);
  965. ep0_reset(udc);
  966. udc_start(udc);
  967. }
  968. } else if (udc->driver && udc->softconnect) {
  969. if (!udc->active)
  970. goto out;
  971. /* stop all the transfer in queue*/
  972. stop_activity(udc, udc->driver);
  973. udc_stop(udc);
  974. mv_udc_disable(udc);
  975. }
  976. out:
  977. spin_unlock_irqrestore(&udc->lock, flags);
  978. return retval;
  979. }
  980. static int mv_udc_pullup(struct usb_gadget *gadget, int is_on)
  981. {
  982. struct mv_udc *udc;
  983. unsigned long flags;
  984. int retval = 0;
  985. udc = container_of(gadget, struct mv_udc, gadget);
  986. spin_lock_irqsave(&udc->lock, flags);
  987. udc->softconnect = (is_on != 0);
  988. dev_dbg(&udc->dev->dev, "%s: softconnect %d, vbus_active %d\n",
  989. __func__, udc->softconnect, udc->vbus_active);
  990. if (udc->driver && udc->softconnect && udc->vbus_active) {
  991. retval = mv_udc_enable(udc);
  992. if (retval == 0) {
  993. /* Clock is disabled, need re-init registers */
  994. udc_reset(udc);
  995. ep0_reset(udc);
  996. udc_start(udc);
  997. }
  998. } else if (udc->driver && udc->vbus_active) {
  999. /* stop all the transfer in queue*/
  1000. stop_activity(udc, udc->driver);
  1001. udc_stop(udc);
  1002. mv_udc_disable(udc);
  1003. }
  1004. spin_unlock_irqrestore(&udc->lock, flags);
  1005. return retval;
  1006. }
  1007. static int mv_udc_start(struct usb_gadget *, struct usb_gadget_driver *);
  1008. static int mv_udc_stop(struct usb_gadget *, struct usb_gadget_driver *);
  1009. /* device controller usb_gadget_ops structure */
  1010. static const struct usb_gadget_ops mv_ops = {
  1011. /* returns the current frame number */
  1012. .get_frame = mv_udc_get_frame,
  1013. /* tries to wake up the host connected to this gadget */
  1014. .wakeup = mv_udc_wakeup,
  1015. /* notify controller that VBUS is powered or not */
  1016. .vbus_session = mv_udc_vbus_session,
  1017. /* D+ pullup, software-controlled connect/disconnect to USB host */
  1018. .pullup = mv_udc_pullup,
  1019. .udc_start = mv_udc_start,
  1020. .udc_stop = mv_udc_stop,
  1021. };
  1022. static int eps_init(struct mv_udc *udc)
  1023. {
  1024. struct mv_ep *ep;
  1025. char name[14];
  1026. int i;
  1027. /* initialize ep0 */
  1028. ep = &udc->eps[0];
  1029. ep->udc = udc;
  1030. strncpy(ep->name, "ep0", sizeof(ep->name));
  1031. ep->ep.name = ep->name;
  1032. ep->ep.ops = &mv_ep_ops;
  1033. ep->wedge = 0;
  1034. ep->stopped = 0;
  1035. ep->ep.maxpacket = EP0_MAX_PKT_SIZE;
  1036. ep->ep_num = 0;
  1037. ep->ep.desc = &mv_ep0_desc;
  1038. INIT_LIST_HEAD(&ep->queue);
  1039. ep->ep_type = USB_ENDPOINT_XFER_CONTROL;
  1040. /* initialize other endpoints */
  1041. for (i = 2; i < udc->max_eps * 2; i++) {
  1042. ep = &udc->eps[i];
  1043. if (i % 2) {
  1044. snprintf(name, sizeof(name), "ep%din", i / 2);
  1045. ep->direction = EP_DIR_IN;
  1046. } else {
  1047. snprintf(name, sizeof(name), "ep%dout", i / 2);
  1048. ep->direction = EP_DIR_OUT;
  1049. }
  1050. ep->udc = udc;
  1051. strncpy(ep->name, name, sizeof(ep->name));
  1052. ep->ep.name = ep->name;
  1053. ep->ep.ops = &mv_ep_ops;
  1054. ep->stopped = 0;
  1055. ep->ep.maxpacket = (unsigned short) ~0;
  1056. ep->ep_num = i / 2;
  1057. INIT_LIST_HEAD(&ep->queue);
  1058. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  1059. ep->dqh = &udc->ep_dqh[i];
  1060. }
  1061. return 0;
  1062. }
  1063. /* delete all endpoint requests, called with spinlock held */
  1064. static void nuke(struct mv_ep *ep, int status)
  1065. {
  1066. /* called with spinlock held */
  1067. ep->stopped = 1;
  1068. /* endpoint fifo flush */
  1069. mv_ep_fifo_flush(&ep->ep);
  1070. while (!list_empty(&ep->queue)) {
  1071. struct mv_req *req = NULL;
  1072. req = list_entry(ep->queue.next, struct mv_req, queue);
  1073. done(ep, req, status);
  1074. }
  1075. }
  1076. /* stop all USB activities */
  1077. static void stop_activity(struct mv_udc *udc, struct usb_gadget_driver *driver)
  1078. {
  1079. struct mv_ep *ep;
  1080. nuke(&udc->eps[0], -ESHUTDOWN);
  1081. list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
  1082. nuke(ep, -ESHUTDOWN);
  1083. }
  1084. /* report disconnect; the driver is already quiesced */
  1085. if (driver) {
  1086. spin_unlock(&udc->lock);
  1087. driver->disconnect(&udc->gadget);
  1088. spin_lock(&udc->lock);
  1089. }
  1090. }
  1091. static int mv_udc_start(struct usb_gadget *gadget,
  1092. struct usb_gadget_driver *driver)
  1093. {
  1094. struct mv_udc *udc;
  1095. int retval = 0;
  1096. unsigned long flags;
  1097. udc = container_of(gadget, struct mv_udc, gadget);
  1098. if (udc->driver)
  1099. return -EBUSY;
  1100. spin_lock_irqsave(&udc->lock, flags);
  1101. /* hook up the driver ... */
  1102. driver->driver.bus = NULL;
  1103. udc->driver = driver;
  1104. udc->usb_state = USB_STATE_ATTACHED;
  1105. udc->ep0_state = WAIT_FOR_SETUP;
  1106. udc->ep0_dir = EP_DIR_OUT;
  1107. spin_unlock_irqrestore(&udc->lock, flags);
  1108. if (udc->transceiver) {
  1109. retval = otg_set_peripheral(udc->transceiver->otg,
  1110. &udc->gadget);
  1111. if (retval) {
  1112. dev_err(&udc->dev->dev,
  1113. "unable to register peripheral to otg\n");
  1114. udc->driver = NULL;
  1115. return retval;
  1116. }
  1117. }
  1118. /* pullup is always on */
  1119. mv_udc_pullup(&udc->gadget, 1);
  1120. /* When boot with cable attached, there will be no vbus irq occurred */
  1121. if (udc->qwork)
  1122. queue_work(udc->qwork, &udc->vbus_work);
  1123. return 0;
  1124. }
  1125. static int mv_udc_stop(struct usb_gadget *gadget,
  1126. struct usb_gadget_driver *driver)
  1127. {
  1128. struct mv_udc *udc;
  1129. unsigned long flags;
  1130. udc = container_of(gadget, struct mv_udc, gadget);
  1131. spin_lock_irqsave(&udc->lock, flags);
  1132. mv_udc_enable(udc);
  1133. udc_stop(udc);
  1134. /* stop all usb activities */
  1135. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1136. stop_activity(udc, driver);
  1137. mv_udc_disable(udc);
  1138. spin_unlock_irqrestore(&udc->lock, flags);
  1139. /* unbind gadget driver */
  1140. udc->driver = NULL;
  1141. return 0;
  1142. }
  1143. static void mv_set_ptc(struct mv_udc *udc, u32 mode)
  1144. {
  1145. u32 portsc;
  1146. portsc = readl(&udc->op_regs->portsc[0]);
  1147. portsc |= mode << 16;
  1148. writel(portsc, &udc->op_regs->portsc[0]);
  1149. }
  1150. static void prime_status_complete(struct usb_ep *ep, struct usb_request *_req)
  1151. {
  1152. struct mv_ep *mvep = container_of(ep, struct mv_ep, ep);
  1153. struct mv_req *req = container_of(_req, struct mv_req, req);
  1154. struct mv_udc *udc;
  1155. unsigned long flags;
  1156. udc = mvep->udc;
  1157. dev_info(&udc->dev->dev, "switch to test mode %d\n", req->test_mode);
  1158. spin_lock_irqsave(&udc->lock, flags);
  1159. if (req->test_mode) {
  1160. mv_set_ptc(udc, req->test_mode);
  1161. req->test_mode = 0;
  1162. }
  1163. spin_unlock_irqrestore(&udc->lock, flags);
  1164. }
  1165. static int
  1166. udc_prime_status(struct mv_udc *udc, u8 direction, u16 status, bool empty)
  1167. {
  1168. int retval = 0;
  1169. struct mv_req *req;
  1170. struct mv_ep *ep;
  1171. ep = &udc->eps[0];
  1172. udc->ep0_dir = direction;
  1173. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1174. req = udc->status_req;
  1175. /* fill in the reqest structure */
  1176. if (empty == false) {
  1177. *((u16 *) req->req.buf) = cpu_to_le16(status);
  1178. req->req.length = 2;
  1179. } else
  1180. req->req.length = 0;
  1181. req->ep = ep;
  1182. req->req.status = -EINPROGRESS;
  1183. req->req.actual = 0;
  1184. if (udc->test_mode) {
  1185. req->req.complete = prime_status_complete;
  1186. req->test_mode = udc->test_mode;
  1187. udc->test_mode = 0;
  1188. } else
  1189. req->req.complete = NULL;
  1190. req->dtd_count = 0;
  1191. if (req->req.dma == DMA_ADDR_INVALID) {
  1192. req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
  1193. req->req.buf, req->req.length,
  1194. ep_dir(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1195. req->mapped = 1;
  1196. }
  1197. /* prime the data phase */
  1198. if (!req_to_dtd(req)) {
  1199. retval = queue_dtd(ep, req);
  1200. if (retval) {
  1201. dev_err(&udc->dev->dev,
  1202. "Failed to queue dtd when prime status\n");
  1203. goto out;
  1204. }
  1205. } else{ /* no mem */
  1206. retval = -ENOMEM;
  1207. dev_err(&udc->dev->dev,
  1208. "Failed to dma_pool_alloc when prime status\n");
  1209. goto out;
  1210. }
  1211. list_add_tail(&req->queue, &ep->queue);
  1212. return 0;
  1213. out:
  1214. usb_gadget_unmap_request(&udc->gadget, &req->req, ep_dir(ep));
  1215. return retval;
  1216. }
  1217. static void mv_udc_testmode(struct mv_udc *udc, u16 index)
  1218. {
  1219. if (index <= TEST_FORCE_EN) {
  1220. udc->test_mode = index;
  1221. if (udc_prime_status(udc, EP_DIR_IN, 0, true))
  1222. ep0_stall(udc);
  1223. } else
  1224. dev_err(&udc->dev->dev,
  1225. "This test mode(%d) is not supported\n", index);
  1226. }
  1227. static void ch9setaddress(struct mv_udc *udc, struct usb_ctrlrequest *setup)
  1228. {
  1229. udc->dev_addr = (u8)setup->wValue;
  1230. /* update usb state */
  1231. udc->usb_state = USB_STATE_ADDRESS;
  1232. if (udc_prime_status(udc, EP_DIR_IN, 0, true))
  1233. ep0_stall(udc);
  1234. }
  1235. static void ch9getstatus(struct mv_udc *udc, u8 ep_num,
  1236. struct usb_ctrlrequest *setup)
  1237. {
  1238. u16 status = 0;
  1239. int retval;
  1240. if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
  1241. != (USB_DIR_IN | USB_TYPE_STANDARD))
  1242. return;
  1243. if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
  1244. status = 1 << USB_DEVICE_SELF_POWERED;
  1245. status |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
  1246. } else if ((setup->bRequestType & USB_RECIP_MASK)
  1247. == USB_RECIP_INTERFACE) {
  1248. /* get interface status */
  1249. status = 0;
  1250. } else if ((setup->bRequestType & USB_RECIP_MASK)
  1251. == USB_RECIP_ENDPOINT) {
  1252. u8 ep_num, direction;
  1253. ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
  1254. direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
  1255. ? EP_DIR_IN : EP_DIR_OUT;
  1256. status = ep_is_stall(udc, ep_num, direction)
  1257. << USB_ENDPOINT_HALT;
  1258. }
  1259. retval = udc_prime_status(udc, EP_DIR_IN, status, false);
  1260. if (retval)
  1261. ep0_stall(udc);
  1262. else
  1263. udc->ep0_state = DATA_STATE_XMIT;
  1264. }
  1265. static void ch9clearfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
  1266. {
  1267. u8 ep_num;
  1268. u8 direction;
  1269. struct mv_ep *ep;
  1270. if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
  1271. == ((USB_TYPE_STANDARD | USB_RECIP_DEVICE))) {
  1272. switch (setup->wValue) {
  1273. case USB_DEVICE_REMOTE_WAKEUP:
  1274. udc->remote_wakeup = 0;
  1275. break;
  1276. default:
  1277. goto out;
  1278. }
  1279. } else if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
  1280. == ((USB_TYPE_STANDARD | USB_RECIP_ENDPOINT))) {
  1281. switch (setup->wValue) {
  1282. case USB_ENDPOINT_HALT:
  1283. ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
  1284. direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
  1285. ? EP_DIR_IN : EP_DIR_OUT;
  1286. if (setup->wValue != 0 || setup->wLength != 0
  1287. || ep_num > udc->max_eps)
  1288. goto out;
  1289. ep = &udc->eps[ep_num * 2 + direction];
  1290. if (ep->wedge == 1)
  1291. break;
  1292. spin_unlock(&udc->lock);
  1293. ep_set_stall(udc, ep_num, direction, 0);
  1294. spin_lock(&udc->lock);
  1295. break;
  1296. default:
  1297. goto out;
  1298. }
  1299. } else
  1300. goto out;
  1301. if (udc_prime_status(udc, EP_DIR_IN, 0, true))
  1302. ep0_stall(udc);
  1303. out:
  1304. return;
  1305. }
  1306. static void ch9setfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
  1307. {
  1308. u8 ep_num;
  1309. u8 direction;
  1310. if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
  1311. == ((USB_TYPE_STANDARD | USB_RECIP_DEVICE))) {
  1312. switch (setup->wValue) {
  1313. case USB_DEVICE_REMOTE_WAKEUP:
  1314. udc->remote_wakeup = 1;
  1315. break;
  1316. case USB_DEVICE_TEST_MODE:
  1317. if (setup->wIndex & 0xFF
  1318. || udc->gadget.speed != USB_SPEED_HIGH)
  1319. ep0_stall(udc);
  1320. if (udc->usb_state != USB_STATE_CONFIGURED
  1321. && udc->usb_state != USB_STATE_ADDRESS
  1322. && udc->usb_state != USB_STATE_DEFAULT)
  1323. ep0_stall(udc);
  1324. mv_udc_testmode(udc, (setup->wIndex >> 8));
  1325. goto out;
  1326. default:
  1327. goto out;
  1328. }
  1329. } else if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
  1330. == ((USB_TYPE_STANDARD | USB_RECIP_ENDPOINT))) {
  1331. switch (setup->wValue) {
  1332. case USB_ENDPOINT_HALT:
  1333. ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
  1334. direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
  1335. ? EP_DIR_IN : EP_DIR_OUT;
  1336. if (setup->wValue != 0 || setup->wLength != 0
  1337. || ep_num > udc->max_eps)
  1338. goto out;
  1339. spin_unlock(&udc->lock);
  1340. ep_set_stall(udc, ep_num, direction, 1);
  1341. spin_lock(&udc->lock);
  1342. break;
  1343. default:
  1344. goto out;
  1345. }
  1346. } else
  1347. goto out;
  1348. if (udc_prime_status(udc, EP_DIR_IN, 0, true))
  1349. ep0_stall(udc);
  1350. out:
  1351. return;
  1352. }
  1353. static void handle_setup_packet(struct mv_udc *udc, u8 ep_num,
  1354. struct usb_ctrlrequest *setup)
  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. 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 = pdev->dev.platform_data;
  1720. struct mv_udc *udc;
  1721. int retval = 0;
  1722. int clk_i = 0;
  1723. struct resource *r;
  1724. size_t size;
  1725. if (pdata == NULL) {
  1726. dev_err(&pdev->dev, "missing platform_data\n");
  1727. return -ENODEV;
  1728. }
  1729. size = sizeof(*udc) + sizeof(struct clk *) * pdata->clknum;
  1730. udc = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  1731. if (udc == NULL) {
  1732. dev_err(&pdev->dev, "failed to allocate memory for udc\n");
  1733. return -ENOMEM;
  1734. }
  1735. udc->done = &release_done;
  1736. udc->pdata = pdev->dev.platform_data;
  1737. spin_lock_init(&udc->lock);
  1738. udc->dev = pdev;
  1739. #ifdef CONFIG_USB_OTG_UTILS
  1740. if (pdata->mode == MV_USB_MODE_OTG) {
  1741. udc->transceiver = devm_usb_get_phy(&pdev->dev,
  1742. USB_PHY_TYPE_USB2);
  1743. if (IS_ERR_OR_NULL(udc->transceiver)) {
  1744. udc->transceiver = NULL;
  1745. return -ENODEV;
  1746. }
  1747. }
  1748. #endif
  1749. udc->clknum = pdata->clknum;
  1750. for (clk_i = 0; clk_i < udc->clknum; clk_i++) {
  1751. udc->clk[clk_i] = devm_clk_get(&pdev->dev,
  1752. pdata->clkname[clk_i]);
  1753. if (IS_ERR(udc->clk[clk_i])) {
  1754. retval = PTR_ERR(udc->clk[clk_i]);
  1755. return retval;
  1756. }
  1757. }
  1758. r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "capregs");
  1759. if (r == NULL) {
  1760. dev_err(&pdev->dev, "no I/O memory resource defined\n");
  1761. return -ENODEV;
  1762. }
  1763. udc->cap_regs = (struct mv_cap_regs __iomem *)
  1764. devm_ioremap(&pdev->dev, r->start, resource_size(r));
  1765. if (udc->cap_regs == NULL) {
  1766. dev_err(&pdev->dev, "failed to map I/O memory\n");
  1767. return -EBUSY;
  1768. }
  1769. r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "phyregs");
  1770. if (r == NULL) {
  1771. dev_err(&pdev->dev, "no phy I/O memory resource defined\n");
  1772. return -ENODEV;
  1773. }
  1774. udc->phy_regs = ioremap(r->start, resource_size(r));
  1775. if (udc->phy_regs == NULL) {
  1776. dev_err(&pdev->dev, "failed to map phy I/O memory\n");
  1777. return -EBUSY;
  1778. }
  1779. /* we will acces controller register, so enable the clk */
  1780. retval = mv_udc_enable_internal(udc);
  1781. if (retval)
  1782. return retval;
  1783. udc->op_regs =
  1784. (struct mv_op_regs __iomem *)((unsigned long)udc->cap_regs
  1785. + (readl(&udc->cap_regs->caplength_hciversion)
  1786. & CAPLENGTH_MASK));
  1787. udc->max_eps = readl(&udc->cap_regs->dccparams) & DCCPARAMS_DEN_MASK;
  1788. /*
  1789. * some platform will use usb to download image, it may not disconnect
  1790. * usb gadget before loading kernel. So first stop udc here.
  1791. */
  1792. udc_stop(udc);
  1793. writel(0xFFFFFFFF, &udc->op_regs->usbsts);
  1794. size = udc->max_eps * sizeof(struct mv_dqh) *2;
  1795. size = (size + DQH_ALIGNMENT - 1) & ~(DQH_ALIGNMENT - 1);
  1796. udc->ep_dqh = dma_alloc_coherent(&pdev->dev, size,
  1797. &udc->ep_dqh_dma, GFP_KERNEL);
  1798. if (udc->ep_dqh == NULL) {
  1799. dev_err(&pdev->dev, "allocate dQH memory failed\n");
  1800. retval = -ENOMEM;
  1801. goto err_disable_clock;
  1802. }
  1803. udc->ep_dqh_size = size;
  1804. /* create dTD dma_pool resource */
  1805. udc->dtd_pool = dma_pool_create("mv_dtd",
  1806. &pdev->dev,
  1807. sizeof(struct mv_dtd),
  1808. DTD_ALIGNMENT,
  1809. DMA_BOUNDARY);
  1810. if (!udc->dtd_pool) {
  1811. retval = -ENOMEM;
  1812. goto err_free_dma;
  1813. }
  1814. size = udc->max_eps * sizeof(struct mv_ep) *2;
  1815. udc->eps = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  1816. if (udc->eps == NULL) {
  1817. dev_err(&pdev->dev, "allocate ep memory failed\n");
  1818. retval = -ENOMEM;
  1819. goto err_destroy_dma;
  1820. }
  1821. /* initialize ep0 status request structure */
  1822. udc->status_req = devm_kzalloc(&pdev->dev, sizeof(struct mv_req),
  1823. GFP_KERNEL);
  1824. if (!udc->status_req) {
  1825. dev_err(&pdev->dev, "allocate status_req memory failed\n");
  1826. retval = -ENOMEM;
  1827. goto err_destroy_dma;
  1828. }
  1829. INIT_LIST_HEAD(&udc->status_req->queue);
  1830. /* allocate a small amount of memory to get valid address */
  1831. udc->status_req->req.buf = kzalloc(8, GFP_KERNEL);
  1832. udc->status_req->req.dma = DMA_ADDR_INVALID;
  1833. udc->resume_state = USB_STATE_NOTATTACHED;
  1834. udc->usb_state = USB_STATE_POWERED;
  1835. udc->ep0_dir = EP_DIR_OUT;
  1836. udc->remote_wakeup = 0;
  1837. r = platform_get_resource(udc->dev, IORESOURCE_IRQ, 0);
  1838. if (r == NULL) {
  1839. dev_err(&pdev->dev, "no IRQ resource defined\n");
  1840. retval = -ENODEV;
  1841. goto err_destroy_dma;
  1842. }
  1843. udc->irq = r->start;
  1844. if (devm_request_irq(&pdev->dev, udc->irq, mv_udc_irq,
  1845. IRQF_SHARED, driver_name, udc)) {
  1846. dev_err(&pdev->dev, "Request irq %d for UDC failed\n",
  1847. udc->irq);
  1848. retval = -ENODEV;
  1849. goto err_destroy_dma;
  1850. }
  1851. /* initialize gadget structure */
  1852. udc->gadget.ops = &mv_ops; /* usb_gadget_ops */
  1853. udc->gadget.ep0 = &udc->eps[0].ep; /* gadget ep0 */
  1854. INIT_LIST_HEAD(&udc->gadget.ep_list); /* ep_list */
  1855. udc->gadget.speed = USB_SPEED_UNKNOWN; /* speed */
  1856. udc->gadget.max_speed = USB_SPEED_HIGH; /* support dual speed */
  1857. /* the "gadget" abstracts/virtualizes the controller */
  1858. udc->gadget.dev.release = gadget_release;
  1859. udc->gadget.name = driver_name; /* gadget name */
  1860. eps_init(udc);
  1861. /* VBUS detect: we can disable/enable clock on demand.*/
  1862. if (udc->transceiver)
  1863. udc->clock_gating = 1;
  1864. else if (pdata->vbus) {
  1865. udc->clock_gating = 1;
  1866. retval = devm_request_threaded_irq(&pdev->dev,
  1867. pdata->vbus->irq, NULL,
  1868. mv_udc_vbus_irq, IRQF_ONESHOT, "vbus", udc);
  1869. if (retval) {
  1870. dev_info(&pdev->dev,
  1871. "Can not request irq for VBUS, "
  1872. "disable clock gating\n");
  1873. udc->clock_gating = 0;
  1874. }
  1875. udc->qwork = create_singlethread_workqueue("mv_udc_queue");
  1876. if (!udc->qwork) {
  1877. dev_err(&pdev->dev, "cannot create workqueue\n");
  1878. retval = -ENOMEM;
  1879. goto err_destroy_dma;
  1880. }
  1881. INIT_WORK(&udc->vbus_work, mv_udc_vbus_work);
  1882. }
  1883. /*
  1884. * When clock gating is supported, we can disable clk and phy.
  1885. * If not, it means that VBUS detection is not supported, we
  1886. * have to enable vbus active all the time to let controller work.
  1887. */
  1888. if (udc->clock_gating)
  1889. mv_udc_disable_internal(udc);
  1890. else
  1891. udc->vbus_active = 1;
  1892. retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
  1893. if (retval)
  1894. goto err_create_workqueue;
  1895. platform_set_drvdata(pdev, udc);
  1896. dev_info(&pdev->dev, "successful probe UDC device %s clock gating.\n",
  1897. udc->clock_gating ? "with" : "without");
  1898. return 0;
  1899. err_create_workqueue:
  1900. destroy_workqueue(udc->qwork);
  1901. err_destroy_dma:
  1902. dma_pool_destroy(udc->dtd_pool);
  1903. err_free_dma:
  1904. dma_free_coherent(&pdev->dev, udc->ep_dqh_size,
  1905. udc->ep_dqh, udc->ep_dqh_dma);
  1906. err_disable_clock:
  1907. mv_udc_disable_internal(udc);
  1908. return retval;
  1909. }
  1910. #ifdef CONFIG_PM
  1911. static int mv_udc_suspend(struct device *dev)
  1912. {
  1913. struct mv_udc *udc;
  1914. udc = dev_get_drvdata(dev);
  1915. /* if OTG is enabled, the following will be done in OTG driver*/
  1916. if (udc->transceiver)
  1917. return 0;
  1918. if (udc->pdata->vbus && udc->pdata->vbus->poll)
  1919. if (udc->pdata->vbus->poll() == VBUS_HIGH) {
  1920. dev_info(&udc->dev->dev, "USB cable is connected!\n");
  1921. return -EAGAIN;
  1922. }
  1923. /*
  1924. * only cable is unplugged, udc can suspend.
  1925. * So do not care about clock_gating == 1.
  1926. */
  1927. if (!udc->clock_gating) {
  1928. udc_stop(udc);
  1929. spin_lock_irq(&udc->lock);
  1930. /* stop all usb activities */
  1931. stop_activity(udc, udc->driver);
  1932. spin_unlock_irq(&udc->lock);
  1933. mv_udc_disable_internal(udc);
  1934. }
  1935. return 0;
  1936. }
  1937. static int mv_udc_resume(struct device *dev)
  1938. {
  1939. struct mv_udc *udc;
  1940. int retval;
  1941. udc = dev_get_drvdata(dev);
  1942. /* if OTG is enabled, the following will be done in OTG driver*/
  1943. if (udc->transceiver)
  1944. return 0;
  1945. if (!udc->clock_gating) {
  1946. retval = mv_udc_enable_internal(udc);
  1947. if (retval)
  1948. return retval;
  1949. if (udc->driver && udc->softconnect) {
  1950. udc_reset(udc);
  1951. ep0_reset(udc);
  1952. udc_start(udc);
  1953. }
  1954. }
  1955. return 0;
  1956. }
  1957. static const struct dev_pm_ops mv_udc_pm_ops = {
  1958. .suspend = mv_udc_suspend,
  1959. .resume = mv_udc_resume,
  1960. };
  1961. #endif
  1962. static void mv_udc_shutdown(struct platform_device *pdev)
  1963. {
  1964. struct mv_udc *udc;
  1965. u32 mode;
  1966. udc = platform_get_drvdata(pdev);
  1967. /* reset controller mode to IDLE */
  1968. mv_udc_enable(udc);
  1969. mode = readl(&udc->op_regs->usbmode);
  1970. mode &= ~3;
  1971. writel(mode, &udc->op_regs->usbmode);
  1972. mv_udc_disable(udc);
  1973. }
  1974. static struct platform_driver udc_driver = {
  1975. .probe = mv_udc_probe,
  1976. .remove = mv_udc_remove,
  1977. .shutdown = mv_udc_shutdown,
  1978. .driver = {
  1979. .owner = THIS_MODULE,
  1980. .name = "mv-udc",
  1981. #ifdef CONFIG_PM
  1982. .pm = &mv_udc_pm_ops,
  1983. #endif
  1984. },
  1985. };
  1986. module_platform_driver(udc_driver);
  1987. MODULE_ALIAS("platform:mv-udc");
  1988. MODULE_DESCRIPTION(DRIVER_DESC);
  1989. MODULE_AUTHOR("Chao Xie <chao.xie@marvell.com>");
  1990. MODULE_VERSION(DRIVER_VERSION);
  1991. MODULE_LICENSE("GPL");