mv_udc_core.c 50 KB

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