mv_udc_core.c 58 KB

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