fsl_udc_core.c 72 KB

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