fsl_udc_core.c 73 KB

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