mv_udc_core.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  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. /* device controller usb_gadget_ops structure */
  949. static const struct usb_gadget_ops mv_ops = {
  950. /* returns the current frame number */
  951. .get_frame = mv_udc_get_frame,
  952. /* tries to wake up the host connected to this gadget */
  953. .wakeup = mv_udc_wakeup,
  954. /* D+ pullup, software-controlled connect/disconnect to USB host */
  955. .pullup = mv_udc_pullup,
  956. };
  957. static void mv_udc_testmode(struct mv_udc *udc, u16 index, bool enter)
  958. {
  959. dev_info(&udc->dev->dev, "Test Mode is not support yet\n");
  960. }
  961. static int eps_init(struct mv_udc *udc)
  962. {
  963. struct mv_ep *ep;
  964. char name[14];
  965. int i;
  966. /* initialize ep0 */
  967. ep = &udc->eps[0];
  968. ep->udc = udc;
  969. strncpy(ep->name, "ep0", sizeof(ep->name));
  970. ep->ep.name = ep->name;
  971. ep->ep.ops = &mv_ep_ops;
  972. ep->wedge = 0;
  973. ep->stopped = 0;
  974. ep->ep.maxpacket = EP0_MAX_PKT_SIZE;
  975. ep->ep_num = 0;
  976. ep->desc = &mv_ep0_desc;
  977. INIT_LIST_HEAD(&ep->queue);
  978. ep->ep_type = USB_ENDPOINT_XFER_CONTROL;
  979. /* initialize other endpoints */
  980. for (i = 2; i < udc->max_eps * 2; i++) {
  981. ep = &udc->eps[i];
  982. if (i % 2) {
  983. snprintf(name, sizeof(name), "ep%din", i / 2);
  984. ep->direction = EP_DIR_IN;
  985. } else {
  986. snprintf(name, sizeof(name), "ep%dout", i / 2);
  987. ep->direction = EP_DIR_OUT;
  988. }
  989. ep->udc = udc;
  990. strncpy(ep->name, name, sizeof(ep->name));
  991. ep->ep.name = ep->name;
  992. ep->ep.ops = &mv_ep_ops;
  993. ep->stopped = 0;
  994. ep->ep.maxpacket = (unsigned short) ~0;
  995. ep->ep_num = i / 2;
  996. INIT_LIST_HEAD(&ep->queue);
  997. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  998. ep->dqh = &udc->ep_dqh[i];
  999. }
  1000. return 0;
  1001. }
  1002. /* delete all endpoint requests, called with spinlock held */
  1003. static void nuke(struct mv_ep *ep, int status)
  1004. {
  1005. /* called with spinlock held */
  1006. ep->stopped = 1;
  1007. /* endpoint fifo flush */
  1008. mv_ep_fifo_flush(&ep->ep);
  1009. while (!list_empty(&ep->queue)) {
  1010. struct mv_req *req = NULL;
  1011. req = list_entry(ep->queue.next, struct mv_req, queue);
  1012. done(ep, req, status);
  1013. }
  1014. }
  1015. /* stop all USB activities */
  1016. static void stop_activity(struct mv_udc *udc, struct usb_gadget_driver *driver)
  1017. {
  1018. struct mv_ep *ep;
  1019. nuke(&udc->eps[0], -ESHUTDOWN);
  1020. list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
  1021. nuke(ep, -ESHUTDOWN);
  1022. }
  1023. /* report disconnect; the driver is already quiesced */
  1024. if (driver) {
  1025. spin_unlock(&udc->lock);
  1026. driver->disconnect(&udc->gadget);
  1027. spin_lock(&udc->lock);
  1028. }
  1029. }
  1030. int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
  1031. int (*bind)(struct usb_gadget *))
  1032. {
  1033. struct mv_udc *udc = the_controller;
  1034. int retval = 0;
  1035. unsigned long flags;
  1036. if (!udc)
  1037. return -ENODEV;
  1038. if (udc->driver)
  1039. return -EBUSY;
  1040. spin_lock_irqsave(&udc->lock, flags);
  1041. /* hook up the driver ... */
  1042. driver->driver.bus = NULL;
  1043. udc->driver = driver;
  1044. udc->gadget.dev.driver = &driver->driver;
  1045. udc->usb_state = USB_STATE_ATTACHED;
  1046. udc->ep0_state = WAIT_FOR_SETUP;
  1047. udc->ep0_dir = USB_DIR_OUT;
  1048. spin_unlock_irqrestore(&udc->lock, flags);
  1049. retval = bind(&udc->gadget);
  1050. if (retval) {
  1051. dev_err(&udc->dev->dev, "bind to driver %s --> %d\n",
  1052. driver->driver.name, retval);
  1053. udc->driver = NULL;
  1054. udc->gadget.dev.driver = NULL;
  1055. return retval;
  1056. }
  1057. udc_reset(udc);
  1058. ep0_reset(udc);
  1059. udc_start(udc);
  1060. return 0;
  1061. }
  1062. EXPORT_SYMBOL(usb_gadget_probe_driver);
  1063. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1064. {
  1065. struct mv_udc *udc = the_controller;
  1066. unsigned long flags;
  1067. if (!udc)
  1068. return -ENODEV;
  1069. udc_stop(udc);
  1070. spin_lock_irqsave(&udc->lock, flags);
  1071. /* stop all usb activities */
  1072. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1073. stop_activity(udc, driver);
  1074. spin_unlock_irqrestore(&udc->lock, flags);
  1075. /* unbind gadget driver */
  1076. driver->unbind(&udc->gadget);
  1077. udc->gadget.dev.driver = NULL;
  1078. udc->driver = NULL;
  1079. return 0;
  1080. }
  1081. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1082. static int
  1083. udc_prime_status(struct mv_udc *udc, u8 direction, u16 status, bool empty)
  1084. {
  1085. int retval = 0;
  1086. struct mv_req *req;
  1087. struct mv_ep *ep;
  1088. ep = &udc->eps[0];
  1089. udc->ep0_dir = direction;
  1090. req = udc->status_req;
  1091. /* fill in the reqest structure */
  1092. if (empty == false) {
  1093. *((u16 *) req->req.buf) = cpu_to_le16(status);
  1094. req->req.length = 2;
  1095. } else
  1096. req->req.length = 0;
  1097. req->ep = ep;
  1098. req->req.status = -EINPROGRESS;
  1099. req->req.actual = 0;
  1100. req->req.complete = NULL;
  1101. req->dtd_count = 0;
  1102. /* prime the data phase */
  1103. if (!req_to_dtd(req))
  1104. retval = queue_dtd(ep, req);
  1105. else{ /* no mem */
  1106. retval = -ENOMEM;
  1107. goto out;
  1108. }
  1109. if (retval) {
  1110. dev_err(&udc->dev->dev, "response error on GET_STATUS request\n");
  1111. goto out;
  1112. }
  1113. list_add_tail(&req->queue, &ep->queue);
  1114. return 0;
  1115. out:
  1116. return retval;
  1117. }
  1118. static void ch9setaddress(struct mv_udc *udc, struct usb_ctrlrequest *setup)
  1119. {
  1120. udc->dev_addr = (u8)setup->wValue;
  1121. /* update usb state */
  1122. udc->usb_state = USB_STATE_ADDRESS;
  1123. if (udc_prime_status(udc, EP_DIR_IN, 0, true))
  1124. ep0_stall(udc);
  1125. }
  1126. static void ch9getstatus(struct mv_udc *udc, u8 ep_num,
  1127. struct usb_ctrlrequest *setup)
  1128. {
  1129. u16 status;
  1130. int retval;
  1131. if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
  1132. != (USB_DIR_IN | USB_TYPE_STANDARD))
  1133. return;
  1134. if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
  1135. status = 1 << USB_DEVICE_SELF_POWERED;
  1136. status |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
  1137. } else if ((setup->bRequestType & USB_RECIP_MASK)
  1138. == USB_RECIP_INTERFACE) {
  1139. /* get interface status */
  1140. status = 0;
  1141. } else if ((setup->bRequestType & USB_RECIP_MASK)
  1142. == USB_RECIP_ENDPOINT) {
  1143. u8 ep_num, direction;
  1144. ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
  1145. direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
  1146. ? EP_DIR_IN : EP_DIR_OUT;
  1147. status = ep_is_stall(udc, ep_num, direction)
  1148. << USB_ENDPOINT_HALT;
  1149. }
  1150. retval = udc_prime_status(udc, EP_DIR_IN, status, false);
  1151. if (retval)
  1152. ep0_stall(udc);
  1153. }
  1154. static void ch9clearfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
  1155. {
  1156. u8 ep_num;
  1157. u8 direction;
  1158. struct mv_ep *ep;
  1159. if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
  1160. == ((USB_TYPE_STANDARD | USB_RECIP_DEVICE))) {
  1161. switch (setup->wValue) {
  1162. case USB_DEVICE_REMOTE_WAKEUP:
  1163. udc->remote_wakeup = 0;
  1164. break;
  1165. case USB_DEVICE_TEST_MODE:
  1166. mv_udc_testmode(udc, 0, false);
  1167. break;
  1168. default:
  1169. goto out;
  1170. }
  1171. } else if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
  1172. == ((USB_TYPE_STANDARD | USB_RECIP_ENDPOINT))) {
  1173. switch (setup->wValue) {
  1174. case USB_ENDPOINT_HALT:
  1175. ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
  1176. direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
  1177. ? EP_DIR_IN : EP_DIR_OUT;
  1178. if (setup->wValue != 0 || setup->wLength != 0
  1179. || ep_num > udc->max_eps)
  1180. goto out;
  1181. ep = &udc->eps[ep_num * 2 + direction];
  1182. if (ep->wedge == 1)
  1183. break;
  1184. spin_unlock(&udc->lock);
  1185. ep_set_stall(udc, ep_num, direction, 0);
  1186. spin_lock(&udc->lock);
  1187. break;
  1188. default:
  1189. goto out;
  1190. }
  1191. } else
  1192. goto out;
  1193. if (udc_prime_status(udc, EP_DIR_IN, 0, true))
  1194. ep0_stall(udc);
  1195. else
  1196. udc->ep0_state = DATA_STATE_XMIT;
  1197. out:
  1198. return;
  1199. }
  1200. static void ch9setfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
  1201. {
  1202. u8 ep_num;
  1203. u8 direction;
  1204. if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
  1205. == ((USB_TYPE_STANDARD | USB_RECIP_DEVICE))) {
  1206. switch (setup->wValue) {
  1207. case USB_DEVICE_REMOTE_WAKEUP:
  1208. udc->remote_wakeup = 1;
  1209. break;
  1210. case USB_DEVICE_TEST_MODE:
  1211. if (setup->wIndex & 0xFF
  1212. && udc->gadget.speed != USB_SPEED_HIGH)
  1213. goto out;
  1214. if (udc->usb_state == USB_STATE_CONFIGURED
  1215. || udc->usb_state == USB_STATE_ADDRESS
  1216. || udc->usb_state == USB_STATE_DEFAULT)
  1217. mv_udc_testmode(udc,
  1218. setup->wIndex & 0xFF00, true);
  1219. else
  1220. goto out;
  1221. break;
  1222. default:
  1223. goto out;
  1224. }
  1225. } else if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
  1226. == ((USB_TYPE_STANDARD | USB_RECIP_ENDPOINT))) {
  1227. switch (setup->wValue) {
  1228. case USB_ENDPOINT_HALT:
  1229. ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
  1230. direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
  1231. ? EP_DIR_IN : EP_DIR_OUT;
  1232. if (setup->wValue != 0 || setup->wLength != 0
  1233. || ep_num > udc->max_eps)
  1234. goto out;
  1235. spin_unlock(&udc->lock);
  1236. ep_set_stall(udc, ep_num, direction, 1);
  1237. spin_lock(&udc->lock);
  1238. break;
  1239. default:
  1240. goto out;
  1241. }
  1242. } else
  1243. goto out;
  1244. if (udc_prime_status(udc, EP_DIR_IN, 0, true))
  1245. ep0_stall(udc);
  1246. out:
  1247. return;
  1248. }
  1249. static void handle_setup_packet(struct mv_udc *udc, u8 ep_num,
  1250. struct usb_ctrlrequest *setup)
  1251. {
  1252. bool delegate = false;
  1253. nuke(&udc->eps[ep_num * 2 + EP_DIR_OUT], -ESHUTDOWN);
  1254. dev_dbg(&udc->dev->dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  1255. setup->bRequestType, setup->bRequest,
  1256. setup->wValue, setup->wIndex, setup->wLength);
  1257. /* We process some stardard setup requests here */
  1258. if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1259. switch (setup->bRequest) {
  1260. case USB_REQ_GET_STATUS:
  1261. ch9getstatus(udc, ep_num, setup);
  1262. break;
  1263. case USB_REQ_SET_ADDRESS:
  1264. ch9setaddress(udc, setup);
  1265. break;
  1266. case USB_REQ_CLEAR_FEATURE:
  1267. ch9clearfeature(udc, setup);
  1268. break;
  1269. case USB_REQ_SET_FEATURE:
  1270. ch9setfeature(udc, setup);
  1271. break;
  1272. default:
  1273. delegate = true;
  1274. }
  1275. } else
  1276. delegate = true;
  1277. /* delegate USB standard requests to the gadget driver */
  1278. if (delegate == true) {
  1279. /* USB requests handled by gadget */
  1280. if (setup->wLength) {
  1281. /* DATA phase from gadget, STATUS phase from udc */
  1282. udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
  1283. ? EP_DIR_IN : EP_DIR_OUT;
  1284. spin_unlock(&udc->lock);
  1285. if (udc->driver->setup(&udc->gadget,
  1286. &udc->local_setup_buff) < 0)
  1287. ep0_stall(udc);
  1288. spin_lock(&udc->lock);
  1289. udc->ep0_state = (setup->bRequestType & USB_DIR_IN)
  1290. ? DATA_STATE_XMIT : DATA_STATE_RECV;
  1291. } else {
  1292. /* no DATA phase, IN STATUS phase from gadget */
  1293. udc->ep0_dir = EP_DIR_IN;
  1294. spin_unlock(&udc->lock);
  1295. if (udc->driver->setup(&udc->gadget,
  1296. &udc->local_setup_buff) < 0)
  1297. ep0_stall(udc);
  1298. spin_lock(&udc->lock);
  1299. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1300. }
  1301. }
  1302. }
  1303. /* complete DATA or STATUS phase of ep0 prime status phase if needed */
  1304. static void ep0_req_complete(struct mv_udc *udc,
  1305. struct mv_ep *ep0, struct mv_req *req)
  1306. {
  1307. u32 new_addr;
  1308. if (udc->usb_state == USB_STATE_ADDRESS) {
  1309. /* set the new address */
  1310. new_addr = (u32)udc->dev_addr;
  1311. writel(new_addr << USB_DEVICE_ADDRESS_BIT_SHIFT,
  1312. &udc->op_regs->deviceaddr);
  1313. }
  1314. done(ep0, req, 0);
  1315. switch (udc->ep0_state) {
  1316. case DATA_STATE_XMIT:
  1317. /* receive status phase */
  1318. if (udc_prime_status(udc, EP_DIR_OUT, 0, true))
  1319. ep0_stall(udc);
  1320. break;
  1321. case DATA_STATE_RECV:
  1322. /* send status phase */
  1323. if (udc_prime_status(udc, EP_DIR_IN, 0 , true))
  1324. ep0_stall(udc);
  1325. break;
  1326. case WAIT_FOR_OUT_STATUS:
  1327. udc->ep0_state = WAIT_FOR_SETUP;
  1328. break;
  1329. case WAIT_FOR_SETUP:
  1330. dev_err(&udc->dev->dev, "unexpect ep0 packets\n");
  1331. break;
  1332. default:
  1333. ep0_stall(udc);
  1334. break;
  1335. }
  1336. }
  1337. static void get_setup_data(struct mv_udc *udc, u8 ep_num, u8 *buffer_ptr)
  1338. {
  1339. u32 temp;
  1340. struct mv_dqh *dqh;
  1341. dqh = &udc->ep_dqh[ep_num * 2 + EP_DIR_OUT];
  1342. /* Clear bit in ENDPTSETUPSTAT */
  1343. temp = readl(&udc->op_regs->epsetupstat);
  1344. writel(temp | (1 << ep_num), &udc->op_regs->epsetupstat);
  1345. /* while a hazard exists when setup package arrives */
  1346. do {
  1347. /* Set Setup Tripwire */
  1348. temp = readl(&udc->op_regs->usbcmd);
  1349. writel(temp | USBCMD_SETUP_TRIPWIRE_SET, &udc->op_regs->usbcmd);
  1350. /* Copy the setup packet to local buffer */
  1351. memcpy(buffer_ptr, (u8 *) dqh->setup_buffer, 8);
  1352. } while (!(readl(&udc->op_regs->usbcmd) & USBCMD_SETUP_TRIPWIRE_SET));
  1353. /* Clear Setup Tripwire */
  1354. temp = readl(&udc->op_regs->usbcmd);
  1355. writel(temp & ~USBCMD_SETUP_TRIPWIRE_SET, &udc->op_regs->usbcmd);
  1356. }
  1357. static void irq_process_tr_complete(struct mv_udc *udc)
  1358. {
  1359. u32 tmp, bit_pos;
  1360. int i, ep_num = 0, direction = 0;
  1361. struct mv_ep *curr_ep;
  1362. struct mv_req *curr_req, *temp_req;
  1363. int status;
  1364. /*
  1365. * We use separate loops for ENDPTSETUPSTAT and ENDPTCOMPLETE
  1366. * because the setup packets are to be read ASAP
  1367. */
  1368. /* Process all Setup packet received interrupts */
  1369. tmp = readl(&udc->op_regs->epsetupstat);
  1370. if (tmp) {
  1371. for (i = 0; i < udc->max_eps; i++) {
  1372. if (tmp & (1 << i)) {
  1373. get_setup_data(udc, i,
  1374. (u8 *)(&udc->local_setup_buff));
  1375. handle_setup_packet(udc, i,
  1376. &udc->local_setup_buff);
  1377. }
  1378. }
  1379. }
  1380. /* Don't clear the endpoint setup status register here.
  1381. * It is cleared as a setup packet is read out of the buffer
  1382. */
  1383. /* Process non-setup transaction complete interrupts */
  1384. tmp = readl(&udc->op_regs->epcomplete);
  1385. if (!tmp)
  1386. return;
  1387. writel(tmp, &udc->op_regs->epcomplete);
  1388. for (i = 0; i < udc->max_eps * 2; i++) {
  1389. ep_num = i >> 1;
  1390. direction = i % 2;
  1391. bit_pos = 1 << (ep_num + 16 * direction);
  1392. if (!(bit_pos & tmp))
  1393. continue;
  1394. if (i == 1)
  1395. curr_ep = &udc->eps[0];
  1396. else
  1397. curr_ep = &udc->eps[i];
  1398. /* process the req queue until an uncomplete request */
  1399. list_for_each_entry_safe(curr_req, temp_req,
  1400. &curr_ep->queue, queue) {
  1401. status = process_ep_req(udc, i, curr_req);
  1402. if (status)
  1403. break;
  1404. /* write back status to req */
  1405. curr_req->req.status = status;
  1406. /* ep0 request completion */
  1407. if (ep_num == 0) {
  1408. ep0_req_complete(udc, curr_ep, curr_req);
  1409. break;
  1410. } else {
  1411. done(curr_ep, curr_req, status);
  1412. }
  1413. }
  1414. }
  1415. }
  1416. void irq_process_reset(struct mv_udc *udc)
  1417. {
  1418. u32 tmp;
  1419. unsigned int loops;
  1420. udc->ep0_dir = EP_DIR_OUT;
  1421. udc->ep0_state = WAIT_FOR_SETUP;
  1422. udc->remote_wakeup = 0; /* default to 0 on reset */
  1423. /* The address bits are past bit 25-31. Set the address */
  1424. tmp = readl(&udc->op_regs->deviceaddr);
  1425. tmp &= ~(USB_DEVICE_ADDRESS_MASK);
  1426. writel(tmp, &udc->op_regs->deviceaddr);
  1427. /* Clear all the setup token semaphores */
  1428. tmp = readl(&udc->op_regs->epsetupstat);
  1429. writel(tmp, &udc->op_regs->epsetupstat);
  1430. /* Clear all the endpoint complete status bits */
  1431. tmp = readl(&udc->op_regs->epcomplete);
  1432. writel(tmp, &udc->op_regs->epcomplete);
  1433. /* wait until all endptprime bits cleared */
  1434. loops = LOOPS(PRIME_TIMEOUT);
  1435. while (readl(&udc->op_regs->epprime) & 0xFFFFFFFF) {
  1436. if (loops == 0) {
  1437. dev_err(&udc->dev->dev,
  1438. "Timeout for ENDPTPRIME = 0x%x\n",
  1439. readl(&udc->op_regs->epprime));
  1440. break;
  1441. }
  1442. loops--;
  1443. udelay(LOOPS_USEC);
  1444. }
  1445. /* Write 1s to the Flush register */
  1446. writel((u32)~0, &udc->op_regs->epflush);
  1447. if (readl(&udc->op_regs->portsc[0]) & PORTSCX_PORT_RESET) {
  1448. dev_info(&udc->dev->dev, "usb bus reset\n");
  1449. udc->usb_state = USB_STATE_DEFAULT;
  1450. /* reset all the queues, stop all USB activities */
  1451. stop_activity(udc, udc->driver);
  1452. } else {
  1453. dev_info(&udc->dev->dev, "USB reset portsc 0x%x\n",
  1454. readl(&udc->op_regs->portsc));
  1455. /*
  1456. * re-initialize
  1457. * controller reset
  1458. */
  1459. udc_reset(udc);
  1460. /* reset all the queues, stop all USB activities */
  1461. stop_activity(udc, udc->driver);
  1462. /* reset ep0 dQH and endptctrl */
  1463. ep0_reset(udc);
  1464. /* enable interrupt and set controller to run state */
  1465. udc_start(udc);
  1466. udc->usb_state = USB_STATE_ATTACHED;
  1467. }
  1468. }
  1469. static void handle_bus_resume(struct mv_udc *udc)
  1470. {
  1471. udc->usb_state = udc->resume_state;
  1472. udc->resume_state = 0;
  1473. /* report resume to the driver */
  1474. if (udc->driver) {
  1475. if (udc->driver->resume) {
  1476. spin_unlock(&udc->lock);
  1477. udc->driver->resume(&udc->gadget);
  1478. spin_lock(&udc->lock);
  1479. }
  1480. }
  1481. }
  1482. static void irq_process_suspend(struct mv_udc *udc)
  1483. {
  1484. udc->resume_state = udc->usb_state;
  1485. udc->usb_state = USB_STATE_SUSPENDED;
  1486. if (udc->driver->suspend) {
  1487. spin_unlock(&udc->lock);
  1488. udc->driver->suspend(&udc->gadget);
  1489. spin_lock(&udc->lock);
  1490. }
  1491. }
  1492. static void irq_process_port_change(struct mv_udc *udc)
  1493. {
  1494. u32 portsc;
  1495. portsc = readl(&udc->op_regs->portsc[0]);
  1496. if (!(portsc & PORTSCX_PORT_RESET)) {
  1497. /* Get the speed */
  1498. u32 speed = portsc & PORTSCX_PORT_SPEED_MASK;
  1499. switch (speed) {
  1500. case PORTSCX_PORT_SPEED_HIGH:
  1501. udc->gadget.speed = USB_SPEED_HIGH;
  1502. break;
  1503. case PORTSCX_PORT_SPEED_FULL:
  1504. udc->gadget.speed = USB_SPEED_FULL;
  1505. break;
  1506. case PORTSCX_PORT_SPEED_LOW:
  1507. udc->gadget.speed = USB_SPEED_LOW;
  1508. break;
  1509. default:
  1510. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1511. break;
  1512. }
  1513. }
  1514. if (portsc & PORTSCX_PORT_SUSPEND) {
  1515. udc->resume_state = udc->usb_state;
  1516. udc->usb_state = USB_STATE_SUSPENDED;
  1517. if (udc->driver->suspend) {
  1518. spin_unlock(&udc->lock);
  1519. udc->driver->suspend(&udc->gadget);
  1520. spin_lock(&udc->lock);
  1521. }
  1522. }
  1523. if (!(portsc & PORTSCX_PORT_SUSPEND)
  1524. && udc->usb_state == USB_STATE_SUSPENDED) {
  1525. handle_bus_resume(udc);
  1526. }
  1527. if (!udc->resume_state)
  1528. udc->usb_state = USB_STATE_DEFAULT;
  1529. }
  1530. static void irq_process_error(struct mv_udc *udc)
  1531. {
  1532. /* Increment the error count */
  1533. udc->errors++;
  1534. }
  1535. static irqreturn_t mv_udc_irq(int irq, void *dev)
  1536. {
  1537. struct mv_udc *udc = (struct mv_udc *)dev;
  1538. u32 status, intr;
  1539. spin_lock(&udc->lock);
  1540. status = readl(&udc->op_regs->usbsts);
  1541. intr = readl(&udc->op_regs->usbintr);
  1542. status &= intr;
  1543. if (status == 0) {
  1544. spin_unlock(&udc->lock);
  1545. return IRQ_NONE;
  1546. }
  1547. /* Clear all the interrupts occurred */
  1548. writel(status, &udc->op_regs->usbsts);
  1549. if (status & USBSTS_ERR)
  1550. irq_process_error(udc);
  1551. if (status & USBSTS_RESET)
  1552. irq_process_reset(udc);
  1553. if (status & USBSTS_PORT_CHANGE)
  1554. irq_process_port_change(udc);
  1555. if (status & USBSTS_INT)
  1556. irq_process_tr_complete(udc);
  1557. if (status & USBSTS_SUSPEND)
  1558. irq_process_suspend(udc);
  1559. spin_unlock(&udc->lock);
  1560. return IRQ_HANDLED;
  1561. }
  1562. /* release device structure */
  1563. static void gadget_release(struct device *_dev)
  1564. {
  1565. struct mv_udc *udc = the_controller;
  1566. complete(udc->done);
  1567. kfree(udc);
  1568. }
  1569. static int mv_udc_remove(struct platform_device *dev)
  1570. {
  1571. struct mv_udc *udc = the_controller;
  1572. DECLARE_COMPLETION(done);
  1573. udc->done = &done;
  1574. /* free memory allocated in probe */
  1575. if (udc->dtd_pool)
  1576. dma_pool_destroy(udc->dtd_pool);
  1577. if (udc->ep_dqh)
  1578. dma_free_coherent(&dev->dev, udc->ep_dqh_size,
  1579. udc->ep_dqh, udc->ep_dqh_dma);
  1580. kfree(udc->eps);
  1581. if (udc->irq)
  1582. free_irq(udc->irq, &dev->dev);
  1583. if (udc->cap_regs)
  1584. iounmap(udc->cap_regs);
  1585. udc->cap_regs = NULL;
  1586. if (udc->phy_regs)
  1587. iounmap((void *)udc->phy_regs);
  1588. udc->phy_regs = 0;
  1589. if (udc->status_req) {
  1590. kfree(udc->status_req->req.buf);
  1591. kfree(udc->status_req);
  1592. }
  1593. device_unregister(&udc->gadget.dev);
  1594. /* free dev, wait for the release() finished */
  1595. wait_for_completion(&done);
  1596. the_controller = NULL;
  1597. return 0;
  1598. }
  1599. int mv_udc_probe(struct platform_device *dev)
  1600. {
  1601. struct mv_udc *udc;
  1602. int retval = 0;
  1603. struct resource *r;
  1604. size_t size;
  1605. udc = kzalloc(sizeof *udc, GFP_KERNEL);
  1606. if (udc == NULL) {
  1607. dev_err(&dev->dev, "failed to allocate memory for udc\n");
  1608. retval = -ENOMEM;
  1609. goto error;
  1610. }
  1611. spin_lock_init(&udc->lock);
  1612. udc->dev = dev;
  1613. udc->clk = clk_get(&dev->dev, "U2OCLK");
  1614. if (IS_ERR(udc->clk)) {
  1615. retval = PTR_ERR(udc->clk);
  1616. goto error;
  1617. }
  1618. r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "u2o");
  1619. if (r == NULL) {
  1620. dev_err(&dev->dev, "no I/O memory resource defined\n");
  1621. retval = -ENODEV;
  1622. goto error;
  1623. }
  1624. udc->cap_regs = (struct mv_cap_regs __iomem *)
  1625. ioremap(r->start, resource_size(r));
  1626. if (udc->cap_regs == NULL) {
  1627. dev_err(&dev->dev, "failed to map I/O memory\n");
  1628. retval = -EBUSY;
  1629. goto error;
  1630. }
  1631. r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "u2ophy");
  1632. if (r == NULL) {
  1633. dev_err(&dev->dev, "no phy I/O memory resource defined\n");
  1634. retval = -ENODEV;
  1635. goto error;
  1636. }
  1637. udc->phy_regs = (unsigned int)ioremap(r->start, resource_size(r));
  1638. if (udc->phy_regs == 0) {
  1639. dev_err(&dev->dev, "failed to map phy I/O memory\n");
  1640. retval = -EBUSY;
  1641. goto error;
  1642. }
  1643. /* we will acces controller register, so enable the clk */
  1644. clk_enable(udc->clk);
  1645. retval = mv_udc_phy_init(udc->phy_regs);
  1646. if (retval) {
  1647. dev_err(&dev->dev, "phy initialization error %d\n", retval);
  1648. goto error;
  1649. }
  1650. udc->op_regs = (struct mv_op_regs __iomem *)((u32)udc->cap_regs
  1651. + (readl(&udc->cap_regs->caplength_hciversion)
  1652. & CAPLENGTH_MASK));
  1653. udc->max_eps = readl(&udc->cap_regs->dccparams) & DCCPARAMS_DEN_MASK;
  1654. size = udc->max_eps * sizeof(struct mv_dqh) *2;
  1655. size = (size + DQH_ALIGNMENT - 1) & ~(DQH_ALIGNMENT - 1);
  1656. udc->ep_dqh = dma_alloc_coherent(&dev->dev, size,
  1657. &udc->ep_dqh_dma, GFP_KERNEL);
  1658. if (udc->ep_dqh == NULL) {
  1659. dev_err(&dev->dev, "allocate dQH memory failed\n");
  1660. retval = -ENOMEM;
  1661. goto error;
  1662. }
  1663. udc->ep_dqh_size = size;
  1664. /* create dTD dma_pool resource */
  1665. udc->dtd_pool = dma_pool_create("mv_dtd",
  1666. &dev->dev,
  1667. sizeof(struct mv_dtd),
  1668. DTD_ALIGNMENT,
  1669. DMA_BOUNDARY);
  1670. if (!udc->dtd_pool) {
  1671. retval = -ENOMEM;
  1672. goto error;
  1673. }
  1674. size = udc->max_eps * sizeof(struct mv_ep) *2;
  1675. udc->eps = kzalloc(size, GFP_KERNEL);
  1676. if (udc->eps == NULL) {
  1677. dev_err(&dev->dev, "allocate ep memory failed\n");
  1678. retval = -ENOMEM;
  1679. goto error;
  1680. }
  1681. /* initialize ep0 status request structure */
  1682. udc->status_req = kzalloc(sizeof(struct mv_req), GFP_KERNEL);
  1683. if (!udc->status_req) {
  1684. dev_err(&dev->dev, "allocate status_req memory failed\n");
  1685. retval = -ENOMEM;
  1686. goto error;
  1687. }
  1688. INIT_LIST_HEAD(&udc->status_req->queue);
  1689. /* allocate a small amount of memory to get valid address */
  1690. udc->status_req->req.buf = kzalloc(8, GFP_KERNEL);
  1691. udc->status_req->req.dma = virt_to_phys(udc->status_req->req.buf);
  1692. udc->resume_state = USB_STATE_NOTATTACHED;
  1693. udc->usb_state = USB_STATE_POWERED;
  1694. udc->ep0_dir = EP_DIR_OUT;
  1695. udc->remote_wakeup = 0;
  1696. r = platform_get_resource(udc->dev, IORESOURCE_IRQ, 0);
  1697. if (r == NULL) {
  1698. dev_err(&dev->dev, "no IRQ resource defined\n");
  1699. retval = -ENODEV;
  1700. goto error;
  1701. }
  1702. udc->irq = r->start;
  1703. if (request_irq(udc->irq, mv_udc_irq,
  1704. IRQF_DISABLED | IRQF_SHARED, driver_name, udc)) {
  1705. dev_err(&dev->dev, "Request irq %d for UDC failed\n",
  1706. udc->irq);
  1707. retval = -ENODEV;
  1708. goto error;
  1709. }
  1710. /* initialize gadget structure */
  1711. udc->gadget.ops = &mv_ops; /* usb_gadget_ops */
  1712. udc->gadget.ep0 = &udc->eps[0].ep; /* gadget ep0 */
  1713. INIT_LIST_HEAD(&udc->gadget.ep_list); /* ep_list */
  1714. udc->gadget.speed = USB_SPEED_UNKNOWN; /* speed */
  1715. udc->gadget.is_dualspeed = 1; /* support dual speed */
  1716. /* the "gadget" abstracts/virtualizes the controller */
  1717. dev_set_name(&udc->gadget.dev, "gadget");
  1718. udc->gadget.dev.parent = &dev->dev;
  1719. udc->gadget.dev.dma_mask = dev->dev.dma_mask;
  1720. udc->gadget.dev.release = gadget_release;
  1721. udc->gadget.name = driver_name; /* gadget name */
  1722. retval = device_register(&udc->gadget.dev);
  1723. if (retval)
  1724. goto error;
  1725. eps_init(udc);
  1726. the_controller = udc;
  1727. goto out;
  1728. error:
  1729. if (udc)
  1730. mv_udc_remove(udc->dev);
  1731. out:
  1732. return retval;
  1733. }
  1734. #ifdef CONFIG_PM
  1735. static int mv_udc_suspend(struct device *_dev)
  1736. {
  1737. struct mv_udc *udc = the_controller;
  1738. udc_stop(udc);
  1739. return 0;
  1740. }
  1741. static int mv_udc_resume(struct device *_dev)
  1742. {
  1743. struct mv_udc *udc = the_controller;
  1744. int retval;
  1745. retval = mv_udc_phy_init(udc->phy_regs);
  1746. if (retval) {
  1747. dev_err(_dev, "phy initialization error %d\n", retval);
  1748. return retval;
  1749. }
  1750. udc_reset(udc);
  1751. ep0_reset(udc);
  1752. udc_start(udc);
  1753. return 0;
  1754. }
  1755. static const struct dev_pm_ops mv_udc_pm_ops = {
  1756. .suspend = mv_udc_suspend,
  1757. .resume = mv_udc_resume,
  1758. };
  1759. #endif
  1760. static struct platform_driver udc_driver = {
  1761. .probe = mv_udc_probe,
  1762. .remove = __exit_p(mv_udc_remove),
  1763. .driver = {
  1764. .owner = THIS_MODULE,
  1765. .name = "pxa-u2o",
  1766. #ifdef CONFIG_PM
  1767. .pm = &mv_udc_pm_ops,
  1768. #endif
  1769. },
  1770. };
  1771. MODULE_DESCRIPTION(DRIVER_DESC);
  1772. MODULE_AUTHOR("Chao Xie <chao.xie@marvell.com>");
  1773. MODULE_VERSION(DRIVER_VERSION);
  1774. MODULE_LICENSE("GPL");
  1775. static int __init init(void)
  1776. {
  1777. return platform_driver_register(&udc_driver);
  1778. }
  1779. module_init(init);
  1780. static void __exit cleanup(void)
  1781. {
  1782. platform_driver_unregister(&udc_driver);
  1783. }
  1784. module_exit(cleanup);