fsl_udc_core.c 71 KB

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