arcotg_udc.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020
  1. /*
  2. * Copyright 2004-2013 Freescale Semiconductor, Inc. All Rights Reserved.
  3. */
  4. /*
  5. * The code contained herein is licensed under the GNU General Public
  6. * License. You may obtain a copy of the GNU General Public License
  7. * Version 2 or later at the following locations:
  8. *
  9. * http://www.opensource.org/licenses/gpl-license.html
  10. * http://www.gnu.org/copyleft/gpl.html
  11. */
  12. #undef VERBOSE
  13. #include <common.h>
  14. #include <asm/errno.h>
  15. #include <linux/list.h>
  16. #include <malloc.h>
  17. #include <linux/usb/ch9.h>
  18. #include <linux/usb/gadget.h>
  19. #include <asm/byteorder.h>
  20. #include <asm/unaligned.h>
  21. #include <asm/io.h>
  22. #include <asm/mach-types.h>
  23. #include <usb/lin_gadget_compat.h>
  24. #include <usb/arcotg_udc.h>
  25. #include <usb/fsl_devices.h>
  26. typedef int irqreturn_t;
  27. typedef int pm_message_t;
  28. #define IRQ_NONE 0
  29. #define GFP_ATOMIC 0
  30. #define __init
  31. #define __exit
  32. #define __devinit
  33. #define pr_warning printf
  34. #define pr_debug(args...)
  35. #define DRIVER_DESC "ARC USBOTG Device Controller driver"
  36. #define DRIVER_AUTHOR "Freescale Semiconductor"
  37. #define DRIVER_VERSION "1 August 2005"
  38. #ifdef CONFIG_PPC_MPC512x
  39. #define BIG_ENDIAN_DESC
  40. #endif
  41. #ifdef BIG_ENDIAN_DESC
  42. #define cpu_to_hc32(x) (x)
  43. #define hc32_to_cpu(x) (x)
  44. #else
  45. #define cpu_to_hc32(x) cpu_to_le32((x))
  46. #define hc32_to_cpu(x) le32_to_cpu((x))
  47. #endif
  48. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  49. DEFINE_MUTEX(udc_resume_mutex);
  50. extern void usb_debounce_id_vbus(void);
  51. static const char driver_name[] = "fsl-usb2-udc";
  52. static const char driver_desc[] = DRIVER_DESC;
  53. volatile static struct usb_dr_device *dr_regs;
  54. volatile static struct usb_sys_interface *usb_sys_regs;
  55. /* it is initialized in probe() */
  56. static struct fsl_udc *udc_controller;
  57. #ifdef POSTPONE_FREE_LAST_DTD
  58. static struct ep_td_struct *last_free_td;
  59. #endif
  60. static const struct usb_endpoint_descriptor
  61. fsl_ep0_desc = {
  62. .bLength = USB_DT_ENDPOINT_SIZE,
  63. .bDescriptorType = USB_DT_ENDPOINT,
  64. .bEndpointAddress = 0,
  65. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  66. .wMaxPacketSize = USB_MAX_CTRL_PAYLOAD,
  67. };
  68. static const size_t g_iram_size = IRAM_TD_PPH_SIZE;
  69. static unsigned long g_iram_base;
  70. static __iomem void *g_iram_addr;
  71. typedef int (*dev_sus)(struct device *dev, pm_message_t state);
  72. typedef int (*dev_res) (struct device *dev);
  73. static int udc_suspend(struct fsl_udc *udc);
  74. static int fsl_udc_suspend(struct fsl_usb2_platform_data *pdata, pm_message_t state);
  75. static int fsl_udc_resume(void);
  76. static void fsl_ep_fifo_flush(struct usb_ep *_ep);
  77. static void gadget_wait_line_to_se0(void);
  78. extern void fsl_platform_set_test_mode(struct fsl_usb2_platform_data *pdata, enum usb_test_mode mode);
  79. #ifdef CONFIG_WORKAROUND_ARCUSB_REG_RW
  80. static void safe_writel(u32 val32, volatile u32 *addr)
  81. {
  82. __asm__ ("swp %0, %0, [%1]" : : "r"(val32), "r"(addr));
  83. }
  84. #endif
  85. #ifdef CONFIG_PPC32
  86. #define fsl_readl(addr) in_le32((addr))
  87. #define fsl_writel(addr, val32) out_le32((val32), (addr))
  88. #elif defined (CONFIG_WORKAROUND_ARCUSB_REG_RW)
  89. #define fsl_readl(addr) readl((addr))
  90. #define fsl_writel(val32, addr) safe_writel(val32, addr)
  91. #else
  92. #define fsl_readl(addr) readl((addr))
  93. #define fsl_writel(addr, val32) writel((addr), (val32))
  94. #endif
  95. /********************************************************************
  96. * Internal Used Function
  97. ********************************************************************/
  98. #ifdef DUMP_QUEUES
  99. static void dump_ep_queue(struct fsl_ep *ep)
  100. {
  101. int ep_index;
  102. struct fsl_req *req;
  103. struct ep_td_struct *dtd;
  104. if (list_empty(&ep->queue)) {
  105. pr_debug("udc: empty\n");
  106. return;
  107. }
  108. ep_index = ep_index(ep) * 2 + ep_is_in(ep);
  109. pr_debug("udc: ep=0x%p index=%d\n", ep, ep_index);
  110. list_for_each_entry(req, &ep->queue, queue) {
  111. pr_debug("udc: req=0x%p dTD count=%d\n", req, req->dtd_count);
  112. pr_debug("udc: dTD head=0x%p tail=0x%p\n", req->head,
  113. req->tail);
  114. dtd = req->head;
  115. while (dtd) {
  116. if (le32_to_cpu(dtd->next_td_ptr) & DTD_NEXT_TERMINATE)
  117. break; /* end of dTD list */
  118. dtd = dtd->next_td_virt;
  119. }
  120. }
  121. }
  122. #else
  123. static inline void dump_ep_queue(struct fsl_ep *ep)
  124. {
  125. }
  126. #endif
  127. #if (defined CONFIG_ARCH_MX35 || defined CONFIG_ARCH_MX25)
  128. /*
  129. * The Phy at MX35 and MX25 have bugs, it must disable, and re-eable phy
  130. * if the phy clock is disabled before
  131. */
  132. void reset_phy(void)
  133. {
  134. u32 phyctrl;
  135. phyctrl = fsl_readl(&dr_regs->phyctrl1);
  136. phyctrl &= ~PHY_CTRL0_USBEN;
  137. fsl_writel(phyctrl, &dr_regs->phyctrl1);
  138. phyctrl = fsl_readl(&dr_regs->phyctrl1);
  139. phyctrl |= PHY_CTRL0_USBEN;
  140. fsl_writel(phyctrl, &dr_regs->phyctrl1);
  141. }
  142. #else
  143. void reset_phy(void){; }
  144. #endif
  145. /* Needed for i2c/serial transceivers */
  146. static inline void
  147. fsl_platform_set_device_mode(struct fsl_usb2_platform_data *pdata)
  148. {
  149. if (pdata->xcvr_ops && pdata->xcvr_ops->set_device)
  150. pdata->xcvr_ops->set_device();
  151. }
  152. static inline void
  153. fsl_platform_pullup_enable(struct fsl_usb2_platform_data *pdata)
  154. {
  155. if (pdata->xcvr_ops && pdata->xcvr_ops->pullup)
  156. pdata->xcvr_ops->pullup(1);
  157. }
  158. static inline void
  159. fsl_platform_pullup_disable(struct fsl_usb2_platform_data *pdata)
  160. {
  161. if (pdata->xcvr_ops && pdata->xcvr_ops->pullup)
  162. pdata->xcvr_ops->pullup(0);
  163. }
  164. static void *malloc_dma_buffer(u32 *dmaaddr, int size, int align)
  165. {
  166. void *mem;
  167. mem = memalign(align, size);
  168. *dmaaddr = mem;
  169. return mem;
  170. int msize = (size + align - 1);
  171. u32 vir, vir_align;
  172. vir = (u32)malloc(msize);
  173. #ifdef CONFIG_ARCH_MMU
  174. vir = ioremap_nocache(iomem_to_phys(vir), msize);
  175. #endif
  176. memset((void *)vir, 0, msize);
  177. vir_align = (vir + align - 1) & (~(align - 1));
  178. #ifdef CONFIG_ARCH_MMU
  179. *dmaaddr = (u32)iomem_to_phys(vir_align);
  180. #else
  181. *dmaaddr = vir_align;
  182. #endif
  183. DBG("vir addr %x, dma addr %x\n", vir_align, *dmaaddr);
  184. return (void *)vir_align;
  185. }
  186. /*-----------------------------------------------------------------
  187. * done() - retire a request; caller blocked irqs
  188. * @status : request status to be set, only works when
  189. * request is still in progress.
  190. *--------------------------------------------------------------*/
  191. static void done(struct fsl_ep *ep, struct fsl_req *req, int status)
  192. {
  193. struct fsl_udc *udc = NULL;
  194. unsigned char stopped = ep->stopped;
  195. struct ep_td_struct *curr_td, *next_td;
  196. int j;
  197. udc = (struct fsl_udc *)ep->udc;
  198. /* Removed the req from fsl_ep->queue */
  199. list_del_init(&req->queue);
  200. /* req.status should be set as -EINPROGRESS in ep_queue() */
  201. if (req->req.status == -EINPROGRESS)
  202. req->req.status = status;
  203. else
  204. status = req->req.status;
  205. /* Free dtd for the request */
  206. next_td = req->head;
  207. for (j = 0; j < req->dtd_count; j++) {
  208. curr_td = next_td;
  209. if (j != req->dtd_count - 1) {
  210. next_td = curr_td->next_td_virt;
  211. #ifdef POSTPONE_FREE_LAST_DTD
  212. free(curr_td);
  213. } else {
  214. if (last_free_td != NULL)
  215. free(last_free_td);
  216. last_free_td = curr_td;
  217. }
  218. #else
  219. }
  220. free(curr_td);
  221. #endif
  222. }
  223. if (USE_MSC_WR(req->req.length)) {
  224. memmove(req->req.buf, req->req.buf + 1, MSC_BULK_CB_WRAP_LEN);
  225. }
  226. if (req->mapped) {
  227. req->req.dma = DMA_ADDR_INVALID;
  228. req->mapped = 0;
  229. }
  230. if (status && (status != -ESHUTDOWN))
  231. VDBG("complete %s req %p stat %d len %u/%u",
  232. ep->ep.name, &req->req, status,
  233. req->req.actual, req->req.length);
  234. ep->stopped = 1;
  235. spin_unlock(&ep->udc->lock);
  236. /* complete() is from gadget layer,
  237. * eg fsg->bulk_in_complete() */
  238. if (req->req.complete)
  239. req->req.complete(&ep->ep, &req->req);
  240. spin_lock(&ep->udc->lock);
  241. ep->stopped = stopped;
  242. }
  243. /*-----------------------------------------------------------------
  244. * nuke(): delete all requests related to this ep
  245. * called with spinlock held
  246. *--------------------------------------------------------------*/
  247. static void nuke(struct fsl_ep *ep, int status)
  248. {
  249. ep->stopped = 1;
  250. /*
  251. * At udc stop mode, the clock is already off
  252. * So flush fifo, should be done at clock on mode.
  253. */
  254. if (!ep->udc->stopped)
  255. fsl_ep_fifo_flush(&ep->ep);
  256. /* Whether this eq has request linked */
  257. while (!list_empty(&ep->queue)) {
  258. struct fsl_req *req = NULL;
  259. req = list_entry(ep->queue.next, struct fsl_req, queue);
  260. done(ep, req, status);
  261. }
  262. dump_ep_queue(ep);
  263. }
  264. /*------------------------------------------------------------------
  265. Internal Hardware related function
  266. ------------------------------------------------------------------*/
  267. static void dr_discharge_line(struct fsl_usb2_platform_data *pdata, bool enable)
  268. {
  269. /* enable/disable pulldown dp and dm */
  270. if (pdata->dr_discharge_line) {
  271. pdata->dr_discharge_line(enable);
  272. /*
  273. * Some platforms, like mx6x, are very slow change line state
  274. * to SE0 for dp and dm.
  275. * So, we need to discharge dp and dm, otherwise there is a wakeup interrupt
  276. * after we enable the wakeup function.
  277. */
  278. if (enable)
  279. gadget_wait_line_to_se0();
  280. }
  281. }
  282. static inline void
  283. dr_wake_up_enable(struct fsl_udc *udc, bool enable)
  284. {
  285. struct fsl_usb2_platform_data *pdata;
  286. pdata = udc->pdata;
  287. if (pdata && pdata->wake_up_enable)
  288. pdata->wake_up_enable(pdata, enable);
  289. }
  290. static inline void dr_clk_gate(bool on)
  291. {
  292. struct fsl_usb2_platform_data *pdata = udc_controller->pdata;
  293. if (!pdata || !pdata->usb_clock_for_pm)
  294. return;
  295. pdata->usb_clock_for_pm(on);
  296. if (on)
  297. reset_phy();
  298. }
  299. static void dr_phy_low_power_mode(struct fsl_udc *udc, bool enable)
  300. {
  301. struct fsl_usb2_platform_data *pdata = udc->pdata;
  302. u32 portsc;
  303. unsigned long flags;
  304. spin_lock_irqsave(&pdata->lock, flags);
  305. if (pdata && pdata->phy_lowpower_suspend) {
  306. pdata->phy_lowpower_suspend(pdata, enable);
  307. } else {
  308. if (enable) {
  309. portsc = fsl_readl(&dr_regs->portsc1);
  310. portsc |= PORTSCX_PHY_LOW_POWER_SPD;
  311. fsl_writel(portsc, &dr_regs->portsc1);
  312. } else {
  313. portsc = fsl_readl(&dr_regs->portsc1);
  314. portsc &= ~PORTSCX_PHY_LOW_POWER_SPD;
  315. fsl_writel(portsc, &dr_regs->portsc1);
  316. }
  317. }
  318. pdata->lowpower = enable;
  319. spin_unlock_irqrestore(&pdata->lock, flags);
  320. }
  321. static int dr_controller_setup(struct fsl_udc *udc)
  322. {
  323. unsigned int tmp = 0, portctrl = 0;
  324. unsigned int ctrl = 0;
  325. unsigned long timeout;
  326. struct fsl_usb2_platform_data *pdata;
  327. #define FSL_UDC_RESET_TIMEOUT 1000 /* msec */
  328. /* before here, make sure dr_regs has been initialized */
  329. if (!udc)
  330. return -EINVAL;
  331. pdata = udc->pdata;
  332. /* Stop and reset the usb controller */
  333. tmp = fsl_readl(&dr_regs->usbcmd);
  334. tmp &= ~USB_CMD_RUN_STOP;
  335. fsl_writel(tmp, &dr_regs->usbcmd);
  336. tmp = fsl_readl(&dr_regs->usbcmd);
  337. tmp |= USB_CMD_CTRL_RESET;
  338. fsl_writel(tmp, &dr_regs->usbcmd);
  339. /* Wait for reset to complete */
  340. timeout = get_timer(0) + FSL_UDC_RESET_TIMEOUT;
  341. while (fsl_readl(&dr_regs->usbcmd) & USB_CMD_CTRL_RESET) {
  342. if (get_timer(0) > timeout) {
  343. ERR("udc reset timeout! \n");
  344. return -ETIMEDOUT;
  345. }
  346. }
  347. /* Set the controller as device mode */
  348. tmp = fsl_readl(&dr_regs->usbmode);
  349. tmp &= ~USB_MODE_CTRL_MODE_MASK; /* clear mode bits */
  350. tmp |= USB_MODE_CTRL_MODE_DEVICE;
  351. /* Disable Setup Lockout */
  352. tmp |= USB_MODE_SETUP_LOCK_OFF;
  353. if (pdata->es)
  354. tmp |= USB_MODE_ES;
  355. fsl_writel(tmp, &dr_regs->usbmode);
  356. /* wait dp to 0v */
  357. dr_discharge_line(pdata, true);
  358. fsl_platform_set_device_mode(pdata);
  359. /* Clear the setup status */
  360. fsl_writel(0xffffffff, &dr_regs->usbsts);
  361. tmp = udc->ep_qh_dma;
  362. tmp &= USB_EP_LIST_ADDRESS_MASK;
  363. fsl_writel(tmp, &dr_regs->endpointlistaddr);
  364. VDBG("vir[qh_base] is %p phy[qh_base] is 0x%8x reg is 0x%8x",
  365. (int)udc->ep_qh, (int)tmp,
  366. fsl_readl(&dr_regs->endpointlistaddr));
  367. /* Config PHY interface */
  368. portctrl = fsl_readl(&dr_regs->portsc1);
  369. portctrl &= ~(PORTSCX_PHY_TYPE_SEL | PORTSCX_PORT_WIDTH);
  370. switch (udc->phy_mode) {
  371. case FSL_USB2_PHY_ULPI:
  372. portctrl |= PORTSCX_PTS_ULPI;
  373. break;
  374. case FSL_USB2_PHY_UTMI_WIDE:
  375. portctrl |= PORTSCX_PTW_16BIT;
  376. /* fall through */
  377. case FSL_USB2_PHY_UTMI:
  378. portctrl |= PORTSCX_PTS_UTMI;
  379. break;
  380. case FSL_USB2_PHY_SERIAL:
  381. portctrl |= PORTSCX_PTS_FSLS;
  382. break;
  383. default:
  384. return -EINVAL;
  385. }
  386. fsl_writel(portctrl, &dr_regs->portsc1);
  387. if (pdata->change_ahb_burst) {
  388. /* if usb should not work in default INCRx mode */
  389. tmp = fsl_readl(&dr_regs->sbuscfg);
  390. tmp = (tmp & ~0x07) | pdata->ahb_burst_mode;
  391. fsl_writel(tmp, &dr_regs->sbuscfg);
  392. }
  393. if (pdata->have_sysif_regs) {
  394. /* Config control enable i/o output, cpu endian register */
  395. ctrl = __raw_readl(&usb_sys_regs->control);
  396. ctrl |= USB_CTRL_IOENB;
  397. __raw_writel(ctrl, &usb_sys_regs->control);
  398. }
  399. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  400. /* Turn on cache snooping hardware, since some PowerPC platforms
  401. * wholly rely on hardware to deal with cache coherent. */
  402. if (pdata->have_sysif_regs) {
  403. /* Setup Snooping for all the 4GB space */
  404. tmp = SNOOP_SIZE_2GB; /* starts from 0x0, size 2G */
  405. __raw_writel(tmp, &usb_sys_regs->snoop1);
  406. tmp |= 0x80000000; /* starts from 0x8000000, size 2G */
  407. __raw_writel(tmp, &usb_sys_regs->snoop2);
  408. }
  409. #endif
  410. return 0;
  411. }
  412. /* Enable DR irq and set controller to run state */
  413. static void dr_controller_run(struct fsl_udc *udc)
  414. {
  415. u32 temp;
  416. udc_controller->usb_state = USB_STATE_ATTACHED;
  417. udc_controller->ep0_dir = 0;
  418. fsl_platform_pullup_enable(udc->pdata);
  419. /* Enable DR irq reg */
  420. temp = USB_INTR_INT_EN | USB_INTR_ERR_INT_EN
  421. | USB_INTR_PTC_DETECT_EN | USB_INTR_RESET_EN
  422. | USB_INTR_DEVICE_SUSPEND | USB_INTR_SYS_ERR_EN;
  423. fsl_writel(temp, &dr_regs->usbintr);
  424. /* enable BSV irq */
  425. temp = fsl_readl(&dr_regs->otgsc);
  426. temp |= OTGSC_B_SESSION_VALID_IRQ_EN;
  427. fsl_writel(temp, &dr_regs->otgsc);
  428. /* If vbus not on and used low power mode */
  429. if (!(temp & OTGSC_B_SESSION_VALID)) {
  430. /* Set stopped before low power mode */
  431. udc->vbus_active = false;
  432. udc->stopped = 1;
  433. /* enable wake up */
  434. dr_wake_up_enable(udc, true);
  435. /* enter lower power mode */
  436. dr_phy_low_power_mode(udc, true);
  437. printk(KERN_DEBUG "%s: udc enter low power mode \n", __func__);
  438. } else {
  439. #ifdef CONFIG_ARCH_MX37
  440. /*
  441. add some delay for USB timing issue. USB may be
  442. recognize as FS device
  443. during USB gadget remote wake up function
  444. */
  445. mdelay(100);
  446. #endif
  447. /* Clear stopped bit */
  448. udc->stopped = 0;
  449. /* disable pulldown dp and dm */
  450. dr_discharge_line(udc->pdata, false);
  451. udc->vbus_active = true;
  452. fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
  453. &dr_regs->usbcmd);
  454. }
  455. return;
  456. }
  457. static void dr_controller_stop(struct fsl_udc *udc)
  458. {
  459. unsigned int tmp;
  460. pr_debug("%s\n", __func__);
  461. /* if we're in OTG mode, and the Host is currently using the port,
  462. * stop now and don't rip the controller out from under the
  463. * ehci driver
  464. */
  465. if (udc->gadget.is_otg) {
  466. if (!(fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID)) {
  467. pr_debug("udc: Leaving early\n");
  468. return;
  469. }
  470. }
  471. /* disable all INTR */
  472. fsl_writel(0, &dr_regs->usbintr);
  473. /* disable wake up */
  474. dr_wake_up_enable(udc, false);
  475. /* disable BSV irq */
  476. tmp = fsl_readl(&dr_regs->otgsc);
  477. tmp &= ~OTGSC_B_SESSION_VALID_IRQ_EN;
  478. fsl_writel(tmp, &dr_regs->otgsc);
  479. /* Set stopped bit for isr */
  480. udc->stopped = 1;
  481. /* disable IO output */
  482. /* usb_sys_regs->control = 0; */
  483. fsl_platform_pullup_disable(udc->pdata);
  484. /* set controller to Stop */
  485. tmp = fsl_readl(&dr_regs->usbcmd);
  486. tmp &= ~USB_CMD_RUN_STOP;
  487. fsl_writel(tmp, &dr_regs->usbcmd);
  488. /* disable pulldown dp and dm */
  489. dr_discharge_line(udc->pdata, true);
  490. return;
  491. }
  492. void dr_ep_setup(unsigned char ep_num, unsigned char dir, unsigned char ep_type)
  493. {
  494. unsigned int tmp_epctrl = 0;
  495. tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  496. if (dir) {
  497. if (ep_num)
  498. tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
  499. tmp_epctrl |= EPCTRL_TX_ENABLE;
  500. tmp_epctrl |= ((unsigned int)(ep_type)
  501. << EPCTRL_TX_EP_TYPE_SHIFT);
  502. } else {
  503. if (ep_num)
  504. tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
  505. tmp_epctrl |= EPCTRL_RX_ENABLE;
  506. tmp_epctrl |= ((unsigned int)(ep_type)
  507. << EPCTRL_RX_EP_TYPE_SHIFT);
  508. }
  509. fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
  510. }
  511. static void
  512. dr_ep_change_stall(unsigned char ep_num, unsigned char dir, int value)
  513. {
  514. u32 tmp_epctrl = 0;
  515. tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  516. if (value) {
  517. /* set the stall bit */
  518. if (dir)
  519. tmp_epctrl |= EPCTRL_TX_EP_STALL;
  520. else
  521. tmp_epctrl |= EPCTRL_RX_EP_STALL;
  522. } else {
  523. /* clear the stall bit and reset data toggle */
  524. if (dir) {
  525. tmp_epctrl &= ~EPCTRL_TX_EP_STALL;
  526. tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
  527. } else {
  528. tmp_epctrl &= ~EPCTRL_RX_EP_STALL;
  529. tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
  530. }
  531. }
  532. fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
  533. }
  534. /* Get stall status of a specific ep
  535. Return: 0: not stalled; 1:stalled */
  536. static int dr_ep_get_stall(unsigned char ep_num, unsigned char dir)
  537. {
  538. u32 epctrl;
  539. epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  540. if (dir)
  541. return (epctrl & EPCTRL_TX_EP_STALL) ? 1 : 0;
  542. else
  543. return (epctrl & EPCTRL_RX_EP_STALL) ? 1 : 0;
  544. }
  545. /********************************************************************
  546. Internal Structure Build up functions
  547. ********************************************************************/
  548. /*------------------------------------------------------------------
  549. * struct_ep_qh_setup(): set the Endpoint Capabilites field of QH
  550. * @zlt: Zero Length Termination Select (1: disable; 0: enable)
  551. * @mult: Mult field
  552. ------------------------------------------------------------------*/
  553. static void struct_ep_qh_setup(struct fsl_udc *udc, unsigned char ep_num,
  554. unsigned char dir, unsigned char ep_type,
  555. unsigned int max_pkt_len,
  556. unsigned int zlt, unsigned char mult)
  557. {
  558. struct ep_queue_head *p_QH = &udc->ep_qh[2 * ep_num + dir];
  559. unsigned int tmp = 0;
  560. /* set the Endpoint Capabilites in QH */
  561. switch (ep_type) {
  562. case USB_ENDPOINT_XFER_CONTROL:
  563. /* Interrupt On Setup (IOS). for control ep */
  564. tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
  565. | EP_QUEUE_HEAD_IOS;
  566. break;
  567. case USB_ENDPOINT_XFER_ISOC:
  568. tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
  569. | (mult << EP_QUEUE_HEAD_MULT_POS);
  570. break;
  571. case USB_ENDPOINT_XFER_BULK:
  572. case USB_ENDPOINT_XFER_INT:
  573. tmp = max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS;
  574. break;
  575. default:
  576. VDBG("error ep type is %d", ep_type);
  577. return;
  578. }
  579. if (zlt)
  580. tmp |= EP_QUEUE_HEAD_ZLT_SEL;
  581. p_QH->max_pkt_length = cpu_to_hc32(tmp);
  582. return;
  583. }
  584. /* Setup qh structure and ep register for ep0. */
  585. static void ep0_setup(struct fsl_udc *udc)
  586. {
  587. /* the intialization of an ep includes: fields in QH, Regs,
  588. * fsl_ep struct */
  589. struct_ep_qh_setup(udc, 0, USB_RECV, USB_ENDPOINT_XFER_CONTROL,
  590. USB_MAX_CTRL_PAYLOAD, 0, 0);
  591. struct_ep_qh_setup(udc, 0, USB_SEND, USB_ENDPOINT_XFER_CONTROL,
  592. USB_MAX_CTRL_PAYLOAD, 0, 0);
  593. dr_ep_setup(0, USB_RECV, USB_ENDPOINT_XFER_CONTROL);
  594. dr_ep_setup(0, USB_SEND, USB_ENDPOINT_XFER_CONTROL);
  595. return;
  596. }
  597. /***********************************************************************
  598. Endpoint Management Functions
  599. ***********************************************************************/
  600. /*-------------------------------------------------------------------------
  601. * when configurations are set, or when interface settings change
  602. * for example the do_set_interface() in gadget layer,
  603. * the driver will enable or disable the relevant endpoints
  604. * ep0 doesn't use this routine. It is always enabled.
  605. -------------------------------------------------------------------------*/
  606. static int fsl_ep_enable(struct usb_ep *_ep,
  607. const struct usb_endpoint_descriptor *desc)
  608. {
  609. struct fsl_udc *udc = NULL;
  610. struct fsl_ep *ep = NULL;
  611. unsigned short max = 0;
  612. unsigned char mult = 0, zlt;
  613. int retval = -EINVAL;
  614. unsigned long flags = 0;
  615. ep = container_of(_ep, struct fsl_ep, ep);
  616. pr_debug("udc: %s ep.name=%s\n", __func__, ep->ep.name);
  617. /* catch various bogus parameters */
  618. if (!_ep || !desc || ep->desc
  619. || (desc->bDescriptorType != USB_DT_ENDPOINT))
  620. return -EINVAL;
  621. udc = ep->udc;
  622. if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN))
  623. return -ESHUTDOWN;
  624. max = le16_to_cpu(desc->wMaxPacketSize);
  625. /* Disable automatic zlp generation. Driver is reponsible to indicate
  626. * explicitly through req->req.zero. This is needed to enable multi-td
  627. * request. */
  628. zlt = 1;
  629. /* Assume the max packet size from gadget is always correct */
  630. switch (desc->bmAttributes & 0x03) {
  631. case USB_ENDPOINT_XFER_CONTROL:
  632. case USB_ENDPOINT_XFER_BULK:
  633. case USB_ENDPOINT_XFER_INT:
  634. /* mult = 0. Execute N Transactions as demonstrated by
  635. * the USB variable length packet protocol where N is
  636. * computed using the Maximum Packet Length (dQH) and
  637. * the Total Bytes field (dTD) */
  638. mult = 0;
  639. break;
  640. case USB_ENDPOINT_XFER_ISOC:
  641. /* Calculate transactions needed for high bandwidth iso */
  642. mult = (unsigned char)(1 + ((max >> 11) & 0x03));
  643. max = max & 0x7ff; /* bit 0~10 */
  644. /* 3 transactions at most */
  645. if (mult > 3)
  646. goto en_done;
  647. break;
  648. default:
  649. goto en_done;
  650. }
  651. spin_lock_irqsave(&udc->lock, flags);
  652. ep->ep.maxpacket = max;
  653. ep->desc = desc;
  654. ep->stopped = 0;
  655. /* Controller related setup */
  656. /* Init EPx Queue Head (Ep Capabilites field in QH
  657. * according to max, zlt, mult) */
  658. struct_ep_qh_setup(udc, (unsigned char) ep_index(ep),
  659. (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
  660. ? USB_SEND : USB_RECV),
  661. (unsigned char) (desc->bmAttributes
  662. & USB_ENDPOINT_XFERTYPE_MASK),
  663. max, zlt, mult);
  664. /* Init endpoint ctrl register */
  665. dr_ep_setup((unsigned char) ep_index(ep),
  666. (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
  667. ? USB_SEND : USB_RECV),
  668. (unsigned char) (desc->bmAttributes
  669. & USB_ENDPOINT_XFERTYPE_MASK));
  670. spin_unlock_irqrestore(&udc->lock, flags);
  671. retval = 0;
  672. VDBG("enabled %s (ep%d%s) maxpacket %d", ep->ep.name,
  673. ep->desc->bEndpointAddress & 0x0f,
  674. (desc->bEndpointAddress & USB_DIR_IN)
  675. ? "in" : "out", max);
  676. en_done:
  677. return retval;
  678. }
  679. /*---------------------------------------------------------------------
  680. * @ep : the ep being unconfigured. May not be ep0
  681. * Any pending and uncomplete req will complete with status (-ESHUTDOWN)
  682. *---------------------------------------------------------------------*/
  683. static int fsl_ep_disable(struct usb_ep *_ep)
  684. {
  685. struct fsl_udc *udc = NULL;
  686. struct fsl_ep *ep = NULL;
  687. unsigned long flags = 0;
  688. u32 epctrl;
  689. int ep_num;
  690. ep = container_of(_ep, struct fsl_ep, ep);
  691. if (!_ep || !ep->desc) {
  692. VDBG("%s not enabled", _ep ? ep->ep.name : NULL);
  693. return -EINVAL;
  694. }
  695. /* disable ep on controller */
  696. ep_num = ep_index(ep);
  697. epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  698. if (ep_is_in(ep))
  699. epctrl &= ~EPCTRL_TX_ENABLE;
  700. else
  701. epctrl &= ~EPCTRL_RX_ENABLE;
  702. fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
  703. udc = (struct fsl_udc *)ep->udc;
  704. spin_lock_irqsave(&udc->lock, flags);
  705. /* nuke all pending requests (does flush) */
  706. nuke(ep, -ESHUTDOWN);
  707. ep->desc = 0;
  708. ep->stopped = 1;
  709. spin_unlock_irqrestore(&udc->lock, flags);
  710. VDBG("disabled %s OK", _ep->name);
  711. return 0;
  712. }
  713. /*---------------------------------------------------------------------
  714. * allocate a request object used by this endpoint
  715. * the main operation is to insert the req->queue to the eq->queue
  716. * Returns the request, or null if one could not be allocated
  717. *---------------------------------------------------------------------*/
  718. static struct usb_request *
  719. fsl_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  720. {
  721. struct fsl_req *req = NULL;
  722. req = kzalloc(sizeof *req, gfp_flags);
  723. if (!req)
  724. return NULL;
  725. req->req.dma = DMA_ADDR_INVALID;
  726. pr_debug("udc: req=0x%p set req.dma=0x%x\n", req, req->req.dma);
  727. INIT_LIST_HEAD(&req->queue);
  728. return &req->req;
  729. }
  730. static void fsl_free_request(struct usb_ep *_ep, struct usb_request *_req)
  731. {
  732. struct fsl_req *req = NULL;
  733. req = container_of(_req, struct fsl_req, req);
  734. if (_req)
  735. kfree(req);
  736. }
  737. static void update_qh(struct fsl_req *req)
  738. {
  739. struct fsl_ep *ep = req->ep;
  740. int i = ep_index(ep) * 2 + ep_is_in(ep);
  741. u32 temp;
  742. struct ep_queue_head *dQH = &ep->udc->ep_qh[i];
  743. /* Write dQH next pointer and terminate bit to 0 */
  744. temp = req->head->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
  745. if (NEED_IRAM(req->ep)) {
  746. /* set next dtd stop bit,ensure only one dtd in this list */
  747. req->cur->next_td_ptr |= cpu_to_hc32(DTD_NEXT_TERMINATE);
  748. temp = req->cur->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
  749. }
  750. dQH->next_dtd_ptr = cpu_to_hc32(temp);
  751. /* Clear active and halt bit */
  752. temp = cpu_to_hc32(~(EP_QUEUE_HEAD_STATUS_ACTIVE
  753. | EP_QUEUE_HEAD_STATUS_HALT));
  754. dQH->size_ioc_int_sts &= temp;
  755. /* Prime endpoint by writing 1 to ENDPTPRIME */
  756. temp = ep_is_in(ep)
  757. ? (1 << (ep_index(ep) + 16))
  758. : (1 << (ep_index(ep)));
  759. fsl_writel(temp, &dr_regs->endpointprime);
  760. }
  761. /*-------------------------------------------------------------------------*/
  762. static int fsl_queue_td(struct fsl_ep *ep, struct fsl_req *req)
  763. {
  764. u32 temp, bitmask, tmp_stat;
  765. /* VDBG("QH addr Register 0x%8x", dr_regs->endpointlistaddr);
  766. VDBG("ep_qh[%d] addr is 0x%8x", i, (u32)&(ep->udc->ep_qh[i])); */
  767. bitmask = ep_is_in(ep)
  768. ? (1 << (ep_index(ep) + 16))
  769. : (1 << (ep_index(ep)));
  770. /*
  771. * check if
  772. * - the request is empty, and
  773. * - the request is not the status request for ep0
  774. */
  775. if (!(list_empty(&ep->queue)) &&
  776. !((ep_index(ep) == 0) && (req->req.length == 0))) {
  777. /* Add td to the end */
  778. struct fsl_req *lastreq;
  779. lastreq = list_entry(ep->queue.prev, struct fsl_req, queue);
  780. if (NEED_IRAM(ep)) {
  781. /* only one dtd in dqh */
  782. lastreq->tail->next_td_ptr =
  783. cpu_to_hc32(req->head->td_dma | DTD_NEXT_TERMINATE);
  784. goto out;
  785. } else {
  786. lastreq->tail->next_td_ptr =
  787. cpu_to_hc32(req->head->td_dma & DTD_ADDR_MASK);
  788. }
  789. /* Read prime bit, if 1 goto done */
  790. if (fsl_readl(&dr_regs->endpointprime) & bitmask)
  791. goto out;
  792. do {
  793. /* Set ATDTW bit in USBCMD */
  794. temp = fsl_readl(&dr_regs->usbcmd);
  795. fsl_writel(temp | USB_CMD_ATDTW, &dr_regs->usbcmd);
  796. /* Read correct status bit */
  797. tmp_stat = fsl_readl(&dr_regs->endptstatus) & bitmask;
  798. } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_ATDTW));
  799. /* Write ATDTW bit to 0 */
  800. temp = fsl_readl(&dr_regs->usbcmd);
  801. fsl_writel(temp & ~USB_CMD_ATDTW, &dr_regs->usbcmd);
  802. if (tmp_stat)
  803. goto out;
  804. }
  805. update_qh(req);
  806. out:
  807. return 0;
  808. }
  809. /* Fill in the dTD structure
  810. * @req: request that the transfer belongs to
  811. * @length: return actually data length of the dTD
  812. * @dma: return dma address of the dTD
  813. * @is_last: return flag if it is the last dTD of the request
  814. * return: pointer to the built dTD */
  815. static struct ep_td_struct *fsl_build_dtd(struct fsl_req *req, unsigned *length,
  816. dma_addr_t *dma, int *is_last)
  817. {
  818. u32 swap_temp;
  819. struct ep_td_struct *dtd;
  820. /* how big will this transfer be? */
  821. *length = min(req->req.length - req->req.actual,
  822. (unsigned)EP_MAX_LENGTH_TRANSFER);
  823. if (NEED_IRAM(req->ep))
  824. *length = min(*length, g_iram_size);
  825. dtd = malloc_dma_buffer(dma, sizeof(struct ep_td_struct), DTD_ALIGNMENT);
  826. if (dtd == NULL)
  827. return dtd;
  828. dtd->td_dma = *dma;
  829. /* Clear reserved field */
  830. swap_temp = hc32_to_cpu(dtd->size_ioc_sts);
  831. swap_temp &= ~DTD_RESERVED_FIELDS;
  832. dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
  833. /* Init all of buffer page pointers */
  834. swap_temp = (u32) (req->req.dma + req->req.actual);
  835. if (NEED_IRAM(req->ep))
  836. swap_temp = (u32) (req->req.dma);
  837. dtd->buff_ptr0 = cpu_to_hc32(swap_temp);
  838. dtd->buff_ptr1 = cpu_to_hc32(swap_temp + 0x1000);
  839. dtd->buff_ptr2 = cpu_to_hc32(swap_temp + 0x2000);
  840. dtd->buff_ptr3 = cpu_to_hc32(swap_temp + 0x3000);
  841. dtd->buff_ptr4 = cpu_to_hc32(swap_temp + 0x4000);
  842. req->req.actual += *length;
  843. /* zlp is needed if req->req.zero is set */
  844. if (req->req.zero) {
  845. if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
  846. *is_last = 1;
  847. else
  848. *is_last = 0;
  849. } else if (req->req.length == req->req.actual)
  850. *is_last = 1;
  851. else
  852. *is_last = 0;
  853. if ((*is_last) == 0)
  854. VDBG("multi-dtd request!\n");
  855. /* Fill in the transfer size; set active bit */
  856. swap_temp = ((*length << DTD_LENGTH_BIT_POS) | DTD_STATUS_ACTIVE);
  857. /* Enable interrupt for the last dtd of a request */
  858. if (*is_last && !req->req.no_interrupt)
  859. swap_temp |= DTD_IOC;
  860. if (NEED_IRAM(req->ep))
  861. swap_temp |= DTD_IOC;
  862. dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
  863. VDBG("length = %d address= 0x%x", *length, (int)dtd);
  864. return dtd;
  865. }
  866. /* Generate dtd chain for a request */
  867. static int fsl_req_to_dtd(struct fsl_req *req)
  868. {
  869. unsigned count;
  870. int is_last;
  871. int is_first = 1;
  872. struct ep_td_struct *last_dtd = NULL, *dtd;
  873. dma_addr_t dma;
  874. if (NEED_IRAM(req->ep)) {
  875. req->oridma = req->req.dma;
  876. /* here, replace user buffer to iram buffer */
  877. if (ep_is_in(req->ep)) {
  878. req->req.dma = req->ep->udc->iram_buffer[1];
  879. if ((list_empty(&req->ep->queue))) {
  880. /* copy data only when no bulk in transfer is
  881. running */
  882. memcpy((char *)req->ep->udc->iram_buffer_v[1],
  883. req->req.buf, min(req->req.length,
  884. g_iram_size));
  885. }
  886. } else {
  887. req->req.dma = req->ep->udc->iram_buffer[0];
  888. }
  889. }
  890. if (USE_MSC_WR(req->req.length))
  891. req->req.dma += 1;
  892. do {
  893. dtd = fsl_build_dtd(req, &count, &dma, &is_last);
  894. if (dtd == NULL)
  895. return -ENOMEM;
  896. if (is_first) {
  897. is_first = 0;
  898. req->head = dtd;
  899. } else {
  900. last_dtd->next_td_ptr = cpu_to_hc32(dma);
  901. last_dtd->next_td_virt = dtd;
  902. }
  903. last_dtd = dtd;
  904. req->dtd_count++;
  905. } while (!is_last);
  906. dtd->next_td_ptr = cpu_to_hc32(DTD_NEXT_TERMINATE);
  907. req->cur = req->head;
  908. req->tail = dtd;
  909. return 0;
  910. }
  911. /* queues (submits) an I/O request to an endpoint */
  912. static int
  913. fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  914. {
  915. struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
  916. struct fsl_req *req = container_of(_req, struct fsl_req, req);
  917. struct fsl_udc *udc;
  918. unsigned long flags;
  919. int is_iso = 0;
  920. if (!_ep || !ep->desc) {
  921. VDBG("%s, bad ep\n", __func__);
  922. return -EINVAL;
  923. }
  924. udc = ep->udc;
  925. spin_lock_irqsave(&udc->lock, flags);
  926. /* catch various bogus parameters */
  927. if (!_req || !req->req.buf || (ep_index(ep)
  928. && !list_empty(&req->queue))) {
  929. VDBG("%s, bad params\n", __func__);
  930. spin_unlock_irqrestore(&udc->lock, flags);
  931. return -EINVAL;
  932. }
  933. if (usb_endpoint_xfer_isoc(ep->desc)) {
  934. if (req->req.length > ep->ep.maxpacket) {
  935. spin_unlock_irqrestore(&udc->lock, flags);
  936. return -EMSGSIZE;
  937. }
  938. is_iso = 1;
  939. }
  940. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
  941. spin_unlock_irqrestore(&udc->lock, flags);
  942. return -ESHUTDOWN;
  943. }
  944. req->ep = ep;
  945. /* map virtual address to hardware */
  946. if (req->req.dma == DMA_ADDR_INVALID) {
  947. req->req.dma = req->req.buf;
  948. req->mapped = 1;
  949. } else {
  950. req->mapped = 0;
  951. }
  952. req->req.status = -EINPROGRESS;
  953. req->req.actual = 0;
  954. req->dtd_count = 0;
  955. if (NEED_IRAM(ep)) {
  956. req->last_one = 0;
  957. req->buffer_offset = 0;
  958. }
  959. /* build dtds and push them to device queue */
  960. if (!fsl_req_to_dtd(req)) {
  961. fsl_queue_td(ep, req);
  962. } else {
  963. spin_unlock_irqrestore(&udc->lock, flags);
  964. return -ENOMEM;
  965. }
  966. /* irq handler advances the queue */
  967. if (req != NULL)
  968. list_add_tail(&req->queue, &ep->queue);
  969. spin_unlock_irqrestore(&udc->lock, flags);
  970. return 0;
  971. }
  972. /* dequeues (cancels, unlinks) an I/O request from an endpoint */
  973. static int fsl_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  974. {
  975. struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
  976. struct fsl_req *req;
  977. unsigned long flags;
  978. int ep_num, stopped, ret = 0;
  979. struct fsl_udc *udc = NULL;
  980. u32 epctrl;
  981. if (!_ep || !_req)
  982. return -EINVAL;
  983. spin_lock_irqsave(&ep->udc->lock, flags);
  984. stopped = ep->stopped;
  985. udc = ep->udc;
  986. /* Stop the ep before we deal with the queue */
  987. ep->stopped = 1;
  988. ep_num = ep_index(ep);
  989. epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  990. if (ep_is_in(ep))
  991. epctrl &= ~EPCTRL_TX_ENABLE;
  992. else
  993. epctrl &= ~EPCTRL_RX_ENABLE;
  994. fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
  995. /* make sure it's actually queued on this endpoint */
  996. list_for_each_entry(req, &ep->queue, queue) {
  997. if (&req->req == _req)
  998. break;
  999. }
  1000. if (&req->req != _req) {
  1001. ret = -EINVAL;
  1002. goto out;
  1003. }
  1004. /* The request is in progress, or completed but not dequeued */
  1005. if (ep->queue.next == &req->queue) {
  1006. _req->status = -ECONNRESET;
  1007. fsl_ep_fifo_flush(_ep); /* flush current transfer */
  1008. /* The request isn't the last request in this ep queue */
  1009. if (req->queue.next != &ep->queue) {
  1010. struct ep_queue_head *qh;
  1011. struct fsl_req *next_req;
  1012. qh = ep->qh;
  1013. next_req = list_entry(req->queue.next, struct fsl_req,
  1014. queue);
  1015. /* prime with dTD of next request */
  1016. update_qh(next_req);
  1017. }
  1018. /* The request hasn't been processed, patch up the TD chain */
  1019. } else {
  1020. struct fsl_req *prev_req;
  1021. prev_req = list_entry(req->queue.prev, struct fsl_req, queue);
  1022. prev_req->tail->next_td_ptr = req->tail->next_td_ptr;
  1023. }
  1024. done(ep, req, -ECONNRESET);
  1025. /* Enable EP */
  1026. out: epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  1027. if (ep_is_in(ep))
  1028. epctrl |= EPCTRL_TX_ENABLE;
  1029. else
  1030. epctrl |= EPCTRL_RX_ENABLE;
  1031. fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
  1032. ep->stopped = stopped;
  1033. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1034. return ret;
  1035. }
  1036. /*-------------------------------------------------------------------------*/
  1037. /*-----------------------------------------------------------------
  1038. * modify the endpoint halt feature
  1039. * @ep: the non-isochronous endpoint being stalled
  1040. * @value: 1--set halt 0--clear halt
  1041. * Returns zero, or a negative error code.
  1042. *----------------------------------------------------------------*/
  1043. static int fsl_ep_set_halt(struct usb_ep *_ep, int value)
  1044. {
  1045. struct fsl_ep *ep = NULL;
  1046. unsigned long flags = 0;
  1047. int status = -EOPNOTSUPP; /* operation not supported */
  1048. unsigned char ep_dir = 0, ep_num = 0;
  1049. struct fsl_udc *udc = NULL;
  1050. ep = container_of(_ep, struct fsl_ep, ep);
  1051. udc = ep->udc;
  1052. if (!_ep || !ep->desc) {
  1053. status = -EINVAL;
  1054. goto out;
  1055. }
  1056. if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
  1057. status = -ESHUTDOWN;
  1058. goto out;
  1059. }
  1060. if (usb_endpoint_xfer_isoc(ep->desc)) {
  1061. status = -EOPNOTSUPP;
  1062. goto out;
  1063. }
  1064. /* Attempt to halt IN ep will fail if any transfer requests
  1065. * are still queue */
  1066. if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
  1067. status = -EAGAIN;
  1068. goto out;
  1069. }
  1070. status = 0;
  1071. ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
  1072. ep_num = (unsigned char)(ep_index(ep));
  1073. spin_lock_irqsave(&ep->udc->lock, flags);
  1074. dr_ep_change_stall(ep_num, ep_dir, value);
  1075. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1076. if (ep_index(ep) == 0) {
  1077. udc->ep0_dir = 0;
  1078. }
  1079. out:
  1080. VDBG(" %s %s halt stat %d", ep->ep.name,
  1081. value ? "set" : "clear", status);
  1082. return status;
  1083. }
  1084. static int arcotg_fifo_status(struct usb_ep *_ep)
  1085. {
  1086. struct fsl_ep *ep;
  1087. struct fsl_udc *udc;
  1088. int size = 0;
  1089. u32 bitmask;
  1090. struct ep_queue_head *d_qh;
  1091. ep = container_of(_ep, struct fsl_ep, ep);
  1092. if (!_ep || (!ep->desc && ep_index(ep) != 0))
  1093. return -ENODEV;
  1094. udc = (struct fsl_udc *)ep->udc;
  1095. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
  1096. return -ESHUTDOWN;
  1097. d_qh = &ep->udc->ep_qh[ep_index(ep) * 2 + ep_is_in(ep)];
  1098. bitmask = (ep_is_in(ep)) ? (1 << (ep_index(ep) + 16)) :
  1099. (1 << (ep_index(ep)));
  1100. if (fsl_readl(&dr_regs->endptstatus) & bitmask)
  1101. size = (d_qh->size_ioc_int_sts & DTD_PACKET_SIZE)
  1102. >> DTD_LENGTH_BIT_POS;
  1103. pr_debug("%s %u\n", __func__, size);
  1104. return size;
  1105. }
  1106. static void fsl_ep_fifo_flush(struct usb_ep *_ep)
  1107. {
  1108. struct fsl_ep *ep;
  1109. int ep_num, ep_dir;
  1110. u32 bits;
  1111. unsigned long timeout;
  1112. #define FSL_UDC_FLUSH_TIMEOUT 1000 /* msec */
  1113. if (!_ep) {
  1114. return;
  1115. } else {
  1116. ep = container_of(_ep, struct fsl_ep, ep);
  1117. if (!ep->desc)
  1118. return;
  1119. }
  1120. ep_num = ep_index(ep);
  1121. ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
  1122. if (ep_num == 0)
  1123. bits = (1 << 16) | 1;
  1124. else if (ep_dir == USB_SEND)
  1125. bits = 1 << (16 + ep_num);
  1126. else
  1127. bits = 1 << ep_num;
  1128. timeout = get_timer(0) + FSL_UDC_FLUSH_TIMEOUT;
  1129. do {
  1130. fsl_writel(bits, &dr_regs->endptflush);
  1131. /* Wait until flush complete */
  1132. while (fsl_readl(&dr_regs->endptflush)) {
  1133. if (get_timer(0) > timeout) {
  1134. ERR("ep flush timeout\n");
  1135. return;
  1136. }
  1137. }
  1138. /* See if we need to flush again */
  1139. } while (fsl_readl(&dr_regs->endptstatus) & bits);
  1140. }
  1141. static struct usb_ep_ops fsl_ep_ops = {
  1142. .enable = fsl_ep_enable,
  1143. .disable = fsl_ep_disable,
  1144. .alloc_request = fsl_alloc_request,
  1145. .free_request = fsl_free_request,
  1146. .queue = fsl_ep_queue,
  1147. .dequeue = fsl_ep_dequeue,
  1148. .set_halt = fsl_ep_set_halt,
  1149. .fifo_status = arcotg_fifo_status,
  1150. .fifo_flush = fsl_ep_fifo_flush, /* flush fifo */
  1151. };
  1152. /*-------------------------------------------------------------------------
  1153. Gadget Driver Layer Operations
  1154. -------------------------------------------------------------------------*/
  1155. /*----------------------------------------------------------------------
  1156. * Get the current frame number (from DR frame_index Reg )
  1157. *----------------------------------------------------------------------*/
  1158. static int fsl_get_frame(struct usb_gadget *gadget)
  1159. {
  1160. return (int)(fsl_readl(&dr_regs->frindex) & USB_FRINDEX_MASKS);
  1161. }
  1162. /*-----------------------------------------------------------------------
  1163. * Tries to wake up the host connected to this gadget
  1164. -----------------------------------------------------------------------*/
  1165. static int fsl_wakeup(struct usb_gadget *gadget)
  1166. {
  1167. struct fsl_udc *udc = container_of(gadget, struct fsl_udc, gadget);
  1168. u32 portsc;
  1169. /* Remote wakeup feature not enabled by host */
  1170. if (!udc->remote_wakeup)
  1171. return -ENOTSUPP;
  1172. portsc = fsl_readl(&dr_regs->portsc1);
  1173. /* not suspended? */
  1174. if (!(portsc & PORTSCX_PORT_SUSPEND))
  1175. return 0;
  1176. /* trigger force resume */
  1177. portsc |= PORTSCX_PORT_FORCE_RESUME;
  1178. fsl_writel(portsc, &dr_regs->portsc1);
  1179. return 0;
  1180. }
  1181. static int can_pullup(struct fsl_udc *udc)
  1182. {
  1183. return udc->driver && udc->softconnect && udc->vbus_active;
  1184. }
  1185. /* Notify controller that VBUS is powered, Called by whatever
  1186. detects VBUS sessions */
  1187. static int fsl_vbus_session(struct usb_gadget *gadget, int is_active)
  1188. {
  1189. struct fsl_udc *udc;
  1190. unsigned long flags;
  1191. udc = container_of(gadget, struct fsl_udc, gadget);
  1192. spin_lock_irqsave(&udc->lock, flags);
  1193. VDBG("VBUS %s\n", is_active ? "on" : "off");
  1194. udc->vbus_active = (is_active != 0);
  1195. if (can_pullup(udc))
  1196. fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
  1197. &dr_regs->usbcmd);
  1198. else
  1199. fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
  1200. &dr_regs->usbcmd);
  1201. spin_unlock_irqrestore(&udc->lock, flags);
  1202. return 0;
  1203. }
  1204. /* constrain controller's VBUS power usage
  1205. * This call is used by gadget drivers during SET_CONFIGURATION calls,
  1206. * reporting how much power the device may consume. For example, this
  1207. * could affect how quickly batteries are recharged.
  1208. *
  1209. * Returns zero on success, else negative errno.
  1210. */
  1211. static int fsl_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  1212. {
  1213. VDBG("in vbus_draw\n");
  1214. struct fsl_udc *udc;
  1215. struct fsl_usb2_platform_data *pdata;
  1216. udc = container_of(gadget, struct fsl_udc, gadget);
  1217. pdata = udc->pdata;
  1218. if (pdata->xcvr_ops && pdata->xcvr_ops->set_vbus_draw) {
  1219. pdata->xcvr_ops->set_vbus_draw(pdata->xcvr_ops, pdata, mA);
  1220. return 0;
  1221. }
  1222. return -ENOTSUPP;
  1223. }
  1224. /* Change Data+ pullup status
  1225. * this func is used by usb_gadget_connect/disconnet
  1226. */
  1227. static int fsl_pullup(struct usb_gadget *gadget, int is_on)
  1228. {
  1229. struct fsl_udc *udc;
  1230. udc = container_of(gadget, struct fsl_udc, gadget);
  1231. udc->softconnect = (is_on != 0);
  1232. if (can_pullup(udc))
  1233. fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
  1234. &dr_regs->usbcmd);
  1235. else
  1236. fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
  1237. &dr_regs->usbcmd);
  1238. return 0;
  1239. }
  1240. /*
  1241. * The USB PHY/Charger driver can't visit usb_gadget directly, so
  1242. * supply a wrapped function for usb charger visiting.
  1243. */
  1244. static void usb_charger_pullup_dp(bool enable)
  1245. {
  1246. fsl_pullup(&udc_controller->gadget, (int)enable);
  1247. }
  1248. /* defined in gadget.h */
  1249. static struct usb_gadget_ops fsl_gadget_ops = {
  1250. .get_frame = fsl_get_frame,
  1251. .wakeup = fsl_wakeup,
  1252. /* .set_selfpowered = fsl_set_selfpowered, */ /* Always selfpowered */
  1253. .vbus_session = fsl_vbus_session,
  1254. .vbus_draw = fsl_vbus_draw,
  1255. .pullup = fsl_pullup,
  1256. };
  1257. /* Set protocol stall on ep0, protocol stall will automatically be cleared
  1258. on new transaction */
  1259. static void ep0stall(struct fsl_udc *udc)
  1260. {
  1261. VDBG("in ep0stall");
  1262. u32 tmp;
  1263. /* must set tx and rx to stall at the same time */
  1264. tmp = fsl_readl(&dr_regs->endptctrl[0]);
  1265. tmp |= EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL;
  1266. fsl_writel(tmp, &dr_regs->endptctrl[0]);
  1267. udc->ep0_dir = 0;
  1268. }
  1269. /* Prime a status phase for ep0 */
  1270. static int ep0_prime_status(struct fsl_udc *udc, int direction)
  1271. {
  1272. struct fsl_req *req = udc->status_req;
  1273. struct fsl_ep *ep;
  1274. int status = 0;
  1275. if (direction == EP_DIR_IN)
  1276. udc->ep0_dir = USB_DIR_IN;
  1277. else
  1278. udc->ep0_dir = USB_DIR_OUT;
  1279. ep = &udc->eps[0];
  1280. req->ep = ep;
  1281. req->req.length = 0;
  1282. req->req.status = -EINPROGRESS;
  1283. status = fsl_ep_queue(&ep->ep, &req->req, GFP_ATOMIC);
  1284. return status;
  1285. }
  1286. static inline int udc_reset_ep_queue(struct fsl_udc *udc, u8 pipe)
  1287. {
  1288. struct fsl_ep *ep = get_ep_by_pipe(udc, pipe);
  1289. if (!ep->name)
  1290. return 0;
  1291. nuke(ep, -ESHUTDOWN);
  1292. return 0;
  1293. }
  1294. /*
  1295. * ch9 Set address
  1296. */
  1297. static void ch9setaddress(struct fsl_udc *udc, u16 value, u16 index, u16 length)
  1298. {
  1299. /* Save the new address to device struct */
  1300. udc->device_address = (u8) value;
  1301. /* Update usb state */
  1302. udc->usb_state = USB_STATE_ADDRESS;
  1303. /* for USB CV 3.0 test, the gap between the ACK of the set_address
  1304. * and the subsequently setup packet may be very little, say 500us,
  1305. * and if the latency we handle the ep completion is greater than
  1306. * this gap, there is no response to the subsequent setup packet.
  1307. * It will cause the CV test fail */
  1308. /* There is another way to set address, we can set the bit 24 to
  1309. * 1 to make IC set this address instead of SW, it is more fast
  1310. * and safe than SW way */
  1311. fsl_writel(udc->device_address << USB_DEVICE_ADDRESS_BIT_POS |
  1312. 1 << USB_DEVICE_ADDRESS_ADV_BIT_POS,
  1313. &dr_regs->deviceaddr);
  1314. /* Status phase */
  1315. if (ep0_prime_status(udc, EP_DIR_IN))
  1316. ep0stall(udc);
  1317. }
  1318. /*
  1319. * ch9 Get status
  1320. */
  1321. static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
  1322. u16 index, u16 length)
  1323. {
  1324. u16 tmp = 0; /* Status, cpu endian */
  1325. struct fsl_req *req;
  1326. struct fsl_ep *ep;
  1327. int status = 0;
  1328. ep = &udc->eps[0];
  1329. if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
  1330. /* Get device status */
  1331. tmp = 1 << USB_DEVICE_SELF_POWERED;
  1332. tmp |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
  1333. } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
  1334. /* Get interface status */
  1335. /* We don't have interface information in udc driver */
  1336. tmp = 0;
  1337. } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
  1338. /* Get endpoint status */
  1339. struct fsl_ep *target_ep;
  1340. target_ep = get_ep_by_pipe(udc, get_pipe_by_windex(index));
  1341. /* stall if endpoint doesn't exist */
  1342. if (!target_ep->desc)
  1343. goto stall;
  1344. tmp = dr_ep_get_stall(ep_index(target_ep), ep_is_in(target_ep))
  1345. << USB_ENDPOINT_HALT;
  1346. }
  1347. udc->ep0_dir = USB_DIR_IN;
  1348. /* Borrow the per device data_req */
  1349. /* status_req had been used to prime status */
  1350. req = udc->data_req;
  1351. /* Fill in the reqest structure */
  1352. *((u16 *) req->req.buf) = cpu_to_le16(tmp);
  1353. req->ep = ep;
  1354. req->req.length = 2;
  1355. status = fsl_ep_queue(&ep->ep, &req->req, GFP_ATOMIC);
  1356. if (status) {
  1357. udc_reset_ep_queue(udc, 0);
  1358. ERR("Can't respond to getstatus request \n");
  1359. goto stall;
  1360. }
  1361. /* Status phase */
  1362. if (ep0_prime_status(udc, EP_DIR_OUT))
  1363. ep0stall(udc);
  1364. return;
  1365. stall:
  1366. ep0stall(udc);
  1367. }
  1368. static void setup_received_irq(struct fsl_udc *udc,
  1369. struct usb_ctrlrequest *setup)
  1370. {
  1371. u16 wValue = le16_to_cpu(setup->wValue);
  1372. u16 wIndex = le16_to_cpu(setup->wIndex);
  1373. u16 wLength = le16_to_cpu(setup->wLength);
  1374. struct usb_gadget *gadget = &(udc->gadget);
  1375. unsigned mA = 500;
  1376. udc_reset_ep_queue(udc, 0);
  1377. VDBG("request: %x", setup->bRequest);
  1378. /* We process some stardard setup requests here */
  1379. switch (setup->bRequest) {
  1380. case USB_REQ_GET_STATUS:
  1381. /* Data+Status phase from udc */
  1382. if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
  1383. != (USB_DIR_IN | USB_TYPE_STANDARD))
  1384. break;
  1385. spin_unlock(&udc->lock);
  1386. ch9getstatus(udc, setup->bRequestType, wValue, wIndex, wLength);
  1387. spin_lock(&udc->lock);
  1388. return;
  1389. case USB_REQ_SET_ADDRESS:
  1390. /* Status phase from udc */
  1391. if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
  1392. | USB_RECIP_DEVICE))
  1393. break;
  1394. spin_unlock(&udc->lock);
  1395. ch9setaddress(udc, wValue, wIndex, wLength);
  1396. spin_lock(&udc->lock);
  1397. return;
  1398. case USB_REQ_SET_CONFIGURATION:
  1399. spin_unlock(&udc->lock);
  1400. spin_lock(&udc->lock);
  1401. break;
  1402. case USB_REQ_CLEAR_FEATURE:
  1403. case USB_REQ_SET_FEATURE:
  1404. /* Status phase from udc */
  1405. {
  1406. int rc = -EOPNOTSUPP;
  1407. u16 ptc = 0;
  1408. if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
  1409. == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
  1410. int pipe = get_pipe_by_windex(wIndex);
  1411. struct fsl_ep *ep;
  1412. if (wValue != 0 || wLength != 0 || pipe >= (udc->max_ep / 2))
  1413. break;
  1414. ep = get_ep_by_pipe(udc, pipe);
  1415. spin_unlock(&udc->lock);
  1416. rc = fsl_ep_set_halt(&ep->ep,
  1417. (setup->bRequest == USB_REQ_SET_FEATURE)
  1418. ? 1 : 0);
  1419. spin_lock(&udc->lock);
  1420. } else if ((setup->bRequestType & (USB_RECIP_MASK
  1421. | USB_TYPE_MASK)) == (USB_RECIP_DEVICE
  1422. | USB_TYPE_STANDARD)) {
  1423. /* Note: The driver has not include OTG support yet.
  1424. * This will be set when OTG support is added */
  1425. if (setup->wValue == USB_DEVICE_TEST_MODE)
  1426. ptc = setup->wIndex >> 8;
  1427. else if (setup->wValue == USB_DEVICE_REMOTE_WAKEUP) {
  1428. if (setup->bRequest == USB_REQ_SET_FEATURE)
  1429. udc->remote_wakeup = 1;
  1430. else
  1431. udc->remote_wakeup = 0;
  1432. }
  1433. else if (gadget_is_otg(&udc->gadget)) {
  1434. if (setup->bRequest ==
  1435. USB_DEVICE_B_HNP_ENABLE)
  1436. udc->gadget.b_hnp_enable = 1;
  1437. else if (setup->bRequest ==
  1438. USB_DEVICE_A_HNP_SUPPORT)
  1439. udc->gadget.a_hnp_support = 1;
  1440. else if (setup->bRequest ==
  1441. USB_DEVICE_A_ALT_HNP_SUPPORT)
  1442. udc->gadget.a_alt_hnp_support = 1;
  1443. else
  1444. break;
  1445. } else {
  1446. break;
  1447. }
  1448. rc = 0;
  1449. } else
  1450. break;
  1451. if (rc == 0) {
  1452. spin_unlock(&udc->lock);
  1453. if (ep0_prime_status(udc, EP_DIR_IN))
  1454. ep0stall(udc);
  1455. spin_lock(&udc->lock);
  1456. }
  1457. if (ptc) {
  1458. u32 tmp;
  1459. mdelay(10);
  1460. tmp = fsl_readl(&dr_regs->portsc1) | (ptc << 16);
  1461. fsl_writel(tmp, &dr_regs->portsc1);
  1462. printk(KERN_INFO "udc: switch to test mode 0x%x.\n", ptc);
  1463. }
  1464. return;
  1465. }
  1466. default:
  1467. break;
  1468. }
  1469. /* Requests handled by gadget */
  1470. if (wLength) {
  1471. /* Data phase from gadget, status phase from udc */
  1472. udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
  1473. ? USB_DIR_IN : USB_DIR_OUT;
  1474. spin_unlock(&udc->lock);
  1475. if (udc->driver->setup(&udc->gadget,
  1476. &udc->local_setup_buff) < 0) {
  1477. /* cancel all requests on ep0 */
  1478. udc_reset_ep_queue(udc, 0);
  1479. ep0stall(udc);
  1480. } else {
  1481. /* prime the status phase */
  1482. int dir = EP_DIR_IN;
  1483. if (setup->bRequestType & USB_DIR_IN)
  1484. dir = EP_DIR_OUT;
  1485. if (ep0_prime_status(udc, dir))
  1486. ep0stall(udc);
  1487. }
  1488. } else {
  1489. /* No data phase, IN status from gadget */
  1490. VDBG("in else branch");
  1491. udc->ep0_dir = USB_DIR_IN;
  1492. spin_unlock(&udc->lock);
  1493. if (udc->driver->setup(&udc->gadget,
  1494. &udc->local_setup_buff) < 0)
  1495. ep0stall(udc);
  1496. }
  1497. spin_lock(&udc->lock);
  1498. VDBG("end of setup_received_irq");
  1499. }
  1500. /* Process request for Data or Status phase of ep0
  1501. * prime status phase if needed */
  1502. static void ep0_req_complete(struct fsl_udc *udc, struct fsl_ep *ep0,
  1503. struct fsl_req *req)
  1504. {
  1505. done(ep0, req, 0);
  1506. }
  1507. /* Tripwire mechanism to ensure a setup packet payload is extracted without
  1508. * being corrupted by another incoming setup packet */
  1509. static void tripwire_handler(struct fsl_udc *udc, u8 ep_num, u8 *buffer_ptr)
  1510. {
  1511. u32 temp;
  1512. struct ep_queue_head *qh;
  1513. struct fsl_usb2_platform_data *pdata = udc->pdata;
  1514. qh = &udc->ep_qh[ep_num * 2 + EP_DIR_OUT];
  1515. /* Clear bit in ENDPTSETUPSTAT */
  1516. temp = fsl_readl(&dr_regs->endptsetupstat);
  1517. fsl_writel(temp | (1 << ep_num), &dr_regs->endptsetupstat);
  1518. /* while a hazard exists when setup package arrives */
  1519. do {
  1520. /* Set Setup Tripwire */
  1521. temp = fsl_readl(&dr_regs->usbcmd);
  1522. fsl_writel(temp | USB_CMD_SUTW, &dr_regs->usbcmd);
  1523. /* Copy the setup packet to local buffer */
  1524. if (pdata->le_setup_buf) {
  1525. u32 *p = (u32 *)buffer_ptr;
  1526. u32 *s = (u32 *)qh->setup_buffer;
  1527. /* Convert little endian setup buffer to CPU endian */
  1528. *p++ = le32_to_cpu(*s++);
  1529. *p = le32_to_cpu(*s);
  1530. } else {
  1531. memcpy(buffer_ptr, (u8 *) qh->setup_buffer, 8);
  1532. }
  1533. } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_SUTW));
  1534. /* Clear Setup Tripwire */
  1535. temp = fsl_readl(&dr_regs->usbcmd);
  1536. fsl_writel(temp & ~USB_CMD_SUTW, &dr_regs->usbcmd);
  1537. }
  1538. static void iram_process_ep_complete(struct fsl_req *curr_req,
  1539. int cur_transfer)
  1540. {
  1541. char *buf;
  1542. u32 len;
  1543. int in = ep_is_in(curr_req->ep);
  1544. if (in)
  1545. buf = (char *)udc_controller->iram_buffer_v[1];
  1546. else
  1547. buf = (char *)udc_controller->iram_buffer_v[0];
  1548. if (curr_req->cur->next_td_ptr == cpu_to_hc32(DTD_NEXT_TERMINATE)
  1549. || (cur_transfer < g_iram_size)
  1550. || (curr_req->req.length == curr_req->req.actual))
  1551. curr_req->last_one = 1;
  1552. if (curr_req->last_one) {
  1553. /* the last transfer */
  1554. if (!in) {
  1555. memcpy(curr_req->req.buf + curr_req->buffer_offset, buf,
  1556. cur_transfer);
  1557. }
  1558. if (curr_req->tail->next_td_ptr !=
  1559. cpu_to_hc32(DTD_NEXT_TERMINATE)) {
  1560. /* have next request,queue it */
  1561. struct fsl_req *next_req;
  1562. next_req =
  1563. list_entry(curr_req->queue.next,
  1564. struct fsl_req, queue);
  1565. if (in)
  1566. memcpy(buf, next_req->req.buf,
  1567. min(g_iram_size, next_req->req.length));
  1568. update_qh(next_req);
  1569. }
  1570. curr_req->req.dma = curr_req->oridma;
  1571. } else {
  1572. /* queue next dtd */
  1573. /* because had next dtd, so should finish */
  1574. /* tranferring g_iram_size data */
  1575. curr_req->buffer_offset += g_iram_size;
  1576. /* pervious set stop bit,now clear it */
  1577. curr_req->cur->next_td_ptr &= ~cpu_to_hc32(DTD_NEXT_TERMINATE);
  1578. curr_req->cur = curr_req->cur->next_td_virt;
  1579. if (in) {
  1580. len =
  1581. min(curr_req->req.length - curr_req->buffer_offset,
  1582. g_iram_size);
  1583. memcpy(buf, curr_req->req.buf + curr_req->buffer_offset,
  1584. len);
  1585. } else {
  1586. memcpy(curr_req->req.buf + curr_req->buffer_offset -
  1587. g_iram_size, buf, g_iram_size);
  1588. }
  1589. update_qh(curr_req);
  1590. }
  1591. }
  1592. /* process-ep_req(): free the completed Tds for this req */
  1593. static int process_ep_req(struct fsl_udc *udc, int pipe,
  1594. struct fsl_req *curr_req)
  1595. {
  1596. struct ep_td_struct *curr_td;
  1597. int td_complete, actual, remaining_length, j, tmp;
  1598. int status = 0;
  1599. int errors = 0;
  1600. struct ep_queue_head *curr_qh = &udc->ep_qh[pipe];
  1601. int direction = pipe % 2;
  1602. int total = 0, real_len;
  1603. curr_td = curr_req->head;
  1604. td_complete = 0;
  1605. actual = curr_req->req.length;
  1606. real_len = curr_req->req.length;
  1607. for (j = 0; j < curr_req->dtd_count; j++) {
  1608. remaining_length = (hc32_to_cpu(curr_td->size_ioc_sts)
  1609. & DTD_PACKET_SIZE)
  1610. >> DTD_LENGTH_BIT_POS;
  1611. if (NEED_IRAM(curr_req->ep)) {
  1612. if (real_len >= g_iram_size) {
  1613. actual = g_iram_size;
  1614. real_len -= g_iram_size;
  1615. } else { /* the last packet */
  1616. actual = real_len;
  1617. curr_req->last_one = 1;
  1618. }
  1619. }
  1620. actual -= remaining_length;
  1621. total += actual;
  1622. errors = hc32_to_cpu(curr_td->size_ioc_sts) & DTD_ERROR_MASK;
  1623. if (errors) {
  1624. if (errors & DTD_STATUS_HALTED) {
  1625. ERR("dTD error %08x QH=%d\n", errors, pipe);
  1626. /* Clear the errors and Halt condition */
  1627. tmp = hc32_to_cpu(curr_qh->size_ioc_int_sts);
  1628. tmp &= ~errors;
  1629. curr_qh->size_ioc_int_sts = cpu_to_hc32(tmp);
  1630. status = -EPIPE;
  1631. /* FIXME: continue with next queued TD? */
  1632. break;
  1633. }
  1634. if (errors & DTD_STATUS_DATA_BUFF_ERR) {
  1635. VDBG("Transfer overflow");
  1636. status = -EPROTO;
  1637. break;
  1638. } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
  1639. VDBG("ISO error");
  1640. status = -EILSEQ;
  1641. break;
  1642. } else
  1643. ERR("Unknown error has occured (0x%x)!\r\n",
  1644. errors);
  1645. } else if (hc32_to_cpu(curr_td->size_ioc_sts)
  1646. & DTD_STATUS_ACTIVE) {
  1647. VDBG("Request not complete");
  1648. status = REQ_UNCOMPLETE;
  1649. return status;
  1650. } else if (remaining_length) {
  1651. if (direction) {
  1652. VDBG("Transmit dTD remaining length not zero");
  1653. status = -EPROTO;
  1654. break;
  1655. } else {
  1656. td_complete++;
  1657. break;
  1658. }
  1659. } else {
  1660. td_complete++;
  1661. VDBG("dTD transmitted successful ");
  1662. }
  1663. if (NEED_IRAM(curr_req->ep))
  1664. if (curr_td->
  1665. next_td_ptr & cpu_to_hc32(DTD_NEXT_TERMINATE))
  1666. break;
  1667. if (j != curr_req->dtd_count - 1)
  1668. curr_td = (struct ep_td_struct *)curr_td->next_td_virt;
  1669. }
  1670. if (status)
  1671. return status;
  1672. curr_req->req.actual = total;
  1673. if (NEED_IRAM(curr_req->ep))
  1674. iram_process_ep_complete(curr_req, actual);
  1675. return 0;
  1676. }
  1677. /* Process a DTD completion interrupt */
  1678. static void dtd_complete_irq(struct fsl_udc *udc)
  1679. {
  1680. u32 bit_pos;
  1681. int i, ep_num, direction, bit_mask, status;
  1682. struct fsl_ep *curr_ep;
  1683. struct fsl_req *curr_req, *temp_req;
  1684. /* Clear the bits in the register */
  1685. bit_pos = fsl_readl(&dr_regs->endptcomplete);
  1686. fsl_writel(bit_pos, &dr_regs->endptcomplete);
  1687. if (!bit_pos)
  1688. return;
  1689. for (i = 0; i < udc->max_ep; i++) {
  1690. ep_num = i >> 1;
  1691. direction = i % 2;
  1692. bit_mask = 1 << (ep_num + 16 * direction);
  1693. if (!(bit_pos & bit_mask))
  1694. continue;
  1695. curr_ep = get_ep_by_pipe(udc, i);
  1696. /* If the ep is configured */
  1697. if (curr_ep->name == NULL) {
  1698. INFO("Invalid EP?");
  1699. continue;
  1700. }
  1701. /* process the req queue until an uncomplete request */
  1702. list_for_each_entry_safe(curr_req, temp_req, &curr_ep->queue,
  1703. queue) {
  1704. status = process_ep_req(udc, i, curr_req);
  1705. VDBG("status of process_ep_req= %d, ep = %d",
  1706. status, ep_num);
  1707. if (status == REQ_UNCOMPLETE)
  1708. break;
  1709. /* write back status to req */
  1710. curr_req->req.status = status;
  1711. if (ep_num == 0) {
  1712. ep0_req_complete(udc, curr_ep, curr_req);
  1713. break;
  1714. } else {
  1715. if (NEED_IRAM(curr_ep)) {
  1716. if (curr_req->last_one)
  1717. done(curr_ep, curr_req, status);
  1718. /* only check the 1th req */
  1719. break;
  1720. } else
  1721. done(curr_ep, curr_req, status);
  1722. }
  1723. }
  1724. dump_ep_queue(curr_ep);
  1725. }
  1726. }
  1727. static void fsl_udc_speed_update(struct fsl_udc *udc)
  1728. {
  1729. u32 speed = 0;
  1730. u32 loop = 0;
  1731. /* Wait for port reset finished */
  1732. while ((fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET)
  1733. && (loop++ < 1000))
  1734. ;
  1735. speed = (fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_SPEED_MASK);
  1736. switch (speed) {
  1737. case PORTSCX_PORT_SPEED_HIGH:
  1738. udc->gadget.speed = USB_SPEED_HIGH;
  1739. break;
  1740. case PORTSCX_PORT_SPEED_FULL:
  1741. udc->gadget.speed = USB_SPEED_FULL;
  1742. break;
  1743. case PORTSCX_PORT_SPEED_LOW:
  1744. udc->gadget.speed = USB_SPEED_LOW;
  1745. break;
  1746. default:
  1747. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1748. break;
  1749. }
  1750. }
  1751. /* Process a port change interrupt */
  1752. static void port_change_irq(struct fsl_udc *udc)
  1753. {
  1754. if (udc->bus_reset)
  1755. udc->bus_reset = 0;
  1756. /* Update port speed */
  1757. fsl_udc_speed_update(udc);
  1758. /* Update USB state */
  1759. if (!udc->resume_state)
  1760. udc->usb_state = USB_STATE_DEFAULT;
  1761. }
  1762. /* Process suspend interrupt */
  1763. static void suspend_irq(struct fsl_udc *udc)
  1764. {
  1765. pr_debug("%s begins\n", __func__);
  1766. udc->resume_state = udc->usb_state;
  1767. udc->usb_state = USB_STATE_SUSPENDED;
  1768. /* report suspend to the driver, serial.c does not support this */
  1769. if (udc->driver->suspend)
  1770. udc->driver->suspend(&udc->gadget);
  1771. pr_debug("%s ends\n", __func__);
  1772. }
  1773. static void bus_resume(struct fsl_udc *udc)
  1774. {
  1775. udc->usb_state = udc->resume_state;
  1776. udc->resume_state = 0;
  1777. /* report resume to the driver, serial.c does not support this */
  1778. if (udc->driver->resume)
  1779. udc->driver->resume(&udc->gadget);
  1780. }
  1781. /* Clear up all ep queues */
  1782. static int reset_queues(struct fsl_udc *udc)
  1783. {
  1784. u8 pipe;
  1785. for (pipe = 0; pipe < udc->max_pipes; pipe++)
  1786. udc_reset_ep_queue(udc, pipe);
  1787. spin_unlock(&udc->lock);
  1788. /* report disconnect; the driver is already quiesced */
  1789. udc->driver->disconnect(&udc->gadget);
  1790. spin_lock(&udc->lock);
  1791. return 0;
  1792. }
  1793. /* Process reset interrupt */
  1794. static void reset_irq(struct fsl_udc *udc)
  1795. {
  1796. u32 temp;
  1797. /* Clear the device address */
  1798. temp = fsl_readl(&dr_regs->deviceaddr);
  1799. fsl_writel(temp & ~USB_DEVICE_ADDRESS_MASK, &dr_regs->deviceaddr);
  1800. udc->device_address = 0;
  1801. /* Clear usb state */
  1802. udc->resume_state = 0;
  1803. udc->ep0_dir = 0;
  1804. udc->remote_wakeup = 0; /* default to 0 on reset */
  1805. udc->gadget.b_hnp_enable = 0;
  1806. udc->gadget.a_hnp_support = 0;
  1807. udc->gadget.a_alt_hnp_support = 0;
  1808. /* Clear all the setup token semaphores */
  1809. temp = fsl_readl(&dr_regs->endptsetupstat);
  1810. fsl_writel(temp, &dr_regs->endptsetupstat);
  1811. /* Clear all the endpoint complete status bits */
  1812. temp = fsl_readl(&dr_regs->endptcomplete);
  1813. fsl_writel(temp, &dr_regs->endptcomplete);
  1814. /* Write 1s to the flush register */
  1815. fsl_writel(0xffffffff, &dr_regs->endptflush);
  1816. /* Bus is reseting */
  1817. udc->bus_reset = 1;
  1818. /* Reset all the queues, include XD, dTD, EP queue
  1819. * head and TR Queue */
  1820. reset_queues(udc);
  1821. udc->usb_state = USB_STATE_DEFAULT;
  1822. }
  1823. #define FSL_DP_CHANGE_TIMEOUT 1000 /* 1000 ms */
  1824. static void gadget_wait_line_to_se0(void)
  1825. {
  1826. unsigned long timeout;
  1827. timeout = get_timer(0) + FSL_DP_CHANGE_TIMEOUT;
  1828. /* Wait for DP to SE0 */
  1829. while (!((fsl_readl(&dr_regs->portsc1) &
  1830. (u32)((1 << 10) | (1 << 11))) == PORTSCX_LINE_STATUS_SE0)) {
  1831. if (get_timer(0) > timeout) {
  1832. pr_warning(KERN_ERR "wait dp to SE0 timeout, please check"
  1833. " your hardware design!\n");
  1834. break;
  1835. }
  1836. mdelay(1);
  1837. }
  1838. }
  1839. #define FSL_WAIT_CLASS_DRIVER_TIMEOUT 3000 /* 3s */
  1840. static void gadget_wait_class_driver_finish(void)
  1841. {
  1842. unsigned long timeout;
  1843. struct fsl_udc *udc = udc_controller;
  1844. struct fsl_ep *ep;
  1845. int i = 2;
  1846. timeout = get_timer(0) + FSL_WAIT_CLASS_DRIVER_TIMEOUT;
  1847. /* for non-control endpoints */
  1848. while (i < (int)(udc_controller->max_ep)) {
  1849. ep = &udc->eps[i++];
  1850. if (ep->stopped == 0) {
  1851. if (get_timer(0) > timeout) {
  1852. i = 2;
  1853. mdelay(10);
  1854. continue;
  1855. } else {
  1856. pr_warning(KERN_WARNING "We have waited 3s, but the class driver"
  1857. " has still not finishes!\n");
  1858. break;
  1859. }
  1860. }
  1861. }
  1862. }
  1863. static void fsl_gadget_disconnect_event(void)
  1864. {
  1865. struct fsl_udc *udc = udc_controller;
  1866. unsigned long flags;
  1867. struct fsl_usb2_platform_data *pdata;
  1868. u32 tmp;
  1869. pdata = udc->pdata;
  1870. /* wait line to se0 */
  1871. dr_discharge_line(pdata, true);
  1872. /*
  1873. * Wait class drivers finish, an well-behaviour class driver should
  1874. * call ep_disable when it is notified to be disconnected.
  1875. */
  1876. gadget_wait_class_driver_finish();
  1877. spin_lock_irqsave(&udc->lock, flags);
  1878. /* here we need to enable the B_SESSION_IRQ
  1879. * to enable the following device attach
  1880. */
  1881. tmp = fsl_readl(&dr_regs->otgsc);
  1882. if (!(tmp & (OTGSC_B_SESSION_VALID_IRQ_EN)))
  1883. fsl_writel(tmp | (OTGSC_B_SESSION_VALID_IRQ_EN),
  1884. &dr_regs->otgsc);
  1885. udc->stopped = 1;
  1886. spin_unlock_irqrestore(&udc->lock, flags);
  1887. /* enable wake up */
  1888. dr_wake_up_enable(udc, true);
  1889. /* close USB PHY clock */
  1890. dr_phy_low_power_mode(udc, true);
  1891. /* close dr controller clock */
  1892. dr_clk_gate(false);
  1893. printk(KERN_DEBUG "%s: udc enter low power mode\n", __func__);
  1894. }
  1895. /* if wakup udc, return true; else return false*/
  1896. bool try_wake_up_udc(struct fsl_udc *udc)
  1897. {
  1898. struct fsl_usb2_platform_data *pdata;
  1899. u32 irq_src;
  1900. pdata = udc->pdata;
  1901. /* check if Vbus change irq */
  1902. irq_src = fsl_readl(&dr_regs->otgsc) & (~OTGSC_ID_CHANGE_IRQ_STS);
  1903. if (irq_src & OTGSC_B_SESSION_VALID_IRQ_STS) {
  1904. u32 tmp;
  1905. /* Only handle device interrupt event
  1906. * For mx53 loco board, the debug ID value is 0 and
  1907. * DO NOT support OTG function
  1908. */
  1909. if (!machine_is_mx53_loco())
  1910. if (!(fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID))
  1911. return false;
  1912. fsl_writel(irq_src, &dr_regs->otgsc);
  1913. tmp = fsl_readl(&dr_regs->usbcmd);
  1914. /* check BSV bit to see if fall or rise */
  1915. if (irq_src & OTGSC_B_SESSION_VALID) {
  1916. if (udc->suspended) /*let the system pm resume the udc */
  1917. return true;
  1918. udc->vbus_active = true;
  1919. udc->stopped = 0;
  1920. /* disable pulldown dp and dm */
  1921. dr_discharge_line(pdata, false);
  1922. } else {
  1923. udc->vbus_active = false;
  1924. fsl_pullup(&udc_controller->gadget, false); /* usbcmd.rs=0 */
  1925. /* here we need disable B_SESSION_IRQ, after
  1926. * schedule_work finished, it need to be enabled again.
  1927. * Doing like this can avoid conflicting between rapid
  1928. * plug in/out.
  1929. */
  1930. tmp = fsl_readl(&dr_regs->otgsc);
  1931. if (tmp & (OTGSC_B_SESSION_VALID_IRQ_EN))
  1932. fsl_writel(tmp &
  1933. (~OTGSC_B_SESSION_VALID_IRQ_EN),
  1934. &dr_regs->otgsc);
  1935. /* update port status */
  1936. fsl_udc_speed_update(udc);
  1937. spin_unlock(&udc->lock);
  1938. if (udc->driver)
  1939. udc->driver->disconnect(&udc->gadget);
  1940. spin_lock(&udc->lock);
  1941. return false;
  1942. }
  1943. }
  1944. return true;
  1945. }
  1946. /*
  1947. * USB device controller interrupt handler
  1948. */
  1949. static irqreturn_t fsl_udc_irq(int irq, void *_udc)
  1950. {
  1951. struct fsl_udc *udc = _udc;
  1952. u32 irq_src;
  1953. irqreturn_t status = IRQ_NONE;
  1954. unsigned long flags;
  1955. struct fsl_usb2_platform_data *pdata = udc->pdata;
  1956. if (pdata->irq_delay)
  1957. return status;
  1958. spin_lock_irqsave(&udc->lock, flags);
  1959. #ifdef CONFIG_USB_OTG
  1960. /* if no gadget register in this driver, we need do noting */
  1961. if (udc->transceiver->gadget == NULL) {
  1962. goto irq_end;
  1963. }
  1964. /* only handle device interrupt event */
  1965. if (!(fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID)) {
  1966. goto irq_end;
  1967. }
  1968. #endif
  1969. if (try_wake_up_udc(udc) == false) {
  1970. goto irq_end;
  1971. }
  1972. irq_src = fsl_readl(&dr_regs->usbsts) & fsl_readl(&dr_regs->usbintr);
  1973. /* Clear notification bits */
  1974. fsl_writel(irq_src, &dr_regs->usbsts);
  1975. /* only handle enabled interrupt */
  1976. if (irq_src == 0x0)
  1977. goto irq_end;
  1978. VDBG("IRQ: 0x%x\n", irq_src);
  1979. /* Need to resume? */
  1980. if (udc->usb_state == USB_STATE_SUSPENDED)
  1981. if ((fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_SUSPEND) == 0)
  1982. bus_resume(udc);
  1983. /* USB Interrupt */
  1984. if (irq_src & USB_STS_INT) {
  1985. VDBG("Packet int");
  1986. /* Setup package, we only support ep0 as control ep */
  1987. if (fsl_readl(&dr_regs->endptsetupstat) & EP_SETUP_STATUS_EP0) {
  1988. tripwire_handler(udc, 0,
  1989. (u8 *) (&udc->local_setup_buff));
  1990. setup_received_irq(udc, &udc->local_setup_buff);
  1991. status = IRQ_HANDLED;
  1992. }
  1993. /* completion of dtd */
  1994. if (fsl_readl(&dr_regs->endptcomplete)) {
  1995. dtd_complete_irq(udc);
  1996. status = IRQ_HANDLED;
  1997. }
  1998. }
  1999. /* SOF (for ISO transfer) */
  2000. if (irq_src & USB_STS_SOF) {
  2001. status = IRQ_HANDLED;
  2002. }
  2003. /* Port Change */
  2004. if (irq_src & USB_STS_PORT_CHANGE) {
  2005. port_change_irq(udc);
  2006. status = IRQ_HANDLED;
  2007. }
  2008. /* Reset Received */
  2009. if (irq_src & USB_STS_RESET) {
  2010. VDBG("reset int");
  2011. reset_irq(udc);
  2012. status = IRQ_HANDLED;
  2013. }
  2014. /* Sleep Enable (Suspend) */
  2015. if (irq_src & USB_STS_SUSPEND) {
  2016. VDBG("suspend int");
  2017. if (!(udc->usb_state == USB_STATE_SUSPENDED))
  2018. suspend_irq(udc);
  2019. status = IRQ_HANDLED;
  2020. }
  2021. if (irq_src & (USB_STS_ERR | USB_STS_SYS_ERR)) {
  2022. VDBG("Error IRQ %x ", irq_src);
  2023. }
  2024. irq_end:
  2025. spin_unlock_irqrestore(&udc->lock, flags);
  2026. return status;
  2027. }
  2028. /*----------------------------------------------------------------*
  2029. * Hook to gadget drivers
  2030. * Called by initialization code of gadget drivers
  2031. *----------------------------------------------------------------*/
  2032. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  2033. {
  2034. int retval = -ENODEV;
  2035. unsigned long flags = 0;
  2036. if (!udc_controller)
  2037. return -ENODEV;
  2038. if (!driver || !driver->bind || (driver->speed != USB_SPEED_FULL
  2039. && driver->speed != USB_SPEED_HIGH)
  2040. || !driver->disconnect
  2041. || !driver->setup)
  2042. return -EINVAL;
  2043. if (udc_controller->driver)
  2044. return -EBUSY;
  2045. /* lock is needed but whether should use this lock or another */
  2046. spin_lock_irqsave(&udc_controller->lock, flags);
  2047. udc_controller->pdata->port_enables = 1;
  2048. /* hook up the driver */
  2049. udc_controller->driver = driver;
  2050. spin_unlock_irqrestore(&udc_controller->lock, flags);
  2051. dr_clk_gate(true);
  2052. /* bind udc driver to gadget driver */
  2053. retval = driver->bind(&udc_controller->gadget);
  2054. if (retval) {
  2055. VDBG("bind udc driver to gadget driver --> %d", retval);
  2056. udc_controller->driver = 0;
  2057. dr_clk_gate(false);
  2058. goto out;
  2059. }
  2060. /* Enable DR IRQ reg and Set usbcmd reg Run bit */
  2061. dr_controller_run(udc_controller);
  2062. if (udc_controller->stopped)
  2063. dr_clk_gate(false);
  2064. printk(KERN_INFO "%s: bind to driver \n",
  2065. udc_controller->gadget.name);
  2066. out:
  2067. if (retval) {
  2068. printk(KERN_DEBUG "retval %d \n", retval);
  2069. udc_controller->pdata->port_enables = 0;
  2070. }
  2071. return retval;
  2072. }
  2073. EXPORT_SYMBOL(usb_gadget_probe_driver);
  2074. /* Disconnect from gadget driver */
  2075. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  2076. {
  2077. struct fsl_ep *loop_ep;
  2078. unsigned long flags;
  2079. if (!udc_controller)
  2080. return -ENODEV;
  2081. if (!driver || driver != udc_controller->driver || !driver->unbind)
  2082. return -EINVAL;
  2083. if (udc_controller->stopped)
  2084. dr_clk_gate(true);
  2085. /* stop DR, disable intr */
  2086. dr_controller_stop(udc_controller);
  2087. udc_controller->pdata->port_enables = 0;
  2088. /* in fact, no needed */
  2089. udc_controller->usb_state = USB_STATE_ATTACHED;
  2090. udc_controller->ep0_dir = 0;
  2091. /* stand operation */
  2092. spin_lock_irqsave(&udc_controller->lock, flags);
  2093. udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
  2094. nuke(&udc_controller->eps[0], -ESHUTDOWN);
  2095. list_for_each_entry(loop_ep, &udc_controller->gadget.ep_list,
  2096. ep.ep_list)
  2097. nuke(loop_ep, -ESHUTDOWN);
  2098. spin_unlock_irqrestore(&udc_controller->lock, flags);
  2099. /* disconnect gadget before unbinding */
  2100. driver->disconnect(&udc_controller->gadget);
  2101. /* unbind gadget and unhook driver. */
  2102. driver->unbind(&udc_controller->gadget);
  2103. udc_controller->driver = 0;
  2104. if (udc_controller->gadget.is_otg) {
  2105. dr_wake_up_enable(udc_controller, true);
  2106. }
  2107. dr_phy_low_power_mode(udc_controller, true);
  2108. dr_clk_gate(false);
  2109. printk(KERN_INFO "unregistered gadget driver \r\n");
  2110. return 0;
  2111. }
  2112. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  2113. /* Release udc structures */
  2114. static void fsl_udc_release(struct device *dev)
  2115. {
  2116. free(udc_controller->ep_qh);
  2117. kfree(udc_controller);
  2118. udc_controller = NULL;
  2119. }
  2120. /******************************************************************
  2121. Internal structure setup functions
  2122. *******************************************************************/
  2123. /*------------------------------------------------------------------
  2124. * init resource for globle controller
  2125. * Return the udc handle on success or NULL on failure
  2126. ------------------------------------------------------------------*/
  2127. static int __init struct_udc_setup(struct fsl_udc *udc,
  2128. struct fsl_usb2_platform_data *pdata)
  2129. {
  2130. size_t size;
  2131. udc->phy_mode = pdata->phy_mode;
  2132. udc->eps = kzalloc(sizeof(struct fsl_ep) * udc->max_ep, GFP_KERNEL);
  2133. if (!udc->eps) {
  2134. ERR("malloc fsl_ep failed\n");
  2135. return -1;
  2136. }
  2137. /* initialized QHs, take care of alignment */
  2138. size = udc->max_ep * sizeof(struct ep_queue_head);
  2139. if (size < QH_ALIGNMENT)
  2140. size = QH_ALIGNMENT;
  2141. else if ((size % QH_ALIGNMENT) != 0) {
  2142. size += QH_ALIGNMENT + 1;
  2143. size &= ~(QH_ALIGNMENT - 1);
  2144. }
  2145. udc->ep_qh = malloc_dma_buffer(&udc->ep_qh_dma, size, QH_ALIGNMENT);
  2146. if (!udc->ep_qh) {
  2147. ERR("malloc QHs for udc failed\n");
  2148. kfree(udc->eps);
  2149. return -1;
  2150. }
  2151. udc->ep_qh_size = size;
  2152. /* Initialize ep0 status request structure */
  2153. /* FIXME: fsl_alloc_request() ignores ep argument */
  2154. udc->status_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL),
  2155. struct fsl_req, req);
  2156. /* allocate a small amount of memory to get valid address */
  2157. udc->status_req->req.buf = kmalloc(8, GFP_KERNEL);
  2158. udc->status_req->req.dma = virt_to_phys(udc->status_req->req.buf);
  2159. /* Initialize ep0 data request structure */
  2160. udc->data_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL),
  2161. struct fsl_req, req);
  2162. udc->data_req->req.buf = kmalloc(8, GFP_KERNEL);
  2163. udc->data_req->req.dma = virt_to_phys(udc->data_req->req.buf);
  2164. udc->resume_state = USB_STATE_NOTATTACHED;
  2165. udc->usb_state = USB_STATE_POWERED;
  2166. udc->ep0_dir = 0;
  2167. udc->remote_wakeup = 0; /* default to 0 on reset */
  2168. spin_lock_init(&udc->lock);
  2169. return 0;
  2170. }
  2171. /*----------------------------------------------------------------
  2172. * Setup the fsl_ep struct for eps
  2173. * Link fsl_ep->ep to gadget->ep_list
  2174. * ep0out is not used so do nothing here
  2175. * ep0in should be taken care
  2176. *--------------------------------------------------------------*/
  2177. static int __init struct_ep_setup(struct fsl_udc *udc, unsigned char index,
  2178. char *name, int link)
  2179. {
  2180. struct fsl_ep *ep = &udc->eps[index];
  2181. ep->udc = udc;
  2182. strcpy(ep->name, name);
  2183. ep->ep.name = ep->name;
  2184. ep->ep.ops = &fsl_ep_ops;
  2185. /*
  2186. * For ep0, the endpoint is enabled after controller initialization
  2187. * For non-ep0, the endpoint is stopped default, and will be enabled
  2188. * by class driver when needed.
  2189. */
  2190. if (index)
  2191. ep->stopped = 1;
  2192. else
  2193. ep->stopped = 0;
  2194. /* for ep0: maxP defined in desc
  2195. * for other eps, maxP is set by epautoconfig() called by gadget layer
  2196. */
  2197. ep->ep.maxpacket = (unsigned short) ~0;
  2198. /* the queue lists any req for this ep */
  2199. INIT_LIST_HEAD(&ep->queue);
  2200. /* gagdet.ep_list used for ep_autoconfig so no ep0 */
  2201. if (link)
  2202. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  2203. ep->gadget = &udc->gadget;
  2204. ep->qh = &udc->ep_qh[index];
  2205. return 0;
  2206. }
  2207. struct usb_dr_device *get_dr_regs(void)
  2208. {
  2209. int i;
  2210. struct usb_dr_device *dr_regs;
  2211. dr_regs = calloc(sizeof(*dr_regs), 1);
  2212. dr_regs->caplength = USB_CAPLENGTH;
  2213. dr_regs->hciversion = USB_HCIVERSION;
  2214. dr_regs->hcsparams = USB_HCSPARAMS;
  2215. dr_regs->hccparams = USB_HCCPARAMS;
  2216. dr_regs->dciversion = USB_DCIVERSION;
  2217. dr_regs->dccparams = USB_DCCPARAMS;
  2218. dr_regs->usbcmd = USB_USBCMD;
  2219. dr_regs->usbsts = USB_USBSTS;
  2220. dr_regs->usbintr = USB_USBINTR;
  2221. dr_regs->frindex = USB_FRINDEX;
  2222. dr_regs->deviceaddr = USB_DEVICEADDR;
  2223. dr_regs->endpointlistaddr = USB_ENDPOINTLISTADDR;
  2224. dr_regs->burstsize = USB_BURSTSIZE;
  2225. dr_regs->txttfilltuning = USB_TXFILLTUNING;
  2226. dr_regs->configflag = 0; /* TODO */
  2227. dr_regs->portsc1 = USB_PORTSC1;
  2228. dr_regs->otgsc = USB_OTGSC;
  2229. dr_regs->usbmode = USB_USBMODE;
  2230. dr_regs->endptsetupstat = USB_ENDPTSETUPSTAT;
  2231. dr_regs->endpointprime = USB_ENDPTPRIME;
  2232. dr_regs->endptflush = USB_ENDPTFLUSH;
  2233. dr_regs->endptstatus = USB_ENDPTSTAT;
  2234. dr_regs->endptcomplete = USB_ENDPTCOMPLETE;
  2235. for(i = 0; i < 6; i++)
  2236. dr_regs->endptctrl[i] = USB_ENDPTCTRL(i);
  2237. return dr_regs;
  2238. }
  2239. /* Driver probe function
  2240. * all intialization operations implemented here except enabling usb_intr reg
  2241. * board setup should have been done in the platform code
  2242. */
  2243. int fsl_udc_probe(struct fsl_usb2_platform_data *pdata)
  2244. {
  2245. int ret = -ENODEV;
  2246. unsigned int i;
  2247. u32 dccparams;
  2248. udc_controller = kzalloc(sizeof(struct fsl_udc), GFP_KERNEL);
  2249. if (udc_controller == NULL) {
  2250. ERR("malloc udc failed\n");
  2251. return -ENOMEM;
  2252. }
  2253. udc_controller->pdata = pdata;
  2254. #ifdef CONFIG_USB_OTG
  2255. /* Memory and interrupt resources will be passed from OTG */
  2256. udc_controller->transceiver = otg_get_transceiver();
  2257. if (!udc_controller->transceiver) {
  2258. printk(KERN_ERR "Can't find OTG driver!\n");
  2259. ret = -ENODEV;
  2260. goto err1a;
  2261. }
  2262. udc_controller->gadget.is_otg = 1;
  2263. #endif
  2264. dr_regs = (void *) OTG_BASE_ADDR;
  2265. if (!dr_regs) {
  2266. ret = -ENOMEM;
  2267. goto err1;
  2268. }
  2269. pdata->regs = (void *)dr_regs;
  2270. /*
  2271. * do platform specific init: check the clock, grab/config pins, etc.
  2272. */
  2273. set_usboh3_clk();
  2274. set_usb_phy1_clk();
  2275. enable_usboh3_clk(1);
  2276. enable_usb_phy1_clk(1);
  2277. spin_lock_init(&pdata->lock);
  2278. /* Due to mx35/mx25's phy's bug */
  2279. reset_phy();
  2280. if (pdata->have_sysif_regs)
  2281. usb_sys_regs = (struct usb_sys_interface *)
  2282. ((u32)dr_regs + USB_DR_SYS_OFFSET);
  2283. /* Read Device Controller Capability Parameters register */
  2284. dccparams = fsl_readl(&dr_regs->dccparams);
  2285. if (!(dccparams & DCCPARAMS_DC)) {
  2286. ERR("This SOC doesn't support device role\n");
  2287. ret = -ENODEV;
  2288. goto err2;
  2289. }
  2290. /* Get max device endpoints */
  2291. /* DEN is bidirectional ep number, max_ep doubles the number */
  2292. udc_controller->max_ep = (dccparams & DCCPARAMS_DEN_MASK) * 2;
  2293. /* Initialize the udc structure including QH member and other member */
  2294. if (struct_udc_setup(udc_controller, pdata)) {
  2295. ERR("Can't initialize udc data structure\n");
  2296. ret = -ENOMEM;
  2297. goto err3;
  2298. }
  2299. /* initialize usb hw reg except for regs for EP,
  2300. * leave usbintr reg untouched */
  2301. dr_controller_setup(udc_controller);
  2302. /* Setup gadget structure */
  2303. udc_controller->gadget.ops = &fsl_gadget_ops;
  2304. udc_controller->gadget.is_dualspeed = 1;
  2305. udc_controller->gadget.ep0 = &udc_controller->eps[0].ep;
  2306. INIT_LIST_HEAD(&udc_controller->gadget.ep_list);
  2307. udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
  2308. udc_controller->gadget.name = driver_name;
  2309. /* setup QH and epctrl for ep0 */
  2310. ep0_setup(udc_controller);
  2311. /* setup udc->eps[] for ep0 */
  2312. struct_ep_setup(udc_controller, 0, "ep0", 0);
  2313. /* for ep0: the desc defined here;
  2314. * for other eps, gadget layer called ep_enable with defined desc
  2315. */
  2316. udc_controller->eps[0].desc = &fsl_ep0_desc;
  2317. udc_controller->eps[0].ep.maxpacket = USB_MAX_CTRL_PAYLOAD;
  2318. /* setup the udc->eps[] for non-control endpoints and link
  2319. * to gadget.ep_list */
  2320. for (i = 1; i < (int)(udc_controller->max_ep / 2); i++) {
  2321. char name[14];
  2322. sprintf(name, "ep%dout", i);
  2323. struct_ep_setup(udc_controller, i * 2, name, 1);
  2324. sprintf(name, "ep%din", i);
  2325. struct_ep_setup(udc_controller, i * 2 + 1, name, 1);
  2326. }
  2327. if (g_iram_size) {
  2328. g_iram_addr = iram_alloc(USB_IRAM_SIZE, &g_iram_base);
  2329. for (i = 0; i < IRAM_PPH_NTD; i++) {
  2330. udc_controller->iram_buffer[i] =
  2331. g_iram_base + i * g_iram_size;
  2332. udc_controller->iram_buffer_v[i] =
  2333. g_iram_addr + i * g_iram_size;
  2334. }
  2335. }
  2336. #ifdef POSTPONE_FREE_LAST_DTD
  2337. last_free_td = NULL;
  2338. #endif
  2339. /* disable all INTR */
  2340. #ifndef CONFIG_USB_OTG
  2341. fsl_writel(0, &dr_regs->usbintr);
  2342. dr_wake_up_enable(udc_controller, false);
  2343. #else
  2344. dr_wake_up_enable(udc_controller, true);
  2345. #endif
  2346. /*
  2347. * As mx25/mx35 does not implement clk_gate, should not let phy to low
  2348. * power mode due to IC bug
  2349. */
  2350. #if !(defined CONFIG_ARCH_MX35 || defined CONFIG_ARCH_MX25)
  2351. {
  2352. dr_phy_low_power_mode(udc_controller, true);
  2353. }
  2354. #endif
  2355. udc_controller->stopped = 1;
  2356. /* let the gadget register function open the clk */
  2357. dr_clk_gate(false);
  2358. return 0;
  2359. err3:
  2360. err2:
  2361. if (pdata->exit)
  2362. pdata->exit(pdata);
  2363. err2a:
  2364. err1:
  2365. err1a:
  2366. kfree(udc_controller);
  2367. udc_controller = NULL;
  2368. return ret;
  2369. }
  2370. /* Driver removal function
  2371. * Free resources and finish pending transactions
  2372. */
  2373. static int fsl_udc_remove(struct fsl_usb2_platform_data *pdata)
  2374. {
  2375. u32 temp;
  2376. if (!udc_controller)
  2377. return -ENODEV;
  2378. /* open USB PHY clock */
  2379. dr_clk_gate(true);
  2380. /* disable wake up and otgsc interrupt for safely remove udc driver*/
  2381. temp = fsl_readl(&dr_regs->otgsc);
  2382. temp &= ~(0x7f << 24);
  2383. fsl_writel(temp, &dr_regs->otgsc);
  2384. dr_wake_up_enable(udc_controller, false);
  2385. dr_discharge_line(pdata, true);
  2386. /* DR has been stopped in usb_gadget_unregister_driver() */
  2387. /* Free allocated memory */
  2388. if (g_iram_size)
  2389. iram_free(g_iram_base, IRAM_PPH_NTD * g_iram_size);
  2390. kfree(udc_controller->status_req->req.buf);
  2391. kfree(udc_controller->status_req);
  2392. kfree(udc_controller->data_req->req.buf);
  2393. kfree(udc_controller->data_req);
  2394. kfree(udc_controller->eps);
  2395. #ifdef CONFIG_IMX_USB_CHARGER
  2396. imx_usb_remove_charger(&udc_controller->charger);
  2397. #endif
  2398. /*
  2399. * do platform specific un-initialization:
  2400. * release iomux pins, etc.
  2401. */
  2402. if (pdata->exit)
  2403. pdata->exit(pdata);
  2404. return 0;
  2405. }
  2406. static bool udc_can_wakeup_system(void)
  2407. {
  2408. return false;
  2409. }
  2410. static int udc_suspend(struct fsl_udc *udc)
  2411. {
  2412. struct fsl_usb2_platform_data *pdata = udc_controller->pdata;
  2413. u32 mode, usbcmd;
  2414. /*
  2415. * When it is the PM suspend routine and the device has no
  2416. * abilities to wakeup system, it should not set wakeup enable.
  2417. * Otherwise, the system will wakeup even the user only wants to
  2418. * charge using usb
  2419. */
  2420. if (pdata->pmflags == 0) {
  2421. if (!udc_can_wakeup_system()) {
  2422. dr_wake_up_enable(udc, false);
  2423. } else {
  2424. if (pdata->platform_phy_power_on)
  2425. pdata->platform_phy_power_on();
  2426. dr_wake_up_enable(udc, true);
  2427. }
  2428. }
  2429. /*
  2430. * If the controller is already stopped, then this must be a
  2431. * PM suspend. Remember this fact, so that we will leave the
  2432. * controller stopped at PM resume time.
  2433. */
  2434. if (udc->suspended) {
  2435. printk(KERN_DEBUG "gadget already suspended, leaving early\n");
  2436. goto out;
  2437. }
  2438. mode = fsl_readl(&dr_regs->usbmode) & USB_MODE_CTRL_MODE_MASK;
  2439. usbcmd = fsl_readl(&dr_regs->usbcmd);
  2440. if (mode != USB_MODE_CTRL_MODE_DEVICE) {
  2441. printk(KERN_DEBUG "gadget not in device mode, leaving early\n");
  2442. goto out;
  2443. }
  2444. if (!(fsl_readl(&dr_regs->otgsc) & OTGSC_A_BUS_VALID)) {
  2445. /* stop the controller */
  2446. usbcmd = fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP;
  2447. fsl_writel(usbcmd, &dr_regs->usbcmd);
  2448. udc->stopped = 1;
  2449. }
  2450. dr_phy_low_power_mode(udc, true);
  2451. out:
  2452. if (udc->suspended > 1) {
  2453. pr_warning(
  2454. "It's the case usb device is on otg port\
  2455. and the gadget driver"
  2456. "is loaded during boots up\n"
  2457. "So, do not increase suspended counter Or\
  2458. there is a error, "
  2459. "please debug it !!!\n"
  2460. );
  2461. return 0;
  2462. }
  2463. udc->suspended++;
  2464. return 0;
  2465. }
  2466. /*-----------------------------------------------------------------
  2467. * Modify Power management attributes
  2468. * Used by OTG statemachine to disable gadget temporarily
  2469. -----------------------------------------------------------------*/
  2470. static int fsl_udc_suspend(struct fsl_usb2_platform_data *pdata, pm_message_t state)
  2471. {
  2472. int ret;
  2473. printk(KERN_DEBUG "udc suspend begins\n");
  2474. if (get_gadget_data(&udc_controller->gadget) == NULL) {
  2475. /* if no gadget is binded, quit */
  2476. return 0;
  2477. }
  2478. if (udc_controller->stopped)
  2479. dr_clk_gate(true);
  2480. if (((!(udc_controller->gadget.is_otg)) ||
  2481. (fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID)) &&
  2482. (udc_controller->usb_state > USB_STATE_POWERED) &&
  2483. (udc_controller->usb_state < USB_STATE_SUSPENDED)) {
  2484. return -EBUSY;/* keep the clk on */
  2485. } else
  2486. ret = udc_suspend(udc_controller);
  2487. dr_clk_gate(false);
  2488. printk(KERN_DEBUG "USB Gadget suspend ends\n");
  2489. return ret;
  2490. }
  2491. /*-----------------------------------------------------------------
  2492. * Invoked on USB resume. May be called in_interrupt.
  2493. * Here we start the DR controller and enable the irq
  2494. *-----------------------------------------------------------------*/
  2495. static int fsl_udc_resume(void)
  2496. {
  2497. struct fsl_usb2_platform_data *pdata = udc_controller->pdata;
  2498. struct fsl_usb2_wakeup_platform_data *wake_up_pdata = pdata->wakeup_pdata;
  2499. printk(KERN_DEBUG "USB Gadget resume begins\n");
  2500. if (get_gadget_data(&udc_controller->gadget) == NULL) {
  2501. /* if no gadget is binded, quit */
  2502. return 0;
  2503. }
  2504. mutex_lock(&udc_resume_mutex);
  2505. pr_debug("%s(): stopped %d suspended %d\n", __func__,
  2506. udc_controller->stopped, udc_controller->suspended);
  2507. /* Do noop if the udc is already at resume state */
  2508. if (udc_controller->suspended == 0) {
  2509. u32 temp;
  2510. if (udc_controller->stopped)
  2511. dr_clk_gate(true);
  2512. mdelay(3);
  2513. if (fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID) {
  2514. temp = fsl_readl(&dr_regs->otgsc);
  2515. /* if b_session_irq_en is cleared by otg */
  2516. if (!(temp & OTGSC_B_SESSION_VALID_IRQ_EN)) {
  2517. temp |= OTGSC_B_SESSION_VALID_IRQ_EN;
  2518. fsl_writel(temp, &dr_regs->otgsc);
  2519. }
  2520. }
  2521. if (udc_controller->stopped)
  2522. dr_clk_gate(false);
  2523. mutex_unlock(&udc_resume_mutex);
  2524. return 0;
  2525. }
  2526. /*
  2527. * If the controller was stopped at suspend time, then
  2528. * don't resume it now.
  2529. */
  2530. if (udc_controller->suspended > 1) {
  2531. printk(KERN_DEBUG "gadget was already stopped, leaving early\n");
  2532. if (udc_controller->stopped) {
  2533. dr_clk_gate(true);
  2534. }
  2535. goto end;
  2536. }
  2537. /*
  2538. * To fix suspend issue connected to usb charger,if stopped is 0
  2539. * suspended is 1,clock on and out of low power mode to avoid
  2540. * next system suspend no clock to cause system hang.
  2541. */
  2542. if (udc_controller->suspended && !udc_controller->stopped) {
  2543. dr_clk_gate(true);
  2544. dr_wake_up_enable(udc_controller, false);
  2545. dr_phy_low_power_mode(udc_controller, false);
  2546. }
  2547. /* Enable DR irq reg and set controller Run */
  2548. if (udc_controller->stopped) {
  2549. /* the clock is already on at usb wakeup routine */
  2550. if (pdata->lowpower)
  2551. dr_clk_gate(true);
  2552. dr_wake_up_enable(udc_controller, false);
  2553. dr_phy_low_power_mode(udc_controller, false);
  2554. mdelay(3);
  2555. /* if in host mode, we need to do nothing */
  2556. if ((fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID) == 0) {
  2557. dr_phy_low_power_mode(udc_controller, true);
  2558. dr_wake_up_enable(udc_controller, true);
  2559. goto end;
  2560. }
  2561. dr_controller_setup(udc_controller);
  2562. dr_controller_run(udc_controller);
  2563. }
  2564. end:
  2565. /* if udc is resume by otg id change and no device
  2566. * connecting to the otg, otg will enter low power mode*/
  2567. if (udc_controller->stopped) {
  2568. /*
  2569. * If it is PM resume routine, the udc is at low power mode,
  2570. * and the udc has no abilities to wakeup system, it should
  2571. * set the abilities to wakeup itself. Otherwise, the usb
  2572. * subsystem will not leave from low power mode.
  2573. */
  2574. if (!udc_can_wakeup_system() &&
  2575. (pdata->pmflags == 0)) {
  2576. dr_wake_up_enable(udc_controller, true);
  2577. }
  2578. dr_clk_gate(false);
  2579. }
  2580. --udc_controller->suspended;
  2581. mutex_unlock(&udc_resume_mutex);
  2582. printk(KERN_DEBUG "USB Gadget resume ends\n");
  2583. return 0;
  2584. }
  2585. int usb_gadget_handle_interrupts(void)
  2586. {
  2587. return fsl_udc_irq(0, udc_controller);
  2588. }