fsl_udc_core.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761
  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_udc_start(struct usb_gadget *g,
  1056. struct usb_gadget_driver *driver);
  1057. static int fsl_udc_stop(struct usb_gadget *g,
  1058. struct usb_gadget_driver *driver);
  1059. /* defined in gadget.h */
  1060. static const struct usb_gadget_ops fsl_gadget_ops = {
  1061. .get_frame = fsl_get_frame,
  1062. .wakeup = fsl_wakeup,
  1063. /* .set_selfpowered = fsl_set_selfpowered, */ /* Always selfpowered */
  1064. .vbus_session = fsl_vbus_session,
  1065. .vbus_draw = fsl_vbus_draw,
  1066. .pullup = fsl_pullup,
  1067. .udc_start = fsl_udc_start,
  1068. .udc_stop = fsl_udc_stop,
  1069. };
  1070. /* Set protocol stall on ep0, protocol stall will automatically be cleared
  1071. on new transaction */
  1072. static void ep0stall(struct fsl_udc *udc)
  1073. {
  1074. u32 tmp;
  1075. /* must set tx and rx to stall at the same time */
  1076. tmp = fsl_readl(&dr_regs->endptctrl[0]);
  1077. tmp |= EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL;
  1078. fsl_writel(tmp, &dr_regs->endptctrl[0]);
  1079. udc->ep0_state = WAIT_FOR_SETUP;
  1080. udc->ep0_dir = 0;
  1081. }
  1082. /* Prime a status phase for ep0 */
  1083. static int ep0_prime_status(struct fsl_udc *udc, int direction)
  1084. {
  1085. struct fsl_req *req = udc->status_req;
  1086. struct fsl_ep *ep;
  1087. if (direction == EP_DIR_IN)
  1088. udc->ep0_dir = USB_DIR_IN;
  1089. else
  1090. udc->ep0_dir = USB_DIR_OUT;
  1091. ep = &udc->eps[0];
  1092. if (udc->ep0_state != DATA_STATE_XMIT)
  1093. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1094. req->ep = ep;
  1095. req->req.length = 0;
  1096. req->req.status = -EINPROGRESS;
  1097. req->req.actual = 0;
  1098. req->req.complete = NULL;
  1099. req->dtd_count = 0;
  1100. req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
  1101. req->req.buf, req->req.length,
  1102. ep_is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1103. req->mapped = 1;
  1104. if (fsl_req_to_dtd(req, GFP_ATOMIC) == 0)
  1105. fsl_queue_td(ep, req);
  1106. else
  1107. return -ENOMEM;
  1108. list_add_tail(&req->queue, &ep->queue);
  1109. return 0;
  1110. }
  1111. static void udc_reset_ep_queue(struct fsl_udc *udc, u8 pipe)
  1112. {
  1113. struct fsl_ep *ep = get_ep_by_pipe(udc, pipe);
  1114. if (ep->name)
  1115. nuke(ep, -ESHUTDOWN);
  1116. }
  1117. /*
  1118. * ch9 Set address
  1119. */
  1120. static void ch9setaddress(struct fsl_udc *udc, u16 value, u16 index, u16 length)
  1121. {
  1122. /* Save the new address to device struct */
  1123. udc->device_address = (u8) value;
  1124. /* Update usb state */
  1125. udc->usb_state = USB_STATE_ADDRESS;
  1126. /* Status phase */
  1127. if (ep0_prime_status(udc, EP_DIR_IN))
  1128. ep0stall(udc);
  1129. }
  1130. /*
  1131. * ch9 Get status
  1132. */
  1133. static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
  1134. u16 index, u16 length)
  1135. {
  1136. u16 tmp = 0; /* Status, cpu endian */
  1137. struct fsl_req *req;
  1138. struct fsl_ep *ep;
  1139. ep = &udc->eps[0];
  1140. if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
  1141. /* Get device status */
  1142. tmp = 1 << USB_DEVICE_SELF_POWERED;
  1143. tmp |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
  1144. } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
  1145. /* Get interface status */
  1146. /* We don't have interface information in udc driver */
  1147. tmp = 0;
  1148. } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
  1149. /* Get endpoint status */
  1150. struct fsl_ep *target_ep;
  1151. target_ep = get_ep_by_pipe(udc, get_pipe_by_windex(index));
  1152. /* stall if endpoint doesn't exist */
  1153. if (!target_ep->ep.desc)
  1154. goto stall;
  1155. tmp = dr_ep_get_stall(ep_index(target_ep), ep_is_in(target_ep))
  1156. << USB_ENDPOINT_HALT;
  1157. }
  1158. udc->ep0_dir = USB_DIR_IN;
  1159. /* Borrow the per device status_req */
  1160. req = udc->status_req;
  1161. /* Fill in the reqest structure */
  1162. *((u16 *) req->req.buf) = cpu_to_le16(tmp);
  1163. req->ep = ep;
  1164. req->req.length = 2;
  1165. req->req.status = -EINPROGRESS;
  1166. req->req.actual = 0;
  1167. req->req.complete = NULL;
  1168. req->dtd_count = 0;
  1169. req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
  1170. req->req.buf, req->req.length,
  1171. ep_is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1172. req->mapped = 1;
  1173. /* prime the data phase */
  1174. if ((fsl_req_to_dtd(req, GFP_ATOMIC) == 0))
  1175. fsl_queue_td(ep, req);
  1176. else /* no mem */
  1177. goto stall;
  1178. list_add_tail(&req->queue, &ep->queue);
  1179. udc->ep0_state = DATA_STATE_XMIT;
  1180. if (ep0_prime_status(udc, EP_DIR_OUT))
  1181. ep0stall(udc);
  1182. return;
  1183. stall:
  1184. ep0stall(udc);
  1185. }
  1186. static void setup_received_irq(struct fsl_udc *udc,
  1187. struct usb_ctrlrequest *setup)
  1188. {
  1189. u16 wValue = le16_to_cpu(setup->wValue);
  1190. u16 wIndex = le16_to_cpu(setup->wIndex);
  1191. u16 wLength = le16_to_cpu(setup->wLength);
  1192. udc_reset_ep_queue(udc, 0);
  1193. /* We process some stardard setup requests here */
  1194. switch (setup->bRequest) {
  1195. case USB_REQ_GET_STATUS:
  1196. /* Data+Status phase from udc */
  1197. if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
  1198. != (USB_DIR_IN | USB_TYPE_STANDARD))
  1199. break;
  1200. ch9getstatus(udc, setup->bRequestType, wValue, wIndex, wLength);
  1201. return;
  1202. case USB_REQ_SET_ADDRESS:
  1203. /* Status phase from udc */
  1204. if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
  1205. | USB_RECIP_DEVICE))
  1206. break;
  1207. ch9setaddress(udc, wValue, wIndex, wLength);
  1208. return;
  1209. case USB_REQ_CLEAR_FEATURE:
  1210. case USB_REQ_SET_FEATURE:
  1211. /* Status phase from udc */
  1212. {
  1213. int rc = -EOPNOTSUPP;
  1214. u16 ptc = 0;
  1215. if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
  1216. == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
  1217. int pipe = get_pipe_by_windex(wIndex);
  1218. struct fsl_ep *ep;
  1219. if (wValue != 0 || wLength != 0 || pipe >= udc->max_ep)
  1220. break;
  1221. ep = get_ep_by_pipe(udc, pipe);
  1222. spin_unlock(&udc->lock);
  1223. rc = fsl_ep_set_halt(&ep->ep,
  1224. (setup->bRequest == USB_REQ_SET_FEATURE)
  1225. ? 1 : 0);
  1226. spin_lock(&udc->lock);
  1227. } else if ((setup->bRequestType & (USB_RECIP_MASK
  1228. | USB_TYPE_MASK)) == (USB_RECIP_DEVICE
  1229. | USB_TYPE_STANDARD)) {
  1230. /* Note: The driver has not include OTG support yet.
  1231. * This will be set when OTG support is added */
  1232. if (wValue == USB_DEVICE_TEST_MODE)
  1233. ptc = wIndex >> 8;
  1234. else if (gadget_is_otg(&udc->gadget)) {
  1235. if (setup->bRequest ==
  1236. USB_DEVICE_B_HNP_ENABLE)
  1237. udc->gadget.b_hnp_enable = 1;
  1238. else if (setup->bRequest ==
  1239. USB_DEVICE_A_HNP_SUPPORT)
  1240. udc->gadget.a_hnp_support = 1;
  1241. else if (setup->bRequest ==
  1242. USB_DEVICE_A_ALT_HNP_SUPPORT)
  1243. udc->gadget.a_alt_hnp_support = 1;
  1244. }
  1245. rc = 0;
  1246. } else
  1247. break;
  1248. if (rc == 0) {
  1249. if (ep0_prime_status(udc, EP_DIR_IN))
  1250. ep0stall(udc);
  1251. }
  1252. if (ptc) {
  1253. u32 tmp;
  1254. mdelay(10);
  1255. tmp = fsl_readl(&dr_regs->portsc1) | (ptc << 16);
  1256. fsl_writel(tmp, &dr_regs->portsc1);
  1257. printk(KERN_INFO "udc: switch to test mode %d.\n", ptc);
  1258. }
  1259. return;
  1260. }
  1261. default:
  1262. break;
  1263. }
  1264. /* Requests handled by gadget */
  1265. if (wLength) {
  1266. /* Data phase from gadget, status phase from udc */
  1267. udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
  1268. ? USB_DIR_IN : USB_DIR_OUT;
  1269. spin_unlock(&udc->lock);
  1270. if (udc->driver->setup(&udc->gadget,
  1271. &udc->local_setup_buff) < 0)
  1272. ep0stall(udc);
  1273. spin_lock(&udc->lock);
  1274. udc->ep0_state = (setup->bRequestType & USB_DIR_IN)
  1275. ? DATA_STATE_XMIT : DATA_STATE_RECV;
  1276. /*
  1277. * If the data stage is IN, send status prime immediately.
  1278. * See 2.0 Spec chapter 8.5.3.3 for detail.
  1279. */
  1280. if (udc->ep0_state == DATA_STATE_XMIT)
  1281. if (ep0_prime_status(udc, EP_DIR_OUT))
  1282. ep0stall(udc);
  1283. } else {
  1284. /* No data phase, IN status from gadget */
  1285. udc->ep0_dir = USB_DIR_IN;
  1286. spin_unlock(&udc->lock);
  1287. if (udc->driver->setup(&udc->gadget,
  1288. &udc->local_setup_buff) < 0)
  1289. ep0stall(udc);
  1290. spin_lock(&udc->lock);
  1291. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1292. }
  1293. }
  1294. /* Process request for Data or Status phase of ep0
  1295. * prime status phase if needed */
  1296. static void ep0_req_complete(struct fsl_udc *udc, struct fsl_ep *ep0,
  1297. struct fsl_req *req)
  1298. {
  1299. if (udc->usb_state == USB_STATE_ADDRESS) {
  1300. /* Set the new address */
  1301. u32 new_address = (u32) udc->device_address;
  1302. fsl_writel(new_address << USB_DEVICE_ADDRESS_BIT_POS,
  1303. &dr_regs->deviceaddr);
  1304. }
  1305. done(ep0, req, 0);
  1306. switch (udc->ep0_state) {
  1307. case DATA_STATE_XMIT:
  1308. /* already primed at setup_received_irq */
  1309. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1310. break;
  1311. case DATA_STATE_RECV:
  1312. /* send status phase */
  1313. if (ep0_prime_status(udc, EP_DIR_IN))
  1314. ep0stall(udc);
  1315. break;
  1316. case WAIT_FOR_OUT_STATUS:
  1317. udc->ep0_state = WAIT_FOR_SETUP;
  1318. break;
  1319. case WAIT_FOR_SETUP:
  1320. ERR("Unexpect ep0 packets\n");
  1321. break;
  1322. default:
  1323. ep0stall(udc);
  1324. break;
  1325. }
  1326. }
  1327. /* Tripwire mechanism to ensure a setup packet payload is extracted without
  1328. * being corrupted by another incoming setup packet */
  1329. static void tripwire_handler(struct fsl_udc *udc, u8 ep_num, u8 *buffer_ptr)
  1330. {
  1331. u32 temp;
  1332. struct ep_queue_head *qh;
  1333. struct fsl_usb2_platform_data *pdata = udc->pdata;
  1334. qh = &udc->ep_qh[ep_num * 2 + EP_DIR_OUT];
  1335. /* Clear bit in ENDPTSETUPSTAT */
  1336. temp = fsl_readl(&dr_regs->endptsetupstat);
  1337. fsl_writel(temp | (1 << ep_num), &dr_regs->endptsetupstat);
  1338. /* while a hazard exists when setup package arrives */
  1339. do {
  1340. /* Set Setup Tripwire */
  1341. temp = fsl_readl(&dr_regs->usbcmd);
  1342. fsl_writel(temp | USB_CMD_SUTW, &dr_regs->usbcmd);
  1343. /* Copy the setup packet to local buffer */
  1344. if (pdata->le_setup_buf) {
  1345. u32 *p = (u32 *)buffer_ptr;
  1346. u32 *s = (u32 *)qh->setup_buffer;
  1347. /* Convert little endian setup buffer to CPU endian */
  1348. *p++ = le32_to_cpu(*s++);
  1349. *p = le32_to_cpu(*s);
  1350. } else {
  1351. memcpy(buffer_ptr, (u8 *) qh->setup_buffer, 8);
  1352. }
  1353. } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_SUTW));
  1354. /* Clear Setup Tripwire */
  1355. temp = fsl_readl(&dr_regs->usbcmd);
  1356. fsl_writel(temp & ~USB_CMD_SUTW, &dr_regs->usbcmd);
  1357. }
  1358. /* process-ep_req(): free the completed Tds for this req */
  1359. static int process_ep_req(struct fsl_udc *udc, int pipe,
  1360. struct fsl_req *curr_req)
  1361. {
  1362. struct ep_td_struct *curr_td;
  1363. int td_complete, actual, remaining_length, j, tmp;
  1364. int status = 0;
  1365. int errors = 0;
  1366. struct ep_queue_head *curr_qh = &udc->ep_qh[pipe];
  1367. int direction = pipe % 2;
  1368. curr_td = curr_req->head;
  1369. td_complete = 0;
  1370. actual = curr_req->req.length;
  1371. for (j = 0; j < curr_req->dtd_count; j++) {
  1372. remaining_length = (hc32_to_cpu(curr_td->size_ioc_sts)
  1373. & DTD_PACKET_SIZE)
  1374. >> DTD_LENGTH_BIT_POS;
  1375. actual -= remaining_length;
  1376. errors = hc32_to_cpu(curr_td->size_ioc_sts);
  1377. if (errors & DTD_ERROR_MASK) {
  1378. if (errors & DTD_STATUS_HALTED) {
  1379. ERR("dTD error %08x QH=%d\n", errors, pipe);
  1380. /* Clear the errors and Halt condition */
  1381. tmp = hc32_to_cpu(curr_qh->size_ioc_int_sts);
  1382. tmp &= ~errors;
  1383. curr_qh->size_ioc_int_sts = cpu_to_hc32(tmp);
  1384. status = -EPIPE;
  1385. /* FIXME: continue with next queued TD? */
  1386. break;
  1387. }
  1388. if (errors & DTD_STATUS_DATA_BUFF_ERR) {
  1389. VDBG("Transfer overflow");
  1390. status = -EPROTO;
  1391. break;
  1392. } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
  1393. VDBG("ISO error");
  1394. status = -EILSEQ;
  1395. break;
  1396. } else
  1397. ERR("Unknown error has occurred (0x%x)!\n",
  1398. errors);
  1399. } else if (hc32_to_cpu(curr_td->size_ioc_sts)
  1400. & DTD_STATUS_ACTIVE) {
  1401. VDBG("Request not complete");
  1402. status = REQ_UNCOMPLETE;
  1403. return status;
  1404. } else if (remaining_length) {
  1405. if (direction) {
  1406. VDBG("Transmit dTD remaining length not zero");
  1407. status = -EPROTO;
  1408. break;
  1409. } else {
  1410. td_complete++;
  1411. break;
  1412. }
  1413. } else {
  1414. td_complete++;
  1415. VDBG("dTD transmitted successful");
  1416. }
  1417. if (j != curr_req->dtd_count - 1)
  1418. curr_td = (struct ep_td_struct *)curr_td->next_td_virt;
  1419. }
  1420. if (status)
  1421. return status;
  1422. curr_req->req.actual = actual;
  1423. return 0;
  1424. }
  1425. /* Process a DTD completion interrupt */
  1426. static void dtd_complete_irq(struct fsl_udc *udc)
  1427. {
  1428. u32 bit_pos;
  1429. int i, ep_num, direction, bit_mask, status;
  1430. struct fsl_ep *curr_ep;
  1431. struct fsl_req *curr_req, *temp_req;
  1432. /* Clear the bits in the register */
  1433. bit_pos = fsl_readl(&dr_regs->endptcomplete);
  1434. fsl_writel(bit_pos, &dr_regs->endptcomplete);
  1435. if (!bit_pos)
  1436. return;
  1437. for (i = 0; i < udc->max_ep; i++) {
  1438. ep_num = i >> 1;
  1439. direction = i % 2;
  1440. bit_mask = 1 << (ep_num + 16 * direction);
  1441. if (!(bit_pos & bit_mask))
  1442. continue;
  1443. curr_ep = get_ep_by_pipe(udc, i);
  1444. /* If the ep is configured */
  1445. if (curr_ep->name == NULL) {
  1446. WARNING("Invalid EP?");
  1447. continue;
  1448. }
  1449. /* process the req queue until an uncomplete request */
  1450. list_for_each_entry_safe(curr_req, temp_req, &curr_ep->queue,
  1451. queue) {
  1452. status = process_ep_req(udc, i, curr_req);
  1453. VDBG("status of process_ep_req= %d, ep = %d",
  1454. status, ep_num);
  1455. if (status == REQ_UNCOMPLETE)
  1456. break;
  1457. /* write back status to req */
  1458. curr_req->req.status = status;
  1459. if (ep_num == 0) {
  1460. ep0_req_complete(udc, curr_ep, curr_req);
  1461. break;
  1462. } else
  1463. done(curr_ep, curr_req, status);
  1464. }
  1465. }
  1466. }
  1467. static inline enum usb_device_speed portscx_device_speed(u32 reg)
  1468. {
  1469. switch (reg & PORTSCX_PORT_SPEED_MASK) {
  1470. case PORTSCX_PORT_SPEED_HIGH:
  1471. return USB_SPEED_HIGH;
  1472. case PORTSCX_PORT_SPEED_FULL:
  1473. return USB_SPEED_FULL;
  1474. case PORTSCX_PORT_SPEED_LOW:
  1475. return USB_SPEED_LOW;
  1476. default:
  1477. return USB_SPEED_UNKNOWN;
  1478. }
  1479. }
  1480. /* Process a port change interrupt */
  1481. static void port_change_irq(struct fsl_udc *udc)
  1482. {
  1483. if (udc->bus_reset)
  1484. udc->bus_reset = 0;
  1485. /* Bus resetting is finished */
  1486. if (!(fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET))
  1487. /* Get the speed */
  1488. udc->gadget.speed =
  1489. portscx_device_speed(fsl_readl(&dr_regs->portsc1));
  1490. /* Update USB state */
  1491. if (!udc->resume_state)
  1492. udc->usb_state = USB_STATE_DEFAULT;
  1493. }
  1494. /* Process suspend interrupt */
  1495. static void suspend_irq(struct fsl_udc *udc)
  1496. {
  1497. udc->resume_state = udc->usb_state;
  1498. udc->usb_state = USB_STATE_SUSPENDED;
  1499. /* report suspend to the driver, serial.c does not support this */
  1500. if (udc->driver->suspend)
  1501. udc->driver->suspend(&udc->gadget);
  1502. }
  1503. static void bus_resume(struct fsl_udc *udc)
  1504. {
  1505. udc->usb_state = udc->resume_state;
  1506. udc->resume_state = 0;
  1507. /* report resume to the driver, serial.c does not support this */
  1508. if (udc->driver->resume)
  1509. udc->driver->resume(&udc->gadget);
  1510. }
  1511. /* Clear up all ep queues */
  1512. static int reset_queues(struct fsl_udc *udc)
  1513. {
  1514. u8 pipe;
  1515. for (pipe = 0; pipe < udc->max_pipes; pipe++)
  1516. udc_reset_ep_queue(udc, pipe);
  1517. /* report disconnect; the driver is already quiesced */
  1518. spin_unlock(&udc->lock);
  1519. udc->driver->disconnect(&udc->gadget);
  1520. spin_lock(&udc->lock);
  1521. return 0;
  1522. }
  1523. /* Process reset interrupt */
  1524. static void reset_irq(struct fsl_udc *udc)
  1525. {
  1526. u32 temp;
  1527. unsigned long timeout;
  1528. /* Clear the device address */
  1529. temp = fsl_readl(&dr_regs->deviceaddr);
  1530. fsl_writel(temp & ~USB_DEVICE_ADDRESS_MASK, &dr_regs->deviceaddr);
  1531. udc->device_address = 0;
  1532. /* Clear usb state */
  1533. udc->resume_state = 0;
  1534. udc->ep0_dir = 0;
  1535. udc->ep0_state = WAIT_FOR_SETUP;
  1536. udc->remote_wakeup = 0; /* default to 0 on reset */
  1537. udc->gadget.b_hnp_enable = 0;
  1538. udc->gadget.a_hnp_support = 0;
  1539. udc->gadget.a_alt_hnp_support = 0;
  1540. /* Clear all the setup token semaphores */
  1541. temp = fsl_readl(&dr_regs->endptsetupstat);
  1542. fsl_writel(temp, &dr_regs->endptsetupstat);
  1543. /* Clear all the endpoint complete status bits */
  1544. temp = fsl_readl(&dr_regs->endptcomplete);
  1545. fsl_writel(temp, &dr_regs->endptcomplete);
  1546. timeout = jiffies + 100;
  1547. while (fsl_readl(&dr_regs->endpointprime)) {
  1548. /* Wait until all endptprime bits cleared */
  1549. if (time_after(jiffies, timeout)) {
  1550. ERR("Timeout for reset\n");
  1551. break;
  1552. }
  1553. cpu_relax();
  1554. }
  1555. /* Write 1s to the flush register */
  1556. fsl_writel(0xffffffff, &dr_regs->endptflush);
  1557. if (fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET) {
  1558. VDBG("Bus reset");
  1559. /* Bus is reseting */
  1560. udc->bus_reset = 1;
  1561. /* Reset all the queues, include XD, dTD, EP queue
  1562. * head and TR Queue */
  1563. reset_queues(udc);
  1564. udc->usb_state = USB_STATE_DEFAULT;
  1565. } else {
  1566. VDBG("Controller reset");
  1567. /* initialize usb hw reg except for regs for EP, not
  1568. * touch usbintr reg */
  1569. dr_controller_setup(udc);
  1570. /* Reset all internal used Queues */
  1571. reset_queues(udc);
  1572. ep0_setup(udc);
  1573. /* Enable DR IRQ reg, Set Run bit, change udc state */
  1574. dr_controller_run(udc);
  1575. udc->usb_state = USB_STATE_ATTACHED;
  1576. }
  1577. }
  1578. /*
  1579. * USB device controller interrupt handler
  1580. */
  1581. static irqreturn_t fsl_udc_irq(int irq, void *_udc)
  1582. {
  1583. struct fsl_udc *udc = _udc;
  1584. u32 irq_src;
  1585. irqreturn_t status = IRQ_NONE;
  1586. unsigned long flags;
  1587. /* Disable ISR for OTG host mode */
  1588. if (udc->stopped)
  1589. return IRQ_NONE;
  1590. spin_lock_irqsave(&udc->lock, flags);
  1591. irq_src = fsl_readl(&dr_regs->usbsts) & fsl_readl(&dr_regs->usbintr);
  1592. /* Clear notification bits */
  1593. fsl_writel(irq_src, &dr_regs->usbsts);
  1594. /* VDBG("irq_src [0x%8x]", irq_src); */
  1595. /* Need to resume? */
  1596. if (udc->usb_state == USB_STATE_SUSPENDED)
  1597. if ((fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_SUSPEND) == 0)
  1598. bus_resume(udc);
  1599. /* USB Interrupt */
  1600. if (irq_src & USB_STS_INT) {
  1601. VDBG("Packet int");
  1602. /* Setup package, we only support ep0 as control ep */
  1603. if (fsl_readl(&dr_regs->endptsetupstat) & EP_SETUP_STATUS_EP0) {
  1604. tripwire_handler(udc, 0,
  1605. (u8 *) (&udc->local_setup_buff));
  1606. setup_received_irq(udc, &udc->local_setup_buff);
  1607. status = IRQ_HANDLED;
  1608. }
  1609. /* completion of dtd */
  1610. if (fsl_readl(&dr_regs->endptcomplete)) {
  1611. dtd_complete_irq(udc);
  1612. status = IRQ_HANDLED;
  1613. }
  1614. }
  1615. /* SOF (for ISO transfer) */
  1616. if (irq_src & USB_STS_SOF) {
  1617. status = IRQ_HANDLED;
  1618. }
  1619. /* Port Change */
  1620. if (irq_src & USB_STS_PORT_CHANGE) {
  1621. port_change_irq(udc);
  1622. status = IRQ_HANDLED;
  1623. }
  1624. /* Reset Received */
  1625. if (irq_src & USB_STS_RESET) {
  1626. VDBG("reset int");
  1627. reset_irq(udc);
  1628. status = IRQ_HANDLED;
  1629. }
  1630. /* Sleep Enable (Suspend) */
  1631. if (irq_src & USB_STS_SUSPEND) {
  1632. suspend_irq(udc);
  1633. status = IRQ_HANDLED;
  1634. }
  1635. if (irq_src & (USB_STS_ERR | USB_STS_SYS_ERR)) {
  1636. VDBG("Error IRQ %x", irq_src);
  1637. }
  1638. spin_unlock_irqrestore(&udc->lock, flags);
  1639. return status;
  1640. }
  1641. /*----------------------------------------------------------------*
  1642. * Hook to gadget drivers
  1643. * Called by initialization code of gadget drivers
  1644. *----------------------------------------------------------------*/
  1645. static int fsl_udc_start(struct usb_gadget *g,
  1646. struct usb_gadget_driver *driver)
  1647. {
  1648. int retval = 0;
  1649. unsigned long flags = 0;
  1650. /* lock is needed but whether should use this lock or another */
  1651. spin_lock_irqsave(&udc_controller->lock, flags);
  1652. driver->driver.bus = NULL;
  1653. /* hook up the driver */
  1654. udc_controller->driver = driver;
  1655. udc_controller->gadget.dev.driver = &driver->driver;
  1656. spin_unlock_irqrestore(&udc_controller->lock, flags);
  1657. if (!IS_ERR_OR_NULL(udc_controller->transceiver)) {
  1658. /* Suspend the controller until OTG enable it */
  1659. udc_controller->stopped = 1;
  1660. printk(KERN_INFO "Suspend udc for OTG auto detect\n");
  1661. /* connect to bus through transceiver */
  1662. if (!IS_ERR_OR_NULL(udc_controller->transceiver)) {
  1663. retval = otg_set_peripheral(
  1664. udc_controller->transceiver->otg,
  1665. &udc_controller->gadget);
  1666. if (retval < 0) {
  1667. ERR("can't bind to transceiver\n");
  1668. driver->unbind(&udc_controller->gadget);
  1669. udc_controller->gadget.dev.driver = 0;
  1670. udc_controller->driver = 0;
  1671. return retval;
  1672. }
  1673. }
  1674. } else {
  1675. /* Enable DR IRQ reg and set USBCMD reg Run bit */
  1676. dr_controller_run(udc_controller);
  1677. udc_controller->usb_state = USB_STATE_ATTACHED;
  1678. udc_controller->ep0_state = WAIT_FOR_SETUP;
  1679. udc_controller->ep0_dir = 0;
  1680. }
  1681. return retval;
  1682. }
  1683. /* Disconnect from gadget driver */
  1684. static int fsl_udc_stop(struct usb_gadget *g,
  1685. struct usb_gadget_driver *driver)
  1686. {
  1687. struct fsl_ep *loop_ep;
  1688. unsigned long flags;
  1689. if (!IS_ERR_OR_NULL(udc_controller->transceiver))
  1690. otg_set_peripheral(udc_controller->transceiver->otg, NULL);
  1691. /* stop DR, disable intr */
  1692. dr_controller_stop(udc_controller);
  1693. /* in fact, no needed */
  1694. udc_controller->usb_state = USB_STATE_ATTACHED;
  1695. udc_controller->ep0_state = WAIT_FOR_SETUP;
  1696. udc_controller->ep0_dir = 0;
  1697. /* stand operation */
  1698. spin_lock_irqsave(&udc_controller->lock, flags);
  1699. udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
  1700. nuke(&udc_controller->eps[0], -ESHUTDOWN);
  1701. list_for_each_entry(loop_ep, &udc_controller->gadget.ep_list,
  1702. ep.ep_list)
  1703. nuke(loop_ep, -ESHUTDOWN);
  1704. spin_unlock_irqrestore(&udc_controller->lock, flags);
  1705. udc_controller->gadget.dev.driver = NULL;
  1706. udc_controller->driver = NULL;
  1707. return 0;
  1708. }
  1709. /*-------------------------------------------------------------------------
  1710. PROC File System Support
  1711. -------------------------------------------------------------------------*/
  1712. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  1713. #include <linux/seq_file.h>
  1714. static const char proc_filename[] = "driver/fsl_usb2_udc";
  1715. static int fsl_proc_read(char *page, char **start, off_t off, int count,
  1716. int *eof, void *_dev)
  1717. {
  1718. char *buf = page;
  1719. char *next = buf;
  1720. unsigned size = count;
  1721. unsigned long flags;
  1722. int t, i;
  1723. u32 tmp_reg;
  1724. struct fsl_ep *ep = NULL;
  1725. struct fsl_req *req;
  1726. struct fsl_udc *udc = udc_controller;
  1727. if (off != 0)
  1728. return 0;
  1729. spin_lock_irqsave(&udc->lock, flags);
  1730. /* ------basic driver information ---- */
  1731. t = scnprintf(next, size,
  1732. DRIVER_DESC "\n"
  1733. "%s version: %s\n"
  1734. "Gadget driver: %s\n\n",
  1735. driver_name, DRIVER_VERSION,
  1736. udc->driver ? udc->driver->driver.name : "(none)");
  1737. size -= t;
  1738. next += t;
  1739. /* ------ DR Registers ----- */
  1740. tmp_reg = fsl_readl(&dr_regs->usbcmd);
  1741. t = scnprintf(next, size,
  1742. "USBCMD reg:\n"
  1743. "SetupTW: %d\n"
  1744. "Run/Stop: %s\n\n",
  1745. (tmp_reg & USB_CMD_SUTW) ? 1 : 0,
  1746. (tmp_reg & USB_CMD_RUN_STOP) ? "Run" : "Stop");
  1747. size -= t;
  1748. next += t;
  1749. tmp_reg = fsl_readl(&dr_regs->usbsts);
  1750. t = scnprintf(next, size,
  1751. "USB Status Reg:\n"
  1752. "Dr Suspend: %d Reset Received: %d System Error: %s "
  1753. "USB Error Interrupt: %s\n\n",
  1754. (tmp_reg & USB_STS_SUSPEND) ? 1 : 0,
  1755. (tmp_reg & USB_STS_RESET) ? 1 : 0,
  1756. (tmp_reg & USB_STS_SYS_ERR) ? "Err" : "Normal",
  1757. (tmp_reg & USB_STS_ERR) ? "Err detected" : "No err");
  1758. size -= t;
  1759. next += t;
  1760. tmp_reg = fsl_readl(&dr_regs->usbintr);
  1761. t = scnprintf(next, size,
  1762. "USB Interrupt Enable Reg:\n"
  1763. "Sleep Enable: %d SOF Received Enable: %d "
  1764. "Reset Enable: %d\n"
  1765. "System Error Enable: %d "
  1766. "Port Change Dectected Enable: %d\n"
  1767. "USB Error Intr Enable: %d USB Intr Enable: %d\n\n",
  1768. (tmp_reg & USB_INTR_DEVICE_SUSPEND) ? 1 : 0,
  1769. (tmp_reg & USB_INTR_SOF_EN) ? 1 : 0,
  1770. (tmp_reg & USB_INTR_RESET_EN) ? 1 : 0,
  1771. (tmp_reg & USB_INTR_SYS_ERR_EN) ? 1 : 0,
  1772. (tmp_reg & USB_INTR_PTC_DETECT_EN) ? 1 : 0,
  1773. (tmp_reg & USB_INTR_ERR_INT_EN) ? 1 : 0,
  1774. (tmp_reg & USB_INTR_INT_EN) ? 1 : 0);
  1775. size -= t;
  1776. next += t;
  1777. tmp_reg = fsl_readl(&dr_regs->frindex);
  1778. t = scnprintf(next, size,
  1779. "USB Frame Index Reg: Frame Number is 0x%x\n\n",
  1780. (tmp_reg & USB_FRINDEX_MASKS));
  1781. size -= t;
  1782. next += t;
  1783. tmp_reg = fsl_readl(&dr_regs->deviceaddr);
  1784. t = scnprintf(next, size,
  1785. "USB Device Address Reg: Device Addr is 0x%x\n\n",
  1786. (tmp_reg & USB_DEVICE_ADDRESS_MASK));
  1787. size -= t;
  1788. next += t;
  1789. tmp_reg = fsl_readl(&dr_regs->endpointlistaddr);
  1790. t = scnprintf(next, size,
  1791. "USB Endpoint List Address Reg: "
  1792. "Device Addr is 0x%x\n\n",
  1793. (tmp_reg & USB_EP_LIST_ADDRESS_MASK));
  1794. size -= t;
  1795. next += t;
  1796. tmp_reg = fsl_readl(&dr_regs->portsc1);
  1797. t = scnprintf(next, size,
  1798. "USB Port Status&Control Reg:\n"
  1799. "Port Transceiver Type : %s Port Speed: %s\n"
  1800. "PHY Low Power Suspend: %s Port Reset: %s "
  1801. "Port Suspend Mode: %s\n"
  1802. "Over-current Change: %s "
  1803. "Port Enable/Disable Change: %s\n"
  1804. "Port Enabled/Disabled: %s "
  1805. "Current Connect Status: %s\n\n", ( {
  1806. char *s;
  1807. switch (tmp_reg & PORTSCX_PTS_FSLS) {
  1808. case PORTSCX_PTS_UTMI:
  1809. s = "UTMI"; break;
  1810. case PORTSCX_PTS_ULPI:
  1811. s = "ULPI "; break;
  1812. case PORTSCX_PTS_FSLS:
  1813. s = "FS/LS Serial"; break;
  1814. default:
  1815. s = "None"; break;
  1816. }
  1817. s;} ),
  1818. usb_speed_string(portscx_device_speed(tmp_reg)),
  1819. (tmp_reg & PORTSCX_PHY_LOW_POWER_SPD) ?
  1820. "Normal PHY mode" : "Low power mode",
  1821. (tmp_reg & PORTSCX_PORT_RESET) ? "In Reset" :
  1822. "Not in Reset",
  1823. (tmp_reg & PORTSCX_PORT_SUSPEND) ? "In " : "Not in",
  1824. (tmp_reg & PORTSCX_OVER_CURRENT_CHG) ? "Dected" :
  1825. "No",
  1826. (tmp_reg & PORTSCX_PORT_EN_DIS_CHANGE) ? "Disable" :
  1827. "Not change",
  1828. (tmp_reg & PORTSCX_PORT_ENABLE) ? "Enable" :
  1829. "Not correct",
  1830. (tmp_reg & PORTSCX_CURRENT_CONNECT_STATUS) ?
  1831. "Attached" : "Not-Att");
  1832. size -= t;
  1833. next += t;
  1834. tmp_reg = fsl_readl(&dr_regs->usbmode);
  1835. t = scnprintf(next, size,
  1836. "USB Mode Reg: Controller Mode is: %s\n\n", ( {
  1837. char *s;
  1838. switch (tmp_reg & USB_MODE_CTRL_MODE_HOST) {
  1839. case USB_MODE_CTRL_MODE_IDLE:
  1840. s = "Idle"; break;
  1841. case USB_MODE_CTRL_MODE_DEVICE:
  1842. s = "Device Controller"; break;
  1843. case USB_MODE_CTRL_MODE_HOST:
  1844. s = "Host Controller"; break;
  1845. default:
  1846. s = "None"; break;
  1847. }
  1848. s;
  1849. } ));
  1850. size -= t;
  1851. next += t;
  1852. tmp_reg = fsl_readl(&dr_regs->endptsetupstat);
  1853. t = scnprintf(next, size,
  1854. "Endpoint Setup Status Reg: SETUP on ep 0x%x\n\n",
  1855. (tmp_reg & EP_SETUP_STATUS_MASK));
  1856. size -= t;
  1857. next += t;
  1858. for (i = 0; i < udc->max_ep / 2; i++) {
  1859. tmp_reg = fsl_readl(&dr_regs->endptctrl[i]);
  1860. t = scnprintf(next, size, "EP Ctrl Reg [0x%x]: = [0x%x]\n",
  1861. i, tmp_reg);
  1862. size -= t;
  1863. next += t;
  1864. }
  1865. tmp_reg = fsl_readl(&dr_regs->endpointprime);
  1866. t = scnprintf(next, size, "EP Prime Reg = [0x%x]\n\n", tmp_reg);
  1867. size -= t;
  1868. next += t;
  1869. #ifndef CONFIG_ARCH_MXC
  1870. if (udc->pdata->have_sysif_regs) {
  1871. tmp_reg = usb_sys_regs->snoop1;
  1872. t = scnprintf(next, size, "Snoop1 Reg : = [0x%x]\n\n", tmp_reg);
  1873. size -= t;
  1874. next += t;
  1875. tmp_reg = usb_sys_regs->control;
  1876. t = scnprintf(next, size, "General Control Reg : = [0x%x]\n\n",
  1877. tmp_reg);
  1878. size -= t;
  1879. next += t;
  1880. }
  1881. #endif
  1882. /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */
  1883. ep = &udc->eps[0];
  1884. t = scnprintf(next, size, "For %s Maxpkt is 0x%x index is 0x%x\n",
  1885. ep->ep.name, ep_maxpacket(ep), ep_index(ep));
  1886. size -= t;
  1887. next += t;
  1888. if (list_empty(&ep->queue)) {
  1889. t = scnprintf(next, size, "its req queue is empty\n\n");
  1890. size -= t;
  1891. next += t;
  1892. } else {
  1893. list_for_each_entry(req, &ep->queue, queue) {
  1894. t = scnprintf(next, size,
  1895. "req %p actual 0x%x length 0x%x buf %p\n",
  1896. &req->req, req->req.actual,
  1897. req->req.length, req->req.buf);
  1898. size -= t;
  1899. next += t;
  1900. }
  1901. }
  1902. /* other gadget->eplist ep */
  1903. list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
  1904. if (ep->ep.desc) {
  1905. t = scnprintf(next, size,
  1906. "\nFor %s Maxpkt is 0x%x "
  1907. "index is 0x%x\n",
  1908. ep->ep.name, ep_maxpacket(ep),
  1909. ep_index(ep));
  1910. size -= t;
  1911. next += t;
  1912. if (list_empty(&ep->queue)) {
  1913. t = scnprintf(next, size,
  1914. "its req queue is empty\n\n");
  1915. size -= t;
  1916. next += t;
  1917. } else {
  1918. list_for_each_entry(req, &ep->queue, queue) {
  1919. t = scnprintf(next, size,
  1920. "req %p actual 0x%x length "
  1921. "0x%x buf %p\n",
  1922. &req->req, req->req.actual,
  1923. req->req.length, req->req.buf);
  1924. size -= t;
  1925. next += t;
  1926. } /* end for each_entry of ep req */
  1927. } /* end for else */
  1928. } /* end for if(ep->queue) */
  1929. } /* end (ep->desc) */
  1930. spin_unlock_irqrestore(&udc->lock, flags);
  1931. *eof = 1;
  1932. return count - size;
  1933. }
  1934. #define create_proc_file() create_proc_read_entry(proc_filename, \
  1935. 0, NULL, fsl_proc_read, NULL)
  1936. #define remove_proc_file() remove_proc_entry(proc_filename, NULL)
  1937. #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
  1938. #define create_proc_file() do {} while (0)
  1939. #define remove_proc_file() do {} while (0)
  1940. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  1941. /*-------------------------------------------------------------------------*/
  1942. /* Release udc structures */
  1943. static void fsl_udc_release(struct device *dev)
  1944. {
  1945. complete(udc_controller->done);
  1946. dma_free_coherent(dev->parent, udc_controller->ep_qh_size,
  1947. udc_controller->ep_qh, udc_controller->ep_qh_dma);
  1948. kfree(udc_controller);
  1949. }
  1950. /******************************************************************
  1951. Internal structure setup functions
  1952. *******************************************************************/
  1953. /*------------------------------------------------------------------
  1954. * init resource for globle controller
  1955. * Return the udc handle on success or NULL on failure
  1956. ------------------------------------------------------------------*/
  1957. static int __init struct_udc_setup(struct fsl_udc *udc,
  1958. struct platform_device *pdev)
  1959. {
  1960. struct fsl_usb2_platform_data *pdata;
  1961. size_t size;
  1962. pdata = pdev->dev.platform_data;
  1963. udc->phy_mode = pdata->phy_mode;
  1964. udc->eps = kzalloc(sizeof(struct fsl_ep) * udc->max_ep, GFP_KERNEL);
  1965. if (!udc->eps) {
  1966. ERR("malloc fsl_ep failed\n");
  1967. return -1;
  1968. }
  1969. /* initialized QHs, take care of alignment */
  1970. size = udc->max_ep * sizeof(struct ep_queue_head);
  1971. if (size < QH_ALIGNMENT)
  1972. size = QH_ALIGNMENT;
  1973. else if ((size % QH_ALIGNMENT) != 0) {
  1974. size += QH_ALIGNMENT + 1;
  1975. size &= ~(QH_ALIGNMENT - 1);
  1976. }
  1977. udc->ep_qh = dma_alloc_coherent(&pdev->dev, size,
  1978. &udc->ep_qh_dma, GFP_KERNEL);
  1979. if (!udc->ep_qh) {
  1980. ERR("malloc QHs for udc failed\n");
  1981. kfree(udc->eps);
  1982. return -1;
  1983. }
  1984. udc->ep_qh_size = size;
  1985. /* Initialize ep0 status request structure */
  1986. /* FIXME: fsl_alloc_request() ignores ep argument */
  1987. udc->status_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL),
  1988. struct fsl_req, req);
  1989. /* allocate a small amount of memory to get valid address */
  1990. udc->status_req->req.buf = kmalloc(8, GFP_KERNEL);
  1991. udc->resume_state = USB_STATE_NOTATTACHED;
  1992. udc->usb_state = USB_STATE_POWERED;
  1993. udc->ep0_dir = 0;
  1994. udc->remote_wakeup = 0; /* default to 0 on reset */
  1995. return 0;
  1996. }
  1997. /*----------------------------------------------------------------
  1998. * Setup the fsl_ep struct for eps
  1999. * Link fsl_ep->ep to gadget->ep_list
  2000. * ep0out is not used so do nothing here
  2001. * ep0in should be taken care
  2002. *--------------------------------------------------------------*/
  2003. static int __init struct_ep_setup(struct fsl_udc *udc, unsigned char index,
  2004. char *name, int link)
  2005. {
  2006. struct fsl_ep *ep = &udc->eps[index];
  2007. ep->udc = udc;
  2008. strcpy(ep->name, name);
  2009. ep->ep.name = ep->name;
  2010. ep->ep.ops = &fsl_ep_ops;
  2011. ep->stopped = 0;
  2012. /* for ep0: maxP defined in desc
  2013. * for other eps, maxP is set by epautoconfig() called by gadget layer
  2014. */
  2015. ep->ep.maxpacket = (unsigned short) ~0;
  2016. /* the queue lists any req for this ep */
  2017. INIT_LIST_HEAD(&ep->queue);
  2018. /* gagdet.ep_list used for ep_autoconfig so no ep0 */
  2019. if (link)
  2020. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  2021. ep->gadget = &udc->gadget;
  2022. ep->qh = &udc->ep_qh[index];
  2023. return 0;
  2024. }
  2025. /* Driver probe function
  2026. * all intialization operations implemented here except enabling usb_intr reg
  2027. * board setup should have been done in the platform code
  2028. */
  2029. static int __init fsl_udc_probe(struct platform_device *pdev)
  2030. {
  2031. struct fsl_usb2_platform_data *pdata;
  2032. struct resource *res;
  2033. int ret = -ENODEV;
  2034. unsigned int i;
  2035. u32 dccparams;
  2036. if (strcmp(pdev->name, driver_name)) {
  2037. VDBG("Wrong device");
  2038. return -ENODEV;
  2039. }
  2040. udc_controller = kzalloc(sizeof(struct fsl_udc), GFP_KERNEL);
  2041. if (udc_controller == NULL) {
  2042. ERR("malloc udc failed\n");
  2043. return -ENOMEM;
  2044. }
  2045. pdata = pdev->dev.platform_data;
  2046. udc_controller->pdata = pdata;
  2047. spin_lock_init(&udc_controller->lock);
  2048. udc_controller->stopped = 1;
  2049. #ifdef CONFIG_USB_OTG
  2050. if (pdata->operating_mode == FSL_USB2_DR_OTG) {
  2051. udc_controller->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
  2052. if (IS_ERR_OR_NULL(udc_controller->transceiver)) {
  2053. ERR("Can't find OTG driver!\n");
  2054. ret = -ENODEV;
  2055. goto err_kfree;
  2056. }
  2057. }
  2058. #endif
  2059. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2060. if (!res) {
  2061. ret = -ENXIO;
  2062. goto err_kfree;
  2063. }
  2064. if (pdata->operating_mode == FSL_USB2_DR_DEVICE) {
  2065. if (!request_mem_region(res->start, resource_size(res),
  2066. driver_name)) {
  2067. ERR("request mem region for %s failed\n", pdev->name);
  2068. ret = -EBUSY;
  2069. goto err_kfree;
  2070. }
  2071. }
  2072. dr_regs = ioremap(res->start, resource_size(res));
  2073. if (!dr_regs) {
  2074. ret = -ENOMEM;
  2075. goto err_release_mem_region;
  2076. }
  2077. pdata->regs = (void *)dr_regs;
  2078. /*
  2079. * do platform specific init: check the clock, grab/config pins, etc.
  2080. */
  2081. if (pdata->init && pdata->init(pdev)) {
  2082. ret = -ENODEV;
  2083. goto err_iounmap_noclk;
  2084. }
  2085. /* Set accessors only after pdata->init() ! */
  2086. fsl_set_accessors(pdata);
  2087. #ifndef CONFIG_ARCH_MXC
  2088. if (pdata->have_sysif_regs)
  2089. usb_sys_regs = (void *)dr_regs + USB_DR_SYS_OFFSET;
  2090. #endif
  2091. /* Initialize USB clocks */
  2092. ret = fsl_udc_clk_init(pdev);
  2093. if (ret < 0)
  2094. goto err_iounmap_noclk;
  2095. /* Read Device Controller Capability Parameters register */
  2096. dccparams = fsl_readl(&dr_regs->dccparams);
  2097. if (!(dccparams & DCCPARAMS_DC)) {
  2098. ERR("This SOC doesn't support device role\n");
  2099. ret = -ENODEV;
  2100. goto err_iounmap;
  2101. }
  2102. /* Get max device endpoints */
  2103. /* DEN is bidirectional ep number, max_ep doubles the number */
  2104. udc_controller->max_ep = (dccparams & DCCPARAMS_DEN_MASK) * 2;
  2105. udc_controller->irq = platform_get_irq(pdev, 0);
  2106. if (!udc_controller->irq) {
  2107. ret = -ENODEV;
  2108. goto err_iounmap;
  2109. }
  2110. ret = request_irq(udc_controller->irq, fsl_udc_irq, IRQF_SHARED,
  2111. driver_name, udc_controller);
  2112. if (ret != 0) {
  2113. ERR("cannot request irq %d err %d\n",
  2114. udc_controller->irq, ret);
  2115. goto err_iounmap;
  2116. }
  2117. /* Initialize the udc structure including QH member and other member */
  2118. if (struct_udc_setup(udc_controller, pdev)) {
  2119. ERR("Can't initialize udc data structure\n");
  2120. ret = -ENOMEM;
  2121. goto err_free_irq;
  2122. }
  2123. if (IS_ERR_OR_NULL(udc_controller->transceiver)) {
  2124. /* initialize usb hw reg except for regs for EP,
  2125. * leave usbintr reg untouched */
  2126. dr_controller_setup(udc_controller);
  2127. }
  2128. fsl_udc_clk_finalize(pdev);
  2129. /* Setup gadget structure */
  2130. udc_controller->gadget.ops = &fsl_gadget_ops;
  2131. udc_controller->gadget.max_speed = USB_SPEED_HIGH;
  2132. udc_controller->gadget.ep0 = &udc_controller->eps[0].ep;
  2133. INIT_LIST_HEAD(&udc_controller->gadget.ep_list);
  2134. udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
  2135. udc_controller->gadget.name = driver_name;
  2136. /* Setup gadget.dev and register with kernel */
  2137. dev_set_name(&udc_controller->gadget.dev, "gadget");
  2138. udc_controller->gadget.dev.release = fsl_udc_release;
  2139. udc_controller->gadget.dev.parent = &pdev->dev;
  2140. udc_controller->gadget.dev.of_node = pdev->dev.of_node;
  2141. ret = device_register(&udc_controller->gadget.dev);
  2142. if (ret < 0)
  2143. goto err_free_irq;
  2144. if (!IS_ERR_OR_NULL(udc_controller->transceiver))
  2145. udc_controller->gadget.is_otg = 1;
  2146. /* setup QH and epctrl for ep0 */
  2147. ep0_setup(udc_controller);
  2148. /* setup udc->eps[] for ep0 */
  2149. struct_ep_setup(udc_controller, 0, "ep0", 0);
  2150. /* for ep0: the desc defined here;
  2151. * for other eps, gadget layer called ep_enable with defined desc
  2152. */
  2153. udc_controller->eps[0].ep.desc = &fsl_ep0_desc;
  2154. udc_controller->eps[0].ep.maxpacket = USB_MAX_CTRL_PAYLOAD;
  2155. /* setup the udc->eps[] for non-control endpoints and link
  2156. * to gadget.ep_list */
  2157. for (i = 1; i < (int)(udc_controller->max_ep / 2); i++) {
  2158. char name[14];
  2159. sprintf(name, "ep%dout", i);
  2160. struct_ep_setup(udc_controller, i * 2, name, 1);
  2161. sprintf(name, "ep%din", i);
  2162. struct_ep_setup(udc_controller, i * 2 + 1, name, 1);
  2163. }
  2164. /* use dma_pool for TD management */
  2165. udc_controller->td_pool = dma_pool_create("udc_td", &pdev->dev,
  2166. sizeof(struct ep_td_struct),
  2167. DTD_ALIGNMENT, UDC_DMA_BOUNDARY);
  2168. if (udc_controller->td_pool == NULL) {
  2169. ret = -ENOMEM;
  2170. goto err_unregister;
  2171. }
  2172. ret = usb_add_gadget_udc(&pdev->dev, &udc_controller->gadget);
  2173. if (ret)
  2174. goto err_del_udc;
  2175. create_proc_file();
  2176. return 0;
  2177. err_del_udc:
  2178. dma_pool_destroy(udc_controller->td_pool);
  2179. err_unregister:
  2180. device_unregister(&udc_controller->gadget.dev);
  2181. err_free_irq:
  2182. free_irq(udc_controller->irq, udc_controller);
  2183. err_iounmap:
  2184. if (pdata->exit)
  2185. pdata->exit(pdev);
  2186. fsl_udc_clk_release();
  2187. err_iounmap_noclk:
  2188. iounmap(dr_regs);
  2189. err_release_mem_region:
  2190. if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
  2191. release_mem_region(res->start, resource_size(res));
  2192. err_kfree:
  2193. kfree(udc_controller);
  2194. udc_controller = NULL;
  2195. return ret;
  2196. }
  2197. /* Driver removal function
  2198. * Free resources and finish pending transactions
  2199. */
  2200. static int __exit fsl_udc_remove(struct platform_device *pdev)
  2201. {
  2202. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2203. struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
  2204. DECLARE_COMPLETION(done);
  2205. if (!udc_controller)
  2206. return -ENODEV;
  2207. usb_del_gadget_udc(&udc_controller->gadget);
  2208. udc_controller->done = &done;
  2209. fsl_udc_clk_release();
  2210. /* DR has been stopped in usb_gadget_unregister_driver() */
  2211. remove_proc_file();
  2212. /* Free allocated memory */
  2213. kfree(udc_controller->status_req->req.buf);
  2214. kfree(udc_controller->status_req);
  2215. kfree(udc_controller->eps);
  2216. dma_pool_destroy(udc_controller->td_pool);
  2217. free_irq(udc_controller->irq, udc_controller);
  2218. iounmap(dr_regs);
  2219. if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
  2220. release_mem_region(res->start, resource_size(res));
  2221. device_unregister(&udc_controller->gadget.dev);
  2222. /* free udc --wait for the release() finished */
  2223. wait_for_completion(&done);
  2224. /*
  2225. * do platform specific un-initialization:
  2226. * release iomux pins, etc.
  2227. */
  2228. if (pdata->exit)
  2229. pdata->exit(pdev);
  2230. return 0;
  2231. }
  2232. /*-----------------------------------------------------------------
  2233. * Modify Power management attributes
  2234. * Used by OTG statemachine to disable gadget temporarily
  2235. -----------------------------------------------------------------*/
  2236. static int fsl_udc_suspend(struct platform_device *pdev, pm_message_t state)
  2237. {
  2238. dr_controller_stop(udc_controller);
  2239. return 0;
  2240. }
  2241. /*-----------------------------------------------------------------
  2242. * Invoked on USB resume. May be called in_interrupt.
  2243. * Here we start the DR controller and enable the irq
  2244. *-----------------------------------------------------------------*/
  2245. static int fsl_udc_resume(struct platform_device *pdev)
  2246. {
  2247. /* Enable DR irq reg and set controller Run */
  2248. if (udc_controller->stopped) {
  2249. dr_controller_setup(udc_controller);
  2250. dr_controller_run(udc_controller);
  2251. }
  2252. udc_controller->usb_state = USB_STATE_ATTACHED;
  2253. udc_controller->ep0_state = WAIT_FOR_SETUP;
  2254. udc_controller->ep0_dir = 0;
  2255. return 0;
  2256. }
  2257. static int fsl_udc_otg_suspend(struct device *dev, pm_message_t state)
  2258. {
  2259. struct fsl_udc *udc = udc_controller;
  2260. u32 mode, usbcmd;
  2261. mode = fsl_readl(&dr_regs->usbmode) & USB_MODE_CTRL_MODE_MASK;
  2262. pr_debug("%s(): mode 0x%x stopped %d\n", __func__, mode, udc->stopped);
  2263. /*
  2264. * If the controller is already stopped, then this must be a
  2265. * PM suspend. Remember this fact, so that we will leave the
  2266. * controller stopped at PM resume time.
  2267. */
  2268. if (udc->stopped) {
  2269. pr_debug("gadget already stopped, leaving early\n");
  2270. udc->already_stopped = 1;
  2271. return 0;
  2272. }
  2273. if (mode != USB_MODE_CTRL_MODE_DEVICE) {
  2274. pr_debug("gadget not in device mode, leaving early\n");
  2275. return 0;
  2276. }
  2277. /* stop the controller */
  2278. usbcmd = fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP;
  2279. fsl_writel(usbcmd, &dr_regs->usbcmd);
  2280. udc->stopped = 1;
  2281. pr_info("USB Gadget suspended\n");
  2282. return 0;
  2283. }
  2284. static int fsl_udc_otg_resume(struct device *dev)
  2285. {
  2286. pr_debug("%s(): stopped %d already_stopped %d\n", __func__,
  2287. udc_controller->stopped, udc_controller->already_stopped);
  2288. /*
  2289. * If the controller was stopped at suspend time, then
  2290. * don't resume it now.
  2291. */
  2292. if (udc_controller->already_stopped) {
  2293. udc_controller->already_stopped = 0;
  2294. pr_debug("gadget was already stopped, leaving early\n");
  2295. return 0;
  2296. }
  2297. pr_info("USB Gadget resume\n");
  2298. return fsl_udc_resume(NULL);
  2299. }
  2300. /*-------------------------------------------------------------------------
  2301. Register entry point for the peripheral controller driver
  2302. --------------------------------------------------------------------------*/
  2303. static struct platform_driver udc_driver = {
  2304. .remove = __exit_p(fsl_udc_remove),
  2305. /* these suspend and resume are not usb suspend and resume */
  2306. .suspend = fsl_udc_suspend,
  2307. .resume = fsl_udc_resume,
  2308. .driver = {
  2309. .name = (char *)driver_name,
  2310. .owner = THIS_MODULE,
  2311. /* udc suspend/resume called from OTG driver */
  2312. .suspend = fsl_udc_otg_suspend,
  2313. .resume = fsl_udc_otg_resume,
  2314. },
  2315. };
  2316. static int __init udc_init(void)
  2317. {
  2318. printk(KERN_INFO "%s (%s)\n", driver_desc, DRIVER_VERSION);
  2319. return platform_driver_probe(&udc_driver, fsl_udc_probe);
  2320. }
  2321. module_init(udc_init);
  2322. static void __exit udc_exit(void)
  2323. {
  2324. platform_driver_unregister(&udc_driver);
  2325. printk(KERN_WARNING "%s unregistered\n", driver_desc);
  2326. }
  2327. module_exit(udc_exit);
  2328. MODULE_DESCRIPTION(DRIVER_DESC);
  2329. MODULE_AUTHOR(DRIVER_AUTHOR);
  2330. MODULE_LICENSE("GPL");
  2331. MODULE_ALIAS("platform:fsl-usb2-udc");