mv_udc_core.c 58 KB

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