mv_udc_core.c 52 KB

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