mv_udc_core.c 57 KB

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