mv_udc_core.c 57 KB

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