mv_udc_core.c 53 KB

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