fsl_udc_core.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752
  1. /*
  2. * Copyright (C) 2004-2007 Freescale Semicondutor, Inc. All rights reserved.
  3. *
  4. * Author: Li Yang <leoli@freescale.com>
  5. * Jiang Bo <tanya.jiang@freescale.com>
  6. *
  7. * Description:
  8. * Freescale high-speed USB SOC DR module device controller driver.
  9. * This can be found on MPC8349E/MPC8313E/MPC5121E cpus.
  10. * The driver is previously named as mpc_udc. Based on bare board
  11. * code from Dave Liu and Shlomi Gridish.
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. */
  18. #undef VERBOSE
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/ioport.h>
  22. #include <linux/types.h>
  23. #include <linux/errno.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/list.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/mm.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/device.h>
  32. #include <linux/usb/ch9.h>
  33. #include <linux/usb/gadget.h>
  34. #include <linux/usb/otg.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/fsl_devices.h>
  38. #include <linux/dmapool.h>
  39. #include <linux/delay.h>
  40. #include <asm/byteorder.h>
  41. #include <asm/io.h>
  42. #include <asm/system.h>
  43. #include <asm/unaligned.h>
  44. #include <asm/dma.h>
  45. #include <asm/cacheflush.h>
  46. #include "fsl_usb2_udc.h"
  47. #define DRIVER_DESC "Freescale High-Speed USB SOC Device Controller driver"
  48. #define DRIVER_AUTHOR "Li Yang/Jiang Bo"
  49. #define DRIVER_VERSION "Apr 20, 2007"
  50. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  51. static const char driver_name[] = "fsl-usb2-udc";
  52. static const char driver_desc[] = DRIVER_DESC;
  53. static struct usb_dr_device *dr_regs;
  54. #ifndef CONFIG_ARCH_MXC
  55. static struct usb_sys_interface *usb_sys_regs;
  56. #endif
  57. /* it is initialized in probe() */
  58. static struct fsl_udc *udc_controller = NULL;
  59. static const struct usb_endpoint_descriptor
  60. fsl_ep0_desc = {
  61. .bLength = USB_DT_ENDPOINT_SIZE,
  62. .bDescriptorType = USB_DT_ENDPOINT,
  63. .bEndpointAddress = 0,
  64. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  65. .wMaxPacketSize = USB_MAX_CTRL_PAYLOAD,
  66. };
  67. static void fsl_ep_fifo_flush(struct usb_ep *_ep);
  68. #ifdef CONFIG_PPC32
  69. /*
  70. * On some SoCs, the USB controller registers can be big or little endian,
  71. * depending on the version of the chip. In order to be able to run the
  72. * same kernel binary on 2 different versions of an SoC, the BE/LE decision
  73. * must be made at run time. _fsl_readl and fsl_writel are pointers to the
  74. * BE or LE readl() and writel() functions, and fsl_readl() and fsl_writel()
  75. * call through those pointers. Platform code for SoCs that have BE USB
  76. * registers should set pdata->big_endian_mmio flag.
  77. *
  78. * This also applies to controller-to-cpu accessors for the USB descriptors,
  79. * since their endianness is also SoC dependant. Platform code for SoCs that
  80. * have BE USB descriptors should set pdata->big_endian_desc flag.
  81. */
  82. static u32 _fsl_readl_be(const unsigned __iomem *p)
  83. {
  84. return in_be32(p);
  85. }
  86. static u32 _fsl_readl_le(const unsigned __iomem *p)
  87. {
  88. return in_le32(p);
  89. }
  90. static void _fsl_writel_be(u32 v, unsigned __iomem *p)
  91. {
  92. out_be32(p, v);
  93. }
  94. static void _fsl_writel_le(u32 v, unsigned __iomem *p)
  95. {
  96. out_le32(p, v);
  97. }
  98. static u32 (*_fsl_readl)(const unsigned __iomem *p);
  99. static void (*_fsl_writel)(u32 v, unsigned __iomem *p);
  100. #define fsl_readl(p) (*_fsl_readl)((p))
  101. #define fsl_writel(v, p) (*_fsl_writel)((v), (p))
  102. static inline u32 cpu_to_hc32(const u32 x)
  103. {
  104. return udc_controller->pdata->big_endian_desc
  105. ? (__force u32)cpu_to_be32(x)
  106. : (__force u32)cpu_to_le32(x);
  107. }
  108. static inline u32 hc32_to_cpu(const u32 x)
  109. {
  110. return udc_controller->pdata->big_endian_desc
  111. ? be32_to_cpu((__force __be32)x)
  112. : le32_to_cpu((__force __le32)x);
  113. }
  114. #else /* !CONFIG_PPC32 */
  115. #define fsl_readl(addr) readl(addr)
  116. #define fsl_writel(val32, addr) writel(val32, addr)
  117. #define cpu_to_hc32(x) cpu_to_le32(x)
  118. #define hc32_to_cpu(x) le32_to_cpu(x)
  119. #endif /* CONFIG_PPC32 */
  120. /********************************************************************
  121. * Internal Used Function
  122. ********************************************************************/
  123. /*-----------------------------------------------------------------
  124. * done() - retire a request; caller blocked irqs
  125. * @status : request status to be set, only works when
  126. * request is still in progress.
  127. *--------------------------------------------------------------*/
  128. static void done(struct fsl_ep *ep, struct fsl_req *req, int status)
  129. {
  130. struct fsl_udc *udc = NULL;
  131. unsigned char stopped = ep->stopped;
  132. struct ep_td_struct *curr_td, *next_td;
  133. int j;
  134. udc = (struct fsl_udc *)ep->udc;
  135. /* Removed the req from fsl_ep->queue */
  136. list_del_init(&req->queue);
  137. /* req.status should be set as -EINPROGRESS in ep_queue() */
  138. if (req->req.status == -EINPROGRESS)
  139. req->req.status = status;
  140. else
  141. status = req->req.status;
  142. /* Free dtd for the request */
  143. next_td = req->head;
  144. for (j = 0; j < req->dtd_count; j++) {
  145. curr_td = next_td;
  146. if (j != req->dtd_count - 1) {
  147. next_td = curr_td->next_td_virt;
  148. }
  149. dma_pool_free(udc->td_pool, curr_td, curr_td->td_dma);
  150. }
  151. if (req->mapped) {
  152. dma_unmap_single(ep->udc->gadget.dev.parent,
  153. req->req.dma, req->req.length,
  154. ep_is_in(ep)
  155. ? DMA_TO_DEVICE
  156. : DMA_FROM_DEVICE);
  157. req->req.dma = DMA_ADDR_INVALID;
  158. req->mapped = 0;
  159. } else
  160. dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
  161. req->req.dma, req->req.length,
  162. ep_is_in(ep)
  163. ? DMA_TO_DEVICE
  164. : DMA_FROM_DEVICE);
  165. if (status && (status != -ESHUTDOWN))
  166. VDBG("complete %s req %p stat %d len %u/%u",
  167. ep->ep.name, &req->req, status,
  168. req->req.actual, req->req.length);
  169. ep->stopped = 1;
  170. spin_unlock(&ep->udc->lock);
  171. /* complete() is from gadget layer,
  172. * eg fsg->bulk_in_complete() */
  173. if (req->req.complete)
  174. req->req.complete(&ep->ep, &req->req);
  175. spin_lock(&ep->udc->lock);
  176. ep->stopped = stopped;
  177. }
  178. /*-----------------------------------------------------------------
  179. * nuke(): delete all requests related to this ep
  180. * called with spinlock held
  181. *--------------------------------------------------------------*/
  182. static void nuke(struct fsl_ep *ep, int status)
  183. {
  184. ep->stopped = 1;
  185. /* Flush fifo */
  186. fsl_ep_fifo_flush(&ep->ep);
  187. /* Whether this eq has request linked */
  188. while (!list_empty(&ep->queue)) {
  189. struct fsl_req *req = NULL;
  190. req = list_entry(ep->queue.next, struct fsl_req, queue);
  191. done(ep, req, status);
  192. }
  193. }
  194. /*------------------------------------------------------------------
  195. Internal Hardware related function
  196. ------------------------------------------------------------------*/
  197. static int dr_controller_setup(struct fsl_udc *udc)
  198. {
  199. unsigned int tmp, portctrl;
  200. #ifndef CONFIG_ARCH_MXC
  201. unsigned int ctrl;
  202. #endif
  203. unsigned long timeout;
  204. #define FSL_UDC_RESET_TIMEOUT 1000
  205. /* Config PHY interface */
  206. portctrl = fsl_readl(&dr_regs->portsc1);
  207. portctrl &= ~(PORTSCX_PHY_TYPE_SEL | PORTSCX_PORT_WIDTH);
  208. switch (udc->phy_mode) {
  209. case FSL_USB2_PHY_ULPI:
  210. portctrl |= PORTSCX_PTS_ULPI;
  211. break;
  212. case FSL_USB2_PHY_UTMI_WIDE:
  213. portctrl |= PORTSCX_PTW_16BIT;
  214. /* fall through */
  215. case FSL_USB2_PHY_UTMI:
  216. portctrl |= PORTSCX_PTS_UTMI;
  217. break;
  218. case FSL_USB2_PHY_SERIAL:
  219. portctrl |= PORTSCX_PTS_FSLS;
  220. break;
  221. default:
  222. return -EINVAL;
  223. }
  224. fsl_writel(portctrl, &dr_regs->portsc1);
  225. /* Stop and reset the usb controller */
  226. tmp = fsl_readl(&dr_regs->usbcmd);
  227. tmp &= ~USB_CMD_RUN_STOP;
  228. fsl_writel(tmp, &dr_regs->usbcmd);
  229. tmp = fsl_readl(&dr_regs->usbcmd);
  230. tmp |= USB_CMD_CTRL_RESET;
  231. fsl_writel(tmp, &dr_regs->usbcmd);
  232. /* Wait for reset to complete */
  233. timeout = jiffies + FSL_UDC_RESET_TIMEOUT;
  234. while (fsl_readl(&dr_regs->usbcmd) & USB_CMD_CTRL_RESET) {
  235. if (time_after(jiffies, timeout)) {
  236. ERR("udc reset timeout!\n");
  237. return -ETIMEDOUT;
  238. }
  239. cpu_relax();
  240. }
  241. /* Set the controller as device mode */
  242. tmp = fsl_readl(&dr_regs->usbmode);
  243. tmp &= ~USB_MODE_CTRL_MODE_MASK; /* clear mode bits */
  244. tmp |= USB_MODE_CTRL_MODE_DEVICE;
  245. /* Disable Setup Lockout */
  246. tmp |= USB_MODE_SETUP_LOCK_OFF;
  247. if (udc->pdata->es)
  248. tmp |= USB_MODE_ES;
  249. fsl_writel(tmp, &dr_regs->usbmode);
  250. /* Clear the setup status */
  251. fsl_writel(0, &dr_regs->usbsts);
  252. tmp = udc->ep_qh_dma;
  253. tmp &= USB_EP_LIST_ADDRESS_MASK;
  254. fsl_writel(tmp, &dr_regs->endpointlistaddr);
  255. VDBG("vir[qh_base] is %p phy[qh_base] is 0x%8x reg is 0x%8x",
  256. udc->ep_qh, (int)tmp,
  257. fsl_readl(&dr_regs->endpointlistaddr));
  258. /* Config control enable i/o output, cpu endian register */
  259. #ifndef CONFIG_ARCH_MXC
  260. if (udc->pdata->have_sysif_regs) {
  261. ctrl = __raw_readl(&usb_sys_regs->control);
  262. ctrl |= USB_CTRL_IOENB;
  263. __raw_writel(ctrl, &usb_sys_regs->control);
  264. }
  265. #endif
  266. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  267. /* Turn on cache snooping hardware, since some PowerPC platforms
  268. * wholly rely on hardware to deal with cache coherent. */
  269. if (udc->pdata->have_sysif_regs) {
  270. /* Setup Snooping for all the 4GB space */
  271. tmp = SNOOP_SIZE_2GB; /* starts from 0x0, size 2G */
  272. __raw_writel(tmp, &usb_sys_regs->snoop1);
  273. tmp |= 0x80000000; /* starts from 0x8000000, size 2G */
  274. __raw_writel(tmp, &usb_sys_regs->snoop2);
  275. }
  276. #endif
  277. return 0;
  278. }
  279. /* Enable DR irq and set controller to run state */
  280. static void dr_controller_run(struct fsl_udc *udc)
  281. {
  282. u32 temp;
  283. /* Enable DR irq reg */
  284. temp = USB_INTR_INT_EN | USB_INTR_ERR_INT_EN
  285. | USB_INTR_PTC_DETECT_EN | USB_INTR_RESET_EN
  286. | USB_INTR_DEVICE_SUSPEND | USB_INTR_SYS_ERR_EN;
  287. fsl_writel(temp, &dr_regs->usbintr);
  288. /* Clear stopped bit */
  289. udc->stopped = 0;
  290. /* Set the controller as device mode */
  291. temp = fsl_readl(&dr_regs->usbmode);
  292. temp |= USB_MODE_CTRL_MODE_DEVICE;
  293. fsl_writel(temp, &dr_regs->usbmode);
  294. /* Set controller to Run */
  295. temp = fsl_readl(&dr_regs->usbcmd);
  296. temp |= USB_CMD_RUN_STOP;
  297. fsl_writel(temp, &dr_regs->usbcmd);
  298. }
  299. static void dr_controller_stop(struct fsl_udc *udc)
  300. {
  301. unsigned int tmp;
  302. pr_debug("%s\n", __func__);
  303. /* if we're in OTG mode, and the Host is currently using the port,
  304. * stop now and don't rip the controller out from under the
  305. * ehci driver
  306. */
  307. if (udc->gadget.is_otg) {
  308. if (!(fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID)) {
  309. pr_debug("udc: Leaving early\n");
  310. return;
  311. }
  312. }
  313. /* disable all INTR */
  314. fsl_writel(0, &dr_regs->usbintr);
  315. /* Set stopped bit for isr */
  316. udc->stopped = 1;
  317. /* disable IO output */
  318. /* usb_sys_regs->control = 0; */
  319. /* set controller to Stop */
  320. tmp = fsl_readl(&dr_regs->usbcmd);
  321. tmp &= ~USB_CMD_RUN_STOP;
  322. fsl_writel(tmp, &dr_regs->usbcmd);
  323. }
  324. static void dr_ep_setup(unsigned char ep_num, unsigned char dir,
  325. unsigned char ep_type)
  326. {
  327. unsigned int tmp_epctrl = 0;
  328. tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  329. if (dir) {
  330. if (ep_num)
  331. tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
  332. tmp_epctrl |= EPCTRL_TX_ENABLE;
  333. tmp_epctrl |= ((unsigned int)(ep_type)
  334. << EPCTRL_TX_EP_TYPE_SHIFT);
  335. } else {
  336. if (ep_num)
  337. tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
  338. tmp_epctrl |= EPCTRL_RX_ENABLE;
  339. tmp_epctrl |= ((unsigned int)(ep_type)
  340. << EPCTRL_RX_EP_TYPE_SHIFT);
  341. }
  342. fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
  343. }
  344. static void
  345. dr_ep_change_stall(unsigned char ep_num, unsigned char dir, int value)
  346. {
  347. u32 tmp_epctrl = 0;
  348. tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  349. if (value) {
  350. /* set the stall bit */
  351. if (dir)
  352. tmp_epctrl |= EPCTRL_TX_EP_STALL;
  353. else
  354. tmp_epctrl |= EPCTRL_RX_EP_STALL;
  355. } else {
  356. /* clear the stall bit and reset data toggle */
  357. if (dir) {
  358. tmp_epctrl &= ~EPCTRL_TX_EP_STALL;
  359. tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
  360. } else {
  361. tmp_epctrl &= ~EPCTRL_RX_EP_STALL;
  362. tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
  363. }
  364. }
  365. fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
  366. }
  367. /* Get stall status of a specific ep
  368. Return: 0: not stalled; 1:stalled */
  369. static int dr_ep_get_stall(unsigned char ep_num, unsigned char dir)
  370. {
  371. u32 epctrl;
  372. epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  373. if (dir)
  374. return (epctrl & EPCTRL_TX_EP_STALL) ? 1 : 0;
  375. else
  376. return (epctrl & EPCTRL_RX_EP_STALL) ? 1 : 0;
  377. }
  378. /********************************************************************
  379. Internal Structure Build up functions
  380. ********************************************************************/
  381. /*------------------------------------------------------------------
  382. * struct_ep_qh_setup(): set the Endpoint Capabilites field of QH
  383. * @zlt: Zero Length Termination Select (1: disable; 0: enable)
  384. * @mult: Mult field
  385. ------------------------------------------------------------------*/
  386. static void struct_ep_qh_setup(struct fsl_udc *udc, unsigned char ep_num,
  387. unsigned char dir, unsigned char ep_type,
  388. unsigned int max_pkt_len,
  389. unsigned int zlt, unsigned char mult)
  390. {
  391. struct ep_queue_head *p_QH = &udc->ep_qh[2 * ep_num + dir];
  392. unsigned int tmp = 0;
  393. /* set the Endpoint Capabilites in QH */
  394. switch (ep_type) {
  395. case USB_ENDPOINT_XFER_CONTROL:
  396. /* Interrupt On Setup (IOS). for control ep */
  397. tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
  398. | EP_QUEUE_HEAD_IOS;
  399. break;
  400. case USB_ENDPOINT_XFER_ISOC:
  401. tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
  402. | (mult << EP_QUEUE_HEAD_MULT_POS);
  403. break;
  404. case USB_ENDPOINT_XFER_BULK:
  405. case USB_ENDPOINT_XFER_INT:
  406. tmp = max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS;
  407. break;
  408. default:
  409. VDBG("error ep type is %d", ep_type);
  410. return;
  411. }
  412. if (zlt)
  413. tmp |= EP_QUEUE_HEAD_ZLT_SEL;
  414. p_QH->max_pkt_length = cpu_to_hc32(tmp);
  415. p_QH->next_dtd_ptr = 1;
  416. p_QH->size_ioc_int_sts = 0;
  417. }
  418. /* Setup qh structure and ep register for ep0. */
  419. static void ep0_setup(struct fsl_udc *udc)
  420. {
  421. /* the intialization of an ep includes: fields in QH, Regs,
  422. * fsl_ep struct */
  423. struct_ep_qh_setup(udc, 0, USB_RECV, USB_ENDPOINT_XFER_CONTROL,
  424. USB_MAX_CTRL_PAYLOAD, 0, 0);
  425. struct_ep_qh_setup(udc, 0, USB_SEND, USB_ENDPOINT_XFER_CONTROL,
  426. USB_MAX_CTRL_PAYLOAD, 0, 0);
  427. dr_ep_setup(0, USB_RECV, USB_ENDPOINT_XFER_CONTROL);
  428. dr_ep_setup(0, USB_SEND, USB_ENDPOINT_XFER_CONTROL);
  429. return;
  430. }
  431. /***********************************************************************
  432. Endpoint Management Functions
  433. ***********************************************************************/
  434. /*-------------------------------------------------------------------------
  435. * when configurations are set, or when interface settings change
  436. * for example the do_set_interface() in gadget layer,
  437. * the driver will enable or disable the relevant endpoints
  438. * ep0 doesn't use this routine. It is always enabled.
  439. -------------------------------------------------------------------------*/
  440. static int fsl_ep_enable(struct usb_ep *_ep,
  441. const struct usb_endpoint_descriptor *desc)
  442. {
  443. struct fsl_udc *udc = NULL;
  444. struct fsl_ep *ep = NULL;
  445. unsigned short max = 0;
  446. unsigned char mult = 0, zlt;
  447. int retval = -EINVAL;
  448. unsigned long flags = 0;
  449. ep = container_of(_ep, struct fsl_ep, ep);
  450. /* catch various bogus parameters */
  451. if (!_ep || !desc || ep->desc
  452. || (desc->bDescriptorType != USB_DT_ENDPOINT))
  453. return -EINVAL;
  454. udc = ep->udc;
  455. if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN))
  456. return -ESHUTDOWN;
  457. max = le16_to_cpu(desc->wMaxPacketSize);
  458. /* Disable automatic zlp generation. Driver is responsible to indicate
  459. * explicitly through req->req.zero. This is needed to enable multi-td
  460. * request. */
  461. zlt = 1;
  462. /* Assume the max packet size from gadget is always correct */
  463. switch (desc->bmAttributes & 0x03) {
  464. case USB_ENDPOINT_XFER_CONTROL:
  465. case USB_ENDPOINT_XFER_BULK:
  466. case USB_ENDPOINT_XFER_INT:
  467. /* mult = 0. Execute N Transactions as demonstrated by
  468. * the USB variable length packet protocol where N is
  469. * computed using the Maximum Packet Length (dQH) and
  470. * the Total Bytes field (dTD) */
  471. mult = 0;
  472. break;
  473. case USB_ENDPOINT_XFER_ISOC:
  474. /* Calculate transactions needed for high bandwidth iso */
  475. mult = (unsigned char)(1 + ((max >> 11) & 0x03));
  476. max = max & 0x7ff; /* bit 0~10 */
  477. /* 3 transactions at most */
  478. if (mult > 3)
  479. goto en_done;
  480. break;
  481. default:
  482. goto en_done;
  483. }
  484. spin_lock_irqsave(&udc->lock, flags);
  485. ep->ep.maxpacket = max;
  486. ep->desc = desc;
  487. ep->stopped = 0;
  488. /* Controller related setup */
  489. /* Init EPx Queue Head (Ep Capabilites field in QH
  490. * according to max, zlt, mult) */
  491. struct_ep_qh_setup(udc, (unsigned char) ep_index(ep),
  492. (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
  493. ? USB_SEND : USB_RECV),
  494. (unsigned char) (desc->bmAttributes
  495. & USB_ENDPOINT_XFERTYPE_MASK),
  496. max, zlt, mult);
  497. /* Init endpoint ctrl register */
  498. dr_ep_setup((unsigned char) ep_index(ep),
  499. (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
  500. ? USB_SEND : USB_RECV),
  501. (unsigned char) (desc->bmAttributes
  502. & USB_ENDPOINT_XFERTYPE_MASK));
  503. spin_unlock_irqrestore(&udc->lock, flags);
  504. retval = 0;
  505. VDBG("enabled %s (ep%d%s) maxpacket %d",ep->ep.name,
  506. ep->desc->bEndpointAddress & 0x0f,
  507. (desc->bEndpointAddress & USB_DIR_IN)
  508. ? "in" : "out", max);
  509. en_done:
  510. return retval;
  511. }
  512. /*---------------------------------------------------------------------
  513. * @ep : the ep being unconfigured. May not be ep0
  514. * Any pending and uncomplete req will complete with status (-ESHUTDOWN)
  515. *---------------------------------------------------------------------*/
  516. static int fsl_ep_disable(struct usb_ep *_ep)
  517. {
  518. struct fsl_udc *udc = NULL;
  519. struct fsl_ep *ep = NULL;
  520. unsigned long flags = 0;
  521. u32 epctrl;
  522. int ep_num;
  523. ep = container_of(_ep, struct fsl_ep, ep);
  524. if (!_ep || !ep->desc) {
  525. VDBG("%s not enabled", _ep ? ep->ep.name : NULL);
  526. return -EINVAL;
  527. }
  528. /* disable ep on controller */
  529. ep_num = ep_index(ep);
  530. epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  531. if (ep_is_in(ep))
  532. epctrl &= ~EPCTRL_TX_ENABLE;
  533. else
  534. epctrl &= ~EPCTRL_RX_ENABLE;
  535. fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
  536. udc = (struct fsl_udc *)ep->udc;
  537. spin_lock_irqsave(&udc->lock, flags);
  538. /* nuke all pending requests (does flush) */
  539. nuke(ep, -ESHUTDOWN);
  540. ep->desc = NULL;
  541. ep->stopped = 1;
  542. spin_unlock_irqrestore(&udc->lock, flags);
  543. VDBG("disabled %s OK", _ep->name);
  544. return 0;
  545. }
  546. /*---------------------------------------------------------------------
  547. * allocate a request object used by this endpoint
  548. * the main operation is to insert the req->queue to the eq->queue
  549. * Returns the request, or null if one could not be allocated
  550. *---------------------------------------------------------------------*/
  551. static struct usb_request *
  552. fsl_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  553. {
  554. struct fsl_req *req = NULL;
  555. req = kzalloc(sizeof *req, gfp_flags);
  556. if (!req)
  557. return NULL;
  558. req->req.dma = DMA_ADDR_INVALID;
  559. INIT_LIST_HEAD(&req->queue);
  560. return &req->req;
  561. }
  562. static void fsl_free_request(struct usb_ep *_ep, struct usb_request *_req)
  563. {
  564. struct fsl_req *req = NULL;
  565. req = container_of(_req, struct fsl_req, req);
  566. if (_req)
  567. kfree(req);
  568. }
  569. /*-------------------------------------------------------------------------*/
  570. static void fsl_queue_td(struct fsl_ep *ep, struct fsl_req *req)
  571. {
  572. int i = ep_index(ep) * 2 + ep_is_in(ep);
  573. u32 temp, bitmask, tmp_stat;
  574. struct ep_queue_head *dQH = &ep->udc->ep_qh[i];
  575. /* VDBG("QH addr Register 0x%8x", dr_regs->endpointlistaddr);
  576. VDBG("ep_qh[%d] addr is 0x%8x", i, (u32)&(ep->udc->ep_qh[i])); */
  577. bitmask = ep_is_in(ep)
  578. ? (1 << (ep_index(ep) + 16))
  579. : (1 << (ep_index(ep)));
  580. /* check if the pipe is empty */
  581. if (!(list_empty(&ep->queue))) {
  582. /* Add td to the end */
  583. struct fsl_req *lastreq;
  584. lastreq = list_entry(ep->queue.prev, struct fsl_req, queue);
  585. lastreq->tail->next_td_ptr =
  586. cpu_to_hc32(req->head->td_dma & DTD_ADDR_MASK);
  587. /* Read prime bit, if 1 goto done */
  588. if (fsl_readl(&dr_regs->endpointprime) & bitmask)
  589. goto out;
  590. do {
  591. /* Set ATDTW bit in USBCMD */
  592. temp = fsl_readl(&dr_regs->usbcmd);
  593. fsl_writel(temp | USB_CMD_ATDTW, &dr_regs->usbcmd);
  594. /* Read correct status bit */
  595. tmp_stat = fsl_readl(&dr_regs->endptstatus) & bitmask;
  596. } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_ATDTW));
  597. /* Write ATDTW bit to 0 */
  598. temp = fsl_readl(&dr_regs->usbcmd);
  599. fsl_writel(temp & ~USB_CMD_ATDTW, &dr_regs->usbcmd);
  600. if (tmp_stat)
  601. goto out;
  602. }
  603. /* Write dQH next pointer and terminate bit to 0 */
  604. temp = req->head->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
  605. dQH->next_dtd_ptr = cpu_to_hc32(temp);
  606. /* Clear active and halt bit */
  607. temp = cpu_to_hc32(~(EP_QUEUE_HEAD_STATUS_ACTIVE
  608. | EP_QUEUE_HEAD_STATUS_HALT));
  609. dQH->size_ioc_int_sts &= temp;
  610. /* Ensure that updates to the QH will occur before priming. */
  611. wmb();
  612. /* Prime endpoint by writing 1 to ENDPTPRIME */
  613. temp = ep_is_in(ep)
  614. ? (1 << (ep_index(ep) + 16))
  615. : (1 << (ep_index(ep)));
  616. fsl_writel(temp, &dr_regs->endpointprime);
  617. out:
  618. return;
  619. }
  620. /* Fill in the dTD structure
  621. * @req: request that the transfer belongs to
  622. * @length: return actually data length of the dTD
  623. * @dma: return dma address of the dTD
  624. * @is_last: return flag if it is the last dTD of the request
  625. * return: pointer to the built dTD */
  626. static struct ep_td_struct *fsl_build_dtd(struct fsl_req *req, unsigned *length,
  627. dma_addr_t *dma, int *is_last)
  628. {
  629. u32 swap_temp;
  630. struct ep_td_struct *dtd;
  631. /* how big will this transfer be? */
  632. *length = min(req->req.length - req->req.actual,
  633. (unsigned)EP_MAX_LENGTH_TRANSFER);
  634. dtd = dma_pool_alloc(udc_controller->td_pool, GFP_KERNEL, dma);
  635. if (dtd == NULL)
  636. return dtd;
  637. dtd->td_dma = *dma;
  638. /* Clear reserved field */
  639. swap_temp = hc32_to_cpu(dtd->size_ioc_sts);
  640. swap_temp &= ~DTD_RESERVED_FIELDS;
  641. dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
  642. /* Init all of buffer page pointers */
  643. swap_temp = (u32) (req->req.dma + req->req.actual);
  644. dtd->buff_ptr0 = cpu_to_hc32(swap_temp);
  645. dtd->buff_ptr1 = cpu_to_hc32(swap_temp + 0x1000);
  646. dtd->buff_ptr2 = cpu_to_hc32(swap_temp + 0x2000);
  647. dtd->buff_ptr3 = cpu_to_hc32(swap_temp + 0x3000);
  648. dtd->buff_ptr4 = cpu_to_hc32(swap_temp + 0x4000);
  649. req->req.actual += *length;
  650. /* zlp is needed if req->req.zero is set */
  651. if (req->req.zero) {
  652. if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
  653. *is_last = 1;
  654. else
  655. *is_last = 0;
  656. } else if (req->req.length == req->req.actual)
  657. *is_last = 1;
  658. else
  659. *is_last = 0;
  660. if ((*is_last) == 0)
  661. VDBG("multi-dtd request!");
  662. /* Fill in the transfer size; set active bit */
  663. swap_temp = ((*length << DTD_LENGTH_BIT_POS) | DTD_STATUS_ACTIVE);
  664. /* Enable interrupt for the last dtd of a request */
  665. if (*is_last && !req->req.no_interrupt)
  666. swap_temp |= DTD_IOC;
  667. dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
  668. mb();
  669. VDBG("length = %d address= 0x%x", *length, (int)*dma);
  670. return dtd;
  671. }
  672. /* Generate dtd chain for a request */
  673. static int fsl_req_to_dtd(struct fsl_req *req)
  674. {
  675. unsigned count;
  676. int is_last;
  677. int is_first =1;
  678. struct ep_td_struct *last_dtd = NULL, *dtd;
  679. dma_addr_t dma;
  680. do {
  681. dtd = fsl_build_dtd(req, &count, &dma, &is_last);
  682. if (dtd == NULL)
  683. return -ENOMEM;
  684. if (is_first) {
  685. is_first = 0;
  686. req->head = dtd;
  687. } else {
  688. last_dtd->next_td_ptr = cpu_to_hc32(dma);
  689. last_dtd->next_td_virt = dtd;
  690. }
  691. last_dtd = dtd;
  692. req->dtd_count++;
  693. } while (!is_last);
  694. dtd->next_td_ptr = cpu_to_hc32(DTD_NEXT_TERMINATE);
  695. req->tail = dtd;
  696. return 0;
  697. }
  698. /* queues (submits) an I/O request to an endpoint */
  699. static int
  700. fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  701. {
  702. struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
  703. struct fsl_req *req = container_of(_req, struct fsl_req, req);
  704. struct fsl_udc *udc;
  705. unsigned long flags;
  706. /* catch various bogus parameters */
  707. if (!_req || !req->req.complete || !req->req.buf
  708. || !list_empty(&req->queue)) {
  709. VDBG("%s, bad params", __func__);
  710. return -EINVAL;
  711. }
  712. if (unlikely(!_ep || !ep->desc)) {
  713. VDBG("%s, bad ep", __func__);
  714. return -EINVAL;
  715. }
  716. if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  717. if (req->req.length > ep->ep.maxpacket)
  718. return -EMSGSIZE;
  719. }
  720. udc = ep->udc;
  721. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
  722. return -ESHUTDOWN;
  723. req->ep = ep;
  724. /* map virtual address to hardware */
  725. if (req->req.dma == DMA_ADDR_INVALID) {
  726. req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
  727. req->req.buf,
  728. req->req.length, ep_is_in(ep)
  729. ? DMA_TO_DEVICE
  730. : DMA_FROM_DEVICE);
  731. req->mapped = 1;
  732. } else {
  733. dma_sync_single_for_device(ep->udc->gadget.dev.parent,
  734. req->req.dma, req->req.length,
  735. ep_is_in(ep)
  736. ? DMA_TO_DEVICE
  737. : DMA_FROM_DEVICE);
  738. req->mapped = 0;
  739. }
  740. req->req.status = -EINPROGRESS;
  741. req->req.actual = 0;
  742. req->dtd_count = 0;
  743. spin_lock_irqsave(&udc->lock, flags);
  744. /* build dtds and push them to device queue */
  745. if (!fsl_req_to_dtd(req)) {
  746. fsl_queue_td(ep, req);
  747. } else {
  748. spin_unlock_irqrestore(&udc->lock, flags);
  749. return -ENOMEM;
  750. }
  751. /* Update ep0 state */
  752. if ((ep_index(ep) == 0))
  753. udc->ep0_state = DATA_STATE_XMIT;
  754. /* irq handler advances the queue */
  755. if (req != NULL)
  756. list_add_tail(&req->queue, &ep->queue);
  757. spin_unlock_irqrestore(&udc->lock, flags);
  758. return 0;
  759. }
  760. /* dequeues (cancels, unlinks) an I/O request from an endpoint */
  761. static int fsl_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  762. {
  763. struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
  764. struct fsl_req *req;
  765. unsigned long flags;
  766. int ep_num, stopped, ret = 0;
  767. u32 epctrl;
  768. if (!_ep || !_req)
  769. return -EINVAL;
  770. spin_lock_irqsave(&ep->udc->lock, flags);
  771. stopped = ep->stopped;
  772. /* Stop the ep before we deal with the queue */
  773. ep->stopped = 1;
  774. ep_num = ep_index(ep);
  775. epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  776. if (ep_is_in(ep))
  777. epctrl &= ~EPCTRL_TX_ENABLE;
  778. else
  779. epctrl &= ~EPCTRL_RX_ENABLE;
  780. fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
  781. /* make sure it's actually queued on this endpoint */
  782. list_for_each_entry(req, &ep->queue, queue) {
  783. if (&req->req == _req)
  784. break;
  785. }
  786. if (&req->req != _req) {
  787. ret = -EINVAL;
  788. goto out;
  789. }
  790. /* The request is in progress, or completed but not dequeued */
  791. if (ep->queue.next == &req->queue) {
  792. _req->status = -ECONNRESET;
  793. fsl_ep_fifo_flush(_ep); /* flush current transfer */
  794. /* The request isn't the last request in this ep queue */
  795. if (req->queue.next != &ep->queue) {
  796. struct ep_queue_head *qh;
  797. struct fsl_req *next_req;
  798. qh = ep->qh;
  799. next_req = list_entry(req->queue.next, struct fsl_req,
  800. queue);
  801. /* Point the QH to the first TD of next request */
  802. fsl_writel((u32) next_req->head, &qh->curr_dtd_ptr);
  803. }
  804. /* The request hasn't been processed, patch up the TD chain */
  805. } else {
  806. struct fsl_req *prev_req;
  807. prev_req = list_entry(req->queue.prev, struct fsl_req, queue);
  808. fsl_writel(fsl_readl(&req->tail->next_td_ptr),
  809. &prev_req->tail->next_td_ptr);
  810. }
  811. done(ep, req, -ECONNRESET);
  812. /* Enable EP */
  813. out: epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  814. if (ep_is_in(ep))
  815. epctrl |= EPCTRL_TX_ENABLE;
  816. else
  817. epctrl |= EPCTRL_RX_ENABLE;
  818. fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
  819. ep->stopped = stopped;
  820. spin_unlock_irqrestore(&ep->udc->lock, flags);
  821. return ret;
  822. }
  823. /*-------------------------------------------------------------------------*/
  824. /*-----------------------------------------------------------------
  825. * modify the endpoint halt feature
  826. * @ep: the non-isochronous endpoint being stalled
  827. * @value: 1--set halt 0--clear halt
  828. * Returns zero, or a negative error code.
  829. *----------------------------------------------------------------*/
  830. static int fsl_ep_set_halt(struct usb_ep *_ep, int value)
  831. {
  832. struct fsl_ep *ep = NULL;
  833. unsigned long flags = 0;
  834. int status = -EOPNOTSUPP; /* operation not supported */
  835. unsigned char ep_dir = 0, ep_num = 0;
  836. struct fsl_udc *udc = NULL;
  837. ep = container_of(_ep, struct fsl_ep, ep);
  838. udc = ep->udc;
  839. if (!_ep || !ep->desc) {
  840. status = -EINVAL;
  841. goto out;
  842. }
  843. if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  844. status = -EOPNOTSUPP;
  845. goto out;
  846. }
  847. /* Attempt to halt IN ep will fail if any transfer requests
  848. * are still queue */
  849. if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
  850. status = -EAGAIN;
  851. goto out;
  852. }
  853. status = 0;
  854. ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
  855. ep_num = (unsigned char)(ep_index(ep));
  856. spin_lock_irqsave(&ep->udc->lock, flags);
  857. dr_ep_change_stall(ep_num, ep_dir, value);
  858. spin_unlock_irqrestore(&ep->udc->lock, flags);
  859. if (ep_index(ep) == 0) {
  860. udc->ep0_state = WAIT_FOR_SETUP;
  861. udc->ep0_dir = 0;
  862. }
  863. out:
  864. VDBG(" %s %s halt stat %d", ep->ep.name,
  865. value ? "set" : "clear", status);
  866. return status;
  867. }
  868. static int fsl_ep_fifo_status(struct usb_ep *_ep)
  869. {
  870. struct fsl_ep *ep;
  871. struct fsl_udc *udc;
  872. int size = 0;
  873. u32 bitmask;
  874. struct ep_queue_head *d_qh;
  875. ep = container_of(_ep, struct fsl_ep, ep);
  876. if (!_ep || (!ep->desc && ep_index(ep) != 0))
  877. return -ENODEV;
  878. udc = (struct fsl_udc *)ep->udc;
  879. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
  880. return -ESHUTDOWN;
  881. d_qh = &ep->udc->ep_qh[ep_index(ep) * 2 + ep_is_in(ep)];
  882. bitmask = (ep_is_in(ep)) ? (1 << (ep_index(ep) + 16)) :
  883. (1 << (ep_index(ep)));
  884. if (fsl_readl(&dr_regs->endptstatus) & bitmask)
  885. size = (d_qh->size_ioc_int_sts & DTD_PACKET_SIZE)
  886. >> DTD_LENGTH_BIT_POS;
  887. pr_debug("%s %u\n", __func__, size);
  888. return size;
  889. }
  890. static void fsl_ep_fifo_flush(struct usb_ep *_ep)
  891. {
  892. struct fsl_ep *ep;
  893. int ep_num, ep_dir;
  894. u32 bits;
  895. unsigned long timeout;
  896. #define FSL_UDC_FLUSH_TIMEOUT 1000
  897. if (!_ep) {
  898. return;
  899. } else {
  900. ep = container_of(_ep, struct fsl_ep, ep);
  901. if (!ep->desc)
  902. return;
  903. }
  904. ep_num = ep_index(ep);
  905. ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
  906. if (ep_num == 0)
  907. bits = (1 << 16) | 1;
  908. else if (ep_dir == USB_SEND)
  909. bits = 1 << (16 + ep_num);
  910. else
  911. bits = 1 << ep_num;
  912. timeout = jiffies + FSL_UDC_FLUSH_TIMEOUT;
  913. do {
  914. fsl_writel(bits, &dr_regs->endptflush);
  915. /* Wait until flush complete */
  916. while (fsl_readl(&dr_regs->endptflush)) {
  917. if (time_after(jiffies, timeout)) {
  918. ERR("ep flush timeout\n");
  919. return;
  920. }
  921. cpu_relax();
  922. }
  923. /* See if we need to flush again */
  924. } while (fsl_readl(&dr_regs->endptstatus) & bits);
  925. }
  926. static struct usb_ep_ops fsl_ep_ops = {
  927. .enable = fsl_ep_enable,
  928. .disable = fsl_ep_disable,
  929. .alloc_request = fsl_alloc_request,
  930. .free_request = fsl_free_request,
  931. .queue = fsl_ep_queue,
  932. .dequeue = fsl_ep_dequeue,
  933. .set_halt = fsl_ep_set_halt,
  934. .fifo_status = fsl_ep_fifo_status,
  935. .fifo_flush = fsl_ep_fifo_flush, /* flush fifo */
  936. };
  937. /*-------------------------------------------------------------------------
  938. Gadget Driver Layer Operations
  939. -------------------------------------------------------------------------*/
  940. /*----------------------------------------------------------------------
  941. * Get the current frame number (from DR frame_index Reg )
  942. *----------------------------------------------------------------------*/
  943. static int fsl_get_frame(struct usb_gadget *gadget)
  944. {
  945. return (int)(fsl_readl(&dr_regs->frindex) & USB_FRINDEX_MASKS);
  946. }
  947. /*-----------------------------------------------------------------------
  948. * Tries to wake up the host connected to this gadget
  949. -----------------------------------------------------------------------*/
  950. static int fsl_wakeup(struct usb_gadget *gadget)
  951. {
  952. struct fsl_udc *udc = container_of(gadget, struct fsl_udc, gadget);
  953. u32 portsc;
  954. /* Remote wakeup feature not enabled by host */
  955. if (!udc->remote_wakeup)
  956. return -ENOTSUPP;
  957. portsc = fsl_readl(&dr_regs->portsc1);
  958. /* not suspended? */
  959. if (!(portsc & PORTSCX_PORT_SUSPEND))
  960. return 0;
  961. /* trigger force resume */
  962. portsc |= PORTSCX_PORT_FORCE_RESUME;
  963. fsl_writel(portsc, &dr_regs->portsc1);
  964. return 0;
  965. }
  966. static int can_pullup(struct fsl_udc *udc)
  967. {
  968. return udc->driver && udc->softconnect && udc->vbus_active;
  969. }
  970. /* Notify controller that VBUS is powered, Called by whatever
  971. detects VBUS sessions */
  972. static int fsl_vbus_session(struct usb_gadget *gadget, int is_active)
  973. {
  974. struct fsl_udc *udc;
  975. unsigned long flags;
  976. udc = container_of(gadget, struct fsl_udc, gadget);
  977. spin_lock_irqsave(&udc->lock, flags);
  978. VDBG("VBUS %s", is_active ? "on" : "off");
  979. udc->vbus_active = (is_active != 0);
  980. if (can_pullup(udc))
  981. fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
  982. &dr_regs->usbcmd);
  983. else
  984. fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
  985. &dr_regs->usbcmd);
  986. spin_unlock_irqrestore(&udc->lock, flags);
  987. return 0;
  988. }
  989. /* constrain controller's VBUS power usage
  990. * This call is used by gadget drivers during SET_CONFIGURATION calls,
  991. * reporting how much power the device may consume. For example, this
  992. * could affect how quickly batteries are recharged.
  993. *
  994. * Returns zero on success, else negative errno.
  995. */
  996. static int fsl_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  997. {
  998. struct fsl_udc *udc;
  999. udc = container_of(gadget, struct fsl_udc, gadget);
  1000. if (udc->transceiver)
  1001. return otg_set_power(udc->transceiver, mA);
  1002. return -ENOTSUPP;
  1003. }
  1004. /* Change Data+ pullup status
  1005. * this func is used by usb_gadget_connect/disconnet
  1006. */
  1007. static int fsl_pullup(struct usb_gadget *gadget, int is_on)
  1008. {
  1009. struct fsl_udc *udc;
  1010. udc = container_of(gadget, struct fsl_udc, gadget);
  1011. udc->softconnect = (is_on != 0);
  1012. if (can_pullup(udc))
  1013. fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
  1014. &dr_regs->usbcmd);
  1015. else
  1016. fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
  1017. &dr_regs->usbcmd);
  1018. return 0;
  1019. }
  1020. /* defined in gadget.h */
  1021. static struct usb_gadget_ops fsl_gadget_ops = {
  1022. .get_frame = fsl_get_frame,
  1023. .wakeup = fsl_wakeup,
  1024. /* .set_selfpowered = fsl_set_selfpowered, */ /* Always selfpowered */
  1025. .vbus_session = fsl_vbus_session,
  1026. .vbus_draw = fsl_vbus_draw,
  1027. .pullup = fsl_pullup,
  1028. };
  1029. /* Set protocol stall on ep0, protocol stall will automatically be cleared
  1030. on new transaction */
  1031. static void ep0stall(struct fsl_udc *udc)
  1032. {
  1033. u32 tmp;
  1034. /* must set tx and rx to stall at the same time */
  1035. tmp = fsl_readl(&dr_regs->endptctrl[0]);
  1036. tmp |= EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL;
  1037. fsl_writel(tmp, &dr_regs->endptctrl[0]);
  1038. udc->ep0_state = WAIT_FOR_SETUP;
  1039. udc->ep0_dir = 0;
  1040. }
  1041. /* Prime a status phase for ep0 */
  1042. static int ep0_prime_status(struct fsl_udc *udc, int direction)
  1043. {
  1044. struct fsl_req *req = udc->status_req;
  1045. struct fsl_ep *ep;
  1046. if (direction == EP_DIR_IN)
  1047. udc->ep0_dir = USB_DIR_IN;
  1048. else
  1049. udc->ep0_dir = USB_DIR_OUT;
  1050. ep = &udc->eps[0];
  1051. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1052. req->ep = ep;
  1053. req->req.length = 0;
  1054. req->req.status = -EINPROGRESS;
  1055. req->req.actual = 0;
  1056. req->req.complete = NULL;
  1057. req->dtd_count = 0;
  1058. if (fsl_req_to_dtd(req) == 0)
  1059. fsl_queue_td(ep, req);
  1060. else
  1061. return -ENOMEM;
  1062. list_add_tail(&req->queue, &ep->queue);
  1063. return 0;
  1064. }
  1065. static void udc_reset_ep_queue(struct fsl_udc *udc, u8 pipe)
  1066. {
  1067. struct fsl_ep *ep = get_ep_by_pipe(udc, pipe);
  1068. if (ep->name)
  1069. nuke(ep, -ESHUTDOWN);
  1070. }
  1071. /*
  1072. * ch9 Set address
  1073. */
  1074. static void ch9setaddress(struct fsl_udc *udc, u16 value, u16 index, u16 length)
  1075. {
  1076. /* Save the new address to device struct */
  1077. udc->device_address = (u8) value;
  1078. /* Update usb state */
  1079. udc->usb_state = USB_STATE_ADDRESS;
  1080. /* Status phase */
  1081. if (ep0_prime_status(udc, EP_DIR_IN))
  1082. ep0stall(udc);
  1083. }
  1084. /*
  1085. * ch9 Get status
  1086. */
  1087. static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
  1088. u16 index, u16 length)
  1089. {
  1090. u16 tmp = 0; /* Status, cpu endian */
  1091. struct fsl_req *req;
  1092. struct fsl_ep *ep;
  1093. ep = &udc->eps[0];
  1094. if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
  1095. /* Get device status */
  1096. tmp = 1 << USB_DEVICE_SELF_POWERED;
  1097. tmp |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
  1098. } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
  1099. /* Get interface status */
  1100. /* We don't have interface information in udc driver */
  1101. tmp = 0;
  1102. } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
  1103. /* Get endpoint status */
  1104. struct fsl_ep *target_ep;
  1105. target_ep = get_ep_by_pipe(udc, get_pipe_by_windex(index));
  1106. /* stall if endpoint doesn't exist */
  1107. if (!target_ep->desc)
  1108. goto stall;
  1109. tmp = dr_ep_get_stall(ep_index(target_ep), ep_is_in(target_ep))
  1110. << USB_ENDPOINT_HALT;
  1111. }
  1112. udc->ep0_dir = USB_DIR_IN;
  1113. /* Borrow the per device status_req */
  1114. req = udc->status_req;
  1115. /* Fill in the reqest structure */
  1116. *((u16 *) req->req.buf) = cpu_to_le16(tmp);
  1117. /* flush cache for the req buffer */
  1118. flush_dcache_range((u32)req->req.buf, (u32)req->req.buf + 8);
  1119. req->ep = ep;
  1120. req->req.length = 2;
  1121. req->req.status = -EINPROGRESS;
  1122. req->req.actual = 0;
  1123. req->req.complete = NULL;
  1124. req->dtd_count = 0;
  1125. /* prime the data phase */
  1126. if ((fsl_req_to_dtd(req) == 0))
  1127. fsl_queue_td(ep, req);
  1128. else /* no mem */
  1129. goto stall;
  1130. list_add_tail(&req->queue, &ep->queue);
  1131. udc->ep0_state = DATA_STATE_XMIT;
  1132. return;
  1133. stall:
  1134. ep0stall(udc);
  1135. }
  1136. static void setup_received_irq(struct fsl_udc *udc,
  1137. struct usb_ctrlrequest *setup)
  1138. {
  1139. u16 wValue = le16_to_cpu(setup->wValue);
  1140. u16 wIndex = le16_to_cpu(setup->wIndex);
  1141. u16 wLength = le16_to_cpu(setup->wLength);
  1142. udc_reset_ep_queue(udc, 0);
  1143. /* We process some stardard setup requests here */
  1144. switch (setup->bRequest) {
  1145. case USB_REQ_GET_STATUS:
  1146. /* Data+Status phase from udc */
  1147. if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
  1148. != (USB_DIR_IN | USB_TYPE_STANDARD))
  1149. break;
  1150. ch9getstatus(udc, setup->bRequestType, wValue, wIndex, wLength);
  1151. return;
  1152. case USB_REQ_SET_ADDRESS:
  1153. /* Status phase from udc */
  1154. if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
  1155. | USB_RECIP_DEVICE))
  1156. break;
  1157. ch9setaddress(udc, wValue, wIndex, wLength);
  1158. return;
  1159. case USB_REQ_CLEAR_FEATURE:
  1160. case USB_REQ_SET_FEATURE:
  1161. /* Status phase from udc */
  1162. {
  1163. int rc = -EOPNOTSUPP;
  1164. u16 ptc = 0;
  1165. if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
  1166. == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
  1167. int pipe = get_pipe_by_windex(wIndex);
  1168. struct fsl_ep *ep;
  1169. if (wValue != 0 || wLength != 0 || pipe > udc->max_ep)
  1170. break;
  1171. ep = get_ep_by_pipe(udc, pipe);
  1172. spin_unlock(&udc->lock);
  1173. rc = fsl_ep_set_halt(&ep->ep,
  1174. (setup->bRequest == USB_REQ_SET_FEATURE)
  1175. ? 1 : 0);
  1176. spin_lock(&udc->lock);
  1177. } else if ((setup->bRequestType & (USB_RECIP_MASK
  1178. | USB_TYPE_MASK)) == (USB_RECIP_DEVICE
  1179. | USB_TYPE_STANDARD)) {
  1180. /* Note: The driver has not include OTG support yet.
  1181. * This will be set when OTG support is added */
  1182. if (wValue == USB_DEVICE_TEST_MODE)
  1183. ptc = wIndex >> 8;
  1184. else if (gadget_is_otg(&udc->gadget)) {
  1185. if (setup->bRequest ==
  1186. USB_DEVICE_B_HNP_ENABLE)
  1187. udc->gadget.b_hnp_enable = 1;
  1188. else if (setup->bRequest ==
  1189. USB_DEVICE_A_HNP_SUPPORT)
  1190. udc->gadget.a_hnp_support = 1;
  1191. else if (setup->bRequest ==
  1192. USB_DEVICE_A_ALT_HNP_SUPPORT)
  1193. udc->gadget.a_alt_hnp_support = 1;
  1194. }
  1195. rc = 0;
  1196. } else
  1197. break;
  1198. if (rc == 0) {
  1199. if (ep0_prime_status(udc, EP_DIR_IN))
  1200. ep0stall(udc);
  1201. }
  1202. if (ptc) {
  1203. u32 tmp;
  1204. mdelay(10);
  1205. tmp = fsl_readl(&dr_regs->portsc1) | (ptc << 16);
  1206. fsl_writel(tmp, &dr_regs->portsc1);
  1207. printk(KERN_INFO "udc: switch to test mode %d.\n", ptc);
  1208. }
  1209. return;
  1210. }
  1211. default:
  1212. break;
  1213. }
  1214. /* Requests handled by gadget */
  1215. if (wLength) {
  1216. /* Data phase from gadget, status phase from udc */
  1217. udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
  1218. ? USB_DIR_IN : USB_DIR_OUT;
  1219. spin_unlock(&udc->lock);
  1220. if (udc->driver->setup(&udc->gadget,
  1221. &udc->local_setup_buff) < 0)
  1222. ep0stall(udc);
  1223. spin_lock(&udc->lock);
  1224. udc->ep0_state = (setup->bRequestType & USB_DIR_IN)
  1225. ? DATA_STATE_XMIT : DATA_STATE_RECV;
  1226. } else {
  1227. /* No data phase, IN status from gadget */
  1228. udc->ep0_dir = USB_DIR_IN;
  1229. spin_unlock(&udc->lock);
  1230. if (udc->driver->setup(&udc->gadget,
  1231. &udc->local_setup_buff) < 0)
  1232. ep0stall(udc);
  1233. spin_lock(&udc->lock);
  1234. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1235. }
  1236. }
  1237. /* Process request for Data or Status phase of ep0
  1238. * prime status phase if needed */
  1239. static void ep0_req_complete(struct fsl_udc *udc, struct fsl_ep *ep0,
  1240. struct fsl_req *req)
  1241. {
  1242. if (udc->usb_state == USB_STATE_ADDRESS) {
  1243. /* Set the new address */
  1244. u32 new_address = (u32) udc->device_address;
  1245. fsl_writel(new_address << USB_DEVICE_ADDRESS_BIT_POS,
  1246. &dr_regs->deviceaddr);
  1247. }
  1248. done(ep0, req, 0);
  1249. switch (udc->ep0_state) {
  1250. case DATA_STATE_XMIT:
  1251. /* receive status phase */
  1252. if (ep0_prime_status(udc, EP_DIR_OUT))
  1253. ep0stall(udc);
  1254. break;
  1255. case DATA_STATE_RECV:
  1256. /* send status phase */
  1257. if (ep0_prime_status(udc, EP_DIR_IN))
  1258. ep0stall(udc);
  1259. break;
  1260. case WAIT_FOR_OUT_STATUS:
  1261. udc->ep0_state = WAIT_FOR_SETUP;
  1262. break;
  1263. case WAIT_FOR_SETUP:
  1264. ERR("Unexpect ep0 packets\n");
  1265. break;
  1266. default:
  1267. ep0stall(udc);
  1268. break;
  1269. }
  1270. }
  1271. /* Tripwire mechanism to ensure a setup packet payload is extracted without
  1272. * being corrupted by another incoming setup packet */
  1273. static void tripwire_handler(struct fsl_udc *udc, u8 ep_num, u8 *buffer_ptr)
  1274. {
  1275. u32 temp;
  1276. struct ep_queue_head *qh;
  1277. struct fsl_usb2_platform_data *pdata = udc->pdata;
  1278. qh = &udc->ep_qh[ep_num * 2 + EP_DIR_OUT];
  1279. /* Clear bit in ENDPTSETUPSTAT */
  1280. temp = fsl_readl(&dr_regs->endptsetupstat);
  1281. fsl_writel(temp | (1 << ep_num), &dr_regs->endptsetupstat);
  1282. /* while a hazard exists when setup package arrives */
  1283. do {
  1284. /* Set Setup Tripwire */
  1285. temp = fsl_readl(&dr_regs->usbcmd);
  1286. fsl_writel(temp | USB_CMD_SUTW, &dr_regs->usbcmd);
  1287. /* Copy the setup packet to local buffer */
  1288. if (pdata->le_setup_buf) {
  1289. u32 *p = (u32 *)buffer_ptr;
  1290. u32 *s = (u32 *)qh->setup_buffer;
  1291. /* Convert little endian setup buffer to CPU endian */
  1292. *p++ = le32_to_cpu(*s++);
  1293. *p = le32_to_cpu(*s);
  1294. } else {
  1295. memcpy(buffer_ptr, (u8 *) qh->setup_buffer, 8);
  1296. }
  1297. } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_SUTW));
  1298. /* Clear Setup Tripwire */
  1299. temp = fsl_readl(&dr_regs->usbcmd);
  1300. fsl_writel(temp & ~USB_CMD_SUTW, &dr_regs->usbcmd);
  1301. }
  1302. /* process-ep_req(): free the completed Tds for this req */
  1303. static int process_ep_req(struct fsl_udc *udc, int pipe,
  1304. struct fsl_req *curr_req)
  1305. {
  1306. struct ep_td_struct *curr_td;
  1307. int td_complete, actual, remaining_length, j, tmp;
  1308. int status = 0;
  1309. int errors = 0;
  1310. struct ep_queue_head *curr_qh = &udc->ep_qh[pipe];
  1311. int direction = pipe % 2;
  1312. curr_td = curr_req->head;
  1313. td_complete = 0;
  1314. actual = curr_req->req.length;
  1315. for (j = 0; j < curr_req->dtd_count; j++) {
  1316. remaining_length = (hc32_to_cpu(curr_td->size_ioc_sts)
  1317. & DTD_PACKET_SIZE)
  1318. >> DTD_LENGTH_BIT_POS;
  1319. actual -= remaining_length;
  1320. errors = hc32_to_cpu(curr_td->size_ioc_sts);
  1321. if (errors & DTD_ERROR_MASK) {
  1322. if (errors & DTD_STATUS_HALTED) {
  1323. ERR("dTD error %08x QH=%d\n", errors, pipe);
  1324. /* Clear the errors and Halt condition */
  1325. tmp = hc32_to_cpu(curr_qh->size_ioc_int_sts);
  1326. tmp &= ~errors;
  1327. curr_qh->size_ioc_int_sts = cpu_to_hc32(tmp);
  1328. status = -EPIPE;
  1329. /* FIXME: continue with next queued TD? */
  1330. break;
  1331. }
  1332. if (errors & DTD_STATUS_DATA_BUFF_ERR) {
  1333. VDBG("Transfer overflow");
  1334. status = -EPROTO;
  1335. break;
  1336. } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
  1337. VDBG("ISO error");
  1338. status = -EILSEQ;
  1339. break;
  1340. } else
  1341. ERR("Unknown error has occurred (0x%x)!\n",
  1342. errors);
  1343. } else if (hc32_to_cpu(curr_td->size_ioc_sts)
  1344. & DTD_STATUS_ACTIVE) {
  1345. VDBG("Request not complete");
  1346. status = REQ_UNCOMPLETE;
  1347. return status;
  1348. } else if (remaining_length) {
  1349. if (direction) {
  1350. VDBG("Transmit dTD remaining length not zero");
  1351. status = -EPROTO;
  1352. break;
  1353. } else {
  1354. td_complete++;
  1355. break;
  1356. }
  1357. } else {
  1358. td_complete++;
  1359. VDBG("dTD transmitted successful");
  1360. }
  1361. if (j != curr_req->dtd_count - 1)
  1362. curr_td = (struct ep_td_struct *)curr_td->next_td_virt;
  1363. }
  1364. if (status)
  1365. return status;
  1366. curr_req->req.actual = actual;
  1367. return 0;
  1368. }
  1369. /* Process a DTD completion interrupt */
  1370. static void dtd_complete_irq(struct fsl_udc *udc)
  1371. {
  1372. u32 bit_pos;
  1373. int i, ep_num, direction, bit_mask, status;
  1374. struct fsl_ep *curr_ep;
  1375. struct fsl_req *curr_req, *temp_req;
  1376. /* Clear the bits in the register */
  1377. bit_pos = fsl_readl(&dr_regs->endptcomplete);
  1378. fsl_writel(bit_pos, &dr_regs->endptcomplete);
  1379. if (!bit_pos)
  1380. return;
  1381. for (i = 0; i < udc->max_ep * 2; i++) {
  1382. ep_num = i >> 1;
  1383. direction = i % 2;
  1384. bit_mask = 1 << (ep_num + 16 * direction);
  1385. if (!(bit_pos & bit_mask))
  1386. continue;
  1387. curr_ep = get_ep_by_pipe(udc, i);
  1388. /* If the ep is configured */
  1389. if (curr_ep->name == NULL) {
  1390. WARNING("Invalid EP?");
  1391. continue;
  1392. }
  1393. /* process the req queue until an uncomplete request */
  1394. list_for_each_entry_safe(curr_req, temp_req, &curr_ep->queue,
  1395. queue) {
  1396. status = process_ep_req(udc, i, curr_req);
  1397. VDBG("status of process_ep_req= %d, ep = %d",
  1398. status, ep_num);
  1399. if (status == REQ_UNCOMPLETE)
  1400. break;
  1401. /* write back status to req */
  1402. curr_req->req.status = status;
  1403. if (ep_num == 0) {
  1404. ep0_req_complete(udc, curr_ep, curr_req);
  1405. break;
  1406. } else
  1407. done(curr_ep, curr_req, status);
  1408. }
  1409. }
  1410. }
  1411. /* Process a port change interrupt */
  1412. static void port_change_irq(struct fsl_udc *udc)
  1413. {
  1414. u32 speed;
  1415. if (udc->bus_reset)
  1416. udc->bus_reset = 0;
  1417. /* Bus resetting is finished */
  1418. if (!(fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET)) {
  1419. /* Get the speed */
  1420. speed = (fsl_readl(&dr_regs->portsc1)
  1421. & PORTSCX_PORT_SPEED_MASK);
  1422. switch (speed) {
  1423. case PORTSCX_PORT_SPEED_HIGH:
  1424. udc->gadget.speed = USB_SPEED_HIGH;
  1425. break;
  1426. case PORTSCX_PORT_SPEED_FULL:
  1427. udc->gadget.speed = USB_SPEED_FULL;
  1428. break;
  1429. case PORTSCX_PORT_SPEED_LOW:
  1430. udc->gadget.speed = USB_SPEED_LOW;
  1431. break;
  1432. default:
  1433. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1434. break;
  1435. }
  1436. }
  1437. /* Update USB state */
  1438. if (!udc->resume_state)
  1439. udc->usb_state = USB_STATE_DEFAULT;
  1440. }
  1441. /* Process suspend interrupt */
  1442. static void suspend_irq(struct fsl_udc *udc)
  1443. {
  1444. udc->resume_state = udc->usb_state;
  1445. udc->usb_state = USB_STATE_SUSPENDED;
  1446. /* report suspend to the driver, serial.c does not support this */
  1447. if (udc->driver->suspend)
  1448. udc->driver->suspend(&udc->gadget);
  1449. }
  1450. static void bus_resume(struct fsl_udc *udc)
  1451. {
  1452. udc->usb_state = udc->resume_state;
  1453. udc->resume_state = 0;
  1454. /* report resume to the driver, serial.c does not support this */
  1455. if (udc->driver->resume)
  1456. udc->driver->resume(&udc->gadget);
  1457. }
  1458. /* Clear up all ep queues */
  1459. static int reset_queues(struct fsl_udc *udc)
  1460. {
  1461. u8 pipe;
  1462. for (pipe = 0; pipe < udc->max_pipes; pipe++)
  1463. udc_reset_ep_queue(udc, pipe);
  1464. /* report disconnect; the driver is already quiesced */
  1465. spin_unlock(&udc->lock);
  1466. udc->driver->disconnect(&udc->gadget);
  1467. spin_lock(&udc->lock);
  1468. return 0;
  1469. }
  1470. /* Process reset interrupt */
  1471. static void reset_irq(struct fsl_udc *udc)
  1472. {
  1473. u32 temp;
  1474. unsigned long timeout;
  1475. /* Clear the device address */
  1476. temp = fsl_readl(&dr_regs->deviceaddr);
  1477. fsl_writel(temp & ~USB_DEVICE_ADDRESS_MASK, &dr_regs->deviceaddr);
  1478. udc->device_address = 0;
  1479. /* Clear usb state */
  1480. udc->resume_state = 0;
  1481. udc->ep0_dir = 0;
  1482. udc->ep0_state = WAIT_FOR_SETUP;
  1483. udc->remote_wakeup = 0; /* default to 0 on reset */
  1484. udc->gadget.b_hnp_enable = 0;
  1485. udc->gadget.a_hnp_support = 0;
  1486. udc->gadget.a_alt_hnp_support = 0;
  1487. /* Clear all the setup token semaphores */
  1488. temp = fsl_readl(&dr_regs->endptsetupstat);
  1489. fsl_writel(temp, &dr_regs->endptsetupstat);
  1490. /* Clear all the endpoint complete status bits */
  1491. temp = fsl_readl(&dr_regs->endptcomplete);
  1492. fsl_writel(temp, &dr_regs->endptcomplete);
  1493. timeout = jiffies + 100;
  1494. while (fsl_readl(&dr_regs->endpointprime)) {
  1495. /* Wait until all endptprime bits cleared */
  1496. if (time_after(jiffies, timeout)) {
  1497. ERR("Timeout for reset\n");
  1498. break;
  1499. }
  1500. cpu_relax();
  1501. }
  1502. /* Write 1s to the flush register */
  1503. fsl_writel(0xffffffff, &dr_regs->endptflush);
  1504. if (fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET) {
  1505. VDBG("Bus reset");
  1506. /* Bus is reseting */
  1507. udc->bus_reset = 1;
  1508. /* Reset all the queues, include XD, dTD, EP queue
  1509. * head and TR Queue */
  1510. reset_queues(udc);
  1511. udc->usb_state = USB_STATE_DEFAULT;
  1512. } else {
  1513. VDBG("Controller reset");
  1514. /* initialize usb hw reg except for regs for EP, not
  1515. * touch usbintr reg */
  1516. dr_controller_setup(udc);
  1517. /* Reset all internal used Queues */
  1518. reset_queues(udc);
  1519. ep0_setup(udc);
  1520. /* Enable DR IRQ reg, Set Run bit, change udc state */
  1521. dr_controller_run(udc);
  1522. udc->usb_state = USB_STATE_ATTACHED;
  1523. }
  1524. }
  1525. /*
  1526. * USB device controller interrupt handler
  1527. */
  1528. static irqreturn_t fsl_udc_irq(int irq, void *_udc)
  1529. {
  1530. struct fsl_udc *udc = _udc;
  1531. u32 irq_src;
  1532. irqreturn_t status = IRQ_NONE;
  1533. unsigned long flags;
  1534. /* Disable ISR for OTG host mode */
  1535. if (udc->stopped)
  1536. return IRQ_NONE;
  1537. spin_lock_irqsave(&udc->lock, flags);
  1538. irq_src = fsl_readl(&dr_regs->usbsts) & fsl_readl(&dr_regs->usbintr);
  1539. /* Clear notification bits */
  1540. fsl_writel(irq_src, &dr_regs->usbsts);
  1541. /* VDBG("irq_src [0x%8x]", irq_src); */
  1542. /* Need to resume? */
  1543. if (udc->usb_state == USB_STATE_SUSPENDED)
  1544. if ((fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_SUSPEND) == 0)
  1545. bus_resume(udc);
  1546. /* USB Interrupt */
  1547. if (irq_src & USB_STS_INT) {
  1548. VDBG("Packet int");
  1549. /* Setup package, we only support ep0 as control ep */
  1550. if (fsl_readl(&dr_regs->endptsetupstat) & EP_SETUP_STATUS_EP0) {
  1551. tripwire_handler(udc, 0,
  1552. (u8 *) (&udc->local_setup_buff));
  1553. setup_received_irq(udc, &udc->local_setup_buff);
  1554. status = IRQ_HANDLED;
  1555. }
  1556. /* completion of dtd */
  1557. if (fsl_readl(&dr_regs->endptcomplete)) {
  1558. dtd_complete_irq(udc);
  1559. status = IRQ_HANDLED;
  1560. }
  1561. }
  1562. /* SOF (for ISO transfer) */
  1563. if (irq_src & USB_STS_SOF) {
  1564. status = IRQ_HANDLED;
  1565. }
  1566. /* Port Change */
  1567. if (irq_src & USB_STS_PORT_CHANGE) {
  1568. port_change_irq(udc);
  1569. status = IRQ_HANDLED;
  1570. }
  1571. /* Reset Received */
  1572. if (irq_src & USB_STS_RESET) {
  1573. VDBG("reset int");
  1574. reset_irq(udc);
  1575. status = IRQ_HANDLED;
  1576. }
  1577. /* Sleep Enable (Suspend) */
  1578. if (irq_src & USB_STS_SUSPEND) {
  1579. suspend_irq(udc);
  1580. status = IRQ_HANDLED;
  1581. }
  1582. if (irq_src & (USB_STS_ERR | USB_STS_SYS_ERR)) {
  1583. VDBG("Error IRQ %x", irq_src);
  1584. }
  1585. spin_unlock_irqrestore(&udc->lock, flags);
  1586. return status;
  1587. }
  1588. /*----------------------------------------------------------------*
  1589. * Hook to gadget drivers
  1590. * Called by initialization code of gadget drivers
  1591. *----------------------------------------------------------------*/
  1592. int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
  1593. int (*bind)(struct usb_gadget *))
  1594. {
  1595. int retval = -ENODEV;
  1596. unsigned long flags = 0;
  1597. if (!udc_controller)
  1598. return -ENODEV;
  1599. if (!driver || (driver->speed != USB_SPEED_FULL
  1600. && driver->speed != USB_SPEED_HIGH)
  1601. || !bind || !driver->disconnect || !driver->setup)
  1602. return -EINVAL;
  1603. if (udc_controller->driver)
  1604. return -EBUSY;
  1605. /* lock is needed but whether should use this lock or another */
  1606. spin_lock_irqsave(&udc_controller->lock, flags);
  1607. driver->driver.bus = NULL;
  1608. /* hook up the driver */
  1609. udc_controller->driver = driver;
  1610. udc_controller->gadget.dev.driver = &driver->driver;
  1611. spin_unlock_irqrestore(&udc_controller->lock, flags);
  1612. /* bind udc driver to gadget driver */
  1613. retval = bind(&udc_controller->gadget);
  1614. if (retval) {
  1615. VDBG("bind to %s --> %d", driver->driver.name, retval);
  1616. udc_controller->gadget.dev.driver = NULL;
  1617. udc_controller->driver = NULL;
  1618. goto out;
  1619. }
  1620. if (udc_controller->transceiver) {
  1621. /* Suspend the controller until OTG enable it */
  1622. udc_controller->stopped = 1;
  1623. printk(KERN_INFO "Suspend udc for OTG auto detect\n");
  1624. /* connect to bus through transceiver */
  1625. if (udc_controller->transceiver) {
  1626. retval = otg_set_peripheral(udc_controller->transceiver,
  1627. &udc_controller->gadget);
  1628. if (retval < 0) {
  1629. ERR("can't bind to transceiver\n");
  1630. driver->unbind(&udc_controller->gadget);
  1631. udc_controller->gadget.dev.driver = 0;
  1632. udc_controller->driver = 0;
  1633. return retval;
  1634. }
  1635. }
  1636. } else {
  1637. /* Enable DR IRQ reg and set USBCMD reg Run bit */
  1638. dr_controller_run(udc_controller);
  1639. udc_controller->usb_state = USB_STATE_ATTACHED;
  1640. udc_controller->ep0_state = WAIT_FOR_SETUP;
  1641. udc_controller->ep0_dir = 0;
  1642. }
  1643. printk(KERN_INFO "%s: bind to driver %s\n",
  1644. udc_controller->gadget.name, driver->driver.name);
  1645. out:
  1646. if (retval)
  1647. printk(KERN_WARNING "gadget driver register failed %d\n",
  1648. retval);
  1649. return retval;
  1650. }
  1651. EXPORT_SYMBOL(usb_gadget_probe_driver);
  1652. /* Disconnect from gadget driver */
  1653. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1654. {
  1655. struct fsl_ep *loop_ep;
  1656. unsigned long flags;
  1657. if (!udc_controller)
  1658. return -ENODEV;
  1659. if (!driver || driver != udc_controller->driver || !driver->unbind)
  1660. return -EINVAL;
  1661. if (udc_controller->transceiver)
  1662. otg_set_peripheral(udc_controller->transceiver, NULL);
  1663. /* stop DR, disable intr */
  1664. dr_controller_stop(udc_controller);
  1665. /* in fact, no needed */
  1666. udc_controller->usb_state = USB_STATE_ATTACHED;
  1667. udc_controller->ep0_state = WAIT_FOR_SETUP;
  1668. udc_controller->ep0_dir = 0;
  1669. /* stand operation */
  1670. spin_lock_irqsave(&udc_controller->lock, flags);
  1671. udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
  1672. nuke(&udc_controller->eps[0], -ESHUTDOWN);
  1673. list_for_each_entry(loop_ep, &udc_controller->gadget.ep_list,
  1674. ep.ep_list)
  1675. nuke(loop_ep, -ESHUTDOWN);
  1676. spin_unlock_irqrestore(&udc_controller->lock, flags);
  1677. /* report disconnect; the controller is already quiesced */
  1678. driver->disconnect(&udc_controller->gadget);
  1679. /* unbind gadget and unhook driver. */
  1680. driver->unbind(&udc_controller->gadget);
  1681. udc_controller->gadget.dev.driver = NULL;
  1682. udc_controller->driver = NULL;
  1683. printk(KERN_WARNING "unregistered gadget driver '%s'\n",
  1684. driver->driver.name);
  1685. return 0;
  1686. }
  1687. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1688. /*-------------------------------------------------------------------------
  1689. PROC File System Support
  1690. -------------------------------------------------------------------------*/
  1691. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  1692. #include <linux/seq_file.h>
  1693. static const char proc_filename[] = "driver/fsl_usb2_udc";
  1694. static int fsl_proc_read(char *page, char **start, off_t off, int count,
  1695. int *eof, void *_dev)
  1696. {
  1697. char *buf = page;
  1698. char *next = buf;
  1699. unsigned size = count;
  1700. unsigned long flags;
  1701. int t, i;
  1702. u32 tmp_reg;
  1703. struct fsl_ep *ep = NULL;
  1704. struct fsl_req *req;
  1705. struct fsl_udc *udc = udc_controller;
  1706. if (off != 0)
  1707. return 0;
  1708. spin_lock_irqsave(&udc->lock, flags);
  1709. /* ------basic driver information ---- */
  1710. t = scnprintf(next, size,
  1711. DRIVER_DESC "\n"
  1712. "%s version: %s\n"
  1713. "Gadget driver: %s\n\n",
  1714. driver_name, DRIVER_VERSION,
  1715. udc->driver ? udc->driver->driver.name : "(none)");
  1716. size -= t;
  1717. next += t;
  1718. /* ------ DR Registers ----- */
  1719. tmp_reg = fsl_readl(&dr_regs->usbcmd);
  1720. t = scnprintf(next, size,
  1721. "USBCMD reg:\n"
  1722. "SetupTW: %d\n"
  1723. "Run/Stop: %s\n\n",
  1724. (tmp_reg & USB_CMD_SUTW) ? 1 : 0,
  1725. (tmp_reg & USB_CMD_RUN_STOP) ? "Run" : "Stop");
  1726. size -= t;
  1727. next += t;
  1728. tmp_reg = fsl_readl(&dr_regs->usbsts);
  1729. t = scnprintf(next, size,
  1730. "USB Status Reg:\n"
  1731. "Dr Suspend: %d Reset Received: %d System Error: %s "
  1732. "USB Error Interrupt: %s\n\n",
  1733. (tmp_reg & USB_STS_SUSPEND) ? 1 : 0,
  1734. (tmp_reg & USB_STS_RESET) ? 1 : 0,
  1735. (tmp_reg & USB_STS_SYS_ERR) ? "Err" : "Normal",
  1736. (tmp_reg & USB_STS_ERR) ? "Err detected" : "No err");
  1737. size -= t;
  1738. next += t;
  1739. tmp_reg = fsl_readl(&dr_regs->usbintr);
  1740. t = scnprintf(next, size,
  1741. "USB Intrrupt Enable Reg:\n"
  1742. "Sleep Enable: %d SOF Received Enable: %d "
  1743. "Reset Enable: %d\n"
  1744. "System Error Enable: %d "
  1745. "Port Change Dectected Enable: %d\n"
  1746. "USB Error Intr Enable: %d USB Intr Enable: %d\n\n",
  1747. (tmp_reg & USB_INTR_DEVICE_SUSPEND) ? 1 : 0,
  1748. (tmp_reg & USB_INTR_SOF_EN) ? 1 : 0,
  1749. (tmp_reg & USB_INTR_RESET_EN) ? 1 : 0,
  1750. (tmp_reg & USB_INTR_SYS_ERR_EN) ? 1 : 0,
  1751. (tmp_reg & USB_INTR_PTC_DETECT_EN) ? 1 : 0,
  1752. (tmp_reg & USB_INTR_ERR_INT_EN) ? 1 : 0,
  1753. (tmp_reg & USB_INTR_INT_EN) ? 1 : 0);
  1754. size -= t;
  1755. next += t;
  1756. tmp_reg = fsl_readl(&dr_regs->frindex);
  1757. t = scnprintf(next, size,
  1758. "USB Frame Index Reg: Frame Number is 0x%x\n\n",
  1759. (tmp_reg & USB_FRINDEX_MASKS));
  1760. size -= t;
  1761. next += t;
  1762. tmp_reg = fsl_readl(&dr_regs->deviceaddr);
  1763. t = scnprintf(next, size,
  1764. "USB Device Address Reg: Device Addr is 0x%x\n\n",
  1765. (tmp_reg & USB_DEVICE_ADDRESS_MASK));
  1766. size -= t;
  1767. next += t;
  1768. tmp_reg = fsl_readl(&dr_regs->endpointlistaddr);
  1769. t = scnprintf(next, size,
  1770. "USB Endpoint List Address Reg: "
  1771. "Device Addr is 0x%x\n\n",
  1772. (tmp_reg & USB_EP_LIST_ADDRESS_MASK));
  1773. size -= t;
  1774. next += t;
  1775. tmp_reg = fsl_readl(&dr_regs->portsc1);
  1776. t = scnprintf(next, size,
  1777. "USB Port Status&Control Reg:\n"
  1778. "Port Transceiver Type : %s Port Speed: %s\n"
  1779. "PHY Low Power Suspend: %s Port Reset: %s "
  1780. "Port Suspend Mode: %s\n"
  1781. "Over-current Change: %s "
  1782. "Port Enable/Disable Change: %s\n"
  1783. "Port Enabled/Disabled: %s "
  1784. "Current Connect Status: %s\n\n", ( {
  1785. char *s;
  1786. switch (tmp_reg & PORTSCX_PTS_FSLS) {
  1787. case PORTSCX_PTS_UTMI:
  1788. s = "UTMI"; break;
  1789. case PORTSCX_PTS_ULPI:
  1790. s = "ULPI "; break;
  1791. case PORTSCX_PTS_FSLS:
  1792. s = "FS/LS Serial"; break;
  1793. default:
  1794. s = "None"; break;
  1795. }
  1796. s;} ), ( {
  1797. char *s;
  1798. switch (tmp_reg & PORTSCX_PORT_SPEED_UNDEF) {
  1799. case PORTSCX_PORT_SPEED_FULL:
  1800. s = "Full Speed"; break;
  1801. case PORTSCX_PORT_SPEED_LOW:
  1802. s = "Low Speed"; break;
  1803. case PORTSCX_PORT_SPEED_HIGH:
  1804. s = "High Speed"; break;
  1805. default:
  1806. s = "Undefined"; break;
  1807. }
  1808. s;
  1809. } ),
  1810. (tmp_reg & PORTSCX_PHY_LOW_POWER_SPD) ?
  1811. "Normal PHY mode" : "Low power mode",
  1812. (tmp_reg & PORTSCX_PORT_RESET) ? "In Reset" :
  1813. "Not in Reset",
  1814. (tmp_reg & PORTSCX_PORT_SUSPEND) ? "In " : "Not in",
  1815. (tmp_reg & PORTSCX_OVER_CURRENT_CHG) ? "Dected" :
  1816. "No",
  1817. (tmp_reg & PORTSCX_PORT_EN_DIS_CHANGE) ? "Disable" :
  1818. "Not change",
  1819. (tmp_reg & PORTSCX_PORT_ENABLE) ? "Enable" :
  1820. "Not correct",
  1821. (tmp_reg & PORTSCX_CURRENT_CONNECT_STATUS) ?
  1822. "Attached" : "Not-Att");
  1823. size -= t;
  1824. next += t;
  1825. tmp_reg = fsl_readl(&dr_regs->usbmode);
  1826. t = scnprintf(next, size,
  1827. "USB Mode Reg: Controller Mode is: %s\n\n", ( {
  1828. char *s;
  1829. switch (tmp_reg & USB_MODE_CTRL_MODE_HOST) {
  1830. case USB_MODE_CTRL_MODE_IDLE:
  1831. s = "Idle"; break;
  1832. case USB_MODE_CTRL_MODE_DEVICE:
  1833. s = "Device Controller"; break;
  1834. case USB_MODE_CTRL_MODE_HOST:
  1835. s = "Host Controller"; break;
  1836. default:
  1837. s = "None"; break;
  1838. }
  1839. s;
  1840. } ));
  1841. size -= t;
  1842. next += t;
  1843. tmp_reg = fsl_readl(&dr_regs->endptsetupstat);
  1844. t = scnprintf(next, size,
  1845. "Endpoint Setup Status Reg: SETUP on ep 0x%x\n\n",
  1846. (tmp_reg & EP_SETUP_STATUS_MASK));
  1847. size -= t;
  1848. next += t;
  1849. for (i = 0; i < udc->max_ep / 2; i++) {
  1850. tmp_reg = fsl_readl(&dr_regs->endptctrl[i]);
  1851. t = scnprintf(next, size, "EP Ctrl Reg [0x%x]: = [0x%x]\n",
  1852. i, tmp_reg);
  1853. size -= t;
  1854. next += t;
  1855. }
  1856. tmp_reg = fsl_readl(&dr_regs->endpointprime);
  1857. t = scnprintf(next, size, "EP Prime Reg = [0x%x]\n\n", tmp_reg);
  1858. size -= t;
  1859. next += t;
  1860. #ifndef CONFIG_ARCH_MXC
  1861. if (udc->pdata->have_sysif_regs) {
  1862. tmp_reg = usb_sys_regs->snoop1;
  1863. t = scnprintf(next, size, "Snoop1 Reg : = [0x%x]\n\n", tmp_reg);
  1864. size -= t;
  1865. next += t;
  1866. tmp_reg = usb_sys_regs->control;
  1867. t = scnprintf(next, size, "General Control Reg : = [0x%x]\n\n",
  1868. tmp_reg);
  1869. size -= t;
  1870. next += t;
  1871. }
  1872. #endif
  1873. /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */
  1874. ep = &udc->eps[0];
  1875. t = scnprintf(next, size, "For %s Maxpkt is 0x%x index is 0x%x\n",
  1876. ep->ep.name, ep_maxpacket(ep), ep_index(ep));
  1877. size -= t;
  1878. next += t;
  1879. if (list_empty(&ep->queue)) {
  1880. t = scnprintf(next, size, "its req queue is empty\n\n");
  1881. size -= t;
  1882. next += t;
  1883. } else {
  1884. list_for_each_entry(req, &ep->queue, queue) {
  1885. t = scnprintf(next, size,
  1886. "req %p actual 0x%x length 0x%x buf %p\n",
  1887. &req->req, req->req.actual,
  1888. req->req.length, req->req.buf);
  1889. size -= t;
  1890. next += t;
  1891. }
  1892. }
  1893. /* other gadget->eplist ep */
  1894. list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
  1895. if (ep->desc) {
  1896. t = scnprintf(next, size,
  1897. "\nFor %s Maxpkt is 0x%x "
  1898. "index is 0x%x\n",
  1899. ep->ep.name, ep_maxpacket(ep),
  1900. ep_index(ep));
  1901. size -= t;
  1902. next += t;
  1903. if (list_empty(&ep->queue)) {
  1904. t = scnprintf(next, size,
  1905. "its req queue is empty\n\n");
  1906. size -= t;
  1907. next += t;
  1908. } else {
  1909. list_for_each_entry(req, &ep->queue, queue) {
  1910. t = scnprintf(next, size,
  1911. "req %p actual 0x%x length "
  1912. "0x%x buf %p\n",
  1913. &req->req, req->req.actual,
  1914. req->req.length, req->req.buf);
  1915. size -= t;
  1916. next += t;
  1917. } /* end for each_entry of ep req */
  1918. } /* end for else */
  1919. } /* end for if(ep->queue) */
  1920. } /* end (ep->desc) */
  1921. spin_unlock_irqrestore(&udc->lock, flags);
  1922. *eof = 1;
  1923. return count - size;
  1924. }
  1925. #define create_proc_file() create_proc_read_entry(proc_filename, \
  1926. 0, NULL, fsl_proc_read, NULL)
  1927. #define remove_proc_file() remove_proc_entry(proc_filename, NULL)
  1928. #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
  1929. #define create_proc_file() do {} while (0)
  1930. #define remove_proc_file() do {} while (0)
  1931. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  1932. /*-------------------------------------------------------------------------*/
  1933. /* Release udc structures */
  1934. static void fsl_udc_release(struct device *dev)
  1935. {
  1936. complete(udc_controller->done);
  1937. dma_free_coherent(dev->parent, udc_controller->ep_qh_size,
  1938. udc_controller->ep_qh, udc_controller->ep_qh_dma);
  1939. kfree(udc_controller);
  1940. }
  1941. /******************************************************************
  1942. Internal structure setup functions
  1943. *******************************************************************/
  1944. /*------------------------------------------------------------------
  1945. * init resource for globle controller
  1946. * Return the udc handle on success or NULL on failure
  1947. ------------------------------------------------------------------*/
  1948. static int __init struct_udc_setup(struct fsl_udc *udc,
  1949. struct platform_device *pdev)
  1950. {
  1951. struct fsl_usb2_platform_data *pdata;
  1952. size_t size;
  1953. pdata = pdev->dev.platform_data;
  1954. udc->phy_mode = pdata->phy_mode;
  1955. udc->eps = kzalloc(sizeof(struct fsl_ep) * udc->max_ep, GFP_KERNEL);
  1956. if (!udc->eps) {
  1957. ERR("malloc fsl_ep failed\n");
  1958. return -1;
  1959. }
  1960. /* initialized QHs, take care of alignment */
  1961. size = udc->max_ep * sizeof(struct ep_queue_head);
  1962. if (size < QH_ALIGNMENT)
  1963. size = QH_ALIGNMENT;
  1964. else if ((size % QH_ALIGNMENT) != 0) {
  1965. size += QH_ALIGNMENT + 1;
  1966. size &= ~(QH_ALIGNMENT - 1);
  1967. }
  1968. udc->ep_qh = dma_alloc_coherent(&pdev->dev, size,
  1969. &udc->ep_qh_dma, GFP_KERNEL);
  1970. if (!udc->ep_qh) {
  1971. ERR("malloc QHs for udc failed\n");
  1972. kfree(udc->eps);
  1973. return -1;
  1974. }
  1975. udc->ep_qh_size = size;
  1976. /* Initialize ep0 status request structure */
  1977. /* FIXME: fsl_alloc_request() ignores ep argument */
  1978. udc->status_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL),
  1979. struct fsl_req, req);
  1980. /* allocate a small amount of memory to get valid address */
  1981. udc->status_req->req.buf = kmalloc(8, GFP_KERNEL);
  1982. udc->status_req->req.dma = virt_to_phys(udc->status_req->req.buf);
  1983. udc->resume_state = USB_STATE_NOTATTACHED;
  1984. udc->usb_state = USB_STATE_POWERED;
  1985. udc->ep0_dir = 0;
  1986. udc->remote_wakeup = 0; /* default to 0 on reset */
  1987. return 0;
  1988. }
  1989. /*----------------------------------------------------------------
  1990. * Setup the fsl_ep struct for eps
  1991. * Link fsl_ep->ep to gadget->ep_list
  1992. * ep0out is not used so do nothing here
  1993. * ep0in should be taken care
  1994. *--------------------------------------------------------------*/
  1995. static int __init struct_ep_setup(struct fsl_udc *udc, unsigned char index,
  1996. char *name, int link)
  1997. {
  1998. struct fsl_ep *ep = &udc->eps[index];
  1999. ep->udc = udc;
  2000. strcpy(ep->name, name);
  2001. ep->ep.name = ep->name;
  2002. ep->ep.ops = &fsl_ep_ops;
  2003. ep->stopped = 0;
  2004. /* for ep0: maxP defined in desc
  2005. * for other eps, maxP is set by epautoconfig() called by gadget layer
  2006. */
  2007. ep->ep.maxpacket = (unsigned short) ~0;
  2008. /* the queue lists any req for this ep */
  2009. INIT_LIST_HEAD(&ep->queue);
  2010. /* gagdet.ep_list used for ep_autoconfig so no ep0 */
  2011. if (link)
  2012. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  2013. ep->gadget = &udc->gadget;
  2014. ep->qh = &udc->ep_qh[index];
  2015. return 0;
  2016. }
  2017. /* Driver probe function
  2018. * all intialization operations implemented here except enabling usb_intr reg
  2019. * board setup should have been done in the platform code
  2020. */
  2021. static int __init fsl_udc_probe(struct platform_device *pdev)
  2022. {
  2023. struct fsl_usb2_platform_data *pdata;
  2024. struct resource *res;
  2025. int ret = -ENODEV;
  2026. unsigned int i;
  2027. u32 dccparams;
  2028. if (strcmp(pdev->name, driver_name)) {
  2029. VDBG("Wrong device");
  2030. return -ENODEV;
  2031. }
  2032. udc_controller = kzalloc(sizeof(struct fsl_udc), GFP_KERNEL);
  2033. if (udc_controller == NULL) {
  2034. ERR("malloc udc failed\n");
  2035. return -ENOMEM;
  2036. }
  2037. pdata = pdev->dev.platform_data;
  2038. udc_controller->pdata = pdata;
  2039. spin_lock_init(&udc_controller->lock);
  2040. udc_controller->stopped = 1;
  2041. #ifdef CONFIG_USB_OTG
  2042. if (pdata->operating_mode == FSL_USB2_DR_OTG) {
  2043. udc_controller->transceiver = otg_get_transceiver();
  2044. if (!udc_controller->transceiver) {
  2045. ERR("Can't find OTG driver!\n");
  2046. ret = -ENODEV;
  2047. goto err_kfree;
  2048. }
  2049. }
  2050. #endif
  2051. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2052. if (!res) {
  2053. ret = -ENXIO;
  2054. goto err_kfree;
  2055. }
  2056. if (pdata->operating_mode == FSL_USB2_DR_DEVICE) {
  2057. if (!request_mem_region(res->start, res->end - res->start + 1,
  2058. driver_name)) {
  2059. ERR("request mem region for %s failed\n", pdev->name);
  2060. ret = -EBUSY;
  2061. goto err_kfree;
  2062. }
  2063. }
  2064. dr_regs = ioremap(res->start, resource_size(res));
  2065. if (!dr_regs) {
  2066. ret = -ENOMEM;
  2067. goto err_release_mem_region;
  2068. }
  2069. pdata->regs = (void *)dr_regs;
  2070. /*
  2071. * do platform specific init: check the clock, grab/config pins, etc.
  2072. */
  2073. if (pdata->init && pdata->init(pdev)) {
  2074. ret = -ENODEV;
  2075. goto err_iounmap_noclk;
  2076. }
  2077. /* Set accessors only after pdata->init() ! */
  2078. if (pdata->big_endian_mmio) {
  2079. _fsl_readl = _fsl_readl_be;
  2080. _fsl_writel = _fsl_writel_be;
  2081. } else {
  2082. _fsl_readl = _fsl_readl_le;
  2083. _fsl_writel = _fsl_writel_le;
  2084. }
  2085. #ifndef CONFIG_ARCH_MXC
  2086. if (pdata->have_sysif_regs)
  2087. usb_sys_regs = (struct usb_sys_interface *)
  2088. ((u32)dr_regs + USB_DR_SYS_OFFSET);
  2089. #endif
  2090. /* Initialize USB clocks */
  2091. ret = fsl_udc_clk_init(pdev);
  2092. if (ret < 0)
  2093. goto err_iounmap_noclk;
  2094. /* Read Device Controller Capability Parameters register */
  2095. dccparams = fsl_readl(&dr_regs->dccparams);
  2096. if (!(dccparams & DCCPARAMS_DC)) {
  2097. ERR("This SOC doesn't support device role\n");
  2098. ret = -ENODEV;
  2099. goto err_iounmap;
  2100. }
  2101. /* Get max device endpoints */
  2102. /* DEN is bidirectional ep number, max_ep doubles the number */
  2103. udc_controller->max_ep = (dccparams & DCCPARAMS_DEN_MASK) * 2;
  2104. udc_controller->irq = platform_get_irq(pdev, 0);
  2105. if (!udc_controller->irq) {
  2106. ret = -ENODEV;
  2107. goto err_iounmap;
  2108. }
  2109. ret = request_irq(udc_controller->irq, fsl_udc_irq, IRQF_SHARED,
  2110. driver_name, udc_controller);
  2111. if (ret != 0) {
  2112. ERR("cannot request irq %d err %d\n",
  2113. udc_controller->irq, ret);
  2114. goto err_iounmap;
  2115. }
  2116. /* Initialize the udc structure including QH member and other member */
  2117. if (struct_udc_setup(udc_controller, pdev)) {
  2118. ERR("Can't initialize udc data structure\n");
  2119. ret = -ENOMEM;
  2120. goto err_free_irq;
  2121. }
  2122. if (!udc_controller->transceiver) {
  2123. /* initialize usb hw reg except for regs for EP,
  2124. * leave usbintr reg untouched */
  2125. dr_controller_setup(udc_controller);
  2126. }
  2127. fsl_udc_clk_finalize(pdev);
  2128. /* Setup gadget structure */
  2129. udc_controller->gadget.ops = &fsl_gadget_ops;
  2130. udc_controller->gadget.is_dualspeed = 1;
  2131. udc_controller->gadget.ep0 = &udc_controller->eps[0].ep;
  2132. INIT_LIST_HEAD(&udc_controller->gadget.ep_list);
  2133. udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
  2134. udc_controller->gadget.name = driver_name;
  2135. /* Setup gadget.dev and register with kernel */
  2136. dev_set_name(&udc_controller->gadget.dev, "gadget");
  2137. udc_controller->gadget.dev.release = fsl_udc_release;
  2138. udc_controller->gadget.dev.parent = &pdev->dev;
  2139. ret = device_register(&udc_controller->gadget.dev);
  2140. if (ret < 0)
  2141. goto err_free_irq;
  2142. if (udc_controller->transceiver)
  2143. udc_controller->gadget.is_otg = 1;
  2144. /* setup QH and epctrl for ep0 */
  2145. ep0_setup(udc_controller);
  2146. /* setup udc->eps[] for ep0 */
  2147. struct_ep_setup(udc_controller, 0, "ep0", 0);
  2148. /* for ep0: the desc defined here;
  2149. * for other eps, gadget layer called ep_enable with defined desc
  2150. */
  2151. udc_controller->eps[0].desc = &fsl_ep0_desc;
  2152. udc_controller->eps[0].ep.maxpacket = USB_MAX_CTRL_PAYLOAD;
  2153. /* setup the udc->eps[] for non-control endpoints and link
  2154. * to gadget.ep_list */
  2155. for (i = 1; i < (int)(udc_controller->max_ep / 2); i++) {
  2156. char name[14];
  2157. sprintf(name, "ep%dout", i);
  2158. struct_ep_setup(udc_controller, i * 2, name, 1);
  2159. sprintf(name, "ep%din", i);
  2160. struct_ep_setup(udc_controller, i * 2 + 1, name, 1);
  2161. }
  2162. /* use dma_pool for TD management */
  2163. udc_controller->td_pool = dma_pool_create("udc_td", &pdev->dev,
  2164. sizeof(struct ep_td_struct),
  2165. DTD_ALIGNMENT, UDC_DMA_BOUNDARY);
  2166. if (udc_controller->td_pool == NULL) {
  2167. ret = -ENOMEM;
  2168. goto err_unregister;
  2169. }
  2170. create_proc_file();
  2171. return 0;
  2172. err_unregister:
  2173. device_unregister(&udc_controller->gadget.dev);
  2174. err_free_irq:
  2175. free_irq(udc_controller->irq, udc_controller);
  2176. err_iounmap:
  2177. if (pdata->exit)
  2178. pdata->exit(pdev);
  2179. fsl_udc_clk_release();
  2180. err_iounmap_noclk:
  2181. iounmap(dr_regs);
  2182. err_release_mem_region:
  2183. if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
  2184. release_mem_region(res->start, res->end - res->start + 1);
  2185. err_kfree:
  2186. kfree(udc_controller);
  2187. udc_controller = NULL;
  2188. return ret;
  2189. }
  2190. /* Driver removal function
  2191. * Free resources and finish pending transactions
  2192. */
  2193. static int __exit fsl_udc_remove(struct platform_device *pdev)
  2194. {
  2195. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2196. struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
  2197. DECLARE_COMPLETION(done);
  2198. if (!udc_controller)
  2199. return -ENODEV;
  2200. udc_controller->done = &done;
  2201. fsl_udc_clk_release();
  2202. /* DR has been stopped in usb_gadget_unregister_driver() */
  2203. remove_proc_file();
  2204. /* Free allocated memory */
  2205. kfree(udc_controller->status_req->req.buf);
  2206. kfree(udc_controller->status_req);
  2207. kfree(udc_controller->eps);
  2208. dma_pool_destroy(udc_controller->td_pool);
  2209. free_irq(udc_controller->irq, udc_controller);
  2210. iounmap(dr_regs);
  2211. if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
  2212. release_mem_region(res->start, res->end - res->start + 1);
  2213. device_unregister(&udc_controller->gadget.dev);
  2214. /* free udc --wait for the release() finished */
  2215. wait_for_completion(&done);
  2216. /*
  2217. * do platform specific un-initialization:
  2218. * release iomux pins, etc.
  2219. */
  2220. if (pdata->exit)
  2221. pdata->exit(pdev);
  2222. return 0;
  2223. }
  2224. /*-----------------------------------------------------------------
  2225. * Modify Power management attributes
  2226. * Used by OTG statemachine to disable gadget temporarily
  2227. -----------------------------------------------------------------*/
  2228. static int fsl_udc_suspend(struct platform_device *pdev, pm_message_t state)
  2229. {
  2230. dr_controller_stop(udc_controller);
  2231. return 0;
  2232. }
  2233. /*-----------------------------------------------------------------
  2234. * Invoked on USB resume. May be called in_interrupt.
  2235. * Here we start the DR controller and enable the irq
  2236. *-----------------------------------------------------------------*/
  2237. static int fsl_udc_resume(struct platform_device *pdev)
  2238. {
  2239. /* Enable DR irq reg and set controller Run */
  2240. if (udc_controller->stopped) {
  2241. dr_controller_setup(udc_controller);
  2242. dr_controller_run(udc_controller);
  2243. }
  2244. udc_controller->usb_state = USB_STATE_ATTACHED;
  2245. udc_controller->ep0_state = WAIT_FOR_SETUP;
  2246. udc_controller->ep0_dir = 0;
  2247. return 0;
  2248. }
  2249. static int fsl_udc_otg_suspend(struct device *dev, pm_message_t state)
  2250. {
  2251. struct fsl_udc *udc = udc_controller;
  2252. u32 mode, usbcmd;
  2253. mode = fsl_readl(&dr_regs->usbmode) & USB_MODE_CTRL_MODE_MASK;
  2254. pr_debug("%s(): mode 0x%x stopped %d\n", __func__, mode, udc->stopped);
  2255. /*
  2256. * If the controller is already stopped, then this must be a
  2257. * PM suspend. Remember this fact, so that we will leave the
  2258. * controller stopped at PM resume time.
  2259. */
  2260. if (udc->stopped) {
  2261. pr_debug("gadget already stopped, leaving early\n");
  2262. udc->already_stopped = 1;
  2263. return 0;
  2264. }
  2265. if (mode != USB_MODE_CTRL_MODE_DEVICE) {
  2266. pr_debug("gadget not in device mode, leaving early\n");
  2267. return 0;
  2268. }
  2269. /* stop the controller */
  2270. usbcmd = fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP;
  2271. fsl_writel(usbcmd, &dr_regs->usbcmd);
  2272. udc->stopped = 1;
  2273. pr_info("USB Gadget suspended\n");
  2274. return 0;
  2275. }
  2276. static int fsl_udc_otg_resume(struct device *dev)
  2277. {
  2278. pr_debug("%s(): stopped %d already_stopped %d\n", __func__,
  2279. udc_controller->stopped, udc_controller->already_stopped);
  2280. /*
  2281. * If the controller was stopped at suspend time, then
  2282. * don't resume it now.
  2283. */
  2284. if (udc_controller->already_stopped) {
  2285. udc_controller->already_stopped = 0;
  2286. pr_debug("gadget was already stopped, leaving early\n");
  2287. return 0;
  2288. }
  2289. pr_info("USB Gadget resume\n");
  2290. return fsl_udc_resume(NULL);
  2291. }
  2292. /*-------------------------------------------------------------------------
  2293. Register entry point for the peripheral controller driver
  2294. --------------------------------------------------------------------------*/
  2295. static struct platform_driver udc_driver = {
  2296. .remove = __exit_p(fsl_udc_remove),
  2297. /* these suspend and resume are not usb suspend and resume */
  2298. .suspend = fsl_udc_suspend,
  2299. .resume = fsl_udc_resume,
  2300. .driver = {
  2301. .name = (char *)driver_name,
  2302. .owner = THIS_MODULE,
  2303. /* udc suspend/resume called from OTG driver */
  2304. .suspend = fsl_udc_otg_suspend,
  2305. .resume = fsl_udc_otg_resume,
  2306. },
  2307. };
  2308. static int __init udc_init(void)
  2309. {
  2310. printk(KERN_INFO "%s (%s)\n", driver_desc, DRIVER_VERSION);
  2311. return platform_driver_probe(&udc_driver, fsl_udc_probe);
  2312. }
  2313. module_init(udc_init);
  2314. static void __exit udc_exit(void)
  2315. {
  2316. platform_driver_unregister(&udc_driver);
  2317. printk(KERN_WARNING "%s unregistered\n", driver_desc);
  2318. }
  2319. module_exit(udc_exit);
  2320. MODULE_DESCRIPTION(DRIVER_DESC);
  2321. MODULE_AUTHOR(DRIVER_AUTHOR);
  2322. MODULE_LICENSE("GPL");
  2323. MODULE_ALIAS("platform:fsl-usb2-udc");