fsl_udc_core.c 74 KB

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