mv_udc_core.c 58 KB

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