mv_udc_core.c 59 KB

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