fsl_udc_core.c 73 KB

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